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