blob: 9c1bb5cd89d20ec66cad3f03a26490eedbe38c0e [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{
234 struct ext4_xattr_entry *entry = IFIRST(header);
235 int error = -EFSCORRUPTED;
236
237 if (((void *) header >= end) ||
Eric Biggers19962502016-10-15 09:39:31 -0400238 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400239 goto errout;
240 error = ext4_xattr_check_names(entry, end, entry);
241errout:
242 if (error)
243 __ext4_error_inode(inode, function, line, 0,
244 "corrupted in-inode xattr");
245 return error;
246}
247
248#define xattr_check_inode(inode, header, end) \
249 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
250
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700252ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253{
254 size_t value_size = le32_to_cpu(entry->e_value_size);
255
256 if (entry->e_value_block != 0 || value_size > size ||
257 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400258 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 return 0;
260}
261
262static int
Mingming Cao617ba132006-10-11 01:20:53 -0700263ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 const char *name, size_t size, int sorted)
265{
Mingming Cao617ba132006-10-11 01:20:53 -0700266 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 size_t name_len;
268 int cmp = 1;
269
270 if (name == NULL)
271 return -EINVAL;
272 name_len = strlen(name);
273 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700274 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 cmp = name_index - entry->e_name_index;
276 if (!cmp)
277 cmp = name_len - entry->e_name_len;
278 if (!cmp)
279 cmp = memcmp(name, entry->e_name, name_len);
280 if (cmp <= 0 && (sorted || cmp == 0))
281 break;
282 }
283 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700284 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400285 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 return cmp ? -ENODATA : 0;
287}
288
289static int
Mingming Cao617ba132006-10-11 01:20:53 -0700290ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 void *buffer, size_t buffer_size)
292{
293 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700294 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 size_t size;
296 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500297 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298
299 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
300 name_index, name, buffer, (long)buffer_size);
301
302 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700303 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400305 ea_idebug(inode, "reading block %llu",
306 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700307 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 if (!bh)
309 goto cleanup;
310 ea_bdebug(bh, "b_count=%d, refcount=%d",
311 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400312 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500313bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400314 EXT4_ERROR_INODE(inode, "bad block %llu",
315 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400316 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317 goto cleanup;
318 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400319 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700321 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400322 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 goto bad_block;
324 if (error)
325 goto cleanup;
326 size = le32_to_cpu(entry->e_value_size);
327 if (buffer) {
328 error = -ERANGE;
329 if (size > buffer_size)
330 goto cleanup;
331 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
332 size);
333 }
334 error = size;
335
336cleanup:
337 brelse(bh);
338 return error;
339}
340
Tao Ma879b3822012-12-05 10:28:46 -0500341int
Mingming Cao617ba132006-10-11 01:20:53 -0700342ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 void *buffer, size_t buffer_size)
344{
Mingming Cao617ba132006-10-11 01:20:53 -0700345 struct ext4_xattr_ibody_header *header;
346 struct ext4_xattr_entry *entry;
347 struct ext4_inode *raw_inode;
348 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 size_t size;
350 void *end;
351 int error;
352
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500353 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700355 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356 if (error)
357 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700358 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359 header = IHDR(inode, raw_inode);
360 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700361 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400362 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 if (error)
364 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700365 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 end - (void *)entry, 0);
367 if (error)
368 goto cleanup;
369 size = le32_to_cpu(entry->e_value_size);
370 if (buffer) {
371 error = -ERANGE;
372 if (size > buffer_size)
373 goto cleanup;
374 memcpy(buffer, (void *)IFIRST(header) +
375 le16_to_cpu(entry->e_value_offs), size);
376 }
377 error = size;
378
379cleanup:
380 brelse(iloc.bh);
381 return error;
382}
383
384/*
Mingming Cao617ba132006-10-11 01:20:53 -0700385 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700386 *
387 * Copy an extended attribute into the buffer
388 * provided, or compute the buffer size required.
389 * Buffer is NULL to compute the size of the buffer required.
390 *
391 * Returns a negative error number on failure, or the number of bytes
392 * used / required on success.
393 */
394int
Mingming Cao617ba132006-10-11 01:20:53 -0700395ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 void *buffer, size_t buffer_size)
397{
398 int error;
399
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400400 if (strlen(name) > 255)
401 return -ERANGE;
402
Mingming Cao617ba132006-10-11 01:20:53 -0700403 down_read(&EXT4_I(inode)->xattr_sem);
404 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 buffer_size);
406 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700407 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700409 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 return error;
411}
412
413static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000414ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415 char *buffer, size_t buffer_size)
416{
417 size_t rest = buffer_size;
418
Mingming Cao617ba132006-10-11 01:20:53 -0700419 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700420 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700421 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100423 if (handler && (!handler->list || handler->list(dentry))) {
424 const char *prefix = handler->prefix ?: handler->name;
425 size_t prefix_len = strlen(prefix);
426 size_t size = prefix_len + entry->e_name_len + 1;
427
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 if (buffer) {
429 if (size > rest)
430 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100431 memcpy(buffer, prefix, prefix_len);
432 buffer += prefix_len;
433 memcpy(buffer, entry->e_name, entry->e_name_len);
434 buffer += entry->e_name_len;
435 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 }
437 rest -= size;
438 }
439 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100440 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441}
442
443static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000444ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445{
David Howells2b0143b2015-03-17 22:25:59 +0000446 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 struct buffer_head *bh = NULL;
448 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500449 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450
451 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
452 buffer, (long)buffer_size);
453
454 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700455 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400457 ea_idebug(inode, "reading block %llu",
458 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700459 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 error = -EIO;
461 if (!bh)
462 goto cleanup;
463 ea_bdebug(bh, "b_count=%d, refcount=%d",
464 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400465 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400466 EXT4_ERROR_INODE(inode, "bad block %llu",
467 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400468 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469 goto cleanup;
470 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400471 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000472 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473
474cleanup:
475 brelse(bh);
476
477 return error;
478}
479
480static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000481ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482{
David Howells2b0143b2015-03-17 22:25:59 +0000483 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700484 struct ext4_xattr_ibody_header *header;
485 struct ext4_inode *raw_inode;
486 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700487 void *end;
488 int error;
489
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500490 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700492 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 if (error)
494 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700495 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700497 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400498 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 if (error)
500 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000501 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502 buffer, buffer_size);
503
504cleanup:
505 brelse(iloc.bh);
506 return error;
507}
508
509/*
Mingming Cao617ba132006-10-11 01:20:53 -0700510 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 *
512 * Copy a list of attribute names into the buffer
513 * provided, or compute the buffer size required.
514 * Buffer is NULL to compute the size of the buffer required.
515 *
516 * Returns a negative error number on failure, or the number of bytes
517 * used / required on success.
518 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400519static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000520ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500522 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523
David Howells2b0143b2015-03-17 22:25:59 +0000524 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500525 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
526 if (ret < 0)
527 goto errout;
528 if (buffer) {
529 buffer += ret;
530 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500532 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
533 if (ret < 0)
534 goto errout;
535 ret += ret2;
536errout:
David Howells2b0143b2015-03-17 22:25:59 +0000537 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500538 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539}
540
541/*
Mingming Cao617ba132006-10-11 01:20:53 -0700542 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 * not set, set it.
544 */
Mingming Cao617ba132006-10-11 01:20:53 -0700545static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 struct super_block *sb)
547{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400548 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 return;
550
liang xie5d601252014-05-12 22:06:43 -0400551 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700552 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400553 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400554 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556}
557
558/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400559 * Release the xattr block BH: If the reference count is > 1, decrement it;
560 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 */
562static void
Mingming Cao617ba132006-10-11 01:20:53 -0700563ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 struct buffer_head *bh)
565{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500566 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
567 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800568 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569
liang xie5d601252014-05-12 22:06:43 -0400570 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800571 error = ext4_journal_get_write_access(handle, bh);
572 if (error)
573 goto out;
574
575 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500576 hash = le32_to_cpu(BHDR(bh)->h_hash);
577 ref = le32_to_cpu(BHDR(bh)->h_refcount);
578 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500580 /*
581 * This must happen under buffer lock for
582 * ext4_xattr_block_set() to reliably detect freed block
583 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500584 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400586 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500587 ext4_free_blocks(handle, inode, bh, 0, 1,
588 EXT4_FREE_BLOCKS_METADATA |
589 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500591 ref--;
592 BHDR(bh)->h_refcount = cpu_to_le32(ref);
593 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
594 struct mb_cache_entry *ce;
595
596 ce = mb_cache_entry_get(ext4_mb_cache, hash,
597 bh->b_blocknr);
598 if (ce) {
599 ce->e_reusable = 1;
600 mb_cache_entry_put(ext4_mb_cache, ce);
601 }
602 }
603
Jan Karaec4cb1a2014-04-07 10:54:21 -0400604 /*
605 * Beware of this ugliness: Releasing of xattr block references
606 * from different inodes can race and so we have to protect
607 * from a race where someone else frees the block (and releases
608 * its journal_head) before we are done dirtying the buffer. In
609 * nojournal mode this race is harmless and we actually cannot
610 * call ext4_handle_dirty_xattr_block() with locked buffer as
611 * that function can call sync_dirty_buffer() so for that case
612 * we handle the dirtying after unlocking the buffer.
613 */
614 if (ext4_handle_valid(handle))
615 error = ext4_handle_dirty_xattr_block(handle, inode,
616 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500617 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400618 if (!ext4_handle_valid(handle))
619 error = ext4_handle_dirty_xattr_block(handle, inode,
620 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800621 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500622 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500623 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800624 ea_bdebug(bh, "refcount now=%d; releasing",
625 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800627out:
628 ext4_std_error(inode->i_sb, error);
629 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630}
631
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400632/*
633 * Find the available free space for EAs. This also returns the total number of
634 * bytes used by EA entries.
635 */
636static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
637 size_t *min_offs, void *base, int *total)
638{
639 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400640 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400641 size_t offs = le16_to_cpu(last->e_value_offs);
642 if (offs < *min_offs)
643 *min_offs = offs;
644 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500645 if (total)
646 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400647 }
648 return (*min_offs - ((void *)last - base) - sizeof(__u32));
649}
650
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651static int
Mingming Cao617ba132006-10-11 01:20:53 -0700652ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653{
Mingming Cao617ba132006-10-11 01:20:53 -0700654 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
656
657 /* Compute min_offs and last. */
658 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700659 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400660 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 size_t offs = le16_to_cpu(last->e_value_offs);
662 if (offs < min_offs)
663 min_offs = offs;
664 }
665 }
666 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
667 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400668 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700670 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 }
Mingming Cao617ba132006-10-11 01:20:53 -0700672 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 }
674 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400675 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700676 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 return -ENOSPC;
678 }
679
680 if (i->value && s->not_found) {
681 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700682 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
684 memmove((void *)s->here + size, s->here, rest);
685 memset(s->here, 0, size);
686 s->here->e_name_index = i->name_index;
687 s->here->e_name_len = name_len;
688 memcpy(s->here->e_name, i->name, name_len);
689 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400690 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 void *first_val = s->base + min_offs;
692 size_t offs = le16_to_cpu(s->here->e_value_offs);
693 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700694 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695 le32_to_cpu(s->here->e_value_size));
696
Mingming Cao617ba132006-10-11 01:20:53 -0700697 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 /* The old and the new value have the same
699 size. Just replace. */
700 s->here->e_value_size =
701 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500702 if (i->value == EXT4_ZERO_XATTR_VALUE) {
703 memset(val, 0, size);
704 } else {
705 /* Clear pad bytes first. */
706 memset(val + size - EXT4_XATTR_PAD, 0,
707 EXT4_XATTR_PAD);
708 memcpy(val, i->value, i->value_len);
709 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 return 0;
711 }
712
713 /* Remove the old value. */
714 memmove(first_val + size, first_val, val - first_val);
715 memset(first_val, 0, size);
716 s->here->e_value_size = 0;
717 s->here->e_value_offs = 0;
718 min_offs += size;
719
720 /* Adjust all value offsets. */
721 last = s->first;
722 while (!IS_LAST_ENTRY(last)) {
723 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400724 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 last->e_value_offs =
726 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700727 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 }
729 }
730 if (!i->value) {
731 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700732 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 last = ENTRY((void *)last - size);
734 memmove(s->here, (void *)s->here + size,
735 (void *)last - (void *)s->here + sizeof(__u32));
736 memset(last, 0, size);
737 }
738 }
739
740 if (i->value) {
741 /* Insert the new value. */
742 s->here->e_value_size = cpu_to_le32(i->value_len);
743 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700744 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 void *val = s->base + min_offs - size;
746 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500747 if (i->value == EXT4_ZERO_XATTR_VALUE) {
748 memset(val, 0, size);
749 } else {
750 /* Clear the pad bytes first. */
751 memset(val + size - EXT4_XATTR_PAD, 0,
752 EXT4_XATTR_PAD);
753 memcpy(val, i->value, i->value_len);
754 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755 }
756 }
757 return 0;
758}
759
Mingming Cao617ba132006-10-11 01:20:53 -0700760struct ext4_xattr_block_find {
761 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 struct buffer_head *bh;
763};
764
765static int
Mingming Cao617ba132006-10-11 01:20:53 -0700766ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
767 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768{
769 struct super_block *sb = inode->i_sb;
770 int error;
771
772 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
773 i->name_index, i->name, i->value, (long)i->value_len);
774
Mingming Cao617ba132006-10-11 01:20:53 -0700775 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700777 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778 error = -EIO;
779 if (!bs->bh)
780 goto cleanup;
781 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
782 atomic_read(&(bs->bh->b_count)),
783 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400784 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400785 EXT4_ERROR_INODE(inode, "bad block %llu",
786 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400787 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 goto cleanup;
789 }
790 /* Find the named attribute. */
791 bs->s.base = BHDR(bs->bh);
792 bs->s.first = BFIRST(bs->bh);
793 bs->s.end = bs->bh->b_data + bs->bh->b_size;
794 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700795 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 i->name, bs->bh->b_size, 1);
797 if (error && error != -ENODATA)
798 goto cleanup;
799 bs->s.not_found = error;
800 }
801 error = 0;
802
803cleanup:
804 return error;
805}
806
807static int
Mingming Cao617ba132006-10-11 01:20:53 -0700808ext4_xattr_block_set(handle_t *handle, struct inode *inode,
809 struct ext4_xattr_info *i,
810 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811{
812 struct super_block *sb = inode->i_sb;
813 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700814 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500815 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800816 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500817 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818
Mingming Cao617ba132006-10-11 01:20:53 -0700819#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700820
821 if (i->value && i->value_len > sb->s_blocksize)
822 return -ENOSPC;
823 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400824 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800825 error = ext4_journal_get_write_access(handle, bs->bh);
826 if (error)
827 goto cleanup;
828 lock_buffer(bs->bh);
829
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500831 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
832
833 /*
834 * This must happen under buffer lock for
835 * ext4_xattr_block_set() to reliably detect modified
836 * block
837 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500838 mb_cache_entry_delete_block(ext4_mb_cache, hash,
839 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700840 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700841 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 if (!error) {
843 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700844 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400846 ext4_xattr_cache_insert(ext4_mb_cache,
847 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 }
849 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400850 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 goto bad_block;
852 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400853 error = ext4_handle_dirty_xattr_block(handle,
854 inode,
855 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 if (error)
857 goto cleanup;
858 goto inserted;
859 } else {
860 int offset = (char *)s->here - bs->bh->b_data;
861
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800862 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400864 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 error = -ENOMEM;
866 if (s->base == NULL)
867 goto cleanup;
868 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
869 s->first = ENTRY(header(s->base)+1);
870 header(s->base)->h_refcount = cpu_to_le32(1);
871 s->here = ENTRY(s->base + offset);
872 s->end = s->base + bs->bh->b_size;
873 }
874 } else {
875 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400876 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 /* assert(header == s->base) */
878 error = -ENOMEM;
879 if (s->base == NULL)
880 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700881 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 header(s->base)->h_blocks = cpu_to_le32(1);
883 header(s->base)->h_refcount = cpu_to_le32(1);
884 s->first = ENTRY(header(s->base)+1);
885 s->here = ENTRY(header(s->base)+1);
886 s->end = s->base + sb->s_blocksize;
887 }
888
Mingming Cao617ba132006-10-11 01:20:53 -0700889 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400890 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 goto bad_block;
892 if (error)
893 goto cleanup;
894 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700895 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896
897inserted:
898 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700899 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900 if (new_bh) {
901 /* We found an identical block in the cache. */
902 if (new_bh == bs->bh)
903 ea_bdebug(new_bh, "keeping");
904 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500905 u32 ref;
906
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 /* The old block is released after updating
908 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500909 error = dquot_alloc_block(inode,
910 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500911 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400913 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700914 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 new_bh);
916 if (error)
917 goto cleanup_dquot;
918 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500919 /*
920 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500921 * freeing, rehashing or adding references to
922 * xattr block. Once we hold buffer lock xattr
923 * block's state is stable so we can check
924 * whether the block got freed / rehashed or
925 * not. Since we unhash mbcache entry under
926 * buffer lock when freeing / rehashing xattr
927 * block, checking whether entry is still
928 * hashed is reliable. Same rules hold for
929 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500930 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500931 if (hlist_bl_unhashed(&ce->e_hash_list) ||
932 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500933 /*
934 * Undo everything and check mbcache
935 * again.
936 */
937 unlock_buffer(new_bh);
938 dquot_free_block(inode,
939 EXT4_C2B(EXT4_SB(sb),
940 1));
941 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500942 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500943 ce = NULL;
944 new_bh = NULL;
945 goto inserted;
946 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500947 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
948 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
949 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
950 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500952 ref);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400954 error = ext4_handle_dirty_xattr_block(handle,
955 inode,
956 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 if (error)
958 goto cleanup_dquot;
959 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500960 mb_cache_entry_touch(ext4_mb_cache, ce);
961 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 ce = NULL;
963 } else if (bs->bh && s->base == bs->bh->b_data) {
964 /* We were modifying this block in-place. */
965 ea_bdebug(bs->bh, "keeping this block");
966 new_bh = bs->bh;
967 get_bh(new_bh);
968 } else {
969 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400970 ext4_fsblk_t goal, block;
971
972 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400973 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400974
975 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400976 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400977 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
978
Allison Henderson55f020d2011-05-25 07:41:26 -0400979 block = ext4_new_meta_blocks(handle, inode, goal, 0,
980 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 if (error)
982 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400983
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400984 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400985 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
986
Joe Perchesace36ad2012-03-19 23:11:43 -0400987 ea_idebug(inode, "creating block %llu",
988 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989
990 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500991 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500992 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500994 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500995 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 goto cleanup;
997 }
998 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700999 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 if (error) {
1001 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001002 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 goto getblk_failed;
1004 }
1005 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1006 set_buffer_uptodate(new_bh);
1007 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001008 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001009 error = ext4_handle_dirty_xattr_block(handle,
1010 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011 if (error)
1012 goto cleanup;
1013 }
1014 }
1015
1016 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001017 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018
1019 /* Drop the previous xattr block. */
1020 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001021 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001022 error = 0;
1023
1024cleanup:
1025 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001026 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001027 brelse(new_bh);
1028 if (!(bs->bh && s->base == bs->bh->b_data))
1029 kfree(s->base);
1030
1031 return error;
1032
1033cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001034 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 goto cleanup;
1036
1037bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001038 EXT4_ERROR_INODE(inode, "bad block %llu",
1039 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 goto cleanup;
1041
1042#undef header
1043}
1044
Tao Ma879b3822012-12-05 10:28:46 -05001045int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1046 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047{
Mingming Cao617ba132006-10-11 01:20:53 -07001048 struct ext4_xattr_ibody_header *header;
1049 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 int error;
1051
Mingming Cao617ba132006-10-11 01:20:53 -07001052 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001054 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 header = IHDR(inode, raw_inode);
1056 is->s.base = is->s.first = IFIRST(header);
1057 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001058 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001059 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001060 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 if (error)
1062 return error;
1063 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001064 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 i->name, is->s.end -
1066 (void *)is->s.base, 0);
1067 if (error && error != -ENODATA)
1068 return error;
1069 is->s.not_found = error;
1070 }
1071 return 0;
1072}
1073
Tao Ma0d812f72012-12-10 14:06:02 -05001074int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1075 struct ext4_xattr_info *i,
1076 struct ext4_xattr_ibody_find *is)
1077{
1078 struct ext4_xattr_ibody_header *header;
1079 struct ext4_xattr_search *s = &is->s;
1080 int error;
1081
1082 if (EXT4_I(inode)->i_extra_isize == 0)
1083 return -ENOSPC;
1084 error = ext4_xattr_set_entry(i, s);
1085 if (error) {
1086 if (error == -ENOSPC &&
1087 ext4_has_inline_data(inode)) {
1088 error = ext4_try_to_evict_inline_data(handle, inode,
1089 EXT4_XATTR_LEN(strlen(i->name) +
1090 EXT4_XATTR_SIZE(i->value_len)));
1091 if (error)
1092 return error;
1093 error = ext4_xattr_ibody_find(inode, i, is);
1094 if (error)
1095 return error;
1096 error = ext4_xattr_set_entry(i, s);
1097 }
1098 if (error)
1099 return error;
1100 }
1101 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1102 if (!IS_LAST_ENTRY(s->first)) {
1103 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1104 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1105 } else {
1106 header->h_magic = cpu_to_le32(0);
1107 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1108 }
1109 return 0;
1110}
1111
1112static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1113 struct ext4_xattr_info *i,
1114 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115{
Mingming Cao617ba132006-10-11 01:20:53 -07001116 struct ext4_xattr_ibody_header *header;
1117 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 int error;
1119
Mingming Cao617ba132006-10-11 01:20:53 -07001120 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001122 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 if (error)
1124 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001125 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001127 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001128 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 } else {
1130 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001131 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 }
1133 return 0;
1134}
1135
Jan Kara3fd16462016-02-22 22:43:04 -05001136static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1137 struct ext4_xattr_info *i)
1138{
1139 void *value;
1140
1141 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1142 return 0;
1143 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1144 return !memcmp(value, i->value, i->value_len);
1145}
1146
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001147/*
Mingming Cao617ba132006-10-11 01:20:53 -07001148 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001150 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001151 * is NULL to remove an existing extended attribute, and non-NULL to
1152 * either replace an existing extended attribute, or create a new extended
1153 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1154 * specify that an extended attribute must exist and must not exist
1155 * previous to the call, respectively.
1156 *
1157 * Returns 0, or a negative error number on failure.
1158 */
1159int
Mingming Cao617ba132006-10-11 01:20:53 -07001160ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 const char *name, const void *value, size_t value_len,
1162 int flags)
1163{
Mingming Cao617ba132006-10-11 01:20:53 -07001164 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 .name_index = name_index,
1166 .name = name,
1167 .value = value,
1168 .value_len = value_len,
1169
1170 };
Mingming Cao617ba132006-10-11 01:20:53 -07001171 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 .s = { .not_found = -ENODATA, },
1173 };
Mingming Cao617ba132006-10-11 01:20:53 -07001174 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 .s = { .not_found = -ENODATA, },
1176 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001177 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 int error;
1179
1180 if (!name)
1181 return -EINVAL;
1182 if (strlen(name) > 255)
1183 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001184 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001185 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1186 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001187
Eric Sandeen66543612011-10-26 03:32:07 -04001188 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001189 if (error)
1190 goto cleanup;
1191
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001192 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001193 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1194 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001195 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 }
1197
Mingming Cao617ba132006-10-11 01:20:53 -07001198 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 if (error)
1200 goto cleanup;
1201 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001202 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203 if (error)
1204 goto cleanup;
1205 if (is.s.not_found && bs.s.not_found) {
1206 error = -ENODATA;
1207 if (flags & XATTR_REPLACE)
1208 goto cleanup;
1209 error = 0;
1210 if (!value)
1211 goto cleanup;
1212 } else {
1213 error = -EEXIST;
1214 if (flags & XATTR_CREATE)
1215 goto cleanup;
1216 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001217 if (!value) {
1218 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001219 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001221 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001223 error = 0;
1224 /* Xattr value did not change? Save us some work and bail out */
1225 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1226 goto cleanup;
1227 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1228 goto cleanup;
1229
Mingming Cao617ba132006-10-11 01:20:53 -07001230 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231 if (!error && !bs.s.not_found) {
1232 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001233 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001235 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1236 error = ext4_xattr_block_find(inode, &i, &bs);
1237 if (error)
1238 goto cleanup;
1239 }
Mingming Cao617ba132006-10-11 01:20:53 -07001240 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 if (error)
1242 goto cleanup;
1243 if (!is.s.not_found) {
1244 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001245 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 &is);
1247 }
1248 }
1249 }
1250 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001251 ext4_xattr_update_super_block(handle, inode->i_sb);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001252 inode->i_ctime = current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001253 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001254 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001255 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001257 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 * error != 0.
1259 */
1260 is.iloc.bh = NULL;
1261 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001262 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 }
1264
1265cleanup:
1266 brelse(is.iloc.bh);
1267 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001268 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001269 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001270 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 return error;
1272}
1273
1274/*
Mingming Cao617ba132006-10-11 01:20:53 -07001275 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 *
Mingming Cao617ba132006-10-11 01:20:53 -07001277 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 * attribute modification is a filesystem transaction by itself.
1279 *
1280 * Returns 0, or a negative error number on failure.
1281 */
1282int
Mingming Cao617ba132006-10-11 01:20:53 -07001283ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 const void *value, size_t value_len, int flags)
1285{
1286 handle_t *handle;
1287 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001288 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001289
1290retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001291 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 if (IS_ERR(handle)) {
1293 error = PTR_ERR(handle);
1294 } else {
1295 int error2;
1296
Mingming Cao617ba132006-10-11 01:20:53 -07001297 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001299 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001301 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 goto retry;
1303 if (error == 0)
1304 error = error2;
1305 }
1306
1307 return error;
1308}
1309
1310/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001311 * Shift the EA entries in the inode to create space for the increased
1312 * i_extra_isize.
1313 */
1314static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1315 int value_offs_shift, void *to,
Jan Kara94405712016-08-29 15:41:11 -04001316 void *from, size_t n)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001317{
1318 struct ext4_xattr_entry *last = entry;
1319 int new_offs;
1320
Jan Kara94405712016-08-29 15:41:11 -04001321 /* We always shift xattr headers further thus offsets get lower */
1322 BUG_ON(value_offs_shift > 0);
1323
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001324 /* Adjust the value offsets of the entries */
1325 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001326 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001327 new_offs = le16_to_cpu(last->e_value_offs) +
1328 value_offs_shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001329 last->e_value_offs = cpu_to_le16(new_offs);
1330 }
1331 }
1332 /* Shift the entries by n bytes */
1333 memmove(to, from, n);
1334}
1335
1336/*
Jan Kara3f2571c2016-08-29 15:42:11 -04001337 * Move xattr pointed to by 'entry' from inode into external xattr block
1338 */
1339static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1340 struct ext4_inode *raw_inode,
1341 struct ext4_xattr_entry *entry)
1342{
1343 struct ext4_xattr_ibody_find *is = NULL;
1344 struct ext4_xattr_block_find *bs = NULL;
1345 char *buffer = NULL, *b_entry_name = NULL;
1346 size_t value_offs, value_size;
1347 struct ext4_xattr_info i = {
1348 .value = NULL,
1349 .value_len = 0,
1350 .name_index = entry->e_name_index,
1351 };
1352 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1353 int error;
1354
1355 value_offs = le16_to_cpu(entry->e_value_offs);
1356 value_size = le32_to_cpu(entry->e_value_size);
1357
1358 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1359 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1360 buffer = kmalloc(value_size, GFP_NOFS);
1361 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1362 if (!is || !bs || !buffer || !b_entry_name) {
1363 error = -ENOMEM;
1364 goto out;
1365 }
1366
1367 is->s.not_found = -ENODATA;
1368 bs->s.not_found = -ENODATA;
1369 is->iloc.bh = NULL;
1370 bs->bh = NULL;
1371
1372 /* Save the entry name and the entry value */
1373 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1374 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1375 b_entry_name[entry->e_name_len] = '\0';
1376 i.name = b_entry_name;
1377
1378 error = ext4_get_inode_loc(inode, &is->iloc);
1379 if (error)
1380 goto out;
1381
1382 error = ext4_xattr_ibody_find(inode, &i, is);
1383 if (error)
1384 goto out;
1385
1386 /* Remove the chosen entry from the inode */
1387 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1388 if (error)
1389 goto out;
1390
1391 i.name = b_entry_name;
1392 i.value = buffer;
1393 i.value_len = value_size;
1394 error = ext4_xattr_block_find(inode, &i, bs);
1395 if (error)
1396 goto out;
1397
1398 /* Add entry which was removed from the inode into the block */
1399 error = ext4_xattr_block_set(handle, inode, &i, bs);
1400 if (error)
1401 goto out;
1402 error = 0;
1403out:
1404 kfree(b_entry_name);
1405 kfree(buffer);
1406 if (is)
1407 brelse(is->iloc.bh);
1408 kfree(is);
1409 kfree(bs);
1410
1411 return error;
1412}
1413
Jan Karadfa20642016-08-29 15:44:11 -04001414static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1415 struct ext4_inode *raw_inode,
1416 int isize_diff, size_t ifree,
1417 size_t bfree, int *total_ino)
1418{
1419 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1420 struct ext4_xattr_entry *small_entry;
1421 struct ext4_xattr_entry *entry;
1422 struct ext4_xattr_entry *last;
1423 unsigned int entry_size; /* EA entry size */
1424 unsigned int total_size; /* EA entry size + value size */
1425 unsigned int min_total_size;
1426 int error;
1427
1428 while (isize_diff > ifree) {
1429 entry = NULL;
1430 small_entry = NULL;
1431 min_total_size = ~0U;
1432 last = IFIRST(header);
1433 /* Find the entry best suited to be pushed into EA block */
1434 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1435 total_size =
1436 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1437 EXT4_XATTR_LEN(last->e_name_len);
1438 if (total_size <= bfree &&
1439 total_size < min_total_size) {
1440 if (total_size + ifree < isize_diff) {
1441 small_entry = last;
1442 } else {
1443 entry = last;
1444 min_total_size = total_size;
1445 }
1446 }
1447 }
1448
1449 if (entry == NULL) {
1450 if (small_entry == NULL)
1451 return -ENOSPC;
1452 entry = small_entry;
1453 }
1454
1455 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1456 total_size = entry_size +
1457 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1458 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1459 entry);
1460 if (error)
1461 return error;
1462
1463 *total_ino -= entry_size;
1464 ifree += total_size;
1465 bfree -= total_size;
1466 }
1467
1468 return 0;
1469}
1470
Jan Kara3f2571c2016-08-29 15:42:11 -04001471/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001472 * Expand an inode by new_extra_isize bytes when EAs are present.
1473 * Returns 0 on success or negative error number on failure.
1474 */
1475int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1476 struct ext4_inode *raw_inode, handle_t *handle)
1477{
1478 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001479 struct buffer_head *bh = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001480 size_t min_offs;
1481 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001482 int total_ino;
Jan Kara6e0cd082016-08-29 15:43:11 -04001483 void *base, *end;
Jan Karad0141192016-08-11 11:50:30 -04001484 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001485 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 -04001486 int isize_diff; /* How much do we need to grow i_extra_isize */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001487
1488 down_write(&EXT4_I(inode)->xattr_sem);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001489 /*
1490 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1491 */
1492 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001493retry:
Jan Karad0141192016-08-11 11:50:30 -04001494 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001495 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1496 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001497
1498 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001499
1500 /*
1501 * Check if enough free space is available in the inode to shift the
1502 * entries ahead by new_extra_isize.
1503 */
1504
Jan Kara6e0cd082016-08-29 15:43:11 -04001505 base = IFIRST(header);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001506 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1507 min_offs = end - base;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001508 total_ino = sizeof(struct ext4_xattr_ibody_header);
1509
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001510 error = xattr_check_inode(inode, header, end);
1511 if (error)
1512 goto cleanup;
1513
Jan Kara6e0cd082016-08-29 15:43:11 -04001514 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001515 if (ifree >= isize_diff)
1516 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001517
1518 /*
1519 * Enough free space isn't available in the inode, check if
1520 * EA block can hold new_extra_isize bytes.
1521 */
1522 if (EXT4_I(inode)->i_file_acl) {
1523 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1524 error = -EIO;
1525 if (!bh)
1526 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001527 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001528 EXT4_ERROR_INODE(inode, "bad block %llu",
1529 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001530 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001531 goto cleanup;
1532 }
1533 base = BHDR(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001534 end = bh->b_data + bh->b_size;
1535 min_offs = end - base;
Jan Kara6e0cd082016-08-29 15:43:11 -04001536 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1537 NULL);
Jan Karae3014d12016-08-29 15:38:11 -04001538 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001539 if (!tried_min_extra_isize && s_min_extra_isize) {
1540 tried_min_extra_isize++;
1541 new_extra_isize = s_min_extra_isize;
1542 brelse(bh);
1543 goto retry;
1544 }
Jan Karadfa20642016-08-29 15:44:11 -04001545 error = -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001546 goto cleanup;
1547 }
1548 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001549 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001550 }
1551
Jan Karadfa20642016-08-29 15:44:11 -04001552 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1553 isize_diff, ifree, bfree,
1554 &total_ino);
1555 if (error) {
1556 if (error == -ENOSPC && !tried_min_extra_isize &&
1557 s_min_extra_isize) {
1558 tried_min_extra_isize++;
1559 new_extra_isize = s_min_extra_isize;
1560 brelse(bh);
1561 goto retry;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001562 }
Jan Karadfa20642016-08-29 15:44:11 -04001563 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001564 }
Jan Karae3014d12016-08-29 15:38:11 -04001565shift:
1566 /* Adjust the offsets and shift the remaining entries ahead */
Jan Kara6e0cd082016-08-29 15:43:11 -04001567 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
Jan Karae3014d12016-08-29 15:38:11 -04001568 - new_extra_isize, (void *)raw_inode +
1569 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
Jan Kara94405712016-08-29 15:41:11 -04001570 (void *)header, total_ino);
Jan Karae3014d12016-08-29 15:38:11 -04001571 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001572 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001573out:
1574 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001575 up_write(&EXT4_I(inode)->xattr_sem);
1576 return 0;
1577
1578cleanup:
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001579 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001580 /*
1581 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1582 * size expansion failed.
1583 */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001584 up_write(&EXT4_I(inode)->xattr_sem);
1585 return error;
1586}
1587
1588
1589
1590/*
Mingming Cao617ba132006-10-11 01:20:53 -07001591 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592 *
1593 * Free extended attribute resources associated with this inode. This
1594 * is called immediately before an inode is freed. We have exclusive
1595 * access to the inode.
1596 */
1597void
Mingming Cao617ba132006-10-11 01:20:53 -07001598ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599{
1600 struct buffer_head *bh = NULL;
1601
Mingming Cao617ba132006-10-11 01:20:53 -07001602 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001604 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001606 EXT4_ERROR_INODE(inode, "block %llu read error",
1607 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608 goto cleanup;
1609 }
Mingming Cao617ba132006-10-11 01:20:53 -07001610 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001612 EXT4_ERROR_INODE(inode, "bad block %llu",
1613 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614 goto cleanup;
1615 }
Mingming Cao617ba132006-10-11 01:20:53 -07001616 ext4_xattr_release_block(handle, inode, bh);
1617 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618
1619cleanup:
1620 brelse(bh);
1621}
1622
1623/*
Mingming Cao617ba132006-10-11 01:20:53 -07001624 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 *
1626 * Create a new entry in the extended attribute cache, and insert
1627 * it unless such an entry is already in the cache.
1628 *
1629 * Returns 0, or a negative error number on failure.
1630 */
1631static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001632ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001633{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001634 struct ext4_xattr_header *header = BHDR(bh);
1635 __u32 hash = le32_to_cpu(header->h_hash);
1636 int reusable = le32_to_cpu(header->h_refcount) <
1637 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638 int error;
1639
Jan Kara7a2508e2016-02-22 22:35:22 -05001640 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001641 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001643 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001644 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001645 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647}
1648
1649/*
Mingming Cao617ba132006-10-11 01:20:53 -07001650 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 *
1652 * Compare two extended attribute blocks for equality.
1653 *
1654 * Returns 0 if the blocks are equal, 1 if they differ, and
1655 * a negative error number on errors.
1656 */
1657static int
Mingming Cao617ba132006-10-11 01:20:53 -07001658ext4_xattr_cmp(struct ext4_xattr_header *header1,
1659 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001660{
Mingming Cao617ba132006-10-11 01:20:53 -07001661 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662
1663 entry1 = ENTRY(header1+1);
1664 entry2 = ENTRY(header2+1);
1665 while (!IS_LAST_ENTRY(entry1)) {
1666 if (IS_LAST_ENTRY(entry2))
1667 return 1;
1668 if (entry1->e_hash != entry2->e_hash ||
1669 entry1->e_name_index != entry2->e_name_index ||
1670 entry1->e_name_len != entry2->e_name_len ||
1671 entry1->e_value_size != entry2->e_value_size ||
1672 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1673 return 1;
1674 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001675 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001676 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1677 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1678 le32_to_cpu(entry1->e_value_size)))
1679 return 1;
1680
Mingming Cao617ba132006-10-11 01:20:53 -07001681 entry1 = EXT4_XATTR_NEXT(entry1);
1682 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 }
1684 if (!IS_LAST_ENTRY(entry2))
1685 return 1;
1686 return 0;
1687}
1688
1689/*
Mingming Cao617ba132006-10-11 01:20:53 -07001690 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 *
1692 * Find an identical extended attribute block.
1693 *
1694 * Returns a pointer to the block found, or NULL if such a block was
1695 * not found or an error occurred.
1696 */
1697static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001698ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001699 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700{
1701 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001702 struct mb_cache_entry *ce;
1703 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001704
1705 if (!header->h_hash)
1706 return NULL; /* never share */
1707 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001708 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 while (ce) {
1710 struct buffer_head *bh;
1711
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 bh = sb_bread(inode->i_sb, ce->e_block);
1713 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001714 EXT4_ERROR_INODE(inode, "block %lu read error",
1715 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001716 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 *pce = ce;
1718 return bh;
1719 }
1720 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001721 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722 }
1723 return NULL;
1724}
1725
1726#define NAME_HASH_SHIFT 5
1727#define VALUE_HASH_SHIFT 16
1728
1729/*
Mingming Cao617ba132006-10-11 01:20:53 -07001730 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001731 *
1732 * Compute the hash of an extended attribute.
1733 */
Mingming Cao617ba132006-10-11 01:20:53 -07001734static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1735 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736{
1737 __u32 hash = 0;
1738 char *name = entry->e_name;
1739 int n;
1740
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001741 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001742 hash = (hash << NAME_HASH_SHIFT) ^
1743 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1744 *name++;
1745 }
1746
Jan Kara1cba4232016-08-29 15:40:11 -04001747 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 __le32 *value = (__le32 *)((char *)header +
1749 le16_to_cpu(entry->e_value_offs));
1750 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001751 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 hash = (hash << VALUE_HASH_SHIFT) ^
1753 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1754 le32_to_cpu(*value++);
1755 }
1756 }
1757 entry->e_hash = cpu_to_le32(hash);
1758}
1759
1760#undef NAME_HASH_SHIFT
1761#undef VALUE_HASH_SHIFT
1762
1763#define BLOCK_HASH_SHIFT 16
1764
1765/*
Mingming Cao617ba132006-10-11 01:20:53 -07001766 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001767 *
1768 * Re-compute the extended attribute hash value after an entry has changed.
1769 */
Mingming Cao617ba132006-10-11 01:20:53 -07001770static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1771 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772{
Mingming Cao617ba132006-10-11 01:20:53 -07001773 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 __u32 hash = 0;
1775
Mingming Cao617ba132006-10-11 01:20:53 -07001776 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777 here = ENTRY(header+1);
1778 while (!IS_LAST_ENTRY(here)) {
1779 if (!here->e_hash) {
1780 /* Block is not shared if an entry's hash value == 0 */
1781 hash = 0;
1782 break;
1783 }
1784 hash = (hash << BLOCK_HASH_SHIFT) ^
1785 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1786 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001787 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 }
1789 header->h_hash = cpu_to_le32(hash);
1790}
1791
1792#undef BLOCK_HASH_SHIFT
1793
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001794#define HASH_BUCKET_BITS 10
1795
Jan Kara7a2508e2016-02-22 22:35:22 -05001796struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001797ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798{
Jan Kara7a2508e2016-02-22 22:35:22 -05001799 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800}
1801
Jan Kara7a2508e2016-02-22 22:35:22 -05001802void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001804 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001805 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001807