xattr.c 32 KB

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