eattr.c 32 KB

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