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