xattr.c 33 KB

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