xfs_attr.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_da_format.h"
  16. #include "xfs_da_btree.h"
  17. #include "xfs_attr_sf.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_alloc.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_inode_item.h"
  22. #include "xfs_bmap.h"
  23. #include "xfs_bmap_util.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_attr.h"
  26. #include "xfs_attr_leaf.h"
  27. #include "xfs_attr_remote.h"
  28. #include "xfs_error.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_trans_space.h"
  31. #include "xfs_trace.h"
  32. /*
  33. * xfs_attr.c
  34. *
  35. * Provide the external interfaces to manage attribute lists.
  36. */
  37. /*========================================================================
  38. * Function prototypes for the kernel.
  39. *========================================================================*/
  40. /*
  41. * Internal routines when attribute list fits inside the inode.
  42. */
  43. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  44. /*
  45. * Internal routines when attribute list is one block.
  46. */
  47. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  48. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  49. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  50. /*
  51. * Internal routines when attribute list is more than one block.
  52. */
  53. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  54. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  55. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  56. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  57. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  58. STATIC int
  59. xfs_attr_args_init(
  60. struct xfs_da_args *args,
  61. struct xfs_inode *dp,
  62. const unsigned char *name,
  63. int flags)
  64. {
  65. if (!name)
  66. return -EINVAL;
  67. memset(args, 0, sizeof(*args));
  68. args->geo = dp->i_mount->m_attr_geo;
  69. args->whichfork = XFS_ATTR_FORK;
  70. args->dp = dp;
  71. args->flags = flags;
  72. args->name = name;
  73. args->namelen = strlen((const char *)name);
  74. if (args->namelen >= MAXNAMELEN)
  75. return -EFAULT; /* match IRIX behaviour */
  76. args->hashval = xfs_da_hashname(args->name, args->namelen);
  77. return 0;
  78. }
  79. int
  80. xfs_inode_hasattr(
  81. struct xfs_inode *ip)
  82. {
  83. if (!XFS_IFORK_Q(ip) ||
  84. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  85. ip->i_d.di_anextents == 0))
  86. return 0;
  87. return 1;
  88. }
  89. /*========================================================================
  90. * Overall external interface routines.
  91. *========================================================================*/
  92. /* Retrieve an extended attribute and its value. Must have ilock. */
  93. int
  94. xfs_attr_get_ilocked(
  95. struct xfs_inode *ip,
  96. struct xfs_da_args *args)
  97. {
  98. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  99. if (!xfs_inode_hasattr(ip))
  100. return -ENOATTR;
  101. else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
  102. return xfs_attr_shortform_getvalue(args);
  103. else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
  104. return xfs_attr_leaf_get(args);
  105. else
  106. return xfs_attr_node_get(args);
  107. }
  108. /* Retrieve an extended attribute by name, and its value. */
  109. int
  110. xfs_attr_get(
  111. struct xfs_inode *ip,
  112. const unsigned char *name,
  113. unsigned char *value,
  114. int *valuelenp,
  115. int flags)
  116. {
  117. struct xfs_da_args args;
  118. uint lock_mode;
  119. int error;
  120. XFS_STATS_INC(ip->i_mount, xs_attr_get);
  121. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  122. return -EIO;
  123. error = xfs_attr_args_init(&args, ip, name, flags);
  124. if (error)
  125. return error;
  126. args.value = value;
  127. args.valuelen = *valuelenp;
  128. /* Entirely possible to look up a name which doesn't exist */
  129. args.op_flags = XFS_DA_OP_OKNOENT;
  130. lock_mode = xfs_ilock_attr_map_shared(ip);
  131. error = xfs_attr_get_ilocked(ip, &args);
  132. xfs_iunlock(ip, lock_mode);
  133. *valuelenp = args.valuelen;
  134. return error == -EEXIST ? 0 : error;
  135. }
  136. /*
  137. * Calculate how many blocks we need for the new attribute,
  138. */
  139. STATIC int
  140. xfs_attr_calc_size(
  141. struct xfs_da_args *args,
  142. int *local)
  143. {
  144. struct xfs_mount *mp = args->dp->i_mount;
  145. int size;
  146. int nblks;
  147. /*
  148. * Determine space new attribute will use, and if it would be
  149. * "local" or "remote" (note: local != inline).
  150. */
  151. size = xfs_attr_leaf_newentsize(args, local);
  152. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  153. if (*local) {
  154. if (size > (args->geo->blksize / 2)) {
  155. /* Double split possible */
  156. nblks *= 2;
  157. }
  158. } else {
  159. /*
  160. * Out of line attribute, cannot double split, but
  161. * make room for the attribute value itself.
  162. */
  163. uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
  164. nblks += dblocks;
  165. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  166. }
  167. return nblks;
  168. }
  169. int
  170. xfs_attr_set(
  171. struct xfs_inode *dp,
  172. const unsigned char *name,
  173. unsigned char *value,
  174. int valuelen,
  175. int flags)
  176. {
  177. struct xfs_mount *mp = dp->i_mount;
  178. struct xfs_buf *leaf_bp = NULL;
  179. struct xfs_da_args args;
  180. struct xfs_defer_ops dfops;
  181. struct xfs_trans_res tres;
  182. xfs_fsblock_t firstblock;
  183. int rsvd = (flags & ATTR_ROOT) != 0;
  184. int error, err2, local;
  185. XFS_STATS_INC(mp, xs_attr_set);
  186. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  187. return -EIO;
  188. error = xfs_attr_args_init(&args, dp, name, flags);
  189. if (error)
  190. return error;
  191. args.value = value;
  192. args.valuelen = valuelen;
  193. args.firstblock = &firstblock;
  194. args.dfops = &dfops;
  195. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  196. args.total = xfs_attr_calc_size(&args, &local);
  197. error = xfs_qm_dqattach(dp);
  198. if (error)
  199. return error;
  200. /*
  201. * If the inode doesn't have an attribute fork, add one.
  202. * (inode must not be locked when we call this routine)
  203. */
  204. if (XFS_IFORK_Q(dp) == 0) {
  205. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  206. XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
  207. error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
  208. if (error)
  209. return error;
  210. }
  211. tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
  212. M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
  213. tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  214. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  215. /*
  216. * Root fork attributes can use reserved data blocks for this
  217. * operation if necessary
  218. */
  219. error = xfs_trans_alloc(mp, &tres, args.total, 0,
  220. rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
  221. if (error)
  222. return error;
  223. xfs_ilock(dp, XFS_ILOCK_EXCL);
  224. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  225. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  226. XFS_QMOPT_RES_REGBLKS);
  227. if (error) {
  228. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  229. xfs_trans_cancel(args.trans);
  230. return error;
  231. }
  232. xfs_trans_ijoin(args.trans, dp, 0);
  233. /*
  234. * If the attribute list is non-existent or a shortform list,
  235. * upgrade it to a single-leaf-block attribute list.
  236. */
  237. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
  238. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  239. dp->i_d.di_anextents == 0)) {
  240. /*
  241. * Build initial attribute list (if required).
  242. */
  243. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  244. xfs_attr_shortform_create(&args);
  245. /*
  246. * Try to add the attr to the attribute list in
  247. * the inode.
  248. */
  249. error = xfs_attr_shortform_addname(&args);
  250. if (error != -ENOSPC) {
  251. /*
  252. * Commit the shortform mods, and we're done.
  253. * NOTE: this is also the error path (EEXIST, etc).
  254. */
  255. ASSERT(args.trans != NULL);
  256. /*
  257. * If this is a synchronous mount, make sure that
  258. * the transaction goes to disk before returning
  259. * to the user.
  260. */
  261. if (mp->m_flags & XFS_MOUNT_WSYNC)
  262. xfs_trans_set_sync(args.trans);
  263. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  264. xfs_trans_ichgtime(args.trans, dp,
  265. XFS_ICHGTIME_CHG);
  266. }
  267. err2 = xfs_trans_commit(args.trans);
  268. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  269. return error ? error : err2;
  270. }
  271. /*
  272. * It won't fit in the shortform, transform to a leaf block.
  273. * GROT: another possible req'mt for a double-split btree op.
  274. */
  275. xfs_defer_init(args.dfops, args.firstblock);
  276. error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
  277. if (error)
  278. goto out_defer_cancel;
  279. /*
  280. * Prevent the leaf buffer from being unlocked so that a
  281. * concurrent AIL push cannot grab the half-baked leaf
  282. * buffer and run into problems with the write verifier.
  283. */
  284. xfs_trans_bhold(args.trans, leaf_bp);
  285. xfs_defer_bjoin(args.dfops, leaf_bp);
  286. xfs_defer_ijoin(args.dfops, dp);
  287. error = xfs_defer_finish(&args.trans, args.dfops);
  288. if (error)
  289. goto out_defer_cancel;
  290. /*
  291. * Commit the leaf transformation. We'll need another (linked)
  292. * transaction to add the new attribute to the leaf, which
  293. * means that we have to hold & join the leaf buffer here too.
  294. */
  295. error = xfs_trans_roll_inode(&args.trans, dp);
  296. if (error)
  297. goto out;
  298. xfs_trans_bjoin(args.trans, leaf_bp);
  299. leaf_bp = NULL;
  300. }
  301. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
  302. error = xfs_attr_leaf_addname(&args);
  303. else
  304. error = xfs_attr_node_addname(&args);
  305. if (error)
  306. goto out;
  307. /*
  308. * If this is a synchronous mount, make sure that the
  309. * transaction goes to disk before returning to the user.
  310. */
  311. if (mp->m_flags & XFS_MOUNT_WSYNC)
  312. xfs_trans_set_sync(args.trans);
  313. if ((flags & ATTR_KERNOTIME) == 0)
  314. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  315. /*
  316. * Commit the last in the sequence of transactions.
  317. */
  318. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  319. error = xfs_trans_commit(args.trans);
  320. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  321. return error;
  322. out_defer_cancel:
  323. xfs_defer_cancel(&dfops);
  324. out:
  325. if (leaf_bp)
  326. xfs_trans_brelse(args.trans, leaf_bp);
  327. if (args.trans)
  328. xfs_trans_cancel(args.trans);
  329. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  330. return error;
  331. }
  332. /*
  333. * Generic handler routine to remove a name from an attribute list.
  334. * Transitions attribute list from Btree to shortform as necessary.
  335. */
  336. int
  337. xfs_attr_remove(
  338. struct xfs_inode *dp,
  339. const unsigned char *name,
  340. int flags)
  341. {
  342. struct xfs_mount *mp = dp->i_mount;
  343. struct xfs_da_args args;
  344. struct xfs_defer_ops dfops;
  345. xfs_fsblock_t firstblock;
  346. int error;
  347. XFS_STATS_INC(mp, xs_attr_remove);
  348. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  349. return -EIO;
  350. error = xfs_attr_args_init(&args, dp, name, flags);
  351. if (error)
  352. return error;
  353. args.firstblock = &firstblock;
  354. args.dfops = &dfops;
  355. /*
  356. * we have no control over the attribute names that userspace passes us
  357. * to remove, so we have to allow the name lookup prior to attribute
  358. * removal to fail.
  359. */
  360. args.op_flags = XFS_DA_OP_OKNOENT;
  361. error = xfs_qm_dqattach(dp);
  362. if (error)
  363. return error;
  364. /*
  365. * Root fork attributes can use reserved data blocks for this
  366. * operation if necessary
  367. */
  368. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
  369. XFS_ATTRRM_SPACE_RES(mp), 0,
  370. (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
  371. &args.trans);
  372. if (error)
  373. return error;
  374. xfs_ilock(dp, XFS_ILOCK_EXCL);
  375. /*
  376. * No need to make quota reservations here. We expect to release some
  377. * blocks not allocate in the common case.
  378. */
  379. xfs_trans_ijoin(args.trans, dp, 0);
  380. if (!xfs_inode_hasattr(dp)) {
  381. error = -ENOATTR;
  382. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  383. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  384. error = xfs_attr_shortform_remove(&args);
  385. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  386. error = xfs_attr_leaf_removename(&args);
  387. } else {
  388. error = xfs_attr_node_removename(&args);
  389. }
  390. if (error)
  391. goto out;
  392. /*
  393. * If this is a synchronous mount, make sure that the
  394. * transaction goes to disk before returning to the user.
  395. */
  396. if (mp->m_flags & XFS_MOUNT_WSYNC)
  397. xfs_trans_set_sync(args.trans);
  398. if ((flags & ATTR_KERNOTIME) == 0)
  399. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  400. /*
  401. * Commit the last in the sequence of transactions.
  402. */
  403. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  404. error = xfs_trans_commit(args.trans);
  405. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  406. return error;
  407. out:
  408. if (args.trans)
  409. xfs_trans_cancel(args.trans);
  410. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  411. return error;
  412. }
  413. /*========================================================================
  414. * External routines when attribute list is inside the inode
  415. *========================================================================*/
  416. /*
  417. * Add a name to the shortform attribute list structure
  418. * This is the external routine.
  419. */
  420. STATIC int
  421. xfs_attr_shortform_addname(xfs_da_args_t *args)
  422. {
  423. int newsize, forkoff, retval;
  424. trace_xfs_attr_sf_addname(args);
  425. retval = xfs_attr_shortform_lookup(args);
  426. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  427. return retval;
  428. } else if (retval == -EEXIST) {
  429. if (args->flags & ATTR_CREATE)
  430. return retval;
  431. retval = xfs_attr_shortform_remove(args);
  432. if (retval)
  433. return retval;
  434. /*
  435. * Since we have removed the old attr, clear ATTR_REPLACE so
  436. * that the leaf format add routine won't trip over the attr
  437. * not being around.
  438. */
  439. args->flags &= ~ATTR_REPLACE;
  440. }
  441. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  442. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  443. return -ENOSPC;
  444. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  445. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  446. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  447. if (!forkoff)
  448. return -ENOSPC;
  449. xfs_attr_shortform_add(args, forkoff);
  450. return 0;
  451. }
  452. /*========================================================================
  453. * External routines when attribute list is one block
  454. *========================================================================*/
  455. /*
  456. * Add a name to the leaf attribute list structure
  457. *
  458. * This leaf block cannot have a "remote" value, we only call this routine
  459. * if bmap_one_block() says there is only one block (ie: no remote blks).
  460. */
  461. STATIC int
  462. xfs_attr_leaf_addname(xfs_da_args_t *args)
  463. {
  464. xfs_inode_t *dp;
  465. struct xfs_buf *bp;
  466. int retval, error, forkoff;
  467. trace_xfs_attr_leaf_addname(args);
  468. /*
  469. * Read the (only) block in the attribute list in.
  470. */
  471. dp = args->dp;
  472. args->blkno = 0;
  473. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  474. if (error)
  475. return error;
  476. /*
  477. * Look up the given attribute in the leaf block. Figure out if
  478. * the given flags produce an error or call for an atomic rename.
  479. */
  480. retval = xfs_attr3_leaf_lookup_int(bp, args);
  481. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  482. xfs_trans_brelse(args->trans, bp);
  483. return retval;
  484. } else if (retval == -EEXIST) {
  485. if (args->flags & ATTR_CREATE) { /* pure create op */
  486. xfs_trans_brelse(args->trans, bp);
  487. return retval;
  488. }
  489. trace_xfs_attr_leaf_replace(args);
  490. /* save the attribute state for later removal*/
  491. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  492. args->blkno2 = args->blkno; /* set 2nd entry info*/
  493. args->index2 = args->index;
  494. args->rmtblkno2 = args->rmtblkno;
  495. args->rmtblkcnt2 = args->rmtblkcnt;
  496. args->rmtvaluelen2 = args->rmtvaluelen;
  497. /*
  498. * clear the remote attr state now that it is saved so that the
  499. * values reflect the state of the attribute we are about to
  500. * add, not the attribute we just found and will remove later.
  501. */
  502. args->rmtblkno = 0;
  503. args->rmtblkcnt = 0;
  504. args->rmtvaluelen = 0;
  505. }
  506. /*
  507. * Add the attribute to the leaf block, transitioning to a Btree
  508. * if required.
  509. */
  510. retval = xfs_attr3_leaf_add(bp, args);
  511. if (retval == -ENOSPC) {
  512. /*
  513. * Promote the attribute list to the Btree format, then
  514. * Commit that transaction so that the node_addname() call
  515. * can manage its own transactions.
  516. */
  517. xfs_defer_init(args->dfops, args->firstblock);
  518. error = xfs_attr3_leaf_to_node(args);
  519. if (error)
  520. goto out_defer_cancel;
  521. xfs_defer_ijoin(args->dfops, dp);
  522. error = xfs_defer_finish(&args->trans, args->dfops);
  523. if (error)
  524. goto out_defer_cancel;
  525. /*
  526. * Commit the current trans (including the inode) and start
  527. * a new one.
  528. */
  529. error = xfs_trans_roll_inode(&args->trans, dp);
  530. if (error)
  531. return error;
  532. /*
  533. * Fob the whole rest of the problem off on the Btree code.
  534. */
  535. error = xfs_attr_node_addname(args);
  536. return error;
  537. }
  538. /*
  539. * Commit the transaction that added the attr name so that
  540. * later routines can manage their own transactions.
  541. */
  542. error = xfs_trans_roll_inode(&args->trans, dp);
  543. if (error)
  544. return error;
  545. /*
  546. * If there was an out-of-line value, allocate the blocks we
  547. * identified for its storage and copy the value. This is done
  548. * after we create the attribute so that we don't overflow the
  549. * maximum size of a transaction and/or hit a deadlock.
  550. */
  551. if (args->rmtblkno > 0) {
  552. error = xfs_attr_rmtval_set(args);
  553. if (error)
  554. return error;
  555. }
  556. /*
  557. * If this is an atomic rename operation, we must "flip" the
  558. * incomplete flags on the "new" and "old" attribute/value pairs
  559. * so that one disappears and one appears atomically. Then we
  560. * must remove the "old" attribute/value pair.
  561. */
  562. if (args->op_flags & XFS_DA_OP_RENAME) {
  563. /*
  564. * In a separate transaction, set the incomplete flag on the
  565. * "old" attr and clear the incomplete flag on the "new" attr.
  566. */
  567. error = xfs_attr3_leaf_flipflags(args);
  568. if (error)
  569. return error;
  570. /*
  571. * Dismantle the "old" attribute/value pair by removing
  572. * a "remote" value (if it exists).
  573. */
  574. args->index = args->index2;
  575. args->blkno = args->blkno2;
  576. args->rmtblkno = args->rmtblkno2;
  577. args->rmtblkcnt = args->rmtblkcnt2;
  578. args->rmtvaluelen = args->rmtvaluelen2;
  579. if (args->rmtblkno) {
  580. error = xfs_attr_rmtval_remove(args);
  581. if (error)
  582. return error;
  583. }
  584. /*
  585. * Read in the block containing the "old" attr, then
  586. * remove the "old" attr from that block (neat, huh!)
  587. */
  588. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  589. -1, &bp);
  590. if (error)
  591. return error;
  592. xfs_attr3_leaf_remove(bp, args);
  593. /*
  594. * If the result is small enough, shrink it all into the inode.
  595. */
  596. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  597. xfs_defer_init(args->dfops, args->firstblock);
  598. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  599. /* bp is gone due to xfs_da_shrink_inode */
  600. if (error)
  601. goto out_defer_cancel;
  602. xfs_defer_ijoin(args->dfops, dp);
  603. error = xfs_defer_finish(&args->trans, args->dfops);
  604. if (error)
  605. goto out_defer_cancel;
  606. }
  607. /*
  608. * Commit the remove and start the next trans in series.
  609. */
  610. error = xfs_trans_roll_inode(&args->trans, dp);
  611. } else if (args->rmtblkno > 0) {
  612. /*
  613. * Added a "remote" value, just clear the incomplete flag.
  614. */
  615. error = xfs_attr3_leaf_clearflag(args);
  616. }
  617. return error;
  618. out_defer_cancel:
  619. xfs_defer_cancel(args->dfops);
  620. return error;
  621. }
  622. /*
  623. * Remove a name from the leaf attribute list structure
  624. *
  625. * This leaf block cannot have a "remote" value, we only call this routine
  626. * if bmap_one_block() says there is only one block (ie: no remote blks).
  627. */
  628. STATIC int
  629. xfs_attr_leaf_removename(xfs_da_args_t *args)
  630. {
  631. xfs_inode_t *dp;
  632. struct xfs_buf *bp;
  633. int error, forkoff;
  634. trace_xfs_attr_leaf_removename(args);
  635. /*
  636. * Remove the attribute.
  637. */
  638. dp = args->dp;
  639. args->blkno = 0;
  640. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  641. if (error)
  642. return error;
  643. error = xfs_attr3_leaf_lookup_int(bp, args);
  644. if (error == -ENOATTR) {
  645. xfs_trans_brelse(args->trans, bp);
  646. return error;
  647. }
  648. xfs_attr3_leaf_remove(bp, args);
  649. /*
  650. * If the result is small enough, shrink it all into the inode.
  651. */
  652. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  653. xfs_defer_init(args->dfops, args->firstblock);
  654. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  655. /* bp is gone due to xfs_da_shrink_inode */
  656. if (error)
  657. goto out_defer_cancel;
  658. xfs_defer_ijoin(args->dfops, dp);
  659. error = xfs_defer_finish(&args->trans, args->dfops);
  660. if (error)
  661. goto out_defer_cancel;
  662. }
  663. return 0;
  664. out_defer_cancel:
  665. xfs_defer_cancel(args->dfops);
  666. return error;
  667. }
  668. /*
  669. * Look up a name in a leaf attribute list structure.
  670. *
  671. * This leaf block cannot have a "remote" value, we only call this routine
  672. * if bmap_one_block() says there is only one block (ie: no remote blks).
  673. */
  674. STATIC int
  675. xfs_attr_leaf_get(xfs_da_args_t *args)
  676. {
  677. struct xfs_buf *bp;
  678. int error;
  679. trace_xfs_attr_leaf_get(args);
  680. args->blkno = 0;
  681. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  682. if (error)
  683. return error;
  684. error = xfs_attr3_leaf_lookup_int(bp, args);
  685. if (error != -EEXIST) {
  686. xfs_trans_brelse(args->trans, bp);
  687. return error;
  688. }
  689. error = xfs_attr3_leaf_getvalue(bp, args);
  690. xfs_trans_brelse(args->trans, bp);
  691. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  692. error = xfs_attr_rmtval_get(args);
  693. }
  694. return error;
  695. }
  696. /*========================================================================
  697. * External routines when attribute list size > geo->blksize
  698. *========================================================================*/
  699. /*
  700. * Add a name to a Btree-format attribute list.
  701. *
  702. * This will involve walking down the Btree, and may involve splitting
  703. * leaf nodes and even splitting intermediate nodes up to and including
  704. * the root node (a special case of an intermediate node).
  705. *
  706. * "Remote" attribute values confuse the issue and atomic rename operations
  707. * add a whole extra layer of confusion on top of that.
  708. */
  709. STATIC int
  710. xfs_attr_node_addname(xfs_da_args_t *args)
  711. {
  712. xfs_da_state_t *state;
  713. xfs_da_state_blk_t *blk;
  714. xfs_inode_t *dp;
  715. xfs_mount_t *mp;
  716. int retval, error;
  717. trace_xfs_attr_node_addname(args);
  718. /*
  719. * Fill in bucket of arguments/results/context to carry around.
  720. */
  721. dp = args->dp;
  722. mp = dp->i_mount;
  723. restart:
  724. state = xfs_da_state_alloc();
  725. state->args = args;
  726. state->mp = mp;
  727. /*
  728. * Search to see if name already exists, and get back a pointer
  729. * to where it should go.
  730. */
  731. error = xfs_da3_node_lookup_int(state, &retval);
  732. if (error)
  733. goto out;
  734. blk = &state->path.blk[ state->path.active-1 ];
  735. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  736. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  737. goto out;
  738. } else if (retval == -EEXIST) {
  739. if (args->flags & ATTR_CREATE)
  740. goto out;
  741. trace_xfs_attr_node_replace(args);
  742. /* save the attribute state for later removal*/
  743. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  744. args->blkno2 = args->blkno; /* set 2nd entry info*/
  745. args->index2 = args->index;
  746. args->rmtblkno2 = args->rmtblkno;
  747. args->rmtblkcnt2 = args->rmtblkcnt;
  748. args->rmtvaluelen2 = args->rmtvaluelen;
  749. /*
  750. * clear the remote attr state now that it is saved so that the
  751. * values reflect the state of the attribute we are about to
  752. * add, not the attribute we just found and will remove later.
  753. */
  754. args->rmtblkno = 0;
  755. args->rmtblkcnt = 0;
  756. args->rmtvaluelen = 0;
  757. }
  758. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  759. if (retval == -ENOSPC) {
  760. if (state->path.active == 1) {
  761. /*
  762. * Its really a single leaf node, but it had
  763. * out-of-line values so it looked like it *might*
  764. * have been a b-tree.
  765. */
  766. xfs_da_state_free(state);
  767. state = NULL;
  768. xfs_defer_init(args->dfops, args->firstblock);
  769. error = xfs_attr3_leaf_to_node(args);
  770. if (error)
  771. goto out_defer_cancel;
  772. xfs_defer_ijoin(args->dfops, dp);
  773. error = xfs_defer_finish(&args->trans, args->dfops);
  774. if (error)
  775. goto out_defer_cancel;
  776. /*
  777. * Commit the node conversion and start the next
  778. * trans in the chain.
  779. */
  780. error = xfs_trans_roll_inode(&args->trans, dp);
  781. if (error)
  782. goto out;
  783. goto restart;
  784. }
  785. /*
  786. * Split as many Btree elements as required.
  787. * This code tracks the new and old attr's location
  788. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  789. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  790. */
  791. xfs_defer_init(args->dfops, args->firstblock);
  792. error = xfs_da3_split(state);
  793. if (error)
  794. goto out_defer_cancel;
  795. xfs_defer_ijoin(args->dfops, dp);
  796. error = xfs_defer_finish(&args->trans, args->dfops);
  797. if (error)
  798. goto out_defer_cancel;
  799. } else {
  800. /*
  801. * Addition succeeded, update Btree hashvals.
  802. */
  803. xfs_da3_fixhashpath(state, &state->path);
  804. }
  805. /*
  806. * Kill the state structure, we're done with it and need to
  807. * allow the buffers to come back later.
  808. */
  809. xfs_da_state_free(state);
  810. state = NULL;
  811. /*
  812. * Commit the leaf addition or btree split and start the next
  813. * trans in the chain.
  814. */
  815. error = xfs_trans_roll_inode(&args->trans, dp);
  816. if (error)
  817. goto out;
  818. /*
  819. * If there was an out-of-line value, allocate the blocks we
  820. * identified for its storage and copy the value. This is done
  821. * after we create the attribute so that we don't overflow the
  822. * maximum size of a transaction and/or hit a deadlock.
  823. */
  824. if (args->rmtblkno > 0) {
  825. error = xfs_attr_rmtval_set(args);
  826. if (error)
  827. return error;
  828. }
  829. /*
  830. * If this is an atomic rename operation, we must "flip" the
  831. * incomplete flags on the "new" and "old" attribute/value pairs
  832. * so that one disappears and one appears atomically. Then we
  833. * must remove the "old" attribute/value pair.
  834. */
  835. if (args->op_flags & XFS_DA_OP_RENAME) {
  836. /*
  837. * In a separate transaction, set the incomplete flag on the
  838. * "old" attr and clear the incomplete flag on the "new" attr.
  839. */
  840. error = xfs_attr3_leaf_flipflags(args);
  841. if (error)
  842. goto out;
  843. /*
  844. * Dismantle the "old" attribute/value pair by removing
  845. * a "remote" value (if it exists).
  846. */
  847. args->index = args->index2;
  848. args->blkno = args->blkno2;
  849. args->rmtblkno = args->rmtblkno2;
  850. args->rmtblkcnt = args->rmtblkcnt2;
  851. args->rmtvaluelen = args->rmtvaluelen2;
  852. if (args->rmtblkno) {
  853. error = xfs_attr_rmtval_remove(args);
  854. if (error)
  855. return error;
  856. }
  857. /*
  858. * Re-find the "old" attribute entry after any split ops.
  859. * The INCOMPLETE flag means that we will find the "old"
  860. * attr, not the "new" one.
  861. */
  862. args->flags |= XFS_ATTR_INCOMPLETE;
  863. state = xfs_da_state_alloc();
  864. state->args = args;
  865. state->mp = mp;
  866. state->inleaf = 0;
  867. error = xfs_da3_node_lookup_int(state, &retval);
  868. if (error)
  869. goto out;
  870. /*
  871. * Remove the name and update the hashvals in the tree.
  872. */
  873. blk = &state->path.blk[ state->path.active-1 ];
  874. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  875. error = xfs_attr3_leaf_remove(blk->bp, args);
  876. xfs_da3_fixhashpath(state, &state->path);
  877. /*
  878. * Check to see if the tree needs to be collapsed.
  879. */
  880. if (retval && (state->path.active > 1)) {
  881. xfs_defer_init(args->dfops, args->firstblock);
  882. error = xfs_da3_join(state);
  883. if (error)
  884. goto out_defer_cancel;
  885. xfs_defer_ijoin(args->dfops, dp);
  886. error = xfs_defer_finish(&args->trans, args->dfops);
  887. if (error)
  888. goto out_defer_cancel;
  889. }
  890. /*
  891. * Commit and start the next trans in the chain.
  892. */
  893. error = xfs_trans_roll_inode(&args->trans, dp);
  894. if (error)
  895. goto out;
  896. } else if (args->rmtblkno > 0) {
  897. /*
  898. * Added a "remote" value, just clear the incomplete flag.
  899. */
  900. error = xfs_attr3_leaf_clearflag(args);
  901. if (error)
  902. goto out;
  903. }
  904. retval = error = 0;
  905. out:
  906. if (state)
  907. xfs_da_state_free(state);
  908. if (error)
  909. return error;
  910. return retval;
  911. out_defer_cancel:
  912. xfs_defer_cancel(args->dfops);
  913. goto out;
  914. }
  915. /*
  916. * Remove a name from a B-tree attribute list.
  917. *
  918. * This will involve walking down the Btree, and may involve joining
  919. * leaf nodes and even joining intermediate nodes up to and including
  920. * the root node (a special case of an intermediate node).
  921. */
  922. STATIC int
  923. xfs_attr_node_removename(xfs_da_args_t *args)
  924. {
  925. xfs_da_state_t *state;
  926. xfs_da_state_blk_t *blk;
  927. xfs_inode_t *dp;
  928. struct xfs_buf *bp;
  929. int retval, error, forkoff;
  930. trace_xfs_attr_node_removename(args);
  931. /*
  932. * Tie a string around our finger to remind us where we are.
  933. */
  934. dp = args->dp;
  935. state = xfs_da_state_alloc();
  936. state->args = args;
  937. state->mp = dp->i_mount;
  938. /*
  939. * Search to see if name exists, and get back a pointer to it.
  940. */
  941. error = xfs_da3_node_lookup_int(state, &retval);
  942. if (error || (retval != -EEXIST)) {
  943. if (error == 0)
  944. error = retval;
  945. goto out;
  946. }
  947. /*
  948. * If there is an out-of-line value, de-allocate the blocks.
  949. * This is done before we remove the attribute so that we don't
  950. * overflow the maximum size of a transaction and/or hit a deadlock.
  951. */
  952. blk = &state->path.blk[ state->path.active-1 ];
  953. ASSERT(blk->bp != NULL);
  954. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  955. if (args->rmtblkno > 0) {
  956. /*
  957. * Fill in disk block numbers in the state structure
  958. * so that we can get the buffers back after we commit
  959. * several transactions in the following calls.
  960. */
  961. error = xfs_attr_fillstate(state);
  962. if (error)
  963. goto out;
  964. /*
  965. * Mark the attribute as INCOMPLETE, then bunmapi() the
  966. * remote value.
  967. */
  968. error = xfs_attr3_leaf_setflag(args);
  969. if (error)
  970. goto out;
  971. error = xfs_attr_rmtval_remove(args);
  972. if (error)
  973. goto out;
  974. /*
  975. * Refill the state structure with buffers, the prior calls
  976. * released our buffers.
  977. */
  978. error = xfs_attr_refillstate(state);
  979. if (error)
  980. goto out;
  981. }
  982. /*
  983. * Remove the name and update the hashvals in the tree.
  984. */
  985. blk = &state->path.blk[ state->path.active-1 ];
  986. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  987. retval = xfs_attr3_leaf_remove(blk->bp, args);
  988. xfs_da3_fixhashpath(state, &state->path);
  989. /*
  990. * Check to see if the tree needs to be collapsed.
  991. */
  992. if (retval && (state->path.active > 1)) {
  993. xfs_defer_init(args->dfops, args->firstblock);
  994. error = xfs_da3_join(state);
  995. if (error)
  996. goto out_defer_cancel;
  997. xfs_defer_ijoin(args->dfops, dp);
  998. error = xfs_defer_finish(&args->trans, args->dfops);
  999. if (error)
  1000. goto out_defer_cancel;
  1001. /*
  1002. * Commit the Btree join operation and start a new trans.
  1003. */
  1004. error = xfs_trans_roll_inode(&args->trans, dp);
  1005. if (error)
  1006. goto out;
  1007. }
  1008. /*
  1009. * If the result is small enough, push it all into the inode.
  1010. */
  1011. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1012. /*
  1013. * Have to get rid of the copy of this dabuf in the state.
  1014. */
  1015. ASSERT(state->path.active == 1);
  1016. ASSERT(state->path.blk[0].bp);
  1017. state->path.blk[0].bp = NULL;
  1018. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1019. if (error)
  1020. goto out;
  1021. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1022. xfs_defer_init(args->dfops, args->firstblock);
  1023. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1024. /* bp is gone due to xfs_da_shrink_inode */
  1025. if (error)
  1026. goto out_defer_cancel;
  1027. xfs_defer_ijoin(args->dfops, dp);
  1028. error = xfs_defer_finish(&args->trans, args->dfops);
  1029. if (error)
  1030. goto out_defer_cancel;
  1031. } else
  1032. xfs_trans_brelse(args->trans, bp);
  1033. }
  1034. error = 0;
  1035. out:
  1036. xfs_da_state_free(state);
  1037. return error;
  1038. out_defer_cancel:
  1039. xfs_defer_cancel(args->dfops);
  1040. goto out;
  1041. }
  1042. /*
  1043. * Fill in the disk block numbers in the state structure for the buffers
  1044. * that are attached to the state structure.
  1045. * This is done so that we can quickly reattach ourselves to those buffers
  1046. * after some set of transaction commits have released these buffers.
  1047. */
  1048. STATIC int
  1049. xfs_attr_fillstate(xfs_da_state_t *state)
  1050. {
  1051. xfs_da_state_path_t *path;
  1052. xfs_da_state_blk_t *blk;
  1053. int level;
  1054. trace_xfs_attr_fillstate(state->args);
  1055. /*
  1056. * Roll down the "path" in the state structure, storing the on-disk
  1057. * block number for those buffers in the "path".
  1058. */
  1059. path = &state->path;
  1060. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1061. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1062. if (blk->bp) {
  1063. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1064. blk->bp = NULL;
  1065. } else {
  1066. blk->disk_blkno = 0;
  1067. }
  1068. }
  1069. /*
  1070. * Roll down the "altpath" in the state structure, storing the on-disk
  1071. * block number for those buffers in the "altpath".
  1072. */
  1073. path = &state->altpath;
  1074. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1075. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1076. if (blk->bp) {
  1077. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1078. blk->bp = NULL;
  1079. } else {
  1080. blk->disk_blkno = 0;
  1081. }
  1082. }
  1083. return 0;
  1084. }
  1085. /*
  1086. * Reattach the buffers to the state structure based on the disk block
  1087. * numbers stored in the state structure.
  1088. * This is done after some set of transaction commits have released those
  1089. * buffers from our grip.
  1090. */
  1091. STATIC int
  1092. xfs_attr_refillstate(xfs_da_state_t *state)
  1093. {
  1094. xfs_da_state_path_t *path;
  1095. xfs_da_state_blk_t *blk;
  1096. int level, error;
  1097. trace_xfs_attr_refillstate(state->args);
  1098. /*
  1099. * Roll down the "path" in the state structure, storing the on-disk
  1100. * block number for those buffers in the "path".
  1101. */
  1102. path = &state->path;
  1103. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1104. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1105. if (blk->disk_blkno) {
  1106. error = xfs_da3_node_read(state->args->trans,
  1107. state->args->dp,
  1108. blk->blkno, blk->disk_blkno,
  1109. &blk->bp, XFS_ATTR_FORK);
  1110. if (error)
  1111. return error;
  1112. } else {
  1113. blk->bp = NULL;
  1114. }
  1115. }
  1116. /*
  1117. * Roll down the "altpath" in the state structure, storing the on-disk
  1118. * block number for those buffers in the "altpath".
  1119. */
  1120. path = &state->altpath;
  1121. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1122. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1123. if (blk->disk_blkno) {
  1124. error = xfs_da3_node_read(state->args->trans,
  1125. state->args->dp,
  1126. blk->blkno, blk->disk_blkno,
  1127. &blk->bp, XFS_ATTR_FORK);
  1128. if (error)
  1129. return error;
  1130. } else {
  1131. blk->bp = NULL;
  1132. }
  1133. }
  1134. return 0;
  1135. }
  1136. /*
  1137. * Look up a filename in a node attribute list.
  1138. *
  1139. * This routine gets called for any attribute fork that has more than one
  1140. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1141. * "remote" values taking up more blocks.
  1142. */
  1143. STATIC int
  1144. xfs_attr_node_get(xfs_da_args_t *args)
  1145. {
  1146. xfs_da_state_t *state;
  1147. xfs_da_state_blk_t *blk;
  1148. int error, retval;
  1149. int i;
  1150. trace_xfs_attr_node_get(args);
  1151. state = xfs_da_state_alloc();
  1152. state->args = args;
  1153. state->mp = args->dp->i_mount;
  1154. /*
  1155. * Search to see if name exists, and get back a pointer to it.
  1156. */
  1157. error = xfs_da3_node_lookup_int(state, &retval);
  1158. if (error) {
  1159. retval = error;
  1160. } else if (retval == -EEXIST) {
  1161. blk = &state->path.blk[ state->path.active-1 ];
  1162. ASSERT(blk->bp != NULL);
  1163. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1164. /*
  1165. * Get the value, local or "remote"
  1166. */
  1167. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1168. if (!retval && (args->rmtblkno > 0)
  1169. && !(args->flags & ATTR_KERNOVAL)) {
  1170. retval = xfs_attr_rmtval_get(args);
  1171. }
  1172. }
  1173. /*
  1174. * If not in a transaction, we have to release all the buffers.
  1175. */
  1176. for (i = 0; i < state->path.active; i++) {
  1177. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1178. state->path.blk[i].bp = NULL;
  1179. }
  1180. xfs_da_state_free(state);
  1181. return retval;
  1182. }