blob: 6304e81bfe6a800c93ba053ce939fcd84a672a77 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/xattr.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 *
7 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
Mingming Cao617ba132006-10-11 01:20:53 -07008 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07009 * Extended attributes for symlinks and special files added per
10 * suggestion of Luka Renko <luka.renko@hermes.si>.
11 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * Red Hat Inc.
13 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14 * and Andreas Gruenbacher <agruen@suse.de>.
15 */
16
17/*
18 * Extended attributes are stored directly in inodes (on file systems with
19 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20 * field contains the block number if an inode uses an additional block. All
21 * attributes must fit in the inode and one additional block. Blocks that
22 * contain the identical set of attributes may be shared among several inodes.
23 * Identical blocks are detected by keeping a cache of blocks that have
24 * recently been accessed.
25 *
26 * The attributes in inodes and on blocks have a different header; the entries
27 * are stored in the same format:
28 *
29 * +------------------+
30 * | header |
31 * | entry 1 | |
32 * | entry 2 | | growing downwards
33 * | entry 3 | v
34 * | four null bytes |
35 * | . . . |
36 * | value 1 | ^
37 * | value 3 | | growing upwards
38 * | value 2 | |
39 * +------------------+
40 *
41 * The header is followed by multiple entry descriptors. In disk blocks, the
42 * entry descriptors are kept sorted. In inodes, they are unsorted. The
43 * attribute values are aligned to the end of the block in no specific order.
44 *
45 * Locking strategy
46 * ----------------
Mingming Cao617ba132006-10-11 01:20:53 -070047 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048 * EA blocks are only changed if they are exclusive to an inode, so
49 * holding xattr_sem also means that nothing but the EA block's reference
50 * count can change. Multiple writers to the same block are synchronized
51 * by the buffer lock.
52 */
53
54#include <linux/init.h>
55#include <linux/fs.h>
56#include <linux/slab.h>
Jan Kara7a2508e2016-02-22 22:35:22 -050057#include <linux/mbcache.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070058#include <linux/quotaops.h>
Jeff Laytonee73f9a2018-01-09 08:21:39 -050059#include <linux/iversion.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040060#include "ext4_jbd2.h"
61#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062#include "xattr.h"
63#include "acl.h"
64
Mingming Cao617ba132006-10-11 01:20:53 -070065#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040066# define ea_idebug(inode, fmt, ...) \
67 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
68 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69# define ea_bdebug(bh, fmt, ...) \
70 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
71 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070072#else
Joe Perchesace36ad2012-03-19 23:11:43 -040073# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
74# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075#endif
76
Tahsin Erdogan47387402017-06-22 11:28:55 -040077static void ext4_xattr_block_cache_insert(struct mb_cache *,
78 struct buffer_head *);
79static struct buffer_head *
80ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
81 struct mb_cache_entry **);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -040082static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
83 size_t value_count);
Tahsin Erdogandaf83282017-06-22 11:52:03 -040084static void ext4_xattr_rehash(struct ext4_xattr_header *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070085
Eric Biggersd6006182017-04-29 23:47:50 -040086static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070087 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040088#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080089 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
90 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091#endif
Mingming Cao617ba132006-10-11 01:20:53 -070092 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040093#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070094 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095#endif
96};
97
Stephen Hemminger11e27522010-05-13 17:53:18 -070098const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070099 &ext4_xattr_user_handler,
100 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400101#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800102 &posix_acl_access_xattr_handler,
103 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400105#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700106 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107#endif
108 NULL
109};
110
Tahsin Erdogan47387402017-06-22 11:28:55 -0400111#define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
112 inode->i_sb->s_fs_info)->s_ea_block_cache)
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400113
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400114#define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
115 inode->i_sb->s_fs_info)->s_ea_inode_cache)
116
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400117static int
118ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
119 struct inode *inode);
120
Tahsin Erdogan33d201e2017-06-21 21:17:10 -0400121#ifdef CONFIG_LOCKDEP
122void ext4_xattr_inode_set_class(struct inode *ea_inode)
123{
124 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
125}
126#endif
127
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400128static __le32 ext4_xattr_block_csum(struct inode *inode,
129 sector_t block_nr,
130 struct ext4_xattr_header *hdr)
131{
132 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400133 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400134 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400135 __u32 dummy_csum = 0;
136 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400137
Theodore Ts'od6a77102013-04-09 23:59:55 -0400138 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
139 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400140 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
141 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
142 offset += sizeof(dummy_csum);
143 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
144 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400145
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400146 return cpu_to_le32(csum);
147}
148
149static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400150 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400151{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400152 struct ext4_xattr_header *hdr = BHDR(bh);
153 int ret = 1;
154
155 if (ext4_has_metadata_csum(inode->i_sb)) {
156 lock_buffer(bh);
157 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
158 bh->b_blocknr, hdr));
159 unlock_buffer(bh);
160 }
161 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400162}
163
164static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400165 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400166{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400167 if (ext4_has_metadata_csum(inode->i_sb))
168 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
169 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400170}
171
Stephen Hemminger11e27522010-05-13 17:53:18 -0700172static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700173ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700175 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176
Mingming Cao617ba132006-10-11 01:20:53 -0700177 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
178 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700179 return handler;
180}
181
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400183ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
184 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400186 struct ext4_xattr_entry *e = entry;
187
Eric Biggersd7614cc2016-12-01 14:57:29 -0500188 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189 while (!IS_LAST_ENTRY(e)) {
190 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400192 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400195
Eric Biggersd7614cc2016-12-01 14:57:29 -0500196 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400197 while (!IS_LAST_ENTRY(entry)) {
Eric Biggersce3fd192018-03-29 14:31:42 -0400198 u32 size = le32_to_cpu(entry->e_value_size);
199
200 if (size > INT_MAX)
201 return -EFSCORRUPTED;
202
203 if (size != 0 && entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500204 u16 offs = le16_to_cpu(entry->e_value_offs);
Eric Biggersd7614cc2016-12-01 14:57:29 -0500205 void *value;
206
207 /*
208 * The value cannot overlap the names, and the value
209 * with padding cannot extend beyond 'end'. Check both
210 * the padded and unpadded sizes, since the size may
211 * overflow to 0 when adding padding.
212 */
213 if (offs > end - value_start)
214 return -EFSCORRUPTED;
215 value = value_start + offs;
216 if (value < (void *)e + sizeof(u32) ||
217 size > end - value ||
218 EXT4_XATTR_SIZE(size) > end - value)
219 return -EFSCORRUPTED;
220 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400221 entry = EXT4_XATTR_NEXT(entry);
222 }
223
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 return 0;
225}
226
227static inline int
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400228__ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
229 const char *function, unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230{
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400231 int error = -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400232
233 if (buffer_verified(bh))
234 return 0;
235
Mingming Cao617ba132006-10-11 01:20:53 -0700236 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 BHDR(bh)->h_blocks != cpu_to_le32(1))
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400238 goto errout;
239 error = -EFSBADCRC;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400240 if (!ext4_xattr_block_csum_verify(inode, bh))
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400241 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400242 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
243 bh->b_data);
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400244errout:
245 if (error)
246 __ext4_error_inode(inode, function, line, 0,
247 "corrupted xattr block %llu",
248 (unsigned long long) bh->b_blocknr);
249 else
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400250 set_buffer_verified(bh);
251 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252}
253
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400254#define ext4_xattr_check_block(inode, bh) \
255 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
256
257
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400258static int
259__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
260 void *end, const char *function, unsigned int line)
261{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400262 int error = -EFSCORRUPTED;
263
Eric Biggers290ab232016-12-01 14:51:58 -0500264 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400265 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400266 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400267 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400268errout:
269 if (error)
270 __ext4_error_inode(inode, function, line, 0,
271 "corrupted in-inode xattr");
272 return error;
273}
274
275#define xattr_check_inode(inode, header, end) \
276 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
277
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278static int
Theodore Ts'o9496005d2018-03-30 20:00:56 -0400279xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
280 void *end, int name_index, const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281{
Theodore Ts'o9496005d2018-03-30 20:00:56 -0400282 struct ext4_xattr_entry *entry, *next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 size_t name_len;
284 int cmp = 1;
285
286 if (name == NULL)
287 return -EINVAL;
288 name_len = strlen(name);
Theodore Ts'o9496005d2018-03-30 20:00:56 -0400289 for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
290 next = EXT4_XATTR_NEXT(entry);
291 if ((void *) next >= end) {
292 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
293 return -EFSCORRUPTED;
294 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 cmp = name_index - entry->e_name_index;
296 if (!cmp)
297 cmp = name_len - entry->e_name_len;
298 if (!cmp)
299 cmp = memcmp(name, entry->e_name, name_len);
300 if (cmp <= 0 && (sorted || cmp == 0))
301 break;
302 }
303 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 return cmp ? -ENODATA : 0;
305}
306
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400307static u32
308ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
309{
310 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
311}
312
313static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
314{
315 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500316 (u32) inode_peek_iversion_raw(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400317}
318
319static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
320{
321 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500322 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400323}
324
325static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
326{
327 return (u32)ea_inode->i_atime.tv_sec;
328}
329
330static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
331{
332 ea_inode->i_atime.tv_sec = hash;
333}
334
Andreas Dilgere50e5122017-06-21 21:10:32 -0400335/*
336 * Read the EA value from an inode.
337 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400338static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400339{
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400340 int blocksize = 1 << ea_inode->i_blkbits;
341 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
342 int tail_size = (size % blocksize) ?: blocksize;
343 struct buffer_head *bhs_inline[8];
344 struct buffer_head **bhs = bhs_inline;
345 int i, ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400346
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400347 if (bh_count > ARRAY_SIZE(bhs_inline)) {
348 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
349 if (!bhs)
350 return -ENOMEM;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400351 }
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400352
353 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
354 true /* wait */, bhs);
355 if (ret)
356 goto free_bhs;
357
358 for (i = 0; i < bh_count; i++) {
359 /* There shouldn't be any holes in ea_inode. */
360 if (!bhs[i]) {
361 ret = -EFSCORRUPTED;
362 goto put_bhs;
363 }
364 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
365 i < bh_count - 1 ? blocksize : tail_size);
366 }
367 ret = 0;
368put_bhs:
369 for (i = 0; i < bh_count; i++)
370 brelse(bhs[i]);
371free_bhs:
372 if (bhs != bhs_inline)
373 kfree(bhs);
374 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400375}
376
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400377#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
378
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400379static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400380 u32 ea_inode_hash, struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400381{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400382 struct inode *inode;
383 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400384
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400385 inode = ext4_iget(parent->i_sb, ea_ino);
386 if (IS_ERR(inode)) {
387 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400388 ext4_error(parent->i_sb,
389 "error while reading EA inode %lu err=%d", ea_ino,
390 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400391 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400392 }
393
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400394 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400395 ext4_error(parent->i_sb,
396 "error while reading EA inode %lu is_bad_inode",
397 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400398 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400399 goto error;
400 }
401
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400402 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400403 ext4_error(parent->i_sb,
404 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
405 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400406 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400407 goto error;
408 }
409
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400410 ext4_xattr_inode_set_class(inode);
411
412 /*
413 * Check whether this is an old Lustre-style xattr inode. Lustre
414 * implementation does not have hash validation, rather it has a
415 * backpointer from ea_inode to the parent inode.
416 */
417 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
418 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
419 inode->i_generation == parent->i_generation) {
420 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
421 ext4_xattr_inode_set_ref(inode, 1);
422 } else {
423 inode_lock(inode);
424 inode->i_flags |= S_NOQUOTA;
425 inode_unlock(inode);
426 }
427
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400428 *ea_inode = inode;
429 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400430error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400431 iput(inode);
432 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400433}
434
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400435static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400436ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
437 struct ext4_xattr_entry *entry, void *buffer,
438 size_t size)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400439{
440 u32 hash;
441
442 /* Verify stored hash matches calculated hash. */
443 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
444 if (hash != ext4_xattr_inode_get_hash(ea_inode))
445 return -EFSCORRUPTED;
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400446
447 if (entry) {
448 __le32 e_hash, tmp_data;
449
450 /* Verify entry hash. */
451 tmp_data = cpu_to_le32(hash);
452 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
453 &tmp_data, 1);
454 if (e_hash != entry->e_hash)
455 return -EFSCORRUPTED;
456 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400457 return 0;
458}
459
Andreas Dilgere50e5122017-06-21 21:10:32 -0400460/*
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400461 * Read xattr value from the EA inode.
Andreas Dilgere50e5122017-06-21 21:10:32 -0400462 */
463static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400464ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
465 void *buffer, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400466{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400467 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400468 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400469 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400470
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400471 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400472 le32_to_cpu(entry->e_hash), &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400473 if (err) {
474 ea_inode = NULL;
475 goto out;
476 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400477
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400478 if (i_size_read(ea_inode) != size) {
479 ext4_warning_inode(ea_inode,
480 "ea_inode file size=%llu entry size=%zu",
481 i_size_read(ea_inode), size);
482 err = -EFSCORRUPTED;
483 goto out;
484 }
485
486 err = ext4_xattr_inode_read(ea_inode, buffer, size);
487 if (err)
488 goto out;
489
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400490 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
491 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
492 size);
493 if (err) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400494 ext4_warning_inode(ea_inode,
495 "EA inode hash validation failed");
496 goto out;
497 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400498
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400499 if (ea_inode_cache)
500 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
501 ext4_xattr_inode_get_hash(ea_inode),
502 ea_inode->i_ino, true /* reusable */);
503 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400504out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400505 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400506 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400507}
508
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509static int
Mingming Cao617ba132006-10-11 01:20:53 -0700510ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 void *buffer, size_t buffer_size)
512{
513 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700514 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 size_t size;
Theodore Ts'o9496005d2018-03-30 20:00:56 -0400516 void *end;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400518 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519
520 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
521 name_index, name, buffer, (long)buffer_size);
522
523 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700524 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400526 ea_idebug(inode, "reading block %llu",
527 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700528 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529 if (!bh)
530 goto cleanup;
531 ea_bdebug(bh, "b_count=%d, refcount=%d",
532 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400533 error = ext4_xattr_check_block(inode, bh);
534 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535 goto cleanup;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400536 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 entry = BFIRST(bh);
Theodore Ts'o9496005d2018-03-30 20:00:56 -0400538 end = bh->b_data + bh->b_size;
539 error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540 if (error)
541 goto cleanup;
542 size = le32_to_cpu(entry->e_value_size);
543 if (buffer) {
544 error = -ERANGE;
545 if (size > buffer_size)
546 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400547 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400548 error = ext4_xattr_inode_get(inode, entry, buffer,
549 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400550 if (error)
551 goto cleanup;
552 } else {
553 memcpy(buffer, bh->b_data +
554 le16_to_cpu(entry->e_value_offs), size);
555 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 }
557 error = size;
558
559cleanup:
560 brelse(bh);
561 return error;
562}
563
Tao Ma879b3822012-12-05 10:28:46 -0500564int
Mingming Cao617ba132006-10-11 01:20:53 -0700565ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566 void *buffer, size_t buffer_size)
567{
Mingming Cao617ba132006-10-11 01:20:53 -0700568 struct ext4_xattr_ibody_header *header;
569 struct ext4_xattr_entry *entry;
570 struct ext4_inode *raw_inode;
571 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 size_t size;
573 void *end;
574 int error;
575
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500576 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700578 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579 if (error)
580 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700581 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700583 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400584 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 if (error)
586 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400587 entry = IFIRST(header);
Theodore Ts'o9496005d2018-03-30 20:00:56 -0400588 error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 if (error)
590 goto cleanup;
591 size = le32_to_cpu(entry->e_value_size);
592 if (buffer) {
593 error = -ERANGE;
594 if (size > buffer_size)
595 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400596 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400597 error = ext4_xattr_inode_get(inode, entry, buffer,
598 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400599 if (error)
600 goto cleanup;
601 } else {
602 memcpy(buffer, (void *)IFIRST(header) +
603 le16_to_cpu(entry->e_value_offs), size);
604 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700605 }
606 error = size;
607
608cleanup:
609 brelse(iloc.bh);
610 return error;
611}
612
613/*
Mingming Cao617ba132006-10-11 01:20:53 -0700614 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615 *
616 * Copy an extended attribute into the buffer
617 * provided, or compute the buffer size required.
618 * Buffer is NULL to compute the size of the buffer required.
619 *
620 * Returns a negative error number on failure, or the number of bytes
621 * used / required on success.
622 */
623int
Mingming Cao617ba132006-10-11 01:20:53 -0700624ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 void *buffer, size_t buffer_size)
626{
627 int error;
628
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500629 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
630 return -EIO;
631
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400632 if (strlen(name) > 255)
633 return -ERANGE;
634
Mingming Cao617ba132006-10-11 01:20:53 -0700635 down_read(&EXT4_I(inode)->xattr_sem);
636 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637 buffer_size);
638 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700639 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700641 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700642 return error;
643}
644
645static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000646ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 char *buffer, size_t buffer_size)
648{
649 size_t rest = buffer_size;
650
Mingming Cao617ba132006-10-11 01:20:53 -0700651 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700652 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700653 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100655 if (handler && (!handler->list || handler->list(dentry))) {
656 const char *prefix = handler->prefix ?: handler->name;
657 size_t prefix_len = strlen(prefix);
658 size_t size = prefix_len + entry->e_name_len + 1;
659
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 if (buffer) {
661 if (size > rest)
662 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100663 memcpy(buffer, prefix, prefix_len);
664 buffer += prefix_len;
665 memcpy(buffer, entry->e_name, entry->e_name_len);
666 buffer += entry->e_name_len;
667 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 }
669 rest -= size;
670 }
671 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100672 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673}
674
675static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000676ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677{
David Howells2b0143b2015-03-17 22:25:59 +0000678 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679 struct buffer_head *bh = NULL;
680 int error;
681
682 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
683 buffer, (long)buffer_size);
684
685 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700686 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400688 ea_idebug(inode, "reading block %llu",
689 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700690 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 error = -EIO;
692 if (!bh)
693 goto cleanup;
694 ea_bdebug(bh, "b_count=%d, refcount=%d",
695 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400696 error = ext4_xattr_check_block(inode, bh);
697 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 goto cleanup;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400699 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000700 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701
702cleanup:
703 brelse(bh);
704
705 return error;
706}
707
708static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000709ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710{
David Howells2b0143b2015-03-17 22:25:59 +0000711 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700712 struct ext4_xattr_ibody_header *header;
713 struct ext4_inode *raw_inode;
714 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 void *end;
716 int error;
717
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500718 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700720 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 if (error)
722 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700723 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700725 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400726 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 if (error)
728 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000729 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 buffer, buffer_size);
731
732cleanup:
733 brelse(iloc.bh);
734 return error;
735}
736
737/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400738 * Inode operation listxattr()
739 *
740 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 *
742 * Copy a list of attribute names into the buffer
743 * provided, or compute the buffer size required.
744 * Buffer is NULL to compute the size of the buffer required.
745 *
746 * Returns a negative error number on failure, or the number of bytes
747 * used / required on success.
748 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400749ssize_t
750ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500752 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753
David Howells2b0143b2015-03-17 22:25:59 +0000754 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500755 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
756 if (ret < 0)
757 goto errout;
758 if (buffer) {
759 buffer += ret;
760 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500762 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
763 if (ret < 0)
764 goto errout;
765 ret += ret2;
766errout:
David Howells2b0143b2015-03-17 22:25:59 +0000767 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500768 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769}
770
771/*
Mingming Cao617ba132006-10-11 01:20:53 -0700772 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773 * not set, set it.
774 */
Mingming Cao617ba132006-10-11 01:20:53 -0700775static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 struct super_block *sb)
777{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400778 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 return;
780
liang xie5d601252014-05-12 22:06:43 -0400781 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700782 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400783 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400784 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786}
787
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400788int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
789{
790 struct ext4_iloc iloc = { .bh = NULL };
791 struct buffer_head *bh = NULL;
792 struct ext4_inode *raw_inode;
793 struct ext4_xattr_ibody_header *header;
794 struct ext4_xattr_entry *entry;
795 qsize_t ea_inode_refs = 0;
796 void *end;
797 int ret;
798
799 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
800
801 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
802 ret = ext4_get_inode_loc(inode, &iloc);
803 if (ret)
804 goto out;
805 raw_inode = ext4_raw_inode(&iloc);
806 header = IHDR(inode, raw_inode);
807 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
808 ret = xattr_check_inode(inode, header, end);
809 if (ret)
810 goto out;
811
812 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
813 entry = EXT4_XATTR_NEXT(entry))
814 if (entry->e_value_inum)
815 ea_inode_refs++;
816 }
817
818 if (EXT4_I(inode)->i_file_acl) {
819 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
820 if (!bh) {
821 ret = -EIO;
822 goto out;
823 }
824
Theodore Ts'ode05ca82018-03-30 15:42:25 -0400825 ret = ext4_xattr_check_block(inode, bh);
826 if (ret)
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400827 goto out;
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400828
829 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
830 entry = EXT4_XATTR_NEXT(entry))
831 if (entry->e_value_inum)
832 ea_inode_refs++;
833 }
834 *usage = ea_inode_refs + 1;
835 ret = 0;
836out:
837 brelse(iloc.bh);
838 brelse(bh);
839 return ret;
840}
841
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400842static inline size_t round_up_cluster(struct inode *inode, size_t length)
843{
844 struct super_block *sb = inode->i_sb;
845 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
846 inode->i_blkbits);
847 size_t mask = ~(cluster_size - 1);
848
849 return (length + cluster_size - 1) & mask;
850}
851
852static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
853{
854 int err;
855
856 err = dquot_alloc_inode(inode);
857 if (err)
858 return err;
859 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
860 if (err)
861 dquot_free_inode(inode);
862 return err;
863}
864
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400865static void ext4_xattr_inode_free_quota(struct inode *parent,
866 struct inode *ea_inode,
867 size_t len)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400868{
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400869 if (ea_inode &&
870 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
871 return;
872 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
873 dquot_free_inode(parent);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400874}
875
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400876int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
877 struct buffer_head *block_bh, size_t value_len,
878 bool is_create)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400879{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400880 int credits;
881 int blocks;
882
883 /*
884 * 1) Owner inode update
885 * 2) Ref count update on old xattr block
886 * 3) new xattr block
887 * 4) block bitmap update for new xattr block
888 * 5) group descriptor for new xattr block
889 * 6) block bitmap update for old xattr block
890 * 7) group descriptor for old block
891 *
892 * 6 & 7 can happen if we have two racing threads T_a and T_b
893 * which are each trying to set an xattr on inodes I_a and I_b
894 * which were both initially sharing an xattr block.
895 */
896 credits = 7;
897
898 /* Quota updates. */
899 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
900
901 /*
902 * In case of inline data, we may push out the data to a block,
903 * so we need to reserve credits for this eventuality
904 */
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400905 if (inode && ext4_has_inline_data(inode))
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400906 credits += ext4_writepage_trans_blocks(inode) + 1;
907
908 /* We are done if ea_inode feature is not enabled. */
909 if (!ext4_has_feature_ea_inode(sb))
910 return credits;
911
912 /* New ea_inode, inode map, block bitmap, group descriptor. */
913 credits += 4;
914
915 /* Data blocks. */
916 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
917
918 /* Indirection block or one level of extent tree. */
919 blocks += 1;
920
921 /* Block bitmap and group descriptor updates for each block. */
922 credits += blocks * 2;
923
924 /* Blocks themselves. */
925 credits += blocks;
926
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400927 if (!is_create) {
928 /* Dereference ea_inode holding old xattr value.
929 * Old ea_inode, inode map, block bitmap, group descriptor.
930 */
931 credits += 4;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400932
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400933 /* Data blocks for old ea_inode. */
934 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400935
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400936 /* Indirection block or one level of extent tree for old
937 * ea_inode.
938 */
939 blocks += 1;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400940
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400941 /* Block bitmap and group descriptor updates for each block. */
942 credits += blocks * 2;
943 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400944
945 /* We may need to clone the existing xattr block in which case we need
946 * to increment ref counts for existing ea_inodes referenced by it.
947 */
948 if (block_bh) {
949 struct ext4_xattr_entry *entry = BFIRST(block_bh);
950
951 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
952 if (entry->e_value_inum)
953 /* Ref count update on ea_inode. */
954 credits += 1;
955 }
956 return credits;
957}
958
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400959static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
960 int credits, struct buffer_head *bh,
961 bool dirty, bool block_csum)
962{
963 int error;
964
965 if (!ext4_handle_valid(handle))
966 return 0;
967
968 if (handle->h_buffer_credits >= credits)
969 return 0;
970
971 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
972 if (!error)
973 return 0;
974 if (error < 0) {
975 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
976 return error;
977 }
978
979 if (bh && dirty) {
980 if (block_csum)
981 ext4_xattr_block_csum_set(inode, bh);
982 error = ext4_handle_dirty_metadata(handle, NULL, bh);
983 if (error) {
984 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
985 error);
986 return error;
987 }
988 }
989
990 error = ext4_journal_restart(handle, credits);
991 if (error) {
992 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
993 return error;
994 }
995
996 if (bh) {
997 error = ext4_journal_get_write_access(handle, bh);
998 if (error) {
999 ext4_warning(inode->i_sb,
1000 "Get write access failed (error %d)",
1001 error);
1002 return error;
1003 }
1004 }
1005 return 0;
1006}
1007
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001008static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
1009 int ref_change)
1010{
1011 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
1012 struct ext4_iloc iloc;
1013 s64 ref_count;
1014 u32 hash;
1015 int ret;
1016
1017 inode_lock(ea_inode);
1018
1019 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1020 if (ret) {
1021 iloc.bh = NULL;
1022 goto out;
1023 }
1024
1025 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1026 ref_count += ref_change;
1027 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1028
1029 if (ref_change > 0) {
1030 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1031 ea_inode->i_ino, ref_count);
1032
1033 if (ref_count == 1) {
1034 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1035 ea_inode->i_ino, ea_inode->i_nlink);
1036
1037 set_nlink(ea_inode, 1);
1038 ext4_orphan_del(handle, ea_inode);
1039
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001040 if (ea_inode_cache) {
1041 hash = ext4_xattr_inode_get_hash(ea_inode);
1042 mb_cache_entry_create(ea_inode_cache,
1043 GFP_NOFS, hash,
1044 ea_inode->i_ino,
1045 true /* reusable */);
1046 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001047 }
1048 } else {
1049 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1050 ea_inode->i_ino, ref_count);
1051
1052 if (ref_count == 0) {
1053 WARN_ONCE(ea_inode->i_nlink != 1,
1054 "EA inode %lu i_nlink=%u",
1055 ea_inode->i_ino, ea_inode->i_nlink);
1056
1057 clear_nlink(ea_inode);
1058 ext4_orphan_add(handle, ea_inode);
1059
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001060 if (ea_inode_cache) {
1061 hash = ext4_xattr_inode_get_hash(ea_inode);
1062 mb_cache_entry_delete(ea_inode_cache, hash,
1063 ea_inode->i_ino);
1064 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001065 }
1066 }
1067
1068 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1069 iloc.bh = NULL;
1070 if (ret)
1071 ext4_warning_inode(ea_inode,
1072 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1073out:
1074 brelse(iloc.bh);
1075 inode_unlock(ea_inode);
1076 return ret;
1077}
1078
1079static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1080{
1081 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1082}
1083
1084static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1085{
1086 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1087}
1088
1089static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1090 struct ext4_xattr_entry *first)
1091{
1092 struct inode *ea_inode;
1093 struct ext4_xattr_entry *entry;
1094 struct ext4_xattr_entry *failed_entry;
1095 unsigned int ea_ino;
1096 int err, saved_err;
1097
1098 for (entry = first; !IS_LAST_ENTRY(entry);
1099 entry = EXT4_XATTR_NEXT(entry)) {
1100 if (!entry->e_value_inum)
1101 continue;
1102 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001103 err = ext4_xattr_inode_iget(parent, ea_ino,
1104 le32_to_cpu(entry->e_hash),
1105 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001106 if (err)
1107 goto cleanup;
1108 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1109 if (err) {
1110 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1111 iput(ea_inode);
1112 goto cleanup;
1113 }
1114 iput(ea_inode);
1115 }
1116 return 0;
1117
1118cleanup:
1119 saved_err = err;
1120 failed_entry = entry;
1121
1122 for (entry = first; entry != failed_entry;
1123 entry = EXT4_XATTR_NEXT(entry)) {
1124 if (!entry->e_value_inum)
1125 continue;
1126 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001127 err = ext4_xattr_inode_iget(parent, ea_ino,
1128 le32_to_cpu(entry->e_hash),
1129 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001130 if (err) {
1131 ext4_warning(parent->i_sb,
1132 "cleanup ea_ino %u iget error %d", ea_ino,
1133 err);
1134 continue;
1135 }
1136 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1137 if (err)
1138 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1139 err);
1140 iput(ea_inode);
1141 }
1142 return saved_err;
1143}
1144
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001145static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001146ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1147 struct buffer_head *bh,
1148 struct ext4_xattr_entry *first, bool block_csum,
1149 struct ext4_xattr_inode_array **ea_inode_array,
1150 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001151{
1152 struct inode *ea_inode;
1153 struct ext4_xattr_entry *entry;
1154 bool dirty = false;
1155 unsigned int ea_ino;
1156 int err;
1157 int credits;
1158
1159 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1160 credits = 2 + extra_credits;
1161
1162 for (entry = first; !IS_LAST_ENTRY(entry);
1163 entry = EXT4_XATTR_NEXT(entry)) {
1164 if (!entry->e_value_inum)
1165 continue;
1166 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001167 err = ext4_xattr_inode_iget(parent, ea_ino,
1168 le32_to_cpu(entry->e_hash),
1169 &ea_inode);
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001170 if (err)
1171 continue;
1172
1173 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1174 if (err) {
1175 ext4_warning_inode(ea_inode,
1176 "Expand inode array err=%d", err);
1177 iput(ea_inode);
1178 continue;
1179 }
1180
1181 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1182 dirty, block_csum);
1183 if (err) {
1184 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1185 err);
1186 continue;
1187 }
1188
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001189 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1190 if (err) {
1191 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1192 err);
1193 continue;
1194 }
1195
1196 if (!skip_quota)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001197 ext4_xattr_inode_free_quota(parent, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001198 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001199
1200 /*
1201 * Forget about ea_inode within the same transaction that
1202 * decrements the ref count. This avoids duplicate decrements in
1203 * case the rest of the work spills over to subsequent
1204 * transactions.
1205 */
1206 entry->e_value_inum = 0;
1207 entry->e_value_size = 0;
1208
1209 dirty = true;
1210 }
1211
1212 if (dirty) {
1213 /*
1214 * Note that we are deliberately skipping csum calculation for
1215 * the final update because we do not expect any journal
1216 * restarts until xattr block is freed.
1217 */
1218
1219 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1220 if (err)
1221 ext4_warning_inode(parent,
1222 "handle dirty metadata err=%d", err);
1223 }
1224}
1225
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001227 * Release the xattr block BH: If the reference count is > 1, decrement it;
1228 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 */
1230static void
Mingming Cao617ba132006-10-11 01:20:53 -07001231ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001232 struct buffer_head *bh,
1233 struct ext4_xattr_inode_array **ea_inode_array,
1234 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001236 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001237 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001238 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239
liang xie5d601252014-05-12 22:06:43 -04001240 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001241 error = ext4_journal_get_write_access(handle, bh);
1242 if (error)
1243 goto out;
1244
1245 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001246 hash = le32_to_cpu(BHDR(bh)->h_hash);
1247 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1248 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001250 /*
1251 * This must happen under buffer lock for
1252 * ext4_xattr_block_set() to reliably detect freed block
1253 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001254 if (ea_block_cache)
1255 mb_cache_entry_delete(ea_block_cache, hash,
1256 bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001258 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001259
1260 if (ext4_has_feature_ea_inode(inode->i_sb))
1261 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1262 BFIRST(bh),
1263 true /* block_csum */,
1264 ea_inode_array,
1265 extra_credits,
1266 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001267 ext4_free_blocks(handle, inode, bh, 0, 1,
1268 EXT4_FREE_BLOCKS_METADATA |
1269 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001271 ref--;
1272 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1273 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1274 struct mb_cache_entry *ce;
1275
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001276 if (ea_block_cache) {
1277 ce = mb_cache_entry_get(ea_block_cache, hash,
1278 bh->b_blocknr);
1279 if (ce) {
1280 ce->e_reusable = 1;
1281 mb_cache_entry_put(ea_block_cache, ce);
1282 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001283 }
1284 }
1285
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001286 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001287 /*
1288 * Beware of this ugliness: Releasing of xattr block references
1289 * from different inodes can race and so we have to protect
1290 * from a race where someone else frees the block (and releases
1291 * its journal_head) before we are done dirtying the buffer. In
1292 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001293 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001294 * that function can call sync_dirty_buffer() so for that case
1295 * we handle the dirtying after unlocking the buffer.
1296 */
1297 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001298 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001299 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001300 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001301 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001302 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001303 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001304 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001305 ea_bdebug(bh, "refcount now=%d; releasing",
1306 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001308out:
1309 ext4_std_error(inode->i_sb, error);
1310 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311}
1312
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001313/*
1314 * Find the available free space for EAs. This also returns the total number of
1315 * bytes used by EA entries.
1316 */
1317static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1318 size_t *min_offs, void *base, int *total)
1319{
1320 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001321 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001322 size_t offs = le16_to_cpu(last->e_value_offs);
1323 if (offs < *min_offs)
1324 *min_offs = offs;
1325 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001326 if (total)
1327 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001328 }
1329 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1330}
1331
Andreas Dilgere50e5122017-06-21 21:10:32 -04001332/*
1333 * Write the value of the EA in an inode.
1334 */
1335static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1336 const void *buf, int bufsize)
1337{
1338 struct buffer_head *bh = NULL;
1339 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001340 int blocksize = ea_inode->i_sb->s_blocksize;
1341 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001342 int csize, wsize = 0;
1343 int ret = 0;
1344 int retries = 0;
1345
1346retry:
1347 while (ret >= 0 && ret < max_blocks) {
1348 struct ext4_map_blocks map;
1349 map.m_lblk = block += ret;
1350 map.m_len = max_blocks -= ret;
1351
1352 ret = ext4_map_blocks(handle, ea_inode, &map,
1353 EXT4_GET_BLOCKS_CREATE);
1354 if (ret <= 0) {
1355 ext4_mark_inode_dirty(handle, ea_inode);
1356 if (ret == -ENOSPC &&
1357 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1358 ret = 0;
1359 goto retry;
1360 }
1361 break;
1362 }
1363 }
1364
1365 if (ret < 0)
1366 return ret;
1367
1368 block = 0;
1369 while (wsize < bufsize) {
1370 if (bh != NULL)
1371 brelse(bh);
1372 csize = (bufsize - wsize) > blocksize ? blocksize :
1373 bufsize - wsize;
1374 bh = ext4_getblk(handle, ea_inode, block, 0);
1375 if (IS_ERR(bh))
1376 return PTR_ERR(bh);
1377 ret = ext4_journal_get_write_access(handle, bh);
1378 if (ret)
1379 goto out;
1380
1381 memcpy(bh->b_data, buf, csize);
1382 set_buffer_uptodate(bh);
1383 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1384
1385 buf += csize;
1386 wsize += csize;
1387 block += 1;
1388 }
1389
1390 inode_lock(ea_inode);
1391 i_size_write(ea_inode, wsize);
1392 ext4_update_i_disksize(ea_inode, wsize);
1393 inode_unlock(ea_inode);
1394
1395 ext4_mark_inode_dirty(handle, ea_inode);
1396
1397out:
1398 brelse(bh);
1399
1400 return ret;
1401}
1402
1403/*
1404 * Create an inode to store the value of a large EA.
1405 */
1406static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001407 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001408{
1409 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001410 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001411 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001412
1413 /*
1414 * Let the next inode be the goal, so we try and allocate the EA inode
1415 * in the same group, or nearby one.
1416 */
1417 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001418 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001419 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001420 if (!IS_ERR(ea_inode)) {
1421 ea_inode->i_op = &ext4_file_inode_operations;
1422 ea_inode->i_fop = &ext4_file_operations;
1423 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001424 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001425 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001426 ext4_xattr_inode_set_ref(ea_inode, 1);
1427 ext4_xattr_inode_set_hash(ea_inode, hash);
1428 err = ext4_mark_inode_dirty(handle, ea_inode);
1429 if (!err)
1430 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001431 if (err) {
1432 iput(ea_inode);
1433 return ERR_PTR(err);
1434 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001435
1436 /*
1437 * Xattr inodes are shared therefore quota charging is performed
1438 * at a higher level.
1439 */
1440 dquot_free_inode(ea_inode);
1441 dquot_drop(ea_inode);
1442 inode_lock(ea_inode);
1443 ea_inode->i_flags |= S_NOQUOTA;
1444 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001445 }
1446
1447 return ea_inode;
1448}
1449
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001450static struct inode *
1451ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1452 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001453{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001454 struct inode *ea_inode;
1455 struct mb_cache_entry *ce;
1456 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1457 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001458
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001459 if (!ea_inode_cache)
1460 return NULL;
1461
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001462 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1463 if (!ce)
1464 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001465
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001466 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1467 if (!ea_data) {
1468 mb_cache_entry_put(ea_inode_cache, ce);
1469 return NULL;
1470 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001471
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001472 while (ce) {
1473 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1474 if (!IS_ERR(ea_inode) &&
1475 !is_bad_inode(ea_inode) &&
1476 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1477 i_size_read(ea_inode) == value_len &&
1478 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001479 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1480 value_len) &&
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001481 !memcmp(value, ea_data, value_len)) {
1482 mb_cache_entry_touch(ea_inode_cache, ce);
1483 mb_cache_entry_put(ea_inode_cache, ce);
1484 kvfree(ea_data);
1485 return ea_inode;
1486 }
1487
1488 if (!IS_ERR(ea_inode))
1489 iput(ea_inode);
1490 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1491 }
1492 kvfree(ea_data);
1493 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001494}
1495
1496/*
1497 * Add value of the EA in an inode.
1498 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001499static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1500 const void *value, size_t value_len,
1501 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001502{
1503 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001504 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001505 int err;
1506
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001507 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1508 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1509 if (ea_inode) {
1510 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1511 if (err) {
1512 iput(ea_inode);
1513 return err;
1514 }
1515
1516 *ret_inode = ea_inode;
1517 return 0;
1518 }
1519
Andreas Dilgere50e5122017-06-21 21:10:32 -04001520 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001521 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001522 if (IS_ERR(ea_inode))
1523 return PTR_ERR(ea_inode);
1524
1525 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001526 if (err) {
1527 ext4_xattr_inode_dec_ref(handle, ea_inode);
1528 iput(ea_inode);
1529 return err;
1530 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001531
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001532 if (EA_INODE_CACHE(inode))
1533 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1534 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001535
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001536 *ret_inode = ea_inode;
1537 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001538}
1539
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001540/*
1541 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1542 * feature is enabled.
1543 */
1544#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1545
Andreas Dilgere50e5122017-06-21 21:10:32 -04001546static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1547 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001548 handle_t *handle, struct inode *inode,
1549 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550{
Mingming Cao617ba132006-10-11 01:20:53 -07001551 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001552 struct ext4_xattr_entry *here = s->here;
1553 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001554 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001555 struct inode *old_ea_inode = NULL;
1556 struct inode *new_ea_inode = NULL;
1557 size_t old_size, new_size;
1558 int ret;
1559
1560 /* Space used by old and new values. */
1561 old_size = (!s->not_found && !here->e_value_inum) ?
1562 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1563 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1564
1565 /*
1566 * Optimization for the simple case when old and new values have the
1567 * same padded sizes. Not applicable if external inodes are involved.
1568 */
1569 if (new_size && new_size == old_size) {
1570 size_t offs = le16_to_cpu(here->e_value_offs);
1571 void *val = s->base + offs;
1572
1573 here->e_value_size = cpu_to_le32(i->value_len);
1574 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1575 memset(val, 0, new_size);
1576 } else {
1577 memcpy(val, i->value, i->value_len);
1578 /* Clear padding bytes. */
1579 memset(val + i->value_len, 0, new_size - i->value_len);
1580 }
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001581 goto update_hash;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001582 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001583
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 /* Compute min_offs and last. */
1585 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001586 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001587 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 size_t offs = le16_to_cpu(last->e_value_offs);
1589 if (offs < min_offs)
1590 min_offs = offs;
1591 }
1592 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001593
1594 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001596 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001597
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001598 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1599 if (!s->not_found)
1600 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001601
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001602 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1603 ret = -ENOSPC;
1604 goto out;
1605 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001606
1607 /*
1608 * If storing the value in an external inode is an option,
1609 * reserve space for xattr entries/names in the external
1610 * attribute block so that a long value does not occupy the
1611 * whole space and prevent futher entries being added.
1612 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001613 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1614 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001615 (min_offs + old_size - new_size) <
1616 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1617 ret = -ENOSPC;
1618 goto out;
1619 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001620 }
1621
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001622 /*
1623 * Getting access to old and new ea inodes is subject to failures.
1624 * Finish that work before doing any modifications to the xattr data.
1625 */
1626 if (!s->not_found && here->e_value_inum) {
1627 ret = ext4_xattr_inode_iget(inode,
1628 le32_to_cpu(here->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001629 le32_to_cpu(here->e_hash),
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001630 &old_ea_inode);
1631 if (ret) {
1632 old_ea_inode = NULL;
1633 goto out;
1634 }
1635 }
1636 if (i->value && in_inode) {
1637 WARN_ON_ONCE(!i->value_len);
1638
1639 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1640 if (ret)
1641 goto out;
1642
1643 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1644 i->value_len,
1645 &new_ea_inode);
1646 if (ret) {
1647 new_ea_inode = NULL;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001648 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001649 goto out;
1650 }
1651 }
1652
1653 if (old_ea_inode) {
1654 /* We are ready to release ref count on the old_ea_inode. */
1655 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1656 if (ret) {
1657 /* Release newly required ref count on new_ea_inode. */
1658 if (new_ea_inode) {
1659 int err;
1660
1661 err = ext4_xattr_inode_dec_ref(handle,
1662 new_ea_inode);
1663 if (err)
1664 ext4_warning_inode(new_ea_inode,
1665 "dec ref new_ea_inode err=%d",
1666 err);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001667 ext4_xattr_inode_free_quota(inode, new_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001668 i->value_len);
1669 }
1670 goto out;
1671 }
1672
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001673 ext4_xattr_inode_free_quota(inode, old_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001674 le32_to_cpu(here->e_value_size));
1675 }
1676
1677 /* No failures allowed past this point. */
1678
1679 if (!s->not_found && here->e_value_offs) {
1680 /* Remove the old value. */
1681 void *first_val = s->base + min_offs;
1682 size_t offs = le16_to_cpu(here->e_value_offs);
1683 void *val = s->base + offs;
1684
1685 memmove(first_val + old_size, first_val, val - first_val);
1686 memset(first_val, 0, old_size);
1687 min_offs += old_size;
1688
1689 /* Adjust all value offsets. */
1690 last = s->first;
1691 while (!IS_LAST_ENTRY(last)) {
1692 size_t o = le16_to_cpu(last->e_value_offs);
1693
1694 if (!last->e_value_inum &&
1695 last->e_value_size && o < offs)
1696 last->e_value_offs = cpu_to_le16(o + old_size);
1697 last = EXT4_XATTR_NEXT(last);
1698 }
1699 }
1700
1701 if (!i->value) {
1702 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001703 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001704
1705 last = ENTRY((void *)last - size);
1706 memmove(here, (void *)here + size,
1707 (void *)last - (void *)here + sizeof(__u32));
1708 memset(last, 0, size);
1709 } else if (s->not_found) {
1710 /* Insert new name. */
1711 size_t size = EXT4_XATTR_LEN(name_len);
1712 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1713
1714 memmove((void *)here + size, here, rest);
1715 memset(here, 0, size);
1716 here->e_name_index = i->name_index;
1717 here->e_name_len = name_len;
1718 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001720 /* This is an update, reset value info. */
1721 here->e_value_inum = 0;
1722 here->e_value_offs = 0;
1723 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 }
1725
1726 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001727 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001728 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001729 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001730 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001731 void *val = s->base + min_offs - new_size;
1732
1733 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001734 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001735 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001736 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001737 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001738 /* Clear padding bytes. */
1739 memset(val + i->value_len, 0,
1740 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001741 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001742 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001743 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001745
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001746update_hash:
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001747 if (i->value) {
1748 __le32 hash = 0;
1749
1750 /* Entry hash calculation. */
1751 if (in_inode) {
1752 __le32 crc32c_hash;
1753
1754 /*
1755 * Feed crc32c hash instead of the raw value for entry
1756 * hash calculation. This is to avoid walking
1757 * potentially long value buffer again.
1758 */
1759 crc32c_hash = cpu_to_le32(
1760 ext4_xattr_inode_get_hash(new_ea_inode));
1761 hash = ext4_xattr_hash_entry(here->e_name,
1762 here->e_name_len,
1763 &crc32c_hash, 1);
1764 } else if (is_block) {
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001765 __le32 *value = s->base + le16_to_cpu(
1766 here->e_value_offs);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001767
1768 hash = ext4_xattr_hash_entry(here->e_name,
1769 here->e_name_len, value,
1770 new_size >> 2);
1771 }
1772 here->e_hash = hash;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001773 }
1774
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001775 if (is_block)
1776 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1777
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001778 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001779out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001780 iput(old_ea_inode);
1781 iput(new_ea_inode);
1782 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783}
1784
Mingming Cao617ba132006-10-11 01:20:53 -07001785struct ext4_xattr_block_find {
1786 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787 struct buffer_head *bh;
1788};
1789
1790static int
Mingming Cao617ba132006-10-11 01:20:53 -07001791ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1792 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793{
1794 struct super_block *sb = inode->i_sb;
1795 int error;
1796
1797 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1798 i->name_index, i->name, i->value, (long)i->value_len);
1799
Mingming Cao617ba132006-10-11 01:20:53 -07001800 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001802 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803 error = -EIO;
1804 if (!bs->bh)
1805 goto cleanup;
1806 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1807 atomic_read(&(bs->bh->b_count)),
1808 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Theodore Ts'ode05ca82018-03-30 15:42:25 -04001809 error = ext4_xattr_check_block(inode, bs->bh);
1810 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001812 /* Find the named attribute. */
1813 bs->s.base = BHDR(bs->bh);
1814 bs->s.first = BFIRST(bs->bh);
1815 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1816 bs->s.here = bs->s.first;
Theodore Ts'o9496005d2018-03-30 20:00:56 -04001817 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1818 i->name_index, i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 if (error && error != -ENODATA)
1820 goto cleanup;
1821 bs->s.not_found = error;
1822 }
1823 error = 0;
1824
1825cleanup:
1826 return error;
1827}
1828
1829static int
Mingming Cao617ba132006-10-11 01:20:53 -07001830ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1831 struct ext4_xattr_info *i,
1832 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001833{
1834 struct super_block *sb = inode->i_sb;
1835 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001836 struct ext4_xattr_search s_copy = bs->s;
1837 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001838 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001839 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001840 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001841 struct inode *ea_inode = NULL, *tmp_inode;
1842 size_t old_ea_inode_quota = 0;
1843 unsigned int ea_ino;
1844
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845
Mingming Cao617ba132006-10-11 01:20:53 -07001846#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001847
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001848 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001849 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001850 error = ext4_journal_get_write_access(handle, bs->bh);
1851 if (error)
1852 goto cleanup;
1853 lock_buffer(bs->bh);
1854
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001855 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001856 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1857
1858 /*
1859 * This must happen under buffer lock for
1860 * ext4_xattr_block_set() to reliably detect modified
1861 * block
1862 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001863 if (ea_block_cache)
1864 mb_cache_entry_delete(ea_block_cache, hash,
1865 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001867 error = ext4_xattr_set_entry(i, s, handle, inode,
1868 true /* is_block */);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001869 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001870 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001871 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 goto bad_block;
1873 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001874 error = ext4_handle_dirty_metadata(handle,
1875 inode,
1876 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877 if (error)
1878 goto cleanup;
1879 goto inserted;
1880 } else {
1881 int offset = (char *)s->here - bs->bh->b_data;
1882
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001883 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001884 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001885 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001886 error = -ENOMEM;
1887 if (s->base == NULL)
1888 goto cleanup;
1889 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1890 s->first = ENTRY(header(s->base)+1);
1891 header(s->base)->h_refcount = cpu_to_le32(1);
1892 s->here = ENTRY(s->base + offset);
1893 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001894
1895 /*
1896 * If existing entry points to an xattr inode, we need
1897 * to prevent ext4_xattr_set_entry() from decrementing
1898 * ref count on it because the reference belongs to the
1899 * original block. In this case, make the entry look
1900 * like it has an empty value.
1901 */
1902 if (!s->not_found && s->here->e_value_inum) {
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001903 ea_ino = le32_to_cpu(s->here->e_value_inum);
1904 error = ext4_xattr_inode_iget(inode, ea_ino,
1905 le32_to_cpu(s->here->e_hash),
1906 &tmp_inode);
1907 if (error)
1908 goto cleanup;
1909
1910 if (!ext4_test_inode_state(tmp_inode,
1911 EXT4_STATE_LUSTRE_EA_INODE)) {
1912 /*
1913 * Defer quota free call for previous
1914 * inode until success is guaranteed.
1915 */
1916 old_ea_inode_quota = le32_to_cpu(
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001917 s->here->e_value_size);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001918 }
1919 iput(tmp_inode);
1920
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001921 s->here->e_value_inum = 0;
1922 s->here->e_value_size = 0;
1923 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001924 }
1925 } else {
1926 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001927 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001928 /* assert(header == s->base) */
1929 error = -ENOMEM;
1930 if (s->base == NULL)
1931 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001932 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001933 header(s->base)->h_blocks = cpu_to_le32(1);
1934 header(s->base)->h_refcount = cpu_to_le32(1);
1935 s->first = ENTRY(header(s->base)+1);
1936 s->here = ENTRY(header(s->base)+1);
1937 s->end = s->base + sb->s_blocksize;
1938 }
1939
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001940 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001941 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001942 goto bad_block;
1943 if (error)
1944 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001945
1946 if (i->value && s->here->e_value_inum) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001947 /*
1948 * A ref count on ea_inode has been taken as part of the call to
1949 * ext4_xattr_set_entry() above. We would like to drop this
1950 * extra ref but we have to wait until the xattr block is
1951 * initialized and has its own ref count on the ea_inode.
1952 */
1953 ea_ino = le32_to_cpu(s->here->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001954 error = ext4_xattr_inode_iget(inode, ea_ino,
1955 le32_to_cpu(s->here->e_hash),
1956 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001957 if (error) {
1958 ea_inode = NULL;
1959 goto cleanup;
1960 }
1961 }
1962
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963inserted:
1964 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001965 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1966 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001967 if (new_bh) {
1968 /* We found an identical block in the cache. */
1969 if (new_bh == bs->bh)
1970 ea_bdebug(new_bh, "keeping");
1971 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001972 u32 ref;
1973
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001974 WARN_ON_ONCE(dquot_initialize_needed(inode));
1975
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001976 /* The old block is released after updating
1977 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001978 error = dquot_alloc_block(inode,
1979 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001980 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001982 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001983 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001984 new_bh);
1985 if (error)
1986 goto cleanup_dquot;
1987 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001988 /*
1989 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001990 * freeing, rehashing or adding references to
1991 * xattr block. Once we hold buffer lock xattr
1992 * block's state is stable so we can check
1993 * whether the block got freed / rehashed or
1994 * not. Since we unhash mbcache entry under
1995 * buffer lock when freeing / rehashing xattr
1996 * block, checking whether entry is still
1997 * hashed is reliable. Same rules hold for
1998 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001999 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002000 if (hlist_bl_unhashed(&ce->e_hash_list) ||
2001 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05002002 /*
2003 * Undo everything and check mbcache
2004 * again.
2005 */
2006 unlock_buffer(new_bh);
2007 dquot_free_block(inode,
2008 EXT4_C2B(EXT4_SB(sb),
2009 1));
2010 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002011 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05002012 ce = NULL;
2013 new_bh = NULL;
2014 goto inserted;
2015 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002016 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2017 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2018 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
2019 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002021 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002022 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002023 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002024 error = ext4_handle_dirty_metadata(handle,
2025 inode,
2026 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027 if (error)
2028 goto cleanup_dquot;
2029 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04002030 mb_cache_entry_touch(ea_block_cache, ce);
2031 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032 ce = NULL;
2033 } else if (bs->bh && s->base == bs->bh->b_data) {
2034 /* We were modifying this block in-place. */
2035 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganec000222017-08-05 22:41:42 -04002036 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 new_bh = bs->bh;
2038 get_bh(new_bh);
2039 } else {
2040 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002041 ext4_fsblk_t goal, block;
2042
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002043 WARN_ON_ONCE(dquot_initialize_needed(inode));
2044
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002045 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04002046 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002047
2048 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002049 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002050 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
2051
Allison Henderson55f020d2011-05-25 07:41:26 -04002052 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2053 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054 if (error)
2055 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002056
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002057 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002058 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
2059
Joe Perchesace36ad2012-03-19 23:11:43 -04002060 ea_idebug(inode, "creating block %llu",
2061 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062
2063 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05002064 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002065 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002066getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05002067 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002068 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002069 goto cleanup;
2070 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002071 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2072 ENTRY(header(s->base)+1));
2073 if (error)
2074 goto getblk_failed;
2075 if (ea_inode) {
2076 /* Drop the extra ref on ea_inode. */
2077 error = ext4_xattr_inode_dec_ref(handle,
2078 ea_inode);
2079 if (error)
2080 ext4_warning_inode(ea_inode,
2081 "dec ref error=%d",
2082 error);
2083 iput(ea_inode);
2084 ea_inode = NULL;
2085 }
2086
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002087 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002088 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 if (error) {
2090 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002091 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092 goto getblk_failed;
2093 }
2094 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002095 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002096 set_buffer_uptodate(new_bh);
2097 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002098 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002099 error = ext4_handle_dirty_metadata(handle, inode,
2100 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101 if (error)
2102 goto cleanup;
2103 }
2104 }
2105
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002106 if (old_ea_inode_quota)
2107 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002108
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07002110 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002111
2112 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002113 if (bs->bh && bs->bh != new_bh) {
2114 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2115
2116 ext4_xattr_release_block(handle, inode, bs->bh,
2117 &ea_inode_array,
2118 0 /* extra_credits */);
2119 ext4_xattr_inode_array_free(ea_inode_array);
2120 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002121 error = 0;
2122
2123cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002124 if (ea_inode) {
2125 int error2;
2126
2127 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2128 if (error2)
2129 ext4_warning_inode(ea_inode, "dec ref error=%d",
2130 error2);
2131
2132 /* If there was an error, revert the quota charge. */
2133 if (error)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002134 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002135 i_size_read(ea_inode));
2136 iput(ea_inode);
2137 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002139 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140 brelse(new_bh);
2141 if (!(bs->bh && s->base == bs->bh->b_data))
2142 kfree(s->base);
2143
2144 return error;
2145
2146cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002147 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 goto cleanup;
2149
2150bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002151 EXT4_ERROR_INODE(inode, "bad block %llu",
2152 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153 goto cleanup;
2154
2155#undef header
2156}
2157
Tao Ma879b3822012-12-05 10:28:46 -05002158int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2159 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002160{
Mingming Cao617ba132006-10-11 01:20:53 -07002161 struct ext4_xattr_ibody_header *header;
2162 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 int error;
2164
Mingming Cao617ba132006-10-11 01:20:53 -07002165 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002167 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002168 header = IHDR(inode, raw_inode);
2169 is->s.base = is->s.first = IFIRST(header);
2170 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002171 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002172 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002173 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 if (error)
2175 return error;
2176 /* Find the named attribute. */
Theodore Ts'o9496005d2018-03-30 20:00:56 -04002177 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2178 i->name_index, i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002179 if (error && error != -ENODATA)
2180 return error;
2181 is->s.not_found = error;
2182 }
2183 return 0;
2184}
2185
Tao Ma0d812f72012-12-10 14:06:02 -05002186int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2187 struct ext4_xattr_info *i,
2188 struct ext4_xattr_ibody_find *is)
2189{
2190 struct ext4_xattr_ibody_header *header;
2191 struct ext4_xattr_search *s = &is->s;
2192 int error;
2193
2194 if (EXT4_I(inode)->i_extra_isize == 0)
2195 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002196 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002197 if (error) {
2198 if (error == -ENOSPC &&
2199 ext4_has_inline_data(inode)) {
2200 error = ext4_try_to_evict_inline_data(handle, inode,
2201 EXT4_XATTR_LEN(strlen(i->name) +
2202 EXT4_XATTR_SIZE(i->value_len)));
2203 if (error)
2204 return error;
2205 error = ext4_xattr_ibody_find(inode, i, is);
2206 if (error)
2207 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002208 error = ext4_xattr_set_entry(i, s, handle, inode,
2209 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002210 }
2211 if (error)
2212 return error;
2213 }
2214 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2215 if (!IS_LAST_ENTRY(s->first)) {
2216 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2217 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2218 } else {
2219 header->h_magic = cpu_to_le32(0);
2220 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2221 }
2222 return 0;
2223}
2224
Andreas Dilgere50e5122017-06-21 21:10:32 -04002225static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002226 struct ext4_xattr_info *i,
2227 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002228{
Mingming Cao617ba132006-10-11 01:20:53 -07002229 struct ext4_xattr_ibody_header *header;
2230 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 int error;
2232
Mingming Cao617ba132006-10-11 01:20:53 -07002233 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002235 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236 if (error)
2237 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002238 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002240 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002241 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 } else {
2243 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002244 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002245 }
2246 return 0;
2247}
2248
Jan Kara3fd16462016-02-22 22:43:04 -05002249static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2250 struct ext4_xattr_info *i)
2251{
2252 void *value;
2253
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002254 /* When e_value_inum is set the value is stored externally. */
2255 if (s->here->e_value_inum)
2256 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002257 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2258 return 0;
2259 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2260 return !memcmp(value, i->value, i->value_len);
2261}
2262
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002263static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2264{
2265 struct buffer_head *bh;
2266 int error;
2267
2268 if (!EXT4_I(inode)->i_file_acl)
2269 return NULL;
2270 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2271 if (!bh)
2272 return ERR_PTR(-EIO);
2273 error = ext4_xattr_check_block(inode, bh);
2274 if (error)
2275 return ERR_PTR(error);
2276 return bh;
2277}
2278
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279/*
Mingming Cao617ba132006-10-11 01:20:53 -07002280 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002282 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283 * is NULL to remove an existing extended attribute, and non-NULL to
2284 * either replace an existing extended attribute, or create a new extended
2285 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2286 * specify that an extended attribute must exist and must not exist
2287 * previous to the call, respectively.
2288 *
2289 * Returns 0, or a negative error number on failure.
2290 */
2291int
Mingming Cao617ba132006-10-11 01:20:53 -07002292ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 const char *name, const void *value, size_t value_len,
2294 int flags)
2295{
Mingming Cao617ba132006-10-11 01:20:53 -07002296 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002297 .name_index = name_index,
2298 .name = name,
2299 .value = value,
2300 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002301 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002302 };
Mingming Cao617ba132006-10-11 01:20:53 -07002303 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002304 .s = { .not_found = -ENODATA, },
2305 };
Mingming Cao617ba132006-10-11 01:20:53 -07002306 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002307 .s = { .not_found = -ENODATA, },
2308 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002309 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002310 int error;
2311
2312 if (!name)
2313 return -EINVAL;
2314 if (strlen(name) > 255)
2315 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002316
Theodore Ts'oc755e252017-01-11 21:50:46 -05002317 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002318
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002319 /* Check journal credits under write lock. */
2320 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002321 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002322 int credits;
2323
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002324 bh = ext4_xattr_get_block(inode);
2325 if (IS_ERR(bh)) {
2326 error = PTR_ERR(bh);
2327 goto cleanup;
2328 }
2329
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002330 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2331 value_len,
2332 flags & XATTR_CREATE);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002333 brelse(bh);
2334
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002335 if (!ext4_handle_has_enough_credits(handle, credits)) {
2336 error = -ENOSPC;
2337 goto cleanup;
2338 }
2339 }
2340
Eric Sandeen66543612011-10-26 03:32:07 -04002341 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002342 if (error)
2343 goto cleanup;
2344
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002345 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002346 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2347 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002348 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002349 }
2350
Mingming Cao617ba132006-10-11 01:20:53 -07002351 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 if (error)
2353 goto cleanup;
2354 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002355 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002356 if (error)
2357 goto cleanup;
2358 if (is.s.not_found && bs.s.not_found) {
2359 error = -ENODATA;
2360 if (flags & XATTR_REPLACE)
2361 goto cleanup;
2362 error = 0;
2363 if (!value)
2364 goto cleanup;
2365 } else {
2366 error = -EEXIST;
2367 if (flags & XATTR_CREATE)
2368 goto cleanup;
2369 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002370
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002371 if (!value) {
2372 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002373 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002374 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002375 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002376 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002377 error = 0;
2378 /* Xattr value did not change? Save us some work and bail out */
2379 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2380 goto cleanup;
2381 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2382 goto cleanup;
2383
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002384 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2385 (EXT4_XATTR_SIZE(i.value_len) >
2386 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2387 i.in_inode = 1;
2388retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002389 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002390 if (!error && !bs.s.not_found) {
2391 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002392 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002393 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002394 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2395 error = ext4_xattr_block_find(inode, &i, &bs);
2396 if (error)
2397 goto cleanup;
2398 }
Mingming Cao617ba132006-10-11 01:20:53 -07002399 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002400 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002401 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002402 error = ext4_xattr_ibody_set(handle, inode, &i,
2403 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002404 } else if (error == -ENOSPC) {
2405 /*
2406 * Xattr does not fit in the block, store at
2407 * external inode if possible.
2408 */
2409 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2410 !i.in_inode) {
2411 i.in_inode = 1;
2412 goto retry_inode;
2413 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 }
2415 }
2416 }
2417 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002418 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002419 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002420 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002421 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002422 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002423 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002424 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002425 * error != 0.
2426 */
2427 is.iloc.bh = NULL;
2428 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002429 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002430 }
2431
2432cleanup:
2433 brelse(is.iloc.bh);
2434 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002435 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002436 return error;
2437}
2438
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002439int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2440 bool is_create, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002441{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002442 struct buffer_head *bh;
2443 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002444
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002445 *credits = 0;
2446
2447 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002448 return 0;
2449
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002450 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002451
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002452 bh = ext4_xattr_get_block(inode);
2453 if (IS_ERR(bh)) {
2454 err = PTR_ERR(bh);
2455 } else {
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002456 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2457 value_len, is_create);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002458 brelse(bh);
2459 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002460 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002461
2462 up_read(&EXT4_I(inode)->xattr_sem);
2463 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002464}
2465
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002466/*
Mingming Cao617ba132006-10-11 01:20:53 -07002467 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 *
Mingming Cao617ba132006-10-11 01:20:53 -07002469 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002470 * attribute modification is a filesystem transaction by itself.
2471 *
2472 * Returns 0, or a negative error number on failure.
2473 */
2474int
Mingming Cao617ba132006-10-11 01:20:53 -07002475ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002476 const void *value, size_t value_len, int flags)
2477{
2478 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002479 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002481 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002482
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002483 error = dquot_initialize(inode);
2484 if (error)
2485 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002486
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002487retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002488 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2489 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002490 if (error)
2491 return error;
2492
Theodore Ts'o9924a922013-02-08 21:59:22 -05002493 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002494 if (IS_ERR(handle)) {
2495 error = PTR_ERR(handle);
2496 } else {
2497 int error2;
2498
Mingming Cao617ba132006-10-11 01:20:53 -07002499 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002500 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002501 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002502 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002503 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002504 goto retry;
2505 if (error == 0)
2506 error = error2;
2507 }
2508
2509 return error;
2510}
2511
2512/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002513 * Shift the EA entries in the inode to create space for the increased
2514 * i_extra_isize.
2515 */
2516static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2517 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002518 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002519{
2520 struct ext4_xattr_entry *last = entry;
2521 int new_offs;
2522
Jan Kara94405712016-08-29 15:41:11 -04002523 /* We always shift xattr headers further thus offsets get lower */
2524 BUG_ON(value_offs_shift > 0);
2525
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002526 /* Adjust the value offsets of the entries */
2527 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002528 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002529 new_offs = le16_to_cpu(last->e_value_offs) +
2530 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002531 last->e_value_offs = cpu_to_le16(new_offs);
2532 }
2533 }
2534 /* Shift the entries by n bytes */
2535 memmove(to, from, n);
2536}
2537
2538/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002539 * Move xattr pointed to by 'entry' from inode into external xattr block
2540 */
2541static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2542 struct ext4_inode *raw_inode,
2543 struct ext4_xattr_entry *entry)
2544{
2545 struct ext4_xattr_ibody_find *is = NULL;
2546 struct ext4_xattr_block_find *bs = NULL;
2547 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002548 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002549 struct ext4_xattr_info i = {
2550 .value = NULL,
2551 .value_len = 0,
2552 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002553 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002554 };
2555 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2556 int error;
2557
Jan Kara3f2571c2016-08-29 15:42:11 -04002558 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2559 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2560 buffer = kmalloc(value_size, GFP_NOFS);
2561 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2562 if (!is || !bs || !buffer || !b_entry_name) {
2563 error = -ENOMEM;
2564 goto out;
2565 }
2566
2567 is->s.not_found = -ENODATA;
2568 bs->s.not_found = -ENODATA;
2569 is->iloc.bh = NULL;
2570 bs->bh = NULL;
2571
2572 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002573 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04002574 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002575 if (error)
2576 goto out;
2577 } else {
2578 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2579 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2580 }
2581
Jan Kara3f2571c2016-08-29 15:42:11 -04002582 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2583 b_entry_name[entry->e_name_len] = '\0';
2584 i.name = b_entry_name;
2585
2586 error = ext4_get_inode_loc(inode, &is->iloc);
2587 if (error)
2588 goto out;
2589
2590 error = ext4_xattr_ibody_find(inode, &i, is);
2591 if (error)
2592 goto out;
2593
2594 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002595 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002596 if (error)
2597 goto out;
2598
Jan Kara3f2571c2016-08-29 15:42:11 -04002599 i.value = buffer;
2600 i.value_len = value_size;
2601 error = ext4_xattr_block_find(inode, &i, bs);
2602 if (error)
2603 goto out;
2604
2605 /* Add entry which was removed from the inode into the block */
2606 error = ext4_xattr_block_set(handle, inode, &i, bs);
2607 if (error)
2608 goto out;
2609 error = 0;
2610out:
2611 kfree(b_entry_name);
2612 kfree(buffer);
2613 if (is)
2614 brelse(is->iloc.bh);
2615 kfree(is);
2616 kfree(bs);
2617
2618 return error;
2619}
2620
Jan Karadfa20642016-08-29 15:44:11 -04002621static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2622 struct ext4_inode *raw_inode,
2623 int isize_diff, size_t ifree,
2624 size_t bfree, int *total_ino)
2625{
2626 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2627 struct ext4_xattr_entry *small_entry;
2628 struct ext4_xattr_entry *entry;
2629 struct ext4_xattr_entry *last;
2630 unsigned int entry_size; /* EA entry size */
2631 unsigned int total_size; /* EA entry size + value size */
2632 unsigned int min_total_size;
2633 int error;
2634
2635 while (isize_diff > ifree) {
2636 entry = NULL;
2637 small_entry = NULL;
2638 min_total_size = ~0U;
2639 last = IFIRST(header);
2640 /* Find the entry best suited to be pushed into EA block */
2641 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002642 total_size = EXT4_XATTR_LEN(last->e_name_len);
2643 if (!last->e_value_inum)
2644 total_size += EXT4_XATTR_SIZE(
2645 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002646 if (total_size <= bfree &&
2647 total_size < min_total_size) {
2648 if (total_size + ifree < isize_diff) {
2649 small_entry = last;
2650 } else {
2651 entry = last;
2652 min_total_size = total_size;
2653 }
2654 }
2655 }
2656
2657 if (entry == NULL) {
2658 if (small_entry == NULL)
2659 return -ENOSPC;
2660 entry = small_entry;
2661 }
2662
2663 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002664 total_size = entry_size;
2665 if (!entry->e_value_inum)
2666 total_size += EXT4_XATTR_SIZE(
2667 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002668 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2669 entry);
2670 if (error)
2671 return error;
2672
2673 *total_ino -= entry_size;
2674 ifree += total_size;
2675 bfree -= total_size;
2676 }
2677
2678 return 0;
2679}
2680
Jan Kara3f2571c2016-08-29 15:42:11 -04002681/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002682 * Expand an inode by new_extra_isize bytes when EAs are present.
2683 * Returns 0 on success or negative error number on failure.
2684 */
2685int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2686 struct ext4_inode *raw_inode, handle_t *handle)
2687{
2688 struct ext4_xattr_ibody_header *header;
Miao Xieb640b2c2017-08-06 00:55:48 -04002689 struct buffer_head *bh;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002690 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2691 static unsigned int mnt_count;
Jan Karae3014d12016-08-29 15:38:11 -04002692 size_t min_offs;
2693 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002694 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002695 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002696 int error = 0, tried_min_extra_isize = 0;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002697 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04002698 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002699
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002700retry:
Jan Karad0141192016-08-11 11:50:30 -04002701 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002702 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
Miao Xieb640b2c2017-08-06 00:55:48 -04002703 return 0;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002704
2705 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002706
2707 /*
2708 * Check if enough free space is available in the inode to shift the
2709 * entries ahead by new_extra_isize.
2710 */
2711
Jan Kara6e0cd082016-08-29 15:43:11 -04002712 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002713 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2714 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002715 total_ino = sizeof(struct ext4_xattr_ibody_header);
2716
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002717 error = xattr_check_inode(inode, header, end);
2718 if (error)
2719 goto cleanup;
2720
Jan Kara6e0cd082016-08-29 15:43:11 -04002721 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002722 if (ifree >= isize_diff)
2723 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002724
2725 /*
2726 * Enough free space isn't available in the inode, check if
2727 * EA block can hold new_extra_isize bytes.
2728 */
2729 if (EXT4_I(inode)->i_file_acl) {
2730 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2731 error = -EIO;
2732 if (!bh)
2733 goto cleanup;
Theodore Ts'ode05ca82018-03-30 15:42:25 -04002734 error = ext4_xattr_check_block(inode, bh);
2735 if (error)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002736 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002737 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002738 end = bh->b_data + bh->b_size;
2739 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002740 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2741 NULL);
Miao Xieb640b2c2017-08-06 00:55:48 -04002742 brelse(bh);
Jan Karae3014d12016-08-29 15:38:11 -04002743 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002744 if (!tried_min_extra_isize && s_min_extra_isize) {
2745 tried_min_extra_isize++;
2746 new_extra_isize = s_min_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002747 goto retry;
2748 }
Jan Karadfa20642016-08-29 15:44:11 -04002749 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002750 goto cleanup;
2751 }
2752 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002753 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002754 }
2755
Jan Karadfa20642016-08-29 15:44:11 -04002756 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2757 isize_diff, ifree, bfree,
2758 &total_ino);
2759 if (error) {
2760 if (error == -ENOSPC && !tried_min_extra_isize &&
2761 s_min_extra_isize) {
2762 tried_min_extra_isize++;
2763 new_extra_isize = s_min_extra_isize;
Jan Karadfa20642016-08-29 15:44:11 -04002764 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002765 }
Jan Karadfa20642016-08-29 15:44:11 -04002766 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002767 }
Jan Karae3014d12016-08-29 15:38:11 -04002768shift:
2769 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002770 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002771 - new_extra_isize, (void *)raw_inode +
2772 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002773 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002774 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002775
2776cleanup:
Miao Xieb640b2c2017-08-06 00:55:48 -04002777 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
Miao Xiecf0a5e82017-08-06 00:40:01 -04002778 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2779 inode->i_ino);
2780 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2781 }
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002782 return error;
2783}
2784
Andreas Dilgere50e5122017-06-21 21:10:32 -04002785#define EIA_INCR 16 /* must be 2^n */
2786#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002787
2788/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002789 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002790 * contents copied over.
2791 */
2792static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002793ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2794 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002795{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002796 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002797 /*
2798 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002799 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002800 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002801 (*ea_inode_array) =
2802 kmalloc(offsetof(struct ext4_xattr_inode_array,
2803 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002804 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002805 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002806 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002807 (*ea_inode_array)->count = 0;
2808 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002809 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002810 struct ext4_xattr_inode_array *new_array = NULL;
2811 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002812
2813 /* if new_array is NULL, this is essentially offsetof() */
2814 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002815 offsetof(struct ext4_xattr_inode_array,
2816 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002817 GFP_NOFS);
2818 if (new_array == NULL)
2819 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002820 memcpy(new_array, *ea_inode_array,
2821 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2822 kfree(*ea_inode_array);
2823 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002824 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002825 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002826 return 0;
2827}
2828
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002829/*
Mingming Cao617ba132006-10-11 01:20:53 -07002830 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002831 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002832 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002833 * all entries and decrement reference on any xattr inodes associated with this
2834 * inode. This is called immediately before an inode is freed. We have exclusive
2835 * access to the inode. If an orphan inode is deleted it will also release its
2836 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002837 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002838int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2839 struct ext4_xattr_inode_array **ea_inode_array,
2840 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002841{
2842 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002843 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002844 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002845 struct ext4_xattr_entry *entry;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002846 struct inode *ea_inode;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002847 int error;
2848
2849 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2850 NULL /* bh */,
2851 false /* dirty */,
2852 false /* block_csum */);
2853 if (error) {
2854 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2855 goto cleanup;
2856 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002858 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2859 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002860
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002861 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002862 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002863 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002864 goto cleanup;
2865 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002866
2867 error = ext4_journal_get_write_access(handle, iloc.bh);
2868 if (error) {
2869 EXT4_ERROR_INODE(inode, "write access (error %d)",
2870 error);
2871 goto cleanup;
2872 }
2873
2874 header = IHDR(inode, ext4_raw_inode(&iloc));
2875 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2876 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2877 IFIRST(header),
2878 false /* block_csum */,
2879 ea_inode_array,
2880 extra_credits,
2881 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002882 }
2883
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002884 if (EXT4_I(inode)->i_file_acl) {
2885 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2886 if (!bh) {
2887 EXT4_ERROR_INODE(inode, "block %llu read error",
2888 EXT4_I(inode)->i_file_acl);
2889 error = -EIO;
2890 goto cleanup;
2891 }
2892 error = ext4_xattr_check_block(inode, bh);
Theodore Ts'ode05ca82018-03-30 15:42:25 -04002893 if (error)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002894 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002895
2896 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2897 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002898 entry = EXT4_XATTR_NEXT(entry)) {
2899 if (!entry->e_value_inum)
2900 continue;
2901 error = ext4_xattr_inode_iget(inode,
2902 le32_to_cpu(entry->e_value_inum),
2903 le32_to_cpu(entry->e_hash),
2904 &ea_inode);
2905 if (error)
2906 continue;
2907 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002908 le32_to_cpu(entry->e_value_size));
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002909 iput(ea_inode);
2910 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002911
2912 }
2913
2914 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2915 extra_credits);
2916 /*
2917 * Update i_file_acl value in the same transaction that releases
2918 * block.
2919 */
2920 EXT4_I(inode)->i_file_acl = 0;
2921 error = ext4_mark_inode_dirty(handle, inode);
2922 if (error) {
2923 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2924 error);
2925 goto cleanup;
2926 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002927 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002928 error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002929cleanup:
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002930 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002932 return error;
2933}
2934
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002935void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002936{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002937 int idx;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002938
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002939 if (ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002940 return;
2941
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002942 for (idx = 0; idx < ea_inode_array->count; ++idx)
2943 iput(ea_inode_array->inodes[idx]);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002944 kfree(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945}
2946
2947/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04002948 * ext4_xattr_block_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949 *
Tahsin Erdogan47387402017-06-22 11:28:55 -04002950 * Create a new entry in the extended attribute block cache, and insert
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951 * it unless such an entry is already in the cache.
2952 *
2953 * Returns 0, or a negative error number on failure.
2954 */
2955static void
Tahsin Erdogan47387402017-06-22 11:28:55 -04002956ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2957 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002959 struct ext4_xattr_header *header = BHDR(bh);
2960 __u32 hash = le32_to_cpu(header->h_hash);
2961 int reusable = le32_to_cpu(header->h_refcount) <
2962 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002963 int error;
2964
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002965 if (!ea_block_cache)
2966 return;
Tahsin Erdogan47387402017-06-22 11:28:55 -04002967 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002968 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002970 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002971 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002972 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002973 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974}
2975
2976/*
Mingming Cao617ba132006-10-11 01:20:53 -07002977 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002978 *
2979 * Compare two extended attribute blocks for equality.
2980 *
2981 * Returns 0 if the blocks are equal, 1 if they differ, and
2982 * a negative error number on errors.
2983 */
2984static int
Mingming Cao617ba132006-10-11 01:20:53 -07002985ext4_xattr_cmp(struct ext4_xattr_header *header1,
2986 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002987{
Mingming Cao617ba132006-10-11 01:20:53 -07002988 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002989
2990 entry1 = ENTRY(header1+1);
2991 entry2 = ENTRY(header2+1);
2992 while (!IS_LAST_ENTRY(entry1)) {
2993 if (IS_LAST_ENTRY(entry2))
2994 return 1;
2995 if (entry1->e_hash != entry2->e_hash ||
2996 entry1->e_name_index != entry2->e_name_index ||
2997 entry1->e_name_len != entry2->e_name_len ||
2998 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002999 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003000 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3001 return 1;
Tahsin Erdogan7cec1912017-06-21 22:14:30 -04003002 if (!entry1->e_value_inum &&
3003 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003004 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3005 le32_to_cpu(entry1->e_value_size)))
3006 return 1;
3007
Mingming Cao617ba132006-10-11 01:20:53 -07003008 entry1 = EXT4_XATTR_NEXT(entry1);
3009 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010 }
3011 if (!IS_LAST_ENTRY(entry2))
3012 return 1;
3013 return 0;
3014}
3015
3016/*
Tahsin Erdogan47387402017-06-22 11:28:55 -04003017 * ext4_xattr_block_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003018 *
3019 * Find an identical extended attribute block.
3020 *
3021 * Returns a pointer to the block found, or NULL if such a block was
3022 * not found or an error occurred.
3023 */
3024static struct buffer_head *
Tahsin Erdogan47387402017-06-22 11:28:55 -04003025ext4_xattr_block_cache_find(struct inode *inode,
3026 struct ext4_xattr_header *header,
3027 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003028{
3029 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05003030 struct mb_cache_entry *ce;
Tahsin Erdogan47387402017-06-22 11:28:55 -04003031 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003032
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04003033 if (!ea_block_cache)
3034 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003035 if (!header->h_hash)
3036 return NULL; /* never share */
3037 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003038 ce = mb_cache_entry_find_first(ea_block_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039 while (ce) {
3040 struct buffer_head *bh;
3041
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003042 bh = sb_bread(inode->i_sb, ce->e_value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003043 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003044 EXT4_ERROR_INODE(inode, "block %lu read error",
Tahsin Erdoganc07dfcb2017-06-22 10:29:53 -04003045 (unsigned long)ce->e_value);
Mingming Cao617ba132006-10-11 01:20:53 -07003046 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047 *pce = ce;
3048 return bh;
3049 }
3050 brelse(bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04003051 ce = mb_cache_entry_find_next(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003052 }
3053 return NULL;
3054}
3055
3056#define NAME_HASH_SHIFT 5
3057#define VALUE_HASH_SHIFT 16
3058
3059/*
Mingming Cao617ba132006-10-11 01:20:53 -07003060 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061 *
3062 * Compute the hash of an extended attribute.
3063 */
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003064static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3065 size_t value_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066{
3067 __u32 hash = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003069 while (name_len--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003070 hash = (hash << NAME_HASH_SHIFT) ^
3071 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3072 *name++;
3073 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003074 while (value_count--) {
3075 hash = (hash << VALUE_HASH_SHIFT) ^
3076 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3077 le32_to_cpu(*value++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003078 }
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04003079 return cpu_to_le32(hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003080}
3081
3082#undef NAME_HASH_SHIFT
3083#undef VALUE_HASH_SHIFT
3084
3085#define BLOCK_HASH_SHIFT 16
3086
3087/*
Mingming Cao617ba132006-10-11 01:20:53 -07003088 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003089 *
3090 * Re-compute the extended attribute hash value after an entry has changed.
3091 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04003092static void ext4_xattr_rehash(struct ext4_xattr_header *header)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093{
Mingming Cao617ba132006-10-11 01:20:53 -07003094 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095 __u32 hash = 0;
3096
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003097 here = ENTRY(header+1);
3098 while (!IS_LAST_ENTRY(here)) {
3099 if (!here->e_hash) {
3100 /* Block is not shared if an entry's hash value == 0 */
3101 hash = 0;
3102 break;
3103 }
3104 hash = (hash << BLOCK_HASH_SHIFT) ^
3105 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3106 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07003107 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003108 }
3109 header->h_hash = cpu_to_le32(hash);
3110}
3111
3112#undef BLOCK_HASH_SHIFT
3113
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003114#define HASH_BUCKET_BITS 10
3115
Jan Kara7a2508e2016-02-22 22:35:22 -05003116struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05003117ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003118{
Jan Kara7a2508e2016-02-22 22:35:22 -05003119 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120}
3121
Jan Kara7a2508e2016-02-22 22:35:22 -05003122void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003123{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003124 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05003125 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003126}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04003127