]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * linux/fs/ext4/namei.c | |
3 | * | |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | |
5 | * Remy Card (card@masi.ibp.fr) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | * | |
9 | * from | |
10 | * | |
11 | * linux/fs/minix/namei.c | |
12 | * | |
13 | * Copyright (C) 1991, 1992 Linus Torvalds | |
14 | * | |
15 | * Big-endian to little-endian byte-swapping/bitmaps by | |
16 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
17 | * Directory entry file type support and forward compatibility hooks | |
18 | * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 | |
19 | * Hash Tree Directory indexing (c) | |
20 | * Daniel Phillips, 2001 | |
21 | * Hash Tree Directory indexing porting | |
22 | * Christopher Li, 2002 | |
23 | * Hash Tree Directory indexing cleanup | |
24 | * Theodore Ts'o, 2002 | |
25 | */ | |
26 | ||
27 | #include <linux/fs.h> | |
28 | #include <linux/pagemap.h> | |
29 | #include <linux/jbd2.h> | |
30 | #include <linux/time.h> | |
31 | #include <linux/fcntl.h> | |
32 | #include <linux/stat.h> | |
33 | #include <linux/string.h> | |
34 | #include <linux/quotaops.h> | |
35 | #include <linux/buffer_head.h> | |
36 | #include <linux/bio.h> | |
37 | #include "ext4.h" | |
38 | #include "ext4_jbd2.h" | |
39 | ||
40 | #include "xattr.h" | |
41 | #include "acl.h" | |
42 | ||
43 | #include <trace/events/ext4.h> | |
44 | /* | |
45 | * define how far ahead to read directories while searching them. | |
46 | */ | |
47 | #define NAMEI_RA_CHUNKS 2 | |
48 | #define NAMEI_RA_BLOCKS 4 | |
49 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) | |
50 | #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) | |
51 | ||
52 | static struct buffer_head *ext4_append(handle_t *handle, | |
53 | struct inode *inode, | |
54 | ext4_lblk_t *block, int *err) | |
55 | { | |
56 | struct buffer_head *bh; | |
57 | ||
58 | if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb && | |
59 | ((inode->i_size >> 10) >= | |
60 | EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) { | |
61 | *err = -ENOSPC; | |
62 | return NULL; | |
63 | } | |
64 | ||
65 | *block = inode->i_size >> inode->i_sb->s_blocksize_bits; | |
66 | ||
67 | bh = ext4_bread(handle, inode, *block, 1, err); | |
68 | if (bh) { | |
69 | inode->i_size += inode->i_sb->s_blocksize; | |
70 | EXT4_I(inode)->i_disksize = inode->i_size; | |
71 | *err = ext4_journal_get_write_access(handle, bh); | |
72 | if (*err) { | |
73 | brelse(bh); | |
74 | bh = NULL; | |
75 | } | |
76 | } | |
77 | if (!bh && !(*err)) { | |
78 | *err = -EIO; | |
79 | ext4_error(inode->i_sb, | |
80 | "Directory hole detected on inode %lu\n", | |
81 | inode->i_ino); | |
82 | } | |
83 | return bh; | |
84 | } | |
85 | ||
86 | #ifndef assert | |
87 | #define assert(test) J_ASSERT(test) | |
88 | #endif | |
89 | ||
90 | #ifdef DX_DEBUG | |
91 | #define dxtrace(command) command | |
92 | #else | |
93 | #define dxtrace(command) | |
94 | #endif | |
95 | ||
96 | struct fake_dirent | |
97 | { | |
98 | __le32 inode; | |
99 | __le16 rec_len; | |
100 | u8 name_len; | |
101 | u8 file_type; | |
102 | }; | |
103 | ||
104 | struct dx_countlimit | |
105 | { | |
106 | __le16 limit; | |
107 | __le16 count; | |
108 | }; | |
109 | ||
110 | struct dx_entry | |
111 | { | |
112 | __le32 hash; | |
113 | __le32 block; | |
114 | }; | |
115 | ||
116 | /* | |
117 | * dx_root_info is laid out so that if it should somehow get overlaid by a | |
118 | * dirent the two low bits of the hash version will be zero. Therefore, the | |
119 | * hash version mod 4 should never be 0. Sincerely, the paranoia department. | |
120 | */ | |
121 | ||
122 | struct dx_root | |
123 | { | |
124 | struct fake_dirent dot; | |
125 | char dot_name[4]; | |
126 | struct fake_dirent dotdot; | |
127 | char dotdot_name[4]; | |
128 | struct dx_root_info | |
129 | { | |
130 | __le32 reserved_zero; | |
131 | u8 hash_version; | |
132 | u8 info_length; /* 8 */ | |
133 | u8 indirect_levels; | |
134 | u8 unused_flags; | |
135 | } | |
136 | info; | |
137 | struct dx_entry entries[0]; | |
138 | }; | |
139 | ||
140 | struct dx_node | |
141 | { | |
142 | struct fake_dirent fake; | |
143 | struct dx_entry entries[0]; | |
144 | }; | |
145 | ||
146 | ||
147 | struct dx_frame | |
148 | { | |
149 | struct buffer_head *bh; | |
150 | struct dx_entry *entries; | |
151 | struct dx_entry *at; | |
152 | }; | |
153 | ||
154 | struct dx_map_entry | |
155 | { | |
156 | u32 hash; | |
157 | u16 offs; | |
158 | u16 size; | |
159 | }; | |
160 | ||
161 | /* | |
162 | * This goes at the end of each htree block. | |
163 | */ | |
164 | struct dx_tail { | |
165 | u32 dt_reserved; | |
166 | __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */ | |
167 | }; | |
168 | ||
169 | static inline ext4_lblk_t dx_get_block(struct dx_entry *entry); | |
170 | static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value); | |
171 | static inline unsigned dx_get_hash(struct dx_entry *entry); | |
172 | static void dx_set_hash(struct dx_entry *entry, unsigned value); | |
173 | static unsigned dx_get_count(struct dx_entry *entries); | |
174 | static unsigned dx_get_limit(struct dx_entry *entries); | |
175 | static void dx_set_count(struct dx_entry *entries, unsigned value); | |
176 | static void dx_set_limit(struct dx_entry *entries, unsigned value); | |
177 | static unsigned dx_root_limit(struct inode *dir, unsigned infosize); | |
178 | static unsigned dx_node_limit(struct inode *dir); | |
179 | static struct dx_frame *dx_probe(const struct qstr *d_name, | |
180 | struct inode *dir, | |
181 | struct dx_hash_info *hinfo, | |
182 | struct dx_frame *frame, | |
183 | int *err); | |
184 | static void dx_release(struct dx_frame *frames); | |
185 | static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize, | |
186 | struct dx_hash_info *hinfo, struct dx_map_entry map[]); | |
187 | static void dx_sort_map(struct dx_map_entry *map, unsigned count); | |
188 | static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to, | |
189 | struct dx_map_entry *offsets, int count, unsigned blocksize); | |
190 | static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize); | |
191 | static void dx_insert_block(struct dx_frame *frame, | |
192 | u32 hash, ext4_lblk_t block); | |
193 | static int ext4_htree_next_block(struct inode *dir, __u32 hash, | |
194 | struct dx_frame *frame, | |
195 | struct dx_frame *frames, | |
196 | __u32 *start_hash); | |
197 | static struct buffer_head * ext4_dx_find_entry(struct inode *dir, | |
198 | const struct qstr *d_name, | |
199 | struct ext4_dir_entry_2 **res_dir, | |
200 | int *err); | |
201 | static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry, | |
202 | struct inode *inode); | |
203 | ||
204 | /* checksumming functions */ | |
205 | #define EXT4_DIRENT_TAIL(block, blocksize) \ | |
206 | ((struct ext4_dir_entry_tail *)(((void *)(block)) + \ | |
207 | ((blocksize) - \ | |
208 | sizeof(struct ext4_dir_entry_tail)))) | |
209 | ||
210 | static void initialize_dirent_tail(struct ext4_dir_entry_tail *t, | |
211 | unsigned int blocksize) | |
212 | { | |
213 | memset(t, 0, sizeof(struct ext4_dir_entry_tail)); | |
214 | t->det_rec_len = ext4_rec_len_to_disk( | |
215 | sizeof(struct ext4_dir_entry_tail), blocksize); | |
216 | t->det_reserved_ft = EXT4_FT_DIR_CSUM; | |
217 | } | |
218 | ||
219 | /* Walk through a dirent block to find a checksum "dirent" at the tail */ | |
220 | static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode, | |
221 | struct ext4_dir_entry *de) | |
222 | { | |
223 | struct ext4_dir_entry_tail *t; | |
224 | ||
225 | #ifdef PARANOID | |
226 | struct ext4_dir_entry *d, *top; | |
227 | ||
228 | d = de; | |
229 | top = (struct ext4_dir_entry *)(((void *)de) + | |
230 | (EXT4_BLOCK_SIZE(inode->i_sb) - | |
231 | sizeof(struct ext4_dir_entry_tail))); | |
232 | while (d < top && d->rec_len) | |
233 | d = (struct ext4_dir_entry *)(((void *)d) + | |
234 | le16_to_cpu(d->rec_len)); | |
235 | ||
236 | if (d != top) | |
237 | return NULL; | |
238 | ||
239 | t = (struct ext4_dir_entry_tail *)d; | |
240 | #else | |
241 | t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb)); | |
242 | #endif | |
243 | ||
244 | if (t->det_reserved_zero1 || | |
245 | le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) || | |
246 | t->det_reserved_zero2 || | |
247 | t->det_reserved_ft != EXT4_FT_DIR_CSUM) | |
248 | return NULL; | |
249 | ||
250 | return t; | |
251 | } | |
252 | ||
253 | static __le32 ext4_dirent_csum(struct inode *inode, | |
254 | struct ext4_dir_entry *dirent, int size) | |
255 | { | |
256 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
257 | struct ext4_inode_info *ei = EXT4_I(inode); | |
258 | __u32 csum; | |
259 | ||
260 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); | |
261 | return cpu_to_le32(csum); | |
262 | } | |
263 | ||
264 | static void warn_no_space_for_csum(struct inode *inode) | |
265 | { | |
266 | ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for " | |
267 | "checksum. Please run e2fsck -D.", inode->i_ino); | |
268 | } | |
269 | ||
270 | int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent) | |
271 | { | |
272 | struct ext4_dir_entry_tail *t; | |
273 | ||
274 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
275 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
276 | return 1; | |
277 | ||
278 | t = get_dirent_tail(inode, dirent); | |
279 | if (!t) { | |
280 | warn_no_space_for_csum(inode); | |
281 | return 0; | |
282 | } | |
283 | ||
284 | if (t->det_checksum != ext4_dirent_csum(inode, dirent, | |
285 | (void *)t - (void *)dirent)) | |
286 | return 0; | |
287 | ||
288 | return 1; | |
289 | } | |
290 | ||
291 | static void ext4_dirent_csum_set(struct inode *inode, | |
292 | struct ext4_dir_entry *dirent) | |
293 | { | |
294 | struct ext4_dir_entry_tail *t; | |
295 | ||
296 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
297 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
298 | return; | |
299 | ||
300 | t = get_dirent_tail(inode, dirent); | |
301 | if (!t) { | |
302 | warn_no_space_for_csum(inode); | |
303 | return; | |
304 | } | |
305 | ||
306 | t->det_checksum = ext4_dirent_csum(inode, dirent, | |
307 | (void *)t - (void *)dirent); | |
308 | } | |
309 | ||
310 | static inline int ext4_handle_dirty_dirent_node(handle_t *handle, | |
311 | struct inode *inode, | |
312 | struct buffer_head *bh) | |
313 | { | |
314 | ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data); | |
315 | return ext4_handle_dirty_metadata(handle, inode, bh); | |
316 | } | |
317 | ||
318 | static struct dx_countlimit *get_dx_countlimit(struct inode *inode, | |
319 | struct ext4_dir_entry *dirent, | |
320 | int *offset) | |
321 | { | |
322 | struct ext4_dir_entry *dp; | |
323 | struct dx_root_info *root; | |
324 | int count_offset; | |
325 | ||
326 | if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb)) | |
327 | count_offset = 8; | |
328 | else if (le16_to_cpu(dirent->rec_len) == 12) { | |
329 | dp = (struct ext4_dir_entry *)(((void *)dirent) + 12); | |
330 | if (le16_to_cpu(dp->rec_len) != | |
331 | EXT4_BLOCK_SIZE(inode->i_sb) - 12) | |
332 | return NULL; | |
333 | root = (struct dx_root_info *)(((void *)dp + 12)); | |
334 | if (root->reserved_zero || | |
335 | root->info_length != sizeof(struct dx_root_info)) | |
336 | return NULL; | |
337 | count_offset = 32; | |
338 | } else | |
339 | return NULL; | |
340 | ||
341 | if (offset) | |
342 | *offset = count_offset; | |
343 | return (struct dx_countlimit *)(((void *)dirent) + count_offset); | |
344 | } | |
345 | ||
346 | static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent, | |
347 | int count_offset, int count, struct dx_tail *t) | |
348 | { | |
349 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
350 | struct ext4_inode_info *ei = EXT4_I(inode); | |
351 | __u32 csum, old_csum; | |
352 | int size; | |
353 | ||
354 | size = count_offset + (count * sizeof(struct dx_entry)); | |
355 | old_csum = t->dt_checksum; | |
356 | t->dt_checksum = 0; | |
357 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); | |
358 | csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail)); | |
359 | t->dt_checksum = old_csum; | |
360 | ||
361 | return cpu_to_le32(csum); | |
362 | } | |
363 | ||
364 | static int ext4_dx_csum_verify(struct inode *inode, | |
365 | struct ext4_dir_entry *dirent) | |
366 | { | |
367 | struct dx_countlimit *c; | |
368 | struct dx_tail *t; | |
369 | int count_offset, limit, count; | |
370 | ||
371 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
372 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
373 | return 1; | |
374 | ||
375 | c = get_dx_countlimit(inode, dirent, &count_offset); | |
376 | if (!c) { | |
377 | EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D."); | |
378 | return 1; | |
379 | } | |
380 | limit = le16_to_cpu(c->limit); | |
381 | count = le16_to_cpu(c->count); | |
382 | if (count_offset + (limit * sizeof(struct dx_entry)) > | |
383 | EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { | |
384 | warn_no_space_for_csum(inode); | |
385 | return 1; | |
386 | } | |
387 | t = (struct dx_tail *)(((struct dx_entry *)c) + limit); | |
388 | ||
389 | if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset, | |
390 | count, t)) | |
391 | return 0; | |
392 | return 1; | |
393 | } | |
394 | ||
395 | static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent) | |
396 | { | |
397 | struct dx_countlimit *c; | |
398 | struct dx_tail *t; | |
399 | int count_offset, limit, count; | |
400 | ||
401 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
402 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
403 | return; | |
404 | ||
405 | c = get_dx_countlimit(inode, dirent, &count_offset); | |
406 | if (!c) { | |
407 | EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D."); | |
408 | return; | |
409 | } | |
410 | limit = le16_to_cpu(c->limit); | |
411 | count = le16_to_cpu(c->count); | |
412 | if (count_offset + (limit * sizeof(struct dx_entry)) > | |
413 | EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { | |
414 | warn_no_space_for_csum(inode); | |
415 | return; | |
416 | } | |
417 | t = (struct dx_tail *)(((struct dx_entry *)c) + limit); | |
418 | ||
419 | t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t); | |
420 | } | |
421 | ||
422 | static inline int ext4_handle_dirty_dx_node(handle_t *handle, | |
423 | struct inode *inode, | |
424 | struct buffer_head *bh) | |
425 | { | |
426 | ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data); | |
427 | return ext4_handle_dirty_metadata(handle, inode, bh); | |
428 | } | |
429 | ||
430 | /* | |
431 | * p is at least 6 bytes before the end of page | |
432 | */ | |
433 | static inline struct ext4_dir_entry_2 * | |
434 | ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize) | |
435 | { | |
436 | return (struct ext4_dir_entry_2 *)((char *)p + | |
437 | ext4_rec_len_from_disk(p->rec_len, blocksize)); | |
438 | } | |
439 | ||
440 | /* | |
441 | * Future: use high four bits of block for coalesce-on-delete flags | |
442 | * Mask them off for now. | |
443 | */ | |
444 | ||
445 | static inline ext4_lblk_t dx_get_block(struct dx_entry *entry) | |
446 | { | |
447 | return le32_to_cpu(entry->block) & 0x00ffffff; | |
448 | } | |
449 | ||
450 | static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value) | |
451 | { | |
452 | entry->block = cpu_to_le32(value); | |
453 | } | |
454 | ||
455 | static inline unsigned dx_get_hash(struct dx_entry *entry) | |
456 | { | |
457 | return le32_to_cpu(entry->hash); | |
458 | } | |
459 | ||
460 | static inline void dx_set_hash(struct dx_entry *entry, unsigned value) | |
461 | { | |
462 | entry->hash = cpu_to_le32(value); | |
463 | } | |
464 | ||
465 | static inline unsigned dx_get_count(struct dx_entry *entries) | |
466 | { | |
467 | return le16_to_cpu(((struct dx_countlimit *) entries)->count); | |
468 | } | |
469 | ||
470 | static inline unsigned dx_get_limit(struct dx_entry *entries) | |
471 | { | |
472 | return le16_to_cpu(((struct dx_countlimit *) entries)->limit); | |
473 | } | |
474 | ||
475 | static inline void dx_set_count(struct dx_entry *entries, unsigned value) | |
476 | { | |
477 | ((struct dx_countlimit *) entries)->count = cpu_to_le16(value); | |
478 | } | |
479 | ||
480 | static inline void dx_set_limit(struct dx_entry *entries, unsigned value) | |
481 | { | |
482 | ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value); | |
483 | } | |
484 | ||
485 | static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize) | |
486 | { | |
487 | unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) - | |
488 | EXT4_DIR_REC_LEN(2) - infosize; | |
489 | ||
490 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, | |
491 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
492 | entry_space -= sizeof(struct dx_tail); | |
493 | return entry_space / sizeof(struct dx_entry); | |
494 | } | |
495 | ||
496 | static inline unsigned dx_node_limit(struct inode *dir) | |
497 | { | |
498 | unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0); | |
499 | ||
500 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, | |
501 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
502 | entry_space -= sizeof(struct dx_tail); | |
503 | return entry_space / sizeof(struct dx_entry); | |
504 | } | |
505 | ||
506 | /* | |
507 | * Debug | |
508 | */ | |
509 | #ifdef DX_DEBUG | |
510 | static void dx_show_index(char * label, struct dx_entry *entries) | |
511 | { | |
512 | int i, n = dx_get_count (entries); | |
513 | printk(KERN_DEBUG "%s index ", label); | |
514 | for (i = 0; i < n; i++) { | |
515 | printk("%x->%lu ", i ? dx_get_hash(entries + i) : | |
516 | 0, (unsigned long)dx_get_block(entries + i)); | |
517 | } | |
518 | printk("\n"); | |
519 | } | |
520 | ||
521 | struct stats | |
522 | { | |
523 | unsigned names; | |
524 | unsigned space; | |
525 | unsigned bcount; | |
526 | }; | |
527 | ||
528 | static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de, | |
529 | int size, int show_names) | |
530 | { | |
531 | unsigned names = 0, space = 0; | |
532 | char *base = (char *) de; | |
533 | struct dx_hash_info h = *hinfo; | |
534 | ||
535 | printk("names: "); | |
536 | while ((char *) de < base + size) | |
537 | { | |
538 | if (de->inode) | |
539 | { | |
540 | if (show_names) | |
541 | { | |
542 | int len = de->name_len; | |
543 | char *name = de->name; | |
544 | while (len--) printk("%c", *name++); | |
545 | ext4fs_dirhash(de->name, de->name_len, &h); | |
546 | printk(":%x.%u ", h.hash, | |
547 | (unsigned) ((char *) de - base)); | |
548 | } | |
549 | space += EXT4_DIR_REC_LEN(de->name_len); | |
550 | names++; | |
551 | } | |
552 | de = ext4_next_entry(de, size); | |
553 | } | |
554 | printk("(%i)\n", names); | |
555 | return (struct stats) { names, space, 1 }; | |
556 | } | |
557 | ||
558 | struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir, | |
559 | struct dx_entry *entries, int levels) | |
560 | { | |
561 | unsigned blocksize = dir->i_sb->s_blocksize; | |
562 | unsigned count = dx_get_count(entries), names = 0, space = 0, i; | |
563 | unsigned bcount = 0; | |
564 | struct buffer_head *bh; | |
565 | int err; | |
566 | printk("%i indexed blocks...\n", count); | |
567 | for (i = 0; i < count; i++, entries++) | |
568 | { | |
569 | ext4_lblk_t block = dx_get_block(entries); | |
570 | ext4_lblk_t hash = i ? dx_get_hash(entries): 0; | |
571 | u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash; | |
572 | struct stats stats; | |
573 | printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range); | |
574 | if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue; | |
575 | stats = levels? | |
576 | dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1): | |
577 | dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0); | |
578 | names += stats.names; | |
579 | space += stats.space; | |
580 | bcount += stats.bcount; | |
581 | brelse(bh); | |
582 | } | |
583 | if (bcount) | |
584 | printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n", | |
585 | levels ? "" : " ", names, space/bcount, | |
586 | (space/bcount)*100/blocksize); | |
587 | return (struct stats) { names, space, bcount}; | |
588 | } | |
589 | #endif /* DX_DEBUG */ | |
590 | ||
591 | /* | |
592 | * Probe for a directory leaf block to search. | |
593 | * | |
594 | * dx_probe can return ERR_BAD_DX_DIR, which means there was a format | |
595 | * error in the directory index, and the caller should fall back to | |
596 | * searching the directory normally. The callers of dx_probe **MUST** | |
597 | * check for this error code, and make sure it never gets reflected | |
598 | * back to userspace. | |
599 | */ | |
600 | static struct dx_frame * | |
601 | dx_probe(const struct qstr *d_name, struct inode *dir, | |
602 | struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err) | |
603 | { | |
604 | unsigned count, indirect; | |
605 | struct dx_entry *at, *entries, *p, *q, *m; | |
606 | struct dx_root *root; | |
607 | struct buffer_head *bh; | |
608 | struct dx_frame *frame = frame_in; | |
609 | u32 hash; | |
610 | ||
611 | frame->bh = NULL; | |
612 | if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) { | |
613 | if (*err == 0) | |
614 | *err = ERR_BAD_DX_DIR; | |
615 | goto fail; | |
616 | } | |
617 | root = (struct dx_root *) bh->b_data; | |
618 | if (root->info.hash_version != DX_HASH_TEA && | |
619 | root->info.hash_version != DX_HASH_HALF_MD4 && | |
620 | root->info.hash_version != DX_HASH_LEGACY) { | |
621 | ext4_warning(dir->i_sb, "Unrecognised inode hash code %d", | |
622 | root->info.hash_version); | |
623 | brelse(bh); | |
624 | *err = ERR_BAD_DX_DIR; | |
625 | goto fail; | |
626 | } | |
627 | hinfo->hash_version = root->info.hash_version; | |
628 | if (hinfo->hash_version <= DX_HASH_TEA) | |
629 | hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; | |
630 | hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed; | |
631 | if (d_name) | |
632 | ext4fs_dirhash(d_name->name, d_name->len, hinfo); | |
633 | hash = hinfo->hash; | |
634 | ||
635 | if (root->info.unused_flags & 1) { | |
636 | ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x", | |
637 | root->info.unused_flags); | |
638 | brelse(bh); | |
639 | *err = ERR_BAD_DX_DIR; | |
640 | goto fail; | |
641 | } | |
642 | ||
643 | if ((indirect = root->info.indirect_levels) > 1) { | |
644 | ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x", | |
645 | root->info.indirect_levels); | |
646 | brelse(bh); | |
647 | *err = ERR_BAD_DX_DIR; | |
648 | goto fail; | |
649 | } | |
650 | ||
651 | if (!buffer_verified(bh) && | |
652 | !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) { | |
653 | ext4_warning(dir->i_sb, "Root failed checksum"); | |
654 | brelse(bh); | |
655 | *err = ERR_BAD_DX_DIR; | |
656 | goto fail; | |
657 | } | |
658 | set_buffer_verified(bh); | |
659 | ||
660 | entries = (struct dx_entry *) (((char *)&root->info) + | |
661 | root->info.info_length); | |
662 | ||
663 | if (dx_get_limit(entries) != dx_root_limit(dir, | |
664 | root->info.info_length)) { | |
665 | ext4_warning(dir->i_sb, "dx entry: limit != root limit"); | |
666 | brelse(bh); | |
667 | *err = ERR_BAD_DX_DIR; | |
668 | goto fail; | |
669 | } | |
670 | ||
671 | dxtrace(printk("Look up %x", hash)); | |
672 | while (1) | |
673 | { | |
674 | count = dx_get_count(entries); | |
675 | if (!count || count > dx_get_limit(entries)) { | |
676 | ext4_warning(dir->i_sb, | |
677 | "dx entry: no count or count > limit"); | |
678 | brelse(bh); | |
679 | *err = ERR_BAD_DX_DIR; | |
680 | goto fail2; | |
681 | } | |
682 | ||
683 | p = entries + 1; | |
684 | q = entries + count - 1; | |
685 | while (p <= q) | |
686 | { | |
687 | m = p + (q - p)/2; | |
688 | dxtrace(printk(".")); | |
689 | if (dx_get_hash(m) > hash) | |
690 | q = m - 1; | |
691 | else | |
692 | p = m + 1; | |
693 | } | |
694 | ||
695 | if (0) // linear search cross check | |
696 | { | |
697 | unsigned n = count - 1; | |
698 | at = entries; | |
699 | while (n--) | |
700 | { | |
701 | dxtrace(printk(",")); | |
702 | if (dx_get_hash(++at) > hash) | |
703 | { | |
704 | at--; | |
705 | break; | |
706 | } | |
707 | } | |
708 | assert (at == p - 1); | |
709 | } | |
710 | ||
711 | at = p - 1; | |
712 | dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at))); | |
713 | frame->bh = bh; | |
714 | frame->entries = entries; | |
715 | frame->at = at; | |
716 | if (!indirect--) return frame; | |
717 | if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) { | |
718 | if (!(*err)) | |
719 | *err = ERR_BAD_DX_DIR; | |
720 | goto fail2; | |
721 | } | |
722 | at = entries = ((struct dx_node *) bh->b_data)->entries; | |
723 | ||
724 | if (!buffer_verified(bh) && | |
725 | !ext4_dx_csum_verify(dir, | |
726 | (struct ext4_dir_entry *)bh->b_data)) { | |
727 | ext4_warning(dir->i_sb, "Node failed checksum"); | |
728 | brelse(bh); | |
729 | *err = ERR_BAD_DX_DIR; | |
730 | goto fail; | |
731 | } | |
732 | set_buffer_verified(bh); | |
733 | ||
734 | if (dx_get_limit(entries) != dx_node_limit (dir)) { | |
735 | ext4_warning(dir->i_sb, | |
736 | "dx entry: limit != node limit"); | |
737 | brelse(bh); | |
738 | *err = ERR_BAD_DX_DIR; | |
739 | goto fail2; | |
740 | } | |
741 | frame++; | |
742 | frame->bh = NULL; | |
743 | } | |
744 | fail2: | |
745 | while (frame >= frame_in) { | |
746 | brelse(frame->bh); | |
747 | frame--; | |
748 | } | |
749 | fail: | |
750 | if (*err == ERR_BAD_DX_DIR) | |
751 | ext4_warning(dir->i_sb, | |
752 | "Corrupt dir inode %lu, running e2fsck is " | |
753 | "recommended.", dir->i_ino); | |
754 | return NULL; | |
755 | } | |
756 | ||
757 | static void dx_release (struct dx_frame *frames) | |
758 | { | |
759 | if (frames[0].bh == NULL) | |
760 | return; | |
761 | ||
762 | if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels) | |
763 | brelse(frames[1].bh); | |
764 | brelse(frames[0].bh); | |
765 | } | |
766 | ||
767 | /* | |
768 | * This function increments the frame pointer to search the next leaf | |
769 | * block, and reads in the necessary intervening nodes if the search | |
770 | * should be necessary. Whether or not the search is necessary is | |
771 | * controlled by the hash parameter. If the hash value is even, then | |
772 | * the search is only continued if the next block starts with that | |
773 | * hash value. This is used if we are searching for a specific file. | |
774 | * | |
775 | * If the hash value is HASH_NB_ALWAYS, then always go to the next block. | |
776 | * | |
777 | * This function returns 1 if the caller should continue to search, | |
778 | * or 0 if it should not. If there is an error reading one of the | |
779 | * index blocks, it will a negative error code. | |
780 | * | |
781 | * If start_hash is non-null, it will be filled in with the starting | |
782 | * hash of the next page. | |
783 | */ | |
784 | static int ext4_htree_next_block(struct inode *dir, __u32 hash, | |
785 | struct dx_frame *frame, | |
786 | struct dx_frame *frames, | |
787 | __u32 *start_hash) | |
788 | { | |
789 | struct dx_frame *p; | |
790 | struct buffer_head *bh; | |
791 | int err, num_frames = 0; | |
792 | __u32 bhash; | |
793 | ||
794 | p = frame; | |
795 | /* | |
796 | * Find the next leaf page by incrementing the frame pointer. | |
797 | * If we run out of entries in the interior node, loop around and | |
798 | * increment pointer in the parent node. When we break out of | |
799 | * this loop, num_frames indicates the number of interior | |
800 | * nodes need to be read. | |
801 | */ | |
802 | while (1) { | |
803 | if (++(p->at) < p->entries + dx_get_count(p->entries)) | |
804 | break; | |
805 | if (p == frames) | |
806 | return 0; | |
807 | num_frames++; | |
808 | p--; | |
809 | } | |
810 | ||
811 | /* | |
812 | * If the hash is 1, then continue only if the next page has a | |
813 | * continuation hash of any value. This is used for readdir | |
814 | * handling. Otherwise, check to see if the hash matches the | |
815 | * desired contiuation hash. If it doesn't, return since | |
816 | * there's no point to read in the successive index pages. | |
817 | */ | |
818 | bhash = dx_get_hash(p->at); | |
819 | if (start_hash) | |
820 | *start_hash = bhash; | |
821 | if ((hash & 1) == 0) { | |
822 | if ((bhash & ~1) != hash) | |
823 | return 0; | |
824 | } | |
825 | /* | |
826 | * If the hash is HASH_NB_ALWAYS, we always go to the next | |
827 | * block so no check is necessary | |
828 | */ | |
829 | while (num_frames--) { | |
830 | if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at), | |
831 | 0, &err))) { | |
832 | if (!err) { | |
833 | ext4_error(dir->i_sb, | |
834 | "Directory hole detected on inode %lu\n", | |
835 | dir->i_ino); | |
836 | return -EIO; | |
837 | } | |
838 | return err; /* Failure */ | |
839 | } | |
840 | ||
841 | if (!buffer_verified(bh) && | |
842 | !ext4_dx_csum_verify(dir, | |
843 | (struct ext4_dir_entry *)bh->b_data)) { | |
844 | ext4_warning(dir->i_sb, "Node failed checksum"); | |
845 | return -EIO; | |
846 | } | |
847 | set_buffer_verified(bh); | |
848 | ||
849 | p++; | |
850 | brelse(p->bh); | |
851 | p->bh = bh; | |
852 | p->at = p->entries = ((struct dx_node *) bh->b_data)->entries; | |
853 | } | |
854 | return 1; | |
855 | } | |
856 | ||
857 | ||
858 | /* | |
859 | * This function fills a red-black tree with information from a | |
860 | * directory block. It returns the number directory entries loaded | |
861 | * into the tree. If there is an error it is returned in err. | |
862 | */ | |
863 | static int htree_dirblock_to_tree(struct file *dir_file, | |
864 | struct inode *dir, ext4_lblk_t block, | |
865 | struct dx_hash_info *hinfo, | |
866 | __u32 start_hash, __u32 start_minor_hash) | |
867 | { | |
868 | struct buffer_head *bh; | |
869 | struct ext4_dir_entry_2 *de, *top; | |
870 | int err = 0, count = 0; | |
871 | ||
872 | dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n", | |
873 | (unsigned long)block)); | |
874 | if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) { | |
875 | if (!err) { | |
876 | err = -EIO; | |
877 | ext4_error(dir->i_sb, | |
878 | "Directory hole detected on inode %lu\n", | |
879 | dir->i_ino); | |
880 | } | |
881 | return err; | |
882 | } | |
883 | ||
884 | if (!buffer_verified(bh) && | |
885 | !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) | |
886 | return -EIO; | |
887 | set_buffer_verified(bh); | |
888 | ||
889 | de = (struct ext4_dir_entry_2 *) bh->b_data; | |
890 | top = (struct ext4_dir_entry_2 *) ((char *) de + | |
891 | dir->i_sb->s_blocksize - | |
892 | EXT4_DIR_REC_LEN(0)); | |
893 | for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) { | |
894 | if (ext4_check_dir_entry(dir, NULL, de, bh, | |
895 | bh->b_data, bh->b_size, | |
896 | (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb)) | |
897 | + ((char *)de - bh->b_data))) { | |
898 | /* On error, skip the f_pos to the next block. */ | |
899 | dir_file->f_pos = (dir_file->f_pos | | |
900 | (dir->i_sb->s_blocksize - 1)) + 1; | |
901 | brelse(bh); | |
902 | return count; | |
903 | } | |
904 | ext4fs_dirhash(de->name, de->name_len, hinfo); | |
905 | if ((hinfo->hash < start_hash) || | |
906 | ((hinfo->hash == start_hash) && | |
907 | (hinfo->minor_hash < start_minor_hash))) | |
908 | continue; | |
909 | if (de->inode == 0) | |
910 | continue; | |
911 | if ((err = ext4_htree_store_dirent(dir_file, | |
912 | hinfo->hash, hinfo->minor_hash, de)) != 0) { | |
913 | brelse(bh); | |
914 | return err; | |
915 | } | |
916 | count++; | |
917 | } | |
918 | brelse(bh); | |
919 | return count; | |
920 | } | |
921 | ||
922 | ||
923 | /* | |
924 | * This function fills a red-black tree with information from a | |
925 | * directory. We start scanning the directory in hash order, starting | |
926 | * at start_hash and start_minor_hash. | |
927 | * | |
928 | * This function returns the number of entries inserted into the tree, | |
929 | * or a negative error code. | |
930 | */ | |
931 | int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, | |
932 | __u32 start_minor_hash, __u32 *next_hash) | |
933 | { | |
934 | struct dx_hash_info hinfo; | |
935 | struct ext4_dir_entry_2 *de; | |
936 | struct dx_frame frames[2], *frame; | |
937 | struct inode *dir; | |
938 | ext4_lblk_t block; | |
939 | int count = 0; | |
940 | int ret, err; | |
941 | __u32 hashval; | |
942 | ||
943 | dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n", | |
944 | start_hash, start_minor_hash)); | |
945 | dir = dir_file->f_path.dentry->d_inode; | |
946 | if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) { | |
947 | hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; | |
948 | if (hinfo.hash_version <= DX_HASH_TEA) | |
949 | hinfo.hash_version += | |
950 | EXT4_SB(dir->i_sb)->s_hash_unsigned; | |
951 | hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; | |
952 | count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo, | |
953 | start_hash, start_minor_hash); | |
954 | *next_hash = ~0; | |
955 | return count; | |
956 | } | |
957 | hinfo.hash = start_hash; | |
958 | hinfo.minor_hash = 0; | |
959 | frame = dx_probe(NULL, dir, &hinfo, frames, &err); | |
960 | if (!frame) | |
961 | return err; | |
962 | ||
963 | /* Add '.' and '..' from the htree header */ | |
964 | if (!start_hash && !start_minor_hash) { | |
965 | de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; | |
966 | if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0) | |
967 | goto errout; | |
968 | count++; | |
969 | } | |
970 | if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { | |
971 | de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; | |
972 | de = ext4_next_entry(de, dir->i_sb->s_blocksize); | |
973 | if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0) | |
974 | goto errout; | |
975 | count++; | |
976 | } | |
977 | ||
978 | while (1) { | |
979 | block = dx_get_block(frame->at); | |
980 | ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo, | |
981 | start_hash, start_minor_hash); | |
982 | if (ret < 0) { | |
983 | err = ret; | |
984 | goto errout; | |
985 | } | |
986 | count += ret; | |
987 | hashval = ~0; | |
988 | ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS, | |
989 | frame, frames, &hashval); | |
990 | *next_hash = hashval; | |
991 | if (ret < 0) { | |
992 | err = ret; | |
993 | goto errout; | |
994 | } | |
995 | /* | |
996 | * Stop if: (a) there are no more entries, or | |
997 | * (b) we have inserted at least one entry and the | |
998 | * next hash value is not a continuation | |
999 | */ | |
1000 | if ((ret == 0) || | |
1001 | (count && ((hashval & 1) == 0))) | |
1002 | break; | |
1003 | } | |
1004 | dx_release(frames); | |
1005 | dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, " | |
1006 | "next hash: %x\n", count, *next_hash)); | |
1007 | return count; | |
1008 | errout: | |
1009 | dx_release(frames); | |
1010 | return (err); | |
1011 | } | |
1012 | ||
1013 | ||
1014 | /* | |
1015 | * Directory block splitting, compacting | |
1016 | */ | |
1017 | ||
1018 | /* | |
1019 | * Create map of hash values, offsets, and sizes, stored at end of block. | |
1020 | * Returns number of entries mapped. | |
1021 | */ | |
1022 | static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize, | |
1023 | struct dx_hash_info *hinfo, | |
1024 | struct dx_map_entry *map_tail) | |
1025 | { | |
1026 | int count = 0; | |
1027 | char *base = (char *) de; | |
1028 | struct dx_hash_info h = *hinfo; | |
1029 | ||
1030 | while ((char *) de < base + blocksize) { | |
1031 | if (de->name_len && de->inode) { | |
1032 | ext4fs_dirhash(de->name, de->name_len, &h); | |
1033 | map_tail--; | |
1034 | map_tail->hash = h.hash; | |
1035 | map_tail->offs = ((char *) de - base)>>2; | |
1036 | map_tail->size = le16_to_cpu(de->rec_len); | |
1037 | count++; | |
1038 | cond_resched(); | |
1039 | } | |
1040 | /* XXX: do we need to check rec_len == 0 case? -Chris */ | |
1041 | de = ext4_next_entry(de, blocksize); | |
1042 | } | |
1043 | return count; | |
1044 | } | |
1045 | ||
1046 | /* Sort map by hash value */ | |
1047 | static void dx_sort_map (struct dx_map_entry *map, unsigned count) | |
1048 | { | |
1049 | struct dx_map_entry *p, *q, *top = map + count - 1; | |
1050 | int more; | |
1051 | /* Combsort until bubble sort doesn't suck */ | |
1052 | while (count > 2) { | |
1053 | count = count*10/13; | |
1054 | if (count - 9 < 2) /* 9, 10 -> 11 */ | |
1055 | count = 11; | |
1056 | for (p = top, q = p - count; q >= map; p--, q--) | |
1057 | if (p->hash < q->hash) | |
1058 | swap(*p, *q); | |
1059 | } | |
1060 | /* Garden variety bubble sort */ | |
1061 | do { | |
1062 | more = 0; | |
1063 | q = top; | |
1064 | while (q-- > map) { | |
1065 | if (q[1].hash >= q[0].hash) | |
1066 | continue; | |
1067 | swap(*(q+1), *q); | |
1068 | more = 1; | |
1069 | } | |
1070 | } while(more); | |
1071 | } | |
1072 | ||
1073 | static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) | |
1074 | { | |
1075 | struct dx_entry *entries = frame->entries; | |
1076 | struct dx_entry *old = frame->at, *new = old + 1; | |
1077 | int count = dx_get_count(entries); | |
1078 | ||
1079 | assert(count < dx_get_limit(entries)); | |
1080 | assert(old < entries + count); | |
1081 | memmove(new + 1, new, (char *)(entries + count) - (char *)(new)); | |
1082 | dx_set_hash(new, hash); | |
1083 | dx_set_block(new, block); | |
1084 | dx_set_count(entries, count + 1); | |
1085 | } | |
1086 | ||
1087 | /* | |
1088 | * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure. | |
1089 | * | |
1090 | * `len <= EXT4_NAME_LEN' is guaranteed by caller. | |
1091 | * `de != NULL' is guaranteed by caller. | |
1092 | */ | |
1093 | static inline int ext4_match (int len, const char * const name, | |
1094 | struct ext4_dir_entry_2 * de) | |
1095 | { | |
1096 | if (len != de->name_len) | |
1097 | return 0; | |
1098 | if (!de->inode) | |
1099 | return 0; | |
1100 | return !memcmp(name, de->name, len); | |
1101 | } | |
1102 | ||
1103 | /* | |
1104 | * Returns 0 if not found, -1 on failure, and 1 on success | |
1105 | */ | |
1106 | static inline int search_dirblock(struct buffer_head *bh, | |
1107 | struct inode *dir, | |
1108 | const struct qstr *d_name, | |
1109 | unsigned int offset, | |
1110 | struct ext4_dir_entry_2 ** res_dir) | |
1111 | { | |
1112 | struct ext4_dir_entry_2 * de; | |
1113 | char * dlimit; | |
1114 | int de_len; | |
1115 | const char *name = d_name->name; | |
1116 | int namelen = d_name->len; | |
1117 | ||
1118 | de = (struct ext4_dir_entry_2 *) bh->b_data; | |
1119 | dlimit = bh->b_data + dir->i_sb->s_blocksize; | |
1120 | while ((char *) de < dlimit) { | |
1121 | /* this code is executed quadratically often */ | |
1122 | /* do minimal checking `by hand' */ | |
1123 | ||
1124 | if ((char *) de + namelen <= dlimit && | |
1125 | ext4_match (namelen, name, de)) { | |
1126 | /* found a match - just to be sure, do a full check */ | |
1127 | if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data, | |
1128 | bh->b_size, offset)) | |
1129 | return -1; | |
1130 | *res_dir = de; | |
1131 | return 1; | |
1132 | } | |
1133 | /* prevent looping on a bad block */ | |
1134 | de_len = ext4_rec_len_from_disk(de->rec_len, | |
1135 | dir->i_sb->s_blocksize); | |
1136 | if (de_len <= 0) | |
1137 | return -1; | |
1138 | offset += de_len; | |
1139 | de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); | |
1140 | } | |
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block, | |
1145 | struct ext4_dir_entry *de) | |
1146 | { | |
1147 | struct super_block *sb = dir->i_sb; | |
1148 | ||
1149 | if (!is_dx(dir)) | |
1150 | return 0; | |
1151 | if (block == 0) | |
1152 | return 1; | |
1153 | if (de->inode == 0 && | |
1154 | ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) == | |
1155 | sb->s_blocksize) | |
1156 | return 1; | |
1157 | return 0; | |
1158 | } | |
1159 | ||
1160 | /* | |
1161 | * ext4_find_entry() | |
1162 | * | |
1163 | * finds an entry in the specified directory with the wanted name. It | |
1164 | * returns the cache buffer in which the entry was found, and the entry | |
1165 | * itself (as a parameter - res_dir). It does NOT read the inode of the | |
1166 | * entry - you'll have to do that yourself if you want to. | |
1167 | * | |
1168 | * The returned buffer_head has ->b_count elevated. The caller is expected | |
1169 | * to brelse() it when appropriate. | |
1170 | */ | |
1171 | static struct buffer_head * ext4_find_entry (struct inode *dir, | |
1172 | const struct qstr *d_name, | |
1173 | struct ext4_dir_entry_2 ** res_dir) | |
1174 | { | |
1175 | struct super_block *sb; | |
1176 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; | |
1177 | struct buffer_head *bh, *ret = NULL; | |
1178 | ext4_lblk_t start, block, b; | |
1179 | const u8 *name = d_name->name; | |
1180 | int ra_max = 0; /* Number of bh's in the readahead | |
1181 | buffer, bh_use[] */ | |
1182 | int ra_ptr = 0; /* Current index into readahead | |
1183 | buffer */ | |
1184 | int num = 0; | |
1185 | ext4_lblk_t nblocks; | |
1186 | int i, err; | |
1187 | int namelen; | |
1188 | ||
1189 | *res_dir = NULL; | |
1190 | sb = dir->i_sb; | |
1191 | namelen = d_name->len; | |
1192 | if (namelen > EXT4_NAME_LEN) | |
1193 | return NULL; | |
1194 | if ((namelen <= 2) && (name[0] == '.') && | |
1195 | (name[1] == '.' || name[1] == '\0')) { | |
1196 | /* | |
1197 | * "." or ".." will only be in the first block | |
1198 | * NFS may look up ".."; "." should be handled by the VFS | |
1199 | */ | |
1200 | block = start = 0; | |
1201 | nblocks = 1; | |
1202 | goto restart; | |
1203 | } | |
1204 | if (is_dx(dir)) { | |
1205 | bh = ext4_dx_find_entry(dir, d_name, res_dir, &err); | |
1206 | /* | |
1207 | * On success, or if the error was file not found, | |
1208 | * return. Otherwise, fall back to doing a search the | |
1209 | * old fashioned way. | |
1210 | */ | |
1211 | if (bh || (err != ERR_BAD_DX_DIR)) | |
1212 | return bh; | |
1213 | dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, " | |
1214 | "falling back\n")); | |
1215 | } | |
1216 | nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); | |
1217 | start = EXT4_I(dir)->i_dir_start_lookup; | |
1218 | if (start >= nblocks) | |
1219 | start = 0; | |
1220 | block = start; | |
1221 | restart: | |
1222 | do { | |
1223 | /* | |
1224 | * We deal with the read-ahead logic here. | |
1225 | */ | |
1226 | if (ra_ptr >= ra_max) { | |
1227 | /* Refill the readahead buffer */ | |
1228 | ra_ptr = 0; | |
1229 | b = block; | |
1230 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { | |
1231 | /* | |
1232 | * Terminate if we reach the end of the | |
1233 | * directory and must wrap, or if our | |
1234 | * search has finished at this block. | |
1235 | */ | |
1236 | if (b >= nblocks || (num && block == start)) { | |
1237 | bh_use[ra_max] = NULL; | |
1238 | break; | |
1239 | } | |
1240 | num++; | |
1241 | bh = ext4_getblk(NULL, dir, b++, 0, &err); | |
1242 | bh_use[ra_max] = bh; | |
1243 | if (bh) | |
1244 | ll_rw_block(READ | REQ_META | REQ_PRIO, | |
1245 | 1, &bh); | |
1246 | } | |
1247 | } | |
1248 | if ((bh = bh_use[ra_ptr++]) == NULL) | |
1249 | goto next; | |
1250 | wait_on_buffer(bh); | |
1251 | if (!buffer_uptodate(bh)) { | |
1252 | /* read error, skip block & hope for the best */ | |
1253 | EXT4_ERROR_INODE(dir, "reading directory lblock %lu", | |
1254 | (unsigned long) block); | |
1255 | brelse(bh); | |
1256 | goto next; | |
1257 | } | |
1258 | if (!buffer_verified(bh) && | |
1259 | !is_dx_internal_node(dir, block, | |
1260 | (struct ext4_dir_entry *)bh->b_data) && | |
1261 | !ext4_dirent_csum_verify(dir, | |
1262 | (struct ext4_dir_entry *)bh->b_data)) { | |
1263 | EXT4_ERROR_INODE(dir, "checksumming directory " | |
1264 | "block %lu", (unsigned long)block); | |
1265 | brelse(bh); | |
1266 | goto next; | |
1267 | } | |
1268 | set_buffer_verified(bh); | |
1269 | i = search_dirblock(bh, dir, d_name, | |
1270 | block << EXT4_BLOCK_SIZE_BITS(sb), res_dir); | |
1271 | if (i == 1) { | |
1272 | EXT4_I(dir)->i_dir_start_lookup = block; | |
1273 | ret = bh; | |
1274 | goto cleanup_and_exit; | |
1275 | } else { | |
1276 | brelse(bh); | |
1277 | if (i < 0) | |
1278 | goto cleanup_and_exit; | |
1279 | } | |
1280 | next: | |
1281 | if (++block >= nblocks) | |
1282 | block = 0; | |
1283 | } while (block != start); | |
1284 | ||
1285 | /* | |
1286 | * If the directory has grown while we were searching, then | |
1287 | * search the last part of the directory before giving up. | |
1288 | */ | |
1289 | block = nblocks; | |
1290 | nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); | |
1291 | if (block < nblocks) { | |
1292 | start = 0; | |
1293 | goto restart; | |
1294 | } | |
1295 | ||
1296 | cleanup_and_exit: | |
1297 | /* Clean up the read-ahead blocks */ | |
1298 | for (; ra_ptr < ra_max; ra_ptr++) | |
1299 | brelse(bh_use[ra_ptr]); | |
1300 | return ret; | |
1301 | } | |
1302 | ||
1303 | static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name, | |
1304 | struct ext4_dir_entry_2 **res_dir, int *err) | |
1305 | { | |
1306 | struct super_block * sb = dir->i_sb; | |
1307 | struct dx_hash_info hinfo; | |
1308 | struct dx_frame frames[2], *frame; | |
1309 | struct buffer_head *bh; | |
1310 | ext4_lblk_t block; | |
1311 | int retval; | |
1312 | ||
1313 | if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err))) | |
1314 | return NULL; | |
1315 | do { | |
1316 | block = dx_get_block(frame->at); | |
1317 | if (!(bh = ext4_bread(NULL, dir, block, 0, err))) { | |
1318 | if (!(*err)) { | |
1319 | *err = -EIO; | |
1320 | ext4_error(dir->i_sb, | |
1321 | "Directory hole detected on inode %lu\n", | |
1322 | dir->i_ino); | |
1323 | } | |
1324 | goto errout; | |
1325 | } | |
1326 | ||
1327 | if (!buffer_verified(bh) && | |
1328 | !ext4_dirent_csum_verify(dir, | |
1329 | (struct ext4_dir_entry *)bh->b_data)) { | |
1330 | EXT4_ERROR_INODE(dir, "checksumming directory " | |
1331 | "block %lu", (unsigned long)block); | |
1332 | brelse(bh); | |
1333 | *err = -EIO; | |
1334 | goto errout; | |
1335 | } | |
1336 | set_buffer_verified(bh); | |
1337 | retval = search_dirblock(bh, dir, d_name, | |
1338 | block << EXT4_BLOCK_SIZE_BITS(sb), | |
1339 | res_dir); | |
1340 | if (retval == 1) { /* Success! */ | |
1341 | dx_release(frames); | |
1342 | return bh; | |
1343 | } | |
1344 | brelse(bh); | |
1345 | if (retval == -1) { | |
1346 | *err = ERR_BAD_DX_DIR; | |
1347 | goto errout; | |
1348 | } | |
1349 | ||
1350 | /* Check to see if we should continue to search */ | |
1351 | retval = ext4_htree_next_block(dir, hinfo.hash, frame, | |
1352 | frames, NULL); | |
1353 | if (retval < 0) { | |
1354 | ext4_warning(sb, | |
1355 | "error reading index page in directory #%lu", | |
1356 | dir->i_ino); | |
1357 | *err = retval; | |
1358 | goto errout; | |
1359 | } | |
1360 | } while (retval == 1); | |
1361 | ||
1362 | *err = -ENOENT; | |
1363 | errout: | |
1364 | dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name)); | |
1365 | dx_release (frames); | |
1366 | return NULL; | |
1367 | } | |
1368 | ||
1369 | static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) | |
1370 | { | |
1371 | struct inode *inode; | |
1372 | struct ext4_dir_entry_2 *de; | |
1373 | struct buffer_head *bh; | |
1374 | ||
1375 | if (dentry->d_name.len > EXT4_NAME_LEN) | |
1376 | return ERR_PTR(-ENAMETOOLONG); | |
1377 | ||
1378 | bh = ext4_find_entry(dir, &dentry->d_name, &de); | |
1379 | inode = NULL; | |
1380 | if (bh) { | |
1381 | __u32 ino = le32_to_cpu(de->inode); | |
1382 | brelse(bh); | |
1383 | if (!ext4_valid_inum(dir->i_sb, ino)) { | |
1384 | EXT4_ERROR_INODE(dir, "bad inode number: %u", ino); | |
1385 | return ERR_PTR(-EIO); | |
1386 | } | |
1387 | if (unlikely(ino == dir->i_ino)) { | |
1388 | EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir", | |
1389 | dentry->d_name.len, | |
1390 | dentry->d_name.name); | |
1391 | return ERR_PTR(-EIO); | |
1392 | } | |
1393 | inode = ext4_iget(dir->i_sb, ino); | |
1394 | if (inode == ERR_PTR(-ESTALE)) { | |
1395 | EXT4_ERROR_INODE(dir, | |
1396 | "deleted inode referenced: %u", | |
1397 | ino); | |
1398 | return ERR_PTR(-EIO); | |
1399 | } | |
1400 | } | |
1401 | return d_splice_alias(inode, dentry); | |
1402 | } | |
1403 | ||
1404 | ||
1405 | struct dentry *ext4_get_parent(struct dentry *child) | |
1406 | { | |
1407 | __u32 ino; | |
1408 | static const struct qstr dotdot = QSTR_INIT("..", 2); | |
1409 | struct ext4_dir_entry_2 * de; | |
1410 | struct buffer_head *bh; | |
1411 | ||
1412 | bh = ext4_find_entry(child->d_inode, &dotdot, &de); | |
1413 | if (!bh) | |
1414 | return ERR_PTR(-ENOENT); | |
1415 | ino = le32_to_cpu(de->inode); | |
1416 | brelse(bh); | |
1417 | ||
1418 | if (!ext4_valid_inum(child->d_inode->i_sb, ino)) { | |
1419 | EXT4_ERROR_INODE(child->d_inode, | |
1420 | "bad parent inode number: %u", ino); | |
1421 | return ERR_PTR(-EIO); | |
1422 | } | |
1423 | ||
1424 | return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino)); | |
1425 | } | |
1426 | ||
1427 | #define S_SHIFT 12 | |
1428 | static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = { | |
1429 | [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE, | |
1430 | [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR, | |
1431 | [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV, | |
1432 | [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV, | |
1433 | [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO, | |
1434 | [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK, | |
1435 | [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK, | |
1436 | }; | |
1437 | ||
1438 | static inline void ext4_set_de_type(struct super_block *sb, | |
1439 | struct ext4_dir_entry_2 *de, | |
1440 | umode_t mode) { | |
1441 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE)) | |
1442 | de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; | |
1443 | } | |
1444 | ||
1445 | /* | |
1446 | * Move count entries from end of map between two memory locations. | |
1447 | * Returns pointer to last entry moved. | |
1448 | */ | |
1449 | static struct ext4_dir_entry_2 * | |
1450 | dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count, | |
1451 | unsigned blocksize) | |
1452 | { | |
1453 | unsigned rec_len = 0; | |
1454 | ||
1455 | while (count--) { | |
1456 | struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) | |
1457 | (from + (map->offs<<2)); | |
1458 | rec_len = EXT4_DIR_REC_LEN(de->name_len); | |
1459 | memcpy (to, de, rec_len); | |
1460 | ((struct ext4_dir_entry_2 *) to)->rec_len = | |
1461 | ext4_rec_len_to_disk(rec_len, blocksize); | |
1462 | de->inode = 0; | |
1463 | map++; | |
1464 | to += rec_len; | |
1465 | } | |
1466 | return (struct ext4_dir_entry_2 *) (to - rec_len); | |
1467 | } | |
1468 | ||
1469 | /* | |
1470 | * Compact each dir entry in the range to the minimal rec_len. | |
1471 | * Returns pointer to last entry in range. | |
1472 | */ | |
1473 | static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize) | |
1474 | { | |
1475 | struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base; | |
1476 | unsigned rec_len = 0; | |
1477 | ||
1478 | prev = to = de; | |
1479 | while ((char*)de < base + blocksize) { | |
1480 | next = ext4_next_entry(de, blocksize); | |
1481 | if (de->inode && de->name_len) { | |
1482 | rec_len = EXT4_DIR_REC_LEN(de->name_len); | |
1483 | if (de > to) | |
1484 | memmove(to, de, rec_len); | |
1485 | to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize); | |
1486 | prev = to; | |
1487 | to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len); | |
1488 | } | |
1489 | de = next; | |
1490 | } | |
1491 | return prev; | |
1492 | } | |
1493 | ||
1494 | /* | |
1495 | * Split a full leaf block to make room for a new dir entry. | |
1496 | * Allocate a new block, and move entries so that they are approx. equally full. | |
1497 | * Returns pointer to de in block into which the new entry will be inserted. | |
1498 | */ | |
1499 | static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, | |
1500 | struct buffer_head **bh,struct dx_frame *frame, | |
1501 | struct dx_hash_info *hinfo, int *error) | |
1502 | { | |
1503 | unsigned blocksize = dir->i_sb->s_blocksize; | |
1504 | unsigned count, continued; | |
1505 | struct buffer_head *bh2; | |
1506 | ext4_lblk_t newblock; | |
1507 | u32 hash2; | |
1508 | struct dx_map_entry *map; | |
1509 | char *data1 = (*bh)->b_data, *data2; | |
1510 | unsigned split, move, size; | |
1511 | struct ext4_dir_entry_2 *de = NULL, *de2; | |
1512 | struct ext4_dir_entry_tail *t; | |
1513 | int csum_size = 0; | |
1514 | int err = 0, i; | |
1515 | ||
1516 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, | |
1517 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
1518 | csum_size = sizeof(struct ext4_dir_entry_tail); | |
1519 | ||
1520 | bh2 = ext4_append (handle, dir, &newblock, &err); | |
1521 | if (!(bh2)) { | |
1522 | brelse(*bh); | |
1523 | *bh = NULL; | |
1524 | goto errout; | |
1525 | } | |
1526 | ||
1527 | BUFFER_TRACE(*bh, "get_write_access"); | |
1528 | err = ext4_journal_get_write_access(handle, *bh); | |
1529 | if (err) | |
1530 | goto journal_error; | |
1531 | ||
1532 | BUFFER_TRACE(frame->bh, "get_write_access"); | |
1533 | err = ext4_journal_get_write_access(handle, frame->bh); | |
1534 | if (err) | |
1535 | goto journal_error; | |
1536 | ||
1537 | data2 = bh2->b_data; | |
1538 | ||
1539 | /* create map in the end of data2 block */ | |
1540 | map = (struct dx_map_entry *) (data2 + blocksize); | |
1541 | count = dx_make_map((struct ext4_dir_entry_2 *) data1, | |
1542 | blocksize, hinfo, map); | |
1543 | map -= count; | |
1544 | dx_sort_map(map, count); | |
1545 | /* Split the existing block in the middle, size-wise */ | |
1546 | size = 0; | |
1547 | move = 0; | |
1548 | for (i = count-1; i >= 0; i--) { | |
1549 | /* is more than half of this entry in 2nd half of the block? */ | |
1550 | if (size + map[i].size/2 > blocksize/2) | |
1551 | break; | |
1552 | size += map[i].size; | |
1553 | move++; | |
1554 | } | |
1555 | /* map index at which we will split */ | |
1556 | split = count - move; | |
1557 | hash2 = map[split].hash; | |
1558 | continued = hash2 == map[split - 1].hash; | |
1559 | dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n", | |
1560 | (unsigned long)dx_get_block(frame->at), | |
1561 | hash2, split, count-split)); | |
1562 | ||
1563 | /* Fancy dance to stay within two buffers */ | |
1564 | de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize); | |
1565 | de = dx_pack_dirents(data1, blocksize); | |
1566 | de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - | |
1567 | (char *) de, | |
1568 | blocksize); | |
1569 | de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) - | |
1570 | (char *) de2, | |
1571 | blocksize); | |
1572 | if (csum_size) { | |
1573 | t = EXT4_DIRENT_TAIL(data2, blocksize); | |
1574 | initialize_dirent_tail(t, blocksize); | |
1575 | ||
1576 | t = EXT4_DIRENT_TAIL(data1, blocksize); | |
1577 | initialize_dirent_tail(t, blocksize); | |
1578 | } | |
1579 | ||
1580 | dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1)); | |
1581 | dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1)); | |
1582 | ||
1583 | /* Which block gets the new entry? */ | |
1584 | if (hinfo->hash >= hash2) | |
1585 | { | |
1586 | swap(*bh, bh2); | |
1587 | de = de2; | |
1588 | } | |
1589 | dx_insert_block(frame, hash2 + continued, newblock); | |
1590 | err = ext4_handle_dirty_dirent_node(handle, dir, bh2); | |
1591 | if (err) | |
1592 | goto journal_error; | |
1593 | err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); | |
1594 | if (err) | |
1595 | goto journal_error; | |
1596 | brelse(bh2); | |
1597 | dxtrace(dx_show_index("frame", frame->entries)); | |
1598 | return de; | |
1599 | ||
1600 | journal_error: | |
1601 | brelse(*bh); | |
1602 | brelse(bh2); | |
1603 | *bh = NULL; | |
1604 | ext4_std_error(dir->i_sb, err); | |
1605 | errout: | |
1606 | *error = err; | |
1607 | return NULL; | |
1608 | } | |
1609 | ||
1610 | int ext4_find_dest_de(struct inode *dir, struct inode *inode, | |
1611 | struct buffer_head *bh, | |
1612 | void *buf, int buf_size, | |
1613 | const char *name, int namelen, | |
1614 | struct ext4_dir_entry_2 **dest_de) | |
1615 | { | |
1616 | struct ext4_dir_entry_2 *de; | |
1617 | unsigned short reclen = EXT4_DIR_REC_LEN(namelen); | |
1618 | int nlen, rlen; | |
1619 | unsigned int offset = 0; | |
1620 | char *top; | |
1621 | ||
1622 | de = (struct ext4_dir_entry_2 *)buf; | |
1623 | top = buf + buf_size - reclen; | |
1624 | while ((char *) de <= top) { | |
1625 | if (ext4_check_dir_entry(dir, NULL, de, bh, | |
1626 | buf, buf_size, offset)) | |
1627 | return -EIO; | |
1628 | if (ext4_match(namelen, name, de)) | |
1629 | return -EEXIST; | |
1630 | nlen = EXT4_DIR_REC_LEN(de->name_len); | |
1631 | rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); | |
1632 | if ((de->inode ? rlen - nlen : rlen) >= reclen) | |
1633 | break; | |
1634 | de = (struct ext4_dir_entry_2 *)((char *)de + rlen); | |
1635 | offset += rlen; | |
1636 | } | |
1637 | if ((char *) de > top) | |
1638 | return -ENOSPC; | |
1639 | ||
1640 | *dest_de = de; | |
1641 | return 0; | |
1642 | } | |
1643 | ||
1644 | void ext4_insert_dentry(struct inode *inode, | |
1645 | struct ext4_dir_entry_2 *de, | |
1646 | int buf_size, | |
1647 | const char *name, int namelen) | |
1648 | { | |
1649 | ||
1650 | int nlen, rlen; | |
1651 | ||
1652 | nlen = EXT4_DIR_REC_LEN(de->name_len); | |
1653 | rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); | |
1654 | if (de->inode) { | |
1655 | struct ext4_dir_entry_2 *de1 = | |
1656 | (struct ext4_dir_entry_2 *)((char *)de + nlen); | |
1657 | de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size); | |
1658 | de->rec_len = ext4_rec_len_to_disk(nlen, buf_size); | |
1659 | de = de1; | |
1660 | } | |
1661 | de->file_type = EXT4_FT_UNKNOWN; | |
1662 | de->inode = cpu_to_le32(inode->i_ino); | |
1663 | ext4_set_de_type(inode->i_sb, de, inode->i_mode); | |
1664 | de->name_len = namelen; | |
1665 | memcpy(de->name, name, namelen); | |
1666 | } | |
1667 | /* | |
1668 | * Add a new entry into a directory (leaf) block. If de is non-NULL, | |
1669 | * it points to a directory entry which is guaranteed to be large | |
1670 | * enough for new directory entry. If de is NULL, then | |
1671 | * add_dirent_to_buf will attempt search the directory block for | |
1672 | * space. It will return -ENOSPC if no space is available, and -EIO | |
1673 | * and -EEXIST if directory entry already exists. | |
1674 | */ | |
1675 | static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry, | |
1676 | struct inode *inode, struct ext4_dir_entry_2 *de, | |
1677 | struct buffer_head *bh) | |
1678 | { | |
1679 | struct inode *dir = dentry->d_parent->d_inode; | |
1680 | const char *name = dentry->d_name.name; | |
1681 | int namelen = dentry->d_name.len; | |
1682 | unsigned int blocksize = dir->i_sb->s_blocksize; | |
1683 | unsigned short reclen; | |
1684 | int csum_size = 0; | |
1685 | int err; | |
1686 | ||
1687 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
1688 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
1689 | csum_size = sizeof(struct ext4_dir_entry_tail); | |
1690 | ||
1691 | reclen = EXT4_DIR_REC_LEN(namelen); | |
1692 | if (!de) { | |
1693 | err = ext4_find_dest_de(dir, inode, | |
1694 | bh, bh->b_data, blocksize - csum_size, | |
1695 | name, namelen, &de); | |
1696 | if (err) | |
1697 | return err; | |
1698 | } | |
1699 | BUFFER_TRACE(bh, "get_write_access"); | |
1700 | err = ext4_journal_get_write_access(handle, bh); | |
1701 | if (err) { | |
1702 | ext4_std_error(dir->i_sb, err); | |
1703 | return err; | |
1704 | } | |
1705 | ||
1706 | /* By now the buffer is marked for journaling */ | |
1707 | ext4_insert_dentry(inode, de, blocksize, name, namelen); | |
1708 | ||
1709 | /* | |
1710 | * XXX shouldn't update any times until successful | |
1711 | * completion of syscall, but too many callers depend | |
1712 | * on this. | |
1713 | * | |
1714 | * XXX similarly, too many callers depend on | |
1715 | * ext4_new_inode() setting the times, but error | |
1716 | * recovery deletes the inode, so the worst that can | |
1717 | * happen is that the times are slightly out of date | |
1718 | * and/or different from the directory change time. | |
1719 | */ | |
1720 | dir->i_mtime = dir->i_ctime = ext4_current_time(dir); | |
1721 | ext4_update_dx_flag(dir); | |
1722 | dir->i_version++; | |
1723 | ext4_mark_inode_dirty(handle, dir); | |
1724 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | |
1725 | err = ext4_handle_dirty_dirent_node(handle, dir, bh); | |
1726 | if (err) | |
1727 | ext4_std_error(dir->i_sb, err); | |
1728 | return 0; | |
1729 | } | |
1730 | ||
1731 | /* | |
1732 | * This converts a one block unindexed directory to a 3 block indexed | |
1733 | * directory, and adds the dentry to the indexed directory. | |
1734 | */ | |
1735 | static int make_indexed_dir(handle_t *handle, struct dentry *dentry, | |
1736 | struct inode *inode, struct buffer_head *bh) | |
1737 | { | |
1738 | struct inode *dir = dentry->d_parent->d_inode; | |
1739 | const char *name = dentry->d_name.name; | |
1740 | int namelen = dentry->d_name.len; | |
1741 | struct buffer_head *bh2; | |
1742 | struct dx_root *root; | |
1743 | struct dx_frame frames[2], *frame; | |
1744 | struct dx_entry *entries; | |
1745 | struct ext4_dir_entry_2 *de, *de2; | |
1746 | struct ext4_dir_entry_tail *t; | |
1747 | char *data1, *top; | |
1748 | unsigned len; | |
1749 | int retval; | |
1750 | unsigned blocksize; | |
1751 | struct dx_hash_info hinfo; | |
1752 | ext4_lblk_t block; | |
1753 | struct fake_dirent *fde; | |
1754 | int csum_size = 0; | |
1755 | ||
1756 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
1757 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
1758 | csum_size = sizeof(struct ext4_dir_entry_tail); | |
1759 | ||
1760 | blocksize = dir->i_sb->s_blocksize; | |
1761 | dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino)); | |
1762 | retval = ext4_journal_get_write_access(handle, bh); | |
1763 | if (retval) { | |
1764 | ext4_std_error(dir->i_sb, retval); | |
1765 | brelse(bh); | |
1766 | return retval; | |
1767 | } | |
1768 | root = (struct dx_root *) bh->b_data; | |
1769 | ||
1770 | /* The 0th block becomes the root, move the dirents out */ | |
1771 | fde = &root->dotdot; | |
1772 | de = (struct ext4_dir_entry_2 *)((char *)fde + | |
1773 | ext4_rec_len_from_disk(fde->rec_len, blocksize)); | |
1774 | if ((char *) de >= (((char *) root) + blocksize)) { | |
1775 | EXT4_ERROR_INODE(dir, "invalid rec_len for '..'"); | |
1776 | brelse(bh); | |
1777 | return -EIO; | |
1778 | } | |
1779 | len = ((char *) root) + (blocksize - csum_size) - (char *) de; | |
1780 | ||
1781 | /* Allocate new block for the 0th block's dirents */ | |
1782 | bh2 = ext4_append(handle, dir, &block, &retval); | |
1783 | if (!(bh2)) { | |
1784 | brelse(bh); | |
1785 | return retval; | |
1786 | } | |
1787 | ext4_set_inode_flag(dir, EXT4_INODE_INDEX); | |
1788 | data1 = bh2->b_data; | |
1789 | ||
1790 | memcpy (data1, de, len); | |
1791 | de = (struct ext4_dir_entry_2 *) data1; | |
1792 | top = data1 + len; | |
1793 | while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) | |
1794 | de = de2; | |
1795 | de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - | |
1796 | (char *) de, | |
1797 | blocksize); | |
1798 | ||
1799 | if (csum_size) { | |
1800 | t = EXT4_DIRENT_TAIL(data1, blocksize); | |
1801 | initialize_dirent_tail(t, blocksize); | |
1802 | } | |
1803 | ||
1804 | /* Initialize the root; the dot dirents already exist */ | |
1805 | de = (struct ext4_dir_entry_2 *) (&root->dotdot); | |
1806 | de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2), | |
1807 | blocksize); | |
1808 | memset (&root->info, 0, sizeof(root->info)); | |
1809 | root->info.info_length = sizeof(root->info); | |
1810 | root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; | |
1811 | entries = root->entries; | |
1812 | dx_set_block(entries, 1); | |
1813 | dx_set_count(entries, 1); | |
1814 | dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info))); | |
1815 | ||
1816 | /* Initialize as for dx_probe */ | |
1817 | hinfo.hash_version = root->info.hash_version; | |
1818 | if (hinfo.hash_version <= DX_HASH_TEA) | |
1819 | hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; | |
1820 | hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; | |
1821 | ext4fs_dirhash(name, namelen, &hinfo); | |
1822 | frame = frames; | |
1823 | frame->entries = entries; | |
1824 | frame->at = entries; | |
1825 | frame->bh = bh; | |
1826 | bh = bh2; | |
1827 | ||
1828 | ext4_handle_dirty_dx_node(handle, dir, frame->bh); | |
1829 | ext4_handle_dirty_dirent_node(handle, dir, bh); | |
1830 | ||
1831 | de = do_split(handle,dir, &bh, frame, &hinfo, &retval); | |
1832 | if (!de) { | |
1833 | /* | |
1834 | * Even if the block split failed, we have to properly write | |
1835 | * out all the changes we did so far. Otherwise we can end up | |
1836 | * with corrupted filesystem. | |
1837 | */ | |
1838 | ext4_mark_inode_dirty(handle, dir); | |
1839 | dx_release(frames); | |
1840 | return retval; | |
1841 | } | |
1842 | dx_release(frames); | |
1843 | ||
1844 | retval = add_dirent_to_buf(handle, dentry, inode, de, bh); | |
1845 | brelse(bh); | |
1846 | return retval; | |
1847 | } | |
1848 | ||
1849 | /* | |
1850 | * ext4_add_entry() | |
1851 | * | |
1852 | * adds a file entry to the specified directory, using the same | |
1853 | * semantics as ext4_find_entry(). It returns NULL if it failed. | |
1854 | * | |
1855 | * NOTE!! The inode part of 'de' is left at 0 - which means you | |
1856 | * may not sleep between calling this and putting something into | |
1857 | * the entry, as someone else might have used it while you slept. | |
1858 | */ | |
1859 | static int ext4_add_entry(handle_t *handle, struct dentry *dentry, | |
1860 | struct inode *inode) | |
1861 | { | |
1862 | struct inode *dir = dentry->d_parent->d_inode; | |
1863 | struct buffer_head *bh; | |
1864 | struct ext4_dir_entry_2 *de; | |
1865 | struct ext4_dir_entry_tail *t; | |
1866 | struct super_block *sb; | |
1867 | int retval; | |
1868 | int dx_fallback=0; | |
1869 | unsigned blocksize; | |
1870 | ext4_lblk_t block, blocks; | |
1871 | int csum_size = 0; | |
1872 | ||
1873 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
1874 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
1875 | csum_size = sizeof(struct ext4_dir_entry_tail); | |
1876 | ||
1877 | sb = dir->i_sb; | |
1878 | blocksize = sb->s_blocksize; | |
1879 | if (!dentry->d_name.len) | |
1880 | return -EINVAL; | |
1881 | if (is_dx(dir)) { | |
1882 | retval = ext4_dx_add_entry(handle, dentry, inode); | |
1883 | if (!retval || (retval != ERR_BAD_DX_DIR)) | |
1884 | return retval; | |
1885 | ext4_clear_inode_flag(dir, EXT4_INODE_INDEX); | |
1886 | dx_fallback++; | |
1887 | ext4_mark_inode_dirty(handle, dir); | |
1888 | } | |
1889 | blocks = dir->i_size >> sb->s_blocksize_bits; | |
1890 | for (block = 0; block < blocks; block++) { | |
1891 | if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) { | |
1892 | if (!retval) { | |
1893 | retval = -EIO; | |
1894 | ext4_error(inode->i_sb, | |
1895 | "Directory hole detected on inode %lu\n", | |
1896 | inode->i_ino); | |
1897 | } | |
1898 | return retval; | |
1899 | } | |
1900 | if (!buffer_verified(bh) && | |
1901 | !ext4_dirent_csum_verify(dir, | |
1902 | (struct ext4_dir_entry *)bh->b_data)) | |
1903 | return -EIO; | |
1904 | set_buffer_verified(bh); | |
1905 | retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh); | |
1906 | if (retval != -ENOSPC) { | |
1907 | brelse(bh); | |
1908 | return retval; | |
1909 | } | |
1910 | ||
1911 | if (blocks == 1 && !dx_fallback && | |
1912 | EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) | |
1913 | return make_indexed_dir(handle, dentry, inode, bh); | |
1914 | brelse(bh); | |
1915 | } | |
1916 | bh = ext4_append(handle, dir, &block, &retval); | |
1917 | if (!bh) | |
1918 | return retval; | |
1919 | de = (struct ext4_dir_entry_2 *) bh->b_data; | |
1920 | de->inode = 0; | |
1921 | de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize); | |
1922 | ||
1923 | if (csum_size) { | |
1924 | t = EXT4_DIRENT_TAIL(bh->b_data, blocksize); | |
1925 | initialize_dirent_tail(t, blocksize); | |
1926 | } | |
1927 | ||
1928 | retval = add_dirent_to_buf(handle, dentry, inode, de, bh); | |
1929 | brelse(bh); | |
1930 | if (retval == 0) | |
1931 | ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY); | |
1932 | return retval; | |
1933 | } | |
1934 | ||
1935 | /* | |
1936 | * Returns 0 for success, or a negative error value | |
1937 | */ | |
1938 | static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry, | |
1939 | struct inode *inode) | |
1940 | { | |
1941 | struct dx_frame frames[2], *frame; | |
1942 | struct dx_entry *entries, *at; | |
1943 | struct dx_hash_info hinfo; | |
1944 | struct buffer_head *bh; | |
1945 | struct inode *dir = dentry->d_parent->d_inode; | |
1946 | struct super_block *sb = dir->i_sb; | |
1947 | struct ext4_dir_entry_2 *de; | |
1948 | int err; | |
1949 | ||
1950 | frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err); | |
1951 | if (!frame) | |
1952 | return err; | |
1953 | entries = frame->entries; | |
1954 | at = frame->at; | |
1955 | ||
1956 | if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) { | |
1957 | if (!err) { | |
1958 | err = -EIO; | |
1959 | ext4_error(dir->i_sb, | |
1960 | "Directory hole detected on inode %lu\n", | |
1961 | dir->i_ino); | |
1962 | } | |
1963 | goto cleanup; | |
1964 | } | |
1965 | ||
1966 | if (!buffer_verified(bh) && | |
1967 | !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) | |
1968 | goto journal_error; | |
1969 | set_buffer_verified(bh); | |
1970 | ||
1971 | BUFFER_TRACE(bh, "get_write_access"); | |
1972 | err = ext4_journal_get_write_access(handle, bh); | |
1973 | if (err) | |
1974 | goto journal_error; | |
1975 | ||
1976 | err = add_dirent_to_buf(handle, dentry, inode, NULL, bh); | |
1977 | if (err != -ENOSPC) | |
1978 | goto cleanup; | |
1979 | ||
1980 | /* Block full, should compress but for now just split */ | |
1981 | dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n", | |
1982 | dx_get_count(entries), dx_get_limit(entries))); | |
1983 | /* Need to split index? */ | |
1984 | if (dx_get_count(entries) == dx_get_limit(entries)) { | |
1985 | ext4_lblk_t newblock; | |
1986 | unsigned icount = dx_get_count(entries); | |
1987 | int levels = frame - frames; | |
1988 | struct dx_entry *entries2; | |
1989 | struct dx_node *node2; | |
1990 | struct buffer_head *bh2; | |
1991 | ||
1992 | if (levels && (dx_get_count(frames->entries) == | |
1993 | dx_get_limit(frames->entries))) { | |
1994 | ext4_warning(sb, "Directory index full!"); | |
1995 | err = -ENOSPC; | |
1996 | goto cleanup; | |
1997 | } | |
1998 | bh2 = ext4_append (handle, dir, &newblock, &err); | |
1999 | if (!(bh2)) | |
2000 | goto cleanup; | |
2001 | node2 = (struct dx_node *)(bh2->b_data); | |
2002 | entries2 = node2->entries; | |
2003 | memset(&node2->fake, 0, sizeof(struct fake_dirent)); | |
2004 | node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize, | |
2005 | sb->s_blocksize); | |
2006 | BUFFER_TRACE(frame->bh, "get_write_access"); | |
2007 | err = ext4_journal_get_write_access(handle, frame->bh); | |
2008 | if (err) | |
2009 | goto journal_error; | |
2010 | if (levels) { | |
2011 | unsigned icount1 = icount/2, icount2 = icount - icount1; | |
2012 | unsigned hash2 = dx_get_hash(entries + icount1); | |
2013 | dxtrace(printk(KERN_DEBUG "Split index %i/%i\n", | |
2014 | icount1, icount2)); | |
2015 | ||
2016 | BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ | |
2017 | err = ext4_journal_get_write_access(handle, | |
2018 | frames[0].bh); | |
2019 | if (err) | |
2020 | goto journal_error; | |
2021 | ||
2022 | memcpy((char *) entries2, (char *) (entries + icount1), | |
2023 | icount2 * sizeof(struct dx_entry)); | |
2024 | dx_set_count(entries, icount1); | |
2025 | dx_set_count(entries2, icount2); | |
2026 | dx_set_limit(entries2, dx_node_limit(dir)); | |
2027 | ||
2028 | /* Which index block gets the new entry? */ | |
2029 | if (at - entries >= icount1) { | |
2030 | frame->at = at = at - entries - icount1 + entries2; | |
2031 | frame->entries = entries = entries2; | |
2032 | swap(frame->bh, bh2); | |
2033 | } | |
2034 | dx_insert_block(frames + 0, hash2, newblock); | |
2035 | dxtrace(dx_show_index("node", frames[1].entries)); | |
2036 | dxtrace(dx_show_index("node", | |
2037 | ((struct dx_node *) bh2->b_data)->entries)); | |
2038 | err = ext4_handle_dirty_dx_node(handle, dir, bh2); | |
2039 | if (err) | |
2040 | goto journal_error; | |
2041 | brelse (bh2); | |
2042 | } else { | |
2043 | dxtrace(printk(KERN_DEBUG | |
2044 | "Creating second level index...\n")); | |
2045 | memcpy((char *) entries2, (char *) entries, | |
2046 | icount * sizeof(struct dx_entry)); | |
2047 | dx_set_limit(entries2, dx_node_limit(dir)); | |
2048 | ||
2049 | /* Set up root */ | |
2050 | dx_set_count(entries, 1); | |
2051 | dx_set_block(entries + 0, newblock); | |
2052 | ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1; | |
2053 | ||
2054 | /* Add new access path frame */ | |
2055 | frame = frames + 1; | |
2056 | frame->at = at = at - entries + entries2; | |
2057 | frame->entries = entries = entries2; | |
2058 | frame->bh = bh2; | |
2059 | err = ext4_journal_get_write_access(handle, | |
2060 | frame->bh); | |
2061 | if (err) | |
2062 | goto journal_error; | |
2063 | } | |
2064 | err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh); | |
2065 | if (err) { | |
2066 | ext4_std_error(inode->i_sb, err); | |
2067 | goto cleanup; | |
2068 | } | |
2069 | } | |
2070 | de = do_split(handle, dir, &bh, frame, &hinfo, &err); | |
2071 | if (!de) | |
2072 | goto cleanup; | |
2073 | err = add_dirent_to_buf(handle, dentry, inode, de, bh); | |
2074 | goto cleanup; | |
2075 | ||
2076 | journal_error: | |
2077 | ext4_std_error(dir->i_sb, err); | |
2078 | cleanup: | |
2079 | if (bh) | |
2080 | brelse(bh); | |
2081 | dx_release(frames); | |
2082 | return err; | |
2083 | } | |
2084 | ||
2085 | /* | |
2086 | * ext4_delete_entry deletes a directory entry by merging it with the | |
2087 | * previous entry | |
2088 | */ | |
2089 | static int ext4_delete_entry(handle_t *handle, | |
2090 | struct inode *dir, | |
2091 | struct ext4_dir_entry_2 *de_del, | |
2092 | struct buffer_head *bh) | |
2093 | { | |
2094 | struct ext4_dir_entry_2 *de, *pde; | |
2095 | unsigned int blocksize = dir->i_sb->s_blocksize; | |
2096 | int csum_size = 0; | |
2097 | int i, err; | |
2098 | ||
2099 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, | |
2100 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
2101 | csum_size = sizeof(struct ext4_dir_entry_tail); | |
2102 | ||
2103 | i = 0; | |
2104 | pde = NULL; | |
2105 | de = (struct ext4_dir_entry_2 *) bh->b_data; | |
2106 | while (i < bh->b_size - csum_size) { | |
2107 | if (ext4_check_dir_entry(dir, NULL, de, bh, | |
2108 | bh->b_data, bh->b_size, i)) | |
2109 | return -EIO; | |
2110 | if (de == de_del) { | |
2111 | BUFFER_TRACE(bh, "get_write_access"); | |
2112 | err = ext4_journal_get_write_access(handle, bh); | |
2113 | if (unlikely(err)) { | |
2114 | ext4_std_error(dir->i_sb, err); | |
2115 | return err; | |
2116 | } | |
2117 | if (pde) | |
2118 | pde->rec_len = ext4_rec_len_to_disk( | |
2119 | ext4_rec_len_from_disk(pde->rec_len, | |
2120 | blocksize) + | |
2121 | ext4_rec_len_from_disk(de->rec_len, | |
2122 | blocksize), | |
2123 | blocksize); | |
2124 | else | |
2125 | de->inode = 0; | |
2126 | dir->i_version++; | |
2127 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | |
2128 | err = ext4_handle_dirty_dirent_node(handle, dir, bh); | |
2129 | if (unlikely(err)) { | |
2130 | ext4_std_error(dir->i_sb, err); | |
2131 | return err; | |
2132 | } | |
2133 | return 0; | |
2134 | } | |
2135 | i += ext4_rec_len_from_disk(de->rec_len, blocksize); | |
2136 | pde = de; | |
2137 | de = ext4_next_entry(de, blocksize); | |
2138 | } | |
2139 | return -ENOENT; | |
2140 | } | |
2141 | ||
2142 | /* | |
2143 | * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2, | |
2144 | * since this indicates that nlinks count was previously 1. | |
2145 | */ | |
2146 | static void ext4_inc_count(handle_t *handle, struct inode *inode) | |
2147 | { | |
2148 | inc_nlink(inode); | |
2149 | if (is_dx(inode) && inode->i_nlink > 1) { | |
2150 | /* limit is 16-bit i_links_count */ | |
2151 | if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) { | |
2152 | set_nlink(inode, 1); | |
2153 | EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb, | |
2154 | EXT4_FEATURE_RO_COMPAT_DIR_NLINK); | |
2155 | } | |
2156 | } | |
2157 | } | |
2158 | ||
2159 | /* | |
2160 | * If a directory had nlink == 1, then we should let it be 1. This indicates | |
2161 | * directory has >EXT4_LINK_MAX subdirs. | |
2162 | */ | |
2163 | static void ext4_dec_count(handle_t *handle, struct inode *inode) | |
2164 | { | |
2165 | if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) | |
2166 | drop_nlink(inode); | |
2167 | } | |
2168 | ||
2169 | ||
2170 | static int ext4_add_nondir(handle_t *handle, | |
2171 | struct dentry *dentry, struct inode *inode) | |
2172 | { | |
2173 | int err = ext4_add_entry(handle, dentry, inode); | |
2174 | if (!err) { | |
2175 | ext4_mark_inode_dirty(handle, inode); | |
2176 | unlock_new_inode(inode); | |
2177 | d_instantiate(dentry, inode); | |
2178 | return 0; | |
2179 | } | |
2180 | drop_nlink(inode); | |
2181 | unlock_new_inode(inode); | |
2182 | iput(inode); | |
2183 | return err; | |
2184 | } | |
2185 | ||
2186 | /* | |
2187 | * By the time this is called, we already have created | |
2188 | * the directory cache entry for the new file, but it | |
2189 | * is so far negative - it has no inode. | |
2190 | * | |
2191 | * If the create succeeds, we fill in the inode information | |
2192 | * with d_instantiate(). | |
2193 | */ | |
2194 | static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode, | |
2195 | bool excl) | |
2196 | { | |
2197 | handle_t *handle; | |
2198 | struct inode *inode; | |
2199 | int err, retries = 0; | |
2200 | ||
2201 | dquot_initialize(dir); | |
2202 | ||
2203 | retry: | |
2204 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + | |
2205 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + | |
2206 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); | |
2207 | if (IS_ERR(handle)) | |
2208 | return PTR_ERR(handle); | |
2209 | ||
2210 | if (IS_DIRSYNC(dir)) | |
2211 | ext4_handle_sync(handle); | |
2212 | ||
2213 | inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL); | |
2214 | err = PTR_ERR(inode); | |
2215 | if (!IS_ERR(inode)) { | |
2216 | inode->i_op = &ext4_file_inode_operations; | |
2217 | inode->i_fop = &ext4_file_operations; | |
2218 | ext4_set_aops(inode); | |
2219 | err = ext4_add_nondir(handle, dentry, inode); | |
2220 | } | |
2221 | ext4_journal_stop(handle); | |
2222 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) | |
2223 | goto retry; | |
2224 | return err; | |
2225 | } | |
2226 | ||
2227 | static int ext4_mknod(struct inode *dir, struct dentry *dentry, | |
2228 | umode_t mode, dev_t rdev) | |
2229 | { | |
2230 | handle_t *handle; | |
2231 | struct inode *inode; | |
2232 | int err, retries = 0; | |
2233 | ||
2234 | if (!new_valid_dev(rdev)) | |
2235 | return -EINVAL; | |
2236 | ||
2237 | dquot_initialize(dir); | |
2238 | ||
2239 | retry: | |
2240 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + | |
2241 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + | |
2242 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); | |
2243 | if (IS_ERR(handle)) | |
2244 | return PTR_ERR(handle); | |
2245 | ||
2246 | if (IS_DIRSYNC(dir)) | |
2247 | ext4_handle_sync(handle); | |
2248 | ||
2249 | inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL); | |
2250 | err = PTR_ERR(inode); | |
2251 | if (!IS_ERR(inode)) { | |
2252 | init_special_inode(inode, inode->i_mode, rdev); | |
2253 | inode->i_op = &ext4_special_inode_operations; | |
2254 | err = ext4_add_nondir(handle, dentry, inode); | |
2255 | } | |
2256 | ext4_journal_stop(handle); | |
2257 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) | |
2258 | goto retry; | |
2259 | return err; | |
2260 | } | |
2261 | ||
2262 | struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode, | |
2263 | struct ext4_dir_entry_2 *de, | |
2264 | int blocksize, int csum_size, | |
2265 | unsigned int parent_ino, int dotdot_real_len) | |
2266 | { | |
2267 | de->inode = cpu_to_le32(inode->i_ino); | |
2268 | de->name_len = 1; | |
2269 | de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len), | |
2270 | blocksize); | |
2271 | strcpy(de->name, "."); | |
2272 | ext4_set_de_type(inode->i_sb, de, S_IFDIR); | |
2273 | ||
2274 | de = ext4_next_entry(de, blocksize); | |
2275 | de->inode = cpu_to_le32(parent_ino); | |
2276 | de->name_len = 2; | |
2277 | if (!dotdot_real_len) | |
2278 | de->rec_len = ext4_rec_len_to_disk(blocksize - | |
2279 | (csum_size + EXT4_DIR_REC_LEN(1)), | |
2280 | blocksize); | |
2281 | else | |
2282 | de->rec_len = ext4_rec_len_to_disk( | |
2283 | EXT4_DIR_REC_LEN(de->name_len), blocksize); | |
2284 | strcpy(de->name, ".."); | |
2285 | ext4_set_de_type(inode->i_sb, de, S_IFDIR); | |
2286 | ||
2287 | return ext4_next_entry(de, blocksize); | |
2288 | } | |
2289 | ||
2290 | static int ext4_init_new_dir(handle_t *handle, struct inode *dir, | |
2291 | struct inode *inode) | |
2292 | { | |
2293 | struct buffer_head *dir_block = NULL; | |
2294 | struct ext4_dir_entry_2 *de; | |
2295 | struct ext4_dir_entry_tail *t; | |
2296 | unsigned int blocksize = dir->i_sb->s_blocksize; | |
2297 | int csum_size = 0; | |
2298 | int err; | |
2299 | ||
2300 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, | |
2301 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
2302 | csum_size = sizeof(struct ext4_dir_entry_tail); | |
2303 | ||
2304 | inode->i_size = EXT4_I(inode)->i_disksize = blocksize; | |
2305 | dir_block = ext4_bread(handle, inode, 0, 1, &err); | |
2306 | if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) { | |
2307 | if (!err) { | |
2308 | err = -EIO; | |
2309 | ext4_error(inode->i_sb, | |
2310 | "Directory hole detected on inode %lu\n", | |
2311 | inode->i_ino); | |
2312 | } | |
2313 | goto out; | |
2314 | } | |
2315 | BUFFER_TRACE(dir_block, "get_write_access"); | |
2316 | err = ext4_journal_get_write_access(handle, dir_block); | |
2317 | if (err) | |
2318 | goto out; | |
2319 | de = (struct ext4_dir_entry_2 *)dir_block->b_data; | |
2320 | ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0); | |
2321 | set_nlink(inode, 2); | |
2322 | if (csum_size) { | |
2323 | t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize); | |
2324 | initialize_dirent_tail(t, blocksize); | |
2325 | } | |
2326 | ||
2327 | BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata"); | |
2328 | err = ext4_handle_dirty_dirent_node(handle, inode, dir_block); | |
2329 | if (err) | |
2330 | goto out; | |
2331 | set_buffer_verified(dir_block); | |
2332 | out: | |
2333 | brelse(dir_block); | |
2334 | return err; | |
2335 | } | |
2336 | ||
2337 | static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) | |
2338 | { | |
2339 | handle_t *handle; | |
2340 | struct inode *inode; | |
2341 | int err, retries = 0; | |
2342 | ||
2343 | if (EXT4_DIR_LINK_MAX(dir)) | |
2344 | return -EMLINK; | |
2345 | ||
2346 | dquot_initialize(dir); | |
2347 | ||
2348 | retry: | |
2349 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + | |
2350 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + | |
2351 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); | |
2352 | if (IS_ERR(handle)) | |
2353 | return PTR_ERR(handle); | |
2354 | ||
2355 | if (IS_DIRSYNC(dir)) | |
2356 | ext4_handle_sync(handle); | |
2357 | ||
2358 | inode = ext4_new_inode(handle, dir, S_IFDIR | mode, | |
2359 | &dentry->d_name, 0, NULL); | |
2360 | err = PTR_ERR(inode); | |
2361 | if (IS_ERR(inode)) | |
2362 | goto out_stop; | |
2363 | ||
2364 | inode->i_op = &ext4_dir_inode_operations; | |
2365 | inode->i_fop = &ext4_dir_operations; | |
2366 | err = ext4_init_new_dir(handle, dir, inode); | |
2367 | if (err) | |
2368 | goto out_clear_inode; | |
2369 | err = ext4_mark_inode_dirty(handle, inode); | |
2370 | if (!err) | |
2371 | err = ext4_add_entry(handle, dentry, inode); | |
2372 | if (err) { | |
2373 | out_clear_inode: | |
2374 | clear_nlink(inode); | |
2375 | unlock_new_inode(inode); | |
2376 | ext4_mark_inode_dirty(handle, inode); | |
2377 | iput(inode); | |
2378 | goto out_stop; | |
2379 | } | |
2380 | ext4_inc_count(handle, dir); | |
2381 | ext4_update_dx_flag(dir); | |
2382 | err = ext4_mark_inode_dirty(handle, dir); | |
2383 | if (err) | |
2384 | goto out_clear_inode; | |
2385 | unlock_new_inode(inode); | |
2386 | d_instantiate(dentry, inode); | |
2387 | out_stop: | |
2388 | ext4_journal_stop(handle); | |
2389 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) | |
2390 | goto retry; | |
2391 | return err; | |
2392 | } | |
2393 | ||
2394 | /* | |
2395 | * routine to check that the specified directory is empty (for rmdir) | |
2396 | */ | |
2397 | static int empty_dir(struct inode *inode) | |
2398 | { | |
2399 | unsigned int offset; | |
2400 | struct buffer_head *bh; | |
2401 | struct ext4_dir_entry_2 *de, *de1; | |
2402 | struct super_block *sb; | |
2403 | int err = 0; | |
2404 | ||
2405 | sb = inode->i_sb; | |
2406 | if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) || | |
2407 | !(bh = ext4_bread(NULL, inode, 0, 0, &err))) { | |
2408 | if (err) | |
2409 | EXT4_ERROR_INODE(inode, | |
2410 | "error %d reading directory lblock 0", err); | |
2411 | else | |
2412 | ext4_warning(inode->i_sb, | |
2413 | "bad directory (dir #%lu) - no data block", | |
2414 | inode->i_ino); | |
2415 | return 1; | |
2416 | } | |
2417 | if (!buffer_verified(bh) && | |
2418 | !ext4_dirent_csum_verify(inode, | |
2419 | (struct ext4_dir_entry *)bh->b_data)) { | |
2420 | EXT4_ERROR_INODE(inode, "checksum error reading directory " | |
2421 | "lblock 0"); | |
2422 | return -EIO; | |
2423 | } | |
2424 | set_buffer_verified(bh); | |
2425 | de = (struct ext4_dir_entry_2 *) bh->b_data; | |
2426 | de1 = ext4_next_entry(de, sb->s_blocksize); | |
2427 | if (le32_to_cpu(de->inode) != inode->i_ino || | |
2428 | !le32_to_cpu(de1->inode) || | |
2429 | strcmp(".", de->name) || | |
2430 | strcmp("..", de1->name)) { | |
2431 | ext4_warning(inode->i_sb, | |
2432 | "bad directory (dir #%lu) - no `.' or `..'", | |
2433 | inode->i_ino); | |
2434 | brelse(bh); | |
2435 | return 1; | |
2436 | } | |
2437 | offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) + | |
2438 | ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize); | |
2439 | de = ext4_next_entry(de1, sb->s_blocksize); | |
2440 | while (offset < inode->i_size) { | |
2441 | if (!bh || | |
2442 | (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) { | |
2443 | unsigned int lblock; | |
2444 | err = 0; | |
2445 | brelse(bh); | |
2446 | lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb); | |
2447 | bh = ext4_bread(NULL, inode, lblock, 0, &err); | |
2448 | if (!bh) { | |
2449 | if (err) | |
2450 | EXT4_ERROR_INODE(inode, | |
2451 | "error %d reading directory " | |
2452 | "lblock %u", err, lblock); | |
2453 | else | |
2454 | ext4_warning(inode->i_sb, | |
2455 | "bad directory (dir #%lu) - no data block", | |
2456 | inode->i_ino); | |
2457 | ||
2458 | offset += sb->s_blocksize; | |
2459 | continue; | |
2460 | } | |
2461 | if (!buffer_verified(bh) && | |
2462 | !ext4_dirent_csum_verify(inode, | |
2463 | (struct ext4_dir_entry *)bh->b_data)) { | |
2464 | EXT4_ERROR_INODE(inode, "checksum error " | |
2465 | "reading directory lblock 0"); | |
2466 | return -EIO; | |
2467 | } | |
2468 | set_buffer_verified(bh); | |
2469 | de = (struct ext4_dir_entry_2 *) bh->b_data; | |
2470 | } | |
2471 | if (ext4_check_dir_entry(inode, NULL, de, bh, | |
2472 | bh->b_data, bh->b_size, offset)) { | |
2473 | de = (struct ext4_dir_entry_2 *)(bh->b_data + | |
2474 | sb->s_blocksize); | |
2475 | offset = (offset | (sb->s_blocksize - 1)) + 1; | |
2476 | continue; | |
2477 | } | |
2478 | if (le32_to_cpu(de->inode)) { | |
2479 | brelse(bh); | |
2480 | return 0; | |
2481 | } | |
2482 | offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize); | |
2483 | de = ext4_next_entry(de, sb->s_blocksize); | |
2484 | } | |
2485 | brelse(bh); | |
2486 | return 1; | |
2487 | } | |
2488 | ||
2489 | /* ext4_orphan_add() links an unlinked or truncated inode into a list of | |
2490 | * such inodes, starting at the superblock, in case we crash before the | |
2491 | * file is closed/deleted, or in case the inode truncate spans multiple | |
2492 | * transactions and the last transaction is not recovered after a crash. | |
2493 | * | |
2494 | * At filesystem recovery time, we walk this list deleting unlinked | |
2495 | * inodes and truncating linked inodes in ext4_orphan_cleanup(). | |
2496 | */ | |
2497 | int ext4_orphan_add(handle_t *handle, struct inode *inode) | |
2498 | { | |
2499 | struct super_block *sb = inode->i_sb; | |
2500 | struct ext4_iloc iloc; | |
2501 | int err = 0, rc; | |
2502 | ||
2503 | if (!EXT4_SB(sb)->s_journal) | |
2504 | return 0; | |
2505 | ||
2506 | mutex_lock(&EXT4_SB(sb)->s_orphan_lock); | |
2507 | if (!list_empty(&EXT4_I(inode)->i_orphan)) | |
2508 | goto out_unlock; | |
2509 | ||
2510 | /* | |
2511 | * Orphan handling is only valid for files with data blocks | |
2512 | * being truncated, or files being unlinked. Note that we either | |
2513 | * hold i_mutex, or the inode can not be referenced from outside, | |
2514 | * so i_nlink should not be bumped due to race | |
2515 | */ | |
2516 | J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
2517 | S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); | |
2518 | ||
2519 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); | |
2520 | err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); | |
2521 | if (err) | |
2522 | goto out_unlock; | |
2523 | ||
2524 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
2525 | if (err) | |
2526 | goto out_unlock; | |
2527 | /* | |
2528 | * Due to previous errors inode may be already a part of on-disk | |
2529 | * orphan list. If so skip on-disk list modification. | |
2530 | */ | |
2531 | if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <= | |
2532 | (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) | |
2533 | goto mem_insert; | |
2534 | ||
2535 | /* Insert this inode at the head of the on-disk orphan list... */ | |
2536 | NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan); | |
2537 | EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); | |
2538 | err = ext4_handle_dirty_super(handle, sb); | |
2539 | rc = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
2540 | if (!err) | |
2541 | err = rc; | |
2542 | ||
2543 | /* Only add to the head of the in-memory list if all the | |
2544 | * previous operations succeeded. If the orphan_add is going to | |
2545 | * fail (possibly taking the journal offline), we can't risk | |
2546 | * leaving the inode on the orphan list: stray orphan-list | |
2547 | * entries can cause panics at unmount time. | |
2548 | * | |
2549 | * This is safe: on error we're going to ignore the orphan list | |
2550 | * anyway on the next recovery. */ | |
2551 | mem_insert: | |
2552 | if (!err) | |
2553 | list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); | |
2554 | ||
2555 | jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); | |
2556 | jbd_debug(4, "orphan inode %lu will point to %d\n", | |
2557 | inode->i_ino, NEXT_ORPHAN(inode)); | |
2558 | out_unlock: | |
2559 | mutex_unlock(&EXT4_SB(sb)->s_orphan_lock); | |
2560 | ext4_std_error(inode->i_sb, err); | |
2561 | return err; | |
2562 | } | |
2563 | ||
2564 | /* | |
2565 | * ext4_orphan_del() removes an unlinked or truncated inode from the list | |
2566 | * of such inodes stored on disk, because it is finally being cleaned up. | |
2567 | */ | |
2568 | int ext4_orphan_del(handle_t *handle, struct inode *inode) | |
2569 | { | |
2570 | struct list_head *prev; | |
2571 | struct ext4_inode_info *ei = EXT4_I(inode); | |
2572 | struct ext4_sb_info *sbi; | |
2573 | __u32 ino_next; | |
2574 | struct ext4_iloc iloc; | |
2575 | int err = 0; | |
2576 | ||
2577 | if (!EXT4_SB(inode->i_sb)->s_journal) | |
2578 | return 0; | |
2579 | ||
2580 | mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock); | |
2581 | if (list_empty(&ei->i_orphan)) | |
2582 | goto out; | |
2583 | ||
2584 | ino_next = NEXT_ORPHAN(inode); | |
2585 | prev = ei->i_orphan.prev; | |
2586 | sbi = EXT4_SB(inode->i_sb); | |
2587 | ||
2588 | jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); | |
2589 | ||
2590 | list_del_init(&ei->i_orphan); | |
2591 | ||
2592 | /* If we're on an error path, we may not have a valid | |
2593 | * transaction handle with which to update the orphan list on | |
2594 | * disk, but we still need to remove the inode from the linked | |
2595 | * list in memory. */ | |
2596 | if (!handle) | |
2597 | goto out; | |
2598 | ||
2599 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
2600 | if (err) | |
2601 | goto out_err; | |
2602 | ||
2603 | if (prev == &sbi->s_orphan) { | |
2604 | jbd_debug(4, "superblock will point to %u\n", ino_next); | |
2605 | BUFFER_TRACE(sbi->s_sbh, "get_write_access"); | |
2606 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); | |
2607 | if (err) | |
2608 | goto out_brelse; | |
2609 | sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); | |
2610 | err = ext4_handle_dirty_super(handle, inode->i_sb); | |
2611 | } else { | |
2612 | struct ext4_iloc iloc2; | |
2613 | struct inode *i_prev = | |
2614 | &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; | |
2615 | ||
2616 | jbd_debug(4, "orphan inode %lu will point to %u\n", | |
2617 | i_prev->i_ino, ino_next); | |
2618 | err = ext4_reserve_inode_write(handle, i_prev, &iloc2); | |
2619 | if (err) | |
2620 | goto out_brelse; | |
2621 | NEXT_ORPHAN(i_prev) = ino_next; | |
2622 | err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); | |
2623 | } | |
2624 | if (err) | |
2625 | goto out_brelse; | |
2626 | NEXT_ORPHAN(inode) = 0; | |
2627 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
2628 | ||
2629 | out_err: | |
2630 | ext4_std_error(inode->i_sb, err); | |
2631 | out: | |
2632 | mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock); | |
2633 | return err; | |
2634 | ||
2635 | out_brelse: | |
2636 | brelse(iloc.bh); | |
2637 | goto out_err; | |
2638 | } | |
2639 | ||
2640 | static int ext4_rmdir(struct inode *dir, struct dentry *dentry) | |
2641 | { | |
2642 | int retval; | |
2643 | struct inode *inode; | |
2644 | struct buffer_head *bh; | |
2645 | struct ext4_dir_entry_2 *de; | |
2646 | handle_t *handle; | |
2647 | ||
2648 | /* Initialize quotas before so that eventual writes go in | |
2649 | * separate transaction */ | |
2650 | dquot_initialize(dir); | |
2651 | dquot_initialize(dentry->d_inode); | |
2652 | ||
2653 | handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb)); | |
2654 | if (IS_ERR(handle)) | |
2655 | return PTR_ERR(handle); | |
2656 | ||
2657 | retval = -ENOENT; | |
2658 | bh = ext4_find_entry(dir, &dentry->d_name, &de); | |
2659 | if (!bh) | |
2660 | goto end_rmdir; | |
2661 | ||
2662 | if (IS_DIRSYNC(dir)) | |
2663 | ext4_handle_sync(handle); | |
2664 | ||
2665 | inode = dentry->d_inode; | |
2666 | ||
2667 | retval = -EIO; | |
2668 | if (le32_to_cpu(de->inode) != inode->i_ino) | |
2669 | goto end_rmdir; | |
2670 | ||
2671 | retval = -ENOTEMPTY; | |
2672 | if (!empty_dir(inode)) | |
2673 | goto end_rmdir; | |
2674 | ||
2675 | retval = ext4_delete_entry(handle, dir, de, bh); | |
2676 | if (retval) | |
2677 | goto end_rmdir; | |
2678 | if (!EXT4_DIR_LINK_EMPTY(inode)) | |
2679 | ext4_warning(inode->i_sb, | |
2680 | "empty directory has too many links (%d)", | |
2681 | inode->i_nlink); | |
2682 | inode->i_version++; | |
2683 | clear_nlink(inode); | |
2684 | /* There's no need to set i_disksize: the fact that i_nlink is | |
2685 | * zero will ensure that the right thing happens during any | |
2686 | * recovery. */ | |
2687 | inode->i_size = 0; | |
2688 | ext4_orphan_add(handle, inode); | |
2689 | inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode); | |
2690 | ext4_mark_inode_dirty(handle, inode); | |
2691 | ext4_dec_count(handle, dir); | |
2692 | ext4_update_dx_flag(dir); | |
2693 | ext4_mark_inode_dirty(handle, dir); | |
2694 | ||
2695 | end_rmdir: | |
2696 | ext4_journal_stop(handle); | |
2697 | brelse(bh); | |
2698 | return retval; | |
2699 | } | |
2700 | ||
2701 | static int ext4_unlink(struct inode *dir, struct dentry *dentry) | |
2702 | { | |
2703 | int retval; | |
2704 | struct inode *inode; | |
2705 | struct buffer_head *bh; | |
2706 | struct ext4_dir_entry_2 *de; | |
2707 | handle_t *handle; | |
2708 | ||
2709 | trace_ext4_unlink_enter(dir, dentry); | |
2710 | /* Initialize quotas before so that eventual writes go | |
2711 | * in separate transaction */ | |
2712 | dquot_initialize(dir); | |
2713 | dquot_initialize(dentry->d_inode); | |
2714 | ||
2715 | handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb)); | |
2716 | if (IS_ERR(handle)) | |
2717 | return PTR_ERR(handle); | |
2718 | ||
2719 | if (IS_DIRSYNC(dir)) | |
2720 | ext4_handle_sync(handle); | |
2721 | ||
2722 | retval = -ENOENT; | |
2723 | bh = ext4_find_entry(dir, &dentry->d_name, &de); | |
2724 | if (!bh) | |
2725 | goto end_unlink; | |
2726 | ||
2727 | inode = dentry->d_inode; | |
2728 | ||
2729 | retval = -EIO; | |
2730 | if (le32_to_cpu(de->inode) != inode->i_ino) | |
2731 | goto end_unlink; | |
2732 | ||
2733 | if (!inode->i_nlink) { | |
2734 | ext4_warning(inode->i_sb, | |
2735 | "Deleting nonexistent file (%lu), %d", | |
2736 | inode->i_ino, inode->i_nlink); | |
2737 | set_nlink(inode, 1); | |
2738 | } | |
2739 | retval = ext4_delete_entry(handle, dir, de, bh); | |
2740 | if (retval) | |
2741 | goto end_unlink; | |
2742 | dir->i_ctime = dir->i_mtime = ext4_current_time(dir); | |
2743 | ext4_update_dx_flag(dir); | |
2744 | ext4_mark_inode_dirty(handle, dir); | |
2745 | drop_nlink(inode); | |
2746 | if (!inode->i_nlink) | |
2747 | ext4_orphan_add(handle, inode); | |
2748 | inode->i_ctime = ext4_current_time(inode); | |
2749 | ext4_mark_inode_dirty(handle, inode); | |
2750 | retval = 0; | |
2751 | ||
2752 | end_unlink: | |
2753 | ext4_journal_stop(handle); | |
2754 | brelse(bh); | |
2755 | trace_ext4_unlink_exit(dentry, retval); | |
2756 | return retval; | |
2757 | } | |
2758 | ||
2759 | static int ext4_symlink(struct inode *dir, | |
2760 | struct dentry *dentry, const char *symname) | |
2761 | { | |
2762 | handle_t *handle; | |
2763 | struct inode *inode; | |
2764 | int l, err, retries = 0; | |
2765 | int credits; | |
2766 | ||
2767 | l = strlen(symname)+1; | |
2768 | if (l > dir->i_sb->s_blocksize) | |
2769 | return -ENAMETOOLONG; | |
2770 | ||
2771 | dquot_initialize(dir); | |
2772 | ||
2773 | if (l > EXT4_N_BLOCKS * 4) { | |
2774 | /* | |
2775 | * For non-fast symlinks, we just allocate inode and put it on | |
2776 | * orphan list in the first transaction => we need bitmap, | |
2777 | * group descriptor, sb, inode block, quota blocks, and | |
2778 | * possibly selinux xattr blocks. | |
2779 | */ | |
2780 | credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + | |
2781 | EXT4_XATTR_TRANS_BLOCKS; | |
2782 | } else { | |
2783 | /* | |
2784 | * Fast symlink. We have to add entry to directory | |
2785 | * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS), | |
2786 | * allocate new inode (bitmap, group descriptor, inode block, | |
2787 | * quota blocks, sb is already counted in previous macros). | |
2788 | */ | |
2789 | credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + | |
2790 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + | |
2791 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb); | |
2792 | } | |
2793 | retry: | |
2794 | handle = ext4_journal_start(dir, credits); | |
2795 | if (IS_ERR(handle)) | |
2796 | return PTR_ERR(handle); | |
2797 | ||
2798 | if (IS_DIRSYNC(dir)) | |
2799 | ext4_handle_sync(handle); | |
2800 | ||
2801 | inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO, | |
2802 | &dentry->d_name, 0, NULL); | |
2803 | err = PTR_ERR(inode); | |
2804 | if (IS_ERR(inode)) | |
2805 | goto out_stop; | |
2806 | ||
2807 | if (l > EXT4_N_BLOCKS * 4) { | |
2808 | inode->i_op = &ext4_symlink_inode_operations; | |
2809 | ext4_set_aops(inode); | |
2810 | /* | |
2811 | * We cannot call page_symlink() with transaction started | |
2812 | * because it calls into ext4_write_begin() which can wait | |
2813 | * for transaction commit if we are running out of space | |
2814 | * and thus we deadlock. So we have to stop transaction now | |
2815 | * and restart it when symlink contents is written. | |
2816 | * | |
2817 | * To keep fs consistent in case of crash, we have to put inode | |
2818 | * to orphan list in the mean time. | |
2819 | */ | |
2820 | drop_nlink(inode); | |
2821 | err = ext4_orphan_add(handle, inode); | |
2822 | ext4_journal_stop(handle); | |
2823 | if (err) | |
2824 | goto err_drop_inode; | |
2825 | err = __page_symlink(inode, symname, l, 1); | |
2826 | if (err) | |
2827 | goto err_drop_inode; | |
2828 | /* | |
2829 | * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS | |
2830 | * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified | |
2831 | */ | |
2832 | handle = ext4_journal_start(dir, | |
2833 | EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + | |
2834 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1); | |
2835 | if (IS_ERR(handle)) { | |
2836 | err = PTR_ERR(handle); | |
2837 | goto err_drop_inode; | |
2838 | } | |
2839 | set_nlink(inode, 1); | |
2840 | err = ext4_orphan_del(handle, inode); | |
2841 | if (err) { | |
2842 | ext4_journal_stop(handle); | |
2843 | clear_nlink(inode); | |
2844 | goto err_drop_inode; | |
2845 | } | |
2846 | } else { | |
2847 | /* clear the extent format for fast symlink */ | |
2848 | ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); | |
2849 | inode->i_op = &ext4_fast_symlink_inode_operations; | |
2850 | memcpy((char *)&EXT4_I(inode)->i_data, symname, l); | |
2851 | inode->i_size = l-1; | |
2852 | } | |
2853 | EXT4_I(inode)->i_disksize = inode->i_size; | |
2854 | err = ext4_add_nondir(handle, dentry, inode); | |
2855 | out_stop: | |
2856 | ext4_journal_stop(handle); | |
2857 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) | |
2858 | goto retry; | |
2859 | return err; | |
2860 | err_drop_inode: | |
2861 | unlock_new_inode(inode); | |
2862 | iput(inode); | |
2863 | return err; | |
2864 | } | |
2865 | ||
2866 | static int ext4_link(struct dentry *old_dentry, | |
2867 | struct inode *dir, struct dentry *dentry) | |
2868 | { | |
2869 | handle_t *handle; | |
2870 | struct inode *inode = old_dentry->d_inode; | |
2871 | int err, retries = 0; | |
2872 | ||
2873 | if (inode->i_nlink >= EXT4_LINK_MAX) | |
2874 | return -EMLINK; | |
2875 | ||
2876 | dquot_initialize(dir); | |
2877 | ||
2878 | retry: | |
2879 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + | |
2880 | EXT4_INDEX_EXTRA_TRANS_BLOCKS); | |
2881 | if (IS_ERR(handle)) | |
2882 | return PTR_ERR(handle); | |
2883 | ||
2884 | if (IS_DIRSYNC(dir)) | |
2885 | ext4_handle_sync(handle); | |
2886 | ||
2887 | inode->i_ctime = ext4_current_time(inode); | |
2888 | ext4_inc_count(handle, inode); | |
2889 | ihold(inode); | |
2890 | ||
2891 | err = ext4_add_entry(handle, dentry, inode); | |
2892 | if (!err) { | |
2893 | ext4_mark_inode_dirty(handle, inode); | |
2894 | d_instantiate(dentry, inode); | |
2895 | } else { | |
2896 | drop_nlink(inode); | |
2897 | iput(inode); | |
2898 | } | |
2899 | ext4_journal_stop(handle); | |
2900 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) | |
2901 | goto retry; | |
2902 | return err; | |
2903 | } | |
2904 | ||
2905 | #define PARENT_INO(buffer, size) \ | |
2906 | (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode) | |
2907 | ||
2908 | /* | |
2909 | * Anybody can rename anything with this: the permission checks are left to the | |
2910 | * higher-level routines. | |
2911 | */ | |
2912 | static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry, | |
2913 | struct inode *new_dir, struct dentry *new_dentry) | |
2914 | { | |
2915 | handle_t *handle; | |
2916 | struct inode *old_inode, *new_inode; | |
2917 | struct buffer_head *old_bh, *new_bh, *dir_bh; | |
2918 | struct ext4_dir_entry_2 *old_de, *new_de; | |
2919 | int retval, force_da_alloc = 0; | |
2920 | ||
2921 | dquot_initialize(old_dir); | |
2922 | dquot_initialize(new_dir); | |
2923 | ||
2924 | old_bh = new_bh = dir_bh = NULL; | |
2925 | ||
2926 | /* Initialize quotas before so that eventual writes go | |
2927 | * in separate transaction */ | |
2928 | if (new_dentry->d_inode) | |
2929 | dquot_initialize(new_dentry->d_inode); | |
2930 | handle = ext4_journal_start(old_dir, 2 * | |
2931 | EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) + | |
2932 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2); | |
2933 | if (IS_ERR(handle)) | |
2934 | return PTR_ERR(handle); | |
2935 | ||
2936 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) | |
2937 | ext4_handle_sync(handle); | |
2938 | ||
2939 | old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de); | |
2940 | /* | |
2941 | * Check for inode number is _not_ due to possible IO errors. | |
2942 | * We might rmdir the source, keep it as pwd of some process | |
2943 | * and merrily kill the link to whatever was created under the | |
2944 | * same name. Goodbye sticky bit ;-< | |
2945 | */ | |
2946 | old_inode = old_dentry->d_inode; | |
2947 | retval = -ENOENT; | |
2948 | if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino) | |
2949 | goto end_rename; | |
2950 | ||
2951 | new_inode = new_dentry->d_inode; | |
2952 | new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de); | |
2953 | if (new_bh) { | |
2954 | if (!new_inode) { | |
2955 | brelse(new_bh); | |
2956 | new_bh = NULL; | |
2957 | } | |
2958 | } | |
2959 | if (S_ISDIR(old_inode->i_mode)) { | |
2960 | if (new_inode) { | |
2961 | retval = -ENOTEMPTY; | |
2962 | if (!empty_dir(new_inode)) | |
2963 | goto end_rename; | |
2964 | } | |
2965 | retval = -EIO; | |
2966 | if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) { | |
2967 | if (!retval) { | |
2968 | retval = -EIO; | |
2969 | ext4_error(old_inode->i_sb, | |
2970 | "Directory hole detected on inode %lu\n", | |
2971 | old_inode->i_ino); | |
2972 | } | |
2973 | goto end_rename; | |
2974 | } | |
2975 | if (!buffer_verified(dir_bh) && | |
2976 | !ext4_dirent_csum_verify(old_inode, | |
2977 | (struct ext4_dir_entry *)dir_bh->b_data)) | |
2978 | goto end_rename; | |
2979 | set_buffer_verified(dir_bh); | |
2980 | if (le32_to_cpu(PARENT_INO(dir_bh->b_data, | |
2981 | old_dir->i_sb->s_blocksize)) != old_dir->i_ino) | |
2982 | goto end_rename; | |
2983 | retval = -EMLINK; | |
2984 | if (!new_inode && new_dir != old_dir && | |
2985 | EXT4_DIR_LINK_MAX(new_dir)) | |
2986 | goto end_rename; | |
2987 | BUFFER_TRACE(dir_bh, "get_write_access"); | |
2988 | retval = ext4_journal_get_write_access(handle, dir_bh); | |
2989 | if (retval) | |
2990 | goto end_rename; | |
2991 | } | |
2992 | if (!new_bh) { | |
2993 | retval = ext4_add_entry(handle, new_dentry, old_inode); | |
2994 | if (retval) | |
2995 | goto end_rename; | |
2996 | } else { | |
2997 | BUFFER_TRACE(new_bh, "get write access"); | |
2998 | retval = ext4_journal_get_write_access(handle, new_bh); | |
2999 | if (retval) | |
3000 | goto end_rename; | |
3001 | new_de->inode = cpu_to_le32(old_inode->i_ino); | |
3002 | if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb, | |
3003 | EXT4_FEATURE_INCOMPAT_FILETYPE)) | |
3004 | new_de->file_type = old_de->file_type; | |
3005 | new_dir->i_version++; | |
3006 | new_dir->i_ctime = new_dir->i_mtime = | |
3007 | ext4_current_time(new_dir); | |
3008 | ext4_mark_inode_dirty(handle, new_dir); | |
3009 | BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata"); | |
3010 | retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh); | |
3011 | if (unlikely(retval)) { | |
3012 | ext4_std_error(new_dir->i_sb, retval); | |
3013 | goto end_rename; | |
3014 | } | |
3015 | brelse(new_bh); | |
3016 | new_bh = NULL; | |
3017 | } | |
3018 | ||
3019 | /* | |
3020 | * Like most other Unix systems, set the ctime for inodes on a | |
3021 | * rename. | |
3022 | */ | |
3023 | old_inode->i_ctime = ext4_current_time(old_inode); | |
3024 | ext4_mark_inode_dirty(handle, old_inode); | |
3025 | ||
3026 | /* | |
3027 | * ok, that's it | |
3028 | */ | |
3029 | if (le32_to_cpu(old_de->inode) != old_inode->i_ino || | |
3030 | old_de->name_len != old_dentry->d_name.len || | |
3031 | strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) || | |
3032 | (retval = ext4_delete_entry(handle, old_dir, | |
3033 | old_de, old_bh)) == -ENOENT) { | |
3034 | /* old_de could have moved from under us during htree split, so | |
3035 | * make sure that we are deleting the right entry. We might | |
3036 | * also be pointing to a stale entry in the unused part of | |
3037 | * old_bh so just checking inum and the name isn't enough. */ | |
3038 | struct buffer_head *old_bh2; | |
3039 | struct ext4_dir_entry_2 *old_de2; | |
3040 | ||
3041 | old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2); | |
3042 | if (old_bh2) { | |
3043 | retval = ext4_delete_entry(handle, old_dir, | |
3044 | old_de2, old_bh2); | |
3045 | brelse(old_bh2); | |
3046 | } | |
3047 | } | |
3048 | if (retval) { | |
3049 | ext4_warning(old_dir->i_sb, | |
3050 | "Deleting old file (%lu), %d, error=%d", | |
3051 | old_dir->i_ino, old_dir->i_nlink, retval); | |
3052 | } | |
3053 | ||
3054 | if (new_inode) { | |
3055 | ext4_dec_count(handle, new_inode); | |
3056 | new_inode->i_ctime = ext4_current_time(new_inode); | |
3057 | } | |
3058 | old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir); | |
3059 | ext4_update_dx_flag(old_dir); | |
3060 | if (dir_bh) { | |
3061 | PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) = | |
3062 | cpu_to_le32(new_dir->i_ino); | |
3063 | BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata"); | |
3064 | if (is_dx(old_inode)) { | |
3065 | retval = ext4_handle_dirty_dx_node(handle, | |
3066 | old_inode, | |
3067 | dir_bh); | |
3068 | } else { | |
3069 | retval = ext4_handle_dirty_dirent_node(handle, | |
3070 | old_inode, | |
3071 | dir_bh); | |
3072 | } | |
3073 | if (retval) { | |
3074 | ext4_std_error(old_dir->i_sb, retval); | |
3075 | goto end_rename; | |
3076 | } | |
3077 | ext4_dec_count(handle, old_dir); | |
3078 | if (new_inode) { | |
3079 | /* checked empty_dir above, can't have another parent, | |
3080 | * ext4_dec_count() won't work for many-linked dirs */ | |
3081 | clear_nlink(new_inode); | |
3082 | } else { | |
3083 | ext4_inc_count(handle, new_dir); | |
3084 | ext4_update_dx_flag(new_dir); | |
3085 | ext4_mark_inode_dirty(handle, new_dir); | |
3086 | } | |
3087 | } | |
3088 | ext4_mark_inode_dirty(handle, old_dir); | |
3089 | if (new_inode) { | |
3090 | ext4_mark_inode_dirty(handle, new_inode); | |
3091 | if (!new_inode->i_nlink) | |
3092 | ext4_orphan_add(handle, new_inode); | |
3093 | if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC)) | |
3094 | force_da_alloc = 1; | |
3095 | } | |
3096 | retval = 0; | |
3097 | ||
3098 | end_rename: | |
3099 | brelse(dir_bh); | |
3100 | brelse(old_bh); | |
3101 | brelse(new_bh); | |
3102 | ext4_journal_stop(handle); | |
3103 | if (retval == 0 && force_da_alloc) | |
3104 | ext4_alloc_da_blocks(old_inode); | |
3105 | return retval; | |
3106 | } | |
3107 | ||
3108 | /* | |
3109 | * directories can handle most operations... | |
3110 | */ | |
3111 | const struct inode_operations ext4_dir_inode_operations = { | |
3112 | .create = ext4_create, | |
3113 | .lookup = ext4_lookup, | |
3114 | .link = ext4_link, | |
3115 | .unlink = ext4_unlink, | |
3116 | .symlink = ext4_symlink, | |
3117 | .mkdir = ext4_mkdir, | |
3118 | .rmdir = ext4_rmdir, | |
3119 | .mknod = ext4_mknod, | |
3120 | .rename = ext4_rename, | |
3121 | .setattr = ext4_setattr, | |
3122 | #ifdef CONFIG_EXT4_FS_XATTR | |
3123 | .setxattr = generic_setxattr, | |
3124 | .getxattr = generic_getxattr, | |
3125 | .listxattr = ext4_listxattr, | |
3126 | .removexattr = generic_removexattr, | |
3127 | #endif | |
3128 | .get_acl = ext4_get_acl, | |
3129 | .fiemap = ext4_fiemap, | |
3130 | }; | |
3131 | ||
3132 | const struct inode_operations ext4_special_inode_operations = { | |
3133 | .setattr = ext4_setattr, | |
3134 | #ifdef CONFIG_EXT4_FS_XATTR | |
3135 | .setxattr = generic_setxattr, | |
3136 | .getxattr = generic_getxattr, | |
3137 | .listxattr = ext4_listxattr, | |
3138 | .removexattr = generic_removexattr, | |
3139 | #endif | |
3140 | .get_acl = ext4_get_acl, | |
3141 | }; |