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