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