blob: 2be891ffeda19d1f215fd138f2368a34edc6aae6 [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 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400281static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400282{
283 unsigned long block = 0;
284 struct buffer_head *bh = NULL;
Tahsin Erdogan909666932017-06-21 21:57:36 -0400285 int blocksize = ea_inode->i_sb->s_blocksize;
286 size_t csize, copied = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400287
Tahsin Erdogan909666932017-06-21 21:57:36 -0400288 while (copied < size) {
289 csize = (size - copied) > blocksize ? blocksize : size - copied;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400290 bh = ext4_bread(NULL, ea_inode, block, 0);
Tahsin Erdogan909666932017-06-21 21:57:36 -0400291 if (IS_ERR(bh))
Andreas Dilgere50e5122017-06-21 21:10:32 -0400292 return PTR_ERR(bh);
Tahsin Erdogan909666932017-06-21 21:57:36 -0400293 if (!bh)
294 return -EFSCORRUPTED;
295
Andreas Dilgere50e5122017-06-21 21:10:32 -0400296 memcpy(buf, bh->b_data, csize);
297 brelse(bh);
298
299 buf += csize;
300 block += 1;
Tahsin Erdogan909666932017-06-21 21:57:36 -0400301 copied += csize;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400302 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400303 return 0;
304}
305
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400306static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
307 struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400308{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400309 struct inode *inode;
310 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400311
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400312 inode = ext4_iget(parent->i_sb, ea_ino);
313 if (IS_ERR(inode)) {
314 err = PTR_ERR(inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400315 ext4_error(parent->i_sb, "error while reading EA inode %lu "
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400316 "err=%d", ea_ino, err);
317 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400318 }
319
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400320 if (is_bad_inode(inode)) {
321 ext4_error(parent->i_sb, "error while reading EA inode %lu "
322 "is_bad_inode", ea_ino);
323 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400324 goto error;
325 }
326
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400327 if (EXT4_XATTR_INODE_GET_PARENT(inode) != parent->i_ino ||
328 inode->i_generation != parent->i_generation) {
329 ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
330 "to parent is invalid.", ea_ino);
331 err = -EINVAL;
332 goto error;
333 }
334
335 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400336 ext4_error(parent->i_sb, "EA inode %lu does not have "
337 "EXT4_EA_INODE_FL flag set.\n", ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400338 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400339 goto error;
340 }
341
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400342 *ea_inode = inode;
343 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400344error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400345 iput(inode);
346 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400347}
348
349/*
350 * Read the value from the EA inode.
351 */
352static int
353ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
Tahsin Erdogan909666932017-06-21 21:57:36 -0400354 size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400355{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400356 struct inode *ea_inode;
357 int ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400358
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400359 ret = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
360 if (ret)
361 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400362
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400363 ret = ext4_xattr_inode_read(ea_inode, buffer, size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400364 iput(ea_inode);
365
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400366 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400367}
368
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700369static int
Mingming Cao617ba132006-10-11 01:20:53 -0700370ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371 void *buffer, size_t buffer_size)
372{
373 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700374 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 size_t size;
376 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500377 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378
379 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
380 name_index, name, buffer, (long)buffer_size);
381
382 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700383 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400385 ea_idebug(inode, "reading block %llu",
386 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700387 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388 if (!bh)
389 goto cleanup;
390 ea_bdebug(bh, "b_count=%d, refcount=%d",
391 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400392 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400393 EXT4_ERROR_INODE(inode, "bad block %llu",
394 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400395 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 goto cleanup;
397 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400398 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400400 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401 if (error)
402 goto cleanup;
403 size = le32_to_cpu(entry->e_value_size);
404 if (buffer) {
405 error = -ERANGE;
406 if (size > buffer_size)
407 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400408 if (entry->e_value_inum) {
409 error = ext4_xattr_inode_get(inode,
410 le32_to_cpu(entry->e_value_inum),
Tahsin Erdogan909666932017-06-21 21:57:36 -0400411 buffer, size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400412 if (error)
413 goto cleanup;
414 } else {
415 memcpy(buffer, bh->b_data +
416 le16_to_cpu(entry->e_value_offs), size);
417 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418 }
419 error = size;
420
421cleanup:
422 brelse(bh);
423 return error;
424}
425
Tao Ma879b3822012-12-05 10:28:46 -0500426int
Mingming Cao617ba132006-10-11 01:20:53 -0700427ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 void *buffer, size_t buffer_size)
429{
Mingming Cao617ba132006-10-11 01:20:53 -0700430 struct ext4_xattr_ibody_header *header;
431 struct ext4_xattr_entry *entry;
432 struct ext4_inode *raw_inode;
433 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 size_t size;
435 void *end;
436 int error;
437
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500438 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700440 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441 if (error)
442 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700443 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700445 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400446 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 if (error)
448 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400449 entry = IFIRST(header);
450 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 if (error)
452 goto cleanup;
453 size = le32_to_cpu(entry->e_value_size);
454 if (buffer) {
455 error = -ERANGE;
456 if (size > buffer_size)
457 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400458 if (entry->e_value_inum) {
459 error = ext4_xattr_inode_get(inode,
460 le32_to_cpu(entry->e_value_inum),
Tahsin Erdogan909666932017-06-21 21:57:36 -0400461 buffer, size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400462 if (error)
463 goto cleanup;
464 } else {
465 memcpy(buffer, (void *)IFIRST(header) +
466 le16_to_cpu(entry->e_value_offs), size);
467 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 }
469 error = size;
470
471cleanup:
472 brelse(iloc.bh);
473 return error;
474}
475
476/*
Mingming Cao617ba132006-10-11 01:20:53 -0700477 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 *
479 * Copy an extended attribute into the buffer
480 * provided, or compute the buffer size required.
481 * Buffer is NULL to compute the size of the buffer required.
482 *
483 * Returns a negative error number on failure, or the number of bytes
484 * used / required on success.
485 */
486int
Mingming Cao617ba132006-10-11 01:20:53 -0700487ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 void *buffer, size_t buffer_size)
489{
490 int error;
491
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500492 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
493 return -EIO;
494
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400495 if (strlen(name) > 255)
496 return -ERANGE;
497
Mingming Cao617ba132006-10-11 01:20:53 -0700498 down_read(&EXT4_I(inode)->xattr_sem);
499 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 buffer_size);
501 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700502 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700504 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 return error;
506}
507
508static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000509ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 char *buffer, size_t buffer_size)
511{
512 size_t rest = buffer_size;
513
Mingming Cao617ba132006-10-11 01:20:53 -0700514 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700515 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700516 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100518 if (handler && (!handler->list || handler->list(dentry))) {
519 const char *prefix = handler->prefix ?: handler->name;
520 size_t prefix_len = strlen(prefix);
521 size_t size = prefix_len + entry->e_name_len + 1;
522
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 if (buffer) {
524 if (size > rest)
525 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100526 memcpy(buffer, prefix, prefix_len);
527 buffer += prefix_len;
528 memcpy(buffer, entry->e_name, entry->e_name_len);
529 buffer += entry->e_name_len;
530 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 }
532 rest -= size;
533 }
534 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100535 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536}
537
538static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000539ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540{
David Howells2b0143b2015-03-17 22:25:59 +0000541 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542 struct buffer_head *bh = NULL;
543 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500544 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545
546 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
547 buffer, (long)buffer_size);
548
549 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700550 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400552 ea_idebug(inode, "reading block %llu",
553 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700554 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 error = -EIO;
556 if (!bh)
557 goto cleanup;
558 ea_bdebug(bh, "b_count=%d, refcount=%d",
559 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400560 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400561 EXT4_ERROR_INODE(inode, "bad block %llu",
562 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400563 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 goto cleanup;
565 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400566 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000567 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568
569cleanup:
570 brelse(bh);
571
572 return error;
573}
574
575static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000576ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577{
David Howells2b0143b2015-03-17 22:25:59 +0000578 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700579 struct ext4_xattr_ibody_header *header;
580 struct ext4_inode *raw_inode;
581 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 void *end;
583 int error;
584
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500585 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700587 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 if (error)
589 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700590 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700592 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400593 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700594 if (error)
595 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000596 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 buffer, buffer_size);
598
599cleanup:
600 brelse(iloc.bh);
601 return error;
602}
603
604/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400605 * Inode operation listxattr()
606 *
607 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608 *
609 * Copy a list of attribute names into the buffer
610 * provided, or compute the buffer size required.
611 * Buffer is NULL to compute the size of the buffer required.
612 *
613 * Returns a negative error number on failure, or the number of bytes
614 * used / required on success.
615 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400616ssize_t
617ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500619 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620
David Howells2b0143b2015-03-17 22:25:59 +0000621 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500622 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
623 if (ret < 0)
624 goto errout;
625 if (buffer) {
626 buffer += ret;
627 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500629 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
630 if (ret < 0)
631 goto errout;
632 ret += ret2;
633errout:
David Howells2b0143b2015-03-17 22:25:59 +0000634 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500635 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636}
637
638/*
Mingming Cao617ba132006-10-11 01:20:53 -0700639 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 * not set, set it.
641 */
Mingming Cao617ba132006-10-11 01:20:53 -0700642static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 struct super_block *sb)
644{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400645 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 return;
647
liang xie5d601252014-05-12 22:06:43 -0400648 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700649 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400650 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400651 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653}
654
655/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400656 * Release the xattr block BH: If the reference count is > 1, decrement it;
657 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 */
659static void
Mingming Cao617ba132006-10-11 01:20:53 -0700660ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 struct buffer_head *bh)
662{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500663 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
664 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800665 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666
liang xie5d601252014-05-12 22:06:43 -0400667 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800668 error = ext4_journal_get_write_access(handle, bh);
669 if (error)
670 goto out;
671
672 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500673 hash = le32_to_cpu(BHDR(bh)->h_hash);
674 ref = le32_to_cpu(BHDR(bh)->h_refcount);
675 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500677 /*
678 * This must happen under buffer lock for
679 * ext4_xattr_block_set() to reliably detect freed block
680 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500681 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400683 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500684 ext4_free_blocks(handle, inode, bh, 0, 1,
685 EXT4_FREE_BLOCKS_METADATA |
686 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500688 ref--;
689 BHDR(bh)->h_refcount = cpu_to_le32(ref);
690 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
691 struct mb_cache_entry *ce;
692
693 ce = mb_cache_entry_get(ext4_mb_cache, hash,
694 bh->b_blocknr);
695 if (ce) {
696 ce->e_reusable = 1;
697 mb_cache_entry_put(ext4_mb_cache, ce);
698 }
699 }
700
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400701 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400702 /*
703 * Beware of this ugliness: Releasing of xattr block references
704 * from different inodes can race and so we have to protect
705 * from a race where someone else frees the block (and releases
706 * its journal_head) before we are done dirtying the buffer. In
707 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400708 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400709 * that function can call sync_dirty_buffer() so for that case
710 * we handle the dirtying after unlocking the buffer.
711 */
712 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400713 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500714 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400715 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400716 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800717 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500718 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500719 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800720 ea_bdebug(bh, "refcount now=%d; releasing",
721 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800723out:
724 ext4_std_error(inode->i_sb, error);
725 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726}
727
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400728/*
729 * Find the available free space for EAs. This also returns the total number of
730 * bytes used by EA entries.
731 */
732static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
733 size_t *min_offs, void *base, int *total)
734{
735 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400736 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400737 size_t offs = le16_to_cpu(last->e_value_offs);
738 if (offs < *min_offs)
739 *min_offs = offs;
740 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500741 if (total)
742 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400743 }
744 return (*min_offs - ((void *)last - base) - sizeof(__u32));
745}
746
Andreas Dilgere50e5122017-06-21 21:10:32 -0400747/*
748 * Write the value of the EA in an inode.
749 */
750static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
751 const void *buf, int bufsize)
752{
753 struct buffer_head *bh = NULL;
754 unsigned long block = 0;
755 unsigned blocksize = ea_inode->i_sb->s_blocksize;
756 unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
757 int csize, wsize = 0;
758 int ret = 0;
759 int retries = 0;
760
761retry:
762 while (ret >= 0 && ret < max_blocks) {
763 struct ext4_map_blocks map;
764 map.m_lblk = block += ret;
765 map.m_len = max_blocks -= ret;
766
767 ret = ext4_map_blocks(handle, ea_inode, &map,
768 EXT4_GET_BLOCKS_CREATE);
769 if (ret <= 0) {
770 ext4_mark_inode_dirty(handle, ea_inode);
771 if (ret == -ENOSPC &&
772 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
773 ret = 0;
774 goto retry;
775 }
776 break;
777 }
778 }
779
780 if (ret < 0)
781 return ret;
782
783 block = 0;
784 while (wsize < bufsize) {
785 if (bh != NULL)
786 brelse(bh);
787 csize = (bufsize - wsize) > blocksize ? blocksize :
788 bufsize - wsize;
789 bh = ext4_getblk(handle, ea_inode, block, 0);
790 if (IS_ERR(bh))
791 return PTR_ERR(bh);
792 ret = ext4_journal_get_write_access(handle, bh);
793 if (ret)
794 goto out;
795
796 memcpy(bh->b_data, buf, csize);
797 set_buffer_uptodate(bh);
798 ext4_handle_dirty_metadata(handle, ea_inode, bh);
799
800 buf += csize;
801 wsize += csize;
802 block += 1;
803 }
804
805 inode_lock(ea_inode);
806 i_size_write(ea_inode, wsize);
807 ext4_update_i_disksize(ea_inode, wsize);
808 inode_unlock(ea_inode);
809
810 ext4_mark_inode_dirty(handle, ea_inode);
811
812out:
813 brelse(bh);
814
815 return ret;
816}
817
818/*
819 * Create an inode to store the value of a large EA.
820 */
821static struct inode *ext4_xattr_inode_create(handle_t *handle,
822 struct inode *inode)
823{
824 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -0400825 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400826 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400827
828 /*
829 * Let the next inode be the goal, so we try and allocate the EA inode
830 * in the same group, or nearby one.
831 */
832 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -0400833 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -0400834 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400835 if (!IS_ERR(ea_inode)) {
836 ea_inode->i_op = &ext4_file_inode_operations;
837 ea_inode->i_fop = &ext4_file_operations;
838 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400839 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400840 ea_inode->i_generation = inode->i_generation;
841 EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
842
843 /*
844 * A back-pointer from EA inode to parent inode will be useful
845 * for e2fsck.
846 */
847 EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
848 unlock_new_inode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400849 err = ext4_inode_attach_jinode(ea_inode);
850 if (err) {
851 iput(ea_inode);
852 return ERR_PTR(err);
853 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400854 }
855
856 return ea_inode;
857}
858
859/*
860 * Unlink the inode storing the value of the EA.
861 */
862int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
863{
864 struct inode *ea_inode = NULL;
865 int err;
866
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400867 err = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400868 if (err)
869 return err;
870
871 clear_nlink(ea_inode);
872 iput(ea_inode);
873
874 return 0;
875}
876
877/*
878 * Add value of the EA in an inode.
879 */
880static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
881 unsigned long *ea_ino, const void *value,
882 size_t value_len)
883{
884 struct inode *ea_inode;
885 int err;
886
887 /* Create an inode for the EA value */
888 ea_inode = ext4_xattr_inode_create(handle, inode);
889 if (IS_ERR(ea_inode))
890 return PTR_ERR(ea_inode);
891
892 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
893 if (err)
894 clear_nlink(ea_inode);
895 else
896 *ea_ino = ea_inode->i_ino;
897
898 iput(ea_inode);
899
900 return err;
901}
902
903static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
904 struct ext4_xattr_search *s,
905 handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906{
Mingming Cao617ba132006-10-11 01:20:53 -0700907 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400909 int in_inode = i->in_inode;
910 int rc;
911
912 if (ext4_has_feature_ea_inode(inode->i_sb) &&
913 (EXT4_XATTR_SIZE(i->value_len) >
914 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
915 in_inode = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916
917 /* Compute min_offs and last. */
918 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700919 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400920 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 size_t offs = le16_to_cpu(last->e_value_offs);
922 if (offs < min_offs)
923 min_offs = offs;
924 }
925 }
926 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
927 if (!s->not_found) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400928 if (!in_inode &&
929 !s->here->e_value_inum && s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700931 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 }
Mingming Cao617ba132006-10-11 01:20:53 -0700933 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 }
935 if (i->value) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400936 size_t value_len = EXT4_XATTR_SIZE(i->value_len);
937
938 if (in_inode)
939 value_len = 0;
940
941 if (free < EXT4_XATTR_LEN(name_len) + value_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942 return -ENOSPC;
943 }
944
945 if (i->value && s->not_found) {
946 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700947 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
949 memmove((void *)s->here + size, s->here, rest);
950 memset(s->here, 0, size);
951 s->here->e_name_index = i->name_index;
952 s->here->e_name_len = name_len;
953 memcpy(s->here->e_name, i->name, name_len);
954 } else {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400955 if (!s->here->e_value_inum && s->here->e_value_size &&
956 s->here->e_value_offs > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 void *first_val = s->base + min_offs;
958 size_t offs = le16_to_cpu(s->here->e_value_offs);
959 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700960 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 le32_to_cpu(s->here->e_value_size));
962
Mingming Cao617ba132006-10-11 01:20:53 -0700963 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 /* The old and the new value have the same
965 size. Just replace. */
966 s->here->e_value_size =
967 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500968 if (i->value == EXT4_ZERO_XATTR_VALUE) {
969 memset(val, 0, size);
970 } else {
971 /* Clear pad bytes first. */
972 memset(val + size - EXT4_XATTR_PAD, 0,
973 EXT4_XATTR_PAD);
974 memcpy(val, i->value, i->value_len);
975 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 return 0;
977 }
978
979 /* Remove the old value. */
980 memmove(first_val + size, first_val, val - first_val);
981 memset(first_val, 0, size);
982 s->here->e_value_size = 0;
983 s->here->e_value_offs = 0;
984 min_offs += size;
985
986 /* Adjust all value offsets. */
987 last = s->first;
988 while (!IS_LAST_ENTRY(last)) {
989 size_t o = le16_to_cpu(last->e_value_offs);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400990 if (!last->e_value_inum &&
991 last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 last->e_value_offs =
993 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700994 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995 }
996 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400997 if (s->here->e_value_inum) {
998 ext4_xattr_inode_unlink(inode,
999 le32_to_cpu(s->here->e_value_inum));
1000 s->here->e_value_inum = 0;
1001 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 if (!i->value) {
1003 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001004 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 last = ENTRY((void *)last - size);
1006 memmove(s->here, (void *)s->here + size,
1007 (void *)last - (void *)s->here + sizeof(__u32));
1008 memset(last, 0, size);
1009 }
1010 }
1011
1012 if (i->value) {
1013 /* Insert the new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001014 if (in_inode) {
1015 unsigned long ea_ino =
1016 le32_to_cpu(s->here->e_value_inum);
1017 rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
1018 i->value, i->value_len);
1019 if (rc)
1020 goto out;
1021 s->here->e_value_inum = cpu_to_le32(ea_ino);
1022 s->here->e_value_offs = 0;
1023 } else if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001024 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 void *val = s->base + min_offs - size;
1026 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001027 s->here->e_value_inum = 0;
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001028 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1029 memset(val, 0, size);
1030 } else {
1031 /* Clear the pad bytes first. */
1032 memset(val + size - EXT4_XATTR_PAD, 0,
1033 EXT4_XATTR_PAD);
1034 memcpy(val, i->value, i->value_len);
1035 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001036 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001037 s->here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001039
1040out:
1041 return rc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001042}
1043
Mingming Cao617ba132006-10-11 01:20:53 -07001044struct ext4_xattr_block_find {
1045 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 struct buffer_head *bh;
1047};
1048
1049static int
Mingming Cao617ba132006-10-11 01:20:53 -07001050ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1051 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052{
1053 struct super_block *sb = inode->i_sb;
1054 int error;
1055
1056 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1057 i->name_index, i->name, i->value, (long)i->value_len);
1058
Mingming Cao617ba132006-10-11 01:20:53 -07001059 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001060 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001061 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 error = -EIO;
1063 if (!bs->bh)
1064 goto cleanup;
1065 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1066 atomic_read(&(bs->bh->b_count)),
1067 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001068 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001069 EXT4_ERROR_INODE(inode, "bad block %llu",
1070 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001071 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 goto cleanup;
1073 }
1074 /* Find the named attribute. */
1075 bs->s.base = BHDR(bs->bh);
1076 bs->s.first = BFIRST(bs->bh);
1077 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1078 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001079 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001080 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081 if (error && error != -ENODATA)
1082 goto cleanup;
1083 bs->s.not_found = error;
1084 }
1085 error = 0;
1086
1087cleanup:
1088 return error;
1089}
1090
1091static int
Mingming Cao617ba132006-10-11 01:20:53 -07001092ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1093 struct ext4_xattr_info *i,
1094 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001095{
1096 struct super_block *sb = inode->i_sb;
1097 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001098 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -05001099 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001100 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -05001101 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102
Mingming Cao617ba132006-10-11 01:20:53 -07001103#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001106 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001107 error = ext4_journal_get_write_access(handle, bs->bh);
1108 if (error)
1109 goto cleanup;
1110 lock_buffer(bs->bh);
1111
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001113 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1114
1115 /*
1116 * This must happen under buffer lock for
1117 * ext4_xattr_block_set() to reliably detect modified
1118 * block
1119 */
Jan Kara7a2508e2016-02-22 22:35:22 -05001120 mb_cache_entry_delete_block(ext4_mb_cache, hash,
1121 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001122 ea_bdebug(bs->bh, "modifying in-place");
Andreas Dilgere50e5122017-06-21 21:10:32 -04001123 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 if (!error) {
1125 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001126 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001128 ext4_xattr_cache_insert(ext4_mb_cache,
1129 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130 }
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001131 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001133 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 goto bad_block;
1135 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001136 error = ext4_handle_dirty_metadata(handle,
1137 inode,
1138 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 if (error)
1140 goto cleanup;
1141 goto inserted;
1142 } else {
1143 int offset = (char *)s->here - bs->bh->b_data;
1144
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001145 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001147 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 error = -ENOMEM;
1149 if (s->base == NULL)
1150 goto cleanup;
1151 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1152 s->first = ENTRY(header(s->base)+1);
1153 header(s->base)->h_refcount = cpu_to_le32(1);
1154 s->here = ENTRY(s->base + offset);
1155 s->end = s->base + bs->bh->b_size;
1156 }
1157 } else {
1158 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001159 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 /* assert(header == s->base) */
1161 error = -ENOMEM;
1162 if (s->base == NULL)
1163 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001164 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 header(s->base)->h_blocks = cpu_to_le32(1);
1166 header(s->base)->h_refcount = cpu_to_le32(1);
1167 s->first = ENTRY(header(s->base)+1);
1168 s->here = ENTRY(header(s->base)+1);
1169 s->end = s->base + sb->s_blocksize;
1170 }
1171
Andreas Dilgere50e5122017-06-21 21:10:32 -04001172 error = ext4_xattr_set_entry(i, s, handle, inode);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001173 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 goto bad_block;
1175 if (error)
1176 goto cleanup;
1177 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001178 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179
1180inserted:
1181 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001182 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 if (new_bh) {
1184 /* We found an identical block in the cache. */
1185 if (new_bh == bs->bh)
1186 ea_bdebug(new_bh, "keeping");
1187 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001188 u32 ref;
1189
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001190 WARN_ON_ONCE(dquot_initialize_needed(inode));
1191
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 /* The old block is released after updating
1193 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001194 error = dquot_alloc_block(inode,
1195 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001196 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001198 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001199 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200 new_bh);
1201 if (error)
1202 goto cleanup_dquot;
1203 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001204 /*
1205 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001206 * freeing, rehashing or adding references to
1207 * xattr block. Once we hold buffer lock xattr
1208 * block's state is stable so we can check
1209 * whether the block got freed / rehashed or
1210 * not. Since we unhash mbcache entry under
1211 * buffer lock when freeing / rehashing xattr
1212 * block, checking whether entry is still
1213 * hashed is reliable. Same rules hold for
1214 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001215 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001216 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1217 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001218 /*
1219 * Undo everything and check mbcache
1220 * again.
1221 */
1222 unlock_buffer(new_bh);
1223 dquot_free_block(inode,
1224 EXT4_C2B(EXT4_SB(sb),
1225 1));
1226 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001227 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001228 ce = NULL;
1229 new_bh = NULL;
1230 goto inserted;
1231 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001232 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1233 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1234 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1235 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001237 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001238 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001240 error = ext4_handle_dirty_metadata(handle,
1241 inode,
1242 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243 if (error)
1244 goto cleanup_dquot;
1245 }
Jan Kara7a2508e2016-02-22 22:35:22 -05001246 mb_cache_entry_touch(ext4_mb_cache, ce);
1247 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001248 ce = NULL;
1249 } else if (bs->bh && s->base == bs->bh->b_data) {
1250 /* We were modifying this block in-place. */
1251 ea_bdebug(bs->bh, "keeping this block");
1252 new_bh = bs->bh;
1253 get_bh(new_bh);
1254 } else {
1255 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001256 ext4_fsblk_t goal, block;
1257
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001258 WARN_ON_ONCE(dquot_initialize_needed(inode));
1259
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001260 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001261 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001262
1263 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001264 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001265 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1266
Allison Henderson55f020d2011-05-25 07:41:26 -04001267 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1268 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 if (error)
1270 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001271
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001272 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001273 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1274
Joe Perchesace36ad2012-03-19 23:11:43 -04001275 ea_idebug(inode, "creating block %llu",
1276 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277
1278 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001279 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001280 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001282 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001283 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 goto cleanup;
1285 }
1286 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001287 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001288 if (error) {
1289 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001290 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001291 goto getblk_failed;
1292 }
1293 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001294 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 set_buffer_uptodate(new_bh);
1296 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001297 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001298 error = ext4_handle_dirty_metadata(handle, inode,
1299 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 if (error)
1301 goto cleanup;
1302 }
1303 }
1304
1305 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001306 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307
1308 /* Drop the previous xattr block. */
1309 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001310 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311 error = 0;
1312
1313cleanup:
1314 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001315 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 brelse(new_bh);
1317 if (!(bs->bh && s->base == bs->bh->b_data))
1318 kfree(s->base);
1319
1320 return error;
1321
1322cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001323 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324 goto cleanup;
1325
1326bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001327 EXT4_ERROR_INODE(inode, "bad block %llu",
1328 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001329 goto cleanup;
1330
1331#undef header
1332}
1333
Tao Ma879b3822012-12-05 10:28:46 -05001334int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1335 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001336{
Mingming Cao617ba132006-10-11 01:20:53 -07001337 struct ext4_xattr_ibody_header *header;
1338 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001339 int error;
1340
Mingming Cao617ba132006-10-11 01:20:53 -07001341 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001343 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344 header = IHDR(inode, raw_inode);
1345 is->s.base = is->s.first = IFIRST(header);
1346 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001347 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001348 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001349 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 if (error)
1351 return error;
1352 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001353 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001354 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001355 if (error && error != -ENODATA)
1356 return error;
1357 is->s.not_found = error;
1358 }
1359 return 0;
1360}
1361
Tao Ma0d812f72012-12-10 14:06:02 -05001362int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1363 struct ext4_xattr_info *i,
1364 struct ext4_xattr_ibody_find *is)
1365{
1366 struct ext4_xattr_ibody_header *header;
1367 struct ext4_xattr_search *s = &is->s;
1368 int error;
1369
1370 if (EXT4_I(inode)->i_extra_isize == 0)
1371 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001372 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001373 if (error) {
1374 if (error == -ENOSPC &&
1375 ext4_has_inline_data(inode)) {
1376 error = ext4_try_to_evict_inline_data(handle, inode,
1377 EXT4_XATTR_LEN(strlen(i->name) +
1378 EXT4_XATTR_SIZE(i->value_len)));
1379 if (error)
1380 return error;
1381 error = ext4_xattr_ibody_find(inode, i, is);
1382 if (error)
1383 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001384 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001385 }
1386 if (error)
1387 return error;
1388 }
1389 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1390 if (!IS_LAST_ENTRY(s->first)) {
1391 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1392 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1393 } else {
1394 header->h_magic = cpu_to_le32(0);
1395 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1396 }
1397 return 0;
1398}
1399
Andreas Dilgere50e5122017-06-21 21:10:32 -04001400static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001401 struct ext4_xattr_info *i,
1402 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403{
Mingming Cao617ba132006-10-11 01:20:53 -07001404 struct ext4_xattr_ibody_header *header;
1405 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 int error;
1407
Mingming Cao617ba132006-10-11 01:20:53 -07001408 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001410 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 if (error)
1412 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001413 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001414 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001415 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001416 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417 } else {
1418 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001419 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 }
1421 return 0;
1422}
1423
Jan Kara3fd16462016-02-22 22:43:04 -05001424static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1425 struct ext4_xattr_info *i)
1426{
1427 void *value;
1428
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04001429 /* When e_value_inum is set the value is stored externally. */
1430 if (s->here->e_value_inum)
1431 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05001432 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1433 return 0;
1434 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1435 return !memcmp(value, i->value, i->value_len);
1436}
1437
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438/*
Mingming Cao617ba132006-10-11 01:20:53 -07001439 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001440 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001441 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442 * is NULL to remove an existing extended attribute, and non-NULL to
1443 * either replace an existing extended attribute, or create a new extended
1444 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1445 * specify that an extended attribute must exist and must not exist
1446 * previous to the call, respectively.
1447 *
1448 * Returns 0, or a negative error number on failure.
1449 */
1450int
Mingming Cao617ba132006-10-11 01:20:53 -07001451ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 const char *name, const void *value, size_t value_len,
1453 int flags)
1454{
Mingming Cao617ba132006-10-11 01:20:53 -07001455 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 .name_index = name_index,
1457 .name = name,
1458 .value = value,
1459 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04001460 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 };
Mingming Cao617ba132006-10-11 01:20:53 -07001462 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001463 .s = { .not_found = -ENODATA, },
1464 };
Mingming Cao617ba132006-10-11 01:20:53 -07001465 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 .s = { .not_found = -ENODATA, },
1467 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001468 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 int error;
1470
1471 if (!name)
1472 return -EINVAL;
1473 if (strlen(name) > 255)
1474 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001475
Theodore Ts'oc755e252017-01-11 21:50:46 -05001476 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001477
Eric Sandeen66543612011-10-26 03:32:07 -04001478 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001479 if (error)
1480 goto cleanup;
1481
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001482 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001483 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1484 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001485 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486 }
1487
Mingming Cao617ba132006-10-11 01:20:53 -07001488 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489 if (error)
1490 goto cleanup;
1491 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001492 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 if (error)
1494 goto cleanup;
1495 if (is.s.not_found && bs.s.not_found) {
1496 error = -ENODATA;
1497 if (flags & XATTR_REPLACE)
1498 goto cleanup;
1499 error = 0;
1500 if (!value)
1501 goto cleanup;
1502 } else {
1503 error = -EEXIST;
1504 if (flags & XATTR_CREATE)
1505 goto cleanup;
1506 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 if (!value) {
1508 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001509 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001511 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001512 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001513 error = 0;
1514 /* Xattr value did not change? Save us some work and bail out */
1515 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1516 goto cleanup;
1517 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1518 goto cleanup;
1519
Andreas Dilgere50e5122017-06-21 21:10:32 -04001520 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001521 if (!error && !bs.s.not_found) {
1522 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001523 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001525 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1526 error = ext4_xattr_block_find(inode, &i, &bs);
1527 if (error)
1528 goto cleanup;
1529 }
Mingming Cao617ba132006-10-11 01:20:53 -07001530 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001531 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1532 error == -ENOSPC) {
1533 /* xattr not fit to block, store at external
1534 * inode */
1535 i.in_inode = 1;
1536 error = ext4_xattr_ibody_set(handle, inode,
1537 &i, &is);
1538 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539 if (error)
1540 goto cleanup;
1541 if (!is.s.not_found) {
1542 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001543 error = ext4_xattr_ibody_set(handle, inode, &i,
1544 &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545 }
1546 }
1547 }
1548 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001549 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001550 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001551 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001552 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001553 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001555 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556 * error != 0.
1557 */
1558 is.iloc.bh = NULL;
1559 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001560 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001561 }
1562
1563cleanup:
1564 brelse(is.iloc.bh);
1565 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001566 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 return error;
1568}
1569
1570/*
Mingming Cao617ba132006-10-11 01:20:53 -07001571 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572 *
Mingming Cao617ba132006-10-11 01:20:53 -07001573 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 * attribute modification is a filesystem transaction by itself.
1575 *
1576 * Returns 0, or a negative error number on failure.
1577 */
1578int
Mingming Cao617ba132006-10-11 01:20:53 -07001579ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 const void *value, size_t value_len, int flags)
1581{
1582 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001583 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001585 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001587 error = dquot_initialize(inode);
1588 if (error)
1589 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001590
1591 if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
1592 ext4_has_feature_ea_inode(sb)) {
1593 int nrblocks = (value_len + sb->s_blocksize - 1) >>
1594 sb->s_blocksize_bits;
1595
1596 /* For new inode */
1597 credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
1598
1599 /* For data blocks of EA inode */
1600 credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
1601 }
1602
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001604 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 if (IS_ERR(handle)) {
1606 error = PTR_ERR(handle);
1607 } else {
1608 int error2;
1609
Mingming Cao617ba132006-10-11 01:20:53 -07001610 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001612 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04001614 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 goto retry;
1616 if (error == 0)
1617 error = error2;
1618 }
1619
1620 return error;
1621}
1622
1623/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001624 * Shift the EA entries in the inode to create space for the increased
1625 * i_extra_isize.
1626 */
1627static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1628 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001629 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001630{
1631 struct ext4_xattr_entry *last = entry;
1632 int new_offs;
1633
Jan Kara94405712016-08-29 15:41:11 -04001634 /* We always shift xattr headers further thus offsets get lower */
1635 BUG_ON(value_offs_shift > 0);
1636
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001637 /* Adjust the value offsets of the entries */
1638 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001639 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001640 new_offs = le16_to_cpu(last->e_value_offs) +
1641 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001642 last->e_value_offs = cpu_to_le16(new_offs);
1643 }
1644 }
1645 /* Shift the entries by n bytes */
1646 memmove(to, from, n);
1647}
1648
1649/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001650 * Move xattr pointed to by 'entry' from inode into external xattr block
1651 */
1652static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1653 struct ext4_inode *raw_inode,
1654 struct ext4_xattr_entry *entry)
1655{
1656 struct ext4_xattr_ibody_find *is = NULL;
1657 struct ext4_xattr_block_find *bs = NULL;
1658 char *buffer = NULL, *b_entry_name = NULL;
1659 size_t value_offs, value_size;
1660 struct ext4_xattr_info i = {
1661 .value = NULL,
1662 .value_len = 0,
1663 .name_index = entry->e_name_index,
1664 };
1665 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1666 int error;
1667
1668 value_offs = le16_to_cpu(entry->e_value_offs);
1669 value_size = le32_to_cpu(entry->e_value_size);
1670
1671 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1672 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1673 buffer = kmalloc(value_size, GFP_NOFS);
1674 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1675 if (!is || !bs || !buffer || !b_entry_name) {
1676 error = -ENOMEM;
1677 goto out;
1678 }
1679
1680 is->s.not_found = -ENODATA;
1681 bs->s.not_found = -ENODATA;
1682 is->iloc.bh = NULL;
1683 bs->bh = NULL;
1684
1685 /* Save the entry name and the entry value */
1686 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1687 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1688 b_entry_name[entry->e_name_len] = '\0';
1689 i.name = b_entry_name;
1690
1691 error = ext4_get_inode_loc(inode, &is->iloc);
1692 if (error)
1693 goto out;
1694
1695 error = ext4_xattr_ibody_find(inode, &i, is);
1696 if (error)
1697 goto out;
1698
1699 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001700 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001701 if (error)
1702 goto out;
1703
1704 i.name = b_entry_name;
1705 i.value = buffer;
1706 i.value_len = value_size;
1707 error = ext4_xattr_block_find(inode, &i, bs);
1708 if (error)
1709 goto out;
1710
1711 /* Add entry which was removed from the inode into the block */
1712 error = ext4_xattr_block_set(handle, inode, &i, bs);
1713 if (error)
1714 goto out;
1715 error = 0;
1716out:
1717 kfree(b_entry_name);
1718 kfree(buffer);
1719 if (is)
1720 brelse(is->iloc.bh);
1721 kfree(is);
1722 kfree(bs);
1723
1724 return error;
1725}
1726
Jan Karadfa20642016-08-29 15:44:11 -04001727static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1728 struct ext4_inode *raw_inode,
1729 int isize_diff, size_t ifree,
1730 size_t bfree, int *total_ino)
1731{
1732 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1733 struct ext4_xattr_entry *small_entry;
1734 struct ext4_xattr_entry *entry;
1735 struct ext4_xattr_entry *last;
1736 unsigned int entry_size; /* EA entry size */
1737 unsigned int total_size; /* EA entry size + value size */
1738 unsigned int min_total_size;
1739 int error;
1740
1741 while (isize_diff > ifree) {
1742 entry = NULL;
1743 small_entry = NULL;
1744 min_total_size = ~0U;
1745 last = IFIRST(header);
1746 /* Find the entry best suited to be pushed into EA block */
1747 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04001748 total_size = EXT4_XATTR_LEN(last->e_name_len);
1749 if (!last->e_value_inum)
1750 total_size += EXT4_XATTR_SIZE(
1751 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04001752 if (total_size <= bfree &&
1753 total_size < min_total_size) {
1754 if (total_size + ifree < isize_diff) {
1755 small_entry = last;
1756 } else {
1757 entry = last;
1758 min_total_size = total_size;
1759 }
1760 }
1761 }
1762
1763 if (entry == NULL) {
1764 if (small_entry == NULL)
1765 return -ENOSPC;
1766 entry = small_entry;
1767 }
1768
1769 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04001770 total_size = entry_size;
1771 if (!entry->e_value_inum)
1772 total_size += EXT4_XATTR_SIZE(
1773 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04001774 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1775 entry);
1776 if (error)
1777 return error;
1778
1779 *total_ino -= entry_size;
1780 ifree += total_size;
1781 bfree -= total_size;
1782 }
1783
1784 return 0;
1785}
1786
Jan Kara3f2571c2016-08-29 15:42:11 -04001787/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001788 * Expand an inode by new_extra_isize bytes when EAs are present.
1789 * Returns 0 on success or negative error number on failure.
1790 */
1791int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1792 struct ext4_inode *raw_inode, handle_t *handle)
1793{
1794 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001795 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001796 size_t min_offs;
1797 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001798 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001799 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001800 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001801 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 -04001802 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001803 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001804
Theodore Ts'oc755e252017-01-11 21:50:46 -05001805 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1806 return 0;
1807
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001808retry:
Jan Karad0141192016-08-11 11:50:30 -04001809 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001810 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1811 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001812
1813 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001814
1815 /*
1816 * Check if enough free space is available in the inode to shift the
1817 * entries ahead by new_extra_isize.
1818 */
1819
Jan Kara6e0cd082016-08-29 15:43:11 -04001820 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001821 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1822 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001823 total_ino = sizeof(struct ext4_xattr_ibody_header);
1824
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001825 error = xattr_check_inode(inode, header, end);
1826 if (error)
1827 goto cleanup;
1828
Jan Kara6e0cd082016-08-29 15:43:11 -04001829 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001830 if (ifree >= isize_diff)
1831 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001832
1833 /*
1834 * Enough free space isn't available in the inode, check if
1835 * EA block can hold new_extra_isize bytes.
1836 */
1837 if (EXT4_I(inode)->i_file_acl) {
1838 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1839 error = -EIO;
1840 if (!bh)
1841 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001842 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001843 EXT4_ERROR_INODE(inode, "bad block %llu",
1844 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001845 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001846 goto cleanup;
1847 }
1848 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001849 end = bh->b_data + bh->b_size;
1850 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001851 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1852 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001853 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001854 if (!tried_min_extra_isize && s_min_extra_isize) {
1855 tried_min_extra_isize++;
1856 new_extra_isize = s_min_extra_isize;
1857 brelse(bh);
1858 goto retry;
1859 }
Jan Karadfa20642016-08-29 15:44:11 -04001860 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001861 goto cleanup;
1862 }
1863 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001864 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001865 }
1866
Jan Karadfa20642016-08-29 15:44:11 -04001867 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1868 isize_diff, ifree, bfree,
1869 &total_ino);
1870 if (error) {
1871 if (error == -ENOSPC && !tried_min_extra_isize &&
1872 s_min_extra_isize) {
1873 tried_min_extra_isize++;
1874 new_extra_isize = s_min_extra_isize;
1875 brelse(bh);
1876 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001877 }
Jan Karadfa20642016-08-29 15:44:11 -04001878 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001879 }
Jan Karae3014d12016-08-29 15:38:11 -04001880shift:
1881 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001882 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001883 - new_extra_isize, (void *)raw_inode +
1884 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001885 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001886 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001887 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001888out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001889 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001890 return 0;
1891
1892cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001893 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001894 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001895 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001896 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001897 no_expand = 1;
1898 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001899 return error;
1900}
1901
1902
Andreas Dilgere50e5122017-06-21 21:10:32 -04001903#define EIA_INCR 16 /* must be 2^n */
1904#define EIA_MASK (EIA_INCR - 1)
1905/* Add the large xattr @ino into @lea_ino_array for later deletion.
1906 * If @lea_ino_array is new or full it will be grown and the old
1907 * contents copied over.
1908 */
1909static int
1910ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
1911{
1912 if (*lea_ino_array == NULL) {
1913 /*
1914 * Start with 15 inodes, so it fits into a power-of-two size.
1915 * If *lea_ino_array is NULL, this is essentially offsetof()
1916 */
1917 (*lea_ino_array) =
1918 kmalloc(offsetof(struct ext4_xattr_ino_array,
1919 xia_inodes[EIA_MASK]),
1920 GFP_NOFS);
1921 if (*lea_ino_array == NULL)
1922 return -ENOMEM;
1923 (*lea_ino_array)->xia_count = 0;
1924 } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
1925 /* expand the array once all 15 + n * 16 slots are full */
1926 struct ext4_xattr_ino_array *new_array = NULL;
1927 int count = (*lea_ino_array)->xia_count;
1928
1929 /* if new_array is NULL, this is essentially offsetof() */
1930 new_array = kmalloc(
1931 offsetof(struct ext4_xattr_ino_array,
1932 xia_inodes[count + EIA_INCR]),
1933 GFP_NOFS);
1934 if (new_array == NULL)
1935 return -ENOMEM;
1936 memcpy(new_array, *lea_ino_array,
1937 offsetof(struct ext4_xattr_ino_array,
1938 xia_inodes[count]));
1939 kfree(*lea_ino_array);
1940 *lea_ino_array = new_array;
1941 }
1942 (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
1943 return 0;
1944}
1945
1946/**
1947 * Add xattr inode to orphan list
1948 */
1949static int
1950ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
1951 int credits, struct ext4_xattr_ino_array *lea_ino_array)
1952{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04001953 struct inode *ea_inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001954 int idx = 0, error = 0;
1955
1956 if (lea_ino_array == NULL)
1957 return 0;
1958
1959 for (; idx < lea_ino_array->xia_count; ++idx) {
1960 if (!ext4_handle_has_enough_credits(handle, credits)) {
1961 error = ext4_journal_extend(handle, credits);
1962 if (error > 0)
1963 error = ext4_journal_restart(handle, credits);
1964
1965 if (error != 0) {
1966 ext4_warning(inode->i_sb,
1967 "couldn't extend journal "
1968 "(err %d)", error);
1969 return error;
1970 }
1971 }
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04001972 error = ext4_xattr_inode_iget(inode,
1973 lea_ino_array->xia_inodes[idx], &ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001974 if (error)
1975 continue;
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001976 inode_lock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001977 ext4_orphan_add(handle, ea_inode);
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001978 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001979 /* the inode's i_count will be released by caller */
1980 }
1981
1982 return 0;
1983}
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001984
1985/*
Mingming Cao617ba132006-10-11 01:20:53 -07001986 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001987 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04001988 * Free extended attribute resources associated with this inode. Traverse
1989 * all entries and unlink any xattr inodes associated with this inode. This
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001990 * is called immediately before an inode is freed. We have exclusive
Andreas Dilgere50e5122017-06-21 21:10:32 -04001991 * access to the inode. If an orphan inode is deleted it will also delete any
1992 * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
1993 * to ensure they belong to the parent inode and were not deleted already.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001995int
1996ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1997 struct ext4_xattr_ino_array **lea_ino_array)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998{
1999 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002000 struct ext4_xattr_ibody_header *header;
2001 struct ext4_inode *raw_inode;
2002 struct ext4_iloc iloc;
2003 struct ext4_xattr_entry *entry;
Tahsin Erdogan990461d2017-06-21 21:59:30 -04002004 unsigned int ea_ino;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002005 int credits = 3, error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006
Andreas Dilgere50e5122017-06-21 21:10:32 -04002007 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
2008 goto delete_external_ea;
2009
2010 error = ext4_get_inode_loc(inode, &iloc);
2011 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002013 raw_inode = ext4_raw_inode(&iloc);
2014 header = IHDR(inode, raw_inode);
2015 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
2016 entry = EXT4_XATTR_NEXT(entry)) {
2017 if (!entry->e_value_inum)
2018 continue;
Tahsin Erdogan990461d2017-06-21 21:59:30 -04002019 ea_ino = le32_to_cpu(entry->e_value_inum);
2020 if (ext4_expand_ino_array(lea_ino_array, ea_ino) != 0) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002021 brelse(iloc.bh);
2022 goto cleanup;
2023 }
2024 entry->e_value_inum = 0;
2025 }
2026 brelse(iloc.bh);
2027
2028delete_external_ea:
2029 if (!EXT4_I(inode)->i_file_acl) {
2030 /* add xattr inode to orphan list */
2031 ext4_xattr_inode_orphan_add(handle, inode, credits,
2032 *lea_ino_array);
2033 goto cleanup;
2034 }
Mingming Cao617ba132006-10-11 01:20:53 -07002035 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002036 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002037 EXT4_ERROR_INODE(inode, "block %llu read error",
2038 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002039 goto cleanup;
2040 }
Mingming Cao617ba132006-10-11 01:20:53 -07002041 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002042 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002043 EXT4_ERROR_INODE(inode, "bad block %llu",
2044 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045 goto cleanup;
2046 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04002047
2048 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2049 entry = EXT4_XATTR_NEXT(entry)) {
2050 if (!entry->e_value_inum)
2051 continue;
Tahsin Erdogan990461d2017-06-21 21:59:30 -04002052 ea_ino = le32_to_cpu(entry->e_value_inum);
2053 if (ext4_expand_ino_array(lea_ino_array, ea_ino) != 0)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002054 goto cleanup;
2055 entry->e_value_inum = 0;
2056 }
2057
2058 /* add xattr inode to orphan list */
2059 error = ext4_xattr_inode_orphan_add(handle, inode, credits,
2060 *lea_ino_array);
2061 if (error != 0)
2062 goto cleanup;
2063
2064 if (!IS_NOQUOTA(inode))
2065 credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
2066
2067 if (!ext4_handle_has_enough_credits(handle, credits)) {
2068 error = ext4_journal_extend(handle, credits);
2069 if (error > 0)
2070 error = ext4_journal_restart(handle, credits);
2071 if (error != 0) {
2072 ext4_warning(inode->i_sb,
2073 "couldn't extend journal (err %d)", error);
2074 goto cleanup;
2075 }
2076 }
2077
Mingming Cao617ba132006-10-11 01:20:53 -07002078 ext4_xattr_release_block(handle, inode, bh);
2079 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002080
2081cleanup:
2082 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002083
2084 return error;
2085}
2086
2087void
2088ext4_xattr_inode_array_free(struct inode *inode,
2089 struct ext4_xattr_ino_array *lea_ino_array)
2090{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04002091 struct inode *ea_inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002092 int idx = 0;
2093 int err;
2094
2095 if (lea_ino_array == NULL)
2096 return;
2097
2098 for (; idx < lea_ino_array->xia_count; ++idx) {
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04002099 err = ext4_xattr_inode_iget(inode,
2100 lea_ino_array->xia_inodes[idx], &ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002101 if (err)
2102 continue;
2103 /* for inode's i_count get from ext4_xattr_delete_inode */
Tahsin Erdogan1e7d3592017-06-21 21:39:38 -04002104 iput(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002105 clear_nlink(ea_inode);
2106 iput(ea_inode);
2107 }
2108 kfree(lea_ino_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109}
2110
2111/*
Mingming Cao617ba132006-10-11 01:20:53 -07002112 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002113 *
2114 * Create a new entry in the extended attribute cache, and insert
2115 * it unless such an entry is already in the cache.
2116 *
2117 * Returns 0, or a negative error number on failure.
2118 */
2119static void
Jan Kara7a2508e2016-02-22 22:35:22 -05002120ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002121{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002122 struct ext4_xattr_header *header = BHDR(bh);
2123 __u32 hash = le32_to_cpu(header->h_hash);
2124 int reusable = le32_to_cpu(header->h_refcount) <
2125 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002126 int error;
2127
Jan Kara7a2508e2016-02-22 22:35:22 -05002128 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002129 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002131 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002132 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002133 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135}
2136
2137/*
Mingming Cao617ba132006-10-11 01:20:53 -07002138 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139 *
2140 * Compare two extended attribute blocks for equality.
2141 *
2142 * Returns 0 if the blocks are equal, 1 if they differ, and
2143 * a negative error number on errors.
2144 */
2145static int
Mingming Cao617ba132006-10-11 01:20:53 -07002146ext4_xattr_cmp(struct ext4_xattr_header *header1,
2147 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148{
Mingming Cao617ba132006-10-11 01:20:53 -07002149 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150
2151 entry1 = ENTRY(header1+1);
2152 entry2 = ENTRY(header2+1);
2153 while (!IS_LAST_ENTRY(entry1)) {
2154 if (IS_LAST_ENTRY(entry2))
2155 return 1;
2156 if (entry1->e_hash != entry2->e_hash ||
2157 entry1->e_name_index != entry2->e_name_index ||
2158 entry1->e_name_len != entry2->e_name_len ||
2159 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002160 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2162 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2164 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2165 le32_to_cpu(entry1->e_value_size)))
2166 return 1;
2167
Mingming Cao617ba132006-10-11 01:20:53 -07002168 entry1 = EXT4_XATTR_NEXT(entry1);
2169 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002170 }
2171 if (!IS_LAST_ENTRY(entry2))
2172 return 1;
2173 return 0;
2174}
2175
2176/*
Mingming Cao617ba132006-10-11 01:20:53 -07002177 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002178 *
2179 * Find an identical extended attribute block.
2180 *
2181 * Returns a pointer to the block found, or NULL if such a block was
2182 * not found or an error occurred.
2183 */
2184static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07002185ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05002186 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002187{
2188 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002189 struct mb_cache_entry *ce;
2190 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191
2192 if (!header->h_hash)
2193 return NULL; /* never share */
2194 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002195 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002196 while (ce) {
2197 struct buffer_head *bh;
2198
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199 bh = sb_bread(inode->i_sb, ce->e_block);
2200 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002201 EXT4_ERROR_INODE(inode, "block %lu read error",
2202 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07002203 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002204 *pce = ce;
2205 return bh;
2206 }
2207 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05002208 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002209 }
2210 return NULL;
2211}
2212
2213#define NAME_HASH_SHIFT 5
2214#define VALUE_HASH_SHIFT 16
2215
2216/*
Mingming Cao617ba132006-10-11 01:20:53 -07002217 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218 *
2219 * Compute the hash of an extended attribute.
2220 */
Mingming Cao617ba132006-10-11 01:20:53 -07002221static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2222 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002223{
2224 __u32 hash = 0;
2225 char *name = entry->e_name;
2226 int n;
2227
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002228 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229 hash = (hash << NAME_HASH_SHIFT) ^
2230 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2231 *name++;
2232 }
2233
Andreas Dilgere50e5122017-06-21 21:10:32 -04002234 if (!entry->e_value_inum && entry->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 __le32 *value = (__le32 *)((char *)header +
2236 le16_to_cpu(entry->e_value_offs));
2237 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07002238 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 hash = (hash << VALUE_HASH_SHIFT) ^
2240 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2241 le32_to_cpu(*value++);
2242 }
2243 }
2244 entry->e_hash = cpu_to_le32(hash);
2245}
2246
2247#undef NAME_HASH_SHIFT
2248#undef VALUE_HASH_SHIFT
2249
2250#define BLOCK_HASH_SHIFT 16
2251
2252/*
Mingming Cao617ba132006-10-11 01:20:53 -07002253 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002254 *
2255 * Re-compute the extended attribute hash value after an entry has changed.
2256 */
Mingming Cao617ba132006-10-11 01:20:53 -07002257static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2258 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002259{
Mingming Cao617ba132006-10-11 01:20:53 -07002260 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261 __u32 hash = 0;
2262
Mingming Cao617ba132006-10-11 01:20:53 -07002263 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264 here = ENTRY(header+1);
2265 while (!IS_LAST_ENTRY(here)) {
2266 if (!here->e_hash) {
2267 /* Block is not shared if an entry's hash value == 0 */
2268 hash = 0;
2269 break;
2270 }
2271 hash = (hash << BLOCK_HASH_SHIFT) ^
2272 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2273 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07002274 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002275 }
2276 header->h_hash = cpu_to_le32(hash);
2277}
2278
2279#undef BLOCK_HASH_SHIFT
2280
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002281#define HASH_BUCKET_BITS 10
2282
Jan Kara7a2508e2016-02-22 22:35:22 -05002283struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05002284ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285{
Jan Kara7a2508e2016-02-22 22:35:22 -05002286 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287}
2288
Jan Kara7a2508e2016-02-22 22:35:22 -05002289void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002290{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002291 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05002292 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002294