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