xattr.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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_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 int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
  283. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  284. void *private)
  285. {
  286. struct ea_list *ei = private;
  287. struct gfs2_ea_request *er = ei->ei_er;
  288. unsigned int ea_size;
  289. char *prefix;
  290. unsigned int l;
  291. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  292. return 0;
  293. switch (ea->ea_type) {
  294. case GFS2_EATYPE_USR:
  295. prefix = "user.";
  296. l = 5;
  297. break;
  298. case GFS2_EATYPE_SYS:
  299. prefix = "system.";
  300. l = 7;
  301. break;
  302. case GFS2_EATYPE_SECURITY:
  303. prefix = "security.";
  304. l = 9;
  305. break;
  306. default:
  307. BUG();
  308. }
  309. ea_size = l + ea->ea_name_len + 1;
  310. if (er->er_data_len) {
  311. if (ei->ei_size + ea_size > er->er_data_len)
  312. return -ERANGE;
  313. memcpy(er->er_data + ei->ei_size, prefix, l);
  314. memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
  315. ea->ea_name_len);
  316. er->er_data[ei->ei_size + ea_size - 1] = 0;
  317. }
  318. ei->ei_size += ea_size;
  319. return 0;
  320. }
  321. /**
  322. * gfs2_listxattr - List gfs2 extended attributes
  323. * @dentry: The dentry whose inode we are interested in
  324. * @buffer: The buffer to write the results
  325. * @size: The size of the buffer
  326. *
  327. * Returns: actual size of data on success, -errno on error
  328. */
  329. ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  330. {
  331. struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
  332. struct gfs2_ea_request er;
  333. struct gfs2_holder i_gh;
  334. int error;
  335. memset(&er, 0, sizeof(struct gfs2_ea_request));
  336. if (size) {
  337. er.er_data = buffer;
  338. er.er_data_len = size;
  339. }
  340. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  341. if (error)
  342. return error;
  343. if (ip->i_eattr) {
  344. struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
  345. error = ea_foreach(ip, ea_list_i, &ei);
  346. if (!error)
  347. error = ei.ei_size;
  348. }
  349. gfs2_glock_dq_uninit(&i_gh);
  350. return error;
  351. }
  352. /**
  353. * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
  354. * request buffer
  355. * @ip: The GFS2 inode
  356. * @ea: The extended attribute header structure
  357. * @din: The data to be copied in
  358. * @dout: The data to be copied out (one of din,dout will be NULL)
  359. *
  360. * Returns: errno
  361. */
  362. static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  363. const char *din, char *dout)
  364. {
  365. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  366. struct buffer_head **bh;
  367. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  368. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  369. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  370. unsigned int x;
  371. int error = 0;
  372. unsigned char *pos;
  373. unsigned cp_size;
  374. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  375. if (!bh)
  376. return -ENOMEM;
  377. for (x = 0; x < nptrs; x++) {
  378. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0,
  379. bh + x);
  380. if (error) {
  381. while (x--)
  382. brelse(bh[x]);
  383. goto out;
  384. }
  385. dataptrs++;
  386. }
  387. for (x = 0; x < nptrs; x++) {
  388. error = gfs2_meta_wait(sdp, bh[x]);
  389. if (error) {
  390. for (; x < nptrs; x++)
  391. brelse(bh[x]);
  392. goto out;
  393. }
  394. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  395. for (; x < nptrs; x++)
  396. brelse(bh[x]);
  397. error = -EIO;
  398. goto out;
  399. }
  400. pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
  401. cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
  402. if (dout) {
  403. memcpy(dout, pos, cp_size);
  404. dout += sdp->sd_jbsize;
  405. }
  406. if (din) {
  407. gfs2_trans_add_meta(ip->i_gl, bh[x]);
  408. memcpy(pos, din, cp_size);
  409. din += sdp->sd_jbsize;
  410. }
  411. amount -= sdp->sd_jbsize;
  412. brelse(bh[x]);
  413. }
  414. out:
  415. kfree(bh);
  416. return error;
  417. }
  418. static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  419. char *data, size_t size)
  420. {
  421. int ret;
  422. size_t len = GFS2_EA_DATA_LEN(el->el_ea);
  423. if (len > size)
  424. return -ERANGE;
  425. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  426. memcpy(data, GFS2_EA2DATA(el->el_ea), len);
  427. return len;
  428. }
  429. ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
  430. if (ret < 0)
  431. return ret;
  432. return len;
  433. }
  434. int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
  435. {
  436. struct gfs2_ea_location el;
  437. int error;
  438. int len;
  439. char *data;
  440. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
  441. if (error)
  442. return error;
  443. if (!el.el_ea)
  444. goto out;
  445. if (!GFS2_EA_DATA_LEN(el.el_ea))
  446. goto out;
  447. len = GFS2_EA_DATA_LEN(el.el_ea);
  448. data = kmalloc(len, GFP_NOFS);
  449. error = -ENOMEM;
  450. if (data == NULL)
  451. goto out;
  452. error = gfs2_ea_get_copy(ip, &el, data, len);
  453. if (error < 0)
  454. kfree(data);
  455. else
  456. *ppdata = data;
  457. out:
  458. brelse(el.el_bh);
  459. return error;
  460. }
  461. /**
  462. * gfs2_xattr_get - Get a GFS2 extended attribute
  463. * @inode: The inode
  464. * @name: The name of the extended attribute
  465. * @buffer: The buffer to write the result into
  466. * @size: The size of the buffer
  467. * @type: The type of extended attribute
  468. *
  469. * Returns: actual size of data on success, -errno on error
  470. */
  471. static int __gfs2_xattr_get(struct inode *inode, const char *name,
  472. void *buffer, size_t size, int type)
  473. {
  474. struct gfs2_inode *ip = GFS2_I(inode);
  475. struct gfs2_ea_location el;
  476. int error;
  477. if (!ip->i_eattr)
  478. return -ENODATA;
  479. if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
  480. return -EINVAL;
  481. error = gfs2_ea_find(ip, type, name, &el);
  482. if (error)
  483. return error;
  484. if (!el.el_ea)
  485. return -ENODATA;
  486. if (size)
  487. error = gfs2_ea_get_copy(ip, &el, buffer, size);
  488. else
  489. error = GFS2_EA_DATA_LEN(el.el_ea);
  490. brelse(el.el_bh);
  491. return error;
  492. }
  493. static int gfs2_xattr_get(const struct xattr_handler *handler,
  494. struct dentry *unused, struct inode *inode,
  495. const char *name, void *buffer, size_t size)
  496. {
  497. struct gfs2_inode *ip = GFS2_I(inode);
  498. struct gfs2_holder gh;
  499. int ret;
  500. /* During lookup, SELinux calls this function with the glock locked. */
  501. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  502. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  503. if (ret)
  504. return ret;
  505. } else {
  506. gfs2_holder_mark_uninitialized(&gh);
  507. }
  508. ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags);
  509. if (gfs2_holder_initialized(&gh))
  510. gfs2_glock_dq_uninit(&gh);
  511. return ret;
  512. }
  513. /**
  514. * ea_alloc_blk - allocates a new block for extended attributes.
  515. * @ip: A pointer to the inode that's getting extended attributes
  516. * @bhp: Pointer to pointer to a struct buffer_head
  517. *
  518. * Returns: errno
  519. */
  520. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  521. {
  522. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  523. struct gfs2_ea_header *ea;
  524. unsigned int n = 1;
  525. u64 block;
  526. int error;
  527. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  528. if (error)
  529. return error;
  530. gfs2_trans_add_unrevoke(sdp, block, 1);
  531. *bhp = gfs2_meta_new(ip->i_gl, block);
  532. gfs2_trans_add_meta(ip->i_gl, *bhp);
  533. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  534. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  535. ea = GFS2_EA_BH2FIRST(*bhp);
  536. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  537. ea->ea_type = GFS2_EATYPE_UNUSED;
  538. ea->ea_flags = GFS2_EAFLAG_LAST;
  539. ea->ea_num_ptrs = 0;
  540. gfs2_add_inode_blocks(&ip->i_inode, 1);
  541. return 0;
  542. }
  543. /**
  544. * ea_write - writes the request info to an ea, creating new blocks if
  545. * necessary
  546. * @ip: inode that is being modified
  547. * @ea: the location of the new ea in a block
  548. * @er: the write request
  549. *
  550. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  551. *
  552. * returns : errno
  553. */
  554. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  555. struct gfs2_ea_request *er)
  556. {
  557. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  558. int error;
  559. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  560. ea->ea_name_len = er->er_name_len;
  561. ea->ea_type = er->er_type;
  562. ea->__pad = 0;
  563. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  564. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  565. ea->ea_num_ptrs = 0;
  566. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  567. } else {
  568. __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
  569. const char *data = er->er_data;
  570. unsigned int data_len = er->er_data_len;
  571. unsigned int copy;
  572. unsigned int x;
  573. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  574. for (x = 0; x < ea->ea_num_ptrs; x++) {
  575. struct buffer_head *bh;
  576. u64 block;
  577. int mh_size = sizeof(struct gfs2_meta_header);
  578. unsigned int n = 1;
  579. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  580. if (error)
  581. return error;
  582. gfs2_trans_add_unrevoke(sdp, block, 1);
  583. bh = gfs2_meta_new(ip->i_gl, block);
  584. gfs2_trans_add_meta(ip->i_gl, bh);
  585. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  586. gfs2_add_inode_blocks(&ip->i_inode, 1);
  587. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  588. data_len;
  589. memcpy(bh->b_data + mh_size, data, copy);
  590. if (copy < sdp->sd_jbsize)
  591. memset(bh->b_data + mh_size + copy, 0,
  592. sdp->sd_jbsize - copy);
  593. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  594. data += copy;
  595. data_len -= copy;
  596. brelse(bh);
  597. }
  598. gfs2_assert_withdraw(sdp, !data_len);
  599. }
  600. return 0;
  601. }
  602. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  603. struct gfs2_ea_request *er, void *private);
  604. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  605. unsigned int blks,
  606. ea_skeleton_call_t skeleton_call, void *private)
  607. {
  608. struct gfs2_alloc_parms ap = { .target = blks };
  609. int error;
  610. error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
  611. if (error)
  612. return error;
  613. error = gfs2_quota_lock_check(ip, &ap);
  614. if (error)
  615. return error;
  616. error = gfs2_inplace_reserve(ip, &ap);
  617. if (error)
  618. goto out_gunlock_q;
  619. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  620. blks + gfs2_rg_blocks(ip, blks) +
  621. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  622. if (error)
  623. goto out_ipres;
  624. error = skeleton_call(ip, er, private);
  625. if (error)
  626. goto out_end_trans;
  627. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  628. __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
  629. out_end_trans:
  630. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  631. out_ipres:
  632. gfs2_inplace_release(ip);
  633. out_gunlock_q:
  634. gfs2_quota_unlock(ip);
  635. return error;
  636. }
  637. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  638. void *private)
  639. {
  640. struct buffer_head *bh;
  641. int error;
  642. error = ea_alloc_blk(ip, &bh);
  643. if (error)
  644. return error;
  645. ip->i_eattr = bh->b_blocknr;
  646. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  647. brelse(bh);
  648. return error;
  649. }
  650. /**
  651. * ea_init - initializes a new eattr block
  652. * @ip:
  653. * @er:
  654. *
  655. * Returns: errno
  656. */
  657. static int ea_init(struct gfs2_inode *ip, int type, const char *name,
  658. const void *data, size_t size)
  659. {
  660. struct gfs2_ea_request er;
  661. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  662. unsigned int blks = 1;
  663. er.er_type = type;
  664. er.er_name = name;
  665. er.er_name_len = strlen(name);
  666. er.er_data = (void *)data;
  667. er.er_data_len = size;
  668. if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
  669. blks += DIV_ROUND_UP(er.er_data_len, jbsize);
  670. return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
  671. }
  672. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  673. {
  674. u32 ea_size = GFS2_EA_SIZE(ea);
  675. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  676. ea_size);
  677. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  678. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  679. ea->ea_rec_len = cpu_to_be32(ea_size);
  680. ea->ea_flags ^= last;
  681. new->ea_rec_len = cpu_to_be32(new_size);
  682. new->ea_flags = last;
  683. return new;
  684. }
  685. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  686. struct gfs2_ea_location *el)
  687. {
  688. struct gfs2_ea_header *ea = el->el_ea;
  689. struct gfs2_ea_header *prev = el->el_prev;
  690. u32 len;
  691. gfs2_trans_add_meta(ip->i_gl, el->el_bh);
  692. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  693. ea->ea_type = GFS2_EATYPE_UNUSED;
  694. return;
  695. } else if (GFS2_EA2NEXT(prev) != ea) {
  696. prev = GFS2_EA2NEXT(prev);
  697. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  698. }
  699. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  700. prev->ea_rec_len = cpu_to_be32(len);
  701. if (GFS2_EA_IS_LAST(ea))
  702. prev->ea_flags |= GFS2_EAFLAG_LAST;
  703. }
  704. struct ea_set {
  705. int ea_split;
  706. struct gfs2_ea_request *es_er;
  707. struct gfs2_ea_location *es_el;
  708. struct buffer_head *es_bh;
  709. struct gfs2_ea_header *es_ea;
  710. };
  711. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  712. struct gfs2_ea_header *ea, struct ea_set *es)
  713. {
  714. struct gfs2_ea_request *er = es->es_er;
  715. int error;
  716. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  717. if (error)
  718. return error;
  719. gfs2_trans_add_meta(ip->i_gl, bh);
  720. if (es->ea_split)
  721. ea = ea_split_ea(ea);
  722. ea_write(ip, ea, er);
  723. if (es->es_el)
  724. ea_set_remove_stuffed(ip, es->es_el);
  725. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  726. __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
  727. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  728. return error;
  729. }
  730. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  731. struct gfs2_ea_request *er, void *private)
  732. {
  733. struct ea_set *es = private;
  734. struct gfs2_ea_header *ea = es->es_ea;
  735. int error;
  736. gfs2_trans_add_meta(ip->i_gl, es->es_bh);
  737. if (es->ea_split)
  738. ea = ea_split_ea(ea);
  739. error = ea_write(ip, ea, er);
  740. if (error)
  741. return error;
  742. if (es->es_el)
  743. ea_set_remove_stuffed(ip, es->es_el);
  744. return 0;
  745. }
  746. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  747. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  748. void *private)
  749. {
  750. struct ea_set *es = private;
  751. unsigned int size;
  752. int stuffed;
  753. int error;
  754. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
  755. es->es_er->er_data_len, &size);
  756. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  757. if (GFS2_EA_REC_LEN(ea) < size)
  758. return 0;
  759. if (!GFS2_EA_IS_STUFFED(ea)) {
  760. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  761. if (error)
  762. return error;
  763. }
  764. es->ea_split = 0;
  765. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  766. es->ea_split = 1;
  767. else
  768. return 0;
  769. if (stuffed) {
  770. error = ea_set_simple_noalloc(ip, bh, ea, es);
  771. if (error)
  772. return error;
  773. } else {
  774. unsigned int blks;
  775. es->es_bh = bh;
  776. es->es_ea = ea;
  777. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  778. GFS2_SB(&ip->i_inode)->sd_jbsize);
  779. error = ea_alloc_skeleton(ip, es->es_er, blks,
  780. ea_set_simple_alloc, es);
  781. if (error)
  782. return error;
  783. }
  784. return 1;
  785. }
  786. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  787. void *private)
  788. {
  789. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  790. struct buffer_head *indbh, *newbh;
  791. __be64 *eablk;
  792. int error;
  793. int mh_size = sizeof(struct gfs2_meta_header);
  794. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  795. __be64 *end;
  796. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0,
  797. &indbh);
  798. if (error)
  799. return error;
  800. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  801. error = -EIO;
  802. goto out;
  803. }
  804. eablk = (__be64 *)(indbh->b_data + mh_size);
  805. end = eablk + sdp->sd_inptrs;
  806. for (; eablk < end; eablk++)
  807. if (!*eablk)
  808. break;
  809. if (eablk == end) {
  810. error = -ENOSPC;
  811. goto out;
  812. }
  813. gfs2_trans_add_meta(ip->i_gl, indbh);
  814. } else {
  815. u64 blk;
  816. unsigned int n = 1;
  817. error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
  818. if (error)
  819. return error;
  820. gfs2_trans_add_unrevoke(sdp, blk, 1);
  821. indbh = gfs2_meta_new(ip->i_gl, blk);
  822. gfs2_trans_add_meta(ip->i_gl, indbh);
  823. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  824. gfs2_buffer_clear_tail(indbh, mh_size);
  825. eablk = (__be64 *)(indbh->b_data + mh_size);
  826. *eablk = cpu_to_be64(ip->i_eattr);
  827. ip->i_eattr = blk;
  828. ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
  829. gfs2_add_inode_blocks(&ip->i_inode, 1);
  830. eablk++;
  831. }
  832. error = ea_alloc_blk(ip, &newbh);
  833. if (error)
  834. goto out;
  835. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  836. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  837. brelse(newbh);
  838. if (error)
  839. goto out;
  840. if (private)
  841. ea_set_remove_stuffed(ip, private);
  842. out:
  843. brelse(indbh);
  844. return error;
  845. }
  846. static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
  847. const void *value, size_t size, struct gfs2_ea_location *el)
  848. {
  849. struct gfs2_ea_request er;
  850. struct ea_set es;
  851. unsigned int blks = 2;
  852. int error;
  853. er.er_type = type;
  854. er.er_name = name;
  855. er.er_data = (void *)value;
  856. er.er_name_len = strlen(name);
  857. er.er_data_len = size;
  858. memset(&es, 0, sizeof(struct ea_set));
  859. es.es_er = &er;
  860. es.es_el = el;
  861. error = ea_foreach(ip, ea_set_simple, &es);
  862. if (error > 0)
  863. return 0;
  864. if (error)
  865. return error;
  866. if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
  867. blks++;
  868. if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  869. blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  870. return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
  871. }
  872. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  873. struct gfs2_ea_location *el)
  874. {
  875. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  876. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  877. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  878. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  879. }
  880. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
  881. }
  882. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  883. {
  884. struct gfs2_ea_header *ea = el->el_ea;
  885. struct gfs2_ea_header *prev = el->el_prev;
  886. int error;
  887. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  888. if (error)
  889. return error;
  890. gfs2_trans_add_meta(ip->i_gl, el->el_bh);
  891. if (prev) {
  892. u32 len;
  893. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  894. prev->ea_rec_len = cpu_to_be32(len);
  895. if (GFS2_EA_IS_LAST(ea))
  896. prev->ea_flags |= GFS2_EAFLAG_LAST;
  897. } else {
  898. ea->ea_type = GFS2_EATYPE_UNUSED;
  899. }
  900. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  901. __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
  902. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  903. return error;
  904. }
  905. /**
  906. * gfs2_xattr_remove - Remove a GFS2 extended attribute
  907. * @ip: The inode
  908. * @type: The type of the extended attribute
  909. * @name: The name of the extended attribute
  910. *
  911. * This is not called directly by the VFS since we use the (common)
  912. * scheme of making a "set with NULL data" mean a remove request. Note
  913. * that this is different from a set with zero length data.
  914. *
  915. * Returns: 0, or errno on failure
  916. */
  917. static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
  918. {
  919. struct gfs2_ea_location el;
  920. int error;
  921. if (!ip->i_eattr)
  922. return -ENODATA;
  923. error = gfs2_ea_find(ip, type, name, &el);
  924. if (error)
  925. return error;
  926. if (!el.el_ea)
  927. return -ENODATA;
  928. if (GFS2_EA_IS_STUFFED(el.el_ea))
  929. error = ea_remove_stuffed(ip, &el);
  930. else
  931. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
  932. brelse(el.el_bh);
  933. return error;
  934. }
  935. /**
  936. * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
  937. * @ip: The inode
  938. * @name: The name of the extended attribute
  939. * @value: The value of the extended attribute (NULL for remove)
  940. * @size: The size of the @value argument
  941. * @flags: Create or Replace
  942. * @type: The type of the extended attribute
  943. *
  944. * See gfs2_xattr_remove() for details of the removal of xattrs.
  945. *
  946. * Returns: 0 or errno on failure
  947. */
  948. int __gfs2_xattr_set(struct inode *inode, const char *name,
  949. const void *value, size_t size, int flags, int type)
  950. {
  951. struct gfs2_inode *ip = GFS2_I(inode);
  952. struct gfs2_sbd *sdp = GFS2_SB(inode);
  953. struct gfs2_ea_location el;
  954. unsigned int namel = strlen(name);
  955. int error;
  956. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  957. return -EPERM;
  958. if (namel > GFS2_EA_MAX_NAME_LEN)
  959. return -ERANGE;
  960. if (value == NULL) {
  961. error = gfs2_xattr_remove(ip, type, name);
  962. if (error == -ENODATA && !(flags & XATTR_REPLACE))
  963. error = 0;
  964. return error;
  965. }
  966. if (ea_check_size(sdp, namel, size))
  967. return -ERANGE;
  968. if (!ip->i_eattr) {
  969. if (flags & XATTR_REPLACE)
  970. return -ENODATA;
  971. return ea_init(ip, type, name, value, size);
  972. }
  973. error = gfs2_ea_find(ip, type, name, &el);
  974. if (error)
  975. return error;
  976. if (el.el_ea) {
  977. if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
  978. brelse(el.el_bh);
  979. return -EPERM;
  980. }
  981. error = -EEXIST;
  982. if (!(flags & XATTR_CREATE)) {
  983. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  984. error = ea_set_i(ip, type, name, value, size, &el);
  985. if (!error && unstuffed)
  986. ea_set_remove_unstuffed(ip, &el);
  987. }
  988. brelse(el.el_bh);
  989. return error;
  990. }
  991. error = -ENODATA;
  992. if (!(flags & XATTR_REPLACE))
  993. error = ea_set_i(ip, type, name, value, size, NULL);
  994. return error;
  995. }
  996. static int gfs2_xattr_set(const struct xattr_handler *handler,
  997. struct dentry *unused, struct inode *inode,
  998. const char *name, const void *value,
  999. size_t size, int flags)
  1000. {
  1001. struct gfs2_inode *ip = GFS2_I(inode);
  1002. struct gfs2_holder gh;
  1003. int ret;
  1004. ret = gfs2_rsqa_alloc(ip);
  1005. if (ret)
  1006. return ret;
  1007. /* May be called from gfs_setattr with the glock locked. */
  1008. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  1009. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1010. if (ret)
  1011. return ret;
  1012. } else {
  1013. if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
  1014. return -EIO;
  1015. gfs2_holder_mark_uninitialized(&gh);
  1016. }
  1017. ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags);
  1018. if (gfs2_holder_initialized(&gh))
  1019. gfs2_glock_dq_uninit(&gh);
  1020. return ret;
  1021. }
  1022. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1023. {
  1024. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1025. struct gfs2_rgrp_list rlist;
  1026. struct buffer_head *indbh, *dibh;
  1027. __be64 *eablk, *end;
  1028. unsigned int rg_blocks = 0;
  1029. u64 bstart = 0;
  1030. unsigned int blen = 0;
  1031. unsigned int blks = 0;
  1032. unsigned int x;
  1033. int error;
  1034. error = gfs2_rindex_update(sdp);
  1035. if (error)
  1036. return error;
  1037. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1038. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
  1039. if (error)
  1040. return error;
  1041. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1042. error = -EIO;
  1043. goto out;
  1044. }
  1045. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1046. end = eablk + sdp->sd_inptrs;
  1047. for (; eablk < end; eablk++) {
  1048. u64 bn;
  1049. if (!*eablk)
  1050. break;
  1051. bn = be64_to_cpu(*eablk);
  1052. if (bstart + blen == bn)
  1053. blen++;
  1054. else {
  1055. if (bstart)
  1056. gfs2_rlist_add(ip, &rlist, bstart);
  1057. bstart = bn;
  1058. blen = 1;
  1059. }
  1060. blks++;
  1061. }
  1062. if (bstart)
  1063. gfs2_rlist_add(ip, &rlist, bstart);
  1064. else
  1065. goto out;
  1066. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1067. for (x = 0; x < rlist.rl_rgrps; x++) {
  1068. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
  1069. rg_blocks += rgd->rd_length;
  1070. }
  1071. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1072. if (error)
  1073. goto out_rlist_free;
  1074. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
  1075. RES_STATFS + RES_QUOTA, blks);
  1076. if (error)
  1077. goto out_gunlock;
  1078. gfs2_trans_add_meta(ip->i_gl, indbh);
  1079. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1080. bstart = 0;
  1081. blen = 0;
  1082. for (; eablk < end; eablk++) {
  1083. u64 bn;
  1084. if (!*eablk)
  1085. break;
  1086. bn = be64_to_cpu(*eablk);
  1087. if (bstart + blen == bn)
  1088. blen++;
  1089. else {
  1090. if (bstart)
  1091. gfs2_free_meta(ip, bstart, blen);
  1092. bstart = bn;
  1093. blen = 1;
  1094. }
  1095. *eablk = 0;
  1096. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1097. }
  1098. if (bstart)
  1099. gfs2_free_meta(ip, bstart, blen);
  1100. ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
  1101. error = gfs2_meta_inode_buffer(ip, &dibh);
  1102. if (!error) {
  1103. gfs2_trans_add_meta(ip->i_gl, dibh);
  1104. gfs2_dinode_out(ip, dibh->b_data);
  1105. brelse(dibh);
  1106. }
  1107. gfs2_trans_end(sdp);
  1108. out_gunlock:
  1109. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1110. out_rlist_free:
  1111. gfs2_rlist_free(&rlist);
  1112. out:
  1113. brelse(indbh);
  1114. return error;
  1115. }
  1116. static int ea_dealloc_block(struct gfs2_inode *ip)
  1117. {
  1118. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1119. struct gfs2_rgrpd *rgd;
  1120. struct buffer_head *dibh;
  1121. struct gfs2_holder gh;
  1122. int error;
  1123. error = gfs2_rindex_update(sdp);
  1124. if (error)
  1125. return error;
  1126. rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
  1127. if (!rgd) {
  1128. gfs2_consist_inode(ip);
  1129. return -EIO;
  1130. }
  1131. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1132. if (error)
  1133. return error;
  1134. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
  1135. RES_QUOTA, 1);
  1136. if (error)
  1137. goto out_gunlock;
  1138. gfs2_free_meta(ip, ip->i_eattr, 1);
  1139. ip->i_eattr = 0;
  1140. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1141. error = gfs2_meta_inode_buffer(ip, &dibh);
  1142. if (!error) {
  1143. gfs2_trans_add_meta(ip->i_gl, dibh);
  1144. gfs2_dinode_out(ip, dibh->b_data);
  1145. brelse(dibh);
  1146. }
  1147. gfs2_trans_end(sdp);
  1148. out_gunlock:
  1149. gfs2_glock_dq_uninit(&gh);
  1150. return error;
  1151. }
  1152. /**
  1153. * gfs2_ea_dealloc - deallocate the extended attribute fork
  1154. * @ip: the inode
  1155. *
  1156. * Returns: errno
  1157. */
  1158. int gfs2_ea_dealloc(struct gfs2_inode *ip)
  1159. {
  1160. int error;
  1161. error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
  1162. if (error)
  1163. return error;
  1164. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1165. if (error)
  1166. return error;
  1167. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1168. if (error)
  1169. goto out_quota;
  1170. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  1171. error = ea_dealloc_indirect(ip);
  1172. if (error)
  1173. goto out_quota;
  1174. }
  1175. error = ea_dealloc_block(ip);
  1176. out_quota:
  1177. gfs2_quota_unhold(ip);
  1178. return error;
  1179. }
  1180. static const struct xattr_handler gfs2_xattr_user_handler = {
  1181. .prefix = XATTR_USER_PREFIX,
  1182. .flags = GFS2_EATYPE_USR,
  1183. .get = gfs2_xattr_get,
  1184. .set = gfs2_xattr_set,
  1185. };
  1186. static const struct xattr_handler gfs2_xattr_security_handler = {
  1187. .prefix = XATTR_SECURITY_PREFIX,
  1188. .flags = GFS2_EATYPE_SECURITY,
  1189. .get = gfs2_xattr_get,
  1190. .set = gfs2_xattr_set,
  1191. };
  1192. const struct xattr_handler *gfs2_xattr_handlers[] = {
  1193. &gfs2_xattr_user_handler,
  1194. &gfs2_xattr_security_handler,
  1195. &posix_acl_access_xattr_handler,
  1196. &posix_acl_default_xattr_handler,
  1197. NULL,
  1198. };