xattr.c 33 KB

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