xattr.c 33 KB

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