xattr.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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, &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, &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;
  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,
  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(const struct xattr_handler *handler,
  489. struct dentry *dentry, const char *name,
  490. void *buffer, size_t size)
  491. {
  492. struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
  493. struct gfs2_ea_location el;
  494. int type = handler->flags;
  495. int error;
  496. if (!ip->i_eattr)
  497. return -ENODATA;
  498. if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
  499. return -EINVAL;
  500. error = gfs2_ea_find(ip, type, name, &el);
  501. if (error)
  502. return error;
  503. if (!el.el_ea)
  504. return -ENODATA;
  505. if (size)
  506. error = gfs2_ea_get_copy(ip, &el, buffer, size);
  507. else
  508. error = GFS2_EA_DATA_LEN(el.el_ea);
  509. brelse(el.el_bh);
  510. return error;
  511. }
  512. /**
  513. * ea_alloc_blk - allocates a new block for extended attributes.
  514. * @ip: A pointer to the inode that's getting extended attributes
  515. * @bhp: Pointer to pointer to a struct buffer_head
  516. *
  517. * Returns: errno
  518. */
  519. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  520. {
  521. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  522. struct gfs2_ea_header *ea;
  523. unsigned int n = 1;
  524. u64 block;
  525. int error;
  526. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  527. if (error)
  528. return error;
  529. gfs2_trans_add_unrevoke(sdp, block, 1);
  530. *bhp = gfs2_meta_new(ip->i_gl, block);
  531. gfs2_trans_add_meta(ip->i_gl, *bhp);
  532. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  533. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  534. ea = GFS2_EA_BH2FIRST(*bhp);
  535. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  536. ea->ea_type = GFS2_EATYPE_UNUSED;
  537. ea->ea_flags = GFS2_EAFLAG_LAST;
  538. ea->ea_num_ptrs = 0;
  539. gfs2_add_inode_blocks(&ip->i_inode, 1);
  540. return 0;
  541. }
  542. /**
  543. * ea_write - writes the request info to an ea, creating new blocks if
  544. * necessary
  545. * @ip: inode that is being modified
  546. * @ea: the location of the new ea in a block
  547. * @er: the write request
  548. *
  549. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  550. *
  551. * returns : errno
  552. */
  553. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  554. struct gfs2_ea_request *er)
  555. {
  556. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  557. int error;
  558. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  559. ea->ea_name_len = er->er_name_len;
  560. ea->ea_type = er->er_type;
  561. ea->__pad = 0;
  562. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  563. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  564. ea->ea_num_ptrs = 0;
  565. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  566. } else {
  567. __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
  568. const char *data = er->er_data;
  569. unsigned int data_len = er->er_data_len;
  570. unsigned int copy;
  571. unsigned int x;
  572. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  573. for (x = 0; x < ea->ea_num_ptrs; x++) {
  574. struct buffer_head *bh;
  575. u64 block;
  576. int mh_size = sizeof(struct gfs2_meta_header);
  577. unsigned int n = 1;
  578. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  579. if (error)
  580. return error;
  581. gfs2_trans_add_unrevoke(sdp, block, 1);
  582. bh = gfs2_meta_new(ip->i_gl, block);
  583. gfs2_trans_add_meta(ip->i_gl, bh);
  584. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  585. gfs2_add_inode_blocks(&ip->i_inode, 1);
  586. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  587. data_len;
  588. memcpy(bh->b_data + mh_size, data, copy);
  589. if (copy < sdp->sd_jbsize)
  590. memset(bh->b_data + mh_size + copy, 0,
  591. sdp->sd_jbsize - copy);
  592. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  593. data += copy;
  594. data_len -= copy;
  595. brelse(bh);
  596. }
  597. gfs2_assert_withdraw(sdp, !data_len);
  598. }
  599. return 0;
  600. }
  601. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  602. struct gfs2_ea_request *er, void *private);
  603. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  604. unsigned int blks,
  605. ea_skeleton_call_t skeleton_call, void *private)
  606. {
  607. struct gfs2_alloc_parms ap = { .target = blks };
  608. struct buffer_head *dibh;
  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. error = gfs2_meta_inode_buffer(ip, &dibh);
  628. if (!error) {
  629. ip->i_inode.i_ctime = CURRENT_TIME;
  630. gfs2_trans_add_meta(ip->i_gl, dibh);
  631. gfs2_dinode_out(ip, dibh->b_data);
  632. brelse(dibh);
  633. }
  634. out_end_trans:
  635. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  636. out_ipres:
  637. gfs2_inplace_release(ip);
  638. out_gunlock_q:
  639. gfs2_quota_unlock(ip);
  640. return error;
  641. }
  642. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  643. void *private)
  644. {
  645. struct buffer_head *bh;
  646. int error;
  647. error = ea_alloc_blk(ip, &bh);
  648. if (error)
  649. return error;
  650. ip->i_eattr = bh->b_blocknr;
  651. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  652. brelse(bh);
  653. return error;
  654. }
  655. /**
  656. * ea_init - initializes a new eattr block
  657. * @ip:
  658. * @er:
  659. *
  660. * Returns: errno
  661. */
  662. static int ea_init(struct gfs2_inode *ip, int type, const char *name,
  663. const void *data, size_t size)
  664. {
  665. struct gfs2_ea_request er;
  666. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  667. unsigned int blks = 1;
  668. er.er_type = type;
  669. er.er_name = name;
  670. er.er_name_len = strlen(name);
  671. er.er_data = (void *)data;
  672. er.er_data_len = size;
  673. if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
  674. blks += DIV_ROUND_UP(er.er_data_len, jbsize);
  675. return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
  676. }
  677. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  678. {
  679. u32 ea_size = GFS2_EA_SIZE(ea);
  680. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  681. ea_size);
  682. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  683. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  684. ea->ea_rec_len = cpu_to_be32(ea_size);
  685. ea->ea_flags ^= last;
  686. new->ea_rec_len = cpu_to_be32(new_size);
  687. new->ea_flags = last;
  688. return new;
  689. }
  690. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  691. struct gfs2_ea_location *el)
  692. {
  693. struct gfs2_ea_header *ea = el->el_ea;
  694. struct gfs2_ea_header *prev = el->el_prev;
  695. u32 len;
  696. gfs2_trans_add_meta(ip->i_gl, el->el_bh);
  697. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  698. ea->ea_type = GFS2_EATYPE_UNUSED;
  699. return;
  700. } else if (GFS2_EA2NEXT(prev) != ea) {
  701. prev = GFS2_EA2NEXT(prev);
  702. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  703. }
  704. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  705. prev->ea_rec_len = cpu_to_be32(len);
  706. if (GFS2_EA_IS_LAST(ea))
  707. prev->ea_flags |= GFS2_EAFLAG_LAST;
  708. }
  709. struct ea_set {
  710. int ea_split;
  711. struct gfs2_ea_request *es_er;
  712. struct gfs2_ea_location *es_el;
  713. struct buffer_head *es_bh;
  714. struct gfs2_ea_header *es_ea;
  715. };
  716. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  717. struct gfs2_ea_header *ea, struct ea_set *es)
  718. {
  719. struct gfs2_ea_request *er = es->es_er;
  720. struct buffer_head *dibh;
  721. int error;
  722. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  723. if (error)
  724. return error;
  725. gfs2_trans_add_meta(ip->i_gl, bh);
  726. if (es->ea_split)
  727. ea = ea_split_ea(ea);
  728. ea_write(ip, ea, er);
  729. if (es->es_el)
  730. ea_set_remove_stuffed(ip, es->es_el);
  731. error = gfs2_meta_inode_buffer(ip, &dibh);
  732. if (error)
  733. goto out;
  734. ip->i_inode.i_ctime = CURRENT_TIME;
  735. gfs2_trans_add_meta(ip->i_gl, dibh);
  736. gfs2_dinode_out(ip, dibh->b_data);
  737. brelse(dibh);
  738. out:
  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,
  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, NULL);
  830. if (error)
  831. return error;
  832. gfs2_trans_add_unrevoke(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. struct buffer_head *dibh;
  899. int error;
  900. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  901. if (error)
  902. return error;
  903. gfs2_trans_add_meta(ip->i_gl, el->el_bh);
  904. if (prev) {
  905. u32 len;
  906. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  907. prev->ea_rec_len = cpu_to_be32(len);
  908. if (GFS2_EA_IS_LAST(ea))
  909. prev->ea_flags |= GFS2_EAFLAG_LAST;
  910. } else {
  911. ea->ea_type = GFS2_EATYPE_UNUSED;
  912. }
  913. error = gfs2_meta_inode_buffer(ip, &dibh);
  914. if (!error) {
  915. ip->i_inode.i_ctime = CURRENT_TIME;
  916. gfs2_trans_add_meta(ip->i_gl, dibh);
  917. gfs2_dinode_out(ip, dibh->b_data);
  918. brelse(dibh);
  919. }
  920. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  921. return error;
  922. }
  923. /**
  924. * gfs2_xattr_remove - Remove a GFS2 extended attribute
  925. * @ip: The inode
  926. * @type: The type of the extended attribute
  927. * @name: The name of the extended attribute
  928. *
  929. * This is not called directly by the VFS since we use the (common)
  930. * scheme of making a "set with NULL data" mean a remove request. Note
  931. * that this is different from a set with zero length data.
  932. *
  933. * Returns: 0, or errno on failure
  934. */
  935. static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
  936. {
  937. struct gfs2_ea_location el;
  938. int error;
  939. if (!ip->i_eattr)
  940. return -ENODATA;
  941. error = gfs2_ea_find(ip, type, name, &el);
  942. if (error)
  943. return error;
  944. if (!el.el_ea)
  945. return -ENODATA;
  946. if (GFS2_EA_IS_STUFFED(el.el_ea))
  947. error = ea_remove_stuffed(ip, &el);
  948. else
  949. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
  950. brelse(el.el_bh);
  951. return error;
  952. }
  953. /**
  954. * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
  955. * @ip: The inode
  956. * @name: The name of the extended attribute
  957. * @value: The value of the extended attribute (NULL for remove)
  958. * @size: The size of the @value argument
  959. * @flags: Create or Replace
  960. * @type: The type of the extended attribute
  961. *
  962. * See gfs2_xattr_remove() for details of the removal of xattrs.
  963. *
  964. * Returns: 0 or errno on failure
  965. */
  966. int __gfs2_xattr_set(struct inode *inode, const char *name,
  967. const void *value, size_t size, int flags, int type)
  968. {
  969. struct gfs2_inode *ip = GFS2_I(inode);
  970. struct gfs2_sbd *sdp = GFS2_SB(inode);
  971. struct gfs2_ea_location el;
  972. unsigned int namel = strlen(name);
  973. int error;
  974. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  975. return -EPERM;
  976. if (namel > GFS2_EA_MAX_NAME_LEN)
  977. return -ERANGE;
  978. if (value == NULL)
  979. return gfs2_xattr_remove(ip, type, name);
  980. if (ea_check_size(sdp, namel, size))
  981. return -ERANGE;
  982. if (!ip->i_eattr) {
  983. if (flags & XATTR_REPLACE)
  984. return -ENODATA;
  985. return ea_init(ip, type, name, value, size);
  986. }
  987. error = gfs2_ea_find(ip, type, name, &el);
  988. if (error)
  989. return error;
  990. if (el.el_ea) {
  991. if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
  992. brelse(el.el_bh);
  993. return -EPERM;
  994. }
  995. error = -EEXIST;
  996. if (!(flags & XATTR_CREATE)) {
  997. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  998. error = ea_set_i(ip, type, name, value, size, &el);
  999. if (!error && unstuffed)
  1000. ea_set_remove_unstuffed(ip, &el);
  1001. }
  1002. brelse(el.el_bh);
  1003. return error;
  1004. }
  1005. error = -ENODATA;
  1006. if (!(flags & XATTR_REPLACE))
  1007. error = ea_set_i(ip, type, name, value, size, NULL);
  1008. return error;
  1009. }
  1010. static int gfs2_xattr_set(const struct xattr_handler *handler,
  1011. struct dentry *dentry, const char *name,
  1012. const void *value, size_t size, int flags)
  1013. {
  1014. return __gfs2_xattr_set(d_inode(dentry), name, value,
  1015. size, flags, handler->flags);
  1016. }
  1017. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  1018. struct gfs2_ea_header *ea, char *data)
  1019. {
  1020. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1021. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  1022. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  1023. int ret;
  1024. ret = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  1025. if (ret)
  1026. return ret;
  1027. ret = gfs2_iter_unstuffed(ip, ea, data, NULL);
  1028. gfs2_trans_end(sdp);
  1029. return ret;
  1030. }
  1031. int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
  1032. {
  1033. struct inode *inode = &ip->i_inode;
  1034. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1035. struct gfs2_ea_location el;
  1036. int error;
  1037. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
  1038. if (error)
  1039. return error;
  1040. if (GFS2_EA_IS_STUFFED(el.el_ea)) {
  1041. error = gfs2_trans_begin(sdp, RES_DINODE + RES_EATTR, 0);
  1042. if (error == 0) {
  1043. gfs2_trans_add_meta(ip->i_gl, el.el_bh);
  1044. memcpy(GFS2_EA2DATA(el.el_ea), data,
  1045. GFS2_EA_DATA_LEN(el.el_ea));
  1046. }
  1047. } else {
  1048. error = ea_acl_chmod_unstuffed(ip, el.el_ea, data);
  1049. }
  1050. brelse(el.el_bh);
  1051. if (error)
  1052. return error;
  1053. error = gfs2_setattr_simple(inode, attr);
  1054. gfs2_trans_end(sdp);
  1055. return error;
  1056. }
  1057. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1058. {
  1059. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1060. struct gfs2_rgrp_list rlist;
  1061. struct buffer_head *indbh, *dibh;
  1062. __be64 *eablk, *end;
  1063. unsigned int rg_blocks = 0;
  1064. u64 bstart = 0;
  1065. unsigned int blen = 0;
  1066. unsigned int blks = 0;
  1067. unsigned int x;
  1068. int error;
  1069. error = gfs2_rindex_update(sdp);
  1070. if (error)
  1071. return error;
  1072. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1073. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh);
  1074. if (error)
  1075. return error;
  1076. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1077. error = -EIO;
  1078. goto out;
  1079. }
  1080. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1081. end = eablk + sdp->sd_inptrs;
  1082. for (; eablk < end; eablk++) {
  1083. u64 bn;
  1084. if (!*eablk)
  1085. break;
  1086. bn = be64_to_cpu(*eablk);
  1087. if (bstart + blen == bn)
  1088. blen++;
  1089. else {
  1090. if (bstart)
  1091. gfs2_rlist_add(ip, &rlist, bstart);
  1092. bstart = bn;
  1093. blen = 1;
  1094. }
  1095. blks++;
  1096. }
  1097. if (bstart)
  1098. gfs2_rlist_add(ip, &rlist, bstart);
  1099. else
  1100. goto out;
  1101. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1102. for (x = 0; x < rlist.rl_rgrps; x++) {
  1103. struct gfs2_rgrpd *rgd;
  1104. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1105. rg_blocks += rgd->rd_length;
  1106. }
  1107. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1108. if (error)
  1109. goto out_rlist_free;
  1110. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
  1111. RES_STATFS + RES_QUOTA, blks);
  1112. if (error)
  1113. goto out_gunlock;
  1114. gfs2_trans_add_meta(ip->i_gl, indbh);
  1115. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1116. bstart = 0;
  1117. blen = 0;
  1118. for (; eablk < end; eablk++) {
  1119. u64 bn;
  1120. if (!*eablk)
  1121. break;
  1122. bn = be64_to_cpu(*eablk);
  1123. if (bstart + blen == bn)
  1124. blen++;
  1125. else {
  1126. if (bstart)
  1127. gfs2_free_meta(ip, bstart, blen);
  1128. bstart = bn;
  1129. blen = 1;
  1130. }
  1131. *eablk = 0;
  1132. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1133. }
  1134. if (bstart)
  1135. gfs2_free_meta(ip, bstart, blen);
  1136. ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
  1137. error = gfs2_meta_inode_buffer(ip, &dibh);
  1138. if (!error) {
  1139. gfs2_trans_add_meta(ip->i_gl, dibh);
  1140. gfs2_dinode_out(ip, dibh->b_data);
  1141. brelse(dibh);
  1142. }
  1143. gfs2_trans_end(sdp);
  1144. out_gunlock:
  1145. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1146. out_rlist_free:
  1147. gfs2_rlist_free(&rlist);
  1148. out:
  1149. brelse(indbh);
  1150. return error;
  1151. }
  1152. static int ea_dealloc_block(struct gfs2_inode *ip)
  1153. {
  1154. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1155. struct gfs2_rgrpd *rgd;
  1156. struct buffer_head *dibh;
  1157. struct gfs2_holder gh;
  1158. int error;
  1159. error = gfs2_rindex_update(sdp);
  1160. if (error)
  1161. return error;
  1162. rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
  1163. if (!rgd) {
  1164. gfs2_consist_inode(ip);
  1165. return -EIO;
  1166. }
  1167. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1168. if (error)
  1169. return error;
  1170. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
  1171. RES_QUOTA, 1);
  1172. if (error)
  1173. goto out_gunlock;
  1174. gfs2_free_meta(ip, ip->i_eattr, 1);
  1175. ip->i_eattr = 0;
  1176. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1177. error = gfs2_meta_inode_buffer(ip, &dibh);
  1178. if (!error) {
  1179. gfs2_trans_add_meta(ip->i_gl, dibh);
  1180. gfs2_dinode_out(ip, dibh->b_data);
  1181. brelse(dibh);
  1182. }
  1183. gfs2_trans_end(sdp);
  1184. out_gunlock:
  1185. gfs2_glock_dq_uninit(&gh);
  1186. return error;
  1187. }
  1188. /**
  1189. * gfs2_ea_dealloc - deallocate the extended attribute fork
  1190. * @ip: the inode
  1191. *
  1192. * Returns: errno
  1193. */
  1194. int gfs2_ea_dealloc(struct gfs2_inode *ip)
  1195. {
  1196. int error;
  1197. error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
  1198. if (error)
  1199. return error;
  1200. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1201. if (error)
  1202. return error;
  1203. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1204. if (error)
  1205. goto out_quota;
  1206. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  1207. error = ea_dealloc_indirect(ip);
  1208. if (error)
  1209. goto out_quota;
  1210. }
  1211. error = ea_dealloc_block(ip);
  1212. out_quota:
  1213. gfs2_quota_unhold(ip);
  1214. return error;
  1215. }
  1216. static const struct xattr_handler gfs2_xattr_user_handler = {
  1217. .prefix = XATTR_USER_PREFIX,
  1218. .flags = GFS2_EATYPE_USR,
  1219. .get = gfs2_xattr_get,
  1220. .set = gfs2_xattr_set,
  1221. };
  1222. static const struct xattr_handler gfs2_xattr_security_handler = {
  1223. .prefix = XATTR_SECURITY_PREFIX,
  1224. .flags = GFS2_EATYPE_SECURITY,
  1225. .get = gfs2_xattr_get,
  1226. .set = gfs2_xattr_set,
  1227. };
  1228. const struct xattr_handler *gfs2_xattr_handlers[] = {
  1229. &gfs2_xattr_user_handler,
  1230. &gfs2_xattr_security_handler,
  1231. &posix_acl_access_xattr_handler,
  1232. &posix_acl_default_xattr_handler,
  1233. NULL,
  1234. };