eattr.c 31 KB

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