eattr.c 32 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 <asm/uaccess.h>
  17. #include "gfs2.h"
  18. #include "lm_interface.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. u64 *eablk, *end;
  96. int error;
  97. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_START | 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 = (u64 *)(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_START | 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. u64 *dataptrs, bn = 0;
  189. u64 bstart = 0;
  190. unsigned int blen = 0;
  191. unsigned int blks = 0;
  192. unsigned int x;
  193. int error;
  194. if (GFS2_EA_IS_STUFFED(ea))
  195. return 0;
  196. dataptrs = GFS2_EA2DATAPTRS(ea);
  197. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  198. if (*dataptrs) {
  199. blks++;
  200. bn = be64_to_cpu(*dataptrs);
  201. }
  202. }
  203. if (!blks)
  204. return 0;
  205. rgd = gfs2_blk2rgrpd(sdp, bn);
  206. if (!rgd) {
  207. gfs2_consist_inode(ip);
  208. return -EIO;
  209. }
  210. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
  211. if (error)
  212. return error;
  213. error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length + RES_DINODE +
  214. RES_EATTR + RES_STATFS + RES_QUOTA, blks);
  215. if (error)
  216. goto out_gunlock;
  217. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  218. dataptrs = GFS2_EA2DATAPTRS(ea);
  219. for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
  220. if (!*dataptrs)
  221. break;
  222. bn = be64_to_cpu(*dataptrs);
  223. if (bstart + blen == bn)
  224. blen++;
  225. else {
  226. if (bstart)
  227. gfs2_free_meta(ip, bstart, blen);
  228. bstart = bn;
  229. blen = 1;
  230. }
  231. *dataptrs = 0;
  232. if (!ip->i_di.di_blocks)
  233. gfs2_consist_inode(ip);
  234. ip->i_di.di_blocks--;
  235. }
  236. if (bstart)
  237. gfs2_free_meta(ip, bstart, blen);
  238. if (prev && !leave) {
  239. u32 len;
  240. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  241. prev->ea_rec_len = cpu_to_be32(len);
  242. if (GFS2_EA_IS_LAST(ea))
  243. prev->ea_flags |= GFS2_EAFLAG_LAST;
  244. } else {
  245. ea->ea_type = GFS2_EATYPE_UNUSED;
  246. ea->ea_num_ptrs = 0;
  247. }
  248. error = gfs2_meta_inode_buffer(ip, &dibh);
  249. if (!error) {
  250. ip->i_di.di_ctime = get_seconds();
  251. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  252. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  253. brelse(dibh);
  254. }
  255. gfs2_trans_end(sdp);
  256. out_gunlock:
  257. gfs2_glock_dq_uninit(&rg_gh);
  258. return error;
  259. }
  260. static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
  261. struct gfs2_ea_header *ea,
  262. struct gfs2_ea_header *prev, int leave)
  263. {
  264. struct gfs2_alloc *al;
  265. int error;
  266. al = gfs2_alloc_get(ip);
  267. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  268. if (error)
  269. goto out_alloc;
  270. error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
  271. if (error)
  272. goto out_quota;
  273. error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
  274. gfs2_glock_dq_uninit(&al->al_ri_gh);
  275. out_quota:
  276. gfs2_quota_unhold(ip);
  277. out_alloc:
  278. gfs2_alloc_put(ip);
  279. return error;
  280. }
  281. struct ea_list {
  282. struct gfs2_ea_request *ei_er;
  283. unsigned int ei_size;
  284. };
  285. static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
  286. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  287. void *private)
  288. {
  289. struct ea_list *ei = private;
  290. struct gfs2_ea_request *er = ei->ei_er;
  291. unsigned int ea_size = gfs2_ea_strlen(ea);
  292. if (ea->ea_type == GFS2_EATYPE_UNUSED)
  293. return 0;
  294. if (er->er_data_len) {
  295. char *prefix = NULL;
  296. unsigned int l = 0;
  297. char c = 0;
  298. if (ei->ei_size + ea_size > er->er_data_len)
  299. return -ERANGE;
  300. switch (ea->ea_type) {
  301. case GFS2_EATYPE_USR:
  302. prefix = "user.";
  303. l = 5;
  304. break;
  305. case GFS2_EATYPE_SYS:
  306. prefix = "system.";
  307. l = 7;
  308. break;
  309. case GFS2_EATYPE_SECURITY:
  310. prefix = "security.";
  311. l = 9;
  312. break;
  313. }
  314. BUG_ON(l == 0);
  315. memcpy(er->er_data + ei->ei_size, prefix, l);
  316. memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
  317. ea->ea_name_len);
  318. memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
  319. }
  320. ei->ei_size += ea_size;
  321. return 0;
  322. }
  323. /**
  324. * gfs2_ea_list -
  325. * @ip:
  326. * @er:
  327. *
  328. * Returns: actual size of data on success, -errno on error
  329. */
  330. int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  331. {
  332. struct gfs2_holder i_gh;
  333. int error;
  334. if (!er->er_data || !er->er_data_len) {
  335. er->er_data = NULL;
  336. er->er_data_len = 0;
  337. }
  338. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  339. if (error)
  340. return error;
  341. if (ip->i_di.di_eattr) {
  342. struct ea_list ei = { .ei_er = er, .ei_size = 0 };
  343. error = ea_foreach(ip, ea_list_i, &ei);
  344. if (!error)
  345. error = ei.ei_size;
  346. }
  347. gfs2_glock_dq_uninit(&i_gh);
  348. return error;
  349. }
  350. /**
  351. * ea_get_unstuffed - actually copies the unstuffed data into the
  352. * request buffer
  353. * @ip: The GFS2 inode
  354. * @ea: The extended attribute header structure
  355. * @data: The data to be copied
  356. *
  357. * Returns: errno
  358. */
  359. static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  360. char *data)
  361. {
  362. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  363. struct buffer_head **bh;
  364. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  365. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  366. u64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  367. unsigned int x;
  368. int error = 0;
  369. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  370. if (!bh)
  371. return -ENOMEM;
  372. for (x = 0; x < nptrs; x++) {
  373. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
  374. DIO_START, bh + x);
  375. if (error) {
  376. while (x--)
  377. brelse(bh[x]);
  378. goto out;
  379. }
  380. dataptrs++;
  381. }
  382. for (x = 0; x < nptrs; x++) {
  383. error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
  384. if (error) {
  385. for (; x < nptrs; x++)
  386. brelse(bh[x]);
  387. goto out;
  388. }
  389. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  390. for (; x < nptrs; x++)
  391. brelse(bh[x]);
  392. error = -EIO;
  393. goto out;
  394. }
  395. memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
  396. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  397. amount -= sdp->sd_jbsize;
  398. data += sdp->sd_jbsize;
  399. brelse(bh[x]);
  400. }
  401. out:
  402. kfree(bh);
  403. return error;
  404. }
  405. int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  406. char *data)
  407. {
  408. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  409. memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
  410. return 0;
  411. } else
  412. return ea_get_unstuffed(ip, el->el_ea, data);
  413. }
  414. /**
  415. * gfs2_ea_get_i -
  416. * @ip: The GFS2 inode
  417. * @er: The request structure
  418. *
  419. * Returns: actual size of data on success, -errno on error
  420. */
  421. int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  422. {
  423. struct gfs2_ea_location el;
  424. int error;
  425. if (!ip->i_di.di_eattr)
  426. return -ENODATA;
  427. error = gfs2_ea_find(ip, er, &el);
  428. if (error)
  429. return error;
  430. if (!el.el_ea)
  431. return -ENODATA;
  432. if (er->er_data_len) {
  433. if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
  434. error = -ERANGE;
  435. else
  436. error = gfs2_ea_get_copy(ip, &el, er->er_data);
  437. }
  438. if (!error)
  439. error = GFS2_EA_DATA_LEN(el.el_ea);
  440. brelse(el.el_bh);
  441. return error;
  442. }
  443. /**
  444. * gfs2_ea_get -
  445. * @ip: The GFS2 inode
  446. * @er: The request structure
  447. *
  448. * Returns: actual size of data on success, -errno on error
  449. */
  450. int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  451. {
  452. struct gfs2_holder i_gh;
  453. int error;
  454. if (!er->er_name_len ||
  455. er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  456. return -EINVAL;
  457. if (!er->er_data || !er->er_data_len) {
  458. er->er_data = NULL;
  459. er->er_data_len = 0;
  460. }
  461. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  462. if (error)
  463. return error;
  464. error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
  465. gfs2_glock_dq_uninit(&i_gh);
  466. return error;
  467. }
  468. /**
  469. * ea_alloc_blk - allocates a new block for extended attributes.
  470. * @ip: A pointer to the inode that's getting extended attributes
  471. * @bhp: Pointer to pointer to a struct buffer_head
  472. *
  473. * Returns: errno
  474. */
  475. static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
  476. {
  477. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  478. struct gfs2_ea_header *ea;
  479. u64 block;
  480. block = gfs2_alloc_meta(ip);
  481. *bhp = gfs2_meta_new(ip->i_gl, block);
  482. gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
  483. gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  484. gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
  485. ea = GFS2_EA_BH2FIRST(*bhp);
  486. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  487. ea->ea_type = GFS2_EATYPE_UNUSED;
  488. ea->ea_flags = GFS2_EAFLAG_LAST;
  489. ea->ea_num_ptrs = 0;
  490. ip->i_di.di_blocks++;
  491. return 0;
  492. }
  493. /**
  494. * ea_write - writes the request info to an ea, creating new blocks if
  495. * necessary
  496. * @ip: inode that is being modified
  497. * @ea: the location of the new ea in a block
  498. * @er: the write request
  499. *
  500. * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
  501. *
  502. * returns : errno
  503. */
  504. static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
  505. struct gfs2_ea_request *er)
  506. {
  507. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  508. ea->ea_data_len = cpu_to_be32(er->er_data_len);
  509. ea->ea_name_len = er->er_name_len;
  510. ea->ea_type = er->er_type;
  511. ea->__pad = 0;
  512. memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
  513. if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
  514. ea->ea_num_ptrs = 0;
  515. memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
  516. } else {
  517. u64 *dataptr = GFS2_EA2DATAPTRS(ea);
  518. const char *data = er->er_data;
  519. unsigned int data_len = er->er_data_len;
  520. unsigned int copy;
  521. unsigned int x;
  522. ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
  523. for (x = 0; x < ea->ea_num_ptrs; x++) {
  524. struct buffer_head *bh;
  525. u64 block;
  526. int mh_size = sizeof(struct gfs2_meta_header);
  527. block = gfs2_alloc_meta(ip);
  528. bh = gfs2_meta_new(ip->i_gl, block);
  529. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  530. gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
  531. ip->i_di.di_blocks++;
  532. copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
  533. data_len;
  534. memcpy(bh->b_data + mh_size, data, copy);
  535. if (copy < sdp->sd_jbsize)
  536. memset(bh->b_data + mh_size + copy, 0,
  537. sdp->sd_jbsize - copy);
  538. *dataptr++ = cpu_to_be64(bh->b_blocknr);
  539. data += copy;
  540. data_len -= copy;
  541. brelse(bh);
  542. }
  543. gfs2_assert_withdraw(sdp, !data_len);
  544. }
  545. return 0;
  546. }
  547. typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
  548. struct gfs2_ea_request *er, void *private);
  549. static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  550. unsigned int blks,
  551. ea_skeleton_call_t skeleton_call, void *private)
  552. {
  553. struct gfs2_alloc *al;
  554. struct buffer_head *dibh;
  555. int error;
  556. al = gfs2_alloc_get(ip);
  557. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  558. if (error)
  559. goto out;
  560. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  561. if (error)
  562. goto out_gunlock_q;
  563. al->al_requested = blks;
  564. error = gfs2_inplace_reserve(ip);
  565. if (error)
  566. goto out_gunlock_q;
  567. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
  568. blks + al->al_rgd->rd_ri.ri_length +
  569. RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  570. if (error)
  571. goto out_ipres;
  572. error = skeleton_call(ip, er, private);
  573. if (error)
  574. goto out_end_trans;
  575. error = gfs2_meta_inode_buffer(ip, &dibh);
  576. if (!error) {
  577. if (er->er_flags & GFS2_ERF_MODE) {
  578. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  579. (ip->i_di.di_mode & S_IFMT) ==
  580. (er->er_mode & S_IFMT));
  581. ip->i_di.di_mode = er->er_mode;
  582. }
  583. ip->i_di.di_ctime = get_seconds();
  584. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  585. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  586. brelse(dibh);
  587. }
  588. out_end_trans:
  589. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  590. out_ipres:
  591. gfs2_inplace_release(ip);
  592. out_gunlock_q:
  593. gfs2_quota_unlock(ip);
  594. out:
  595. gfs2_alloc_put(ip);
  596. return error;
  597. }
  598. static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  599. void *private)
  600. {
  601. struct buffer_head *bh;
  602. int error;
  603. error = ea_alloc_blk(ip, &bh);
  604. if (error)
  605. return error;
  606. ip->i_di.di_eattr = bh->b_blocknr;
  607. error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
  608. brelse(bh);
  609. return error;
  610. }
  611. /**
  612. * ea_init - initializes a new eattr block
  613. * @ip:
  614. * @er:
  615. *
  616. * Returns: errno
  617. */
  618. static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  619. {
  620. unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
  621. unsigned int blks = 1;
  622. if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
  623. blks += DIV_ROUND_UP(er->er_data_len, jbsize);
  624. return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
  625. }
  626. static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
  627. {
  628. u32 ea_size = GFS2_EA_SIZE(ea);
  629. struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
  630. ea_size);
  631. u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
  632. int last = ea->ea_flags & GFS2_EAFLAG_LAST;
  633. ea->ea_rec_len = cpu_to_be32(ea_size);
  634. ea->ea_flags ^= last;
  635. new->ea_rec_len = cpu_to_be32(new_size);
  636. new->ea_flags = last;
  637. return new;
  638. }
  639. static void ea_set_remove_stuffed(struct gfs2_inode *ip,
  640. struct gfs2_ea_location *el)
  641. {
  642. struct gfs2_ea_header *ea = el->el_ea;
  643. struct gfs2_ea_header *prev = el->el_prev;
  644. u32 len;
  645. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  646. if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
  647. ea->ea_type = GFS2_EATYPE_UNUSED;
  648. return;
  649. } else if (GFS2_EA2NEXT(prev) != ea) {
  650. prev = GFS2_EA2NEXT(prev);
  651. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
  652. }
  653. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  654. prev->ea_rec_len = cpu_to_be32(len);
  655. if (GFS2_EA_IS_LAST(ea))
  656. prev->ea_flags |= GFS2_EAFLAG_LAST;
  657. }
  658. struct ea_set {
  659. int ea_split;
  660. struct gfs2_ea_request *es_er;
  661. struct gfs2_ea_location *es_el;
  662. struct buffer_head *es_bh;
  663. struct gfs2_ea_header *es_ea;
  664. };
  665. static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
  666. struct gfs2_ea_header *ea, struct ea_set *es)
  667. {
  668. struct gfs2_ea_request *er = es->es_er;
  669. struct buffer_head *dibh;
  670. int error;
  671. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
  672. if (error)
  673. return error;
  674. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  675. if (es->ea_split)
  676. ea = ea_split_ea(ea);
  677. ea_write(ip, ea, er);
  678. if (es->es_el)
  679. ea_set_remove_stuffed(ip, es->es_el);
  680. error = gfs2_meta_inode_buffer(ip, &dibh);
  681. if (error)
  682. goto out;
  683. if (er->er_flags & GFS2_ERF_MODE) {
  684. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  685. (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
  686. ip->i_di.di_mode = er->er_mode;
  687. }
  688. ip->i_di.di_ctime = get_seconds();
  689. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  690. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  691. brelse(dibh);
  692. out:
  693. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  694. return error;
  695. }
  696. static int ea_set_simple_alloc(struct gfs2_inode *ip,
  697. struct gfs2_ea_request *er, void *private)
  698. {
  699. struct ea_set *es = private;
  700. struct gfs2_ea_header *ea = es->es_ea;
  701. int error;
  702. gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
  703. if (es->ea_split)
  704. ea = ea_split_ea(ea);
  705. error = ea_write(ip, ea, er);
  706. if (error)
  707. return error;
  708. if (es->es_el)
  709. ea_set_remove_stuffed(ip, es->es_el);
  710. return 0;
  711. }
  712. static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
  713. struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
  714. void *private)
  715. {
  716. struct ea_set *es = private;
  717. unsigned int size;
  718. int stuffed;
  719. int error;
  720. stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
  721. if (ea->ea_type == GFS2_EATYPE_UNUSED) {
  722. if (GFS2_EA_REC_LEN(ea) < size)
  723. return 0;
  724. if (!GFS2_EA_IS_STUFFED(ea)) {
  725. error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
  726. if (error)
  727. return error;
  728. }
  729. es->ea_split = 0;
  730. } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
  731. es->ea_split = 1;
  732. else
  733. return 0;
  734. if (stuffed) {
  735. error = ea_set_simple_noalloc(ip, bh, ea, es);
  736. if (error)
  737. return error;
  738. } else {
  739. unsigned int blks;
  740. es->es_bh = bh;
  741. es->es_ea = ea;
  742. blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
  743. GFS2_SB(&ip->i_inode)->sd_jbsize);
  744. error = ea_alloc_skeleton(ip, es->es_er, blks,
  745. ea_set_simple_alloc, es);
  746. if (error)
  747. return error;
  748. }
  749. return 1;
  750. }
  751. static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  752. void *private)
  753. {
  754. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  755. struct buffer_head *indbh, *newbh;
  756. u64 *eablk;
  757. int error;
  758. int mh_size = sizeof(struct gfs2_meta_header);
  759. if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
  760. u64 *end;
  761. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  762. DIO_START | DIO_WAIT, &indbh);
  763. if (error)
  764. return error;
  765. if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
  766. error = -EIO;
  767. goto out;
  768. }
  769. eablk = (u64 *)(indbh->b_data + mh_size);
  770. end = eablk + sdp->sd_inptrs;
  771. for (; eablk < end; eablk++)
  772. if (!*eablk)
  773. break;
  774. if (eablk == end) {
  775. error = -ENOSPC;
  776. goto out;
  777. }
  778. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  779. } else {
  780. u64 blk;
  781. blk = gfs2_alloc_meta(ip);
  782. indbh = gfs2_meta_new(ip->i_gl, blk);
  783. gfs2_trans_add_bh(ip->i_gl, indbh, 1);
  784. gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  785. gfs2_buffer_clear_tail(indbh, mh_size);
  786. eablk = (u64 *)(indbh->b_data + mh_size);
  787. *eablk = cpu_to_be64(ip->i_di.di_eattr);
  788. ip->i_di.di_eattr = blk;
  789. ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
  790. ip->i_di.di_blocks++;
  791. eablk++;
  792. }
  793. error = ea_alloc_blk(ip, &newbh);
  794. if (error)
  795. goto out;
  796. *eablk = cpu_to_be64((u64)newbh->b_blocknr);
  797. error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
  798. brelse(newbh);
  799. if (error)
  800. goto out;
  801. if (private)
  802. ea_set_remove_stuffed(ip, private);
  803. out:
  804. brelse(indbh);
  805. return error;
  806. }
  807. static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
  808. struct gfs2_ea_location *el)
  809. {
  810. struct ea_set es;
  811. unsigned int blks = 2;
  812. int error;
  813. memset(&es, 0, sizeof(struct ea_set));
  814. es.es_er = er;
  815. es.es_el = el;
  816. error = ea_foreach(ip, ea_set_simple, &es);
  817. if (error > 0)
  818. return 0;
  819. if (error)
  820. return error;
  821. if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
  822. blks++;
  823. if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
  824. blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
  825. return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
  826. }
  827. static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
  828. struct gfs2_ea_location *el)
  829. {
  830. if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
  831. el->el_prev = GFS2_EA2NEXT(el->el_prev);
  832. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
  833. GFS2_EA2NEXT(el->el_prev) == el->el_ea);
  834. }
  835. return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
  836. }
  837. int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  838. {
  839. struct gfs2_ea_location el;
  840. int error;
  841. if (!ip->i_di.di_eattr) {
  842. if (er->er_flags & XATTR_REPLACE)
  843. return -ENODATA;
  844. return ea_init(ip, er);
  845. }
  846. error = gfs2_ea_find(ip, er, &el);
  847. if (error)
  848. return error;
  849. if (el.el_ea) {
  850. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
  851. brelse(el.el_bh);
  852. return -EPERM;
  853. }
  854. error = -EEXIST;
  855. if (!(er->er_flags & XATTR_CREATE)) {
  856. int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
  857. error = ea_set_i(ip, er, &el);
  858. if (!error && unstuffed)
  859. ea_set_remove_unstuffed(ip, &el);
  860. }
  861. brelse(el.el_bh);
  862. } else {
  863. error = -ENODATA;
  864. if (!(er->er_flags & XATTR_REPLACE))
  865. error = ea_set_i(ip, er, NULL);
  866. }
  867. return error;
  868. }
  869. int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  870. {
  871. struct gfs2_holder i_gh;
  872. int error;
  873. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  874. return -EINVAL;
  875. if (!er->er_data || !er->er_data_len) {
  876. er->er_data = NULL;
  877. er->er_data_len = 0;
  878. }
  879. error = ea_check_size(GFS2_SB(&ip->i_inode), er);
  880. if (error)
  881. return error;
  882. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  883. if (error)
  884. return error;
  885. if (IS_IMMUTABLE(&ip->i_inode))
  886. error = -EPERM;
  887. else
  888. error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
  889. gfs2_glock_dq_uninit(&i_gh);
  890. return error;
  891. }
  892. static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
  893. {
  894. struct gfs2_ea_header *ea = el->el_ea;
  895. struct gfs2_ea_header *prev = el->el_prev;
  896. struct buffer_head *dibh;
  897. int error;
  898. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  899. if (error)
  900. return error;
  901. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  902. if (prev) {
  903. u32 len;
  904. len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
  905. prev->ea_rec_len = cpu_to_be32(len);
  906. if (GFS2_EA_IS_LAST(ea))
  907. prev->ea_flags |= GFS2_EAFLAG_LAST;
  908. } else
  909. ea->ea_type = GFS2_EATYPE_UNUSED;
  910. error = gfs2_meta_inode_buffer(ip, &dibh);
  911. if (!error) {
  912. ip->i_di.di_ctime = get_seconds();
  913. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  914. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  915. brelse(dibh);
  916. }
  917. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  918. return error;
  919. }
  920. int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  921. {
  922. struct gfs2_ea_location el;
  923. int error;
  924. if (!ip->i_di.di_eattr)
  925. return -ENODATA;
  926. error = gfs2_ea_find(ip, er, &el);
  927. if (error)
  928. return error;
  929. if (!el.el_ea)
  930. return -ENODATA;
  931. if (GFS2_EA_IS_STUFFED(el.el_ea))
  932. error = ea_remove_stuffed(ip, &el);
  933. else
  934. error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
  935. 0);
  936. brelse(el.el_bh);
  937. return error;
  938. }
  939. /**
  940. * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
  941. * @ip: pointer to the inode of the target file
  942. * @er: request information
  943. *
  944. * Returns: errno
  945. */
  946. int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  947. {
  948. struct gfs2_holder i_gh;
  949. int error;
  950. if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
  951. return -EINVAL;
  952. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  953. if (error)
  954. return error;
  955. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  956. error = -EPERM;
  957. else
  958. error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
  959. gfs2_glock_dq_uninit(&i_gh);
  960. return error;
  961. }
  962. static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
  963. struct gfs2_ea_header *ea, char *data)
  964. {
  965. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  966. struct buffer_head **bh;
  967. unsigned int amount = GFS2_EA_DATA_LEN(ea);
  968. unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
  969. u64 *dataptrs = GFS2_EA2DATAPTRS(ea);
  970. unsigned int x;
  971. int error;
  972. bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
  973. if (!bh)
  974. return -ENOMEM;
  975. error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
  976. if (error)
  977. goto out;
  978. for (x = 0; x < nptrs; x++) {
  979. error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
  980. DIO_START, bh + x);
  981. if (error) {
  982. while (x--)
  983. brelse(bh[x]);
  984. goto fail;
  985. }
  986. dataptrs++;
  987. }
  988. for (x = 0; x < nptrs; x++) {
  989. error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
  990. if (error) {
  991. for (; x < nptrs; x++)
  992. brelse(bh[x]);
  993. goto fail;
  994. }
  995. if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
  996. for (; x < nptrs; x++)
  997. brelse(bh[x]);
  998. error = -EIO;
  999. goto fail;
  1000. }
  1001. gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
  1002. memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
  1003. (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
  1004. amount -= sdp->sd_jbsize;
  1005. data += sdp->sd_jbsize;
  1006. brelse(bh[x]);
  1007. }
  1008. out:
  1009. kfree(bh);
  1010. return error;
  1011. fail:
  1012. gfs2_trans_end(sdp);
  1013. kfree(bh);
  1014. return error;
  1015. }
  1016. int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
  1017. struct iattr *attr, char *data)
  1018. {
  1019. struct buffer_head *dibh;
  1020. int error;
  1021. if (GFS2_EA_IS_STUFFED(el->el_ea)) {
  1022. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
  1023. if (error)
  1024. return error;
  1025. gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
  1026. memcpy(GFS2_EA2DATA(el->el_ea), data,
  1027. GFS2_EA_DATA_LEN(el->el_ea));
  1028. } else
  1029. error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
  1030. if (error)
  1031. return error;
  1032. error = gfs2_meta_inode_buffer(ip, &dibh);
  1033. if (!error) {
  1034. error = inode_setattr(&ip->i_inode, attr);
  1035. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1036. gfs2_inode_attr_out(ip);
  1037. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1038. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1039. brelse(dibh);
  1040. }
  1041. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1042. return error;
  1043. }
  1044. static int ea_dealloc_indirect(struct gfs2_inode *ip)
  1045. {
  1046. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1047. struct gfs2_rgrp_list rlist;
  1048. struct buffer_head *indbh, *dibh;
  1049. u64 *eablk, *end;
  1050. unsigned int rg_blocks = 0;
  1051. u64 bstart = 0;
  1052. unsigned int blen = 0;
  1053. unsigned int blks = 0;
  1054. unsigned int x;
  1055. int error;
  1056. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1057. error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
  1058. DIO_START | 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 = (u64 *)(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 = (u64 *)(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. }