blob: 2077d87b09f2bc24fdb77e9131df398d7c5b0720 [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
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400228ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400230 int error;
231
232 if (buffer_verified(bh))
233 return 0;
234
Mingming Cao617ba132006-10-11 01:20:53 -0700235 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400237 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400238 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400239 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400240 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
241 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400242 if (!error)
243 set_buffer_verified(bh);
244 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245}
246
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400247static int
248__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
249 void *end, const char *function, unsigned int line)
250{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400251 int error = -EFSCORRUPTED;
252
Eric Biggers290ab232016-12-01 14:51:58 -0500253 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400254 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400255 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400256 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400257errout:
258 if (error)
259 __ext4_error_inode(inode, function, line, 0,
260 "corrupted in-inode xattr");
261 return error;
262}
263
264#define xattr_check_inode(inode, header, end) \
265 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
266
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267static int
Mingming Cao617ba132006-10-11 01:20:53 -0700268ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400269 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270{
Mingming Cao617ba132006-10-11 01:20:53 -0700271 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 size_t name_len;
273 int cmp = 1;
274
275 if (name == NULL)
276 return -EINVAL;
277 name_len = strlen(name);
278 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700279 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280 cmp = name_index - entry->e_name_index;
281 if (!cmp)
282 cmp = name_len - entry->e_name_len;
283 if (!cmp)
284 cmp = memcmp(name, entry->e_name, name_len);
285 if (cmp <= 0 && (sorted || cmp == 0))
286 break;
287 }
288 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 return cmp ? -ENODATA : 0;
290}
291
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400292static u32
293ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
294{
295 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
296}
297
298static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
299{
300 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500301 (u32) inode_peek_iversion_raw(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400302}
303
304static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
305{
306 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500307 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400308}
309
310static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
311{
312 return (u32)ea_inode->i_atime.tv_sec;
313}
314
315static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
316{
317 ea_inode->i_atime.tv_sec = hash;
318}
319
Andreas Dilgere50e5122017-06-21 21:10:32 -0400320/*
321 * Read the EA value from an inode.
322 */
Tahsin Erdogan909666932017-06-21 21:57:36 -0400323static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400324{
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400325 int blocksize = 1 << ea_inode->i_blkbits;
326 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
327 int tail_size = (size % blocksize) ?: blocksize;
328 struct buffer_head *bhs_inline[8];
329 struct buffer_head **bhs = bhs_inline;
330 int i, ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400331
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400332 if (bh_count > ARRAY_SIZE(bhs_inline)) {
333 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
334 if (!bhs)
335 return -ENOMEM;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400336 }
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400337
338 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
339 true /* wait */, bhs);
340 if (ret)
341 goto free_bhs;
342
343 for (i = 0; i < bh_count; i++) {
344 /* There shouldn't be any holes in ea_inode. */
345 if (!bhs[i]) {
346 ret = -EFSCORRUPTED;
347 goto put_bhs;
348 }
349 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
350 i < bh_count - 1 ? blocksize : tail_size);
351 }
352 ret = 0;
353put_bhs:
354 for (i = 0; i < bh_count; i++)
355 brelse(bhs[i]);
356free_bhs:
357 if (bhs != bhs_inline)
358 kfree(bhs);
359 return ret;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400360}
361
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400362#define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
363
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400364static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400365 u32 ea_inode_hash, struct inode **ea_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400366{
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400367 struct inode *inode;
368 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400369
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400370 inode = ext4_iget(parent->i_sb, ea_ino);
371 if (IS_ERR(inode)) {
372 err = PTR_ERR(inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400373 ext4_error(parent->i_sb,
374 "error while reading EA inode %lu err=%d", ea_ino,
375 err);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400376 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400377 }
378
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400379 if (is_bad_inode(inode)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400380 ext4_error(parent->i_sb,
381 "error while reading EA inode %lu is_bad_inode",
382 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400383 err = -EIO;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400384 goto error;
385 }
386
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400387 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400388 ext4_error(parent->i_sb,
389 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
390 ea_ino);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400391 err = -EINVAL;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400392 goto error;
393 }
394
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400395 ext4_xattr_inode_set_class(inode);
396
397 /*
398 * Check whether this is an old Lustre-style xattr inode. Lustre
399 * implementation does not have hash validation, rather it has a
400 * backpointer from ea_inode to the parent inode.
401 */
402 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
403 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
404 inode->i_generation == parent->i_generation) {
405 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
406 ext4_xattr_inode_set_ref(inode, 1);
407 } else {
408 inode_lock(inode);
409 inode->i_flags |= S_NOQUOTA;
410 inode_unlock(inode);
411 }
412
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400413 *ea_inode = inode;
414 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400415error:
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400416 iput(inode);
417 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400418}
419
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400420static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400421ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
422 struct ext4_xattr_entry *entry, void *buffer,
423 size_t size)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400424{
425 u32 hash;
426
427 /* Verify stored hash matches calculated hash. */
428 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
429 if (hash != ext4_xattr_inode_get_hash(ea_inode))
430 return -EFSCORRUPTED;
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400431
432 if (entry) {
433 __le32 e_hash, tmp_data;
434
435 /* Verify entry hash. */
436 tmp_data = cpu_to_le32(hash);
437 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
438 &tmp_data, 1);
439 if (e_hash != entry->e_hash)
440 return -EFSCORRUPTED;
441 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400442 return 0;
443}
444
Andreas Dilgere50e5122017-06-21 21:10:32 -0400445/*
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400446 * Read xattr value from the EA inode.
Andreas Dilgere50e5122017-06-21 21:10:32 -0400447 */
448static int
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400449ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
450 void *buffer, size_t size)
Andreas Dilgere50e5122017-06-21 21:10:32 -0400451{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400452 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
Tahsin Erdoganbab79b02017-06-21 21:49:53 -0400453 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400454 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400455
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400456 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400457 le32_to_cpu(entry->e_hash), &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400458 if (err) {
459 ea_inode = NULL;
460 goto out;
461 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400462
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400463 if (i_size_read(ea_inode) != size) {
464 ext4_warning_inode(ea_inode,
465 "ea_inode file size=%llu entry size=%zu",
466 i_size_read(ea_inode), size);
467 err = -EFSCORRUPTED;
468 goto out;
469 }
470
471 err = ext4_xattr_inode_read(ea_inode, buffer, size);
472 if (err)
473 goto out;
474
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400475 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
476 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
477 size);
478 if (err) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400479 ext4_warning_inode(ea_inode,
480 "EA inode hash validation failed");
481 goto out;
482 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400483
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400484 if (ea_inode_cache)
485 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
486 ext4_xattr_inode_get_hash(ea_inode),
487 ea_inode->i_ino, true /* reusable */);
488 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400489out:
Andreas Dilgere50e5122017-06-21 21:10:32 -0400490 iput(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400491 return err;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400492}
493
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494static int
Mingming Cao617ba132006-10-11 01:20:53 -0700495ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 void *buffer, size_t buffer_size)
497{
498 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700499 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 size_t size;
501 int error;
Tahsin Erdogan47387402017-06-22 11:28:55 -0400502 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503
504 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
505 name_index, name, buffer, (long)buffer_size);
506
507 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700508 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400510 ea_idebug(inode, "reading block %llu",
511 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700512 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513 if (!bh)
514 goto cleanup;
515 ea_bdebug(bh, "b_count=%d, refcount=%d",
516 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400517 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400518 EXT4_ERROR_INODE(inode, "bad block %llu",
519 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400520 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521 goto cleanup;
522 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400523 ext4_xattr_block_cache_insert(ea_block_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400525 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526 if (error)
527 goto cleanup;
528 size = le32_to_cpu(entry->e_value_size);
529 if (buffer) {
530 error = -ERANGE;
531 if (size > buffer_size)
532 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400533 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400534 error = ext4_xattr_inode_get(inode, entry, buffer,
535 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400536 if (error)
537 goto cleanup;
538 } else {
539 memcpy(buffer, bh->b_data +
540 le16_to_cpu(entry->e_value_offs), size);
541 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542 }
543 error = size;
544
545cleanup:
546 brelse(bh);
547 return error;
548}
549
Tao Ma879b3822012-12-05 10:28:46 -0500550int
Mingming Cao617ba132006-10-11 01:20:53 -0700551ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 void *buffer, size_t buffer_size)
553{
Mingming Cao617ba132006-10-11 01:20:53 -0700554 struct ext4_xattr_ibody_header *header;
555 struct ext4_xattr_entry *entry;
556 struct ext4_inode *raw_inode;
557 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 size_t size;
559 void *end;
560 int error;
561
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500562 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700564 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 if (error)
566 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700567 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700569 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400570 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571 if (error)
572 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400573 entry = IFIRST(header);
574 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575 if (error)
576 goto cleanup;
577 size = le32_to_cpu(entry->e_value_size);
578 if (buffer) {
579 error = -ERANGE;
580 if (size > buffer_size)
581 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400582 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -0400583 error = ext4_xattr_inode_get(inode, entry, buffer,
584 size);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400585 if (error)
586 goto cleanup;
587 } else {
588 memcpy(buffer, (void *)IFIRST(header) +
589 le16_to_cpu(entry->e_value_offs), size);
590 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 }
592 error = size;
593
594cleanup:
595 brelse(iloc.bh);
596 return error;
597}
598
599/*
Mingming Cao617ba132006-10-11 01:20:53 -0700600 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601 *
602 * Copy an extended attribute into the buffer
603 * provided, or compute the buffer size required.
604 * Buffer is NULL to compute the size of the buffer required.
605 *
606 * Returns a negative error number on failure, or the number of bytes
607 * used / required on success.
608 */
609int
Mingming Cao617ba132006-10-11 01:20:53 -0700610ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611 void *buffer, size_t buffer_size)
612{
613 int error;
614
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500615 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
616 return -EIO;
617
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400618 if (strlen(name) > 255)
619 return -ERANGE;
620
Mingming Cao617ba132006-10-11 01:20:53 -0700621 down_read(&EXT4_I(inode)->xattr_sem);
622 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 buffer_size);
624 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700625 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700627 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 return error;
629}
630
631static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000632ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700633 char *buffer, size_t buffer_size)
634{
635 size_t rest = buffer_size;
636
Mingming Cao617ba132006-10-11 01:20:53 -0700637 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700638 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700639 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100641 if (handler && (!handler->list || handler->list(dentry))) {
642 const char *prefix = handler->prefix ?: handler->name;
643 size_t prefix_len = strlen(prefix);
644 size_t size = prefix_len + entry->e_name_len + 1;
645
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 if (buffer) {
647 if (size > rest)
648 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100649 memcpy(buffer, prefix, prefix_len);
650 buffer += prefix_len;
651 memcpy(buffer, entry->e_name, entry->e_name_len);
652 buffer += entry->e_name_len;
653 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654 }
655 rest -= size;
656 }
657 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100658 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659}
660
661static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000662ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663{
David Howells2b0143b2015-03-17 22:25:59 +0000664 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 struct buffer_head *bh = NULL;
666 int error;
667
668 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
669 buffer, (long)buffer_size);
670
671 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700672 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400674 ea_idebug(inode, "reading block %llu",
675 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700676 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 error = -EIO;
678 if (!bh)
679 goto cleanup;
680 ea_bdebug(bh, "b_count=%d, refcount=%d",
681 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400682 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400683 EXT4_ERROR_INODE(inode, "bad block %llu",
684 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400685 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 goto cleanup;
687 }
Tahsin Erdogan47387402017-06-22 11:28:55 -0400688 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000689 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690
691cleanup:
692 brelse(bh);
693
694 return error;
695}
696
697static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000698ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699{
David Howells2b0143b2015-03-17 22:25:59 +0000700 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700701 struct ext4_xattr_ibody_header *header;
702 struct ext4_inode *raw_inode;
703 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 void *end;
705 int error;
706
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500707 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700709 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 if (error)
711 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700712 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700714 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400715 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 if (error)
717 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000718 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 buffer, buffer_size);
720
721cleanup:
722 brelse(iloc.bh);
723 return error;
724}
725
726/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400727 * Inode operation listxattr()
728 *
729 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 *
731 * Copy a list of attribute names into the buffer
732 * provided, or compute the buffer size required.
733 * Buffer is NULL to compute the size of the buffer required.
734 *
735 * Returns a negative error number on failure, or the number of bytes
736 * used / required on success.
737 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400738ssize_t
739ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500741 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742
David Howells2b0143b2015-03-17 22:25:59 +0000743 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500744 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
745 if (ret < 0)
746 goto errout;
747 if (buffer) {
748 buffer += ret;
749 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500751 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
752 if (ret < 0)
753 goto errout;
754 ret += ret2;
755errout:
David Howells2b0143b2015-03-17 22:25:59 +0000756 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500757 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758}
759
760/*
Mingming Cao617ba132006-10-11 01:20:53 -0700761 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 * not set, set it.
763 */
Mingming Cao617ba132006-10-11 01:20:53 -0700764static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 struct super_block *sb)
766{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400767 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 return;
769
liang xie5d601252014-05-12 22:06:43 -0400770 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700771 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400772 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400773 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775}
776
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400777int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
778{
779 struct ext4_iloc iloc = { .bh = NULL };
780 struct buffer_head *bh = NULL;
781 struct ext4_inode *raw_inode;
782 struct ext4_xattr_ibody_header *header;
783 struct ext4_xattr_entry *entry;
784 qsize_t ea_inode_refs = 0;
785 void *end;
786 int ret;
787
788 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
789
790 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
791 ret = ext4_get_inode_loc(inode, &iloc);
792 if (ret)
793 goto out;
794 raw_inode = ext4_raw_inode(&iloc);
795 header = IHDR(inode, raw_inode);
796 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
797 ret = xattr_check_inode(inode, header, end);
798 if (ret)
799 goto out;
800
801 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
802 entry = EXT4_XATTR_NEXT(entry))
803 if (entry->e_value_inum)
804 ea_inode_refs++;
805 }
806
807 if (EXT4_I(inode)->i_file_acl) {
808 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
809 if (!bh) {
810 ret = -EIO;
811 goto out;
812 }
813
814 if (ext4_xattr_check_block(inode, bh)) {
815 ret = -EFSCORRUPTED;
816 goto out;
817 }
818
819 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
820 entry = EXT4_XATTR_NEXT(entry))
821 if (entry->e_value_inum)
822 ea_inode_refs++;
823 }
824 *usage = ea_inode_refs + 1;
825 ret = 0;
826out:
827 brelse(iloc.bh);
828 brelse(bh);
829 return ret;
830}
831
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400832static inline size_t round_up_cluster(struct inode *inode, size_t length)
833{
834 struct super_block *sb = inode->i_sb;
835 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
836 inode->i_blkbits);
837 size_t mask = ~(cluster_size - 1);
838
839 return (length + cluster_size - 1) & mask;
840}
841
842static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
843{
844 int err;
845
846 err = dquot_alloc_inode(inode);
847 if (err)
848 return err;
849 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
850 if (err)
851 dquot_free_inode(inode);
852 return err;
853}
854
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400855static void ext4_xattr_inode_free_quota(struct inode *parent,
856 struct inode *ea_inode,
857 size_t len)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400858{
Tahsin Erdogana6d05672017-08-24 14:25:02 -0400859 if (ea_inode &&
860 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
861 return;
862 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
863 dquot_free_inode(parent);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400864}
865
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400866int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
867 struct buffer_head *block_bh, size_t value_len,
868 bool is_create)
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400869{
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400870 int credits;
871 int blocks;
872
873 /*
874 * 1) Owner inode update
875 * 2) Ref count update on old xattr block
876 * 3) new xattr block
877 * 4) block bitmap update for new xattr block
878 * 5) group descriptor for new xattr block
879 * 6) block bitmap update for old xattr block
880 * 7) group descriptor for old block
881 *
882 * 6 & 7 can happen if we have two racing threads T_a and T_b
883 * which are each trying to set an xattr on inodes I_a and I_b
884 * which were both initially sharing an xattr block.
885 */
886 credits = 7;
887
888 /* Quota updates. */
889 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
890
891 /*
892 * In case of inline data, we may push out the data to a block,
893 * so we need to reserve credits for this eventuality
894 */
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400895 if (inode && ext4_has_inline_data(inode))
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400896 credits += ext4_writepage_trans_blocks(inode) + 1;
897
898 /* We are done if ea_inode feature is not enabled. */
899 if (!ext4_has_feature_ea_inode(sb))
900 return credits;
901
902 /* New ea_inode, inode map, block bitmap, group descriptor. */
903 credits += 4;
904
905 /* Data blocks. */
906 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
907
908 /* Indirection block or one level of extent tree. */
909 blocks += 1;
910
911 /* Block bitmap and group descriptor updates for each block. */
912 credits += blocks * 2;
913
914 /* Blocks themselves. */
915 credits += blocks;
916
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400917 if (!is_create) {
918 /* Dereference ea_inode holding old xattr value.
919 * Old ea_inode, inode map, block bitmap, group descriptor.
920 */
921 credits += 4;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400922
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400923 /* Data blocks for old ea_inode. */
924 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400925
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400926 /* Indirection block or one level of extent tree for old
927 * ea_inode.
928 */
929 blocks += 1;
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400930
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400931 /* Block bitmap and group descriptor updates for each block. */
932 credits += blocks * 2;
933 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400934
935 /* We may need to clone the existing xattr block in which case we need
936 * to increment ref counts for existing ea_inodes referenced by it.
937 */
938 if (block_bh) {
939 struct ext4_xattr_entry *entry = BFIRST(block_bh);
940
941 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
942 if (entry->e_value_inum)
943 /* Ref count update on ea_inode. */
944 credits += 1;
945 }
946 return credits;
947}
948
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400949static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
950 int credits, struct buffer_head *bh,
951 bool dirty, bool block_csum)
952{
953 int error;
954
955 if (!ext4_handle_valid(handle))
956 return 0;
957
958 if (handle->h_buffer_credits >= credits)
959 return 0;
960
961 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
962 if (!error)
963 return 0;
964 if (error < 0) {
965 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
966 return error;
967 }
968
969 if (bh && dirty) {
970 if (block_csum)
971 ext4_xattr_block_csum_set(inode, bh);
972 error = ext4_handle_dirty_metadata(handle, NULL, bh);
973 if (error) {
974 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
975 error);
976 return error;
977 }
978 }
979
980 error = ext4_journal_restart(handle, credits);
981 if (error) {
982 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
983 return error;
984 }
985
986 if (bh) {
987 error = ext4_journal_get_write_access(handle, bh);
988 if (error) {
989 ext4_warning(inode->i_sb,
990 "Get write access failed (error %d)",
991 error);
992 return error;
993 }
994 }
995 return 0;
996}
997
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400998static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
999 int ref_change)
1000{
1001 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
1002 struct ext4_iloc iloc;
1003 s64 ref_count;
1004 u32 hash;
1005 int ret;
1006
1007 inode_lock(ea_inode);
1008
1009 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1010 if (ret) {
1011 iloc.bh = NULL;
1012 goto out;
1013 }
1014
1015 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1016 ref_count += ref_change;
1017 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1018
1019 if (ref_change > 0) {
1020 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1021 ea_inode->i_ino, ref_count);
1022
1023 if (ref_count == 1) {
1024 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1025 ea_inode->i_ino, ea_inode->i_nlink);
1026
1027 set_nlink(ea_inode, 1);
1028 ext4_orphan_del(handle, ea_inode);
1029
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001030 if (ea_inode_cache) {
1031 hash = ext4_xattr_inode_get_hash(ea_inode);
1032 mb_cache_entry_create(ea_inode_cache,
1033 GFP_NOFS, hash,
1034 ea_inode->i_ino,
1035 true /* reusable */);
1036 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001037 }
1038 } else {
1039 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1040 ea_inode->i_ino, ref_count);
1041
1042 if (ref_count == 0) {
1043 WARN_ONCE(ea_inode->i_nlink != 1,
1044 "EA inode %lu i_nlink=%u",
1045 ea_inode->i_ino, ea_inode->i_nlink);
1046
1047 clear_nlink(ea_inode);
1048 ext4_orphan_add(handle, ea_inode);
1049
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001050 if (ea_inode_cache) {
1051 hash = ext4_xattr_inode_get_hash(ea_inode);
1052 mb_cache_entry_delete(ea_inode_cache, hash,
1053 ea_inode->i_ino);
1054 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001055 }
1056 }
1057
1058 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1059 iloc.bh = NULL;
1060 if (ret)
1061 ext4_warning_inode(ea_inode,
1062 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1063out:
1064 brelse(iloc.bh);
1065 inode_unlock(ea_inode);
1066 return ret;
1067}
1068
1069static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1070{
1071 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1072}
1073
1074static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1075{
1076 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1077}
1078
1079static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1080 struct ext4_xattr_entry *first)
1081{
1082 struct inode *ea_inode;
1083 struct ext4_xattr_entry *entry;
1084 struct ext4_xattr_entry *failed_entry;
1085 unsigned int ea_ino;
1086 int err, saved_err;
1087
1088 for (entry = first; !IS_LAST_ENTRY(entry);
1089 entry = EXT4_XATTR_NEXT(entry)) {
1090 if (!entry->e_value_inum)
1091 continue;
1092 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001093 err = ext4_xattr_inode_iget(parent, ea_ino,
1094 le32_to_cpu(entry->e_hash),
1095 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001096 if (err)
1097 goto cleanup;
1098 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1099 if (err) {
1100 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1101 iput(ea_inode);
1102 goto cleanup;
1103 }
1104 iput(ea_inode);
1105 }
1106 return 0;
1107
1108cleanup:
1109 saved_err = err;
1110 failed_entry = entry;
1111
1112 for (entry = first; entry != failed_entry;
1113 entry = EXT4_XATTR_NEXT(entry)) {
1114 if (!entry->e_value_inum)
1115 continue;
1116 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001117 err = ext4_xattr_inode_iget(parent, ea_ino,
1118 le32_to_cpu(entry->e_hash),
1119 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001120 if (err) {
1121 ext4_warning(parent->i_sb,
1122 "cleanup ea_ino %u iget error %d", ea_ino,
1123 err);
1124 continue;
1125 }
1126 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1127 if (err)
1128 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1129 err);
1130 iput(ea_inode);
1131 }
1132 return saved_err;
1133}
1134
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001135static void
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001136ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1137 struct buffer_head *bh,
1138 struct ext4_xattr_entry *first, bool block_csum,
1139 struct ext4_xattr_inode_array **ea_inode_array,
1140 int extra_credits, bool skip_quota)
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001141{
1142 struct inode *ea_inode;
1143 struct ext4_xattr_entry *entry;
1144 bool dirty = false;
1145 unsigned int ea_ino;
1146 int err;
1147 int credits;
1148
1149 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1150 credits = 2 + extra_credits;
1151
1152 for (entry = first; !IS_LAST_ENTRY(entry);
1153 entry = EXT4_XATTR_NEXT(entry)) {
1154 if (!entry->e_value_inum)
1155 continue;
1156 ea_ino = le32_to_cpu(entry->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001157 err = ext4_xattr_inode_iget(parent, ea_ino,
1158 le32_to_cpu(entry->e_hash),
1159 &ea_inode);
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001160 if (err)
1161 continue;
1162
1163 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1164 if (err) {
1165 ext4_warning_inode(ea_inode,
1166 "Expand inode array err=%d", err);
1167 iput(ea_inode);
1168 continue;
1169 }
1170
1171 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1172 dirty, block_csum);
1173 if (err) {
1174 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1175 err);
1176 continue;
1177 }
1178
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001179 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1180 if (err) {
1181 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1182 err);
1183 continue;
1184 }
1185
1186 if (!skip_quota)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001187 ext4_xattr_inode_free_quota(parent, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001188 le32_to_cpu(entry->e_value_size));
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04001189
1190 /*
1191 * Forget about ea_inode within the same transaction that
1192 * decrements the ref count. This avoids duplicate decrements in
1193 * case the rest of the work spills over to subsequent
1194 * transactions.
1195 */
1196 entry->e_value_inum = 0;
1197 entry->e_value_size = 0;
1198
1199 dirty = true;
1200 }
1201
1202 if (dirty) {
1203 /*
1204 * Note that we are deliberately skipping csum calculation for
1205 * the final update because we do not expect any journal
1206 * restarts until xattr block is freed.
1207 */
1208
1209 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1210 if (err)
1211 ext4_warning_inode(parent,
1212 "handle dirty metadata err=%d", err);
1213 }
1214}
1215
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216/*
Jan Karaec4cb1a2014-04-07 10:54:21 -04001217 * Release the xattr block BH: If the reference count is > 1, decrement it;
1218 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219 */
1220static void
Mingming Cao617ba132006-10-11 01:20:53 -07001221ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001222 struct buffer_head *bh,
1223 struct ext4_xattr_inode_array **ea_inode_array,
1224 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225{
Tahsin Erdogan47387402017-06-22 11:28:55 -04001226 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001227 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001228 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229
liang xie5d601252014-05-12 22:06:43 -04001230 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001231 error = ext4_journal_get_write_access(handle, bh);
1232 if (error)
1233 goto out;
1234
1235 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001236 hash = le32_to_cpu(BHDR(bh)->h_hash);
1237 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1238 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -05001240 /*
1241 * This must happen under buffer lock for
1242 * ext4_xattr_block_set() to reliably detect freed block
1243 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001244 if (ea_block_cache)
1245 mb_cache_entry_delete(ea_block_cache, hash,
1246 bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001248 unlock_buffer(bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001249
1250 if (ext4_has_feature_ea_inode(inode->i_sb))
1251 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1252 BFIRST(bh),
1253 true /* block_csum */,
1254 ea_inode_array,
1255 extra_credits,
1256 true /* skip_quota */);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001257 ext4_free_blocks(handle, inode, bh, 0, 1,
1258 EXT4_FREE_BLOCKS_METADATA |
1259 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001261 ref--;
1262 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1263 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1264 struct mb_cache_entry *ce;
1265
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001266 if (ea_block_cache) {
1267 ce = mb_cache_entry_get(ea_block_cache, hash,
1268 bh->b_blocknr);
1269 if (ce) {
1270 ce->e_reusable = 1;
1271 mb_cache_entry_put(ea_block_cache, ce);
1272 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001273 }
1274 }
1275
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001276 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001277 /*
1278 * Beware of this ugliness: Releasing of xattr block references
1279 * from different inodes can race and so we have to protect
1280 * from a race where someone else frees the block (and releases
1281 * its journal_head) before we are done dirtying the buffer. In
1282 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001283 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -04001284 * that function can call sync_dirty_buffer() so for that case
1285 * we handle the dirtying after unlocking the buffer.
1286 */
1287 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001288 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -05001289 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -04001290 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001291 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001292 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001293 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001294 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001295 ea_bdebug(bh, "refcount now=%d; releasing",
1296 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001298out:
1299 ext4_std_error(inode->i_sb, error);
1300 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301}
1302
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001303/*
1304 * Find the available free space for EAs. This also returns the total number of
1305 * bytes used by EA entries.
1306 */
1307static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1308 size_t *min_offs, void *base, int *total)
1309{
1310 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001311 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001312 size_t offs = le16_to_cpu(last->e_value_offs);
1313 if (offs < *min_offs)
1314 *min_offs = offs;
1315 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001316 if (total)
1317 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001318 }
1319 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1320}
1321
Andreas Dilgere50e5122017-06-21 21:10:32 -04001322/*
1323 * Write the value of the EA in an inode.
1324 */
1325static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1326 const void *buf, int bufsize)
1327{
1328 struct buffer_head *bh = NULL;
1329 unsigned long block = 0;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001330 int blocksize = ea_inode->i_sb->s_blocksize;
1331 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001332 int csize, wsize = 0;
1333 int ret = 0;
1334 int retries = 0;
1335
1336retry:
1337 while (ret >= 0 && ret < max_blocks) {
1338 struct ext4_map_blocks map;
1339 map.m_lblk = block += ret;
1340 map.m_len = max_blocks -= ret;
1341
1342 ret = ext4_map_blocks(handle, ea_inode, &map,
1343 EXT4_GET_BLOCKS_CREATE);
1344 if (ret <= 0) {
1345 ext4_mark_inode_dirty(handle, ea_inode);
1346 if (ret == -ENOSPC &&
1347 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1348 ret = 0;
1349 goto retry;
1350 }
1351 break;
1352 }
1353 }
1354
1355 if (ret < 0)
1356 return ret;
1357
1358 block = 0;
1359 while (wsize < bufsize) {
1360 if (bh != NULL)
1361 brelse(bh);
1362 csize = (bufsize - wsize) > blocksize ? blocksize :
1363 bufsize - wsize;
1364 bh = ext4_getblk(handle, ea_inode, block, 0);
1365 if (IS_ERR(bh))
1366 return PTR_ERR(bh);
1367 ret = ext4_journal_get_write_access(handle, bh);
1368 if (ret)
1369 goto out;
1370
1371 memcpy(bh->b_data, buf, csize);
1372 set_buffer_uptodate(bh);
1373 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1374
1375 buf += csize;
1376 wsize += csize;
1377 block += 1;
1378 }
1379
1380 inode_lock(ea_inode);
1381 i_size_write(ea_inode, wsize);
1382 ext4_update_i_disksize(ea_inode, wsize);
1383 inode_unlock(ea_inode);
1384
1385 ext4_mark_inode_dirty(handle, ea_inode);
1386
1387out:
1388 brelse(bh);
1389
1390 return ret;
1391}
1392
1393/*
1394 * Create an inode to store the value of a large EA.
1395 */
1396static struct inode *ext4_xattr_inode_create(handle_t *handle,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001397 struct inode *inode, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001398{
1399 struct inode *ea_inode = NULL;
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001400 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001401 int err;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001402
1403 /*
1404 * Let the next inode be the goal, so we try and allocate the EA inode
1405 * in the same group, or nearby one.
1406 */
1407 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
Tahsin Erdogan9e1ba002017-06-21 21:27:00 -04001408 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
Tahsin Erdogan1b917ed2017-06-21 21:21:39 -04001409 EXT4_EA_INODE_FL);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001410 if (!IS_ERR(ea_inode)) {
1411 ea_inode->i_op = &ext4_file_inode_operations;
1412 ea_inode->i_fop = &ext4_file_operations;
1413 ext4_set_aops(ea_inode);
Tahsin Erdogan33d201e2017-06-21 21:17:10 -04001414 ext4_xattr_inode_set_class(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001415 unlock_new_inode(ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001416 ext4_xattr_inode_set_ref(ea_inode, 1);
1417 ext4_xattr_inode_set_hash(ea_inode, hash);
1418 err = ext4_mark_inode_dirty(handle, ea_inode);
1419 if (!err)
1420 err = ext4_inode_attach_jinode(ea_inode);
Tahsin Erdoganbd3b9632017-06-21 21:24:31 -04001421 if (err) {
1422 iput(ea_inode);
1423 return ERR_PTR(err);
1424 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001425
1426 /*
1427 * Xattr inodes are shared therefore quota charging is performed
1428 * at a higher level.
1429 */
1430 dquot_free_inode(ea_inode);
1431 dquot_drop(ea_inode);
1432 inode_lock(ea_inode);
1433 ea_inode->i_flags |= S_NOQUOTA;
1434 inode_unlock(ea_inode);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001435 }
1436
1437 return ea_inode;
1438}
1439
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001440static struct inode *
1441ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1442 size_t value_len, u32 hash)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001443{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001444 struct inode *ea_inode;
1445 struct mb_cache_entry *ce;
1446 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1447 void *ea_data;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001448
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001449 if (!ea_inode_cache)
1450 return NULL;
1451
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001452 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1453 if (!ce)
1454 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001455
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001456 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1457 if (!ea_data) {
1458 mb_cache_entry_put(ea_inode_cache, ce);
1459 return NULL;
1460 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001461
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001462 while (ce) {
1463 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1464 if (!IS_ERR(ea_inode) &&
1465 !is_bad_inode(ea_inode) &&
1466 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1467 i_size_read(ea_inode) == value_len &&
1468 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001469 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1470 value_len) &&
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001471 !memcmp(value, ea_data, value_len)) {
1472 mb_cache_entry_touch(ea_inode_cache, ce);
1473 mb_cache_entry_put(ea_inode_cache, ce);
1474 kvfree(ea_data);
1475 return ea_inode;
1476 }
1477
1478 if (!IS_ERR(ea_inode))
1479 iput(ea_inode);
1480 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1481 }
1482 kvfree(ea_data);
1483 return NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001484}
1485
1486/*
1487 * Add value of the EA in an inode.
1488 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001489static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1490 const void *value, size_t value_len,
1491 struct inode **ret_inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001492{
1493 struct inode *ea_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001494 u32 hash;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001495 int err;
1496
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001497 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1498 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1499 if (ea_inode) {
1500 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1501 if (err) {
1502 iput(ea_inode);
1503 return err;
1504 }
1505
1506 *ret_inode = ea_inode;
1507 return 0;
1508 }
1509
Andreas Dilgere50e5122017-06-21 21:10:32 -04001510 /* Create an inode for the EA value */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001511 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001512 if (IS_ERR(ea_inode))
1513 return PTR_ERR(ea_inode);
1514
1515 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001516 if (err) {
1517 ext4_xattr_inode_dec_ref(handle, ea_inode);
1518 iput(ea_inode);
1519 return err;
1520 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001521
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001522 if (EA_INODE_CACHE(inode))
1523 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1524 ea_inode->i_ino, true /* reusable */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001525
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001526 *ret_inode = ea_inode;
1527 return 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001528}
1529
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001530/*
1531 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1532 * feature is enabled.
1533 */
1534#define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1535
Andreas Dilgere50e5122017-06-21 21:10:32 -04001536static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1537 struct ext4_xattr_search *s,
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001538 handle_t *handle, struct inode *inode,
1539 bool is_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540{
Mingming Cao617ba132006-10-11 01:20:53 -07001541 struct ext4_xattr_entry *last;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001542 struct ext4_xattr_entry *here = s->here;
1543 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001544 int in_inode = i->in_inode;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001545 struct inode *old_ea_inode = NULL;
1546 struct inode *new_ea_inode = NULL;
1547 size_t old_size, new_size;
1548 int ret;
1549
1550 /* Space used by old and new values. */
1551 old_size = (!s->not_found && !here->e_value_inum) ?
1552 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1553 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1554
1555 /*
1556 * Optimization for the simple case when old and new values have the
1557 * same padded sizes. Not applicable if external inodes are involved.
1558 */
1559 if (new_size && new_size == old_size) {
1560 size_t offs = le16_to_cpu(here->e_value_offs);
1561 void *val = s->base + offs;
1562
1563 here->e_value_size = cpu_to_le32(i->value_len);
1564 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1565 memset(val, 0, new_size);
1566 } else {
1567 memcpy(val, i->value, i->value_len);
1568 /* Clear padding bytes. */
1569 memset(val + i->value_len, 0, new_size - i->value_len);
1570 }
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001571 goto update_hash;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001572 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001573
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 /* Compute min_offs and last. */
1575 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -07001576 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001577 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 size_t offs = le16_to_cpu(last->e_value_offs);
1579 if (offs < min_offs)
1580 min_offs = offs;
1581 }
1582 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001583
1584 /* Check whether we have enough space. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001586 size_t free;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001587
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001588 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1589 if (!s->not_found)
1590 free += EXT4_XATTR_LEN(name_len) + old_size;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001591
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001592 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1593 ret = -ENOSPC;
1594 goto out;
1595 }
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001596
1597 /*
1598 * If storing the value in an external inode is an option,
1599 * reserve space for xattr entries/names in the external
1600 * attribute block so that a long value does not occupy the
1601 * whole space and prevent futher entries being added.
1602 */
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001603 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1604 new_size && is_block &&
Tahsin Erdogan9c6e7852017-06-22 11:48:53 -04001605 (min_offs + old_size - new_size) <
1606 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1607 ret = -ENOSPC;
1608 goto out;
1609 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 }
1611
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001612 /*
1613 * Getting access to old and new ea inodes is subject to failures.
1614 * Finish that work before doing any modifications to the xattr data.
1615 */
1616 if (!s->not_found && here->e_value_inum) {
1617 ret = ext4_xattr_inode_iget(inode,
1618 le32_to_cpu(here->e_value_inum),
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001619 le32_to_cpu(here->e_hash),
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001620 &old_ea_inode);
1621 if (ret) {
1622 old_ea_inode = NULL;
1623 goto out;
1624 }
1625 }
1626 if (i->value && in_inode) {
1627 WARN_ON_ONCE(!i->value_len);
1628
1629 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1630 if (ret)
1631 goto out;
1632
1633 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1634 i->value_len,
1635 &new_ea_inode);
1636 if (ret) {
1637 new_ea_inode = NULL;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001638 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001639 goto out;
1640 }
1641 }
1642
1643 if (old_ea_inode) {
1644 /* We are ready to release ref count on the old_ea_inode. */
1645 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1646 if (ret) {
1647 /* Release newly required ref count on new_ea_inode. */
1648 if (new_ea_inode) {
1649 int err;
1650
1651 err = ext4_xattr_inode_dec_ref(handle,
1652 new_ea_inode);
1653 if (err)
1654 ext4_warning_inode(new_ea_inode,
1655 "dec ref new_ea_inode err=%d",
1656 err);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001657 ext4_xattr_inode_free_quota(inode, new_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001658 i->value_len);
1659 }
1660 goto out;
1661 }
1662
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001663 ext4_xattr_inode_free_quota(inode, old_ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001664 le32_to_cpu(here->e_value_size));
1665 }
1666
1667 /* No failures allowed past this point. */
1668
1669 if (!s->not_found && here->e_value_offs) {
1670 /* Remove the old value. */
1671 void *first_val = s->base + min_offs;
1672 size_t offs = le16_to_cpu(here->e_value_offs);
1673 void *val = s->base + offs;
1674
1675 memmove(first_val + old_size, first_val, val - first_val);
1676 memset(first_val, 0, old_size);
1677 min_offs += old_size;
1678
1679 /* Adjust all value offsets. */
1680 last = s->first;
1681 while (!IS_LAST_ENTRY(last)) {
1682 size_t o = le16_to_cpu(last->e_value_offs);
1683
1684 if (!last->e_value_inum &&
1685 last->e_value_size && o < offs)
1686 last->e_value_offs = cpu_to_le16(o + old_size);
1687 last = EXT4_XATTR_NEXT(last);
1688 }
1689 }
1690
1691 if (!i->value) {
1692 /* Remove old name. */
Mingming Cao617ba132006-10-11 01:20:53 -07001693 size_t size = EXT4_XATTR_LEN(name_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001694
1695 last = ENTRY((void *)last - size);
1696 memmove(here, (void *)here + size,
1697 (void *)last - (void *)here + sizeof(__u32));
1698 memset(last, 0, size);
1699 } else if (s->not_found) {
1700 /* Insert new name. */
1701 size_t size = EXT4_XATTR_LEN(name_len);
1702 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1703
1704 memmove((void *)here + size, here, rest);
1705 memset(here, 0, size);
1706 here->e_name_index = i->name_index;
1707 here->e_name_len = name_len;
1708 memcpy(here->e_name, i->name, name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 } else {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001710 /* This is an update, reset value info. */
1711 here->e_value_inum = 0;
1712 here->e_value_offs = 0;
1713 here->e_value_size = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 }
1715
1716 if (i->value) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001717 /* Insert new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001718 if (in_inode) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001719 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001720 } else if (i->value_len) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001721 void *val = s->base + min_offs - new_size;
1722
1723 here->e_value_offs = cpu_to_le16(min_offs - new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001724 if (i->value == EXT4_ZERO_XATTR_VALUE) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001725 memset(val, 0, new_size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001726 } else {
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001727 memcpy(val, i->value, i->value_len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001728 /* Clear padding bytes. */
1729 memset(val + i->value_len, 0,
1730 new_size - i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001731 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001733 here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001734 }
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001735
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001736update_hash:
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001737 if (i->value) {
1738 __le32 hash = 0;
1739
1740 /* Entry hash calculation. */
1741 if (in_inode) {
1742 __le32 crc32c_hash;
1743
1744 /*
1745 * Feed crc32c hash instead of the raw value for entry
1746 * hash calculation. This is to avoid walking
1747 * potentially long value buffer again.
1748 */
1749 crc32c_hash = cpu_to_le32(
1750 ext4_xattr_inode_get_hash(new_ea_inode));
1751 hash = ext4_xattr_hash_entry(here->e_name,
1752 here->e_name_len,
1753 &crc32c_hash, 1);
1754 } else if (is_block) {
Tahsin Erdogan32aaf192017-08-14 08:30:06 -04001755 __le32 *value = s->base + le16_to_cpu(
1756 here->e_value_offs);
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001757
1758 hash = ext4_xattr_hash_entry(here->e_name,
1759 here->e_name_len, value,
1760 new_size >> 2);
1761 }
1762 here->e_hash = hash;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001763 }
1764
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04001765 if (is_block)
1766 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1767
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001768 ret = 0;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001769out:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001770 iput(old_ea_inode);
1771 iput(new_ea_inode);
1772 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773}
1774
Mingming Cao617ba132006-10-11 01:20:53 -07001775struct ext4_xattr_block_find {
1776 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777 struct buffer_head *bh;
1778};
1779
1780static int
Mingming Cao617ba132006-10-11 01:20:53 -07001781ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1782 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783{
1784 struct super_block *sb = inode->i_sb;
1785 int error;
1786
1787 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1788 i->name_index, i->name, i->value, (long)i->value_len);
1789
Mingming Cao617ba132006-10-11 01:20:53 -07001790 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001792 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793 error = -EIO;
1794 if (!bs->bh)
1795 goto cleanup;
1796 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1797 atomic_read(&(bs->bh->b_count)),
1798 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001799 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001800 EXT4_ERROR_INODE(inode, "bad block %llu",
1801 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001802 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803 goto cleanup;
1804 }
1805 /* Find the named attribute. */
1806 bs->s.base = BHDR(bs->bh);
1807 bs->s.first = BFIRST(bs->bh);
1808 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1809 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001810 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001811 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001812 if (error && error != -ENODATA)
1813 goto cleanup;
1814 bs->s.not_found = error;
1815 }
1816 error = 0;
1817
1818cleanup:
1819 return error;
1820}
1821
1822static int
Mingming Cao617ba132006-10-11 01:20:53 -07001823ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1824 struct ext4_xattr_info *i,
1825 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826{
1827 struct super_block *sb = inode->i_sb;
1828 struct buffer_head *new_bh = NULL;
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04001829 struct ext4_xattr_search s_copy = bs->s;
1830 struct ext4_xattr_search *s = &s_copy;
Jan Kara7a2508e2016-02-22 22:35:22 -05001831 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001832 int error = 0;
Tahsin Erdogan47387402017-06-22 11:28:55 -04001833 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001834 struct inode *ea_inode = NULL, *tmp_inode;
1835 size_t old_ea_inode_quota = 0;
1836 unsigned int ea_ino;
1837
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838
Mingming Cao617ba132006-10-11 01:20:53 -07001839#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001841 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001842 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001843 error = ext4_journal_get_write_access(handle, bs->bh);
1844 if (error)
1845 goto cleanup;
1846 lock_buffer(bs->bh);
1847
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001848 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001849 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1850
1851 /*
1852 * This must happen under buffer lock for
1853 * ext4_xattr_block_set() to reliably detect modified
1854 * block
1855 */
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001856 if (ea_block_cache)
1857 mb_cache_entry_delete(ea_block_cache, hash,
1858 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001859 ea_bdebug(bs->bh, "modifying in-place");
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001860 error = ext4_xattr_set_entry(i, s, handle, inode,
1861 true /* is_block */);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001862 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001864 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 goto bad_block;
1866 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001867 error = ext4_handle_dirty_metadata(handle,
1868 inode,
1869 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001870 if (error)
1871 goto cleanup;
1872 goto inserted;
1873 } else {
1874 int offset = (char *)s->here - bs->bh->b_data;
1875
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001876 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001878 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879 error = -ENOMEM;
1880 if (s->base == NULL)
1881 goto cleanup;
1882 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1883 s->first = ENTRY(header(s->base)+1);
1884 header(s->base)->h_refcount = cpu_to_le32(1);
1885 s->here = ENTRY(s->base + offset);
1886 s->end = s->base + bs->bh->b_size;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001887
1888 /*
1889 * If existing entry points to an xattr inode, we need
1890 * to prevent ext4_xattr_set_entry() from decrementing
1891 * ref count on it because the reference belongs to the
1892 * original block. In this case, make the entry look
1893 * like it has an empty value.
1894 */
1895 if (!s->not_found && s->here->e_value_inum) {
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001896 ea_ino = le32_to_cpu(s->here->e_value_inum);
1897 error = ext4_xattr_inode_iget(inode, ea_ino,
1898 le32_to_cpu(s->here->e_hash),
1899 &tmp_inode);
1900 if (error)
1901 goto cleanup;
1902
1903 if (!ext4_test_inode_state(tmp_inode,
1904 EXT4_STATE_LUSTRE_EA_INODE)) {
1905 /*
1906 * Defer quota free call for previous
1907 * inode until success is guaranteed.
1908 */
1909 old_ea_inode_quota = le32_to_cpu(
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001910 s->here->e_value_size);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001911 }
1912 iput(tmp_inode);
1913
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001914 s->here->e_value_inum = 0;
1915 s->here->e_value_size = 0;
1916 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001917 }
1918 } else {
1919 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001920 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001921 /* assert(header == s->base) */
1922 error = -ENOMEM;
1923 if (s->base == NULL)
1924 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001925 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 header(s->base)->h_blocks = cpu_to_le32(1);
1927 header(s->base)->h_refcount = cpu_to_le32(1);
1928 s->first = ENTRY(header(s->base)+1);
1929 s->here = ENTRY(header(s->base)+1);
1930 s->end = s->base + sb->s_blocksize;
1931 }
1932
Tahsin Erdogandaf83282017-06-22 11:52:03 -04001933 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001934 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001935 goto bad_block;
1936 if (error)
1937 goto cleanup;
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001938
1939 if (i->value && s->here->e_value_inum) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001940 /*
1941 * A ref count on ea_inode has been taken as part of the call to
1942 * ext4_xattr_set_entry() above. We would like to drop this
1943 * extra ref but we have to wait until the xattr block is
1944 * initialized and has its own ref count on the ea_inode.
1945 */
1946 ea_ino = le32_to_cpu(s->here->e_value_inum);
Tahsin Erdogana6d05672017-08-24 14:25:02 -04001947 error = ext4_xattr_inode_iget(inode, ea_ino,
1948 le32_to_cpu(s->here->e_hash),
1949 &ea_inode);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001950 if (error) {
1951 ea_inode = NULL;
1952 goto cleanup;
1953 }
1954 }
1955
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956inserted:
1957 if (!IS_LAST_ENTRY(s->first)) {
Tahsin Erdogan47387402017-06-22 11:28:55 -04001958 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1959 &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001960 if (new_bh) {
1961 /* We found an identical block in the cache. */
1962 if (new_bh == bs->bh)
1963 ea_bdebug(new_bh, "keeping");
1964 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001965 u32 ref;
1966
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001967 WARN_ON_ONCE(dquot_initialize_needed(inode));
1968
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 /* The old block is released after updating
1970 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001971 error = dquot_alloc_block(inode,
1972 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001973 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001974 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001975 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001976 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001977 new_bh);
1978 if (error)
1979 goto cleanup_dquot;
1980 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001981 /*
1982 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001983 * freeing, rehashing or adding references to
1984 * xattr block. Once we hold buffer lock xattr
1985 * block's state is stable so we can check
1986 * whether the block got freed / rehashed or
1987 * not. Since we unhash mbcache entry under
1988 * buffer lock when freeing / rehashing xattr
1989 * block, checking whether entry is still
1990 * hashed is reliable. Same rules hold for
1991 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001992 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001993 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1994 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001995 /*
1996 * Undo everything and check mbcache
1997 * again.
1998 */
1999 unlock_buffer(new_bh);
2000 dquot_free_block(inode,
2001 EXT4_C2B(EXT4_SB(sb),
2002 1));
2003 brelse(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002004 mb_cache_entry_put(ea_block_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05002005 ce = NULL;
2006 new_bh = NULL;
2007 goto inserted;
2008 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002009 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2010 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2011 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
2012 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002014 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002015 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002017 error = ext4_handle_dirty_metadata(handle,
2018 inode,
2019 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020 if (error)
2021 goto cleanup_dquot;
2022 }
Tahsin Erdogan47387402017-06-22 11:28:55 -04002023 mb_cache_entry_touch(ea_block_cache, ce);
2024 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 ce = NULL;
2026 } else if (bs->bh && s->base == bs->bh->b_data) {
2027 /* We were modifying this block in-place. */
2028 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganec000222017-08-05 22:41:42 -04002029 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 new_bh = bs->bh;
2031 get_bh(new_bh);
2032 } else {
2033 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002034 ext4_fsblk_t goal, block;
2035
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002036 WARN_ON_ONCE(dquot_initialize_needed(inode));
2037
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002038 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04002039 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002040
2041 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002042 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002043 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
2044
Allison Henderson55f020d2011-05-25 07:41:26 -04002045 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2046 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 if (error)
2048 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002049
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002050 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002051 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
2052
Joe Perchesace36ad2012-03-19 23:11:43 -04002053 ea_idebug(inode, "creating block %llu",
2054 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002055
2056 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05002057 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002058 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05002060 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002061 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062 goto cleanup;
2063 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002064 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2065 ENTRY(header(s->base)+1));
2066 if (error)
2067 goto getblk_failed;
2068 if (ea_inode) {
2069 /* Drop the extra ref on ea_inode. */
2070 error = ext4_xattr_inode_dec_ref(handle,
2071 ea_inode);
2072 if (error)
2073 ext4_warning_inode(ea_inode,
2074 "dec ref error=%d",
2075 error);
2076 iput(ea_inode);
2077 ea_inode = NULL;
2078 }
2079
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002080 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002081 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 if (error) {
2083 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05002084 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085 goto getblk_failed;
2086 }
2087 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002088 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 set_buffer_uptodate(new_bh);
2090 unlock_buffer(new_bh);
Tahsin Erdogan47387402017-06-22 11:28:55 -04002091 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04002092 error = ext4_handle_dirty_metadata(handle, inode,
2093 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002094 if (error)
2095 goto cleanup;
2096 }
2097 }
2098
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002099 if (old_ea_inode_quota)
2100 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002101
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002102 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07002103 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104
2105 /* Drop the previous xattr block. */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002106 if (bs->bh && bs->bh != new_bh) {
2107 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2108
2109 ext4_xattr_release_block(handle, inode, bs->bh,
2110 &ea_inode_array,
2111 0 /* extra_credits */);
2112 ext4_xattr_inode_array_free(ea_inode_array);
2113 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002114 error = 0;
2115
2116cleanup:
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002117 if (ea_inode) {
2118 int error2;
2119
2120 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2121 if (error2)
2122 ext4_warning_inode(ea_inode, "dec ref error=%d",
2123 error2);
2124
2125 /* If there was an error, revert the quota charge. */
2126 if (error)
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002127 ext4_xattr_inode_free_quota(inode, ea_inode,
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002128 i_size_read(ea_inode));
2129 iput(ea_inode);
2130 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131 if (ce)
Tahsin Erdogan47387402017-06-22 11:28:55 -04002132 mb_cache_entry_put(ea_block_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002133 brelse(new_bh);
2134 if (!(bs->bh && s->base == bs->bh->b_data))
2135 kfree(s->base);
2136
2137 return error;
2138
2139cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05002140 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 goto cleanup;
2142
2143bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04002144 EXT4_ERROR_INODE(inode, "bad block %llu",
2145 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146 goto cleanup;
2147
2148#undef header
2149}
2150
Tao Ma879b3822012-12-05 10:28:46 -05002151int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2152 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153{
Mingming Cao617ba132006-10-11 01:20:53 -07002154 struct ext4_xattr_ibody_header *header;
2155 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002156 int error;
2157
Mingming Cao617ba132006-10-11 01:20:53 -07002158 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002160 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161 header = IHDR(inode, raw_inode);
2162 is->s.base = is->s.first = IFIRST(header);
2163 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07002164 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002165 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002166 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002167 if (error)
2168 return error;
2169 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07002170 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04002171 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 if (error && error != -ENODATA)
2173 return error;
2174 is->s.not_found = error;
2175 }
2176 return 0;
2177}
2178
Tao Ma0d812f72012-12-10 14:06:02 -05002179int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2180 struct ext4_xattr_info *i,
2181 struct ext4_xattr_ibody_find *is)
2182{
2183 struct ext4_xattr_ibody_header *header;
2184 struct ext4_xattr_search *s = &is->s;
2185 int error;
2186
2187 if (EXT4_I(inode)->i_extra_isize == 0)
2188 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002189 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002190 if (error) {
2191 if (error == -ENOSPC &&
2192 ext4_has_inline_data(inode)) {
2193 error = ext4_try_to_evict_inline_data(handle, inode,
2194 EXT4_XATTR_LEN(strlen(i->name) +
2195 EXT4_XATTR_SIZE(i->value_len)));
2196 if (error)
2197 return error;
2198 error = ext4_xattr_ibody_find(inode, i, is);
2199 if (error)
2200 return error;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002201 error = ext4_xattr_set_entry(i, s, handle, inode,
2202 false /* is_block */);
Tao Ma0d812f72012-12-10 14:06:02 -05002203 }
2204 if (error)
2205 return error;
2206 }
2207 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2208 if (!IS_LAST_ENTRY(s->first)) {
2209 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2210 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2211 } else {
2212 header->h_magic = cpu_to_le32(0);
2213 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2214 }
2215 return 0;
2216}
2217
Andreas Dilgere50e5122017-06-21 21:10:32 -04002218static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05002219 struct ext4_xattr_info *i,
2220 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221{
Mingming Cao617ba132006-10-11 01:20:53 -07002222 struct ext4_xattr_ibody_header *header;
2223 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 int error;
2225
Mingming Cao617ba132006-10-11 01:20:53 -07002226 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002227 return -ENOSPC;
Tahsin Erdogandaf83282017-06-22 11:52:03 -04002228 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229 if (error)
2230 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07002231 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002233 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002234 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 } else {
2236 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002237 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002238 }
2239 return 0;
2240}
2241
Jan Kara3fd16462016-02-22 22:43:04 -05002242static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2243 struct ext4_xattr_info *i)
2244{
2245 void *value;
2246
Tahsin Erdogan0bd454c2017-06-21 22:02:06 -04002247 /* When e_value_inum is set the value is stored externally. */
2248 if (s->here->e_value_inum)
2249 return 0;
Jan Kara3fd16462016-02-22 22:43:04 -05002250 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2251 return 0;
2252 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2253 return !memcmp(value, i->value, i->value_len);
2254}
2255
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002256static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2257{
2258 struct buffer_head *bh;
2259 int error;
2260
2261 if (!EXT4_I(inode)->i_file_acl)
2262 return NULL;
2263 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2264 if (!bh)
2265 return ERR_PTR(-EIO);
2266 error = ext4_xattr_check_block(inode, bh);
2267 if (error)
2268 return ERR_PTR(error);
2269 return bh;
2270}
2271
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002272/*
Mingming Cao617ba132006-10-11 01:20:53 -07002273 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05002275 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002276 * is NULL to remove an existing extended attribute, and non-NULL to
2277 * either replace an existing extended attribute, or create a new extended
2278 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2279 * specify that an extended attribute must exist and must not exist
2280 * previous to the call, respectively.
2281 *
2282 * Returns 0, or a negative error number on failure.
2283 */
2284int
Mingming Cao617ba132006-10-11 01:20:53 -07002285ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 const char *name, const void *value, size_t value_len,
2287 int flags)
2288{
Mingming Cao617ba132006-10-11 01:20:53 -07002289 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002290 .name_index = name_index,
2291 .name = name,
2292 .value = value,
2293 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04002294 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 };
Mingming Cao617ba132006-10-11 01:20:53 -07002296 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002297 .s = { .not_found = -ENODATA, },
2298 };
Mingming Cao617ba132006-10-11 01:20:53 -07002299 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300 .s = { .not_found = -ENODATA, },
2301 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05002302 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002303 int error;
2304
2305 if (!name)
2306 return -EINVAL;
2307 if (strlen(name) > 255)
2308 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002309
Theodore Ts'oc755e252017-01-11 21:50:46 -05002310 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04002311
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002312 /* Check journal credits under write lock. */
2313 if (ext4_handle_valid(handle)) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002314 struct buffer_head *bh;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002315 int credits;
2316
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002317 bh = ext4_xattr_get_block(inode);
2318 if (IS_ERR(bh)) {
2319 error = PTR_ERR(bh);
2320 goto cleanup;
2321 }
2322
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002323 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2324 value_len,
2325 flags & XATTR_CREATE);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002326 brelse(bh);
2327
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002328 if (!ext4_handle_has_enough_credits(handle, credits)) {
2329 error = -ENOSPC;
2330 goto cleanup;
2331 }
2332 }
2333
Eric Sandeen66543612011-10-26 03:32:07 -04002334 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05002335 if (error)
2336 goto cleanup;
2337
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002338 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002339 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2340 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002341 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002342 }
2343
Mingming Cao617ba132006-10-11 01:20:53 -07002344 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002345 if (error)
2346 goto cleanup;
2347 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002348 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002349 if (error)
2350 goto cleanup;
2351 if (is.s.not_found && bs.s.not_found) {
2352 error = -ENODATA;
2353 if (flags & XATTR_REPLACE)
2354 goto cleanup;
2355 error = 0;
2356 if (!value)
2357 goto cleanup;
2358 } else {
2359 error = -EEXIST;
2360 if (flags & XATTR_CREATE)
2361 goto cleanup;
2362 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002363
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 if (!value) {
2365 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002366 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002367 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07002368 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002369 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05002370 error = 0;
2371 /* Xattr value did not change? Save us some work and bail out */
2372 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2373 goto cleanup;
2374 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2375 goto cleanup;
2376
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002377 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2378 (EXT4_XATTR_SIZE(i.value_len) >
2379 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2380 i.in_inode = 1;
2381retry_inode:
Andreas Dilgere50e5122017-06-21 21:10:32 -04002382 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 if (!error && !bs.s.not_found) {
2384 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002385 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002386 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07002387 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2388 error = ext4_xattr_block_find(inode, &i, &bs);
2389 if (error)
2390 goto cleanup;
2391 }
Mingming Cao617ba132006-10-11 01:20:53 -07002392 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002393 if (!error && !is.s.not_found) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002394 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002395 error = ext4_xattr_ibody_set(handle, inode, &i,
2396 &is);
Tahsin Erdoganb347e2b2017-06-21 22:20:32 -04002397 } else if (error == -ENOSPC) {
2398 /*
2399 * Xattr does not fit in the block, store at
2400 * external inode if possible.
2401 */
2402 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2403 !i.in_inode) {
2404 i.in_inode = 1;
2405 goto retry_inode;
2406 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002407 }
2408 }
2409 }
2410 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07002411 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002412 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002413 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05002414 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002415 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002416 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002417 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418 * error != 0.
2419 */
2420 is.iloc.bh = NULL;
2421 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002422 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002423 }
2424
2425cleanup:
2426 brelse(is.iloc.bh);
2427 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05002428 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429 return error;
2430}
2431
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002432int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2433 bool is_create, int *credits)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002434{
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002435 struct buffer_head *bh;
2436 int err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002437
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002438 *credits = 0;
2439
2440 if (!EXT4_SB(inode->i_sb)->s_journal)
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002441 return 0;
2442
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002443 down_read(&EXT4_I(inode)->xattr_sem);
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002444
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002445 bh = ext4_xattr_get_block(inode);
2446 if (IS_ERR(bh)) {
2447 err = PTR_ERR(bh);
2448 } else {
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002449 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2450 value_len, is_create);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002451 brelse(bh);
2452 err = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002453 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002454
2455 up_read(&EXT4_I(inode)->xattr_sem);
2456 return err;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002457}
2458
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002459/*
Mingming Cao617ba132006-10-11 01:20:53 -07002460 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002461 *
Mingming Cao617ba132006-10-11 01:20:53 -07002462 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002463 * attribute modification is a filesystem transaction by itself.
2464 *
2465 * Returns 0, or a negative error number on failure.
2466 */
2467int
Mingming Cao617ba132006-10-11 01:20:53 -07002468ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469 const void *value, size_t value_len, int flags)
2470{
2471 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002472 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002473 int error, retries = 0;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04002474 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04002476 error = dquot_initialize(inode);
2477 if (error)
2478 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002479
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04002481 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2482 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002483 if (error)
2484 return error;
2485
Theodore Ts'o9924a922013-02-08 21:59:22 -05002486 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002487 if (IS_ERR(handle)) {
2488 error = PTR_ERR(handle);
2489 } else {
2490 int error2;
2491
Mingming Cao617ba132006-10-11 01:20:53 -07002492 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002493 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07002494 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002495 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04002496 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002497 goto retry;
2498 if (error == 0)
2499 error = error2;
2500 }
2501
2502 return error;
2503}
2504
2505/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002506 * Shift the EA entries in the inode to create space for the increased
2507 * i_extra_isize.
2508 */
2509static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2510 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04002511 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002512{
2513 struct ext4_xattr_entry *last = entry;
2514 int new_offs;
2515
Jan Kara94405712016-08-29 15:41:11 -04002516 /* We always shift xattr headers further thus offsets get lower */
2517 BUG_ON(value_offs_shift > 0);
2518
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002519 /* Adjust the value offsets of the entries */
2520 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002521 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002522 new_offs = le16_to_cpu(last->e_value_offs) +
2523 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002524 last->e_value_offs = cpu_to_le16(new_offs);
2525 }
2526 }
2527 /* Shift the entries by n bytes */
2528 memmove(to, from, n);
2529}
2530
2531/*
Jan Kara3f2571c2016-08-29 15:42:11 -04002532 * Move xattr pointed to by 'entry' from inode into external xattr block
2533 */
2534static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2535 struct ext4_inode *raw_inode,
2536 struct ext4_xattr_entry *entry)
2537{
2538 struct ext4_xattr_ibody_find *is = NULL;
2539 struct ext4_xattr_block_find *bs = NULL;
2540 char *buffer = NULL, *b_entry_name = NULL;
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002541 size_t value_size = le32_to_cpu(entry->e_value_size);
Jan Kara3f2571c2016-08-29 15:42:11 -04002542 struct ext4_xattr_info i = {
2543 .value = NULL,
2544 .value_len = 0,
2545 .name_index = entry->e_name_index,
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002546 .in_inode = !!entry->e_value_inum,
Jan Kara3f2571c2016-08-29 15:42:11 -04002547 };
2548 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2549 int error;
2550
Jan Kara3f2571c2016-08-29 15:42:11 -04002551 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2552 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2553 buffer = kmalloc(value_size, GFP_NOFS);
2554 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2555 if (!is || !bs || !buffer || !b_entry_name) {
2556 error = -ENOMEM;
2557 goto out;
2558 }
2559
2560 is->s.not_found = -ENODATA;
2561 bs->s.not_found = -ENODATA;
2562 is->iloc.bh = NULL;
2563 bs->bh = NULL;
2564
2565 /* Save the entry name and the entry value */
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002566 if (entry->e_value_inum) {
Tahsin Erdoganb9fc7612017-06-22 11:53:15 -04002567 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
Tahsin Erdoganf6109102017-06-21 22:11:54 -04002568 if (error)
2569 goto out;
2570 } else {
2571 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2572 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2573 }
2574
Jan Kara3f2571c2016-08-29 15:42:11 -04002575 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2576 b_entry_name[entry->e_name_len] = '\0';
2577 i.name = b_entry_name;
2578
2579 error = ext4_get_inode_loc(inode, &is->iloc);
2580 if (error)
2581 goto out;
2582
2583 error = ext4_xattr_ibody_find(inode, &i, is);
2584 if (error)
2585 goto out;
2586
2587 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04002588 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04002589 if (error)
2590 goto out;
2591
Jan Kara3f2571c2016-08-29 15:42:11 -04002592 i.value = buffer;
2593 i.value_len = value_size;
2594 error = ext4_xattr_block_find(inode, &i, bs);
2595 if (error)
2596 goto out;
2597
2598 /* Add entry which was removed from the inode into the block */
2599 error = ext4_xattr_block_set(handle, inode, &i, bs);
2600 if (error)
2601 goto out;
2602 error = 0;
2603out:
2604 kfree(b_entry_name);
2605 kfree(buffer);
2606 if (is)
2607 brelse(is->iloc.bh);
2608 kfree(is);
2609 kfree(bs);
2610
2611 return error;
2612}
2613
Jan Karadfa20642016-08-29 15:44:11 -04002614static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2615 struct ext4_inode *raw_inode,
2616 int isize_diff, size_t ifree,
2617 size_t bfree, int *total_ino)
2618{
2619 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2620 struct ext4_xattr_entry *small_entry;
2621 struct ext4_xattr_entry *entry;
2622 struct ext4_xattr_entry *last;
2623 unsigned int entry_size; /* EA entry size */
2624 unsigned int total_size; /* EA entry size + value size */
2625 unsigned int min_total_size;
2626 int error;
2627
2628 while (isize_diff > ifree) {
2629 entry = NULL;
2630 small_entry = NULL;
2631 min_total_size = ~0U;
2632 last = IFIRST(header);
2633 /* Find the entry best suited to be pushed into EA block */
2634 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002635 total_size = EXT4_XATTR_LEN(last->e_name_len);
2636 if (!last->e_value_inum)
2637 total_size += EXT4_XATTR_SIZE(
2638 le32_to_cpu(last->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002639 if (total_size <= bfree &&
2640 total_size < min_total_size) {
2641 if (total_size + ifree < isize_diff) {
2642 small_entry = last;
2643 } else {
2644 entry = last;
2645 min_total_size = total_size;
2646 }
2647 }
2648 }
2649
2650 if (entry == NULL) {
2651 if (small_entry == NULL)
2652 return -ENOSPC;
2653 entry = small_entry;
2654 }
2655
2656 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Tahsin Erdogan9bb21ce2017-06-21 22:05:44 -04002657 total_size = entry_size;
2658 if (!entry->e_value_inum)
2659 total_size += EXT4_XATTR_SIZE(
2660 le32_to_cpu(entry->e_value_size));
Jan Karadfa20642016-08-29 15:44:11 -04002661 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2662 entry);
2663 if (error)
2664 return error;
2665
2666 *total_ino -= entry_size;
2667 ifree += total_size;
2668 bfree -= total_size;
2669 }
2670
2671 return 0;
2672}
2673
Jan Kara3f2571c2016-08-29 15:42:11 -04002674/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002675 * Expand an inode by new_extra_isize bytes when EAs are present.
2676 * Returns 0 on success or negative error number on failure.
2677 */
2678int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2679 struct ext4_inode *raw_inode, handle_t *handle)
2680{
2681 struct ext4_xattr_ibody_header *header;
Miao Xieb640b2c2017-08-06 00:55:48 -04002682 struct buffer_head *bh;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002683 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2684 static unsigned int mnt_count;
Jan Karae3014d12016-08-29 15:38:11 -04002685 size_t min_offs;
2686 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05002687 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04002688 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04002689 int error = 0, tried_min_extra_isize = 0;
Miao Xiecf0a5e82017-08-06 00:40:01 -04002690 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04002691 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05002692
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002693retry:
Jan Karad0141192016-08-11 11:50:30 -04002694 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04002695 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
Miao Xieb640b2c2017-08-06 00:55:48 -04002696 return 0;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002697
2698 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002699
2700 /*
2701 * Check if enough free space is available in the inode to shift the
2702 * entries ahead by new_extra_isize.
2703 */
2704
Jan Kara6e0cd082016-08-29 15:43:11 -04002705 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002706 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2707 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002708 total_ino = sizeof(struct ext4_xattr_ibody_header);
2709
Theodore Ts'o9e92f482016-03-22 16:13:15 -04002710 error = xattr_check_inode(inode, header, end);
2711 if (error)
2712 goto cleanup;
2713
Jan Kara6e0cd082016-08-29 15:43:11 -04002714 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002715 if (ifree >= isize_diff)
2716 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002717
2718 /*
2719 * Enough free space isn't available in the inode, check if
2720 * EA block can hold new_extra_isize bytes.
2721 */
2722 if (EXT4_I(inode)->i_file_acl) {
2723 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2724 error = -EIO;
2725 if (!bh)
2726 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04002727 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002728 EXT4_ERROR_INODE(inode, "bad block %llu",
2729 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002730 error = -EFSCORRUPTED;
Miao Xieb640b2c2017-08-06 00:55:48 -04002731 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002732 goto cleanup;
2733 }
2734 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002735 end = bh->b_data + bh->b_size;
2736 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04002737 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2738 NULL);
Miao Xieb640b2c2017-08-06 00:55:48 -04002739 brelse(bh);
Jan Karae3014d12016-08-29 15:38:11 -04002740 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002741 if (!tried_min_extra_isize && s_min_extra_isize) {
2742 tried_min_extra_isize++;
2743 new_extra_isize = s_min_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002744 goto retry;
2745 }
Jan Karadfa20642016-08-29 15:44:11 -04002746 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002747 goto cleanup;
2748 }
2749 } else {
Jan Karae3014d12016-08-29 15:38:11 -04002750 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002751 }
2752
Jan Karadfa20642016-08-29 15:44:11 -04002753 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2754 isize_diff, ifree, bfree,
2755 &total_ino);
2756 if (error) {
2757 if (error == -ENOSPC && !tried_min_extra_isize &&
2758 s_min_extra_isize) {
2759 tried_min_extra_isize++;
2760 new_extra_isize = s_min_extra_isize;
Jan Karadfa20642016-08-29 15:44:11 -04002761 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002762 }
Jan Karadfa20642016-08-29 15:44:11 -04002763 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002764 }
Jan Karae3014d12016-08-29 15:38:11 -04002765shift:
2766 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04002767 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04002768 - new_extra_isize, (void *)raw_inode +
2769 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04002770 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04002771 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002772
2773cleanup:
Miao Xieb640b2c2017-08-06 00:55:48 -04002774 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
Miao Xiecf0a5e82017-08-06 00:40:01 -04002775 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2776 inode->i_ino);
2777 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2778 }
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002779 return error;
2780}
2781
Andreas Dilgere50e5122017-06-21 21:10:32 -04002782#define EIA_INCR 16 /* must be 2^n */
2783#define EIA_MASK (EIA_INCR - 1)
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002784
2785/* Add the large xattr @inode into @ea_inode_array for deferred iput().
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002786 * If @ea_inode_array is new or full it will be grown and the old
Andreas Dilgere50e5122017-06-21 21:10:32 -04002787 * contents copied over.
2788 */
2789static int
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002790ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2791 struct inode *inode)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002792{
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002793 if (*ea_inode_array == NULL) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002794 /*
2795 * Start with 15 inodes, so it fits into a power-of-two size.
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002796 * If *ea_inode_array is NULL, this is essentially offsetof()
Andreas Dilgere50e5122017-06-21 21:10:32 -04002797 */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002798 (*ea_inode_array) =
2799 kmalloc(offsetof(struct ext4_xattr_inode_array,
2800 inodes[EIA_MASK]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002801 GFP_NOFS);
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002802 if (*ea_inode_array == NULL)
Andreas Dilgere50e5122017-06-21 21:10:32 -04002803 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002804 (*ea_inode_array)->count = 0;
2805 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002806 /* expand the array once all 15 + n * 16 slots are full */
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002807 struct ext4_xattr_inode_array *new_array = NULL;
2808 int count = (*ea_inode_array)->count;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002809
2810 /* if new_array is NULL, this is essentially offsetof() */
2811 new_array = kmalloc(
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002812 offsetof(struct ext4_xattr_inode_array,
2813 inodes[count + EIA_INCR]),
Andreas Dilgere50e5122017-06-21 21:10:32 -04002814 GFP_NOFS);
2815 if (new_array == NULL)
2816 return -ENOMEM;
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002817 memcpy(new_array, *ea_inode_array,
2818 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2819 kfree(*ea_inode_array);
2820 *ea_inode_array = new_array;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002821 }
Tahsin Erdogan0421a182017-06-22 10:26:31 -04002822 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002823 return 0;
2824}
2825
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04002826/*
Mingming Cao617ba132006-10-11 01:20:53 -07002827 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002828 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04002829 * Free extended attribute resources associated with this inode. Traverse
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002830 * all entries and decrement reference on any xattr inodes associated with this
2831 * inode. This is called immediately before an inode is freed. We have exclusive
2832 * access to the inode. If an orphan inode is deleted it will also release its
2833 * references on xattr block and xattr inodes.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002834 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002835int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2836 struct ext4_xattr_inode_array **ea_inode_array,
2837 int extra_credits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002838{
2839 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04002840 struct ext4_xattr_ibody_header *header;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002841 struct ext4_iloc iloc = { .bh = NULL };
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002842 struct ext4_xattr_entry *entry;
Tahsin Erdogana6d05672017-08-24 14:25:02 -04002843 struct inode *ea_inode;
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -04002844 int error;
2845
2846 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2847 NULL /* bh */,
2848 false /* dirty */,
2849 false /* block_csum */);
2850 if (error) {
2851 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2852 goto cleanup;
2853 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002855 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2856 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04002857
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002858 error = ext4_get_inode_loc(inode, &iloc);
Tahsin Erdogan65d30002017-06-21 22:24:38 -04002859 if (error) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002860 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002861 goto cleanup;
2862 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002863
2864 error = ext4_journal_get_write_access(handle, iloc.bh);
2865 if (error) {
2866 EXT4_ERROR_INODE(inode, "write access (error %d)",
2867 error);
2868 goto cleanup;
2869 }
2870
2871 header = IHDR(inode, ext4_raw_inode(&iloc));
2872 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2873 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2874 IFIRST(header),
2875 false /* block_csum */,
2876 ea_inode_array,
2877 extra_credits,
2878 false /* skip_quota */);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002879 }
2880
Tahsin Erdogandec214d2017-06-22 11:44:55 -04002881 if (EXT4_I(inode)->i_file_acl) {
2882 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2883 if (!bh) {
2884 EXT4_ERROR_INODE(inode, "block %llu read error",
2885 EXT4_I(inode)->i_file_acl);
2886 error = -EIO;
2887 goto cleanup;
2888 }
2889 error = ext4_xattr_check_block(inode, bh);
2890 if (error) {
2891 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2892 EXT4_I(inode)->i_file_acl, error);
2893 goto cleanup;
2894 }
2895
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