xattr.c 32 KB

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