xattr.c 33 KB

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