eattr.c 31 KB

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