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