GNU Linux-libre 4.9.308-gnu1
[releases.git] / fs / ext2 / xattr.c
1 /*
2  * linux/fs/ext2/xattr.c
3  *
4  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
5  *
6  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7  * Extended attributes for symlinks and special files added per
8  *  suggestion of Luka Renko <luka.renko@hermes.si>.
9  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
10  *  Red Hat Inc.
11  *
12  */
13
14 /*
15  * Extended attributes are stored on disk blocks allocated outside of
16  * any inode. The i_file_acl field is then made to point to this allocated
17  * block. If all extended attributes of an inode are identical, these
18  * inodes may share the same extended attribute block. Such situations
19  * are automatically detected by keeping a cache of recent attribute block
20  * numbers and hashes over the block's contents in memory.
21  *
22  *
23  * Extended attribute block layout:
24  *
25  *   +------------------+
26  *   | header           |
27  *   | entry 1          | |
28  *   | entry 2          | | growing downwards
29  *   | entry 3          | v
30  *   | four null bytes  |
31  *   | . . .            |
32  *   | value 1          | ^
33  *   | value 3          | | growing upwards
34  *   | value 2          | |
35  *   +------------------+
36  *
37  * The block header is followed by multiple entry descriptors. These entry
38  * descriptors are variable in size, and aligned to EXT2_XATTR_PAD
39  * byte boundaries. The entry descriptors are sorted by attribute name,
40  * so that two extended attribute blocks can be compared efficiently.
41  *
42  * Attribute values are aligned to the end of the block, stored in
43  * no specific order. They are also padded to EXT2_XATTR_PAD byte
44  * boundaries. No additional gaps are left between them.
45  *
46  * Locking strategy
47  * ----------------
48  * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
49  * EA blocks are only changed if they are exclusive to an inode, so
50  * holding xattr_sem also means that nothing but the EA block's reference
51  * count will change. Multiple writers to an EA block are synchronized
52  * by the bh lock. No more than a single bh lock is held at any time
53  * to avoid deadlocks.
54  */
55
56 #include <linux/buffer_head.h>
57 #include <linux/init.h>
58 #include <linux/printk.h>
59 #include <linux/slab.h>
60 #include <linux/mbcache.h>
61 #include <linux/quotaops.h>
62 #include <linux/rwsem.h>
63 #include <linux/security.h>
64 #include "ext2.h"
65 #include "xattr.h"
66 #include "acl.h"
67
68 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
69 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
70 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
71 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
72
73 #ifdef EXT2_XATTR_DEBUG
74 # define ea_idebug(inode, f...) do { \
75                 printk(KERN_DEBUG "inode %s:%ld: ", \
76                         inode->i_sb->s_id, inode->i_ino); \
77                 printk(f); \
78                 printk("\n"); \
79         } while (0)
80 # define ea_bdebug(bh, f...) do { \
81                 printk(KERN_DEBUG "block %pg:%lu: ", \
82                         bh->b_bdev, (unsigned long) bh->b_blocknr); \
83                 printk(f); \
84                 printk("\n"); \
85         } while (0)
86 #else
87 # define ea_idebug(inode, f...) no_printk(f)
88 # define ea_bdebug(bh, f...)    no_printk(f)
89 #endif
90
91 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
92                            struct ext2_xattr_header *);
93
94 static int ext2_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
95 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
96                                                  struct ext2_xattr_header *);
97 static void ext2_xattr_rehash(struct ext2_xattr_header *,
98                               struct ext2_xattr_entry *);
99
100 static const struct xattr_handler *ext2_xattr_handler_map[] = {
101         [EXT2_XATTR_INDEX_USER]              = &ext2_xattr_user_handler,
102 #ifdef CONFIG_EXT2_FS_POSIX_ACL
103         [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
104         [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
105 #endif
106         [EXT2_XATTR_INDEX_TRUSTED]           = &ext2_xattr_trusted_handler,
107 #ifdef CONFIG_EXT2_FS_SECURITY
108         [EXT2_XATTR_INDEX_SECURITY]          = &ext2_xattr_security_handler,
109 #endif
110 };
111
112 const struct xattr_handler *ext2_xattr_handlers[] = {
113         &ext2_xattr_user_handler,
114         &ext2_xattr_trusted_handler,
115 #ifdef CONFIG_EXT2_FS_POSIX_ACL
116         &posix_acl_access_xattr_handler,
117         &posix_acl_default_xattr_handler,
118 #endif
119 #ifdef CONFIG_EXT2_FS_SECURITY
120         &ext2_xattr_security_handler,
121 #endif
122         NULL
123 };
124
125 static inline const struct xattr_handler *
126 ext2_xattr_handler(int name_index)
127 {
128         const struct xattr_handler *handler = NULL;
129
130         if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
131                 handler = ext2_xattr_handler_map[name_index];
132         return handler;
133 }
134
135 /*
136  * ext2_xattr_get()
137  *
138  * Copy an extended attribute into the buffer
139  * provided, or compute the buffer size required.
140  * Buffer is NULL to compute the size of the buffer required.
141  *
142  * Returns a negative error number on failure, or the number of bytes
143  * used / required on success.
144  */
145 int
146 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
147                void *buffer, size_t buffer_size)
148 {
149         struct buffer_head *bh = NULL;
150         struct ext2_xattr_entry *entry;
151         size_t name_len, size;
152         char *end;
153         int error;
154         struct mb_cache *ext2_mb_cache = EXT2_SB(inode->i_sb)->s_mb_cache;
155
156         ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
157                   name_index, name, buffer, (long)buffer_size);
158
159         if (name == NULL)
160                 return -EINVAL;
161         name_len = strlen(name);
162         if (name_len > 255)
163                 return -ERANGE;
164
165         down_read(&EXT2_I(inode)->xattr_sem);
166         error = -ENODATA;
167         if (!EXT2_I(inode)->i_file_acl)
168                 goto cleanup;
169         ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
170         bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
171         error = -EIO;
172         if (!bh)
173                 goto cleanup;
174         ea_bdebug(bh, "b_count=%d, refcount=%d",
175                 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
176         end = bh->b_data + bh->b_size;
177         if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
178             HDR(bh)->h_blocks != cpu_to_le32(1)) {
179 bad_block:      ext2_error(inode->i_sb, "ext2_xattr_get",
180                         "inode %ld: bad block %d", inode->i_ino,
181                         EXT2_I(inode)->i_file_acl);
182                 error = -EIO;
183                 goto cleanup;
184         }
185
186         /* find named attribute */
187         entry = FIRST_ENTRY(bh);
188         while (!IS_LAST_ENTRY(entry)) {
189                 struct ext2_xattr_entry *next =
190                         EXT2_XATTR_NEXT(entry);
191                 if ((char *)next >= end)
192                         goto bad_block;
193                 if (name_index == entry->e_name_index &&
194                     name_len == entry->e_name_len &&
195                     memcmp(name, entry->e_name, name_len) == 0)
196                         goto found;
197                 entry = next;
198         }
199         if (ext2_xattr_cache_insert(ext2_mb_cache, bh))
200                 ea_idebug(inode, "cache insert failed");
201         error = -ENODATA;
202         goto cleanup;
203 found:
204         /* check the buffer size */
205         if (entry->e_value_block != 0)
206                 goto bad_block;
207         size = le32_to_cpu(entry->e_value_size);
208         if (size > inode->i_sb->s_blocksize ||
209             le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
210                 goto bad_block;
211
212         if (ext2_xattr_cache_insert(ext2_mb_cache, bh))
213                 ea_idebug(inode, "cache insert failed");
214         if (buffer) {
215                 error = -ERANGE;
216                 if (size > buffer_size)
217                         goto cleanup;
218                 /* return value of attribute */
219                 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
220                         size);
221         }
222         error = size;
223
224 cleanup:
225         brelse(bh);
226         up_read(&EXT2_I(inode)->xattr_sem);
227
228         return error;
229 }
230
231 /*
232  * ext2_xattr_list()
233  *
234  * Copy a list of attribute names into the buffer
235  * provided, or compute the buffer size required.
236  * Buffer is NULL to compute the size of the buffer required.
237  *
238  * Returns a negative error number on failure, or the number of bytes
239  * used / required on success.
240  */
241 static int
242 ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
243 {
244         struct inode *inode = d_inode(dentry);
245         struct buffer_head *bh = NULL;
246         struct ext2_xattr_entry *entry;
247         char *end;
248         size_t rest = buffer_size;
249         int error;
250         struct mb_cache *ext2_mb_cache = EXT2_SB(inode->i_sb)->s_mb_cache;
251
252         ea_idebug(inode, "buffer=%p, buffer_size=%ld",
253                   buffer, (long)buffer_size);
254
255         down_read(&EXT2_I(inode)->xattr_sem);
256         error = 0;
257         if (!EXT2_I(inode)->i_file_acl)
258                 goto cleanup;
259         ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
260         bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
261         error = -EIO;
262         if (!bh)
263                 goto cleanup;
264         ea_bdebug(bh, "b_count=%d, refcount=%d",
265                 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
266         end = bh->b_data + bh->b_size;
267         if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
268             HDR(bh)->h_blocks != cpu_to_le32(1)) {
269 bad_block:      ext2_error(inode->i_sb, "ext2_xattr_list",
270                         "inode %ld: bad block %d", inode->i_ino,
271                         EXT2_I(inode)->i_file_acl);
272                 error = -EIO;
273                 goto cleanup;
274         }
275
276         /* check the on-disk data structure */
277         entry = FIRST_ENTRY(bh);
278         while (!IS_LAST_ENTRY(entry)) {
279                 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(entry);
280
281                 if ((char *)next >= end)
282                         goto bad_block;
283                 entry = next;
284         }
285         if (ext2_xattr_cache_insert(ext2_mb_cache, bh))
286                 ea_idebug(inode, "cache insert failed");
287
288         /* list the attribute names */
289         for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
290              entry = EXT2_XATTR_NEXT(entry)) {
291                 const struct xattr_handler *handler =
292                         ext2_xattr_handler(entry->e_name_index);
293
294                 if (handler && (!handler->list || handler->list(dentry))) {
295                         const char *prefix = handler->prefix ?: handler->name;
296                         size_t prefix_len = strlen(prefix);
297                         size_t size = prefix_len + entry->e_name_len + 1;
298
299                         if (buffer) {
300                                 if (size > rest) {
301                                         error = -ERANGE;
302                                         goto cleanup;
303                                 }
304                                 memcpy(buffer, prefix, prefix_len);
305                                 buffer += prefix_len;
306                                 memcpy(buffer, entry->e_name, entry->e_name_len);
307                                 buffer += entry->e_name_len;
308                                 *buffer++ = 0;
309                         }
310                         rest -= size;
311                 }
312         }
313         error = buffer_size - rest;  /* total size */
314
315 cleanup:
316         brelse(bh);
317         up_read(&EXT2_I(inode)->xattr_sem);
318
319         return error;
320 }
321
322 /*
323  * Inode operation listxattr()
324  *
325  * d_inode(dentry)->i_mutex: don't care
326  */
327 ssize_t
328 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
329 {
330         return ext2_xattr_list(dentry, buffer, size);
331 }
332
333 /*
334  * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
335  * not set, set it.
336  */
337 static void ext2_xattr_update_super_block(struct super_block *sb)
338 {
339         if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
340                 return;
341
342         spin_lock(&EXT2_SB(sb)->s_lock);
343         EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
344         spin_unlock(&EXT2_SB(sb)->s_lock);
345         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
346 }
347
348 /*
349  * ext2_xattr_set()
350  *
351  * Create, replace or remove an extended attribute for this inode.  Value
352  * is NULL to remove an existing extended attribute, and non-NULL to
353  * either replace an existing extended attribute, or create a new extended
354  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
355  * specify that an extended attribute must exist and must not exist
356  * previous to the call, respectively.
357  *
358  * Returns 0, or a negative error number on failure.
359  */
360 int
361 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
362                const void *value, size_t value_len, int flags)
363 {
364         struct super_block *sb = inode->i_sb;
365         struct buffer_head *bh = NULL;
366         struct ext2_xattr_header *header = NULL;
367         struct ext2_xattr_entry *here, *last;
368         size_t name_len, free, min_offs = sb->s_blocksize;
369         int not_found = 1, error;
370         char *end;
371         
372         /*
373          * header -- Points either into bh, or to a temporarily
374          *           allocated buffer.
375          * here -- The named entry found, or the place for inserting, within
376          *         the block pointed to by header.
377          * last -- Points right after the last named entry within the block
378          *         pointed to by header.
379          * min_offs -- The offset of the first value (values are aligned
380          *             towards the end of the block).
381          * end -- Points right after the block pointed to by header.
382          */
383         
384         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
385                   name_index, name, value, (long)value_len);
386
387         if (value == NULL)
388                 value_len = 0;
389         if (name == NULL)
390                 return -EINVAL;
391         name_len = strlen(name);
392         if (name_len > 255 || value_len > sb->s_blocksize)
393                 return -ERANGE;
394         down_write(&EXT2_I(inode)->xattr_sem);
395         if (EXT2_I(inode)->i_file_acl) {
396                 /* The inode already has an extended attribute block. */
397                 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
398                 error = -EIO;
399                 if (!bh)
400                         goto cleanup;
401                 ea_bdebug(bh, "b_count=%d, refcount=%d",
402                         atomic_read(&(bh->b_count)),
403                         le32_to_cpu(HDR(bh)->h_refcount));
404                 header = HDR(bh);
405                 end = bh->b_data + bh->b_size;
406                 if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
407                     header->h_blocks != cpu_to_le32(1)) {
408 bad_block:              ext2_error(sb, "ext2_xattr_set",
409                                 "inode %ld: bad block %d", inode->i_ino, 
410                                    EXT2_I(inode)->i_file_acl);
411                         error = -EIO;
412                         goto cleanup;
413                 }
414                 /* Find the named attribute. */
415                 here = FIRST_ENTRY(bh);
416                 while (!IS_LAST_ENTRY(here)) {
417                         struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
418                         if ((char *)next >= end)
419                                 goto bad_block;
420                         if (!here->e_value_block && here->e_value_size) {
421                                 size_t offs = le16_to_cpu(here->e_value_offs);
422                                 if (offs < min_offs)
423                                         min_offs = offs;
424                         }
425                         not_found = name_index - here->e_name_index;
426                         if (!not_found)
427                                 not_found = name_len - here->e_name_len;
428                         if (!not_found)
429                                 not_found = memcmp(name, here->e_name,name_len);
430                         if (not_found <= 0)
431                                 break;
432                         here = next;
433                 }
434                 last = here;
435                 /* We still need to compute min_offs and last. */
436                 while (!IS_LAST_ENTRY(last)) {
437                         struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
438                         if ((char *)next >= end)
439                                 goto bad_block;
440                         if (!last->e_value_block && last->e_value_size) {
441                                 size_t offs = le16_to_cpu(last->e_value_offs);
442                                 if (offs < min_offs)
443                                         min_offs = offs;
444                         }
445                         last = next;
446                 }
447
448                 /* Check whether we have enough space left. */
449                 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
450         } else {
451                 /* We will use a new extended attribute block. */
452                 free = sb->s_blocksize -
453                         sizeof(struct ext2_xattr_header) - sizeof(__u32);
454                 here = last = NULL;  /* avoid gcc uninitialized warning. */
455         }
456
457         if (not_found) {
458                 /* Request to remove a nonexistent attribute? */
459                 error = -ENODATA;
460                 if (flags & XATTR_REPLACE)
461                         goto cleanup;
462                 error = 0;
463                 if (value == NULL)
464                         goto cleanup;
465         } else {
466                 /* Request to create an existing attribute? */
467                 error = -EEXIST;
468                 if (flags & XATTR_CREATE)
469                         goto cleanup;
470                 if (!here->e_value_block && here->e_value_size) {
471                         size_t size = le32_to_cpu(here->e_value_size);
472
473                         if (le16_to_cpu(here->e_value_offs) + size > 
474                             sb->s_blocksize || size > sb->s_blocksize)
475                                 goto bad_block;
476                         free += EXT2_XATTR_SIZE(size);
477                 }
478                 free += EXT2_XATTR_LEN(name_len);
479         }
480         error = -ENOSPC;
481         if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
482                 goto cleanup;
483
484         /* Here we know that we can set the new attribute. */
485
486         if (header) {
487                 /* assert(header == HDR(bh)); */
488                 lock_buffer(bh);
489                 if (header->h_refcount == cpu_to_le32(1)) {
490                         __u32 hash = le32_to_cpu(header->h_hash);
491
492                         ea_bdebug(bh, "modifying in-place");
493                         /*
494                          * This must happen under buffer lock for
495                          * ext2_xattr_set2() to reliably detect modified block
496                          */
497                         mb_cache_entry_delete_block(EXT2_SB(sb)->s_mb_cache,
498                                                     hash, bh->b_blocknr);
499
500                         /* keep the buffer locked while modifying it. */
501                 } else {
502                         int offset;
503
504                         unlock_buffer(bh);
505                         ea_bdebug(bh, "cloning");
506                         header = kmalloc(bh->b_size, GFP_KERNEL);
507                         error = -ENOMEM;
508                         if (header == NULL)
509                                 goto cleanup;
510                         memcpy(header, HDR(bh), bh->b_size);
511                         header->h_refcount = cpu_to_le32(1);
512
513                         offset = (char *)here - bh->b_data;
514                         here = ENTRY((char *)header + offset);
515                         offset = (char *)last - bh->b_data;
516                         last = ENTRY((char *)header + offset);
517                 }
518         } else {
519                 /* Allocate a buffer where we construct the new block. */
520                 header = kzalloc(sb->s_blocksize, GFP_KERNEL);
521                 error = -ENOMEM;
522                 if (header == NULL)
523                         goto cleanup;
524                 end = (char *)header + sb->s_blocksize;
525                 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
526                 header->h_blocks = header->h_refcount = cpu_to_le32(1);
527                 last = here = ENTRY(header+1);
528         }
529
530         /* Iff we are modifying the block in-place, bh is locked here. */
531
532         if (not_found) {
533                 /* Insert the new name. */
534                 size_t size = EXT2_XATTR_LEN(name_len);
535                 size_t rest = (char *)last - (char *)here;
536                 memmove((char *)here + size, here, rest);
537                 memset(here, 0, size);
538                 here->e_name_index = name_index;
539                 here->e_name_len = name_len;
540                 memcpy(here->e_name, name, name_len);
541         } else {
542                 if (!here->e_value_block && here->e_value_size) {
543                         char *first_val = (char *)header + min_offs;
544                         size_t offs = le16_to_cpu(here->e_value_offs);
545                         char *val = (char *)header + offs;
546                         size_t size = EXT2_XATTR_SIZE(
547                                 le32_to_cpu(here->e_value_size));
548
549                         if (size == EXT2_XATTR_SIZE(value_len)) {
550                                 /* The old and the new value have the same
551                                    size. Just replace. */
552                                 here->e_value_size = cpu_to_le32(value_len);
553                                 memset(val + size - EXT2_XATTR_PAD, 0,
554                                        EXT2_XATTR_PAD); /* Clear pad bytes. */
555                                 memcpy(val, value, value_len);
556                                 goto skip_replace;
557                         }
558
559                         /* Remove the old value. */
560                         memmove(first_val + size, first_val, val - first_val);
561                         memset(first_val, 0, size);
562                         here->e_value_offs = 0;
563                         min_offs += size;
564
565                         /* Adjust all value offsets. */
566                         last = ENTRY(header+1);
567                         while (!IS_LAST_ENTRY(last)) {
568                                 size_t o = le16_to_cpu(last->e_value_offs);
569                                 if (!last->e_value_block && o < offs)
570                                         last->e_value_offs =
571                                                 cpu_to_le16(o + size);
572                                 last = EXT2_XATTR_NEXT(last);
573                         }
574                 }
575                 if (value == NULL) {
576                         /* Remove the old name. */
577                         size_t size = EXT2_XATTR_LEN(name_len);
578                         last = ENTRY((char *)last - size);
579                         memmove(here, (char*)here + size,
580                                 (char*)last - (char*)here);
581                         memset(last, 0, size);
582                 }
583         }
584
585         if (value != NULL) {
586                 /* Insert the new value. */
587                 here->e_value_size = cpu_to_le32(value_len);
588                 if (value_len) {
589                         size_t size = EXT2_XATTR_SIZE(value_len);
590                         char *val = (char *)header + min_offs - size;
591                         here->e_value_offs =
592                                 cpu_to_le16((char *)val - (char *)header);
593                         memset(val + size - EXT2_XATTR_PAD, 0,
594                                EXT2_XATTR_PAD); /* Clear the pad bytes. */
595                         memcpy(val, value, value_len);
596                 }
597         }
598
599 skip_replace:
600         if (IS_LAST_ENTRY(ENTRY(header+1))) {
601                 /* This block is now empty. */
602                 if (bh && header == HDR(bh))
603                         unlock_buffer(bh);  /* we were modifying in-place. */
604                 error = ext2_xattr_set2(inode, bh, NULL);
605         } else {
606                 ext2_xattr_rehash(header, here);
607                 if (bh && header == HDR(bh))
608                         unlock_buffer(bh);  /* we were modifying in-place. */
609                 error = ext2_xattr_set2(inode, bh, header);
610         }
611
612 cleanup:
613         if (!(bh && header == HDR(bh)))
614                 kfree(header);
615         brelse(bh);
616         up_write(&EXT2_I(inode)->xattr_sem);
617
618         return error;
619 }
620
621 /*
622  * Second half of ext2_xattr_set(): Update the file system.
623  */
624 static int
625 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
626                 struct ext2_xattr_header *header)
627 {
628         struct super_block *sb = inode->i_sb;
629         struct buffer_head *new_bh = NULL;
630         int error;
631         struct mb_cache *ext2_mb_cache = EXT2_SB(sb)->s_mb_cache;
632
633         if (header) {
634                 new_bh = ext2_xattr_cache_find(inode, header);
635                 if (new_bh) {
636                         /* We found an identical block in the cache. */
637                         if (new_bh == old_bh) {
638                                 ea_bdebug(new_bh, "keeping this block");
639                         } else {
640                                 /* The old block is released after updating
641                                    the inode.  */
642                                 ea_bdebug(new_bh, "reusing block");
643
644                                 error = dquot_alloc_block(inode, 1);
645                                 if (error) {
646                                         unlock_buffer(new_bh);
647                                         goto cleanup;
648                                 }
649                                 le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
650                                 ea_bdebug(new_bh, "refcount now=%d",
651                                         le32_to_cpu(HDR(new_bh)->h_refcount));
652                         }
653                         unlock_buffer(new_bh);
654                 } else if (old_bh && header == HDR(old_bh)) {
655                         /* Keep this block. No need to lock the block as we
656                            don't need to change the reference count. */
657                         new_bh = old_bh;
658                         get_bh(new_bh);
659                         ext2_xattr_cache_insert(ext2_mb_cache, new_bh);
660                 } else {
661                         /* We need to allocate a new block */
662                         ext2_fsblk_t goal = ext2_group_first_block_no(sb,
663                                                 EXT2_I(inode)->i_block_group);
664                         int block = ext2_new_block(inode, goal, &error);
665                         if (error)
666                                 goto cleanup;
667                         ea_idebug(inode, "creating block %d", block);
668
669                         new_bh = sb_getblk(sb, block);
670                         if (unlikely(!new_bh)) {
671                                 ext2_free_blocks(inode, block, 1);
672                                 mark_inode_dirty(inode);
673                                 error = -ENOMEM;
674                                 goto cleanup;
675                         }
676                         lock_buffer(new_bh);
677                         memcpy(new_bh->b_data, header, new_bh->b_size);
678                         set_buffer_uptodate(new_bh);
679                         unlock_buffer(new_bh);
680                         ext2_xattr_cache_insert(ext2_mb_cache, new_bh);
681                         
682                         ext2_xattr_update_super_block(sb);
683                 }
684                 mark_buffer_dirty(new_bh);
685                 if (IS_SYNC(inode)) {
686                         sync_dirty_buffer(new_bh);
687                         error = -EIO;
688                         if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
689                                 goto cleanup;
690                 }
691         }
692
693         /* Update the inode. */
694         EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
695         inode->i_ctime = current_time(inode);
696         if (IS_SYNC(inode)) {
697                 error = sync_inode_metadata(inode, 1);
698                 /* In case sync failed due to ENOSPC the inode was actually
699                  * written (only some dirty data were not) so we just proceed
700                  * as if nothing happened and cleanup the unused block */
701                 if (error && error != -ENOSPC) {
702                         if (new_bh && new_bh != old_bh) {
703                                 dquot_free_block_nodirty(inode, 1);
704                                 mark_inode_dirty(inode);
705                         }
706                         goto cleanup;
707                 }
708         } else
709                 mark_inode_dirty(inode);
710
711         error = 0;
712         if (old_bh && old_bh != new_bh) {
713                 /*
714                  * If there was an old block and we are no longer using it,
715                  * release the old block.
716                  */
717                 lock_buffer(old_bh);
718                 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
719                         __u32 hash = le32_to_cpu(HDR(old_bh)->h_hash);
720
721                         /*
722                          * This must happen under buffer lock for
723                          * ext2_xattr_set2() to reliably detect freed block
724                          */
725                         mb_cache_entry_delete_block(ext2_mb_cache,
726                                                     hash, old_bh->b_blocknr);
727                         /* Free the old block. */
728                         ea_bdebug(old_bh, "freeing");
729                         ext2_free_blocks(inode, old_bh->b_blocknr, 1);
730                         mark_inode_dirty(inode);
731                         /* We let our caller release old_bh, so we
732                          * need to duplicate the buffer before. */
733                         get_bh(old_bh);
734                         bforget(old_bh);
735                 } else {
736                         /* Decrement the refcount only. */
737                         le32_add_cpu(&HDR(old_bh)->h_refcount, -1);
738                         dquot_free_block_nodirty(inode, 1);
739                         mark_inode_dirty(inode);
740                         mark_buffer_dirty(old_bh);
741                         ea_bdebug(old_bh, "refcount now=%d",
742                                 le32_to_cpu(HDR(old_bh)->h_refcount));
743                 }
744                 unlock_buffer(old_bh);
745         }
746
747 cleanup:
748         brelse(new_bh);
749
750         return error;
751 }
752
753 /*
754  * ext2_xattr_delete_inode()
755  *
756  * Free extended attribute resources associated with this inode. This
757  * is called immediately before an inode is freed.
758  */
759 void
760 ext2_xattr_delete_inode(struct inode *inode)
761 {
762         struct buffer_head *bh = NULL;
763         struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
764
765         down_write(&EXT2_I(inode)->xattr_sem);
766         if (!EXT2_I(inode)->i_file_acl)
767                 goto cleanup;
768
769         if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 0)) {
770                 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
771                         "inode %ld: xattr block %d is out of data blocks range",
772                         inode->i_ino, EXT2_I(inode)->i_file_acl);
773                 goto cleanup;
774         }
775
776         bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
777         if (!bh) {
778                 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
779                         "inode %ld: block %d read error", inode->i_ino,
780                         EXT2_I(inode)->i_file_acl);
781                 goto cleanup;
782         }
783         ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
784         if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
785             HDR(bh)->h_blocks != cpu_to_le32(1)) {
786                 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
787                         "inode %ld: bad block %d", inode->i_ino,
788                         EXT2_I(inode)->i_file_acl);
789                 goto cleanup;
790         }
791         lock_buffer(bh);
792         if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
793                 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
794
795                 /*
796                  * This must happen under buffer lock for ext2_xattr_set2() to
797                  * reliably detect freed block
798                  */
799                 mb_cache_entry_delete_block(EXT2_SB(inode->i_sb)->s_mb_cache,
800                                             hash, bh->b_blocknr);
801                 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
802                 get_bh(bh);
803                 bforget(bh);
804                 unlock_buffer(bh);
805         } else {
806                 le32_add_cpu(&HDR(bh)->h_refcount, -1);
807                 ea_bdebug(bh, "refcount now=%d",
808                         le32_to_cpu(HDR(bh)->h_refcount));
809                 unlock_buffer(bh);
810                 mark_buffer_dirty(bh);
811                 if (IS_SYNC(inode))
812                         sync_dirty_buffer(bh);
813                 dquot_free_block_nodirty(inode, 1);
814         }
815         EXT2_I(inode)->i_file_acl = 0;
816
817 cleanup:
818         brelse(bh);
819         up_write(&EXT2_I(inode)->xattr_sem);
820 }
821
822 /*
823  * ext2_xattr_cache_insert()
824  *
825  * Create a new entry in the extended attribute cache, and insert
826  * it unless such an entry is already in the cache.
827  *
828  * Returns 0, or a negative error number on failure.
829  */
830 static int
831 ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
832 {
833         __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
834         int error;
835
836         error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr, 1);
837         if (error) {
838                 if (error == -EBUSY) {
839                         ea_bdebug(bh, "already in cache");
840                         error = 0;
841                 }
842         } else
843                 ea_bdebug(bh, "inserting [%x]", (int)hash);
844         return error;
845 }
846
847 /*
848  * ext2_xattr_cmp()
849  *
850  * Compare two extended attribute blocks for equality.
851  *
852  * Returns 0 if the blocks are equal, 1 if they differ, and
853  * a negative error number on errors.
854  */
855 static int
856 ext2_xattr_cmp(struct ext2_xattr_header *header1,
857                struct ext2_xattr_header *header2)
858 {
859         struct ext2_xattr_entry *entry1, *entry2;
860
861         entry1 = ENTRY(header1+1);
862         entry2 = ENTRY(header2+1);
863         while (!IS_LAST_ENTRY(entry1)) {
864                 if (IS_LAST_ENTRY(entry2))
865                         return 1;
866                 if (entry1->e_hash != entry2->e_hash ||
867                     entry1->e_name_index != entry2->e_name_index ||
868                     entry1->e_name_len != entry2->e_name_len ||
869                     entry1->e_value_size != entry2->e_value_size ||
870                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
871                         return 1;
872                 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
873                         return -EIO;
874                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
875                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
876                            le32_to_cpu(entry1->e_value_size)))
877                         return 1;
878
879                 entry1 = EXT2_XATTR_NEXT(entry1);
880                 entry2 = EXT2_XATTR_NEXT(entry2);
881         }
882         if (!IS_LAST_ENTRY(entry2))
883                 return 1;
884         return 0;
885 }
886
887 /*
888  * ext2_xattr_cache_find()
889  *
890  * Find an identical extended attribute block.
891  *
892  * Returns a locked buffer head to the block found, or NULL if such
893  * a block was not found or an error occurred.
894  */
895 static struct buffer_head *
896 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
897 {
898         __u32 hash = le32_to_cpu(header->h_hash);
899         struct mb_cache_entry *ce;
900         struct mb_cache *ext2_mb_cache = EXT2_SB(inode->i_sb)->s_mb_cache;
901
902         if (!header->h_hash)
903                 return NULL;  /* never share */
904         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
905 again:
906         ce = mb_cache_entry_find_first(ext2_mb_cache, hash);
907         while (ce) {
908                 struct buffer_head *bh;
909
910                 bh = sb_bread(inode->i_sb, ce->e_block);
911                 if (!bh) {
912                         ext2_error(inode->i_sb, "ext2_xattr_cache_find",
913                                 "inode %ld: block %ld read error",
914                                 inode->i_ino, (unsigned long) ce->e_block);
915                 } else {
916                         lock_buffer(bh);
917                         /*
918                          * We have to be careful about races with freeing or
919                          * rehashing of xattr block. Once we hold buffer lock
920                          * xattr block's state is stable so we can check
921                          * whether the block got freed / rehashed or not.
922                          * Since we unhash mbcache entry under buffer lock when
923                          * freeing / rehashing xattr block, checking whether
924                          * entry is still hashed is reliable.
925                          */
926                         if (hlist_bl_unhashed(&ce->e_hash_list)) {
927                                 mb_cache_entry_put(ext2_mb_cache, ce);
928                                 unlock_buffer(bh);
929                                 brelse(bh);
930                                 goto again;
931                         } else if (le32_to_cpu(HDR(bh)->h_refcount) >
932                                    EXT2_XATTR_REFCOUNT_MAX) {
933                                 ea_idebug(inode, "block %ld refcount %d>%d",
934                                           (unsigned long) ce->e_block,
935                                           le32_to_cpu(HDR(bh)->h_refcount),
936                                           EXT2_XATTR_REFCOUNT_MAX);
937                         } else if (!ext2_xattr_cmp(header, HDR(bh))) {
938                                 ea_bdebug(bh, "b_count=%d",
939                                           atomic_read(&(bh->b_count)));
940                                 mb_cache_entry_touch(ext2_mb_cache, ce);
941                                 mb_cache_entry_put(ext2_mb_cache, ce);
942                                 return bh;
943                         }
944                         unlock_buffer(bh);
945                         brelse(bh);
946                 }
947                 ce = mb_cache_entry_find_next(ext2_mb_cache, ce);
948         }
949         return NULL;
950 }
951
952 #define NAME_HASH_SHIFT 5
953 #define VALUE_HASH_SHIFT 16
954
955 /*
956  * ext2_xattr_hash_entry()
957  *
958  * Compute the hash of an extended attribute.
959  */
960 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
961                                          struct ext2_xattr_entry *entry)
962 {
963         __u32 hash = 0;
964         char *name = entry->e_name;
965         int n;
966
967         for (n=0; n < entry->e_name_len; n++) {
968                 hash = (hash << NAME_HASH_SHIFT) ^
969                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
970                        *name++;
971         }
972
973         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
974                 __le32 *value = (__le32 *)((char *)header +
975                         le16_to_cpu(entry->e_value_offs));
976                 for (n = (le32_to_cpu(entry->e_value_size) +
977                      EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
978                         hash = (hash << VALUE_HASH_SHIFT) ^
979                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
980                                le32_to_cpu(*value++);
981                 }
982         }
983         entry->e_hash = cpu_to_le32(hash);
984 }
985
986 #undef NAME_HASH_SHIFT
987 #undef VALUE_HASH_SHIFT
988
989 #define BLOCK_HASH_SHIFT 16
990
991 /*
992  * ext2_xattr_rehash()
993  *
994  * Re-compute the extended attribute hash value after an entry has changed.
995  */
996 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
997                               struct ext2_xattr_entry *entry)
998 {
999         struct ext2_xattr_entry *here;
1000         __u32 hash = 0;
1001         
1002         ext2_xattr_hash_entry(header, entry);
1003         here = ENTRY(header+1);
1004         while (!IS_LAST_ENTRY(here)) {
1005                 if (!here->e_hash) {
1006                         /* Block is not shared if an entry's hash value == 0 */
1007                         hash = 0;
1008                         break;
1009                 }
1010                 hash = (hash << BLOCK_HASH_SHIFT) ^
1011                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1012                        le32_to_cpu(here->e_hash);
1013                 here = EXT2_XATTR_NEXT(here);
1014         }
1015         header->h_hash = cpu_to_le32(hash);
1016 }
1017
1018 #undef BLOCK_HASH_SHIFT
1019
1020 #define HASH_BUCKET_BITS 10
1021
1022 struct mb_cache *ext2_xattr_create_cache(void)
1023 {
1024         return mb_cache_create(HASH_BUCKET_BITS);
1025 }
1026
1027 void ext2_xattr_destroy_cache(struct mb_cache *cache)
1028 {
1029         if (cache)
1030                 mb_cache_destroy(cache);
1031 }