xattr.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
  276. gfs2_quota_unhold(ip);
  277. out_alloc:
  278. gfs2_alloc_put(ip);
  279. return error;
  280. }
  281. struct ea_list {
  282. struct gfs2_ea_request *ei_er;
  283. unsigned int ei_size;
  284. };
  285. static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea)
  286. {
  287. switch (ea->ea_type) {
  288. case GFS2_EATYPE_USR:
  289. return 5 + ea->ea_name_len + 1;
  290. case GFS2_EATYPE_SYS:
  291. return 7 + ea->ea_name_len + 1;
  292. case GFS2_EATYPE_SECURITY:
  293. return 9 + ea->ea_name_len + 1;
  294. default:
  295. return 0;
  296. }
  297. }
  298. static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
  299. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  300. void *private)
  301. {
  302. struct ea_list *ei = private;
  303. struct gfs2_ea_request *er = ei->ei_er;
  304. unsigned int ea_size = gfs2_ea_strlen(ea);
  305. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  306. return 0;
  307. if (er->er_data_len) {
  308. char *prefix = NULL;
  309. unsigned int l = 0;
  310. char c = 0;
  311. if (ei->ei_size + ea_size > er->er_data_len)
  312. return -ERANGE;
  313. switch (ea->ea_type) {
  314. case GFS2_EATYPE_USR:
  315. prefix = "user.";
  316. l = 5;
  317. break;
  318. case GFS2_EATYPE_SYS:
  319. prefix = "system.";
  320. l = 7;
  321. break;
  322. case GFS2_EATYPE_SECURITY:
  323. prefix = "security.";
  324. l = 9;
  325. break;
  326. }
  327. BUG_ON(l == 0);
  328. memcpy(er->er_data + ei->ei_size, prefix, l);
  329. memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
  330. ea->ea_name_len);
  331. memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
  332. }
  333. ei->ei_size += ea_size;
  334. return 0;
  335. }
  336. /**
  337. * gfs2_listxattr - List gfs2 extended attributes
  338. * @dentry: The dentry whose inode we are interested in
  339. * @buffer: The buffer to write the results
  340. * @size: The size of the buffer
  341. *
  342. * Returns: actual size of data on success, -errno on error
  343. */
  344. ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  345. {
  346. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  347. struct gfs2_ea_request er;
  348. struct gfs2_holder i_gh;
  349. int error;
  350. memset(&er, 0, sizeof(struct gfs2_ea_request));
  351. if (size) {
  352. er.er_data = buffer;
  353. er.er_data_len = size;
  354. }
  355. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  356. if (error)
  357. return error;
  358. if (ip->i_eattr) {
  359. struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
  360. error = ea_foreach(ip, ea_list_i, &ei);
  361. if (!error)
  362. error = ei.ei_size;
  363. }
  364. gfs2_glock_dq_uninit(&i_gh);
  365. return error;
  366. }
  367. /**
  368. * ea_get_unstuffed - actually copies the unstuffed data into the
  369. * request buffer
  370. * @ip: The GFS2 inode
  371. * @ea: The extended attribute header structure
  372. * @data: The data to be copied
  373. *
  374. * Returns: errno
  375. */
  376. static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  377. char *data)
  378. {
  379. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  380. struct buffer_head **bh;
  381. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  382. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  383. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  384. unsigned int x;
  385. int error = 0;
  386. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  387. if (!bh)
  388. return -ENOMEM;
  389. for (x = 0; x < nptrs; x++) {
  390. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
  391. bh + x);
  392. if (error) {
  393. while (x--)
  394. brelse(bh[x]);
  395. goto out;
  396. }
  397. dataptrs++;
  398. }
  399. for (x = 0; x < nptrs; x++) {
  400. error = gfs2_meta_wait(sdp, bh[x]);
  401. if (error) {
  402. for (; x < nptrs; x++)
  403. brelse(bh[x]);
  404. goto out;
  405. }
  406. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  407. for (; x < nptrs; x++)
  408. brelse(bh[x]);
  409. error = -EIO;
  410. goto out;
  411. }
  412. memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
  413. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  414. amount -= sdp->sd_jbsize;
  415. data += sdp->sd_jbsize;
  416. brelse(bh[x]);
  417. }
  418. out:
  419. kfree(bh);
  420. return error;
  421. }
  422. static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  423. char *data, size_t size)
  424. {
  425. int ret;
  426. size_t len = GFS2_EA_DATA_LEN(el->el_ea);
  427. if (len > size)
  428. return -ERANGE;
  429. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  430. memcpy(data, GFS2_EA2DATA(el->el_ea), len);
  431. return len;
  432. }
  433. ret = ea_get_unstuffed(ip, el->el_ea, data);
  434. if (ret < 0)
  435. return ret;
  436. return len;
  437. }
  438. int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
  439. {
  440. struct gfs2_ea_location el;
  441. int error;
  442. int len;
  443. char *data;
  444. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
  445. if (error)
  446. return error;
  447. if (!el.el_ea)
  448. goto out;
  449. if (!GFS2_EA_DATA_LEN(el.el_ea))
  450. goto out;
  451. len = GFS2_EA_DATA_LEN(el.el_ea);
  452. data = kmalloc(len, GFP_NOFS);
  453. error = -ENOMEM;
  454. if (data == NULL)
  455. goto out;
  456. error = gfs2_ea_get_copy(ip, &el, data, len);
  457. if (error < 0)
  458. kfree(data);
  459. else
  460. *ppdata = data;
  461. out:
  462. brelse(el.el_bh);
  463. return error;
  464. }
  465. /**
  466. * gfs2_xattr_get - Get a GFS2 extended attribute
  467. * @inode: The inode
  468. * @name: The name of the extended attribute
  469. * @buffer: The buffer to write the result into
  470. * @size: The size of the buffer
  471. * @type: The type of extended attribute
  472. *
  473. * Returns: actual size of data on success, -errno on error
  474. */
  475. static int gfs2_xattr_get(struct dentry *dentry, const char *name,
  476. void *buffer, size_t size, int type)
  477. {
  478. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  479. struct gfs2_ea_location el;
  480. int error;
  481. if (!ip->i_eattr)
  482. return -ENODATA;
  483. if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
  484. return -EINVAL;
  485. error = gfs2_ea_find(ip, type, name, &el);
  486. if (error)
  487. return error;
  488. if (!el.el_ea)
  489. return -ENODATA;
  490. if (size)
  491. error = gfs2_ea_get_copy(ip, &el, buffer, size);
  492. else
  493. error = GFS2_EA_DATA_LEN(el.el_ea);
  494. brelse(el.el_bh);
  495. return error;
  496. }
  497. /**
  498. * ea_alloc_blk - allocates a new block for extended attributes.
  499. * @ip: A pointer to the inode that's getting extended attributes
  500. * @bhp: Pointer to pointer to a struct buffer_head
  501. *
  502. * Returns: errno
  503. */
  504. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  505. {
  506. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  507. struct gfs2_ea_header *ea;
  508. unsigned int n = 1;
  509. u64 block;
  510. int error;
  511. error = gfs2_alloc_block(ip, &block, &n);
  512. if (error)
  513. return error;
  514. gfs2_trans_add_unrevoke(sdp, block, 1);
  515. *bhp = gfs2_meta_new(ip->i_gl, block);
  516. gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
  517. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  518. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  519. ea = GFS2_EA_BH2FIRST(*bhp);
  520. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  521. ea->ea_type = GFS2_EATYPE_UNUSED;
  522. ea->ea_flags = GFS2_EAFLAG_LAST;
  523. ea->ea_num_ptrs = 0;
  524. gfs2_add_inode_blocks(&ip->i_inode, 1);
  525. return 0;
  526. }
  527. /**
  528. * ea_write - writes the request info to an ea, creating new blocks if
  529. * necessary
  530. * @ip: inode that is being modified
  531. * @ea: the location of the new ea in a block
  532. * @er: the write request
  533. *
  534. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  535. *
  536. * returns : errno
  537. */
  538. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  539. struct gfs2_ea_request *er)
  540. {
  541. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  542. int error;
  543. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  544. ea->ea_name_len = er->er_name_len;
  545. ea->ea_type = er->er_type;
  546. ea->__pad = 0;
  547. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  548. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  549. ea->ea_num_ptrs = 0;
  550. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  551. } else {
  552. __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
  553. const char *data = er->er_data;
  554. unsigned int data_len = er->er_data_len;
  555. unsigned int copy;
  556. unsigned int x;
  557. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  558. for (x = 0; x < ea->ea_num_ptrs; x++) {
  559. struct buffer_head *bh;
  560. u64 block;
  561. int mh_size = sizeof(struct gfs2_meta_header);
  562. unsigned int n = 1;
  563. error = gfs2_alloc_block(ip, &block, &n);
  564. if (error)
  565. return error;
  566. gfs2_trans_add_unrevoke(sdp, block, 1);
  567. bh = gfs2_meta_new(ip->i_gl, block);
  568. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  569. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  570. gfs2_add_inode_blocks(&ip->i_inode, 1);
  571. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  572. data_len;
  573. memcpy(bh->b_data + mh_size, data, copy);
  574. if (copy < sdp->sd_jbsize)
  575. memset(bh->b_data + mh_size + copy, 0,
  576. sdp->sd_jbsize - copy);
  577. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  578. data += copy;
  579. data_len -= copy;
  580. brelse(bh);
  581. }
  582. gfs2_assert_withdraw(sdp, !data_len);
  583. }
  584. return 0;
  585. }
  586. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  587. struct gfs2_ea_request *er, void *private);
  588. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  589. unsigned int blks,
  590. ea_skeleton_call_t skeleton_call, void *private)
  591. {
  592. struct gfs2_alloc *al;
  593. struct buffer_head *dibh;
  594. int error;
  595. al = gfs2_alloc_get(ip);
  596. if (!al)
  597. return -ENOMEM;
  598. error = gfs2_quota_lock_check(ip);
  599. if (error)
  600. goto out;
  601. al->al_requested = blks;
  602. error = gfs2_inplace_reserve(ip);
  603. if (error)
  604. goto out_gunlock_q;
  605. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  606. blks + gfs2_rg_blocks(ip) +
  607. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  608. if (error)
  609. goto out_ipres;
  610. error = skeleton_call(ip, er, private);
  611. if (error)
  612. goto out_end_trans;
  613. error = gfs2_meta_inode_buffer(ip, &dibh);
  614. if (!error) {
  615. ip->i_inode.i_ctime = CURRENT_TIME;
  616. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  617. gfs2_dinode_out(ip, dibh->b_data);
  618. brelse(dibh);
  619. }
  620. out_end_trans:
  621. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  622. out_ipres:
  623. gfs2_inplace_release(ip);
  624. out_gunlock_q:
  625. gfs2_quota_unlock(ip);
  626. out:
  627. gfs2_alloc_put(ip);
  628. return error;
  629. }
  630. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  631. void *private)
  632. {
  633. struct buffer_head *bh;
  634. int error;
  635. error = ea_alloc_blk(ip, &bh);
  636. if (error)
  637. return error;
  638. ip->i_eattr = bh->b_blocknr;
  639. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  640. brelse(bh);
  641. return error;
  642. }
  643. /**
  644. * ea_init - initializes a new eattr block
  645. * @ip:
  646. * @er:
  647. *
  648. * Returns: errno
  649. */
  650. static int ea_init(struct gfs2_inode *ip, int type, const char *name,
  651. const void *data, size_t size)
  652. {
  653. struct gfs2_ea_request er;
  654. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  655. unsigned int blks = 1;
  656. er.er_type = type;
  657. er.er_name = name;
  658. er.er_name_len = strlen(name);
  659. er.er_data = (void *)data;
  660. er.er_data_len = size;
  661. if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
  662. blks += DIV_ROUND_UP(er.er_data_len, jbsize);
  663. return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
  664. }
  665. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  666. {
  667. u32 ea_size = GFS2_EA_SIZE(ea);
  668. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  669. ea_size);
  670. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  671. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  672. ea->ea_rec_len = cpu_to_be32(ea_size);
  673. ea->ea_flags ^= last;
  674. new->ea_rec_len = cpu_to_be32(new_size);
  675. new->ea_flags = last;
  676. return new;
  677. }
  678. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  679. struct gfs2_ea_location *el)
  680. {
  681. struct gfs2_ea_header *ea = el->el_ea;
  682. struct gfs2_ea_header *prev = el->el_prev;
  683. u32 len;
  684. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  685. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  686. ea->ea_type = GFS2_EATYPE_UNUSED;
  687. return;
  688. } else if (GFS2_EA2NEXT(prev) != ea) {
  689. prev = GFS2_EA2NEXT(prev);
  690. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  691. }
  692. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  693. prev->ea_rec_len = cpu_to_be32(len);
  694. if (GFS2_EA_IS_LAST(ea))
  695. prev->ea_flags |= GFS2_EAFLAG_LAST;
  696. }
  697. struct ea_set {
  698. int ea_split;
  699. struct gfs2_ea_request *es_er;
  700. struct gfs2_ea_location *es_el;
  701. struct buffer_head *es_bh;
  702. struct gfs2_ea_header *es_ea;
  703. };
  704. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  705. struct gfs2_ea_header *ea, struct ea_set *es)
  706. {
  707. struct gfs2_ea_request *er = es->es_er;
  708. struct buffer_head *dibh;
  709. int error;
  710. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  711. if (error)
  712. return error;
  713. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  714. if (es->ea_split)
  715. ea = ea_split_ea(ea);
  716. ea_write(ip, ea, er);
  717. if (es->es_el)
  718. ea_set_remove_stuffed(ip, es->es_el);
  719. error = gfs2_meta_inode_buffer(ip, &dibh);
  720. if (error)
  721. goto out;
  722. ip->i_inode.i_ctime = CURRENT_TIME;
  723. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  724. gfs2_dinode_out(ip, dibh->b_data);
  725. brelse(dibh);
  726. out:
  727. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  728. return error;
  729. }
  730. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  731. struct gfs2_ea_request *er, void *private)
  732. {
  733. struct ea_set *es = private;
  734. struct gfs2_ea_header *ea = es->es_ea;
  735. int error;
  736. gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
  737. if (es->ea_split)
  738. ea = ea_split_ea(ea);
  739. error = ea_write(ip, ea, er);
  740. if (error)
  741. return error;
  742. if (es->es_el)
  743. ea_set_remove_stuffed(ip, es->es_el);
  744. return 0;
  745. }
  746. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  747. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  748. void *private)
  749. {
  750. struct ea_set *es = private;
  751. unsigned int size;
  752. int stuffed;
  753. int error;
  754. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
  755. es->es_er->er_data_len, &size);
  756. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  757. if (GFS2_EA_REC_LEN(ea) < size)
  758. return 0;
  759. if (!GFS2_EA_IS_STUFFED(ea)) {
  760. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  761. if (error)
  762. return error;
  763. }
  764. es->ea_split = 0;
  765. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  766. es->ea_split = 1;
  767. else
  768. return 0;
  769. if (stuffed) {
  770. error = ea_set_simple_noalloc(ip, bh, ea, es);
  771. if (error)
  772. return error;
  773. } else {
  774. unsigned int blks;
  775. es->es_bh = bh;
  776. es->es_ea = ea;
  777. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  778. GFS2_SB(&ip->i_inode)->sd_jbsize);
  779. error = ea_alloc_skeleton(ip, es->es_er, blks,
  780. ea_set_simple_alloc, es);
  781. if (error)
  782. return error;
  783. }
  784. return 1;
  785. }
  786. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  787. void *private)
  788. {
  789. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  790. struct buffer_head *indbh, *newbh;
  791. __be64 *eablk;
  792. int error;
  793. int mh_size = sizeof(struct gfs2_meta_header);
  794. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  795. __be64 *end;
  796. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT,
  797. &indbh);
  798. if (error)
  799. return error;
  800. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  801. error = -EIO;
  802. goto out;
  803. }
  804. eablk = (__be64 *)(indbh->b_data + mh_size);
  805. end = eablk + sdp->sd_inptrs;
  806. for (; eablk < end; eablk++)
  807. if (!*eablk)
  808. break;
  809. if (eablk == end) {
  810. error = -ENOSPC;
  811. goto out;
  812. }
  813. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  814. } else {
  815. u64 blk;
  816. unsigned int n = 1;
  817. error = gfs2_alloc_block(ip, &blk, &n);
  818. if (error)
  819. return error;
  820. gfs2_trans_add_unrevoke(sdp, blk, 1);
  821. indbh = gfs2_meta_new(ip->i_gl, blk);
  822. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  823. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  824. gfs2_buffer_clear_tail(indbh, mh_size);
  825. eablk = (__be64 *)(indbh->b_data + mh_size);
  826. *eablk = cpu_to_be64(ip->i_eattr);
  827. ip->i_eattr = blk;
  828. ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
  829. gfs2_add_inode_blocks(&ip->i_inode, 1);
  830. eablk++;
  831. }
  832. error = ea_alloc_blk(ip, &newbh);
  833. if (error)
  834. goto out;
  835. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  836. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  837. brelse(newbh);
  838. if (error)
  839. goto out;
  840. if (private)
  841. ea_set_remove_stuffed(ip, private);
  842. out:
  843. brelse(indbh);
  844. return error;
  845. }
  846. static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
  847. const void *value, size_t size, struct gfs2_ea_location *el)
  848. {
  849. struct gfs2_ea_request er;
  850. struct ea_set es;
  851. unsigned int blks = 2;
  852. int error;
  853. er.er_type = type;
  854. er.er_name = name;
  855. er.er_data = (void *)value;
  856. er.er_name_len = strlen(name);
  857. er.er_data_len = size;
  858. memset(&es, 0, sizeof(struct ea_set));
  859. es.es_er = &er;
  860. es.es_el = el;
  861. error = ea_foreach(ip, ea_set_simple, &es);
  862. if (error > 0)
  863. return 0;
  864. if (error)
  865. return error;
  866. if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
  867. blks++;
  868. if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  869. blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  870. return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
  871. }
  872. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  873. struct gfs2_ea_location *el)
  874. {
  875. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  876. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  877. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  878. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  879. }
  880. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
  881. }
  882. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  883. {
  884. struct gfs2_ea_header *ea = el->el_ea;
  885. struct gfs2_ea_header *prev = el->el_prev;
  886. struct buffer_head *dibh;
  887. int error;
  888. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  889. if (error)
  890. return error;
  891. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  892. if (prev) {
  893. u32 len;
  894. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  895. prev->ea_rec_len = cpu_to_be32(len);
  896. if (GFS2_EA_IS_LAST(ea))
  897. prev->ea_flags |= GFS2_EAFLAG_LAST;
  898. } else {
  899. ea->ea_type = GFS2_EATYPE_UNUSED;
  900. }
  901. error = gfs2_meta_inode_buffer(ip, &dibh);
  902. if (!error) {
  903. ip->i_inode.i_ctime = CURRENT_TIME;
  904. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  905. gfs2_dinode_out(ip, dibh->b_data);
  906. brelse(dibh);
  907. }
  908. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  909. return error;
  910. }
  911. /**
  912. * gfs2_xattr_remove - Remove a GFS2 extended attribute
  913. * @ip: The inode
  914. * @type: The type of the extended attribute
  915. * @name: The name of the extended attribute
  916. *
  917. * This is not called directly by the VFS since we use the (common)
  918. * scheme of making a "set with NULL data" mean a remove request. Note
  919. * that this is different from a set with zero length data.
  920. *
  921. * Returns: 0, or errno on failure
  922. */
  923. static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
  924. {
  925. struct gfs2_ea_location el;
  926. int error;
  927. if (!ip->i_eattr)
  928. return -ENODATA;
  929. error = gfs2_ea_find(ip, type, name, &el);
  930. if (error)
  931. return error;
  932. if (!el.el_ea)
  933. return -ENODATA;
  934. if (GFS2_EA_IS_STUFFED(el.el_ea))
  935. error = ea_remove_stuffed(ip, &el);
  936. else
  937. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
  938. brelse(el.el_bh);
  939. return error;
  940. }
  941. /**
  942. * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
  943. * @ip: The inode
  944. * @name: The name of the extended attribute
  945. * @value: The value of the extended attribute (NULL for remove)
  946. * @size: The size of the @value argument
  947. * @flags: Create or Replace
  948. * @type: The type of the extended attribute
  949. *
  950. * See gfs2_xattr_remove() for details of the removal of xattrs.
  951. *
  952. * Returns: 0 or errno on failure
  953. */
  954. int __gfs2_xattr_set(struct inode *inode, const char *name,
  955. const void *value, size_t size, int flags, int type)
  956. {
  957. struct gfs2_inode *ip = GFS2_I(inode);
  958. struct gfs2_sbd *sdp = GFS2_SB(inode);
  959. struct gfs2_ea_location el;
  960. unsigned int namel = strlen(name);
  961. int error;
  962. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  963. return -EPERM;
  964. if (namel > GFS2_EA_MAX_NAME_LEN)
  965. return -ERANGE;
  966. if (value == NULL)
  967. return gfs2_xattr_remove(ip, type, name);
  968. if (ea_check_size(sdp, namel, size))
  969. return -ERANGE;
  970. if (!ip->i_eattr) {
  971. if (flags & XATTR_REPLACE)
  972. return -ENODATA;
  973. return ea_init(ip, type, name, value, size);
  974. }
  975. error = gfs2_ea_find(ip, type, name, &el);
  976. if (error)
  977. return error;
  978. if (el.el_ea) {
  979. if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
  980. brelse(el.el_bh);
  981. return -EPERM;
  982. }
  983. error = -EEXIST;
  984. if (!(flags & XATTR_CREATE)) {
  985. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  986. error = ea_set_i(ip, type, name, value, size, &el);
  987. if (!error && unstuffed)
  988. ea_set_remove_unstuffed(ip, &el);
  989. }
  990. brelse(el.el_bh);
  991. return error;
  992. }
  993. error = -ENODATA;
  994. if (!(flags & XATTR_REPLACE))
  995. error = ea_set_i(ip, type, name, value, size, NULL);
  996. return error;
  997. }
  998. static int gfs2_xattr_set(struct dentry *dentry, const char *name,
  999. const void *value, size_t size, int flags, int type)
  1000. {
  1001. return __gfs2_xattr_set(dentry->d_inode, name, value,
  1002. size, flags, type);
  1003. }
  1004. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  1005. struct gfs2_ea_header *ea, char *data)
  1006. {
  1007. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1008. struct buffer_head **bh;
  1009. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  1010. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  1011. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  1012. unsigned int x;
  1013. int error;
  1014. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  1015. if (!bh)
  1016. return -ENOMEM;
  1017. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  1018. if (error)
  1019. goto out;
  1020. for (x = 0; x < nptrs; x++) {
  1021. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
  1022. bh + x);
  1023. if (error) {
  1024. while (x--)
  1025. brelse(bh[x]);
  1026. goto fail;
  1027. }
  1028. dataptrs++;
  1029. }
  1030. for (x = 0; x < nptrs; x++) {
  1031. error = gfs2_meta_wait(sdp, bh[x]);
  1032. if (error) {
  1033. for (; x < nptrs; x++)
  1034. brelse(bh[x]);
  1035. goto fail;
  1036. }
  1037. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  1038. for (; x < nptrs; x++)
  1039. brelse(bh[x]);
  1040. error = -EIO;
  1041. goto fail;
  1042. }
  1043. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1044. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
  1045. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1046. amount -= sdp->sd_jbsize;
  1047. data += sdp->sd_jbsize;
  1048. brelse(bh[x]);
  1049. }
  1050. out:
  1051. kfree(bh);
  1052. return error;
  1053. fail:
  1054. gfs2_trans_end(sdp);
  1055. kfree(bh);
  1056. return error;
  1057. }
  1058. int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
  1059. {
  1060. struct inode *inode = &ip->i_inode;
  1061. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1062. struct gfs2_ea_location el;
  1063. int error;
  1064. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
  1065. if (error)
  1066. return error;
  1067. if (GFS2_EA_IS_STUFFED(el.el_ea)) {
  1068. error = gfs2_trans_begin(sdp, RES_DINODE + RES_EATTR, 0);
  1069. if (error == 0) {
  1070. gfs2_trans_add_bh(ip->i_gl, el.el_bh, 1);
  1071. memcpy(GFS2_EA2DATA(el.el_ea), data,
  1072. GFS2_EA_DATA_LEN(el.el_ea));
  1073. }
  1074. } else {
  1075. error = ea_acl_chmod_unstuffed(ip, el.el_ea, data);
  1076. }
  1077. brelse(el.el_bh);
  1078. if (error)
  1079. return error;
  1080. error = gfs2_setattr_simple(inode, attr);
  1081. gfs2_trans_end(sdp);
  1082. return error;
  1083. }
  1084. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1085. {
  1086. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1087. struct gfs2_rgrp_list rlist;
  1088. struct buffer_head *indbh, *dibh;
  1089. __be64 *eablk, *end;
  1090. unsigned int rg_blocks = 0;
  1091. u64 bstart = 0;
  1092. unsigned int blen = 0;
  1093. unsigned int blks = 0;
  1094. unsigned int x;
  1095. int error;
  1096. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1097. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh);
  1098. if (error)
  1099. return error;
  1100. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1101. error = -EIO;
  1102. goto out;
  1103. }
  1104. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1105. end = eablk + sdp->sd_inptrs;
  1106. for (; eablk < end; eablk++) {
  1107. u64 bn;
  1108. if (!*eablk)
  1109. break;
  1110. bn = be64_to_cpu(*eablk);
  1111. if (bstart + blen == bn)
  1112. blen++;
  1113. else {
  1114. if (bstart)
  1115. gfs2_rlist_add(ip, &rlist, bstart);
  1116. bstart = bn;
  1117. blen = 1;
  1118. }
  1119. blks++;
  1120. }
  1121. if (bstart)
  1122. gfs2_rlist_add(ip, &rlist, bstart);
  1123. else
  1124. goto out;
  1125. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1126. for (x = 0; x < rlist.rl_rgrps; x++) {
  1127. struct gfs2_rgrpd *rgd;
  1128. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1129. rg_blocks += rgd->rd_length;
  1130. }
  1131. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1132. if (error)
  1133. goto out_rlist_free;
  1134. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
  1135. RES_STATFS + RES_QUOTA, blks);
  1136. if (error)
  1137. goto out_gunlock;
  1138. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  1139. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1140. bstart = 0;
  1141. blen = 0;
  1142. for (; eablk < end; eablk++) {
  1143. u64 bn;
  1144. if (!*eablk)
  1145. break;
  1146. bn = be64_to_cpu(*eablk);
  1147. if (bstart + blen == bn)
  1148. blen++;
  1149. else {
  1150. if (bstart)
  1151. gfs2_free_meta(ip, bstart, blen);
  1152. bstart = bn;
  1153. blen = 1;
  1154. }
  1155. *eablk = 0;
  1156. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1157. }
  1158. if (bstart)
  1159. gfs2_free_meta(ip, bstart, blen);
  1160. ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
  1161. error = gfs2_meta_inode_buffer(ip, &dibh);
  1162. if (!error) {
  1163. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1164. gfs2_dinode_out(ip, dibh->b_data);
  1165. brelse(dibh);
  1166. }
  1167. gfs2_trans_end(sdp);
  1168. out_gunlock:
  1169. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1170. out_rlist_free:
  1171. gfs2_rlist_free(&rlist);
  1172. out:
  1173. brelse(indbh);
  1174. return error;
  1175. }
  1176. static int ea_dealloc_block(struct gfs2_inode *ip)
  1177. {
  1178. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1179. struct gfs2_alloc *al = ip->i_alloc;
  1180. struct gfs2_rgrpd *rgd;
  1181. struct buffer_head *dibh;
  1182. int error;
  1183. rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr);
  1184. if (!rgd) {
  1185. gfs2_consist_inode(ip);
  1186. return -EIO;
  1187. }
  1188. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  1189. &al->al_rgd_gh);
  1190. if (error)
  1191. return error;
  1192. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
  1193. RES_QUOTA, 1);
  1194. if (error)
  1195. goto out_gunlock;
  1196. gfs2_free_meta(ip, ip->i_eattr, 1);
  1197. ip->i_eattr = 0;
  1198. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1199. error = gfs2_meta_inode_buffer(ip, &dibh);
  1200. if (!error) {
  1201. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1202. gfs2_dinode_out(ip, dibh->b_data);
  1203. brelse(dibh);
  1204. }
  1205. gfs2_trans_end(sdp);
  1206. out_gunlock:
  1207. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1208. return error;
  1209. }
  1210. /**
  1211. * gfs2_ea_dealloc - deallocate the extended attribute fork
  1212. * @ip: the inode
  1213. *
  1214. * Returns: errno
  1215. */
  1216. int gfs2_ea_dealloc(struct gfs2_inode *ip)
  1217. {
  1218. struct gfs2_alloc *al;
  1219. int error;
  1220. al = gfs2_alloc_get(ip);
  1221. if (!al)
  1222. return -ENOMEM;
  1223. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1224. if (error)
  1225. goto out_alloc;
  1226. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1227. if (error)
  1228. goto out_quota;
  1229. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  1230. error = ea_dealloc_indirect(ip);
  1231. if (error)
  1232. goto out_quota;
  1233. }
  1234. error = ea_dealloc_block(ip);
  1235. out_quota:
  1236. gfs2_quota_unhold(ip);
  1237. out_alloc:
  1238. gfs2_alloc_put(ip);
  1239. return error;
  1240. }
  1241. static const struct xattr_handler gfs2_xattr_user_handler = {
  1242. .prefix = XATTR_USER_PREFIX,
  1243. .flags = GFS2_EATYPE_USR,
  1244. .get = gfs2_xattr_get,
  1245. .set = gfs2_xattr_set,
  1246. };
  1247. static const struct xattr_handler gfs2_xattr_security_handler = {
  1248. .prefix = XATTR_SECURITY_PREFIX,
  1249. .flags = GFS2_EATYPE_SECURITY,
  1250. .get = gfs2_xattr_get,
  1251. .set = gfs2_xattr_set,
  1252. };
  1253. const struct xattr_handler *gfs2_xattr_handlers[] = {
  1254. &gfs2_xattr_user_handler,
  1255. &gfs2_xattr_security_handler,
  1256. &gfs2_xattr_system_handler,
  1257. NULL,
  1258. };