eattr.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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(ip->i_sbd, 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(ip->i_sbd, 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 + ip->i_sbd->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 = ip->i_sbd;
  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(ip->i_sbd, &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;
  308. unsigned int l;
  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. default:
  326. /* FIXME: Needs looking at again */
  327. break;
  328. }
  329. memcpy(er->er_data + ei->ei_size, prefix, l);
  330. memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
  331. ea->ea_name_len);
  332. memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
  333. }
  334. ei->ei_size += ea_size;
  335. return 0;
  336. }
  337. /**
  338. * gfs2_ea_list -
  339. * @ip:
  340. * @er:
  341. *
  342. * Returns: actual size of data on success, -errno on error
  343. */
  344. int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  345. {
  346. struct gfs2_holder i_gh;
  347. int error;
  348. if (!er->er_data || !er->er_data_len) {
  349. er->er_data = NULL;
  350. er->er_data_len = 0;
  351. }
  352. error = gfs2_glock_nq_init(ip->i_gl,
  353. LM_ST_SHARED, LM_FLAG_ANY,
  354. &i_gh);
  355. if (error)
  356. return error;
  357. if (ip->i_di.di_eattr) {
  358. struct ea_list ei = { .ei_er = er, .ei_size = 0 };
  359. error = ea_foreach(ip, ea_list_i, &ei);
  360. if (!error)
  361. error = ei.ei_size;
  362. }
  363. gfs2_glock_dq_uninit(&i_gh);
  364. return error;
  365. }
  366. /**
  367. * ea_get_unstuffed - actually copies the unstuffed data into the
  368. * request buffer
  369. * @ip:
  370. * @ea:
  371. * @data:
  372. *
  373. * Returns: errno
  374. */
  375. static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  376. char *data)
  377. {
  378. struct gfs2_sbd *sdp = ip->i_sbd;
  379. struct buffer_head **bh;
  380. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  381. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  382. uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
  383. unsigned int x;
  384. int error = 0;
  385. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  386. if (!bh)
  387. return -ENOMEM;
  388. for (x = 0; x < nptrs; x++) {
  389. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
  390. DIO_START, bh + x);
  391. if (error) {
  392. while (x--)
  393. brelse(bh[x]);
  394. goto out;
  395. }
  396. dataptrs++;
  397. }
  398. for (x = 0; x < nptrs; x++) {
  399. error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
  400. if (error) {
  401. for (; x < nptrs; x++)
  402. brelse(bh[x]);
  403. goto out;
  404. }
  405. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  406. for (; x < nptrs; x++)
  407. brelse(bh[x]);
  408. error = -EIO;
  409. goto out;
  410. }
  411. memcpy(data,
  412. bh[x]->b_data + sizeof(struct gfs2_meta_header),
  413. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  414. amount -= sdp->sd_jbsize;
  415. data += sdp->sd_jbsize;
  416. brelse(bh[x]);
  417. }
  418. out:
  419. kfree(bh);
  420. return error;
  421. }
  422. int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  423. char *data)
  424. {
  425. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  426. memcpy(data,
  427. GFS2_EA2DATA(el->el_ea),
  428. GFS2_EA_DATA_LEN(el->el_ea));
  429. return 0;
  430. } else
  431. return ea_get_unstuffed(ip, el->el_ea, data);
  432. }
  433. /**
  434. * gfs2_ea_get_i -
  435. * @ip:
  436. * @er:
  437. *
  438. * Returns: actual size of data on success, -errno on error
  439. */
  440. int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  441. {
  442. struct gfs2_ea_location el;
  443. int error;
  444. if (!ip->i_di.di_eattr)
  445. return -ENODATA;
  446. error = gfs2_ea_find(ip, er, &el);
  447. if (error)
  448. return error;
  449. if (!el.el_ea)
  450. return -ENODATA;
  451. if (er->er_data_len) {
  452. if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
  453. error = -ERANGE;
  454. else
  455. error = gfs2_ea_get_copy(ip, &el, er->er_data);
  456. }
  457. if (!error)
  458. error = GFS2_EA_DATA_LEN(el.el_ea);
  459. brelse(el.el_bh);
  460. return error;
  461. }
  462. /**
  463. * gfs2_ea_get -
  464. * @ip:
  465. * @er:
  466. *
  467. * Returns: actual size of data on success, -errno on error
  468. */
  469. int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  470. {
  471. struct gfs2_holder i_gh;
  472. int error;
  473. if (!er->er_name_len ||
  474. er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  475. return -EINVAL;
  476. if (!er->er_data || !er->er_data_len) {
  477. er->er_data = NULL;
  478. er->er_data_len = 0;
  479. }
  480. error = gfs2_glock_nq_init(ip->i_gl,
  481. LM_ST_SHARED, LM_FLAG_ANY,
  482. &i_gh);
  483. if (error)
  484. return error;
  485. error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
  486. gfs2_glock_dq_uninit(&i_gh);
  487. return error;
  488. }
  489. /**
  490. * ea_alloc_blk - allocates a new block for extended attributes.
  491. * @ip: A pointer to the inode that's getting extended attributes
  492. * @bhp:
  493. *
  494. * Returns: errno
  495. */
  496. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  497. {
  498. struct gfs2_sbd *sdp = ip->i_sbd;
  499. struct gfs2_ea_header *ea;
  500. uint64_t block;
  501. block = gfs2_alloc_meta(ip);
  502. *bhp = gfs2_meta_new(ip->i_gl, block);
  503. gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
  504. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  505. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  506. ea = GFS2_EA_BH2FIRST(*bhp);
  507. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  508. ea->ea_type = GFS2_EATYPE_UNUSED;
  509. ea->ea_flags = GFS2_EAFLAG_LAST;
  510. ea->ea_num_ptrs = 0;
  511. ip->i_di.di_blocks++;
  512. return 0;
  513. }
  514. /**
  515. * ea_write - writes the request info to an ea, creating new blocks if
  516. * necessary
  517. * @ip: inode that is being modified
  518. * @ea: the location of the new ea in a block
  519. * @er: the write request
  520. *
  521. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  522. *
  523. * returns : errno
  524. */
  525. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  526. struct gfs2_ea_request *er)
  527. {
  528. struct gfs2_sbd *sdp = ip->i_sbd;
  529. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  530. ea->ea_name_len = er->er_name_len;
  531. ea->ea_type = er->er_type;
  532. ea->__pad = 0;
  533. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  534. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  535. ea->ea_num_ptrs = 0;
  536. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  537. } else {
  538. uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
  539. const char *data = er->er_data;
  540. unsigned int data_len = er->er_data_len;
  541. unsigned int copy;
  542. unsigned int x;
  543. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  544. for (x = 0; x < ea->ea_num_ptrs; x++) {
  545. struct buffer_head *bh;
  546. uint64_t block;
  547. int mh_size = sizeof(struct gfs2_meta_header);
  548. block = gfs2_alloc_meta(ip);
  549. bh = gfs2_meta_new(ip->i_gl, block);
  550. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  551. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  552. ip->i_di.di_blocks++;
  553. copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
  554. data_len;
  555. memcpy(bh->b_data + mh_size, data, copy);
  556. if (copy < sdp->sd_jbsize)
  557. memset(bh->b_data + mh_size + copy, 0,
  558. sdp->sd_jbsize - copy);
  559. *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
  560. data += copy;
  561. data_len -= copy;
  562. brelse(bh);
  563. }
  564. gfs2_assert_withdraw(sdp, !data_len);
  565. }
  566. return 0;
  567. }
  568. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  569. struct gfs2_ea_request *er,
  570. void *private);
  571. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  572. unsigned int blks,
  573. ea_skeleton_call_t skeleton_call,
  574. void *private)
  575. {
  576. struct gfs2_alloc *al;
  577. struct buffer_head *dibh;
  578. int error;
  579. al = gfs2_alloc_get(ip);
  580. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  581. if (error)
  582. goto out;
  583. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  584. if (error)
  585. goto out_gunlock_q;
  586. al->al_requested = blks;
  587. error = gfs2_inplace_reserve(ip);
  588. if (error)
  589. goto out_gunlock_q;
  590. error = gfs2_trans_begin(ip->i_sbd,
  591. blks + al->al_rgd->rd_ri.ri_length +
  592. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  593. if (error)
  594. goto out_ipres;
  595. error = skeleton_call(ip, er, private);
  596. if (error)
  597. goto out_end_trans;
  598. error = gfs2_meta_inode_buffer(ip, &dibh);
  599. if (!error) {
  600. if (er->er_flags & GFS2_ERF_MODE) {
  601. gfs2_assert_withdraw(ip->i_sbd,
  602. (ip->i_di.di_mode & S_IFMT) ==
  603. (er->er_mode & S_IFMT));
  604. ip->i_di.di_mode = er->er_mode;
  605. }
  606. ip->i_di.di_ctime = get_seconds();
  607. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  608. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  609. brelse(dibh);
  610. }
  611. out_end_trans:
  612. gfs2_trans_end(ip->i_sbd);
  613. out_ipres:
  614. gfs2_inplace_release(ip);
  615. out_gunlock_q:
  616. gfs2_quota_unlock(ip);
  617. out:
  618. gfs2_alloc_put(ip);
  619. return error;
  620. }
  621. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  622. void *private)
  623. {
  624. struct buffer_head *bh;
  625. int error;
  626. error = ea_alloc_blk(ip, &bh);
  627. if (error)
  628. return error;
  629. ip->i_di.di_eattr = bh->b_blocknr;
  630. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  631. brelse(bh);
  632. return error;
  633. }
  634. /**
  635. * ea_init - initializes a new eattr block
  636. * @ip:
  637. * @er:
  638. *
  639. * Returns: errno
  640. */
  641. static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  642. {
  643. unsigned int jbsize = ip->i_sbd->sd_jbsize;
  644. unsigned int blks = 1;
  645. if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
  646. blks += DIV_ROUND_UP(er->er_data_len, jbsize);
  647. return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
  648. }
  649. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  650. {
  651. uint32_t ea_size = GFS2_EA_SIZE(ea);
  652. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  653. ea_size);
  654. uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  655. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  656. ea->ea_rec_len = cpu_to_be32(ea_size);
  657. ea->ea_flags ^= last;
  658. new->ea_rec_len = cpu_to_be32(new_size);
  659. new->ea_flags = last;
  660. return new;
  661. }
  662. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  663. struct gfs2_ea_location *el)
  664. {
  665. struct gfs2_ea_header *ea = el->el_ea;
  666. struct gfs2_ea_header *prev = el->el_prev;
  667. uint32_t len;
  668. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  669. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  670. ea->ea_type = GFS2_EATYPE_UNUSED;
  671. return;
  672. } else if (GFS2_EA2NEXT(prev) != ea) {
  673. prev = GFS2_EA2NEXT(prev);
  674. gfs2_assert_withdraw(ip->i_sbd, GFS2_EA2NEXT(prev) == ea);
  675. }
  676. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  677. prev->ea_rec_len = cpu_to_be32(len);
  678. if (GFS2_EA_IS_LAST(ea))
  679. prev->ea_flags |= GFS2_EAFLAG_LAST;
  680. }
  681. struct ea_set {
  682. int ea_split;
  683. struct gfs2_ea_request *es_er;
  684. struct gfs2_ea_location *es_el;
  685. struct buffer_head *es_bh;
  686. struct gfs2_ea_header *es_ea;
  687. };
  688. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  689. struct gfs2_ea_header *ea, struct ea_set *es)
  690. {
  691. struct gfs2_ea_request *er = es->es_er;
  692. struct buffer_head *dibh;
  693. int error;
  694. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + 2 * RES_EATTR, 0);
  695. if (error)
  696. return error;
  697. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  698. if (es->ea_split)
  699. ea = ea_split_ea(ea);
  700. ea_write(ip, ea, er);
  701. if (es->es_el)
  702. ea_set_remove_stuffed(ip, es->es_el);
  703. error = gfs2_meta_inode_buffer(ip, &dibh);
  704. if (error)
  705. goto out;
  706. if (er->er_flags & GFS2_ERF_MODE) {
  707. gfs2_assert_withdraw(ip->i_sbd,
  708. (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
  709. ip->i_di.di_mode = er->er_mode;
  710. }
  711. ip->i_di.di_ctime = get_seconds();
  712. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  713. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  714. brelse(dibh);
  715. out:
  716. gfs2_trans_end(ip->i_sbd);
  717. return error;
  718. }
  719. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  720. struct gfs2_ea_request *er, void *private)
  721. {
  722. struct ea_set *es = private;
  723. struct gfs2_ea_header *ea = es->es_ea;
  724. int error;
  725. gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
  726. if (es->ea_split)
  727. ea = ea_split_ea(ea);
  728. error = ea_write(ip, ea, er);
  729. if (error)
  730. return error;
  731. if (es->es_el)
  732. ea_set_remove_stuffed(ip, es->es_el);
  733. return 0;
  734. }
  735. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  736. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  737. void *private)
  738. {
  739. struct ea_set *es = private;
  740. unsigned int size;
  741. int stuffed;
  742. int error;
  743. stuffed = ea_calc_size(ip->i_sbd, es->es_er, &size);
  744. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  745. if (GFS2_EA_REC_LEN(ea) < size)
  746. return 0;
  747. if (!GFS2_EA_IS_STUFFED(ea)) {
  748. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  749. if (error)
  750. return error;
  751. }
  752. es->ea_split = 0;
  753. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  754. es->ea_split = 1;
  755. else
  756. return 0;
  757. if (stuffed) {
  758. error = ea_set_simple_noalloc(ip, bh, ea, es);
  759. if (error)
  760. return error;
  761. } else {
  762. unsigned int blks;
  763. es->es_bh = bh;
  764. es->es_ea = ea;
  765. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  766. ip->i_sbd->sd_jbsize);
  767. error = ea_alloc_skeleton(ip, es->es_er, blks,
  768. ea_set_simple_alloc, es);
  769. if (error)
  770. return error;
  771. }
  772. return 1;
  773. }
  774. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  775. void *private)
  776. {
  777. struct gfs2_sbd *sdp = ip->i_sbd;
  778. struct buffer_head *indbh, *newbh;
  779. uint64_t *eablk;
  780. int error;
  781. int mh_size = sizeof(struct gfs2_meta_header);
  782. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  783. uint64_t *end;
  784. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  785. DIO_START | DIO_WAIT, &indbh);
  786. if (error)
  787. return error;
  788. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  789. error = -EIO;
  790. goto out;
  791. }
  792. eablk = (uint64_t *)(indbh->b_data + mh_size);
  793. end = eablk + sdp->sd_inptrs;
  794. for (; eablk < end; eablk++)
  795. if (!*eablk)
  796. break;
  797. if (eablk == end) {
  798. error = -ENOSPC;
  799. goto out;
  800. }
  801. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  802. } else {
  803. uint64_t blk;
  804. blk = gfs2_alloc_meta(ip);
  805. indbh = gfs2_meta_new(ip->i_gl, blk);
  806. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  807. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  808. gfs2_buffer_clear_tail(indbh, mh_size);
  809. eablk = (uint64_t *)(indbh->b_data + mh_size);
  810. *eablk = cpu_to_be64(ip->i_di.di_eattr);
  811. ip->i_di.di_eattr = blk;
  812. ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
  813. ip->i_di.di_blocks++;
  814. eablk++;
  815. }
  816. error = ea_alloc_blk(ip, &newbh);
  817. if (error)
  818. goto out;
  819. *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
  820. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  821. brelse(newbh);
  822. if (error)
  823. goto out;
  824. if (private)
  825. ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
  826. out:
  827. brelse(indbh);
  828. return error;
  829. }
  830. static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  831. struct gfs2_ea_location *el)
  832. {
  833. struct ea_set es;
  834. unsigned int blks = 2;
  835. int error;
  836. memset(&es, 0, sizeof(struct ea_set));
  837. es.es_er = er;
  838. es.es_el = el;
  839. error = ea_foreach(ip, ea_set_simple, &es);
  840. if (error > 0)
  841. return 0;
  842. if (error)
  843. return error;
  844. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
  845. blks++;
  846. if (GFS2_EAREQ_SIZE_STUFFED(er) > ip->i_sbd->sd_jbsize)
  847. blks += DIV_ROUND_UP(er->er_data_len, ip->i_sbd->sd_jbsize);
  848. return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
  849. }
  850. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  851. struct gfs2_ea_location *el)
  852. {
  853. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  854. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  855. gfs2_assert_withdraw(ip->i_sbd,
  856. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  857. }
  858. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
  859. }
  860. int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  861. {
  862. struct gfs2_ea_location el;
  863. int error;
  864. if (!ip->i_di.di_eattr) {
  865. if (er->er_flags & XATTR_REPLACE)
  866. return -ENODATA;
  867. return ea_init(ip, er);
  868. }
  869. error = gfs2_ea_find(ip, er, &el);
  870. if (error)
  871. return error;
  872. if (el.el_ea) {
  873. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
  874. brelse(el.el_bh);
  875. return -EPERM;
  876. }
  877. error = -EEXIST;
  878. if (!(er->er_flags & XATTR_CREATE)) {
  879. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  880. error = ea_set_i(ip, er, &el);
  881. if (!error && unstuffed)
  882. ea_set_remove_unstuffed(ip, &el);
  883. }
  884. brelse(el.el_bh);
  885. } else {
  886. error = -ENODATA;
  887. if (!(er->er_flags & XATTR_REPLACE))
  888. error = ea_set_i(ip, er, NULL);
  889. }
  890. return error;
  891. }
  892. int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  893. {
  894. struct gfs2_holder i_gh;
  895. int error;
  896. if (!er->er_name_len ||
  897. er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  898. return -EINVAL;
  899. if (!er->er_data || !er->er_data_len) {
  900. er->er_data = NULL;
  901. er->er_data_len = 0;
  902. }
  903. error = ea_check_size(ip->i_sbd, er);
  904. if (error)
  905. return error;
  906. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  907. if (error)
  908. return error;
  909. if (IS_IMMUTABLE(ip->i_vnode))
  910. error = -EPERM;
  911. else
  912. error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
  913. gfs2_glock_dq_uninit(&i_gh);
  914. return error;
  915. }
  916. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  917. {
  918. struct gfs2_ea_header *ea = el->el_ea;
  919. struct gfs2_ea_header *prev = el->el_prev;
  920. struct buffer_head *dibh;
  921. int error;
  922. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
  923. if (error)
  924. return error;
  925. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  926. if (prev) {
  927. uint32_t len;
  928. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  929. prev->ea_rec_len = cpu_to_be32(len);
  930. if (GFS2_EA_IS_LAST(ea))
  931. prev->ea_flags |= GFS2_EAFLAG_LAST;
  932. } else
  933. ea->ea_type = GFS2_EATYPE_UNUSED;
  934. error = gfs2_meta_inode_buffer(ip, &dibh);
  935. if (!error) {
  936. ip->i_di.di_ctime = get_seconds();
  937. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  938. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  939. brelse(dibh);
  940. }
  941. gfs2_trans_end(ip->i_sbd);
  942. return error;
  943. }
  944. int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  945. {
  946. struct gfs2_ea_location el;
  947. int error;
  948. if (!ip->i_di.di_eattr)
  949. return -ENODATA;
  950. error = gfs2_ea_find(ip, er, &el);
  951. if (error)
  952. return error;
  953. if (!el.el_ea)
  954. return -ENODATA;
  955. if (GFS2_EA_IS_STUFFED(el.el_ea))
  956. error = ea_remove_stuffed(ip, &el);
  957. else
  958. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
  959. 0);
  960. brelse(el.el_bh);
  961. return error;
  962. }
  963. /**
  964. * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
  965. * @ip: pointer to the inode of the target file
  966. * @er: request information
  967. *
  968. * Returns: errno
  969. */
  970. int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  971. {
  972. struct gfs2_holder i_gh;
  973. int error;
  974. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  975. return -EINVAL;
  976. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  977. if (error)
  978. return error;
  979. if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
  980. error = -EPERM;
  981. else
  982. error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
  983. gfs2_glock_dq_uninit(&i_gh);
  984. return error;
  985. }
  986. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  987. struct gfs2_ea_header *ea, char *data)
  988. {
  989. struct gfs2_sbd *sdp = ip->i_sbd;
  990. struct buffer_head **bh;
  991. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  992. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  993. uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
  994. unsigned int x;
  995. int error;
  996. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  997. if (!bh)
  998. return -ENOMEM;
  999. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  1000. if (error)
  1001. goto out;
  1002. for (x = 0; x < nptrs; x++) {
  1003. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
  1004. DIO_START, bh + x);
  1005. if (error) {
  1006. while (x--)
  1007. brelse(bh[x]);
  1008. goto fail;
  1009. }
  1010. dataptrs++;
  1011. }
  1012. for (x = 0; x < nptrs; x++) {
  1013. error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
  1014. if (error) {
  1015. for (; x < nptrs; x++)
  1016. brelse(bh[x]);
  1017. goto fail;
  1018. }
  1019. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  1020. for (; x < nptrs; x++)
  1021. brelse(bh[x]);
  1022. error = -EIO;
  1023. goto fail;
  1024. }
  1025. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1026. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
  1027. data,
  1028. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1029. amount -= sdp->sd_jbsize;
  1030. data += sdp->sd_jbsize;
  1031. brelse(bh[x]);
  1032. }
  1033. out:
  1034. kfree(bh);
  1035. return error;
  1036. fail:
  1037. gfs2_trans_end(sdp);
  1038. kfree(bh);
  1039. return error;
  1040. }
  1041. int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  1042. struct iattr *attr, char *data)
  1043. {
  1044. struct buffer_head *dibh;
  1045. int error;
  1046. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  1047. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
  1048. if (error)
  1049. return error;
  1050. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  1051. memcpy(GFS2_EA2DATA(el->el_ea),
  1052. data,
  1053. GFS2_EA_DATA_LEN(el->el_ea));
  1054. } else
  1055. error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
  1056. if (error)
  1057. return error;
  1058. error = gfs2_meta_inode_buffer(ip, &dibh);
  1059. if (!error) {
  1060. error = inode_setattr(ip->i_vnode, attr);
  1061. gfs2_assert_warn(ip->i_sbd, !error);
  1062. gfs2_inode_attr_out(ip);
  1063. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1064. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1065. brelse(dibh);
  1066. }
  1067. gfs2_trans_end(ip->i_sbd);
  1068. return error;
  1069. }
  1070. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1071. {
  1072. struct gfs2_sbd *sdp = ip->i_sbd;
  1073. struct gfs2_rgrp_list rlist;
  1074. struct buffer_head *indbh, *dibh;
  1075. uint64_t *eablk, *end;
  1076. unsigned int rg_blocks = 0;
  1077. uint64_t bstart = 0;
  1078. unsigned int blen = 0;
  1079. unsigned int blks = 0;
  1080. unsigned int x;
  1081. int error;
  1082. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1083. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  1084. DIO_START | DIO_WAIT, &indbh);
  1085. if (error)
  1086. return error;
  1087. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1088. error = -EIO;
  1089. goto out;
  1090. }
  1091. eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1092. end = eablk + sdp->sd_inptrs;
  1093. for (; eablk < end; eablk++) {
  1094. uint64_t bn;
  1095. if (!*eablk)
  1096. break;
  1097. bn = be64_to_cpu(*eablk);
  1098. if (bstart + blen == bn)
  1099. blen++;
  1100. else {
  1101. if (bstart)
  1102. gfs2_rlist_add(sdp, &rlist, bstart);
  1103. bstart = bn;
  1104. blen = 1;
  1105. }
  1106. blks++;
  1107. }
  1108. if (bstart)
  1109. gfs2_rlist_add(sdp, &rlist, bstart);
  1110. else
  1111. goto out;
  1112. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  1113. for (x = 0; x < rlist.rl_rgrps; x++) {
  1114. struct gfs2_rgrpd *rgd;
  1115. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1116. rg_blocks += rgd->rd_ri.ri_length;
  1117. }
  1118. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1119. if (error)
  1120. goto out_rlist_free;
  1121. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  1122. RES_INDIRECT + RES_STATFS +
  1123. RES_QUOTA, blks);
  1124. if (error)
  1125. goto out_gunlock;
  1126. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  1127. eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1128. bstart = 0;
  1129. blen = 0;
  1130. for (; eablk < end; eablk++) {
  1131. uint64_t bn;
  1132. if (!*eablk)
  1133. break;
  1134. bn = be64_to_cpu(*eablk);
  1135. if (bstart + blen == bn)
  1136. blen++;
  1137. else {
  1138. if (bstart)
  1139. gfs2_free_meta(ip, bstart, blen);
  1140. bstart = bn;
  1141. blen = 1;
  1142. }
  1143. *eablk = 0;
  1144. if (!ip->i_di.di_blocks)
  1145. gfs2_consist_inode(ip);
  1146. ip->i_di.di_blocks--;
  1147. }
  1148. if (bstart)
  1149. gfs2_free_meta(ip, bstart, blen);
  1150. ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
  1151. error = gfs2_meta_inode_buffer(ip, &dibh);
  1152. if (!error) {
  1153. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1154. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1155. brelse(dibh);
  1156. }
  1157. gfs2_trans_end(sdp);
  1158. out_gunlock:
  1159. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1160. out_rlist_free:
  1161. gfs2_rlist_free(&rlist);
  1162. out:
  1163. brelse(indbh);
  1164. return error;
  1165. }
  1166. static int ea_dealloc_block(struct gfs2_inode *ip)
  1167. {
  1168. struct gfs2_sbd *sdp = ip->i_sbd;
  1169. struct gfs2_alloc *al = &ip->i_alloc;
  1170. struct gfs2_rgrpd *rgd;
  1171. struct buffer_head *dibh;
  1172. int error;
  1173. rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
  1174. if (!rgd) {
  1175. gfs2_consist_inode(ip);
  1176. return -EIO;
  1177. }
  1178. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  1179. &al->al_rgd_gh);
  1180. if (error)
  1181. return error;
  1182. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
  1183. RES_STATFS + RES_QUOTA, 1);
  1184. if (error)
  1185. goto out_gunlock;
  1186. gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
  1187. ip->i_di.di_eattr = 0;
  1188. if (!ip->i_di.di_blocks)
  1189. gfs2_consist_inode(ip);
  1190. ip->i_di.di_blocks--;
  1191. error = gfs2_meta_inode_buffer(ip, &dibh);
  1192. if (!error) {
  1193. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1194. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1195. brelse(dibh);
  1196. }
  1197. gfs2_trans_end(sdp);
  1198. out_gunlock:
  1199. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1200. return error;
  1201. }
  1202. /**
  1203. * gfs2_ea_dealloc - deallocate the extended attribute fork
  1204. * @ip: the inode
  1205. *
  1206. * Returns: errno
  1207. */
  1208. int gfs2_ea_dealloc(struct gfs2_inode *ip)
  1209. {
  1210. struct gfs2_alloc *al;
  1211. int error;
  1212. al = gfs2_alloc_get(ip);
  1213. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1214. if (error)
  1215. goto out_alloc;
  1216. error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
  1217. if (error)
  1218. goto out_quota;
  1219. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1220. if (error)
  1221. goto out_rindex;
  1222. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  1223. error = ea_dealloc_indirect(ip);
  1224. if (error)
  1225. goto out_rindex;
  1226. }
  1227. error = ea_dealloc_block(ip);
  1228. out_rindex:
  1229. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1230. out_quota:
  1231. gfs2_quota_unhold(ip);
  1232. out_alloc:
  1233. gfs2_alloc_put(ip);
  1234. return error;
  1235. }