xattr.c 33 KB

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