| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/xattr.c | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * | 
|  | 4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> | 
|  | 5 | * | 
|  | 6 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 7 | * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 8 | * Extended attributes for symlinks and special files added per | 
|  | 9 | *  suggestion of Luka Renko <luka.renko@hermes.si>. | 
|  | 10 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, | 
|  | 11 | *  Red Hat Inc. | 
|  | 12 | * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz | 
|  | 13 | *  and Andreas Gruenbacher <agruen@suse.de>. | 
|  | 14 | */ | 
|  | 15 |  | 
|  | 16 | /* | 
|  | 17 | * Extended attributes are stored directly in inodes (on file systems with | 
|  | 18 | * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl | 
|  | 19 | * field contains the block number if an inode uses an additional block. All | 
|  | 20 | * attributes must fit in the inode and one additional block. Blocks that | 
|  | 21 | * contain the identical set of attributes may be shared among several inodes. | 
|  | 22 | * Identical blocks are detected by keeping a cache of blocks that have | 
|  | 23 | * recently been accessed. | 
|  | 24 | * | 
|  | 25 | * The attributes in inodes and on blocks have a different header; the entries | 
|  | 26 | * are stored in the same format: | 
|  | 27 | * | 
|  | 28 | *   +------------------+ | 
|  | 29 | *   | header           | | 
|  | 30 | *   | entry 1          | | | 
|  | 31 | *   | entry 2          | | growing downwards | 
|  | 32 | *   | entry 3          | v | 
|  | 33 | *   | four null bytes  | | 
|  | 34 | *   | . . .            | | 
|  | 35 | *   | value 1          | ^ | 
|  | 36 | *   | value 3          | | growing upwards | 
|  | 37 | *   | value 2          | | | 
|  | 38 | *   +------------------+ | 
|  | 39 | * | 
|  | 40 | * The header is followed by multiple entry descriptors. In disk blocks, the | 
|  | 41 | * entry descriptors are kept sorted. In inodes, they are unsorted. The | 
|  | 42 | * attribute values are aligned to the end of the block in no specific order. | 
|  | 43 | * | 
|  | 44 | * Locking strategy | 
|  | 45 | * ---------------- | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 46 | * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | * EA blocks are only changed if they are exclusive to an inode, so | 
|  | 48 | * holding xattr_sem also means that nothing but the EA block's reference | 
|  | 49 | * count can change. Multiple writers to the same block are synchronized | 
|  | 50 | * by the buffer lock. | 
|  | 51 | */ | 
|  | 52 |  | 
|  | 53 | #include <linux/init.h> | 
|  | 54 | #include <linux/fs.h> | 
|  | 55 | #include <linux/slab.h> | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 56 | #include <linux/mbcache.h> | 
|  | 57 | #include <linux/quotaops.h> | 
|  | 58 | #include <linux/rwsem.h> | 
| Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 59 | #include "ext4_jbd2.h" | 
|  | 60 | #include "ext4.h" | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 61 | #include "xattr.h" | 
|  | 62 | #include "acl.h" | 
|  | 63 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 64 | #ifdef EXT4_XATTR_DEBUG | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 65 | # define ea_idebug(inode, f...) do { \ | 
|  | 66 | printk(KERN_DEBUG "inode %s:%lu: ", \ | 
|  | 67 | inode->i_sb->s_id, inode->i_ino); \ | 
|  | 68 | printk(f); \ | 
|  | 69 | printk("\n"); \ | 
|  | 70 | } while (0) | 
|  | 71 | # define ea_bdebug(bh, f...) do { \ | 
|  | 72 | char b[BDEVNAME_SIZE]; \ | 
|  | 73 | printk(KERN_DEBUG "block %s:%lu: ", \ | 
|  | 74 | bdevname(bh->b_bdev, b), \ | 
|  | 75 | (unsigned long) bh->b_blocknr); \ | 
|  | 76 | printk(f); \ | 
|  | 77 | printk("\n"); \ | 
|  | 78 | } while (0) | 
|  | 79 | #else | 
| Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 80 | # define ea_idebug(inode, fmt, ...)	no_printk(fmt, ##__VA_ARGS__) | 
|  | 81 | # define ea_bdebug(bh, fmt, ...)	no_printk(fmt, ##__VA_ARGS__) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 82 | #endif | 
|  | 83 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 84 | static void ext4_xattr_cache_insert(struct buffer_head *); | 
|  | 85 | static struct buffer_head *ext4_xattr_cache_find(struct inode *, | 
|  | 86 | struct ext4_xattr_header *, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 87 | struct mb_cache_entry **); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 88 | static void ext4_xattr_rehash(struct ext4_xattr_header *, | 
|  | 89 | struct ext4_xattr_entry *); | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 90 | static int ext4_xattr_list(struct dentry *dentry, char *buffer, | 
| Mingming Cao | d3a95d4 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 91 | size_t buffer_size); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 92 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 93 | static struct mb_cache *ext4_xattr_cache; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 94 |  | 
| Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 95 | static const struct xattr_handler *ext4_xattr_handler_map[] = { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 96 | [EXT4_XATTR_INDEX_USER]		     = &ext4_xattr_user_handler, | 
| Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 97 | #ifdef CONFIG_EXT4_FS_POSIX_ACL | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 98 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext4_xattr_acl_access_handler, | 
|  | 99 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 100 | #endif | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 101 | [EXT4_XATTR_INDEX_TRUSTED]	     = &ext4_xattr_trusted_handler, | 
| Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 102 | #ifdef CONFIG_EXT4_FS_SECURITY | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 103 | [EXT4_XATTR_INDEX_SECURITY]	     = &ext4_xattr_security_handler, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 104 | #endif | 
|  | 105 | }; | 
|  | 106 |  | 
| Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 107 | const struct xattr_handler *ext4_xattr_handlers[] = { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 108 | &ext4_xattr_user_handler, | 
|  | 109 | &ext4_xattr_trusted_handler, | 
| Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 110 | #ifdef CONFIG_EXT4_FS_POSIX_ACL | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 111 | &ext4_xattr_acl_access_handler, | 
|  | 112 | &ext4_xattr_acl_default_handler, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 113 | #endif | 
| Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 114 | #ifdef CONFIG_EXT4_FS_SECURITY | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 115 | &ext4_xattr_security_handler, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 116 | #endif | 
|  | 117 | NULL | 
|  | 118 | }; | 
|  | 119 |  | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 120 | static __le32 ext4_xattr_block_csum(struct inode *inode, | 
|  | 121 | sector_t block_nr, | 
|  | 122 | struct ext4_xattr_header *hdr) | 
|  | 123 | { | 
|  | 124 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 125 | __u32 csum, old; | 
|  | 126 |  | 
|  | 127 | old = hdr->h_checksum; | 
|  | 128 | hdr->h_checksum = 0; | 
| Tao Ma | 41eb70d | 2012-07-09 16:29:27 -0400 | [diff] [blame] | 129 | block_nr = cpu_to_le64(block_nr); | 
|  | 130 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&block_nr, | 
|  | 131 | sizeof(block_nr)); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 132 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, | 
|  | 133 | EXT4_BLOCK_SIZE(inode->i_sb)); | 
| Tao Ma | 41eb70d | 2012-07-09 16:29:27 -0400 | [diff] [blame] | 134 |  | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 135 | hdr->h_checksum = old; | 
|  | 136 | return cpu_to_le32(csum); | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | static int ext4_xattr_block_csum_verify(struct inode *inode, | 
|  | 140 | sector_t block_nr, | 
|  | 141 | struct ext4_xattr_header *hdr) | 
|  | 142 | { | 
|  | 143 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | 
|  | 144 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) && | 
|  | 145 | (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr))) | 
|  | 146 | return 0; | 
|  | 147 | return 1; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static void ext4_xattr_block_csum_set(struct inode *inode, | 
|  | 151 | sector_t block_nr, | 
|  | 152 | struct ext4_xattr_header *hdr) | 
|  | 153 | { | 
|  | 154 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | 
|  | 155 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | 
|  | 156 | return; | 
|  | 157 |  | 
|  | 158 | hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | static inline int ext4_handle_dirty_xattr_block(handle_t *handle, | 
|  | 162 | struct inode *inode, | 
|  | 163 | struct buffer_head *bh) | 
|  | 164 | { | 
|  | 165 | ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh)); | 
|  | 166 | return ext4_handle_dirty_metadata(handle, inode, bh); | 
|  | 167 | } | 
|  | 168 |  | 
| Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 169 | static inline const struct xattr_handler * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 170 | ext4_xattr_handler(int name_index) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 171 | { | 
| Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 172 | const struct xattr_handler *handler = NULL; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 173 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 174 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) | 
|  | 175 | handler = ext4_xattr_handler_map[name_index]; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 176 | return handler; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | /* | 
|  | 180 | * Inode operation listxattr() | 
|  | 181 | * | 
|  | 182 | * dentry->d_inode->i_mutex: don't care | 
|  | 183 | */ | 
|  | 184 | ssize_t | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 185 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t size) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 186 | { | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 187 | return ext4_xattr_list(dentry, buffer, size); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
|  | 190 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 191 | ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | { | 
|  | 193 | while (!IS_LAST_ENTRY(entry)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 194 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 195 | if ((void *)next >= end) | 
|  | 196 | return -EIO; | 
|  | 197 | entry = next; | 
|  | 198 | } | 
|  | 199 | return 0; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | static inline int | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 203 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 204 | { | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 205 | int error; | 
|  | 206 |  | 
|  | 207 | if (buffer_verified(bh)) | 
|  | 208 | return 0; | 
|  | 209 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 210 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 211 | BHDR(bh)->h_blocks != cpu_to_le32(1)) | 
|  | 212 | return -EIO; | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 213 | if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh))) | 
|  | 214 | return -EIO; | 
|  | 215 | error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size); | 
|  | 216 | if (!error) | 
|  | 217 | set_buffer_verified(bh); | 
|  | 218 | return error; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | static inline int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 222 | ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 223 | { | 
|  | 224 | size_t value_size = le32_to_cpu(entry->e_value_size); | 
|  | 225 |  | 
|  | 226 | if (entry->e_value_block != 0 || value_size > size || | 
|  | 227 | le16_to_cpu(entry->e_value_offs) + value_size > size) | 
|  | 228 | return -EIO; | 
|  | 229 | return 0; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 233 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 234 | const char *name, size_t size, int sorted) | 
|  | 235 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 236 | struct ext4_xattr_entry *entry; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 237 | size_t name_len; | 
|  | 238 | int cmp = 1; | 
|  | 239 |  | 
|  | 240 | if (name == NULL) | 
|  | 241 | return -EINVAL; | 
|  | 242 | name_len = strlen(name); | 
|  | 243 | entry = *pentry; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 244 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 245 | cmp = name_index - entry->e_name_index; | 
|  | 246 | if (!cmp) | 
|  | 247 | cmp = name_len - entry->e_name_len; | 
|  | 248 | if (!cmp) | 
|  | 249 | cmp = memcmp(name, entry->e_name, name_len); | 
|  | 250 | if (cmp <= 0 && (sorted || cmp == 0)) | 
|  | 251 | break; | 
|  | 252 | } | 
|  | 253 | *pentry = entry; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 254 | if (!cmp && ext4_xattr_check_entry(entry, size)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 255 | return -EIO; | 
|  | 256 | return cmp ? -ENODATA : 0; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 260 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 261 | void *buffer, size_t buffer_size) | 
|  | 262 | { | 
|  | 263 | struct buffer_head *bh = NULL; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 264 | struct ext4_xattr_entry *entry; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | size_t size; | 
|  | 266 | int error; | 
|  | 267 |  | 
|  | 268 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", | 
|  | 269 | name_index, name, buffer, (long)buffer_size); | 
|  | 270 |  | 
|  | 271 | error = -ENODATA; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 272 | if (!EXT4_I(inode)->i_file_acl) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 273 | goto cleanup; | 
| Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 274 | ea_idebug(inode, "reading block %llu", | 
|  | 275 | (unsigned long long)EXT4_I(inode)->i_file_acl); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 276 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 277 | if (!bh) | 
|  | 278 | goto cleanup; | 
|  | 279 | ea_bdebug(bh, "b_count=%d, refcount=%d", | 
|  | 280 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 281 | if (ext4_xattr_check_block(inode, bh)) { | 
| Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 282 | bad_block: | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 283 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|  | 284 | EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 285 | error = -EIO; | 
|  | 286 | goto cleanup; | 
|  | 287 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 288 | ext4_xattr_cache_insert(bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 289 | entry = BFIRST(bh); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 290 | error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 291 | if (error == -EIO) | 
|  | 292 | goto bad_block; | 
|  | 293 | if (error) | 
|  | 294 | goto cleanup; | 
|  | 295 | size = le32_to_cpu(entry->e_value_size); | 
|  | 296 | if (buffer) { | 
|  | 297 | error = -ERANGE; | 
|  | 298 | if (size > buffer_size) | 
|  | 299 | goto cleanup; | 
|  | 300 | memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs), | 
|  | 301 | size); | 
|  | 302 | } | 
|  | 303 | error = size; | 
|  | 304 |  | 
|  | 305 | cleanup: | 
|  | 306 | brelse(bh); | 
|  | 307 | return error; | 
|  | 308 | } | 
|  | 309 |  | 
| Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 310 | int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 311 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 312 | void *buffer, size_t buffer_size) | 
|  | 313 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 314 | struct ext4_xattr_ibody_header *header; | 
|  | 315 | struct ext4_xattr_entry *entry; | 
|  | 316 | struct ext4_inode *raw_inode; | 
|  | 317 | struct ext4_iloc iloc; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 318 | size_t size; | 
|  | 319 | void *end; | 
|  | 320 | int error; | 
|  | 321 |  | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 322 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 323 | return -ENODATA; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 324 | error = ext4_get_inode_loc(inode, &iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 325 | if (error) | 
|  | 326 | return error; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 327 | raw_inode = ext4_raw_inode(&iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 328 | header = IHDR(inode, raw_inode); | 
|  | 329 | entry = IFIRST(header); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 330 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; | 
|  | 331 | error = ext4_xattr_check_names(entry, end); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 332 | if (error) | 
|  | 333 | goto cleanup; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 334 | error = ext4_xattr_find_entry(&entry, name_index, name, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 335 | end - (void *)entry, 0); | 
|  | 336 | if (error) | 
|  | 337 | goto cleanup; | 
|  | 338 | size = le32_to_cpu(entry->e_value_size); | 
|  | 339 | if (buffer) { | 
|  | 340 | error = -ERANGE; | 
|  | 341 | if (size > buffer_size) | 
|  | 342 | goto cleanup; | 
|  | 343 | memcpy(buffer, (void *)IFIRST(header) + | 
|  | 344 | le16_to_cpu(entry->e_value_offs), size); | 
|  | 345 | } | 
|  | 346 | error = size; | 
|  | 347 |  | 
|  | 348 | cleanup: | 
|  | 349 | brelse(iloc.bh); | 
|  | 350 | return error; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 354 | * ext4_xattr_get() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 355 | * | 
|  | 356 | * Copy an extended attribute into the buffer | 
|  | 357 | * provided, or compute the buffer size required. | 
|  | 358 | * Buffer is NULL to compute the size of the buffer required. | 
|  | 359 | * | 
|  | 360 | * Returns a negative error number on failure, or the number of bytes | 
|  | 361 | * used / required on success. | 
|  | 362 | */ | 
|  | 363 | int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 364 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 365 | void *buffer, size_t buffer_size) | 
|  | 366 | { | 
|  | 367 | int error; | 
|  | 368 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 369 | down_read(&EXT4_I(inode)->xattr_sem); | 
|  | 370 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 371 | buffer_size); | 
|  | 372 | if (error == -ENODATA) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 373 | error = ext4_xattr_block_get(inode, name_index, name, buffer, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 374 | buffer_size); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 375 | up_read(&EXT4_I(inode)->xattr_sem); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 376 | return error; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | static int | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 380 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 381 | char *buffer, size_t buffer_size) | 
|  | 382 | { | 
|  | 383 | size_t rest = buffer_size; | 
|  | 384 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 385 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { | 
| Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 386 | const struct xattr_handler *handler = | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 387 | ext4_xattr_handler(entry->e_name_index); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 388 |  | 
|  | 389 | if (handler) { | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 390 | size_t size = handler->list(dentry, buffer, rest, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 391 | entry->e_name, | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 392 | entry->e_name_len, | 
|  | 393 | handler->flags); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 394 | if (buffer) { | 
|  | 395 | if (size > rest) | 
|  | 396 | return -ERANGE; | 
|  | 397 | buffer += size; | 
|  | 398 | } | 
|  | 399 | rest -= size; | 
|  | 400 | } | 
|  | 401 | } | 
|  | 402 | return buffer_size - rest; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | static int | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 406 | ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 407 | { | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 408 | struct inode *inode = dentry->d_inode; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 409 | struct buffer_head *bh = NULL; | 
|  | 410 | int error; | 
|  | 411 |  | 
|  | 412 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", | 
|  | 413 | buffer, (long)buffer_size); | 
|  | 414 |  | 
|  | 415 | error = 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 416 | if (!EXT4_I(inode)->i_file_acl) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 417 | goto cleanup; | 
| Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 418 | ea_idebug(inode, "reading block %llu", | 
|  | 419 | (unsigned long long)EXT4_I(inode)->i_file_acl); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 420 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 421 | error = -EIO; | 
|  | 422 | if (!bh) | 
|  | 423 | goto cleanup; | 
|  | 424 | ea_bdebug(bh, "b_count=%d, refcount=%d", | 
|  | 425 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 426 | if (ext4_xattr_check_block(inode, bh)) { | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 427 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|  | 428 | EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 429 | error = -EIO; | 
|  | 430 | goto cleanup; | 
|  | 431 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 432 | ext4_xattr_cache_insert(bh); | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 433 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 434 |  | 
|  | 435 | cleanup: | 
|  | 436 | brelse(bh); | 
|  | 437 |  | 
|  | 438 | return error; | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | static int | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 442 | ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 443 | { | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 444 | struct inode *inode = dentry->d_inode; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 445 | struct ext4_xattr_ibody_header *header; | 
|  | 446 | struct ext4_inode *raw_inode; | 
|  | 447 | struct ext4_iloc iloc; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 448 | void *end; | 
|  | 449 | int error; | 
|  | 450 |  | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 451 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 452 | return 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 453 | error = ext4_get_inode_loc(inode, &iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 454 | if (error) | 
|  | 455 | return error; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 456 | raw_inode = ext4_raw_inode(&iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 457 | header = IHDR(inode, raw_inode); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 458 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; | 
|  | 459 | error = ext4_xattr_check_names(IFIRST(header), end); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 460 | if (error) | 
|  | 461 | goto cleanup; | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 462 | error = ext4_xattr_list_entries(dentry, IFIRST(header), | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 463 | buffer, buffer_size); | 
|  | 464 |  | 
|  | 465 | cleanup: | 
|  | 466 | brelse(iloc.bh); | 
|  | 467 | return error; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 471 | * ext4_xattr_list() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 472 | * | 
|  | 473 | * Copy a list of attribute names into the buffer | 
|  | 474 | * provided, or compute the buffer size required. | 
|  | 475 | * Buffer is NULL to compute the size of the buffer required. | 
|  | 476 | * | 
|  | 477 | * Returns a negative error number on failure, or the number of bytes | 
|  | 478 | * used / required on success. | 
|  | 479 | */ | 
| Mingming Cao | d3a95d4 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 480 | static int | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 481 | ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 482 | { | 
| Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 483 | int ret, ret2; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 484 |  | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 485 | down_read(&EXT4_I(dentry->d_inode)->xattr_sem); | 
| Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 486 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); | 
|  | 487 | if (ret < 0) | 
|  | 488 | goto errout; | 
|  | 489 | if (buffer) { | 
|  | 490 | buffer += ret; | 
|  | 491 | buffer_size -= ret; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 492 | } | 
| Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 493 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); | 
|  | 494 | if (ret < 0) | 
|  | 495 | goto errout; | 
|  | 496 | ret += ret2; | 
|  | 497 | errout: | 
| Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 498 | up_read(&EXT4_I(dentry->d_inode)->xattr_sem); | 
| Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 499 | return ret; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 500 | } | 
|  | 501 |  | 
|  | 502 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 503 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 504 | * not set, set it. | 
|  | 505 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 506 | static void ext4_xattr_update_super_block(handle_t *handle, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 507 | struct super_block *sb) | 
|  | 508 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 509 | if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 510 | return; | 
|  | 511 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 512 | if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { | 
| Andreas Gruenbacher | ed2908f | 2006-12-06 20:36:16 -0800 | [diff] [blame] | 513 | EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR); | 
| Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 514 | ext4_handle_dirty_super(handle, sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 515 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 516 | } | 
|  | 517 |  | 
|  | 518 | /* | 
|  | 519 | * Release the xattr block BH: If the reference count is > 1, decrement | 
|  | 520 | * it; otherwise free the block. | 
|  | 521 | */ | 
|  | 522 | static void | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 523 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 524 | struct buffer_head *bh) | 
|  | 525 | { | 
|  | 526 | struct mb_cache_entry *ce = NULL; | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 527 | int error = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 528 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 529 | ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr); | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 530 | error = ext4_journal_get_write_access(handle, bh); | 
|  | 531 | if (error) | 
|  | 532 | goto out; | 
|  | 533 |  | 
|  | 534 | lock_buffer(bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 535 | if (BHDR(bh)->h_refcount == cpu_to_le32(1)) { | 
|  | 536 | ea_bdebug(bh, "refcount now=0; freeing"); | 
|  | 537 | if (ce) | 
|  | 538 | mb_cache_entry_free(ce); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 539 | get_bh(bh); | 
| Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 540 | ext4_free_blocks(handle, inode, bh, 0, 1, | 
|  | 541 | EXT4_FREE_BLOCKS_METADATA | | 
|  | 542 | EXT4_FREE_BLOCKS_FORGET); | 
| Eric Sandeen | c1bb05a | 2012-02-20 23:06:18 -0500 | [diff] [blame] | 543 | unlock_buffer(bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 544 | } else { | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 545 | le32_add_cpu(&BHDR(bh)->h_refcount, -1); | 
| Eric Sandeen | c1bb05a | 2012-02-20 23:06:18 -0500 | [diff] [blame] | 546 | if (ce) | 
|  | 547 | mb_cache_entry_release(ce); | 
|  | 548 | unlock_buffer(bh); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 549 | error = ext4_handle_dirty_xattr_block(handle, inode, bh); | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 550 | if (IS_SYNC(inode)) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 551 | ext4_handle_sync(handle); | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 552 | dquot_free_block(inode, 1); | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 553 | ea_bdebug(bh, "refcount now=%d; releasing", | 
|  | 554 | le32_to_cpu(BHDR(bh)->h_refcount)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 555 | } | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 556 | out: | 
|  | 557 | ext4_std_error(inode->i_sb, error); | 
|  | 558 | return; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 559 | } | 
|  | 560 |  | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 561 | /* | 
|  | 562 | * Find the available free space for EAs. This also returns the total number of | 
|  | 563 | * bytes used by EA entries. | 
|  | 564 | */ | 
|  | 565 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, | 
|  | 566 | size_t *min_offs, void *base, int *total) | 
|  | 567 | { | 
|  | 568 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
|  | 569 | *total += EXT4_XATTR_LEN(last->e_name_len); | 
|  | 570 | if (!last->e_value_block && last->e_value_size) { | 
|  | 571 | size_t offs = le16_to_cpu(last->e_value_offs); | 
|  | 572 | if (offs < *min_offs) | 
|  | 573 | *min_offs = offs; | 
|  | 574 | } | 
|  | 575 | } | 
|  | 576 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); | 
|  | 577 | } | 
|  | 578 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 579 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 580 | ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 581 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 582 | struct ext4_xattr_entry *last; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 583 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); | 
|  | 584 |  | 
|  | 585 | /* Compute min_offs and last. */ | 
|  | 586 | last = s->first; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 587 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 588 | if (!last->e_value_block && last->e_value_size) { | 
|  | 589 | size_t offs = le16_to_cpu(last->e_value_offs); | 
|  | 590 | if (offs < min_offs) | 
|  | 591 | min_offs = offs; | 
|  | 592 | } | 
|  | 593 | } | 
|  | 594 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); | 
|  | 595 | if (!s->not_found) { | 
|  | 596 | if (!s->here->e_value_block && s->here->e_value_size) { | 
|  | 597 | size_t size = le32_to_cpu(s->here->e_value_size); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 598 | free += EXT4_XATTR_SIZE(size); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 599 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 600 | free += EXT4_XATTR_LEN(name_len); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 601 | } | 
|  | 602 | if (i->value) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 603 | if (free < EXT4_XATTR_SIZE(i->value_len) || | 
|  | 604 | free < EXT4_XATTR_LEN(name_len) + | 
|  | 605 | EXT4_XATTR_SIZE(i->value_len)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 606 | return -ENOSPC; | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | if (i->value && s->not_found) { | 
|  | 610 | /* Insert the new name. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 611 | size_t size = EXT4_XATTR_LEN(name_len); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 612 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); | 
|  | 613 | memmove((void *)s->here + size, s->here, rest); | 
|  | 614 | memset(s->here, 0, size); | 
|  | 615 | s->here->e_name_index = i->name_index; | 
|  | 616 | s->here->e_name_len = name_len; | 
|  | 617 | memcpy(s->here->e_name, i->name, name_len); | 
|  | 618 | } else { | 
|  | 619 | if (!s->here->e_value_block && s->here->e_value_size) { | 
|  | 620 | void *first_val = s->base + min_offs; | 
|  | 621 | size_t offs = le16_to_cpu(s->here->e_value_offs); | 
|  | 622 | void *val = s->base + offs; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 623 | size_t size = EXT4_XATTR_SIZE( | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 624 | le32_to_cpu(s->here->e_value_size)); | 
|  | 625 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 626 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 627 | /* The old and the new value have the same | 
|  | 628 | size. Just replace. */ | 
|  | 629 | s->here->e_value_size = | 
|  | 630 | cpu_to_le32(i->value_len); | 
| Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 631 | if (i->value == EXT4_ZERO_XATTR_VALUE) { | 
|  | 632 | memset(val, 0, size); | 
|  | 633 | } else { | 
|  | 634 | /* Clear pad bytes first. */ | 
|  | 635 | memset(val + size - EXT4_XATTR_PAD, 0, | 
|  | 636 | EXT4_XATTR_PAD); | 
|  | 637 | memcpy(val, i->value, i->value_len); | 
|  | 638 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 639 | return 0; | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | /* Remove the old value. */ | 
|  | 643 | memmove(first_val + size, first_val, val - first_val); | 
|  | 644 | memset(first_val, 0, size); | 
|  | 645 | s->here->e_value_size = 0; | 
|  | 646 | s->here->e_value_offs = 0; | 
|  | 647 | min_offs += size; | 
|  | 648 |  | 
|  | 649 | /* Adjust all value offsets. */ | 
|  | 650 | last = s->first; | 
|  | 651 | while (!IS_LAST_ENTRY(last)) { | 
|  | 652 | size_t o = le16_to_cpu(last->e_value_offs); | 
|  | 653 | if (!last->e_value_block && | 
|  | 654 | last->e_value_size && o < offs) | 
|  | 655 | last->e_value_offs = | 
|  | 656 | cpu_to_le16(o + size); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 657 | last = EXT4_XATTR_NEXT(last); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 658 | } | 
|  | 659 | } | 
|  | 660 | if (!i->value) { | 
|  | 661 | /* Remove the old name. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 662 | size_t size = EXT4_XATTR_LEN(name_len); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 663 | last = ENTRY((void *)last - size); | 
|  | 664 | memmove(s->here, (void *)s->here + size, | 
|  | 665 | (void *)last - (void *)s->here + sizeof(__u32)); | 
|  | 666 | memset(last, 0, size); | 
|  | 667 | } | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | if (i->value) { | 
|  | 671 | /* Insert the new value. */ | 
|  | 672 | s->here->e_value_size = cpu_to_le32(i->value_len); | 
|  | 673 | if (i->value_len) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 674 | size_t size = EXT4_XATTR_SIZE(i->value_len); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 675 | void *val = s->base + min_offs - size; | 
|  | 676 | s->here->e_value_offs = cpu_to_le16(min_offs - size); | 
| Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 677 | if (i->value == EXT4_ZERO_XATTR_VALUE) { | 
|  | 678 | memset(val, 0, size); | 
|  | 679 | } else { | 
|  | 680 | /* Clear the pad bytes first. */ | 
|  | 681 | memset(val + size - EXT4_XATTR_PAD, 0, | 
|  | 682 | EXT4_XATTR_PAD); | 
|  | 683 | memcpy(val, i->value, i->value_len); | 
|  | 684 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 685 | } | 
|  | 686 | } | 
|  | 687 | return 0; | 
|  | 688 | } | 
|  | 689 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 690 | struct ext4_xattr_block_find { | 
|  | 691 | struct ext4_xattr_search s; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 692 | struct buffer_head *bh; | 
|  | 693 | }; | 
|  | 694 |  | 
|  | 695 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 696 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, | 
|  | 697 | struct ext4_xattr_block_find *bs) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 698 | { | 
|  | 699 | struct super_block *sb = inode->i_sb; | 
|  | 700 | int error; | 
|  | 701 |  | 
|  | 702 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", | 
|  | 703 | i->name_index, i->name, i->value, (long)i->value_len); | 
|  | 704 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 705 | if (EXT4_I(inode)->i_file_acl) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 706 | /* The inode already has an extended attribute block. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 707 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 708 | error = -EIO; | 
|  | 709 | if (!bs->bh) | 
|  | 710 | goto cleanup; | 
|  | 711 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", | 
|  | 712 | atomic_read(&(bs->bh->b_count)), | 
|  | 713 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 714 | if (ext4_xattr_check_block(inode, bs->bh)) { | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 715 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|  | 716 | EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 717 | error = -EIO; | 
|  | 718 | goto cleanup; | 
|  | 719 | } | 
|  | 720 | /* Find the named attribute. */ | 
|  | 721 | bs->s.base = BHDR(bs->bh); | 
|  | 722 | bs->s.first = BFIRST(bs->bh); | 
|  | 723 | bs->s.end = bs->bh->b_data + bs->bh->b_size; | 
|  | 724 | bs->s.here = bs->s.first; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 725 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 726 | i->name, bs->bh->b_size, 1); | 
|  | 727 | if (error && error != -ENODATA) | 
|  | 728 | goto cleanup; | 
|  | 729 | bs->s.not_found = error; | 
|  | 730 | } | 
|  | 731 | error = 0; | 
|  | 732 |  | 
|  | 733 | cleanup: | 
|  | 734 | return error; | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 738 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, | 
|  | 739 | struct ext4_xattr_info *i, | 
|  | 740 | struct ext4_xattr_block_find *bs) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 741 | { | 
|  | 742 | struct super_block *sb = inode->i_sb; | 
|  | 743 | struct buffer_head *new_bh = NULL; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 744 | struct ext4_xattr_search *s = &bs->s; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 745 | struct mb_cache_entry *ce = NULL; | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 746 | int error = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 747 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 748 | #define header(x) ((struct ext4_xattr_header *)(x)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 749 |  | 
|  | 750 | if (i->value && i->value_len > sb->s_blocksize) | 
|  | 751 | return -ENOSPC; | 
|  | 752 | if (s->base) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 753 | ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 754 | bs->bh->b_blocknr); | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 755 | error = ext4_journal_get_write_access(handle, bs->bh); | 
|  | 756 | if (error) | 
|  | 757 | goto cleanup; | 
|  | 758 | lock_buffer(bs->bh); | 
|  | 759 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 760 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { | 
|  | 761 | if (ce) { | 
|  | 762 | mb_cache_entry_free(ce); | 
|  | 763 | ce = NULL; | 
|  | 764 | } | 
|  | 765 | ea_bdebug(bs->bh, "modifying in-place"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 766 | error = ext4_xattr_set_entry(i, s); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 767 | if (!error) { | 
|  | 768 | if (!IS_LAST_ENTRY(s->first)) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 769 | ext4_xattr_rehash(header(s->base), | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 770 | s->here); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 771 | ext4_xattr_cache_insert(bs->bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 772 | } | 
|  | 773 | unlock_buffer(bs->bh); | 
|  | 774 | if (error == -EIO) | 
|  | 775 | goto bad_block; | 
|  | 776 | if (!error) | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 777 | error = ext4_handle_dirty_xattr_block(handle, | 
|  | 778 | inode, | 
|  | 779 | bs->bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 780 | if (error) | 
|  | 781 | goto cleanup; | 
|  | 782 | goto inserted; | 
|  | 783 | } else { | 
|  | 784 | int offset = (char *)s->here - bs->bh->b_data; | 
|  | 785 |  | 
| Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 786 | unlock_buffer(bs->bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 787 | if (ce) { | 
|  | 788 | mb_cache_entry_release(ce); | 
|  | 789 | ce = NULL; | 
|  | 790 | } | 
|  | 791 | ea_bdebug(bs->bh, "cloning"); | 
| Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 792 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 793 | error = -ENOMEM; | 
|  | 794 | if (s->base == NULL) | 
|  | 795 | goto cleanup; | 
|  | 796 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); | 
|  | 797 | s->first = ENTRY(header(s->base)+1); | 
|  | 798 | header(s->base)->h_refcount = cpu_to_le32(1); | 
|  | 799 | s->here = ENTRY(s->base + offset); | 
|  | 800 | s->end = s->base + bs->bh->b_size; | 
|  | 801 | } | 
|  | 802 | } else { | 
|  | 803 | /* Allocate a buffer where we construct the new block. */ | 
| Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 804 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 805 | /* assert(header == s->base) */ | 
|  | 806 | error = -ENOMEM; | 
|  | 807 | if (s->base == NULL) | 
|  | 808 | goto cleanup; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 809 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 810 | header(s->base)->h_blocks = cpu_to_le32(1); | 
|  | 811 | header(s->base)->h_refcount = cpu_to_le32(1); | 
|  | 812 | s->first = ENTRY(header(s->base)+1); | 
|  | 813 | s->here = ENTRY(header(s->base)+1); | 
|  | 814 | s->end = s->base + sb->s_blocksize; | 
|  | 815 | } | 
|  | 816 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 817 | error = ext4_xattr_set_entry(i, s); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 818 | if (error == -EIO) | 
|  | 819 | goto bad_block; | 
|  | 820 | if (error) | 
|  | 821 | goto cleanup; | 
|  | 822 | if (!IS_LAST_ENTRY(s->first)) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 823 | ext4_xattr_rehash(header(s->base), s->here); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 824 |  | 
|  | 825 | inserted: | 
|  | 826 | if (!IS_LAST_ENTRY(s->first)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 827 | new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 828 | if (new_bh) { | 
|  | 829 | /* We found an identical block in the cache. */ | 
|  | 830 | if (new_bh == bs->bh) | 
|  | 831 | ea_bdebug(new_bh, "keeping"); | 
|  | 832 | else { | 
|  | 833 | /* The old block is released after updating | 
|  | 834 | the inode. */ | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 835 | error = dquot_alloc_block(inode, 1); | 
|  | 836 | if (error) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 837 | goto cleanup; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 838 | error = ext4_journal_get_write_access(handle, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 839 | new_bh); | 
|  | 840 | if (error) | 
|  | 841 | goto cleanup_dquot; | 
|  | 842 | lock_buffer(new_bh); | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 843 | le32_add_cpu(&BHDR(new_bh)->h_refcount, 1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 844 | ea_bdebug(new_bh, "reusing; refcount now=%d", | 
|  | 845 | le32_to_cpu(BHDR(new_bh)->h_refcount)); | 
|  | 846 | unlock_buffer(new_bh); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 847 | error = ext4_handle_dirty_xattr_block(handle, | 
|  | 848 | inode, | 
|  | 849 | new_bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 850 | if (error) | 
|  | 851 | goto cleanup_dquot; | 
|  | 852 | } | 
|  | 853 | mb_cache_entry_release(ce); | 
|  | 854 | ce = NULL; | 
|  | 855 | } else if (bs->bh && s->base == bs->bh->b_data) { | 
|  | 856 | /* We were modifying this block in-place. */ | 
|  | 857 | ea_bdebug(bs->bh, "keeping this block"); | 
|  | 858 | new_bh = bs->bh; | 
|  | 859 | get_bh(new_bh); | 
|  | 860 | } else { | 
|  | 861 | /* We need to allocate a new block */ | 
| Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 862 | ext4_fsblk_t goal, block; | 
|  | 863 |  | 
|  | 864 | goal = ext4_group_first_block_no(sb, | 
| Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 865 | EXT4_I(inode)->i_block_group); | 
| Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 866 |  | 
|  | 867 | /* non-extent files can't have physical blocks past 2^32 */ | 
| Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 868 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) | 
| Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 869 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; | 
|  | 870 |  | 
| Eric Sandeen | 6d6a4351 | 2011-10-29 10:15:35 -0400 | [diff] [blame] | 871 | /* | 
|  | 872 | * take i_data_sem because we will test | 
|  | 873 | * i_delalloc_reserved_flag in ext4_mb_new_blocks | 
|  | 874 | */ | 
|  | 875 | down_read((&EXT4_I(inode)->i_data_sem)); | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 876 | block = ext4_new_meta_blocks(handle, inode, goal, 0, | 
|  | 877 | NULL, &error); | 
| Eric Sandeen | 6d6a4351 | 2011-10-29 10:15:35 -0400 | [diff] [blame] | 878 | up_read((&EXT4_I(inode)->i_data_sem)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 879 | if (error) | 
|  | 880 | goto cleanup; | 
| Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 881 |  | 
| Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 882 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) | 
| Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 883 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); | 
|  | 884 |  | 
| Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 885 | ea_idebug(inode, "creating block %llu", | 
|  | 886 | (unsigned long long)block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 887 |  | 
|  | 888 | new_bh = sb_getblk(sb, block); | 
|  | 889 | if (!new_bh) { | 
|  | 890 | getblk_failed: | 
| Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 891 | ext4_free_blocks(handle, inode, NULL, block, 1, | 
| Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 892 | EXT4_FREE_BLOCKS_METADATA); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 893 | error = -EIO; | 
|  | 894 | goto cleanup; | 
|  | 895 | } | 
|  | 896 | lock_buffer(new_bh); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 897 | error = ext4_journal_get_create_access(handle, new_bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 898 | if (error) { | 
|  | 899 | unlock_buffer(new_bh); | 
|  | 900 | goto getblk_failed; | 
|  | 901 | } | 
|  | 902 | memcpy(new_bh->b_data, s->base, new_bh->b_size); | 
|  | 903 | set_buffer_uptodate(new_bh); | 
|  | 904 | unlock_buffer(new_bh); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 905 | ext4_xattr_cache_insert(new_bh); | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 906 | error = ext4_handle_dirty_xattr_block(handle, | 
|  | 907 | inode, new_bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 908 | if (error) | 
|  | 909 | goto cleanup; | 
|  | 910 | } | 
|  | 911 | } | 
|  | 912 |  | 
|  | 913 | /* Update the inode. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 914 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 915 |  | 
|  | 916 | /* Drop the previous xattr block. */ | 
|  | 917 | if (bs->bh && bs->bh != new_bh) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 918 | ext4_xattr_release_block(handle, inode, bs->bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 919 | error = 0; | 
|  | 920 |  | 
|  | 921 | cleanup: | 
|  | 922 | if (ce) | 
|  | 923 | mb_cache_entry_release(ce); | 
|  | 924 | brelse(new_bh); | 
|  | 925 | if (!(bs->bh && s->base == bs->bh->b_data)) | 
|  | 926 | kfree(s->base); | 
|  | 927 |  | 
|  | 928 | return error; | 
|  | 929 |  | 
|  | 930 | cleanup_dquot: | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 931 | dquot_free_block(inode, 1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 932 | goto cleanup; | 
|  | 933 |  | 
|  | 934 | bad_block: | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 935 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|  | 936 | EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 937 | goto cleanup; | 
|  | 938 |  | 
|  | 939 | #undef header | 
|  | 940 | } | 
|  | 941 |  | 
| Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 942 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, | 
|  | 943 | struct ext4_xattr_ibody_find *is) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 944 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 945 | struct ext4_xattr_ibody_header *header; | 
|  | 946 | struct ext4_inode *raw_inode; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 947 | int error; | 
|  | 948 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 949 | if (EXT4_I(inode)->i_extra_isize == 0) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 950 | return 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 951 | raw_inode = ext4_raw_inode(&is->iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 952 | header = IHDR(inode, raw_inode); | 
|  | 953 | is->s.base = is->s.first = IFIRST(header); | 
|  | 954 | is->s.here = is->s.first; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 955 | is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 956 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 957 | error = ext4_xattr_check_names(IFIRST(header), is->s.end); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 958 | if (error) | 
|  | 959 | return error; | 
|  | 960 | /* Find the named attribute. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 961 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 962 | i->name, is->s.end - | 
|  | 963 | (void *)is->s.base, 0); | 
|  | 964 | if (error && error != -ENODATA) | 
|  | 965 | return error; | 
|  | 966 | is->s.not_found = error; | 
|  | 967 | } | 
|  | 968 | return 0; | 
|  | 969 | } | 
|  | 970 |  | 
| Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 971 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, | 
|  | 972 | struct ext4_xattr_info *i, | 
|  | 973 | struct ext4_xattr_ibody_find *is) | 
|  | 974 | { | 
|  | 975 | struct ext4_xattr_ibody_header *header; | 
|  | 976 | struct ext4_xattr_search *s = &is->s; | 
|  | 977 | int error; | 
|  | 978 |  | 
|  | 979 | if (EXT4_I(inode)->i_extra_isize == 0) | 
|  | 980 | return -ENOSPC; | 
|  | 981 | error = ext4_xattr_set_entry(i, s); | 
|  | 982 | if (error) { | 
|  | 983 | if (error == -ENOSPC && | 
|  | 984 | ext4_has_inline_data(inode)) { | 
|  | 985 | error = ext4_try_to_evict_inline_data(handle, inode, | 
|  | 986 | EXT4_XATTR_LEN(strlen(i->name) + | 
|  | 987 | EXT4_XATTR_SIZE(i->value_len))); | 
|  | 988 | if (error) | 
|  | 989 | return error; | 
|  | 990 | error = ext4_xattr_ibody_find(inode, i, is); | 
|  | 991 | if (error) | 
|  | 992 | return error; | 
|  | 993 | error = ext4_xattr_set_entry(i, s); | 
|  | 994 | } | 
|  | 995 | if (error) | 
|  | 996 | return error; | 
|  | 997 | } | 
|  | 998 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); | 
|  | 999 | if (!IS_LAST_ENTRY(s->first)) { | 
|  | 1000 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | 
|  | 1001 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); | 
|  | 1002 | } else { | 
|  | 1003 | header->h_magic = cpu_to_le32(0); | 
|  | 1004 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); | 
|  | 1005 | } | 
|  | 1006 | return 0; | 
|  | 1007 | } | 
|  | 1008 |  | 
|  | 1009 | static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, | 
|  | 1010 | struct ext4_xattr_info *i, | 
|  | 1011 | struct ext4_xattr_ibody_find *is) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1012 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1013 | struct ext4_xattr_ibody_header *header; | 
|  | 1014 | struct ext4_xattr_search *s = &is->s; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1015 | int error; | 
|  | 1016 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1017 | if (EXT4_I(inode)->i_extra_isize == 0) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1018 | return -ENOSPC; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1019 | error = ext4_xattr_set_entry(i, s); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1020 | if (error) | 
|  | 1021 | return error; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1022 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1023 | if (!IS_LAST_ENTRY(s->first)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1024 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1025 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1026 | } else { | 
|  | 1027 | header->h_magic = cpu_to_le32(0); | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1028 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1029 | } | 
|  | 1030 | return 0; | 
|  | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1034 | * ext4_xattr_set_handle() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1035 | * | 
| Wang Sheng-Hui | 6e9510b | 2011-01-10 12:10:30 -0500 | [diff] [blame] | 1036 | * Create, replace or remove an extended attribute for this inode.  Value | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1037 | * is NULL to remove an existing extended attribute, and non-NULL to | 
|  | 1038 | * either replace an existing extended attribute, or create a new extended | 
|  | 1039 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE | 
|  | 1040 | * specify that an extended attribute must exist and must not exist | 
|  | 1041 | * previous to the call, respectively. | 
|  | 1042 | * | 
|  | 1043 | * Returns 0, or a negative error number on failure. | 
|  | 1044 | */ | 
|  | 1045 | int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1046 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1047 | const char *name, const void *value, size_t value_len, | 
|  | 1048 | int flags) | 
|  | 1049 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1050 | struct ext4_xattr_info i = { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1051 | .name_index = name_index, | 
|  | 1052 | .name = name, | 
|  | 1053 | .value = value, | 
|  | 1054 | .value_len = value_len, | 
|  | 1055 |  | 
|  | 1056 | }; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1057 | struct ext4_xattr_ibody_find is = { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1058 | .s = { .not_found = -ENODATA, }, | 
|  | 1059 | }; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1060 | struct ext4_xattr_block_find bs = { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1061 | .s = { .not_found = -ENODATA, }, | 
|  | 1062 | }; | 
| Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 1063 | unsigned long no_expand; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1064 | int error; | 
|  | 1065 |  | 
|  | 1066 | if (!name) | 
|  | 1067 | return -EINVAL; | 
|  | 1068 | if (strlen(name) > 255) | 
|  | 1069 | return -ERANGE; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1070 | down_write(&EXT4_I(inode)->xattr_sem); | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1071 | no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND); | 
|  | 1072 | ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND); | 
| Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 1073 |  | 
| Eric Sandeen | 6654361 | 2011-10-26 03:32:07 -0400 | [diff] [blame] | 1074 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); | 
| Eric Sandeen | 86ebfd0 | 2009-11-15 15:30:52 -0500 | [diff] [blame] | 1075 | if (error) | 
|  | 1076 | goto cleanup; | 
|  | 1077 |  | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1078 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1079 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); | 
|  | 1080 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1081 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1082 | } | 
|  | 1083 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1084 | error = ext4_xattr_ibody_find(inode, &i, &is); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1085 | if (error) | 
|  | 1086 | goto cleanup; | 
|  | 1087 | if (is.s.not_found) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1088 | error = ext4_xattr_block_find(inode, &i, &bs); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1089 | if (error) | 
|  | 1090 | goto cleanup; | 
|  | 1091 | if (is.s.not_found && bs.s.not_found) { | 
|  | 1092 | error = -ENODATA; | 
|  | 1093 | if (flags & XATTR_REPLACE) | 
|  | 1094 | goto cleanup; | 
|  | 1095 | error = 0; | 
|  | 1096 | if (!value) | 
|  | 1097 | goto cleanup; | 
|  | 1098 | } else { | 
|  | 1099 | error = -EEXIST; | 
|  | 1100 | if (flags & XATTR_CREATE) | 
|  | 1101 | goto cleanup; | 
|  | 1102 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1103 | if (!value) { | 
|  | 1104 | if (!is.s.not_found) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1105 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1106 | else if (!bs.s.not_found) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1107 | error = ext4_xattr_block_set(handle, inode, &i, &bs); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1108 | } else { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1109 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1110 | if (!error && !bs.s.not_found) { | 
|  | 1111 | i.value = NULL; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1112 | error = ext4_xattr_block_set(handle, inode, &i, &bs); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1113 | } else if (error == -ENOSPC) { | 
| Tiger Yang | 7e01c8e | 2008-05-14 16:05:47 -0700 | [diff] [blame] | 1114 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { | 
|  | 1115 | error = ext4_xattr_block_find(inode, &i, &bs); | 
|  | 1116 | if (error) | 
|  | 1117 | goto cleanup; | 
|  | 1118 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1119 | error = ext4_xattr_block_set(handle, inode, &i, &bs); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1120 | if (error) | 
|  | 1121 | goto cleanup; | 
|  | 1122 | if (!is.s.not_found) { | 
|  | 1123 | i.value = NULL; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1124 | error = ext4_xattr_ibody_set(handle, inode, &i, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1125 | &is); | 
|  | 1126 | } | 
|  | 1127 | } | 
|  | 1128 | } | 
|  | 1129 | if (!error) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1130 | ext4_xattr_update_super_block(handle, inode->i_sb); | 
| Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 1131 | inode->i_ctime = ext4_current_time(inode); | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1132 | if (!value) | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1133 | ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1134 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1135 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1136 | * The bh is consumed by ext4_mark_iloc_dirty, even with | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1137 | * error != 0. | 
|  | 1138 | */ | 
|  | 1139 | is.iloc.bh = NULL; | 
|  | 1140 | if (IS_SYNC(inode)) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1141 | ext4_handle_sync(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | cleanup: | 
|  | 1145 | brelse(is.iloc.bh); | 
|  | 1146 | brelse(bs.bh); | 
| Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 1147 | if (no_expand == 0) | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1148 | ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1149 | up_write(&EXT4_I(inode)->xattr_sem); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1150 | return error; | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1154 | * ext4_xattr_set() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1155 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1156 | * Like ext4_xattr_set_handle, but start from an inode. This extended | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1157 | * attribute modification is a filesystem transaction by itself. | 
|  | 1158 | * | 
|  | 1159 | * Returns 0, or a negative error number on failure. | 
|  | 1160 | */ | 
|  | 1161 | int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1162 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1163 | const void *value, size_t value_len, int flags) | 
|  | 1164 | { | 
|  | 1165 | handle_t *handle; | 
|  | 1166 | int error, retries = 0; | 
| Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1167 | int credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1168 |  | 
|  | 1169 | retry: | 
| Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1170 | /* | 
|  | 1171 | * In case of inline data, we may push out the data to a block, | 
|  | 1172 | * So reserve the journal space first. | 
|  | 1173 | */ | 
|  | 1174 | if (ext4_has_inline_data(inode)) | 
|  | 1175 | credits += ext4_writepage_trans_blocks(inode) + 1; | 
|  | 1176 |  | 
|  | 1177 | handle = ext4_journal_start(inode, credits); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1178 | if (IS_ERR(handle)) { | 
|  | 1179 | error = PTR_ERR(handle); | 
|  | 1180 | } else { | 
|  | 1181 | int error2; | 
|  | 1182 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1183 | error = ext4_xattr_set_handle(handle, inode, name_index, name, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1184 | value, value_len, flags); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1185 | error2 = ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1186 | if (error == -ENOSPC && | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1187 | ext4_should_retry_alloc(inode->i_sb, &retries)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1188 | goto retry; | 
|  | 1189 | if (error == 0) | 
|  | 1190 | error = error2; | 
|  | 1191 | } | 
|  | 1192 |  | 
|  | 1193 | return error; | 
|  | 1194 | } | 
|  | 1195 |  | 
|  | 1196 | /* | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1197 | * Shift the EA entries in the inode to create space for the increased | 
|  | 1198 | * i_extra_isize. | 
|  | 1199 | */ | 
|  | 1200 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, | 
|  | 1201 | int value_offs_shift, void *to, | 
|  | 1202 | void *from, size_t n, int blocksize) | 
|  | 1203 | { | 
|  | 1204 | struct ext4_xattr_entry *last = entry; | 
|  | 1205 | int new_offs; | 
|  | 1206 |  | 
|  | 1207 | /* Adjust the value offsets of the entries */ | 
|  | 1208 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
|  | 1209 | if (!last->e_value_block && last->e_value_size) { | 
|  | 1210 | new_offs = le16_to_cpu(last->e_value_offs) + | 
|  | 1211 | value_offs_shift; | 
|  | 1212 | BUG_ON(new_offs + le32_to_cpu(last->e_value_size) | 
|  | 1213 | > blocksize); | 
|  | 1214 | last->e_value_offs = cpu_to_le16(new_offs); | 
|  | 1215 | } | 
|  | 1216 | } | 
|  | 1217 | /* Shift the entries by n bytes */ | 
|  | 1218 | memmove(to, from, n); | 
|  | 1219 | } | 
|  | 1220 |  | 
|  | 1221 | /* | 
|  | 1222 | * Expand an inode by new_extra_isize bytes when EAs are present. | 
|  | 1223 | * Returns 0 on success or negative error number on failure. | 
|  | 1224 | */ | 
|  | 1225 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, | 
|  | 1226 | struct ext4_inode *raw_inode, handle_t *handle) | 
|  | 1227 | { | 
|  | 1228 | struct ext4_xattr_ibody_header *header; | 
|  | 1229 | struct ext4_xattr_entry *entry, *last, *first; | 
|  | 1230 | struct buffer_head *bh = NULL; | 
|  | 1231 | struct ext4_xattr_ibody_find *is = NULL; | 
|  | 1232 | struct ext4_xattr_block_find *bs = NULL; | 
|  | 1233 | char *buffer = NULL, *b_entry_name = NULL; | 
|  | 1234 | size_t min_offs, free; | 
|  | 1235 | int total_ino, total_blk; | 
|  | 1236 | void *base, *start, *end; | 
|  | 1237 | int extra_isize = 0, error = 0, tried_min_extra_isize = 0; | 
| Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1238 | int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize); | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1239 |  | 
|  | 1240 | down_write(&EXT4_I(inode)->xattr_sem); | 
|  | 1241 | retry: | 
|  | 1242 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) { | 
|  | 1243 | up_write(&EXT4_I(inode)->xattr_sem); | 
|  | 1244 | return 0; | 
|  | 1245 | } | 
|  | 1246 |  | 
|  | 1247 | header = IHDR(inode, raw_inode); | 
|  | 1248 | entry = IFIRST(header); | 
|  | 1249 |  | 
|  | 1250 | /* | 
|  | 1251 | * Check if enough free space is available in the inode to shift the | 
|  | 1252 | * entries ahead by new_extra_isize. | 
|  | 1253 | */ | 
|  | 1254 |  | 
|  | 1255 | base = start = entry; | 
|  | 1256 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; | 
|  | 1257 | min_offs = end - base; | 
|  | 1258 | last = entry; | 
|  | 1259 | total_ino = sizeof(struct ext4_xattr_ibody_header); | 
|  | 1260 |  | 
|  | 1261 | free = ext4_xattr_free_space(last, &min_offs, base, &total_ino); | 
|  | 1262 | if (free >= new_extra_isize) { | 
|  | 1263 | entry = IFIRST(header); | 
|  | 1264 | ext4_xattr_shift_entries(entry,	EXT4_I(inode)->i_extra_isize | 
|  | 1265 | - new_extra_isize, (void *)raw_inode + | 
|  | 1266 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, | 
|  | 1267 | (void *)header, total_ino, | 
|  | 1268 | inode->i_sb->s_blocksize); | 
|  | 1269 | EXT4_I(inode)->i_extra_isize = new_extra_isize; | 
|  | 1270 | error = 0; | 
|  | 1271 | goto cleanup; | 
|  | 1272 | } | 
|  | 1273 |  | 
|  | 1274 | /* | 
|  | 1275 | * Enough free space isn't available in the inode, check if | 
|  | 1276 | * EA block can hold new_extra_isize bytes. | 
|  | 1277 | */ | 
|  | 1278 | if (EXT4_I(inode)->i_file_acl) { | 
|  | 1279 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); | 
|  | 1280 | error = -EIO; | 
|  | 1281 | if (!bh) | 
|  | 1282 | goto cleanup; | 
| Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1283 | if (ext4_xattr_check_block(inode, bh)) { | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1284 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|  | 1285 | EXT4_I(inode)->i_file_acl); | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1286 | error = -EIO; | 
|  | 1287 | goto cleanup; | 
|  | 1288 | } | 
|  | 1289 | base = BHDR(bh); | 
|  | 1290 | first = BFIRST(bh); | 
|  | 1291 | end = bh->b_data + bh->b_size; | 
|  | 1292 | min_offs = end - base; | 
|  | 1293 | free = ext4_xattr_free_space(first, &min_offs, base, | 
|  | 1294 | &total_blk); | 
|  | 1295 | if (free < new_extra_isize) { | 
|  | 1296 | if (!tried_min_extra_isize && s_min_extra_isize) { | 
|  | 1297 | tried_min_extra_isize++; | 
|  | 1298 | new_extra_isize = s_min_extra_isize; | 
|  | 1299 | brelse(bh); | 
|  | 1300 | goto retry; | 
|  | 1301 | } | 
|  | 1302 | error = -1; | 
|  | 1303 | goto cleanup; | 
|  | 1304 | } | 
|  | 1305 | } else { | 
|  | 1306 | free = inode->i_sb->s_blocksize; | 
|  | 1307 | } | 
|  | 1308 |  | 
|  | 1309 | while (new_extra_isize > 0) { | 
|  | 1310 | size_t offs, size, entry_size; | 
|  | 1311 | struct ext4_xattr_entry *small_entry = NULL; | 
|  | 1312 | struct ext4_xattr_info i = { | 
|  | 1313 | .value = NULL, | 
|  | 1314 | .value_len = 0, | 
|  | 1315 | }; | 
|  | 1316 | unsigned int total_size;  /* EA entry size + value size */ | 
|  | 1317 | unsigned int shift_bytes; /* No. of bytes to shift EAs by? */ | 
|  | 1318 | unsigned int min_total_size = ~0U; | 
|  | 1319 |  | 
|  | 1320 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); | 
|  | 1321 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); | 
|  | 1322 | if (!is || !bs) { | 
|  | 1323 | error = -ENOMEM; | 
|  | 1324 | goto cleanup; | 
|  | 1325 | } | 
|  | 1326 |  | 
|  | 1327 | is->s.not_found = -ENODATA; | 
|  | 1328 | bs->s.not_found = -ENODATA; | 
|  | 1329 | is->iloc.bh = NULL; | 
|  | 1330 | bs->bh = NULL; | 
|  | 1331 |  | 
|  | 1332 | last = IFIRST(header); | 
|  | 1333 | /* Find the entry best suited to be pushed into EA block */ | 
|  | 1334 | entry = NULL; | 
|  | 1335 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
|  | 1336 | total_size = | 
|  | 1337 | EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) + | 
|  | 1338 | EXT4_XATTR_LEN(last->e_name_len); | 
|  | 1339 | if (total_size <= free && total_size < min_total_size) { | 
|  | 1340 | if (total_size < new_extra_isize) { | 
|  | 1341 | small_entry = last; | 
|  | 1342 | } else { | 
|  | 1343 | entry = last; | 
|  | 1344 | min_total_size = total_size; | 
|  | 1345 | } | 
|  | 1346 | } | 
|  | 1347 | } | 
|  | 1348 |  | 
|  | 1349 | if (entry == NULL) { | 
|  | 1350 | if (small_entry) { | 
|  | 1351 | entry = small_entry; | 
|  | 1352 | } else { | 
|  | 1353 | if (!tried_min_extra_isize && | 
|  | 1354 | s_min_extra_isize) { | 
|  | 1355 | tried_min_extra_isize++; | 
|  | 1356 | new_extra_isize = s_min_extra_isize; | 
|  | 1357 | goto retry; | 
|  | 1358 | } | 
|  | 1359 | error = -1; | 
|  | 1360 | goto cleanup; | 
|  | 1361 | } | 
|  | 1362 | } | 
|  | 1363 | offs = le16_to_cpu(entry->e_value_offs); | 
|  | 1364 | size = le32_to_cpu(entry->e_value_size); | 
|  | 1365 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); | 
|  | 1366 | i.name_index = entry->e_name_index, | 
|  | 1367 | buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS); | 
|  | 1368 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); | 
|  | 1369 | if (!buffer || !b_entry_name) { | 
|  | 1370 | error = -ENOMEM; | 
|  | 1371 | goto cleanup; | 
|  | 1372 | } | 
|  | 1373 | /* Save the entry name and the entry value */ | 
|  | 1374 | memcpy(buffer, (void *)IFIRST(header) + offs, | 
|  | 1375 | EXT4_XATTR_SIZE(size)); | 
|  | 1376 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); | 
|  | 1377 | b_entry_name[entry->e_name_len] = '\0'; | 
|  | 1378 | i.name = b_entry_name; | 
|  | 1379 |  | 
|  | 1380 | error = ext4_get_inode_loc(inode, &is->iloc); | 
|  | 1381 | if (error) | 
|  | 1382 | goto cleanup; | 
|  | 1383 |  | 
|  | 1384 | error = ext4_xattr_ibody_find(inode, &i, is); | 
|  | 1385 | if (error) | 
|  | 1386 | goto cleanup; | 
|  | 1387 |  | 
|  | 1388 | /* Remove the chosen entry from the inode */ | 
|  | 1389 | error = ext4_xattr_ibody_set(handle, inode, &i, is); | 
| Roel Kluin | 9aaab05 | 2010-02-15 14:26:16 -0500 | [diff] [blame] | 1390 | if (error) | 
|  | 1391 | goto cleanup; | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1392 |  | 
|  | 1393 | entry = IFIRST(header); | 
|  | 1394 | if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize) | 
|  | 1395 | shift_bytes = new_extra_isize; | 
|  | 1396 | else | 
|  | 1397 | shift_bytes = entry_size + size; | 
|  | 1398 | /* Adjust the offsets and shift the remaining entries ahead */ | 
|  | 1399 | ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize - | 
|  | 1400 | shift_bytes, (void *)raw_inode + | 
|  | 1401 | EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes, | 
|  | 1402 | (void *)header, total_ino - entry_size, | 
|  | 1403 | inode->i_sb->s_blocksize); | 
|  | 1404 |  | 
|  | 1405 | extra_isize += shift_bytes; | 
|  | 1406 | new_extra_isize -= shift_bytes; | 
|  | 1407 | EXT4_I(inode)->i_extra_isize = extra_isize; | 
|  | 1408 |  | 
|  | 1409 | i.name = b_entry_name; | 
|  | 1410 | i.value = buffer; | 
| Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1411 | i.value_len = size; | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1412 | error = ext4_xattr_block_find(inode, &i, bs); | 
|  | 1413 | if (error) | 
|  | 1414 | goto cleanup; | 
|  | 1415 |  | 
|  | 1416 | /* Add entry which was removed from the inode into the block */ | 
|  | 1417 | error = ext4_xattr_block_set(handle, inode, &i, bs); | 
|  | 1418 | if (error) | 
|  | 1419 | goto cleanup; | 
|  | 1420 | kfree(b_entry_name); | 
|  | 1421 | kfree(buffer); | 
| Julia Lawall | d3533d7 | 2009-12-23 07:52:31 -0500 | [diff] [blame] | 1422 | b_entry_name = NULL; | 
|  | 1423 | buffer = NULL; | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1424 | brelse(is->iloc.bh); | 
|  | 1425 | kfree(is); | 
|  | 1426 | kfree(bs); | 
|  | 1427 | } | 
|  | 1428 | brelse(bh); | 
|  | 1429 | up_write(&EXT4_I(inode)->xattr_sem); | 
|  | 1430 | return 0; | 
|  | 1431 |  | 
|  | 1432 | cleanup: | 
|  | 1433 | kfree(b_entry_name); | 
|  | 1434 | kfree(buffer); | 
|  | 1435 | if (is) | 
|  | 1436 | brelse(is->iloc.bh); | 
|  | 1437 | kfree(is); | 
|  | 1438 | kfree(bs); | 
|  | 1439 | brelse(bh); | 
|  | 1440 | up_write(&EXT4_I(inode)->xattr_sem); | 
|  | 1441 | return error; | 
|  | 1442 | } | 
|  | 1443 |  | 
|  | 1444 |  | 
|  | 1445 |  | 
|  | 1446 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1447 | * ext4_xattr_delete_inode() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1448 | * | 
|  | 1449 | * Free extended attribute resources associated with this inode. This | 
|  | 1450 | * is called immediately before an inode is freed. We have exclusive | 
|  | 1451 | * access to the inode. | 
|  | 1452 | */ | 
|  | 1453 | void | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1454 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1455 | { | 
|  | 1456 | struct buffer_head *bh = NULL; | 
|  | 1457 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1458 | if (!EXT4_I(inode)->i_file_acl) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1459 | goto cleanup; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1460 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1461 | if (!bh) { | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1462 | EXT4_ERROR_INODE(inode, "block %llu read error", | 
|  | 1463 | EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1464 | goto cleanup; | 
|  | 1465 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1466 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1467 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1468 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|  | 1469 | EXT4_I(inode)->i_file_acl); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1470 | goto cleanup; | 
|  | 1471 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1472 | ext4_xattr_release_block(handle, inode, bh); | 
|  | 1473 | EXT4_I(inode)->i_file_acl = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1474 |  | 
|  | 1475 | cleanup: | 
|  | 1476 | brelse(bh); | 
|  | 1477 | } | 
|  | 1478 |  | 
|  | 1479 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1480 | * ext4_xattr_put_super() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1481 | * | 
|  | 1482 | * This is called when a file system is unmounted. | 
|  | 1483 | */ | 
|  | 1484 | void | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1485 | ext4_xattr_put_super(struct super_block *sb) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1486 | { | 
|  | 1487 | mb_cache_shrink(sb->s_bdev); | 
|  | 1488 | } | 
|  | 1489 |  | 
|  | 1490 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1491 | * ext4_xattr_cache_insert() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1492 | * | 
|  | 1493 | * Create a new entry in the extended attribute cache, and insert | 
|  | 1494 | * it unless such an entry is already in the cache. | 
|  | 1495 | * | 
|  | 1496 | * Returns 0, or a negative error number on failure. | 
|  | 1497 | */ | 
|  | 1498 | static void | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1499 | ext4_xattr_cache_insert(struct buffer_head *bh) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1500 | { | 
|  | 1501 | __u32 hash = le32_to_cpu(BHDR(bh)->h_hash); | 
|  | 1502 | struct mb_cache_entry *ce; | 
|  | 1503 | int error; | 
|  | 1504 |  | 
| Jan Kara | 335e92e | 2008-04-15 14:34:43 -0700 | [diff] [blame] | 1505 | ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1506 | if (!ce) { | 
|  | 1507 | ea_bdebug(bh, "out of memory"); | 
|  | 1508 | return; | 
|  | 1509 | } | 
| Andreas Gruenbacher | 2aec7c5 | 2010-07-19 18:19:41 +0200 | [diff] [blame] | 1510 | error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1511 | if (error) { | 
|  | 1512 | mb_cache_entry_free(ce); | 
|  | 1513 | if (error == -EBUSY) { | 
|  | 1514 | ea_bdebug(bh, "already in cache"); | 
|  | 1515 | error = 0; | 
|  | 1516 | } | 
|  | 1517 | } else { | 
|  | 1518 | ea_bdebug(bh, "inserting [%x]", (int)hash); | 
|  | 1519 | mb_cache_entry_release(ce); | 
|  | 1520 | } | 
|  | 1521 | } | 
|  | 1522 |  | 
|  | 1523 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1524 | * ext4_xattr_cmp() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1525 | * | 
|  | 1526 | * Compare two extended attribute blocks for equality. | 
|  | 1527 | * | 
|  | 1528 | * Returns 0 if the blocks are equal, 1 if they differ, and | 
|  | 1529 | * a negative error number on errors. | 
|  | 1530 | */ | 
|  | 1531 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1532 | ext4_xattr_cmp(struct ext4_xattr_header *header1, | 
|  | 1533 | struct ext4_xattr_header *header2) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1534 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1535 | struct ext4_xattr_entry *entry1, *entry2; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1536 |  | 
|  | 1537 | entry1 = ENTRY(header1+1); | 
|  | 1538 | entry2 = ENTRY(header2+1); | 
|  | 1539 | while (!IS_LAST_ENTRY(entry1)) { | 
|  | 1540 | if (IS_LAST_ENTRY(entry2)) | 
|  | 1541 | return 1; | 
|  | 1542 | if (entry1->e_hash != entry2->e_hash || | 
|  | 1543 | entry1->e_name_index != entry2->e_name_index || | 
|  | 1544 | entry1->e_name_len != entry2->e_name_len || | 
|  | 1545 | entry1->e_value_size != entry2->e_value_size || | 
|  | 1546 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) | 
|  | 1547 | return 1; | 
|  | 1548 | if (entry1->e_value_block != 0 || entry2->e_value_block != 0) | 
|  | 1549 | return -EIO; | 
|  | 1550 | if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), | 
|  | 1551 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), | 
|  | 1552 | le32_to_cpu(entry1->e_value_size))) | 
|  | 1553 | return 1; | 
|  | 1554 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1555 | entry1 = EXT4_XATTR_NEXT(entry1); | 
|  | 1556 | entry2 = EXT4_XATTR_NEXT(entry2); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1557 | } | 
|  | 1558 | if (!IS_LAST_ENTRY(entry2)) | 
|  | 1559 | return 1; | 
|  | 1560 | return 0; | 
|  | 1561 | } | 
|  | 1562 |  | 
|  | 1563 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1564 | * ext4_xattr_cache_find() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1565 | * | 
|  | 1566 | * Find an identical extended attribute block. | 
|  | 1567 | * | 
|  | 1568 | * Returns a pointer to the block found, or NULL if such a block was | 
|  | 1569 | * not found or an error occurred. | 
|  | 1570 | */ | 
|  | 1571 | static struct buffer_head * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1572 | ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1573 | struct mb_cache_entry **pce) | 
|  | 1574 | { | 
|  | 1575 | __u32 hash = le32_to_cpu(header->h_hash); | 
|  | 1576 | struct mb_cache_entry *ce; | 
|  | 1577 |  | 
|  | 1578 | if (!header->h_hash) | 
|  | 1579 | return NULL;  /* never share */ | 
|  | 1580 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); | 
|  | 1581 | again: | 
| Andreas Gruenbacher | 2aec7c5 | 2010-07-19 18:19:41 +0200 | [diff] [blame] | 1582 | ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev, | 
|  | 1583 | hash); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1584 | while (ce) { | 
|  | 1585 | struct buffer_head *bh; | 
|  | 1586 |  | 
|  | 1587 | if (IS_ERR(ce)) { | 
|  | 1588 | if (PTR_ERR(ce) == -EAGAIN) | 
|  | 1589 | goto again; | 
|  | 1590 | break; | 
|  | 1591 | } | 
|  | 1592 | bh = sb_bread(inode->i_sb, ce->e_block); | 
|  | 1593 | if (!bh) { | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1594 | EXT4_ERROR_INODE(inode, "block %lu read error", | 
|  | 1595 | (unsigned long) ce->e_block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1596 | } else if (le32_to_cpu(BHDR(bh)->h_refcount) >= | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1597 | EXT4_XATTR_REFCOUNT_MAX) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1598 | ea_idebug(inode, "block %lu refcount %d>=%d", | 
|  | 1599 | (unsigned long) ce->e_block, | 
|  | 1600 | le32_to_cpu(BHDR(bh)->h_refcount), | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1601 | EXT4_XATTR_REFCOUNT_MAX); | 
|  | 1602 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1603 | *pce = ce; | 
|  | 1604 | return bh; | 
|  | 1605 | } | 
|  | 1606 | brelse(bh); | 
| Andreas Gruenbacher | 2aec7c5 | 2010-07-19 18:19:41 +0200 | [diff] [blame] | 1607 | ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1608 | } | 
|  | 1609 | return NULL; | 
|  | 1610 | } | 
|  | 1611 |  | 
|  | 1612 | #define NAME_HASH_SHIFT 5 | 
|  | 1613 | #define VALUE_HASH_SHIFT 16 | 
|  | 1614 |  | 
|  | 1615 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1616 | * ext4_xattr_hash_entry() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1617 | * | 
|  | 1618 | * Compute the hash of an extended attribute. | 
|  | 1619 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1620 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, | 
|  | 1621 | struct ext4_xattr_entry *entry) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1622 | { | 
|  | 1623 | __u32 hash = 0; | 
|  | 1624 | char *name = entry->e_name; | 
|  | 1625 | int n; | 
|  | 1626 |  | 
| Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1627 | for (n = 0; n < entry->e_name_len; n++) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1628 | hash = (hash << NAME_HASH_SHIFT) ^ | 
|  | 1629 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ | 
|  | 1630 | *name++; | 
|  | 1631 | } | 
|  | 1632 |  | 
|  | 1633 | if (entry->e_value_block == 0 && entry->e_value_size != 0) { | 
|  | 1634 | __le32 *value = (__le32 *)((char *)header + | 
|  | 1635 | le16_to_cpu(entry->e_value_offs)); | 
|  | 1636 | for (n = (le32_to_cpu(entry->e_value_size) + | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1637 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1638 | hash = (hash << VALUE_HASH_SHIFT) ^ | 
|  | 1639 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ | 
|  | 1640 | le32_to_cpu(*value++); | 
|  | 1641 | } | 
|  | 1642 | } | 
|  | 1643 | entry->e_hash = cpu_to_le32(hash); | 
|  | 1644 | } | 
|  | 1645 |  | 
|  | 1646 | #undef NAME_HASH_SHIFT | 
|  | 1647 | #undef VALUE_HASH_SHIFT | 
|  | 1648 |  | 
|  | 1649 | #define BLOCK_HASH_SHIFT 16 | 
|  | 1650 |  | 
|  | 1651 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1652 | * ext4_xattr_rehash() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1653 | * | 
|  | 1654 | * Re-compute the extended attribute hash value after an entry has changed. | 
|  | 1655 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1656 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, | 
|  | 1657 | struct ext4_xattr_entry *entry) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1658 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1659 | struct ext4_xattr_entry *here; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1660 | __u32 hash = 0; | 
|  | 1661 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1662 | ext4_xattr_hash_entry(header, entry); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1663 | here = ENTRY(header+1); | 
|  | 1664 | while (!IS_LAST_ENTRY(here)) { | 
|  | 1665 | if (!here->e_hash) { | 
|  | 1666 | /* Block is not shared if an entry's hash value == 0 */ | 
|  | 1667 | hash = 0; | 
|  | 1668 | break; | 
|  | 1669 | } | 
|  | 1670 | hash = (hash << BLOCK_HASH_SHIFT) ^ | 
|  | 1671 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ | 
|  | 1672 | le32_to_cpu(here->e_hash); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1673 | here = EXT4_XATTR_NEXT(here); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1674 | } | 
|  | 1675 | header->h_hash = cpu_to_le32(hash); | 
|  | 1676 | } | 
|  | 1677 |  | 
|  | 1678 | #undef BLOCK_HASH_SHIFT | 
|  | 1679 |  | 
|  | 1680 | int __init | 
| Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1681 | ext4_init_xattr(void) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1682 | { | 
| Andreas Gruenbacher | 2aec7c5 | 2010-07-19 18:19:41 +0200 | [diff] [blame] | 1683 | ext4_xattr_cache = mb_cache_create("ext4_xattr", 6); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1684 | if (!ext4_xattr_cache) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1685 | return -ENOMEM; | 
|  | 1686 | return 0; | 
|  | 1687 | } | 
|  | 1688 |  | 
|  | 1689 | void | 
| Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1690 | ext4_exit_xattr(void) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1691 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1692 | if (ext4_xattr_cache) | 
|  | 1693 | mb_cache_destroy(ext4_xattr_cache); | 
|  | 1694 | ext4_xattr_cache = NULL; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1695 | } |