blob: f2e9cb71763d2b4b5b1086ff9916ae873b681702 [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
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185#ifdef DX_DEBUG
186#define dxtrace(command) command
187#else
188#define dxtrace(command)
189#endif
190
191struct fake_dirent
192{
193 __le32 inode;
194 __le16 rec_len;
195 u8 name_len;
196 u8 file_type;
197};
198
199struct dx_countlimit
200{
201 __le16 limit;
202 __le16 count;
203};
204
205struct dx_entry
206{
207 __le32 hash;
208 __le32 block;
209};
210
211/*
212 * dx_root_info is laid out so that if it should somehow get overlaid by a
213 * dirent the two low bits of the hash version will be zero. Therefore, the
214 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
215 */
216
217struct dx_root
218{
219 struct fake_dirent dot;
220 char dot_name[4];
221 struct fake_dirent dotdot;
222 char dotdot_name[4];
223 struct dx_root_info
224 {
225 __le32 reserved_zero;
226 u8 hash_version;
227 u8 info_length; /* 8 */
228 u8 indirect_levels;
229 u8 unused_flags;
230 }
231 info;
Gustavo A. R. Silva6cfb0612020-02-13 10:06:48 -0600232 struct dx_entry entries[];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233};
234
235struct dx_node
236{
237 struct fake_dirent fake;
Gustavo A. R. Silva6cfb0612020-02-13 10:06:48 -0600238 struct dx_entry entries[];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239};
240
241
242struct dx_frame
243{
244 struct buffer_head *bh;
245 struct dx_entry *entries;
246 struct dx_entry *at;
247};
248
249struct dx_map_entry
250{
251 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700252 u16 offs;
253 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254};
255
Darrick J. Wonge6153912012-04-29 18:23:10 -0400256/*
257 * This goes at the end of each htree block.
258 */
259struct dx_tail {
260 u32 dt_reserved;
261 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
262};
263
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500264static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
265static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400266static inline unsigned dx_get_hash(struct dx_entry *entry);
267static void dx_set_hash(struct dx_entry *entry, unsigned value);
268static unsigned dx_get_count(struct dx_entry *entries);
269static unsigned dx_get_limit(struct dx_entry *entries);
270static void dx_set_count(struct dx_entry *entries, unsigned value);
271static void dx_set_limit(struct dx_entry *entries, unsigned value);
272static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
273static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400274static struct dx_frame *dx_probe(struct ext4_filename *fname,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 struct inode *dir,
276 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400277 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400278static void dx_release(struct dx_frame *frames);
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400279static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
280 unsigned blocksize, struct dx_hash_info *hinfo,
281 struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000283static struct ext4_dir_entry_2 *dx_move_dirents(struct inode *dir, char *from,
284 char *to, struct dx_map_entry *offsets,
285 int count, unsigned int blocksize);
286static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
287 unsigned int blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500288static void dx_insert_block(struct dx_frame *frame,
289 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700290static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 struct dx_frame *frame,
292 struct dx_frame *frames,
293 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400294static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400295 struct ext4_filename *fname,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400296 struct ext4_dir_entry_2 **res_dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400297static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -0500298 struct inode *dir, struct inode *inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400300/* checksumming functions */
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400301void ext4_initialize_dirent_tail(struct buffer_head *bh,
302 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400303{
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400304 struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
305
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400306 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
307 t->det_rec_len = ext4_rec_len_to_disk(
308 sizeof(struct ext4_dir_entry_tail), blocksize);
309 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
310}
311
312/* Walk through a dirent block to find a checksum "dirent" at the tail */
313static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
Theodore Ts'of036adb2019-06-21 15:49:26 -0400314 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400315{
316 struct ext4_dir_entry_tail *t;
317
318#ifdef PARANOID
319 struct ext4_dir_entry *d, *top;
320
Theodore Ts'of036adb2019-06-21 15:49:26 -0400321 d = (struct ext4_dir_entry *)bh->b_data;
322 top = (struct ext4_dir_entry *)(bh->b_data +
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400323 (EXT4_BLOCK_SIZE(inode->i_sb) -
Theodore Ts'of036adb2019-06-21 15:49:26 -0400324 sizeof(struct ext4_dir_entry_tail)));
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400325 while (d < top && d->rec_len)
326 d = (struct ext4_dir_entry *)(((void *)d) +
327 le16_to_cpu(d->rec_len));
328
329 if (d != top)
330 return NULL;
331
332 t = (struct ext4_dir_entry_tail *)d;
333#else
Theodore Ts'of036adb2019-06-21 15:49:26 -0400334 t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400335#endif
336
337 if (t->det_reserved_zero1 ||
338 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
339 t->det_reserved_zero2 ||
340 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
341 return NULL;
342
343 return t;
344}
345
Theodore Ts'of036adb2019-06-21 15:49:26 -0400346static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400347{
348 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
349 struct ext4_inode_info *ei = EXT4_I(inode);
350 __u32 csum;
351
352 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
353 return cpu_to_le32(csum);
354}
355
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400356#define warn_no_space_for_csum(inode) \
357 __warn_no_space_for_csum((inode), __func__, __LINE__)
358
359static void __warn_no_space_for_csum(struct inode *inode, const char *func,
360 unsigned int line)
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500361{
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400362 __ext4_warning_inode(inode, func, line,
363 "No space for directory leaf checksum. Please run e2fsck -D.");
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500364}
365
Theodore Ts'of036adb2019-06-21 15:49:26 -0400366int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400367{
368 struct ext4_dir_entry_tail *t;
369
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400370 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400371 return 1;
372
Theodore Ts'of036adb2019-06-21 15:49:26 -0400373 t = get_dirent_tail(inode, bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400374 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500375 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400376 return 0;
377 }
378
Theodore Ts'of036adb2019-06-21 15:49:26 -0400379 if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400380 (char *)t - bh->b_data))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400381 return 0;
382
383 return 1;
384}
385
Theodore Ts'of036adb2019-06-21 15:49:26 -0400386static void ext4_dirblock_csum_set(struct inode *inode,
387 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400388{
389 struct ext4_dir_entry_tail *t;
390
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400391 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400392 return;
393
Theodore Ts'of036adb2019-06-21 15:49:26 -0400394 t = get_dirent_tail(inode, bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400395 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500396 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400397 return;
398 }
399
Theodore Ts'of036adb2019-06-21 15:49:26 -0400400 t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
Theodore Ts'oddce3b92019-06-21 16:31:47 -0400401 (char *)t - bh->b_data);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400402}
403
Theodore Ts'of036adb2019-06-21 15:49:26 -0400404int ext4_handle_dirty_dirblock(handle_t *handle,
405 struct inode *inode,
406 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400407{
Theodore Ts'of036adb2019-06-21 15:49:26 -0400408 ext4_dirblock_csum_set(inode, bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400409 return ext4_handle_dirty_metadata(handle, inode, bh);
410}
411
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400412static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
413 struct ext4_dir_entry *dirent,
414 int *offset)
415{
416 struct ext4_dir_entry *dp;
417 struct dx_root_info *root;
418 int count_offset;
419
420 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
421 count_offset = 8;
422 else if (le16_to_cpu(dirent->rec_len) == 12) {
423 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
424 if (le16_to_cpu(dp->rec_len) !=
425 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
426 return NULL;
427 root = (struct dx_root_info *)(((void *)dp + 12));
428 if (root->reserved_zero ||
429 root->info_length != sizeof(struct dx_root_info))
430 return NULL;
431 count_offset = 32;
432 } else
433 return NULL;
434
435 if (offset)
436 *offset = count_offset;
437 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
438}
439
440static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
441 int count_offset, int count, struct dx_tail *t)
442{
443 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
444 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400445 __u32 csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400446 int size;
Daeho Jeongb47820e2016-07-03 17:51:39 -0400447 __u32 dummy_csum = 0;
448 int offset = offsetof(struct dx_tail, dt_checksum);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400449
450 size = count_offset + (count * sizeof(struct dx_entry));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400451 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400452 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
453 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400454
455 return cpu_to_le32(csum);
456}
457
458static int ext4_dx_csum_verify(struct inode *inode,
459 struct ext4_dir_entry *dirent)
460{
461 struct dx_countlimit *c;
462 struct dx_tail *t;
463 int count_offset, limit, count;
464
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400465 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400466 return 1;
467
468 c = get_dx_countlimit(inode, dirent, &count_offset);
469 if (!c) {
470 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
Daeho Jeongfa964542016-07-03 21:11:08 -0400471 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400472 }
473 limit = le16_to_cpu(c->limit);
474 count = le16_to_cpu(c->count);
475 if (count_offset + (limit * sizeof(struct dx_entry)) >
476 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500477 warn_no_space_for_csum(inode);
Daeho Jeongfa964542016-07-03 21:11:08 -0400478 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400479 }
480 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
481
482 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
483 count, t))
484 return 0;
485 return 1;
486}
487
488static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
489{
490 struct dx_countlimit *c;
491 struct dx_tail *t;
492 int count_offset, limit, count;
493
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400494 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400495 return;
496
497 c = get_dx_countlimit(inode, dirent, &count_offset);
498 if (!c) {
499 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
500 return;
501 }
502 limit = le16_to_cpu(c->limit);
503 count = le16_to_cpu(c->count);
504 if (count_offset + (limit * sizeof(struct dx_entry)) >
505 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500506 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400507 return;
508 }
509 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
510
511 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
512}
513
514static inline int ext4_handle_dirty_dx_node(handle_t *handle,
515 struct inode *inode,
516 struct buffer_head *bh)
517{
518 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
519 return ext4_handle_dirty_metadata(handle, inode, bh);
520}
521
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522/*
Li Zefanf795e142008-07-11 19:27:31 -0400523 * p is at least 6 bytes before the end of page
524 */
525static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500526ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400527{
528 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500529 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400530}
531
532/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700533 * Future: use high four bits of block for coalesce-on-delete flags
534 * Mask them off for now.
535 */
536
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500537static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400539 return le32_to_cpu(entry->block) & 0x0fffffff;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540}
541
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500542static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543{
544 entry->block = cpu_to_le32(value);
545}
546
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400547static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548{
549 return le32_to_cpu(entry->hash);
550}
551
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400552static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553{
554 entry->hash = cpu_to_le32(value);
555}
556
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400557static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558{
559 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
560}
561
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400562static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563{
564 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
565}
566
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400567static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568{
569 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
570}
571
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400572static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573{
574 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
575}
576
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400577static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578{
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000579 unsigned int entry_space = dir->i_sb->s_blocksize -
580 ext4_dir_rec_len(1, NULL) -
581 ext4_dir_rec_len(2, NULL) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400582
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400583 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400584 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400585 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586}
587
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400588static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589{
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000590 unsigned int entry_space = dir->i_sb->s_blocksize -
591 ext4_dir_rec_len(0, dir);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400592
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400593 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400594 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400595 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596}
597
598/*
599 * Debug
600 */
601#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400602static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603{
Andrew Morton63f57932006-10-11 01:21:24 -0700604 int i, n = dx_get_count (entries);
Joe Perchesd74f3d22016-10-15 09:57:31 -0400605 printk(KERN_DEBUG "%s index", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700606 for (i = 0; i < n; i++) {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400607 printk(KERN_CONT " %x->%lu",
608 i ? dx_get_hash(entries + i) : 0,
609 (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700610 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400611 printk(KERN_CONT "\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612}
613
614struct stats
615{
616 unsigned names;
617 unsigned space;
618 unsigned bcount;
619};
620
Michael Halcrowb3098482015-04-12 01:07:01 -0400621static struct stats dx_show_leaf(struct inode *dir,
622 struct dx_hash_info *hinfo,
623 struct ext4_dir_entry_2 *de,
624 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625{
626 unsigned names = 0, space = 0;
627 char *base = (char *) de;
628 struct dx_hash_info h = *hinfo;
629
630 printk("names: ");
631 while ((char *) de < base + size)
632 {
633 if (de->inode)
634 {
635 if (show_names)
636 {
Chandan Rajendra643fa962018-12-12 15:20:12 +0530637#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrowb3098482015-04-12 01:07:01 -0400638 int len;
639 char *name;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400640 struct fscrypt_str fname_crypto_str =
641 FSTR_INIT(NULL, 0);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400642 int res = 0;
Michael Halcrowb3098482015-04-12 01:07:01 -0400643
644 name = de->name;
645 len = de->name_len;
Eric Biggers91d0d892020-12-02 18:20:36 -0800646 if (!IS_ENCRYPTED(dir)) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400647 /* Directory is not encrypted */
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400648 ext4fs_dirhash(dir, de->name,
Michael Halcrowb3098482015-04-12 01:07:01 -0400649 de->name_len, &h);
650 printk("%*.s:(U)%x.%u ", len,
651 name, h.hash,
652 (unsigned) ((char *) de
653 - base));
654 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400655 struct fscrypt_str de_name =
656 FSTR_INIT(name, len);
657
Michael Halcrowb3098482015-04-12 01:07:01 -0400658 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400659 res = fscrypt_fname_alloc_buffer(
Jeff Layton8b10fe62020-08-10 10:21:39 -0400660 len, &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400661 if (res)
Michael Halcrowb3098482015-04-12 01:07:01 -0400662 printk(KERN_WARNING "Error "
663 "allocating crypto "
664 "buffer--skipping "
665 "crypto\n");
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400666 res = fscrypt_fname_disk_to_usr(dir,
667 0, 0, &de_name,
668 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400669 if (res) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400670 printk(KERN_WARNING "Error "
671 "converting filename "
672 "from disk to usr"
673 "\n");
674 name = "??";
675 len = 2;
676 } else {
677 name = fname_crypto_str.name;
678 len = fname_crypto_str.len;
679 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000680 if (IS_CASEFOLDED(dir))
681 h.hash = EXT4_DIRENT_HASH(de);
682 else
683 ext4fs_dirhash(dir, de->name,
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400684 de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400685 printk("%*.s:(E)%x.%u ", len, name,
686 h.hash, (unsigned) ((char *) de
687 - base));
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400688 fscrypt_fname_free_buffer(
689 &fname_crypto_str);
Michael Halcrowb3098482015-04-12 01:07:01 -0400690 }
691#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 int len = de->name_len;
693 char *name = de->name;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400694 ext4fs_dirhash(dir, de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400695 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400696 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400697#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000699 space += ext4_dir_rec_len(de->name_len, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 names++;
701 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500702 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400704 printk(KERN_CONT "(%i)\n", names);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 return (struct stats) { names, space, 1 };
706}
707
708struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
709 struct dx_entry *entries, int levels)
710{
711 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400712 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 unsigned bcount = 0;
714 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 printk("%i indexed blocks...\n", count);
716 for (i = 0; i < count; i++, entries++)
717 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500718 ext4_lblk_t block = dx_get_block(entries);
719 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
721 struct stats stats;
722 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400723 bh = ext4_bread(NULL,dir, block, 0);
724 if (!bh || IS_ERR(bh))
725 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 stats = levels?
727 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400728 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
729 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 names += stats.names;
731 space += stats.space;
732 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400733 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 }
735 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400736 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400737 levels ? "" : " ", names, space/bcount,
738 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 return (struct stats) { names, space, bcount};
740}
Vinicius Tintic6c818e2021-02-02 16:28:37 +0000741
742/*
743 * Linear search cross check
744 */
745static inline void htree_rep_invariant_check(struct dx_entry *at,
746 struct dx_entry *target,
747 u32 hash, unsigned int n)
748{
749 while (n--) {
750 dxtrace(printk(KERN_CONT ","));
751 if (dx_get_hash(++at) > hash) {
752 at--;
753 break;
754 }
755 }
756 ASSERT(at == target - 1);
757}
758#else /* DX_DEBUG */
759static inline void htree_rep_invariant_check(struct dx_entry *at,
760 struct dx_entry *target,
761 u32 hash, unsigned int n)
762{
763}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764#endif /* DX_DEBUG */
765
766/*
767 * Probe for a directory leaf block to search.
768 *
769 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
770 * error in the directory index, and the caller should fall back to
771 * searching the directory normally. The callers of dx_probe **MUST**
772 * check for this error code, and make sure it never gets reflected
773 * back to userspace.
774 */
775static struct dx_frame *
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400776dx_probe(struct ext4_filename *fname, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400777 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778{
779 unsigned count, indirect;
780 struct dx_entry *at, *entries, *p, *q, *m;
781 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400783 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784 u32 hash;
785
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400786 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400787 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
788 if (IS_ERR(frame->bh))
789 return (struct dx_frame *) frame->bh;
790
791 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 if (root->info.hash_version != DX_HASH_TEA &&
793 root->info.hash_version != DX_HASH_HALF_MD4 &&
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000794 root->info.hash_version != DX_HASH_LEGACY &&
795 root->info.hash_version != DX_HASH_SIPHASH) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400796 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
797 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 goto fail;
799 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +0000800 if (ext4_hash_in_dirent(dir)) {
801 if (root->info.hash_version != DX_HASH_SIPHASH) {
802 ext4_warning_inode(dir,
803 "Hash in dirent, but hash is not SIPHASH");
804 goto fail;
805 }
806 } else {
807 if (root->info.hash_version == DX_HASH_SIPHASH) {
808 ext4_warning_inode(dir,
809 "Hash code is SIPHASH, but hash not in dirent");
810 goto fail;
811 }
812 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400813 if (fname)
814 hinfo = &fname->hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400816 if (hinfo->hash_version <= DX_HASH_TEA)
817 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700818 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +0000819 /* hash is already computed for encrypted casefolded directory */
820 if (fname && fname_name(fname) &&
821 !(IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)))
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400822 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823 hash = hinfo->hash;
824
825 if (root->info.unused_flags & 1) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400826 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
827 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700828 goto fail;
829 }
830
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400831 indirect = root->info.indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400832 if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
833 ext4_warning(dir->i_sb,
834 "Directory (ino: %lu) htree depth %#06x exceed"
835 "supported value", dir->i_ino,
836 ext4_dir_htree_level(dir->i_sb));
837 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
838 ext4_warning(dir->i_sb, "Enable large directory "
839 "feature to access it");
840 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 goto fail;
842 }
843
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400844 entries = (struct dx_entry *)(((char *)&root->info) +
845 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700846
847 if (dx_get_limit(entries) != dx_root_limit(dir,
848 root->info.info_length)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400849 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
850 dx_get_limit(entries),
851 dx_root_limit(dir, root->info.info_length));
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700852 goto fail;
853 }
854
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400855 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400856 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700858 if (!count || count > dx_get_limit(entries)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400859 ext4_warning_inode(dir,
860 "dx entry: count %u beyond limit %u",
861 count, dx_get_limit(entries));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400862 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700863 }
864
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 p = entries + 1;
866 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400867 while (p <= q) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400868 m = p + (q - p) / 2;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400869 dxtrace(printk(KERN_CONT "."));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 if (dx_get_hash(m) > hash)
871 q = m - 1;
872 else
873 p = m + 1;
874 }
875
Vinicius Tintic6c818e2021-02-02 16:28:37 +0000876 htree_rep_invariant_check(entries, p, hash, count - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877
878 at = p - 1;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400879 dxtrace(printk(KERN_CONT " %x->%u\n",
880 at == entries ? 0 : dx_get_hash(at),
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400881 dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 frame->entries = entries;
883 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400884 if (!indirect--)
885 return frame;
886 frame++;
887 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
888 if (IS_ERR(frame->bh)) {
889 ret_err = (struct dx_frame *) frame->bh;
890 frame->bh = NULL;
891 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400892 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400893 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400894
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400895 if (dx_get_limit(entries) != dx_node_limit(dir)) {
896 ext4_warning_inode(dir,
897 "dx entry: limit %u != node limit %u",
898 dx_get_limit(entries), dx_node_limit(dir));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400899 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700900 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400902fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700903 while (frame >= frame_in) {
904 brelse(frame->bh);
905 frame--;
906 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400907
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400908 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400909 ext4_warning_inode(dir,
910 "Corrupt directory, running e2fsck is recommended");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400911 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912}
913
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400914static void dx_release(struct dx_frame *frames)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400916 struct dx_root_info *info;
917 int i;
Sahitya Tummala08fc98a2019-05-10 22:00:33 -0400918 unsigned int indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400919
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920 if (frames[0].bh == NULL)
921 return;
922
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400923 info = &((struct dx_root *)frames[0].bh->b_data)->info;
Sahitya Tummala08fc98a2019-05-10 22:00:33 -0400924 /* save local copy, "info" may be freed after brelse() */
925 indirect_levels = info->indirect_levels;
926 for (i = 0; i <= indirect_levels; i++) {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400927 if (frames[i].bh == NULL)
928 break;
929 brelse(frames[i].bh);
930 frames[i].bh = NULL;
931 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932}
933
934/*
935 * This function increments the frame pointer to search the next leaf
936 * block, and reads in the necessary intervening nodes if the search
937 * should be necessary. Whether or not the search is necessary is
938 * controlled by the hash parameter. If the hash value is even, then
939 * the search is only continued if the next block starts with that
940 * hash value. This is used if we are searching for a specific file.
941 *
942 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
943 *
944 * This function returns 1 if the caller should continue to search,
945 * or 0 if it should not. If there is an error reading one of the
946 * index blocks, it will a negative error code.
947 *
948 * If start_hash is non-null, it will be filled in with the starting
949 * hash of the next page.
950 */
Mingming Cao617ba132006-10-11 01:20:53 -0700951static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 struct dx_frame *frame,
953 struct dx_frame *frames,
954 __u32 *start_hash)
955{
956 struct dx_frame *p;
957 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500958 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 __u32 bhash;
960
961 p = frame;
962 /*
963 * Find the next leaf page by incrementing the frame pointer.
964 * If we run out of entries in the interior node, loop around and
965 * increment pointer in the parent node. When we break out of
966 * this loop, num_frames indicates the number of interior
967 * nodes need to be read.
968 */
969 while (1) {
970 if (++(p->at) < p->entries + dx_get_count(p->entries))
971 break;
972 if (p == frames)
973 return 0;
974 num_frames++;
975 p--;
976 }
977
978 /*
979 * If the hash is 1, then continue only if the next page has a
980 * continuation hash of any value. This is used for readdir
981 * handling. Otherwise, check to see if the hash matches the
982 * desired contiuation hash. If it doesn't, return since
983 * there's no point to read in the successive index pages.
984 */
985 bhash = dx_get_hash(p->at);
986 if (start_hash)
987 *start_hash = bhash;
988 if ((hash & 1) == 0) {
989 if ((bhash & ~1) != hash)
990 return 0;
991 }
992 /*
993 * If the hash is HASH_NB_ALWAYS, we always go to the next
994 * block so no check is necessary
995 */
996 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500997 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
998 if (IS_ERR(bh))
999 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001001 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 p->bh = bh;
1003 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1004 }
1005 return 1;
1006}
1007
1008
1009/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 * This function fills a red-black tree with information from a
1011 * directory block. It returns the number directory entries loaded
1012 * into the tree. If there is an error it is returned in err.
1013 */
1014static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001015 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 struct dx_hash_info *hinfo,
1017 __u32 start_hash, __u32 start_minor_hash)
1018{
1019 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001020 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -04001021 int err = 0, count = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001022 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001023 int csum = ext4_has_metadata_csum(dir->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001025 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
1026 (unsigned long)block));
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04001027 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001028 if (IS_ERR(bh))
1029 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001030
Mingming Cao617ba132006-10-11 01:20:53 -07001031 de = (struct ext4_dir_entry_2 *) bh->b_data;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001032 /* csum entries are not larger in the casefolded encrypted case */
Mingming Cao617ba132006-10-11 01:20:53 -07001033 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 dir->i_sb->s_blocksize -
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001035 ext4_dir_rec_len(0,
1036 csum ? NULL : dir));
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001037 /* Check if the directory is encrypted */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301038 if (IS_ENCRYPTED(dir)) {
Eric Biggersec0caa972020-12-02 18:20:37 -08001039 err = fscrypt_prepare_readdir(dir);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -04001040 if (err < 0) {
1041 brelse(bh);
1042 return err;
1043 }
Jeff Layton8b10fe62020-08-10 10:21:39 -04001044 err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
1045 &fname_crypto_str);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001046 if (err < 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001047 brelse(bh);
1048 return err;
1049 }
1050 }
Eric Biggers64c314f2019-12-09 13:32:25 -08001051
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001052 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -05001053 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -05001054 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -05001055 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1056 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001057 /* silently ignore the rest of the block */
1058 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001059 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001060 if (ext4_hash_in_dirent(dir)) {
1061 if (de->name_len && de->inode) {
1062 hinfo->hash = EXT4_DIRENT_HASH(de);
1063 hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1064 } else {
1065 hinfo->hash = 0;
1066 hinfo->minor_hash = 0;
1067 }
1068 } else {
1069 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1070 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 if ((hinfo->hash < start_hash) ||
1072 ((hinfo->hash == start_hash) &&
1073 (hinfo->minor_hash < start_minor_hash)))
1074 continue;
1075 if (de->inode == 0)
1076 continue;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301077 if (!IS_ENCRYPTED(dir)) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001078 tmp_str.name = de->name;
1079 tmp_str.len = de->name_len;
1080 err = ext4_htree_store_dirent(dir_file,
1081 hinfo->hash, hinfo->minor_hash, de,
1082 &tmp_str);
1083 } else {
Theodore Ts'od2299592015-05-18 13:15:47 -04001084 int save_len = fname_crypto_str.len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001085 struct fscrypt_str de_name = FSTR_INIT(de->name,
1086 de->name_len);
Theodore Ts'od2299592015-05-18 13:15:47 -04001087
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001088 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001089 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1090 hinfo->minor_hash, &de_name,
1091 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001092 if (err) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001093 count = err;
1094 goto errout;
1095 }
1096 err = ext4_htree_store_dirent(dir_file,
1097 hinfo->hash, hinfo->minor_hash, de,
1098 &fname_crypto_str);
Theodore Ts'od2299592015-05-18 13:15:47 -04001099 fname_crypto_str.len = save_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001100 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001101 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001102 count = err;
1103 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104 }
1105 count++;
1106 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001107errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001109 fscrypt_fname_free_buffer(&fname_crypto_str);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 return count;
1111}
1112
1113
1114/*
1115 * This function fills a red-black tree with information from a
1116 * directory. We start scanning the directory in hash order, starting
1117 * at start_hash and start_minor_hash.
1118 *
1119 * This function returns the number of entries inserted into the tree,
1120 * or a negative error code.
1121 */
Mingming Cao617ba132006-10-11 01:20:53 -07001122int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 __u32 start_minor_hash, __u32 *next_hash)
1124{
1125 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001126 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001127 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001129 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001131 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 __u32 hashval;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001133 struct fscrypt_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134
Theodore Ts'o60e66792010-05-17 07:00:00 -04001135 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001136 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001137 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001138 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001139 if (ext4_hash_in_dirent(dir))
1140 hinfo.hash_version = DX_HASH_SIPHASH;
1141 else
1142 hinfo.hash_version =
1143 EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001144 if (hinfo.hash_version <= DX_HASH_TEA)
1145 hinfo.hash_version +=
1146 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001147 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001148 if (ext4_has_inline_data(dir)) {
1149 int has_inline_data = 1;
Theodore Ts'o7633b082019-06-21 21:57:00 -04001150 count = ext4_inlinedir_to_tree(dir_file, dir, 0,
1151 &hinfo, start_hash,
1152 start_minor_hash,
1153 &has_inline_data);
Tao Ma8af0f082013-04-19 17:53:09 -04001154 if (has_inline_data) {
1155 *next_hash = ~0;
1156 return count;
1157 }
1158 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1160 start_hash, start_minor_hash);
1161 *next_hash = ~0;
1162 return count;
1163 }
1164 hinfo.hash = start_hash;
1165 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001166 frame = dx_probe(NULL, dir, &hinfo, frames);
1167 if (IS_ERR(frame))
1168 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169
1170 /* Add '.' and '..' from the htree header */
1171 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001172 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001173 tmp_str.name = de->name;
1174 tmp_str.len = de->name_len;
1175 err = ext4_htree_store_dirent(dir_file, 0, 0,
1176 de, &tmp_str);
1177 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 goto errout;
1179 count++;
1180 }
1181 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001182 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001183 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001184 tmp_str.name = de->name;
1185 tmp_str.len = de->name_len;
1186 err = ext4_htree_store_dirent(dir_file, 2, 0,
1187 de, &tmp_str);
1188 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189 goto errout;
1190 count++;
1191 }
1192
1193 while (1) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -04001194 if (fatal_signal_pending(current)) {
1195 err = -ERESTARTSYS;
1196 goto errout;
1197 }
1198 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 block = dx_get_block(frame->at);
1200 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1201 start_hash, start_minor_hash);
1202 if (ret < 0) {
1203 err = ret;
1204 goto errout;
1205 }
1206 count += ret;
1207 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001208 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 frame, frames, &hashval);
1210 *next_hash = hashval;
1211 if (ret < 0) {
1212 err = ret;
1213 goto errout;
1214 }
1215 /*
1216 * Stop if: (a) there are no more entries, or
1217 * (b) we have inserted at least one entry and the
1218 * next hash value is not a continuation
1219 */
1220 if ((ret == 0) ||
1221 (count && ((hashval & 1) == 0)))
1222 break;
1223 }
1224 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001225 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1226 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001227 return count;
1228errout:
1229 dx_release(frames);
1230 return (err);
1231}
1232
Tao Ma7335cd32012-12-10 14:05:59 -05001233static inline int search_dirblock(struct buffer_head *bh,
1234 struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001235 struct ext4_filename *fname,
Tao Ma7335cd32012-12-10 14:05:59 -05001236 unsigned int offset,
1237 struct ext4_dir_entry_2 **res_dir)
1238{
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001239 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
Eric Biggersd6b97552017-05-24 18:10:49 -04001240 fname, offset, res_dir);
Tao Ma7335cd32012-12-10 14:05:59 -05001241}
1242
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243/*
1244 * Directory block splitting, compacting
1245 */
1246
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001247/*
1248 * Create map of hash values, offsets, and sizes, stored at end of block.
1249 * Returns number of entries mapped.
1250 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001251static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1252 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001253 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254{
1255 int count = 0;
1256 char *base = (char *) de;
1257 struct dx_hash_info h = *hinfo;
1258
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001259 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260 if (de->name_len && de->inode) {
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001261 if (ext4_hash_in_dirent(dir))
1262 h.hash = EXT4_DIRENT_HASH(de);
1263 else
1264 ext4fs_dirhash(dir, de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001265 map_tail--;
1266 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001267 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001268 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 count++;
1270 cond_resched();
1271 }
1272 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001273 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 }
1275 return count;
1276}
1277
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001278/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001279static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1280{
Andrew Morton63f57932006-10-11 01:21:24 -07001281 struct dx_map_entry *p, *q, *top = map + count - 1;
1282 int more;
1283 /* Combsort until bubble sort doesn't suck */
1284 while (count > 2) {
1285 count = count*10/13;
1286 if (count - 9 < 2) /* 9, 10 -> 11 */
1287 count = 11;
1288 for (p = top, q = p - count; q >= map; p--, q--)
1289 if (p->hash < q->hash)
1290 swap(*p, *q);
1291 }
1292 /* Garden variety bubble sort */
1293 do {
1294 more = 0;
1295 q = top;
1296 while (q-- > map) {
1297 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001299 swap(*(q+1), *q);
1300 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301 }
1302 } while(more);
1303}
1304
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001305static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306{
1307 struct dx_entry *entries = frame->entries;
1308 struct dx_entry *old = frame->at, *new = old + 1;
1309 int count = dx_get_count(entries);
1310
Chunguang Xu837c23f2020-11-07 23:58:11 +08001311 ASSERT(count < dx_get_limit(entries));
1312 ASSERT(old < entries + count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1314 dx_set_hash(new, hash);
1315 dx_set_block(new, block);
1316 dx_set_count(entries, count + 1);
1317}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001319#ifdef CONFIG_UNICODE
1320/*
1321 * Test whether a case-insensitive directory entry matches the filename
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001322 * being searched for. If quick is set, assume the name being looked up
1323 * is already in the casefolded form.
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001324 *
1325 * Returns: 0 if the directory entry matches, more than 0 if it
1326 * doesn't match or less than zero on error.
1327 */
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001328static int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
1329 u8 *de_name, size_t de_name_len, bool quick)
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001330{
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00001331 const struct super_block *sb = parent->i_sb;
1332 const struct unicode_map *um = sb->s_encoding;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001333 struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
1334 struct qstr entry = QSTR_INIT(de_name, de_name_len);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001335 int ret;
1336
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001337 if (IS_ENCRYPTED(parent)) {
1338 const struct fscrypt_str encrypted_name =
1339 FSTR_INIT(de_name, de_name_len);
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001340
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001341 decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
1342 if (!decrypted_name.name)
1343 return -ENOMEM;
1344 ret = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
1345 &decrypted_name);
1346 if (ret < 0)
1347 goto out;
1348 entry.name = decrypted_name.name;
1349 entry.len = decrypted_name.len;
1350 }
1351
1352 if (quick)
1353 ret = utf8_strncasecmp_folded(um, name, &entry);
1354 else
1355 ret = utf8_strncasecmp(um, name, &entry);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001356 if (ret < 0) {
1357 /* Handle invalid character sequence as either an error
1358 * or as an opaque byte sequence.
1359 */
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00001360 if (sb_has_strict_encoding(sb))
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001361 ret = -EINVAL;
1362 else if (name->len != entry.len)
1363 ret = 1;
1364 else
1365 ret = !!memcmp(name->name, entry.name, entry.len);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001366 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001367out:
1368 kfree(decrypted_name.name);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001369 return ret;
1370}
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001371
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001372int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1373 struct ext4_filename *name)
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001374{
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001375 struct fscrypt_str *cf_name = &name->cf_name;
1376 struct dx_hash_info *hinfo = &name->hinfo;
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001377 int len;
1378
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00001379 if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding) {
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001380 cf_name->name = NULL;
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001381 return 0;
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001382 }
1383
1384 cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
1385 if (!cf_name->name)
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001386 return -ENOMEM;
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001387
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00001388 len = utf8_casefold(dir->i_sb->s_encoding,
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001389 iname, cf_name->name,
1390 EXT4_NAME_LEN);
1391 if (len <= 0) {
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001392 kfree(cf_name->name);
1393 cf_name->name = NULL;
1394 }
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001395 cf_name->len = (unsigned) len;
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001396 if (!IS_ENCRYPTED(dir))
1397 return 0;
Gabriel Krisman Bertazi96fcaf82019-07-02 17:53:22 -04001398
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001399 hinfo->hash_version = DX_HASH_SIPHASH;
1400 hinfo->seed = NULL;
1401 if (cf_name->name)
1402 ext4fs_dirhash(dir, cf_name->name, cf_name->len, hinfo);
1403 else
1404 ext4fs_dirhash(dir, iname->name, iname->len, hinfo);
1405 return 0;
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001406}
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001407#endif
1408
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409/*
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001410 * Test whether a directory entry matches the filename being searched for.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 *
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001412 * Return: %true if the directory entry matches, otherwise %false.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413 */
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001414static bool ext4_match(struct inode *parent,
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001415 const struct ext4_filename *fname,
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001416 struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417{
Eric Biggers067d1022017-04-24 10:00:11 -07001418 struct fscrypt_name f;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001419
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 if (!de->inode)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001421 return false;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001422
Eric Biggers067d1022017-04-24 10:00:11 -07001423 f.usr_fname = fname->usr_fname;
1424 f.disk_name = fname->disk_name;
Chandan Rajendra643fa962018-12-12 15:20:12 +05301425#ifdef CONFIG_FS_ENCRYPTION
Eric Biggers067d1022017-04-24 10:00:11 -07001426 f.crypto_buf = fname->crypto_buf;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001427#endif
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001428
1429#ifdef CONFIG_UNICODE
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00001430 if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) {
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001431 if (fname->cf_name.name) {
1432 struct qstr cf = {.name = fname->cf_name.name,
1433 .len = fname->cf_name.len};
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001434 if (IS_ENCRYPTED(parent)) {
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001435 if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
1436 fname->hinfo.minor_hash !=
1437 EXT4_DIRENT_MINOR_HASH(de)) {
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001438
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001439 return 0;
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00001440 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001441 }
1442 return !ext4_ci_compare(parent, &cf, de->name,
1443 de->name_len, true);
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001444 }
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001445 return !ext4_ci_compare(parent, fname->usr_fname, de->name,
1446 de->name_len, false);
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001447 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001448#endif
1449
Eric Biggers067d1022017-04-24 10:00:11 -07001450 return fscrypt_match_name(&f, de->name, de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451}
1452
1453/*
1454 * Returns 0 if not found, -1 on failure, and 1 on success
1455 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001456int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1457 struct inode *dir, struct ext4_filename *fname,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001458 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459{
Mingming Cao617ba132006-10-11 01:20:53 -07001460 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 char * dlimit;
1462 int de_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001463
Tao Ma7335cd32012-12-10 14:05:59 -05001464 de = (struct ext4_dir_entry_2 *)search_buf;
1465 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 while ((char *) de < dlimit) {
1467 /* this code is executed quadratically often */
1468 /* do minimal checking `by hand' */
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001469 if ((char *) de + de->name_len <= dlimit &&
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001470 ext4_match(dir, fname, de)) {
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001471 /* found a match - just to be sure, do
1472 * a full check */
Jan Kara7303cb52020-07-31 18:21:35 +02001473 if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
1474 buf_size, offset))
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001475 return -1;
1476 *res_dir = de;
1477 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001478 }
1479 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001480 de_len = ext4_rec_len_from_disk(de->rec_len,
1481 dir->i_sb->s_blocksize);
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001482 if (de_len <= 0)
1483 return -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001485 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486 }
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001487 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001488}
1489
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001490static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1491 struct ext4_dir_entry *de)
1492{
1493 struct super_block *sb = dir->i_sb;
1494
1495 if (!is_dx(dir))
1496 return 0;
1497 if (block == 0)
1498 return 1;
1499 if (de->inode == 0 &&
1500 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1501 sb->s_blocksize)
1502 return 1;
1503 return 0;
1504}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505
1506/*
Eric Biggersb01531d2019-03-20 11:39:13 -07001507 * __ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508 *
1509 * finds an entry in the specified directory with the wanted name. It
1510 * returns the cache buffer in which the entry was found, and the entry
1511 * itself (as a parameter - res_dir). It does NOT read the inode of the
1512 * entry - you'll have to do that yourself if you want to.
1513 *
1514 * The returned buffer_head has ->b_count elevated. The caller is expected
1515 * to brelse() it when appropriate.
1516 */
Eric Biggersb01531d2019-03-20 11:39:13 -07001517static struct buffer_head *__ext4_find_entry(struct inode *dir,
1518 struct ext4_filename *fname,
1519 struct ext4_dir_entry_2 **res_dir,
1520 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001521{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001522 struct super_block *sb;
1523 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1524 struct buffer_head *bh, *ret = NULL;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001525 ext4_lblk_t start, block;
Eric Biggersb01531d2019-03-20 11:39:13 -07001526 const u8 *name = fname->usr_fname->name;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001527 size_t ra_max = 0; /* Number of bh's in the readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528 buffer, bh_use[] */
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001529 size_t ra_ptr = 0; /* Current index into readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530 buffer */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001531 ext4_lblk_t nblocks;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001532 int i, namelen, retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001533
1534 *res_dir = NULL;
1535 sb = dir->i_sb;
Eric Biggersb01531d2019-03-20 11:39:13 -07001536 namelen = fname->usr_fname->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001537 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001538 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001539
1540 if (ext4_has_inline_data(dir)) {
1541 int has_inline_data = 1;
Eric Biggersb01531d2019-03-20 11:39:13 -07001542 ret = ext4_find_inline_entry(dir, fname, res_dir,
Tao Mae8e948e2012-12-10 14:06:00 -05001543 &has_inline_data);
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)) {
Eric Biggersb01531d2019-03-20 11:39:13 -07001562 ret = ext4_dx_find_entry(dir, fname, res_dir);
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);
Eric Biggersb01531d2019-03-20 11:39:13 -07001628 i = search_dirblock(bh, dir, fname,
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) {
Mingming Cao617ba132006-10-11 01:20:53 -07001631 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 ret = bh;
1633 goto cleanup_and_exit;
1634 } else {
1635 brelse(bh);
1636 if (i < 0)
1637 goto cleanup_and_exit;
1638 }
1639 next:
1640 if (++block >= nblocks)
1641 block = 0;
1642 } while (block != start);
1643
1644 /*
1645 * If the directory has grown while we were searching, then
1646 * search the last part of the directory before giving up.
1647 */
1648 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001649 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650 if (block < nblocks) {
1651 start = 0;
1652 goto restart;
1653 }
1654
1655cleanup_and_exit:
1656 /* Clean up the read-ahead blocks */
1657 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001658 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 return ret;
1660}
1661
Eric Biggersb01531d2019-03-20 11:39:13 -07001662static struct buffer_head *ext4_find_entry(struct inode *dir,
1663 const struct qstr *d_name,
1664 struct ext4_dir_entry_2 **res_dir,
1665 int *inlined)
1666{
1667 int err;
1668 struct ext4_filename fname;
1669 struct buffer_head *bh;
1670
1671 err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1672 if (err == -ENOENT)
1673 return NULL;
1674 if (err)
1675 return ERR_PTR(err);
1676
1677 bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1678
1679 ext4_fname_free_filename(&fname);
1680 return bh;
1681}
1682
1683static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1684 struct dentry *dentry,
1685 struct ext4_dir_entry_2 **res_dir)
1686{
1687 int err;
1688 struct ext4_filename fname;
1689 struct buffer_head *bh;
1690
1691 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
Daniel Rosenbergbb9cd912020-11-19 06:09:03 +00001692 generic_set_encrypted_ci_d_ops(dentry);
Eric Biggersb01531d2019-03-20 11:39:13 -07001693 if (err == -ENOENT)
1694 return NULL;
1695 if (err)
1696 return ERR_PTR(err);
1697
1698 bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1699
1700 ext4_fname_free_filename(&fname);
1701 return bh;
1702}
1703
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001704static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1705 struct ext4_filename *fname,
1706 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001708 struct super_block * sb = dir->i_sb;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001709 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001711 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713
Chandan Rajendra643fa962018-12-12 15:20:12 +05301714#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001715 *res_dir = NULL;
1716#endif
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001717 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001718 if (IS_ERR(frame))
1719 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 do {
1721 block = dx_get_block(frame->at);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04001722 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001723 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001725
Eric Biggersd6b97552017-05-24 18:10:49 -04001726 retval = search_dirblock(bh, dir, fname,
Theodore Ts'o7845c042010-10-27 21:30:08 -04001727 block << EXT4_BLOCK_SIZE_BITS(sb),
1728 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001729 if (retval == 1)
1730 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001731 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001732 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001733 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001734 goto errout;
1735 }
1736
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 /* Check to see if we should continue to search */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001738 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001739 frames, NULL);
1740 if (retval < 0) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04001741 ext4_warning_inode(dir,
1742 "error %d reading directory index block",
1743 retval);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001744 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 goto errout;
1746 }
1747 } while (retval == 1);
1748
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001749 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750errout:
Eric Biggersd6b97552017-05-24 18:10:49 -04001751 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001752success:
1753 dx_release(frames);
1754 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001756
Al Viro00cd8dd2012-06-10 17:13:09 -04001757static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001759 struct inode *inode;
1760 struct ext4_dir_entry_2 *de;
1761 struct buffer_head *bh;
Theodore Ts'o28b4c262016-02-07 19:35:05 -05001762
Eric Biggers89904272017-10-18 20:21:58 -04001763 if (dentry->d_name.len > EXT4_NAME_LEN)
1764 return ERR_PTR(-ENAMETOOLONG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765
Eric Biggersb01531d2019-03-20 11:39:13 -07001766 bh = ext4_lookup_entry(dir, dentry, &de);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001767 if (IS_ERR(bh))
Al Viroe884bce2018-10-10 16:41:40 -04001768 return ERR_CAST(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 inode = NULL;
1770 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001771 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001772 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001773 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001774 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001775 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001776 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001777 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001778 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1779 dentry);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001780 return ERR_PTR(-EFSCORRUPTED);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001781 }
Theodore Ts'o8a363972018-12-19 12:29:13 -05001782 inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
Al Viroa9049372011-07-08 21:20:11 -04001783 if (inode == ERR_PTR(-ESTALE)) {
1784 EXT4_ERROR_INODE(dir,
1785 "deleted inode referenced: %u",
1786 ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001787 return ERR_PTR(-EFSCORRUPTED);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001788 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301789 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
Theodore Ts'off978b02016-02-08 00:54:26 -05001790 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001791 !fscrypt_has_permitted_context(dir, inode)) {
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001792 ext4_warning(inode->i_sb,
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001793 "Inconsistent encryption contexts: %lu/%lu",
Eric Biggers8c680842017-04-07 10:58:38 -07001794 dir->i_ino, inode->i_ino);
Eric Biggersdd01b692017-02-01 21:07:11 -05001795 iput(inode);
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001796 return ERR_PTR(-EPERM);
1797 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001799
1800#ifdef CONFIG_UNICODE
1801 if (!inode && IS_CASEFOLDED(dir)) {
1802 /* Eventually we want to call d_add_ci(dentry, NULL)
1803 * for negative dentries in the encoding case as
1804 * well. For now, prevent the negative dentry
1805 * from being cached.
1806 */
1807 return NULL;
1808 }
1809#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001810 return d_splice_alias(inode, dentry);
1811}
1812
1813
Mingming Cao617ba132006-10-11 01:20:53 -07001814struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001815{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001816 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001817 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001818 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 struct buffer_head *bh;
1820
David Howells2b0143b2015-03-17 22:25:59 +00001821 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001822 if (IS_ERR(bh))
Al Viroe884bce2018-10-10 16:41:40 -04001823 return ERR_CAST(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001824 if (!bh)
1825 return ERR_PTR(-ENOENT);
1826 ino = le32_to_cpu(de->inode);
1827 brelse(bh);
1828
Al Virofc640052016-04-10 01:33:30 -04001829 if (!ext4_valid_inum(child->d_sb, ino)) {
David Howells2b0143b2015-03-17 22:25:59 +00001830 EXT4_ERROR_INODE(d_inode(child),
Theodore Ts'o24676da2010-05-16 21:00:00 -04001831 "bad parent inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001832 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001833 }
1834
Theodore Ts'o8a363972018-12-19 12:29:13 -05001835 return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836}
1837
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001838/*
1839 * Move count entries from end of map between two memory locations.
1840 * Returns pointer to last entry moved.
1841 */
Mingming Cao617ba132006-10-11 01:20:53 -07001842static struct ext4_dir_entry_2 *
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001843dx_move_dirents(struct inode *dir, char *from, char *to,
1844 struct dx_map_entry *map, int count,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001845 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001846{
1847 unsigned rec_len = 0;
1848
1849 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001850 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001851 (from + (map->offs<<2));
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001852 rec_len = ext4_dir_rec_len(de->name_len, dir);
1853
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001855 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001856 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001857 de->inode = 0;
1858 map++;
1859 to += rec_len;
1860 }
Mingming Cao617ba132006-10-11 01:20:53 -07001861 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862}
1863
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001864/*
1865 * Compact each dir entry in the range to the minimal rec_len.
1866 * Returns pointer to last entry in range.
1867 */
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001868static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
1869 unsigned int blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001870{
Mingming Cao617ba132006-10-11 01:20:53 -07001871 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 unsigned rec_len = 0;
1873
1874 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001875 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001876 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877 if (de->inode && de->name_len) {
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001878 rec_len = ext4_dir_rec_len(de->name_len, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879 if (de > to)
1880 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001881 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001882 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001883 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001884 }
1885 de = next;
1886 }
1887 return prev;
1888}
1889
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001890/*
1891 * Split a full leaf block to make room for a new dir entry.
1892 * Allocate a new block, and move entries so that they are approx. equally full.
1893 * Returns pointer to de in block into which the new entry will be inserted.
1894 */
Mingming Cao617ba132006-10-11 01:20:53 -07001895static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001897 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001898{
1899 unsigned blocksize = dir->i_sb->s_blocksize;
1900 unsigned count, continued;
1901 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001902 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001903 u32 hash2;
1904 struct dx_map_entry *map;
1905 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001906 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001907 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001908 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001909 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001910
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001911 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001912 csum_size = sizeof(struct ext4_dir_entry_tail);
1913
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001914 bh2 = ext4_append(handle, dir, &newblock);
1915 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001916 brelse(*bh);
1917 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001918 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919 }
1920
1921 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001922 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001923 if (err)
1924 goto journal_error;
1925
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001927 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001928 if (err)
1929 goto journal_error;
1930
1931 data2 = bh2->b_data;
1932
1933 /* create map in the end of data2 block */
1934 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001935 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001936 blocksize, hinfo, map);
1937 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001938 dx_sort_map(map, count);
Eric Sandeen58723312020-06-17 14:19:04 -05001939 /* Ensure that neither split block is over half full */
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001940 size = 0;
1941 move = 0;
1942 for (i = count-1; i >= 0; i--) {
1943 /* is more than half of this entry in 2nd half of the block? */
1944 if (size + map[i].size/2 > blocksize/2)
1945 break;
1946 size += map[i].size;
1947 move++;
1948 }
Eric Sandeen58723312020-06-17 14:19:04 -05001949 /*
1950 * map index at which we will split
1951 *
1952 * If the sum of active entries didn't exceed half the block size, just
1953 * split it in half by count; each resulting block will have at least
1954 * half the space free.
1955 */
1956 if (i > 0)
1957 split = count - move;
1958 else
1959 split = count/2;
1960
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961 hash2 = map[split].hash;
1962 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001963 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1964 (unsigned long)dx_get_block(frame->at),
1965 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966
1967 /* Fancy dance to stay within two buffers */
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001968 de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001969 blocksize);
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00001970 de = dx_pack_dirents(dir, data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001971 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1972 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001973 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001974 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1975 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001976 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001977 if (csum_size) {
Theodore Ts'oddce3b92019-06-21 16:31:47 -04001978 ext4_initialize_dirent_tail(*bh, blocksize);
1979 ext4_initialize_dirent_tail(bh2, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001980 }
1981
Michael Halcrowb3098482015-04-12 01:07:01 -04001982 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1983 blocksize, 1));
1984 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1985 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001986
1987 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001988 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 swap(*bh, bh2);
1990 de = de2;
1991 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001992 dx_insert_block(frame, hash2 + continued, newblock);
Theodore Ts'of036adb2019-06-21 15:49:26 -04001993 err = ext4_handle_dirty_dirblock(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 if (err)
1995 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001996 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997 if (err)
1998 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001999 brelse(bh2);
2000 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07002002
2003journal_error:
2004 brelse(*bh);
2005 brelse(bh2);
2006 *bh = NULL;
2007 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002008 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002010
Tao Ma978fef92012-12-10 14:05:58 -05002011int ext4_find_dest_de(struct inode *dir, struct inode *inode,
2012 struct buffer_head *bh,
2013 void *buf, int buf_size,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002014 struct ext4_filename *fname,
Tao Ma978fef92012-12-10 14:05:58 -05002015 struct ext4_dir_entry_2 **dest_de)
2016{
2017 struct ext4_dir_entry_2 *de;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002018 unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
Tao Ma978fef92012-12-10 14:05:58 -05002019 int nlen, rlen;
2020 unsigned int offset = 0;
2021 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04002022
Tao Ma978fef92012-12-10 14:05:58 -05002023 de = (struct ext4_dir_entry_2 *)buf;
2024 top = buf + buf_size - reclen;
2025 while ((char *) de <= top) {
2026 if (ext4_check_dir_entry(dir, NULL, de, bh,
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07002027 buf, buf_size, offset))
2028 return -EFSCORRUPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002029 if (ext4_match(dir, fname, de))
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07002030 return -EEXIST;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002031 nlen = ext4_dir_rec_len(de->name_len, dir);
Tao Ma978fef92012-12-10 14:05:58 -05002032 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2033 if ((de->inode ? rlen - nlen : rlen) >= reclen)
2034 break;
2035 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
2036 offset += rlen;
2037 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04002038 if ((char *) de > top)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07002039 return -ENOSPC;
2040
2041 *dest_de = de;
2042 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05002043}
2044
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002045void ext4_insert_dentry(struct inode *dir,
2046 struct inode *inode,
Eric Biggers1bc0af62017-04-29 23:27:26 -04002047 struct ext4_dir_entry_2 *de,
2048 int buf_size,
2049 struct ext4_filename *fname)
Tao Ma978fef92012-12-10 14:05:58 -05002050{
2051
2052 int nlen, rlen;
2053
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002054 nlen = ext4_dir_rec_len(de->name_len, dir);
Tao Ma978fef92012-12-10 14:05:58 -05002055 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2056 if (de->inode) {
2057 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002058 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05002059 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
2060 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
2061 de = de1;
2062 }
2063 de->file_type = EXT4_FT_UNKNOWN;
2064 de->inode = cpu_to_le32(inode->i_ino);
2065 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002066 de->name_len = fname_len(fname);
2067 memcpy(de->name, fname_name(fname), fname_len(fname));
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002068 if (ext4_hash_in_dirent(dir)) {
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00002069 struct dx_hash_info *hinfo = &fname->hinfo;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002070
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00002071 EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002072 EXT4_DIRENT_HASHES(de)->minor_hash =
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00002073 cpu_to_le32(hinfo->minor_hash);
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002074 }
Tao Ma978fef92012-12-10 14:05:58 -05002075}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002076
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077/*
2078 * Add a new entry into a directory (leaf) block. If de is non-NULL,
2079 * it points to a directory entry which is guaranteed to be large
2080 * enough for new directory entry. If de is NULL, then
2081 * add_dirent_to_buf will attempt search the directory block for
2082 * space. It will return -ENOSPC if no space is available, and -EIO
2083 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002084 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002085static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
2086 struct inode *dir,
Mingming Cao617ba132006-10-11 01:20:53 -07002087 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002088 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089{
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002090 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002091 int csum_size = 0;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002092 int err, err2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002093
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002094 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002095 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002096
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002097 if (!de) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002098 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
2099 blocksize - csum_size, fname, &de);
Tao Ma978fef92012-12-10 14:05:58 -05002100 if (err)
2101 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002102 }
2103 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002104 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002105 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002106 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002107 return err;
2108 }
2109
Eric Biggers1bc0af62017-04-29 23:27:26 -04002110 /* By now the buffer is marked for journaling */
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002111 ext4_insert_dentry(dir, inode, de, blocksize, fname);
Tao Ma978fef92012-12-10 14:05:58 -05002112
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002113 /*
2114 * XXX shouldn't update any times until successful
2115 * completion of syscall, but too many callers depend
2116 * on this.
2117 *
2118 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07002119 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002120 * recovery deletes the inode, so the worst that can
2121 * happen is that the times are slightly out of date
2122 * and/or different from the directory change time.
2123 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002124 dir->i_mtime = dir->i_ctime = current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002125 ext4_update_dx_flag(dir);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002126 inode_inc_iversion(dir);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002127 err2 = ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05002128 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Theodore Ts'of036adb2019-06-21 15:49:26 -04002129 err = ext4_handle_dirty_dirblock(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07002131 ext4_std_error(dir->i_sb, err);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002132 return err ? err : err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002133}
2134
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135/*
2136 * This converts a one block unindexed directory to a 3 block indexed
2137 * directory, and adds the dentry to the indexed directory.
2138 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002139static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002140 struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 struct inode *inode, struct buffer_head *bh)
2142{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002143 struct buffer_head *bh2;
2144 struct dx_root *root;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002145 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07002147 struct ext4_dir_entry_2 *de, *de2;
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002148 char *data2, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002149 unsigned len;
2150 int retval;
2151 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002152 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002154 int csum_size = 0;
2155
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002156 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002157 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158
2159 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002160 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04002161 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002162 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002164 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165 brelse(bh);
2166 return retval;
2167 }
2168 root = (struct dx_root *) bh->b_data;
2169
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002170 /* The 0th block becomes the root, move the dirents out */
2171 fde = &root->dotdot;
2172 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002173 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002174 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002175 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002176 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002177 return -EFSCORRUPTED;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002178 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002179 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002180
2181 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002182 bh2 = ext4_append(handle, dir, &block);
2183 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002184 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002185 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002187 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002188 data2 = bh2->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002190 memcpy(data2, de, len);
2191 de = (struct ext4_dir_entry_2 *) data2;
2192 top = data2 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002193 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002194 de = de2;
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002195 de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
2196 (char *) de, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002197
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002198 if (csum_size)
2199 ext4_initialize_dirent_tail(bh2, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002200
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002201 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07002202 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002203 de->rec_len = ext4_rec_len_to_disk(
2204 blocksize - ext4_dir_rec_len(2, NULL), blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205 memset (&root->info, 0, sizeof(root->info));
2206 root->info.info_length = sizeof(root->info);
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002207 if (ext4_hash_in_dirent(dir))
2208 root->info.hash_version = DX_HASH_SIPHASH;
2209 else
2210 root->info.hash_version =
2211 EXT4_SB(dir->i_sb)->s_def_hash_version;
2212
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002213 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002214 dx_set_block(entries, 1);
2215 dx_set_count(entries, 1);
2216 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217
2218 /* Initialize as for dx_probe */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002219 fname->hinfo.hash_version = root->info.hash_version;
2220 if (fname->hinfo.hash_version <= DX_HASH_TEA)
2221 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2222 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Daniel Rosenberg1ae98e22021-03-19 07:34:14 +00002223
2224 /* casefolded encrypted hashes are computed on fname setup */
2225 if (!ext4_hash_in_dirent(dir))
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002226 ext4fs_dirhash(dir, fname_name(fname),
2227 fname_len(fname), &fname->hinfo);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002228
Jan Kara6050d472014-10-30 10:53:17 -04002229 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 frame = frames;
2231 frame->entries = entries;
2232 frame->at = entries;
2233 frame->bh = bh;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002234
Jan Kara6050d472014-10-30 10:53:17 -04002235 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2236 if (retval)
2237 goto out_frames;
Theodore Ts'of036adb2019-06-21 15:49:26 -04002238 retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002239 if (retval)
2240 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002241
gmaile81d4472016-09-30 01:33:37 -04002242 de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002243 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002244 retval = PTR_ERR(de);
2245 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002246 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002247
gmaile81d4472016-09-30 01:33:37 -04002248 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002249out_frames:
2250 /*
2251 * Even if the block split failed, we have to properly write
2252 * out all the changes we did so far. Otherwise we can end up
2253 * with corrupted filesystem.
2254 */
gmaile81d4472016-09-30 01:33:37 -04002255 if (retval)
2256 ext4_mark_inode_dirty(handle, dir);
Jan Kara6050d472014-10-30 10:53:17 -04002257 dx_release(frames);
gmaile81d4472016-09-30 01:33:37 -04002258 brelse(bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002259 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261
2262/*
Mingming Cao617ba132006-10-11 01:20:53 -07002263 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264 *
2265 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002266 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267 *
2268 * NOTE!! The inode part of 'de' is left at 0 - which means you
2269 * may not sleep between calling this and putting something into
2270 * the entry, as someone else might have used it while you slept.
2271 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002272static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2273 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274{
David Howells2b0143b2015-03-17 22:25:59 +00002275 struct inode *dir = d_inode(dentry->d_parent);
Lukas Czernere12fb972015-04-03 10:46:58 -04002276 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002277 struct ext4_dir_entry_2 *de;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002278 struct super_block *sb;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002279 struct ext4_filename fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002282 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002283 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002284 int csum_size = 0;
2285
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002286 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002287 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002288
2289 sb = dir->i_sb;
2290 blocksize = sb->s_blocksize;
2291 if (!dentry->d_name.len)
2292 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002293
Eric Biggers75d18cd2020-11-17 23:56:06 -08002294 if (fscrypt_is_nokey_name(dentry))
2295 return -ENOKEY;
2296
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002297#ifdef CONFIG_UNICODE
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00002298 if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
2299 sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002300 return -EINVAL;
2301#endif
2302
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002303 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2304 if (retval)
2305 return retval;
2306
Tao Ma3c47d542012-12-10 14:05:59 -05002307 if (ext4_has_inline_data(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002308 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05002309 if (retval < 0)
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002310 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002311 if (retval == 1) {
2312 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002313 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002314 }
2315 }
2316
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002317 if (is_dx(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002318 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002320 goto out;
Jan Kara48a34312020-02-10 15:43:16 +01002321 /* Can we just ignore htree data? */
2322 if (ext4_has_metadata_csum(sb)) {
2323 EXT4_ERROR_INODE(dir,
2324 "Directory has corrupted htree index.");
2325 retval = -EFSCORRUPTED;
2326 goto out;
2327 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002328 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002329 dx_fallback++;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002330 retval = ext4_mark_inode_dirty(handle, dir);
2331 if (unlikely(retval))
2332 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002333 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002334 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002335 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002336 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002337 if (bh == NULL) {
2338 bh = ext4_bread(handle, dir, block,
2339 EXT4_GET_BLOCKS_CREATE);
2340 goto add_to_new_block;
2341 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002342 if (IS_ERR(bh)) {
2343 retval = PTR_ERR(bh);
2344 bh = NULL;
2345 goto out;
2346 }
2347 retval = add_dirent_to_buf(handle, &fname, dir, inode,
2348 NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002349 if (retval != -ENOSPC)
2350 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002351
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 if (blocks == 1 && !dx_fallback &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002353 ext4_has_feature_dir_index(sb)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002354 retval = make_indexed_dir(handle, &fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002355 inode, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002356 bh = NULL; /* make_indexed_dir releases bh */
2357 goto out;
2358 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002359 brelse(bh);
2360 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002361 bh = ext4_append(handle, dir, &block);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002362add_to_new_block:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002363 if (IS_ERR(bh)) {
2364 retval = PTR_ERR(bh);
2365 bh = NULL;
2366 goto out;
2367 }
Mingming Cao617ba132006-10-11 01:20:53 -07002368 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002369 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002370 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2371
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002372 if (csum_size)
2373 ext4_initialize_dirent_tail(bh, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002374
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002375 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002376out:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002377 ext4_fname_free_filename(&fname);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002378 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002379 if (retval == 0)
2380 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002381 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002382}
2383
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002384/*
2385 * Returns 0 for success, or a negative error value
2386 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002387static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002388 struct inode *dir, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002389{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002390 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002391 struct dx_entry *entries, *at;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002392 struct buffer_head *bh;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002393 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002394 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002395 int restart;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002396 int err;
2397
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002398again:
2399 restart = 0;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002400 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002401 if (IS_ERR(frame))
2402 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403 entries = frame->entries;
2404 at = frame->at;
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002405 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002406 if (IS_ERR(bh)) {
2407 err = PTR_ERR(bh);
2408 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002409 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002410 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002411
2412 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002413 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 if (err)
2415 goto journal_error;
2416
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002417 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002418 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002419 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002421 err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002422 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002423 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002424 dx_get_count(entries), dx_get_limit(entries)));
2425 /* Need to split index? */
2426 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002427 ext4_lblk_t newblock;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002428 int levels = frame - frames + 1;
2429 unsigned int icount;
2430 int add_level = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002431 struct dx_entry *entries2;
2432 struct dx_node *node2;
2433 struct buffer_head *bh2;
2434
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002435 while (frame > frames) {
2436 if (dx_get_count((frame - 1)->entries) <
2437 dx_get_limit((frame - 1)->entries)) {
2438 add_level = 0;
2439 break;
2440 }
2441 frame--; /* split higher index block */
2442 at = frame->at;
2443 entries = frame->entries;
2444 restart = 1;
2445 }
2446 if (add_level && levels == ext4_dir_htree_level(sb)) {
2447 ext4_warning(sb, "Directory (ino: %lu) index full, "
2448 "reach max htree level :%d",
2449 dir->i_ino, levels);
2450 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2451 ext4_warning(sb, "Large directory feature is "
2452 "not enabled on this "
2453 "filesystem");
2454 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002455 err = -ENOSPC;
2456 goto cleanup;
2457 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002458 icount = dx_get_count(entries);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002459 bh2 = ext4_append(handle, dir, &newblock);
2460 if (IS_ERR(bh2)) {
2461 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002463 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002464 node2 = (struct dx_node *)(bh2->b_data);
2465 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002466 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002467 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2468 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002470 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471 if (err)
2472 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002473 if (!add_level) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002474 unsigned icount1 = icount/2, icount2 = icount - icount1;
2475 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002476 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2477 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002478
2479 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002480 err = ext4_journal_get_write_access(handle,
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002481 (frame - 1)->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002482 if (err)
2483 goto journal_error;
2484
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002485 memcpy((char *) entries2, (char *) (entries + icount1),
2486 icount2 * sizeof(struct dx_entry));
2487 dx_set_count(entries, icount1);
2488 dx_set_count(entries2, icount2);
2489 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490
2491 /* Which index block gets the new entry? */
2492 if (at - entries >= icount1) {
2493 frame->at = at = at - entries - icount1 + entries2;
2494 frame->entries = entries = entries2;
2495 swap(frame->bh, bh2);
2496 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002497 dx_insert_block((frame - 1), hash2, newblock);
2498 dxtrace(dx_show_index("node", frame->entries));
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002499 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002500 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002501 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002502 if (err)
2503 goto journal_error;
2504 brelse (bh2);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002505 err = ext4_handle_dirty_dx_node(handle, dir,
2506 (frame - 1)->bh);
2507 if (err)
2508 goto journal_error;
Theodore Ts'ob5776e72021-02-04 00:05:20 -05002509 err = ext4_handle_dirty_dx_node(handle, dir,
2510 frame->bh);
2511 if (err)
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002512 goto journal_error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 } else {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002514 struct dx_root *dxroot;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002515 memcpy((char *) entries2, (char *) entries,
2516 icount * sizeof(struct dx_entry));
2517 dx_set_limit(entries2, dx_node_limit(dir));
2518
2519 /* Set up root */
2520 dx_set_count(entries, 1);
2521 dx_set_block(entries + 0, newblock);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002522 dxroot = (struct dx_root *)frames[0].bh->b_data;
2523 dxroot->info.indirect_levels += 1;
2524 dxtrace(printk(KERN_DEBUG
2525 "Creating %d level index...\n",
Gabriel Krisman Bertazi799578a2018-10-02 12:43:51 -04002526 dxroot->info.indirect_levels));
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002527 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002528 if (err)
2529 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002530 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2531 brelse(bh2);
2532 restart = 1;
2533 goto journal_error;
Theodore Ts'ob4097142011-01-10 12:46:59 -05002534 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002535 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002536 de = do_split(handle, dir, &bh, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002537 if (IS_ERR(de)) {
2538 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002539 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002540 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002541 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002542 goto cleanup;
2543
2544journal_error:
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002545 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002547 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002548 dx_release(frames);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002549 /* @restart is true means htree-path has been changed, we need to
2550 * repeat dx_probe() to find out valid htree-path
2551 */
2552 if (restart && err == 0)
2553 goto again;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002554 return err;
2555}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002556
2557/*
Tao Ma05019a92012-12-10 14:06:00 -05002558 * ext4_generic_delete_entry deletes a directory entry by merging it
2559 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002560 */
Kyoungho Koo2fe34d292020-08-10 17:07:05 +09002561int ext4_generic_delete_entry(struct inode *dir,
Tao Ma05019a92012-12-10 14:06:00 -05002562 struct ext4_dir_entry_2 *de_del,
2563 struct buffer_head *bh,
2564 void *entry_buf,
2565 int buf_size,
2566 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002568 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002569 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002570 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002571
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002572 i = 0;
2573 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002574 de = (struct ext4_dir_entry_2 *)entry_buf;
2575 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002576 if (ext4_check_dir_entry(dir, NULL, de, bh,
Jan Kara7303cb52020-07-31 18:21:35 +02002577 entry_buf, buf_size, i))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002578 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002579 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002580 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002581 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002582 ext4_rec_len_from_disk(pde->rec_len,
2583 blocksize) +
2584 ext4_rec_len_from_disk(de->rec_len,
2585 blocksize),
2586 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002587 else
2588 de->inode = 0;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002589 inode_inc_iversion(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002590 return 0;
2591 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002592 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002593 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002594 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002595 }
2596 return -ENOENT;
2597}
2598
Tao Ma05019a92012-12-10 14:06:00 -05002599static int ext4_delete_entry(handle_t *handle,
2600 struct inode *dir,
2601 struct ext4_dir_entry_2 *de_del,
2602 struct buffer_head *bh)
2603{
2604 int err, csum_size = 0;
2605
Tao Ma9f40fe52012-12-10 14:06:00 -05002606 if (ext4_has_inline_data(dir)) {
2607 int has_inline_data = 1;
2608 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2609 &has_inline_data);
2610 if (has_inline_data)
2611 return err;
2612 }
2613
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002614 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002615 csum_size = sizeof(struct ext4_dir_entry_tail);
2616
2617 BUFFER_TRACE(bh, "get_write_access");
2618 err = ext4_journal_get_write_access(handle, bh);
2619 if (unlikely(err))
2620 goto out;
2621
Kyoungho Koo2fe34d292020-08-10 17:07:05 +09002622 err = ext4_generic_delete_entry(dir, de_del, bh, bh->b_data,
Tao Ma05019a92012-12-10 14:06:00 -05002623 dir->i_sb->s_blocksize, csum_size);
2624 if (err)
2625 goto out;
2626
2627 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Theodore Ts'of036adb2019-06-21 15:49:26 -04002628 err = ext4_handle_dirty_dirblock(handle, dir, bh);
Tao Ma05019a92012-12-10 14:06:00 -05002629 if (unlikely(err))
2630 goto out;
2631
2632 return 0;
2633out:
2634 if (err != -ENOENT)
2635 ext4_std_error(dir->i_sb, err);
2636 return err;
2637}
2638
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002639/*
Andreas Dilgerc7414892017-08-05 19:47:34 -04002640 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2641 * since this indicates that nlinks count was previously 1 to avoid overflowing
2642 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2643 * that subdirectory link counts are not being maintained accurately.
2644 *
2645 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2646 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2647 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2648 * on regular files) and to avoid creating huge/slow non-HTREE directories.
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002649 */
Nikolay Borisov15ed2852020-08-26 16:31:16 +03002650static void ext4_inc_count(struct inode *inode)
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002651{
2652 inc_nlink(inode);
Andreas Dilgerc7414892017-08-05 19:47:34 -04002653 if (is_dx(inode) &&
2654 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2655 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002656}
2657
2658/*
2659 * If a directory had nlink == 1, then we should let it be 1. This indicates
2660 * directory has >EXT4_LINK_MAX subdirs.
2661 */
Nikolay Borisov15ed2852020-08-26 16:31:16 +03002662static void ext4_dec_count(struct inode *inode)
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002663{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002664 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2665 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002666}
2667
2668
Jan Kara9b88f9f2019-11-05 17:44:11 +01002669/*
2670 * Add non-directory inode to a directory. On success, the inode reference is
2671 * consumed by dentry is instantiation. This is also indicated by clearing of
2672 * *inodep pointer. On failure, the caller is responsible for dropping the
2673 * inode reference in the safe context.
2674 */
Mingming Cao617ba132006-10-11 01:20:53 -07002675static int ext4_add_nondir(handle_t *handle,
Jan Kara9b88f9f2019-11-05 17:44:11 +01002676 struct dentry *dentry, struct inode **inodep)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002677{
Jan Karaa9e26322019-11-05 17:44:10 +01002678 struct inode *dir = d_inode(dentry->d_parent);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002679 struct inode *inode = *inodep;
Mingming Cao617ba132006-10-11 01:20:53 -07002680 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681 if (!err) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002682 err = ext4_mark_inode_dirty(handle, inode);
Jan Karaa9e26322019-11-05 17:44:10 +01002683 if (IS_DIRSYNC(dir))
2684 ext4_handle_sync(handle);
Al Viro1e2e5472018-05-04 08:23:01 -04002685 d_instantiate_new(dentry, inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002686 *inodep = NULL;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002687 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002688 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002689 drop_nlink(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002690 ext4_orphan_add(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002691 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002692 return err;
2693}
2694
2695/*
2696 * By the time this is called, we already have created
2697 * the directory cache entry for the new file, but it
2698 * is so far negative - it has no inode.
2699 *
2700 * If the create succeeds, we fill in the inode information
2701 * with d_instantiate().
2702 */
Christian Brauner549c7292021-01-21 14:19:43 +01002703static int ext4_create(struct user_namespace *mnt_userns, struct inode *dir,
2704 struct dentry *dentry, umode_t mode, bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002705{
2706 handle_t *handle;
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002707 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002708 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002709
Jan Karaa7cdade2015-06-29 16:22:54 +02002710 err = dquot_initialize(dir);
2711 if (err)
2712 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002713
Theodore Ts'o11395752013-02-09 16:27:09 -05002714 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002715 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002716retry:
Christian Brauner14f3db52021-01-21 14:19:57 +01002717 inode = ext4_new_inode_start_handle(mnt_userns, dir, mode, &dentry->d_name,
2718 0, NULL, EXT4_HT_DIR, credits);
Theodore Ts'o11395752013-02-09 16:27:09 -05002719 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002720 err = PTR_ERR(inode);
2721 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002722 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002723 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002724 ext4_set_aops(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002725 err = ext4_add_nondir(handle, dentry, &inode);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002726 if (!err)
2727 ext4_fc_track_create(handle, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002728 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002729 if (handle)
2730 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002731 if (!IS_ERR_OR_NULL(inode))
2732 iput(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002733 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002734 goto retry;
2735 return err;
2736}
2737
Christian Brauner549c7292021-01-21 14:19:43 +01002738static int ext4_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2739 struct dentry *dentry, umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002740{
2741 handle_t *handle;
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002742 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002743 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744
Jan Karaa7cdade2015-06-29 16:22:54 +02002745 err = dquot_initialize(dir);
2746 if (err)
2747 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002748
Theodore Ts'o11395752013-02-09 16:27:09 -05002749 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002750 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002751retry:
Christian Brauner14f3db52021-01-21 14:19:57 +01002752 inode = ext4_new_inode_start_handle(mnt_userns, dir, mode, &dentry->d_name,
2753 0, NULL, EXT4_HT_DIR, credits);
Theodore Ts'o11395752013-02-09 16:27:09 -05002754 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002755 err = PTR_ERR(inode);
2756 if (!IS_ERR(inode)) {
2757 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002758 inode->i_op = &ext4_special_inode_operations;
Jan Kara9b88f9f2019-11-05 17:44:11 +01002759 err = ext4_add_nondir(handle, dentry, &inode);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07002760 if (!err)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002761 ext4_fc_track_create(handle, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002762 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002763 if (handle)
2764 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002765 if (!IS_ERR_OR_NULL(inode))
2766 iput(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002767 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002768 goto retry;
2769 return err;
2770}
2771
Christian Brauner549c7292021-01-21 14:19:43 +01002772static int ext4_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
2773 struct dentry *dentry, umode_t mode)
Al Viroaf51a2a2013-06-29 13:23:08 +04002774{
2775 handle_t *handle;
2776 struct inode *inode;
2777 int err, retries = 0;
2778
Jan Karaa7cdade2015-06-29 16:22:54 +02002779 err = dquot_initialize(dir);
2780 if (err)
2781 return err;
Al Viroaf51a2a2013-06-29 13:23:08 +04002782
2783retry:
Christian Brauner14f3db52021-01-21 14:19:57 +01002784 inode = ext4_new_inode_start_handle(mnt_userns, dir, mode,
Al Viroaf51a2a2013-06-29 13:23:08 +04002785 NULL, 0, NULL,
2786 EXT4_HT_DIR,
2787 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2788 4 + EXT4_XATTR_TRANS_BLOCKS);
2789 handle = ext4_journal_current_handle();
2790 err = PTR_ERR(inode);
2791 if (!IS_ERR(inode)) {
2792 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002793 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002794 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002795 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002796 err = ext4_orphan_add(handle, inode);
2797 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002798 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002799 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002800 unlock_new_inode(inode);
2801 }
2802 if (handle)
2803 ext4_journal_stop(handle);
2804 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2805 goto retry;
2806 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002807err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002808 ext4_journal_stop(handle);
2809 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002810 return err;
2811}
2812
Tao Maa774f9c2012-12-10 14:05:57 -05002813struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2814 struct ext4_dir_entry_2 *de,
2815 int blocksize, int csum_size,
2816 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002817{
Tao Maa774f9c2012-12-10 14:05:57 -05002818 de->inode = cpu_to_le32(inode->i_ino);
2819 de->name_len = 1;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002820 de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
Tao Maa774f9c2012-12-10 14:05:57 -05002821 blocksize);
2822 strcpy(de->name, ".");
2823 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2824
2825 de = ext4_next_entry(de, blocksize);
2826 de->inode = cpu_to_le32(parent_ino);
2827 de->name_len = 2;
2828 if (!dotdot_real_len)
2829 de->rec_len = ext4_rec_len_to_disk(blocksize -
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002830 (csum_size + ext4_dir_rec_len(1, NULL)),
Tao Maa774f9c2012-12-10 14:05:57 -05002831 blocksize);
2832 else
2833 de->rec_len = ext4_rec_len_to_disk(
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002834 ext4_dir_rec_len(de->name_len, NULL),
2835 blocksize);
Tao Maa774f9c2012-12-10 14:05:57 -05002836 strcpy(de->name, "..");
2837 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2838
2839 return ext4_next_entry(de, blocksize);
2840}
2841
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07002842int ext4_init_new_dir(handle_t *handle, struct inode *dir,
Tao Maa774f9c2012-12-10 14:05:57 -05002843 struct inode *inode)
2844{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002845 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002846 struct ext4_dir_entry_2 *de;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002847 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002848 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002849 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002850 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002852 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002853 csum_size = sizeof(struct ext4_dir_entry_tail);
2854
Tao Ma3c47d542012-12-10 14:05:59 -05002855 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2856 err = ext4_try_create_inline_dir(handle, dir, inode);
2857 if (err < 0 && err != -ENOSPC)
2858 goto out;
2859 if (!err)
2860 goto out;
2861 }
2862
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002863 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002864 dir_block = ext4_append(handle, inode, &block);
2865 if (IS_ERR(dir_block))
2866 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002867 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2868 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2869 set_nlink(inode, 2);
Theodore Ts'oddce3b92019-06-21 16:31:47 -04002870 if (csum_size)
2871 ext4_initialize_dirent_tail(dir_block, blocksize);
Tao Maa774f9c2012-12-10 14:05:57 -05002872
2873 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
Theodore Ts'of036adb2019-06-21 15:49:26 -04002874 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002875 if (err)
2876 goto out;
2877 set_buffer_verified(dir_block);
2878out:
2879 brelse(dir_block);
2880 return err;
2881}
2882
Christian Brauner549c7292021-01-21 14:19:43 +01002883static int ext4_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2884 struct dentry *dentry, umode_t mode)
Tao Maa774f9c2012-12-10 14:05:57 -05002885{
2886 handle_t *handle;
2887 struct inode *inode;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002888 int err, err2 = 0, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002889
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002890 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 return -EMLINK;
2892
Jan Karaa7cdade2015-06-29 16:22:54 +02002893 err = dquot_initialize(dir);
2894 if (err)
2895 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002896
Theodore Ts'o11395752013-02-09 16:27:09 -05002897 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002898 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002899retry:
Christian Brauner14f3db52021-01-21 14:19:57 +01002900 inode = ext4_new_inode_start_handle(mnt_userns, dir, S_IFDIR | mode,
Theodore Ts'o11395752013-02-09 16:27:09 -05002901 &dentry->d_name,
2902 0, NULL, EXT4_HT_DIR, credits);
2903 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002904 err = PTR_ERR(inode);
2905 if (IS_ERR(inode))
2906 goto out_stop;
2907
Mingming Cao617ba132006-10-11 01:20:53 -07002908 inode->i_op = &ext4_dir_inode_operations;
2909 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002910 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002911 if (err)
2912 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002913 err = ext4_mark_inode_dirty(handle, inode);
2914 if (!err)
2915 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002917out_clear_inode:
2918 clear_nlink(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002919 ext4_orphan_add(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002920 unlock_new_inode(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07002921 err2 = ext4_mark_inode_dirty(handle, inode);
2922 if (unlikely(err2))
2923 err = err2;
Jan Kara9b88f9f2019-11-05 17:44:11 +01002924 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002925 iput(inode);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002926 goto out_retry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002927 }
Nikolay Borisov15ed2852020-08-26 16:31:16 +03002928 ext4_inc_count(dir);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07002929
Mingming Cao617ba132006-10-11 01:20:53 -07002930 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002931 err = ext4_mark_inode_dirty(handle, dir);
2932 if (err)
2933 goto out_clear_inode;
Al Viro1e2e5472018-05-04 08:23:01 -04002934 d_instantiate_new(dentry, inode);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08002935 ext4_fc_track_create(handle, dentry);
Theodore Ts'o11395752013-02-09 16:27:09 -05002936 if (IS_DIRSYNC(dir))
2937 ext4_handle_sync(handle);
2938
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002940 if (handle)
2941 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01002942out_retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002943 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944 goto retry;
2945 return err;
2946}
2947
2948/*
2949 * routine to check that the specified directory is empty (for rmdir)
2950 */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002951bool ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002952{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002953 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002954 struct buffer_head *bh;
Jan Kara64d4ce82019-12-02 18:02:12 +01002955 struct ext4_dir_entry_2 *de;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002956 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002957
Tao Ma61f86632012-12-10 14:06:01 -05002958 if (ext4_has_inline_data(inode)) {
2959 int has_inline_data = 1;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002960 int ret;
Tao Ma61f86632012-12-10 14:06:01 -05002961
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002962 ret = empty_inline_dir(inode, &has_inline_data);
Tao Ma61f86632012-12-10 14:06:01 -05002963 if (has_inline_data)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002964 return ret;
Tao Ma61f86632012-12-10 14:06:01 -05002965 }
2966
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002967 sb = inode->i_sb;
Daniel Rosenberg471fbbe2021-03-19 07:34:13 +00002968 if (inode->i_size < ext4_dir_rec_len(1, NULL) +
2969 ext4_dir_rec_len(2, NULL)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002970 EXT4_ERROR_INODE(inode, "invalid size");
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002971 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002972 }
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04002973 /* The first directory block must not be a hole,
2974 * so treat it as DIRENT_HTREE
2975 */
2976 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002977 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002978 return true;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002979
Mingming Cao617ba132006-10-11 01:20:53 -07002980 de = (struct ext4_dir_entry_2 *) bh->b_data;
Jan Kara64d4ce82019-12-02 18:02:12 +01002981 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2982 0) ||
2983 le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
2984 ext4_warning_inode(inode, "directory missing '.'");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002985 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002986 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002987 }
Jan Kara64d4ce82019-12-02 18:02:12 +01002988 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2989 de = ext4_next_entry(de, sb->s_blocksize);
2990 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2991 offset) ||
2992 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
2993 ext4_warning_inode(inode, "directory missing '..'");
2994 brelse(bh);
2995 return true;
2996 }
2997 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002998 while (offset < inode->i_size) {
Jan Kara64d4ce82019-12-02 18:02:12 +01002999 if (!(offset & (sb->s_blocksize - 1))) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003000 unsigned int lblock;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003001 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04003002 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003003 bh = ext4_read_dirblock(inode, lblock, EITHER);
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04003004 if (bh == NULL) {
3005 offset += sb->s_blocksize;
3006 continue;
3007 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003008 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003009 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010 }
Jan Kara64d4ce82019-12-02 18:02:12 +01003011 de = (struct ext4_dir_entry_2 *) (bh->b_data +
3012 (offset & (sb->s_blocksize - 1)));
Tao Ma226ba972012-12-10 14:05:58 -05003013 if (ext4_check_dir_entry(inode, NULL, de, bh,
3014 bh->b_data, bh->b_size, offset)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003015 offset = (offset | (sb->s_blocksize - 1)) + 1;
3016 continue;
3017 }
3018 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003019 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003020 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05003022 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003023 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003024 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003025 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003026}
3027
Jan Karad745a8c2014-05-26 11:56:53 -04003028/*
3029 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030 * such inodes, starting at the superblock, in case we crash before the
3031 * file is closed/deleted, or in case the inode truncate spans multiple
3032 * transactions and the last transaction is not recovered after a crash.
3033 *
3034 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07003035 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04003036 *
3037 * Orphan list manipulation functions must be called under i_mutex unless
3038 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039 */
Mingming Cao617ba132006-10-11 01:20:53 -07003040int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003041{
3042 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04003043 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07003044 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003045 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04003046 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04003048 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003049 return 0;
3050
Jan Karad745a8c2014-05-26 11:56:53 -04003051 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05003052 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04003053 /*
3054 * Exit early if inode already is on orphan list. This is a big speedup
3055 * since we don't have to contend on the global s_orphan_lock.
3056 */
Mingming Cao617ba132006-10-11 01:20:53 -07003057 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04003058 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003059
Lukas Czernerafb86172011-07-11 18:47:04 -04003060 /*
3061 * Orphan handling is only valid for files with data blocks
3062 * being truncated, or files being unlinked. Note that we either
3063 * hold i_mutex, or the inode can not be referenced from outside,
3064 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 */
Chunguang Xu837c23f2020-11-07 23:58:11 +08003066 ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003067 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068
Jan Karacd2c0802014-05-26 11:39:17 -04003069 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
3070 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04003072 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003073
Mingming Cao617ba132006-10-11 01:20:53 -07003074 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04003076 goto out;
3077
3078 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05003079 /*
3080 * Due to previous errors inode may be already a part of on-disk
3081 * orphan list. If so skip on-disk list modification.
3082 */
Jan Karad745a8c2014-05-26 11:56:53 -04003083 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
3084 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
3085 /* Insert this inode at the head of the on-disk orphan list */
3086 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
Jan Kara05c2c002020-12-16 11:18:39 +01003087 lock_buffer(sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003088 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
Jan Kara05c2c002020-12-16 11:18:39 +01003089 ext4_superblock_csum_set(sb);
3090 unlock_buffer(sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003091 dirty = true;
3092 }
3093 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
3094 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095
Jan Karad745a8c2014-05-26 11:56:53 -04003096 if (dirty) {
Jan Karaa3f5cf12020-12-16 11:18:44 +01003097 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003098 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
3099 if (!err)
3100 err = rc;
3101 if (err) {
3102 /*
3103 * We have to remove inode from in-memory list if
3104 * addition to on disk orphan list failed. Stray orphan
3105 * list entries can cause panics at unmount time.
3106 */
3107 mutex_lock(&sbi->s_orphan_lock);
Jan Kara74177f552016-05-05 11:10:15 -04003108 list_del_init(&EXT4_I(inode)->i_orphan);
Jan Karad745a8c2014-05-26 11:56:53 -04003109 mutex_unlock(&sbi->s_orphan_lock);
3110 }
Vasily Averinfeaf2642018-11-06 17:01:36 -05003111 } else
3112 brelse(iloc.bh);
3113
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003114 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
3115 jbd_debug(4, "orphan inode %lu will point to %d\n",
3116 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04003117out:
Jan Karacd2c0802014-05-26 11:39:17 -04003118 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003119 return err;
3120}
3121
3122/*
Mingming Cao617ba132006-10-11 01:20:53 -07003123 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124 * of such inodes stored on disk, because it is finally being cleaned up.
3125 */
Mingming Cao617ba132006-10-11 01:20:53 -07003126int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003127{
3128 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07003129 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04003130 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003131 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07003132 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003133 int err = 0;
3134
Jan Karacd2c0802014-05-26 11:39:17 -04003135 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05003136 return 0;
3137
Jan Karad745a8c2014-05-26 11:56:53 -04003138 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05003139 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04003140 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04003141 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04003142 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003143
Jan Karad745a8c2014-05-26 11:56:53 -04003144 if (handle) {
3145 /* Grab inode buffer early before taking global s_orphan_lock */
3146 err = ext4_reserve_inode_write(handle, inode, &iloc);
3147 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003148
Jan Karad745a8c2014-05-26 11:56:53 -04003149 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003150 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
3151
Jan Karad745a8c2014-05-26 11:56:53 -04003152 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003153 list_del_init(&ei->i_orphan);
3154
3155 /* If we're on an error path, we may not have a valid
3156 * transaction handle with which to update the orphan list on
3157 * disk, but we still need to remove the inode from the linked
3158 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04003159 if (!handle || err) {
3160 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04003162 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163
Jan Karad745a8c2014-05-26 11:56:53 -04003164 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003166 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003168 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003169 if (err) {
3170 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003172 }
Jan Kara05c2c002020-12-16 11:18:39 +01003173 lock_buffer(sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003174 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Kara05c2c002020-12-16 11:18:39 +01003175 ext4_superblock_csum_set(inode->i_sb);
3176 unlock_buffer(sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003177 mutex_unlock(&sbi->s_orphan_lock);
Jan Karaa3f5cf12020-12-16 11:18:44 +01003178 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003180 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003181 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07003182 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003184 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07003186 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003187 if (err) {
3188 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003190 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003191 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07003192 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003193 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003194 }
3195 if (err)
3196 goto out_brelse;
3197 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003198 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003199out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07003200 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201 return err;
3202
3203out_brelse:
3204 brelse(iloc.bh);
3205 goto out_err;
3206}
3207
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003208static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003209{
3210 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003211 struct inode *inode;
3212 struct buffer_head *bh;
3213 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003214 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003215
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003216 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3217 return -EIO;
3218
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003219 /* Initialize quotas before so that eventual writes go in
3220 * separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003221 retval = dquot_initialize(dir);
3222 if (retval)
3223 return retval;
3224 retval = dquot_initialize(d_inode(dentry));
3225 if (retval)
3226 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003227
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003228 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003229 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003230 if (IS_ERR(bh))
3231 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003232 if (!bh)
3233 goto end_rmdir;
3234
David Howells2b0143b2015-03-17 22:25:59 +00003235 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003237 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003238 if (le32_to_cpu(de->inode) != inode->i_ino)
3239 goto end_rmdir;
3240
3241 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003242 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003243 goto end_rmdir;
3244
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003245 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003246 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003247 if (IS_ERR(handle)) {
3248 retval = PTR_ERR(handle);
3249 handle = NULL;
3250 goto end_rmdir;
3251 }
3252
3253 if (IS_DIRSYNC(dir))
3254 ext4_handle_sync(handle);
3255
Mingming Cao617ba132006-10-11 01:20:53 -07003256 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003257 if (retval)
3258 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003259 if (!EXT4_DIR_LINK_EMPTY(inode))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003260 ext4_warning_inode(inode,
3261 "empty directory '%.*s' has too many links (%u)",
3262 dentry->d_name.len, dentry->d_name.name,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003263 inode->i_nlink);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05003264 inode_inc_iversion(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003265 clear_nlink(inode);
3266 /* There's no need to set i_disksize: the fact that i_nlink is
3267 * zero will ensure that the right thing happens during any
3268 * recovery. */
3269 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003270 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003271 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003272 retval = ext4_mark_inode_dirty(handle, inode);
3273 if (retval)
3274 goto end_rmdir;
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003275 ext4_dec_count(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003276 ext4_update_dx_flag(dir);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003277 ext4_fc_track_unlink(handle, dentry);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003278 retval = ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003280#ifdef CONFIG_UNICODE
3281 /* VFS negative dentries are incompatible with Encoding and
3282 * Case-insensitiveness. Eventually we'll want avoid
3283 * invalidating the dentries here, alongside with returning the
3284 * negative dentries at ext4_lookup(), when it is better
3285 * supported by the VFS for the CI case.
3286 */
3287 if (IS_CASEFOLDED(dir))
3288 d_invalidate(dentry);
3289#endif
3290
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003291end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003292 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003293 if (handle)
3294 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003295 return retval;
3296}
3297
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003298int __ext4_unlink(handle_t *handle, struct inode *dir, const struct qstr *d_name,
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003299 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003300{
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003301 int retval = -ENOENT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003302 struct buffer_head *bh;
3303 struct ext4_dir_entry_2 *de;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003304 int skip_remove_dentry = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003305
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003306 bh = ext4_find_entry(dir, d_name, &de, NULL);
3307 if (IS_ERR(bh))
3308 return PTR_ERR(bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003309
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003310 if (!bh)
3311 return -ENOENT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312
Yi Zhuange5f78152020-06-29 20:26:21 +08003313 if (le32_to_cpu(de->inode) != inode->i_ino) {
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003314 /*
3315 * It's okay if we find dont find dentry which matches
3316 * the inode. That's because it might have gotten
3317 * renamed to a different inode number
3318 */
3319 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
3320 skip_remove_dentry = 1;
3321 else
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003322 goto out;
Theodore Ts'o931b6862013-02-09 09:43:39 -05003323 }
3324
3325 if (IS_DIRSYNC(dir))
3326 ext4_handle_sync(handle);
3327
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003328 if (!skip_remove_dentry) {
3329 retval = ext4_delete_entry(handle, dir, de, bh);
3330 if (retval)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003331 goto out;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003332 dir->i_ctime = dir->i_mtime = current_time(dir);
3333 ext4_update_dx_flag(dir);
3334 retval = ext4_mark_inode_dirty(handle, dir);
3335 if (retval)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003336 goto out;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003337 } else {
3338 retval = 0;
3339 }
Theodore Ts'oc7df4a12019-11-11 22:18:13 -05003340 if (inode->i_nlink == 0)
3341 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003342 d_name->len, d_name->name);
Theodore Ts'oc7df4a12019-11-11 22:18:13 -05003343 else
3344 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003346 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003347 inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003348 retval = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003349
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003350out:
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003351 brelse(bh);
3352 return retval;
3353}
3354
3355static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3356{
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003357 handle_t *handle;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003358 int retval;
3359
3360 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3361 return -EIO;
3362
3363 trace_ext4_unlink_enter(dir, dentry);
3364 /*
3365 * Initialize quotas before so that eventual writes go
3366 * in separate transaction
3367 */
3368 retval = dquot_initialize(dir);
3369 if (retval)
3370 goto out_trace;
3371 retval = dquot_initialize(d_inode(dentry));
3372 if (retval)
3373 goto out_trace;
3374
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003375 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3376 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3377 if (IS_ERR(handle)) {
3378 retval = PTR_ERR(handle);
3379 goto out_trace;
3380 }
3381
3382 retval = __ext4_unlink(handle, dir, &dentry->d_name, d_inode(dentry));
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07003383 if (!retval)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003384 ext4_fc_track_unlink(handle, dentry);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003385#ifdef CONFIG_UNICODE
3386 /* VFS negative dentries are incompatible with Encoding and
3387 * Case-insensitiveness. Eventually we'll want avoid
3388 * invalidating the dentries here, alongside with returning the
3389 * negative dentries at ext4_lookup(), when it is better
3390 * supported by the VFS for the CI case.
3391 */
3392 if (IS_CASEFOLDED(dir))
3393 d_invalidate(dentry);
3394#endif
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003395 if (handle)
3396 ext4_journal_stop(handle);
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003397
Yi Zhuange5f78152020-06-29 20:26:21 +08003398out_trace:
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003399 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003400 return retval;
3401}
3402
Christian Brauner549c7292021-01-21 14:19:43 +01003403static int ext4_symlink(struct user_namespace *mnt_userns, struct inode *dir,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003404 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405{
3406 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003407 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003408 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003409 int credits;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003410 struct fscrypt_str disk_link;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003411
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003412 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3413 return -EIO;
3414
Eric Biggers78e10602018-01-11 22:10:40 -05003415 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3416 &disk_link);
3417 if (err)
3418 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003419
Jan Karaa7cdade2015-06-29 16:22:54 +02003420 err = dquot_initialize(dir);
3421 if (err)
Eric Biggers78e10602018-01-11 22:10:40 -05003422 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003423
Theodore Ts'of348c252015-04-16 01:55:00 -04003424 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003425 /*
3426 * For non-fast symlinks, we just allocate inode and put it on
3427 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003428 * group descriptor, sb, inode block, quota blocks, and
3429 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003430 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003431 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3432 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003433 } else {
3434 /*
3435 * Fast symlink. We have to add entry to directory
3436 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3437 * allocate new inode (bitmap, group descriptor, inode block,
3438 * quota blocks, sb is already counted in previous macros).
3439 */
3440 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003441 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003442 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003443
Christian Brauner14f3db52021-01-21 14:19:57 +01003444 inode = ext4_new_inode_start_handle(mnt_userns, dir, S_IFLNK|S_IRWXUGO,
Theodore Ts'o11395752013-02-09 16:27:09 -05003445 &dentry->d_name, 0, NULL,
3446 EXT4_HT_DIR, credits);
3447 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003448 if (IS_ERR(inode)) {
3449 if (handle)
3450 ext4_journal_stop(handle);
Eric Biggers78e10602018-01-11 22:10:40 -05003451 return PTR_ERR(inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003452 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003453
Eric Biggers78e10602018-01-11 22:10:40 -05003454 if (IS_ENCRYPTED(inode)) {
3455 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04003456 if (err)
Theodore Ts'of348c252015-04-16 01:55:00 -04003457 goto err_drop_inode;
Al Viroa7a67e82015-04-27 17:51:30 -04003458 inode->i_op = &ext4_encrypted_symlink_inode_operations;
Theodore Ts'of348c252015-04-16 01:55:00 -04003459 }
3460
3461 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Eric Biggers78e10602018-01-11 22:10:40 -05003462 if (!IS_ENCRYPTED(inode))
Al Viroa7a67e82015-04-27 17:51:30 -04003463 inode->i_op = &ext4_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003464 inode_nohighmem(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003465 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003466 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003467 * We cannot call page_symlink() with transaction started
3468 * because it calls into ext4_write_begin() which can wait
3469 * for transaction commit if we are running out of space
3470 * and thus we deadlock. So we have to stop transaction now
3471 * and restart it when symlink contents is written.
3472 *
3473 * To keep fs consistent in case of crash, we have to put inode
3474 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003475 */
Jan Karadf5e6222011-05-03 11:12:58 -04003476 drop_nlink(inode);
3477 err = ext4_orphan_add(handle, inode);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003478 if (handle)
3479 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003480 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003481 if (err)
3482 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003483 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003484 if (err)
3485 goto err_drop_inode;
3486 /*
3487 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3488 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3489 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003490 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003491 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3492 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3493 if (IS_ERR(handle)) {
3494 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003495 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003496 goto err_drop_inode;
3497 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003498 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003499 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003500 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003501 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003502 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003503 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003504 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Eric Biggers78e10602018-01-11 22:10:40 -05003505 if (!IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04003506 inode->i_op = &ext4_fast_symlink_inode_operations;
Al Viro75e75662015-05-02 10:13:58 -04003507 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3508 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003509 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3510 disk_link.len);
3511 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003512 }
Mingming Cao617ba132006-10-11 01:20:53 -07003513 EXT4_I(inode)->i_disksize = inode->i_size;
Jan Kara9b88f9f2019-11-05 17:44:11 +01003514 err = ext4_add_nondir(handle, dentry, &inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003515 if (handle)
3516 ext4_journal_stop(handle);
Jan Kara9b88f9f2019-11-05 17:44:11 +01003517 if (inode)
3518 iput(inode);
Eric Biggers78e10602018-01-11 22:10:40 -05003519 goto out_free_encrypted_link;
3520
Jan Karadf5e6222011-05-03 11:12:58 -04003521err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003522 if (handle)
3523 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003524 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003525 unlock_new_inode(inode);
3526 iput(inode);
Eric Biggers78e10602018-01-11 22:10:40 -05003527out_free_encrypted_link:
3528 if (disk_link.name != (unsigned char *)symname)
3529 kfree(disk_link.name);
Jan Karadf5e6222011-05-03 11:12:58 -04003530 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531}
3532
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003533int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534{
3535 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003536 int err, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003537retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003538 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3539 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003540 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003541 if (IS_ERR(handle))
3542 return PTR_ERR(handle);
3543
3544 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003545 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003546
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003547 inode->i_ctime = current_time(inode);
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003548 ext4_inc_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003549 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003550
Al Viro6b38e842008-12-30 02:03:31 -05003551 err = ext4_add_entry(handle, dentry, inode);
3552 if (!err) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003553 err = ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003554 /* this can happen only for tmpfile being
3555 * linked the first time
3556 */
3557 if (inode->i_nlink == 1)
3558 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003559 d_instantiate(dentry, inode);
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08003560 ext4_fc_track_link(handle, dentry);
Al Viro6b38e842008-12-30 02:03:31 -05003561 } else {
3562 drop_nlink(inode);
3563 iput(inode);
3564 }
Mingming Cao617ba132006-10-11 01:20:53 -07003565 ext4_journal_stop(handle);
3566 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003567 goto retry;
3568 return err;
3569}
3570
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003571static int ext4_link(struct dentry *old_dentry,
3572 struct inode *dir, struct dentry *dentry)
3573{
3574 struct inode *inode = d_inode(old_dentry);
3575 int err;
3576
3577 if (inode->i_nlink >= EXT4_LINK_MAX)
3578 return -EMLINK;
3579
3580 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3581 if (err)
3582 return err;
3583
3584 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3585 (!projid_eq(EXT4_I(dir)->i_projid,
3586 EXT4_I(old_dentry->d_inode)->i_projid)))
3587 return -EXDEV;
3588
3589 err = dquot_initialize(dir);
3590 if (err)
3591 return err;
3592 return __ext4_link(dir, inode, dentry);
3593}
Tao Ma32f7f222012-12-10 14:06:01 -05003594
3595/*
3596 * Try to find buffer head where contains the parent block.
3597 * It should be the inode block if it is inlined or the 1st block
3598 * if it is a normal dir.
3599 */
3600static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3601 struct inode *inode,
3602 int *retval,
3603 struct ext4_dir_entry_2 **parent_de,
3604 int *inlined)
3605{
3606 struct buffer_head *bh;
3607
3608 if (!ext4_has_inline_data(inode)) {
Theodore Ts'o4e19d6b2019-06-20 21:19:02 -04003609 /* The first directory block must not be a hole, so
3610 * treat it as DIRENT_HTREE
3611 */
3612 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003613 if (IS_ERR(bh)) {
3614 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003615 return NULL;
3616 }
3617 *parent_de = ext4_next_entry(
3618 (struct ext4_dir_entry_2 *)bh->b_data,
3619 inode->i_sb->s_blocksize);
3620 return bh;
3621 }
3622
3623 *inlined = 1;
3624 return ext4_get_first_inline_block(inode, parent_de, retval);
3625}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003626
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003627struct ext4_renament {
3628 struct inode *dir;
3629 struct dentry *dentry;
3630 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003631 bool is_dir;
3632 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003633
3634 /* entry for "dentry" */
3635 struct buffer_head *bh;
3636 struct ext4_dir_entry_2 *de;
3637 int inlined;
3638
3639 /* entry for ".." in inode if it's a directory */
3640 struct buffer_head *dir_bh;
3641 struct ext4_dir_entry_2 *parent_de;
3642 int dir_inlined;
3643};
3644
Miklos Szeredibd1af142014-04-01 17:08:44 +02003645static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3646{
3647 int retval;
3648
3649 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3650 &retval, &ent->parent_de,
3651 &ent->dir_inlined);
3652 if (!ent->dir_bh)
3653 return retval;
3654 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003655 return -EFSCORRUPTED;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003656 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3657 return ext4_journal_get_write_access(handle, ent->dir_bh);
3658}
3659
3660static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3661 unsigned dir_ino)
3662{
3663 int retval;
3664
3665 ent->parent_de->inode = cpu_to_le32(dir_ino);
3666 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3667 if (!ent->dir_inlined) {
3668 if (is_dx(ent->inode)) {
3669 retval = ext4_handle_dirty_dx_node(handle,
3670 ent->inode,
3671 ent->dir_bh);
3672 } else {
Theodore Ts'of036adb2019-06-21 15:49:26 -04003673 retval = ext4_handle_dirty_dirblock(handle, ent->inode,
3674 ent->dir_bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003675 }
3676 } else {
3677 retval = ext4_mark_inode_dirty(handle, ent->inode);
3678 }
3679 if (retval) {
3680 ext4_std_error(ent->dir->i_sb, retval);
3681 return retval;
3682 }
3683 return 0;
3684}
3685
3686static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3687 unsigned ino, unsigned file_type)
3688{
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003689 int retval, retval2;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003690
3691 BUFFER_TRACE(ent->bh, "get write access");
3692 retval = ext4_journal_get_write_access(handle, ent->bh);
3693 if (retval)
3694 return retval;
3695 ent->de->inode = cpu_to_le32(ino);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003696 if (ext4_has_feature_filetype(ent->dir->i_sb))
Miklos Szeredibd1af142014-04-01 17:08:44 +02003697 ent->de->file_type = file_type;
Jeff Laytonee73f9a2018-01-09 08:21:39 -05003698 inode_inc_iversion(ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003699 ent->dir->i_ctime = ent->dir->i_mtime =
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003700 current_time(ent->dir);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003701 retval = ext4_mark_inode_dirty(handle, ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003702 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3703 if (!ent->inlined) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003704 retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
3705 if (unlikely(retval2)) {
3706 ext4_std_error(ent->dir->i_sb, retval2);
3707 return retval2;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003708 }
3709 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003710 return retval;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003711}
3712
zhangyi (F)b7ff91f2021-03-03 21:17:02 +08003713static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
3714 unsigned ino, unsigned file_type)
3715{
3716 struct ext4_renament old = *ent;
3717 int retval = 0;
3718
3719 /*
3720 * old->de could have moved from under us during make indexed dir,
3721 * so the old->de may no longer valid and need to find it again
3722 * before reset old inode info.
3723 */
3724 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
3725 if (IS_ERR(old.bh))
3726 retval = PTR_ERR(old.bh);
3727 if (!old.bh)
3728 retval = -ENOENT;
3729 if (retval) {
3730 ext4_std_error(old.dir->i_sb, retval);
3731 return;
3732 }
3733
3734 ext4_setent(handle, &old, ino, file_type);
3735 brelse(old.bh);
3736}
3737
Miklos Szeredibd1af142014-04-01 17:08:44 +02003738static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3739 const struct qstr *d_name)
3740{
3741 int retval = -ENOENT;
3742 struct buffer_head *bh;
3743 struct ext4_dir_entry_2 *de;
3744
3745 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003746 if (IS_ERR(bh))
3747 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003748 if (bh) {
3749 retval = ext4_delete_entry(handle, dir, de, bh);
3750 brelse(bh);
3751 }
3752 return retval;
3753}
3754
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003755static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3756 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003757{
3758 int retval;
3759 /*
3760 * ent->de could have moved from under us during htree split, so make
3761 * sure that we are deleting the right entry. We might also be pointing
3762 * to a stale entry in the unused part of ent->bh so just checking inum
3763 * and the name isn't enough.
3764 */
3765 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3766 ent->de->name_len != ent->dentry->d_name.len ||
3767 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003768 ent->de->name_len) ||
3769 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003770 retval = ext4_find_delete_entry(handle, ent->dir,
3771 &ent->dentry->d_name);
3772 } else {
3773 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3774 if (retval == -ENOENT) {
3775 retval = ext4_find_delete_entry(handle, ent->dir,
3776 &ent->dentry->d_name);
3777 }
3778 }
3779
3780 if (retval) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003781 ext4_warning_inode(ent->dir,
3782 "Deleting old file: nlink %d, error=%d",
3783 ent->dir->i_nlink, retval);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003784 }
3785}
3786
Miklos Szeredibd429982014-04-01 17:08:44 +02003787static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3788{
3789 if (ent->dir_nlink_delta) {
3790 if (ent->dir_nlink_delta == -1)
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003791 ext4_dec_count(ent->dir);
Miklos Szeredibd429982014-04-01 17:08:44 +02003792 else
Nikolay Borisov15ed2852020-08-26 16:31:16 +03003793 ext4_inc_count(ent->dir);
Miklos Szeredibd429982014-04-01 17:08:44 +02003794 ext4_mark_inode_dirty(handle, ent->dir);
3795 }
3796}
3797
Christian Brauner14f3db52021-01-21 14:19:57 +01003798static struct inode *ext4_whiteout_for_rename(struct user_namespace *mnt_userns,
3799 struct ext4_renament *ent,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003800 int credits, handle_t **h)
3801{
3802 struct inode *wh;
3803 handle_t *handle;
3804 int retries = 0;
3805
3806 /*
3807 * for inode block, sb block, group summaries,
3808 * and inode bitmap
3809 */
3810 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3811 EXT4_XATTR_TRANS_BLOCKS + 4);
3812retry:
Christian Brauner14f3db52021-01-21 14:19:57 +01003813 wh = ext4_new_inode_start_handle(mnt_userns, ent->dir,
3814 S_IFCHR | WHITEOUT_MODE,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003815 &ent->dentry->d_name, 0, NULL,
3816 EXT4_HT_DIR, credits);
3817
3818 handle = ext4_journal_current_handle();
3819 if (IS_ERR(wh)) {
3820 if (handle)
3821 ext4_journal_stop(handle);
3822 if (PTR_ERR(wh) == -ENOSPC &&
3823 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3824 goto retry;
3825 } else {
3826 *h = handle;
3827 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3828 wh->i_op = &ext4_special_inode_operations;
3829 }
3830 return wh;
3831}
3832
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003833/*
3834 * Anybody can rename anything with this: the permission checks are left to the
3835 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003836 *
3837 * n.b. old_{dentry,inode) refers to the source dentry/inode
3838 * while new_{dentry,inode) refers to the destination dentry/inode
3839 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003840 */
Christian Brauner14f3db52021-01-21 14:19:57 +01003841static int ext4_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
3842 struct dentry *old_dentry, struct inode *new_dir,
3843 struct dentry *new_dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003844{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003845 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003846 struct ext4_renament old = {
3847 .dir = old_dir,
3848 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003849 .inode = d_inode(old_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003850 };
3851 struct ext4_renament new = {
3852 .dir = new_dir,
3853 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003854 .inode = d_inode(new_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003855 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003856 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003857 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003858 struct inode *whiteout = NULL;
3859 int credits;
3860 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003861
Theodore Ts'ob50282f2018-08-27 01:47:09 -04003862 if (new.inode && new.inode->i_nlink == 0) {
3863 EXT4_ERROR_INODE(new.inode,
3864 "target of rename is already freed");
3865 return -EFSCORRUPTED;
3866 }
3867
Li Xi040cb372016-01-08 16:01:21 -05003868 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3869 (!projid_eq(EXT4_I(new_dir)->i_projid,
3870 EXT4_I(old_dentry->d_inode)->i_projid)))
3871 return -EXDEV;
3872
Jan Karaa7cdade2015-06-29 16:22:54 +02003873 retval = dquot_initialize(old.dir);
3874 if (retval)
3875 return retval;
3876 retval = dquot_initialize(new.dir);
3877 if (retval)
3878 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879
3880 /* Initialize quotas before so that eventual writes go
3881 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003882 if (new.inode) {
3883 retval = dquot_initialize(new.inode);
3884 if (retval)
3885 return retval;
3886 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003887
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003888 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003889 if (IS_ERR(old.bh))
3890 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003891 /*
3892 * Check for inode number is _not_ due to possible IO errors.
3893 * We might rmdir the source, keep it as pwd of some process
3894 * and merrily kill the link to whatever was created under the
3895 * same name. Goodbye sticky bit ;-<
3896 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003897 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003898 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
zhangyi (F)5dccdc52021-03-03 21:17:03 +08003899 goto release_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003900
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003901 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3902 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003903 if (IS_ERR(new.bh)) {
3904 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003905 new.bh = NULL;
zhangyi (F)5dccdc52021-03-03 21:17:03 +08003906 goto release_bh;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003907 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003908 if (new.bh) {
3909 if (!new.inode) {
3910 brelse(new.bh);
3911 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003912 }
3913 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003914 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3915 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003916
Miklos Szeredicd808de2014-10-24 00:14:37 +02003917 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3918 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3919 if (!(flags & RENAME_WHITEOUT)) {
3920 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003921 if (IS_ERR(handle)) {
3922 retval = PTR_ERR(handle);
zhangyi (F)5dccdc52021-03-03 21:17:03 +08003923 goto release_bh;
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003924 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003925 } else {
Christian Brauner14f3db52021-01-21 14:19:57 +01003926 whiteout = ext4_whiteout_for_rename(mnt_userns, &old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003927 if (IS_ERR(whiteout)) {
3928 retval = PTR_ERR(whiteout);
zhangyi (F)5dccdc52021-03-03 21:17:03 +08003929 goto release_bh;
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003930 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003931 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003932
yangerkun6b4b8e62021-01-05 14:28:57 +08003933 old_file_type = old.de->file_type;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003934 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003935 ext4_handle_sync(handle);
3936
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003937 if (S_ISDIR(old.inode->i_mode)) {
3938 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003939 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003940 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003942 } else {
3943 retval = -EMLINK;
3944 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3945 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003947 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003948 if (retval)
3949 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003951 /*
3952 * If we're renaming a file within an inline_data dir and adding or
3953 * setting the new dirent causes a conversion from inline_data to
3954 * extents/blockmap, we need to force the dirent delete code to
3955 * re-read the directory, or else we end up trying to delete a dirent
3956 * from what is now the extent tree root (or a block map).
3957 */
3958 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3959 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003960
Miklos Szeredicd808de2014-10-24 00:14:37 +02003961 if (whiteout) {
3962 /*
3963 * Do this before adding a new entry, so the old entry is sure
3964 * to be still pointing to the valid old entry.
3965 */
3966 retval = ext4_setent(handle, &old, whiteout->i_ino,
3967 EXT4_FT_CHRDEV);
3968 if (retval)
3969 goto end_rename;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003970 retval = ext4_mark_inode_dirty(handle, whiteout);
3971 if (unlikely(retval))
3972 goto end_rename;
Harshad Shirwadkar8210bb22021-03-16 15:19:21 -07003973
Miklos Szeredicd808de2014-10-24 00:14:37 +02003974 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003975 if (!new.bh) {
3976 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003977 if (retval)
3978 goto end_rename;
3979 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003980 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003981 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003982 if (retval)
3983 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003985 if (force_reread)
3986 force_reread = !ext4_test_inode_flag(new.dir,
3987 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003988
3989 /*
3990 * Like most other Unix systems, set the ctime for inodes on a
3991 * rename.
3992 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003993 old.inode->i_ctime = current_time(old.inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003994 retval = ext4_mark_inode_dirty(handle, old.inode);
3995 if (unlikely(retval))
3996 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003997
Miklos Szeredicd808de2014-10-24 00:14:37 +02003998 if (!whiteout) {
3999 /*
4000 * ok, that's it
4001 */
4002 ext4_rename_delete(handle, &old, force_reread);
4003 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004004
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004005 if (new.inode) {
Nikolay Borisov15ed2852020-08-26 16:31:16 +03004006 ext4_dec_count(new.inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004007 new.inode->i_ctime = current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004008 }
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004009 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004010 ext4_update_dx_flag(old.dir);
4011 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02004012 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4013 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05004014 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02004015
Nikolay Borisov15ed2852020-08-26 16:31:16 +03004016 ext4_dec_count(old.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004017 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04004018 /* checked ext4_empty_dir above, can't have another
4019 * parent, ext4_dec_count() won't work for many-linked
4020 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004021 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004022 } else {
Nikolay Borisov15ed2852020-08-26 16:31:16 +03004023 ext4_inc_count(new.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004024 ext4_update_dx_flag(new.dir);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004025 retval = ext4_mark_inode_dirty(handle, new.dir);
4026 if (unlikely(retval))
4027 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004028 }
4029 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004030 retval = ext4_mark_inode_dirty(handle, old.dir);
4031 if (unlikely(retval))
4032 goto end_rename;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004033
4034 if (S_ISDIR(old.inode->i_mode)) {
4035 /*
4036 * We disable fast commits here that's because the
4037 * replay code is not yet capable of changing dot dot
4038 * dirents in directories.
4039 */
4040 ext4_fc_mark_ineligible(old.inode->i_sb,
4041 EXT4_FC_REASON_RENAME_DIR);
4042 } else {
4043 if (new.inode)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08004044 ext4_fc_track_unlink(handle, new.dentry);
4045 __ext4_fc_track_link(handle, old.inode, new.dentry);
4046 __ext4_fc_track_unlink(handle, old.inode, old.dentry);
Harshad Shirwadkar8210bb22021-03-16 15:19:21 -07004047 if (whiteout)
4048 __ext4_fc_track_create(handle, whiteout, old.dentry);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004049 }
4050
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004051 if (new.inode) {
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004052 retval = ext4_mark_inode_dirty(handle, new.inode);
4053 if (unlikely(retval))
4054 goto end_rename;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004055 if (!new.inode->i_nlink)
4056 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004057 }
4058 retval = 0;
4059
4060end_rename:
yangerkun6b4b8e62021-01-05 14:28:57 +08004061 if (whiteout) {
4062 if (retval) {
zhangyi (F)b7ff91f2021-03-03 21:17:02 +08004063 ext4_resetent(handle, &old,
4064 old.inode->i_ino, old_file_type);
yangerkun6b4b8e62021-01-05 14:28:57 +08004065 drop_nlink(whiteout);
zhangyi (F)5dccdc52021-03-03 21:17:03 +08004066 ext4_orphan_add(handle, whiteout);
yangerkun6b4b8e62021-01-05 14:28:57 +08004067 }
4068 unlock_new_inode(whiteout);
zhangyi (F)5dccdc52021-03-03 21:17:03 +08004069 ext4_journal_stop(handle);
yangerkun6b4b8e62021-01-05 14:28:57 +08004070 iput(whiteout);
zhangyi (F)5dccdc52021-03-03 21:17:03 +08004071 } else {
4072 ext4_journal_stop(handle);
yangerkun6b4b8e62021-01-05 14:28:57 +08004073 }
zhangyi (F)5dccdc52021-03-03 21:17:03 +08004074release_bh:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02004075 brelse(old.dir_bh);
4076 brelse(old.bh);
4077 brelse(new.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004078 return retval;
4079}
4080
Miklos Szeredibd429982014-04-01 17:08:44 +02004081static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
4082 struct inode *new_dir, struct dentry *new_dentry)
4083{
4084 handle_t *handle = NULL;
4085 struct ext4_renament old = {
4086 .dir = old_dir,
4087 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00004088 .inode = d_inode(old_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02004089 };
4090 struct ext4_renament new = {
4091 .dir = new_dir,
4092 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00004093 .inode = d_inode(new_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02004094 };
4095 u8 new_file_type;
4096 int retval;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004097 struct timespec64 ctime;
Miklos Szeredibd429982014-04-01 17:08:44 +02004098
Li Xi040cb372016-01-08 16:01:21 -05004099 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
4100 !projid_eq(EXT4_I(new_dir)->i_projid,
4101 EXT4_I(old_dentry->d_inode)->i_projid)) ||
4102 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
4103 !projid_eq(EXT4_I(old_dir)->i_projid,
4104 EXT4_I(new_dentry->d_inode)->i_projid)))
4105 return -EXDEV;
4106
Jan Karaa7cdade2015-06-29 16:22:54 +02004107 retval = dquot_initialize(old.dir);
4108 if (retval)
4109 return retval;
4110 retval = dquot_initialize(new.dir);
4111 if (retval)
4112 return retval;
Miklos Szeredibd429982014-04-01 17:08:44 +02004113
4114 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
4115 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04004116 if (IS_ERR(old.bh))
4117 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02004118 /*
4119 * Check for inode number is _not_ due to possible IO errors.
4120 * We might rmdir the source, keep it as pwd of some process
4121 * and merrily kill the link to whatever was created under the
4122 * same name. Goodbye sticky bit ;-<
4123 */
4124 retval = -ENOENT;
4125 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
4126 goto end_rename;
4127
4128 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
4129 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04004130 if (IS_ERR(new.bh)) {
4131 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04004132 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04004133 goto end_rename;
4134 }
Miklos Szeredibd429982014-04-01 17:08:44 +02004135
4136 /* RENAME_EXCHANGE case: old *and* new must both exist */
4137 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
4138 goto end_rename;
4139
4140 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
4141 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
4142 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04004143 if (IS_ERR(handle)) {
4144 retval = PTR_ERR(handle);
4145 handle = NULL;
4146 goto end_rename;
4147 }
Miklos Szeredibd429982014-04-01 17:08:44 +02004148
4149 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
4150 ext4_handle_sync(handle);
4151
4152 if (S_ISDIR(old.inode->i_mode)) {
4153 old.is_dir = true;
4154 retval = ext4_rename_dir_prepare(handle, &old);
4155 if (retval)
4156 goto end_rename;
4157 }
4158 if (S_ISDIR(new.inode->i_mode)) {
4159 new.is_dir = true;
4160 retval = ext4_rename_dir_prepare(handle, &new);
4161 if (retval)
4162 goto end_rename;
4163 }
4164
4165 /*
4166 * Other than the special case of overwriting a directory, parents'
4167 * nlink only needs to be modified if this is a cross directory rename.
4168 */
4169 if (old.dir != new.dir && old.is_dir != new.is_dir) {
4170 old.dir_nlink_delta = old.is_dir ? -1 : 1;
4171 new.dir_nlink_delta = -old.dir_nlink_delta;
4172 retval = -EMLINK;
4173 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
4174 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
4175 goto end_rename;
4176 }
4177
4178 new_file_type = new.de->file_type;
4179 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
4180 if (retval)
4181 goto end_rename;
4182
4183 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
4184 if (retval)
4185 goto end_rename;
4186
4187 /*
4188 * Like most other Unix systems, set the ctime for inodes on a
4189 * rename.
4190 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004191 ctime = current_time(old.inode);
4192 old.inode->i_ctime = ctime;
4193 new.inode->i_ctime = ctime;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004194 retval = ext4_mark_inode_dirty(handle, old.inode);
4195 if (unlikely(retval))
4196 goto end_rename;
4197 retval = ext4_mark_inode_dirty(handle, new.inode);
4198 if (unlikely(retval))
4199 goto end_rename;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004200 ext4_fc_mark_ineligible(new.inode->i_sb,
4201 EXT4_FC_REASON_CROSS_RENAME);
Miklos Szeredibd429982014-04-01 17:08:44 +02004202 if (old.dir_bh) {
4203 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4204 if (retval)
4205 goto end_rename;
4206 }
4207 if (new.dir_bh) {
4208 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
4209 if (retval)
4210 goto end_rename;
4211 }
4212 ext4_update_dir_count(handle, &old);
4213 ext4_update_dir_count(handle, &new);
4214 retval = 0;
4215
4216end_rename:
4217 brelse(old.dir_bh);
4218 brelse(new.dir_bh);
4219 brelse(old.bh);
4220 brelse(new.bh);
4221 if (handle)
4222 ext4_journal_stop(handle);
4223 return retval;
4224}
4225
Christian Brauner549c7292021-01-21 14:19:43 +01004226static int ext4_rename2(struct user_namespace *mnt_userns,
4227 struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004228 struct inode *new_dir, struct dentry *new_dentry,
4229 unsigned int flags)
4230{
Eric Biggers07543d12017-10-18 20:21:57 -04004231 int err;
4232
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05004233 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
4234 return -EIO;
4235
Miklos Szeredicd808de2014-10-24 00:14:37 +02004236 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004237 return -EINVAL;
4238
Eric Biggers07543d12017-10-18 20:21:57 -04004239 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
4240 flags);
4241 if (err)
4242 return err;
4243
Miklos Szeredibd429982014-04-01 17:08:44 +02004244 if (flags & RENAME_EXCHANGE) {
4245 return ext4_cross_rename(old_dir, old_dentry,
4246 new_dir, new_dentry);
4247 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02004248
Christian Brauner14f3db52021-01-21 14:19:57 +01004249 return ext4_rename(mnt_userns, old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004250}
4251
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252/*
4253 * directories can handle most operations...
4254 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08004255const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07004256 .create = ext4_create,
4257 .lookup = ext4_lookup,
4258 .link = ext4_link,
4259 .unlink = ext4_unlink,
4260 .symlink = ext4_symlink,
4261 .mkdir = ext4_mkdir,
4262 .rmdir = ext4_rmdir,
4263 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04004264 .tmpfile = ext4_tmpfile,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004265 .rename = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07004266 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01004267 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07004268 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02004269 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004270 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04004271 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004272};
4273
Arjan van de Ven754661f2007-02-12 00:55:38 -08004274const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07004275 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01004276 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07004277 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02004278 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004279 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280};