arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_attr.c
blob9687208c676f51f14ac1b07fa085980d55a571cd
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
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.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_bmap.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_attr.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_trace.h"
46 * xfs_attr.c
48 * Provide the external interfaces to manage attribute lists.
51 /*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
56 * Internal routines when attribute list fits inside the inode.
58 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
61 * Internal routines when attribute list is one block.
63 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
64 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
68 * Internal routines when attribute list is more than one block.
70 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
71 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
72 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
73 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
74 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
77 STATIC int
78 xfs_attr_args_init(
79 struct xfs_da_args *args,
80 struct xfs_inode *dp,
81 const unsigned char *name,
82 int flags)
85 if (!name)
86 return -EINVAL;
88 memset(args, 0, sizeof(*args));
89 args->geo = dp->i_mount->m_attr_geo;
90 args->whichfork = XFS_ATTR_FORK;
91 args->dp = dp;
92 args->flags = flags;
93 args->name = name;
94 args->namelen = strlen((const char *)name);
95 if (args->namelen >= MAXNAMELEN)
96 return -EFAULT; /* match IRIX behaviour */
98 args->hashval = xfs_da_hashname(args->name, args->namelen);
99 return 0;
103 xfs_inode_hasattr(
104 struct xfs_inode *ip)
106 if (!XFS_IFORK_Q(ip) ||
107 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108 ip->i_d.di_anextents == 0))
109 return 0;
110 return 1;
113 /*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
118 xfs_attr_get(
119 struct xfs_inode *ip,
120 const unsigned char *name,
121 unsigned char *value,
122 int *valuelenp,
123 int flags)
125 struct xfs_da_args args;
126 uint lock_mode;
127 int error;
129 XFS_STATS_INC(ip->i_mount, xs_attr_get);
131 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
132 return -EIO;
134 error = xfs_attr_args_init(&args, ip, name, flags);
135 if (error)
136 return error;
138 args.value = value;
139 args.valuelen = *valuelenp;
140 /* Entirely possible to look up a name which doesn't exist */
141 args.op_flags = XFS_DA_OP_OKNOENT;
143 lock_mode = xfs_ilock_attr_map_shared(ip);
144 if (!xfs_inode_hasattr(ip))
145 error = -ENOATTR;
146 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
147 error = xfs_attr_shortform_getvalue(&args);
148 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
149 error = xfs_attr_leaf_get(&args);
150 else
151 error = xfs_attr_node_get(&args);
152 xfs_iunlock(ip, lock_mode);
154 *valuelenp = args.valuelen;
155 return error == -EEXIST ? 0 : error;
159 * Calculate how many blocks we need for the new attribute,
161 STATIC int
162 xfs_attr_calc_size(
163 struct xfs_da_args *args,
164 int *local)
166 struct xfs_mount *mp = args->dp->i_mount;
167 int size;
168 int nblks;
171 * Determine space new attribute will use, and if it would be
172 * "local" or "remote" (note: local != inline).
174 size = xfs_attr_leaf_newentsize(args, local);
175 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176 if (*local) {
177 if (size > (args->geo->blksize / 2)) {
178 /* Double split possible */
179 nblks *= 2;
181 } else {
183 * Out of line attribute, cannot double split, but
184 * make room for the attribute value itself.
186 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
187 nblks += dblocks;
188 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
191 return nblks;
195 xfs_attr_set(
196 struct xfs_inode *dp,
197 const unsigned char *name,
198 unsigned char *value,
199 int valuelen,
200 int flags)
202 struct xfs_mount *mp = dp->i_mount;
203 struct xfs_da_args args;
204 struct xfs_defer_ops dfops;
205 struct xfs_trans_res tres;
206 xfs_fsblock_t firstblock;
207 int rsvd = (flags & ATTR_ROOT) != 0;
208 int error, err2, local;
210 XFS_STATS_INC(mp, xs_attr_set);
212 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
213 return -EIO;
215 error = xfs_attr_args_init(&args, dp, name, flags);
216 if (error)
217 return error;
219 args.value = value;
220 args.valuelen = valuelen;
221 args.firstblock = &firstblock;
222 args.dfops = &dfops;
223 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
224 args.total = xfs_attr_calc_size(&args, &local);
226 error = xfs_qm_dqattach(dp, 0);
227 if (error)
228 return error;
231 * If the inode doesn't have an attribute fork, add one.
232 * (inode must not be locked when we call this routine)
234 if (XFS_IFORK_Q(dp) == 0) {
235 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
236 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
238 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
239 if (error)
240 return error;
243 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
244 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
245 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
246 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
249 * Root fork attributes can use reserved data blocks for this
250 * operation if necessary
252 error = xfs_trans_alloc(mp, &tres, args.total, 0,
253 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
254 if (error)
255 return error;
257 xfs_ilock(dp, XFS_ILOCK_EXCL);
258 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
259 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
260 XFS_QMOPT_RES_REGBLKS);
261 if (error) {
262 xfs_iunlock(dp, XFS_ILOCK_EXCL);
263 xfs_trans_cancel(args.trans);
264 return error;
267 xfs_trans_ijoin(args.trans, dp, 0);
270 * If the attribute list is non-existent or a shortform list,
271 * upgrade it to a single-leaf-block attribute list.
273 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
274 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
275 dp->i_d.di_anextents == 0)) {
278 * Build initial attribute list (if required).
280 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
281 xfs_attr_shortform_create(&args);
284 * Try to add the attr to the attribute list in
285 * the inode.
287 error = xfs_attr_shortform_addname(&args);
288 if (error != -ENOSPC) {
290 * Commit the shortform mods, and we're done.
291 * NOTE: this is also the error path (EEXIST, etc).
293 ASSERT(args.trans != NULL);
296 * If this is a synchronous mount, make sure that
297 * the transaction goes to disk before returning
298 * to the user.
300 if (mp->m_flags & XFS_MOUNT_WSYNC)
301 xfs_trans_set_sync(args.trans);
303 if (!error && (flags & ATTR_KERNOTIME) == 0) {
304 xfs_trans_ichgtime(args.trans, dp,
305 XFS_ICHGTIME_CHG);
307 err2 = xfs_trans_commit(args.trans);
308 xfs_iunlock(dp, XFS_ILOCK_EXCL);
310 return error ? error : err2;
314 * It won't fit in the shortform, transform to a leaf block.
315 * GROT: another possible req'mt for a double-split btree op.
317 xfs_defer_init(args.dfops, args.firstblock);
318 error = xfs_attr_shortform_to_leaf(&args);
319 if (!error)
320 error = xfs_defer_finish(&args.trans, args.dfops, dp);
321 if (error) {
322 args.trans = NULL;
323 xfs_defer_cancel(&dfops);
324 goto out;
328 * Commit the leaf transformation. We'll need another (linked)
329 * transaction to add the new attribute to the leaf.
332 error = xfs_trans_roll(&args.trans, dp);
333 if (error)
334 goto out;
338 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
339 error = xfs_attr_leaf_addname(&args);
340 else
341 error = xfs_attr_node_addname(&args);
342 if (error)
343 goto out;
346 * If this is a synchronous mount, make sure that the
347 * transaction goes to disk before returning to the user.
349 if (mp->m_flags & XFS_MOUNT_WSYNC)
350 xfs_trans_set_sync(args.trans);
352 if ((flags & ATTR_KERNOTIME) == 0)
353 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
356 * Commit the last in the sequence of transactions.
358 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
359 error = xfs_trans_commit(args.trans);
360 xfs_iunlock(dp, XFS_ILOCK_EXCL);
362 return error;
364 out:
365 if (args.trans)
366 xfs_trans_cancel(args.trans);
367 xfs_iunlock(dp, XFS_ILOCK_EXCL);
368 return error;
372 * Generic handler routine to remove a name from an attribute list.
373 * Transitions attribute list from Btree to shortform as necessary.
376 xfs_attr_remove(
377 struct xfs_inode *dp,
378 const unsigned char *name,
379 int flags)
381 struct xfs_mount *mp = dp->i_mount;
382 struct xfs_da_args args;
383 struct xfs_defer_ops dfops;
384 xfs_fsblock_t firstblock;
385 int error;
387 XFS_STATS_INC(mp, xs_attr_remove);
389 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
390 return -EIO;
392 error = xfs_attr_args_init(&args, dp, name, flags);
393 if (error)
394 return error;
396 args.firstblock = &firstblock;
397 args.dfops = &dfops;
400 * we have no control over the attribute names that userspace passes us
401 * to remove, so we have to allow the name lookup prior to attribute
402 * removal to fail.
404 args.op_flags = XFS_DA_OP_OKNOENT;
406 error = xfs_qm_dqattach(dp, 0);
407 if (error)
408 return error;
411 * Root fork attributes can use reserved data blocks for this
412 * operation if necessary
414 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
415 XFS_ATTRRM_SPACE_RES(mp), 0,
416 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
417 &args.trans);
418 if (error)
419 return error;
421 xfs_ilock(dp, XFS_ILOCK_EXCL);
423 * No need to make quota reservations here. We expect to release some
424 * blocks not allocate in the common case.
426 xfs_trans_ijoin(args.trans, dp, 0);
428 if (!xfs_inode_hasattr(dp)) {
429 error = -ENOATTR;
430 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
431 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
432 error = xfs_attr_shortform_remove(&args);
433 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
434 error = xfs_attr_leaf_removename(&args);
435 } else {
436 error = xfs_attr_node_removename(&args);
439 if (error)
440 goto out;
443 * If this is a synchronous mount, make sure that the
444 * transaction goes to disk before returning to the user.
446 if (mp->m_flags & XFS_MOUNT_WSYNC)
447 xfs_trans_set_sync(args.trans);
449 if ((flags & ATTR_KERNOTIME) == 0)
450 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
453 * Commit the last in the sequence of transactions.
455 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
456 error = xfs_trans_commit(args.trans);
457 xfs_iunlock(dp, XFS_ILOCK_EXCL);
459 return error;
461 out:
462 if (args.trans)
463 xfs_trans_cancel(args.trans);
464 xfs_iunlock(dp, XFS_ILOCK_EXCL);
465 return error;
468 /*========================================================================
469 * External routines when attribute list is inside the inode
470 *========================================================================*/
473 * Add a name to the shortform attribute list structure
474 * This is the external routine.
476 STATIC int
477 xfs_attr_shortform_addname(xfs_da_args_t *args)
479 int newsize, forkoff, retval;
481 trace_xfs_attr_sf_addname(args);
483 retval = xfs_attr_shortform_lookup(args);
484 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
485 return retval;
486 } else if (retval == -EEXIST) {
487 if (args->flags & ATTR_CREATE)
488 return retval;
489 retval = xfs_attr_shortform_remove(args);
490 if (retval)
491 return retval;
493 * Since we have removed the old attr, clear ATTR_REPLACE so
494 * that the leaf format add routine won't trip over the attr
495 * not being around.
497 args->flags &= ~ATTR_REPLACE;
500 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
501 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
502 return -ENOSPC;
504 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
505 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
507 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
508 if (!forkoff)
509 return -ENOSPC;
511 xfs_attr_shortform_add(args, forkoff);
512 return 0;
516 /*========================================================================
517 * External routines when attribute list is one block
518 *========================================================================*/
521 * Add a name to the leaf attribute list structure
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).
526 STATIC int
527 xfs_attr_leaf_addname(xfs_da_args_t *args)
529 xfs_inode_t *dp;
530 struct xfs_buf *bp;
531 int retval, error, forkoff;
533 trace_xfs_attr_leaf_addname(args);
536 * Read the (only) block in the attribute list in.
538 dp = args->dp;
539 args->blkno = 0;
540 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
541 if (error)
542 return error;
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.
548 retval = xfs_attr3_leaf_lookup_int(bp, args);
549 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
550 xfs_trans_brelse(args->trans, bp);
551 return retval;
552 } else if (retval == -EEXIST) {
553 if (args->flags & ATTR_CREATE) { /* pure create op */
554 xfs_trans_brelse(args->trans, bp);
555 return retval;
558 trace_xfs_attr_leaf_replace(args);
560 /* save the attribute state for later removal*/
561 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
562 args->blkno2 = args->blkno; /* set 2nd entry info*/
563 args->index2 = args->index;
564 args->rmtblkno2 = args->rmtblkno;
565 args->rmtblkcnt2 = args->rmtblkcnt;
566 args->rmtvaluelen2 = args->rmtvaluelen;
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.
573 args->rmtblkno = 0;
574 args->rmtblkcnt = 0;
575 args->rmtvaluelen = 0;
579 * Add the attribute to the leaf block, transitioning to a Btree
580 * if required.
582 retval = xfs_attr3_leaf_add(bp, args);
583 if (retval == -ENOSPC) {
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.
589 xfs_defer_init(args->dfops, args->firstblock);
590 error = xfs_attr3_leaf_to_node(args);
591 if (!error)
592 error = xfs_defer_finish(&args->trans, args->dfops, dp);
593 if (error) {
594 args->trans = NULL;
595 xfs_defer_cancel(args->dfops);
596 return error;
600 * Commit the current trans (including the inode) and start
601 * a new one.
603 error = xfs_trans_roll(&args->trans, dp);
604 if (error)
605 return error;
608 * Fob the whole rest of the problem off on the Btree code.
610 error = xfs_attr_node_addname(args);
611 return error;
615 * Commit the transaction that added the attr name so that
616 * later routines can manage their own transactions.
618 error = xfs_trans_roll(&args->trans, dp);
619 if (error)
620 return error;
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.
628 if (args->rmtblkno > 0) {
629 error = xfs_attr_rmtval_set(args);
630 if (error)
631 return error;
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.
640 if (args->op_flags & XFS_DA_OP_RENAME) {
642 * In a separate transaction, set the incomplete flag on the
643 * "old" attr and clear the incomplete flag on the "new" attr.
645 error = xfs_attr3_leaf_flipflags(args);
646 if (error)
647 return error;
650 * Dismantle the "old" attribute/value pair by removing
651 * a "remote" value (if it exists).
653 args->index = args->index2;
654 args->blkno = args->blkno2;
655 args->rmtblkno = args->rmtblkno2;
656 args->rmtblkcnt = args->rmtblkcnt2;
657 args->rmtvaluelen = args->rmtvaluelen2;
658 if (args->rmtblkno) {
659 error = xfs_attr_rmtval_remove(args);
660 if (error)
661 return error;
665 * Read in the block containing the "old" attr, then
666 * remove the "old" attr from that block (neat, huh!)
668 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
669 -1, &bp);
670 if (error)
671 return error;
673 xfs_attr3_leaf_remove(bp, args);
676 * If the result is small enough, shrink it all into the inode.
678 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
679 xfs_defer_init(args->dfops, args->firstblock);
680 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
681 /* bp is gone due to xfs_da_shrink_inode */
682 if (!error)
683 error = xfs_defer_finish(&args->trans,
684 args->dfops, dp);
685 if (error) {
686 args->trans = NULL;
687 xfs_defer_cancel(args->dfops);
688 return error;
693 * Commit the remove and start the next trans in series.
695 error = xfs_trans_roll(&args->trans, dp);
697 } else if (args->rmtblkno > 0) {
699 * Added a "remote" value, just clear the incomplete flag.
701 error = xfs_attr3_leaf_clearflag(args);
703 return error;
707 * Remove a name from the leaf attribute list structure
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).
712 STATIC int
713 xfs_attr_leaf_removename(xfs_da_args_t *args)
715 xfs_inode_t *dp;
716 struct xfs_buf *bp;
717 int error, forkoff;
719 trace_xfs_attr_leaf_removename(args);
722 * Remove the attribute.
724 dp = args->dp;
725 args->blkno = 0;
726 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
727 if (error)
728 return error;
730 error = xfs_attr3_leaf_lookup_int(bp, args);
731 if (error == -ENOATTR) {
732 xfs_trans_brelse(args->trans, bp);
733 return error;
736 xfs_attr3_leaf_remove(bp, args);
739 * If the result is small enough, shrink it all into the inode.
741 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
742 xfs_defer_init(args->dfops, args->firstblock);
743 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
744 /* bp is gone due to xfs_da_shrink_inode */
745 if (!error)
746 error = xfs_defer_finish(&args->trans, args->dfops, dp);
747 if (error) {
748 args->trans = NULL;
749 xfs_defer_cancel(args->dfops);
750 return error;
753 return 0;
757 * Look up a name in a leaf attribute list structure.
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).
762 STATIC int
763 xfs_attr_leaf_get(xfs_da_args_t *args)
765 struct xfs_buf *bp;
766 int error;
768 trace_xfs_attr_leaf_get(args);
770 args->blkno = 0;
771 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
772 if (error)
773 return error;
775 error = xfs_attr3_leaf_lookup_int(bp, args);
776 if (error != -EEXIST) {
777 xfs_trans_brelse(args->trans, bp);
778 return error;
780 error = xfs_attr3_leaf_getvalue(bp, args);
781 xfs_trans_brelse(args->trans, bp);
782 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
783 error = xfs_attr_rmtval_get(args);
785 return error;
788 /*========================================================================
789 * External routines when attribute list size > geo->blksize
790 *========================================================================*/
793 * Add a name to a Btree-format attribute list.
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).
799 * "Remote" attribute values confuse the issue and atomic rename operations
800 * add a whole extra layer of confusion on top of that.
802 STATIC int
803 xfs_attr_node_addname(xfs_da_args_t *args)
805 xfs_da_state_t *state;
806 xfs_da_state_blk_t *blk;
807 xfs_inode_t *dp;
808 xfs_mount_t *mp;
809 int retval, error;
811 trace_xfs_attr_node_addname(args);
814 * Fill in bucket of arguments/results/context to carry around.
816 dp = args->dp;
817 mp = dp->i_mount;
818 restart:
819 state = xfs_da_state_alloc();
820 state->args = args;
821 state->mp = mp;
824 * Search to see if name already exists, and get back a pointer
825 * to where it should go.
827 error = xfs_da3_node_lookup_int(state, &retval);
828 if (error)
829 goto out;
830 blk = &state->path.blk[ state->path.active-1 ];
831 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
832 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
833 goto out;
834 } else if (retval == -EEXIST) {
835 if (args->flags & ATTR_CREATE)
836 goto out;
838 trace_xfs_attr_node_replace(args);
840 /* save the attribute state for later removal*/
841 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
842 args->blkno2 = args->blkno; /* set 2nd entry info*/
843 args->index2 = args->index;
844 args->rmtblkno2 = args->rmtblkno;
845 args->rmtblkcnt2 = args->rmtblkcnt;
846 args->rmtvaluelen2 = args->rmtvaluelen;
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.
853 args->rmtblkno = 0;
854 args->rmtblkcnt = 0;
855 args->rmtvaluelen = 0;
858 retval = xfs_attr3_leaf_add(blk->bp, state->args);
859 if (retval == -ENOSPC) {
860 if (state->path.active == 1) {
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.
866 xfs_da_state_free(state);
867 state = NULL;
868 xfs_defer_init(args->dfops, args->firstblock);
869 error = xfs_attr3_leaf_to_node(args);
870 if (!error)
871 error = xfs_defer_finish(&args->trans,
872 args->dfops, dp);
873 if (error) {
874 args->trans = NULL;
875 xfs_defer_cancel(args->dfops);
876 goto out;
880 * Commit the node conversion and start the next
881 * trans in the chain.
883 error = xfs_trans_roll(&args->trans, dp);
884 if (error)
885 goto out;
887 goto restart;
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.
896 xfs_defer_init(args->dfops, args->firstblock);
897 error = xfs_da3_split(state);
898 if (!error)
899 error = xfs_defer_finish(&args->trans, args->dfops, dp);
900 if (error) {
901 args->trans = NULL;
902 xfs_defer_cancel(args->dfops);
903 goto out;
905 } else {
907 * Addition succeeded, update Btree hashvals.
909 xfs_da3_fixhashpath(state, &state->path);
913 * Kill the state structure, we're done with it and need to
914 * allow the buffers to come back later.
916 xfs_da_state_free(state);
917 state = NULL;
920 * Commit the leaf addition or btree split and start the next
921 * trans in the chain.
923 error = xfs_trans_roll(&args->trans, dp);
924 if (error)
925 goto out;
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.
933 if (args->rmtblkno > 0) {
934 error = xfs_attr_rmtval_set(args);
935 if (error)
936 return error;
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.
945 if (args->op_flags & XFS_DA_OP_RENAME) {
947 * In a separate transaction, set the incomplete flag on the
948 * "old" attr and clear the incomplete flag on the "new" attr.
950 error = xfs_attr3_leaf_flipflags(args);
951 if (error)
952 goto out;
955 * Dismantle the "old" attribute/value pair by removing
956 * a "remote" value (if it exists).
958 args->index = args->index2;
959 args->blkno = args->blkno2;
960 args->rmtblkno = args->rmtblkno2;
961 args->rmtblkcnt = args->rmtblkcnt2;
962 args->rmtvaluelen = args->rmtvaluelen2;
963 if (args->rmtblkno) {
964 error = xfs_attr_rmtval_remove(args);
965 if (error)
966 return error;
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.
974 args->flags |= XFS_ATTR_INCOMPLETE;
975 state = xfs_da_state_alloc();
976 state->args = args;
977 state->mp = mp;
978 state->inleaf = 0;
979 error = xfs_da3_node_lookup_int(state, &retval);
980 if (error)
981 goto out;
984 * Remove the name and update the hashvals in the tree.
986 blk = &state->path.blk[ state->path.active-1 ];
987 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
988 error = xfs_attr3_leaf_remove(blk->bp, args);
989 xfs_da3_fixhashpath(state, &state->path);
992 * Check to see if the tree needs to be collapsed.
994 if (retval && (state->path.active > 1)) {
995 xfs_defer_init(args->dfops, args->firstblock);
996 error = xfs_da3_join(state);
997 if (!error)
998 error = xfs_defer_finish(&args->trans,
999 args->dfops, dp);
1000 if (error) {
1001 args->trans = NULL;
1002 xfs_defer_cancel(args->dfops);
1003 goto out;
1008 * Commit and start the next trans in the chain.
1010 error = xfs_trans_roll(&args->trans, dp);
1011 if (error)
1012 goto out;
1014 } else if (args->rmtblkno > 0) {
1016 * Added a "remote" value, just clear the incomplete flag.
1018 error = xfs_attr3_leaf_clearflag(args);
1019 if (error)
1020 goto out;
1022 retval = error = 0;
1024 out:
1025 if (state)
1026 xfs_da_state_free(state);
1027 if (error)
1028 return error;
1029 return retval;
1033 * Remove a name from a B-tree attribute list.
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).
1039 STATIC int
1040 xfs_attr_node_removename(xfs_da_args_t *args)
1042 xfs_da_state_t *state;
1043 xfs_da_state_blk_t *blk;
1044 xfs_inode_t *dp;
1045 struct xfs_buf *bp;
1046 int retval, error, forkoff;
1048 trace_xfs_attr_node_removename(args);
1051 * Tie a string around our finger to remind us where we are.
1053 dp = args->dp;
1054 state = xfs_da_state_alloc();
1055 state->args = args;
1056 state->mp = dp->i_mount;
1059 * Search to see if name exists, and get back a pointer to it.
1061 error = xfs_da3_node_lookup_int(state, &retval);
1062 if (error || (retval != -EEXIST)) {
1063 if (error == 0)
1064 error = retval;
1065 goto out;
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.
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) {
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.
1082 error = xfs_attr_fillstate(state);
1083 if (error)
1084 goto out;
1087 * Mark the attribute as INCOMPLETE, then bunmapi() the
1088 * remote value.
1090 error = xfs_attr3_leaf_setflag(args);
1091 if (error)
1092 goto out;
1093 error = xfs_attr_rmtval_remove(args);
1094 if (error)
1095 goto out;
1098 * Refill the state structure with buffers, the prior calls
1099 * released our buffers.
1101 error = xfs_attr_refillstate(state);
1102 if (error)
1103 goto out;
1107 * Remove the name and update the hashvals in the tree.
1109 blk = &state->path.blk[ state->path.active-1 ];
1110 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1111 retval = xfs_attr3_leaf_remove(blk->bp, args);
1112 xfs_da3_fixhashpath(state, &state->path);
1115 * Check to see if the tree needs to be collapsed.
1117 if (retval && (state->path.active > 1)) {
1118 xfs_defer_init(args->dfops, args->firstblock);
1119 error = xfs_da3_join(state);
1120 if (!error)
1121 error = xfs_defer_finish(&args->trans, args->dfops, dp);
1122 if (error) {
1123 args->trans = NULL;
1124 xfs_defer_cancel(args->dfops);
1125 goto out;
1128 * Commit the Btree join operation and start a new trans.
1130 error = xfs_trans_roll(&args->trans, dp);
1131 if (error)
1132 goto out;
1136 * If the result is small enough, push it all into the inode.
1138 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1140 * Have to get rid of the copy of this dabuf in the state.
1142 ASSERT(state->path.active == 1);
1143 ASSERT(state->path.blk[0].bp);
1144 state->path.blk[0].bp = NULL;
1146 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1147 if (error)
1148 goto out;
1150 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1151 xfs_defer_init(args->dfops, args->firstblock);
1152 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1153 /* bp is gone due to xfs_da_shrink_inode */
1154 if (!error)
1155 error = xfs_defer_finish(&args->trans,
1156 args->dfops, dp);
1157 if (error) {
1158 args->trans = NULL;
1159 xfs_defer_cancel(args->dfops);
1160 goto out;
1162 } else
1163 xfs_trans_brelse(args->trans, bp);
1165 error = 0;
1167 out:
1168 xfs_da_state_free(state);
1169 return error;
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
1176 * after some set of transaction commits have released these buffers.
1178 STATIC int
1179 xfs_attr_fillstate(xfs_da_state_t *state)
1181 xfs_da_state_path_t *path;
1182 xfs_da_state_blk_t *blk;
1183 int level;
1185 trace_xfs_attr_fillstate(state->args);
1188 * Roll down the "path" in the state structure, storing the on-disk
1189 * block number for those buffers in the "path".
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) {
1195 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1196 blk->bp = NULL;
1197 } else {
1198 blk->disk_blkno = 0;
1203 * Roll down the "altpath" in the state structure, storing the on-disk
1204 * block number for those buffers in the "altpath".
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) {
1210 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1211 blk->bp = NULL;
1212 } else {
1213 blk->disk_blkno = 0;
1217 return 0;
1221 * Reattach the buffers to the state structure based on the disk block
1222 * numbers stored in the state structure.
1223 * This is done after some set of transaction commits have released those
1224 * buffers from our grip.
1226 STATIC int
1227 xfs_attr_refillstate(xfs_da_state_t *state)
1229 xfs_da_state_path_t *path;
1230 xfs_da_state_blk_t *blk;
1231 int level, error;
1233 trace_xfs_attr_refillstate(state->args);
1236 * Roll down the "path" in the state structure, storing the on-disk
1237 * block number for those buffers in the "path".
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) {
1243 error = xfs_da3_node_read(state->args->trans,
1244 state->args->dp,
1245 blk->blkno, blk->disk_blkno,
1246 &blk->bp, XFS_ATTR_FORK);
1247 if (error)
1248 return error;
1249 } else {
1250 blk->bp = NULL;
1255 * Roll down the "altpath" in the state structure, storing the on-disk
1256 * block number for those buffers in the "altpath".
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) {
1262 error = xfs_da3_node_read(state->args->trans,
1263 state->args->dp,
1264 blk->blkno, blk->disk_blkno,
1265 &blk->bp, XFS_ATTR_FORK);
1266 if (error)
1267 return error;
1268 } else {
1269 blk->bp = NULL;
1273 return 0;
1277 * Look up a filename in a node attribute list.
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.
1283 STATIC int
1284 xfs_attr_node_get(xfs_da_args_t *args)
1286 xfs_da_state_t *state;
1287 xfs_da_state_blk_t *blk;
1288 int error, retval;
1289 int i;
1291 trace_xfs_attr_node_get(args);
1293 state = xfs_da_state_alloc();
1294 state->args = args;
1295 state->mp = args->dp->i_mount;
1298 * Search to see if name exists, and get back a pointer to it.
1300 error = xfs_da3_node_lookup_int(state, &retval);
1301 if (error) {
1302 retval = error;
1303 } else if (retval == -EEXIST) {
1304 blk = &state->path.blk[ state->path.active-1 ];
1305 ASSERT(blk->bp != NULL);
1306 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1309 * Get the value, local or "remote"
1311 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1312 if (!retval && (args->rmtblkno > 0)
1313 && !(args->flags & ATTR_KERNOVAL)) {
1314 retval = xfs_attr_rmtval_get(args);
1319 * If not in a transaction, we have to release all the buffers.
1321 for (i = 0; i < state->path.active; i++) {
1322 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1323 state->path.blk[i].bp = NULL;
1326 xfs_da_state_free(state);
1327 return retval;