KVM: PPC: Book3S: Fix compile error that occurs with some gcc versions
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_attr.c
blobce4a34a2751d6ebe3ee0806d88ff58adf1023061
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 *========================================================================*/
117 /* Retrieve an extended attribute and its value. Must have ilock. */
119 xfs_attr_get_ilocked(
120 struct xfs_inode *ip,
121 struct xfs_da_args *args)
123 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
125 if (!xfs_inode_hasattr(ip))
126 return -ENOATTR;
127 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
128 return xfs_attr_shortform_getvalue(args);
129 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
130 return xfs_attr_leaf_get(args);
131 else
132 return xfs_attr_node_get(args);
135 /* Retrieve an extended attribute by name, and its value. */
137 xfs_attr_get(
138 struct xfs_inode *ip,
139 const unsigned char *name,
140 unsigned char *value,
141 int *valuelenp,
142 int flags)
144 struct xfs_da_args args;
145 uint lock_mode;
146 int error;
148 XFS_STATS_INC(ip->i_mount, xs_attr_get);
150 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
151 return -EIO;
153 error = xfs_attr_args_init(&args, ip, name, flags);
154 if (error)
155 return error;
157 args.value = value;
158 args.valuelen = *valuelenp;
159 /* Entirely possible to look up a name which doesn't exist */
160 args.op_flags = XFS_DA_OP_OKNOENT;
162 lock_mode = xfs_ilock_attr_map_shared(ip);
163 error = xfs_attr_get_ilocked(ip, &args);
164 xfs_iunlock(ip, lock_mode);
166 *valuelenp = args.valuelen;
167 return error == -EEXIST ? 0 : error;
171 * Calculate how many blocks we need for the new attribute,
173 STATIC int
174 xfs_attr_calc_size(
175 struct xfs_da_args *args,
176 int *local)
178 struct xfs_mount *mp = args->dp->i_mount;
179 int size;
180 int nblks;
183 * Determine space new attribute will use, and if it would be
184 * "local" or "remote" (note: local != inline).
186 size = xfs_attr_leaf_newentsize(args, local);
187 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
188 if (*local) {
189 if (size > (args->geo->blksize / 2)) {
190 /* Double split possible */
191 nblks *= 2;
193 } else {
195 * Out of line attribute, cannot double split, but
196 * make room for the attribute value itself.
198 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
199 nblks += dblocks;
200 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
203 return nblks;
207 xfs_attr_set(
208 struct xfs_inode *dp,
209 const unsigned char *name,
210 unsigned char *value,
211 int valuelen,
212 int flags)
214 struct xfs_mount *mp = dp->i_mount;
215 struct xfs_buf *leaf_bp = NULL;
216 struct xfs_da_args args;
217 struct xfs_defer_ops dfops;
218 struct xfs_trans_res tres;
219 xfs_fsblock_t firstblock;
220 int rsvd = (flags & ATTR_ROOT) != 0;
221 int error, err2, local;
223 XFS_STATS_INC(mp, xs_attr_set);
225 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
226 return -EIO;
228 error = xfs_attr_args_init(&args, dp, name, flags);
229 if (error)
230 return error;
232 args.value = value;
233 args.valuelen = valuelen;
234 args.firstblock = &firstblock;
235 args.dfops = &dfops;
236 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
237 args.total = xfs_attr_calc_size(&args, &local);
239 error = xfs_qm_dqattach(dp, 0);
240 if (error)
241 return error;
244 * If the inode doesn't have an attribute fork, add one.
245 * (inode must not be locked when we call this routine)
247 if (XFS_IFORK_Q(dp) == 0) {
248 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
249 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
251 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
252 if (error)
253 return error;
256 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
257 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
258 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
259 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
262 * Root fork attributes can use reserved data blocks for this
263 * operation if necessary
265 error = xfs_trans_alloc(mp, &tres, args.total, 0,
266 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
267 if (error)
268 return error;
270 xfs_ilock(dp, XFS_ILOCK_EXCL);
271 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
272 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
273 XFS_QMOPT_RES_REGBLKS);
274 if (error) {
275 xfs_iunlock(dp, XFS_ILOCK_EXCL);
276 xfs_trans_cancel(args.trans);
277 return error;
280 xfs_trans_ijoin(args.trans, dp, 0);
283 * If the attribute list is non-existent or a shortform list,
284 * upgrade it to a single-leaf-block attribute list.
286 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
287 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
288 dp->i_d.di_anextents == 0)) {
291 * Build initial attribute list (if required).
293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
294 xfs_attr_shortform_create(&args);
297 * Try to add the attr to the attribute list in
298 * the inode.
300 error = xfs_attr_shortform_addname(&args);
301 if (error != -ENOSPC) {
303 * Commit the shortform mods, and we're done.
304 * NOTE: this is also the error path (EEXIST, etc).
306 ASSERT(args.trans != NULL);
309 * If this is a synchronous mount, make sure that
310 * the transaction goes to disk before returning
311 * to the user.
313 if (mp->m_flags & XFS_MOUNT_WSYNC)
314 xfs_trans_set_sync(args.trans);
316 if (!error && (flags & ATTR_KERNOTIME) == 0) {
317 xfs_trans_ichgtime(args.trans, dp,
318 XFS_ICHGTIME_CHG);
320 err2 = xfs_trans_commit(args.trans);
321 xfs_iunlock(dp, XFS_ILOCK_EXCL);
323 return error ? error : err2;
327 * It won't fit in the shortform, transform to a leaf block.
328 * GROT: another possible req'mt for a double-split btree op.
330 xfs_defer_init(args.dfops, args.firstblock);
331 error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
332 if (error)
333 goto out_defer_cancel;
335 * Prevent the leaf buffer from being unlocked so that a
336 * concurrent AIL push cannot grab the half-baked leaf
337 * buffer and run into problems with the write verifier.
339 xfs_trans_bhold(args.trans, leaf_bp);
340 xfs_defer_bjoin(args.dfops, leaf_bp);
341 xfs_defer_ijoin(args.dfops, dp);
342 error = xfs_defer_finish(&args.trans, args.dfops);
343 if (error)
344 goto out_defer_cancel;
347 * Commit the leaf transformation. We'll need another (linked)
348 * transaction to add the new attribute to the leaf, which
349 * means that we have to hold & join the leaf buffer here too.
351 error = xfs_trans_roll_inode(&args.trans, dp);
352 if (error)
353 goto out;
354 xfs_trans_bjoin(args.trans, leaf_bp);
355 leaf_bp = NULL;
358 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
359 error = xfs_attr_leaf_addname(&args);
360 else
361 error = xfs_attr_node_addname(&args);
362 if (error)
363 goto out;
366 * If this is a synchronous mount, make sure that the
367 * transaction goes to disk before returning to the user.
369 if (mp->m_flags & XFS_MOUNT_WSYNC)
370 xfs_trans_set_sync(args.trans);
372 if ((flags & ATTR_KERNOTIME) == 0)
373 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
376 * Commit the last in the sequence of transactions.
378 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
379 error = xfs_trans_commit(args.trans);
380 xfs_iunlock(dp, XFS_ILOCK_EXCL);
382 return error;
384 out_defer_cancel:
385 xfs_defer_cancel(&dfops);
386 out:
387 if (leaf_bp)
388 xfs_trans_brelse(args.trans, leaf_bp);
389 if (args.trans)
390 xfs_trans_cancel(args.trans);
391 xfs_iunlock(dp, XFS_ILOCK_EXCL);
392 return error;
396 * Generic handler routine to remove a name from an attribute list.
397 * Transitions attribute list from Btree to shortform as necessary.
400 xfs_attr_remove(
401 struct xfs_inode *dp,
402 const unsigned char *name,
403 int flags)
405 struct xfs_mount *mp = dp->i_mount;
406 struct xfs_da_args args;
407 struct xfs_defer_ops dfops;
408 xfs_fsblock_t firstblock;
409 int error;
411 XFS_STATS_INC(mp, xs_attr_remove);
413 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
414 return -EIO;
416 error = xfs_attr_args_init(&args, dp, name, flags);
417 if (error)
418 return error;
420 args.firstblock = &firstblock;
421 args.dfops = &dfops;
424 * we have no control over the attribute names that userspace passes us
425 * to remove, so we have to allow the name lookup prior to attribute
426 * removal to fail.
428 args.op_flags = XFS_DA_OP_OKNOENT;
430 error = xfs_qm_dqattach(dp, 0);
431 if (error)
432 return error;
435 * Root fork attributes can use reserved data blocks for this
436 * operation if necessary
438 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
439 XFS_ATTRRM_SPACE_RES(mp), 0,
440 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
441 &args.trans);
442 if (error)
443 return error;
445 xfs_ilock(dp, XFS_ILOCK_EXCL);
447 * No need to make quota reservations here. We expect to release some
448 * blocks not allocate in the common case.
450 xfs_trans_ijoin(args.trans, dp, 0);
452 if (!xfs_inode_hasattr(dp)) {
453 error = -ENOATTR;
454 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
455 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
456 error = xfs_attr_shortform_remove(&args);
457 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
458 error = xfs_attr_leaf_removename(&args);
459 } else {
460 error = xfs_attr_node_removename(&args);
463 if (error)
464 goto out;
467 * If this is a synchronous mount, make sure that the
468 * transaction goes to disk before returning to the user.
470 if (mp->m_flags & XFS_MOUNT_WSYNC)
471 xfs_trans_set_sync(args.trans);
473 if ((flags & ATTR_KERNOTIME) == 0)
474 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
477 * Commit the last in the sequence of transactions.
479 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
480 error = xfs_trans_commit(args.trans);
481 xfs_iunlock(dp, XFS_ILOCK_EXCL);
483 return error;
485 out:
486 if (args.trans)
487 xfs_trans_cancel(args.trans);
488 xfs_iunlock(dp, XFS_ILOCK_EXCL);
489 return error;
492 /*========================================================================
493 * External routines when attribute list is inside the inode
494 *========================================================================*/
497 * Add a name to the shortform attribute list structure
498 * This is the external routine.
500 STATIC int
501 xfs_attr_shortform_addname(xfs_da_args_t *args)
503 int newsize, forkoff, retval;
505 trace_xfs_attr_sf_addname(args);
507 retval = xfs_attr_shortform_lookup(args);
508 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
509 return retval;
510 } else if (retval == -EEXIST) {
511 if (args->flags & ATTR_CREATE)
512 return retval;
513 retval = xfs_attr_shortform_remove(args);
514 ASSERT(retval == 0);
517 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
518 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
519 return -ENOSPC;
521 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
522 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
524 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
525 if (!forkoff)
526 return -ENOSPC;
528 xfs_attr_shortform_add(args, forkoff);
529 return 0;
533 /*========================================================================
534 * External routines when attribute list is one block
535 *========================================================================*/
538 * Add a name to the leaf attribute list structure
540 * This leaf block cannot have a "remote" value, we only call this routine
541 * if bmap_one_block() says there is only one block (ie: no remote blks).
543 STATIC int
544 xfs_attr_leaf_addname(xfs_da_args_t *args)
546 xfs_inode_t *dp;
547 struct xfs_buf *bp;
548 int retval, error, forkoff;
550 trace_xfs_attr_leaf_addname(args);
553 * Read the (only) block in the attribute list in.
555 dp = args->dp;
556 args->blkno = 0;
557 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
558 if (error)
559 return error;
562 * Look up the given attribute in the leaf block. Figure out if
563 * the given flags produce an error or call for an atomic rename.
565 retval = xfs_attr3_leaf_lookup_int(bp, args);
566 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
567 xfs_trans_brelse(args->trans, bp);
568 return retval;
569 } else if (retval == -EEXIST) {
570 if (args->flags & ATTR_CREATE) { /* pure create op */
571 xfs_trans_brelse(args->trans, bp);
572 return retval;
575 trace_xfs_attr_leaf_replace(args);
577 /* save the attribute state for later removal*/
578 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
579 args->blkno2 = args->blkno; /* set 2nd entry info*/
580 args->index2 = args->index;
581 args->rmtblkno2 = args->rmtblkno;
582 args->rmtblkcnt2 = args->rmtblkcnt;
583 args->rmtvaluelen2 = args->rmtvaluelen;
586 * clear the remote attr state now that it is saved so that the
587 * values reflect the state of the attribute we are about to
588 * add, not the attribute we just found and will remove later.
590 args->rmtblkno = 0;
591 args->rmtblkcnt = 0;
592 args->rmtvaluelen = 0;
596 * Add the attribute to the leaf block, transitioning to a Btree
597 * if required.
599 retval = xfs_attr3_leaf_add(bp, args);
600 if (retval == -ENOSPC) {
602 * Promote the attribute list to the Btree format, then
603 * Commit that transaction so that the node_addname() call
604 * can manage its own transactions.
606 xfs_defer_init(args->dfops, args->firstblock);
607 error = xfs_attr3_leaf_to_node(args);
608 if (error)
609 goto out_defer_cancel;
610 xfs_defer_ijoin(args->dfops, dp);
611 error = xfs_defer_finish(&args->trans, args->dfops);
612 if (error)
613 goto out_defer_cancel;
616 * Commit the current trans (including the inode) and start
617 * a new one.
619 error = xfs_trans_roll_inode(&args->trans, dp);
620 if (error)
621 return error;
624 * Fob the whole rest of the problem off on the Btree code.
626 error = xfs_attr_node_addname(args);
627 return error;
631 * Commit the transaction that added the attr name so that
632 * later routines can manage their own transactions.
634 error = xfs_trans_roll_inode(&args->trans, dp);
635 if (error)
636 return error;
639 * If there was an out-of-line value, allocate the blocks we
640 * identified for its storage and copy the value. This is done
641 * after we create the attribute so that we don't overflow the
642 * maximum size of a transaction and/or hit a deadlock.
644 if (args->rmtblkno > 0) {
645 error = xfs_attr_rmtval_set(args);
646 if (error)
647 return error;
651 * If this is an atomic rename operation, we must "flip" the
652 * incomplete flags on the "new" and "old" attribute/value pairs
653 * so that one disappears and one appears atomically. Then we
654 * must remove the "old" attribute/value pair.
656 if (args->op_flags & XFS_DA_OP_RENAME) {
658 * In a separate transaction, set the incomplete flag on the
659 * "old" attr and clear the incomplete flag on the "new" attr.
661 error = xfs_attr3_leaf_flipflags(args);
662 if (error)
663 return error;
666 * Dismantle the "old" attribute/value pair by removing
667 * a "remote" value (if it exists).
669 args->index = args->index2;
670 args->blkno = args->blkno2;
671 args->rmtblkno = args->rmtblkno2;
672 args->rmtblkcnt = args->rmtblkcnt2;
673 args->rmtvaluelen = args->rmtvaluelen2;
674 if (args->rmtblkno) {
675 error = xfs_attr_rmtval_remove(args);
676 if (error)
677 return error;
681 * Read in the block containing the "old" attr, then
682 * remove the "old" attr from that block (neat, huh!)
684 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
685 -1, &bp);
686 if (error)
687 return error;
689 xfs_attr3_leaf_remove(bp, args);
692 * If the result is small enough, shrink it all into the inode.
694 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
695 xfs_defer_init(args->dfops, args->firstblock);
696 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
697 /* bp is gone due to xfs_da_shrink_inode */
698 if (error)
699 goto out_defer_cancel;
700 xfs_defer_ijoin(args->dfops, dp);
701 error = xfs_defer_finish(&args->trans, args->dfops);
702 if (error)
703 goto out_defer_cancel;
707 * Commit the remove and start the next trans in series.
709 error = xfs_trans_roll_inode(&args->trans, dp);
711 } else if (args->rmtblkno > 0) {
713 * Added a "remote" value, just clear the incomplete flag.
715 error = xfs_attr3_leaf_clearflag(args);
717 return error;
718 out_defer_cancel:
719 xfs_defer_cancel(args->dfops);
720 return error;
724 * Remove a name from the leaf attribute list structure
726 * This leaf block cannot have a "remote" value, we only call this routine
727 * if bmap_one_block() says there is only one block (ie: no remote blks).
729 STATIC int
730 xfs_attr_leaf_removename(xfs_da_args_t *args)
732 xfs_inode_t *dp;
733 struct xfs_buf *bp;
734 int error, forkoff;
736 trace_xfs_attr_leaf_removename(args);
739 * Remove the attribute.
741 dp = args->dp;
742 args->blkno = 0;
743 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
744 if (error)
745 return error;
747 error = xfs_attr3_leaf_lookup_int(bp, args);
748 if (error == -ENOATTR) {
749 xfs_trans_brelse(args->trans, bp);
750 return error;
753 xfs_attr3_leaf_remove(bp, args);
756 * If the result is small enough, shrink it all into the inode.
758 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
759 xfs_defer_init(args->dfops, args->firstblock);
760 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
761 /* bp is gone due to xfs_da_shrink_inode */
762 if (error)
763 goto out_defer_cancel;
764 xfs_defer_ijoin(args->dfops, dp);
765 error = xfs_defer_finish(&args->trans, args->dfops);
766 if (error)
767 goto out_defer_cancel;
769 return 0;
770 out_defer_cancel:
771 xfs_defer_cancel(args->dfops);
772 return error;
776 * Look up a name in a leaf attribute list structure.
778 * This leaf block cannot have a "remote" value, we only call this routine
779 * if bmap_one_block() says there is only one block (ie: no remote blks).
781 STATIC int
782 xfs_attr_leaf_get(xfs_da_args_t *args)
784 struct xfs_buf *bp;
785 int error;
787 trace_xfs_attr_leaf_get(args);
789 args->blkno = 0;
790 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
791 if (error)
792 return error;
794 error = xfs_attr3_leaf_lookup_int(bp, args);
795 if (error != -EEXIST) {
796 xfs_trans_brelse(args->trans, bp);
797 return error;
799 error = xfs_attr3_leaf_getvalue(bp, args);
800 xfs_trans_brelse(args->trans, bp);
801 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
802 error = xfs_attr_rmtval_get(args);
804 return error;
807 /*========================================================================
808 * External routines when attribute list size > geo->blksize
809 *========================================================================*/
812 * Add a name to a Btree-format attribute list.
814 * This will involve walking down the Btree, and may involve splitting
815 * leaf nodes and even splitting intermediate nodes up to and including
816 * the root node (a special case of an intermediate node).
818 * "Remote" attribute values confuse the issue and atomic rename operations
819 * add a whole extra layer of confusion on top of that.
821 STATIC int
822 xfs_attr_node_addname(xfs_da_args_t *args)
824 xfs_da_state_t *state;
825 xfs_da_state_blk_t *blk;
826 xfs_inode_t *dp;
827 xfs_mount_t *mp;
828 int retval, error;
830 trace_xfs_attr_node_addname(args);
833 * Fill in bucket of arguments/results/context to carry around.
835 dp = args->dp;
836 mp = dp->i_mount;
837 restart:
838 state = xfs_da_state_alloc();
839 state->args = args;
840 state->mp = mp;
843 * Search to see if name already exists, and get back a pointer
844 * to where it should go.
846 error = xfs_da3_node_lookup_int(state, &retval);
847 if (error)
848 goto out;
849 blk = &state->path.blk[ state->path.active-1 ];
850 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
851 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
852 goto out;
853 } else if (retval == -EEXIST) {
854 if (args->flags & ATTR_CREATE)
855 goto out;
857 trace_xfs_attr_node_replace(args);
859 /* save the attribute state for later removal*/
860 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
861 args->blkno2 = args->blkno; /* set 2nd entry info*/
862 args->index2 = args->index;
863 args->rmtblkno2 = args->rmtblkno;
864 args->rmtblkcnt2 = args->rmtblkcnt;
865 args->rmtvaluelen2 = args->rmtvaluelen;
868 * clear the remote attr state now that it is saved so that the
869 * values reflect the state of the attribute we are about to
870 * add, not the attribute we just found and will remove later.
872 args->rmtblkno = 0;
873 args->rmtblkcnt = 0;
874 args->rmtvaluelen = 0;
877 retval = xfs_attr3_leaf_add(blk->bp, state->args);
878 if (retval == -ENOSPC) {
879 if (state->path.active == 1) {
881 * Its really a single leaf node, but it had
882 * out-of-line values so it looked like it *might*
883 * have been a b-tree.
885 xfs_da_state_free(state);
886 state = NULL;
887 xfs_defer_init(args->dfops, args->firstblock);
888 error = xfs_attr3_leaf_to_node(args);
889 if (error)
890 goto out_defer_cancel;
891 xfs_defer_ijoin(args->dfops, dp);
892 error = xfs_defer_finish(&args->trans, args->dfops);
893 if (error)
894 goto out_defer_cancel;
897 * Commit the node conversion and start the next
898 * trans in the chain.
900 error = xfs_trans_roll_inode(&args->trans, dp);
901 if (error)
902 goto out;
904 goto restart;
908 * Split as many Btree elements as required.
909 * This code tracks the new and old attr's location
910 * in the index/blkno/rmtblkno/rmtblkcnt fields and
911 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
913 xfs_defer_init(args->dfops, args->firstblock);
914 error = xfs_da3_split(state);
915 if (error)
916 goto out_defer_cancel;
917 xfs_defer_ijoin(args->dfops, dp);
918 error = xfs_defer_finish(&args->trans, args->dfops);
919 if (error)
920 goto out_defer_cancel;
921 } else {
923 * Addition succeeded, update Btree hashvals.
925 xfs_da3_fixhashpath(state, &state->path);
929 * Kill the state structure, we're done with it and need to
930 * allow the buffers to come back later.
932 xfs_da_state_free(state);
933 state = NULL;
936 * Commit the leaf addition or btree split and start the next
937 * trans in the chain.
939 error = xfs_trans_roll_inode(&args->trans, dp);
940 if (error)
941 goto out;
944 * If there was an out-of-line value, allocate the blocks we
945 * identified for its storage and copy the value. This is done
946 * after we create the attribute so that we don't overflow the
947 * maximum size of a transaction and/or hit a deadlock.
949 if (args->rmtblkno > 0) {
950 error = xfs_attr_rmtval_set(args);
951 if (error)
952 return error;
956 * If this is an atomic rename operation, we must "flip" the
957 * incomplete flags on the "new" and "old" attribute/value pairs
958 * so that one disappears and one appears atomically. Then we
959 * must remove the "old" attribute/value pair.
961 if (args->op_flags & XFS_DA_OP_RENAME) {
963 * In a separate transaction, set the incomplete flag on the
964 * "old" attr and clear the incomplete flag on the "new" attr.
966 error = xfs_attr3_leaf_flipflags(args);
967 if (error)
968 goto out;
971 * Dismantle the "old" attribute/value pair by removing
972 * a "remote" value (if it exists).
974 args->index = args->index2;
975 args->blkno = args->blkno2;
976 args->rmtblkno = args->rmtblkno2;
977 args->rmtblkcnt = args->rmtblkcnt2;
978 args->rmtvaluelen = args->rmtvaluelen2;
979 if (args->rmtblkno) {
980 error = xfs_attr_rmtval_remove(args);
981 if (error)
982 return error;
986 * Re-find the "old" attribute entry after any split ops.
987 * The INCOMPLETE flag means that we will find the "old"
988 * attr, not the "new" one.
990 args->flags |= XFS_ATTR_INCOMPLETE;
991 state = xfs_da_state_alloc();
992 state->args = args;
993 state->mp = mp;
994 state->inleaf = 0;
995 error = xfs_da3_node_lookup_int(state, &retval);
996 if (error)
997 goto out;
1000 * Remove the name and update the hashvals in the tree.
1002 blk = &state->path.blk[ state->path.active-1 ];
1003 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1004 error = xfs_attr3_leaf_remove(blk->bp, args);
1005 xfs_da3_fixhashpath(state, &state->path);
1008 * Check to see if the tree needs to be collapsed.
1010 if (retval && (state->path.active > 1)) {
1011 xfs_defer_init(args->dfops, args->firstblock);
1012 error = xfs_da3_join(state);
1013 if (error)
1014 goto out_defer_cancel;
1015 xfs_defer_ijoin(args->dfops, dp);
1016 error = xfs_defer_finish(&args->trans, args->dfops);
1017 if (error)
1018 goto out_defer_cancel;
1022 * Commit and start the next trans in the chain.
1024 error = xfs_trans_roll_inode(&args->trans, dp);
1025 if (error)
1026 goto out;
1028 } else if (args->rmtblkno > 0) {
1030 * Added a "remote" value, just clear the incomplete flag.
1032 error = xfs_attr3_leaf_clearflag(args);
1033 if (error)
1034 goto out;
1036 retval = error = 0;
1038 out:
1039 if (state)
1040 xfs_da_state_free(state);
1041 if (error)
1042 return error;
1043 return retval;
1044 out_defer_cancel:
1045 xfs_defer_cancel(args->dfops);
1046 goto out;
1050 * Remove a name from a B-tree attribute list.
1052 * This will involve walking down the Btree, and may involve joining
1053 * leaf nodes and even joining intermediate nodes up to and including
1054 * the root node (a special case of an intermediate node).
1056 STATIC int
1057 xfs_attr_node_removename(xfs_da_args_t *args)
1059 xfs_da_state_t *state;
1060 xfs_da_state_blk_t *blk;
1061 xfs_inode_t *dp;
1062 struct xfs_buf *bp;
1063 int retval, error, forkoff;
1065 trace_xfs_attr_node_removename(args);
1068 * Tie a string around our finger to remind us where we are.
1070 dp = args->dp;
1071 state = xfs_da_state_alloc();
1072 state->args = args;
1073 state->mp = dp->i_mount;
1076 * Search to see if name exists, and get back a pointer to it.
1078 error = xfs_da3_node_lookup_int(state, &retval);
1079 if (error || (retval != -EEXIST)) {
1080 if (error == 0)
1081 error = retval;
1082 goto out;
1086 * If there is an out-of-line value, de-allocate the blocks.
1087 * This is done before we remove the attribute so that we don't
1088 * overflow the maximum size of a transaction and/or hit a deadlock.
1090 blk = &state->path.blk[ state->path.active-1 ];
1091 ASSERT(blk->bp != NULL);
1092 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1093 if (args->rmtblkno > 0) {
1095 * Fill in disk block numbers in the state structure
1096 * so that we can get the buffers back after we commit
1097 * several transactions in the following calls.
1099 error = xfs_attr_fillstate(state);
1100 if (error)
1101 goto out;
1104 * Mark the attribute as INCOMPLETE, then bunmapi() the
1105 * remote value.
1107 error = xfs_attr3_leaf_setflag(args);
1108 if (error)
1109 goto out;
1110 error = xfs_attr_rmtval_remove(args);
1111 if (error)
1112 goto out;
1115 * Refill the state structure with buffers, the prior calls
1116 * released our buffers.
1118 error = xfs_attr_refillstate(state);
1119 if (error)
1120 goto out;
1124 * Remove the name and update the hashvals in the tree.
1126 blk = &state->path.blk[ state->path.active-1 ];
1127 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1128 retval = xfs_attr3_leaf_remove(blk->bp, args);
1129 xfs_da3_fixhashpath(state, &state->path);
1132 * Check to see if the tree needs to be collapsed.
1134 if (retval && (state->path.active > 1)) {
1135 xfs_defer_init(args->dfops, args->firstblock);
1136 error = xfs_da3_join(state);
1137 if (error)
1138 goto out_defer_cancel;
1139 xfs_defer_ijoin(args->dfops, dp);
1140 error = xfs_defer_finish(&args->trans, args->dfops);
1141 if (error)
1142 goto out_defer_cancel;
1144 * Commit the Btree join operation and start a new trans.
1146 error = xfs_trans_roll_inode(&args->trans, dp);
1147 if (error)
1148 goto out;
1152 * If the result is small enough, push it all into the inode.
1154 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1156 * Have to get rid of the copy of this dabuf in the state.
1158 ASSERT(state->path.active == 1);
1159 ASSERT(state->path.blk[0].bp);
1160 state->path.blk[0].bp = NULL;
1162 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1163 if (error)
1164 goto out;
1166 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1167 xfs_defer_init(args->dfops, args->firstblock);
1168 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1169 /* bp is gone due to xfs_da_shrink_inode */
1170 if (error)
1171 goto out_defer_cancel;
1172 xfs_defer_ijoin(args->dfops, dp);
1173 error = xfs_defer_finish(&args->trans, args->dfops);
1174 if (error)
1175 goto out_defer_cancel;
1176 } else
1177 xfs_trans_brelse(args->trans, bp);
1179 error = 0;
1181 out:
1182 xfs_da_state_free(state);
1183 return error;
1184 out_defer_cancel:
1185 xfs_defer_cancel(args->dfops);
1186 goto out;
1190 * Fill in the disk block numbers in the state structure for the buffers
1191 * that are attached to the state structure.
1192 * This is done so that we can quickly reattach ourselves to those buffers
1193 * after some set of transaction commits have released these buffers.
1195 STATIC int
1196 xfs_attr_fillstate(xfs_da_state_t *state)
1198 xfs_da_state_path_t *path;
1199 xfs_da_state_blk_t *blk;
1200 int level;
1202 trace_xfs_attr_fillstate(state->args);
1205 * Roll down the "path" in the state structure, storing the on-disk
1206 * block number for those buffers in the "path".
1208 path = &state->path;
1209 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1210 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1211 if (blk->bp) {
1212 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1213 blk->bp = NULL;
1214 } else {
1215 blk->disk_blkno = 0;
1220 * Roll down the "altpath" in the state structure, storing the on-disk
1221 * block number for those buffers in the "altpath".
1223 path = &state->altpath;
1224 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1225 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1226 if (blk->bp) {
1227 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1228 blk->bp = NULL;
1229 } else {
1230 blk->disk_blkno = 0;
1234 return 0;
1238 * Reattach the buffers to the state structure based on the disk block
1239 * numbers stored in the state structure.
1240 * This is done after some set of transaction commits have released those
1241 * buffers from our grip.
1243 STATIC int
1244 xfs_attr_refillstate(xfs_da_state_t *state)
1246 xfs_da_state_path_t *path;
1247 xfs_da_state_blk_t *blk;
1248 int level, error;
1250 trace_xfs_attr_refillstate(state->args);
1253 * Roll down the "path" in the state structure, storing the on-disk
1254 * block number for those buffers in the "path".
1256 path = &state->path;
1257 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1258 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1259 if (blk->disk_blkno) {
1260 error = xfs_da3_node_read(state->args->trans,
1261 state->args->dp,
1262 blk->blkno, blk->disk_blkno,
1263 &blk->bp, XFS_ATTR_FORK);
1264 if (error)
1265 return error;
1266 } else {
1267 blk->bp = NULL;
1272 * Roll down the "altpath" in the state structure, storing the on-disk
1273 * block number for those buffers in the "altpath".
1275 path = &state->altpath;
1276 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1277 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1278 if (blk->disk_blkno) {
1279 error = xfs_da3_node_read(state->args->trans,
1280 state->args->dp,
1281 blk->blkno, blk->disk_blkno,
1282 &blk->bp, XFS_ATTR_FORK);
1283 if (error)
1284 return error;
1285 } else {
1286 blk->bp = NULL;
1290 return 0;
1294 * Look up a filename in a node attribute list.
1296 * This routine gets called for any attribute fork that has more than one
1297 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1298 * "remote" values taking up more blocks.
1300 STATIC int
1301 xfs_attr_node_get(xfs_da_args_t *args)
1303 xfs_da_state_t *state;
1304 xfs_da_state_blk_t *blk;
1305 int error, retval;
1306 int i;
1308 trace_xfs_attr_node_get(args);
1310 state = xfs_da_state_alloc();
1311 state->args = args;
1312 state->mp = args->dp->i_mount;
1315 * Search to see if name exists, and get back a pointer to it.
1317 error = xfs_da3_node_lookup_int(state, &retval);
1318 if (error) {
1319 retval = error;
1320 } else if (retval == -EEXIST) {
1321 blk = &state->path.blk[ state->path.active-1 ];
1322 ASSERT(blk->bp != NULL);
1323 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1326 * Get the value, local or "remote"
1328 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1329 if (!retval && (args->rmtblkno > 0)
1330 && !(args->flags & ATTR_KERNOVAL)) {
1331 retval = xfs_attr_rmtval_get(args);
1336 * If not in a transaction, we have to release all the buffers.
1338 for (i = 0; i < state->path.active; i++) {
1339 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1340 state->path.blk[i].bp = NULL;
1343 xfs_da_state_free(state);
1344 return retval;