blob: 218a7ba57819411cd9dd12271a0005e963157dbb [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>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040059#include "ext4_jbd2.h"
60#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061#include "xattr.h"
62#include "acl.h"
63
Mingming Cao617ba132006-10-11 01:20:53 -070064#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040065# define ea_idebug(inode, fmt, ...) \
66 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
67 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
68# define ea_bdebug(bh, fmt, ...) \
69 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
70 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070071#else
Joe Perchesace36ad2012-03-19 23:11:43 -040072# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
73# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070074#endif
75
Tahsin Erdogan47387402017-06-22 11:28:55 -040076static void ext4_xattr_block_cache_insert(struct mb_cache *,
77 struct buffer_head *);
78static struct buffer_head *
79ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
80 struct mb_cache_entry **);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -040081static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
82 size_t value_count);
Tahsin Erdogandaf83282017-06-22 11:52:03 -040083static void ext4_xattr_rehash(struct ext4_xattr_header *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084
Eric Biggersd6006182017-04-29 23:47:50 -040085static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070086 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040087#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080088 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
89 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070090#endif
Mingming Cao617ba132006-10-11 01:20:53 -070091 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040092#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070093 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070094#endif
95};
96
Stephen Hemminger11e27522010-05-13 17:53:18 -070097const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070098 &ext4_xattr_user_handler,
99 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400100#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800101 &posix_acl_access_xattr_handler,
102 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400104#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700105 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700106#endif
107 NULL
108};
109
Tahsin Erdogan47387402017-06-22 11:28:55 -0400110#define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
111 inode->i_sb->s_fs_info)->s_ea_block_cache)
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400112
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400113#define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
114 inode->i_sb->s_fs_info)->s_ea_inode_cache)
115
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400116static int
117ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
118 struct inode *inode);
119
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400120#ifdef CONFIG_LOCKDEP
121void ext4_xattr_inode_set_class(struct inode *ea_inode)
122{
123 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
124}
125#endif
126
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400127static __le32 ext4_xattr_block_csum(struct inode *inode,
128 sector_t block_nr,
129 struct ext4_xattr_header *hdr)
130{
131 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400132 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400133 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400134 __u32 dummy_csum = 0;
135 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400136
Theodore Ts'od6a77102013-04-09 23:59:55 -0400137 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
138 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400139 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
140 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
141 offset += sizeof(dummy_csum);
142 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
143 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400144
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400145 return cpu_to_le32(csum);
146}
147
148static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400149 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400150{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400151 struct ext4_xattr_header *hdr = BHDR(bh);
152 int ret = 1;
153
154 if (ext4_has_metadata_csum(inode->i_sb)) {
155 lock_buffer(bh);
156 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
157 bh->b_blocknr, hdr));
158 unlock_buffer(bh);
159 }
160 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400161}
162
163static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400164 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400165{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400166 if (ext4_has_metadata_csum(inode->i_sb))
167 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
168 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400169}
170
Stephen Hemminger11e27522010-05-13 17:53:18 -0700171static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700172ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700174 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175
Mingming Cao617ba132006-10-11 01:20:53 -0700176 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
177 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178 return handler;
179}
180
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400182ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
183 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400185 struct ext4_xattr_entry *e = entry;
186
Eric Biggersd7614cc2016-12-01 14:57:29 -0500187 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400188 while (!IS_LAST_ENTRY(e)) {
189 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400191 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400192 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400194
Eric Biggersd7614cc2016-12-01 14:57:29 -0500195 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400196 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400197 if (entry->e_value_size != 0 &&
198 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500199 u16 offs = le16_to_cpu(entry->e_value_offs);
200 u32 size = le32_to_cpu(entry->e_value_size);
201 void *value;
202
203 /*
204 * The value cannot overlap the names, and the value
205 * with padding cannot extend beyond 'end'. Check both
206 * the padded and unpadded sizes, since the size may
207 * overflow to 0 when adding padding.
208 */
209 if (offs > end - value_start)
210 return -EFSCORRUPTED;
211 value = value_start + offs;
212 if (value < (void *)e + sizeof(u32) ||
213 size > end - value ||
214 EXT4_XATTR_SIZE(size) > end - value)
215 return -EFSCORRUPTED;
216 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400217 entry = EXT4_XATTR_NEXT(entry);
218 }
219
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 return 0;
221}
222
223static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400224ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400226 int error;
227
228 if (buffer_verified(bh))
229 return 0;
230
Mingming Cao617ba132006-10-11 01:20:53 -0700231 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400233 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400234 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400235 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400236 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
237 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400238 if (!error)
239 set_buffer_verified(bh);
240 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241}
242
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400243static int
244__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
245 void *end, const char *function, unsigned int line)
246{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400247 int error = -EFSCORRUPTED;
248
Eric Biggers290ab232016-12-01 14:51:58 -0500249 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400250 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400251 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400252 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400253errout:
254 if (error)
255 __ext4_error_inode(inode, function, line, 0,
256 "corrupted in-inode xattr");
257 return error;
258}
259
260#define xattr_check_inode(inode, header, end) \
261 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
262
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263static int
Mingming Cao617ba132006-10-11 01:20:53 -0700264ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400265 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266{
Mingming Cao617ba132006-10-11 01:20:53 -0700267 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 size_t name_len;
269 int cmp = 1;
270
271 if (name == NULL)
272 return -EINVAL;
273 name_len = strlen(name);
274 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700275 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 cmp = name_index - entry->e_name_index;
277 if (!cmp)
278 cmp = name_len - entry->e_name_len;
279 if (!cmp)
280 cmp = memcmp(name, entry->e_name, name_len);
281 if (cmp <= 0 && (sorted || cmp == 0))
282 break;
283 }
284 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 return cmp ? -ENODATA : 0;
286}
287
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400288static u32
289ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
290{
291 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
292}
293
294static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
295{
296 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
297 ((u32)ea_inode->i_version);
298}
299
300static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
301{
302 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
303 ea_inode->i_version = (u32)ref_count;
304}
305
306static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
307{
308 return (u32)ea_inode->i_atime.tv_sec;
309}
310
311static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
312{
313 ea_inode->i_atime.tv_sec = hash;
314}
315
Andreas Dilgere50e5122017-06-21 21:10:32 -0400316/*
317 * Read the EA value from an inode.
318 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400319static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400320{
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400321 int blocksize = 1 << ea_inode->i_blkbits;
322 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
323 int tail_size = (size % blocksize) ?: blocksize;
324 struct buffer_head *bhs_inline[8];
325 struct buffer_head **bhs = bhs_inline;
326 int i, ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400327
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400328 if (bh_count > ARRAY_SIZE(bhs_inline)) {
329 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
330 if (!bhs)
331 return -ENOMEM;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400332 }
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400333
334 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
335 true /* wait */, bhs);
336 if (ret)
337 goto free_bhs;
338
339 for (i = 0; i < bh_count; i++) {
340 /* There shouldn't be any holes in ea_inode. */
341 if (!bhs[i]) {
342 ret = -EFSCORRUPTED;
343 goto put_bhs;
344 }
345 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
346 i < bh_count - 1 ? blocksize : tail_size);
347 }
348 ret = 0;
349put_bhs:
350 for (i = 0; i < bh_count; i++)
351 brelse(bhs[i]);
352free_bhs:
353 if (bhs != bhs_inline)
354 kfree(bhs);
355 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400356}
357
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400358#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
359
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400360static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400361 u32 ea_inode_hash, struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400362{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400363 struct inode *inode;
364 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400365
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400366 inode = ext4_iget(parent->i_sb, ea_ino);
367 if (IS_ERR(inode)) {
368 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400369 ext4_error(parent->i_sb,
370 "error while reading EA inode %lu err=%d", ea_ino,
371 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400372 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400373 }
374
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400375 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400376 ext4_error(parent->i_sb,
377 "error while reading EA inode %lu is_bad_inode",
378 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400379 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400380 goto error;
381 }
382
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400383 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400384 ext4_error(parent->i_sb,
385 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
386 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400387 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400388 goto error;
389 }
390
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400391 ext4_xattr_inode_set_class(inode);
392
393 /*
394 * Check whether this is an old Lustre-style xattr inode. Lustre
395 * implementation does not have hash validation, rather it has a
396 * backpointer from ea_inode to the parent inode.
397 */
398 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
399 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
400 inode->i_generation == parent->i_generation) {
401 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
402 ext4_xattr_inode_set_ref(inode, 1);
403 } else {
404 inode_lock(inode);
405 inode->i_flags |= S_NOQUOTA;
406 inode_unlock(inode);
407 }
408
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400409 *ea_inode = inode;
410 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400411error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400412 iput(inode);
413 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400414}
415
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400416static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400417ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
418 struct ext4_xattr_entry *entry, void *buffer,
419 size_t size)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400420{
421 u32 hash;
422
423 /* Verify stored hash matches calculated hash. */
424 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
425 if (hash != ext4_xattr_inode_get_hash(ea_inode))
426 return -EFSCORRUPTED;
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400427
428 if (entry) {
429 __le32 e_hash, tmp_data;
430
431 /* Verify entry hash. */
432 tmp_data = cpu_to_le32(hash);
433 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
434 &tmp_data, 1);
435 if (e_hash != entry->e_hash)
436 return -EFSCORRUPTED;
437 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400438 return 0;
439}
440
Andreas Dilgere50e5122017-06-21 21:10:32 -0400441/*
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400442 * Read xattr value from the EA inode.
Andreas Dilgere50e5122017-06-21 21:10:32 -0400443 */
444static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400445ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
446 void *buffer, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400447{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400448 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400449 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400450 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400451
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400452 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400453 le32_to_cpu(entry->e_hash), &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400454 if (err) {
455 ea_inode = NULL;
456 goto out;
457 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400458
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400459 if (i_size_read(ea_inode) != size) {
460 ext4_warning_inode(ea_inode,
461 "ea_inode file size=%llu entry size=%zu",
462 i_size_read(ea_inode), size);
463 err = -EFSCORRUPTED;
464 goto out;
465 }
466
467 err = ext4_xattr_inode_read(ea_inode, buffer, size);
468 if (err)
469 goto out;
470
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400471 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
472 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
473 size);
474 if (err) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400475 ext4_warning_inode(ea_inode,
476 "EA inode hash validation failed");
477 goto out;
478 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400479
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400480 if (ea_inode_cache)
481 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
482 ext4_xattr_inode_get_hash(ea_inode),
483 ea_inode->i_ino, true /* reusable */);
484 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400485out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400486 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400487 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400488}
489
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490static int
Mingming Cao617ba132006-10-11 01:20:53 -0700491ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 void *buffer, size_t buffer_size)
493{
494 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700495 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 size_t size;
497 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400498 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499
500 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
501 name_index, name, buffer, (long)buffer_size);
502
503 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700504 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400506 ea_idebug(inode, "reading block %llu",
507 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700508 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 if (!bh)
510 goto cleanup;
511 ea_bdebug(bh, "b_count=%d, refcount=%d",
512 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400513 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400514 EXT4_ERROR_INODE(inode, "bad block %llu",
515 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400516 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 goto cleanup;
518 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400519 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400521 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 if (error)
523 goto cleanup;
524 size = le32_to_cpu(entry->e_value_size);
525 if (buffer) {
526 error = -ERANGE;
527 if (size > buffer_size)
528 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400529 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400530 error = ext4_xattr_inode_get(inode, entry, buffer,
531 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400532 if (error)
533 goto cleanup;
534 } else {
535 memcpy(buffer, bh->b_data +
536 le16_to_cpu(entry->e_value_offs), size);
537 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 }
539 error = size;
540
541cleanup:
542 brelse(bh);
543 return error;
544}
545
Tao Ma879b3822012-12-05 10:28:46 -0500546int
Mingming Cao617ba132006-10-11 01:20:53 -0700547ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 void *buffer, size_t buffer_size)
549{
Mingming Cao617ba132006-10-11 01:20:53 -0700550 struct ext4_xattr_ibody_header *header;
551 struct ext4_xattr_entry *entry;
552 struct ext4_inode *raw_inode;
553 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 size_t size;
555 void *end;
556 int error;
557
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500558 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700560 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 if (error)
562 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700563 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700565 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400566 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567 if (error)
568 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400569 entry = IFIRST(header);
570 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571 if (error)
572 goto cleanup;
573 size = le32_to_cpu(entry->e_value_size);
574 if (buffer) {
575 error = -ERANGE;
576 if (size > buffer_size)
577 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400578 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400579 error = ext4_xattr_inode_get(inode, entry, buffer,
580 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400581 if (error)
582 goto cleanup;
583 } else {
584 memcpy(buffer, (void *)IFIRST(header) +
585 le16_to_cpu(entry->e_value_offs), size);
586 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587 }
588 error = size;
589
590cleanup:
591 brelse(iloc.bh);
592 return error;
593}
594
595/*
Mingming Cao617ba132006-10-11 01:20:53 -0700596 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 *
598 * Copy an extended attribute into the buffer
599 * provided, or compute the buffer size required.
600 * Buffer is NULL to compute the size of the buffer required.
601 *
602 * Returns a negative error number on failure, or the number of bytes
603 * used / required on success.
604 */
605int
Mingming Cao617ba132006-10-11 01:20:53 -0700606ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 void *buffer, size_t buffer_size)
608{
609 int error;
610
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500611 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
612 return -EIO;
613
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400614 if (strlen(name) > 255)
615 return -ERANGE;
616
Mingming Cao617ba132006-10-11 01:20:53 -0700617 down_read(&EXT4_I(inode)->xattr_sem);
618 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 buffer_size);
620 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700621 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700623 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 return error;
625}
626
627static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000628ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629 char *buffer, size_t buffer_size)
630{
631 size_t rest = buffer_size;
632
Mingming Cao617ba132006-10-11 01:20:53 -0700633 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700634 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700635 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100637 if (handler && (!handler->list || handler->list(dentry))) {
638 const char *prefix = handler->prefix ?: handler->name;
639 size_t prefix_len = strlen(prefix);
640 size_t size = prefix_len + entry->e_name_len + 1;
641
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700642 if (buffer) {
643 if (size > rest)
644 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100645 memcpy(buffer, prefix, prefix_len);
646 buffer += prefix_len;
647 memcpy(buffer, entry->e_name, entry->e_name_len);
648 buffer += entry->e_name_len;
649 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 }
651 rest -= size;
652 }
653 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100654 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655}
656
657static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000658ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659{
David Howells2b0143b2015-03-17 22:25:59 +0000660 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 struct buffer_head *bh = NULL;
662 int error;
663
664 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
665 buffer, (long)buffer_size);
666
667 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700668 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400670 ea_idebug(inode, "reading block %llu",
671 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700672 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 error = -EIO;
674 if (!bh)
675 goto cleanup;
676 ea_bdebug(bh, "b_count=%d, refcount=%d",
677 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400678 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400679 EXT4_ERROR_INODE(inode, "bad block %llu",
680 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400681 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 goto cleanup;
683 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400684 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000685 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686
687cleanup:
688 brelse(bh);
689
690 return error;
691}
692
693static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000694ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695{
David Howells2b0143b2015-03-17 22:25:59 +0000696 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700697 struct ext4_xattr_ibody_header *header;
698 struct ext4_inode *raw_inode;
699 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 void *end;
701 int error;
702
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500703 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700705 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 if (error)
707 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700708 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700710 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400711 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712 if (error)
713 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000714 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 buffer, buffer_size);
716
717cleanup:
718 brelse(iloc.bh);
719 return error;
720}
721
722/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400723 * Inode operation listxattr()
724 *
725 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 *
727 * Copy a list of attribute names into the buffer
728 * provided, or compute the buffer size required.
729 * Buffer is NULL to compute the size of the buffer required.
730 *
731 * Returns a negative error number on failure, or the number of bytes
732 * used / required on success.
733 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400734ssize_t
735ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500737 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738
David Howells2b0143b2015-03-17 22:25:59 +0000739 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500740 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
741 if (ret < 0)
742 goto errout;
743 if (buffer) {
744 buffer += ret;
745 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500747 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
748 if (ret < 0)
749 goto errout;
750 ret += ret2;
751errout:
David Howells2b0143b2015-03-17 22:25:59 +0000752 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500753 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754}
755
756/*
Mingming Cao617ba132006-10-11 01:20:53 -0700757 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 * not set, set it.
759 */
Mingming Cao617ba132006-10-11 01:20:53 -0700760static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 struct super_block *sb)
762{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400763 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764 return;
765
liang xie5d601252014-05-12 22:06:43 -0400766 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700767 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400768 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400769 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771}
772
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400773int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
774{
775 struct ext4_iloc iloc = { .bh = NULL };
776 struct buffer_head *bh = NULL;
777 struct ext4_inode *raw_inode;
778 struct ext4_xattr_ibody_header *header;
779 struct ext4_xattr_entry *entry;
780 qsize_t ea_inode_refs = 0;
781 void *end;
782 int ret;
783
784 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
785
786 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
787 ret = ext4_get_inode_loc(inode, &iloc);
788 if (ret)
789 goto out;
790 raw_inode = ext4_raw_inode(&iloc);
791 header = IHDR(inode, raw_inode);
792 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
793 ret = xattr_check_inode(inode, header, end);
794 if (ret)
795 goto out;
796
797 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
798 entry = EXT4_XATTR_NEXT(entry))
799 if (entry->e_value_inum)
800 ea_inode_refs++;
801 }
802
803 if (EXT4_I(inode)->i_file_acl) {
804 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
805 if (!bh) {
806 ret = -EIO;
807 goto out;
808 }
809
810 if (ext4_xattr_check_block(inode, bh)) {
811 ret = -EFSCORRUPTED;
812 goto out;
813 }
814
815 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
816 entry = EXT4_XATTR_NEXT(entry))
817 if (entry->e_value_inum)
818 ea_inode_refs++;
819 }
820 *usage = ea_inode_refs + 1;
821 ret = 0;
822out:
823 brelse(iloc.bh);
824 brelse(bh);
825 return ret;
826}
827
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400828static inline size_t round_up_cluster(struct inode *inode, size_t length)
829{
830 struct super_block *sb = inode->i_sb;
831 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
832 inode->i_blkbits);
833 size_t mask = ~(cluster_size - 1);
834
835 return (length + cluster_size - 1) & mask;
836}
837
838static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
839{
840 int err;
841
842 err = dquot_alloc_inode(inode);
843 if (err)
844 return err;
845 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
846 if (err)
847 dquot_free_inode(inode);
848 return err;
849}
850
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400851static void ext4_xattr_inode_free_quota(struct inode *parent,
852 struct inode *ea_inode,
853 size_t len)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400854{
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400855 if (ea_inode &&
856 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
857 return;
858 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
859 dquot_free_inode(parent);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400860}
861
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400862int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
863 struct buffer_head *block_bh, size_t value_len,
864 bool is_create)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400865{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400866 int credits;
867 int blocks;
868
869 /*
870 * 1) Owner inode update
871 * 2) Ref count update on old xattr block
872 * 3) new xattr block
873 * 4) block bitmap update for new xattr block
874 * 5) group descriptor for new xattr block
875 * 6) block bitmap update for old xattr block
876 * 7) group descriptor for old block
877 *
878 * 6 & 7 can happen if we have two racing threads T_a and T_b
879 * which are each trying to set an xattr on inodes I_a and I_b
880 * which were both initially sharing an xattr block.
881 */
882 credits = 7;
883
884 /* Quota updates. */
885 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
886
887 /*
888 * In case of inline data, we may push out the data to a block,
889 * so we need to reserve credits for this eventuality
890 */
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400891 if (inode && ext4_has_inline_data(inode))
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400892 credits += ext4_writepage_trans_blocks(inode) + 1;
893
894 /* We are done if ea_inode feature is not enabled. */
895 if (!ext4_has_feature_ea_inode(sb))
896 return credits;
897
898 /* New ea_inode, inode map, block bitmap, group descriptor. */
899 credits += 4;
900
901 /* Data blocks. */
902 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
903
904 /* Indirection block or one level of extent tree. */
905 blocks += 1;
906
907 /* Block bitmap and group descriptor updates for each block. */
908 credits += blocks * 2;
909
910 /* Blocks themselves. */
911 credits += blocks;
912
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400913 if (!is_create) {
914 /* Dereference ea_inode holding old xattr value.
915 * Old ea_inode, inode map, block bitmap, group descriptor.
916 */
917 credits += 4;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400918
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400919 /* Data blocks for old ea_inode. */
920 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400921
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400922 /* Indirection block or one level of extent tree for old
923 * ea_inode.
924 */
925 blocks += 1;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400926
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400927 /* Block bitmap and group descriptor updates for each block. */
928 credits += blocks * 2;
929 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400930
931 /* We may need to clone the existing xattr block in which case we need
932 * to increment ref counts for existing ea_inodes referenced by it.
933 */
934 if (block_bh) {
935 struct ext4_xattr_entry *entry = BFIRST(block_bh);
936
937 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
938 if (entry->e_value_inum)
939 /* Ref count update on ea_inode. */
940 credits += 1;
941 }
942 return credits;
943}
944
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400945static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
946 int credits, struct buffer_head *bh,
947 bool dirty, bool block_csum)
948{
949 int error;
950
951 if (!ext4_handle_valid(handle))
952 return 0;
953
954 if (handle->h_buffer_credits >= credits)
955 return 0;
956
957 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
958 if (!error)
959 return 0;
960 if (error < 0) {
961 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
962 return error;
963 }
964
965 if (bh && dirty) {
966 if (block_csum)
967 ext4_xattr_block_csum_set(inode, bh);
968 error = ext4_handle_dirty_metadata(handle, NULL, bh);
969 if (error) {
970 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
971 error);
972 return error;
973 }
974 }
975
976 error = ext4_journal_restart(handle, credits);
977 if (error) {
978 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
979 return error;
980 }
981
982 if (bh) {
983 error = ext4_journal_get_write_access(handle, bh);
984 if (error) {
985 ext4_warning(inode->i_sb,
986 "Get write access failed (error %d)",
987 error);
988 return error;
989 }
990 }
991 return 0;
992}
993
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400994static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
995 int ref_change)
996{
997 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
998 struct ext4_iloc iloc;
999 s64 ref_count;
1000 u32 hash;
1001 int ret;
1002
1003 inode_lock(ea_inode);
1004
1005 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1006 if (ret) {
1007 iloc.bh = NULL;
1008 goto out;
1009 }
1010
1011 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1012 ref_count += ref_change;
1013 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1014
1015 if (ref_change > 0) {
1016 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1017 ea_inode->i_ino, ref_count);
1018
1019 if (ref_count == 1) {
1020 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1021 ea_inode->i_ino, ea_inode->i_nlink);
1022
1023 set_nlink(ea_inode, 1);
1024 ext4_orphan_del(handle, ea_inode);
1025
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001026 if (ea_inode_cache) {
1027 hash = ext4_xattr_inode_get_hash(ea_inode);
1028 mb_cache_entry_create(ea_inode_cache,
1029 GFP_NOFS, hash,
1030 ea_inode->i_ino,
1031 true /* reusable */);
1032 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001033 }
1034 } else {
1035 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1036 ea_inode->i_ino, ref_count);
1037
1038 if (ref_count == 0) {
1039 WARN_ONCE(ea_inode->i_nlink != 1,
1040 "EA inode %lu i_nlink=%u",
1041 ea_inode->i_ino, ea_inode->i_nlink);
1042
1043 clear_nlink(ea_inode);
1044 ext4_orphan_add(handle, ea_inode);
1045
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001046 if (ea_inode_cache) {
1047 hash = ext4_xattr_inode_get_hash(ea_inode);
1048 mb_cache_entry_delete(ea_inode_cache, hash,
1049 ea_inode->i_ino);
1050 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001051 }
1052 }
1053
1054 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1055 iloc.bh = NULL;
1056 if (ret)
1057 ext4_warning_inode(ea_inode,
1058 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1059out:
1060 brelse(iloc.bh);
1061 inode_unlock(ea_inode);
1062 return ret;
1063}
1064
1065static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1066{
1067 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1068}
1069
1070static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1071{
1072 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1073}
1074
1075static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1076 struct ext4_xattr_entry *first)
1077{
1078 struct inode *ea_inode;
1079 struct ext4_xattr_entry *entry;
1080 struct ext4_xattr_entry *failed_entry;
1081 unsigned int ea_ino;
1082 int err, saved_err;
1083
1084 for (entry = first; !IS_LAST_ENTRY(entry);
1085 entry = EXT4_XATTR_NEXT(entry)) {
1086 if (!entry->e_value_inum)
1087 continue;
1088 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001089 err = ext4_xattr_inode_iget(parent, ea_ino,
1090 le32_to_cpu(entry->e_hash),
1091 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001092 if (err)
1093 goto cleanup;
1094 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1095 if (err) {
1096 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1097 iput(ea_inode);
1098 goto cleanup;
1099 }
1100 iput(ea_inode);
1101 }
1102 return 0;
1103
1104cleanup:
1105 saved_err = err;
1106 failed_entry = entry;
1107
1108 for (entry = first; entry != failed_entry;
1109 entry = EXT4_XATTR_NEXT(entry)) {
1110 if (!entry->e_value_inum)
1111 continue;
1112 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001113 err = ext4_xattr_inode_iget(parent, ea_ino,
1114 le32_to_cpu(entry->e_hash),
1115 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001116 if (err) {
1117 ext4_warning(parent->i_sb,
1118 "cleanup ea_ino %u iget error %d", ea_ino,
1119 err);
1120 continue;
1121 }
1122 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1123 if (err)
1124 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1125 err);
1126 iput(ea_inode);
1127 }
1128 return saved_err;
1129}
1130
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001131static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001132ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1133 struct buffer_head *bh,
1134 struct ext4_xattr_entry *first, bool block_csum,
1135 struct ext4_xattr_inode_array **ea_inode_array,
1136 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001137{
1138 struct inode *ea_inode;
1139 struct ext4_xattr_entry *entry;
1140 bool dirty = false;
1141 unsigned int ea_ino;
1142 int err;
1143 int credits;
1144
1145 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1146 credits = 2 + extra_credits;
1147
1148 for (entry = first; !IS_LAST_ENTRY(entry);
1149 entry = EXT4_XATTR_NEXT(entry)) {
1150 if (!entry->e_value_inum)
1151 continue;
1152 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001153 err = ext4_xattr_inode_iget(parent, ea_ino,
1154 le32_to_cpu(entry->e_hash),
1155 &ea_inode);
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001156 if (err)
1157 continue;
1158
1159 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1160 if (err) {
1161 ext4_warning_inode(ea_inode,
1162 "Expand inode array err=%d", err);
1163 iput(ea_inode);
1164 continue;
1165 }
1166
1167 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1168 dirty, block_csum);
1169 if (err) {
1170 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1171 err);
1172 continue;
1173 }
1174
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001175 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1176 if (err) {
1177 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1178 err);
1179 continue;
1180 }
1181
1182 if (!skip_quota)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001183 ext4_xattr_inode_free_quota(parent, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001184 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001185
1186 /*
1187 * Forget about ea_inode within the same transaction that
1188 * decrements the ref count. This avoids duplicate decrements in
1189 * case the rest of the work spills over to subsequent
1190 * transactions.
1191 */
1192 entry->e_value_inum = 0;
1193 entry->e_value_size = 0;
1194
1195 dirty = true;
1196 }
1197
1198 if (dirty) {
1199 /*
1200 * Note that we are deliberately skipping csum calculation for
1201 * the final update because we do not expect any journal
1202 * restarts until xattr block is freed.
1203 */
1204
1205 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1206 if (err)
1207 ext4_warning_inode(parent,
1208 "handle dirty metadata err=%d", err);
1209 }
1210}
1211
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001213 * Release the xattr block BH: If the reference count is > 1, decrement it;
1214 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 */
1216static void
Mingming Cao617ba132006-10-11 01:20:53 -07001217ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001218 struct buffer_head *bh,
1219 struct ext4_xattr_inode_array **ea_inode_array,
1220 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001222 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001223 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001224 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225
liang xie5d601252014-05-12 22:06:43 -04001226 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001227 error = ext4_journal_get_write_access(handle, bh);
1228 if (error)
1229 goto out;
1230
1231 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001232 hash = le32_to_cpu(BHDR(bh)->h_hash);
1233 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1234 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001236 /*
1237 * This must happen under buffer lock for
1238 * ext4_xattr_block_set() to reliably detect freed block
1239 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001240 if (ea_block_cache)
1241 mb_cache_entry_delete(ea_block_cache, hash,
1242 bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001244 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001245
1246 if (ext4_has_feature_ea_inode(inode->i_sb))
1247 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1248 BFIRST(bh),
1249 true /* block_csum */,
1250 ea_inode_array,
1251 extra_credits,
1252 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001253 ext4_free_blocks(handle, inode, bh, 0, 1,
1254 EXT4_FREE_BLOCKS_METADATA |
1255 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001257 ref--;
1258 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1259 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1260 struct mb_cache_entry *ce;
1261
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001262 if (ea_block_cache) {
1263 ce = mb_cache_entry_get(ea_block_cache, hash,
1264 bh->b_blocknr);
1265 if (ce) {
1266 ce->e_reusable = 1;
1267 mb_cache_entry_put(ea_block_cache, ce);
1268 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001269 }
1270 }
1271
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001272 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001273 /*
1274 * Beware of this ugliness: Releasing of xattr block references
1275 * from different inodes can race and so we have to protect
1276 * from a race where someone else frees the block (and releases
1277 * its journal_head) before we are done dirtying the buffer. In
1278 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001279 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001280 * that function can call sync_dirty_buffer() so for that case
1281 * we handle the dirtying after unlocking the buffer.
1282 */
1283 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001284 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001285 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001286 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001287 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001288 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001289 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001290 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001291 ea_bdebug(bh, "refcount now=%d; releasing",
1292 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001294out:
1295 ext4_std_error(inode->i_sb, error);
1296 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297}
1298
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001299/*
1300 * Find the available free space for EAs. This also returns the total number of
1301 * bytes used by EA entries.
1302 */
1303static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1304 size_t *min_offs, void *base, int *total)
1305{
1306 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001307 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001308 size_t offs = le16_to_cpu(last->e_value_offs);
1309 if (offs < *min_offs)
1310 *min_offs = offs;
1311 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001312 if (total)
1313 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001314 }
1315 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1316}
1317
Andreas Dilgere50e5122017-06-21 21:10:32 -04001318/*
1319 * Write the value of the EA in an inode.
1320 */
1321static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1322 const void *buf, int bufsize)
1323{
1324 struct buffer_head *bh = NULL;
1325 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001326 int blocksize = ea_inode->i_sb->s_blocksize;
1327 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001328 int csize, wsize = 0;
1329 int ret = 0;
1330 int retries = 0;
1331
1332retry:
1333 while (ret >= 0 && ret < max_blocks) {
1334 struct ext4_map_blocks map;
1335 map.m_lblk = block += ret;
1336 map.m_len = max_blocks -= ret;
1337
1338 ret = ext4_map_blocks(handle, ea_inode, &map,
1339 EXT4_GET_BLOCKS_CREATE);
1340 if (ret <= 0) {
1341 ext4_mark_inode_dirty(handle, ea_inode);
1342 if (ret == -ENOSPC &&
1343 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1344 ret = 0;
1345 goto retry;
1346 }
1347 break;
1348 }
1349 }
1350
1351 if (ret < 0)
1352 return ret;
1353
1354 block = 0;
1355 while (wsize < bufsize) {
1356 if (bh != NULL)
1357 brelse(bh);
1358 csize = (bufsize - wsize) > blocksize ? blocksize :
1359 bufsize - wsize;
1360 bh = ext4_getblk(handle, ea_inode, block, 0);
1361 if (IS_ERR(bh))
1362 return PTR_ERR(bh);
1363 ret = ext4_journal_get_write_access(handle, bh);
1364 if (ret)
1365 goto out;
1366
1367 memcpy(bh->b_data, buf, csize);
1368 set_buffer_uptodate(bh);
1369 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1370
1371 buf += csize;
1372 wsize += csize;
1373 block += 1;
1374 }
1375
1376 inode_lock(ea_inode);
1377 i_size_write(ea_inode, wsize);
1378 ext4_update_i_disksize(ea_inode, wsize);
1379 inode_unlock(ea_inode);
1380
1381 ext4_mark_inode_dirty(handle, ea_inode);
1382
1383out:
1384 brelse(bh);
1385
1386 return ret;
1387}
1388
1389/*
1390 * Create an inode to store the value of a large EA.
1391 */
1392static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001393 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001394{
1395 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001396 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001397 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001398
1399 /*
1400 * Let the next inode be the goal, so we try and allocate the EA inode
1401 * in the same group, or nearby one.
1402 */
1403 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001404 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001405 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001406 if (!IS_ERR(ea_inode)) {
1407 ea_inode->i_op = &ext4_file_inode_operations;
1408 ea_inode->i_fop = &ext4_file_operations;
1409 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001410 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001411 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001412 ext4_xattr_inode_set_ref(ea_inode, 1);
1413 ext4_xattr_inode_set_hash(ea_inode, hash);
1414 err = ext4_mark_inode_dirty(handle, ea_inode);
1415 if (!err)
1416 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001417 if (err) {
1418 iput(ea_inode);
1419 return ERR_PTR(err);
1420 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001421
1422 /*
1423 * Xattr inodes are shared therefore quota charging is performed
1424 * at a higher level.
1425 */
1426 dquot_free_inode(ea_inode);
1427 dquot_drop(ea_inode);
1428 inode_lock(ea_inode);
1429 ea_inode->i_flags |= S_NOQUOTA;
1430 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001431 }
1432
1433 return ea_inode;
1434}
1435
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001436static struct inode *
1437ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1438 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001439{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001440 struct inode *ea_inode;
1441 struct mb_cache_entry *ce;
1442 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1443 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001444
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001445 if (!ea_inode_cache)
1446 return NULL;
1447
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001448 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1449 if (!ce)
1450 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001451
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001452 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1453 if (!ea_data) {
1454 mb_cache_entry_put(ea_inode_cache, ce);
1455 return NULL;
1456 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001457
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001458 while (ce) {
1459 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1460 if (!IS_ERR(ea_inode) &&
1461 !is_bad_inode(ea_inode) &&
1462 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1463 i_size_read(ea_inode) == value_len &&
1464 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001465 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1466 value_len) &&
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001467 !memcmp(value, ea_data, value_len)) {
1468 mb_cache_entry_touch(ea_inode_cache, ce);
1469 mb_cache_entry_put(ea_inode_cache, ce);
1470 kvfree(ea_data);
1471 return ea_inode;
1472 }
1473
1474 if (!IS_ERR(ea_inode))
1475 iput(ea_inode);
1476 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1477 }
1478 kvfree(ea_data);
1479 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001480}
1481
1482/*
1483 * Add value of the EA in an inode.
1484 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001485static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1486 const void *value, size_t value_len,
1487 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001488{
1489 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001490 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001491 int err;
1492
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001493 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1494 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1495 if (ea_inode) {
1496 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1497 if (err) {
1498 iput(ea_inode);
1499 return err;
1500 }
1501
1502 *ret_inode = ea_inode;
1503 return 0;
1504 }
1505
Andreas Dilgere50e5122017-06-21 21:10:32 -04001506 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001507 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001508 if (IS_ERR(ea_inode))
1509 return PTR_ERR(ea_inode);
1510
1511 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001512 if (err) {
1513 ext4_xattr_inode_dec_ref(handle, ea_inode);
1514 iput(ea_inode);
1515 return err;
1516 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001517
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001518 if (EA_INODE_CACHE(inode))
1519 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1520 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001521
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001522 *ret_inode = ea_inode;
1523 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001524}
1525
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001526/*
1527 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1528 * feature is enabled.
1529 */
1530#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1531
Andreas Dilgere50e5122017-06-21 21:10:32 -04001532static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1533 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001534 handle_t *handle, struct inode *inode,
1535 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536{
Mingming Cao617ba132006-10-11 01:20:53 -07001537 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001538 struct ext4_xattr_entry *here = s->here;
1539 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001540 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001541 struct inode *old_ea_inode = NULL;
1542 struct inode *new_ea_inode = NULL;
1543 size_t old_size, new_size;
1544 int ret;
1545
1546 /* Space used by old and new values. */
1547 old_size = (!s->not_found && !here->e_value_inum) ?
1548 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1549 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1550
1551 /*
1552 * Optimization for the simple case when old and new values have the
1553 * same padded sizes. Not applicable if external inodes are involved.
1554 */
1555 if (new_size && new_size == old_size) {
1556 size_t offs = le16_to_cpu(here->e_value_offs);
1557 void *val = s->base + offs;
1558
1559 here->e_value_size = cpu_to_le32(i->value_len);
1560 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1561 memset(val, 0, new_size);
1562 } else {
1563 memcpy(val, i->value, i->value_len);
1564 /* Clear padding bytes. */
1565 memset(val + i->value_len, 0, new_size - i->value_len);
1566 }
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001567 goto update_hash;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001568 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001569
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001570 /* Compute min_offs and last. */
1571 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001572 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001573 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 size_t offs = le16_to_cpu(last->e_value_offs);
1575 if (offs < min_offs)
1576 min_offs = offs;
1577 }
1578 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001579
1580 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001581 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001582 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001583
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001584 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1585 if (!s->not_found)
1586 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001587
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001588 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1589 ret = -ENOSPC;
1590 goto out;
1591 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001592
1593 /*
1594 * If storing the value in an external inode is an option,
1595 * reserve space for xattr entries/names in the external
1596 * attribute block so that a long value does not occupy the
1597 * whole space and prevent futher entries being added.
1598 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001599 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1600 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001601 (min_offs + old_size - new_size) <
1602 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1603 ret = -ENOSPC;
1604 goto out;
1605 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 }
1607
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001608 /*
1609 * Getting access to old and new ea inodes is subject to failures.
1610 * Finish that work before doing any modifications to the xattr data.
1611 */
1612 if (!s->not_found && here->e_value_inum) {
1613 ret = ext4_xattr_inode_iget(inode,
1614 le32_to_cpu(here->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001615 le32_to_cpu(here->e_hash),
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001616 &old_ea_inode);
1617 if (ret) {
1618 old_ea_inode = NULL;
1619 goto out;
1620 }
1621 }
1622 if (i->value && in_inode) {
1623 WARN_ON_ONCE(!i->value_len);
1624
1625 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1626 if (ret)
1627 goto out;
1628
1629 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1630 i->value_len,
1631 &new_ea_inode);
1632 if (ret) {
1633 new_ea_inode = NULL;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001634 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001635 goto out;
1636 }
1637 }
1638
1639 if (old_ea_inode) {
1640 /* We are ready to release ref count on the old_ea_inode. */
1641 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1642 if (ret) {
1643 /* Release newly required ref count on new_ea_inode. */
1644 if (new_ea_inode) {
1645 int err;
1646
1647 err = ext4_xattr_inode_dec_ref(handle,
1648 new_ea_inode);
1649 if (err)
1650 ext4_warning_inode(new_ea_inode,
1651 "dec ref new_ea_inode err=%d",
1652 err);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001653 ext4_xattr_inode_free_quota(inode, new_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001654 i->value_len);
1655 }
1656 goto out;
1657 }
1658
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001659 ext4_xattr_inode_free_quota(inode, old_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001660 le32_to_cpu(here->e_value_size));
1661 }
1662
1663 /* No failures allowed past this point. */
1664
1665 if (!s->not_found && here->e_value_offs) {
1666 /* Remove the old value. */
1667 void *first_val = s->base + min_offs;
1668 size_t offs = le16_to_cpu(here->e_value_offs);
1669 void *val = s->base + offs;
1670
1671 memmove(first_val + old_size, first_val, val - first_val);
1672 memset(first_val, 0, old_size);
1673 min_offs += old_size;
1674
1675 /* Adjust all value offsets. */
1676 last = s->first;
1677 while (!IS_LAST_ENTRY(last)) {
1678 size_t o = le16_to_cpu(last->e_value_offs);
1679
1680 if (!last->e_value_inum &&
1681 last->e_value_size && o < offs)
1682 last->e_value_offs = cpu_to_le16(o + old_size);
1683 last = EXT4_XATTR_NEXT(last);
1684 }
1685 }
1686
1687 if (!i->value) {
1688 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001689 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001690
1691 last = ENTRY((void *)last - size);
1692 memmove(here, (void *)here + size,
1693 (void *)last - (void *)here + sizeof(__u32));
1694 memset(last, 0, size);
1695 } else if (s->not_found) {
1696 /* Insert new name. */
1697 size_t size = EXT4_XATTR_LEN(name_len);
1698 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1699
1700 memmove((void *)here + size, here, rest);
1701 memset(here, 0, size);
1702 here->e_name_index = i->name_index;
1703 here->e_name_len = name_len;
1704 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001706 /* This is an update, reset value info. */
1707 here->e_value_inum = 0;
1708 here->e_value_offs = 0;
1709 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710 }
1711
1712 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001713 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001714 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001715 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001716 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001717 void *val = s->base + min_offs - new_size;
1718
1719 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001720 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001721 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001722 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001723 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001724 /* Clear padding bytes. */
1725 memset(val + i->value_len, 0,
1726 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001727 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001729 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001731
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001732update_hash:
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001733 if (i->value) {
1734 __le32 hash = 0;
1735
1736 /* Entry hash calculation. */
1737 if (in_inode) {
1738 __le32 crc32c_hash;
1739
1740 /*
1741 * Feed crc32c hash instead of the raw value for entry
1742 * hash calculation. This is to avoid walking
1743 * potentially long value buffer again.
1744 */
1745 crc32c_hash = cpu_to_le32(
1746 ext4_xattr_inode_get_hash(new_ea_inode));
1747 hash = ext4_xattr_hash_entry(here->e_name,
1748 here->e_name_len,
1749 &crc32c_hash, 1);
1750 } else if (is_block) {
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001751 __le32 *value = s->base + le16_to_cpu(
1752 here->e_value_offs);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001753
1754 hash = ext4_xattr_hash_entry(here->e_name,
1755 here->e_name_len, value,
1756 new_size >> 2);
1757 }
1758 here->e_hash = hash;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001759 }
1760
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001761 if (is_block)
1762 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1763
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001764 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001765out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001766 iput(old_ea_inode);
1767 iput(new_ea_inode);
1768 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769}
1770
Mingming Cao617ba132006-10-11 01:20:53 -07001771struct ext4_xattr_block_find {
1772 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773 struct buffer_head *bh;
1774};
1775
1776static int
Mingming Cao617ba132006-10-11 01:20:53 -07001777ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1778 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779{
1780 struct super_block *sb = inode->i_sb;
1781 int error;
1782
1783 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1784 i->name_index, i->name, i->value, (long)i->value_len);
1785
Mingming Cao617ba132006-10-11 01:20:53 -07001786 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001788 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001789 error = -EIO;
1790 if (!bs->bh)
1791 goto cleanup;
1792 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1793 atomic_read(&(bs->bh->b_count)),
1794 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001795 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001796 EXT4_ERROR_INODE(inode, "bad block %llu",
1797 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001798 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001799 goto cleanup;
1800 }
1801 /* Find the named attribute. */
1802 bs->s.base = BHDR(bs->bh);
1803 bs->s.first = BFIRST(bs->bh);
1804 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1805 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001806 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001807 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001808 if (error && error != -ENODATA)
1809 goto cleanup;
1810 bs->s.not_found = error;
1811 }
1812 error = 0;
1813
1814cleanup:
1815 return error;
1816}
1817
1818static int
Mingming Cao617ba132006-10-11 01:20:53 -07001819ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1820 struct ext4_xattr_info *i,
1821 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001822{
1823 struct super_block *sb = inode->i_sb;
1824 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001825 struct ext4_xattr_search s_copy = bs->s;
1826 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001827 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001828 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001829 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001830 struct inode *ea_inode = NULL, *tmp_inode;
1831 size_t old_ea_inode_quota = 0;
1832 unsigned int ea_ino;
1833
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834
Mingming Cao617ba132006-10-11 01:20:53 -07001835#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001838 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001839 error = ext4_journal_get_write_access(handle, bs->bh);
1840 if (error)
1841 goto cleanup;
1842 lock_buffer(bs->bh);
1843
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001844 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001845 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1846
1847 /*
1848 * This must happen under buffer lock for
1849 * ext4_xattr_block_set() to reliably detect modified
1850 * block
1851 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001852 if (ea_block_cache)
1853 mb_cache_entry_delete(ea_block_cache, hash,
1854 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001855 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001856 error = ext4_xattr_set_entry(i, s, handle, inode,
1857 true /* is_block */);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001858 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001859 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001860 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001861 goto bad_block;
1862 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001863 error = ext4_handle_dirty_metadata(handle,
1864 inode,
1865 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 if (error)
1867 goto cleanup;
1868 goto inserted;
1869 } else {
1870 int offset = (char *)s->here - bs->bh->b_data;
1871
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001872 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001873 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001874 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 error = -ENOMEM;
1876 if (s->base == NULL)
1877 goto cleanup;
1878 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1879 s->first = ENTRY(header(s->base)+1);
1880 header(s->base)->h_refcount = cpu_to_le32(1);
1881 s->here = ENTRY(s->base + offset);
1882 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001883
1884 /*
1885 * If existing entry points to an xattr inode, we need
1886 * to prevent ext4_xattr_set_entry() from decrementing
1887 * ref count on it because the reference belongs to the
1888 * original block. In this case, make the entry look
1889 * like it has an empty value.
1890 */
1891 if (!s->not_found && s->here->e_value_inum) {
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001892 ea_ino = le32_to_cpu(s->here->e_value_inum);
1893 error = ext4_xattr_inode_iget(inode, ea_ino,
1894 le32_to_cpu(s->here->e_hash),
1895 &tmp_inode);
1896 if (error)
1897 goto cleanup;
1898
1899 if (!ext4_test_inode_state(tmp_inode,
1900 EXT4_STATE_LUSTRE_EA_INODE)) {
1901 /*
1902 * Defer quota free call for previous
1903 * inode until success is guaranteed.
1904 */
1905 old_ea_inode_quota = le32_to_cpu(
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001906 s->here->e_value_size);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001907 }
1908 iput(tmp_inode);
1909
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001910 s->here->e_value_inum = 0;
1911 s->here->e_value_size = 0;
1912 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001913 }
1914 } else {
1915 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001916 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001917 /* assert(header == s->base) */
1918 error = -ENOMEM;
1919 if (s->base == NULL)
1920 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001921 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 header(s->base)->h_blocks = cpu_to_le32(1);
1923 header(s->base)->h_refcount = cpu_to_le32(1);
1924 s->first = ENTRY(header(s->base)+1);
1925 s->here = ENTRY(header(s->base)+1);
1926 s->end = s->base + sb->s_blocksize;
1927 }
1928
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001929 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001930 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001931 goto bad_block;
1932 if (error)
1933 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001934
1935 if (i->value && s->here->e_value_inum) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001936 /*
1937 * A ref count on ea_inode has been taken as part of the call to
1938 * ext4_xattr_set_entry() above. We would like to drop this
1939 * extra ref but we have to wait until the xattr block is
1940 * initialized and has its own ref count on the ea_inode.
1941 */
1942 ea_ino = le32_to_cpu(s->here->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001943 error = ext4_xattr_inode_iget(inode, ea_ino,
1944 le32_to_cpu(s->here->e_hash),
1945 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001946 if (error) {
1947 ea_inode = NULL;
1948 goto cleanup;
1949 }
1950 }
1951
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952inserted:
1953 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001954 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1955 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956 if (new_bh) {
1957 /* We found an identical block in the cache. */
1958 if (new_bh == bs->bh)
1959 ea_bdebug(new_bh, "keeping");
1960 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001961 u32 ref;
1962
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001963 WARN_ON_ONCE(dquot_initialize_needed(inode));
1964
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965 /* The old block is released after updating
1966 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001967 error = dquot_alloc_block(inode,
1968 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001969 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001970 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001971 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001972 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001973 new_bh);
1974 if (error)
1975 goto cleanup_dquot;
1976 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001977 /*
1978 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001979 * freeing, rehashing or adding references to
1980 * xattr block. Once we hold buffer lock xattr
1981 * block's state is stable so we can check
1982 * whether the block got freed / rehashed or
1983 * not. Since we unhash mbcache entry under
1984 * buffer lock when freeing / rehashing xattr
1985 * block, checking whether entry is still
1986 * hashed is reliable. Same rules hold for
1987 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001988 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001989 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1990 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001991 /*
1992 * Undo everything and check mbcache
1993 * again.
1994 */
1995 unlock_buffer(new_bh);
1996 dquot_free_block(inode,
1997 EXT4_C2B(EXT4_SB(sb),
1998 1));
1999 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002000 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05002001 ce = NULL;
2002 new_bh = NULL;
2003 goto inserted;
2004 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002005 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2006 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2007 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
2008 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002010 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002011 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002013 error = ext4_handle_dirty_metadata(handle,
2014 inode,
2015 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 if (error)
2017 goto cleanup_dquot;
2018 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04002019 mb_cache_entry_touch(ea_block_cache, ce);
2020 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021 ce = NULL;
2022 } else if (bs->bh && s->base == bs->bh->b_data) {
2023 /* We were modifying this block in-place. */
2024 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganec000222017-08-05 22:41:42 -04002025 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002026 new_bh = bs->bh;
2027 get_bh(new_bh);
2028 } else {
2029 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002030 ext4_fsblk_t goal, block;
2031
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002032 WARN_ON_ONCE(dquot_initialize_needed(inode));
2033
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002034 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04002035 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002036
2037 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002038 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002039 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
2040
Allison Henderson55f020d2011-05-25 07:41:26 -04002041 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2042 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002043 if (error)
2044 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002045
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002046 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002047 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
2048
Joe Perchesace36ad2012-03-19 23:11:43 -04002049 ea_idebug(inode, "creating block %llu",
2050 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002051
2052 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05002053 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002054 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002055getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05002056 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002057 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002058 goto cleanup;
2059 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002060 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2061 ENTRY(header(s->base)+1));
2062 if (error)
2063 goto getblk_failed;
2064 if (ea_inode) {
2065 /* Drop the extra ref on ea_inode. */
2066 error = ext4_xattr_inode_dec_ref(handle,
2067 ea_inode);
2068 if (error)
2069 ext4_warning_inode(ea_inode,
2070 "dec ref error=%d",
2071 error);
2072 iput(ea_inode);
2073 ea_inode = NULL;
2074 }
2075
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002077 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002078 if (error) {
2079 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002080 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 goto getblk_failed;
2082 }
2083 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002084 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085 set_buffer_uptodate(new_bh);
2086 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002087 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002088 error = ext4_handle_dirty_metadata(handle, inode,
2089 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002090 if (error)
2091 goto cleanup;
2092 }
2093 }
2094
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002095 if (old_ea_inode_quota)
2096 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002097
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002098 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07002099 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002100
2101 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002102 if (bs->bh && bs->bh != new_bh) {
2103 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2104
2105 ext4_xattr_release_block(handle, inode, bs->bh,
2106 &ea_inode_array,
2107 0 /* extra_credits */);
2108 ext4_xattr_inode_array_free(ea_inode_array);
2109 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 error = 0;
2111
2112cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002113 if (ea_inode) {
2114 int error2;
2115
2116 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2117 if (error2)
2118 ext4_warning_inode(ea_inode, "dec ref error=%d",
2119 error2);
2120
2121 /* If there was an error, revert the quota charge. */
2122 if (error)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002123 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002124 i_size_read(ea_inode));
2125 iput(ea_inode);
2126 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002128 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 brelse(new_bh);
2130 if (!(bs->bh && s->base == bs->bh->b_data))
2131 kfree(s->base);
2132
2133 return error;
2134
2135cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002136 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002137 goto cleanup;
2138
2139bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002140 EXT4_ERROR_INODE(inode, "bad block %llu",
2141 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002142 goto cleanup;
2143
2144#undef header
2145}
2146
Tao Ma879b3822012-12-05 10:28:46 -05002147int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2148 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002149{
Mingming Cao617ba132006-10-11 01:20:53 -07002150 struct ext4_xattr_ibody_header *header;
2151 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002152 int error;
2153
Mingming Cao617ba132006-10-11 01:20:53 -07002154 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002155 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002156 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002157 header = IHDR(inode, raw_inode);
2158 is->s.base = is->s.first = IFIRST(header);
2159 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002160 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002161 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002162 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 if (error)
2164 return error;
2165 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07002166 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04002167 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002168 if (error && error != -ENODATA)
2169 return error;
2170 is->s.not_found = error;
2171 }
2172 return 0;
2173}
2174
Tao Ma0d812f72012-12-10 14:06:02 -05002175int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2176 struct ext4_xattr_info *i,
2177 struct ext4_xattr_ibody_find *is)
2178{
2179 struct ext4_xattr_ibody_header *header;
2180 struct ext4_xattr_search *s = &is->s;
2181 int error;
2182
2183 if (EXT4_I(inode)->i_extra_isize == 0)
2184 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002185 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002186 if (error) {
2187 if (error == -ENOSPC &&
2188 ext4_has_inline_data(inode)) {
2189 error = ext4_try_to_evict_inline_data(handle, inode,
2190 EXT4_XATTR_LEN(strlen(i->name) +
2191 EXT4_XATTR_SIZE(i->value_len)));
2192 if (error)
2193 return error;
2194 error = ext4_xattr_ibody_find(inode, i, is);
2195 if (error)
2196 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002197 error = ext4_xattr_set_entry(i, s, handle, inode,
2198 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002199 }
2200 if (error)
2201 return error;
2202 }
2203 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2204 if (!IS_LAST_ENTRY(s->first)) {
2205 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2206 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2207 } else {
2208 header->h_magic = cpu_to_le32(0);
2209 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2210 }
2211 return 0;
2212}
2213
Andreas Dilgere50e5122017-06-21 21:10:32 -04002214static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002215 struct ext4_xattr_info *i,
2216 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217{
Mingming Cao617ba132006-10-11 01:20:53 -07002218 struct ext4_xattr_ibody_header *header;
2219 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002220 int error;
2221
Mingming Cao617ba132006-10-11 01:20:53 -07002222 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002223 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002224 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225 if (error)
2226 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002227 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002228 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002229 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002230 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 } else {
2232 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002233 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234 }
2235 return 0;
2236}
2237
Jan Kara3fd16462016-02-22 22:43:04 -05002238static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2239 struct ext4_xattr_info *i)
2240{
2241 void *value;
2242
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002243 /* When e_value_inum is set the value is stored externally. */
2244 if (s->here->e_value_inum)
2245 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002246 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2247 return 0;
2248 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2249 return !memcmp(value, i->value, i->value_len);
2250}
2251
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002252static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2253{
2254 struct buffer_head *bh;
2255 int error;
2256
2257 if (!EXT4_I(inode)->i_file_acl)
2258 return NULL;
2259 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2260 if (!bh)
2261 return ERR_PTR(-EIO);
2262 error = ext4_xattr_check_block(inode, bh);
2263 if (error)
2264 return ERR_PTR(error);
2265 return bh;
2266}
2267
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268/*
Mingming Cao617ba132006-10-11 01:20:53 -07002269 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002270 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002271 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002272 * is NULL to remove an existing extended attribute, and non-NULL to
2273 * either replace an existing extended attribute, or create a new extended
2274 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2275 * specify that an extended attribute must exist and must not exist
2276 * previous to the call, respectively.
2277 *
2278 * Returns 0, or a negative error number on failure.
2279 */
2280int
Mingming Cao617ba132006-10-11 01:20:53 -07002281ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002282 const char *name, const void *value, size_t value_len,
2283 int flags)
2284{
Mingming Cao617ba132006-10-11 01:20:53 -07002285 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 .name_index = name_index,
2287 .name = name,
2288 .value = value,
2289 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002290 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002291 };
Mingming Cao617ba132006-10-11 01:20:53 -07002292 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 .s = { .not_found = -ENODATA, },
2294 };
Mingming Cao617ba132006-10-11 01:20:53 -07002295 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002296 .s = { .not_found = -ENODATA, },
2297 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002298 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002299 int error;
2300
2301 if (!name)
2302 return -EINVAL;
2303 if (strlen(name) > 255)
2304 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002305
Theodore Ts'oc755e252017-01-11 21:50:46 -05002306 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002307
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002308 /* Check journal credits under write lock. */
2309 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002310 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002311 int credits;
2312
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002313 bh = ext4_xattr_get_block(inode);
2314 if (IS_ERR(bh)) {
2315 error = PTR_ERR(bh);
2316 goto cleanup;
2317 }
2318
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002319 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2320 value_len,
2321 flags & XATTR_CREATE);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002322 brelse(bh);
2323
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002324 if (!ext4_handle_has_enough_credits(handle, credits)) {
2325 error = -ENOSPC;
2326 goto cleanup;
2327 }
2328 }
2329
Eric Sandeen66543612011-10-26 03:32:07 -04002330 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002331 if (error)
2332 goto cleanup;
2333
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002334 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002335 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2336 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002337 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002338 }
2339
Mingming Cao617ba132006-10-11 01:20:53 -07002340 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002341 if (error)
2342 goto cleanup;
2343 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002344 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002345 if (error)
2346 goto cleanup;
2347 if (is.s.not_found && bs.s.not_found) {
2348 error = -ENODATA;
2349 if (flags & XATTR_REPLACE)
2350 goto cleanup;
2351 error = 0;
2352 if (!value)
2353 goto cleanup;
2354 } else {
2355 error = -EEXIST;
2356 if (flags & XATTR_CREATE)
2357 goto cleanup;
2358 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002359
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002360 if (!value) {
2361 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002362 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002363 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002364 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002365 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002366 error = 0;
2367 /* Xattr value did not change? Save us some work and bail out */
2368 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2369 goto cleanup;
2370 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2371 goto cleanup;
2372
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002373 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2374 (EXT4_XATTR_SIZE(i.value_len) >
2375 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2376 i.in_inode = 1;
2377retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002378 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002379 if (!error && !bs.s.not_found) {
2380 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002381 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002382 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002383 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2384 error = ext4_xattr_block_find(inode, &i, &bs);
2385 if (error)
2386 goto cleanup;
2387 }
Mingming Cao617ba132006-10-11 01:20:53 -07002388 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002389 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002390 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002391 error = ext4_xattr_ibody_set(handle, inode, &i,
2392 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002393 } else if (error == -ENOSPC) {
2394 /*
2395 * Xattr does not fit in the block, store at
2396 * external inode if possible.
2397 */
2398 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2399 !i.in_inode) {
2400 i.in_inode = 1;
2401 goto retry_inode;
2402 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403 }
2404 }
2405 }
2406 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002407 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002408 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002409 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002410 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002411 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002413 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 * error != 0.
2415 */
2416 is.iloc.bh = NULL;
2417 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002418 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002419 }
2420
2421cleanup:
2422 brelse(is.iloc.bh);
2423 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002424 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002425 return error;
2426}
2427
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002428int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2429 bool is_create, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002430{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002431 struct buffer_head *bh;
2432 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002433
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002434 *credits = 0;
2435
2436 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002437 return 0;
2438
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002439 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002440
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002441 bh = ext4_xattr_get_block(inode);
2442 if (IS_ERR(bh)) {
2443 err = PTR_ERR(bh);
2444 } else {
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002445 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2446 value_len, is_create);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002447 brelse(bh);
2448 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002449 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002450
2451 up_read(&EXT4_I(inode)->xattr_sem);
2452 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002453}
2454
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002455/*
Mingming Cao617ba132006-10-11 01:20:53 -07002456 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457 *
Mingming Cao617ba132006-10-11 01:20:53 -07002458 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002459 * attribute modification is a filesystem transaction by itself.
2460 *
2461 * Returns 0, or a negative error number on failure.
2462 */
2463int
Mingming Cao617ba132006-10-11 01:20:53 -07002464ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002465 const void *value, size_t value_len, int flags)
2466{
2467 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002468 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002470 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002472 error = dquot_initialize(inode);
2473 if (error)
2474 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002475
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002476retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002477 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2478 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002479 if (error)
2480 return error;
2481
Theodore Ts'o9924a922013-02-08 21:59:22 -05002482 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002483 if (IS_ERR(handle)) {
2484 error = PTR_ERR(handle);
2485 } else {
2486 int error2;
2487
Mingming Cao617ba132006-10-11 01:20:53 -07002488 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002489 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002490 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002491 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002492 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002493 goto retry;
2494 if (error == 0)
2495 error = error2;
2496 }
2497
2498 return error;
2499}
2500
2501/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002502 * Shift the EA entries in the inode to create space for the increased
2503 * i_extra_isize.
2504 */
2505static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2506 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002507 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002508{
2509 struct ext4_xattr_entry *last = entry;
2510 int new_offs;
2511
Jan Kara94405712016-08-29 15:41:11 -04002512 /* We always shift xattr headers further thus offsets get lower */
2513 BUG_ON(value_offs_shift > 0);
2514
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002515 /* Adjust the value offsets of the entries */
2516 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002517 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002518 new_offs = le16_to_cpu(last->e_value_offs) +
2519 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002520 last->e_value_offs = cpu_to_le16(new_offs);
2521 }
2522 }
2523 /* Shift the entries by n bytes */
2524 memmove(to, from, n);
2525}
2526
2527/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002528 * Move xattr pointed to by 'entry' from inode into external xattr block
2529 */
2530static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2531 struct ext4_inode *raw_inode,
2532 struct ext4_xattr_entry *entry)
2533{
2534 struct ext4_xattr_ibody_find *is = NULL;
2535 struct ext4_xattr_block_find *bs = NULL;
2536 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002537 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002538 struct ext4_xattr_info i = {
2539 .value = NULL,
2540 .value_len = 0,
2541 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002542 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002543 };
2544 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2545 int error;
2546
Jan Kara3f2571c2016-08-29 15:42:11 -04002547 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2548 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2549 buffer = kmalloc(value_size, GFP_NOFS);
2550 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2551 if (!is || !bs || !buffer || !b_entry_name) {
2552 error = -ENOMEM;
2553 goto out;
2554 }
2555
2556 is->s.not_found = -ENODATA;
2557 bs->s.not_found = -ENODATA;
2558 is->iloc.bh = NULL;
2559 bs->bh = NULL;
2560
2561 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002562 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04002563 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002564 if (error)
2565 goto out;
2566 } else {
2567 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2568 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2569 }
2570
Jan Kara3f2571c2016-08-29 15:42:11 -04002571 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2572 b_entry_name[entry->e_name_len] = '\0';
2573 i.name = b_entry_name;
2574
2575 error = ext4_get_inode_loc(inode, &is->iloc);
2576 if (error)
2577 goto out;
2578
2579 error = ext4_xattr_ibody_find(inode, &i, is);
2580 if (error)
2581 goto out;
2582
2583 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002584 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002585 if (error)
2586 goto out;
2587
Jan Kara3f2571c2016-08-29 15:42:11 -04002588 i.value = buffer;
2589 i.value_len = value_size;
2590 error = ext4_xattr_block_find(inode, &i, bs);
2591 if (error)
2592 goto out;
2593
2594 /* Add entry which was removed from the inode into the block */
2595 error = ext4_xattr_block_set(handle, inode, &i, bs);
2596 if (error)
2597 goto out;
2598 error = 0;
2599out:
2600 kfree(b_entry_name);
2601 kfree(buffer);
2602 if (is)
2603 brelse(is->iloc.bh);
2604 kfree(is);
2605 kfree(bs);
2606
2607 return error;
2608}
2609
Jan Karadfa20642016-08-29 15:44:11 -04002610static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2611 struct ext4_inode *raw_inode,
2612 int isize_diff, size_t ifree,
2613 size_t bfree, int *total_ino)
2614{
2615 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2616 struct ext4_xattr_entry *small_entry;
2617 struct ext4_xattr_entry *entry;
2618 struct ext4_xattr_entry *last;
2619 unsigned int entry_size; /* EA entry size */
2620 unsigned int total_size; /* EA entry size + value size */
2621 unsigned int min_total_size;
2622 int error;
2623
2624 while (isize_diff > ifree) {
2625 entry = NULL;
2626 small_entry = NULL;
2627 min_total_size = ~0U;
2628 last = IFIRST(header);
2629 /* Find the entry best suited to be pushed into EA block */
2630 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002631 total_size = EXT4_XATTR_LEN(last->e_name_len);
2632 if (!last->e_value_inum)
2633 total_size += EXT4_XATTR_SIZE(
2634 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002635 if (total_size <= bfree &&
2636 total_size < min_total_size) {
2637 if (total_size + ifree < isize_diff) {
2638 small_entry = last;
2639 } else {
2640 entry = last;
2641 min_total_size = total_size;
2642 }
2643 }
2644 }
2645
2646 if (entry == NULL) {
2647 if (small_entry == NULL)
2648 return -ENOSPC;
2649 entry = small_entry;
2650 }
2651
2652 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002653 total_size = entry_size;
2654 if (!entry->e_value_inum)
2655 total_size += EXT4_XATTR_SIZE(
2656 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002657 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2658 entry);
2659 if (error)
2660 return error;
2661
2662 *total_ino -= entry_size;
2663 ifree += total_size;
2664 bfree -= total_size;
2665 }
2666
2667 return 0;
2668}
2669
Jan Kara3f2571c2016-08-29 15:42:11 -04002670/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002671 * Expand an inode by new_extra_isize bytes when EAs are present.
2672 * Returns 0 on success or negative error number on failure.
2673 */
2674int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2675 struct ext4_inode *raw_inode, handle_t *handle)
2676{
2677 struct ext4_xattr_ibody_header *header;
Miao Xieb640b2c2017-08-06 00:55:48 -04002678 struct buffer_head *bh;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002679 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2680 static unsigned int mnt_count;
Jan Karae3014d12016-08-29 15:38:11 -04002681 size_t min_offs;
2682 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002683 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002684 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002685 int error = 0, tried_min_extra_isize = 0;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002686 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04002687 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002688
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002689retry:
Jan Karad0141192016-08-11 11:50:30 -04002690 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002691 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
Miao Xieb640b2c2017-08-06 00:55:48 -04002692 return 0;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002693
2694 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002695
2696 /*
2697 * Check if enough free space is available in the inode to shift the
2698 * entries ahead by new_extra_isize.
2699 */
2700
Jan Kara6e0cd082016-08-29 15:43:11 -04002701 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002702 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2703 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002704 total_ino = sizeof(struct ext4_xattr_ibody_header);
2705
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002706 error = xattr_check_inode(inode, header, end);
2707 if (error)
2708 goto cleanup;
2709
Jan Kara6e0cd082016-08-29 15:43:11 -04002710 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002711 if (ifree >= isize_diff)
2712 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002713
2714 /*
2715 * Enough free space isn't available in the inode, check if
2716 * EA block can hold new_extra_isize bytes.
2717 */
2718 if (EXT4_I(inode)->i_file_acl) {
2719 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2720 error = -EIO;
2721 if (!bh)
2722 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04002723 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002724 EXT4_ERROR_INODE(inode, "bad block %llu",
2725 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002726 error = -EFSCORRUPTED;
Miao Xieb640b2c2017-08-06 00:55:48 -04002727 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002728 goto cleanup;
2729 }
2730 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002731 end = bh->b_data + bh->b_size;
2732 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002733 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2734 NULL);
Miao Xieb640b2c2017-08-06 00:55:48 -04002735 brelse(bh);
Jan Karae3014d12016-08-29 15:38:11 -04002736 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002737 if (!tried_min_extra_isize && s_min_extra_isize) {
2738 tried_min_extra_isize++;
2739 new_extra_isize = s_min_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002740 goto retry;
2741 }
Jan Karadfa20642016-08-29 15:44:11 -04002742 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002743 goto cleanup;
2744 }
2745 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002746 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002747 }
2748
Jan Karadfa20642016-08-29 15:44:11 -04002749 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2750 isize_diff, ifree, bfree,
2751 &total_ino);
2752 if (error) {
2753 if (error == -ENOSPC && !tried_min_extra_isize &&
2754 s_min_extra_isize) {
2755 tried_min_extra_isize++;
2756 new_extra_isize = s_min_extra_isize;
Jan Karadfa20642016-08-29 15:44:11 -04002757 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002758 }
Jan Karadfa20642016-08-29 15:44:11 -04002759 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002760 }
Jan Karae3014d12016-08-29 15:38:11 -04002761shift:
2762 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002763 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002764 - new_extra_isize, (void *)raw_inode +
2765 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002766 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002767 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002768
2769cleanup:
Miao Xieb640b2c2017-08-06 00:55:48 -04002770 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
Miao Xiecf0a5e82017-08-06 00:40:01 -04002771 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2772 inode->i_ino);
2773 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2774 }
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002775 return error;
2776}
2777
Andreas Dilgere50e5122017-06-21 21:10:32 -04002778#define EIA_INCR 16 /* must be 2^n */
2779#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002780
2781/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002782 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002783 * contents copied over.
2784 */
2785static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002786ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2787 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002788{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002789 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002790 /*
2791 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002792 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002793 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002794 (*ea_inode_array) =
2795 kmalloc(offsetof(struct ext4_xattr_inode_array,
2796 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002797 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002798 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002799 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002800 (*ea_inode_array)->count = 0;
2801 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002802 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002803 struct ext4_xattr_inode_array *new_array = NULL;
2804 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002805
2806 /* if new_array is NULL, this is essentially offsetof() */
2807 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002808 offsetof(struct ext4_xattr_inode_array,
2809 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002810 GFP_NOFS);
2811 if (new_array == NULL)
2812 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002813 memcpy(new_array, *ea_inode_array,
2814 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2815 kfree(*ea_inode_array);
2816 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002817 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002818 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002819 return 0;
2820}
2821
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002822/*
Mingming Cao617ba132006-10-11 01:20:53 -07002823 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002824 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002825 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002826 * all entries and decrement reference on any xattr inodes associated with this
2827 * inode. This is called immediately before an inode is freed. We have exclusive
2828 * access to the inode. If an orphan inode is deleted it will also release its
2829 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002831int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2832 struct ext4_xattr_inode_array **ea_inode_array,
2833 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002834{
2835 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002836 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002837 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002838 struct ext4_xattr_entry *entry;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002839 struct inode *ea_inode;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002840 int error;
2841
2842 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2843 NULL /* bh */,
2844 false /* dirty */,
2845 false /* block_csum */);
2846 if (error) {
2847 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2848 goto cleanup;
2849 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002851 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2852 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002853
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002854 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002855 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002856 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002857 goto cleanup;
2858 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002859
2860 error = ext4_journal_get_write_access(handle, iloc.bh);
2861 if (error) {
2862 EXT4_ERROR_INODE(inode, "write access (error %d)",
2863 error);
2864 goto cleanup;
2865 }
2866
2867 header = IHDR(inode, ext4_raw_inode(&iloc));
2868 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2869 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2870 IFIRST(header),
2871 false /* block_csum */,
2872 ea_inode_array,
2873 extra_credits,
2874 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002875 }
2876
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002877 if (EXT4_I(inode)->i_file_acl) {
2878 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2879 if (!bh) {
2880 EXT4_ERROR_INODE(inode, "block %llu read error",
2881 EXT4_I(inode)->i_file_acl);
2882 error = -EIO;
2883 goto cleanup;
2884 }
2885 error = ext4_xattr_check_block(inode, bh);
2886 if (error) {
2887 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2888 EXT4_I(inode)->i_file_acl, error);
2889 goto cleanup;
2890 }
2891
2892 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2893 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002894 entry = EXT4_XATTR_NEXT(entry)) {
2895 if (!entry->e_value_inum)
2896 continue;
2897 error = ext4_xattr_inode_iget(inode,
2898 le32_to_cpu(entry->e_value_inum),
2899 le32_to_cpu(entry->e_hash),
2900 &ea_inode);
2901 if (error)
2902 continue;
2903 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002904 le32_to_cpu(entry->e_value_size));
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002905 iput(ea_inode);
2906 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002907
2908 }
2909
2910 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2911 extra_credits);
2912 /*
2913 * Update i_file_acl value in the same transaction that releases
2914 * block.
2915 */
2916 EXT4_I(inode)->i_file_acl = 0;
2917 error = ext4_mark_inode_dirty(handle, inode);
2918 if (error) {
2919 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2920 error);
2921 goto cleanup;
2922 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002923 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002924 error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925cleanup:
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002926 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002927 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002928 return error;
2929}
2930
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002931void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002932{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002933 int idx;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002934
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002935 if (ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002936 return;
2937
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002938 for (idx = 0; idx < ea_inode_array->count; ++idx)
2939 iput(ea_inode_array->inodes[idx]);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002940 kfree(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002941}
2942
2943/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002944 * ext4_xattr_block_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945 *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002946 * Create a new entry in the extended attribute block cache, and insert
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 * it unless such an entry is already in the cache.
2948 *
2949 * Returns 0, or a negative error number on failure.
2950 */
2951static void
Tahsin Erdogan47387402017-06-22 11:28:55 -04002952ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2953 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002954{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002955 struct ext4_xattr_header *header = BHDR(bh);
2956 __u32 hash = le32_to_cpu(header->h_hash);
2957 int reusable = le32_to_cpu(header->h_refcount) <
2958 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959 int error;
2960
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002961 if (!ea_block_cache)
2962 return;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002963 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002964 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002965 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002966 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002967 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002968 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002970}
2971
2972/*
Mingming Cao617ba132006-10-11 01:20:53 -07002973 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974 *
2975 * Compare two extended attribute blocks for equality.
2976 *
2977 * Returns 0 if the blocks are equal, 1 if they differ, and
2978 * a negative error number on errors.
2979 */
2980static int
Mingming Cao617ba132006-10-11 01:20:53 -07002981ext4_xattr_cmp(struct ext4_xattr_header *header1,
2982 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983{
Mingming Cao617ba132006-10-11 01:20:53 -07002984 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002985
2986 entry1 = ENTRY(header1+1);
2987 entry2 = ENTRY(header2+1);
2988 while (!IS_LAST_ENTRY(entry1)) {
2989 if (IS_LAST_ENTRY(entry2))
2990 return 1;
2991 if (entry1->e_hash != entry2->e_hash ||
2992 entry1->e_name_index != entry2->e_name_index ||
2993 entry1->e_name_len != entry2->e_name_len ||
2994 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002995 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2997 return 1;
Tahsin Erdogan7cec1912017-06-21 22:14:30 -04002998 if (!entry1->e_value_inum &&
2999 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003000 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3001 le32_to_cpu(entry1->e_value_size)))
3002 return 1;
3003
Mingming Cao617ba132006-10-11 01:20:53 -07003004 entry1 = EXT4_XATTR_NEXT(entry1);
3005 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003006 }
3007 if (!IS_LAST_ENTRY(entry2))
3008 return 1;
3009 return 0;
3010}
3011
3012/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04003013 * ext4_xattr_block_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003014 *
3015 * Find an identical extended attribute block.
3016 *
3017 * Returns a pointer to the block found, or NULL if such a block was
3018 * not found or an error occurred.
3019 */
3020static struct buffer_head *
Tahsin Erdogan47387402017-06-22 11:28:55 -04003021ext4_xattr_block_cache_find(struct inode *inode,
3022 struct ext4_xattr_header *header,
3023 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003024{
3025 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05003026 struct mb_cache_entry *ce;
Tahsin Erdogan47387402017-06-22 11:28:55 -04003027 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003028
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04003029 if (!ea_block_cache)
3030 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003031 if (!header->h_hash)
3032 return NULL; /* never share */
3033 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003034 ce = mb_cache_entry_find_first(ea_block_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003035 while (ce) {
3036 struct buffer_head *bh;
3037
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003038 bh = sb_bread(inode->i_sb, ce->e_value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003040 EXT4_ERROR_INODE(inode, "block %lu read error",
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003041 (unsigned long)ce->e_value);
Mingming Cao617ba132006-10-11 01:20:53 -07003042 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003043 *pce = ce;
3044 return bh;
3045 }
3046 brelse(bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003047 ce = mb_cache_entry_find_next(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003048 }
3049 return NULL;
3050}
3051
3052#define NAME_HASH_SHIFT 5
3053#define VALUE_HASH_SHIFT 16
3054
3055/*
Mingming Cao617ba132006-10-11 01:20:53 -07003056 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003057 *
3058 * Compute the hash of an extended attribute.
3059 */
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003060static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3061 size_t value_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003062{
3063 __u32 hash = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003064
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003065 while (name_len--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066 hash = (hash << NAME_HASH_SHIFT) ^
3067 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3068 *name++;
3069 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003070 while (value_count--) {
3071 hash = (hash << VALUE_HASH_SHIFT) ^
3072 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3073 le32_to_cpu(*value++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003074 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003075 return cpu_to_le32(hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003076}
3077
3078#undef NAME_HASH_SHIFT
3079#undef VALUE_HASH_SHIFT
3080
3081#define BLOCK_HASH_SHIFT 16
3082
3083/*
Mingming Cao617ba132006-10-11 01:20:53 -07003084 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 *
3086 * Re-compute the extended attribute hash value after an entry has changed.
3087 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04003088static void ext4_xattr_rehash(struct ext4_xattr_header *header)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003089{
Mingming Cao617ba132006-10-11 01:20:53 -07003090 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003091 __u32 hash = 0;
3092
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093 here = ENTRY(header+1);
3094 while (!IS_LAST_ENTRY(here)) {
3095 if (!here->e_hash) {
3096 /* Block is not shared if an entry's hash value == 0 */
3097 hash = 0;
3098 break;
3099 }
3100 hash = (hash << BLOCK_HASH_SHIFT) ^
3101 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3102 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07003103 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003104 }
3105 header->h_hash = cpu_to_le32(hash);
3106}
3107
3108#undef BLOCK_HASH_SHIFT
3109
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003110#define HASH_BUCKET_BITS 10
3111
Jan Kara7a2508e2016-02-22 22:35:22 -05003112struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05003113ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003114{
Jan Kara7a2508e2016-02-22 22:35:22 -05003115 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003116}
3117
Jan Kara7a2508e2016-02-22 22:35:22 -05003118void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003119{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003120 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05003121 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003123