xfs_attr.c 34 KB

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