eattr.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/xattr.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <asm/semaphore.h>
  17. #include <asm/uaccess.h>
  18. #include "gfs2.h"
  19. #include "lm_interface.h"
  20. #include "incore.h"
  21. #include "acl.h"
  22. #include "eaops.h"
  23. #include "eattr.h"
  24. #include "glock.h"
  25. #include "inode.h"
  26. #include "meta_io.h"
  27. #include "quota.h"
  28. #include "rgrp.h"
  29. #include "trans.h"
  30. #include "util.h"
  31. /**
  32. * ea_calc_size - returns the acutal number of bytes the request will take up
  33. * (not counting any unstuffed data blocks)
  34. * @sdp:
  35. * @er:
  36. * @size:
  37. *
  38. * Returns: 1 if the EA should be stuffed
  39. */
  40. static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
  41. unsigned int *size)
  42. {
  43. *size = GFS2_EAREQ_SIZE_STUFFED(er);
  44. if (*size <= sdp->sd_jbsize)
  45. return 1;
  46. *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
  47. return 0;
  48. }
  49. static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
  50. {
  51. unsigned int size;
  52. if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
  53. return -ERANGE;
  54. ea_calc_size(sdp, er, &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,
  61. struct buffer_head *bh,
  62. struct gfs2_ea_header *ea,
  63. struct gfs2_ea_header *prev,
  64. void *private);
  65. static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
  66. ea_call_t ea_call, void *data)
  67. {
  68. struct gfs2_ea_header *ea, *prev = NULL;
  69. int error = 0;
  70. if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_EA))
  71. return -EIO;
  72. for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
  73. if (!GFS2_EA_REC_LEN(ea))
  74. goto fail;
  75. if (!(bh->b_data <= (char *)ea &&
  76. (char *)GFS2_EA2NEXT(ea) <=
  77. bh->b_data + bh->b_size))
  78. goto fail;
  79. if (!GFS2_EATYPE_VALID(ea->ea_type))
  80. goto fail;
  81. error = ea_call(ip, bh, ea, prev, data);
  82. if (error)
  83. return error;
  84. if (GFS2_EA_IS_LAST(ea)) {
  85. if ((char *)GFS2_EA2NEXT(ea) !=
  86. bh->b_data + bh->b_size)
  87. goto fail;
  88. break;
  89. }
  90. }
  91. return error;
  92. fail:
  93. gfs2_consist_inode(ip);
  94. return -EIO;
  95. }
  96. static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
  97. {
  98. struct buffer_head *bh, *eabh;
  99. uint64_t *eablk, *end;
  100. int error;
  101. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  102. DIO_START | DIO_WAIT, &bh);
  103. if (error)
  104. return error;
  105. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
  106. error = ea_foreach_i(ip, bh, ea_call, data);
  107. goto out;
  108. }
  109. if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_IN)) {
  110. error = -EIO;
  111. goto out;
  112. }
  113. eablk = (uint64_t *)(bh->b_data + sizeof(struct gfs2_meta_header));
  114. end = eablk + ip->i_sbd->sd_inptrs;
  115. for (; eablk < end; eablk++) {
  116. uint64_t bn;
  117. if (!*eablk)
  118. break;
  119. bn = be64_to_cpu(*eablk);
  120. error = gfs2_meta_read(ip->i_gl, bn, DIO_START | DIO_WAIT,
  121. &eabh);
  122. if (error)
  123. break;
  124. error = ea_foreach_i(ip, eabh, ea_call, data);
  125. brelse(eabh);
  126. if (error)
  127. break;
  128. }
  129. out:
  130. brelse(bh);
  131. return error;
  132. }
  133. struct ea_find {
  134. struct gfs2_ea_request *ef_er;
  135. struct gfs2_ea_location *ef_el;
  136. };
  137. static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
  138. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  139. void *private)
  140. {
  141. struct ea_find *ef = private;
  142. struct gfs2_ea_request *er = ef->ef_er;
  143. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  144. return 0;
  145. if (ea->ea_type == er->er_type) {
  146. if (ea->ea_name_len == er->er_name_len &&
  147. !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
  148. struct gfs2_ea_location *el = ef->ef_el;
  149. get_bh(bh);
  150. el->el_bh = bh;
  151. el->el_ea = ea;
  152. el->el_prev = prev;
  153. return 1;
  154. }
  155. }
  156. #if 0
  157. else if ((ip->i_di.di_flags & GFS2_DIF_EA_PACKED) &&
  158. er->er_type == GFS2_EATYPE_SYS)
  159. return 1;
  160. #endif
  161. return 0;
  162. }
  163. int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  164. struct gfs2_ea_location *el)
  165. {
  166. struct ea_find ef;
  167. int error;
  168. ef.ef_er = er;
  169. ef.ef_el = el;
  170. memset(el, 0, sizeof(struct gfs2_ea_location));
  171. error = ea_foreach(ip, ea_find_i, &ef);
  172. if (error > 0)
  173. return 0;
  174. return error;
  175. }
  176. /**
  177. * ea_dealloc_unstuffed -
  178. * @ip:
  179. * @bh:
  180. * @ea:
  181. * @prev:
  182. * @private:
  183. *
  184. * Take advantage of the fact that all unstuffed blocks are
  185. * allocated from the same RG. But watch, this may not always
  186. * be true.
  187. *
  188. * Returns: errno
  189. */
  190. static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  191. struct gfs2_ea_header *ea,
  192. struct gfs2_ea_header *prev, void *private)
  193. {
  194. int *leave = private;
  195. struct gfs2_sbd *sdp = ip->i_sbd;
  196. struct gfs2_rgrpd *rgd;
  197. struct gfs2_holder rg_gh;
  198. struct buffer_head *dibh;
  199. uint64_t *dataptrs, bn = 0;
  200. uint64_t bstart = 0;
  201. unsigned int blen = 0;
  202. unsigned int blks = 0;
  203. unsigned int x;
  204. int error;
  205. if (GFS2_EA_IS_STUFFED(ea))
  206. return 0;
  207. dataptrs = GFS2_EA2DATAPTRS(ea);
  208. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++)
  209. if (*dataptrs) {
  210. blks++;
  211. bn = be64_to_cpu(*dataptrs);
  212. }
  213. if (!blks)
  214. return 0;
  215. rgd = gfs2_blk2rgrpd(sdp, bn);
  216. if (!rgd) {
  217. gfs2_consist_inode(ip);
  218. return -EIO;
  219. }
  220. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
  221. if (error)
  222. return error;
  223. error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length +
  224. RES_DINODE + RES_EATTR + RES_STATFS +
  225. RES_QUOTA, blks);
  226. if (error)
  227. goto out_gunlock;
  228. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  229. dataptrs = GFS2_EA2DATAPTRS(ea);
  230. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  231. if (!*dataptrs)
  232. break;
  233. bn = be64_to_cpu(*dataptrs);
  234. if (bstart + blen == bn)
  235. blen++;
  236. else {
  237. if (bstart)
  238. gfs2_free_meta(ip, bstart, blen);
  239. bstart = bn;
  240. blen = 1;
  241. }
  242. *dataptrs = 0;
  243. if (!ip->i_di.di_blocks)
  244. gfs2_consist_inode(ip);
  245. ip->i_di.di_blocks--;
  246. }
  247. if (bstart)
  248. gfs2_free_meta(ip, bstart, blen);
  249. if (prev && !leave) {
  250. uint32_t len;
  251. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  252. prev->ea_rec_len = cpu_to_be32(len);
  253. if (GFS2_EA_IS_LAST(ea))
  254. prev->ea_flags |= GFS2_EAFLAG_LAST;
  255. } else {
  256. ea->ea_type = GFS2_EATYPE_UNUSED;
  257. ea->ea_num_ptrs = 0;
  258. }
  259. error = gfs2_meta_inode_buffer(ip, &dibh);
  260. if (!error) {
  261. ip->i_di.di_ctime = get_seconds();
  262. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  263. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  264. brelse(dibh);
  265. }
  266. gfs2_trans_end(sdp);
  267. out_gunlock:
  268. gfs2_glock_dq_uninit(&rg_gh);
  269. return error;
  270. }
  271. static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  272. struct gfs2_ea_header *ea,
  273. struct gfs2_ea_header *prev, int leave)
  274. {
  275. struct gfs2_alloc *al;
  276. int error;
  277. al = gfs2_alloc_get(ip);
  278. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  279. if (error)
  280. goto out_alloc;
  281. error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
  282. if (error)
  283. goto out_quota;
  284. error = ea_dealloc_unstuffed(ip,
  285. bh, ea, prev,
  286. (leave) ? &error : NULL);
  287. gfs2_glock_dq_uninit(&al->al_ri_gh);
  288. out_quota:
  289. gfs2_quota_unhold(ip);
  290. out_alloc:
  291. gfs2_alloc_put(ip);
  292. return error;
  293. }
  294. static int gfs2_ea_repack_i(struct gfs2_inode *ip)
  295. {
  296. return -EOPNOTSUPP;
  297. }
  298. int gfs2_ea_repack(struct gfs2_inode *ip)
  299. {
  300. struct gfs2_holder gh;
  301. int error;
  302. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  303. if (error)
  304. return error;
  305. /* Some sort of permissions checking would be nice */
  306. error = gfs2_ea_repack_i(ip);
  307. gfs2_glock_dq_uninit(&gh);
  308. return error;
  309. }
  310. struct ea_list {
  311. struct gfs2_ea_request *ei_er;
  312. unsigned int ei_size;
  313. };
  314. static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
  315. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  316. void *private)
  317. {
  318. struct ea_list *ei = private;
  319. struct gfs2_ea_request *er = ei->ei_er;
  320. unsigned int ea_size = GFS2_EA_STRLEN(ea);
  321. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  322. return 0;
  323. if (er->er_data_len) {
  324. char *prefix;
  325. unsigned int l;
  326. char c = 0;
  327. if (ei->ei_size + ea_size > er->er_data_len)
  328. return -ERANGE;
  329. if (ea->ea_type == GFS2_EATYPE_USR) {
  330. prefix = "user.";
  331. l = 5;
  332. } else {
  333. prefix = "system.";
  334. l = 7;
  335. }
  336. memcpy(er->er_data + ei->ei_size,
  337. prefix, l);
  338. memcpy(er->er_data + ei->ei_size + l,
  339. GFS2_EA2NAME(ea),
  340. ea->ea_name_len);
  341. memcpy(er->er_data + ei->ei_size +
  342. ea_size - 1,
  343. &c, 1);
  344. }
  345. ei->ei_size += ea_size;
  346. return 0;
  347. }
  348. /**
  349. * gfs2_ea_list -
  350. * @ip:
  351. * @er:
  352. *
  353. * Returns: actual size of data on success, -errno on error
  354. */
  355. int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  356. {
  357. struct gfs2_holder i_gh;
  358. int error;
  359. if (!er->er_data || !er->er_data_len) {
  360. er->er_data = NULL;
  361. er->er_data_len = 0;
  362. }
  363. error = gfs2_glock_nq_init(ip->i_gl,
  364. LM_ST_SHARED, LM_FLAG_ANY,
  365. &i_gh);
  366. if (error)
  367. return error;
  368. if (ip->i_di.di_eattr) {
  369. struct ea_list ei = { .ei_er = er, .ei_size = 0 };
  370. error = ea_foreach(ip, ea_list_i, &ei);
  371. if (!error)
  372. error = ei.ei_size;
  373. }
  374. gfs2_glock_dq_uninit(&i_gh);
  375. return error;
  376. }
  377. /**
  378. * ea_get_unstuffed - actually copies the unstuffed data into the
  379. * request buffer
  380. * @ip:
  381. * @ea:
  382. * @data:
  383. *
  384. * Returns: errno
  385. */
  386. static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  387. char *data)
  388. {
  389. struct gfs2_sbd *sdp = ip->i_sbd;
  390. struct buffer_head **bh;
  391. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  392. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  393. uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
  394. unsigned int x;
  395. int error = 0;
  396. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  397. if (!bh)
  398. return -ENOMEM;
  399. for (x = 0; x < nptrs; x++) {
  400. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
  401. DIO_START, bh + x);
  402. if (error) {
  403. while (x--)
  404. brelse(bh[x]);
  405. goto out;
  406. }
  407. dataptrs++;
  408. }
  409. for (x = 0; x < nptrs; x++) {
  410. error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
  411. if (error) {
  412. for (; x < nptrs; x++)
  413. brelse(bh[x]);
  414. goto out;
  415. }
  416. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  417. for (; x < nptrs; x++)
  418. brelse(bh[x]);
  419. error = -EIO;
  420. goto out;
  421. }
  422. memcpy(data,
  423. bh[x]->b_data + sizeof(struct gfs2_meta_header),
  424. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  425. amount -= sdp->sd_jbsize;
  426. data += sdp->sd_jbsize;
  427. brelse(bh[x]);
  428. }
  429. out:
  430. kfree(bh);
  431. return error;
  432. }
  433. int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  434. char *data)
  435. {
  436. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  437. memcpy(data,
  438. GFS2_EA2DATA(el->el_ea),
  439. GFS2_EA_DATA_LEN(el->el_ea));
  440. return 0;
  441. } else
  442. return ea_get_unstuffed(ip, el->el_ea, data);
  443. }
  444. /**
  445. * gfs2_ea_get_i -
  446. * @ip:
  447. * @er:
  448. *
  449. * Returns: actual size of data on success, -errno on error
  450. */
  451. int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  452. {
  453. struct gfs2_ea_location el;
  454. int error;
  455. if (!ip->i_di.di_eattr)
  456. return -ENODATA;
  457. error = gfs2_ea_find(ip, er, &el);
  458. if (error)
  459. return error;
  460. if (!el.el_ea)
  461. return -ENODATA;
  462. if (er->er_data_len) {
  463. if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
  464. error = -ERANGE;
  465. else
  466. error = gfs2_ea_get_copy(ip, &el, er->er_data);
  467. }
  468. if (!error)
  469. error = GFS2_EA_DATA_LEN(el.el_ea);
  470. brelse(el.el_bh);
  471. return error;
  472. }
  473. /**
  474. * gfs2_ea_get -
  475. * @ip:
  476. * @er:
  477. *
  478. * Returns: actual size of data on success, -errno on error
  479. */
  480. int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  481. {
  482. struct gfs2_holder i_gh;
  483. int error;
  484. if (!er->er_name_len ||
  485. er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  486. return -EINVAL;
  487. if (!er->er_data || !er->er_data_len) {
  488. er->er_data = NULL;
  489. er->er_data_len = 0;
  490. }
  491. error = gfs2_glock_nq_init(ip->i_gl,
  492. LM_ST_SHARED, LM_FLAG_ANY,
  493. &i_gh);
  494. if (error)
  495. return error;
  496. error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
  497. gfs2_glock_dq_uninit(&i_gh);
  498. return error;
  499. }
  500. /**
  501. * ea_alloc_blk - allocates a new block for extended attributes.
  502. * @ip: A pointer to the inode that's getting extended attributes
  503. * @bhp:
  504. *
  505. * Returns: errno
  506. */
  507. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  508. {
  509. struct gfs2_sbd *sdp = ip->i_sbd;
  510. struct gfs2_ea_header *ea;
  511. uint64_t block;
  512. block = gfs2_alloc_meta(ip);
  513. *bhp = gfs2_meta_new(ip->i_gl, block);
  514. gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
  515. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  516. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  517. ea = GFS2_EA_BH2FIRST(*bhp);
  518. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  519. ea->ea_type = GFS2_EATYPE_UNUSED;
  520. ea->ea_flags = GFS2_EAFLAG_LAST;
  521. ea->ea_num_ptrs = 0;
  522. ip->i_di.di_blocks++;
  523. return 0;
  524. }
  525. /**
  526. * ea_write - writes the request info to an ea, creating new blocks if
  527. * necessary
  528. * @ip: inode that is being modified
  529. * @ea: the location of the new ea in a block
  530. * @er: the write request
  531. *
  532. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  533. *
  534. * returns : errno
  535. */
  536. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  537. struct gfs2_ea_request *er)
  538. {
  539. struct gfs2_sbd *sdp = ip->i_sbd;
  540. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  541. ea->ea_name_len = er->er_name_len;
  542. ea->ea_type = er->er_type;
  543. ea->__pad = 0;
  544. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  545. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  546. ea->ea_num_ptrs = 0;
  547. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  548. } else {
  549. uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
  550. const char *data = er->er_data;
  551. unsigned int data_len = er->er_data_len;
  552. unsigned int copy;
  553. unsigned int x;
  554. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  555. for (x = 0; x < ea->ea_num_ptrs; x++) {
  556. struct buffer_head *bh;
  557. uint64_t block;
  558. int mh_size = sizeof(struct gfs2_meta_header);
  559. block = gfs2_alloc_meta(ip);
  560. bh = gfs2_meta_new(ip->i_gl, block);
  561. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  562. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  563. ip->i_di.di_blocks++;
  564. copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
  565. data_len;
  566. memcpy(bh->b_data + mh_size, data, copy);
  567. if (copy < sdp->sd_jbsize)
  568. memset(bh->b_data + mh_size + copy, 0,
  569. sdp->sd_jbsize - copy);
  570. *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
  571. data += copy;
  572. data_len -= copy;
  573. brelse(bh);
  574. }
  575. gfs2_assert_withdraw(sdp, !data_len);
  576. }
  577. return 0;
  578. }
  579. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  580. struct gfs2_ea_request *er,
  581. void *private);
  582. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  583. unsigned int blks,
  584. ea_skeleton_call_t skeleton_call,
  585. void *private)
  586. {
  587. struct gfs2_alloc *al;
  588. struct buffer_head *dibh;
  589. int error;
  590. al = gfs2_alloc_get(ip);
  591. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  592. if (error)
  593. goto out;
  594. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  595. if (error)
  596. goto out_gunlock_q;
  597. al->al_requested = blks;
  598. error = gfs2_inplace_reserve(ip);
  599. if (error)
  600. goto out_gunlock_q;
  601. error = gfs2_trans_begin(ip->i_sbd,
  602. blks + al->al_rgd->rd_ri.ri_length +
  603. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  604. if (error)
  605. goto out_ipres;
  606. error = skeleton_call(ip, er, private);
  607. if (error)
  608. goto out_end_trans;
  609. error = gfs2_meta_inode_buffer(ip, &dibh);
  610. if (!error) {
  611. if (er->er_flags & GFS2_ERF_MODE) {
  612. gfs2_assert_withdraw(ip->i_sbd,
  613. (ip->i_di.di_mode & S_IFMT) ==
  614. (er->er_mode & S_IFMT));
  615. ip->i_di.di_mode = er->er_mode;
  616. }
  617. ip->i_di.di_ctime = get_seconds();
  618. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  619. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  620. brelse(dibh);
  621. }
  622. out_end_trans:
  623. gfs2_trans_end(ip->i_sbd);
  624. out_ipres:
  625. gfs2_inplace_release(ip);
  626. out_gunlock_q:
  627. gfs2_quota_unlock(ip);
  628. out:
  629. gfs2_alloc_put(ip);
  630. return error;
  631. }
  632. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  633. void *private)
  634. {
  635. struct buffer_head *bh;
  636. int error;
  637. error = ea_alloc_blk(ip, &bh);
  638. if (error)
  639. return error;
  640. ip->i_di.di_eattr = bh->b_blocknr;
  641. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  642. brelse(bh);
  643. return error;
  644. }
  645. /**
  646. * ea_init - initializes a new eattr block
  647. * @ip:
  648. * @er:
  649. *
  650. * Returns: errno
  651. */
  652. static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  653. {
  654. unsigned int jbsize = ip->i_sbd->sd_jbsize;
  655. unsigned int blks = 1;
  656. if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
  657. blks += DIV_ROUND_UP(er->er_data_len, jbsize);
  658. return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
  659. }
  660. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  661. {
  662. uint32_t ea_size = GFS2_EA_SIZE(ea);
  663. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  664. ea_size);
  665. uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  666. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  667. ea->ea_rec_len = cpu_to_be32(ea_size);
  668. ea->ea_flags ^= last;
  669. new->ea_rec_len = cpu_to_be32(new_size);
  670. new->ea_flags = last;
  671. return new;
  672. }
  673. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  674. struct gfs2_ea_location *el)
  675. {
  676. struct gfs2_ea_header *ea = el->el_ea;
  677. struct gfs2_ea_header *prev = el->el_prev;
  678. uint32_t len;
  679. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  680. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  681. ea->ea_type = GFS2_EATYPE_UNUSED;
  682. return;
  683. } else if (GFS2_EA2NEXT(prev) != ea) {
  684. prev = GFS2_EA2NEXT(prev);
  685. gfs2_assert_withdraw(ip->i_sbd, GFS2_EA2NEXT(prev) == ea);
  686. }
  687. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  688. prev->ea_rec_len = cpu_to_be32(len);
  689. if (GFS2_EA_IS_LAST(ea))
  690. prev->ea_flags |= GFS2_EAFLAG_LAST;
  691. }
  692. struct ea_set {
  693. int ea_split;
  694. struct gfs2_ea_request *es_er;
  695. struct gfs2_ea_location *es_el;
  696. struct buffer_head *es_bh;
  697. struct gfs2_ea_header *es_ea;
  698. };
  699. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  700. struct gfs2_ea_header *ea, struct ea_set *es)
  701. {
  702. struct gfs2_ea_request *er = es->es_er;
  703. struct buffer_head *dibh;
  704. int error;
  705. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + 2 * RES_EATTR, 0);
  706. if (error)
  707. return error;
  708. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  709. if (es->ea_split)
  710. ea = ea_split_ea(ea);
  711. ea_write(ip, ea, er);
  712. if (es->es_el)
  713. ea_set_remove_stuffed(ip, es->es_el);
  714. error = gfs2_meta_inode_buffer(ip, &dibh);
  715. if (error)
  716. goto out;
  717. if (er->er_flags & GFS2_ERF_MODE) {
  718. gfs2_assert_withdraw(ip->i_sbd,
  719. (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
  720. ip->i_di.di_mode = er->er_mode;
  721. }
  722. ip->i_di.di_ctime = get_seconds();
  723. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  724. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  725. brelse(dibh);
  726. out:
  727. gfs2_trans_end(ip->i_sbd);
  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(ip->i_sbd, es->es_er, &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. ip->i_sbd->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 = ip->i_sbd;
  789. struct buffer_head *indbh, *newbh;
  790. uint64_t *eablk;
  791. int error;
  792. int mh_size = sizeof(struct gfs2_meta_header);
  793. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  794. uint64_t *end;
  795. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  796. DIO_START | DIO_WAIT, &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 = (uint64_t *)(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. uint64_t blk;
  815. blk = gfs2_alloc_meta(ip);
  816. indbh = gfs2_meta_new(ip->i_gl, blk);
  817. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  818. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  819. gfs2_buffer_clear_tail(indbh, mh_size);
  820. eablk = (uint64_t *)(indbh->b_data + mh_size);
  821. *eablk = cpu_to_be64(ip->i_di.di_eattr);
  822. ip->i_di.di_eattr = blk;
  823. ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
  824. ip->i_di.di_blocks++;
  825. eablk++;
  826. }
  827. error = ea_alloc_blk(ip, &newbh);
  828. if (error)
  829. goto out;
  830. *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
  831. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  832. brelse(newbh);
  833. if (error)
  834. goto out;
  835. if (private)
  836. ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
  837. out:
  838. brelse(indbh);
  839. return error;
  840. }
  841. static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  842. struct gfs2_ea_location *el)
  843. {
  844. struct ea_set es;
  845. unsigned int blks = 2;
  846. int error;
  847. memset(&es, 0, sizeof(struct ea_set));
  848. es.es_er = er;
  849. es.es_el = el;
  850. error = ea_foreach(ip, ea_set_simple, &es);
  851. if (error > 0)
  852. return 0;
  853. if (error)
  854. return error;
  855. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
  856. blks++;
  857. if (GFS2_EAREQ_SIZE_STUFFED(er) > ip->i_sbd->sd_jbsize)
  858. blks += DIV_ROUND_UP(er->er_data_len, ip->i_sbd->sd_jbsize);
  859. return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
  860. }
  861. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  862. struct gfs2_ea_location *el)
  863. {
  864. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  865. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  866. gfs2_assert_withdraw(ip->i_sbd,
  867. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  868. }
  869. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
  870. }
  871. int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  872. {
  873. struct gfs2_ea_location el;
  874. int error;
  875. if (!ip->i_di.di_eattr) {
  876. if (er->er_flags & XATTR_REPLACE)
  877. return -ENODATA;
  878. return ea_init(ip, er);
  879. }
  880. error = gfs2_ea_find(ip, er, &el);
  881. if (error)
  882. return error;
  883. if (el.el_ea) {
  884. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
  885. brelse(el.el_bh);
  886. return -EPERM;
  887. }
  888. error = -EEXIST;
  889. if (!(er->er_flags & XATTR_CREATE)) {
  890. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  891. error = ea_set_i(ip, er, &el);
  892. if (!error && unstuffed)
  893. ea_set_remove_unstuffed(ip, &el);
  894. }
  895. brelse(el.el_bh);
  896. } else {
  897. error = -ENODATA;
  898. if (!(er->er_flags & XATTR_REPLACE))
  899. error = ea_set_i(ip, er, NULL);
  900. }
  901. return error;
  902. }
  903. int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  904. {
  905. struct gfs2_holder i_gh;
  906. int error;
  907. if (!er->er_name_len ||
  908. er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  909. return -EINVAL;
  910. if (!er->er_data || !er->er_data_len) {
  911. er->er_data = NULL;
  912. er->er_data_len = 0;
  913. }
  914. error = ea_check_size(ip->i_sbd, er);
  915. if (error)
  916. return error;
  917. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  918. if (error)
  919. return error;
  920. if (IS_IMMUTABLE(ip->i_vnode))
  921. error = -EPERM;
  922. else
  923. error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
  924. gfs2_glock_dq_uninit(&i_gh);
  925. return error;
  926. }
  927. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  928. {
  929. struct gfs2_ea_header *ea = el->el_ea;
  930. struct gfs2_ea_header *prev = el->el_prev;
  931. struct buffer_head *dibh;
  932. int error;
  933. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
  934. if (error)
  935. return error;
  936. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  937. if (prev) {
  938. uint32_t len;
  939. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  940. prev->ea_rec_len = cpu_to_be32(len);
  941. if (GFS2_EA_IS_LAST(ea))
  942. prev->ea_flags |= GFS2_EAFLAG_LAST;
  943. } else
  944. ea->ea_type = GFS2_EATYPE_UNUSED;
  945. error = gfs2_meta_inode_buffer(ip, &dibh);
  946. if (!error) {
  947. ip->i_di.di_ctime = get_seconds();
  948. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  949. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  950. brelse(dibh);
  951. }
  952. gfs2_trans_end(ip->i_sbd);
  953. return error;
  954. }
  955. int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  956. {
  957. struct gfs2_ea_location el;
  958. int error;
  959. if (!ip->i_di.di_eattr)
  960. return -ENODATA;
  961. error = gfs2_ea_find(ip, er, &el);
  962. if (error)
  963. return error;
  964. if (!el.el_ea)
  965. return -ENODATA;
  966. if (GFS2_EA_IS_STUFFED(el.el_ea))
  967. error = ea_remove_stuffed(ip, &el);
  968. else
  969. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
  970. 0);
  971. brelse(el.el_bh);
  972. return error;
  973. }
  974. /**
  975. * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
  976. * @ip: pointer to the inode of the target file
  977. * @er: request information
  978. *
  979. * Returns: errno
  980. */
  981. int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  982. {
  983. struct gfs2_holder i_gh;
  984. int error;
  985. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  986. return -EINVAL;
  987. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  988. if (error)
  989. return error;
  990. if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
  991. error = -EPERM;
  992. else
  993. error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
  994. gfs2_glock_dq_uninit(&i_gh);
  995. return error;
  996. }
  997. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  998. struct gfs2_ea_header *ea, char *data)
  999. {
  1000. struct gfs2_sbd *sdp = ip->i_sbd;
  1001. struct buffer_head **bh;
  1002. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  1003. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  1004. uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
  1005. unsigned int x;
  1006. int error;
  1007. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  1008. if (!bh)
  1009. return -ENOMEM;
  1010. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  1011. if (error)
  1012. goto out;
  1013. for (x = 0; x < nptrs; x++) {
  1014. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
  1015. DIO_START, bh + x);
  1016. if (error) {
  1017. while (x--)
  1018. brelse(bh[x]);
  1019. goto fail;
  1020. }
  1021. dataptrs++;
  1022. }
  1023. for (x = 0; x < nptrs; x++) {
  1024. error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
  1025. if (error) {
  1026. for (; x < nptrs; x++)
  1027. brelse(bh[x]);
  1028. goto fail;
  1029. }
  1030. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  1031. for (; x < nptrs; x++)
  1032. brelse(bh[x]);
  1033. error = -EIO;
  1034. goto fail;
  1035. }
  1036. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1037. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
  1038. data,
  1039. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1040. amount -= sdp->sd_jbsize;
  1041. data += sdp->sd_jbsize;
  1042. brelse(bh[x]);
  1043. }
  1044. out:
  1045. kfree(bh);
  1046. return error;
  1047. fail:
  1048. gfs2_trans_end(sdp);
  1049. kfree(bh);
  1050. return error;
  1051. }
  1052. int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  1053. struct iattr *attr, char *data)
  1054. {
  1055. struct buffer_head *dibh;
  1056. int error;
  1057. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  1058. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
  1059. if (error)
  1060. return error;
  1061. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  1062. memcpy(GFS2_EA2DATA(el->el_ea),
  1063. data,
  1064. GFS2_EA_DATA_LEN(el->el_ea));
  1065. } else
  1066. error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
  1067. if (error)
  1068. return error;
  1069. error = gfs2_meta_inode_buffer(ip, &dibh);
  1070. if (!error) {
  1071. error = inode_setattr(ip->i_vnode, attr);
  1072. gfs2_assert_warn(ip->i_sbd, !error);
  1073. gfs2_inode_attr_out(ip);
  1074. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1075. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1076. brelse(dibh);
  1077. }
  1078. gfs2_trans_end(ip->i_sbd);
  1079. return error;
  1080. }
  1081. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1082. {
  1083. struct gfs2_sbd *sdp = ip->i_sbd;
  1084. struct gfs2_rgrp_list rlist;
  1085. struct buffer_head *indbh, *dibh;
  1086. uint64_t *eablk, *end;
  1087. unsigned int rg_blocks = 0;
  1088. uint64_t bstart = 0;
  1089. unsigned int blen = 0;
  1090. unsigned int blks = 0;
  1091. unsigned int x;
  1092. int error;
  1093. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1094. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  1095. DIO_START | DIO_WAIT, &indbh);
  1096. if (error)
  1097. return error;
  1098. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  1099. error = -EIO;
  1100. goto out;
  1101. }
  1102. eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1103. end = eablk + sdp->sd_inptrs;
  1104. for (; eablk < end; eablk++) {
  1105. uint64_t bn;
  1106. if (!*eablk)
  1107. break;
  1108. bn = be64_to_cpu(*eablk);
  1109. if (bstart + blen == bn)
  1110. blen++;
  1111. else {
  1112. if (bstart)
  1113. gfs2_rlist_add(sdp, &rlist, bstart);
  1114. bstart = bn;
  1115. blen = 1;
  1116. }
  1117. blks++;
  1118. }
  1119. if (bstart)
  1120. gfs2_rlist_add(sdp, &rlist, bstart);
  1121. else
  1122. goto out;
  1123. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  1124. for (x = 0; x < rlist.rl_rgrps; x++) {
  1125. struct gfs2_rgrpd *rgd;
  1126. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1127. rg_blocks += rgd->rd_ri.ri_length;
  1128. }
  1129. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1130. if (error)
  1131. goto out_rlist_free;
  1132. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  1133. RES_INDIRECT + RES_STATFS +
  1134. RES_QUOTA, blks);
  1135. if (error)
  1136. goto out_gunlock;
  1137. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  1138. eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
  1139. bstart = 0;
  1140. blen = 0;
  1141. for (; eablk < end; eablk++) {
  1142. uint64_t 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. if (!ip->i_di.di_blocks)
  1156. gfs2_consist_inode(ip);
  1157. ip->i_di.di_blocks--;
  1158. }
  1159. if (bstart)
  1160. gfs2_free_meta(ip, bstart, blen);
  1161. ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
  1162. error = gfs2_meta_inode_buffer(ip, &dibh);
  1163. if (!error) {
  1164. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1165. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1166. brelse(dibh);
  1167. }
  1168. gfs2_trans_end(sdp);
  1169. out_gunlock:
  1170. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1171. out_rlist_free:
  1172. gfs2_rlist_free(&rlist);
  1173. out:
  1174. brelse(indbh);
  1175. return error;
  1176. }
  1177. static int ea_dealloc_block(struct gfs2_inode *ip)
  1178. {
  1179. struct gfs2_sbd *sdp = ip->i_sbd;
  1180. struct gfs2_alloc *al = &ip->i_alloc;
  1181. struct gfs2_rgrpd *rgd;
  1182. struct buffer_head *dibh;
  1183. int error;
  1184. rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
  1185. if (!rgd) {
  1186. gfs2_consist_inode(ip);
  1187. return -EIO;
  1188. }
  1189. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  1190. &al->al_rgd_gh);
  1191. if (error)
  1192. return error;
  1193. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
  1194. RES_STATFS + RES_QUOTA, 1);
  1195. if (error)
  1196. goto out_gunlock;
  1197. gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
  1198. ip->i_di.di_eattr = 0;
  1199. if (!ip->i_di.di_blocks)
  1200. gfs2_consist_inode(ip);
  1201. ip->i_di.di_blocks--;
  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->i_di, 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. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1225. if (error)
  1226. goto out_alloc;
  1227. error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
  1228. if (error)
  1229. goto out_quota;
  1230. error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
  1231. if (error)
  1232. goto out_rindex;
  1233. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  1234. error = ea_dealloc_indirect(ip);
  1235. if (error)
  1236. goto out_rindex;
  1237. }
  1238. error = ea_dealloc_block(ip);
  1239. out_rindex:
  1240. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1241. out_quota:
  1242. gfs2_quota_unhold(ip);
  1243. out_alloc:
  1244. gfs2_alloc_put(ip);
  1245. return error;
  1246. }