xattr.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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, 1);
  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_qadata *qa;
  268. int error;
  269. qa = gfs2_qadata_get(ip);
  270. if (!qa)
  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_qadata_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_blocks(ip, &block, &n, 0, NULL);
  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_blocks(ip, &block, &n, 0, NULL);
  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_qadata *qa;
  593. struct buffer_head *dibh;
  594. int error;
  595. qa = gfs2_qadata_get(ip);
  596. if (!qa)
  597. return -ENOMEM;
  598. error = gfs2_quota_lock_check(ip);
  599. if (error)
  600. goto out;
  601. error = gfs2_inplace_reserve(ip, blks);
  602. if (error)
  603. goto out_gunlock_q;
  604. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  605. blks + gfs2_rg_blocks(ip) +
  606. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  607. if (error)
  608. goto out_ipres;
  609. error = skeleton_call(ip, er, private);
  610. if (error)
  611. goto out_end_trans;
  612. error = gfs2_meta_inode_buffer(ip, &dibh);
  613. if (!error) {
  614. ip->i_inode.i_ctime = CURRENT_TIME;
  615. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  616. gfs2_dinode_out(ip, dibh->b_data);
  617. brelse(dibh);
  618. }
  619. out_end_trans:
  620. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  621. out_ipres:
  622. gfs2_inplace_release(ip);
  623. out_gunlock_q:
  624. gfs2_quota_unlock(ip);
  625. out:
  626. gfs2_qadata_put(ip);
  627. return error;
  628. }
  629. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  630. void *private)
  631. {
  632. struct buffer_head *bh;
  633. int error;
  634. error = ea_alloc_blk(ip, &bh);
  635. if (error)
  636. return error;
  637. ip->i_eattr = bh->b_blocknr;
  638. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  639. brelse(bh);
  640. return error;
  641. }
  642. /**
  643. * ea_init - initializes a new eattr block
  644. * @ip:
  645. * @er:
  646. *
  647. * Returns: errno
  648. */
  649. static int ea_init(struct gfs2_inode *ip, int type, const char *name,
  650. const void *data, size_t size)
  651. {
  652. struct gfs2_ea_request er;
  653. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  654. unsigned int blks = 1;
  655. er.er_type = type;
  656. er.er_name = name;
  657. er.er_name_len = strlen(name);
  658. er.er_data = (void *)data;
  659. er.er_data_len = size;
  660. if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
  661. blks += DIV_ROUND_UP(er.er_data_len, jbsize);
  662. return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
  663. }
  664. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  665. {
  666. u32 ea_size = GFS2_EA_SIZE(ea);
  667. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  668. ea_size);
  669. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  670. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  671. ea->ea_rec_len = cpu_to_be32(ea_size);
  672. ea->ea_flags ^= last;
  673. new->ea_rec_len = cpu_to_be32(new_size);
  674. new->ea_flags = last;
  675. return new;
  676. }
  677. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  678. struct gfs2_ea_location *el)
  679. {
  680. struct gfs2_ea_header *ea = el->el_ea;
  681. struct gfs2_ea_header *prev = el->el_prev;
  682. u32 len;
  683. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  684. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  685. ea->ea_type = GFS2_EATYPE_UNUSED;
  686. return;
  687. } else if (GFS2_EA2NEXT(prev) != ea) {
  688. prev = GFS2_EA2NEXT(prev);
  689. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  690. }
  691. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  692. prev->ea_rec_len = cpu_to_be32(len);
  693. if (GFS2_EA_IS_LAST(ea))
  694. prev->ea_flags |= GFS2_EAFLAG_LAST;
  695. }
  696. struct ea_set {
  697. int ea_split;
  698. struct gfs2_ea_request *es_er;
  699. struct gfs2_ea_location *es_el;
  700. struct buffer_head *es_bh;
  701. struct gfs2_ea_header *es_ea;
  702. };
  703. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  704. struct gfs2_ea_header *ea, struct ea_set *es)
  705. {
  706. struct gfs2_ea_request *er = es->es_er;
  707. struct buffer_head *dibh;
  708. int error;
  709. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  710. if (error)
  711. return error;
  712. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  713. if (es->ea_split)
  714. ea = ea_split_ea(ea);
  715. ea_write(ip, ea, er);
  716. if (es->es_el)
  717. ea_set_remove_stuffed(ip, es->es_el);
  718. error = gfs2_meta_inode_buffer(ip, &dibh);
  719. if (error)
  720. goto out;
  721. ip->i_inode.i_ctime = CURRENT_TIME;
  722. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  723. gfs2_dinode_out(ip, dibh->b_data);
  724. brelse(dibh);
  725. out:
  726. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  727. return error;
  728. }
  729. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  730. struct gfs2_ea_request *er, void *private)
  731. {
  732. struct ea_set *es = private;
  733. struct gfs2_ea_header *ea = es->es_ea;
  734. int error;
  735. gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
  736. if (es->ea_split)
  737. ea = ea_split_ea(ea);
  738. error = ea_write(ip, ea, er);
  739. if (error)
  740. return error;
  741. if (es->es_el)
  742. ea_set_remove_stuffed(ip, es->es_el);
  743. return 0;
  744. }
  745. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  746. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  747. void *private)
  748. {
  749. struct ea_set *es = private;
  750. unsigned int size;
  751. int stuffed;
  752. int error;
  753. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
  754. es->es_er->er_data_len, &size);
  755. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  756. if (GFS2_EA_REC_LEN(ea) < size)
  757. return 0;
  758. if (!GFS2_EA_IS_STUFFED(ea)) {
  759. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  760. if (error)
  761. return error;
  762. }
  763. es->ea_split = 0;
  764. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  765. es->ea_split = 1;
  766. else
  767. return 0;
  768. if (stuffed) {
  769. error = ea_set_simple_noalloc(ip, bh, ea, es);
  770. if (error)
  771. return error;
  772. } else {
  773. unsigned int blks;
  774. es->es_bh = bh;
  775. es->es_ea = ea;
  776. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  777. GFS2_SB(&ip->i_inode)->sd_jbsize);
  778. error = ea_alloc_skeleton(ip, es->es_er, blks,
  779. ea_set_simple_alloc, es);
  780. if (error)
  781. return error;
  782. }
  783. return 1;
  784. }
  785. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  786. void *private)
  787. {
  788. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  789. struct buffer_head *indbh, *newbh;
  790. __be64 *eablk;
  791. int error;
  792. int mh_size = sizeof(struct gfs2_meta_header);
  793. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  794. __be64 *end;
  795. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT,
  796. &indbh);
  797. if (error)
  798. return error;
  799. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  800. error = -EIO;
  801. goto out;
  802. }
  803. eablk = (__be64 *)(indbh->b_data + mh_size);
  804. end = eablk + sdp->sd_inptrs;
  805. for (; eablk < end; eablk++)
  806. if (!*eablk)
  807. break;
  808. if (eablk == end) {
  809. error = -ENOSPC;
  810. goto out;
  811. }
  812. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  813. } else {
  814. u64 blk;
  815. unsigned int n = 1;
  816. error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
  817. if (error)
  818. return error;
  819. gfs2_trans_add_unrevoke(sdp, blk, 1);
  820. indbh = gfs2_meta_new(ip->i_gl, blk);
  821. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  822. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  823. gfs2_buffer_clear_tail(indbh, mh_size);
  824. eablk = (__be64 *)(indbh->b_data + mh_size);
  825. *eablk = cpu_to_be64(ip->i_eattr);
  826. ip->i_eattr = blk;
  827. ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
  828. gfs2_add_inode_blocks(&ip->i_inode, 1);
  829. eablk++;
  830. }
  831. error = ea_alloc_blk(ip, &newbh);
  832. if (error)
  833. goto out;
  834. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  835. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  836. brelse(newbh);
  837. if (error)
  838. goto out;
  839. if (private)
  840. ea_set_remove_stuffed(ip, private);
  841. out:
  842. brelse(indbh);
  843. return error;
  844. }
  845. static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
  846. const void *value, size_t size, struct gfs2_ea_location *el)
  847. {
  848. struct gfs2_ea_request er;
  849. struct ea_set es;
  850. unsigned int blks = 2;
  851. int error;
  852. er.er_type = type;
  853. er.er_name = name;
  854. er.er_data = (void *)value;
  855. er.er_name_len = strlen(name);
  856. er.er_data_len = size;
  857. memset(&es, 0, sizeof(struct ea_set));
  858. es.es_er = &er;
  859. es.es_el = el;
  860. error = ea_foreach(ip, ea_set_simple, &es);
  861. if (error > 0)
  862. return 0;
  863. if (error)
  864. return error;
  865. if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
  866. blks++;
  867. if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  868. blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  869. return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
  870. }
  871. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  872. struct gfs2_ea_location *el)
  873. {
  874. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  875. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  876. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  877. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  878. }
  879. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
  880. }
  881. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  882. {
  883. struct gfs2_ea_header *ea = el->el_ea;
  884. struct gfs2_ea_header *prev = el->el_prev;
  885. struct buffer_head *dibh;
  886. int error;
  887. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  888. if (error)
  889. return error;
  890. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  891. if (prev) {
  892. u32 len;
  893. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  894. prev->ea_rec_len = cpu_to_be32(len);
  895. if (GFS2_EA_IS_LAST(ea))
  896. prev->ea_flags |= GFS2_EAFLAG_LAST;
  897. } else {
  898. ea->ea_type = GFS2_EATYPE_UNUSED;
  899. }
  900. error = gfs2_meta_inode_buffer(ip, &dibh);
  901. if (!error) {
  902. ip->i_inode.i_ctime = CURRENT_TIME;
  903. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  904. gfs2_dinode_out(ip, dibh->b_data);
  905. brelse(dibh);
  906. }
  907. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  908. return error;
  909. }
  910. /**
  911. * gfs2_xattr_remove - Remove a GFS2 extended attribute
  912. * @ip: The inode
  913. * @type: The type of the extended attribute
  914. * @name: The name of the extended attribute
  915. *
  916. * This is not called directly by the VFS since we use the (common)
  917. * scheme of making a "set with NULL data" mean a remove request. Note
  918. * that this is different from a set with zero length data.
  919. *
  920. * Returns: 0, or errno on failure
  921. */
  922. static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
  923. {
  924. struct gfs2_ea_location el;
  925. int error;
  926. if (!ip->i_eattr)
  927. return -ENODATA;
  928. error = gfs2_ea_find(ip, type, name, &el);
  929. if (error)
  930. return error;
  931. if (!el.el_ea)
  932. return -ENODATA;
  933. if (GFS2_EA_IS_STUFFED(el.el_ea))
  934. error = ea_remove_stuffed(ip, &el);
  935. else
  936. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
  937. brelse(el.el_bh);
  938. return error;
  939. }
  940. /**
  941. * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
  942. * @ip: The inode
  943. * @name: The name of the extended attribute
  944. * @value: The value of the extended attribute (NULL for remove)
  945. * @size: The size of the @value argument
  946. * @flags: Create or Replace
  947. * @type: The type of the extended attribute
  948. *
  949. * See gfs2_xattr_remove() for details of the removal of xattrs.
  950. *
  951. * Returns: 0 or errno on failure
  952. */
  953. int __gfs2_xattr_set(struct inode *inode, const char *name,
  954. const void *value, size_t size, int flags, int type)
  955. {
  956. struct gfs2_inode *ip = GFS2_I(inode);
  957. struct gfs2_sbd *sdp = GFS2_SB(inode);
  958. struct gfs2_ea_location el;
  959. unsigned int namel = strlen(name);
  960. int error;
  961. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  962. return -EPERM;
  963. if (namel > GFS2_EA_MAX_NAME_LEN)
  964. return -ERANGE;
  965. if (value == NULL)
  966. return gfs2_xattr_remove(ip, type, name);
  967. if (ea_check_size(sdp, namel, size))
  968. return -ERANGE;
  969. if (!ip->i_eattr) {
  970. if (flags & XATTR_REPLACE)
  971. return -ENODATA;
  972. return ea_init(ip, type, name, value, size);
  973. }
  974. error = gfs2_ea_find(ip, type, name, &el);
  975. if (error)
  976. return error;
  977. if (el.el_ea) {
  978. if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
  979. brelse(el.el_bh);
  980. return -EPERM;
  981. }
  982. error = -EEXIST;
  983. if (!(flags & XATTR_CREATE)) {
  984. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  985. error = ea_set_i(ip, type, name, value, size, &el);
  986. if (!error && unstuffed)
  987. ea_set_remove_unstuffed(ip, &el);
  988. }
  989. brelse(el.el_bh);
  990. return error;
  991. }
  992. error = -ENODATA;
  993. if (!(flags & XATTR_REPLACE))
  994. error = ea_set_i(ip, type, name, value, size, NULL);
  995. return error;
  996. }
  997. static int gfs2_xattr_set(struct dentry *dentry, const char *name,
  998. const void *value, size_t size, int flags, int type)
  999. {
  1000. return __gfs2_xattr_set(dentry->d_inode, name, value,
  1001. size, flags, type);
  1002. }
  1003. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  1004. struct gfs2_ea_header *ea, char *data)
  1005. {
  1006. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1007. struct buffer_head **bh;
  1008. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  1009. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  1010. __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  1011. unsigned int x;
  1012. int error;
  1013. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
  1014. if (!bh)
  1015. return -ENOMEM;
  1016. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  1017. if (error)
  1018. goto out;
  1019. for (x = 0; x < nptrs; x++) {
  1020. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
  1021. bh + x);
  1022. if (error) {
  1023. while (x--)
  1024. brelse(bh[x]);
  1025. goto fail;
  1026. }
  1027. dataptrs++;
  1028. }
  1029. for (x = 0; x < nptrs; x++) {
  1030. error = gfs2_meta_wait(sdp, bh[x]);
  1031. if (error) {
  1032. for (; x < nptrs; x++)
  1033. brelse(bh[x]);
  1034. goto fail;
  1035. }
  1036. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  1037. for (; x < nptrs; x++)
  1038. brelse(bh[x]);
  1039. error = -EIO;
  1040. goto fail;
  1041. }
  1042. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1043. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
  1044. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1045. amount -= sdp->sd_jbsize;
  1046. data += sdp->sd_jbsize;
  1047. brelse(bh[x]);
  1048. }
  1049. out:
  1050. kfree(bh);
  1051. return error;
  1052. fail:
  1053. gfs2_trans_end(sdp);
  1054. kfree(bh);
  1055. return error;
  1056. }
  1057. int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
  1058. {
  1059. struct inode *inode = &ip->i_inode;
  1060. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1061. struct gfs2_ea_location el;
  1062. int error;
  1063. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
  1064. if (error)
  1065. return error;
  1066. if (GFS2_EA_IS_STUFFED(el.el_ea)) {
  1067. error = gfs2_trans_begin(sdp, RES_DINODE + RES_EATTR, 0);
  1068. if (error == 0) {
  1069. gfs2_trans_add_bh(ip->i_gl, el.el_bh, 1);
  1070. memcpy(GFS2_EA2DATA(el.el_ea), data,
  1071. GFS2_EA_DATA_LEN(el.el_ea));
  1072. }
  1073. } else {
  1074. error = ea_acl_chmod_unstuffed(ip, el.el_ea, data);
  1075. }
  1076. brelse(el.el_bh);
  1077. if (error)
  1078. return error;
  1079. error = gfs2_setattr_simple(inode, attr);
  1080. gfs2_trans_end(sdp);
  1081. return error;
  1082. }
  1083. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1084. {
  1085. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1086. struct gfs2_rgrp_list rlist;
  1087. struct buffer_head *indbh, *dibh;
  1088. __be64 *eablk, *end;
  1089. unsigned int rg_blocks = 0;
  1090. u64 bstart = 0;
  1091. unsigned int blen = 0;
  1092. unsigned int blks = 0;
  1093. unsigned int x;
  1094. int error;
  1095. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1096. error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh);
  1097. if (error)
  1098. return error;
  1099. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1100. error = -EIO;
  1101. goto out;
  1102. }
  1103. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1104. end = eablk + sdp->sd_inptrs;
  1105. for (; eablk < end; eablk++) {
  1106. u64 bn;
  1107. if (!*eablk)
  1108. break;
  1109. bn = be64_to_cpu(*eablk);
  1110. if (bstart + blen == bn)
  1111. blen++;
  1112. else {
  1113. if (bstart)
  1114. gfs2_rlist_add(ip, &rlist, bstart);
  1115. bstart = bn;
  1116. blen = 1;
  1117. }
  1118. blks++;
  1119. }
  1120. if (bstart)
  1121. gfs2_rlist_add(ip, &rlist, bstart);
  1122. else
  1123. goto out;
  1124. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1125. for (x = 0; x < rlist.rl_rgrps; x++) {
  1126. struct gfs2_rgrpd *rgd;
  1127. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1128. rg_blocks += rgd->rd_length;
  1129. }
  1130. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1131. if (error)
  1132. goto out_rlist_free;
  1133. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
  1134. RES_STATFS + RES_QUOTA, blks);
  1135. if (error)
  1136. goto out_gunlock;
  1137. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  1138. eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1139. bstart = 0;
  1140. blen = 0;
  1141. for (; eablk < end; eablk++) {
  1142. u64 bn;
  1143. if (!*eablk)
  1144. break;
  1145. bn = be64_to_cpu(*eablk);
  1146. if (bstart + blen == bn)
  1147. blen++;
  1148. else {
  1149. if (bstart)
  1150. gfs2_free_meta(ip, bstart, blen);
  1151. bstart = bn;
  1152. blen = 1;
  1153. }
  1154. *eablk = 0;
  1155. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1156. }
  1157. if (bstart)
  1158. gfs2_free_meta(ip, bstart, blen);
  1159. ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
  1160. error = gfs2_meta_inode_buffer(ip, &dibh);
  1161. if (!error) {
  1162. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1163. gfs2_dinode_out(ip, dibh->b_data);
  1164. brelse(dibh);
  1165. }
  1166. gfs2_trans_end(sdp);
  1167. out_gunlock:
  1168. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1169. out_rlist_free:
  1170. gfs2_rlist_free(&rlist);
  1171. out:
  1172. brelse(indbh);
  1173. return error;
  1174. }
  1175. static int ea_dealloc_block(struct gfs2_inode *ip)
  1176. {
  1177. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1178. struct gfs2_rgrpd *rgd;
  1179. struct buffer_head *dibh;
  1180. struct gfs2_holder gh;
  1181. int error;
  1182. rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
  1183. if (!rgd) {
  1184. gfs2_consist_inode(ip);
  1185. return -EIO;
  1186. }
  1187. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1188. if (error)
  1189. return error;
  1190. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
  1191. RES_QUOTA, 1);
  1192. if (error)
  1193. goto out_gunlock;
  1194. gfs2_free_meta(ip, ip->i_eattr, 1);
  1195. ip->i_eattr = 0;
  1196. gfs2_add_inode_blocks(&ip->i_inode, -1);
  1197. error = gfs2_meta_inode_buffer(ip, &dibh);
  1198. if (!error) {
  1199. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1200. gfs2_dinode_out(ip, dibh->b_data);
  1201. brelse(dibh);
  1202. }
  1203. gfs2_trans_end(sdp);
  1204. out_gunlock:
  1205. gfs2_glock_dq_uninit(&gh);
  1206. return error;
  1207. }
  1208. /**
  1209. * gfs2_ea_dealloc - deallocate the extended attribute fork
  1210. * @ip: the inode
  1211. *
  1212. * Returns: errno
  1213. */
  1214. int gfs2_ea_dealloc(struct gfs2_inode *ip)
  1215. {
  1216. struct gfs2_qadata *qa;
  1217. int error;
  1218. qa = gfs2_qadata_get(ip);
  1219. if (!qa)
  1220. return -ENOMEM;
  1221. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1222. if (error)
  1223. goto out_alloc;
  1224. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1225. if (error)
  1226. goto out_quota;
  1227. if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
  1228. error = ea_dealloc_indirect(ip);
  1229. if (error)
  1230. goto out_quota;
  1231. }
  1232. error = ea_dealloc_block(ip);
  1233. out_quota:
  1234. gfs2_quota_unhold(ip);
  1235. out_alloc:
  1236. gfs2_qadata_put(ip);
  1237. return error;
  1238. }
  1239. static const struct xattr_handler gfs2_xattr_user_handler = {
  1240. .prefix = XATTR_USER_PREFIX,
  1241. .flags = GFS2_EATYPE_USR,
  1242. .get = gfs2_xattr_get,
  1243. .set = gfs2_xattr_set,
  1244. };
  1245. static const struct xattr_handler gfs2_xattr_security_handler = {
  1246. .prefix = XATTR_SECURITY_PREFIX,
  1247. .flags = GFS2_EATYPE_SECURITY,
  1248. .get = gfs2_xattr_get,
  1249. .set = gfs2_xattr_set,
  1250. };
  1251. const struct xattr_handler *gfs2_xattr_handlers[] = {
  1252. &gfs2_xattr_user_handler,
  1253. &gfs2_xattr_security_handler,
  1254. &gfs2_xattr_system_handler,
  1255. NULL,
  1256. };