blob: c19c96840480a66acfce9a830fe3c4cd02a488dc [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 *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000081static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040082 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083
Stephen Hemminger11e27522010-05-13 17:53:18 -070084static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070085 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040086#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080087 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089#endif
Mingming Cao617ba132006-10-11 01:20:53 -070090 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040091#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070092 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093#endif
94};
95
Stephen Hemminger11e27522010-05-13 17:53:18 -070096const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070097 &ext4_xattr_user_handler,
98 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040099#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800100 &posix_acl_access_xattr_handler,
101 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400103#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700104 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105#endif
106 NULL
107};
108
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400109#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
111
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400112static __le32 ext4_xattr_block_csum(struct inode *inode,
113 sector_t block_nr,
114 struct ext4_xattr_header *hdr)
115{
116 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400117 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400118 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400119 __u32 dummy_csum = 0;
120 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400121
Theodore Ts'od6a77102013-04-09 23:59:55 -0400122 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
123 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400124 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
125 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
126 offset += sizeof(dummy_csum);
127 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
128 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400129
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400130 return cpu_to_le32(csum);
131}
132
133static int ext4_xattr_block_csum_verify(struct inode *inode,
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400134 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135{
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400136 struct ext4_xattr_header *hdr = BHDR(bh);
137 int ret = 1;
138
139 if (ext4_has_metadata_csum(inode->i_sb)) {
140 lock_buffer(bh);
141 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
142 bh->b_blocknr, hdr));
143 unlock_buffer(bh);
144 }
145 return ret;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400146}
147
148static void ext4_xattr_block_csum_set(struct inode *inode,
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400149 struct buffer_head *bh)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400150{
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400151 if (ext4_has_metadata_csum(inode->i_sb))
152 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
153 bh->b_blocknr, BHDR(bh));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400154}
155
Stephen Hemminger11e27522010-05-13 17:53:18 -0700156static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700157ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700159 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160
Mingming Cao617ba132006-10-11 01:20:53 -0700161 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
162 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 return handler;
164}
165
166/*
167 * Inode operation listxattr()
168 *
David Howells2b0143b2015-03-17 22:25:59 +0000169 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 */
171ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700172ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000174 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175}
176
177static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400178ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
179 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400181 struct ext4_xattr_entry *e = entry;
182
183 while (!IS_LAST_ENTRY(e)) {
184 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400186 return -EFSCORRUPTED;
Theodore Ts'o9c2860f2018-08-01 12:36:52 -0400187 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
188 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400191
192 while (!IS_LAST_ENTRY(entry)) {
Jan Kara2de58f12016-08-29 15:39:11 -0400193 if (entry->e_value_block != 0)
194 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400195 if (entry->e_value_size != 0 &&
196 (value_start + le16_to_cpu(entry->e_value_offs) <
197 (void *)e + sizeof(__u32) ||
198 value_start + le16_to_cpu(entry->e_value_offs) +
199 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400200 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400201 entry = EXT4_XATTR_NEXT(entry);
202 }
203
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204 return 0;
205}
206
207static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400208ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400210 int error;
211
212 if (buffer_verified(bh))
213 return 0;
214
Mingming Cao617ba132006-10-11 01:20:53 -0700215 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400217 return -EFSCORRUPTED;
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400218 if (!ext4_xattr_block_csum_verify(inode, bh))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400219 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400220 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
221 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400222 if (!error)
223 set_buffer_verified(bh);
224 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225}
226
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400227static int
228__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
229 void *end, const char *function, unsigned int line)
230{
231 struct ext4_xattr_entry *entry = IFIRST(header);
232 int error = -EFSCORRUPTED;
233
234 if (((void *) header >= end) ||
Eric Biggers19962502016-10-15 09:39:31 -0400235 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400236 goto errout;
237 error = ext4_xattr_check_names(entry, end, entry);
238errout:
239 if (error)
240 __ext4_error_inode(inode, function, line, 0,
241 "corrupted in-inode xattr");
242 return error;
243}
244
245#define xattr_check_inode(inode, header, end) \
246 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
247
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700249ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250{
251 size_t value_size = le32_to_cpu(entry->e_value_size);
252
253 if (entry->e_value_block != 0 || value_size > size ||
254 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400255 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 return 0;
257}
258
259static int
Mingming Cao617ba132006-10-11 01:20:53 -0700260ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 const char *name, size_t size, int sorted)
262{
Mingming Cao617ba132006-10-11 01:20:53 -0700263 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 size_t name_len;
265 int cmp = 1;
266
267 if (name == NULL)
268 return -EINVAL;
269 name_len = strlen(name);
270 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700271 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 cmp = name_index - entry->e_name_index;
273 if (!cmp)
274 cmp = name_len - entry->e_name_len;
275 if (!cmp)
276 cmp = memcmp(name, entry->e_name, name_len);
277 if (cmp <= 0 && (sorted || cmp == 0))
278 break;
279 }
280 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700281 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400282 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 return cmp ? -ENODATA : 0;
284}
285
286static int
Mingming Cao617ba132006-10-11 01:20:53 -0700287ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 void *buffer, size_t buffer_size)
289{
290 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700291 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 size_t size;
293 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500294 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295
296 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
297 name_index, name, buffer, (long)buffer_size);
298
299 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700300 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400302 ea_idebug(inode, "reading block %llu",
303 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700304 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 if (!bh)
306 goto cleanup;
307 ea_bdebug(bh, "b_count=%d, refcount=%d",
308 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400309 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500310bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400311 EXT4_ERROR_INODE(inode, "bad block %llu",
312 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400313 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 goto cleanup;
315 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400316 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700318 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400319 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 goto bad_block;
321 if (error)
322 goto cleanup;
323 size = le32_to_cpu(entry->e_value_size);
324 if (buffer) {
325 error = -ERANGE;
326 if (size > buffer_size)
327 goto cleanup;
328 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
329 size);
330 }
331 error = size;
332
333cleanup:
334 brelse(bh);
335 return error;
336}
337
Tao Ma879b3822012-12-05 10:28:46 -0500338int
Mingming Cao617ba132006-10-11 01:20:53 -0700339ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 void *buffer, size_t buffer_size)
341{
Mingming Cao617ba132006-10-11 01:20:53 -0700342 struct ext4_xattr_ibody_header *header;
343 struct ext4_xattr_entry *entry;
344 struct ext4_inode *raw_inode;
345 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700346 size_t size;
347 void *end;
348 int error;
349
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500350 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700352 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353 if (error)
354 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700355 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356 header = IHDR(inode, raw_inode);
357 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700358 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400359 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360 if (error)
361 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700362 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 end - (void *)entry, 0);
364 if (error)
365 goto cleanup;
366 size = le32_to_cpu(entry->e_value_size);
367 if (buffer) {
368 error = -ERANGE;
369 if (size > buffer_size)
370 goto cleanup;
371 memcpy(buffer, (void *)IFIRST(header) +
372 le16_to_cpu(entry->e_value_offs), size);
373 }
374 error = size;
375
376cleanup:
377 brelse(iloc.bh);
378 return error;
379}
380
381/*
Mingming Cao617ba132006-10-11 01:20:53 -0700382 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700383 *
384 * Copy an extended attribute into the buffer
385 * provided, or compute the buffer size required.
386 * Buffer is NULL to compute the size of the buffer required.
387 *
388 * Returns a negative error number on failure, or the number of bytes
389 * used / required on success.
390 */
391int
Mingming Cao617ba132006-10-11 01:20:53 -0700392ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700393 void *buffer, size_t buffer_size)
394{
395 int error;
396
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400397 if (strlen(name) > 255)
398 return -ERANGE;
399
Mingming Cao617ba132006-10-11 01:20:53 -0700400 down_read(&EXT4_I(inode)->xattr_sem);
401 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402 buffer_size);
403 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700404 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700406 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407 return error;
408}
409
410static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000411ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 char *buffer, size_t buffer_size)
413{
414 size_t rest = buffer_size;
415
Mingming Cao617ba132006-10-11 01:20:53 -0700416 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700417 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700418 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700419
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100420 if (handler && (!handler->list || handler->list(dentry))) {
421 const char *prefix = handler->prefix ?: handler->name;
422 size_t prefix_len = strlen(prefix);
423 size_t size = prefix_len + entry->e_name_len + 1;
424
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425 if (buffer) {
426 if (size > rest)
427 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100428 memcpy(buffer, prefix, prefix_len);
429 buffer += prefix_len;
430 memcpy(buffer, entry->e_name, entry->e_name_len);
431 buffer += entry->e_name_len;
432 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700433 }
434 rest -= size;
435 }
436 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100437 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438}
439
440static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000441ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442{
David Howells2b0143b2015-03-17 22:25:59 +0000443 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 struct buffer_head *bh = NULL;
445 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500446 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447
448 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
449 buffer, (long)buffer_size);
450
451 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700452 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400454 ea_idebug(inode, "reading block %llu",
455 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700456 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700457 error = -EIO;
458 if (!bh)
459 goto cleanup;
460 ea_bdebug(bh, "b_count=%d, refcount=%d",
461 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400462 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400463 EXT4_ERROR_INODE(inode, "bad block %llu",
464 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400465 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466 goto cleanup;
467 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400468 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000469 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470
471cleanup:
472 brelse(bh);
473
474 return error;
475}
476
477static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000478ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479{
David Howells2b0143b2015-03-17 22:25:59 +0000480 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700481 struct ext4_xattr_ibody_header *header;
482 struct ext4_inode *raw_inode;
483 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484 void *end;
485 int error;
486
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500487 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700489 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 if (error)
491 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700492 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700494 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400495 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 if (error)
497 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000498 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 buffer, buffer_size);
500
501cleanup:
502 brelse(iloc.bh);
503 return error;
504}
505
506/*
Mingming Cao617ba132006-10-11 01:20:53 -0700507 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 *
509 * Copy a list of attribute names into the buffer
510 * provided, or compute the buffer size required.
511 * Buffer is NULL to compute the size of the buffer required.
512 *
513 * Returns a negative error number on failure, or the number of bytes
514 * used / required on success.
515 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400516static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000517ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500519 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520
David Howells2b0143b2015-03-17 22:25:59 +0000521 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500522 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
523 if (ret < 0)
524 goto errout;
525 if (buffer) {
526 buffer += ret;
527 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500529 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
530 if (ret < 0)
531 goto errout;
532 ret += ret2;
533errout:
David Howells2b0143b2015-03-17 22:25:59 +0000534 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500535 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536}
537
538/*
Mingming Cao617ba132006-10-11 01:20:53 -0700539 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540 * not set, set it.
541 */
Mingming Cao617ba132006-10-11 01:20:53 -0700542static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 struct super_block *sb)
544{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400545 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 return;
547
liang xie5d601252014-05-12 22:06:43 -0400548 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700549 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400550 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400551 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553}
554
555/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400556 * Release the xattr block BH: If the reference count is > 1, decrement it;
557 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 */
559static void
Mingming Cao617ba132006-10-11 01:20:53 -0700560ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 struct buffer_head *bh)
562{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500563 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
564 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800565 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566
liang xie5d601252014-05-12 22:06:43 -0400567 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800568 error = ext4_journal_get_write_access(handle, bh);
569 if (error)
570 goto out;
571
572 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500573 hash = le32_to_cpu(BHDR(bh)->h_hash);
574 ref = le32_to_cpu(BHDR(bh)->h_refcount);
575 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500577 /*
578 * This must happen under buffer lock for
579 * ext4_xattr_block_set() to reliably detect freed block
580 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500581 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400583 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500584 ext4_free_blocks(handle, inode, bh, 0, 1,
585 EXT4_FREE_BLOCKS_METADATA |
586 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500588 ref--;
589 BHDR(bh)->h_refcount = cpu_to_le32(ref);
590 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
591 struct mb_cache_entry *ce;
592
593 ce = mb_cache_entry_get(ext4_mb_cache, hash,
594 bh->b_blocknr);
595 if (ce) {
596 ce->e_reusable = 1;
597 mb_cache_entry_put(ext4_mb_cache, ce);
598 }
599 }
600
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400601 ext4_xattr_block_csum_set(inode, bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400602 /*
603 * Beware of this ugliness: Releasing of xattr block references
604 * from different inodes can race and so we have to protect
605 * from a race where someone else frees the block (and releases
606 * its journal_head) before we are done dirtying the buffer. In
607 * nojournal mode this race is harmless and we actually cannot
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400608 * call ext4_handle_dirty_metadata() with locked buffer as
Jan Karaec4cb1a2014-04-07 10:54:21 -0400609 * that function can call sync_dirty_buffer() so for that case
610 * we handle the dirtying after unlocking the buffer.
611 */
612 if (ext4_handle_valid(handle))
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400613 error = ext4_handle_dirty_metadata(handle, inode, bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500614 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400615 if (!ext4_handle_valid(handle))
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400616 error = ext4_handle_dirty_metadata(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800617 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500618 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500619 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800620 ea_bdebug(bh, "refcount now=%d; releasing",
621 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800623out:
624 ext4_std_error(inode->i_sb, error);
625 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626}
627
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400628/*
629 * Find the available free space for EAs. This also returns the total number of
630 * bytes used by EA entries.
631 */
632static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
633 size_t *min_offs, void *base, int *total)
634{
635 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400636 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400637 size_t offs = le16_to_cpu(last->e_value_offs);
638 if (offs < *min_offs)
639 *min_offs = offs;
640 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500641 if (total)
642 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400643 }
644 return (*min_offs - ((void *)last - base) - sizeof(__u32));
645}
646
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647static int
Mingming Cao617ba132006-10-11 01:20:53 -0700648ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649{
Mingming Cao617ba132006-10-11 01:20:53 -0700650 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
652
653 /* Compute min_offs and last. */
654 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700655 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400656 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657 size_t offs = le16_to_cpu(last->e_value_offs);
658 if (offs < min_offs)
659 min_offs = offs;
660 }
661 }
662 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
663 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400664 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700666 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 }
Mingming Cao617ba132006-10-11 01:20:53 -0700668 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 }
670 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400671 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700672 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 return -ENOSPC;
674 }
675
676 if (i->value && s->not_found) {
677 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700678 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
680 memmove((void *)s->here + size, s->here, rest);
681 memset(s->here, 0, size);
682 s->here->e_name_index = i->name_index;
683 s->here->e_name_len = name_len;
684 memcpy(s->here->e_name, i->name, name_len);
685 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400686 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 void *first_val = s->base + min_offs;
688 size_t offs = le16_to_cpu(s->here->e_value_offs);
689 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700690 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 le32_to_cpu(s->here->e_value_size));
692
Mingming Cao617ba132006-10-11 01:20:53 -0700693 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694 /* The old and the new value have the same
695 size. Just replace. */
696 s->here->e_value_size =
697 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500698 if (i->value == EXT4_ZERO_XATTR_VALUE) {
699 memset(val, 0, size);
700 } else {
701 /* Clear pad bytes first. */
702 memset(val + size - EXT4_XATTR_PAD, 0,
703 EXT4_XATTR_PAD);
704 memcpy(val, i->value, i->value_len);
705 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 return 0;
707 }
708
709 /* Remove the old value. */
710 memmove(first_val + size, first_val, val - first_val);
711 memset(first_val, 0, size);
712 s->here->e_value_size = 0;
713 s->here->e_value_offs = 0;
714 min_offs += size;
715
716 /* Adjust all value offsets. */
717 last = s->first;
718 while (!IS_LAST_ENTRY(last)) {
719 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400720 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 last->e_value_offs =
722 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700723 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 }
725 }
726 if (!i->value) {
727 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700728 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 last = ENTRY((void *)last - size);
730 memmove(s->here, (void *)s->here + size,
731 (void *)last - (void *)s->here + sizeof(__u32));
732 memset(last, 0, size);
733 }
734 }
735
736 if (i->value) {
737 /* Insert the new value. */
738 s->here->e_value_size = cpu_to_le32(i->value_len);
739 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700740 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 void *val = s->base + min_offs - size;
742 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500743 if (i->value == EXT4_ZERO_XATTR_VALUE) {
744 memset(val, 0, size);
745 } else {
746 /* Clear the pad bytes first. */
747 memset(val + size - EXT4_XATTR_PAD, 0,
748 EXT4_XATTR_PAD);
749 memcpy(val, i->value, i->value_len);
750 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 }
752 }
753 return 0;
754}
755
Mingming Cao617ba132006-10-11 01:20:53 -0700756struct ext4_xattr_block_find {
757 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 struct buffer_head *bh;
759};
760
761static int
Mingming Cao617ba132006-10-11 01:20:53 -0700762ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
763 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764{
765 struct super_block *sb = inode->i_sb;
766 int error;
767
768 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
769 i->name_index, i->name, i->value, (long)i->value_len);
770
Mingming Cao617ba132006-10-11 01:20:53 -0700771 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700773 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774 error = -EIO;
775 if (!bs->bh)
776 goto cleanup;
777 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
778 atomic_read(&(bs->bh->b_count)),
779 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400780 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400781 EXT4_ERROR_INODE(inode, "bad block %llu",
782 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400783 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784 goto cleanup;
785 }
786 /* Find the named attribute. */
787 bs->s.base = BHDR(bs->bh);
788 bs->s.first = BFIRST(bs->bh);
789 bs->s.end = bs->bh->b_data + bs->bh->b_size;
790 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700791 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 i->name, bs->bh->b_size, 1);
793 if (error && error != -ENODATA)
794 goto cleanup;
795 bs->s.not_found = error;
796 }
797 error = 0;
798
799cleanup:
800 return error;
801}
802
803static int
Mingming Cao617ba132006-10-11 01:20:53 -0700804ext4_xattr_block_set(handle_t *handle, struct inode *inode,
805 struct ext4_xattr_info *i,
806 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807{
808 struct super_block *sb = inode->i_sb;
809 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700810 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500811 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800812 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500813 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814
Mingming Cao617ba132006-10-11 01:20:53 -0700815#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816
817 if (i->value && i->value_len > sb->s_blocksize)
818 return -ENOSPC;
819 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400820 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800821 error = ext4_journal_get_write_access(handle, bs->bh);
822 if (error)
823 goto cleanup;
824 lock_buffer(bs->bh);
825
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500827 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
828
829 /*
830 * This must happen under buffer lock for
831 * ext4_xattr_block_set() to reliably detect modified
832 * block
833 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500834 mb_cache_entry_delete_block(ext4_mb_cache, hash,
835 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700837 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 if (!error) {
839 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700840 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 }
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400843 ext4_xattr_block_csum_set(inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400845 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 goto bad_block;
847 if (!error)
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400848 error = ext4_handle_dirty_metadata(handle,
849 inode,
850 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 if (error)
852 goto cleanup;
853 goto inserted;
854 } else {
855 int offset = (char *)s->here - bs->bh->b_data;
856
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800857 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400859 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 error = -ENOMEM;
861 if (s->base == NULL)
862 goto cleanup;
863 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
864 s->first = ENTRY(header(s->base)+1);
865 header(s->base)->h_refcount = cpu_to_le32(1);
866 s->here = ENTRY(s->base + offset);
867 s->end = s->base + bs->bh->b_size;
868 }
869 } else {
870 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400871 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 /* assert(header == s->base) */
873 error = -ENOMEM;
874 if (s->base == NULL)
875 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700876 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 header(s->base)->h_blocks = cpu_to_le32(1);
878 header(s->base)->h_refcount = cpu_to_le32(1);
879 s->first = ENTRY(header(s->base)+1);
880 s->here = ENTRY(header(s->base)+1);
881 s->end = s->base + sb->s_blocksize;
882 }
883
Mingming Cao617ba132006-10-11 01:20:53 -0700884 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400885 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 goto bad_block;
887 if (error)
888 goto cleanup;
889 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700890 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891
892inserted:
893 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700894 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 if (new_bh) {
896 /* We found an identical block in the cache. */
897 if (new_bh == bs->bh)
898 ea_bdebug(new_bh, "keeping");
899 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500900 u32 ref;
901
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 /* The old block is released after updating
903 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500904 error = dquot_alloc_block(inode,
905 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500906 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400908 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700909 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 new_bh);
911 if (error)
912 goto cleanup_dquot;
913 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500914 /*
915 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500916 * freeing, rehashing or adding references to
917 * xattr block. Once we hold buffer lock xattr
918 * block's state is stable so we can check
919 * whether the block got freed / rehashed or
920 * not. Since we unhash mbcache entry under
921 * buffer lock when freeing / rehashing xattr
922 * block, checking whether entry is still
923 * hashed is reliable. Same rules hold for
924 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500925 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500926 if (hlist_bl_unhashed(&ce->e_hash_list) ||
927 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500928 /*
929 * Undo everything and check mbcache
930 * again.
931 */
932 unlock_buffer(new_bh);
933 dquot_free_block(inode,
934 EXT4_C2B(EXT4_SB(sb),
935 1));
936 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500937 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500938 ce = NULL;
939 new_bh = NULL;
940 goto inserted;
941 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500942 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
943 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
944 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
945 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500947 ref);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400948 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 unlock_buffer(new_bh);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -0400950 error = ext4_handle_dirty_metadata(handle,
951 inode,
952 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 if (error)
954 goto cleanup_dquot;
955 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500956 mb_cache_entry_touch(ext4_mb_cache, ce);
957 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 ce = NULL;
959 } else if (bs->bh && s->base == bs->bh->b_data) {
960 /* We were modifying this block in-place. */
961 ea_bdebug(bs->bh, "keeping this block");
Tahsin Erdoganb496b242017-08-05 22:41:42 -0400962 ext4_xattr_cache_insert(ext4_mb_cache, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 new_bh = bs->bh;
964 get_bh(new_bh);
965 } else {
966 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400967 ext4_fsblk_t goal, block;
968
969 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400970 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400971
972 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400973 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400974 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
975
Allison Henderson55f020d2011-05-25 07:41:26 -0400976 block = ext4_new_meta_blocks(handle, inode, goal, 0,
977 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 if (error)
979 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400980
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400981 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400982 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
983
Joe Perchesace36ad2012-03-19 23:11:43 -0400984 ea_idebug(inode, "creating block %llu",
985 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986
987 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500988 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500989 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700990getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500991 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500992 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 goto cleanup;
994 }
995 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700996 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700997 if (error) {
998 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500999 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 goto getblk_failed;
1001 }
1002 memcpy(new_bh->b_data, s->base, new_bh->b_size);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -04001003 ext4_xattr_block_csum_set(inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 set_buffer_uptodate(new_bh);
1005 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001006 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Theodore Ts'o9a9dc3ec2017-03-25 17:22:47 -04001007 error = ext4_handle_dirty_metadata(handle, inode,
1008 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 if (error)
1010 goto cleanup;
1011 }
1012 }
1013
1014 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001015 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016
1017 /* Drop the previous xattr block. */
1018 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001019 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020 error = 0;
1021
1022cleanup:
1023 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001024 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 brelse(new_bh);
1026 if (!(bs->bh && s->base == bs->bh->b_data))
1027 kfree(s->base);
1028
1029 return error;
1030
1031cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001032 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001033 goto cleanup;
1034
1035bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001036 EXT4_ERROR_INODE(inode, "bad block %llu",
1037 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 goto cleanup;
1039
1040#undef header
1041}
1042
Tao Ma879b3822012-12-05 10:28:46 -05001043int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1044 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045{
Mingming Cao617ba132006-10-11 01:20:53 -07001046 struct ext4_xattr_ibody_header *header;
1047 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048 int error;
1049
Mingming Cao617ba132006-10-11 01:20:53 -07001050 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001052 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 header = IHDR(inode, raw_inode);
1054 is->s.base = is->s.first = IFIRST(header);
1055 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001056 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001057 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001058 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059 if (error)
1060 return error;
1061 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001062 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 i->name, is->s.end -
1064 (void *)is->s.base, 0);
1065 if (error && error != -ENODATA)
1066 return error;
1067 is->s.not_found = error;
1068 }
1069 return 0;
1070}
1071
Tao Ma0d812f72012-12-10 14:06:02 -05001072int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1073 struct ext4_xattr_info *i,
1074 struct ext4_xattr_ibody_find *is)
1075{
1076 struct ext4_xattr_ibody_header *header;
1077 struct ext4_xattr_search *s = &is->s;
1078 int error;
1079
1080 if (EXT4_I(inode)->i_extra_isize == 0)
1081 return -ENOSPC;
1082 error = ext4_xattr_set_entry(i, s);
1083 if (error) {
1084 if (error == -ENOSPC &&
1085 ext4_has_inline_data(inode)) {
1086 error = ext4_try_to_evict_inline_data(handle, inode,
1087 EXT4_XATTR_LEN(strlen(i->name) +
1088 EXT4_XATTR_SIZE(i->value_len)));
1089 if (error)
1090 return error;
1091 error = ext4_xattr_ibody_find(inode, i, is);
1092 if (error)
1093 return error;
1094 error = ext4_xattr_set_entry(i, s);
1095 }
1096 if (error)
1097 return error;
1098 }
1099 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1100 if (!IS_LAST_ENTRY(s->first)) {
1101 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1102 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1103 } else {
1104 header->h_magic = cpu_to_le32(0);
1105 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1106 }
1107 return 0;
1108}
1109
1110static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1111 struct ext4_xattr_info *i,
1112 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113{
Mingming Cao617ba132006-10-11 01:20:53 -07001114 struct ext4_xattr_ibody_header *header;
1115 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116 int error;
1117
Mingming Cao617ba132006-10-11 01:20:53 -07001118 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001120 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 if (error)
1122 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001123 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001125 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001126 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 } else {
1128 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001129 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130 }
1131 return 0;
1132}
1133
Jan Kara3fd16462016-02-22 22:43:04 -05001134static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1135 struct ext4_xattr_info *i)
1136{
1137 void *value;
1138
1139 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1140 return 0;
1141 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1142 return !memcmp(value, i->value, i->value_len);
1143}
1144
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145/*
Mingming Cao617ba132006-10-11 01:20:53 -07001146 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001147 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001148 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 * is NULL to remove an existing extended attribute, and non-NULL to
1150 * either replace an existing extended attribute, or create a new extended
1151 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1152 * specify that an extended attribute must exist and must not exist
1153 * previous to the call, respectively.
1154 *
1155 * Returns 0, or a negative error number on failure.
1156 */
1157int
Mingming Cao617ba132006-10-11 01:20:53 -07001158ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 const char *name, const void *value, size_t value_len,
1160 int flags)
1161{
Mingming Cao617ba132006-10-11 01:20:53 -07001162 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 .name_index = name_index,
1164 .name = name,
1165 .value = value,
1166 .value_len = value_len,
1167
1168 };
Mingming Cao617ba132006-10-11 01:20:53 -07001169 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 .s = { .not_found = -ENODATA, },
1171 };
Mingming Cao617ba132006-10-11 01:20:53 -07001172 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 .s = { .not_found = -ENODATA, },
1174 };
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001175 int no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 int error;
1177
1178 if (!name)
1179 return -EINVAL;
1180 if (strlen(name) > 255)
1181 return -ERANGE;
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001182 ext4_write_lock_xattr(inode, &no_expand);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001183
Eric Sandeen66543612011-10-26 03:32:07 -04001184 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001185 if (error)
1186 goto cleanup;
1187
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001188 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001189 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1190 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001191 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 }
1193
Mingming Cao617ba132006-10-11 01:20:53 -07001194 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001195 if (error)
1196 goto cleanup;
1197 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001198 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 if (error)
1200 goto cleanup;
1201 if (is.s.not_found && bs.s.not_found) {
1202 error = -ENODATA;
1203 if (flags & XATTR_REPLACE)
1204 goto cleanup;
1205 error = 0;
1206 if (!value)
1207 goto cleanup;
1208 } else {
1209 error = -EEXIST;
1210 if (flags & XATTR_CREATE)
1211 goto cleanup;
1212 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001213 if (!value) {
1214 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001215 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001217 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001219 error = 0;
1220 /* Xattr value did not change? Save us some work and bail out */
1221 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1222 goto cleanup;
1223 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1224 goto cleanup;
1225
Mingming Cao617ba132006-10-11 01:20:53 -07001226 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001227 if (!error && !bs.s.not_found) {
1228 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001229 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001231 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1232 error = ext4_xattr_block_find(inode, &i, &bs);
1233 if (error)
1234 goto cleanup;
1235 }
Mingming Cao617ba132006-10-11 01:20:53 -07001236 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237 if (error)
1238 goto cleanup;
1239 if (!is.s.not_found) {
1240 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001241 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 &is);
1243 }
1244 }
1245 }
1246 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001247 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001248 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001249 if (!value)
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001250 no_expand = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001251 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001253 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 * error != 0.
1255 */
1256 is.iloc.bh = NULL;
1257 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001258 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259 }
1260
1261cleanup:
1262 brelse(is.iloc.bh);
1263 brelse(bs.bh);
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001264 ext4_write_unlock_xattr(inode, &no_expand);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001265 return error;
1266}
1267
1268/*
Mingming Cao617ba132006-10-11 01:20:53 -07001269 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 *
Mingming Cao617ba132006-10-11 01:20:53 -07001271 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272 * attribute modification is a filesystem transaction by itself.
1273 *
1274 * Returns 0, or a negative error number on failure.
1275 */
1276int
Mingming Cao617ba132006-10-11 01:20:53 -07001277ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 const void *value, size_t value_len, int flags)
1279{
1280 handle_t *handle;
1281 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001282 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283
1284retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001285 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 if (IS_ERR(handle)) {
1287 error = PTR_ERR(handle);
1288 } else {
1289 int error2;
1290
Mingming Cao617ba132006-10-11 01:20:53 -07001291 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001293 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001295 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 goto retry;
1297 if (error == 0)
1298 error = error2;
1299 }
1300
1301 return error;
1302}
1303
1304/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001305 * Shift the EA entries in the inode to create space for the increased
1306 * i_extra_isize.
1307 */
1308static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1309 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001310 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001311{
1312 struct ext4_xattr_entry *last = entry;
1313 int new_offs;
1314
Jan Kara94405712016-08-29 15:41:11 -04001315 /* We always shift xattr headers further thus offsets get lower */
1316 BUG_ON(value_offs_shift > 0);
1317
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001318 /* Adjust the value offsets of the entries */
1319 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001320 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001321 new_offs = le16_to_cpu(last->e_value_offs) +
1322 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001323 last->e_value_offs = cpu_to_le16(new_offs);
1324 }
1325 }
1326 /* Shift the entries by n bytes */
1327 memmove(to, from, n);
1328}
1329
1330/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001331 * Move xattr pointed to by 'entry' from inode into external xattr block
1332 */
1333static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1334 struct ext4_inode *raw_inode,
1335 struct ext4_xattr_entry *entry)
1336{
1337 struct ext4_xattr_ibody_find *is = NULL;
1338 struct ext4_xattr_block_find *bs = NULL;
1339 char *buffer = NULL, *b_entry_name = NULL;
1340 size_t value_offs, value_size;
1341 struct ext4_xattr_info i = {
1342 .value = NULL,
1343 .value_len = 0,
1344 .name_index = entry->e_name_index,
1345 };
1346 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1347 int error;
1348
1349 value_offs = le16_to_cpu(entry->e_value_offs);
1350 value_size = le32_to_cpu(entry->e_value_size);
1351
1352 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1353 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1354 buffer = kmalloc(value_size, GFP_NOFS);
1355 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1356 if (!is || !bs || !buffer || !b_entry_name) {
1357 error = -ENOMEM;
1358 goto out;
1359 }
1360
1361 is->s.not_found = -ENODATA;
1362 bs->s.not_found = -ENODATA;
1363 is->iloc.bh = NULL;
1364 bs->bh = NULL;
1365
1366 /* Save the entry name and the entry value */
1367 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1368 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1369 b_entry_name[entry->e_name_len] = '\0';
1370 i.name = b_entry_name;
1371
1372 error = ext4_get_inode_loc(inode, &is->iloc);
1373 if (error)
1374 goto out;
1375
1376 error = ext4_xattr_ibody_find(inode, &i, is);
1377 if (error)
1378 goto out;
1379
1380 /* Remove the chosen entry from the inode */
1381 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1382 if (error)
1383 goto out;
1384
1385 i.name = b_entry_name;
1386 i.value = buffer;
1387 i.value_len = value_size;
1388 error = ext4_xattr_block_find(inode, &i, bs);
1389 if (error)
1390 goto out;
1391
1392 /* Add entry which was removed from the inode into the block */
1393 error = ext4_xattr_block_set(handle, inode, &i, bs);
1394 if (error)
1395 goto out;
1396 error = 0;
1397out:
1398 kfree(b_entry_name);
1399 kfree(buffer);
1400 if (is)
1401 brelse(is->iloc.bh);
1402 kfree(is);
1403 kfree(bs);
1404
1405 return error;
1406}
1407
Jan Karadfa20642016-08-29 15:44:11 -04001408static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1409 struct ext4_inode *raw_inode,
1410 int isize_diff, size_t ifree,
1411 size_t bfree, int *total_ino)
1412{
1413 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1414 struct ext4_xattr_entry *small_entry;
1415 struct ext4_xattr_entry *entry;
1416 struct ext4_xattr_entry *last;
1417 unsigned int entry_size; /* EA entry size */
1418 unsigned int total_size; /* EA entry size + value size */
1419 unsigned int min_total_size;
1420 int error;
1421
1422 while (isize_diff > ifree) {
1423 entry = NULL;
1424 small_entry = NULL;
1425 min_total_size = ~0U;
1426 last = IFIRST(header);
1427 /* Find the entry best suited to be pushed into EA block */
1428 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Theodore Ts'o3a282472018-06-16 15:40:48 -04001429 /* never move system.data out of the inode */
1430 if ((last->e_name_len == 4) &&
1431 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
1432 !memcmp(last->e_name, "data", 4))
1433 continue;
Jan Karadfa20642016-08-29 15:44:11 -04001434 total_size =
1435 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1436 EXT4_XATTR_LEN(last->e_name_len);
1437 if (total_size <= bfree &&
1438 total_size < min_total_size) {
1439 if (total_size + ifree < isize_diff) {
1440 small_entry = last;
1441 } else {
1442 entry = last;
1443 min_total_size = total_size;
1444 }
1445 }
1446 }
1447
1448 if (entry == NULL) {
1449 if (small_entry == NULL)
1450 return -ENOSPC;
1451 entry = small_entry;
1452 }
1453
1454 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1455 total_size = entry_size +
1456 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1457 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1458 entry);
1459 if (error)
1460 return error;
1461
1462 *total_ino -= entry_size;
1463 ifree += total_size;
1464 bfree -= total_size;
1465 }
1466
1467 return 0;
1468}
1469
Jan Kara3f2571c2016-08-29 15:42:11 -04001470/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001471 * Expand an inode by new_extra_isize bytes when EAs are present.
1472 * Returns 0 on success or negative error number on failure.
1473 */
1474int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1475 struct ext4_inode *raw_inode, handle_t *handle)
1476{
1477 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001478 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001479 size_t min_offs;
1480 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001481 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001482 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001483 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001484 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 -04001485 int isize_diff; /* How much do we need to grow i_extra_isize */
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001486 int no_expand;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001487
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001488 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1489 return 0;
1490
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001491retry:
Jan Karad0141192016-08-11 11:50:30 -04001492 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001493 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1494 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001495
1496 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001497
1498 /*
1499 * Check if enough free space is available in the inode to shift the
1500 * entries ahead by new_extra_isize.
1501 */
1502
Jan Kara6e0cd082016-08-29 15:43:11 -04001503 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001504 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1505 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001506 total_ino = sizeof(struct ext4_xattr_ibody_header);
1507
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001508 error = xattr_check_inode(inode, header, end);
1509 if (error)
1510 goto cleanup;
1511
Jan Kara6e0cd082016-08-29 15:43:11 -04001512 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001513 if (ifree >= isize_diff)
1514 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001515
1516 /*
1517 * Enough free space isn't available in the inode, check if
1518 * EA block can hold new_extra_isize bytes.
1519 */
1520 if (EXT4_I(inode)->i_file_acl) {
1521 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1522 error = -EIO;
1523 if (!bh)
1524 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001525 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001526 EXT4_ERROR_INODE(inode, "bad block %llu",
1527 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001528 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001529 goto cleanup;
1530 }
1531 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001532 end = bh->b_data + bh->b_size;
1533 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001534 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1535 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001536 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001537 if (!tried_min_extra_isize && s_min_extra_isize) {
1538 tried_min_extra_isize++;
1539 new_extra_isize = s_min_extra_isize;
1540 brelse(bh);
1541 goto retry;
1542 }
Jan Karadfa20642016-08-29 15:44:11 -04001543 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001544 goto cleanup;
1545 }
1546 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001547 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001548 }
1549
Jan Karadfa20642016-08-29 15:44:11 -04001550 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1551 isize_diff, ifree, bfree,
1552 &total_ino);
1553 if (error) {
1554 if (error == -ENOSPC && !tried_min_extra_isize &&
1555 s_min_extra_isize) {
1556 tried_min_extra_isize++;
1557 new_extra_isize = s_min_extra_isize;
1558 brelse(bh);
1559 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001560 }
Jan Karadfa20642016-08-29 15:44:11 -04001561 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001562 }
Jan Karae3014d12016-08-29 15:38:11 -04001563shift:
1564 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001565 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001566 - new_extra_isize, (void *)raw_inode +
1567 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001568 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001569 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001570 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001571out:
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001572 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001573 return 0;
1574
1575cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001576 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001577 /*
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001578 * Inode size expansion failed; don't try again
Jan Kara2e81a4e2016-08-11 12:38:55 -04001579 */
Theodore Ts'oda1e4022017-01-11 21:50:46 -05001580 no_expand = 1;
1581 ext4_write_unlock_xattr(inode, &no_expand);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001582 return error;
1583}
1584
1585
1586
1587/*
Mingming Cao617ba132006-10-11 01:20:53 -07001588 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 *
1590 * Free extended attribute resources associated with this inode. This
1591 * is called immediately before an inode is freed. We have exclusive
1592 * access to the inode.
1593 */
1594void
Mingming Cao617ba132006-10-11 01:20:53 -07001595ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596{
1597 struct buffer_head *bh = NULL;
1598
Mingming Cao617ba132006-10-11 01:20:53 -07001599 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001600 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001601 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001603 EXT4_ERROR_INODE(inode, "block %llu read error",
1604 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 goto cleanup;
1606 }
Mingming Cao617ba132006-10-11 01:20:53 -07001607 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001609 EXT4_ERROR_INODE(inode, "bad block %llu",
1610 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 goto cleanup;
1612 }
Mingming Cao617ba132006-10-11 01:20:53 -07001613 ext4_xattr_release_block(handle, inode, bh);
1614 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615
1616cleanup:
1617 brelse(bh);
1618}
1619
1620/*
Mingming Cao617ba132006-10-11 01:20:53 -07001621 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622 *
1623 * Create a new entry in the extended attribute cache, and insert
1624 * it unless such an entry is already in the cache.
1625 *
1626 * Returns 0, or a negative error number on failure.
1627 */
1628static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001629ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001631 struct ext4_xattr_header *header = BHDR(bh);
1632 __u32 hash = le32_to_cpu(header->h_hash);
1633 int reusable = le32_to_cpu(header->h_refcount) <
1634 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001635 int error;
1636
Jan Kara7a2508e2016-02-22 22:35:22 -05001637 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001638 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001640 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001642 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001644}
1645
1646/*
Mingming Cao617ba132006-10-11 01:20:53 -07001647 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001648 *
1649 * Compare two extended attribute blocks for equality.
1650 *
1651 * Returns 0 if the blocks are equal, 1 if they differ, and
1652 * a negative error number on errors.
1653 */
1654static int
Mingming Cao617ba132006-10-11 01:20:53 -07001655ext4_xattr_cmp(struct ext4_xattr_header *header1,
1656 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657{
Mingming Cao617ba132006-10-11 01:20:53 -07001658 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659
1660 entry1 = ENTRY(header1+1);
1661 entry2 = ENTRY(header2+1);
1662 while (!IS_LAST_ENTRY(entry1)) {
1663 if (IS_LAST_ENTRY(entry2))
1664 return 1;
1665 if (entry1->e_hash != entry2->e_hash ||
1666 entry1->e_name_index != entry2->e_name_index ||
1667 entry1->e_name_len != entry2->e_name_len ||
1668 entry1->e_value_size != entry2->e_value_size ||
1669 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1670 return 1;
1671 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001672 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1674 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1675 le32_to_cpu(entry1->e_value_size)))
1676 return 1;
1677
Mingming Cao617ba132006-10-11 01:20:53 -07001678 entry1 = EXT4_XATTR_NEXT(entry1);
1679 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001680 }
1681 if (!IS_LAST_ENTRY(entry2))
1682 return 1;
1683 return 0;
1684}
1685
1686/*
Mingming Cao617ba132006-10-11 01:20:53 -07001687 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688 *
1689 * Find an identical extended attribute block.
1690 *
1691 * Returns a pointer to the block found, or NULL if such a block was
1692 * not found or an error occurred.
1693 */
1694static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001695ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001696 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697{
1698 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001699 struct mb_cache_entry *ce;
1700 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701
1702 if (!header->h_hash)
1703 return NULL; /* never share */
1704 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001705 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706 while (ce) {
1707 struct buffer_head *bh;
1708
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 bh = sb_bread(inode->i_sb, ce->e_block);
1710 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001711 EXT4_ERROR_INODE(inode, "block %lu read error",
1712 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001713 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 *pce = ce;
1715 return bh;
1716 }
1717 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001718 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 }
1720 return NULL;
1721}
1722
1723#define NAME_HASH_SHIFT 5
1724#define VALUE_HASH_SHIFT 16
1725
1726/*
Mingming Cao617ba132006-10-11 01:20:53 -07001727 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728 *
1729 * Compute the hash of an extended attribute.
1730 */
Mingming Cao617ba132006-10-11 01:20:53 -07001731static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1732 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733{
1734 __u32 hash = 0;
1735 char *name = entry->e_name;
1736 int n;
1737
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001738 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001739 hash = (hash << NAME_HASH_SHIFT) ^
1740 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1741 *name++;
1742 }
1743
Jan Kara1cba4232016-08-29 15:40:11 -04001744 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 __le32 *value = (__le32 *)((char *)header +
1746 le16_to_cpu(entry->e_value_offs));
1747 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001748 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001749 hash = (hash << VALUE_HASH_SHIFT) ^
1750 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1751 le32_to_cpu(*value++);
1752 }
1753 }
1754 entry->e_hash = cpu_to_le32(hash);
1755}
1756
1757#undef NAME_HASH_SHIFT
1758#undef VALUE_HASH_SHIFT
1759
1760#define BLOCK_HASH_SHIFT 16
1761
1762/*
Mingming Cao617ba132006-10-11 01:20:53 -07001763 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001764 *
1765 * Re-compute the extended attribute hash value after an entry has changed.
1766 */
Mingming Cao617ba132006-10-11 01:20:53 -07001767static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1768 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769{
Mingming Cao617ba132006-10-11 01:20:53 -07001770 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 __u32 hash = 0;
1772
Mingming Cao617ba132006-10-11 01:20:53 -07001773 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 here = ENTRY(header+1);
1775 while (!IS_LAST_ENTRY(here)) {
1776 if (!here->e_hash) {
1777 /* Block is not shared if an entry's hash value == 0 */
1778 hash = 0;
1779 break;
1780 }
1781 hash = (hash << BLOCK_HASH_SHIFT) ^
1782 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1783 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001784 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001785 }
1786 header->h_hash = cpu_to_le32(hash);
1787}
1788
1789#undef BLOCK_HASH_SHIFT
1790
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001791#define HASH_BUCKET_BITS 10
1792
Jan Kara7a2508e2016-02-22 22:35:22 -05001793struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001794ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795{
Jan Kara7a2508e2016-02-22 22:35:22 -05001796 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001797}
1798
Jan Kara7a2508e2016-02-22 22:35:22 -05001799void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001801 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001802 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001804