blob: 4025666c59912661a2cf9f799bf4cce9bb6a411d [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/xattr.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
Mingming Cao617ba132006-10-11 01:20:53 -07007 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07008 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11 * Red Hat Inc.
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
14 */
15
16/*
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
24 *
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
27 *
28 * +------------------+
29 * | header |
30 * | entry 1 | |
31 * | entry 2 | | growing downwards
32 * | entry 3 | v
33 * | four null bytes |
34 * | . . . |
35 * | value 1 | ^
36 * | value 3 | | growing upwards
37 * | value 2 | |
38 * +------------------+
39 *
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
43 *
44 * Locking strategy
45 * ----------------
Mingming Cao617ba132006-10-11 01:20:53 -070046 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
50 * by the buffer lock.
51 */
52
53#include <linux/init.h>
54#include <linux/fs.h>
55#include <linux/slab.h>
Jan Kara7a2508e2016-02-22 22:35:22 -050056#include <linux/mbcache.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057#include <linux/quotaops.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040058#include "ext4_jbd2.h"
59#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060#include "xattr.h"
61#include "acl.h"
62
Mingming Cao617ba132006-10-11 01:20:53 -070063#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040064# define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67# define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070#else
Joe Perchesace36ad2012-03-19 23:11:43 -040071# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073#endif
74
Tahsin Erdogan47387402017-06-22 11:28:55 -040075static void ext4_xattr_block_cache_insert(struct mb_cache *,
76 struct buffer_head *);
77static struct buffer_head *
78ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
79 struct mb_cache_entry **);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -040080static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
81 size_t value_count);
Tahsin Erdogandaf83282017-06-22 11:52:03 -040082static void ext4_xattr_rehash(struct ext4_xattr_header *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083
Eric Biggersd6006182017-04-29 23:47:50 -040084static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070085 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040086#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080087 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089#endif
Mingming Cao617ba132006-10-11 01:20:53 -070090 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040091#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070092 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093#endif
94};
95
Stephen Hemminger11e27522010-05-13 17:53:18 -070096const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070097 &ext4_xattr_user_handler,
98 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040099#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800100 &posix_acl_access_xattr_handler,
101 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400103#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700104 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105#endif
106 NULL
107};
108
Tahsin Erdogan47387402017-06-22 11:28:55 -0400109#define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_ea_block_cache)
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400111
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400112#define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
113 inode->i_sb->s_fs_info)->s_ea_inode_cache)
114
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400115static int
116ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
117 struct inode *inode);
118
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400119#ifdef CONFIG_LOCKDEP
120void ext4_xattr_inode_set_class(struct inode *ea_inode)
121{
122 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
123}
124#endif
125
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400126static __le32 ext4_xattr_block_csum(struct inode *inode,
127 sector_t block_nr,
128 struct ext4_xattr_header *hdr)
129{
130 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400131 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400132 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400133 __u32 dummy_csum = 0;
134 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135
Theodore Ts'od6a77102013-04-09 23:59:55 -0400136 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
137 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400138 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
139 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
140 offset += sizeof(dummy_csum);
141 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
142 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400143
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400144 return cpu_to_le32(csum);
145}
146
147static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400148 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400149{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400150 struct ext4_xattr_header *hdr = BHDR(bh);
151 int ret = 1;
152
153 if (ext4_has_metadata_csum(inode->i_sb)) {
154 lock_buffer(bh);
155 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
156 bh->b_blocknr, hdr));
157 unlock_buffer(bh);
158 }
159 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400160}
161
162static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400163 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400164{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400165 if (ext4_has_metadata_csum(inode->i_sb))
166 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
167 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400168}
169
Stephen Hemminger11e27522010-05-13 17:53:18 -0700170static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700171ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700173 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174
Mingming Cao617ba132006-10-11 01:20:53 -0700175 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
176 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 return handler;
178}
179
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400181ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
182 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400184 struct ext4_xattr_entry *e = entry;
185
Eric Biggersd7614cc2016-12-01 14:57:29 -0500186 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400187 while (!IS_LAST_ENTRY(e)) {
188 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400190 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400191 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193
Eric Biggersd7614cc2016-12-01 14:57:29 -0500194 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400195 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400196 if (entry->e_value_size != 0 &&
197 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500198 u16 offs = le16_to_cpu(entry->e_value_offs);
199 u32 size = le32_to_cpu(entry->e_value_size);
200 void *value;
201
202 /*
203 * The value cannot overlap the names, and the value
204 * with padding cannot extend beyond 'end'. Check both
205 * the padded and unpadded sizes, since the size may
206 * overflow to 0 when adding padding.
207 */
208 if (offs > end - value_start)
209 return -EFSCORRUPTED;
210 value = value_start + offs;
211 if (value < (void *)e + sizeof(u32) ||
212 size > end - value ||
213 EXT4_XATTR_SIZE(size) > end - value)
214 return -EFSCORRUPTED;
215 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400216 entry = EXT4_XATTR_NEXT(entry);
217 }
218
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 return 0;
220}
221
222static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400223ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400225 int error;
226
227 if (buffer_verified(bh))
228 return 0;
229
Mingming Cao617ba132006-10-11 01:20:53 -0700230 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400232 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400233 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400234 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400235 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
236 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400237 if (!error)
238 set_buffer_verified(bh);
239 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240}
241
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400242static int
243__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
244 void *end, const char *function, unsigned int line)
245{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400246 int error = -EFSCORRUPTED;
247
Eric Biggers290ab232016-12-01 14:51:58 -0500248 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400249 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400250 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400251 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400252errout:
253 if (error)
254 __ext4_error_inode(inode, function, line, 0,
255 "corrupted in-inode xattr");
256 return error;
257}
258
259#define xattr_check_inode(inode, header, end) \
260 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
261
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262static int
Mingming Cao617ba132006-10-11 01:20:53 -0700263ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400264 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265{
Mingming Cao617ba132006-10-11 01:20:53 -0700266 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 size_t name_len;
268 int cmp = 1;
269
270 if (name == NULL)
271 return -EINVAL;
272 name_len = strlen(name);
273 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700274 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 cmp = name_index - entry->e_name_index;
276 if (!cmp)
277 cmp = name_len - entry->e_name_len;
278 if (!cmp)
279 cmp = memcmp(name, entry->e_name, name_len);
280 if (cmp <= 0 && (sorted || cmp == 0))
281 break;
282 }
283 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 return cmp ? -ENODATA : 0;
285}
286
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400287static u32
288ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
289{
290 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
291}
292
293static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
294{
295 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
296 ((u32)ea_inode->i_version);
297}
298
299static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
300{
301 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
302 ea_inode->i_version = (u32)ref_count;
303}
304
305static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
306{
307 return (u32)ea_inode->i_atime.tv_sec;
308}
309
310static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
311{
312 ea_inode->i_atime.tv_sec = hash;
313}
314
Andreas Dilgere50e5122017-06-21 21:10:32 -0400315/*
316 * Read the EA value from an inode.
317 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400318static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400319{
320 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400321 struct buffer_head *bh;
Tahsin Erdogan909666932017-06-21 21:57:36 -0400322 int blocksize = ea_inode->i_sb->s_blocksize;
323 size_t csize, copied = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400324 void *copy_pos = buf;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400325
Tahsin Erdogan909666932017-06-21 21:57:36 -0400326 while (copied < size) {
327 csize = (size - copied) > blocksize ? blocksize : size - copied;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400328 bh = ext4_bread(NULL, ea_inode, block, 0);
Tahsin Erdogan909666932017-06-21 21:57:36 -0400329 if (IS_ERR(bh))
Andreas Dilgere50e5122017-06-21 21:10:32 -0400330 return PTR_ERR(bh);
Tahsin Erdogan909666932017-06-21 21:57:36 -0400331 if (!bh)
332 return -EFSCORRUPTED;
333
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400334 memcpy(copy_pos, bh->b_data, csize);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400335 brelse(bh);
336
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400337 copy_pos += csize;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400338 block += 1;
Tahsin Erdogan909666932017-06-21 21:57:36 -0400339 copied += csize;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400340 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400341 return 0;
342}
343
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400344static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
345 struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400346{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400347 struct inode *inode;
348 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400349
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400350 inode = ext4_iget(parent->i_sb, ea_ino);
351 if (IS_ERR(inode)) {
352 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400353 ext4_error(parent->i_sb,
354 "error while reading EA inode %lu err=%d", ea_ino,
355 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400356 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400357 }
358
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400359 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400360 ext4_error(parent->i_sb,
361 "error while reading EA inode %lu is_bad_inode",
362 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400363 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400364 goto error;
365 }
366
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400367 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400368 ext4_error(parent->i_sb,
369 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
370 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400371 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400372 goto error;
373 }
374
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400375 *ea_inode = inode;
376 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400377error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400378 iput(inode);
379 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400380}
381
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400382static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400383ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
384 struct ext4_xattr_entry *entry, void *buffer,
385 size_t size)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400386{
387 u32 hash;
388
389 /* Verify stored hash matches calculated hash. */
390 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
391 if (hash != ext4_xattr_inode_get_hash(ea_inode))
392 return -EFSCORRUPTED;
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400393
394 if (entry) {
395 __le32 e_hash, tmp_data;
396
397 /* Verify entry hash. */
398 tmp_data = cpu_to_le32(hash);
399 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
400 &tmp_data, 1);
401 if (e_hash != entry->e_hash)
402 return -EFSCORRUPTED;
403 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400404 return 0;
405}
406
407#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
408
Andreas Dilgere50e5122017-06-21 21:10:32 -0400409/*
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400410 * Read xattr value from the EA inode.
Andreas Dilgere50e5122017-06-21 21:10:32 -0400411 */
412static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400413ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
414 void *buffer, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400415{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400416 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400417 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400418 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400419
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400420 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
421 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400422 if (err) {
423 ea_inode = NULL;
424 goto out;
425 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400426
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400427 if (i_size_read(ea_inode) != size) {
428 ext4_warning_inode(ea_inode,
429 "ea_inode file size=%llu entry size=%zu",
430 i_size_read(ea_inode), size);
431 err = -EFSCORRUPTED;
432 goto out;
433 }
434
435 err = ext4_xattr_inode_read(ea_inode, buffer, size);
436 if (err)
437 goto out;
438
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400439 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, size);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400440 /*
441 * Compatibility check for old Lustre ea_inode implementation. Old
442 * version does not have hash validation, but it has a backpointer
443 * from ea_inode to the parent inode.
444 */
445 if (err == -EFSCORRUPTED) {
446 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != inode->i_ino ||
447 ea_inode->i_generation != inode->i_generation) {
448 ext4_warning_inode(ea_inode,
449 "EA inode hash validation failed");
450 goto out;
451 }
452 /* Do not add ea_inode to the cache. */
453 ea_inode_cache = NULL;
Emoly Liu191eac32017-07-31 00:40:22 -0400454 err = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400455 } else if (err)
456 goto out;
457
458 if (ea_inode_cache)
459 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
460 ext4_xattr_inode_get_hash(ea_inode),
461 ea_inode->i_ino, true /* reusable */);
462out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400463 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400464 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400465}
466
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700467static int
Mingming Cao617ba132006-10-11 01:20:53 -0700468ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469 void *buffer, size_t buffer_size)
470{
471 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700472 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 size_t size;
474 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400475 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476
477 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
478 name_index, name, buffer, (long)buffer_size);
479
480 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700481 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400483 ea_idebug(inode, "reading block %llu",
484 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700485 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 if (!bh)
487 goto cleanup;
488 ea_bdebug(bh, "b_count=%d, refcount=%d",
489 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400490 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400491 EXT4_ERROR_INODE(inode, "bad block %llu",
492 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400493 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494 goto cleanup;
495 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400496 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400498 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 if (error)
500 goto cleanup;
501 size = le32_to_cpu(entry->e_value_size);
502 if (buffer) {
503 error = -ERANGE;
504 if (size > buffer_size)
505 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400506 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400507 error = ext4_xattr_inode_get(inode, entry, buffer,
508 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400509 if (error)
510 goto cleanup;
511 } else {
512 memcpy(buffer, bh->b_data +
513 le16_to_cpu(entry->e_value_offs), size);
514 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 }
516 error = size;
517
518cleanup:
519 brelse(bh);
520 return error;
521}
522
Tao Ma879b3822012-12-05 10:28:46 -0500523int
Mingming Cao617ba132006-10-11 01:20:53 -0700524ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 void *buffer, size_t buffer_size)
526{
Mingming Cao617ba132006-10-11 01:20:53 -0700527 struct ext4_xattr_ibody_header *header;
528 struct ext4_xattr_entry *entry;
529 struct ext4_inode *raw_inode;
530 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 size_t size;
532 void *end;
533 int error;
534
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500535 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700537 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 if (error)
539 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700540 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700542 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400543 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 if (error)
545 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400546 entry = IFIRST(header);
547 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 if (error)
549 goto cleanup;
550 size = le32_to_cpu(entry->e_value_size);
551 if (buffer) {
552 error = -ERANGE;
553 if (size > buffer_size)
554 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400555 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400556 error = ext4_xattr_inode_get(inode, entry, buffer,
557 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400558 if (error)
559 goto cleanup;
560 } else {
561 memcpy(buffer, (void *)IFIRST(header) +
562 le16_to_cpu(entry->e_value_offs), size);
563 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 }
565 error = size;
566
567cleanup:
568 brelse(iloc.bh);
569 return error;
570}
571
572/*
Mingming Cao617ba132006-10-11 01:20:53 -0700573 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574 *
575 * Copy an extended attribute into the buffer
576 * provided, or compute the buffer size required.
577 * Buffer is NULL to compute the size of the buffer required.
578 *
579 * Returns a negative error number on failure, or the number of bytes
580 * used / required on success.
581 */
582int
Mingming Cao617ba132006-10-11 01:20:53 -0700583ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584 void *buffer, size_t buffer_size)
585{
586 int error;
587
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500588 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
589 return -EIO;
590
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400591 if (strlen(name) > 255)
592 return -ERANGE;
593
Mingming Cao617ba132006-10-11 01:20:53 -0700594 down_read(&EXT4_I(inode)->xattr_sem);
595 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 buffer_size);
597 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700598 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700600 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601 return error;
602}
603
604static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000605ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 char *buffer, size_t buffer_size)
607{
608 size_t rest = buffer_size;
609
Mingming Cao617ba132006-10-11 01:20:53 -0700610 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700611 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700612 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100614 if (handler && (!handler->list || handler->list(dentry))) {
615 const char *prefix = handler->prefix ?: handler->name;
616 size_t prefix_len = strlen(prefix);
617 size_t size = prefix_len + entry->e_name_len + 1;
618
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 if (buffer) {
620 if (size > rest)
621 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100622 memcpy(buffer, prefix, prefix_len);
623 buffer += prefix_len;
624 memcpy(buffer, entry->e_name, entry->e_name_len);
625 buffer += entry->e_name_len;
626 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 }
628 rest -= size;
629 }
630 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100631 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632}
633
634static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000635ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636{
David Howells2b0143b2015-03-17 22:25:59 +0000637 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 struct buffer_head *bh = NULL;
639 int error;
640
641 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
642 buffer, (long)buffer_size);
643
644 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700645 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400647 ea_idebug(inode, "reading block %llu",
648 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700649 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 error = -EIO;
651 if (!bh)
652 goto cleanup;
653 ea_bdebug(bh, "b_count=%d, refcount=%d",
654 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400655 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400656 EXT4_ERROR_INODE(inode, "bad block %llu",
657 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400658 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 goto cleanup;
660 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400661 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000662 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663
664cleanup:
665 brelse(bh);
666
667 return error;
668}
669
670static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000671ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672{
David Howells2b0143b2015-03-17 22:25:59 +0000673 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700674 struct ext4_xattr_ibody_header *header;
675 struct ext4_inode *raw_inode;
676 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 void *end;
678 int error;
679
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500680 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700682 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 if (error)
684 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700685 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700687 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400688 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 if (error)
690 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000691 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 buffer, buffer_size);
693
694cleanup:
695 brelse(iloc.bh);
696 return error;
697}
698
699/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400700 * Inode operation listxattr()
701 *
702 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 *
704 * Copy a list of attribute names into the buffer
705 * provided, or compute the buffer size required.
706 * Buffer is NULL to compute the size of the buffer required.
707 *
708 * Returns a negative error number on failure, or the number of bytes
709 * used / required on success.
710 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400711ssize_t
712ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500714 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715
David Howells2b0143b2015-03-17 22:25:59 +0000716 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500717 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
718 if (ret < 0)
719 goto errout;
720 if (buffer) {
721 buffer += ret;
722 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500724 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
725 if (ret < 0)
726 goto errout;
727 ret += ret2;
728errout:
David Howells2b0143b2015-03-17 22:25:59 +0000729 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500730 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700731}
732
733/*
Mingming Cao617ba132006-10-11 01:20:53 -0700734 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735 * not set, set it.
736 */
Mingming Cao617ba132006-10-11 01:20:53 -0700737static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 struct super_block *sb)
739{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400740 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 return;
742
liang xie5d601252014-05-12 22:06:43 -0400743 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700744 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400745 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400746 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748}
749
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400750int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
751{
752 struct ext4_iloc iloc = { .bh = NULL };
753 struct buffer_head *bh = NULL;
754 struct ext4_inode *raw_inode;
755 struct ext4_xattr_ibody_header *header;
756 struct ext4_xattr_entry *entry;
757 qsize_t ea_inode_refs = 0;
758 void *end;
759 int ret;
760
761 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
762
763 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
764 ret = ext4_get_inode_loc(inode, &iloc);
765 if (ret)
766 goto out;
767 raw_inode = ext4_raw_inode(&iloc);
768 header = IHDR(inode, raw_inode);
769 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
770 ret = xattr_check_inode(inode, header, end);
771 if (ret)
772 goto out;
773
774 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
775 entry = EXT4_XATTR_NEXT(entry))
776 if (entry->e_value_inum)
777 ea_inode_refs++;
778 }
779
780 if (EXT4_I(inode)->i_file_acl) {
781 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
782 if (!bh) {
783 ret = -EIO;
784 goto out;
785 }
786
787 if (ext4_xattr_check_block(inode, bh)) {
788 ret = -EFSCORRUPTED;
789 goto out;
790 }
791
792 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
793 entry = EXT4_XATTR_NEXT(entry))
794 if (entry->e_value_inum)
795 ea_inode_refs++;
796 }
797 *usage = ea_inode_refs + 1;
798 ret = 0;
799out:
800 brelse(iloc.bh);
801 brelse(bh);
802 return ret;
803}
804
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400805static inline size_t round_up_cluster(struct inode *inode, size_t length)
806{
807 struct super_block *sb = inode->i_sb;
808 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
809 inode->i_blkbits);
810 size_t mask = ~(cluster_size - 1);
811
812 return (length + cluster_size - 1) & mask;
813}
814
815static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
816{
817 int err;
818
819 err = dquot_alloc_inode(inode);
820 if (err)
821 return err;
822 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
823 if (err)
824 dquot_free_inode(inode);
825 return err;
826}
827
828static void ext4_xattr_inode_free_quota(struct inode *inode, size_t len)
829{
830 dquot_free_space_nodirty(inode, round_up_cluster(inode, len));
831 dquot_free_inode(inode);
832}
833
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400834int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
835 struct buffer_head *block_bh, size_t value_len,
836 bool is_create)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400837{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400838 int credits;
839 int blocks;
840
841 /*
842 * 1) Owner inode update
843 * 2) Ref count update on old xattr block
844 * 3) new xattr block
845 * 4) block bitmap update for new xattr block
846 * 5) group descriptor for new xattr block
847 * 6) block bitmap update for old xattr block
848 * 7) group descriptor for old block
849 *
850 * 6 & 7 can happen if we have two racing threads T_a and T_b
851 * which are each trying to set an xattr on inodes I_a and I_b
852 * which were both initially sharing an xattr block.
853 */
854 credits = 7;
855
856 /* Quota updates. */
857 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
858
859 /*
860 * In case of inline data, we may push out the data to a block,
861 * so we need to reserve credits for this eventuality
862 */
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400863 if (inode && ext4_has_inline_data(inode))
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400864 credits += ext4_writepage_trans_blocks(inode) + 1;
865
866 /* We are done if ea_inode feature is not enabled. */
867 if (!ext4_has_feature_ea_inode(sb))
868 return credits;
869
870 /* New ea_inode, inode map, block bitmap, group descriptor. */
871 credits += 4;
872
873 /* Data blocks. */
874 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
875
876 /* Indirection block or one level of extent tree. */
877 blocks += 1;
878
879 /* Block bitmap and group descriptor updates for each block. */
880 credits += blocks * 2;
881
882 /* Blocks themselves. */
883 credits += blocks;
884
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400885 if (!is_create) {
886 /* Dereference ea_inode holding old xattr value.
887 * Old ea_inode, inode map, block bitmap, group descriptor.
888 */
889 credits += 4;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400890
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400891 /* Data blocks for old ea_inode. */
892 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400893
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400894 /* Indirection block or one level of extent tree for old
895 * ea_inode.
896 */
897 blocks += 1;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400898
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400899 /* Block bitmap and group descriptor updates for each block. */
900 credits += blocks * 2;
901 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400902
903 /* We may need to clone the existing xattr block in which case we need
904 * to increment ref counts for existing ea_inodes referenced by it.
905 */
906 if (block_bh) {
907 struct ext4_xattr_entry *entry = BFIRST(block_bh);
908
909 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
910 if (entry->e_value_inum)
911 /* Ref count update on ea_inode. */
912 credits += 1;
913 }
914 return credits;
915}
916
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400917static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
918 int credits, struct buffer_head *bh,
919 bool dirty, bool block_csum)
920{
921 int error;
922
923 if (!ext4_handle_valid(handle))
924 return 0;
925
926 if (handle->h_buffer_credits >= credits)
927 return 0;
928
929 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
930 if (!error)
931 return 0;
932 if (error < 0) {
933 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
934 return error;
935 }
936
937 if (bh && dirty) {
938 if (block_csum)
939 ext4_xattr_block_csum_set(inode, bh);
940 error = ext4_handle_dirty_metadata(handle, NULL, bh);
941 if (error) {
942 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
943 error);
944 return error;
945 }
946 }
947
948 error = ext4_journal_restart(handle, credits);
949 if (error) {
950 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
951 return error;
952 }
953
954 if (bh) {
955 error = ext4_journal_get_write_access(handle, bh);
956 if (error) {
957 ext4_warning(inode->i_sb,
958 "Get write access failed (error %d)",
959 error);
960 return error;
961 }
962 }
963 return 0;
964}
965
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400966static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
967 int ref_change)
968{
969 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
970 struct ext4_iloc iloc;
971 s64 ref_count;
972 u32 hash;
973 int ret;
974
975 inode_lock(ea_inode);
976
977 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
978 if (ret) {
979 iloc.bh = NULL;
980 goto out;
981 }
982
983 ref_count = ext4_xattr_inode_get_ref(ea_inode);
984 ref_count += ref_change;
985 ext4_xattr_inode_set_ref(ea_inode, ref_count);
986
987 if (ref_change > 0) {
988 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
989 ea_inode->i_ino, ref_count);
990
991 if (ref_count == 1) {
992 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
993 ea_inode->i_ino, ea_inode->i_nlink);
994
995 set_nlink(ea_inode, 1);
996 ext4_orphan_del(handle, ea_inode);
997
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -0400998 if (ea_inode_cache) {
999 hash = ext4_xattr_inode_get_hash(ea_inode);
1000 mb_cache_entry_create(ea_inode_cache,
1001 GFP_NOFS, hash,
1002 ea_inode->i_ino,
1003 true /* reusable */);
1004 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001005 }
1006 } else {
1007 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1008 ea_inode->i_ino, ref_count);
1009
1010 if (ref_count == 0) {
1011 WARN_ONCE(ea_inode->i_nlink != 1,
1012 "EA inode %lu i_nlink=%u",
1013 ea_inode->i_ino, ea_inode->i_nlink);
1014
1015 clear_nlink(ea_inode);
1016 ext4_orphan_add(handle, ea_inode);
1017
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001018 if (ea_inode_cache) {
1019 hash = ext4_xattr_inode_get_hash(ea_inode);
1020 mb_cache_entry_delete(ea_inode_cache, hash,
1021 ea_inode->i_ino);
1022 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001023 }
1024 }
1025
1026 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1027 iloc.bh = NULL;
1028 if (ret)
1029 ext4_warning_inode(ea_inode,
1030 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1031out:
1032 brelse(iloc.bh);
1033 inode_unlock(ea_inode);
1034 return ret;
1035}
1036
1037static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1038{
1039 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1040}
1041
1042static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1043{
1044 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1045}
1046
1047static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1048 struct ext4_xattr_entry *first)
1049{
1050 struct inode *ea_inode;
1051 struct ext4_xattr_entry *entry;
1052 struct ext4_xattr_entry *failed_entry;
1053 unsigned int ea_ino;
1054 int err, saved_err;
1055
1056 for (entry = first; !IS_LAST_ENTRY(entry);
1057 entry = EXT4_XATTR_NEXT(entry)) {
1058 if (!entry->e_value_inum)
1059 continue;
1060 ea_ino = le32_to_cpu(entry->e_value_inum);
1061 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1062 if (err)
1063 goto cleanup;
1064 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1065 if (err) {
1066 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1067 iput(ea_inode);
1068 goto cleanup;
1069 }
1070 iput(ea_inode);
1071 }
1072 return 0;
1073
1074cleanup:
1075 saved_err = err;
1076 failed_entry = entry;
1077
1078 for (entry = first; entry != failed_entry;
1079 entry = EXT4_XATTR_NEXT(entry)) {
1080 if (!entry->e_value_inum)
1081 continue;
1082 ea_ino = le32_to_cpu(entry->e_value_inum);
1083 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1084 if (err) {
1085 ext4_warning(parent->i_sb,
1086 "cleanup ea_ino %u iget error %d", ea_ino,
1087 err);
1088 continue;
1089 }
1090 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1091 if (err)
1092 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1093 err);
1094 iput(ea_inode);
1095 }
1096 return saved_err;
1097}
1098
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001099static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001100ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1101 struct buffer_head *bh,
1102 struct ext4_xattr_entry *first, bool block_csum,
1103 struct ext4_xattr_inode_array **ea_inode_array,
1104 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001105{
1106 struct inode *ea_inode;
1107 struct ext4_xattr_entry *entry;
1108 bool dirty = false;
1109 unsigned int ea_ino;
1110 int err;
1111 int credits;
1112
1113 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1114 credits = 2 + extra_credits;
1115
1116 for (entry = first; !IS_LAST_ENTRY(entry);
1117 entry = EXT4_XATTR_NEXT(entry)) {
1118 if (!entry->e_value_inum)
1119 continue;
1120 ea_ino = le32_to_cpu(entry->e_value_inum);
1121 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1122 if (err)
1123 continue;
1124
1125 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1126 if (err) {
1127 ext4_warning_inode(ea_inode,
1128 "Expand inode array err=%d", err);
1129 iput(ea_inode);
1130 continue;
1131 }
1132
1133 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1134 dirty, block_csum);
1135 if (err) {
1136 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1137 err);
1138 continue;
1139 }
1140
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001141 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1142 if (err) {
1143 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1144 err);
1145 continue;
1146 }
1147
1148 if (!skip_quota)
1149 ext4_xattr_inode_free_quota(parent,
1150 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001151
1152 /*
1153 * Forget about ea_inode within the same transaction that
1154 * decrements the ref count. This avoids duplicate decrements in
1155 * case the rest of the work spills over to subsequent
1156 * transactions.
1157 */
1158 entry->e_value_inum = 0;
1159 entry->e_value_size = 0;
1160
1161 dirty = true;
1162 }
1163
1164 if (dirty) {
1165 /*
1166 * Note that we are deliberately skipping csum calculation for
1167 * the final update because we do not expect any journal
1168 * restarts until xattr block is freed.
1169 */
1170
1171 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1172 if (err)
1173 ext4_warning_inode(parent,
1174 "handle dirty metadata err=%d", err);
1175 }
1176}
1177
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001179 * Release the xattr block BH: If the reference count is > 1, decrement it;
1180 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 */
1182static void
Mingming Cao617ba132006-10-11 01:20:53 -07001183ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001184 struct buffer_head *bh,
1185 struct ext4_xattr_inode_array **ea_inode_array,
1186 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001187{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001188 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001189 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001190 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191
liang xie5d601252014-05-12 22:06:43 -04001192 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001193 error = ext4_journal_get_write_access(handle, bh);
1194 if (error)
1195 goto out;
1196
1197 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001198 hash = le32_to_cpu(BHDR(bh)->h_hash);
1199 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1200 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001202 /*
1203 * This must happen under buffer lock for
1204 * ext4_xattr_block_set() to reliably detect freed block
1205 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001206 if (ea_block_cache)
1207 mb_cache_entry_delete(ea_block_cache, hash,
1208 bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001210 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001211
1212 if (ext4_has_feature_ea_inode(inode->i_sb))
1213 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1214 BFIRST(bh),
1215 true /* block_csum */,
1216 ea_inode_array,
1217 extra_credits,
1218 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001219 ext4_free_blocks(handle, inode, bh, 0, 1,
1220 EXT4_FREE_BLOCKS_METADATA |
1221 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001223 ref--;
1224 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1225 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1226 struct mb_cache_entry *ce;
1227
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001228 if (ea_block_cache) {
1229 ce = mb_cache_entry_get(ea_block_cache, hash,
1230 bh->b_blocknr);
1231 if (ce) {
1232 ce->e_reusable = 1;
1233 mb_cache_entry_put(ea_block_cache, ce);
1234 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001235 }
1236 }
1237
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001238 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001239 /*
1240 * Beware of this ugliness: Releasing of xattr block references
1241 * from different inodes can race and so we have to protect
1242 * from a race where someone else frees the block (and releases
1243 * its journal_head) before we are done dirtying the buffer. In
1244 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001245 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001246 * that function can call sync_dirty_buffer() so for that case
1247 * we handle the dirtying after unlocking the buffer.
1248 */
1249 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001250 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001251 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001252 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001253 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001254 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001255 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001256 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001257 ea_bdebug(bh, "refcount now=%d; releasing",
1258 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001260out:
1261 ext4_std_error(inode->i_sb, error);
1262 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263}
1264
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001265/*
1266 * Find the available free space for EAs. This also returns the total number of
1267 * bytes used by EA entries.
1268 */
1269static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1270 size_t *min_offs, void *base, int *total)
1271{
1272 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001273 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001274 size_t offs = le16_to_cpu(last->e_value_offs);
1275 if (offs < *min_offs)
1276 *min_offs = offs;
1277 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001278 if (total)
1279 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001280 }
1281 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1282}
1283
Andreas Dilgere50e5122017-06-21 21:10:32 -04001284/*
1285 * Write the value of the EA in an inode.
1286 */
1287static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1288 const void *buf, int bufsize)
1289{
1290 struct buffer_head *bh = NULL;
1291 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001292 int blocksize = ea_inode->i_sb->s_blocksize;
1293 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001294 int csize, wsize = 0;
1295 int ret = 0;
1296 int retries = 0;
1297
1298retry:
1299 while (ret >= 0 && ret < max_blocks) {
1300 struct ext4_map_blocks map;
1301 map.m_lblk = block += ret;
1302 map.m_len = max_blocks -= ret;
1303
1304 ret = ext4_map_blocks(handle, ea_inode, &map,
1305 EXT4_GET_BLOCKS_CREATE);
1306 if (ret <= 0) {
1307 ext4_mark_inode_dirty(handle, ea_inode);
1308 if (ret == -ENOSPC &&
1309 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1310 ret = 0;
1311 goto retry;
1312 }
1313 break;
1314 }
1315 }
1316
1317 if (ret < 0)
1318 return ret;
1319
1320 block = 0;
1321 while (wsize < bufsize) {
1322 if (bh != NULL)
1323 brelse(bh);
1324 csize = (bufsize - wsize) > blocksize ? blocksize :
1325 bufsize - wsize;
1326 bh = ext4_getblk(handle, ea_inode, block, 0);
1327 if (IS_ERR(bh))
1328 return PTR_ERR(bh);
1329 ret = ext4_journal_get_write_access(handle, bh);
1330 if (ret)
1331 goto out;
1332
1333 memcpy(bh->b_data, buf, csize);
1334 set_buffer_uptodate(bh);
1335 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1336
1337 buf += csize;
1338 wsize += csize;
1339 block += 1;
1340 }
1341
1342 inode_lock(ea_inode);
1343 i_size_write(ea_inode, wsize);
1344 ext4_update_i_disksize(ea_inode, wsize);
1345 inode_unlock(ea_inode);
1346
1347 ext4_mark_inode_dirty(handle, ea_inode);
1348
1349out:
1350 brelse(bh);
1351
1352 return ret;
1353}
1354
1355/*
1356 * Create an inode to store the value of a large EA.
1357 */
1358static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001359 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001360{
1361 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001362 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001363 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001364
1365 /*
1366 * Let the next inode be the goal, so we try and allocate the EA inode
1367 * in the same group, or nearby one.
1368 */
1369 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001370 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001371 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001372 if (!IS_ERR(ea_inode)) {
1373 ea_inode->i_op = &ext4_file_inode_operations;
1374 ea_inode->i_fop = &ext4_file_operations;
1375 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001376 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001377 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001378 ext4_xattr_inode_set_ref(ea_inode, 1);
1379 ext4_xattr_inode_set_hash(ea_inode, hash);
1380 err = ext4_mark_inode_dirty(handle, ea_inode);
1381 if (!err)
1382 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001383 if (err) {
1384 iput(ea_inode);
1385 return ERR_PTR(err);
1386 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001387
1388 /*
1389 * Xattr inodes are shared therefore quota charging is performed
1390 * at a higher level.
1391 */
1392 dquot_free_inode(ea_inode);
1393 dquot_drop(ea_inode);
1394 inode_lock(ea_inode);
1395 ea_inode->i_flags |= S_NOQUOTA;
1396 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001397 }
1398
1399 return ea_inode;
1400}
1401
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001402static struct inode *
1403ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1404 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001405{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001406 struct inode *ea_inode;
1407 struct mb_cache_entry *ce;
1408 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1409 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001410
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001411 if (!ea_inode_cache)
1412 return NULL;
1413
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001414 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1415 if (!ce)
1416 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001417
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001418 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1419 if (!ea_data) {
1420 mb_cache_entry_put(ea_inode_cache, ce);
1421 return NULL;
1422 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001423
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001424 while (ce) {
1425 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1426 if (!IS_ERR(ea_inode) &&
1427 !is_bad_inode(ea_inode) &&
1428 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1429 i_size_read(ea_inode) == value_len &&
1430 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001431 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1432 value_len) &&
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001433 !memcmp(value, ea_data, value_len)) {
1434 mb_cache_entry_touch(ea_inode_cache, ce);
1435 mb_cache_entry_put(ea_inode_cache, ce);
1436 kvfree(ea_data);
1437 return ea_inode;
1438 }
1439
1440 if (!IS_ERR(ea_inode))
1441 iput(ea_inode);
1442 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1443 }
1444 kvfree(ea_data);
1445 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001446}
1447
1448/*
1449 * Add value of the EA in an inode.
1450 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001451static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1452 const void *value, size_t value_len,
1453 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001454{
1455 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001456 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001457 int err;
1458
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001459 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1460 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1461 if (ea_inode) {
1462 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1463 if (err) {
1464 iput(ea_inode);
1465 return err;
1466 }
1467
1468 *ret_inode = ea_inode;
1469 return 0;
1470 }
1471
Andreas Dilgere50e5122017-06-21 21:10:32 -04001472 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001473 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001474 if (IS_ERR(ea_inode))
1475 return PTR_ERR(ea_inode);
1476
1477 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001478 if (err) {
1479 ext4_xattr_inode_dec_ref(handle, ea_inode);
1480 iput(ea_inode);
1481 return err;
1482 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001483
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001484 if (EA_INODE_CACHE(inode))
1485 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1486 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001487
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001488 *ret_inode = ea_inode;
1489 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001490}
1491
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001492/*
1493 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1494 * feature is enabled.
1495 */
1496#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1497
Andreas Dilgere50e5122017-06-21 21:10:32 -04001498static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1499 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001500 handle_t *handle, struct inode *inode,
1501 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001502{
Mingming Cao617ba132006-10-11 01:20:53 -07001503 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001504 struct ext4_xattr_entry *here = s->here;
1505 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001506 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001507 struct inode *old_ea_inode = NULL;
1508 struct inode *new_ea_inode = NULL;
1509 size_t old_size, new_size;
1510 int ret;
1511
1512 /* Space used by old and new values. */
1513 old_size = (!s->not_found && !here->e_value_inum) ?
1514 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1515 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1516
1517 /*
1518 * Optimization for the simple case when old and new values have the
1519 * same padded sizes. Not applicable if external inodes are involved.
1520 */
1521 if (new_size && new_size == old_size) {
1522 size_t offs = le16_to_cpu(here->e_value_offs);
1523 void *val = s->base + offs;
1524
1525 here->e_value_size = cpu_to_le32(i->value_len);
1526 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1527 memset(val, 0, new_size);
1528 } else {
1529 memcpy(val, i->value, i->value_len);
1530 /* Clear padding bytes. */
1531 memset(val + i->value_len, 0, new_size - i->value_len);
1532 }
1533 return 0;
1534 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001535
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 /* Compute min_offs and last. */
1537 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001538 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001539 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 size_t offs = le16_to_cpu(last->e_value_offs);
1541 if (offs < min_offs)
1542 min_offs = offs;
1543 }
1544 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001545
1546 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001548 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001549
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001550 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1551 if (!s->not_found)
1552 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001553
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001554 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1555 ret = -ENOSPC;
1556 goto out;
1557 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001558
1559 /*
1560 * If storing the value in an external inode is an option,
1561 * reserve space for xattr entries/names in the external
1562 * attribute block so that a long value does not occupy the
1563 * whole space and prevent futher entries being added.
1564 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001565 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1566 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001567 (min_offs + old_size - new_size) <
1568 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1569 ret = -ENOSPC;
1570 goto out;
1571 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572 }
1573
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001574 /*
1575 * Getting access to old and new ea inodes is subject to failures.
1576 * Finish that work before doing any modifications to the xattr data.
1577 */
1578 if (!s->not_found && here->e_value_inum) {
1579 ret = ext4_xattr_inode_iget(inode,
1580 le32_to_cpu(here->e_value_inum),
1581 &old_ea_inode);
1582 if (ret) {
1583 old_ea_inode = NULL;
1584 goto out;
1585 }
1586 }
1587 if (i->value && in_inode) {
1588 WARN_ON_ONCE(!i->value_len);
1589
1590 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1591 if (ret)
1592 goto out;
1593
1594 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1595 i->value_len,
1596 &new_ea_inode);
1597 if (ret) {
1598 new_ea_inode = NULL;
1599 ext4_xattr_inode_free_quota(inode, i->value_len);
1600 goto out;
1601 }
1602 }
1603
1604 if (old_ea_inode) {
1605 /* We are ready to release ref count on the old_ea_inode. */
1606 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1607 if (ret) {
1608 /* Release newly required ref count on new_ea_inode. */
1609 if (new_ea_inode) {
1610 int err;
1611
1612 err = ext4_xattr_inode_dec_ref(handle,
1613 new_ea_inode);
1614 if (err)
1615 ext4_warning_inode(new_ea_inode,
1616 "dec ref new_ea_inode err=%d",
1617 err);
1618 ext4_xattr_inode_free_quota(inode,
1619 i->value_len);
1620 }
1621 goto out;
1622 }
1623
1624 ext4_xattr_inode_free_quota(inode,
1625 le32_to_cpu(here->e_value_size));
1626 }
1627
1628 /* No failures allowed past this point. */
1629
1630 if (!s->not_found && here->e_value_offs) {
1631 /* Remove the old value. */
1632 void *first_val = s->base + min_offs;
1633 size_t offs = le16_to_cpu(here->e_value_offs);
1634 void *val = s->base + offs;
1635
1636 memmove(first_val + old_size, first_val, val - first_val);
1637 memset(first_val, 0, old_size);
1638 min_offs += old_size;
1639
1640 /* Adjust all value offsets. */
1641 last = s->first;
1642 while (!IS_LAST_ENTRY(last)) {
1643 size_t o = le16_to_cpu(last->e_value_offs);
1644
1645 if (!last->e_value_inum &&
1646 last->e_value_size && o < offs)
1647 last->e_value_offs = cpu_to_le16(o + old_size);
1648 last = EXT4_XATTR_NEXT(last);
1649 }
1650 }
1651
1652 if (!i->value) {
1653 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001654 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001655
1656 last = ENTRY((void *)last - size);
1657 memmove(here, (void *)here + size,
1658 (void *)last - (void *)here + sizeof(__u32));
1659 memset(last, 0, size);
1660 } else if (s->not_found) {
1661 /* Insert new name. */
1662 size_t size = EXT4_XATTR_LEN(name_len);
1663 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1664
1665 memmove((void *)here + size, here, rest);
1666 memset(here, 0, size);
1667 here->e_name_index = i->name_index;
1668 here->e_name_len = name_len;
1669 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001670 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001671 /* This is an update, reset value info. */
1672 here->e_value_inum = 0;
1673 here->e_value_offs = 0;
1674 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001675 }
1676
1677 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001678 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001679 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001680 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001681 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001682 void *val = s->base + min_offs - new_size;
1683
1684 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001685 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001686 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001687 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001688 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001689 /* Clear padding bytes. */
1690 memset(val + i->value_len, 0,
1691 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001692 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001694 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001696
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001697 if (i->value) {
1698 __le32 hash = 0;
1699
1700 /* Entry hash calculation. */
1701 if (in_inode) {
1702 __le32 crc32c_hash;
1703
1704 /*
1705 * Feed crc32c hash instead of the raw value for entry
1706 * hash calculation. This is to avoid walking
1707 * potentially long value buffer again.
1708 */
1709 crc32c_hash = cpu_to_le32(
1710 ext4_xattr_inode_get_hash(new_ea_inode));
1711 hash = ext4_xattr_hash_entry(here->e_name,
1712 here->e_name_len,
1713 &crc32c_hash, 1);
1714 } else if (is_block) {
1715 __le32 *value = s->base + min_offs - new_size;
1716
1717 hash = ext4_xattr_hash_entry(here->e_name,
1718 here->e_name_len, value,
1719 new_size >> 2);
1720 }
1721 here->e_hash = hash;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001722 }
1723
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001724 if (is_block)
1725 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1726
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001727 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001728out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001729 iput(old_ea_inode);
1730 iput(new_ea_inode);
1731 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732}
1733
Mingming Cao617ba132006-10-11 01:20:53 -07001734struct ext4_xattr_block_find {
1735 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736 struct buffer_head *bh;
1737};
1738
1739static int
Mingming Cao617ba132006-10-11 01:20:53 -07001740ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1741 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001742{
1743 struct super_block *sb = inode->i_sb;
1744 int error;
1745
1746 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1747 i->name_index, i->name, i->value, (long)i->value_len);
1748
Mingming Cao617ba132006-10-11 01:20:53 -07001749 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001751 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 error = -EIO;
1753 if (!bs->bh)
1754 goto cleanup;
1755 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1756 atomic_read(&(bs->bh->b_count)),
1757 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001758 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001759 EXT4_ERROR_INODE(inode, "bad block %llu",
1760 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001761 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762 goto cleanup;
1763 }
1764 /* Find the named attribute. */
1765 bs->s.base = BHDR(bs->bh);
1766 bs->s.first = BFIRST(bs->bh);
1767 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1768 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001769 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001770 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 if (error && error != -ENODATA)
1772 goto cleanup;
1773 bs->s.not_found = error;
1774 }
1775 error = 0;
1776
1777cleanup:
1778 return error;
1779}
1780
1781static int
Mingming Cao617ba132006-10-11 01:20:53 -07001782ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1783 struct ext4_xattr_info *i,
1784 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001785{
1786 struct super_block *sb = inode->i_sb;
1787 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001788 struct ext4_xattr_search s_copy = bs->s;
1789 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001790 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001791 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001792 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001793 struct inode *ea_inode = NULL;
1794 size_t old_ea_inode_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795
Mingming Cao617ba132006-10-11 01:20:53 -07001796#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001797
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001799 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001800 error = ext4_journal_get_write_access(handle, bs->bh);
1801 if (error)
1802 goto cleanup;
1803 lock_buffer(bs->bh);
1804
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001805 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001806 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1807
1808 /*
1809 * This must happen under buffer lock for
1810 * ext4_xattr_block_set() to reliably detect modified
1811 * block
1812 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001813 if (ea_block_cache)
1814 mb_cache_entry_delete(ea_block_cache, hash,
1815 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001816 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001817 error = ext4_xattr_set_entry(i, s, handle, inode,
1818 true /* is_block */);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001819 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001820 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001821 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001822 goto bad_block;
1823 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001824 error = ext4_handle_dirty_metadata(handle,
1825 inode,
1826 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001827 if (error)
1828 goto cleanup;
1829 goto inserted;
1830 } else {
1831 int offset = (char *)s->here - bs->bh->b_data;
1832
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001833 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001835 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 error = -ENOMEM;
1837 if (s->base == NULL)
1838 goto cleanup;
1839 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1840 s->first = ENTRY(header(s->base)+1);
1841 header(s->base)->h_refcount = cpu_to_le32(1);
1842 s->here = ENTRY(s->base + offset);
1843 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001844
1845 /*
1846 * If existing entry points to an xattr inode, we need
1847 * to prevent ext4_xattr_set_entry() from decrementing
1848 * ref count on it because the reference belongs to the
1849 * original block. In this case, make the entry look
1850 * like it has an empty value.
1851 */
1852 if (!s->not_found && s->here->e_value_inum) {
1853 /*
1854 * Defer quota free call for previous inode
1855 * until success is guaranteed.
1856 */
1857 old_ea_inode_size = le32_to_cpu(
1858 s->here->e_value_size);
1859 s->here->e_value_inum = 0;
1860 s->here->e_value_size = 0;
1861 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862 }
1863 } else {
1864 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001865 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 /* assert(header == s->base) */
1867 error = -ENOMEM;
1868 if (s->base == NULL)
1869 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001870 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001871 header(s->base)->h_blocks = cpu_to_le32(1);
1872 header(s->base)->h_refcount = cpu_to_le32(1);
1873 s->first = ENTRY(header(s->base)+1);
1874 s->here = ENTRY(header(s->base)+1);
1875 s->end = s->base + sb->s_blocksize;
1876 }
1877
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001878 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001879 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001880 goto bad_block;
1881 if (error)
1882 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001883
1884 if (i->value && s->here->e_value_inum) {
1885 unsigned int ea_ino;
1886
1887 /*
1888 * A ref count on ea_inode has been taken as part of the call to
1889 * ext4_xattr_set_entry() above. We would like to drop this
1890 * extra ref but we have to wait until the xattr block is
1891 * initialized and has its own ref count on the ea_inode.
1892 */
1893 ea_ino = le32_to_cpu(s->here->e_value_inum);
1894 error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
1895 if (error) {
1896 ea_inode = NULL;
1897 goto cleanup;
1898 }
1899 }
1900
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001901inserted:
1902 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001903 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1904 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001905 if (new_bh) {
1906 /* We found an identical block in the cache. */
1907 if (new_bh == bs->bh)
1908 ea_bdebug(new_bh, "keeping");
1909 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001910 u32 ref;
1911
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001912 WARN_ON_ONCE(dquot_initialize_needed(inode));
1913
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914 /* The old block is released after updating
1915 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001916 error = dquot_alloc_block(inode,
1917 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001918 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001920 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001921 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 new_bh);
1923 if (error)
1924 goto cleanup_dquot;
1925 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001926 /*
1927 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001928 * freeing, rehashing or adding references to
1929 * xattr block. Once we hold buffer lock xattr
1930 * block's state is stable so we can check
1931 * whether the block got freed / rehashed or
1932 * not. Since we unhash mbcache entry under
1933 * buffer lock when freeing / rehashing xattr
1934 * block, checking whether entry is still
1935 * hashed is reliable. Same rules hold for
1936 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001937 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001938 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1939 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001940 /*
1941 * Undo everything and check mbcache
1942 * again.
1943 */
1944 unlock_buffer(new_bh);
1945 dquot_free_block(inode,
1946 EXT4_C2B(EXT4_SB(sb),
1947 1));
1948 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04001949 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001950 ce = NULL;
1951 new_bh = NULL;
1952 goto inserted;
1953 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001954 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1955 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1956 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1957 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001958 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001959 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001960 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001962 error = ext4_handle_dirty_metadata(handle,
1963 inode,
1964 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965 if (error)
1966 goto cleanup_dquot;
1967 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04001968 mb_cache_entry_touch(ea_block_cache, ce);
1969 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001970 ce = NULL;
1971 } else if (bs->bh && s->base == bs->bh->b_data) {
1972 /* We were modifying this block in-place. */
1973 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganec000222017-08-05 22:41:42 -04001974 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001975 new_bh = bs->bh;
1976 get_bh(new_bh);
1977 } else {
1978 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001979 ext4_fsblk_t goal, block;
1980
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001981 WARN_ON_ONCE(dquot_initialize_needed(inode));
1982
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001983 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001984 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001985
1986 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001987 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001988 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1989
Allison Henderson55f020d2011-05-25 07:41:26 -04001990 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1991 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 if (error)
1993 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001994
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001995 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001996 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1997
Joe Perchesace36ad2012-03-19 23:11:43 -04001998 ea_idebug(inode, "creating block %llu",
1999 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000
2001 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05002002 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002003 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002004getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05002005 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002006 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 goto cleanup;
2008 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002009 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2010 ENTRY(header(s->base)+1));
2011 if (error)
2012 goto getblk_failed;
2013 if (ea_inode) {
2014 /* Drop the extra ref on ea_inode. */
2015 error = ext4_xattr_inode_dec_ref(handle,
2016 ea_inode);
2017 if (error)
2018 ext4_warning_inode(ea_inode,
2019 "dec ref error=%d",
2020 error);
2021 iput(ea_inode);
2022 ea_inode = NULL;
2023 }
2024
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002026 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027 if (error) {
2028 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002029 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 goto getblk_failed;
2031 }
2032 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002033 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 set_buffer_uptodate(new_bh);
2035 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002036 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002037 error = ext4_handle_dirty_metadata(handle, inode,
2038 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002039 if (error)
2040 goto cleanup;
2041 }
2042 }
2043
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002044 if (old_ea_inode_size)
2045 ext4_xattr_inode_free_quota(inode, old_ea_inode_size);
2046
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07002048 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002049
2050 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002051 if (bs->bh && bs->bh != new_bh) {
2052 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2053
2054 ext4_xattr_release_block(handle, inode, bs->bh,
2055 &ea_inode_array,
2056 0 /* extra_credits */);
2057 ext4_xattr_inode_array_free(ea_inode_array);
2058 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059 error = 0;
2060
2061cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002062 if (ea_inode) {
2063 int error2;
2064
2065 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2066 if (error2)
2067 ext4_warning_inode(ea_inode, "dec ref error=%d",
2068 error2);
2069
2070 /* If there was an error, revert the quota charge. */
2071 if (error)
2072 ext4_xattr_inode_free_quota(inode,
2073 i_size_read(ea_inode));
2074 iput(ea_inode);
2075 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002077 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002078 brelse(new_bh);
2079 if (!(bs->bh && s->base == bs->bh->b_data))
2080 kfree(s->base);
2081
2082 return error;
2083
2084cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002085 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086 goto cleanup;
2087
2088bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002089 EXT4_ERROR_INODE(inode, "bad block %llu",
2090 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 goto cleanup;
2092
2093#undef header
2094}
2095
Tao Ma879b3822012-12-05 10:28:46 -05002096int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2097 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002098{
Mingming Cao617ba132006-10-11 01:20:53 -07002099 struct ext4_xattr_ibody_header *header;
2100 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101 int error;
2102
Mingming Cao617ba132006-10-11 01:20:53 -07002103 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002105 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002106 header = IHDR(inode, raw_inode);
2107 is->s.base = is->s.first = IFIRST(header);
2108 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002109 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002110 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002111 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002112 if (error)
2113 return error;
2114 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07002115 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04002116 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117 if (error && error != -ENODATA)
2118 return error;
2119 is->s.not_found = error;
2120 }
2121 return 0;
2122}
2123
Tao Ma0d812f72012-12-10 14:06:02 -05002124int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2125 struct ext4_xattr_info *i,
2126 struct ext4_xattr_ibody_find *is)
2127{
2128 struct ext4_xattr_ibody_header *header;
2129 struct ext4_xattr_search *s = &is->s;
2130 int error;
2131
2132 if (EXT4_I(inode)->i_extra_isize == 0)
2133 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002134 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002135 if (error) {
2136 if (error == -ENOSPC &&
2137 ext4_has_inline_data(inode)) {
2138 error = ext4_try_to_evict_inline_data(handle, inode,
2139 EXT4_XATTR_LEN(strlen(i->name) +
2140 EXT4_XATTR_SIZE(i->value_len)));
2141 if (error)
2142 return error;
2143 error = ext4_xattr_ibody_find(inode, i, is);
2144 if (error)
2145 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002146 error = ext4_xattr_set_entry(i, s, handle, inode,
2147 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002148 }
2149 if (error)
2150 return error;
2151 }
2152 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2153 if (!IS_LAST_ENTRY(s->first)) {
2154 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2155 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2156 } else {
2157 header->h_magic = cpu_to_le32(0);
2158 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2159 }
2160 return 0;
2161}
2162
Andreas Dilgere50e5122017-06-21 21:10:32 -04002163static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002164 struct ext4_xattr_info *i,
2165 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166{
Mingming Cao617ba132006-10-11 01:20:53 -07002167 struct ext4_xattr_ibody_header *header;
2168 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 int error;
2170
Mingming Cao617ba132006-10-11 01:20:53 -07002171 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002173 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 if (error)
2175 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002176 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002177 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002178 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002179 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180 } else {
2181 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002182 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183 }
2184 return 0;
2185}
2186
Jan Kara3fd16462016-02-22 22:43:04 -05002187static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2188 struct ext4_xattr_info *i)
2189{
2190 void *value;
2191
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002192 /* When e_value_inum is set the value is stored externally. */
2193 if (s->here->e_value_inum)
2194 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002195 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2196 return 0;
2197 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2198 return !memcmp(value, i->value, i->value_len);
2199}
2200
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002201static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2202{
2203 struct buffer_head *bh;
2204 int error;
2205
2206 if (!EXT4_I(inode)->i_file_acl)
2207 return NULL;
2208 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2209 if (!bh)
2210 return ERR_PTR(-EIO);
2211 error = ext4_xattr_check_block(inode, bh);
2212 if (error)
2213 return ERR_PTR(error);
2214 return bh;
2215}
2216
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217/*
Mingming Cao617ba132006-10-11 01:20:53 -07002218 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002219 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002220 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221 * is NULL to remove an existing extended attribute, and non-NULL to
2222 * either replace an existing extended attribute, or create a new extended
2223 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2224 * specify that an extended attribute must exist and must not exist
2225 * previous to the call, respectively.
2226 *
2227 * Returns 0, or a negative error number on failure.
2228 */
2229int
Mingming Cao617ba132006-10-11 01:20:53 -07002230ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 const char *name, const void *value, size_t value_len,
2232 int flags)
2233{
Mingming Cao617ba132006-10-11 01:20:53 -07002234 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 .name_index = name_index,
2236 .name = name,
2237 .value = value,
2238 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002239 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002240 };
Mingming Cao617ba132006-10-11 01:20:53 -07002241 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 .s = { .not_found = -ENODATA, },
2243 };
Mingming Cao617ba132006-10-11 01:20:53 -07002244 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002245 .s = { .not_found = -ENODATA, },
2246 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002247 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002248 int error;
2249
2250 if (!name)
2251 return -EINVAL;
2252 if (strlen(name) > 255)
2253 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002254
Theodore Ts'oc755e252017-01-11 21:50:46 -05002255 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002256
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002257 /* Check journal credits under write lock. */
2258 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002259 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002260 int credits;
2261
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002262 bh = ext4_xattr_get_block(inode);
2263 if (IS_ERR(bh)) {
2264 error = PTR_ERR(bh);
2265 goto cleanup;
2266 }
2267
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002268 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2269 value_len,
2270 flags & XATTR_CREATE);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002271 brelse(bh);
2272
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002273 if (!ext4_handle_has_enough_credits(handle, credits)) {
2274 error = -ENOSPC;
2275 goto cleanup;
2276 }
2277 }
2278
Eric Sandeen66543612011-10-26 03:32:07 -04002279 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002280 if (error)
2281 goto cleanup;
2282
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002283 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002284 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2285 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002286 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 }
2288
Mingming Cao617ba132006-10-11 01:20:53 -07002289 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002290 if (error)
2291 goto cleanup;
2292 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002293 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002294 if (error)
2295 goto cleanup;
2296 if (is.s.not_found && bs.s.not_found) {
2297 error = -ENODATA;
2298 if (flags & XATTR_REPLACE)
2299 goto cleanup;
2300 error = 0;
2301 if (!value)
2302 goto cleanup;
2303 } else {
2304 error = -EEXIST;
2305 if (flags & XATTR_CREATE)
2306 goto cleanup;
2307 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002308
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 if (!value) {
2310 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002311 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002312 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002313 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002315 error = 0;
2316 /* Xattr value did not change? Save us some work and bail out */
2317 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2318 goto cleanup;
2319 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2320 goto cleanup;
2321
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002322 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2323 (EXT4_XATTR_SIZE(i.value_len) >
2324 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2325 i.in_inode = 1;
2326retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002327 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002328 if (!error && !bs.s.not_found) {
2329 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002330 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002331 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002332 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2333 error = ext4_xattr_block_find(inode, &i, &bs);
2334 if (error)
2335 goto cleanup;
2336 }
Mingming Cao617ba132006-10-11 01:20:53 -07002337 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002338 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002340 error = ext4_xattr_ibody_set(handle, inode, &i,
2341 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002342 } else if (error == -ENOSPC) {
2343 /*
2344 * Xattr does not fit in the block, store at
2345 * external inode if possible.
2346 */
2347 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2348 !i.in_inode) {
2349 i.in_inode = 1;
2350 goto retry_inode;
2351 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 }
2353 }
2354 }
2355 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002356 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002357 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002358 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002359 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002360 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002361 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002362 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002363 * error != 0.
2364 */
2365 is.iloc.bh = NULL;
2366 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002367 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002368 }
2369
2370cleanup:
2371 brelse(is.iloc.bh);
2372 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002373 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002374 return error;
2375}
2376
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002377int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2378 bool is_create, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002379{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002380 struct buffer_head *bh;
2381 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002382
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002383 *credits = 0;
2384
2385 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002386 return 0;
2387
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002388 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002389
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002390 bh = ext4_xattr_get_block(inode);
2391 if (IS_ERR(bh)) {
2392 err = PTR_ERR(bh);
2393 } else {
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002394 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2395 value_len, is_create);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002396 brelse(bh);
2397 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002398 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002399
2400 up_read(&EXT4_I(inode)->xattr_sem);
2401 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002402}
2403
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002404/*
Mingming Cao617ba132006-10-11 01:20:53 -07002405 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002406 *
Mingming Cao617ba132006-10-11 01:20:53 -07002407 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002408 * attribute modification is a filesystem transaction by itself.
2409 *
2410 * Returns 0, or a negative error number on failure.
2411 */
2412int
Mingming Cao617ba132006-10-11 01:20:53 -07002413ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 const void *value, size_t value_len, int flags)
2415{
2416 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002417 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002419 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002421 error = dquot_initialize(inode);
2422 if (error)
2423 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002424
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002425retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002426 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2427 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002428 if (error)
2429 return error;
2430
Theodore Ts'o9924a922013-02-08 21:59:22 -05002431 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002432 if (IS_ERR(handle)) {
2433 error = PTR_ERR(handle);
2434 } else {
2435 int error2;
2436
Mingming Cao617ba132006-10-11 01:20:53 -07002437 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002438 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002439 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002440 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002441 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002442 goto retry;
2443 if (error == 0)
2444 error = error2;
2445 }
2446
2447 return error;
2448}
2449
2450/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002451 * Shift the EA entries in the inode to create space for the increased
2452 * i_extra_isize.
2453 */
2454static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2455 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002456 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002457{
2458 struct ext4_xattr_entry *last = entry;
2459 int new_offs;
2460
Jan Kara94405712016-08-29 15:41:11 -04002461 /* We always shift xattr headers further thus offsets get lower */
2462 BUG_ON(value_offs_shift > 0);
2463
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002464 /* Adjust the value offsets of the entries */
2465 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002466 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002467 new_offs = le16_to_cpu(last->e_value_offs) +
2468 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002469 last->e_value_offs = cpu_to_le16(new_offs);
2470 }
2471 }
2472 /* Shift the entries by n bytes */
2473 memmove(to, from, n);
2474}
2475
2476/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002477 * Move xattr pointed to by 'entry' from inode into external xattr block
2478 */
2479static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2480 struct ext4_inode *raw_inode,
2481 struct ext4_xattr_entry *entry)
2482{
2483 struct ext4_xattr_ibody_find *is = NULL;
2484 struct ext4_xattr_block_find *bs = NULL;
2485 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002486 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002487 struct ext4_xattr_info i = {
2488 .value = NULL,
2489 .value_len = 0,
2490 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002491 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002492 };
2493 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2494 int error;
2495
Jan Kara3f2571c2016-08-29 15:42:11 -04002496 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2497 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2498 buffer = kmalloc(value_size, GFP_NOFS);
2499 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2500 if (!is || !bs || !buffer || !b_entry_name) {
2501 error = -ENOMEM;
2502 goto out;
2503 }
2504
2505 is->s.not_found = -ENODATA;
2506 bs->s.not_found = -ENODATA;
2507 is->iloc.bh = NULL;
2508 bs->bh = NULL;
2509
2510 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002511 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04002512 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002513 if (error)
2514 goto out;
2515 } else {
2516 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2517 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2518 }
2519
Jan Kara3f2571c2016-08-29 15:42:11 -04002520 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2521 b_entry_name[entry->e_name_len] = '\0';
2522 i.name = b_entry_name;
2523
2524 error = ext4_get_inode_loc(inode, &is->iloc);
2525 if (error)
2526 goto out;
2527
2528 error = ext4_xattr_ibody_find(inode, &i, is);
2529 if (error)
2530 goto out;
2531
2532 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002533 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002534 if (error)
2535 goto out;
2536
Jan Kara3f2571c2016-08-29 15:42:11 -04002537 i.value = buffer;
2538 i.value_len = value_size;
2539 error = ext4_xattr_block_find(inode, &i, bs);
2540 if (error)
2541 goto out;
2542
2543 /* Add entry which was removed from the inode into the block */
2544 error = ext4_xattr_block_set(handle, inode, &i, bs);
2545 if (error)
2546 goto out;
2547 error = 0;
2548out:
2549 kfree(b_entry_name);
2550 kfree(buffer);
2551 if (is)
2552 brelse(is->iloc.bh);
2553 kfree(is);
2554 kfree(bs);
2555
2556 return error;
2557}
2558
Jan Karadfa20642016-08-29 15:44:11 -04002559static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2560 struct ext4_inode *raw_inode,
2561 int isize_diff, size_t ifree,
2562 size_t bfree, int *total_ino)
2563{
2564 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2565 struct ext4_xattr_entry *small_entry;
2566 struct ext4_xattr_entry *entry;
2567 struct ext4_xattr_entry *last;
2568 unsigned int entry_size; /* EA entry size */
2569 unsigned int total_size; /* EA entry size + value size */
2570 unsigned int min_total_size;
2571 int error;
2572
2573 while (isize_diff > ifree) {
2574 entry = NULL;
2575 small_entry = NULL;
2576 min_total_size = ~0U;
2577 last = IFIRST(header);
2578 /* Find the entry best suited to be pushed into EA block */
2579 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002580 total_size = EXT4_XATTR_LEN(last->e_name_len);
2581 if (!last->e_value_inum)
2582 total_size += EXT4_XATTR_SIZE(
2583 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002584 if (total_size <= bfree &&
2585 total_size < min_total_size) {
2586 if (total_size + ifree < isize_diff) {
2587 small_entry = last;
2588 } else {
2589 entry = last;
2590 min_total_size = total_size;
2591 }
2592 }
2593 }
2594
2595 if (entry == NULL) {
2596 if (small_entry == NULL)
2597 return -ENOSPC;
2598 entry = small_entry;
2599 }
2600
2601 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002602 total_size = entry_size;
2603 if (!entry->e_value_inum)
2604 total_size += EXT4_XATTR_SIZE(
2605 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002606 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2607 entry);
2608 if (error)
2609 return error;
2610
2611 *total_ino -= entry_size;
2612 ifree += total_size;
2613 bfree -= total_size;
2614 }
2615
2616 return 0;
2617}
2618
Jan Kara3f2571c2016-08-29 15:42:11 -04002619/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002620 * Expand an inode by new_extra_isize bytes when EAs are present.
2621 * Returns 0 on success or negative error number on failure.
2622 */
2623int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2624 struct ext4_inode *raw_inode, handle_t *handle)
2625{
2626 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002627 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04002628 size_t min_offs;
2629 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002630 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002631 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002632 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04002633 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04002634 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002635 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002636
Theodore Ts'oc755e252017-01-11 21:50:46 -05002637 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
2638 return 0;
2639
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002640retry:
Jan Karad0141192016-08-11 11:50:30 -04002641 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002642 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2643 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002644
2645 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002646
2647 /*
2648 * Check if enough free space is available in the inode to shift the
2649 * entries ahead by new_extra_isize.
2650 */
2651
Jan Kara6e0cd082016-08-29 15:43:11 -04002652 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002653 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2654 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002655 total_ino = sizeof(struct ext4_xattr_ibody_header);
2656
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002657 error = xattr_check_inode(inode, header, end);
2658 if (error)
2659 goto cleanup;
2660
Jan Kara6e0cd082016-08-29 15:43:11 -04002661 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002662 if (ifree >= isize_diff)
2663 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002664
2665 /*
2666 * Enough free space isn't available in the inode, check if
2667 * EA block can hold new_extra_isize bytes.
2668 */
2669 if (EXT4_I(inode)->i_file_acl) {
2670 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2671 error = -EIO;
2672 if (!bh)
2673 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04002674 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002675 EXT4_ERROR_INODE(inode, "bad block %llu",
2676 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002677 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002678 goto cleanup;
2679 }
2680 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002681 end = bh->b_data + bh->b_size;
2682 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002683 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2684 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04002685 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002686 if (!tried_min_extra_isize && s_min_extra_isize) {
2687 tried_min_extra_isize++;
2688 new_extra_isize = s_min_extra_isize;
2689 brelse(bh);
2690 goto retry;
2691 }
Jan Karadfa20642016-08-29 15:44:11 -04002692 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002693 goto cleanup;
2694 }
2695 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002696 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002697 }
2698
Jan Karadfa20642016-08-29 15:44:11 -04002699 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2700 isize_diff, ifree, bfree,
2701 &total_ino);
2702 if (error) {
2703 if (error == -ENOSPC && !tried_min_extra_isize &&
2704 s_min_extra_isize) {
2705 tried_min_extra_isize++;
2706 new_extra_isize = s_min_extra_isize;
2707 brelse(bh);
2708 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002709 }
Jan Karadfa20642016-08-29 15:44:11 -04002710 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002711 }
Jan Karae3014d12016-08-29 15:38:11 -04002712shift:
2713 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002714 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002715 - new_extra_isize, (void *)raw_inode +
2716 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002717 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002718 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002719 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04002720out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05002721 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002722 return 0;
2723
2724cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002725 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04002726 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05002727 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04002728 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002729 no_expand = 1;
2730 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002731 return error;
2732}
2733
Andreas Dilgere50e5122017-06-21 21:10:32 -04002734#define EIA_INCR 16 /* must be 2^n */
2735#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002736
2737/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002738 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002739 * contents copied over.
2740 */
2741static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002742ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2743 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002744{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002745 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002746 /*
2747 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002748 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002749 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002750 (*ea_inode_array) =
2751 kmalloc(offsetof(struct ext4_xattr_inode_array,
2752 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002753 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002754 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002755 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002756 (*ea_inode_array)->count = 0;
2757 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002758 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002759 struct ext4_xattr_inode_array *new_array = NULL;
2760 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002761
2762 /* if new_array is NULL, this is essentially offsetof() */
2763 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002764 offsetof(struct ext4_xattr_inode_array,
2765 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002766 GFP_NOFS);
2767 if (new_array == NULL)
2768 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002769 memcpy(new_array, *ea_inode_array,
2770 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2771 kfree(*ea_inode_array);
2772 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002773 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002774 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002775 return 0;
2776}
2777
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002778/*
Mingming Cao617ba132006-10-11 01:20:53 -07002779 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002780 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002781 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002782 * all entries and decrement reference on any xattr inodes associated with this
2783 * inode. This is called immediately before an inode is freed. We have exclusive
2784 * access to the inode. If an orphan inode is deleted it will also release its
2785 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002787int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2788 struct ext4_xattr_inode_array **ea_inode_array,
2789 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002790{
2791 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002792 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002793 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002794 struct ext4_xattr_entry *entry;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002795 int error;
2796
2797 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2798 NULL /* bh */,
2799 false /* dirty */,
2800 false /* block_csum */);
2801 if (error) {
2802 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2803 goto cleanup;
2804 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002805
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002806 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2807 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002808
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002809 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002810 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002811 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002812 goto cleanup;
2813 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002814
2815 error = ext4_journal_get_write_access(handle, iloc.bh);
2816 if (error) {
2817 EXT4_ERROR_INODE(inode, "write access (error %d)",
2818 error);
2819 goto cleanup;
2820 }
2821
2822 header = IHDR(inode, ext4_raw_inode(&iloc));
2823 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2824 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2825 IFIRST(header),
2826 false /* block_csum */,
2827 ea_inode_array,
2828 extra_credits,
2829 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002830 }
2831
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002832 if (EXT4_I(inode)->i_file_acl) {
2833 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2834 if (!bh) {
2835 EXT4_ERROR_INODE(inode, "block %llu read error",
2836 EXT4_I(inode)->i_file_acl);
2837 error = -EIO;
2838 goto cleanup;
2839 }
2840 error = ext4_xattr_check_block(inode, bh);
2841 if (error) {
2842 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2843 EXT4_I(inode)->i_file_acl, error);
2844 goto cleanup;
2845 }
2846
2847 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2848 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2849 entry = EXT4_XATTR_NEXT(entry))
2850 if (entry->e_value_inum)
2851 ext4_xattr_inode_free_quota(inode,
2852 le32_to_cpu(entry->e_value_size));
2853
2854 }
2855
2856 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2857 extra_credits);
2858 /*
2859 * Update i_file_acl value in the same transaction that releases
2860 * block.
2861 */
2862 EXT4_I(inode)->i_file_acl = 0;
2863 error = ext4_mark_inode_dirty(handle, inode);
2864 if (error) {
2865 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2866 error);
2867 goto cleanup;
2868 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002869 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002870 error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002871cleanup:
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002872 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002874 return error;
2875}
2876
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002877void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002878{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002879 int idx;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002880
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002881 if (ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002882 return;
2883
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002884 for (idx = 0; idx < ea_inode_array->count; ++idx)
2885 iput(ea_inode_array->inodes[idx]);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002886 kfree(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002887}
2888
2889/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002890 * ext4_xattr_block_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002892 * Create a new entry in the extended attribute block cache, and insert
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893 * it unless such an entry is already in the cache.
2894 *
2895 * Returns 0, or a negative error number on failure.
2896 */
2897static void
Tahsin Erdogan47387402017-06-22 11:28:55 -04002898ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2899 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002901 struct ext4_xattr_header *header = BHDR(bh);
2902 __u32 hash = le32_to_cpu(header->h_hash);
2903 int reusable = le32_to_cpu(header->h_refcount) <
2904 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905 int error;
2906
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002907 if (!ea_block_cache)
2908 return;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002909 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002910 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002912 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002913 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002914 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916}
2917
2918/*
Mingming Cao617ba132006-10-11 01:20:53 -07002919 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002920 *
2921 * Compare two extended attribute blocks for equality.
2922 *
2923 * Returns 0 if the blocks are equal, 1 if they differ, and
2924 * a negative error number on errors.
2925 */
2926static int
Mingming Cao617ba132006-10-11 01:20:53 -07002927ext4_xattr_cmp(struct ext4_xattr_header *header1,
2928 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002929{
Mingming Cao617ba132006-10-11 01:20:53 -07002930 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931
2932 entry1 = ENTRY(header1+1);
2933 entry2 = ENTRY(header2+1);
2934 while (!IS_LAST_ENTRY(entry1)) {
2935 if (IS_LAST_ENTRY(entry2))
2936 return 1;
2937 if (entry1->e_hash != entry2->e_hash ||
2938 entry1->e_name_index != entry2->e_name_index ||
2939 entry1->e_name_len != entry2->e_name_len ||
2940 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002941 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002942 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2943 return 1;
Tahsin Erdogan7cec1912017-06-21 22:14:30 -04002944 if (!entry1->e_value_inum &&
2945 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002946 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2947 le32_to_cpu(entry1->e_value_size)))
2948 return 1;
2949
Mingming Cao617ba132006-10-11 01:20:53 -07002950 entry1 = EXT4_XATTR_NEXT(entry1);
2951 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002952 }
2953 if (!IS_LAST_ENTRY(entry2))
2954 return 1;
2955 return 0;
2956}
2957
2958/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002959 * ext4_xattr_block_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002960 *
2961 * Find an identical extended attribute block.
2962 *
2963 * Returns a pointer to the block found, or NULL if such a block was
2964 * not found or an error occurred.
2965 */
2966static struct buffer_head *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002967ext4_xattr_block_cache_find(struct inode *inode,
2968 struct ext4_xattr_header *header,
2969 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002970{
2971 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002972 struct mb_cache_entry *ce;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002973 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002975 if (!ea_block_cache)
2976 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002977 if (!header->h_hash)
2978 return NULL; /* never share */
2979 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002980 ce = mb_cache_entry_find_first(ea_block_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981 while (ce) {
2982 struct buffer_head *bh;
2983
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04002984 bh = sb_bread(inode->i_sb, ce->e_value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002985 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002986 EXT4_ERROR_INODE(inode, "block %lu read error",
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04002987 (unsigned long)ce->e_value);
Mingming Cao617ba132006-10-11 01:20:53 -07002988 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002989 *pce = ce;
2990 return bh;
2991 }
2992 brelse(bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002993 ce = mb_cache_entry_find_next(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002994 }
2995 return NULL;
2996}
2997
2998#define NAME_HASH_SHIFT 5
2999#define VALUE_HASH_SHIFT 16
3000
3001/*
Mingming Cao617ba132006-10-11 01:20:53 -07003002 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003 *
3004 * Compute the hash of an extended attribute.
3005 */
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003006static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3007 size_t value_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003008{
3009 __u32 hash = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003011 while (name_len--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 hash = (hash << NAME_HASH_SHIFT) ^
3013 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3014 *name++;
3015 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003016 while (value_count--) {
3017 hash = (hash << VALUE_HASH_SHIFT) ^
3018 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3019 le32_to_cpu(*value++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003020 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003021 return cpu_to_le32(hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022}
3023
3024#undef NAME_HASH_SHIFT
3025#undef VALUE_HASH_SHIFT
3026
3027#define BLOCK_HASH_SHIFT 16
3028
3029/*
Mingming Cao617ba132006-10-11 01:20:53 -07003030 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003031 *
3032 * Re-compute the extended attribute hash value after an entry has changed.
3033 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04003034static void ext4_xattr_rehash(struct ext4_xattr_header *header)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003035{
Mingming Cao617ba132006-10-11 01:20:53 -07003036 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037 __u32 hash = 0;
3038
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039 here = ENTRY(header+1);
3040 while (!IS_LAST_ENTRY(here)) {
3041 if (!here->e_hash) {
3042 /* Block is not shared if an entry's hash value == 0 */
3043 hash = 0;
3044 break;
3045 }
3046 hash = (hash << BLOCK_HASH_SHIFT) ^
3047 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3048 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07003049 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003050 }
3051 header->h_hash = cpu_to_le32(hash);
3052}
3053
3054#undef BLOCK_HASH_SHIFT
3055
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003056#define HASH_BUCKET_BITS 10
3057
Jan Kara7a2508e2016-02-22 22:35:22 -05003058struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05003059ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003060{
Jan Kara7a2508e2016-02-22 22:35:22 -05003061 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003062}
3063
Jan Kara7a2508e2016-02-22 22:35:22 -05003064void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003066 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05003067 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003069