blob: 83b7be849bd50745b3f60b3b4351eadc28f00aa3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/xattr.c
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
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 * ----------------
46 * EXT3_I(inode)->i_file_acl is protected by EXT3_I(inode)->xattr_sem.
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>
56#include <linux/ext3_jbd.h>
57#include <linux/ext3_fs.h>
58#include <linux/mbcache.h>
59#include <linux/quotaops.h>
60#include <linux/rwsem.h>
61#include "xattr.h"
62#include "acl.h"
63
64#define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
65#define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
66#define BFIRST(bh) ENTRY(BHDR(bh)+1)
67#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
68
69#define IHDR(inode, raw_inode) \
70 ((struct ext3_xattr_ibody_header *) \
71 ((void *)raw_inode + \
72 EXT3_GOOD_OLD_INODE_SIZE + \
73 EXT3_I(inode)->i_extra_isize))
74#define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
75
76#ifdef EXT3_XATTR_DEBUG
77# define ea_idebug(inode, f...) do { \
Eric Sandeeneee194e2006-09-27 01:49:30 -070078 printk(KERN_DEBUG "inode %s:%lu: ", \
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 inode->i_sb->s_id, inode->i_ino); \
80 printk(f); \
81 printk("\n"); \
82 } while (0)
83# define ea_bdebug(bh, f...) do { \
84 char b[BDEVNAME_SIZE]; \
85 printk(KERN_DEBUG "block %s:%lu: ", \
86 bdevname(bh->b_bdev, b), \
87 (unsigned long) bh->b_blocknr); \
88 printk(f); \
89 printk("\n"); \
90 } while (0)
91#else
92# define ea_idebug(f...)
93# define ea_bdebug(f...)
94#endif
95
96static void ext3_xattr_cache_insert(struct buffer_head *);
97static struct buffer_head *ext3_xattr_cache_find(struct inode *,
98 struct ext3_xattr_header *,
99 struct mb_cache_entry **);
100static void ext3_xattr_rehash(struct ext3_xattr_header *,
101 struct ext3_xattr_entry *);
Adrian Bunk15633002008-04-28 02:16:06 -0700102static int ext3_xattr_list(struct inode *inode, char *buffer,
103 size_t buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct mb_cache *ext3_xattr_cache;
106
107static struct xattr_handler *ext3_xattr_handler_map[] = {
108 [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
109#ifdef CONFIG_EXT3_FS_POSIX_ACL
110 [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
111 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
112#endif
113 [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
114#ifdef CONFIG_EXT3_FS_SECURITY
115 [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
116#endif
117};
118
119struct xattr_handler *ext3_xattr_handlers[] = {
120 &ext3_xattr_user_handler,
121 &ext3_xattr_trusted_handler,
122#ifdef CONFIG_EXT3_FS_POSIX_ACL
123 &ext3_xattr_acl_access_handler,
124 &ext3_xattr_acl_default_handler,
125#endif
126#ifdef CONFIG_EXT3_FS_SECURITY
127 &ext3_xattr_security_handler,
128#endif
129 NULL
130};
131
132static inline struct xattr_handler *
133ext3_xattr_handler(int name_index)
134{
135 struct xattr_handler *handler = NULL;
136
137 if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
138 handler = ext3_xattr_handler_map[name_index];
139 return handler;
140}
141
142/*
143 * Inode operation listxattr()
144 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800145 * dentry->d_inode->i_mutex: don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
147ssize_t
148ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
149{
150 return ext3_xattr_list(dentry->d_inode, buffer, size);
151}
152
153static int
154ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
155{
156 while (!IS_LAST_ENTRY(entry)) {
157 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
158 if ((void *)next >= end)
159 return -EIO;
160 entry = next;
161 }
162 return 0;
163}
164
165static inline int
166ext3_xattr_check_block(struct buffer_head *bh)
167{
168 int error;
169
170 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
171 BHDR(bh)->h_blocks != cpu_to_le32(1))
172 return -EIO;
173 error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
174 return error;
175}
176
177static inline int
178ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
179{
180 size_t value_size = le32_to_cpu(entry->e_value_size);
181
182 if (entry->e_value_block != 0 || value_size > size ||
183 le16_to_cpu(entry->e_value_offs) + value_size > size)
184 return -EIO;
185 return 0;
186}
187
188static int
189ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
190 const char *name, size_t size, int sorted)
191{
192 struct ext3_xattr_entry *entry;
193 size_t name_len;
194 int cmp = 1;
195
196 if (name == NULL)
197 return -EINVAL;
198 name_len = strlen(name);
199 entry = *pentry;
200 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
201 cmp = name_index - entry->e_name_index;
202 if (!cmp)
203 cmp = name_len - entry->e_name_len;
204 if (!cmp)
205 cmp = memcmp(name, entry->e_name, name_len);
206 if (cmp <= 0 && (sorted || cmp == 0))
207 break;
208 }
209 *pentry = entry;
210 if (!cmp && ext3_xattr_check_entry(entry, size))
211 return -EIO;
212 return cmp ? -ENODATA : 0;
213}
214
Ben Dooks381be2542005-10-30 15:02:56 -0800215static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
217 void *buffer, size_t buffer_size)
218{
219 struct buffer_head *bh = NULL;
220 struct ext3_xattr_entry *entry;
221 size_t size;
222 int error;
223
224 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
225 name_index, name, buffer, (long)buffer_size);
226
227 error = -ENODATA;
228 if (!EXT3_I(inode)->i_file_acl)
229 goto cleanup;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700230 ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
232 if (!bh)
233 goto cleanup;
234 ea_bdebug(bh, "b_count=%d, refcount=%d",
235 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
236 if (ext3_xattr_check_block(bh)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700237bad_block: ext3_error(inode->i_sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700238 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 EXT3_I(inode)->i_file_acl);
240 error = -EIO;
241 goto cleanup;
242 }
243 ext3_xattr_cache_insert(bh);
244 entry = BFIRST(bh);
245 error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
246 if (error == -EIO)
247 goto bad_block;
248 if (error)
249 goto cleanup;
250 size = le32_to_cpu(entry->e_value_size);
251 if (buffer) {
252 error = -ERANGE;
253 if (size > buffer_size)
254 goto cleanup;
255 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
256 size);
257 }
258 error = size;
259
260cleanup:
261 brelse(bh);
262 return error;
263}
264
265static int
266ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
267 void *buffer, size_t buffer_size)
268{
269 struct ext3_xattr_ibody_header *header;
270 struct ext3_xattr_entry *entry;
271 struct ext3_inode *raw_inode;
272 struct ext3_iloc iloc;
273 size_t size;
274 void *end;
275 int error;
276
277 if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
278 return -ENODATA;
279 error = ext3_get_inode_loc(inode, &iloc);
280 if (error)
281 return error;
282 raw_inode = ext3_raw_inode(&iloc);
283 header = IHDR(inode, raw_inode);
284 entry = IFIRST(header);
285 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
286 error = ext3_xattr_check_names(entry, end);
287 if (error)
288 goto cleanup;
289 error = ext3_xattr_find_entry(&entry, name_index, name,
290 end - (void *)entry, 0);
291 if (error)
292 goto cleanup;
293 size = le32_to_cpu(entry->e_value_size);
294 if (buffer) {
295 error = -ERANGE;
296 if (size > buffer_size)
297 goto cleanup;
298 memcpy(buffer, (void *)IFIRST(header) +
299 le16_to_cpu(entry->e_value_offs), size);
300 }
301 error = size;
302
303cleanup:
304 brelse(iloc.bh);
305 return error;
306}
307
308/*
309 * ext3_xattr_get()
310 *
311 * Copy an extended attribute into the buffer
312 * provided, or compute the buffer size required.
313 * Buffer is NULL to compute the size of the buffer required.
314 *
315 * Returns a negative error number on failure, or the number of bytes
316 * used / required on success.
317 */
318int
319ext3_xattr_get(struct inode *inode, int name_index, const char *name,
320 void *buffer, size_t buffer_size)
321{
322 int error;
323
324 down_read(&EXT3_I(inode)->xattr_sem);
325 error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
326 buffer_size);
327 if (error == -ENODATA)
328 error = ext3_xattr_block_get(inode, name_index, name, buffer,
329 buffer_size);
330 up_read(&EXT3_I(inode)->xattr_sem);
331 return error;
332}
333
334static int
335ext3_xattr_list_entries(struct inode *inode, struct ext3_xattr_entry *entry,
336 char *buffer, size_t buffer_size)
337{
338 size_t rest = buffer_size;
339
340 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
341 struct xattr_handler *handler =
342 ext3_xattr_handler(entry->e_name_index);
343
344 if (handler) {
345 size_t size = handler->list(inode, buffer, rest,
346 entry->e_name,
347 entry->e_name_len);
348 if (buffer) {
349 if (size > rest)
350 return -ERANGE;
351 buffer += size;
352 }
353 rest -= size;
354 }
355 }
356 return buffer_size - rest;
357}
358
Ben Dooks381be2542005-10-30 15:02:56 -0800359static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360ext3_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
361{
362 struct buffer_head *bh = NULL;
363 int error;
364
365 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
366 buffer, (long)buffer_size);
367
368 error = 0;
369 if (!EXT3_I(inode)->i_file_acl)
370 goto cleanup;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700371 ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
373 error = -EIO;
374 if (!bh)
375 goto cleanup;
376 ea_bdebug(bh, "b_count=%d, refcount=%d",
377 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
378 if (ext3_xattr_check_block(bh)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700379 ext3_error(inode->i_sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700380 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 EXT3_I(inode)->i_file_acl);
382 error = -EIO;
383 goto cleanup;
384 }
385 ext3_xattr_cache_insert(bh);
386 error = ext3_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
387
388cleanup:
389 brelse(bh);
390
391 return error;
392}
393
394static int
395ext3_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
396{
397 struct ext3_xattr_ibody_header *header;
398 struct ext3_inode *raw_inode;
399 struct ext3_iloc iloc;
400 void *end;
401 int error;
402
403 if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
404 return 0;
405 error = ext3_get_inode_loc(inode, &iloc);
406 if (error)
407 return error;
408 raw_inode = ext3_raw_inode(&iloc);
409 header = IHDR(inode, raw_inode);
410 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
411 error = ext3_xattr_check_names(IFIRST(header), end);
412 if (error)
413 goto cleanup;
414 error = ext3_xattr_list_entries(inode, IFIRST(header),
415 buffer, buffer_size);
416
417cleanup:
418 brelse(iloc.bh);
419 return error;
420}
421
422/*
423 * ext3_xattr_list()
424 *
425 * Copy a list of attribute names into the buffer
426 * provided, or compute the buffer size required.
427 * Buffer is NULL to compute the size of the buffer required.
428 *
429 * Returns a negative error number on failure, or the number of bytes
430 * used / required on success.
431 */
Adrian Bunk15633002008-04-28 02:16:06 -0700432static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
434{
435 int i_error, b_error;
436
437 down_read(&EXT3_I(inode)->xattr_sem);
438 i_error = ext3_xattr_ibody_list(inode, buffer, buffer_size);
439 if (i_error < 0) {
440 b_error = 0;
441 } else {
442 if (buffer) {
443 buffer += i_error;
444 buffer_size -= i_error;
445 }
446 b_error = ext3_xattr_block_list(inode, buffer, buffer_size);
447 if (b_error < 0)
448 i_error = 0;
449 }
450 up_read(&EXT3_I(inode)->xattr_sem);
451 return i_error + b_error;
452}
453
454/*
455 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
456 * not set, set it.
457 */
458static void ext3_xattr_update_super_block(handle_t *handle,
459 struct super_block *sb)
460{
461 if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
462 return;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
Andreas Gruenbachered2908f2006-12-06 20:36:16 -0800465 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 sb->s_dirt = 1;
467 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/*
472 * Release the xattr block BH: If the reference count is > 1, decrement
473 * it; otherwise free the block.
474 */
475static void
476ext3_xattr_release_block(handle_t *handle, struct inode *inode,
477 struct buffer_head *bh)
478{
479 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800480 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800483 error = ext3_journal_get_write_access(handle, bh);
484 if (error)
485 goto out;
486
487 lock_buffer(bh);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
490 ea_bdebug(bh, "refcount now=0; freeing");
491 if (ce)
492 mb_cache_entry_free(ce);
493 ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
494 get_bh(bh);
495 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
496 } else {
Marcin Slusarz50e8a282008-02-08 04:20:13 -0800497 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800498 error = ext3_journal_dirty_metadata(handle, bh);
Andrew Morton105fd102007-03-23 00:10:02 -0700499 if (IS_SYNC(inode))
500 handle->h_sync = 1;
Jan Kara81a05222009-01-26 16:58:01 +0100501 vfs_dq_free_block(inode, 1);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800502 ea_bdebug(bh, "refcount now=%d; releasing",
503 le32_to_cpu(BHDR(bh)->h_refcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (ce)
505 mb_cache_entry_release(ce);
506 }
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800507 unlock_buffer(bh);
508out:
509 ext3_std_error(inode->i_sb, error);
510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513struct ext3_xattr_info {
514 int name_index;
515 const char *name;
516 const void *value;
517 size_t value_len;
518};
519
520struct ext3_xattr_search {
521 struct ext3_xattr_entry *first;
522 void *base;
523 void *end;
524 struct ext3_xattr_entry *here;
525 int not_found;
526};
527
528static int
529ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
530{
531 struct ext3_xattr_entry *last;
532 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
533
534 /* Compute min_offs and last. */
535 last = s->first;
536 for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
537 if (!last->e_value_block && last->e_value_size) {
538 size_t offs = le16_to_cpu(last->e_value_offs);
539 if (offs < min_offs)
540 min_offs = offs;
541 }
542 }
543 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
544 if (!s->not_found) {
545 if (!s->here->e_value_block && s->here->e_value_size) {
546 size_t size = le32_to_cpu(s->here->e_value_size);
547 free += EXT3_XATTR_SIZE(size);
548 }
549 free += EXT3_XATTR_LEN(name_len);
550 }
551 if (i->value) {
552 if (free < EXT3_XATTR_SIZE(i->value_len) ||
553 free < EXT3_XATTR_LEN(name_len) +
554 EXT3_XATTR_SIZE(i->value_len))
555 return -ENOSPC;
556 }
557
558 if (i->value && s->not_found) {
559 /* Insert the new name. */
560 size_t size = EXT3_XATTR_LEN(name_len);
561 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
562 memmove((void *)s->here + size, s->here, rest);
563 memset(s->here, 0, size);
564 s->here->e_name_index = i->name_index;
565 s->here->e_name_len = name_len;
566 memcpy(s->here->e_name, i->name, name_len);
567 } else {
568 if (!s->here->e_value_block && s->here->e_value_size) {
569 void *first_val = s->base + min_offs;
570 size_t offs = le16_to_cpu(s->here->e_value_offs);
571 void *val = s->base + offs;
572 size_t size = EXT3_XATTR_SIZE(
573 le32_to_cpu(s->here->e_value_size));
574
575 if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
576 /* The old and the new value have the same
577 size. Just replace. */
578 s->here->e_value_size =
579 cpu_to_le32(i->value_len);
580 memset(val + size - EXT3_XATTR_PAD, 0,
581 EXT3_XATTR_PAD); /* Clear pad bytes. */
582 memcpy(val, i->value, i->value_len);
583 return 0;
584 }
585
586 /* Remove the old value. */
587 memmove(first_val + size, first_val, val - first_val);
588 memset(first_val, 0, size);
589 s->here->e_value_size = 0;
590 s->here->e_value_offs = 0;
591 min_offs += size;
592
593 /* Adjust all value offsets. */
594 last = s->first;
595 while (!IS_LAST_ENTRY(last)) {
596 size_t o = le16_to_cpu(last->e_value_offs);
597 if (!last->e_value_block &&
598 last->e_value_size && o < offs)
599 last->e_value_offs =
600 cpu_to_le16(o + size);
601 last = EXT3_XATTR_NEXT(last);
602 }
603 }
604 if (!i->value) {
605 /* Remove the old name. */
606 size_t size = EXT3_XATTR_LEN(name_len);
607 last = ENTRY((void *)last - size);
608 memmove(s->here, (void *)s->here + size,
609 (void *)last - (void *)s->here + sizeof(__u32));
610 memset(last, 0, size);
611 }
612 }
613
614 if (i->value) {
615 /* Insert the new value. */
616 s->here->e_value_size = cpu_to_le32(i->value_len);
617 if (i->value_len) {
618 size_t size = EXT3_XATTR_SIZE(i->value_len);
619 void *val = s->base + min_offs - size;
620 s->here->e_value_offs = cpu_to_le16(min_offs - size);
621 memset(val + size - EXT3_XATTR_PAD, 0,
622 EXT3_XATTR_PAD); /* Clear the pad bytes. */
623 memcpy(val, i->value, i->value_len);
624 }
625 }
626 return 0;
627}
628
629struct ext3_xattr_block_find {
630 struct ext3_xattr_search s;
631 struct buffer_head *bh;
632};
633
Ben Dooks381be2542005-10-30 15:02:56 -0800634static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
636 struct ext3_xattr_block_find *bs)
637{
638 struct super_block *sb = inode->i_sb;
639 int error;
640
641 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
642 i->name_index, i->name, i->value, (long)i->value_len);
643
644 if (EXT3_I(inode)->i_file_acl) {
645 /* The inode already has an extended attribute block. */
646 bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
647 error = -EIO;
648 if (!bs->bh)
649 goto cleanup;
650 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
651 atomic_read(&(bs->bh->b_count)),
652 le32_to_cpu(BHDR(bs->bh)->h_refcount));
653 if (ext3_xattr_check_block(bs->bh)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700654 ext3_error(sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700655 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 EXT3_I(inode)->i_file_acl);
657 error = -EIO;
658 goto cleanup;
659 }
660 /* Find the named attribute. */
661 bs->s.base = BHDR(bs->bh);
662 bs->s.first = BFIRST(bs->bh);
663 bs->s.end = bs->bh->b_data + bs->bh->b_size;
664 bs->s.here = bs->s.first;
665 error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
666 i->name, bs->bh->b_size, 1);
667 if (error && error != -ENODATA)
668 goto cleanup;
669 bs->s.not_found = error;
670 }
671 error = 0;
672
673cleanup:
674 return error;
675}
676
677static int
678ext3_xattr_block_set(handle_t *handle, struct inode *inode,
679 struct ext3_xattr_info *i,
680 struct ext3_xattr_block_find *bs)
681{
682 struct super_block *sb = inode->i_sb;
683 struct buffer_head *new_bh = NULL;
684 struct ext3_xattr_search *s = &bs->s;
685 struct mb_cache_entry *ce = NULL;
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800686 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688#define header(x) ((struct ext3_xattr_header *)(x))
689
690 if (i->value && i->value_len > sb->s_blocksize)
691 return -ENOSPC;
692 if (s->base) {
693 ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
694 bs->bh->b_blocknr);
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800695 error = ext3_journal_get_write_access(handle, bs->bh);
696 if (error)
697 goto cleanup;
698 lock_buffer(bs->bh);
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
701 if (ce) {
702 mb_cache_entry_free(ce);
703 ce = NULL;
704 }
705 ea_bdebug(bs->bh, "modifying in-place");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 error = ext3_xattr_set_entry(i, s);
707 if (!error) {
708 if (!IS_LAST_ENTRY(s->first))
709 ext3_xattr_rehash(header(s->base),
710 s->here);
711 ext3_xattr_cache_insert(bs->bh);
712 }
713 unlock_buffer(bs->bh);
714 if (error == -EIO)
715 goto bad_block;
716 if (!error)
717 error = ext3_journal_dirty_metadata(handle,
718 bs->bh);
719 if (error)
720 goto cleanup;
721 goto inserted;
722 } else {
723 int offset = (char *)s->here - bs->bh->b_data;
724
Mingming Cao8a2bfdc2007-02-28 20:13:35 -0800725 unlock_buffer(bs->bh);
726 journal_release_buffer(handle, bs->bh);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (ce) {
729 mb_cache_entry_release(ce);
730 ce = NULL;
731 }
732 ea_bdebug(bs->bh, "cloning");
Josef Bacikc587f0c2008-03-19 17:00:49 -0700733 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 error = -ENOMEM;
735 if (s->base == NULL)
736 goto cleanup;
737 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
738 s->first = ENTRY(header(s->base)+1);
739 header(s->base)->h_refcount = cpu_to_le32(1);
740 s->here = ENTRY(s->base + offset);
741 s->end = s->base + bs->bh->b_size;
742 }
743 } else {
744 /* Allocate a buffer where we construct the new block. */
Josef Bacikc587f0c2008-03-19 17:00:49 -0700745 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* assert(header == s->base) */
747 error = -ENOMEM;
748 if (s->base == NULL)
749 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
751 header(s->base)->h_blocks = cpu_to_le32(1);
752 header(s->base)->h_refcount = cpu_to_le32(1);
753 s->first = ENTRY(header(s->base)+1);
754 s->here = ENTRY(header(s->base)+1);
755 s->end = s->base + sb->s_blocksize;
756 }
757
758 error = ext3_xattr_set_entry(i, s);
759 if (error == -EIO)
760 goto bad_block;
761 if (error)
762 goto cleanup;
763 if (!IS_LAST_ENTRY(s->first))
764 ext3_xattr_rehash(header(s->base), s->here);
765
766inserted:
767 if (!IS_LAST_ENTRY(s->first)) {
768 new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
769 if (new_bh) {
770 /* We found an identical block in the cache. */
771 if (new_bh == bs->bh)
772 ea_bdebug(new_bh, "keeping");
773 else {
774 /* The old block is released after updating
775 the inode. */
776 error = -EDQUOT;
Jan Kara81a05222009-01-26 16:58:01 +0100777 if (vfs_dq_alloc_block(inode, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto cleanup;
779 error = ext3_journal_get_write_access(handle,
780 new_bh);
781 if (error)
782 goto cleanup_dquot;
783 lock_buffer(new_bh);
Marcin Slusarz50e8a282008-02-08 04:20:13 -0800784 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 ea_bdebug(new_bh, "reusing; refcount now=%d",
786 le32_to_cpu(BHDR(new_bh)->h_refcount));
787 unlock_buffer(new_bh);
788 error = ext3_journal_dirty_metadata(handle,
789 new_bh);
790 if (error)
791 goto cleanup_dquot;
792 }
793 mb_cache_entry_release(ce);
794 ce = NULL;
795 } else if (bs->bh && s->base == bs->bh->b_data) {
796 /* We were modifying this block in-place. */
797 ea_bdebug(bs->bh, "keeping this block");
798 new_bh = bs->bh;
799 get_bh(new_bh);
800 } else {
801 /* We need to allocate a new block */
Akinobu Mita1eaafea2008-04-28 02:16:07 -0700802 ext3_fsblk_t goal = ext3_group_first_block_no(sb,
803 EXT3_I(inode)->i_block_group);
Mingming Cao1c2bf372006-06-25 05:48:06 -0700804 ext3_fsblk_t block = ext3_new_block(handle, inode,
805 goal, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (error)
807 goto cleanup;
808 ea_idebug(inode, "creating block %d", block);
809
810 new_bh = sb_getblk(sb, block);
811 if (!new_bh) {
812getblk_failed:
813 ext3_free_blocks(handle, inode, block, 1);
814 error = -EIO;
815 goto cleanup;
816 }
817 lock_buffer(new_bh);
818 error = ext3_journal_get_create_access(handle, new_bh);
819 if (error) {
820 unlock_buffer(new_bh);
821 goto getblk_failed;
822 }
823 memcpy(new_bh->b_data, s->base, new_bh->b_size);
824 set_buffer_uptodate(new_bh);
825 unlock_buffer(new_bh);
826 ext3_xattr_cache_insert(new_bh);
827 error = ext3_journal_dirty_metadata(handle, new_bh);
828 if (error)
829 goto cleanup;
830 }
831 }
832
833 /* Update the inode. */
834 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
835
836 /* Drop the previous xattr block. */
837 if (bs->bh && bs->bh != new_bh)
838 ext3_xattr_release_block(handle, inode, bs->bh);
839 error = 0;
840
841cleanup:
842 if (ce)
843 mb_cache_entry_release(ce);
844 brelse(new_bh);
845 if (!(bs->bh && s->base == bs->bh->b_data))
846 kfree(s->base);
847
848 return error;
849
850cleanup_dquot:
Jan Kara81a05222009-01-26 16:58:01 +0100851 vfs_dq_free_block(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto cleanup;
853
854bad_block:
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700855 ext3_error(inode->i_sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700856 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 EXT3_I(inode)->i_file_acl);
858 goto cleanup;
859
860#undef header
861}
862
863struct ext3_xattr_ibody_find {
864 struct ext3_xattr_search s;
865 struct ext3_iloc iloc;
866};
867
Ben Dooks381be2542005-10-30 15:02:56 -0800868static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
870 struct ext3_xattr_ibody_find *is)
871{
872 struct ext3_xattr_ibody_header *header;
873 struct ext3_inode *raw_inode;
874 int error;
875
876 if (EXT3_I(inode)->i_extra_isize == 0)
877 return 0;
878 raw_inode = ext3_raw_inode(&is->iloc);
879 header = IHDR(inode, raw_inode);
880 is->s.base = is->s.first = IFIRST(header);
881 is->s.here = is->s.first;
882 is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
883 if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
884 error = ext3_xattr_check_names(IFIRST(header), is->s.end);
885 if (error)
886 return error;
887 /* Find the named attribute. */
888 error = ext3_xattr_find_entry(&is->s.here, i->name_index,
889 i->name, is->s.end -
890 (void *)is->s.base, 0);
891 if (error && error != -ENODATA)
892 return error;
893 is->s.not_found = error;
894 }
895 return 0;
896}
897
898static int
899ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
900 struct ext3_xattr_info *i,
901 struct ext3_xattr_ibody_find *is)
902{
903 struct ext3_xattr_ibody_header *header;
904 struct ext3_xattr_search *s = &is->s;
905 int error;
906
907 if (EXT3_I(inode)->i_extra_isize == 0)
908 return -ENOSPC;
909 error = ext3_xattr_set_entry(i, s);
910 if (error)
911 return error;
912 header = IHDR(inode, ext3_raw_inode(&is->iloc));
913 if (!IS_LAST_ENTRY(s->first)) {
914 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
915 EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
916 } else {
917 header->h_magic = cpu_to_le32(0);
918 EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
919 }
920 return 0;
921}
922
923/*
924 * ext3_xattr_set_handle()
925 *
926 * Create, replace or remove an extended attribute for this inode. Buffer
927 * is NULL to remove an existing extended attribute, and non-NULL to
928 * either replace an existing extended attribute, or create a new extended
929 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
930 * specify that an extended attribute must exist and must not exist
931 * previous to the call, respectively.
932 *
933 * Returns 0, or a negative error number on failure.
934 */
935int
936ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
937 const char *name, const void *value, size_t value_len,
938 int flags)
939{
940 struct ext3_xattr_info i = {
941 .name_index = name_index,
942 .name = name,
943 .value = value,
944 .value_len = value_len,
945
946 };
947 struct ext3_xattr_ibody_find is = {
948 .s = { .not_found = -ENODATA, },
949 };
950 struct ext3_xattr_block_find bs = {
951 .s = { .not_found = -ENODATA, },
952 };
953 int error;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!name)
956 return -EINVAL;
957 if (strlen(name) > 255)
958 return -ERANGE;
959 down_write(&EXT3_I(inode)->xattr_sem);
960 error = ext3_get_inode_loc(inode, &is.iloc);
961 if (error)
962 goto cleanup;
963
964 if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
965 struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
966 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
967 EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
968 }
969
970 error = ext3_xattr_ibody_find(inode, &i, &is);
971 if (error)
972 goto cleanup;
973 if (is.s.not_found)
974 error = ext3_xattr_block_find(inode, &i, &bs);
975 if (error)
976 goto cleanup;
977 if (is.s.not_found && bs.s.not_found) {
978 error = -ENODATA;
979 if (flags & XATTR_REPLACE)
980 goto cleanup;
981 error = 0;
982 if (!value)
983 goto cleanup;
984 } else {
985 error = -EEXIST;
986 if (flags & XATTR_CREATE)
987 goto cleanup;
988 }
989 error = ext3_journal_get_write_access(handle, is.iloc.bh);
990 if (error)
991 goto cleanup;
992 if (!value) {
993 if (!is.s.not_found)
994 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
995 else if (!bs.s.not_found)
996 error = ext3_xattr_block_set(handle, inode, &i, &bs);
997 } else {
998 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
999 if (!error && !bs.s.not_found) {
1000 i.value = NULL;
1001 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1002 } else if (error == -ENOSPC) {
Tiger Yang7e01c8e2008-05-14 16:05:47 -07001003 if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
1004 error = ext3_xattr_block_find(inode, &i, &bs);
1005 if (error)
1006 goto cleanup;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1009 if (error)
1010 goto cleanup;
1011 if (!is.s.not_found) {
1012 i.value = NULL;
1013 error = ext3_xattr_ibody_set(handle, inode, &i,
1014 &is);
1015 }
1016 }
1017 }
1018 if (!error) {
1019 ext3_xattr_update_super_block(handle, inode->i_sb);
1020 inode->i_ctime = CURRENT_TIME_SEC;
1021 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1022 /*
1023 * The bh is consumed by ext3_mark_iloc_dirty, even with
1024 * error != 0.
1025 */
1026 is.iloc.bh = NULL;
1027 if (IS_SYNC(inode))
1028 handle->h_sync = 1;
1029 }
1030
1031cleanup:
1032 brelse(is.iloc.bh);
1033 brelse(bs.bh);
1034 up_write(&EXT3_I(inode)->xattr_sem);
1035 return error;
1036}
1037
1038/*
1039 * ext3_xattr_set()
1040 *
1041 * Like ext3_xattr_set_handle, but start from an inode. This extended
1042 * attribute modification is a filesystem transaction by itself.
1043 *
1044 * Returns 0, or a negative error number on failure.
1045 */
1046int
1047ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1048 const void *value, size_t value_len, int flags)
1049{
1050 handle_t *handle;
1051 int error, retries = 0;
1052
1053retry:
Jan Kara1f545872005-06-23 22:01:04 -07001054 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (IS_ERR(handle)) {
1056 error = PTR_ERR(handle);
1057 } else {
1058 int error2;
1059
1060 error = ext3_xattr_set_handle(handle, inode, name_index, name,
1061 value, value_len, flags);
1062 error2 = ext3_journal_stop(handle);
1063 if (error == -ENOSPC &&
1064 ext3_should_retry_alloc(inode->i_sb, &retries))
1065 goto retry;
1066 if (error == 0)
1067 error = error2;
1068 }
1069
1070 return error;
1071}
1072
1073/*
1074 * ext3_xattr_delete_inode()
1075 *
1076 * Free extended attribute resources associated with this inode. This
1077 * is called immediately before an inode is freed. We have exclusive
1078 * access to the inode.
1079 */
1080void
1081ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1082{
1083 struct buffer_head *bh = NULL;
1084
1085 if (!EXT3_I(inode)->i_file_acl)
1086 goto cleanup;
1087 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1088 if (!bh) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001089 ext3_error(inode->i_sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -07001090 "inode %lu: block "E3FSBLK" read error", inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 EXT3_I(inode)->i_file_acl);
1092 goto cleanup;
1093 }
1094 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1095 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001096 ext3_error(inode->i_sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -07001097 "inode %lu: bad block "E3FSBLK, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 EXT3_I(inode)->i_file_acl);
1099 goto cleanup;
1100 }
1101 ext3_xattr_release_block(handle, inode, bh);
1102 EXT3_I(inode)->i_file_acl = 0;
1103
1104cleanup:
1105 brelse(bh);
1106}
1107
1108/*
1109 * ext3_xattr_put_super()
1110 *
1111 * This is called when a file system is unmounted.
1112 */
1113void
1114ext3_xattr_put_super(struct super_block *sb)
1115{
Andreas Gruenbacher8c52ab42005-07-27 11:45:15 -07001116 mb_cache_shrink(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119/*
1120 * ext3_xattr_cache_insert()
1121 *
1122 * Create a new entry in the extended attribute cache, and insert
1123 * it unless such an entry is already in the cache.
1124 *
1125 * Returns 0, or a negative error number on failure.
1126 */
1127static void
1128ext3_xattr_cache_insert(struct buffer_head *bh)
1129{
1130 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1131 struct mb_cache_entry *ce;
1132 int error;
1133
Jan Kara335e92e2008-04-15 14:34:43 -07001134 ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!ce) {
1136 ea_bdebug(bh, "out of memory");
1137 return;
1138 }
1139 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
1140 if (error) {
1141 mb_cache_entry_free(ce);
1142 if (error == -EBUSY) {
1143 ea_bdebug(bh, "already in cache");
1144 error = 0;
1145 }
1146 } else {
1147 ea_bdebug(bh, "inserting [%x]", (int)hash);
1148 mb_cache_entry_release(ce);
1149 }
1150}
1151
1152/*
1153 * ext3_xattr_cmp()
1154 *
1155 * Compare two extended attribute blocks for equality.
1156 *
1157 * Returns 0 if the blocks are equal, 1 if they differ, and
1158 * a negative error number on errors.
1159 */
1160static int
1161ext3_xattr_cmp(struct ext3_xattr_header *header1,
1162 struct ext3_xattr_header *header2)
1163{
1164 struct ext3_xattr_entry *entry1, *entry2;
1165
1166 entry1 = ENTRY(header1+1);
1167 entry2 = ENTRY(header2+1);
1168 while (!IS_LAST_ENTRY(entry1)) {
1169 if (IS_LAST_ENTRY(entry2))
1170 return 1;
1171 if (entry1->e_hash != entry2->e_hash ||
1172 entry1->e_name_index != entry2->e_name_index ||
1173 entry1->e_name_len != entry2->e_name_len ||
1174 entry1->e_value_size != entry2->e_value_size ||
1175 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1176 return 1;
1177 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1178 return -EIO;
1179 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1180 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1181 le32_to_cpu(entry1->e_value_size)))
1182 return 1;
1183
1184 entry1 = EXT3_XATTR_NEXT(entry1);
1185 entry2 = EXT3_XATTR_NEXT(entry2);
1186 }
1187 if (!IS_LAST_ENTRY(entry2))
1188 return 1;
1189 return 0;
1190}
1191
1192/*
1193 * ext3_xattr_cache_find()
1194 *
1195 * Find an identical extended attribute block.
1196 *
1197 * Returns a pointer to the block found, or NULL if such a block was
1198 * not found or an error occurred.
1199 */
1200static struct buffer_head *
1201ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1202 struct mb_cache_entry **pce)
1203{
1204 __u32 hash = le32_to_cpu(header->h_hash);
1205 struct mb_cache_entry *ce;
1206
1207 if (!header->h_hash)
1208 return NULL; /* never share */
1209 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1210again:
1211 ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
1212 inode->i_sb->s_bdev, hash);
1213 while (ce) {
1214 struct buffer_head *bh;
1215
1216 if (IS_ERR(ce)) {
1217 if (PTR_ERR(ce) == -EAGAIN)
1218 goto again;
1219 break;
1220 }
1221 bh = sb_bread(inode->i_sb, ce->e_block);
1222 if (!bh) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001223 ext3_error(inode->i_sb, __func__,
Eric Sandeeneee194e2006-09-27 01:49:30 -07001224 "inode %lu: block %lu read error",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 inode->i_ino, (unsigned long) ce->e_block);
1226 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1227 EXT3_XATTR_REFCOUNT_MAX) {
Mingming Cao1c2bf372006-06-25 05:48:06 -07001228 ea_idebug(inode, "block %lu refcount %d>=%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 (unsigned long) ce->e_block,
1230 le32_to_cpu(BHDR(bh)->h_refcount),
1231 EXT3_XATTR_REFCOUNT_MAX);
1232 } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1233 *pce = ce;
1234 return bh;
1235 }
1236 brelse(bh);
1237 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1238 }
1239 return NULL;
1240}
1241
1242#define NAME_HASH_SHIFT 5
1243#define VALUE_HASH_SHIFT 16
1244
1245/*
1246 * ext3_xattr_hash_entry()
1247 *
1248 * Compute the hash of an extended attribute.
1249 */
1250static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1251 struct ext3_xattr_entry *entry)
1252{
1253 __u32 hash = 0;
1254 char *name = entry->e_name;
1255 int n;
1256
1257 for (n=0; n < entry->e_name_len; n++) {
1258 hash = (hash << NAME_HASH_SHIFT) ^
1259 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1260 *name++;
1261 }
1262
1263 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1264 __le32 *value = (__le32 *)((char *)header +
1265 le16_to_cpu(entry->e_value_offs));
1266 for (n = (le32_to_cpu(entry->e_value_size) +
1267 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1268 hash = (hash << VALUE_HASH_SHIFT) ^
1269 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1270 le32_to_cpu(*value++);
1271 }
1272 }
1273 entry->e_hash = cpu_to_le32(hash);
1274}
1275
1276#undef NAME_HASH_SHIFT
1277#undef VALUE_HASH_SHIFT
1278
1279#define BLOCK_HASH_SHIFT 16
1280
1281/*
1282 * ext3_xattr_rehash()
1283 *
1284 * Re-compute the extended attribute hash value after an entry has changed.
1285 */
1286static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1287 struct ext3_xattr_entry *entry)
1288{
1289 struct ext3_xattr_entry *here;
1290 __u32 hash = 0;
1291
1292 ext3_xattr_hash_entry(header, entry);
1293 here = ENTRY(header+1);
1294 while (!IS_LAST_ENTRY(here)) {
1295 if (!here->e_hash) {
1296 /* Block is not shared if an entry's hash value == 0 */
1297 hash = 0;
1298 break;
1299 }
1300 hash = (hash << BLOCK_HASH_SHIFT) ^
1301 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1302 le32_to_cpu(here->e_hash);
1303 here = EXT3_XATTR_NEXT(here);
1304 }
1305 header->h_hash = cpu_to_le32(hash);
1306}
1307
1308#undef BLOCK_HASH_SHIFT
1309
1310int __init
1311init_ext3_xattr(void)
1312{
1313 ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
1314 sizeof(struct mb_cache_entry) +
1315 sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
1316 if (!ext3_xattr_cache)
1317 return -ENOMEM;
1318 return 0;
1319}
1320
1321void
1322exit_ext3_xattr(void)
1323{
1324 if (ext3_xattr_cache)
1325 mb_cache_destroy(ext3_xattr_cache);
1326 ext3_xattr_cache = NULL;
1327}