1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_da_format.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_attr_sf.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
20 #include "xfs_bmap_btree.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_quota.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_trace.h"
27 #include "xfs_attr_item.h"
30 struct kmem_cache *xfs_attri_cache;
31 struct kmem_cache *xfs_attrd_cache;
36 * Provide the external interfaces to manage attribute lists.
39 /*========================================================================
40 * Function prototypes for the kernel.
41 *========================================================================*/
44 * Internal routines when attribute list fits inside the inode.
46 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
49 * Internal routines when attribute list is one block.
51 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
53 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
54 STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args, struct xfs_buf *bp);
57 * Internal routines when attribute list is more than one block.
59 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
60 STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
61 STATIC int xfs_attr_node_addname(struct xfs_attr_item *attr);
62 STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_item *attr);
63 STATIC int xfs_attr_node_addname_clear_incomplete(struct xfs_attr_item *attr);
64 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
65 struct xfs_da_state **state);
66 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
67 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
68 STATIC int xfs_attr_node_removename(struct xfs_da_args *args,
69 struct xfs_da_state *state);
75 if (!XFS_IFORK_Q(ip) ||
76 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
77 ip->i_afp->if_nextents == 0))
83 * Returns true if the there is exactly only block in the attr fork, in which
84 * case the attribute fork consists of a single leaf block entry.
90 struct xfs_ifork *ifp = ip->i_afp;
91 struct xfs_iext_cursor icur;
92 struct xfs_bmbt_irec imap;
94 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
97 xfs_iext_first(ifp, &icur);
98 xfs_iext_get_extent(ifp, &icur, &imap);
99 return imap.br_startoff == 0 && imap.br_blockcount == 1;
102 /*========================================================================
103 * Overall external interface routines.
104 *========================================================================*/
107 * Retrieve an extended attribute and its value. Must have ilock.
108 * Returns 0 on successful retrieval, otherwise an error.
111 xfs_attr_get_ilocked(
112 struct xfs_da_args *args)
114 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
116 if (!xfs_inode_hasattr(args->dp))
119 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
120 return xfs_attr_shortform_getvalue(args);
121 if (xfs_attr_is_leaf(args->dp))
122 return xfs_attr_leaf_get(args);
123 return xfs_attr_node_get(args);
127 * Retrieve an extended attribute by name, and its value if requested.
129 * If args->valuelen is zero, then the caller does not want the value, just an
130 * indication whether the attribute exists and the size of the value if it
131 * exists. The size is returned in args.valuelen.
133 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
134 * for the value after existence of the attribute has been determined. The
135 * caller always has to free args->value if it is set, no matter if this
136 * function was successful or not.
138 * If the attribute is found, but exceeds the size limit set by the caller in
139 * args->valuelen, return -ERANGE with the size of the attribute that was found
144 struct xfs_da_args *args)
149 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
151 if (xfs_is_shutdown(args->dp->i_mount))
154 args->geo = args->dp->i_mount->m_attr_geo;
155 args->whichfork = XFS_ATTR_FORK;
156 args->hashval = xfs_da_hashname(args->name, args->namelen);
158 /* Entirely possible to look up a name which doesn't exist */
159 args->op_flags = XFS_DA_OP_OKNOENT;
161 lock_mode = xfs_ilock_attr_map_shared(args->dp);
162 error = xfs_attr_get_ilocked(args);
163 xfs_iunlock(args->dp, lock_mode);
169 * Calculate how many blocks we need for the new attribute,
173 struct xfs_da_args *args,
176 struct xfs_mount *mp = args->dp->i_mount;
181 * Determine space new attribute will use, and if it would be
182 * "local" or "remote" (note: local != inline).
184 size = xfs_attr_leaf_newentsize(args, local);
185 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
187 if (size > (args->geo->blksize / 2)) {
188 /* Double split possible */
193 * Out of line attribute, cannot double split, but
194 * make room for the attribute value itself.
196 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
198 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
204 /* Initialize transaction reservation for attr operations */
207 struct xfs_da_args *args,
208 struct xfs_trans_res *tres,
211 struct xfs_mount *mp = args->dp->i_mount;
214 tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
215 M_RES(mp)->tr_attrsetrt.tr_logres *
217 tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
218 tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
219 *total = args->total;
221 *tres = M_RES(mp)->tr_attrrm;
222 *total = XFS_ATTRRM_SPACE_RES(mp);
227 xfs_attr_try_sf_addname(
228 struct xfs_inode *dp,
229 struct xfs_da_args *args)
235 * Build initial attribute list (if required).
237 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
238 xfs_attr_shortform_create(args);
240 error = xfs_attr_shortform_addname(args);
241 if (error == -ENOSPC)
245 * Commit the shortform mods, and we're done.
246 * NOTE: this is also the error path (EEXIST, etc).
248 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
249 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
251 if (xfs_has_wsync(dp->i_mount))
252 xfs_trans_set_sync(args->trans);
258 * Check to see if the attr should be upgraded from non-existent or shortform to
259 * single-leaf-block attribute list.
262 xfs_attr_is_shortform(
263 struct xfs_inode *ip)
265 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
266 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
267 ip->i_afp->if_nextents == 0);
272 struct xfs_attr_item *attr)
274 struct xfs_da_args *args = attr->xattri_da_args;
275 struct xfs_inode *dp = args->dp;
279 * Try to add the attr to the attribute list in the inode.
281 error = xfs_attr_try_sf_addname(dp, args);
283 /* Should only be 0, -EEXIST or -ENOSPC */
284 if (error != -ENOSPC)
288 * It won't fit in the shortform, transform to a leaf block. GROT:
289 * another possible req'mt for a double-split btree op.
291 error = xfs_attr_shortform_to_leaf(args, &attr->xattri_leaf_bp);
296 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
297 * push cannot grab the half-baked leaf buffer and run into problems
298 * with the write verifier.
300 xfs_trans_bhold(args->trans, attr->xattri_leaf_bp);
303 * We're still in XFS_DAS_UNINIT state here. We've converted
304 * the attr fork to leaf format and will restart with the leaf
307 trace_xfs_attr_sf_addname_return(XFS_DAS_UNINIT, args->dp);
312 xfs_attr_leaf_addname(
313 struct xfs_attr_item *attr)
315 struct xfs_da_args *args = attr->xattri_da_args;
316 struct xfs_inode *dp = args->dp;
317 enum xfs_delattr_state next_state = XFS_DAS_UNINIT;
320 if (xfs_attr_is_leaf(dp)) {
323 * Use the leaf buffer we may already hold locked as a result of
324 * a sf-to-leaf conversion. The held buffer is no longer valid
325 * after this call, regardless of the result.
327 error = xfs_attr_leaf_try_add(args, attr->xattri_leaf_bp);
328 attr->xattri_leaf_bp = NULL;
330 if (error == -ENOSPC) {
331 error = xfs_attr3_leaf_to_node(args);
336 * Finish any deferred work items and roll the
337 * transaction once more. The goal here is to call
338 * node_addname with the inode and transaction in the
339 * same state (inode locked and joined, transaction
340 * clean) no matter how we got to this step.
342 * At this point, we are still in XFS_DAS_UNINIT, but
343 * when we come back, we'll be a node, so we'll fall
344 * down into the node handling code below
349 next_state = XFS_DAS_FOUND_LBLK;
351 ASSERT(!attr->xattri_leaf_bp);
353 error = xfs_attr_node_addname_find_attr(attr);
357 next_state = XFS_DAS_FOUND_NBLK;
358 error = xfs_attr_node_addname(attr);
364 * We need to commit and roll if we need to allocate remote xattr blocks
365 * or perform more xattr manipulations. Otherwise there is nothing more
366 * to do and we can return success.
368 if (args->rmtblkno ||
369 (args->op_flags & XFS_DA_OP_RENAME)) {
370 attr->xattri_dela_state = next_state;
375 trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
380 * Set the attribute specified in @args.
381 * This routine is meant to function as a delayed operation, and may return
382 * -EAGAIN when the transaction needs to be rolled. Calling functions will need
383 * to handle this, and recall the function until a successful error code is
388 struct xfs_attr_item *attr)
390 struct xfs_da_args *args = attr->xattri_da_args;
391 struct xfs_inode *dp = args->dp;
392 struct xfs_buf *bp = NULL;
393 int forkoff, error = 0;
394 struct xfs_mount *mp = args->dp->i_mount;
396 /* State machine switch */
397 switch (attr->xattri_dela_state) {
400 * If the fork is shortform, attempt to add the attr. If there
401 * is no space, this converts to leaf format and returns
402 * -EAGAIN with the leaf buffer held across the roll. The caller
403 * will deal with a transaction roll error, but otherwise
404 * release the hold once we return with a clean transaction.
406 if (xfs_attr_is_shortform(dp))
407 return xfs_attr_sf_addname(attr);
408 return xfs_attr_leaf_addname(attr);
410 case XFS_DAS_FOUND_LBLK:
412 * If there was an out-of-line value, allocate the blocks we
413 * identified for its storage and copy the value. This is done
414 * after we create the attribute so that we don't overflow the
415 * maximum size of a transaction and/or hit a deadlock.
418 /* Open coded xfs_attr_rmtval_set without trans handling */
419 if ((attr->xattri_flags & XFS_DAC_LEAF_ADDNAME_INIT) == 0) {
420 attr->xattri_flags |= XFS_DAC_LEAF_ADDNAME_INIT;
421 if (args->rmtblkno > 0) {
422 error = xfs_attr_rmtval_find_space(attr);
429 * Repeat allocating remote blocks for the attr value until
430 * blkcnt drops to zero.
432 if (attr->xattri_blkcnt > 0) {
433 error = xfs_attr_rmtval_set_blk(attr);
436 trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
441 error = xfs_attr_rmtval_set_value(args);
446 * If this is not a rename, clear the incomplete flag and we're
449 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
450 if (args->rmtblkno > 0)
451 error = xfs_attr3_leaf_clearflag(args);
456 * If this is an atomic rename operation, we must "flip" the
457 * incomplete flags on the "new" and "old" attribute/value pairs
458 * so that one disappears and one appears atomically. Then we
459 * must remove the "old" attribute/value pair.
461 * In a separate transaction, set the incomplete flag on the
462 * "old" attr and clear the incomplete flag on the "new" attr.
464 if (!xfs_has_larp(mp)) {
465 error = xfs_attr3_leaf_flipflags(args);
469 * Commit the flag value change and start the next trans
472 attr->xattri_dela_state = XFS_DAS_FLIP_LFLAG;
473 trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
479 case XFS_DAS_FLIP_LFLAG:
481 * Dismantle the "old" attribute/value pair by removing a
482 * "remote" value (if it exists).
484 xfs_attr_restore_rmt_blk(args);
485 error = xfs_attr_rmtval_invalidate(args);
490 case XFS_DAS_RM_LBLK:
491 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
492 attr->xattri_dela_state = XFS_DAS_RM_LBLK;
493 if (args->rmtblkno) {
494 error = xfs_attr_rmtval_remove(attr);
495 if (error == -EAGAIN)
496 trace_xfs_attr_set_iter_return(
497 attr->xattri_dela_state, args->dp);
501 attr->xattri_dela_state = XFS_DAS_RD_LEAF;
502 trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
508 case XFS_DAS_RD_LEAF:
510 * This is the last step for leaf format. Read the block with
511 * the old attr, remove the old attr, check for shortform
512 * conversion and return.
514 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
519 xfs_attr3_leaf_remove(bp, args);
521 forkoff = xfs_attr_shortform_allfit(bp, dp);
523 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
524 /* bp is gone due to xfs_da_shrink_inode */
528 case XFS_DAS_FOUND_NBLK:
530 * Find space for remote blocks and fall into the allocation
533 if (args->rmtblkno > 0) {
534 error = xfs_attr_rmtval_find_space(attr);
540 case XFS_DAS_ALLOC_NODE:
542 * If there was an out-of-line value, allocate the blocks we
543 * identified for its storage and copy the value. This is done
544 * after we create the attribute so that we don't overflow the
545 * maximum size of a transaction and/or hit a deadlock.
547 attr->xattri_dela_state = XFS_DAS_ALLOC_NODE;
548 if (args->rmtblkno > 0) {
549 if (attr->xattri_blkcnt > 0) {
550 error = xfs_attr_rmtval_set_blk(attr);
553 trace_xfs_attr_set_iter_return(
554 attr->xattri_dela_state, args->dp);
558 error = xfs_attr_rmtval_set_value(args);
564 * If this was not a rename, clear the incomplete flag and we're
567 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
568 if (args->rmtblkno > 0)
569 error = xfs_attr3_leaf_clearflag(args);
574 * If this is an atomic rename operation, we must "flip" the
575 * incomplete flags on the "new" and "old" attribute/value pairs
576 * so that one disappears and one appears atomically. Then we
577 * must remove the "old" attribute/value pair.
579 * In a separate transaction, set the incomplete flag on the
580 * "old" attr and clear the incomplete flag on the "new" attr.
582 if (!xfs_has_larp(mp)) {
583 error = xfs_attr3_leaf_flipflags(args);
587 * Commit the flag value change and start the next trans
590 attr->xattri_dela_state = XFS_DAS_FLIP_NFLAG;
591 trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
597 case XFS_DAS_FLIP_NFLAG:
599 * Dismantle the "old" attribute/value pair by removing a
600 * "remote" value (if it exists).
602 xfs_attr_restore_rmt_blk(args);
604 error = xfs_attr_rmtval_invalidate(args);
609 case XFS_DAS_RM_NBLK:
610 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
611 attr->xattri_dela_state = XFS_DAS_RM_NBLK;
612 if (args->rmtblkno) {
613 error = xfs_attr_rmtval_remove(attr);
614 if (error == -EAGAIN)
615 trace_xfs_attr_set_iter_return(
616 attr->xattri_dela_state, args->dp);
621 attr->xattri_dela_state = XFS_DAS_CLR_FLAG;
622 trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
628 case XFS_DAS_CLR_FLAG:
630 * The last state for node format. Look up the old attr and
633 error = xfs_attr_node_addname_clear_incomplete(attr);
645 * Return EEXIST if attr is found, or ENOATTR if not
649 struct xfs_da_args *args)
651 struct xfs_inode *dp = args->dp;
652 struct xfs_buf *bp = NULL;
655 if (!xfs_inode_hasattr(dp))
658 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
659 return xfs_attr_sf_findname(args, NULL, NULL);
661 if (xfs_attr_is_leaf(dp)) {
662 error = xfs_attr_leaf_hasname(args, &bp);
665 xfs_trans_brelse(args->trans, bp);
670 return xfs_attr_node_hasname(args, NULL);
675 struct xfs_da_args *args,
676 unsigned int op_flags, /* op flag (set or remove) */
677 struct xfs_attr_item **attr) /* new xfs_attr_item */
680 struct xfs_attr_item *new;
682 new = kmem_zalloc(sizeof(struct xfs_attr_item), KM_NOFS);
683 new->xattri_op_flags = op_flags;
684 new->xattri_da_args = args;
690 /* Sets an attribute for an inode as a deferred operation */
693 struct xfs_da_args *args)
695 struct xfs_attr_item *new;
698 error = xfs_attr_item_init(args, XFS_ATTR_OP_FLAGS_SET, &new);
702 new->xattri_dela_state = XFS_DAS_UNINIT;
703 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
704 trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp);
709 /* Sets an attribute for an inode as a deferred operation */
711 xfs_attr_defer_replace(
712 struct xfs_da_args *args)
714 struct xfs_attr_item *new;
717 error = xfs_attr_item_init(args, XFS_ATTR_OP_FLAGS_REPLACE, &new);
721 new->xattri_dela_state = XFS_DAS_UNINIT;
722 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
723 trace_xfs_attr_defer_replace(new->xattri_dela_state, args->dp);
728 /* Removes an attribute for an inode as a deferred operation */
730 xfs_attr_defer_remove(
731 struct xfs_da_args *args)
734 struct xfs_attr_item *new;
737 error = xfs_attr_item_init(args, XFS_ATTR_OP_FLAGS_REMOVE, &new);
741 new->xattri_dela_state = XFS_DAS_UNINIT;
742 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
743 trace_xfs_attr_defer_remove(new->xattri_dela_state, args->dp);
749 * Note: If args->value is NULL the attribute will be removed, just like the
750 * Linux ->setattr API.
754 struct xfs_da_args *args)
756 struct xfs_inode *dp = args->dp;
757 struct xfs_mount *mp = dp->i_mount;
758 struct xfs_trans_res tres;
759 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
763 int delayed = xfs_has_larp(mp);
765 if (xfs_is_shutdown(dp->i_mount))
768 error = xfs_qm_dqattach(dp);
772 args->geo = mp->m_attr_geo;
773 args->whichfork = XFS_ATTR_FORK;
774 args->hashval = xfs_da_hashname(args->name, args->namelen);
777 * We have no control over the attribute names that userspace passes us
778 * to remove, so we have to allow the name lookup prior to attribute
779 * removal to fail as well.
781 args->op_flags = XFS_DA_OP_OKNOENT;
784 XFS_STATS_INC(mp, xs_attr_set);
786 args->op_flags |= XFS_DA_OP_ADDNAME;
787 args->total = xfs_attr_calc_size(args, &local);
790 * If the inode doesn't have an attribute fork, add one.
791 * (inode must not be locked when we call this routine)
793 if (XFS_IFORK_Q(dp) == 0) {
794 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
795 xfs_attr_sf_entsize_byname(args->namelen,
798 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
804 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
806 XFS_STATS_INC(mp, xs_attr_remove);
807 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
811 error = xfs_attr_use_log_assist(mp);
817 * Root fork attributes can use reserved data blocks for this
818 * operation if necessary
820 xfs_init_attr_trans(args, &tres, &total);
821 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
825 if (args->value || xfs_inode_hasattr(dp)) {
826 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
827 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
829 error = xfs_iext_count_upgrade(args->trans, dp,
830 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
832 goto out_trans_cancel;
835 error = xfs_attr_lookup(args);
838 /* if no value, we are performing a remove operation */
840 error = xfs_attr_defer_remove(args);
843 /* Pure create fails if the attr already exists */
844 if (args->attr_flags & XATTR_CREATE)
845 goto out_trans_cancel;
847 error = xfs_attr_defer_replace(args);
850 /* Can't remove what isn't there. */
852 goto out_trans_cancel;
854 /* Pure replace fails if no existing attr to replace. */
855 if (args->attr_flags & XATTR_REPLACE)
856 goto out_trans_cancel;
858 error = xfs_attr_defer_add(args);
861 goto out_trans_cancel;
864 goto out_trans_cancel;
867 * If this is a synchronous mount, make sure that the
868 * transaction goes to disk before returning to the user.
870 if (xfs_has_wsync(mp))
871 xfs_trans_set_sync(args->trans);
873 if (!(args->op_flags & XFS_DA_OP_NOTIME))
874 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
877 * Commit the last in the sequence of transactions.
879 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
880 error = xfs_trans_commit(args->trans);
882 xfs_iunlock(dp, XFS_ILOCK_EXCL);
885 xlog_drop_incompat_feat(mp->m_log);
890 xfs_trans_cancel(args->trans);
895 xfs_attri_init_cache(void)
897 xfs_attri_cache = kmem_cache_create("xfs_attri",
898 sizeof(struct xfs_attri_log_item),
901 return xfs_attri_cache != NULL ? 0 : -ENOMEM;
905 xfs_attri_destroy_cache(void)
907 kmem_cache_destroy(xfs_attri_cache);
908 xfs_attri_cache = NULL;
912 xfs_attrd_init_cache(void)
914 xfs_attrd_cache = kmem_cache_create("xfs_attrd",
915 sizeof(struct xfs_attrd_log_item),
918 return xfs_attrd_cache != NULL ? 0 : -ENOMEM;
922 xfs_attrd_destroy_cache(void)
924 kmem_cache_destroy(xfs_attrd_cache);
925 xfs_attrd_cache = NULL;
928 /*========================================================================
929 * External routines when attribute list is inside the inode
930 *========================================================================*/
932 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
934 struct xfs_attr_shortform *sf;
936 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
937 return be16_to_cpu(sf->hdr.totsize);
941 * Add a name to the shortform attribute list structure
942 * This is the external routine.
945 xfs_attr_shortform_addname(xfs_da_args_t *args)
947 int newsize, forkoff, retval;
949 trace_xfs_attr_sf_addname(args);
951 retval = xfs_attr_shortform_lookup(args);
952 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
954 if (retval == -EEXIST) {
955 if (args->attr_flags & XATTR_CREATE)
957 retval = xfs_attr_sf_removename(args);
961 * Since we have removed the old attr, clear ATTR_REPLACE so
962 * that the leaf format add routine won't trip over the attr
965 args->attr_flags &= ~XATTR_REPLACE;
968 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
969 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
972 newsize = xfs_attr_sf_totsize(args->dp);
973 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
975 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
979 xfs_attr_shortform_add(args, forkoff);
984 /*========================================================================
985 * External routines when attribute list is one block
986 *========================================================================*/
988 /* Store info about a remote block */
990 xfs_attr_save_rmt_blk(
991 struct xfs_da_args *args)
993 args->blkno2 = args->blkno;
994 args->index2 = args->index;
995 args->rmtblkno2 = args->rmtblkno;
996 args->rmtblkcnt2 = args->rmtblkcnt;
997 args->rmtvaluelen2 = args->rmtvaluelen;
1000 /* Set stored info about a remote block */
1002 xfs_attr_restore_rmt_blk(
1003 struct xfs_da_args *args)
1005 args->blkno = args->blkno2;
1006 args->index = args->index2;
1007 args->rmtblkno = args->rmtblkno2;
1008 args->rmtblkcnt = args->rmtblkcnt2;
1009 args->rmtvaluelen = args->rmtvaluelen2;
1013 * Tries to add an attribute to an inode in leaf form
1015 * This function is meant to execute as part of a delayed operation and leaves
1016 * the transaction handling to the caller. On success the attribute is added
1017 * and the inode and transaction are left dirty. If there is not enough space,
1018 * the attr data is converted to node format and -ENOSPC is returned. Caller is
1019 * responsible for handling the dirty inode and transaction or adding the attr
1023 xfs_attr_leaf_try_add(
1024 struct xfs_da_args *args,
1030 * If the caller provided a buffer to us, it is locked and held in
1031 * the transaction because it just did a shortform to leaf conversion.
1032 * Hence we don't need to read it again. Otherwise read in the leaf
1036 xfs_trans_bhold_release(args->trans, bp);
1038 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1044 * Look up the xattr name to set the insertion point for the new xattr.
1046 error = xfs_attr3_leaf_lookup_int(bp, args);
1047 if (error != -ENOATTR && error != -EEXIST)
1049 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
1051 if (error == -EEXIST) {
1052 if (args->attr_flags & XATTR_CREATE)
1055 trace_xfs_attr_leaf_replace(args);
1057 /* save the attribute state for later removal*/
1058 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
1059 xfs_attr_save_rmt_blk(args);
1062 * clear the remote attr state now that it is saved so that the
1063 * values reflect the state of the attribute we are about to
1064 * add, not the attribute we just found and will remove later.
1067 args->rmtblkcnt = 0;
1068 args->rmtvaluelen = 0;
1071 return xfs_attr3_leaf_add(bp, args);
1074 xfs_trans_brelse(args->trans, bp);
1079 * Return EEXIST if attr is found, or ENOATTR if not
1082 xfs_attr_leaf_hasname(
1083 struct xfs_da_args *args,
1084 struct xfs_buf **bp)
1088 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
1092 error = xfs_attr3_leaf_lookup_int(*bp, args);
1093 if (error != -ENOATTR && error != -EEXIST)
1094 xfs_trans_brelse(args->trans, *bp);
1100 * Remove a name from the leaf attribute list structure
1102 * This leaf block cannot have a "remote" value, we only call this routine
1103 * if bmap_one_block() says there is only one block (ie: no remote blks).
1106 xfs_attr_leaf_removename(
1107 struct xfs_da_args *args)
1109 struct xfs_inode *dp;
1113 trace_xfs_attr_leaf_removename(args);
1116 * Remove the attribute.
1120 error = xfs_attr_leaf_hasname(args, &bp);
1122 if (error == -ENOATTR) {
1123 xfs_trans_brelse(args->trans, bp);
1125 } else if (error != -EEXIST)
1128 xfs_attr3_leaf_remove(bp, args);
1131 * If the result is small enough, shrink it all into the inode.
1133 forkoff = xfs_attr_shortform_allfit(bp, dp);
1135 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1136 /* bp is gone due to xfs_da_shrink_inode */
1142 * Look up a name in a leaf attribute list structure.
1144 * This leaf block cannot have a "remote" value, we only call this routine
1145 * if bmap_one_block() says there is only one block (ie: no remote blks).
1147 * Returns 0 on successful retrieval, otherwise an error.
1150 xfs_attr_leaf_get(xfs_da_args_t *args)
1155 trace_xfs_attr_leaf_get(args);
1157 error = xfs_attr_leaf_hasname(args, &bp);
1159 if (error == -ENOATTR) {
1160 xfs_trans_brelse(args->trans, bp);
1162 } else if (error != -EEXIST)
1166 error = xfs_attr3_leaf_getvalue(bp, args);
1167 xfs_trans_brelse(args->trans, bp);
1172 * Return EEXIST if attr is found, or ENOATTR if not
1173 * statep: If not null is set to point at the found state. Caller will
1174 * be responsible for freeing the state in this case.
1177 xfs_attr_node_hasname(
1178 struct xfs_da_args *args,
1179 struct xfs_da_state **statep)
1181 struct xfs_da_state *state;
1184 state = xfs_da_state_alloc(args);
1189 * Search to see if name exists, and get back a pointer to it.
1191 error = xfs_da3_node_lookup_int(state, &retval);
1196 xfs_da_state_free(state);
1201 /*========================================================================
1202 * External routines when attribute list size > geo->blksize
1203 *========================================================================*/
1206 xfs_attr_node_addname_find_attr(
1207 struct xfs_attr_item *attr)
1209 struct xfs_da_args *args = attr->xattri_da_args;
1213 * Search to see if name already exists, and get back a pointer
1214 * to where it should go.
1216 retval = xfs_attr_node_hasname(args, &attr->xattri_da_state);
1217 if (retval != -ENOATTR && retval != -EEXIST)
1220 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
1222 if (retval == -EEXIST) {
1223 if (args->attr_flags & XATTR_CREATE)
1226 trace_xfs_attr_node_replace(args);
1228 /* save the attribute state for later removal*/
1229 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1230 xfs_attr_save_rmt_blk(args);
1233 * clear the remote attr state now that it is saved so that the
1234 * values reflect the state of the attribute we are about to
1235 * add, not the attribute we just found and will remove later.
1238 args->rmtblkcnt = 0;
1239 args->rmtvaluelen = 0;
1244 if (attr->xattri_da_state)
1245 xfs_da_state_free(attr->xattri_da_state);
1250 * Add a name to a Btree-format attribute list.
1252 * This will involve walking down the Btree, and may involve splitting
1253 * leaf nodes and even splitting intermediate nodes up to and including
1254 * the root node (a special case of an intermediate node).
1256 * "Remote" attribute values confuse the issue and atomic rename operations
1257 * add a whole extra layer of confusion on top of that.
1259 * This routine is meant to function as a delayed operation, and may return
1260 * -EAGAIN when the transaction needs to be rolled. Calling functions will need
1261 * to handle this, and recall the function until a successful error code is
1265 xfs_attr_node_addname(
1266 struct xfs_attr_item *attr)
1268 struct xfs_da_args *args = attr->xattri_da_args;
1269 struct xfs_da_state *state = attr->xattri_da_state;
1270 struct xfs_da_state_blk *blk;
1273 trace_xfs_attr_node_addname(args);
1275 blk = &state->path.blk[state->path.active-1];
1276 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1278 error = xfs_attr3_leaf_add(blk->bp, state->args);
1279 if (error == -ENOSPC) {
1280 if (state->path.active == 1) {
1282 * Its really a single leaf node, but it had
1283 * out-of-line values so it looked like it *might*
1284 * have been a b-tree.
1286 xfs_da_state_free(state);
1288 error = xfs_attr3_leaf_to_node(args);
1293 * Now that we have converted the leaf to a node, we can
1294 * roll the transaction, and try xfs_attr3_leaf_add
1295 * again on re-entry. No need to set dela_state to do
1296 * this. dela_state is still unset by this function at
1299 trace_xfs_attr_node_addname_return(
1300 attr->xattri_dela_state, args->dp);
1305 * Split as many Btree elements as required.
1306 * This code tracks the new and old attr's location
1307 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1308 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1310 error = xfs_da3_split(state);
1315 * Addition succeeded, update Btree hashvals.
1317 xfs_da3_fixhashpath(state, &state->path);
1322 xfs_da_state_free(state);
1328 xfs_attr_node_addname_clear_incomplete(
1329 struct xfs_attr_item *attr)
1331 struct xfs_da_args *args = attr->xattri_da_args;
1332 struct xfs_da_state *state = NULL;
1333 struct xfs_mount *mp = args->dp->i_mount;
1338 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1339 * flag means that we will find the "old" attr, not the "new" one.
1341 if (!xfs_has_larp(mp))
1342 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1343 state = xfs_da_state_alloc(args);
1345 error = xfs_da3_node_lookup_int(state, &retval);
1349 error = xfs_attr_node_removename(args, state);
1352 * Check to see if the tree needs to be collapsed.
1354 if (retval && (state->path.active > 1)) {
1355 error = xfs_da3_join(state);
1363 xfs_da_state_free(state);
1370 * Shrink an attribute from leaf to shortform
1373 xfs_attr_node_shrink(
1374 struct xfs_da_args *args,
1375 struct xfs_da_state *state)
1377 struct xfs_inode *dp = args->dp;
1382 * Have to get rid of the copy of this dabuf in the state.
1384 ASSERT(state->path.active == 1);
1385 ASSERT(state->path.blk[0].bp);
1386 state->path.blk[0].bp = NULL;
1388 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1392 forkoff = xfs_attr_shortform_allfit(bp, dp);
1394 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1395 /* bp is gone due to xfs_da_shrink_inode */
1397 xfs_trans_brelse(args->trans, bp);
1403 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1404 * for later deletion of the entry.
1407 xfs_attr_leaf_mark_incomplete(
1408 struct xfs_da_args *args,
1409 struct xfs_da_state *state)
1414 * Fill in disk block numbers in the state structure
1415 * so that we can get the buffers back after we commit
1416 * several transactions in the following calls.
1418 error = xfs_attr_fillstate(state);
1423 * Mark the attribute as INCOMPLETE
1425 return xfs_attr3_leaf_setflag(args);
1429 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1430 * the blocks are valid. Attr keys with remote blocks will be marked
1434 int xfs_attr_node_removename_setup(
1435 struct xfs_attr_item *attr)
1437 struct xfs_da_args *args = attr->xattri_da_args;
1438 struct xfs_da_state **state = &attr->xattri_da_state;
1441 error = xfs_attr_node_hasname(args, state);
1442 if (error != -EEXIST)
1446 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1447 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1448 XFS_ATTR_LEAF_MAGIC);
1450 if (args->rmtblkno > 0) {
1451 error = xfs_attr_leaf_mark_incomplete(args, *state);
1455 error = xfs_attr_rmtval_invalidate(args);
1459 xfs_da_state_free(*state);
1465 xfs_attr_node_removename(
1466 struct xfs_da_args *args,
1467 struct xfs_da_state *state)
1469 struct xfs_da_state_blk *blk;
1473 * Remove the name and update the hashvals in the tree.
1475 blk = &state->path.blk[state->path.active-1];
1476 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1477 retval = xfs_attr3_leaf_remove(blk->bp, args);
1478 xfs_da3_fixhashpath(state, &state->path);
1484 * Remove the attribute specified in @args.
1486 * This will involve walking down the Btree, and may involve joining
1487 * leaf nodes and even joining intermediate nodes up to and including
1488 * the root node (a special case of an intermediate node).
1490 * This routine is meant to function as either an in-line or delayed operation,
1491 * and may return -EAGAIN when the transaction needs to be rolled. Calling
1492 * functions will need to handle this, and call the function until a
1493 * successful error code is returned.
1496 xfs_attr_remove_iter(
1497 struct xfs_attr_item *attr)
1499 struct xfs_da_args *args = attr->xattri_da_args;
1500 struct xfs_da_state *state = attr->xattri_da_state;
1501 int retval, error = 0;
1502 struct xfs_inode *dp = args->dp;
1504 trace_xfs_attr_node_removename(args);
1506 switch (attr->xattri_dela_state) {
1507 case XFS_DAS_UNINIT:
1508 if (!xfs_inode_hasattr(dp))
1512 * Shortform or leaf formats don't require transaction rolls and
1513 * thus state transitions. Call the right helper and return.
1515 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
1516 return xfs_attr_sf_removename(args);
1518 if (xfs_attr_is_leaf(dp))
1519 return xfs_attr_leaf_removename(args);
1522 * Node format may require transaction rolls. Set up the
1523 * state context and fall into the state machine.
1525 if (!attr->xattri_da_state) {
1526 error = xfs_attr_node_removename_setup(attr);
1529 state = attr->xattri_da_state;
1533 case XFS_DAS_RMTBLK:
1534 attr->xattri_dela_state = XFS_DAS_RMTBLK;
1537 * If there is an out-of-line value, de-allocate the blocks.
1538 * This is done before we remove the attribute so that we don't
1539 * overflow the maximum size of a transaction and/or hit a
1542 if (args->rmtblkno > 0) {
1544 * May return -EAGAIN. Roll and repeat until all remote
1545 * blocks are removed.
1547 error = xfs_attr_rmtval_remove(attr);
1548 if (error == -EAGAIN) {
1549 trace_xfs_attr_remove_iter_return(
1550 attr->xattri_dela_state, args->dp);
1557 * Refill the state structure with buffers (the prior
1558 * calls released our buffers) and close out this
1559 * transaction before proceeding.
1561 ASSERT(args->rmtblkno == 0);
1562 error = xfs_attr_refillstate(state);
1566 attr->xattri_dela_state = XFS_DAS_RM_NAME;
1567 trace_xfs_attr_remove_iter_return(
1568 attr->xattri_dela_state, args->dp);
1573 case XFS_DAS_RM_NAME:
1575 * If we came here fresh from a transaction roll, reattach all
1576 * the buffers to the current transaction.
1578 if (attr->xattri_dela_state == XFS_DAS_RM_NAME) {
1579 error = xfs_attr_refillstate(state);
1584 retval = xfs_attr_node_removename(args, state);
1587 * Check to see if the tree needs to be collapsed. If so, roll
1588 * the transacton and fall into the shrink state.
1590 if (retval && (state->path.active > 1)) {
1591 error = xfs_da3_join(state);
1595 attr->xattri_dela_state = XFS_DAS_RM_SHRINK;
1596 trace_xfs_attr_remove_iter_return(
1597 attr->xattri_dela_state, args->dp);
1602 case XFS_DAS_RM_SHRINK:
1604 * If the result is small enough, push it all into the inode.
1605 * This is our final state so it's safe to return a dirty
1608 if (xfs_attr_is_leaf(dp))
1609 error = xfs_attr_node_shrink(args, state);
1610 ASSERT(error != -EAGAIN);
1619 xfs_da_state_free(state);
1624 * Fill in the disk block numbers in the state structure for the buffers
1625 * that are attached to the state structure.
1626 * This is done so that we can quickly reattach ourselves to those buffers
1627 * after some set of transaction commits have released these buffers.
1630 xfs_attr_fillstate(xfs_da_state_t *state)
1632 xfs_da_state_path_t *path;
1633 xfs_da_state_blk_t *blk;
1636 trace_xfs_attr_fillstate(state->args);
1639 * Roll down the "path" in the state structure, storing the on-disk
1640 * block number for those buffers in the "path".
1642 path = &state->path;
1643 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1644 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1646 blk->disk_blkno = xfs_buf_daddr(blk->bp);
1649 blk->disk_blkno = 0;
1654 * Roll down the "altpath" in the state structure, storing the on-disk
1655 * block number for those buffers in the "altpath".
1657 path = &state->altpath;
1658 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1659 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1661 blk->disk_blkno = xfs_buf_daddr(blk->bp);
1664 blk->disk_blkno = 0;
1672 * Reattach the buffers to the state structure based on the disk block
1673 * numbers stored in the state structure.
1674 * This is done after some set of transaction commits have released those
1675 * buffers from our grip.
1678 xfs_attr_refillstate(xfs_da_state_t *state)
1680 xfs_da_state_path_t *path;
1681 xfs_da_state_blk_t *blk;
1684 trace_xfs_attr_refillstate(state->args);
1687 * Roll down the "path" in the state structure, storing the on-disk
1688 * block number for those buffers in the "path".
1690 path = &state->path;
1691 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1692 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1693 if (blk->disk_blkno) {
1694 error = xfs_da3_node_read_mapped(state->args->trans,
1695 state->args->dp, blk->disk_blkno,
1696 &blk->bp, XFS_ATTR_FORK);
1705 * Roll down the "altpath" in the state structure, storing the on-disk
1706 * block number for those buffers in the "altpath".
1708 path = &state->altpath;
1709 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1710 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1711 if (blk->disk_blkno) {
1712 error = xfs_da3_node_read_mapped(state->args->trans,
1713 state->args->dp, blk->disk_blkno,
1714 &blk->bp, XFS_ATTR_FORK);
1726 * Retrieve the attribute data from a node attribute list.
1728 * This routine gets called for any attribute fork that has more than one
1729 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1730 * "remote" values taking up more blocks.
1732 * Returns 0 on successful retrieval, otherwise an error.
1736 struct xfs_da_args *args)
1738 struct xfs_da_state *state;
1739 struct xfs_da_state_blk *blk;
1743 trace_xfs_attr_node_get(args);
1746 * Search to see if name exists, and get back a pointer to it.
1748 error = xfs_attr_node_hasname(args, &state);
1749 if (error != -EEXIST)
1753 * Get the value, local or "remote"
1755 blk = &state->path.blk[state->path.active - 1];
1756 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1759 * If not in a transaction, we have to release all the buffers.
1762 for (i = 0; state != NULL && i < state->path.active; i++) {
1763 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1764 state->path.blk[i].bp = NULL;
1768 xfs_da_state_free(state);
1772 /* Returns true if the attribute entry name is valid. */
1779 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1780 * out, so use >= for the length check.
1782 if (length >= MAXNAMELEN)
1785 /* There shouldn't be any nulls here */
1786 return !memchr(name, 0, length);