blob: 59c9ec7eabaee5d408db87ce871a18a49d0ce849 [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,
134 sector_t block_nr,
135 struct ext4_xattr_header *hdr)
136{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400137 if (ext4_has_metadata_csum(inode->i_sb) &&
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400138 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
139 return 0;
140 return 1;
141}
142
143static void ext4_xattr_block_csum_set(struct inode *inode,
144 sector_t block_nr,
145 struct ext4_xattr_header *hdr)
146{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400147 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400148 return;
149
150 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
151}
152
153static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
154 struct inode *inode,
155 struct buffer_head *bh)
156{
157 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
158 return ext4_handle_dirty_metadata(handle, inode, bh);
159}
160
Stephen Hemminger11e27522010-05-13 17:53:18 -0700161static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700162ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700164 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165
Mingming Cao617ba132006-10-11 01:20:53 -0700166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 return handler;
169}
170
171/*
172 * Inode operation listxattr()
173 *
David Howells2b0143b2015-03-17 22:25:59 +0000174 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175 */
176ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700177ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000179 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180}
181
182static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400183ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
184 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400186 struct ext4_xattr_entry *e = entry;
187
188 while (!IS_LAST_ENTRY(e)) {
189 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400191 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400192 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400194
195 while (!IS_LAST_ENTRY(entry)) {
Jan Kara2de58f12016-08-29 15:39:11 -0400196 if (entry->e_value_block != 0)
197 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400198 if (entry->e_value_size != 0 &&
199 (value_start + le16_to_cpu(entry->e_value_offs) <
200 (void *)e + sizeof(__u32) ||
201 value_start + le16_to_cpu(entry->e_value_offs) +
202 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400203 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400204 entry = EXT4_XATTR_NEXT(entry);
205 }
206
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 return 0;
208}
209
210static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400211ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400213 int error;
214
215 if (buffer_verified(bh))
216 return 0;
217
Mingming Cao617ba132006-10-11 01:20:53 -0700218 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400220 return -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400221 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400222 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400223 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
224 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400225 if (!error)
226 set_buffer_verified(bh);
227 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228}
229
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400230static int
231__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
232 void *end, const char *function, unsigned int line)
233{
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400234 int error = -EFSCORRUPTED;
235
Eric Biggers290ab232016-12-01 14:51:58 -0500236 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
Eric Biggers19962502016-10-15 09:39:31 -0400237 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400238 goto errout;
Eric Biggers290ab232016-12-01 14:51:58 -0500239 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400240errout:
241 if (error)
242 __ext4_error_inode(inode, function, line, 0,
243 "corrupted in-inode xattr");
244 return error;
245}
246
247#define xattr_check_inode(inode, header, end) \
248 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
249
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700251ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252{
253 size_t value_size = le32_to_cpu(entry->e_value_size);
254
255 if (entry->e_value_block != 0 || value_size > size ||
256 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400257 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258 return 0;
259}
260
261static int
Mingming Cao617ba132006-10-11 01:20:53 -0700262ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 const char *name, size_t size, int sorted)
264{
Mingming Cao617ba132006-10-11 01:20:53 -0700265 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 size_t name_len;
267 int cmp = 1;
268
269 if (name == NULL)
270 return -EINVAL;
271 name_len = strlen(name);
272 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700273 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 cmp = name_index - entry->e_name_index;
275 if (!cmp)
276 cmp = name_len - entry->e_name_len;
277 if (!cmp)
278 cmp = memcmp(name, entry->e_name, name_len);
279 if (cmp <= 0 && (sorted || cmp == 0))
280 break;
281 }
282 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700283 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400284 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 return cmp ? -ENODATA : 0;
286}
287
288static int
Mingming Cao617ba132006-10-11 01:20:53 -0700289ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 void *buffer, size_t buffer_size)
291{
292 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700293 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 size_t size;
295 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500296 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297
298 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
299 name_index, name, buffer, (long)buffer_size);
300
301 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700302 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400304 ea_idebug(inode, "reading block %llu",
305 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700306 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307 if (!bh)
308 goto cleanup;
309 ea_bdebug(bh, "b_count=%d, refcount=%d",
310 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400311 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500312bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400313 EXT4_ERROR_INODE(inode, "bad block %llu",
314 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400315 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 goto cleanup;
317 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400318 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700320 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400321 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700322 goto bad_block;
323 if (error)
324 goto cleanup;
325 size = le32_to_cpu(entry->e_value_size);
326 if (buffer) {
327 error = -ERANGE;
328 if (size > buffer_size)
329 goto cleanup;
330 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
331 size);
332 }
333 error = size;
334
335cleanup:
336 brelse(bh);
337 return error;
338}
339
Tao Ma879b3822012-12-05 10:28:46 -0500340int
Mingming Cao617ba132006-10-11 01:20:53 -0700341ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342 void *buffer, size_t buffer_size)
343{
Mingming Cao617ba132006-10-11 01:20:53 -0700344 struct ext4_xattr_ibody_header *header;
345 struct ext4_xattr_entry *entry;
346 struct ext4_inode *raw_inode;
347 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348 size_t size;
349 void *end;
350 int error;
351
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500352 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700354 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 if (error)
356 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700357 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 header = IHDR(inode, raw_inode);
359 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700360 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400361 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 if (error)
363 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700364 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 end - (void *)entry, 0);
366 if (error)
367 goto cleanup;
368 size = le32_to_cpu(entry->e_value_size);
369 if (buffer) {
370 error = -ERANGE;
371 if (size > buffer_size)
372 goto cleanup;
373 memcpy(buffer, (void *)IFIRST(header) +
374 le16_to_cpu(entry->e_value_offs), size);
375 }
376 error = size;
377
378cleanup:
379 brelse(iloc.bh);
380 return error;
381}
382
383/*
Mingming Cao617ba132006-10-11 01:20:53 -0700384 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700385 *
386 * Copy an extended attribute into the buffer
387 * provided, or compute the buffer size required.
388 * Buffer is NULL to compute the size of the buffer required.
389 *
390 * Returns a negative error number on failure, or the number of bytes
391 * used / required on success.
392 */
393int
Mingming Cao617ba132006-10-11 01:20:53 -0700394ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700395 void *buffer, size_t buffer_size)
396{
397 int error;
398
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400399 if (strlen(name) > 255)
400 return -ERANGE;
401
Mingming Cao617ba132006-10-11 01:20:53 -0700402 down_read(&EXT4_I(inode)->xattr_sem);
403 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404 buffer_size);
405 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700406 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700408 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 return error;
410}
411
412static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000413ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 char *buffer, size_t buffer_size)
415{
416 size_t rest = buffer_size;
417
Mingming Cao617ba132006-10-11 01:20:53 -0700418 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700419 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700420 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100422 if (handler && (!handler->list || handler->list(dentry))) {
423 const char *prefix = handler->prefix ?: handler->name;
424 size_t prefix_len = strlen(prefix);
425 size_t size = prefix_len + entry->e_name_len + 1;
426
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427 if (buffer) {
428 if (size > rest)
429 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100430 memcpy(buffer, prefix, prefix_len);
431 buffer += prefix_len;
432 memcpy(buffer, entry->e_name, entry->e_name_len);
433 buffer += entry->e_name_len;
434 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 }
436 rest -= size;
437 }
438 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100439 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440}
441
442static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000443ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444{
David Howells2b0143b2015-03-17 22:25:59 +0000445 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 struct buffer_head *bh = NULL;
447 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500448 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449
450 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
451 buffer, (long)buffer_size);
452
453 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700454 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400456 ea_idebug(inode, "reading block %llu",
457 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700458 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459 error = -EIO;
460 if (!bh)
461 goto cleanup;
462 ea_bdebug(bh, "b_count=%d, refcount=%d",
463 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400464 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400465 EXT4_ERROR_INODE(inode, "bad block %llu",
466 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400467 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 goto cleanup;
469 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400470 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000471 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700472
473cleanup:
474 brelse(bh);
475
476 return error;
477}
478
479static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000480ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481{
David Howells2b0143b2015-03-17 22:25:59 +0000482 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700483 struct ext4_xattr_ibody_header *header;
484 struct ext4_inode *raw_inode;
485 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 void *end;
487 int error;
488
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500489 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700491 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 if (error)
493 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700494 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700496 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400497 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 if (error)
499 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000500 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501 buffer, buffer_size);
502
503cleanup:
504 brelse(iloc.bh);
505 return error;
506}
507
508/*
Mingming Cao617ba132006-10-11 01:20:53 -0700509 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 *
511 * Copy a list of attribute names into the buffer
512 * provided, or compute the buffer size required.
513 * Buffer is NULL to compute the size of the buffer required.
514 *
515 * Returns a negative error number on failure, or the number of bytes
516 * used / required on success.
517 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400518static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000519ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500521 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522
David Howells2b0143b2015-03-17 22:25:59 +0000523 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500524 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
525 if (ret < 0)
526 goto errout;
527 if (buffer) {
528 buffer += ret;
529 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500531 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
532 if (ret < 0)
533 goto errout;
534 ret += ret2;
535errout:
David Howells2b0143b2015-03-17 22:25:59 +0000536 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500537 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538}
539
540/*
Mingming Cao617ba132006-10-11 01:20:53 -0700541 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542 * not set, set it.
543 */
Mingming Cao617ba132006-10-11 01:20:53 -0700544static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 struct super_block *sb)
546{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400547 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 return;
549
liang xie5d601252014-05-12 22:06:43 -0400550 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700551 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400552 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400553 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555}
556
557/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400558 * Release the xattr block BH: If the reference count is > 1, decrement it;
559 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 */
561static void
Mingming Cao617ba132006-10-11 01:20:53 -0700562ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 struct buffer_head *bh)
564{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500565 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
566 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800567 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568
liang xie5d601252014-05-12 22:06:43 -0400569 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800570 error = ext4_journal_get_write_access(handle, bh);
571 if (error)
572 goto out;
573
574 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500575 hash = le32_to_cpu(BHDR(bh)->h_hash);
576 ref = le32_to_cpu(BHDR(bh)->h_refcount);
577 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500579 /*
580 * This must happen under buffer lock for
581 * ext4_xattr_block_set() to reliably detect freed block
582 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500583 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400585 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500586 ext4_free_blocks(handle, inode, bh, 0, 1,
587 EXT4_FREE_BLOCKS_METADATA |
588 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500590 ref--;
591 BHDR(bh)->h_refcount = cpu_to_le32(ref);
592 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
593 struct mb_cache_entry *ce;
594
595 ce = mb_cache_entry_get(ext4_mb_cache, hash,
596 bh->b_blocknr);
597 if (ce) {
598 ce->e_reusable = 1;
599 mb_cache_entry_put(ext4_mb_cache, ce);
600 }
601 }
602
Jan Karaec4cb1a2014-04-07 10:54:21 -0400603 /*
604 * Beware of this ugliness: Releasing of xattr block references
605 * from different inodes can race and so we have to protect
606 * from a race where someone else frees the block (and releases
607 * its journal_head) before we are done dirtying the buffer. In
608 * nojournal mode this race is harmless and we actually cannot
609 * call ext4_handle_dirty_xattr_block() with locked buffer as
610 * that function can call sync_dirty_buffer() so for that case
611 * we handle the dirtying after unlocking the buffer.
612 */
613 if (ext4_handle_valid(handle))
614 error = ext4_handle_dirty_xattr_block(handle, inode,
615 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500616 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400617 if (!ext4_handle_valid(handle))
618 error = ext4_handle_dirty_xattr_block(handle, inode,
619 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800620 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500621 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500622 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800623 ea_bdebug(bh, "refcount now=%d; releasing",
624 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800626out:
627 ext4_std_error(inode->i_sb, error);
628 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629}
630
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400631/*
632 * Find the available free space for EAs. This also returns the total number of
633 * bytes used by EA entries.
634 */
635static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
636 size_t *min_offs, void *base, int *total)
637{
638 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400639 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400640 size_t offs = le16_to_cpu(last->e_value_offs);
641 if (offs < *min_offs)
642 *min_offs = offs;
643 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500644 if (total)
645 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400646 }
647 return (*min_offs - ((void *)last - base) - sizeof(__u32));
648}
649
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650static int
Mingming Cao617ba132006-10-11 01:20:53 -0700651ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652{
Mingming Cao617ba132006-10-11 01:20:53 -0700653 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
655
656 /* Compute min_offs and last. */
657 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700658 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400659 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 size_t offs = le16_to_cpu(last->e_value_offs);
661 if (offs < min_offs)
662 min_offs = offs;
663 }
664 }
665 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
666 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400667 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700669 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 }
Mingming Cao617ba132006-10-11 01:20:53 -0700671 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 }
673 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400674 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700675 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 return -ENOSPC;
677 }
678
679 if (i->value && s->not_found) {
680 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700681 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
683 memmove((void *)s->here + size, s->here, rest);
684 memset(s->here, 0, size);
685 s->here->e_name_index = i->name_index;
686 s->here->e_name_len = name_len;
687 memcpy(s->here->e_name, i->name, name_len);
688 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400689 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690 void *first_val = s->base + min_offs;
691 size_t offs = le16_to_cpu(s->here->e_value_offs);
692 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700693 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694 le32_to_cpu(s->here->e_value_size));
695
Mingming Cao617ba132006-10-11 01:20:53 -0700696 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 /* The old and the new value have the same
698 size. Just replace. */
699 s->here->e_value_size =
700 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500701 if (i->value == EXT4_ZERO_XATTR_VALUE) {
702 memset(val, 0, size);
703 } else {
704 /* Clear pad bytes first. */
705 memset(val + size - EXT4_XATTR_PAD, 0,
706 EXT4_XATTR_PAD);
707 memcpy(val, i->value, i->value_len);
708 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 return 0;
710 }
711
712 /* Remove the old value. */
713 memmove(first_val + size, first_val, val - first_val);
714 memset(first_val, 0, size);
715 s->here->e_value_size = 0;
716 s->here->e_value_offs = 0;
717 min_offs += size;
718
719 /* Adjust all value offsets. */
720 last = s->first;
721 while (!IS_LAST_ENTRY(last)) {
722 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400723 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 last->e_value_offs =
725 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700726 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 }
728 }
729 if (!i->value) {
730 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700731 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732 last = ENTRY((void *)last - size);
733 memmove(s->here, (void *)s->here + size,
734 (void *)last - (void *)s->here + sizeof(__u32));
735 memset(last, 0, size);
736 }
737 }
738
739 if (i->value) {
740 /* Insert the new value. */
741 s->here->e_value_size = cpu_to_le32(i->value_len);
742 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700743 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700744 void *val = s->base + min_offs - size;
745 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500746 if (i->value == EXT4_ZERO_XATTR_VALUE) {
747 memset(val, 0, size);
748 } else {
749 /* Clear the pad bytes first. */
750 memset(val + size - EXT4_XATTR_PAD, 0,
751 EXT4_XATTR_PAD);
752 memcpy(val, i->value, i->value_len);
753 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 }
755 }
756 return 0;
757}
758
Mingming Cao617ba132006-10-11 01:20:53 -0700759struct ext4_xattr_block_find {
760 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 struct buffer_head *bh;
762};
763
764static int
Mingming Cao617ba132006-10-11 01:20:53 -0700765ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
766 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767{
768 struct super_block *sb = inode->i_sb;
769 int error;
770
771 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
772 i->name_index, i->name, i->value, (long)i->value_len);
773
Mingming Cao617ba132006-10-11 01:20:53 -0700774 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700776 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777 error = -EIO;
778 if (!bs->bh)
779 goto cleanup;
780 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
781 atomic_read(&(bs->bh->b_count)),
782 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400783 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400784 EXT4_ERROR_INODE(inode, "bad block %llu",
785 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400786 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 goto cleanup;
788 }
789 /* Find the named attribute. */
790 bs->s.base = BHDR(bs->bh);
791 bs->s.first = BFIRST(bs->bh);
792 bs->s.end = bs->bh->b_data + bs->bh->b_size;
793 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700794 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 i->name, bs->bh->b_size, 1);
796 if (error && error != -ENODATA)
797 goto cleanup;
798 bs->s.not_found = error;
799 }
800 error = 0;
801
802cleanup:
803 return error;
804}
805
806static int
Mingming Cao617ba132006-10-11 01:20:53 -0700807ext4_xattr_block_set(handle_t *handle, struct inode *inode,
808 struct ext4_xattr_info *i,
809 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810{
811 struct super_block *sb = inode->i_sb;
812 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700813 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500814 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800815 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500816 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817
Mingming Cao617ba132006-10-11 01:20:53 -0700818#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819
820 if (i->value && i->value_len > sb->s_blocksize)
821 return -ENOSPC;
822 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400823 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800824 error = ext4_journal_get_write_access(handle, bs->bh);
825 if (error)
826 goto cleanup;
827 lock_buffer(bs->bh);
828
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500830 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
831
832 /*
833 * This must happen under buffer lock for
834 * ext4_xattr_block_set() to reliably detect modified
835 * block
836 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500837 mb_cache_entry_delete_block(ext4_mb_cache, hash,
838 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700840 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 if (!error) {
842 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700843 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400845 ext4_xattr_cache_insert(ext4_mb_cache,
846 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847 }
848 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400849 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 goto bad_block;
851 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400852 error = ext4_handle_dirty_xattr_block(handle,
853 inode,
854 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 if (error)
856 goto cleanup;
857 goto inserted;
858 } else {
859 int offset = (char *)s->here - bs->bh->b_data;
860
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800861 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400863 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864 error = -ENOMEM;
865 if (s->base == NULL)
866 goto cleanup;
867 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
868 s->first = ENTRY(header(s->base)+1);
869 header(s->base)->h_refcount = cpu_to_le32(1);
870 s->here = ENTRY(s->base + offset);
871 s->end = s->base + bs->bh->b_size;
872 }
873 } else {
874 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400875 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 /* assert(header == s->base) */
877 error = -ENOMEM;
878 if (s->base == NULL)
879 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700880 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 header(s->base)->h_blocks = cpu_to_le32(1);
882 header(s->base)->h_refcount = cpu_to_le32(1);
883 s->first = ENTRY(header(s->base)+1);
884 s->here = ENTRY(header(s->base)+1);
885 s->end = s->base + sb->s_blocksize;
886 }
887
Mingming Cao617ba132006-10-11 01:20:53 -0700888 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400889 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700890 goto bad_block;
891 if (error)
892 goto cleanup;
893 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700894 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895
896inserted:
897 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700898 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 if (new_bh) {
900 /* We found an identical block in the cache. */
901 if (new_bh == bs->bh)
902 ea_bdebug(new_bh, "keeping");
903 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500904 u32 ref;
905
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906 /* The old block is released after updating
907 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500908 error = dquot_alloc_block(inode,
909 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500910 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400912 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700913 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914 new_bh);
915 if (error)
916 goto cleanup_dquot;
917 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500918 /*
919 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500920 * freeing, rehashing or adding references to
921 * xattr block. Once we hold buffer lock xattr
922 * block's state is stable so we can check
923 * whether the block got freed / rehashed or
924 * not. Since we unhash mbcache entry under
925 * buffer lock when freeing / rehashing xattr
926 * block, checking whether entry is still
927 * hashed is reliable. Same rules hold for
928 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500929 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500930 if (hlist_bl_unhashed(&ce->e_hash_list) ||
931 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500932 /*
933 * Undo everything and check mbcache
934 * again.
935 */
936 unlock_buffer(new_bh);
937 dquot_free_block(inode,
938 EXT4_C2B(EXT4_SB(sb),
939 1));
940 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500941 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500942 ce = NULL;
943 new_bh = NULL;
944 goto inserted;
945 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500946 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
947 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
948 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
949 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500951 ref);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400953 error = ext4_handle_dirty_xattr_block(handle,
954 inode,
955 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956 if (error)
957 goto cleanup_dquot;
958 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500959 mb_cache_entry_touch(ext4_mb_cache, ce);
960 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 ce = NULL;
962 } else if (bs->bh && s->base == bs->bh->b_data) {
963 /* We were modifying this block in-place. */
964 ea_bdebug(bs->bh, "keeping this block");
965 new_bh = bs->bh;
966 get_bh(new_bh);
967 } else {
968 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400969 ext4_fsblk_t goal, block;
970
971 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400972 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400973
974 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400975 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400976 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
977
Allison Henderson55f020d2011-05-25 07:41:26 -0400978 block = ext4_new_meta_blocks(handle, inode, goal, 0,
979 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980 if (error)
981 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400982
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400983 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400984 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
985
Joe Perchesace36ad2012-03-19 23:11:43 -0400986 ea_idebug(inode, "creating block %llu",
987 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988
989 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500990 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500991 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500993 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500994 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995 goto cleanup;
996 }
997 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700998 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999 if (error) {
1000 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001001 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 goto getblk_failed;
1003 }
1004 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1005 set_buffer_uptodate(new_bh);
1006 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001007 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001008 error = ext4_handle_dirty_xattr_block(handle,
1009 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 if (error)
1011 goto cleanup;
1012 }
1013 }
1014
1015 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001016 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001017
1018 /* Drop the previous xattr block. */
1019 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001020 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 error = 0;
1022
1023cleanup:
1024 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001025 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 brelse(new_bh);
1027 if (!(bs->bh && s->base == bs->bh->b_data))
1028 kfree(s->base);
1029
1030 return error;
1031
1032cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001033 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 goto cleanup;
1035
1036bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001037 EXT4_ERROR_INODE(inode, "bad block %llu",
1038 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039 goto cleanup;
1040
1041#undef header
1042}
1043
Tao Ma879b3822012-12-05 10:28:46 -05001044int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1045 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046{
Mingming Cao617ba132006-10-11 01:20:53 -07001047 struct ext4_xattr_ibody_header *header;
1048 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049 int error;
1050
Mingming Cao617ba132006-10-11 01:20:53 -07001051 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001053 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 header = IHDR(inode, raw_inode);
1055 is->s.base = is->s.first = IFIRST(header);
1056 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001057 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001058 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001059 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001060 if (error)
1061 return error;
1062 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001063 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 i->name, is->s.end -
1065 (void *)is->s.base, 0);
1066 if (error && error != -ENODATA)
1067 return error;
1068 is->s.not_found = error;
1069 }
1070 return 0;
1071}
1072
Tao Ma0d812f72012-12-10 14:06:02 -05001073int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1074 struct ext4_xattr_info *i,
1075 struct ext4_xattr_ibody_find *is)
1076{
1077 struct ext4_xattr_ibody_header *header;
1078 struct ext4_xattr_search *s = &is->s;
1079 int error;
1080
1081 if (EXT4_I(inode)->i_extra_isize == 0)
1082 return -ENOSPC;
1083 error = ext4_xattr_set_entry(i, s);
1084 if (error) {
1085 if (error == -ENOSPC &&
1086 ext4_has_inline_data(inode)) {
1087 error = ext4_try_to_evict_inline_data(handle, inode,
1088 EXT4_XATTR_LEN(strlen(i->name) +
1089 EXT4_XATTR_SIZE(i->value_len)));
1090 if (error)
1091 return error;
1092 error = ext4_xattr_ibody_find(inode, i, is);
1093 if (error)
1094 return error;
1095 error = ext4_xattr_set_entry(i, s);
1096 }
1097 if (error)
1098 return error;
1099 }
1100 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1101 if (!IS_LAST_ENTRY(s->first)) {
1102 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1103 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1104 } else {
1105 header->h_magic = cpu_to_le32(0);
1106 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1107 }
1108 return 0;
1109}
1110
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001111static int ext4_xattr_ibody_set(struct inode *inode,
Tao Ma0d812f72012-12-10 14:06:02 -05001112 struct ext4_xattr_info *i,
1113 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114{
Mingming Cao617ba132006-10-11 01:20:53 -07001115 struct ext4_xattr_ibody_header *header;
1116 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 int error;
1118
Mingming Cao617ba132006-10-11 01:20:53 -07001119 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001121 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001122 if (error)
1123 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001124 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001126 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001127 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 } else {
1129 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001130 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 }
1132 return 0;
1133}
1134
Jan Kara3fd16462016-02-22 22:43:04 -05001135static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1136 struct ext4_xattr_info *i)
1137{
1138 void *value;
1139
1140 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1141 return 0;
1142 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1143 return !memcmp(value, i->value, i->value_len);
1144}
1145
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146/*
Mingming Cao617ba132006-10-11 01:20:53 -07001147 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001149 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 * is NULL to remove an existing extended attribute, and non-NULL to
1151 * either replace an existing extended attribute, or create a new extended
1152 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1153 * specify that an extended attribute must exist and must not exist
1154 * previous to the call, respectively.
1155 *
1156 * Returns 0, or a negative error number on failure.
1157 */
1158int
Mingming Cao617ba132006-10-11 01:20:53 -07001159ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 const char *name, const void *value, size_t value_len,
1161 int flags)
1162{
Mingming Cao617ba132006-10-11 01:20:53 -07001163 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 .name_index = name_index,
1165 .name = name,
1166 .value = value,
1167 .value_len = value_len,
1168
1169 };
Mingming Cao617ba132006-10-11 01:20:53 -07001170 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 .s = { .not_found = -ENODATA, },
1172 };
Mingming Cao617ba132006-10-11 01:20:53 -07001173 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 .s = { .not_found = -ENODATA, },
1175 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001176 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 int error;
1178
1179 if (!name)
1180 return -EINVAL;
1181 if (strlen(name) > 255)
1182 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001183 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001184 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1185 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001186
Eric Sandeen66543612011-10-26 03:32:07 -04001187 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001188 if (error)
1189 goto cleanup;
1190
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001191 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001192 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1193 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001194 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001195 }
1196
Mingming Cao617ba132006-10-11 01:20:53 -07001197 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198 if (error)
1199 goto cleanup;
1200 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001201 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 if (error)
1203 goto cleanup;
1204 if (is.s.not_found && bs.s.not_found) {
1205 error = -ENODATA;
1206 if (flags & XATTR_REPLACE)
1207 goto cleanup;
1208 error = 0;
1209 if (!value)
1210 goto cleanup;
1211 } else {
1212 error = -EEXIST;
1213 if (flags & XATTR_CREATE)
1214 goto cleanup;
1215 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 if (!value) {
1217 if (!is.s.not_found)
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001218 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001220 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001222 error = 0;
1223 /* Xattr value did not change? Save us some work and bail out */
1224 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1225 goto cleanup;
1226 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1227 goto cleanup;
1228
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001229 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230 if (!error && !bs.s.not_found) {
1231 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001232 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001234 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1235 error = ext4_xattr_block_find(inode, &i, &bs);
1236 if (error)
1237 goto cleanup;
1238 }
Mingming Cao617ba132006-10-11 01:20:53 -07001239 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001240 if (error)
1241 goto cleanup;
1242 if (!is.s.not_found) {
1243 i.value = NULL;
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001244 error = ext4_xattr_ibody_set(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 }
1246 }
1247 }
1248 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001249 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001250 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001251 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001252 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001253 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001255 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 * error != 0.
1257 */
1258 is.iloc.bh = NULL;
1259 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001260 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001261 }
1262
1263cleanup:
1264 brelse(is.iloc.bh);
1265 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001266 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001267 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001268 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 return error;
1270}
1271
1272/*
Mingming Cao617ba132006-10-11 01:20:53 -07001273 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 *
Mingming Cao617ba132006-10-11 01:20:53 -07001275 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 * attribute modification is a filesystem transaction by itself.
1277 *
1278 * Returns 0, or a negative error number on failure.
1279 */
1280int
Mingming Cao617ba132006-10-11 01:20:53 -07001281ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 const void *value, size_t value_len, int flags)
1283{
1284 handle_t *handle;
1285 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001286 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287
1288retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001289 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290 if (IS_ERR(handle)) {
1291 error = PTR_ERR(handle);
1292 } else {
1293 int error2;
1294
Mingming Cao617ba132006-10-11 01:20:53 -07001295 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001297 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001299 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 goto retry;
1301 if (error == 0)
1302 error = error2;
1303 }
1304
1305 return error;
1306}
1307
1308/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001309 * Shift the EA entries in the inode to create space for the increased
1310 * i_extra_isize.
1311 */
1312static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1313 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001314 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001315{
1316 struct ext4_xattr_entry *last = entry;
1317 int new_offs;
1318
Jan Kara94405712016-08-29 15:41:11 -04001319 /* We always shift xattr headers further thus offsets get lower */
1320 BUG_ON(value_offs_shift > 0);
1321
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001322 /* Adjust the value offsets of the entries */
1323 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001324 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001325 new_offs = le16_to_cpu(last->e_value_offs) +
1326 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001327 last->e_value_offs = cpu_to_le16(new_offs);
1328 }
1329 }
1330 /* Shift the entries by n bytes */
1331 memmove(to, from, n);
1332}
1333
1334/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001335 * Move xattr pointed to by 'entry' from inode into external xattr block
1336 */
1337static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1338 struct ext4_inode *raw_inode,
1339 struct ext4_xattr_entry *entry)
1340{
1341 struct ext4_xattr_ibody_find *is = NULL;
1342 struct ext4_xattr_block_find *bs = NULL;
1343 char *buffer = NULL, *b_entry_name = NULL;
1344 size_t value_offs, value_size;
1345 struct ext4_xattr_info i = {
1346 .value = NULL,
1347 .value_len = 0,
1348 .name_index = entry->e_name_index,
1349 };
1350 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1351 int error;
1352
1353 value_offs = le16_to_cpu(entry->e_value_offs);
1354 value_size = le32_to_cpu(entry->e_value_size);
1355
1356 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1357 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1358 buffer = kmalloc(value_size, GFP_NOFS);
1359 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1360 if (!is || !bs || !buffer || !b_entry_name) {
1361 error = -ENOMEM;
1362 goto out;
1363 }
1364
1365 is->s.not_found = -ENODATA;
1366 bs->s.not_found = -ENODATA;
1367 is->iloc.bh = NULL;
1368 bs->bh = NULL;
1369
1370 /* Save the entry name and the entry value */
1371 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1372 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1373 b_entry_name[entry->e_name_len] = '\0';
1374 i.name = b_entry_name;
1375
1376 error = ext4_get_inode_loc(inode, &is->iloc);
1377 if (error)
1378 goto out;
1379
1380 error = ext4_xattr_ibody_find(inode, &i, is);
1381 if (error)
1382 goto out;
1383
1384 /* Remove the chosen entry from the inode */
Eric Whitneyd5c8dab2016-11-14 21:56:48 -05001385 error = ext4_xattr_ibody_set(inode, &i, is);
Jan Kara3f2571c2016-08-29 15:42:11 -04001386 if (error)
1387 goto out;
1388
1389 i.name = b_entry_name;
1390 i.value = buffer;
1391 i.value_len = value_size;
1392 error = ext4_xattr_block_find(inode, &i, bs);
1393 if (error)
1394 goto out;
1395
1396 /* Add entry which was removed from the inode into the block */
1397 error = ext4_xattr_block_set(handle, inode, &i, bs);
1398 if (error)
1399 goto out;
1400 error = 0;
1401out:
1402 kfree(b_entry_name);
1403 kfree(buffer);
1404 if (is)
1405 brelse(is->iloc.bh);
1406 kfree(is);
1407 kfree(bs);
1408
1409 return error;
1410}
1411
Jan Karadfa20642016-08-29 15:44:11 -04001412static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1413 struct ext4_inode *raw_inode,
1414 int isize_diff, size_t ifree,
1415 size_t bfree, int *total_ino)
1416{
1417 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1418 struct ext4_xattr_entry *small_entry;
1419 struct ext4_xattr_entry *entry;
1420 struct ext4_xattr_entry *last;
1421 unsigned int entry_size; /* EA entry size */
1422 unsigned int total_size; /* EA entry size + value size */
1423 unsigned int min_total_size;
1424 int error;
1425
1426 while (isize_diff > ifree) {
1427 entry = NULL;
1428 small_entry = NULL;
1429 min_total_size = ~0U;
1430 last = IFIRST(header);
1431 /* Find the entry best suited to be pushed into EA block */
1432 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1433 total_size =
1434 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1435 EXT4_XATTR_LEN(last->e_name_len);
1436 if (total_size <= bfree &&
1437 total_size < min_total_size) {
1438 if (total_size + ifree < isize_diff) {
1439 small_entry = last;
1440 } else {
1441 entry = last;
1442 min_total_size = total_size;
1443 }
1444 }
1445 }
1446
1447 if (entry == NULL) {
1448 if (small_entry == NULL)
1449 return -ENOSPC;
1450 entry = small_entry;
1451 }
1452
1453 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1454 total_size = entry_size +
1455 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1456 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1457 entry);
1458 if (error)
1459 return error;
1460
1461 *total_ino -= entry_size;
1462 ifree += total_size;
1463 bfree -= total_size;
1464 }
1465
1466 return 0;
1467}
1468
Jan Kara3f2571c2016-08-29 15:42:11 -04001469/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001470 * Expand an inode by new_extra_isize bytes when EAs are present.
1471 * Returns 0 on success or negative error number on failure.
1472 */
1473int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1474 struct ext4_inode *raw_inode, handle_t *handle)
1475{
1476 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001477 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001478 size_t min_offs;
1479 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001480 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001481 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001482 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001483 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 -04001484 int isize_diff; /* How much do we need to grow i_extra_isize */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001485
1486 down_write(&EXT4_I(inode)->xattr_sem);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001487 /*
1488 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1489 */
1490 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
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:
1572 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001573 up_write(&EXT4_I(inode)->xattr_sem);
1574 return 0;
1575
1576cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001577 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001578 /*
1579 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1580 * size expansion failed.
1581 */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001582 up_write(&EXT4_I(inode)->xattr_sem);
1583 return error;
1584}
1585
1586
1587
1588/*
Mingming Cao617ba132006-10-11 01:20:53 -07001589 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001590 *
1591 * Free extended attribute resources associated with this inode. This
1592 * is called immediately before an inode is freed. We have exclusive
1593 * access to the inode.
1594 */
1595void
Mingming Cao617ba132006-10-11 01:20:53 -07001596ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597{
1598 struct buffer_head *bh = NULL;
1599
Mingming Cao617ba132006-10-11 01:20:53 -07001600 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001602 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001604 EXT4_ERROR_INODE(inode, "block %llu read error",
1605 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 goto cleanup;
1607 }
Mingming Cao617ba132006-10-11 01:20:53 -07001608 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001610 EXT4_ERROR_INODE(inode, "bad block %llu",
1611 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612 goto cleanup;
1613 }
Mingming Cao617ba132006-10-11 01:20:53 -07001614 ext4_xattr_release_block(handle, inode, bh);
1615 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616
1617cleanup:
1618 brelse(bh);
1619}
1620
1621/*
Mingming Cao617ba132006-10-11 01:20:53 -07001622 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623 *
1624 * Create a new entry in the extended attribute cache, and insert
1625 * it unless such an entry is already in the cache.
1626 *
1627 * Returns 0, or a negative error number on failure.
1628 */
1629static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001630ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001632 struct ext4_xattr_header *header = BHDR(bh);
1633 __u32 hash = le32_to_cpu(header->h_hash);
1634 int reusable = le32_to_cpu(header->h_refcount) <
1635 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001636 int error;
1637
Jan Kara7a2508e2016-02-22 22:35:22 -05001638 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001639 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001641 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001643 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001644 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645}
1646
1647/*
Mingming Cao617ba132006-10-11 01:20:53 -07001648 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 *
1650 * Compare two extended attribute blocks for equality.
1651 *
1652 * Returns 0 if the blocks are equal, 1 if they differ, and
1653 * a negative error number on errors.
1654 */
1655static int
Mingming Cao617ba132006-10-11 01:20:53 -07001656ext4_xattr_cmp(struct ext4_xattr_header *header1,
1657 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658{
Mingming Cao617ba132006-10-11 01:20:53 -07001659 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001660
1661 entry1 = ENTRY(header1+1);
1662 entry2 = ENTRY(header2+1);
1663 while (!IS_LAST_ENTRY(entry1)) {
1664 if (IS_LAST_ENTRY(entry2))
1665 return 1;
1666 if (entry1->e_hash != entry2->e_hash ||
1667 entry1->e_name_index != entry2->e_name_index ||
1668 entry1->e_name_len != entry2->e_name_len ||
1669 entry1->e_value_size != entry2->e_value_size ||
1670 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1671 return 1;
1672 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001673 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1675 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1676 le32_to_cpu(entry1->e_value_size)))
1677 return 1;
1678
Mingming Cao617ba132006-10-11 01:20:53 -07001679 entry1 = EXT4_XATTR_NEXT(entry1);
1680 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681 }
1682 if (!IS_LAST_ENTRY(entry2))
1683 return 1;
1684 return 0;
1685}
1686
1687/*
Mingming Cao617ba132006-10-11 01:20:53 -07001688 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689 *
1690 * Find an identical extended attribute block.
1691 *
1692 * Returns a pointer to the block found, or NULL if such a block was
1693 * not found or an error occurred.
1694 */
1695static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001696ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001697 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698{
1699 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001700 struct mb_cache_entry *ce;
1701 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001702
1703 if (!header->h_hash)
1704 return NULL; /* never share */
1705 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001706 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707 while (ce) {
1708 struct buffer_head *bh;
1709
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710 bh = sb_bread(inode->i_sb, ce->e_block);
1711 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001712 EXT4_ERROR_INODE(inode, "block %lu read error",
1713 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001714 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001715 *pce = ce;
1716 return bh;
1717 }
1718 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001719 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 }
1721 return NULL;
1722}
1723
1724#define NAME_HASH_SHIFT 5
1725#define VALUE_HASH_SHIFT 16
1726
1727/*
Mingming Cao617ba132006-10-11 01:20:53 -07001728 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 *
1730 * Compute the hash of an extended attribute.
1731 */
Mingming Cao617ba132006-10-11 01:20:53 -07001732static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1733 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001734{
1735 __u32 hash = 0;
1736 char *name = entry->e_name;
1737 int n;
1738
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001739 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001740 hash = (hash << NAME_HASH_SHIFT) ^
1741 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1742 *name++;
1743 }
1744
Jan Kara1cba4232016-08-29 15:40:11 -04001745 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746 __le32 *value = (__le32 *)((char *)header +
1747 le16_to_cpu(entry->e_value_offs));
1748 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001749 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750 hash = (hash << VALUE_HASH_SHIFT) ^
1751 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1752 le32_to_cpu(*value++);
1753 }
1754 }
1755 entry->e_hash = cpu_to_le32(hash);
1756}
1757
1758#undef NAME_HASH_SHIFT
1759#undef VALUE_HASH_SHIFT
1760
1761#define BLOCK_HASH_SHIFT 16
1762
1763/*
Mingming Cao617ba132006-10-11 01:20:53 -07001764 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765 *
1766 * Re-compute the extended attribute hash value after an entry has changed.
1767 */
Mingming Cao617ba132006-10-11 01:20:53 -07001768static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1769 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770{
Mingming Cao617ba132006-10-11 01:20:53 -07001771 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772 __u32 hash = 0;
1773
Mingming Cao617ba132006-10-11 01:20:53 -07001774 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001775 here = ENTRY(header+1);
1776 while (!IS_LAST_ENTRY(here)) {
1777 if (!here->e_hash) {
1778 /* Block is not shared if an entry's hash value == 0 */
1779 hash = 0;
1780 break;
1781 }
1782 hash = (hash << BLOCK_HASH_SHIFT) ^
1783 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1784 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001785 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001786 }
1787 header->h_hash = cpu_to_le32(hash);
1788}
1789
1790#undef BLOCK_HASH_SHIFT
1791
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001792#define HASH_BUCKET_BITS 10
1793
Jan Kara7a2508e2016-02-22 22:35:22 -05001794struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001795ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001796{
Jan Kara7a2508e2016-02-22 22:35:22 -05001797 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798}
1799
Jan Kara7a2508e2016-02-22 22:35:22 -05001800void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001802 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001803 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001804}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001805