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