HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_attr.c
blob5d8d12746e6e33e9a88b282131f180dabea4efcd
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_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_attr_sf.h"
29 #include "xfs_inode.h"
30 #include "xfs_alloc.h"
31 #include "xfs_trans.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_bmap.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_attr.h"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_attr_remote.h"
39 #include "xfs_error.h"
40 #include "xfs_quota.h"
41 #include "xfs_trans_space.h"
42 #include "xfs_trace.h"
45 * xfs_attr.c
47 * Provide the external interfaces to manage attribute lists.
50 /*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
55 * Internal routines when attribute list fits inside the inode.
57 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
60 * Internal routines when attribute list is one block.
62 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
63 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
64 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
67 * Internal routines when attribute list is more than one block.
69 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
70 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
71 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
72 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
73 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
76 STATIC int
77 xfs_attr_args_init(
78 struct xfs_da_args *args,
79 struct xfs_inode *dp,
80 const unsigned char *name,
81 int flags)
84 if (!name)
85 return -EINVAL;
87 memset(args, 0, sizeof(*args));
88 args->geo = dp->i_mount->m_attr_geo;
89 args->whichfork = XFS_ATTR_FORK;
90 args->dp = dp;
91 args->flags = flags;
92 args->name = name;
93 args->namelen = strlen((const char *)name);
94 if (args->namelen >= MAXNAMELEN)
95 return -EFAULT; /* match IRIX behaviour */
97 args->hashval = xfs_da_hashname(args->name, args->namelen);
98 return 0;
102 xfs_inode_hasattr(
103 struct xfs_inode *ip)
105 if (!XFS_IFORK_Q(ip) ||
106 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
107 ip->i_d.di_anextents == 0))
108 return 0;
109 return 1;
112 /*========================================================================
113 * Overall external interface routines.
114 *========================================================================*/
117 xfs_attr_get(
118 struct xfs_inode *ip,
119 const unsigned char *name,
120 unsigned char *value,
121 int *valuelenp,
122 int flags)
124 struct xfs_da_args args;
125 uint lock_mode;
126 int error;
128 XFS_STATS_INC(ip->i_mount, xs_attr_get);
130 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
131 return -EIO;
133 error = xfs_attr_args_init(&args, ip, name, flags);
134 if (error)
135 return error;
137 args.value = value;
138 args.valuelen = *valuelenp;
139 /* Entirely possible to look up a name which doesn't exist */
140 args.op_flags = XFS_DA_OP_OKNOENT;
142 lock_mode = xfs_ilock_attr_map_shared(ip);
143 if (!xfs_inode_hasattr(ip))
144 error = -ENOATTR;
145 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
146 error = xfs_attr_shortform_getvalue(&args);
147 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
148 error = xfs_attr_leaf_get(&args);
149 else
150 error = xfs_attr_node_get(&args);
151 xfs_iunlock(ip, lock_mode);
153 *valuelenp = args.valuelen;
154 return error == -EEXIST ? 0 : error;
158 * Calculate how many blocks we need for the new attribute,
160 STATIC int
161 xfs_attr_calc_size(
162 struct xfs_da_args *args,
163 int *local)
165 struct xfs_mount *mp = args->dp->i_mount;
166 int size;
167 int nblks;
170 * Determine space new attribute will use, and if it would be
171 * "local" or "remote" (note: local != inline).
173 size = xfs_attr_leaf_newentsize(args, local);
174 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
175 if (*local) {
176 if (size > (args->geo->blksize / 2)) {
177 /* Double split possible */
178 nblks *= 2;
180 } else {
182 * Out of line attribute, cannot double split, but
183 * make room for the attribute value itself.
185 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
186 nblks += dblocks;
187 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
190 return nblks;
194 xfs_attr_set(
195 struct xfs_inode *dp,
196 const unsigned char *name,
197 unsigned char *value,
198 int valuelen,
199 int flags)
201 struct xfs_mount *mp = dp->i_mount;
202 struct xfs_da_args args;
203 struct xfs_bmap_free flist;
204 struct xfs_trans_res tres;
205 xfs_fsblock_t firstblock;
206 int rsvd = (flags & ATTR_ROOT) != 0;
207 int error, err2, committed, local;
209 XFS_STATS_INC(mp, xs_attr_set);
211 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
212 return -EIO;
214 error = xfs_attr_args_init(&args, dp, name, flags);
215 if (error)
216 return error;
218 args.value = value;
219 args.valuelen = valuelen;
220 args.firstblock = &firstblock;
221 args.flist = &flist;
222 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
223 args.total = xfs_attr_calc_size(&args, &local);
225 error = xfs_qm_dqattach(dp, 0);
226 if (error)
227 return error;
230 * If the inode doesn't have an attribute fork, add one.
231 * (inode must not be locked when we call this routine)
233 if (XFS_IFORK_Q(dp) == 0) {
234 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
235 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
237 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
238 if (error)
239 return error;
243 * Start our first transaction of the day.
245 * All future transactions during this code must be "chained" off
246 * this one via the trans_dup() call. All transactions will contain
247 * the inode, and the inode will always be marked with trans_ihold().
248 * Since the inode will be locked in all transactions, we must log
249 * the inode in every transaction to let it float upward through
250 * the log.
252 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
255 * Root fork attributes can use reserved data blocks for this
256 * operation if necessary
259 if (rsvd)
260 args.trans->t_flags |= XFS_TRANS_RESERVE;
262 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
263 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
264 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
265 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
266 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
267 if (error) {
268 xfs_trans_cancel(args.trans);
269 return error;
271 xfs_ilock(dp, XFS_ILOCK_EXCL);
273 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
274 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
275 XFS_QMOPT_RES_REGBLKS);
276 if (error) {
277 xfs_iunlock(dp, XFS_ILOCK_EXCL);
278 xfs_trans_cancel(args.trans);
279 return error;
282 xfs_trans_ijoin(args.trans, dp, 0);
285 * If the attribute list is non-existent or a shortform list,
286 * upgrade it to a single-leaf-block attribute list.
288 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
289 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
290 dp->i_d.di_anextents == 0)) {
293 * Build initial attribute list (if required).
295 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
296 xfs_attr_shortform_create(&args);
299 * Try to add the attr to the attribute list in
300 * the inode.
302 error = xfs_attr_shortform_addname(&args);
303 if (error != -ENOSPC) {
305 * Commit the shortform mods, and we're done.
306 * NOTE: this is also the error path (EEXIST, etc).
308 ASSERT(args.trans != NULL);
311 * If this is a synchronous mount, make sure that
312 * the transaction goes to disk before returning
313 * to the user.
315 if (mp->m_flags & XFS_MOUNT_WSYNC)
316 xfs_trans_set_sync(args.trans);
318 if (!error && (flags & ATTR_KERNOTIME) == 0) {
319 xfs_trans_ichgtime(args.trans, dp,
320 XFS_ICHGTIME_CHG);
322 err2 = xfs_trans_commit(args.trans);
323 xfs_iunlock(dp, XFS_ILOCK_EXCL);
325 return error ? error : err2;
329 * It won't fit in the shortform, transform to a leaf block.
330 * GROT: another possible req'mt for a double-split btree op.
332 xfs_bmap_init(args.flist, args.firstblock);
333 error = xfs_attr_shortform_to_leaf(&args);
334 if (!error) {
335 error = xfs_bmap_finish(&args.trans, args.flist,
336 &committed);
338 if (error) {
339 ASSERT(committed);
340 args.trans = NULL;
341 xfs_bmap_cancel(&flist);
342 goto out;
346 * bmap_finish() may have committed the last trans and started
347 * a new one. We need the inode to be in all transactions.
349 if (committed)
350 xfs_trans_ijoin(args.trans, dp, 0);
353 * Commit the leaf transformation. We'll need another (linked)
354 * transaction to add the new attribute to the leaf.
357 error = xfs_trans_roll(&args.trans, dp);
358 if (error)
359 goto out;
363 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
364 error = xfs_attr_leaf_addname(&args);
365 else
366 error = xfs_attr_node_addname(&args);
367 if (error)
368 goto out;
371 * If this is a synchronous mount, make sure that the
372 * transaction goes to disk before returning to the user.
374 if (mp->m_flags & XFS_MOUNT_WSYNC)
375 xfs_trans_set_sync(args.trans);
377 if ((flags & ATTR_KERNOTIME) == 0)
378 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
381 * Commit the last in the sequence of transactions.
383 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
384 error = xfs_trans_commit(args.trans);
385 xfs_iunlock(dp, XFS_ILOCK_EXCL);
387 return error;
389 out:
390 if (args.trans)
391 xfs_trans_cancel(args.trans);
392 xfs_iunlock(dp, XFS_ILOCK_EXCL);
393 return error;
397 * Generic handler routine to remove a name from an attribute list.
398 * Transitions attribute list from Btree to shortform as necessary.
401 xfs_attr_remove(
402 struct xfs_inode *dp,
403 const unsigned char *name,
404 int flags)
406 struct xfs_mount *mp = dp->i_mount;
407 struct xfs_da_args args;
408 struct xfs_bmap_free flist;
409 xfs_fsblock_t firstblock;
410 int error;
412 XFS_STATS_INC(mp, xs_attr_remove);
414 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
415 return -EIO;
417 error = xfs_attr_args_init(&args, dp, name, flags);
418 if (error)
419 return error;
421 args.firstblock = &firstblock;
422 args.flist = &flist;
425 * we have no control over the attribute names that userspace passes us
426 * to remove, so we have to allow the name lookup prior to attribute
427 * removal to fail.
429 args.op_flags = XFS_DA_OP_OKNOENT;
431 error = xfs_qm_dqattach(dp, 0);
432 if (error)
433 return error;
436 * Start our first transaction of the day.
438 * All future transactions during this code must be "chained" off
439 * this one via the trans_dup() call. All transactions will contain
440 * the inode, and the inode will always be marked with trans_ihold().
441 * Since the inode will be locked in all transactions, we must log
442 * the inode in every transaction to let it float upward through
443 * the log.
445 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
448 * Root fork attributes can use reserved data blocks for this
449 * operation if necessary
452 if (flags & ATTR_ROOT)
453 args.trans->t_flags |= XFS_TRANS_RESERVE;
455 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
456 XFS_ATTRRM_SPACE_RES(mp), 0);
457 if (error) {
458 xfs_trans_cancel(args.trans);
459 return error;
462 xfs_ilock(dp, XFS_ILOCK_EXCL);
464 * No need to make quota reservations here. We expect to release some
465 * blocks not allocate in the common case.
467 xfs_trans_ijoin(args.trans, dp, 0);
469 if (!xfs_inode_hasattr(dp)) {
470 error = -ENOATTR;
471 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
472 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
473 error = xfs_attr_shortform_remove(&args);
474 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
475 error = xfs_attr_leaf_removename(&args);
476 } else {
477 error = xfs_attr_node_removename(&args);
480 if (error)
481 goto out;
484 * If this is a synchronous mount, make sure that the
485 * transaction goes to disk before returning to the user.
487 if (mp->m_flags & XFS_MOUNT_WSYNC)
488 xfs_trans_set_sync(args.trans);
490 if ((flags & ATTR_KERNOTIME) == 0)
491 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
494 * Commit the last in the sequence of transactions.
496 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
497 error = xfs_trans_commit(args.trans);
498 xfs_iunlock(dp, XFS_ILOCK_EXCL);
500 return error;
502 out:
503 if (args.trans)
504 xfs_trans_cancel(args.trans);
505 xfs_iunlock(dp, XFS_ILOCK_EXCL);
506 return error;
509 /*========================================================================
510 * External routines when attribute list is inside the inode
511 *========================================================================*/
514 * Add a name to the shortform attribute list structure
515 * This is the external routine.
517 STATIC int
518 xfs_attr_shortform_addname(xfs_da_args_t *args)
520 int newsize, forkoff, retval;
522 trace_xfs_attr_sf_addname(args);
524 retval = xfs_attr_shortform_lookup(args);
525 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
526 return retval;
527 } else if (retval == -EEXIST) {
528 if (args->flags & ATTR_CREATE)
529 return retval;
530 retval = xfs_attr_shortform_remove(args);
531 if (retval)
532 return retval;
534 * Since we have removed the old attr, clear ATTR_REPLACE so
535 * that the leaf format add routine won't trip over the attr
536 * not being around.
538 args->flags &= ~ATTR_REPLACE;
541 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
542 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
543 return -ENOSPC;
545 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
546 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
548 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
549 if (!forkoff)
550 return -ENOSPC;
552 xfs_attr_shortform_add(args, forkoff);
553 return 0;
557 /*========================================================================
558 * External routines when attribute list is one block
559 *========================================================================*/
562 * Add a name to the leaf attribute list structure
564 * This leaf block cannot have a "remote" value, we only call this routine
565 * if bmap_one_block() says there is only one block (ie: no remote blks).
567 STATIC int
568 xfs_attr_leaf_addname(xfs_da_args_t *args)
570 xfs_inode_t *dp;
571 struct xfs_buf *bp;
572 int retval, error, committed, forkoff;
574 trace_xfs_attr_leaf_addname(args);
577 * Read the (only) block in the attribute list in.
579 dp = args->dp;
580 args->blkno = 0;
581 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
582 if (error)
583 return error;
586 * Look up the given attribute in the leaf block. Figure out if
587 * the given flags produce an error or call for an atomic rename.
589 retval = xfs_attr3_leaf_lookup_int(bp, args);
590 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
591 xfs_trans_brelse(args->trans, bp);
592 return retval;
593 } else if (retval == -EEXIST) {
594 if (args->flags & ATTR_CREATE) { /* pure create op */
595 xfs_trans_brelse(args->trans, bp);
596 return retval;
599 trace_xfs_attr_leaf_replace(args);
601 /* save the attribute state for later removal*/
602 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
603 args->blkno2 = args->blkno; /* set 2nd entry info*/
604 args->index2 = args->index;
605 args->rmtblkno2 = args->rmtblkno;
606 args->rmtblkcnt2 = args->rmtblkcnt;
607 args->rmtvaluelen2 = args->rmtvaluelen;
610 * clear the remote attr state now that it is saved so that the
611 * values reflect the state of the attribute we are about to
612 * add, not the attribute we just found and will remove later.
614 args->rmtblkno = 0;
615 args->rmtblkcnt = 0;
616 args->rmtvaluelen = 0;
620 * Add the attribute to the leaf block, transitioning to a Btree
621 * if required.
623 retval = xfs_attr3_leaf_add(bp, args);
624 if (retval == -ENOSPC) {
626 * Promote the attribute list to the Btree format, then
627 * Commit that transaction so that the node_addname() call
628 * can manage its own transactions.
630 xfs_bmap_init(args->flist, args->firstblock);
631 error = xfs_attr3_leaf_to_node(args);
632 if (!error) {
633 error = xfs_bmap_finish(&args->trans, args->flist,
634 &committed);
636 if (error) {
637 ASSERT(committed);
638 args->trans = NULL;
639 xfs_bmap_cancel(args->flist);
640 return error;
644 * bmap_finish() may have committed the last trans and started
645 * a new one. We need the inode to be in all transactions.
647 if (committed)
648 xfs_trans_ijoin(args->trans, dp, 0);
651 * Commit the current trans (including the inode) and start
652 * a new one.
654 error = xfs_trans_roll(&args->trans, dp);
655 if (error)
656 return error;
659 * Fob the whole rest of the problem off on the Btree code.
661 error = xfs_attr_node_addname(args);
662 return error;
666 * Commit the transaction that added the attr name so that
667 * later routines can manage their own transactions.
669 error = xfs_trans_roll(&args->trans, dp);
670 if (error)
671 return error;
674 * If there was an out-of-line value, allocate the blocks we
675 * identified for its storage and copy the value. This is done
676 * after we create the attribute so that we don't overflow the
677 * maximum size of a transaction and/or hit a deadlock.
679 if (args->rmtblkno > 0) {
680 error = xfs_attr_rmtval_set(args);
681 if (error)
682 return error;
686 * If this is an atomic rename operation, we must "flip" the
687 * incomplete flags on the "new" and "old" attribute/value pairs
688 * so that one disappears and one appears atomically. Then we
689 * must remove the "old" attribute/value pair.
691 if (args->op_flags & XFS_DA_OP_RENAME) {
693 * In a separate transaction, set the incomplete flag on the
694 * "old" attr and clear the incomplete flag on the "new" attr.
696 error = xfs_attr3_leaf_flipflags(args);
697 if (error)
698 return error;
701 * Dismantle the "old" attribute/value pair by removing
702 * a "remote" value (if it exists).
704 args->index = args->index2;
705 args->blkno = args->blkno2;
706 args->rmtblkno = args->rmtblkno2;
707 args->rmtblkcnt = args->rmtblkcnt2;
708 args->rmtvaluelen = args->rmtvaluelen2;
709 if (args->rmtblkno) {
710 error = xfs_attr_rmtval_remove(args);
711 if (error)
712 return error;
716 * Read in the block containing the "old" attr, then
717 * remove the "old" attr from that block (neat, huh!)
719 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
720 -1, &bp);
721 if (error)
722 return error;
724 xfs_attr3_leaf_remove(bp, args);
727 * If the result is small enough, shrink it all into the inode.
729 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
730 xfs_bmap_init(args->flist, args->firstblock);
731 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
732 /* bp is gone due to xfs_da_shrink_inode */
733 if (!error) {
734 error = xfs_bmap_finish(&args->trans,
735 args->flist,
736 &committed);
738 if (error) {
739 ASSERT(committed);
740 args->trans = NULL;
741 xfs_bmap_cancel(args->flist);
742 return error;
746 * bmap_finish() may have committed the last trans
747 * and started a new one. We need the inode to be
748 * in all transactions.
750 if (committed)
751 xfs_trans_ijoin(args->trans, dp, 0);
755 * Commit the remove and start the next trans in series.
757 error = xfs_trans_roll(&args->trans, dp);
759 } else if (args->rmtblkno > 0) {
761 * Added a "remote" value, just clear the incomplete flag.
763 error = xfs_attr3_leaf_clearflag(args);
765 return error;
769 * Remove a name from the leaf attribute list structure
771 * This leaf block cannot have a "remote" value, we only call this routine
772 * if bmap_one_block() says there is only one block (ie: no remote blks).
774 STATIC int
775 xfs_attr_leaf_removename(xfs_da_args_t *args)
777 xfs_inode_t *dp;
778 struct xfs_buf *bp;
779 int error, committed, forkoff;
781 trace_xfs_attr_leaf_removename(args);
784 * Remove the attribute.
786 dp = args->dp;
787 args->blkno = 0;
788 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
789 if (error)
790 return error;
792 error = xfs_attr3_leaf_lookup_int(bp, args);
793 if (error == -ENOATTR) {
794 xfs_trans_brelse(args->trans, bp);
795 return error;
798 xfs_attr3_leaf_remove(bp, args);
801 * If the result is small enough, shrink it all into the inode.
803 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
804 xfs_bmap_init(args->flist, args->firstblock);
805 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
806 /* bp is gone due to xfs_da_shrink_inode */
807 if (!error) {
808 error = xfs_bmap_finish(&args->trans, args->flist,
809 &committed);
811 if (error) {
812 ASSERT(committed);
813 args->trans = NULL;
814 xfs_bmap_cancel(args->flist);
815 return error;
819 * bmap_finish() may have committed the last trans and started
820 * a new one. We need the inode to be in all transactions.
822 if (committed)
823 xfs_trans_ijoin(args->trans, dp, 0);
825 return 0;
829 * Look up a name in a leaf attribute list structure.
831 * This leaf block cannot have a "remote" value, we only call this routine
832 * if bmap_one_block() says there is only one block (ie: no remote blks).
834 STATIC int
835 xfs_attr_leaf_get(xfs_da_args_t *args)
837 struct xfs_buf *bp;
838 int error;
840 trace_xfs_attr_leaf_get(args);
842 args->blkno = 0;
843 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
844 if (error)
845 return error;
847 error = xfs_attr3_leaf_lookup_int(bp, args);
848 if (error != -EEXIST) {
849 xfs_trans_brelse(args->trans, bp);
850 return error;
852 error = xfs_attr3_leaf_getvalue(bp, args);
853 xfs_trans_brelse(args->trans, bp);
854 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
855 error = xfs_attr_rmtval_get(args);
857 return error;
860 /*========================================================================
861 * External routines when attribute list size > geo->blksize
862 *========================================================================*/
865 * Add a name to a Btree-format attribute list.
867 * This will involve walking down the Btree, and may involve splitting
868 * leaf nodes and even splitting intermediate nodes up to and including
869 * the root node (a special case of an intermediate node).
871 * "Remote" attribute values confuse the issue and atomic rename operations
872 * add a whole extra layer of confusion on top of that.
874 STATIC int
875 xfs_attr_node_addname(xfs_da_args_t *args)
877 xfs_da_state_t *state;
878 xfs_da_state_blk_t *blk;
879 xfs_inode_t *dp;
880 xfs_mount_t *mp;
881 int committed, retval, error;
883 trace_xfs_attr_node_addname(args);
886 * Fill in bucket of arguments/results/context to carry around.
888 dp = args->dp;
889 mp = dp->i_mount;
890 restart:
891 state = xfs_da_state_alloc();
892 state->args = args;
893 state->mp = mp;
896 * Search to see if name already exists, and get back a pointer
897 * to where it should go.
899 error = xfs_da3_node_lookup_int(state, &retval);
900 if (error)
901 goto out;
902 blk = &state->path.blk[ state->path.active-1 ];
903 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
904 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
905 goto out;
906 } else if (retval == -EEXIST) {
907 if (args->flags & ATTR_CREATE)
908 goto out;
910 trace_xfs_attr_node_replace(args);
912 /* save the attribute state for later removal*/
913 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
914 args->blkno2 = args->blkno; /* set 2nd entry info*/
915 args->index2 = args->index;
916 args->rmtblkno2 = args->rmtblkno;
917 args->rmtblkcnt2 = args->rmtblkcnt;
918 args->rmtvaluelen2 = args->rmtvaluelen;
921 * clear the remote attr state now that it is saved so that the
922 * values reflect the state of the attribute we are about to
923 * add, not the attribute we just found and will remove later.
925 args->rmtblkno = 0;
926 args->rmtblkcnt = 0;
927 args->rmtvaluelen = 0;
930 retval = xfs_attr3_leaf_add(blk->bp, state->args);
931 if (retval == -ENOSPC) {
932 if (state->path.active == 1) {
934 * Its really a single leaf node, but it had
935 * out-of-line values so it looked like it *might*
936 * have been a b-tree.
938 xfs_da_state_free(state);
939 state = NULL;
940 xfs_bmap_init(args->flist, args->firstblock);
941 error = xfs_attr3_leaf_to_node(args);
942 if (!error) {
943 error = xfs_bmap_finish(&args->trans,
944 args->flist,
945 &committed);
947 if (error) {
948 ASSERT(committed);
949 args->trans = NULL;
950 xfs_bmap_cancel(args->flist);
951 goto out;
955 * bmap_finish() may have committed the last trans
956 * and started a new one. We need the inode to be
957 * in all transactions.
959 if (committed)
960 xfs_trans_ijoin(args->trans, dp, 0);
963 * Commit the node conversion and start the next
964 * trans in the chain.
966 error = xfs_trans_roll(&args->trans, dp);
967 if (error)
968 goto out;
970 goto restart;
974 * Split as many Btree elements as required.
975 * This code tracks the new and old attr's location
976 * in the index/blkno/rmtblkno/rmtblkcnt fields and
977 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
979 xfs_bmap_init(args->flist, args->firstblock);
980 error = xfs_da3_split(state);
981 if (!error) {
982 error = xfs_bmap_finish(&args->trans, args->flist,
983 &committed);
985 if (error) {
986 ASSERT(committed);
987 args->trans = NULL;
988 xfs_bmap_cancel(args->flist);
989 goto out;
993 * bmap_finish() may have committed the last trans and started
994 * a new one. We need the inode to be in all transactions.
996 if (committed)
997 xfs_trans_ijoin(args->trans, dp, 0);
998 } else {
1000 * Addition succeeded, update Btree hashvals.
1002 xfs_da3_fixhashpath(state, &state->path);
1006 * Kill the state structure, we're done with it and need to
1007 * allow the buffers to come back later.
1009 xfs_da_state_free(state);
1010 state = NULL;
1013 * Commit the leaf addition or btree split and start the next
1014 * trans in the chain.
1016 error = xfs_trans_roll(&args->trans, dp);
1017 if (error)
1018 goto out;
1021 * If there was an out-of-line value, allocate the blocks we
1022 * identified for its storage and copy the value. This is done
1023 * after we create the attribute so that we don't overflow the
1024 * maximum size of a transaction and/or hit a deadlock.
1026 if (args->rmtblkno > 0) {
1027 error = xfs_attr_rmtval_set(args);
1028 if (error)
1029 return error;
1033 * If this is an atomic rename operation, we must "flip" the
1034 * incomplete flags on the "new" and "old" attribute/value pairs
1035 * so that one disappears and one appears atomically. Then we
1036 * must remove the "old" attribute/value pair.
1038 if (args->op_flags & XFS_DA_OP_RENAME) {
1040 * In a separate transaction, set the incomplete flag on the
1041 * "old" attr and clear the incomplete flag on the "new" attr.
1043 error = xfs_attr3_leaf_flipflags(args);
1044 if (error)
1045 goto out;
1048 * Dismantle the "old" attribute/value pair by removing
1049 * a "remote" value (if it exists).
1051 args->index = args->index2;
1052 args->blkno = args->blkno2;
1053 args->rmtblkno = args->rmtblkno2;
1054 args->rmtblkcnt = args->rmtblkcnt2;
1055 args->rmtvaluelen = args->rmtvaluelen2;
1056 if (args->rmtblkno) {
1057 error = xfs_attr_rmtval_remove(args);
1058 if (error)
1059 return error;
1063 * Re-find the "old" attribute entry after any split ops.
1064 * The INCOMPLETE flag means that we will find the "old"
1065 * attr, not the "new" one.
1067 args->flags |= XFS_ATTR_INCOMPLETE;
1068 state = xfs_da_state_alloc();
1069 state->args = args;
1070 state->mp = mp;
1071 state->inleaf = 0;
1072 error = xfs_da3_node_lookup_int(state, &retval);
1073 if (error)
1074 goto out;
1077 * Remove the name and update the hashvals in the tree.
1079 blk = &state->path.blk[ state->path.active-1 ];
1080 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1081 error = xfs_attr3_leaf_remove(blk->bp, args);
1082 xfs_da3_fixhashpath(state, &state->path);
1085 * Check to see if the tree needs to be collapsed.
1087 if (retval && (state->path.active > 1)) {
1088 xfs_bmap_init(args->flist, args->firstblock);
1089 error = xfs_da3_join(state);
1090 if (!error) {
1091 error = xfs_bmap_finish(&args->trans,
1092 args->flist,
1093 &committed);
1095 if (error) {
1096 ASSERT(committed);
1097 args->trans = NULL;
1098 xfs_bmap_cancel(args->flist);
1099 goto out;
1103 * bmap_finish() may have committed the last trans
1104 * and started a new one. We need the inode to be
1105 * in all transactions.
1107 if (committed)
1108 xfs_trans_ijoin(args->trans, dp, 0);
1112 * Commit and start the next trans in the chain.
1114 error = xfs_trans_roll(&args->trans, dp);
1115 if (error)
1116 goto out;
1118 } else if (args->rmtblkno > 0) {
1120 * Added a "remote" value, just clear the incomplete flag.
1122 error = xfs_attr3_leaf_clearflag(args);
1123 if (error)
1124 goto out;
1126 retval = error = 0;
1128 out:
1129 if (state)
1130 xfs_da_state_free(state);
1131 if (error)
1132 return error;
1133 return retval;
1137 * Remove a name from a B-tree attribute list.
1139 * This will involve walking down the Btree, and may involve joining
1140 * leaf nodes and even joining intermediate nodes up to and including
1141 * the root node (a special case of an intermediate node).
1143 STATIC int
1144 xfs_attr_node_removename(xfs_da_args_t *args)
1146 xfs_da_state_t *state;
1147 xfs_da_state_blk_t *blk;
1148 xfs_inode_t *dp;
1149 struct xfs_buf *bp;
1150 int retval, error, committed, forkoff;
1152 trace_xfs_attr_node_removename(args);
1155 * Tie a string around our finger to remind us where we are.
1157 dp = args->dp;
1158 state = xfs_da_state_alloc();
1159 state->args = args;
1160 state->mp = dp->i_mount;
1163 * Search to see if name exists, and get back a pointer to it.
1165 error = xfs_da3_node_lookup_int(state, &retval);
1166 if (error || (retval != -EEXIST)) {
1167 if (error == 0)
1168 error = retval;
1169 goto out;
1173 * If there is an out-of-line value, de-allocate the blocks.
1174 * This is done before we remove the attribute so that we don't
1175 * overflow the maximum size of a transaction and/or hit a deadlock.
1177 blk = &state->path.blk[ state->path.active-1 ];
1178 ASSERT(blk->bp != NULL);
1179 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1180 if (args->rmtblkno > 0) {
1182 * Fill in disk block numbers in the state structure
1183 * so that we can get the buffers back after we commit
1184 * several transactions in the following calls.
1186 error = xfs_attr_fillstate(state);
1187 if (error)
1188 goto out;
1191 * Mark the attribute as INCOMPLETE, then bunmapi() the
1192 * remote value.
1194 error = xfs_attr3_leaf_setflag(args);
1195 if (error)
1196 goto out;
1197 error = xfs_attr_rmtval_remove(args);
1198 if (error)
1199 goto out;
1202 * Refill the state structure with buffers, the prior calls
1203 * released our buffers.
1205 error = xfs_attr_refillstate(state);
1206 if (error)
1207 goto out;
1211 * Remove the name and update the hashvals in the tree.
1213 blk = &state->path.blk[ state->path.active-1 ];
1214 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1215 retval = xfs_attr3_leaf_remove(blk->bp, args);
1216 xfs_da3_fixhashpath(state, &state->path);
1219 * Check to see if the tree needs to be collapsed.
1221 if (retval && (state->path.active > 1)) {
1222 xfs_bmap_init(args->flist, args->firstblock);
1223 error = xfs_da3_join(state);
1224 if (!error) {
1225 error = xfs_bmap_finish(&args->trans, args->flist,
1226 &committed);
1228 if (error) {
1229 ASSERT(committed);
1230 args->trans = NULL;
1231 xfs_bmap_cancel(args->flist);
1232 goto out;
1236 * bmap_finish() may have committed the last trans and started
1237 * a new one. We need the inode to be in all transactions.
1239 if (committed)
1240 xfs_trans_ijoin(args->trans, dp, 0);
1243 * Commit the Btree join operation and start a new trans.
1245 error = xfs_trans_roll(&args->trans, dp);
1246 if (error)
1247 goto out;
1251 * If the result is small enough, push it all into the inode.
1253 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1255 * Have to get rid of the copy of this dabuf in the state.
1257 ASSERT(state->path.active == 1);
1258 ASSERT(state->path.blk[0].bp);
1259 state->path.blk[0].bp = NULL;
1261 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1262 if (error)
1263 goto out;
1265 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1266 xfs_bmap_init(args->flist, args->firstblock);
1267 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1268 /* bp is gone due to xfs_da_shrink_inode */
1269 if (!error) {
1270 error = xfs_bmap_finish(&args->trans,
1271 args->flist,
1272 &committed);
1274 if (error) {
1275 ASSERT(committed);
1276 args->trans = NULL;
1277 xfs_bmap_cancel(args->flist);
1278 goto out;
1282 * bmap_finish() may have committed the last trans
1283 * and started a new one. We need the inode to be
1284 * in all transactions.
1286 if (committed)
1287 xfs_trans_ijoin(args->trans, dp, 0);
1288 } else
1289 xfs_trans_brelse(args->trans, bp);
1291 error = 0;
1293 out:
1294 xfs_da_state_free(state);
1295 return error;
1299 * Fill in the disk block numbers in the state structure for the buffers
1300 * that are attached to the state structure.
1301 * This is done so that we can quickly reattach ourselves to those buffers
1302 * after some set of transaction commits have released these buffers.
1304 STATIC int
1305 xfs_attr_fillstate(xfs_da_state_t *state)
1307 xfs_da_state_path_t *path;
1308 xfs_da_state_blk_t *blk;
1309 int level;
1311 trace_xfs_attr_fillstate(state->args);
1314 * Roll down the "path" in the state structure, storing the on-disk
1315 * block number for those buffers in the "path".
1317 path = &state->path;
1318 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1319 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1320 if (blk->bp) {
1321 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1322 blk->bp = NULL;
1323 } else {
1324 blk->disk_blkno = 0;
1329 * Roll down the "altpath" in the state structure, storing the on-disk
1330 * block number for those buffers in the "altpath".
1332 path = &state->altpath;
1333 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1334 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1335 if (blk->bp) {
1336 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1337 blk->bp = NULL;
1338 } else {
1339 blk->disk_blkno = 0;
1343 return 0;
1347 * Reattach the buffers to the state structure based on the disk block
1348 * numbers stored in the state structure.
1349 * This is done after some set of transaction commits have released those
1350 * buffers from our grip.
1352 STATIC int
1353 xfs_attr_refillstate(xfs_da_state_t *state)
1355 xfs_da_state_path_t *path;
1356 xfs_da_state_blk_t *blk;
1357 int level, error;
1359 trace_xfs_attr_refillstate(state->args);
1362 * Roll down the "path" in the state structure, storing the on-disk
1363 * block number for those buffers in the "path".
1365 path = &state->path;
1366 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1367 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1368 if (blk->disk_blkno) {
1369 error = xfs_da3_node_read(state->args->trans,
1370 state->args->dp,
1371 blk->blkno, blk->disk_blkno,
1372 &blk->bp, XFS_ATTR_FORK);
1373 if (error)
1374 return error;
1375 } else {
1376 blk->bp = NULL;
1381 * Roll down the "altpath" in the state structure, storing the on-disk
1382 * block number for those buffers in the "altpath".
1384 path = &state->altpath;
1385 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1386 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1387 if (blk->disk_blkno) {
1388 error = xfs_da3_node_read(state->args->trans,
1389 state->args->dp,
1390 blk->blkno, blk->disk_blkno,
1391 &blk->bp, XFS_ATTR_FORK);
1392 if (error)
1393 return error;
1394 } else {
1395 blk->bp = NULL;
1399 return 0;
1403 * Look up a filename in a node attribute list.
1405 * This routine gets called for any attribute fork that has more than one
1406 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1407 * "remote" values taking up more blocks.
1409 STATIC int
1410 xfs_attr_node_get(xfs_da_args_t *args)
1412 xfs_da_state_t *state;
1413 xfs_da_state_blk_t *blk;
1414 int error, retval;
1415 int i;
1417 trace_xfs_attr_node_get(args);
1419 state = xfs_da_state_alloc();
1420 state->args = args;
1421 state->mp = args->dp->i_mount;
1424 * Search to see if name exists, and get back a pointer to it.
1426 error = xfs_da3_node_lookup_int(state, &retval);
1427 if (error) {
1428 retval = error;
1429 } else if (retval == -EEXIST) {
1430 blk = &state->path.blk[ state->path.active-1 ];
1431 ASSERT(blk->bp != NULL);
1432 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1435 * Get the value, local or "remote"
1437 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1438 if (!retval && (args->rmtblkno > 0)
1439 && !(args->flags & ATTR_KERNOVAL)) {
1440 retval = xfs_attr_rmtval_get(args);
1445 * If not in a transaction, we have to release all the buffers.
1447 for (i = 0; i < state->path.active; i++) {
1448 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1449 state->path.blk[i].bp = NULL;
1452 xfs_da_state_free(state);
1453 return retval;