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