eattr.c 33 KB

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