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. __be64 *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 = (__be64 *)(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. __be64 *dataptrs;
  189. u64 bn = 0;
  190. u64 bstart = 0;
  191. unsigned int blen = 0;
  192. unsigned int blks = 0;
  193. unsigned int x;
  194. int error;
  195. if (GFS2_EA_IS_STUFFED(ea))
  196. return 0;
  197. dataptrs = GFS2_EA2DATAPTRS(ea);
  198. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  199. if (*dataptrs) {
  200. blks++;
  201. bn = be64_to_cpu(*dataptrs);
  202. }
  203. }
  204. if (!blks)
  205. return 0;
  206. rgd = gfs2_blk2rgrpd(sdp, bn);
  207. if (!rgd) {
  208. gfs2_consist_inode(ip);
  209. return -EIO;
  210. }
  211. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
  212. if (error)
  213. return error;
  214. error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length + RES_DINODE +
  215. RES_EATTR + RES_STATFS + RES_QUOTA, blks);
  216. if (error)
  217. goto out_gunlock;
  218. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  219. dataptrs = GFS2_EA2DATAPTRS(ea);
  220. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  221. if (!*dataptrs)
  222. break;
  223. bn = be64_to_cpu(*dataptrs);
  224. if (bstart + blen == bn)
  225. blen++;
  226. else {
  227. if (bstart)
  228. gfs2_free_meta(ip, bstart, blen);
  229. bstart = bn;
  230. blen = 1;
  231. }
  232. *dataptrs = 0;
  233. if (!ip->i_di.di_blocks)
  234. gfs2_consist_inode(ip);
  235. ip->i_di.di_blocks--;
  236. }
  237. if (bstart)
  238. gfs2_free_meta(ip, bstart, blen);
  239. if (prev && !leave) {
  240. u32 len;
  241. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  242. prev->ea_rec_len = cpu_to_be32(len);
  243. if (GFS2_EA_IS_LAST(ea))
  244. prev->ea_flags |= GFS2_EAFLAG_LAST;
  245. } else {
  246. ea->ea_type = GFS2_EATYPE_UNUSED;
  247. ea->ea_num_ptrs = 0;
  248. }
  249. error = gfs2_meta_inode_buffer(ip, &dibh);
  250. if (!error) {
  251. ip->i_inode.i_ctime.tv_sec = get_seconds();
  252. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  253. gfs2_dinode_out(ip, dibh->b_data);
  254. brelse(dibh);
  255. }
  256. gfs2_trans_end(sdp);
  257. out_gunlock:
  258. gfs2_glock_dq_uninit(&rg_gh);
  259. return error;
  260. }
  261. static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  262. struct gfs2_ea_header *ea,
  263. struct gfs2_ea_header *prev, int leave)
  264. {
  265. struct gfs2_alloc *al;
  266. int error;
  267. al = gfs2_alloc_get(ip);
  268. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  269. if (error)
  270. goto out_alloc;
  271. error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
  272. if (error)
  273. goto out_quota;
  274. error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
  275. gfs2_glock_dq_uninit(&al->al_ri_gh);
  276. out_quota:
  277. gfs2_quota_unhold(ip);
  278. out_alloc:
  279. gfs2_alloc_put(ip);
  280. return error;
  281. }
  282. struct ea_list {
  283. struct gfs2_ea_request *ei_er;
  284. unsigned int ei_size;
  285. };
  286. static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
  287. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  288. void *private)
  289. {
  290. struct ea_list *ei = private;
  291. struct gfs2_ea_request *er = ei->ei_er;
  292. unsigned int ea_size = gfs2_ea_strlen(ea);
  293. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  294. return 0;
  295. if (er->er_data_len) {
  296. char *prefix = NULL;
  297. unsigned int l = 0;
  298. char c = 0;
  299. if (ei->ei_size + ea_size > er->er_data_len)
  300. return -ERANGE;
  301. switch (ea->ea_type) {
  302. case GFS2_EATYPE_USR:
  303. prefix = "user.";
  304. l = 5;
  305. break;
  306. case GFS2_EATYPE_SYS:
  307. prefix = "system.";
  308. l = 7;
  309. break;
  310. case GFS2_EATYPE_SECURITY:
  311. prefix = "security.";
  312. l = 9;
  313. break;
  314. }
  315. BUG_ON(l == 0);
  316. memcpy(er->er_data + ei->ei_size, prefix, l);
  317. memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
  318. ea->ea_name_len);
  319. memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
  320. }
  321. ei->ei_size += ea_size;
  322. return 0;
  323. }
  324. /**
  325. * gfs2_ea_list -
  326. * @ip:
  327. * @er:
  328. *
  329. * Returns: actual size of data on success, -errno on error
  330. */
  331. int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  332. {
  333. struct gfs2_holder i_gh;
  334. int error;
  335. if (!er->er_data || !er->er_data_len) {
  336. er->er_data = NULL;
  337. er->er_data_len = 0;
  338. }
  339. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  340. if (error)
  341. return error;
  342. if (ip->i_di.di_eattr) {
  343. struct ea_list ei = { .ei_er = er, .ei_size = 0 };
  344. error = ea_foreach(ip, ea_list_i, &ei);
  345. if (!error)
  346. error = ei.ei_size;
  347. }
  348. gfs2_glock_dq_uninit(&i_gh);
  349. return error;
  350. }
  351. /**
  352. * ea_get_unstuffed - actually copies the unstuffed data into the
  353. * request buffer
  354. * @ip: The GFS2 inode
  355. * @ea: The extended attribute header structure
  356. * @data: The data to be copied
  357. *
  358. * Returns: errno
  359. */
  360. static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  361. char *data)
  362. {
  363. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  364. struct buffer_head **bh;
  365. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  366. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  367. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  368. unsigned int x;
  369. int error = 0;
  370. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  371. if (!bh)
  372. return -ENOMEM;
  373. for (x = 0; x < nptrs; x++) {
  374. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
  375. bh + x);
  376. if (error) {
  377. while (x--)
  378. brelse(bh[x]);
  379. goto out;
  380. }
  381. dataptrs++;
  382. }
  383. for (x = 0; x < nptrs; x++) {
  384. error = gfs2_meta_wait(sdp, bh[x]);
  385. if (error) {
  386. for (; x < nptrs; x++)
  387. brelse(bh[x]);
  388. goto out;
  389. }
  390. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  391. for (; x < nptrs; x++)
  392. brelse(bh[x]);
  393. error = -EIO;
  394. goto out;
  395. }
  396. memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
  397. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  398. amount -= sdp->sd_jbsize;
  399. data += sdp->sd_jbsize;
  400. brelse(bh[x]);
  401. }
  402. out:
  403. kfree(bh);
  404. return error;
  405. }
  406. int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  407. char *data)
  408. {
  409. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  410. memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
  411. return 0;
  412. } else
  413. return ea_get_unstuffed(ip, el->el_ea, data);
  414. }
  415. /**
  416. * gfs2_ea_get_i -
  417. * @ip: The GFS2 inode
  418. * @er: The request structure
  419. *
  420. * Returns: actual size of data on success, -errno on error
  421. */
  422. int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  423. {
  424. struct gfs2_ea_location el;
  425. int error;
  426. if (!ip->i_di.di_eattr)
  427. return -ENODATA;
  428. error = gfs2_ea_find(ip, er, &el);
  429. if (error)
  430. return error;
  431. if (!el.el_ea)
  432. return -ENODATA;
  433. if (er->er_data_len) {
  434. if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
  435. error = -ERANGE;
  436. else
  437. error = gfs2_ea_get_copy(ip, &el, er->er_data);
  438. }
  439. if (!error)
  440. error = GFS2_EA_DATA_LEN(el.el_ea);
  441. brelse(el.el_bh);
  442. return error;
  443. }
  444. /**
  445. * gfs2_ea_get -
  446. * @ip: The GFS2 inode
  447. * @er: The request structure
  448. *
  449. * Returns: actual size of data on success, -errno on error
  450. */
  451. int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  452. {
  453. struct gfs2_holder i_gh;
  454. int error;
  455. if (!er->er_name_len ||
  456. er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  457. return -EINVAL;
  458. if (!er->er_data || !er->er_data_len) {
  459. er->er_data = NULL;
  460. er->er_data_len = 0;
  461. }
  462. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  463. if (error)
  464. return error;
  465. error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
  466. gfs2_glock_dq_uninit(&i_gh);
  467. return error;
  468. }
  469. /**
  470. * ea_alloc_blk - allocates a new block for extended attributes.
  471. * @ip: A pointer to the inode that's getting extended attributes
  472. * @bhp: Pointer to pointer to a struct buffer_head
  473. *
  474. * Returns: errno
  475. */
  476. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  477. {
  478. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  479. struct gfs2_ea_header *ea;
  480. u64 block;
  481. block = gfs2_alloc_meta(ip);
  482. *bhp = gfs2_meta_new(ip->i_gl, block);
  483. gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
  484. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  485. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  486. ea = GFS2_EA_BH2FIRST(*bhp);
  487. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  488. ea->ea_type = GFS2_EATYPE_UNUSED;
  489. ea->ea_flags = GFS2_EAFLAG_LAST;
  490. ea->ea_num_ptrs = 0;
  491. ip->i_di.di_blocks++;
  492. return 0;
  493. }
  494. /**
  495. * ea_write - writes the request info to an ea, creating new blocks if
  496. * necessary
  497. * @ip: inode that is being modified
  498. * @ea: the location of the new ea in a block
  499. * @er: the write request
  500. *
  501. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  502. *
  503. * returns : errno
  504. */
  505. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  506. struct gfs2_ea_request *er)
  507. {
  508. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  509. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  510. ea->ea_name_len = er->er_name_len;
  511. ea->ea_type = er->er_type;
  512. ea->__pad = 0;
  513. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  514. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  515. ea->ea_num_ptrs = 0;
  516. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  517. } else {
  518. __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
  519. const char *data = er->er_data;
  520. unsigned int data_len = er->er_data_len;
  521. unsigned int copy;
  522. unsigned int x;
  523. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  524. for (x = 0; x < ea->ea_num_ptrs; x++) {
  525. struct buffer_head *bh;
  526. u64 block;
  527. int mh_size = sizeof(struct gfs2_meta_header);
  528. block = gfs2_alloc_meta(ip);
  529. bh = gfs2_meta_new(ip->i_gl, block);
  530. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  531. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  532. ip->i_di.di_blocks++;
  533. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  534. data_len;
  535. memcpy(bh->b_data + mh_size, data, copy);
  536. if (copy < sdp->sd_jbsize)
  537. memset(bh->b_data + mh_size + copy, 0,
  538. sdp->sd_jbsize - copy);
  539. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  540. data += copy;
  541. data_len -= copy;
  542. brelse(bh);
  543. }
  544. gfs2_assert_withdraw(sdp, !data_len);
  545. }
  546. return 0;
  547. }
  548. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  549. struct gfs2_ea_request *er, void *private);
  550. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  551. unsigned int blks,
  552. ea_skeleton_call_t skeleton_call, void *private)
  553. {
  554. struct gfs2_alloc *al;
  555. struct buffer_head *dibh;
  556. int error;
  557. al = gfs2_alloc_get(ip);
  558. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  559. if (error)
  560. goto out;
  561. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  562. if (error)
  563. goto out_gunlock_q;
  564. al->al_requested = blks;
  565. error = gfs2_inplace_reserve(ip);
  566. if (error)
  567. goto out_gunlock_q;
  568. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  569. blks + al->al_rgd->rd_ri.ri_length +
  570. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  571. if (error)
  572. goto out_ipres;
  573. error = skeleton_call(ip, er, private);
  574. if (error)
  575. goto out_end_trans;
  576. error = gfs2_meta_inode_buffer(ip, &dibh);
  577. if (!error) {
  578. if (er->er_flags & GFS2_ERF_MODE) {
  579. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  580. (ip->i_inode.i_mode & S_IFMT) ==
  581. (er->er_mode & S_IFMT));
  582. ip->i_inode.i_mode = er->er_mode;
  583. }
  584. ip->i_inode.i_ctime.tv_sec = get_seconds();
  585. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  586. gfs2_dinode_out(ip, dibh->b_data);
  587. brelse(dibh);
  588. }
  589. out_end_trans:
  590. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  591. out_ipres:
  592. gfs2_inplace_release(ip);
  593. out_gunlock_q:
  594. gfs2_quota_unlock(ip);
  595. out:
  596. gfs2_alloc_put(ip);
  597. return error;
  598. }
  599. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  600. void *private)
  601. {
  602. struct buffer_head *bh;
  603. int error;
  604. error = ea_alloc_blk(ip, &bh);
  605. if (error)
  606. return error;
  607. ip->i_di.di_eattr = bh->b_blocknr;
  608. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  609. brelse(bh);
  610. return error;
  611. }
  612. /**
  613. * ea_init - initializes a new eattr block
  614. * @ip:
  615. * @er:
  616. *
  617. * Returns: errno
  618. */
  619. static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  620. {
  621. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  622. unsigned int blks = 1;
  623. if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
  624. blks += DIV_ROUND_UP(er->er_data_len, jbsize);
  625. return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
  626. }
  627. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  628. {
  629. u32 ea_size = GFS2_EA_SIZE(ea);
  630. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  631. ea_size);
  632. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  633. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  634. ea->ea_rec_len = cpu_to_be32(ea_size);
  635. ea->ea_flags ^= last;
  636. new->ea_rec_len = cpu_to_be32(new_size);
  637. new->ea_flags = last;
  638. return new;
  639. }
  640. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  641. struct gfs2_ea_location *el)
  642. {
  643. struct gfs2_ea_header *ea = el->el_ea;
  644. struct gfs2_ea_header *prev = el->el_prev;
  645. u32 len;
  646. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  647. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  648. ea->ea_type = GFS2_EATYPE_UNUSED;
  649. return;
  650. } else if (GFS2_EA2NEXT(prev) != ea) {
  651. prev = GFS2_EA2NEXT(prev);
  652. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  653. }
  654. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  655. prev->ea_rec_len = cpu_to_be32(len);
  656. if (GFS2_EA_IS_LAST(ea))
  657. prev->ea_flags |= GFS2_EAFLAG_LAST;
  658. }
  659. struct ea_set {
  660. int ea_split;
  661. struct gfs2_ea_request *es_er;
  662. struct gfs2_ea_location *es_el;
  663. struct buffer_head *es_bh;
  664. struct gfs2_ea_header *es_ea;
  665. };
  666. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  667. struct gfs2_ea_header *ea, struct ea_set *es)
  668. {
  669. struct gfs2_ea_request *er = es->es_er;
  670. struct buffer_head *dibh;
  671. int error;
  672. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  673. if (error)
  674. return error;
  675. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  676. if (es->ea_split)
  677. ea = ea_split_ea(ea);
  678. ea_write(ip, ea, er);
  679. if (es->es_el)
  680. ea_set_remove_stuffed(ip, es->es_el);
  681. error = gfs2_meta_inode_buffer(ip, &dibh);
  682. if (error)
  683. goto out;
  684. if (er->er_flags & GFS2_ERF_MODE) {
  685. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  686. (ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT));
  687. ip->i_inode.i_mode = er->er_mode;
  688. }
  689. ip->i_inode.i_ctime.tv_sec = get_seconds();
  690. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  691. gfs2_dinode_out(ip, dibh->b_data);
  692. brelse(dibh);
  693. out:
  694. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  695. return error;
  696. }
  697. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  698. struct gfs2_ea_request *er, void *private)
  699. {
  700. struct ea_set *es = private;
  701. struct gfs2_ea_header *ea = es->es_ea;
  702. int error;
  703. gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
  704. if (es->ea_split)
  705. ea = ea_split_ea(ea);
  706. error = ea_write(ip, ea, er);
  707. if (error)
  708. return error;
  709. if (es->es_el)
  710. ea_set_remove_stuffed(ip, es->es_el);
  711. return 0;
  712. }
  713. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  714. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  715. void *private)
  716. {
  717. struct ea_set *es = private;
  718. unsigned int size;
  719. int stuffed;
  720. int error;
  721. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
  722. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  723. if (GFS2_EA_REC_LEN(ea) < size)
  724. return 0;
  725. if (!GFS2_EA_IS_STUFFED(ea)) {
  726. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  727. if (error)
  728. return error;
  729. }
  730. es->ea_split = 0;
  731. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  732. es->ea_split = 1;
  733. else
  734. return 0;
  735. if (stuffed) {
  736. error = ea_set_simple_noalloc(ip, bh, ea, es);
  737. if (error)
  738. return error;
  739. } else {
  740. unsigned int blks;
  741. es->es_bh = bh;
  742. es->es_ea = ea;
  743. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  744. GFS2_SB(&ip->i_inode)->sd_jbsize);
  745. error = ea_alloc_skeleton(ip, es->es_er, blks,
  746. ea_set_simple_alloc, es);
  747. if (error)
  748. return error;
  749. }
  750. return 1;
  751. }
  752. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  753. void *private)
  754. {
  755. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  756. struct buffer_head *indbh, *newbh;
  757. __be64 *eablk;
  758. int error;
  759. int mh_size = sizeof(struct gfs2_meta_header);
  760. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  761. __be64 *end;
  762. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT,
  763. &indbh);
  764. if (error)
  765. return error;
  766. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  767. error = -EIO;
  768. goto out;
  769. }
  770. eablk = (__be64 *)(indbh->b_data + mh_size);
  771. end = eablk + sdp->sd_inptrs;
  772. for (; eablk < end; eablk++)
  773. if (!*eablk)
  774. break;
  775. if (eablk == end) {
  776. error = -ENOSPC;
  777. goto out;
  778. }
  779. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  780. } else {
  781. u64 blk;
  782. blk = gfs2_alloc_meta(ip);
  783. indbh = gfs2_meta_new(ip->i_gl, blk);
  784. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  785. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  786. gfs2_buffer_clear_tail(indbh, mh_size);
  787. eablk = (__be64 *)(indbh->b_data + mh_size);
  788. *eablk = cpu_to_be64(ip->i_di.di_eattr);
  789. ip->i_di.di_eattr = blk;
  790. ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
  791. ip->i_di.di_blocks++;
  792. eablk++;
  793. }
  794. error = ea_alloc_blk(ip, &newbh);
  795. if (error)
  796. goto out;
  797. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  798. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  799. brelse(newbh);
  800. if (error)
  801. goto out;
  802. if (private)
  803. ea_set_remove_stuffed(ip, private);
  804. out:
  805. brelse(indbh);
  806. return error;
  807. }
  808. static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  809. struct gfs2_ea_location *el)
  810. {
  811. struct ea_set es;
  812. unsigned int blks = 2;
  813. int error;
  814. memset(&es, 0, sizeof(struct ea_set));
  815. es.es_er = er;
  816. es.es_el = el;
  817. error = ea_foreach(ip, ea_set_simple, &es);
  818. if (error > 0)
  819. return 0;
  820. if (error)
  821. return error;
  822. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
  823. blks++;
  824. if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  825. blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  826. return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
  827. }
  828. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  829. struct gfs2_ea_location *el)
  830. {
  831. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  832. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  833. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  834. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  835. }
  836. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
  837. }
  838. int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  839. {
  840. struct gfs2_ea_location el;
  841. int error;
  842. if (!ip->i_di.di_eattr) {
  843. if (er->er_flags & XATTR_REPLACE)
  844. return -ENODATA;
  845. return ea_init(ip, er);
  846. }
  847. error = gfs2_ea_find(ip, er, &el);
  848. if (error)
  849. return error;
  850. if (el.el_ea) {
  851. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
  852. brelse(el.el_bh);
  853. return -EPERM;
  854. }
  855. error = -EEXIST;
  856. if (!(er->er_flags & XATTR_CREATE)) {
  857. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  858. error = ea_set_i(ip, er, &el);
  859. if (!error && unstuffed)
  860. ea_set_remove_unstuffed(ip, &el);
  861. }
  862. brelse(el.el_bh);
  863. } else {
  864. error = -ENODATA;
  865. if (!(er->er_flags & XATTR_REPLACE))
  866. error = ea_set_i(ip, er, NULL);
  867. }
  868. return error;
  869. }
  870. int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  871. {
  872. struct gfs2_holder i_gh;
  873. int error;
  874. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  875. return -EINVAL;
  876. if (!er->er_data || !er->er_data_len) {
  877. er->er_data = NULL;
  878. er->er_data_len = 0;
  879. }
  880. error = ea_check_size(GFS2_SB(&ip->i_inode), er);
  881. if (error)
  882. return error;
  883. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  884. if (error)
  885. return error;
  886. if (IS_IMMUTABLE(&ip->i_inode))
  887. error = -EPERM;
  888. else
  889. error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
  890. gfs2_glock_dq_uninit(&i_gh);
  891. return error;
  892. }
  893. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  894. {
  895. struct gfs2_ea_header *ea = el->el_ea;
  896. struct gfs2_ea_header *prev = el->el_prev;
  897. struct buffer_head *dibh;
  898. int error;
  899. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  900. if (error)
  901. return error;
  902. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  903. if (prev) {
  904. u32 len;
  905. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  906. prev->ea_rec_len = cpu_to_be32(len);
  907. if (GFS2_EA_IS_LAST(ea))
  908. prev->ea_flags |= GFS2_EAFLAG_LAST;
  909. } else
  910. ea->ea_type = GFS2_EATYPE_UNUSED;
  911. error = gfs2_meta_inode_buffer(ip, &dibh);
  912. if (!error) {
  913. ip->i_inode.i_ctime.tv_sec = get_seconds();
  914. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  915. gfs2_dinode_out(ip, dibh->b_data);
  916. brelse(dibh);
  917. }
  918. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  919. return error;
  920. }
  921. int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  922. {
  923. struct gfs2_ea_location el;
  924. int error;
  925. if (!ip->i_di.di_eattr)
  926. return -ENODATA;
  927. error = gfs2_ea_find(ip, er, &el);
  928. if (error)
  929. return error;
  930. if (!el.el_ea)
  931. return -ENODATA;
  932. if (GFS2_EA_IS_STUFFED(el.el_ea))
  933. error = ea_remove_stuffed(ip, &el);
  934. else
  935. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
  936. 0);
  937. brelse(el.el_bh);
  938. return error;
  939. }
  940. /**
  941. * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
  942. * @ip: pointer to the inode of the target file
  943. * @er: request information
  944. *
  945. * Returns: errno
  946. */
  947. int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  948. {
  949. struct gfs2_holder i_gh;
  950. int error;
  951. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  952. return -EINVAL;
  953. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  954. if (error)
  955. return error;
  956. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  957. error = -EPERM;
  958. else
  959. error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
  960. gfs2_glock_dq_uninit(&i_gh);
  961. return error;
  962. }
  963. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  964. struct gfs2_ea_header *ea, char *data)
  965. {
  966. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  967. struct buffer_head **bh;
  968. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  969. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  970. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  971. unsigned int x;
  972. int error;
  973. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  974. if (!bh)
  975. return -ENOMEM;
  976. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  977. if (error)
  978. goto out;
  979. for (x = 0; x < nptrs; x++) {
  980. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
  981. bh + x);
  982. if (error) {
  983. while (x--)
  984. brelse(bh[x]);
  985. goto fail;
  986. }
  987. dataptrs++;
  988. }
  989. for (x = 0; x < nptrs; x++) {
  990. error = gfs2_meta_wait(sdp, bh[x]);
  991. if (error) {
  992. for (; x < nptrs; x++)
  993. brelse(bh[x]);
  994. goto fail;
  995. }
  996. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  997. for (; x < nptrs; x++)
  998. brelse(bh[x]);
  999. error = -EIO;
  1000. goto fail;
  1001. }
  1002. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1003. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
  1004. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1005. amount -= sdp->sd_jbsize;
  1006. data += sdp->sd_jbsize;
  1007. brelse(bh[x]);
  1008. }
  1009. out:
  1010. kfree(bh);
  1011. return error;
  1012. fail:
  1013. gfs2_trans_end(sdp);
  1014. kfree(bh);
  1015. return error;
  1016. }
  1017. int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  1018. struct iattr *attr, char *data)
  1019. {
  1020. struct buffer_head *dibh;
  1021. int error;
  1022. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  1023. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  1024. if (error)
  1025. return error;
  1026. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  1027. memcpy(GFS2_EA2DATA(el->el_ea), data,
  1028. GFS2_EA_DATA_LEN(el->el_ea));
  1029. } else
  1030. error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
  1031. if (error)
  1032. return error;
  1033. error = gfs2_meta_inode_buffer(ip, &dibh);
  1034. if (!error) {
  1035. error = inode_setattr(&ip->i_inode, attr);
  1036. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1037. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1038. gfs2_dinode_out(ip, 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. __be64 *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 = (__be64 *)(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 = (__be64 *)(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, 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, 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. }