blob: c6ae6fab99befc751015f7b2e4e8f0ed06653d1c [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>
58#include <linux/rwsem.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040059#include "ext4_jbd2.h"
60#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061#include "xattr.h"
62#include "acl.h"
63
Mingming Cao617ba132006-10-11 01:20:53 -070064#ifdef EXT4_XATTR_DEBUG
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065# define ea_idebug(inode, f...) do { \
66 printk(KERN_DEBUG "inode %s:%lu: ", \
67 inode->i_sb->s_id, inode->i_ino); \
68 printk(f); \
69 printk("\n"); \
70 } while (0)
71# define ea_bdebug(bh, f...) do { \
72 char b[BDEVNAME_SIZE]; \
73 printk(KERN_DEBUG "block %s:%lu: ", \
74 bdevname(bh->b_bdev, b), \
75 (unsigned long) bh->b_blocknr); \
76 printk(f); \
77 printk("\n"); \
78 } while (0)
79#else
Joe Perchesace36ad2012-03-19 23:11:43 -040080# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
81# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070082#endif
83
T Makphaibulchoke9c191f72014-03-18 19:24:49 -040084static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
Mingming Cao617ba132006-10-11 01:20:53 -070085static struct buffer_head *ext4_xattr_cache_find(struct inode *,
86 struct ext4_xattr_header *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070088static void ext4_xattr_rehash(struct ext4_xattr_header *,
89 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000090static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040091 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092
Stephen Hemminger11e27522010-05-13 17:53:18 -070093static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070094 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040095#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080096 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
97 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070098#endif
Mingming Cao617ba132006-10-11 01:20:53 -070099 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400100#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700101 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102#endif
103};
104
Stephen Hemminger11e27522010-05-13 17:53:18 -0700105const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700106 &ext4_xattr_user_handler,
107 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400108#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800109 &posix_acl_access_xattr_handler,
110 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400112#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700113 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114#endif
115 NULL
116};
117
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400118#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
119 inode->i_sb->s_fs_info)->s_mb_cache)
120
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400121static __le32 ext4_xattr_block_csum(struct inode *inode,
122 sector_t block_nr,
123 struct ext4_xattr_header *hdr)
124{
125 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400126 __u32 csum;
127 __le32 save_csum;
128 __le64 dsk_block_nr = cpu_to_le64(block_nr);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400129
Theodore Ts'od6a77102013-04-09 23:59:55 -0400130 save_csum = hdr->h_checksum;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400131 hdr->h_checksum = 0;
Theodore Ts'od6a77102013-04-09 23:59:55 -0400132 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
133 sizeof(dsk_block_nr));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400134 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
135 EXT4_BLOCK_SIZE(inode->i_sb));
Tao Ma41eb70d2012-07-09 16:29:27 -0400136
Theodore Ts'od6a77102013-04-09 23:59:55 -0400137 hdr->h_checksum = save_csum;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400138 return cpu_to_le32(csum);
139}
140
141static int ext4_xattr_block_csum_verify(struct inode *inode,
142 sector_t block_nr,
143 struct ext4_xattr_header *hdr)
144{
145 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
146 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
147 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
148 return 0;
149 return 1;
150}
151
152static void ext4_xattr_block_csum_set(struct inode *inode,
153 sector_t block_nr,
154 struct ext4_xattr_header *hdr)
155{
156 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
157 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
158 return;
159
160 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
161}
162
163static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
164 struct inode *inode,
165 struct buffer_head *bh)
166{
167 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
168 return ext4_handle_dirty_metadata(handle, inode, bh);
169}
170
Stephen Hemminger11e27522010-05-13 17:53:18 -0700171static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700172ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700174 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175
Mingming Cao617ba132006-10-11 01:20:53 -0700176 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
177 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178 return handler;
179}
180
181/*
182 * Inode operation listxattr()
183 *
184 * dentry->d_inode->i_mutex: don't care
185 */
186ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700187ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000189 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190}
191
192static int
Mingming Cao617ba132006-10-11 01:20:53 -0700193ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194{
195 while (!IS_LAST_ENTRY(entry)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700196 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 if ((void *)next >= end)
198 return -EIO;
199 entry = next;
200 }
201 return 0;
202}
203
204static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400205ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400207 int error;
208
209 if (buffer_verified(bh))
210 return 0;
211
Mingming Cao617ba132006-10-11 01:20:53 -0700212 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 BHDR(bh)->h_blocks != cpu_to_le32(1))
214 return -EIO;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400215 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
216 return -EIO;
217 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
218 if (!error)
219 set_buffer_verified(bh);
220 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221}
222
223static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700224ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225{
226 size_t value_size = le32_to_cpu(entry->e_value_size);
227
228 if (entry->e_value_block != 0 || value_size > size ||
229 le16_to_cpu(entry->e_value_offs) + value_size > size)
230 return -EIO;
231 return 0;
232}
233
234static int
Mingming Cao617ba132006-10-11 01:20:53 -0700235ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236 const char *name, size_t size, int sorted)
237{
Mingming Cao617ba132006-10-11 01:20:53 -0700238 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239 size_t name_len;
240 int cmp = 1;
241
242 if (name == NULL)
243 return -EINVAL;
244 name_len = strlen(name);
245 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700246 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 cmp = name_index - entry->e_name_index;
248 if (!cmp)
249 cmp = name_len - entry->e_name_len;
250 if (!cmp)
251 cmp = memcmp(name, entry->e_name, name_len);
252 if (cmp <= 0 && (sorted || cmp == 0))
253 break;
254 }
255 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700256 if (!cmp && ext4_xattr_check_entry(entry, size))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 return -EIO;
258 return cmp ? -ENODATA : 0;
259}
260
261static int
Mingming Cao617ba132006-10-11 01:20:53 -0700262ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 void *buffer, size_t buffer_size)
264{
265 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700266 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 size_t size;
268 int error;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400269 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270
271 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
272 name_index, name, buffer, (long)buffer_size);
273
274 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700275 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400277 ea_idebug(inode, "reading block %llu",
278 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700279 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280 if (!bh)
281 goto cleanup;
282 ea_bdebug(bh, "b_count=%d, refcount=%d",
283 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400284 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500285bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400286 EXT4_ERROR_INODE(inode, "bad block %llu",
287 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 error = -EIO;
289 goto cleanup;
290 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400291 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700293 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 if (error == -EIO)
295 goto bad_block;
296 if (error)
297 goto cleanup;
298 size = le32_to_cpu(entry->e_value_size);
299 if (buffer) {
300 error = -ERANGE;
301 if (size > buffer_size)
302 goto cleanup;
303 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
304 size);
305 }
306 error = size;
307
308cleanup:
309 brelse(bh);
310 return error;
311}
312
Tao Ma879b3822012-12-05 10:28:46 -0500313int
Mingming Cao617ba132006-10-11 01:20:53 -0700314ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315 void *buffer, size_t buffer_size)
316{
Mingming Cao617ba132006-10-11 01:20:53 -0700317 struct ext4_xattr_ibody_header *header;
318 struct ext4_xattr_entry *entry;
319 struct ext4_inode *raw_inode;
320 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 size_t size;
322 void *end;
323 int error;
324
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500325 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700327 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 if (error)
329 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700330 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 header = IHDR(inode, raw_inode);
332 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700333 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
334 error = ext4_xattr_check_names(entry, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335 if (error)
336 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700337 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 end - (void *)entry, 0);
339 if (error)
340 goto cleanup;
341 size = le32_to_cpu(entry->e_value_size);
342 if (buffer) {
343 error = -ERANGE;
344 if (size > buffer_size)
345 goto cleanup;
346 memcpy(buffer, (void *)IFIRST(header) +
347 le16_to_cpu(entry->e_value_offs), size);
348 }
349 error = size;
350
351cleanup:
352 brelse(iloc.bh);
353 return error;
354}
355
356/*
Mingming Cao617ba132006-10-11 01:20:53 -0700357 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 *
359 * Copy an extended attribute into the buffer
360 * provided, or compute the buffer size required.
361 * Buffer is NULL to compute the size of the buffer required.
362 *
363 * Returns a negative error number on failure, or the number of bytes
364 * used / required on success.
365 */
366int
Mingming Cao617ba132006-10-11 01:20:53 -0700367ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 void *buffer, size_t buffer_size)
369{
370 int error;
371
Zhang Zhen230b8c1a2014-05-12 09:57:59 -0400372 if (strlen(name) > 255)
373 return -ERANGE;
374
Mingming Cao617ba132006-10-11 01:20:53 -0700375 down_read(&EXT4_I(inode)->xattr_sem);
376 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377 buffer_size);
378 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700379 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700381 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382 return error;
383}
384
385static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000386ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700387 char *buffer, size_t buffer_size)
388{
389 size_t rest = buffer_size;
390
Mingming Cao617ba132006-10-11 01:20:53 -0700391 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700392 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700393 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700394
395 if (handler) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000396 size_t size = handler->list(dentry, buffer, rest,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700397 entry->e_name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000398 entry->e_name_len,
399 handler->flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 if (buffer) {
401 if (size > rest)
402 return -ERANGE;
403 buffer += size;
404 }
405 rest -= size;
406 }
407 }
408 return buffer_size - rest;
409}
410
411static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000412ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000414 struct inode *inode = dentry->d_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415 struct buffer_head *bh = NULL;
416 int error;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400417 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418
419 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
420 buffer, (long)buffer_size);
421
422 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700423 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700424 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400425 ea_idebug(inode, "reading block %llu",
426 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700427 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 error = -EIO;
429 if (!bh)
430 goto cleanup;
431 ea_bdebug(bh, "b_count=%d, refcount=%d",
432 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400433 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400434 EXT4_ERROR_INODE(inode, "bad block %llu",
435 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 error = -EIO;
437 goto cleanup;
438 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400439 ext4_xattr_cache_insert(ext4_mb_cache, bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000440 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441
442cleanup:
443 brelse(bh);
444
445 return error;
446}
447
448static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000449ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000451 struct inode *inode = dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700452 struct ext4_xattr_ibody_header *header;
453 struct ext4_inode *raw_inode;
454 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 void *end;
456 int error;
457
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500458 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700460 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461 if (error)
462 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700463 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700465 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
466 error = ext4_xattr_check_names(IFIRST(header), end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700467 if (error)
468 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000469 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 buffer, buffer_size);
471
472cleanup:
473 brelse(iloc.bh);
474 return error;
475}
476
477/*
Mingming Cao617ba132006-10-11 01:20:53 -0700478 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 *
480 * Copy a list of attribute names into the buffer
481 * provided, or compute the buffer size required.
482 * Buffer is NULL to compute the size of the buffer required.
483 *
484 * Returns a negative error number on failure, or the number of bytes
485 * used / required on success.
486 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400487static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000488ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500490 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491
Christoph Hellwig431547b2009-11-13 09:52:56 +0000492 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500493 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
494 if (ret < 0)
495 goto errout;
496 if (buffer) {
497 buffer += ret;
498 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500500 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
501 if (ret < 0)
502 goto errout;
503 ret += ret2;
504errout:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000505 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500506 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507}
508
509/*
Mingming Cao617ba132006-10-11 01:20:53 -0700510 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 * not set, set it.
512 */
Mingming Cao617ba132006-10-11 01:20:53 -0700513static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 struct super_block *sb)
515{
Mingming Cao617ba132006-10-11 01:20:53 -0700516 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 return;
518
Mingming Cao617ba132006-10-11 01:20:53 -0700519 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Andreas Gruenbachered2908f2006-12-06 20:36:16 -0800520 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400521 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523}
524
525/*
Jan Karaec4cb1a2014-04-07 10:54:21 -0400526 * Release the xattr block BH: If the reference count is > 1, decrement it;
527 * otherwise free the block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 */
529static void
Mingming Cao617ba132006-10-11 01:20:53 -0700530ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 struct buffer_head *bh)
532{
533 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800534 int error = 0;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400535 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400537 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800538 error = ext4_journal_get_write_access(handle, bh);
539 if (error)
540 goto out;
541
542 lock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
544 ea_bdebug(bh, "refcount now=0; freeing");
545 if (ce)
546 mb_cache_entry_free(ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 get_bh(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400548 unlock_buffer(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500549 ext4_free_blocks(handle, inode, bh, 0, 1,
550 EXT4_FREE_BLOCKS_METADATA |
551 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 } else {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400553 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500554 if (ce)
555 mb_cache_entry_release(ce);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400556 /*
557 * Beware of this ugliness: Releasing of xattr block references
558 * from different inodes can race and so we have to protect
559 * from a race where someone else frees the block (and releases
560 * its journal_head) before we are done dirtying the buffer. In
561 * nojournal mode this race is harmless and we actually cannot
562 * call ext4_handle_dirty_xattr_block() with locked buffer as
563 * that function can call sync_dirty_buffer() so for that case
564 * we handle the dirtying after unlocking the buffer.
565 */
566 if (ext4_handle_valid(handle))
567 error = ext4_handle_dirty_xattr_block(handle, inode,
568 bh);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500569 unlock_buffer(bh);
Jan Karaec4cb1a2014-04-07 10:54:21 -0400570 if (!ext4_handle_valid(handle))
571 error = ext4_handle_dirty_xattr_block(handle, inode,
572 bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800573 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500574 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500575 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800576 ea_bdebug(bh, "refcount now=%d; releasing",
577 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800579out:
580 ext4_std_error(inode->i_sb, error);
581 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582}
583
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400584/*
585 * Find the available free space for EAs. This also returns the total number of
586 * bytes used by EA entries.
587 */
588static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
589 size_t *min_offs, void *base, int *total)
590{
591 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400592 if (!last->e_value_block && last->e_value_size) {
593 size_t offs = le16_to_cpu(last->e_value_offs);
594 if (offs < *min_offs)
595 *min_offs = offs;
596 }
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -0500597 if (total)
598 *total += EXT4_XATTR_LEN(last->e_name_len);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400599 }
600 return (*min_offs - ((void *)last - base) - sizeof(__u32));
601}
602
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603static int
Mingming Cao617ba132006-10-11 01:20:53 -0700604ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700605{
Mingming Cao617ba132006-10-11 01:20:53 -0700606 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
608
609 /* Compute min_offs and last. */
610 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700611 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 if (!last->e_value_block && last->e_value_size) {
613 size_t offs = le16_to_cpu(last->e_value_offs);
614 if (offs < min_offs)
615 min_offs = offs;
616 }
617 }
618 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
619 if (!s->not_found) {
620 if (!s->here->e_value_block && s->here->e_value_size) {
621 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700622 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 }
Mingming Cao617ba132006-10-11 01:20:53 -0700624 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 }
626 if (i->value) {
Mingming Cao617ba132006-10-11 01:20:53 -0700627 if (free < EXT4_XATTR_SIZE(i->value_len) ||
628 free < EXT4_XATTR_LEN(name_len) +
629 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 return -ENOSPC;
631 }
632
633 if (i->value && s->not_found) {
634 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700635 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
637 memmove((void *)s->here + size, s->here, rest);
638 memset(s->here, 0, size);
639 s->here->e_name_index = i->name_index;
640 s->here->e_name_len = name_len;
641 memcpy(s->here->e_name, i->name, name_len);
642 } else {
643 if (!s->here->e_value_block && s->here->e_value_size) {
644 void *first_val = s->base + min_offs;
645 size_t offs = le16_to_cpu(s->here->e_value_offs);
646 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700647 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700648 le32_to_cpu(s->here->e_value_size));
649
Mingming Cao617ba132006-10-11 01:20:53 -0700650 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 /* The old and the new value have the same
652 size. Just replace. */
653 s->here->e_value_size =
654 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500655 if (i->value == EXT4_ZERO_XATTR_VALUE) {
656 memset(val, 0, size);
657 } else {
658 /* Clear pad bytes first. */
659 memset(val + size - EXT4_XATTR_PAD, 0,
660 EXT4_XATTR_PAD);
661 memcpy(val, i->value, i->value_len);
662 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 return 0;
664 }
665
666 /* Remove the old value. */
667 memmove(first_val + size, first_val, val - first_val);
668 memset(first_val, 0, size);
669 s->here->e_value_size = 0;
670 s->here->e_value_offs = 0;
671 min_offs += size;
672
673 /* Adjust all value offsets. */
674 last = s->first;
675 while (!IS_LAST_ENTRY(last)) {
676 size_t o = le16_to_cpu(last->e_value_offs);
677 if (!last->e_value_block &&
678 last->e_value_size && o < offs)
679 last->e_value_offs =
680 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700681 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 }
683 }
684 if (!i->value) {
685 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700686 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 last = ENTRY((void *)last - size);
688 memmove(s->here, (void *)s->here + size,
689 (void *)last - (void *)s->here + sizeof(__u32));
690 memset(last, 0, size);
691 }
692 }
693
694 if (i->value) {
695 /* Insert the new value. */
696 s->here->e_value_size = cpu_to_le32(i->value_len);
697 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700698 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 void *val = s->base + min_offs - size;
700 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500701 if (i->value == EXT4_ZERO_XATTR_VALUE) {
702 memset(val, 0, size);
703 } else {
704 /* Clear the pad bytes first. */
705 memset(val + size - EXT4_XATTR_PAD, 0,
706 EXT4_XATTR_PAD);
707 memcpy(val, i->value, i->value_len);
708 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 }
710 }
711 return 0;
712}
713
Mingming Cao617ba132006-10-11 01:20:53 -0700714struct ext4_xattr_block_find {
715 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 struct buffer_head *bh;
717};
718
719static int
Mingming Cao617ba132006-10-11 01:20:53 -0700720ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
721 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722{
723 struct super_block *sb = inode->i_sb;
724 int error;
725
726 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
727 i->name_index, i->name, i->value, (long)i->value_len);
728
Mingming Cao617ba132006-10-11 01:20:53 -0700729 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700731 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732 error = -EIO;
733 if (!bs->bh)
734 goto cleanup;
735 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
736 atomic_read(&(bs->bh->b_count)),
737 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400738 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400739 EXT4_ERROR_INODE(inode, "bad block %llu",
740 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 error = -EIO;
742 goto cleanup;
743 }
744 /* Find the named attribute. */
745 bs->s.base = BHDR(bs->bh);
746 bs->s.first = BFIRST(bs->bh);
747 bs->s.end = bs->bh->b_data + bs->bh->b_size;
748 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700749 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 i->name, bs->bh->b_size, 1);
751 if (error && error != -ENODATA)
752 goto cleanup;
753 bs->s.not_found = error;
754 }
755 error = 0;
756
757cleanup:
758 return error;
759}
760
761static int
Mingming Cao617ba132006-10-11 01:20:53 -0700762ext4_xattr_block_set(handle_t *handle, struct inode *inode,
763 struct ext4_xattr_info *i,
764 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765{
766 struct super_block *sb = inode->i_sb;
767 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700768 struct ext4_xattr_search *s = &bs->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800770 int error = 0;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400771 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772
Mingming Cao617ba132006-10-11 01:20:53 -0700773#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774
775 if (i->value && i->value_len > sb->s_blocksize)
776 return -ENOSPC;
777 if (s->base) {
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400778 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 bs->bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800780 error = ext4_journal_get_write_access(handle, bs->bh);
781 if (error)
782 goto cleanup;
783 lock_buffer(bs->bh);
784
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
786 if (ce) {
787 mb_cache_entry_free(ce);
788 ce = NULL;
789 }
790 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700791 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 if (!error) {
793 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700794 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 s->here);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400796 ext4_xattr_cache_insert(ext4_mb_cache,
797 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 }
799 unlock_buffer(bs->bh);
800 if (error == -EIO)
801 goto bad_block;
802 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400803 error = ext4_handle_dirty_xattr_block(handle,
804 inode,
805 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 if (error)
807 goto cleanup;
808 goto inserted;
809 } else {
810 int offset = (char *)s->here - bs->bh->b_data;
811
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800812 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 if (ce) {
814 mb_cache_entry_release(ce);
815 ce = NULL;
816 }
817 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400818 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 error = -ENOMEM;
820 if (s->base == NULL)
821 goto cleanup;
822 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
823 s->first = ENTRY(header(s->base)+1);
824 header(s->base)->h_refcount = cpu_to_le32(1);
825 s->here = ENTRY(s->base + offset);
826 s->end = s->base + bs->bh->b_size;
827 }
828 } else {
829 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400830 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831 /* assert(header == s->base) */
832 error = -ENOMEM;
833 if (s->base == NULL)
834 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700835 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 header(s->base)->h_blocks = cpu_to_le32(1);
837 header(s->base)->h_refcount = cpu_to_le32(1);
838 s->first = ENTRY(header(s->base)+1);
839 s->here = ENTRY(header(s->base)+1);
840 s->end = s->base + sb->s_blocksize;
841 }
842
Mingming Cao617ba132006-10-11 01:20:53 -0700843 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 if (error == -EIO)
845 goto bad_block;
846 if (error)
847 goto cleanup;
848 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700849 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850
851inserted:
852 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700853 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 if (new_bh) {
855 /* We found an identical block in the cache. */
856 if (new_bh == bs->bh)
857 ea_bdebug(new_bh, "keeping");
858 else {
859 /* The old block is released after updating
860 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500861 error = dquot_alloc_block(inode,
862 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500863 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700865 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866 new_bh);
867 if (error)
868 goto cleanup_dquot;
869 lock_buffer(new_bh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400870 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 ea_bdebug(new_bh, "reusing; refcount now=%d",
872 le32_to_cpu(BHDR(new_bh)->h_refcount));
873 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400874 error = ext4_handle_dirty_xattr_block(handle,
875 inode,
876 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 if (error)
878 goto cleanup_dquot;
879 }
880 mb_cache_entry_release(ce);
881 ce = NULL;
882 } else if (bs->bh && s->base == bs->bh->b_data) {
883 /* We were modifying this block in-place. */
884 ea_bdebug(bs->bh, "keeping this block");
885 new_bh = bs->bh;
886 get_bh(new_bh);
887 } else {
888 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400889 ext4_fsblk_t goal, block;
890
891 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400892 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400893
894 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400895 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400896 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
897
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400898 /*
899 * take i_data_sem because we will test
900 * i_delalloc_reserved_flag in ext4_mb_new_blocks
901 */
902 down_read((&EXT4_I(inode)->i_data_sem));
Allison Henderson55f020d2011-05-25 07:41:26 -0400903 block = ext4_new_meta_blocks(handle, inode, goal, 0,
904 NULL, &error);
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400905 up_read((&EXT4_I(inode)->i_data_sem));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906 if (error)
907 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400908
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400909 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400910 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
911
Joe Perchesace36ad2012-03-19 23:11:43 -0400912 ea_idebug(inode, "creating block %llu",
913 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914
915 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500916 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500917 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500919 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500920 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 goto cleanup;
922 }
923 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700924 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925 if (error) {
926 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500927 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928 goto getblk_failed;
929 }
930 memcpy(new_bh->b_data, s->base, new_bh->b_size);
931 set_buffer_uptodate(new_bh);
932 unlock_buffer(new_bh);
T Makphaibulchoke9c191f72014-03-18 19:24:49 -0400933 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400934 error = ext4_handle_dirty_xattr_block(handle,
935 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 if (error)
937 goto cleanup;
938 }
939 }
940
941 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -0700942 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943
944 /* Drop the previous xattr block. */
945 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700946 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947 error = 0;
948
949cleanup:
950 if (ce)
951 mb_cache_entry_release(ce);
952 brelse(new_bh);
953 if (!(bs->bh && s->base == bs->bh->b_data))
954 kfree(s->base);
955
956 return error;
957
958cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500959 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960 goto cleanup;
961
962bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400963 EXT4_ERROR_INODE(inode, "bad block %llu",
964 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 goto cleanup;
966
967#undef header
968}
969
Tao Ma879b3822012-12-05 10:28:46 -0500970int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
971 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972{
Mingming Cao617ba132006-10-11 01:20:53 -0700973 struct ext4_xattr_ibody_header *header;
974 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700975 int error;
976
Mingming Cao617ba132006-10-11 01:20:53 -0700977 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700979 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980 header = IHDR(inode, raw_inode);
981 is->s.base = is->s.first = IFIRST(header);
982 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700983 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500984 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700985 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 if (error)
987 return error;
988 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -0700989 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700990 i->name, is->s.end -
991 (void *)is->s.base, 0);
992 if (error && error != -ENODATA)
993 return error;
994 is->s.not_found = error;
995 }
996 return 0;
997}
998
Tao Ma0d812f72012-12-10 14:06:02 -0500999int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1000 struct ext4_xattr_info *i,
1001 struct ext4_xattr_ibody_find *is)
1002{
1003 struct ext4_xattr_ibody_header *header;
1004 struct ext4_xattr_search *s = &is->s;
1005 int error;
1006
1007 if (EXT4_I(inode)->i_extra_isize == 0)
1008 return -ENOSPC;
1009 error = ext4_xattr_set_entry(i, s);
1010 if (error) {
1011 if (error == -ENOSPC &&
1012 ext4_has_inline_data(inode)) {
1013 error = ext4_try_to_evict_inline_data(handle, inode,
1014 EXT4_XATTR_LEN(strlen(i->name) +
1015 EXT4_XATTR_SIZE(i->value_len)));
1016 if (error)
1017 return error;
1018 error = ext4_xattr_ibody_find(inode, i, is);
1019 if (error)
1020 return error;
1021 error = ext4_xattr_set_entry(i, s);
1022 }
1023 if (error)
1024 return error;
1025 }
1026 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1027 if (!IS_LAST_ENTRY(s->first)) {
1028 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1029 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1030 } else {
1031 header->h_magic = cpu_to_le32(0);
1032 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1033 }
1034 return 0;
1035}
1036
1037static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1038 struct ext4_xattr_info *i,
1039 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040{
Mingming Cao617ba132006-10-11 01:20:53 -07001041 struct ext4_xattr_ibody_header *header;
1042 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 int error;
1044
Mingming Cao617ba132006-10-11 01:20:53 -07001045 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001047 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048 if (error)
1049 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001050 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001052 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001053 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 } else {
1055 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001056 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057 }
1058 return 0;
1059}
1060
1061/*
Mingming Cao617ba132006-10-11 01:20:53 -07001062 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001064 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 * is NULL to remove an existing extended attribute, and non-NULL to
1066 * either replace an existing extended attribute, or create a new extended
1067 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1068 * specify that an extended attribute must exist and must not exist
1069 * previous to the call, respectively.
1070 *
1071 * Returns 0, or a negative error number on failure.
1072 */
1073int
Mingming Cao617ba132006-10-11 01:20:53 -07001074ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 const char *name, const void *value, size_t value_len,
1076 int flags)
1077{
Mingming Cao617ba132006-10-11 01:20:53 -07001078 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 .name_index = name_index,
1080 .name = name,
1081 .value = value,
1082 .value_len = value_len,
1083
1084 };
Mingming Cao617ba132006-10-11 01:20:53 -07001085 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 .s = { .not_found = -ENODATA, },
1087 };
Mingming Cao617ba132006-10-11 01:20:53 -07001088 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 .s = { .not_found = -ENODATA, },
1090 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001091 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001092 int error;
1093
1094 if (!name)
1095 return -EINVAL;
1096 if (strlen(name) > 255)
1097 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001098 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001099 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1100 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001101
Eric Sandeen66543612011-10-26 03:32:07 -04001102 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001103 if (error)
1104 goto cleanup;
1105
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001106 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001107 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1108 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001109 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 }
1111
Mingming Cao617ba132006-10-11 01:20:53 -07001112 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113 if (error)
1114 goto cleanup;
1115 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001116 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 if (error)
1118 goto cleanup;
1119 if (is.s.not_found && bs.s.not_found) {
1120 error = -ENODATA;
1121 if (flags & XATTR_REPLACE)
1122 goto cleanup;
1123 error = 0;
1124 if (!value)
1125 goto cleanup;
1126 } else {
1127 error = -EEXIST;
1128 if (flags & XATTR_CREATE)
1129 goto cleanup;
1130 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 if (!value) {
1132 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001133 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001135 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001137 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 if (!error && !bs.s.not_found) {
1139 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001140 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001142 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1143 error = ext4_xattr_block_find(inode, &i, &bs);
1144 if (error)
1145 goto cleanup;
1146 }
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 if (error)
1149 goto cleanup;
1150 if (!is.s.not_found) {
1151 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001152 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153 &is);
1154 }
1155 }
1156 }
1157 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001158 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001159 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001160 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001161 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001162 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001164 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 * error != 0.
1166 */
1167 is.iloc.bh = NULL;
1168 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001169 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 }
1171
1172cleanup:
1173 brelse(is.iloc.bh);
1174 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001175 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001176 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001177 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 return error;
1179}
1180
1181/*
Mingming Cao617ba132006-10-11 01:20:53 -07001182 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 *
Mingming Cao617ba132006-10-11 01:20:53 -07001184 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 * attribute modification is a filesystem transaction by itself.
1186 *
1187 * Returns 0, or a negative error number on failure.
1188 */
1189int
Mingming Cao617ba132006-10-11 01:20:53 -07001190ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191 const void *value, size_t value_len, int flags)
1192{
1193 handle_t *handle;
1194 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001195 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196
1197retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001198 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 if (IS_ERR(handle)) {
1200 error = PTR_ERR(handle);
1201 } else {
1202 int error2;
1203
Mingming Cao617ba132006-10-11 01:20:53 -07001204 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001206 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001207 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001208 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 goto retry;
1210 if (error == 0)
1211 error = error2;
1212 }
1213
1214 return error;
1215}
1216
1217/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001218 * Shift the EA entries in the inode to create space for the increased
1219 * i_extra_isize.
1220 */
1221static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1222 int value_offs_shift, void *to,
1223 void *from, size_t n, int blocksize)
1224{
1225 struct ext4_xattr_entry *last = entry;
1226 int new_offs;
1227
1228 /* Adjust the value offsets of the entries */
1229 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1230 if (!last->e_value_block && last->e_value_size) {
1231 new_offs = le16_to_cpu(last->e_value_offs) +
1232 value_offs_shift;
1233 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1234 > blocksize);
1235 last->e_value_offs = cpu_to_le16(new_offs);
1236 }
1237 }
1238 /* Shift the entries by n bytes */
1239 memmove(to, from, n);
1240}
1241
1242/*
1243 * Expand an inode by new_extra_isize bytes when EAs are present.
1244 * Returns 0 on success or negative error number on failure.
1245 */
1246int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1247 struct ext4_inode *raw_inode, handle_t *handle)
1248{
1249 struct ext4_xattr_ibody_header *header;
1250 struct ext4_xattr_entry *entry, *last, *first;
1251 struct buffer_head *bh = NULL;
1252 struct ext4_xattr_ibody_find *is = NULL;
1253 struct ext4_xattr_block_find *bs = NULL;
1254 char *buffer = NULL, *b_entry_name = NULL;
1255 size_t min_offs, free;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001256 int total_ino;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001257 void *base, *start, *end;
1258 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001259 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 -04001260
1261 down_write(&EXT4_I(inode)->xattr_sem);
1262retry:
1263 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1264 up_write(&EXT4_I(inode)->xattr_sem);
1265 return 0;
1266 }
1267
1268 header = IHDR(inode, raw_inode);
1269 entry = IFIRST(header);
1270
1271 /*
1272 * Check if enough free space is available in the inode to shift the
1273 * entries ahead by new_extra_isize.
1274 */
1275
1276 base = start = entry;
1277 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1278 min_offs = end - base;
1279 last = entry;
1280 total_ino = sizeof(struct ext4_xattr_ibody_header);
1281
1282 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1283 if (free >= new_extra_isize) {
1284 entry = IFIRST(header);
1285 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1286 - new_extra_isize, (void *)raw_inode +
1287 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1288 (void *)header, total_ino,
1289 inode->i_sb->s_blocksize);
1290 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1291 error = 0;
1292 goto cleanup;
1293 }
1294
1295 /*
1296 * Enough free space isn't available in the inode, check if
1297 * EA block can hold new_extra_isize bytes.
1298 */
1299 if (EXT4_I(inode)->i_file_acl) {
1300 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1301 error = -EIO;
1302 if (!bh)
1303 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001304 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001305 EXT4_ERROR_INODE(inode, "bad block %llu",
1306 EXT4_I(inode)->i_file_acl);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001307 error = -EIO;
1308 goto cleanup;
1309 }
1310 base = BHDR(bh);
1311 first = BFIRST(bh);
1312 end = bh->b_data + bh->b_size;
1313 min_offs = end - base;
Theodore Ts'o7b1b2c12014-02-19 20:15:21 -05001314 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001315 if (free < new_extra_isize) {
1316 if (!tried_min_extra_isize && s_min_extra_isize) {
1317 tried_min_extra_isize++;
1318 new_extra_isize = s_min_extra_isize;
1319 brelse(bh);
1320 goto retry;
1321 }
1322 error = -1;
1323 goto cleanup;
1324 }
1325 } else {
1326 free = inode->i_sb->s_blocksize;
1327 }
1328
1329 while (new_extra_isize > 0) {
1330 size_t offs, size, entry_size;
1331 struct ext4_xattr_entry *small_entry = NULL;
1332 struct ext4_xattr_info i = {
1333 .value = NULL,
1334 .value_len = 0,
1335 };
1336 unsigned int total_size; /* EA entry size + value size */
1337 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1338 unsigned int min_total_size = ~0U;
1339
1340 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1341 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1342 if (!is || !bs) {
1343 error = -ENOMEM;
1344 goto cleanup;
1345 }
1346
1347 is->s.not_found = -ENODATA;
1348 bs->s.not_found = -ENODATA;
1349 is->iloc.bh = NULL;
1350 bs->bh = NULL;
1351
1352 last = IFIRST(header);
1353 /* Find the entry best suited to be pushed into EA block */
1354 entry = NULL;
1355 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1356 total_size =
1357 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1358 EXT4_XATTR_LEN(last->e_name_len);
1359 if (total_size <= free && total_size < min_total_size) {
1360 if (total_size < new_extra_isize) {
1361 small_entry = last;
1362 } else {
1363 entry = last;
1364 min_total_size = total_size;
1365 }
1366 }
1367 }
1368
1369 if (entry == NULL) {
1370 if (small_entry) {
1371 entry = small_entry;
1372 } else {
1373 if (!tried_min_extra_isize &&
1374 s_min_extra_isize) {
1375 tried_min_extra_isize++;
1376 new_extra_isize = s_min_extra_isize;
Dave Jones6e4ea8e2013-10-10 20:05:35 -04001377 kfree(is); is = NULL;
1378 kfree(bs); bs = NULL;
Theodore Ts'odcb99172013-10-31 23:00:24 -04001379 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001380 goto retry;
1381 }
1382 error = -1;
1383 goto cleanup;
1384 }
1385 }
1386 offs = le16_to_cpu(entry->e_value_offs);
1387 size = le32_to_cpu(entry->e_value_size);
1388 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1389 i.name_index = entry->e_name_index,
1390 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1391 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1392 if (!buffer || !b_entry_name) {
1393 error = -ENOMEM;
1394 goto cleanup;
1395 }
1396 /* Save the entry name and the entry value */
1397 memcpy(buffer, (void *)IFIRST(header) + offs,
1398 EXT4_XATTR_SIZE(size));
1399 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1400 b_entry_name[entry->e_name_len] = '\0';
1401 i.name = b_entry_name;
1402
1403 error = ext4_get_inode_loc(inode, &is->iloc);
1404 if (error)
1405 goto cleanup;
1406
1407 error = ext4_xattr_ibody_find(inode, &i, is);
1408 if (error)
1409 goto cleanup;
1410
1411 /* Remove the chosen entry from the inode */
1412 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001413 if (error)
1414 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001415
1416 entry = IFIRST(header);
1417 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1418 shift_bytes = new_extra_isize;
1419 else
1420 shift_bytes = entry_size + size;
1421 /* Adjust the offsets and shift the remaining entries ahead */
1422 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1423 shift_bytes, (void *)raw_inode +
1424 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1425 (void *)header, total_ino - entry_size,
1426 inode->i_sb->s_blocksize);
1427
1428 extra_isize += shift_bytes;
1429 new_extra_isize -= shift_bytes;
1430 EXT4_I(inode)->i_extra_isize = extra_isize;
1431
1432 i.name = b_entry_name;
1433 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001434 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001435 error = ext4_xattr_block_find(inode, &i, bs);
1436 if (error)
1437 goto cleanup;
1438
1439 /* Add entry which was removed from the inode into the block */
1440 error = ext4_xattr_block_set(handle, inode, &i, bs);
1441 if (error)
1442 goto cleanup;
1443 kfree(b_entry_name);
1444 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001445 b_entry_name = NULL;
1446 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001447 brelse(is->iloc.bh);
1448 kfree(is);
1449 kfree(bs);
1450 }
1451 brelse(bh);
1452 up_write(&EXT4_I(inode)->xattr_sem);
1453 return 0;
1454
1455cleanup:
1456 kfree(b_entry_name);
1457 kfree(buffer);
1458 if (is)
1459 brelse(is->iloc.bh);
1460 kfree(is);
1461 kfree(bs);
1462 brelse(bh);
1463 up_write(&EXT4_I(inode)->xattr_sem);
1464 return error;
1465}
1466
1467
1468
1469/*
Mingming Cao617ba132006-10-11 01:20:53 -07001470 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 *
1472 * Free extended attribute resources associated with this inode. This
1473 * is called immediately before an inode is freed. We have exclusive
1474 * access to the inode.
1475 */
1476void
Mingming Cao617ba132006-10-11 01:20:53 -07001477ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001478{
1479 struct buffer_head *bh = NULL;
1480
Mingming Cao617ba132006-10-11 01:20:53 -07001481 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001483 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001485 EXT4_ERROR_INODE(inode, "block %llu read error",
1486 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487 goto cleanup;
1488 }
Mingming Cao617ba132006-10-11 01:20:53 -07001489 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001491 EXT4_ERROR_INODE(inode, "bad block %llu",
1492 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 goto cleanup;
1494 }
Mingming Cao617ba132006-10-11 01:20:53 -07001495 ext4_xattr_release_block(handle, inode, bh);
1496 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497
1498cleanup:
1499 brelse(bh);
1500}
1501
1502/*
Mingming Cao617ba132006-10-11 01:20:53 -07001503 * ext4_xattr_put_super()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 *
1505 * This is called when a file system is unmounted.
1506 */
1507void
Mingming Cao617ba132006-10-11 01:20:53 -07001508ext4_xattr_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509{
1510 mb_cache_shrink(sb->s_bdev);
1511}
1512
1513/*
Mingming Cao617ba132006-10-11 01:20:53 -07001514 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001515 *
1516 * Create a new entry in the extended attribute cache, and insert
1517 * it unless such an entry is already in the cache.
1518 *
1519 * Returns 0, or a negative error number on failure.
1520 */
1521static void
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001522ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523{
1524 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1525 struct mb_cache_entry *ce;
1526 int error;
1527
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001528 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001529 if (!ce) {
1530 ea_bdebug(bh, "out of memory");
1531 return;
1532 }
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001533 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534 if (error) {
1535 mb_cache_entry_free(ce);
1536 if (error == -EBUSY) {
1537 ea_bdebug(bh, "already in cache");
1538 error = 0;
1539 }
1540 } else {
1541 ea_bdebug(bh, "inserting [%x]", (int)hash);
1542 mb_cache_entry_release(ce);
1543 }
1544}
1545
1546/*
Mingming Cao617ba132006-10-11 01:20:53 -07001547 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001548 *
1549 * Compare two extended attribute blocks for equality.
1550 *
1551 * Returns 0 if the blocks are equal, 1 if they differ, and
1552 * a negative error number on errors.
1553 */
1554static int
Mingming Cao617ba132006-10-11 01:20:53 -07001555ext4_xattr_cmp(struct ext4_xattr_header *header1,
1556 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001557{
Mingming Cao617ba132006-10-11 01:20:53 -07001558 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001559
1560 entry1 = ENTRY(header1+1);
1561 entry2 = ENTRY(header2+1);
1562 while (!IS_LAST_ENTRY(entry1)) {
1563 if (IS_LAST_ENTRY(entry2))
1564 return 1;
1565 if (entry1->e_hash != entry2->e_hash ||
1566 entry1->e_name_index != entry2->e_name_index ||
1567 entry1->e_name_len != entry2->e_name_len ||
1568 entry1->e_value_size != entry2->e_value_size ||
1569 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1570 return 1;
1571 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1572 return -EIO;
1573 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1574 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1575 le32_to_cpu(entry1->e_value_size)))
1576 return 1;
1577
Mingming Cao617ba132006-10-11 01:20:53 -07001578 entry1 = EXT4_XATTR_NEXT(entry1);
1579 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 }
1581 if (!IS_LAST_ENTRY(entry2))
1582 return 1;
1583 return 0;
1584}
1585
1586/*
Mingming Cao617ba132006-10-11 01:20:53 -07001587 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 *
1589 * Find an identical extended attribute block.
1590 *
1591 * Returns a pointer to the block found, or NULL if such a block was
1592 * not found or an error occurred.
1593 */
1594static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001595ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 struct mb_cache_entry **pce)
1597{
1598 __u32 hash = le32_to_cpu(header->h_hash);
1599 struct mb_cache_entry *ce;
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001600 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601
1602 if (!header->h_hash)
1603 return NULL; /* never share */
1604 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1605again:
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001606 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001607 hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608 while (ce) {
1609 struct buffer_head *bh;
1610
1611 if (IS_ERR(ce)) {
1612 if (PTR_ERR(ce) == -EAGAIN)
1613 goto again;
1614 break;
1615 }
1616 bh = sb_bread(inode->i_sb, ce->e_block);
1617 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001618 EXT4_ERROR_INODE(inode, "block %lu read error",
1619 (unsigned long) ce->e_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001620 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
Mingming Cao617ba132006-10-11 01:20:53 -07001621 EXT4_XATTR_REFCOUNT_MAX) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622 ea_idebug(inode, "block %lu refcount %d>=%d",
1623 (unsigned long) ce->e_block,
1624 le32_to_cpu(BHDR(bh)->h_refcount),
Mingming Cao617ba132006-10-11 01:20:53 -07001625 EXT4_XATTR_REFCOUNT_MAX);
1626 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 *pce = ce;
1628 return bh;
1629 }
1630 brelse(bh);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001631 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 }
1633 return NULL;
1634}
1635
1636#define NAME_HASH_SHIFT 5
1637#define VALUE_HASH_SHIFT 16
1638
1639/*
Mingming Cao617ba132006-10-11 01:20:53 -07001640 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641 *
1642 * Compute the hash of an extended attribute.
1643 */
Mingming Cao617ba132006-10-11 01:20:53 -07001644static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1645 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646{
1647 __u32 hash = 0;
1648 char *name = entry->e_name;
1649 int n;
1650
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001651 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 hash = (hash << NAME_HASH_SHIFT) ^
1653 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1654 *name++;
1655 }
1656
1657 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1658 __le32 *value = (__le32 *)((char *)header +
1659 le16_to_cpu(entry->e_value_offs));
1660 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001661 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 hash = (hash << VALUE_HASH_SHIFT) ^
1663 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1664 le32_to_cpu(*value++);
1665 }
1666 }
1667 entry->e_hash = cpu_to_le32(hash);
1668}
1669
1670#undef NAME_HASH_SHIFT
1671#undef VALUE_HASH_SHIFT
1672
1673#define BLOCK_HASH_SHIFT 16
1674
1675/*
Mingming Cao617ba132006-10-11 01:20:53 -07001676 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001677 *
1678 * Re-compute the extended attribute hash value after an entry has changed.
1679 */
Mingming Cao617ba132006-10-11 01:20:53 -07001680static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1681 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001682{
Mingming Cao617ba132006-10-11 01:20:53 -07001683 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 __u32 hash = 0;
1685
Mingming Cao617ba132006-10-11 01:20:53 -07001686 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 here = ENTRY(header+1);
1688 while (!IS_LAST_ENTRY(here)) {
1689 if (!here->e_hash) {
1690 /* Block is not shared if an entry's hash value == 0 */
1691 hash = 0;
1692 break;
1693 }
1694 hash = (hash << BLOCK_HASH_SHIFT) ^
1695 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1696 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001697 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 }
1699 header->h_hash = cpu_to_le32(hash);
1700}
1701
1702#undef BLOCK_HASH_SHIFT
1703
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001704#define HASH_BUCKET_BITS 10
1705
1706struct mb_cache *
1707ext4_xattr_create_cache(char *name)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001709 return mb_cache_create(name, HASH_BUCKET_BITS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710}
1711
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001712void ext4_xattr_destroy_cache(struct mb_cache *cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713{
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001714 if (cache)
1715 mb_cache_destroy(cache);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716}
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04001717