blob: ec4e50ce0733d2891c0faecbd1ae594c33195d46 [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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056#include <linux/mbcache.h>
57#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
T Makphaibulchoke9c191f72014-03-18 19:24:49 -040081static 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 *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084 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;
124 __le32 save_csum;
125 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400126
Theodore Ts'od6a77102013-04-09 23:59:55 -0400127 save_csum = hdr->h_checksum;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400128 hdr->h_checksum = 0;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400129 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
130 sizeof(dsk_block_nr));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400131 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
132 EXT4_BLOCK_SIZE(inode->i_sb));
Tao Ma41eb70d2012-07-09 16:29:27 -0400133
Theodore Ts'od6a77102013-04-09 23:59:55 -0400134 hdr->h_checksum = save_csum;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135 return cpu_to_le32(csum);
136}
137
138static int ext4_xattr_block_csum_verify(struct inode *inode,
139 sector_t block_nr,
140 struct ext4_xattr_header *hdr)
141{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400142 if (ext4_has_metadata_csum(inode->i_sb) &&
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400143 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
144 return 0;
145 return 1;
146}
147
148static void ext4_xattr_block_csum_set(struct inode *inode,
149 sector_t block_nr,
150 struct ext4_xattr_header *hdr)
151{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400152 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400153 return;
154
155 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
156}
157
158static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
159 struct inode *inode,
160 struct buffer_head *bh)
161{
162 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
163 return ext4_handle_dirty_metadata(handle, inode, bh);
164}
165
Stephen Hemminger11e27522010-05-13 17:53:18 -0700166static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700167ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700169 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170
Mingming Cao617ba132006-10-11 01:20:53 -0700171 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
172 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173 return handler;
174}
175
176/*
177 * Inode operation listxattr()
178 *
David Howells2b0143b2015-03-17 22:25:59 +0000179 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 */
181ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700182ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000184 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185}
186
187static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400188ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
189 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400191 struct ext4_xattr_entry *e = entry;
192
193 while (!IS_LAST_ENTRY(e)) {
194 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400196 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400197 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400199
200 while (!IS_LAST_ENTRY(entry)) {
201 if (entry->e_value_size != 0 &&
202 (value_start + le16_to_cpu(entry->e_value_offs) <
203 (void *)e + sizeof(__u32) ||
204 value_start + le16_to_cpu(entry->e_value_offs) +
205 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400206 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400207 entry = EXT4_XATTR_NEXT(entry);
208 }
209
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 return 0;
211}
212
213static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400214ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400216 int error;
217
218 if (buffer_verified(bh))
219 return 0;
220
Mingming Cao617ba132006-10-11 01:20:53 -0700221 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400223 return -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400224 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400225 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400226 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
227 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400228 if (!error)
229 set_buffer_verified(bh);
230 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231}
232
233static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700234ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235{
236 size_t value_size = le32_to_cpu(entry->e_value_size);
237
238 if (entry->e_value_block != 0 || value_size > size ||
239 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400240 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 return 0;
242}
243
244static int
Mingming Cao617ba132006-10-11 01:20:53 -0700245ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 const char *name, size_t size, int sorted)
247{
Mingming Cao617ba132006-10-11 01:20:53 -0700248 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 size_t name_len;
250 int cmp = 1;
251
252 if (name == NULL)
253 return -EINVAL;
254 name_len = strlen(name);
255 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700256 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 cmp = name_index - entry->e_name_index;
258 if (!cmp)
259 cmp = name_len - entry->e_name_len;
260 if (!cmp)
261 cmp = memcmp(name, entry->e_name, name_len);
262 if (cmp <= 0 && (sorted || cmp == 0))
263 break;
264 }
265 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700266 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400267 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 return cmp ? -ENODATA : 0;
269}
270
271static int
Mingming Cao617ba132006-10-11 01:20:53 -0700272ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 void *buffer, size_t buffer_size)
274{
275 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700276 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 size_t size;
278 int error;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400279 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280
281 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
282 name_index, name, buffer, (long)buffer_size);
283
284 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700285 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400287 ea_idebug(inode, "reading block %llu",
288 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700289 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 if (!bh)
291 goto cleanup;
292 ea_bdebug(bh, "b_count=%d, refcount=%d",
293 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400294 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500295bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400296 EXT4_ERROR_INODE(inode, "bad block %llu",
297 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400298 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 goto cleanup;
300 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400301 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700303 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400304 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 goto bad_block;
306 if (error)
307 goto cleanup;
308 size = le32_to_cpu(entry->e_value_size);
309 if (buffer) {
310 error = -ERANGE;
311 if (size > buffer_size)
312 goto cleanup;
313 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
314 size);
315 }
316 error = size;
317
318cleanup:
319 brelse(bh);
320 return error;
321}
322
Tao Ma879b3822012-12-05 10:28:46 -0500323int
Mingming Cao617ba132006-10-11 01:20:53 -0700324ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700325 void *buffer, size_t buffer_size)
326{
Mingming Cao617ba132006-10-11 01:20:53 -0700327 struct ext4_xattr_ibody_header *header;
328 struct ext4_xattr_entry *entry;
329 struct ext4_inode *raw_inode;
330 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 size_t size;
332 void *end;
333 int error;
334
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500335 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700337 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 if (error)
339 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700340 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341 header = IHDR(inode, raw_inode);
342 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700343 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400344 error = ext4_xattr_check_names(entry, end, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700345 if (error)
346 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700347 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348 end - (void *)entry, 0);
349 if (error)
350 goto cleanup;
351 size = le32_to_cpu(entry->e_value_size);
352 if (buffer) {
353 error = -ERANGE;
354 if (size > buffer_size)
355 goto cleanup;
356 memcpy(buffer, (void *)IFIRST(header) +
357 le16_to_cpu(entry->e_value_offs), size);
358 }
359 error = size;
360
361cleanup:
362 brelse(iloc.bh);
363 return error;
364}
365
366/*
Mingming Cao617ba132006-10-11 01:20:53 -0700367 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 *
369 * Copy an extended attribute into the buffer
370 * provided, or compute the buffer size required.
371 * Buffer is NULL to compute the size of the buffer required.
372 *
373 * Returns a negative error number on failure, or the number of bytes
374 * used / required on success.
375 */
376int
Mingming Cao617ba132006-10-11 01:20:53 -0700377ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378 void *buffer, size_t buffer_size)
379{
380 int error;
381
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400382 if (strlen(name) > 255)
383 return -ERANGE;
384
Mingming Cao617ba132006-10-11 01:20:53 -0700385 down_read(&EXT4_I(inode)->xattr_sem);
386 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700387 buffer_size);
388 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700389 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700390 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700391 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 return error;
393}
394
395static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000396ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700397 char *buffer, size_t buffer_size)
398{
399 size_t rest = buffer_size;
400
Mingming Cao617ba132006-10-11 01:20:53 -0700401 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700402 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700403 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404
405 if (handler) {
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200406 size_t size = handler->list(handler, dentry, buffer,
407 rest, entry->e_name,
408 entry->e_name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 if (buffer) {
410 if (size > rest)
411 return -ERANGE;
412 buffer += size;
413 }
414 rest -= size;
415 }
416 }
417 return buffer_size - rest;
418}
419
420static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000421ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422{
David Howells2b0143b2015-03-17 22:25:59 +0000423 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700424 struct buffer_head *bh = NULL;
425 int error;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400426 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427
428 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
429 buffer, (long)buffer_size);
430
431 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700432 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700433 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400434 ea_idebug(inode, "reading block %llu",
435 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700436 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437 error = -EIO;
438 if (!bh)
439 goto cleanup;
440 ea_bdebug(bh, "b_count=%d, refcount=%d",
441 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400442 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400443 EXT4_ERROR_INODE(inode, "bad block %llu",
444 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400445 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 goto cleanup;
447 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400448 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000449 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450
451cleanup:
452 brelse(bh);
453
454 return error;
455}
456
457static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000458ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459{
David Howells2b0143b2015-03-17 22:25:59 +0000460 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700461 struct ext4_xattr_ibody_header *header;
462 struct ext4_inode *raw_inode;
463 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464 void *end;
465 int error;
466
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500467 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700469 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 if (error)
471 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700472 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700474 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400475 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 if (error)
477 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000478 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 buffer, buffer_size);
480
481cleanup:
482 brelse(iloc.bh);
483 return error;
484}
485
486/*
Mingming Cao617ba132006-10-11 01:20:53 -0700487 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 *
489 * Copy a list of attribute names into the buffer
490 * provided, or compute the buffer size required.
491 * Buffer is NULL to compute the size of the buffer required.
492 *
493 * Returns a negative error number on failure, or the number of bytes
494 * used / required on success.
495 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400496static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000497ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500499 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500
David Howells2b0143b2015-03-17 22:25:59 +0000501 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500502 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
503 if (ret < 0)
504 goto errout;
505 if (buffer) {
506 buffer += ret;
507 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500509 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
510 if (ret < 0)
511 goto errout;
512 ret += ret2;
513errout:
David Howells2b0143b2015-03-17 22:25:59 +0000514 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500515 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516}
517
518/*
Mingming Cao617ba132006-10-11 01:20:53 -0700519 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 * not set, set it.
521 */
Mingming Cao617ba132006-10-11 01:20:53 -0700522static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 struct super_block *sb)
524{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400525 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526 return;
527
liang xie5d601252014-05-12 22:06:43 -0400528 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700529 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400530 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400531 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700533}
534
535/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400536 * Release the xattr block BH: If the reference count is > 1, decrement it;
537 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 */
539static void
Mingming Cao617ba132006-10-11 01:20:53 -0700540ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 struct buffer_head *bh)
542{
543 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800544 int error = 0;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400545 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400547 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
liang xie5d601252014-05-12 22:06:43 -0400548 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800549 error = ext4_journal_get_write_access(handle, bh);
550 if (error)
551 goto out;
552
553 lock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
555 ea_bdebug(bh, "refcount now=0; freeing");
556 if (ce)
557 mb_cache_entry_free(ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400559 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500560 ext4_free_blocks(handle, inode, bh, 0, 1,
561 EXT4_FREE_BLOCKS_METADATA |
562 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 } else {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400564 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500565 if (ce)
566 mb_cache_entry_release(ce);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400567 /*
568 * Beware of this ugliness: Releasing of xattr block references
569 * from different inodes can race and so we have to protect
570 * from a race where someone else frees the block (and releases
571 * its journal_head) before we are done dirtying the buffer. In
572 * nojournal mode this race is harmless and we actually cannot
573 * call ext4_handle_dirty_xattr_block() with locked buffer as
574 * that function can call sync_dirty_buffer() so for that case
575 * we handle the dirtying after unlocking the buffer.
576 */
577 if (ext4_handle_valid(handle))
578 error = ext4_handle_dirty_xattr_block(handle, inode,
579 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500580 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400581 if (!ext4_handle_valid(handle))
582 error = ext4_handle_dirty_xattr_block(handle, inode,
583 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800584 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500585 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500586 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800587 ea_bdebug(bh, "refcount now=%d; releasing",
588 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800590out:
591 ext4_std_error(inode->i_sb, error);
592 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593}
594
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400595/*
596 * Find the available free space for EAs. This also returns the total number of
597 * bytes used by EA entries.
598 */
599static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
600 size_t *min_offs, void *base, int *total)
601{
602 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400603 if (!last->e_value_block && last->e_value_size) {
604 size_t offs = le16_to_cpu(last->e_value_offs);
605 if (offs < *min_offs)
606 *min_offs = offs;
607 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500608 if (total)
609 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400610 }
611 return (*min_offs - ((void *)last - base) - sizeof(__u32));
612}
613
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614static int
Mingming Cao617ba132006-10-11 01:20:53 -0700615ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616{
Mingming Cao617ba132006-10-11 01:20:53 -0700617 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
619
620 /* Compute min_offs and last. */
621 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700622 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 if (!last->e_value_block && last->e_value_size) {
624 size_t offs = le16_to_cpu(last->e_value_offs);
625 if (offs < min_offs)
626 min_offs = offs;
627 }
628 }
629 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
630 if (!s->not_found) {
631 if (!s->here->e_value_block && s->here->e_value_size) {
632 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700633 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700634 }
Mingming Cao617ba132006-10-11 01:20:53 -0700635 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 }
637 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400638 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700639 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 return -ENOSPC;
641 }
642
643 if (i->value && s->not_found) {
644 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700645 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
647 memmove((void *)s->here + size, s->here, rest);
648 memset(s->here, 0, size);
649 s->here->e_name_index = i->name_index;
650 s->here->e_name_len = name_len;
651 memcpy(s->here->e_name, i->name, name_len);
652 } else {
653 if (!s->here->e_value_block && s->here->e_value_size) {
654 void *first_val = s->base + min_offs;
655 size_t offs = le16_to_cpu(s->here->e_value_offs);
656 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700657 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 le32_to_cpu(s->here->e_value_size));
659
Mingming Cao617ba132006-10-11 01:20:53 -0700660 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 /* The old and the new value have the same
662 size. Just replace. */
663 s->here->e_value_size =
664 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500665 if (i->value == EXT4_ZERO_XATTR_VALUE) {
666 memset(val, 0, size);
667 } else {
668 /* Clear pad bytes first. */
669 memset(val + size - EXT4_XATTR_PAD, 0,
670 EXT4_XATTR_PAD);
671 memcpy(val, i->value, i->value_len);
672 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 return 0;
674 }
675
676 /* Remove the old value. */
677 memmove(first_val + size, first_val, val - first_val);
678 memset(first_val, 0, size);
679 s->here->e_value_size = 0;
680 s->here->e_value_offs = 0;
681 min_offs += size;
682
683 /* Adjust all value offsets. */
684 last = s->first;
685 while (!IS_LAST_ENTRY(last)) {
686 size_t o = le16_to_cpu(last->e_value_offs);
687 if (!last->e_value_block &&
688 last->e_value_size && o < offs)
689 last->e_value_offs =
690 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700691 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 }
693 }
694 if (!i->value) {
695 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700696 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 last = ENTRY((void *)last - size);
698 memmove(s->here, (void *)s->here + size,
699 (void *)last - (void *)s->here + sizeof(__u32));
700 memset(last, 0, size);
701 }
702 }
703
704 if (i->value) {
705 /* Insert the new value. */
706 s->here->e_value_size = cpu_to_le32(i->value_len);
707 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700708 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 void *val = s->base + min_offs - size;
710 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500711 if (i->value == EXT4_ZERO_XATTR_VALUE) {
712 memset(val, 0, size);
713 } else {
714 /* Clear the pad bytes first. */
715 memset(val + size - EXT4_XATTR_PAD, 0,
716 EXT4_XATTR_PAD);
717 memcpy(val, i->value, i->value_len);
718 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 }
720 }
721 return 0;
722}
723
Mingming Cao617ba132006-10-11 01:20:53 -0700724struct ext4_xattr_block_find {
725 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 struct buffer_head *bh;
727};
728
729static int
Mingming Cao617ba132006-10-11 01:20:53 -0700730ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
731 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732{
733 struct super_block *sb = inode->i_sb;
734 int error;
735
736 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
737 i->name_index, i->name, i->value, (long)i->value_len);
738
Mingming Cao617ba132006-10-11 01:20:53 -0700739 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700741 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 error = -EIO;
743 if (!bs->bh)
744 goto cleanup;
745 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
746 atomic_read(&(bs->bh->b_count)),
747 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400748 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400749 EXT4_ERROR_INODE(inode, "bad block %llu",
750 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400751 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 goto cleanup;
753 }
754 /* Find the named attribute. */
755 bs->s.base = BHDR(bs->bh);
756 bs->s.first = BFIRST(bs->bh);
757 bs->s.end = bs->bh->b_data + bs->bh->b_size;
758 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700759 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 i->name, bs->bh->b_size, 1);
761 if (error && error != -ENODATA)
762 goto cleanup;
763 bs->s.not_found = error;
764 }
765 error = 0;
766
767cleanup:
768 return error;
769}
770
771static int
Mingming Cao617ba132006-10-11 01:20:53 -0700772ext4_xattr_block_set(handle_t *handle, struct inode *inode,
773 struct ext4_xattr_info *i,
774 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775{
776 struct super_block *sb = inode->i_sb;
777 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700778 struct ext4_xattr_search *s = &bs->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800780 int error = 0;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400781 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782
Mingming Cao617ba132006-10-11 01:20:53 -0700783#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784
785 if (i->value && i->value_len > sb->s_blocksize)
786 return -ENOSPC;
787 if (s->base) {
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400788 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 bs->bh->b_blocknr);
liang xie5d601252014-05-12 22:06:43 -0400790 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800791 error = ext4_journal_get_write_access(handle, bs->bh);
792 if (error)
793 goto cleanup;
794 lock_buffer(bs->bh);
795
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
797 if (ce) {
798 mb_cache_entry_free(ce);
799 ce = NULL;
800 }
801 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700802 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803 if (!error) {
804 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700805 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400807 ext4_xattr_cache_insert(ext4_mb_cache,
808 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 }
810 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400811 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812 goto bad_block;
813 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400814 error = ext4_handle_dirty_xattr_block(handle,
815 inode,
816 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 if (error)
818 goto cleanup;
819 goto inserted;
820 } else {
821 int offset = (char *)s->here - bs->bh->b_data;
822
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800823 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 if (ce) {
825 mb_cache_entry_release(ce);
826 ce = NULL;
827 }
828 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400829 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 error = -ENOMEM;
831 if (s->base == NULL)
832 goto cleanup;
833 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
834 s->first = ENTRY(header(s->base)+1);
835 header(s->base)->h_refcount = cpu_to_le32(1);
836 s->here = ENTRY(s->base + offset);
837 s->end = s->base + bs->bh->b_size;
838 }
839 } else {
840 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400841 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 /* assert(header == s->base) */
843 error = -ENOMEM;
844 if (s->base == NULL)
845 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700846 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847 header(s->base)->h_blocks = cpu_to_le32(1);
848 header(s->base)->h_refcount = cpu_to_le32(1);
849 s->first = ENTRY(header(s->base)+1);
850 s->here = ENTRY(header(s->base)+1);
851 s->end = s->base + sb->s_blocksize;
852 }
853
Mingming Cao617ba132006-10-11 01:20:53 -0700854 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400855 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 goto bad_block;
857 if (error)
858 goto cleanup;
859 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700860 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861
862inserted:
863 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700864 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 if (new_bh) {
866 /* We found an identical block in the cache. */
867 if (new_bh == bs->bh)
868 ea_bdebug(new_bh, "keeping");
869 else {
870 /* The old block is released after updating
871 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500872 error = dquot_alloc_block(inode,
873 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500874 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400876 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700877 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878 new_bh);
879 if (error)
880 goto cleanup_dquot;
881 lock_buffer(new_bh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400882 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883 ea_bdebug(new_bh, "reusing; refcount now=%d",
884 le32_to_cpu(BHDR(new_bh)->h_refcount));
885 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400886 error = ext4_handle_dirty_xattr_block(handle,
887 inode,
888 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 if (error)
890 goto cleanup_dquot;
891 }
892 mb_cache_entry_release(ce);
893 ce = NULL;
894 } else if (bs->bh && s->base == bs->bh->b_data) {
895 /* We were modifying this block in-place. */
896 ea_bdebug(bs->bh, "keeping this block");
897 new_bh = bs->bh;
898 get_bh(new_bh);
899 } else {
900 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400901 ext4_fsblk_t goal, block;
902
903 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400904 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400905
906 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400907 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400908 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
909
Allison Henderson55f020d2011-05-25 07:41:26 -0400910 block = ext4_new_meta_blocks(handle, inode, goal, 0,
911 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 if (error)
913 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400914
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400915 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400916 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
917
Joe Perchesace36ad2012-03-19 23:11:43 -0400918 ea_idebug(inode, "creating block %llu",
919 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920
921 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500922 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500923 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500925 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500926 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 goto cleanup;
928 }
929 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700930 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931 if (error) {
932 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500933 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 goto getblk_failed;
935 }
936 memcpy(new_bh->b_data, s->base, new_bh->b_size);
937 set_buffer_uptodate(new_bh);
938 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400939 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400940 error = ext4_handle_dirty_xattr_block(handle,
941 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942 if (error)
943 goto cleanup;
944 }
945 }
946
947 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -0700948 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949
950 /* Drop the previous xattr block. */
951 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700952 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 error = 0;
954
955cleanup:
956 if (ce)
957 mb_cache_entry_release(ce);
958 brelse(new_bh);
959 if (!(bs->bh && s->base == bs->bh->b_data))
960 kfree(s->base);
961
962 return error;
963
964cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500965 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 goto cleanup;
967
968bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400969 EXT4_ERROR_INODE(inode, "bad block %llu",
970 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 goto cleanup;
972
973#undef header
974}
975
Tao Ma879b3822012-12-05 10:28:46 -0500976int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
977 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978{
Mingming Cao617ba132006-10-11 01:20:53 -0700979 struct ext4_xattr_ibody_header *header;
980 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 int error;
982
Mingming Cao617ba132006-10-11 01:20:53 -0700983 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700985 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 header = IHDR(inode, raw_inode);
987 is->s.base = is->s.first = IFIRST(header);
988 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700989 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500990 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400991 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
992 IFIRST(header));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 if (error)
994 return error;
995 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -0700996 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700997 i->name, is->s.end -
998 (void *)is->s.base, 0);
999 if (error && error != -ENODATA)
1000 return error;
1001 is->s.not_found = error;
1002 }
1003 return 0;
1004}
1005
Tao Ma0d812f72012-12-10 14:06:02 -05001006int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1007 struct ext4_xattr_info *i,
1008 struct ext4_xattr_ibody_find *is)
1009{
1010 struct ext4_xattr_ibody_header *header;
1011 struct ext4_xattr_search *s = &is->s;
1012 int error;
1013
1014 if (EXT4_I(inode)->i_extra_isize == 0)
1015 return -ENOSPC;
1016 error = ext4_xattr_set_entry(i, s);
1017 if (error) {
1018 if (error == -ENOSPC &&
1019 ext4_has_inline_data(inode)) {
1020 error = ext4_try_to_evict_inline_data(handle, inode,
1021 EXT4_XATTR_LEN(strlen(i->name) +
1022 EXT4_XATTR_SIZE(i->value_len)));
1023 if (error)
1024 return error;
1025 error = ext4_xattr_ibody_find(inode, i, is);
1026 if (error)
1027 return error;
1028 error = ext4_xattr_set_entry(i, s);
1029 }
1030 if (error)
1031 return error;
1032 }
1033 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1034 if (!IS_LAST_ENTRY(s->first)) {
1035 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1036 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1037 } else {
1038 header->h_magic = cpu_to_le32(0);
1039 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1040 }
1041 return 0;
1042}
1043
1044static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1045 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_xattr_search *s = &is->s;
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 -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001054 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 if (error)
1056 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001057 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001059 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001060 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 } else {
1062 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001063 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 }
1065 return 0;
1066}
1067
1068/*
Mingming Cao617ba132006-10-11 01:20:53 -07001069 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001071 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 * is NULL to remove an existing extended attribute, and non-NULL to
1073 * either replace an existing extended attribute, or create a new extended
1074 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1075 * specify that an extended attribute must exist and must not exist
1076 * previous to the call, respectively.
1077 *
1078 * Returns 0, or a negative error number on failure.
1079 */
1080int
Mingming Cao617ba132006-10-11 01:20:53 -07001081ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082 const char *name, const void *value, size_t value_len,
1083 int flags)
1084{
Mingming Cao617ba132006-10-11 01:20:53 -07001085 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 .name_index = name_index,
1087 .name = name,
1088 .value = value,
1089 .value_len = value_len,
1090
1091 };
Mingming Cao617ba132006-10-11 01:20:53 -07001092 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 .s = { .not_found = -ENODATA, },
1094 };
Mingming Cao617ba132006-10-11 01:20:53 -07001095 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096 .s = { .not_found = -ENODATA, },
1097 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001098 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001099 int error;
1100
1101 if (!name)
1102 return -EINVAL;
1103 if (strlen(name) > 255)
1104 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001105 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001106 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1107 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001108
Eric Sandeen66543612011-10-26 03:32:07 -04001109 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001110 if (error)
1111 goto cleanup;
1112
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001113 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001114 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1115 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001116 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 }
1118
Mingming Cao617ba132006-10-11 01:20:53 -07001119 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 if (error)
1121 goto cleanup;
1122 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001123 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 if (error)
1125 goto cleanup;
1126 if (is.s.not_found && bs.s.not_found) {
1127 error = -ENODATA;
1128 if (flags & XATTR_REPLACE)
1129 goto cleanup;
1130 error = 0;
1131 if (!value)
1132 goto cleanup;
1133 } else {
1134 error = -EEXIST;
1135 if (flags & XATTR_CREATE)
1136 goto cleanup;
1137 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 if (!value) {
1139 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001140 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001142 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001144 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145 if (!error && !bs.s.not_found) {
1146 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001147 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001149 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1150 error = ext4_xattr_block_find(inode, &i, &bs);
1151 if (error)
1152 goto cleanup;
1153 }
Mingming Cao617ba132006-10-11 01:20:53 -07001154 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155 if (error)
1156 goto cleanup;
1157 if (!is.s.not_found) {
1158 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001159 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 &is);
1161 }
1162 }
1163 }
1164 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001165 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001166 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001167 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001168 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001169 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001171 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 * error != 0.
1173 */
1174 is.iloc.bh = NULL;
1175 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001176 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 }
1178
1179cleanup:
1180 brelse(is.iloc.bh);
1181 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001182 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001183 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001184 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 return error;
1186}
1187
1188/*
Mingming Cao617ba132006-10-11 01:20:53 -07001189 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001190 *
Mingming Cao617ba132006-10-11 01:20:53 -07001191 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 * attribute modification is a filesystem transaction by itself.
1193 *
1194 * Returns 0, or a negative error number on failure.
1195 */
1196int
Mingming Cao617ba132006-10-11 01:20:53 -07001197ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198 const void *value, size_t value_len, int flags)
1199{
1200 handle_t *handle;
1201 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001202 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203
1204retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001205 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 if (IS_ERR(handle)) {
1207 error = PTR_ERR(handle);
1208 } else {
1209 int error2;
1210
Mingming Cao617ba132006-10-11 01:20:53 -07001211 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001213 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001214 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001215 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 goto retry;
1217 if (error == 0)
1218 error = error2;
1219 }
1220
1221 return error;
1222}
1223
1224/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001225 * Shift the EA entries in the inode to create space for the increased
1226 * i_extra_isize.
1227 */
1228static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1229 int value_offs_shift, void *to,
1230 void *from, size_t n, int blocksize)
1231{
1232 struct ext4_xattr_entry *last = entry;
1233 int new_offs;
1234
1235 /* Adjust the value offsets of the entries */
1236 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1237 if (!last->e_value_block && last->e_value_size) {
1238 new_offs = le16_to_cpu(last->e_value_offs) +
1239 value_offs_shift;
1240 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1241 > blocksize);
1242 last->e_value_offs = cpu_to_le16(new_offs);
1243 }
1244 }
1245 /* Shift the entries by n bytes */
1246 memmove(to, from, n);
1247}
1248
1249/*
1250 * Expand an inode by new_extra_isize bytes when EAs are present.
1251 * Returns 0 on success or negative error number on failure.
1252 */
1253int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1254 struct ext4_inode *raw_inode, handle_t *handle)
1255{
1256 struct ext4_xattr_ibody_header *header;
1257 struct ext4_xattr_entry *entry, *last, *first;
1258 struct buffer_head *bh = NULL;
1259 struct ext4_xattr_ibody_find *is = NULL;
1260 struct ext4_xattr_block_find *bs = NULL;
1261 char *buffer = NULL, *b_entry_name = NULL;
1262 size_t min_offs, free;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001263 int total_ino;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001264 void *base, *start, *end;
1265 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001266 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001267
1268 down_write(&EXT4_I(inode)->xattr_sem);
1269retry:
1270 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1271 up_write(&EXT4_I(inode)->xattr_sem);
1272 return 0;
1273 }
1274
1275 header = IHDR(inode, raw_inode);
1276 entry = IFIRST(header);
1277
1278 /*
1279 * Check if enough free space is available in the inode to shift the
1280 * entries ahead by new_extra_isize.
1281 */
1282
1283 base = start = entry;
1284 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1285 min_offs = end - base;
1286 last = entry;
1287 total_ino = sizeof(struct ext4_xattr_ibody_header);
1288
1289 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1290 if (free >= new_extra_isize) {
1291 entry = IFIRST(header);
1292 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1293 - new_extra_isize, (void *)raw_inode +
1294 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1295 (void *)header, total_ino,
1296 inode->i_sb->s_blocksize);
1297 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1298 error = 0;
1299 goto cleanup;
1300 }
1301
1302 /*
1303 * Enough free space isn't available in the inode, check if
1304 * EA block can hold new_extra_isize bytes.
1305 */
1306 if (EXT4_I(inode)->i_file_acl) {
1307 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1308 error = -EIO;
1309 if (!bh)
1310 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001311 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001312 EXT4_ERROR_INODE(inode, "bad block %llu",
1313 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001314 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001315 goto cleanup;
1316 }
1317 base = BHDR(bh);
1318 first = BFIRST(bh);
1319 end = bh->b_data + bh->b_size;
1320 min_offs = end - base;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001321 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001322 if (free < new_extra_isize) {
1323 if (!tried_min_extra_isize && s_min_extra_isize) {
1324 tried_min_extra_isize++;
1325 new_extra_isize = s_min_extra_isize;
1326 brelse(bh);
1327 goto retry;
1328 }
1329 error = -1;
1330 goto cleanup;
1331 }
1332 } else {
1333 free = inode->i_sb->s_blocksize;
1334 }
1335
1336 while (new_extra_isize > 0) {
1337 size_t offs, size, entry_size;
1338 struct ext4_xattr_entry *small_entry = NULL;
1339 struct ext4_xattr_info i = {
1340 .value = NULL,
1341 .value_len = 0,
1342 };
1343 unsigned int total_size; /* EA entry size + value size */
1344 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1345 unsigned int min_total_size = ~0U;
1346
1347 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1348 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1349 if (!is || !bs) {
1350 error = -ENOMEM;
1351 goto cleanup;
1352 }
1353
1354 is->s.not_found = -ENODATA;
1355 bs->s.not_found = -ENODATA;
1356 is->iloc.bh = NULL;
1357 bs->bh = NULL;
1358
1359 last = IFIRST(header);
1360 /* Find the entry best suited to be pushed into EA block */
1361 entry = NULL;
1362 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1363 total_size =
1364 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1365 EXT4_XATTR_LEN(last->e_name_len);
1366 if (total_size <= free && total_size < min_total_size) {
1367 if (total_size < new_extra_isize) {
1368 small_entry = last;
1369 } else {
1370 entry = last;
1371 min_total_size = total_size;
1372 }
1373 }
1374 }
1375
1376 if (entry == NULL) {
1377 if (small_entry) {
1378 entry = small_entry;
1379 } else {
1380 if (!tried_min_extra_isize &&
1381 s_min_extra_isize) {
1382 tried_min_extra_isize++;
1383 new_extra_isize = s_min_extra_isize;
Dave Jones6e4ea8e2013-10-10 20:05:35 -04001384 kfree(is); is = NULL;
1385 kfree(bs); bs = NULL;
Theodore Ts'odcb99172013-10-31 23:00:24 -04001386 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001387 goto retry;
1388 }
1389 error = -1;
1390 goto cleanup;
1391 }
1392 }
1393 offs = le16_to_cpu(entry->e_value_offs);
1394 size = le32_to_cpu(entry->e_value_size);
1395 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1396 i.name_index = entry->e_name_index,
1397 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1398 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1399 if (!buffer || !b_entry_name) {
1400 error = -ENOMEM;
1401 goto cleanup;
1402 }
1403 /* Save the entry name and the entry value */
1404 memcpy(buffer, (void *)IFIRST(header) + offs,
1405 EXT4_XATTR_SIZE(size));
1406 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1407 b_entry_name[entry->e_name_len] = '\0';
1408 i.name = b_entry_name;
1409
1410 error = ext4_get_inode_loc(inode, &is->iloc);
1411 if (error)
1412 goto cleanup;
1413
1414 error = ext4_xattr_ibody_find(inode, &i, is);
1415 if (error)
1416 goto cleanup;
1417
1418 /* Remove the chosen entry from the inode */
1419 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001420 if (error)
1421 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001422
1423 entry = IFIRST(header);
1424 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1425 shift_bytes = new_extra_isize;
1426 else
1427 shift_bytes = entry_size + size;
1428 /* Adjust the offsets and shift the remaining entries ahead */
1429 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1430 shift_bytes, (void *)raw_inode +
1431 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1432 (void *)header, total_ino - entry_size,
1433 inode->i_sb->s_blocksize);
1434
1435 extra_isize += shift_bytes;
1436 new_extra_isize -= shift_bytes;
1437 EXT4_I(inode)->i_extra_isize = extra_isize;
1438
1439 i.name = b_entry_name;
1440 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001441 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001442 error = ext4_xattr_block_find(inode, &i, bs);
1443 if (error)
1444 goto cleanup;
1445
1446 /* Add entry which was removed from the inode into the block */
1447 error = ext4_xattr_block_set(handle, inode, &i, bs);
1448 if (error)
1449 goto cleanup;
1450 kfree(b_entry_name);
1451 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001452 b_entry_name = NULL;
1453 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001454 brelse(is->iloc.bh);
1455 kfree(is);
1456 kfree(bs);
1457 }
1458 brelse(bh);
1459 up_write(&EXT4_I(inode)->xattr_sem);
1460 return 0;
1461
1462cleanup:
1463 kfree(b_entry_name);
1464 kfree(buffer);
1465 if (is)
1466 brelse(is->iloc.bh);
1467 kfree(is);
1468 kfree(bs);
1469 brelse(bh);
1470 up_write(&EXT4_I(inode)->xattr_sem);
1471 return error;
1472}
1473
1474
1475
1476/*
Mingming Cao617ba132006-10-11 01:20:53 -07001477 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001478 *
1479 * Free extended attribute resources associated with this inode. This
1480 * is called immediately before an inode is freed. We have exclusive
1481 * access to the inode.
1482 */
1483void
Mingming Cao617ba132006-10-11 01:20:53 -07001484ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485{
1486 struct buffer_head *bh = NULL;
1487
Mingming Cao617ba132006-10-11 01:20:53 -07001488 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001490 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001491 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001492 EXT4_ERROR_INODE(inode, "block %llu read error",
1493 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494 goto cleanup;
1495 }
Mingming Cao617ba132006-10-11 01:20:53 -07001496 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001498 EXT4_ERROR_INODE(inode, "bad block %llu",
1499 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500 goto cleanup;
1501 }
Mingming Cao617ba132006-10-11 01:20:53 -07001502 ext4_xattr_release_block(handle, inode, bh);
1503 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504
1505cleanup:
1506 brelse(bh);
1507}
1508
1509/*
Mingming Cao617ba132006-10-11 01:20:53 -07001510 * ext4_xattr_put_super()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 *
1512 * This is called when a file system is unmounted.
1513 */
1514void
Mingming Cao617ba132006-10-11 01:20:53 -07001515ext4_xattr_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516{
1517 mb_cache_shrink(sb->s_bdev);
1518}
1519
1520/*
Mingming Cao617ba132006-10-11 01:20:53 -07001521 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 *
1523 * Create a new entry in the extended attribute cache, and insert
1524 * it unless such an entry is already in the cache.
1525 *
1526 * Returns 0, or a negative error number on failure.
1527 */
1528static void
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001529ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530{
1531 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1532 struct mb_cache_entry *ce;
1533 int error;
1534
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001535 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 if (!ce) {
1537 ea_bdebug(bh, "out of memory");
1538 return;
1539 }
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001540 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001541 if (error) {
1542 mb_cache_entry_free(ce);
1543 if (error == -EBUSY) {
1544 ea_bdebug(bh, "already in cache");
1545 error = 0;
1546 }
1547 } else {
1548 ea_bdebug(bh, "inserting [%x]", (int)hash);
1549 mb_cache_entry_release(ce);
1550 }
1551}
1552
1553/*
Mingming Cao617ba132006-10-11 01:20:53 -07001554 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 *
1556 * Compare two extended attribute blocks for equality.
1557 *
1558 * Returns 0 if the blocks are equal, 1 if they differ, and
1559 * a negative error number on errors.
1560 */
1561static int
Mingming Cao617ba132006-10-11 01:20:53 -07001562ext4_xattr_cmp(struct ext4_xattr_header *header1,
1563 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564{
Mingming Cao617ba132006-10-11 01:20:53 -07001565 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001566
1567 entry1 = ENTRY(header1+1);
1568 entry2 = ENTRY(header2+1);
1569 while (!IS_LAST_ENTRY(entry1)) {
1570 if (IS_LAST_ENTRY(entry2))
1571 return 1;
1572 if (entry1->e_hash != entry2->e_hash ||
1573 entry1->e_name_index != entry2->e_name_index ||
1574 entry1->e_name_len != entry2->e_name_len ||
1575 entry1->e_value_size != entry2->e_value_size ||
1576 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1577 return 1;
1578 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001579 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1581 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1582 le32_to_cpu(entry1->e_value_size)))
1583 return 1;
1584
Mingming Cao617ba132006-10-11 01:20:53 -07001585 entry1 = EXT4_XATTR_NEXT(entry1);
1586 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587 }
1588 if (!IS_LAST_ENTRY(entry2))
1589 return 1;
1590 return 0;
1591}
1592
1593/*
Mingming Cao617ba132006-10-11 01:20:53 -07001594 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 *
1596 * Find an identical extended attribute block.
1597 *
1598 * Returns a pointer to the block found, or NULL if such a block was
1599 * not found or an error occurred.
1600 */
1601static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001602ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 struct mb_cache_entry **pce)
1604{
1605 __u32 hash = le32_to_cpu(header->h_hash);
1606 struct mb_cache_entry *ce;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001607 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608
1609 if (!header->h_hash)
1610 return NULL; /* never share */
1611 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1612again:
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001613 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001614 hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 while (ce) {
1616 struct buffer_head *bh;
1617
1618 if (IS_ERR(ce)) {
1619 if (PTR_ERR(ce) == -EAGAIN)
1620 goto again;
1621 break;
1622 }
1623 bh = sb_bread(inode->i_sb, ce->e_block);
1624 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001625 EXT4_ERROR_INODE(inode, "block %lu read error",
1626 (unsigned long) ce->e_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
Mingming Cao617ba132006-10-11 01:20:53 -07001628 EXT4_XATTR_REFCOUNT_MAX) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 ea_idebug(inode, "block %lu refcount %d>=%d",
1630 (unsigned long) ce->e_block,
1631 le32_to_cpu(BHDR(bh)->h_refcount),
Mingming Cao617ba132006-10-11 01:20:53 -07001632 EXT4_XATTR_REFCOUNT_MAX);
1633 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 *pce = ce;
1635 return bh;
1636 }
1637 brelse(bh);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001638 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639 }
1640 return NULL;
1641}
1642
1643#define NAME_HASH_SHIFT 5
1644#define VALUE_HASH_SHIFT 16
1645
1646/*
Mingming Cao617ba132006-10-11 01:20:53 -07001647 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001648 *
1649 * Compute the hash of an extended attribute.
1650 */
Mingming Cao617ba132006-10-11 01:20:53 -07001651static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1652 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653{
1654 __u32 hash = 0;
1655 char *name = entry->e_name;
1656 int n;
1657
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001658 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 hash = (hash << NAME_HASH_SHIFT) ^
1660 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1661 *name++;
1662 }
1663
1664 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1665 __le32 *value = (__le32 *)((char *)header +
1666 le16_to_cpu(entry->e_value_offs));
1667 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001668 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669 hash = (hash << VALUE_HASH_SHIFT) ^
1670 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1671 le32_to_cpu(*value++);
1672 }
1673 }
1674 entry->e_hash = cpu_to_le32(hash);
1675}
1676
1677#undef NAME_HASH_SHIFT
1678#undef VALUE_HASH_SHIFT
1679
1680#define BLOCK_HASH_SHIFT 16
1681
1682/*
Mingming Cao617ba132006-10-11 01:20:53 -07001683 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 *
1685 * Re-compute the extended attribute hash value after an entry has changed.
1686 */
Mingming Cao617ba132006-10-11 01:20:53 -07001687static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1688 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689{
Mingming Cao617ba132006-10-11 01:20:53 -07001690 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 __u32 hash = 0;
1692
Mingming Cao617ba132006-10-11 01:20:53 -07001693 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 here = ENTRY(header+1);
1695 while (!IS_LAST_ENTRY(here)) {
1696 if (!here->e_hash) {
1697 /* Block is not shared if an entry's hash value == 0 */
1698 hash = 0;
1699 break;
1700 }
1701 hash = (hash << BLOCK_HASH_SHIFT) ^
1702 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1703 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001704 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705 }
1706 header->h_hash = cpu_to_le32(hash);
1707}
1708
1709#undef BLOCK_HASH_SHIFT
1710
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001711#define HASH_BUCKET_BITS 10
1712
1713struct mb_cache *
1714ext4_xattr_create_cache(char *name)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001715{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001716 return mb_cache_create(name, HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717}
1718
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001719void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001721 if (cache)
1722 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001724