blob: f16a90824d4489c7251b7f21a7e3dfacf964ac7a [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/xattr.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
Mingming Cao617ba132006-10-11 01:20:53 -07007 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07008 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11 * Red Hat Inc.
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
14 */
15
16/*
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
24 *
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
27 *
28 * +------------------+
29 * | header |
30 * | entry 1 | |
31 * | entry 2 | | growing downwards
32 * | entry 3 | v
33 * | four null bytes |
34 * | . . . |
35 * | value 1 | ^
36 * | value 3 | | growing upwards
37 * | value 2 | |
38 * +------------------+
39 *
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
43 *
44 * Locking strategy
45 * ----------------
Mingming Cao617ba132006-10-11 01:20:53 -070046 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
50 * by the buffer lock.
51 */
52
53#include <linux/init.h>
54#include <linux/fs.h>
55#include <linux/slab.h>
Jan Kara7a2508e2016-02-22 22:35:22 -050056#include <linux/mbcache.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057#include <linux/quotaops.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040058#include "ext4_jbd2.h"
59#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060#include "xattr.h"
61#include "acl.h"
62
Mingming Cao617ba132006-10-11 01:20:53 -070063#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040064# define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67# define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070#else
Joe Perchesace36ad2012-03-19 23:11:43 -040071# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073#endif
74
Jan Kara7a2508e2016-02-22 22:35:22 -050075static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070076static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
Jan Kara7a2508e2016-02-22 22:35:22 -050078 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070079static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081
Eric Biggersd6006182017-04-29 23:47:50 -040082static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070083 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040084#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080085 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
86 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087#endif
Mingming Cao617ba132006-10-11 01:20:53 -070088 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040089#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070090 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091#endif
92};
93
Stephen Hemminger11e27522010-05-13 17:53:18 -070094const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070095 &ext4_xattr_user_handler,
96 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040097#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080098 &posix_acl_access_xattr_handler,
99 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400101#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700102 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103#endif
104 NULL
105};
106
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400107#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
108 inode->i_sb->s_fs_info)->s_mb_cache)
109
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400110#ifdef CONFIG_LOCKDEP
111void ext4_xattr_inode_set_class(struct inode *ea_inode)
112{
113 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
114}
115#endif
116
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400117static __le32 ext4_xattr_block_csum(struct inode *inode,
118 sector_t block_nr,
119 struct ext4_xattr_header *hdr)
120{
121 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400122 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400123 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400124 __u32 dummy_csum = 0;
125 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400126
Theodore Ts'od6a77102013-04-09 23:59:55 -0400127 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
128 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400129 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
130 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
131 offset += sizeof(dummy_csum);
132 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
133 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400134
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135 return cpu_to_le32(csum);
136}
137
138static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400139 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400140{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400141 struct ext4_xattr_header *hdr = BHDR(bh);
142 int ret = 1;
143
144 if (ext4_has_metadata_csum(inode->i_sb)) {
145 lock_buffer(bh);
146 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
147 bh->b_blocknr, hdr));
148 unlock_buffer(bh);
149 }
150 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400151}
152
153static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400154 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400155{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400156 if (ext4_has_metadata_csum(inode->i_sb))
157 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
158 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400159}
160
Stephen Hemminger11e27522010-05-13 17:53:18 -0700161static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700162ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700164 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165
Mingming Cao617ba132006-10-11 01:20:53 -0700166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 return handler;
169}
170
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400172ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
173 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400175 struct ext4_xattr_entry *e = entry;
176
Eric Biggersd7614cc2016-12-01 14:57:29 -0500177 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400178 while (!IS_LAST_ENTRY(e)) {
179 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400181 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400182 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400184
Eric Biggersd7614cc2016-12-01 14:57:29 -0500185 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400186 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400187 if (entry->e_value_size != 0 &&
188 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500189 u16 offs = le16_to_cpu(entry->e_value_offs);
190 u32 size = le32_to_cpu(entry->e_value_size);
191 void *value;
192
193 /*
194 * The value cannot overlap the names, and the value
195 * with padding cannot extend beyond 'end'. Check both
196 * the padded and unpadded sizes, since the size may
197 * overflow to 0 when adding padding.
198 */
199 if (offs > end - value_start)
200 return -EFSCORRUPTED;
201 value = value_start + offs;
202 if (value < (void *)e + sizeof(u32) ||
203 size > end - value ||
204 EXT4_XATTR_SIZE(size) > end - value)
205 return -EFSCORRUPTED;
206 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400207 entry = EXT4_XATTR_NEXT(entry);
208 }
209
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 return 0;
211}
212
213static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400214ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400216 int error;
217
218 if (buffer_verified(bh))
219 return 0;
220
Mingming Cao617ba132006-10-11 01:20:53 -0700221 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400223 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400224 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400225 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400226 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
227 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400228 if (!error)
229 set_buffer_verified(bh);
230 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231}
232
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400233static int
234__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
235 void *end, const char *function, unsigned int line)
236{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400237 int error = -EFSCORRUPTED;
238
Eric Biggers290ab232016-12-01 14:51:58 -0500239 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400240 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400241 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400242 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400243errout:
244 if (error)
245 __ext4_error_inode(inode, function, line, 0,
246 "corrupted in-inode xattr");
247 return error;
248}
249
250#define xattr_check_inode(inode, header, end) \
251 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
252
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253static int
Mingming Cao617ba132006-10-11 01:20:53 -0700254ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400255 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256{
Mingming Cao617ba132006-10-11 01:20:53 -0700257 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258 size_t name_len;
259 int cmp = 1;
260
261 if (name == NULL)
262 return -EINVAL;
263 name_len = strlen(name);
264 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700265 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 cmp = name_index - entry->e_name_index;
267 if (!cmp)
268 cmp = name_len - entry->e_name_len;
269 if (!cmp)
270 cmp = memcmp(name, entry->e_name, name_len);
271 if (cmp <= 0 && (sorted || cmp == 0))
272 break;
273 }
274 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 return cmp ? -ENODATA : 0;
276}
277
Andreas Dilgere50e5122017-06-21 21:10:32 -0400278/*
279 * Read the EA value from an inode.
280 */
281static int
282ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
283{
284 unsigned long block = 0;
285 struct buffer_head *bh = NULL;
286 int blocksize;
287 size_t csize, ret_size = 0;
288
289 if (*size == 0)
290 return 0;
291
292 blocksize = ea_inode->i_sb->s_blocksize;
293
294 while (ret_size < *size) {
295 csize = (*size - ret_size) > blocksize ? blocksize :
296 *size - ret_size;
297 bh = ext4_bread(NULL, ea_inode, block, 0);
298 if (IS_ERR(bh)) {
299 *size = ret_size;
300 return PTR_ERR(bh);
301 }
302 memcpy(buf, bh->b_data, csize);
303 brelse(bh);
304
305 buf += csize;
306 block += 1;
307 ret_size += csize;
308 }
309
310 *size = ret_size;
311
312 return 0;
313}
314
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400315static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
316 struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400317{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400318 struct inode *inode;
319 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400320
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400321 inode = ext4_iget(parent->i_sb, ea_ino);
322 if (IS_ERR(inode)) {
323 err = PTR_ERR(inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400324 ext4_error(parent->i_sb, "error while reading EA inode %lu "
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400325 "err=%d", ea_ino, err);
326 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400327 }
328
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400329 if (is_bad_inode(inode)) {
330 ext4_error(parent->i_sb, "error while reading EA inode %lu "
331 "is_bad_inode", ea_ino);
332 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400333 goto error;
334 }
335
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400336 if (EXT4_XATTR_INODE_GET_PARENT(inode) != parent->i_ino ||
337 inode->i_generation != parent->i_generation) {
338 ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
339 "to parent is invalid.", ea_ino);
340 err = -EINVAL;
341 goto error;
342 }
343
344 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400345 ext4_error(parent->i_sb, "EA inode %lu does not have "
346 "EXT4_EA_INODE_FL flag set.\n", ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400347 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400348 goto error;
349 }
350
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400351 *ea_inode = inode;
352 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400353error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400354 iput(inode);
355 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400356}
357
358/*
359 * Read the value from the EA inode.
360 */
361static int
362ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
363 size_t *size)
364{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400365 struct inode *ea_inode;
366 int ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400367
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400368 ret = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
369 if (ret)
370 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400371
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400372 ret = ext4_xattr_inode_read(ea_inode, buffer, size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400373 iput(ea_inode);
374
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400375 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400376}
377
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378static int
Mingming Cao617ba132006-10-11 01:20:53 -0700379ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 void *buffer, size_t buffer_size)
381{
382 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700383 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384 size_t size;
385 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500386 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700387
388 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
389 name_index, name, buffer, (long)buffer_size);
390
391 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700392 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700393 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400394 ea_idebug(inode, "reading block %llu",
395 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700396 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700397 if (!bh)
398 goto cleanup;
399 ea_bdebug(bh, "b_count=%d, refcount=%d",
400 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400401 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400402 EXT4_ERROR_INODE(inode, "bad block %llu",
403 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400404 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 goto cleanup;
406 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400407 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400409 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 if (error)
411 goto cleanup;
412 size = le32_to_cpu(entry->e_value_size);
413 if (buffer) {
414 error = -ERANGE;
415 if (size > buffer_size)
416 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400417 if (entry->e_value_inum) {
418 error = ext4_xattr_inode_get(inode,
419 le32_to_cpu(entry->e_value_inum),
420 buffer, &size);
421 if (error)
422 goto cleanup;
423 } else {
424 memcpy(buffer, bh->b_data +
425 le16_to_cpu(entry->e_value_offs), size);
426 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427 }
428 error = size;
429
430cleanup:
431 brelse(bh);
432 return error;
433}
434
Tao Ma879b3822012-12-05 10:28:46 -0500435int
Mingming Cao617ba132006-10-11 01:20:53 -0700436ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437 void *buffer, size_t buffer_size)
438{
Mingming Cao617ba132006-10-11 01:20:53 -0700439 struct ext4_xattr_ibody_header *header;
440 struct ext4_xattr_entry *entry;
441 struct ext4_inode *raw_inode;
442 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 size_t size;
444 void *end;
445 int error;
446
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500447 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700448 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700449 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 if (error)
451 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700452 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700454 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400455 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 if (error)
457 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400458 entry = IFIRST(header);
459 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 if (error)
461 goto cleanup;
462 size = le32_to_cpu(entry->e_value_size);
463 if (buffer) {
464 error = -ERANGE;
465 if (size > buffer_size)
466 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400467 if (entry->e_value_inum) {
468 error = ext4_xattr_inode_get(inode,
469 le32_to_cpu(entry->e_value_inum),
470 buffer, &size);
471 if (error)
472 goto cleanup;
473 } else {
474 memcpy(buffer, (void *)IFIRST(header) +
475 le16_to_cpu(entry->e_value_offs), size);
476 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477 }
478 error = size;
479
480cleanup:
481 brelse(iloc.bh);
482 return error;
483}
484
485/*
Mingming Cao617ba132006-10-11 01:20:53 -0700486 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700487 *
488 * Copy an extended attribute into the buffer
489 * provided, or compute the buffer size required.
490 * Buffer is NULL to compute the size of the buffer required.
491 *
492 * Returns a negative error number on failure, or the number of bytes
493 * used / required on success.
494 */
495int
Mingming Cao617ba132006-10-11 01:20:53 -0700496ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 void *buffer, size_t buffer_size)
498{
499 int error;
500
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500501 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
502 return -EIO;
503
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400504 if (strlen(name) > 255)
505 return -ERANGE;
506
Mingming Cao617ba132006-10-11 01:20:53 -0700507 down_read(&EXT4_I(inode)->xattr_sem);
508 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 buffer_size);
510 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700511 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700513 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 return error;
515}
516
517static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000518ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519 char *buffer, size_t buffer_size)
520{
521 size_t rest = buffer_size;
522
Mingming Cao617ba132006-10-11 01:20:53 -0700523 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700524 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700525 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100527 if (handler && (!handler->list || handler->list(dentry))) {
528 const char *prefix = handler->prefix ?: handler->name;
529 size_t prefix_len = strlen(prefix);
530 size_t size = prefix_len + entry->e_name_len + 1;
531
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 if (buffer) {
533 if (size > rest)
534 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100535 memcpy(buffer, prefix, prefix_len);
536 buffer += prefix_len;
537 memcpy(buffer, entry->e_name, entry->e_name_len);
538 buffer += entry->e_name_len;
539 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540 }
541 rest -= size;
542 }
543 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100544 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545}
546
547static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000548ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
David Howells2b0143b2015-03-17 22:25:59 +0000550 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 struct buffer_head *bh = NULL;
552 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500553 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554
555 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
556 buffer, (long)buffer_size);
557
558 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700559 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400561 ea_idebug(inode, "reading block %llu",
562 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700563 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 error = -EIO;
565 if (!bh)
566 goto cleanup;
567 ea_bdebug(bh, "b_count=%d, refcount=%d",
568 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400569 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400570 EXT4_ERROR_INODE(inode, "bad block %llu",
571 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400572 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573 goto cleanup;
574 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400575 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000576 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577
578cleanup:
579 brelse(bh);
580
581 return error;
582}
583
584static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000585ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586{
David Howells2b0143b2015-03-17 22:25:59 +0000587 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700588 struct ext4_xattr_ibody_header *header;
589 struct ext4_inode *raw_inode;
590 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 void *end;
592 int error;
593
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500594 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700595 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700596 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 if (error)
598 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700599 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700601 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400602 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 if (error)
604 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000605 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 buffer, buffer_size);
607
608cleanup:
609 brelse(iloc.bh);
610 return error;
611}
612
613/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400614 * Inode operation listxattr()
615 *
616 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617 *
618 * Copy a list of attribute names into the buffer
619 * provided, or compute the buffer size required.
620 * Buffer is NULL to compute the size of the buffer required.
621 *
622 * Returns a negative error number on failure, or the number of bytes
623 * used / required on success.
624 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400625ssize_t
626ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500628 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629
David Howells2b0143b2015-03-17 22:25:59 +0000630 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500631 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
632 if (ret < 0)
633 goto errout;
634 if (buffer) {
635 buffer += ret;
636 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500638 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
639 if (ret < 0)
640 goto errout;
641 ret += ret2;
642errout:
David Howells2b0143b2015-03-17 22:25:59 +0000643 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500644 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645}
646
647/*
Mingming Cao617ba132006-10-11 01:20:53 -0700648 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 * not set, set it.
650 */
Mingming Cao617ba132006-10-11 01:20:53 -0700651static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652 struct super_block *sb)
653{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400654 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 return;
656
liang xie5d601252014-05-12 22:06:43 -0400657 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700658 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400659 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400660 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662}
663
664/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400665 * Release the xattr block BH: If the reference count is > 1, decrement it;
666 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 */
668static void
Mingming Cao617ba132006-10-11 01:20:53 -0700669ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 struct buffer_head *bh)
671{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500672 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
673 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800674 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675
liang xie5d601252014-05-12 22:06:43 -0400676 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800677 error = ext4_journal_get_write_access(handle, bh);
678 if (error)
679 goto out;
680
681 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500682 hash = le32_to_cpu(BHDR(bh)->h_hash);
683 ref = le32_to_cpu(BHDR(bh)->h_refcount);
684 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500686 /*
687 * This must happen under buffer lock for
688 * ext4_xattr_block_set() to reliably detect freed block
689 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500690 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400692 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500693 ext4_free_blocks(handle, inode, bh, 0, 1,
694 EXT4_FREE_BLOCKS_METADATA |
695 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500697 ref--;
698 BHDR(bh)->h_refcount = cpu_to_le32(ref);
699 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
700 struct mb_cache_entry *ce;
701
702 ce = mb_cache_entry_get(ext4_mb_cache, hash,
703 bh->b_blocknr);
704 if (ce) {
705 ce->e_reusable = 1;
706 mb_cache_entry_put(ext4_mb_cache, ce);
707 }
708 }
709
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400710 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400711 /*
712 * Beware of this ugliness: Releasing of xattr block references
713 * from different inodes can race and so we have to protect
714 * from a race where someone else frees the block (and releases
715 * its journal_head) before we are done dirtying the buffer. In
716 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400717 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400718 * that function can call sync_dirty_buffer() so for that case
719 * we handle the dirtying after unlocking the buffer.
720 */
721 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400722 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500723 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400724 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400725 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800726 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500727 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500728 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800729 ea_bdebug(bh, "refcount now=%d; releasing",
730 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700731 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800732out:
733 ext4_std_error(inode->i_sb, error);
734 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735}
736
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400737/*
738 * Find the available free space for EAs. This also returns the total number of
739 * bytes used by EA entries.
740 */
741static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
742 size_t *min_offs, void *base, int *total)
743{
744 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400745 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400746 size_t offs = le16_to_cpu(last->e_value_offs);
747 if (offs < *min_offs)
748 *min_offs = offs;
749 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500750 if (total)
751 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400752 }
753 return (*min_offs - ((void *)last - base) - sizeof(__u32));
754}
755
Andreas Dilgere50e5122017-06-21 21:10:32 -0400756/*
757 * Write the value of the EA in an inode.
758 */
759static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
760 const void *buf, int bufsize)
761{
762 struct buffer_head *bh = NULL;
763 unsigned long block = 0;
764 unsigned blocksize = ea_inode->i_sb->s_blocksize;
765 unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
766 int csize, wsize = 0;
767 int ret = 0;
768 int retries = 0;
769
770retry:
771 while (ret >= 0 && ret < max_blocks) {
772 struct ext4_map_blocks map;
773 map.m_lblk = block += ret;
774 map.m_len = max_blocks -= ret;
775
776 ret = ext4_map_blocks(handle, ea_inode, &map,
777 EXT4_GET_BLOCKS_CREATE);
778 if (ret <= 0) {
779 ext4_mark_inode_dirty(handle, ea_inode);
780 if (ret == -ENOSPC &&
781 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
782 ret = 0;
783 goto retry;
784 }
785 break;
786 }
787 }
788
789 if (ret < 0)
790 return ret;
791
792 block = 0;
793 while (wsize < bufsize) {
794 if (bh != NULL)
795 brelse(bh);
796 csize = (bufsize - wsize) > blocksize ? blocksize :
797 bufsize - wsize;
798 bh = ext4_getblk(handle, ea_inode, block, 0);
799 if (IS_ERR(bh))
800 return PTR_ERR(bh);
801 ret = ext4_journal_get_write_access(handle, bh);
802 if (ret)
803 goto out;
804
805 memcpy(bh->b_data, buf, csize);
806 set_buffer_uptodate(bh);
807 ext4_handle_dirty_metadata(handle, ea_inode, bh);
808
809 buf += csize;
810 wsize += csize;
811 block += 1;
812 }
813
814 inode_lock(ea_inode);
815 i_size_write(ea_inode, wsize);
816 ext4_update_i_disksize(ea_inode, wsize);
817 inode_unlock(ea_inode);
818
819 ext4_mark_inode_dirty(handle, ea_inode);
820
821out:
822 brelse(bh);
823
824 return ret;
825}
826
827/*
828 * Create an inode to store the value of a large EA.
829 */
830static struct inode *ext4_xattr_inode_create(handle_t *handle,
831 struct inode *inode)
832{
833 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -0400834 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400835 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400836
837 /*
838 * Let the next inode be the goal, so we try and allocate the EA inode
839 * in the same group, or nearby one.
840 */
841 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -0400842 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -0400843 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400844 if (!IS_ERR(ea_inode)) {
845 ea_inode->i_op = &ext4_file_inode_operations;
846 ea_inode->i_fop = &ext4_file_operations;
847 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400848 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400849 ea_inode->i_generation = inode->i_generation;
850 EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
851
852 /*
853 * A back-pointer from EA inode to parent inode will be useful
854 * for e2fsck.
855 */
856 EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
857 unlock_new_inode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -0400858 err = ext4_inode_attach_jinode(ea_inode);
859 if (err) {
860 iput(ea_inode);
861 return ERR_PTR(err);
862 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400863 }
864
865 return ea_inode;
866}
867
868/*
869 * Unlink the inode storing the value of the EA.
870 */
871int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
872{
873 struct inode *ea_inode = NULL;
874 int err;
875
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400876 err = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400877 if (err)
878 return err;
879
880 clear_nlink(ea_inode);
881 iput(ea_inode);
882
883 return 0;
884}
885
886/*
887 * Add value of the EA in an inode.
888 */
889static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
890 unsigned long *ea_ino, const void *value,
891 size_t value_len)
892{
893 struct inode *ea_inode;
894 int err;
895
896 /* Create an inode for the EA value */
897 ea_inode = ext4_xattr_inode_create(handle, inode);
898 if (IS_ERR(ea_inode))
899 return PTR_ERR(ea_inode);
900
901 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
902 if (err)
903 clear_nlink(ea_inode);
904 else
905 *ea_ino = ea_inode->i_ino;
906
907 iput(ea_inode);
908
909 return err;
910}
911
912static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
913 struct ext4_xattr_search *s,
914 handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915{
Mingming Cao617ba132006-10-11 01:20:53 -0700916 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400918 int in_inode = i->in_inode;
919 int rc;
920
921 if (ext4_has_feature_ea_inode(inode->i_sb) &&
922 (EXT4_XATTR_SIZE(i->value_len) >
923 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
924 in_inode = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925
926 /* Compute min_offs and last. */
927 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700928 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400929 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 size_t offs = le16_to_cpu(last->e_value_offs);
931 if (offs < min_offs)
932 min_offs = offs;
933 }
934 }
935 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
936 if (!s->not_found) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400937 if (!in_inode &&
938 !s->here->e_value_inum && s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700939 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700940 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941 }
Mingming Cao617ba132006-10-11 01:20:53 -0700942 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 }
944 if (i->value) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400945 size_t value_len = EXT4_XATTR_SIZE(i->value_len);
946
947 if (in_inode)
948 value_len = 0;
949
950 if (free < EXT4_XATTR_LEN(name_len) + value_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 return -ENOSPC;
952 }
953
954 if (i->value && s->not_found) {
955 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700956 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
958 memmove((void *)s->here + size, s->here, rest);
959 memset(s->here, 0, size);
960 s->here->e_name_index = i->name_index;
961 s->here->e_name_len = name_len;
962 memcpy(s->here->e_name, i->name, name_len);
963 } else {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400964 if (!s->here->e_value_inum && s->here->e_value_size &&
965 s->here->e_value_offs > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 void *first_val = s->base + min_offs;
967 size_t offs = le16_to_cpu(s->here->e_value_offs);
968 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700969 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 le32_to_cpu(s->here->e_value_size));
971
Mingming Cao617ba132006-10-11 01:20:53 -0700972 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 /* The old and the new value have the same
974 size. Just replace. */
975 s->here->e_value_size =
976 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500977 if (i->value == EXT4_ZERO_XATTR_VALUE) {
978 memset(val, 0, size);
979 } else {
980 /* Clear pad bytes first. */
981 memset(val + size - EXT4_XATTR_PAD, 0,
982 EXT4_XATTR_PAD);
983 memcpy(val, i->value, i->value_len);
984 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 return 0;
986 }
987
988 /* Remove the old value. */
989 memmove(first_val + size, first_val, val - first_val);
990 memset(first_val, 0, size);
991 s->here->e_value_size = 0;
992 s->here->e_value_offs = 0;
993 min_offs += size;
994
995 /* Adjust all value offsets. */
996 last = s->first;
997 while (!IS_LAST_ENTRY(last)) {
998 size_t o = le16_to_cpu(last->e_value_offs);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400999 if (!last->e_value_inum &&
1000 last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 last->e_value_offs =
1002 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -07001003 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 }
1005 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001006 if (s->here->e_value_inum) {
1007 ext4_xattr_inode_unlink(inode,
1008 le32_to_cpu(s->here->e_value_inum));
1009 s->here->e_value_inum = 0;
1010 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011 if (!i->value) {
1012 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001013 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 last = ENTRY((void *)last - size);
1015 memmove(s->here, (void *)s->here + size,
1016 (void *)last - (void *)s->here + sizeof(__u32));
1017 memset(last, 0, size);
1018 }
1019 }
1020
1021 if (i->value) {
1022 /* Insert the new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001023 if (in_inode) {
1024 unsigned long ea_ino =
1025 le32_to_cpu(s->here->e_value_inum);
1026 rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
1027 i->value, i->value_len);
1028 if (rc)
1029 goto out;
1030 s->here->e_value_inum = cpu_to_le32(ea_ino);
1031 s->here->e_value_offs = 0;
1032 } else if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001033 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 void *val = s->base + min_offs - size;
1035 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001036 s->here->e_value_inum = 0;
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001037 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1038 memset(val, 0, size);
1039 } else {
1040 /* Clear the pad bytes first. */
1041 memset(val + size - EXT4_XATTR_PAD, 0,
1042 EXT4_XATTR_PAD);
1043 memcpy(val, i->value, i->value_len);
1044 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001046 s->here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001048
1049out:
1050 return rc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051}
1052
Mingming Cao617ba132006-10-11 01:20:53 -07001053struct ext4_xattr_block_find {
1054 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 struct buffer_head *bh;
1056};
1057
1058static int
Mingming Cao617ba132006-10-11 01:20:53 -07001059ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1060 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061{
1062 struct super_block *sb = inode->i_sb;
1063 int error;
1064
1065 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1066 i->name_index, i->name, i->value, (long)i->value_len);
1067
Mingming Cao617ba132006-10-11 01:20:53 -07001068 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001070 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 error = -EIO;
1072 if (!bs->bh)
1073 goto cleanup;
1074 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1075 atomic_read(&(bs->bh->b_count)),
1076 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001077 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001078 EXT4_ERROR_INODE(inode, "bad block %llu",
1079 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001080 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081 goto cleanup;
1082 }
1083 /* Find the named attribute. */
1084 bs->s.base = BHDR(bs->bh);
1085 bs->s.first = BFIRST(bs->bh);
1086 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1087 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001088 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001089 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090 if (error && error != -ENODATA)
1091 goto cleanup;
1092 bs->s.not_found = error;
1093 }
1094 error = 0;
1095
1096cleanup:
1097 return error;
1098}
1099
1100static int
Mingming Cao617ba132006-10-11 01:20:53 -07001101ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1102 struct ext4_xattr_info *i,
1103 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104{
1105 struct super_block *sb = inode->i_sb;
1106 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001107 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -05001108 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001109 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -05001110 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111
Mingming Cao617ba132006-10-11 01:20:53 -07001112#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001115 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001116 error = ext4_journal_get_write_access(handle, bs->bh);
1117 if (error)
1118 goto cleanup;
1119 lock_buffer(bs->bh);
1120
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001122 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1123
1124 /*
1125 * This must happen under buffer lock for
1126 * ext4_xattr_block_set() to reliably detect modified
1127 * block
1128 */
Jan Kara7a2508e2016-02-22 22:35:22 -05001129 mb_cache_entry_delete_block(ext4_mb_cache, hash,
1130 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 ea_bdebug(bs->bh, "modifying in-place");
Andreas Dilgere50e5122017-06-21 21:10:32 -04001132 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 if (!error) {
1134 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001135 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001137 ext4_xattr_cache_insert(ext4_mb_cache,
1138 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 }
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001140 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001142 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 goto bad_block;
1144 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001145 error = ext4_handle_dirty_metadata(handle,
1146 inode,
1147 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 if (error)
1149 goto cleanup;
1150 goto inserted;
1151 } else {
1152 int offset = (char *)s->here - bs->bh->b_data;
1153
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001154 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001156 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157 error = -ENOMEM;
1158 if (s->base == NULL)
1159 goto cleanup;
1160 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1161 s->first = ENTRY(header(s->base)+1);
1162 header(s->base)->h_refcount = cpu_to_le32(1);
1163 s->here = ENTRY(s->base + offset);
1164 s->end = s->base + bs->bh->b_size;
1165 }
1166 } else {
1167 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001168 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169 /* assert(header == s->base) */
1170 error = -ENOMEM;
1171 if (s->base == NULL)
1172 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001173 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 header(s->base)->h_blocks = cpu_to_le32(1);
1175 header(s->base)->h_refcount = cpu_to_le32(1);
1176 s->first = ENTRY(header(s->base)+1);
1177 s->here = ENTRY(header(s->base)+1);
1178 s->end = s->base + sb->s_blocksize;
1179 }
1180
Andreas Dilgere50e5122017-06-21 21:10:32 -04001181 error = ext4_xattr_set_entry(i, s, handle, inode);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001182 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 goto bad_block;
1184 if (error)
1185 goto cleanup;
1186 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001187 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188
1189inserted:
1190 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001191 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 if (new_bh) {
1193 /* We found an identical block in the cache. */
1194 if (new_bh == bs->bh)
1195 ea_bdebug(new_bh, "keeping");
1196 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001197 u32 ref;
1198
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001199 WARN_ON_ONCE(dquot_initialize_needed(inode));
1200
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 /* The old block is released after updating
1202 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001203 error = dquot_alloc_block(inode,
1204 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001205 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001207 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001208 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 new_bh);
1210 if (error)
1211 goto cleanup_dquot;
1212 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001213 /*
1214 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001215 * freeing, rehashing or adding references to
1216 * xattr block. Once we hold buffer lock xattr
1217 * block's state is stable so we can check
1218 * whether the block got freed / rehashed or
1219 * not. Since we unhash mbcache entry under
1220 * buffer lock when freeing / rehashing xattr
1221 * block, checking whether entry is still
1222 * hashed is reliable. Same rules hold for
1223 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001224 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001225 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1226 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001227 /*
1228 * Undo everything and check mbcache
1229 * again.
1230 */
1231 unlock_buffer(new_bh);
1232 dquot_free_block(inode,
1233 EXT4_C2B(EXT4_SB(sb),
1234 1));
1235 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001236 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001237 ce = NULL;
1238 new_bh = NULL;
1239 goto inserted;
1240 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001241 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1242 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1243 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1244 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001246 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001247 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001248 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001249 error = ext4_handle_dirty_metadata(handle,
1250 inode,
1251 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252 if (error)
1253 goto cleanup_dquot;
1254 }
Jan Kara7a2508e2016-02-22 22:35:22 -05001255 mb_cache_entry_touch(ext4_mb_cache, ce);
1256 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 ce = NULL;
1258 } else if (bs->bh && s->base == bs->bh->b_data) {
1259 /* We were modifying this block in-place. */
1260 ea_bdebug(bs->bh, "keeping this block");
1261 new_bh = bs->bh;
1262 get_bh(new_bh);
1263 } else {
1264 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001265 ext4_fsblk_t goal, block;
1266
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001267 WARN_ON_ONCE(dquot_initialize_needed(inode));
1268
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001269 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001270 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001271
1272 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001273 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001274 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1275
Allison Henderson55f020d2011-05-25 07:41:26 -04001276 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1277 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 if (error)
1279 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001280
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001281 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001282 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1283
Joe Perchesace36ad2012-03-19 23:11:43 -04001284 ea_idebug(inode, "creating block %llu",
1285 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286
1287 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001288 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001289 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001291 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001292 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 goto cleanup;
1294 }
1295 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001296 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 if (error) {
1298 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001299 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 goto getblk_failed;
1301 }
1302 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001303 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001304 set_buffer_uptodate(new_bh);
1305 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001306 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001307 error = ext4_handle_dirty_metadata(handle, inode,
1308 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309 if (error)
1310 goto cleanup;
1311 }
1312 }
1313
1314 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001315 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316
1317 /* Drop the previous xattr block. */
1318 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001319 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320 error = 0;
1321
1322cleanup:
1323 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001324 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001325 brelse(new_bh);
1326 if (!(bs->bh && s->base == bs->bh->b_data))
1327 kfree(s->base);
1328
1329 return error;
1330
1331cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001332 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001333 goto cleanup;
1334
1335bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001336 EXT4_ERROR_INODE(inode, "bad block %llu",
1337 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001338 goto cleanup;
1339
1340#undef header
1341}
1342
Tao Ma879b3822012-12-05 10:28:46 -05001343int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1344 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345{
Mingming Cao617ba132006-10-11 01:20:53 -07001346 struct ext4_xattr_ibody_header *header;
1347 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 int error;
1349
Mingming Cao617ba132006-10-11 01:20:53 -07001350 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001352 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001353 header = IHDR(inode, raw_inode);
1354 is->s.base = is->s.first = IFIRST(header);
1355 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001356 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001357 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001358 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001359 if (error)
1360 return error;
1361 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001362 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001363 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 if (error && error != -ENODATA)
1365 return error;
1366 is->s.not_found = error;
1367 }
1368 return 0;
1369}
1370
Tao Ma0d812f72012-12-10 14:06:02 -05001371int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1372 struct ext4_xattr_info *i,
1373 struct ext4_xattr_ibody_find *is)
1374{
1375 struct ext4_xattr_ibody_header *header;
1376 struct ext4_xattr_search *s = &is->s;
1377 int error;
1378
1379 if (EXT4_I(inode)->i_extra_isize == 0)
1380 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001381 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001382 if (error) {
1383 if (error == -ENOSPC &&
1384 ext4_has_inline_data(inode)) {
1385 error = ext4_try_to_evict_inline_data(handle, inode,
1386 EXT4_XATTR_LEN(strlen(i->name) +
1387 EXT4_XATTR_SIZE(i->value_len)));
1388 if (error)
1389 return error;
1390 error = ext4_xattr_ibody_find(inode, i, is);
1391 if (error)
1392 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001393 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001394 }
1395 if (error)
1396 return error;
1397 }
1398 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1399 if (!IS_LAST_ENTRY(s->first)) {
1400 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1401 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1402 } else {
1403 header->h_magic = cpu_to_le32(0);
1404 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1405 }
1406 return 0;
1407}
1408
Andreas Dilgere50e5122017-06-21 21:10:32 -04001409static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001410 struct ext4_xattr_info *i,
1411 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001412{
Mingming Cao617ba132006-10-11 01:20:53 -07001413 struct ext4_xattr_ibody_header *header;
1414 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 int error;
1416
Mingming Cao617ba132006-10-11 01:20:53 -07001417 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001419 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 if (error)
1421 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001422 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001424 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001425 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001426 } else {
1427 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001428 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429 }
1430 return 0;
1431}
1432
Jan Kara3fd16462016-02-22 22:43:04 -05001433static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1434 struct ext4_xattr_info *i)
1435{
1436 void *value;
1437
1438 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1439 return 0;
1440 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1441 return !memcmp(value, i->value, i->value_len);
1442}
1443
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444/*
Mingming Cao617ba132006-10-11 01:20:53 -07001445 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001447 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001448 * is NULL to remove an existing extended attribute, and non-NULL to
1449 * either replace an existing extended attribute, or create a new extended
1450 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1451 * specify that an extended attribute must exist and must not exist
1452 * previous to the call, respectively.
1453 *
1454 * Returns 0, or a negative error number on failure.
1455 */
1456int
Mingming Cao617ba132006-10-11 01:20:53 -07001457ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458 const char *name, const void *value, size_t value_len,
1459 int flags)
1460{
Mingming Cao617ba132006-10-11 01:20:53 -07001461 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 .name_index = name_index,
1463 .name = name,
1464 .value = value,
1465 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04001466 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467 };
Mingming Cao617ba132006-10-11 01:20:53 -07001468 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 .s = { .not_found = -ENODATA, },
1470 };
Mingming Cao617ba132006-10-11 01:20:53 -07001471 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001472 .s = { .not_found = -ENODATA, },
1473 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001474 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001475 int error;
1476
1477 if (!name)
1478 return -EINVAL;
1479 if (strlen(name) > 255)
1480 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001481
Theodore Ts'oc755e252017-01-11 21:50:46 -05001482 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001483
Eric Sandeen66543612011-10-26 03:32:07 -04001484 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001485 if (error)
1486 goto cleanup;
1487
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001488 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001489 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1490 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001491 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 }
1493
Mingming Cao617ba132006-10-11 01:20:53 -07001494 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001495 if (error)
1496 goto cleanup;
1497 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001498 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001499 if (error)
1500 goto cleanup;
1501 if (is.s.not_found && bs.s.not_found) {
1502 error = -ENODATA;
1503 if (flags & XATTR_REPLACE)
1504 goto cleanup;
1505 error = 0;
1506 if (!value)
1507 goto cleanup;
1508 } else {
1509 error = -EEXIST;
1510 if (flags & XATTR_CREATE)
1511 goto cleanup;
1512 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513 if (!value) {
1514 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001515 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001517 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001519 error = 0;
1520 /* Xattr value did not change? Save us some work and bail out */
1521 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1522 goto cleanup;
1523 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1524 goto cleanup;
1525
Andreas Dilgere50e5122017-06-21 21:10:32 -04001526 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001527 if (!error && !bs.s.not_found) {
1528 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001529 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001531 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1532 error = ext4_xattr_block_find(inode, &i, &bs);
1533 if (error)
1534 goto cleanup;
1535 }
Mingming Cao617ba132006-10-11 01:20:53 -07001536 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001537 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1538 error == -ENOSPC) {
1539 /* xattr not fit to block, store at external
1540 * inode */
1541 i.in_inode = 1;
1542 error = ext4_xattr_ibody_set(handle, inode,
1543 &i, &is);
1544 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545 if (error)
1546 goto cleanup;
1547 if (!is.s.not_found) {
1548 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001549 error = ext4_xattr_ibody_set(handle, inode, &i,
1550 &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 }
1552 }
1553 }
1554 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001555 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001556 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001557 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001558 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001559 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001561 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562 * error != 0.
1563 */
1564 is.iloc.bh = NULL;
1565 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001566 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 }
1568
1569cleanup:
1570 brelse(is.iloc.bh);
1571 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001572 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 return error;
1574}
1575
1576/*
Mingming Cao617ba132006-10-11 01:20:53 -07001577 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 *
Mingming Cao617ba132006-10-11 01:20:53 -07001579 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 * attribute modification is a filesystem transaction by itself.
1581 *
1582 * Returns 0, or a negative error number on failure.
1583 */
1584int
Mingming Cao617ba132006-10-11 01:20:53 -07001585ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586 const void *value, size_t value_len, int flags)
1587{
1588 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001589 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001590 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001591 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001593 error = dquot_initialize(inode);
1594 if (error)
1595 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001596
1597 if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
1598 ext4_has_feature_ea_inode(sb)) {
1599 int nrblocks = (value_len + sb->s_blocksize - 1) >>
1600 sb->s_blocksize_bits;
1601
1602 /* For new inode */
1603 credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
1604
1605 /* For data blocks of EA inode */
1606 credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
1607 }
1608
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001610 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 if (IS_ERR(handle)) {
1612 error = PTR_ERR(handle);
1613 } else {
1614 int error2;
1615
Mingming Cao617ba132006-10-11 01:20:53 -07001616 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001617 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001618 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04001620 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621 goto retry;
1622 if (error == 0)
1623 error = error2;
1624 }
1625
1626 return error;
1627}
1628
1629/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001630 * Shift the EA entries in the inode to create space for the increased
1631 * i_extra_isize.
1632 */
1633static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1634 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001635 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001636{
1637 struct ext4_xattr_entry *last = entry;
1638 int new_offs;
1639
Jan Kara94405712016-08-29 15:41:11 -04001640 /* We always shift xattr headers further thus offsets get lower */
1641 BUG_ON(value_offs_shift > 0);
1642
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001643 /* Adjust the value offsets of the entries */
1644 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001645 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001646 new_offs = le16_to_cpu(last->e_value_offs) +
1647 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001648 last->e_value_offs = cpu_to_le16(new_offs);
1649 }
1650 }
1651 /* Shift the entries by n bytes */
1652 memmove(to, from, n);
1653}
1654
1655/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001656 * Move xattr pointed to by 'entry' from inode into external xattr block
1657 */
1658static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1659 struct ext4_inode *raw_inode,
1660 struct ext4_xattr_entry *entry)
1661{
1662 struct ext4_xattr_ibody_find *is = NULL;
1663 struct ext4_xattr_block_find *bs = NULL;
1664 char *buffer = NULL, *b_entry_name = NULL;
1665 size_t value_offs, value_size;
1666 struct ext4_xattr_info i = {
1667 .value = NULL,
1668 .value_len = 0,
1669 .name_index = entry->e_name_index,
1670 };
1671 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1672 int error;
1673
1674 value_offs = le16_to_cpu(entry->e_value_offs);
1675 value_size = le32_to_cpu(entry->e_value_size);
1676
1677 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1678 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1679 buffer = kmalloc(value_size, GFP_NOFS);
1680 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1681 if (!is || !bs || !buffer || !b_entry_name) {
1682 error = -ENOMEM;
1683 goto out;
1684 }
1685
1686 is->s.not_found = -ENODATA;
1687 bs->s.not_found = -ENODATA;
1688 is->iloc.bh = NULL;
1689 bs->bh = NULL;
1690
1691 /* Save the entry name and the entry value */
1692 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1693 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1694 b_entry_name[entry->e_name_len] = '\0';
1695 i.name = b_entry_name;
1696
1697 error = ext4_get_inode_loc(inode, &is->iloc);
1698 if (error)
1699 goto out;
1700
1701 error = ext4_xattr_ibody_find(inode, &i, is);
1702 if (error)
1703 goto out;
1704
1705 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001706 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001707 if (error)
1708 goto out;
1709
1710 i.name = b_entry_name;
1711 i.value = buffer;
1712 i.value_len = value_size;
1713 error = ext4_xattr_block_find(inode, &i, bs);
1714 if (error)
1715 goto out;
1716
1717 /* Add entry which was removed from the inode into the block */
1718 error = ext4_xattr_block_set(handle, inode, &i, bs);
1719 if (error)
1720 goto out;
1721 error = 0;
1722out:
1723 kfree(b_entry_name);
1724 kfree(buffer);
1725 if (is)
1726 brelse(is->iloc.bh);
1727 kfree(is);
1728 kfree(bs);
1729
1730 return error;
1731}
1732
Jan Karadfa20642016-08-29 15:44:11 -04001733static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1734 struct ext4_inode *raw_inode,
1735 int isize_diff, size_t ifree,
1736 size_t bfree, int *total_ino)
1737{
1738 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1739 struct ext4_xattr_entry *small_entry;
1740 struct ext4_xattr_entry *entry;
1741 struct ext4_xattr_entry *last;
1742 unsigned int entry_size; /* EA entry size */
1743 unsigned int total_size; /* EA entry size + value size */
1744 unsigned int min_total_size;
1745 int error;
1746
1747 while (isize_diff > ifree) {
1748 entry = NULL;
1749 small_entry = NULL;
1750 min_total_size = ~0U;
1751 last = IFIRST(header);
1752 /* Find the entry best suited to be pushed into EA block */
1753 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1754 total_size =
1755 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1756 EXT4_XATTR_LEN(last->e_name_len);
1757 if (total_size <= bfree &&
1758 total_size < min_total_size) {
1759 if (total_size + ifree < isize_diff) {
1760 small_entry = last;
1761 } else {
1762 entry = last;
1763 min_total_size = total_size;
1764 }
1765 }
1766 }
1767
1768 if (entry == NULL) {
1769 if (small_entry == NULL)
1770 return -ENOSPC;
1771 entry = small_entry;
1772 }
1773
1774 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1775 total_size = entry_size +
1776 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1777 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1778 entry);
1779 if (error)
1780 return error;
1781
1782 *total_ino -= entry_size;
1783 ifree += total_size;
1784 bfree -= total_size;
1785 }
1786
1787 return 0;
1788}
1789
Jan Kara3f2571c2016-08-29 15:42:11 -04001790/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001791 * Expand an inode by new_extra_isize bytes when EAs are present.
1792 * Returns 0 on success or negative error number on failure.
1793 */
1794int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1795 struct ext4_inode *raw_inode, handle_t *handle)
1796{
1797 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001798 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001799 size_t min_offs;
1800 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001801 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001802 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001803 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001804 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 -04001805 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001806 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001807
Theodore Ts'oc755e252017-01-11 21:50:46 -05001808 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1809 return 0;
1810
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001811retry:
Jan Karad0141192016-08-11 11:50:30 -04001812 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001813 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1814 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001815
1816 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001817
1818 /*
1819 * Check if enough free space is available in the inode to shift the
1820 * entries ahead by new_extra_isize.
1821 */
1822
Jan Kara6e0cd082016-08-29 15:43:11 -04001823 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001824 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1825 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001826 total_ino = sizeof(struct ext4_xattr_ibody_header);
1827
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001828 error = xattr_check_inode(inode, header, end);
1829 if (error)
1830 goto cleanup;
1831
Jan Kara6e0cd082016-08-29 15:43:11 -04001832 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001833 if (ifree >= isize_diff)
1834 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001835
1836 /*
1837 * Enough free space isn't available in the inode, check if
1838 * EA block can hold new_extra_isize bytes.
1839 */
1840 if (EXT4_I(inode)->i_file_acl) {
1841 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1842 error = -EIO;
1843 if (!bh)
1844 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001845 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001846 EXT4_ERROR_INODE(inode, "bad block %llu",
1847 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001848 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001849 goto cleanup;
1850 }
1851 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001852 end = bh->b_data + bh->b_size;
1853 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001854 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1855 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001856 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001857 if (!tried_min_extra_isize && s_min_extra_isize) {
1858 tried_min_extra_isize++;
1859 new_extra_isize = s_min_extra_isize;
1860 brelse(bh);
1861 goto retry;
1862 }
Jan Karadfa20642016-08-29 15:44:11 -04001863 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001864 goto cleanup;
1865 }
1866 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001867 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001868 }
1869
Jan Karadfa20642016-08-29 15:44:11 -04001870 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1871 isize_diff, ifree, bfree,
1872 &total_ino);
1873 if (error) {
1874 if (error == -ENOSPC && !tried_min_extra_isize &&
1875 s_min_extra_isize) {
1876 tried_min_extra_isize++;
1877 new_extra_isize = s_min_extra_isize;
1878 brelse(bh);
1879 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001880 }
Jan Karadfa20642016-08-29 15:44:11 -04001881 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001882 }
Jan Karae3014d12016-08-29 15:38:11 -04001883shift:
1884 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001885 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001886 - new_extra_isize, (void *)raw_inode +
1887 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001888 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001889 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001890 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001891out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001892 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001893 return 0;
1894
1895cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001896 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001897 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001898 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001899 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001900 no_expand = 1;
1901 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001902 return error;
1903}
1904
1905
Andreas Dilgere50e5122017-06-21 21:10:32 -04001906#define EIA_INCR 16 /* must be 2^n */
1907#define EIA_MASK (EIA_INCR - 1)
1908/* Add the large xattr @ino into @lea_ino_array for later deletion.
1909 * If @lea_ino_array is new or full it will be grown and the old
1910 * contents copied over.
1911 */
1912static int
1913ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
1914{
1915 if (*lea_ino_array == NULL) {
1916 /*
1917 * Start with 15 inodes, so it fits into a power-of-two size.
1918 * If *lea_ino_array is NULL, this is essentially offsetof()
1919 */
1920 (*lea_ino_array) =
1921 kmalloc(offsetof(struct ext4_xattr_ino_array,
1922 xia_inodes[EIA_MASK]),
1923 GFP_NOFS);
1924 if (*lea_ino_array == NULL)
1925 return -ENOMEM;
1926 (*lea_ino_array)->xia_count = 0;
1927 } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
1928 /* expand the array once all 15 + n * 16 slots are full */
1929 struct ext4_xattr_ino_array *new_array = NULL;
1930 int count = (*lea_ino_array)->xia_count;
1931
1932 /* if new_array is NULL, this is essentially offsetof() */
1933 new_array = kmalloc(
1934 offsetof(struct ext4_xattr_ino_array,
1935 xia_inodes[count + EIA_INCR]),
1936 GFP_NOFS);
1937 if (new_array == NULL)
1938 return -ENOMEM;
1939 memcpy(new_array, *lea_ino_array,
1940 offsetof(struct ext4_xattr_ino_array,
1941 xia_inodes[count]));
1942 kfree(*lea_ino_array);
1943 *lea_ino_array = new_array;
1944 }
1945 (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
1946 return 0;
1947}
1948
1949/**
1950 * Add xattr inode to orphan list
1951 */
1952static int
1953ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
1954 int credits, struct ext4_xattr_ino_array *lea_ino_array)
1955{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04001956 struct inode *ea_inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001957 int idx = 0, error = 0;
1958
1959 if (lea_ino_array == NULL)
1960 return 0;
1961
1962 for (; idx < lea_ino_array->xia_count; ++idx) {
1963 if (!ext4_handle_has_enough_credits(handle, credits)) {
1964 error = ext4_journal_extend(handle, credits);
1965 if (error > 0)
1966 error = ext4_journal_restart(handle, credits);
1967
1968 if (error != 0) {
1969 ext4_warning(inode->i_sb,
1970 "couldn't extend journal "
1971 "(err %d)", error);
1972 return error;
1973 }
1974 }
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04001975 error = ext4_xattr_inode_iget(inode,
1976 lea_ino_array->xia_inodes[idx], &ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001977 if (error)
1978 continue;
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001979 inode_lock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001980 ext4_orphan_add(handle, ea_inode);
Tahsin Erdogan0de59832017-06-21 21:19:16 -04001981 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001982 /* the inode's i_count will be released by caller */
1983 }
1984
1985 return 0;
1986}
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001987
1988/*
Mingming Cao617ba132006-10-11 01:20:53 -07001989 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001990 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04001991 * Free extended attribute resources associated with this inode. Traverse
1992 * all entries and unlink any xattr inodes associated with this inode. This
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993 * is called immediately before an inode is freed. We have exclusive
Andreas Dilgere50e5122017-06-21 21:10:32 -04001994 * access to the inode. If an orphan inode is deleted it will also delete any
1995 * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
1996 * to ensure they belong to the parent inode and were not deleted already.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997 */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001998int
1999ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2000 struct ext4_xattr_ino_array **lea_ino_array)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001{
2002 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002003 struct ext4_xattr_ibody_header *header;
2004 struct ext4_inode *raw_inode;
2005 struct ext4_iloc iloc;
2006 struct ext4_xattr_entry *entry;
2007 int credits = 3, error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002008
Andreas Dilgere50e5122017-06-21 21:10:32 -04002009 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
2010 goto delete_external_ea;
2011
2012 error = ext4_get_inode_loc(inode, &iloc);
2013 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002014 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002015 raw_inode = ext4_raw_inode(&iloc);
2016 header = IHDR(inode, raw_inode);
2017 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
2018 entry = EXT4_XATTR_NEXT(entry)) {
2019 if (!entry->e_value_inum)
2020 continue;
2021 if (ext4_expand_ino_array(lea_ino_array,
2022 entry->e_value_inum) != 0) {
2023 brelse(iloc.bh);
2024 goto cleanup;
2025 }
2026 entry->e_value_inum = 0;
2027 }
2028 brelse(iloc.bh);
2029
2030delete_external_ea:
2031 if (!EXT4_I(inode)->i_file_acl) {
2032 /* add xattr inode to orphan list */
2033 ext4_xattr_inode_orphan_add(handle, inode, credits,
2034 *lea_ino_array);
2035 goto cleanup;
2036 }
Mingming Cao617ba132006-10-11 01:20:53 -07002037 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002039 EXT4_ERROR_INODE(inode, "block %llu read error",
2040 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 goto cleanup;
2042 }
Mingming Cao617ba132006-10-11 01:20:53 -07002043 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002045 EXT4_ERROR_INODE(inode, "bad block %llu",
2046 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 goto cleanup;
2048 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04002049
2050 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2051 entry = EXT4_XATTR_NEXT(entry)) {
2052 if (!entry->e_value_inum)
2053 continue;
2054 if (ext4_expand_ino_array(lea_ino_array,
2055 entry->e_value_inum) != 0)
2056 goto cleanup;
2057 entry->e_value_inum = 0;
2058 }
2059
2060 /* add xattr inode to orphan list */
2061 error = ext4_xattr_inode_orphan_add(handle, inode, credits,
2062 *lea_ino_array);
2063 if (error != 0)
2064 goto cleanup;
2065
2066 if (!IS_NOQUOTA(inode))
2067 credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
2068
2069 if (!ext4_handle_has_enough_credits(handle, credits)) {
2070 error = ext4_journal_extend(handle, credits);
2071 if (error > 0)
2072 error = ext4_journal_restart(handle, credits);
2073 if (error != 0) {
2074 ext4_warning(inode->i_sb,
2075 "couldn't extend journal (err %d)", error);
2076 goto cleanup;
2077 }
2078 }
2079
Mingming Cao617ba132006-10-11 01:20:53 -07002080 ext4_xattr_release_block(handle, inode, bh);
2081 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082
2083cleanup:
2084 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002085
2086 return error;
2087}
2088
2089void
2090ext4_xattr_inode_array_free(struct inode *inode,
2091 struct ext4_xattr_ino_array *lea_ino_array)
2092{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04002093 struct inode *ea_inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002094 int idx = 0;
2095 int err;
2096
2097 if (lea_ino_array == NULL)
2098 return;
2099
2100 for (; idx < lea_ino_array->xia_count; ++idx) {
Tahsin Erdoganbab79b02017-06-21 21:49:53 -04002101 err = ext4_xattr_inode_iget(inode,
2102 lea_ino_array->xia_inodes[idx], &ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002103 if (err)
2104 continue;
2105 /* for inode's i_count get from ext4_xattr_delete_inode */
Tahsin Erdogan1e7d3592017-06-21 21:39:38 -04002106 iput(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002107 clear_nlink(ea_inode);
2108 iput(ea_inode);
2109 }
2110 kfree(lea_ino_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002111}
2112
2113/*
Mingming Cao617ba132006-10-11 01:20:53 -07002114 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002115 *
2116 * Create a new entry in the extended attribute cache, and insert
2117 * it unless such an entry is already in the cache.
2118 *
2119 * Returns 0, or a negative error number on failure.
2120 */
2121static void
Jan Kara7a2508e2016-02-22 22:35:22 -05002122ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002123{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002124 struct ext4_xattr_header *header = BHDR(bh);
2125 __u32 hash = le32_to_cpu(header->h_hash);
2126 int reusable = le32_to_cpu(header->h_refcount) <
2127 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128 int error;
2129
Jan Kara7a2508e2016-02-22 22:35:22 -05002130 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002131 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002132 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002133 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002135 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002136 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002137}
2138
2139/*
Mingming Cao617ba132006-10-11 01:20:53 -07002140 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 *
2142 * Compare two extended attribute blocks for equality.
2143 *
2144 * Returns 0 if the blocks are equal, 1 if they differ, and
2145 * a negative error number on errors.
2146 */
2147static int
Mingming Cao617ba132006-10-11 01:20:53 -07002148ext4_xattr_cmp(struct ext4_xattr_header *header1,
2149 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150{
Mingming Cao617ba132006-10-11 01:20:53 -07002151 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002152
2153 entry1 = ENTRY(header1+1);
2154 entry2 = ENTRY(header2+1);
2155 while (!IS_LAST_ENTRY(entry1)) {
2156 if (IS_LAST_ENTRY(entry2))
2157 return 1;
2158 if (entry1->e_hash != entry2->e_hash ||
2159 entry1->e_name_index != entry2->e_name_index ||
2160 entry1->e_name_len != entry2->e_name_len ||
2161 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002162 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2164 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2166 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2167 le32_to_cpu(entry1->e_value_size)))
2168 return 1;
2169
Mingming Cao617ba132006-10-11 01:20:53 -07002170 entry1 = EXT4_XATTR_NEXT(entry1);
2171 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 }
2173 if (!IS_LAST_ENTRY(entry2))
2174 return 1;
2175 return 0;
2176}
2177
2178/*
Mingming Cao617ba132006-10-11 01:20:53 -07002179 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180 *
2181 * Find an identical extended attribute block.
2182 *
2183 * Returns a pointer to the block found, or NULL if such a block was
2184 * not found or an error occurred.
2185 */
2186static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07002187ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05002188 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189{
2190 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002191 struct mb_cache_entry *ce;
2192 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193
2194 if (!header->h_hash)
2195 return NULL; /* never share */
2196 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002197 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002198 while (ce) {
2199 struct buffer_head *bh;
2200
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002201 bh = sb_bread(inode->i_sb, ce->e_block);
2202 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002203 EXT4_ERROR_INODE(inode, "block %lu read error",
2204 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07002205 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002206 *pce = ce;
2207 return bh;
2208 }
2209 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05002210 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002211 }
2212 return NULL;
2213}
2214
2215#define NAME_HASH_SHIFT 5
2216#define VALUE_HASH_SHIFT 16
2217
2218/*
Mingming Cao617ba132006-10-11 01:20:53 -07002219 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002220 *
2221 * Compute the hash of an extended attribute.
2222 */
Mingming Cao617ba132006-10-11 01:20:53 -07002223static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2224 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225{
2226 __u32 hash = 0;
2227 char *name = entry->e_name;
2228 int n;
2229
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002230 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 hash = (hash << NAME_HASH_SHIFT) ^
2232 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2233 *name++;
2234 }
2235
Andreas Dilgere50e5122017-06-21 21:10:32 -04002236 if (!entry->e_value_inum && entry->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002237 __le32 *value = (__le32 *)((char *)header +
2238 le16_to_cpu(entry->e_value_offs));
2239 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07002240 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002241 hash = (hash << VALUE_HASH_SHIFT) ^
2242 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2243 le32_to_cpu(*value++);
2244 }
2245 }
2246 entry->e_hash = cpu_to_le32(hash);
2247}
2248
2249#undef NAME_HASH_SHIFT
2250#undef VALUE_HASH_SHIFT
2251
2252#define BLOCK_HASH_SHIFT 16
2253
2254/*
Mingming Cao617ba132006-10-11 01:20:53 -07002255 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256 *
2257 * Re-compute the extended attribute hash value after an entry has changed.
2258 */
Mingming Cao617ba132006-10-11 01:20:53 -07002259static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2260 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261{
Mingming Cao617ba132006-10-11 01:20:53 -07002262 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002263 __u32 hash = 0;
2264
Mingming Cao617ba132006-10-11 01:20:53 -07002265 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002266 here = ENTRY(header+1);
2267 while (!IS_LAST_ENTRY(here)) {
2268 if (!here->e_hash) {
2269 /* Block is not shared if an entry's hash value == 0 */
2270 hash = 0;
2271 break;
2272 }
2273 hash = (hash << BLOCK_HASH_SHIFT) ^
2274 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2275 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07002276 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002277 }
2278 header->h_hash = cpu_to_le32(hash);
2279}
2280
2281#undef BLOCK_HASH_SHIFT
2282
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002283#define HASH_BUCKET_BITS 10
2284
Jan Kara7a2508e2016-02-22 22:35:22 -05002285struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05002286ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287{
Jan Kara7a2508e2016-02-22 22:35:22 -05002288 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289}
2290
Jan Kara7a2508e2016-02-22 22:35:22 -05002291void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002292{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002293 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05002294 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002296