blob: e9b9afdd1d964ab3bfca8dcc55e26976c5f8b45e [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 { \
71 char b[BDEVNAME_SIZE]; \
72 printk(KERN_DEBUG "block %s:%lu: ", \
73 bdevname(bh->b_bdev, b), \
74 (unsigned long) bh->b_blocknr); \
75 printk(f); \
76 printk("\n"); \
77 } while (0)
78#else
Joe Perchesace36ad2012-03-19 23:11:43 -040079# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
80# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081#endif
82
T Makphaibulchoke9c191f72014-03-18 19:24:49 -040083static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070084static struct buffer_head *ext4_xattr_cache_find(struct inode *,
85 struct ext4_xattr_header *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070087static void ext4_xattr_rehash(struct ext4_xattr_header *,
88 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000089static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040090 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091
Stephen Hemminger11e27522010-05-13 17:53:18 -070092static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070093 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040094#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080095 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
96 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097#endif
Mingming Cao617ba132006-10-11 01:20:53 -070098 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040099#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700100 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700101#endif
102};
103
Stephen Hemminger11e27522010-05-13 17:53:18 -0700104const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700105 &ext4_xattr_user_handler,
106 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400107#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800108 &posix_acl_access_xattr_handler,
109 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700110#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400111#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700112 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700113#endif
114 NULL
115};
116
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400117#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
118 inode->i_sb->s_fs_info)->s_mb_cache)
119
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400120static __le32 ext4_xattr_block_csum(struct inode *inode,
121 sector_t block_nr,
122 struct ext4_xattr_header *hdr)
123{
124 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400125 __u32 csum;
126 __le32 save_csum;
127 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400128
Theodore Ts'od6a77102013-04-09 23:59:55 -0400129 save_csum = hdr->h_checksum;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400130 hdr->h_checksum = 0;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400131 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
132 sizeof(dsk_block_nr));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400133 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
134 EXT4_BLOCK_SIZE(inode->i_sb));
Tao Ma41eb70d2012-07-09 16:29:27 -0400135
Theodore Ts'od6a77102013-04-09 23:59:55 -0400136 hdr->h_checksum = save_csum;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400137 return cpu_to_le32(csum);
138}
139
140static int ext4_xattr_block_csum_verify(struct inode *inode,
141 sector_t block_nr,
142 struct ext4_xattr_header *hdr)
143{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400144 if (ext4_has_metadata_csum(inode->i_sb) &&
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400145 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
146 return 0;
147 return 1;
148}
149
150static void ext4_xattr_block_csum_set(struct inode *inode,
151 sector_t block_nr,
152 struct ext4_xattr_header *hdr)
153{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400154 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400155 return;
156
157 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
158}
159
160static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
161 struct inode *inode,
162 struct buffer_head *bh)
163{
164 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
165 return ext4_handle_dirty_metadata(handle, inode, bh);
166}
167
Stephen Hemminger11e27522010-05-13 17:53:18 -0700168static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700169ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700171 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172
Mingming Cao617ba132006-10-11 01:20:53 -0700173 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
174 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175 return handler;
176}
177
178/*
179 * Inode operation listxattr()
180 *
David Howells2b0143b2015-03-17 22:25:59 +0000181 * d_inode(dentry)->i_mutex: don't care
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 */
183ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700184ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000186 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187}
188
189static int
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400190ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
191 void *value_start)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192{
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400193 struct ext4_xattr_entry *e = entry;
194
195 while (!IS_LAST_ENTRY(e)) {
196 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 if ((void *)next >= end)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400198 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400199 e = next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200 }
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400201
202 while (!IS_LAST_ENTRY(entry)) {
203 if (entry->e_value_size != 0 &&
204 (value_start + le16_to_cpu(entry->e_value_offs) <
205 (void *)e + sizeof(__u32) ||
206 value_start + le16_to_cpu(entry->e_value_offs) +
207 le32_to_cpu(entry->e_value_size) > end))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400208 return -EFSCORRUPTED;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400209 entry = EXT4_XATTR_NEXT(entry);
210 }
211
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212 return 0;
213}
214
215static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400216ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400218 int error;
219
220 if (buffer_verified(bh))
221 return 0;
222
Mingming Cao617ba132006-10-11 01:20:53 -0700223 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 BHDR(bh)->h_blocks != cpu_to_le32(1))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400225 return -EFSCORRUPTED;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400226 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400227 return -EFSBADCRC;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400228 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
229 bh->b_data);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400230 if (!error)
231 set_buffer_verified(bh);
232 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233}
234
235static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700236ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237{
238 size_t value_size = le32_to_cpu(entry->e_value_size);
239
240 if (entry->e_value_block != 0 || value_size > size ||
241 le16_to_cpu(entry->e_value_offs) + value_size > size)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400242 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243 return 0;
244}
245
246static int
Mingming Cao617ba132006-10-11 01:20:53 -0700247ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 const char *name, size_t size, int sorted)
249{
Mingming Cao617ba132006-10-11 01:20:53 -0700250 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 size_t name_len;
252 int cmp = 1;
253
254 if (name == NULL)
255 return -EINVAL;
256 name_len = strlen(name);
257 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700258 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 cmp = name_index - entry->e_name_index;
260 if (!cmp)
261 cmp = name_len - entry->e_name_len;
262 if (!cmp)
263 cmp = memcmp(name, entry->e_name, name_len);
264 if (cmp <= 0 && (sorted || cmp == 0))
265 break;
266 }
267 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700268 if (!cmp && ext4_xattr_check_entry(entry, size))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400269 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 return cmp ? -ENODATA : 0;
271}
272
273static int
Mingming Cao617ba132006-10-11 01:20:53 -0700274ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 void *buffer, size_t buffer_size)
276{
277 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700278 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 size_t size;
280 int error;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400281 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282
283 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
284 name_index, name, buffer, (long)buffer_size);
285
286 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700287 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400289 ea_idebug(inode, "reading block %llu",
290 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700291 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 if (!bh)
293 goto cleanup;
294 ea_bdebug(bh, "b_count=%d, refcount=%d",
295 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400296 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500297bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400298 EXT4_ERROR_INODE(inode, "bad block %llu",
299 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400300 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 goto cleanup;
302 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400303 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700305 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400306 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307 goto bad_block;
308 if (error)
309 goto cleanup;
310 size = le32_to_cpu(entry->e_value_size);
311 if (buffer) {
312 error = -ERANGE;
313 if (size > buffer_size)
314 goto cleanup;
315 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
316 size);
317 }
318 error = size;
319
320cleanup:
321 brelse(bh);
322 return error;
323}
324
Tao Ma879b3822012-12-05 10:28:46 -0500325int
Mingming Cao617ba132006-10-11 01:20:53 -0700326ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 void *buffer, size_t buffer_size)
328{
Mingming Cao617ba132006-10-11 01:20:53 -0700329 struct ext4_xattr_ibody_header *header;
330 struct ext4_xattr_entry *entry;
331 struct ext4_inode *raw_inode;
332 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 size_t size;
334 void *end;
335 int error;
336
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500337 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700339 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 if (error)
341 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700342 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 header = IHDR(inode, raw_inode);
344 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700345 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400346 error = ext4_xattr_check_names(entry, end, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700347 if (error)
348 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700349 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350 end - (void *)entry, 0);
351 if (error)
352 goto cleanup;
353 size = le32_to_cpu(entry->e_value_size);
354 if (buffer) {
355 error = -ERANGE;
356 if (size > buffer_size)
357 goto cleanup;
358 memcpy(buffer, (void *)IFIRST(header) +
359 le16_to_cpu(entry->e_value_offs), size);
360 }
361 error = size;
362
363cleanup:
364 brelse(iloc.bh);
365 return error;
366}
367
368/*
Mingming Cao617ba132006-10-11 01:20:53 -0700369 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 *
371 * Copy an extended attribute into the buffer
372 * provided, or compute the buffer size required.
373 * Buffer is NULL to compute the size of the buffer required.
374 *
375 * Returns a negative error number on failure, or the number of bytes
376 * used / required on success.
377 */
378int
Mingming Cao617ba132006-10-11 01:20:53 -0700379ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 void *buffer, size_t buffer_size)
381{
382 int error;
383
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400384 if (strlen(name) > 255)
385 return -ERANGE;
386
Mingming Cao617ba132006-10-11 01:20:53 -0700387 down_read(&EXT4_I(inode)->xattr_sem);
388 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700389 buffer_size);
390 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700391 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700393 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700394 return error;
395}
396
397static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000398ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399 char *buffer, size_t buffer_size)
400{
401 size_t rest = buffer_size;
402
Mingming Cao617ba132006-10-11 01:20:53 -0700403 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700404 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700405 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100407 if (handler && (!handler->list || handler->list(dentry))) {
408 const char *prefix = handler->prefix ?: handler->name;
409 size_t prefix_len = strlen(prefix);
410 size_t size = prefix_len + entry->e_name_len + 1;
411
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 if (buffer) {
413 if (size > rest)
414 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100415 memcpy(buffer, prefix, prefix_len);
416 buffer += prefix_len;
417 memcpy(buffer, entry->e_name, entry->e_name_len);
418 buffer += entry->e_name_len;
419 *buffer++ = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420 }
421 rest -= size;
422 }
423 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100424 return buffer_size - rest; /* total size */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425}
426
427static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000428ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429{
David Howells2b0143b2015-03-17 22:25:59 +0000430 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431 struct buffer_head *bh = NULL;
432 int error;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400433 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434
435 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
436 buffer, (long)buffer_size);
437
438 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700439 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400441 ea_idebug(inode, "reading block %llu",
442 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700443 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 error = -EIO;
445 if (!bh)
446 goto cleanup;
447 ea_bdebug(bh, "b_count=%d, refcount=%d",
448 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400449 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400450 EXT4_ERROR_INODE(inode, "bad block %llu",
451 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400452 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 goto cleanup;
454 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400455 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000456 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700457
458cleanup:
459 brelse(bh);
460
461 return error;
462}
463
464static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000465ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466{
David Howells2b0143b2015-03-17 22:25:59 +0000467 struct inode *inode = d_inode(dentry);
Mingming Cao617ba132006-10-11 01:20:53 -0700468 struct ext4_xattr_ibody_header *header;
469 struct ext4_inode *raw_inode;
470 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 void *end;
472 int error;
473
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500474 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700476 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477 if (error)
478 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700479 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700481 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400482 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 if (error)
484 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000485 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 buffer, buffer_size);
487
488cleanup:
489 brelse(iloc.bh);
490 return error;
491}
492
493/*
Mingming Cao617ba132006-10-11 01:20:53 -0700494 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 *
496 * Copy a list of attribute names into the buffer
497 * provided, or compute the buffer size required.
498 * Buffer is NULL to compute the size of the buffer required.
499 *
500 * Returns a negative error number on failure, or the number of bytes
501 * used / required on success.
502 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400503static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000504ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500506 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507
David Howells2b0143b2015-03-17 22:25:59 +0000508 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500509 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
510 if (ret < 0)
511 goto errout;
512 if (buffer) {
513 buffer += ret;
514 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500516 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
517 if (ret < 0)
518 goto errout;
519 ret += ret2;
520errout:
David Howells2b0143b2015-03-17 22:25:59 +0000521 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500522 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523}
524
525/*
Mingming Cao617ba132006-10-11 01:20:53 -0700526 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527 * not set, set it.
528 */
Mingming Cao617ba132006-10-11 01:20:53 -0700529static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530 struct super_block *sb)
531{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400532 if (ext4_has_feature_xattr(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700533 return;
534
liang xie5d601252014-05-12 22:06:43 -0400535 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700536 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400537 ext4_set_feature_xattr(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400538 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540}
541
542/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400543 * Release the xattr block BH: If the reference count is > 1, decrement it;
544 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 */
546static void
Mingming Cao617ba132006-10-11 01:20:53 -0700547ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 struct buffer_head *bh)
549{
550 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800551 int error = 0;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400552 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400554 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
liang xie5d601252014-05-12 22:06:43 -0400555 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800556 error = ext4_journal_get_write_access(handle, bh);
557 if (error)
558 goto out;
559
560 lock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
562 ea_bdebug(bh, "refcount now=0; freeing");
563 if (ce)
564 mb_cache_entry_free(ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400566 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500567 ext4_free_blocks(handle, inode, bh, 0, 1,
568 EXT4_FREE_BLOCKS_METADATA |
569 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 } else {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400571 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500572 if (ce)
573 mb_cache_entry_release(ce);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400574 /*
575 * Beware of this ugliness: Releasing of xattr block references
576 * from different inodes can race and so we have to protect
577 * from a race where someone else frees the block (and releases
578 * its journal_head) before we are done dirtying the buffer. In
579 * nojournal mode this race is harmless and we actually cannot
580 * call ext4_handle_dirty_xattr_block() with locked buffer as
581 * that function can call sync_dirty_buffer() so for that case
582 * we handle the dirtying after unlocking the buffer.
583 */
584 if (ext4_handle_valid(handle))
585 error = ext4_handle_dirty_xattr_block(handle, inode,
586 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500587 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400588 if (!ext4_handle_valid(handle))
589 error = ext4_handle_dirty_xattr_block(handle, inode,
590 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800591 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500592 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500593 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800594 ea_bdebug(bh, "refcount now=%d; releasing",
595 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800597out:
598 ext4_std_error(inode->i_sb, error);
599 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600}
601
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400602/*
603 * Find the available free space for EAs. This also returns the total number of
604 * bytes used by EA entries.
605 */
606static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
607 size_t *min_offs, void *base, int *total)
608{
609 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400610 if (!last->e_value_block && last->e_value_size) {
611 size_t offs = le16_to_cpu(last->e_value_offs);
612 if (offs < *min_offs)
613 *min_offs = offs;
614 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500615 if (total)
616 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400617 }
618 return (*min_offs - ((void *)last - base) - sizeof(__u32));
619}
620
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621static int
Mingming Cao617ba132006-10-11 01:20:53 -0700622ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623{
Mingming Cao617ba132006-10-11 01:20:53 -0700624 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
626
627 /* Compute min_offs and last. */
628 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700629 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 if (!last->e_value_block && last->e_value_size) {
631 size_t offs = le16_to_cpu(last->e_value_offs);
632 if (offs < min_offs)
633 min_offs = offs;
634 }
635 }
636 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
637 if (!s->not_found) {
638 if (!s->here->e_value_block && s->here->e_value_size) {
639 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700640 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641 }
Mingming Cao617ba132006-10-11 01:20:53 -0700642 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 }
644 if (i->value) {
Wei Yuan5f80f622015-04-02 23:50:48 -0400645 if (free < EXT4_XATTR_LEN(name_len) +
Mingming Cao617ba132006-10-11 01:20:53 -0700646 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 return -ENOSPC;
648 }
649
650 if (i->value && s->not_found) {
651 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700652 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
654 memmove((void *)s->here + size, s->here, rest);
655 memset(s->here, 0, size);
656 s->here->e_name_index = i->name_index;
657 s->here->e_name_len = name_len;
658 memcpy(s->here->e_name, i->name, name_len);
659 } else {
660 if (!s->here->e_value_block && s->here->e_value_size) {
661 void *first_val = s->base + min_offs;
662 size_t offs = le16_to_cpu(s->here->e_value_offs);
663 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700664 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 le32_to_cpu(s->here->e_value_size));
666
Mingming Cao617ba132006-10-11 01:20:53 -0700667 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 /* The old and the new value have the same
669 size. Just replace. */
670 s->here->e_value_size =
671 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500672 if (i->value == EXT4_ZERO_XATTR_VALUE) {
673 memset(val, 0, size);
674 } else {
675 /* Clear pad bytes first. */
676 memset(val + size - EXT4_XATTR_PAD, 0,
677 EXT4_XATTR_PAD);
678 memcpy(val, i->value, i->value_len);
679 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700680 return 0;
681 }
682
683 /* Remove the old value. */
684 memmove(first_val + size, first_val, val - first_val);
685 memset(first_val, 0, size);
686 s->here->e_value_size = 0;
687 s->here->e_value_offs = 0;
688 min_offs += size;
689
690 /* Adjust all value offsets. */
691 last = s->first;
692 while (!IS_LAST_ENTRY(last)) {
693 size_t o = le16_to_cpu(last->e_value_offs);
694 if (!last->e_value_block &&
695 last->e_value_size && o < offs)
696 last->e_value_offs =
697 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700698 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 }
700 }
701 if (!i->value) {
702 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700703 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 last = ENTRY((void *)last - size);
705 memmove(s->here, (void *)s->here + size,
706 (void *)last - (void *)s->here + sizeof(__u32));
707 memset(last, 0, size);
708 }
709 }
710
711 if (i->value) {
712 /* Insert the new value. */
713 s->here->e_value_size = cpu_to_le32(i->value_len);
714 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700715 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 void *val = s->base + min_offs - size;
717 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500718 if (i->value == EXT4_ZERO_XATTR_VALUE) {
719 memset(val, 0, size);
720 } else {
721 /* Clear the pad bytes first. */
722 memset(val + size - EXT4_XATTR_PAD, 0,
723 EXT4_XATTR_PAD);
724 memcpy(val, i->value, i->value_len);
725 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 }
727 }
728 return 0;
729}
730
Mingming Cao617ba132006-10-11 01:20:53 -0700731struct ext4_xattr_block_find {
732 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 struct buffer_head *bh;
734};
735
736static int
Mingming Cao617ba132006-10-11 01:20:53 -0700737ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
738 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739{
740 struct super_block *sb = inode->i_sb;
741 int error;
742
743 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
744 i->name_index, i->name, i->value, (long)i->value_len);
745
Mingming Cao617ba132006-10-11 01:20:53 -0700746 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700748 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 error = -EIO;
750 if (!bs->bh)
751 goto cleanup;
752 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
753 atomic_read(&(bs->bh->b_count)),
754 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400755 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400756 EXT4_ERROR_INODE(inode, "bad block %llu",
757 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400758 error = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 goto cleanup;
760 }
761 /* Find the named attribute. */
762 bs->s.base = BHDR(bs->bh);
763 bs->s.first = BFIRST(bs->bh);
764 bs->s.end = bs->bh->b_data + bs->bh->b_size;
765 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700766 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 i->name, bs->bh->b_size, 1);
768 if (error && error != -ENODATA)
769 goto cleanup;
770 bs->s.not_found = error;
771 }
772 error = 0;
773
774cleanup:
775 return error;
776}
777
778static int
Mingming Cao617ba132006-10-11 01:20:53 -0700779ext4_xattr_block_set(handle_t *handle, struct inode *inode,
780 struct ext4_xattr_info *i,
781 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782{
783 struct super_block *sb = inode->i_sb;
784 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700785 struct ext4_xattr_search *s = &bs->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800787 int error = 0;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400788 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789
Mingming Cao617ba132006-10-11 01:20:53 -0700790#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791
792 if (i->value && i->value_len > sb->s_blocksize)
793 return -ENOSPC;
794 if (s->base) {
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400795 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 bs->bh->b_blocknr);
liang xie5d601252014-05-12 22:06:43 -0400797 BUFFER_TRACE(bs->bh, "get_write_access");
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800798 error = ext4_journal_get_write_access(handle, bs->bh);
799 if (error)
800 goto cleanup;
801 lock_buffer(bs->bh);
802
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
804 if (ce) {
805 mb_cache_entry_free(ce);
806 ce = NULL;
807 }
808 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700809 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 if (!error) {
811 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700812 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400814 ext4_xattr_cache_insert(ext4_mb_cache,
815 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 }
817 unlock_buffer(bs->bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400818 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 goto bad_block;
820 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400821 error = ext4_handle_dirty_xattr_block(handle,
822 inode,
823 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 if (error)
825 goto cleanup;
826 goto inserted;
827 } else {
828 int offset = (char *)s->here - bs->bh->b_data;
829
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800830 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831 if (ce) {
832 mb_cache_entry_release(ce);
833 ce = NULL;
834 }
835 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400836 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 error = -ENOMEM;
838 if (s->base == NULL)
839 goto cleanup;
840 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
841 s->first = ENTRY(header(s->base)+1);
842 header(s->base)->h_refcount = cpu_to_le32(1);
843 s->here = ENTRY(s->base + offset);
844 s->end = s->base + bs->bh->b_size;
845 }
846 } else {
847 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400848 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700849 /* assert(header == s->base) */
850 error = -ENOMEM;
851 if (s->base == NULL)
852 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700853 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 header(s->base)->h_blocks = cpu_to_le32(1);
855 header(s->base)->h_refcount = cpu_to_le32(1);
856 s->first = ENTRY(header(s->base)+1);
857 s->here = ENTRY(header(s->base)+1);
858 s->end = s->base + sb->s_blocksize;
859 }
860
Mingming Cao617ba132006-10-11 01:20:53 -0700861 error = ext4_xattr_set_entry(i, s);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400862 if (error == -EFSCORRUPTED)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 goto bad_block;
864 if (error)
865 goto cleanup;
866 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700867 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868
869inserted:
870 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700871 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 if (new_bh) {
873 /* We found an identical block in the cache. */
874 if (new_bh == bs->bh)
875 ea_bdebug(new_bh, "keeping");
876 else {
877 /* The old block is released after updating
878 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500879 error = dquot_alloc_block(inode,
880 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500881 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 goto cleanup;
liang xie5d601252014-05-12 22:06:43 -0400883 BUFFER_TRACE(new_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700884 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700885 new_bh);
886 if (error)
887 goto cleanup_dquot;
888 lock_buffer(new_bh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400889 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700890 ea_bdebug(new_bh, "reusing; refcount now=%d",
891 le32_to_cpu(BHDR(new_bh)->h_refcount));
892 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400893 error = ext4_handle_dirty_xattr_block(handle,
894 inode,
895 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896 if (error)
897 goto cleanup_dquot;
898 }
899 mb_cache_entry_release(ce);
900 ce = NULL;
901 } else if (bs->bh && s->base == bs->bh->b_data) {
902 /* We were modifying this block in-place. */
903 ea_bdebug(bs->bh, "keeping this block");
904 new_bh = bs->bh;
905 get_bh(new_bh);
906 } else {
907 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400908 ext4_fsblk_t goal, block;
909
910 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400911 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400912
913 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400914 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400915 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
916
Allison Henderson55f020d2011-05-25 07:41:26 -0400917 block = ext4_new_meta_blocks(handle, inode, goal, 0,
918 NULL, &error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700919 if (error)
920 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400921
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400922 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400923 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
924
Joe Perchesace36ad2012-03-19 23:11:43 -0400925 ea_idebug(inode, "creating block %llu",
926 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927
928 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500929 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500930 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500932 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500933 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 goto cleanup;
935 }
936 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700937 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938 if (error) {
939 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500940 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941 goto getblk_failed;
942 }
943 memcpy(new_bh->b_data, s->base, new_bh->b_size);
944 set_buffer_uptodate(new_bh);
945 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400946 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400947 error = ext4_handle_dirty_xattr_block(handle,
948 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 if (error)
950 goto cleanup;
951 }
952 }
953
954 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -0700955 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956
957 /* Drop the previous xattr block. */
958 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700959 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960 error = 0;
961
962cleanup:
963 if (ce)
964 mb_cache_entry_release(ce);
965 brelse(new_bh);
966 if (!(bs->bh && s->base == bs->bh->b_data))
967 kfree(s->base);
968
969 return error;
970
971cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500972 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 goto cleanup;
974
975bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400976 EXT4_ERROR_INODE(inode, "bad block %llu",
977 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 goto cleanup;
979
980#undef header
981}
982
Tao Ma879b3822012-12-05 10:28:46 -0500983int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
984 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985{
Mingming Cao617ba132006-10-11 01:20:53 -0700986 struct ext4_xattr_ibody_header *header;
987 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 int error;
989
Mingming Cao617ba132006-10-11 01:20:53 -0700990 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700992 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 header = IHDR(inode, raw_inode);
994 is->s.base = is->s.first = IFIRST(header);
995 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700996 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500997 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Darrick J. Wonga0626e752014-09-16 14:34:59 -0400998 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
999 IFIRST(header));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 if (error)
1001 return error;
1002 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -07001003 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 i->name, is->s.end -
1005 (void *)is->s.base, 0);
1006 if (error && error != -ENODATA)
1007 return error;
1008 is->s.not_found = error;
1009 }
1010 return 0;
1011}
1012
Tao Ma0d812f72012-12-10 14:06:02 -05001013int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1014 struct ext4_xattr_info *i,
1015 struct ext4_xattr_ibody_find *is)
1016{
1017 struct ext4_xattr_ibody_header *header;
1018 struct ext4_xattr_search *s = &is->s;
1019 int error;
1020
1021 if (EXT4_I(inode)->i_extra_isize == 0)
1022 return -ENOSPC;
1023 error = ext4_xattr_set_entry(i, s);
1024 if (error) {
1025 if (error == -ENOSPC &&
1026 ext4_has_inline_data(inode)) {
1027 error = ext4_try_to_evict_inline_data(handle, inode,
1028 EXT4_XATTR_LEN(strlen(i->name) +
1029 EXT4_XATTR_SIZE(i->value_len)));
1030 if (error)
1031 return error;
1032 error = ext4_xattr_ibody_find(inode, i, is);
1033 if (error)
1034 return error;
1035 error = ext4_xattr_set_entry(i, s);
1036 }
1037 if (error)
1038 return error;
1039 }
1040 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1041 if (!IS_LAST_ENTRY(s->first)) {
1042 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1043 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1044 } else {
1045 header->h_magic = cpu_to_le32(0);
1046 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1047 }
1048 return 0;
1049}
1050
1051static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1052 struct ext4_xattr_info *i,
1053 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054{
Mingming Cao617ba132006-10-11 01:20:53 -07001055 struct ext4_xattr_ibody_header *header;
1056 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057 int error;
1058
Mingming Cao617ba132006-10-11 01:20:53 -07001059 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001060 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001061 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 if (error)
1063 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001064 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001066 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001067 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 } else {
1069 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001070 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 }
1072 return 0;
1073}
1074
1075/*
Mingming Cao617ba132006-10-11 01:20:53 -07001076 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001078 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 * is NULL to remove an existing extended attribute, and non-NULL to
1080 * either replace an existing extended attribute, or create a new extended
1081 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1082 * specify that an extended attribute must exist and must not exist
1083 * previous to the call, respectively.
1084 *
1085 * Returns 0, or a negative error number on failure.
1086 */
1087int
Mingming Cao617ba132006-10-11 01:20:53 -07001088ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 const char *name, const void *value, size_t value_len,
1090 int flags)
1091{
Mingming Cao617ba132006-10-11 01:20:53 -07001092 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 .name_index = name_index,
1094 .name = name,
1095 .value = value,
1096 .value_len = value_len,
1097
1098 };
Mingming Cao617ba132006-10-11 01:20:53 -07001099 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001100 .s = { .not_found = -ENODATA, },
1101 };
Mingming Cao617ba132006-10-11 01:20:53 -07001102 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001103 .s = { .not_found = -ENODATA, },
1104 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001105 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106 int error;
1107
1108 if (!name)
1109 return -EINVAL;
1110 if (strlen(name) > 255)
1111 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001112 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001113 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1114 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001115
Eric Sandeen66543612011-10-26 03:32:07 -04001116 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001117 if (error)
1118 goto cleanup;
1119
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001120 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001121 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1122 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001123 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 }
1125
Mingming Cao617ba132006-10-11 01:20:53 -07001126 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 if (error)
1128 goto cleanup;
1129 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001130 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 if (error)
1132 goto cleanup;
1133 if (is.s.not_found && bs.s.not_found) {
1134 error = -ENODATA;
1135 if (flags & XATTR_REPLACE)
1136 goto cleanup;
1137 error = 0;
1138 if (!value)
1139 goto cleanup;
1140 } else {
1141 error = -EEXIST;
1142 if (flags & XATTR_CREATE)
1143 goto cleanup;
1144 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145 if (!value) {
1146 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001147 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001149 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001151 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 if (!error && !bs.s.not_found) {
1153 i.value = NULL;
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 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001156 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1157 error = ext4_xattr_block_find(inode, &i, &bs);
1158 if (error)
1159 goto cleanup;
1160 }
Mingming Cao617ba132006-10-11 01:20:53 -07001161 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 if (error)
1163 goto cleanup;
1164 if (!is.s.not_found) {
1165 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001166 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 &is);
1168 }
1169 }
1170 }
1171 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001172 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001173 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001174 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001175 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001176 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001178 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 * error != 0.
1180 */
1181 is.iloc.bh = NULL;
1182 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001183 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001184 }
1185
1186cleanup:
1187 brelse(is.iloc.bh);
1188 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001189 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001190 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001191 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 return error;
1193}
1194
1195/*
Mingming Cao617ba132006-10-11 01:20:53 -07001196 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 *
Mingming Cao617ba132006-10-11 01:20:53 -07001198 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 * attribute modification is a filesystem transaction by itself.
1200 *
1201 * Returns 0, or a negative error number on failure.
1202 */
1203int
Mingming Cao617ba132006-10-11 01:20:53 -07001204ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 const void *value, size_t value_len, int flags)
1206{
1207 handle_t *handle;
1208 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001209 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210
1211retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001212 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001213 if (IS_ERR(handle)) {
1214 error = PTR_ERR(handle);
1215 } else {
1216 int error2;
1217
Mingming Cao617ba132006-10-11 01:20:53 -07001218 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001220 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001222 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001223 goto retry;
1224 if (error == 0)
1225 error = error2;
1226 }
1227
1228 return error;
1229}
1230
1231/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001232 * Shift the EA entries in the inode to create space for the increased
1233 * i_extra_isize.
1234 */
1235static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1236 int value_offs_shift, void *to,
1237 void *from, size_t n, int blocksize)
1238{
1239 struct ext4_xattr_entry *last = entry;
1240 int new_offs;
1241
1242 /* Adjust the value offsets of the entries */
1243 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1244 if (!last->e_value_block && last->e_value_size) {
1245 new_offs = le16_to_cpu(last->e_value_offs) +
1246 value_offs_shift;
1247 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1248 > blocksize);
1249 last->e_value_offs = cpu_to_le16(new_offs);
1250 }
1251 }
1252 /* Shift the entries by n bytes */
1253 memmove(to, from, n);
1254}
1255
1256/*
1257 * Expand an inode by new_extra_isize bytes when EAs are present.
1258 * Returns 0 on success or negative error number on failure.
1259 */
1260int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1261 struct ext4_inode *raw_inode, handle_t *handle)
1262{
1263 struct ext4_xattr_ibody_header *header;
1264 struct ext4_xattr_entry *entry, *last, *first;
1265 struct buffer_head *bh = NULL;
1266 struct ext4_xattr_ibody_find *is = NULL;
1267 struct ext4_xattr_block_find *bs = NULL;
1268 char *buffer = NULL, *b_entry_name = NULL;
1269 size_t min_offs, free;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001270 int total_ino;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001271 void *base, *start, *end;
1272 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001273 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 -04001274
1275 down_write(&EXT4_I(inode)->xattr_sem);
1276retry:
1277 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1278 up_write(&EXT4_I(inode)->xattr_sem);
1279 return 0;
1280 }
1281
1282 header = IHDR(inode, raw_inode);
1283 entry = IFIRST(header);
1284
1285 /*
1286 * Check if enough free space is available in the inode to shift the
1287 * entries ahead by new_extra_isize.
1288 */
1289
1290 base = start = entry;
1291 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1292 min_offs = end - base;
1293 last = entry;
1294 total_ino = sizeof(struct ext4_xattr_ibody_header);
1295
1296 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1297 if (free >= new_extra_isize) {
1298 entry = IFIRST(header);
1299 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1300 - new_extra_isize, (void *)raw_inode +
1301 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1302 (void *)header, total_ino,
1303 inode->i_sb->s_blocksize);
1304 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1305 error = 0;
1306 goto cleanup;
1307 }
1308
1309 /*
1310 * Enough free space isn't available in the inode, check if
1311 * EA block can hold new_extra_isize bytes.
1312 */
1313 if (EXT4_I(inode)->i_file_acl) {
1314 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1315 error = -EIO;
1316 if (!bh)
1317 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001318 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001319 EXT4_ERROR_INODE(inode, "bad block %llu",
1320 EXT4_I(inode)->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001321 error = -EFSCORRUPTED;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001322 goto cleanup;
1323 }
1324 base = BHDR(bh);
1325 first = BFIRST(bh);
1326 end = bh->b_data + bh->b_size;
1327 min_offs = end - base;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001328 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001329 if (free < new_extra_isize) {
1330 if (!tried_min_extra_isize && s_min_extra_isize) {
1331 tried_min_extra_isize++;
1332 new_extra_isize = s_min_extra_isize;
1333 brelse(bh);
1334 goto retry;
1335 }
1336 error = -1;
1337 goto cleanup;
1338 }
1339 } else {
1340 free = inode->i_sb->s_blocksize;
1341 }
1342
1343 while (new_extra_isize > 0) {
1344 size_t offs, size, entry_size;
1345 struct ext4_xattr_entry *small_entry = NULL;
1346 struct ext4_xattr_info i = {
1347 .value = NULL,
1348 .value_len = 0,
1349 };
1350 unsigned int total_size; /* EA entry size + value size */
1351 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1352 unsigned int min_total_size = ~0U;
1353
1354 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1355 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1356 if (!is || !bs) {
1357 error = -ENOMEM;
1358 goto cleanup;
1359 }
1360
1361 is->s.not_found = -ENODATA;
1362 bs->s.not_found = -ENODATA;
1363 is->iloc.bh = NULL;
1364 bs->bh = NULL;
1365
1366 last = IFIRST(header);
1367 /* Find the entry best suited to be pushed into EA block */
1368 entry = NULL;
1369 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1370 total_size =
1371 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1372 EXT4_XATTR_LEN(last->e_name_len);
1373 if (total_size <= free && total_size < min_total_size) {
1374 if (total_size < new_extra_isize) {
1375 small_entry = last;
1376 } else {
1377 entry = last;
1378 min_total_size = total_size;
1379 }
1380 }
1381 }
1382
1383 if (entry == NULL) {
1384 if (small_entry) {
1385 entry = small_entry;
1386 } else {
1387 if (!tried_min_extra_isize &&
1388 s_min_extra_isize) {
1389 tried_min_extra_isize++;
1390 new_extra_isize = s_min_extra_isize;
Dave Jones6e4ea8e2013-10-10 20:05:35 -04001391 kfree(is); is = NULL;
1392 kfree(bs); bs = NULL;
Theodore Ts'odcb99172013-10-31 23:00:24 -04001393 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001394 goto retry;
1395 }
1396 error = -1;
1397 goto cleanup;
1398 }
1399 }
1400 offs = le16_to_cpu(entry->e_value_offs);
1401 size = le32_to_cpu(entry->e_value_size);
1402 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1403 i.name_index = entry->e_name_index,
1404 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1405 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1406 if (!buffer || !b_entry_name) {
1407 error = -ENOMEM;
1408 goto cleanup;
1409 }
1410 /* Save the entry name and the entry value */
1411 memcpy(buffer, (void *)IFIRST(header) + offs,
1412 EXT4_XATTR_SIZE(size));
1413 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1414 b_entry_name[entry->e_name_len] = '\0';
1415 i.name = b_entry_name;
1416
1417 error = ext4_get_inode_loc(inode, &is->iloc);
1418 if (error)
1419 goto cleanup;
1420
1421 error = ext4_xattr_ibody_find(inode, &i, is);
1422 if (error)
1423 goto cleanup;
1424
1425 /* Remove the chosen entry from the inode */
1426 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001427 if (error)
1428 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001429
1430 entry = IFIRST(header);
1431 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1432 shift_bytes = new_extra_isize;
1433 else
1434 shift_bytes = entry_size + size;
1435 /* Adjust the offsets and shift the remaining entries ahead */
1436 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1437 shift_bytes, (void *)raw_inode +
1438 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1439 (void *)header, total_ino - entry_size,
1440 inode->i_sb->s_blocksize);
1441
1442 extra_isize += shift_bytes;
1443 new_extra_isize -= shift_bytes;
1444 EXT4_I(inode)->i_extra_isize = extra_isize;
1445
1446 i.name = b_entry_name;
1447 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001448 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001449 error = ext4_xattr_block_find(inode, &i, bs);
1450 if (error)
1451 goto cleanup;
1452
1453 /* Add entry which was removed from the inode into the block */
1454 error = ext4_xattr_block_set(handle, inode, &i, bs);
1455 if (error)
1456 goto cleanup;
1457 kfree(b_entry_name);
1458 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001459 b_entry_name = NULL;
1460 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001461 brelse(is->iloc.bh);
1462 kfree(is);
1463 kfree(bs);
1464 }
1465 brelse(bh);
1466 up_write(&EXT4_I(inode)->xattr_sem);
1467 return 0;
1468
1469cleanup:
1470 kfree(b_entry_name);
1471 kfree(buffer);
1472 if (is)
1473 brelse(is->iloc.bh);
1474 kfree(is);
1475 kfree(bs);
1476 brelse(bh);
1477 up_write(&EXT4_I(inode)->xattr_sem);
1478 return error;
1479}
1480
1481
1482
1483/*
Mingming Cao617ba132006-10-11 01:20:53 -07001484 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 *
1486 * Free extended attribute resources associated with this inode. This
1487 * is called immediately before an inode is freed. We have exclusive
1488 * access to the inode.
1489 */
1490void
Mingming Cao617ba132006-10-11 01:20:53 -07001491ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492{
1493 struct buffer_head *bh = NULL;
1494
Mingming Cao617ba132006-10-11 01:20:53 -07001495 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001496 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001497 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001499 EXT4_ERROR_INODE(inode, "block %llu read error",
1500 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 goto cleanup;
1502 }
Mingming Cao617ba132006-10-11 01:20:53 -07001503 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001505 EXT4_ERROR_INODE(inode, "bad block %llu",
1506 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 goto cleanup;
1508 }
Mingming Cao617ba132006-10-11 01:20:53 -07001509 ext4_xattr_release_block(handle, inode, bh);
1510 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511
1512cleanup:
1513 brelse(bh);
1514}
1515
1516/*
Mingming Cao617ba132006-10-11 01:20:53 -07001517 * ext4_xattr_put_super()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 *
1519 * This is called when a file system is unmounted.
1520 */
1521void
Mingming Cao617ba132006-10-11 01:20:53 -07001522ext4_xattr_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523{
1524 mb_cache_shrink(sb->s_bdev);
1525}
1526
1527/*
Mingming Cao617ba132006-10-11 01:20:53 -07001528 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001529 *
1530 * Create a new entry in the extended attribute cache, and insert
1531 * it unless such an entry is already in the cache.
1532 *
1533 * Returns 0, or a negative error number on failure.
1534 */
1535static void
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001536ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537{
1538 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1539 struct mb_cache_entry *ce;
1540 int error;
1541
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001542 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543 if (!ce) {
1544 ea_bdebug(bh, "out of memory");
1545 return;
1546 }
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001547 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001548 if (error) {
1549 mb_cache_entry_free(ce);
1550 if (error == -EBUSY) {
1551 ea_bdebug(bh, "already in cache");
1552 error = 0;
1553 }
1554 } else {
1555 ea_bdebug(bh, "inserting [%x]", (int)hash);
1556 mb_cache_entry_release(ce);
1557 }
1558}
1559
1560/*
Mingming Cao617ba132006-10-11 01:20:53 -07001561 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562 *
1563 * Compare two extended attribute blocks for equality.
1564 *
1565 * Returns 0 if the blocks are equal, 1 if they differ, and
1566 * a negative error number on errors.
1567 */
1568static int
Mingming Cao617ba132006-10-11 01:20:53 -07001569ext4_xattr_cmp(struct ext4_xattr_header *header1,
1570 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571{
Mingming Cao617ba132006-10-11 01:20:53 -07001572 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573
1574 entry1 = ENTRY(header1+1);
1575 entry2 = ENTRY(header2+1);
1576 while (!IS_LAST_ENTRY(entry1)) {
1577 if (IS_LAST_ENTRY(entry2))
1578 return 1;
1579 if (entry1->e_hash != entry2->e_hash ||
1580 entry1->e_name_index != entry2->e_name_index ||
1581 entry1->e_name_len != entry2->e_name_len ||
1582 entry1->e_value_size != entry2->e_value_size ||
1583 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1584 return 1;
1585 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001586 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1588 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1589 le32_to_cpu(entry1->e_value_size)))
1590 return 1;
1591
Mingming Cao617ba132006-10-11 01:20:53 -07001592 entry1 = EXT4_XATTR_NEXT(entry1);
1593 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001594 }
1595 if (!IS_LAST_ENTRY(entry2))
1596 return 1;
1597 return 0;
1598}
1599
1600/*
Mingming Cao617ba132006-10-11 01:20:53 -07001601 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 *
1603 * Find an identical extended attribute block.
1604 *
1605 * Returns a pointer to the block found, or NULL if such a block was
1606 * not found or an error occurred.
1607 */
1608static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001609ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 struct mb_cache_entry **pce)
1611{
1612 __u32 hash = le32_to_cpu(header->h_hash);
1613 struct mb_cache_entry *ce;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001614 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615
1616 if (!header->h_hash)
1617 return NULL; /* never share */
1618 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1619again:
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001620 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001621 hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622 while (ce) {
1623 struct buffer_head *bh;
1624
1625 if (IS_ERR(ce)) {
1626 if (PTR_ERR(ce) == -EAGAIN)
1627 goto again;
1628 break;
1629 }
1630 bh = sb_bread(inode->i_sb, ce->e_block);
1631 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001632 EXT4_ERROR_INODE(inode, "block %lu read error",
1633 (unsigned long) ce->e_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
Mingming Cao617ba132006-10-11 01:20:53 -07001635 EXT4_XATTR_REFCOUNT_MAX) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001636 ea_idebug(inode, "block %lu refcount %d>=%d",
1637 (unsigned long) ce->e_block,
1638 le32_to_cpu(BHDR(bh)->h_refcount),
Mingming Cao617ba132006-10-11 01:20:53 -07001639 EXT4_XATTR_REFCOUNT_MAX);
1640 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641 *pce = ce;
1642 return bh;
1643 }
1644 brelse(bh);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001645 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646 }
1647 return NULL;
1648}
1649
1650#define NAME_HASH_SHIFT 5
1651#define VALUE_HASH_SHIFT 16
1652
1653/*
Mingming Cao617ba132006-10-11 01:20:53 -07001654 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001655 *
1656 * Compute the hash of an extended attribute.
1657 */
Mingming Cao617ba132006-10-11 01:20:53 -07001658static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1659 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001660{
1661 __u32 hash = 0;
1662 char *name = entry->e_name;
1663 int n;
1664
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001665 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666 hash = (hash << NAME_HASH_SHIFT) ^
1667 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1668 *name++;
1669 }
1670
1671 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1672 __le32 *value = (__le32 *)((char *)header +
1673 le16_to_cpu(entry->e_value_offs));
1674 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001675 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001676 hash = (hash << VALUE_HASH_SHIFT) ^
1677 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1678 le32_to_cpu(*value++);
1679 }
1680 }
1681 entry->e_hash = cpu_to_le32(hash);
1682}
1683
1684#undef NAME_HASH_SHIFT
1685#undef VALUE_HASH_SHIFT
1686
1687#define BLOCK_HASH_SHIFT 16
1688
1689/*
Mingming Cao617ba132006-10-11 01:20:53 -07001690 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 *
1692 * Re-compute the extended attribute hash value after an entry has changed.
1693 */
Mingming Cao617ba132006-10-11 01:20:53 -07001694static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1695 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696{
Mingming Cao617ba132006-10-11 01:20:53 -07001697 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 __u32 hash = 0;
1699
Mingming Cao617ba132006-10-11 01:20:53 -07001700 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701 here = ENTRY(header+1);
1702 while (!IS_LAST_ENTRY(here)) {
1703 if (!here->e_hash) {
1704 /* Block is not shared if an entry's hash value == 0 */
1705 hash = 0;
1706 break;
1707 }
1708 hash = (hash << BLOCK_HASH_SHIFT) ^
1709 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1710 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001711 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 }
1713 header->h_hash = cpu_to_le32(hash);
1714}
1715
1716#undef BLOCK_HASH_SHIFT
1717
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001718#define HASH_BUCKET_BITS 10
1719
1720struct mb_cache *
1721ext4_xattr_create_cache(char *name)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001723 return mb_cache_create(name, HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724}
1725
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001726void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001728 if (cache)
1729 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001731