xattr.c 34 KB

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