2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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
20 #include "xfs_format.h"
21 #include "xfs_shared.h"
24 #include "xfs_trans.h"
25 #include "xfs_trans_priv.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_alloc.h"
35 #include "xfs_inode_item.h"
37 #include "xfs_bmap_util.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_attr_remote.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_trace.h"
49 * Provide the external interfaces to manage attribute lists.
52 /*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
57 * Internal routines when attribute list fits inside the inode.
59 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
62 * Internal routines when attribute list is one block.
64 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
65 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
66 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
69 * Internal routines when attribute list is more than one block.
71 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
73 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
74 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
75 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
79 xfs_attr_name_to_xname(
80 struct xfs_name
*xname
,
81 const unsigned char *aname
)
86 xname
->len
= strlen((char *)aname
);
87 if (xname
->len
>= MAXNAMELEN
)
88 return EFAULT
; /* match IRIX behaviour */
97 if (!XFS_IFORK_Q(ip
) ||
98 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
99 ip
->i_d
.di_anextents
== 0))
104 /*========================================================================
105 * Overall external interface routines.
106 *========================================================================*/
110 struct xfs_inode
*ip
,
111 struct xfs_name
*name
,
112 unsigned char *value
,
119 if (!xfs_inode_hasattr(ip
))
123 * Fill in the arg structure for this request.
125 memset((char *)&args
, 0, sizeof(args
));
126 args
.name
= name
->name
;
127 args
.namelen
= name
->len
;
129 args
.valuelen
= *valuelenp
;
131 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
133 args
.whichfork
= XFS_ATTR_FORK
;
136 * Decide on what work routines to call based on the inode size.
138 if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
139 error
= xfs_attr_shortform_getvalue(&args
);
140 } else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
)) {
141 error
= xfs_attr_leaf_get(&args
);
143 error
= xfs_attr_node_get(&args
);
147 * Return the number of bytes in the value to the caller.
149 *valuelenp
= args
.valuelen
;
159 const unsigned char *name
,
160 unsigned char *value
,
165 struct xfs_name xname
;
167 XFS_STATS_INC(xs_attr_get
);
169 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
172 error
= xfs_attr_name_to_xname(&xname
, name
);
176 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
177 error
= xfs_attr_get_int(ip
, &xname
, value
, valuelenp
, flags
);
178 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
183 * Calculate how many blocks we need for the new attribute,
187 struct xfs_inode
*ip
,
192 struct xfs_mount
*mp
= ip
->i_mount
;
197 * Determine space new attribute will use, and if it would be
198 * "local" or "remote" (note: local != inline).
200 size
= xfs_attr_leaf_newentsize(namelen
, valuelen
,
201 mp
->m_sb
.sb_blocksize
, local
);
203 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
205 if (size
> (mp
->m_sb
.sb_blocksize
>> 1)) {
206 /* Double split possible */
211 * Out of line attribute, cannot double split, but
212 * make room for the attribute value itself.
214 uint dblocks
= XFS_B_TO_FSB(mp
, valuelen
);
216 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
224 struct xfs_inode
*dp
,
225 struct xfs_name
*name
,
226 unsigned char *value
,
231 xfs_fsblock_t firstblock
;
232 xfs_bmap_free_t flist
;
233 int error
, err2
, committed
;
234 struct xfs_mount
*mp
= dp
->i_mount
;
235 struct xfs_trans_res tres
;
236 int rsvd
= (flags
& ATTR_ROOT
) != 0;
240 * Attach the dquots to the inode.
242 error
= xfs_qm_dqattach(dp
, 0);
247 * If the inode doesn't have an attribute fork, add one.
248 * (inode must not be locked when we call this routine)
250 if (XFS_IFORK_Q(dp
) == 0) {
251 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
252 XFS_ATTR_SF_ENTSIZE_BYNAME(name
->len
, valuelen
);
254 if ((error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
)))
259 * Fill in the arg structure for this request.
261 memset((char *)&args
, 0, sizeof(args
));
262 args
.name
= name
->name
;
263 args
.namelen
= name
->len
;
265 args
.valuelen
= valuelen
;
267 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
269 args
.firstblock
= &firstblock
;
271 args
.whichfork
= XFS_ATTR_FORK
;
272 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
274 /* Size is now blocks for attribute data */
275 args
.total
= xfs_attr_calc_size(dp
, name
->len
, valuelen
, &local
);
278 * Start our first transaction of the day.
280 * All future transactions during this code must be "chained" off
281 * this one via the trans_dup() call. All transactions will contain
282 * the inode, and the inode will always be marked with trans_ihold().
283 * Since the inode will be locked in all transactions, we must log
284 * the inode in every transaction to let it float upward through
287 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
290 * Root fork attributes can use reserved data blocks for this
291 * operation if necessary
295 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
297 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
298 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
299 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
300 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
301 error
= xfs_trans_reserve(args
.trans
, &tres
, args
.total
, 0);
303 xfs_trans_cancel(args
.trans
, 0);
306 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
308 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
309 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
310 XFS_QMOPT_RES_REGBLKS
);
312 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
313 xfs_trans_cancel(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
317 xfs_trans_ijoin(args
.trans
, dp
, 0);
320 * If the attribute list is non-existent or a shortform list,
321 * upgrade it to a single-leaf-block attribute list.
323 if ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) ||
324 ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) &&
325 (dp
->i_d
.di_anextents
== 0))) {
328 * Build initial attribute list (if required).
330 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
331 xfs_attr_shortform_create(&args
);
334 * Try to add the attr to the attribute list in
337 error
= xfs_attr_shortform_addname(&args
);
338 if (error
!= ENOSPC
) {
340 * Commit the shortform mods, and we're done.
341 * NOTE: this is also the error path (EEXIST, etc).
343 ASSERT(args
.trans
!= NULL
);
346 * If this is a synchronous mount, make sure that
347 * the transaction goes to disk before returning
350 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
351 xfs_trans_set_sync(args
.trans
);
354 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
355 xfs_trans_ichgtime(args
.trans
, dp
,
358 err2
= xfs_trans_commit(args
.trans
,
359 XFS_TRANS_RELEASE_LOG_RES
);
360 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
362 return(error
== 0 ? err2
: error
);
366 * It won't fit in the shortform, transform to a leaf block.
367 * GROT: another possible req'mt for a double-split btree op.
369 xfs_bmap_init(args
.flist
, args
.firstblock
);
370 error
= xfs_attr_shortform_to_leaf(&args
);
372 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
378 xfs_bmap_cancel(&flist
);
383 * bmap_finish() may have committed the last trans and started
384 * a new one. We need the inode to be in all transactions.
387 xfs_trans_ijoin(args
.trans
, dp
, 0);
390 * Commit the leaf transformation. We'll need another (linked)
391 * transaction to add the new attribute to the leaf.
394 error
= xfs_trans_roll(&args
.trans
, dp
);
400 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
401 error
= xfs_attr_leaf_addname(&args
);
403 error
= xfs_attr_node_addname(&args
);
410 * If this is a synchronous mount, make sure that the
411 * transaction goes to disk before returning to the user.
413 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
414 xfs_trans_set_sync(args
.trans
);
417 if ((flags
& ATTR_KERNOTIME
) == 0)
418 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
421 * Commit the last in the sequence of transactions.
423 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
424 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
425 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
431 xfs_trans_cancel(args
.trans
,
432 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
433 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
440 const unsigned char *name
,
441 unsigned char *value
,
446 struct xfs_name xname
;
448 XFS_STATS_INC(xs_attr_set
);
450 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
453 error
= xfs_attr_name_to_xname(&xname
, name
);
457 return xfs_attr_set_int(dp
, &xname
, value
, valuelen
, flags
);
461 * Generic handler routine to remove a name from an attribute list.
462 * Transitions attribute list from Btree to shortform as necessary.
465 xfs_attr_remove_int(xfs_inode_t
*dp
, struct xfs_name
*name
, int flags
)
468 xfs_fsblock_t firstblock
;
469 xfs_bmap_free_t flist
;
471 xfs_mount_t
*mp
= dp
->i_mount
;
474 * Fill in the arg structure for this request.
476 memset((char *)&args
, 0, sizeof(args
));
477 args
.name
= name
->name
;
478 args
.namelen
= name
->len
;
480 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
482 args
.firstblock
= &firstblock
;
485 args
.whichfork
= XFS_ATTR_FORK
;
488 * we have no control over the attribute names that userspace passes us
489 * to remove, so we have to allow the name lookup prior to attribute
492 args
.op_flags
= XFS_DA_OP_OKNOENT
;
495 * Attach the dquots to the inode.
497 error
= xfs_qm_dqattach(dp
, 0);
502 * Start our first transaction of the day.
504 * All future transactions during this code must be "chained" off
505 * this one via the trans_dup() call. All transactions will contain
506 * the inode, and the inode will always be marked with trans_ihold().
507 * Since the inode will be locked in all transactions, we must log
508 * the inode in every transaction to let it float upward through
511 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
514 * Root fork attributes can use reserved data blocks for this
515 * operation if necessary
518 if (flags
& ATTR_ROOT
)
519 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
521 error
= xfs_trans_reserve(args
.trans
, &M_RES(mp
)->tr_attrrm
,
522 XFS_ATTRRM_SPACE_RES(mp
), 0);
524 xfs_trans_cancel(args
.trans
, 0);
528 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
530 * No need to make quota reservations here. We expect to release some
531 * blocks not allocate in the common case.
533 xfs_trans_ijoin(args
.trans
, dp
, 0);
536 * Decide on what work routines to call based on the inode size.
538 if (!xfs_inode_hasattr(dp
)) {
539 error
= XFS_ERROR(ENOATTR
);
542 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
543 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
544 error
= xfs_attr_shortform_remove(&args
);
548 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
549 error
= xfs_attr_leaf_removename(&args
);
551 error
= xfs_attr_node_removename(&args
);
558 * If this is a synchronous mount, make sure that the
559 * transaction goes to disk before returning to the user.
561 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
562 xfs_trans_set_sync(args
.trans
);
565 if ((flags
& ATTR_KERNOTIME
) == 0)
566 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
569 * Commit the last in the sequence of transactions.
571 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
572 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
573 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
579 xfs_trans_cancel(args
.trans
,
580 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
581 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
588 const unsigned char *name
,
592 struct xfs_name xname
;
594 XFS_STATS_INC(xs_attr_remove
);
596 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
599 error
= xfs_attr_name_to_xname(&xname
, name
);
603 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
604 if (!xfs_inode_hasattr(dp
)) {
605 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
606 return XFS_ERROR(ENOATTR
);
608 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
610 return xfs_attr_remove_int(dp
, &xname
, flags
);
614 /*========================================================================
615 * External routines when attribute list is inside the inode
616 *========================================================================*/
619 * Add a name to the shortform attribute list structure
620 * This is the external routine.
623 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
625 int newsize
, forkoff
, retval
;
627 trace_xfs_attr_sf_addname(args
);
629 retval
= xfs_attr_shortform_lookup(args
);
630 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
632 } else if (retval
== EEXIST
) {
633 if (args
->flags
& ATTR_CREATE
)
635 retval
= xfs_attr_shortform_remove(args
);
639 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
640 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
641 return(XFS_ERROR(ENOSPC
));
643 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
644 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
646 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
648 return(XFS_ERROR(ENOSPC
));
650 xfs_attr_shortform_add(args
, forkoff
);
655 /*========================================================================
656 * External routines when attribute list is one block
657 *========================================================================*/
660 * Add a name to the leaf attribute list structure
662 * This leaf block cannot have a "remote" value, we only call this routine
663 * if bmap_one_block() says there is only one block (ie: no remote blks).
666 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
670 int retval
, error
, committed
, forkoff
;
672 trace_xfs_attr_leaf_addname(args
);
675 * Read the (only) block in the attribute list in.
679 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
684 * Look up the given attribute in the leaf block. Figure out if
685 * the given flags produce an error or call for an atomic rename.
687 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
688 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
689 xfs_trans_brelse(args
->trans
, bp
);
691 } else if (retval
== EEXIST
) {
692 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
693 xfs_trans_brelse(args
->trans
, bp
);
697 trace_xfs_attr_leaf_replace(args
);
699 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
700 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
701 args
->index2
= args
->index
;
702 args
->rmtblkno2
= args
->rmtblkno
;
703 args
->rmtblkcnt2
= args
->rmtblkcnt
;
707 * Add the attribute to the leaf block, transitioning to a Btree
710 retval
= xfs_attr3_leaf_add(bp
, args
);
711 if (retval
== ENOSPC
) {
713 * Promote the attribute list to the Btree format, then
714 * Commit that transaction so that the node_addname() call
715 * can manage its own transactions.
717 xfs_bmap_init(args
->flist
, args
->firstblock
);
718 error
= xfs_attr3_leaf_to_node(args
);
720 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
726 xfs_bmap_cancel(args
->flist
);
731 * bmap_finish() may have committed the last trans and started
732 * a new one. We need the inode to be in all transactions.
735 xfs_trans_ijoin(args
->trans
, dp
, 0);
738 * Commit the current trans (including the inode) and start
741 error
= xfs_trans_roll(&args
->trans
, dp
);
746 * Fob the whole rest of the problem off on the Btree code.
748 error
= xfs_attr_node_addname(args
);
753 * Commit the transaction that added the attr name so that
754 * later routines can manage their own transactions.
756 error
= xfs_trans_roll(&args
->trans
, dp
);
761 * If there was an out-of-line value, allocate the blocks we
762 * identified for its storage and copy the value. This is done
763 * after we create the attribute so that we don't overflow the
764 * maximum size of a transaction and/or hit a deadlock.
766 if (args
->rmtblkno
> 0) {
767 error
= xfs_attr_rmtval_set(args
);
773 * If this is an atomic rename operation, we must "flip" the
774 * incomplete flags on the "new" and "old" attribute/value pairs
775 * so that one disappears and one appears atomically. Then we
776 * must remove the "old" attribute/value pair.
778 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
780 * In a separate transaction, set the incomplete flag on the
781 * "old" attr and clear the incomplete flag on the "new" attr.
783 error
= xfs_attr3_leaf_flipflags(args
);
788 * Dismantle the "old" attribute/value pair by removing
789 * a "remote" value (if it exists).
791 args
->index
= args
->index2
;
792 args
->blkno
= args
->blkno2
;
793 args
->rmtblkno
= args
->rmtblkno2
;
794 args
->rmtblkcnt
= args
->rmtblkcnt2
;
795 if (args
->rmtblkno
) {
796 error
= xfs_attr_rmtval_remove(args
);
802 * Read in the block containing the "old" attr, then
803 * remove the "old" attr from that block (neat, huh!)
805 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
810 xfs_attr3_leaf_remove(bp
, args
);
813 * If the result is small enough, shrink it all into the inode.
815 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
816 xfs_bmap_init(args
->flist
, args
->firstblock
);
817 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
818 /* bp is gone due to xfs_da_shrink_inode */
820 error
= xfs_bmap_finish(&args
->trans
,
827 xfs_bmap_cancel(args
->flist
);
832 * bmap_finish() may have committed the last trans
833 * and started a new one. We need the inode to be
834 * in all transactions.
837 xfs_trans_ijoin(args
->trans
, dp
, 0);
841 * Commit the remove and start the next trans in series.
843 error
= xfs_trans_roll(&args
->trans
, dp
);
845 } else if (args
->rmtblkno
> 0) {
847 * Added a "remote" value, just clear the incomplete flag.
849 error
= xfs_attr3_leaf_clearflag(args
);
855 * Remove a name from the leaf attribute list structure
857 * This leaf block cannot have a "remote" value, we only call this routine
858 * if bmap_one_block() says there is only one block (ie: no remote blks).
861 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
865 int error
, committed
, forkoff
;
867 trace_xfs_attr_leaf_removename(args
);
870 * Remove the attribute.
874 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
878 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
879 if (error
== ENOATTR
) {
880 xfs_trans_brelse(args
->trans
, bp
);
884 xfs_attr3_leaf_remove(bp
, args
);
887 * If the result is small enough, shrink it all into the inode.
889 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
890 xfs_bmap_init(args
->flist
, args
->firstblock
);
891 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
892 /* bp is gone due to xfs_da_shrink_inode */
894 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
900 xfs_bmap_cancel(args
->flist
);
905 * bmap_finish() may have committed the last trans and started
906 * a new one. We need the inode to be in all transactions.
909 xfs_trans_ijoin(args
->trans
, dp
, 0);
915 * Look up a name in a leaf attribute list structure.
917 * This leaf block cannot have a "remote" value, we only call this routine
918 * if bmap_one_block() says there is only one block (ie: no remote blks).
921 xfs_attr_leaf_get(xfs_da_args_t
*args
)
926 trace_xfs_attr_leaf_get(args
);
929 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
933 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
934 if (error
!= EEXIST
) {
935 xfs_trans_brelse(args
->trans
, bp
);
938 error
= xfs_attr3_leaf_getvalue(bp
, args
);
939 xfs_trans_brelse(args
->trans
, bp
);
940 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
941 error
= xfs_attr_rmtval_get(args
);
946 /*========================================================================
947 * External routines when attribute list size > XFS_LBSIZE(mp).
948 *========================================================================*/
951 * Add a name to a Btree-format attribute list.
953 * This will involve walking down the Btree, and may involve splitting
954 * leaf nodes and even splitting intermediate nodes up to and including
955 * the root node (a special case of an intermediate node).
957 * "Remote" attribute values confuse the issue and atomic rename operations
958 * add a whole extra layer of confusion on top of that.
961 xfs_attr_node_addname(xfs_da_args_t
*args
)
963 xfs_da_state_t
*state
;
964 xfs_da_state_blk_t
*blk
;
967 int committed
, retval
, error
;
969 trace_xfs_attr_node_addname(args
);
972 * Fill in bucket of arguments/results/context to carry around.
977 state
= xfs_da_state_alloc();
980 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
981 state
->node_ents
= state
->mp
->m_attr_node_ents
;
984 * Search to see if name already exists, and get back a pointer
985 * to where it should go.
987 error
= xfs_da3_node_lookup_int(state
, &retval
);
990 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
991 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
992 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
994 } else if (retval
== EEXIST
) {
995 if (args
->flags
& ATTR_CREATE
)
998 trace_xfs_attr_node_replace(args
);
1000 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
1001 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
1002 args
->index2
= args
->index
;
1003 args
->rmtblkno2
= args
->rmtblkno
;
1004 args
->rmtblkcnt2
= args
->rmtblkcnt
;
1006 args
->rmtblkcnt
= 0;
1009 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
1010 if (retval
== ENOSPC
) {
1011 if (state
->path
.active
== 1) {
1013 * Its really a single leaf node, but it had
1014 * out-of-line values so it looked like it *might*
1015 * have been a b-tree.
1017 xfs_da_state_free(state
);
1019 xfs_bmap_init(args
->flist
, args
->firstblock
);
1020 error
= xfs_attr3_leaf_to_node(args
);
1022 error
= xfs_bmap_finish(&args
->trans
,
1029 xfs_bmap_cancel(args
->flist
);
1034 * bmap_finish() may have committed the last trans
1035 * and started a new one. We need the inode to be
1036 * in all transactions.
1039 xfs_trans_ijoin(args
->trans
, dp
, 0);
1042 * Commit the node conversion and start the next
1043 * trans in the chain.
1045 error
= xfs_trans_roll(&args
->trans
, dp
);
1053 * Split as many Btree elements as required.
1054 * This code tracks the new and old attr's location
1055 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1056 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1058 xfs_bmap_init(args
->flist
, args
->firstblock
);
1059 error
= xfs_da3_split(state
);
1061 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1067 xfs_bmap_cancel(args
->flist
);
1072 * bmap_finish() may have committed the last trans and started
1073 * a new one. We need the inode to be in all transactions.
1076 xfs_trans_ijoin(args
->trans
, dp
, 0);
1079 * Addition succeeded, update Btree hashvals.
1081 xfs_da3_fixhashpath(state
, &state
->path
);
1085 * Kill the state structure, we're done with it and need to
1086 * allow the buffers to come back later.
1088 xfs_da_state_free(state
);
1092 * Commit the leaf addition or btree split and start the next
1093 * trans in the chain.
1095 error
= xfs_trans_roll(&args
->trans
, dp
);
1100 * If there was an out-of-line value, allocate the blocks we
1101 * identified for its storage and copy the value. This is done
1102 * after we create the attribute so that we don't overflow the
1103 * maximum size of a transaction and/or hit a deadlock.
1105 if (args
->rmtblkno
> 0) {
1106 error
= xfs_attr_rmtval_set(args
);
1112 * If this is an atomic rename operation, we must "flip" the
1113 * incomplete flags on the "new" and "old" attribute/value pairs
1114 * so that one disappears and one appears atomically. Then we
1115 * must remove the "old" attribute/value pair.
1117 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1119 * In a separate transaction, set the incomplete flag on the
1120 * "old" attr and clear the incomplete flag on the "new" attr.
1122 error
= xfs_attr3_leaf_flipflags(args
);
1127 * Dismantle the "old" attribute/value pair by removing
1128 * a "remote" value (if it exists).
1130 args
->index
= args
->index2
;
1131 args
->blkno
= args
->blkno2
;
1132 args
->rmtblkno
= args
->rmtblkno2
;
1133 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1134 if (args
->rmtblkno
) {
1135 error
= xfs_attr_rmtval_remove(args
);
1141 * Re-find the "old" attribute entry after any split ops.
1142 * The INCOMPLETE flag means that we will find the "old"
1143 * attr, not the "new" one.
1145 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1146 state
= xfs_da_state_alloc();
1149 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1150 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1152 error
= xfs_da3_node_lookup_int(state
, &retval
);
1157 * Remove the name and update the hashvals in the tree.
1159 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1160 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1161 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1162 xfs_da3_fixhashpath(state
, &state
->path
);
1165 * Check to see if the tree needs to be collapsed.
1167 if (retval
&& (state
->path
.active
> 1)) {
1168 xfs_bmap_init(args
->flist
, args
->firstblock
);
1169 error
= xfs_da3_join(state
);
1171 error
= xfs_bmap_finish(&args
->trans
,
1178 xfs_bmap_cancel(args
->flist
);
1183 * bmap_finish() may have committed the last trans
1184 * and started a new one. We need the inode to be
1185 * in all transactions.
1188 xfs_trans_ijoin(args
->trans
, dp
, 0);
1192 * Commit and start the next trans in the chain.
1194 error
= xfs_trans_roll(&args
->trans
, dp
);
1198 } else if (args
->rmtblkno
> 0) {
1200 * Added a "remote" value, just clear the incomplete flag.
1202 error
= xfs_attr3_leaf_clearflag(args
);
1210 xfs_da_state_free(state
);
1217 * Remove a name from a B-tree attribute list.
1219 * This will involve walking down the Btree, and may involve joining
1220 * leaf nodes and even joining intermediate nodes up to and including
1221 * the root node (a special case of an intermediate node).
1224 xfs_attr_node_removename(xfs_da_args_t
*args
)
1226 xfs_da_state_t
*state
;
1227 xfs_da_state_blk_t
*blk
;
1230 int retval
, error
, committed
, forkoff
;
1232 trace_xfs_attr_node_removename(args
);
1235 * Tie a string around our finger to remind us where we are.
1238 state
= xfs_da_state_alloc();
1240 state
->mp
= dp
->i_mount
;
1241 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1242 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1245 * Search to see if name exists, and get back a pointer to it.
1247 error
= xfs_da3_node_lookup_int(state
, &retval
);
1248 if (error
|| (retval
!= EEXIST
)) {
1255 * If there is an out-of-line value, de-allocate the blocks.
1256 * This is done before we remove the attribute so that we don't
1257 * overflow the maximum size of a transaction and/or hit a deadlock.
1259 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1260 ASSERT(blk
->bp
!= NULL
);
1261 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1262 if (args
->rmtblkno
> 0) {
1264 * Fill in disk block numbers in the state structure
1265 * so that we can get the buffers back after we commit
1266 * several transactions in the following calls.
1268 error
= xfs_attr_fillstate(state
);
1273 * Mark the attribute as INCOMPLETE, then bunmapi() the
1276 error
= xfs_attr3_leaf_setflag(args
);
1279 error
= xfs_attr_rmtval_remove(args
);
1284 * Refill the state structure with buffers, the prior calls
1285 * released our buffers.
1287 error
= xfs_attr_refillstate(state
);
1293 * Remove the name and update the hashvals in the tree.
1295 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1296 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1297 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1298 xfs_da3_fixhashpath(state
, &state
->path
);
1301 * Check to see if the tree needs to be collapsed.
1303 if (retval
&& (state
->path
.active
> 1)) {
1304 xfs_bmap_init(args
->flist
, args
->firstblock
);
1305 error
= xfs_da3_join(state
);
1307 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1313 xfs_bmap_cancel(args
->flist
);
1318 * bmap_finish() may have committed the last trans and started
1319 * a new one. We need the inode to be in all transactions.
1322 xfs_trans_ijoin(args
->trans
, dp
, 0);
1325 * Commit the Btree join operation and start a new trans.
1327 error
= xfs_trans_roll(&args
->trans
, dp
);
1333 * If the result is small enough, push it all into the inode.
1335 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1337 * Have to get rid of the copy of this dabuf in the state.
1339 ASSERT(state
->path
.active
== 1);
1340 ASSERT(state
->path
.blk
[0].bp
);
1341 state
->path
.blk
[0].bp
= NULL
;
1343 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1347 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1348 xfs_bmap_init(args
->flist
, args
->firstblock
);
1349 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1350 /* bp is gone due to xfs_da_shrink_inode */
1352 error
= xfs_bmap_finish(&args
->trans
,
1359 xfs_bmap_cancel(args
->flist
);
1364 * bmap_finish() may have committed the last trans
1365 * and started a new one. We need the inode to be
1366 * in all transactions.
1369 xfs_trans_ijoin(args
->trans
, dp
, 0);
1371 xfs_trans_brelse(args
->trans
, bp
);
1376 xfs_da_state_free(state
);
1381 * Fill in the disk block numbers in the state structure for the buffers
1382 * that are attached to the state structure.
1383 * This is done so that we can quickly reattach ourselves to those buffers
1384 * after some set of transaction commits have released these buffers.
1387 xfs_attr_fillstate(xfs_da_state_t
*state
)
1389 xfs_da_state_path_t
*path
;
1390 xfs_da_state_blk_t
*blk
;
1393 trace_xfs_attr_fillstate(state
->args
);
1396 * Roll down the "path" in the state structure, storing the on-disk
1397 * block number for those buffers in the "path".
1399 path
= &state
->path
;
1400 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1401 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1403 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1406 blk
->disk_blkno
= 0;
1411 * Roll down the "altpath" in the state structure, storing the on-disk
1412 * block number for those buffers in the "altpath".
1414 path
= &state
->altpath
;
1415 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1416 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1418 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1421 blk
->disk_blkno
= 0;
1429 * Reattach the buffers to the state structure based on the disk block
1430 * numbers stored in the state structure.
1431 * This is done after some set of transaction commits have released those
1432 * buffers from our grip.
1435 xfs_attr_refillstate(xfs_da_state_t
*state
)
1437 xfs_da_state_path_t
*path
;
1438 xfs_da_state_blk_t
*blk
;
1441 trace_xfs_attr_refillstate(state
->args
);
1444 * Roll down the "path" in the state structure, storing the on-disk
1445 * block number for those buffers in the "path".
1447 path
= &state
->path
;
1448 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1449 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1450 if (blk
->disk_blkno
) {
1451 error
= xfs_da3_node_read(state
->args
->trans
,
1453 blk
->blkno
, blk
->disk_blkno
,
1454 &blk
->bp
, XFS_ATTR_FORK
);
1463 * Roll down the "altpath" in the state structure, storing the on-disk
1464 * block number for those buffers in the "altpath".
1466 path
= &state
->altpath
;
1467 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1468 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1469 if (blk
->disk_blkno
) {
1470 error
= xfs_da3_node_read(state
->args
->trans
,
1472 blk
->blkno
, blk
->disk_blkno
,
1473 &blk
->bp
, XFS_ATTR_FORK
);
1485 * Look up a filename in a node attribute list.
1487 * This routine gets called for any attribute fork that has more than one
1488 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1489 * "remote" values taking up more blocks.
1492 xfs_attr_node_get(xfs_da_args_t
*args
)
1494 xfs_da_state_t
*state
;
1495 xfs_da_state_blk_t
*blk
;
1499 trace_xfs_attr_node_get(args
);
1501 state
= xfs_da_state_alloc();
1503 state
->mp
= args
->dp
->i_mount
;
1504 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1505 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1508 * Search to see if name exists, and get back a pointer to it.
1510 error
= xfs_da3_node_lookup_int(state
, &retval
);
1513 } else if (retval
== EEXIST
) {
1514 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1515 ASSERT(blk
->bp
!= NULL
);
1516 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1519 * Get the value, local or "remote"
1521 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1522 if (!retval
&& (args
->rmtblkno
> 0)
1523 && !(args
->flags
& ATTR_KERNOVAL
)) {
1524 retval
= xfs_attr_rmtval_get(args
);
1529 * If not in a transaction, we have to release all the buffers.
1531 for (i
= 0; i
< state
->path
.active
; i
++) {
1532 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1533 state
->path
.blk
[i
].bp
= NULL
;
1536 xfs_da_state_free(state
);