xattr.c 32 KB

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