blob: 3b69330a42506dc85bf5a51812326b862d499c50 [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{
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400320 int blocksize = 1 << ea_inode->i_blkbits;
321 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
322 int tail_size = (size % blocksize) ?: blocksize;
323 struct buffer_head *bhs_inline[8];
324 struct buffer_head **bhs = bhs_inline;
325 int i, ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400326
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400327 if (bh_count > ARRAY_SIZE(bhs_inline)) {
328 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
329 if (!bhs)
330 return -ENOMEM;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400331 }
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400332
333 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
334 true /* wait */, bhs);
335 if (ret)
336 goto free_bhs;
337
338 for (i = 0; i < bh_count; i++) {
339 /* There shouldn't be any holes in ea_inode. */
340 if (!bhs[i]) {
341 ret = -EFSCORRUPTED;
342 goto put_bhs;
343 }
344 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
345 i < bh_count - 1 ? blocksize : tail_size);
346 }
347 ret = 0;
348put_bhs:
349 for (i = 0; i < bh_count; i++)
350 brelse(bhs[i]);
351free_bhs:
352 if (bhs != bhs_inline)
353 kfree(bhs);
354 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400355}
356
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400357#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
358
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400359static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400360 u32 ea_inode_hash, struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400361{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400362 struct inode *inode;
363 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400364
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400365 inode = ext4_iget(parent->i_sb, ea_ino);
366 if (IS_ERR(inode)) {
367 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400368 ext4_error(parent->i_sb,
369 "error while reading EA inode %lu err=%d", ea_ino,
370 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400371 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400372 }
373
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400374 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400375 ext4_error(parent->i_sb,
376 "error while reading EA inode %lu is_bad_inode",
377 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400378 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400379 goto error;
380 }
381
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400382 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400383 ext4_error(parent->i_sb,
384 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
385 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400386 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400387 goto error;
388 }
389
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400390 ext4_xattr_inode_set_class(inode);
391
392 /*
393 * Check whether this is an old Lustre-style xattr inode. Lustre
394 * implementation does not have hash validation, rather it has a
395 * backpointer from ea_inode to the parent inode.
396 */
397 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
398 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
399 inode->i_generation == parent->i_generation) {
400 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
401 ext4_xattr_inode_set_ref(inode, 1);
402 } else {
403 inode_lock(inode);
404 inode->i_flags |= S_NOQUOTA;
405 inode_unlock(inode);
406 }
407
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400408 *ea_inode = inode;
409 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400410error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400411 iput(inode);
412 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400413}
414
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400415static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400416ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
417 struct ext4_xattr_entry *entry, void *buffer,
418 size_t size)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400419{
420 u32 hash;
421
422 /* Verify stored hash matches calculated hash. */
423 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
424 if (hash != ext4_xattr_inode_get_hash(ea_inode))
425 return -EFSCORRUPTED;
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400426
427 if (entry) {
428 __le32 e_hash, tmp_data;
429
430 /* Verify entry hash. */
431 tmp_data = cpu_to_le32(hash);
432 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
433 &tmp_data, 1);
434 if (e_hash != entry->e_hash)
435 return -EFSCORRUPTED;
436 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400437 return 0;
438}
439
Andreas Dilgere50e5122017-06-21 21:10:32 -0400440/*
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400441 * Read xattr value from the EA inode.
Andreas Dilgere50e5122017-06-21 21:10:32 -0400442 */
443static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400444ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
445 void *buffer, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400446{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400447 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400448 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400449 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400450
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400451 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400452 le32_to_cpu(entry->e_hash), &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400453 if (err) {
454 ea_inode = NULL;
455 goto out;
456 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400457
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400458 if (i_size_read(ea_inode) != size) {
459 ext4_warning_inode(ea_inode,
460 "ea_inode file size=%llu entry size=%zu",
461 i_size_read(ea_inode), size);
462 err = -EFSCORRUPTED;
463 goto out;
464 }
465
466 err = ext4_xattr_inode_read(ea_inode, buffer, size);
467 if (err)
468 goto out;
469
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400470 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
471 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
472 size);
473 if (err) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400474 ext4_warning_inode(ea_inode,
475 "EA inode hash validation failed");
476 goto out;
477 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400478
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400479 if (ea_inode_cache)
480 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
481 ext4_xattr_inode_get_hash(ea_inode),
482 ea_inode->i_ino, true /* reusable */);
483 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400484out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400485 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400486 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400487}
488
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489static int
Mingming Cao617ba132006-10-11 01:20:53 -0700490ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 void *buffer, size_t buffer_size)
492{
493 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700494 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 size_t size;
496 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400497 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498
499 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
500 name_index, name, buffer, (long)buffer_size);
501
502 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700503 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400505 ea_idebug(inode, "reading block %llu",
506 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700507 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 if (!bh)
509 goto cleanup;
510 ea_bdebug(bh, "b_count=%d, refcount=%d",
511 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400512 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400513 EXT4_ERROR_INODE(inode, "bad block %llu",
514 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400515 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 goto cleanup;
517 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400518 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400520 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521 if (error)
522 goto cleanup;
523 size = le32_to_cpu(entry->e_value_size);
524 if (buffer) {
525 error = -ERANGE;
526 if (size > buffer_size)
527 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400528 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400529 error = ext4_xattr_inode_get(inode, entry, buffer,
530 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400531 if (error)
532 goto cleanup;
533 } else {
534 memcpy(buffer, bh->b_data +
535 le16_to_cpu(entry->e_value_offs), size);
536 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 }
538 error = size;
539
540cleanup:
541 brelse(bh);
542 return error;
543}
544
Tao Ma879b3822012-12-05 10:28:46 -0500545int
Mingming Cao617ba132006-10-11 01:20:53 -0700546ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 void *buffer, size_t buffer_size)
548{
Mingming Cao617ba132006-10-11 01:20:53 -0700549 struct ext4_xattr_ibody_header *header;
550 struct ext4_xattr_entry *entry;
551 struct ext4_inode *raw_inode;
552 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 size_t size;
554 void *end;
555 int error;
556
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500557 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700559 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 if (error)
561 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700562 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700564 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400565 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566 if (error)
567 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400568 entry = IFIRST(header);
569 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 if (error)
571 goto cleanup;
572 size = le32_to_cpu(entry->e_value_size);
573 if (buffer) {
574 error = -ERANGE;
575 if (size > buffer_size)
576 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400577 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400578 error = ext4_xattr_inode_get(inode, entry, buffer,
579 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400580 if (error)
581 goto cleanup;
582 } else {
583 memcpy(buffer, (void *)IFIRST(header) +
584 le16_to_cpu(entry->e_value_offs), size);
585 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 }
587 error = size;
588
589cleanup:
590 brelse(iloc.bh);
591 return error;
592}
593
594/*
Mingming Cao617ba132006-10-11 01:20:53 -0700595 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 *
597 * Copy an extended attribute into the buffer
598 * provided, or compute the buffer size required.
599 * Buffer is NULL to compute the size of the buffer required.
600 *
601 * Returns a negative error number on failure, or the number of bytes
602 * used / required on success.
603 */
604int
Mingming Cao617ba132006-10-11 01:20:53 -0700605ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 void *buffer, size_t buffer_size)
607{
608 int error;
609
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500610 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
611 return -EIO;
612
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400613 if (strlen(name) > 255)
614 return -ERANGE;
615
Mingming Cao617ba132006-10-11 01:20:53 -0700616 down_read(&EXT4_I(inode)->xattr_sem);
617 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618 buffer_size);
619 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700620 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700622 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 return error;
624}
625
626static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000627ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 char *buffer, size_t buffer_size)
629{
630 size_t rest = buffer_size;
631
Mingming Cao617ba132006-10-11 01:20:53 -0700632 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700633 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700634 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100636 if (handler && (!handler->list || handler->list(dentry))) {
637 const char *prefix = handler->prefix ?: handler->name;
638 size_t prefix_len = strlen(prefix);
639 size_t size = prefix_len + entry->e_name_len + 1;
640
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641 if (buffer) {
642 if (size > rest)
643 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100644 memcpy(buffer, prefix, prefix_len);
645 buffer += prefix_len;
646 memcpy(buffer, entry->e_name, entry->e_name_len);
647 buffer += entry->e_name_len;
648 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 }
650 rest -= size;
651 }
652 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100653 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654}
655
656static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000657ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658{
David Howells2b0143b2015-03-17 22:25:59 +0000659 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 struct buffer_head *bh = NULL;
661 int error;
662
663 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
664 buffer, (long)buffer_size);
665
666 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700667 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400669 ea_idebug(inode, "reading block %llu",
670 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700671 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 error = -EIO;
673 if (!bh)
674 goto cleanup;
675 ea_bdebug(bh, "b_count=%d, refcount=%d",
676 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400677 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400678 EXT4_ERROR_INODE(inode, "bad block %llu",
679 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400680 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 goto cleanup;
682 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400683 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000684 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685
686cleanup:
687 brelse(bh);
688
689 return error;
690}
691
692static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000693ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694{
David Howells2b0143b2015-03-17 22:25:59 +0000695 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700696 struct ext4_xattr_ibody_header *header;
697 struct ext4_inode *raw_inode;
698 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 void *end;
700 int error;
701
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500702 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700704 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 if (error)
706 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700707 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700709 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400710 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711 if (error)
712 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000713 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 buffer, buffer_size);
715
716cleanup:
717 brelse(iloc.bh);
718 return error;
719}
720
721/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400722 * Inode operation listxattr()
723 *
724 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 *
726 * Copy a list of attribute names into the buffer
727 * provided, or compute the buffer size required.
728 * Buffer is NULL to compute the size of the buffer required.
729 *
730 * Returns a negative error number on failure, or the number of bytes
731 * used / required on success.
732 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400733ssize_t
734ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500736 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737
David Howells2b0143b2015-03-17 22:25:59 +0000738 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500739 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
740 if (ret < 0)
741 goto errout;
742 if (buffer) {
743 buffer += ret;
744 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500746 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
747 if (ret < 0)
748 goto errout;
749 ret += ret2;
750errout:
David Howells2b0143b2015-03-17 22:25:59 +0000751 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500752 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753}
754
755/*
Mingming Cao617ba132006-10-11 01:20:53 -0700756 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757 * not set, set it.
758 */
Mingming Cao617ba132006-10-11 01:20:53 -0700759static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 struct super_block *sb)
761{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400762 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700763 return;
764
liang xie5d601252014-05-12 22:06:43 -0400765 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700766 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400767 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400768 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770}
771
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400772int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
773{
774 struct ext4_iloc iloc = { .bh = NULL };
775 struct buffer_head *bh = NULL;
776 struct ext4_inode *raw_inode;
777 struct ext4_xattr_ibody_header *header;
778 struct ext4_xattr_entry *entry;
779 qsize_t ea_inode_refs = 0;
780 void *end;
781 int ret;
782
783 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
784
785 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
786 ret = ext4_get_inode_loc(inode, &iloc);
787 if (ret)
788 goto out;
789 raw_inode = ext4_raw_inode(&iloc);
790 header = IHDR(inode, raw_inode);
791 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
792 ret = xattr_check_inode(inode, header, end);
793 if (ret)
794 goto out;
795
796 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
797 entry = EXT4_XATTR_NEXT(entry))
798 if (entry->e_value_inum)
799 ea_inode_refs++;
800 }
801
802 if (EXT4_I(inode)->i_file_acl) {
803 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
804 if (!bh) {
805 ret = -EIO;
806 goto out;
807 }
808
809 if (ext4_xattr_check_block(inode, bh)) {
810 ret = -EFSCORRUPTED;
811 goto out;
812 }
813
814 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
815 entry = EXT4_XATTR_NEXT(entry))
816 if (entry->e_value_inum)
817 ea_inode_refs++;
818 }
819 *usage = ea_inode_refs + 1;
820 ret = 0;
821out:
822 brelse(iloc.bh);
823 brelse(bh);
824 return ret;
825}
826
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400827static inline size_t round_up_cluster(struct inode *inode, size_t length)
828{
829 struct super_block *sb = inode->i_sb;
830 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
831 inode->i_blkbits);
832 size_t mask = ~(cluster_size - 1);
833
834 return (length + cluster_size - 1) & mask;
835}
836
837static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
838{
839 int err;
840
841 err = dquot_alloc_inode(inode);
842 if (err)
843 return err;
844 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
845 if (err)
846 dquot_free_inode(inode);
847 return err;
848}
849
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400850static void ext4_xattr_inode_free_quota(struct inode *parent,
851 struct inode *ea_inode,
852 size_t len)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400853{
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400854 if (ea_inode &&
855 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
856 return;
857 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
858 dquot_free_inode(parent);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400859}
860
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400861int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
862 struct buffer_head *block_bh, size_t value_len,
863 bool is_create)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400864{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400865 int credits;
866 int blocks;
867
868 /*
869 * 1) Owner inode update
870 * 2) Ref count update on old xattr block
871 * 3) new xattr block
872 * 4) block bitmap update for new xattr block
873 * 5) group descriptor for new xattr block
874 * 6) block bitmap update for old xattr block
875 * 7) group descriptor for old block
876 *
877 * 6 & 7 can happen if we have two racing threads T_a and T_b
878 * which are each trying to set an xattr on inodes I_a and I_b
879 * which were both initially sharing an xattr block.
880 */
881 credits = 7;
882
883 /* Quota updates. */
884 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
885
886 /*
887 * In case of inline data, we may push out the data to a block,
888 * so we need to reserve credits for this eventuality
889 */
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400890 if (inode && ext4_has_inline_data(inode))
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400891 credits += ext4_writepage_trans_blocks(inode) + 1;
892
893 /* We are done if ea_inode feature is not enabled. */
894 if (!ext4_has_feature_ea_inode(sb))
895 return credits;
896
897 /* New ea_inode, inode map, block bitmap, group descriptor. */
898 credits += 4;
899
900 /* Data blocks. */
901 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
902
903 /* Indirection block or one level of extent tree. */
904 blocks += 1;
905
906 /* Block bitmap and group descriptor updates for each block. */
907 credits += blocks * 2;
908
909 /* Blocks themselves. */
910 credits += blocks;
911
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400912 if (!is_create) {
913 /* Dereference ea_inode holding old xattr value.
914 * Old ea_inode, inode map, block bitmap, group descriptor.
915 */
916 credits += 4;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400917
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400918 /* Data blocks for old ea_inode. */
919 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400920
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400921 /* Indirection block or one level of extent tree for old
922 * ea_inode.
923 */
924 blocks += 1;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400925
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400926 /* Block bitmap and group descriptor updates for each block. */
927 credits += blocks * 2;
928 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400929
930 /* We may need to clone the existing xattr block in which case we need
931 * to increment ref counts for existing ea_inodes referenced by it.
932 */
933 if (block_bh) {
934 struct ext4_xattr_entry *entry = BFIRST(block_bh);
935
936 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
937 if (entry->e_value_inum)
938 /* Ref count update on ea_inode. */
939 credits += 1;
940 }
941 return credits;
942}
943
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400944static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
945 int credits, struct buffer_head *bh,
946 bool dirty, bool block_csum)
947{
948 int error;
949
950 if (!ext4_handle_valid(handle))
951 return 0;
952
953 if (handle->h_buffer_credits >= credits)
954 return 0;
955
956 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
957 if (!error)
958 return 0;
959 if (error < 0) {
960 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
961 return error;
962 }
963
964 if (bh && dirty) {
965 if (block_csum)
966 ext4_xattr_block_csum_set(inode, bh);
967 error = ext4_handle_dirty_metadata(handle, NULL, bh);
968 if (error) {
969 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
970 error);
971 return error;
972 }
973 }
974
975 error = ext4_journal_restart(handle, credits);
976 if (error) {
977 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
978 return error;
979 }
980
981 if (bh) {
982 error = ext4_journal_get_write_access(handle, bh);
983 if (error) {
984 ext4_warning(inode->i_sb,
985 "Get write access failed (error %d)",
986 error);
987 return error;
988 }
989 }
990 return 0;
991}
992
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400993static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
994 int ref_change)
995{
996 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
997 struct ext4_iloc iloc;
998 s64 ref_count;
999 u32 hash;
1000 int ret;
1001
1002 inode_lock(ea_inode);
1003
1004 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1005 if (ret) {
1006 iloc.bh = NULL;
1007 goto out;
1008 }
1009
1010 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1011 ref_count += ref_change;
1012 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1013
1014 if (ref_change > 0) {
1015 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1016 ea_inode->i_ino, ref_count);
1017
1018 if (ref_count == 1) {
1019 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1020 ea_inode->i_ino, ea_inode->i_nlink);
1021
1022 set_nlink(ea_inode, 1);
1023 ext4_orphan_del(handle, ea_inode);
1024
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001025 if (ea_inode_cache) {
1026 hash = ext4_xattr_inode_get_hash(ea_inode);
1027 mb_cache_entry_create(ea_inode_cache,
1028 GFP_NOFS, hash,
1029 ea_inode->i_ino,
1030 true /* reusable */);
1031 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001032 }
1033 } else {
1034 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1035 ea_inode->i_ino, ref_count);
1036
1037 if (ref_count == 0) {
1038 WARN_ONCE(ea_inode->i_nlink != 1,
1039 "EA inode %lu i_nlink=%u",
1040 ea_inode->i_ino, ea_inode->i_nlink);
1041
1042 clear_nlink(ea_inode);
1043 ext4_orphan_add(handle, ea_inode);
1044
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001045 if (ea_inode_cache) {
1046 hash = ext4_xattr_inode_get_hash(ea_inode);
1047 mb_cache_entry_delete(ea_inode_cache, hash,
1048 ea_inode->i_ino);
1049 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001050 }
1051 }
1052
1053 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1054 iloc.bh = NULL;
1055 if (ret)
1056 ext4_warning_inode(ea_inode,
1057 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1058out:
1059 brelse(iloc.bh);
1060 inode_unlock(ea_inode);
1061 return ret;
1062}
1063
1064static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1065{
1066 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1067}
1068
1069static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1070{
1071 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1072}
1073
1074static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1075 struct ext4_xattr_entry *first)
1076{
1077 struct inode *ea_inode;
1078 struct ext4_xattr_entry *entry;
1079 struct ext4_xattr_entry *failed_entry;
1080 unsigned int ea_ino;
1081 int err, saved_err;
1082
1083 for (entry = first; !IS_LAST_ENTRY(entry);
1084 entry = EXT4_XATTR_NEXT(entry)) {
1085 if (!entry->e_value_inum)
1086 continue;
1087 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001088 err = ext4_xattr_inode_iget(parent, ea_ino,
1089 le32_to_cpu(entry->e_hash),
1090 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001091 if (err)
1092 goto cleanup;
1093 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1094 if (err) {
1095 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1096 iput(ea_inode);
1097 goto cleanup;
1098 }
1099 iput(ea_inode);
1100 }
1101 return 0;
1102
1103cleanup:
1104 saved_err = err;
1105 failed_entry = entry;
1106
1107 for (entry = first; entry != failed_entry;
1108 entry = EXT4_XATTR_NEXT(entry)) {
1109 if (!entry->e_value_inum)
1110 continue;
1111 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001112 err = ext4_xattr_inode_iget(parent, ea_ino,
1113 le32_to_cpu(entry->e_hash),
1114 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001115 if (err) {
1116 ext4_warning(parent->i_sb,
1117 "cleanup ea_ino %u iget error %d", ea_ino,
1118 err);
1119 continue;
1120 }
1121 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1122 if (err)
1123 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1124 err);
1125 iput(ea_inode);
1126 }
1127 return saved_err;
1128}
1129
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001130static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001131ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1132 struct buffer_head *bh,
1133 struct ext4_xattr_entry *first, bool block_csum,
1134 struct ext4_xattr_inode_array **ea_inode_array,
1135 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001136{
1137 struct inode *ea_inode;
1138 struct ext4_xattr_entry *entry;
1139 bool dirty = false;
1140 unsigned int ea_ino;
1141 int err;
1142 int credits;
1143
1144 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1145 credits = 2 + extra_credits;
1146
1147 for (entry = first; !IS_LAST_ENTRY(entry);
1148 entry = EXT4_XATTR_NEXT(entry)) {
1149 if (!entry->e_value_inum)
1150 continue;
1151 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001152 err = ext4_xattr_inode_iget(parent, ea_ino,
1153 le32_to_cpu(entry->e_hash),
1154 &ea_inode);
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001155 if (err)
1156 continue;
1157
1158 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1159 if (err) {
1160 ext4_warning_inode(ea_inode,
1161 "Expand inode array err=%d", err);
1162 iput(ea_inode);
1163 continue;
1164 }
1165
1166 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1167 dirty, block_csum);
1168 if (err) {
1169 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1170 err);
1171 continue;
1172 }
1173
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001174 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1175 if (err) {
1176 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1177 err);
1178 continue;
1179 }
1180
1181 if (!skip_quota)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001182 ext4_xattr_inode_free_quota(parent, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001183 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001184
1185 /*
1186 * Forget about ea_inode within the same transaction that
1187 * decrements the ref count. This avoids duplicate decrements in
1188 * case the rest of the work spills over to subsequent
1189 * transactions.
1190 */
1191 entry->e_value_inum = 0;
1192 entry->e_value_size = 0;
1193
1194 dirty = true;
1195 }
1196
1197 if (dirty) {
1198 /*
1199 * Note that we are deliberately skipping csum calculation for
1200 * the final update because we do not expect any journal
1201 * restarts until xattr block is freed.
1202 */
1203
1204 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1205 if (err)
1206 ext4_warning_inode(parent,
1207 "handle dirty metadata err=%d", err);
1208 }
1209}
1210
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001212 * Release the xattr block BH: If the reference count is > 1, decrement it;
1213 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001214 */
1215static void
Mingming Cao617ba132006-10-11 01:20:53 -07001216ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001217 struct buffer_head *bh,
1218 struct ext4_xattr_inode_array **ea_inode_array,
1219 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001221 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001222 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001223 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001224
liang xie5d601252014-05-12 22:06:43 -04001225 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001226 error = ext4_journal_get_write_access(handle, bh);
1227 if (error)
1228 goto out;
1229
1230 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001231 hash = le32_to_cpu(BHDR(bh)->h_hash);
1232 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1233 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001235 /*
1236 * This must happen under buffer lock for
1237 * ext4_xattr_block_set() to reliably detect freed block
1238 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001239 if (ea_block_cache)
1240 mb_cache_entry_delete(ea_block_cache, hash,
1241 bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001243 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001244
1245 if (ext4_has_feature_ea_inode(inode->i_sb))
1246 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1247 BFIRST(bh),
1248 true /* block_csum */,
1249 ea_inode_array,
1250 extra_credits,
1251 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001252 ext4_free_blocks(handle, inode, bh, 0, 1,
1253 EXT4_FREE_BLOCKS_METADATA |
1254 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001256 ref--;
1257 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1258 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1259 struct mb_cache_entry *ce;
1260
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001261 if (ea_block_cache) {
1262 ce = mb_cache_entry_get(ea_block_cache, hash,
1263 bh->b_blocknr);
1264 if (ce) {
1265 ce->e_reusable = 1;
1266 mb_cache_entry_put(ea_block_cache, ce);
1267 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001268 }
1269 }
1270
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001271 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001272 /*
1273 * Beware of this ugliness: Releasing of xattr block references
1274 * from different inodes can race and so we have to protect
1275 * from a race where someone else frees the block (and releases
1276 * its journal_head) before we are done dirtying the buffer. In
1277 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001278 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001279 * that function can call sync_dirty_buffer() so for that case
1280 * we handle the dirtying after unlocking the buffer.
1281 */
1282 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001283 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001284 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001285 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001286 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001287 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001288 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001289 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001290 ea_bdebug(bh, "refcount now=%d; releasing",
1291 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001293out:
1294 ext4_std_error(inode->i_sb, error);
1295 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296}
1297
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001298/*
1299 * Find the available free space for EAs. This also returns the total number of
1300 * bytes used by EA entries.
1301 */
1302static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1303 size_t *min_offs, void *base, int *total)
1304{
1305 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001306 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001307 size_t offs = le16_to_cpu(last->e_value_offs);
1308 if (offs < *min_offs)
1309 *min_offs = offs;
1310 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001311 if (total)
1312 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001313 }
1314 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1315}
1316
Andreas Dilgere50e5122017-06-21 21:10:32 -04001317/*
1318 * Write the value of the EA in an inode.
1319 */
1320static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1321 const void *buf, int bufsize)
1322{
1323 struct buffer_head *bh = NULL;
1324 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001325 int blocksize = ea_inode->i_sb->s_blocksize;
1326 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001327 int csize, wsize = 0;
1328 int ret = 0;
1329 int retries = 0;
1330
1331retry:
1332 while (ret >= 0 && ret < max_blocks) {
1333 struct ext4_map_blocks map;
1334 map.m_lblk = block += ret;
1335 map.m_len = max_blocks -= ret;
1336
1337 ret = ext4_map_blocks(handle, ea_inode, &map,
1338 EXT4_GET_BLOCKS_CREATE);
1339 if (ret <= 0) {
1340 ext4_mark_inode_dirty(handle, ea_inode);
1341 if (ret == -ENOSPC &&
1342 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1343 ret = 0;
1344 goto retry;
1345 }
1346 break;
1347 }
1348 }
1349
1350 if (ret < 0)
1351 return ret;
1352
1353 block = 0;
1354 while (wsize < bufsize) {
1355 if (bh != NULL)
1356 brelse(bh);
1357 csize = (bufsize - wsize) > blocksize ? blocksize :
1358 bufsize - wsize;
1359 bh = ext4_getblk(handle, ea_inode, block, 0);
1360 if (IS_ERR(bh))
1361 return PTR_ERR(bh);
1362 ret = ext4_journal_get_write_access(handle, bh);
1363 if (ret)
1364 goto out;
1365
1366 memcpy(bh->b_data, buf, csize);
1367 set_buffer_uptodate(bh);
1368 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1369
1370 buf += csize;
1371 wsize += csize;
1372 block += 1;
1373 }
1374
1375 inode_lock(ea_inode);
1376 i_size_write(ea_inode, wsize);
1377 ext4_update_i_disksize(ea_inode, wsize);
1378 inode_unlock(ea_inode);
1379
1380 ext4_mark_inode_dirty(handle, ea_inode);
1381
1382out:
1383 brelse(bh);
1384
1385 return ret;
1386}
1387
1388/*
1389 * Create an inode to store the value of a large EA.
1390 */
1391static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001392 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001393{
1394 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001395 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001396 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001397
1398 /*
1399 * Let the next inode be the goal, so we try and allocate the EA inode
1400 * in the same group, or nearby one.
1401 */
1402 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001403 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001404 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001405 if (!IS_ERR(ea_inode)) {
1406 ea_inode->i_op = &ext4_file_inode_operations;
1407 ea_inode->i_fop = &ext4_file_operations;
1408 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001409 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001410 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001411 ext4_xattr_inode_set_ref(ea_inode, 1);
1412 ext4_xattr_inode_set_hash(ea_inode, hash);
1413 err = ext4_mark_inode_dirty(handle, ea_inode);
1414 if (!err)
1415 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001416 if (err) {
1417 iput(ea_inode);
1418 return ERR_PTR(err);
1419 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001420
1421 /*
1422 * Xattr inodes are shared therefore quota charging is performed
1423 * at a higher level.
1424 */
1425 dquot_free_inode(ea_inode);
1426 dquot_drop(ea_inode);
1427 inode_lock(ea_inode);
1428 ea_inode->i_flags |= S_NOQUOTA;
1429 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001430 }
1431
1432 return ea_inode;
1433}
1434
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001435static struct inode *
1436ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1437 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001438{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001439 struct inode *ea_inode;
1440 struct mb_cache_entry *ce;
1441 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1442 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001443
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001444 if (!ea_inode_cache)
1445 return NULL;
1446
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001447 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1448 if (!ce)
1449 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001450
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001451 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1452 if (!ea_data) {
1453 mb_cache_entry_put(ea_inode_cache, ce);
1454 return NULL;
1455 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001456
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001457 while (ce) {
1458 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1459 if (!IS_ERR(ea_inode) &&
1460 !is_bad_inode(ea_inode) &&
1461 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1462 i_size_read(ea_inode) == value_len &&
1463 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001464 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1465 value_len) &&
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001466 !memcmp(value, ea_data, value_len)) {
1467 mb_cache_entry_touch(ea_inode_cache, ce);
1468 mb_cache_entry_put(ea_inode_cache, ce);
1469 kvfree(ea_data);
1470 return ea_inode;
1471 }
1472
1473 if (!IS_ERR(ea_inode))
1474 iput(ea_inode);
1475 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1476 }
1477 kvfree(ea_data);
1478 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001479}
1480
1481/*
1482 * Add value of the EA in an inode.
1483 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001484static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1485 const void *value, size_t value_len,
1486 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001487{
1488 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001489 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001490 int err;
1491
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001492 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1493 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1494 if (ea_inode) {
1495 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1496 if (err) {
1497 iput(ea_inode);
1498 return err;
1499 }
1500
1501 *ret_inode = ea_inode;
1502 return 0;
1503 }
1504
Andreas Dilgere50e5122017-06-21 21:10:32 -04001505 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001506 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001507 if (IS_ERR(ea_inode))
1508 return PTR_ERR(ea_inode);
1509
1510 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001511 if (err) {
1512 ext4_xattr_inode_dec_ref(handle, ea_inode);
1513 iput(ea_inode);
1514 return err;
1515 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001516
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001517 if (EA_INODE_CACHE(inode))
1518 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1519 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001520
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001521 *ret_inode = ea_inode;
1522 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001523}
1524
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001525/*
1526 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1527 * feature is enabled.
1528 */
1529#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1530
Andreas Dilgere50e5122017-06-21 21:10:32 -04001531static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1532 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001533 handle_t *handle, struct inode *inode,
1534 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535{
Mingming Cao617ba132006-10-11 01:20:53 -07001536 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001537 struct ext4_xattr_entry *here = s->here;
1538 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001539 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001540 struct inode *old_ea_inode = NULL;
1541 struct inode *new_ea_inode = NULL;
1542 size_t old_size, new_size;
1543 int ret;
1544
1545 /* Space used by old and new values. */
1546 old_size = (!s->not_found && !here->e_value_inum) ?
1547 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1548 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1549
1550 /*
1551 * Optimization for the simple case when old and new values have the
1552 * same padded sizes. Not applicable if external inodes are involved.
1553 */
1554 if (new_size && new_size == old_size) {
1555 size_t offs = le16_to_cpu(here->e_value_offs);
1556 void *val = s->base + offs;
1557
1558 here->e_value_size = cpu_to_le32(i->value_len);
1559 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1560 memset(val, 0, new_size);
1561 } else {
1562 memcpy(val, i->value, i->value_len);
1563 /* Clear padding bytes. */
1564 memset(val + i->value_len, 0, new_size - i->value_len);
1565 }
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001566 goto update_hash;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001567 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001568
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001569 /* Compute min_offs and last. */
1570 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001571 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001572 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 size_t offs = le16_to_cpu(last->e_value_offs);
1574 if (offs < min_offs)
1575 min_offs = offs;
1576 }
1577 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001578
1579 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001581 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001582
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001583 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1584 if (!s->not_found)
1585 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001586
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001587 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1588 ret = -ENOSPC;
1589 goto out;
1590 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001591
1592 /*
1593 * If storing the value in an external inode is an option,
1594 * reserve space for xattr entries/names in the external
1595 * attribute block so that a long value does not occupy the
1596 * whole space and prevent futher entries being added.
1597 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001598 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1599 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001600 (min_offs + old_size - new_size) <
1601 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1602 ret = -ENOSPC;
1603 goto out;
1604 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 }
1606
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001607 /*
1608 * Getting access to old and new ea inodes is subject to failures.
1609 * Finish that work before doing any modifications to the xattr data.
1610 */
1611 if (!s->not_found && here->e_value_inum) {
1612 ret = ext4_xattr_inode_iget(inode,
1613 le32_to_cpu(here->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001614 le32_to_cpu(here->e_hash),
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001615 &old_ea_inode);
1616 if (ret) {
1617 old_ea_inode = NULL;
1618 goto out;
1619 }
1620 }
1621 if (i->value && in_inode) {
1622 WARN_ON_ONCE(!i->value_len);
1623
1624 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1625 if (ret)
1626 goto out;
1627
1628 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1629 i->value_len,
1630 &new_ea_inode);
1631 if (ret) {
1632 new_ea_inode = NULL;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001633 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001634 goto out;
1635 }
1636 }
1637
1638 if (old_ea_inode) {
1639 /* We are ready to release ref count on the old_ea_inode. */
1640 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1641 if (ret) {
1642 /* Release newly required ref count on new_ea_inode. */
1643 if (new_ea_inode) {
1644 int err;
1645
1646 err = ext4_xattr_inode_dec_ref(handle,
1647 new_ea_inode);
1648 if (err)
1649 ext4_warning_inode(new_ea_inode,
1650 "dec ref new_ea_inode err=%d",
1651 err);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001652 ext4_xattr_inode_free_quota(inode, new_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001653 i->value_len);
1654 }
1655 goto out;
1656 }
1657
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001658 ext4_xattr_inode_free_quota(inode, old_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001659 le32_to_cpu(here->e_value_size));
1660 }
1661
1662 /* No failures allowed past this point. */
1663
1664 if (!s->not_found && here->e_value_offs) {
1665 /* Remove the old value. */
1666 void *first_val = s->base + min_offs;
1667 size_t offs = le16_to_cpu(here->e_value_offs);
1668 void *val = s->base + offs;
1669
1670 memmove(first_val + old_size, first_val, val - first_val);
1671 memset(first_val, 0, old_size);
1672 min_offs += old_size;
1673
1674 /* Adjust all value offsets. */
1675 last = s->first;
1676 while (!IS_LAST_ENTRY(last)) {
1677 size_t o = le16_to_cpu(last->e_value_offs);
1678
1679 if (!last->e_value_inum &&
1680 last->e_value_size && o < offs)
1681 last->e_value_offs = cpu_to_le16(o + old_size);
1682 last = EXT4_XATTR_NEXT(last);
1683 }
1684 }
1685
1686 if (!i->value) {
1687 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001688 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001689
1690 last = ENTRY((void *)last - size);
1691 memmove(here, (void *)here + size,
1692 (void *)last - (void *)here + sizeof(__u32));
1693 memset(last, 0, size);
1694 } else if (s->not_found) {
1695 /* Insert new name. */
1696 size_t size = EXT4_XATTR_LEN(name_len);
1697 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1698
1699 memmove((void *)here + size, here, rest);
1700 memset(here, 0, size);
1701 here->e_name_index = i->name_index;
1702 here->e_name_len = name_len;
1703 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001704 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001705 /* This is an update, reset value info. */
1706 here->e_value_inum = 0;
1707 here->e_value_offs = 0;
1708 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 }
1710
1711 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001712 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001713 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001714 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001715 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001716 void *val = s->base + min_offs - new_size;
1717
1718 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001719 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001720 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001721 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001722 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001723 /* Clear padding bytes. */
1724 memset(val + i->value_len, 0,
1725 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001726 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001728 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001730
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001731update_hash:
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001732 if (i->value) {
1733 __le32 hash = 0;
1734
1735 /* Entry hash calculation. */
1736 if (in_inode) {
1737 __le32 crc32c_hash;
1738
1739 /*
1740 * Feed crc32c hash instead of the raw value for entry
1741 * hash calculation. This is to avoid walking
1742 * potentially long value buffer again.
1743 */
1744 crc32c_hash = cpu_to_le32(
1745 ext4_xattr_inode_get_hash(new_ea_inode));
1746 hash = ext4_xattr_hash_entry(here->e_name,
1747 here->e_name_len,
1748 &crc32c_hash, 1);
1749 } else if (is_block) {
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001750 __le32 *value = s->base + le16_to_cpu(
1751 here->e_value_offs);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001752
1753 hash = ext4_xattr_hash_entry(here->e_name,
1754 here->e_name_len, value,
1755 new_size >> 2);
1756 }
1757 here->e_hash = hash;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001758 }
1759
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001760 if (is_block)
1761 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1762
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001763 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001764out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001765 iput(old_ea_inode);
1766 iput(new_ea_inode);
1767 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768}
1769
Mingming Cao617ba132006-10-11 01:20:53 -07001770struct ext4_xattr_block_find {
1771 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772 struct buffer_head *bh;
1773};
1774
1775static int
Mingming Cao617ba132006-10-11 01:20:53 -07001776ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1777 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001778{
1779 struct super_block *sb = inode->i_sb;
1780 int error;
1781
1782 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1783 i->name_index, i->name, i->value, (long)i->value_len);
1784
Mingming Cao617ba132006-10-11 01:20:53 -07001785 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001786 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001787 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 error = -EIO;
1789 if (!bs->bh)
1790 goto cleanup;
1791 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1792 atomic_read(&(bs->bh->b_count)),
1793 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001794 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001795 EXT4_ERROR_INODE(inode, "bad block %llu",
1796 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001797 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 goto cleanup;
1799 }
1800 /* Find the named attribute. */
1801 bs->s.base = BHDR(bs->bh);
1802 bs->s.first = BFIRST(bs->bh);
1803 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1804 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001805 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001806 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807 if (error && error != -ENODATA)
1808 goto cleanup;
1809 bs->s.not_found = error;
1810 }
1811 error = 0;
1812
1813cleanup:
1814 return error;
1815}
1816
1817static int
Mingming Cao617ba132006-10-11 01:20:53 -07001818ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1819 struct ext4_xattr_info *i,
1820 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821{
1822 struct super_block *sb = inode->i_sb;
1823 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001824 struct ext4_xattr_search s_copy = bs->s;
1825 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001826 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001827 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001828 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001829 struct inode *ea_inode = NULL, *tmp_inode;
1830 size_t old_ea_inode_quota = 0;
1831 unsigned int ea_ino;
1832
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001833
Mingming Cao617ba132006-10-11 01:20:53 -07001834#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001835
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001837 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001838 error = ext4_journal_get_write_access(handle, bs->bh);
1839 if (error)
1840 goto cleanup;
1841 lock_buffer(bs->bh);
1842
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001843 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001844 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1845
1846 /*
1847 * This must happen under buffer lock for
1848 * ext4_xattr_block_set() to reliably detect modified
1849 * block
1850 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001851 if (ea_block_cache)
1852 mb_cache_entry_delete(ea_block_cache, hash,
1853 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001855 error = ext4_xattr_set_entry(i, s, handle, inode,
1856 true /* is_block */);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001857 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001858 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001859 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860 goto bad_block;
1861 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001862 error = ext4_handle_dirty_metadata(handle,
1863 inode,
1864 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 if (error)
1866 goto cleanup;
1867 goto inserted;
1868 } else {
1869 int offset = (char *)s->here - bs->bh->b_data;
1870
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001871 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001873 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001874 error = -ENOMEM;
1875 if (s->base == NULL)
1876 goto cleanup;
1877 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1878 s->first = ENTRY(header(s->base)+1);
1879 header(s->base)->h_refcount = cpu_to_le32(1);
1880 s->here = ENTRY(s->base + offset);
1881 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001882
1883 /*
1884 * If existing entry points to an xattr inode, we need
1885 * to prevent ext4_xattr_set_entry() from decrementing
1886 * ref count on it because the reference belongs to the
1887 * original block. In this case, make the entry look
1888 * like it has an empty value.
1889 */
1890 if (!s->not_found && s->here->e_value_inum) {
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001891 ea_ino = le32_to_cpu(s->here->e_value_inum);
1892 error = ext4_xattr_inode_iget(inode, ea_ino,
1893 le32_to_cpu(s->here->e_hash),
1894 &tmp_inode);
1895 if (error)
1896 goto cleanup;
1897
1898 if (!ext4_test_inode_state(tmp_inode,
1899 EXT4_STATE_LUSTRE_EA_INODE)) {
1900 /*
1901 * Defer quota free call for previous
1902 * inode until success is guaranteed.
1903 */
1904 old_ea_inode_quota = le32_to_cpu(
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001905 s->here->e_value_size);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001906 }
1907 iput(tmp_inode);
1908
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001909 s->here->e_value_inum = 0;
1910 s->here->e_value_size = 0;
1911 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001912 }
1913 } else {
1914 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001915 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001916 /* assert(header == s->base) */
1917 error = -ENOMEM;
1918 if (s->base == NULL)
1919 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001920 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001921 header(s->base)->h_blocks = cpu_to_le32(1);
1922 header(s->base)->h_refcount = cpu_to_le32(1);
1923 s->first = ENTRY(header(s->base)+1);
1924 s->here = ENTRY(header(s->base)+1);
1925 s->end = s->base + sb->s_blocksize;
1926 }
1927
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001928 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001929 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 goto bad_block;
1931 if (error)
1932 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001933
1934 if (i->value && s->here->e_value_inum) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001935 /*
1936 * A ref count on ea_inode has been taken as part of the call to
1937 * ext4_xattr_set_entry() above. We would like to drop this
1938 * extra ref but we have to wait until the xattr block is
1939 * initialized and has its own ref count on the ea_inode.
1940 */
1941 ea_ino = le32_to_cpu(s->here->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001942 error = ext4_xattr_inode_iget(inode, ea_ino,
1943 le32_to_cpu(s->here->e_hash),
1944 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001945 if (error) {
1946 ea_inode = NULL;
1947 goto cleanup;
1948 }
1949 }
1950
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001951inserted:
1952 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001953 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1954 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001955 if (new_bh) {
1956 /* We found an identical block in the cache. */
1957 if (new_bh == bs->bh)
1958 ea_bdebug(new_bh, "keeping");
1959 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001960 u32 ref;
1961
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001962 WARN_ON_ONCE(dquot_initialize_needed(inode));
1963
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001964 /* The old block is released after updating
1965 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001966 error = dquot_alloc_block(inode,
1967 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001968 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001970 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001971 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 new_bh);
1973 if (error)
1974 goto cleanup_dquot;
1975 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001976 /*
1977 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001978 * freeing, rehashing or adding references to
1979 * xattr block. Once we hold buffer lock xattr
1980 * block's state is stable so we can check
1981 * whether the block got freed / rehashed or
1982 * not. Since we unhash mbcache entry under
1983 * buffer lock when freeing / rehashing xattr
1984 * block, checking whether entry is still
1985 * hashed is reliable. Same rules hold for
1986 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001987 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001988 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1989 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001990 /*
1991 * Undo everything and check mbcache
1992 * again.
1993 */
1994 unlock_buffer(new_bh);
1995 dquot_free_block(inode,
1996 EXT4_C2B(EXT4_SB(sb),
1997 1));
1998 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04001999 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05002000 ce = NULL;
2001 new_bh = NULL;
2002 goto inserted;
2003 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002004 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2005 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2006 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
2007 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002008 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002009 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002010 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002012 error = ext4_handle_dirty_metadata(handle,
2013 inode,
2014 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002015 if (error)
2016 goto cleanup_dquot;
2017 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04002018 mb_cache_entry_touch(ea_block_cache, ce);
2019 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020 ce = NULL;
2021 } else if (bs->bh && s->base == bs->bh->b_data) {
2022 /* We were modifying this block in-place. */
2023 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganec000222017-08-05 22:41:42 -04002024 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 new_bh = bs->bh;
2026 get_bh(new_bh);
2027 } else {
2028 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002029 ext4_fsblk_t goal, block;
2030
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002031 WARN_ON_ONCE(dquot_initialize_needed(inode));
2032
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002033 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04002034 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002035
2036 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002037 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002038 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
2039
Allison Henderson55f020d2011-05-25 07:41:26 -04002040 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2041 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002042 if (error)
2043 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002044
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002045 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002046 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
2047
Joe Perchesace36ad2012-03-19 23:11:43 -04002048 ea_idebug(inode, "creating block %llu",
2049 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002050
2051 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05002052 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002053 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05002055 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002056 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 goto cleanup;
2058 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002059 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2060 ENTRY(header(s->base)+1));
2061 if (error)
2062 goto getblk_failed;
2063 if (ea_inode) {
2064 /* Drop the extra ref on ea_inode. */
2065 error = ext4_xattr_inode_dec_ref(handle,
2066 ea_inode);
2067 if (error)
2068 ext4_warning_inode(ea_inode,
2069 "dec ref error=%d",
2070 error);
2071 iput(ea_inode);
2072 ea_inode = NULL;
2073 }
2074
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002076 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077 if (error) {
2078 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002079 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002080 goto getblk_failed;
2081 }
2082 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002083 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002084 set_buffer_uptodate(new_bh);
2085 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002086 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002087 error = ext4_handle_dirty_metadata(handle, inode,
2088 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 if (error)
2090 goto cleanup;
2091 }
2092 }
2093
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002094 if (old_ea_inode_quota)
2095 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002096
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002097 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07002098 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099
2100 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002101 if (bs->bh && bs->bh != new_bh) {
2102 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2103
2104 ext4_xattr_release_block(handle, inode, bs->bh,
2105 &ea_inode_array,
2106 0 /* extra_credits */);
2107 ext4_xattr_inode_array_free(ea_inode_array);
2108 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109 error = 0;
2110
2111cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002112 if (ea_inode) {
2113 int error2;
2114
2115 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2116 if (error2)
2117 ext4_warning_inode(ea_inode, "dec ref error=%d",
2118 error2);
2119
2120 /* If there was an error, revert the quota charge. */
2121 if (error)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002122 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002123 i_size_read(ea_inode));
2124 iput(ea_inode);
2125 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002126 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002127 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128 brelse(new_bh);
2129 if (!(bs->bh && s->base == bs->bh->b_data))
2130 kfree(s->base);
2131
2132 return error;
2133
2134cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002135 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002136 goto cleanup;
2137
2138bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002139 EXT4_ERROR_INODE(inode, "bad block %llu",
2140 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 goto cleanup;
2142
2143#undef header
2144}
2145
Tao Ma879b3822012-12-05 10:28:46 -05002146int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2147 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148{
Mingming Cao617ba132006-10-11 01:20:53 -07002149 struct ext4_xattr_ibody_header *header;
2150 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151 int error;
2152
Mingming Cao617ba132006-10-11 01:20:53 -07002153 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002154 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002155 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002156 header = IHDR(inode, raw_inode);
2157 is->s.base = is->s.first = IFIRST(header);
2158 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002159 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002160 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002161 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002162 if (error)
2163 return error;
2164 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07002165 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04002166 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002167 if (error && error != -ENODATA)
2168 return error;
2169 is->s.not_found = error;
2170 }
2171 return 0;
2172}
2173
Tao Ma0d812f72012-12-10 14:06:02 -05002174int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2175 struct ext4_xattr_info *i,
2176 struct ext4_xattr_ibody_find *is)
2177{
2178 struct ext4_xattr_ibody_header *header;
2179 struct ext4_xattr_search *s = &is->s;
2180 int error;
2181
2182 if (EXT4_I(inode)->i_extra_isize == 0)
2183 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002184 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002185 if (error) {
2186 if (error == -ENOSPC &&
2187 ext4_has_inline_data(inode)) {
2188 error = ext4_try_to_evict_inline_data(handle, inode,
2189 EXT4_XATTR_LEN(strlen(i->name) +
2190 EXT4_XATTR_SIZE(i->value_len)));
2191 if (error)
2192 return error;
2193 error = ext4_xattr_ibody_find(inode, i, is);
2194 if (error)
2195 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002196 error = ext4_xattr_set_entry(i, s, handle, inode,
2197 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002198 }
2199 if (error)
2200 return error;
2201 }
2202 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2203 if (!IS_LAST_ENTRY(s->first)) {
2204 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2205 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2206 } else {
2207 header->h_magic = cpu_to_le32(0);
2208 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2209 }
2210 return 0;
2211}
2212
Andreas Dilgere50e5122017-06-21 21:10:32 -04002213static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002214 struct ext4_xattr_info *i,
2215 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002216{
Mingming Cao617ba132006-10-11 01:20:53 -07002217 struct ext4_xattr_ibody_header *header;
2218 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002219 int error;
2220
Mingming Cao617ba132006-10-11 01:20:53 -07002221 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002222 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002223 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 if (error)
2225 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002226 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002227 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002228 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002229 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 } else {
2231 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002232 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233 }
2234 return 0;
2235}
2236
Jan Kara3fd16462016-02-22 22:43:04 -05002237static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2238 struct ext4_xattr_info *i)
2239{
2240 void *value;
2241
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002242 /* When e_value_inum is set the value is stored externally. */
2243 if (s->here->e_value_inum)
2244 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002245 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2246 return 0;
2247 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2248 return !memcmp(value, i->value, i->value_len);
2249}
2250
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002251static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2252{
2253 struct buffer_head *bh;
2254 int error;
2255
2256 if (!EXT4_I(inode)->i_file_acl)
2257 return NULL;
2258 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2259 if (!bh)
2260 return ERR_PTR(-EIO);
2261 error = ext4_xattr_check_block(inode, bh);
2262 if (error)
2263 return ERR_PTR(error);
2264 return bh;
2265}
2266
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267/*
Mingming Cao617ba132006-10-11 01:20:53 -07002268 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002270 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 * is NULL to remove an existing extended attribute, and non-NULL to
2272 * either replace an existing extended attribute, or create a new extended
2273 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2274 * specify that an extended attribute must exist and must not exist
2275 * previous to the call, respectively.
2276 *
2277 * Returns 0, or a negative error number on failure.
2278 */
2279int
Mingming Cao617ba132006-10-11 01:20:53 -07002280ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281 const char *name, const void *value, size_t value_len,
2282 int flags)
2283{
Mingming Cao617ba132006-10-11 01:20:53 -07002284 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285 .name_index = name_index,
2286 .name = name,
2287 .value = value,
2288 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002289 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002290 };
Mingming Cao617ba132006-10-11 01:20:53 -07002291 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002292 .s = { .not_found = -ENODATA, },
2293 };
Mingming Cao617ba132006-10-11 01:20:53 -07002294 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 .s = { .not_found = -ENODATA, },
2296 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002297 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298 int error;
2299
2300 if (!name)
2301 return -EINVAL;
2302 if (strlen(name) > 255)
2303 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002304
Theodore Ts'oc755e252017-01-11 21:50:46 -05002305 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002306
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002307 /* Check journal credits under write lock. */
2308 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002309 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002310 int credits;
2311
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002312 bh = ext4_xattr_get_block(inode);
2313 if (IS_ERR(bh)) {
2314 error = PTR_ERR(bh);
2315 goto cleanup;
2316 }
2317
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002318 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2319 value_len,
2320 flags & XATTR_CREATE);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002321 brelse(bh);
2322
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002323 if (!ext4_handle_has_enough_credits(handle, credits)) {
2324 error = -ENOSPC;
2325 goto cleanup;
2326 }
2327 }
2328
Eric Sandeen66543612011-10-26 03:32:07 -04002329 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002330 if (error)
2331 goto cleanup;
2332
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002333 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002334 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2335 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002336 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002337 }
2338
Mingming Cao617ba132006-10-11 01:20:53 -07002339 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002340 if (error)
2341 goto cleanup;
2342 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002343 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002344 if (error)
2345 goto cleanup;
2346 if (is.s.not_found && bs.s.not_found) {
2347 error = -ENODATA;
2348 if (flags & XATTR_REPLACE)
2349 goto cleanup;
2350 error = 0;
2351 if (!value)
2352 goto cleanup;
2353 } else {
2354 error = -EEXIST;
2355 if (flags & XATTR_CREATE)
2356 goto cleanup;
2357 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002358
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002359 if (!value) {
2360 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002361 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002362 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002363 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002365 error = 0;
2366 /* Xattr value did not change? Save us some work and bail out */
2367 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2368 goto cleanup;
2369 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2370 goto cleanup;
2371
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002372 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2373 (EXT4_XATTR_SIZE(i.value_len) >
2374 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2375 i.in_inode = 1;
2376retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002377 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002378 if (!error && !bs.s.not_found) {
2379 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002380 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002381 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002382 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2383 error = ext4_xattr_block_find(inode, &i, &bs);
2384 if (error)
2385 goto cleanup;
2386 }
Mingming Cao617ba132006-10-11 01:20:53 -07002387 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002388 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002389 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002390 error = ext4_xattr_ibody_set(handle, inode, &i,
2391 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002392 } else if (error == -ENOSPC) {
2393 /*
2394 * Xattr does not fit in the block, store at
2395 * external inode if possible.
2396 */
2397 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2398 !i.in_inode) {
2399 i.in_inode = 1;
2400 goto retry_inode;
2401 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002402 }
2403 }
2404 }
2405 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002406 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002407 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002408 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002409 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002410 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002411 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002412 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002413 * error != 0.
2414 */
2415 is.iloc.bh = NULL;
2416 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002417 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418 }
2419
2420cleanup:
2421 brelse(is.iloc.bh);
2422 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002423 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002424 return error;
2425}
2426
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002427int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2428 bool is_create, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002429{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002430 struct buffer_head *bh;
2431 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002432
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002433 *credits = 0;
2434
2435 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002436 return 0;
2437
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002438 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002439
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002440 bh = ext4_xattr_get_block(inode);
2441 if (IS_ERR(bh)) {
2442 err = PTR_ERR(bh);
2443 } else {
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002444 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2445 value_len, is_create);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002446 brelse(bh);
2447 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002448 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002449
2450 up_read(&EXT4_I(inode)->xattr_sem);
2451 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002452}
2453
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454/*
Mingming Cao617ba132006-10-11 01:20:53 -07002455 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002456 *
Mingming Cao617ba132006-10-11 01:20:53 -07002457 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458 * attribute modification is a filesystem transaction by itself.
2459 *
2460 * Returns 0, or a negative error number on failure.
2461 */
2462int
Mingming Cao617ba132006-10-11 01:20:53 -07002463ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002464 const void *value, size_t value_len, int flags)
2465{
2466 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002467 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002469 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002470
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002471 error = dquot_initialize(inode);
2472 if (error)
2473 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002474
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002476 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2477 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002478 if (error)
2479 return error;
2480
Theodore Ts'o9924a922013-02-08 21:59:22 -05002481 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002482 if (IS_ERR(handle)) {
2483 error = PTR_ERR(handle);
2484 } else {
2485 int error2;
2486
Mingming Cao617ba132006-10-11 01:20:53 -07002487 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002488 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002489 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002491 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002492 goto retry;
2493 if (error == 0)
2494 error = error2;
2495 }
2496
2497 return error;
2498}
2499
2500/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002501 * Shift the EA entries in the inode to create space for the increased
2502 * i_extra_isize.
2503 */
2504static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2505 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002506 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002507{
2508 struct ext4_xattr_entry *last = entry;
2509 int new_offs;
2510
Jan Kara94405712016-08-29 15:41:11 -04002511 /* We always shift xattr headers further thus offsets get lower */
2512 BUG_ON(value_offs_shift > 0);
2513
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002514 /* Adjust the value offsets of the entries */
2515 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002516 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002517 new_offs = le16_to_cpu(last->e_value_offs) +
2518 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002519 last->e_value_offs = cpu_to_le16(new_offs);
2520 }
2521 }
2522 /* Shift the entries by n bytes */
2523 memmove(to, from, n);
2524}
2525
2526/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002527 * Move xattr pointed to by 'entry' from inode into external xattr block
2528 */
2529static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2530 struct ext4_inode *raw_inode,
2531 struct ext4_xattr_entry *entry)
2532{
2533 struct ext4_xattr_ibody_find *is = NULL;
2534 struct ext4_xattr_block_find *bs = NULL;
2535 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002536 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002537 struct ext4_xattr_info i = {
2538 .value = NULL,
2539 .value_len = 0,
2540 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002541 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002542 };
2543 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2544 int error;
2545
Jan Kara3f2571c2016-08-29 15:42:11 -04002546 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2547 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2548 buffer = kmalloc(value_size, GFP_NOFS);
2549 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2550 if (!is || !bs || !buffer || !b_entry_name) {
2551 error = -ENOMEM;
2552 goto out;
2553 }
2554
2555 is->s.not_found = -ENODATA;
2556 bs->s.not_found = -ENODATA;
2557 is->iloc.bh = NULL;
2558 bs->bh = NULL;
2559
2560 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002561 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04002562 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002563 if (error)
2564 goto out;
2565 } else {
2566 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2567 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2568 }
2569
Jan Kara3f2571c2016-08-29 15:42:11 -04002570 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2571 b_entry_name[entry->e_name_len] = '\0';
2572 i.name = b_entry_name;
2573
2574 error = ext4_get_inode_loc(inode, &is->iloc);
2575 if (error)
2576 goto out;
2577
2578 error = ext4_xattr_ibody_find(inode, &i, is);
2579 if (error)
2580 goto out;
2581
2582 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002583 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002584 if (error)
2585 goto out;
2586
Jan Kara3f2571c2016-08-29 15:42:11 -04002587 i.value = buffer;
2588 i.value_len = value_size;
2589 error = ext4_xattr_block_find(inode, &i, bs);
2590 if (error)
2591 goto out;
2592
2593 /* Add entry which was removed from the inode into the block */
2594 error = ext4_xattr_block_set(handle, inode, &i, bs);
2595 if (error)
2596 goto out;
2597 error = 0;
2598out:
2599 kfree(b_entry_name);
2600 kfree(buffer);
2601 if (is)
2602 brelse(is->iloc.bh);
2603 kfree(is);
2604 kfree(bs);
2605
2606 return error;
2607}
2608
Jan Karadfa20642016-08-29 15:44:11 -04002609static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2610 struct ext4_inode *raw_inode,
2611 int isize_diff, size_t ifree,
2612 size_t bfree, int *total_ino)
2613{
2614 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2615 struct ext4_xattr_entry *small_entry;
2616 struct ext4_xattr_entry *entry;
2617 struct ext4_xattr_entry *last;
2618 unsigned int entry_size; /* EA entry size */
2619 unsigned int total_size; /* EA entry size + value size */
2620 unsigned int min_total_size;
2621 int error;
2622
2623 while (isize_diff > ifree) {
2624 entry = NULL;
2625 small_entry = NULL;
2626 min_total_size = ~0U;
2627 last = IFIRST(header);
2628 /* Find the entry best suited to be pushed into EA block */
2629 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002630 total_size = EXT4_XATTR_LEN(last->e_name_len);
2631 if (!last->e_value_inum)
2632 total_size += EXT4_XATTR_SIZE(
2633 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002634 if (total_size <= bfree &&
2635 total_size < min_total_size) {
2636 if (total_size + ifree < isize_diff) {
2637 small_entry = last;
2638 } else {
2639 entry = last;
2640 min_total_size = total_size;
2641 }
2642 }
2643 }
2644
2645 if (entry == NULL) {
2646 if (small_entry == NULL)
2647 return -ENOSPC;
2648 entry = small_entry;
2649 }
2650
2651 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002652 total_size = entry_size;
2653 if (!entry->e_value_inum)
2654 total_size += EXT4_XATTR_SIZE(
2655 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002656 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2657 entry);
2658 if (error)
2659 return error;
2660
2661 *total_ino -= entry_size;
2662 ifree += total_size;
2663 bfree -= total_size;
2664 }
2665
2666 return 0;
2667}
2668
Jan Kara3f2571c2016-08-29 15:42:11 -04002669/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002670 * Expand an inode by new_extra_isize bytes when EAs are present.
2671 * Returns 0 on success or negative error number on failure.
2672 */
2673int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2674 struct ext4_inode *raw_inode, handle_t *handle)
2675{
2676 struct ext4_xattr_ibody_header *header;
Miao Xieb640b2c2017-08-06 00:55:48 -04002677 struct buffer_head *bh;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002678 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2679 static unsigned int mnt_count;
Jan Karae3014d12016-08-29 15:38:11 -04002680 size_t min_offs;
2681 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002682 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002683 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002684 int error = 0, tried_min_extra_isize = 0;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002685 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04002686 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002687
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002688retry:
Jan Karad0141192016-08-11 11:50:30 -04002689 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002690 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
Miao Xieb640b2c2017-08-06 00:55:48 -04002691 return 0;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002692
2693 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002694
2695 /*
2696 * Check if enough free space is available in the inode to shift the
2697 * entries ahead by new_extra_isize.
2698 */
2699
Jan Kara6e0cd082016-08-29 15:43:11 -04002700 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002701 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2702 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002703 total_ino = sizeof(struct ext4_xattr_ibody_header);
2704
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002705 error = xattr_check_inode(inode, header, end);
2706 if (error)
2707 goto cleanup;
2708
Jan Kara6e0cd082016-08-29 15:43:11 -04002709 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002710 if (ifree >= isize_diff)
2711 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002712
2713 /*
2714 * Enough free space isn't available in the inode, check if
2715 * EA block can hold new_extra_isize bytes.
2716 */
2717 if (EXT4_I(inode)->i_file_acl) {
2718 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2719 error = -EIO;
2720 if (!bh)
2721 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04002722 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002723 EXT4_ERROR_INODE(inode, "bad block %llu",
2724 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002725 error = -EFSCORRUPTED;
Miao Xieb640b2c2017-08-06 00:55:48 -04002726 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002727 goto cleanup;
2728 }
2729 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002730 end = bh->b_data + bh->b_size;
2731 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002732 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2733 NULL);
Miao Xieb640b2c2017-08-06 00:55:48 -04002734 brelse(bh);
Jan Karae3014d12016-08-29 15:38:11 -04002735 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002736 if (!tried_min_extra_isize && s_min_extra_isize) {
2737 tried_min_extra_isize++;
2738 new_extra_isize = s_min_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002739 goto retry;
2740 }
Jan Karadfa20642016-08-29 15:44:11 -04002741 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002742 goto cleanup;
2743 }
2744 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002745 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002746 }
2747
Jan Karadfa20642016-08-29 15:44:11 -04002748 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2749 isize_diff, ifree, bfree,
2750 &total_ino);
2751 if (error) {
2752 if (error == -ENOSPC && !tried_min_extra_isize &&
2753 s_min_extra_isize) {
2754 tried_min_extra_isize++;
2755 new_extra_isize = s_min_extra_isize;
Jan Karadfa20642016-08-29 15:44:11 -04002756 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002757 }
Jan Karadfa20642016-08-29 15:44:11 -04002758 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002759 }
Jan Karae3014d12016-08-29 15:38:11 -04002760shift:
2761 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002762 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002763 - new_extra_isize, (void *)raw_inode +
2764 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002765 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002766 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002767
2768cleanup:
Miao Xieb640b2c2017-08-06 00:55:48 -04002769 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
Miao Xiecf0a5e82017-08-06 00:40:01 -04002770 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2771 inode->i_ino);
2772 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2773 }
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002774 return error;
2775}
2776
Andreas Dilgere50e5122017-06-21 21:10:32 -04002777#define EIA_INCR 16 /* must be 2^n */
2778#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002779
2780/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002781 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002782 * contents copied over.
2783 */
2784static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002785ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2786 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002787{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002788 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002789 /*
2790 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002791 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002792 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002793 (*ea_inode_array) =
2794 kmalloc(offsetof(struct ext4_xattr_inode_array,
2795 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002796 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002797 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002798 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002799 (*ea_inode_array)->count = 0;
2800 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002801 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002802 struct ext4_xattr_inode_array *new_array = NULL;
2803 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002804
2805 /* if new_array is NULL, this is essentially offsetof() */
2806 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002807 offsetof(struct ext4_xattr_inode_array,
2808 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002809 GFP_NOFS);
2810 if (new_array == NULL)
2811 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002812 memcpy(new_array, *ea_inode_array,
2813 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2814 kfree(*ea_inode_array);
2815 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002816 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002817 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002818 return 0;
2819}
2820
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002821/*
Mingming Cao617ba132006-10-11 01:20:53 -07002822 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002823 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002824 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002825 * all entries and decrement reference on any xattr inodes associated with this
2826 * inode. This is called immediately before an inode is freed. We have exclusive
2827 * access to the inode. If an orphan inode is deleted it will also release its
2828 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002829 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002830int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2831 struct ext4_xattr_inode_array **ea_inode_array,
2832 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002833{
2834 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002835 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002836 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002837 struct ext4_xattr_entry *entry;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002838 struct inode *ea_inode;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002839 int error;
2840
2841 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2842 NULL /* bh */,
2843 false /* dirty */,
2844 false /* block_csum */);
2845 if (error) {
2846 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2847 goto cleanup;
2848 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002849
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002850 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2851 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002852
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002853 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002854 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002855 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002856 goto cleanup;
2857 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002858
2859 error = ext4_journal_get_write_access(handle, iloc.bh);
2860 if (error) {
2861 EXT4_ERROR_INODE(inode, "write access (error %d)",
2862 error);
2863 goto cleanup;
2864 }
2865
2866 header = IHDR(inode, ext4_raw_inode(&iloc));
2867 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2868 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2869 IFIRST(header),
2870 false /* block_csum */,
2871 ea_inode_array,
2872 extra_credits,
2873 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002874 }
2875
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002876 if (EXT4_I(inode)->i_file_acl) {
2877 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2878 if (!bh) {
2879 EXT4_ERROR_INODE(inode, "block %llu read error",
2880 EXT4_I(inode)->i_file_acl);
2881 error = -EIO;
2882 goto cleanup;
2883 }
2884 error = ext4_xattr_check_block(inode, bh);
2885 if (error) {
2886 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2887 EXT4_I(inode)->i_file_acl, error);
2888 goto cleanup;
2889 }
2890
2891 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2892 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002893 entry = EXT4_XATTR_NEXT(entry)) {
2894 if (!entry->e_value_inum)
2895 continue;
2896 error = ext4_xattr_inode_iget(inode,
2897 le32_to_cpu(entry->e_value_inum),
2898 le32_to_cpu(entry->e_hash),
2899 &ea_inode);
2900 if (error)
2901 continue;
2902 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002903 le32_to_cpu(entry->e_value_size));
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002904 iput(ea_inode);
2905 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002906
2907 }
2908
2909 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2910 extra_credits);
2911 /*
2912 * Update i_file_acl value in the same transaction that releases
2913 * block.
2914 */
2915 EXT4_I(inode)->i_file_acl = 0;
2916 error = ext4_mark_inode_dirty(handle, inode);
2917 if (error) {
2918 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2919 error);
2920 goto cleanup;
2921 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002922 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002923 error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002924cleanup:
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002925 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002926 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002927 return error;
2928}
2929
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002930void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002931{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002932 int idx;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002933
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002934 if (ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002935 return;
2936
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002937 for (idx = 0; idx < ea_inode_array->count; ++idx)
2938 iput(ea_inode_array->inodes[idx]);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002939 kfree(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002940}
2941
2942/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002943 * ext4_xattr_block_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944 *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002945 * Create a new entry in the extended attribute block cache, and insert
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002946 * it unless such an entry is already in the cache.
2947 *
2948 * Returns 0, or a negative error number on failure.
2949 */
2950static void
Tahsin Erdogan47387402017-06-22 11:28:55 -04002951ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2952 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002953{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002954 struct ext4_xattr_header *header = BHDR(bh);
2955 __u32 hash = le32_to_cpu(header->h_hash);
2956 int reusable = le32_to_cpu(header->h_refcount) <
2957 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958 int error;
2959
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002960 if (!ea_block_cache)
2961 return;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002962 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002963 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002964 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002965 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002966 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002967 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002968 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969}
2970
2971/*
Mingming Cao617ba132006-10-11 01:20:53 -07002972 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002973 *
2974 * Compare two extended attribute blocks for equality.
2975 *
2976 * Returns 0 if the blocks are equal, 1 if they differ, and
2977 * a negative error number on errors.
2978 */
2979static int
Mingming Cao617ba132006-10-11 01:20:53 -07002980ext4_xattr_cmp(struct ext4_xattr_header *header1,
2981 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002982{
Mingming Cao617ba132006-10-11 01:20:53 -07002983 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984
2985 entry1 = ENTRY(header1+1);
2986 entry2 = ENTRY(header2+1);
2987 while (!IS_LAST_ENTRY(entry1)) {
2988 if (IS_LAST_ENTRY(entry2))
2989 return 1;
2990 if (entry1->e_hash != entry2->e_hash ||
2991 entry1->e_name_index != entry2->e_name_index ||
2992 entry1->e_name_len != entry2->e_name_len ||
2993 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002994 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002995 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2996 return 1;
Tahsin Erdogan7cec1912017-06-21 22:14:30 -04002997 if (!entry1->e_value_inum &&
2998 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002999 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3000 le32_to_cpu(entry1->e_value_size)))
3001 return 1;
3002
Mingming Cao617ba132006-10-11 01:20:53 -07003003 entry1 = EXT4_XATTR_NEXT(entry1);
3004 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003005 }
3006 if (!IS_LAST_ENTRY(entry2))
3007 return 1;
3008 return 0;
3009}
3010
3011/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04003012 * ext4_xattr_block_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003013 *
3014 * Find an identical extended attribute block.
3015 *
3016 * Returns a pointer to the block found, or NULL if such a block was
3017 * not found or an error occurred.
3018 */
3019static struct buffer_head *
Tahsin Erdogan47387402017-06-22 11:28:55 -04003020ext4_xattr_block_cache_find(struct inode *inode,
3021 struct ext4_xattr_header *header,
3022 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003023{
3024 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05003025 struct mb_cache_entry *ce;
Tahsin Erdogan47387402017-06-22 11:28:55 -04003026 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04003028 if (!ea_block_cache)
3029 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030 if (!header->h_hash)
3031 return NULL; /* never share */
3032 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003033 ce = mb_cache_entry_find_first(ea_block_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003034 while (ce) {
3035 struct buffer_head *bh;
3036
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003037 bh = sb_bread(inode->i_sb, ce->e_value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003038 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003039 EXT4_ERROR_INODE(inode, "block %lu read error",
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003040 (unsigned long)ce->e_value);
Mingming Cao617ba132006-10-11 01:20:53 -07003041 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003042 *pce = ce;
3043 return bh;
3044 }
3045 brelse(bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003046 ce = mb_cache_entry_find_next(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047 }
3048 return NULL;
3049}
3050
3051#define NAME_HASH_SHIFT 5
3052#define VALUE_HASH_SHIFT 16
3053
3054/*
Mingming Cao617ba132006-10-11 01:20:53 -07003055 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056 *
3057 * Compute the hash of an extended attribute.
3058 */
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003059static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3060 size_t value_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061{
3062 __u32 hash = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003063
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003064 while (name_len--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 hash = (hash << NAME_HASH_SHIFT) ^
3066 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3067 *name++;
3068 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003069 while (value_count--) {
3070 hash = (hash << VALUE_HASH_SHIFT) ^
3071 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3072 le32_to_cpu(*value++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003073 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003074 return cpu_to_le32(hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075}
3076
3077#undef NAME_HASH_SHIFT
3078#undef VALUE_HASH_SHIFT
3079
3080#define BLOCK_HASH_SHIFT 16
3081
3082/*
Mingming Cao617ba132006-10-11 01:20:53 -07003083 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003084 *
3085 * Re-compute the extended attribute hash value after an entry has changed.
3086 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04003087static void ext4_xattr_rehash(struct ext4_xattr_header *header)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088{
Mingming Cao617ba132006-10-11 01:20:53 -07003089 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090 __u32 hash = 0;
3091
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003092 here = ENTRY(header+1);
3093 while (!IS_LAST_ENTRY(here)) {
3094 if (!here->e_hash) {
3095 /* Block is not shared if an entry's hash value == 0 */
3096 hash = 0;
3097 break;
3098 }
3099 hash = (hash << BLOCK_HASH_SHIFT) ^
3100 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3101 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07003102 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103 }
3104 header->h_hash = cpu_to_le32(hash);
3105}
3106
3107#undef BLOCK_HASH_SHIFT
3108
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003109#define HASH_BUCKET_BITS 10
3110
Jan Kara7a2508e2016-02-22 22:35:22 -05003111struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05003112ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003113{
Jan Kara7a2508e2016-02-22 22:35:22 -05003114 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003115}
3116
Jan Kara7a2508e2016-02-22 22:35:22 -05003117void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003118{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003119 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05003120 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003121}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003122