ext4: ensure Inode flags consistency are checked at build time
[deliverable/linux.git] / fs / ext4 / xattr.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/xattr.c
ac27a0ec
DK
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
617ba13b 7 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
ac27a0ec
DK
8 * 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 * ----------------
617ba13b 46 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
ac27a0ec
DK
47 * 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>
ac27a0ec
DK
56#include <linux/mbcache.h>
57#include <linux/quotaops.h>
58#include <linux/rwsem.h>
3dcf5451
CH
59#include "ext4_jbd2.h"
60#include "ext4.h"
ac27a0ec
DK
61#include "xattr.h"
62#include "acl.h"
63
617ba13b 64#ifdef EXT4_XATTR_DEBUG
ac27a0ec
DK
65# 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
ace36ad4
JP
80# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
81# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
ac27a0ec
DK
82#endif
83
617ba13b
MC
84static void ext4_xattr_cache_insert(struct buffer_head *);
85static struct buffer_head *ext4_xattr_cache_find(struct inode *,
86 struct ext4_xattr_header *,
ac27a0ec 87 struct mb_cache_entry **);
617ba13b
MC
88static void ext4_xattr_rehash(struct ext4_xattr_header *,
89 struct ext4_xattr_entry *);
431547b3 90static int ext4_xattr_list(struct dentry *dentry, char *buffer,
d3a95d47 91 size_t buffer_size);
ac27a0ec 92
617ba13b 93static struct mb_cache *ext4_xattr_cache;
ac27a0ec 94
11e27528 95static const struct xattr_handler *ext4_xattr_handler_map[] = {
617ba13b 96 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
03010a33 97#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b
MC
98 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
99 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
ac27a0ec 100#endif
617ba13b 101 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
03010a33 102#ifdef CONFIG_EXT4_FS_SECURITY
617ba13b 103 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
ac27a0ec
DK
104#endif
105};
106
11e27528 107const struct xattr_handler *ext4_xattr_handlers[] = {
617ba13b
MC
108 &ext4_xattr_user_handler,
109 &ext4_xattr_trusted_handler,
03010a33 110#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b
MC
111 &ext4_xattr_acl_access_handler,
112 &ext4_xattr_acl_default_handler,
ac27a0ec 113#endif
03010a33 114#ifdef CONFIG_EXT4_FS_SECURITY
617ba13b 115 &ext4_xattr_security_handler,
ac27a0ec
DK
116#endif
117 NULL
118};
119
cc8e94fd
DW
120static __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);
cc8e94fd
DW
125 __u32 csum, old;
126
127 old = hdr->h_checksum;
128 hdr->h_checksum = 0;
41eb70dd
TM
129 block_nr = cpu_to_le64(block_nr);
130 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&block_nr,
131 sizeof(block_nr));
cc8e94fd
DW
132 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
133 EXT4_BLOCK_SIZE(inode->i_sb));
41eb70dd 134
cc8e94fd
DW
135 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
11e27528 169static inline const struct xattr_handler *
617ba13b 170ext4_xattr_handler(int name_index)
ac27a0ec 171{
11e27528 172 const struct xattr_handler *handler = NULL;
ac27a0ec 173
617ba13b
MC
174 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
175 handler = ext4_xattr_handler_map[name_index];
ac27a0ec
DK
176 return handler;
177}
178
179/*
180 * Inode operation listxattr()
181 *
182 * dentry->d_inode->i_mutex: don't care
183 */
184ssize_t
617ba13b 185ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
ac27a0ec 186{
431547b3 187 return ext4_xattr_list(dentry, buffer, size);
ac27a0ec
DK
188}
189
190static int
617ba13b 191ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
ac27a0ec
DK
192{
193 while (!IS_LAST_ENTRY(entry)) {
617ba13b 194 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
ac27a0ec
DK
195 if ((void *)next >= end)
196 return -EIO;
197 entry = next;
198 }
199 return 0;
200}
201
202static inline int
cc8e94fd 203ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
ac27a0ec 204{
cc8e94fd
DW
205 int error;
206
207 if (buffer_verified(bh))
208 return 0;
209
617ba13b 210 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
ac27a0ec
DK
211 BHDR(bh)->h_blocks != cpu_to_le32(1))
212 return -EIO;
cc8e94fd
DW
213 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;
ac27a0ec
DK
219}
220
221static inline int
617ba13b 222ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
ac27a0ec
DK
223{
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
617ba13b 233ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
ac27a0ec
DK
234 const char *name, size_t size, int sorted)
235{
617ba13b 236 struct ext4_xattr_entry *entry;
ac27a0ec
DK
237 size_t name_len;
238 int cmp = 1;
239
240 if (name == NULL)
241 return -EINVAL;
242 name_len = strlen(name);
243 entry = *pentry;
617ba13b 244 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
ac27a0ec
DK
245 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;
617ba13b 254 if (!cmp && ext4_xattr_check_entry(entry, size))
ac27a0ec
DK
255 return -EIO;
256 return cmp ? -ENODATA : 0;
257}
258
259static int
617ba13b 260ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
261 void *buffer, size_t buffer_size)
262{
263 struct buffer_head *bh = NULL;
617ba13b 264 struct ext4_xattr_entry *entry;
ac27a0ec
DK
265 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;
617ba13b 272 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 273 goto cleanup;
ace36ad4
JP
274 ea_idebug(inode, "reading block %llu",
275 (unsigned long long)EXT4_I(inode)->i_file_acl);
617ba13b 276 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
277 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));
cc8e94fd 281 if (ext4_xattr_check_block(inode, bh)) {
12062ddd 282bad_block:
24676da4
TT
283 EXT4_ERROR_INODE(inode, "bad block %llu",
284 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
285 error = -EIO;
286 goto cleanup;
287 }
617ba13b 288 ext4_xattr_cache_insert(bh);
ac27a0ec 289 entry = BFIRST(bh);
617ba13b 290 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
ac27a0ec
DK
291 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
879b3825 310int
617ba13b 311ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
312 void *buffer, size_t buffer_size)
313{
617ba13b
MC
314 struct ext4_xattr_ibody_header *header;
315 struct ext4_xattr_entry *entry;
316 struct ext4_inode *raw_inode;
317 struct ext4_iloc iloc;
ac27a0ec
DK
318 size_t size;
319 void *end;
320 int error;
321
19f5fb7a 322 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
ac27a0ec 323 return -ENODATA;
617ba13b 324 error = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
325 if (error)
326 return error;
617ba13b 327 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec
DK
328 header = IHDR(inode, raw_inode);
329 entry = IFIRST(header);
617ba13b
MC
330 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
331 error = ext4_xattr_check_names(entry, end);
ac27a0ec
DK
332 if (error)
333 goto cleanup;
617ba13b 334 error = ext4_xattr_find_entry(&entry, name_index, name,
ac27a0ec
DK
335 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/*
617ba13b 354 * ext4_xattr_get()
ac27a0ec
DK
355 *
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
617ba13b 364ext4_xattr_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
365 void *buffer, size_t buffer_size)
366{
367 int error;
368
617ba13b
MC
369 down_read(&EXT4_I(inode)->xattr_sem);
370 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
ac27a0ec
DK
371 buffer_size);
372 if (error == -ENODATA)
617ba13b 373 error = ext4_xattr_block_get(inode, name_index, name, buffer,
ac27a0ec 374 buffer_size);
617ba13b 375 up_read(&EXT4_I(inode)->xattr_sem);
ac27a0ec
DK
376 return error;
377}
378
379static int
431547b3 380ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
ac27a0ec
DK
381 char *buffer, size_t buffer_size)
382{
383 size_t rest = buffer_size;
384
617ba13b 385 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
11e27528 386 const struct xattr_handler *handler =
617ba13b 387 ext4_xattr_handler(entry->e_name_index);
ac27a0ec
DK
388
389 if (handler) {
431547b3 390 size_t size = handler->list(dentry, buffer, rest,
ac27a0ec 391 entry->e_name,
431547b3
CH
392 entry->e_name_len,
393 handler->flags);
ac27a0ec
DK
394 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
431547b3 406ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 407{
431547b3 408 struct inode *inode = dentry->d_inode;
ac27a0ec
DK
409 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;
617ba13b 416 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 417 goto cleanup;
ace36ad4
JP
418 ea_idebug(inode, "reading block %llu",
419 (unsigned long long)EXT4_I(inode)->i_file_acl);
617ba13b 420 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
421 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));
cc8e94fd 426 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
427 EXT4_ERROR_INODE(inode, "bad block %llu",
428 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
429 error = -EIO;
430 goto cleanup;
431 }
617ba13b 432 ext4_xattr_cache_insert(bh);
431547b3 433 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
ac27a0ec
DK
434
435cleanup:
436 brelse(bh);
437
438 return error;
439}
440
441static int
431547b3 442ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 443{
431547b3 444 struct inode *inode = dentry->d_inode;
617ba13b
MC
445 struct ext4_xattr_ibody_header *header;
446 struct ext4_inode *raw_inode;
447 struct ext4_iloc iloc;
ac27a0ec
DK
448 void *end;
449 int error;
450
19f5fb7a 451 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
ac27a0ec 452 return 0;
617ba13b 453 error = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
454 if (error)
455 return error;
617ba13b 456 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec 457 header = IHDR(inode, raw_inode);
617ba13b
MC
458 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
459 error = ext4_xattr_check_names(IFIRST(header), end);
ac27a0ec
DK
460 if (error)
461 goto cleanup;
431547b3 462 error = ext4_xattr_list_entries(dentry, IFIRST(header),
ac27a0ec
DK
463 buffer, buffer_size);
464
465cleanup:
466 brelse(iloc.bh);
467 return error;
468}
469
470/*
617ba13b 471 * ext4_xattr_list()
ac27a0ec
DK
472 *
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 */
d3a95d47 480static int
431547b3 481ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 482{
eaeef867 483 int ret, ret2;
ac27a0ec 484
431547b3 485 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
eaeef867
TT
486 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;
ac27a0ec 492 }
eaeef867
TT
493 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
494 if (ret < 0)
495 goto errout;
496 ret += ret2;
497errout:
431547b3 498 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
eaeef867 499 return ret;
ac27a0ec
DK
500}
501
502/*
617ba13b 503 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
ac27a0ec
DK
504 * not set, set it.
505 */
617ba13b 506static void ext4_xattr_update_super_block(handle_t *handle,
ac27a0ec
DK
507 struct super_block *sb)
508{
617ba13b 509 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
ac27a0ec
DK
510 return;
511
617ba13b 512 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
ed2908f3 513 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
a0375156 514 ext4_handle_dirty_super(handle, sb);
ac27a0ec 515 }
ac27a0ec
DK
516}
517
518/*
519 * Release the xattr block BH: If the reference count is > 1, decrement
520 * it; otherwise free the block.
521 */
522static void
617ba13b 523ext4_xattr_release_block(handle_t *handle, struct inode *inode,
ac27a0ec
DK
524 struct buffer_head *bh)
525{
526 struct mb_cache_entry *ce = NULL;
8a2bfdcb 527 int error = 0;
ac27a0ec 528
617ba13b 529 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
8a2bfdcb
MC
530 error = ext4_journal_get_write_access(handle, bh);
531 if (error)
532 goto out;
533
534 lock_buffer(bh);
ac27a0ec
DK
535 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);
ac27a0ec 539 get_bh(bh);
e6362609
TT
540 ext4_free_blocks(handle, inode, bh, 0, 1,
541 EXT4_FREE_BLOCKS_METADATA |
542 EXT4_FREE_BLOCKS_FORGET);
c1bb05a6 543 unlock_buffer(bh);
ac27a0ec 544 } else {
e8546d06 545 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
c1bb05a6
ES
546 if (ce)
547 mb_cache_entry_release(ce);
548 unlock_buffer(bh);
cc8e94fd 549 error = ext4_handle_dirty_xattr_block(handle, inode, bh);
8a2bfdcb 550 if (IS_SYNC(inode))
0390131b 551 ext4_handle_sync(handle);
5dd4056d 552 dquot_free_block(inode, 1);
8a2bfdcb
MC
553 ea_bdebug(bh, "refcount now=%d; releasing",
554 le32_to_cpu(BHDR(bh)->h_refcount));
ac27a0ec 555 }
8a2bfdcb
MC
556out:
557 ext4_std_error(inode->i_sb, error);
558 return;
ac27a0ec
DK
559}
560
6dd4ee7c
KS
561/*
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
ac27a0ec 579static int
617ba13b 580ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
ac27a0ec 581{
617ba13b 582 struct ext4_xattr_entry *last;
ac27a0ec
DK
583 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;
617ba13b 587 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
ac27a0ec
DK
588 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);
617ba13b 598 free += EXT4_XATTR_SIZE(size);
ac27a0ec 599 }
617ba13b 600 free += EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
601 }
602 if (i->value) {
617ba13b
MC
603 if (free < EXT4_XATTR_SIZE(i->value_len) ||
604 free < EXT4_XATTR_LEN(name_len) +
605 EXT4_XATTR_SIZE(i->value_len))
ac27a0ec
DK
606 return -ENOSPC;
607 }
608
609 if (i->value && s->not_found) {
610 /* Insert the new name. */
617ba13b 611 size_t size = EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
612 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;
617ba13b 623 size_t size = EXT4_XATTR_SIZE(
ac27a0ec
DK
624 le32_to_cpu(s->here->e_value_size));
625
617ba13b 626 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
ac27a0ec
DK
627 /* 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);
617ba13b
MC
631 memset(val + size - EXT4_XATTR_PAD, 0,
632 EXT4_XATTR_PAD); /* Clear pad bytes. */
ac27a0ec
DK
633 memcpy(val, i->value, i->value_len);
634 return 0;
635 }
636
637 /* Remove the old value. */
638 memmove(first_val + size, first_val, val - first_val);
639 memset(first_val, 0, size);
640 s->here->e_value_size = 0;
641 s->here->e_value_offs = 0;
642 min_offs += size;
643
644 /* Adjust all value offsets. */
645 last = s->first;
646 while (!IS_LAST_ENTRY(last)) {
647 size_t o = le16_to_cpu(last->e_value_offs);
648 if (!last->e_value_block &&
649 last->e_value_size && o < offs)
650 last->e_value_offs =
651 cpu_to_le16(o + size);
617ba13b 652 last = EXT4_XATTR_NEXT(last);
ac27a0ec
DK
653 }
654 }
655 if (!i->value) {
656 /* Remove the old name. */
617ba13b 657 size_t size = EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
658 last = ENTRY((void *)last - size);
659 memmove(s->here, (void *)s->here + size,
660 (void *)last - (void *)s->here + sizeof(__u32));
661 memset(last, 0, size);
662 }
663 }
664
665 if (i->value) {
666 /* Insert the new value. */
667 s->here->e_value_size = cpu_to_le32(i->value_len);
668 if (i->value_len) {
617ba13b 669 size_t size = EXT4_XATTR_SIZE(i->value_len);
ac27a0ec
DK
670 void *val = s->base + min_offs - size;
671 s->here->e_value_offs = cpu_to_le16(min_offs - size);
617ba13b
MC
672 memset(val + size - EXT4_XATTR_PAD, 0,
673 EXT4_XATTR_PAD); /* Clear the pad bytes. */
ac27a0ec
DK
674 memcpy(val, i->value, i->value_len);
675 }
676 }
677 return 0;
678}
679
617ba13b
MC
680struct ext4_xattr_block_find {
681 struct ext4_xattr_search s;
ac27a0ec
DK
682 struct buffer_head *bh;
683};
684
685static int
617ba13b
MC
686ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
687 struct ext4_xattr_block_find *bs)
ac27a0ec
DK
688{
689 struct super_block *sb = inode->i_sb;
690 int error;
691
692 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
693 i->name_index, i->name, i->value, (long)i->value_len);
694
617ba13b 695 if (EXT4_I(inode)->i_file_acl) {
ac27a0ec 696 /* The inode already has an extended attribute block. */
617ba13b 697 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
698 error = -EIO;
699 if (!bs->bh)
700 goto cleanup;
701 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
702 atomic_read(&(bs->bh->b_count)),
703 le32_to_cpu(BHDR(bs->bh)->h_refcount));
cc8e94fd 704 if (ext4_xattr_check_block(inode, bs->bh)) {
24676da4
TT
705 EXT4_ERROR_INODE(inode, "bad block %llu",
706 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
707 error = -EIO;
708 goto cleanup;
709 }
710 /* Find the named attribute. */
711 bs->s.base = BHDR(bs->bh);
712 bs->s.first = BFIRST(bs->bh);
713 bs->s.end = bs->bh->b_data + bs->bh->b_size;
714 bs->s.here = bs->s.first;
617ba13b 715 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
ac27a0ec
DK
716 i->name, bs->bh->b_size, 1);
717 if (error && error != -ENODATA)
718 goto cleanup;
719 bs->s.not_found = error;
720 }
721 error = 0;
722
723cleanup:
724 return error;
725}
726
727static int
617ba13b
MC
728ext4_xattr_block_set(handle_t *handle, struct inode *inode,
729 struct ext4_xattr_info *i,
730 struct ext4_xattr_block_find *bs)
ac27a0ec
DK
731{
732 struct super_block *sb = inode->i_sb;
733 struct buffer_head *new_bh = NULL;
617ba13b 734 struct ext4_xattr_search *s = &bs->s;
ac27a0ec 735 struct mb_cache_entry *ce = NULL;
8a2bfdcb 736 int error = 0;
ac27a0ec 737
617ba13b 738#define header(x) ((struct ext4_xattr_header *)(x))
ac27a0ec
DK
739
740 if (i->value && i->value_len > sb->s_blocksize)
741 return -ENOSPC;
742 if (s->base) {
617ba13b 743 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
ac27a0ec 744 bs->bh->b_blocknr);
8a2bfdcb
MC
745 error = ext4_journal_get_write_access(handle, bs->bh);
746 if (error)
747 goto cleanup;
748 lock_buffer(bs->bh);
749
ac27a0ec
DK
750 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
751 if (ce) {
752 mb_cache_entry_free(ce);
753 ce = NULL;
754 }
755 ea_bdebug(bs->bh, "modifying in-place");
617ba13b 756 error = ext4_xattr_set_entry(i, s);
ac27a0ec
DK
757 if (!error) {
758 if (!IS_LAST_ENTRY(s->first))
617ba13b 759 ext4_xattr_rehash(header(s->base),
ac27a0ec 760 s->here);
617ba13b 761 ext4_xattr_cache_insert(bs->bh);
ac27a0ec
DK
762 }
763 unlock_buffer(bs->bh);
764 if (error == -EIO)
765 goto bad_block;
766 if (!error)
cc8e94fd
DW
767 error = ext4_handle_dirty_xattr_block(handle,
768 inode,
769 bs->bh);
ac27a0ec
DK
770 if (error)
771 goto cleanup;
772 goto inserted;
773 } else {
774 int offset = (char *)s->here - bs->bh->b_data;
775
8a2bfdcb 776 unlock_buffer(bs->bh);
ac27a0ec
DK
777 if (ce) {
778 mb_cache_entry_release(ce);
779 ce = NULL;
780 }
781 ea_bdebug(bs->bh, "cloning");
216553c4 782 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
ac27a0ec
DK
783 error = -ENOMEM;
784 if (s->base == NULL)
785 goto cleanup;
786 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
787 s->first = ENTRY(header(s->base)+1);
788 header(s->base)->h_refcount = cpu_to_le32(1);
789 s->here = ENTRY(s->base + offset);
790 s->end = s->base + bs->bh->b_size;
791 }
792 } else {
793 /* Allocate a buffer where we construct the new block. */
216553c4 794 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
ac27a0ec
DK
795 /* assert(header == s->base) */
796 error = -ENOMEM;
797 if (s->base == NULL)
798 goto cleanup;
617ba13b 799 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
ac27a0ec
DK
800 header(s->base)->h_blocks = cpu_to_le32(1);
801 header(s->base)->h_refcount = cpu_to_le32(1);
802 s->first = ENTRY(header(s->base)+1);
803 s->here = ENTRY(header(s->base)+1);
804 s->end = s->base + sb->s_blocksize;
805 }
806
617ba13b 807 error = ext4_xattr_set_entry(i, s);
ac27a0ec
DK
808 if (error == -EIO)
809 goto bad_block;
810 if (error)
811 goto cleanup;
812 if (!IS_LAST_ENTRY(s->first))
617ba13b 813 ext4_xattr_rehash(header(s->base), s->here);
ac27a0ec
DK
814
815inserted:
816 if (!IS_LAST_ENTRY(s->first)) {
617ba13b 817 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
ac27a0ec
DK
818 if (new_bh) {
819 /* We found an identical block in the cache. */
820 if (new_bh == bs->bh)
821 ea_bdebug(new_bh, "keeping");
822 else {
823 /* The old block is released after updating
824 the inode. */
5dd4056d
CH
825 error = dquot_alloc_block(inode, 1);
826 if (error)
ac27a0ec 827 goto cleanup;
617ba13b 828 error = ext4_journal_get_write_access(handle,
ac27a0ec
DK
829 new_bh);
830 if (error)
831 goto cleanup_dquot;
832 lock_buffer(new_bh);
e8546d06 833 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
ac27a0ec
DK
834 ea_bdebug(new_bh, "reusing; refcount now=%d",
835 le32_to_cpu(BHDR(new_bh)->h_refcount));
836 unlock_buffer(new_bh);
cc8e94fd
DW
837 error = ext4_handle_dirty_xattr_block(handle,
838 inode,
839 new_bh);
ac27a0ec
DK
840 if (error)
841 goto cleanup_dquot;
842 }
843 mb_cache_entry_release(ce);
844 ce = NULL;
845 } else if (bs->bh && s->base == bs->bh->b_data) {
846 /* We were modifying this block in-place. */
847 ea_bdebug(bs->bh, "keeping this block");
848 new_bh = bs->bh;
849 get_bh(new_bh);
850 } else {
851 /* We need to allocate a new block */
fb0a387d
ES
852 ext4_fsblk_t goal, block;
853
854 goal = ext4_group_first_block_no(sb,
d00a6d7b 855 EXT4_I(inode)->i_block_group);
fb0a387d
ES
856
857 /* non-extent files can't have physical blocks past 2^32 */
12e9b892 858 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
fb0a387d
ES
859 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
860
6d6a4351
ES
861 /*
862 * take i_data_sem because we will test
863 * i_delalloc_reserved_flag in ext4_mb_new_blocks
864 */
865 down_read((&EXT4_I(inode)->i_data_sem));
55f020db
AH
866 block = ext4_new_meta_blocks(handle, inode, goal, 0,
867 NULL, &error);
6d6a4351 868 up_read((&EXT4_I(inode)->i_data_sem));
ac27a0ec
DK
869 if (error)
870 goto cleanup;
fb0a387d 871
12e9b892 872 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
fb0a387d
ES
873 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
874
ace36ad4
JP
875 ea_idebug(inode, "creating block %llu",
876 (unsigned long long)block);
ac27a0ec
DK
877
878 new_bh = sb_getblk(sb, block);
879 if (!new_bh) {
880getblk_failed:
7dc57615 881 ext4_free_blocks(handle, inode, NULL, block, 1,
e6362609 882 EXT4_FREE_BLOCKS_METADATA);
ac27a0ec
DK
883 error = -EIO;
884 goto cleanup;
885 }
886 lock_buffer(new_bh);
617ba13b 887 error = ext4_journal_get_create_access(handle, new_bh);
ac27a0ec
DK
888 if (error) {
889 unlock_buffer(new_bh);
890 goto getblk_failed;
891 }
892 memcpy(new_bh->b_data, s->base, new_bh->b_size);
893 set_buffer_uptodate(new_bh);
894 unlock_buffer(new_bh);
617ba13b 895 ext4_xattr_cache_insert(new_bh);
cc8e94fd
DW
896 error = ext4_handle_dirty_xattr_block(handle,
897 inode, new_bh);
ac27a0ec
DK
898 if (error)
899 goto cleanup;
900 }
901 }
902
903 /* Update the inode. */
617ba13b 904 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
ac27a0ec
DK
905
906 /* Drop the previous xattr block. */
907 if (bs->bh && bs->bh != new_bh)
617ba13b 908 ext4_xattr_release_block(handle, inode, bs->bh);
ac27a0ec
DK
909 error = 0;
910
911cleanup:
912 if (ce)
913 mb_cache_entry_release(ce);
914 brelse(new_bh);
915 if (!(bs->bh && s->base == bs->bh->b_data))
916 kfree(s->base);
917
918 return error;
919
920cleanup_dquot:
5dd4056d 921 dquot_free_block(inode, 1);
ac27a0ec
DK
922 goto cleanup;
923
924bad_block:
24676da4
TT
925 EXT4_ERROR_INODE(inode, "bad block %llu",
926 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
927 goto cleanup;
928
929#undef header
930}
931
879b3825
TM
932int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
933 struct ext4_xattr_ibody_find *is)
ac27a0ec 934{
617ba13b
MC
935 struct ext4_xattr_ibody_header *header;
936 struct ext4_inode *raw_inode;
ac27a0ec
DK
937 int error;
938
617ba13b 939 if (EXT4_I(inode)->i_extra_isize == 0)
ac27a0ec 940 return 0;
617ba13b 941 raw_inode = ext4_raw_inode(&is->iloc);
ac27a0ec
DK
942 header = IHDR(inode, raw_inode);
943 is->s.base = is->s.first = IFIRST(header);
944 is->s.here = is->s.first;
617ba13b 945 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
19f5fb7a 946 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
617ba13b 947 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
ac27a0ec
DK
948 if (error)
949 return error;
950 /* Find the named attribute. */
617ba13b 951 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
ac27a0ec
DK
952 i->name, is->s.end -
953 (void *)is->s.base, 0);
954 if (error && error != -ENODATA)
955 return error;
956 is->s.not_found = error;
957 }
958 return 0;
959}
960
0d812f77
TM
961int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
962 struct ext4_xattr_info *i,
963 struct ext4_xattr_ibody_find *is)
964{
965 struct ext4_xattr_ibody_header *header;
966 struct ext4_xattr_search *s = &is->s;
967 int error;
968
969 if (EXT4_I(inode)->i_extra_isize == 0)
970 return -ENOSPC;
971 error = ext4_xattr_set_entry(i, s);
972 if (error) {
973 if (error == -ENOSPC &&
974 ext4_has_inline_data(inode)) {
975 error = ext4_try_to_evict_inline_data(handle, inode,
976 EXT4_XATTR_LEN(strlen(i->name) +
977 EXT4_XATTR_SIZE(i->value_len)));
978 if (error)
979 return error;
980 error = ext4_xattr_ibody_find(inode, i, is);
981 if (error)
982 return error;
983 error = ext4_xattr_set_entry(i, s);
984 }
985 if (error)
986 return error;
987 }
988 header = IHDR(inode, ext4_raw_inode(&is->iloc));
989 if (!IS_LAST_ENTRY(s->first)) {
990 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
991 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
992 } else {
993 header->h_magic = cpu_to_le32(0);
994 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
995 }
996 return 0;
997}
998
999static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1000 struct ext4_xattr_info *i,
1001 struct ext4_xattr_ibody_find *is)
ac27a0ec 1002{
617ba13b
MC
1003 struct ext4_xattr_ibody_header *header;
1004 struct ext4_xattr_search *s = &is->s;
ac27a0ec
DK
1005 int error;
1006
617ba13b 1007 if (EXT4_I(inode)->i_extra_isize == 0)
ac27a0ec 1008 return -ENOSPC;
617ba13b 1009 error = ext4_xattr_set_entry(i, s);
ac27a0ec
DK
1010 if (error)
1011 return error;
617ba13b 1012 header = IHDR(inode, ext4_raw_inode(&is->iloc));
ac27a0ec 1013 if (!IS_LAST_ENTRY(s->first)) {
617ba13b 1014 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
19f5fb7a 1015 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
1016 } else {
1017 header->h_magic = cpu_to_le32(0);
19f5fb7a 1018 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
1019 }
1020 return 0;
1021}
1022
1023/*
617ba13b 1024 * ext4_xattr_set_handle()
ac27a0ec 1025 *
6e9510b0 1026 * Create, replace or remove an extended attribute for this inode. Value
ac27a0ec
DK
1027 * is NULL to remove an existing extended attribute, and non-NULL to
1028 * either replace an existing extended attribute, or create a new extended
1029 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1030 * specify that an extended attribute must exist and must not exist
1031 * previous to the call, respectively.
1032 *
1033 * Returns 0, or a negative error number on failure.
1034 */
1035int
617ba13b 1036ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ac27a0ec
DK
1037 const char *name, const void *value, size_t value_len,
1038 int flags)
1039{
617ba13b 1040 struct ext4_xattr_info i = {
ac27a0ec
DK
1041 .name_index = name_index,
1042 .name = name,
1043 .value = value,
1044 .value_len = value_len,
1045
1046 };
617ba13b 1047 struct ext4_xattr_ibody_find is = {
ac27a0ec
DK
1048 .s = { .not_found = -ENODATA, },
1049 };
617ba13b 1050 struct ext4_xattr_block_find bs = {
ac27a0ec
DK
1051 .s = { .not_found = -ENODATA, },
1052 };
4d20c685 1053 unsigned long no_expand;
ac27a0ec
DK
1054 int error;
1055
1056 if (!name)
1057 return -EINVAL;
1058 if (strlen(name) > 255)
1059 return -ERANGE;
617ba13b 1060 down_write(&EXT4_I(inode)->xattr_sem);
19f5fb7a
TT
1061 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1062 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
4d20c685 1063
66543617 1064 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
86ebfd08
ES
1065 if (error)
1066 goto cleanup;
1067
19f5fb7a 1068 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
617ba13b
MC
1069 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1070 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
19f5fb7a 1071 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
ac27a0ec
DK
1072 }
1073
617ba13b 1074 error = ext4_xattr_ibody_find(inode, &i, &is);
ac27a0ec
DK
1075 if (error)
1076 goto cleanup;
1077 if (is.s.not_found)
617ba13b 1078 error = ext4_xattr_block_find(inode, &i, &bs);
ac27a0ec
DK
1079 if (error)
1080 goto cleanup;
1081 if (is.s.not_found && bs.s.not_found) {
1082 error = -ENODATA;
1083 if (flags & XATTR_REPLACE)
1084 goto cleanup;
1085 error = 0;
1086 if (!value)
1087 goto cleanup;
1088 } else {
1089 error = -EEXIST;
1090 if (flags & XATTR_CREATE)
1091 goto cleanup;
1092 }
ac27a0ec
DK
1093 if (!value) {
1094 if (!is.s.not_found)
617ba13b 1095 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
ac27a0ec 1096 else if (!bs.s.not_found)
617ba13b 1097 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec 1098 } else {
617ba13b 1099 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
ac27a0ec
DK
1100 if (!error && !bs.s.not_found) {
1101 i.value = NULL;
617ba13b 1102 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec 1103 } else if (error == -ENOSPC) {
7e01c8e5
TY
1104 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1105 error = ext4_xattr_block_find(inode, &i, &bs);
1106 if (error)
1107 goto cleanup;
1108 }
617ba13b 1109 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec
DK
1110 if (error)
1111 goto cleanup;
1112 if (!is.s.not_found) {
1113 i.value = NULL;
617ba13b 1114 error = ext4_xattr_ibody_set(handle, inode, &i,
ac27a0ec
DK
1115 &is);
1116 }
1117 }
1118 }
1119 if (!error) {
617ba13b 1120 ext4_xattr_update_super_block(handle, inode->i_sb);
ef7f3835 1121 inode->i_ctime = ext4_current_time(inode);
6dd4ee7c 1122 if (!value)
19f5fb7a 1123 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
617ba13b 1124 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
ac27a0ec 1125 /*
617ba13b 1126 * The bh is consumed by ext4_mark_iloc_dirty, even with
ac27a0ec
DK
1127 * error != 0.
1128 */
1129 is.iloc.bh = NULL;
1130 if (IS_SYNC(inode))
0390131b 1131 ext4_handle_sync(handle);
ac27a0ec
DK
1132 }
1133
1134cleanup:
1135 brelse(is.iloc.bh);
1136 brelse(bs.bh);
4d20c685 1137 if (no_expand == 0)
19f5fb7a 1138 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
617ba13b 1139 up_write(&EXT4_I(inode)->xattr_sem);
ac27a0ec
DK
1140 return error;
1141}
1142
1143/*
617ba13b 1144 * ext4_xattr_set()
ac27a0ec 1145 *
617ba13b 1146 * Like ext4_xattr_set_handle, but start from an inode. This extended
ac27a0ec
DK
1147 * attribute modification is a filesystem transaction by itself.
1148 *
1149 * Returns 0, or a negative error number on failure.
1150 */
1151int
617ba13b 1152ext4_xattr_set(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
1153 const void *value, size_t value_len, int flags)
1154{
1155 handle_t *handle;
1156 int error, retries = 0;
0d812f77 1157 int credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb);
ac27a0ec
DK
1158
1159retry:
0d812f77
TM
1160 /*
1161 * In case of inline data, we may push out the data to a block,
1162 * So reserve the journal space first.
1163 */
1164 if (ext4_has_inline_data(inode))
1165 credits += ext4_writepage_trans_blocks(inode) + 1;
1166
1167 handle = ext4_journal_start(inode, credits);
ac27a0ec
DK
1168 if (IS_ERR(handle)) {
1169 error = PTR_ERR(handle);
1170 } else {
1171 int error2;
1172
617ba13b 1173 error = ext4_xattr_set_handle(handle, inode, name_index, name,
ac27a0ec 1174 value, value_len, flags);
617ba13b 1175 error2 = ext4_journal_stop(handle);
ac27a0ec 1176 if (error == -ENOSPC &&
617ba13b 1177 ext4_should_retry_alloc(inode->i_sb, &retries))
ac27a0ec
DK
1178 goto retry;
1179 if (error == 0)
1180 error = error2;
1181 }
1182
1183 return error;
1184}
1185
6dd4ee7c
KS
1186/*
1187 * Shift the EA entries in the inode to create space for the increased
1188 * i_extra_isize.
1189 */
1190static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1191 int value_offs_shift, void *to,
1192 void *from, size_t n, int blocksize)
1193{
1194 struct ext4_xattr_entry *last = entry;
1195 int new_offs;
1196
1197 /* Adjust the value offsets of the entries */
1198 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1199 if (!last->e_value_block && last->e_value_size) {
1200 new_offs = le16_to_cpu(last->e_value_offs) +
1201 value_offs_shift;
1202 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1203 > blocksize);
1204 last->e_value_offs = cpu_to_le16(new_offs);
1205 }
1206 }
1207 /* Shift the entries by n bytes */
1208 memmove(to, from, n);
1209}
1210
1211/*
1212 * Expand an inode by new_extra_isize bytes when EAs are present.
1213 * Returns 0 on success or negative error number on failure.
1214 */
1215int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1216 struct ext4_inode *raw_inode, handle_t *handle)
1217{
1218 struct ext4_xattr_ibody_header *header;
1219 struct ext4_xattr_entry *entry, *last, *first;
1220 struct buffer_head *bh = NULL;
1221 struct ext4_xattr_ibody_find *is = NULL;
1222 struct ext4_xattr_block_find *bs = NULL;
1223 char *buffer = NULL, *b_entry_name = NULL;
1224 size_t min_offs, free;
1225 int total_ino, total_blk;
1226 void *base, *start, *end;
1227 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
ac39849d 1228 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
6dd4ee7c
KS
1229
1230 down_write(&EXT4_I(inode)->xattr_sem);
1231retry:
1232 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1233 up_write(&EXT4_I(inode)->xattr_sem);
1234 return 0;
1235 }
1236
1237 header = IHDR(inode, raw_inode);
1238 entry = IFIRST(header);
1239
1240 /*
1241 * Check if enough free space is available in the inode to shift the
1242 * entries ahead by new_extra_isize.
1243 */
1244
1245 base = start = entry;
1246 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1247 min_offs = end - base;
1248 last = entry;
1249 total_ino = sizeof(struct ext4_xattr_ibody_header);
1250
1251 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1252 if (free >= new_extra_isize) {
1253 entry = IFIRST(header);
1254 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1255 - new_extra_isize, (void *)raw_inode +
1256 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1257 (void *)header, total_ino,
1258 inode->i_sb->s_blocksize);
1259 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1260 error = 0;
1261 goto cleanup;
1262 }
1263
1264 /*
1265 * Enough free space isn't available in the inode, check if
1266 * EA block can hold new_extra_isize bytes.
1267 */
1268 if (EXT4_I(inode)->i_file_acl) {
1269 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1270 error = -EIO;
1271 if (!bh)
1272 goto cleanup;
cc8e94fd 1273 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
1274 EXT4_ERROR_INODE(inode, "bad block %llu",
1275 EXT4_I(inode)->i_file_acl);
6dd4ee7c
KS
1276 error = -EIO;
1277 goto cleanup;
1278 }
1279 base = BHDR(bh);
1280 first = BFIRST(bh);
1281 end = bh->b_data + bh->b_size;
1282 min_offs = end - base;
1283 free = ext4_xattr_free_space(first, &min_offs, base,
1284 &total_blk);
1285 if (free < new_extra_isize) {
1286 if (!tried_min_extra_isize && s_min_extra_isize) {
1287 tried_min_extra_isize++;
1288 new_extra_isize = s_min_extra_isize;
1289 brelse(bh);
1290 goto retry;
1291 }
1292 error = -1;
1293 goto cleanup;
1294 }
1295 } else {
1296 free = inode->i_sb->s_blocksize;
1297 }
1298
1299 while (new_extra_isize > 0) {
1300 size_t offs, size, entry_size;
1301 struct ext4_xattr_entry *small_entry = NULL;
1302 struct ext4_xattr_info i = {
1303 .value = NULL,
1304 .value_len = 0,
1305 };
1306 unsigned int total_size; /* EA entry size + value size */
1307 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1308 unsigned int min_total_size = ~0U;
1309
1310 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1311 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1312 if (!is || !bs) {
1313 error = -ENOMEM;
1314 goto cleanup;
1315 }
1316
1317 is->s.not_found = -ENODATA;
1318 bs->s.not_found = -ENODATA;
1319 is->iloc.bh = NULL;
1320 bs->bh = NULL;
1321
1322 last = IFIRST(header);
1323 /* Find the entry best suited to be pushed into EA block */
1324 entry = NULL;
1325 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1326 total_size =
1327 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1328 EXT4_XATTR_LEN(last->e_name_len);
1329 if (total_size <= free && total_size < min_total_size) {
1330 if (total_size < new_extra_isize) {
1331 small_entry = last;
1332 } else {
1333 entry = last;
1334 min_total_size = total_size;
1335 }
1336 }
1337 }
1338
1339 if (entry == NULL) {
1340 if (small_entry) {
1341 entry = small_entry;
1342 } else {
1343 if (!tried_min_extra_isize &&
1344 s_min_extra_isize) {
1345 tried_min_extra_isize++;
1346 new_extra_isize = s_min_extra_isize;
1347 goto retry;
1348 }
1349 error = -1;
1350 goto cleanup;
1351 }
1352 }
1353 offs = le16_to_cpu(entry->e_value_offs);
1354 size = le32_to_cpu(entry->e_value_size);
1355 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1356 i.name_index = entry->e_name_index,
1357 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1358 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1359 if (!buffer || !b_entry_name) {
1360 error = -ENOMEM;
1361 goto cleanup;
1362 }
1363 /* Save the entry name and the entry value */
1364 memcpy(buffer, (void *)IFIRST(header) + offs,
1365 EXT4_XATTR_SIZE(size));
1366 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1367 b_entry_name[entry->e_name_len] = '\0';
1368 i.name = b_entry_name;
1369
1370 error = ext4_get_inode_loc(inode, &is->iloc);
1371 if (error)
1372 goto cleanup;
1373
1374 error = ext4_xattr_ibody_find(inode, &i, is);
1375 if (error)
1376 goto cleanup;
1377
1378 /* Remove the chosen entry from the inode */
1379 error = ext4_xattr_ibody_set(handle, inode, &i, is);
9aaab058
RK
1380 if (error)
1381 goto cleanup;
6dd4ee7c
KS
1382
1383 entry = IFIRST(header);
1384 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1385 shift_bytes = new_extra_isize;
1386 else
1387 shift_bytes = entry_size + size;
1388 /* Adjust the offsets and shift the remaining entries ahead */
1389 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1390 shift_bytes, (void *)raw_inode +
1391 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1392 (void *)header, total_ino - entry_size,
1393 inode->i_sb->s_blocksize);
1394
1395 extra_isize += shift_bytes;
1396 new_extra_isize -= shift_bytes;
1397 EXT4_I(inode)->i_extra_isize = extra_isize;
1398
1399 i.name = b_entry_name;
1400 i.value = buffer;
ac39849d 1401 i.value_len = size;
6dd4ee7c
KS
1402 error = ext4_xattr_block_find(inode, &i, bs);
1403 if (error)
1404 goto cleanup;
1405
1406 /* Add entry which was removed from the inode into the block */
1407 error = ext4_xattr_block_set(handle, inode, &i, bs);
1408 if (error)
1409 goto cleanup;
1410 kfree(b_entry_name);
1411 kfree(buffer);
d3533d72
JL
1412 b_entry_name = NULL;
1413 buffer = NULL;
6dd4ee7c
KS
1414 brelse(is->iloc.bh);
1415 kfree(is);
1416 kfree(bs);
1417 }
1418 brelse(bh);
1419 up_write(&EXT4_I(inode)->xattr_sem);
1420 return 0;
1421
1422cleanup:
1423 kfree(b_entry_name);
1424 kfree(buffer);
1425 if (is)
1426 brelse(is->iloc.bh);
1427 kfree(is);
1428 kfree(bs);
1429 brelse(bh);
1430 up_write(&EXT4_I(inode)->xattr_sem);
1431 return error;
1432}
1433
1434
1435
ac27a0ec 1436/*
617ba13b 1437 * ext4_xattr_delete_inode()
ac27a0ec
DK
1438 *
1439 * Free extended attribute resources associated with this inode. This
1440 * is called immediately before an inode is freed. We have exclusive
1441 * access to the inode.
1442 */
1443void
617ba13b 1444ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
ac27a0ec
DK
1445{
1446 struct buffer_head *bh = NULL;
1447
617ba13b 1448 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 1449 goto cleanup;
617ba13b 1450 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec 1451 if (!bh) {
24676da4
TT
1452 EXT4_ERROR_INODE(inode, "block %llu read error",
1453 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1454 goto cleanup;
1455 }
617ba13b 1456 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
ac27a0ec 1457 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
24676da4
TT
1458 EXT4_ERROR_INODE(inode, "bad block %llu",
1459 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1460 goto cleanup;
1461 }
617ba13b
MC
1462 ext4_xattr_release_block(handle, inode, bh);
1463 EXT4_I(inode)->i_file_acl = 0;
ac27a0ec
DK
1464
1465cleanup:
1466 brelse(bh);
1467}
1468
1469/*
617ba13b 1470 * ext4_xattr_put_super()
ac27a0ec
DK
1471 *
1472 * This is called when a file system is unmounted.
1473 */
1474void
617ba13b 1475ext4_xattr_put_super(struct super_block *sb)
ac27a0ec
DK
1476{
1477 mb_cache_shrink(sb->s_bdev);
1478}
1479
1480/*
617ba13b 1481 * ext4_xattr_cache_insert()
ac27a0ec
DK
1482 *
1483 * Create a new entry in the extended attribute cache, and insert
1484 * it unless such an entry is already in the cache.
1485 *
1486 * Returns 0, or a negative error number on failure.
1487 */
1488static void
617ba13b 1489ext4_xattr_cache_insert(struct buffer_head *bh)
ac27a0ec
DK
1490{
1491 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1492 struct mb_cache_entry *ce;
1493 int error;
1494
335e92e8 1495 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
ac27a0ec
DK
1496 if (!ce) {
1497 ea_bdebug(bh, "out of memory");
1498 return;
1499 }
2aec7c52 1500 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
ac27a0ec
DK
1501 if (error) {
1502 mb_cache_entry_free(ce);
1503 if (error == -EBUSY) {
1504 ea_bdebug(bh, "already in cache");
1505 error = 0;
1506 }
1507 } else {
1508 ea_bdebug(bh, "inserting [%x]", (int)hash);
1509 mb_cache_entry_release(ce);
1510 }
1511}
1512
1513/*
617ba13b 1514 * ext4_xattr_cmp()
ac27a0ec
DK
1515 *
1516 * Compare two extended attribute blocks for equality.
1517 *
1518 * Returns 0 if the blocks are equal, 1 if they differ, and
1519 * a negative error number on errors.
1520 */
1521static int
617ba13b
MC
1522ext4_xattr_cmp(struct ext4_xattr_header *header1,
1523 struct ext4_xattr_header *header2)
ac27a0ec 1524{
617ba13b 1525 struct ext4_xattr_entry *entry1, *entry2;
ac27a0ec
DK
1526
1527 entry1 = ENTRY(header1+1);
1528 entry2 = ENTRY(header2+1);
1529 while (!IS_LAST_ENTRY(entry1)) {
1530 if (IS_LAST_ENTRY(entry2))
1531 return 1;
1532 if (entry1->e_hash != entry2->e_hash ||
1533 entry1->e_name_index != entry2->e_name_index ||
1534 entry1->e_name_len != entry2->e_name_len ||
1535 entry1->e_value_size != entry2->e_value_size ||
1536 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1537 return 1;
1538 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1539 return -EIO;
1540 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1541 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1542 le32_to_cpu(entry1->e_value_size)))
1543 return 1;
1544
617ba13b
MC
1545 entry1 = EXT4_XATTR_NEXT(entry1);
1546 entry2 = EXT4_XATTR_NEXT(entry2);
ac27a0ec
DK
1547 }
1548 if (!IS_LAST_ENTRY(entry2))
1549 return 1;
1550 return 0;
1551}
1552
1553/*
617ba13b 1554 * ext4_xattr_cache_find()
ac27a0ec
DK
1555 *
1556 * Find an identical extended attribute block.
1557 *
1558 * Returns a pointer to the block found, or NULL if such a block was
1559 * not found or an error occurred.
1560 */
1561static struct buffer_head *
617ba13b 1562ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
ac27a0ec
DK
1563 struct mb_cache_entry **pce)
1564{
1565 __u32 hash = le32_to_cpu(header->h_hash);
1566 struct mb_cache_entry *ce;
1567
1568 if (!header->h_hash)
1569 return NULL; /* never share */
1570 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1571again:
2aec7c52
AG
1572 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1573 hash);
ac27a0ec
DK
1574 while (ce) {
1575 struct buffer_head *bh;
1576
1577 if (IS_ERR(ce)) {
1578 if (PTR_ERR(ce) == -EAGAIN)
1579 goto again;
1580 break;
1581 }
1582 bh = sb_bread(inode->i_sb, ce->e_block);
1583 if (!bh) {
24676da4
TT
1584 EXT4_ERROR_INODE(inode, "block %lu read error",
1585 (unsigned long) ce->e_block);
ac27a0ec 1586 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
617ba13b 1587 EXT4_XATTR_REFCOUNT_MAX) {
ac27a0ec
DK
1588 ea_idebug(inode, "block %lu refcount %d>=%d",
1589 (unsigned long) ce->e_block,
1590 le32_to_cpu(BHDR(bh)->h_refcount),
617ba13b
MC
1591 EXT4_XATTR_REFCOUNT_MAX);
1592 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
ac27a0ec
DK
1593 *pce = ce;
1594 return bh;
1595 }
1596 brelse(bh);
2aec7c52 1597 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
ac27a0ec
DK
1598 }
1599 return NULL;
1600}
1601
1602#define NAME_HASH_SHIFT 5
1603#define VALUE_HASH_SHIFT 16
1604
1605/*
617ba13b 1606 * ext4_xattr_hash_entry()
ac27a0ec
DK
1607 *
1608 * Compute the hash of an extended attribute.
1609 */
617ba13b
MC
1610static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1611 struct ext4_xattr_entry *entry)
ac27a0ec
DK
1612{
1613 __u32 hash = 0;
1614 char *name = entry->e_name;
1615 int n;
1616
2b2d6d01 1617 for (n = 0; n < entry->e_name_len; n++) {
ac27a0ec
DK
1618 hash = (hash << NAME_HASH_SHIFT) ^
1619 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1620 *name++;
1621 }
1622
1623 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1624 __le32 *value = (__le32 *)((char *)header +
1625 le16_to_cpu(entry->e_value_offs));
1626 for (n = (le32_to_cpu(entry->e_value_size) +
617ba13b 1627 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
ac27a0ec
DK
1628 hash = (hash << VALUE_HASH_SHIFT) ^
1629 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1630 le32_to_cpu(*value++);
1631 }
1632 }
1633 entry->e_hash = cpu_to_le32(hash);
1634}
1635
1636#undef NAME_HASH_SHIFT
1637#undef VALUE_HASH_SHIFT
1638
1639#define BLOCK_HASH_SHIFT 16
1640
1641/*
617ba13b 1642 * ext4_xattr_rehash()
ac27a0ec
DK
1643 *
1644 * Re-compute the extended attribute hash value after an entry has changed.
1645 */
617ba13b
MC
1646static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1647 struct ext4_xattr_entry *entry)
ac27a0ec 1648{
617ba13b 1649 struct ext4_xattr_entry *here;
ac27a0ec
DK
1650 __u32 hash = 0;
1651
617ba13b 1652 ext4_xattr_hash_entry(header, entry);
ac27a0ec
DK
1653 here = ENTRY(header+1);
1654 while (!IS_LAST_ENTRY(here)) {
1655 if (!here->e_hash) {
1656 /* Block is not shared if an entry's hash value == 0 */
1657 hash = 0;
1658 break;
1659 }
1660 hash = (hash << BLOCK_HASH_SHIFT) ^
1661 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1662 le32_to_cpu(here->e_hash);
617ba13b 1663 here = EXT4_XATTR_NEXT(here);
ac27a0ec
DK
1664 }
1665 header->h_hash = cpu_to_le32(hash);
1666}
1667
1668#undef BLOCK_HASH_SHIFT
1669
1670int __init
5dabfc78 1671ext4_init_xattr(void)
ac27a0ec 1672{
2aec7c52 1673 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
617ba13b 1674 if (!ext4_xattr_cache)
ac27a0ec
DK
1675 return -ENOMEM;
1676 return 0;
1677}
1678
1679void
5dabfc78 1680ext4_exit_xattr(void)
ac27a0ec 1681{
617ba13b
MC
1682 if (ext4_xattr_cache)
1683 mb_cache_destroy(ext4_xattr_cache);
1684 ext4_xattr_cache = NULL;
ac27a0ec 1685}
This page took 0.568139 seconds and 5 git commands to generate.