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