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