blob: a29e68293d592bf0b0383388a05589d6432607fa [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 Erdogan9e1ba002017-06-21 21:27:00 -0400827 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400828 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400829
830 /*
831 * Let the next inode be the goal, so we try and allocate the EA inode
832 * in the same group, or nearby one.
833 */
834 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -0400835 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -0400836 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400837 if (!IS_ERR(ea_inode)) {
838 ea_inode->i_op = &ext4_file_inode_operations;
839 ea_inode->i_fop = &ext4_file_operations;
840 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400841 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400842 ea_inode->i_generation = inode->i_generation;
843 EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
844
845 /*
846 * A back-pointer from EA inode to parent inode will be useful
847 * for e2fsck.
848 */
849 EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
850 unlock_new_inode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400851 err = ext4_inode_attach_jinode(ea_inode);
852 if (err) {
853 iput(ea_inode);
854 return ERR_PTR(err);
855 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400856 }
857
858 return ea_inode;
859}
860
861/*
862 * Unlink the inode storing the value of the EA.
863 */
864int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
865{
866 struct inode *ea_inode = NULL;
867 int err;
868
869 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
870 if (err)
871 return err;
872
873 clear_nlink(ea_inode);
874 iput(ea_inode);
875
876 return 0;
877}
878
879/*
880 * Add value of the EA in an inode.
881 */
882static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
883 unsigned long *ea_ino, const void *value,
884 size_t value_len)
885{
886 struct inode *ea_inode;
887 int err;
888
889 /* Create an inode for the EA value */
890 ea_inode = ext4_xattr_inode_create(handle, inode);
891 if (IS_ERR(ea_inode))
892 return PTR_ERR(ea_inode);
893
894 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
895 if (err)
896 clear_nlink(ea_inode);
897 else
898 *ea_ino = ea_inode->i_ino;
899
900 iput(ea_inode);
901
902 return err;
903}
904
905static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
906 struct ext4_xattr_search *s,
907 handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908{
Mingming Cao617ba132006-10-11 01:20:53 -0700909 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400911 int in_inode = i->in_inode;
912 int rc;
913
914 if (ext4_has_feature_ea_inode(inode->i_sb) &&
915 (EXT4_XATTR_SIZE(i->value_len) >
916 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
917 in_inode = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918
919 /* Compute min_offs and last. */
920 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700921 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400922 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700923 size_t offs = le16_to_cpu(last->e_value_offs);
924 if (offs < min_offs)
925 min_offs = offs;
926 }
927 }
928 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
929 if (!s->not_found) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400930 if (!in_inode &&
931 !s->here->e_value_inum && s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700933 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 }
Mingming Cao617ba132006-10-11 01:20:53 -0700935 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 }
937 if (i->value) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400938 size_t value_len = EXT4_XATTR_SIZE(i->value_len);
939
940 if (in_inode)
941 value_len = 0;
942
943 if (free < EXT4_XATTR_LEN(name_len) + value_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944 return -ENOSPC;
945 }
946
947 if (i->value && s->not_found) {
948 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700949 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
951 memmove((void *)s->here + size, s->here, rest);
952 memset(s->here, 0, size);
953 s->here->e_name_index = i->name_index;
954 s->here->e_name_len = name_len;
955 memcpy(s->here->e_name, i->name, name_len);
956 } else {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400957 if (!s->here->e_value_inum && s->here->e_value_size &&
958 s->here->e_value_offs > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 void *first_val = s->base + min_offs;
960 size_t offs = le16_to_cpu(s->here->e_value_offs);
961 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700962 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 le32_to_cpu(s->here->e_value_size));
964
Mingming Cao617ba132006-10-11 01:20:53 -0700965 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 /* The old and the new value have the same
967 size. Just replace. */
968 s->here->e_value_size =
969 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500970 if (i->value == EXT4_ZERO_XATTR_VALUE) {
971 memset(val, 0, size);
972 } else {
973 /* Clear pad bytes first. */
974 memset(val + size - EXT4_XATTR_PAD, 0,
975 EXT4_XATTR_PAD);
976 memcpy(val, i->value, i->value_len);
977 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 return 0;
979 }
980
981 /* Remove the old value. */
982 memmove(first_val + size, first_val, val - first_val);
983 memset(first_val, 0, size);
984 s->here->e_value_size = 0;
985 s->here->e_value_offs = 0;
986 min_offs += size;
987
988 /* Adjust all value offsets. */
989 last = s->first;
990 while (!IS_LAST_ENTRY(last)) {
991 size_t o = le16_to_cpu(last->e_value_offs);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400992 if (!last->e_value_inum &&
993 last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700994 last->e_value_offs =
995 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700996 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700997 }
998 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400999 if (s->here->e_value_inum) {
1000 ext4_xattr_inode_unlink(inode,
1001 le32_to_cpu(s->here->e_value_inum));
1002 s->here->e_value_inum = 0;
1003 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 if (!i->value) {
1005 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001006 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007 last = ENTRY((void *)last - size);
1008 memmove(s->here, (void *)s->here + size,
1009 (void *)last - (void *)s->here + sizeof(__u32));
1010 memset(last, 0, size);
1011 }
1012 }
1013
1014 if (i->value) {
1015 /* Insert the new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001016 if (in_inode) {
1017 unsigned long ea_ino =
1018 le32_to_cpu(s->here->e_value_inum);
1019 rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
1020 i->value, i->value_len);
1021 if (rc)
1022 goto out;
1023 s->here->e_value_inum = cpu_to_le32(ea_ino);
1024 s->here->e_value_offs = 0;
1025 } else if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001026 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001027 void *val = s->base + min_offs - size;
1028 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001029 s->here->e_value_inum = 0;
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001030 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1031 memset(val, 0, size);
1032 } else {
1033 /* Clear the pad bytes first. */
1034 memset(val + size - EXT4_XATTR_PAD, 0,
1035 EXT4_XATTR_PAD);
1036 memcpy(val, i->value, i->value_len);
1037 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001039 s->here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001041
1042out:
1043 return rc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001044}
1045
Mingming Cao617ba132006-10-11 01:20:53 -07001046struct ext4_xattr_block_find {
1047 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048 struct buffer_head *bh;
1049};
1050
1051static int
Mingming Cao617ba132006-10-11 01:20:53 -07001052ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1053 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054{
1055 struct super_block *sb = inode->i_sb;
1056 int error;
1057
1058 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1059 i->name_index, i->name, i->value, (long)i->value_len);
1060
Mingming Cao617ba132006-10-11 01:20:53 -07001061 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001063 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 error = -EIO;
1065 if (!bs->bh)
1066 goto cleanup;
1067 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1068 atomic_read(&(bs->bh->b_count)),
1069 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001070 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001071 EXT4_ERROR_INODE(inode, "bad block %llu",
1072 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001073 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074 goto cleanup;
1075 }
1076 /* Find the named attribute. */
1077 bs->s.base = BHDR(bs->bh);
1078 bs->s.first = BFIRST(bs->bh);
1079 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1080 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001081 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001082 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001083 if (error && error != -ENODATA)
1084 goto cleanup;
1085 bs->s.not_found = error;
1086 }
1087 error = 0;
1088
1089cleanup:
1090 return error;
1091}
1092
1093static int
Mingming Cao617ba132006-10-11 01:20:53 -07001094ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1095 struct ext4_xattr_info *i,
1096 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001097{
1098 struct super_block *sb = inode->i_sb;
1099 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001100 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -05001101 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001102 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -05001103 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104
Mingming Cao617ba132006-10-11 01:20:53 -07001105#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001107 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001108 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001109 error = ext4_journal_get_write_access(handle, bs->bh);
1110 if (error)
1111 goto cleanup;
1112 lock_buffer(bs->bh);
1113
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001115 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1116
1117 /*
1118 * This must happen under buffer lock for
1119 * ext4_xattr_block_set() to reliably detect modified
1120 * block
1121 */
Jan Kara7a2508e2016-02-22 22:35:22 -05001122 mb_cache_entry_delete_block(ext4_mb_cache, hash,
1123 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 ea_bdebug(bs->bh, "modifying in-place");
Andreas Dilgere50e5122017-06-21 21:10:32 -04001125 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 if (!error) {
1127 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001128 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001130 ext4_xattr_cache_insert(ext4_mb_cache,
1131 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 }
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001133 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001135 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 goto bad_block;
1137 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001138 error = ext4_handle_dirty_metadata(handle,
1139 inode,
1140 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 if (error)
1142 goto cleanup;
1143 goto inserted;
1144 } else {
1145 int offset = (char *)s->here - bs->bh->b_data;
1146
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001147 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001149 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 error = -ENOMEM;
1151 if (s->base == NULL)
1152 goto cleanup;
1153 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1154 s->first = ENTRY(header(s->base)+1);
1155 header(s->base)->h_refcount = cpu_to_le32(1);
1156 s->here = ENTRY(s->base + offset);
1157 s->end = s->base + bs->bh->b_size;
1158 }
1159 } else {
1160 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001161 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 /* assert(header == s->base) */
1163 error = -ENOMEM;
1164 if (s->base == NULL)
1165 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001166 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 header(s->base)->h_blocks = cpu_to_le32(1);
1168 header(s->base)->h_refcount = cpu_to_le32(1);
1169 s->first = ENTRY(header(s->base)+1);
1170 s->here = ENTRY(header(s->base)+1);
1171 s->end = s->base + sb->s_blocksize;
1172 }
1173
Andreas Dilgere50e5122017-06-21 21:10:32 -04001174 error = ext4_xattr_set_entry(i, s, handle, inode);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001175 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 goto bad_block;
1177 if (error)
1178 goto cleanup;
1179 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001180 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181
1182inserted:
1183 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001184 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 if (new_bh) {
1186 /* We found an identical block in the cache. */
1187 if (new_bh == bs->bh)
1188 ea_bdebug(new_bh, "keeping");
1189 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001190 u32 ref;
1191
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001192 WARN_ON_ONCE(dquot_initialize_needed(inode));
1193
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 /* The old block is released after updating
1195 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001196 error = dquot_alloc_block(inode,
1197 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001198 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001200 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001201 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 new_bh);
1203 if (error)
1204 goto cleanup_dquot;
1205 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001206 /*
1207 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001208 * freeing, rehashing or adding references to
1209 * xattr block. Once we hold buffer lock xattr
1210 * block's state is stable so we can check
1211 * whether the block got freed / rehashed or
1212 * not. Since we unhash mbcache entry under
1213 * buffer lock when freeing / rehashing xattr
1214 * block, checking whether entry is still
1215 * hashed is reliable. Same rules hold for
1216 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001217 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001218 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1219 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001220 /*
1221 * Undo everything and check mbcache
1222 * again.
1223 */
1224 unlock_buffer(new_bh);
1225 dquot_free_block(inode,
1226 EXT4_C2B(EXT4_SB(sb),
1227 1));
1228 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001229 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001230 ce = NULL;
1231 new_bh = NULL;
1232 goto inserted;
1233 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001234 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1235 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1236 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1237 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001239 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001240 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001242 error = ext4_handle_dirty_metadata(handle,
1243 inode,
1244 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 if (error)
1246 goto cleanup_dquot;
1247 }
Jan Kara7a2508e2016-02-22 22:35:22 -05001248 mb_cache_entry_touch(ext4_mb_cache, ce);
1249 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001250 ce = NULL;
1251 } else if (bs->bh && s->base == bs->bh->b_data) {
1252 /* We were modifying this block in-place. */
1253 ea_bdebug(bs->bh, "keeping this block");
1254 new_bh = bs->bh;
1255 get_bh(new_bh);
1256 } else {
1257 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001258 ext4_fsblk_t goal, block;
1259
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001260 WARN_ON_ONCE(dquot_initialize_needed(inode));
1261
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001262 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001263 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001264
1265 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001266 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001267 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1268
Allison Henderson55f020d2011-05-25 07:41:26 -04001269 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1270 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 if (error)
1272 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001273
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001274 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001275 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1276
Joe Perchesace36ad2012-03-19 23:11:43 -04001277 ea_idebug(inode, "creating block %llu",
1278 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001279
1280 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001281 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001282 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001284 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001285 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 goto cleanup;
1287 }
1288 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001289 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290 if (error) {
1291 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001292 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 goto getblk_failed;
1294 }
1295 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001296 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 set_buffer_uptodate(new_bh);
1298 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001299 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001300 error = ext4_handle_dirty_metadata(handle, inode,
1301 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 if (error)
1303 goto cleanup;
1304 }
1305 }
1306
1307 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001308 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309
1310 /* Drop the previous xattr block. */
1311 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001312 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313 error = 0;
1314
1315cleanup:
1316 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001317 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 brelse(new_bh);
1319 if (!(bs->bh && s->base == bs->bh->b_data))
1320 kfree(s->base);
1321
1322 return error;
1323
1324cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001325 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326 goto cleanup;
1327
1328bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001329 EXT4_ERROR_INODE(inode, "bad block %llu",
1330 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 goto cleanup;
1332
1333#undef header
1334}
1335
Tao Ma879b3822012-12-05 10:28:46 -05001336int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1337 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001338{
Mingming Cao617ba132006-10-11 01:20:53 -07001339 struct ext4_xattr_ibody_header *header;
1340 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341 int error;
1342
Mingming Cao617ba132006-10-11 01:20:53 -07001343 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001345 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001346 header = IHDR(inode, raw_inode);
1347 is->s.base = is->s.first = IFIRST(header);
1348 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001349 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001350 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001351 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001352 if (error)
1353 return error;
1354 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001355 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001356 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001357 if (error && error != -ENODATA)
1358 return error;
1359 is->s.not_found = error;
1360 }
1361 return 0;
1362}
1363
Tao Ma0d812f72012-12-10 14:06:02 -05001364int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1365 struct ext4_xattr_info *i,
1366 struct ext4_xattr_ibody_find *is)
1367{
1368 struct ext4_xattr_ibody_header *header;
1369 struct ext4_xattr_search *s = &is->s;
1370 int error;
1371
1372 if (EXT4_I(inode)->i_extra_isize == 0)
1373 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001374 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001375 if (error) {
1376 if (error == -ENOSPC &&
1377 ext4_has_inline_data(inode)) {
1378 error = ext4_try_to_evict_inline_data(handle, inode,
1379 EXT4_XATTR_LEN(strlen(i->name) +
1380 EXT4_XATTR_SIZE(i->value_len)));
1381 if (error)
1382 return error;
1383 error = ext4_xattr_ibody_find(inode, i, is);
1384 if (error)
1385 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001386 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001387 }
1388 if (error)
1389 return error;
1390 }
1391 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1392 if (!IS_LAST_ENTRY(s->first)) {
1393 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1394 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1395 } else {
1396 header->h_magic = cpu_to_le32(0);
1397 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1398 }
1399 return 0;
1400}
1401
Andreas Dilgere50e5122017-06-21 21:10:32 -04001402static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001403 struct ext4_xattr_info *i,
1404 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001405{
Mingming Cao617ba132006-10-11 01:20:53 -07001406 struct ext4_xattr_ibody_header *header;
1407 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408 int error;
1409
Mingming Cao617ba132006-10-11 01:20:53 -07001410 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001412 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413 if (error)
1414 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001415 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001416 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001417 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001418 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419 } else {
1420 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001421 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001422 }
1423 return 0;
1424}
1425
Jan Kara3fd16462016-02-22 22:43:04 -05001426static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1427 struct ext4_xattr_info *i)
1428{
1429 void *value;
1430
1431 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1432 return 0;
1433 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1434 return !memcmp(value, i->value, i->value_len);
1435}
1436
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001437/*
Mingming Cao617ba132006-10-11 01:20:53 -07001438 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001440 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 * is NULL to remove an existing extended attribute, and non-NULL to
1442 * either replace an existing extended attribute, or create a new extended
1443 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1444 * specify that an extended attribute must exist and must not exist
1445 * previous to the call, respectively.
1446 *
1447 * Returns 0, or a negative error number on failure.
1448 */
1449int
Mingming Cao617ba132006-10-11 01:20:53 -07001450ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451 const char *name, const void *value, size_t value_len,
1452 int flags)
1453{
Mingming Cao617ba132006-10-11 01:20:53 -07001454 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455 .name_index = name_index,
1456 .name = name,
1457 .value = value,
1458 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04001459 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001460 };
Mingming Cao617ba132006-10-11 01:20:53 -07001461 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 .s = { .not_found = -ENODATA, },
1463 };
Mingming Cao617ba132006-10-11 01:20:53 -07001464 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 .s = { .not_found = -ENODATA, },
1466 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001467 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 int error;
1469
1470 if (!name)
1471 return -EINVAL;
1472 if (strlen(name) > 255)
1473 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001474
Theodore Ts'oc755e252017-01-11 21:50:46 -05001475 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001476
Eric Sandeen66543612011-10-26 03:32:07 -04001477 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001478 if (error)
1479 goto cleanup;
1480
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001481 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001482 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1483 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001484 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 }
1486
Mingming Cao617ba132006-10-11 01:20:53 -07001487 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001488 if (error)
1489 goto cleanup;
1490 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001491 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 if (error)
1493 goto cleanup;
1494 if (is.s.not_found && bs.s.not_found) {
1495 error = -ENODATA;
1496 if (flags & XATTR_REPLACE)
1497 goto cleanup;
1498 error = 0;
1499 if (!value)
1500 goto cleanup;
1501 } else {
1502 error = -EEXIST;
1503 if (flags & XATTR_CREATE)
1504 goto cleanup;
1505 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001506 if (!value) {
1507 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001508 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001510 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001512 error = 0;
1513 /* Xattr value did not change? Save us some work and bail out */
1514 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1515 goto cleanup;
1516 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1517 goto cleanup;
1518
Andreas Dilgere50e5122017-06-21 21:10:32 -04001519 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001520 if (!error && !bs.s.not_found) {
1521 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001522 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001524 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1525 error = ext4_xattr_block_find(inode, &i, &bs);
1526 if (error)
1527 goto cleanup;
1528 }
Mingming Cao617ba132006-10-11 01:20:53 -07001529 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001530 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1531 error == -ENOSPC) {
1532 /* xattr not fit to block, store at external
1533 * inode */
1534 i.in_inode = 1;
1535 error = ext4_xattr_ibody_set(handle, inode,
1536 &i, &is);
1537 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001538 if (error)
1539 goto cleanup;
1540 if (!is.s.not_found) {
1541 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001542 error = ext4_xattr_ibody_set(handle, inode, &i,
1543 &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001544 }
1545 }
1546 }
1547 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001548 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001549 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001550 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001551 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001552 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001553 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001554 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 * error != 0.
1556 */
1557 is.iloc.bh = NULL;
1558 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001559 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560 }
1561
1562cleanup:
1563 brelse(is.iloc.bh);
1564 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001565 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001566 return error;
1567}
1568
1569/*
Mingming Cao617ba132006-10-11 01:20:53 -07001570 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571 *
Mingming Cao617ba132006-10-11 01:20:53 -07001572 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 * attribute modification is a filesystem transaction by itself.
1574 *
1575 * Returns 0, or a negative error number on failure.
1576 */
1577int
Mingming Cao617ba132006-10-11 01:20:53 -07001578ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001579 const void *value, size_t value_len, int flags)
1580{
1581 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001582 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001584 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001586 error = dquot_initialize(inode);
1587 if (error)
1588 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001589
1590 if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
1591 ext4_has_feature_ea_inode(sb)) {
1592 int nrblocks = (value_len + sb->s_blocksize - 1) >>
1593 sb->s_blocksize_bits;
1594
1595 /* For new inode */
1596 credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
1597
1598 /* For data blocks of EA inode */
1599 credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
1600 }
1601
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001603 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604 if (IS_ERR(handle)) {
1605 error = PTR_ERR(handle);
1606 } else {
1607 int error2;
1608
Mingming Cao617ba132006-10-11 01:20:53 -07001609 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001611 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04001613 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614 goto retry;
1615 if (error == 0)
1616 error = error2;
1617 }
1618
1619 return error;
1620}
1621
1622/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001623 * Shift the EA entries in the inode to create space for the increased
1624 * i_extra_isize.
1625 */
1626static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1627 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001628 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001629{
1630 struct ext4_xattr_entry *last = entry;
1631 int new_offs;
1632
Jan Kara94405712016-08-29 15:41:11 -04001633 /* We always shift xattr headers further thus offsets get lower */
1634 BUG_ON(value_offs_shift > 0);
1635
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001636 /* Adjust the value offsets of the entries */
1637 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001638 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001639 new_offs = le16_to_cpu(last->e_value_offs) +
1640 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001641 last->e_value_offs = cpu_to_le16(new_offs);
1642 }
1643 }
1644 /* Shift the entries by n bytes */
1645 memmove(to, from, n);
1646}
1647
1648/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001649 * Move xattr pointed to by 'entry' from inode into external xattr block
1650 */
1651static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1652 struct ext4_inode *raw_inode,
1653 struct ext4_xattr_entry *entry)
1654{
1655 struct ext4_xattr_ibody_find *is = NULL;
1656 struct ext4_xattr_block_find *bs = NULL;
1657 char *buffer = NULL, *b_entry_name = NULL;
1658 size_t value_offs, value_size;
1659 struct ext4_xattr_info i = {
1660 .value = NULL,
1661 .value_len = 0,
1662 .name_index = entry->e_name_index,
1663 };
1664 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1665 int error;
1666
1667 value_offs = le16_to_cpu(entry->e_value_offs);
1668 value_size = le32_to_cpu(entry->e_value_size);
1669
1670 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1671 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1672 buffer = kmalloc(value_size, GFP_NOFS);
1673 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1674 if (!is || !bs || !buffer || !b_entry_name) {
1675 error = -ENOMEM;
1676 goto out;
1677 }
1678
1679 is->s.not_found = -ENODATA;
1680 bs->s.not_found = -ENODATA;
1681 is->iloc.bh = NULL;
1682 bs->bh = NULL;
1683
1684 /* Save the entry name and the entry value */
1685 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1686 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1687 b_entry_name[entry->e_name_len] = '\0';
1688 i.name = b_entry_name;
1689
1690 error = ext4_get_inode_loc(inode, &is->iloc);
1691 if (error)
1692 goto out;
1693
1694 error = ext4_xattr_ibody_find(inode, &i, is);
1695 if (error)
1696 goto out;
1697
1698 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001699 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001700 if (error)
1701 goto out;
1702
1703 i.name = b_entry_name;
1704 i.value = buffer;
1705 i.value_len = value_size;
1706 error = ext4_xattr_block_find(inode, &i, bs);
1707 if (error)
1708 goto out;
1709
1710 /* Add entry which was removed from the inode into the block */
1711 error = ext4_xattr_block_set(handle, inode, &i, bs);
1712 if (error)
1713 goto out;
1714 error = 0;
1715out:
1716 kfree(b_entry_name);
1717 kfree(buffer);
1718 if (is)
1719 brelse(is->iloc.bh);
1720 kfree(is);
1721 kfree(bs);
1722
1723 return error;
1724}
1725
Jan Karadfa20642016-08-29 15:44:11 -04001726static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1727 struct ext4_inode *raw_inode,
1728 int isize_diff, size_t ifree,
1729 size_t bfree, int *total_ino)
1730{
1731 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1732 struct ext4_xattr_entry *small_entry;
1733 struct ext4_xattr_entry *entry;
1734 struct ext4_xattr_entry *last;
1735 unsigned int entry_size; /* EA entry size */
1736 unsigned int total_size; /* EA entry size + value size */
1737 unsigned int min_total_size;
1738 int error;
1739
1740 while (isize_diff > ifree) {
1741 entry = NULL;
1742 small_entry = NULL;
1743 min_total_size = ~0U;
1744 last = IFIRST(header);
1745 /* Find the entry best suited to be pushed into EA block */
1746 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1747 total_size =
1748 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1749 EXT4_XATTR_LEN(last->e_name_len);
1750 if (total_size <= bfree &&
1751 total_size < min_total_size) {
1752 if (total_size + ifree < isize_diff) {
1753 small_entry = last;
1754 } else {
1755 entry = last;
1756 min_total_size = total_size;
1757 }
1758 }
1759 }
1760
1761 if (entry == NULL) {
1762 if (small_entry == NULL)
1763 return -ENOSPC;
1764 entry = small_entry;
1765 }
1766
1767 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1768 total_size = entry_size +
1769 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1770 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1771 entry);
1772 if (error)
1773 return error;
1774
1775 *total_ino -= entry_size;
1776 ifree += total_size;
1777 bfree -= total_size;
1778 }
1779
1780 return 0;
1781}
1782
Jan Kara3f2571c2016-08-29 15:42:11 -04001783/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001784 * Expand an inode by new_extra_isize bytes when EAs are present.
1785 * Returns 0 on success or negative error number on failure.
1786 */
1787int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1788 struct ext4_inode *raw_inode, handle_t *handle)
1789{
1790 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001791 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001792 size_t min_offs;
1793 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001794 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001795 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001796 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001797 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 -04001798 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001799 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001800
Theodore Ts'oc755e252017-01-11 21:50:46 -05001801 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1802 return 0;
1803
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001804retry:
Jan Karad0141192016-08-11 11:50:30 -04001805 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001806 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1807 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001808
1809 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001810
1811 /*
1812 * Check if enough free space is available in the inode to shift the
1813 * entries ahead by new_extra_isize.
1814 */
1815
Jan Kara6e0cd082016-08-29 15:43:11 -04001816 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001817 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1818 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001819 total_ino = sizeof(struct ext4_xattr_ibody_header);
1820
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001821 error = xattr_check_inode(inode, header, end);
1822 if (error)
1823 goto cleanup;
1824
Jan Kara6e0cd082016-08-29 15:43:11 -04001825 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001826 if (ifree >= isize_diff)
1827 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001828
1829 /*
1830 * Enough free space isn't available in the inode, check if
1831 * EA block can hold new_extra_isize bytes.
1832 */
1833 if (EXT4_I(inode)->i_file_acl) {
1834 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1835 error = -EIO;
1836 if (!bh)
1837 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001838 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001839 EXT4_ERROR_INODE(inode, "bad block %llu",
1840 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001841 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001842 goto cleanup;
1843 }
1844 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001845 end = bh->b_data + bh->b_size;
1846 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001847 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1848 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001849 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001850 if (!tried_min_extra_isize && s_min_extra_isize) {
1851 tried_min_extra_isize++;
1852 new_extra_isize = s_min_extra_isize;
1853 brelse(bh);
1854 goto retry;
1855 }
Jan Karadfa20642016-08-29 15:44:11 -04001856 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001857 goto cleanup;
1858 }
1859 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001860 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001861 }
1862
Jan Karadfa20642016-08-29 15:44:11 -04001863 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1864 isize_diff, ifree, bfree,
1865 &total_ino);
1866 if (error) {
1867 if (error == -ENOSPC && !tried_min_extra_isize &&
1868 s_min_extra_isize) {
1869 tried_min_extra_isize++;
1870 new_extra_isize = s_min_extra_isize;
1871 brelse(bh);
1872 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001873 }
Jan Karadfa20642016-08-29 15:44:11 -04001874 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001875 }
Jan Karae3014d12016-08-29 15:38:11 -04001876shift:
1877 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001878 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001879 - new_extra_isize, (void *)raw_inode +
1880 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001881 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001882 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001883 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001884out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001885 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001886 return 0;
1887
1888cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001889 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001890 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001891 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001892 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001893 no_expand = 1;
1894 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001895 return error;
1896}
1897
1898
Andreas Dilgere50e5122017-06-21 21:10:32 -04001899#define EIA_INCR 16 /* must be 2^n */
1900#define EIA_MASK (EIA_INCR - 1)
1901/* Add the large xattr @ino into @lea_ino_array for later deletion.
1902 * If @lea_ino_array is new or full it will be grown and the old
1903 * contents copied over.
1904 */
1905static int
1906ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
1907{
1908 if (*lea_ino_array == NULL) {
1909 /*
1910 * Start with 15 inodes, so it fits into a power-of-two size.
1911 * If *lea_ino_array is NULL, this is essentially offsetof()
1912 */
1913 (*lea_ino_array) =
1914 kmalloc(offsetof(struct ext4_xattr_ino_array,
1915 xia_inodes[EIA_MASK]),
1916 GFP_NOFS);
1917 if (*lea_ino_array == NULL)
1918 return -ENOMEM;
1919 (*lea_ino_array)->xia_count = 0;
1920 } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
1921 /* expand the array once all 15 + n * 16 slots are full */
1922 struct ext4_xattr_ino_array *new_array = NULL;
1923 int count = (*lea_ino_array)->xia_count;
1924
1925 /* if new_array is NULL, this is essentially offsetof() */
1926 new_array = kmalloc(
1927 offsetof(struct ext4_xattr_ino_array,
1928 xia_inodes[count + EIA_INCR]),
1929 GFP_NOFS);
1930 if (new_array == NULL)
1931 return -ENOMEM;
1932 memcpy(new_array, *lea_ino_array,
1933 offsetof(struct ext4_xattr_ino_array,
1934 xia_inodes[count]));
1935 kfree(*lea_ino_array);
1936 *lea_ino_array = new_array;
1937 }
1938 (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
1939 return 0;
1940}
1941
1942/**
1943 * Add xattr inode to orphan list
1944 */
1945static int
1946ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
1947 int credits, struct ext4_xattr_ino_array *lea_ino_array)
1948{
1949 struct inode *ea_inode = NULL;
1950 int idx = 0, error = 0;
1951
1952 if (lea_ino_array == NULL)
1953 return 0;
1954
1955 for (; idx < lea_ino_array->xia_count; ++idx) {
1956 if (!ext4_handle_has_enough_credits(handle, credits)) {
1957 error = ext4_journal_extend(handle, credits);
1958 if (error > 0)
1959 error = ext4_journal_restart(handle, credits);
1960
1961 if (error != 0) {
1962 ext4_warning(inode->i_sb,
1963 "couldn't extend journal "
1964 "(err %d)", error);
1965 return error;
1966 }
1967 }
1968 ea_inode = ext4_xattr_inode_iget(inode,
1969 lea_ino_array->xia_inodes[idx], &error);
1970 if (error)
1971 continue;
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001972 inode_lock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001973 ext4_orphan_add(handle, ea_inode);
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001974 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001975 /* the inode's i_count will be released by caller */
1976 }
1977
1978 return 0;
1979}
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001980
1981/*
Mingming Cao617ba132006-10-11 01:20:53 -07001982 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04001984 * Free extended attribute resources associated with this inode. Traverse
1985 * all entries and unlink any xattr inodes associated with this inode. This
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001986 * is called immediately before an inode is freed. We have exclusive
Andreas Dilgere50e5122017-06-21 21:10:32 -04001987 * access to the inode. If an orphan inode is deleted it will also delete any
1988 * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
1989 * to ensure they belong to the parent inode and were not deleted already.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001990 */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001991int
1992ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1993 struct ext4_xattr_ino_array **lea_ino_array)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994{
1995 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001996 struct ext4_xattr_ibody_header *header;
1997 struct ext4_inode *raw_inode;
1998 struct ext4_iloc iloc;
1999 struct ext4_xattr_entry *entry;
2000 int credits = 3, error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001
Andreas Dilgere50e5122017-06-21 21:10:32 -04002002 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
2003 goto delete_external_ea;
2004
2005 error = ext4_get_inode_loc(inode, &iloc);
2006 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002008 raw_inode = ext4_raw_inode(&iloc);
2009 header = IHDR(inode, raw_inode);
2010 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
2011 entry = EXT4_XATTR_NEXT(entry)) {
2012 if (!entry->e_value_inum)
2013 continue;
2014 if (ext4_expand_ino_array(lea_ino_array,
2015 entry->e_value_inum) != 0) {
2016 brelse(iloc.bh);
2017 goto cleanup;
2018 }
2019 entry->e_value_inum = 0;
2020 }
2021 brelse(iloc.bh);
2022
2023delete_external_ea:
2024 if (!EXT4_I(inode)->i_file_acl) {
2025 /* add xattr inode to orphan list */
2026 ext4_xattr_inode_orphan_add(handle, inode, credits,
2027 *lea_ino_array);
2028 goto cleanup;
2029 }
Mingming Cao617ba132006-10-11 01:20:53 -07002030 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002031 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002032 EXT4_ERROR_INODE(inode, "block %llu read error",
2033 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 goto cleanup;
2035 }
Mingming Cao617ba132006-10-11 01:20:53 -07002036 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002038 EXT4_ERROR_INODE(inode, "bad block %llu",
2039 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002040 goto cleanup;
2041 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04002042
2043 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2044 entry = EXT4_XATTR_NEXT(entry)) {
2045 if (!entry->e_value_inum)
2046 continue;
2047 if (ext4_expand_ino_array(lea_ino_array,
2048 entry->e_value_inum) != 0)
2049 goto cleanup;
2050 entry->e_value_inum = 0;
2051 }
2052
2053 /* add xattr inode to orphan list */
2054 error = ext4_xattr_inode_orphan_add(handle, inode, credits,
2055 *lea_ino_array);
2056 if (error != 0)
2057 goto cleanup;
2058
2059 if (!IS_NOQUOTA(inode))
2060 credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
2061
2062 if (!ext4_handle_has_enough_credits(handle, credits)) {
2063 error = ext4_journal_extend(handle, credits);
2064 if (error > 0)
2065 error = ext4_journal_restart(handle, credits);
2066 if (error != 0) {
2067 ext4_warning(inode->i_sb,
2068 "couldn't extend journal (err %d)", error);
2069 goto cleanup;
2070 }
2071 }
2072
Mingming Cao617ba132006-10-11 01:20:53 -07002073 ext4_xattr_release_block(handle, inode, bh);
2074 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075
2076cleanup:
2077 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002078
2079 return error;
2080}
2081
2082void
2083ext4_xattr_inode_array_free(struct inode *inode,
2084 struct ext4_xattr_ino_array *lea_ino_array)
2085{
2086 struct inode *ea_inode = NULL;
2087 int idx = 0;
2088 int err;
2089
2090 if (lea_ino_array == NULL)
2091 return;
2092
2093 for (; idx < lea_ino_array->xia_count; ++idx) {
2094 ea_inode = ext4_xattr_inode_iget(inode,
2095 lea_ino_array->xia_inodes[idx], &err);
2096 if (err)
2097 continue;
2098 /* for inode's i_count get from ext4_xattr_delete_inode */
2099 if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
2100 iput(ea_inode);
2101 clear_nlink(ea_inode);
2102 iput(ea_inode);
2103 }
2104 kfree(lea_ino_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002105}
2106
2107/*
Mingming Cao617ba132006-10-11 01:20:53 -07002108 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109 *
2110 * Create a new entry in the extended attribute cache, and insert
2111 * it unless such an entry is already in the cache.
2112 *
2113 * Returns 0, or a negative error number on failure.
2114 */
2115static void
Jan Kara7a2508e2016-02-22 22:35:22 -05002116ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002118 struct ext4_xattr_header *header = BHDR(bh);
2119 __u32 hash = le32_to_cpu(header->h_hash);
2120 int reusable = le32_to_cpu(header->h_refcount) <
2121 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 int error;
2123
Jan Kara7a2508e2016-02-22 22:35:22 -05002124 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002125 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002126 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002127 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002129 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131}
2132
2133/*
Mingming Cao617ba132006-10-11 01:20:53 -07002134 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 *
2136 * Compare two extended attribute blocks for equality.
2137 *
2138 * Returns 0 if the blocks are equal, 1 if they differ, and
2139 * a negative error number on errors.
2140 */
2141static int
Mingming Cao617ba132006-10-11 01:20:53 -07002142ext4_xattr_cmp(struct ext4_xattr_header *header1,
2143 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144{
Mingming Cao617ba132006-10-11 01:20:53 -07002145 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146
2147 entry1 = ENTRY(header1+1);
2148 entry2 = ENTRY(header2+1);
2149 while (!IS_LAST_ENTRY(entry1)) {
2150 if (IS_LAST_ENTRY(entry2))
2151 return 1;
2152 if (entry1->e_hash != entry2->e_hash ||
2153 entry1->e_name_index != entry2->e_name_index ||
2154 entry1->e_name_len != entry2->e_name_len ||
2155 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002156 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002157 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2158 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2160 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2161 le32_to_cpu(entry1->e_value_size)))
2162 return 1;
2163
Mingming Cao617ba132006-10-11 01:20:53 -07002164 entry1 = EXT4_XATTR_NEXT(entry1);
2165 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166 }
2167 if (!IS_LAST_ENTRY(entry2))
2168 return 1;
2169 return 0;
2170}
2171
2172/*
Mingming Cao617ba132006-10-11 01:20:53 -07002173 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 *
2175 * Find an identical extended attribute block.
2176 *
2177 * Returns a pointer to the block found, or NULL if such a block was
2178 * not found or an error occurred.
2179 */
2180static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07002181ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05002182 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183{
2184 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002185 struct mb_cache_entry *ce;
2186 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002187
2188 if (!header->h_hash)
2189 return NULL; /* never share */
2190 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002191 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002192 while (ce) {
2193 struct buffer_head *bh;
2194
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002195 bh = sb_bread(inode->i_sb, ce->e_block);
2196 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002197 EXT4_ERROR_INODE(inode, "block %lu read error",
2198 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07002199 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002200 *pce = ce;
2201 return bh;
2202 }
2203 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05002204 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205 }
2206 return NULL;
2207}
2208
2209#define NAME_HASH_SHIFT 5
2210#define VALUE_HASH_SHIFT 16
2211
2212/*
Mingming Cao617ba132006-10-11 01:20:53 -07002213 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002214 *
2215 * Compute the hash of an extended attribute.
2216 */
Mingming Cao617ba132006-10-11 01:20:53 -07002217static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2218 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002219{
2220 __u32 hash = 0;
2221 char *name = entry->e_name;
2222 int n;
2223
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002224 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225 hash = (hash << NAME_HASH_SHIFT) ^
2226 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2227 *name++;
2228 }
2229
Andreas Dilgere50e5122017-06-21 21:10:32 -04002230 if (!entry->e_value_inum && entry->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 __le32 *value = (__le32 *)((char *)header +
2232 le16_to_cpu(entry->e_value_offs));
2233 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07002234 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 hash = (hash << VALUE_HASH_SHIFT) ^
2236 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2237 le32_to_cpu(*value++);
2238 }
2239 }
2240 entry->e_hash = cpu_to_le32(hash);
2241}
2242
2243#undef NAME_HASH_SHIFT
2244#undef VALUE_HASH_SHIFT
2245
2246#define BLOCK_HASH_SHIFT 16
2247
2248/*
Mingming Cao617ba132006-10-11 01:20:53 -07002249 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002250 *
2251 * Re-compute the extended attribute hash value after an entry has changed.
2252 */
Mingming Cao617ba132006-10-11 01:20:53 -07002253static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2254 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255{
Mingming Cao617ba132006-10-11 01:20:53 -07002256 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002257 __u32 hash = 0;
2258
Mingming Cao617ba132006-10-11 01:20:53 -07002259 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 here = ENTRY(header+1);
2261 while (!IS_LAST_ENTRY(here)) {
2262 if (!here->e_hash) {
2263 /* Block is not shared if an entry's hash value == 0 */
2264 hash = 0;
2265 break;
2266 }
2267 hash = (hash << BLOCK_HASH_SHIFT) ^
2268 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2269 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07002270 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 }
2272 header->h_hash = cpu_to_le32(hash);
2273}
2274
2275#undef BLOCK_HASH_SHIFT
2276
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002277#define HASH_BUCKET_BITS 10
2278
Jan Kara7a2508e2016-02-22 22:35:22 -05002279struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05002280ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281{
Jan Kara7a2508e2016-02-22 22:35:22 -05002282 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283}
2284
Jan Kara7a2508e2016-02-22 22:35:22 -05002285void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002287 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05002288 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002290