blob: 63656dbafdc4519f40bfb885aec78b65d66cc967 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/xattr.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 *
7 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
Mingming Cao617ba132006-10-11 01:20:53 -07008 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07009 * Extended attributes for symlinks and special files added per
10 * suggestion of Luka Renko <luka.renko@hermes.si>.
11 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * Red Hat Inc.
13 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14 * and Andreas Gruenbacher <agruen@suse.de>.
15 */
16
17/*
18 * Extended attributes are stored directly in inodes (on file systems with
19 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20 * field contains the block number if an inode uses an additional block. All
21 * attributes must fit in the inode and one additional block. Blocks that
22 * contain the identical set of attributes may be shared among several inodes.
23 * Identical blocks are detected by keeping a cache of blocks that have
24 * recently been accessed.
25 *
26 * The attributes in inodes and on blocks have a different header; the entries
27 * are stored in the same format:
28 *
29 * +------------------+
30 * | header |
31 * | entry 1 | |
32 * | entry 2 | | growing downwards
33 * | entry 3 | v
34 * | four null bytes |
35 * | . . . |
36 * | value 1 | ^
37 * | value 3 | | growing upwards
38 * | value 2 | |
39 * +------------------+
40 *
41 * The header is followed by multiple entry descriptors. In disk blocks, the
42 * entry descriptors are kept sorted. In inodes, they are unsorted. The
43 * attribute values are aligned to the end of the block in no specific order.
44 *
45 * Locking strategy
46 * ----------------
Mingming Cao617ba132006-10-11 01:20:53 -070047 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048 * EA blocks are only changed if they are exclusive to an inode, so
49 * holding xattr_sem also means that nothing but the EA block's reference
50 * count can change. Multiple writers to the same block are synchronized
51 * by the buffer lock.
52 */
53
54#include <linux/init.h>
55#include <linux/fs.h>
56#include <linux/slab.h>
Jan Kara7a2508e2016-02-22 22:35:22 -050057#include <linux/mbcache.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070058#include <linux/quotaops.h>
Jeff Laytonee73f9a2018-01-09 08:21:39 -050059#include <linux/iversion.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040060#include "ext4_jbd2.h"
61#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062#include "xattr.h"
63#include "acl.h"
64
Mingming Cao617ba132006-10-11 01:20:53 -070065#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040066# define ea_idebug(inode, fmt, ...) \
67 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
68 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69# define ea_bdebug(bh, fmt, ...) \
70 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
71 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070072#else
Joe Perchesace36ad2012-03-19 23:11:43 -040073# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
74# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075#endif
76
Tahsin Erdogan47387402017-06-22 11:28:55 -040077static void ext4_xattr_block_cache_insert(struct mb_cache *,
78 struct buffer_head *);
79static struct buffer_head *
80ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
81 struct mb_cache_entry **);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -040082static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
83 size_t value_count);
Tahsin Erdogandaf83282017-06-22 11:52:03 -040084static void ext4_xattr_rehash(struct ext4_xattr_header *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070085
Eric Biggersd6006182017-04-29 23:47:50 -040086static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070087 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040088#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080089 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
90 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091#endif
Mingming Cao617ba132006-10-11 01:20:53 -070092 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040093#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070094 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095#endif
96};
97
Stephen Hemminger11e27522010-05-13 17:53:18 -070098const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070099 &ext4_xattr_user_handler,
100 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400101#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800102 &posix_acl_access_xattr_handler,
103 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400105#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700106 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107#endif
108 NULL
109};
110
Tahsin Erdogan47387402017-06-22 11:28:55 -0400111#define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
112 inode->i_sb->s_fs_info)->s_ea_block_cache)
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400113
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400114#define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
115 inode->i_sb->s_fs_info)->s_ea_inode_cache)
116
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400117static int
118ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
119 struct inode *inode);
120
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400121#ifdef CONFIG_LOCKDEP
122void ext4_xattr_inode_set_class(struct inode *ea_inode)
123{
124 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
125}
126#endif
127
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400128static __le32 ext4_xattr_block_csum(struct inode *inode,
129 sector_t block_nr,
130 struct ext4_xattr_header *hdr)
131{
132 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400133 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400134 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400135 __u32 dummy_csum = 0;
136 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400137
Theodore Ts'od6a77102013-04-09 23:59:55 -0400138 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
139 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400140 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
141 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
142 offset += sizeof(dummy_csum);
143 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
144 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400145
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400146 return cpu_to_le32(csum);
147}
148
149static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400150 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400151{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400152 struct ext4_xattr_header *hdr = BHDR(bh);
153 int ret = 1;
154
155 if (ext4_has_metadata_csum(inode->i_sb)) {
156 lock_buffer(bh);
157 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
158 bh->b_blocknr, hdr));
159 unlock_buffer(bh);
160 }
161 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400162}
163
164static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400165 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400166{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400167 if (ext4_has_metadata_csum(inode->i_sb))
168 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
169 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400170}
171
Stephen Hemminger11e27522010-05-13 17:53:18 -0700172static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700173ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700175 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176
Mingming Cao617ba132006-10-11 01:20:53 -0700177 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
178 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700179 return handler;
180}
181
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400183ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
184 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400186 struct ext4_xattr_entry *e = entry;
187
Eric Biggersd7614cc2016-12-01 14:57:29 -0500188 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189 while (!IS_LAST_ENTRY(e)) {
190 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400192 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400195
Eric Biggersd7614cc2016-12-01 14:57:29 -0500196 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400197 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400198 if (entry->e_value_size != 0 &&
199 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500200 u16 offs = le16_to_cpu(entry->e_value_offs);
201 u32 size = le32_to_cpu(entry->e_value_size);
202 void *value;
203
204 /*
205 * The value cannot overlap the names, and the value
206 * with padding cannot extend beyond 'end'. Check both
207 * the padded and unpadded sizes, since the size may
208 * overflow to 0 when adding padding.
209 */
210 if (offs > end - value_start)
211 return -EFSCORRUPTED;
212 value = value_start + offs;
213 if (value < (void *)e + sizeof(u32) ||
214 size > end - value ||
215 EXT4_XATTR_SIZE(size) > end - value)
216 return -EFSCORRUPTED;
217 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400218 entry = EXT4_XATTR_NEXT(entry);
219 }
220
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221 return 0;
222}
223
224static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400225ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400227 int error;
228
229 if (buffer_verified(bh))
230 return 0;
231
Mingming Cao617ba132006-10-11 01:20:53 -0700232 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400234 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400235 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400236 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400237 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
238 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400239 if (!error)
240 set_buffer_verified(bh);
241 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242}
243
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400244static int
245__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
246 void *end, const char *function, unsigned int line)
247{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400248 int error = -EFSCORRUPTED;
249
Eric Biggers290ab232016-12-01 14:51:58 -0500250 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400251 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400252 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400253 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400254errout:
255 if (error)
256 __ext4_error_inode(inode, function, line, 0,
257 "corrupted in-inode xattr");
258 return error;
259}
260
261#define xattr_check_inode(inode, header, end) \
262 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
263
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264static int
Mingming Cao617ba132006-10-11 01:20:53 -0700265ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400266 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267{
Mingming Cao617ba132006-10-11 01:20:53 -0700268 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269 size_t name_len;
270 int cmp = 1;
271
272 if (name == NULL)
273 return -EINVAL;
274 name_len = strlen(name);
275 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700276 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 cmp = name_index - entry->e_name_index;
278 if (!cmp)
279 cmp = name_len - entry->e_name_len;
280 if (!cmp)
281 cmp = memcmp(name, entry->e_name, name_len);
282 if (cmp <= 0 && (sorted || cmp == 0))
283 break;
284 }
285 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 return cmp ? -ENODATA : 0;
287}
288
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400289static u32
290ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
291{
292 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
293}
294
295static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
296{
297 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500298 (u32) inode_peek_iversion_raw(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400299}
300
301static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
302{
303 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500304 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400305}
306
307static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
308{
309 return (u32)ea_inode->i_atime.tv_sec;
310}
311
312static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
313{
314 ea_inode->i_atime.tv_sec = hash;
315}
316
Andreas Dilgere50e5122017-06-21 21:10:32 -0400317/*
318 * Read the EA value from an inode.
319 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400320static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400321{
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400322 int blocksize = 1 << ea_inode->i_blkbits;
323 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
324 int tail_size = (size % blocksize) ?: blocksize;
325 struct buffer_head *bhs_inline[8];
326 struct buffer_head **bhs = bhs_inline;
327 int i, ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400328
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400329 if (bh_count > ARRAY_SIZE(bhs_inline)) {
330 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
331 if (!bhs)
332 return -ENOMEM;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400333 }
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400334
335 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
336 true /* wait */, bhs);
337 if (ret)
338 goto free_bhs;
339
340 for (i = 0; i < bh_count; i++) {
341 /* There shouldn't be any holes in ea_inode. */
342 if (!bhs[i]) {
343 ret = -EFSCORRUPTED;
344 goto put_bhs;
345 }
346 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
347 i < bh_count - 1 ? blocksize : tail_size);
348 }
349 ret = 0;
350put_bhs:
351 for (i = 0; i < bh_count; i++)
352 brelse(bhs[i]);
353free_bhs:
354 if (bhs != bhs_inline)
355 kfree(bhs);
356 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400357}
358
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400359#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
360
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400361static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400362 u32 ea_inode_hash, struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400363{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400364 struct inode *inode;
365 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400366
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400367 inode = ext4_iget(parent->i_sb, ea_ino);
368 if (IS_ERR(inode)) {
369 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400370 ext4_error(parent->i_sb,
371 "error while reading EA inode %lu err=%d", ea_ino,
372 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400373 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400374 }
375
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400376 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400377 ext4_error(parent->i_sb,
378 "error while reading EA inode %lu is_bad_inode",
379 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400380 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400381 goto error;
382 }
383
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400384 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400385 ext4_error(parent->i_sb,
386 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
387 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400388 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400389 goto error;
390 }
391
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400392 ext4_xattr_inode_set_class(inode);
393
394 /*
395 * Check whether this is an old Lustre-style xattr inode. Lustre
396 * implementation does not have hash validation, rather it has a
397 * backpointer from ea_inode to the parent inode.
398 */
399 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
400 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
401 inode->i_generation == parent->i_generation) {
402 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
403 ext4_xattr_inode_set_ref(inode, 1);
404 } else {
405 inode_lock(inode);
406 inode->i_flags |= S_NOQUOTA;
407 inode_unlock(inode);
408 }
409
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400410 *ea_inode = inode;
411 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400412error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400413 iput(inode);
414 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400415}
416
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400417static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400418ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
419 struct ext4_xattr_entry *entry, void *buffer,
420 size_t size)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400421{
422 u32 hash;
423
424 /* Verify stored hash matches calculated hash. */
425 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
426 if (hash != ext4_xattr_inode_get_hash(ea_inode))
427 return -EFSCORRUPTED;
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400428
429 if (entry) {
430 __le32 e_hash, tmp_data;
431
432 /* Verify entry hash. */
433 tmp_data = cpu_to_le32(hash);
434 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
435 &tmp_data, 1);
436 if (e_hash != entry->e_hash)
437 return -EFSCORRUPTED;
438 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400439 return 0;
440}
441
Andreas Dilgere50e5122017-06-21 21:10:32 -0400442/*
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400443 * Read xattr value from the EA inode.
Andreas Dilgere50e5122017-06-21 21:10:32 -0400444 */
445static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400446ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
447 void *buffer, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400448{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400449 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400450 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400451 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400452
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400453 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400454 le32_to_cpu(entry->e_hash), &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400455 if (err) {
456 ea_inode = NULL;
457 goto out;
458 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400459
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400460 if (i_size_read(ea_inode) != size) {
461 ext4_warning_inode(ea_inode,
462 "ea_inode file size=%llu entry size=%zu",
463 i_size_read(ea_inode), size);
464 err = -EFSCORRUPTED;
465 goto out;
466 }
467
468 err = ext4_xattr_inode_read(ea_inode, buffer, size);
469 if (err)
470 goto out;
471
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400472 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
473 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
474 size);
475 if (err) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400476 ext4_warning_inode(ea_inode,
477 "EA inode hash validation failed");
478 goto out;
479 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400480
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400481 if (ea_inode_cache)
482 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
483 ext4_xattr_inode_get_hash(ea_inode),
484 ea_inode->i_ino, true /* reusable */);
485 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400486out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400487 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400488 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400489}
490
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491static int
Mingming Cao617ba132006-10-11 01:20:53 -0700492ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 void *buffer, size_t buffer_size)
494{
495 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700496 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 size_t size;
498 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400499 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500
501 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
502 name_index, name, buffer, (long)buffer_size);
503
504 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700505 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400507 ea_idebug(inode, "reading block %llu",
508 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700509 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 if (!bh)
511 goto cleanup;
512 ea_bdebug(bh, "b_count=%d, refcount=%d",
513 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400514 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400515 EXT4_ERROR_INODE(inode, "bad block %llu",
516 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400517 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 goto cleanup;
519 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400520 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400522 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 if (error)
524 goto cleanup;
525 size = le32_to_cpu(entry->e_value_size);
526 if (buffer) {
527 error = -ERANGE;
528 if (size > buffer_size)
529 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400530 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400531 error = ext4_xattr_inode_get(inode, entry, buffer,
532 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400533 if (error)
534 goto cleanup;
535 } else {
536 memcpy(buffer, bh->b_data +
537 le16_to_cpu(entry->e_value_offs), size);
538 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 }
540 error = size;
541
542cleanup:
543 brelse(bh);
544 return error;
545}
546
Tao Ma879b3822012-12-05 10:28:46 -0500547int
Mingming Cao617ba132006-10-11 01:20:53 -0700548ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 void *buffer, size_t buffer_size)
550{
Mingming Cao617ba132006-10-11 01:20:53 -0700551 struct ext4_xattr_ibody_header *header;
552 struct ext4_xattr_entry *entry;
553 struct ext4_inode *raw_inode;
554 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 size_t size;
556 void *end;
557 int error;
558
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500559 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700561 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 if (error)
563 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700564 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700566 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400567 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 if (error)
569 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400570 entry = IFIRST(header);
571 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 if (error)
573 goto cleanup;
574 size = le32_to_cpu(entry->e_value_size);
575 if (buffer) {
576 error = -ERANGE;
577 if (size > buffer_size)
578 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400579 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400580 error = ext4_xattr_inode_get(inode, entry, buffer,
581 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400582 if (error)
583 goto cleanup;
584 } else {
585 memcpy(buffer, (void *)IFIRST(header) +
586 le16_to_cpu(entry->e_value_offs), size);
587 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 }
589 error = size;
590
591cleanup:
592 brelse(iloc.bh);
593 return error;
594}
595
596/*
Mingming Cao617ba132006-10-11 01:20:53 -0700597 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700598 *
599 * Copy an extended attribute into the buffer
600 * provided, or compute the buffer size required.
601 * Buffer is NULL to compute the size of the buffer required.
602 *
603 * Returns a negative error number on failure, or the number of bytes
604 * used / required on success.
605 */
606int
Mingming Cao617ba132006-10-11 01:20:53 -0700607ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608 void *buffer, size_t buffer_size)
609{
610 int error;
611
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500612 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
613 return -EIO;
614
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400615 if (strlen(name) > 255)
616 return -ERANGE;
617
Mingming Cao617ba132006-10-11 01:20:53 -0700618 down_read(&EXT4_I(inode)->xattr_sem);
619 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 buffer_size);
621 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700622 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700624 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 return error;
626}
627
628static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000629ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 char *buffer, size_t buffer_size)
631{
632 size_t rest = buffer_size;
633
Mingming Cao617ba132006-10-11 01:20:53 -0700634 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700635 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700636 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100638 if (handler && (!handler->list || handler->list(dentry))) {
639 const char *prefix = handler->prefix ?: handler->name;
640 size_t prefix_len = strlen(prefix);
641 size_t size = prefix_len + entry->e_name_len + 1;
642
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 if (buffer) {
644 if (size > rest)
645 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100646 memcpy(buffer, prefix, prefix_len);
647 buffer += prefix_len;
648 memcpy(buffer, entry->e_name, entry->e_name_len);
649 buffer += entry->e_name_len;
650 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 }
652 rest -= size;
653 }
654 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100655 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700656}
657
658static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000659ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660{
David Howells2b0143b2015-03-17 22:25:59 +0000661 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662 struct buffer_head *bh = NULL;
663 int error;
664
665 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
666 buffer, (long)buffer_size);
667
668 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700669 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400671 ea_idebug(inode, "reading block %llu",
672 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700673 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 error = -EIO;
675 if (!bh)
676 goto cleanup;
677 ea_bdebug(bh, "b_count=%d, refcount=%d",
678 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400679 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400680 EXT4_ERROR_INODE(inode, "bad block %llu",
681 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400682 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 goto cleanup;
684 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400685 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000686 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687
688cleanup:
689 brelse(bh);
690
691 return error;
692}
693
694static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000695ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696{
David Howells2b0143b2015-03-17 22:25:59 +0000697 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700698 struct ext4_xattr_ibody_header *header;
699 struct ext4_inode *raw_inode;
700 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 void *end;
702 int error;
703
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500704 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700706 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 if (error)
708 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700709 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700711 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400712 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 if (error)
714 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000715 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 buffer, buffer_size);
717
718cleanup:
719 brelse(iloc.bh);
720 return error;
721}
722
723/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400724 * Inode operation listxattr()
725 *
726 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 *
728 * Copy a list of attribute names into the buffer
729 * provided, or compute the buffer size required.
730 * Buffer is NULL to compute the size of the buffer required.
731 *
732 * Returns a negative error number on failure, or the number of bytes
733 * used / required on success.
734 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400735ssize_t
736ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500738 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739
David Howells2b0143b2015-03-17 22:25:59 +0000740 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500741 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
742 if (ret < 0)
743 goto errout;
744 if (buffer) {
745 buffer += ret;
746 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500748 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
749 if (ret < 0)
750 goto errout;
751 ret += ret2;
752errout:
David Howells2b0143b2015-03-17 22:25:59 +0000753 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500754 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755}
756
757/*
Mingming Cao617ba132006-10-11 01:20:53 -0700758 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 * not set, set it.
760 */
Mingming Cao617ba132006-10-11 01:20:53 -0700761static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 struct super_block *sb)
763{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400764 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 return;
766
liang xie5d601252014-05-12 22:06:43 -0400767 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700768 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400769 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400770 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772}
773
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400774int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
775{
776 struct ext4_iloc iloc = { .bh = NULL };
777 struct buffer_head *bh = NULL;
778 struct ext4_inode *raw_inode;
779 struct ext4_xattr_ibody_header *header;
780 struct ext4_xattr_entry *entry;
781 qsize_t ea_inode_refs = 0;
782 void *end;
783 int ret;
784
785 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
786
787 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
788 ret = ext4_get_inode_loc(inode, &iloc);
789 if (ret)
790 goto out;
791 raw_inode = ext4_raw_inode(&iloc);
792 header = IHDR(inode, raw_inode);
793 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
794 ret = xattr_check_inode(inode, header, end);
795 if (ret)
796 goto out;
797
798 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
799 entry = EXT4_XATTR_NEXT(entry))
800 if (entry->e_value_inum)
801 ea_inode_refs++;
802 }
803
804 if (EXT4_I(inode)->i_file_acl) {
805 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
806 if (!bh) {
807 ret = -EIO;
808 goto out;
809 }
810
811 if (ext4_xattr_check_block(inode, bh)) {
812 ret = -EFSCORRUPTED;
813 goto out;
814 }
815
816 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
817 entry = EXT4_XATTR_NEXT(entry))
818 if (entry->e_value_inum)
819 ea_inode_refs++;
820 }
821 *usage = ea_inode_refs + 1;
822 ret = 0;
823out:
824 brelse(iloc.bh);
825 brelse(bh);
826 return ret;
827}
828
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400829static inline size_t round_up_cluster(struct inode *inode, size_t length)
830{
831 struct super_block *sb = inode->i_sb;
832 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
833 inode->i_blkbits);
834 size_t mask = ~(cluster_size - 1);
835
836 return (length + cluster_size - 1) & mask;
837}
838
839static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
840{
841 int err;
842
843 err = dquot_alloc_inode(inode);
844 if (err)
845 return err;
846 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
847 if (err)
848 dquot_free_inode(inode);
849 return err;
850}
851
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400852static void ext4_xattr_inode_free_quota(struct inode *parent,
853 struct inode *ea_inode,
854 size_t len)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400855{
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400856 if (ea_inode &&
857 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
858 return;
859 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
860 dquot_free_inode(parent);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400861}
862
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400863int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
864 struct buffer_head *block_bh, size_t value_len,
865 bool is_create)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400866{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400867 int credits;
868 int blocks;
869
870 /*
871 * 1) Owner inode update
872 * 2) Ref count update on old xattr block
873 * 3) new xattr block
874 * 4) block bitmap update for new xattr block
875 * 5) group descriptor for new xattr block
876 * 6) block bitmap update for old xattr block
877 * 7) group descriptor for old block
878 *
879 * 6 & 7 can happen if we have two racing threads T_a and T_b
880 * which are each trying to set an xattr on inodes I_a and I_b
881 * which were both initially sharing an xattr block.
882 */
883 credits = 7;
884
885 /* Quota updates. */
886 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
887
888 /*
889 * In case of inline data, we may push out the data to a block,
890 * so we need to reserve credits for this eventuality
891 */
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400892 if (inode && ext4_has_inline_data(inode))
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400893 credits += ext4_writepage_trans_blocks(inode) + 1;
894
895 /* We are done if ea_inode feature is not enabled. */
896 if (!ext4_has_feature_ea_inode(sb))
897 return credits;
898
899 /* New ea_inode, inode map, block bitmap, group descriptor. */
900 credits += 4;
901
902 /* Data blocks. */
903 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
904
905 /* Indirection block or one level of extent tree. */
906 blocks += 1;
907
908 /* Block bitmap and group descriptor updates for each block. */
909 credits += blocks * 2;
910
911 /* Blocks themselves. */
912 credits += blocks;
913
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400914 if (!is_create) {
915 /* Dereference ea_inode holding old xattr value.
916 * Old ea_inode, inode map, block bitmap, group descriptor.
917 */
918 credits += 4;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400919
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400920 /* Data blocks for old ea_inode. */
921 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400922
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400923 /* Indirection block or one level of extent tree for old
924 * ea_inode.
925 */
926 blocks += 1;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400927
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400928 /* Block bitmap and group descriptor updates for each block. */
929 credits += blocks * 2;
930 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400931
932 /* We may need to clone the existing xattr block in which case we need
933 * to increment ref counts for existing ea_inodes referenced by it.
934 */
935 if (block_bh) {
936 struct ext4_xattr_entry *entry = BFIRST(block_bh);
937
938 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
939 if (entry->e_value_inum)
940 /* Ref count update on ea_inode. */
941 credits += 1;
942 }
943 return credits;
944}
945
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400946static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
947 int credits, struct buffer_head *bh,
948 bool dirty, bool block_csum)
949{
950 int error;
951
952 if (!ext4_handle_valid(handle))
953 return 0;
954
955 if (handle->h_buffer_credits >= credits)
956 return 0;
957
958 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
959 if (!error)
960 return 0;
961 if (error < 0) {
962 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
963 return error;
964 }
965
966 if (bh && dirty) {
967 if (block_csum)
968 ext4_xattr_block_csum_set(inode, bh);
969 error = ext4_handle_dirty_metadata(handle, NULL, bh);
970 if (error) {
971 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
972 error);
973 return error;
974 }
975 }
976
977 error = ext4_journal_restart(handle, credits);
978 if (error) {
979 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
980 return error;
981 }
982
983 if (bh) {
984 error = ext4_journal_get_write_access(handle, bh);
985 if (error) {
986 ext4_warning(inode->i_sb,
987 "Get write access failed (error %d)",
988 error);
989 return error;
990 }
991 }
992 return 0;
993}
994
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400995static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
996 int ref_change)
997{
998 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
999 struct ext4_iloc iloc;
1000 s64 ref_count;
1001 u32 hash;
1002 int ret;
1003
1004 inode_lock(ea_inode);
1005
1006 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1007 if (ret) {
1008 iloc.bh = NULL;
1009 goto out;
1010 }
1011
1012 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1013 ref_count += ref_change;
1014 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1015
1016 if (ref_change > 0) {
1017 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1018 ea_inode->i_ino, ref_count);
1019
1020 if (ref_count == 1) {
1021 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1022 ea_inode->i_ino, ea_inode->i_nlink);
1023
1024 set_nlink(ea_inode, 1);
1025 ext4_orphan_del(handle, ea_inode);
1026
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001027 if (ea_inode_cache) {
1028 hash = ext4_xattr_inode_get_hash(ea_inode);
1029 mb_cache_entry_create(ea_inode_cache,
1030 GFP_NOFS, hash,
1031 ea_inode->i_ino,
1032 true /* reusable */);
1033 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001034 }
1035 } else {
1036 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1037 ea_inode->i_ino, ref_count);
1038
1039 if (ref_count == 0) {
1040 WARN_ONCE(ea_inode->i_nlink != 1,
1041 "EA inode %lu i_nlink=%u",
1042 ea_inode->i_ino, ea_inode->i_nlink);
1043
1044 clear_nlink(ea_inode);
1045 ext4_orphan_add(handle, ea_inode);
1046
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001047 if (ea_inode_cache) {
1048 hash = ext4_xattr_inode_get_hash(ea_inode);
1049 mb_cache_entry_delete(ea_inode_cache, hash,
1050 ea_inode->i_ino);
1051 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001052 }
1053 }
1054
1055 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1056 iloc.bh = NULL;
1057 if (ret)
1058 ext4_warning_inode(ea_inode,
1059 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1060out:
1061 brelse(iloc.bh);
1062 inode_unlock(ea_inode);
1063 return ret;
1064}
1065
1066static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1067{
1068 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1069}
1070
1071static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1072{
1073 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1074}
1075
1076static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1077 struct ext4_xattr_entry *first)
1078{
1079 struct inode *ea_inode;
1080 struct ext4_xattr_entry *entry;
1081 struct ext4_xattr_entry *failed_entry;
1082 unsigned int ea_ino;
1083 int err, saved_err;
1084
1085 for (entry = first; !IS_LAST_ENTRY(entry);
1086 entry = EXT4_XATTR_NEXT(entry)) {
1087 if (!entry->e_value_inum)
1088 continue;
1089 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001090 err = ext4_xattr_inode_iget(parent, ea_ino,
1091 le32_to_cpu(entry->e_hash),
1092 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001093 if (err)
1094 goto cleanup;
1095 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1096 if (err) {
1097 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1098 iput(ea_inode);
1099 goto cleanup;
1100 }
1101 iput(ea_inode);
1102 }
1103 return 0;
1104
1105cleanup:
1106 saved_err = err;
1107 failed_entry = entry;
1108
1109 for (entry = first; entry != failed_entry;
1110 entry = EXT4_XATTR_NEXT(entry)) {
1111 if (!entry->e_value_inum)
1112 continue;
1113 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001114 err = ext4_xattr_inode_iget(parent, ea_ino,
1115 le32_to_cpu(entry->e_hash),
1116 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001117 if (err) {
1118 ext4_warning(parent->i_sb,
1119 "cleanup ea_ino %u iget error %d", ea_ino,
1120 err);
1121 continue;
1122 }
1123 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1124 if (err)
1125 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1126 err);
1127 iput(ea_inode);
1128 }
1129 return saved_err;
1130}
1131
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001132static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001133ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1134 struct buffer_head *bh,
1135 struct ext4_xattr_entry *first, bool block_csum,
1136 struct ext4_xattr_inode_array **ea_inode_array,
1137 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001138{
1139 struct inode *ea_inode;
1140 struct ext4_xattr_entry *entry;
1141 bool dirty = false;
1142 unsigned int ea_ino;
1143 int err;
1144 int credits;
1145
1146 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1147 credits = 2 + extra_credits;
1148
1149 for (entry = first; !IS_LAST_ENTRY(entry);
1150 entry = EXT4_XATTR_NEXT(entry)) {
1151 if (!entry->e_value_inum)
1152 continue;
1153 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001154 err = ext4_xattr_inode_iget(parent, ea_ino,
1155 le32_to_cpu(entry->e_hash),
1156 &ea_inode);
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001157 if (err)
1158 continue;
1159
1160 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1161 if (err) {
1162 ext4_warning_inode(ea_inode,
1163 "Expand inode array err=%d", err);
1164 iput(ea_inode);
1165 continue;
1166 }
1167
1168 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1169 dirty, block_csum);
1170 if (err) {
1171 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1172 err);
1173 continue;
1174 }
1175
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001176 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1177 if (err) {
1178 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1179 err);
1180 continue;
1181 }
1182
1183 if (!skip_quota)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001184 ext4_xattr_inode_free_quota(parent, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001185 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001186
1187 /*
1188 * Forget about ea_inode within the same transaction that
1189 * decrements the ref count. This avoids duplicate decrements in
1190 * case the rest of the work spills over to subsequent
1191 * transactions.
1192 */
1193 entry->e_value_inum = 0;
1194 entry->e_value_size = 0;
1195
1196 dirty = true;
1197 }
1198
1199 if (dirty) {
1200 /*
1201 * Note that we are deliberately skipping csum calculation for
1202 * the final update because we do not expect any journal
1203 * restarts until xattr block is freed.
1204 */
1205
1206 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1207 if (err)
1208 ext4_warning_inode(parent,
1209 "handle dirty metadata err=%d", err);
1210 }
1211}
1212
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001213/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001214 * Release the xattr block BH: If the reference count is > 1, decrement it;
1215 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 */
1217static void
Mingming Cao617ba132006-10-11 01:20:53 -07001218ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001219 struct buffer_head *bh,
1220 struct ext4_xattr_inode_array **ea_inode_array,
1221 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001223 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001224 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001225 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226
liang xie5d601252014-05-12 22:06:43 -04001227 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001228 error = ext4_journal_get_write_access(handle, bh);
1229 if (error)
1230 goto out;
1231
1232 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001233 hash = le32_to_cpu(BHDR(bh)->h_hash);
1234 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1235 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001237 /*
1238 * This must happen under buffer lock for
1239 * ext4_xattr_block_set() to reliably detect freed block
1240 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001241 if (ea_block_cache)
1242 mb_cache_entry_delete(ea_block_cache, hash,
1243 bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001244 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001245 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001246
1247 if (ext4_has_feature_ea_inode(inode->i_sb))
1248 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1249 BFIRST(bh),
1250 true /* block_csum */,
1251 ea_inode_array,
1252 extra_credits,
1253 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001254 ext4_free_blocks(handle, inode, bh, 0, 1,
1255 EXT4_FREE_BLOCKS_METADATA |
1256 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001258 ref--;
1259 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1260 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1261 struct mb_cache_entry *ce;
1262
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001263 if (ea_block_cache) {
1264 ce = mb_cache_entry_get(ea_block_cache, hash,
1265 bh->b_blocknr);
1266 if (ce) {
1267 ce->e_reusable = 1;
1268 mb_cache_entry_put(ea_block_cache, ce);
1269 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001270 }
1271 }
1272
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001273 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001274 /*
1275 * Beware of this ugliness: Releasing of xattr block references
1276 * from different inodes can race and so we have to protect
1277 * from a race where someone else frees the block (and releases
1278 * its journal_head) before we are done dirtying the buffer. In
1279 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001280 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001281 * that function can call sync_dirty_buffer() so for that case
1282 * we handle the dirtying after unlocking the buffer.
1283 */
1284 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001285 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001286 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001287 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001288 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001289 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001290 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001291 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001292 ea_bdebug(bh, "refcount now=%d; releasing",
1293 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001295out:
1296 ext4_std_error(inode->i_sb, error);
1297 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298}
1299
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001300/*
1301 * Find the available free space for EAs. This also returns the total number of
1302 * bytes used by EA entries.
1303 */
1304static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1305 size_t *min_offs, void *base, int *total)
1306{
1307 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001308 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001309 size_t offs = le16_to_cpu(last->e_value_offs);
1310 if (offs < *min_offs)
1311 *min_offs = offs;
1312 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001313 if (total)
1314 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001315 }
1316 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1317}
1318
Andreas Dilgere50e5122017-06-21 21:10:32 -04001319/*
1320 * Write the value of the EA in an inode.
1321 */
1322static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1323 const void *buf, int bufsize)
1324{
1325 struct buffer_head *bh = NULL;
1326 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001327 int blocksize = ea_inode->i_sb->s_blocksize;
1328 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001329 int csize, wsize = 0;
1330 int ret = 0;
1331 int retries = 0;
1332
1333retry:
1334 while (ret >= 0 && ret < max_blocks) {
1335 struct ext4_map_blocks map;
1336 map.m_lblk = block += ret;
1337 map.m_len = max_blocks -= ret;
1338
1339 ret = ext4_map_blocks(handle, ea_inode, &map,
1340 EXT4_GET_BLOCKS_CREATE);
1341 if (ret <= 0) {
1342 ext4_mark_inode_dirty(handle, ea_inode);
1343 if (ret == -ENOSPC &&
1344 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1345 ret = 0;
1346 goto retry;
1347 }
1348 break;
1349 }
1350 }
1351
1352 if (ret < 0)
1353 return ret;
1354
1355 block = 0;
1356 while (wsize < bufsize) {
1357 if (bh != NULL)
1358 brelse(bh);
1359 csize = (bufsize - wsize) > blocksize ? blocksize :
1360 bufsize - wsize;
1361 bh = ext4_getblk(handle, ea_inode, block, 0);
1362 if (IS_ERR(bh))
1363 return PTR_ERR(bh);
1364 ret = ext4_journal_get_write_access(handle, bh);
1365 if (ret)
1366 goto out;
1367
1368 memcpy(bh->b_data, buf, csize);
1369 set_buffer_uptodate(bh);
1370 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1371
1372 buf += csize;
1373 wsize += csize;
1374 block += 1;
1375 }
1376
1377 inode_lock(ea_inode);
1378 i_size_write(ea_inode, wsize);
1379 ext4_update_i_disksize(ea_inode, wsize);
1380 inode_unlock(ea_inode);
1381
1382 ext4_mark_inode_dirty(handle, ea_inode);
1383
1384out:
1385 brelse(bh);
1386
1387 return ret;
1388}
1389
1390/*
1391 * Create an inode to store the value of a large EA.
1392 */
1393static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001394 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001395{
1396 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001397 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001398 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001399
1400 /*
1401 * Let the next inode be the goal, so we try and allocate the EA inode
1402 * in the same group, or nearby one.
1403 */
1404 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001405 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001406 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001407 if (!IS_ERR(ea_inode)) {
1408 ea_inode->i_op = &ext4_file_inode_operations;
1409 ea_inode->i_fop = &ext4_file_operations;
1410 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001411 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001412 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001413 ext4_xattr_inode_set_ref(ea_inode, 1);
1414 ext4_xattr_inode_set_hash(ea_inode, hash);
1415 err = ext4_mark_inode_dirty(handle, ea_inode);
1416 if (!err)
1417 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001418 if (err) {
1419 iput(ea_inode);
1420 return ERR_PTR(err);
1421 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001422
1423 /*
1424 * Xattr inodes are shared therefore quota charging is performed
1425 * at a higher level.
1426 */
1427 dquot_free_inode(ea_inode);
1428 dquot_drop(ea_inode);
1429 inode_lock(ea_inode);
1430 ea_inode->i_flags |= S_NOQUOTA;
1431 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001432 }
1433
1434 return ea_inode;
1435}
1436
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001437static struct inode *
1438ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1439 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001440{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001441 struct inode *ea_inode;
1442 struct mb_cache_entry *ce;
1443 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1444 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001445
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001446 if (!ea_inode_cache)
1447 return NULL;
1448
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001449 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1450 if (!ce)
1451 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001452
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001453 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1454 if (!ea_data) {
1455 mb_cache_entry_put(ea_inode_cache, ce);
1456 return NULL;
1457 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001458
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001459 while (ce) {
1460 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1461 if (!IS_ERR(ea_inode) &&
1462 !is_bad_inode(ea_inode) &&
1463 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1464 i_size_read(ea_inode) == value_len &&
1465 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001466 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1467 value_len) &&
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001468 !memcmp(value, ea_data, value_len)) {
1469 mb_cache_entry_touch(ea_inode_cache, ce);
1470 mb_cache_entry_put(ea_inode_cache, ce);
1471 kvfree(ea_data);
1472 return ea_inode;
1473 }
1474
1475 if (!IS_ERR(ea_inode))
1476 iput(ea_inode);
1477 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1478 }
1479 kvfree(ea_data);
1480 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001481}
1482
1483/*
1484 * Add value of the EA in an inode.
1485 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001486static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1487 const void *value, size_t value_len,
1488 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001489{
1490 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001491 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001492 int err;
1493
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001494 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1495 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1496 if (ea_inode) {
1497 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1498 if (err) {
1499 iput(ea_inode);
1500 return err;
1501 }
1502
1503 *ret_inode = ea_inode;
1504 return 0;
1505 }
1506
Andreas Dilgere50e5122017-06-21 21:10:32 -04001507 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001508 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001509 if (IS_ERR(ea_inode))
1510 return PTR_ERR(ea_inode);
1511
1512 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001513 if (err) {
1514 ext4_xattr_inode_dec_ref(handle, ea_inode);
1515 iput(ea_inode);
1516 return err;
1517 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001518
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001519 if (EA_INODE_CACHE(inode))
1520 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1521 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001522
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001523 *ret_inode = ea_inode;
1524 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001525}
1526
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001527/*
1528 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1529 * feature is enabled.
1530 */
1531#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1532
Andreas Dilgere50e5122017-06-21 21:10:32 -04001533static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1534 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001535 handle_t *handle, struct inode *inode,
1536 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537{
Mingming Cao617ba132006-10-11 01:20:53 -07001538 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001539 struct ext4_xattr_entry *here = s->here;
1540 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001541 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001542 struct inode *old_ea_inode = NULL;
1543 struct inode *new_ea_inode = NULL;
1544 size_t old_size, new_size;
1545 int ret;
1546
1547 /* Space used by old and new values. */
1548 old_size = (!s->not_found && !here->e_value_inum) ?
1549 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1550 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1551
1552 /*
1553 * Optimization for the simple case when old and new values have the
1554 * same padded sizes. Not applicable if external inodes are involved.
1555 */
1556 if (new_size && new_size == old_size) {
1557 size_t offs = le16_to_cpu(here->e_value_offs);
1558 void *val = s->base + offs;
1559
1560 here->e_value_size = cpu_to_le32(i->value_len);
1561 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1562 memset(val, 0, new_size);
1563 } else {
1564 memcpy(val, i->value, i->value_len);
1565 /* Clear padding bytes. */
1566 memset(val + i->value_len, 0, new_size - i->value_len);
1567 }
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001568 goto update_hash;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001569 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001570
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571 /* Compute min_offs and last. */
1572 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001573 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001574 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001575 size_t offs = le16_to_cpu(last->e_value_offs);
1576 if (offs < min_offs)
1577 min_offs = offs;
1578 }
1579 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001580
1581 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001583 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001584
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001585 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1586 if (!s->not_found)
1587 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001588
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001589 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1590 ret = -ENOSPC;
1591 goto out;
1592 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001593
1594 /*
1595 * If storing the value in an external inode is an option,
1596 * reserve space for xattr entries/names in the external
1597 * attribute block so that a long value does not occupy the
1598 * whole space and prevent futher entries being added.
1599 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001600 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1601 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001602 (min_offs + old_size - new_size) <
1603 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1604 ret = -ENOSPC;
1605 goto out;
1606 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 }
1608
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001609 /*
1610 * Getting access to old and new ea inodes is subject to failures.
1611 * Finish that work before doing any modifications to the xattr data.
1612 */
1613 if (!s->not_found && here->e_value_inum) {
1614 ret = ext4_xattr_inode_iget(inode,
1615 le32_to_cpu(here->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001616 le32_to_cpu(here->e_hash),
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001617 &old_ea_inode);
1618 if (ret) {
1619 old_ea_inode = NULL;
1620 goto out;
1621 }
1622 }
1623 if (i->value && in_inode) {
1624 WARN_ON_ONCE(!i->value_len);
1625
1626 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1627 if (ret)
1628 goto out;
1629
1630 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1631 i->value_len,
1632 &new_ea_inode);
1633 if (ret) {
1634 new_ea_inode = NULL;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001635 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001636 goto out;
1637 }
1638 }
1639
1640 if (old_ea_inode) {
1641 /* We are ready to release ref count on the old_ea_inode. */
1642 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1643 if (ret) {
1644 /* Release newly required ref count on new_ea_inode. */
1645 if (new_ea_inode) {
1646 int err;
1647
1648 err = ext4_xattr_inode_dec_ref(handle,
1649 new_ea_inode);
1650 if (err)
1651 ext4_warning_inode(new_ea_inode,
1652 "dec ref new_ea_inode err=%d",
1653 err);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001654 ext4_xattr_inode_free_quota(inode, new_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001655 i->value_len);
1656 }
1657 goto out;
1658 }
1659
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001660 ext4_xattr_inode_free_quota(inode, old_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001661 le32_to_cpu(here->e_value_size));
1662 }
1663
1664 /* No failures allowed past this point. */
1665
1666 if (!s->not_found && here->e_value_offs) {
1667 /* Remove the old value. */
1668 void *first_val = s->base + min_offs;
1669 size_t offs = le16_to_cpu(here->e_value_offs);
1670 void *val = s->base + offs;
1671
1672 memmove(first_val + old_size, first_val, val - first_val);
1673 memset(first_val, 0, old_size);
1674 min_offs += old_size;
1675
1676 /* Adjust all value offsets. */
1677 last = s->first;
1678 while (!IS_LAST_ENTRY(last)) {
1679 size_t o = le16_to_cpu(last->e_value_offs);
1680
1681 if (!last->e_value_inum &&
1682 last->e_value_size && o < offs)
1683 last->e_value_offs = cpu_to_le16(o + old_size);
1684 last = EXT4_XATTR_NEXT(last);
1685 }
1686 }
1687
1688 if (!i->value) {
1689 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001690 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001691
1692 last = ENTRY((void *)last - size);
1693 memmove(here, (void *)here + size,
1694 (void *)last - (void *)here + sizeof(__u32));
1695 memset(last, 0, size);
1696 } else if (s->not_found) {
1697 /* Insert new name. */
1698 size_t size = EXT4_XATTR_LEN(name_len);
1699 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1700
1701 memmove((void *)here + size, here, rest);
1702 memset(here, 0, size);
1703 here->e_name_index = i->name_index;
1704 here->e_name_len = name_len;
1705 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001707 /* This is an update, reset value info. */
1708 here->e_value_inum = 0;
1709 here->e_value_offs = 0;
1710 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711 }
1712
1713 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001714 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001715 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001716 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001717 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001718 void *val = s->base + min_offs - new_size;
1719
1720 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001721 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001722 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001723 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001724 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001725 /* Clear padding bytes. */
1726 memset(val + i->value_len, 0,
1727 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001728 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001730 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001731 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001732
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001733update_hash:
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001734 if (i->value) {
1735 __le32 hash = 0;
1736
1737 /* Entry hash calculation. */
1738 if (in_inode) {
1739 __le32 crc32c_hash;
1740
1741 /*
1742 * Feed crc32c hash instead of the raw value for entry
1743 * hash calculation. This is to avoid walking
1744 * potentially long value buffer again.
1745 */
1746 crc32c_hash = cpu_to_le32(
1747 ext4_xattr_inode_get_hash(new_ea_inode));
1748 hash = ext4_xattr_hash_entry(here->e_name,
1749 here->e_name_len,
1750 &crc32c_hash, 1);
1751 } else if (is_block) {
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001752 __le32 *value = s->base + le16_to_cpu(
1753 here->e_value_offs);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001754
1755 hash = ext4_xattr_hash_entry(here->e_name,
1756 here->e_name_len, value,
1757 new_size >> 2);
1758 }
1759 here->e_hash = hash;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001760 }
1761
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001762 if (is_block)
1763 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1764
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001765 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001766out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001767 iput(old_ea_inode);
1768 iput(new_ea_inode);
1769 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770}
1771
Mingming Cao617ba132006-10-11 01:20:53 -07001772struct ext4_xattr_block_find {
1773 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 struct buffer_head *bh;
1775};
1776
1777static int
Mingming Cao617ba132006-10-11 01:20:53 -07001778ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1779 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780{
1781 struct super_block *sb = inode->i_sb;
1782 int error;
1783
1784 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1785 i->name_index, i->name, i->value, (long)i->value_len);
1786
Mingming Cao617ba132006-10-11 01:20:53 -07001787 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001789 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001790 error = -EIO;
1791 if (!bs->bh)
1792 goto cleanup;
1793 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1794 atomic_read(&(bs->bh->b_count)),
1795 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001796 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001797 EXT4_ERROR_INODE(inode, "bad block %llu",
1798 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001799 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800 goto cleanup;
1801 }
1802 /* Find the named attribute. */
1803 bs->s.base = BHDR(bs->bh);
1804 bs->s.first = BFIRST(bs->bh);
1805 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1806 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001807 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001808 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001809 if (error && error != -ENODATA)
1810 goto cleanup;
1811 bs->s.not_found = error;
1812 }
1813 error = 0;
1814
1815cleanup:
1816 return error;
1817}
1818
1819static int
Mingming Cao617ba132006-10-11 01:20:53 -07001820ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1821 struct ext4_xattr_info *i,
1822 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823{
1824 struct super_block *sb = inode->i_sb;
1825 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001826 struct ext4_xattr_search s_copy = bs->s;
1827 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001828 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001829 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001830 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001831 struct inode *ea_inode = NULL, *tmp_inode;
1832 size_t old_ea_inode_quota = 0;
1833 unsigned int ea_ino;
1834
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001835
Mingming Cao617ba132006-10-11 01:20:53 -07001836#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001839 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001840 error = ext4_journal_get_write_access(handle, bs->bh);
1841 if (error)
1842 goto cleanup;
1843 lock_buffer(bs->bh);
1844
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001846 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1847
1848 /*
1849 * This must happen under buffer lock for
1850 * ext4_xattr_block_set() to reliably detect modified
1851 * block
1852 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001853 if (ea_block_cache)
1854 mb_cache_entry_delete(ea_block_cache, hash,
1855 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001856 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001857 error = ext4_xattr_set_entry(i, s, handle, inode,
1858 true /* is_block */);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001859 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001861 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862 goto bad_block;
1863 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001864 error = ext4_handle_dirty_metadata(handle,
1865 inode,
1866 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001867 if (error)
1868 goto cleanup;
1869 goto inserted;
1870 } else {
1871 int offset = (char *)s->here - bs->bh->b_data;
1872
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001873 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001874 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001875 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 error = -ENOMEM;
1877 if (s->base == NULL)
1878 goto cleanup;
1879 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1880 s->first = ENTRY(header(s->base)+1);
1881 header(s->base)->h_refcount = cpu_to_le32(1);
1882 s->here = ENTRY(s->base + offset);
1883 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001884
1885 /*
1886 * If existing entry points to an xattr inode, we need
1887 * to prevent ext4_xattr_set_entry() from decrementing
1888 * ref count on it because the reference belongs to the
1889 * original block. In this case, make the entry look
1890 * like it has an empty value.
1891 */
1892 if (!s->not_found && s->here->e_value_inum) {
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001893 ea_ino = le32_to_cpu(s->here->e_value_inum);
1894 error = ext4_xattr_inode_iget(inode, ea_ino,
1895 le32_to_cpu(s->here->e_hash),
1896 &tmp_inode);
1897 if (error)
1898 goto cleanup;
1899
1900 if (!ext4_test_inode_state(tmp_inode,
1901 EXT4_STATE_LUSTRE_EA_INODE)) {
1902 /*
1903 * Defer quota free call for previous
1904 * inode until success is guaranteed.
1905 */
1906 old_ea_inode_quota = le32_to_cpu(
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001907 s->here->e_value_size);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001908 }
1909 iput(tmp_inode);
1910
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001911 s->here->e_value_inum = 0;
1912 s->here->e_value_size = 0;
1913 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914 }
1915 } else {
1916 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001917 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001918 /* assert(header == s->base) */
1919 error = -ENOMEM;
1920 if (s->base == NULL)
1921 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001922 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001923 header(s->base)->h_blocks = cpu_to_le32(1);
1924 header(s->base)->h_refcount = cpu_to_le32(1);
1925 s->first = ENTRY(header(s->base)+1);
1926 s->here = ENTRY(header(s->base)+1);
1927 s->end = s->base + sb->s_blocksize;
1928 }
1929
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001930 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001931 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001932 goto bad_block;
1933 if (error)
1934 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001935
1936 if (i->value && s->here->e_value_inum) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001937 /*
1938 * A ref count on ea_inode has been taken as part of the call to
1939 * ext4_xattr_set_entry() above. We would like to drop this
1940 * extra ref but we have to wait until the xattr block is
1941 * initialized and has its own ref count on the ea_inode.
1942 */
1943 ea_ino = le32_to_cpu(s->here->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001944 error = ext4_xattr_inode_iget(inode, ea_ino,
1945 le32_to_cpu(s->here->e_hash),
1946 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001947 if (error) {
1948 ea_inode = NULL;
1949 goto cleanup;
1950 }
1951 }
1952
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001953inserted:
1954 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001955 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1956 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001957 if (new_bh) {
1958 /* We found an identical block in the cache. */
1959 if (new_bh == bs->bh)
1960 ea_bdebug(new_bh, "keeping");
1961 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001962 u32 ref;
1963
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001964 WARN_ON_ONCE(dquot_initialize_needed(inode));
1965
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966 /* The old block is released after updating
1967 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001968 error = dquot_alloc_block(inode,
1969 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001970 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001972 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001973 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001974 new_bh);
1975 if (error)
1976 goto cleanup_dquot;
1977 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001978 /*
1979 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001980 * freeing, rehashing or adding references to
1981 * xattr block. Once we hold buffer lock xattr
1982 * block's state is stable so we can check
1983 * whether the block got freed / rehashed or
1984 * not. Since we unhash mbcache entry under
1985 * buffer lock when freeing / rehashing xattr
1986 * block, checking whether entry is still
1987 * hashed is reliable. Same rules hold for
1988 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001989 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001990 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1991 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001992 /*
1993 * Undo everything and check mbcache
1994 * again.
1995 */
1996 unlock_buffer(new_bh);
1997 dquot_free_block(inode,
1998 EXT4_C2B(EXT4_SB(sb),
1999 1));
2000 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002001 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05002002 ce = NULL;
2003 new_bh = NULL;
2004 goto inserted;
2005 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002006 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2007 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2008 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
2009 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002010 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002011 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002012 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002014 error = ext4_handle_dirty_metadata(handle,
2015 inode,
2016 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002017 if (error)
2018 goto cleanup_dquot;
2019 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04002020 mb_cache_entry_touch(ea_block_cache, ce);
2021 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022 ce = NULL;
2023 } else if (bs->bh && s->base == bs->bh->b_data) {
2024 /* We were modifying this block in-place. */
2025 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganec000222017-08-05 22:41:42 -04002026 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027 new_bh = bs->bh;
2028 get_bh(new_bh);
2029 } else {
2030 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002031 ext4_fsblk_t goal, block;
2032
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002033 WARN_ON_ONCE(dquot_initialize_needed(inode));
2034
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002035 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04002036 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002037
2038 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002039 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002040 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
2041
Allison Henderson55f020d2011-05-25 07:41:26 -04002042 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2043 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044 if (error)
2045 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002046
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002047 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002048 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
2049
Joe Perchesace36ad2012-03-19 23:11:43 -04002050 ea_idebug(inode, "creating block %llu",
2051 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052
2053 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05002054 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002055 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002056getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05002057 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002058 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059 goto cleanup;
2060 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002061 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2062 ENTRY(header(s->base)+1));
2063 if (error)
2064 goto getblk_failed;
2065 if (ea_inode) {
2066 /* Drop the extra ref on ea_inode. */
2067 error = ext4_xattr_inode_dec_ref(handle,
2068 ea_inode);
2069 if (error)
2070 ext4_warning_inode(ea_inode,
2071 "dec ref error=%d",
2072 error);
2073 iput(ea_inode);
2074 ea_inode = NULL;
2075 }
2076
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002078 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079 if (error) {
2080 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002081 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 goto getblk_failed;
2083 }
2084 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002085 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086 set_buffer_uptodate(new_bh);
2087 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002088 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002089 error = ext4_handle_dirty_metadata(handle, inode,
2090 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 if (error)
2092 goto cleanup;
2093 }
2094 }
2095
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002096 if (old_ea_inode_quota)
2097 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002098
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07002100 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101
2102 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002103 if (bs->bh && bs->bh != new_bh) {
2104 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2105
2106 ext4_xattr_release_block(handle, inode, bs->bh,
2107 &ea_inode_array,
2108 0 /* extra_credits */);
2109 ext4_xattr_inode_array_free(ea_inode_array);
2110 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002111 error = 0;
2112
2113cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002114 if (ea_inode) {
2115 int error2;
2116
2117 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2118 if (error2)
2119 ext4_warning_inode(ea_inode, "dec ref error=%d",
2120 error2);
2121
2122 /* If there was an error, revert the quota charge. */
2123 if (error)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002124 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002125 i_size_read(ea_inode));
2126 iput(ea_inode);
2127 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002129 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 brelse(new_bh);
2131 if (!(bs->bh && s->base == bs->bh->b_data))
2132 kfree(s->base);
2133
2134 return error;
2135
2136cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002137 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138 goto cleanup;
2139
2140bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002141 EXT4_ERROR_INODE(inode, "bad block %llu",
2142 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002143 goto cleanup;
2144
2145#undef header
2146}
2147
Tao Ma879b3822012-12-05 10:28:46 -05002148int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2149 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150{
Mingming Cao617ba132006-10-11 01:20:53 -07002151 struct ext4_xattr_ibody_header *header;
2152 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153 int error;
2154
Mingming Cao617ba132006-10-11 01:20:53 -07002155 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002156 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002157 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 header = IHDR(inode, raw_inode);
2159 is->s.base = is->s.first = IFIRST(header);
2160 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002161 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002162 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002163 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 if (error)
2165 return error;
2166 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07002167 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04002168 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 if (error && error != -ENODATA)
2170 return error;
2171 is->s.not_found = error;
2172 }
2173 return 0;
2174}
2175
Tao Ma0d812f72012-12-10 14:06:02 -05002176int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2177 struct ext4_xattr_info *i,
2178 struct ext4_xattr_ibody_find *is)
2179{
2180 struct ext4_xattr_ibody_header *header;
2181 struct ext4_xattr_search *s = &is->s;
2182 int error;
2183
2184 if (EXT4_I(inode)->i_extra_isize == 0)
2185 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002186 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002187 if (error) {
2188 if (error == -ENOSPC &&
2189 ext4_has_inline_data(inode)) {
2190 error = ext4_try_to_evict_inline_data(handle, inode,
2191 EXT4_XATTR_LEN(strlen(i->name) +
2192 EXT4_XATTR_SIZE(i->value_len)));
2193 if (error)
2194 return error;
2195 error = ext4_xattr_ibody_find(inode, i, is);
2196 if (error)
2197 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002198 error = ext4_xattr_set_entry(i, s, handle, inode,
2199 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002200 }
2201 if (error)
2202 return error;
2203 }
2204 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2205 if (!IS_LAST_ENTRY(s->first)) {
2206 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2207 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2208 } else {
2209 header->h_magic = cpu_to_le32(0);
2210 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2211 }
2212 return 0;
2213}
2214
Andreas Dilgere50e5122017-06-21 21:10:32 -04002215static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002216 struct ext4_xattr_info *i,
2217 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218{
Mingming Cao617ba132006-10-11 01:20:53 -07002219 struct ext4_xattr_ibody_header *header;
2220 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221 int error;
2222
Mingming Cao617ba132006-10-11 01:20:53 -07002223 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002225 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002226 if (error)
2227 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002228 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002230 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002231 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 } else {
2233 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002234 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 }
2236 return 0;
2237}
2238
Jan Kara3fd16462016-02-22 22:43:04 -05002239static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2240 struct ext4_xattr_info *i)
2241{
2242 void *value;
2243
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002244 /* When e_value_inum is set the value is stored externally. */
2245 if (s->here->e_value_inum)
2246 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002247 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2248 return 0;
2249 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2250 return !memcmp(value, i->value, i->value_len);
2251}
2252
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002253static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2254{
2255 struct buffer_head *bh;
2256 int error;
2257
2258 if (!EXT4_I(inode)->i_file_acl)
2259 return NULL;
2260 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2261 if (!bh)
2262 return ERR_PTR(-EIO);
2263 error = ext4_xattr_check_block(inode, bh);
2264 if (error)
2265 return ERR_PTR(error);
2266 return bh;
2267}
2268
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269/*
Mingming Cao617ba132006-10-11 01:20:53 -07002270 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002272 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002273 * is NULL to remove an existing extended attribute, and non-NULL to
2274 * either replace an existing extended attribute, or create a new extended
2275 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2276 * specify that an extended attribute must exist and must not exist
2277 * previous to the call, respectively.
2278 *
2279 * Returns 0, or a negative error number on failure.
2280 */
2281int
Mingming Cao617ba132006-10-11 01:20:53 -07002282ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283 const char *name, const void *value, size_t value_len,
2284 int flags)
2285{
Mingming Cao617ba132006-10-11 01:20:53 -07002286 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 .name_index = name_index,
2288 .name = name,
2289 .value = value,
2290 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002291 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002292 };
Mingming Cao617ba132006-10-11 01:20:53 -07002293 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002294 .s = { .not_found = -ENODATA, },
2295 };
Mingming Cao617ba132006-10-11 01:20:53 -07002296 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002297 .s = { .not_found = -ENODATA, },
2298 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002299 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300 int error;
2301
2302 if (!name)
2303 return -EINVAL;
2304 if (strlen(name) > 255)
2305 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002306
Theodore Ts'oc755e252017-01-11 21:50:46 -05002307 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002308
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002309 /* Check journal credits under write lock. */
2310 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002311 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002312 int credits;
2313
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002314 bh = ext4_xattr_get_block(inode);
2315 if (IS_ERR(bh)) {
2316 error = PTR_ERR(bh);
2317 goto cleanup;
2318 }
2319
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002320 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2321 value_len,
2322 flags & XATTR_CREATE);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002323 brelse(bh);
2324
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002325 if (!ext4_handle_has_enough_credits(handle, credits)) {
2326 error = -ENOSPC;
2327 goto cleanup;
2328 }
2329 }
2330
Eric Sandeen66543612011-10-26 03:32:07 -04002331 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002332 if (error)
2333 goto cleanup;
2334
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002335 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002336 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2337 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002338 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 }
2340
Mingming Cao617ba132006-10-11 01:20:53 -07002341 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002342 if (error)
2343 goto cleanup;
2344 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002345 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002346 if (error)
2347 goto cleanup;
2348 if (is.s.not_found && bs.s.not_found) {
2349 error = -ENODATA;
2350 if (flags & XATTR_REPLACE)
2351 goto cleanup;
2352 error = 0;
2353 if (!value)
2354 goto cleanup;
2355 } else {
2356 error = -EEXIST;
2357 if (flags & XATTR_CREATE)
2358 goto cleanup;
2359 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002360
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002361 if (!value) {
2362 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002363 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002365 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002366 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002367 error = 0;
2368 /* Xattr value did not change? Save us some work and bail out */
2369 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2370 goto cleanup;
2371 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2372 goto cleanup;
2373
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002374 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2375 (EXT4_XATTR_SIZE(i.value_len) >
2376 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2377 i.in_inode = 1;
2378retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002379 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380 if (!error && !bs.s.not_found) {
2381 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002382 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002384 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2385 error = ext4_xattr_block_find(inode, &i, &bs);
2386 if (error)
2387 goto cleanup;
2388 }
Mingming Cao617ba132006-10-11 01:20:53 -07002389 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002390 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002391 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002392 error = ext4_xattr_ibody_set(handle, inode, &i,
2393 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002394 } else if (error == -ENOSPC) {
2395 /*
2396 * Xattr does not fit in the block, store at
2397 * external inode if possible.
2398 */
2399 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2400 !i.in_inode) {
2401 i.in_inode = 1;
2402 goto retry_inode;
2403 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002404 }
2405 }
2406 }
2407 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002408 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002409 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002410 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002411 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002412 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002413 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002414 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002415 * error != 0.
2416 */
2417 is.iloc.bh = NULL;
2418 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002419 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420 }
2421
2422cleanup:
2423 brelse(is.iloc.bh);
2424 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002425 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426 return error;
2427}
2428
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002429int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2430 bool is_create, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002431{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002432 struct buffer_head *bh;
2433 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002434
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002435 *credits = 0;
2436
2437 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002438 return 0;
2439
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002440 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002441
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002442 bh = ext4_xattr_get_block(inode);
2443 if (IS_ERR(bh)) {
2444 err = PTR_ERR(bh);
2445 } else {
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002446 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2447 value_len, is_create);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002448 brelse(bh);
2449 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002450 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002451
2452 up_read(&EXT4_I(inode)->xattr_sem);
2453 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002454}
2455
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002456/*
Mingming Cao617ba132006-10-11 01:20:53 -07002457 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458 *
Mingming Cao617ba132006-10-11 01:20:53 -07002459 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002460 * attribute modification is a filesystem transaction by itself.
2461 *
2462 * Returns 0, or a negative error number on failure.
2463 */
2464int
Mingming Cao617ba132006-10-11 01:20:53 -07002465ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002466 const void *value, size_t value_len, int flags)
2467{
2468 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002469 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002470 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002471 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002472
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002473 error = dquot_initialize(inode);
2474 if (error)
2475 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002476
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002477retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002478 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2479 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002480 if (error)
2481 return error;
2482
Theodore Ts'o9924a922013-02-08 21:59:22 -05002483 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002484 if (IS_ERR(handle)) {
2485 error = PTR_ERR(handle);
2486 } else {
2487 int error2;
2488
Mingming Cao617ba132006-10-11 01:20:53 -07002489 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002491 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002492 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002493 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002494 goto retry;
2495 if (error == 0)
2496 error = error2;
2497 }
2498
2499 return error;
2500}
2501
2502/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002503 * Shift the EA entries in the inode to create space for the increased
2504 * i_extra_isize.
2505 */
2506static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2507 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002508 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002509{
2510 struct ext4_xattr_entry *last = entry;
2511 int new_offs;
2512
Jan Kara94405712016-08-29 15:41:11 -04002513 /* We always shift xattr headers further thus offsets get lower */
2514 BUG_ON(value_offs_shift > 0);
2515
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002516 /* Adjust the value offsets of the entries */
2517 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002518 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002519 new_offs = le16_to_cpu(last->e_value_offs) +
2520 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002521 last->e_value_offs = cpu_to_le16(new_offs);
2522 }
2523 }
2524 /* Shift the entries by n bytes */
2525 memmove(to, from, n);
2526}
2527
2528/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002529 * Move xattr pointed to by 'entry' from inode into external xattr block
2530 */
2531static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2532 struct ext4_inode *raw_inode,
2533 struct ext4_xattr_entry *entry)
2534{
2535 struct ext4_xattr_ibody_find *is = NULL;
2536 struct ext4_xattr_block_find *bs = NULL;
2537 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002538 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002539 struct ext4_xattr_info i = {
2540 .value = NULL,
2541 .value_len = 0,
2542 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002543 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002544 };
2545 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2546 int error;
2547
Jan Kara3f2571c2016-08-29 15:42:11 -04002548 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2549 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2550 buffer = kmalloc(value_size, GFP_NOFS);
2551 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2552 if (!is || !bs || !buffer || !b_entry_name) {
2553 error = -ENOMEM;
2554 goto out;
2555 }
2556
2557 is->s.not_found = -ENODATA;
2558 bs->s.not_found = -ENODATA;
2559 is->iloc.bh = NULL;
2560 bs->bh = NULL;
2561
2562 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002563 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04002564 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002565 if (error)
2566 goto out;
2567 } else {
2568 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2569 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2570 }
2571
Jan Kara3f2571c2016-08-29 15:42:11 -04002572 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2573 b_entry_name[entry->e_name_len] = '\0';
2574 i.name = b_entry_name;
2575
2576 error = ext4_get_inode_loc(inode, &is->iloc);
2577 if (error)
2578 goto out;
2579
2580 error = ext4_xattr_ibody_find(inode, &i, is);
2581 if (error)
2582 goto out;
2583
2584 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002585 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002586 if (error)
2587 goto out;
2588
Jan Kara3f2571c2016-08-29 15:42:11 -04002589 i.value = buffer;
2590 i.value_len = value_size;
2591 error = ext4_xattr_block_find(inode, &i, bs);
2592 if (error)
2593 goto out;
2594
2595 /* Add entry which was removed from the inode into the block */
2596 error = ext4_xattr_block_set(handle, inode, &i, bs);
2597 if (error)
2598 goto out;
2599 error = 0;
2600out:
2601 kfree(b_entry_name);
2602 kfree(buffer);
2603 if (is)
2604 brelse(is->iloc.bh);
2605 kfree(is);
2606 kfree(bs);
2607
2608 return error;
2609}
2610
Jan Karadfa20642016-08-29 15:44:11 -04002611static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2612 struct ext4_inode *raw_inode,
2613 int isize_diff, size_t ifree,
2614 size_t bfree, int *total_ino)
2615{
2616 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2617 struct ext4_xattr_entry *small_entry;
2618 struct ext4_xattr_entry *entry;
2619 struct ext4_xattr_entry *last;
2620 unsigned int entry_size; /* EA entry size */
2621 unsigned int total_size; /* EA entry size + value size */
2622 unsigned int min_total_size;
2623 int error;
2624
2625 while (isize_diff > ifree) {
2626 entry = NULL;
2627 small_entry = NULL;
2628 min_total_size = ~0U;
2629 last = IFIRST(header);
2630 /* Find the entry best suited to be pushed into EA block */
2631 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002632 total_size = EXT4_XATTR_LEN(last->e_name_len);
2633 if (!last->e_value_inum)
2634 total_size += EXT4_XATTR_SIZE(
2635 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002636 if (total_size <= bfree &&
2637 total_size < min_total_size) {
2638 if (total_size + ifree < isize_diff) {
2639 small_entry = last;
2640 } else {
2641 entry = last;
2642 min_total_size = total_size;
2643 }
2644 }
2645 }
2646
2647 if (entry == NULL) {
2648 if (small_entry == NULL)
2649 return -ENOSPC;
2650 entry = small_entry;
2651 }
2652
2653 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002654 total_size = entry_size;
2655 if (!entry->e_value_inum)
2656 total_size += EXT4_XATTR_SIZE(
2657 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002658 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2659 entry);
2660 if (error)
2661 return error;
2662
2663 *total_ino -= entry_size;
2664 ifree += total_size;
2665 bfree -= total_size;
2666 }
2667
2668 return 0;
2669}
2670
Jan Kara3f2571c2016-08-29 15:42:11 -04002671/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002672 * Expand an inode by new_extra_isize bytes when EAs are present.
2673 * Returns 0 on success or negative error number on failure.
2674 */
2675int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2676 struct ext4_inode *raw_inode, handle_t *handle)
2677{
2678 struct ext4_xattr_ibody_header *header;
Miao Xieb640b2c2017-08-06 00:55:48 -04002679 struct buffer_head *bh;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002680 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2681 static unsigned int mnt_count;
Jan Karae3014d12016-08-29 15:38:11 -04002682 size_t min_offs;
2683 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002684 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002685 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002686 int error = 0, tried_min_extra_isize = 0;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002687 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04002688 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002689
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002690retry:
Jan Karad0141192016-08-11 11:50:30 -04002691 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002692 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
Miao Xieb640b2c2017-08-06 00:55:48 -04002693 return 0;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002694
2695 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002696
2697 /*
2698 * Check if enough free space is available in the inode to shift the
2699 * entries ahead by new_extra_isize.
2700 */
2701
Jan Kara6e0cd082016-08-29 15:43:11 -04002702 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002703 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2704 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002705 total_ino = sizeof(struct ext4_xattr_ibody_header);
2706
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002707 error = xattr_check_inode(inode, header, end);
2708 if (error)
2709 goto cleanup;
2710
Jan Kara6e0cd082016-08-29 15:43:11 -04002711 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002712 if (ifree >= isize_diff)
2713 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002714
2715 /*
2716 * Enough free space isn't available in the inode, check if
2717 * EA block can hold new_extra_isize bytes.
2718 */
2719 if (EXT4_I(inode)->i_file_acl) {
2720 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2721 error = -EIO;
2722 if (!bh)
2723 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04002724 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002725 EXT4_ERROR_INODE(inode, "bad block %llu",
2726 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002727 error = -EFSCORRUPTED;
Miao Xieb640b2c2017-08-06 00:55:48 -04002728 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002729 goto cleanup;
2730 }
2731 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002732 end = bh->b_data + bh->b_size;
2733 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002734 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2735 NULL);
Miao Xieb640b2c2017-08-06 00:55:48 -04002736 brelse(bh);
Jan Karae3014d12016-08-29 15:38:11 -04002737 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002738 if (!tried_min_extra_isize && s_min_extra_isize) {
2739 tried_min_extra_isize++;
2740 new_extra_isize = s_min_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002741 goto retry;
2742 }
Jan Karadfa20642016-08-29 15:44:11 -04002743 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002744 goto cleanup;
2745 }
2746 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002747 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002748 }
2749
Jan Karadfa20642016-08-29 15:44:11 -04002750 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2751 isize_diff, ifree, bfree,
2752 &total_ino);
2753 if (error) {
2754 if (error == -ENOSPC && !tried_min_extra_isize &&
2755 s_min_extra_isize) {
2756 tried_min_extra_isize++;
2757 new_extra_isize = s_min_extra_isize;
Jan Karadfa20642016-08-29 15:44:11 -04002758 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002759 }
Jan Karadfa20642016-08-29 15:44:11 -04002760 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002761 }
Jan Karae3014d12016-08-29 15:38:11 -04002762shift:
2763 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002764 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002765 - new_extra_isize, (void *)raw_inode +
2766 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002767 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002768 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002769
2770cleanup:
Miao Xieb640b2c2017-08-06 00:55:48 -04002771 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
Miao Xiecf0a5e82017-08-06 00:40:01 -04002772 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2773 inode->i_ino);
2774 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2775 }
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002776 return error;
2777}
2778
Andreas Dilgere50e5122017-06-21 21:10:32 -04002779#define EIA_INCR 16 /* must be 2^n */
2780#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002781
2782/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002783 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002784 * contents copied over.
2785 */
2786static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002787ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2788 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002789{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002790 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002791 /*
2792 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002793 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002794 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002795 (*ea_inode_array) =
2796 kmalloc(offsetof(struct ext4_xattr_inode_array,
2797 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002798 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002799 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002800 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002801 (*ea_inode_array)->count = 0;
2802 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002803 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002804 struct ext4_xattr_inode_array *new_array = NULL;
2805 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002806
2807 /* if new_array is NULL, this is essentially offsetof() */
2808 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002809 offsetof(struct ext4_xattr_inode_array,
2810 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002811 GFP_NOFS);
2812 if (new_array == NULL)
2813 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002814 memcpy(new_array, *ea_inode_array,
2815 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2816 kfree(*ea_inode_array);
2817 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002818 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002819 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002820 return 0;
2821}
2822
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002823/*
Mingming Cao617ba132006-10-11 01:20:53 -07002824 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002826 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002827 * all entries and decrement reference on any xattr inodes associated with this
2828 * inode. This is called immediately before an inode is freed. We have exclusive
2829 * access to the inode. If an orphan inode is deleted it will also release its
2830 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002831 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002832int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2833 struct ext4_xattr_inode_array **ea_inode_array,
2834 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002835{
2836 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002837 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002838 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002839 struct ext4_xattr_entry *entry;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002840 struct inode *ea_inode;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002841 int error;
2842
2843 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2844 NULL /* bh */,
2845 false /* dirty */,
2846 false /* block_csum */);
2847 if (error) {
2848 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2849 goto cleanup;
2850 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002852 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2853 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002854
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002855 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002856 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002857 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002858 goto cleanup;
2859 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002860
2861 error = ext4_journal_get_write_access(handle, iloc.bh);
2862 if (error) {
2863 EXT4_ERROR_INODE(inode, "write access (error %d)",
2864 error);
2865 goto cleanup;
2866 }
2867
2868 header = IHDR(inode, ext4_raw_inode(&iloc));
2869 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2870 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2871 IFIRST(header),
2872 false /* block_csum */,
2873 ea_inode_array,
2874 extra_credits,
2875 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002876 }
2877
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002878 if (EXT4_I(inode)->i_file_acl) {
2879 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2880 if (!bh) {
2881 EXT4_ERROR_INODE(inode, "block %llu read error",
2882 EXT4_I(inode)->i_file_acl);
2883 error = -EIO;
2884 goto cleanup;
2885 }
2886 error = ext4_xattr_check_block(inode, bh);
2887 if (error) {
2888 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2889 EXT4_I(inode)->i_file_acl, error);
2890 goto cleanup;
2891 }
2892
2893 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2894 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002895 entry = EXT4_XATTR_NEXT(entry)) {
2896 if (!entry->e_value_inum)
2897 continue;
2898 error = ext4_xattr_inode_iget(inode,
2899 le32_to_cpu(entry->e_value_inum),
2900 le32_to_cpu(entry->e_hash),
2901 &ea_inode);
2902 if (error)
2903 continue;
2904 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002905 le32_to_cpu(entry->e_value_size));
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002906 iput(ea_inode);
2907 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002908
2909 }
2910
2911 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2912 extra_credits);
2913 /*
2914 * Update i_file_acl value in the same transaction that releases
2915 * block.
2916 */
2917 EXT4_I(inode)->i_file_acl = 0;
2918 error = ext4_mark_inode_dirty(handle, inode);
2919 if (error) {
2920 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2921 error);
2922 goto cleanup;
2923 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002924 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002925 error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002926cleanup:
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002927 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002928 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002929 return error;
2930}
2931
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002932void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002933{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002934 int idx;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002935
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002936 if (ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002937 return;
2938
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002939 for (idx = 0; idx < ea_inode_array->count; ++idx)
2940 iput(ea_inode_array->inodes[idx]);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002941 kfree(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002942}
2943
2944/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002945 * ext4_xattr_block_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002946 *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002947 * Create a new entry in the extended attribute block cache, and insert
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002948 * it unless such an entry is already in the cache.
2949 *
2950 * Returns 0, or a negative error number on failure.
2951 */
2952static void
Tahsin Erdogan47387402017-06-22 11:28:55 -04002953ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2954 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002956 struct ext4_xattr_header *header = BHDR(bh);
2957 __u32 hash = le32_to_cpu(header->h_hash);
2958 int reusable = le32_to_cpu(header->h_refcount) <
2959 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002960 int error;
2961
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002962 if (!ea_block_cache)
2963 return;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002964 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002965 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002966 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002967 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002968 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002969 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002970 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002971}
2972
2973/*
Mingming Cao617ba132006-10-11 01:20:53 -07002974 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002975 *
2976 * Compare two extended attribute blocks for equality.
2977 *
2978 * Returns 0 if the blocks are equal, 1 if they differ, and
2979 * a negative error number on errors.
2980 */
2981static int
Mingming Cao617ba132006-10-11 01:20:53 -07002982ext4_xattr_cmp(struct ext4_xattr_header *header1,
2983 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984{
Mingming Cao617ba132006-10-11 01:20:53 -07002985 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986
2987 entry1 = ENTRY(header1+1);
2988 entry2 = ENTRY(header2+1);
2989 while (!IS_LAST_ENTRY(entry1)) {
2990 if (IS_LAST_ENTRY(entry2))
2991 return 1;
2992 if (entry1->e_hash != entry2->e_hash ||
2993 entry1->e_name_index != entry2->e_name_index ||
2994 entry1->e_name_len != entry2->e_name_len ||
2995 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002996 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002997 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2998 return 1;
Tahsin Erdogan7cec1912017-06-21 22:14:30 -04002999 if (!entry1->e_value_inum &&
3000 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003001 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3002 le32_to_cpu(entry1->e_value_size)))
3003 return 1;
3004
Mingming Cao617ba132006-10-11 01:20:53 -07003005 entry1 = EXT4_XATTR_NEXT(entry1);
3006 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003007 }
3008 if (!IS_LAST_ENTRY(entry2))
3009 return 1;
3010 return 0;
3011}
3012
3013/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04003014 * ext4_xattr_block_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003015 *
3016 * Find an identical extended attribute block.
3017 *
3018 * Returns a pointer to the block found, or NULL if such a block was
3019 * not found or an error occurred.
3020 */
3021static struct buffer_head *
Tahsin Erdogan47387402017-06-22 11:28:55 -04003022ext4_xattr_block_cache_find(struct inode *inode,
3023 struct ext4_xattr_header *header,
3024 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003025{
3026 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05003027 struct mb_cache_entry *ce;
Tahsin Erdogan47387402017-06-22 11:28:55 -04003028 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04003030 if (!ea_block_cache)
3031 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003032 if (!header->h_hash)
3033 return NULL; /* never share */
3034 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003035 ce = mb_cache_entry_find_first(ea_block_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036 while (ce) {
3037 struct buffer_head *bh;
3038
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003039 bh = sb_bread(inode->i_sb, ce->e_value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003040 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003041 EXT4_ERROR_INODE(inode, "block %lu read error",
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003042 (unsigned long)ce->e_value);
Mingming Cao617ba132006-10-11 01:20:53 -07003043 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003044 *pce = ce;
3045 return bh;
3046 }
3047 brelse(bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003048 ce = mb_cache_entry_find_next(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003049 }
3050 return NULL;
3051}
3052
3053#define NAME_HASH_SHIFT 5
3054#define VALUE_HASH_SHIFT 16
3055
3056/*
Mingming Cao617ba132006-10-11 01:20:53 -07003057 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058 *
3059 * Compute the hash of an extended attribute.
3060 */
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003061static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3062 size_t value_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003063{
3064 __u32 hash = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003066 while (name_len--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003067 hash = (hash << NAME_HASH_SHIFT) ^
3068 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3069 *name++;
3070 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003071 while (value_count--) {
3072 hash = (hash << VALUE_HASH_SHIFT) ^
3073 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3074 le32_to_cpu(*value++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003076 return cpu_to_le32(hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003077}
3078
3079#undef NAME_HASH_SHIFT
3080#undef VALUE_HASH_SHIFT
3081
3082#define BLOCK_HASH_SHIFT 16
3083
3084/*
Mingming Cao617ba132006-10-11 01:20:53 -07003085 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003086 *
3087 * Re-compute the extended attribute hash value after an entry has changed.
3088 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04003089static void ext4_xattr_rehash(struct ext4_xattr_header *header)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090{
Mingming Cao617ba132006-10-11 01:20:53 -07003091 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003092 __u32 hash = 0;
3093
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003094 here = ENTRY(header+1);
3095 while (!IS_LAST_ENTRY(here)) {
3096 if (!here->e_hash) {
3097 /* Block is not shared if an entry's hash value == 0 */
3098 hash = 0;
3099 break;
3100 }
3101 hash = (hash << BLOCK_HASH_SHIFT) ^
3102 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3103 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07003104 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003105 }
3106 header->h_hash = cpu_to_le32(hash);
3107}
3108
3109#undef BLOCK_HASH_SHIFT
3110
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003111#define HASH_BUCKET_BITS 10
3112
Jan Kara7a2508e2016-02-22 22:35:22 -05003113struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05003114ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003115{
Jan Kara7a2508e2016-02-22 22:35:22 -05003116 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003117}
3118
Jan Kara7a2508e2016-02-22 22:35:22 -05003119void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003121 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05003122 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003123}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003124