blob: 3eeed8f0aa06136a0b733279a0732feb5051e55b [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
Jan Kara7a2508e2016-02-22 22:35:22 -050075static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070076static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
Jan Kara7a2508e2016-02-22 22:35:22 -050078 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070079static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000081static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040082 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083
Stephen Hemminger11e27522010-05-13 17:53:18 -070084static const struct xattr_handler *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
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400109#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
111
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400112static __le32 ext4_xattr_block_csum(struct inode *inode,
113 sector_t block_nr,
114 struct ext4_xattr_header *hdr)
115{
116 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400117 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400118 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400119 __u32 dummy_csum = 0;
120 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400121
Theodore Ts'od6a77102013-04-09 23:59:55 -0400122 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
123 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400124 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
125 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
126 offset += sizeof(dummy_csum);
127 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
128 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400129
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400130 return cpu_to_le32(csum);
131}
132
133static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400134 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135{
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400136 struct ext4_xattr_header *hdr = BHDR(bh);
137 int ret = 1;
138
139 if (ext4_has_metadata_csum(inode->i_sb)) {
140 lock_buffer(bh);
141 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
142 bh->b_blocknr, hdr));
143 unlock_buffer(bh);
144 }
145 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400146}
147
148static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400149 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400150{
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400151 if (ext4_has_metadata_csum(inode->i_sb))
152 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
153 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400154}
155
Stephen Hemminger11e27522010-05-13 17:53:18 -0700156static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700157ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700159 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160
Mingming Cao617ba132006-10-11 01:20:53 -0700161 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
162 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 return handler;
164}
165
166/*
167 * Inode operation listxattr()
168 *
David Howells2b0143b2015-03-17 22:25:59 +0000169 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 */
171ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700172ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000174 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175}
176
177static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400178ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
179 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400181 struct ext4_xattr_entry *e = entry;
182
183 while (!IS_LAST_ENTRY(e)) {
184 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400186 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400187 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189
190 while (!IS_LAST_ENTRY(entry)) {
Jan Kara2de58f12016-08-29 15:39:11 -0400191 if (entry->e_value_block != 0)
192 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193 if (entry->e_value_size != 0 &&
194 (value_start + le16_to_cpu(entry->e_value_offs) <
195 (void *)e + sizeof(__u32) ||
196 value_start + le16_to_cpu(entry->e_value_offs) +
197 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400198 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400199 entry = EXT4_XATTR_NEXT(entry);
200 }
201
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 return 0;
203}
204
205static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400206ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400208 int error;
209
210 if (buffer_verified(bh))
211 return 0;
212
Mingming Cao617ba132006-10-11 01:20:53 -0700213 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400215 return -EFSCORRUPTED;
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400216 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400217 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400218 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
219 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400220 if (!error)
221 set_buffer_verified(bh);
222 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223}
224
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400225static int
226__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
227 void *end, const char *function, unsigned int line)
228{
229 struct ext4_xattr_entry *entry = IFIRST(header);
230 int error = -EFSCORRUPTED;
231
232 if (((void *) header >= end) ||
Eric Biggers19962502016-10-15 09:39:31 -0400233 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400234 goto errout;
235 error = ext4_xattr_check_names(entry, end, entry);
236errout:
237 if (error)
238 __ext4_error_inode(inode, function, line, 0,
239 "corrupted in-inode xattr");
240 return error;
241}
242
243#define xattr_check_inode(inode, header, end) \
244 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
245
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700247ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248{
249 size_t value_size = le32_to_cpu(entry->e_value_size);
250
251 if (entry->e_value_block != 0 || value_size > size ||
252 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400253 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254 return 0;
255}
256
257static int
Mingming Cao617ba132006-10-11 01:20:53 -0700258ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 const char *name, size_t size, int sorted)
260{
Mingming Cao617ba132006-10-11 01:20:53 -0700261 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 size_t name_len;
263 int cmp = 1;
264
265 if (name == NULL)
266 return -EINVAL;
267 name_len = strlen(name);
268 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700269 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 cmp = name_index - entry->e_name_index;
271 if (!cmp)
272 cmp = name_len - entry->e_name_len;
273 if (!cmp)
274 cmp = memcmp(name, entry->e_name, name_len);
275 if (cmp <= 0 && (sorted || cmp == 0))
276 break;
277 }
278 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700279 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400280 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 return cmp ? -ENODATA : 0;
282}
283
284static int
Mingming Cao617ba132006-10-11 01:20:53 -0700285ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 void *buffer, size_t buffer_size)
287{
288 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700289 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 size_t size;
291 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500292 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293
294 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
295 name_index, name, buffer, (long)buffer_size);
296
297 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700298 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400300 ea_idebug(inode, "reading block %llu",
301 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700302 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 if (!bh)
304 goto cleanup;
305 ea_bdebug(bh, "b_count=%d, refcount=%d",
306 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400307 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500308bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400309 EXT4_ERROR_INODE(inode, "bad block %llu",
310 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400311 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 goto cleanup;
313 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400314 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700316 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400317 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 goto bad_block;
319 if (error)
320 goto cleanup;
321 size = le32_to_cpu(entry->e_value_size);
322 if (buffer) {
323 error = -ERANGE;
324 if (size > buffer_size)
325 goto cleanup;
326 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
327 size);
328 }
329 error = size;
330
331cleanup:
332 brelse(bh);
333 return error;
334}
335
Tao Ma879b3822012-12-05 10:28:46 -0500336int
Mingming Cao617ba132006-10-11 01:20:53 -0700337ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 void *buffer, size_t buffer_size)
339{
Mingming Cao617ba132006-10-11 01:20:53 -0700340 struct ext4_xattr_ibody_header *header;
341 struct ext4_xattr_entry *entry;
342 struct ext4_inode *raw_inode;
343 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344 size_t size;
345 void *end;
346 int error;
347
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500348 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700350 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 if (error)
352 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700353 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354 header = IHDR(inode, raw_inode);
355 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700356 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400357 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 if (error)
359 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700360 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 end - (void *)entry, 0);
362 if (error)
363 goto cleanup;
364 size = le32_to_cpu(entry->e_value_size);
365 if (buffer) {
366 error = -ERANGE;
367 if (size > buffer_size)
368 goto cleanup;
369 memcpy(buffer, (void *)IFIRST(header) +
370 le16_to_cpu(entry->e_value_offs), size);
371 }
372 error = size;
373
374cleanup:
375 brelse(iloc.bh);
376 return error;
377}
378
379/*
Mingming Cao617ba132006-10-11 01:20:53 -0700380 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700381 *
382 * Copy an extended attribute into the buffer
383 * provided, or compute the buffer size required.
384 * Buffer is NULL to compute the size of the buffer required.
385 *
386 * Returns a negative error number on failure, or the number of bytes
387 * used / required on success.
388 */
389int
Mingming Cao617ba132006-10-11 01:20:53 -0700390ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700391 void *buffer, size_t buffer_size)
392{
393 int error;
394
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400395 if (strlen(name) > 255)
396 return -ERANGE;
397
Mingming Cao617ba132006-10-11 01:20:53 -0700398 down_read(&EXT4_I(inode)->xattr_sem);
399 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 buffer_size);
401 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700402 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700403 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700404 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 return error;
406}
407
408static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000409ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 char *buffer, size_t buffer_size)
411{
412 size_t rest = buffer_size;
413
Mingming Cao617ba132006-10-11 01:20:53 -0700414 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700415 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700416 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100418 if (handler && (!handler->list || handler->list(dentry))) {
419 const char *prefix = handler->prefix ?: handler->name;
420 size_t prefix_len = strlen(prefix);
421 size_t size = prefix_len + entry->e_name_len + 1;
422
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423 if (buffer) {
424 if (size > rest)
425 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100426 memcpy(buffer, prefix, prefix_len);
427 buffer += prefix_len;
428 memcpy(buffer, entry->e_name, entry->e_name_len);
429 buffer += entry->e_name_len;
430 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431 }
432 rest -= size;
433 }
434 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100435 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436}
437
438static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000439ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440{
David Howells2b0143b2015-03-17 22:25:59 +0000441 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442 struct buffer_head *bh = NULL;
443 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500444 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445
446 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
447 buffer, (long)buffer_size);
448
449 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700450 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400452 ea_idebug(inode, "reading block %llu",
453 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700454 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 error = -EIO;
456 if (!bh)
457 goto cleanup;
458 ea_bdebug(bh, "b_count=%d, refcount=%d",
459 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400460 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400461 EXT4_ERROR_INODE(inode, "bad block %llu",
462 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400463 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464 goto cleanup;
465 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400466 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000467 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468
469cleanup:
470 brelse(bh);
471
472 return error;
473}
474
475static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000476ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477{
David Howells2b0143b2015-03-17 22:25:59 +0000478 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700479 struct ext4_xattr_ibody_header *header;
480 struct ext4_inode *raw_inode;
481 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482 void *end;
483 int error;
484
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500485 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700487 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 if (error)
489 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700490 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700492 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400493 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494 if (error)
495 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000496 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 buffer, buffer_size);
498
499cleanup:
500 brelse(iloc.bh);
501 return error;
502}
503
504/*
Mingming Cao617ba132006-10-11 01:20:53 -0700505 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506 *
507 * Copy a list of attribute names into the buffer
508 * provided, or compute the buffer size required.
509 * Buffer is NULL to compute the size of the buffer required.
510 *
511 * Returns a negative error number on failure, or the number of bytes
512 * used / required on success.
513 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400514static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000515ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500517 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518
David Howells2b0143b2015-03-17 22:25:59 +0000519 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500520 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
521 if (ret < 0)
522 goto errout;
523 if (buffer) {
524 buffer += ret;
525 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500527 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
528 if (ret < 0)
529 goto errout;
530 ret += ret2;
531errout:
David Howells2b0143b2015-03-17 22:25:59 +0000532 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500533 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534}
535
536/*
Mingming Cao617ba132006-10-11 01:20:53 -0700537 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 * not set, set it.
539 */
Mingming Cao617ba132006-10-11 01:20:53 -0700540static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 struct super_block *sb)
542{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400543 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 return;
545
liang xie5d601252014-05-12 22:06:43 -0400546 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700547 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400548 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400549 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551}
552
553/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400554 * Release the xattr block BH: If the reference count is > 1, decrement it;
555 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 */
557static void
Mingming Cao617ba132006-10-11 01:20:53 -0700558ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 struct buffer_head *bh)
560{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500561 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
562 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800563 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564
liang xie5d601252014-05-12 22:06:43 -0400565 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800566 error = ext4_journal_get_write_access(handle, bh);
567 if (error)
568 goto out;
569
570 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500571 hash = le32_to_cpu(BHDR(bh)->h_hash);
572 ref = le32_to_cpu(BHDR(bh)->h_refcount);
573 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500575 /*
576 * This must happen under buffer lock for
577 * ext4_xattr_block_set() to reliably detect freed block
578 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500579 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400581 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500582 ext4_free_blocks(handle, inode, bh, 0, 1,
583 EXT4_FREE_BLOCKS_METADATA |
584 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500586 ref--;
587 BHDR(bh)->h_refcount = cpu_to_le32(ref);
588 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
589 struct mb_cache_entry *ce;
590
591 ce = mb_cache_entry_get(ext4_mb_cache, hash,
592 bh->b_blocknr);
593 if (ce) {
594 ce->e_reusable = 1;
595 mb_cache_entry_put(ext4_mb_cache, ce);
596 }
597 }
598
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400599 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400600 /*
601 * Beware of this ugliness: Releasing of xattr block references
602 * from different inodes can race and so we have to protect
603 * from a race where someone else frees the block (and releases
604 * its journal_head) before we are done dirtying the buffer. In
605 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400606 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400607 * that function can call sync_dirty_buffer() so for that case
608 * we handle the dirtying after unlocking the buffer.
609 */
610 if (ext4_handle_valid(handle))
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400611 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500612 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400613 if (!ext4_handle_valid(handle))
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400614 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800615 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500616 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500617 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800618 ea_bdebug(bh, "refcount now=%d; releasing",
619 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800621out:
622 ext4_std_error(inode->i_sb, error);
623 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624}
625
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400626/*
627 * Find the available free space for EAs. This also returns the total number of
628 * bytes used by EA entries.
629 */
630static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
631 size_t *min_offs, void *base, int *total)
632{
633 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400634 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400635 size_t offs = le16_to_cpu(last->e_value_offs);
636 if (offs < *min_offs)
637 *min_offs = offs;
638 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500639 if (total)
640 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400641 }
642 return (*min_offs - ((void *)last - base) - sizeof(__u32));
643}
644
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645static int
Mingming Cao617ba132006-10-11 01:20:53 -0700646ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647{
Mingming Cao617ba132006-10-11 01:20:53 -0700648 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
650
651 /* Compute min_offs and last. */
652 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700653 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400654 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 size_t offs = le16_to_cpu(last->e_value_offs);
656 if (offs < min_offs)
657 min_offs = offs;
658 }
659 }
660 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
661 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400662 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700664 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 }
Mingming Cao617ba132006-10-11 01:20:53 -0700666 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 }
668 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400669 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700670 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 return -ENOSPC;
672 }
673
674 if (i->value && s->not_found) {
675 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700676 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
678 memmove((void *)s->here + size, s->here, rest);
679 memset(s->here, 0, size);
680 s->here->e_name_index = i->name_index;
681 s->here->e_name_len = name_len;
682 memcpy(s->here->e_name, i->name, name_len);
683 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400684 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 void *first_val = s->base + min_offs;
686 size_t offs = le16_to_cpu(s->here->e_value_offs);
687 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700688 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 le32_to_cpu(s->here->e_value_size));
690
Mingming Cao617ba132006-10-11 01:20:53 -0700691 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 /* The old and the new value have the same
693 size. Just replace. */
694 s->here->e_value_size =
695 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500696 if (i->value == EXT4_ZERO_XATTR_VALUE) {
697 memset(val, 0, size);
698 } else {
699 /* Clear pad bytes first. */
700 memset(val + size - EXT4_XATTR_PAD, 0,
701 EXT4_XATTR_PAD);
702 memcpy(val, i->value, i->value_len);
703 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 return 0;
705 }
706
707 /* Remove the old value. */
708 memmove(first_val + size, first_val, val - first_val);
709 memset(first_val, 0, size);
710 s->here->e_value_size = 0;
711 s->here->e_value_offs = 0;
712 min_offs += size;
713
714 /* Adjust all value offsets. */
715 last = s->first;
716 while (!IS_LAST_ENTRY(last)) {
717 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400718 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 last->e_value_offs =
720 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700721 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 }
723 }
724 if (!i->value) {
725 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700726 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 last = ENTRY((void *)last - size);
728 memmove(s->here, (void *)s->here + size,
729 (void *)last - (void *)s->here + sizeof(__u32));
730 memset(last, 0, size);
731 }
732 }
733
734 if (i->value) {
735 /* Insert the new value. */
736 s->here->e_value_size = cpu_to_le32(i->value_len);
737 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700738 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 void *val = s->base + min_offs - size;
740 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500741 if (i->value == EXT4_ZERO_XATTR_VALUE) {
742 memset(val, 0, size);
743 } else {
744 /* Clear the pad bytes first. */
745 memset(val + size - EXT4_XATTR_PAD, 0,
746 EXT4_XATTR_PAD);
747 memcpy(val, i->value, i->value_len);
748 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 }
750 }
751 return 0;
752}
753
Mingming Cao617ba132006-10-11 01:20:53 -0700754struct ext4_xattr_block_find {
755 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700756 struct buffer_head *bh;
757};
758
759static int
Mingming Cao617ba132006-10-11 01:20:53 -0700760ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
761 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762{
763 struct super_block *sb = inode->i_sb;
764 int error;
765
766 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
767 i->name_index, i->name, i->value, (long)i->value_len);
768
Mingming Cao617ba132006-10-11 01:20:53 -0700769 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700771 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 error = -EIO;
773 if (!bs->bh)
774 goto cleanup;
775 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
776 atomic_read(&(bs->bh->b_count)),
777 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400778 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400779 EXT4_ERROR_INODE(inode, "bad block %llu",
780 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400781 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 goto cleanup;
783 }
784 /* Find the named attribute. */
785 bs->s.base = BHDR(bs->bh);
786 bs->s.first = BFIRST(bs->bh);
787 bs->s.end = bs->bh->b_data + bs->bh->b_size;
788 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700789 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790 i->name, bs->bh->b_size, 1);
791 if (error && error != -ENODATA)
792 goto cleanup;
793 bs->s.not_found = error;
794 }
795 error = 0;
796
797cleanup:
798 return error;
799}
800
801static int
Mingming Cao617ba132006-10-11 01:20:53 -0700802ext4_xattr_block_set(handle_t *handle, struct inode *inode,
803 struct ext4_xattr_info *i,
804 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805{
806 struct super_block *sb = inode->i_sb;
807 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700808 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500809 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800810 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500811 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812
Mingming Cao617ba132006-10-11 01:20:53 -0700813#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814
815 if (i->value && i->value_len > sb->s_blocksize)
816 return -ENOSPC;
817 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400818 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800819 error = ext4_journal_get_write_access(handle, bs->bh);
820 if (error)
821 goto cleanup;
822 lock_buffer(bs->bh);
823
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500825 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
826
827 /*
828 * This must happen under buffer lock for
829 * ext4_xattr_block_set() to reliably detect modified
830 * block
831 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500832 mb_cache_entry_delete_block(ext4_mb_cache, hash,
833 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700835 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 if (!error) {
837 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700838 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400840 ext4_xattr_cache_insert(ext4_mb_cache,
841 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 }
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400843 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400845 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 goto bad_block;
847 if (!error)
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400848 error = ext4_handle_dirty_metadata(handle,
849 inode,
850 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 if (error)
852 goto cleanup;
853 goto inserted;
854 } else {
855 int offset = (char *)s->here - bs->bh->b_data;
856
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800857 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400859 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 error = -ENOMEM;
861 if (s->base == NULL)
862 goto cleanup;
863 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
864 s->first = ENTRY(header(s->base)+1);
865 header(s->base)->h_refcount = cpu_to_le32(1);
866 s->here = ENTRY(s->base + offset);
867 s->end = s->base + bs->bh->b_size;
868 }
869 } else {
870 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400871 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 /* assert(header == s->base) */
873 error = -ENOMEM;
874 if (s->base == NULL)
875 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700876 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 header(s->base)->h_blocks = cpu_to_le32(1);
878 header(s->base)->h_refcount = cpu_to_le32(1);
879 s->first = ENTRY(header(s->base)+1);
880 s->here = ENTRY(header(s->base)+1);
881 s->end = s->base + sb->s_blocksize;
882 }
883
Mingming Cao617ba132006-10-11 01:20:53 -0700884 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400885 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 goto bad_block;
887 if (error)
888 goto cleanup;
889 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700890 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891
892inserted:
893 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700894 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 if (new_bh) {
896 /* We found an identical block in the cache. */
897 if (new_bh == bs->bh)
898 ea_bdebug(new_bh, "keeping");
899 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500900 u32 ref;
901
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 /* The old block is released after updating
903 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500904 error = dquot_alloc_block(inode,
905 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500906 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400908 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700909 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 new_bh);
911 if (error)
912 goto cleanup_dquot;
913 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500914 /*
915 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500916 * freeing, rehashing or adding references to
917 * xattr block. Once we hold buffer lock xattr
918 * block's state is stable so we can check
919 * whether the block got freed / rehashed or
920 * not. Since we unhash mbcache entry under
921 * buffer lock when freeing / rehashing xattr
922 * block, checking whether entry is still
923 * hashed is reliable. Same rules hold for
924 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500925 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500926 if (hlist_bl_unhashed(&ce->e_hash_list) ||
927 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500928 /*
929 * Undo everything and check mbcache
930 * again.
931 */
932 unlock_buffer(new_bh);
933 dquot_free_block(inode,
934 EXT4_C2B(EXT4_SB(sb),
935 1));
936 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500937 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500938 ce = NULL;
939 new_bh = NULL;
940 goto inserted;
941 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500942 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
943 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
944 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
945 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500947 ref);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400948 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 unlock_buffer(new_bh);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400950 error = ext4_handle_dirty_metadata(handle,
951 inode,
952 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 if (error)
954 goto cleanup_dquot;
955 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500956 mb_cache_entry_touch(ext4_mb_cache, ce);
957 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 ce = NULL;
959 } else if (bs->bh && s->base == bs->bh->b_data) {
960 /* We were modifying this block in-place. */
961 ea_bdebug(bs->bh, "keeping this block");
962 new_bh = bs->bh;
963 get_bh(new_bh);
964 } else {
965 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400966 ext4_fsblk_t goal, block;
967
968 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400969 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400970
971 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400972 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400973 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
974
Allison Henderson55f020d2011-05-25 07:41:26 -0400975 block = ext4_new_meta_blocks(handle, inode, goal, 0,
976 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 if (error)
978 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400979
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400980 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400981 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
982
Joe Perchesace36ad2012-03-19 23:11:43 -0400983 ea_idebug(inode, "creating block %llu",
984 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985
986 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500987 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500988 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500990 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500991 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 goto cleanup;
993 }
994 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700995 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 if (error) {
997 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500998 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999 goto getblk_failed;
1000 }
1001 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -04001002 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 set_buffer_uptodate(new_bh);
1004 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001005 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -04001006 error = ext4_handle_dirty_metadata(handle, inode,
1007 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008 if (error)
1009 goto cleanup;
1010 }
1011 }
1012
1013 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001014 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015
1016 /* Drop the previous xattr block. */
1017 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001018 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001019 error = 0;
1020
1021cleanup:
1022 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001023 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024 brelse(new_bh);
1025 if (!(bs->bh && s->base == bs->bh->b_data))
1026 kfree(s->base);
1027
1028 return error;
1029
1030cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001031 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032 goto cleanup;
1033
1034bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001035 EXT4_ERROR_INODE(inode, "bad block %llu",
1036 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 goto cleanup;
1038
1039#undef header
1040}
1041
Tao Ma879b3822012-12-05 10:28:46 -05001042int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1043 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001044{
Mingming Cao617ba132006-10-11 01:20:53 -07001045 struct ext4_xattr_ibody_header *header;
1046 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 int error;
1048
Mingming Cao617ba132006-10-11 01:20:53 -07001049 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001051 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 header = IHDR(inode, raw_inode);
1053 is->s.base = is->s.first = IFIRST(header);
1054 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001055 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001056 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001057 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 if (error)
1059 return error;
1060 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001061 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 i->name, is->s.end -
1063 (void *)is->s.base, 0);
1064 if (error && error != -ENODATA)
1065 return error;
1066 is->s.not_found = error;
1067 }
1068 return 0;
1069}
1070
Tao Ma0d812f72012-12-10 14:06:02 -05001071int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1072 struct ext4_xattr_info *i,
1073 struct ext4_xattr_ibody_find *is)
1074{
1075 struct ext4_xattr_ibody_header *header;
1076 struct ext4_xattr_search *s = &is->s;
1077 int error;
1078
1079 if (EXT4_I(inode)->i_extra_isize == 0)
1080 return -ENOSPC;
1081 error = ext4_xattr_set_entry(i, s);
1082 if (error) {
1083 if (error == -ENOSPC &&
1084 ext4_has_inline_data(inode)) {
1085 error = ext4_try_to_evict_inline_data(handle, inode,
1086 EXT4_XATTR_LEN(strlen(i->name) +
1087 EXT4_XATTR_SIZE(i->value_len)));
1088 if (error)
1089 return error;
1090 error = ext4_xattr_ibody_find(inode, i, is);
1091 if (error)
1092 return error;
1093 error = ext4_xattr_set_entry(i, s);
1094 }
1095 if (error)
1096 return error;
1097 }
1098 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1099 if (!IS_LAST_ENTRY(s->first)) {
1100 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1101 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1102 } else {
1103 header->h_magic = cpu_to_le32(0);
1104 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1105 }
1106 return 0;
1107}
1108
1109static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1110 struct ext4_xattr_info *i,
1111 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112{
Mingming Cao617ba132006-10-11 01:20:53 -07001113 struct ext4_xattr_ibody_header *header;
1114 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 int error;
1116
Mingming Cao617ba132006-10-11 01:20:53 -07001117 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001119 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 if (error)
1121 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001122 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001124 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001125 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 } else {
1127 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001128 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 }
1130 return 0;
1131}
1132
Jan Kara3fd16462016-02-22 22:43:04 -05001133static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1134 struct ext4_xattr_info *i)
1135{
1136 void *value;
1137
1138 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1139 return 0;
1140 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1141 return !memcmp(value, i->value, i->value_len);
1142}
1143
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144/*
Mingming Cao617ba132006-10-11 01:20:53 -07001145 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001147 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 * is NULL to remove an existing extended attribute, and non-NULL to
1149 * either replace an existing extended attribute, or create a new extended
1150 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1151 * specify that an extended attribute must exist and must not exist
1152 * previous to the call, respectively.
1153 *
1154 * Returns 0, or a negative error number on failure.
1155 */
1156int
Mingming Cao617ba132006-10-11 01:20:53 -07001157ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 const char *name, const void *value, size_t value_len,
1159 int flags)
1160{
Mingming Cao617ba132006-10-11 01:20:53 -07001161 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 .name_index = name_index,
1163 .name = name,
1164 .value = value,
1165 .value_len = value_len,
1166
1167 };
Mingming Cao617ba132006-10-11 01:20:53 -07001168 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169 .s = { .not_found = -ENODATA, },
1170 };
Mingming Cao617ba132006-10-11 01:20:53 -07001171 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 .s = { .not_found = -ENODATA, },
1173 };
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001174 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 int error;
1176
1177 if (!name)
1178 return -EINVAL;
1179 if (strlen(name) > 255)
1180 return -ERANGE;
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001181 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001182
Eric Sandeen66543612011-10-26 03:32:07 -04001183 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001184 if (error)
1185 goto cleanup;
1186
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001187 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001188 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1189 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001190 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191 }
1192
Mingming Cao617ba132006-10-11 01:20:53 -07001193 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 if (error)
1195 goto cleanup;
1196 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001197 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198 if (error)
1199 goto cleanup;
1200 if (is.s.not_found && bs.s.not_found) {
1201 error = -ENODATA;
1202 if (flags & XATTR_REPLACE)
1203 goto cleanup;
1204 error = 0;
1205 if (!value)
1206 goto cleanup;
1207 } else {
1208 error = -EEXIST;
1209 if (flags & XATTR_CREATE)
1210 goto cleanup;
1211 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 if (!value) {
1213 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001214 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001216 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001217 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001218 error = 0;
1219 /* Xattr value did not change? Save us some work and bail out */
1220 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1221 goto cleanup;
1222 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1223 goto cleanup;
1224
Mingming Cao617ba132006-10-11 01:20:53 -07001225 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 if (!error && !bs.s.not_found) {
1227 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001228 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001230 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1231 error = ext4_xattr_block_find(inode, &i, &bs);
1232 if (error)
1233 goto cleanup;
1234 }
Mingming Cao617ba132006-10-11 01:20:53 -07001235 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 if (error)
1237 goto cleanup;
1238 if (!is.s.not_found) {
1239 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001240 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 &is);
1242 }
1243 }
1244 }
1245 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001246 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001247 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001248 if (!value)
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001249 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001250 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001252 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253 * error != 0.
1254 */
1255 is.iloc.bh = NULL;
1256 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001257 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 }
1259
1260cleanup:
1261 brelse(is.iloc.bh);
1262 brelse(bs.bh);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001263 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264 return error;
1265}
1266
1267/*
Mingming Cao617ba132006-10-11 01:20:53 -07001268 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 *
Mingming Cao617ba132006-10-11 01:20:53 -07001270 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 * attribute modification is a filesystem transaction by itself.
1272 *
1273 * Returns 0, or a negative error number on failure.
1274 */
1275int
Mingming Cao617ba132006-10-11 01:20:53 -07001276ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 const void *value, size_t value_len, int flags)
1278{
1279 handle_t *handle;
1280 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001281 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282
1283retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001284 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285 if (IS_ERR(handle)) {
1286 error = PTR_ERR(handle);
1287 } else {
1288 int error2;
1289
Mingming Cao617ba132006-10-11 01:20:53 -07001290 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001291 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001292 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001294 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 goto retry;
1296 if (error == 0)
1297 error = error2;
1298 }
1299
1300 return error;
1301}
1302
1303/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001304 * Shift the EA entries in the inode to create space for the increased
1305 * i_extra_isize.
1306 */
1307static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1308 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001309 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001310{
1311 struct ext4_xattr_entry *last = entry;
1312 int new_offs;
1313
Jan Kara94405712016-08-29 15:41:11 -04001314 /* We always shift xattr headers further thus offsets get lower */
1315 BUG_ON(value_offs_shift > 0);
1316
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001317 /* Adjust the value offsets of the entries */
1318 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001319 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001320 new_offs = le16_to_cpu(last->e_value_offs) +
1321 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001322 last->e_value_offs = cpu_to_le16(new_offs);
1323 }
1324 }
1325 /* Shift the entries by n bytes */
1326 memmove(to, from, n);
1327}
1328
1329/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001330 * Move xattr pointed to by 'entry' from inode into external xattr block
1331 */
1332static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1333 struct ext4_inode *raw_inode,
1334 struct ext4_xattr_entry *entry)
1335{
1336 struct ext4_xattr_ibody_find *is = NULL;
1337 struct ext4_xattr_block_find *bs = NULL;
1338 char *buffer = NULL, *b_entry_name = NULL;
1339 size_t value_offs, value_size;
1340 struct ext4_xattr_info i = {
1341 .value = NULL,
1342 .value_len = 0,
1343 .name_index = entry->e_name_index,
1344 };
1345 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1346 int error;
1347
1348 value_offs = le16_to_cpu(entry->e_value_offs);
1349 value_size = le32_to_cpu(entry->e_value_size);
1350
1351 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1352 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1353 buffer = kmalloc(value_size, GFP_NOFS);
1354 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1355 if (!is || !bs || !buffer || !b_entry_name) {
1356 error = -ENOMEM;
1357 goto out;
1358 }
1359
1360 is->s.not_found = -ENODATA;
1361 bs->s.not_found = -ENODATA;
1362 is->iloc.bh = NULL;
1363 bs->bh = NULL;
1364
1365 /* Save the entry name and the entry value */
1366 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1367 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1368 b_entry_name[entry->e_name_len] = '\0';
1369 i.name = b_entry_name;
1370
1371 error = ext4_get_inode_loc(inode, &is->iloc);
1372 if (error)
1373 goto out;
1374
1375 error = ext4_xattr_ibody_find(inode, &i, is);
1376 if (error)
1377 goto out;
1378
1379 /* Remove the chosen entry from the inode */
1380 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1381 if (error)
1382 goto out;
1383
1384 i.name = b_entry_name;
1385 i.value = buffer;
1386 i.value_len = value_size;
1387 error = ext4_xattr_block_find(inode, &i, bs);
1388 if (error)
1389 goto out;
1390
1391 /* Add entry which was removed from the inode into the block */
1392 error = ext4_xattr_block_set(handle, inode, &i, bs);
1393 if (error)
1394 goto out;
1395 error = 0;
1396out:
1397 kfree(b_entry_name);
1398 kfree(buffer);
1399 if (is)
1400 brelse(is->iloc.bh);
1401 kfree(is);
1402 kfree(bs);
1403
1404 return error;
1405}
1406
Jan Karadfa20642016-08-29 15:44:11 -04001407static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1408 struct ext4_inode *raw_inode,
1409 int isize_diff, size_t ifree,
1410 size_t bfree, int *total_ino)
1411{
1412 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1413 struct ext4_xattr_entry *small_entry;
1414 struct ext4_xattr_entry *entry;
1415 struct ext4_xattr_entry *last;
1416 unsigned int entry_size; /* EA entry size */
1417 unsigned int total_size; /* EA entry size + value size */
1418 unsigned int min_total_size;
1419 int error;
1420
1421 while (isize_diff > ifree) {
1422 entry = NULL;
1423 small_entry = NULL;
1424 min_total_size = ~0U;
1425 last = IFIRST(header);
1426 /* Find the entry best suited to be pushed into EA block */
1427 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1428 total_size =
1429 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1430 EXT4_XATTR_LEN(last->e_name_len);
1431 if (total_size <= bfree &&
1432 total_size < min_total_size) {
1433 if (total_size + ifree < isize_diff) {
1434 small_entry = last;
1435 } else {
1436 entry = last;
1437 min_total_size = total_size;
1438 }
1439 }
1440 }
1441
1442 if (entry == NULL) {
1443 if (small_entry == NULL)
1444 return -ENOSPC;
1445 entry = small_entry;
1446 }
1447
1448 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1449 total_size = entry_size +
1450 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1451 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1452 entry);
1453 if (error)
1454 return error;
1455
1456 *total_ino -= entry_size;
1457 ifree += total_size;
1458 bfree -= total_size;
1459 }
1460
1461 return 0;
1462}
1463
Jan Kara3f2571c2016-08-29 15:42:11 -04001464/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001465 * Expand an inode by new_extra_isize bytes when EAs are present.
1466 * Returns 0 on success or negative error number on failure.
1467 */
1468int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1469 struct ext4_inode *raw_inode, handle_t *handle)
1470{
1471 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001472 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001473 size_t min_offs;
1474 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001475 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001476 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001477 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001478 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04001479 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001480 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001481
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001482 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1483 return 0;
1484
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001485retry:
Jan Karad0141192016-08-11 11:50:30 -04001486 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001487 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1488 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001489
1490 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001491
1492 /*
1493 * Check if enough free space is available in the inode to shift the
1494 * entries ahead by new_extra_isize.
1495 */
1496
Jan Kara6e0cd082016-08-29 15:43:11 -04001497 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001498 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1499 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001500 total_ino = sizeof(struct ext4_xattr_ibody_header);
1501
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001502 error = xattr_check_inode(inode, header, end);
1503 if (error)
1504 goto cleanup;
1505
Jan Kara6e0cd082016-08-29 15:43:11 -04001506 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001507 if (ifree >= isize_diff)
1508 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001509
1510 /*
1511 * Enough free space isn't available in the inode, check if
1512 * EA block can hold new_extra_isize bytes.
1513 */
1514 if (EXT4_I(inode)->i_file_acl) {
1515 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1516 error = -EIO;
1517 if (!bh)
1518 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001519 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001520 EXT4_ERROR_INODE(inode, "bad block %llu",
1521 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001522 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001523 goto cleanup;
1524 }
1525 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001526 end = bh->b_data + bh->b_size;
1527 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001528 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1529 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001530 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001531 if (!tried_min_extra_isize && s_min_extra_isize) {
1532 tried_min_extra_isize++;
1533 new_extra_isize = s_min_extra_isize;
1534 brelse(bh);
1535 goto retry;
1536 }
Jan Karadfa20642016-08-29 15:44:11 -04001537 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001538 goto cleanup;
1539 }
1540 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001541 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001542 }
1543
Jan Karadfa20642016-08-29 15:44:11 -04001544 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1545 isize_diff, ifree, bfree,
1546 &total_ino);
1547 if (error) {
1548 if (error == -ENOSPC && !tried_min_extra_isize &&
1549 s_min_extra_isize) {
1550 tried_min_extra_isize++;
1551 new_extra_isize = s_min_extra_isize;
1552 brelse(bh);
1553 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001554 }
Jan Karadfa20642016-08-29 15:44:11 -04001555 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001556 }
Jan Karae3014d12016-08-29 15:38:11 -04001557shift:
1558 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001559 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001560 - new_extra_isize, (void *)raw_inode +
1561 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001562 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001563 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001564 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001565out:
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001566 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001567 return 0;
1568
1569cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001570 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001571 /*
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001572 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001573 */
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001574 no_expand = 1;
1575 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001576 return error;
1577}
1578
1579
1580
1581/*
Mingming Cao617ba132006-10-11 01:20:53 -07001582 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 *
1584 * Free extended attribute resources associated with this inode. This
1585 * is called immediately before an inode is freed. We have exclusive
1586 * access to the inode.
1587 */
1588void
Mingming Cao617ba132006-10-11 01:20:53 -07001589ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001590{
1591 struct buffer_head *bh = NULL;
1592
Mingming Cao617ba132006-10-11 01:20:53 -07001593 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001594 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001595 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001597 EXT4_ERROR_INODE(inode, "block %llu read error",
1598 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 goto cleanup;
1600 }
Mingming Cao617ba132006-10-11 01:20:53 -07001601 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001603 EXT4_ERROR_INODE(inode, "bad block %llu",
1604 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 goto cleanup;
1606 }
Mingming Cao617ba132006-10-11 01:20:53 -07001607 ext4_xattr_release_block(handle, inode, bh);
1608 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609
1610cleanup:
1611 brelse(bh);
1612}
1613
1614/*
Mingming Cao617ba132006-10-11 01:20:53 -07001615 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616 *
1617 * Create a new entry in the extended attribute cache, and insert
1618 * it unless such an entry is already in the cache.
1619 *
1620 * Returns 0, or a negative error number on failure.
1621 */
1622static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001623ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001625 struct ext4_xattr_header *header = BHDR(bh);
1626 __u32 hash = le32_to_cpu(header->h_hash);
1627 int reusable = le32_to_cpu(header->h_refcount) <
1628 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 int error;
1630
Jan Kara7a2508e2016-02-22 22:35:22 -05001631 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001632 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001633 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001634 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001635 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001636 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001637 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638}
1639
1640/*
Mingming Cao617ba132006-10-11 01:20:53 -07001641 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 *
1643 * Compare two extended attribute blocks for equality.
1644 *
1645 * Returns 0 if the blocks are equal, 1 if they differ, and
1646 * a negative error number on errors.
1647 */
1648static int
Mingming Cao617ba132006-10-11 01:20:53 -07001649ext4_xattr_cmp(struct ext4_xattr_header *header1,
1650 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651{
Mingming Cao617ba132006-10-11 01:20:53 -07001652 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653
1654 entry1 = ENTRY(header1+1);
1655 entry2 = ENTRY(header2+1);
1656 while (!IS_LAST_ENTRY(entry1)) {
1657 if (IS_LAST_ENTRY(entry2))
1658 return 1;
1659 if (entry1->e_hash != entry2->e_hash ||
1660 entry1->e_name_index != entry2->e_name_index ||
1661 entry1->e_name_len != entry2->e_name_len ||
1662 entry1->e_value_size != entry2->e_value_size ||
1663 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1664 return 1;
1665 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001666 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1668 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1669 le32_to_cpu(entry1->e_value_size)))
1670 return 1;
1671
Mingming Cao617ba132006-10-11 01:20:53 -07001672 entry1 = EXT4_XATTR_NEXT(entry1);
1673 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674 }
1675 if (!IS_LAST_ENTRY(entry2))
1676 return 1;
1677 return 0;
1678}
1679
1680/*
Mingming Cao617ba132006-10-11 01:20:53 -07001681 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001682 *
1683 * Find an identical extended attribute block.
1684 *
1685 * Returns a pointer to the block found, or NULL if such a block was
1686 * not found or an error occurred.
1687 */
1688static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001689ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001690 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691{
1692 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001693 struct mb_cache_entry *ce;
1694 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695
1696 if (!header->h_hash)
1697 return NULL; /* never share */
1698 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001699 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 while (ce) {
1701 struct buffer_head *bh;
1702
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001703 bh = sb_bread(inode->i_sb, ce->e_block);
1704 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001705 EXT4_ERROR_INODE(inode, "block %lu read error",
1706 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001707 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708 *pce = ce;
1709 return bh;
1710 }
1711 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001712 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713 }
1714 return NULL;
1715}
1716
1717#define NAME_HASH_SHIFT 5
1718#define VALUE_HASH_SHIFT 16
1719
1720/*
Mingming Cao617ba132006-10-11 01:20:53 -07001721 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722 *
1723 * Compute the hash of an extended attribute.
1724 */
Mingming Cao617ba132006-10-11 01:20:53 -07001725static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1726 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727{
1728 __u32 hash = 0;
1729 char *name = entry->e_name;
1730 int n;
1731
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001732 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733 hash = (hash << NAME_HASH_SHIFT) ^
1734 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1735 *name++;
1736 }
1737
Jan Kara1cba4232016-08-29 15:40:11 -04001738 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001739 __le32 *value = (__le32 *)((char *)header +
1740 le16_to_cpu(entry->e_value_offs));
1741 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001742 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743 hash = (hash << VALUE_HASH_SHIFT) ^
1744 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1745 le32_to_cpu(*value++);
1746 }
1747 }
1748 entry->e_hash = cpu_to_le32(hash);
1749}
1750
1751#undef NAME_HASH_SHIFT
1752#undef VALUE_HASH_SHIFT
1753
1754#define BLOCK_HASH_SHIFT 16
1755
1756/*
Mingming Cao617ba132006-10-11 01:20:53 -07001757 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758 *
1759 * Re-compute the extended attribute hash value after an entry has changed.
1760 */
Mingming Cao617ba132006-10-11 01:20:53 -07001761static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1762 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763{
Mingming Cao617ba132006-10-11 01:20:53 -07001764 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765 __u32 hash = 0;
1766
Mingming Cao617ba132006-10-11 01:20:53 -07001767 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768 here = ENTRY(header+1);
1769 while (!IS_LAST_ENTRY(here)) {
1770 if (!here->e_hash) {
1771 /* Block is not shared if an entry's hash value == 0 */
1772 hash = 0;
1773 break;
1774 }
1775 hash = (hash << BLOCK_HASH_SHIFT) ^
1776 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1777 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001778 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779 }
1780 header->h_hash = cpu_to_le32(hash);
1781}
1782
1783#undef BLOCK_HASH_SHIFT
1784
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001785#define HASH_BUCKET_BITS 10
1786
Jan Kara7a2508e2016-02-22 22:35:22 -05001787struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001788ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001789{
Jan Kara7a2508e2016-02-22 22:35:22 -05001790 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791}
1792
Jan Kara7a2508e2016-02-22 22:35:22 -05001793void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001794{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001795 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001796 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001797}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001798