/kern_oII/fs/gfs2/eattr.c

http://omnia2droid.googlecode.com/ · C · 1505 lines · 1116 code · 289 blank · 100 comment · 215 complexity · f22274c107a52146934a865b79f5c6c1 MD5 · raw file

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