| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | * linux/fs/ext4/xattr.c | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> | 
|---|
| 6 | * | 
|---|
| 7 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. | 
|---|
| 8 | * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>. | 
|---|
| 9 | * Extended attributes for symlinks and special files added per | 
|---|
| 10 | *  suggestion of Luka Renko <luka.renko@hermes.si>. | 
|---|
| 11 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, | 
|---|
| 12 | *  Red Hat Inc. | 
|---|
| 13 | * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz | 
|---|
| 14 | *  and Andreas Gruenbacher <agruen@suse.de>. | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 | /* | 
|---|
| 18 | * Extended attributes are stored directly in inodes (on file systems with | 
|---|
| 19 | * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl | 
|---|
| 20 | * field contains the block number if an inode uses an additional block. All | 
|---|
| 21 | * attributes must fit in the inode and one additional block. Blocks that | 
|---|
| 22 | * contain the identical set of attributes may be shared among several inodes. | 
|---|
| 23 | * Identical blocks are detected by keeping a cache of blocks that have | 
|---|
| 24 | * recently been accessed. | 
|---|
| 25 | * | 
|---|
| 26 | * The attributes in inodes and on blocks have a different header; the entries | 
|---|
| 27 | * are stored in the same format: | 
|---|
| 28 | * | 
|---|
| 29 | *   +------------------+ | 
|---|
| 30 | *   | header           | | 
|---|
| 31 | *   | entry 1          | | | 
|---|
| 32 | *   | entry 2          | | growing downwards | 
|---|
| 33 | *   | entry 3          | v | 
|---|
| 34 | *   | four null bytes  | | 
|---|
| 35 | *   | . . .            | | 
|---|
| 36 | *   | value 1          | ^ | 
|---|
| 37 | *   | value 3          | | growing upwards | 
|---|
| 38 | *   | value 2          | | | 
|---|
| 39 | *   +------------------+ | 
|---|
| 40 | * | 
|---|
| 41 | * The header is followed by multiple entry descriptors. In disk blocks, the | 
|---|
| 42 | * entry descriptors are kept sorted. In inodes, they are unsorted. The | 
|---|
| 43 | * attribute values are aligned to the end of the block in no specific order. | 
|---|
| 44 | * | 
|---|
| 45 | * Locking strategy | 
|---|
| 46 | * ---------------- | 
|---|
| 47 | * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem. | 
|---|
| 48 | * EA blocks are only changed if they are exclusive to an inode, so | 
|---|
| 49 | * holding xattr_sem also means that nothing but the EA block's reference | 
|---|
| 50 | * count can change. Multiple writers to the same block are synchronized | 
|---|
| 51 | * by the buffer lock. | 
|---|
| 52 | */ | 
|---|
| 53 |  | 
|---|
| 54 | #include <linux/init.h> | 
|---|
| 55 | #include <linux/fs.h> | 
|---|
| 56 | #include <linux/slab.h> | 
|---|
| 57 | #include <linux/mbcache.h> | 
|---|
| 58 | #include <linux/quotaops.h> | 
|---|
| 59 | #include <linux/iversion.h> | 
|---|
| 60 | #include "ext4_jbd2.h" | 
|---|
| 61 | #include "ext4.h" | 
|---|
| 62 | #include "xattr.h" | 
|---|
| 63 | #include "acl.h" | 
|---|
| 64 |  | 
|---|
| 65 | #ifdef EXT4_XATTR_DEBUG | 
|---|
| 66 | # define ea_idebug(inode, fmt, ...)					\ | 
|---|
| 67 | printk(KERN_DEBUG "inode %s:%lu: " fmt "\n",			\ | 
|---|
| 68 | inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__) | 
|---|
| 69 | # define ea_bdebug(bh, fmt, ...)					\ | 
|---|
| 70 | printk(KERN_DEBUG "block %pg:%lu: " fmt "\n",			\ | 
|---|
| 71 | bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__) | 
|---|
| 72 | #else | 
|---|
| 73 | # define ea_idebug(inode, fmt, ...)	no_printk(fmt, ##__VA_ARGS__) | 
|---|
| 74 | # define ea_bdebug(bh, fmt, ...)	no_printk(fmt, ##__VA_ARGS__) | 
|---|
| 75 | #endif | 
|---|
| 76 |  | 
|---|
| 77 | static void ext4_xattr_block_cache_insert(struct mb_cache *, | 
|---|
| 78 | struct buffer_head *); | 
|---|
| 79 | static struct buffer_head * | 
|---|
| 80 | ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *, | 
|---|
| 81 | struct mb_cache_entry **); | 
|---|
| 82 | static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, | 
|---|
| 83 | size_t value_count); | 
|---|
| 84 | static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, | 
|---|
| 85 | size_t value_count); | 
|---|
| 86 | static void ext4_xattr_rehash(struct ext4_xattr_header *); | 
|---|
| 87 |  | 
|---|
| 88 | static const struct xattr_handler * const ext4_xattr_handler_map[] = { | 
|---|
| 89 | [EXT4_XATTR_INDEX_USER]		     = &ext4_xattr_user_handler, | 
|---|
| 90 | #ifdef CONFIG_EXT4_FS_POSIX_ACL | 
|---|
| 91 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &nop_posix_acl_access, | 
|---|
| 92 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default, | 
|---|
| 93 | #endif | 
|---|
| 94 | [EXT4_XATTR_INDEX_TRUSTED]	     = &ext4_xattr_trusted_handler, | 
|---|
| 95 | #ifdef CONFIG_EXT4_FS_SECURITY | 
|---|
| 96 | [EXT4_XATTR_INDEX_SECURITY]	     = &ext4_xattr_security_handler, | 
|---|
| 97 | #endif | 
|---|
| 98 | [EXT4_XATTR_INDEX_HURD]		     = &ext4_xattr_hurd_handler, | 
|---|
| 99 | }; | 
|---|
| 100 |  | 
|---|
| 101 | const struct xattr_handler * const ext4_xattr_handlers[] = { | 
|---|
| 102 | &ext4_xattr_user_handler, | 
|---|
| 103 | &ext4_xattr_trusted_handler, | 
|---|
| 104 | #ifdef CONFIG_EXT4_FS_SECURITY | 
|---|
| 105 | &ext4_xattr_security_handler, | 
|---|
| 106 | #endif | 
|---|
| 107 | &ext4_xattr_hurd_handler, | 
|---|
| 108 | NULL | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 | #define EA_BLOCK_CACHE(inode)	(((struct ext4_sb_info *) \ | 
|---|
| 112 | inode->i_sb->s_fs_info)->s_ea_block_cache) | 
|---|
| 113 |  | 
|---|
| 114 | #define EA_INODE_CACHE(inode)	(((struct ext4_sb_info *) \ | 
|---|
| 115 | inode->i_sb->s_fs_info)->s_ea_inode_cache) | 
|---|
| 116 |  | 
|---|
| 117 | static int | 
|---|
| 118 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, | 
|---|
| 119 | struct inode *inode); | 
|---|
| 120 |  | 
|---|
| 121 | #ifdef CONFIG_LOCKDEP | 
|---|
| 122 | void ext4_xattr_inode_set_class(struct inode *ea_inode) | 
|---|
| 123 | { | 
|---|
| 124 | struct ext4_inode_info *ei = EXT4_I(ea_inode); | 
|---|
| 125 |  | 
|---|
| 126 | lockdep_set_subclass(&ea_inode->i_rwsem, 1); | 
|---|
| 127 | (void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */ | 
|---|
| 128 | lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA); | 
|---|
| 129 | } | 
|---|
| 130 | #endif | 
|---|
| 131 |  | 
|---|
| 132 | static __le32 ext4_xattr_block_csum(struct inode *inode, | 
|---|
| 133 | sector_t block_nr, | 
|---|
| 134 | struct ext4_xattr_header *hdr) | 
|---|
| 135 | { | 
|---|
| 136 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 137 | __u32 csum; | 
|---|
| 138 | __le64 dsk_block_nr = cpu_to_le64(block_nr); | 
|---|
| 139 | __u32 dummy_csum = 0; | 
|---|
| 140 | int offset = offsetof(struct ext4_xattr_header, h_checksum); | 
|---|
| 141 |  | 
|---|
| 142 | csum = ext4_chksum(crc: sbi->s_csum_seed, address: (__u8 *)&dsk_block_nr, | 
|---|
| 143 | length: sizeof(dsk_block_nr)); | 
|---|
| 144 | csum = ext4_chksum(crc: csum, address: (__u8 *)hdr, length: offset); | 
|---|
| 145 | csum = ext4_chksum(crc: csum, address: (__u8 *)&dummy_csum, length: sizeof(dummy_csum)); | 
|---|
| 146 | offset += sizeof(dummy_csum); | 
|---|
| 147 | csum = ext4_chksum(crc: csum, address: (__u8 *)hdr + offset, | 
|---|
| 148 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); | 
|---|
| 149 |  | 
|---|
| 150 | return cpu_to_le32(csum); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | static int ext4_xattr_block_csum_verify(struct inode *inode, | 
|---|
| 154 | struct buffer_head *bh) | 
|---|
| 155 | { | 
|---|
| 156 | struct ext4_xattr_header *hdr = BHDR(bh); | 
|---|
| 157 | int ret = 1; | 
|---|
| 158 |  | 
|---|
| 159 | if (ext4_has_feature_metadata_csum(sb: inode->i_sb)) { | 
|---|
| 160 | lock_buffer(bh); | 
|---|
| 161 | ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, | 
|---|
| 162 | block_nr: bh->b_blocknr, hdr)); | 
|---|
| 163 | unlock_buffer(bh); | 
|---|
| 164 | } | 
|---|
| 165 | return ret; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | static void ext4_xattr_block_csum_set(struct inode *inode, | 
|---|
| 169 | struct buffer_head *bh) | 
|---|
| 170 | { | 
|---|
| 171 | if (ext4_has_feature_metadata_csum(sb: inode->i_sb)) | 
|---|
| 172 | BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, | 
|---|
| 173 | block_nr: bh->b_blocknr, BHDR(bh)); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | static inline const char *ext4_xattr_prefix(int name_index, | 
|---|
| 177 | struct dentry *dentry) | 
|---|
| 178 | { | 
|---|
| 179 | const struct xattr_handler *handler = NULL; | 
|---|
| 180 |  | 
|---|
| 181 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) | 
|---|
| 182 | handler = ext4_xattr_handler_map[name_index]; | 
|---|
| 183 |  | 
|---|
| 184 | if (!xattr_handler_can_list(handler, dentry)) | 
|---|
| 185 | return NULL; | 
|---|
| 186 |  | 
|---|
| 187 | return xattr_prefix(handler); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | static int | 
|---|
| 191 | check_xattrs(struct inode *inode, struct buffer_head *bh, | 
|---|
| 192 | struct ext4_xattr_entry *entry, void *end, void *value_start, | 
|---|
| 193 | const char *function, unsigned int line) | 
|---|
| 194 | { | 
|---|
| 195 | struct ext4_xattr_entry *e = entry; | 
|---|
| 196 | int err = -EFSCORRUPTED; | 
|---|
| 197 | char *err_str; | 
|---|
| 198 |  | 
|---|
| 199 | if (bh) { | 
|---|
| 200 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || | 
|---|
| 201 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { | 
|---|
| 202 | err_str = "invalid header"; | 
|---|
| 203 | goto errout; | 
|---|
| 204 | } | 
|---|
| 205 | if (buffer_verified(bh)) | 
|---|
| 206 | return 0; | 
|---|
| 207 | if (!ext4_xattr_block_csum_verify(inode, bh)) { | 
|---|
| 208 | err = -EFSBADCRC; | 
|---|
| 209 | err_str = "invalid checksum"; | 
|---|
| 210 | goto errout; | 
|---|
| 211 | } | 
|---|
| 212 | } else { | 
|---|
| 213 | struct ext4_xattr_ibody_header * = value_start; | 
|---|
| 214 |  | 
|---|
| 215 | header -= 1; | 
|---|
| 216 | if (end - (void *)header < sizeof(*header) + sizeof(u32)) { | 
|---|
| 217 | err_str = "in-inode xattr block too small"; | 
|---|
| 218 | goto errout; | 
|---|
| 219 | } | 
|---|
| 220 | if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { | 
|---|
| 221 | err_str = "bad magic number in in-inode xattr"; | 
|---|
| 222 | goto errout; | 
|---|
| 223 | } | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | /* Find the end of the names list */ | 
|---|
| 227 | while (!IS_LAST_ENTRY(e)) { | 
|---|
| 228 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); | 
|---|
| 229 | if ((void *)next >= end) { | 
|---|
| 230 | err_str = "e_name out of bounds"; | 
|---|
| 231 | goto errout; | 
|---|
| 232 | } | 
|---|
| 233 | if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) { | 
|---|
| 234 | err_str = "bad e_name length"; | 
|---|
| 235 | goto errout; | 
|---|
| 236 | } | 
|---|
| 237 | e = next; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | /* Check the values */ | 
|---|
| 241 | while (!IS_LAST_ENTRY(entry)) { | 
|---|
| 242 | u32 size = le32_to_cpu(entry->e_value_size); | 
|---|
| 243 | unsigned long ea_ino = le32_to_cpu(entry->e_value_inum); | 
|---|
| 244 |  | 
|---|
| 245 | if (!ext4_has_feature_ea_inode(sb: inode->i_sb) && ea_ino) { | 
|---|
| 246 | err_str = "ea_inode specified without ea_inode feature enabled"; | 
|---|
| 247 | goto errout; | 
|---|
| 248 | } | 
|---|
| 249 | if (ea_ino && ((ea_ino == EXT4_ROOT_INO) || | 
|---|
| 250 | !ext4_valid_inum(sb: inode->i_sb, ino: ea_ino))) { | 
|---|
| 251 | err_str = "invalid ea_ino"; | 
|---|
| 252 | goto errout; | 
|---|
| 253 | } | 
|---|
| 254 | if (ea_ino && !size) { | 
|---|
| 255 | err_str = "invalid size in ea xattr"; | 
|---|
| 256 | goto errout; | 
|---|
| 257 | } | 
|---|
| 258 | if (size > EXT4_XATTR_SIZE_MAX) { | 
|---|
| 259 | err_str = "e_value size too large"; | 
|---|
| 260 | goto errout; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | if (size != 0 && entry->e_value_inum == 0) { | 
|---|
| 264 | u16 offs = le16_to_cpu(entry->e_value_offs); | 
|---|
| 265 | void *value; | 
|---|
| 266 |  | 
|---|
| 267 | /* | 
|---|
| 268 | * The value cannot overlap the names, and the value | 
|---|
| 269 | * with padding cannot extend beyond 'end'.  Check both | 
|---|
| 270 | * the padded and unpadded sizes, since the size may | 
|---|
| 271 | * overflow to 0 when adding padding. | 
|---|
| 272 | */ | 
|---|
| 273 | if (offs > end - value_start) { | 
|---|
| 274 | err_str = "e_value out of bounds"; | 
|---|
| 275 | goto errout; | 
|---|
| 276 | } | 
|---|
| 277 | value = value_start + offs; | 
|---|
| 278 | if (value < (void *)e + sizeof(u32) || | 
|---|
| 279 | size > end - value || | 
|---|
| 280 | EXT4_XATTR_SIZE(size) > end - value) { | 
|---|
| 281 | err_str = "overlapping e_value "; | 
|---|
| 282 | goto errout; | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 | entry = EXT4_XATTR_NEXT(entry); | 
|---|
| 286 | } | 
|---|
| 287 | if (bh) | 
|---|
| 288 | set_buffer_verified(bh); | 
|---|
| 289 | return 0; | 
|---|
| 290 |  | 
|---|
| 291 | errout: | 
|---|
| 292 | if (bh) | 
|---|
| 293 | __ext4_error_inode(inode, function, line, 0, -err, | 
|---|
| 294 | "corrupted xattr block %llu: %s", | 
|---|
| 295 | (unsigned long long) bh->b_blocknr, | 
|---|
| 296 | err_str); | 
|---|
| 297 | else | 
|---|
| 298 | __ext4_error_inode(inode, function, line, 0, -err, | 
|---|
| 299 | "corrupted in-inode xattr: %s", err_str); | 
|---|
| 300 | return err; | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | static inline int | 
|---|
| 304 | __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh, | 
|---|
| 305 | const char *function, unsigned int line) | 
|---|
| 306 | { | 
|---|
| 307 | return check_xattrs(inode, bh, BFIRST(bh), end: bh->b_data + bh->b_size, | 
|---|
| 308 | value_start: bh->b_data, function, line); | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | #define ext4_xattr_check_block(inode, bh) \ | 
|---|
| 312 | __ext4_xattr_check_block((inode), (bh),  __func__, __LINE__) | 
|---|
| 313 |  | 
|---|
| 314 |  | 
|---|
| 315 | int | 
|---|
| 316 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *, | 
|---|
| 317 | void *end, const char *function, unsigned int line) | 
|---|
| 318 | { | 
|---|
| 319 | return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header), | 
|---|
| 320 | function, line); | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | static int | 
|---|
| 324 | xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry, | 
|---|
| 325 | void *end, int name_index, const char *name, int sorted) | 
|---|
| 326 | { | 
|---|
| 327 | struct ext4_xattr_entry *entry, *next; | 
|---|
| 328 | size_t name_len; | 
|---|
| 329 | int cmp = 1; | 
|---|
| 330 |  | 
|---|
| 331 | if (name == NULL) | 
|---|
| 332 | return -EINVAL; | 
|---|
| 333 | name_len = strlen(name); | 
|---|
| 334 | for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) { | 
|---|
| 335 | next = EXT4_XATTR_NEXT(entry); | 
|---|
| 336 | if ((void *) next >= end) { | 
|---|
| 337 | EXT4_ERROR_INODE(inode, "corrupted xattr entries"); | 
|---|
| 338 | return -EFSCORRUPTED; | 
|---|
| 339 | } | 
|---|
| 340 | cmp = name_index - entry->e_name_index; | 
|---|
| 341 | if (!cmp) | 
|---|
| 342 | cmp = name_len - entry->e_name_len; | 
|---|
| 343 | if (!cmp) | 
|---|
| 344 | cmp = memcmp(name, entry->e_name, name_len); | 
|---|
| 345 | if (!cmp || (cmp < 0 && sorted)) | 
|---|
| 346 | break; | 
|---|
| 347 | } | 
|---|
| 348 | *pentry = entry; | 
|---|
| 349 | return cmp ? -ENODATA : 0; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | static u32 | 
|---|
| 353 | ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size) | 
|---|
| 354 | { | 
|---|
| 355 | return ext4_chksum(crc: sbi->s_csum_seed, address: buffer, length: size); | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode) | 
|---|
| 359 | { | 
|---|
| 360 | return ((u64) inode_get_ctime_sec(inode: ea_inode) << 32) | | 
|---|
| 361 | (u32) inode_peek_iversion_raw(inode: ea_inode); | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count) | 
|---|
| 365 | { | 
|---|
| 366 | inode_set_ctime(inode: ea_inode, sec: (u32)(ref_count >> 32), nsec: 0); | 
|---|
| 367 | inode_set_iversion_raw(inode: ea_inode, val: ref_count & 0xffffffff); | 
|---|
| 368 | } | 
|---|
| 369 |  | 
|---|
| 370 | static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode) | 
|---|
| 371 | { | 
|---|
| 372 | return (u32) inode_get_atime_sec(inode: ea_inode); | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash) | 
|---|
| 376 | { | 
|---|
| 377 | inode_set_atime(inode: ea_inode, sec: hash, nsec: 0); | 
|---|
| 378 | } | 
|---|
| 379 |  | 
|---|
| 380 | /* | 
|---|
| 381 | * Read the EA value from an inode. | 
|---|
| 382 | */ | 
|---|
| 383 | static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size) | 
|---|
| 384 | { | 
|---|
| 385 | int blocksize = 1 << ea_inode->i_blkbits; | 
|---|
| 386 | int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits; | 
|---|
| 387 | int tail_size = (size % blocksize) ?: blocksize; | 
|---|
| 388 | struct buffer_head *bhs_inline[8]; | 
|---|
| 389 | struct buffer_head **bhs = bhs_inline; | 
|---|
| 390 | int i, ret; | 
|---|
| 391 |  | 
|---|
| 392 | if (bh_count > ARRAY_SIZE(bhs_inline)) { | 
|---|
| 393 | bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS); | 
|---|
| 394 | if (!bhs) | 
|---|
| 395 | return -ENOMEM; | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | ret = ext4_bread_batch(inode: ea_inode, block: 0 /* block */, bh_count, | 
|---|
| 399 | wait: true /* wait */, bhs); | 
|---|
| 400 | if (ret) | 
|---|
| 401 | goto free_bhs; | 
|---|
| 402 |  | 
|---|
| 403 | for (i = 0; i < bh_count; i++) { | 
|---|
| 404 | /* There shouldn't be any holes in ea_inode. */ | 
|---|
| 405 | if (!bhs[i]) { | 
|---|
| 406 | ret = -EFSCORRUPTED; | 
|---|
| 407 | goto put_bhs; | 
|---|
| 408 | } | 
|---|
| 409 | memcpy(to: (char *)buf + blocksize * i, from: bhs[i]->b_data, | 
|---|
| 410 | len: i < bh_count - 1 ? blocksize : tail_size); | 
|---|
| 411 | } | 
|---|
| 412 | ret = 0; | 
|---|
| 413 | put_bhs: | 
|---|
| 414 | for (i = 0; i < bh_count; i++) | 
|---|
| 415 | brelse(bh: bhs[i]); | 
|---|
| 416 | free_bhs: | 
|---|
| 417 | if (bhs != bhs_inline) | 
|---|
| 418 | kfree(objp: bhs); | 
|---|
| 419 | return ret; | 
|---|
| 420 | } | 
|---|
| 421 |  | 
|---|
| 422 | #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode_get_mtime_sec(inode))) | 
|---|
| 423 |  | 
|---|
| 424 | static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, | 
|---|
| 425 | u32 ea_inode_hash, struct inode **ea_inode) | 
|---|
| 426 | { | 
|---|
| 427 | struct inode *inode; | 
|---|
| 428 | int err; | 
|---|
| 429 |  | 
|---|
| 430 | /* | 
|---|
| 431 | * We have to check for this corruption early as otherwise | 
|---|
| 432 | * iget_locked() could wait indefinitely for the state of our | 
|---|
| 433 | * parent inode. | 
|---|
| 434 | */ | 
|---|
| 435 | if (parent->i_ino == ea_ino) { | 
|---|
| 436 | ext4_error(parent->i_sb, | 
|---|
| 437 | "Parent and EA inode have the same ino %lu", ea_ino); | 
|---|
| 438 | return -EFSCORRUPTED; | 
|---|
| 439 | } | 
|---|
| 440 |  | 
|---|
| 441 | inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE); | 
|---|
| 442 | if (IS_ERR(ptr: inode)) { | 
|---|
| 443 | err = PTR_ERR(ptr: inode); | 
|---|
| 444 | ext4_error(parent->i_sb, | 
|---|
| 445 | "error while reading EA inode %lu err=%d", ea_ino, | 
|---|
| 446 | err); | 
|---|
| 447 | return err; | 
|---|
| 448 | } | 
|---|
| 449 | ext4_xattr_inode_set_class(ea_inode: inode); | 
|---|
| 450 |  | 
|---|
| 451 | /* | 
|---|
| 452 | * Check whether this is an old Lustre-style xattr inode. Lustre | 
|---|
| 453 | * implementation does not have hash validation, rather it has a | 
|---|
| 454 | * backpointer from ea_inode to the parent inode. | 
|---|
| 455 | */ | 
|---|
| 456 | if (ea_inode_hash != ext4_xattr_inode_get_hash(ea_inode: inode) && | 
|---|
| 457 | EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino && | 
|---|
| 458 | inode->i_generation == parent->i_generation) { | 
|---|
| 459 | ext4_set_inode_state(inode, bit: EXT4_STATE_LUSTRE_EA_INODE); | 
|---|
| 460 | ext4_xattr_inode_set_ref(ea_inode: inode, ref_count: 1); | 
|---|
| 461 | } else { | 
|---|
| 462 | inode_lock_nested(inode, subclass: I_MUTEX_XATTR); | 
|---|
| 463 | inode->i_flags |= S_NOQUOTA; | 
|---|
| 464 | inode_unlock(inode); | 
|---|
| 465 | } | 
|---|
| 466 |  | 
|---|
| 467 | *ea_inode = inode; | 
|---|
| 468 | return 0; | 
|---|
| 469 | } | 
|---|
| 470 |  | 
|---|
| 471 | /* Remove entry from mbcache when EA inode is getting evicted */ | 
|---|
| 472 | void ext4_evict_ea_inode(struct inode *inode) | 
|---|
| 473 | { | 
|---|
| 474 | struct mb_cache_entry *oe; | 
|---|
| 475 |  | 
|---|
| 476 | if (!EA_INODE_CACHE(inode)) | 
|---|
| 477 | return; | 
|---|
| 478 | /* Wait for entry to get unused so that we can remove it */ | 
|---|
| 479 | while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode), | 
|---|
| 480 | key: ext4_xattr_inode_get_hash(ea_inode: inode), value: inode->i_ino))) { | 
|---|
| 481 | mb_cache_entry_wait_unused(entry: oe); | 
|---|
| 482 | mb_cache_entry_put(EA_INODE_CACHE(inode), entry: oe); | 
|---|
| 483 | } | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 | static int | 
|---|
| 487 | ext4_xattr_inode_verify_hashes(struct inode *ea_inode, | 
|---|
| 488 | struct ext4_xattr_entry *entry, void *buffer, | 
|---|
| 489 | size_t size) | 
|---|
| 490 | { | 
|---|
| 491 | u32 hash; | 
|---|
| 492 |  | 
|---|
| 493 | /* Verify stored hash matches calculated hash. */ | 
|---|
| 494 | hash = ext4_xattr_inode_hash(sbi: EXT4_SB(sb: ea_inode->i_sb), buffer, size); | 
|---|
| 495 | if (hash != ext4_xattr_inode_get_hash(ea_inode)) | 
|---|
| 496 | return -EFSCORRUPTED; | 
|---|
| 497 |  | 
|---|
| 498 | if (entry) { | 
|---|
| 499 | __le32 e_hash, tmp_data; | 
|---|
| 500 |  | 
|---|
| 501 | /* Verify entry hash. */ | 
|---|
| 502 | tmp_data = cpu_to_le32(hash); | 
|---|
| 503 | e_hash = ext4_xattr_hash_entry(name: entry->e_name, name_len: entry->e_name_len, | 
|---|
| 504 | value: &tmp_data, value_count: 1); | 
|---|
| 505 | /* All good? */ | 
|---|
| 506 | if (e_hash == entry->e_hash) | 
|---|
| 507 | return 0; | 
|---|
| 508 |  | 
|---|
| 509 | /* | 
|---|
| 510 | * Not good. Maybe the entry hash was calculated | 
|---|
| 511 | * using the buggy signed char version? | 
|---|
| 512 | */ | 
|---|
| 513 | e_hash = ext4_xattr_hash_entry_signed(name: entry->e_name, name_len: entry->e_name_len, | 
|---|
| 514 | value: &tmp_data, value_count: 1); | 
|---|
| 515 | /* Still no match - bad */ | 
|---|
| 516 | if (e_hash != entry->e_hash) | 
|---|
| 517 | return -EFSCORRUPTED; | 
|---|
| 518 |  | 
|---|
| 519 | /* Let people know about old hash */ | 
|---|
| 520 | pr_warn_once( "ext4: filesystem with signed xattr name hash"); | 
|---|
| 521 | } | 
|---|
| 522 | return 0; | 
|---|
| 523 | } | 
|---|
| 524 |  | 
|---|
| 525 | /* | 
|---|
| 526 | * Read xattr value from the EA inode. | 
|---|
| 527 | */ | 
|---|
| 528 | static int | 
|---|
| 529 | ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry, | 
|---|
| 530 | void *buffer, size_t size) | 
|---|
| 531 | { | 
|---|
| 532 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); | 
|---|
| 533 | struct inode *ea_inode; | 
|---|
| 534 | int err; | 
|---|
| 535 |  | 
|---|
| 536 | err = ext4_xattr_inode_iget(parent: inode, le32_to_cpu(entry->e_value_inum), | 
|---|
| 537 | le32_to_cpu(entry->e_hash), ea_inode: &ea_inode); | 
|---|
| 538 | if (err) { | 
|---|
| 539 | ea_inode = NULL; | 
|---|
| 540 | goto out; | 
|---|
| 541 | } | 
|---|
| 542 |  | 
|---|
| 543 | if (i_size_read(inode: ea_inode) != size) { | 
|---|
| 544 | ext4_warning_inode(ea_inode, | 
|---|
| 545 | "ea_inode file size=%llu entry size=%zu", | 
|---|
| 546 | i_size_read(ea_inode), size); | 
|---|
| 547 | err = -EFSCORRUPTED; | 
|---|
| 548 | goto out; | 
|---|
| 549 | } | 
|---|
| 550 |  | 
|---|
| 551 | err = ext4_xattr_inode_read(ea_inode, buf: buffer, size); | 
|---|
| 552 | if (err) | 
|---|
| 553 | goto out; | 
|---|
| 554 |  | 
|---|
| 555 | if (!ext4_test_inode_state(inode: ea_inode, bit: EXT4_STATE_LUSTRE_EA_INODE)) { | 
|---|
| 556 | err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, | 
|---|
| 557 | size); | 
|---|
| 558 | if (err) { | 
|---|
| 559 | ext4_warning_inode(ea_inode, | 
|---|
| 560 | "EA inode hash validation failed"); | 
|---|
| 561 | goto out; | 
|---|
| 562 | } | 
|---|
| 563 |  | 
|---|
| 564 | if (ea_inode_cache) | 
|---|
| 565 | mb_cache_entry_create(cache: ea_inode_cache, GFP_NOFS, | 
|---|
| 566 | key: ext4_xattr_inode_get_hash(ea_inode), | 
|---|
| 567 | value: ea_inode->i_ino, reusable: true /* reusable */); | 
|---|
| 568 | } | 
|---|
| 569 | out: | 
|---|
| 570 | iput(ea_inode); | 
|---|
| 571 | return err; | 
|---|
| 572 | } | 
|---|
| 573 |  | 
|---|
| 574 | static int | 
|---|
| 575 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, | 
|---|
| 576 | void *buffer, size_t buffer_size) | 
|---|
| 577 | { | 
|---|
| 578 | struct buffer_head *bh = NULL; | 
|---|
| 579 | struct ext4_xattr_entry *entry; | 
|---|
| 580 | size_t size; | 
|---|
| 581 | void *end; | 
|---|
| 582 | int error; | 
|---|
| 583 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); | 
|---|
| 584 |  | 
|---|
| 585 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", | 
|---|
| 586 | name_index, name, buffer, (long)buffer_size); | 
|---|
| 587 |  | 
|---|
| 588 | if (!EXT4_I(inode)->i_file_acl) | 
|---|
| 589 | return -ENODATA; | 
|---|
| 590 | ea_idebug(inode, "reading block %llu", | 
|---|
| 591 | (unsigned long long)EXT4_I(inode)->i_file_acl); | 
|---|
| 592 | bh = ext4_sb_bread(sb: inode->i_sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 593 | if (IS_ERR(ptr: bh)) | 
|---|
| 594 | return PTR_ERR(ptr: bh); | 
|---|
| 595 | ea_bdebug(bh, "b_count=%d, refcount=%d", | 
|---|
| 596 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | 
|---|
| 597 | error = ext4_xattr_check_block(inode, bh); | 
|---|
| 598 | if (error) | 
|---|
| 599 | goto cleanup; | 
|---|
| 600 | ext4_xattr_block_cache_insert(ea_block_cache, bh); | 
|---|
| 601 | entry = BFIRST(bh); | 
|---|
| 602 | end = bh->b_data + bh->b_size; | 
|---|
| 603 | error = xattr_find_entry(inode, pentry: &entry, end, name_index, name, sorted: 1); | 
|---|
| 604 | if (error) | 
|---|
| 605 | goto cleanup; | 
|---|
| 606 | size = le32_to_cpu(entry->e_value_size); | 
|---|
| 607 | error = -ERANGE; | 
|---|
| 608 | if (unlikely(size > EXT4_XATTR_SIZE_MAX)) | 
|---|
| 609 | goto cleanup; | 
|---|
| 610 | if (buffer) { | 
|---|
| 611 | if (size > buffer_size) | 
|---|
| 612 | goto cleanup; | 
|---|
| 613 | if (entry->e_value_inum) { | 
|---|
| 614 | error = ext4_xattr_inode_get(inode, entry, buffer, | 
|---|
| 615 | size); | 
|---|
| 616 | if (error) | 
|---|
| 617 | goto cleanup; | 
|---|
| 618 | } else { | 
|---|
| 619 | u16 offset = le16_to_cpu(entry->e_value_offs); | 
|---|
| 620 | void *p = bh->b_data + offset; | 
|---|
| 621 |  | 
|---|
| 622 | if (unlikely(p + size > end)) | 
|---|
| 623 | goto cleanup; | 
|---|
| 624 | memcpy(to: buffer, from: p, len: size); | 
|---|
| 625 | } | 
|---|
| 626 | } | 
|---|
| 627 | error = size; | 
|---|
| 628 |  | 
|---|
| 629 | cleanup: | 
|---|
| 630 | brelse(bh); | 
|---|
| 631 | return error; | 
|---|
| 632 | } | 
|---|
| 633 |  | 
|---|
| 634 | int | 
|---|
| 635 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, | 
|---|
| 636 | void *buffer, size_t buffer_size) | 
|---|
| 637 | { | 
|---|
| 638 | struct ext4_xattr_ibody_header *; | 
|---|
| 639 | struct ext4_xattr_entry *entry; | 
|---|
| 640 | struct ext4_inode *raw_inode; | 
|---|
| 641 | struct ext4_iloc iloc; | 
|---|
| 642 | size_t size; | 
|---|
| 643 | void *end; | 
|---|
| 644 | int error; | 
|---|
| 645 |  | 
|---|
| 646 | if (!ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR)) | 
|---|
| 647 | return -ENODATA; | 
|---|
| 648 | error = ext4_get_inode_loc(inode, &iloc); | 
|---|
| 649 | if (error) | 
|---|
| 650 | return error; | 
|---|
| 651 | raw_inode = ext4_raw_inode(iloc: &iloc); | 
|---|
| 652 | header = IHDR(inode, raw_inode); | 
|---|
| 653 | end = ITAIL(inode, raw_inode); | 
|---|
| 654 | entry = IFIRST(header); | 
|---|
| 655 | error = xattr_find_entry(inode, pentry: &entry, end, name_index, name, sorted: 0); | 
|---|
| 656 | if (error) | 
|---|
| 657 | goto cleanup; | 
|---|
| 658 | size = le32_to_cpu(entry->e_value_size); | 
|---|
| 659 | error = -ERANGE; | 
|---|
| 660 | if (unlikely(size > EXT4_XATTR_SIZE_MAX)) | 
|---|
| 661 | goto cleanup; | 
|---|
| 662 | if (buffer) { | 
|---|
| 663 | if (size > buffer_size) | 
|---|
| 664 | goto cleanup; | 
|---|
| 665 | if (entry->e_value_inum) { | 
|---|
| 666 | error = ext4_xattr_inode_get(inode, entry, buffer, | 
|---|
| 667 | size); | 
|---|
| 668 | if (error) | 
|---|
| 669 | goto cleanup; | 
|---|
| 670 | } else { | 
|---|
| 671 | u16 offset = le16_to_cpu(entry->e_value_offs); | 
|---|
| 672 | void *p = (void *)IFIRST(header) + offset; | 
|---|
| 673 |  | 
|---|
| 674 | if (unlikely(p + size > end)) | 
|---|
| 675 | goto cleanup; | 
|---|
| 676 | memcpy(to: buffer, from: p, len: size); | 
|---|
| 677 | } | 
|---|
| 678 | } | 
|---|
| 679 | error = size; | 
|---|
| 680 |  | 
|---|
| 681 | cleanup: | 
|---|
| 682 | brelse(bh: iloc.bh); | 
|---|
| 683 | return error; | 
|---|
| 684 | } | 
|---|
| 685 |  | 
|---|
| 686 | /* | 
|---|
| 687 | * ext4_xattr_get() | 
|---|
| 688 | * | 
|---|
| 689 | * Copy an extended attribute into the buffer | 
|---|
| 690 | * provided, or compute the buffer size required. | 
|---|
| 691 | * Buffer is NULL to compute the size of the buffer required. | 
|---|
| 692 | * | 
|---|
| 693 | * Returns a negative error number on failure, or the number of bytes | 
|---|
| 694 | * used / required on success. | 
|---|
| 695 | */ | 
|---|
| 696 | int | 
|---|
| 697 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, | 
|---|
| 698 | void *buffer, size_t buffer_size) | 
|---|
| 699 | { | 
|---|
| 700 | int error; | 
|---|
| 701 |  | 
|---|
| 702 | if (unlikely(ext4_forced_shutdown(inode->i_sb))) | 
|---|
| 703 | return -EIO; | 
|---|
| 704 |  | 
|---|
| 705 | if (strlen(name) > 255) | 
|---|
| 706 | return -ERANGE; | 
|---|
| 707 |  | 
|---|
| 708 | down_read(sem: &EXT4_I(inode)->xattr_sem); | 
|---|
| 709 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, | 
|---|
| 710 | buffer_size); | 
|---|
| 711 | if (error == -ENODATA) | 
|---|
| 712 | error = ext4_xattr_block_get(inode, name_index, name, buffer, | 
|---|
| 713 | buffer_size); | 
|---|
| 714 | up_read(sem: &EXT4_I(inode)->xattr_sem); | 
|---|
| 715 | return error; | 
|---|
| 716 | } | 
|---|
| 717 |  | 
|---|
| 718 | static int | 
|---|
| 719 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, | 
|---|
| 720 | char *buffer, size_t buffer_size) | 
|---|
| 721 | { | 
|---|
| 722 | size_t rest = buffer_size; | 
|---|
| 723 |  | 
|---|
| 724 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { | 
|---|
| 725 | const char *prefix; | 
|---|
| 726 |  | 
|---|
| 727 | prefix = ext4_xattr_prefix(name_index: entry->e_name_index, dentry); | 
|---|
| 728 | if (prefix) { | 
|---|
| 729 | size_t prefix_len = strlen(prefix); | 
|---|
| 730 | size_t size = prefix_len + entry->e_name_len + 1; | 
|---|
| 731 |  | 
|---|
| 732 | if (buffer) { | 
|---|
| 733 | if (size > rest) | 
|---|
| 734 | return -ERANGE; | 
|---|
| 735 | memcpy(to: buffer, from: prefix, len: prefix_len); | 
|---|
| 736 | buffer += prefix_len; | 
|---|
| 737 | memcpy(to: buffer, from: entry->e_name, len: entry->e_name_len); | 
|---|
| 738 | buffer += entry->e_name_len; | 
|---|
| 739 | *buffer++ = 0; | 
|---|
| 740 | } | 
|---|
| 741 | rest -= size; | 
|---|
| 742 | } | 
|---|
| 743 | } | 
|---|
| 744 | return buffer_size - rest;  /* total size */ | 
|---|
| 745 | } | 
|---|
| 746 |  | 
|---|
| 747 | static int | 
|---|
| 748 | ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) | 
|---|
| 749 | { | 
|---|
| 750 | struct inode *inode = d_inode(dentry); | 
|---|
| 751 | struct buffer_head *bh = NULL; | 
|---|
| 752 | int error; | 
|---|
| 753 |  | 
|---|
| 754 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", | 
|---|
| 755 | buffer, (long)buffer_size); | 
|---|
| 756 |  | 
|---|
| 757 | if (!EXT4_I(inode)->i_file_acl) | 
|---|
| 758 | return 0; | 
|---|
| 759 | ea_idebug(inode, "reading block %llu", | 
|---|
| 760 | (unsigned long long)EXT4_I(inode)->i_file_acl); | 
|---|
| 761 | bh = ext4_sb_bread(sb: inode->i_sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 762 | if (IS_ERR(ptr: bh)) | 
|---|
| 763 | return PTR_ERR(ptr: bh); | 
|---|
| 764 | ea_bdebug(bh, "b_count=%d, refcount=%d", | 
|---|
| 765 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); | 
|---|
| 766 | error = ext4_xattr_check_block(inode, bh); | 
|---|
| 767 | if (error) | 
|---|
| 768 | goto cleanup; | 
|---|
| 769 | ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh); | 
|---|
| 770 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, | 
|---|
| 771 | buffer_size); | 
|---|
| 772 | cleanup: | 
|---|
| 773 | brelse(bh); | 
|---|
| 774 | return error; | 
|---|
| 775 | } | 
|---|
| 776 |  | 
|---|
| 777 | static int | 
|---|
| 778 | ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) | 
|---|
| 779 | { | 
|---|
| 780 | struct inode *inode = d_inode(dentry); | 
|---|
| 781 | struct ext4_xattr_ibody_header *; | 
|---|
| 782 | struct ext4_inode *raw_inode; | 
|---|
| 783 | struct ext4_iloc iloc; | 
|---|
| 784 | int error; | 
|---|
| 785 |  | 
|---|
| 786 | if (!ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR)) | 
|---|
| 787 | return 0; | 
|---|
| 788 | error = ext4_get_inode_loc(inode, &iloc); | 
|---|
| 789 | if (error) | 
|---|
| 790 | return error; | 
|---|
| 791 | raw_inode = ext4_raw_inode(iloc: &iloc); | 
|---|
| 792 | header = IHDR(inode, raw_inode); | 
|---|
| 793 | error = ext4_xattr_list_entries(dentry, IFIRST(header), | 
|---|
| 794 | buffer, buffer_size); | 
|---|
| 795 |  | 
|---|
| 796 | brelse(bh: iloc.bh); | 
|---|
| 797 | return error; | 
|---|
| 798 | } | 
|---|
| 799 |  | 
|---|
| 800 | /* | 
|---|
| 801 | * Inode operation listxattr() | 
|---|
| 802 | * | 
|---|
| 803 | * d_inode(dentry)->i_rwsem: don't care | 
|---|
| 804 | * | 
|---|
| 805 | * Copy a list of attribute names into the buffer | 
|---|
| 806 | * provided, or compute the buffer size required. | 
|---|
| 807 | * Buffer is NULL to compute the size of the buffer required. | 
|---|
| 808 | * | 
|---|
| 809 | * Returns a negative error number on failure, or the number of bytes | 
|---|
| 810 | * used / required on success. | 
|---|
| 811 | */ | 
|---|
| 812 | ssize_t | 
|---|
| 813 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | 
|---|
| 814 | { | 
|---|
| 815 | int ret, ret2; | 
|---|
| 816 |  | 
|---|
| 817 | down_read(sem: &EXT4_I(inode: d_inode(dentry))->xattr_sem); | 
|---|
| 818 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); | 
|---|
| 819 | if (ret < 0) | 
|---|
| 820 | goto errout; | 
|---|
| 821 | if (buffer) { | 
|---|
| 822 | buffer += ret; | 
|---|
| 823 | buffer_size -= ret; | 
|---|
| 824 | } | 
|---|
| 825 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); | 
|---|
| 826 | if (ret < 0) | 
|---|
| 827 | goto errout; | 
|---|
| 828 | ret += ret2; | 
|---|
| 829 | errout: | 
|---|
| 830 | up_read(sem: &EXT4_I(inode: d_inode(dentry))->xattr_sem); | 
|---|
| 831 | return ret; | 
|---|
| 832 | } | 
|---|
| 833 |  | 
|---|
| 834 | /* | 
|---|
| 835 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is | 
|---|
| 836 | * not set, set it. | 
|---|
| 837 | */ | 
|---|
| 838 | static void ext4_xattr_update_super_block(handle_t *handle, | 
|---|
| 839 | struct super_block *sb) | 
|---|
| 840 | { | 
|---|
| 841 | if (ext4_has_feature_xattr(sb)) | 
|---|
| 842 | return; | 
|---|
| 843 |  | 
|---|
| 844 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); | 
|---|
| 845 | if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, | 
|---|
| 846 | EXT4_JTR_NONE) == 0) { | 
|---|
| 847 | lock_buffer(bh: EXT4_SB(sb)->s_sbh); | 
|---|
| 848 | ext4_set_feature_xattr(sb); | 
|---|
| 849 | ext4_superblock_csum_set(sb); | 
|---|
| 850 | unlock_buffer(bh: EXT4_SB(sb)->s_sbh); | 
|---|
| 851 | ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); | 
|---|
| 852 | } | 
|---|
| 853 | } | 
|---|
| 854 |  | 
|---|
| 855 | int ext4_get_inode_usage(struct inode *inode, qsize_t *usage) | 
|---|
| 856 | { | 
|---|
| 857 | struct ext4_iloc iloc = { .bh = NULL }; | 
|---|
| 858 | struct buffer_head *bh = NULL; | 
|---|
| 859 | struct ext4_inode *raw_inode; | 
|---|
| 860 | struct ext4_xattr_ibody_header *; | 
|---|
| 861 | struct ext4_xattr_entry *entry; | 
|---|
| 862 | qsize_t ea_inode_refs = 0; | 
|---|
| 863 | int ret; | 
|---|
| 864 |  | 
|---|
| 865 | lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem); | 
|---|
| 866 |  | 
|---|
| 867 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR)) { | 
|---|
| 868 | ret = ext4_get_inode_loc(inode, &iloc); | 
|---|
| 869 | if (ret) | 
|---|
| 870 | goto out; | 
|---|
| 871 | raw_inode = ext4_raw_inode(iloc: &iloc); | 
|---|
| 872 | header = IHDR(inode, raw_inode); | 
|---|
| 873 |  | 
|---|
| 874 | for (entry = IFIRST(header); !IS_LAST_ENTRY(entry); | 
|---|
| 875 | entry = EXT4_XATTR_NEXT(entry)) | 
|---|
| 876 | if (entry->e_value_inum) | 
|---|
| 877 | ea_inode_refs++; | 
|---|
| 878 | } | 
|---|
| 879 |  | 
|---|
| 880 | if (EXT4_I(inode)->i_file_acl) { | 
|---|
| 881 | bh = ext4_sb_bread(sb: inode->i_sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 882 | if (IS_ERR(ptr: bh)) { | 
|---|
| 883 | ret = PTR_ERR(ptr: bh); | 
|---|
| 884 | bh = NULL; | 
|---|
| 885 | goto out; | 
|---|
| 886 | } | 
|---|
| 887 |  | 
|---|
| 888 | ret = ext4_xattr_check_block(inode, bh); | 
|---|
| 889 | if (ret) | 
|---|
| 890 | goto out; | 
|---|
| 891 |  | 
|---|
| 892 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); | 
|---|
| 893 | entry = EXT4_XATTR_NEXT(entry)) | 
|---|
| 894 | if (entry->e_value_inum) | 
|---|
| 895 | ea_inode_refs++; | 
|---|
| 896 | } | 
|---|
| 897 | *usage = ea_inode_refs + 1; | 
|---|
| 898 | ret = 0; | 
|---|
| 899 | out: | 
|---|
| 900 | brelse(bh: iloc.bh); | 
|---|
| 901 | brelse(bh); | 
|---|
| 902 | return ret; | 
|---|
| 903 | } | 
|---|
| 904 |  | 
|---|
| 905 | static inline size_t round_up_cluster(struct inode *inode, size_t length) | 
|---|
| 906 | { | 
|---|
| 907 | struct super_block *sb = inode->i_sb; | 
|---|
| 908 | size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits + | 
|---|
| 909 | inode->i_blkbits); | 
|---|
| 910 | size_t mask = ~(cluster_size - 1); | 
|---|
| 911 |  | 
|---|
| 912 | return (length + cluster_size - 1) & mask; | 
|---|
| 913 | } | 
|---|
| 914 |  | 
|---|
| 915 | static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len) | 
|---|
| 916 | { | 
|---|
| 917 | int err; | 
|---|
| 918 |  | 
|---|
| 919 | err = dquot_alloc_inode(inode); | 
|---|
| 920 | if (err) | 
|---|
| 921 | return err; | 
|---|
| 922 | err = dquot_alloc_space_nodirty(inode, nr: round_up_cluster(inode, length: len)); | 
|---|
| 923 | if (err) | 
|---|
| 924 | dquot_free_inode(inode); | 
|---|
| 925 | return err; | 
|---|
| 926 | } | 
|---|
| 927 |  | 
|---|
| 928 | static void ext4_xattr_inode_free_quota(struct inode *parent, | 
|---|
| 929 | struct inode *ea_inode, | 
|---|
| 930 | size_t len) | 
|---|
| 931 | { | 
|---|
| 932 | if (ea_inode && | 
|---|
| 933 | ext4_test_inode_state(inode: ea_inode, bit: EXT4_STATE_LUSTRE_EA_INODE)) | 
|---|
| 934 | return; | 
|---|
| 935 | dquot_free_space_nodirty(inode: parent, nr: round_up_cluster(inode: parent, length: len)); | 
|---|
| 936 | dquot_free_inode(inode: parent); | 
|---|
| 937 | } | 
|---|
| 938 |  | 
|---|
| 939 | int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode, | 
|---|
| 940 | struct buffer_head *block_bh, size_t value_len, | 
|---|
| 941 | bool is_create) | 
|---|
| 942 | { | 
|---|
| 943 | int credits; | 
|---|
| 944 | int blocks; | 
|---|
| 945 |  | 
|---|
| 946 | /* | 
|---|
| 947 | * 1) Owner inode update | 
|---|
| 948 | * 2) Ref count update on old xattr block | 
|---|
| 949 | * 3) new xattr block | 
|---|
| 950 | * 4) block bitmap update for new xattr block | 
|---|
| 951 | * 5) group descriptor for new xattr block | 
|---|
| 952 | * 6) block bitmap update for old xattr block | 
|---|
| 953 | * 7) group descriptor for old block | 
|---|
| 954 | * | 
|---|
| 955 | * 6 & 7 can happen if we have two racing threads T_a and T_b | 
|---|
| 956 | * which are each trying to set an xattr on inodes I_a and I_b | 
|---|
| 957 | * which were both initially sharing an xattr block. | 
|---|
| 958 | */ | 
|---|
| 959 | credits = 7; | 
|---|
| 960 |  | 
|---|
| 961 | /* Quota updates. */ | 
|---|
| 962 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb); | 
|---|
| 963 |  | 
|---|
| 964 | /* | 
|---|
| 965 | * In case of inline data, we may push out the data to a block, | 
|---|
| 966 | * so we need to reserve credits for this eventuality | 
|---|
| 967 | */ | 
|---|
| 968 | if (inode && ext4_has_inline_data(inode)) | 
|---|
| 969 | credits += ext4_chunk_trans_extent(inode, nrblocks: 1) + 1; | 
|---|
| 970 |  | 
|---|
| 971 | /* We are done if ea_inode feature is not enabled. */ | 
|---|
| 972 | if (!ext4_has_feature_ea_inode(sb)) | 
|---|
| 973 | return credits; | 
|---|
| 974 |  | 
|---|
| 975 | /* New ea_inode, inode map, block bitmap, group descriptor. */ | 
|---|
| 976 | credits += 4; | 
|---|
| 977 |  | 
|---|
| 978 | /* Data blocks. */ | 
|---|
| 979 | blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits; | 
|---|
| 980 |  | 
|---|
| 981 | /* Indirection block or one level of extent tree. */ | 
|---|
| 982 | blocks += 1; | 
|---|
| 983 |  | 
|---|
| 984 | /* Block bitmap and group descriptor updates for each block. */ | 
|---|
| 985 | credits += blocks * 2; | 
|---|
| 986 |  | 
|---|
| 987 | /* Blocks themselves. */ | 
|---|
| 988 | credits += blocks; | 
|---|
| 989 |  | 
|---|
| 990 | if (!is_create) { | 
|---|
| 991 | /* Dereference ea_inode holding old xattr value. | 
|---|
| 992 | * Old ea_inode, inode map, block bitmap, group descriptor. | 
|---|
| 993 | */ | 
|---|
| 994 | credits += 4; | 
|---|
| 995 |  | 
|---|
| 996 | /* Data blocks for old ea_inode. */ | 
|---|
| 997 | blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits; | 
|---|
| 998 |  | 
|---|
| 999 | /* Indirection block or one level of extent tree for old | 
|---|
| 1000 | * ea_inode. | 
|---|
| 1001 | */ | 
|---|
| 1002 | blocks += 1; | 
|---|
| 1003 |  | 
|---|
| 1004 | /* Block bitmap and group descriptor updates for each block. */ | 
|---|
| 1005 | credits += blocks * 2; | 
|---|
| 1006 | } | 
|---|
| 1007 |  | 
|---|
| 1008 | /* We may need to clone the existing xattr block in which case we need | 
|---|
| 1009 | * to increment ref counts for existing ea_inodes referenced by it. | 
|---|
| 1010 | */ | 
|---|
| 1011 | if (block_bh) { | 
|---|
| 1012 | struct ext4_xattr_entry *entry = BFIRST(block_bh); | 
|---|
| 1013 |  | 
|---|
| 1014 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) | 
|---|
| 1015 | if (entry->e_value_inum) | 
|---|
| 1016 | /* Ref count update on ea_inode. */ | 
|---|
| 1017 | credits += 1; | 
|---|
| 1018 | } | 
|---|
| 1019 | return credits; | 
|---|
| 1020 | } | 
|---|
| 1021 |  | 
|---|
| 1022 | static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode, | 
|---|
| 1023 | int ref_change) | 
|---|
| 1024 | { | 
|---|
| 1025 | struct ext4_iloc iloc; | 
|---|
| 1026 | u64 ref_count; | 
|---|
| 1027 | int ret; | 
|---|
| 1028 |  | 
|---|
| 1029 | inode_lock_nested(inode: ea_inode, subclass: I_MUTEX_XATTR); | 
|---|
| 1030 |  | 
|---|
| 1031 | ret = ext4_reserve_inode_write(handle, inode: ea_inode, iloc: &iloc); | 
|---|
| 1032 | if (ret) | 
|---|
| 1033 | goto out; | 
|---|
| 1034 |  | 
|---|
| 1035 | ref_count = ext4_xattr_inode_get_ref(ea_inode); | 
|---|
| 1036 | if ((ref_count == 0 && ref_change < 0) || (ref_count == U64_MAX && ref_change > 0)) { | 
|---|
| 1037 | ext4_error_inode(ea_inode, __func__, __LINE__, 0, | 
|---|
| 1038 | "EA inode %lu ref wraparound: ref_count=%lld ref_change=%d", | 
|---|
| 1039 | ea_inode->i_ino, ref_count, ref_change); | 
|---|
| 1040 | ret = -EFSCORRUPTED; | 
|---|
| 1041 | goto out; | 
|---|
| 1042 | } | 
|---|
| 1043 | ref_count += ref_change; | 
|---|
| 1044 | ext4_xattr_inode_set_ref(ea_inode, ref_count); | 
|---|
| 1045 |  | 
|---|
| 1046 | if (ref_change > 0) { | 
|---|
| 1047 | if (ref_count == 1) { | 
|---|
| 1048 | WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u", | 
|---|
| 1049 | ea_inode->i_ino, ea_inode->i_nlink); | 
|---|
| 1050 |  | 
|---|
| 1051 | set_nlink(inode: ea_inode, nlink: 1); | 
|---|
| 1052 | ext4_orphan_del(handle, ea_inode); | 
|---|
| 1053 | } | 
|---|
| 1054 | } else { | 
|---|
| 1055 | if (ref_count == 0) { | 
|---|
| 1056 | WARN_ONCE(ea_inode->i_nlink != 1, | 
|---|
| 1057 | "EA inode %lu i_nlink=%u", | 
|---|
| 1058 | ea_inode->i_ino, ea_inode->i_nlink); | 
|---|
| 1059 |  | 
|---|
| 1060 | clear_nlink(inode: ea_inode); | 
|---|
| 1061 | ext4_orphan_add(handle, ea_inode); | 
|---|
| 1062 | } | 
|---|
| 1063 | } | 
|---|
| 1064 |  | 
|---|
| 1065 | ret = ext4_mark_iloc_dirty(handle, inode: ea_inode, iloc: &iloc); | 
|---|
| 1066 | if (ret) | 
|---|
| 1067 | ext4_warning_inode(ea_inode, | 
|---|
| 1068 | "ext4_mark_iloc_dirty() failed ret=%d", ret); | 
|---|
| 1069 | out: | 
|---|
| 1070 | inode_unlock(inode: ea_inode); | 
|---|
| 1071 | return ret; | 
|---|
| 1072 | } | 
|---|
| 1073 |  | 
|---|
| 1074 | static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode) | 
|---|
| 1075 | { | 
|---|
| 1076 | return ext4_xattr_inode_update_ref(handle, ea_inode, ref_change: 1); | 
|---|
| 1077 | } | 
|---|
| 1078 |  | 
|---|
| 1079 | static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode) | 
|---|
| 1080 | { | 
|---|
| 1081 | return ext4_xattr_inode_update_ref(handle, ea_inode, ref_change: -1); | 
|---|
| 1082 | } | 
|---|
| 1083 |  | 
|---|
| 1084 | static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent, | 
|---|
| 1085 | struct ext4_xattr_entry *first) | 
|---|
| 1086 | { | 
|---|
| 1087 | struct inode *ea_inode; | 
|---|
| 1088 | struct ext4_xattr_entry *entry; | 
|---|
| 1089 | struct ext4_xattr_entry *failed_entry; | 
|---|
| 1090 | unsigned int ea_ino; | 
|---|
| 1091 | int err, saved_err; | 
|---|
| 1092 |  | 
|---|
| 1093 | for (entry = first; !IS_LAST_ENTRY(entry); | 
|---|
| 1094 | entry = EXT4_XATTR_NEXT(entry)) { | 
|---|
| 1095 | if (!entry->e_value_inum) | 
|---|
| 1096 | continue; | 
|---|
| 1097 | ea_ino = le32_to_cpu(entry->e_value_inum); | 
|---|
| 1098 | err = ext4_xattr_inode_iget(parent, ea_ino, | 
|---|
| 1099 | le32_to_cpu(entry->e_hash), | 
|---|
| 1100 | ea_inode: &ea_inode); | 
|---|
| 1101 | if (err) | 
|---|
| 1102 | goto cleanup; | 
|---|
| 1103 | err = ext4_xattr_inode_inc_ref(handle, ea_inode); | 
|---|
| 1104 | if (err) { | 
|---|
| 1105 | ext4_warning_inode(ea_inode, "inc ref error %d", err); | 
|---|
| 1106 | iput(ea_inode); | 
|---|
| 1107 | goto cleanup; | 
|---|
| 1108 | } | 
|---|
| 1109 | iput(ea_inode); | 
|---|
| 1110 | } | 
|---|
| 1111 | return 0; | 
|---|
| 1112 |  | 
|---|
| 1113 | cleanup: | 
|---|
| 1114 | saved_err = err; | 
|---|
| 1115 | failed_entry = entry; | 
|---|
| 1116 |  | 
|---|
| 1117 | for (entry = first; entry != failed_entry; | 
|---|
| 1118 | entry = EXT4_XATTR_NEXT(entry)) { | 
|---|
| 1119 | if (!entry->e_value_inum) | 
|---|
| 1120 | continue; | 
|---|
| 1121 | ea_ino = le32_to_cpu(entry->e_value_inum); | 
|---|
| 1122 | err = ext4_xattr_inode_iget(parent, ea_ino, | 
|---|
| 1123 | le32_to_cpu(entry->e_hash), | 
|---|
| 1124 | ea_inode: &ea_inode); | 
|---|
| 1125 | if (err) { | 
|---|
| 1126 | ext4_warning(parent->i_sb, | 
|---|
| 1127 | "cleanup ea_ino %u iget error %d", ea_ino, | 
|---|
| 1128 | err); | 
|---|
| 1129 | continue; | 
|---|
| 1130 | } | 
|---|
| 1131 | err = ext4_xattr_inode_dec_ref(handle, ea_inode); | 
|---|
| 1132 | if (err) | 
|---|
| 1133 | ext4_warning_inode(ea_inode, "cleanup dec ref error %d", | 
|---|
| 1134 | err); | 
|---|
| 1135 | iput(ea_inode); | 
|---|
| 1136 | } | 
|---|
| 1137 | return saved_err; | 
|---|
| 1138 | } | 
|---|
| 1139 |  | 
|---|
| 1140 | static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode, | 
|---|
| 1141 | struct buffer_head *bh, bool block_csum, bool dirty) | 
|---|
| 1142 | { | 
|---|
| 1143 | int error; | 
|---|
| 1144 |  | 
|---|
| 1145 | if (bh && dirty) { | 
|---|
| 1146 | if (block_csum) | 
|---|
| 1147 | ext4_xattr_block_csum_set(inode, bh); | 
|---|
| 1148 | error = ext4_handle_dirty_metadata(handle, NULL, bh); | 
|---|
| 1149 | if (error) { | 
|---|
| 1150 | ext4_warning(inode->i_sb, "Handle metadata (error %d)", | 
|---|
| 1151 | error); | 
|---|
| 1152 | return error; | 
|---|
| 1153 | } | 
|---|
| 1154 | } | 
|---|
| 1155 | return 0; | 
|---|
| 1156 | } | 
|---|
| 1157 |  | 
|---|
| 1158 | static void | 
|---|
| 1159 | ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent, | 
|---|
| 1160 | struct buffer_head *bh, | 
|---|
| 1161 | struct ext4_xattr_entry *first, bool block_csum, | 
|---|
| 1162 | struct ext4_xattr_inode_array **ea_inode_array, | 
|---|
| 1163 | int , bool skip_quota) | 
|---|
| 1164 | { | 
|---|
| 1165 | struct inode *ea_inode; | 
|---|
| 1166 | struct ext4_xattr_entry *entry; | 
|---|
| 1167 | struct ext4_iloc iloc; | 
|---|
| 1168 | bool dirty = false; | 
|---|
| 1169 | unsigned int ea_ino; | 
|---|
| 1170 | int err; | 
|---|
| 1171 | int credits; | 
|---|
| 1172 | void *end; | 
|---|
| 1173 |  | 
|---|
| 1174 | if (block_csum) | 
|---|
| 1175 | end = (void *)bh->b_data + bh->b_size; | 
|---|
| 1176 | else { | 
|---|
| 1177 | ext4_get_inode_loc(parent, &iloc); | 
|---|
| 1178 | end = (void *)ext4_raw_inode(iloc: &iloc) + EXT4_SB(sb: parent->i_sb)->s_inode_size; | 
|---|
| 1179 | } | 
|---|
| 1180 |  | 
|---|
| 1181 | /* One credit for dec ref on ea_inode, one for orphan list addition, */ | 
|---|
| 1182 | credits = 2 + extra_credits; | 
|---|
| 1183 |  | 
|---|
| 1184 | for (entry = first; (void *)entry < end && !IS_LAST_ENTRY(entry); | 
|---|
| 1185 | entry = EXT4_XATTR_NEXT(entry)) { | 
|---|
| 1186 | if (!entry->e_value_inum) | 
|---|
| 1187 | continue; | 
|---|
| 1188 | ea_ino = le32_to_cpu(entry->e_value_inum); | 
|---|
| 1189 | err = ext4_xattr_inode_iget(parent, ea_ino, | 
|---|
| 1190 | le32_to_cpu(entry->e_hash), | 
|---|
| 1191 | ea_inode: &ea_inode); | 
|---|
| 1192 | if (err) | 
|---|
| 1193 | continue; | 
|---|
| 1194 |  | 
|---|
| 1195 | err = ext4_expand_inode_array(ea_inode_array, inode: ea_inode); | 
|---|
| 1196 | if (err) { | 
|---|
| 1197 | ext4_warning_inode(ea_inode, | 
|---|
| 1198 | "Expand inode array err=%d", err); | 
|---|
| 1199 | iput(ea_inode); | 
|---|
| 1200 | continue; | 
|---|
| 1201 | } | 
|---|
| 1202 |  | 
|---|
| 1203 | err = ext4_journal_ensure_credits_fn(handle, credits, credits, | 
|---|
| 1204 | ext4_free_metadata_revoke_credits(parent->i_sb, 1), | 
|---|
| 1205 | ext4_xattr_restart_fn(handle, parent, bh, block_csum, | 
|---|
| 1206 | dirty)); | 
|---|
| 1207 | if (err < 0) { | 
|---|
| 1208 | ext4_warning_inode(ea_inode, "Ensure credits err=%d", | 
|---|
| 1209 | err); | 
|---|
| 1210 | continue; | 
|---|
| 1211 | } | 
|---|
| 1212 | if (err > 0) { | 
|---|
| 1213 | err = ext4_journal_get_write_access(handle, | 
|---|
| 1214 | parent->i_sb, bh, EXT4_JTR_NONE); | 
|---|
| 1215 | if (err) { | 
|---|
| 1216 | ext4_warning_inode(ea_inode, | 
|---|
| 1217 | "Re-get write access err=%d", | 
|---|
| 1218 | err); | 
|---|
| 1219 | continue; | 
|---|
| 1220 | } | 
|---|
| 1221 | } | 
|---|
| 1222 |  | 
|---|
| 1223 | err = ext4_xattr_inode_dec_ref(handle, ea_inode); | 
|---|
| 1224 | if (err) { | 
|---|
| 1225 | ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d", | 
|---|
| 1226 | err); | 
|---|
| 1227 | continue; | 
|---|
| 1228 | } | 
|---|
| 1229 |  | 
|---|
| 1230 | if (!skip_quota) | 
|---|
| 1231 | ext4_xattr_inode_free_quota(parent, ea_inode, | 
|---|
| 1232 | le32_to_cpu(entry->e_value_size)); | 
|---|
| 1233 |  | 
|---|
| 1234 | /* | 
|---|
| 1235 | * Forget about ea_inode within the same transaction that | 
|---|
| 1236 | * decrements the ref count. This avoids duplicate decrements in | 
|---|
| 1237 | * case the rest of the work spills over to subsequent | 
|---|
| 1238 | * transactions. | 
|---|
| 1239 | */ | 
|---|
| 1240 | entry->e_value_inum = 0; | 
|---|
| 1241 | entry->e_value_size = 0; | 
|---|
| 1242 |  | 
|---|
| 1243 | dirty = true; | 
|---|
| 1244 | } | 
|---|
| 1245 |  | 
|---|
| 1246 | if (dirty) { | 
|---|
| 1247 | /* | 
|---|
| 1248 | * Note that we are deliberately skipping csum calculation for | 
|---|
| 1249 | * the final update because we do not expect any journal | 
|---|
| 1250 | * restarts until xattr block is freed. | 
|---|
| 1251 | */ | 
|---|
| 1252 |  | 
|---|
| 1253 | err = ext4_handle_dirty_metadata(handle, NULL, bh); | 
|---|
| 1254 | if (err) | 
|---|
| 1255 | ext4_warning_inode(parent, | 
|---|
| 1256 | "handle dirty metadata err=%d", err); | 
|---|
| 1257 | } | 
|---|
| 1258 | } | 
|---|
| 1259 |  | 
|---|
| 1260 | /* | 
|---|
| 1261 | * Release the xattr block BH: If the reference count is > 1, decrement it; | 
|---|
| 1262 | * otherwise free the block. | 
|---|
| 1263 | */ | 
|---|
| 1264 | static void | 
|---|
| 1265 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, | 
|---|
| 1266 | struct buffer_head *bh, | 
|---|
| 1267 | struct ext4_xattr_inode_array **ea_inode_array, | 
|---|
| 1268 | int ) | 
|---|
| 1269 | { | 
|---|
| 1270 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); | 
|---|
| 1271 | u32 hash, ref; | 
|---|
| 1272 | int error = 0; | 
|---|
| 1273 |  | 
|---|
| 1274 | BUFFER_TRACE(bh, "get_write_access"); | 
|---|
| 1275 | error = ext4_journal_get_write_access(handle, inode->i_sb, bh, | 
|---|
| 1276 | EXT4_JTR_NONE); | 
|---|
| 1277 | if (error) | 
|---|
| 1278 | goto out; | 
|---|
| 1279 |  | 
|---|
| 1280 | retry_ref: | 
|---|
| 1281 | lock_buffer(bh); | 
|---|
| 1282 | hash = le32_to_cpu(BHDR(bh)->h_hash); | 
|---|
| 1283 | ref = le32_to_cpu(BHDR(bh)->h_refcount); | 
|---|
| 1284 | if (ref == 1) { | 
|---|
| 1285 | ea_bdebug(bh, "refcount now=0; freeing"); | 
|---|
| 1286 | /* | 
|---|
| 1287 | * This must happen under buffer lock for | 
|---|
| 1288 | * ext4_xattr_block_set() to reliably detect freed block | 
|---|
| 1289 | */ | 
|---|
| 1290 | if (ea_block_cache) { | 
|---|
| 1291 | struct mb_cache_entry *oe; | 
|---|
| 1292 |  | 
|---|
| 1293 | oe = mb_cache_entry_delete_or_get(cache: ea_block_cache, key: hash, | 
|---|
| 1294 | value: bh->b_blocknr); | 
|---|
| 1295 | if (oe) { | 
|---|
| 1296 | unlock_buffer(bh); | 
|---|
| 1297 | mb_cache_entry_wait_unused(entry: oe); | 
|---|
| 1298 | mb_cache_entry_put(cache: ea_block_cache, entry: oe); | 
|---|
| 1299 | goto retry_ref; | 
|---|
| 1300 | } | 
|---|
| 1301 | } | 
|---|
| 1302 | get_bh(bh); | 
|---|
| 1303 | unlock_buffer(bh); | 
|---|
| 1304 |  | 
|---|
| 1305 | if (ext4_has_feature_ea_inode(sb: inode->i_sb)) | 
|---|
| 1306 | ext4_xattr_inode_dec_ref_all(handle, parent: inode, bh, | 
|---|
| 1307 | BFIRST(bh), | 
|---|
| 1308 | block_csum: true /* block_csum */, | 
|---|
| 1309 | ea_inode_array, | 
|---|
| 1310 | extra_credits, | 
|---|
| 1311 | skip_quota: true /* skip_quota */); | 
|---|
| 1312 | ext4_free_blocks(handle, inode, bh, block: 0, count: 1, | 
|---|
| 1313 | EXT4_FREE_BLOCKS_METADATA | | 
|---|
| 1314 | EXT4_FREE_BLOCKS_FORGET); | 
|---|
| 1315 | } else { | 
|---|
| 1316 | ref--; | 
|---|
| 1317 | BHDR(bh)->h_refcount = cpu_to_le32(ref); | 
|---|
| 1318 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { | 
|---|
| 1319 | struct mb_cache_entry *ce; | 
|---|
| 1320 |  | 
|---|
| 1321 | if (ea_block_cache) { | 
|---|
| 1322 | ce = mb_cache_entry_get(cache: ea_block_cache, key: hash, | 
|---|
| 1323 | value: bh->b_blocknr); | 
|---|
| 1324 | if (ce) { | 
|---|
| 1325 | set_bit(nr: MBE_REUSABLE_B, addr: &ce->e_flags); | 
|---|
| 1326 | mb_cache_entry_put(cache: ea_block_cache, entry: ce); | 
|---|
| 1327 | } | 
|---|
| 1328 | } | 
|---|
| 1329 | } | 
|---|
| 1330 |  | 
|---|
| 1331 | ext4_xattr_block_csum_set(inode, bh); | 
|---|
| 1332 | /* | 
|---|
| 1333 | * Beware of this ugliness: Releasing of xattr block references | 
|---|
| 1334 | * from different inodes can race and so we have to protect | 
|---|
| 1335 | * from a race where someone else frees the block (and releases | 
|---|
| 1336 | * its journal_head) before we are done dirtying the buffer. In | 
|---|
| 1337 | * nojournal mode this race is harmless and we actually cannot | 
|---|
| 1338 | * call ext4_handle_dirty_metadata() with locked buffer as | 
|---|
| 1339 | * that function can call sync_dirty_buffer() so for that case | 
|---|
| 1340 | * we handle the dirtying after unlocking the buffer. | 
|---|
| 1341 | */ | 
|---|
| 1342 | if (ext4_handle_valid(handle)) | 
|---|
| 1343 | error = ext4_handle_dirty_metadata(handle, inode, bh); | 
|---|
| 1344 | unlock_buffer(bh); | 
|---|
| 1345 | if (!ext4_handle_valid(handle)) | 
|---|
| 1346 | error = ext4_handle_dirty_metadata(handle, inode, bh); | 
|---|
| 1347 | if (IS_SYNC(inode)) | 
|---|
| 1348 | ext4_handle_sync(handle); | 
|---|
| 1349 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); | 
|---|
| 1350 | ea_bdebug(bh, "refcount now=%d; releasing", | 
|---|
| 1351 | le32_to_cpu(BHDR(bh)->h_refcount)); | 
|---|
| 1352 | } | 
|---|
| 1353 | out: | 
|---|
| 1354 | ext4_std_error(inode->i_sb, error); | 
|---|
| 1355 | return; | 
|---|
| 1356 | } | 
|---|
| 1357 |  | 
|---|
| 1358 | /* | 
|---|
| 1359 | * Find the available free space for EAs. This also returns the total number of | 
|---|
| 1360 | * bytes used by EA entries. | 
|---|
| 1361 | */ | 
|---|
| 1362 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, | 
|---|
| 1363 | size_t *min_offs, void *base, int *total) | 
|---|
| 1364 | { | 
|---|
| 1365 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
|---|
| 1366 | if (!last->e_value_inum && last->e_value_size) { | 
|---|
| 1367 | size_t offs = le16_to_cpu(last->e_value_offs); | 
|---|
| 1368 | if (offs < *min_offs) | 
|---|
| 1369 | *min_offs = offs; | 
|---|
| 1370 | } | 
|---|
| 1371 | if (total) | 
|---|
| 1372 | *total += EXT4_XATTR_LEN(last->e_name_len); | 
|---|
| 1373 | } | 
|---|
| 1374 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); | 
|---|
| 1375 | } | 
|---|
| 1376 |  | 
|---|
| 1377 | /* | 
|---|
| 1378 | * Write the value of the EA in an inode. | 
|---|
| 1379 | */ | 
|---|
| 1380 | static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode, | 
|---|
| 1381 | const void *buf, int bufsize) | 
|---|
| 1382 | { | 
|---|
| 1383 | struct buffer_head *bh = NULL; | 
|---|
| 1384 | unsigned long block = 0; | 
|---|
| 1385 | int blocksize = ea_inode->i_sb->s_blocksize; | 
|---|
| 1386 | int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits; | 
|---|
| 1387 | int csize, wsize = 0; | 
|---|
| 1388 | int ret = 0, ret2 = 0; | 
|---|
| 1389 | int retries = 0; | 
|---|
| 1390 |  | 
|---|
| 1391 | retry: | 
|---|
| 1392 | while (ret >= 0 && ret < max_blocks) { | 
|---|
| 1393 | struct ext4_map_blocks map; | 
|---|
| 1394 | map.m_lblk = block += ret; | 
|---|
| 1395 | map.m_len = max_blocks -= ret; | 
|---|
| 1396 |  | 
|---|
| 1397 | ret = ext4_map_blocks(handle, inode: ea_inode, map: &map, | 
|---|
| 1398 | EXT4_GET_BLOCKS_CREATE); | 
|---|
| 1399 | if (ret <= 0) { | 
|---|
| 1400 | ext4_mark_inode_dirty(handle, ea_inode); | 
|---|
| 1401 | if (ret == -ENOSPC && | 
|---|
| 1402 | ext4_should_retry_alloc(sb: ea_inode->i_sb, retries: &retries)) { | 
|---|
| 1403 | ret = 0; | 
|---|
| 1404 | goto retry; | 
|---|
| 1405 | } | 
|---|
| 1406 | break; | 
|---|
| 1407 | } | 
|---|
| 1408 | } | 
|---|
| 1409 |  | 
|---|
| 1410 | if (ret < 0) | 
|---|
| 1411 | return ret; | 
|---|
| 1412 |  | 
|---|
| 1413 | block = 0; | 
|---|
| 1414 | while (wsize < bufsize) { | 
|---|
| 1415 | brelse(bh); | 
|---|
| 1416 | csize = (bufsize - wsize) > blocksize ? blocksize : | 
|---|
| 1417 | bufsize - wsize; | 
|---|
| 1418 | bh = ext4_getblk(handle, ea_inode, block, 0); | 
|---|
| 1419 | if (IS_ERR(ptr: bh)) | 
|---|
| 1420 | return PTR_ERR(ptr: bh); | 
|---|
| 1421 | if (!bh) { | 
|---|
| 1422 | WARN_ON_ONCE(1); | 
|---|
| 1423 | EXT4_ERROR_INODE(ea_inode, | 
|---|
| 1424 | "ext4_getblk() return bh = NULL"); | 
|---|
| 1425 | return -EFSCORRUPTED; | 
|---|
| 1426 | } | 
|---|
| 1427 | ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh, | 
|---|
| 1428 | EXT4_JTR_NONE); | 
|---|
| 1429 | if (ret) | 
|---|
| 1430 | goto out; | 
|---|
| 1431 |  | 
|---|
| 1432 | memcpy(to: bh->b_data, from: buf, len: csize); | 
|---|
| 1433 | /* | 
|---|
| 1434 | * Zero out block tail to avoid writing uninitialized memory | 
|---|
| 1435 | * to disk. | 
|---|
| 1436 | */ | 
|---|
| 1437 | if (csize < blocksize) | 
|---|
| 1438 | memset(s: bh->b_data + csize, c: 0, n: blocksize - csize); | 
|---|
| 1439 | set_buffer_uptodate(bh); | 
|---|
| 1440 | ext4_handle_dirty_metadata(handle, ea_inode, bh); | 
|---|
| 1441 |  | 
|---|
| 1442 | buf += csize; | 
|---|
| 1443 | wsize += csize; | 
|---|
| 1444 | block += 1; | 
|---|
| 1445 | } | 
|---|
| 1446 |  | 
|---|
| 1447 | inode_lock(inode: ea_inode); | 
|---|
| 1448 | i_size_write(inode: ea_inode, i_size: wsize); | 
|---|
| 1449 | ext4_update_i_disksize(inode: ea_inode, newsize: wsize); | 
|---|
| 1450 | inode_unlock(inode: ea_inode); | 
|---|
| 1451 |  | 
|---|
| 1452 | ret2 = ext4_mark_inode_dirty(handle, ea_inode); | 
|---|
| 1453 | if (unlikely(ret2 && !ret)) | 
|---|
| 1454 | ret = ret2; | 
|---|
| 1455 |  | 
|---|
| 1456 | out: | 
|---|
| 1457 | brelse(bh); | 
|---|
| 1458 |  | 
|---|
| 1459 | return ret; | 
|---|
| 1460 | } | 
|---|
| 1461 |  | 
|---|
| 1462 | /* | 
|---|
| 1463 | * Create an inode to store the value of a large EA. | 
|---|
| 1464 | */ | 
|---|
| 1465 | static struct inode *ext4_xattr_inode_create(handle_t *handle, | 
|---|
| 1466 | struct inode *inode, u32 hash) | 
|---|
| 1467 | { | 
|---|
| 1468 | struct inode *ea_inode = NULL; | 
|---|
| 1469 | uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) }; | 
|---|
| 1470 | int err; | 
|---|
| 1471 |  | 
|---|
| 1472 | if (inode->i_sb->s_root == NULL) { | 
|---|
| 1473 | ext4_warning(inode->i_sb, | 
|---|
| 1474 | "refuse to create EA inode when umounting"); | 
|---|
| 1475 | WARN_ON(1); | 
|---|
| 1476 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1477 | } | 
|---|
| 1478 |  | 
|---|
| 1479 | /* | 
|---|
| 1480 | * Let the next inode be the goal, so we try and allocate the EA inode | 
|---|
| 1481 | * in the same group, or nearby one. | 
|---|
| 1482 | */ | 
|---|
| 1483 | ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode, | 
|---|
| 1484 | S_IFREG | 0600, NULL, inode->i_ino + 1, owner, | 
|---|
| 1485 | EXT4_EA_INODE_FL); | 
|---|
| 1486 | if (!IS_ERR(ptr: ea_inode)) { | 
|---|
| 1487 | ea_inode->i_op = &ext4_file_inode_operations; | 
|---|
| 1488 | ea_inode->i_fop = &ext4_file_operations; | 
|---|
| 1489 | ext4_set_aops(inode: ea_inode); | 
|---|
| 1490 | ext4_xattr_inode_set_class(ea_inode); | 
|---|
| 1491 | unlock_new_inode(ea_inode); | 
|---|
| 1492 | ext4_xattr_inode_set_ref(ea_inode, ref_count: 1); | 
|---|
| 1493 | ext4_xattr_inode_set_hash(ea_inode, hash); | 
|---|
| 1494 | err = ext4_mark_inode_dirty(handle, ea_inode); | 
|---|
| 1495 | if (!err) | 
|---|
| 1496 | err = ext4_inode_attach_jinode(inode: ea_inode); | 
|---|
| 1497 | if (err) { | 
|---|
| 1498 | if (ext4_xattr_inode_dec_ref(handle, ea_inode)) | 
|---|
| 1499 | ext4_warning_inode(ea_inode, | 
|---|
| 1500 | "cleanup dec ref error %d", err); | 
|---|
| 1501 | iput(ea_inode); | 
|---|
| 1502 | return ERR_PTR(error: err); | 
|---|
| 1503 | } | 
|---|
| 1504 |  | 
|---|
| 1505 | /* | 
|---|
| 1506 | * Xattr inodes are shared therefore quota charging is performed | 
|---|
| 1507 | * at a higher level. | 
|---|
| 1508 | */ | 
|---|
| 1509 | dquot_free_inode(inode: ea_inode); | 
|---|
| 1510 | dquot_drop(inode: ea_inode); | 
|---|
| 1511 | inode_lock(inode: ea_inode); | 
|---|
| 1512 | ea_inode->i_flags |= S_NOQUOTA; | 
|---|
| 1513 | inode_unlock(inode: ea_inode); | 
|---|
| 1514 | } | 
|---|
| 1515 |  | 
|---|
| 1516 | return ea_inode; | 
|---|
| 1517 | } | 
|---|
| 1518 |  | 
|---|
| 1519 | static struct inode * | 
|---|
| 1520 | ext4_xattr_inode_cache_find(struct inode *inode, const void *value, | 
|---|
| 1521 | size_t value_len, u32 hash) | 
|---|
| 1522 | { | 
|---|
| 1523 | struct inode *ea_inode; | 
|---|
| 1524 | struct mb_cache_entry *ce; | 
|---|
| 1525 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); | 
|---|
| 1526 | void *ea_data; | 
|---|
| 1527 |  | 
|---|
| 1528 | if (!ea_inode_cache) | 
|---|
| 1529 | return NULL; | 
|---|
| 1530 |  | 
|---|
| 1531 | ce = mb_cache_entry_find_first(cache: ea_inode_cache, key: hash); | 
|---|
| 1532 | if (!ce) | 
|---|
| 1533 | return NULL; | 
|---|
| 1534 |  | 
|---|
| 1535 | WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) && | 
|---|
| 1536 | !(current->flags & PF_MEMALLOC_NOFS)); | 
|---|
| 1537 |  | 
|---|
| 1538 | ea_data = kvmalloc(value_len, GFP_NOFS); | 
|---|
| 1539 | if (!ea_data) { | 
|---|
| 1540 | mb_cache_entry_put(cache: ea_inode_cache, entry: ce); | 
|---|
| 1541 | return NULL; | 
|---|
| 1542 | } | 
|---|
| 1543 |  | 
|---|
| 1544 | while (ce) { | 
|---|
| 1545 | ea_inode = ext4_iget(inode->i_sb, ce->e_value, | 
|---|
| 1546 | EXT4_IGET_EA_INODE); | 
|---|
| 1547 | if (IS_ERR(ptr: ea_inode)) | 
|---|
| 1548 | goto next_entry; | 
|---|
| 1549 | ext4_xattr_inode_set_class(ea_inode); | 
|---|
| 1550 | if (i_size_read(inode: ea_inode) == value_len && | 
|---|
| 1551 | !ext4_xattr_inode_read(ea_inode, buf: ea_data, size: value_len) && | 
|---|
| 1552 | !ext4_xattr_inode_verify_hashes(ea_inode, NULL, buffer: ea_data, | 
|---|
| 1553 | size: value_len) && | 
|---|
| 1554 | !memcmp(value, ea_data, value_len)) { | 
|---|
| 1555 | mb_cache_entry_touch(cache: ea_inode_cache, entry: ce); | 
|---|
| 1556 | mb_cache_entry_put(cache: ea_inode_cache, entry: ce); | 
|---|
| 1557 | kvfree(addr: ea_data); | 
|---|
| 1558 | return ea_inode; | 
|---|
| 1559 | } | 
|---|
| 1560 | iput(ea_inode); | 
|---|
| 1561 | next_entry: | 
|---|
| 1562 | ce = mb_cache_entry_find_next(cache: ea_inode_cache, entry: ce); | 
|---|
| 1563 | } | 
|---|
| 1564 | kvfree(addr: ea_data); | 
|---|
| 1565 | return NULL; | 
|---|
| 1566 | } | 
|---|
| 1567 |  | 
|---|
| 1568 | /* | 
|---|
| 1569 | * Add value of the EA in an inode. | 
|---|
| 1570 | */ | 
|---|
| 1571 | static struct inode *ext4_xattr_inode_lookup_create(handle_t *handle, | 
|---|
| 1572 | struct inode *inode, const void *value, size_t value_len) | 
|---|
| 1573 | { | 
|---|
| 1574 | struct inode *ea_inode; | 
|---|
| 1575 | u32 hash; | 
|---|
| 1576 | int err; | 
|---|
| 1577 |  | 
|---|
| 1578 | /* Account inode & space to quota even if sharing... */ | 
|---|
| 1579 | err = ext4_xattr_inode_alloc_quota(inode, len: value_len); | 
|---|
| 1580 | if (err) | 
|---|
| 1581 | return ERR_PTR(error: err); | 
|---|
| 1582 |  | 
|---|
| 1583 | hash = ext4_xattr_inode_hash(sbi: EXT4_SB(sb: inode->i_sb), buffer: value, size: value_len); | 
|---|
| 1584 | ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash); | 
|---|
| 1585 | if (ea_inode) { | 
|---|
| 1586 | err = ext4_xattr_inode_inc_ref(handle, ea_inode); | 
|---|
| 1587 | if (err) | 
|---|
| 1588 | goto out_err; | 
|---|
| 1589 | return ea_inode; | 
|---|
| 1590 | } | 
|---|
| 1591 |  | 
|---|
| 1592 | /* Create an inode for the EA value */ | 
|---|
| 1593 | ea_inode = ext4_xattr_inode_create(handle, inode, hash); | 
|---|
| 1594 | if (IS_ERR(ptr: ea_inode)) { | 
|---|
| 1595 | ext4_xattr_inode_free_quota(parent: inode, NULL, len: value_len); | 
|---|
| 1596 | return ea_inode; | 
|---|
| 1597 | } | 
|---|
| 1598 |  | 
|---|
| 1599 | err = ext4_xattr_inode_write(handle, ea_inode, buf: value, bufsize: value_len); | 
|---|
| 1600 | if (err) { | 
|---|
| 1601 | if (ext4_xattr_inode_dec_ref(handle, ea_inode)) | 
|---|
| 1602 | ext4_warning_inode(ea_inode, "cleanup dec ref error %d", err); | 
|---|
| 1603 | goto out_err; | 
|---|
| 1604 | } | 
|---|
| 1605 |  | 
|---|
| 1606 | if (EA_INODE_CACHE(inode)) | 
|---|
| 1607 | mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, key: hash, | 
|---|
| 1608 | value: ea_inode->i_ino, reusable: true /* reusable */); | 
|---|
| 1609 | return ea_inode; | 
|---|
| 1610 | out_err: | 
|---|
| 1611 | iput(ea_inode); | 
|---|
| 1612 | ext4_xattr_inode_free_quota(parent: inode, NULL, len: value_len); | 
|---|
| 1613 | return ERR_PTR(error: err); | 
|---|
| 1614 | } | 
|---|
| 1615 |  | 
|---|
| 1616 | /* | 
|---|
| 1617 | * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode | 
|---|
| 1618 | * feature is enabled. | 
|---|
| 1619 | */ | 
|---|
| 1620 | #define EXT4_XATTR_BLOCK_RESERVE(inode)	min(i_blocksize(inode)/8, 1024U) | 
|---|
| 1621 |  | 
|---|
| 1622 | static int ext4_xattr_set_entry(struct ext4_xattr_info *i, | 
|---|
| 1623 | struct ext4_xattr_search *s, | 
|---|
| 1624 | handle_t *handle, struct inode *inode, | 
|---|
| 1625 | struct inode *new_ea_inode, | 
|---|
| 1626 | bool is_block) | 
|---|
| 1627 | { | 
|---|
| 1628 | struct ext4_xattr_entry *last, *next; | 
|---|
| 1629 | struct ext4_xattr_entry *here = s->here; | 
|---|
| 1630 | size_t min_offs = s->end - s->base, name_len = strlen(i->name); | 
|---|
| 1631 | int in_inode = i->in_inode; | 
|---|
| 1632 | struct inode *old_ea_inode = NULL; | 
|---|
| 1633 | size_t old_size, new_size; | 
|---|
| 1634 | int ret; | 
|---|
| 1635 |  | 
|---|
| 1636 | /* Space used by old and new values. */ | 
|---|
| 1637 | old_size = (!s->not_found && !here->e_value_inum) ? | 
|---|
| 1638 | EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0; | 
|---|
| 1639 | new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0; | 
|---|
| 1640 |  | 
|---|
| 1641 | /* | 
|---|
| 1642 | * Optimization for the simple case when old and new values have the | 
|---|
| 1643 | * same padded sizes. Not applicable if external inodes are involved. | 
|---|
| 1644 | */ | 
|---|
| 1645 | if (new_size && new_size == old_size) { | 
|---|
| 1646 | size_t offs = le16_to_cpu(here->e_value_offs); | 
|---|
| 1647 | void *val = s->base + offs; | 
|---|
| 1648 |  | 
|---|
| 1649 | here->e_value_size = cpu_to_le32(i->value_len); | 
|---|
| 1650 | if (i->value == EXT4_ZERO_XATTR_VALUE) { | 
|---|
| 1651 | memset(s: val, c: 0, n: new_size); | 
|---|
| 1652 | } else { | 
|---|
| 1653 | memcpy(to: val, from: i->value, len: i->value_len); | 
|---|
| 1654 | /* Clear padding bytes. */ | 
|---|
| 1655 | memset(s: val + i->value_len, c: 0, n: new_size - i->value_len); | 
|---|
| 1656 | } | 
|---|
| 1657 | goto update_hash; | 
|---|
| 1658 | } | 
|---|
| 1659 |  | 
|---|
| 1660 | /* Compute min_offs and last. */ | 
|---|
| 1661 | last = s->first; | 
|---|
| 1662 | for (; !IS_LAST_ENTRY(last); last = next) { | 
|---|
| 1663 | next = EXT4_XATTR_NEXT(last); | 
|---|
| 1664 | if ((void *)next >= s->end) { | 
|---|
| 1665 | EXT4_ERROR_INODE(inode, "corrupted xattr entries"); | 
|---|
| 1666 | ret = -EFSCORRUPTED; | 
|---|
| 1667 | goto out; | 
|---|
| 1668 | } | 
|---|
| 1669 | if (!last->e_value_inum && last->e_value_size) { | 
|---|
| 1670 | size_t offs = le16_to_cpu(last->e_value_offs); | 
|---|
| 1671 | if (offs < min_offs) | 
|---|
| 1672 | min_offs = offs; | 
|---|
| 1673 | } | 
|---|
| 1674 | } | 
|---|
| 1675 |  | 
|---|
| 1676 | /* Check whether we have enough space. */ | 
|---|
| 1677 | if (i->value) { | 
|---|
| 1678 | size_t free; | 
|---|
| 1679 |  | 
|---|
| 1680 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); | 
|---|
| 1681 | if (!s->not_found) | 
|---|
| 1682 | free += EXT4_XATTR_LEN(name_len) + old_size; | 
|---|
| 1683 |  | 
|---|
| 1684 | if (free < EXT4_XATTR_LEN(name_len) + new_size) { | 
|---|
| 1685 | ret = -ENOSPC; | 
|---|
| 1686 | goto out; | 
|---|
| 1687 | } | 
|---|
| 1688 |  | 
|---|
| 1689 | /* | 
|---|
| 1690 | * If storing the value in an external inode is an option, | 
|---|
| 1691 | * reserve space for xattr entries/names in the external | 
|---|
| 1692 | * attribute block so that a long value does not occupy the | 
|---|
| 1693 | * whole space and prevent further entries being added. | 
|---|
| 1694 | */ | 
|---|
| 1695 | if (ext4_has_feature_ea_inode(sb: inode->i_sb) && | 
|---|
| 1696 | new_size && is_block && | 
|---|
| 1697 | (min_offs + old_size - new_size) < | 
|---|
| 1698 | EXT4_XATTR_BLOCK_RESERVE(inode)) { | 
|---|
| 1699 | ret = -ENOSPC; | 
|---|
| 1700 | goto out; | 
|---|
| 1701 | } | 
|---|
| 1702 | } | 
|---|
| 1703 |  | 
|---|
| 1704 | /* | 
|---|
| 1705 | * Getting access to old and new ea inodes is subject to failures. | 
|---|
| 1706 | * Finish that work before doing any modifications to the xattr data. | 
|---|
| 1707 | */ | 
|---|
| 1708 | if (!s->not_found && here->e_value_inum) { | 
|---|
| 1709 | ret = ext4_xattr_inode_iget(parent: inode, | 
|---|
| 1710 | le32_to_cpu(here->e_value_inum), | 
|---|
| 1711 | le32_to_cpu(here->e_hash), | 
|---|
| 1712 | ea_inode: &old_ea_inode); | 
|---|
| 1713 | if (ret) { | 
|---|
| 1714 | old_ea_inode = NULL; | 
|---|
| 1715 | goto out; | 
|---|
| 1716 | } | 
|---|
| 1717 |  | 
|---|
| 1718 | /* We are ready to release ref count on the old_ea_inode. */ | 
|---|
| 1719 | ret = ext4_xattr_inode_dec_ref(handle, ea_inode: old_ea_inode); | 
|---|
| 1720 | if (ret) | 
|---|
| 1721 | goto out; | 
|---|
| 1722 |  | 
|---|
| 1723 | ext4_xattr_inode_free_quota(parent: inode, ea_inode: old_ea_inode, | 
|---|
| 1724 | le32_to_cpu(here->e_value_size)); | 
|---|
| 1725 | } | 
|---|
| 1726 |  | 
|---|
| 1727 | /* No failures allowed past this point. */ | 
|---|
| 1728 |  | 
|---|
| 1729 | if (!s->not_found && here->e_value_size && !here->e_value_inum) { | 
|---|
| 1730 | /* Remove the old value. */ | 
|---|
| 1731 | void *first_val = s->base + min_offs; | 
|---|
| 1732 | size_t offs = le16_to_cpu(here->e_value_offs); | 
|---|
| 1733 | void *val = s->base + offs; | 
|---|
| 1734 |  | 
|---|
| 1735 | memmove(dest: first_val + old_size, src: first_val, count: val - first_val); | 
|---|
| 1736 | memset(s: first_val, c: 0, n: old_size); | 
|---|
| 1737 | min_offs += old_size; | 
|---|
| 1738 |  | 
|---|
| 1739 | /* Adjust all value offsets. */ | 
|---|
| 1740 | last = s->first; | 
|---|
| 1741 | while (!IS_LAST_ENTRY(last)) { | 
|---|
| 1742 | size_t o = le16_to_cpu(last->e_value_offs); | 
|---|
| 1743 |  | 
|---|
| 1744 | if (!last->e_value_inum && | 
|---|
| 1745 | last->e_value_size && o < offs) | 
|---|
| 1746 | last->e_value_offs = cpu_to_le16(o + old_size); | 
|---|
| 1747 | last = EXT4_XATTR_NEXT(last); | 
|---|
| 1748 | } | 
|---|
| 1749 | } | 
|---|
| 1750 |  | 
|---|
| 1751 | if (!i->value) { | 
|---|
| 1752 | /* Remove old name. */ | 
|---|
| 1753 | size_t size = EXT4_XATTR_LEN(name_len); | 
|---|
| 1754 |  | 
|---|
| 1755 | last = ENTRY((void *)last - size); | 
|---|
| 1756 | memmove(dest: here, src: (void *)here + size, | 
|---|
| 1757 | count: (void *)last - (void *)here + sizeof(__u32)); | 
|---|
| 1758 | memset(s: last, c: 0, n: size); | 
|---|
| 1759 |  | 
|---|
| 1760 | /* | 
|---|
| 1761 | * Update i_inline_off - moved ibody region might contain | 
|---|
| 1762 | * system.data attribute.  Handling a failure here won't | 
|---|
| 1763 | * cause other complications for setting an xattr. | 
|---|
| 1764 | */ | 
|---|
| 1765 | if (!is_block && ext4_has_inline_data(inode)) { | 
|---|
| 1766 | ret = ext4_find_inline_data_nolock(inode); | 
|---|
| 1767 | if (ret) { | 
|---|
| 1768 | ext4_warning_inode(inode, | 
|---|
| 1769 | "unable to update i_inline_off"); | 
|---|
| 1770 | goto out; | 
|---|
| 1771 | } | 
|---|
| 1772 | } | 
|---|
| 1773 | } else if (s->not_found) { | 
|---|
| 1774 | /* Insert new name. */ | 
|---|
| 1775 | size_t size = EXT4_XATTR_LEN(name_len); | 
|---|
| 1776 | size_t rest = (void *)last - (void *)here + sizeof(__u32); | 
|---|
| 1777 |  | 
|---|
| 1778 | memmove(dest: (void *)here + size, src: here, count: rest); | 
|---|
| 1779 | memset(s: here, c: 0, n: size); | 
|---|
| 1780 | here->e_name_index = i->name_index; | 
|---|
| 1781 | here->e_name_len = name_len; | 
|---|
| 1782 | memcpy(to: here->e_name, from: i->name, len: name_len); | 
|---|
| 1783 | } else { | 
|---|
| 1784 | /* This is an update, reset value info. */ | 
|---|
| 1785 | here->e_value_inum = 0; | 
|---|
| 1786 | here->e_value_offs = 0; | 
|---|
| 1787 | here->e_value_size = 0; | 
|---|
| 1788 | } | 
|---|
| 1789 |  | 
|---|
| 1790 | if (i->value) { | 
|---|
| 1791 | /* Insert new value. */ | 
|---|
| 1792 | if (in_inode) { | 
|---|
| 1793 | here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino); | 
|---|
| 1794 | } else if (i->value_len) { | 
|---|
| 1795 | void *val = s->base + min_offs - new_size; | 
|---|
| 1796 |  | 
|---|
| 1797 | here->e_value_offs = cpu_to_le16(min_offs - new_size); | 
|---|
| 1798 | if (i->value == EXT4_ZERO_XATTR_VALUE) { | 
|---|
| 1799 | memset(s: val, c: 0, n: new_size); | 
|---|
| 1800 | } else { | 
|---|
| 1801 | memcpy(to: val, from: i->value, len: i->value_len); | 
|---|
| 1802 | /* Clear padding bytes. */ | 
|---|
| 1803 | memset(s: val + i->value_len, c: 0, | 
|---|
| 1804 | n: new_size - i->value_len); | 
|---|
| 1805 | } | 
|---|
| 1806 | } | 
|---|
| 1807 | here->e_value_size = cpu_to_le32(i->value_len); | 
|---|
| 1808 | } | 
|---|
| 1809 |  | 
|---|
| 1810 | update_hash: | 
|---|
| 1811 | if (i->value) { | 
|---|
| 1812 | __le32 hash = 0; | 
|---|
| 1813 |  | 
|---|
| 1814 | /* Entry hash calculation. */ | 
|---|
| 1815 | if (in_inode) { | 
|---|
| 1816 | __le32 crc32c_hash; | 
|---|
| 1817 |  | 
|---|
| 1818 | /* | 
|---|
| 1819 | * Feed crc32c hash instead of the raw value for entry | 
|---|
| 1820 | * hash calculation. This is to avoid walking | 
|---|
| 1821 | * potentially long value buffer again. | 
|---|
| 1822 | */ | 
|---|
| 1823 | crc32c_hash = cpu_to_le32( | 
|---|
| 1824 | ext4_xattr_inode_get_hash(new_ea_inode)); | 
|---|
| 1825 | hash = ext4_xattr_hash_entry(name: here->e_name, | 
|---|
| 1826 | name_len: here->e_name_len, | 
|---|
| 1827 | value: &crc32c_hash, value_count: 1); | 
|---|
| 1828 | } else if (is_block) { | 
|---|
| 1829 | __le32 *value = s->base + le16_to_cpu( | 
|---|
| 1830 | here->e_value_offs); | 
|---|
| 1831 |  | 
|---|
| 1832 | hash = ext4_xattr_hash_entry(name: here->e_name, | 
|---|
| 1833 | name_len: here->e_name_len, value, | 
|---|
| 1834 | value_count: new_size >> 2); | 
|---|
| 1835 | } | 
|---|
| 1836 | here->e_hash = hash; | 
|---|
| 1837 | } | 
|---|
| 1838 |  | 
|---|
| 1839 | if (is_block) | 
|---|
| 1840 | ext4_xattr_rehash((struct ext4_xattr_header *)s->base); | 
|---|
| 1841 |  | 
|---|
| 1842 | ret = 0; | 
|---|
| 1843 | out: | 
|---|
| 1844 | iput(old_ea_inode); | 
|---|
| 1845 | return ret; | 
|---|
| 1846 | } | 
|---|
| 1847 |  | 
|---|
| 1848 | struct ext4_xattr_block_find { | 
|---|
| 1849 | struct ext4_xattr_search s; | 
|---|
| 1850 | struct buffer_head *bh; | 
|---|
| 1851 | }; | 
|---|
| 1852 |  | 
|---|
| 1853 | static int | 
|---|
| 1854 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, | 
|---|
| 1855 | struct ext4_xattr_block_find *bs) | 
|---|
| 1856 | { | 
|---|
| 1857 | struct super_block *sb = inode->i_sb; | 
|---|
| 1858 | int error; | 
|---|
| 1859 |  | 
|---|
| 1860 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", | 
|---|
| 1861 | i->name_index, i->name, i->value, (long)i->value_len); | 
|---|
| 1862 |  | 
|---|
| 1863 | if (EXT4_I(inode)->i_file_acl) { | 
|---|
| 1864 | /* The inode already has an extended attribute block. */ | 
|---|
| 1865 | bs->bh = ext4_sb_bread(sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 1866 | if (IS_ERR(ptr: bs->bh)) { | 
|---|
| 1867 | error = PTR_ERR(ptr: bs->bh); | 
|---|
| 1868 | bs->bh = NULL; | 
|---|
| 1869 | return error; | 
|---|
| 1870 | } | 
|---|
| 1871 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", | 
|---|
| 1872 | atomic_read(&(bs->bh->b_count)), | 
|---|
| 1873 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); | 
|---|
| 1874 | error = ext4_xattr_check_block(inode, bs->bh); | 
|---|
| 1875 | if (error) | 
|---|
| 1876 | return error; | 
|---|
| 1877 | /* Find the named attribute. */ | 
|---|
| 1878 | bs->s.base = BHDR(bs->bh); | 
|---|
| 1879 | bs->s.first = BFIRST(bs->bh); | 
|---|
| 1880 | bs->s.end = bs->bh->b_data + bs->bh->b_size; | 
|---|
| 1881 | bs->s.here = bs->s.first; | 
|---|
| 1882 | error = xattr_find_entry(inode, pentry: &bs->s.here, end: bs->s.end, | 
|---|
| 1883 | name_index: i->name_index, name: i->name, sorted: 1); | 
|---|
| 1884 | if (error && error != -ENODATA) | 
|---|
| 1885 | return error; | 
|---|
| 1886 | bs->s.not_found = error; | 
|---|
| 1887 | } | 
|---|
| 1888 | return 0; | 
|---|
| 1889 | } | 
|---|
| 1890 |  | 
|---|
| 1891 | static int | 
|---|
| 1892 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, | 
|---|
| 1893 | struct ext4_xattr_info *i, | 
|---|
| 1894 | struct ext4_xattr_block_find *bs) | 
|---|
| 1895 | { | 
|---|
| 1896 | struct super_block *sb = inode->i_sb; | 
|---|
| 1897 | struct buffer_head *new_bh = NULL; | 
|---|
| 1898 | struct ext4_xattr_search s_copy = bs->s; | 
|---|
| 1899 | struct ext4_xattr_search *s = &s_copy; | 
|---|
| 1900 | struct mb_cache_entry *ce = NULL; | 
|---|
| 1901 | int error = 0; | 
|---|
| 1902 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); | 
|---|
| 1903 | struct inode *ea_inode = NULL, *tmp_inode; | 
|---|
| 1904 | size_t old_ea_inode_quota = 0; | 
|---|
| 1905 | unsigned int ea_ino; | 
|---|
| 1906 |  | 
|---|
| 1907 | #define (x) ((struct ext4_xattr_header *)(x)) | 
|---|
| 1908 |  | 
|---|
| 1909 | /* If we need EA inode, prepare it before locking the buffer */ | 
|---|
| 1910 | if (i->value && i->in_inode) { | 
|---|
| 1911 | WARN_ON_ONCE(!i->value_len); | 
|---|
| 1912 |  | 
|---|
| 1913 | ea_inode = ext4_xattr_inode_lookup_create(handle, inode, | 
|---|
| 1914 | value: i->value, value_len: i->value_len); | 
|---|
| 1915 | if (IS_ERR(ptr: ea_inode)) { | 
|---|
| 1916 | error = PTR_ERR(ptr: ea_inode); | 
|---|
| 1917 | ea_inode = NULL; | 
|---|
| 1918 | goto cleanup; | 
|---|
| 1919 | } | 
|---|
| 1920 | } | 
|---|
| 1921 |  | 
|---|
| 1922 | if (s->base) { | 
|---|
| 1923 | int offset = (char *)s->here - bs->bh->b_data; | 
|---|
| 1924 |  | 
|---|
| 1925 | BUFFER_TRACE(bs->bh, "get_write_access"); | 
|---|
| 1926 | error = ext4_journal_get_write_access(handle, sb, bs->bh, | 
|---|
| 1927 | EXT4_JTR_NONE); | 
|---|
| 1928 | if (error) | 
|---|
| 1929 | goto cleanup; | 
|---|
| 1930 |  | 
|---|
| 1931 | lock_buffer(bh: bs->bh); | 
|---|
| 1932 |  | 
|---|
| 1933 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { | 
|---|
| 1934 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); | 
|---|
| 1935 |  | 
|---|
| 1936 | /* | 
|---|
| 1937 | * This must happen under buffer lock for | 
|---|
| 1938 | * ext4_xattr_block_set() to reliably detect modified | 
|---|
| 1939 | * block | 
|---|
| 1940 | */ | 
|---|
| 1941 | if (ea_block_cache) { | 
|---|
| 1942 | struct mb_cache_entry *oe; | 
|---|
| 1943 |  | 
|---|
| 1944 | oe = mb_cache_entry_delete_or_get(cache: ea_block_cache, | 
|---|
| 1945 | key: hash, value: bs->bh->b_blocknr); | 
|---|
| 1946 | if (oe) { | 
|---|
| 1947 | /* | 
|---|
| 1948 | * Xattr block is getting reused. Leave | 
|---|
| 1949 | * it alone. | 
|---|
| 1950 | */ | 
|---|
| 1951 | mb_cache_entry_put(cache: ea_block_cache, entry: oe); | 
|---|
| 1952 | goto clone_block; | 
|---|
| 1953 | } | 
|---|
| 1954 | } | 
|---|
| 1955 | ea_bdebug(bs->bh, "modifying in-place"); | 
|---|
| 1956 | error = ext4_xattr_set_entry(i, s, handle, inode, | 
|---|
| 1957 | new_ea_inode: ea_inode, is_block: true /* is_block */); | 
|---|
| 1958 | ext4_xattr_block_csum_set(inode, bh: bs->bh); | 
|---|
| 1959 | unlock_buffer(bh: bs->bh); | 
|---|
| 1960 | if (error == -EFSCORRUPTED) | 
|---|
| 1961 | goto bad_block; | 
|---|
| 1962 | if (!error) | 
|---|
| 1963 | error = ext4_handle_dirty_metadata(handle, | 
|---|
| 1964 | inode, | 
|---|
| 1965 | bs->bh); | 
|---|
| 1966 | if (error) | 
|---|
| 1967 | goto cleanup; | 
|---|
| 1968 | goto inserted; | 
|---|
| 1969 | } | 
|---|
| 1970 | clone_block: | 
|---|
| 1971 | unlock_buffer(bh: bs->bh); | 
|---|
| 1972 | ea_bdebug(bs->bh, "cloning"); | 
|---|
| 1973 | s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS); | 
|---|
| 1974 | error = -ENOMEM; | 
|---|
| 1975 | if (s->base == NULL) | 
|---|
| 1976 | goto cleanup; | 
|---|
| 1977 | s->first = ENTRY(header(s->base)+1); | 
|---|
| 1978 | header(s->base)->h_refcount = cpu_to_le32(1); | 
|---|
| 1979 | s->here = ENTRY(s->base + offset); | 
|---|
| 1980 | s->end = s->base + bs->bh->b_size; | 
|---|
| 1981 |  | 
|---|
| 1982 | /* | 
|---|
| 1983 | * If existing entry points to an xattr inode, we need | 
|---|
| 1984 | * to prevent ext4_xattr_set_entry() from decrementing | 
|---|
| 1985 | * ref count on it because the reference belongs to the | 
|---|
| 1986 | * original block. In this case, make the entry look | 
|---|
| 1987 | * like it has an empty value. | 
|---|
| 1988 | */ | 
|---|
| 1989 | if (!s->not_found && s->here->e_value_inum) { | 
|---|
| 1990 | ea_ino = le32_to_cpu(s->here->e_value_inum); | 
|---|
| 1991 | error = ext4_xattr_inode_iget(parent: inode, ea_ino, | 
|---|
| 1992 | le32_to_cpu(s->here->e_hash), | 
|---|
| 1993 | ea_inode: &tmp_inode); | 
|---|
| 1994 | if (error) | 
|---|
| 1995 | goto cleanup; | 
|---|
| 1996 |  | 
|---|
| 1997 | if (!ext4_test_inode_state(inode: tmp_inode, | 
|---|
| 1998 | bit: EXT4_STATE_LUSTRE_EA_INODE)) { | 
|---|
| 1999 | /* | 
|---|
| 2000 | * Defer quota free call for previous | 
|---|
| 2001 | * inode until success is guaranteed. | 
|---|
| 2002 | */ | 
|---|
| 2003 | old_ea_inode_quota = le32_to_cpu( | 
|---|
| 2004 | s->here->e_value_size); | 
|---|
| 2005 | } | 
|---|
| 2006 | iput(tmp_inode); | 
|---|
| 2007 |  | 
|---|
| 2008 | s->here->e_value_inum = 0; | 
|---|
| 2009 | s->here->e_value_size = 0; | 
|---|
| 2010 | } | 
|---|
| 2011 | } else { | 
|---|
| 2012 | /* Allocate a buffer where we construct the new block. */ | 
|---|
| 2013 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); | 
|---|
| 2014 | error = -ENOMEM; | 
|---|
| 2015 | if (s->base == NULL) | 
|---|
| 2016 | goto cleanup; | 
|---|
| 2017 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | 
|---|
| 2018 | header(s->base)->h_blocks = cpu_to_le32(1); | 
|---|
| 2019 | header(s->base)->h_refcount = cpu_to_le32(1); | 
|---|
| 2020 | s->first = ENTRY(header(s->base)+1); | 
|---|
| 2021 | s->here = ENTRY(header(s->base)+1); | 
|---|
| 2022 | s->end = s->base + sb->s_blocksize; | 
|---|
| 2023 | } | 
|---|
| 2024 |  | 
|---|
| 2025 | error = ext4_xattr_set_entry(i, s, handle, inode, new_ea_inode: ea_inode, | 
|---|
| 2026 | is_block: true /* is_block */); | 
|---|
| 2027 | if (error == -EFSCORRUPTED) | 
|---|
| 2028 | goto bad_block; | 
|---|
| 2029 | if (error) | 
|---|
| 2030 | goto cleanup; | 
|---|
| 2031 |  | 
|---|
| 2032 | inserted: | 
|---|
| 2033 | if (!IS_LAST_ENTRY(s->first)) { | 
|---|
| 2034 | new_bh = ext4_xattr_block_cache_find(inode, header(s->base), &ce); | 
|---|
| 2035 | if (IS_ERR(ptr: new_bh)) { | 
|---|
| 2036 | error = PTR_ERR(ptr: new_bh); | 
|---|
| 2037 | new_bh = NULL; | 
|---|
| 2038 | goto cleanup; | 
|---|
| 2039 | } | 
|---|
| 2040 |  | 
|---|
| 2041 | if (new_bh) { | 
|---|
| 2042 | /* We found an identical block in the cache. */ | 
|---|
| 2043 | if (new_bh == bs->bh) | 
|---|
| 2044 | ea_bdebug(new_bh, "keeping"); | 
|---|
| 2045 | else { | 
|---|
| 2046 | u32 ref; | 
|---|
| 2047 |  | 
|---|
| 2048 | #ifdef EXT4_XATTR_DEBUG | 
|---|
| 2049 | WARN_ON_ONCE(dquot_initialize_needed(inode)); | 
|---|
| 2050 | #endif | 
|---|
| 2051 | /* The old block is released after updating | 
|---|
| 2052 | the inode. */ | 
|---|
| 2053 | error = dquot_alloc_block(inode, | 
|---|
| 2054 | EXT4_C2B(EXT4_SB(sb), 1)); | 
|---|
| 2055 | if (error) | 
|---|
| 2056 | goto cleanup; | 
|---|
| 2057 | BUFFER_TRACE(new_bh, "get_write_access"); | 
|---|
| 2058 | error = ext4_journal_get_write_access( | 
|---|
| 2059 | handle, sb, new_bh, | 
|---|
| 2060 | EXT4_JTR_NONE); | 
|---|
| 2061 | if (error) | 
|---|
| 2062 | goto cleanup_dquot; | 
|---|
| 2063 | lock_buffer(bh: new_bh); | 
|---|
| 2064 | /* | 
|---|
| 2065 | * We have to be careful about races with | 
|---|
| 2066 | * adding references to xattr block. Once we | 
|---|
| 2067 | * hold buffer lock xattr block's state is | 
|---|
| 2068 | * stable so we can check the additional | 
|---|
| 2069 | * reference fits. | 
|---|
| 2070 | */ | 
|---|
| 2071 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; | 
|---|
| 2072 | if (ref > EXT4_XATTR_REFCOUNT_MAX) { | 
|---|
| 2073 | /* | 
|---|
| 2074 | * Undo everything and check mbcache | 
|---|
| 2075 | * again. | 
|---|
| 2076 | */ | 
|---|
| 2077 | unlock_buffer(bh: new_bh); | 
|---|
| 2078 | dquot_free_block(inode, | 
|---|
| 2079 | EXT4_C2B(EXT4_SB(sb), | 
|---|
| 2080 | 1)); | 
|---|
| 2081 | brelse(bh: new_bh); | 
|---|
| 2082 | mb_cache_entry_put(cache: ea_block_cache, entry: ce); | 
|---|
| 2083 | ce = NULL; | 
|---|
| 2084 | new_bh = NULL; | 
|---|
| 2085 | goto inserted; | 
|---|
| 2086 | } | 
|---|
| 2087 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); | 
|---|
| 2088 | if (ref == EXT4_XATTR_REFCOUNT_MAX) | 
|---|
| 2089 | clear_bit(nr: MBE_REUSABLE_B, addr: &ce->e_flags); | 
|---|
| 2090 | ea_bdebug(new_bh, "reusing; refcount now=%d", | 
|---|
| 2091 | ref); | 
|---|
| 2092 | ext4_xattr_block_csum_set(inode, bh: new_bh); | 
|---|
| 2093 | unlock_buffer(bh: new_bh); | 
|---|
| 2094 | error = ext4_handle_dirty_metadata(handle, | 
|---|
| 2095 | inode, | 
|---|
| 2096 | new_bh); | 
|---|
| 2097 | if (error) | 
|---|
| 2098 | goto cleanup_dquot; | 
|---|
| 2099 | } | 
|---|
| 2100 | mb_cache_entry_touch(cache: ea_block_cache, entry: ce); | 
|---|
| 2101 | mb_cache_entry_put(cache: ea_block_cache, entry: ce); | 
|---|
| 2102 | ce = NULL; | 
|---|
| 2103 | } else if (bs->bh && s->base == bs->bh->b_data) { | 
|---|
| 2104 | /* We were modifying this block in-place. */ | 
|---|
| 2105 | ea_bdebug(bs->bh, "keeping this block"); | 
|---|
| 2106 | ext4_xattr_block_cache_insert(ea_block_cache, bs->bh); | 
|---|
| 2107 | new_bh = bs->bh; | 
|---|
| 2108 | get_bh(bh: new_bh); | 
|---|
| 2109 | } else { | 
|---|
| 2110 | /* We need to allocate a new block */ | 
|---|
| 2111 | ext4_fsblk_t goal, block; | 
|---|
| 2112 |  | 
|---|
| 2113 | #ifdef EXT4_XATTR_DEBUG | 
|---|
| 2114 | WARN_ON_ONCE(dquot_initialize_needed(inode)); | 
|---|
| 2115 | #endif | 
|---|
| 2116 | goal = ext4_group_first_block_no(sb, | 
|---|
| 2117 | group_no: EXT4_I(inode)->i_block_group); | 
|---|
| 2118 | block = ext4_new_meta_blocks(handle, inode, goal, flags: 0, | 
|---|
| 2119 | NULL, errp: &error); | 
|---|
| 2120 | if (error) | 
|---|
| 2121 | goto cleanup; | 
|---|
| 2122 |  | 
|---|
| 2123 | ea_idebug(inode, "creating block %llu", | 
|---|
| 2124 | (unsigned long long)block); | 
|---|
| 2125 |  | 
|---|
| 2126 | new_bh = sb_getblk(sb, block); | 
|---|
| 2127 | if (unlikely(!new_bh)) { | 
|---|
| 2128 | error = -ENOMEM; | 
|---|
| 2129 | getblk_failed: | 
|---|
| 2130 | ext4_free_blocks(handle, inode, NULL, block, count: 1, | 
|---|
| 2131 | EXT4_FREE_BLOCKS_METADATA); | 
|---|
| 2132 | goto cleanup; | 
|---|
| 2133 | } | 
|---|
| 2134 | error = ext4_xattr_inode_inc_ref_all(handle, parent: inode, | 
|---|
| 2135 | ENTRY(header(s->base)+1)); | 
|---|
| 2136 | if (error) | 
|---|
| 2137 | goto getblk_failed; | 
|---|
| 2138 | if (ea_inode) { | 
|---|
| 2139 | /* Drop the extra ref on ea_inode. */ | 
|---|
| 2140 | error = ext4_xattr_inode_dec_ref(handle, | 
|---|
| 2141 | ea_inode); | 
|---|
| 2142 | if (error) | 
|---|
| 2143 | ext4_warning_inode(ea_inode, | 
|---|
| 2144 | "dec ref error=%d", | 
|---|
| 2145 | error); | 
|---|
| 2146 | iput(ea_inode); | 
|---|
| 2147 | ea_inode = NULL; | 
|---|
| 2148 | } | 
|---|
| 2149 |  | 
|---|
| 2150 | lock_buffer(bh: new_bh); | 
|---|
| 2151 | error = ext4_journal_get_create_access(handle, sb, | 
|---|
| 2152 | new_bh, EXT4_JTR_NONE); | 
|---|
| 2153 | if (error) { | 
|---|
| 2154 | unlock_buffer(bh: new_bh); | 
|---|
| 2155 | error = -EIO; | 
|---|
| 2156 | goto getblk_failed; | 
|---|
| 2157 | } | 
|---|
| 2158 | memcpy(to: new_bh->b_data, from: s->base, len: new_bh->b_size); | 
|---|
| 2159 | ext4_xattr_block_csum_set(inode, bh: new_bh); | 
|---|
| 2160 | set_buffer_uptodate(new_bh); | 
|---|
| 2161 | unlock_buffer(bh: new_bh); | 
|---|
| 2162 | ext4_xattr_block_cache_insert(ea_block_cache, new_bh); | 
|---|
| 2163 | error = ext4_handle_dirty_metadata(handle, inode, | 
|---|
| 2164 | new_bh); | 
|---|
| 2165 | if (error) | 
|---|
| 2166 | goto cleanup; | 
|---|
| 2167 | } | 
|---|
| 2168 | } | 
|---|
| 2169 |  | 
|---|
| 2170 | if (old_ea_inode_quota) | 
|---|
| 2171 | ext4_xattr_inode_free_quota(parent: inode, NULL, len: old_ea_inode_quota); | 
|---|
| 2172 |  | 
|---|
| 2173 | /* Update the inode. */ | 
|---|
| 2174 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; | 
|---|
| 2175 |  | 
|---|
| 2176 | /* Drop the previous xattr block. */ | 
|---|
| 2177 | if (bs->bh && bs->bh != new_bh) { | 
|---|
| 2178 | struct ext4_xattr_inode_array *ea_inode_array = NULL; | 
|---|
| 2179 |  | 
|---|
| 2180 | ext4_xattr_release_block(handle, inode, bh: bs->bh, | 
|---|
| 2181 | ea_inode_array: &ea_inode_array, | 
|---|
| 2182 | extra_credits: 0 /* extra_credits */); | 
|---|
| 2183 | ext4_xattr_inode_array_free(array: ea_inode_array); | 
|---|
| 2184 | } | 
|---|
| 2185 | error = 0; | 
|---|
| 2186 |  | 
|---|
| 2187 | cleanup: | 
|---|
| 2188 | if (ea_inode) { | 
|---|
| 2189 | if (error) { | 
|---|
| 2190 | int error2; | 
|---|
| 2191 |  | 
|---|
| 2192 | error2 = ext4_xattr_inode_dec_ref(handle, ea_inode); | 
|---|
| 2193 | if (error2) | 
|---|
| 2194 | ext4_warning_inode(ea_inode, "dec ref error=%d", | 
|---|
| 2195 | error2); | 
|---|
| 2196 | ext4_xattr_inode_free_quota(parent: inode, ea_inode, | 
|---|
| 2197 | len: i_size_read(inode: ea_inode)); | 
|---|
| 2198 | } | 
|---|
| 2199 | iput(ea_inode); | 
|---|
| 2200 | } | 
|---|
| 2201 | if (ce) | 
|---|
| 2202 | mb_cache_entry_put(cache: ea_block_cache, entry: ce); | 
|---|
| 2203 | brelse(bh: new_bh); | 
|---|
| 2204 | if (!(bs->bh && s->base == bs->bh->b_data)) | 
|---|
| 2205 | kfree(objp: s->base); | 
|---|
| 2206 |  | 
|---|
| 2207 | return error; | 
|---|
| 2208 |  | 
|---|
| 2209 | cleanup_dquot: | 
|---|
| 2210 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); | 
|---|
| 2211 | goto cleanup; | 
|---|
| 2212 |  | 
|---|
| 2213 | bad_block: | 
|---|
| 2214 | EXT4_ERROR_INODE(inode, "bad block %llu", | 
|---|
| 2215 | EXT4_I(inode)->i_file_acl); | 
|---|
| 2216 | goto cleanup; | 
|---|
| 2217 |  | 
|---|
| 2218 | #undef header | 
|---|
| 2219 | } | 
|---|
| 2220 |  | 
|---|
| 2221 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, | 
|---|
| 2222 | struct ext4_xattr_ibody_find *is) | 
|---|
| 2223 | { | 
|---|
| 2224 | struct ext4_xattr_ibody_header *; | 
|---|
| 2225 | struct ext4_inode *raw_inode; | 
|---|
| 2226 | int error; | 
|---|
| 2227 |  | 
|---|
| 2228 | if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) | 
|---|
| 2229 | return 0; | 
|---|
| 2230 |  | 
|---|
| 2231 | raw_inode = ext4_raw_inode(iloc: &is->iloc); | 
|---|
| 2232 | header = IHDR(inode, raw_inode); | 
|---|
| 2233 | is->s.base = is->s.first = IFIRST(header); | 
|---|
| 2234 | is->s.here = is->s.first; | 
|---|
| 2235 | is->s.end = ITAIL(inode, raw_inode); | 
|---|
| 2236 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR)) { | 
|---|
| 2237 | /* Find the named attribute. */ | 
|---|
| 2238 | error = xattr_find_entry(inode, pentry: &is->s.here, end: is->s.end, | 
|---|
| 2239 | name_index: i->name_index, name: i->name, sorted: 0); | 
|---|
| 2240 | if (error && error != -ENODATA) | 
|---|
| 2241 | return error; | 
|---|
| 2242 | is->s.not_found = error; | 
|---|
| 2243 | } | 
|---|
| 2244 | return 0; | 
|---|
| 2245 | } | 
|---|
| 2246 |  | 
|---|
| 2247 | int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, | 
|---|
| 2248 | struct ext4_xattr_info *i, | 
|---|
| 2249 | struct ext4_xattr_ibody_find *is) | 
|---|
| 2250 | { | 
|---|
| 2251 | struct ext4_xattr_ibody_header *; | 
|---|
| 2252 | struct ext4_xattr_search *s = &is->s; | 
|---|
| 2253 | struct inode *ea_inode = NULL; | 
|---|
| 2254 | int error; | 
|---|
| 2255 |  | 
|---|
| 2256 | if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) | 
|---|
| 2257 | return -ENOSPC; | 
|---|
| 2258 |  | 
|---|
| 2259 | /* If we need EA inode, prepare it before locking the buffer */ | 
|---|
| 2260 | if (i->value && i->in_inode) { | 
|---|
| 2261 | WARN_ON_ONCE(!i->value_len); | 
|---|
| 2262 |  | 
|---|
| 2263 | ea_inode = ext4_xattr_inode_lookup_create(handle, inode, | 
|---|
| 2264 | value: i->value, value_len: i->value_len); | 
|---|
| 2265 | if (IS_ERR(ptr: ea_inode)) | 
|---|
| 2266 | return PTR_ERR(ptr: ea_inode); | 
|---|
| 2267 | } | 
|---|
| 2268 | error = ext4_xattr_set_entry(i, s, handle, inode, new_ea_inode: ea_inode, | 
|---|
| 2269 | is_block: false /* is_block */); | 
|---|
| 2270 | if (error) { | 
|---|
| 2271 | if (ea_inode) { | 
|---|
| 2272 | int error2; | 
|---|
| 2273 |  | 
|---|
| 2274 | error2 = ext4_xattr_inode_dec_ref(handle, ea_inode); | 
|---|
| 2275 | if (error2) | 
|---|
| 2276 | ext4_warning_inode(ea_inode, "dec ref error=%d", | 
|---|
| 2277 | error2); | 
|---|
| 2278 |  | 
|---|
| 2279 | ext4_xattr_inode_free_quota(parent: inode, ea_inode, | 
|---|
| 2280 | len: i_size_read(inode: ea_inode)); | 
|---|
| 2281 | iput(ea_inode); | 
|---|
| 2282 | } | 
|---|
| 2283 | return error; | 
|---|
| 2284 | } | 
|---|
| 2285 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); | 
|---|
| 2286 | if (!IS_LAST_ENTRY(s->first)) { | 
|---|
| 2287 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); | 
|---|
| 2288 | ext4_set_inode_state(inode, bit: EXT4_STATE_XATTR); | 
|---|
| 2289 | } else { | 
|---|
| 2290 | header->h_magic = cpu_to_le32(0); | 
|---|
| 2291 | ext4_clear_inode_state(inode, bit: EXT4_STATE_XATTR); | 
|---|
| 2292 | } | 
|---|
| 2293 | iput(ea_inode); | 
|---|
| 2294 | return 0; | 
|---|
| 2295 | } | 
|---|
| 2296 |  | 
|---|
| 2297 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, | 
|---|
| 2298 | struct ext4_xattr_info *i) | 
|---|
| 2299 | { | 
|---|
| 2300 | void *value; | 
|---|
| 2301 |  | 
|---|
| 2302 | /* When e_value_inum is set the value is stored externally. */ | 
|---|
| 2303 | if (s->here->e_value_inum) | 
|---|
| 2304 | return 0; | 
|---|
| 2305 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) | 
|---|
| 2306 | return 0; | 
|---|
| 2307 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); | 
|---|
| 2308 | return !memcmp(value, i->value, i->value_len); | 
|---|
| 2309 | } | 
|---|
| 2310 |  | 
|---|
| 2311 | static struct buffer_head *ext4_xattr_get_block(struct inode *inode) | 
|---|
| 2312 | { | 
|---|
| 2313 | struct buffer_head *bh; | 
|---|
| 2314 | int error; | 
|---|
| 2315 |  | 
|---|
| 2316 | if (!EXT4_I(inode)->i_file_acl) | 
|---|
| 2317 | return NULL; | 
|---|
| 2318 | bh = ext4_sb_bread(sb: inode->i_sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 2319 | if (IS_ERR(ptr: bh)) | 
|---|
| 2320 | return bh; | 
|---|
| 2321 | error = ext4_xattr_check_block(inode, bh); | 
|---|
| 2322 | if (error) { | 
|---|
| 2323 | brelse(bh); | 
|---|
| 2324 | return ERR_PTR(error); | 
|---|
| 2325 | } | 
|---|
| 2326 | return bh; | 
|---|
| 2327 | } | 
|---|
| 2328 |  | 
|---|
| 2329 | /* | 
|---|
| 2330 | * ext4_xattr_set_handle() | 
|---|
| 2331 | * | 
|---|
| 2332 | * Create, replace or remove an extended attribute for this inode.  Value | 
|---|
| 2333 | * is NULL to remove an existing extended attribute, and non-NULL to | 
|---|
| 2334 | * either replace an existing extended attribute, or create a new extended | 
|---|
| 2335 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE | 
|---|
| 2336 | * specify that an extended attribute must exist and must not exist | 
|---|
| 2337 | * previous to the call, respectively. | 
|---|
| 2338 | * | 
|---|
| 2339 | * Returns 0, or a negative error number on failure. | 
|---|
| 2340 | */ | 
|---|
| 2341 | int | 
|---|
| 2342 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, | 
|---|
| 2343 | const char *name, const void *value, size_t value_len, | 
|---|
| 2344 | int flags) | 
|---|
| 2345 | { | 
|---|
| 2346 | struct ext4_xattr_info i = { | 
|---|
| 2347 | .name_index = name_index, | 
|---|
| 2348 | .name = name, | 
|---|
| 2349 | .value = value, | 
|---|
| 2350 | .value_len = value_len, | 
|---|
| 2351 | .in_inode = 0, | 
|---|
| 2352 | }; | 
|---|
| 2353 | struct ext4_xattr_ibody_find is = { | 
|---|
| 2354 | .s = { .not_found = -ENODATA, }, | 
|---|
| 2355 | }; | 
|---|
| 2356 | struct ext4_xattr_block_find bs = { | 
|---|
| 2357 | .s = { .not_found = -ENODATA, }, | 
|---|
| 2358 | }; | 
|---|
| 2359 | int no_expand; | 
|---|
| 2360 | int error; | 
|---|
| 2361 |  | 
|---|
| 2362 | if (!name) | 
|---|
| 2363 | return -EINVAL; | 
|---|
| 2364 | if (strlen(name) > 255) | 
|---|
| 2365 | return -ERANGE; | 
|---|
| 2366 |  | 
|---|
| 2367 | ext4_write_lock_xattr(inode, save: &no_expand); | 
|---|
| 2368 |  | 
|---|
| 2369 | /* Check journal credits under write lock. */ | 
|---|
| 2370 | if (ext4_handle_valid(handle)) { | 
|---|
| 2371 | struct buffer_head *bh; | 
|---|
| 2372 | int credits; | 
|---|
| 2373 |  | 
|---|
| 2374 | bh = ext4_xattr_get_block(inode); | 
|---|
| 2375 | if (IS_ERR(ptr: bh)) { | 
|---|
| 2376 | error = PTR_ERR(ptr: bh); | 
|---|
| 2377 | goto cleanup; | 
|---|
| 2378 | } | 
|---|
| 2379 |  | 
|---|
| 2380 | credits = __ext4_xattr_set_credits(sb: inode->i_sb, inode, block_bh: bh, | 
|---|
| 2381 | value_len, | 
|---|
| 2382 | is_create: flags & XATTR_CREATE); | 
|---|
| 2383 | brelse(bh); | 
|---|
| 2384 |  | 
|---|
| 2385 | if (jbd2_handle_buffer_credits(handle) < credits) { | 
|---|
| 2386 | error = -ENOSPC; | 
|---|
| 2387 | goto cleanup; | 
|---|
| 2388 | } | 
|---|
| 2389 | WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS)); | 
|---|
| 2390 | } | 
|---|
| 2391 |  | 
|---|
| 2392 | error = ext4_reserve_inode_write(handle, inode, iloc: &is.iloc); | 
|---|
| 2393 | if (error) | 
|---|
| 2394 | goto cleanup; | 
|---|
| 2395 |  | 
|---|
| 2396 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_NEW)) { | 
|---|
| 2397 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc: &is.iloc); | 
|---|
| 2398 | memset(s: raw_inode, c: 0, n: EXT4_SB(sb: inode->i_sb)->s_inode_size); | 
|---|
| 2399 | ext4_clear_inode_state(inode, bit: EXT4_STATE_NEW); | 
|---|
| 2400 | } | 
|---|
| 2401 |  | 
|---|
| 2402 | error = ext4_xattr_ibody_find(inode, i: &i, is: &is); | 
|---|
| 2403 | if (error) | 
|---|
| 2404 | goto cleanup; | 
|---|
| 2405 | if (is.s.not_found) | 
|---|
| 2406 | error = ext4_xattr_block_find(inode, i: &i, bs: &bs); | 
|---|
| 2407 | if (error) | 
|---|
| 2408 | goto cleanup; | 
|---|
| 2409 | if (is.s.not_found && bs.s.not_found) { | 
|---|
| 2410 | error = -ENODATA; | 
|---|
| 2411 | if (flags & XATTR_REPLACE) | 
|---|
| 2412 | goto cleanup; | 
|---|
| 2413 | error = 0; | 
|---|
| 2414 | if (!value) | 
|---|
| 2415 | goto cleanup; | 
|---|
| 2416 | } else { | 
|---|
| 2417 | error = -EEXIST; | 
|---|
| 2418 | if (flags & XATTR_CREATE) | 
|---|
| 2419 | goto cleanup; | 
|---|
| 2420 | } | 
|---|
| 2421 |  | 
|---|
| 2422 | if (!value) { | 
|---|
| 2423 | if (!is.s.not_found) | 
|---|
| 2424 | error = ext4_xattr_ibody_set(handle, inode, i: &i, is: &is); | 
|---|
| 2425 | else if (!bs.s.not_found) | 
|---|
| 2426 | error = ext4_xattr_block_set(handle, inode, i: &i, bs: &bs); | 
|---|
| 2427 | } else { | 
|---|
| 2428 | error = 0; | 
|---|
| 2429 | /* Xattr value did not change? Save us some work and bail out */ | 
|---|
| 2430 | if (!is.s.not_found && ext4_xattr_value_same(s: &is.s, i: &i)) | 
|---|
| 2431 | goto cleanup; | 
|---|
| 2432 | if (!bs.s.not_found && ext4_xattr_value_same(s: &bs.s, i: &i)) | 
|---|
| 2433 | goto cleanup; | 
|---|
| 2434 |  | 
|---|
| 2435 | if (ext4_has_feature_ea_inode(sb: inode->i_sb) && | 
|---|
| 2436 | (EXT4_XATTR_SIZE(i.value_len) > | 
|---|
| 2437 | EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize))) | 
|---|
| 2438 | i.in_inode = 1; | 
|---|
| 2439 | retry_inode: | 
|---|
| 2440 | error = ext4_xattr_ibody_set(handle, inode, i: &i, is: &is); | 
|---|
| 2441 | if (!error && !bs.s.not_found) { | 
|---|
| 2442 | i.value = NULL; | 
|---|
| 2443 | error = ext4_xattr_block_set(handle, inode, i: &i, bs: &bs); | 
|---|
| 2444 | } else if (error == -ENOSPC) { | 
|---|
| 2445 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { | 
|---|
| 2446 | brelse(bh: bs.bh); | 
|---|
| 2447 | bs.bh = NULL; | 
|---|
| 2448 | error = ext4_xattr_block_find(inode, i: &i, bs: &bs); | 
|---|
| 2449 | if (error) | 
|---|
| 2450 | goto cleanup; | 
|---|
| 2451 | } | 
|---|
| 2452 | error = ext4_xattr_block_set(handle, inode, i: &i, bs: &bs); | 
|---|
| 2453 | if (!error && !is.s.not_found) { | 
|---|
| 2454 | i.value = NULL; | 
|---|
| 2455 | error = ext4_xattr_ibody_set(handle, inode, i: &i, | 
|---|
| 2456 | is: &is); | 
|---|
| 2457 | } else if (error == -ENOSPC) { | 
|---|
| 2458 | /* | 
|---|
| 2459 | * Xattr does not fit in the block, store at | 
|---|
| 2460 | * external inode if possible. | 
|---|
| 2461 | */ | 
|---|
| 2462 | if (ext4_has_feature_ea_inode(sb: inode->i_sb) && | 
|---|
| 2463 | i.value_len && !i.in_inode) { | 
|---|
| 2464 | i.in_inode = 1; | 
|---|
| 2465 | goto retry_inode; | 
|---|
| 2466 | } | 
|---|
| 2467 | } | 
|---|
| 2468 | } | 
|---|
| 2469 | } | 
|---|
| 2470 | if (!error) { | 
|---|
| 2471 | ext4_xattr_update_super_block(handle, sb: inode->i_sb); | 
|---|
| 2472 | inode_set_ctime_current(inode); | 
|---|
| 2473 | inode_inc_iversion(inode); | 
|---|
| 2474 | if (!value) | 
|---|
| 2475 | no_expand = 0; | 
|---|
| 2476 | error = ext4_mark_iloc_dirty(handle, inode, iloc: &is.iloc); | 
|---|
| 2477 | /* | 
|---|
| 2478 | * The bh is consumed by ext4_mark_iloc_dirty, even with | 
|---|
| 2479 | * error != 0. | 
|---|
| 2480 | */ | 
|---|
| 2481 | is.iloc.bh = NULL; | 
|---|
| 2482 | if (IS_SYNC(inode)) | 
|---|
| 2483 | ext4_handle_sync(handle); | 
|---|
| 2484 | } | 
|---|
| 2485 | ext4_fc_mark_ineligible(sb: inode->i_sb, reason: EXT4_FC_REASON_XATTR, handle); | 
|---|
| 2486 |  | 
|---|
| 2487 | cleanup: | 
|---|
| 2488 | brelse(bh: is.iloc.bh); | 
|---|
| 2489 | brelse(bh: bs.bh); | 
|---|
| 2490 | ext4_write_unlock_xattr(inode, save: &no_expand); | 
|---|
| 2491 | return error; | 
|---|
| 2492 | } | 
|---|
| 2493 |  | 
|---|
| 2494 | int ext4_xattr_set_credits(struct inode *inode, size_t value_len, | 
|---|
| 2495 | bool is_create, int *credits) | 
|---|
| 2496 | { | 
|---|
| 2497 | struct buffer_head *bh; | 
|---|
| 2498 | int err; | 
|---|
| 2499 |  | 
|---|
| 2500 | *credits = 0; | 
|---|
| 2501 |  | 
|---|
| 2502 | if (!EXT4_SB(sb: inode->i_sb)->s_journal) | 
|---|
| 2503 | return 0; | 
|---|
| 2504 |  | 
|---|
| 2505 | down_read(sem: &EXT4_I(inode)->xattr_sem); | 
|---|
| 2506 |  | 
|---|
| 2507 | bh = ext4_xattr_get_block(inode); | 
|---|
| 2508 | if (IS_ERR(ptr: bh)) { | 
|---|
| 2509 | err = PTR_ERR(ptr: bh); | 
|---|
| 2510 | } else { | 
|---|
| 2511 | *credits = __ext4_xattr_set_credits(sb: inode->i_sb, inode, block_bh: bh, | 
|---|
| 2512 | value_len, is_create); | 
|---|
| 2513 | brelse(bh); | 
|---|
| 2514 | err = 0; | 
|---|
| 2515 | } | 
|---|
| 2516 |  | 
|---|
| 2517 | up_read(sem: &EXT4_I(inode)->xattr_sem); | 
|---|
| 2518 | return err; | 
|---|
| 2519 | } | 
|---|
| 2520 |  | 
|---|
| 2521 | /* | 
|---|
| 2522 | * ext4_xattr_set() | 
|---|
| 2523 | * | 
|---|
| 2524 | * Like ext4_xattr_set_handle, but start from an inode. This extended | 
|---|
| 2525 | * attribute modification is a filesystem transaction by itself. | 
|---|
| 2526 | * | 
|---|
| 2527 | * Returns 0, or a negative error number on failure. | 
|---|
| 2528 | */ | 
|---|
| 2529 | int | 
|---|
| 2530 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, | 
|---|
| 2531 | const void *value, size_t value_len, int flags) | 
|---|
| 2532 | { | 
|---|
| 2533 | handle_t *handle; | 
|---|
| 2534 | struct super_block *sb = inode->i_sb; | 
|---|
| 2535 | int error, retries = 0; | 
|---|
| 2536 | int credits; | 
|---|
| 2537 |  | 
|---|
| 2538 | error = dquot_initialize(inode); | 
|---|
| 2539 | if (error) | 
|---|
| 2540 | return error; | 
|---|
| 2541 |  | 
|---|
| 2542 | retry: | 
|---|
| 2543 | error = ext4_xattr_set_credits(inode, value_len, is_create: flags & XATTR_CREATE, | 
|---|
| 2544 | credits: &credits); | 
|---|
| 2545 | if (error) | 
|---|
| 2546 | return error; | 
|---|
| 2547 |  | 
|---|
| 2548 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); | 
|---|
| 2549 | if (IS_ERR(ptr: handle)) { | 
|---|
| 2550 | error = PTR_ERR(ptr: handle); | 
|---|
| 2551 | } else { | 
|---|
| 2552 | int error2; | 
|---|
| 2553 |  | 
|---|
| 2554 | error = ext4_xattr_set_handle(handle, inode, name_index, name, | 
|---|
| 2555 | value, value_len, flags); | 
|---|
| 2556 | ext4_fc_mark_ineligible(sb: inode->i_sb, reason: EXT4_FC_REASON_XATTR, | 
|---|
| 2557 | handle); | 
|---|
| 2558 | error2 = ext4_journal_stop(handle); | 
|---|
| 2559 | if (error == -ENOSPC && | 
|---|
| 2560 | ext4_should_retry_alloc(sb, retries: &retries)) | 
|---|
| 2561 | goto retry; | 
|---|
| 2562 | if (error == 0) | 
|---|
| 2563 | error = error2; | 
|---|
| 2564 | } | 
|---|
| 2565 |  | 
|---|
| 2566 | return error; | 
|---|
| 2567 | } | 
|---|
| 2568 |  | 
|---|
| 2569 | /* | 
|---|
| 2570 | * Shift the EA entries in the inode to create space for the increased | 
|---|
| 2571 | * i_extra_isize. | 
|---|
| 2572 | */ | 
|---|
| 2573 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, | 
|---|
| 2574 | int value_offs_shift, void *to, | 
|---|
| 2575 | void *from, size_t n) | 
|---|
| 2576 | { | 
|---|
| 2577 | struct ext4_xattr_entry *last = entry; | 
|---|
| 2578 | int new_offs; | 
|---|
| 2579 |  | 
|---|
| 2580 | /* We always shift xattr headers further thus offsets get lower */ | 
|---|
| 2581 | BUG_ON(value_offs_shift > 0); | 
|---|
| 2582 |  | 
|---|
| 2583 | /* Adjust the value offsets of the entries */ | 
|---|
| 2584 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
|---|
| 2585 | if (!last->e_value_inum && last->e_value_size) { | 
|---|
| 2586 | new_offs = le16_to_cpu(last->e_value_offs) + | 
|---|
| 2587 | value_offs_shift; | 
|---|
| 2588 | last->e_value_offs = cpu_to_le16(new_offs); | 
|---|
| 2589 | } | 
|---|
| 2590 | } | 
|---|
| 2591 | /* Shift the entries by n bytes */ | 
|---|
| 2592 | memmove(dest: to, src: from, count: n); | 
|---|
| 2593 | } | 
|---|
| 2594 |  | 
|---|
| 2595 | /* | 
|---|
| 2596 | * Move xattr pointed to by 'entry' from inode into external xattr block | 
|---|
| 2597 | */ | 
|---|
| 2598 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, | 
|---|
| 2599 | struct ext4_inode *raw_inode, | 
|---|
| 2600 | struct ext4_xattr_entry *entry) | 
|---|
| 2601 | { | 
|---|
| 2602 | struct ext4_xattr_ibody_find *is = NULL; | 
|---|
| 2603 | struct ext4_xattr_block_find *bs = NULL; | 
|---|
| 2604 | char *buffer = NULL, *b_entry_name = NULL; | 
|---|
| 2605 | size_t value_size = le32_to_cpu(entry->e_value_size); | 
|---|
| 2606 | struct ext4_xattr_info i = { | 
|---|
| 2607 | .value = NULL, | 
|---|
| 2608 | .value_len = 0, | 
|---|
| 2609 | .name_index = entry->e_name_index, | 
|---|
| 2610 | .in_inode = !!entry->e_value_inum, | 
|---|
| 2611 | }; | 
|---|
| 2612 | struct ext4_xattr_ibody_header * = IHDR(inode, raw_inode); | 
|---|
| 2613 | int needs_kvfree = 0; | 
|---|
| 2614 | int error; | 
|---|
| 2615 |  | 
|---|
| 2616 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); | 
|---|
| 2617 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); | 
|---|
| 2618 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); | 
|---|
| 2619 | if (!is || !bs || !b_entry_name) { | 
|---|
| 2620 | error = -ENOMEM; | 
|---|
| 2621 | goto out; | 
|---|
| 2622 | } | 
|---|
| 2623 |  | 
|---|
| 2624 | is->s.not_found = -ENODATA; | 
|---|
| 2625 | bs->s.not_found = -ENODATA; | 
|---|
| 2626 | is->iloc.bh = NULL; | 
|---|
| 2627 | bs->bh = NULL; | 
|---|
| 2628 |  | 
|---|
| 2629 | /* Save the entry name and the entry value */ | 
|---|
| 2630 | if (entry->e_value_inum) { | 
|---|
| 2631 | buffer = kvmalloc(value_size, GFP_NOFS); | 
|---|
| 2632 | if (!buffer) { | 
|---|
| 2633 | error = -ENOMEM; | 
|---|
| 2634 | goto out; | 
|---|
| 2635 | } | 
|---|
| 2636 | needs_kvfree = 1; | 
|---|
| 2637 | error = ext4_xattr_inode_get(inode, entry, buffer, size: value_size); | 
|---|
| 2638 | if (error) | 
|---|
| 2639 | goto out; | 
|---|
| 2640 | } else { | 
|---|
| 2641 | size_t value_offs = le16_to_cpu(entry->e_value_offs); | 
|---|
| 2642 | buffer = (void *)IFIRST(header) + value_offs; | 
|---|
| 2643 | } | 
|---|
| 2644 |  | 
|---|
| 2645 | memcpy(to: b_entry_name, from: entry->e_name, len: entry->e_name_len); | 
|---|
| 2646 | b_entry_name[entry->e_name_len] = '\0'; | 
|---|
| 2647 | i.name = b_entry_name; | 
|---|
| 2648 |  | 
|---|
| 2649 | error = ext4_get_inode_loc(inode, &is->iloc); | 
|---|
| 2650 | if (error) | 
|---|
| 2651 | goto out; | 
|---|
| 2652 |  | 
|---|
| 2653 | error = ext4_xattr_ibody_find(inode, i: &i, is); | 
|---|
| 2654 | if (error) | 
|---|
| 2655 | goto out; | 
|---|
| 2656 |  | 
|---|
| 2657 | i.value = buffer; | 
|---|
| 2658 | i.value_len = value_size; | 
|---|
| 2659 | error = ext4_xattr_block_find(inode, i: &i, bs); | 
|---|
| 2660 | if (error) | 
|---|
| 2661 | goto out; | 
|---|
| 2662 |  | 
|---|
| 2663 | /* Move ea entry from the inode into the block */ | 
|---|
| 2664 | error = ext4_xattr_block_set(handle, inode, i: &i, bs); | 
|---|
| 2665 | if (error) | 
|---|
| 2666 | goto out; | 
|---|
| 2667 |  | 
|---|
| 2668 | /* Remove the chosen entry from the inode */ | 
|---|
| 2669 | i.value = NULL; | 
|---|
| 2670 | i.value_len = 0; | 
|---|
| 2671 | error = ext4_xattr_ibody_set(handle, inode, i: &i, is); | 
|---|
| 2672 |  | 
|---|
| 2673 | out: | 
|---|
| 2674 | kfree(objp: b_entry_name); | 
|---|
| 2675 | if (needs_kvfree && buffer) | 
|---|
| 2676 | kvfree(addr: buffer); | 
|---|
| 2677 | if (is) | 
|---|
| 2678 | brelse(bh: is->iloc.bh); | 
|---|
| 2679 | if (bs) | 
|---|
| 2680 | brelse(bh: bs->bh); | 
|---|
| 2681 | kfree(objp: is); | 
|---|
| 2682 | kfree(objp: bs); | 
|---|
| 2683 |  | 
|---|
| 2684 | return error; | 
|---|
| 2685 | } | 
|---|
| 2686 |  | 
|---|
| 2687 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, | 
|---|
| 2688 | struct ext4_inode *raw_inode, | 
|---|
| 2689 | int isize_diff, size_t ifree, | 
|---|
| 2690 | size_t bfree, int *total_ino) | 
|---|
| 2691 | { | 
|---|
| 2692 | struct ext4_xattr_ibody_header * = IHDR(inode, raw_inode); | 
|---|
| 2693 | struct ext4_xattr_entry *small_entry; | 
|---|
| 2694 | struct ext4_xattr_entry *entry; | 
|---|
| 2695 | struct ext4_xattr_entry *last; | 
|---|
| 2696 | unsigned int entry_size;	/* EA entry size */ | 
|---|
| 2697 | unsigned int total_size;	/* EA entry size + value size */ | 
|---|
| 2698 | unsigned int min_total_size; | 
|---|
| 2699 | int error; | 
|---|
| 2700 |  | 
|---|
| 2701 | while (isize_diff > ifree) { | 
|---|
| 2702 | entry = NULL; | 
|---|
| 2703 | small_entry = NULL; | 
|---|
| 2704 | min_total_size = ~0U; | 
|---|
| 2705 | last = IFIRST(header); | 
|---|
| 2706 | /* Find the entry best suited to be pushed into EA block */ | 
|---|
| 2707 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { | 
|---|
| 2708 | /* never move system.data out of the inode */ | 
|---|
| 2709 | if ((last->e_name_len == 4) && | 
|---|
| 2710 | (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) && | 
|---|
| 2711 | !memcmp(last->e_name, "data", 4)) | 
|---|
| 2712 | continue; | 
|---|
| 2713 | total_size = EXT4_XATTR_LEN(last->e_name_len); | 
|---|
| 2714 | if (!last->e_value_inum) | 
|---|
| 2715 | total_size += EXT4_XATTR_SIZE( | 
|---|
| 2716 | le32_to_cpu(last->e_value_size)); | 
|---|
| 2717 | if (total_size <= bfree && | 
|---|
| 2718 | total_size < min_total_size) { | 
|---|
| 2719 | if (total_size + ifree < isize_diff) { | 
|---|
| 2720 | small_entry = last; | 
|---|
| 2721 | } else { | 
|---|
| 2722 | entry = last; | 
|---|
| 2723 | min_total_size = total_size; | 
|---|
| 2724 | } | 
|---|
| 2725 | } | 
|---|
| 2726 | } | 
|---|
| 2727 |  | 
|---|
| 2728 | if (entry == NULL) { | 
|---|
| 2729 | if (small_entry == NULL) | 
|---|
| 2730 | return -ENOSPC; | 
|---|
| 2731 | entry = small_entry; | 
|---|
| 2732 | } | 
|---|
| 2733 |  | 
|---|
| 2734 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); | 
|---|
| 2735 | total_size = entry_size; | 
|---|
| 2736 | if (!entry->e_value_inum) | 
|---|
| 2737 | total_size += EXT4_XATTR_SIZE( | 
|---|
| 2738 | le32_to_cpu(entry->e_value_size)); | 
|---|
| 2739 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, | 
|---|
| 2740 | entry); | 
|---|
| 2741 | if (error) | 
|---|
| 2742 | return error; | 
|---|
| 2743 |  | 
|---|
| 2744 | *total_ino -= entry_size; | 
|---|
| 2745 | ifree += total_size; | 
|---|
| 2746 | bfree -= total_size; | 
|---|
| 2747 | } | 
|---|
| 2748 |  | 
|---|
| 2749 | return 0; | 
|---|
| 2750 | } | 
|---|
| 2751 |  | 
|---|
| 2752 | /* | 
|---|
| 2753 | * Expand an inode by new_extra_isize bytes when EAs are present. | 
|---|
| 2754 | * Returns 0 on success or negative error number on failure. | 
|---|
| 2755 | */ | 
|---|
| 2756 | int ext4_expand_extra_isize_ea(struct inode *inode, int , | 
|---|
| 2757 | struct ext4_inode *raw_inode, handle_t *handle) | 
|---|
| 2758 | { | 
|---|
| 2759 | struct ext4_xattr_ibody_header *; | 
|---|
| 2760 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 2761 | static unsigned int mnt_count; | 
|---|
| 2762 | size_t min_offs; | 
|---|
| 2763 | size_t ifree, bfree; | 
|---|
| 2764 | int total_ino; | 
|---|
| 2765 | void *base, *end; | 
|---|
| 2766 | int error = 0,  = 0; | 
|---|
| 2767 | int  = le16_to_cpu(sbi->s_es->s_min_extra_isize); | 
|---|
| 2768 | int isize_diff;	/* How much do we need to grow i_extra_isize */ | 
|---|
| 2769 |  | 
|---|
| 2770 | retry: | 
|---|
| 2771 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; | 
|---|
| 2772 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) | 
|---|
| 2773 | return 0; | 
|---|
| 2774 |  | 
|---|
| 2775 | header = IHDR(inode, raw_inode); | 
|---|
| 2776 |  | 
|---|
| 2777 | /* | 
|---|
| 2778 | * Check if enough free space is available in the inode to shift the | 
|---|
| 2779 | * entries ahead by new_extra_isize. | 
|---|
| 2780 | */ | 
|---|
| 2781 |  | 
|---|
| 2782 | base = IFIRST(header); | 
|---|
| 2783 | end = ITAIL(inode, raw_inode); | 
|---|
| 2784 | min_offs = end - base; | 
|---|
| 2785 | total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32); | 
|---|
| 2786 |  | 
|---|
| 2787 | ifree = ext4_xattr_free_space(last: base, min_offs: &min_offs, base, total: &total_ino); | 
|---|
| 2788 | if (ifree >= isize_diff) | 
|---|
| 2789 | goto shift; | 
|---|
| 2790 |  | 
|---|
| 2791 | /* | 
|---|
| 2792 | * Enough free space isn't available in the inode, check if | 
|---|
| 2793 | * EA block can hold new_extra_isize bytes. | 
|---|
| 2794 | */ | 
|---|
| 2795 | if (EXT4_I(inode)->i_file_acl) { | 
|---|
| 2796 | struct buffer_head *bh; | 
|---|
| 2797 |  | 
|---|
| 2798 | bh = ext4_sb_bread(sb: inode->i_sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 2799 | if (IS_ERR(ptr: bh)) { | 
|---|
| 2800 | error = PTR_ERR(ptr: bh); | 
|---|
| 2801 | goto cleanup; | 
|---|
| 2802 | } | 
|---|
| 2803 | error = ext4_xattr_check_block(inode, bh); | 
|---|
| 2804 | if (error) { | 
|---|
| 2805 | brelse(bh); | 
|---|
| 2806 | goto cleanup; | 
|---|
| 2807 | } | 
|---|
| 2808 | base = BHDR(bh); | 
|---|
| 2809 | end = bh->b_data + bh->b_size; | 
|---|
| 2810 | min_offs = end - base; | 
|---|
| 2811 | bfree = ext4_xattr_free_space(BFIRST(bh), min_offs: &min_offs, base, | 
|---|
| 2812 | NULL); | 
|---|
| 2813 | brelse(bh); | 
|---|
| 2814 | if (bfree + ifree < isize_diff) { | 
|---|
| 2815 | if (!tried_min_extra_isize && s_min_extra_isize) { | 
|---|
| 2816 | tried_min_extra_isize++; | 
|---|
| 2817 | new_extra_isize = s_min_extra_isize; | 
|---|
| 2818 | goto retry; | 
|---|
| 2819 | } | 
|---|
| 2820 | error = -ENOSPC; | 
|---|
| 2821 | goto cleanup; | 
|---|
| 2822 | } | 
|---|
| 2823 | } else { | 
|---|
| 2824 | bfree = inode->i_sb->s_blocksize; | 
|---|
| 2825 | } | 
|---|
| 2826 |  | 
|---|
| 2827 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, | 
|---|
| 2828 | isize_diff, ifree, bfree, | 
|---|
| 2829 | total_ino: &total_ino); | 
|---|
| 2830 | if (error) { | 
|---|
| 2831 | if (error == -ENOSPC && !tried_min_extra_isize && | 
|---|
| 2832 | s_min_extra_isize) { | 
|---|
| 2833 | tried_min_extra_isize++; | 
|---|
| 2834 | new_extra_isize = s_min_extra_isize; | 
|---|
| 2835 | goto retry; | 
|---|
| 2836 | } | 
|---|
| 2837 | goto cleanup; | 
|---|
| 2838 | } | 
|---|
| 2839 | shift: | 
|---|
| 2840 | /* Adjust the offsets and shift the remaining entries ahead */ | 
|---|
| 2841 | ext4_xattr_shift_entries(IFIRST(header), value_offs_shift: EXT4_I(inode)->i_extra_isize | 
|---|
| 2842 | - new_extra_isize, to: (void *)raw_inode + | 
|---|
| 2843 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, | 
|---|
| 2844 | from: (void *)header, n: total_ino); | 
|---|
| 2845 | EXT4_I(inode)->i_extra_isize = new_extra_isize; | 
|---|
| 2846 |  | 
|---|
| 2847 | if (ext4_has_inline_data(inode)) | 
|---|
| 2848 | error = ext4_find_inline_data_nolock(inode); | 
|---|
| 2849 |  | 
|---|
| 2850 | cleanup: | 
|---|
| 2851 | if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) { | 
|---|
| 2852 | ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.", | 
|---|
| 2853 | inode->i_ino); | 
|---|
| 2854 | mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count); | 
|---|
| 2855 | } | 
|---|
| 2856 | return error; | 
|---|
| 2857 | } | 
|---|
| 2858 |  | 
|---|
| 2859 | #define EIA_INCR 16 /* must be 2^n */ | 
|---|
| 2860 | #define EIA_MASK (EIA_INCR - 1) | 
|---|
| 2861 |  | 
|---|
| 2862 | /* Add the large xattr @inode into @ea_inode_array for deferred iput(). | 
|---|
| 2863 | * If @ea_inode_array is new or full it will be grown and the old | 
|---|
| 2864 | * contents copied over. | 
|---|
| 2865 | */ | 
|---|
| 2866 | static int | 
|---|
| 2867 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, | 
|---|
| 2868 | struct inode *inode) | 
|---|
| 2869 | { | 
|---|
| 2870 | if (*ea_inode_array == NULL) { | 
|---|
| 2871 | /* | 
|---|
| 2872 | * Start with 15 inodes, so it fits into a power-of-two size. | 
|---|
| 2873 | */ | 
|---|
| 2874 | (*ea_inode_array) = kmalloc( | 
|---|
| 2875 | struct_size(*ea_inode_array, inodes, EIA_MASK), | 
|---|
| 2876 | GFP_NOFS); | 
|---|
| 2877 | if (*ea_inode_array == NULL) | 
|---|
| 2878 | return -ENOMEM; | 
|---|
| 2879 | (*ea_inode_array)->count = 0; | 
|---|
| 2880 | } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { | 
|---|
| 2881 | /* expand the array once all 15 + n * 16 slots are full */ | 
|---|
| 2882 | struct ext4_xattr_inode_array *new_array = NULL; | 
|---|
| 2883 |  | 
|---|
| 2884 | new_array = kmalloc( | 
|---|
| 2885 | struct_size(*ea_inode_array, inodes, | 
|---|
| 2886 | (*ea_inode_array)->count + EIA_INCR), | 
|---|
| 2887 | GFP_NOFS); | 
|---|
| 2888 | if (new_array == NULL) | 
|---|
| 2889 | return -ENOMEM; | 
|---|
| 2890 | memcpy(to: new_array, from: *ea_inode_array, | 
|---|
| 2891 | struct_size(*ea_inode_array, inodes, | 
|---|
| 2892 | (*ea_inode_array)->count)); | 
|---|
| 2893 | kfree(objp: *ea_inode_array); | 
|---|
| 2894 | *ea_inode_array = new_array; | 
|---|
| 2895 | } | 
|---|
| 2896 | (*ea_inode_array)->count++; | 
|---|
| 2897 | (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode; | 
|---|
| 2898 | return 0; | 
|---|
| 2899 | } | 
|---|
| 2900 |  | 
|---|
| 2901 | /* | 
|---|
| 2902 | * ext4_xattr_delete_inode() | 
|---|
| 2903 | * | 
|---|
| 2904 | * Free extended attribute resources associated with this inode. Traverse | 
|---|
| 2905 | * all entries and decrement reference on any xattr inodes associated with this | 
|---|
| 2906 | * inode. This is called immediately before an inode is freed. We have exclusive | 
|---|
| 2907 | * access to the inode. If an orphan inode is deleted it will also release its | 
|---|
| 2908 | * references on xattr block and xattr inodes. | 
|---|
| 2909 | */ | 
|---|
| 2910 | int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, | 
|---|
| 2911 | struct ext4_xattr_inode_array **ea_inode_array, | 
|---|
| 2912 | int ) | 
|---|
| 2913 | { | 
|---|
| 2914 | struct buffer_head *bh = NULL; | 
|---|
| 2915 | struct ext4_xattr_ibody_header *; | 
|---|
| 2916 | struct ext4_iloc iloc = { .bh = NULL }; | 
|---|
| 2917 | struct ext4_xattr_entry *entry; | 
|---|
| 2918 | struct inode *ea_inode; | 
|---|
| 2919 | int error; | 
|---|
| 2920 |  | 
|---|
| 2921 | error = ext4_journal_ensure_credits(handle, credits: extra_credits, | 
|---|
| 2922 | revoke_creds: ext4_free_metadata_revoke_credits(sb: inode->i_sb, blocks: 1)); | 
|---|
| 2923 | if (error < 0) { | 
|---|
| 2924 | EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error); | 
|---|
| 2925 | goto cleanup; | 
|---|
| 2926 | } | 
|---|
| 2927 |  | 
|---|
| 2928 | if (ext4_has_feature_ea_inode(sb: inode->i_sb) && | 
|---|
| 2929 | ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR)) { | 
|---|
| 2930 |  | 
|---|
| 2931 | error = ext4_get_inode_loc(inode, &iloc); | 
|---|
| 2932 | if (error) { | 
|---|
| 2933 | EXT4_ERROR_INODE(inode, "inode loc (error %d)", error); | 
|---|
| 2934 | goto cleanup; | 
|---|
| 2935 | } | 
|---|
| 2936 |  | 
|---|
| 2937 | error = ext4_journal_get_write_access(handle, inode->i_sb, | 
|---|
| 2938 | iloc.bh, EXT4_JTR_NONE); | 
|---|
| 2939 | if (error) { | 
|---|
| 2940 | EXT4_ERROR_INODE(inode, "write access (error %d)", | 
|---|
| 2941 | error); | 
|---|
| 2942 | goto cleanup; | 
|---|
| 2943 | } | 
|---|
| 2944 |  | 
|---|
| 2945 | header = IHDR(inode, ext4_raw_inode(&iloc)); | 
|---|
| 2946 | if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC)) | 
|---|
| 2947 | ext4_xattr_inode_dec_ref_all(handle, parent: inode, bh: iloc.bh, | 
|---|
| 2948 | IFIRST(header), | 
|---|
| 2949 | block_csum: false /* block_csum */, | 
|---|
| 2950 | ea_inode_array, | 
|---|
| 2951 | extra_credits, | 
|---|
| 2952 | skip_quota: false /* skip_quota */); | 
|---|
| 2953 | } | 
|---|
| 2954 |  | 
|---|
| 2955 | if (EXT4_I(inode)->i_file_acl) { | 
|---|
| 2956 | bh = ext4_sb_bread(sb: inode->i_sb, block: EXT4_I(inode)->i_file_acl, REQ_PRIO); | 
|---|
| 2957 | if (IS_ERR(ptr: bh)) { | 
|---|
| 2958 | error = PTR_ERR(ptr: bh); | 
|---|
| 2959 | if (error == -EIO) { | 
|---|
| 2960 | EXT4_ERROR_INODE_ERR(inode, EIO, | 
|---|
| 2961 | "block %llu read error", | 
|---|
| 2962 | EXT4_I(inode)->i_file_acl); | 
|---|
| 2963 | } | 
|---|
| 2964 | bh = NULL; | 
|---|
| 2965 | goto cleanup; | 
|---|
| 2966 | } | 
|---|
| 2967 | error = ext4_xattr_check_block(inode, bh); | 
|---|
| 2968 | if (error) | 
|---|
| 2969 | goto cleanup; | 
|---|
| 2970 |  | 
|---|
| 2971 | if (ext4_has_feature_ea_inode(sb: inode->i_sb)) { | 
|---|
| 2972 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); | 
|---|
| 2973 | entry = EXT4_XATTR_NEXT(entry)) { | 
|---|
| 2974 | if (!entry->e_value_inum) | 
|---|
| 2975 | continue; | 
|---|
| 2976 | error = ext4_xattr_inode_iget(parent: inode, | 
|---|
| 2977 | le32_to_cpu(entry->e_value_inum), | 
|---|
| 2978 | le32_to_cpu(entry->e_hash), | 
|---|
| 2979 | ea_inode: &ea_inode); | 
|---|
| 2980 | if (error) | 
|---|
| 2981 | continue; | 
|---|
| 2982 | ext4_xattr_inode_free_quota(parent: inode, ea_inode, | 
|---|
| 2983 | le32_to_cpu(entry->e_value_size)); | 
|---|
| 2984 | iput(ea_inode); | 
|---|
| 2985 | } | 
|---|
| 2986 |  | 
|---|
| 2987 | } | 
|---|
| 2988 |  | 
|---|
| 2989 | ext4_xattr_release_block(handle, inode, bh, ea_inode_array, | 
|---|
| 2990 | extra_credits); | 
|---|
| 2991 | /* | 
|---|
| 2992 | * Update i_file_acl value in the same transaction that releases | 
|---|
| 2993 | * block. | 
|---|
| 2994 | */ | 
|---|
| 2995 | EXT4_I(inode)->i_file_acl = 0; | 
|---|
| 2996 | error = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 2997 | if (error) { | 
|---|
| 2998 | EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)", | 
|---|
| 2999 | error); | 
|---|
| 3000 | goto cleanup; | 
|---|
| 3001 | } | 
|---|
| 3002 | ext4_fc_mark_ineligible(sb: inode->i_sb, reason: EXT4_FC_REASON_XATTR, handle); | 
|---|
| 3003 | } | 
|---|
| 3004 | error = 0; | 
|---|
| 3005 | cleanup: | 
|---|
| 3006 | brelse(bh: iloc.bh); | 
|---|
| 3007 | brelse(bh); | 
|---|
| 3008 | return error; | 
|---|
| 3009 | } | 
|---|
| 3010 |  | 
|---|
| 3011 | void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array) | 
|---|
| 3012 | { | 
|---|
| 3013 | int idx; | 
|---|
| 3014 |  | 
|---|
| 3015 | if (ea_inode_array == NULL) | 
|---|
| 3016 | return; | 
|---|
| 3017 |  | 
|---|
| 3018 | for (idx = 0; idx < ea_inode_array->count; ++idx) | 
|---|
| 3019 | iput(ea_inode_array->inodes[idx]); | 
|---|
| 3020 | kfree(objp: ea_inode_array); | 
|---|
| 3021 | } | 
|---|
| 3022 |  | 
|---|
| 3023 | /* | 
|---|
| 3024 | * ext4_xattr_block_cache_insert() | 
|---|
| 3025 | * | 
|---|
| 3026 | * Create a new entry in the extended attribute block cache, and insert | 
|---|
| 3027 | * it unless such an entry is already in the cache. | 
|---|
| 3028 | */ | 
|---|
| 3029 | static void | 
|---|
| 3030 | ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache, | 
|---|
| 3031 | struct buffer_head *bh) | 
|---|
| 3032 | { | 
|---|
| 3033 | struct ext4_xattr_header * = BHDR(bh); | 
|---|
| 3034 | __u32 hash = le32_to_cpu(header->h_hash); | 
|---|
| 3035 | int reusable = le32_to_cpu(header->h_refcount) < | 
|---|
| 3036 | EXT4_XATTR_REFCOUNT_MAX; | 
|---|
| 3037 | int error; | 
|---|
| 3038 |  | 
|---|
| 3039 | if (!ea_block_cache) | 
|---|
| 3040 | return; | 
|---|
| 3041 | error = mb_cache_entry_create(cache: ea_block_cache, GFP_NOFS, key: hash, | 
|---|
| 3042 | value: bh->b_blocknr, reusable); | 
|---|
| 3043 | if (error) { | 
|---|
| 3044 | if (error == -EBUSY) | 
|---|
| 3045 | ea_bdebug(bh, "already in cache"); | 
|---|
| 3046 | } else | 
|---|
| 3047 | ea_bdebug(bh, "inserting [%x]", (int)hash); | 
|---|
| 3048 | } | 
|---|
| 3049 |  | 
|---|
| 3050 | /* | 
|---|
| 3051 | * ext4_xattr_cmp() | 
|---|
| 3052 | * | 
|---|
| 3053 | * Compare two extended attribute blocks for equality. | 
|---|
| 3054 | * | 
|---|
| 3055 | * Returns 0 if the blocks are equal, 1 if they differ. | 
|---|
| 3056 | */ | 
|---|
| 3057 | static int | 
|---|
| 3058 | ext4_xattr_cmp(struct ext4_xattr_header *, | 
|---|
| 3059 | struct ext4_xattr_header *) | 
|---|
| 3060 | { | 
|---|
| 3061 | struct ext4_xattr_entry *entry1, *entry2; | 
|---|
| 3062 |  | 
|---|
| 3063 | entry1 = ENTRY(header1+1); | 
|---|
| 3064 | entry2 = ENTRY(header2+1); | 
|---|
| 3065 | while (!IS_LAST_ENTRY(entry1)) { | 
|---|
| 3066 | if (IS_LAST_ENTRY(entry2)) | 
|---|
| 3067 | return 1; | 
|---|
| 3068 | if (entry1->e_hash != entry2->e_hash || | 
|---|
| 3069 | entry1->e_name_index != entry2->e_name_index || | 
|---|
| 3070 | entry1->e_name_len != entry2->e_name_len || | 
|---|
| 3071 | entry1->e_value_size != entry2->e_value_size || | 
|---|
| 3072 | entry1->e_value_inum != entry2->e_value_inum || | 
|---|
| 3073 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) | 
|---|
| 3074 | return 1; | 
|---|
| 3075 | if (!entry1->e_value_inum && | 
|---|
| 3076 | memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), | 
|---|
| 3077 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), | 
|---|
| 3078 | le32_to_cpu(entry1->e_value_size))) | 
|---|
| 3079 | return 1; | 
|---|
| 3080 |  | 
|---|
| 3081 | entry1 = EXT4_XATTR_NEXT(entry1); | 
|---|
| 3082 | entry2 = EXT4_XATTR_NEXT(entry2); | 
|---|
| 3083 | } | 
|---|
| 3084 | if (!IS_LAST_ENTRY(entry2)) | 
|---|
| 3085 | return 1; | 
|---|
| 3086 | return 0; | 
|---|
| 3087 | } | 
|---|
| 3088 |  | 
|---|
| 3089 | /* | 
|---|
| 3090 | * ext4_xattr_block_cache_find() | 
|---|
| 3091 | * | 
|---|
| 3092 | * Find an identical extended attribute block. | 
|---|
| 3093 | * | 
|---|
| 3094 | * Returns a pointer to the block found, or NULL if such a block was not | 
|---|
| 3095 | * found, or an error pointer if an error occurred while reading ea block. | 
|---|
| 3096 | */ | 
|---|
| 3097 | static struct buffer_head * | 
|---|
| 3098 | ext4_xattr_block_cache_find(struct inode *inode, | 
|---|
| 3099 | struct ext4_xattr_header *, | 
|---|
| 3100 | struct mb_cache_entry **pce) | 
|---|
| 3101 | { | 
|---|
| 3102 | __u32 hash = le32_to_cpu(header->h_hash); | 
|---|
| 3103 | struct mb_cache_entry *ce; | 
|---|
| 3104 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); | 
|---|
| 3105 |  | 
|---|
| 3106 | if (!ea_block_cache) | 
|---|
| 3107 | return NULL; | 
|---|
| 3108 | if (!header->h_hash) | 
|---|
| 3109 | return NULL;  /* never share */ | 
|---|
| 3110 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); | 
|---|
| 3111 | ce = mb_cache_entry_find_first(cache: ea_block_cache, key: hash); | 
|---|
| 3112 | while (ce) { | 
|---|
| 3113 | struct buffer_head *bh; | 
|---|
| 3114 |  | 
|---|
| 3115 | bh = ext4_sb_bread(sb: inode->i_sb, block: ce->e_value, REQ_PRIO); | 
|---|
| 3116 | if (IS_ERR(ptr: bh)) { | 
|---|
| 3117 | if (PTR_ERR(ptr: bh) != -ENOMEM) | 
|---|
| 3118 | EXT4_ERROR_INODE(inode, "block %lu read error", | 
|---|
| 3119 | (unsigned long)ce->e_value); | 
|---|
| 3120 | mb_cache_entry_put(cache: ea_block_cache, entry: ce); | 
|---|
| 3121 | return bh; | 
|---|
| 3122 | } else if (ext4_xattr_cmp(header1: header, BHDR(bh)) == 0) { | 
|---|
| 3123 | *pce = ce; | 
|---|
| 3124 | return bh; | 
|---|
| 3125 | } | 
|---|
| 3126 | brelse(bh); | 
|---|
| 3127 | ce = mb_cache_entry_find_next(cache: ea_block_cache, entry: ce); | 
|---|
| 3128 | } | 
|---|
| 3129 | return NULL; | 
|---|
| 3130 | } | 
|---|
| 3131 |  | 
|---|
| 3132 | #define NAME_HASH_SHIFT 5 | 
|---|
| 3133 | #define VALUE_HASH_SHIFT 16 | 
|---|
| 3134 |  | 
|---|
| 3135 | /* | 
|---|
| 3136 | * ext4_xattr_hash_entry() | 
|---|
| 3137 | * | 
|---|
| 3138 | * Compute the hash of an extended attribute. | 
|---|
| 3139 | */ | 
|---|
| 3140 | static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, | 
|---|
| 3141 | size_t value_count) | 
|---|
| 3142 | { | 
|---|
| 3143 | __u32 hash = 0; | 
|---|
| 3144 |  | 
|---|
| 3145 | while (name_len--) { | 
|---|
| 3146 | hash = (hash << NAME_HASH_SHIFT) ^ | 
|---|
| 3147 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ | 
|---|
| 3148 | (unsigned char)*name++; | 
|---|
| 3149 | } | 
|---|
| 3150 | while (value_count--) { | 
|---|
| 3151 | hash = (hash << VALUE_HASH_SHIFT) ^ | 
|---|
| 3152 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ | 
|---|
| 3153 | le32_to_cpu(*value++); | 
|---|
| 3154 | } | 
|---|
| 3155 | return cpu_to_le32(hash); | 
|---|
| 3156 | } | 
|---|
| 3157 |  | 
|---|
| 3158 | /* | 
|---|
| 3159 | * ext4_xattr_hash_entry_signed() | 
|---|
| 3160 | * | 
|---|
| 3161 | * Compute the hash of an extended attribute incorrectly. | 
|---|
| 3162 | */ | 
|---|
| 3163 | static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count) | 
|---|
| 3164 | { | 
|---|
| 3165 | __u32 hash = 0; | 
|---|
| 3166 |  | 
|---|
| 3167 | while (name_len--) { | 
|---|
| 3168 | hash = (hash << NAME_HASH_SHIFT) ^ | 
|---|
| 3169 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ | 
|---|
| 3170 | (signed char)*name++; | 
|---|
| 3171 | } | 
|---|
| 3172 | while (value_count--) { | 
|---|
| 3173 | hash = (hash << VALUE_HASH_SHIFT) ^ | 
|---|
| 3174 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ | 
|---|
| 3175 | le32_to_cpu(*value++); | 
|---|
| 3176 | } | 
|---|
| 3177 | return cpu_to_le32(hash); | 
|---|
| 3178 | } | 
|---|
| 3179 |  | 
|---|
| 3180 | #undef NAME_HASH_SHIFT | 
|---|
| 3181 | #undef VALUE_HASH_SHIFT | 
|---|
| 3182 |  | 
|---|
| 3183 | #define BLOCK_HASH_SHIFT 16 | 
|---|
| 3184 |  | 
|---|
| 3185 | /* | 
|---|
| 3186 | * ext4_xattr_rehash() | 
|---|
| 3187 | * | 
|---|
| 3188 | * Re-compute the extended attribute hash value after an entry has changed. | 
|---|
| 3189 | */ | 
|---|
| 3190 | static void ext4_xattr_rehash(struct ext4_xattr_header *) | 
|---|
| 3191 | { | 
|---|
| 3192 | struct ext4_xattr_entry *here; | 
|---|
| 3193 | __u32 hash = 0; | 
|---|
| 3194 |  | 
|---|
| 3195 | here = ENTRY(header+1); | 
|---|
| 3196 | while (!IS_LAST_ENTRY(here)) { | 
|---|
| 3197 | if (!here->e_hash) { | 
|---|
| 3198 | /* Block is not shared if an entry's hash value == 0 */ | 
|---|
| 3199 | hash = 0; | 
|---|
| 3200 | break; | 
|---|
| 3201 | } | 
|---|
| 3202 | hash = (hash << BLOCK_HASH_SHIFT) ^ | 
|---|
| 3203 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ | 
|---|
| 3204 | le32_to_cpu(here->e_hash); | 
|---|
| 3205 | here = EXT4_XATTR_NEXT(here); | 
|---|
| 3206 | } | 
|---|
| 3207 | header->h_hash = cpu_to_le32(hash); | 
|---|
| 3208 | } | 
|---|
| 3209 |  | 
|---|
| 3210 | #undef BLOCK_HASH_SHIFT | 
|---|
| 3211 |  | 
|---|
| 3212 | #define	HASH_BUCKET_BITS	10 | 
|---|
| 3213 |  | 
|---|
| 3214 | struct mb_cache * | 
|---|
| 3215 | ext4_xattr_create_cache(void) | 
|---|
| 3216 | { | 
|---|
| 3217 | return mb_cache_create(HASH_BUCKET_BITS); | 
|---|
| 3218 | } | 
|---|
| 3219 |  | 
|---|
| 3220 | void ext4_xattr_destroy_cache(struct mb_cache *cache) | 
|---|
| 3221 | { | 
|---|
| 3222 | if (cache) | 
|---|
| 3223 | mb_cache_destroy(cache); | 
|---|
| 3224 | } | 
|---|
| 3225 |  | 
|---|
| 3226 |  | 
|---|