blob: 0b77ab944c3faaee65c6d3ca3e2726d0c9b97f5c [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
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400110#ifdef CONFIG_LOCKDEP
111void ext4_xattr_inode_set_class(struct inode *ea_inode)
112{
113 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
114}
115#endif
116
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400117static __le32 ext4_xattr_block_csum(struct inode *inode,
118 sector_t block_nr,
119 struct ext4_xattr_header *hdr)
120{
121 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400122 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400123 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400124 __u32 dummy_csum = 0;
125 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400126
Theodore Ts'od6a77102013-04-09 23:59:55 -0400127 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
128 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400129 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
130 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
131 offset += sizeof(dummy_csum);
132 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
133 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400134
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135 return cpu_to_le32(csum);
136}
137
138static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400139 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400140{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400141 struct ext4_xattr_header *hdr = BHDR(bh);
142 int ret = 1;
143
144 if (ext4_has_metadata_csum(inode->i_sb)) {
145 lock_buffer(bh);
146 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
147 bh->b_blocknr, hdr));
148 unlock_buffer(bh);
149 }
150 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400151}
152
153static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400154 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400155{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400156 if (ext4_has_metadata_csum(inode->i_sb))
157 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
158 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400159}
160
Stephen Hemminger11e27522010-05-13 17:53:18 -0700161static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700162ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700164 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165
Mingming Cao617ba132006-10-11 01:20:53 -0700166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 return handler;
169}
170
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400172ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
173 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400175 struct ext4_xattr_entry *e = entry;
176
Eric Biggersd7614cc2016-12-01 14:57:29 -0500177 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400178 while (!IS_LAST_ENTRY(e)) {
179 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400181 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400182 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400184
Eric Biggersd7614cc2016-12-01 14:57:29 -0500185 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400186 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400187 if (entry->e_value_size != 0 &&
188 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500189 u16 offs = le16_to_cpu(entry->e_value_offs);
190 u32 size = le32_to_cpu(entry->e_value_size);
191 void *value;
192
193 /*
194 * The value cannot overlap the names, and the value
195 * with padding cannot extend beyond 'end'. Check both
196 * the padded and unpadded sizes, since the size may
197 * overflow to 0 when adding padding.
198 */
199 if (offs > end - value_start)
200 return -EFSCORRUPTED;
201 value = value_start + offs;
202 if (value < (void *)e + sizeof(u32) ||
203 size > end - value ||
204 EXT4_XATTR_SIZE(size) > end - value)
205 return -EFSCORRUPTED;
206 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400207 entry = EXT4_XATTR_NEXT(entry);
208 }
209
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 return 0;
211}
212
213static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400214ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400216 int error;
217
218 if (buffer_verified(bh))
219 return 0;
220
Mingming Cao617ba132006-10-11 01:20:53 -0700221 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400223 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400224 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400225 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400226 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
227 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400228 if (!error)
229 set_buffer_verified(bh);
230 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231}
232
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400233static int
234__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
235 void *end, const char *function, unsigned int line)
236{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400237 int error = -EFSCORRUPTED;
238
Eric Biggers290ab232016-12-01 14:51:58 -0500239 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400240 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400241 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400242 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400243errout:
244 if (error)
245 __ext4_error_inode(inode, function, line, 0,
246 "corrupted in-inode xattr");
247 return error;
248}
249
250#define xattr_check_inode(inode, header, end) \
251 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
252
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253static int
Mingming Cao617ba132006-10-11 01:20:53 -0700254ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400255 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256{
Mingming Cao617ba132006-10-11 01:20:53 -0700257 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258 size_t name_len;
259 int cmp = 1;
260
261 if (name == NULL)
262 return -EINVAL;
263 name_len = strlen(name);
264 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700265 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 cmp = name_index - entry->e_name_index;
267 if (!cmp)
268 cmp = name_len - entry->e_name_len;
269 if (!cmp)
270 cmp = memcmp(name, entry->e_name, name_len);
271 if (cmp <= 0 && (sorted || cmp == 0))
272 break;
273 }
274 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 return cmp ? -ENODATA : 0;
276}
277
Andreas Dilgere50e5122017-06-21 21:10:32 -0400278/*
279 * Read the EA value from an inode.
280 */
281static int
282ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
283{
284 unsigned long block = 0;
285 struct buffer_head *bh = NULL;
286 int blocksize;
287 size_t csize, ret_size = 0;
288
289 if (*size == 0)
290 return 0;
291
292 blocksize = ea_inode->i_sb->s_blocksize;
293
294 while (ret_size < *size) {
295 csize = (*size - ret_size) > blocksize ? blocksize :
296 *size - ret_size;
297 bh = ext4_bread(NULL, ea_inode, block, 0);
298 if (IS_ERR(bh)) {
299 *size = ret_size;
300 return PTR_ERR(bh);
301 }
302 memcpy(buf, bh->b_data, csize);
303 brelse(bh);
304
305 buf += csize;
306 block += 1;
307 ret_size += csize;
308 }
309
310 *size = ret_size;
311
312 return 0;
313}
314
315struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
316{
317 struct inode *ea_inode = NULL;
318
319 ea_inode = ext4_iget(parent->i_sb, ea_ino);
320 if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
321 int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
322 ext4_error(parent->i_sb, "error while reading EA inode %lu "
323 "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
324 *err = rc != 0 ? rc : -EIO;
325 return NULL;
326 }
327
328 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
329 ea_inode->i_generation != parent->i_generation) {
330 ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
331 "to parent invalid.", ea_ino);
332 *err = -EINVAL;
333 goto error;
334 }
335
336 if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
337 ext4_error(parent->i_sb, "EA inode %lu does not have "
338 "EXT4_EA_INODE_FL flag set.\n", ea_ino);
339 *err = -EINVAL;
340 goto error;
341 }
342
343 *err = 0;
344 return ea_inode;
345
346error:
347 iput(ea_inode);
348 return NULL;
349}
350
351/*
352 * Read the value from the EA inode.
353 */
354static int
355ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
356 size_t *size)
357{
358 struct inode *ea_inode = NULL;
359 int err;
360
361 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
362 if (err)
363 return err;
364
365 err = ext4_xattr_inode_read(ea_inode, buffer, size);
366 iput(ea_inode);
367
368 return err;
369}
370
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371static int
Mingming Cao617ba132006-10-11 01:20:53 -0700372ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700373 void *buffer, size_t buffer_size)
374{
375 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700376 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377 size_t size;
378 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500379 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380
381 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
382 name_index, name, buffer, (long)buffer_size);
383
384 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700385 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700386 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400387 ea_idebug(inode, "reading block %llu",
388 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700389 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700390 if (!bh)
391 goto cleanup;
392 ea_bdebug(bh, "b_count=%d, refcount=%d",
393 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400394 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400395 EXT4_ERROR_INODE(inode, "bad block %llu",
396 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400397 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398 goto cleanup;
399 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400400 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400402 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700403 if (error)
404 goto cleanup;
405 size = le32_to_cpu(entry->e_value_size);
406 if (buffer) {
407 error = -ERANGE;
408 if (size > buffer_size)
409 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400410 if (entry->e_value_inum) {
411 error = ext4_xattr_inode_get(inode,
412 le32_to_cpu(entry->e_value_inum),
413 buffer, &size);
414 if (error)
415 goto cleanup;
416 } else {
417 memcpy(buffer, bh->b_data +
418 le16_to_cpu(entry->e_value_offs), size);
419 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420 }
421 error = size;
422
423cleanup:
424 brelse(bh);
425 return error;
426}
427
Tao Ma879b3822012-12-05 10:28:46 -0500428int
Mingming Cao617ba132006-10-11 01:20:53 -0700429ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430 void *buffer, size_t buffer_size)
431{
Mingming Cao617ba132006-10-11 01:20:53 -0700432 struct ext4_xattr_ibody_header *header;
433 struct ext4_xattr_entry *entry;
434 struct ext4_inode *raw_inode;
435 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 size_t size;
437 void *end;
438 int error;
439
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500440 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700442 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 if (error)
444 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700445 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700447 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400448 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449 if (error)
450 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400451 entry = IFIRST(header);
452 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 if (error)
454 goto cleanup;
455 size = le32_to_cpu(entry->e_value_size);
456 if (buffer) {
457 error = -ERANGE;
458 if (size > buffer_size)
459 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400460 if (entry->e_value_inum) {
461 error = ext4_xattr_inode_get(inode,
462 le32_to_cpu(entry->e_value_inum),
463 buffer, &size);
464 if (error)
465 goto cleanup;
466 } else {
467 memcpy(buffer, (void *)IFIRST(header) +
468 le16_to_cpu(entry->e_value_offs), size);
469 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 }
471 error = size;
472
473cleanup:
474 brelse(iloc.bh);
475 return error;
476}
477
478/*
Mingming Cao617ba132006-10-11 01:20:53 -0700479 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 *
481 * Copy an extended attribute into the buffer
482 * provided, or compute the buffer size required.
483 * Buffer is NULL to compute the size of the buffer required.
484 *
485 * Returns a negative error number on failure, or the number of bytes
486 * used / required on success.
487 */
488int
Mingming Cao617ba132006-10-11 01:20:53 -0700489ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 void *buffer, size_t buffer_size)
491{
492 int error;
493
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500494 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
495 return -EIO;
496
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400497 if (strlen(name) > 255)
498 return -ERANGE;
499
Mingming Cao617ba132006-10-11 01:20:53 -0700500 down_read(&EXT4_I(inode)->xattr_sem);
501 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502 buffer_size);
503 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700504 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700506 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 return error;
508}
509
510static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000511ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 char *buffer, size_t buffer_size)
513{
514 size_t rest = buffer_size;
515
Mingming Cao617ba132006-10-11 01:20:53 -0700516 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700517 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700518 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100520 if (handler && (!handler->list || handler->list(dentry))) {
521 const char *prefix = handler->prefix ?: handler->name;
522 size_t prefix_len = strlen(prefix);
523 size_t size = prefix_len + entry->e_name_len + 1;
524
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 if (buffer) {
526 if (size > rest)
527 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100528 memcpy(buffer, prefix, prefix_len);
529 buffer += prefix_len;
530 memcpy(buffer, entry->e_name, entry->e_name_len);
531 buffer += entry->e_name_len;
532 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700533 }
534 rest -= size;
535 }
536 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100537 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538}
539
540static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000541ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542{
David Howells2b0143b2015-03-17 22:25:59 +0000543 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 struct buffer_head *bh = NULL;
545 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500546 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547
548 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
549 buffer, (long)buffer_size);
550
551 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700552 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400554 ea_idebug(inode, "reading block %llu",
555 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700556 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 error = -EIO;
558 if (!bh)
559 goto cleanup;
560 ea_bdebug(bh, "b_count=%d, refcount=%d",
561 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400562 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400563 EXT4_ERROR_INODE(inode, "bad block %llu",
564 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400565 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566 goto cleanup;
567 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400568 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000569 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570
571cleanup:
572 brelse(bh);
573
574 return error;
575}
576
577static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000578ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579{
David Howells2b0143b2015-03-17 22:25:59 +0000580 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700581 struct ext4_xattr_ibody_header *header;
582 struct ext4_inode *raw_inode;
583 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584 void *end;
585 int error;
586
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500587 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700589 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590 if (error)
591 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700592 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700594 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400595 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 if (error)
597 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000598 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 buffer, buffer_size);
600
601cleanup:
602 brelse(iloc.bh);
603 return error;
604}
605
606/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400607 * Inode operation listxattr()
608 *
609 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 *
611 * Copy a list of attribute names into the buffer
612 * provided, or compute the buffer size required.
613 * Buffer is NULL to compute the size of the buffer required.
614 *
615 * Returns a negative error number on failure, or the number of bytes
616 * used / required on success.
617 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400618ssize_t
619ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500621 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622
David Howells2b0143b2015-03-17 22:25:59 +0000623 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500624 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
625 if (ret < 0)
626 goto errout;
627 if (buffer) {
628 buffer += ret;
629 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500631 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
632 if (ret < 0)
633 goto errout;
634 ret += ret2;
635errout:
David Howells2b0143b2015-03-17 22:25:59 +0000636 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500637 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638}
639
640/*
Mingming Cao617ba132006-10-11 01:20:53 -0700641 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700642 * not set, set it.
643 */
Mingming Cao617ba132006-10-11 01:20:53 -0700644static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645 struct super_block *sb)
646{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400647 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700648 return;
649
liang xie5d601252014-05-12 22:06:43 -0400650 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700651 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400652 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400653 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655}
656
657/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400658 * Release the xattr block BH: If the reference count is > 1, decrement it;
659 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 */
661static void
Mingming Cao617ba132006-10-11 01:20:53 -0700662ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 struct buffer_head *bh)
664{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500665 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
666 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800667 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668
liang xie5d601252014-05-12 22:06:43 -0400669 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800670 error = ext4_journal_get_write_access(handle, bh);
671 if (error)
672 goto out;
673
674 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500675 hash = le32_to_cpu(BHDR(bh)->h_hash);
676 ref = le32_to_cpu(BHDR(bh)->h_refcount);
677 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500679 /*
680 * This must happen under buffer lock for
681 * ext4_xattr_block_set() to reliably detect freed block
682 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500683 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400685 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500686 ext4_free_blocks(handle, inode, bh, 0, 1,
687 EXT4_FREE_BLOCKS_METADATA |
688 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500690 ref--;
691 BHDR(bh)->h_refcount = cpu_to_le32(ref);
692 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
693 struct mb_cache_entry *ce;
694
695 ce = mb_cache_entry_get(ext4_mb_cache, hash,
696 bh->b_blocknr);
697 if (ce) {
698 ce->e_reusable = 1;
699 mb_cache_entry_put(ext4_mb_cache, ce);
700 }
701 }
702
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400703 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400704 /*
705 * Beware of this ugliness: Releasing of xattr block references
706 * from different inodes can race and so we have to protect
707 * from a race where someone else frees the block (and releases
708 * its journal_head) before we are done dirtying the buffer. In
709 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400710 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400711 * that function can call sync_dirty_buffer() so for that case
712 * we handle the dirtying after unlocking the buffer.
713 */
714 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400715 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500716 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400717 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400718 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800719 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500720 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500721 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800722 ea_bdebug(bh, "refcount now=%d; releasing",
723 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800725out:
726 ext4_std_error(inode->i_sb, error);
727 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728}
729
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400730/*
731 * Find the available free space for EAs. This also returns the total number of
732 * bytes used by EA entries.
733 */
734static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
735 size_t *min_offs, void *base, int *total)
736{
737 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400738 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400739 size_t offs = le16_to_cpu(last->e_value_offs);
740 if (offs < *min_offs)
741 *min_offs = offs;
742 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500743 if (total)
744 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400745 }
746 return (*min_offs - ((void *)last - base) - sizeof(__u32));
747}
748
Andreas Dilgere50e5122017-06-21 21:10:32 -0400749/*
750 * Write the value of the EA in an inode.
751 */
752static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
753 const void *buf, int bufsize)
754{
755 struct buffer_head *bh = NULL;
756 unsigned long block = 0;
757 unsigned blocksize = ea_inode->i_sb->s_blocksize;
758 unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
759 int csize, wsize = 0;
760 int ret = 0;
761 int retries = 0;
762
763retry:
764 while (ret >= 0 && ret < max_blocks) {
765 struct ext4_map_blocks map;
766 map.m_lblk = block += ret;
767 map.m_len = max_blocks -= ret;
768
769 ret = ext4_map_blocks(handle, ea_inode, &map,
770 EXT4_GET_BLOCKS_CREATE);
771 if (ret <= 0) {
772 ext4_mark_inode_dirty(handle, ea_inode);
773 if (ret == -ENOSPC &&
774 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
775 ret = 0;
776 goto retry;
777 }
778 break;
779 }
780 }
781
782 if (ret < 0)
783 return ret;
784
785 block = 0;
786 while (wsize < bufsize) {
787 if (bh != NULL)
788 brelse(bh);
789 csize = (bufsize - wsize) > blocksize ? blocksize :
790 bufsize - wsize;
791 bh = ext4_getblk(handle, ea_inode, block, 0);
792 if (IS_ERR(bh))
793 return PTR_ERR(bh);
794 ret = ext4_journal_get_write_access(handle, bh);
795 if (ret)
796 goto out;
797
798 memcpy(bh->b_data, buf, csize);
799 set_buffer_uptodate(bh);
800 ext4_handle_dirty_metadata(handle, ea_inode, bh);
801
802 buf += csize;
803 wsize += csize;
804 block += 1;
805 }
806
807 inode_lock(ea_inode);
808 i_size_write(ea_inode, wsize);
809 ext4_update_i_disksize(ea_inode, wsize);
810 inode_unlock(ea_inode);
811
812 ext4_mark_inode_dirty(handle, ea_inode);
813
814out:
815 brelse(bh);
816
817 return ret;
818}
819
820/*
821 * Create an inode to store the value of a large EA.
822 */
823static struct inode *ext4_xattr_inode_create(handle_t *handle,
824 struct inode *inode)
825{
826 struct inode *ea_inode = NULL;
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400827 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400828
829 /*
830 * Let the next inode be the goal, so we try and allocate the EA inode
831 * in the same group, or nearby one.
832 */
833 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -0400834 S_IFREG | 0600, NULL, inode->i_ino + 1, NULL,
835 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400836 if (!IS_ERR(ea_inode)) {
837 ea_inode->i_op = &ext4_file_inode_operations;
838 ea_inode->i_fop = &ext4_file_operations;
839 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400840 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400841 ea_inode->i_generation = inode->i_generation;
842 EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
843
844 /*
845 * A back-pointer from EA inode to parent inode will be useful
846 * for e2fsck.
847 */
848 EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
849 unlock_new_inode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400850 err = ext4_inode_attach_jinode(ea_inode);
851 if (err) {
852 iput(ea_inode);
853 return ERR_PTR(err);
854 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400855 }
856
857 return ea_inode;
858}
859
860/*
861 * Unlink the inode storing the value of the EA.
862 */
863int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
864{
865 struct inode *ea_inode = NULL;
866 int err;
867
868 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
869 if (err)
870 return err;
871
872 clear_nlink(ea_inode);
873 iput(ea_inode);
874
875 return 0;
876}
877
878/*
879 * Add value of the EA in an inode.
880 */
881static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
882 unsigned long *ea_ino, const void *value,
883 size_t value_len)
884{
885 struct inode *ea_inode;
886 int err;
887
888 /* Create an inode for the EA value */
889 ea_inode = ext4_xattr_inode_create(handle, inode);
890 if (IS_ERR(ea_inode))
891 return PTR_ERR(ea_inode);
892
893 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
894 if (err)
895 clear_nlink(ea_inode);
896 else
897 *ea_ino = ea_inode->i_ino;
898
899 iput(ea_inode);
900
901 return err;
902}
903
904static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
905 struct ext4_xattr_search *s,
906 handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907{
Mingming Cao617ba132006-10-11 01:20:53 -0700908 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400910 int in_inode = i->in_inode;
911 int rc;
912
913 if (ext4_has_feature_ea_inode(inode->i_sb) &&
914 (EXT4_XATTR_SIZE(i->value_len) >
915 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
916 in_inode = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917
918 /* Compute min_offs and last. */
919 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700920 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400921 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 size_t offs = le16_to_cpu(last->e_value_offs);
923 if (offs < min_offs)
924 min_offs = offs;
925 }
926 }
927 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
928 if (!s->not_found) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400929 if (!in_inode &&
930 !s->here->e_value_inum && s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700932 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 }
Mingming Cao617ba132006-10-11 01:20:53 -0700934 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 }
936 if (i->value) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400937 size_t value_len = EXT4_XATTR_SIZE(i->value_len);
938
939 if (in_inode)
940 value_len = 0;
941
942 if (free < EXT4_XATTR_LEN(name_len) + value_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 return -ENOSPC;
944 }
945
946 if (i->value && s->not_found) {
947 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700948 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
950 memmove((void *)s->here + size, s->here, rest);
951 memset(s->here, 0, size);
952 s->here->e_name_index = i->name_index;
953 s->here->e_name_len = name_len;
954 memcpy(s->here->e_name, i->name, name_len);
955 } else {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400956 if (!s->here->e_value_inum && s->here->e_value_size &&
957 s->here->e_value_offs > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 void *first_val = s->base + min_offs;
959 size_t offs = le16_to_cpu(s->here->e_value_offs);
960 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700961 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 le32_to_cpu(s->here->e_value_size));
963
Mingming Cao617ba132006-10-11 01:20:53 -0700964 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 /* The old and the new value have the same
966 size. Just replace. */
967 s->here->e_value_size =
968 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500969 if (i->value == EXT4_ZERO_XATTR_VALUE) {
970 memset(val, 0, size);
971 } else {
972 /* Clear pad bytes first. */
973 memset(val + size - EXT4_XATTR_PAD, 0,
974 EXT4_XATTR_PAD);
975 memcpy(val, i->value, i->value_len);
976 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 return 0;
978 }
979
980 /* Remove the old value. */
981 memmove(first_val + size, first_val, val - first_val);
982 memset(first_val, 0, size);
983 s->here->e_value_size = 0;
984 s->here->e_value_offs = 0;
985 min_offs += size;
986
987 /* Adjust all value offsets. */
988 last = s->first;
989 while (!IS_LAST_ENTRY(last)) {
990 size_t o = le16_to_cpu(last->e_value_offs);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400991 if (!last->e_value_inum &&
992 last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 last->e_value_offs =
994 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700995 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 }
997 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400998 if (s->here->e_value_inum) {
999 ext4_xattr_inode_unlink(inode,
1000 le32_to_cpu(s->here->e_value_inum));
1001 s->here->e_value_inum = 0;
1002 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 if (!i->value) {
1004 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001005 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 last = ENTRY((void *)last - size);
1007 memmove(s->here, (void *)s->here + size,
1008 (void *)last - (void *)s->here + sizeof(__u32));
1009 memset(last, 0, size);
1010 }
1011 }
1012
1013 if (i->value) {
1014 /* Insert the new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001015 if (in_inode) {
1016 unsigned long ea_ino =
1017 le32_to_cpu(s->here->e_value_inum);
1018 rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
1019 i->value, i->value_len);
1020 if (rc)
1021 goto out;
1022 s->here->e_value_inum = cpu_to_le32(ea_ino);
1023 s->here->e_value_offs = 0;
1024 } else if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001025 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 void *val = s->base + min_offs - size;
1027 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001028 s->here->e_value_inum = 0;
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001029 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1030 memset(val, 0, size);
1031 } else {
1032 /* Clear the pad bytes first. */
1033 memset(val + size - EXT4_XATTR_PAD, 0,
1034 EXT4_XATTR_PAD);
1035 memcpy(val, i->value, i->value_len);
1036 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001038 s->here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001040
1041out:
1042 return rc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043}
1044
Mingming Cao617ba132006-10-11 01:20:53 -07001045struct ext4_xattr_block_find {
1046 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 struct buffer_head *bh;
1048};
1049
1050static int
Mingming Cao617ba132006-10-11 01:20:53 -07001051ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1052 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053{
1054 struct super_block *sb = inode->i_sb;
1055 int error;
1056
1057 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1058 i->name_index, i->name, i->value, (long)i->value_len);
1059
Mingming Cao617ba132006-10-11 01:20:53 -07001060 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001062 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 error = -EIO;
1064 if (!bs->bh)
1065 goto cleanup;
1066 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1067 atomic_read(&(bs->bh->b_count)),
1068 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001069 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001070 EXT4_ERROR_INODE(inode, "bad block %llu",
1071 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001072 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 goto cleanup;
1074 }
1075 /* Find the named attribute. */
1076 bs->s.base = BHDR(bs->bh);
1077 bs->s.first = BFIRST(bs->bh);
1078 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1079 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001080 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001081 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082 if (error && error != -ENODATA)
1083 goto cleanup;
1084 bs->s.not_found = error;
1085 }
1086 error = 0;
1087
1088cleanup:
1089 return error;
1090}
1091
1092static int
Mingming Cao617ba132006-10-11 01:20:53 -07001093ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1094 struct ext4_xattr_info *i,
1095 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096{
1097 struct super_block *sb = inode->i_sb;
1098 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001099 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -05001100 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001101 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -05001102 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001103
Mingming Cao617ba132006-10-11 01:20:53 -07001104#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001107 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001108 error = ext4_journal_get_write_access(handle, bs->bh);
1109 if (error)
1110 goto cleanup;
1111 lock_buffer(bs->bh);
1112
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001114 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1115
1116 /*
1117 * This must happen under buffer lock for
1118 * ext4_xattr_block_set() to reliably detect modified
1119 * block
1120 */
Jan Kara7a2508e2016-02-22 22:35:22 -05001121 mb_cache_entry_delete_block(ext4_mb_cache, hash,
1122 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 ea_bdebug(bs->bh, "modifying in-place");
Andreas Dilgere50e5122017-06-21 21:10:32 -04001124 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 if (!error) {
1126 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001127 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001129 ext4_xattr_cache_insert(ext4_mb_cache,
1130 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 }
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001132 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001134 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135 goto bad_block;
1136 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001137 error = ext4_handle_dirty_metadata(handle,
1138 inode,
1139 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 if (error)
1141 goto cleanup;
1142 goto inserted;
1143 } else {
1144 int offset = (char *)s->here - bs->bh->b_data;
1145
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001146 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001147 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001148 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 error = -ENOMEM;
1150 if (s->base == NULL)
1151 goto cleanup;
1152 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1153 s->first = ENTRY(header(s->base)+1);
1154 header(s->base)->h_refcount = cpu_to_le32(1);
1155 s->here = ENTRY(s->base + offset);
1156 s->end = s->base + bs->bh->b_size;
1157 }
1158 } else {
1159 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001160 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 /* assert(header == s->base) */
1162 error = -ENOMEM;
1163 if (s->base == NULL)
1164 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001165 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 header(s->base)->h_blocks = cpu_to_le32(1);
1167 header(s->base)->h_refcount = cpu_to_le32(1);
1168 s->first = ENTRY(header(s->base)+1);
1169 s->here = ENTRY(header(s->base)+1);
1170 s->end = s->base + sb->s_blocksize;
1171 }
1172
Andreas Dilgere50e5122017-06-21 21:10:32 -04001173 error = ext4_xattr_set_entry(i, s, handle, inode);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001174 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 goto bad_block;
1176 if (error)
1177 goto cleanup;
1178 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001179 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180
1181inserted:
1182 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001183 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001184 if (new_bh) {
1185 /* We found an identical block in the cache. */
1186 if (new_bh == bs->bh)
1187 ea_bdebug(new_bh, "keeping");
1188 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001189 u32 ref;
1190
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001191 WARN_ON_ONCE(dquot_initialize_needed(inode));
1192
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193 /* The old block is released after updating
1194 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001195 error = dquot_alloc_block(inode,
1196 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001197 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001199 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001200 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 new_bh);
1202 if (error)
1203 goto cleanup_dquot;
1204 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001205 /*
1206 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001207 * freeing, rehashing or adding references to
1208 * xattr block. Once we hold buffer lock xattr
1209 * block's state is stable so we can check
1210 * whether the block got freed / rehashed or
1211 * not. Since we unhash mbcache entry under
1212 * buffer lock when freeing / rehashing xattr
1213 * block, checking whether entry is still
1214 * hashed is reliable. Same rules hold for
1215 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001216 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001217 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1218 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001219 /*
1220 * Undo everything and check mbcache
1221 * again.
1222 */
1223 unlock_buffer(new_bh);
1224 dquot_free_block(inode,
1225 EXT4_C2B(EXT4_SB(sb),
1226 1));
1227 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001228 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001229 ce = NULL;
1230 new_bh = NULL;
1231 goto inserted;
1232 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001233 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1234 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1235 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1236 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001238 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001239 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001240 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001241 error = ext4_handle_dirty_metadata(handle,
1242 inode,
1243 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001244 if (error)
1245 goto cleanup_dquot;
1246 }
Jan Kara7a2508e2016-02-22 22:35:22 -05001247 mb_cache_entry_touch(ext4_mb_cache, ce);
1248 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249 ce = NULL;
1250 } else if (bs->bh && s->base == bs->bh->b_data) {
1251 /* We were modifying this block in-place. */
1252 ea_bdebug(bs->bh, "keeping this block");
1253 new_bh = bs->bh;
1254 get_bh(new_bh);
1255 } else {
1256 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001257 ext4_fsblk_t goal, block;
1258
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001259 WARN_ON_ONCE(dquot_initialize_needed(inode));
1260
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001261 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001262 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001263
1264 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001265 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001266 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1267
Allison Henderson55f020d2011-05-25 07:41:26 -04001268 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1269 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 if (error)
1271 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001272
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001273 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001274 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1275
Joe Perchesace36ad2012-03-19 23:11:43 -04001276 ea_idebug(inode, "creating block %llu",
1277 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278
1279 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001280 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001281 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001283 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001284 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285 goto cleanup;
1286 }
1287 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001288 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001289 if (error) {
1290 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001291 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 goto getblk_failed;
1293 }
1294 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001295 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 set_buffer_uptodate(new_bh);
1297 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001298 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001299 error = ext4_handle_dirty_metadata(handle, inode,
1300 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301 if (error)
1302 goto cleanup;
1303 }
1304 }
1305
1306 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001307 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308
1309 /* Drop the previous xattr block. */
1310 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001311 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001312 error = 0;
1313
1314cleanup:
1315 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001316 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001317 brelse(new_bh);
1318 if (!(bs->bh && s->base == bs->bh->b_data))
1319 kfree(s->base);
1320
1321 return error;
1322
1323cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001324 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001325 goto cleanup;
1326
1327bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001328 EXT4_ERROR_INODE(inode, "bad block %llu",
1329 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 goto cleanup;
1331
1332#undef header
1333}
1334
Tao Ma879b3822012-12-05 10:28:46 -05001335int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1336 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337{
Mingming Cao617ba132006-10-11 01:20:53 -07001338 struct ext4_xattr_ibody_header *header;
1339 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001340 int error;
1341
Mingming Cao617ba132006-10-11 01:20:53 -07001342 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001343 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001344 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345 header = IHDR(inode, raw_inode);
1346 is->s.base = is->s.first = IFIRST(header);
1347 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001348 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001349 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001350 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 if (error)
1352 return error;
1353 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001354 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001355 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001356 if (error && error != -ENODATA)
1357 return error;
1358 is->s.not_found = error;
1359 }
1360 return 0;
1361}
1362
Tao Ma0d812f72012-12-10 14:06:02 -05001363int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1364 struct ext4_xattr_info *i,
1365 struct ext4_xattr_ibody_find *is)
1366{
1367 struct ext4_xattr_ibody_header *header;
1368 struct ext4_xattr_search *s = &is->s;
1369 int error;
1370
1371 if (EXT4_I(inode)->i_extra_isize == 0)
1372 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001373 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001374 if (error) {
1375 if (error == -ENOSPC &&
1376 ext4_has_inline_data(inode)) {
1377 error = ext4_try_to_evict_inline_data(handle, inode,
1378 EXT4_XATTR_LEN(strlen(i->name) +
1379 EXT4_XATTR_SIZE(i->value_len)));
1380 if (error)
1381 return error;
1382 error = ext4_xattr_ibody_find(inode, i, is);
1383 if (error)
1384 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001385 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001386 }
1387 if (error)
1388 return error;
1389 }
1390 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1391 if (!IS_LAST_ENTRY(s->first)) {
1392 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1393 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1394 } else {
1395 header->h_magic = cpu_to_le32(0);
1396 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1397 }
1398 return 0;
1399}
1400
Andreas Dilgere50e5122017-06-21 21:10:32 -04001401static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001402 struct ext4_xattr_info *i,
1403 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001404{
Mingming Cao617ba132006-10-11 01:20:53 -07001405 struct ext4_xattr_ibody_header *header;
1406 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001407 int error;
1408
Mingming Cao617ba132006-10-11 01:20:53 -07001409 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001410 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001411 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001412 if (error)
1413 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001414 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001416 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001417 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 } else {
1419 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001420 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 }
1422 return 0;
1423}
1424
Jan Kara3fd16462016-02-22 22:43:04 -05001425static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1426 struct ext4_xattr_info *i)
1427{
1428 void *value;
1429
1430 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1431 return 0;
1432 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1433 return !memcmp(value, i->value, i->value_len);
1434}
1435
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436/*
Mingming Cao617ba132006-10-11 01:20:53 -07001437 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001439 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001440 * is NULL to remove an existing extended attribute, and non-NULL to
1441 * either replace an existing extended attribute, or create a new extended
1442 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1443 * specify that an extended attribute must exist and must not exist
1444 * previous to the call, respectively.
1445 *
1446 * Returns 0, or a negative error number on failure.
1447 */
1448int
Mingming Cao617ba132006-10-11 01:20:53 -07001449ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450 const char *name, const void *value, size_t value_len,
1451 int flags)
1452{
Mingming Cao617ba132006-10-11 01:20:53 -07001453 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 .name_index = name_index,
1455 .name = name,
1456 .value = value,
1457 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04001458 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 };
Mingming Cao617ba132006-10-11 01:20:53 -07001460 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 .s = { .not_found = -ENODATA, },
1462 };
Mingming Cao617ba132006-10-11 01:20:53 -07001463 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464 .s = { .not_found = -ENODATA, },
1465 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001466 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467 int error;
1468
1469 if (!name)
1470 return -EINVAL;
1471 if (strlen(name) > 255)
1472 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001473
Theodore Ts'oc755e252017-01-11 21:50:46 -05001474 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001475
Eric Sandeen66543612011-10-26 03:32:07 -04001476 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001477 if (error)
1478 goto cleanup;
1479
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001480 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001481 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1482 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001483 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484 }
1485
Mingming Cao617ba132006-10-11 01:20:53 -07001486 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487 if (error)
1488 goto cleanup;
1489 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001490 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001491 if (error)
1492 goto cleanup;
1493 if (is.s.not_found && bs.s.not_found) {
1494 error = -ENODATA;
1495 if (flags & XATTR_REPLACE)
1496 goto cleanup;
1497 error = 0;
1498 if (!value)
1499 goto cleanup;
1500 } else {
1501 error = -EEXIST;
1502 if (flags & XATTR_CREATE)
1503 goto cleanup;
1504 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 if (!value) {
1506 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001507 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001509 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001511 error = 0;
1512 /* Xattr value did not change? Save us some work and bail out */
1513 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1514 goto cleanup;
1515 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1516 goto cleanup;
1517
Andreas Dilgere50e5122017-06-21 21:10:32 -04001518 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001519 if (!error && !bs.s.not_found) {
1520 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001521 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001523 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1524 error = ext4_xattr_block_find(inode, &i, &bs);
1525 if (error)
1526 goto cleanup;
1527 }
Mingming Cao617ba132006-10-11 01:20:53 -07001528 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001529 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1530 error == -ENOSPC) {
1531 /* xattr not fit to block, store at external
1532 * inode */
1533 i.in_inode = 1;
1534 error = ext4_xattr_ibody_set(handle, inode,
1535 &i, &is);
1536 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 if (error)
1538 goto cleanup;
1539 if (!is.s.not_found) {
1540 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001541 error = ext4_xattr_ibody_set(handle, inode, &i,
1542 &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543 }
1544 }
1545 }
1546 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001547 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001548 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001549 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001550 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001551 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001553 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554 * error != 0.
1555 */
1556 is.iloc.bh = NULL;
1557 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001558 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001559 }
1560
1561cleanup:
1562 brelse(is.iloc.bh);
1563 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001564 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001565 return error;
1566}
1567
1568/*
Mingming Cao617ba132006-10-11 01:20:53 -07001569 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001570 *
Mingming Cao617ba132006-10-11 01:20:53 -07001571 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572 * attribute modification is a filesystem transaction by itself.
1573 *
1574 * Returns 0, or a negative error number on failure.
1575 */
1576int
Mingming Cao617ba132006-10-11 01:20:53 -07001577ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 const void *value, size_t value_len, int flags)
1579{
1580 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001581 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001583 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001585 error = dquot_initialize(inode);
1586 if (error)
1587 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001588
1589 if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
1590 ext4_has_feature_ea_inode(sb)) {
1591 int nrblocks = (value_len + sb->s_blocksize - 1) >>
1592 sb->s_blocksize_bits;
1593
1594 /* For new inode */
1595 credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
1596
1597 /* For data blocks of EA inode */
1598 credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
1599 }
1600
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001602 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 if (IS_ERR(handle)) {
1604 error = PTR_ERR(handle);
1605 } else {
1606 int error2;
1607
Mingming Cao617ba132006-10-11 01:20:53 -07001608 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001610 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04001612 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 goto retry;
1614 if (error == 0)
1615 error = error2;
1616 }
1617
1618 return error;
1619}
1620
1621/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001622 * Shift the EA entries in the inode to create space for the increased
1623 * i_extra_isize.
1624 */
1625static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1626 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001627 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001628{
1629 struct ext4_xattr_entry *last = entry;
1630 int new_offs;
1631
Jan Kara94405712016-08-29 15:41:11 -04001632 /* We always shift xattr headers further thus offsets get lower */
1633 BUG_ON(value_offs_shift > 0);
1634
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001635 /* Adjust the value offsets of the entries */
1636 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001637 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001638 new_offs = le16_to_cpu(last->e_value_offs) +
1639 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001640 last->e_value_offs = cpu_to_le16(new_offs);
1641 }
1642 }
1643 /* Shift the entries by n bytes */
1644 memmove(to, from, n);
1645}
1646
1647/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001648 * Move xattr pointed to by 'entry' from inode into external xattr block
1649 */
1650static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1651 struct ext4_inode *raw_inode,
1652 struct ext4_xattr_entry *entry)
1653{
1654 struct ext4_xattr_ibody_find *is = NULL;
1655 struct ext4_xattr_block_find *bs = NULL;
1656 char *buffer = NULL, *b_entry_name = NULL;
1657 size_t value_offs, value_size;
1658 struct ext4_xattr_info i = {
1659 .value = NULL,
1660 .value_len = 0,
1661 .name_index = entry->e_name_index,
1662 };
1663 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1664 int error;
1665
1666 value_offs = le16_to_cpu(entry->e_value_offs);
1667 value_size = le32_to_cpu(entry->e_value_size);
1668
1669 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1670 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1671 buffer = kmalloc(value_size, GFP_NOFS);
1672 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1673 if (!is || !bs || !buffer || !b_entry_name) {
1674 error = -ENOMEM;
1675 goto out;
1676 }
1677
1678 is->s.not_found = -ENODATA;
1679 bs->s.not_found = -ENODATA;
1680 is->iloc.bh = NULL;
1681 bs->bh = NULL;
1682
1683 /* Save the entry name and the entry value */
1684 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1685 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1686 b_entry_name[entry->e_name_len] = '\0';
1687 i.name = b_entry_name;
1688
1689 error = ext4_get_inode_loc(inode, &is->iloc);
1690 if (error)
1691 goto out;
1692
1693 error = ext4_xattr_ibody_find(inode, &i, is);
1694 if (error)
1695 goto out;
1696
1697 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001698 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001699 if (error)
1700 goto out;
1701
1702 i.name = b_entry_name;
1703 i.value = buffer;
1704 i.value_len = value_size;
1705 error = ext4_xattr_block_find(inode, &i, bs);
1706 if (error)
1707 goto out;
1708
1709 /* Add entry which was removed from the inode into the block */
1710 error = ext4_xattr_block_set(handle, inode, &i, bs);
1711 if (error)
1712 goto out;
1713 error = 0;
1714out:
1715 kfree(b_entry_name);
1716 kfree(buffer);
1717 if (is)
1718 brelse(is->iloc.bh);
1719 kfree(is);
1720 kfree(bs);
1721
1722 return error;
1723}
1724
Jan Karadfa20642016-08-29 15:44:11 -04001725static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1726 struct ext4_inode *raw_inode,
1727 int isize_diff, size_t ifree,
1728 size_t bfree, int *total_ino)
1729{
1730 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1731 struct ext4_xattr_entry *small_entry;
1732 struct ext4_xattr_entry *entry;
1733 struct ext4_xattr_entry *last;
1734 unsigned int entry_size; /* EA entry size */
1735 unsigned int total_size; /* EA entry size + value size */
1736 unsigned int min_total_size;
1737 int error;
1738
1739 while (isize_diff > ifree) {
1740 entry = NULL;
1741 small_entry = NULL;
1742 min_total_size = ~0U;
1743 last = IFIRST(header);
1744 /* Find the entry best suited to be pushed into EA block */
1745 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1746 total_size =
1747 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1748 EXT4_XATTR_LEN(last->e_name_len);
1749 if (total_size <= bfree &&
1750 total_size < min_total_size) {
1751 if (total_size + ifree < isize_diff) {
1752 small_entry = last;
1753 } else {
1754 entry = last;
1755 min_total_size = total_size;
1756 }
1757 }
1758 }
1759
1760 if (entry == NULL) {
1761 if (small_entry == NULL)
1762 return -ENOSPC;
1763 entry = small_entry;
1764 }
1765
1766 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1767 total_size = entry_size +
1768 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1769 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1770 entry);
1771 if (error)
1772 return error;
1773
1774 *total_ino -= entry_size;
1775 ifree += total_size;
1776 bfree -= total_size;
1777 }
1778
1779 return 0;
1780}
1781
Jan Kara3f2571c2016-08-29 15:42:11 -04001782/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001783 * Expand an inode by new_extra_isize bytes when EAs are present.
1784 * Returns 0 on success or negative error number on failure.
1785 */
1786int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1787 struct ext4_inode *raw_inode, handle_t *handle)
1788{
1789 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001790 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001791 size_t min_offs;
1792 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001793 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001794 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001795 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001796 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 -04001797 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001798 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001799
Theodore Ts'oc755e252017-01-11 21:50:46 -05001800 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1801 return 0;
1802
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001803retry:
Jan Karad0141192016-08-11 11:50:30 -04001804 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001805 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1806 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001807
1808 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001809
1810 /*
1811 * Check if enough free space is available in the inode to shift the
1812 * entries ahead by new_extra_isize.
1813 */
1814
Jan Kara6e0cd082016-08-29 15:43:11 -04001815 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001816 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1817 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001818 total_ino = sizeof(struct ext4_xattr_ibody_header);
1819
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001820 error = xattr_check_inode(inode, header, end);
1821 if (error)
1822 goto cleanup;
1823
Jan Kara6e0cd082016-08-29 15:43:11 -04001824 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001825 if (ifree >= isize_diff)
1826 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001827
1828 /*
1829 * Enough free space isn't available in the inode, check if
1830 * EA block can hold new_extra_isize bytes.
1831 */
1832 if (EXT4_I(inode)->i_file_acl) {
1833 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1834 error = -EIO;
1835 if (!bh)
1836 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001837 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001838 EXT4_ERROR_INODE(inode, "bad block %llu",
1839 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001840 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001841 goto cleanup;
1842 }
1843 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001844 end = bh->b_data + bh->b_size;
1845 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001846 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1847 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001848 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001849 if (!tried_min_extra_isize && s_min_extra_isize) {
1850 tried_min_extra_isize++;
1851 new_extra_isize = s_min_extra_isize;
1852 brelse(bh);
1853 goto retry;
1854 }
Jan Karadfa20642016-08-29 15:44:11 -04001855 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001856 goto cleanup;
1857 }
1858 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001859 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001860 }
1861
Jan Karadfa20642016-08-29 15:44:11 -04001862 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1863 isize_diff, ifree, bfree,
1864 &total_ino);
1865 if (error) {
1866 if (error == -ENOSPC && !tried_min_extra_isize &&
1867 s_min_extra_isize) {
1868 tried_min_extra_isize++;
1869 new_extra_isize = s_min_extra_isize;
1870 brelse(bh);
1871 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001872 }
Jan Karadfa20642016-08-29 15:44:11 -04001873 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001874 }
Jan Karae3014d12016-08-29 15:38:11 -04001875shift:
1876 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001877 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001878 - new_extra_isize, (void *)raw_inode +
1879 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001880 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001881 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001882 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001883out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001884 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001885 return 0;
1886
1887cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001888 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001889 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001890 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001891 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001892 no_expand = 1;
1893 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001894 return error;
1895}
1896
1897
Andreas Dilgere50e5122017-06-21 21:10:32 -04001898#define EIA_INCR 16 /* must be 2^n */
1899#define EIA_MASK (EIA_INCR - 1)
1900/* Add the large xattr @ino into @lea_ino_array for later deletion.
1901 * If @lea_ino_array is new or full it will be grown and the old
1902 * contents copied over.
1903 */
1904static int
1905ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
1906{
1907 if (*lea_ino_array == NULL) {
1908 /*
1909 * Start with 15 inodes, so it fits into a power-of-two size.
1910 * If *lea_ino_array is NULL, this is essentially offsetof()
1911 */
1912 (*lea_ino_array) =
1913 kmalloc(offsetof(struct ext4_xattr_ino_array,
1914 xia_inodes[EIA_MASK]),
1915 GFP_NOFS);
1916 if (*lea_ino_array == NULL)
1917 return -ENOMEM;
1918 (*lea_ino_array)->xia_count = 0;
1919 } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
1920 /* expand the array once all 15 + n * 16 slots are full */
1921 struct ext4_xattr_ino_array *new_array = NULL;
1922 int count = (*lea_ino_array)->xia_count;
1923
1924 /* if new_array is NULL, this is essentially offsetof() */
1925 new_array = kmalloc(
1926 offsetof(struct ext4_xattr_ino_array,
1927 xia_inodes[count + EIA_INCR]),
1928 GFP_NOFS);
1929 if (new_array == NULL)
1930 return -ENOMEM;
1931 memcpy(new_array, *lea_ino_array,
1932 offsetof(struct ext4_xattr_ino_array,
1933 xia_inodes[count]));
1934 kfree(*lea_ino_array);
1935 *lea_ino_array = new_array;
1936 }
1937 (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
1938 return 0;
1939}
1940
1941/**
1942 * Add xattr inode to orphan list
1943 */
1944static int
1945ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
1946 int credits, struct ext4_xattr_ino_array *lea_ino_array)
1947{
1948 struct inode *ea_inode = NULL;
1949 int idx = 0, error = 0;
1950
1951 if (lea_ino_array == NULL)
1952 return 0;
1953
1954 for (; idx < lea_ino_array->xia_count; ++idx) {
1955 if (!ext4_handle_has_enough_credits(handle, credits)) {
1956 error = ext4_journal_extend(handle, credits);
1957 if (error > 0)
1958 error = ext4_journal_restart(handle, credits);
1959
1960 if (error != 0) {
1961 ext4_warning(inode->i_sb,
1962 "couldn't extend journal "
1963 "(err %d)", error);
1964 return error;
1965 }
1966 }
1967 ea_inode = ext4_xattr_inode_iget(inode,
1968 lea_ino_array->xia_inodes[idx], &error);
1969 if (error)
1970 continue;
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001971 inode_lock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001972 ext4_orphan_add(handle, ea_inode);
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001973 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001974 /* the inode's i_count will be released by caller */
1975 }
1976
1977 return 0;
1978}
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001979
1980/*
Mingming Cao617ba132006-10-11 01:20:53 -07001981 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001982 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04001983 * Free extended attribute resources associated with this inode. Traverse
1984 * all entries and unlink any xattr inodes associated with this inode. This
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001985 * is called immediately before an inode is freed. We have exclusive
Andreas Dilgere50e5122017-06-21 21:10:32 -04001986 * access to the inode. If an orphan inode is deleted it will also delete any
1987 * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
1988 * to ensure they belong to the parent inode and were not deleted already.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001990int
1991ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1992 struct ext4_xattr_ino_array **lea_ino_array)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993{
1994 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001995 struct ext4_xattr_ibody_header *header;
1996 struct ext4_inode *raw_inode;
1997 struct ext4_iloc iloc;
1998 struct ext4_xattr_entry *entry;
1999 int credits = 3, error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000
Andreas Dilgere50e5122017-06-21 21:10:32 -04002001 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
2002 goto delete_external_ea;
2003
2004 error = ext4_get_inode_loc(inode, &iloc);
2005 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002007 raw_inode = ext4_raw_inode(&iloc);
2008 header = IHDR(inode, raw_inode);
2009 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
2010 entry = EXT4_XATTR_NEXT(entry)) {
2011 if (!entry->e_value_inum)
2012 continue;
2013 if (ext4_expand_ino_array(lea_ino_array,
2014 entry->e_value_inum) != 0) {
2015 brelse(iloc.bh);
2016 goto cleanup;
2017 }
2018 entry->e_value_inum = 0;
2019 }
2020 brelse(iloc.bh);
2021
2022delete_external_ea:
2023 if (!EXT4_I(inode)->i_file_acl) {
2024 /* add xattr inode to orphan list */
2025 ext4_xattr_inode_orphan_add(handle, inode, credits,
2026 *lea_ino_array);
2027 goto cleanup;
2028 }
Mingming Cao617ba132006-10-11 01:20:53 -07002029 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002031 EXT4_ERROR_INODE(inode, "block %llu read error",
2032 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002033 goto cleanup;
2034 }
Mingming Cao617ba132006-10-11 01:20:53 -07002035 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002036 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002037 EXT4_ERROR_INODE(inode, "bad block %llu",
2038 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002039 goto cleanup;
2040 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04002041
2042 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2043 entry = EXT4_XATTR_NEXT(entry)) {
2044 if (!entry->e_value_inum)
2045 continue;
2046 if (ext4_expand_ino_array(lea_ino_array,
2047 entry->e_value_inum) != 0)
2048 goto cleanup;
2049 entry->e_value_inum = 0;
2050 }
2051
2052 /* add xattr inode to orphan list */
2053 error = ext4_xattr_inode_orphan_add(handle, inode, credits,
2054 *lea_ino_array);
2055 if (error != 0)
2056 goto cleanup;
2057
2058 if (!IS_NOQUOTA(inode))
2059 credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
2060
2061 if (!ext4_handle_has_enough_credits(handle, credits)) {
2062 error = ext4_journal_extend(handle, credits);
2063 if (error > 0)
2064 error = ext4_journal_restart(handle, credits);
2065 if (error != 0) {
2066 ext4_warning(inode->i_sb,
2067 "couldn't extend journal (err %d)", error);
2068 goto cleanup;
2069 }
2070 }
2071
Mingming Cao617ba132006-10-11 01:20:53 -07002072 ext4_xattr_release_block(handle, inode, bh);
2073 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002074
2075cleanup:
2076 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002077
2078 return error;
2079}
2080
2081void
2082ext4_xattr_inode_array_free(struct inode *inode,
2083 struct ext4_xattr_ino_array *lea_ino_array)
2084{
2085 struct inode *ea_inode = NULL;
2086 int idx = 0;
2087 int err;
2088
2089 if (lea_ino_array == NULL)
2090 return;
2091
2092 for (; idx < lea_ino_array->xia_count; ++idx) {
2093 ea_inode = ext4_xattr_inode_iget(inode,
2094 lea_ino_array->xia_inodes[idx], &err);
2095 if (err)
2096 continue;
2097 /* for inode's i_count get from ext4_xattr_delete_inode */
2098 if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
2099 iput(ea_inode);
2100 clear_nlink(ea_inode);
2101 iput(ea_inode);
2102 }
2103 kfree(lea_ino_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104}
2105
2106/*
Mingming Cao617ba132006-10-11 01:20:53 -07002107 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 *
2109 * Create a new entry in the extended attribute cache, and insert
2110 * it unless such an entry is already in the cache.
2111 *
2112 * Returns 0, or a negative error number on failure.
2113 */
2114static void
Jan Kara7a2508e2016-02-22 22:35:22 -05002115ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002116{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002117 struct ext4_xattr_header *header = BHDR(bh);
2118 __u32 hash = le32_to_cpu(header->h_hash);
2119 int reusable = le32_to_cpu(header->h_refcount) <
2120 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002121 int error;
2122
Jan Kara7a2508e2016-02-22 22:35:22 -05002123 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002124 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002126 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002128 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130}
2131
2132/*
Mingming Cao617ba132006-10-11 01:20:53 -07002133 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 *
2135 * Compare two extended attribute blocks for equality.
2136 *
2137 * Returns 0 if the blocks are equal, 1 if they differ, and
2138 * a negative error number on errors.
2139 */
2140static int
Mingming Cao617ba132006-10-11 01:20:53 -07002141ext4_xattr_cmp(struct ext4_xattr_header *header1,
2142 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002143{
Mingming Cao617ba132006-10-11 01:20:53 -07002144 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002145
2146 entry1 = ENTRY(header1+1);
2147 entry2 = ENTRY(header2+1);
2148 while (!IS_LAST_ENTRY(entry1)) {
2149 if (IS_LAST_ENTRY(entry2))
2150 return 1;
2151 if (entry1->e_hash != entry2->e_hash ||
2152 entry1->e_name_index != entry2->e_name_index ||
2153 entry1->e_name_len != entry2->e_name_len ||
2154 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002155 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002156 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2157 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2159 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2160 le32_to_cpu(entry1->e_value_size)))
2161 return 1;
2162
Mingming Cao617ba132006-10-11 01:20:53 -07002163 entry1 = EXT4_XATTR_NEXT(entry1);
2164 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165 }
2166 if (!IS_LAST_ENTRY(entry2))
2167 return 1;
2168 return 0;
2169}
2170
2171/*
Mingming Cao617ba132006-10-11 01:20:53 -07002172 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002173 *
2174 * Find an identical extended attribute block.
2175 *
2176 * Returns a pointer to the block found, or NULL if such a block was
2177 * not found or an error occurred.
2178 */
2179static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07002180ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05002181 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002182{
2183 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002184 struct mb_cache_entry *ce;
2185 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186
2187 if (!header->h_hash)
2188 return NULL; /* never share */
2189 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002190 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191 while (ce) {
2192 struct buffer_head *bh;
2193
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002194 bh = sb_bread(inode->i_sb, ce->e_block);
2195 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002196 EXT4_ERROR_INODE(inode, "block %lu read error",
2197 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07002198 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199 *pce = ce;
2200 return bh;
2201 }
2202 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05002203 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002204 }
2205 return NULL;
2206}
2207
2208#define NAME_HASH_SHIFT 5
2209#define VALUE_HASH_SHIFT 16
2210
2211/*
Mingming Cao617ba132006-10-11 01:20:53 -07002212 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002213 *
2214 * Compute the hash of an extended attribute.
2215 */
Mingming Cao617ba132006-10-11 01:20:53 -07002216static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2217 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218{
2219 __u32 hash = 0;
2220 char *name = entry->e_name;
2221 int n;
2222
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002223 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 hash = (hash << NAME_HASH_SHIFT) ^
2225 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2226 *name++;
2227 }
2228
Andreas Dilgere50e5122017-06-21 21:10:32 -04002229 if (!entry->e_value_inum && entry->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 __le32 *value = (__le32 *)((char *)header +
2231 le16_to_cpu(entry->e_value_offs));
2232 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07002233 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234 hash = (hash << VALUE_HASH_SHIFT) ^
2235 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2236 le32_to_cpu(*value++);
2237 }
2238 }
2239 entry->e_hash = cpu_to_le32(hash);
2240}
2241
2242#undef NAME_HASH_SHIFT
2243#undef VALUE_HASH_SHIFT
2244
2245#define BLOCK_HASH_SHIFT 16
2246
2247/*
Mingming Cao617ba132006-10-11 01:20:53 -07002248 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249 *
2250 * Re-compute the extended attribute hash value after an entry has changed.
2251 */
Mingming Cao617ba132006-10-11 01:20:53 -07002252static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2253 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002254{
Mingming Cao617ba132006-10-11 01:20:53 -07002255 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256 __u32 hash = 0;
2257
Mingming Cao617ba132006-10-11 01:20:53 -07002258 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002259 here = ENTRY(header+1);
2260 while (!IS_LAST_ENTRY(here)) {
2261 if (!here->e_hash) {
2262 /* Block is not shared if an entry's hash value == 0 */
2263 hash = 0;
2264 break;
2265 }
2266 hash = (hash << BLOCK_HASH_SHIFT) ^
2267 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2268 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07002269 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002270 }
2271 header->h_hash = cpu_to_le32(hash);
2272}
2273
2274#undef BLOCK_HASH_SHIFT
2275
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002276#define HASH_BUCKET_BITS 10
2277
Jan Kara7a2508e2016-02-22 22:35:22 -05002278struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05002279ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280{
Jan Kara7a2508e2016-02-22 22:35:22 -05002281 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002282}
2283
Jan Kara7a2508e2016-02-22 22:35:22 -05002284void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002286 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05002287 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002288}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002289