eattr.c 31 KB

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