blob: e175e94116acefdc7685896e5c8744f7bd677dc4 [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
Mingming Cao617ba132006-10-11 01:20:53 -070084static void ext4_xattr_cache_insert(struct buffer_head *);
85static 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
Mingming Cao617ba132006-10-11 01:20:53 -070093static struct mb_cache *ext4_xattr_cache;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070094
Stephen Hemminger11e27522010-05-13 17:53:18 -070095static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -070096 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -040097#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -080098 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
99 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700101 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400102#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700103 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104#endif
105};
106
Stephen Hemminger11e27522010-05-13 17:53:18 -0700107const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700108 &ext4_xattr_user_handler,
109 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400110#ifdef CONFIG_EXT4_FS_POSIX_ACL
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800111 &posix_acl_access_xattr_handler,
112 &posix_acl_default_xattr_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700113#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400114#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700115 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116#endif
117 NULL
118};
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{
144 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
145 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
146 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
147 return 0;
148 return 1;
149}
150
151static void ext4_xattr_block_csum_set(struct inode *inode,
152 sector_t block_nr,
153 struct ext4_xattr_header *hdr)
154{
155 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
156 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
157 return;
158
159 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
160}
161
162static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
163 struct inode *inode,
164 struct buffer_head *bh)
165{
166 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
167 return ext4_handle_dirty_metadata(handle, inode, bh);
168}
169
Stephen Hemminger11e27522010-05-13 17:53:18 -0700170static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700171ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700173 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174
Mingming Cao617ba132006-10-11 01:20:53 -0700175 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
176 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 return handler;
178}
179
180/*
181 * Inode operation listxattr()
182 *
183 * dentry->d_inode->i_mutex: don't care
184 */
185ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700186ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000188 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189}
190
191static int
Mingming Cao617ba132006-10-11 01:20:53 -0700192ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193{
194 while (!IS_LAST_ENTRY(entry)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700195 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196 if ((void *)next >= end)
197 return -EIO;
198 entry = next;
199 }
200 return 0;
201}
202
203static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400204ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400206 int error;
207
208 if (buffer_verified(bh))
209 return 0;
210
Mingming Cao617ba132006-10-11 01:20:53 -0700211 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212 BHDR(bh)->h_blocks != cpu_to_le32(1))
213 return -EIO;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400214 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
215 return -EIO;
216 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
217 if (!error)
218 set_buffer_verified(bh);
219 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220}
221
222static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700223ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224{
225 size_t value_size = le32_to_cpu(entry->e_value_size);
226
227 if (entry->e_value_block != 0 || value_size > size ||
228 le16_to_cpu(entry->e_value_offs) + value_size > size)
229 return -EIO;
230 return 0;
231}
232
233static int
Mingming Cao617ba132006-10-11 01:20:53 -0700234ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235 const char *name, size_t size, int sorted)
236{
Mingming Cao617ba132006-10-11 01:20:53 -0700237 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 size_t name_len;
239 int cmp = 1;
240
241 if (name == NULL)
242 return -EINVAL;
243 name_len = strlen(name);
244 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700245 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 cmp = name_index - entry->e_name_index;
247 if (!cmp)
248 cmp = name_len - entry->e_name_len;
249 if (!cmp)
250 cmp = memcmp(name, entry->e_name, name_len);
251 if (cmp <= 0 && (sorted || cmp == 0))
252 break;
253 }
254 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700255 if (!cmp && ext4_xattr_check_entry(entry, size))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 return -EIO;
257 return cmp ? -ENODATA : 0;
258}
259
260static int
Mingming Cao617ba132006-10-11 01:20:53 -0700261ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 void *buffer, size_t buffer_size)
263{
264 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700265 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 size_t size;
267 int error;
268
269 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
270 name_index, name, buffer, (long)buffer_size);
271
272 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700273 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400275 ea_idebug(inode, "reading block %llu",
276 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700277 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 if (!bh)
279 goto cleanup;
280 ea_bdebug(bh, "b_count=%d, refcount=%d",
281 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400282 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500283bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400284 EXT4_ERROR_INODE(inode, "bad block %llu",
285 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 error = -EIO;
287 goto cleanup;
288 }
Mingming Cao617ba132006-10-11 01:20:53 -0700289 ext4_xattr_cache_insert(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700291 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 if (error == -EIO)
293 goto bad_block;
294 if (error)
295 goto cleanup;
296 size = le32_to_cpu(entry->e_value_size);
297 if (buffer) {
298 error = -ERANGE;
299 if (size > buffer_size)
300 goto cleanup;
301 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
302 size);
303 }
304 error = size;
305
306cleanup:
307 brelse(bh);
308 return error;
309}
310
Tao Ma879b3822012-12-05 10:28:46 -0500311int
Mingming Cao617ba132006-10-11 01:20:53 -0700312ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700313 void *buffer, size_t buffer_size)
314{
Mingming Cao617ba132006-10-11 01:20:53 -0700315 struct ext4_xattr_ibody_header *header;
316 struct ext4_xattr_entry *entry;
317 struct ext4_inode *raw_inode;
318 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319 size_t size;
320 void *end;
321 int error;
322
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500323 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700325 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 if (error)
327 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700328 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329 header = IHDR(inode, raw_inode);
330 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700331 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
332 error = ext4_xattr_check_names(entry, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 if (error)
334 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700335 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 end - (void *)entry, 0);
337 if (error)
338 goto cleanup;
339 size = le32_to_cpu(entry->e_value_size);
340 if (buffer) {
341 error = -ERANGE;
342 if (size > buffer_size)
343 goto cleanup;
344 memcpy(buffer, (void *)IFIRST(header) +
345 le16_to_cpu(entry->e_value_offs), size);
346 }
347 error = size;
348
349cleanup:
350 brelse(iloc.bh);
351 return error;
352}
353
354/*
Mingming Cao617ba132006-10-11 01:20:53 -0700355 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356 *
357 * Copy an extended attribute into the buffer
358 * provided, or compute the buffer size required.
359 * Buffer is NULL to compute the size of the buffer required.
360 *
361 * Returns a negative error number on failure, or the number of bytes
362 * used / required on success.
363 */
364int
Mingming Cao617ba132006-10-11 01:20:53 -0700365ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 void *buffer, size_t buffer_size)
367{
368 int error;
369
Mingming Cao617ba132006-10-11 01:20:53 -0700370 down_read(&EXT4_I(inode)->xattr_sem);
371 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700372 buffer_size);
373 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700374 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700376 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377 return error;
378}
379
380static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000381ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382 char *buffer, size_t buffer_size)
383{
384 size_t rest = buffer_size;
385
Mingming Cao617ba132006-10-11 01:20:53 -0700386 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700387 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700388 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700389
390 if (handler) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000391 size_t size = handler->list(dentry, buffer, rest,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 entry->e_name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000393 entry->e_name_len,
394 handler->flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700395 if (buffer) {
396 if (size > rest)
397 return -ERANGE;
398 buffer += size;
399 }
400 rest -= size;
401 }
402 }
403 return buffer_size - rest;
404}
405
406static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000407ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000409 struct inode *inode = dentry->d_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 struct buffer_head *bh = NULL;
411 int error;
412
413 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
414 buffer, (long)buffer_size);
415
416 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700417 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400419 ea_idebug(inode, "reading block %llu",
420 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700421 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422 error = -EIO;
423 if (!bh)
424 goto cleanup;
425 ea_bdebug(bh, "b_count=%d, refcount=%d",
426 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400427 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400428 EXT4_ERROR_INODE(inode, "bad block %llu",
429 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430 error = -EIO;
431 goto cleanup;
432 }
Mingming Cao617ba132006-10-11 01:20:53 -0700433 ext4_xattr_cache_insert(bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000434 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435
436cleanup:
437 brelse(bh);
438
439 return error;
440}
441
442static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000443ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000445 struct inode *inode = dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700446 struct ext4_xattr_ibody_header *header;
447 struct ext4_inode *raw_inode;
448 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449 void *end;
450 int error;
451
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500452 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700454 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 if (error)
456 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700457 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700459 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
460 error = ext4_xattr_check_names(IFIRST(header), end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461 if (error)
462 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000463 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464 buffer, buffer_size);
465
466cleanup:
467 brelse(iloc.bh);
468 return error;
469}
470
471/*
Mingming Cao617ba132006-10-11 01:20:53 -0700472 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 *
474 * Copy a list of attribute names into the buffer
475 * provided, or compute the buffer size required.
476 * Buffer is NULL to compute the size of the buffer required.
477 *
478 * Returns a negative error number on failure, or the number of bytes
479 * used / required on success.
480 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400481static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000482ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500484 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485
Christoph Hellwig431547b2009-11-13 09:52:56 +0000486 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500487 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
488 if (ret < 0)
489 goto errout;
490 if (buffer) {
491 buffer += ret;
492 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500494 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
495 if (ret < 0)
496 goto errout;
497 ret += ret2;
498errout:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000499 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500500 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501}
502
503/*
Mingming Cao617ba132006-10-11 01:20:53 -0700504 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 * not set, set it.
506 */
Mingming Cao617ba132006-10-11 01:20:53 -0700507static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 struct super_block *sb)
509{
Mingming Cao617ba132006-10-11 01:20:53 -0700510 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 return;
512
Mingming Cao617ba132006-10-11 01:20:53 -0700513 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Andreas Gruenbachered2908f2006-12-06 20:36:16 -0800514 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400515 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517}
518
519/*
520 * Release the xattr block BH: If the reference count is > 1, decrement
521 * it; otherwise free the block.
522 */
523static void
Mingming Cao617ba132006-10-11 01:20:53 -0700524ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 struct buffer_head *bh)
526{
527 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800528 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529
Mingming Cao617ba132006-10-11 01:20:53 -0700530 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800531 error = ext4_journal_get_write_access(handle, bh);
532 if (error)
533 goto out;
534
535 lock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
537 ea_bdebug(bh, "refcount now=0; freeing");
538 if (ce)
539 mb_cache_entry_free(ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540 get_bh(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500541 ext4_free_blocks(handle, inode, bh, 0, 1,
542 EXT4_FREE_BLOCKS_METADATA |
543 EXT4_FREE_BLOCKS_FORGET);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500544 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 } else {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400546 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500547 if (ce)
548 mb_cache_entry_release(ce);
549 unlock_buffer(bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400550 error = ext4_handle_dirty_xattr_block(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800551 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500552 ext4_handle_sync(handle);
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500553 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800554 ea_bdebug(bh, "refcount now=%d; releasing",
555 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800557out:
558 ext4_std_error(inode->i_sb, error);
559 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560}
561
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400562/*
563 * Find the available free space for EAs. This also returns the total number of
564 * bytes used by EA entries.
565 */
566static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
567 size_t *min_offs, void *base, int *total)
568{
569 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
570 *total += EXT4_XATTR_LEN(last->e_name_len);
571 if (!last->e_value_block && last->e_value_size) {
572 size_t offs = le16_to_cpu(last->e_value_offs);
573 if (offs < *min_offs)
574 *min_offs = offs;
575 }
576 }
577 return (*min_offs - ((void *)last - base) - sizeof(__u32));
578}
579
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580static int
Mingming Cao617ba132006-10-11 01:20:53 -0700581ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582{
Mingming Cao617ba132006-10-11 01:20:53 -0700583 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
585
586 /* Compute min_offs and last. */
587 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700588 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 if (!last->e_value_block && last->e_value_size) {
590 size_t offs = le16_to_cpu(last->e_value_offs);
591 if (offs < min_offs)
592 min_offs = offs;
593 }
594 }
595 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
596 if (!s->not_found) {
597 if (!s->here->e_value_block && s->here->e_value_size) {
598 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700599 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 }
Mingming Cao617ba132006-10-11 01:20:53 -0700601 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700602 }
603 if (i->value) {
Mingming Cao617ba132006-10-11 01:20:53 -0700604 if (free < EXT4_XATTR_SIZE(i->value_len) ||
605 free < EXT4_XATTR_LEN(name_len) +
606 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 return -ENOSPC;
608 }
609
610 if (i->value && s->not_found) {
611 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700612 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
614 memmove((void *)s->here + size, s->here, rest);
615 memset(s->here, 0, size);
616 s->here->e_name_index = i->name_index;
617 s->here->e_name_len = name_len;
618 memcpy(s->here->e_name, i->name, name_len);
619 } else {
620 if (!s->here->e_value_block && s->here->e_value_size) {
621 void *first_val = s->base + min_offs;
622 size_t offs = le16_to_cpu(s->here->e_value_offs);
623 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700624 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 le32_to_cpu(s->here->e_value_size));
626
Mingming Cao617ba132006-10-11 01:20:53 -0700627 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 /* The old and the new value have the same
629 size. Just replace. */
630 s->here->e_value_size =
631 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500632 if (i->value == EXT4_ZERO_XATTR_VALUE) {
633 memset(val, 0, size);
634 } else {
635 /* Clear pad bytes first. */
636 memset(val + size - EXT4_XATTR_PAD, 0,
637 EXT4_XATTR_PAD);
638 memcpy(val, i->value, i->value_len);
639 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 return 0;
641 }
642
643 /* Remove the old value. */
644 memmove(first_val + size, first_val, val - first_val);
645 memset(first_val, 0, size);
646 s->here->e_value_size = 0;
647 s->here->e_value_offs = 0;
648 min_offs += size;
649
650 /* Adjust all value offsets. */
651 last = s->first;
652 while (!IS_LAST_ENTRY(last)) {
653 size_t o = le16_to_cpu(last->e_value_offs);
654 if (!last->e_value_block &&
655 last->e_value_size && o < offs)
656 last->e_value_offs =
657 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700658 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 }
660 }
661 if (!i->value) {
662 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700663 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 last = ENTRY((void *)last - size);
665 memmove(s->here, (void *)s->here + size,
666 (void *)last - (void *)s->here + sizeof(__u32));
667 memset(last, 0, size);
668 }
669 }
670
671 if (i->value) {
672 /* Insert the new value. */
673 s->here->e_value_size = cpu_to_le32(i->value_len);
674 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700675 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 void *val = s->base + min_offs - size;
677 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500678 if (i->value == EXT4_ZERO_XATTR_VALUE) {
679 memset(val, 0, size);
680 } else {
681 /* Clear the pad bytes first. */
682 memset(val + size - EXT4_XATTR_PAD, 0,
683 EXT4_XATTR_PAD);
684 memcpy(val, i->value, i->value_len);
685 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 }
687 }
688 return 0;
689}
690
Mingming Cao617ba132006-10-11 01:20:53 -0700691struct ext4_xattr_block_find {
692 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 struct buffer_head *bh;
694};
695
696static int
Mingming Cao617ba132006-10-11 01:20:53 -0700697ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
698 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699{
700 struct super_block *sb = inode->i_sb;
701 int error;
702
703 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
704 i->name_index, i->name, i->value, (long)i->value_len);
705
Mingming Cao617ba132006-10-11 01:20:53 -0700706 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700708 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 error = -EIO;
710 if (!bs->bh)
711 goto cleanup;
712 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
713 atomic_read(&(bs->bh->b_count)),
714 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400715 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400716 EXT4_ERROR_INODE(inode, "bad block %llu",
717 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 error = -EIO;
719 goto cleanup;
720 }
721 /* Find the named attribute. */
722 bs->s.base = BHDR(bs->bh);
723 bs->s.first = BFIRST(bs->bh);
724 bs->s.end = bs->bh->b_data + bs->bh->b_size;
725 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700726 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 i->name, bs->bh->b_size, 1);
728 if (error && error != -ENODATA)
729 goto cleanup;
730 bs->s.not_found = error;
731 }
732 error = 0;
733
734cleanup:
735 return error;
736}
737
738static int
Mingming Cao617ba132006-10-11 01:20:53 -0700739ext4_xattr_block_set(handle_t *handle, struct inode *inode,
740 struct ext4_xattr_info *i,
741 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742{
743 struct super_block *sb = inode->i_sb;
744 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700745 struct ext4_xattr_search *s = &bs->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800747 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748
Mingming Cao617ba132006-10-11 01:20:53 -0700749#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750
751 if (i->value && i->value_len > sb->s_blocksize)
752 return -ENOSPC;
753 if (s->base) {
Mingming Cao617ba132006-10-11 01:20:53 -0700754 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755 bs->bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800756 error = ext4_journal_get_write_access(handle, bs->bh);
757 if (error)
758 goto cleanup;
759 lock_buffer(bs->bh);
760
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
762 if (ce) {
763 mb_cache_entry_free(ce);
764 ce = NULL;
765 }
766 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700767 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 if (!error) {
769 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700770 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 s->here);
Mingming Cao617ba132006-10-11 01:20:53 -0700772 ext4_xattr_cache_insert(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773 }
774 unlock_buffer(bs->bh);
775 if (error == -EIO)
776 goto bad_block;
777 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400778 error = ext4_handle_dirty_xattr_block(handle,
779 inode,
780 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 if (error)
782 goto cleanup;
783 goto inserted;
784 } else {
785 int offset = (char *)s->here - bs->bh->b_data;
786
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800787 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 if (ce) {
789 mb_cache_entry_release(ce);
790 ce = NULL;
791 }
792 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400793 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700794 error = -ENOMEM;
795 if (s->base == NULL)
796 goto cleanup;
797 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
798 s->first = ENTRY(header(s->base)+1);
799 header(s->base)->h_refcount = cpu_to_le32(1);
800 s->here = ENTRY(s->base + offset);
801 s->end = s->base + bs->bh->b_size;
802 }
803 } else {
804 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400805 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 /* assert(header == s->base) */
807 error = -ENOMEM;
808 if (s->base == NULL)
809 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700810 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 header(s->base)->h_blocks = cpu_to_le32(1);
812 header(s->base)->h_refcount = cpu_to_le32(1);
813 s->first = ENTRY(header(s->base)+1);
814 s->here = ENTRY(header(s->base)+1);
815 s->end = s->base + sb->s_blocksize;
816 }
817
Mingming Cao617ba132006-10-11 01:20:53 -0700818 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 if (error == -EIO)
820 goto bad_block;
821 if (error)
822 goto cleanup;
823 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700824 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825
826inserted:
827 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700828 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829 if (new_bh) {
830 /* We found an identical block in the cache. */
831 if (new_bh == bs->bh)
832 ea_bdebug(new_bh, "keeping");
833 else {
834 /* The old block is released after updating
835 the inode. */
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500836 error = dquot_alloc_block(inode,
837 EXT4_C2B(EXT4_SB(sb), 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500838 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700840 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 new_bh);
842 if (error)
843 goto cleanup_dquot;
844 lock_buffer(new_bh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400845 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 ea_bdebug(new_bh, "reusing; refcount now=%d",
847 le32_to_cpu(BHDR(new_bh)->h_refcount));
848 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400849 error = ext4_handle_dirty_xattr_block(handle,
850 inode,
851 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852 if (error)
853 goto cleanup_dquot;
854 }
855 mb_cache_entry_release(ce);
856 ce = NULL;
857 } else if (bs->bh && s->base == bs->bh->b_data) {
858 /* We were modifying this block in-place. */
859 ea_bdebug(bs->bh, "keeping this block");
860 new_bh = bs->bh;
861 get_bh(new_bh);
862 } else {
863 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400864 ext4_fsblk_t goal, block;
865
866 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400867 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400868
869 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400870 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400871 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
872
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400873 /*
874 * take i_data_sem because we will test
875 * i_delalloc_reserved_flag in ext4_mb_new_blocks
876 */
877 down_read((&EXT4_I(inode)->i_data_sem));
Allison Henderson55f020d2011-05-25 07:41:26 -0400878 block = ext4_new_meta_blocks(handle, inode, goal, 0,
879 NULL, &error);
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400880 up_read((&EXT4_I(inode)->i_data_sem));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 if (error)
882 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400883
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400884 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400885 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
886
Joe Perchesace36ad2012-03-19 23:11:43 -0400887 ea_idebug(inode, "creating block %llu",
888 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889
890 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500891 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500892 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500894 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500895 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896 goto cleanup;
897 }
898 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700899 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900 if (error) {
901 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500902 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700903 goto getblk_failed;
904 }
905 memcpy(new_bh->b_data, s->base, new_bh->b_size);
906 set_buffer_uptodate(new_bh);
907 unlock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700908 ext4_xattr_cache_insert(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400909 error = ext4_handle_dirty_xattr_block(handle,
910 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 if (error)
912 goto cleanup;
913 }
914 }
915
916 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -0700917 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918
919 /* Drop the previous xattr block. */
920 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700921 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 error = 0;
923
924cleanup:
925 if (ce)
926 mb_cache_entry_release(ce);
927 brelse(new_bh);
928 if (!(bs->bh && s->base == bs->bh->b_data))
929 kfree(s->base);
930
931 return error;
932
933cleanup_dquot:
Lukas Czerner1231b3a2013-02-18 12:12:07 -0500934 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 goto cleanup;
936
937bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400938 EXT4_ERROR_INODE(inode, "bad block %llu",
939 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940 goto cleanup;
941
942#undef header
943}
944
Tao Ma879b3822012-12-05 10:28:46 -0500945int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
946 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947{
Mingming Cao617ba132006-10-11 01:20:53 -0700948 struct ext4_xattr_ibody_header *header;
949 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 int error;
951
Mingming Cao617ba132006-10-11 01:20:53 -0700952 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700954 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955 header = IHDR(inode, raw_inode);
956 is->s.base = is->s.first = IFIRST(header);
957 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700958 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500959 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700960 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 if (error)
962 return error;
963 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -0700964 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 i->name, is->s.end -
966 (void *)is->s.base, 0);
967 if (error && error != -ENODATA)
968 return error;
969 is->s.not_found = error;
970 }
971 return 0;
972}
973
Tao Ma0d812f72012-12-10 14:06:02 -0500974int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
975 struct ext4_xattr_info *i,
976 struct ext4_xattr_ibody_find *is)
977{
978 struct ext4_xattr_ibody_header *header;
979 struct ext4_xattr_search *s = &is->s;
980 int error;
981
982 if (EXT4_I(inode)->i_extra_isize == 0)
983 return -ENOSPC;
984 error = ext4_xattr_set_entry(i, s);
985 if (error) {
986 if (error == -ENOSPC &&
987 ext4_has_inline_data(inode)) {
988 error = ext4_try_to_evict_inline_data(handle, inode,
989 EXT4_XATTR_LEN(strlen(i->name) +
990 EXT4_XATTR_SIZE(i->value_len)));
991 if (error)
992 return error;
993 error = ext4_xattr_ibody_find(inode, i, is);
994 if (error)
995 return error;
996 error = ext4_xattr_set_entry(i, s);
997 }
998 if (error)
999 return error;
1000 }
1001 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1002 if (!IS_LAST_ENTRY(s->first)) {
1003 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1004 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1005 } else {
1006 header->h_magic = cpu_to_le32(0);
1007 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1008 }
1009 return 0;
1010}
1011
1012static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1013 struct ext4_xattr_info *i,
1014 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015{
Mingming Cao617ba132006-10-11 01:20:53 -07001016 struct ext4_xattr_ibody_header *header;
1017 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 int error;
1019
Mingming Cao617ba132006-10-11 01:20:53 -07001020 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001022 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001023 if (error)
1024 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001025 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001027 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001028 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029 } else {
1030 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001031 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032 }
1033 return 0;
1034}
1035
1036/*
Mingming Cao617ba132006-10-11 01:20:53 -07001037 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001039 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 * is NULL to remove an existing extended attribute, and non-NULL to
1041 * either replace an existing extended attribute, or create a new extended
1042 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1043 * specify that an extended attribute must exist and must not exist
1044 * previous to the call, respectively.
1045 *
1046 * Returns 0, or a negative error number on failure.
1047 */
1048int
Mingming Cao617ba132006-10-11 01:20:53 -07001049ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 const char *name, const void *value, size_t value_len,
1051 int flags)
1052{
Mingming Cao617ba132006-10-11 01:20:53 -07001053 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 .name_index = name_index,
1055 .name = name,
1056 .value = value,
1057 .value_len = value_len,
1058
1059 };
Mingming Cao617ba132006-10-11 01:20:53 -07001060 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 .s = { .not_found = -ENODATA, },
1062 };
Mingming Cao617ba132006-10-11 01:20:53 -07001063 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 .s = { .not_found = -ENODATA, },
1065 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001066 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 int error;
1068
1069 if (!name)
1070 return -EINVAL;
1071 if (strlen(name) > 255)
1072 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001073 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001074 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1075 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001076
Eric Sandeen66543612011-10-26 03:32:07 -04001077 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001078 if (error)
1079 goto cleanup;
1080
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001081 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001082 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1083 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001084 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085 }
1086
Mingming Cao617ba132006-10-11 01:20:53 -07001087 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001088 if (error)
1089 goto cleanup;
1090 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001091 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001092 if (error)
1093 goto cleanup;
1094 if (is.s.not_found && bs.s.not_found) {
1095 error = -ENODATA;
1096 if (flags & XATTR_REPLACE)
1097 goto cleanup;
1098 error = 0;
1099 if (!value)
1100 goto cleanup;
1101 } else {
1102 error = -EEXIST;
1103 if (flags & XATTR_CREATE)
1104 goto cleanup;
1105 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106 if (!value) {
1107 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001108 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001110 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001112 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113 if (!error && !bs.s.not_found) {
1114 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001115 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001117 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1118 error = ext4_xattr_block_find(inode, &i, &bs);
1119 if (error)
1120 goto cleanup;
1121 }
Mingming Cao617ba132006-10-11 01:20:53 -07001122 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 if (error)
1124 goto cleanup;
1125 if (!is.s.not_found) {
1126 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001127 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 &is);
1129 }
1130 }
1131 }
1132 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001133 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001134 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001135 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001136 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001137 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001139 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 * error != 0.
1141 */
1142 is.iloc.bh = NULL;
1143 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001144 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145 }
1146
1147cleanup:
1148 brelse(is.iloc.bh);
1149 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001150 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001151 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001152 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153 return error;
1154}
1155
1156/*
Mingming Cao617ba132006-10-11 01:20:53 -07001157 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 *
Mingming Cao617ba132006-10-11 01:20:53 -07001159 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 * attribute modification is a filesystem transaction by itself.
1161 *
1162 * Returns 0, or a negative error number on failure.
1163 */
1164int
Mingming Cao617ba132006-10-11 01:20:53 -07001165ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 const void *value, size_t value_len, int flags)
1167{
1168 handle_t *handle;
1169 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001170 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171
1172retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001173 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 if (IS_ERR(handle)) {
1175 error = PTR_ERR(handle);
1176 } else {
1177 int error2;
1178
Mingming Cao617ba132006-10-11 01:20:53 -07001179 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001181 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001182 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001183 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001184 goto retry;
1185 if (error == 0)
1186 error = error2;
1187 }
1188
1189 return error;
1190}
1191
1192/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001193 * Shift the EA entries in the inode to create space for the increased
1194 * i_extra_isize.
1195 */
1196static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1197 int value_offs_shift, void *to,
1198 void *from, size_t n, int blocksize)
1199{
1200 struct ext4_xattr_entry *last = entry;
1201 int new_offs;
1202
1203 /* Adjust the value offsets of the entries */
1204 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1205 if (!last->e_value_block && last->e_value_size) {
1206 new_offs = le16_to_cpu(last->e_value_offs) +
1207 value_offs_shift;
1208 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1209 > blocksize);
1210 last->e_value_offs = cpu_to_le16(new_offs);
1211 }
1212 }
1213 /* Shift the entries by n bytes */
1214 memmove(to, from, n);
1215}
1216
1217/*
1218 * Expand an inode by new_extra_isize bytes when EAs are present.
1219 * Returns 0 on success or negative error number on failure.
1220 */
1221int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1222 struct ext4_inode *raw_inode, handle_t *handle)
1223{
1224 struct ext4_xattr_ibody_header *header;
1225 struct ext4_xattr_entry *entry, *last, *first;
1226 struct buffer_head *bh = NULL;
1227 struct ext4_xattr_ibody_find *is = NULL;
1228 struct ext4_xattr_block_find *bs = NULL;
1229 char *buffer = NULL, *b_entry_name = NULL;
1230 size_t min_offs, free;
1231 int total_ino, total_blk;
1232 void *base, *start, *end;
1233 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001234 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 -04001235
1236 down_write(&EXT4_I(inode)->xattr_sem);
1237retry:
1238 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1239 up_write(&EXT4_I(inode)->xattr_sem);
1240 return 0;
1241 }
1242
1243 header = IHDR(inode, raw_inode);
1244 entry = IFIRST(header);
1245
1246 /*
1247 * Check if enough free space is available in the inode to shift the
1248 * entries ahead by new_extra_isize.
1249 */
1250
1251 base = start = entry;
1252 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1253 min_offs = end - base;
1254 last = entry;
1255 total_ino = sizeof(struct ext4_xattr_ibody_header);
1256
1257 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1258 if (free >= new_extra_isize) {
1259 entry = IFIRST(header);
1260 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1261 - new_extra_isize, (void *)raw_inode +
1262 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1263 (void *)header, total_ino,
1264 inode->i_sb->s_blocksize);
1265 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1266 error = 0;
1267 goto cleanup;
1268 }
1269
1270 /*
1271 * Enough free space isn't available in the inode, check if
1272 * EA block can hold new_extra_isize bytes.
1273 */
1274 if (EXT4_I(inode)->i_file_acl) {
1275 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1276 error = -EIO;
1277 if (!bh)
1278 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001279 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001280 EXT4_ERROR_INODE(inode, "bad block %llu",
1281 EXT4_I(inode)->i_file_acl);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001282 error = -EIO;
1283 goto cleanup;
1284 }
1285 base = BHDR(bh);
1286 first = BFIRST(bh);
1287 end = bh->b_data + bh->b_size;
1288 min_offs = end - base;
1289 free = ext4_xattr_free_space(first, &min_offs, base,
1290 &total_blk);
1291 if (free < new_extra_isize) {
1292 if (!tried_min_extra_isize && s_min_extra_isize) {
1293 tried_min_extra_isize++;
1294 new_extra_isize = s_min_extra_isize;
1295 brelse(bh);
1296 goto retry;
1297 }
1298 error = -1;
1299 goto cleanup;
1300 }
1301 } else {
1302 free = inode->i_sb->s_blocksize;
1303 }
1304
1305 while (new_extra_isize > 0) {
1306 size_t offs, size, entry_size;
1307 struct ext4_xattr_entry *small_entry = NULL;
1308 struct ext4_xattr_info i = {
1309 .value = NULL,
1310 .value_len = 0,
1311 };
1312 unsigned int total_size; /* EA entry size + value size */
1313 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1314 unsigned int min_total_size = ~0U;
1315
1316 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1317 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1318 if (!is || !bs) {
1319 error = -ENOMEM;
1320 goto cleanup;
1321 }
1322
1323 is->s.not_found = -ENODATA;
1324 bs->s.not_found = -ENODATA;
1325 is->iloc.bh = NULL;
1326 bs->bh = NULL;
1327
1328 last = IFIRST(header);
1329 /* Find the entry best suited to be pushed into EA block */
1330 entry = NULL;
1331 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1332 total_size =
1333 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1334 EXT4_XATTR_LEN(last->e_name_len);
1335 if (total_size <= free && total_size < min_total_size) {
1336 if (total_size < new_extra_isize) {
1337 small_entry = last;
1338 } else {
1339 entry = last;
1340 min_total_size = total_size;
1341 }
1342 }
1343 }
1344
1345 if (entry == NULL) {
1346 if (small_entry) {
1347 entry = small_entry;
1348 } else {
1349 if (!tried_min_extra_isize &&
1350 s_min_extra_isize) {
1351 tried_min_extra_isize++;
1352 new_extra_isize = s_min_extra_isize;
Dave Jones6e4ea8e2013-10-10 20:05:35 -04001353 kfree(is); is = NULL;
1354 kfree(bs); bs = NULL;
Theodore Ts'odcb99172013-10-31 23:00:24 -04001355 brelse(bh);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001356 goto retry;
1357 }
1358 error = -1;
1359 goto cleanup;
1360 }
1361 }
1362 offs = le16_to_cpu(entry->e_value_offs);
1363 size = le32_to_cpu(entry->e_value_size);
1364 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1365 i.name_index = entry->e_name_index,
1366 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1367 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1368 if (!buffer || !b_entry_name) {
1369 error = -ENOMEM;
1370 goto cleanup;
1371 }
1372 /* Save the entry name and the entry value */
1373 memcpy(buffer, (void *)IFIRST(header) + offs,
1374 EXT4_XATTR_SIZE(size));
1375 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1376 b_entry_name[entry->e_name_len] = '\0';
1377 i.name = b_entry_name;
1378
1379 error = ext4_get_inode_loc(inode, &is->iloc);
1380 if (error)
1381 goto cleanup;
1382
1383 error = ext4_xattr_ibody_find(inode, &i, is);
1384 if (error)
1385 goto cleanup;
1386
1387 /* Remove the chosen entry from the inode */
1388 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001389 if (error)
1390 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001391
1392 entry = IFIRST(header);
1393 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1394 shift_bytes = new_extra_isize;
1395 else
1396 shift_bytes = entry_size + size;
1397 /* Adjust the offsets and shift the remaining entries ahead */
1398 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1399 shift_bytes, (void *)raw_inode +
1400 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1401 (void *)header, total_ino - entry_size,
1402 inode->i_sb->s_blocksize);
1403
1404 extra_isize += shift_bytes;
1405 new_extra_isize -= shift_bytes;
1406 EXT4_I(inode)->i_extra_isize = extra_isize;
1407
1408 i.name = b_entry_name;
1409 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001410 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001411 error = ext4_xattr_block_find(inode, &i, bs);
1412 if (error)
1413 goto cleanup;
1414
1415 /* Add entry which was removed from the inode into the block */
1416 error = ext4_xattr_block_set(handle, inode, &i, bs);
1417 if (error)
1418 goto cleanup;
1419 kfree(b_entry_name);
1420 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001421 b_entry_name = NULL;
1422 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001423 brelse(is->iloc.bh);
1424 kfree(is);
1425 kfree(bs);
1426 }
1427 brelse(bh);
1428 up_write(&EXT4_I(inode)->xattr_sem);
1429 return 0;
1430
1431cleanup:
1432 kfree(b_entry_name);
1433 kfree(buffer);
1434 if (is)
1435 brelse(is->iloc.bh);
1436 kfree(is);
1437 kfree(bs);
1438 brelse(bh);
1439 up_write(&EXT4_I(inode)->xattr_sem);
1440 return error;
1441}
1442
1443
1444
1445/*
Mingming Cao617ba132006-10-11 01:20:53 -07001446 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447 *
1448 * Free extended attribute resources associated with this inode. This
1449 * is called immediately before an inode is freed. We have exclusive
1450 * access to the inode.
1451 */
1452void
Mingming Cao617ba132006-10-11 01:20:53 -07001453ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454{
1455 struct buffer_head *bh = NULL;
1456
Mingming Cao617ba132006-10-11 01:20:53 -07001457 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001459 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001460 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001461 EXT4_ERROR_INODE(inode, "block %llu read error",
1462 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001463 goto cleanup;
1464 }
Mingming Cao617ba132006-10-11 01:20:53 -07001465 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001467 EXT4_ERROR_INODE(inode, "bad block %llu",
1468 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 goto cleanup;
1470 }
Mingming Cao617ba132006-10-11 01:20:53 -07001471 ext4_xattr_release_block(handle, inode, bh);
1472 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473
1474cleanup:
1475 brelse(bh);
1476}
1477
1478/*
Mingming Cao617ba132006-10-11 01:20:53 -07001479 * ext4_xattr_put_super()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 *
1481 * This is called when a file system is unmounted.
1482 */
1483void
Mingming Cao617ba132006-10-11 01:20:53 -07001484ext4_xattr_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485{
1486 mb_cache_shrink(sb->s_bdev);
1487}
1488
1489/*
Mingming Cao617ba132006-10-11 01:20:53 -07001490 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001491 *
1492 * Create a new entry in the extended attribute cache, and insert
1493 * it unless such an entry is already in the cache.
1494 *
1495 * Returns 0, or a negative error number on failure.
1496 */
1497static void
Mingming Cao617ba132006-10-11 01:20:53 -07001498ext4_xattr_cache_insert(struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001499{
1500 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1501 struct mb_cache_entry *ce;
1502 int error;
1503
Jan Kara335e92e2008-04-15 14:34:43 -07001504 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 if (!ce) {
1506 ea_bdebug(bh, "out of memory");
1507 return;
1508 }
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001509 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 if (error) {
1511 mb_cache_entry_free(ce);
1512 if (error == -EBUSY) {
1513 ea_bdebug(bh, "already in cache");
1514 error = 0;
1515 }
1516 } else {
1517 ea_bdebug(bh, "inserting [%x]", (int)hash);
1518 mb_cache_entry_release(ce);
1519 }
1520}
1521
1522/*
Mingming Cao617ba132006-10-11 01:20:53 -07001523 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524 *
1525 * Compare two extended attribute blocks for equality.
1526 *
1527 * Returns 0 if the blocks are equal, 1 if they differ, and
1528 * a negative error number on errors.
1529 */
1530static int
Mingming Cao617ba132006-10-11 01:20:53 -07001531ext4_xattr_cmp(struct ext4_xattr_header *header1,
1532 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001533{
Mingming Cao617ba132006-10-11 01:20:53 -07001534 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535
1536 entry1 = ENTRY(header1+1);
1537 entry2 = ENTRY(header2+1);
1538 while (!IS_LAST_ENTRY(entry1)) {
1539 if (IS_LAST_ENTRY(entry2))
1540 return 1;
1541 if (entry1->e_hash != entry2->e_hash ||
1542 entry1->e_name_index != entry2->e_name_index ||
1543 entry1->e_name_len != entry2->e_name_len ||
1544 entry1->e_value_size != entry2->e_value_size ||
1545 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1546 return 1;
1547 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1548 return -EIO;
1549 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1550 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1551 le32_to_cpu(entry1->e_value_size)))
1552 return 1;
1553
Mingming Cao617ba132006-10-11 01:20:53 -07001554 entry1 = EXT4_XATTR_NEXT(entry1);
1555 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556 }
1557 if (!IS_LAST_ENTRY(entry2))
1558 return 1;
1559 return 0;
1560}
1561
1562/*
Mingming Cao617ba132006-10-11 01:20:53 -07001563 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564 *
1565 * Find an identical extended attribute block.
1566 *
1567 * Returns a pointer to the block found, or NULL if such a block was
1568 * not found or an error occurred.
1569 */
1570static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001571ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572 struct mb_cache_entry **pce)
1573{
1574 __u32 hash = le32_to_cpu(header->h_hash);
1575 struct mb_cache_entry *ce;
1576
1577 if (!header->h_hash)
1578 return NULL; /* never share */
1579 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1580again:
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001581 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1582 hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 while (ce) {
1584 struct buffer_head *bh;
1585
1586 if (IS_ERR(ce)) {
1587 if (PTR_ERR(ce) == -EAGAIN)
1588 goto again;
1589 break;
1590 }
1591 bh = sb_bread(inode->i_sb, ce->e_block);
1592 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001593 EXT4_ERROR_INODE(inode, "block %lu read error",
1594 (unsigned long) ce->e_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
Mingming Cao617ba132006-10-11 01:20:53 -07001596 EXT4_XATTR_REFCOUNT_MAX) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597 ea_idebug(inode, "block %lu refcount %d>=%d",
1598 (unsigned long) ce->e_block,
1599 le32_to_cpu(BHDR(bh)->h_refcount),
Mingming Cao617ba132006-10-11 01:20:53 -07001600 EXT4_XATTR_REFCOUNT_MAX);
1601 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 *pce = ce;
1603 return bh;
1604 }
1605 brelse(bh);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001606 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 }
1608 return NULL;
1609}
1610
1611#define NAME_HASH_SHIFT 5
1612#define VALUE_HASH_SHIFT 16
1613
1614/*
Mingming Cao617ba132006-10-11 01:20:53 -07001615 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616 *
1617 * Compute the hash of an extended attribute.
1618 */
Mingming Cao617ba132006-10-11 01:20:53 -07001619static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1620 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621{
1622 __u32 hash = 0;
1623 char *name = entry->e_name;
1624 int n;
1625
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001626 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 hash = (hash << NAME_HASH_SHIFT) ^
1628 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1629 *name++;
1630 }
1631
1632 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1633 __le32 *value = (__le32 *)((char *)header +
1634 le16_to_cpu(entry->e_value_offs));
1635 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001636 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001637 hash = (hash << VALUE_HASH_SHIFT) ^
1638 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1639 le32_to_cpu(*value++);
1640 }
1641 }
1642 entry->e_hash = cpu_to_le32(hash);
1643}
1644
1645#undef NAME_HASH_SHIFT
1646#undef VALUE_HASH_SHIFT
1647
1648#define BLOCK_HASH_SHIFT 16
1649
1650/*
Mingming Cao617ba132006-10-11 01:20:53 -07001651 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 *
1653 * Re-compute the extended attribute hash value after an entry has changed.
1654 */
Mingming Cao617ba132006-10-11 01:20:53 -07001655static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1656 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657{
Mingming Cao617ba132006-10-11 01:20:53 -07001658 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 __u32 hash = 0;
1660
Mingming Cao617ba132006-10-11 01:20:53 -07001661 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 here = ENTRY(header+1);
1663 while (!IS_LAST_ENTRY(here)) {
1664 if (!here->e_hash) {
1665 /* Block is not shared if an entry's hash value == 0 */
1666 hash = 0;
1667 break;
1668 }
1669 hash = (hash << BLOCK_HASH_SHIFT) ^
1670 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1671 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001672 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 }
1674 header->h_hash = cpu_to_le32(hash);
1675}
1676
1677#undef BLOCK_HASH_SHIFT
1678
1679int __init
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04001680ext4_init_xattr(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681{
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001682 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
Mingming Cao617ba132006-10-11 01:20:53 -07001683 if (!ext4_xattr_cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 return -ENOMEM;
1685 return 0;
1686}
1687
1688void
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04001689ext4_exit_xattr(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690{
Mingming Cao617ba132006-10-11 01:20:53 -07001691 if (ext4_xattr_cache)
1692 mb_cache_destroy(ext4_xattr_cache);
1693 ext4_xattr_cache = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694}