xattr.c 32 KB

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