Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003   File: fs/ext4/xattr.h
0004 
0005   On-disk format of extended attributes for the ext4 filesystem.
0006 
0007   (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
0008 */
0009 
0010 #include <linux/xattr.h>
0011 
0012 /* Magic value in attribute blocks */
0013 #define EXT4_XATTR_MAGIC        0xEA020000
0014 
0015 /* Maximum number of references to one attribute block */
0016 #define EXT4_XATTR_REFCOUNT_MAX     1024
0017 
0018 /* Name indexes */
0019 #define EXT4_XATTR_INDEX_USER           1
0020 #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS   2
0021 #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT  3
0022 #define EXT4_XATTR_INDEX_TRUSTED        4
0023 #define EXT4_XATTR_INDEX_LUSTRE         5
0024 #define EXT4_XATTR_INDEX_SECURITY           6
0025 #define EXT4_XATTR_INDEX_SYSTEM         7
0026 #define EXT4_XATTR_INDEX_RICHACL        8
0027 #define EXT4_XATTR_INDEX_ENCRYPTION     9
0028 #define EXT4_XATTR_INDEX_HURD           10 /* Reserved for Hurd */
0029 
0030 struct ext4_xattr_header {
0031     __le32  h_magic;    /* magic number for identification */
0032     __le32  h_refcount; /* reference count */
0033     __le32  h_blocks;   /* number of disk blocks used */
0034     __le32  h_hash;     /* hash value of all attributes */
0035     __le32  h_checksum; /* crc32c(uuid+id+xattrblock) */
0036                 /* id = inum if refcount=1, blknum otherwise */
0037     __u32   h_reserved[3];  /* zero right now */
0038 };
0039 
0040 struct ext4_xattr_ibody_header {
0041     __le32  h_magic;    /* magic number for identification */
0042 };
0043 
0044 struct ext4_xattr_entry {
0045     __u8    e_name_len; /* length of name */
0046     __u8    e_name_index;   /* attribute name index */
0047     __le16  e_value_offs;   /* offset in disk block of value */
0048     __le32  e_value_inum;   /* inode in which the value is stored */
0049     __le32  e_value_size;   /* size of attribute value */
0050     __le32  e_hash;     /* hash value of name and value */
0051     char    e_name[];   /* attribute name */
0052 };
0053 
0054 #define EXT4_XATTR_PAD_BITS     2
0055 #define EXT4_XATTR_PAD      (1<<EXT4_XATTR_PAD_BITS)
0056 #define EXT4_XATTR_ROUND        (EXT4_XATTR_PAD-1)
0057 #define EXT4_XATTR_LEN(name_len) \
0058     (((name_len) + EXT4_XATTR_ROUND + \
0059     sizeof(struct ext4_xattr_entry)) & ~EXT4_XATTR_ROUND)
0060 #define EXT4_XATTR_NEXT(entry) \
0061     ((struct ext4_xattr_entry *)( \
0062      (char *)(entry) + EXT4_XATTR_LEN((entry)->e_name_len)))
0063 #define EXT4_XATTR_SIZE(size) \
0064     (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND)
0065 
0066 #define IHDR(inode, raw_inode) \
0067     ((struct ext4_xattr_ibody_header *) \
0068         ((void *)raw_inode + \
0069         EXT4_GOOD_OLD_INODE_SIZE + \
0070         EXT4_I(inode)->i_extra_isize))
0071 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
0072 
0073 /*
0074  * XATTR_SIZE_MAX is currently 64k, but for the purposes of checking
0075  * for file system consistency errors, we use a somewhat bigger value.
0076  * This allows XATTR_SIZE_MAX to grow in the future, but by using this
0077  * instead of INT_MAX for certain consistency checks, we don't need to
0078  * worry about arithmetic overflows.  (Actually XATTR_SIZE_MAX is
0079  * defined in include/uapi/linux/limits.h, so changing it is going
0080  * not going to be trivial....)
0081  */
0082 #define EXT4_XATTR_SIZE_MAX (1 << 24)
0083 
0084 /*
0085  * The minimum size of EA value when you start storing it in an external inode
0086  * size of block - size of header - size of 1 entry - 4 null bytes
0087  */
0088 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b)                 \
0089     ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
0090 
0091 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
0092 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
0093 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
0094 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
0095 
0096 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
0097 
0098 /*
0099  * If we want to add an xattr to the inode, we should make sure that
0100  * i_extra_isize is not 0 and that the inode size is not less than
0101  * EXT4_GOOD_OLD_INODE_SIZE + extra_isize + pad.
0102  *   EXT4_GOOD_OLD_INODE_SIZE   extra_isize header   entry   pad  data
0103  * |--------------------------|------------|------|---------|---|-------|
0104  */
0105 #define EXT4_INODE_HAS_XATTR_SPACE(inode)               \
0106     ((EXT4_I(inode)->i_extra_isize != 0) &&             \
0107      (EXT4_GOOD_OLD_INODE_SIZE + EXT4_I(inode)->i_extra_isize + \
0108       sizeof(struct ext4_xattr_ibody_header) + EXT4_XATTR_PAD <=    \
0109       EXT4_INODE_SIZE((inode)->i_sb)))
0110 
0111 struct ext4_xattr_info {
0112     const char *name;
0113     const void *value;
0114     size_t value_len;
0115     int name_index;
0116     int in_inode;
0117 };
0118 
0119 struct ext4_xattr_search {
0120     struct ext4_xattr_entry *first;
0121     void *base;
0122     void *end;
0123     struct ext4_xattr_entry *here;
0124     int not_found;
0125 };
0126 
0127 struct ext4_xattr_ibody_find {
0128     struct ext4_xattr_search s;
0129     struct ext4_iloc iloc;
0130 };
0131 
0132 struct ext4_xattr_inode_array {
0133     unsigned int count;     /* # of used items in the array */
0134     struct inode *inodes[];
0135 };
0136 
0137 extern const struct xattr_handler ext4_xattr_user_handler;
0138 extern const struct xattr_handler ext4_xattr_trusted_handler;
0139 extern const struct xattr_handler ext4_xattr_security_handler;
0140 extern const struct xattr_handler ext4_xattr_hurd_handler;
0141 
0142 #define EXT4_XATTR_NAME_ENCRYPTION_CONTEXT "c"
0143 
0144 /*
0145  * The EXT4_STATE_NO_EXPAND is overloaded and used for two purposes.
0146  * The first is to signal that there the inline xattrs and data are
0147  * taking up so much space that we might as well not keep trying to
0148  * expand it.  The second is that xattr_sem is taken for writing, so
0149  * we shouldn't try to recurse into the inode expansion.  For this
0150  * second case, we need to make sure that we take save and restore the
0151  * NO_EXPAND state flag appropriately.
0152  */
0153 static inline void ext4_write_lock_xattr(struct inode *inode, int *save)
0154 {
0155     down_write(&EXT4_I(inode)->xattr_sem);
0156     *save = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
0157     ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
0158 }
0159 
0160 static inline int ext4_write_trylock_xattr(struct inode *inode, int *save)
0161 {
0162     if (down_write_trylock(&EXT4_I(inode)->xattr_sem) == 0)
0163         return 0;
0164     *save = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
0165     ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
0166     return 1;
0167 }
0168 
0169 static inline void ext4_write_unlock_xattr(struct inode *inode, int *save)
0170 {
0171     if (*save == 0)
0172         ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
0173     up_write(&EXT4_I(inode)->xattr_sem);
0174 }
0175 
0176 extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
0177 
0178 extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
0179 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
0180 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
0181 extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
0182                   bool is_create, int *credits);
0183 extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
0184                 struct buffer_head *block_bh, size_t value_len,
0185                 bool is_create);
0186 
0187 extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
0188                    struct ext4_xattr_inode_array **array,
0189                    int extra_credits);
0190 extern void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *array);
0191 
0192 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
0193                 struct ext4_inode *raw_inode, handle_t *handle);
0194 extern void ext4_evict_ea_inode(struct inode *inode);
0195 
0196 extern const struct xattr_handler *ext4_xattr_handlers[];
0197 
0198 extern int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
0199                  struct ext4_xattr_ibody_find *is);
0200 extern int ext4_xattr_ibody_get(struct inode *inode, int name_index,
0201                 const char *name,
0202                 void *buffer, size_t buffer_size);
0203 extern int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
0204                 struct ext4_xattr_info *i,
0205                 struct ext4_xattr_ibody_find *is);
0206 
0207 extern struct mb_cache *ext4_xattr_create_cache(void);
0208 extern void ext4_xattr_destroy_cache(struct mb_cache *);
0209 
0210 #ifdef CONFIG_EXT4_FS_SECURITY
0211 extern int ext4_init_security(handle_t *handle, struct inode *inode,
0212                   struct inode *dir, const struct qstr *qstr);
0213 #else
0214 static inline int ext4_init_security(handle_t *handle, struct inode *inode,
0215                      struct inode *dir, const struct qstr *qstr)
0216 {
0217     return 0;
0218 }
0219 #endif
0220 
0221 #ifdef CONFIG_LOCKDEP
0222 extern void ext4_xattr_inode_set_class(struct inode *ea_inode);
0223 #else
0224 static inline void ext4_xattr_inode_set_class(struct inode *ea_inode) { }
0225 #endif
0226 
0227 extern int ext4_get_inode_usage(struct inode *inode, qsize_t *usage);