xattr.c 33 KB

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