blob: c5a3e98bd74d54db332f71c651d06c0cb3286dd7 [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 *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081
Eric Biggersd6006182017-04-29 23:47:50 -040082static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070083 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040084#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080085 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
86 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087#endif
Mingming Cao617ba132006-10-11 01:20:53 -070088 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040089#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070090 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091#endif
92};
93
Stephen Hemminger11e27522010-05-13 17:53:18 -070094const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070095 &ext4_xattr_user_handler,
96 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040097#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080098 &posix_acl_access_xattr_handler,
99 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400101#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700102 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103#endif
104 NULL
105};
106
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400107#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
108 inode->i_sb->s_fs_info)->s_mb_cache)
109
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400110static __le32 ext4_xattr_block_csum(struct inode *inode,
111 sector_t block_nr,
112 struct ext4_xattr_header *hdr)
113{
114 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400115 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400116 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400117 __u32 dummy_csum = 0;
118 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400119
Theodore Ts'od6a77102013-04-09 23:59:55 -0400120 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
121 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400122 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
123 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
124 offset += sizeof(dummy_csum);
125 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
126 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400127
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400128 return cpu_to_le32(csum);
129}
130
131static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400132 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400133{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400134 struct ext4_xattr_header *hdr = BHDR(bh);
135 int ret = 1;
136
137 if (ext4_has_metadata_csum(inode->i_sb)) {
138 lock_buffer(bh);
139 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
140 bh->b_blocknr, hdr));
141 unlock_buffer(bh);
142 }
143 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400144}
145
146static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400147 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400148{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400149 if (ext4_has_metadata_csum(inode->i_sb))
150 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
151 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400152}
153
Stephen Hemminger11e27522010-05-13 17:53:18 -0700154static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700155ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700156{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700157 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158
Mingming Cao617ba132006-10-11 01:20:53 -0700159 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
160 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 return handler;
162}
163
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400165ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
166 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400168 struct ext4_xattr_entry *e = entry;
169
Eric Biggersd7614cc2016-12-01 14:57:29 -0500170 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400171 while (!IS_LAST_ENTRY(e)) {
172 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400174 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400175 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400177
Eric Biggersd7614cc2016-12-01 14:57:29 -0500178 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400179 while (!IS_LAST_ENTRY(entry)) {
Jan Kara2de58f12016-08-29 15:39:11 -0400180 if (entry->e_value_block != 0)
181 return -EFSCORRUPTED;
Eric Biggersd7614cc2016-12-01 14:57:29 -0500182 if (entry->e_value_size != 0) {
183 u16 offs = le16_to_cpu(entry->e_value_offs);
184 u32 size = le32_to_cpu(entry->e_value_size);
185 void *value;
186
187 /*
188 * The value cannot overlap the names, and the value
189 * with padding cannot extend beyond 'end'. Check both
190 * the padded and unpadded sizes, since the size may
191 * overflow to 0 when adding padding.
192 */
193 if (offs > end - value_start)
194 return -EFSCORRUPTED;
195 value = value_start + offs;
196 if (value < (void *)e + sizeof(u32) ||
197 size > end - value ||
198 EXT4_XATTR_SIZE(size) > end - value)
199 return -EFSCORRUPTED;
200 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400201 entry = EXT4_XATTR_NEXT(entry);
202 }
203
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204 return 0;
205}
206
207static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400208ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400210 int error;
211
212 if (buffer_verified(bh))
213 return 0;
214
Mingming Cao617ba132006-10-11 01:20:53 -0700215 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400217 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400218 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400219 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400220 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
221 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400222 if (!error)
223 set_buffer_verified(bh);
224 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225}
226
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400227static int
228__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
229 void *end, const char *function, unsigned int line)
230{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400231 int error = -EFSCORRUPTED;
232
Eric Biggers290ab232016-12-01 14:51:58 -0500233 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400234 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400235 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400236 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400237errout:
238 if (error)
239 __ext4_error_inode(inode, function, line, 0,
240 "corrupted in-inode xattr");
241 return error;
242}
243
244#define xattr_check_inode(inode, header, end) \
245 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
246
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700248ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249{
250 size_t value_size = le32_to_cpu(entry->e_value_size);
251
252 if (entry->e_value_block != 0 || value_size > size ||
253 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400254 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255 return 0;
256}
257
258static int
Mingming Cao617ba132006-10-11 01:20:53 -0700259ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 const char *name, size_t size, int sorted)
261{
Mingming Cao617ba132006-10-11 01:20:53 -0700262 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 size_t name_len;
264 int cmp = 1;
265
266 if (name == NULL)
267 return -EINVAL;
268 name_len = strlen(name);
269 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700270 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700271 cmp = name_index - entry->e_name_index;
272 if (!cmp)
273 cmp = name_len - entry->e_name_len;
274 if (!cmp)
275 cmp = memcmp(name, entry->e_name, name_len);
276 if (cmp <= 0 && (sorted || cmp == 0))
277 break;
278 }
279 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700280 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400281 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 return cmp ? -ENODATA : 0;
283}
284
285static int
Mingming Cao617ba132006-10-11 01:20:53 -0700286ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 void *buffer, size_t buffer_size)
288{
289 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700290 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 size_t size;
292 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500293 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294
295 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
296 name_index, name, buffer, (long)buffer_size);
297
298 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700299 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400301 ea_idebug(inode, "reading block %llu",
302 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700303 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 if (!bh)
305 goto cleanup;
306 ea_bdebug(bh, "b_count=%d, refcount=%d",
307 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400308 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500309bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400310 EXT4_ERROR_INODE(inode, "bad block %llu",
311 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400312 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700313 goto cleanup;
314 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400315 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700317 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400318 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319 goto bad_block;
320 if (error)
321 goto cleanup;
322 size = le32_to_cpu(entry->e_value_size);
323 if (buffer) {
324 error = -ERANGE;
325 if (size > buffer_size)
326 goto cleanup;
327 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
328 size);
329 }
330 error = size;
331
332cleanup:
333 brelse(bh);
334 return error;
335}
336
Tao Ma879b3822012-12-05 10:28:46 -0500337int
Mingming Cao617ba132006-10-11 01:20:53 -0700338ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700339 void *buffer, size_t buffer_size)
340{
Mingming Cao617ba132006-10-11 01:20:53 -0700341 struct ext4_xattr_ibody_header *header;
342 struct ext4_xattr_entry *entry;
343 struct ext4_inode *raw_inode;
344 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700345 size_t size;
346 void *end;
347 int error;
348
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500349 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700351 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 if (error)
353 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700354 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 header = IHDR(inode, raw_inode);
356 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700357 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400358 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359 if (error)
360 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700361 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 end - (void *)entry, 0);
363 if (error)
364 goto cleanup;
365 size = le32_to_cpu(entry->e_value_size);
366 if (buffer) {
367 error = -ERANGE;
368 if (size > buffer_size)
369 goto cleanup;
370 memcpy(buffer, (void *)IFIRST(header) +
371 le16_to_cpu(entry->e_value_offs), size);
372 }
373 error = size;
374
375cleanup:
376 brelse(iloc.bh);
377 return error;
378}
379
380/*
Mingming Cao617ba132006-10-11 01:20:53 -0700381 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382 *
383 * Copy an extended attribute into the buffer
384 * provided, or compute the buffer size required.
385 * Buffer is NULL to compute the size of the buffer required.
386 *
387 * Returns a negative error number on failure, or the number of bytes
388 * used / required on success.
389 */
390int
Mingming Cao617ba132006-10-11 01:20:53 -0700391ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 void *buffer, size_t buffer_size)
393{
394 int error;
395
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500396 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
397 return -EIO;
398
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400399 if (strlen(name) > 255)
400 return -ERANGE;
401
Mingming Cao617ba132006-10-11 01:20:53 -0700402 down_read(&EXT4_I(inode)->xattr_sem);
403 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404 buffer_size);
405 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700406 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700408 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 return error;
410}
411
412static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000413ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 char *buffer, size_t buffer_size)
415{
416 size_t rest = buffer_size;
417
Mingming Cao617ba132006-10-11 01:20:53 -0700418 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700419 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700420 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100422 if (handler && (!handler->list || handler->list(dentry))) {
423 const char *prefix = handler->prefix ?: handler->name;
424 size_t prefix_len = strlen(prefix);
425 size_t size = prefix_len + entry->e_name_len + 1;
426
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427 if (buffer) {
428 if (size > rest)
429 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100430 memcpy(buffer, prefix, prefix_len);
431 buffer += prefix_len;
432 memcpy(buffer, entry->e_name, entry->e_name_len);
433 buffer += entry->e_name_len;
434 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 }
436 rest -= size;
437 }
438 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100439 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440}
441
442static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000443ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444{
David Howells2b0143b2015-03-17 22:25:59 +0000445 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 struct buffer_head *bh = NULL;
447 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500448 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449
450 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
451 buffer, (long)buffer_size);
452
453 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700454 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400456 ea_idebug(inode, "reading block %llu",
457 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700458 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459 error = -EIO;
460 if (!bh)
461 goto cleanup;
462 ea_bdebug(bh, "b_count=%d, refcount=%d",
463 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400464 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400465 EXT4_ERROR_INODE(inode, "bad block %llu",
466 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400467 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 goto cleanup;
469 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400470 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000471 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700472
473cleanup:
474 brelse(bh);
475
476 return error;
477}
478
479static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000480ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481{
David Howells2b0143b2015-03-17 22:25:59 +0000482 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700483 struct ext4_xattr_ibody_header *header;
484 struct ext4_inode *raw_inode;
485 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 void *end;
487 int error;
488
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500489 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700491 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 if (error)
493 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700494 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700496 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400497 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 if (error)
499 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000500 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501 buffer, buffer_size);
502
503cleanup:
504 brelse(iloc.bh);
505 return error;
506}
507
508/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400509 * Inode operation listxattr()
510 *
511 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 *
513 * Copy a list of attribute names into the buffer
514 * provided, or compute the buffer size required.
515 * Buffer is NULL to compute the size of the buffer required.
516 *
517 * Returns a negative error number on failure, or the number of bytes
518 * used / required on success.
519 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400520ssize_t
521ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500523 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524
David Howells2b0143b2015-03-17 22:25:59 +0000525 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500526 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
527 if (ret < 0)
528 goto errout;
529 if (buffer) {
530 buffer += ret;
531 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500533 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
534 if (ret < 0)
535 goto errout;
536 ret += ret2;
537errout:
David Howells2b0143b2015-03-17 22:25:59 +0000538 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500539 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540}
541
542/*
Mingming Cao617ba132006-10-11 01:20:53 -0700543 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 * not set, set it.
545 */
Mingming Cao617ba132006-10-11 01:20:53 -0700546static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 struct super_block *sb)
548{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400549 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 return;
551
liang xie5d601252014-05-12 22:06:43 -0400552 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700553 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400554 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400555 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557}
558
559/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400560 * Release the xattr block BH: If the reference count is > 1, decrement it;
561 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 */
563static void
Mingming Cao617ba132006-10-11 01:20:53 -0700564ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 struct buffer_head *bh)
566{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500567 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
568 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800569 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570
liang xie5d601252014-05-12 22:06:43 -0400571 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800572 error = ext4_journal_get_write_access(handle, bh);
573 if (error)
574 goto out;
575
576 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500577 hash = le32_to_cpu(BHDR(bh)->h_hash);
578 ref = le32_to_cpu(BHDR(bh)->h_refcount);
579 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500581 /*
582 * This must happen under buffer lock for
583 * ext4_xattr_block_set() to reliably detect freed block
584 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500585 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400587 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500588 ext4_free_blocks(handle, inode, bh, 0, 1,
589 EXT4_FREE_BLOCKS_METADATA |
590 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500592 ref--;
593 BHDR(bh)->h_refcount = cpu_to_le32(ref);
594 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
595 struct mb_cache_entry *ce;
596
597 ce = mb_cache_entry_get(ext4_mb_cache, hash,
598 bh->b_blocknr);
599 if (ce) {
600 ce->e_reusable = 1;
601 mb_cache_entry_put(ext4_mb_cache, ce);
602 }
603 }
604
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400605 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400606 /*
607 * Beware of this ugliness: Releasing of xattr block references
608 * from different inodes can race and so we have to protect
609 * from a race where someone else frees the block (and releases
610 * its journal_head) before we are done dirtying the buffer. In
611 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400612 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400613 * that function can call sync_dirty_buffer() so for that case
614 * we handle the dirtying after unlocking the buffer.
615 */
616 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400617 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500618 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400619 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400620 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800621 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500622 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500623 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800624 ea_bdebug(bh, "refcount now=%d; releasing",
625 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800627out:
628 ext4_std_error(inode->i_sb, error);
629 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630}
631
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400632/*
633 * Find the available free space for EAs. This also returns the total number of
634 * bytes used by EA entries.
635 */
636static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
637 size_t *min_offs, void *base, int *total)
638{
639 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400640 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400641 size_t offs = le16_to_cpu(last->e_value_offs);
642 if (offs < *min_offs)
643 *min_offs = offs;
644 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500645 if (total)
646 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400647 }
648 return (*min_offs - ((void *)last - base) - sizeof(__u32));
649}
650
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651static int
Mingming Cao617ba132006-10-11 01:20:53 -0700652ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653{
Mingming Cao617ba132006-10-11 01:20:53 -0700654 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
656
657 /* Compute min_offs and last. */
658 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700659 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400660 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 size_t offs = le16_to_cpu(last->e_value_offs);
662 if (offs < min_offs)
663 min_offs = offs;
664 }
665 }
666 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
667 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400668 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700670 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 }
Mingming Cao617ba132006-10-11 01:20:53 -0700672 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 }
674 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400675 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700676 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 return -ENOSPC;
678 }
679
680 if (i->value && s->not_found) {
681 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700682 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
684 memmove((void *)s->here + size, s->here, rest);
685 memset(s->here, 0, size);
686 s->here->e_name_index = i->name_index;
687 s->here->e_name_len = name_len;
688 memcpy(s->here->e_name, i->name, name_len);
689 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400690 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 void *first_val = s->base + min_offs;
692 size_t offs = le16_to_cpu(s->here->e_value_offs);
693 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700694 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695 le32_to_cpu(s->here->e_value_size));
696
Mingming Cao617ba132006-10-11 01:20:53 -0700697 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 /* The old and the new value have the same
699 size. Just replace. */
700 s->here->e_value_size =
701 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500702 if (i->value == EXT4_ZERO_XATTR_VALUE) {
703 memset(val, 0, size);
704 } else {
705 /* Clear pad bytes first. */
706 memset(val + size - EXT4_XATTR_PAD, 0,
707 EXT4_XATTR_PAD);
708 memcpy(val, i->value, i->value_len);
709 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 return 0;
711 }
712
713 /* Remove the old value. */
714 memmove(first_val + size, first_val, val - first_val);
715 memset(first_val, 0, size);
716 s->here->e_value_size = 0;
717 s->here->e_value_offs = 0;
718 min_offs += size;
719
720 /* Adjust all value offsets. */
721 last = s->first;
722 while (!IS_LAST_ENTRY(last)) {
723 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400724 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 last->e_value_offs =
726 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700727 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 }
729 }
730 if (!i->value) {
731 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700732 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 last = ENTRY((void *)last - size);
734 memmove(s->here, (void *)s->here + size,
735 (void *)last - (void *)s->here + sizeof(__u32));
736 memset(last, 0, size);
737 }
738 }
739
740 if (i->value) {
741 /* Insert the new value. */
742 s->here->e_value_size = cpu_to_le32(i->value_len);
743 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700744 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 void *val = s->base + min_offs - size;
746 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500747 if (i->value == EXT4_ZERO_XATTR_VALUE) {
748 memset(val, 0, size);
749 } else {
750 /* Clear the pad bytes first. */
751 memset(val + size - EXT4_XATTR_PAD, 0,
752 EXT4_XATTR_PAD);
753 memcpy(val, i->value, i->value_len);
754 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755 }
756 }
757 return 0;
758}
759
Mingming Cao617ba132006-10-11 01:20:53 -0700760struct ext4_xattr_block_find {
761 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 struct buffer_head *bh;
763};
764
765static int
Mingming Cao617ba132006-10-11 01:20:53 -0700766ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
767 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768{
769 struct super_block *sb = inode->i_sb;
770 int error;
771
772 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
773 i->name_index, i->name, i->value, (long)i->value_len);
774
Mingming Cao617ba132006-10-11 01:20:53 -0700775 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700777 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778 error = -EIO;
779 if (!bs->bh)
780 goto cleanup;
781 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
782 atomic_read(&(bs->bh->b_count)),
783 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400784 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400785 EXT4_ERROR_INODE(inode, "bad block %llu",
786 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400787 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 goto cleanup;
789 }
790 /* Find the named attribute. */
791 bs->s.base = BHDR(bs->bh);
792 bs->s.first = BFIRST(bs->bh);
793 bs->s.end = bs->bh->b_data + bs->bh->b_size;
794 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700795 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 i->name, bs->bh->b_size, 1);
797 if (error && error != -ENODATA)
798 goto cleanup;
799 bs->s.not_found = error;
800 }
801 error = 0;
802
803cleanup:
804 return error;
805}
806
807static int
Mingming Cao617ba132006-10-11 01:20:53 -0700808ext4_xattr_block_set(handle_t *handle, struct inode *inode,
809 struct ext4_xattr_info *i,
810 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811{
812 struct super_block *sb = inode->i_sb;
813 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700814 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500815 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800816 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500817 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818
Mingming Cao617ba132006-10-11 01:20:53 -0700819#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700820
821 if (i->value && i->value_len > sb->s_blocksize)
822 return -ENOSPC;
823 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400824 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800825 error = ext4_journal_get_write_access(handle, bs->bh);
826 if (error)
827 goto cleanup;
828 lock_buffer(bs->bh);
829
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500831 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
832
833 /*
834 * This must happen under buffer lock for
835 * ext4_xattr_block_set() to reliably detect modified
836 * block
837 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500838 mb_cache_entry_delete_block(ext4_mb_cache, hash,
839 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700840 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700841 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 if (!error) {
843 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700844 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400846 ext4_xattr_cache_insert(ext4_mb_cache,
847 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 }
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400849 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400851 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852 goto bad_block;
853 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400854 error = ext4_handle_dirty_metadata(handle,
855 inode,
856 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 if (error)
858 goto cleanup;
859 goto inserted;
860 } else {
861 int offset = (char *)s->here - bs->bh->b_data;
862
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800863 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400865 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866 error = -ENOMEM;
867 if (s->base == NULL)
868 goto cleanup;
869 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
870 s->first = ENTRY(header(s->base)+1);
871 header(s->base)->h_refcount = cpu_to_le32(1);
872 s->here = ENTRY(s->base + offset);
873 s->end = s->base + bs->bh->b_size;
874 }
875 } else {
876 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400877 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878 /* assert(header == s->base) */
879 error = -ENOMEM;
880 if (s->base == NULL)
881 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700882 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883 header(s->base)->h_blocks = cpu_to_le32(1);
884 header(s->base)->h_refcount = cpu_to_le32(1);
885 s->first = ENTRY(header(s->base)+1);
886 s->here = ENTRY(header(s->base)+1);
887 s->end = s->base + sb->s_blocksize;
888 }
889
Mingming Cao617ba132006-10-11 01:20:53 -0700890 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400891 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 goto bad_block;
893 if (error)
894 goto cleanup;
895 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700896 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897
898inserted:
899 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700900 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 if (new_bh) {
902 /* We found an identical block in the cache. */
903 if (new_bh == bs->bh)
904 ea_bdebug(new_bh, "keeping");
905 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500906 u32 ref;
907
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 /* The old block is released after updating
909 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500910 error = dquot_alloc_block(inode,
911 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500912 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400914 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700915 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916 new_bh);
917 if (error)
918 goto cleanup_dquot;
919 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500920 /*
921 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500922 * freeing, rehashing or adding references to
923 * xattr block. Once we hold buffer lock xattr
924 * block's state is stable so we can check
925 * whether the block got freed / rehashed or
926 * not. Since we unhash mbcache entry under
927 * buffer lock when freeing / rehashing xattr
928 * block, checking whether entry is still
929 * hashed is reliable. Same rules hold for
930 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500931 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500932 if (hlist_bl_unhashed(&ce->e_hash_list) ||
933 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500934 /*
935 * Undo everything and check mbcache
936 * again.
937 */
938 unlock_buffer(new_bh);
939 dquot_free_block(inode,
940 EXT4_C2B(EXT4_SB(sb),
941 1));
942 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500943 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500944 ce = NULL;
945 new_bh = NULL;
946 goto inserted;
947 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500948 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
949 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
950 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
951 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500953 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400954 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400956 error = ext4_handle_dirty_metadata(handle,
957 inode,
958 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 if (error)
960 goto cleanup_dquot;
961 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500962 mb_cache_entry_touch(ext4_mb_cache, ce);
963 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 ce = NULL;
965 } else if (bs->bh && s->base == bs->bh->b_data) {
966 /* We were modifying this block in-place. */
967 ea_bdebug(bs->bh, "keeping this block");
968 new_bh = bs->bh;
969 get_bh(new_bh);
970 } else {
971 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400972 ext4_fsblk_t goal, block;
973
974 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400975 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400976
977 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400978 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400979 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
980
Allison Henderson55f020d2011-05-25 07:41:26 -0400981 block = ext4_new_meta_blocks(handle, inode, goal, 0,
982 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 if (error)
984 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400985
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400986 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400987 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
988
Joe Perchesace36ad2012-03-19 23:11:43 -0400989 ea_idebug(inode, "creating block %llu",
990 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991
992 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500993 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500994 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500996 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500997 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 goto cleanup;
999 }
1000 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001001 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 if (error) {
1003 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001004 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 goto getblk_failed;
1006 }
1007 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001008 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 set_buffer_uptodate(new_bh);
1010 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001011 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001012 error = ext4_handle_dirty_metadata(handle, inode,
1013 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 if (error)
1015 goto cleanup;
1016 }
1017 }
1018
1019 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001020 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021
1022 /* Drop the previous xattr block. */
1023 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001024 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 error = 0;
1026
1027cleanup:
1028 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001029 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 brelse(new_bh);
1031 if (!(bs->bh && s->base == bs->bh->b_data))
1032 kfree(s->base);
1033
1034 return error;
1035
1036cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001037 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 goto cleanup;
1039
1040bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001041 EXT4_ERROR_INODE(inode, "bad block %llu",
1042 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 goto cleanup;
1044
1045#undef header
1046}
1047
Tao Ma879b3822012-12-05 10:28:46 -05001048int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1049 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050{
Mingming Cao617ba132006-10-11 01:20:53 -07001051 struct ext4_xattr_ibody_header *header;
1052 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 int error;
1054
Mingming Cao617ba132006-10-11 01:20:53 -07001055 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001057 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 header = IHDR(inode, raw_inode);
1059 is->s.base = is->s.first = IFIRST(header);
1060 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001061 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001062 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001063 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 if (error)
1065 return error;
1066 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001067 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 i->name, is->s.end -
1069 (void *)is->s.base, 0);
1070 if (error && error != -ENODATA)
1071 return error;
1072 is->s.not_found = error;
1073 }
1074 return 0;
1075}
1076
Tao Ma0d812f72012-12-10 14:06:02 -05001077int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1078 struct ext4_xattr_info *i,
1079 struct ext4_xattr_ibody_find *is)
1080{
1081 struct ext4_xattr_ibody_header *header;
1082 struct ext4_xattr_search *s = &is->s;
1083 int error;
1084
1085 if (EXT4_I(inode)->i_extra_isize == 0)
1086 return -ENOSPC;
1087 error = ext4_xattr_set_entry(i, s);
1088 if (error) {
1089 if (error == -ENOSPC &&
1090 ext4_has_inline_data(inode)) {
1091 error = ext4_try_to_evict_inline_data(handle, inode,
1092 EXT4_XATTR_LEN(strlen(i->name) +
1093 EXT4_XATTR_SIZE(i->value_len)));
1094 if (error)
1095 return error;
1096 error = ext4_xattr_ibody_find(inode, i, is);
1097 if (error)
1098 return error;
1099 error = ext4_xattr_set_entry(i, s);
1100 }
1101 if (error)
1102 return error;
1103 }
1104 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1105 if (!IS_LAST_ENTRY(s->first)) {
1106 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1107 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1108 } else {
1109 header->h_magic = cpu_to_le32(0);
1110 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1111 }
1112 return 0;
1113}
1114
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001115static int ext4_xattr_ibody_set(struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001116 struct ext4_xattr_info *i,
1117 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118{
Mingming Cao617ba132006-10-11 01:20:53 -07001119 struct ext4_xattr_ibody_header *header;
1120 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 int error;
1122
Mingming Cao617ba132006-10-11 01:20:53 -07001123 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001125 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 if (error)
1127 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001128 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001130 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001131 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 } else {
1133 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001134 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135 }
1136 return 0;
1137}
1138
Jan Kara3fd16462016-02-22 22:43:04 -05001139static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1140 struct ext4_xattr_info *i)
1141{
1142 void *value;
1143
1144 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1145 return 0;
1146 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1147 return !memcmp(value, i->value, i->value_len);
1148}
1149
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150/*
Mingming Cao617ba132006-10-11 01:20:53 -07001151 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001153 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001154 * is NULL to remove an existing extended attribute, and non-NULL to
1155 * either replace an existing extended attribute, or create a new extended
1156 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1157 * specify that an extended attribute must exist and must not exist
1158 * previous to the call, respectively.
1159 *
1160 * Returns 0, or a negative error number on failure.
1161 */
1162int
Mingming Cao617ba132006-10-11 01:20:53 -07001163ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 const char *name, const void *value, size_t value_len,
1165 int flags)
1166{
Mingming Cao617ba132006-10-11 01:20:53 -07001167 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168 .name_index = name_index,
1169 .name = name,
1170 .value = value,
1171 .value_len = value_len,
1172
1173 };
Mingming Cao617ba132006-10-11 01:20:53 -07001174 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 .s = { .not_found = -ENODATA, },
1176 };
Mingming Cao617ba132006-10-11 01:20:53 -07001177 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 .s = { .not_found = -ENODATA, },
1179 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001180 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 int error;
1182
1183 if (!name)
1184 return -EINVAL;
1185 if (strlen(name) > 255)
1186 return -ERANGE;
Theodore Ts'oc755e252017-01-11 21:50:46 -05001187 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001188
Eric Sandeen66543612011-10-26 03:32:07 -04001189 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001190 if (error)
1191 goto cleanup;
1192
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001193 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001194 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1195 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001196 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 }
1198
Mingming Cao617ba132006-10-11 01:20:53 -07001199 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200 if (error)
1201 goto cleanup;
1202 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001203 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 if (error)
1205 goto cleanup;
1206 if (is.s.not_found && bs.s.not_found) {
1207 error = -ENODATA;
1208 if (flags & XATTR_REPLACE)
1209 goto cleanup;
1210 error = 0;
1211 if (!value)
1212 goto cleanup;
1213 } else {
1214 error = -EEXIST;
1215 if (flags & XATTR_CREATE)
1216 goto cleanup;
1217 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 if (!value) {
1219 if (!is.s.not_found)
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001220 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001222 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001223 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001224 error = 0;
1225 /* Xattr value did not change? Save us some work and bail out */
1226 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1227 goto cleanup;
1228 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1229 goto cleanup;
1230
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001231 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 if (!error && !bs.s.not_found) {
1233 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001234 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001236 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1237 error = ext4_xattr_block_find(inode, &i, &bs);
1238 if (error)
1239 goto cleanup;
1240 }
Mingming Cao617ba132006-10-11 01:20:53 -07001241 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 if (error)
1243 goto cleanup;
1244 if (!is.s.not_found) {
1245 i.value = NULL;
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001246 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 }
1248 }
1249 }
1250 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001251 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001252 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001253 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001254 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001255 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001257 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 * error != 0.
1259 */
1260 is.iloc.bh = NULL;
1261 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001262 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 }
1264
1265cleanup:
1266 brelse(is.iloc.bh);
1267 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001268 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 return error;
1270}
1271
1272/*
Mingming Cao617ba132006-10-11 01:20:53 -07001273 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 *
Mingming Cao617ba132006-10-11 01:20:53 -07001275 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 * attribute modification is a filesystem transaction by itself.
1277 *
1278 * Returns 0, or a negative error number on failure.
1279 */
1280int
Mingming Cao617ba132006-10-11 01:20:53 -07001281ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 const void *value, size_t value_len, int flags)
1283{
1284 handle_t *handle;
1285 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001286 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287
1288retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001289 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290 if (IS_ERR(handle)) {
1291 error = PTR_ERR(handle);
1292 } else {
1293 int error2;
1294
Mingming Cao617ba132006-10-11 01:20:53 -07001295 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001297 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001299 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 goto retry;
1301 if (error == 0)
1302 error = error2;
1303 }
1304
1305 return error;
1306}
1307
1308/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001309 * Shift the EA entries in the inode to create space for the increased
1310 * i_extra_isize.
1311 */
1312static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1313 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001314 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001315{
1316 struct ext4_xattr_entry *last = entry;
1317 int new_offs;
1318
Jan Kara94405712016-08-29 15:41:11 -04001319 /* We always shift xattr headers further thus offsets get lower */
1320 BUG_ON(value_offs_shift > 0);
1321
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001322 /* Adjust the value offsets of the entries */
1323 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001324 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001325 new_offs = le16_to_cpu(last->e_value_offs) +
1326 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001327 last->e_value_offs = cpu_to_le16(new_offs);
1328 }
1329 }
1330 /* Shift the entries by n bytes */
1331 memmove(to, from, n);
1332}
1333
1334/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001335 * Move xattr pointed to by 'entry' from inode into external xattr block
1336 */
1337static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1338 struct ext4_inode *raw_inode,
1339 struct ext4_xattr_entry *entry)
1340{
1341 struct ext4_xattr_ibody_find *is = NULL;
1342 struct ext4_xattr_block_find *bs = NULL;
1343 char *buffer = NULL, *b_entry_name = NULL;
1344 size_t value_offs, value_size;
1345 struct ext4_xattr_info i = {
1346 .value = NULL,
1347 .value_len = 0,
1348 .name_index = entry->e_name_index,
1349 };
1350 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1351 int error;
1352
1353 value_offs = le16_to_cpu(entry->e_value_offs);
1354 value_size = le32_to_cpu(entry->e_value_size);
1355
1356 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1357 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1358 buffer = kmalloc(value_size, GFP_NOFS);
1359 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1360 if (!is || !bs || !buffer || !b_entry_name) {
1361 error = -ENOMEM;
1362 goto out;
1363 }
1364
1365 is->s.not_found = -ENODATA;
1366 bs->s.not_found = -ENODATA;
1367 is->iloc.bh = NULL;
1368 bs->bh = NULL;
1369
1370 /* Save the entry name and the entry value */
1371 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1372 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1373 b_entry_name[entry->e_name_len] = '\0';
1374 i.name = b_entry_name;
1375
1376 error = ext4_get_inode_loc(inode, &is->iloc);
1377 if (error)
1378 goto out;
1379
1380 error = ext4_xattr_ibody_find(inode, &i, is);
1381 if (error)
1382 goto out;
1383
1384 /* Remove the chosen entry from the inode */
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001385 error = ext4_xattr_ibody_set(inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001386 if (error)
1387 goto out;
1388
1389 i.name = b_entry_name;
1390 i.value = buffer;
1391 i.value_len = value_size;
1392 error = ext4_xattr_block_find(inode, &i, bs);
1393 if (error)
1394 goto out;
1395
1396 /* Add entry which was removed from the inode into the block */
1397 error = ext4_xattr_block_set(handle, inode, &i, bs);
1398 if (error)
1399 goto out;
1400 error = 0;
1401out:
1402 kfree(b_entry_name);
1403 kfree(buffer);
1404 if (is)
1405 brelse(is->iloc.bh);
1406 kfree(is);
1407 kfree(bs);
1408
1409 return error;
1410}
1411
Jan Karadfa20642016-08-29 15:44:11 -04001412static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1413 struct ext4_inode *raw_inode,
1414 int isize_diff, size_t ifree,
1415 size_t bfree, int *total_ino)
1416{
1417 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1418 struct ext4_xattr_entry *small_entry;
1419 struct ext4_xattr_entry *entry;
1420 struct ext4_xattr_entry *last;
1421 unsigned int entry_size; /* EA entry size */
1422 unsigned int total_size; /* EA entry size + value size */
1423 unsigned int min_total_size;
1424 int error;
1425
1426 while (isize_diff > ifree) {
1427 entry = NULL;
1428 small_entry = NULL;
1429 min_total_size = ~0U;
1430 last = IFIRST(header);
1431 /* Find the entry best suited to be pushed into EA block */
1432 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1433 total_size =
1434 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1435 EXT4_XATTR_LEN(last->e_name_len);
1436 if (total_size <= bfree &&
1437 total_size < min_total_size) {
1438 if (total_size + ifree < isize_diff) {
1439 small_entry = last;
1440 } else {
1441 entry = last;
1442 min_total_size = total_size;
1443 }
1444 }
1445 }
1446
1447 if (entry == NULL) {
1448 if (small_entry == NULL)
1449 return -ENOSPC;
1450 entry = small_entry;
1451 }
1452
1453 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1454 total_size = entry_size +
1455 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1456 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1457 entry);
1458 if (error)
1459 return error;
1460
1461 *total_ino -= entry_size;
1462 ifree += total_size;
1463 bfree -= total_size;
1464 }
1465
1466 return 0;
1467}
1468
Jan Kara3f2571c2016-08-29 15:42:11 -04001469/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001470 * Expand an inode by new_extra_isize bytes when EAs are present.
1471 * Returns 0 on success or negative error number on failure.
1472 */
1473int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1474 struct ext4_inode *raw_inode, handle_t *handle)
1475{
1476 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001477 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001478 size_t min_offs;
1479 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001480 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001481 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001482 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001483 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 -04001484 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001485 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001486
Theodore Ts'oc755e252017-01-11 21:50:46 -05001487 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1488 return 0;
1489
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001490retry:
Jan Karad0141192016-08-11 11:50:30 -04001491 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001492 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1493 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001494
1495 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001496
1497 /*
1498 * Check if enough free space is available in the inode to shift the
1499 * entries ahead by new_extra_isize.
1500 */
1501
Jan Kara6e0cd082016-08-29 15:43:11 -04001502 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001503 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1504 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001505 total_ino = sizeof(struct ext4_xattr_ibody_header);
1506
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001507 error = xattr_check_inode(inode, header, end);
1508 if (error)
1509 goto cleanup;
1510
Jan Kara6e0cd082016-08-29 15:43:11 -04001511 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001512 if (ifree >= isize_diff)
1513 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001514
1515 /*
1516 * Enough free space isn't available in the inode, check if
1517 * EA block can hold new_extra_isize bytes.
1518 */
1519 if (EXT4_I(inode)->i_file_acl) {
1520 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1521 error = -EIO;
1522 if (!bh)
1523 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001524 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001525 EXT4_ERROR_INODE(inode, "bad block %llu",
1526 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001527 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001528 goto cleanup;
1529 }
1530 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001531 end = bh->b_data + bh->b_size;
1532 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001533 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1534 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001535 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001536 if (!tried_min_extra_isize && s_min_extra_isize) {
1537 tried_min_extra_isize++;
1538 new_extra_isize = s_min_extra_isize;
1539 brelse(bh);
1540 goto retry;
1541 }
Jan Karadfa20642016-08-29 15:44:11 -04001542 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001543 goto cleanup;
1544 }
1545 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001546 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001547 }
1548
Jan Karadfa20642016-08-29 15:44:11 -04001549 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1550 isize_diff, ifree, bfree,
1551 &total_ino);
1552 if (error) {
1553 if (error == -ENOSPC && !tried_min_extra_isize &&
1554 s_min_extra_isize) {
1555 tried_min_extra_isize++;
1556 new_extra_isize = s_min_extra_isize;
1557 brelse(bh);
1558 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001559 }
Jan Karadfa20642016-08-29 15:44:11 -04001560 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001561 }
Jan Karae3014d12016-08-29 15:38:11 -04001562shift:
1563 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001564 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001565 - new_extra_isize, (void *)raw_inode +
1566 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001567 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001568 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001569 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001570out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001571 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001572 return 0;
1573
1574cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001575 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001576 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001577 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001578 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001579 no_expand = 1;
1580 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001581 return error;
1582}
1583
1584
1585
1586/*
Mingming Cao617ba132006-10-11 01:20:53 -07001587 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 *
1589 * Free extended attribute resources associated with this inode. This
1590 * is called immediately before an inode is freed. We have exclusive
1591 * access to the inode.
1592 */
1593void
Mingming Cao617ba132006-10-11 01:20:53 -07001594ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595{
1596 struct buffer_head *bh = NULL;
1597
Mingming Cao617ba132006-10-11 01:20:53 -07001598 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001600 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001602 EXT4_ERROR_INODE(inode, "block %llu read error",
1603 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604 goto cleanup;
1605 }
Mingming Cao617ba132006-10-11 01:20:53 -07001606 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001608 EXT4_ERROR_INODE(inode, "bad block %llu",
1609 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 goto cleanup;
1611 }
Mingming Cao617ba132006-10-11 01:20:53 -07001612 ext4_xattr_release_block(handle, inode, bh);
1613 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614
1615cleanup:
1616 brelse(bh);
1617}
1618
1619/*
Mingming Cao617ba132006-10-11 01:20:53 -07001620 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621 *
1622 * Create a new entry in the extended attribute cache, and insert
1623 * it unless such an entry is already in the cache.
1624 *
1625 * Returns 0, or a negative error number on failure.
1626 */
1627static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001628ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001630 struct ext4_xattr_header *header = BHDR(bh);
1631 __u32 hash = le32_to_cpu(header->h_hash);
1632 int reusable = le32_to_cpu(header->h_refcount) <
1633 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 int error;
1635
Jan Kara7a2508e2016-02-22 22:35:22 -05001636 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001637 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001639 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001641 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643}
1644
1645/*
Mingming Cao617ba132006-10-11 01:20:53 -07001646 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 *
1648 * Compare two extended attribute blocks for equality.
1649 *
1650 * Returns 0 if the blocks are equal, 1 if they differ, and
1651 * a negative error number on errors.
1652 */
1653static int
Mingming Cao617ba132006-10-11 01:20:53 -07001654ext4_xattr_cmp(struct ext4_xattr_header *header1,
1655 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656{
Mingming Cao617ba132006-10-11 01:20:53 -07001657 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658
1659 entry1 = ENTRY(header1+1);
1660 entry2 = ENTRY(header2+1);
1661 while (!IS_LAST_ENTRY(entry1)) {
1662 if (IS_LAST_ENTRY(entry2))
1663 return 1;
1664 if (entry1->e_hash != entry2->e_hash ||
1665 entry1->e_name_index != entry2->e_name_index ||
1666 entry1->e_name_len != entry2->e_name_len ||
1667 entry1->e_value_size != entry2->e_value_size ||
1668 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1669 return 1;
1670 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001671 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001672 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1673 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1674 le32_to_cpu(entry1->e_value_size)))
1675 return 1;
1676
Mingming Cao617ba132006-10-11 01:20:53 -07001677 entry1 = EXT4_XATTR_NEXT(entry1);
1678 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679 }
1680 if (!IS_LAST_ENTRY(entry2))
1681 return 1;
1682 return 0;
1683}
1684
1685/*
Mingming Cao617ba132006-10-11 01:20:53 -07001686 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 *
1688 * Find an identical extended attribute block.
1689 *
1690 * Returns a pointer to the block found, or NULL if such a block was
1691 * not found or an error occurred.
1692 */
1693static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001694ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001695 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696{
1697 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001698 struct mb_cache_entry *ce;
1699 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700
1701 if (!header->h_hash)
1702 return NULL; /* never share */
1703 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001704 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705 while (ce) {
1706 struct buffer_head *bh;
1707
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708 bh = sb_bread(inode->i_sb, ce->e_block);
1709 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001710 EXT4_ERROR_INODE(inode, "block %lu read error",
1711 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001712 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713 *pce = ce;
1714 return bh;
1715 }
1716 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001717 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001718 }
1719 return NULL;
1720}
1721
1722#define NAME_HASH_SHIFT 5
1723#define VALUE_HASH_SHIFT 16
1724
1725/*
Mingming Cao617ba132006-10-11 01:20:53 -07001726 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 *
1728 * Compute the hash of an extended attribute.
1729 */
Mingming Cao617ba132006-10-11 01:20:53 -07001730static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1731 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732{
1733 __u32 hash = 0;
1734 char *name = entry->e_name;
1735 int n;
1736
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001737 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001738 hash = (hash << NAME_HASH_SHIFT) ^
1739 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1740 *name++;
1741 }
1742
Jan Kara1cba4232016-08-29 15:40:11 -04001743 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744 __le32 *value = (__le32 *)((char *)header +
1745 le16_to_cpu(entry->e_value_offs));
1746 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001747 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 hash = (hash << VALUE_HASH_SHIFT) ^
1749 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1750 le32_to_cpu(*value++);
1751 }
1752 }
1753 entry->e_hash = cpu_to_le32(hash);
1754}
1755
1756#undef NAME_HASH_SHIFT
1757#undef VALUE_HASH_SHIFT
1758
1759#define BLOCK_HASH_SHIFT 16
1760
1761/*
Mingming Cao617ba132006-10-11 01:20:53 -07001762 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763 *
1764 * Re-compute the extended attribute hash value after an entry has changed.
1765 */
Mingming Cao617ba132006-10-11 01:20:53 -07001766static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1767 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768{
Mingming Cao617ba132006-10-11 01:20:53 -07001769 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770 __u32 hash = 0;
1771
Mingming Cao617ba132006-10-11 01:20:53 -07001772 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773 here = ENTRY(header+1);
1774 while (!IS_LAST_ENTRY(here)) {
1775 if (!here->e_hash) {
1776 /* Block is not shared if an entry's hash value == 0 */
1777 hash = 0;
1778 break;
1779 }
1780 hash = (hash << BLOCK_HASH_SHIFT) ^
1781 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1782 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001783 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001784 }
1785 header->h_hash = cpu_to_le32(hash);
1786}
1787
1788#undef BLOCK_HASH_SHIFT
1789
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001790#define HASH_BUCKET_BITS 10
1791
Jan Kara7a2508e2016-02-22 22:35:22 -05001792struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001793ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001794{
Jan Kara7a2508e2016-02-22 22:35:22 -05001795 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001796}
1797
Jan Kara7a2508e2016-02-22 22:35:22 -05001798void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001799{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001800 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001801 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001802}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001803