blob: cc31da02759675501245c8a14e957834c94f8fd5 [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
Mingming Cao617ba132006-10-11 01:20:53 -070098 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
99 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_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
Mingming Cao617ba132006-10-11 01:20:53 -0700111 &ext4_xattr_acl_access_handler,
112 &ext4_xattr_acl_default_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);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400125 __u32 csum, old;
126
127 old = hdr->h_checksum;
128 hdr->h_checksum = 0;
Tao Ma41eb70d2012-07-09 16:29:27 -0400129 block_nr = cpu_to_le64(block_nr);
130 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&block_nr,
131 sizeof(block_nr));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400132 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
133 EXT4_BLOCK_SIZE(inode->i_sb));
Tao Ma41eb70d2012-07-09 16:29:27 -0400134
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400135 hdr->h_checksum = old;
136 return cpu_to_le32(csum);
137}
138
139static int ext4_xattr_block_csum_verify(struct inode *inode,
140 sector_t block_nr,
141 struct ext4_xattr_header *hdr)
142{
143 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
144 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
145 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
146 return 0;
147 return 1;
148}
149
150static void ext4_xattr_block_csum_set(struct inode *inode,
151 sector_t block_nr,
152 struct ext4_xattr_header *hdr)
153{
154 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
155 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
156 return;
157
158 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
159}
160
161static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
162 struct inode *inode,
163 struct buffer_head *bh)
164{
165 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
166 return ext4_handle_dirty_metadata(handle, inode, bh);
167}
168
Stephen Hemminger11e27522010-05-13 17:53:18 -0700169static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700170ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700172 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173
Mingming Cao617ba132006-10-11 01:20:53 -0700174 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
175 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 return handler;
177}
178
179/*
180 * Inode operation listxattr()
181 *
182 * dentry->d_inode->i_mutex: don't care
183 */
184ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700185ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000187 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188}
189
190static int
Mingming Cao617ba132006-10-11 01:20:53 -0700191ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192{
193 while (!IS_LAST_ENTRY(entry)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700194 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195 if ((void *)next >= end)
196 return -EIO;
197 entry = next;
198 }
199 return 0;
200}
201
202static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400203ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400205 int error;
206
207 if (buffer_verified(bh))
208 return 0;
209
Mingming Cao617ba132006-10-11 01:20:53 -0700210 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211 BHDR(bh)->h_blocks != cpu_to_le32(1))
212 return -EIO;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400213 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
214 return -EIO;
215 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
216 if (!error)
217 set_buffer_verified(bh);
218 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219}
220
221static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700222ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223{
224 size_t value_size = le32_to_cpu(entry->e_value_size);
225
226 if (entry->e_value_block != 0 || value_size > size ||
227 le16_to_cpu(entry->e_value_offs) + value_size > size)
228 return -EIO;
229 return 0;
230}
231
232static int
Mingming Cao617ba132006-10-11 01:20:53 -0700233ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700234 const char *name, size_t size, int sorted)
235{
Mingming Cao617ba132006-10-11 01:20:53 -0700236 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 size_t name_len;
238 int cmp = 1;
239
240 if (name == NULL)
241 return -EINVAL;
242 name_len = strlen(name);
243 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700244 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 cmp = name_index - entry->e_name_index;
246 if (!cmp)
247 cmp = name_len - entry->e_name_len;
248 if (!cmp)
249 cmp = memcmp(name, entry->e_name, name_len);
250 if (cmp <= 0 && (sorted || cmp == 0))
251 break;
252 }
253 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700254 if (!cmp && ext4_xattr_check_entry(entry, size))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255 return -EIO;
256 return cmp ? -ENODATA : 0;
257}
258
259static int
Mingming Cao617ba132006-10-11 01:20:53 -0700260ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 void *buffer, size_t buffer_size)
262{
263 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700264 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265 size_t size;
266 int error;
267
268 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
269 name_index, name, buffer, (long)buffer_size);
270
271 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700272 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400274 ea_idebug(inode, "reading block %llu",
275 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700276 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 if (!bh)
278 goto cleanup;
279 ea_bdebug(bh, "b_count=%d, refcount=%d",
280 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400281 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500282bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400283 EXT4_ERROR_INODE(inode, "bad block %llu",
284 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 error = -EIO;
286 goto cleanup;
287 }
Mingming Cao617ba132006-10-11 01:20:53 -0700288 ext4_xattr_cache_insert(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700290 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 if (error == -EIO)
292 goto bad_block;
293 if (error)
294 goto cleanup;
295 size = le32_to_cpu(entry->e_value_size);
296 if (buffer) {
297 error = -ERANGE;
298 if (size > buffer_size)
299 goto cleanup;
300 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
301 size);
302 }
303 error = size;
304
305cleanup:
306 brelse(bh);
307 return error;
308}
309
Tao Ma879b3822012-12-05 10:28:46 -0500310int
Mingming Cao617ba132006-10-11 01:20:53 -0700311ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 void *buffer, size_t buffer_size)
313{
Mingming Cao617ba132006-10-11 01:20:53 -0700314 struct ext4_xattr_ibody_header *header;
315 struct ext4_xattr_entry *entry;
316 struct ext4_inode *raw_inode;
317 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 size_t size;
319 void *end;
320 int error;
321
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500322 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700324 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700325 if (error)
326 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700327 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 header = IHDR(inode, raw_inode);
329 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700330 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
331 error = ext4_xattr_check_names(entry, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332 if (error)
333 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700334 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335 end - (void *)entry, 0);
336 if (error)
337 goto cleanup;
338 size = le32_to_cpu(entry->e_value_size);
339 if (buffer) {
340 error = -ERANGE;
341 if (size > buffer_size)
342 goto cleanup;
343 memcpy(buffer, (void *)IFIRST(header) +
344 le16_to_cpu(entry->e_value_offs), size);
345 }
346 error = size;
347
348cleanup:
349 brelse(iloc.bh);
350 return error;
351}
352
353/*
Mingming Cao617ba132006-10-11 01:20:53 -0700354 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 *
356 * Copy an extended attribute into the buffer
357 * provided, or compute the buffer size required.
358 * Buffer is NULL to compute the size of the buffer required.
359 *
360 * Returns a negative error number on failure, or the number of bytes
361 * used / required on success.
362 */
363int
Mingming Cao617ba132006-10-11 01:20:53 -0700364ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 void *buffer, size_t buffer_size)
366{
367 int error;
368
Mingming Cao617ba132006-10-11 01:20:53 -0700369 down_read(&EXT4_I(inode)->xattr_sem);
370 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371 buffer_size);
372 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700373 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700374 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700375 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700376 return error;
377}
378
379static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000380ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700381 char *buffer, size_t buffer_size)
382{
383 size_t rest = buffer_size;
384
Mingming Cao617ba132006-10-11 01:20:53 -0700385 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700386 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700387 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388
389 if (handler) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000390 size_t size = handler->list(dentry, buffer, rest,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700391 entry->e_name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000392 entry->e_name_len,
393 handler->flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700394 if (buffer) {
395 if (size > rest)
396 return -ERANGE;
397 buffer += size;
398 }
399 rest -= size;
400 }
401 }
402 return buffer_size - rest;
403}
404
405static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000406ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000408 struct inode *inode = dentry->d_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 struct buffer_head *bh = NULL;
410 int error;
411
412 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
413 buffer, (long)buffer_size);
414
415 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700416 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400418 ea_idebug(inode, "reading block %llu",
419 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700420 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421 error = -EIO;
422 if (!bh)
423 goto cleanup;
424 ea_bdebug(bh, "b_count=%d, refcount=%d",
425 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400426 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400427 EXT4_ERROR_INODE(inode, "bad block %llu",
428 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429 error = -EIO;
430 goto cleanup;
431 }
Mingming Cao617ba132006-10-11 01:20:53 -0700432 ext4_xattr_cache_insert(bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000433 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434
435cleanup:
436 brelse(bh);
437
438 return error;
439}
440
441static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000442ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000444 struct inode *inode = dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700445 struct ext4_xattr_ibody_header *header;
446 struct ext4_inode *raw_inode;
447 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700448 void *end;
449 int error;
450
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500451 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700452 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700453 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454 if (error)
455 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700456 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700457 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700458 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
459 error = ext4_xattr_check_names(IFIRST(header), end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 if (error)
461 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000462 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463 buffer, buffer_size);
464
465cleanup:
466 brelse(iloc.bh);
467 return error;
468}
469
470/*
Mingming Cao617ba132006-10-11 01:20:53 -0700471 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700472 *
473 * Copy a list of attribute names into the buffer
474 * provided, or compute the buffer size required.
475 * Buffer is NULL to compute the size of the buffer required.
476 *
477 * Returns a negative error number on failure, or the number of bytes
478 * used / required on success.
479 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400480static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000481ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500483 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484
Christoph Hellwig431547b2009-11-13 09:52:56 +0000485 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500486 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
487 if (ret < 0)
488 goto errout;
489 if (buffer) {
490 buffer += ret;
491 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500493 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
494 if (ret < 0)
495 goto errout;
496 ret += ret2;
497errout:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000498 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500499 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500}
501
502/*
Mingming Cao617ba132006-10-11 01:20:53 -0700503 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 * not set, set it.
505 */
Mingming Cao617ba132006-10-11 01:20:53 -0700506static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 struct super_block *sb)
508{
Mingming Cao617ba132006-10-11 01:20:53 -0700509 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 return;
511
Mingming Cao617ba132006-10-11 01:20:53 -0700512 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Andreas Gruenbachered2908f2006-12-06 20:36:16 -0800513 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400514 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516}
517
518/*
519 * Release the xattr block BH: If the reference count is > 1, decrement
520 * it; otherwise free the block.
521 */
522static void
Mingming Cao617ba132006-10-11 01:20:53 -0700523ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 struct buffer_head *bh)
525{
526 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800527 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528
Mingming Cao617ba132006-10-11 01:20:53 -0700529 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800530 error = ext4_journal_get_write_access(handle, bh);
531 if (error)
532 goto out;
533
534 lock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
536 ea_bdebug(bh, "refcount now=0; freeing");
537 if (ce)
538 mb_cache_entry_free(ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 get_bh(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500540 ext4_free_blocks(handle, inode, bh, 0, 1,
541 EXT4_FREE_BLOCKS_METADATA |
542 EXT4_FREE_BLOCKS_FORGET);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500543 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 } else {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400545 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500546 if (ce)
547 mb_cache_entry_release(ce);
548 unlock_buffer(bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400549 error = ext4_handle_dirty_xattr_block(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800550 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500551 ext4_handle_sync(handle);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500552 dquot_free_block(inode, 1);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800553 ea_bdebug(bh, "refcount now=%d; releasing",
554 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800556out:
557 ext4_std_error(inode->i_sb, error);
558 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559}
560
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400561/*
562 * Find the available free space for EAs. This also returns the total number of
563 * bytes used by EA entries.
564 */
565static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
566 size_t *min_offs, void *base, int *total)
567{
568 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
569 *total += EXT4_XATTR_LEN(last->e_name_len);
570 if (!last->e_value_block && last->e_value_size) {
571 size_t offs = le16_to_cpu(last->e_value_offs);
572 if (offs < *min_offs)
573 *min_offs = offs;
574 }
575 }
576 return (*min_offs - ((void *)last - base) - sizeof(__u32));
577}
578
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579static int
Mingming Cao617ba132006-10-11 01:20:53 -0700580ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581{
Mingming Cao617ba132006-10-11 01:20:53 -0700582 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
584
585 /* Compute min_offs and last. */
586 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700587 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 if (!last->e_value_block && last->e_value_size) {
589 size_t offs = le16_to_cpu(last->e_value_offs);
590 if (offs < min_offs)
591 min_offs = offs;
592 }
593 }
594 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
595 if (!s->not_found) {
596 if (!s->here->e_value_block && s->here->e_value_size) {
597 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700598 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 }
Mingming Cao617ba132006-10-11 01:20:53 -0700600 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601 }
602 if (i->value) {
Mingming Cao617ba132006-10-11 01:20:53 -0700603 if (free < EXT4_XATTR_SIZE(i->value_len) ||
604 free < EXT4_XATTR_LEN(name_len) +
605 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 return -ENOSPC;
607 }
608
609 if (i->value && s->not_found) {
610 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700611 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
613 memmove((void *)s->here + size, s->here, rest);
614 memset(s->here, 0, size);
615 s->here->e_name_index = i->name_index;
616 s->here->e_name_len = name_len;
617 memcpy(s->here->e_name, i->name, name_len);
618 } else {
619 if (!s->here->e_value_block && s->here->e_value_size) {
620 void *first_val = s->base + min_offs;
621 size_t offs = le16_to_cpu(s->here->e_value_offs);
622 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700623 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 le32_to_cpu(s->here->e_value_size));
625
Mingming Cao617ba132006-10-11 01:20:53 -0700626 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 /* The old and the new value have the same
628 size. Just replace. */
629 s->here->e_value_size =
630 cpu_to_le32(i->value_len);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500631 if (i->value == EXT4_ZERO_XATTR_VALUE) {
632 memset(val, 0, size);
633 } else {
634 /* Clear pad bytes first. */
635 memset(val + size - EXT4_XATTR_PAD, 0,
636 EXT4_XATTR_PAD);
637 memcpy(val, i->value, i->value_len);
638 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639 return 0;
640 }
641
642 /* Remove the old value. */
643 memmove(first_val + size, first_val, val - first_val);
644 memset(first_val, 0, size);
645 s->here->e_value_size = 0;
646 s->here->e_value_offs = 0;
647 min_offs += size;
648
649 /* Adjust all value offsets. */
650 last = s->first;
651 while (!IS_LAST_ENTRY(last)) {
652 size_t o = le16_to_cpu(last->e_value_offs);
653 if (!last->e_value_block &&
654 last->e_value_size && o < offs)
655 last->e_value_offs =
656 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700657 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 }
659 }
660 if (!i->value) {
661 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700662 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 last = ENTRY((void *)last - size);
664 memmove(s->here, (void *)s->here + size,
665 (void *)last - (void *)s->here + sizeof(__u32));
666 memset(last, 0, size);
667 }
668 }
669
670 if (i->value) {
671 /* Insert the new value. */
672 s->here->e_value_size = cpu_to_le32(i->value_len);
673 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700674 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 void *val = s->base + min_offs - size;
676 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Theodore Ts'obd9926e2012-12-11 03:31:49 -0500677 if (i->value == EXT4_ZERO_XATTR_VALUE) {
678 memset(val, 0, size);
679 } else {
680 /* Clear the pad bytes first. */
681 memset(val + size - EXT4_XATTR_PAD, 0,
682 EXT4_XATTR_PAD);
683 memcpy(val, i->value, i->value_len);
684 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 }
686 }
687 return 0;
688}
689
Mingming Cao617ba132006-10-11 01:20:53 -0700690struct ext4_xattr_block_find {
691 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 struct buffer_head *bh;
693};
694
695static int
Mingming Cao617ba132006-10-11 01:20:53 -0700696ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
697 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698{
699 struct super_block *sb = inode->i_sb;
700 int error;
701
702 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
703 i->name_index, i->name, i->value, (long)i->value_len);
704
Mingming Cao617ba132006-10-11 01:20:53 -0700705 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700707 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 error = -EIO;
709 if (!bs->bh)
710 goto cleanup;
711 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
712 atomic_read(&(bs->bh->b_count)),
713 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400714 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400715 EXT4_ERROR_INODE(inode, "bad block %llu",
716 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 error = -EIO;
718 goto cleanup;
719 }
720 /* Find the named attribute. */
721 bs->s.base = BHDR(bs->bh);
722 bs->s.first = BFIRST(bs->bh);
723 bs->s.end = bs->bh->b_data + bs->bh->b_size;
724 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700725 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 i->name, bs->bh->b_size, 1);
727 if (error && error != -ENODATA)
728 goto cleanup;
729 bs->s.not_found = error;
730 }
731 error = 0;
732
733cleanup:
734 return error;
735}
736
737static int
Mingming Cao617ba132006-10-11 01:20:53 -0700738ext4_xattr_block_set(handle_t *handle, struct inode *inode,
739 struct ext4_xattr_info *i,
740 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741{
742 struct super_block *sb = inode->i_sb;
743 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700744 struct ext4_xattr_search *s = &bs->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800746 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747
Mingming Cao617ba132006-10-11 01:20:53 -0700748#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749
750 if (i->value && i->value_len > sb->s_blocksize)
751 return -ENOSPC;
752 if (s->base) {
Mingming Cao617ba132006-10-11 01:20:53 -0700753 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 bs->bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800755 error = ext4_journal_get_write_access(handle, bs->bh);
756 if (error)
757 goto cleanup;
758 lock_buffer(bs->bh);
759
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
761 if (ce) {
762 mb_cache_entry_free(ce);
763 ce = NULL;
764 }
765 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700766 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 if (!error) {
768 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700769 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 s->here);
Mingming Cao617ba132006-10-11 01:20:53 -0700771 ext4_xattr_cache_insert(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 }
773 unlock_buffer(bs->bh);
774 if (error == -EIO)
775 goto bad_block;
776 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400777 error = ext4_handle_dirty_xattr_block(handle,
778 inode,
779 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 if (error)
781 goto cleanup;
782 goto inserted;
783 } else {
784 int offset = (char *)s->here - bs->bh->b_data;
785
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800786 unlock_buffer(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 if (ce) {
788 mb_cache_entry_release(ce);
789 ce = NULL;
790 }
791 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400792 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 error = -ENOMEM;
794 if (s->base == NULL)
795 goto cleanup;
796 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
797 s->first = ENTRY(header(s->base)+1);
798 header(s->base)->h_refcount = cpu_to_le32(1);
799 s->here = ENTRY(s->base + offset);
800 s->end = s->base + bs->bh->b_size;
801 }
802 } else {
803 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400804 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805 /* assert(header == s->base) */
806 error = -ENOMEM;
807 if (s->base == NULL)
808 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700809 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 header(s->base)->h_blocks = cpu_to_le32(1);
811 header(s->base)->h_refcount = cpu_to_le32(1);
812 s->first = ENTRY(header(s->base)+1);
813 s->here = ENTRY(header(s->base)+1);
814 s->end = s->base + sb->s_blocksize;
815 }
816
Mingming Cao617ba132006-10-11 01:20:53 -0700817 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818 if (error == -EIO)
819 goto bad_block;
820 if (error)
821 goto cleanup;
822 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700823 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824
825inserted:
826 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700827 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700828 if (new_bh) {
829 /* We found an identical block in the cache. */
830 if (new_bh == bs->bh)
831 ea_bdebug(new_bh, "keeping");
832 else {
833 /* The old block is released after updating
834 the inode. */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500835 error = dquot_alloc_block(inode, 1);
836 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700838 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 new_bh);
840 if (error)
841 goto cleanup_dquot;
842 lock_buffer(new_bh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400843 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 ea_bdebug(new_bh, "reusing; refcount now=%d",
845 le32_to_cpu(BHDR(new_bh)->h_refcount));
846 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400847 error = ext4_handle_dirty_xattr_block(handle,
848 inode,
849 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 if (error)
851 goto cleanup_dquot;
852 }
853 mb_cache_entry_release(ce);
854 ce = NULL;
855 } else if (bs->bh && s->base == bs->bh->b_data) {
856 /* We were modifying this block in-place. */
857 ea_bdebug(bs->bh, "keeping this block");
858 new_bh = bs->bh;
859 get_bh(new_bh);
860 } else {
861 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400862 ext4_fsblk_t goal, block;
863
864 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400865 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400866
867 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400868 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400869 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
870
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400871 /*
872 * take i_data_sem because we will test
873 * i_delalloc_reserved_flag in ext4_mb_new_blocks
874 */
875 down_read((&EXT4_I(inode)->i_data_sem));
Allison Henderson55f020d2011-05-25 07:41:26 -0400876 block = ext4_new_meta_blocks(handle, inode, goal, 0,
877 NULL, &error);
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400878 up_read((&EXT4_I(inode)->i_data_sem));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 if (error)
880 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400881
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400882 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400883 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
884
Joe Perchesace36ad2012-03-19 23:11:43 -0400885 ea_idebug(inode, "creating block %llu",
886 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887
888 new_bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500889 if (unlikely(!new_bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500890 error = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500892 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500893 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700894 goto cleanup;
895 }
896 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700897 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898 if (error) {
899 unlock_buffer(new_bh);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500900 error = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 goto getblk_failed;
902 }
903 memcpy(new_bh->b_data, s->base, new_bh->b_size);
904 set_buffer_uptodate(new_bh);
905 unlock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700906 ext4_xattr_cache_insert(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400907 error = ext4_handle_dirty_xattr_block(handle,
908 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 if (error)
910 goto cleanup;
911 }
912 }
913
914 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -0700915 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916
917 /* Drop the previous xattr block. */
918 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700919 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920 error = 0;
921
922cleanup:
923 if (ce)
924 mb_cache_entry_release(ce);
925 brelse(new_bh);
926 if (!(bs->bh && s->base == bs->bh->b_data))
927 kfree(s->base);
928
929 return error;
930
931cleanup_dquot:
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500932 dquot_free_block(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 goto cleanup;
934
935bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400936 EXT4_ERROR_INODE(inode, "bad block %llu",
937 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938 goto cleanup;
939
940#undef header
941}
942
Tao Ma879b3822012-12-05 10:28:46 -0500943int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
944 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945{
Mingming Cao617ba132006-10-11 01:20:53 -0700946 struct ext4_xattr_ibody_header *header;
947 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 int error;
949
Mingming Cao617ba132006-10-11 01:20:53 -0700950 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700952 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 header = IHDR(inode, raw_inode);
954 is->s.base = is->s.first = IFIRST(header);
955 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700956 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500957 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700958 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 if (error)
960 return error;
961 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -0700962 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 i->name, is->s.end -
964 (void *)is->s.base, 0);
965 if (error && error != -ENODATA)
966 return error;
967 is->s.not_found = error;
968 }
969 return 0;
970}
971
Tao Ma0d812f72012-12-10 14:06:02 -0500972int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
973 struct ext4_xattr_info *i,
974 struct ext4_xattr_ibody_find *is)
975{
976 struct ext4_xattr_ibody_header *header;
977 struct ext4_xattr_search *s = &is->s;
978 int error;
979
980 if (EXT4_I(inode)->i_extra_isize == 0)
981 return -ENOSPC;
982 error = ext4_xattr_set_entry(i, s);
983 if (error) {
984 if (error == -ENOSPC &&
985 ext4_has_inline_data(inode)) {
986 error = ext4_try_to_evict_inline_data(handle, inode,
987 EXT4_XATTR_LEN(strlen(i->name) +
988 EXT4_XATTR_SIZE(i->value_len)));
989 if (error)
990 return error;
991 error = ext4_xattr_ibody_find(inode, i, is);
992 if (error)
993 return error;
994 error = ext4_xattr_set_entry(i, s);
995 }
996 if (error)
997 return error;
998 }
999 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1000 if (!IS_LAST_ENTRY(s->first)) {
1001 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1002 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1003 } else {
1004 header->h_magic = cpu_to_le32(0);
1005 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1006 }
1007 return 0;
1008}
1009
1010static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1011 struct ext4_xattr_info *i,
1012 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013{
Mingming Cao617ba132006-10-11 01:20:53 -07001014 struct ext4_xattr_ibody_header *header;
1015 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 int error;
1017
Mingming Cao617ba132006-10-11 01:20:53 -07001018 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001019 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001020 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 if (error)
1022 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001023 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001025 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001026 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001027 } else {
1028 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001029 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 }
1031 return 0;
1032}
1033
1034/*
Mingming Cao617ba132006-10-11 01:20:53 -07001035 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001036 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001037 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 * is NULL to remove an existing extended attribute, and non-NULL to
1039 * either replace an existing extended attribute, or create a new extended
1040 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1041 * specify that an extended attribute must exist and must not exist
1042 * previous to the call, respectively.
1043 *
1044 * Returns 0, or a negative error number on failure.
1045 */
1046int
Mingming Cao617ba132006-10-11 01:20:53 -07001047ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048 const char *name, const void *value, size_t value_len,
1049 int flags)
1050{
Mingming Cao617ba132006-10-11 01:20:53 -07001051 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 .name_index = name_index,
1053 .name = name,
1054 .value = value,
1055 .value_len = value_len,
1056
1057 };
Mingming Cao617ba132006-10-11 01:20:53 -07001058 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059 .s = { .not_found = -ENODATA, },
1060 };
Mingming Cao617ba132006-10-11 01:20:53 -07001061 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 .s = { .not_found = -ENODATA, },
1063 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001064 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 int error;
1066
1067 if (!name)
1068 return -EINVAL;
1069 if (strlen(name) > 255)
1070 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001071 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001072 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1073 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001074
Eric Sandeen66543612011-10-26 03:32:07 -04001075 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001076 if (error)
1077 goto cleanup;
1078
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001079 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001080 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1081 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001082 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001083 }
1084
Mingming Cao617ba132006-10-11 01:20:53 -07001085 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 if (error)
1087 goto cleanup;
1088 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001089 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090 if (error)
1091 goto cleanup;
1092 if (is.s.not_found && bs.s.not_found) {
1093 error = -ENODATA;
1094 if (flags & XATTR_REPLACE)
1095 goto cleanup;
1096 error = 0;
1097 if (!value)
1098 goto cleanup;
1099 } else {
1100 error = -EEXIST;
1101 if (flags & XATTR_CREATE)
1102 goto cleanup;
1103 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104 if (!value) {
1105 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001106 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001107 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001108 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001110 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111 if (!error && !bs.s.not_found) {
1112 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001113 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001115 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1116 error = ext4_xattr_block_find(inode, &i, &bs);
1117 if (error)
1118 goto cleanup;
1119 }
Mingming Cao617ba132006-10-11 01:20:53 -07001120 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 if (error)
1122 goto cleanup;
1123 if (!is.s.not_found) {
1124 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001125 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 &is);
1127 }
1128 }
1129 }
1130 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001131 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001132 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001133 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001134 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001135 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001137 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 * error != 0.
1139 */
1140 is.iloc.bh = NULL;
1141 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001142 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 }
1144
1145cleanup:
1146 brelse(is.iloc.bh);
1147 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001148 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001149 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001150 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001151 return error;
1152}
1153
1154/*
Mingming Cao617ba132006-10-11 01:20:53 -07001155 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156 *
Mingming Cao617ba132006-10-11 01:20:53 -07001157 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 * attribute modification is a filesystem transaction by itself.
1159 *
1160 * Returns 0, or a negative error number on failure.
1161 */
1162int
Mingming Cao617ba132006-10-11 01:20:53 -07001163ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 const void *value, size_t value_len, int flags)
1165{
1166 handle_t *handle;
1167 int error, retries = 0;
Theodore Ts'o95eaefb2013-02-09 15:23:03 -05001168 int credits = ext4_jbd2_credits_xattr(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169
1170retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001171 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 if (IS_ERR(handle)) {
1173 error = PTR_ERR(handle);
1174 } else {
1175 int error2;
1176
Mingming Cao617ba132006-10-11 01:20:53 -07001177 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001179 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001181 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001182 goto retry;
1183 if (error == 0)
1184 error = error2;
1185 }
1186
1187 return error;
1188}
1189
1190/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001191 * Shift the EA entries in the inode to create space for the increased
1192 * i_extra_isize.
1193 */
1194static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1195 int value_offs_shift, void *to,
1196 void *from, size_t n, int blocksize)
1197{
1198 struct ext4_xattr_entry *last = entry;
1199 int new_offs;
1200
1201 /* Adjust the value offsets of the entries */
1202 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1203 if (!last->e_value_block && last->e_value_size) {
1204 new_offs = le16_to_cpu(last->e_value_offs) +
1205 value_offs_shift;
1206 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1207 > blocksize);
1208 last->e_value_offs = cpu_to_le16(new_offs);
1209 }
1210 }
1211 /* Shift the entries by n bytes */
1212 memmove(to, from, n);
1213}
1214
1215/*
1216 * Expand an inode by new_extra_isize bytes when EAs are present.
1217 * Returns 0 on success or negative error number on failure.
1218 */
1219int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1220 struct ext4_inode *raw_inode, handle_t *handle)
1221{
1222 struct ext4_xattr_ibody_header *header;
1223 struct ext4_xattr_entry *entry, *last, *first;
1224 struct buffer_head *bh = NULL;
1225 struct ext4_xattr_ibody_find *is = NULL;
1226 struct ext4_xattr_block_find *bs = NULL;
1227 char *buffer = NULL, *b_entry_name = NULL;
1228 size_t min_offs, free;
1229 int total_ino, total_blk;
1230 void *base, *start, *end;
1231 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001232 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 -04001233
1234 down_write(&EXT4_I(inode)->xattr_sem);
1235retry:
1236 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1237 up_write(&EXT4_I(inode)->xattr_sem);
1238 return 0;
1239 }
1240
1241 header = IHDR(inode, raw_inode);
1242 entry = IFIRST(header);
1243
1244 /*
1245 * Check if enough free space is available in the inode to shift the
1246 * entries ahead by new_extra_isize.
1247 */
1248
1249 base = start = entry;
1250 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1251 min_offs = end - base;
1252 last = entry;
1253 total_ino = sizeof(struct ext4_xattr_ibody_header);
1254
1255 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1256 if (free >= new_extra_isize) {
1257 entry = IFIRST(header);
1258 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1259 - new_extra_isize, (void *)raw_inode +
1260 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1261 (void *)header, total_ino,
1262 inode->i_sb->s_blocksize);
1263 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1264 error = 0;
1265 goto cleanup;
1266 }
1267
1268 /*
1269 * Enough free space isn't available in the inode, check if
1270 * EA block can hold new_extra_isize bytes.
1271 */
1272 if (EXT4_I(inode)->i_file_acl) {
1273 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1274 error = -EIO;
1275 if (!bh)
1276 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001277 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001278 EXT4_ERROR_INODE(inode, "bad block %llu",
1279 EXT4_I(inode)->i_file_acl);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001280 error = -EIO;
1281 goto cleanup;
1282 }
1283 base = BHDR(bh);
1284 first = BFIRST(bh);
1285 end = bh->b_data + bh->b_size;
1286 min_offs = end - base;
1287 free = ext4_xattr_free_space(first, &min_offs, base,
1288 &total_blk);
1289 if (free < new_extra_isize) {
1290 if (!tried_min_extra_isize && s_min_extra_isize) {
1291 tried_min_extra_isize++;
1292 new_extra_isize = s_min_extra_isize;
1293 brelse(bh);
1294 goto retry;
1295 }
1296 error = -1;
1297 goto cleanup;
1298 }
1299 } else {
1300 free = inode->i_sb->s_blocksize;
1301 }
1302
1303 while (new_extra_isize > 0) {
1304 size_t offs, size, entry_size;
1305 struct ext4_xattr_entry *small_entry = NULL;
1306 struct ext4_xattr_info i = {
1307 .value = NULL,
1308 .value_len = 0,
1309 };
1310 unsigned int total_size; /* EA entry size + value size */
1311 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1312 unsigned int min_total_size = ~0U;
1313
1314 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1315 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1316 if (!is || !bs) {
1317 error = -ENOMEM;
1318 goto cleanup;
1319 }
1320
1321 is->s.not_found = -ENODATA;
1322 bs->s.not_found = -ENODATA;
1323 is->iloc.bh = NULL;
1324 bs->bh = NULL;
1325
1326 last = IFIRST(header);
1327 /* Find the entry best suited to be pushed into EA block */
1328 entry = NULL;
1329 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1330 total_size =
1331 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1332 EXT4_XATTR_LEN(last->e_name_len);
1333 if (total_size <= free && total_size < min_total_size) {
1334 if (total_size < new_extra_isize) {
1335 small_entry = last;
1336 } else {
1337 entry = last;
1338 min_total_size = total_size;
1339 }
1340 }
1341 }
1342
1343 if (entry == NULL) {
1344 if (small_entry) {
1345 entry = small_entry;
1346 } else {
1347 if (!tried_min_extra_isize &&
1348 s_min_extra_isize) {
1349 tried_min_extra_isize++;
1350 new_extra_isize = s_min_extra_isize;
1351 goto retry;
1352 }
1353 error = -1;
1354 goto cleanup;
1355 }
1356 }
1357 offs = le16_to_cpu(entry->e_value_offs);
1358 size = le32_to_cpu(entry->e_value_size);
1359 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1360 i.name_index = entry->e_name_index,
1361 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1362 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1363 if (!buffer || !b_entry_name) {
1364 error = -ENOMEM;
1365 goto cleanup;
1366 }
1367 /* Save the entry name and the entry value */
1368 memcpy(buffer, (void *)IFIRST(header) + offs,
1369 EXT4_XATTR_SIZE(size));
1370 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1371 b_entry_name[entry->e_name_len] = '\0';
1372 i.name = b_entry_name;
1373
1374 error = ext4_get_inode_loc(inode, &is->iloc);
1375 if (error)
1376 goto cleanup;
1377
1378 error = ext4_xattr_ibody_find(inode, &i, is);
1379 if (error)
1380 goto cleanup;
1381
1382 /* Remove the chosen entry from the inode */
1383 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001384 if (error)
1385 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001386
1387 entry = IFIRST(header);
1388 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1389 shift_bytes = new_extra_isize;
1390 else
1391 shift_bytes = entry_size + size;
1392 /* Adjust the offsets and shift the remaining entries ahead */
1393 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1394 shift_bytes, (void *)raw_inode +
1395 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1396 (void *)header, total_ino - entry_size,
1397 inode->i_sb->s_blocksize);
1398
1399 extra_isize += shift_bytes;
1400 new_extra_isize -= shift_bytes;
1401 EXT4_I(inode)->i_extra_isize = extra_isize;
1402
1403 i.name = b_entry_name;
1404 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001405 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001406 error = ext4_xattr_block_find(inode, &i, bs);
1407 if (error)
1408 goto cleanup;
1409
1410 /* Add entry which was removed from the inode into the block */
1411 error = ext4_xattr_block_set(handle, inode, &i, bs);
1412 if (error)
1413 goto cleanup;
1414 kfree(b_entry_name);
1415 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001416 b_entry_name = NULL;
1417 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001418 brelse(is->iloc.bh);
1419 kfree(is);
1420 kfree(bs);
1421 }
1422 brelse(bh);
1423 up_write(&EXT4_I(inode)->xattr_sem);
1424 return 0;
1425
1426cleanup:
1427 kfree(b_entry_name);
1428 kfree(buffer);
1429 if (is)
1430 brelse(is->iloc.bh);
1431 kfree(is);
1432 kfree(bs);
1433 brelse(bh);
1434 up_write(&EXT4_I(inode)->xattr_sem);
1435 return error;
1436}
1437
1438
1439
1440/*
Mingming Cao617ba132006-10-11 01:20:53 -07001441 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442 *
1443 * Free extended attribute resources associated with this inode. This
1444 * is called immediately before an inode is freed. We have exclusive
1445 * access to the inode.
1446 */
1447void
Mingming Cao617ba132006-10-11 01:20:53 -07001448ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449{
1450 struct buffer_head *bh = NULL;
1451
Mingming Cao617ba132006-10-11 01:20:53 -07001452 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001454 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001456 EXT4_ERROR_INODE(inode, "block %llu read error",
1457 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458 goto cleanup;
1459 }
Mingming Cao617ba132006-10-11 01:20:53 -07001460 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001462 EXT4_ERROR_INODE(inode, "bad block %llu",
1463 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464 goto cleanup;
1465 }
Mingming Cao617ba132006-10-11 01:20:53 -07001466 ext4_xattr_release_block(handle, inode, bh);
1467 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468
1469cleanup:
1470 brelse(bh);
1471}
1472
1473/*
Mingming Cao617ba132006-10-11 01:20:53 -07001474 * ext4_xattr_put_super()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001475 *
1476 * This is called when a file system is unmounted.
1477 */
1478void
Mingming Cao617ba132006-10-11 01:20:53 -07001479ext4_xattr_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480{
1481 mb_cache_shrink(sb->s_bdev);
1482}
1483
1484/*
Mingming Cao617ba132006-10-11 01:20:53 -07001485 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486 *
1487 * Create a new entry in the extended attribute cache, and insert
1488 * it unless such an entry is already in the cache.
1489 *
1490 * Returns 0, or a negative error number on failure.
1491 */
1492static void
Mingming Cao617ba132006-10-11 01:20:53 -07001493ext4_xattr_cache_insert(struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494{
1495 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1496 struct mb_cache_entry *ce;
1497 int error;
1498
Jan Kara335e92e2008-04-15 14:34:43 -07001499 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500 if (!ce) {
1501 ea_bdebug(bh, "out of memory");
1502 return;
1503 }
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001504 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 if (error) {
1506 mb_cache_entry_free(ce);
1507 if (error == -EBUSY) {
1508 ea_bdebug(bh, "already in cache");
1509 error = 0;
1510 }
1511 } else {
1512 ea_bdebug(bh, "inserting [%x]", (int)hash);
1513 mb_cache_entry_release(ce);
1514 }
1515}
1516
1517/*
Mingming Cao617ba132006-10-11 01:20:53 -07001518 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001519 *
1520 * Compare two extended attribute blocks for equality.
1521 *
1522 * Returns 0 if the blocks are equal, 1 if they differ, and
1523 * a negative error number on errors.
1524 */
1525static int
Mingming Cao617ba132006-10-11 01:20:53 -07001526ext4_xattr_cmp(struct ext4_xattr_header *header1,
1527 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528{
Mingming Cao617ba132006-10-11 01:20:53 -07001529 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530
1531 entry1 = ENTRY(header1+1);
1532 entry2 = ENTRY(header2+1);
1533 while (!IS_LAST_ENTRY(entry1)) {
1534 if (IS_LAST_ENTRY(entry2))
1535 return 1;
1536 if (entry1->e_hash != entry2->e_hash ||
1537 entry1->e_name_index != entry2->e_name_index ||
1538 entry1->e_name_len != entry2->e_name_len ||
1539 entry1->e_value_size != entry2->e_value_size ||
1540 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1541 return 1;
1542 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1543 return -EIO;
1544 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1545 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1546 le32_to_cpu(entry1->e_value_size)))
1547 return 1;
1548
Mingming Cao617ba132006-10-11 01:20:53 -07001549 entry1 = EXT4_XATTR_NEXT(entry1);
1550 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 }
1552 if (!IS_LAST_ENTRY(entry2))
1553 return 1;
1554 return 0;
1555}
1556
1557/*
Mingming Cao617ba132006-10-11 01:20:53 -07001558 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001559 *
1560 * Find an identical extended attribute block.
1561 *
1562 * Returns a pointer to the block found, or NULL if such a block was
1563 * not found or an error occurred.
1564 */
1565static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001566ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 struct mb_cache_entry **pce)
1568{
1569 __u32 hash = le32_to_cpu(header->h_hash);
1570 struct mb_cache_entry *ce;
1571
1572 if (!header->h_hash)
1573 return NULL; /* never share */
1574 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1575again:
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001576 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1577 hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 while (ce) {
1579 struct buffer_head *bh;
1580
1581 if (IS_ERR(ce)) {
1582 if (PTR_ERR(ce) == -EAGAIN)
1583 goto again;
1584 break;
1585 }
1586 bh = sb_bread(inode->i_sb, ce->e_block);
1587 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001588 EXT4_ERROR_INODE(inode, "block %lu read error",
1589 (unsigned long) ce->e_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001590 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
Mingming Cao617ba132006-10-11 01:20:53 -07001591 EXT4_XATTR_REFCOUNT_MAX) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592 ea_idebug(inode, "block %lu refcount %d>=%d",
1593 (unsigned long) ce->e_block,
1594 le32_to_cpu(BHDR(bh)->h_refcount),
Mingming Cao617ba132006-10-11 01:20:53 -07001595 EXT4_XATTR_REFCOUNT_MAX);
1596 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597 *pce = ce;
1598 return bh;
1599 }
1600 brelse(bh);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001601 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 }
1603 return NULL;
1604}
1605
1606#define NAME_HASH_SHIFT 5
1607#define VALUE_HASH_SHIFT 16
1608
1609/*
Mingming Cao617ba132006-10-11 01:20:53 -07001610 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 *
1612 * Compute the hash of an extended attribute.
1613 */
Mingming Cao617ba132006-10-11 01:20:53 -07001614static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1615 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616{
1617 __u32 hash = 0;
1618 char *name = entry->e_name;
1619 int n;
1620
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001621 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622 hash = (hash << NAME_HASH_SHIFT) ^
1623 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1624 *name++;
1625 }
1626
1627 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1628 __le32 *value = (__le32 *)((char *)header +
1629 le16_to_cpu(entry->e_value_offs));
1630 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001631 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 hash = (hash << VALUE_HASH_SHIFT) ^
1633 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1634 le32_to_cpu(*value++);
1635 }
1636 }
1637 entry->e_hash = cpu_to_le32(hash);
1638}
1639
1640#undef NAME_HASH_SHIFT
1641#undef VALUE_HASH_SHIFT
1642
1643#define BLOCK_HASH_SHIFT 16
1644
1645/*
Mingming Cao617ba132006-10-11 01:20:53 -07001646 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 *
1648 * Re-compute the extended attribute hash value after an entry has changed.
1649 */
Mingming Cao617ba132006-10-11 01:20:53 -07001650static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1651 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652{
Mingming Cao617ba132006-10-11 01:20:53 -07001653 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654 __u32 hash = 0;
1655
Mingming Cao617ba132006-10-11 01:20:53 -07001656 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 here = ENTRY(header+1);
1658 while (!IS_LAST_ENTRY(here)) {
1659 if (!here->e_hash) {
1660 /* Block is not shared if an entry's hash value == 0 */
1661 hash = 0;
1662 break;
1663 }
1664 hash = (hash << BLOCK_HASH_SHIFT) ^
1665 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1666 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001667 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001668 }
1669 header->h_hash = cpu_to_le32(hash);
1670}
1671
1672#undef BLOCK_HASH_SHIFT
1673
1674int __init
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04001675ext4_init_xattr(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001676{
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001677 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
Mingming Cao617ba132006-10-11 01:20:53 -07001678 if (!ext4_xattr_cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679 return -ENOMEM;
1680 return 0;
1681}
1682
1683void
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04001684ext4_exit_xattr(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001685{
Mingming Cao617ba132006-10-11 01:20:53 -07001686 if (ext4_xattr_cache)
1687 mb_cache_destroy(ext4_xattr_cache);
1688 ext4_xattr_cache = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689}