1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao File: fs/ext4/xattr.h 3ac27a0ecSDave Kleikamp 4617ba13bSMingming Cao On-disk format of extended attributes for the ext4 filesystem. 5ac27a0ecSDave Kleikamp 6ac27a0ecSDave Kleikamp (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org> 7ac27a0ecSDave Kleikamp */ 8ac27a0ecSDave Kleikamp 9ac27a0ecSDave Kleikamp #include <linux/xattr.h> 10ac27a0ecSDave Kleikamp 11ac27a0ecSDave Kleikamp /* Magic value in attribute blocks */ 12617ba13bSMingming Cao #define EXT4_XATTR_MAGIC 0xEA020000 13ac27a0ecSDave Kleikamp 14ac27a0ecSDave Kleikamp /* Maximum number of references to one attribute block */ 15617ba13bSMingming Cao #define EXT4_XATTR_REFCOUNT_MAX 1024 16ac27a0ecSDave Kleikamp 17ac27a0ecSDave Kleikamp /* Name indexes */ 18617ba13bSMingming Cao #define EXT4_XATTR_INDEX_USER 1 19617ba13bSMingming Cao #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS 2 20617ba13bSMingming Cao #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT 3 21617ba13bSMingming Cao #define EXT4_XATTR_INDEX_TRUSTED 4 22617ba13bSMingming Cao #define EXT4_XATTR_INDEX_LUSTRE 5 23617ba13bSMingming Cao #define EXT4_XATTR_INDEX_SECURITY 6 2467cf5b09STao Ma #define EXT4_XATTR_INDEX_SYSTEM 7 25fd03d8daSTheodore Ts'o #define EXT4_XATTR_INDEX_RICHACL 8 26887e2c45SMichael Halcrow #define EXT4_XATTR_INDEX_ENCRYPTION 9 273980bd3bSTheodore Ts'o #define EXT4_XATTR_INDEX_HURD 10 /* Reserved for Hurd */ 28ac27a0ecSDave Kleikamp 29617ba13bSMingming Cao struct ext4_xattr_header { 30ac27a0ecSDave Kleikamp __le32 h_magic; /* magic number for identification */ 31ac27a0ecSDave Kleikamp __le32 h_refcount; /* reference count */ 32ac27a0ecSDave Kleikamp __le32 h_blocks; /* number of disk blocks used */ 33ac27a0ecSDave Kleikamp __le32 h_hash; /* hash value of all attributes */ 34e6153918SDarrick J. Wong __le32 h_checksum; /* crc32c(uuid+id+xattrblock) */ 35e6153918SDarrick J. Wong /* id = inum if refcount=1, blknum otherwise */ 36e6153918SDarrick J. Wong __u32 h_reserved[3]; /* zero right now */ 37ac27a0ecSDave Kleikamp }; 38ac27a0ecSDave Kleikamp 39617ba13bSMingming Cao struct ext4_xattr_ibody_header { 40ac27a0ecSDave Kleikamp __le32 h_magic; /* magic number for identification */ 41ac27a0ecSDave Kleikamp }; 42ac27a0ecSDave Kleikamp 43617ba13bSMingming Cao struct ext4_xattr_entry { 44ac27a0ecSDave Kleikamp __u8 e_name_len; /* length of name */ 45ac27a0ecSDave Kleikamp __u8 e_name_index; /* attribute name index */ 46ac27a0ecSDave Kleikamp __le16 e_value_offs; /* offset in disk block of value */ 47e50e5129SAndreas Dilger __le32 e_value_inum; /* inode in which the value is stored */ 48ac27a0ecSDave Kleikamp __le32 e_value_size; /* size of attribute value */ 49ac27a0ecSDave Kleikamp __le32 e_hash; /* hash value of name and value */ 50ac27a0ecSDave Kleikamp char e_name[0]; /* attribute name */ 51ac27a0ecSDave Kleikamp }; 52ac27a0ecSDave Kleikamp 53617ba13bSMingming Cao #define EXT4_XATTR_PAD_BITS 2 54617ba13bSMingming Cao #define EXT4_XATTR_PAD (1<<EXT4_XATTR_PAD_BITS) 55617ba13bSMingming Cao #define EXT4_XATTR_ROUND (EXT4_XATTR_PAD-1) 56617ba13bSMingming Cao #define EXT4_XATTR_LEN(name_len) \ 57617ba13bSMingming Cao (((name_len) + EXT4_XATTR_ROUND + \ 58617ba13bSMingming Cao sizeof(struct ext4_xattr_entry)) & ~EXT4_XATTR_ROUND) 59617ba13bSMingming Cao #define EXT4_XATTR_NEXT(entry) \ 60617ba13bSMingming Cao ((struct ext4_xattr_entry *)( \ 61617ba13bSMingming Cao (char *)(entry) + EXT4_XATTR_LEN((entry)->e_name_len))) 62617ba13bSMingming Cao #define EXT4_XATTR_SIZE(size) \ 63617ba13bSMingming Cao (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND) 64ac27a0ecSDave Kleikamp 656dd4ee7cSKalpak Shah #define IHDR(inode, raw_inode) \ 666dd4ee7cSKalpak Shah ((struct ext4_xattr_ibody_header *) \ 676dd4ee7cSKalpak Shah ((void *)raw_inode + \ 686dd4ee7cSKalpak Shah EXT4_GOOD_OLD_INODE_SIZE + \ 696dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize)) 706dd4ee7cSKalpak Shah #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1)) 716dd4ee7cSKalpak Shah 72e50e5129SAndreas Dilger /* 73e50e5129SAndreas Dilger * Link EA inode back to parent one using i_mtime field. 74e50e5129SAndreas Dilger * Extra integer type conversion added to ignore higher 75e50e5129SAndreas Dilger * bits in i_mtime.tv_sec which might be set by ext4_get() 76e50e5129SAndreas Dilger */ 77e50e5129SAndreas Dilger #define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \ 78e50e5129SAndreas Dilger do { \ 79e50e5129SAndreas Dilger (inode)->i_mtime.tv_sec = inum; \ 80e50e5129SAndreas Dilger } while(0) 81e50e5129SAndreas Dilger 82e50e5129SAndreas Dilger #define EXT4_XATTR_INODE_GET_PARENT(inode) \ 83e50e5129SAndreas Dilger ((__u32)(inode)->i_mtime.tv_sec) 84e50e5129SAndreas Dilger 85e50e5129SAndreas Dilger /* 86e50e5129SAndreas Dilger * The minimum size of EA value when you start storing it in an external inode 87e50e5129SAndreas Dilger * size of block - size of header - size of 1 entry - 4 null bytes 88e50e5129SAndreas Dilger */ 89e50e5129SAndreas Dilger #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \ 90e50e5129SAndreas Dilger ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4) 91e50e5129SAndreas Dilger 92879b3825STao Ma #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data)) 93879b3825STao Ma #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr)) 94879b3825STao Ma #define BFIRST(bh) ENTRY(BHDR(bh)+1) 95879b3825STao Ma #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0) 96879b3825STao Ma 97bd9926e8STheodore Ts'o #define EXT4_ZERO_XATTR_VALUE ((void *)-1) 98879b3825STao Ma 99879b3825STao Ma struct ext4_xattr_info { 100879b3825STao Ma const char *name; 101879b3825STao Ma const void *value; 102879b3825STao Ma size_t value_len; 103e50e5129SAndreas Dilger int name_index; 104e50e5129SAndreas Dilger int in_inode; 105879b3825STao Ma }; 106879b3825STao Ma 107879b3825STao Ma struct ext4_xattr_search { 108879b3825STao Ma struct ext4_xattr_entry *first; 109879b3825STao Ma void *base; 110879b3825STao Ma void *end; 111879b3825STao Ma struct ext4_xattr_entry *here; 112879b3825STao Ma int not_found; 113879b3825STao Ma }; 114879b3825STao Ma 115879b3825STao Ma struct ext4_xattr_ibody_find { 116879b3825STao Ma struct ext4_xattr_search s; 117879b3825STao Ma struct ext4_iloc iloc; 118879b3825STao Ma }; 119879b3825STao Ma 120b6d9029dSTahsin Erdogan struct ext4_xattr_inode_array { 121b6d9029dSTahsin Erdogan unsigned int count; /* # of used items in the array */ 122b6d9029dSTahsin Erdogan struct inode *inodes[0]; 123b6d9029dSTahsin Erdogan }; 124b6d9029dSTahsin Erdogan 12511e27528SStephen Hemminger extern const struct xattr_handler ext4_xattr_user_handler; 12611e27528SStephen Hemminger extern const struct xattr_handler ext4_xattr_trusted_handler; 12711e27528SStephen Hemminger extern const struct xattr_handler ext4_xattr_security_handler; 128ac27a0ecSDave Kleikamp 129887e2c45SMichael Halcrow #define EXT4_XATTR_NAME_ENCRYPTION_CONTEXT "c" 130887e2c45SMichael Halcrow 131c755e251STheodore Ts'o /* 132c755e251STheodore Ts'o * The EXT4_STATE_NO_EXPAND is overloaded and used for two purposes. 133c755e251STheodore Ts'o * The first is to signal that there the inline xattrs and data are 134c755e251STheodore Ts'o * taking up so much space that we might as well not keep trying to 135c755e251STheodore Ts'o * expand it. The second is that xattr_sem is taken for writing, so 136c755e251STheodore Ts'o * we shouldn't try to recurse into the inode expansion. For this 137c755e251STheodore Ts'o * second case, we need to make sure that we take save and restore the 138c755e251STheodore Ts'o * NO_EXPAND state flag appropriately. 139c755e251STheodore Ts'o */ 140c755e251STheodore Ts'o static inline void ext4_write_lock_xattr(struct inode *inode, int *save) 141c755e251STheodore Ts'o { 142c755e251STheodore Ts'o down_write(&EXT4_I(inode)->xattr_sem); 143c755e251STheodore Ts'o *save = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND); 144c755e251STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND); 145c755e251STheodore Ts'o } 146c755e251STheodore Ts'o 147c755e251STheodore Ts'o static inline int ext4_write_trylock_xattr(struct inode *inode, int *save) 148c755e251STheodore Ts'o { 149c755e251STheodore Ts'o if (down_write_trylock(&EXT4_I(inode)->xattr_sem) == 0) 150c755e251STheodore Ts'o return 0; 151c755e251STheodore Ts'o *save = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND); 152c755e251STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND); 153c755e251STheodore Ts'o return 1; 154c755e251STheodore Ts'o } 155c755e251STheodore Ts'o 156c755e251STheodore Ts'o static inline void ext4_write_unlock_xattr(struct inode *inode, int *save) 157c755e251STheodore Ts'o { 158c755e251STheodore Ts'o if (*save == 0) 159c755e251STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND); 160c755e251STheodore Ts'o up_write(&EXT4_I(inode)->xattr_sem); 161c755e251STheodore Ts'o } 162c755e251STheodore Ts'o 163617ba13bSMingming Cao extern ssize_t ext4_listxattr(struct dentry *, char *, size_t); 164ac27a0ecSDave Kleikamp 165617ba13bSMingming Cao extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t); 166617ba13bSMingming Cao extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int); 167617ba13bSMingming Cao extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int); 168c1a5d5f6STahsin Erdogan extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len); 169ac27a0ecSDave Kleikamp 170e50e5129SAndreas Dilger extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino); 171e50e5129SAndreas Dilger extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, 17230a7eb97STahsin Erdogan struct ext4_xattr_inode_array **array, 17330a7eb97STahsin Erdogan int extra_credits); 1740421a189STahsin Erdogan extern void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *array); 175ac27a0ecSDave Kleikamp 1766dd4ee7cSKalpak Shah extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, 1776dd4ee7cSKalpak Shah struct ext4_inode *raw_inode, handle_t *handle); 1786dd4ee7cSKalpak Shah 17911e27528SStephen Hemminger extern const struct xattr_handler *ext4_xattr_handlers[]; 180ac27a0ecSDave Kleikamp 181879b3825STao Ma extern int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, 182879b3825STao Ma struct ext4_xattr_ibody_find *is); 183879b3825STao Ma extern int ext4_xattr_ibody_get(struct inode *inode, int name_index, 184879b3825STao Ma const char *name, 185879b3825STao Ma void *buffer, size_t buffer_size); 1860d812f77STao Ma extern int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, 187879b3825STao Ma struct ext4_xattr_info *i, 188879b3825STao Ma struct ext4_xattr_ibody_find *is); 189879b3825STao Ma 1907a2508e1SJan Kara extern struct mb_cache *ext4_xattr_create_cache(void); 1917a2508e1SJan Kara extern void ext4_xattr_destroy_cache(struct mb_cache *); 1929c191f70ST Makphaibulchoke 19303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_SECURITY 194617ba13bSMingming Cao extern int ext4_init_security(handle_t *handle, struct inode *inode, 1952a7dba39SEric Paris struct inode *dir, const struct qstr *qstr); 196ac27a0ecSDave Kleikamp #else 197617ba13bSMingming Cao static inline int ext4_init_security(handle_t *handle, struct inode *inode, 1982a7dba39SEric Paris struct inode *dir, const struct qstr *qstr) 199ac27a0ecSDave Kleikamp { 200ac27a0ecSDave Kleikamp return 0; 201ac27a0ecSDave Kleikamp } 202ac27a0ecSDave Kleikamp #endif 20333d201e0STahsin Erdogan 20433d201e0STahsin Erdogan #ifdef CONFIG_LOCKDEP 20533d201e0STahsin Erdogan extern void ext4_xattr_inode_set_class(struct inode *ea_inode); 20633d201e0STahsin Erdogan #else 20733d201e0STahsin Erdogan static inline void ext4_xattr_inode_set_class(struct inode *ea_inode) { } 20833d201e0STahsin Erdogan #endif 209