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