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