xattr.c 32 KB

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