blob: 7dd80d16f98ed691bcface4f2cff432ed4c19d1f [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/xattr.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
Mingming Cao617ba132006-10-11 01:20:53 -07007 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07008 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11 * Red Hat Inc.
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
14 */
15
16/*
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
24 *
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
27 *
28 * +------------------+
29 * | header |
30 * | entry 1 | |
31 * | entry 2 | | growing downwards
32 * | entry 3 | v
33 * | four null bytes |
34 * | . . . |
35 * | value 1 | ^
36 * | value 3 | | growing upwards
37 * | value 2 | |
38 * +------------------+
39 *
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
43 *
44 * Locking strategy
45 * ----------------
Mingming Cao617ba132006-10-11 01:20:53 -070046 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
50 * by the buffer lock.
51 */
52
53#include <linux/init.h>
54#include <linux/fs.h>
55#include <linux/slab.h>
Jan Kara7a2508e2016-02-22 22:35:22 -050056#include <linux/mbcache.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057#include <linux/quotaops.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040058#include "ext4_jbd2.h"
59#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060#include "xattr.h"
61#include "acl.h"
62
Mingming Cao617ba132006-10-11 01:20:53 -070063#ifdef EXT4_XATTR_DEBUG
Joe Perchesd74f3d22016-10-15 09:57:31 -040064# define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67# define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070#else
Joe Perchesace36ad2012-03-19 23:11:43 -040071# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073#endif
74
Jan Kara7a2508e2016-02-22 22:35:22 -050075static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070076static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
Jan Kara7a2508e2016-02-22 22:35:22 -050078 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070079static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081
Eric Biggersd6006182017-04-29 23:47:50 -040082static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070083 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040084#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080085 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
86 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087#endif
Mingming Cao617ba132006-10-11 01:20:53 -070088 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040089#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070090 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091#endif
92};
93
Stephen Hemminger11e27522010-05-13 17:53:18 -070094const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070095 &ext4_xattr_user_handler,
96 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040097#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080098 &posix_acl_access_xattr_handler,
99 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400101#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700102 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103#endif
104 NULL
105};
106
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400107#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
108 inode->i_sb->s_fs_info)->s_mb_cache)
109
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400110static __le32 ext4_xattr_block_csum(struct inode *inode,
111 sector_t block_nr,
112 struct ext4_xattr_header *hdr)
113{
114 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400115 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400116 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400117 __u32 dummy_csum = 0;
118 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400119
Theodore Ts'od6a77102013-04-09 23:59:55 -0400120 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
121 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400122 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
123 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
124 offset += sizeof(dummy_csum);
125 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
126 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400127
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400128 return cpu_to_le32(csum);
129}
130
131static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400132 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400133{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400134 struct ext4_xattr_header *hdr = BHDR(bh);
135 int ret = 1;
136
137 if (ext4_has_metadata_csum(inode->i_sb)) {
138 lock_buffer(bh);
139 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
140 bh->b_blocknr, hdr));
141 unlock_buffer(bh);
142 }
143 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400144}
145
146static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400147 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400148{
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400149 if (ext4_has_metadata_csum(inode->i_sb))
150 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
151 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400152}
153
Stephen Hemminger11e27522010-05-13 17:53:18 -0700154static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700155ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700156{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700157 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158
Mingming Cao617ba132006-10-11 01:20:53 -0700159 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
160 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 return handler;
162}
163
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164static int
Eric Biggers2c4f9922017-04-29 23:56:52 -0400165ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
166 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400168 struct ext4_xattr_entry *e = entry;
169
Eric Biggersd7614cc2016-12-01 14:57:29 -0500170 /* Find the end of the names list */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400171 while (!IS_LAST_ENTRY(e)) {
172 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400174 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400175 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400177
Eric Biggersd7614cc2016-12-01 14:57:29 -0500178 /* Check the values */
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400179 while (!IS_LAST_ENTRY(entry)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400180 if (entry->e_value_size != 0 &&
181 entry->e_value_inum == 0) {
Eric Biggersd7614cc2016-12-01 14:57:29 -0500182 u16 offs = le16_to_cpu(entry->e_value_offs);
183 u32 size = le32_to_cpu(entry->e_value_size);
184 void *value;
185
186 /*
187 * The value cannot overlap the names, and the value
188 * with padding cannot extend beyond 'end'. Check both
189 * the padded and unpadded sizes, since the size may
190 * overflow to 0 when adding padding.
191 */
192 if (offs > end - value_start)
193 return -EFSCORRUPTED;
194 value = value_start + offs;
195 if (value < (void *)e + sizeof(u32) ||
196 size > end - value ||
197 EXT4_XATTR_SIZE(size) > end - value)
198 return -EFSCORRUPTED;
199 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400200 entry = EXT4_XATTR_NEXT(entry);
201 }
202
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 return 0;
204}
205
206static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400207ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400209 int error;
210
211 if (buffer_verified(bh))
212 return 0;
213
Mingming Cao617ba132006-10-11 01:20:53 -0700214 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400216 return -EFSCORRUPTED;
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400217 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400218 return -EFSBADCRC;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400219 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
220 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400221 if (!error)
222 set_buffer_verified(bh);
223 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224}
225
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400226static int
227__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
228 void *end, const char *function, unsigned int line)
229{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400230 int error = -EFSCORRUPTED;
231
Eric Biggers290ab232016-12-01 14:51:58 -0500232 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400233 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400234 goto errout;
Eric Biggers2c4f9922017-04-29 23:56:52 -0400235 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400236errout:
237 if (error)
238 __ext4_error_inode(inode, function, line, 0,
239 "corrupted in-inode xattr");
240 return error;
241}
242
243#define xattr_check_inode(inode, header, end) \
244 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
245
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246static int
Mingming Cao617ba132006-10-11 01:20:53 -0700247ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -0400248 const char *name, int sorted)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249{
Mingming Cao617ba132006-10-11 01:20:53 -0700250 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 size_t name_len;
252 int cmp = 1;
253
254 if (name == NULL)
255 return -EINVAL;
256 name_len = strlen(name);
257 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700258 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 cmp = name_index - entry->e_name_index;
260 if (!cmp)
261 cmp = name_len - entry->e_name_len;
262 if (!cmp)
263 cmp = memcmp(name, entry->e_name, name_len);
264 if (cmp <= 0 && (sorted || cmp == 0))
265 break;
266 }
267 *pentry = entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 return cmp ? -ENODATA : 0;
269}
270
Andreas Dilgere50e5122017-06-21 21:10:32 -0400271/*
272 * Read the EA value from an inode.
273 */
274static int
275ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
276{
277 unsigned long block = 0;
278 struct buffer_head *bh = NULL;
279 int blocksize;
280 size_t csize, ret_size = 0;
281
282 if (*size == 0)
283 return 0;
284
285 blocksize = ea_inode->i_sb->s_blocksize;
286
287 while (ret_size < *size) {
288 csize = (*size - ret_size) > blocksize ? blocksize :
289 *size - ret_size;
290 bh = ext4_bread(NULL, ea_inode, block, 0);
291 if (IS_ERR(bh)) {
292 *size = ret_size;
293 return PTR_ERR(bh);
294 }
295 memcpy(buf, bh->b_data, csize);
296 brelse(bh);
297
298 buf += csize;
299 block += 1;
300 ret_size += csize;
301 }
302
303 *size = ret_size;
304
305 return 0;
306}
307
308struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
309{
310 struct inode *ea_inode = NULL;
311
312 ea_inode = ext4_iget(parent->i_sb, ea_ino);
313 if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
314 int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
315 ext4_error(parent->i_sb, "error while reading EA inode %lu "
316 "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
317 *err = rc != 0 ? rc : -EIO;
318 return NULL;
319 }
320
321 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
322 ea_inode->i_generation != parent->i_generation) {
323 ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
324 "to parent invalid.", ea_ino);
325 *err = -EINVAL;
326 goto error;
327 }
328
329 if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
330 ext4_error(parent->i_sb, "EA inode %lu does not have "
331 "EXT4_EA_INODE_FL flag set.\n", ea_ino);
332 *err = -EINVAL;
333 goto error;
334 }
335
336 *err = 0;
337 return ea_inode;
338
339error:
340 iput(ea_inode);
341 return NULL;
342}
343
344/*
345 * Read the value from the EA inode.
346 */
347static int
348ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
349 size_t *size)
350{
351 struct inode *ea_inode = NULL;
352 int err;
353
354 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
355 if (err)
356 return err;
357
358 err = ext4_xattr_inode_read(ea_inode, buffer, size);
359 iput(ea_inode);
360
361 return err;
362}
363
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700364static int
Mingming Cao617ba132006-10-11 01:20:53 -0700365ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 void *buffer, size_t buffer_size)
367{
368 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700369 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 size_t size;
371 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500372 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700373
374 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
375 name_index, name, buffer, (long)buffer_size);
376
377 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700378 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700379 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400380 ea_idebug(inode, "reading block %llu",
381 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700382 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700383 if (!bh)
384 goto cleanup;
385 ea_bdebug(bh, "b_count=%d, refcount=%d",
386 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400387 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400388 EXT4_ERROR_INODE(inode, "bad block %llu",
389 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400390 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700391 goto cleanup;
392 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400393 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700394 entry = BFIRST(bh);
Eric Biggers6ba644b2017-04-30 00:01:02 -0400395 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 if (error)
397 goto cleanup;
398 size = le32_to_cpu(entry->e_value_size);
399 if (buffer) {
400 error = -ERANGE;
401 if (size > buffer_size)
402 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400403 if (entry->e_value_inum) {
404 error = ext4_xattr_inode_get(inode,
405 le32_to_cpu(entry->e_value_inum),
406 buffer, &size);
407 if (error)
408 goto cleanup;
409 } else {
410 memcpy(buffer, bh->b_data +
411 le16_to_cpu(entry->e_value_offs), size);
412 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413 }
414 error = size;
415
416cleanup:
417 brelse(bh);
418 return error;
419}
420
Tao Ma879b3822012-12-05 10:28:46 -0500421int
Mingming Cao617ba132006-10-11 01:20:53 -0700422ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423 void *buffer, size_t buffer_size)
424{
Mingming Cao617ba132006-10-11 01:20:53 -0700425 struct ext4_xattr_ibody_header *header;
426 struct ext4_xattr_entry *entry;
427 struct ext4_inode *raw_inode;
428 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429 size_t size;
430 void *end;
431 int error;
432
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500433 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700435 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 if (error)
437 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700438 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700440 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400441 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442 if (error)
443 goto cleanup;
Eric Biggers6ba644b2017-04-30 00:01:02 -0400444 entry = IFIRST(header);
445 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 if (error)
447 goto cleanup;
448 size = le32_to_cpu(entry->e_value_size);
449 if (buffer) {
450 error = -ERANGE;
451 if (size > buffer_size)
452 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -0400453 if (entry->e_value_inum) {
454 error = ext4_xattr_inode_get(inode,
455 le32_to_cpu(entry->e_value_inum),
456 buffer, &size);
457 if (error)
458 goto cleanup;
459 } else {
460 memcpy(buffer, (void *)IFIRST(header) +
461 le16_to_cpu(entry->e_value_offs), size);
462 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463 }
464 error = size;
465
466cleanup:
467 brelse(iloc.bh);
468 return error;
469}
470
471/*
Mingming Cao617ba132006-10-11 01:20:53 -0700472 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 *
474 * Copy an extended attribute into the buffer
475 * provided, or compute the buffer size required.
476 * Buffer is NULL to compute the size of the buffer required.
477 *
478 * Returns a negative error number on failure, or the number of bytes
479 * used / required on success.
480 */
481int
Mingming Cao617ba132006-10-11 01:20:53 -0700482ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 void *buffer, size_t buffer_size)
484{
485 int error;
486
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500487 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
488 return -EIO;
489
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400490 if (strlen(name) > 255)
491 return -ERANGE;
492
Mingming Cao617ba132006-10-11 01:20:53 -0700493 down_read(&EXT4_I(inode)->xattr_sem);
494 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 buffer_size);
496 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700497 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700499 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 return error;
501}
502
503static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000504ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 char *buffer, size_t buffer_size)
506{
507 size_t rest = buffer_size;
508
Mingming Cao617ba132006-10-11 01:20:53 -0700509 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700510 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700511 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100513 if (handler && (!handler->list || handler->list(dentry))) {
514 const char *prefix = handler->prefix ?: handler->name;
515 size_t prefix_len = strlen(prefix);
516 size_t size = prefix_len + entry->e_name_len + 1;
517
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 if (buffer) {
519 if (size > rest)
520 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100521 memcpy(buffer, prefix, prefix_len);
522 buffer += prefix_len;
523 memcpy(buffer, entry->e_name, entry->e_name_len);
524 buffer += entry->e_name_len;
525 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526 }
527 rest -= size;
528 }
529 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100530 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531}
532
533static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000534ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535{
David Howells2b0143b2015-03-17 22:25:59 +0000536 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 struct buffer_head *bh = NULL;
538 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500539 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540
541 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
542 buffer, (long)buffer_size);
543
544 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700545 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400547 ea_idebug(inode, "reading block %llu",
548 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700549 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 error = -EIO;
551 if (!bh)
552 goto cleanup;
553 ea_bdebug(bh, "b_count=%d, refcount=%d",
554 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400555 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400556 EXT4_ERROR_INODE(inode, "bad block %llu",
557 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400558 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 goto cleanup;
560 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400561 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000562 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563
564cleanup:
565 brelse(bh);
566
567 return error;
568}
569
570static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000571ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572{
David Howells2b0143b2015-03-17 22:25:59 +0000573 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700574 struct ext4_xattr_ibody_header *header;
575 struct ext4_inode *raw_inode;
576 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577 void *end;
578 int error;
579
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500580 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700582 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 if (error)
584 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700585 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700587 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400588 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 if (error)
590 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000591 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592 buffer, buffer_size);
593
594cleanup:
595 brelse(iloc.bh);
596 return error;
597}
598
599/*
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400600 * Inode operation listxattr()
601 *
602 * d_inode(dentry)->i_rwsem: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 *
604 * Copy a list of attribute names into the buffer
605 * provided, or compute the buffer size required.
606 * Buffer is NULL to compute the size of the buffer required.
607 *
608 * Returns a negative error number on failure, or the number of bytes
609 * used / required on success.
610 */
Eric Biggersba7ea1d2017-04-29 23:53:17 -0400611ssize_t
612ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500614 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615
David Howells2b0143b2015-03-17 22:25:59 +0000616 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500617 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
618 if (ret < 0)
619 goto errout;
620 if (buffer) {
621 buffer += ret;
622 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500624 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
625 if (ret < 0)
626 goto errout;
627 ret += ret2;
628errout:
David Howells2b0143b2015-03-17 22:25:59 +0000629 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500630 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700631}
632
633/*
Mingming Cao617ba132006-10-11 01:20:53 -0700634 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635 * not set, set it.
636 */
Mingming Cao617ba132006-10-11 01:20:53 -0700637static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 struct super_block *sb)
639{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400640 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641 return;
642
liang xie5d601252014-05-12 22:06:43 -0400643 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700644 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400645 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400646 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700648}
649
650/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400651 * Release the xattr block BH: If the reference count is > 1, decrement it;
652 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 */
654static void
Mingming Cao617ba132006-10-11 01:20:53 -0700655ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700656 struct buffer_head *bh)
657{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500658 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
659 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800660 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661
liang xie5d601252014-05-12 22:06:43 -0400662 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800663 error = ext4_journal_get_write_access(handle, bh);
664 if (error)
665 goto out;
666
667 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500668 hash = le32_to_cpu(BHDR(bh)->h_hash);
669 ref = le32_to_cpu(BHDR(bh)->h_refcount);
670 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500672 /*
673 * This must happen under buffer lock for
674 * ext4_xattr_block_set() to reliably detect freed block
675 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500676 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400678 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500679 ext4_free_blocks(handle, inode, bh, 0, 1,
680 EXT4_FREE_BLOCKS_METADATA |
681 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500683 ref--;
684 BHDR(bh)->h_refcount = cpu_to_le32(ref);
685 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
686 struct mb_cache_entry *ce;
687
688 ce = mb_cache_entry_get(ext4_mb_cache, hash,
689 bh->b_blocknr);
690 if (ce) {
691 ce->e_reusable = 1;
692 mb_cache_entry_put(ext4_mb_cache, ce);
693 }
694 }
695
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400696 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400697 /*
698 * Beware of this ugliness: Releasing of xattr block references
699 * from different inodes can race and so we have to protect
700 * from a race where someone else frees the block (and releases
701 * its journal_head) before we are done dirtying the buffer. In
702 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400703 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400704 * that function can call sync_dirty_buffer() so for that case
705 * we handle the dirtying after unlocking the buffer.
706 */
707 if (ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400708 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500709 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400710 if (!ext4_handle_valid(handle))
Theodore Ts'odac7a4b2017-03-25 17:22:47 -0400711 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800712 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500713 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500714 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800715 ea_bdebug(bh, "refcount now=%d; releasing",
716 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800718out:
719 ext4_std_error(inode->i_sb, error);
720 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721}
722
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400723/*
724 * Find the available free space for EAs. This also returns the total number of
725 * bytes used by EA entries.
726 */
727static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
728 size_t *min_offs, void *base, int *total)
729{
730 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400731 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400732 size_t offs = le16_to_cpu(last->e_value_offs);
733 if (offs < *min_offs)
734 *min_offs = offs;
735 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500736 if (total)
737 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400738 }
739 return (*min_offs - ((void *)last - base) - sizeof(__u32));
740}
741
Andreas Dilgere50e5122017-06-21 21:10:32 -0400742/*
743 * Write the value of the EA in an inode.
744 */
745static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
746 const void *buf, int bufsize)
747{
748 struct buffer_head *bh = NULL;
749 unsigned long block = 0;
750 unsigned blocksize = ea_inode->i_sb->s_blocksize;
751 unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
752 int csize, wsize = 0;
753 int ret = 0;
754 int retries = 0;
755
756retry:
757 while (ret >= 0 && ret < max_blocks) {
758 struct ext4_map_blocks map;
759 map.m_lblk = block += ret;
760 map.m_len = max_blocks -= ret;
761
762 ret = ext4_map_blocks(handle, ea_inode, &map,
763 EXT4_GET_BLOCKS_CREATE);
764 if (ret <= 0) {
765 ext4_mark_inode_dirty(handle, ea_inode);
766 if (ret == -ENOSPC &&
767 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
768 ret = 0;
769 goto retry;
770 }
771 break;
772 }
773 }
774
775 if (ret < 0)
776 return ret;
777
778 block = 0;
779 while (wsize < bufsize) {
780 if (bh != NULL)
781 brelse(bh);
782 csize = (bufsize - wsize) > blocksize ? blocksize :
783 bufsize - wsize;
784 bh = ext4_getblk(handle, ea_inode, block, 0);
785 if (IS_ERR(bh))
786 return PTR_ERR(bh);
787 ret = ext4_journal_get_write_access(handle, bh);
788 if (ret)
789 goto out;
790
791 memcpy(bh->b_data, buf, csize);
792 set_buffer_uptodate(bh);
793 ext4_handle_dirty_metadata(handle, ea_inode, bh);
794
795 buf += csize;
796 wsize += csize;
797 block += 1;
798 }
799
800 inode_lock(ea_inode);
801 i_size_write(ea_inode, wsize);
802 ext4_update_i_disksize(ea_inode, wsize);
803 inode_unlock(ea_inode);
804
805 ext4_mark_inode_dirty(handle, ea_inode);
806
807out:
808 brelse(bh);
809
810 return ret;
811}
812
813/*
814 * Create an inode to store the value of a large EA.
815 */
816static struct inode *ext4_xattr_inode_create(handle_t *handle,
817 struct inode *inode)
818{
819 struct inode *ea_inode = NULL;
820
821 /*
822 * Let the next inode be the goal, so we try and allocate the EA inode
823 * in the same group, or nearby one.
824 */
825 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
826 S_IFREG | 0600, NULL, inode->i_ino + 1, NULL);
827 if (!IS_ERR(ea_inode)) {
828 ea_inode->i_op = &ext4_file_inode_operations;
829 ea_inode->i_fop = &ext4_file_operations;
830 ext4_set_aops(ea_inode);
831 ea_inode->i_generation = inode->i_generation;
832 EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
833
834 /*
835 * A back-pointer from EA inode to parent inode will be useful
836 * for e2fsck.
837 */
838 EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
839 unlock_new_inode(ea_inode);
840 }
841
842 return ea_inode;
843}
844
845/*
846 * Unlink the inode storing the value of the EA.
847 */
848int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
849{
850 struct inode *ea_inode = NULL;
851 int err;
852
853 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
854 if (err)
855 return err;
856
857 clear_nlink(ea_inode);
858 iput(ea_inode);
859
860 return 0;
861}
862
863/*
864 * Add value of the EA in an inode.
865 */
866static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
867 unsigned long *ea_ino, const void *value,
868 size_t value_len)
869{
870 struct inode *ea_inode;
871 int err;
872
873 /* Create an inode for the EA value */
874 ea_inode = ext4_xattr_inode_create(handle, inode);
875 if (IS_ERR(ea_inode))
876 return PTR_ERR(ea_inode);
877
878 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
879 if (err)
880 clear_nlink(ea_inode);
881 else
882 *ea_ino = ea_inode->i_ino;
883
884 iput(ea_inode);
885
886 return err;
887}
888
889static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
890 struct ext4_xattr_search *s,
891 handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892{
Mingming Cao617ba132006-10-11 01:20:53 -0700893 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700894 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400895 int in_inode = i->in_inode;
896 int rc;
897
898 if (ext4_has_feature_ea_inode(inode->i_sb) &&
899 (EXT4_XATTR_SIZE(i->value_len) >
900 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
901 in_inode = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902
903 /* Compute min_offs and last. */
904 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700905 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400906 if (!last->e_value_inum && last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 size_t offs = le16_to_cpu(last->e_value_offs);
908 if (offs < min_offs)
909 min_offs = offs;
910 }
911 }
912 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
913 if (!s->not_found) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400914 if (!in_inode &&
915 !s->here->e_value_inum && s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700917 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918 }
Mingming Cao617ba132006-10-11 01:20:53 -0700919 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920 }
921 if (i->value) {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400922 size_t value_len = EXT4_XATTR_SIZE(i->value_len);
923
924 if (in_inode)
925 value_len = 0;
926
927 if (free < EXT4_XATTR_LEN(name_len) + value_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928 return -ENOSPC;
929 }
930
931 if (i->value && s->not_found) {
932 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700933 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
935 memmove((void *)s->here + size, s->here, rest);
936 memset(s->here, 0, size);
937 s->here->e_name_index = i->name_index;
938 s->here->e_name_len = name_len;
939 memcpy(s->here->e_name, i->name, name_len);
940 } else {
Andreas Dilgere50e5122017-06-21 21:10:32 -0400941 if (!s->here->e_value_inum && s->here->e_value_size &&
942 s->here->e_value_offs > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 void *first_val = s->base + min_offs;
944 size_t offs = le16_to_cpu(s->here->e_value_offs);
945 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700946 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947 le32_to_cpu(s->here->e_value_size));
948
Mingming Cao617ba132006-10-11 01:20:53 -0700949 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 /* The old and the new value have the same
951 size. Just replace. */
952 s->here->e_value_size =
953 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500954 if (i->value == EXT4_ZERO_XATTR_VALUE) {
955 memset(val, 0, size);
956 } else {
957 /* Clear pad bytes first. */
958 memset(val + size - EXT4_XATTR_PAD, 0,
959 EXT4_XATTR_PAD);
960 memcpy(val, i->value, i->value_len);
961 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 return 0;
963 }
964
965 /* Remove the old value. */
966 memmove(first_val + size, first_val, val - first_val);
967 memset(first_val, 0, size);
968 s->here->e_value_size = 0;
969 s->here->e_value_offs = 0;
970 min_offs += size;
971
972 /* Adjust all value offsets. */
973 last = s->first;
974 while (!IS_LAST_ENTRY(last)) {
975 size_t o = le16_to_cpu(last->e_value_offs);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400976 if (!last->e_value_inum &&
977 last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 last->e_value_offs =
979 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700980 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 }
982 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400983 if (s->here->e_value_inum) {
984 ext4_xattr_inode_unlink(inode,
985 le32_to_cpu(s->here->e_value_inum));
986 s->here->e_value_inum = 0;
987 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 if (!i->value) {
989 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700990 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 last = ENTRY((void *)last - size);
992 memmove(s->here, (void *)s->here + size,
993 (void *)last - (void *)s->here + sizeof(__u32));
994 memset(last, 0, size);
995 }
996 }
997
998 if (i->value) {
999 /* Insert the new value. */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001000 if (in_inode) {
1001 unsigned long ea_ino =
1002 le32_to_cpu(s->here->e_value_inum);
1003 rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
1004 i->value, i->value_len);
1005 if (rc)
1006 goto out;
1007 s->here->e_value_inum = cpu_to_le32(ea_ino);
1008 s->here->e_value_offs = 0;
1009 } else if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001010 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011 void *val = s->base + min_offs - size;
1012 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001013 s->here->e_value_inum = 0;
Theodore Ts'obd9926e2012-12-11 03:31:49 -05001014 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1015 memset(val, 0, size);
1016 } else {
1017 /* Clear the pad bytes first. */
1018 memset(val + size - EXT4_XATTR_PAD, 0,
1019 EXT4_XATTR_PAD);
1020 memcpy(val, i->value, i->value_len);
1021 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001022 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001023 s->here->e_value_size = cpu_to_le32(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04001025
1026out:
1027 return rc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001028}
1029
Mingming Cao617ba132006-10-11 01:20:53 -07001030struct ext4_xattr_block_find {
1031 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032 struct buffer_head *bh;
1033};
1034
1035static int
Mingming Cao617ba132006-10-11 01:20:53 -07001036ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1037 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038{
1039 struct super_block *sb = inode->i_sb;
1040 int error;
1041
1042 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1043 i->name_index, i->name, i->value, (long)i->value_len);
1044
Mingming Cao617ba132006-10-11 01:20:53 -07001045 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -07001047 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048 error = -EIO;
1049 if (!bs->bh)
1050 goto cleanup;
1051 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1052 atomic_read(&(bs->bh->b_count)),
1053 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001054 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001055 EXT4_ERROR_INODE(inode, "bad block %llu",
1056 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001057 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 goto cleanup;
1059 }
1060 /* Find the named attribute. */
1061 bs->s.base = BHDR(bs->bh);
1062 bs->s.first = BFIRST(bs->bh);
1063 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1064 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001065 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001066 i->name, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 if (error && error != -ENODATA)
1068 goto cleanup;
1069 bs->s.not_found = error;
1070 }
1071 error = 0;
1072
1073cleanup:
1074 return error;
1075}
1076
1077static int
Mingming Cao617ba132006-10-11 01:20:53 -07001078ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1079 struct ext4_xattr_info *i,
1080 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081{
1082 struct super_block *sb = inode->i_sb;
1083 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001084 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -05001085 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001086 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -05001087 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001088
Mingming Cao617ba132006-10-11 01:20:53 -07001089#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -04001092 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001093 error = ext4_journal_get_write_access(handle, bs->bh);
1094 if (error)
1095 goto cleanup;
1096 lock_buffer(bs->bh);
1097
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -05001099 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1100
1101 /*
1102 * This must happen under buffer lock for
1103 * ext4_xattr_block_set() to reliably detect modified
1104 * block
1105 */
Jan Kara7a2508e2016-02-22 22:35:22 -05001106 mb_cache_entry_delete_block(ext4_mb_cache, hash,
1107 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 ea_bdebug(bs->bh, "modifying in-place");
Andreas Dilgere50e5122017-06-21 21:10:32 -04001109 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 if (!error) {
1111 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001112 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001114 ext4_xattr_cache_insert(ext4_mb_cache,
1115 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116 }
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001117 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001119 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 goto bad_block;
1121 if (!error)
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001122 error = ext4_handle_dirty_metadata(handle,
1123 inode,
1124 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 if (error)
1126 goto cleanup;
1127 goto inserted;
1128 } else {
1129 int offset = (char *)s->here - bs->bh->b_data;
1130
Mingming Cao8a2bfdc2007-02-28 20:13:35 -08001131 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -04001133 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 error = -ENOMEM;
1135 if (s->base == NULL)
1136 goto cleanup;
1137 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1138 s->first = ENTRY(header(s->base)+1);
1139 header(s->base)->h_refcount = cpu_to_le32(1);
1140 s->here = ENTRY(s->base + offset);
1141 s->end = s->base + bs->bh->b_size;
1142 }
1143 } else {
1144 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -04001145 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 /* assert(header == s->base) */
1147 error = -ENOMEM;
1148 if (s->base == NULL)
1149 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001150 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001151 header(s->base)->h_blocks = cpu_to_le32(1);
1152 header(s->base)->h_refcount = cpu_to_le32(1);
1153 s->first = ENTRY(header(s->base)+1);
1154 s->here = ENTRY(header(s->base)+1);
1155 s->end = s->base + sb->s_blocksize;
1156 }
1157
Andreas Dilgere50e5122017-06-21 21:10:32 -04001158 error = ext4_xattr_set_entry(i, s, handle, inode);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001159 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 goto bad_block;
1161 if (error)
1162 goto cleanup;
1163 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -07001164 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165
1166inserted:
1167 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001168 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169 if (new_bh) {
1170 /* We found an identical block in the cache. */
1171 if (new_bh == bs->bh)
1172 ea_bdebug(new_bh, "keeping");
1173 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001174 u32 ref;
1175
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001176 WARN_ON_ONCE(dquot_initialize_needed(inode));
1177
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 /* The old block is released after updating
1179 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001180 error = dquot_alloc_block(inode,
1181 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001182 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -04001184 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001185 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186 new_bh);
1187 if (error)
1188 goto cleanup_dquot;
1189 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -05001190 /*
1191 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001192 * freeing, rehashing or adding references to
1193 * xattr block. Once we hold buffer lock xattr
1194 * block's state is stable so we can check
1195 * whether the block got freed / rehashed or
1196 * not. Since we unhash mbcache entry under
1197 * buffer lock when freeing / rehashing xattr
1198 * block, checking whether entry is still
1199 * hashed is reliable. Same rules hold for
1200 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -05001201 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001202 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1203 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -05001204 /*
1205 * Undo everything and check mbcache
1206 * again.
1207 */
1208 unlock_buffer(new_bh);
1209 dquot_free_block(inode,
1210 EXT4_C2B(EXT4_SB(sb),
1211 1));
1212 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001213 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -05001214 ce = NULL;
1215 new_bh = NULL;
1216 goto inserted;
1217 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001218 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1219 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1220 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1221 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001223 ref);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001224 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 unlock_buffer(new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001226 error = ext4_handle_dirty_metadata(handle,
1227 inode,
1228 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 if (error)
1230 goto cleanup_dquot;
1231 }
Jan Kara7a2508e2016-02-22 22:35:22 -05001232 mb_cache_entry_touch(ext4_mb_cache, ce);
1233 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 ce = NULL;
1235 } else if (bs->bh && s->base == bs->bh->b_data) {
1236 /* We were modifying this block in-place. */
1237 ea_bdebug(bs->bh, "keeping this block");
1238 new_bh = bs->bh;
1239 get_bh(new_bh);
1240 } else {
1241 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001242 ext4_fsblk_t goal, block;
1243
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001244 WARN_ON_ONCE(dquot_initialize_needed(inode));
1245
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001246 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -04001247 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001248
1249 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001250 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001251 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1252
Allison Henderson55f020d2011-05-25 07:41:26 -04001253 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1254 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 if (error)
1256 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001257
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001258 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001259 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1260
Joe Perchesace36ad2012-03-19 23:11:43 -04001261 ea_idebug(inode, "creating block %llu",
1262 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263
1264 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001265 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001266 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001267getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001268 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001269 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 goto cleanup;
1271 }
1272 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001273 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 if (error) {
1275 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001276 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 goto getblk_failed;
1278 }
1279 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001280 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 set_buffer_uptodate(new_bh);
1282 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001283 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'odac7a4b2017-03-25 17:22:47 -04001284 error = ext4_handle_dirty_metadata(handle, inode,
1285 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 if (error)
1287 goto cleanup;
1288 }
1289 }
1290
1291 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001292 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293
1294 /* Drop the previous xattr block. */
1295 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001296 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 error = 0;
1298
1299cleanup:
1300 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001301 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 brelse(new_bh);
1303 if (!(bs->bh && s->base == bs->bh->b_data))
1304 kfree(s->base);
1305
1306 return error;
1307
1308cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001309 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310 goto cleanup;
1311
1312bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001313 EXT4_ERROR_INODE(inode, "bad block %llu",
1314 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001315 goto cleanup;
1316
1317#undef header
1318}
1319
Tao Ma879b3822012-12-05 10:28:46 -05001320int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1321 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322{
Mingming Cao617ba132006-10-11 01:20:53 -07001323 struct ext4_xattr_ibody_header *header;
1324 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001325 int error;
1326
Mingming Cao617ba132006-10-11 01:20:53 -07001327 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001328 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001329 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 header = IHDR(inode, raw_inode);
1331 is->s.base = is->s.first = IFIRST(header);
1332 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001333 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001334 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001335 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001336 if (error)
1337 return error;
1338 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001339 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Eric Biggers6ba644b2017-04-30 00:01:02 -04001340 i->name, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341 if (error && error != -ENODATA)
1342 return error;
1343 is->s.not_found = error;
1344 }
1345 return 0;
1346}
1347
Tao Ma0d812f72012-12-10 14:06:02 -05001348int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1349 struct ext4_xattr_info *i,
1350 struct ext4_xattr_ibody_find *is)
1351{
1352 struct ext4_xattr_ibody_header *header;
1353 struct ext4_xattr_search *s = &is->s;
1354 int error;
1355
1356 if (EXT4_I(inode)->i_extra_isize == 0)
1357 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001358 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001359 if (error) {
1360 if (error == -ENOSPC &&
1361 ext4_has_inline_data(inode)) {
1362 error = ext4_try_to_evict_inline_data(handle, inode,
1363 EXT4_XATTR_LEN(strlen(i->name) +
1364 EXT4_XATTR_SIZE(i->value_len)));
1365 if (error)
1366 return error;
1367 error = ext4_xattr_ibody_find(inode, i, is);
1368 if (error)
1369 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001370 error = ext4_xattr_set_entry(i, s, handle, inode);
Tao Ma0d812f72012-12-10 14:06:02 -05001371 }
1372 if (error)
1373 return error;
1374 }
1375 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1376 if (!IS_LAST_ENTRY(s->first)) {
1377 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1378 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1379 } else {
1380 header->h_magic = cpu_to_le32(0);
1381 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1382 }
1383 return 0;
1384}
1385
Andreas Dilgere50e5122017-06-21 21:10:32 -04001386static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001387 struct ext4_xattr_info *i,
1388 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389{
Mingming Cao617ba132006-10-11 01:20:53 -07001390 struct ext4_xattr_ibody_header *header;
1391 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001392 int error;
1393
Mingming Cao617ba132006-10-11 01:20:53 -07001394 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395 return -ENOSPC;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001396 error = ext4_xattr_set_entry(i, s, handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001397 if (error)
1398 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001399 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001401 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001402 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 } else {
1404 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001405 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 }
1407 return 0;
1408}
1409
Jan Kara3fd16462016-02-22 22:43:04 -05001410static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1411 struct ext4_xattr_info *i)
1412{
1413 void *value;
1414
1415 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1416 return 0;
1417 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1418 return !memcmp(value, i->value, i->value_len);
1419}
1420
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421/*
Mingming Cao617ba132006-10-11 01:20:53 -07001422 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001424 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 * is NULL to remove an existing extended attribute, and non-NULL to
1426 * either replace an existing extended attribute, or create a new extended
1427 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1428 * specify that an extended attribute must exist and must not exist
1429 * previous to the call, respectively.
1430 *
1431 * Returns 0, or a negative error number on failure.
1432 */
1433int
Mingming Cao617ba132006-10-11 01:20:53 -07001434ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001435 const char *name, const void *value, size_t value_len,
1436 int flags)
1437{
Mingming Cao617ba132006-10-11 01:20:53 -07001438 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 .name_index = name_index,
1440 .name = name,
1441 .value = value,
1442 .value_len = value_len,
Andreas Dilgere50e5122017-06-21 21:10:32 -04001443 .in_inode = 0,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444 };
Mingming Cao617ba132006-10-11 01:20:53 -07001445 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 .s = { .not_found = -ENODATA, },
1447 };
Mingming Cao617ba132006-10-11 01:20:53 -07001448 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449 .s = { .not_found = -ENODATA, },
1450 };
Theodore Ts'oc755e252017-01-11 21:50:46 -05001451 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 int error;
1453
1454 if (!name)
1455 return -EINVAL;
1456 if (strlen(name) > 255)
1457 return -ERANGE;
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001458
Theodore Ts'oc755e252017-01-11 21:50:46 -05001459 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001460
Eric Sandeen66543612011-10-26 03:32:07 -04001461 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001462 if (error)
1463 goto cleanup;
1464
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001465 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001466 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1467 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001468 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 }
1470
Mingming Cao617ba132006-10-11 01:20:53 -07001471 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001472 if (error)
1473 goto cleanup;
1474 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001475 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 if (error)
1477 goto cleanup;
1478 if (is.s.not_found && bs.s.not_found) {
1479 error = -ENODATA;
1480 if (flags & XATTR_REPLACE)
1481 goto cleanup;
1482 error = 0;
1483 if (!value)
1484 goto cleanup;
1485 } else {
1486 error = -EEXIST;
1487 if (flags & XATTR_CREATE)
1488 goto cleanup;
1489 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 if (!value) {
1491 if (!is.s.not_found)
Andreas Dilgere50e5122017-06-21 21:10:32 -04001492 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001494 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001495 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001496 error = 0;
1497 /* Xattr value did not change? Save us some work and bail out */
1498 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1499 goto cleanup;
1500 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1501 goto cleanup;
1502
Andreas Dilgere50e5122017-06-21 21:10:32 -04001503 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 if (!error && !bs.s.not_found) {
1505 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001506 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001508 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1509 error = ext4_xattr_block_find(inode, &i, &bs);
1510 if (error)
1511 goto cleanup;
1512 }
Mingming Cao617ba132006-10-11 01:20:53 -07001513 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Andreas Dilgere50e5122017-06-21 21:10:32 -04001514 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1515 error == -ENOSPC) {
1516 /* xattr not fit to block, store at external
1517 * inode */
1518 i.in_inode = 1;
1519 error = ext4_xattr_ibody_set(handle, inode,
1520 &i, &is);
1521 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 if (error)
1523 goto cleanup;
1524 if (!is.s.not_found) {
1525 i.value = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001526 error = ext4_xattr_ibody_set(handle, inode, &i,
1527 &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528 }
1529 }
1530 }
1531 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001532 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001533 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001534 if (!value)
Theodore Ts'oc755e252017-01-11 21:50:46 -05001535 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001536 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001538 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539 * error != 0.
1540 */
1541 is.iloc.bh = NULL;
1542 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001543 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001544 }
1545
1546cleanup:
1547 brelse(is.iloc.bh);
1548 brelse(bs.bh);
Theodore Ts'oc755e252017-01-11 21:50:46 -05001549 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550 return error;
1551}
1552
1553/*
Mingming Cao617ba132006-10-11 01:20:53 -07001554 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 *
Mingming Cao617ba132006-10-11 01:20:53 -07001556 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001557 * attribute modification is a filesystem transaction by itself.
1558 *
1559 * Returns 0, or a negative error number on failure.
1560 */
1561int
Mingming Cao617ba132006-10-11 01:20:53 -07001562ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 const void *value, size_t value_len, int flags)
1564{
1565 handle_t *handle;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001566 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001568 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001569
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001570 error = dquot_initialize(inode);
1571 if (error)
1572 return error;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001573
1574 if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
1575 ext4_has_feature_ea_inode(sb)) {
1576 int nrblocks = (value_len + sb->s_blocksize - 1) >>
1577 sb->s_blocksize_bits;
1578
1579 /* For new inode */
1580 credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
1581
1582 /* For data blocks of EA inode */
1583 credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
1584 }
1585
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001587 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 if (IS_ERR(handle)) {
1589 error = PTR_ERR(handle);
1590 } else {
1591 int error2;
1592
Mingming Cao617ba132006-10-11 01:20:53 -07001593 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001594 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001595 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 if (error == -ENOSPC &&
Andreas Dilgere50e5122017-06-21 21:10:32 -04001597 ext4_should_retry_alloc(sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598 goto retry;
1599 if (error == 0)
1600 error = error2;
1601 }
1602
1603 return error;
1604}
1605
1606/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001607 * Shift the EA entries in the inode to create space for the increased
1608 * i_extra_isize.
1609 */
1610static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1611 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001612 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001613{
1614 struct ext4_xattr_entry *last = entry;
1615 int new_offs;
1616
Jan Kara94405712016-08-29 15:41:11 -04001617 /* We always shift xattr headers further thus offsets get lower */
1618 BUG_ON(value_offs_shift > 0);
1619
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001620 /* Adjust the value offsets of the entries */
1621 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Andreas Dilgere50e5122017-06-21 21:10:32 -04001622 if (!last->e_value_inum && last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001623 new_offs = le16_to_cpu(last->e_value_offs) +
1624 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001625 last->e_value_offs = cpu_to_le16(new_offs);
1626 }
1627 }
1628 /* Shift the entries by n bytes */
1629 memmove(to, from, n);
1630}
1631
1632/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001633 * Move xattr pointed to by 'entry' from inode into external xattr block
1634 */
1635static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1636 struct ext4_inode *raw_inode,
1637 struct ext4_xattr_entry *entry)
1638{
1639 struct ext4_xattr_ibody_find *is = NULL;
1640 struct ext4_xattr_block_find *bs = NULL;
1641 char *buffer = NULL, *b_entry_name = NULL;
1642 size_t value_offs, value_size;
1643 struct ext4_xattr_info i = {
1644 .value = NULL,
1645 .value_len = 0,
1646 .name_index = entry->e_name_index,
1647 };
1648 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1649 int error;
1650
1651 value_offs = le16_to_cpu(entry->e_value_offs);
1652 value_size = le32_to_cpu(entry->e_value_size);
1653
1654 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1655 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1656 buffer = kmalloc(value_size, GFP_NOFS);
1657 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1658 if (!is || !bs || !buffer || !b_entry_name) {
1659 error = -ENOMEM;
1660 goto out;
1661 }
1662
1663 is->s.not_found = -ENODATA;
1664 bs->s.not_found = -ENODATA;
1665 is->iloc.bh = NULL;
1666 bs->bh = NULL;
1667
1668 /* Save the entry name and the entry value */
1669 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1670 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1671 b_entry_name[entry->e_name_len] = '\0';
1672 i.name = b_entry_name;
1673
1674 error = ext4_get_inode_loc(inode, &is->iloc);
1675 if (error)
1676 goto out;
1677
1678 error = ext4_xattr_ibody_find(inode, &i, is);
1679 if (error)
1680 goto out;
1681
1682 /* Remove the chosen entry from the inode */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001683 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001684 if (error)
1685 goto out;
1686
1687 i.name = b_entry_name;
1688 i.value = buffer;
1689 i.value_len = value_size;
1690 error = ext4_xattr_block_find(inode, &i, bs);
1691 if (error)
1692 goto out;
1693
1694 /* Add entry which was removed from the inode into the block */
1695 error = ext4_xattr_block_set(handle, inode, &i, bs);
1696 if (error)
1697 goto out;
1698 error = 0;
1699out:
1700 kfree(b_entry_name);
1701 kfree(buffer);
1702 if (is)
1703 brelse(is->iloc.bh);
1704 kfree(is);
1705 kfree(bs);
1706
1707 return error;
1708}
1709
Jan Karadfa20642016-08-29 15:44:11 -04001710static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1711 struct ext4_inode *raw_inode,
1712 int isize_diff, size_t ifree,
1713 size_t bfree, int *total_ino)
1714{
1715 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1716 struct ext4_xattr_entry *small_entry;
1717 struct ext4_xattr_entry *entry;
1718 struct ext4_xattr_entry *last;
1719 unsigned int entry_size; /* EA entry size */
1720 unsigned int total_size; /* EA entry size + value size */
1721 unsigned int min_total_size;
1722 int error;
1723
1724 while (isize_diff > ifree) {
1725 entry = NULL;
1726 small_entry = NULL;
1727 min_total_size = ~0U;
1728 last = IFIRST(header);
1729 /* Find the entry best suited to be pushed into EA block */
1730 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1731 total_size =
1732 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1733 EXT4_XATTR_LEN(last->e_name_len);
1734 if (total_size <= bfree &&
1735 total_size < min_total_size) {
1736 if (total_size + ifree < isize_diff) {
1737 small_entry = last;
1738 } else {
1739 entry = last;
1740 min_total_size = total_size;
1741 }
1742 }
1743 }
1744
1745 if (entry == NULL) {
1746 if (small_entry == NULL)
1747 return -ENOSPC;
1748 entry = small_entry;
1749 }
1750
1751 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1752 total_size = entry_size +
1753 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1754 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1755 entry);
1756 if (error)
1757 return error;
1758
1759 *total_ino -= entry_size;
1760 ifree += total_size;
1761 bfree -= total_size;
1762 }
1763
1764 return 0;
1765}
1766
Jan Kara3f2571c2016-08-29 15:42:11 -04001767/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001768 * Expand an inode by new_extra_isize bytes when EAs are present.
1769 * Returns 0 on success or negative error number on failure.
1770 */
1771int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1772 struct ext4_inode *raw_inode, handle_t *handle)
1773{
1774 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001775 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001776 size_t min_offs;
1777 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001778 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001779 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001780 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001781 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
Jan Karad0141192016-08-11 11:50:30 -04001782 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001783 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001784
Theodore Ts'oc755e252017-01-11 21:50:46 -05001785 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1786 return 0;
1787
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001788retry:
Jan Karad0141192016-08-11 11:50:30 -04001789 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001790 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1791 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001792
1793 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001794
1795 /*
1796 * Check if enough free space is available in the inode to shift the
1797 * entries ahead by new_extra_isize.
1798 */
1799
Jan Kara6e0cd082016-08-29 15:43:11 -04001800 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001801 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1802 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001803 total_ino = sizeof(struct ext4_xattr_ibody_header);
1804
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001805 error = xattr_check_inode(inode, header, end);
1806 if (error)
1807 goto cleanup;
1808
Jan Kara6e0cd082016-08-29 15:43:11 -04001809 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001810 if (ifree >= isize_diff)
1811 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001812
1813 /*
1814 * Enough free space isn't available in the inode, check if
1815 * EA block can hold new_extra_isize bytes.
1816 */
1817 if (EXT4_I(inode)->i_file_acl) {
1818 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1819 error = -EIO;
1820 if (!bh)
1821 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001822 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001823 EXT4_ERROR_INODE(inode, "bad block %llu",
1824 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001825 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001826 goto cleanup;
1827 }
1828 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001829 end = bh->b_data + bh->b_size;
1830 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001831 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1832 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001833 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001834 if (!tried_min_extra_isize && s_min_extra_isize) {
1835 tried_min_extra_isize++;
1836 new_extra_isize = s_min_extra_isize;
1837 brelse(bh);
1838 goto retry;
1839 }
Jan Karadfa20642016-08-29 15:44:11 -04001840 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001841 goto cleanup;
1842 }
1843 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001844 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001845 }
1846
Jan Karadfa20642016-08-29 15:44:11 -04001847 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1848 isize_diff, ifree, bfree,
1849 &total_ino);
1850 if (error) {
1851 if (error == -ENOSPC && !tried_min_extra_isize &&
1852 s_min_extra_isize) {
1853 tried_min_extra_isize++;
1854 new_extra_isize = s_min_extra_isize;
1855 brelse(bh);
1856 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001857 }
Jan Karadfa20642016-08-29 15:44:11 -04001858 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001859 }
Jan Karae3014d12016-08-29 15:38:11 -04001860shift:
1861 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001862 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001863 - new_extra_isize, (void *)raw_inode +
1864 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001865 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001866 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001867 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001868out:
Theodore Ts'oc755e252017-01-11 21:50:46 -05001869 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001870 return 0;
1871
1872cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001873 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001874 /*
Theodore Ts'oc755e252017-01-11 21:50:46 -05001875 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001876 */
Theodore Ts'oc755e252017-01-11 21:50:46 -05001877 no_expand = 1;
1878 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001879 return error;
1880}
1881
1882
Andreas Dilgere50e5122017-06-21 21:10:32 -04001883#define EIA_INCR 16 /* must be 2^n */
1884#define EIA_MASK (EIA_INCR - 1)
1885/* Add the large xattr @ino into @lea_ino_array for later deletion.
1886 * If @lea_ino_array is new or full it will be grown and the old
1887 * contents copied over.
1888 */
1889static int
1890ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
1891{
1892 if (*lea_ino_array == NULL) {
1893 /*
1894 * Start with 15 inodes, so it fits into a power-of-two size.
1895 * If *lea_ino_array is NULL, this is essentially offsetof()
1896 */
1897 (*lea_ino_array) =
1898 kmalloc(offsetof(struct ext4_xattr_ino_array,
1899 xia_inodes[EIA_MASK]),
1900 GFP_NOFS);
1901 if (*lea_ino_array == NULL)
1902 return -ENOMEM;
1903 (*lea_ino_array)->xia_count = 0;
1904 } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
1905 /* expand the array once all 15 + n * 16 slots are full */
1906 struct ext4_xattr_ino_array *new_array = NULL;
1907 int count = (*lea_ino_array)->xia_count;
1908
1909 /* if new_array is NULL, this is essentially offsetof() */
1910 new_array = kmalloc(
1911 offsetof(struct ext4_xattr_ino_array,
1912 xia_inodes[count + EIA_INCR]),
1913 GFP_NOFS);
1914 if (new_array == NULL)
1915 return -ENOMEM;
1916 memcpy(new_array, *lea_ino_array,
1917 offsetof(struct ext4_xattr_ino_array,
1918 xia_inodes[count]));
1919 kfree(*lea_ino_array);
1920 *lea_ino_array = new_array;
1921 }
1922 (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
1923 return 0;
1924}
1925
1926/**
1927 * Add xattr inode to orphan list
1928 */
1929static int
1930ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
1931 int credits, struct ext4_xattr_ino_array *lea_ino_array)
1932{
1933 struct inode *ea_inode = NULL;
1934 int idx = 0, error = 0;
1935
1936 if (lea_ino_array == NULL)
1937 return 0;
1938
1939 for (; idx < lea_ino_array->xia_count; ++idx) {
1940 if (!ext4_handle_has_enough_credits(handle, credits)) {
1941 error = ext4_journal_extend(handle, credits);
1942 if (error > 0)
1943 error = ext4_journal_restart(handle, credits);
1944
1945 if (error != 0) {
1946 ext4_warning(inode->i_sb,
1947 "couldn't extend journal "
1948 "(err %d)", error);
1949 return error;
1950 }
1951 }
1952 ea_inode = ext4_xattr_inode_iget(inode,
1953 lea_ino_array->xia_inodes[idx], &error);
1954 if (error)
1955 continue;
1956 ext4_orphan_add(handle, ea_inode);
1957 /* the inode's i_count will be released by caller */
1958 }
1959
1960 return 0;
1961}
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001962
1963/*
Mingming Cao617ba132006-10-11 01:20:53 -07001964 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965 *
Andreas Dilgere50e5122017-06-21 21:10:32 -04001966 * Free extended attribute resources associated with this inode. Traverse
1967 * all entries and unlink any xattr inodes associated with this inode. This
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001968 * is called immediately before an inode is freed. We have exclusive
Andreas Dilgere50e5122017-06-21 21:10:32 -04001969 * access to the inode. If an orphan inode is deleted it will also delete any
1970 * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
1971 * to ensure they belong to the parent inode and were not deleted already.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 */
Andreas Dilgere50e5122017-06-21 21:10:32 -04001973int
1974ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1975 struct ext4_xattr_ino_array **lea_ino_array)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001976{
1977 struct buffer_head *bh = NULL;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001978 struct ext4_xattr_ibody_header *header;
1979 struct ext4_inode *raw_inode;
1980 struct ext4_iloc iloc;
1981 struct ext4_xattr_entry *entry;
1982 int credits = 3, error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983
Andreas Dilgere50e5122017-06-21 21:10:32 -04001984 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
1985 goto delete_external_ea;
1986
1987 error = ext4_get_inode_loc(inode, &iloc);
1988 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 goto cleanup;
Andreas Dilgere50e5122017-06-21 21:10:32 -04001990 raw_inode = ext4_raw_inode(&iloc);
1991 header = IHDR(inode, raw_inode);
1992 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
1993 entry = EXT4_XATTR_NEXT(entry)) {
1994 if (!entry->e_value_inum)
1995 continue;
1996 if (ext4_expand_ino_array(lea_ino_array,
1997 entry->e_value_inum) != 0) {
1998 brelse(iloc.bh);
1999 goto cleanup;
2000 }
2001 entry->e_value_inum = 0;
2002 }
2003 brelse(iloc.bh);
2004
2005delete_external_ea:
2006 if (!EXT4_I(inode)->i_file_acl) {
2007 /* add xattr inode to orphan list */
2008 ext4_xattr_inode_orphan_add(handle, inode, credits,
2009 *lea_ino_array);
2010 goto cleanup;
2011 }
Mingming Cao617ba132006-10-11 01:20:53 -07002012 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002014 EXT4_ERROR_INODE(inode, "block %llu read error",
2015 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 goto cleanup;
2017 }
Mingming Cao617ba132006-10-11 01:20:53 -07002018 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002020 EXT4_ERROR_INODE(inode, "bad block %llu",
2021 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022 goto cleanup;
2023 }
Andreas Dilgere50e5122017-06-21 21:10:32 -04002024
2025 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2026 entry = EXT4_XATTR_NEXT(entry)) {
2027 if (!entry->e_value_inum)
2028 continue;
2029 if (ext4_expand_ino_array(lea_ino_array,
2030 entry->e_value_inum) != 0)
2031 goto cleanup;
2032 entry->e_value_inum = 0;
2033 }
2034
2035 /* add xattr inode to orphan list */
2036 error = ext4_xattr_inode_orphan_add(handle, inode, credits,
2037 *lea_ino_array);
2038 if (error != 0)
2039 goto cleanup;
2040
2041 if (!IS_NOQUOTA(inode))
2042 credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
2043
2044 if (!ext4_handle_has_enough_credits(handle, credits)) {
2045 error = ext4_journal_extend(handle, credits);
2046 if (error > 0)
2047 error = ext4_journal_restart(handle, credits);
2048 if (error != 0) {
2049 ext4_warning(inode->i_sb,
2050 "couldn't extend journal (err %d)", error);
2051 goto cleanup;
2052 }
2053 }
2054
Mingming Cao617ba132006-10-11 01:20:53 -07002055 ext4_xattr_release_block(handle, inode, bh);
2056 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057
2058cleanup:
2059 brelse(bh);
Andreas Dilgere50e5122017-06-21 21:10:32 -04002060
2061 return error;
2062}
2063
2064void
2065ext4_xattr_inode_array_free(struct inode *inode,
2066 struct ext4_xattr_ino_array *lea_ino_array)
2067{
2068 struct inode *ea_inode = NULL;
2069 int idx = 0;
2070 int err;
2071
2072 if (lea_ino_array == NULL)
2073 return;
2074
2075 for (; idx < lea_ino_array->xia_count; ++idx) {
2076 ea_inode = ext4_xattr_inode_iget(inode,
2077 lea_ino_array->xia_inodes[idx], &err);
2078 if (err)
2079 continue;
2080 /* for inode's i_count get from ext4_xattr_delete_inode */
2081 if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
2082 iput(ea_inode);
2083 clear_nlink(ea_inode);
2084 iput(ea_inode);
2085 }
2086 kfree(lea_ino_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002087}
2088
2089/*
Mingming Cao617ba132006-10-11 01:20:53 -07002090 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 *
2092 * Create a new entry in the extended attribute cache, and insert
2093 * it unless such an entry is already in the cache.
2094 *
2095 * Returns 0, or a negative error number on failure.
2096 */
2097static void
Jan Kara7a2508e2016-02-22 22:35:22 -05002098ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002100 struct ext4_xattr_header *header = BHDR(bh);
2101 __u32 hash = le32_to_cpu(header->h_hash);
2102 int reusable = le32_to_cpu(header->h_refcount) <
2103 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104 int error;
2105
Jan Kara7a2508e2016-02-22 22:35:22 -05002106 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05002107 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05002109 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05002111 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002112 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002113}
2114
2115/*
Mingming Cao617ba132006-10-11 01:20:53 -07002116 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117 *
2118 * Compare two extended attribute blocks for equality.
2119 *
2120 * Returns 0 if the blocks are equal, 1 if they differ, and
2121 * a negative error number on errors.
2122 */
2123static int
Mingming Cao617ba132006-10-11 01:20:53 -07002124ext4_xattr_cmp(struct ext4_xattr_header *header1,
2125 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002126{
Mingming Cao617ba132006-10-11 01:20:53 -07002127 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128
2129 entry1 = ENTRY(header1+1);
2130 entry2 = ENTRY(header2+1);
2131 while (!IS_LAST_ENTRY(entry1)) {
2132 if (IS_LAST_ENTRY(entry2))
2133 return 1;
2134 if (entry1->e_hash != entry2->e_hash ||
2135 entry1->e_name_index != entry2->e_name_index ||
2136 entry1->e_name_len != entry2->e_name_len ||
2137 entry1->e_value_size != entry2->e_value_size ||
Andreas Dilgere50e5122017-06-21 21:10:32 -04002138 entry1->e_value_inum != entry2->e_value_inum ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2140 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2142 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2143 le32_to_cpu(entry1->e_value_size)))
2144 return 1;
2145
Mingming Cao617ba132006-10-11 01:20:53 -07002146 entry1 = EXT4_XATTR_NEXT(entry1);
2147 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 }
2149 if (!IS_LAST_ENTRY(entry2))
2150 return 1;
2151 return 0;
2152}
2153
2154/*
Mingming Cao617ba132006-10-11 01:20:53 -07002155 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002156 *
2157 * Find an identical extended attribute block.
2158 *
2159 * Returns a pointer to the block found, or NULL if such a block was
2160 * not found or an error occurred.
2161 */
2162static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07002163ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05002164 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165{
2166 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002167 struct mb_cache_entry *ce;
2168 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169
2170 if (!header->h_hash)
2171 return NULL; /* never share */
2172 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05002173 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 while (ce) {
2175 struct buffer_head *bh;
2176
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002177 bh = sb_bread(inode->i_sb, ce->e_block);
2178 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002179 EXT4_ERROR_INODE(inode, "block %lu read error",
2180 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07002181 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002182 *pce = ce;
2183 return bh;
2184 }
2185 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05002186 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002187 }
2188 return NULL;
2189}
2190
2191#define NAME_HASH_SHIFT 5
2192#define VALUE_HASH_SHIFT 16
2193
2194/*
Mingming Cao617ba132006-10-11 01:20:53 -07002195 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002196 *
2197 * Compute the hash of an extended attribute.
2198 */
Mingming Cao617ba132006-10-11 01:20:53 -07002199static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2200 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002201{
2202 __u32 hash = 0;
2203 char *name = entry->e_name;
2204 int n;
2205
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002206 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002207 hash = (hash << NAME_HASH_SHIFT) ^
2208 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2209 *name++;
2210 }
2211
Andreas Dilgere50e5122017-06-21 21:10:32 -04002212 if (!entry->e_value_inum && entry->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002213 __le32 *value = (__le32 *)((char *)header +
2214 le16_to_cpu(entry->e_value_offs));
2215 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07002216 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217 hash = (hash << VALUE_HASH_SHIFT) ^
2218 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2219 le32_to_cpu(*value++);
2220 }
2221 }
2222 entry->e_hash = cpu_to_le32(hash);
2223}
2224
2225#undef NAME_HASH_SHIFT
2226#undef VALUE_HASH_SHIFT
2227
2228#define BLOCK_HASH_SHIFT 16
2229
2230/*
Mingming Cao617ba132006-10-11 01:20:53 -07002231 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 *
2233 * Re-compute the extended attribute hash value after an entry has changed.
2234 */
Mingming Cao617ba132006-10-11 01:20:53 -07002235static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2236 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002237{
Mingming Cao617ba132006-10-11 01:20:53 -07002238 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 __u32 hash = 0;
2240
Mingming Cao617ba132006-10-11 01:20:53 -07002241 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 here = ENTRY(header+1);
2243 while (!IS_LAST_ENTRY(here)) {
2244 if (!here->e_hash) {
2245 /* Block is not shared if an entry's hash value == 0 */
2246 hash = 0;
2247 break;
2248 }
2249 hash = (hash << BLOCK_HASH_SHIFT) ^
2250 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2251 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07002252 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002253 }
2254 header->h_hash = cpu_to_le32(hash);
2255}
2256
2257#undef BLOCK_HASH_SHIFT
2258
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002259#define HASH_BUCKET_BITS 10
2260
Jan Kara7a2508e2016-02-22 22:35:22 -05002261struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05002262ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002263{
Jan Kara7a2508e2016-02-22 22:35:22 -05002264 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002265}
2266
Jan Kara7a2508e2016-02-22 22:35:22 -05002267void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002269 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05002270 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04002272