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