blob: 39e9cfb1b3715c2fcf2b81c0e03c2183d58c9100 [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
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064# define ea_idebug(inode, f...) do { \
65 printk(KERN_DEBUG "inode %s:%lu: ", \
66 inode->i_sb->s_id, inode->i_ino); \
67 printk(f); \
68 printk("\n"); \
69 } while (0)
70# define ea_bdebug(bh, f...) do { \
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +040071 printk(KERN_DEBUG "block %pg:%lu: ", \
72 bh->b_bdev, (unsigned long) bh->b_blocknr); \
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073 printk(f); \
74 printk("\n"); \
75 } while (0)
76#else
Joe Perchesace36ad2012-03-19 23:11:43 -040077# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
78# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070079#endif
80
Jan Kara7a2508e2016-02-22 22:35:22 -050081static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070082static struct buffer_head *ext4_xattr_cache_find(struct inode *,
83 struct ext4_xattr_header *,
Jan Kara7a2508e2016-02-22 22:35:22 -050084 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070085static void ext4_xattr_rehash(struct ext4_xattr_header *,
86 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000087static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040088 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089
Stephen Hemminger11e27522010-05-13 17:53:18 -070090static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070091 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040092#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080093 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
94 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095#endif
Mingming Cao617ba132006-10-11 01:20:53 -070096 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040097#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070098 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070099#endif
100};
101
Stephen Hemminger11e27522010-05-13 17:53:18 -0700102const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700103 &ext4_xattr_user_handler,
104 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400105#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800106 &posix_acl_access_xattr_handler,
107 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400109#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700110 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111#endif
112 NULL
113};
114
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400115#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
116 inode->i_sb->s_fs_info)->s_mb_cache)
117
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400118static __le32 ext4_xattr_block_csum(struct inode *inode,
119 sector_t block_nr,
120 struct ext4_xattr_header *hdr)
121{
122 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400123 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400124 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400125 __u32 dummy_csum = 0;
126 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400127
Theodore Ts'od6a77102013-04-09 23:59:55 -0400128 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
129 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400130 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
131 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
132 offset += sizeof(dummy_csum);
133 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
134 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400135
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400136 return cpu_to_le32(csum);
137}
138
139static int ext4_xattr_block_csum_verify(struct inode *inode,
140 sector_t block_nr,
141 struct ext4_xattr_header *hdr)
142{
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400143 if (ext4_has_metadata_csum(inode->i_sb) &&
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400144 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
145 return 0;
146 return 1;
147}
148
149static void ext4_xattr_block_csum_set(struct inode *inode,
150 sector_t block_nr,
151 struct ext4_xattr_header *hdr)
152{
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400153 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400154 return;
155
156 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
157}
158
159static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
160 struct inode *inode,
161 struct buffer_head *bh)
162{
163 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
164 return ext4_handle_dirty_metadata(handle, inode, bh);
165}
166
Stephen Hemminger11e27522010-05-13 17:53:18 -0700167static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700168ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700170 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171
Mingming Cao617ba132006-10-11 01:20:53 -0700172 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
173 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 return handler;
175}
176
177/*
178 * Inode operation listxattr()
179 *
David Howells2b0143b2015-03-17 22:25:59 +0000180 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 */
182ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700183ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000185 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186}
187
188static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
190 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400192 struct ext4_xattr_entry *e = entry;
193
194 while (!IS_LAST_ENTRY(e)) {
195 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400197 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400198 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700199 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400200
201 while (!IS_LAST_ENTRY(entry)) {
202 if (entry->e_value_size != 0 &&
203 (value_start + le16_to_cpu(entry->e_value_offs) <
204 (void *)e + sizeof(__u32) ||
205 value_start + le16_to_cpu(entry->e_value_offs) +
206 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400207 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400208 entry = EXT4_XATTR_NEXT(entry);
209 }
210
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211 return 0;
212}
213
214static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400215ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400217 int error;
218
219 if (buffer_verified(bh))
220 return 0;
221
Mingming Cao617ba132006-10-11 01:20:53 -0700222 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400224 return -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400225 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400226 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400227 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
228 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400229 if (!error)
230 set_buffer_verified(bh);
231 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232}
233
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400234static int
235__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
236 void *end, const char *function, unsigned int line)
237{
238 struct ext4_xattr_entry *entry = IFIRST(header);
239 int error = -EFSCORRUPTED;
240
241 if (((void *) header >= end) ||
242 (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
243 goto errout;
244 error = ext4_xattr_check_names(entry, end, entry);
245errout:
246 if (error)
247 __ext4_error_inode(inode, function, line, 0,
248 "corrupted in-inode xattr");
249 return error;
250}
251
252#define xattr_check_inode(inode, header, end) \
253 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
254
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700256ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257{
258 size_t value_size = le32_to_cpu(entry->e_value_size);
259
260 if (entry->e_value_block != 0 || value_size > size ||
261 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400262 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 return 0;
264}
265
266static int
Mingming Cao617ba132006-10-11 01:20:53 -0700267ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 const char *name, size_t size, int sorted)
269{
Mingming Cao617ba132006-10-11 01:20:53 -0700270 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700271 size_t name_len;
272 int cmp = 1;
273
274 if (name == NULL)
275 return -EINVAL;
276 name_len = strlen(name);
277 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700278 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 cmp = name_index - entry->e_name_index;
280 if (!cmp)
281 cmp = name_len - entry->e_name_len;
282 if (!cmp)
283 cmp = memcmp(name, entry->e_name, name_len);
284 if (cmp <= 0 && (sorted || cmp == 0))
285 break;
286 }
287 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700288 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400289 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 return cmp ? -ENODATA : 0;
291}
292
293static int
Mingming Cao617ba132006-10-11 01:20:53 -0700294ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 void *buffer, size_t buffer_size)
296{
297 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700298 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 size_t size;
300 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500301 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302
303 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
304 name_index, name, buffer, (long)buffer_size);
305
306 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700307 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400309 ea_idebug(inode, "reading block %llu",
310 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700311 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 if (!bh)
313 goto cleanup;
314 ea_bdebug(bh, "b_count=%d, refcount=%d",
315 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400316 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500317bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400318 EXT4_ERROR_INODE(inode, "bad block %llu",
319 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400320 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 goto cleanup;
322 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400323 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700325 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400326 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 goto bad_block;
328 if (error)
329 goto cleanup;
330 size = le32_to_cpu(entry->e_value_size);
331 if (buffer) {
332 error = -ERANGE;
333 if (size > buffer_size)
334 goto cleanup;
335 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
336 size);
337 }
338 error = size;
339
340cleanup:
341 brelse(bh);
342 return error;
343}
344
Tao Ma879b3822012-12-05 10:28:46 -0500345int
Mingming Cao617ba132006-10-11 01:20:53 -0700346ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700347 void *buffer, size_t buffer_size)
348{
Mingming Cao617ba132006-10-11 01:20:53 -0700349 struct ext4_xattr_ibody_header *header;
350 struct ext4_xattr_entry *entry;
351 struct ext4_inode *raw_inode;
352 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353 size_t size;
354 void *end;
355 int error;
356
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500357 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700359 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360 if (error)
361 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700362 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 header = IHDR(inode, raw_inode);
364 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700365 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400366 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 if (error)
368 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700369 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 end - (void *)entry, 0);
371 if (error)
372 goto cleanup;
373 size = le32_to_cpu(entry->e_value_size);
374 if (buffer) {
375 error = -ERANGE;
376 if (size > buffer_size)
377 goto cleanup;
378 memcpy(buffer, (void *)IFIRST(header) +
379 le16_to_cpu(entry->e_value_offs), size);
380 }
381 error = size;
382
383cleanup:
384 brelse(iloc.bh);
385 return error;
386}
387
388/*
Mingming Cao617ba132006-10-11 01:20:53 -0700389 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700390 *
391 * Copy an extended attribute into the buffer
392 * provided, or compute the buffer size required.
393 * Buffer is NULL to compute the size of the buffer required.
394 *
395 * Returns a negative error number on failure, or the number of bytes
396 * used / required on success.
397 */
398int
Mingming Cao617ba132006-10-11 01:20:53 -0700399ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 void *buffer, size_t buffer_size)
401{
402 int error;
403
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400404 if (strlen(name) > 255)
405 return -ERANGE;
406
Mingming Cao617ba132006-10-11 01:20:53 -0700407 down_read(&EXT4_I(inode)->xattr_sem);
408 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 buffer_size);
410 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700411 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700413 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 return error;
415}
416
417static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000418ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700419 char *buffer, size_t buffer_size)
420{
421 size_t rest = buffer_size;
422
Mingming Cao617ba132006-10-11 01:20:53 -0700423 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700424 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700425 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700426
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100427 if (handler && (!handler->list || handler->list(dentry))) {
428 const char *prefix = handler->prefix ?: handler->name;
429 size_t prefix_len = strlen(prefix);
430 size_t size = prefix_len + entry->e_name_len + 1;
431
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700432 if (buffer) {
433 if (size > rest)
434 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100435 memcpy(buffer, prefix, prefix_len);
436 buffer += prefix_len;
437 memcpy(buffer, entry->e_name, entry->e_name_len);
438 buffer += entry->e_name_len;
439 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 }
441 rest -= size;
442 }
443 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100444 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445}
446
447static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000448ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449{
David Howells2b0143b2015-03-17 22:25:59 +0000450 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 struct buffer_head *bh = NULL;
452 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500453 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454
455 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
456 buffer, (long)buffer_size);
457
458 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700459 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400461 ea_idebug(inode, "reading block %llu",
462 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700463 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464 error = -EIO;
465 if (!bh)
466 goto cleanup;
467 ea_bdebug(bh, "b_count=%d, refcount=%d",
468 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400469 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400470 EXT4_ERROR_INODE(inode, "bad block %llu",
471 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400472 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 goto cleanup;
474 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400475 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000476 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477
478cleanup:
479 brelse(bh);
480
481 return error;
482}
483
484static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000485ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486{
David Howells2b0143b2015-03-17 22:25:59 +0000487 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700488 struct ext4_xattr_ibody_header *header;
489 struct ext4_inode *raw_inode;
490 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 void *end;
492 int error;
493
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500494 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700496 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 if (error)
498 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700499 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700501 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400502 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503 if (error)
504 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000505 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506 buffer, buffer_size);
507
508cleanup:
509 brelse(iloc.bh);
510 return error;
511}
512
513/*
Mingming Cao617ba132006-10-11 01:20:53 -0700514 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 *
516 * Copy a list of attribute names into the buffer
517 * provided, or compute the buffer size required.
518 * Buffer is NULL to compute the size of the buffer required.
519 *
520 * Returns a negative error number on failure, or the number of bytes
521 * used / required on success.
522 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400523static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000524ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500526 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527
David Howells2b0143b2015-03-17 22:25:59 +0000528 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500529 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
530 if (ret < 0)
531 goto errout;
532 if (buffer) {
533 buffer += ret;
534 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500536 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
537 if (ret < 0)
538 goto errout;
539 ret += ret2;
540errout:
David Howells2b0143b2015-03-17 22:25:59 +0000541 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500542 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543}
544
545/*
Mingming Cao617ba132006-10-11 01:20:53 -0700546 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 * not set, set it.
548 */
Mingming Cao617ba132006-10-11 01:20:53 -0700549static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 struct super_block *sb)
551{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400552 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 return;
554
liang xie5d601252014-05-12 22:06:43 -0400555 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700556 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400557 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400558 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560}
561
562/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400563 * Release the xattr block BH: If the reference count is > 1, decrement it;
564 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 */
566static void
Mingming Cao617ba132006-10-11 01:20:53 -0700567ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 struct buffer_head *bh)
569{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500570 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
571 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800572 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573
liang xie5d601252014-05-12 22:06:43 -0400574 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800575 error = ext4_journal_get_write_access(handle, bh);
576 if (error)
577 goto out;
578
579 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500580 hash = le32_to_cpu(BHDR(bh)->h_hash);
581 ref = le32_to_cpu(BHDR(bh)->h_refcount);
582 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500584 /*
585 * This must happen under buffer lock for
586 * ext4_xattr_block_set() to reliably detect freed block
587 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500588 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400590 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500591 ext4_free_blocks(handle, inode, bh, 0, 1,
592 EXT4_FREE_BLOCKS_METADATA |
593 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700594 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500595 ref--;
596 BHDR(bh)->h_refcount = cpu_to_le32(ref);
597 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
598 struct mb_cache_entry *ce;
599
600 ce = mb_cache_entry_get(ext4_mb_cache, hash,
601 bh->b_blocknr);
602 if (ce) {
603 ce->e_reusable = 1;
604 mb_cache_entry_put(ext4_mb_cache, ce);
605 }
606 }
607
Jan Karaec4cb1a2014-04-07 10:54:21 -0400608 /*
609 * Beware of this ugliness: Releasing of xattr block references
610 * from different inodes can race and so we have to protect
611 * from a race where someone else frees the block (and releases
612 * its journal_head) before we are done dirtying the buffer. In
613 * nojournal mode this race is harmless and we actually cannot
614 * call ext4_handle_dirty_xattr_block() with locked buffer as
615 * that function can call sync_dirty_buffer() so for that case
616 * we handle the dirtying after unlocking the buffer.
617 */
618 if (ext4_handle_valid(handle))
619 error = ext4_handle_dirty_xattr_block(handle, inode,
620 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500621 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400622 if (!ext4_handle_valid(handle))
623 error = ext4_handle_dirty_xattr_block(handle, inode,
624 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800625 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500626 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500627 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800628 ea_bdebug(bh, "refcount now=%d; releasing",
629 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800631out:
632 ext4_std_error(inode->i_sb, error);
633 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700634}
635
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400636/*
637 * Find the available free space for EAs. This also returns the total number of
638 * bytes used by EA entries.
639 */
640static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
641 size_t *min_offs, void *base, int *total)
642{
643 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400644 if (!last->e_value_block && last->e_value_size) {
645 size_t offs = le16_to_cpu(last->e_value_offs);
646 if (offs < *min_offs)
647 *min_offs = offs;
648 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500649 if (total)
650 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400651 }
652 return (*min_offs - ((void *)last - base) - sizeof(__u32));
653}
654
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655static int
Mingming Cao617ba132006-10-11 01:20:53 -0700656ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657{
Mingming Cao617ba132006-10-11 01:20:53 -0700658 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
660
661 /* Compute min_offs and last. */
662 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700663 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 if (!last->e_value_block && last->e_value_size) {
665 size_t offs = le16_to_cpu(last->e_value_offs);
666 if (offs < min_offs)
667 min_offs = offs;
668 }
669 }
670 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
671 if (!s->not_found) {
672 if (!s->here->e_value_block && s->here->e_value_size) {
673 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700674 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 }
Mingming Cao617ba132006-10-11 01:20:53 -0700676 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 }
678 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400679 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700680 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 return -ENOSPC;
682 }
683
684 if (i->value && s->not_found) {
685 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700686 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
688 memmove((void *)s->here + size, s->here, rest);
689 memset(s->here, 0, size);
690 s->here->e_name_index = i->name_index;
691 s->here->e_name_len = name_len;
692 memcpy(s->here->e_name, i->name, name_len);
693 } else {
694 if (!s->here->e_value_block && s->here->e_value_size) {
695 void *first_val = s->base + min_offs;
696 size_t offs = le16_to_cpu(s->here->e_value_offs);
697 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700698 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 le32_to_cpu(s->here->e_value_size));
700
Mingming Cao617ba132006-10-11 01:20:53 -0700701 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702 /* The old and the new value have the same
703 size. Just replace. */
704 s->here->e_value_size =
705 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500706 if (i->value == EXT4_ZERO_XATTR_VALUE) {
707 memset(val, 0, size);
708 } else {
709 /* Clear pad bytes first. */
710 memset(val + size - EXT4_XATTR_PAD, 0,
711 EXT4_XATTR_PAD);
712 memcpy(val, i->value, i->value_len);
713 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 return 0;
715 }
716
717 /* Remove the old value. */
718 memmove(first_val + size, first_val, val - first_val);
719 memset(first_val, 0, size);
720 s->here->e_value_size = 0;
721 s->here->e_value_offs = 0;
722 min_offs += size;
723
724 /* Adjust all value offsets. */
725 last = s->first;
726 while (!IS_LAST_ENTRY(last)) {
727 size_t o = le16_to_cpu(last->e_value_offs);
728 if (!last->e_value_block &&
729 last->e_value_size && o < offs)
730 last->e_value_offs =
731 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700732 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 }
734 }
735 if (!i->value) {
736 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700737 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 last = ENTRY((void *)last - size);
739 memmove(s->here, (void *)s->here + size,
740 (void *)last - (void *)s->here + sizeof(__u32));
741 memset(last, 0, size);
742 }
743 }
744
745 if (i->value) {
746 /* Insert the new value. */
747 s->here->e_value_size = cpu_to_le32(i->value_len);
748 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700749 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 void *val = s->base + min_offs - size;
751 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500752 if (i->value == EXT4_ZERO_XATTR_VALUE) {
753 memset(val, 0, size);
754 } else {
755 /* Clear the pad bytes first. */
756 memset(val + size - EXT4_XATTR_PAD, 0,
757 EXT4_XATTR_PAD);
758 memcpy(val, i->value, i->value_len);
759 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 }
761 }
762 return 0;
763}
764
Mingming Cao617ba132006-10-11 01:20:53 -0700765struct ext4_xattr_block_find {
766 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 struct buffer_head *bh;
768};
769
770static int
Mingming Cao617ba132006-10-11 01:20:53 -0700771ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
772 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773{
774 struct super_block *sb = inode->i_sb;
775 int error;
776
777 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
778 i->name_index, i->name, i->value, (long)i->value_len);
779
Mingming Cao617ba132006-10-11 01:20:53 -0700780 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700782 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 error = -EIO;
784 if (!bs->bh)
785 goto cleanup;
786 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
787 atomic_read(&(bs->bh->b_count)),
788 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400789 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400790 EXT4_ERROR_INODE(inode, "bad block %llu",
791 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400792 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 goto cleanup;
794 }
795 /* Find the named attribute. */
796 bs->s.base = BHDR(bs->bh);
797 bs->s.first = BFIRST(bs->bh);
798 bs->s.end = bs->bh->b_data + bs->bh->b_size;
799 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700800 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 i->name, bs->bh->b_size, 1);
802 if (error && error != -ENODATA)
803 goto cleanup;
804 bs->s.not_found = error;
805 }
806 error = 0;
807
808cleanup:
809 return error;
810}
811
812static int
Mingming Cao617ba132006-10-11 01:20:53 -0700813ext4_xattr_block_set(handle_t *handle, struct inode *inode,
814 struct ext4_xattr_info *i,
815 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816{
817 struct super_block *sb = inode->i_sb;
818 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700819 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500820 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800821 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500822 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823
Mingming Cao617ba132006-10-11 01:20:53 -0700824#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825
826 if (i->value && i->value_len > sb->s_blocksize)
827 return -ENOSPC;
828 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400829 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800830 error = ext4_journal_get_write_access(handle, bs->bh);
831 if (error)
832 goto cleanup;
833 lock_buffer(bs->bh);
834
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700835 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500836 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
837
838 /*
839 * This must happen under buffer lock for
840 * ext4_xattr_block_set() to reliably detect modified
841 * block
842 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500843 mb_cache_entry_delete_block(ext4_mb_cache, hash,
844 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700846 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847 if (!error) {
848 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700849 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400851 ext4_xattr_cache_insert(ext4_mb_cache,
852 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 }
854 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400855 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 goto bad_block;
857 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400858 error = ext4_handle_dirty_xattr_block(handle,
859 inode,
860 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 if (error)
862 goto cleanup;
863 goto inserted;
864 } else {
865 int offset = (char *)s->here - bs->bh->b_data;
866
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800867 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400869 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 error = -ENOMEM;
871 if (s->base == NULL)
872 goto cleanup;
873 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
874 s->first = ENTRY(header(s->base)+1);
875 header(s->base)->h_refcount = cpu_to_le32(1);
876 s->here = ENTRY(s->base + offset);
877 s->end = s->base + bs->bh->b_size;
878 }
879 } else {
880 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400881 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 /* assert(header == s->base) */
883 error = -ENOMEM;
884 if (s->base == NULL)
885 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700886 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887 header(s->base)->h_blocks = cpu_to_le32(1);
888 header(s->base)->h_refcount = cpu_to_le32(1);
889 s->first = ENTRY(header(s->base)+1);
890 s->here = ENTRY(header(s->base)+1);
891 s->end = s->base + sb->s_blocksize;
892 }
893
Mingming Cao617ba132006-10-11 01:20:53 -0700894 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400895 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896 goto bad_block;
897 if (error)
898 goto cleanup;
899 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700900 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901
902inserted:
903 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700904 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 if (new_bh) {
906 /* We found an identical block in the cache. */
907 if (new_bh == bs->bh)
908 ea_bdebug(new_bh, "keeping");
909 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500910 u32 ref;
911
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 /* The old block is released after updating
913 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500914 error = dquot_alloc_block(inode,
915 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500916 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400918 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700919 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920 new_bh);
921 if (error)
922 goto cleanup_dquot;
923 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500924 /*
925 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500926 * freeing, rehashing or adding references to
927 * xattr block. Once we hold buffer lock xattr
928 * block's state is stable so we can check
929 * whether the block got freed / rehashed or
930 * not. Since we unhash mbcache entry under
931 * buffer lock when freeing / rehashing xattr
932 * block, checking whether entry is still
933 * hashed is reliable. Same rules hold for
934 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500935 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500936 if (hlist_bl_unhashed(&ce->e_hash_list) ||
937 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500938 /*
939 * Undo everything and check mbcache
940 * again.
941 */
942 unlock_buffer(new_bh);
943 dquot_free_block(inode,
944 EXT4_C2B(EXT4_SB(sb),
945 1));
946 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500947 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500948 ce = NULL;
949 new_bh = NULL;
950 goto inserted;
951 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500952 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
953 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
954 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
955 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500957 ref);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400959 error = ext4_handle_dirty_xattr_block(handle,
960 inode,
961 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 if (error)
963 goto cleanup_dquot;
964 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500965 mb_cache_entry_touch(ext4_mb_cache, ce);
966 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 ce = NULL;
968 } else if (bs->bh && s->base == bs->bh->b_data) {
969 /* We were modifying this block in-place. */
970 ea_bdebug(bs->bh, "keeping this block");
971 new_bh = bs->bh;
972 get_bh(new_bh);
973 } else {
974 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400975 ext4_fsblk_t goal, block;
976
977 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400978 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400979
980 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400981 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400982 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
983
Allison Henderson55f020d2011-05-25 07:41:26 -0400984 block = ext4_new_meta_blocks(handle, inode, goal, 0,
985 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 if (error)
987 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400988
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400989 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400990 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
991
Joe Perchesace36ad2012-03-19 23:11:43 -0400992 ea_idebug(inode, "creating block %llu",
993 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700994
995 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500996 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500997 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500999 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001000 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 goto cleanup;
1002 }
1003 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001004 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 if (error) {
1006 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001007 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008 goto getblk_failed;
1009 }
1010 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1011 set_buffer_uptodate(new_bh);
1012 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001013 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001014 error = ext4_handle_dirty_xattr_block(handle,
1015 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 if (error)
1017 goto cleanup;
1018 }
1019 }
1020
1021 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001022 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001023
1024 /* Drop the previous xattr block. */
1025 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001026 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001027 error = 0;
1028
1029cleanup:
1030 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001031 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032 brelse(new_bh);
1033 if (!(bs->bh && s->base == bs->bh->b_data))
1034 kfree(s->base);
1035
1036 return error;
1037
1038cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001039 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 goto cleanup;
1041
1042bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001043 EXT4_ERROR_INODE(inode, "bad block %llu",
1044 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045 goto cleanup;
1046
1047#undef header
1048}
1049
Tao Ma879b3822012-12-05 10:28:46 -05001050int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1051 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052{
Mingming Cao617ba132006-10-11 01:20:53 -07001053 struct ext4_xattr_ibody_header *header;
1054 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 int error;
1056
Mingming Cao617ba132006-10-11 01:20:53 -07001057 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001059 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001060 header = IHDR(inode, raw_inode);
1061 is->s.base = is->s.first = IFIRST(header);
1062 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001063 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001064 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001065 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066 if (error)
1067 return error;
1068 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001069 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 i->name, is->s.end -
1071 (void *)is->s.base, 0);
1072 if (error && error != -ENODATA)
1073 return error;
1074 is->s.not_found = error;
1075 }
1076 return 0;
1077}
1078
Tao Ma0d812f72012-12-10 14:06:02 -05001079int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1080 struct ext4_xattr_info *i,
1081 struct ext4_xattr_ibody_find *is)
1082{
1083 struct ext4_xattr_ibody_header *header;
1084 struct ext4_xattr_search *s = &is->s;
1085 int error;
1086
1087 if (EXT4_I(inode)->i_extra_isize == 0)
1088 return -ENOSPC;
1089 error = ext4_xattr_set_entry(i, s);
1090 if (error) {
1091 if (error == -ENOSPC &&
1092 ext4_has_inline_data(inode)) {
1093 error = ext4_try_to_evict_inline_data(handle, inode,
1094 EXT4_XATTR_LEN(strlen(i->name) +
1095 EXT4_XATTR_SIZE(i->value_len)));
1096 if (error)
1097 return error;
1098 error = ext4_xattr_ibody_find(inode, i, is);
1099 if (error)
1100 return error;
1101 error = ext4_xattr_set_entry(i, s);
1102 }
1103 if (error)
1104 return error;
1105 }
1106 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1107 if (!IS_LAST_ENTRY(s->first)) {
1108 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1109 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1110 } else {
1111 header->h_magic = cpu_to_le32(0);
1112 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1113 }
1114 return 0;
1115}
1116
1117static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1118 struct ext4_xattr_info *i,
1119 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120{
Mingming Cao617ba132006-10-11 01:20:53 -07001121 struct ext4_xattr_ibody_header *header;
1122 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 int error;
1124
Mingming Cao617ba132006-10-11 01:20:53 -07001125 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001127 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 if (error)
1129 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001130 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001132 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001133 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 } else {
1135 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001136 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001137 }
1138 return 0;
1139}
1140
Jan Kara3fd16462016-02-22 22:43:04 -05001141static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1142 struct ext4_xattr_info *i)
1143{
1144 void *value;
1145
1146 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1147 return 0;
1148 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1149 return !memcmp(value, i->value, i->value_len);
1150}
1151
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152/*
Mingming Cao617ba132006-10-11 01:20:53 -07001153 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001154 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001155 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156 * is NULL to remove an existing extended attribute, and non-NULL to
1157 * either replace an existing extended attribute, or create a new extended
1158 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1159 * specify that an extended attribute must exist and must not exist
1160 * previous to the call, respectively.
1161 *
1162 * Returns 0, or a negative error number on failure.
1163 */
1164int
Mingming Cao617ba132006-10-11 01:20:53 -07001165ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 const char *name, const void *value, size_t value_len,
1167 int flags)
1168{
Mingming Cao617ba132006-10-11 01:20:53 -07001169 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 .name_index = name_index,
1171 .name = name,
1172 .value = value,
1173 .value_len = value_len,
1174
1175 };
Mingming Cao617ba132006-10-11 01:20:53 -07001176 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 .s = { .not_found = -ENODATA, },
1178 };
Mingming Cao617ba132006-10-11 01:20:53 -07001179 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 .s = { .not_found = -ENODATA, },
1181 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001182 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 int error;
1184
1185 if (!name)
1186 return -EINVAL;
1187 if (strlen(name) > 255)
1188 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001189 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001190 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1191 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001192
Eric Sandeen66543612011-10-26 03:32:07 -04001193 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001194 if (error)
1195 goto cleanup;
1196
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001197 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001198 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1199 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001200 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 }
1202
Mingming Cao617ba132006-10-11 01:20:53 -07001203 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 if (error)
1205 goto cleanup;
1206 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001207 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 if (error)
1209 goto cleanup;
1210 if (is.s.not_found && bs.s.not_found) {
1211 error = -ENODATA;
1212 if (flags & XATTR_REPLACE)
1213 goto cleanup;
1214 error = 0;
1215 if (!value)
1216 goto cleanup;
1217 } else {
1218 error = -EEXIST;
1219 if (flags & XATTR_CREATE)
1220 goto cleanup;
1221 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 if (!value) {
1223 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001224 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001226 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001227 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001228 error = 0;
1229 /* Xattr value did not change? Save us some work and bail out */
1230 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1231 goto cleanup;
1232 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1233 goto cleanup;
1234
Mingming Cao617ba132006-10-11 01:20:53 -07001235 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 if (!error && !bs.s.not_found) {
1237 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001238 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001240 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1241 error = ext4_xattr_block_find(inode, &i, &bs);
1242 if (error)
1243 goto cleanup;
1244 }
Mingming Cao617ba132006-10-11 01:20:53 -07001245 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 if (error)
1247 goto cleanup;
1248 if (!is.s.not_found) {
1249 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001250 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251 &is);
1252 }
1253 }
1254 }
1255 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001256 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001257 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001258 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001259 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001260 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001261 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001262 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 * error != 0.
1264 */
1265 is.iloc.bh = NULL;
1266 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001267 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268 }
1269
1270cleanup:
1271 brelse(is.iloc.bh);
1272 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001273 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001274 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001275 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 return error;
1277}
1278
1279/*
Mingming Cao617ba132006-10-11 01:20:53 -07001280 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 *
Mingming Cao617ba132006-10-11 01:20:53 -07001282 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 * attribute modification is a filesystem transaction by itself.
1284 *
1285 * Returns 0, or a negative error number on failure.
1286 */
1287int
Mingming Cao617ba132006-10-11 01:20:53 -07001288ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001289 const void *value, size_t value_len, int flags)
1290{
1291 handle_t *handle;
1292 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001293 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294
1295retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001296 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 if (IS_ERR(handle)) {
1298 error = PTR_ERR(handle);
1299 } else {
1300 int error2;
1301
Mingming Cao617ba132006-10-11 01:20:53 -07001302 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001303 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001304 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001306 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 goto retry;
1308 if (error == 0)
1309 error = error2;
1310 }
1311
1312 return error;
1313}
1314
1315/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001316 * Shift the EA entries in the inode to create space for the increased
1317 * i_extra_isize.
1318 */
1319static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1320 int value_offs_shift, void *to,
1321 void *from, size_t n, int blocksize)
1322{
1323 struct ext4_xattr_entry *last = entry;
1324 int new_offs;
1325
1326 /* Adjust the value offsets of the entries */
1327 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1328 if (!last->e_value_block && last->e_value_size) {
1329 new_offs = le16_to_cpu(last->e_value_offs) +
1330 value_offs_shift;
1331 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1332 > blocksize);
1333 last->e_value_offs = cpu_to_le16(new_offs);
1334 }
1335 }
1336 /* Shift the entries by n bytes */
1337 memmove(to, from, n);
1338}
1339
1340/*
1341 * Expand an inode by new_extra_isize bytes when EAs are present.
1342 * Returns 0 on success or negative error number on failure.
1343 */
1344int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1345 struct ext4_inode *raw_inode, handle_t *handle)
1346{
1347 struct ext4_xattr_ibody_header *header;
1348 struct ext4_xattr_entry *entry, *last, *first;
1349 struct buffer_head *bh = NULL;
1350 struct ext4_xattr_ibody_find *is = NULL;
1351 struct ext4_xattr_block_find *bs = NULL;
1352 char *buffer = NULL, *b_entry_name = NULL;
1353 size_t min_offs, free;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001354 int total_ino;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001355 void *base, *start, *end;
1356 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001357 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001358
1359 down_write(&EXT4_I(inode)->xattr_sem);
1360retry:
1361 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1362 up_write(&EXT4_I(inode)->xattr_sem);
1363 return 0;
1364 }
1365
1366 header = IHDR(inode, raw_inode);
1367 entry = IFIRST(header);
1368
1369 /*
1370 * Check if enough free space is available in the inode to shift the
1371 * entries ahead by new_extra_isize.
1372 */
1373
1374 base = start = entry;
1375 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1376 min_offs = end - base;
1377 last = entry;
1378 total_ino = sizeof(struct ext4_xattr_ibody_header);
1379
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001380 error = xattr_check_inode(inode, header, end);
1381 if (error)
1382 goto cleanup;
1383
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001384 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1385 if (free >= new_extra_isize) {
1386 entry = IFIRST(header);
1387 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1388 - new_extra_isize, (void *)raw_inode +
1389 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1390 (void *)header, total_ino,
1391 inode->i_sb->s_blocksize);
1392 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1393 error = 0;
1394 goto cleanup;
1395 }
1396
1397 /*
1398 * Enough free space isn't available in the inode, check if
1399 * EA block can hold new_extra_isize bytes.
1400 */
1401 if (EXT4_I(inode)->i_file_acl) {
1402 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1403 error = -EIO;
1404 if (!bh)
1405 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001406 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001407 EXT4_ERROR_INODE(inode, "bad block %llu",
1408 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001409 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001410 goto cleanup;
1411 }
1412 base = BHDR(bh);
1413 first = BFIRST(bh);
1414 end = bh->b_data + bh->b_size;
1415 min_offs = end - base;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001416 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001417 if (free < new_extra_isize) {
1418 if (!tried_min_extra_isize && s_min_extra_isize) {
1419 tried_min_extra_isize++;
1420 new_extra_isize = s_min_extra_isize;
1421 brelse(bh);
1422 goto retry;
1423 }
1424 error = -1;
1425 goto cleanup;
1426 }
1427 } else {
1428 free = inode->i_sb->s_blocksize;
1429 }
1430
1431 while (new_extra_isize > 0) {
1432 size_t offs, size, entry_size;
1433 struct ext4_xattr_entry *small_entry = NULL;
1434 struct ext4_xattr_info i = {
1435 .value = NULL,
1436 .value_len = 0,
1437 };
1438 unsigned int total_size; /* EA entry size + value size */
1439 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1440 unsigned int min_total_size = ~0U;
1441
1442 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1443 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1444 if (!is || !bs) {
1445 error = -ENOMEM;
1446 goto cleanup;
1447 }
1448
1449 is->s.not_found = -ENODATA;
1450 bs->s.not_found = -ENODATA;
1451 is->iloc.bh = NULL;
1452 bs->bh = NULL;
1453
1454 last = IFIRST(header);
1455 /* Find the entry best suited to be pushed into EA block */
1456 entry = NULL;
1457 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1458 total_size =
1459 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1460 EXT4_XATTR_LEN(last->e_name_len);
1461 if (total_size <= free && total_size < min_total_size) {
1462 if (total_size < new_extra_isize) {
1463 small_entry = last;
1464 } else {
1465 entry = last;
1466 min_total_size = total_size;
1467 }
1468 }
1469 }
1470
1471 if (entry == NULL) {
1472 if (small_entry) {
1473 entry = small_entry;
1474 } else {
1475 if (!tried_min_extra_isize &&
1476 s_min_extra_isize) {
1477 tried_min_extra_isize++;
1478 new_extra_isize = s_min_extra_isize;
Dave Jones6e4ea8e2013-10-10 20:05:35 -04001479 kfree(is); is = NULL;
1480 kfree(bs); bs = NULL;
Theodore Ts'odcb99172013-10-31 23:00:24 -04001481 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001482 goto retry;
1483 }
1484 error = -1;
1485 goto cleanup;
1486 }
1487 }
1488 offs = le16_to_cpu(entry->e_value_offs);
1489 size = le32_to_cpu(entry->e_value_size);
1490 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1491 i.name_index = entry->e_name_index,
1492 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1493 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1494 if (!buffer || !b_entry_name) {
1495 error = -ENOMEM;
1496 goto cleanup;
1497 }
1498 /* Save the entry name and the entry value */
1499 memcpy(buffer, (void *)IFIRST(header) + offs,
1500 EXT4_XATTR_SIZE(size));
1501 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1502 b_entry_name[entry->e_name_len] = '\0';
1503 i.name = b_entry_name;
1504
1505 error = ext4_get_inode_loc(inode, &is->iloc);
1506 if (error)
1507 goto cleanup;
1508
1509 error = ext4_xattr_ibody_find(inode, &i, is);
1510 if (error)
1511 goto cleanup;
1512
1513 /* Remove the chosen entry from the inode */
1514 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001515 if (error)
1516 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001517
1518 entry = IFIRST(header);
1519 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1520 shift_bytes = new_extra_isize;
1521 else
1522 shift_bytes = entry_size + size;
1523 /* Adjust the offsets and shift the remaining entries ahead */
1524 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1525 shift_bytes, (void *)raw_inode +
1526 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1527 (void *)header, total_ino - entry_size,
1528 inode->i_sb->s_blocksize);
1529
1530 extra_isize += shift_bytes;
1531 new_extra_isize -= shift_bytes;
1532 EXT4_I(inode)->i_extra_isize = extra_isize;
1533
1534 i.name = b_entry_name;
1535 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001536 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001537 error = ext4_xattr_block_find(inode, &i, bs);
1538 if (error)
1539 goto cleanup;
1540
1541 /* Add entry which was removed from the inode into the block */
1542 error = ext4_xattr_block_set(handle, inode, &i, bs);
1543 if (error)
1544 goto cleanup;
1545 kfree(b_entry_name);
1546 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001547 b_entry_name = NULL;
1548 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001549 brelse(is->iloc.bh);
1550 kfree(is);
1551 kfree(bs);
1552 }
1553 brelse(bh);
1554 up_write(&EXT4_I(inode)->xattr_sem);
1555 return 0;
1556
1557cleanup:
1558 kfree(b_entry_name);
1559 kfree(buffer);
1560 if (is)
1561 brelse(is->iloc.bh);
1562 kfree(is);
1563 kfree(bs);
1564 brelse(bh);
1565 up_write(&EXT4_I(inode)->xattr_sem);
1566 return error;
1567}
1568
1569
1570
1571/*
Mingming Cao617ba132006-10-11 01:20:53 -07001572 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 *
1574 * Free extended attribute resources associated with this inode. This
1575 * is called immediately before an inode is freed. We have exclusive
1576 * access to the inode.
1577 */
1578void
Mingming Cao617ba132006-10-11 01:20:53 -07001579ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580{
1581 struct buffer_head *bh = NULL;
1582
Mingming Cao617ba132006-10-11 01:20:53 -07001583 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001585 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001587 EXT4_ERROR_INODE(inode, "block %llu read error",
1588 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 goto cleanup;
1590 }
Mingming Cao617ba132006-10-11 01:20:53 -07001591 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001593 EXT4_ERROR_INODE(inode, "bad block %llu",
1594 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 goto cleanup;
1596 }
Mingming Cao617ba132006-10-11 01:20:53 -07001597 ext4_xattr_release_block(handle, inode, bh);
1598 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599
1600cleanup:
1601 brelse(bh);
1602}
1603
1604/*
Mingming Cao617ba132006-10-11 01:20:53 -07001605 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 *
1607 * Create a new entry in the extended attribute cache, and insert
1608 * it unless such an entry is already in the cache.
1609 *
1610 * Returns 0, or a negative error number on failure.
1611 */
1612static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001613ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001615 struct ext4_xattr_header *header = BHDR(bh);
1616 __u32 hash = le32_to_cpu(header->h_hash);
1617 int reusable = le32_to_cpu(header->h_refcount) <
1618 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619 int error;
1620
Jan Kara7a2508e2016-02-22 22:35:22 -05001621 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001622 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001624 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001626 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628}
1629
1630/*
Mingming Cao617ba132006-10-11 01:20:53 -07001631 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 *
1633 * Compare two extended attribute blocks for equality.
1634 *
1635 * Returns 0 if the blocks are equal, 1 if they differ, and
1636 * a negative error number on errors.
1637 */
1638static int
Mingming Cao617ba132006-10-11 01:20:53 -07001639ext4_xattr_cmp(struct ext4_xattr_header *header1,
1640 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641{
Mingming Cao617ba132006-10-11 01:20:53 -07001642 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643
1644 entry1 = ENTRY(header1+1);
1645 entry2 = ENTRY(header2+1);
1646 while (!IS_LAST_ENTRY(entry1)) {
1647 if (IS_LAST_ENTRY(entry2))
1648 return 1;
1649 if (entry1->e_hash != entry2->e_hash ||
1650 entry1->e_name_index != entry2->e_name_index ||
1651 entry1->e_name_len != entry2->e_name_len ||
1652 entry1->e_value_size != entry2->e_value_size ||
1653 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1654 return 1;
1655 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001656 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1658 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1659 le32_to_cpu(entry1->e_value_size)))
1660 return 1;
1661
Mingming Cao617ba132006-10-11 01:20:53 -07001662 entry1 = EXT4_XATTR_NEXT(entry1);
1663 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 }
1665 if (!IS_LAST_ENTRY(entry2))
1666 return 1;
1667 return 0;
1668}
1669
1670/*
Mingming Cao617ba132006-10-11 01:20:53 -07001671 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001672 *
1673 * Find an identical extended attribute block.
1674 *
1675 * Returns a pointer to the block found, or NULL if such a block was
1676 * not found or an error occurred.
1677 */
1678static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001679ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001680 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681{
1682 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001683 struct mb_cache_entry *ce;
1684 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001685
1686 if (!header->h_hash)
1687 return NULL; /* never share */
1688 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001689 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690 while (ce) {
1691 struct buffer_head *bh;
1692
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693 bh = sb_bread(inode->i_sb, ce->e_block);
1694 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001695 EXT4_ERROR_INODE(inode, "block %lu read error",
1696 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001697 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 *pce = ce;
1699 return bh;
1700 }
1701 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001702 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001703 }
1704 return NULL;
1705}
1706
1707#define NAME_HASH_SHIFT 5
1708#define VALUE_HASH_SHIFT 16
1709
1710/*
Mingming Cao617ba132006-10-11 01:20:53 -07001711 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 *
1713 * Compute the hash of an extended attribute.
1714 */
Mingming Cao617ba132006-10-11 01:20:53 -07001715static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1716 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717{
1718 __u32 hash = 0;
1719 char *name = entry->e_name;
1720 int n;
1721
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001722 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 hash = (hash << NAME_HASH_SHIFT) ^
1724 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1725 *name++;
1726 }
1727
1728 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1729 __le32 *value = (__le32 *)((char *)header +
1730 le16_to_cpu(entry->e_value_offs));
1731 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001732 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733 hash = (hash << VALUE_HASH_SHIFT) ^
1734 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1735 le32_to_cpu(*value++);
1736 }
1737 }
1738 entry->e_hash = cpu_to_le32(hash);
1739}
1740
1741#undef NAME_HASH_SHIFT
1742#undef VALUE_HASH_SHIFT
1743
1744#define BLOCK_HASH_SHIFT 16
1745
1746/*
Mingming Cao617ba132006-10-11 01:20:53 -07001747 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 *
1749 * Re-compute the extended attribute hash value after an entry has changed.
1750 */
Mingming Cao617ba132006-10-11 01:20:53 -07001751static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1752 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001753{
Mingming Cao617ba132006-10-11 01:20:53 -07001754 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 __u32 hash = 0;
1756
Mingming Cao617ba132006-10-11 01:20:53 -07001757 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758 here = ENTRY(header+1);
1759 while (!IS_LAST_ENTRY(here)) {
1760 if (!here->e_hash) {
1761 /* Block is not shared if an entry's hash value == 0 */
1762 hash = 0;
1763 break;
1764 }
1765 hash = (hash << BLOCK_HASH_SHIFT) ^
1766 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1767 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001768 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 }
1770 header->h_hash = cpu_to_le32(hash);
1771}
1772
1773#undef BLOCK_HASH_SHIFT
1774
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001775#define HASH_BUCKET_BITS 10
1776
Jan Kara7a2508e2016-02-22 22:35:22 -05001777struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001778ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779{
Jan Kara7a2508e2016-02-22 22:35:22 -05001780 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781}
1782
Jan Kara7a2508e2016-02-22 22:35:22 -05001783void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001784{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001785 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001786 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001788