blob: 1447860b61ec90a5a759cb92caf4bfc27acf9136 [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
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064# define ea_idebug(inode, f...) do { \
65 printk(KERN_DEBUG "inode %s:%lu: ", \
66 inode->i_sb->s_id, inode->i_ino); \
67 printk(f); \
68 printk("\n"); \
69 } while (0)
70# define ea_bdebug(bh, f...) do { \
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +040071 printk(KERN_DEBUG "block %pg:%lu: ", \
72 bh->b_bdev, (unsigned long) bh->b_blocknr); \
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073 printk(f); \
74 printk("\n"); \
75 } while (0)
76#else
Joe Perchesace36ad2012-03-19 23:11:43 -040077# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
78# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070079#endif
80
Jan Kara7a2508e2016-02-22 22:35:22 -050081static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070082static struct buffer_head *ext4_xattr_cache_find(struct inode *,
83 struct ext4_xattr_header *,
Jan Kara7a2508e2016-02-22 22:35:22 -050084 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070085static void ext4_xattr_rehash(struct ext4_xattr_header *,
86 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000087static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040088 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089
Stephen Hemminger11e27522010-05-13 17:53:18 -070090static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070091 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040092#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080093 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
94 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095#endif
Mingming Cao617ba132006-10-11 01:20:53 -070096 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040097#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -070098 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070099#endif
100};
101
Stephen Hemminger11e27522010-05-13 17:53:18 -0700102const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700103 &ext4_xattr_user_handler,
104 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400105#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800106 &posix_acl_access_xattr_handler,
107 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400109#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700110 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111#endif
112 NULL
113};
114
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400115#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
116 inode->i_sb->s_fs_info)->s_mb_cache)
117
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400118static __le32 ext4_xattr_block_csum(struct inode *inode,
119 sector_t block_nr,
120 struct ext4_xattr_header *hdr)
121{
122 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400123 __u32 csum;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400124 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400125 __u32 dummy_csum = 0;
126 int offset = offsetof(struct ext4_xattr_header, h_checksum);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400127
Theodore Ts'od6a77102013-04-09 23:59:55 -0400128 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
129 sizeof(dsk_block_nr));
Daeho Jeongb47820e2016-07-03 17:51:39 -0400130 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
131 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
132 offset += sizeof(dummy_csum);
133 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
134 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
Tao Ma41eb70d2012-07-09 16:29:27 -0400135
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400136 return cpu_to_le32(csum);
137}
138
139static int ext4_xattr_block_csum_verify(struct inode *inode,
140 sector_t block_nr,
141 struct ext4_xattr_header *hdr)
142{
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400143 if (ext4_has_metadata_csum(inode->i_sb) &&
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400144 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
145 return 0;
146 return 1;
147}
148
149static void ext4_xattr_block_csum_set(struct inode *inode,
150 sector_t block_nr,
151 struct ext4_xattr_header *hdr)
152{
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400153 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400154 return;
155
156 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
157}
158
159static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
160 struct inode *inode,
161 struct buffer_head *bh)
162{
163 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
164 return ext4_handle_dirty_metadata(handle, inode, bh);
165}
166
Stephen Hemminger11e27522010-05-13 17:53:18 -0700167static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700168ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700170 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171
Mingming Cao617ba132006-10-11 01:20:53 -0700172 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
173 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 return handler;
175}
176
177/*
178 * Inode operation listxattr()
179 *
David Howells2b0143b2015-03-17 22:25:59 +0000180 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 */
182ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700183ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000185 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186}
187
188static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400189ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
190 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400192 struct ext4_xattr_entry *e = entry;
193
194 while (!IS_LAST_ENTRY(e)) {
195 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400197 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400198 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700199 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400200
201 while (!IS_LAST_ENTRY(entry)) {
Jan Kara2de58f12016-08-29 15:39:11 -0400202 if (entry->e_value_block != 0)
203 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400204 if (entry->e_value_size != 0 &&
205 (value_start + le16_to_cpu(entry->e_value_offs) <
206 (void *)e + sizeof(__u32) ||
207 value_start + le16_to_cpu(entry->e_value_offs) +
208 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400209 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400210 entry = EXT4_XATTR_NEXT(entry);
211 }
212
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 return 0;
214}
215
216static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400217ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400219 int error;
220
221 if (buffer_verified(bh))
222 return 0;
223
Mingming Cao617ba132006-10-11 01:20:53 -0700224 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400226 return -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400227 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400228 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400229 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
230 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400231 if (!error)
232 set_buffer_verified(bh);
233 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700234}
235
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400236static int
237__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
238 void *end, const char *function, unsigned int line)
239{
240 struct ext4_xattr_entry *entry = IFIRST(header);
241 int error = -EFSCORRUPTED;
242
243 if (((void *) header >= end) ||
244 (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
245 goto errout;
246 error = ext4_xattr_check_names(entry, end, entry);
247errout:
248 if (error)
249 __ext4_error_inode(inode, function, line, 0,
250 "corrupted in-inode xattr");
251 return error;
252}
253
254#define xattr_check_inode(inode, header, end) \
255 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
256
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700258ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259{
260 size_t value_size = le32_to_cpu(entry->e_value_size);
261
262 if (entry->e_value_block != 0 || value_size > size ||
263 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400264 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265 return 0;
266}
267
268static int
Mingming Cao617ba132006-10-11 01:20:53 -0700269ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 const char *name, size_t size, int sorted)
271{
Mingming Cao617ba132006-10-11 01:20:53 -0700272 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 size_t name_len;
274 int cmp = 1;
275
276 if (name == NULL)
277 return -EINVAL;
278 name_len = strlen(name);
279 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700280 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 cmp = name_index - entry->e_name_index;
282 if (!cmp)
283 cmp = name_len - entry->e_name_len;
284 if (!cmp)
285 cmp = memcmp(name, entry->e_name, name_len);
286 if (cmp <= 0 && (sorted || cmp == 0))
287 break;
288 }
289 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700290 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400291 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 return cmp ? -ENODATA : 0;
293}
294
295static int
Mingming Cao617ba132006-10-11 01:20:53 -0700296ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 void *buffer, size_t buffer_size)
298{
299 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700300 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 size_t size;
302 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500303 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304
305 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
306 name_index, name, buffer, (long)buffer_size);
307
308 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700309 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400311 ea_idebug(inode, "reading block %llu",
312 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700313 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 if (!bh)
315 goto cleanup;
316 ea_bdebug(bh, "b_count=%d, refcount=%d",
317 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400318 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500319bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400320 EXT4_ERROR_INODE(inode, "bad block %llu",
321 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400322 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 goto cleanup;
324 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400325 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700327 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400328 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329 goto bad_block;
330 if (error)
331 goto cleanup;
332 size = le32_to_cpu(entry->e_value_size);
333 if (buffer) {
334 error = -ERANGE;
335 if (size > buffer_size)
336 goto cleanup;
337 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
338 size);
339 }
340 error = size;
341
342cleanup:
343 brelse(bh);
344 return error;
345}
346
Tao Ma879b3822012-12-05 10:28:46 -0500347int
Mingming Cao617ba132006-10-11 01:20:53 -0700348ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 void *buffer, size_t buffer_size)
350{
Mingming Cao617ba132006-10-11 01:20:53 -0700351 struct ext4_xattr_ibody_header *header;
352 struct ext4_xattr_entry *entry;
353 struct ext4_inode *raw_inode;
354 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 size_t size;
356 void *end;
357 int error;
358
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500359 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700361 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 if (error)
363 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700364 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 header = IHDR(inode, raw_inode);
366 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700367 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400368 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700369 if (error)
370 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700371 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700372 end - (void *)entry, 0);
373 if (error)
374 goto cleanup;
375 size = le32_to_cpu(entry->e_value_size);
376 if (buffer) {
377 error = -ERANGE;
378 if (size > buffer_size)
379 goto cleanup;
380 memcpy(buffer, (void *)IFIRST(header) +
381 le16_to_cpu(entry->e_value_offs), size);
382 }
383 error = size;
384
385cleanup:
386 brelse(iloc.bh);
387 return error;
388}
389
390/*
Mingming Cao617ba132006-10-11 01:20:53 -0700391 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 *
393 * Copy an extended attribute into the buffer
394 * provided, or compute the buffer size required.
395 * Buffer is NULL to compute the size of the buffer required.
396 *
397 * Returns a negative error number on failure, or the number of bytes
398 * used / required on success.
399 */
400int
Mingming Cao617ba132006-10-11 01:20:53 -0700401ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402 void *buffer, size_t buffer_size)
403{
404 int error;
405
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400406 if (strlen(name) > 255)
407 return -ERANGE;
408
Mingming Cao617ba132006-10-11 01:20:53 -0700409 down_read(&EXT4_I(inode)->xattr_sem);
410 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700411 buffer_size);
412 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700413 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700415 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700416 return error;
417}
418
419static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000420ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421 char *buffer, size_t buffer_size)
422{
423 size_t rest = buffer_size;
424
Mingming Cao617ba132006-10-11 01:20:53 -0700425 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700426 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700427 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100429 if (handler && (!handler->list || handler->list(dentry))) {
430 const char *prefix = handler->prefix ?: handler->name;
431 size_t prefix_len = strlen(prefix);
432 size_t size = prefix_len + entry->e_name_len + 1;
433
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 if (buffer) {
435 if (size > rest)
436 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100437 memcpy(buffer, prefix, prefix_len);
438 buffer += prefix_len;
439 memcpy(buffer, entry->e_name, entry->e_name_len);
440 buffer += entry->e_name_len;
441 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442 }
443 rest -= size;
444 }
445 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100446 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447}
448
449static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000450ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451{
David Howells2b0143b2015-03-17 22:25:59 +0000452 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 struct buffer_head *bh = NULL;
454 int error;
Jan Kara7a2508e2016-02-22 22:35:22 -0500455 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456
457 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
458 buffer, (long)buffer_size);
459
460 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700461 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400463 ea_idebug(inode, "reading block %llu",
464 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700465 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466 error = -EIO;
467 if (!bh)
468 goto cleanup;
469 ea_bdebug(bh, "b_count=%d, refcount=%d",
470 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400471 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400472 EXT4_ERROR_INODE(inode, "bad block %llu",
473 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400474 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 goto cleanup;
476 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400477 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000478 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479
480cleanup:
481 brelse(bh);
482
483 return error;
484}
485
486static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000487ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488{
David Howells2b0143b2015-03-17 22:25:59 +0000489 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700490 struct ext4_xattr_ibody_header *header;
491 struct ext4_inode *raw_inode;
492 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 void *end;
494 int error;
495
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500496 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700498 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 if (error)
500 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700501 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700503 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o9e92f482016-03-22 16:13:15 -0400504 error = xattr_check_inode(inode, header, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 if (error)
506 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000507 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 buffer, buffer_size);
509
510cleanup:
511 brelse(iloc.bh);
512 return error;
513}
514
515/*
Mingming Cao617ba132006-10-11 01:20:53 -0700516 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 *
518 * Copy a list of attribute names into the buffer
519 * provided, or compute the buffer size required.
520 * Buffer is NULL to compute the size of the buffer required.
521 *
522 * Returns a negative error number on failure, or the number of bytes
523 * used / required on success.
524 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400525static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000526ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500528 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529
David Howells2b0143b2015-03-17 22:25:59 +0000530 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500531 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
532 if (ret < 0)
533 goto errout;
534 if (buffer) {
535 buffer += ret;
536 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500538 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
539 if (ret < 0)
540 goto errout;
541 ret += ret2;
542errout:
David Howells2b0143b2015-03-17 22:25:59 +0000543 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500544 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545}
546
547/*
Mingming Cao617ba132006-10-11 01:20:53 -0700548 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 * not set, set it.
550 */
Mingming Cao617ba132006-10-11 01:20:53 -0700551static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 struct super_block *sb)
553{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400554 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 return;
556
liang xie5d601252014-05-12 22:06:43 -0400557 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700558 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400559 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400560 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562}
563
564/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400565 * Release the xattr block BH: If the reference count is > 1, decrement it;
566 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567 */
568static void
Mingming Cao617ba132006-10-11 01:20:53 -0700569ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 struct buffer_head *bh)
571{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500572 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
573 u32 hash, ref;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800574 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575
liang xie5d601252014-05-12 22:06:43 -0400576 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800577 error = ext4_journal_get_write_access(handle, bh);
578 if (error)
579 goto out;
580
581 lock_buffer(bh);
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500582 hash = le32_to_cpu(BHDR(bh)->h_hash);
583 ref = le32_to_cpu(BHDR(bh)->h_refcount);
584 if (ref == 1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 ea_bdebug(bh, "refcount now=0; freeing");
Jan Kara82939d72016-02-22 11:50:13 -0500586 /*
587 * This must happen under buffer lock for
588 * ext4_xattr_block_set() to reliably detect freed block
589 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500590 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400592 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500593 ext4_free_blocks(handle, inode, bh, 0, 1,
594 EXT4_FREE_BLOCKS_METADATA |
595 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 } else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500597 ref--;
598 BHDR(bh)->h_refcount = cpu_to_le32(ref);
599 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
600 struct mb_cache_entry *ce;
601
602 ce = mb_cache_entry_get(ext4_mb_cache, hash,
603 bh->b_blocknr);
604 if (ce) {
605 ce->e_reusable = 1;
606 mb_cache_entry_put(ext4_mb_cache, ce);
607 }
608 }
609
Jan Karaec4cb1a2014-04-07 10:54:21 -0400610 /*
611 * Beware of this ugliness: Releasing of xattr block references
612 * from different inodes can race and so we have to protect
613 * from a race where someone else frees the block (and releases
614 * its journal_head) before we are done dirtying the buffer. In
615 * nojournal mode this race is harmless and we actually cannot
616 * call ext4_handle_dirty_xattr_block() with locked buffer as
617 * that function can call sync_dirty_buffer() so for that case
618 * we handle the dirtying after unlocking the buffer.
619 */
620 if (ext4_handle_valid(handle))
621 error = ext4_handle_dirty_xattr_block(handle, inode,
622 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500623 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400624 if (!ext4_handle_valid(handle))
625 error = ext4_handle_dirty_xattr_block(handle, inode,
626 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800627 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500628 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500629 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800630 ea_bdebug(bh, "refcount now=%d; releasing",
631 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800633out:
634 ext4_std_error(inode->i_sb, error);
635 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636}
637
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400638/*
639 * Find the available free space for EAs. This also returns the total number of
640 * bytes used by EA entries.
641 */
642static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
643 size_t *min_offs, void *base, int *total)
644{
645 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400646 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400647 size_t offs = le16_to_cpu(last->e_value_offs);
648 if (offs < *min_offs)
649 *min_offs = offs;
650 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500651 if (total)
652 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400653 }
654 return (*min_offs - ((void *)last - base) - sizeof(__u32));
655}
656
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657static int
Mingming Cao617ba132006-10-11 01:20:53 -0700658ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659{
Mingming Cao617ba132006-10-11 01:20:53 -0700660 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
662
663 /* Compute min_offs and last. */
664 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700665 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -0400666 if (last->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 size_t offs = le16_to_cpu(last->e_value_offs);
668 if (offs < min_offs)
669 min_offs = offs;
670 }
671 }
672 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
673 if (!s->not_found) {
Jan Kara1cba4232016-08-29 15:40:11 -0400674 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700676 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 }
Mingming Cao617ba132006-10-11 01:20:53 -0700678 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679 }
680 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400681 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700682 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 return -ENOSPC;
684 }
685
686 if (i->value && s->not_found) {
687 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700688 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
690 memmove((void *)s->here + size, s->here, rest);
691 memset(s->here, 0, size);
692 s->here->e_name_index = i->name_index;
693 s->here->e_name_len = name_len;
694 memcpy(s->here->e_name, i->name, name_len);
695 } else {
Jan Kara1cba4232016-08-29 15:40:11 -0400696 if (s->here->e_value_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 void *first_val = s->base + min_offs;
698 size_t offs = le16_to_cpu(s->here->e_value_offs);
699 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700700 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 le32_to_cpu(s->here->e_value_size));
702
Mingming Cao617ba132006-10-11 01:20:53 -0700703 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 /* The old and the new value have the same
705 size. Just replace. */
706 s->here->e_value_size =
707 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500708 if (i->value == EXT4_ZERO_XATTR_VALUE) {
709 memset(val, 0, size);
710 } else {
711 /* Clear pad bytes first. */
712 memset(val + size - EXT4_XATTR_PAD, 0,
713 EXT4_XATTR_PAD);
714 memcpy(val, i->value, i->value_len);
715 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 return 0;
717 }
718
719 /* Remove the old value. */
720 memmove(first_val + size, first_val, val - first_val);
721 memset(first_val, 0, size);
722 s->here->e_value_size = 0;
723 s->here->e_value_offs = 0;
724 min_offs += size;
725
726 /* Adjust all value offsets. */
727 last = s->first;
728 while (!IS_LAST_ENTRY(last)) {
729 size_t o = le16_to_cpu(last->e_value_offs);
Jan Kara1cba4232016-08-29 15:40:11 -0400730 if (last->e_value_size && o < offs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700731 last->e_value_offs =
732 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700733 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 }
735 }
736 if (!i->value) {
737 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700738 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 last = ENTRY((void *)last - size);
740 memmove(s->here, (void *)s->here + size,
741 (void *)last - (void *)s->here + sizeof(__u32));
742 memset(last, 0, size);
743 }
744 }
745
746 if (i->value) {
747 /* Insert the new value. */
748 s->here->e_value_size = cpu_to_le32(i->value_len);
749 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700750 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 void *val = s->base + min_offs - size;
752 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500753 if (i->value == EXT4_ZERO_XATTR_VALUE) {
754 memset(val, 0, size);
755 } else {
756 /* Clear the pad bytes first. */
757 memset(val + size - EXT4_XATTR_PAD, 0,
758 EXT4_XATTR_PAD);
759 memcpy(val, i->value, i->value_len);
760 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 }
762 }
763 return 0;
764}
765
Mingming Cao617ba132006-10-11 01:20:53 -0700766struct ext4_xattr_block_find {
767 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 struct buffer_head *bh;
769};
770
771static int
Mingming Cao617ba132006-10-11 01:20:53 -0700772ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
773 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774{
775 struct super_block *sb = inode->i_sb;
776 int error;
777
778 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
779 i->name_index, i->name, i->value, (long)i->value_len);
780
Mingming Cao617ba132006-10-11 01:20:53 -0700781 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700783 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784 error = -EIO;
785 if (!bs->bh)
786 goto cleanup;
787 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
788 atomic_read(&(bs->bh->b_count)),
789 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400790 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400791 EXT4_ERROR_INODE(inode, "bad block %llu",
792 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400793 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700794 goto cleanup;
795 }
796 /* Find the named attribute. */
797 bs->s.base = BHDR(bs->bh);
798 bs->s.first = BFIRST(bs->bh);
799 bs->s.end = bs->bh->b_data + bs->bh->b_size;
800 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700801 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802 i->name, bs->bh->b_size, 1);
803 if (error && error != -ENODATA)
804 goto cleanup;
805 bs->s.not_found = error;
806 }
807 error = 0;
808
809cleanup:
810 return error;
811}
812
813static int
Mingming Cao617ba132006-10-11 01:20:53 -0700814ext4_xattr_block_set(handle_t *handle, struct inode *inode,
815 struct ext4_xattr_info *i,
816 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817{
818 struct super_block *sb = inode->i_sb;
819 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700820 struct ext4_xattr_search *s = &bs->s;
Jan Kara7a2508e2016-02-22 22:35:22 -0500821 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800822 int error = 0;
Jan Kara7a2508e2016-02-22 22:35:22 -0500823 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824
Mingming Cao617ba132006-10-11 01:20:53 -0700825#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826
827 if (i->value && i->value_len > sb->s_blocksize)
828 return -ENOSPC;
829 if (s->base) {
liang xie5d601252014-05-12 22:06:43 -0400830 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800831 error = ext4_journal_get_write_access(handle, bs->bh);
832 if (error)
833 goto cleanup;
834 lock_buffer(bs->bh);
835
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
Jan Kara82939d72016-02-22 11:50:13 -0500837 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
838
839 /*
840 * This must happen under buffer lock for
841 * ext4_xattr_block_set() to reliably detect modified
842 * block
843 */
Jan Kara7a2508e2016-02-22 22:35:22 -0500844 mb_cache_entry_delete_block(ext4_mb_cache, hash,
845 bs->bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700847 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 if (!error) {
849 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700850 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400852 ext4_xattr_cache_insert(ext4_mb_cache,
853 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 }
855 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400856 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 goto bad_block;
858 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400859 error = ext4_handle_dirty_xattr_block(handle,
860 inode,
861 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862 if (error)
863 goto cleanup;
864 goto inserted;
865 } else {
866 int offset = (char *)s->here - bs->bh->b_data;
867
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800868 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400870 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 error = -ENOMEM;
872 if (s->base == NULL)
873 goto cleanup;
874 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
875 s->first = ENTRY(header(s->base)+1);
876 header(s->base)->h_refcount = cpu_to_le32(1);
877 s->here = ENTRY(s->base + offset);
878 s->end = s->base + bs->bh->b_size;
879 }
880 } else {
881 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400882 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883 /* assert(header == s->base) */
884 error = -ENOMEM;
885 if (s->base == NULL)
886 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700887 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888 header(s->base)->h_blocks = cpu_to_le32(1);
889 header(s->base)->h_refcount = cpu_to_le32(1);
890 s->first = ENTRY(header(s->base)+1);
891 s->here = ENTRY(header(s->base)+1);
892 s->end = s->base + sb->s_blocksize;
893 }
894
Mingming Cao617ba132006-10-11 01:20:53 -0700895 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400896 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 goto bad_block;
898 if (error)
899 goto cleanup;
900 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700901 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902
903inserted:
904 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700905 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906 if (new_bh) {
907 /* We found an identical block in the cache. */
908 if (new_bh == bs->bh)
909 ea_bdebug(new_bh, "keeping");
910 else {
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500911 u32 ref;
912
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 /* The old block is released after updating
914 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500915 error = dquot_alloc_block(inode,
916 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500917 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400919 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700920 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 new_bh);
922 if (error)
923 goto cleanup_dquot;
924 lock_buffer(new_bh);
Jan Kara82939d72016-02-22 11:50:13 -0500925 /*
926 * We have to be careful about races with
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500927 * freeing, rehashing or adding references to
928 * xattr block. Once we hold buffer lock xattr
929 * block's state is stable so we can check
930 * whether the block got freed / rehashed or
931 * not. Since we unhash mbcache entry under
932 * buffer lock when freeing / rehashing xattr
933 * block, checking whether entry is still
934 * hashed is reliable. Same rules hold for
935 * e_reusable handling.
Jan Kara82939d72016-02-22 11:50:13 -0500936 */
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500937 if (hlist_bl_unhashed(&ce->e_hash_list) ||
938 !ce->e_reusable) {
Jan Kara82939d72016-02-22 11:50:13 -0500939 /*
940 * Undo everything and check mbcache
941 * again.
942 */
943 unlock_buffer(new_bh);
944 dquot_free_block(inode,
945 EXT4_C2B(EXT4_SB(sb),
946 1));
947 brelse(new_bh);
Jan Kara7a2508e2016-02-22 22:35:22 -0500948 mb_cache_entry_put(ext4_mb_cache, ce);
Jan Kara82939d72016-02-22 11:50:13 -0500949 ce = NULL;
950 new_bh = NULL;
951 goto inserted;
952 }
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500953 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
954 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
955 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
956 ce->e_reusable = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 ea_bdebug(new_bh, "reusing; refcount now=%d",
Andreas Gruenbacher6048c642016-02-22 22:44:04 -0500958 ref);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400960 error = ext4_handle_dirty_xattr_block(handle,
961 inode,
962 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 if (error)
964 goto cleanup_dquot;
965 }
Jan Kara7a2508e2016-02-22 22:35:22 -0500966 mb_cache_entry_touch(ext4_mb_cache, ce);
967 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700968 ce = NULL;
969 } else if (bs->bh && s->base == bs->bh->b_data) {
970 /* We were modifying this block in-place. */
971 ea_bdebug(bs->bh, "keeping this block");
972 new_bh = bs->bh;
973 get_bh(new_bh);
974 } else {
975 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400976 ext4_fsblk_t goal, block;
977
978 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400979 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400980
981 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400982 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400983 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
984
Allison Henderson55f020d2011-05-25 07:41:26 -0400985 block = ext4_new_meta_blocks(handle, inode, goal, 0,
986 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987 if (error)
988 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400989
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400990 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400991 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
992
Joe Perchesace36ad2012-03-19 23:11:43 -0400993 ea_idebug(inode, "creating block %llu",
994 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995
996 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500997 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500998 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -05001000 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001001 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 goto cleanup;
1003 }
1004 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001005 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 if (error) {
1007 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001008 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 goto getblk_failed;
1010 }
1011 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1012 set_buffer_uptodate(new_bh);
1013 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001014 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001015 error = ext4_handle_dirty_xattr_block(handle,
1016 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001017 if (error)
1018 goto cleanup;
1019 }
1020 }
1021
1022 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -07001023 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024
1025 /* Drop the previous xattr block. */
1026 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -07001027 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001028 error = 0;
1029
1030cleanup:
1031 if (ce)
Jan Kara7a2508e2016-02-22 22:35:22 -05001032 mb_cache_entry_put(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001033 brelse(new_bh);
1034 if (!(bs->bh && s->base == bs->bh->b_data))
1035 kfree(s->base);
1036
1037 return error;
1038
1039cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -05001040 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 goto cleanup;
1042
1043bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -04001044 EXT4_ERROR_INODE(inode, "bad block %llu",
1045 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 goto cleanup;
1047
1048#undef header
1049}
1050
Tao Ma879b3822012-12-05 10:28:46 -05001051int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1052 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053{
Mingming Cao617ba132006-10-11 01:20:53 -07001054 struct ext4_xattr_ibody_header *header;
1055 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 int error;
1057
Mingming Cao617ba132006-10-11 01:20:53 -07001058 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001060 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 header = IHDR(inode, raw_inode);
1062 is->s.base = is->s.first = IFIRST(header);
1063 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -07001064 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001065 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001066 error = xattr_check_inode(inode, header, is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 if (error)
1068 return error;
1069 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001070 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 i->name, is->s.end -
1072 (void *)is->s.base, 0);
1073 if (error && error != -ENODATA)
1074 return error;
1075 is->s.not_found = error;
1076 }
1077 return 0;
1078}
1079
Tao Ma0d812f72012-12-10 14:06:02 -05001080int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1081 struct ext4_xattr_info *i,
1082 struct ext4_xattr_ibody_find *is)
1083{
1084 struct ext4_xattr_ibody_header *header;
1085 struct ext4_xattr_search *s = &is->s;
1086 int error;
1087
1088 if (EXT4_I(inode)->i_extra_isize == 0)
1089 return -ENOSPC;
1090 error = ext4_xattr_set_entry(i, s);
1091 if (error) {
1092 if (error == -ENOSPC &&
1093 ext4_has_inline_data(inode)) {
1094 error = ext4_try_to_evict_inline_data(handle, inode,
1095 EXT4_XATTR_LEN(strlen(i->name) +
1096 EXT4_XATTR_SIZE(i->value_len)));
1097 if (error)
1098 return error;
1099 error = ext4_xattr_ibody_find(inode, i, is);
1100 if (error)
1101 return error;
1102 error = ext4_xattr_set_entry(i, s);
1103 }
1104 if (error)
1105 return error;
1106 }
1107 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1108 if (!IS_LAST_ENTRY(s->first)) {
1109 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1110 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1111 } else {
1112 header->h_magic = cpu_to_le32(0);
1113 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1114 }
1115 return 0;
1116}
1117
1118static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1119 struct ext4_xattr_info *i,
1120 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121{
Mingming Cao617ba132006-10-11 01:20:53 -07001122 struct ext4_xattr_ibody_header *header;
1123 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 int error;
1125
Mingming Cao617ba132006-10-11 01:20:53 -07001126 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001128 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 if (error)
1130 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001131 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001133 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001134 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135 } else {
1136 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001137 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 }
1139 return 0;
1140}
1141
Jan Kara3fd16462016-02-22 22:43:04 -05001142static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1143 struct ext4_xattr_info *i)
1144{
1145 void *value;
1146
1147 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1148 return 0;
1149 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1150 return !memcmp(value, i->value, i->value_len);
1151}
1152
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153/*
Mingming Cao617ba132006-10-11 01:20:53 -07001154 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001156 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157 * is NULL to remove an existing extended attribute, and non-NULL to
1158 * either replace an existing extended attribute, or create a new extended
1159 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1160 * specify that an extended attribute must exist and must not exist
1161 * previous to the call, respectively.
1162 *
1163 * Returns 0, or a negative error number on failure.
1164 */
1165int
Mingming Cao617ba132006-10-11 01:20:53 -07001166ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 const char *name, const void *value, size_t value_len,
1168 int flags)
1169{
Mingming Cao617ba132006-10-11 01:20:53 -07001170 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 .name_index = name_index,
1172 .name = name,
1173 .value = value,
1174 .value_len = value_len,
1175
1176 };
Mingming Cao617ba132006-10-11 01:20:53 -07001177 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 .s = { .not_found = -ENODATA, },
1179 };
Mingming Cao617ba132006-10-11 01:20:53 -07001180 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 .s = { .not_found = -ENODATA, },
1182 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001183 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001184 int error;
1185
1186 if (!name)
1187 return -EINVAL;
1188 if (strlen(name) > 255)
1189 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001190 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001191 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1192 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001193
Eric Sandeen66543612011-10-26 03:32:07 -04001194 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001195 if (error)
1196 goto cleanup;
1197
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001198 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001199 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1200 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001201 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 }
1203
Mingming Cao617ba132006-10-11 01:20:53 -07001204 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 if (error)
1206 goto cleanup;
1207 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001208 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 if (error)
1210 goto cleanup;
1211 if (is.s.not_found && bs.s.not_found) {
1212 error = -ENODATA;
1213 if (flags & XATTR_REPLACE)
1214 goto cleanup;
1215 error = 0;
1216 if (!value)
1217 goto cleanup;
1218 } else {
1219 error = -EEXIST;
1220 if (flags & XATTR_CREATE)
1221 goto cleanup;
1222 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001223 if (!value) {
1224 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001225 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001227 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001228 } else {
Jan Kara3fd16462016-02-22 22:43:04 -05001229 error = 0;
1230 /* Xattr value did not change? Save us some work and bail out */
1231 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1232 goto cleanup;
1233 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1234 goto cleanup;
1235
Mingming Cao617ba132006-10-11 01:20:53 -07001236 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237 if (!error && !bs.s.not_found) {
1238 i.value = NULL;
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 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001241 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1242 error = ext4_xattr_block_find(inode, &i, &bs);
1243 if (error)
1244 goto cleanup;
1245 }
Mingming Cao617ba132006-10-11 01:20:53 -07001246 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 if (error)
1248 goto cleanup;
1249 if (!is.s.not_found) {
1250 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001251 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252 &is);
1253 }
1254 }
1255 }
1256 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001257 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001258 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001259 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001260 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001261 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001263 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264 * error != 0.
1265 */
1266 is.iloc.bh = NULL;
1267 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001268 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 }
1270
1271cleanup:
1272 brelse(is.iloc.bh);
1273 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001274 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001275 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001276 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 return error;
1278}
1279
1280/*
Mingming Cao617ba132006-10-11 01:20:53 -07001281 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 *
Mingming Cao617ba132006-10-11 01:20:53 -07001283 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 * attribute modification is a filesystem transaction by itself.
1285 *
1286 * Returns 0, or a negative error number on failure.
1287 */
1288int
Mingming Cao617ba132006-10-11 01:20:53 -07001289ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290 const void *value, size_t value_len, int flags)
1291{
1292 handle_t *handle;
1293 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001294 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295
1296retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001297 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 if (IS_ERR(handle)) {
1299 error = PTR_ERR(handle);
1300 } else {
1301 int error2;
1302
Mingming Cao617ba132006-10-11 01:20:53 -07001303 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001304 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001305 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001307 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308 goto retry;
1309 if (error == 0)
1310 error = error2;
1311 }
1312
1313 return error;
1314}
1315
1316/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001317 * Shift the EA entries in the inode to create space for the increased
1318 * i_extra_isize.
1319 */
1320static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1321 int value_offs_shift, void *to,
1322 void *from, size_t n, int blocksize)
1323{
1324 struct ext4_xattr_entry *last = entry;
1325 int new_offs;
1326
1327 /* Adjust the value offsets of the entries */
1328 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Jan Kara1cba4232016-08-29 15:40:11 -04001329 if (last->e_value_size) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001330 new_offs = le16_to_cpu(last->e_value_offs) +
1331 value_offs_shift;
1332 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1333 > blocksize);
1334 last->e_value_offs = cpu_to_le16(new_offs);
1335 }
1336 }
1337 /* Shift the entries by n bytes */
1338 memmove(to, from, n);
1339}
1340
1341/*
1342 * Expand an inode by new_extra_isize bytes when EAs are present.
1343 * Returns 0 on success or negative error number on failure.
1344 */
1345int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1346 struct ext4_inode *raw_inode, handle_t *handle)
1347{
1348 struct ext4_xattr_ibody_header *header;
1349 struct ext4_xattr_entry *entry, *last, *first;
1350 struct buffer_head *bh = NULL;
1351 struct ext4_xattr_ibody_find *is = NULL;
1352 struct ext4_xattr_block_find *bs = NULL;
1353 char *buffer = NULL, *b_entry_name = NULL;
Jan Karae3014d12016-08-29 15:38:11 -04001354 size_t min_offs;
1355 size_t ifree, bfree;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001356 int total_ino;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001357 void *base, *start, *end;
Jan Karad0141192016-08-11 11:50:30 -04001358 int error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001359 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 -04001360 int isize_diff; /* How much do we need to grow i_extra_isize */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001361
1362 down_write(&EXT4_I(inode)->xattr_sem);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001363 /*
1364 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1365 */
1366 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001367retry:
Jan Karad0141192016-08-11 11:50:30 -04001368 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
Jan Kara2e81a4e2016-08-11 12:38:55 -04001369 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1370 goto out;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001371
1372 header = IHDR(inode, raw_inode);
1373 entry = IFIRST(header);
1374
1375 /*
1376 * Check if enough free space is available in the inode to shift the
1377 * entries ahead by new_extra_isize.
1378 */
1379
1380 base = start = entry;
1381 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1382 min_offs = end - base;
1383 last = entry;
1384 total_ino = sizeof(struct ext4_xattr_ibody_header);
1385
Theodore Ts'o9e92f482016-03-22 16:13:15 -04001386 error = xattr_check_inode(inode, header, end);
1387 if (error)
1388 goto cleanup;
1389
Jan Karae3014d12016-08-29 15:38:11 -04001390 ifree = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1391 if (ifree >= isize_diff)
1392 goto shift;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001393
1394 /*
1395 * Enough free space isn't available in the inode, check if
1396 * EA block can hold new_extra_isize bytes.
1397 */
1398 if (EXT4_I(inode)->i_file_acl) {
1399 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1400 error = -EIO;
1401 if (!bh)
1402 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001403 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001404 EXT4_ERROR_INODE(inode, "bad block %llu",
1405 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001406 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001407 goto cleanup;
1408 }
1409 base = BHDR(bh);
1410 first = BFIRST(bh);
1411 end = bh->b_data + bh->b_size;
1412 min_offs = end - base;
Jan Karae3014d12016-08-29 15:38:11 -04001413 bfree = ext4_xattr_free_space(first, &min_offs, base, NULL);
1414 if (bfree + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001415 if (!tried_min_extra_isize && s_min_extra_isize) {
1416 tried_min_extra_isize++;
1417 new_extra_isize = s_min_extra_isize;
1418 brelse(bh);
1419 goto retry;
1420 }
1421 error = -1;
1422 goto cleanup;
1423 }
1424 } else {
Jan Karae3014d12016-08-29 15:38:11 -04001425 bfree = inode->i_sb->s_blocksize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001426 }
1427
Jan Karae3014d12016-08-29 15:38:11 -04001428 while (isize_diff > ifree) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001429 size_t offs, size, entry_size;
1430 struct ext4_xattr_entry *small_entry = NULL;
1431 struct ext4_xattr_info i = {
1432 .value = NULL,
1433 .value_len = 0,
1434 };
1435 unsigned int total_size; /* EA entry size + value size */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001436 unsigned int min_total_size = ~0U;
1437
1438 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1439 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1440 if (!is || !bs) {
1441 error = -ENOMEM;
1442 goto cleanup;
1443 }
1444
1445 is->s.not_found = -ENODATA;
1446 bs->s.not_found = -ENODATA;
1447 is->iloc.bh = NULL;
1448 bs->bh = NULL;
1449
1450 last = IFIRST(header);
1451 /* Find the entry best suited to be pushed into EA block */
1452 entry = NULL;
1453 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1454 total_size =
1455 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1456 EXT4_XATTR_LEN(last->e_name_len);
Jan Karae3014d12016-08-29 15:38:11 -04001457 if (total_size <= bfree &&
1458 total_size < min_total_size) {
1459 if (total_size + ifree < isize_diff) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001460 small_entry = last;
1461 } else {
1462 entry = last;
1463 min_total_size = total_size;
1464 }
1465 }
1466 }
1467
1468 if (entry == NULL) {
1469 if (small_entry) {
1470 entry = small_entry;
1471 } else {
1472 if (!tried_min_extra_isize &&
1473 s_min_extra_isize) {
1474 tried_min_extra_isize++;
1475 new_extra_isize = s_min_extra_isize;
Dave Jones6e4ea8e2013-10-10 20:05:35 -04001476 kfree(is); is = NULL;
1477 kfree(bs); bs = NULL;
Theodore Ts'odcb99172013-10-31 23:00:24 -04001478 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001479 goto retry;
1480 }
1481 error = -1;
1482 goto cleanup;
1483 }
1484 }
1485 offs = le16_to_cpu(entry->e_value_offs);
1486 size = le32_to_cpu(entry->e_value_size);
1487 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
Jan Karae3014d12016-08-29 15:38:11 -04001488 total_size = entry_size + EXT4_XATTR_SIZE(size);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001489 i.name_index = entry->e_name_index,
1490 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1491 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1492 if (!buffer || !b_entry_name) {
1493 error = -ENOMEM;
1494 goto cleanup;
1495 }
1496 /* Save the entry name and the entry value */
1497 memcpy(buffer, (void *)IFIRST(header) + offs,
1498 EXT4_XATTR_SIZE(size));
1499 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1500 b_entry_name[entry->e_name_len] = '\0';
1501 i.name = b_entry_name;
1502
1503 error = ext4_get_inode_loc(inode, &is->iloc);
1504 if (error)
1505 goto cleanup;
1506
1507 error = ext4_xattr_ibody_find(inode, &i, is);
1508 if (error)
1509 goto cleanup;
1510
1511 /* Remove the chosen entry from the inode */
1512 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001513 if (error)
1514 goto cleanup;
Jan Kara418c12d2016-08-11 11:58:32 -04001515 total_ino -= entry_size;
Jan Karae3014d12016-08-29 15:38:11 -04001516 ifree += total_size;
1517 bfree -= total_size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001518
1519 i.name = b_entry_name;
1520 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001521 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001522 error = ext4_xattr_block_find(inode, &i, bs);
1523 if (error)
1524 goto cleanup;
1525
1526 /* Add entry which was removed from the inode into the block */
1527 error = ext4_xattr_block_set(handle, inode, &i, bs);
1528 if (error)
1529 goto cleanup;
1530 kfree(b_entry_name);
1531 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001532 b_entry_name = NULL;
1533 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001534 brelse(is->iloc.bh);
1535 kfree(is);
1536 kfree(bs);
1537 }
Jan Karae3014d12016-08-29 15:38:11 -04001538
1539shift:
1540 /* Adjust the offsets and shift the remaining entries ahead */
1541 entry = IFIRST(header);
1542 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1543 - new_extra_isize, (void *)raw_inode +
1544 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1545 (void *)header, total_ino, inode->i_sb->s_blocksize);
1546 EXT4_I(inode)->i_extra_isize = new_extra_isize;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001547 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001548out:
1549 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001550 up_write(&EXT4_I(inode)->xattr_sem);
1551 return 0;
1552
1553cleanup:
1554 kfree(b_entry_name);
1555 kfree(buffer);
1556 if (is)
1557 brelse(is->iloc.bh);
1558 kfree(is);
1559 kfree(bs);
1560 brelse(bh);
Jan Kara2e81a4e2016-08-11 12:38:55 -04001561 /*
1562 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1563 * size expansion failed.
1564 */
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001565 up_write(&EXT4_I(inode)->xattr_sem);
1566 return error;
1567}
1568
1569
1570
1571/*
Mingming Cao617ba132006-10-11 01:20:53 -07001572 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 *
1574 * Free extended attribute resources associated with this inode. This
1575 * is called immediately before an inode is freed. We have exclusive
1576 * access to the inode.
1577 */
1578void
Mingming Cao617ba132006-10-11 01:20:53 -07001579ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580{
1581 struct buffer_head *bh = NULL;
1582
Mingming Cao617ba132006-10-11 01:20:53 -07001583 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001585 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001587 EXT4_ERROR_INODE(inode, "block %llu read error",
1588 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 goto cleanup;
1590 }
Mingming Cao617ba132006-10-11 01:20:53 -07001591 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001593 EXT4_ERROR_INODE(inode, "bad block %llu",
1594 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 goto cleanup;
1596 }
Mingming Cao617ba132006-10-11 01:20:53 -07001597 ext4_xattr_release_block(handle, inode, bh);
1598 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599
1600cleanup:
1601 brelse(bh);
1602}
1603
1604/*
Mingming Cao617ba132006-10-11 01:20:53 -07001605 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 *
1607 * Create a new entry in the extended attribute cache, and insert
1608 * it unless such an entry is already in the cache.
1609 *
1610 * Returns 0, or a negative error number on failure.
1611 */
1612static void
Jan Kara7a2508e2016-02-22 22:35:22 -05001613ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614{
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001615 struct ext4_xattr_header *header = BHDR(bh);
1616 __u32 hash = le32_to_cpu(header->h_hash);
1617 int reusable = le32_to_cpu(header->h_refcount) <
1618 EXT4_XATTR_REFCOUNT_MAX;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619 int error;
1620
Jan Kara7a2508e2016-02-22 22:35:22 -05001621 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
Andreas Gruenbacher6048c642016-02-22 22:44:04 -05001622 bh->b_blocknr, reusable);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623 if (error) {
Jan Kara82939d72016-02-22 11:50:13 -05001624 if (error == -EBUSY)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 ea_bdebug(bh, "already in cache");
Jan Kara82939d72016-02-22 11:50:13 -05001626 } else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 ea_bdebug(bh, "inserting [%x]", (int)hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628}
1629
1630/*
Mingming Cao617ba132006-10-11 01:20:53 -07001631 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 *
1633 * Compare two extended attribute blocks for equality.
1634 *
1635 * Returns 0 if the blocks are equal, 1 if they differ, and
1636 * a negative error number on errors.
1637 */
1638static int
Mingming Cao617ba132006-10-11 01:20:53 -07001639ext4_xattr_cmp(struct ext4_xattr_header *header1,
1640 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641{
Mingming Cao617ba132006-10-11 01:20:53 -07001642 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643
1644 entry1 = ENTRY(header1+1);
1645 entry2 = ENTRY(header2+1);
1646 while (!IS_LAST_ENTRY(entry1)) {
1647 if (IS_LAST_ENTRY(entry2))
1648 return 1;
1649 if (entry1->e_hash != entry2->e_hash ||
1650 entry1->e_name_index != entry2->e_name_index ||
1651 entry1->e_name_len != entry2->e_name_len ||
1652 entry1->e_value_size != entry2->e_value_size ||
1653 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1654 return 1;
1655 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001656 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1658 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1659 le32_to_cpu(entry1->e_value_size)))
1660 return 1;
1661
Mingming Cao617ba132006-10-11 01:20:53 -07001662 entry1 = EXT4_XATTR_NEXT(entry1);
1663 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 }
1665 if (!IS_LAST_ENTRY(entry2))
1666 return 1;
1667 return 0;
1668}
1669
1670/*
Mingming Cao617ba132006-10-11 01:20:53 -07001671 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001672 *
1673 * Find an identical extended attribute block.
1674 *
1675 * Returns a pointer to the block found, or NULL if such a block was
1676 * not found or an error occurred.
1677 */
1678static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001679ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Jan Kara7a2508e2016-02-22 22:35:22 -05001680 struct mb_cache_entry **pce)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681{
1682 __u32 hash = le32_to_cpu(header->h_hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001683 struct mb_cache_entry *ce;
1684 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001685
1686 if (!header->h_hash)
1687 return NULL; /* never share */
1688 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
Jan Kara7a2508e2016-02-22 22:35:22 -05001689 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690 while (ce) {
1691 struct buffer_head *bh;
1692
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693 bh = sb_bread(inode->i_sb, ce->e_block);
1694 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001695 EXT4_ERROR_INODE(inode, "block %lu read error",
1696 (unsigned long) ce->e_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001697 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 *pce = ce;
1699 return bh;
1700 }
1701 brelse(bh);
Jan Kara7a2508e2016-02-22 22:35:22 -05001702 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001703 }
1704 return NULL;
1705}
1706
1707#define NAME_HASH_SHIFT 5
1708#define VALUE_HASH_SHIFT 16
1709
1710/*
Mingming Cao617ba132006-10-11 01:20:53 -07001711 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 *
1713 * Compute the hash of an extended attribute.
1714 */
Mingming Cao617ba132006-10-11 01:20:53 -07001715static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1716 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717{
1718 __u32 hash = 0;
1719 char *name = entry->e_name;
1720 int n;
1721
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001722 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 hash = (hash << NAME_HASH_SHIFT) ^
1724 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1725 *name++;
1726 }
1727
Jan Kara1cba4232016-08-29 15:40:11 -04001728 if (entry->e_value_size != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 __le32 *value = (__le32 *)((char *)header +
1730 le16_to_cpu(entry->e_value_offs));
1731 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001732 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733 hash = (hash << VALUE_HASH_SHIFT) ^
1734 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1735 le32_to_cpu(*value++);
1736 }
1737 }
1738 entry->e_hash = cpu_to_le32(hash);
1739}
1740
1741#undef NAME_HASH_SHIFT
1742#undef VALUE_HASH_SHIFT
1743
1744#define BLOCK_HASH_SHIFT 16
1745
1746/*
Mingming Cao617ba132006-10-11 01:20:53 -07001747 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 *
1749 * Re-compute the extended attribute hash value after an entry has changed.
1750 */
Mingming Cao617ba132006-10-11 01:20:53 -07001751static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1752 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001753{
Mingming Cao617ba132006-10-11 01:20:53 -07001754 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 __u32 hash = 0;
1756
Mingming Cao617ba132006-10-11 01:20:53 -07001757 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758 here = ENTRY(header+1);
1759 while (!IS_LAST_ENTRY(here)) {
1760 if (!here->e_hash) {
1761 /* Block is not shared if an entry's hash value == 0 */
1762 hash = 0;
1763 break;
1764 }
1765 hash = (hash << BLOCK_HASH_SHIFT) ^
1766 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1767 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001768 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 }
1770 header->h_hash = cpu_to_le32(hash);
1771}
1772
1773#undef BLOCK_HASH_SHIFT
1774
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001775#define HASH_BUCKET_BITS 10
1776
Jan Kara7a2508e2016-02-22 22:35:22 -05001777struct mb_cache *
Jan Kara82939d72016-02-22 11:50:13 -05001778ext4_xattr_create_cache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779{
Jan Kara7a2508e2016-02-22 22:35:22 -05001780 return mb_cache_create(HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781}
1782
Jan Kara7a2508e2016-02-22 22:35:22 -05001783void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001784{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001785 if (cache)
Jan Kara7a2508e2016-02-22 22:35:22 -05001786 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001788