PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/gfs2/xattr.c

https://bitbucket.org/emiliolopez/linux
C | 1485 lines | 1090 code | 275 blank | 120 comment | 208 complexity | 330ff58367145d461adf833fa804903d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/xattr.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/posix_acl_xattr.h>
  16. #include <linux/uaccess.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "acl.h"
  20. #include "xattr.h"
  21. #include "glock.h"
  22. #include "inode.h"
  23. #include "meta_io.h"
  24. #include "quota.h"
  25. #include "rgrp.h"
  26. #include "super.h"
  27. #include "trans.h"
  28. #include "util.h"
  29. /**
  30. * ea_calc_size - returns the acutal number of bytes the request will take up
  31. * (not counting any unstuffed data blocks)
  32. * @sdp:
  33. * @er:
  34. * @size:
  35. *
  36. * Returns: 1 if the EA should be stuffed
  37. */
  38. static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize,
  39. unsigned int *size)
  40. {
  41. unsigned int jbsize = sdp->sd_jbsize;
  42. /* Stuffed */
  43. *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8);
  44. if (*size <= jbsize)
  45. return 1;
  46. /* Unstuffed */
  47. *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize +
  48. (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8);
  49. return 0;
  50. }
  51. static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize)
  52. {
  53. unsigned int size;
  54. if (dsize > GFS2_EA_MAX_DATA_LEN)
  55. return -ERANGE;
  56. ea_calc_size(sdp, nsize, dsize, &size);
  57. /* This can only happen with 512 byte blocks */
  58. if (size > sdp->sd_jbsize)
  59. return -ERANGE;
  60. return 0;
  61. }
  62. typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
  63. struct gfs2_ea_header *ea,
  64. struct gfs2_ea_header *prev, void *private);
  65. static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
  66. ea_call_t ea_call, void *data)
  67. {
  68. struct gfs2_ea_header *ea, *prev = NULL;
  69. int error = 0;
  70. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
  71. return -EIO;
  72. for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
  73. if (!GFS2_EA_REC_LEN(ea))
  74. goto fail;
  75. if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
  76. bh->b_data + bh->b_size))
  77. goto fail;
  78. if (!GFS2_EATYPE_VALID(ea->ea_type))
  79. goto fail;
  80. error = ea_call(ip, bh, ea, prev, data);
  81. if (error)
  82. return error;
  83. if (GFS2_EA_IS_LAST(ea)) {
  84. if ((char *)GFS2_EA2NEXT(ea) !=
  85. bh->b_data + bh->b_size)
  86. goto fail;
  87. break;
  88. }
  89. }
  90. return error;
  91. fail:
  92. gfs2_consist_inode(ip);
  93. return -EIO;
  94. }
  95. static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
  96. {
  97. struct buffer_head *bh, *eabh;
  98. __be64 *eablk, *end;
  99. int error;
  100. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &bh);
  101. if (error)
  102. return error;
  103. if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
  104. error = ea_foreach_i(ip, bh, ea_call, data);
  105. goto out;
  106. }
  107. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
  108. error = -EIO;
  109. goto out;
  110. }
  111. eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
  112. end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
  113. for (; eablk < end; eablk++) {
  114. u64 bn;
  115. if (!*eablk)
  116. break;
  117. bn = be64_to_cpu(*eablk);
  118. error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, 0, &eabh);
  119. if (error)
  120. break;
  121. error = ea_foreach_i(ip, eabh, ea_call, data);
  122. brelse(eabh);
  123. if (error)
  124. break;
  125. }
  126. out:
  127. brelse(bh);
  128. return error;
  129. }
  130. struct ea_find {
  131. int type;
  132. const char *name;
  133. size_t namel;
  134. struct gfs2_ea_location *ef_el;
  135. };
  136. static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
  137. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  138. void *private)
  139. {
  140. struct ea_find *ef = private;
  141. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  142. return 0;
  143. if (ea->ea_type == ef->type) {
  144. if (ea->ea_name_len == ef->namel &&
  145. !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) {
  146. struct gfs2_ea_location *el = ef->ef_el;
  147. get_bh(bh);
  148. el->el_bh = bh;
  149. el->el_ea = ea;
  150. el->el_prev = prev;
  151. return 1;
  152. }
  153. }
  154. return 0;
  155. }
  156. static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name,
  157. struct gfs2_ea_location *el)
  158. {
  159. struct ea_find ef;
  160. int error;
  161. ef.type = type;
  162. ef.name = name;
  163. ef.namel = strlen(name);
  164. ef.ef_el = el;
  165. memset(el, 0, sizeof(struct gfs2_ea_location));
  166. error = ea_foreach(ip, ea_find_i, &ef);
  167. if (error > 0)
  168. return 0;
  169. return error;
  170. }
  171. /**
  172. * ea_dealloc_unstuffed -
  173. * @ip:
  174. * @bh:
  175. * @ea:
  176. * @prev:
  177. * @private:
  178. *
  179. * Take advantage of the fact that all unstuffed blocks are
  180. * allocated from the same RG. But watch, this may not always
  181. * be true.
  182. *
  183. * Returns: errno
  184. */
  185. static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  186. struct gfs2_ea_header *ea,
  187. struct gfs2_ea_header *prev, void *private)
  188. {
  189. int *leave = private;
  190. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  191. struct gfs2_rgrpd *rgd;
  192. struct gfs2_holder rg_gh;
  193. __be64 *dataptrs;
  194. u64 bn = 0;
  195. u64 bstart = 0;
  196. unsigned int blen = 0;
  197. unsigned int blks = 0;
  198. unsigned int x;
  199. int error;
  200. error = gfs2_rindex_update(sdp);
  201. if (error)
  202. return error;
  203. if (GFS2_EA_IS_STUFFED(ea))
  204. return 0;
  205. dataptrs = GFS2_EA2DATAPTRS(ea);
  206. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  207. if (*dataptrs) {
  208. blks++;
  209. bn = be64_to_cpu(*dataptrs);
  210. }
  211. }
  212. if (!blks)
  213. return 0;
  214. rgd = gfs2_blk2rgrpd(sdp, bn, 1);
  215. if (!rgd) {
  216. gfs2_consist_inode(ip);
  217. return -EIO;
  218. }
  219. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
  220. if (error)
  221. return error;
  222. error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
  223. RES_EATTR + RES_STATFS + RES_QUOTA, blks);
  224. if (error)
  225. goto out_gunlock;
  226. gfs2_trans_add_meta(ip->i_gl, bh);
  227. dataptrs = GFS2_EA2DATAPTRS(ea);
  228. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  229. if (!*dataptrs)
  230. break;
  231. bn = be64_to_cpu(*dataptrs);
  232. if (bstart + blen == bn)
  233. blen++;
  234. else {
  235. if (bstart)
  236. gfs2_free_meta(ip, bstart, blen);
  237. bstart = bn;
  238. blen = 1;
  239. }
  240. *dataptrs = 0;
  241. gfs2_add_inode_blocks(&ip->i_inode, -1);
  242. }
  243. if (bstart)
  244. gfs2_free_meta(ip, bstart, blen);
  245. if (prev && !leave) {
  246. u32 len;
  247. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  248. prev->ea_rec_len = cpu_to_be32(len);
  249. if (GFS2_EA_IS_LAST(ea))
  250. prev->ea_flags |= GFS2_EAFLAG_LAST;
  251. } else {
  252. ea->ea_type = GFS2_EATYPE_UNUSED;
  253. ea->ea_num_ptrs = 0;
  254. }
  255. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  256. __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  257. gfs2_trans_end(sdp);
  258. out_gunlock:
  259. gfs2_glock_dq_uninit(&rg_gh);
  260. return error;
  261. }
  262. static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  263. struct gfs2_ea_header *ea,
  264. struct gfs2_ea_header *prev, int leave)
  265. {
  266. int error;
  267. error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
  268. if (error)
  269. return error;
  270. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  271. if (error)
  272. goto out_alloc;
  273. error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
  274. gfs2_quota_unhold(ip);
  275. out_alloc:
  276. return error;
  277. }
  278. struct ea_list {
  279. struct gfs2_ea_request *ei_er;
  280. unsigned int ei_size;
  281. };
  282. static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea)
  283. {
  284. switch (ea->ea_type) {
  285. case GFS2_EATYPE_USR:
  286. return 5 + ea->ea_name_len + 1;
  287. case GFS2_EATYPE_SYS:
  288. return 7 + ea->ea_name_len + 1;
  289. case GFS2_EATYPE_SECURITY:
  290. return 9 + ea->ea_name_len + 1;
  291. default:
  292. return 0;
  293. }
  294. }
  295. static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
  296. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  297. void *private)
  298. {
  299. struct ea_list *ei = private;
  300. struct gfs2_ea_request *er = ei->ei_er;
  301. unsigned int ea_size = gfs2_ea_strlen(ea);
  302. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  303. return 0;
  304. if (er->er_data_len) {
  305. char *prefix = NULL;
  306. unsigned int l = 0;
  307. char c = 0;
  308. if (ei->ei_size + ea_size > er->er_data_len)
  309. return -ERANGE;
  310. switch (ea->ea_type) {
  311. case GFS2_EATYPE_USR:
  312. prefix = "user.";
  313. l = 5;
  314. break;
  315. case GFS2_EATYPE_SYS:
  316. prefix = "system.";
  317. l = 7;
  318. break;
  319. case GFS2_EATYPE_SECURITY:
  320. prefix = "security.";
  321. l = 9;
  322. break;
  323. }
  324. BUG_ON(l == 0);
  325. memcpy(er->er_data + ei->ei_size, prefix, l);
  326. memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
  327. ea->ea_name_len);
  328. memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
  329. }
  330. ei->ei_size += ea_size;
  331. return 0;
  332. }
  333. /**
  334. * gfs2_listxattr - List gfs2 extended attributes
  335. * @dentry: The dentry whose inode we are interested in
  336. * @buffer: The buffer to write the results
  337. * @size: The size of the buffer
  338. *
  339. * Returns: actual size of data on success, -errno on error
  340. */
  341. ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  342. {
  343. struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
  344. struct gfs2_ea_request er;
  345. struct gfs2_holder i_gh;
  346. int error;
  347. memset(&er, 0, sizeof(struct gfs2_ea_request));
  348. if (size) {
  349. er.er_data = buffer;
  350. er.er_data_len = size;
  351. }
  352. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  353. if (error)
  354. return error;
  355. if (ip->i_eattr) {
  356. struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
  357. error = ea_foreach(ip, ea_list_i, &ei);
  358. if (!error)
  359. error = ei.ei_size;
  360. }
  361. gfs2_glock_dq_uninit(&i_gh);
  362. return error;
  363. }
  364. /**
  365. * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
  366. * request buffer
  367. * @ip: The GFS2 inode
  368. * @ea: The extended attribute header structure
  369. * @din: The data to be copied in
  370. * @dout: The data to be copied out (one of din,dout will be NULL)
  371. *
  372. * Returns: errno
  373. */
  374. static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  375. const char *din, char *dout)
  376. {
  377. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  378. struct buffer_head **bh;
  379. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  380. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  381. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  382. unsigned int x;
  383. int error = 0;
  384. unsigned char *pos;
  385. unsigned cp_size;
  386. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  387. if (!bh)
  388. return -ENOMEM;
  389. for (x = 0; x < nptrs; x++) {
  390. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0,
  391. bh + x);
  392. if (error) {
  393. while (x--)
  394. brelse(bh[x]);
  395. goto out;
  396. }
  397. dataptrs++;
  398. }
  399. for (x = 0; x < nptrs; x++) {
  400. error = gfs2_meta_wait(sdp, bh[x]);
  401. if (error) {
  402. for (; x < nptrs; x++)
  403. brelse(bh[x]);
  404. goto out;
  405. }
  406. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  407. for (; x < nptrs; x++)
  408. brelse(bh[x]);
  409. error = -EIO;
  410. goto out;
  411. }
  412. pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
  413. cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
  414. if (dout) {
  415. memcpy(dout, pos, cp_size);
  416. dout += sdp->sd_jbsize;
  417. }
  418. if (din) {
  419. gfs2_trans_add_meta(ip->i_gl, bh[x]);
  420. memcpy(pos, din, cp_size);
  421. din += sdp->sd_jbsize;
  422. }
  423. amount -= sdp->sd_jbsize;
  424. brelse(bh[x]);
  425. }
  426. out:
  427. kfree(bh);
  428. return error;
  429. }
  430. static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  431. char *data, size_t size)
  432. {
  433. int ret;
  434. size_t len = GFS2_EA_DATA_LEN(el->el_ea);
  435. if (len > size)
  436. return -ERANGE;
  437. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  438. memcpy(data, GFS2_EA2DATA(el->el_ea), len);
  439. return len;
  440. }
  441. ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
  442. if (ret < 0)
  443. return ret;
  444. return len;
  445. }
  446. int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
  447. {
  448. struct gfs2_ea_location el;
  449. int error;
  450. int len;
  451. char *data;
  452. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
  453. if (error)
  454. return error;
  455. if (!el.el_ea)
  456. goto out;
  457. if (!GFS2_EA_DATA_LEN(el.el_ea))
  458. goto out;
  459. len = GFS2_EA_DATA_LEN(el.el_ea);
  460. data = kmalloc(len, GFP_NOFS);
  461. error = -ENOMEM;
  462. if (data == NULL)
  463. goto out;
  464. error = gfs2_ea_get_copy(ip, &el, data, len);
  465. if (error < 0)
  466. kfree(data);
  467. else
  468. *ppdata = data;
  469. out:
  470. brelse(el.el_bh);
  471. return error;
  472. }
  473. /**
  474. * gfs2_xattr_get - Get a GFS2 extended attribute
  475. * @inode: The inode
  476. * @name: The name of the extended attribute
  477. * @buffer: The buffer to write the result into
  478. * @size: The size of the buffer
  479. * @type: The type of extended attribute
  480. *
  481. * Returns: actual size of data on success, -errno on error
  482. */
  483. static int __gfs2_xattr_get(struct inode *inode, const char *name,
  484. void *buffer, size_t size, int type)
  485. {
  486. struct gfs2_inode *ip = GFS2_I(inode);
  487. struct gfs2_ea_location el;
  488. int error;
  489. if (!ip->i_eattr)
  490. return -ENODATA;
  491. if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
  492. return -EINVAL;
  493. error = gfs2_ea_find(ip, type, name, &el);
  494. if (error)
  495. return error;
  496. if (!el.el_ea)
  497. return -ENODATA;
  498. if (size)
  499. error = gfs2_ea_get_copy(ip, &el, buffer, size);
  500. else
  501. error = GFS2_EA_DATA_LEN(el.el_ea);
  502. brelse(el.el_bh);
  503. return error;
  504. }
  505. static int gfs2_xattr_get(const struct xattr_handler *handler,
  506. struct dentry *unused, struct inode *inode,
  507. const char *name, void *buffer, size_t size)
  508. {
  509. struct gfs2_inode *ip = GFS2_I(inode);
  510. struct gfs2_holder gh;
  511. int ret;
  512. /* During lookup, SELinux calls this function with the glock locked. */
  513. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  514. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  515. if (ret)
  516. return ret;
  517. } else {
  518. gfs2_holder_mark_uninitialized(&gh);
  519. }
  520. ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags);
  521. if (gfs2_holder_initialized(&gh))
  522. gfs2_glock_dq_uninit(&gh);
  523. return ret;
  524. }
  525. /**
  526. * ea_alloc_blk - allocates a new block for extended attributes.
  527. * @ip: A pointer to the inode that's getting extended attributes
  528. * @bhp: Pointer to pointer to a struct buffer_head
  529. *
  530. * Returns: errno
  531. */
  532. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  533. {
  534. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  535. struct gfs2_ea_header *ea;
  536. unsigned int n = 1;
  537. u64 block;
  538. int error;
  539. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  540. if (error)
  541. return error;
  542. gfs2_trans_add_unrevoke(sdp, block, 1);
  543. *bhp = gfs2_meta_new(ip->i_gl, block);
  544. gfs2_trans_add_meta(ip->i_gl, *bhp);
  545. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  546. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  547. ea = GFS2_EA_BH2FIRST(*bhp);
  548. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  549. ea->ea_type = GFS2_EATYPE_UNUSED;
  550. ea->ea_flags = GFS2_EAFLAG_LAST;
  551. ea->ea_num_ptrs = 0;
  552. gfs2_add_inode_blocks(&ip->i_inode, 1);
  553. return 0;
  554. }
  555. /**
  556. * ea_write - writes the request info to an ea, creating new blocks if
  557. * necessary
  558. * @ip: inode that is being modified
  559. * @ea: the location of the new ea in a block
  560. * @er: the write request
  561. *
  562. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  563. *
  564. * returns : errno
  565. */
  566. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  567. struct gfs2_ea_request *er)
  568. {
  569. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  570. int error;
  571. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  572. ea->ea_name_len = er->er_name_len;
  573. ea->ea_type = er->er_type;
  574. ea->__pad = 0;
  575. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  576. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  577. ea->ea_num_ptrs = 0;
  578. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  579. } else {
  580. __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
  581. const char *data = er->er_data;
  582. unsigned int data_len = er->er_data_len;
  583. unsigned int copy;
  584. unsigned int x;
  585. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  586. for (x = 0; x < ea->ea_num_ptrs; x++) {
  587. struct buffer_head *bh;
  588. u64 block;
  589. int mh_size = sizeof(struct gfs2_meta_header);
  590. unsigned int n = 1;
  591. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  592. if (error)
  593. return error;
  594. gfs2_trans_add_unrevoke(sdp, block, 1);
  595. bh = gfs2_meta_new(ip->i_gl, block);
  596. gfs2_trans_add_meta(ip->i_gl, bh);
  597. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  598. gfs2_add_inode_blocks(&ip->i_inode, 1);
  599. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  600. data_len;
  601. memcpy(bh->b_data + mh_size, data, copy);
  602. if (copy < sdp->sd_jbsize)
  603. memset(bh->b_data + mh_size + copy, 0,
  604. sdp->sd_jbsize - copy);
  605. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  606. data += copy;
  607. data_len -= copy;
  608. brelse(bh);
  609. }
  610. gfs2_assert_withdraw(sdp, !data_len);
  611. }
  612. return 0;
  613. }
  614. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  615. struct gfs2_ea_request *er, void *private);
  616. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  617. unsigned int blks,
  618. ea_skeleton_call_t skeleton_call, void *private)
  619. {
  620. struct gfs2_alloc_parms ap = { .target = blks };
  621. int error;
  622. error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
  623. if (error)
  624. return error;
  625. error = gfs2_quota_lock_check(ip, &ap);
  626. if (error)
  627. return error;
  628. error = gfs2_inplace_reserve(ip, &ap);
  629. if (error)
  630. goto out_gunlock_q;
  631. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  632. blks + gfs2_rg_blocks(ip, blks) +
  633. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  634. if (error)
  635. goto out_ipres;
  636. error = skeleton_call(ip, er, private);
  637. if (error)
  638. goto out_end_trans;
  639. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  640. __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  641. out_end_trans:
  642. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  643. out_ipres:
  644. gfs2_inplace_release(ip);
  645. out_gunlock_q:
  646. gfs2_quota_unlock(ip);
  647. return error;
  648. }
  649. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  650. void *private)
  651. {
  652. struct buffer_head *bh;
  653. int error;
  654. error = ea_alloc_blk(ip, &bh);
  655. if (error)
  656. return error;
  657. ip->i_eattr = bh->b_blocknr;
  658. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  659. brelse(bh);
  660. return error;
  661. }
  662. /**
  663. * ea_init - initializes a new eattr block
  664. * @ip:
  665. * @er:
  666. *
  667. * Returns: errno
  668. */
  669. static int ea_init(struct gfs2_inode *ip, int type, const char *name,
  670. const void *data, size_t size)
  671. {
  672. struct gfs2_ea_request er;
  673. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  674. unsigned int blks = 1;
  675. er.er_type = type;
  676. er.er_name = name;
  677. er.er_name_len = strlen(name);
  678. er.er_data = (void *)data;
  679. er.er_data_len = size;
  680. if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
  681. blks += DIV_ROUND_UP(er.er_data_len, jbsize);
  682. return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
  683. }
  684. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  685. {
  686. u32 ea_size = GFS2_EA_SIZE(ea);
  687. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  688. ea_size);
  689. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  690. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  691. ea->ea_rec_len = cpu_to_be32(ea_size);
  692. ea->ea_flags ^= last;
  693. new->ea_rec_len = cpu_to_be32(new_size);
  694. new->ea_flags = last;
  695. return new;
  696. }
  697. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  698. struct gfs2_ea_location *el)
  699. {
  700. struct gfs2_ea_header *ea = el->el_ea;
  701. struct gfs2_ea_header *prev = el->el_prev;
  702. u32 len;
  703. gfs2_trans_add_meta(ip->i_gl, el->el_bh);
  704. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  705. ea->ea_type = GFS2_EATYPE_UNUSED;
  706. return;
  707. } else if (GFS2_EA2NEXT(prev) != ea) {
  708. prev = GFS2_EA2NEXT(prev);
  709. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  710. }
  711. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  712. prev->ea_rec_len = cpu_to_be32(len);
  713. if (GFS2_EA_IS_LAST(ea))
  714. prev->ea_flags |= GFS2_EAFLAG_LAST;
  715. }
  716. struct ea_set {
  717. int ea_split;
  718. struct gfs2_ea_request *es_er;
  719. struct gfs2_ea_location *es_el;
  720. struct buffer_head *es_bh;
  721. struct gfs2_ea_header *es_ea;
  722. };
  723. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  724. struct gfs2_ea_header *ea, struct ea_set *es)
  725. {
  726. struct gfs2_ea_request *er = es->es_er;
  727. int error;
  728. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  729. if (error)
  730. return error;
  731. gfs2_trans_add_meta(ip->i_gl, bh);
  732. if (es->ea_split)
  733. ea = ea_split_ea(ea);
  734. ea_write(ip, ea, er);
  735. if (es->es_el)
  736. ea_set_remove_stuffed(ip, es->es_el);
  737. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  738. __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  739. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  740. return error;
  741. }
  742. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  743. struct gfs2_ea_request *er, void *private)
  744. {
  745. struct ea_set *es = private;
  746. struct gfs2_ea_header *ea = es->es_ea;
  747. int error;
  748. gfs2_trans_add_meta(ip->i_gl, es->es_bh);
  749. if (es->ea_split)
  750. ea = ea_split_ea(ea);
  751. error = ea_write(ip, ea, er);
  752. if (error)
  753. return error;
  754. if (es->es_el)
  755. ea_set_remove_stuffed(ip, es->es_el);
  756. return 0;
  757. }
  758. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  759. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  760. void *private)
  761. {
  762. struct ea_set *es = private;
  763. unsigned int size;
  764. int stuffed;
  765. int error;
  766. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
  767. es->es_er->er_data_len, &size);
  768. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  769. if (GFS2_EA_REC_LEN(ea) < size)
  770. return 0;
  771. if (!GFS2_EA_IS_STUFFED(ea)) {
  772. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  773. if (error)
  774. return error;
  775. }
  776. es->ea_split = 0;
  777. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  778. es->ea_split = 1;
  779. else
  780. return 0;
  781. if (stuffed) {
  782. error = ea_set_simple_noalloc(ip, bh, ea, es);
  783. if (error)
  784. return error;
  785. } else {
  786. unsigned int blks;
  787. es->es_bh = bh;
  788. es->es_ea = ea;
  789. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  790. GFS2_SB(&ip->i_inode)->sd_jbsize);
  791. error = ea_alloc_skeleton(ip, es->es_er, blks,
  792. ea_set_simple_alloc, es);
  793. if (error)
  794. return error;
  795. }
  796. return 1;
  797. }
  798. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  799. void *private)
  800. {
  801. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  802. struct buffer_head *indbh, *newbh;
  803. __be64 *eablk;
  804. int error;
  805. int mh_size = sizeof(struct gfs2_meta_header);
  806. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  807. __be64 *end;
  808. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0,
  809. &indbh);
  810. if (error)
  811. return error;
  812. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  813. error = -EIO;
  814. goto out;
  815. }
  816. eablk = (__be64 *)(indbh->b_data + mh_size);
  817. end = eablk + sdp->sd_inptrs;
  818. for (; eablk < end; eablk++)
  819. if (!*eablk)
  820. break;
  821. if (eablk == end) {
  822. error = -ENOSPC;
  823. goto out;
  824. }
  825. gfs2_trans_add_meta(ip->i_gl, indbh);
  826. } else {
  827. u64 blk;
  828. unsigned int n = 1;
  829. error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
  830. if (error)
  831. return error;
  832. gfs2_trans_add_unrevoke(sdp, blk, 1);
  833. indbh = gfs2_meta_new(ip->i_gl, blk);
  834. gfs2_trans_add_meta(ip->i_gl, indbh);
  835. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  836. gfs2_buffer_clear_tail(indbh, mh_size);
  837. eablk = (__be64 *)(indbh->b_data + mh_size);
  838. *eablk = cpu_to_be64(ip->i_eattr);
  839. ip->i_eattr = blk;
  840. ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
  841. gfs2_add_inode_blocks(&ip->i_inode, 1);
  842. eablk++;
  843. }
  844. error = ea_alloc_blk(ip, &newbh);
  845. if (error)
  846. goto out;
  847. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  848. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  849. brelse(newbh);
  850. if (error)
  851. goto out;
  852. if (private)
  853. ea_set_remove_stuffed(ip, private);
  854. out:
  855. brelse(indbh);
  856. return error;
  857. }
  858. static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
  859. const void *value, size_t size, struct gfs2_ea_location *el)
  860. {
  861. struct gfs2_ea_request er;
  862. struct ea_set es;
  863. unsigned int blks = 2;
  864. int error;
  865. er.er_type = type;
  866. er.er_name = name;
  867. er.er_data = (void *)value;
  868. er.er_name_len = strlen(name);
  869. er.er_data_len = size;
  870. memset(&es, 0, sizeof(struct ea_set));
  871. es.es_er = &er;
  872. es.es_el = el;
  873. error = ea_foreach(ip, ea_set_simple, &es);
  874. if (error > 0)
  875. return 0;
  876. if (error)
  877. return error;
  878. if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
  879. blks++;
  880. if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  881. blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  882. return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
  883. }
  884. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  885. struct gfs2_ea_location *el)
  886. {
  887. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  888. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  889. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  890. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  891. }
  892. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
  893. }
  894. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  895. {
  896. struct gfs2_ea_header *ea = el->el_ea;
  897. struct gfs2_ea_header *prev = el->el_prev;
  898. int error;
  899. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  900. if (error)
  901. return error;
  902. gfs2_trans_add_meta(ip->i_gl, el->el_bh);
  903. if (prev) {
  904. u32 len;
  905. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  906. prev->ea_rec_len = cpu_to_be32(len);
  907. if (GFS2_EA_IS_LAST(ea))
  908. prev->ea_flags |= GFS2_EAFLAG_LAST;
  909. } else {
  910. ea->ea_type = GFS2_EATYPE_UNUSED;
  911. }
  912. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  913. __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  914. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  915. return error;
  916. }
  917. /**
  918. * gfs2_xattr_remove - Remove a GFS2 extended attribute
  919. * @ip: The inode
  920. * @type: The type of the extended attribute
  921. * @name: The name of the extended attribute
  922. *
  923. * This is not called directly by the VFS since we use the (common)
  924. * scheme of making a "set with NULL data" mean a remove request. Note
  925. * that this is different from a set with zero length data.
  926. *
  927. * Returns: 0, or errno on failure
  928. */
  929. static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
  930. {
  931. struct gfs2_ea_location el;
  932. int error;
  933. if (!ip->i_eattr)
  934. return -ENODATA;
  935. error = gfs2_ea_find(ip, type, name, &el);
  936. if (error)
  937. return error;
  938. if (!el.el_ea)
  939. return -ENODATA;
  940. if (GFS2_EA_IS_STUFFED(el.el_ea))
  941. error = ea_remove_stuffed(ip, &el);
  942. else
  943. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
  944. brelse(el.el_bh);
  945. return error;
  946. }
  947. /**
  948. * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
  949. * @ip: The inode
  950. * @name: The name of the extended attribute
  951. * @value: The value of the extended attribute (NULL for remove)
  952. * @size: The size of the @value argument
  953. * @flags: Create or Replace
  954. * @type: The type of the extended attribute
  955. *
  956. * See gfs2_xattr_remove() for details of the removal of xattrs.
  957. *
  958. * Returns: 0 or errno on failure
  959. */
  960. int __gfs2_xattr_set(struct inode *inode, const char *name,
  961. const void *value, size_t size, int flags, int type)
  962. {
  963. struct gfs2_inode *ip = GFS2_I(inode);
  964. struct gfs2_sbd *sdp = GFS2_SB(inode);
  965. struct gfs2_ea_location el;
  966. unsigned int namel = strlen(name);
  967. int error;
  968. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  969. return -EPERM;
  970. if (namel > GFS2_EA_MAX_NAME_LEN)
  971. return -ERANGE;
  972. if (value == NULL) {
  973. error = gfs2_xattr_remove(ip, type, name);
  974. if (error == -ENODATA && !(flags & XATTR_REPLACE))
  975. error = 0;
  976. return error;
  977. }
  978. if (ea_check_size(sdp, namel, size))
  979. return -ERANGE;
  980. if (!ip->i_eattr) {
  981. if (flags & XATTR_REPLACE)
  982. return -ENODATA;
  983. return ea_init(ip, type, name, value, size);
  984. }
  985. error = gfs2_ea_find(ip, type, name, &el);
  986. if (error)
  987. return error;
  988. if (el.el_ea) {
  989. if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
  990. brelse(el.el_bh);
  991. return -EPERM;
  992. }
  993. error = -EEXIST;
  994. if (!(flags & XATTR_CREATE)) {
  995. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  996. error = ea_set_i(ip, type, name, value, size, &el);
  997. if (!error && unstuffed)
  998. ea_set_remove_unstuffed(ip, &el);
  999. }
  1000. brelse(el.el_bh);
  1001. return error;
  1002. }
  1003. error = -ENODATA;
  1004. if (!(flags & XATTR_REPLACE))
  1005. error = ea_set_i(ip, type, name, value, size, NULL);
  1006. return error;
  1007. }
  1008. static int gfs2_xattr_set(const struct xattr_handler *handler,
  1009. struct dentry *unused, struct inode *inode,
  1010. const char *name, const void *value,
  1011. size_t size, int flags)
  1012. {
  1013. struct gfs2_inode *ip = GFS2_I(inode);
  1014. struct gfs2_holder gh;
  1015. int ret;
  1016. ret = gfs2_rsqa_alloc(ip);
  1017. if (ret)
  1018. return ret;
  1019. /* May be called from gfs_setattr with the glock locked. */
  1020. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  1021. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1022. if (ret)
  1023. return ret;
  1024. } else {
  1025. if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
  1026. return -EIO;
  1027. gfs2_holder_mark_uninitialized(&gh);
  1028. }
  1029. ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags);
  1030. if (gfs2_holder_initialized(&gh))
  1031. gfs2_glock_dq_uninit(&gh);
  1032. return ret;
  1033. }
  1034. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1035. {
  1036. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1037. struct gfs2_rgrp_list rlist;
  1038. struct buffer_head *indbh, *dibh;
  1039. __be64 *eablk, *end;
  1040. unsigned int rg_blocks = 0;
  1041. u64 bstart = 0;
  1042. unsigned int blen = 0;
  1043. unsigned int blks = 0;
  1044. unsigned int x;
  1045. int error;
  1046. error = gfs2_rindex_update(sdp);
  1047. if (error)
  1048. return error;
  1049. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1050. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
  1051. if (error)
  1052. return error;
  1053. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1054. error = -EIO;
  1055. goto out;
  1056. }
  1057. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1058. end = eablk + sdp->sd_inptrs;
  1059. for (; eablk < end; eablk++) {
  1060. u64 bn;
  1061. if (!*eablk)
  1062. break;
  1063. bn = be64_to_cpu(*eablk);
  1064. if (bstart + blen == bn)
  1065. blen++;
  1066. else {
  1067. if (bstart)
  1068. gfs2_rlist_add(ip, &rlist, bstart);
  1069. bstart = bn;
  1070. blen = 1;
  1071. }
  1072. blks++;
  1073. }
  1074. if (bstart)
  1075. gfs2_rlist_add(ip, &rlist, bstart);
  1076. else
  1077. goto out;
  1078. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1079. for (x = 0; x < rlist.rl_rgrps; x++) {
  1080. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
  1081. rg_blocks += rgd->rd_length;
  1082. }
  1083. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1084. if (error)
  1085. goto out_rlist_free;
  1086. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
  1087. RES_STATFS + RES_QUOTA, blks);
  1088. if (error)
  1089. goto out_gunlock;
  1090. gfs2_trans_add_meta(ip->i_gl, indbh);
  1091. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1092. bstart = 0;
  1093. blen = 0;
  1094. for (; eablk < end; eablk++) {
  1095. u64 bn;
  1096. if (!*eablk)
  1097. break;
  1098. bn = be64_to_cpu(*eablk);
  1099. if (bstart + blen == bn)
  1100. blen++;
  1101. else {
  1102. if (bstart)
  1103. gfs2_free_meta(ip, bstart, blen);
  1104. bstart = bn;
  1105. blen = 1;
  1106. }
  1107. *eablk = 0;
  1108. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1109. }
  1110. if (bstart)
  1111. gfs2_free_meta(ip, bstart, blen);
  1112. ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
  1113. error = gfs2_meta_inode_buffer(ip, &dibh);
  1114. if (!error) {
  1115. gfs2_trans_add_meta(ip->i_gl, dibh);
  1116. gfs2_dinode_out(ip, dibh->b_data);
  1117. brelse(dibh);
  1118. }
  1119. gfs2_trans_end(sdp);
  1120. out_gunlock:
  1121. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1122. out_rlist_free:
  1123. gfs2_rlist_free(&rlist);
  1124. out:
  1125. brelse(indbh);
  1126. return error;
  1127. }
  1128. static int ea_dealloc_block(struct gfs2_inode *ip)
  1129. {
  1130. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1131. struct gfs2_rgrpd *rgd;
  1132. struct buffer_head *dibh;
  1133. struct gfs2_holder gh;
  1134. int error;
  1135. error = gfs2_rindex_update(sdp);
  1136. if (error)
  1137. return error;
  1138. rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
  1139. if (!rgd) {
  1140. gfs2_consist_inode(ip);
  1141. return -EIO;
  1142. }
  1143. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1144. if (error)
  1145. return error;
  1146. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
  1147. RES_QUOTA, 1);
  1148. if (error)
  1149. goto out_gunlock;
  1150. gfs2_free_meta(ip, ip->i_eattr, 1);
  1151. ip->i_eattr = 0;
  1152. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1153. error = gfs2_meta_inode_buffer(ip, &dibh);
  1154. if (!error) {
  1155. gfs2_trans_add_meta(ip->i_gl, dibh);
  1156. gfs2_dinode_out(ip, dibh->b_data);
  1157. brelse(dibh);
  1158. }
  1159. gfs2_trans_end(sdp);
  1160. out_gunlock:
  1161. gfs2_glock_dq_uninit(&gh);
  1162. return error;
  1163. }
  1164. /**
  1165. * gfs2_ea_dealloc - deallocate the extended attribute fork
  1166. * @ip: the inode
  1167. *
  1168. * Returns: errno
  1169. */
  1170. int gfs2_ea_dealloc(struct gfs2_inode *ip)
  1171. {
  1172. int error;
  1173. error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
  1174. if (error)
  1175. return error;
  1176. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1177. if (error)
  1178. return error;
  1179. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1180. if (error)
  1181. goto out_quota;
  1182. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  1183. error = ea_dealloc_indirect(ip);
  1184. if (error)
  1185. goto out_quota;
  1186. }
  1187. error = ea_dealloc_block(ip);
  1188. out_quota:
  1189. gfs2_quota_unhold(ip);
  1190. return error;
  1191. }
  1192. static const struct xattr_handler gfs2_xattr_user_handler = {
  1193. .prefix = XATTR_USER_PREFIX,
  1194. .flags = GFS2_EATYPE_USR,
  1195. .get = gfs2_xattr_get,
  1196. .set = gfs2_xattr_set,
  1197. };
  1198. static const struct xattr_handler gfs2_xattr_security_handler = {
  1199. .prefix = XATTR_SECURITY_PREFIX,
  1200. .flags = GFS2_EATYPE_SECURITY,
  1201. .get = gfs2_xattr_get,
  1202. .set = gfs2_xattr_set,
  1203. };
  1204. const struct xattr_handler *gfs2_xattr_handlers[] = {
  1205. &gfs2_xattr_user_handler,
  1206. &gfs2_xattr_security_handler,
  1207. &posix_acl_access_xattr_handler,
  1208. &posix_acl_default_xattr_handler,
  1209. NULL,
  1210. };