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