blob: b6c7f0dfc1e8fefda237176d69277a7d69f57a3c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#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>
Jeff Laytonae5e1652018-01-29 06:41:30 -050037#include <linux/iversion.h>
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -040038#include <linux/unicode.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040039#include "ext4.h"
40#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include "xattr.h"
43#include "acl.h"
44
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040045#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046/*
47 * define how far ahead to read directories while searching them.
48 */
49#define NAMEI_RA_CHUNKS 2
50#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040051#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052
Mingming Cao617ba132006-10-11 01:20:53 -070053static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050055 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056{
57 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040058 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059
Theodore Ts'odf981d02012-08-17 09:48:17 -040060 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
61 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050062 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
63 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040064
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
66
Theodore Ts'oc5e298a2015-06-21 01:25:29 -040067 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
Theodore Ts'o1c215022014-08-29 20:52:15 -040068 if (IS_ERR(bh))
69 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050070 inode->i_size += inode->i_sb->s_blocksize;
71 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040072 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050073 err = ext4_journal_get_write_access(handle, bh);
74 if (err) {
75 brelse(bh);
76 ext4_std_error(inode->i_sb, err);
77 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040078 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070079 return bh;
80}
81
Theodore Ts'odc6982f2013-02-14 23:59:26 -050082static int ext4_dx_csum_verify(struct inode *inode,
83 struct ext4_dir_entry *dirent);
84
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -040085/*
86 * Hints to ext4_read_dirblock regarding whether we expect a directory
87 * block being read to be an index block, or a block containing
88 * directory entries (and if the latter, whether it was found via a
89 * logical block in an htree index block). This is used to control
90 * what sort of sanity checkinig ext4_read_dirblock() will do on the
91 * directory block read from the storage device. EITHER will means
92 * the caller doesn't know what kind of directory block will be read,
93 * so no specific verification will be done.
94 */
Theodore Ts'odc6982f2013-02-14 23:59:26 -050095typedef enum {
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -040096 EITHER, INDEX, DIRENT, DIRENT_HTREE
Theodore Ts'odc6982f2013-02-14 23:59:26 -050097} dirblock_type_t;
98
99#define ext4_read_dirblock(inode, block, type) \
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400100 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500101
102static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400103 ext4_lblk_t block,
104 dirblock_type_t type,
105 const char *func,
106 unsigned int line)
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500107{
108 struct buffer_head *bh;
109 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400110 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500111
Theodore Ts'o46f870d62019-11-21 13:09:43 -0500112 if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
113 bh = ERR_PTR(-EIO);
114 else
115 bh = ext4_bread(NULL, inode, block, 0);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400116 if (IS_ERR(bh)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400117 __ext4_warning(inode->i_sb, func, line,
118 "inode #%lu: lblock %lu: comm %s: "
119 "error %ld reading directory block",
120 inode->i_ino, (unsigned long)block,
121 current->comm, PTR_ERR(bh));
Theodore Ts'o1c215022014-08-29 20:52:15 -0400122
123 return bh;
124 }
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -0400125 if (!bh && (type == INDEX || type == DIRENT_HTREE)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400126 ext4_error_inode(inode, func, line, block,
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -0400127 "Directory hole found for htree %s block",
128 (type == INDEX) ? "index" : "leaf");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400129 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500130 }
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -0400131 if (!bh)
132 return NULL;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500133 dirent = (struct ext4_dir_entry *) bh->b_data;
134 /* Determine whether or not we have an index block */
135 if (is_dx(inode)) {
136 if (block == 0)
137 is_dx_block = 1;
138 else if (ext4_rec_len_from_disk(dirent->rec_len,
139 inode->i_sb->s_blocksize) ==
140 inode->i_sb->s_blocksize)
141 is_dx_block = 1;
142 }
143 if (!is_dx_block && type == INDEX) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400144 ext4_error_inode(inode, func, line, block,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500145 "directory leaf block found instead of index block");
Vasily Averinde59fae2018-11-07 22:36:23 -0500146 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400147 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500148 }
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400149 if (!ext4_has_metadata_csum(inode->i_sb) ||
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500150 buffer_verified(bh))
151 return bh;
152
153 /*
154 * An empty leaf block can get mistaken for a index block; for
155 * this reason, we can only check the index checksum when the
156 * caller is sure it should be an index block.
157 */
158 if (is_dx_block && type == INDEX) {
Theodore Ts'o46f870d62019-11-21 13:09:43 -0500159 if (ext4_dx_csum_verify(inode, dirent) &&
160 !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500161 set_buffer_verified(bh);
162 else {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400163 ext4_error_inode_err(inode, func, line, block,
164 EFSBADCRC,
165 "Directory index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400167 return ERR_PTR(-EFSBADCRC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 }
169 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500170 if (!is_dx_block) {
Theodore Ts'o46f870d62019-11-21 13:09:43 -0500171 if (ext4_dirblock_csum_verify(inode, bh) &&
172 !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500173 set_buffer_verified(bh);
174 else {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400175 ext4_error_inode_err(inode, func, line, block,
176 EFSBADCRC,
177 "Directory block failed checksum");
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500178 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400179 return ERR_PTR(-EFSBADCRC);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500180 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 }
182 return bh;
183}
184
185#ifndef assert
186#define assert(test) J_ASSERT(test)
187#endif
188
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189#ifdef DX_DEBUG
190#define dxtrace(command) command
191#else
192#define dxtrace(command)
193#endif
194
195struct fake_dirent
196{
197 __le32 inode;
198 __le16 rec_len;
199 u8 name_len;
200 u8 file_type;
201};
202
203struct dx_countlimit
204{
205 __le16 limit;
206 __le16 count;
207};
208
209struct dx_entry
210{
211 __le32 hash;
212 __le32 block;
213};
214
215/*
216 * dx_root_info is laid out so that if it should somehow get overlaid by a
217 * dirent the two low bits of the hash version will be zero. Therefore, the
218 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
219 */
220
221struct dx_root
222{
223 struct fake_dirent dot;
224 char dot_name[4];
225 struct fake_dirent dotdot;
226 char dotdot_name[4];
227 struct dx_root_info
228 {
229 __le32 reserved_zero;
230 u8 hash_version;
231 u8 info_length; /* 8 */
232 u8 indirect_levels;
233 u8 unused_flags;
234 }
235 info;
Gustavo A. R. Silva6cfb0612020-02-13 10:06:48 -0600236 struct dx_entry entries[];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237};
238
239struct dx_node
240{
241 struct fake_dirent fake;
Gustavo A. R. Silva6cfb0612020-02-13 10:06:48 -0600242 struct dx_entry entries[];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243};
244
245
246struct dx_frame
247{
248 struct buffer_head *bh;
249 struct dx_entry *entries;
250 struct dx_entry *at;
251};
252
253struct dx_map_entry
254{
255 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700256 u16 offs;
257 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258};
259
Darrick J. Wonge6153912012-04-29 18:23:10 -0400260/*
261 * This goes at the end of each htree block.
262 */
263struct dx_tail {
264 u32 dt_reserved;
265 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
266};
267
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500268static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
269static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400270static inline unsigned dx_get_hash(struct dx_entry *entry);
271static void dx_set_hash(struct dx_entry *entry, unsigned value);
272static unsigned dx_get_count(struct dx_entry *entries);
273static unsigned dx_get_limit(struct dx_entry *entries);
274static void dx_set_count(struct dx_entry *entries, unsigned value);
275static void dx_set_limit(struct dx_entry *entries, unsigned value);
276static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
277static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400278static struct dx_frame *dx_probe(struct ext4_filename *fname,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 struct inode *dir,
280 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400281 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400282static void dx_release(struct dx_frame *frames);
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400283static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
284 unsigned blocksize, struct dx_hash_info *hinfo,
285 struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700287static struct ext4_dir_entry_2 *dx_move_dirents(struct inode *dir, char *from,
288 char *to, struct dx_map_entry *offsets,
289 int count, unsigned int blocksize);
290static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
291 unsigned int blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500292static void dx_insert_block(struct dx_frame *frame,
293 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700294static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 struct dx_frame *frame,
296 struct dx_frame *frames,
297 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400298static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400299 struct ext4_filename *fname,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700300 struct ext4_dir_entry_2 **res_dir, ext4_lblk_t *lblk);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400301static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -0500302 struct inode *dir, struct inode *inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400304/* checksumming functions */
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400305void ext4_initialize_dirent_tail(struct buffer_head *bh,
306 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400307{
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400308 struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
309
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400310 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
311 t->det_rec_len = ext4_rec_len_to_disk(
312 sizeof(struct ext4_dir_entry_tail), blocksize);
313 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
314}
315
316/* Walk through a dirent block to find a checksum "dirent" at the tail */
317static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
Theodore Ts'of036adb2019-06-21 15:49:26 -0400318 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400319{
320 struct ext4_dir_entry_tail *t;
321
322#ifdef PARANOID
323 struct ext4_dir_entry *d, *top;
324
Theodore Ts'of036adb2019-06-21 15:49:26 -0400325 d = (struct ext4_dir_entry *)bh->b_data;
326 top = (struct ext4_dir_entry *)(bh->b_data +
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400327 (EXT4_BLOCK_SIZE(inode->i_sb) -
Theodore Ts'of036adb2019-06-21 15:49:26 -0400328 sizeof(struct ext4_dir_entry_tail)));
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400329 while (d < top && d->rec_len)
330 d = (struct ext4_dir_entry *)(((void *)d) +
331 le16_to_cpu(d->rec_len));
332
333 if (d != top)
334 return NULL;
335
336 t = (struct ext4_dir_entry_tail *)d;
337#else
Theodore Ts'of036adb2019-06-21 15:49:26 -0400338 t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400339#endif
340
341 if (t->det_reserved_zero1 ||
342 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
343 t->det_reserved_zero2 ||
344 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
345 return NULL;
346
347 return t;
348}
349
Theodore Ts'of036adb2019-06-21 15:49:26 -0400350static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400351{
352 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
353 struct ext4_inode_info *ei = EXT4_I(inode);
354 __u32 csum;
355
356 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
357 return cpu_to_le32(csum);
358}
359
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400360#define warn_no_space_for_csum(inode) \
361 __warn_no_space_for_csum((inode), __func__, __LINE__)
362
363static void __warn_no_space_for_csum(struct inode *inode, const char *func,
364 unsigned int line)
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500365{
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400366 __ext4_warning_inode(inode, func, line,
367 "No space for directory leaf checksum. Please run e2fsck -D.");
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500368}
369
Theodore Ts'of036adb2019-06-21 15:49:26 -0400370int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400371{
372 struct ext4_dir_entry_tail *t;
373
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400374 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400375 return 1;
376
Theodore Ts'of036adb2019-06-21 15:49:26 -0400377 t = get_dirent_tail(inode, bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400378 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500379 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400380 return 0;
381 }
382
Theodore Ts'of036adb2019-06-21 15:49:26 -0400383 if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400384 (char *)t - bh->b_data))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400385 return 0;
386
387 return 1;
388}
389
Theodore Ts'of036adb2019-06-21 15:49:26 -0400390static void ext4_dirblock_csum_set(struct inode *inode,
391 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400392{
393 struct ext4_dir_entry_tail *t;
394
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400395 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400396 return;
397
Theodore Ts'of036adb2019-06-21 15:49:26 -0400398 t = get_dirent_tail(inode, bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400399 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500400 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400401 return;
402 }
403
Theodore Ts'of036adb2019-06-21 15:49:26 -0400404 t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400405 (char *)t - bh->b_data);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400406}
407
Theodore Ts'of036adb2019-06-21 15:49:26 -0400408int ext4_handle_dirty_dirblock(handle_t *handle,
409 struct inode *inode,
410 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400411{
Theodore Ts'of036adb2019-06-21 15:49:26 -0400412 ext4_dirblock_csum_set(inode, bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400413 return ext4_handle_dirty_metadata(handle, inode, bh);
414}
415
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400416static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
417 struct ext4_dir_entry *dirent,
418 int *offset)
419{
420 struct ext4_dir_entry *dp;
421 struct dx_root_info *root;
422 int count_offset;
423
424 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
425 count_offset = 8;
426 else if (le16_to_cpu(dirent->rec_len) == 12) {
427 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
428 if (le16_to_cpu(dp->rec_len) !=
429 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
430 return NULL;
431 root = (struct dx_root_info *)(((void *)dp + 12));
432 if (root->reserved_zero ||
433 root->info_length != sizeof(struct dx_root_info))
434 return NULL;
435 count_offset = 32;
436 } else
437 return NULL;
438
439 if (offset)
440 *offset = count_offset;
441 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
442}
443
444static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
445 int count_offset, int count, struct dx_tail *t)
446{
447 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
448 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400449 __u32 csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400450 int size;
Daeho Jeongb47820e2016-07-03 17:51:39 -0400451 __u32 dummy_csum = 0;
452 int offset = offsetof(struct dx_tail, dt_checksum);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400453
454 size = count_offset + (count * sizeof(struct dx_entry));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400455 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400456 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
457 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400458
459 return cpu_to_le32(csum);
460}
461
462static int ext4_dx_csum_verify(struct inode *inode,
463 struct ext4_dir_entry *dirent)
464{
465 struct dx_countlimit *c;
466 struct dx_tail *t;
467 int count_offset, limit, count;
468
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400469 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400470 return 1;
471
472 c = get_dx_countlimit(inode, dirent, &count_offset);
473 if (!c) {
474 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
Daeho Jeongfa964542016-07-03 21:11:08 -0400475 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400476 }
477 limit = le16_to_cpu(c->limit);
478 count = le16_to_cpu(c->count);
479 if (count_offset + (limit * sizeof(struct dx_entry)) >
480 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500481 warn_no_space_for_csum(inode);
Daeho Jeongfa964542016-07-03 21:11:08 -0400482 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400483 }
484 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
485
486 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
487 count, t))
488 return 0;
489 return 1;
490}
491
492static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
493{
494 struct dx_countlimit *c;
495 struct dx_tail *t;
496 int count_offset, limit, count;
497
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400498 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400499 return;
500
501 c = get_dx_countlimit(inode, dirent, &count_offset);
502 if (!c) {
503 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
504 return;
505 }
506 limit = le16_to_cpu(c->limit);
507 count = le16_to_cpu(c->count);
508 if (count_offset + (limit * sizeof(struct dx_entry)) >
509 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500510 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400511 return;
512 }
513 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
514
515 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
516}
517
518static inline int ext4_handle_dirty_dx_node(handle_t *handle,
519 struct inode *inode,
520 struct buffer_head *bh)
521{
522 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
523 return ext4_handle_dirty_metadata(handle, inode, bh);
524}
525
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526/*
Li Zefanf795e142008-07-11 19:27:31 -0400527 * p is at least 6 bytes before the end of page
528 */
529static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500530ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400531{
532 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500533 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400534}
535
536/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 * Future: use high four bits of block for coalesce-on-delete flags
538 * Mask them off for now.
539 */
540
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500541static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400543 return le32_to_cpu(entry->block) & 0x0fffffff;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544}
545
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500546static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547{
548 entry->block = cpu_to_le32(value);
549}
550
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400551static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552{
553 return le32_to_cpu(entry->hash);
554}
555
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400556static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557{
558 entry->hash = cpu_to_le32(value);
559}
560
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400561static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562{
563 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
564}
565
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400566static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567{
568 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
569}
570
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400571static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572{
573 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
574}
575
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400576static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577{
578 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
579}
580
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400581static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582{
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700583 unsigned int entry_space = dir->i_sb->s_blocksize -
584 ext4_dir_rec_len(1, NULL) -
585 ext4_dir_rec_len(2, NULL) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400586
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400587 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400588 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400589 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590}
591
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400592static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593{
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700594 unsigned int entry_space = dir->i_sb->s_blocksize -
595 ext4_dir_rec_len(0, dir);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400596
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400597 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400598 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400599 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600}
601
602/*
603 * Debug
604 */
605#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400606static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607{
Andrew Morton63f57932006-10-11 01:21:24 -0700608 int i, n = dx_get_count (entries);
Joe Perchesd74f3d22016-10-15 09:57:31 -0400609 printk(KERN_DEBUG "%s index", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700610 for (i = 0; i < n; i++) {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400611 printk(KERN_CONT " %x->%lu",
612 i ? dx_get_hash(entries + i) : 0,
613 (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700614 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400615 printk(KERN_CONT "\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616}
617
618struct stats
619{
620 unsigned names;
621 unsigned space;
622 unsigned bcount;
623};
624
Michael Halcrowb3098482015-04-12 01:07:01 -0400625static struct stats dx_show_leaf(struct inode *dir,
626 struct dx_hash_info *hinfo,
627 struct ext4_dir_entry_2 *de,
628 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629{
630 unsigned names = 0, space = 0;
631 char *base = (char *) de;
632 struct dx_hash_info h = *hinfo;
633
634 printk("names: ");
635 while ((char *) de < base + size)
636 {
637 if (de->inode)
638 {
639 if (show_names)
640 {
Chandan Rajendra643fa962018-12-12 15:20:12 +0530641#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrowb3098482015-04-12 01:07:01 -0400642 int len;
643 char *name;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400644 struct fscrypt_str fname_crypto_str =
645 FSTR_INIT(NULL, 0);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400646 int res = 0;
Michael Halcrowb3098482015-04-12 01:07:01 -0400647
648 name = de->name;
649 len = de->name_len;
Eric Biggers70817922020-12-02 18:20:36 -0800650 if (!IS_ENCRYPTED(dir)) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400651 /* Directory is not encrypted */
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400652 ext4fs_dirhash(dir, de->name,
Michael Halcrowb3098482015-04-12 01:07:01 -0400653 de->name_len, &h);
654 printk("%*.s:(U)%x.%u ", len,
655 name, h.hash,
656 (unsigned) ((char *) de
657 - base));
658 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400659 struct fscrypt_str de_name =
660 FSTR_INIT(name, len);
661
Michael Halcrowb3098482015-04-12 01:07:01 -0400662 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400663 res = fscrypt_fname_alloc_buffer(
Jeff Layton8b10fe62020-08-10 10:21:39 -0400664 len, &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400665 if (res)
Michael Halcrowb3098482015-04-12 01:07:01 -0400666 printk(KERN_WARNING "Error "
667 "allocating crypto "
668 "buffer--skipping "
669 "crypto\n");
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400670 res = fscrypt_fname_disk_to_usr(dir,
671 0, 0, &de_name,
672 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400673 if (res) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400674 printk(KERN_WARNING "Error "
675 "converting filename "
676 "from disk to usr"
677 "\n");
678 name = "??";
679 len = 2;
680 } else {
681 name = fname_crypto_str.name;
682 len = fname_crypto_str.len;
683 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700684 if (IS_CASEFOLDED(dir))
685 h.hash = EXT4_DIRENT_HASH(de);
686 else
687 ext4fs_dirhash(dir, de->name,
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400688 de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400689 printk("%*.s:(E)%x.%u ", len, name,
690 h.hash, (unsigned) ((char *) de
691 - base));
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400692 fscrypt_fname_free_buffer(
693 &fname_crypto_str);
Michael Halcrowb3098482015-04-12 01:07:01 -0400694 }
695#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 int len = de->name_len;
697 char *name = de->name;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400698 ext4fs_dirhash(dir, de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400699 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400700 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400701#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700703 space += ext4_dir_rec_len(de->name_len, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 names++;
705 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500706 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400708 printk(KERN_CONT "(%i)\n", names);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 return (struct stats) { names, space, 1 };
710}
711
712struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
713 struct dx_entry *entries, int levels)
714{
715 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400716 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 unsigned bcount = 0;
718 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 printk("%i indexed blocks...\n", count);
720 for (i = 0; i < count; i++, entries++)
721 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500722 ext4_lblk_t block = dx_get_block(entries);
723 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
725 struct stats stats;
726 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400727 bh = ext4_bread(NULL,dir, block, 0);
728 if (!bh || IS_ERR(bh))
729 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 stats = levels?
731 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400732 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
733 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 names += stats.names;
735 space += stats.space;
736 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400737 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 }
739 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400740 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400741 levels ? "" : " ", names, space/bcount,
742 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743 return (struct stats) { names, space, bcount};
744}
745#endif /* DX_DEBUG */
746
747/*
748 * Probe for a directory leaf block to search.
749 *
750 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
751 * error in the directory index, and the caller should fall back to
752 * searching the directory normally. The callers of dx_probe **MUST**
753 * check for this error code, and make sure it never gets reflected
754 * back to userspace.
755 */
756static struct dx_frame *
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400757dx_probe(struct ext4_filename *fname, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400758 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759{
760 unsigned count, indirect;
761 struct dx_entry *at, *entries, *p, *q, *m;
762 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700763 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400764 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 u32 hash;
766
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400767 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400768 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
769 if (IS_ERR(frame->bh))
770 return (struct dx_frame *) frame->bh;
771
772 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773 if (root->info.hash_version != DX_HASH_TEA &&
774 root->info.hash_version != DX_HASH_HALF_MD4 &&
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700775 root->info.hash_version != DX_HASH_LEGACY &&
776 root->info.hash_version != DX_HASH_SIPHASH) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400777 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
778 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 goto fail;
780 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -0700781 if (ext4_hash_in_dirent(dir)) {
782 if (root->info.hash_version != DX_HASH_SIPHASH) {
783 ext4_warning_inode(dir,
784 "Hash in dirent, but hash is not SIPHASH");
785 goto fail;
786 }
787 } else {
788 if (root->info.hash_version == DX_HASH_SIPHASH) {
789 ext4_warning_inode(dir,
790 "Hash code is SIPHASH, but hash not in dirent");
791 goto fail;
792 }
793 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400794 if (fname)
795 hinfo = &fname->hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400797 if (hinfo->hash_version <= DX_HASH_TEA)
798 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700799 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -0800800 /* hash is already computed for encrypted casefolded directory */
801 if (fname && fname_name(fname) &&
802 !(IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)))
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400803 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700804 hash = hinfo->hash;
805
806 if (root->info.unused_flags & 1) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400807 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
808 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 goto fail;
810 }
811
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400812 indirect = root->info.indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400813 if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
814 ext4_warning(dir->i_sb,
815 "Directory (ino: %lu) htree depth %#06x exceed"
816 "supported value", dir->i_ino,
817 ext4_dir_htree_level(dir->i_sb));
818 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
819 ext4_warning(dir->i_sb, "Enable large directory "
820 "feature to access it");
821 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 goto fail;
823 }
824
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400825 entries = (struct dx_entry *)(((char *)&root->info) +
826 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700827
828 if (dx_get_limit(entries) != dx_root_limit(dir,
829 root->info.info_length)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400830 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
831 dx_get_limit(entries),
832 dx_root_limit(dir, root->info.info_length));
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700833 goto fail;
834 }
835
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400836 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400837 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700839 if (!count || count > dx_get_limit(entries)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400840 ext4_warning_inode(dir,
841 "dx entry: count %u beyond limit %u",
842 count, dx_get_limit(entries));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400843 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700844 }
845
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 p = entries + 1;
847 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400848 while (p <= q) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400849 m = p + (q - p) / 2;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400850 dxtrace(printk(KERN_CONT "."));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 if (dx_get_hash(m) > hash)
852 q = m - 1;
853 else
854 p = m + 1;
855 }
856
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400857 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 unsigned n = count - 1;
859 at = entries;
860 while (n--)
861 {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400862 dxtrace(printk(KERN_CONT ","));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 if (dx_get_hash(++at) > hash)
864 {
865 at--;
866 break;
867 }
868 }
869 assert (at == p - 1);
870 }
871
872 at = p - 1;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400873 dxtrace(printk(KERN_CONT " %x->%u\n",
874 at == entries ? 0 : dx_get_hash(at),
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400875 dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 frame->entries = entries;
877 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400878 if (!indirect--)
879 return frame;
880 frame++;
881 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
882 if (IS_ERR(frame->bh)) {
883 ret_err = (struct dx_frame *) frame->bh;
884 frame->bh = NULL;
885 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400886 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400887 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400888
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400889 if (dx_get_limit(entries) != dx_node_limit(dir)) {
890 ext4_warning_inode(dir,
891 "dx entry: limit %u != node limit %u",
892 dx_get_limit(entries), dx_node_limit(dir));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400893 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700894 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400896fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 while (frame >= frame_in) {
898 brelse(frame->bh);
899 frame--;
900 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400901
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400902 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400903 ext4_warning_inode(dir,
904 "Corrupt directory, running e2fsck is recommended");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400905 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906}
907
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400908static void dx_release(struct dx_frame *frames)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400910 struct dx_root_info *info;
911 int i;
Sahitya Tummala08fc98a2019-05-10 22:00:33 -0400912 unsigned int indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400913
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914 if (frames[0].bh == NULL)
915 return;
916
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400917 info = &((struct dx_root *)frames[0].bh->b_data)->info;
Sahitya Tummala08fc98a2019-05-10 22:00:33 -0400918 /* save local copy, "info" may be freed after brelse() */
919 indirect_levels = info->indirect_levels;
920 for (i = 0; i <= indirect_levels; i++) {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400921 if (frames[i].bh == NULL)
922 break;
923 brelse(frames[i].bh);
924 frames[i].bh = NULL;
925 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926}
927
928/*
929 * This function increments the frame pointer to search the next leaf
930 * block, and reads in the necessary intervening nodes if the search
931 * should be necessary. Whether or not the search is necessary is
932 * controlled by the hash parameter. If the hash value is even, then
933 * the search is only continued if the next block starts with that
934 * hash value. This is used if we are searching for a specific file.
935 *
936 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
937 *
938 * This function returns 1 if the caller should continue to search,
939 * or 0 if it should not. If there is an error reading one of the
940 * index blocks, it will a negative error code.
941 *
942 * If start_hash is non-null, it will be filled in with the starting
943 * hash of the next page.
944 */
Mingming Cao617ba132006-10-11 01:20:53 -0700945static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 struct dx_frame *frame,
947 struct dx_frame *frames,
948 __u32 *start_hash)
949{
950 struct dx_frame *p;
951 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500952 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 __u32 bhash;
954
955 p = frame;
956 /*
957 * Find the next leaf page by incrementing the frame pointer.
958 * If we run out of entries in the interior node, loop around and
959 * increment pointer in the parent node. When we break out of
960 * this loop, num_frames indicates the number of interior
961 * nodes need to be read.
962 */
963 while (1) {
964 if (++(p->at) < p->entries + dx_get_count(p->entries))
965 break;
966 if (p == frames)
967 return 0;
968 num_frames++;
969 p--;
970 }
971
972 /*
973 * If the hash is 1, then continue only if the next page has a
974 * continuation hash of any value. This is used for readdir
975 * handling. Otherwise, check to see if the hash matches the
976 * desired contiuation hash. If it doesn't, return since
977 * there's no point to read in the successive index pages.
978 */
979 bhash = dx_get_hash(p->at);
980 if (start_hash)
981 *start_hash = bhash;
982 if ((hash & 1) == 0) {
983 if ((bhash & ~1) != hash)
984 return 0;
985 }
986 /*
987 * If the hash is HASH_NB_ALWAYS, we always go to the next
988 * block so no check is necessary
989 */
990 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500991 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
992 if (IS_ERR(bh))
993 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700994 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400995 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 p->bh = bh;
997 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
998 }
999 return 1;
1000}
1001
1002
1003/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 * This function fills a red-black tree with information from a
1005 * directory block. It returns the number directory entries loaded
1006 * into the tree. If there is an error it is returned in err.
1007 */
1008static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001009 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 struct dx_hash_info *hinfo,
1011 __u32 start_hash, __u32 start_minor_hash)
1012{
1013 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001014 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -04001015 int err = 0, count = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001016 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001017 int csum = ext4_has_metadata_csum(dir->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001019 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
1020 (unsigned long)block));
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04001021 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001022 if (IS_ERR(bh))
1023 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001024
Mingming Cao617ba132006-10-11 01:20:53 -07001025 de = (struct ext4_dir_entry_2 *) bh->b_data;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001026 /* csum entries are not larger in the casefolded encrypted case */
Mingming Cao617ba132006-10-11 01:20:53 -07001027 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001028 dir->i_sb->s_blocksize -
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001029 ext4_dir_rec_len(0,
1030 csum ? NULL : dir));
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001031 /* Check if the directory is encrypted */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301032 if (IS_ENCRYPTED(dir)) {
Eric Biggers4af7c342020-12-02 18:20:37 -08001033 err = fscrypt_prepare_readdir(dir);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -04001034 if (err < 0) {
1035 brelse(bh);
1036 return err;
1037 }
Jeff Layton8b10fe62020-08-10 10:21:39 -04001038 err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
1039 &fname_crypto_str);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001040 if (err < 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001041 brelse(bh);
1042 return err;
1043 }
1044 }
Eric Biggers64c314f2019-12-09 13:32:25 -08001045
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001046 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -05001047 if (ext4_check_dir_entry(dir, NULL, de, bh,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001048 bh->b_data, bh->b_size, block,
Theodore Ts'ocad3f002010-12-19 22:07:02 -05001049 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1050 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001051 /* silently ignore the rest of the block */
1052 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001053 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001054 if (ext4_hash_in_dirent(dir)) {
1055 if (de->name_len && de->inode) {
1056 hinfo->hash = EXT4_DIRENT_HASH(de);
1057 hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1058 } else {
1059 hinfo->hash = 0;
1060 hinfo->minor_hash = 0;
1061 }
1062 } else {
1063 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1064 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 if ((hinfo->hash < start_hash) ||
1066 ((hinfo->hash == start_hash) &&
1067 (hinfo->minor_hash < start_minor_hash)))
1068 continue;
1069 if (de->inode == 0)
1070 continue;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301071 if (!IS_ENCRYPTED(dir)) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001072 tmp_str.name = de->name;
1073 tmp_str.len = de->name_len;
1074 err = ext4_htree_store_dirent(dir_file,
1075 hinfo->hash, hinfo->minor_hash, de,
1076 &tmp_str);
1077 } else {
Theodore Ts'od2299592015-05-18 13:15:47 -04001078 int save_len = fname_crypto_str.len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001079 struct fscrypt_str de_name = FSTR_INIT(de->name,
1080 de->name_len);
Theodore Ts'od2299592015-05-18 13:15:47 -04001081
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001082 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001083 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1084 hinfo->minor_hash, &de_name,
1085 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001086 if (err) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001087 count = err;
1088 goto errout;
1089 }
1090 err = ext4_htree_store_dirent(dir_file,
1091 hinfo->hash, hinfo->minor_hash, de,
1092 &fname_crypto_str);
Theodore Ts'od2299592015-05-18 13:15:47 -04001093 fname_crypto_str.len = save_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001094 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001095 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001096 count = err;
1097 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 }
1099 count++;
1100 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001101errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001103 fscrypt_fname_free_buffer(&fname_crypto_str);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104 return count;
1105}
1106
1107
1108/*
1109 * This function fills a red-black tree with information from a
1110 * directory. We start scanning the directory in hash order, starting
1111 * at start_hash and start_minor_hash.
1112 *
1113 * This function returns the number of entries inserted into the tree,
1114 * or a negative error code.
1115 */
Mingming Cao617ba132006-10-11 01:20:53 -07001116int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 __u32 start_minor_hash, __u32 *next_hash)
1118{
1119 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001120 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001121 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001122 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001123 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001125 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 __u32 hashval;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001127 struct fscrypt_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128
Theodore Ts'o60e66792010-05-17 07:00:00 -04001129 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001130 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001131 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001132 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001133 if (ext4_hash_in_dirent(dir))
1134 hinfo.hash_version = DX_HASH_SIPHASH;
1135 else
1136 hinfo.hash_version =
1137 EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001138 if (hinfo.hash_version <= DX_HASH_TEA)
1139 hinfo.hash_version +=
1140 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001141 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001142 if (ext4_has_inline_data(dir)) {
1143 int has_inline_data = 1;
Theodore Ts'o7633b082019-06-21 21:57:00 -04001144 count = ext4_inlinedir_to_tree(dir_file, dir, 0,
1145 &hinfo, start_hash,
1146 start_minor_hash,
1147 &has_inline_data);
Tao Ma8af0f082013-04-19 17:53:09 -04001148 if (has_inline_data) {
1149 *next_hash = ~0;
1150 return count;
1151 }
1152 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1154 start_hash, start_minor_hash);
1155 *next_hash = ~0;
1156 return count;
1157 }
1158 hinfo.hash = start_hash;
1159 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001160 frame = dx_probe(NULL, dir, &hinfo, frames);
1161 if (IS_ERR(frame))
1162 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163
1164 /* Add '.' and '..' from the htree header */
1165 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001166 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001167 tmp_str.name = de->name;
1168 tmp_str.len = de->name_len;
1169 err = ext4_htree_store_dirent(dir_file, 0, 0,
1170 de, &tmp_str);
1171 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 goto errout;
1173 count++;
1174 }
1175 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001176 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001177 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001178 tmp_str.name = de->name;
1179 tmp_str.len = de->name_len;
1180 err = ext4_htree_store_dirent(dir_file, 2, 0,
1181 de, &tmp_str);
1182 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 goto errout;
1184 count++;
1185 }
1186
1187 while (1) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -04001188 if (fatal_signal_pending(current)) {
1189 err = -ERESTARTSYS;
1190 goto errout;
1191 }
1192 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193 block = dx_get_block(frame->at);
1194 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1195 start_hash, start_minor_hash);
1196 if (ret < 0) {
1197 err = ret;
1198 goto errout;
1199 }
1200 count += ret;
1201 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001202 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203 frame, frames, &hashval);
1204 *next_hash = hashval;
1205 if (ret < 0) {
1206 err = ret;
1207 goto errout;
1208 }
1209 /*
1210 * Stop if: (a) there are no more entries, or
1211 * (b) we have inserted at least one entry and the
1212 * next hash value is not a continuation
1213 */
1214 if ((ret == 0) ||
1215 (count && ((hashval & 1) == 0)))
1216 break;
1217 }
1218 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001219 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1220 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 return count;
1222errout:
1223 dx_release(frames);
1224 return (err);
1225}
1226
Tao Ma7335cd32012-12-10 14:05:59 -05001227static inline int search_dirblock(struct buffer_head *bh,
1228 struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001229 struct ext4_filename *fname,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001230 ext4_lblk_t lblk,
Tao Ma7335cd32012-12-10 14:05:59 -05001231 unsigned int offset,
1232 struct ext4_dir_entry_2 **res_dir)
1233{
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001234 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001235 fname, lblk, offset, res_dir);
Tao Ma7335cd32012-12-10 14:05:59 -05001236}
1237
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238/*
1239 * Directory block splitting, compacting
1240 */
1241
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001242/*
1243 * Create map of hash values, offsets, and sizes, stored at end of block.
1244 * Returns number of entries mapped.
1245 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001246static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1247 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001248 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249{
1250 int count = 0;
1251 char *base = (char *) de;
1252 struct dx_hash_info h = *hinfo;
1253
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001254 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 if (de->name_len && de->inode) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001256 if (ext4_hash_in_dirent(dir))
1257 h.hash = EXT4_DIRENT_HASH(de);
1258 else
1259 ext4fs_dirhash(dir, de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260 map_tail--;
1261 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001262 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001263 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264 count++;
1265 cond_resched();
1266 }
1267 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001268 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 }
1270 return count;
1271}
1272
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001273/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1275{
Andrew Morton63f57932006-10-11 01:21:24 -07001276 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)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001294 swap(*(q+1), *q);
1295 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 }
1297 } while(more);
1298}
1299
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001300static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301{
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}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001314#ifdef CONFIG_UNICODE
1315/*
1316 * Test whether a case-insensitive directory entry matches the filename
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001317 * being searched for. If quick is set, assume the name being looked up
1318 * is already in the casefolded form.
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001319 *
1320 * Returns: 0 if the directory entry matches, more than 0 if it
1321 * doesn't match or less than zero on error.
1322 */
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001323static int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
1324 u8 *de_name, size_t de_name_len, bool quick)
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001325{
Daniel Rosenberg8b67e802020-02-06 14:41:16 -08001326 const struct super_block *sb = parent->i_sb;
1327 const struct unicode_map *um = sb->s_encoding;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001328 struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
1329 struct qstr entry = QSTR_INIT(de_name, de_name_len);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001330 int ret;
1331
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001332 if (IS_ENCRYPTED(parent)) {
1333 const struct fscrypt_str encrypted_name =
1334 FSTR_INIT(de_name, de_name_len);
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001335
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001336 decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
1337 if (!decrypted_name.name)
1338 return -ENOMEM;
1339 ret = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
1340 &decrypted_name);
1341 if (ret < 0)
1342 goto out;
1343 entry.name = decrypted_name.name;
1344 entry.len = decrypted_name.len;
1345 }
1346
1347 if (quick)
1348 ret = utf8_strncasecmp_folded(um, name, &entry);
1349 else
1350 ret = utf8_strncasecmp(um, name, &entry);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001351 if (ret < 0) {
1352 /* Handle invalid character sequence as either an error
1353 * or as an opaque byte sequence.
1354 */
Eric Biggers76bfcb22020-09-21 16:10:07 -07001355 if (sb_has_strict_encoding(sb))
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001356 ret = -EINVAL;
1357 else if (name->len != entry.len)
1358 ret = 1;
1359 else
1360 ret = !!memcmp(name->name, entry.name, entry.len);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001361 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001362out:
1363 kfree(decrypted_name.name);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001364 return ret;
1365}
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001366
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001367int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1368 struct ext4_filename *name)
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001369{
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001370 struct fscrypt_str *cf_name = &name->cf_name;
1371 struct dx_hash_info *hinfo = &name->hinfo;
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001372 int len;
1373
Eric Biggers4358dcf2020-11-09 12:49:32 -08001374 if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding ||
1375 (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001376 cf_name->name = NULL;
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001377 return 0;
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001378 }
1379
1380 cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
1381 if (!cf_name->name)
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001382 return -ENOMEM;
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001383
Daniel Rosenberg8b67e802020-02-06 14:41:16 -08001384 len = utf8_casefold(dir->i_sb->s_encoding,
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001385 iname, cf_name->name,
1386 EXT4_NAME_LEN);
1387 if (len <= 0) {
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001388 kfree(cf_name->name);
1389 cf_name->name = NULL;
1390 }
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001391 cf_name->len = (unsigned) len;
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001392 if (!IS_ENCRYPTED(dir))
1393 return 0;
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001394
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001395 hinfo->hash_version = DX_HASH_SIPHASH;
1396 hinfo->seed = NULL;
1397 if (cf_name->name)
1398 ext4fs_dirhash(dir, cf_name->name, cf_name->len, hinfo);
1399 else
1400 ext4fs_dirhash(dir, iname->name, iname->len, hinfo);
1401 return 0;
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001402}
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001403#endif
1404
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001405/*
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001406 * Test whether a directory entry matches the filename being searched for.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001407 *
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001408 * Return: %true if the directory entry matches, otherwise %false.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 */
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001410static bool ext4_match(struct inode *parent,
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001411 const struct ext4_filename *fname,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001412 struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413{
Eric Biggers067d1022017-04-24 10:00:11 -07001414 struct fscrypt_name f;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001415
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001416 if (!de->inode)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001417 return false;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001418
Eric Biggers067d1022017-04-24 10:00:11 -07001419 f.usr_fname = fname->usr_fname;
1420 f.disk_name = fname->disk_name;
Chandan Rajendra643fa962018-12-12 15:20:12 +05301421#ifdef CONFIG_FS_ENCRYPTION
Eric Biggers067d1022017-04-24 10:00:11 -07001422 f.crypto_buf = fname->crypto_buf;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001423#endif
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001424
1425#ifdef CONFIG_UNICODE
Eric Biggers4358dcf2020-11-09 12:49:32 -08001426 if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) &&
1427 (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001428 if (fname->cf_name.name) {
1429 struct qstr cf = {.name = fname->cf_name.name,
1430 .len = fname->cf_name.len};
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001431 if (IS_ENCRYPTED(parent)) {
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001432 if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
1433 fname->hinfo.minor_hash !=
1434 EXT4_DIRENT_MINOR_HASH(de)) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001435
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001436 return 0;
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08001437 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001438 }
1439 return !ext4_ci_compare(parent, &cf, de->name,
1440 de->name_len, true);
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001441 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001442 return !ext4_ci_compare(parent, fname->usr_fname, de->name,
1443 de->name_len, false);
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001444 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001445#endif
1446
Eric Biggers067d1022017-04-24 10:00:11 -07001447 return fscrypt_match_name(&f, de->name, de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001448}
1449
1450/*
1451 * Returns 0 if not found, -1 on failure, and 1 on success
1452 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001453int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1454 struct inode *dir, struct ext4_filename *fname,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001455 ext4_lblk_t lblk, unsigned int offset,
1456 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001457{
Mingming Cao617ba132006-10-11 01:20:53 -07001458 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 char * dlimit;
1460 int de_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001461
Tao Ma7335cd32012-12-10 14:05:59 -05001462 de = (struct ext4_dir_entry_2 *)search_buf;
1463 dlimit = search_buf + buf_size;
Ye Bin75ac7242022-03-24 14:48:16 +08001464 while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 /* this code is executed quadratically often */
1466 /* do minimal checking `by hand' */
Ye Bin75ac7242022-03-24 14:48:16 +08001467 if (de->name + de->name_len <= dlimit &&
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001468 ext4_match(dir, fname, de)) {
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001469 /* found a match - just to be sure, do
1470 * a full check */
Jan Kara7303cb52020-07-31 18:21:35 +02001471 if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
Eric Biggers7dda6582020-09-14 11:15:25 -07001472 buf_size, lblk, offset))
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001473 return -1;
1474 *res_dir = de;
1475 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 }
1477 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001478 de_len = ext4_rec_len_from_disk(de->rec_len,
1479 dir->i_sb->s_blocksize);
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001480 if (de_len <= 0)
1481 return -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001483 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484 }
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001485 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486}
1487
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001488static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1489 struct ext4_dir_entry *de)
1490{
1491 struct super_block *sb = dir->i_sb;
1492
1493 if (!is_dx(dir))
1494 return 0;
1495 if (block == 0)
1496 return 1;
1497 if (de->inode == 0 &&
1498 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1499 sb->s_blocksize)
1500 return 1;
1501 return 0;
1502}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503
1504/*
Eric Biggersb01531d2019-03-20 11:39:13 -07001505 * __ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001506 *
1507 * finds an entry in the specified directory with the wanted name. It
1508 * returns the cache buffer in which the entry was found, and the entry
1509 * itself (as a parameter - res_dir). It does NOT read the inode of the
1510 * entry - you'll have to do that yourself if you want to.
1511 *
1512 * The returned buffer_head has ->b_count elevated. The caller is expected
1513 * to brelse() it when appropriate.
1514 */
Eric Biggersb01531d2019-03-20 11:39:13 -07001515static struct buffer_head *__ext4_find_entry(struct inode *dir,
1516 struct ext4_filename *fname,
1517 struct ext4_dir_entry_2 **res_dir,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001518 int *inlined, ext4_lblk_t *lblk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001519{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001520 struct super_block *sb;
1521 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1522 struct buffer_head *bh, *ret = NULL;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001523 ext4_lblk_t start, block;
Eric Biggersb01531d2019-03-20 11:39:13 -07001524 const u8 *name = fname->usr_fname->name;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001525 size_t ra_max = 0; /* Number of bh's in the readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 buffer, bh_use[] */
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001527 size_t ra_ptr = 0; /* Current index into readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528 buffer */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001529 ext4_lblk_t nblocks;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001530 int i, namelen, retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001531
1532 *res_dir = NULL;
1533 sb = dir->i_sb;
Eric Biggersb01531d2019-03-20 11:39:13 -07001534 namelen = fname->usr_fname->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001535 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001537
1538 if (ext4_has_inline_data(dir)) {
1539 int has_inline_data = 1;
Eric Biggersb01531d2019-03-20 11:39:13 -07001540 ret = ext4_find_inline_entry(dir, fname, res_dir,
Tao Mae8e948e2012-12-10 14:06:00 -05001541 &has_inline_data);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001542 if (lblk)
1543 *lblk = 0;
Tao Ma32f7f222012-12-10 14:06:01 -05001544 if (has_inline_data) {
1545 if (inlined)
1546 *inlined = 1;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001547 goto cleanup_and_exit;
Tao Ma32f7f222012-12-10 14:06:01 -05001548 }
Tao Mae8e948e2012-12-10 14:06:00 -05001549 }
1550
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001551 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001552 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001553 /*
1554 * "." or ".." will only be in the first block
1555 * NFS may look up ".."; "." should be handled by the VFS
1556 */
1557 block = start = 0;
1558 nblocks = 1;
1559 goto restart;
1560 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001561 if (is_dx(dir)) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001562 ret = ext4_dx_find_entry(dir, fname, res_dir, lblk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 /*
1564 * On success, or if the error was file not found,
1565 * return. Otherwise, fall back to doing a search the
1566 * old fashioned way.
1567 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001568 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
1569 goto cleanup_and_exit;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001570 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1571 "falling back\n"));
Eric Sandeenf39b3f42018-07-29 17:13:42 -04001572 ret = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 }
Mingming Cao617ba132006-10-11 01:20:53 -07001574 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Chandan Rajendra9d5afec2017-12-11 15:00:57 -05001575 if (!nblocks) {
1576 ret = NULL;
1577 goto cleanup_and_exit;
1578 }
Mingming Cao617ba132006-10-11 01:20:53 -07001579 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 if (start >= nblocks)
1581 start = 0;
1582 block = start;
1583restart:
1584 do {
1585 /*
1586 * We deal with the read-ahead logic here.
1587 */
Shijie Luo9424ef52020-02-15 03:02:06 -05001588 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 if (ra_ptr >= ra_max) {
1590 /* Refill the readahead buffer */
1591 ra_ptr = 0;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001592 if (block < start)
1593 ra_max = start - block;
1594 else
1595 ra_max = nblocks - block;
1596 ra_max = min(ra_max, ARRAY_SIZE(bh_use));
1597 retval = ext4_bread_batch(dir, block, ra_max,
1598 false /* wait */, bh_use);
1599 if (retval) {
1600 ret = ERR_PTR(retval);
1601 ra_max = 0;
1602 goto cleanup_and_exit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 }
1604 }
1605 if ((bh = bh_use[ra_ptr++]) == NULL)
1606 goto next;
1607 wait_on_buffer(bh);
1608 if (!buffer_uptodate(bh)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04001609 EXT4_ERROR_INODE_ERR(dir, EIO,
1610 "reading directory lblock %lu",
1611 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612 brelse(bh);
Khazhismel Kumykov6febe6f2017-06-23 00:29:05 -04001613 ret = ERR_PTR(-EIO);
1614 goto cleanup_and_exit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001616 if (!buffer_verified(bh) &&
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001617 !is_dx_internal_node(dir, block,
1618 (struct ext4_dir_entry *)bh->b_data) &&
Theodore Ts'of036adb2019-06-21 15:49:26 -04001619 !ext4_dirblock_csum_verify(dir, bh)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04001620 EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
1621 "checksumming directory "
1622 "block %lu", (unsigned long)block);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001623 brelse(bh);
Theodore Ts'obdddf342017-06-23 00:47:05 -04001624 ret = ERR_PTR(-EFSBADCRC);
1625 goto cleanup_and_exit;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001626 }
1627 set_buffer_verified(bh);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001628 i = search_dirblock(bh, dir, fname, block,
Mingming Cao617ba132006-10-11 01:20:53 -07001629 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630 if (i == 1) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001631 if (lblk)
1632 *lblk = block;
Mingming Cao617ba132006-10-11 01:20:53 -07001633 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 ret = bh;
1635 goto cleanup_and_exit;
1636 } else {
1637 brelse(bh);
1638 if (i < 0)
1639 goto cleanup_and_exit;
1640 }
1641 next:
1642 if (++block >= nblocks)
1643 block = 0;
1644 } while (block != start);
1645
1646 /*
1647 * If the directory has grown while we were searching, then
1648 * search the last part of the directory before giving up.
1649 */
1650 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001651 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 if (block < nblocks) {
1653 start = 0;
1654 goto restart;
1655 }
1656
1657cleanup_and_exit:
1658 /* Clean up the read-ahead blocks */
1659 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001660 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001661 return ret;
1662}
1663
Eric Biggersb01531d2019-03-20 11:39:13 -07001664static struct buffer_head *ext4_find_entry(struct inode *dir,
1665 const struct qstr *d_name,
1666 struct ext4_dir_entry_2 **res_dir,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001667 int *inlined, ext4_lblk_t *lblk)
Eric Biggersb01531d2019-03-20 11:39:13 -07001668{
1669 int err;
1670 struct ext4_filename fname;
1671 struct buffer_head *bh;
1672
1673 err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1674 if (err == -ENOENT)
1675 return NULL;
1676 if (err)
1677 return ERR_PTR(err);
1678
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001679 bh = __ext4_find_entry(dir, &fname, res_dir, inlined, lblk);
Eric Biggersb01531d2019-03-20 11:39:13 -07001680
1681 ext4_fname_free_filename(&fname);
1682 return bh;
1683}
1684
1685static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1686 struct dentry *dentry,
1687 struct ext4_dir_entry_2 **res_dir)
1688{
1689 int err;
1690 struct ext4_filename fname;
1691 struct buffer_head *bh;
1692
1693 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
Eric Biggers35693712020-11-20 10:34:32 -08001694 generic_set_encrypted_ci_d_ops(dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -07001695 if (err == -ENOENT)
1696 return NULL;
1697 if (err)
1698 return ERR_PTR(err);
1699
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001700 bh = __ext4_find_entry(dir, &fname, res_dir, NULL, NULL);
Eric Biggersb01531d2019-03-20 11:39:13 -07001701
1702 ext4_fname_free_filename(&fname);
1703 return bh;
1704}
1705
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001706static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1707 struct ext4_filename *fname,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001708 struct ext4_dir_entry_2 **res_dir, ext4_lblk_t *lblk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001710 struct super_block * sb = dir->i_sb;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001711 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001713 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001715
Chandan Rajendra643fa962018-12-12 15:20:12 +05301716#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001717 *res_dir = NULL;
1718#endif
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001719 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001720 if (IS_ERR(frame))
1721 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722 do {
1723 block = dx_get_block(frame->at);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001724 if (lblk)
1725 *lblk = block;
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04001726 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001727 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001729
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001730 retval = search_dirblock(bh, dir, fname, block,
Theodore Ts'o7845c042010-10-27 21:30:08 -04001731 block << EXT4_BLOCK_SIZE_BITS(sb),
1732 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001733 if (retval == 1)
1734 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001735 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001736 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001737 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001738 goto errout;
1739 }
1740
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 /* Check to see if we should continue to search */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001742 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743 frames, NULL);
1744 if (retval < 0) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04001745 ext4_warning_inode(dir,
1746 "error %d reading directory index block",
1747 retval);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001748 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001749 goto errout;
1750 }
1751 } while (retval == 1);
1752
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001753 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001754errout:
Eric Biggersd6b97552017-05-24 18:10:49 -04001755 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001756success:
1757 dx_release(frames);
1758 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001760
Al Viro00cd8dd2012-06-10 17:13:09 -04001761static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001763 struct inode *inode;
1764 struct ext4_dir_entry_2 *de;
1765 struct buffer_head *bh;
Theodore Ts'o28b4c262016-02-07 19:35:05 -05001766
Eric Biggers89904272017-10-18 20:21:58 -04001767 if (dentry->d_name.len > EXT4_NAME_LEN)
1768 return ERR_PTR(-ENAMETOOLONG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769
Eric Biggersb01531d2019-03-20 11:39:13 -07001770 bh = ext4_lookup_entry(dir, dentry, &de);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001771 if (IS_ERR(bh))
Al Viroe884bce2018-10-10 16:41:40 -04001772 return ERR_CAST(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773 inode = NULL;
1774 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001775 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001776 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001777 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001778 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001779 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001780 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001781 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001782 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1783 dentry);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001784 return ERR_PTR(-EFSCORRUPTED);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001785 }
Theodore Ts'o8a363972018-12-19 12:29:13 -05001786 inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
Al Viroa9049372011-07-08 21:20:11 -04001787 if (inode == ERR_PTR(-ESTALE)) {
1788 EXT4_ERROR_INODE(dir,
1789 "deleted inode referenced: %u",
1790 ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001791 return ERR_PTR(-EFSCORRUPTED);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001792 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301793 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
Theodore Ts'off978b02016-02-08 00:54:26 -05001794 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001795 !fscrypt_has_permitted_context(dir, inode)) {
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001796 ext4_warning(inode->i_sb,
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001797 "Inconsistent encryption contexts: %lu/%lu",
Eric Biggers8c680842017-04-07 10:58:38 -07001798 dir->i_ino, inode->i_ino);
Eric Biggersdd01b692017-02-01 21:07:11 -05001799 iput(inode);
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001800 return ERR_PTR(-EPERM);
1801 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001802 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001803
1804#ifdef CONFIG_UNICODE
1805 if (!inode && IS_CASEFOLDED(dir)) {
1806 /* Eventually we want to call d_add_ci(dentry, NULL)
1807 * for negative dentries in the encoding case as
1808 * well. For now, prevent the negative dentry
1809 * from being cached.
1810 */
1811 return NULL;
1812 }
1813#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814 return d_splice_alias(inode, dentry);
1815}
1816
1817
Mingming Cao617ba132006-10-11 01:20:53 -07001818struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001820 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001821 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001822 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823 struct buffer_head *bh;
1824
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001825 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001826 if (IS_ERR(bh))
Al Viroe884bce2018-10-10 16:41:40 -04001827 return ERR_CAST(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001828 if (!bh)
1829 return ERR_PTR(-ENOENT);
1830 ino = le32_to_cpu(de->inode);
1831 brelse(bh);
1832
Al Virofc640052016-04-10 01:33:30 -04001833 if (!ext4_valid_inum(child->d_sb, ino)) {
David Howells2b0143b2015-03-17 22:25:59 +00001834 EXT4_ERROR_INODE(d_inode(child),
Theodore Ts'o24676da2010-05-16 21:00:00 -04001835 "bad parent inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001836 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001837 }
1838
Theodore Ts'o8a363972018-12-19 12:29:13 -05001839 return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840}
1841
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001842/*
1843 * Move count entries from end of map between two memory locations.
1844 * Returns pointer to last entry moved.
1845 */
Mingming Cao617ba132006-10-11 01:20:53 -07001846static struct ext4_dir_entry_2 *
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001847dx_move_dirents(struct inode *dir, char *from, char *to,
1848 struct dx_map_entry *map, int count,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001849 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001850{
1851 unsigned rec_len = 0;
1852
1853 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001854 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001855 (from + (map->offs<<2));
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001856 rec_len = ext4_dir_rec_len(de->name_len, dir);
1857
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001858 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001859 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001860 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001861 de->inode = 0;
1862 map++;
1863 to += rec_len;
1864 }
Mingming Cao617ba132006-10-11 01:20:53 -07001865 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866}
1867
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001868/*
1869 * Compact each dir entry in the range to the minimal rec_len.
1870 * Returns pointer to last entry in range.
1871 */
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001872static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
1873 unsigned int blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001874{
Mingming Cao617ba132006-10-11 01:20:53 -07001875 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 unsigned rec_len = 0;
1877
1878 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001879 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001880 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001881 if (de->inode && de->name_len) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001882 rec_len = ext4_dir_rec_len(de->name_len, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883 if (de > to)
1884 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001885 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001886 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001887 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001888 }
1889 de = next;
1890 }
1891 return prev;
1892}
1893
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001894/*
1895 * Split a full leaf block to make room for a new dir entry.
1896 * Allocate a new block, and move entries so that they are approx. equally full.
1897 * Returns pointer to de in block into which the new entry will be inserted.
1898 */
Mingming Cao617ba132006-10-11 01:20:53 -07001899static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001900 struct buffer_head **bh, struct dx_frame *frame,
1901 struct dx_hash_info *hinfo, ext4_lblk_t *newblock)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001902{
1903 unsigned blocksize = dir->i_sb->s_blocksize;
1904 unsigned count, continued;
1905 struct buffer_head *bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001906 u32 hash2;
1907 struct dx_map_entry *map;
1908 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001909 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001910 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001911 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001912 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001913
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001914 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001915 csum_size = sizeof(struct ext4_dir_entry_tail);
1916
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001917 bh2 = ext4_append(handle, dir, newblock);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001918 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919 brelse(*bh);
1920 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001921 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 }
1923
1924 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001925 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001926 if (err)
1927 goto journal_error;
1928
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001929 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001930 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001931 if (err)
1932 goto journal_error;
1933
1934 data2 = bh2->b_data;
1935
1936 /* create map in the end of data2 block */
1937 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001938 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001939 blocksize, hinfo, map);
1940 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001941 dx_sort_map(map, count);
Eric Sandeen58723312020-06-17 14:19:04 -05001942 /* Ensure that neither split block is over half full */
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001943 size = 0;
1944 move = 0;
1945 for (i = count-1; i >= 0; i--) {
1946 /* is more than half of this entry in 2nd half of the block? */
1947 if (size + map[i].size/2 > blocksize/2)
1948 break;
1949 size += map[i].size;
1950 move++;
1951 }
Eric Sandeen58723312020-06-17 14:19:04 -05001952 /*
1953 * map index at which we will split
1954 *
1955 * If the sum of active entries didn't exceed half the block size, just
1956 * split it in half by count; each resulting block will have at least
1957 * half the space free.
1958 */
1959 if (i > 0)
1960 split = count - move;
1961 else
1962 split = count/2;
1963
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001964 hash2 = map[split].hash;
1965 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001966 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1967 (unsigned long)dx_get_block(frame->at),
1968 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969
1970 /* Fancy dance to stay within two buffers */
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001971 de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001972 blocksize);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001973 de = dx_pack_dirents(dir, data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001974 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1975 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001976 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001977 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1978 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001979 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001980 if (csum_size) {
Theodore Ts'oddce3b92019-06-21 16:31:47 -04001981 ext4_initialize_dirent_tail(*bh, blocksize);
1982 ext4_initialize_dirent_tail(bh2, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001983 }
1984
Michael Halcrowb3098482015-04-12 01:07:01 -04001985 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1986 blocksize, 1));
1987 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1988 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989
1990 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001991 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 swap(*bh, bh2);
1993 de = de2;
1994 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07001995 dx_insert_block(frame, hash2 + continued, *newblock);
Theodore Ts'of036adb2019-06-21 15:49:26 -04001996 err = ext4_handle_dirty_dirblock(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997 if (err)
1998 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001999 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000 if (err)
2001 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002002 brelse(bh2);
2003 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002004 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07002005
2006journal_error:
2007 brelse(*bh);
2008 brelse(bh2);
2009 *bh = NULL;
2010 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002011 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013
Tao Ma978fef92012-12-10 14:05:58 -05002014int ext4_find_dest_de(struct inode *dir, struct inode *inode,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002015 ext4_lblk_t lblk,
Tao Ma978fef92012-12-10 14:05:58 -05002016 struct buffer_head *bh,
2017 void *buf, int buf_size,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002018 struct ext4_filename *fname,
Tao Ma978fef92012-12-10 14:05:58 -05002019 struct ext4_dir_entry_2 **dest_de)
2020{
2021 struct ext4_dir_entry_2 *de;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002022 unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
Tao Ma978fef92012-12-10 14:05:58 -05002023 int nlen, rlen;
2024 unsigned int offset = 0;
2025 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04002026
Tao Ma978fef92012-12-10 14:05:58 -05002027 de = (struct ext4_dir_entry_2 *)buf;
2028 top = buf + buf_size - reclen;
2029 while ((char *) de <= top) {
2030 if (ext4_check_dir_entry(dir, NULL, de, bh,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002031 buf, buf_size, lblk, offset))
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07002032 return -EFSCORRUPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002033 if (ext4_match(dir, fname, de))
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07002034 return -EEXIST;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002035 nlen = ext4_dir_rec_len(de->name_len, dir);
Tao Ma978fef92012-12-10 14:05:58 -05002036 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2037 if ((de->inode ? rlen - nlen : rlen) >= reclen)
2038 break;
2039 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
2040 offset += rlen;
2041 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04002042 if ((char *) de > top)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07002043 return -ENOSPC;
2044
2045 *dest_de = de;
2046 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05002047}
2048
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002049void ext4_insert_dentry(struct inode *dir,
2050 struct inode *inode,
Eric Biggers1bc0af62017-04-29 23:27:26 -04002051 struct ext4_dir_entry_2 *de,
2052 int buf_size,
2053 struct ext4_filename *fname)
Tao Ma978fef92012-12-10 14:05:58 -05002054{
2055
2056 int nlen, rlen;
2057
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002058 nlen = ext4_dir_rec_len(de->name_len, dir);
Tao Ma978fef92012-12-10 14:05:58 -05002059 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2060 if (de->inode) {
2061 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002062 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05002063 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
2064 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
2065 de = de1;
2066 }
2067 de->file_type = EXT4_FT_UNKNOWN;
2068 de->inode = cpu_to_le32(inode->i_ino);
2069 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002070 de->name_len = fname_len(fname);
2071 memcpy(de->name, fname_name(fname), fname_len(fname));
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002072 if (ext4_hash_in_dirent(dir)) {
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08002073 struct dx_hash_info *hinfo = &fname->hinfo;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002074
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08002075 EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002076 EXT4_DIRENT_HASHES(de)->minor_hash =
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08002077 cpu_to_le32(hinfo->minor_hash);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002078 }
Tao Ma978fef92012-12-10 14:05:58 -05002079}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002080
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081/*
2082 * Add a new entry into a directory (leaf) block. If de is non-NULL,
2083 * it points to a directory entry which is guaranteed to be large
2084 * enough for new directory entry. If de is NULL, then
2085 * add_dirent_to_buf will attempt search the directory block for
2086 * space. It will return -ENOSPC if no space is available, and -EIO
2087 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002089static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
2090 struct inode *dir,
Mingming Cao617ba132006-10-11 01:20:53 -07002091 struct inode *inode, struct ext4_dir_entry_2 *de,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002092 ext4_lblk_t blk,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002093 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002094{
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002095 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002096 int csum_size = 0;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002097 int err, err2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002098
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002099 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002100 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002102 if (!de) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002103 err = ext4_find_dest_de(dir, inode, blk, bh, bh->b_data,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002104 blocksize - csum_size, fname, &de);
Tao Ma978fef92012-12-10 14:05:58 -05002105 if (err)
2106 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002107 }
2108 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002109 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002111 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002112 return err;
2113 }
2114
Eric Biggers1bc0af62017-04-29 23:27:26 -04002115 /* By now the buffer is marked for journaling */
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002116 ext4_insert_dentry(dir, inode, de, blocksize, fname);
Tao Ma978fef92012-12-10 14:05:58 -05002117
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118 /*
2119 * XXX shouldn't update any times until successful
2120 * completion of syscall, but too many callers depend
2121 * on this.
2122 *
2123 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07002124 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125 * recovery deletes the inode, so the worst that can
2126 * happen is that the times are slightly out of date
2127 * and/or different from the directory change time.
2128 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002129 dir->i_mtime = dir->i_ctime = current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002130 ext4_update_dx_flag(dir);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002131 inode_inc_iversion(dir);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002132 err2 = ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05002133 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Theodore Ts'of036adb2019-06-21 15:49:26 -04002134 err = ext4_handle_dirty_dirblock(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07002136 ext4_std_error(dir->i_sb, err);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002137 return err ? err : err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138}
2139
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140/*
2141 * This converts a one block unindexed directory to a 3 block indexed
2142 * directory, and adds the dentry to the indexed directory.
2143 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002144static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002145 struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146 struct inode *inode, struct buffer_head *bh)
2147{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 struct buffer_head *bh2;
2149 struct dx_root *root;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002150 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07002152 struct ext4_dir_entry_2 *de, *de2;
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002153 char *data2, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002154 unsigned len;
2155 int retval;
2156 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002157 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002159 int csum_size = 0;
2160
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002161 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002162 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163
2164 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002165 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04002166 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002167 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002168 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002169 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002170 brelse(bh);
2171 return retval;
2172 }
2173 root = (struct dx_root *) bh->b_data;
2174
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002175 /* The 0th block becomes the root, move the dirents out */
2176 fde = &root->dotdot;
2177 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002178 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002179 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002180 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002181 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002182 return -EFSCORRUPTED;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002183 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002184 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002185
2186 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002187 bh2 = ext4_append(handle, dir, &block);
2188 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002190 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002192 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002193 data2 = bh2->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002194
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002195 memcpy(data2, de, len);
2196 de = (struct ext4_dir_entry_2 *) data2;
2197 top = data2 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002198 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199 de = de2;
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002200 de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
2201 (char *) de, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002202
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002203 if (csum_size)
2204 ext4_initialize_dirent_tail(bh2, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002205
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002206 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07002207 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002208 de->rec_len = ext4_rec_len_to_disk(
2209 blocksize - ext4_dir_rec_len(2, NULL), blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002210 memset (&root->info, 0, sizeof(root->info));
2211 root->info.info_length = sizeof(root->info);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002212 if (ext4_hash_in_dirent(dir))
2213 root->info.hash_version = DX_HASH_SIPHASH;
2214 else
2215 root->info.hash_version =
2216 EXT4_SB(dir->i_sb)->s_def_hash_version;
2217
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002219 dx_set_block(entries, 1);
2220 dx_set_count(entries, 1);
2221 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002222
2223 /* Initialize as for dx_probe */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002224 fname->hinfo.hash_version = root->info.hash_version;
2225 if (fname->hinfo.hash_version <= DX_HASH_TEA)
2226 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2227 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Daniel Rosenberg48b5fe62019-11-06 17:00:25 -08002228
2229 /* casefolded encrypted hashes are computed on fname setup */
2230 if (!ext4_hash_in_dirent(dir))
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002231 ext4fs_dirhash(dir, fname_name(fname),
2232 fname_len(fname), &fname->hinfo);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002233
Jan Kara6050d472014-10-30 10:53:17 -04002234 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 frame = frames;
2236 frame->entries = entries;
2237 frame->at = entries;
2238 frame->bh = bh;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002239
Jan Kara6050d472014-10-30 10:53:17 -04002240 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2241 if (retval)
2242 goto out_frames;
Theodore Ts'of036adb2019-06-21 15:49:26 -04002243 retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002244 if (retval)
2245 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002246
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002247 de = do_split(handle, dir, &bh2, frame, &fname->hinfo, &block);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002248 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002249 retval = PTR_ERR(de);
2250 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002251 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002252
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002253 retval = add_dirent_to_buf(handle, fname, dir, inode, de, block, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002254out_frames:
2255 /*
2256 * Even if the block split failed, we have to properly write
2257 * out all the changes we did so far. Otherwise we can end up
2258 * with corrupted filesystem.
2259 */
gmaile81d4472016-09-30 01:33:37 -04002260 if (retval)
2261 ext4_mark_inode_dirty(handle, dir);
Jan Kara6050d472014-10-30 10:53:17 -04002262 dx_release(frames);
gmaile81d4472016-09-30 01:33:37 -04002263 brelse(bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002264 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002265}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002266
2267/*
Mingming Cao617ba132006-10-11 01:20:53 -07002268 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269 *
2270 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002271 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002272 *
2273 * NOTE!! The inode part of 'de' is left at 0 - which means you
2274 * may not sleep between calling this and putting something into
2275 * the entry, as someone else might have used it while you slept.
2276 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002277static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2278 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279{
David Howells2b0143b2015-03-17 22:25:59 +00002280 struct inode *dir = d_inode(dentry->d_parent);
Lukas Czernere12fb972015-04-03 10:46:58 -04002281 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002282 struct ext4_dir_entry_2 *de;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002283 struct super_block *sb;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002284 struct ext4_filename fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002288 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002289 int csum_size = 0;
2290
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002291 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002292 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293
2294 sb = dir->i_sb;
2295 blocksize = sb->s_blocksize;
2296 if (!dentry->d_name.len)
2297 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002298
Eric Biggerse5a2a002020-11-17 23:56:06 -08002299 if (fscrypt_is_nokey_name(dentry))
2300 return -ENOKEY;
2301
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002302#ifdef CONFIG_UNICODE
Eric Biggers76bfcb22020-09-21 16:10:07 -07002303 if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
Daniel Rosenberg8b67e802020-02-06 14:41:16 -08002304 sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002305 return -EINVAL;
2306#endif
2307
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002308 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2309 if (retval)
2310 return retval;
2311
Tao Ma3c47d542012-12-10 14:05:59 -05002312 if (ext4_has_inline_data(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002313 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05002314 if (retval < 0)
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002315 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002316 if (retval == 1) {
2317 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002318 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002319 }
2320 }
2321
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002322 if (is_dx(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002323 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002324 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002325 goto out;
Jan Kara48a34312020-02-10 15:43:16 +01002326 /* Can we just ignore htree data? */
2327 if (ext4_has_metadata_csum(sb)) {
2328 EXT4_ERROR_INODE(dir,
2329 "Directory has corrupted htree index.");
2330 retval = -EFSCORRUPTED;
2331 goto out;
2332 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002333 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002334 dx_fallback++;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002335 retval = ext4_mark_inode_dirty(handle, dir);
2336 if (unlikely(retval))
2337 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002338 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002340 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002341 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002342 if (bh == NULL) {
2343 bh = ext4_bread(handle, dir, block,
2344 EXT4_GET_BLOCKS_CREATE);
2345 goto add_to_new_block;
2346 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002347 if (IS_ERR(bh)) {
2348 retval = PTR_ERR(bh);
2349 bh = NULL;
2350 goto out;
2351 }
2352 retval = add_dirent_to_buf(handle, &fname, dir, inode,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002353 NULL, block, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002354 if (retval != -ENOSPC)
2355 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002356
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002357 if (blocks == 1 && !dx_fallback &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002358 ext4_has_feature_dir_index(sb)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002359 retval = make_indexed_dir(handle, &fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002360 inode, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002361 bh = NULL; /* make_indexed_dir releases bh */
2362 goto out;
2363 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 brelse(bh);
2365 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002366 bh = ext4_append(handle, dir, &block);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002367add_to_new_block:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002368 if (IS_ERR(bh)) {
2369 retval = PTR_ERR(bh);
2370 bh = NULL;
2371 goto out;
2372 }
Mingming Cao617ba132006-10-11 01:20:53 -07002373 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002374 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002375 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2376
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002377 if (csum_size)
2378 ext4_initialize_dirent_tail(bh, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002379
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002380 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, block, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002381out:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002382 ext4_fname_free_filename(&fname);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002383 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002384 if (retval == 0)
2385 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002386 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002387}
2388
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002389/*
2390 * Returns 0 for success, or a negative error value
2391 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002392static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002393 struct inode *dir, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002394{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002395 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002396 struct dx_entry *entries, *at;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002397 struct buffer_head *bh;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002398 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002399 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002400 int restart;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002401 int err;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002402 ext4_lblk_t lblk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002404again:
2405 restart = 0;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002406 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002407 if (IS_ERR(frame))
2408 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002409 entries = frame->entries;
2410 at = frame->at;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002411 lblk = dx_get_block(frame->at);
2412 bh = ext4_read_dirblock(dir, lblk, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002413 if (IS_ERR(bh)) {
2414 err = PTR_ERR(bh);
2415 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002416 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002417 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418
2419 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002420 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002421 if (err)
2422 goto journal_error;
2423
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002424 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, lblk, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002425 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002427
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002428 err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002430 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002431 dx_get_count(entries), dx_get_limit(entries)));
2432 /* Need to split index? */
2433 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002434 ext4_lblk_t newblock;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002435 int levels = frame - frames + 1;
2436 unsigned int icount;
2437 int add_level = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002438 struct dx_entry *entries2;
2439 struct dx_node *node2;
2440 struct buffer_head *bh2;
2441
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002442 while (frame > frames) {
2443 if (dx_get_count((frame - 1)->entries) <
2444 dx_get_limit((frame - 1)->entries)) {
2445 add_level = 0;
2446 break;
2447 }
2448 frame--; /* split higher index block */
2449 at = frame->at;
2450 entries = frame->entries;
2451 restart = 1;
2452 }
2453 if (add_level && levels == ext4_dir_htree_level(sb)) {
2454 ext4_warning(sb, "Directory (ino: %lu) index full, "
2455 "reach max htree level :%d",
2456 dir->i_ino, levels);
2457 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2458 ext4_warning(sb, "Large directory feature is "
2459 "not enabled on this "
2460 "filesystem");
2461 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 err = -ENOSPC;
2463 goto cleanup;
2464 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002465 icount = dx_get_count(entries);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002466 bh2 = ext4_append(handle, dir, &newblock);
2467 if (IS_ERR(bh2)) {
2468 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002470 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471 node2 = (struct dx_node *)(bh2->b_data);
2472 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002473 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002474 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2475 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002476 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002477 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002478 if (err)
2479 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002480 if (!add_level) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002481 unsigned icount1 = icount/2, icount2 = icount - icount1;
2482 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002483 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2484 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002485
2486 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002487 err = ext4_journal_get_write_access(handle,
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002488 (frame - 1)->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002489 if (err)
2490 goto journal_error;
2491
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002492 memcpy((char *) entries2, (char *) (entries + icount1),
2493 icount2 * sizeof(struct dx_entry));
2494 dx_set_count(entries, icount1);
2495 dx_set_count(entries2, icount2);
2496 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002497
2498 /* Which index block gets the new entry? */
2499 if (at - entries >= icount1) {
2500 frame->at = at = at - entries - icount1 + entries2;
2501 frame->entries = entries = entries2;
2502 swap(frame->bh, bh2);
2503 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002504 dx_insert_block((frame - 1), hash2, newblock);
2505 dxtrace(dx_show_index("node", frame->entries));
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002506 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002507 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002508 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002509 if (err)
2510 goto journal_error;
2511 brelse (bh2);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002512 err = ext4_handle_dirty_dx_node(handle, dir,
2513 (frame - 1)->bh);
2514 if (err)
2515 goto journal_error;
Theodore Ts'o858d3432021-02-04 00:05:20 -05002516 err = ext4_handle_dirty_dx_node(handle, dir,
2517 frame->bh);
Theodore Ts'occ730072021-08-04 14:23:55 -04002518 if (restart || err)
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002519 goto journal_error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002520 } else {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002521 struct dx_root *dxroot;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002522 memcpy((char *) entries2, (char *) entries,
2523 icount * sizeof(struct dx_entry));
2524 dx_set_limit(entries2, dx_node_limit(dir));
2525
2526 /* Set up root */
2527 dx_set_count(entries, 1);
2528 dx_set_block(entries + 0, newblock);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002529 dxroot = (struct dx_root *)frames[0].bh->b_data;
2530 dxroot->info.indirect_levels += 1;
2531 dxtrace(printk(KERN_DEBUG
2532 "Creating %d level index...\n",
Gabriel Krisman Bertazi799578ab2018-10-02 12:43:51 -04002533 dxroot->info.indirect_levels));
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002534 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002535 if (err)
2536 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002537 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2538 brelse(bh2);
2539 restart = 1;
2540 goto journal_error;
Theodore Ts'ob4097142011-01-10 12:46:59 -05002541 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002542 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002543 de = do_split(handle, dir, &bh, frame, &fname->hinfo, &lblk);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002544 if (IS_ERR(de)) {
2545 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002547 }
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002548 err = add_dirent_to_buf(handle, fname, dir, inode, de, lblk, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549 goto cleanup;
2550
2551journal_error:
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002552 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002554 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002555 dx_release(frames);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002556 /* @restart is true means htree-path has been changed, we need to
2557 * repeat dx_probe() to find out valid htree-path
2558 */
2559 if (restart && err == 0)
2560 goto again;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002561 return err;
2562}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002563
2564/*
Tao Ma05019a92012-12-10 14:06:00 -05002565 * ext4_generic_delete_entry deletes a directory entry by merging it
2566 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567 */
Kyoungho Koo2fe34d292020-08-10 17:07:05 +09002568int ext4_generic_delete_entry(struct inode *dir,
Tao Ma05019a92012-12-10 14:06:00 -05002569 struct ext4_dir_entry_2 *de_del,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002570 ext4_lblk_t lblk,
Tao Ma05019a92012-12-10 14:06:00 -05002571 struct buffer_head *bh,
2572 void *entry_buf,
2573 int buf_size,
2574 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002575{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002576 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002577 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002578 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002579
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002580 i = 0;
2581 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002582 de = (struct ext4_dir_entry_2 *)entry_buf;
2583 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002584 if (ext4_check_dir_entry(dir, NULL, de, bh,
Greg Kroah-Hartmane6d16012020-08-24 10:01:23 +02002585 entry_buf, buf_size, lblk, i))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002586 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002587 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002588 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002589 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002590 ext4_rec_len_from_disk(pde->rec_len,
2591 blocksize) +
2592 ext4_rec_len_from_disk(de->rec_len,
2593 blocksize),
2594 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002595 else
2596 de->inode = 0;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002597 inode_inc_iversion(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 return 0;
2599 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002600 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002601 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002602 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002603 }
2604 return -ENOENT;
2605}
2606
Tao Ma05019a92012-12-10 14:06:00 -05002607static int ext4_delete_entry(handle_t *handle,
2608 struct inode *dir,
2609 struct ext4_dir_entry_2 *de_del,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002610 ext4_lblk_t lblk,
Tao Ma05019a92012-12-10 14:06:00 -05002611 struct buffer_head *bh)
2612{
2613 int err, csum_size = 0;
2614
Tao Ma9f40fe52012-12-10 14:06:00 -05002615 if (ext4_has_inline_data(dir)) {
2616 int has_inline_data = 1;
2617 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2618 &has_inline_data);
2619 if (has_inline_data)
2620 return err;
2621 }
2622
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002623 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002624 csum_size = sizeof(struct ext4_dir_entry_tail);
2625
2626 BUFFER_TRACE(bh, "get_write_access");
2627 err = ext4_journal_get_write_access(handle, bh);
2628 if (unlikely(err))
2629 goto out;
2630
Greg Kroah-Hartmane6d16012020-08-24 10:01:23 +02002631 err = ext4_generic_delete_entry(dir, de_del, lblk, bh, bh->b_data,
Tao Ma05019a92012-12-10 14:06:00 -05002632 dir->i_sb->s_blocksize, csum_size);
2633 if (err)
2634 goto out;
2635
2636 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Theodore Ts'of036adb2019-06-21 15:49:26 -04002637 err = ext4_handle_dirty_dirblock(handle, dir, bh);
Tao Ma05019a92012-12-10 14:06:00 -05002638 if (unlikely(err))
2639 goto out;
2640
2641 return 0;
2642out:
2643 if (err != -ENOENT)
2644 ext4_std_error(dir->i_sb, err);
2645 return err;
2646}
2647
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002648/*
Andreas Dilgerc7414892017-08-05 19:47:34 -04002649 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2650 * since this indicates that nlinks count was previously 1 to avoid overflowing
2651 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2652 * that subdirectory link counts are not being maintained accurately.
2653 *
2654 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2655 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2656 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2657 * on regular files) and to avoid creating huge/slow non-HTREE directories.
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002658 */
Nikolay Borisov15ed2852020-08-26 16:31:16 +03002659static void ext4_inc_count(struct inode *inode)
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002660{
2661 inc_nlink(inode);
Andreas Dilgerc7414892017-08-05 19:47:34 -04002662 if (is_dx(inode) &&
2663 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2664 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002665}
2666
2667/*
2668 * If a directory had nlink == 1, then we should let it be 1. This indicates
2669 * directory has >EXT4_LINK_MAX subdirs.
2670 */
Nikolay Borisov15ed2852020-08-26 16:31:16 +03002671static void ext4_dec_count(struct inode *inode)
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002672{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002673 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2674 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002675}
2676
2677
Jan Kara9b88f9f2019-11-05 17:44:11 +01002678/*
2679 * Add non-directory inode to a directory. On success, the inode reference is
2680 * consumed by dentry is instantiation. This is also indicated by clearing of
2681 * *inodep pointer. On failure, the caller is responsible for dropping the
2682 * inode reference in the safe context.
2683 */
Mingming Cao617ba132006-10-11 01:20:53 -07002684static int ext4_add_nondir(handle_t *handle,
Jan Kara9b88f9f2019-11-05 17:44:11 +01002685 struct dentry *dentry, struct inode **inodep)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002686{
Jan Karaa9e26322019-11-05 17:44:10 +01002687 struct inode *dir = d_inode(dentry->d_parent);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002688 struct inode *inode = *inodep;
Mingming Cao617ba132006-10-11 01:20:53 -07002689 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002690 if (!err) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002691 err = ext4_mark_inode_dirty(handle, inode);
Jan Karaa9e26322019-11-05 17:44:10 +01002692 if (IS_DIRSYNC(dir))
2693 ext4_handle_sync(handle);
Al Viro1e2e5472018-05-04 08:23:01 -04002694 d_instantiate_new(dentry, inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002695 *inodep = NULL;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002696 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002697 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002698 drop_nlink(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002699 ext4_orphan_add(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002700 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701 return err;
2702}
2703
2704/*
2705 * By the time this is called, we already have created
2706 * the directory cache entry for the new file, but it
2707 * is so far negative - it has no inode.
2708 *
2709 * If the create succeeds, we fill in the inode information
2710 * with d_instantiate().
2711 */
Al Viro4acdaf22011-07-26 01:42:34 -04002712static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002713 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714{
2715 handle_t *handle;
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002716 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002717 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718
Jan Karaa7cdade2015-06-29 16:22:54 +02002719 err = dquot_initialize(dir);
2720 if (err)
2721 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002722
Theodore Ts'o11395752013-02-09 16:27:09 -05002723 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002724 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002725retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002726 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2727 NULL, EXT4_HT_DIR, credits);
2728 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002729 err = PTR_ERR(inode);
2730 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002731 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002732 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002733 ext4_set_aops(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002734 err = ext4_add_nondir(handle, dentry, &inode);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002735 if (!err)
2736 ext4_fc_track_create(handle, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002737 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002738 if (handle)
2739 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002740 if (!IS_ERR_OR_NULL(inode))
2741 iput(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002742 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002743 goto retry;
2744 return err;
2745}
2746
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002747static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002748 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002749{
2750 handle_t *handle;
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002751 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002752 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002753
Jan Karaa7cdade2015-06-29 16:22:54 +02002754 err = dquot_initialize(dir);
2755 if (err)
2756 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002757
Theodore Ts'o11395752013-02-09 16:27:09 -05002758 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002759 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002760retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002761 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2762 NULL, EXT4_HT_DIR, credits);
2763 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764 err = PTR_ERR(inode);
2765 if (!IS_ERR(inode)) {
2766 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002767 inode->i_op = &ext4_special_inode_operations;
Jan Kara9b88f9f2019-11-05 17:44:11 +01002768 err = ext4_add_nondir(handle, dentry, &inode);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07002769 if (!err)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002770 ext4_fc_track_create(handle, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002771 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002772 if (handle)
2773 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002774 if (!IS_ERR_OR_NULL(inode))
2775 iput(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002776 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 goto retry;
2778 return err;
2779}
2780
Al Viroaf51a2a2013-06-29 13:23:08 +04002781static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2782{
2783 handle_t *handle;
2784 struct inode *inode;
2785 int err, retries = 0;
2786
Jan Karaa7cdade2015-06-29 16:22:54 +02002787 err = dquot_initialize(dir);
2788 if (err)
2789 return err;
Al Viroaf51a2a2013-06-29 13:23:08 +04002790
2791retry:
2792 inode = ext4_new_inode_start_handle(dir, mode,
2793 NULL, 0, NULL,
2794 EXT4_HT_DIR,
2795 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2796 4 + EXT4_XATTR_TRANS_BLOCKS);
2797 handle = ext4_journal_current_handle();
2798 err = PTR_ERR(inode);
2799 if (!IS_ERR(inode)) {
2800 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002801 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002802 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002803 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002804 err = ext4_orphan_add(handle, inode);
2805 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002806 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002807 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002808 unlock_new_inode(inode);
2809 }
2810 if (handle)
2811 ext4_journal_stop(handle);
2812 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2813 goto retry;
2814 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002815err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002816 ext4_journal_stop(handle);
2817 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002818 return err;
2819}
2820
Tao Maa774f9c2012-12-10 14:05:57 -05002821struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2822 struct ext4_dir_entry_2 *de,
2823 int blocksize, int csum_size,
2824 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825{
Tao Maa774f9c2012-12-10 14:05:57 -05002826 de->inode = cpu_to_le32(inode->i_ino);
2827 de->name_len = 1;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002828 de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
Tao Maa774f9c2012-12-10 14:05:57 -05002829 blocksize);
2830 strcpy(de->name, ".");
2831 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2832
2833 de = ext4_next_entry(de, blocksize);
2834 de->inode = cpu_to_le32(parent_ino);
2835 de->name_len = 2;
2836 if (!dotdot_real_len)
2837 de->rec_len = ext4_rec_len_to_disk(blocksize -
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002838 (csum_size + ext4_dir_rec_len(1, NULL)),
Tao Maa774f9c2012-12-10 14:05:57 -05002839 blocksize);
2840 else
2841 de->rec_len = ext4_rec_len_to_disk(
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002842 ext4_dir_rec_len(de->name_len, NULL),
2843 blocksize);
Tao Maa774f9c2012-12-10 14:05:57 -05002844 strcpy(de->name, "..");
2845 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2846
2847 return ext4_next_entry(de, blocksize);
2848}
2849
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07002850int ext4_init_new_dir(handle_t *handle, struct inode *dir,
Tao Maa774f9c2012-12-10 14:05:57 -05002851 struct inode *inode)
2852{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002853 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002854 struct ext4_dir_entry_2 *de;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002855 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002856 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002857 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002858 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002859
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002860 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002861 csum_size = sizeof(struct ext4_dir_entry_tail);
2862
Tao Ma3c47d542012-12-10 14:05:59 -05002863 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2864 err = ext4_try_create_inline_dir(handle, dir, inode);
2865 if (err < 0 && err != -ENOSPC)
2866 goto out;
2867 if (!err)
2868 goto out;
2869 }
2870
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002871 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002872 dir_block = ext4_append(handle, inode, &block);
2873 if (IS_ERR(dir_block))
2874 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002875 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2876 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2877 set_nlink(inode, 2);
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002878 if (csum_size)
2879 ext4_initialize_dirent_tail(dir_block, blocksize);
Tao Maa774f9c2012-12-10 14:05:57 -05002880
2881 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
Theodore Ts'of036adb2019-06-21 15:49:26 -04002882 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002883 if (err)
2884 goto out;
2885 set_buffer_verified(dir_block);
2886out:
2887 brelse(dir_block);
2888 return err;
2889}
2890
2891static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2892{
2893 handle_t *handle;
2894 struct inode *inode;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002895 int err, err2 = 0, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002896
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002897 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002898 return -EMLINK;
2899
Jan Karaa7cdade2015-06-29 16:22:54 +02002900 err = dquot_initialize(dir);
2901 if (err)
2902 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002903
Theodore Ts'o11395752013-02-09 16:27:09 -05002904 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002905 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002907 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2908 &dentry->d_name,
2909 0, NULL, EXT4_HT_DIR, credits);
2910 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 err = PTR_ERR(inode);
2912 if (IS_ERR(inode))
2913 goto out_stop;
2914
Mingming Cao617ba132006-10-11 01:20:53 -07002915 inode->i_op = &ext4_dir_inode_operations;
2916 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002917 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002918 if (err)
2919 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002920 err = ext4_mark_inode_dirty(handle, inode);
2921 if (!err)
2922 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002923 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002924out_clear_inode:
2925 clear_nlink(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002926 ext4_orphan_add(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002927 unlock_new_inode(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002928 err2 = ext4_mark_inode_dirty(handle, inode);
2929 if (unlikely(err2))
2930 err = err2;
Jan Kara9b88f9f2019-11-05 17:44:11 +01002931 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002932 iput(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002933 goto out_retry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002934 }
Nikolay Borisov15ed2852020-08-26 16:31:16 +03002935 ext4_inc_count(dir);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07002936
Mingming Cao617ba132006-10-11 01:20:53 -07002937 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002938 err = ext4_mark_inode_dirty(handle, dir);
2939 if (err)
2940 goto out_clear_inode;
Al Viro1e2e5472018-05-04 08:23:01 -04002941 d_instantiate_new(dentry, inode);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002942 ext4_fc_track_create(handle, dentry);
Theodore Ts'o11395752013-02-09 16:27:09 -05002943 if (IS_DIRSYNC(dir))
2944 ext4_handle_sync(handle);
2945
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002946out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002947 if (handle)
2948 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002949out_retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002950 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951 goto retry;
2952 return err;
2953}
2954
2955/*
2956 * routine to check that the specified directory is empty (for rmdir)
2957 */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002958bool ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002960 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002961 struct buffer_head *bh;
Jan Kara64d4ce82019-12-02 18:02:12 +01002962 struct ext4_dir_entry_2 *de;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002963 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002964
Tao Ma61f86632012-12-10 14:06:01 -05002965 if (ext4_has_inline_data(inode)) {
2966 int has_inline_data = 1;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002967 int ret;
Tao Ma61f86632012-12-10 14:06:01 -05002968
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002969 ret = empty_inline_dir(inode, &has_inline_data);
Tao Ma61f86632012-12-10 14:06:01 -05002970 if (has_inline_data)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002971 return ret;
Tao Ma61f86632012-12-10 14:06:01 -05002972 }
2973
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974 sb = inode->i_sb;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002975 if (inode->i_size < ext4_dir_rec_len(1, NULL) +
2976 ext4_dir_rec_len(2, NULL)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002977 EXT4_ERROR_INODE(inode, "invalid size");
Ye Binb35eb482022-02-28 10:48:15 +08002978 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002979 }
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002980 /* The first directory block must not be a hole,
2981 * so treat it as DIRENT_HTREE
2982 */
2983 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002984 if (IS_ERR(bh))
Ye Binb35eb482022-02-28 10:48:15 +08002985 return false;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002986
Mingming Cao617ba132006-10-11 01:20:53 -07002987 de = (struct ext4_dir_entry_2 *) bh->b_data;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002988 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size, 0,
Jan Kara64d4ce82019-12-02 18:02:12 +01002989 0) ||
2990 le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
2991 ext4_warning_inode(inode, "directory missing '.'");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002992 brelse(bh);
Ye Binb35eb482022-02-28 10:48:15 +08002993 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002994 }
Jan Kara64d4ce82019-12-02 18:02:12 +01002995 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2996 de = ext4_next_entry(de, sb->s_blocksize);
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07002997 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size, 0,
Jan Kara64d4ce82019-12-02 18:02:12 +01002998 offset) ||
2999 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
3000 ext4_warning_inode(inode, "directory missing '..'");
3001 brelse(bh);
Ye Binb35eb482022-02-28 10:48:15 +08003002 return false;
Jan Kara64d4ce82019-12-02 18:02:12 +01003003 }
3004 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003005 while (offset < inode->i_size) {
Jan Kara64d4ce82019-12-02 18:02:12 +01003006 if (!(offset & (sb->s_blocksize - 1))) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003007 unsigned int lblock;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003008 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04003009 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003010 bh = ext4_read_dirblock(inode, lblock, EITHER);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04003011 if (bh == NULL) {
3012 offset += sb->s_blocksize;
3013 continue;
3014 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003015 if (IS_ERR(bh))
Ye Binb35eb482022-02-28 10:48:15 +08003016 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017 }
Jan Kara64d4ce82019-12-02 18:02:12 +01003018 de = (struct ext4_dir_entry_2 *) (bh->b_data +
3019 (offset & (sb->s_blocksize - 1)));
Tao Ma226ba972012-12-10 14:05:58 -05003020 if (ext4_check_dir_entry(inode, NULL, de, bh,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003021 bh->b_data, bh->b_size, 0, offset)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022 offset = (offset | (sb->s_blocksize - 1)) + 1;
3023 continue;
3024 }
3025 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003026 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003027 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003028 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05003029 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003031 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003032 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003033}
3034
Jan Karad745a8c2014-05-26 11:56:53 -04003035/*
3036 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037 * such inodes, starting at the superblock, in case we crash before the
3038 * file is closed/deleted, or in case the inode truncate spans multiple
3039 * transactions and the last transaction is not recovered after a crash.
3040 *
3041 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07003042 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04003043 *
3044 * Orphan list manipulation functions must be called under i_mutex unless
3045 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003046 */
Mingming Cao617ba132006-10-11 01:20:53 -07003047int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003048{
3049 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04003050 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07003051 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003052 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04003053 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04003055 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003056 return 0;
3057
Jan Karad745a8c2014-05-26 11:56:53 -04003058 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05003059 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04003060 /*
3061 * Exit early if inode already is on orphan list. This is a big speedup
3062 * since we don't have to contend on the global s_orphan_lock.
3063 */
Mingming Cao617ba132006-10-11 01:20:53 -07003064 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04003065 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066
Lukas Czernerafb86172011-07-11 18:47:04 -04003067 /*
3068 * Orphan handling is only valid for files with data blocks
3069 * being truncated, or files being unlinked. Note that we either
3070 * hold i_mutex, or the inode can not be referenced from outside,
3071 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003072 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003073 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
3074 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075
Jan Karacd2c0802014-05-26 11:39:17 -04003076 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
3077 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003078 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04003079 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003080
Mingming Cao617ba132006-10-11 01:20:53 -07003081 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003082 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04003083 goto out;
3084
3085 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05003086 /*
3087 * Due to previous errors inode may be already a part of on-disk
3088 * orphan list. If so skip on-disk list modification.
3089 */
Jan Karad745a8c2014-05-26 11:56:53 -04003090 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
3091 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
3092 /* Insert this inode at the head of the on-disk orphan list */
3093 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
3094 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
3095 dirty = true;
3096 }
3097 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
3098 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003099
Jan Karad745a8c2014-05-26 11:56:53 -04003100 if (dirty) {
3101 err = ext4_handle_dirty_super(handle, sb);
3102 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
3103 if (!err)
3104 err = rc;
3105 if (err) {
3106 /*
3107 * We have to remove inode from in-memory list if
3108 * addition to on disk orphan list failed. Stray orphan
3109 * list entries can cause panics at unmount time.
3110 */
3111 mutex_lock(&sbi->s_orphan_lock);
Jan Kara74177f552016-05-05 11:10:15 -04003112 list_del_init(&EXT4_I(inode)->i_orphan);
Jan Karad745a8c2014-05-26 11:56:53 -04003113 mutex_unlock(&sbi->s_orphan_lock);
3114 }
Vasily Averinfeaf2642018-11-06 17:01:36 -05003115 } else
3116 brelse(iloc.bh);
3117
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003118 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
3119 jbd_debug(4, "orphan inode %lu will point to %d\n",
3120 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04003121out:
Jan Karacd2c0802014-05-26 11:39:17 -04003122 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003123 return err;
3124}
3125
3126/*
Mingming Cao617ba132006-10-11 01:20:53 -07003127 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128 * of such inodes stored on disk, because it is finally being cleaned up.
3129 */
Mingming Cao617ba132006-10-11 01:20:53 -07003130int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003131{
3132 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07003133 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04003134 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003135 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07003136 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003137 int err = 0;
3138
Jan Karacd2c0802014-05-26 11:39:17 -04003139 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05003140 return 0;
3141
Jan Karad745a8c2014-05-26 11:56:53 -04003142 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05003143 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04003144 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04003145 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04003146 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003147
Jan Karad745a8c2014-05-26 11:56:53 -04003148 if (handle) {
3149 /* Grab inode buffer early before taking global s_orphan_lock */
3150 err = ext4_reserve_inode_write(handle, inode, &iloc);
3151 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152
Jan Karad745a8c2014-05-26 11:56:53 -04003153 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003154 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
3155
Jan Karad745a8c2014-05-26 11:56:53 -04003156 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157 list_del_init(&ei->i_orphan);
3158
3159 /* If we're on an error path, we may not have a valid
3160 * transaction handle with which to update the orphan list on
3161 * disk, but we still need to remove the inode from the linked
3162 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04003163 if (!handle || err) {
3164 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04003166 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167
Jan Karad745a8c2014-05-26 11:56:53 -04003168 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003169 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003170 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003172 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003173 if (err) {
3174 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003175 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003176 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003177 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04003178 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04003179 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003180 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003181 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003182 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07003183 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003185 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003186 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07003187 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003188 if (err) {
3189 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003190 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003191 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003192 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07003193 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003194 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 }
3196 if (err)
3197 goto out_brelse;
3198 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003199 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003200out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07003201 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003202 return err;
3203
3204out_brelse:
3205 brelse(iloc.bh);
3206 goto out_err;
3207}
3208
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003209static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003210{
3211 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003212 struct inode *inode;
3213 struct buffer_head *bh;
3214 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003215 handle_t *handle = NULL;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003216 ext4_lblk_t lblk;
3217
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003218
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003219 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3220 return -EIO;
3221
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003222 /* Initialize quotas before so that eventual writes go in
3223 * separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003224 retval = dquot_initialize(dir);
3225 if (retval)
3226 return retval;
3227 retval = dquot_initialize(d_inode(dentry));
3228 if (retval)
3229 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003230
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003231 retval = -ENOENT;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003232 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL, &lblk);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003233 if (IS_ERR(bh))
3234 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235 if (!bh)
3236 goto end_rmdir;
3237
David Howells2b0143b2015-03-17 22:25:59 +00003238 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003240 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003241 if (le32_to_cpu(de->inode) != inode->i_ino)
3242 goto end_rmdir;
3243
3244 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003245 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003246 goto end_rmdir;
3247
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003248 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003249 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003250 if (IS_ERR(handle)) {
3251 retval = PTR_ERR(handle);
3252 handle = NULL;
3253 goto end_rmdir;
3254 }
3255
3256 if (IS_DIRSYNC(dir))
3257 ext4_handle_sync(handle);
3258
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003259 retval = ext4_delete_entry(handle, dir, de, lblk, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260 if (retval)
3261 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003262 if (!EXT4_DIR_LINK_EMPTY(inode))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003263 ext4_warning_inode(inode,
3264 "empty directory '%.*s' has too many links (%u)",
3265 dentry->d_name.len, dentry->d_name.name,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003266 inode->i_nlink);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05003267 inode_inc_iversion(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003268 clear_nlink(inode);
3269 /* There's no need to set i_disksize: the fact that i_nlink is
3270 * zero will ensure that the right thing happens during any
3271 * recovery. */
3272 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003273 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003274 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003275 retval = ext4_mark_inode_dirty(handle, inode);
3276 if (retval)
3277 goto end_rmdir;
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003278 ext4_dec_count(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003279 ext4_update_dx_flag(dir);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003280 ext4_fc_track_unlink(handle, dentry);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003281 retval = ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003282
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003283#ifdef CONFIG_UNICODE
3284 /* VFS negative dentries are incompatible with Encoding and
3285 * Case-insensitiveness. Eventually we'll want avoid
3286 * invalidating the dentries here, alongside with returning the
3287 * negative dentries at ext4_lookup(), when it is better
3288 * supported by the VFS for the CI case.
3289 */
3290 if (IS_CASEFOLDED(dir))
3291 d_invalidate(dentry);
3292#endif
3293
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003295 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003296 if (handle)
3297 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298 return retval;
3299}
3300
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003301int __ext4_unlink(handle_t *handle, struct inode *dir, const struct qstr *d_name,
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003302 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303{
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003304 int retval = -ENOENT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003305 struct buffer_head *bh;
3306 struct ext4_dir_entry_2 *de;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003307 int skip_remove_dentry = 0;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003308 ext4_lblk_t lblk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309
Greg Kroah-Hartman862e6672020-10-26 13:38:50 +01003310 bh = ext4_find_entry(dir, d_name, &de, NULL, &lblk);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003311 if (IS_ERR(bh))
3312 return PTR_ERR(bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003313
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003314 if (!bh)
3315 return -ENOENT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003316
Yi Zhuange5f78152020-06-29 20:26:21 +08003317 if (le32_to_cpu(de->inode) != inode->i_ino) {
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003318 /*
3319 * It's okay if we find dont find dentry which matches
3320 * the inode. That's because it might have gotten
3321 * renamed to a different inode number
3322 */
3323 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
3324 skip_remove_dentry = 1;
3325 else
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003326 goto out;
Theodore Ts'o931b6862013-02-09 09:43:39 -05003327 }
3328
3329 if (IS_DIRSYNC(dir))
3330 ext4_handle_sync(handle);
3331
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003332 if (!skip_remove_dentry) {
Greg Kroah-Hartman862e6672020-10-26 13:38:50 +01003333 retval = ext4_delete_entry(handle, dir, de, lblk, bh);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003334 if (retval)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003335 goto out;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003336 dir->i_ctime = dir->i_mtime = current_time(dir);
3337 ext4_update_dx_flag(dir);
3338 retval = ext4_mark_inode_dirty(handle, dir);
3339 if (retval)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003340 goto out;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003341 } else {
3342 retval = 0;
3343 }
Theodore Ts'oc7df4a12019-11-11 22:18:13 -05003344 if (inode->i_nlink == 0)
3345 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003346 d_name->len, d_name->name);
Theodore Ts'oc7df4a12019-11-11 22:18:13 -05003347 else
3348 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003349 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003350 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003351 inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003352 retval = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003353
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003354out:
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003355 brelse(bh);
3356 return retval;
3357}
3358
3359static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3360{
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003361 handle_t *handle;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003362 int retval;
3363
3364 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3365 return -EIO;
3366
3367 trace_ext4_unlink_enter(dir, dentry);
3368 /*
3369 * Initialize quotas before so that eventual writes go
3370 * in separate transaction
3371 */
3372 retval = dquot_initialize(dir);
3373 if (retval)
3374 goto out_trace;
3375 retval = dquot_initialize(d_inode(dentry));
3376 if (retval)
3377 goto out_trace;
3378
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003379 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3380 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3381 if (IS_ERR(handle)) {
3382 retval = PTR_ERR(handle);
3383 goto out_trace;
3384 }
3385
3386 retval = __ext4_unlink(handle, dir, &dentry->d_name, d_inode(dentry));
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07003387 if (!retval)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003388 ext4_fc_track_unlink(handle, dentry);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003389#ifdef CONFIG_UNICODE
3390 /* VFS negative dentries are incompatible with Encoding and
3391 * Case-insensitiveness. Eventually we'll want avoid
3392 * invalidating the dentries here, alongside with returning the
3393 * negative dentries at ext4_lookup(), when it is better
3394 * supported by the VFS for the CI case.
3395 */
3396 if (IS_CASEFOLDED(dir))
3397 d_invalidate(dentry);
3398#endif
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003399 if (handle)
3400 ext4_journal_stop(handle);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003401
Yi Zhuange5f78152020-06-29 20:26:21 +08003402out_trace:
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003403 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003404 return retval;
3405}
3406
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003407static int ext4_symlink(struct inode *dir,
3408 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003409{
3410 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003411 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003412 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003413 int credits;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003414 struct fscrypt_str disk_link;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003415
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003416 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3417 return -EIO;
3418
Eric Biggers78e10602018-01-11 22:10:40 -05003419 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3420 &disk_link);
3421 if (err)
3422 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003423
Jan Karaa7cdade2015-06-29 16:22:54 +02003424 err = dquot_initialize(dir);
3425 if (err)
Eric Biggers78e10602018-01-11 22:10:40 -05003426 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003427
Theodore Ts'of348c252015-04-16 01:55:00 -04003428 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003429 /*
3430 * For non-fast symlinks, we just allocate inode and put it on
3431 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003432 * group descriptor, sb, inode block, quota blocks, and
3433 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003434 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003435 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3436 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003437 } else {
3438 /*
3439 * Fast symlink. We have to add entry to directory
3440 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3441 * allocate new inode (bitmap, group descriptor, inode block,
3442 * quota blocks, sb is already counted in previous macros).
3443 */
3444 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003445 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003446 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003447
Theodore Ts'o11395752013-02-09 16:27:09 -05003448 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3449 &dentry->d_name, 0, NULL,
3450 EXT4_HT_DIR, credits);
3451 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003452 if (IS_ERR(inode)) {
3453 if (handle)
3454 ext4_journal_stop(handle);
Eric Biggers78e10602018-01-11 22:10:40 -05003455 return PTR_ERR(inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003456 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003457
Eric Biggers78e10602018-01-11 22:10:40 -05003458 if (IS_ENCRYPTED(inode)) {
3459 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04003460 if (err)
Theodore Ts'of348c252015-04-16 01:55:00 -04003461 goto err_drop_inode;
Al Viroa7a67e82015-04-27 17:51:30 -04003462 inode->i_op = &ext4_encrypted_symlink_inode_operations;
Theodore Ts'of348c252015-04-16 01:55:00 -04003463 }
3464
3465 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Eric Biggers78e10602018-01-11 22:10:40 -05003466 if (!IS_ENCRYPTED(inode))
Al Viroa7a67e82015-04-27 17:51:30 -04003467 inode->i_op = &ext4_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003468 inode_nohighmem(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003469 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003470 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003471 * We cannot call page_symlink() with transaction started
3472 * because it calls into ext4_write_begin() which can wait
3473 * for transaction commit if we are running out of space
3474 * and thus we deadlock. So we have to stop transaction now
3475 * and restart it when symlink contents is written.
3476 *
3477 * To keep fs consistent in case of crash, we have to put inode
3478 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003479 */
Jan Karadf5e6222011-05-03 11:12:58 -04003480 drop_nlink(inode);
3481 err = ext4_orphan_add(handle, inode);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003482 if (handle)
3483 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003484 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003485 if (err)
3486 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003487 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003488 if (err)
3489 goto err_drop_inode;
3490 /*
3491 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3492 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3493 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003494 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003495 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3496 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3497 if (IS_ERR(handle)) {
3498 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003499 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003500 goto err_drop_inode;
3501 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003502 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003503 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003504 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003505 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003506 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003507 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003508 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Eric Biggers78e10602018-01-11 22:10:40 -05003509 if (!IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04003510 inode->i_op = &ext4_fast_symlink_inode_operations;
Al Viro75e75662015-05-02 10:13:58 -04003511 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3512 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003513 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3514 disk_link.len);
3515 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003516 }
Mingming Cao617ba132006-10-11 01:20:53 -07003517 EXT4_I(inode)->i_disksize = inode->i_size;
Jan Kara9b88f9f2019-11-05 17:44:11 +01003518 err = ext4_add_nondir(handle, dentry, &inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003519 if (handle)
3520 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01003521 if (inode)
3522 iput(inode);
Eric Biggers78e10602018-01-11 22:10:40 -05003523 goto out_free_encrypted_link;
3524
Jan Karadf5e6222011-05-03 11:12:58 -04003525err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003526 if (handle)
3527 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003528 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003529 unlock_new_inode(inode);
3530 iput(inode);
Eric Biggers78e10602018-01-11 22:10:40 -05003531out_free_encrypted_link:
3532 if (disk_link.name != (unsigned char *)symname)
3533 kfree(disk_link.name);
Jan Karadf5e6222011-05-03 11:12:58 -04003534 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003535}
3536
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003537int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538{
3539 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003540 int err, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003541retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003542 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3543 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003544 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003545 if (IS_ERR(handle))
3546 return PTR_ERR(handle);
3547
3548 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003549 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003550
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003551 inode->i_ctime = current_time(inode);
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003552 ext4_inc_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003553 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003554
Al Viro6b38e842008-12-30 02:03:31 -05003555 err = ext4_add_entry(handle, dentry, inode);
3556 if (!err) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003557 err = ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003558 /* this can happen only for tmpfile being
3559 * linked the first time
3560 */
3561 if (inode->i_nlink == 1)
3562 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003563 d_instantiate(dentry, inode);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003564 ext4_fc_track_link(handle, dentry);
Al Viro6b38e842008-12-30 02:03:31 -05003565 } else {
3566 drop_nlink(inode);
3567 iput(inode);
3568 }
Mingming Cao617ba132006-10-11 01:20:53 -07003569 ext4_journal_stop(handle);
3570 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003571 goto retry;
3572 return err;
3573}
3574
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003575static int ext4_link(struct dentry *old_dentry,
3576 struct inode *dir, struct dentry *dentry)
3577{
3578 struct inode *inode = d_inode(old_dentry);
3579 int err;
3580
3581 if (inode->i_nlink >= EXT4_LINK_MAX)
3582 return -EMLINK;
3583
3584 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3585 if (err)
3586 return err;
3587
3588 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3589 (!projid_eq(EXT4_I(dir)->i_projid,
3590 EXT4_I(old_dentry->d_inode)->i_projid)))
3591 return -EXDEV;
3592
3593 err = dquot_initialize(dir);
3594 if (err)
3595 return err;
3596 return __ext4_link(dir, inode, dentry);
3597}
Tao Ma32f7f222012-12-10 14:06:01 -05003598
3599/*
3600 * Try to find buffer head where contains the parent block.
3601 * It should be the inode block if it is inlined or the 1st block
3602 * if it is a normal dir.
3603 */
3604static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3605 struct inode *inode,
3606 int *retval,
3607 struct ext4_dir_entry_2 **parent_de,
3608 int *inlined)
3609{
3610 struct buffer_head *bh;
3611
3612 if (!ext4_has_inline_data(inode)) {
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04003613 /* The first directory block must not be a hole, so
3614 * treat it as DIRENT_HTREE
3615 */
3616 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003617 if (IS_ERR(bh)) {
3618 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003619 return NULL;
3620 }
3621 *parent_de = ext4_next_entry(
3622 (struct ext4_dir_entry_2 *)bh->b_data,
3623 inode->i_sb->s_blocksize);
3624 return bh;
3625 }
3626
3627 *inlined = 1;
3628 return ext4_get_first_inline_block(inode, parent_de, retval);
3629}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003630
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003631struct ext4_renament {
3632 struct inode *dir;
3633 struct dentry *dentry;
3634 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003635 bool is_dir;
3636 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003637
3638 /* entry for "dentry" */
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003639 ext4_lblk_t lblk;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003640 struct buffer_head *bh;
3641 struct ext4_dir_entry_2 *de;
3642 int inlined;
3643
3644 /* entry for ".." in inode if it's a directory */
3645 struct buffer_head *dir_bh;
3646 struct ext4_dir_entry_2 *parent_de;
3647 int dir_inlined;
3648};
3649
Miklos Szeredibd1af142014-04-01 17:08:44 +02003650static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3651{
3652 int retval;
3653
3654 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3655 &retval, &ent->parent_de,
3656 &ent->dir_inlined);
3657 if (!ent->dir_bh)
3658 return retval;
3659 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003660 return -EFSCORRUPTED;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003661 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3662 return ext4_journal_get_write_access(handle, ent->dir_bh);
3663}
3664
3665static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3666 unsigned dir_ino)
3667{
3668 int retval;
3669
3670 ent->parent_de->inode = cpu_to_le32(dir_ino);
3671 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3672 if (!ent->dir_inlined) {
3673 if (is_dx(ent->inode)) {
3674 retval = ext4_handle_dirty_dx_node(handle,
3675 ent->inode,
3676 ent->dir_bh);
3677 } else {
Theodore Ts'of036adb2019-06-21 15:49:26 -04003678 retval = ext4_handle_dirty_dirblock(handle, ent->inode,
3679 ent->dir_bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003680 }
3681 } else {
3682 retval = ext4_mark_inode_dirty(handle, ent->inode);
3683 }
3684 if (retval) {
3685 ext4_std_error(ent->dir->i_sb, retval);
3686 return retval;
3687 }
3688 return 0;
3689}
3690
3691static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3692 unsigned ino, unsigned file_type)
3693{
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003694 int retval, retval2;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003695
3696 BUFFER_TRACE(ent->bh, "get write access");
3697 retval = ext4_journal_get_write_access(handle, ent->bh);
3698 if (retval)
3699 return retval;
3700 ent->de->inode = cpu_to_le32(ino);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003701 if (ext4_has_feature_filetype(ent->dir->i_sb))
Miklos Szeredibd1af142014-04-01 17:08:44 +02003702 ent->de->file_type = file_type;
Jeff Laytonee73f9a2018-01-09 08:21:39 -05003703 inode_inc_iversion(ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003704 ent->dir->i_ctime = ent->dir->i_mtime =
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003705 current_time(ent->dir);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003706 retval = ext4_mark_inode_dirty(handle, ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003707 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3708 if (!ent->inlined) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003709 retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
3710 if (unlikely(retval2)) {
3711 ext4_std_error(ent->dir->i_sb, retval2);
3712 return retval2;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003713 }
3714 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003715 return retval;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003716}
3717
zhangyi (F)258db8e2021-03-03 21:17:02 +08003718static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
3719 unsigned ino, unsigned file_type)
3720{
3721 struct ext4_renament old = *ent;
3722 int retval = 0;
3723
3724 /*
3725 * old->de could have moved from under us during make indexed dir,
3726 * so the old->de may no longer valid and need to find it again
3727 * before reset old inode info.
3728 */
Greg Kroah-Hartmand5992d52021-03-22 09:25:40 +01003729 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL,
3730 NULL);
zhangyi (F)258db8e2021-03-03 21:17:02 +08003731 if (IS_ERR(old.bh))
3732 retval = PTR_ERR(old.bh);
3733 if (!old.bh)
3734 retval = -ENOENT;
3735 if (retval) {
3736 ext4_std_error(old.dir->i_sb, retval);
3737 return;
3738 }
3739
3740 ext4_setent(handle, &old, ino, file_type);
3741 brelse(old.bh);
3742}
3743
Miklos Szeredibd1af142014-04-01 17:08:44 +02003744static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3745 const struct qstr *d_name)
3746{
3747 int retval = -ENOENT;
3748 struct buffer_head *bh;
3749 struct ext4_dir_entry_2 *de;
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003750 ext4_lblk_t lblk;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003751
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003752 bh = ext4_find_entry(dir, d_name, &de, NULL, &lblk);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003753 if (IS_ERR(bh))
3754 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003755 if (bh) {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003756 retval = ext4_delete_entry(handle, dir, de, lblk, bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003757 brelse(bh);
3758 }
3759 return retval;
3760}
3761
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003762static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3763 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003764{
3765 int retval;
3766 /*
3767 * ent->de could have moved from under us during htree split, so make
3768 * sure that we are deleting the right entry. We might also be pointing
3769 * to a stale entry in the unused part of ent->bh so just checking inum
3770 * and the name isn't enough.
3771 */
3772 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3773 ent->de->name_len != ent->dentry->d_name.len ||
3774 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003775 ent->de->name_len) ||
3776 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003777 retval = ext4_find_delete_entry(handle, ent->dir,
3778 &ent->dentry->d_name);
3779 } else {
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003780 retval = ext4_delete_entry(handle, ent->dir, ent->de,
3781 ent->lblk, ent->bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003782 if (retval == -ENOENT) {
3783 retval = ext4_find_delete_entry(handle, ent->dir,
3784 &ent->dentry->d_name);
3785 }
3786 }
3787
3788 if (retval) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003789 ext4_warning_inode(ent->dir,
3790 "Deleting old file: nlink %d, error=%d",
3791 ent->dir->i_nlink, retval);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003792 }
3793}
3794
Miklos Szeredibd429982014-04-01 17:08:44 +02003795static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3796{
3797 if (ent->dir_nlink_delta) {
3798 if (ent->dir_nlink_delta == -1)
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003799 ext4_dec_count(ent->dir);
Miklos Szeredibd429982014-04-01 17:08:44 +02003800 else
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003801 ext4_inc_count(ent->dir);
Miklos Szeredibd429982014-04-01 17:08:44 +02003802 ext4_mark_inode_dirty(handle, ent->dir);
3803 }
3804}
3805
Miklos Szeredicd808de2014-10-24 00:14:37 +02003806static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3807 int credits, handle_t **h)
3808{
3809 struct inode *wh;
3810 handle_t *handle;
3811 int retries = 0;
3812
3813 /*
3814 * for inode block, sb block, group summaries,
3815 * and inode bitmap
3816 */
3817 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3818 EXT4_XATTR_TRANS_BLOCKS + 4);
3819retry:
3820 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3821 &ent->dentry->d_name, 0, NULL,
3822 EXT4_HT_DIR, credits);
3823
3824 handle = ext4_journal_current_handle();
3825 if (IS_ERR(wh)) {
3826 if (handle)
3827 ext4_journal_stop(handle);
3828 if (PTR_ERR(wh) == -ENOSPC &&
3829 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3830 goto retry;
3831 } else {
3832 *h = handle;
3833 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3834 wh->i_op = &ext4_special_inode_operations;
3835 }
3836 return wh;
3837}
3838
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003839/*
3840 * Anybody can rename anything with this: the permission checks are left to the
3841 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003842 *
3843 * n.b. old_{dentry,inode) refers to the source dentry/inode
3844 * while new_{dentry,inode) refers to the destination dentry/inode
3845 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003846 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003847static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003848 struct inode *new_dir, struct dentry *new_dentry,
3849 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003850{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003851 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003852 struct ext4_renament old = {
3853 .dir = old_dir,
3854 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003855 .inode = d_inode(old_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003856 };
3857 struct ext4_renament new = {
3858 .dir = new_dir,
3859 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003860 .inode = d_inode(new_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003861 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003862 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003863 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003864 struct inode *whiteout = NULL;
3865 int credits;
3866 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003867
Theodore Ts'ob50282f2018-08-27 01:47:09 -04003868 if (new.inode && new.inode->i_nlink == 0) {
3869 EXT4_ERROR_INODE(new.inode,
3870 "target of rename is already freed");
3871 return -EFSCORRUPTED;
3872 }
3873
Li Xi040cb372016-01-08 16:01:21 -05003874 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3875 (!projid_eq(EXT4_I(new_dir)->i_projid,
3876 EXT4_I(old_dentry->d_inode)->i_projid)))
3877 return -EXDEV;
3878
Jan Karaa7cdade2015-06-29 16:22:54 +02003879 retval = dquot_initialize(old.dir);
3880 if (retval)
3881 return retval;
3882 retval = dquot_initialize(new.dir);
3883 if (retval)
3884 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003885
3886 /* Initialize quotas before so that eventual writes go
3887 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003888 if (new.inode) {
3889 retval = dquot_initialize(new.inode);
3890 if (retval)
3891 return retval;
3892 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003894 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL,
3895 &old.lblk);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003896 if (IS_ERR(old.bh))
3897 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003898 /*
3899 * Check for inode number is _not_ due to possible IO errors.
3900 * We might rmdir the source, keep it as pwd of some process
3901 * and merrily kill the link to whatever was created under the
3902 * same name. Goodbye sticky bit ;-<
3903 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003904 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003905 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
zhangyi (F)5038c112021-03-03 21:17:03 +08003906 goto release_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003907
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003908 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07003909 &new.de, &new.inlined, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003910 if (IS_ERR(new.bh)) {
3911 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003912 new.bh = NULL;
zhangyi (F)5038c112021-03-03 21:17:03 +08003913 goto release_bh;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003914 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003915 if (new.bh) {
3916 if (!new.inode) {
3917 brelse(new.bh);
3918 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003919 }
3920 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003921 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3922 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003923
Miklos Szeredicd808de2014-10-24 00:14:37 +02003924 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3925 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3926 if (!(flags & RENAME_WHITEOUT)) {
3927 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003928 if (IS_ERR(handle)) {
3929 retval = PTR_ERR(handle);
zhangyi (F)5038c112021-03-03 21:17:03 +08003930 goto release_bh;
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003931 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003932 } else {
3933 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003934 if (IS_ERR(whiteout)) {
3935 retval = PTR_ERR(whiteout);
zhangyi (F)5038c112021-03-03 21:17:03 +08003936 goto release_bh;
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003937 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003938 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003939
yangerkun2207c3c2021-01-05 14:28:57 +08003940 old_file_type = old.de->file_type;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003941 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003942 ext4_handle_sync(handle);
3943
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003944 if (S_ISDIR(old.inode->i_mode)) {
3945 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003947 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003948 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003949 } else {
3950 retval = -EMLINK;
3951 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3952 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003953 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003954 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003955 if (retval)
3956 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003957 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003958 /*
3959 * If we're renaming a file within an inline_data dir and adding or
3960 * setting the new dirent causes a conversion from inline_data to
3961 * extents/blockmap, we need to force the dirent delete code to
3962 * re-read the directory, or else we end up trying to delete a dirent
3963 * from what is now the extent tree root (or a block map).
3964 */
3965 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3966 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003967
Miklos Szeredicd808de2014-10-24 00:14:37 +02003968 if (whiteout) {
3969 /*
3970 * Do this before adding a new entry, so the old entry is sure
3971 * to be still pointing to the valid old entry.
3972 */
3973 retval = ext4_setent(handle, &old, whiteout->i_ino,
3974 EXT4_FT_CHRDEV);
3975 if (retval)
3976 goto end_rename;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003977 retval = ext4_mark_inode_dirty(handle, whiteout);
3978 if (unlikely(retval))
3979 goto end_rename;
Harshad Shirwadkar35ecf662021-03-16 15:19:21 -07003980
Miklos Szeredicd808de2014-10-24 00:14:37 +02003981 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003982 if (!new.bh) {
3983 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984 if (retval)
3985 goto end_rename;
3986 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003987 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003988 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003989 if (retval)
3990 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003991 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003992 if (force_reread)
3993 force_reread = !ext4_test_inode_flag(new.dir,
3994 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003995
3996 /*
3997 * Like most other Unix systems, set the ctime for inodes on a
3998 * rename.
3999 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004000 old.inode->i_ctime = current_time(old.inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004001 retval = ext4_mark_inode_dirty(handle, old.inode);
4002 if (unlikely(retval))
4003 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004004
Miklos Szeredicd808de2014-10-24 00:14:37 +02004005 if (!whiteout) {
4006 /*
4007 * ok, that's it
4008 */
4009 ext4_rename_delete(handle, &old, force_reread);
4010 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004011
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004012 if (new.inode) {
Nikolay Borisov15ed2852020-08-26 16:31:16 +03004013 ext4_dec_count(new.inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004014 new.inode->i_ctime = current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004015 }
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004016 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004017 ext4_update_dx_flag(old.dir);
4018 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02004019 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4020 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05004021 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02004022
Nikolay Borisov15ed2852020-08-26 16:31:16 +03004023 ext4_dec_count(old.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004024 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04004025 /* checked ext4_empty_dir above, can't have another
4026 * parent, ext4_dec_count() won't work for many-linked
4027 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004028 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004029 } else {
Nikolay Borisov15ed2852020-08-26 16:31:16 +03004030 ext4_inc_count(new.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004031 ext4_update_dx_flag(new.dir);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004032 retval = ext4_mark_inode_dirty(handle, new.dir);
4033 if (unlikely(retval))
4034 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004035 }
4036 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004037 retval = ext4_mark_inode_dirty(handle, old.dir);
4038 if (unlikely(retval))
4039 goto end_rename;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004040
4041 if (S_ISDIR(old.inode->i_mode)) {
4042 /*
4043 * We disable fast commits here that's because the
4044 * replay code is not yet capable of changing dot dot
4045 * dirents in directories.
4046 */
4047 ext4_fc_mark_ineligible(old.inode->i_sb,
4048 EXT4_FC_REASON_RENAME_DIR);
4049 } else {
4050 if (new.inode)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08004051 ext4_fc_track_unlink(handle, new.dentry);
4052 __ext4_fc_track_link(handle, old.inode, new.dentry);
4053 __ext4_fc_track_unlink(handle, old.inode, old.dentry);
Harshad Shirwadkar35ecf662021-03-16 15:19:21 -07004054 if (whiteout)
4055 __ext4_fc_track_create(handle, whiteout, old.dentry);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004056 }
4057
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004058 if (new.inode) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004059 retval = ext4_mark_inode_dirty(handle, new.inode);
4060 if (unlikely(retval))
4061 goto end_rename;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004062 if (!new.inode->i_nlink)
4063 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004064 }
4065 retval = 0;
4066
4067end_rename:
yangerkun2207c3c2021-01-05 14:28:57 +08004068 if (whiteout) {
4069 if (retval) {
zhangyi (F)258db8e2021-03-03 21:17:02 +08004070 ext4_resetent(handle, &old,
4071 old.inode->i_ino, old_file_type);
yangerkun2207c3c2021-01-05 14:28:57 +08004072 drop_nlink(whiteout);
zhangyi (F)5038c112021-03-03 21:17:03 +08004073 ext4_orphan_add(handle, whiteout);
yangerkun2207c3c2021-01-05 14:28:57 +08004074 }
4075 unlock_new_inode(whiteout);
zhangyi (F)5038c112021-03-03 21:17:03 +08004076 ext4_journal_stop(handle);
yangerkun2207c3c2021-01-05 14:28:57 +08004077 iput(whiteout);
zhangyi (F)5038c112021-03-03 21:17:03 +08004078 } else {
4079 ext4_journal_stop(handle);
yangerkun2207c3c2021-01-05 14:28:57 +08004080 }
zhangyi (F)5038c112021-03-03 21:17:03 +08004081release_bh:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004082 brelse(old.dir_bh);
4083 brelse(old.bh);
4084 brelse(new.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004085 return retval;
4086}
4087
Miklos Szeredibd429982014-04-01 17:08:44 +02004088static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
4089 struct inode *new_dir, struct dentry *new_dentry)
4090{
4091 handle_t *handle = NULL;
4092 struct ext4_renament old = {
4093 .dir = old_dir,
4094 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00004095 .inode = d_inode(old_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02004096 };
4097 struct ext4_renament new = {
4098 .dir = new_dir,
4099 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00004100 .inode = d_inode(new_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02004101 };
4102 u8 new_file_type;
4103 int retval;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004104 struct timespec64 ctime;
Miklos Szeredibd429982014-04-01 17:08:44 +02004105
Li Xi040cb372016-01-08 16:01:21 -05004106 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
4107 !projid_eq(EXT4_I(new_dir)->i_projid,
4108 EXT4_I(old_dentry->d_inode)->i_projid)) ||
4109 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
4110 !projid_eq(EXT4_I(old_dir)->i_projid,
4111 EXT4_I(new_dentry->d_inode)->i_projid)))
4112 return -EXDEV;
4113
Jan Karaa7cdade2015-06-29 16:22:54 +02004114 retval = dquot_initialize(old.dir);
4115 if (retval)
4116 return retval;
4117 retval = dquot_initialize(new.dir);
4118 if (retval)
4119 return retval;
Miklos Szeredibd429982014-04-01 17:08:44 +02004120
4121 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07004122 &old.de, &old.inlined, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04004123 if (IS_ERR(old.bh))
4124 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02004125 /*
4126 * Check for inode number is _not_ due to possible IO errors.
4127 * We might rmdir the source, keep it as pwd of some process
4128 * and merrily kill the link to whatever was created under the
4129 * same name. Goodbye sticky bit ;-<
4130 */
4131 retval = -ENOENT;
4132 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
4133 goto end_rename;
4134
4135 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
Daniel Rosenberg705a3e52019-10-07 18:12:23 -07004136 &new.de, &new.inlined, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04004137 if (IS_ERR(new.bh)) {
4138 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04004139 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04004140 goto end_rename;
4141 }
Miklos Szeredibd429982014-04-01 17:08:44 +02004142
4143 /* RENAME_EXCHANGE case: old *and* new must both exist */
4144 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
4145 goto end_rename;
4146
4147 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
4148 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
4149 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04004150 if (IS_ERR(handle)) {
4151 retval = PTR_ERR(handle);
4152 handle = NULL;
4153 goto end_rename;
4154 }
Miklos Szeredibd429982014-04-01 17:08:44 +02004155
4156 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
4157 ext4_handle_sync(handle);
4158
4159 if (S_ISDIR(old.inode->i_mode)) {
4160 old.is_dir = true;
4161 retval = ext4_rename_dir_prepare(handle, &old);
4162 if (retval)
4163 goto end_rename;
4164 }
4165 if (S_ISDIR(new.inode->i_mode)) {
4166 new.is_dir = true;
4167 retval = ext4_rename_dir_prepare(handle, &new);
4168 if (retval)
4169 goto end_rename;
4170 }
4171
4172 /*
4173 * Other than the special case of overwriting a directory, parents'
4174 * nlink only needs to be modified if this is a cross directory rename.
4175 */
4176 if (old.dir != new.dir && old.is_dir != new.is_dir) {
4177 old.dir_nlink_delta = old.is_dir ? -1 : 1;
4178 new.dir_nlink_delta = -old.dir_nlink_delta;
4179 retval = -EMLINK;
4180 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
4181 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
4182 goto end_rename;
4183 }
4184
4185 new_file_type = new.de->file_type;
4186 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
4187 if (retval)
4188 goto end_rename;
4189
4190 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
4191 if (retval)
4192 goto end_rename;
4193
4194 /*
4195 * Like most other Unix systems, set the ctime for inodes on a
4196 * rename.
4197 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004198 ctime = current_time(old.inode);
4199 old.inode->i_ctime = ctime;
4200 new.inode->i_ctime = ctime;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004201 retval = ext4_mark_inode_dirty(handle, old.inode);
4202 if (unlikely(retval))
4203 goto end_rename;
4204 retval = ext4_mark_inode_dirty(handle, new.inode);
4205 if (unlikely(retval))
4206 goto end_rename;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004207 ext4_fc_mark_ineligible(new.inode->i_sb,
4208 EXT4_FC_REASON_CROSS_RENAME);
Miklos Szeredibd429982014-04-01 17:08:44 +02004209 if (old.dir_bh) {
4210 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4211 if (retval)
4212 goto end_rename;
4213 }
4214 if (new.dir_bh) {
4215 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
4216 if (retval)
4217 goto end_rename;
4218 }
4219 ext4_update_dir_count(handle, &old);
4220 ext4_update_dir_count(handle, &new);
4221 retval = 0;
4222
4223end_rename:
4224 brelse(old.dir_bh);
4225 brelse(new.dir_bh);
4226 brelse(old.bh);
4227 brelse(new.bh);
4228 if (handle)
4229 ext4_journal_stop(handle);
4230 return retval;
4231}
4232
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004233static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
4234 struct inode *new_dir, struct dentry *new_dentry,
4235 unsigned int flags)
4236{
Eric Biggers07543d12017-10-18 20:21:57 -04004237 int err;
4238
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05004239 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
4240 return -EIO;
4241
Miklos Szeredicd808de2014-10-24 00:14:37 +02004242 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004243 return -EINVAL;
4244
Eric Biggers07543d12017-10-18 20:21:57 -04004245 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
4246 flags);
4247 if (err)
4248 return err;
4249
Miklos Szeredibd429982014-04-01 17:08:44 +02004250 if (flags & RENAME_EXCHANGE) {
4251 return ext4_cross_rename(old_dir, old_dentry,
4252 new_dir, new_dentry);
4253 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02004254
4255 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004256}
4257
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004258/*
4259 * directories can handle most operations...
4260 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08004261const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07004262 .create = ext4_create,
4263 .lookup = ext4_lookup,
4264 .link = ext4_link,
4265 .unlink = ext4_unlink,
4266 .symlink = ext4_symlink,
4267 .mkdir = ext4_mkdir,
4268 .rmdir = ext4_rmdir,
4269 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04004270 .tmpfile = ext4_tmpfile,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004271 .rename = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07004272 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01004273 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07004274 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02004275 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004276 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04004277 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004278};
4279
Arjan van de Ven754661f2007-02-12 00:55:38 -08004280const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07004281 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01004282 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07004283 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02004284 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004285 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004286};