eattr.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/xattr.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/lm_interface.h>
  16. #include <asm/uaccess.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "acl.h"
  20. #include "eaops.h"
  21. #include "eattr.h"
  22. #include "glock.h"
  23. #include "inode.h"
  24. #include "meta_io.h"
  25. #include "quota.h"
  26. #include "rgrp.h"
  27. #include "trans.h"
  28. #include "util.h"
  29. /**
  30. * ea_calc_size - returns the acutal number of bytes the request will take up
  31. * (not counting any unstuffed data blocks)
  32. * @sdp:
  33. * @er:
  34. * @size:
  35. *
  36. * Returns: 1 if the EA should be stuffed
  37. */
  38. static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
  39. unsigned int *size)
  40. {
  41. *size = GFS2_EAREQ_SIZE_STUFFED(er);
  42. if (*size <= sdp->sd_jbsize)
  43. return 1;
  44. *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
  45. return 0;
  46. }
  47. static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
  48. {
  49. unsigned int size;
  50. if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
  51. return -ERANGE;
  52. ea_calc_size(sdp, er, &size);
  53. /* This can only happen with 512 byte blocks */
  54. if (size > sdp->sd_jbsize)
  55. return -ERANGE;
  56. return 0;
  57. }
  58. typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
  59. struct gfs2_ea_header *ea,
  60. struct gfs2_ea_header *prev, void *private);
  61. static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
  62. ea_call_t ea_call, void *data)
  63. {
  64. struct gfs2_ea_header *ea, *prev = NULL;
  65. int error = 0;
  66. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
  67. return -EIO;
  68. for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
  69. if (!GFS2_EA_REC_LEN(ea))
  70. goto fail;
  71. if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
  72. bh->b_data + bh->b_size))
  73. goto fail;
  74. if (!GFS2_EATYPE_VALID(ea->ea_type))
  75. goto fail;
  76. error = ea_call(ip, bh, ea, prev, data);
  77. if (error)
  78. return error;
  79. if (GFS2_EA_IS_LAST(ea)) {
  80. if ((char *)GFS2_EA2NEXT(ea) !=
  81. bh->b_data + bh->b_size)
  82. goto fail;
  83. break;
  84. }
  85. }
  86. return error;
  87. fail:
  88. gfs2_consist_inode(ip);
  89. return -EIO;
  90. }
  91. static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
  92. {
  93. struct buffer_head *bh, *eabh;
  94. __be64 *eablk, *end;
  95. int error;
  96. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &bh);
  97. if (error)
  98. return error;
  99. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
  100. error = ea_foreach_i(ip, bh, ea_call, data);
  101. goto out;
  102. }
  103. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
  104. error = -EIO;
  105. goto out;
  106. }
  107. eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
  108. end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
  109. for (; eablk < end; eablk++) {
  110. u64 bn;
  111. if (!*eablk)
  112. break;
  113. bn = be64_to_cpu(*eablk);
  114. error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
  115. if (error)
  116. break;
  117. error = ea_foreach_i(ip, eabh, ea_call, data);
  118. brelse(eabh);
  119. if (error)
  120. break;
  121. }
  122. out:
  123. brelse(bh);
  124. return error;
  125. }
  126. struct ea_find {
  127. struct gfs2_ea_request *ef_er;
  128. struct gfs2_ea_location *ef_el;
  129. };
  130. static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
  131. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  132. void *private)
  133. {
  134. struct ea_find *ef = private;
  135. struct gfs2_ea_request *er = ef->ef_er;
  136. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  137. return 0;
  138. if (ea->ea_type == er->er_type) {
  139. if (ea->ea_name_len == er->er_name_len &&
  140. !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
  141. struct gfs2_ea_location *el = ef->ef_el;
  142. get_bh(bh);
  143. el->el_bh = bh;
  144. el->el_ea = ea;
  145. el->el_prev = prev;
  146. return 1;
  147. }
  148. }
  149. return 0;
  150. }
  151. int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  152. struct gfs2_ea_location *el)
  153. {
  154. struct ea_find ef;
  155. int error;
  156. ef.ef_er = er;
  157. ef.ef_el = el;
  158. memset(el, 0, sizeof(struct gfs2_ea_location));
  159. error = ea_foreach(ip, ea_find_i, &ef);
  160. if (error > 0)
  161. return 0;
  162. return error;
  163. }
  164. /**
  165. * ea_dealloc_unstuffed -
  166. * @ip:
  167. * @bh:
  168. * @ea:
  169. * @prev:
  170. * @private:
  171. *
  172. * Take advantage of the fact that all unstuffed blocks are
  173. * allocated from the same RG. But watch, this may not always
  174. * be true.
  175. *
  176. * Returns: errno
  177. */
  178. static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  179. struct gfs2_ea_header *ea,
  180. struct gfs2_ea_header *prev, void *private)
  181. {
  182. int *leave = private;
  183. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  184. struct gfs2_rgrpd *rgd;
  185. struct gfs2_holder rg_gh;
  186. struct buffer_head *dibh;
  187. __be64 *dataptrs;
  188. u64 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_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. gfs2_add_inode_blocks(&ip->i_inode, -1);
  233. }
  234. if (bstart)
  235. gfs2_free_meta(ip, bstart, blen);
  236. if (prev && !leave) {
  237. u32 len;
  238. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  239. prev->ea_rec_len = cpu_to_be32(len);
  240. if (GFS2_EA_IS_LAST(ea))
  241. prev->ea_flags |= GFS2_EAFLAG_LAST;
  242. } else {
  243. ea->ea_type = GFS2_EATYPE_UNUSED;
  244. ea->ea_num_ptrs = 0;
  245. }
  246. error = gfs2_meta_inode_buffer(ip, &dibh);
  247. if (!error) {
  248. ip->i_inode.i_ctime = CURRENT_TIME;
  249. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  250. gfs2_dinode_out(ip, dibh->b_data);
  251. brelse(dibh);
  252. }
  253. gfs2_trans_end(sdp);
  254. out_gunlock:
  255. gfs2_glock_dq_uninit(&rg_gh);
  256. return error;
  257. }
  258. static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  259. struct gfs2_ea_header *ea,
  260. struct gfs2_ea_header *prev, int leave)
  261. {
  262. struct gfs2_alloc *al;
  263. int error;
  264. al = gfs2_alloc_get(ip);
  265. if (!al)
  266. return -ENOMEM;
  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. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  367. unsigned int x;
  368. int error = 0;
  369. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  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. unsigned int n = 1;
  480. u64 block;
  481. block = gfs2_alloc_block(ip, &n);
  482. gfs2_trans_add_unrevoke(sdp, block, 1);
  483. *bhp = gfs2_meta_new(ip->i_gl, block);
  484. gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
  485. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  486. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  487. ea = GFS2_EA_BH2FIRST(*bhp);
  488. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  489. ea->ea_type = GFS2_EATYPE_UNUSED;
  490. ea->ea_flags = GFS2_EAFLAG_LAST;
  491. ea->ea_num_ptrs = 0;
  492. gfs2_add_inode_blocks(&ip->i_inode, 1);
  493. return 0;
  494. }
  495. /**
  496. * ea_write - writes the request info to an ea, creating new blocks if
  497. * necessary
  498. * @ip: inode that is being modified
  499. * @ea: the location of the new ea in a block
  500. * @er: the write request
  501. *
  502. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  503. *
  504. * returns : errno
  505. */
  506. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  507. struct gfs2_ea_request *er)
  508. {
  509. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  510. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  511. ea->ea_name_len = er->er_name_len;
  512. ea->ea_type = er->er_type;
  513. ea->__pad = 0;
  514. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  515. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  516. ea->ea_num_ptrs = 0;
  517. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  518. } else {
  519. __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
  520. const char *data = er->er_data;
  521. unsigned int data_len = er->er_data_len;
  522. unsigned int copy;
  523. unsigned int x;
  524. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  525. for (x = 0; x < ea->ea_num_ptrs; x++) {
  526. struct buffer_head *bh;
  527. u64 block;
  528. int mh_size = sizeof(struct gfs2_meta_header);
  529. unsigned int n = 1;
  530. block = gfs2_alloc_block(ip, &n);
  531. gfs2_trans_add_unrevoke(sdp, block, 1);
  532. bh = gfs2_meta_new(ip->i_gl, block);
  533. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  534. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  535. gfs2_add_inode_blocks(&ip->i_inode, 1);
  536. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  537. data_len;
  538. memcpy(bh->b_data + mh_size, data, copy);
  539. if (copy < sdp->sd_jbsize)
  540. memset(bh->b_data + mh_size + copy, 0,
  541. sdp->sd_jbsize - copy);
  542. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  543. data += copy;
  544. data_len -= copy;
  545. brelse(bh);
  546. }
  547. gfs2_assert_withdraw(sdp, !data_len);
  548. }
  549. return 0;
  550. }
  551. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  552. struct gfs2_ea_request *er, void *private);
  553. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  554. unsigned int blks,
  555. ea_skeleton_call_t skeleton_call, void *private)
  556. {
  557. struct gfs2_alloc *al;
  558. struct buffer_head *dibh;
  559. int error;
  560. al = gfs2_alloc_get(ip);
  561. if (!al)
  562. return -ENOMEM;
  563. error = gfs2_quota_lock_check(ip);
  564. if (error)
  565. goto out;
  566. al->al_requested = blks;
  567. error = gfs2_inplace_reserve(ip);
  568. if (error)
  569. goto out_gunlock_q;
  570. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  571. blks + al->al_rgd->rd_length +
  572. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  573. if (error)
  574. goto out_ipres;
  575. error = skeleton_call(ip, er, private);
  576. if (error)
  577. goto out_end_trans;
  578. error = gfs2_meta_inode_buffer(ip, &dibh);
  579. if (!error) {
  580. if (er->er_flags & GFS2_ERF_MODE) {
  581. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  582. (ip->i_inode.i_mode & S_IFMT) ==
  583. (er->er_mode & S_IFMT));
  584. ip->i_inode.i_mode = er->er_mode;
  585. }
  586. ip->i_inode.i_ctime = CURRENT_TIME;
  587. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  588. gfs2_dinode_out(ip, dibh->b_data);
  589. brelse(dibh);
  590. }
  591. out_end_trans:
  592. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  593. out_ipres:
  594. gfs2_inplace_release(ip);
  595. out_gunlock_q:
  596. gfs2_quota_unlock(ip);
  597. out:
  598. gfs2_alloc_put(ip);
  599. return error;
  600. }
  601. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  602. void *private)
  603. {
  604. struct buffer_head *bh;
  605. int error;
  606. error = ea_alloc_blk(ip, &bh);
  607. if (error)
  608. return error;
  609. ip->i_di.di_eattr = bh->b_blocknr;
  610. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  611. brelse(bh);
  612. return error;
  613. }
  614. /**
  615. * ea_init - initializes a new eattr block
  616. * @ip:
  617. * @er:
  618. *
  619. * Returns: errno
  620. */
  621. static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  622. {
  623. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  624. unsigned int blks = 1;
  625. if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
  626. blks += DIV_ROUND_UP(er->er_data_len, jbsize);
  627. return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
  628. }
  629. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  630. {
  631. u32 ea_size = GFS2_EA_SIZE(ea);
  632. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  633. ea_size);
  634. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  635. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  636. ea->ea_rec_len = cpu_to_be32(ea_size);
  637. ea->ea_flags ^= last;
  638. new->ea_rec_len = cpu_to_be32(new_size);
  639. new->ea_flags = last;
  640. return new;
  641. }
  642. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  643. struct gfs2_ea_location *el)
  644. {
  645. struct gfs2_ea_header *ea = el->el_ea;
  646. struct gfs2_ea_header *prev = el->el_prev;
  647. u32 len;
  648. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  649. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  650. ea->ea_type = GFS2_EATYPE_UNUSED;
  651. return;
  652. } else if (GFS2_EA2NEXT(prev) != ea) {
  653. prev = GFS2_EA2NEXT(prev);
  654. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  655. }
  656. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  657. prev->ea_rec_len = cpu_to_be32(len);
  658. if (GFS2_EA_IS_LAST(ea))
  659. prev->ea_flags |= GFS2_EAFLAG_LAST;
  660. }
  661. struct ea_set {
  662. int ea_split;
  663. struct gfs2_ea_request *es_er;
  664. struct gfs2_ea_location *es_el;
  665. struct buffer_head *es_bh;
  666. struct gfs2_ea_header *es_ea;
  667. };
  668. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  669. struct gfs2_ea_header *ea, struct ea_set *es)
  670. {
  671. struct gfs2_ea_request *er = es->es_er;
  672. struct buffer_head *dibh;
  673. int error;
  674. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  675. if (error)
  676. return error;
  677. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  678. if (es->ea_split)
  679. ea = ea_split_ea(ea);
  680. ea_write(ip, ea, er);
  681. if (es->es_el)
  682. ea_set_remove_stuffed(ip, es->es_el);
  683. error = gfs2_meta_inode_buffer(ip, &dibh);
  684. if (error)
  685. goto out;
  686. if (er->er_flags & GFS2_ERF_MODE) {
  687. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  688. (ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT));
  689. ip->i_inode.i_mode = er->er_mode;
  690. }
  691. ip->i_inode.i_ctime = CURRENT_TIME;
  692. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  693. gfs2_dinode_out(ip, dibh->b_data);
  694. brelse(dibh);
  695. out:
  696. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  697. return error;
  698. }
  699. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  700. struct gfs2_ea_request *er, void *private)
  701. {
  702. struct ea_set *es = private;
  703. struct gfs2_ea_header *ea = es->es_ea;
  704. int error;
  705. gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
  706. if (es->ea_split)
  707. ea = ea_split_ea(ea);
  708. error = ea_write(ip, ea, er);
  709. if (error)
  710. return error;
  711. if (es->es_el)
  712. ea_set_remove_stuffed(ip, es->es_el);
  713. return 0;
  714. }
  715. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  716. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  717. void *private)
  718. {
  719. struct ea_set *es = private;
  720. unsigned int size;
  721. int stuffed;
  722. int error;
  723. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
  724. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  725. if (GFS2_EA_REC_LEN(ea) < size)
  726. return 0;
  727. if (!GFS2_EA_IS_STUFFED(ea)) {
  728. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  729. if (error)
  730. return error;
  731. }
  732. es->ea_split = 0;
  733. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  734. es->ea_split = 1;
  735. else
  736. return 0;
  737. if (stuffed) {
  738. error = ea_set_simple_noalloc(ip, bh, ea, es);
  739. if (error)
  740. return error;
  741. } else {
  742. unsigned int blks;
  743. es->es_bh = bh;
  744. es->es_ea = ea;
  745. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  746. GFS2_SB(&ip->i_inode)->sd_jbsize);
  747. error = ea_alloc_skeleton(ip, es->es_er, blks,
  748. ea_set_simple_alloc, es);
  749. if (error)
  750. return error;
  751. }
  752. return 1;
  753. }
  754. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  755. void *private)
  756. {
  757. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  758. struct buffer_head *indbh, *newbh;
  759. __be64 *eablk;
  760. int error;
  761. int mh_size = sizeof(struct gfs2_meta_header);
  762. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  763. __be64 *end;
  764. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT,
  765. &indbh);
  766. if (error)
  767. return error;
  768. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  769. error = -EIO;
  770. goto out;
  771. }
  772. eablk = (__be64 *)(indbh->b_data + mh_size);
  773. end = eablk + sdp->sd_inptrs;
  774. for (; eablk < end; eablk++)
  775. if (!*eablk)
  776. break;
  777. if (eablk == end) {
  778. error = -ENOSPC;
  779. goto out;
  780. }
  781. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  782. } else {
  783. u64 blk;
  784. unsigned int n = 1;
  785. blk = gfs2_alloc_block(ip, &n);
  786. gfs2_trans_add_unrevoke(sdp, blk, 1);
  787. indbh = gfs2_meta_new(ip->i_gl, blk);
  788. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  789. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  790. gfs2_buffer_clear_tail(indbh, mh_size);
  791. eablk = (__be64 *)(indbh->b_data + mh_size);
  792. *eablk = cpu_to_be64(ip->i_di.di_eattr);
  793. ip->i_di.di_eattr = blk;
  794. ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
  795. gfs2_add_inode_blocks(&ip->i_inode, 1);
  796. eablk++;
  797. }
  798. error = ea_alloc_blk(ip, &newbh);
  799. if (error)
  800. goto out;
  801. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  802. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  803. brelse(newbh);
  804. if (error)
  805. goto out;
  806. if (private)
  807. ea_set_remove_stuffed(ip, private);
  808. out:
  809. brelse(indbh);
  810. return error;
  811. }
  812. static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  813. struct gfs2_ea_location *el)
  814. {
  815. struct ea_set es;
  816. unsigned int blks = 2;
  817. int error;
  818. memset(&es, 0, sizeof(struct ea_set));
  819. es.es_er = er;
  820. es.es_el = el;
  821. error = ea_foreach(ip, ea_set_simple, &es);
  822. if (error > 0)
  823. return 0;
  824. if (error)
  825. return error;
  826. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
  827. blks++;
  828. if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  829. blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  830. return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
  831. }
  832. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  833. struct gfs2_ea_location *el)
  834. {
  835. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  836. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  837. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  838. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  839. }
  840. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
  841. }
  842. int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  843. {
  844. struct gfs2_ea_location el;
  845. int error;
  846. if (!ip->i_di.di_eattr) {
  847. if (er->er_flags & XATTR_REPLACE)
  848. return -ENODATA;
  849. return ea_init(ip, er);
  850. }
  851. error = gfs2_ea_find(ip, er, &el);
  852. if (error)
  853. return error;
  854. if (el.el_ea) {
  855. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
  856. brelse(el.el_bh);
  857. return -EPERM;
  858. }
  859. error = -EEXIST;
  860. if (!(er->er_flags & XATTR_CREATE)) {
  861. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  862. error = ea_set_i(ip, er, &el);
  863. if (!error && unstuffed)
  864. ea_set_remove_unstuffed(ip, &el);
  865. }
  866. brelse(el.el_bh);
  867. } else {
  868. error = -ENODATA;
  869. if (!(er->er_flags & XATTR_REPLACE))
  870. error = ea_set_i(ip, er, NULL);
  871. }
  872. return error;
  873. }
  874. int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  875. {
  876. struct gfs2_holder i_gh;
  877. int error;
  878. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  879. return -EINVAL;
  880. if (!er->er_data || !er->er_data_len) {
  881. er->er_data = NULL;
  882. er->er_data_len = 0;
  883. }
  884. error = ea_check_size(GFS2_SB(&ip->i_inode), er);
  885. if (error)
  886. return error;
  887. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  888. if (error)
  889. return error;
  890. if (IS_IMMUTABLE(&ip->i_inode))
  891. error = -EPERM;
  892. else
  893. error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
  894. gfs2_glock_dq_uninit(&i_gh);
  895. return error;
  896. }
  897. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  898. {
  899. struct gfs2_ea_header *ea = el->el_ea;
  900. struct gfs2_ea_header *prev = el->el_prev;
  901. struct buffer_head *dibh;
  902. int error;
  903. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  904. if (error)
  905. return error;
  906. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  907. if (prev) {
  908. u32 len;
  909. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  910. prev->ea_rec_len = cpu_to_be32(len);
  911. if (GFS2_EA_IS_LAST(ea))
  912. prev->ea_flags |= GFS2_EAFLAG_LAST;
  913. } else
  914. ea->ea_type = GFS2_EATYPE_UNUSED;
  915. error = gfs2_meta_inode_buffer(ip, &dibh);
  916. if (!error) {
  917. ip->i_inode.i_ctime = CURRENT_TIME;
  918. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  919. gfs2_dinode_out(ip, dibh->b_data);
  920. brelse(dibh);
  921. }
  922. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  923. return error;
  924. }
  925. int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  926. {
  927. struct gfs2_ea_location el;
  928. int error;
  929. if (!ip->i_di.di_eattr)
  930. return -ENODATA;
  931. error = gfs2_ea_find(ip, er, &el);
  932. if (error)
  933. return error;
  934. if (!el.el_ea)
  935. return -ENODATA;
  936. if (GFS2_EA_IS_STUFFED(el.el_ea))
  937. error = ea_remove_stuffed(ip, &el);
  938. else
  939. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
  940. 0);
  941. brelse(el.el_bh);
  942. return error;
  943. }
  944. /**
  945. * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
  946. * @ip: pointer to the inode of the target file
  947. * @er: request information
  948. *
  949. * Returns: errno
  950. */
  951. int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  952. {
  953. struct gfs2_holder i_gh;
  954. int error;
  955. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  956. return -EINVAL;
  957. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  958. if (error)
  959. return error;
  960. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  961. error = -EPERM;
  962. else
  963. error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
  964. gfs2_glock_dq_uninit(&i_gh);
  965. return error;
  966. }
  967. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  968. struct gfs2_ea_header *ea, char *data)
  969. {
  970. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  971. struct buffer_head **bh;
  972. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  973. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  974. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  975. unsigned int x;
  976. int error;
  977. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  978. if (!bh)
  979. return -ENOMEM;
  980. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  981. if (error)
  982. goto out;
  983. for (x = 0; x < nptrs; x++) {
  984. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
  985. bh + x);
  986. if (error) {
  987. while (x--)
  988. brelse(bh[x]);
  989. goto fail;
  990. }
  991. dataptrs++;
  992. }
  993. for (x = 0; x < nptrs; x++) {
  994. error = gfs2_meta_wait(sdp, bh[x]);
  995. if (error) {
  996. for (; x < nptrs; x++)
  997. brelse(bh[x]);
  998. goto fail;
  999. }
  1000. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  1001. for (; x < nptrs; x++)
  1002. brelse(bh[x]);
  1003. error = -EIO;
  1004. goto fail;
  1005. }
  1006. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1007. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
  1008. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1009. amount -= sdp->sd_jbsize;
  1010. data += sdp->sd_jbsize;
  1011. brelse(bh[x]);
  1012. }
  1013. out:
  1014. kfree(bh);
  1015. return error;
  1016. fail:
  1017. gfs2_trans_end(sdp);
  1018. kfree(bh);
  1019. return error;
  1020. }
  1021. int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  1022. struct iattr *attr, char *data)
  1023. {
  1024. struct buffer_head *dibh;
  1025. int error;
  1026. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  1027. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  1028. if (error)
  1029. return error;
  1030. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  1031. memcpy(GFS2_EA2DATA(el->el_ea), data,
  1032. GFS2_EA_DATA_LEN(el->el_ea));
  1033. } else
  1034. error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
  1035. if (error)
  1036. return error;
  1037. error = gfs2_meta_inode_buffer(ip, &dibh);
  1038. if (!error) {
  1039. error = inode_setattr(&ip->i_inode, attr);
  1040. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1041. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1042. gfs2_dinode_out(ip, dibh->b_data);
  1043. brelse(dibh);
  1044. }
  1045. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1046. return error;
  1047. }
  1048. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1049. {
  1050. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1051. struct gfs2_rgrp_list rlist;
  1052. struct buffer_head *indbh, *dibh;
  1053. __be64 *eablk, *end;
  1054. unsigned int rg_blocks = 0;
  1055. u64 bstart = 0;
  1056. unsigned int blen = 0;
  1057. unsigned int blks = 0;
  1058. unsigned int x;
  1059. int error;
  1060. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1061. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &indbh);
  1062. if (error)
  1063. return error;
  1064. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1065. error = -EIO;
  1066. goto out;
  1067. }
  1068. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1069. end = eablk + sdp->sd_inptrs;
  1070. for (; eablk < end; eablk++) {
  1071. u64 bn;
  1072. if (!*eablk)
  1073. break;
  1074. bn = be64_to_cpu(*eablk);
  1075. if (bstart + blen == bn)
  1076. blen++;
  1077. else {
  1078. if (bstart)
  1079. gfs2_rlist_add(sdp, &rlist, bstart);
  1080. bstart = bn;
  1081. blen = 1;
  1082. }
  1083. blks++;
  1084. }
  1085. if (bstart)
  1086. gfs2_rlist_add(sdp, &rlist, bstart);
  1087. else
  1088. goto out;
  1089. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1090. for (x = 0; x < rlist.rl_rgrps; x++) {
  1091. struct gfs2_rgrpd *rgd;
  1092. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1093. rg_blocks += rgd->rd_length;
  1094. }
  1095. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1096. if (error)
  1097. goto out_rlist_free;
  1098. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
  1099. RES_STATFS + RES_QUOTA, blks);
  1100. if (error)
  1101. goto out_gunlock;
  1102. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  1103. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1104. bstart = 0;
  1105. blen = 0;
  1106. for (; eablk < end; eablk++) {
  1107. u64 bn;
  1108. if (!*eablk)
  1109. break;
  1110. bn = be64_to_cpu(*eablk);
  1111. if (bstart + blen == bn)
  1112. blen++;
  1113. else {
  1114. if (bstart)
  1115. gfs2_free_meta(ip, bstart, blen);
  1116. bstart = bn;
  1117. blen = 1;
  1118. }
  1119. *eablk = 0;
  1120. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1121. }
  1122. if (bstart)
  1123. gfs2_free_meta(ip, bstart, blen);
  1124. ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
  1125. error = gfs2_meta_inode_buffer(ip, &dibh);
  1126. if (!error) {
  1127. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1128. gfs2_dinode_out(ip, dibh->b_data);
  1129. brelse(dibh);
  1130. }
  1131. gfs2_trans_end(sdp);
  1132. out_gunlock:
  1133. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1134. out_rlist_free:
  1135. gfs2_rlist_free(&rlist);
  1136. out:
  1137. brelse(indbh);
  1138. return error;
  1139. }
  1140. static int ea_dealloc_block(struct gfs2_inode *ip)
  1141. {
  1142. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1143. struct gfs2_alloc *al = ip->i_alloc;
  1144. struct gfs2_rgrpd *rgd;
  1145. struct buffer_head *dibh;
  1146. int error;
  1147. rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
  1148. if (!rgd) {
  1149. gfs2_consist_inode(ip);
  1150. return -EIO;
  1151. }
  1152. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  1153. &al->al_rgd_gh);
  1154. if (error)
  1155. return error;
  1156. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
  1157. RES_QUOTA, 1);
  1158. if (error)
  1159. goto out_gunlock;
  1160. gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
  1161. ip->i_di.di_eattr = 0;
  1162. gfs2_add_inode_blocks(&ip->i_inode, -1);
  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. if (!al)
  1186. return -ENOMEM;
  1187. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1188. if (error)
  1189. goto out_alloc;
  1190. error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
  1191. if (error)
  1192. goto out_quota;
  1193. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1194. if (error)
  1195. goto out_rindex;
  1196. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  1197. error = ea_dealloc_indirect(ip);
  1198. if (error)
  1199. goto out_rindex;
  1200. }
  1201. error = ea_dealloc_block(ip);
  1202. out_rindex:
  1203. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1204. out_quota:
  1205. gfs2_quota_unhold(ip);
  1206. out_alloc:
  1207. gfs2_alloc_put(ip);
  1208. return error;
  1209. }