~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ext4/xattr.c

Version: ~ [ linux-5.4 ] ~ [ linux-5.3.13 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.86 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.156 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.203 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.202 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp