PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/gfs2/xattr.c

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