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