1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2000,2002-2003,2005 Silicon Graphics, Inc.
11 struct xfs_attr_list_context;
14 * Large attribute lists are structured around Btrees where all the data
15 * elements are in the leaf nodes. Attribute names are hashed into an int,
16 * then that int is used as the index into the Btree. Since the hashval
17 * of an attribute name may not be unique, we may have duplicate keys.
18 * The internal links in the Btree are logical block offsets into the file.
20 * Small attribute lists use a different format and are packed as tightly
21 * as possible so as to fit into the literal area of the inode.
25 * The maximum size (into the kernel or returned from the kernel) of an
26 * attribute value or the buffer used for an attr_list() call. Larger
27 * sizes will result in an ERANGE return code.
29 #define ATTR_MAX_VALUELEN (64*1024) /* max length of a value */
31 static inline bool xfs_has_larp(struct xfs_mount *mp)
34 /* Logged xattrs require a V5 super for log_incompat */
35 return xfs_has_crc(mp) && xfs_globals.larp;
42 * Kernel-internal version of the attrlist cursor.
44 struct xfs_attrlist_cursor_kern {
45 __u32 hashval; /* hash value of next entry to add */
46 __u32 blkno; /* block containing entry (suggestion) */
47 __u32 offset; /* offset in list of equal-hashvals */
48 __u16 pad1; /* padding to match user-level */
49 __u8 pad2; /* padding to match user-level */
50 __u8 initted; /* T/F: cursor has been initialized */
54 /*========================================================================
55 * Structure used to pass context around among the routines.
56 *========================================================================*/
59 /* void; state communicated via *context */
60 typedef void (*put_listent_func_t)(struct xfs_attr_list_context *, int,
61 unsigned char *, int, int);
63 struct xfs_attr_list_context {
65 struct xfs_inode *dp; /* inode */
66 struct xfs_attrlist_cursor_kern cursor; /* position in list */
67 void *buffer; /* output buffer */
70 * Abort attribute list iteration if non-zero. Can be used to pass
71 * error values to the xfs_attr_list caller.
74 bool allow_incomplete;
76 ssize_t count; /* num used entries */
77 int dupcnt; /* count dup hashvals seen */
78 int bufsize; /* total buffer size */
79 int firstu; /* first used byte in buffer */
80 unsigned int attr_filter; /* XFS_ATTR_{ROOT,SECURE} */
81 int resynch; /* T/F: resynch with cursor */
82 put_listent_func_t put_listent; /* list output fmt function */
83 int index; /* index into output buffer */
88 * ========================================================================
89 * Structure used to pass context around among the delayed routines.
90 * ========================================================================
94 * Below is a state machine diagram for attr remove operations. The XFS_DAS_*
95 * states indicate places where the function would return -EAGAIN, and then
96 * immediately resume from after being called by the calling function. States
97 * marked as a "subroutine state" indicate that they belong to a subroutine, and
98 * so the calling function needs to pass them back to that subroutine to allow
99 * it to finish where it left off. But they otherwise do not have a role in the
100 * calling function other than just passing through.
102 * xfs_attr_remove_iter()
105 * have attr to remove? ──n──> done
110 * are we short form? ──y──> xfs_attr_shortform_remove ──> done
115 * are we leaf form? ──y──> xfs_attr_leaf_removename ──> done
120 * ┌── need to setup state?
125 * │ find attr and get state
126 * │ attr has remote blks? ──n─┐
128 * │ │ find and invalidate
129 * │ y the remote blocks.
130 * │ │ mark attr incomplete
131 * │ ├────────────────┘
135 * Have remote blks to remove? ───y─────┐
136 * │ ^ remove the blks
139 * │ XFS_DAS_RMTBLK <─n── done?
141 * │ one less blk to y
149 * ├─────────────────────────┘
154 * xfs_attr_node_remove_cleanup
166 * XFS_DAS_RM_SHRINK │
178 * Below is a state machine diagram for attr set operations.
180 * It seems the challenge with understanding this system comes from trying to
181 * absorb the state machine all at once, when really one should only be looking
182 * at it with in the context of a single function. Once a state sensitive
183 * function is called, the idea is that it "takes ownership" of the
184 * state machine. It isn't concerned with the states that may have belonged to
185 * it's calling parent. Only the states relevant to itself or any other
186 * subroutines there in. Once a calling function hands off the state machine to
187 * a subroutine, it needs to respect the simple rule that it doesn't "own" the
188 * state machine anymore, and it's the responsibility of that calling function
189 * to propagate the -EAGAIN back up the call stack. Upon reentry, it is
190 * committed to re-calling that subroutine until it returns something other than
191 * -EAGAIN. Once that subroutine signals completion (by returning anything other
192 * than -EAGAIN), the calling function can resume using the state machine.
194 * xfs_attr_set_iter()
197 * ┌─y─ has an attr fork?
215 * │ xfs_attr_try_sf_addname
218 * │ had enough ──y──> done
224 * │ transform to leaf
227 * │ hold the leaf buffer
234 * └─> release leaf buffer
244 * │ xfs_attr_leaf_try_add()
247 * │ had enough ──────────────y─────────────┐
260 * xfs_attr_node_addname_find_attr │
261 * determines if this │
262 * is create or rename │
263 * find space to store attr │
266 * xfs_attr_node_addname │
269 * fits in a node leaf? ────n─────┐ │
271 * │ │ single leaf node? │
276 * update │ grow the leaf split if │
277 * hashvals └── return -EAGAIN needed │
278 * │ retry leaf add │ │
280 * ├────────────────────────────┘ │
284 * ┌─y── or flip flag? │
292 * │ XFS_DAS_FOUND_LBLK <────────────────┘
295 * │ xfs_attr_leaf_addname()
298 * │ ┌──first time through?
303 * │ │ if we have rmt blks
304 * │ │ find space for them
310 * │ ┌─n─ blks to alloc? <──┐
315 * │ │ alloc one blk │
316 * │ │ return -EAGAIN ──┘
317 * │ │ re-enter with one
318 * │ │ less blk to alloc
321 * │ └───> set the rmt
331 * │ flip incomplete │
335 * │ XFS_DAS_FLIP_LFLAG │
347 * │ XFS_DAS_RM_LBLK │ │
356 * │ XFS_DAS_RD_LEAF │
368 * └──────> XFS_DAS_FOUND_NBLK
380 * │ ┌─>XFS_DAS_ALLOC_NODE
386 * │ └──y── need to alloc
392 * │ set the rmt value
396 * └────────> a rename? ──n─┐
405 * XFS_DAS_FLIP_NFLAG │
415 * ┌────────> old blks │
417 * XFS_DAS_RM_NBLK │ │
420 * └──────y── more to │
438 * Enum values for xfs_attr_intent.xattri_da_state
440 * These values are used by delayed attribute operations to keep track of where
441 * they were before they returned -EAGAIN. A return code of -EAGAIN signals the
442 * calling function to roll the transaction, and then call the subroutine to
443 * finish the operation. The enum is then used by the subroutine to jump back
444 * to where it was and resume executing where it left off.
446 enum xfs_delattr_state {
447 XFS_DAS_UNINIT = 0, /* No state has been set yet */
450 * Initial sequence states. The replace setup code relies on the
451 * ADD and REMOVE states for a specific format to be sequential so
452 * that we can transform the initial operation to be performed
453 * according to the xfs_has_larp() state easily.
455 XFS_DAS_SF_ADD, /* Initial sf add state */
456 XFS_DAS_SF_REMOVE, /* Initial sf replace/remove state */
458 XFS_DAS_LEAF_ADD, /* Initial leaf add state */
459 XFS_DAS_LEAF_REMOVE, /* Initial leaf replace/remove state */
461 XFS_DAS_NODE_ADD, /* Initial node add state */
462 XFS_DAS_NODE_REMOVE, /* Initial node replace/remove state */
464 /* Leaf state set/replace/remove sequence */
465 XFS_DAS_LEAF_SET_RMT, /* set a remote xattr from a leaf */
466 XFS_DAS_LEAF_ALLOC_RMT, /* We are allocating remote blocks */
467 XFS_DAS_LEAF_REPLACE, /* Perform replace ops on a leaf */
468 XFS_DAS_LEAF_REMOVE_OLD, /* Start removing old attr from leaf */
469 XFS_DAS_LEAF_REMOVE_RMT, /* A rename is removing remote blocks */
470 XFS_DAS_LEAF_REMOVE_ATTR, /* Remove the old attr from a leaf */
472 /* Node state sequence, must match leaf state above */
473 XFS_DAS_NODE_SET_RMT, /* set a remote xattr from a node */
474 XFS_DAS_NODE_ALLOC_RMT, /* We are allocating remote blocks */
475 XFS_DAS_NODE_REPLACE, /* Perform replace ops on a node */
476 XFS_DAS_NODE_REMOVE_OLD, /* Start removing old attr from node */
477 XFS_DAS_NODE_REMOVE_RMT, /* A rename is removing remote blocks */
478 XFS_DAS_NODE_REMOVE_ATTR, /* Remove the old attr from a node */
480 XFS_DAS_DONE, /* finished operation */
483 #define XFS_DAS_STRINGS \
484 { XFS_DAS_UNINIT, "XFS_DAS_UNINIT" }, \
485 { XFS_DAS_SF_ADD, "XFS_DAS_SF_ADD" }, \
486 { XFS_DAS_SF_REMOVE, "XFS_DAS_SF_REMOVE" }, \
487 { XFS_DAS_LEAF_ADD, "XFS_DAS_LEAF_ADD" }, \
488 { XFS_DAS_LEAF_REMOVE, "XFS_DAS_LEAF_REMOVE" }, \
489 { XFS_DAS_NODE_ADD, "XFS_DAS_NODE_ADD" }, \
490 { XFS_DAS_NODE_REMOVE, "XFS_DAS_NODE_REMOVE" }, \
491 { XFS_DAS_LEAF_SET_RMT, "XFS_DAS_LEAF_SET_RMT" }, \
492 { XFS_DAS_LEAF_ALLOC_RMT, "XFS_DAS_LEAF_ALLOC_RMT" }, \
493 { XFS_DAS_LEAF_REPLACE, "XFS_DAS_LEAF_REPLACE" }, \
494 { XFS_DAS_LEAF_REMOVE_OLD, "XFS_DAS_LEAF_REMOVE_OLD" }, \
495 { XFS_DAS_LEAF_REMOVE_RMT, "XFS_DAS_LEAF_REMOVE_RMT" }, \
496 { XFS_DAS_LEAF_REMOVE_ATTR, "XFS_DAS_LEAF_REMOVE_ATTR" }, \
497 { XFS_DAS_NODE_SET_RMT, "XFS_DAS_NODE_SET_RMT" }, \
498 { XFS_DAS_NODE_ALLOC_RMT, "XFS_DAS_NODE_ALLOC_RMT" }, \
499 { XFS_DAS_NODE_REPLACE, "XFS_DAS_NODE_REPLACE" }, \
500 { XFS_DAS_NODE_REMOVE_OLD, "XFS_DAS_NODE_REMOVE_OLD" }, \
501 { XFS_DAS_NODE_REMOVE_RMT, "XFS_DAS_NODE_REMOVE_RMT" }, \
502 { XFS_DAS_NODE_REMOVE_ATTR, "XFS_DAS_NODE_REMOVE_ATTR" }, \
503 { XFS_DAS_DONE, "XFS_DAS_DONE" }
505 struct xfs_attri_log_nameval;
508 * Context used for keeping track of delayed attribute operations
510 struct xfs_attr_intent {
512 * used to log this item to an intent containing a list of attrs to
515 struct list_head xattri_list;
517 /* Used in xfs_attr_node_removename to roll through removing blocks */
518 struct xfs_da_state *xattri_da_state;
520 struct xfs_da_args *xattri_da_args;
523 * Shared buffer containing the attr name and value so that the logging
524 * code can share large memory buffers between log items.
526 struct xfs_attri_log_nameval *xattri_nameval;
529 * Used by xfs_attr_set to hold a leaf buffer across a transaction roll
531 struct xfs_buf *xattri_leaf_bp;
533 /* Used to keep track of current state of delayed operation */
534 enum xfs_delattr_state xattri_dela_state;
537 * Attr operation being performed - XFS_ATTRI_OP_FLAGS_*
539 unsigned int xattri_op_flags;
541 /* Used in xfs_attr_rmtval_set_blk to roll through allocating blocks */
542 xfs_dablk_t xattri_lblkno;
544 struct xfs_bmbt_irec xattri_map;
548 /*========================================================================
549 * Function prototypes for the kernel.
550 *========================================================================*/
553 * Overall external interface routines.
555 int xfs_attr_inactive(struct xfs_inode *dp);
556 int xfs_attr_list_ilocked(struct xfs_attr_list_context *);
557 int xfs_attr_list(struct xfs_attr_list_context *);
558 int xfs_inode_hasattr(struct xfs_inode *ip);
559 bool xfs_attr_is_leaf(struct xfs_inode *ip);
560 int xfs_attr_get_ilocked(struct xfs_da_args *args);
561 int xfs_attr_get(struct xfs_da_args *args);
562 int xfs_attr_set(struct xfs_da_args *args);
563 int xfs_attr_set_iter(struct xfs_attr_intent *attr);
564 int xfs_attr_remove_iter(struct xfs_attr_intent *attr);
565 bool xfs_attr_namecheck(const void *name, size_t length);
566 int xfs_attr_calc_size(struct xfs_da_args *args, int *local);
567 void xfs_init_attr_trans(struct xfs_da_args *args, struct xfs_trans_res *tres,
568 unsigned int *total);
571 * Check to see if the attr should be upgraded from non-existent or shortform to
572 * single-leaf-block attribute list.
575 xfs_attr_is_shortform(
576 struct xfs_inode *ip)
578 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
579 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
580 ip->i_afp->if_nextents == 0);
583 static inline enum xfs_delattr_state
584 xfs_attr_init_add_state(struct xfs_da_args *args)
587 * When called from the completion of a attr remove to determine the
588 * next state, the attribute fork may be null. This can occur only occur
589 * on a pure remove, but we grab the next state before we check if a
590 * replace operation is being performed. If we are called from any other
591 * context, i_afp is guaranteed to exist. Hence if the attr fork is
592 * null, we were called from a pure remove operation and so we are done.
594 if (!args->dp->i_afp)
597 args->op_flags |= XFS_DA_OP_ADDNAME;
598 if (xfs_attr_is_shortform(args->dp))
599 return XFS_DAS_SF_ADD;
600 if (xfs_attr_is_leaf(args->dp))
601 return XFS_DAS_LEAF_ADD;
602 return XFS_DAS_NODE_ADD;
605 static inline enum xfs_delattr_state
606 xfs_attr_init_remove_state(struct xfs_da_args *args)
608 args->op_flags |= XFS_DA_OP_REMOVE;
609 if (xfs_attr_is_shortform(args->dp))
610 return XFS_DAS_SF_REMOVE;
611 if (xfs_attr_is_leaf(args->dp))
612 return XFS_DAS_LEAF_REMOVE;
613 return XFS_DAS_NODE_REMOVE;
617 * If we are logging the attributes, then we have to start with removal of the
618 * old attribute so that there is always consistent state that we can recover
619 * from if the system goes down part way through. We always log the new attr
620 * value, so even when we remove the attr first we still have the information in
621 * the log to finish the replace operation atomically.
623 static inline enum xfs_delattr_state
624 xfs_attr_init_replace_state(struct xfs_da_args *args)
626 args->op_flags |= XFS_DA_OP_ADDNAME | XFS_DA_OP_REPLACE;
627 if (xfs_has_larp(args->dp->i_mount))
628 return xfs_attr_init_remove_state(args);
629 return xfs_attr_init_add_state(args);
632 extern struct kmem_cache *xfs_attr_intent_cache;
633 int __init xfs_attr_intent_init_cache(void);
634 void xfs_attr_intent_destroy_cache(void);
636 #endif /* __XFS_ATTR_H__ */