eattr.c 32 KB

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