blob: e56c9ed7d6e30d523b7f8e4b638f9190427cf50d [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#define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
65#define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066#define BFIRST(bh) ENTRY(BHDR(bh)+1)
67#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
68
Mingming Cao617ba132006-10-11 01:20:53 -070069#ifdef EXT4_XATTR_DEBUG
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070# define ea_idebug(inode, f...) do { \
71 printk(KERN_DEBUG "inode %s:%lu: ", \
72 inode->i_sb->s_id, inode->i_ino); \
73 printk(f); \
74 printk("\n"); \
75 } while (0)
76# define ea_bdebug(bh, f...) do { \
77 char b[BDEVNAME_SIZE]; \
78 printk(KERN_DEBUG "block %s:%lu: ", \
79 bdevname(bh->b_bdev, b), \
80 (unsigned long) bh->b_blocknr); \
81 printk(f); \
82 printk("\n"); \
83 } while (0)
84#else
Joe Perchesace36ad2012-03-19 23:11:43 -040085# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
86# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087#endif
88
Mingming Cao617ba132006-10-11 01:20:53 -070089static void ext4_xattr_cache_insert(struct buffer_head *);
90static struct buffer_head *ext4_xattr_cache_find(struct inode *,
91 struct ext4_xattr_header *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092 struct mb_cache_entry **);
Mingming Cao617ba132006-10-11 01:20:53 -070093static void ext4_xattr_rehash(struct ext4_xattr_header *,
94 struct ext4_xattr_entry *);
Christoph Hellwig431547b2009-11-13 09:52:56 +000095static int ext4_xattr_list(struct dentry *dentry, char *buffer,
Mingming Caod3a95d42008-04-17 10:38:59 -040096 size_t buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097
Mingming Cao617ba132006-10-11 01:20:53 -070098static struct mb_cache *ext4_xattr_cache;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070099
Stephen Hemminger11e27522010-05-13 17:53:18 -0700100static const struct xattr_handler *ext4_xattr_handler_map[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700101 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400102#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700103 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
104 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700106 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400107#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700108 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700109#endif
110};
111
Stephen Hemminger11e27522010-05-13 17:53:18 -0700112const struct xattr_handler *ext4_xattr_handlers[] = {
Mingming Cao617ba132006-10-11 01:20:53 -0700113 &ext4_xattr_user_handler,
114 &ext4_xattr_trusted_handler,
Theodore Ts'o03010a32008-10-10 20:02:48 -0400115#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700116 &ext4_xattr_acl_access_handler,
117 &ext4_xattr_acl_default_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700118#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400119#ifdef CONFIG_EXT4_FS_SECURITY
Mingming Cao617ba132006-10-11 01:20:53 -0700120 &ext4_xattr_security_handler,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700121#endif
122 NULL
123};
124
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400125static __le32 ext4_xattr_block_csum(struct inode *inode,
126 sector_t block_nr,
127 struct ext4_xattr_header *hdr)
128{
129 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
130 struct ext4_inode_info *ei = EXT4_I(inode);
131 __u32 csum, old;
132
133 old = hdr->h_checksum;
134 hdr->h_checksum = 0;
135 if (le32_to_cpu(hdr->h_refcount) != 1) {
136 block_nr = cpu_to_le64(block_nr);
137 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&block_nr,
138 sizeof(block_nr));
139 } else
140 csum = ei->i_csum_seed;
141 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
142 EXT4_BLOCK_SIZE(inode->i_sb));
143 hdr->h_checksum = old;
144 return cpu_to_le32(csum);
145}
146
147static int ext4_xattr_block_csum_verify(struct inode *inode,
148 sector_t block_nr,
149 struct ext4_xattr_header *hdr)
150{
151 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
152 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
153 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
154 return 0;
155 return 1;
156}
157
158static void ext4_xattr_block_csum_set(struct inode *inode,
159 sector_t block_nr,
160 struct ext4_xattr_header *hdr)
161{
162 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
163 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
164 return;
165
166 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
167}
168
169static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
170 struct inode *inode,
171 struct buffer_head *bh)
172{
173 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
174 return ext4_handle_dirty_metadata(handle, inode, bh);
175}
176
Stephen Hemminger11e27522010-05-13 17:53:18 -0700177static inline const struct xattr_handler *
Mingming Cao617ba132006-10-11 01:20:53 -0700178ext4_xattr_handler(int name_index)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700179{
Stephen Hemminger11e27522010-05-13 17:53:18 -0700180 const struct xattr_handler *handler = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181
Mingming Cao617ba132006-10-11 01:20:53 -0700182 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
183 handler = ext4_xattr_handler_map[name_index];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 return handler;
185}
186
187/*
188 * Inode operation listxattr()
189 *
190 * dentry->d_inode->i_mutex: don't care
191 */
192ssize_t
Mingming Cao617ba132006-10-11 01:20:53 -0700193ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000195 return ext4_xattr_list(dentry, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196}
197
198static int
Mingming Cao617ba132006-10-11 01:20:53 -0700199ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200{
201 while (!IS_LAST_ENTRY(entry)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700202 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 if ((void *)next >= end)
204 return -EIO;
205 entry = next;
206 }
207 return 0;
208}
209
210static inline int
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400211ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212{
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400213 int error;
214
215 if (buffer_verified(bh))
216 return 0;
217
Mingming Cao617ba132006-10-11 01:20:53 -0700218 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 BHDR(bh)->h_blocks != cpu_to_le32(1))
220 return -EIO;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400221 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
222 return -EIO;
223 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
224 if (!error)
225 set_buffer_verified(bh);
226 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227}
228
229static inline int
Mingming Cao617ba132006-10-11 01:20:53 -0700230ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231{
232 size_t value_size = le32_to_cpu(entry->e_value_size);
233
234 if (entry->e_value_block != 0 || value_size > size ||
235 le16_to_cpu(entry->e_value_offs) + value_size > size)
236 return -EIO;
237 return 0;
238}
239
240static int
Mingming Cao617ba132006-10-11 01:20:53 -0700241ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 const char *name, size_t size, int sorted)
243{
Mingming Cao617ba132006-10-11 01:20:53 -0700244 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 size_t name_len;
246 int cmp = 1;
247
248 if (name == NULL)
249 return -EINVAL;
250 name_len = strlen(name);
251 entry = *pentry;
Mingming Cao617ba132006-10-11 01:20:53 -0700252 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253 cmp = name_index - entry->e_name_index;
254 if (!cmp)
255 cmp = name_len - entry->e_name_len;
256 if (!cmp)
257 cmp = memcmp(name, entry->e_name, name_len);
258 if (cmp <= 0 && (sorted || cmp == 0))
259 break;
260 }
261 *pentry = entry;
Mingming Cao617ba132006-10-11 01:20:53 -0700262 if (!cmp && ext4_xattr_check_entry(entry, size))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 return -EIO;
264 return cmp ? -ENODATA : 0;
265}
266
267static int
Mingming Cao617ba132006-10-11 01:20:53 -0700268ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269 void *buffer, size_t buffer_size)
270{
271 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700272 struct ext4_xattr_entry *entry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 size_t size;
274 int error;
275
276 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
277 name_index, name, buffer, (long)buffer_size);
278
279 error = -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700280 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400282 ea_idebug(inode, "reading block %llu",
283 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700284 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 if (!bh)
286 goto cleanup;
287 ea_bdebug(bh, "b_count=%d, refcount=%d",
288 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400289 if (ext4_xattr_check_block(inode, bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500290bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400291 EXT4_ERROR_INODE(inode, "bad block %llu",
292 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 error = -EIO;
294 goto cleanup;
295 }
Mingming Cao617ba132006-10-11 01:20:53 -0700296 ext4_xattr_cache_insert(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 entry = BFIRST(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700298 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 if (error == -EIO)
300 goto bad_block;
301 if (error)
302 goto cleanup;
303 size = le32_to_cpu(entry->e_value_size);
304 if (buffer) {
305 error = -ERANGE;
306 if (size > buffer_size)
307 goto cleanup;
308 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
309 size);
310 }
311 error = size;
312
313cleanup:
314 brelse(bh);
315 return error;
316}
317
318static int
Mingming Cao617ba132006-10-11 01:20:53 -0700319ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 void *buffer, size_t buffer_size)
321{
Mingming Cao617ba132006-10-11 01:20:53 -0700322 struct ext4_xattr_ibody_header *header;
323 struct ext4_xattr_entry *entry;
324 struct ext4_inode *raw_inode;
325 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 size_t size;
327 void *end;
328 int error;
329
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500330 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 return -ENODATA;
Mingming Cao617ba132006-10-11 01:20:53 -0700332 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 if (error)
334 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700335 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 header = IHDR(inode, raw_inode);
337 entry = IFIRST(header);
Mingming Cao617ba132006-10-11 01:20:53 -0700338 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
339 error = ext4_xattr_check_names(entry, end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 if (error)
341 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700342 error = ext4_xattr_find_entry(&entry, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 end - (void *)entry, 0);
344 if (error)
345 goto cleanup;
346 size = le32_to_cpu(entry->e_value_size);
347 if (buffer) {
348 error = -ERANGE;
349 if (size > buffer_size)
350 goto cleanup;
351 memcpy(buffer, (void *)IFIRST(header) +
352 le16_to_cpu(entry->e_value_offs), size);
353 }
354 error = size;
355
356cleanup:
357 brelse(iloc.bh);
358 return error;
359}
360
361/*
Mingming Cao617ba132006-10-11 01:20:53 -0700362 * ext4_xattr_get()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 *
364 * Copy an extended attribute into the buffer
365 * provided, or compute the buffer size required.
366 * Buffer is NULL to compute the size of the buffer required.
367 *
368 * Returns a negative error number on failure, or the number of bytes
369 * used / required on success.
370 */
371int
Mingming Cao617ba132006-10-11 01:20:53 -0700372ext4_xattr_get(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700373 void *buffer, size_t buffer_size)
374{
375 int error;
376
Mingming Cao617ba132006-10-11 01:20:53 -0700377 down_read(&EXT4_I(inode)->xattr_sem);
378 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700379 buffer_size);
380 if (error == -ENODATA)
Mingming Cao617ba132006-10-11 01:20:53 -0700381 error = ext4_xattr_block_get(inode, name_index, name, buffer,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382 buffer_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700383 up_read(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384 return error;
385}
386
387static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000388ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700389 char *buffer, size_t buffer_size)
390{
391 size_t rest = buffer_size;
392
Mingming Cao617ba132006-10-11 01:20:53 -0700393 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
Stephen Hemminger11e27522010-05-13 17:53:18 -0700394 const struct xattr_handler *handler =
Mingming Cao617ba132006-10-11 01:20:53 -0700395 ext4_xattr_handler(entry->e_name_index);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396
397 if (handler) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000398 size_t size = handler->list(dentry, buffer, rest,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399 entry->e_name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000400 entry->e_name_len,
401 handler->flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402 if (buffer) {
403 if (size > rest)
404 return -ERANGE;
405 buffer += size;
406 }
407 rest -= size;
408 }
409 }
410 return buffer_size - rest;
411}
412
413static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000414ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000416 struct inode *inode = dentry->d_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417 struct buffer_head *bh = NULL;
418 int error;
419
420 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
421 buffer, (long)buffer_size);
422
423 error = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700424 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425 goto cleanup;
Joe Perchesace36ad2012-03-19 23:11:43 -0400426 ea_idebug(inode, "reading block %llu",
427 (unsigned long long)EXT4_I(inode)->i_file_acl);
Mingming Cao617ba132006-10-11 01:20:53 -0700428 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429 error = -EIO;
430 if (!bh)
431 goto cleanup;
432 ea_bdebug(bh, "b_count=%d, refcount=%d",
433 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400434 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400435 EXT4_ERROR_INODE(inode, "bad block %llu",
436 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437 error = -EIO;
438 goto cleanup;
439 }
Mingming Cao617ba132006-10-11 01:20:53 -0700440 ext4_xattr_cache_insert(bh);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000441 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442
443cleanup:
444 brelse(bh);
445
446 return error;
447}
448
449static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000450ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000452 struct inode *inode = dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700453 struct ext4_xattr_ibody_header *header;
454 struct ext4_inode *raw_inode;
455 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 void *end;
457 int error;
458
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500459 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700461 error = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 if (error)
463 return error;
Mingming Cao617ba132006-10-11 01:20:53 -0700464 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465 header = IHDR(inode, raw_inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700466 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
467 error = ext4_xattr_check_names(IFIRST(header), end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 if (error)
469 goto cleanup;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000470 error = ext4_xattr_list_entries(dentry, IFIRST(header),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 buffer, buffer_size);
472
473cleanup:
474 brelse(iloc.bh);
475 return error;
476}
477
478/*
Mingming Cao617ba132006-10-11 01:20:53 -0700479 * ext4_xattr_list()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 *
481 * Copy a list of attribute names into the buffer
482 * provided, or compute the buffer size required.
483 * Buffer is NULL to compute the size of the buffer required.
484 *
485 * Returns a negative error number on failure, or the number of bytes
486 * used / required on success.
487 */
Mingming Caod3a95d42008-04-17 10:38:59 -0400488static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000489ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490{
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500491 int ret, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492
Christoph Hellwig431547b2009-11-13 09:52:56 +0000493 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500494 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
495 if (ret < 0)
496 goto errout;
497 if (buffer) {
498 buffer += ret;
499 buffer_size -= ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 }
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500501 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
502 if (ret < 0)
503 goto errout;
504 ret += ret2;
505errout:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000506 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
Theodore Ts'oeaeef862011-01-10 12:10:07 -0500507 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508}
509
510/*
Mingming Cao617ba132006-10-11 01:20:53 -0700511 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 * not set, set it.
513 */
Mingming Cao617ba132006-10-11 01:20:53 -0700514static void ext4_xattr_update_super_block(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 struct super_block *sb)
516{
Mingming Cao617ba132006-10-11 01:20:53 -0700517 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 return;
519
Mingming Cao617ba132006-10-11 01:20:53 -0700520 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
Andreas Gruenbachered2908f2006-12-06 20:36:16 -0800521 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400522 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524}
525
526/*
527 * Release the xattr block BH: If the reference count is > 1, decrement
528 * it; otherwise free the block.
529 */
530static void
Mingming Cao617ba132006-10-11 01:20:53 -0700531ext4_xattr_release_block(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 struct buffer_head *bh)
533{
534 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800535 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536
Mingming Cao617ba132006-10-11 01:20:53 -0700537 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800538 error = ext4_journal_get_write_access(handle, bh);
539 if (error)
540 goto out;
541
542 lock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
544 ea_bdebug(bh, "refcount now=0; freeing");
545 if (ce)
546 mb_cache_entry_free(ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 get_bh(bh);
Theodore Ts'oe6362602009-11-23 07:17:05 -0500548 ext4_free_blocks(handle, inode, bh, 0, 1,
549 EXT4_FREE_BLOCKS_METADATA |
550 EXT4_FREE_BLOCKS_FORGET);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500551 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 } else {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400553 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Eric Sandeenc1bb05a2012-02-20 23:06:18 -0500554 if (ce)
555 mb_cache_entry_release(ce);
556 unlock_buffer(bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400557 error = ext4_handle_dirty_xattr_block(handle, inode, bh);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800558 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500559 ext4_handle_sync(handle);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500560 dquot_free_block(inode, 1);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800561 ea_bdebug(bh, "refcount now=%d; releasing",
562 le32_to_cpu(BHDR(bh)->h_refcount));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800564out:
565 ext4_std_error(inode->i_sb, error);
566 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567}
568
Kalpak Shah6dd4ee72007-07-18 09:19:57 -0400569/*
570 * Find the available free space for EAs. This also returns the total number of
571 * bytes used by EA entries.
572 */
573static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
574 size_t *min_offs, void *base, int *total)
575{
576 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
577 *total += EXT4_XATTR_LEN(last->e_name_len);
578 if (!last->e_value_block && last->e_value_size) {
579 size_t offs = le16_to_cpu(last->e_value_offs);
580 if (offs < *min_offs)
581 *min_offs = offs;
582 }
583 }
584 return (*min_offs - ((void *)last - base) - sizeof(__u32));
585}
586
Mingming Cao617ba132006-10-11 01:20:53 -0700587struct ext4_xattr_info {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 int name_index;
589 const char *name;
590 const void *value;
591 size_t value_len;
592};
593
Mingming Cao617ba132006-10-11 01:20:53 -0700594struct ext4_xattr_search {
595 struct ext4_xattr_entry *first;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 void *base;
597 void *end;
Mingming Cao617ba132006-10-11 01:20:53 -0700598 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 int not_found;
600};
601
602static int
Mingming Cao617ba132006-10-11 01:20:53 -0700603ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700604{
Mingming Cao617ba132006-10-11 01:20:53 -0700605 struct ext4_xattr_entry *last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
607
608 /* Compute min_offs and last. */
609 last = s->first;
Mingming Cao617ba132006-10-11 01:20:53 -0700610 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611 if (!last->e_value_block && last->e_value_size) {
612 size_t offs = le16_to_cpu(last->e_value_offs);
613 if (offs < min_offs)
614 min_offs = offs;
615 }
616 }
617 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
618 if (!s->not_found) {
619 if (!s->here->e_value_block && s->here->e_value_size) {
620 size_t size = le32_to_cpu(s->here->e_value_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700621 free += EXT4_XATTR_SIZE(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 }
Mingming Cao617ba132006-10-11 01:20:53 -0700623 free += EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 }
625 if (i->value) {
Mingming Cao617ba132006-10-11 01:20:53 -0700626 if (free < EXT4_XATTR_SIZE(i->value_len) ||
627 free < EXT4_XATTR_LEN(name_len) +
628 EXT4_XATTR_SIZE(i->value_len))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629 return -ENOSPC;
630 }
631
632 if (i->value && s->not_found) {
633 /* Insert the new name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700634 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
636 memmove((void *)s->here + size, s->here, rest);
637 memset(s->here, 0, size);
638 s->here->e_name_index = i->name_index;
639 s->here->e_name_len = name_len;
640 memcpy(s->here->e_name, i->name, name_len);
641 } else {
642 if (!s->here->e_value_block && s->here->e_value_size) {
643 void *first_val = s->base + min_offs;
644 size_t offs = le16_to_cpu(s->here->e_value_offs);
645 void *val = s->base + offs;
Mingming Cao617ba132006-10-11 01:20:53 -0700646 size_t size = EXT4_XATTR_SIZE(
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 le32_to_cpu(s->here->e_value_size));
648
Mingming Cao617ba132006-10-11 01:20:53 -0700649 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 /* The old and the new value have the same
651 size. Just replace. */
652 s->here->e_value_size =
653 cpu_to_le32(i->value_len);
Mingming Cao617ba132006-10-11 01:20:53 -0700654 memset(val + size - EXT4_XATTR_PAD, 0,
655 EXT4_XATTR_PAD); /* Clear pad bytes. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700656 memcpy(val, i->value, i->value_len);
657 return 0;
658 }
659
660 /* Remove the old value. */
661 memmove(first_val + size, first_val, val - first_val);
662 memset(first_val, 0, size);
663 s->here->e_value_size = 0;
664 s->here->e_value_offs = 0;
665 min_offs += size;
666
667 /* Adjust all value offsets. */
668 last = s->first;
669 while (!IS_LAST_ENTRY(last)) {
670 size_t o = le16_to_cpu(last->e_value_offs);
671 if (!last->e_value_block &&
672 last->e_value_size && o < offs)
673 last->e_value_offs =
674 cpu_to_le16(o + size);
Mingming Cao617ba132006-10-11 01:20:53 -0700675 last = EXT4_XATTR_NEXT(last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 }
677 }
678 if (!i->value) {
679 /* Remove the old name. */
Mingming Cao617ba132006-10-11 01:20:53 -0700680 size_t size = EXT4_XATTR_LEN(name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 last = ENTRY((void *)last - size);
682 memmove(s->here, (void *)s->here + size,
683 (void *)last - (void *)s->here + sizeof(__u32));
684 memset(last, 0, size);
685 }
686 }
687
688 if (i->value) {
689 /* Insert the new value. */
690 s->here->e_value_size = cpu_to_le32(i->value_len);
691 if (i->value_len) {
Mingming Cao617ba132006-10-11 01:20:53 -0700692 size_t size = EXT4_XATTR_SIZE(i->value_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 void *val = s->base + min_offs - size;
694 s->here->e_value_offs = cpu_to_le16(min_offs - size);
Mingming Cao617ba132006-10-11 01:20:53 -0700695 memset(val + size - EXT4_XATTR_PAD, 0,
696 EXT4_XATTR_PAD); /* Clear the pad bytes. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 memcpy(val, i->value, i->value_len);
698 }
699 }
700 return 0;
701}
702
Mingming Cao617ba132006-10-11 01:20:53 -0700703struct ext4_xattr_block_find {
704 struct ext4_xattr_search s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 struct buffer_head *bh;
706};
707
708static int
Mingming Cao617ba132006-10-11 01:20:53 -0700709ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
710 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711{
712 struct super_block *sb = inode->i_sb;
713 int error;
714
715 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
716 i->name_index, i->name, i->value, (long)i->value_len);
717
Mingming Cao617ba132006-10-11 01:20:53 -0700718 if (EXT4_I(inode)->i_file_acl) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 /* The inode already has an extended attribute block. */
Mingming Cao617ba132006-10-11 01:20:53 -0700720 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 error = -EIO;
722 if (!bs->bh)
723 goto cleanup;
724 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
725 atomic_read(&(bs->bh->b_count)),
726 le32_to_cpu(BHDR(bs->bh)->h_refcount));
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400727 if (ext4_xattr_check_block(inode, bs->bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400728 EXT4_ERROR_INODE(inode, "bad block %llu",
729 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 error = -EIO;
731 goto cleanup;
732 }
733 /* Find the named attribute. */
734 bs->s.base = BHDR(bs->bh);
735 bs->s.first = BFIRST(bs->bh);
736 bs->s.end = bs->bh->b_data + bs->bh->b_size;
737 bs->s.here = bs->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700738 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 i->name, bs->bh->b_size, 1);
740 if (error && error != -ENODATA)
741 goto cleanup;
742 bs->s.not_found = error;
743 }
744 error = 0;
745
746cleanup:
747 return error;
748}
749
750static int
Mingming Cao617ba132006-10-11 01:20:53 -0700751ext4_xattr_block_set(handle_t *handle, struct inode *inode,
752 struct ext4_xattr_info *i,
753 struct ext4_xattr_block_find *bs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754{
755 struct super_block *sb = inode->i_sb;
756 struct buffer_head *new_bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700757 struct ext4_xattr_search *s = &bs->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800759 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760
Mingming Cao617ba132006-10-11 01:20:53 -0700761#define header(x) ((struct ext4_xattr_header *)(x))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762
763 if (i->value && i->value_len > sb->s_blocksize)
764 return -ENOSPC;
765 if (s->base) {
Mingming Cao617ba132006-10-11 01:20:53 -0700766 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 bs->bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800768 error = ext4_journal_get_write_access(handle, bs->bh);
769 if (error)
770 goto cleanup;
771 lock_buffer(bs->bh);
772
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
774 if (ce) {
775 mb_cache_entry_free(ce);
776 ce = NULL;
777 }
778 ea_bdebug(bs->bh, "modifying in-place");
Mingming Cao617ba132006-10-11 01:20:53 -0700779 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 if (!error) {
781 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700782 ext4_xattr_rehash(header(s->base),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 s->here);
Mingming Cao617ba132006-10-11 01:20:53 -0700784 ext4_xattr_cache_insert(bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 }
786 unlock_buffer(bs->bh);
787 if (error == -EIO)
788 goto bad_block;
789 if (!error)
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400790 error = ext4_handle_dirty_xattr_block(handle,
791 inode,
792 bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 if (error)
794 goto cleanup;
795 goto inserted;
796 } else {
797 int offset = (char *)s->here - bs->bh->b_data;
798
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800799 unlock_buffer(bs->bh);
Amir Goldstein537a0312011-03-20 22:57:02 -0400800 ext4_handle_release_buffer(handle, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 if (ce) {
802 mb_cache_entry_release(ce);
803 ce = NULL;
804 }
805 ea_bdebug(bs->bh, "cloning");
Josef Bacik216553c2008-04-29 22:02:02 -0400806 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 error = -ENOMEM;
808 if (s->base == NULL)
809 goto cleanup;
810 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
811 s->first = ENTRY(header(s->base)+1);
812 header(s->base)->h_refcount = cpu_to_le32(1);
813 s->here = ENTRY(s->base + offset);
814 s->end = s->base + bs->bh->b_size;
815 }
816 } else {
817 /* Allocate a buffer where we construct the new block. */
Josef Bacik216553c2008-04-29 22:02:02 -0400818 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 /* assert(header == s->base) */
820 error = -ENOMEM;
821 if (s->base == NULL)
822 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700823 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 header(s->base)->h_blocks = cpu_to_le32(1);
825 header(s->base)->h_refcount = cpu_to_le32(1);
826 s->first = ENTRY(header(s->base)+1);
827 s->here = ENTRY(header(s->base)+1);
828 s->end = s->base + sb->s_blocksize;
829 }
830
Mingming Cao617ba132006-10-11 01:20:53 -0700831 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832 if (error == -EIO)
833 goto bad_block;
834 if (error)
835 goto cleanup;
836 if (!IS_LAST_ENTRY(s->first))
Mingming Cao617ba132006-10-11 01:20:53 -0700837 ext4_xattr_rehash(header(s->base), s->here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838
839inserted:
840 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700841 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 if (new_bh) {
843 /* We found an identical block in the cache. */
844 if (new_bh == bs->bh)
845 ea_bdebug(new_bh, "keeping");
846 else {
847 /* The old block is released after updating
848 the inode. */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500849 error = dquot_alloc_block(inode, 1);
850 if (error)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -0700852 error = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 new_bh);
854 if (error)
855 goto cleanup_dquot;
856 lock_buffer(new_bh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400857 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 ea_bdebug(new_bh, "reusing; refcount now=%d",
859 le32_to_cpu(BHDR(new_bh)->h_refcount));
860 unlock_buffer(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400861 error = ext4_handle_dirty_xattr_block(handle,
862 inode,
863 new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864 if (error)
865 goto cleanup_dquot;
866 }
867 mb_cache_entry_release(ce);
868 ce = NULL;
869 } else if (bs->bh && s->base == bs->bh->b_data) {
870 /* We were modifying this block in-place. */
871 ea_bdebug(bs->bh, "keeping this block");
872 new_bh = bs->bh;
873 get_bh(new_bh);
874 } else {
875 /* We need to allocate a new block */
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400876 ext4_fsblk_t goal, block;
877
878 goal = ext4_group_first_block_no(sb,
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400879 EXT4_I(inode)->i_block_group);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400880
881 /* non-extent files can't have physical blocks past 2^32 */
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 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
884
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400885 /*
886 * take i_data_sem because we will test
887 * i_delalloc_reserved_flag in ext4_mb_new_blocks
888 */
889 down_read((&EXT4_I(inode)->i_data_sem));
Allison Henderson55f020d2011-05-25 07:41:26 -0400890 block = ext4_new_meta_blocks(handle, inode, goal, 0,
891 NULL, &error);
Eric Sandeen6d6a43512011-10-29 10:15:35 -0400892 up_read((&EXT4_I(inode)->i_data_sem));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893 if (error)
894 goto cleanup;
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400895
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400896 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400897 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
898
Joe Perchesace36ad2012-03-19 23:11:43 -0400899 ea_idebug(inode, "creating block %llu",
900 (unsigned long long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901
902 new_bh = sb_getblk(sb, block);
903 if (!new_bh) {
904getblk_failed:
Peter Huewe7dc57612011-02-21 21:01:42 -0500905 ext4_free_blocks(handle, inode, NULL, block, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500906 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 error = -EIO;
908 goto cleanup;
909 }
910 lock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700911 error = ext4_journal_get_create_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 if (error) {
913 unlock_buffer(new_bh);
914 goto getblk_failed;
915 }
916 memcpy(new_bh->b_data, s->base, new_bh->b_size);
917 set_buffer_uptodate(new_bh);
918 unlock_buffer(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700919 ext4_xattr_cache_insert(new_bh);
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -0400920 error = ext4_handle_dirty_xattr_block(handle,
921 inode, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 if (error)
923 goto cleanup;
924 }
925 }
926
927 /* Update the inode. */
Mingming Cao617ba132006-10-11 01:20:53 -0700928 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929
930 /* Drop the previous xattr block. */
931 if (bs->bh && bs->bh != new_bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700932 ext4_xattr_release_block(handle, inode, bs->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 error = 0;
934
935cleanup:
936 if (ce)
937 mb_cache_entry_release(ce);
938 brelse(new_bh);
939 if (!(bs->bh && s->base == bs->bh->b_data))
940 kfree(s->base);
941
942 return error;
943
944cleanup_dquot:
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500945 dquot_free_block(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 goto cleanup;
947
948bad_block:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400949 EXT4_ERROR_INODE(inode, "bad block %llu",
950 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 goto cleanup;
952
953#undef header
954}
955
Mingming Cao617ba132006-10-11 01:20:53 -0700956struct ext4_xattr_ibody_find {
957 struct ext4_xattr_search s;
958 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959};
960
961static int
Mingming Cao617ba132006-10-11 01:20:53 -0700962ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
963 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964{
Mingming Cao617ba132006-10-11 01:20:53 -0700965 struct ext4_xattr_ibody_header *header;
966 struct ext4_inode *raw_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 int error;
968
Mingming Cao617ba132006-10-11 01:20:53 -0700969 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700971 raw_inode = ext4_raw_inode(&is->iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 header = IHDR(inode, raw_inode);
973 is->s.base = is->s.first = IFIRST(header);
974 is->s.here = is->s.first;
Mingming Cao617ba132006-10-11 01:20:53 -0700975 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500976 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700977 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 if (error)
979 return error;
980 /* Find the named attribute. */
Mingming Cao617ba132006-10-11 01:20:53 -0700981 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 i->name, is->s.end -
983 (void *)is->s.base, 0);
984 if (error && error != -ENODATA)
985 return error;
986 is->s.not_found = error;
987 }
988 return 0;
989}
990
991static int
Mingming Cao617ba132006-10-11 01:20:53 -0700992ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
993 struct ext4_xattr_info *i,
994 struct ext4_xattr_ibody_find *is)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995{
Mingming Cao617ba132006-10-11 01:20:53 -0700996 struct ext4_xattr_ibody_header *header;
997 struct ext4_xattr_search *s = &is->s;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 int error;
999
Mingming Cao617ba132006-10-11 01:20:53 -07001000 if (EXT4_I(inode)->i_extra_isize == 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 return -ENOSPC;
Mingming Cao617ba132006-10-11 01:20:53 -07001002 error = ext4_xattr_set_entry(i, s);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 if (error)
1004 return error;
Mingming Cao617ba132006-10-11 01:20:53 -07001005 header = IHDR(inode, ext4_raw_inode(&is->iloc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 if (!IS_LAST_ENTRY(s->first)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001007 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001008 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 } else {
1010 header->h_magic = cpu_to_le32(0);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001011 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 }
1013 return 0;
1014}
1015
1016/*
Mingming Cao617ba132006-10-11 01:20:53 -07001017 * ext4_xattr_set_handle()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 *
Wang Sheng-Hui6e9510b2011-01-10 12:10:30 -05001019 * Create, replace or remove an extended attribute for this inode. Value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020 * is NULL to remove an existing extended attribute, and non-NULL to
1021 * either replace an existing extended attribute, or create a new extended
1022 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1023 * specify that an extended attribute must exist and must not exist
1024 * previous to the call, respectively.
1025 *
1026 * Returns 0, or a negative error number on failure.
1027 */
1028int
Mingming Cao617ba132006-10-11 01:20:53 -07001029ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 const char *name, const void *value, size_t value_len,
1031 int flags)
1032{
Mingming Cao617ba132006-10-11 01:20:53 -07001033 struct ext4_xattr_info i = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 .name_index = name_index,
1035 .name = name,
1036 .value = value,
1037 .value_len = value_len,
1038
1039 };
Mingming Cao617ba132006-10-11 01:20:53 -07001040 struct ext4_xattr_ibody_find is = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 .s = { .not_found = -ENODATA, },
1042 };
Mingming Cao617ba132006-10-11 01:20:53 -07001043 struct ext4_xattr_block_find bs = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001044 .s = { .not_found = -ENODATA, },
1045 };
Kalpak Shah4d20c682008-10-08 23:21:54 -04001046 unsigned long no_expand;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 int error;
1048
1049 if (!name)
1050 return -EINVAL;
1051 if (strlen(name) > 255)
1052 return -ERANGE;
Mingming Cao617ba132006-10-11 01:20:53 -07001053 down_write(&EXT4_I(inode)->xattr_sem);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001054 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1055 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001056
Eric Sandeen66543612011-10-26 03:32:07 -04001057 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
Eric Sandeen86ebfd02009-11-15 15:30:52 -05001058 if (error)
1059 goto cleanup;
1060
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001061 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001062 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1063 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001064 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 }
1066
Mingming Cao617ba132006-10-11 01:20:53 -07001067 error = ext4_xattr_ibody_find(inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 if (error)
1069 goto cleanup;
1070 if (is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001071 error = ext4_xattr_block_find(inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 if (error)
1073 goto cleanup;
1074 if (is.s.not_found && bs.s.not_found) {
1075 error = -ENODATA;
1076 if (flags & XATTR_REPLACE)
1077 goto cleanup;
1078 error = 0;
1079 if (!value)
1080 goto cleanup;
1081 } else {
1082 error = -EEXIST;
1083 if (flags & XATTR_CREATE)
1084 goto cleanup;
1085 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 if (!value) {
1087 if (!is.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001088 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 else if (!bs.s.not_found)
Mingming Cao617ba132006-10-11 01:20:53 -07001090 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001092 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 if (!error && !bs.s.not_found) {
1094 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001095 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001097 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1098 error = ext4_xattr_block_find(inode, &i, &bs);
1099 if (error)
1100 goto cleanup;
1101 }
Mingming Cao617ba132006-10-11 01:20:53 -07001102 error = ext4_xattr_block_set(handle, inode, &i, &bs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001103 if (error)
1104 goto cleanup;
1105 if (!is.s.not_found) {
1106 i.value = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001107 error = ext4_xattr_ibody_set(handle, inode, &i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 &is);
1109 }
1110 }
1111 }
1112 if (!error) {
Mingming Cao617ba132006-10-11 01:20:53 -07001113 ext4_xattr_update_super_block(handle, inode->i_sb);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001114 inode->i_ctime = ext4_current_time(inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001115 if (!value)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001116 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001117 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001119 * The bh is consumed by ext4_mark_iloc_dirty, even with
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 * error != 0.
1121 */
1122 is.iloc.bh = NULL;
1123 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001124 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 }
1126
1127cleanup:
1128 brelse(is.iloc.bh);
1129 brelse(bs.bh);
Kalpak Shah4d20c682008-10-08 23:21:54 -04001130 if (no_expand == 0)
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001131 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
Mingming Cao617ba132006-10-11 01:20:53 -07001132 up_write(&EXT4_I(inode)->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 return error;
1134}
1135
1136/*
Mingming Cao617ba132006-10-11 01:20:53 -07001137 * ext4_xattr_set()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 *
Mingming Cao617ba132006-10-11 01:20:53 -07001139 * Like ext4_xattr_set_handle, but start from an inode. This extended
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 * attribute modification is a filesystem transaction by itself.
1141 *
1142 * Returns 0, or a negative error number on failure.
1143 */
1144int
Mingming Cao617ba132006-10-11 01:20:53 -07001145ext4_xattr_set(struct inode *inode, int name_index, const char *name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 const void *value, size_t value_len, int flags)
1147{
1148 handle_t *handle;
1149 int error, retries = 0;
1150
1151retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001152 handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153 if (IS_ERR(handle)) {
1154 error = PTR_ERR(handle);
1155 } else {
1156 int error2;
1157
Mingming Cao617ba132006-10-11 01:20:53 -07001158 error = ext4_xattr_set_handle(handle, inode, name_index, name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 value, value_len, flags);
Mingming Cao617ba132006-10-11 01:20:53 -07001160 error2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 if (error == -ENOSPC &&
Mingming Cao617ba132006-10-11 01:20:53 -07001162 ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 goto retry;
1164 if (error == 0)
1165 error = error2;
1166 }
1167
1168 return error;
1169}
1170
1171/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001172 * Shift the EA entries in the inode to create space for the increased
1173 * i_extra_isize.
1174 */
1175static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1176 int value_offs_shift, void *to,
1177 void *from, size_t n, int blocksize)
1178{
1179 struct ext4_xattr_entry *last = entry;
1180 int new_offs;
1181
1182 /* Adjust the value offsets of the entries */
1183 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1184 if (!last->e_value_block && last->e_value_size) {
1185 new_offs = le16_to_cpu(last->e_value_offs) +
1186 value_offs_shift;
1187 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1188 > blocksize);
1189 last->e_value_offs = cpu_to_le16(new_offs);
1190 }
1191 }
1192 /* Shift the entries by n bytes */
1193 memmove(to, from, n);
1194}
1195
1196/*
1197 * Expand an inode by new_extra_isize bytes when EAs are present.
1198 * Returns 0 on success or negative error number on failure.
1199 */
1200int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1201 struct ext4_inode *raw_inode, handle_t *handle)
1202{
1203 struct ext4_xattr_ibody_header *header;
1204 struct ext4_xattr_entry *entry, *last, *first;
1205 struct buffer_head *bh = NULL;
1206 struct ext4_xattr_ibody_find *is = NULL;
1207 struct ext4_xattr_block_find *bs = NULL;
1208 char *buffer = NULL, *b_entry_name = NULL;
1209 size_t min_offs, free;
1210 int total_ino, total_blk;
1211 void *base, *start, *end;
1212 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001213 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 -04001214
1215 down_write(&EXT4_I(inode)->xattr_sem);
1216retry:
1217 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1218 up_write(&EXT4_I(inode)->xattr_sem);
1219 return 0;
1220 }
1221
1222 header = IHDR(inode, raw_inode);
1223 entry = IFIRST(header);
1224
1225 /*
1226 * Check if enough free space is available in the inode to shift the
1227 * entries ahead by new_extra_isize.
1228 */
1229
1230 base = start = entry;
1231 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1232 min_offs = end - base;
1233 last = entry;
1234 total_ino = sizeof(struct ext4_xattr_ibody_header);
1235
1236 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1237 if (free >= new_extra_isize) {
1238 entry = IFIRST(header);
1239 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1240 - new_extra_isize, (void *)raw_inode +
1241 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1242 (void *)header, total_ino,
1243 inode->i_sb->s_blocksize);
1244 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1245 error = 0;
1246 goto cleanup;
1247 }
1248
1249 /*
1250 * Enough free space isn't available in the inode, check if
1251 * EA block can hold new_extra_isize bytes.
1252 */
1253 if (EXT4_I(inode)->i_file_acl) {
1254 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1255 error = -EIO;
1256 if (!bh)
1257 goto cleanup;
Darrick J. Wongcc8e94f2012-04-29 18:43:10 -04001258 if (ext4_xattr_check_block(inode, bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001259 EXT4_ERROR_INODE(inode, "bad block %llu",
1260 EXT4_I(inode)->i_file_acl);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001261 error = -EIO;
1262 goto cleanup;
1263 }
1264 base = BHDR(bh);
1265 first = BFIRST(bh);
1266 end = bh->b_data + bh->b_size;
1267 min_offs = end - base;
1268 free = ext4_xattr_free_space(first, &min_offs, base,
1269 &total_blk);
1270 if (free < new_extra_isize) {
1271 if (!tried_min_extra_isize && s_min_extra_isize) {
1272 tried_min_extra_isize++;
1273 new_extra_isize = s_min_extra_isize;
1274 brelse(bh);
1275 goto retry;
1276 }
1277 error = -1;
1278 goto cleanup;
1279 }
1280 } else {
1281 free = inode->i_sb->s_blocksize;
1282 }
1283
1284 while (new_extra_isize > 0) {
1285 size_t offs, size, entry_size;
1286 struct ext4_xattr_entry *small_entry = NULL;
1287 struct ext4_xattr_info i = {
1288 .value = NULL,
1289 .value_len = 0,
1290 };
1291 unsigned int total_size; /* EA entry size + value size */
1292 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1293 unsigned int min_total_size = ~0U;
1294
1295 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1296 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1297 if (!is || !bs) {
1298 error = -ENOMEM;
1299 goto cleanup;
1300 }
1301
1302 is->s.not_found = -ENODATA;
1303 bs->s.not_found = -ENODATA;
1304 is->iloc.bh = NULL;
1305 bs->bh = NULL;
1306
1307 last = IFIRST(header);
1308 /* Find the entry best suited to be pushed into EA block */
1309 entry = NULL;
1310 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1311 total_size =
1312 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1313 EXT4_XATTR_LEN(last->e_name_len);
1314 if (total_size <= free && total_size < min_total_size) {
1315 if (total_size < new_extra_isize) {
1316 small_entry = last;
1317 } else {
1318 entry = last;
1319 min_total_size = total_size;
1320 }
1321 }
1322 }
1323
1324 if (entry == NULL) {
1325 if (small_entry) {
1326 entry = small_entry;
1327 } else {
1328 if (!tried_min_extra_isize &&
1329 s_min_extra_isize) {
1330 tried_min_extra_isize++;
1331 new_extra_isize = s_min_extra_isize;
1332 goto retry;
1333 }
1334 error = -1;
1335 goto cleanup;
1336 }
1337 }
1338 offs = le16_to_cpu(entry->e_value_offs);
1339 size = le32_to_cpu(entry->e_value_size);
1340 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1341 i.name_index = entry->e_name_index,
1342 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1343 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1344 if (!buffer || !b_entry_name) {
1345 error = -ENOMEM;
1346 goto cleanup;
1347 }
1348 /* Save the entry name and the entry value */
1349 memcpy(buffer, (void *)IFIRST(header) + offs,
1350 EXT4_XATTR_SIZE(size));
1351 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1352 b_entry_name[entry->e_name_len] = '\0';
1353 i.name = b_entry_name;
1354
1355 error = ext4_get_inode_loc(inode, &is->iloc);
1356 if (error)
1357 goto cleanup;
1358
1359 error = ext4_xattr_ibody_find(inode, &i, is);
1360 if (error)
1361 goto cleanup;
1362
1363 /* Remove the chosen entry from the inode */
1364 error = ext4_xattr_ibody_set(handle, inode, &i, is);
Roel Kluin9aaab052010-02-15 14:26:16 -05001365 if (error)
1366 goto cleanup;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001367
1368 entry = IFIRST(header);
1369 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1370 shift_bytes = new_extra_isize;
1371 else
1372 shift_bytes = entry_size + size;
1373 /* Adjust the offsets and shift the remaining entries ahead */
1374 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1375 shift_bytes, (void *)raw_inode +
1376 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1377 (void *)header, total_ino - entry_size,
1378 inode->i_sb->s_blocksize);
1379
1380 extra_isize += shift_bytes;
1381 new_extra_isize -= shift_bytes;
1382 EXT4_I(inode)->i_extra_isize = extra_isize;
1383
1384 i.name = b_entry_name;
1385 i.value = buffer;
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001386 i.value_len = size;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001387 error = ext4_xattr_block_find(inode, &i, bs);
1388 if (error)
1389 goto cleanup;
1390
1391 /* Add entry which was removed from the inode into the block */
1392 error = ext4_xattr_block_set(handle, inode, &i, bs);
1393 if (error)
1394 goto cleanup;
1395 kfree(b_entry_name);
1396 kfree(buffer);
Julia Lawalld3533d72009-12-23 07:52:31 -05001397 b_entry_name = NULL;
1398 buffer = NULL;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04001399 brelse(is->iloc.bh);
1400 kfree(is);
1401 kfree(bs);
1402 }
1403 brelse(bh);
1404 up_write(&EXT4_I(inode)->xattr_sem);
1405 return 0;
1406
1407cleanup:
1408 kfree(b_entry_name);
1409 kfree(buffer);
1410 if (is)
1411 brelse(is->iloc.bh);
1412 kfree(is);
1413 kfree(bs);
1414 brelse(bh);
1415 up_write(&EXT4_I(inode)->xattr_sem);
1416 return error;
1417}
1418
1419
1420
1421/*
Mingming Cao617ba132006-10-11 01:20:53 -07001422 * ext4_xattr_delete_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423 *
1424 * Free extended attribute resources associated with this inode. This
1425 * is called immediately before an inode is freed. We have exclusive
1426 * access to the inode.
1427 */
1428void
Mingming Cao617ba132006-10-11 01:20:53 -07001429ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001430{
1431 struct buffer_head *bh = NULL;
1432
Mingming Cao617ba132006-10-11 01:20:53 -07001433 if (!EXT4_I(inode)->i_file_acl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434 goto cleanup;
Mingming Cao617ba132006-10-11 01:20:53 -07001435 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001437 EXT4_ERROR_INODE(inode, "block %llu read error",
1438 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 goto cleanup;
1440 }
Mingming Cao617ba132006-10-11 01:20:53 -07001441 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001443 EXT4_ERROR_INODE(inode, "bad block %llu",
1444 EXT4_I(inode)->i_file_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445 goto cleanup;
1446 }
Mingming Cao617ba132006-10-11 01:20:53 -07001447 ext4_xattr_release_block(handle, inode, bh);
1448 EXT4_I(inode)->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449
1450cleanup:
1451 brelse(bh);
1452}
1453
1454/*
Mingming Cao617ba132006-10-11 01:20:53 -07001455 * ext4_xattr_put_super()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 *
1457 * This is called when a file system is unmounted.
1458 */
1459void
Mingming Cao617ba132006-10-11 01:20:53 -07001460ext4_xattr_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461{
1462 mb_cache_shrink(sb->s_bdev);
1463}
1464
1465/*
Mingming Cao617ba132006-10-11 01:20:53 -07001466 * ext4_xattr_cache_insert()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467 *
1468 * Create a new entry in the extended attribute cache, and insert
1469 * it unless such an entry is already in the cache.
1470 *
1471 * Returns 0, or a negative error number on failure.
1472 */
1473static void
Mingming Cao617ba132006-10-11 01:20:53 -07001474ext4_xattr_cache_insert(struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001475{
1476 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1477 struct mb_cache_entry *ce;
1478 int error;
1479
Jan Kara335e92e2008-04-15 14:34:43 -07001480 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001481 if (!ce) {
1482 ea_bdebug(bh, "out of memory");
1483 return;
1484 }
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001485 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486 if (error) {
1487 mb_cache_entry_free(ce);
1488 if (error == -EBUSY) {
1489 ea_bdebug(bh, "already in cache");
1490 error = 0;
1491 }
1492 } else {
1493 ea_bdebug(bh, "inserting [%x]", (int)hash);
1494 mb_cache_entry_release(ce);
1495 }
1496}
1497
1498/*
Mingming Cao617ba132006-10-11 01:20:53 -07001499 * ext4_xattr_cmp()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500 *
1501 * Compare two extended attribute blocks for equality.
1502 *
1503 * Returns 0 if the blocks are equal, 1 if they differ, and
1504 * a negative error number on errors.
1505 */
1506static int
Mingming Cao617ba132006-10-11 01:20:53 -07001507ext4_xattr_cmp(struct ext4_xattr_header *header1,
1508 struct ext4_xattr_header *header2)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509{
Mingming Cao617ba132006-10-11 01:20:53 -07001510 struct ext4_xattr_entry *entry1, *entry2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511
1512 entry1 = ENTRY(header1+1);
1513 entry2 = ENTRY(header2+1);
1514 while (!IS_LAST_ENTRY(entry1)) {
1515 if (IS_LAST_ENTRY(entry2))
1516 return 1;
1517 if (entry1->e_hash != entry2->e_hash ||
1518 entry1->e_name_index != entry2->e_name_index ||
1519 entry1->e_name_len != entry2->e_name_len ||
1520 entry1->e_value_size != entry2->e_value_size ||
1521 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1522 return 1;
1523 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1524 return -EIO;
1525 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1526 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1527 le32_to_cpu(entry1->e_value_size)))
1528 return 1;
1529
Mingming Cao617ba132006-10-11 01:20:53 -07001530 entry1 = EXT4_XATTR_NEXT(entry1);
1531 entry2 = EXT4_XATTR_NEXT(entry2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532 }
1533 if (!IS_LAST_ENTRY(entry2))
1534 return 1;
1535 return 0;
1536}
1537
1538/*
Mingming Cao617ba132006-10-11 01:20:53 -07001539 * ext4_xattr_cache_find()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 *
1541 * Find an identical extended attribute block.
1542 *
1543 * Returns a pointer to the block found, or NULL if such a block was
1544 * not found or an error occurred.
1545 */
1546static struct buffer_head *
Mingming Cao617ba132006-10-11 01:20:53 -07001547ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001548 struct mb_cache_entry **pce)
1549{
1550 __u32 hash = le32_to_cpu(header->h_hash);
1551 struct mb_cache_entry *ce;
1552
1553 if (!header->h_hash)
1554 return NULL; /* never share */
1555 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1556again:
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001557 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1558 hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001559 while (ce) {
1560 struct buffer_head *bh;
1561
1562 if (IS_ERR(ce)) {
1563 if (PTR_ERR(ce) == -EAGAIN)
1564 goto again;
1565 break;
1566 }
1567 bh = sb_bread(inode->i_sb, ce->e_block);
1568 if (!bh) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001569 EXT4_ERROR_INODE(inode, "block %lu read error",
1570 (unsigned long) ce->e_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
Mingming Cao617ba132006-10-11 01:20:53 -07001572 EXT4_XATTR_REFCOUNT_MAX) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 ea_idebug(inode, "block %lu refcount %d>=%d",
1574 (unsigned long) ce->e_block,
1575 le32_to_cpu(BHDR(bh)->h_refcount),
Mingming Cao617ba132006-10-11 01:20:53 -07001576 EXT4_XATTR_REFCOUNT_MAX);
1577 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 *pce = ce;
1579 return bh;
1580 }
1581 brelse(bh);
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001582 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 }
1584 return NULL;
1585}
1586
1587#define NAME_HASH_SHIFT 5
1588#define VALUE_HASH_SHIFT 16
1589
1590/*
Mingming Cao617ba132006-10-11 01:20:53 -07001591 * ext4_xattr_hash_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592 *
1593 * Compute the hash of an extended attribute.
1594 */
Mingming Cao617ba132006-10-11 01:20:53 -07001595static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1596 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597{
1598 __u32 hash = 0;
1599 char *name = entry->e_name;
1600 int n;
1601
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001602 for (n = 0; n < entry->e_name_len; n++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 hash = (hash << NAME_HASH_SHIFT) ^
1604 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1605 *name++;
1606 }
1607
1608 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1609 __le32 *value = (__le32 *)((char *)header +
1610 le16_to_cpu(entry->e_value_offs));
1611 for (n = (le32_to_cpu(entry->e_value_size) +
Mingming Cao617ba132006-10-11 01:20:53 -07001612 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 hash = (hash << VALUE_HASH_SHIFT) ^
1614 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1615 le32_to_cpu(*value++);
1616 }
1617 }
1618 entry->e_hash = cpu_to_le32(hash);
1619}
1620
1621#undef NAME_HASH_SHIFT
1622#undef VALUE_HASH_SHIFT
1623
1624#define BLOCK_HASH_SHIFT 16
1625
1626/*
Mingming Cao617ba132006-10-11 01:20:53 -07001627 * ext4_xattr_rehash()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628 *
1629 * Re-compute the extended attribute hash value after an entry has changed.
1630 */
Mingming Cao617ba132006-10-11 01:20:53 -07001631static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1632 struct ext4_xattr_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001633{
Mingming Cao617ba132006-10-11 01:20:53 -07001634 struct ext4_xattr_entry *here;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001635 __u32 hash = 0;
1636
Mingming Cao617ba132006-10-11 01:20:53 -07001637 ext4_xattr_hash_entry(header, entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638 here = ENTRY(header+1);
1639 while (!IS_LAST_ENTRY(here)) {
1640 if (!here->e_hash) {
1641 /* Block is not shared if an entry's hash value == 0 */
1642 hash = 0;
1643 break;
1644 }
1645 hash = (hash << BLOCK_HASH_SHIFT) ^
1646 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1647 le32_to_cpu(here->e_hash);
Mingming Cao617ba132006-10-11 01:20:53 -07001648 here = EXT4_XATTR_NEXT(here);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 }
1650 header->h_hash = cpu_to_le32(hash);
1651}
1652
1653#undef BLOCK_HASH_SHIFT
1654
1655int __init
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04001656ext4_init_xattr(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657{
Andreas Gruenbacher2aec7c52010-07-19 18:19:41 +02001658 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
Mingming Cao617ba132006-10-11 01:20:53 -07001659 if (!ext4_xattr_cache)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001660 return -ENOMEM;
1661 return 0;
1662}
1663
1664void
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04001665ext4_exit_xattr(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666{
Mingming Cao617ba132006-10-11 01:20:53 -07001667 if (ext4_xattr_cache)
1668 mb_cache_destroy(ext4_xattr_cache);
1669 ext4_xattr_cache = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001670}