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