xattr.c 32 KB

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