Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3 | * linux/fs/ext4/namei.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 6 | * Remy Card (card@masi.ibp.fr) |
| 7 | * Laboratoire MASI - Institut Blaise Pascal |
| 8 | * Universite Pierre et Marie Curie (Paris VI) |
| 9 | * |
| 10 | * from |
| 11 | * |
| 12 | * linux/fs/minix/namei.c |
| 13 | * |
| 14 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 15 | * |
| 16 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 17 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 18 | * Directory entry file type support and forward compatibility hooks |
| 19 | * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 |
| 20 | * Hash Tree Directory indexing (c) |
| 21 | * Daniel Phillips, 2001 |
| 22 | * Hash Tree Directory indexing porting |
| 23 | * Christopher Li, 2002 |
| 24 | * Hash Tree Directory indexing cleanup |
| 25 | * Theodore Ts'o, 2002 |
| 26 | */ |
| 27 | |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/pagemap.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 30 | #include <linux/time.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 31 | #include <linux/fcntl.h> |
| 32 | #include <linux/stat.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/quotaops.h> |
| 35 | #include <linux/buffer_head.h> |
| 36 | #include <linux/bio.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 37 | #include <linux/iversion.h> |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 38 | #include <linux/unicode.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 39 | #include "ext4.h" |
| 40 | #include "ext4_jbd2.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 41 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 42 | #include "xattr.h" |
| 43 | #include "acl.h" |
| 44 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 45 | #include <trace/events/ext4.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 46 | /* |
| 47 | * define how far ahead to read directories while searching them. |
| 48 | */ |
| 49 | #define NAMEI_RA_CHUNKS 2 |
| 50 | #define NAMEI_RA_BLOCKS 4 |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 51 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 52 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 53 | static struct buffer_head *ext4_append(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 54 | struct inode *inode, |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 55 | ext4_lblk_t *block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 56 | { |
| 57 | struct buffer_head *bh; |
Theodore Ts'o | 1c21502 | 2014-08-29 20:52:15 -0400 | [diff] [blame] | 58 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 59 | |
Theodore Ts'o | df981d0 | 2012-08-17 09:48:17 -0400 | [diff] [blame] | 60 | if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb && |
| 61 | ((inode->i_size >> 10) >= |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 62 | EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) |
| 63 | return ERR_PTR(-ENOSPC); |
Theodore Ts'o | df981d0 | 2012-08-17 09:48:17 -0400 | [diff] [blame] | 64 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 65 | *block = inode->i_size >> inode->i_sb->s_blocksize_bits; |
| 66 | |
Theodore Ts'o | c5e298a | 2015-06-21 01:25:29 -0400 | [diff] [blame] | 67 | bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE); |
Theodore Ts'o | 1c21502 | 2014-08-29 20:52:15 -0400 | [diff] [blame] | 68 | if (IS_ERR(bh)) |
| 69 | return bh; |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 70 | inode->i_size += inode->i_sb->s_blocksize; |
| 71 | EXT4_I(inode)->i_disksize = inode->i_size; |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 72 | BUFFER_TRACE(bh, "get_write_access"); |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 73 | 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 Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 78 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 79 | return bh; |
| 80 | } |
| 81 | |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 82 | static int ext4_dx_csum_verify(struct inode *inode, |
| 83 | struct ext4_dir_entry *dirent); |
| 84 | |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 85 | /* |
| 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'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 95 | typedef enum { |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 96 | EITHER, INDEX, DIRENT, DIRENT_HTREE |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 97 | } dirblock_type_t; |
| 98 | |
| 99 | #define ext4_read_dirblock(inode, block, type) \ |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 100 | __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__) |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 101 | |
| 102 | static struct buffer_head *__ext4_read_dirblock(struct inode *inode, |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 103 | ext4_lblk_t block, |
| 104 | dirblock_type_t type, |
| 105 | const char *func, |
| 106 | unsigned int line) |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 107 | { |
| 108 | struct buffer_head *bh; |
| 109 | struct ext4_dir_entry *dirent; |
Theodore Ts'o | 1c21502 | 2014-08-29 20:52:15 -0400 | [diff] [blame] | 110 | int is_dx_block = 0; |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 111 | |
Theodore Ts'o | 46f870d6 | 2019-11-21 13:09:43 -0500 | [diff] [blame] | 112 | 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'o | 1c21502 | 2014-08-29 20:52:15 -0400 | [diff] [blame] | 116 | if (IS_ERR(bh)) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 117 | __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'o | 1c21502 | 2014-08-29 20:52:15 -0400 | [diff] [blame] | 122 | |
| 123 | return bh; |
| 124 | } |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 125 | if (!bh && (type == INDEX || type == DIRENT_HTREE)) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 126 | ext4_error_inode(inode, func, line, block, |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 127 | "Directory hole found for htree %s block", |
| 128 | (type == INDEX) ? "index" : "leaf"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 129 | return ERR_PTR(-EFSCORRUPTED); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 130 | } |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 131 | if (!bh) |
| 132 | return NULL; |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 133 | 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 Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 144 | ext4_error_inode(inode, func, line, block, |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 145 | "directory leaf block found instead of index block"); |
Vasily Averin | de59fae | 2018-11-07 22:36:23 -0500 | [diff] [blame] | 146 | brelse(bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 147 | return ERR_PTR(-EFSCORRUPTED); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 148 | } |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 149 | if (!ext4_has_metadata_csum(inode->i_sb) || |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 150 | 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'o | 46f870d6 | 2019-11-21 13:09:43 -0500 | [diff] [blame] | 159 | if (ext4_dx_csum_verify(inode, dirent) && |
| 160 | !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC)) |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 161 | set_buffer_verified(bh); |
| 162 | else { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 163 | ext4_error_inode_err(inode, func, line, block, |
| 164 | EFSBADCRC, |
| 165 | "Directory index failed checksum"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 166 | brelse(bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 167 | return ERR_PTR(-EFSBADCRC); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 170 | if (!is_dx_block) { |
Theodore Ts'o | 46f870d6 | 2019-11-21 13:09:43 -0500 | [diff] [blame] | 171 | if (ext4_dirblock_csum_verify(inode, bh) && |
| 172 | !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC)) |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 173 | set_buffer_verified(bh); |
| 174 | else { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 175 | ext4_error_inode_err(inode, func, line, block, |
| 176 | EFSBADCRC, |
| 177 | "Directory block failed checksum"); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 178 | brelse(bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 179 | return ERR_PTR(-EFSBADCRC); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 180 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 181 | } |
| 182 | return bh; |
| 183 | } |
| 184 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 185 | #ifdef DX_DEBUG |
| 186 | #define dxtrace(command) command |
| 187 | #else |
| 188 | #define dxtrace(command) |
| 189 | #endif |
| 190 | |
| 191 | struct fake_dirent |
| 192 | { |
| 193 | __le32 inode; |
| 194 | __le16 rec_len; |
| 195 | u8 name_len; |
| 196 | u8 file_type; |
| 197 | }; |
| 198 | |
| 199 | struct dx_countlimit |
| 200 | { |
| 201 | __le16 limit; |
| 202 | __le16 count; |
| 203 | }; |
| 204 | |
| 205 | struct 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 | |
| 217 | struct 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. Silva | 6cfb061 | 2020-02-13 10:06:48 -0600 | [diff] [blame] | 232 | struct dx_entry entries[]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | struct dx_node |
| 236 | { |
| 237 | struct fake_dirent fake; |
Gustavo A. R. Silva | 6cfb061 | 2020-02-13 10:06:48 -0600 | [diff] [blame] | 238 | struct dx_entry entries[]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | |
| 242 | struct dx_frame |
| 243 | { |
| 244 | struct buffer_head *bh; |
| 245 | struct dx_entry *entries; |
| 246 | struct dx_entry *at; |
| 247 | }; |
| 248 | |
| 249 | struct dx_map_entry |
| 250 | { |
| 251 | u32 hash; |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 252 | u16 offs; |
| 253 | u16 size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
Darrick J. Wong | e615391 | 2012-04-29 18:23:10 -0400 | [diff] [blame] | 256 | /* |
| 257 | * This goes at the end of each htree block. |
| 258 | */ |
| 259 | struct dx_tail { |
| 260 | u32 dt_reserved; |
| 261 | __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */ |
| 262 | }; |
| 263 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 264 | static inline ext4_lblk_t dx_get_block(struct dx_entry *entry); |
| 265 | static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 266 | static inline unsigned dx_get_hash(struct dx_entry *entry); |
| 267 | static void dx_set_hash(struct dx_entry *entry, unsigned value); |
| 268 | static unsigned dx_get_count(struct dx_entry *entries); |
| 269 | static unsigned dx_get_limit(struct dx_entry *entries); |
| 270 | static void dx_set_count(struct dx_entry *entries, unsigned value); |
| 271 | static void dx_set_limit(struct dx_entry *entries, unsigned value); |
| 272 | static unsigned dx_root_limit(struct inode *dir, unsigned infosize); |
| 273 | static unsigned dx_node_limit(struct inode *dir); |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 274 | static struct dx_frame *dx_probe(struct ext4_filename *fname, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 275 | struct inode *dir, |
| 276 | struct dx_hash_info *hinfo, |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 277 | struct dx_frame *frame); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 278 | static void dx_release(struct dx_frame *frames); |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 279 | static 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 282 | static void dx_sort_map(struct dx_map_entry *map, unsigned count); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 283 | static 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); |
| 286 | static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base, |
| 287 | unsigned int blocksize); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 288 | static void dx_insert_block(struct dx_frame *frame, |
| 289 | u32 hash, ext4_lblk_t block); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 290 | static int ext4_htree_next_block(struct inode *dir, __u32 hash, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 291 | struct dx_frame *frame, |
| 292 | struct dx_frame *frames, |
| 293 | __u32 *start_hash); |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 294 | static struct buffer_head * ext4_dx_find_entry(struct inode *dir, |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 295 | struct ext4_filename *fname, |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 296 | struct ext4_dir_entry_2 **res_dir); |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 297 | static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, |
Theodore Ts'o | 56a0491 | 2016-01-08 16:00:31 -0500 | [diff] [blame] | 298 | struct inode *dir, struct inode *inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 299 | |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 300 | /* checksumming functions */ |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 301 | void ext4_initialize_dirent_tail(struct buffer_head *bh, |
| 302 | unsigned int blocksize) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 303 | { |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 304 | struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize); |
| 305 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 306 | 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 */ |
| 313 | static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode, |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 314 | struct buffer_head *bh) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 315 | { |
| 316 | struct ext4_dir_entry_tail *t; |
| 317 | |
| 318 | #ifdef PARANOID |
| 319 | struct ext4_dir_entry *d, *top; |
| 320 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 321 | d = (struct ext4_dir_entry *)bh->b_data; |
| 322 | top = (struct ext4_dir_entry *)(bh->b_data + |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 323 | (EXT4_BLOCK_SIZE(inode->i_sb) - |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 324 | sizeof(struct ext4_dir_entry_tail))); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 325 | 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'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 334 | t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb)); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 335 | #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'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 346 | static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 347 | { |
| 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 Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 356 | #define warn_no_space_for_csum(inode) \ |
| 357 | __warn_no_space_for_csum((inode), __func__, __LINE__) |
| 358 | |
| 359 | static void __warn_no_space_for_csum(struct inode *inode, const char *func, |
| 360 | unsigned int line) |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 361 | { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 362 | __ext4_warning_inode(inode, func, line, |
| 363 | "No space for directory leaf checksum. Please run e2fsck -D."); |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 366 | int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 367 | { |
| 368 | struct ext4_dir_entry_tail *t; |
| 369 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 370 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 371 | return 1; |
| 372 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 373 | t = get_dirent_tail(inode, bh); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 374 | if (!t) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 375 | warn_no_space_for_csum(inode); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 379 | if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data, |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 380 | (char *)t - bh->b_data)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 381 | return 0; |
| 382 | |
| 383 | return 1; |
| 384 | } |
| 385 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 386 | static void ext4_dirblock_csum_set(struct inode *inode, |
| 387 | struct buffer_head *bh) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 388 | { |
| 389 | struct ext4_dir_entry_tail *t; |
| 390 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 391 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 392 | return; |
| 393 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 394 | t = get_dirent_tail(inode, bh); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 395 | if (!t) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 396 | warn_no_space_for_csum(inode); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 400 | t->det_checksum = ext4_dirblock_csum(inode, bh->b_data, |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 401 | (char *)t - bh->b_data); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 404 | int ext4_handle_dirty_dirblock(handle_t *handle, |
| 405 | struct inode *inode, |
| 406 | struct buffer_head *bh) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 407 | { |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 408 | ext4_dirblock_csum_set(inode, bh); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 409 | return ext4_handle_dirty_metadata(handle, inode, bh); |
| 410 | } |
| 411 | |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 412 | static 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 | |
| 440 | static __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'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 445 | __u32 csum; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 446 | int size; |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 447 | __u32 dummy_csum = 0; |
| 448 | int offset = offsetof(struct dx_tail, dt_checksum); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 449 | |
| 450 | size = count_offset + (count * sizeof(struct dx_entry)); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 451 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 452 | csum = ext4_chksum(sbi, csum, (__u8 *)t, offset); |
| 453 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 454 | |
| 455 | return cpu_to_le32(csum); |
| 456 | } |
| 457 | |
| 458 | static 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 Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 465 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 466 | 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 Jeong | fa96454 | 2016-07-03 21:11:08 -0400 | [diff] [blame] | 471 | return 0; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 472 | } |
| 473 | limit = le16_to_cpu(c->limit); |
| 474 | count = le16_to_cpu(c->count); |
| 475 | if (count_offset + (limit * sizeof(struct dx_entry)) > |
| 476 | EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 477 | warn_no_space_for_csum(inode); |
Daeho Jeong | fa96454 | 2016-07-03 21:11:08 -0400 | [diff] [blame] | 478 | return 0; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 479 | } |
| 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 | |
| 488 | static 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 Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 494 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 495 | 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'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 506 | warn_no_space_for_csum(inode); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 507 | 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 | |
| 514 | static 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 522 | /* |
Li Zefan | f795e14 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 523 | * p is at least 6 bytes before the end of page |
| 524 | */ |
| 525 | static inline struct ext4_dir_entry_2 * |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 526 | ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize) |
Li Zefan | f795e14 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 527 | { |
| 528 | return (struct ext4_dir_entry_2 *)((char *)p + |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 529 | ext4_rec_len_from_disk(p->rec_len, blocksize)); |
Li Zefan | f795e14 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 533 | * Future: use high four bits of block for coalesce-on-delete flags |
| 534 | * Mask them off for now. |
| 535 | */ |
| 536 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 537 | static inline ext4_lblk_t dx_get_block(struct dx_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 538 | { |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 539 | return le32_to_cpu(entry->block) & 0x0fffffff; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 542 | static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 543 | { |
| 544 | entry->block = cpu_to_le32(value); |
| 545 | } |
| 546 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 547 | static inline unsigned dx_get_hash(struct dx_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 548 | { |
| 549 | return le32_to_cpu(entry->hash); |
| 550 | } |
| 551 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 552 | static inline void dx_set_hash(struct dx_entry *entry, unsigned value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 553 | { |
| 554 | entry->hash = cpu_to_le32(value); |
| 555 | } |
| 556 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 557 | static inline unsigned dx_get_count(struct dx_entry *entries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 558 | { |
| 559 | return le16_to_cpu(((struct dx_countlimit *) entries)->count); |
| 560 | } |
| 561 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 562 | static inline unsigned dx_get_limit(struct dx_entry *entries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 | { |
| 564 | return le16_to_cpu(((struct dx_countlimit *) entries)->limit); |
| 565 | } |
| 566 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 567 | static inline void dx_set_count(struct dx_entry *entries, unsigned value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 568 | { |
| 569 | ((struct dx_countlimit *) entries)->count = cpu_to_le16(value); |
| 570 | } |
| 571 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 572 | static inline void dx_set_limit(struct dx_entry *entries, unsigned value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 573 | { |
| 574 | ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value); |
| 575 | } |
| 576 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 577 | static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 578 | { |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 579 | 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. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 582 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 583 | if (ext4_has_metadata_csum(dir->i_sb)) |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 584 | entry_space -= sizeof(struct dx_tail); |
Li Zefan | d9c769b | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 585 | return entry_space / sizeof(struct dx_entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 588 | static inline unsigned dx_node_limit(struct inode *dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 589 | { |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 590 | unsigned int entry_space = dir->i_sb->s_blocksize - |
| 591 | ext4_dir_rec_len(0, dir); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 592 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 593 | if (ext4_has_metadata_csum(dir->i_sb)) |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 594 | entry_space -= sizeof(struct dx_tail); |
Li Zefan | d9c769b | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 595 | return entry_space / sizeof(struct dx_entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Debug |
| 600 | */ |
| 601 | #ifdef DX_DEBUG |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 602 | static void dx_show_index(char * label, struct dx_entry *entries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 603 | { |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 604 | int i, n = dx_get_count (entries); |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 605 | printk(KERN_DEBUG "%s index", label); |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 606 | for (i = 0; i < n; i++) { |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 607 | printk(KERN_CONT " %x->%lu", |
| 608 | i ? dx_get_hash(entries + i) : 0, |
| 609 | (unsigned long)dx_get_block(entries + i)); |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 610 | } |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 611 | printk(KERN_CONT "\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | struct stats |
| 615 | { |
| 616 | unsigned names; |
| 617 | unsigned space; |
| 618 | unsigned bcount; |
| 619 | }; |
| 620 | |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 621 | static 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 625 | { |
| 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 Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 637 | #ifdef CONFIG_FS_ENCRYPTION |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 638 | int len; |
| 639 | char *name; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 640 | struct fscrypt_str fname_crypto_str = |
| 641 | FSTR_INIT(NULL, 0); |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 642 | int res = 0; |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 643 | |
| 644 | name = de->name; |
| 645 | len = de->name_len; |
Eric Biggers | 91d0d89 | 2020-12-02 18:20:36 -0800 | [diff] [blame] | 646 | if (!IS_ENCRYPTED(dir)) { |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 647 | /* Directory is not encrypted */ |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 648 | ext4fs_dirhash(dir, de->name, |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 649 | de->name_len, &h); |
| 650 | printk("%*.s:(U)%x.%u ", len, |
| 651 | name, h.hash, |
| 652 | (unsigned) ((char *) de |
| 653 | - base)); |
| 654 | } else { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 655 | struct fscrypt_str de_name = |
| 656 | FSTR_INIT(name, len); |
| 657 | |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 658 | /* Directory is encrypted */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 659 | res = fscrypt_fname_alloc_buffer( |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 660 | len, &fname_crypto_str); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 661 | if (res) |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 662 | printk(KERN_WARNING "Error " |
| 663 | "allocating crypto " |
| 664 | "buffer--skipping " |
| 665 | "crypto\n"); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 666 | res = fscrypt_fname_disk_to_usr(dir, |
| 667 | 0, 0, &de_name, |
| 668 | &fname_crypto_str); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 669 | if (res) { |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 670 | 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 Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 680 | if (IS_CASEFOLDED(dir)) |
| 681 | h.hash = EXT4_DIRENT_HASH(de); |
| 682 | else |
| 683 | ext4fs_dirhash(dir, de->name, |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 684 | de->name_len, &h); |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 685 | printk("%*.s:(E)%x.%u ", len, name, |
| 686 | h.hash, (unsigned) ((char *) de |
| 687 | - base)); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 688 | fscrypt_fname_free_buffer( |
| 689 | &fname_crypto_str); |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 690 | } |
| 691 | #else |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 692 | int len = de->name_len; |
| 693 | char *name = de->name; |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 694 | ext4fs_dirhash(dir, de->name, de->name_len, &h); |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 695 | printk("%*.s:%x.%u ", len, name, h.hash, |
Bernd Schubert | 265c6a0 | 2011-07-16 19:41:23 -0400 | [diff] [blame] | 696 | (unsigned) ((char *) de - base)); |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 697 | #endif |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 698 | } |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 699 | space += ext4_dir_rec_len(de->name_len, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 700 | names++; |
| 701 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 702 | de = ext4_next_entry(de, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 703 | } |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 704 | printk(KERN_CONT "(%i)\n", names); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 705 | return (struct stats) { names, space, 1 }; |
| 706 | } |
| 707 | |
| 708 | struct 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'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 712 | unsigned count = dx_get_count(entries), names = 0, space = 0, i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 713 | unsigned bcount = 0; |
| 714 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 715 | printk("%i indexed blocks...\n", count); |
| 716 | for (i = 0; i < count; i++, entries++) |
| 717 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 718 | ext4_lblk_t block = dx_get_block(entries); |
| 719 | ext4_lblk_t hash = i ? dx_get_hash(entries): 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 720 | 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'o | 1c21502 | 2014-08-29 20:52:15 -0400 | [diff] [blame] | 723 | bh = ext4_bread(NULL,dir, block, 0); |
| 724 | if (!bh || IS_ERR(bh)) |
| 725 | continue; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 726 | stats = levels? |
| 727 | dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1): |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 728 | dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) |
| 729 | bh->b_data, blocksize, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 730 | names += stats.names; |
| 731 | space += stats.space; |
| 732 | bcount += stats.bcount; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 733 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 734 | } |
| 735 | if (bcount) |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 736 | printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n", |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 737 | levels ? "" : " ", names, space/bcount, |
| 738 | (space/bcount)*100/blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 739 | return (struct stats) { names, space, bcount}; |
| 740 | } |
Vinicius Tinti | c6c818e | 2021-02-02 16:28:37 +0000 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | * Linear search cross check |
| 744 | */ |
| 745 | static 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 */ |
| 759 | static inline void htree_rep_invariant_check(struct dx_entry *at, |
| 760 | struct dx_entry *target, |
| 761 | u32 hash, unsigned int n) |
| 762 | { |
| 763 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 764 | #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 | */ |
| 775 | static struct dx_frame * |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 776 | dx_probe(struct ext4_filename *fname, struct inode *dir, |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 777 | struct dx_hash_info *hinfo, struct dx_frame *frame_in) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 778 | { |
| 779 | unsigned count, indirect; |
| 780 | struct dx_entry *at, *entries, *p, *q, *m; |
| 781 | struct dx_root *root; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 782 | struct dx_frame *frame = frame_in; |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 783 | struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 784 | u32 hash; |
| 785 | |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 786 | memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0])); |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 787 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 792 | if (root->info.hash_version != DX_HASH_TEA && |
| 793 | root->info.hash_version != DX_HASH_HALF_MD4 && |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 794 | root->info.hash_version != DX_HASH_LEGACY && |
| 795 | root->info.hash_version != DX_HASH_SIPHASH) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 796 | ext4_warning_inode(dir, "Unrecognised inode hash code %u", |
| 797 | root->info.hash_version); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 798 | goto fail; |
| 799 | } |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 800 | 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'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 813 | if (fname) |
| 814 | hinfo = &fname->hinfo; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 815 | hinfo->hash_version = root->info.hash_version; |
Theodore Ts'o | f99b258 | 2008-10-28 13:21:44 -0400 | [diff] [blame] | 816 | if (hinfo->hash_version <= DX_HASH_TEA) |
| 817 | hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 818 | hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed; |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 819 | /* hash is already computed for encrypted casefolded directory */ |
| 820 | if (fname && fname_name(fname) && |
| 821 | !(IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir))) |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 822 | ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 823 | hash = hinfo->hash; |
| 824 | |
| 825 | if (root->info.unused_flags & 1) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 826 | ext4_warning_inode(dir, "Unimplemented hash flags: %#06x", |
| 827 | root->info.unused_flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 828 | goto fail; |
| 829 | } |
| 830 | |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 831 | indirect = root->info.indirect_levels; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 832 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 841 | goto fail; |
| 842 | } |
| 843 | |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 844 | entries = (struct dx_entry *)(((char *)&root->info) + |
| 845 | root->info.info_length); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 846 | |
| 847 | if (dx_get_limit(entries) != dx_root_limit(dir, |
| 848 | root->info.info_length)) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 849 | 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 Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 852 | goto fail; |
| 853 | } |
| 854 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 855 | dxtrace(printk("Look up %x", hash)); |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 856 | while (1) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 857 | count = dx_get_count(entries); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 858 | if (!count || count > dx_get_limit(entries)) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 859 | ext4_warning_inode(dir, |
| 860 | "dx entry: count %u beyond limit %u", |
| 861 | count, dx_get_limit(entries)); |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 862 | goto fail; |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 865 | p = entries + 1; |
| 866 | q = entries + count - 1; |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 867 | while (p <= q) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 868 | m = p + (q - p) / 2; |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 869 | dxtrace(printk(KERN_CONT ".")); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 870 | if (dx_get_hash(m) > hash) |
| 871 | q = m - 1; |
| 872 | else |
| 873 | p = m + 1; |
| 874 | } |
| 875 | |
Vinicius Tinti | c6c818e | 2021-02-02 16:28:37 +0000 | [diff] [blame] | 876 | htree_rep_invariant_check(entries, p, hash, count - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 877 | |
| 878 | at = p - 1; |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 879 | dxtrace(printk(KERN_CONT " %x->%u\n", |
| 880 | at == entries ? 0 : dx_get_hash(at), |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 881 | dx_get_block(at))); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 882 | frame->entries = entries; |
| 883 | frame->at = at; |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 884 | 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 Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 892 | } |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 893 | entries = ((struct dx_node *) frame->bh->b_data)->entries; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 894 | |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 895 | 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'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 899 | goto fail; |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 900 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 901 | } |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 902 | fail: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 903 | while (frame >= frame_in) { |
| 904 | brelse(frame->bh); |
| 905 | frame--; |
| 906 | } |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 907 | |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 908 | if (ret_err == ERR_PTR(ERR_BAD_DX_DIR)) |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 909 | ext4_warning_inode(dir, |
| 910 | "Corrupt directory, running e2fsck is recommended"); |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 911 | return ret_err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 914 | static void dx_release(struct dx_frame *frames) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 915 | { |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 916 | struct dx_root_info *info; |
| 917 | int i; |
Sahitya Tummala | 08fc98a | 2019-05-10 22:00:33 -0400 | [diff] [blame] | 918 | unsigned int indirect_levels; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 919 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 920 | if (frames[0].bh == NULL) |
| 921 | return; |
| 922 | |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 923 | info = &((struct dx_root *)frames[0].bh->b_data)->info; |
Sahitya Tummala | 08fc98a | 2019-05-10 22:00:33 -0400 | [diff] [blame] | 924 | /* save local copy, "info" may be freed after brelse() */ |
| 925 | indirect_levels = info->indirect_levels; |
| 926 | for (i = 0; i <= indirect_levels; i++) { |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 927 | if (frames[i].bh == NULL) |
| 928 | break; |
| 929 | brelse(frames[i].bh); |
| 930 | frames[i].bh = NULL; |
| 931 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 932 | } |
| 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 951 | static int ext4_htree_next_block(struct inode *dir, __u32 hash, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 952 | 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'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 958 | int num_frames = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 959 | __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'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 997 | bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX); |
| 998 | if (IS_ERR(bh)) |
| 999 | return PTR_ERR(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1000 | p++; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1001 | brelse(p->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1002 | p->bh = bh; |
| 1003 | p->at = p->entries = ((struct dx_node *) bh->b_data)->entries; |
| 1004 | } |
| 1005 | return 1; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1010 | * 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 | */ |
| 1014 | static int htree_dirblock_to_tree(struct file *dir_file, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1015 | struct inode *dir, ext4_lblk_t block, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1016 | struct dx_hash_info *hinfo, |
| 1017 | __u32 start_hash, __u32 start_minor_hash) |
| 1018 | { |
| 1019 | struct buffer_head *bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1020 | struct ext4_dir_entry_2 *de, *top; |
Carlos Maiolino | 90b0a97 | 2012-09-17 23:39:12 -0400 | [diff] [blame] | 1021 | int err = 0, count = 0; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 1022 | struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1023 | int csum = ext4_has_metadata_csum(dir->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1024 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1025 | dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n", |
| 1026 | (unsigned long)block)); |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 1027 | bh = ext4_read_dirblock(dir, block, DIRENT_HTREE); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 1028 | if (IS_ERR(bh)) |
| 1029 | return PTR_ERR(bh); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1030 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1031 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1032 | /* csum entries are not larger in the casefolded encrypted case */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1033 | top = (struct ext4_dir_entry_2 *) ((char *) de + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1034 | dir->i_sb->s_blocksize - |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1035 | ext4_dir_rec_len(0, |
| 1036 | csum ? NULL : dir)); |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1037 | /* Check if the directory is encrypted */ |
Chandan Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 1038 | if (IS_ENCRYPTED(dir)) { |
Eric Biggers | ec0caa97 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 1039 | err = fscrypt_prepare_readdir(dir); |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 1040 | if (err < 0) { |
| 1041 | brelse(bh); |
| 1042 | return err; |
| 1043 | } |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 1044 | err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN, |
| 1045 | &fname_crypto_str); |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1046 | if (err < 0) { |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1047 | brelse(bh); |
| 1048 | return err; |
| 1049 | } |
| 1050 | } |
Eric Biggers | 64c314f | 2019-12-09 13:32:25 -0800 | [diff] [blame] | 1051 | |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1052 | for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) { |
Theodore Ts'o | f7c2117 | 2011-01-10 12:10:55 -0500 | [diff] [blame] | 1053 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 1054 | bh->b_data, bh->b_size, |
Theodore Ts'o | cad3f00 | 2010-12-19 22:07:02 -0500 | [diff] [blame] | 1055 | (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb)) |
| 1056 | + ((char *)de - bh->b_data))) { |
Al Viro | 64cb927 | 2013-07-01 08:12:38 -0400 | [diff] [blame] | 1057 | /* silently ignore the rest of the block */ |
| 1058 | break; |
Eric Sandeen | e6c4021 | 2006-12-06 20:36:28 -0800 | [diff] [blame] | 1059 | } |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1060 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1071 | 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 Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 1077 | if (!IS_ENCRYPTED(dir)) { |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1078 | 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'o | d229959 | 2015-05-18 13:15:47 -0400 | [diff] [blame] | 1084 | int save_len = fname_crypto_str.len; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 1085 | struct fscrypt_str de_name = FSTR_INIT(de->name, |
| 1086 | de->name_len); |
Theodore Ts'o | d229959 | 2015-05-18 13:15:47 -0400 | [diff] [blame] | 1087 | |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1088 | /* Directory is encrypted */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 1089 | err = fscrypt_fname_disk_to_usr(dir, hinfo->hash, |
| 1090 | hinfo->minor_hash, &de_name, |
| 1091 | &fname_crypto_str); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 1092 | if (err) { |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1093 | 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'o | d229959 | 2015-05-18 13:15:47 -0400 | [diff] [blame] | 1099 | fname_crypto_str.len = save_len; |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1100 | } |
Theodore Ts'o | 2f61830 | 2015-04-12 00:56:26 -0400 | [diff] [blame] | 1101 | if (err != 0) { |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1102 | count = err; |
| 1103 | goto errout; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1104 | } |
| 1105 | count++; |
| 1106 | } |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1107 | errout: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1108 | brelse(bh); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 1109 | fscrypt_fname_free_buffer(&fname_crypto_str); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1110 | 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1122 | int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1123 | __u32 start_minor_hash, __u32 *next_hash) |
| 1124 | { |
| 1125 | struct dx_hash_info hinfo; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1126 | struct ext4_dir_entry_2 *de; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 1127 | struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1128 | struct inode *dir; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1129 | ext4_lblk_t block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1130 | int count = 0; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1131 | int ret, err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1132 | __u32 hashval; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 1133 | struct fscrypt_str tmp_str; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1134 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 1135 | dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n", |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1136 | start_hash, start_minor_hash)); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1137 | dir = file_inode(dir_file); |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1138 | if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) { |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1139 | 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'o | f99b258 | 2008-10-28 13:21:44 -0400 | [diff] [blame] | 1144 | if (hinfo.hash_version <= DX_HASH_TEA) |
| 1145 | hinfo.hash_version += |
| 1146 | EXT4_SB(dir->i_sb)->s_hash_unsigned; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1147 | hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; |
Tao Ma | 8af0f08 | 2013-04-19 17:53:09 -0400 | [diff] [blame] | 1148 | if (ext4_has_inline_data(dir)) { |
| 1149 | int has_inline_data = 1; |
Theodore Ts'o | 7633b08 | 2019-06-21 21:57:00 -0400 | [diff] [blame] | 1150 | count = ext4_inlinedir_to_tree(dir_file, dir, 0, |
| 1151 | &hinfo, start_hash, |
| 1152 | start_minor_hash, |
| 1153 | &has_inline_data); |
Tao Ma | 8af0f08 | 2013-04-19 17:53:09 -0400 | [diff] [blame] | 1154 | if (has_inline_data) { |
| 1155 | *next_hash = ~0; |
| 1156 | return count; |
| 1157 | } |
| 1158 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1159 | 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'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 1166 | frame = dx_probe(NULL, dir, &hinfo, frames); |
| 1167 | if (IS_ERR(frame)) |
| 1168 | return PTR_ERR(frame); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1169 | |
| 1170 | /* Add '.' and '..' from the htree header */ |
| 1171 | if (!start_hash && !start_minor_hash) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1172 | de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; |
Theodore Ts'o | 2f61830 | 2015-04-12 00:56:26 -0400 | [diff] [blame] | 1173 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1178 | goto errout; |
| 1179 | count++; |
| 1180 | } |
| 1181 | if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1182 | de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1183 | de = ext4_next_entry(de, dir->i_sb->s_blocksize); |
Theodore Ts'o | 2f61830 | 2015-04-12 00:56:26 -0400 | [diff] [blame] | 1184 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1189 | goto errout; |
| 1190 | count++; |
| 1191 | } |
| 1192 | |
| 1193 | while (1) { |
Theodore Ts'o | 1f60fbe | 2016-04-23 22:50:07 -0400 | [diff] [blame] | 1194 | if (fatal_signal_pending(current)) { |
| 1195 | err = -ERESTARTSYS; |
| 1196 | goto errout; |
| 1197 | } |
| 1198 | cond_resched(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1199 | 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1208 | ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1209 | 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'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1225 | dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, " |
| 1226 | "next hash: %x\n", count, *next_hash)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1227 | return count; |
| 1228 | errout: |
| 1229 | dx_release(frames); |
| 1230 | return (err); |
| 1231 | } |
| 1232 | |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1233 | static inline int search_dirblock(struct buffer_head *bh, |
| 1234 | struct inode *dir, |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1235 | struct ext4_filename *fname, |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1236 | unsigned int offset, |
| 1237 | struct ext4_dir_entry_2 **res_dir) |
| 1238 | { |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1239 | return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir, |
Eric Biggers | d6b9755 | 2017-05-24 18:10:49 -0400 | [diff] [blame] | 1240 | fname, offset, res_dir); |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1243 | /* |
| 1244 | * Directory block splitting, compacting |
| 1245 | */ |
| 1246 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1247 | /* |
| 1248 | * Create map of hash values, offsets, and sizes, stored at end of block. |
| 1249 | * Returns number of entries mapped. |
| 1250 | */ |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1251 | static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, |
| 1252 | unsigned blocksize, struct dx_hash_info *hinfo, |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1253 | struct dx_map_entry *map_tail) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1254 | { |
| 1255 | int count = 0; |
| 1256 | char *base = (char *) de; |
| 1257 | struct dx_hash_info h = *hinfo; |
| 1258 | |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1259 | while ((char *) de < base + blocksize) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1260 | if (de->name_len && de->inode) { |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1261 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1265 | map_tail--; |
| 1266 | map_tail->hash = h.hash; |
Toshiyuki Okajima | 9aee228 | 2009-06-08 12:41:35 -0400 | [diff] [blame] | 1267 | map_tail->offs = ((char *) de - base)>>2; |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1268 | map_tail->size = le16_to_cpu(de->rec_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1269 | count++; |
| 1270 | cond_resched(); |
| 1271 | } |
| 1272 | /* XXX: do we need to check rec_len == 0 case? -Chris */ |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1273 | de = ext4_next_entry(de, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1274 | } |
| 1275 | return count; |
| 1276 | } |
| 1277 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1278 | /* Sort map by hash value */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1279 | static void dx_sort_map (struct dx_map_entry *map, unsigned count) |
| 1280 | { |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1281 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1298 | continue; |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1299 | swap(*(q+1), *q); |
| 1300 | more = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } while(more); |
| 1303 | } |
| 1304 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1305 | static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1306 | { |
| 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 Xu | 837c23f | 2020-11-07 23:58:11 +0800 | [diff] [blame] | 1311 | ASSERT(count < dx_get_limit(entries)); |
| 1312 | ASSERT(old < entries + count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1313 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1318 | |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1319 | #ifdef CONFIG_UNICODE |
| 1320 | /* |
| 1321 | * Test whether a case-insensitive directory entry matches the filename |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1322 | * being searched for. If quick is set, assume the name being looked up |
| 1323 | * is already in the casefolded form. |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1324 | * |
| 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 Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1328 | static 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 Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1330 | { |
Daniel Rosenberg | f8f4acb | 2020-10-28 05:08:20 +0000 | [diff] [blame] | 1331 | const struct super_block *sb = parent->i_sb; |
| 1332 | const struct unicode_map *um = sb->s_encoding; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1333 | struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len); |
| 1334 | struct qstr entry = QSTR_INIT(de_name, de_name_len); |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1335 | int ret; |
| 1336 | |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1337 | if (IS_ENCRYPTED(parent)) { |
| 1338 | const struct fscrypt_str encrypted_name = |
| 1339 | FSTR_INIT(de_name, de_name_len); |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1340 | |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1341 | 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 Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1356 | if (ret < 0) { |
| 1357 | /* Handle invalid character sequence as either an error |
| 1358 | * or as an opaque byte sequence. |
| 1359 | */ |
Daniel Rosenberg | f8f4acb | 2020-10-28 05:08:20 +0000 | [diff] [blame] | 1360 | if (sb_has_strict_encoding(sb)) |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1361 | 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 Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1366 | } |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1367 | out: |
| 1368 | kfree(decrypted_name.name); |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1369 | return ret; |
| 1370 | } |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1371 | |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1372 | int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, |
| 1373 | struct ext4_filename *name) |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1374 | { |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1375 | struct fscrypt_str *cf_name = &name->cf_name; |
| 1376 | struct dx_hash_info *hinfo = &name->hinfo; |
Gabriel Krisman Bertazi | 96fcaf8 | 2019-07-02 17:53:22 -0400 | [diff] [blame] | 1377 | int len; |
| 1378 | |
Daniel Rosenberg | f8f4acb | 2020-10-28 05:08:20 +0000 | [diff] [blame] | 1379 | if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding) { |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1380 | cf_name->name = NULL; |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1381 | return 0; |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS); |
| 1385 | if (!cf_name->name) |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1386 | return -ENOMEM; |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1387 | |
Daniel Rosenberg | f8f4acb | 2020-10-28 05:08:20 +0000 | [diff] [blame] | 1388 | len = utf8_casefold(dir->i_sb->s_encoding, |
Gabriel Krisman Bertazi | 96fcaf8 | 2019-07-02 17:53:22 -0400 | [diff] [blame] | 1389 | iname, cf_name->name, |
| 1390 | EXT4_NAME_LEN); |
| 1391 | if (len <= 0) { |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1392 | kfree(cf_name->name); |
| 1393 | cf_name->name = NULL; |
| 1394 | } |
Gabriel Krisman Bertazi | 96fcaf8 | 2019-07-02 17:53:22 -0400 | [diff] [blame] | 1395 | cf_name->len = (unsigned) len; |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1396 | if (!IS_ENCRYPTED(dir)) |
| 1397 | return 0; |
Gabriel Krisman Bertazi | 96fcaf8 | 2019-07-02 17:53:22 -0400 | [diff] [blame] | 1398 | |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1399 | 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 Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1406 | } |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1407 | #endif |
| 1408 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1409 | /* |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1410 | * Test whether a directory entry matches the filename being searched for. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1411 | * |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1412 | * Return: %true if the directory entry matches, otherwise %false. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1413 | */ |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1414 | static bool ext4_match(struct inode *parent, |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1415 | const struct ext4_filename *fname, |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1416 | struct ext4_dir_entry_2 *de) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1417 | { |
Eric Biggers | 067d102 | 2017-04-24 10:00:11 -0700 | [diff] [blame] | 1418 | struct fscrypt_name f; |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1419 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1420 | if (!de->inode) |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1421 | return false; |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1422 | |
Eric Biggers | 067d102 | 2017-04-24 10:00:11 -0700 | [diff] [blame] | 1423 | f.usr_fname = fname->usr_fname; |
| 1424 | f.disk_name = fname->disk_name; |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 1425 | #ifdef CONFIG_FS_ENCRYPTION |
Eric Biggers | 067d102 | 2017-04-24 10:00:11 -0700 | [diff] [blame] | 1426 | f.crypto_buf = fname->crypto_buf; |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1427 | #endif |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1428 | |
| 1429 | #ifdef CONFIG_UNICODE |
Daniel Rosenberg | f8f4acb | 2020-10-28 05:08:20 +0000 | [diff] [blame] | 1430 | if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) { |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1431 | if (fname->cf_name.name) { |
| 1432 | struct qstr cf = {.name = fname->cf_name.name, |
| 1433 | .len = fname->cf_name.len}; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1434 | if (IS_ENCRYPTED(parent)) { |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1435 | if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) || |
| 1436 | fname->hinfo.minor_hash != |
| 1437 | EXT4_DIRENT_MINOR_HASH(de)) { |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1438 | |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1439 | return 0; |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 1440 | } |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1441 | } |
| 1442 | return !ext4_ci_compare(parent, &cf, de->name, |
| 1443 | de->name_len, true); |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1444 | } |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1445 | return !ext4_ci_compare(parent, fname->usr_fname, de->name, |
| 1446 | de->name_len, false); |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 1447 | } |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1448 | #endif |
| 1449 | |
Eric Biggers | 067d102 | 2017-04-24 10:00:11 -0700 | [diff] [blame] | 1450 | return fscrypt_match_name(&f, de->name, de->name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 1455 | */ |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1456 | int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size, |
| 1457 | struct inode *dir, struct ext4_filename *fname, |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1458 | unsigned int offset, struct ext4_dir_entry_2 **res_dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1459 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1460 | struct ext4_dir_entry_2 * de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1461 | char * dlimit; |
| 1462 | int de_len; |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1463 | |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1464 | de = (struct ext4_dir_entry_2 *)search_buf; |
| 1465 | dlimit = search_buf + buf_size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1466 | while ((char *) de < dlimit) { |
| 1467 | /* this code is executed quadratically often */ |
| 1468 | /* do minimal checking `by hand' */ |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1469 | if ((char *) de + de->name_len <= dlimit && |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1470 | ext4_match(dir, fname, de)) { |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1471 | /* found a match - just to be sure, do |
| 1472 | * a full check */ |
Jan Kara | 7303cb5 | 2020-07-31 18:21:35 +0200 | [diff] [blame] | 1473 | if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf, |
| 1474 | buf_size, offset)) |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1475 | return -1; |
| 1476 | *res_dir = de; |
| 1477 | return 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1478 | } |
| 1479 | /* prevent looping on a bad block */ |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1480 | de_len = ext4_rec_len_from_disk(de->rec_len, |
| 1481 | dir->i_sb->s_blocksize); |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1482 | if (de_len <= 0) |
| 1483 | return -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1484 | offset += de_len; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1485 | de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1486 | } |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 1487 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Darrick J. Wong | c6af8803 | 2012-11-12 23:51:02 -0500 | [diff] [blame] | 1490 | static 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1505 | |
| 1506 | /* |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1507 | * __ext4_find_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1508 | * |
| 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 Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1517 | static 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1521 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1522 | struct super_block *sb; |
| 1523 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; |
| 1524 | struct buffer_head *bh, *ret = NULL; |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 1525 | ext4_lblk_t start, block; |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1526 | const u8 *name = fname->usr_fname->name; |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 1527 | size_t ra_max = 0; /* Number of bh's in the readahead |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1528 | buffer, bh_use[] */ |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 1529 | size_t ra_ptr = 0; /* Current index into readahead |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1530 | buffer */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1531 | ext4_lblk_t nblocks; |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1532 | int i, namelen, retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1533 | |
| 1534 | *res_dir = NULL; |
| 1535 | sb = dir->i_sb; |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1536 | namelen = fname->usr_fname->len; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1537 | if (namelen > EXT4_NAME_LEN) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1538 | return NULL; |
Tao Ma | e8e948e | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 1539 | |
| 1540 | if (ext4_has_inline_data(dir)) { |
| 1541 | int has_inline_data = 1; |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1542 | ret = ext4_find_inline_entry(dir, fname, res_dir, |
Tao Ma | e8e948e | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 1543 | &has_inline_data); |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 1544 | if (has_inline_data) { |
| 1545 | if (inlined) |
| 1546 | *inlined = 1; |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1547 | goto cleanup_and_exit; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 1548 | } |
Tao Ma | e8e948e | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 1549 | } |
| 1550 | |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1551 | if ((namelen <= 2) && (name[0] == '.') && |
Aaro Koskinen | 6d5c3aa | 2010-12-14 21:45:31 -0500 | [diff] [blame] | 1552 | (name[1] == '.' || name[1] == '\0')) { |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1553 | /* |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1561 | if (is_dx(dir)) { |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1562 | ret = ext4_dx_find_entry(dir, fname, res_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1563 | /* |
| 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'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1568 | if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR) |
| 1569 | goto cleanup_and_exit; |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1570 | dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, " |
| 1571 | "falling back\n")); |
Eric Sandeen | f39b3f4 | 2018-07-29 17:13:42 -0400 | [diff] [blame] | 1572 | ret = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1573 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1574 | nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); |
Chandan Rajendra | 9d5afec | 2017-12-11 15:00:57 -0500 | [diff] [blame] | 1575 | if (!nblocks) { |
| 1576 | ret = NULL; |
| 1577 | goto cleanup_and_exit; |
| 1578 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1579 | start = EXT4_I(dir)->i_dir_start_lookup; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1580 | if (start >= nblocks) |
| 1581 | start = 0; |
| 1582 | block = start; |
| 1583 | restart: |
| 1584 | do { |
| 1585 | /* |
| 1586 | * We deal with the read-ahead logic here. |
| 1587 | */ |
Shijie Luo | 9424ef5 | 2020-02-15 03:02:06 -0500 | [diff] [blame] | 1588 | cond_resched(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1589 | if (ra_ptr >= ra_max) { |
| 1590 | /* Refill the readahead buffer */ |
| 1591 | ra_ptr = 0; |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 1592 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 1606 | goto next; |
| 1607 | wait_on_buffer(bh); |
| 1608 | if (!buffer_uptodate(bh)) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 1609 | EXT4_ERROR_INODE_ERR(dir, EIO, |
| 1610 | "reading directory lblock %lu", |
| 1611 | (unsigned long) block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1612 | brelse(bh); |
Khazhismel Kumykov | 6febe6f | 2017-06-23 00:29:05 -0400 | [diff] [blame] | 1613 | ret = ERR_PTR(-EIO); |
| 1614 | goto cleanup_and_exit; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1615 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1616 | if (!buffer_verified(bh) && |
Darrick J. Wong | c6af8803 | 2012-11-12 23:51:02 -0500 | [diff] [blame] | 1617 | !is_dx_internal_node(dir, block, |
| 1618 | (struct ext4_dir_entry *)bh->b_data) && |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 1619 | !ext4_dirblock_csum_verify(dir, bh)) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 1620 | EXT4_ERROR_INODE_ERR(dir, EFSBADCRC, |
| 1621 | "checksumming directory " |
| 1622 | "block %lu", (unsigned long)block); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1623 | brelse(bh); |
Theodore Ts'o | bdddf34 | 2017-06-23 00:47:05 -0400 | [diff] [blame] | 1624 | ret = ERR_PTR(-EFSBADCRC); |
| 1625 | goto cleanup_and_exit; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1626 | } |
| 1627 | set_buffer_verified(bh); |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1628 | i = search_dirblock(bh, dir, fname, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1629 | block << EXT4_BLOCK_SIZE_BITS(sb), res_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1630 | if (i == 1) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1631 | EXT4_I(dir)->i_dir_start_lookup = block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1632 | 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1649 | nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1650 | if (block < nblocks) { |
| 1651 | start = 0; |
| 1652 | goto restart; |
| 1653 | } |
| 1654 | |
| 1655 | cleanup_and_exit: |
| 1656 | /* Clean up the read-ahead blocks */ |
| 1657 | for (; ra_ptr < ra_max; ra_ptr++) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1658 | brelse(bh_use[ra_ptr]); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1659 | return ret; |
| 1660 | } |
| 1661 | |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1662 | static 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 | |
| 1683 | static 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 Rosenberg | bb9cd91 | 2020-11-19 06:09:03 +0000 | [diff] [blame] | 1692 | generic_set_encrypted_ci_d_ops(dentry); |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1693 | 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'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1704 | static struct buffer_head * ext4_dx_find_entry(struct inode *dir, |
| 1705 | struct ext4_filename *fname, |
| 1706 | struct ext4_dir_entry_2 **res_dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1707 | { |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1708 | struct super_block * sb = dir->i_sb; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 1709 | struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1710 | struct buffer_head *bh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1711 | ext4_lblk_t block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1712 | int retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1713 | |
Chandan Rajendra | 643fa96 | 2018-12-12 15:20:12 +0530 | [diff] [blame] | 1714 | #ifdef CONFIG_FS_ENCRYPTION |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1715 | *res_dir = NULL; |
| 1716 | #endif |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1717 | frame = dx_probe(fname, dir, NULL, frames); |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 1718 | if (IS_ERR(frame)) |
| 1719 | return (struct buffer_head *) frame; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1720 | do { |
| 1721 | block = dx_get_block(frame->at); |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 1722 | bh = ext4_read_dirblock(dir, block, DIRENT_HTREE); |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1723 | if (IS_ERR(bh)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1724 | goto errout; |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1725 | |
Eric Biggers | d6b9755 | 2017-05-24 18:10:49 -0400 | [diff] [blame] | 1726 | retval = search_dirblock(bh, dir, fname, |
Theodore Ts'o | 7845c04 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1727 | block << EXT4_BLOCK_SIZE_BITS(sb), |
| 1728 | res_dir); |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1729 | if (retval == 1) |
| 1730 | goto success; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1731 | brelse(bh); |
Theodore Ts'o | 7845c04 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1732 | if (retval == -1) { |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1733 | bh = ERR_PTR(ERR_BAD_DX_DIR); |
Theodore Ts'o | 7845c04 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1734 | goto errout; |
| 1735 | } |
| 1736 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1737 | /* Check to see if we should continue to search */ |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 1738 | retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1739 | frames, NULL); |
| 1740 | if (retval < 0) { |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 1741 | ext4_warning_inode(dir, |
| 1742 | "error %d reading directory index block", |
| 1743 | retval); |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1744 | bh = ERR_PTR(retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1745 | goto errout; |
| 1746 | } |
| 1747 | } while (retval == 1); |
| 1748 | |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1749 | bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1750 | errout: |
Eric Biggers | d6b9755 | 2017-05-24 18:10:49 -0400 | [diff] [blame] | 1751 | dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name)); |
Theodore Ts'o | 537d8f9 | 2014-08-29 20:49:51 -0400 | [diff] [blame] | 1752 | success: |
| 1753 | dx_release(frames); |
| 1754 | return bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1755 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1756 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 1757 | static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1758 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1759 | struct inode *inode; |
| 1760 | struct ext4_dir_entry_2 *de; |
| 1761 | struct buffer_head *bh; |
Theodore Ts'o | 28b4c26 | 2016-02-07 19:35:05 -0500 | [diff] [blame] | 1762 | |
Eric Biggers | 8990427 | 2017-10-18 20:21:58 -0400 | [diff] [blame] | 1763 | if (dentry->d_name.len > EXT4_NAME_LEN) |
| 1764 | return ERR_PTR(-ENAMETOOLONG); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1765 | |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 1766 | bh = ext4_lookup_entry(dir, dentry, &de); |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 1767 | if (IS_ERR(bh)) |
Al Viro | e884bce | 2018-10-10 16:41:40 -0400 | [diff] [blame] | 1768 | return ERR_CAST(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1769 | inode = NULL; |
| 1770 | if (bh) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1771 | __u32 ino = le32_to_cpu(de->inode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1772 | brelse(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1773 | if (!ext4_valid_inum(dir->i_sb, ino)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1774 | EXT4_ERROR_INODE(dir, "bad inode number: %u", ino); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1775 | return ERR_PTR(-EFSCORRUPTED); |
Vasily Averin | a6c15c2 | 2007-07-15 23:40:46 -0700 | [diff] [blame] | 1776 | } |
Andreas Dilger | 7e936b7 | 2012-05-28 17:02:25 -0400 | [diff] [blame] | 1777 | if (unlikely(ino == dir->i_ino)) { |
David Howells | a34e15c | 2014-01-06 14:04:23 -0500 | [diff] [blame] | 1778 | EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir", |
| 1779 | dentry); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1780 | return ERR_PTR(-EFSCORRUPTED); |
Andreas Dilger | 7e936b7 | 2012-05-28 17:02:25 -0400 | [diff] [blame] | 1781 | } |
Theodore Ts'o | 8a36397 | 2018-12-19 12:29:13 -0500 | [diff] [blame] | 1782 | inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 1783 | if (inode == ERR_PTR(-ESTALE)) { |
| 1784 | EXT4_ERROR_INODE(dir, |
| 1785 | "deleted inode referenced: %u", |
| 1786 | ino); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1787 | return ERR_PTR(-EFSCORRUPTED); |
Bryan Donlan | e6f009b | 2009-02-22 21:20:25 -0500 | [diff] [blame] | 1788 | } |
Chandan Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 1789 | if (!IS_ERR(inode) && IS_ENCRYPTED(dir) && |
Theodore Ts'o | ff978b0 | 2016-02-08 00:54:26 -0500 | [diff] [blame] | 1790 | (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 1791 | !fscrypt_has_permitted_context(dir, inode)) { |
Theodore Ts'o | d9cdc903 | 2015-04-12 00:55:08 -0400 | [diff] [blame] | 1792 | ext4_warning(inode->i_sb, |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 1793 | "Inconsistent encryption contexts: %lu/%lu", |
Eric Biggers | 8c68084 | 2017-04-07 10:58:38 -0700 | [diff] [blame] | 1794 | dir->i_ino, inode->i_ino); |
Eric Biggers | dd01b69 | 2017-02-01 21:07:11 -0500 | [diff] [blame] | 1795 | iput(inode); |
Theodore Ts'o | d9cdc903 | 2015-04-12 00:55:08 -0400 | [diff] [blame] | 1796 | return ERR_PTR(-EPERM); |
| 1797 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1798 | } |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 1799 | |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1810 | return d_splice_alias(inode, dentry); |
| 1811 | } |
| 1812 | |
| 1813 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1814 | struct dentry *ext4_get_parent(struct dentry *child) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1815 | { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1816 | __u32 ino; |
Linus Torvalds | 26fe575 | 2012-05-10 13:14:12 -0700 | [diff] [blame] | 1817 | static const struct qstr dotdot = QSTR_INIT("..", 2); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1818 | struct ext4_dir_entry_2 * de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1819 | struct buffer_head *bh; |
| 1820 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1821 | bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL); |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 1822 | if (IS_ERR(bh)) |
Al Viro | e884bce | 2018-10-10 16:41:40 -0400 | [diff] [blame] | 1823 | return ERR_CAST(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1824 | if (!bh) |
| 1825 | return ERR_PTR(-ENOENT); |
| 1826 | ino = le32_to_cpu(de->inode); |
| 1827 | brelse(bh); |
| 1828 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 1829 | if (!ext4_valid_inum(child->d_sb, ino)) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1830 | EXT4_ERROR_INODE(d_inode(child), |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1831 | "bad parent inode number: %u", ino); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1832 | return ERR_PTR(-EFSCORRUPTED); |
Vasily Averin | a6c15c2 | 2007-07-15 23:40:46 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Theodore Ts'o | 8a36397 | 2018-12-19 12:29:13 -0500 | [diff] [blame] | 1835 | return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1838 | /* |
| 1839 | * Move count entries from end of map between two memory locations. |
| 1840 | * Returns pointer to last entry moved. |
| 1841 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1842 | static struct ext4_dir_entry_2 * |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1843 | dx_move_dirents(struct inode *dir, char *from, char *to, |
| 1844 | struct dx_map_entry *map, int count, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1845 | unsigned blocksize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1846 | { |
| 1847 | unsigned rec_len = 0; |
| 1848 | |
| 1849 | while (count--) { |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 1850 | struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) |
Toshiyuki Okajima | 9aee228 | 2009-06-08 12:41:35 -0400 | [diff] [blame] | 1851 | (from + (map->offs<<2)); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1852 | rec_len = ext4_dir_rec_len(de->name_len, dir); |
| 1853 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1854 | memcpy (to, de, rec_len); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1855 | ((struct ext4_dir_entry_2 *) to)->rec_len = |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1856 | ext4_rec_len_to_disk(rec_len, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1857 | de->inode = 0; |
| 1858 | map++; |
| 1859 | to += rec_len; |
| 1860 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1861 | return (struct ext4_dir_entry_2 *) (to - rec_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1864 | /* |
| 1865 | * Compact each dir entry in the range to the minimal rec_len. |
| 1866 | * Returns pointer to last entry in range. |
| 1867 | */ |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1868 | static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base, |
| 1869 | unsigned int blocksize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1870 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1871 | struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1872 | unsigned rec_len = 0; |
| 1873 | |
| 1874 | prev = to = de; |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1875 | while ((char*)de < base + blocksize) { |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1876 | next = ext4_next_entry(de, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1877 | if (de->inode && de->name_len) { |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1878 | rec_len = ext4_dir_rec_len(de->name_len, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1879 | if (de > to) |
| 1880 | memmove(to, de, rec_len); |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1881 | to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1882 | prev = to; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1883 | to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1884 | } |
| 1885 | de = next; |
| 1886 | } |
| 1887 | return prev; |
| 1888 | } |
| 1889 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1890 | /* |
| 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1895 | static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1896 | struct buffer_head **bh,struct dx_frame *frame, |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 1897 | struct dx_hash_info *hinfo) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1898 | { |
| 1899 | unsigned blocksize = dir->i_sb->s_blocksize; |
| 1900 | unsigned count, continued; |
| 1901 | struct buffer_head *bh2; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1902 | ext4_lblk_t newblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1903 | u32 hash2; |
| 1904 | struct dx_map_entry *map; |
| 1905 | char *data1 = (*bh)->b_data, *data2; |
Theodore Ts'o | 59e315b | 2008-12-06 16:58:39 -0500 | [diff] [blame] | 1906 | unsigned split, move, size; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1907 | struct ext4_dir_entry_2 *de = NULL, *de2; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1908 | int csum_size = 0; |
Theodore Ts'o | 59e315b | 2008-12-06 16:58:39 -0500 | [diff] [blame] | 1909 | int err = 0, i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1910 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 1911 | if (ext4_has_metadata_csum(dir->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1912 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 1913 | |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 1914 | bh2 = ext4_append(handle, dir, &newblock); |
| 1915 | if (IS_ERR(bh2)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1916 | brelse(*bh); |
| 1917 | *bh = NULL; |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 1918 | return (struct ext4_dir_entry_2 *) bh2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | BUFFER_TRACE(*bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1922 | err = ext4_journal_get_write_access(handle, *bh); |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1923 | if (err) |
| 1924 | goto journal_error; |
| 1925 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1926 | BUFFER_TRACE(frame->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1927 | err = ext4_journal_get_write_access(handle, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1928 | 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 Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1935 | count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1936 | blocksize, hinfo, map); |
| 1937 | map -= count; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1938 | dx_sort_map(map, count); |
Eric Sandeen | 5872331 | 2020-06-17 14:19:04 -0500 | [diff] [blame] | 1939 | /* Ensure that neither split block is over half full */ |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1940 | 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 Sandeen | 5872331 | 2020-06-17 14:19:04 -0500 | [diff] [blame] | 1949 | /* |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1961 | hash2 = map[split].hash; |
| 1962 | continued = hash2 == map[split - 1].hash; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1963 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* Fancy dance to stay within two buffers */ |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1968 | de2 = dx_move_dirents(dir, data1, data2, map + split, count - split, |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 1969 | blocksize); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 1970 | de = dx_pack_dirents(dir, data1, blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1971 | de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - |
| 1972 | (char *) de, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1973 | blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1974 | de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) - |
| 1975 | (char *) de2, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1976 | blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1977 | if (csum_size) { |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 1978 | ext4_initialize_dirent_tail(*bh, blocksize); |
| 1979 | ext4_initialize_dirent_tail(bh2, blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1980 | } |
| 1981 | |
Michael Halcrow | b309848 | 2015-04-12 01:07:01 -0400 | [diff] [blame] | 1982 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1986 | |
| 1987 | /* Which block gets the new entry? */ |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 1988 | if (hinfo->hash >= hash2) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1989 | swap(*bh, bh2); |
| 1990 | de = de2; |
| 1991 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1992 | dx_insert_block(frame, hash2 + continued, newblock); |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 1993 | err = ext4_handle_dirty_dirblock(handle, dir, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1994 | if (err) |
| 1995 | goto journal_error; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 1996 | err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1997 | if (err) |
| 1998 | goto journal_error; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1999 | brelse(bh2); |
| 2000 | dxtrace(dx_show_index("frame", frame->entries)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2001 | return de; |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 2002 | |
| 2003 | journal_error: |
| 2004 | brelse(*bh); |
| 2005 | brelse(bh2); |
| 2006 | *bh = NULL; |
| 2007 | ext4_std_error(dir->i_sb, err); |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 2008 | return ERR_PTR(err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2009 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2010 | |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2011 | int ext4_find_dest_de(struct inode *dir, struct inode *inode, |
| 2012 | struct buffer_head *bh, |
| 2013 | void *buf, int buf_size, |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2014 | struct ext4_filename *fname, |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2015 | struct ext4_dir_entry_2 **dest_de) |
| 2016 | { |
| 2017 | struct ext4_dir_entry_2 *de; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2018 | unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir); |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2019 | int nlen, rlen; |
| 2020 | unsigned int offset = 0; |
| 2021 | char *top; |
Michael Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 2022 | |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2023 | 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 Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 2027 | buf, buf_size, offset)) |
| 2028 | return -EFSCORRUPTED; |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 2029 | if (ext4_match(dir, fname, de)) |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 2030 | return -EEXIST; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2031 | nlen = ext4_dir_rec_len(de->name_len, dir); |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2032 | 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 Halcrow | 1f3862b | 2015-04-12 01:09:03 -0400 | [diff] [blame] | 2038 | if ((char *) de > top) |
Eric Biggers | d9b9f8d | 2017-04-24 10:00:13 -0700 | [diff] [blame] | 2039 | return -ENOSPC; |
| 2040 | |
| 2041 | *dest_de = de; |
| 2042 | return 0; |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2043 | } |
| 2044 | |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2045 | void ext4_insert_dentry(struct inode *dir, |
| 2046 | struct inode *inode, |
Eric Biggers | 1bc0af6 | 2017-04-29 23:27:26 -0400 | [diff] [blame] | 2047 | struct ext4_dir_entry_2 *de, |
| 2048 | int buf_size, |
| 2049 | struct ext4_filename *fname) |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2050 | { |
| 2051 | |
| 2052 | int nlen, rlen; |
| 2053 | |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2054 | nlen = ext4_dir_rec_len(de->name_len, dir); |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2055 | rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); |
| 2056 | if (de->inode) { |
| 2057 | struct ext4_dir_entry_2 *de1 = |
Michael Halcrow | 4bdfc87 | 2015-04-12 00:56:28 -0400 | [diff] [blame] | 2058 | (struct ext4_dir_entry_2 *)((char *)de + nlen); |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2059 | 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'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2066 | de->name_len = fname_len(fname); |
| 2067 | memcpy(de->name, fname_name(fname), fname_len(fname)); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2068 | if (ext4_hash_in_dirent(dir)) { |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 2069 | struct dx_hash_info *hinfo = &fname->hinfo; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2070 | |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 2071 | EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2072 | EXT4_DIRENT_HASHES(de)->minor_hash = |
Daniel Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 2073 | cpu_to_le32(hinfo->minor_hash); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2074 | } |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2075 | } |
Michael Halcrow | 4bdfc87 | 2015-04-12 00:56:28 -0400 | [diff] [blame] | 2076 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2077 | /* |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2084 | */ |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2085 | static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname, |
| 2086 | struct inode *dir, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2087 | struct inode *inode, struct ext4_dir_entry_2 *de, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2088 | struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2089 | { |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2090 | unsigned int blocksize = dir->i_sb->s_blocksize; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2091 | int csum_size = 0; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2092 | int err, err2; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2093 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 2094 | if (ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2095 | csum_size = sizeof(struct ext4_dir_entry_tail); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2096 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2097 | if (!de) { |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2098 | err = ext4_find_dest_de(dir, inode, bh, bh->b_data, |
| 2099 | blocksize - csum_size, fname, &de); |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2100 | if (err) |
| 2101 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2102 | } |
| 2103 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2104 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2105 | if (err) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2106 | ext4_std_error(dir->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2107 | return err; |
| 2108 | } |
| 2109 | |
Eric Biggers | 1bc0af6 | 2017-04-29 23:27:26 -0400 | [diff] [blame] | 2110 | /* By now the buffer is marked for journaling */ |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2111 | ext4_insert_dentry(dir, inode, de, blocksize, fname); |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2112 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2113 | /* |
| 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2119 | * ext4_new_inode() setting the times, but error |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2120 | * 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 Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 2124 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2125 | ext4_update_dx_flag(dir); |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2126 | inode_inc_iversion(dir); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2127 | err2 = ext4_mark_inode_dirty(handle, dir); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2128 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 2129 | err = ext4_handle_dirty_dirblock(handle, dir, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2130 | if (err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2131 | ext4_std_error(dir->i_sb, err); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2132 | return err ? err : err2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2135 | /* |
| 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'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2139 | static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, |
Theodore Ts'o | 56a0491 | 2016-01-08 16:00:31 -0500 | [diff] [blame] | 2140 | struct inode *dir, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2141 | struct inode *inode, struct buffer_head *bh) |
| 2142 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2143 | struct buffer_head *bh2; |
| 2144 | struct dx_root *root; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2145 | struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2146 | struct dx_entry *entries; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2147 | struct ext4_dir_entry_2 *de, *de2; |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2148 | char *data2, *top; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2149 | unsigned len; |
| 2150 | int retval; |
| 2151 | unsigned blocksize; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2152 | ext4_lblk_t block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2153 | struct fake_dirent *fde; |
Michael Halcrow | 4bdfc87 | 2015-04-12 00:56:28 -0400 | [diff] [blame] | 2154 | int csum_size = 0; |
| 2155 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 2156 | if (ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2157 | csum_size = sizeof(struct ext4_dir_entry_tail); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2158 | |
| 2159 | blocksize = dir->i_sb->s_blocksize; |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 2160 | dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino)); |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 2161 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2162 | retval = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2163 | if (retval) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2164 | ext4_std_error(dir->i_sb, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2165 | brelse(bh); |
| 2166 | return retval; |
| 2167 | } |
| 2168 | root = (struct dx_root *) bh->b_data; |
| 2169 | |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 2170 | /* The 0th block becomes the root, move the dirents out */ |
| 2171 | fde = &root->dotdot; |
| 2172 | de = (struct ext4_dir_entry_2 *)((char *)fde + |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2173 | ext4_rec_len_from_disk(fde->rec_len, blocksize)); |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 2174 | if ((char *) de >= (((char *) root) + blocksize)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2175 | EXT4_ERROR_INODE(dir, "invalid rec_len for '..'"); |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 2176 | brelse(bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2177 | return -EFSCORRUPTED; |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 2178 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2179 | len = ((char *) root) + (blocksize - csum_size) - (char *) de; |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 2180 | |
| 2181 | /* Allocate new block for the 0th block's dirents */ |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 2182 | bh2 = ext4_append(handle, dir, &block); |
| 2183 | if (IS_ERR(bh2)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2184 | brelse(bh); |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 2185 | return PTR_ERR(bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2186 | } |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2187 | ext4_set_inode_flag(dir, EXT4_INODE_INDEX); |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2188 | data2 = bh2->b_data; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2189 | |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2190 | memcpy(data2, de, len); |
| 2191 | de = (struct ext4_dir_entry_2 *) data2; |
| 2192 | top = data2 + len; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2193 | while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2194 | de = de2; |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2195 | de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) - |
| 2196 | (char *) de, blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2197 | |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2198 | if (csum_size) |
| 2199 | ext4_initialize_dirent_tail(bh2, blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2200 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2201 | /* Initialize the root; the dot dirents already exist */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2202 | de = (struct ext4_dir_entry_2 *) (&root->dotdot); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2203 | de->rec_len = ext4_rec_len_to_disk( |
| 2204 | blocksize - ext4_dir_rec_len(2, NULL), blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2205 | memset (&root->info, 0, sizeof(root->info)); |
| 2206 | root->info.info_length = sizeof(root->info); |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2207 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2213 | entries = root->entries; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2214 | dx_set_block(entries, 1); |
| 2215 | dx_set_count(entries, 1); |
| 2216 | dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info))); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2217 | |
| 2218 | /* Initialize as for dx_probe */ |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2219 | 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 Rosenberg | 1ae98e2 | 2021-03-19 07:34:14 +0000 | [diff] [blame^] | 2223 | |
| 2224 | /* casefolded encrypted hashes are computed on fname setup */ |
| 2225 | if (!ext4_hash_in_dirent(dir)) |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2226 | ext4fs_dirhash(dir, fname_name(fname), |
| 2227 | fname_len(fname), &fname->hinfo); |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2228 | |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2229 | memset(frames, 0, sizeof(frames)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2230 | frame = frames; |
| 2231 | frame->entries = entries; |
| 2232 | frame->at = entries; |
| 2233 | frame->bh = bh; |
Allison Henderson | 6976a6f | 2011-05-15 00:19:41 -0400 | [diff] [blame] | 2234 | |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2235 | retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh); |
| 2236 | if (retval) |
| 2237 | goto out_frames; |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 2238 | retval = ext4_handle_dirty_dirblock(handle, dir, bh2); |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2239 | if (retval) |
| 2240 | goto out_frames; |
Allison Henderson | 6976a6f | 2011-05-15 00:19:41 -0400 | [diff] [blame] | 2241 | |
gmail | e81d447 | 2016-09-30 01:33:37 -0400 | [diff] [blame] | 2242 | de = do_split(handle,dir, &bh2, frame, &fname->hinfo); |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 2243 | if (IS_ERR(de)) { |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2244 | retval = PTR_ERR(de); |
| 2245 | goto out_frames; |
Jan Kara | 7ad8e4e | 2011-05-03 11:05:55 -0400 | [diff] [blame] | 2246 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2247 | |
gmail | e81d447 | 2016-09-30 01:33:37 -0400 | [diff] [blame] | 2248 | retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2); |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2249 | out_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 | */ |
gmail | e81d447 | 2016-09-30 01:33:37 -0400 | [diff] [blame] | 2255 | if (retval) |
| 2256 | ext4_mark_inode_dirty(handle, dir); |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2257 | dx_release(frames); |
gmail | e81d447 | 2016-09-30 01:33:37 -0400 | [diff] [blame] | 2258 | brelse(bh2); |
Jan Kara | 6050d47 | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 2259 | return retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2260 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2261 | |
| 2262 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2263 | * ext4_add_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2264 | * |
| 2265 | * adds a file entry to the specified directory, using the same |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2266 | * semantics as ext4_find_entry(). It returns NULL if it failed. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2267 | * |
| 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'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2272 | static int ext4_add_entry(handle_t *handle, struct dentry *dentry, |
| 2273 | struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2274 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2275 | struct inode *dir = d_inode(dentry->d_parent); |
Lukas Czerner | e12fb97 | 2015-04-03 10:46:58 -0400 | [diff] [blame] | 2276 | struct buffer_head *bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2277 | struct ext4_dir_entry_2 *de; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2278 | struct super_block *sb; |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2279 | struct ext4_filename fname; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2280 | int retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2281 | int dx_fallback=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2282 | unsigned blocksize; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2283 | ext4_lblk_t block, blocks; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2284 | int csum_size = 0; |
| 2285 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 2286 | if (ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2287 | csum_size = sizeof(struct ext4_dir_entry_tail); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2288 | |
| 2289 | sb = dir->i_sb; |
| 2290 | blocksize = sb->s_blocksize; |
| 2291 | if (!dentry->d_name.len) |
| 2292 | return -EINVAL; |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2293 | |
Eric Biggers | 75d18cd | 2020-11-17 23:56:06 -0800 | [diff] [blame] | 2294 | if (fscrypt_is_nokey_name(dentry)) |
| 2295 | return -ENOKEY; |
| 2296 | |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 2297 | #ifdef CONFIG_UNICODE |
Daniel Rosenberg | f8f4acb | 2020-10-28 05:08:20 +0000 | [diff] [blame] | 2298 | if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) && |
| 2299 | sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name)) |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 2300 | return -EINVAL; |
| 2301 | #endif |
| 2302 | |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2303 | retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname); |
| 2304 | if (retval) |
| 2305 | return retval; |
| 2306 | |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2307 | if (ext4_has_inline_data(dir)) { |
Theodore Ts'o | 56a0491 | 2016-01-08 16:00:31 -0500 | [diff] [blame] | 2308 | retval = ext4_try_add_inline_entry(handle, &fname, dir, inode); |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2309 | if (retval < 0) |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2310 | goto out; |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2311 | if (retval == 1) { |
| 2312 | retval = 0; |
Lukas Czerner | e12fb97 | 2015-04-03 10:46:58 -0400 | [diff] [blame] | 2313 | goto out; |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2317 | if (is_dx(dir)) { |
Theodore Ts'o | 56a0491 | 2016-01-08 16:00:31 -0500 | [diff] [blame] | 2318 | retval = ext4_dx_add_entry(handle, &fname, dir, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2319 | if (!retval || (retval != ERR_BAD_DX_DIR)) |
Lukas Czerner | e12fb97 | 2015-04-03 10:46:58 -0400 | [diff] [blame] | 2320 | goto out; |
Jan Kara | 48a3431 | 2020-02-10 15:43:16 +0100 | [diff] [blame] | 2321 | /* 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 Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2328 | ext4_clear_inode_flag(dir, EXT4_INODE_INDEX); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2329 | dx_fallback++; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2330 | retval = ext4_mark_inode_dirty(handle, dir); |
| 2331 | if (unlikely(retval)) |
| 2332 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2333 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2334 | blocks = dir->i_size >> sb->s_blocksize_bits; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2335 | for (block = 0; block < blocks; block++) { |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2336 | bh = ext4_read_dirblock(dir, block, DIRENT); |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 2337 | if (bh == NULL) { |
| 2338 | bh = ext4_bread(handle, dir, block, |
| 2339 | EXT4_GET_BLOCKS_CREATE); |
| 2340 | goto add_to_new_block; |
| 2341 | } |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2342 | 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 Czerner | e12fb97 | 2015-04-03 10:46:58 -0400 | [diff] [blame] | 2349 | if (retval != -ENOSPC) |
| 2350 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2351 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2352 | if (blocks == 1 && !dx_fallback && |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 2353 | ext4_has_feature_dir_index(sb)) { |
Theodore Ts'o | 56a0491 | 2016-01-08 16:00:31 -0500 | [diff] [blame] | 2354 | retval = make_indexed_dir(handle, &fname, dir, |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2355 | inode, bh); |
Lukas Czerner | e12fb97 | 2015-04-03 10:46:58 -0400 | [diff] [blame] | 2356 | bh = NULL; /* make_indexed_dir releases bh */ |
| 2357 | goto out; |
| 2358 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2359 | brelse(bh); |
| 2360 | } |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 2361 | bh = ext4_append(handle, dir, &block); |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 2362 | add_to_new_block: |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2363 | if (IS_ERR(bh)) { |
| 2364 | retval = PTR_ERR(bh); |
| 2365 | bh = NULL; |
| 2366 | goto out; |
| 2367 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2368 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2369 | de->inode = 0; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2370 | de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize); |
| 2371 | |
Theodore Ts'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2372 | if (csum_size) |
| 2373 | ext4_initialize_dirent_tail(bh, blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2374 | |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2375 | retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh); |
Lukas Czerner | e12fb97 | 2015-04-03 10:46:58 -0400 | [diff] [blame] | 2376 | out: |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2377 | ext4_fname_free_filename(&fname); |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 2378 | brelse(bh); |
Frank Mayhar | 14ece10 | 2010-05-17 08:00:00 -0400 | [diff] [blame] | 2379 | if (retval == 0) |
| 2380 | ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY); |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 2381 | return retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2384 | /* |
| 2385 | * Returns 0 for success, or a negative error value |
| 2386 | */ |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2387 | static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, |
Theodore Ts'o | 56a0491 | 2016-01-08 16:00:31 -0500 | [diff] [blame] | 2388 | struct inode *dir, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2389 | { |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2390 | struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2391 | struct dx_entry *entries, *at; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2392 | struct buffer_head *bh; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2393 | struct super_block *sb = dir->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2394 | struct ext4_dir_entry_2 *de; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2395 | int restart; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2396 | int err; |
| 2397 | |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2398 | again: |
| 2399 | restart = 0; |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2400 | frame = dx_probe(fname, dir, NULL, frames); |
Theodore Ts'o | dd73b5d | 2014-08-29 20:52:17 -0400 | [diff] [blame] | 2401 | if (IS_ERR(frame)) |
| 2402 | return PTR_ERR(frame); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2403 | entries = frame->entries; |
| 2404 | at = frame->at; |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 2405 | bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2406 | if (IS_ERR(bh)) { |
| 2407 | err = PTR_ERR(bh); |
| 2408 | bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2409 | goto cleanup; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 2410 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2411 | |
| 2412 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2413 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2414 | if (err) |
| 2415 | goto journal_error; |
| 2416 | |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2417 | err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh); |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 2418 | if (err != -ENOSPC) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2419 | goto cleanup; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2420 | |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2421 | err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2422 | /* Block full, should compress but for now just split */ |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2423 | dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2424 | 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.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2427 | ext4_lblk_t newblock; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2428 | int levels = frame - frames + 1; |
| 2429 | unsigned int icount; |
| 2430 | int add_level = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2431 | struct dx_entry *entries2; |
| 2432 | struct dx_node *node2; |
| 2433 | struct buffer_head *bh2; |
| 2434 | |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2435 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2455 | err = -ENOSPC; |
| 2456 | goto cleanup; |
| 2457 | } |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2458 | icount = dx_get_count(entries); |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 2459 | bh2 = ext4_append(handle, dir, &newblock); |
| 2460 | if (IS_ERR(bh2)) { |
| 2461 | err = PTR_ERR(bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2462 | goto cleanup; |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 2463 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2464 | node2 = (struct dx_node *)(bh2->b_data); |
| 2465 | entries2 = node2->entries; |
Andreas Schlick | 1f7bebb | 2009-09-10 23:16:07 -0400 | [diff] [blame] | 2466 | memset(&node2->fake, 0, sizeof(struct fake_dirent)); |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2467 | node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize, |
| 2468 | sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2469 | BUFFER_TRACE(frame->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2470 | err = ext4_journal_get_write_access(handle, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2471 | if (err) |
| 2472 | goto journal_error; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2473 | if (!add_level) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2474 | unsigned icount1 = icount/2, icount2 = icount - icount1; |
| 2475 | unsigned hash2 = dx_get_hash(entries + icount1); |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2476 | dxtrace(printk(KERN_DEBUG "Split index %i/%i\n", |
| 2477 | icount1, icount2)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2478 | |
| 2479 | BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2480 | err = ext4_journal_get_write_access(handle, |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2481 | (frame - 1)->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2482 | if (err) |
| 2483 | goto journal_error; |
| 2484 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2485 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2490 | |
| 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 Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2497 | dx_insert_block((frame - 1), hash2, newblock); |
| 2498 | dxtrace(dx_show_index("node", frame->entries)); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2499 | dxtrace(dx_show_index("node", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2500 | ((struct dx_node *) bh2->b_data)->entries)); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 2501 | err = ext4_handle_dirty_dx_node(handle, dir, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2502 | if (err) |
| 2503 | goto journal_error; |
| 2504 | brelse (bh2); |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2505 | err = ext4_handle_dirty_dx_node(handle, dir, |
| 2506 | (frame - 1)->bh); |
| 2507 | if (err) |
| 2508 | goto journal_error; |
Theodore Ts'o | b5776e7 | 2021-02-04 00:05:20 -0500 | [diff] [blame] | 2509 | err = ext4_handle_dirty_dx_node(handle, dir, |
| 2510 | frame->bh); |
| 2511 | if (err) |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2512 | goto journal_error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2513 | } else { |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2514 | struct dx_root *dxroot; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2515 | 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 Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2522 | 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 Bertazi | 799578a | 2018-10-02 12:43:51 -0400 | [diff] [blame] | 2526 | dxroot->info.indirect_levels)); |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2527 | err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2528 | if (err) |
| 2529 | goto journal_error; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2530 | err = ext4_handle_dirty_dx_node(handle, dir, bh2); |
| 2531 | brelse(bh2); |
| 2532 | restart = 1; |
| 2533 | goto journal_error; |
Theodore Ts'o | b409714 | 2011-01-10 12:46:59 -0500 | [diff] [blame] | 2534 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2535 | } |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2536 | de = do_split(handle, dir, &bh, frame, &fname->hinfo); |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 2537 | if (IS_ERR(de)) { |
| 2538 | err = PTR_ERR(de); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2539 | goto cleanup; |
Theodore Ts'o | f8b3b59 | 2014-08-29 20:52:18 -0400 | [diff] [blame] | 2540 | } |
Theodore Ts'o | 5b643f9 | 2015-05-18 13:14:47 -0400 | [diff] [blame] | 2541 | err = add_dirent_to_buf(handle, fname, dir, inode, de, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2542 | goto cleanup; |
| 2543 | |
| 2544 | journal_error: |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2545 | ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2546 | cleanup: |
Guo Chao | b1deefc | 2013-01-28 21:41:02 -0500 | [diff] [blame] | 2547 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2548 | dx_release(frames); |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2549 | /* @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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2554 | return err; |
| 2555 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2556 | |
| 2557 | /* |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2558 | * ext4_generic_delete_entry deletes a directory entry by merging it |
| 2559 | * with the previous entry |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2560 | */ |
Kyoungho Koo | 2fe34d29 | 2020-08-10 17:07:05 +0900 | [diff] [blame] | 2561 | int ext4_generic_delete_entry(struct inode *dir, |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2562 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2567 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2568 | struct ext4_dir_entry_2 *de, *pde; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2569 | unsigned int blocksize = dir->i_sb->s_blocksize; |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2570 | int i; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2571 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2572 | i = 0; |
| 2573 | pde = NULL; |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2574 | de = (struct ext4_dir_entry_2 *)entry_buf; |
| 2575 | while (i < buf_size - csum_size) { |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2576 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
Jan Kara | 7303cb5 | 2020-07-31 18:21:35 +0200 | [diff] [blame] | 2577 | entry_buf, buf_size, i)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2578 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2579 | if (de == de_del) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2580 | if (pde) |
Jan Kara | a72d7f8 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2581 | pde->rec_len = ext4_rec_len_to_disk( |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2582 | ext4_rec_len_from_disk(pde->rec_len, |
| 2583 | blocksize) + |
| 2584 | ext4_rec_len_from_disk(de->rec_len, |
| 2585 | blocksize), |
| 2586 | blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2587 | else |
| 2588 | de->inode = 0; |
Artem Blagodarenko | e08ac99 | 2017-06-21 21:09:57 -0400 | [diff] [blame] | 2589 | inode_inc_iversion(dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2590 | return 0; |
| 2591 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2592 | i += ext4_rec_len_from_disk(de->rec_len, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2593 | pde = de; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2594 | de = ext4_next_entry(de, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2595 | } |
| 2596 | return -ENOENT; |
| 2597 | } |
| 2598 | |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2599 | static 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 Ma | 9f40fe5 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2606 | 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 Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 2614 | if (ext4_has_metadata_csum(dir->i_sb)) |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2615 | 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 Koo | 2fe34d29 | 2020-08-10 17:07:05 +0900 | [diff] [blame] | 2622 | err = ext4_generic_delete_entry(dir, de_del, bh, bh->b_data, |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2623 | 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'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 2628 | err = ext4_handle_dirty_dirblock(handle, dir, bh); |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2629 | if (unlikely(err)) |
| 2630 | goto out; |
| 2631 | |
| 2632 | return 0; |
| 2633 | out: |
| 2634 | if (err != -ENOENT) |
| 2635 | ext4_std_error(dir->i_sb, err); |
| 2636 | return err; |
| 2637 | } |
| 2638 | |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2639 | /* |
Andreas Dilger | c741489 | 2017-08-05 19:47:34 -0400 | [diff] [blame] | 2640 | * 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 Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2649 | */ |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 2650 | static void ext4_inc_count(struct inode *inode) |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2651 | { |
| 2652 | inc_nlink(inode); |
Andreas Dilger | c741489 | 2017-08-05 19:47:34 -0400 | [diff] [blame] | 2653 | if (is_dx(inode) && |
| 2654 | (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2)) |
| 2655 | set_nlink(inode, 1); |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2656 | } |
| 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 Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 2662 | static void ext4_dec_count(struct inode *inode) |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2663 | { |
Andreas Dilger | 909a4cf | 2011-10-26 03:22:31 -0400 | [diff] [blame] | 2664 | if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) |
| 2665 | drop_nlink(inode); |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2669 | /* |
| 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2675 | static int ext4_add_nondir(handle_t *handle, |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2676 | struct dentry *dentry, struct inode **inodep) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2677 | { |
Jan Kara | a9e2632 | 2019-11-05 17:44:10 +0100 | [diff] [blame] | 2678 | struct inode *dir = d_inode(dentry->d_parent); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2679 | struct inode *inode = *inodep; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2680 | int err = ext4_add_entry(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2681 | if (!err) { |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2682 | err = ext4_mark_inode_dirty(handle, inode); |
Jan Kara | a9e2632 | 2019-11-05 17:44:10 +0100 | [diff] [blame] | 2683 | if (IS_DIRSYNC(dir)) |
| 2684 | ext4_handle_sync(handle); |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 2685 | d_instantiate_new(dentry, inode); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2686 | *inodep = NULL; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2687 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2688 | } |
Eric Sandeen | 731b9a5 | 2007-02-10 01:46:16 -0800 | [diff] [blame] | 2689 | drop_nlink(inode); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2690 | ext4_orphan_add(handle, inode); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2691 | unlock_new_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2692 | 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 Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2703 | static int ext4_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 2704 | struct dentry *dentry, umode_t mode, bool excl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2705 | { |
| 2706 | handle_t *handle; |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 2707 | struct inode *inode; |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2708 | int err, credits, retries = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2709 | |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 2710 | err = dquot_initialize(dir); |
| 2711 | if (err) |
| 2712 | return err; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2713 | |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2714 | credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
Jan Kara | eb9cc7e | 2013-04-19 13:38:14 -0400 | [diff] [blame] | 2715 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2716 | retry: |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 2717 | inode = ext4_new_inode_start_handle(mnt_userns, dir, mode, &dentry->d_name, |
| 2718 | 0, NULL, EXT4_HT_DIR, credits); |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2719 | handle = ext4_journal_current_handle(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2720 | err = PTR_ERR(inode); |
| 2721 | if (!IS_ERR(inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2722 | inode->i_op = &ext4_file_inode_operations; |
Boaz Harrosh | be64f88 | 2015-04-15 16:15:17 -0700 | [diff] [blame] | 2723 | inode->i_fop = &ext4_file_operations; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2724 | ext4_set_aops(inode); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2725 | err = ext4_add_nondir(handle, dentry, &inode); |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 2726 | if (!err) |
| 2727 | ext4_fc_track_create(handle, dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2728 | } |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2729 | if (handle) |
| 2730 | ext4_journal_stop(handle); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2731 | if (!IS_ERR_OR_NULL(inode)) |
| 2732 | iput(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2733 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2734 | goto retry; |
| 2735 | return err; |
| 2736 | } |
| 2737 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2738 | static int ext4_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 2739 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2740 | { |
| 2741 | handle_t *handle; |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 2742 | struct inode *inode; |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2743 | int err, credits, retries = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2744 | |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 2745 | err = dquot_initialize(dir); |
| 2746 | if (err) |
| 2747 | return err; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2748 | |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2749 | credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
Jan Kara | eb9cc7e | 2013-04-19 13:38:14 -0400 | [diff] [blame] | 2750 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2751 | retry: |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 2752 | inode = ext4_new_inode_start_handle(mnt_userns, dir, mode, &dentry->d_name, |
| 2753 | 0, NULL, EXT4_HT_DIR, credits); |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2754 | handle = ext4_journal_current_handle(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2755 | err = PTR_ERR(inode); |
| 2756 | if (!IS_ERR(inode)) { |
| 2757 | init_special_inode(inode, inode->i_mode, rdev); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2758 | inode->i_op = &ext4_special_inode_operations; |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2759 | err = ext4_add_nondir(handle, dentry, &inode); |
Harshad Shirwadkar | aa75f4d | 2020-10-15 13:37:57 -0700 | [diff] [blame] | 2760 | if (!err) |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 2761 | ext4_fc_track_create(handle, dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2762 | } |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2763 | if (handle) |
| 2764 | ext4_journal_stop(handle); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2765 | if (!IS_ERR_OR_NULL(inode)) |
| 2766 | iput(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2767 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2768 | goto retry; |
| 2769 | return err; |
| 2770 | } |
| 2771 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2772 | static int ext4_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
| 2773 | struct dentry *dentry, umode_t mode) |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2774 | { |
| 2775 | handle_t *handle; |
| 2776 | struct inode *inode; |
| 2777 | int err, retries = 0; |
| 2778 | |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 2779 | err = dquot_initialize(dir); |
| 2780 | if (err) |
| 2781 | return err; |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2782 | |
| 2783 | retry: |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 2784 | inode = ext4_new_inode_start_handle(mnt_userns, dir, mode, |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2785 | 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 Harrosh | be64f88 | 2015-04-15 16:15:17 -0700 | [diff] [blame] | 2793 | inode->i_fop = &ext4_file_operations; |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2794 | ext4_set_aops(inode); |
Zheng Liu | e94bd34 | 2013-07-20 21:58:38 -0400 | [diff] [blame] | 2795 | d_tmpfile(dentry, inode); |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2796 | err = ext4_orphan_add(handle, inode); |
| 2797 | if (err) |
Miklos Szeredi | 43ae9e3 | 2013-10-10 16:48:19 +0200 | [diff] [blame] | 2798 | goto err_unlock_inode; |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2799 | mark_inode_dirty(inode); |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2800 | 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 Szeredi | 43ae9e3 | 2013-10-10 16:48:19 +0200 | [diff] [blame] | 2807 | err_unlock_inode: |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2808 | ext4_journal_stop(handle); |
| 2809 | unlock_new_inode(inode); |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 2810 | return err; |
| 2811 | } |
| 2812 | |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2813 | struct 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2817 | { |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2818 | de->inode = cpu_to_le32(inode->i_ino); |
| 2819 | de->name_len = 1; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2820 | de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL), |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2821 | 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 Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2830 | (csum_size + ext4_dir_rec_len(1, NULL)), |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2831 | blocksize); |
| 2832 | else |
| 2833 | de->rec_len = ext4_rec_len_to_disk( |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2834 | ext4_dir_rec_len(de->name_len, NULL), |
| 2835 | blocksize); |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2836 | 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 Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 2842 | int ext4_init_new_dir(handle_t *handle, struct inode *dir, |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2843 | struct inode *inode) |
| 2844 | { |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2845 | struct buffer_head *dir_block = NULL; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2846 | struct ext4_dir_entry_2 *de; |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2847 | ext4_lblk_t block = 0; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2848 | unsigned int blocksize = dir->i_sb->s_blocksize; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2849 | int csum_size = 0; |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2850 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2851 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 2852 | if (ext4_has_metadata_csum(dir->i_sb)) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2853 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 2854 | |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2855 | 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'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2863 | inode->i_size = 0; |
Theodore Ts'o | 0f70b40 | 2013-02-15 03:35:57 -0500 | [diff] [blame] | 2864 | dir_block = ext4_append(handle, inode, &block); |
| 2865 | if (IS_ERR(dir_block)) |
| 2866 | return PTR_ERR(dir_block); |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2867 | 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'o | ddce3b9 | 2019-06-21 16:31:47 -0400 | [diff] [blame] | 2870 | if (csum_size) |
| 2871 | ext4_initialize_dirent_tail(dir_block, blocksize); |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2872 | |
| 2873 | BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata"); |
Theodore Ts'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 2874 | err = ext4_handle_dirty_dirblock(handle, inode, dir_block); |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2875 | if (err) |
| 2876 | goto out; |
| 2877 | set_buffer_verified(dir_block); |
| 2878 | out: |
| 2879 | brelse(dir_block); |
| 2880 | return err; |
| 2881 | } |
| 2882 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2883 | static int ext4_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 2884 | struct dentry *dentry, umode_t mode) |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2885 | { |
| 2886 | handle_t *handle; |
| 2887 | struct inode *inode; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2888 | int err, err2 = 0, credits, retries = 0; |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2889 | |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2890 | if (EXT4_DIR_LINK_MAX(dir)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2891 | return -EMLINK; |
| 2892 | |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 2893 | err = dquot_initialize(dir); |
| 2894 | if (err) |
| 2895 | return err; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2896 | |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2897 | credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
Jan Kara | eb9cc7e | 2013-04-19 13:38:14 -0400 | [diff] [blame] | 2898 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2899 | retry: |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 2900 | inode = ext4_new_inode_start_handle(mnt_userns, dir, S_IFDIR | mode, |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2901 | &dentry->d_name, |
| 2902 | 0, NULL, EXT4_HT_DIR, credits); |
| 2903 | handle = ext4_journal_current_handle(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2904 | err = PTR_ERR(inode); |
| 2905 | if (IS_ERR(inode)) |
| 2906 | goto out_stop; |
| 2907 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2908 | inode->i_op = &ext4_dir_inode_operations; |
| 2909 | inode->i_fop = &ext4_dir_operations; |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2910 | err = ext4_init_new_dir(handle, dir, inode); |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2911 | if (err) |
| 2912 | goto out_clear_inode; |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2913 | err = ext4_mark_inode_dirty(handle, inode); |
| 2914 | if (!err) |
| 2915 | err = ext4_add_entry(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2916 | if (err) { |
Aneesh Kumar K.V | 4cdeed8 | 2008-02-22 06:17:31 -0500 | [diff] [blame] | 2917 | out_clear_inode: |
| 2918 | clear_nlink(inode); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2919 | ext4_orphan_add(handle, inode); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2920 | unlock_new_inode(inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 2921 | err2 = ext4_mark_inode_dirty(handle, inode); |
| 2922 | if (unlikely(err2)) |
| 2923 | err = err2; |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2924 | ext4_journal_stop(handle); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2925 | iput(inode); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2926 | goto out_retry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2927 | } |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 2928 | ext4_inc_count(dir); |
Harshad Shirwadkar | aa75f4d | 2020-10-15 13:37:57 -0700 | [diff] [blame] | 2929 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2930 | ext4_update_dx_flag(dir); |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2931 | err = ext4_mark_inode_dirty(handle, dir); |
| 2932 | if (err) |
| 2933 | goto out_clear_inode; |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 2934 | d_instantiate_new(dentry, inode); |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 2935 | ext4_fc_track_create(handle, dentry); |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2936 | if (IS_DIRSYNC(dir)) |
| 2937 | ext4_handle_sync(handle); |
| 2938 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2939 | out_stop: |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 2940 | if (handle) |
| 2941 | ext4_journal_stop(handle); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 2942 | out_retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2943 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2944 | goto retry; |
| 2945 | return err; |
| 2946 | } |
| 2947 | |
| 2948 | /* |
| 2949 | * routine to check that the specified directory is empty (for rmdir) |
| 2950 | */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2951 | bool ext4_empty_dir(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2952 | { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2953 | unsigned int offset; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2954 | struct buffer_head *bh; |
Jan Kara | 64d4ce8 | 2019-12-02 18:02:12 +0100 | [diff] [blame] | 2955 | struct ext4_dir_entry_2 *de; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2956 | struct super_block *sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2957 | |
Tao Ma | 61f8663 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 2958 | if (ext4_has_inline_data(inode)) { |
| 2959 | int has_inline_data = 1; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2960 | int ret; |
Tao Ma | 61f8663 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 2961 | |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2962 | ret = empty_inline_dir(inode, &has_inline_data); |
Tao Ma | 61f8663 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 2963 | if (has_inline_data) |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2964 | return ret; |
Tao Ma | 61f8663 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 2965 | } |
| 2966 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2967 | sb = inode->i_sb; |
Daniel Rosenberg | 471fbbe | 2021-03-19 07:34:13 +0000 | [diff] [blame] | 2968 | if (inode->i_size < ext4_dir_rec_len(1, NULL) + |
| 2969 | ext4_dir_rec_len(2, NULL)) { |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2970 | EXT4_ERROR_INODE(inode, "invalid size"); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2971 | return true; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2972 | } |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 2973 | /* 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'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2977 | if (IS_ERR(bh)) |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2978 | return true; |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 2979 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2980 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
Jan Kara | 64d4ce8 | 2019-12-02 18:02:12 +0100 | [diff] [blame] | 2981 | 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'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2985 | brelse(bh); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 2986 | return true; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2987 | } |
Jan Kara | 64d4ce8 | 2019-12-02 18:02:12 +0100 | [diff] [blame] | 2988 | 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'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2998 | while (offset < inode->i_size) { |
Jan Kara | 64d4ce8 | 2019-12-02 18:02:12 +0100 | [diff] [blame] | 2999 | if (!(offset & (sb->s_blocksize - 1))) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 3000 | unsigned int lblock; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3001 | brelse(bh); |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 3002 | lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 3003 | bh = ext4_read_dirblock(inode, lblock, EITHER); |
Theodore Ts'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 3004 | if (bh == NULL) { |
| 3005 | offset += sb->s_blocksize; |
| 3006 | continue; |
| 3007 | } |
Theodore Ts'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 3008 | if (IS_ERR(bh)) |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 3009 | return true; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3010 | } |
Jan Kara | 64d4ce8 | 2019-12-02 18:02:12 +0100 | [diff] [blame] | 3011 | de = (struct ext4_dir_entry_2 *) (bh->b_data + |
| 3012 | (offset & (sb->s_blocksize - 1))); |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 3013 | if (ext4_check_dir_entry(inode, NULL, de, bh, |
| 3014 | bh->b_data, bh->b_size, offset)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3015 | offset = (offset | (sb->s_blocksize - 1)) + 1; |
| 3016 | continue; |
| 3017 | } |
| 3018 | if (le32_to_cpu(de->inode)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3019 | brelse(bh); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 3020 | return false; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3021 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 3022 | offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3023 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3024 | brelse(bh); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 3025 | return true; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3028 | /* |
| 3029 | * ext4_orphan_add() links an unlinked or truncated inode into a list of |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3030 | * 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3035 | * inodes and truncating linked inodes in ext4_orphan_cleanup(). |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3036 | * |
| 3037 | * Orphan list manipulation functions must be called under i_mutex unless |
| 3038 | * we are just creating the inode or deleting it. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3039 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3040 | int ext4_orphan_add(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3041 | { |
| 3042 | struct super_block *sb = inode->i_sb; |
Jan Kara | cd2c080 | 2014-05-26 11:39:17 -0400 | [diff] [blame] | 3043 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3044 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3045 | int err = 0, rc; |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3046 | bool dirty = false; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3047 | |
Theodore Ts'o | e2bfb08 | 2014-10-05 22:47:07 -0400 | [diff] [blame] | 3048 | if (!sbi->s_journal || is_bad_inode(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3049 | return 0; |
| 3050 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3051 | WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3052 | !inode_is_locked(inode)); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3053 | /* |
| 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3057 | if (!list_empty(&EXT4_I(inode)->i_orphan)) |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3058 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3059 | |
Lukas Czerner | afb8617 | 2011-07-11 18:47:04 -0400 | [diff] [blame] | 3060 | /* |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3065 | */ |
Chunguang Xu | 837c23f | 2020-11-07 23:58:11 +0800 | [diff] [blame] | 3066 | ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3067 | S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3068 | |
Jan Kara | cd2c080 | 2014-05-26 11:39:17 -0400 | [diff] [blame] | 3069 | BUFFER_TRACE(sbi->s_sbh, "get_write_access"); |
| 3070 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3071 | if (err) |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3072 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3073 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3074 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3075 | if (err) |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3076 | goto out; |
| 3077 | |
| 3078 | mutex_lock(&sbi->s_orphan_lock); |
Dmitry Monakhov | 6e3617e | 2010-03-01 23:29:39 -0500 | [diff] [blame] | 3079 | /* |
| 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 Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3083 | 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 Kara | 05c2c00 | 2020-12-16 11:18:39 +0100 | [diff] [blame] | 3087 | lock_buffer(sbi->s_sbh); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3088 | sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); |
Jan Kara | 05c2c00 | 2020-12-16 11:18:39 +0100 | [diff] [blame] | 3089 | ext4_superblock_csum_set(sb); |
| 3090 | unlock_buffer(sbi->s_sbh); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3091 | dirty = true; |
| 3092 | } |
| 3093 | list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan); |
| 3094 | mutex_unlock(&sbi->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3095 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3096 | if (dirty) { |
Jan Kara | a3f5cf1 | 2020-12-16 11:18:44 +0100 | [diff] [blame] | 3097 | err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3098 | 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 Kara | 74177f55 | 2016-05-05 11:10:15 -0400 | [diff] [blame] | 3108 | list_del_init(&EXT4_I(inode)->i_orphan); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3109 | mutex_unlock(&sbi->s_orphan_lock); |
| 3110 | } |
Vasily Averin | feaf264 | 2018-11-06 17:01:36 -0500 | [diff] [blame] | 3111 | } else |
| 3112 | brelse(iloc.bh); |
| 3113 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3114 | 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 Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3117 | out: |
Jan Kara | cd2c080 | 2014-05-26 11:39:17 -0400 | [diff] [blame] | 3118 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3119 | return err; |
| 3120 | } |
| 3121 | |
| 3122 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3123 | * ext4_orphan_del() removes an unlinked or truncated inode from the list |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3124 | * of such inodes stored on disk, because it is finally being cleaned up. |
| 3125 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3126 | int ext4_orphan_del(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3127 | { |
| 3128 | struct list_head *prev; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3129 | struct ext4_inode_info *ei = EXT4_I(inode); |
Jan Kara | cd2c080 | 2014-05-26 11:39:17 -0400 | [diff] [blame] | 3130 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3131 | __u32 ino_next; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3132 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3133 | int err = 0; |
| 3134 | |
Jan Kara | cd2c080 | 2014-05-26 11:39:17 -0400 | [diff] [blame] | 3135 | if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3136 | return 0; |
| 3137 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3138 | WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3139 | !inode_is_locked(inode)); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3140 | /* Do this quick check before taking global s_orphan_lock. */ |
Theodore Ts'o | 3b9d4ed | 2009-04-25 22:54:04 -0400 | [diff] [blame] | 3141 | if (list_empty(&ei->i_orphan)) |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3142 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3143 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3144 | if (handle) { |
| 3145 | /* Grab inode buffer early before taking global s_orphan_lock */ |
| 3146 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
| 3147 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3148 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3149 | mutex_lock(&sbi->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3150 | jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); |
| 3151 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3152 | prev = ei->i_orphan.prev; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3153 | 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 Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3159 | if (!handle || err) { |
| 3160 | mutex_unlock(&sbi->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3161 | goto out_err; |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3162 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3163 | |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3164 | ino_next = NEXT_ORPHAN(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3165 | if (prev == &sbi->s_orphan) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3166 | jbd_debug(4, "superblock will point to %u\n", ino_next); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3167 | BUFFER_TRACE(sbi->s_sbh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3168 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3169 | if (err) { |
| 3170 | mutex_unlock(&sbi->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3171 | goto out_brelse; |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3172 | } |
Jan Kara | 05c2c00 | 2020-12-16 11:18:39 +0100 | [diff] [blame] | 3173 | lock_buffer(sbi->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3174 | sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); |
Jan Kara | 05c2c00 | 2020-12-16 11:18:39 +0100 | [diff] [blame] | 3175 | ext4_superblock_csum_set(inode->i_sb); |
| 3176 | unlock_buffer(sbi->s_sbh); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3177 | mutex_unlock(&sbi->s_orphan_lock); |
Jan Kara | a3f5cf1 | 2020-12-16 11:18:44 +0100 | [diff] [blame] | 3178 | err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3179 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3180 | struct ext4_iloc iloc2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3181 | struct inode *i_prev = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3182 | &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3183 | |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3184 | jbd_debug(4, "orphan inode %lu will point to %u\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3185 | i_prev->i_ino, ino_next); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3186 | err = ext4_reserve_inode_write(handle, i_prev, &iloc2); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3187 | if (err) { |
| 3188 | mutex_unlock(&sbi->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3189 | goto out_brelse; |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3190 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3191 | NEXT_ORPHAN(i_prev) = ino_next; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3192 | err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); |
Jan Kara | d745a8c | 2014-05-26 11:56:53 -0400 | [diff] [blame] | 3193 | mutex_unlock(&sbi->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3194 | } |
| 3195 | if (err) |
| 3196 | goto out_brelse; |
| 3197 | NEXT_ORPHAN(inode) = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3198 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3199 | out_err: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3200 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3201 | return err; |
| 3202 | |
| 3203 | out_brelse: |
| 3204 | brelse(iloc.bh); |
| 3205 | goto out_err; |
| 3206 | } |
| 3207 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3208 | static int ext4_rmdir(struct inode *dir, struct dentry *dentry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3209 | { |
| 3210 | int retval; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3211 | struct inode *inode; |
| 3212 | struct buffer_head *bh; |
| 3213 | struct ext4_dir_entry_2 *de; |
Theodore Ts'o | 8dcfaad | 2013-02-09 09:45:11 -0500 | [diff] [blame] | 3214 | handle_t *handle = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3215 | |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 3216 | if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb)))) |
| 3217 | return -EIO; |
| 3218 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3219 | /* Initialize quotas before so that eventual writes go in |
| 3220 | * separate transaction */ |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 3221 | retval = dquot_initialize(dir); |
| 3222 | if (retval) |
| 3223 | return retval; |
| 3224 | retval = dquot_initialize(d_inode(dentry)); |
| 3225 | if (retval) |
| 3226 | return retval; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 3227 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3228 | retval = -ENOENT; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 3229 | bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 3230 | if (IS_ERR(bh)) |
| 3231 | return PTR_ERR(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3232 | if (!bh) |
| 3233 | goto end_rmdir; |
| 3234 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3235 | inode = d_inode(dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3236 | |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3237 | retval = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3238 | if (le32_to_cpu(de->inode) != inode->i_ino) |
| 3239 | goto end_rmdir; |
| 3240 | |
| 3241 | retval = -ENOTEMPTY; |
Michael Halcrow | e875a2d | 2015-04-11 07:46:49 -0400 | [diff] [blame] | 3242 | if (!ext4_empty_dir(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3243 | goto end_rmdir; |
| 3244 | |
Theodore Ts'o | 8dcfaad | 2013-02-09 09:45:11 -0500 | [diff] [blame] | 3245 | handle = ext4_journal_start(dir, EXT4_HT_DIR, |
Theodore Ts'o | 64044ab | 2013-02-09 15:06:24 -0500 | [diff] [blame] | 3246 | EXT4_DATA_TRANS_BLOCKS(dir->i_sb)); |
Theodore Ts'o | 8dcfaad | 2013-02-09 09:45:11 -0500 | [diff] [blame] | 3247 | 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3256 | retval = ext4_delete_entry(handle, dir, de, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3257 | if (retval) |
| 3258 | goto end_rmdir; |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 3259 | if (!EXT4_DIR_LINK_EMPTY(inode)) |
Andreas Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 3260 | ext4_warning_inode(inode, |
| 3261 | "empty directory '%.*s' has too many links (%u)", |
| 3262 | dentry->d_name.len, dentry->d_name.name, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3263 | inode->i_nlink); |
Jeff Layton | ee73f9a | 2018-01-09 08:21:39 -0500 | [diff] [blame] | 3264 | inode_inc_iversion(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3265 | 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 Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3270 | ext4_orphan_add(handle, inode); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 3271 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3272 | retval = ext4_mark_inode_dirty(handle, inode); |
| 3273 | if (retval) |
| 3274 | goto end_rmdir; |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 3275 | ext4_dec_count(dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3276 | ext4_update_dx_flag(dir); |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3277 | ext4_fc_track_unlink(handle, dentry); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3278 | retval = ext4_mark_inode_dirty(handle, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3279 | |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 3280 | #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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3291 | end_rmdir: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3292 | brelse(bh); |
Theodore Ts'o | 8dcfaad | 2013-02-09 09:45:11 -0500 | [diff] [blame] | 3293 | if (handle) |
| 3294 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3295 | return retval; |
| 3296 | } |
| 3297 | |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3298 | int __ext4_unlink(handle_t *handle, struct inode *dir, const struct qstr *d_name, |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3299 | struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3300 | { |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3301 | int retval = -ENOENT; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3302 | struct buffer_head *bh; |
| 3303 | struct ext4_dir_entry_2 *de; |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3304 | int skip_remove_dentry = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3305 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3306 | bh = ext4_find_entry(dir, d_name, &de, NULL); |
| 3307 | if (IS_ERR(bh)) |
| 3308 | return PTR_ERR(bh); |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 3309 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3310 | if (!bh) |
| 3311 | return -ENOENT; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3312 | |
Yi Zhuang | e5f7815 | 2020-06-29 20:26:21 +0800 | [diff] [blame] | 3313 | if (le32_to_cpu(de->inode) != inode->i_ino) { |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3314 | /* |
| 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 Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3322 | goto out; |
Theodore Ts'o | 931b686 | 2013-02-09 09:43:39 -0500 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | if (IS_DIRSYNC(dir)) |
| 3326 | ext4_handle_sync(handle); |
| 3327 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3328 | if (!skip_remove_dentry) { |
| 3329 | retval = ext4_delete_entry(handle, dir, de, bh); |
| 3330 | if (retval) |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3331 | goto out; |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3332 | 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 Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3336 | goto out; |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3337 | } else { |
| 3338 | retval = 0; |
| 3339 | } |
Theodore Ts'o | c7df4a1 | 2019-11-11 22:18:13 -0500 | [diff] [blame] | 3340 | if (inode->i_nlink == 0) |
| 3341 | ext4_warning_inode(inode, "Deleting file '%.*s' with no links", |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3342 | d_name->len, d_name->name); |
Theodore Ts'o | c7df4a1 | 2019-11-11 22:18:13 -0500 | [diff] [blame] | 3343 | else |
| 3344 | drop_nlink(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3345 | if (!inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3346 | ext4_orphan_add(handle, inode); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 3347 | inode->i_ctime = current_time(inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3348 | retval = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3349 | |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3350 | out: |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3351 | brelse(bh); |
| 3352 | return retval; |
| 3353 | } |
| 3354 | |
| 3355 | static int ext4_unlink(struct inode *dir, struct dentry *dentry) |
| 3356 | { |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3357 | handle_t *handle; |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3358 | 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 Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3375 | 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 Shirwadkar | aa75f4d | 2020-10-15 13:37:57 -0700 | [diff] [blame] | 3383 | if (!retval) |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3384 | ext4_fc_track_unlink(handle, dentry); |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 3385 | #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 Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3395 | if (handle) |
| 3396 | ext4_journal_stop(handle); |
Gabriel Krisman Bertazi | b886ee3 | 2019-04-25 14:12:08 -0400 | [diff] [blame] | 3397 | |
Yi Zhuang | e5f7815 | 2020-06-29 20:26:21 +0800 | [diff] [blame] | 3398 | out_trace: |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3399 | trace_ext4_unlink_exit(dentry, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3400 | return retval; |
| 3401 | } |
| 3402 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3403 | static int ext4_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3404 | struct dentry *dentry, const char *symname) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3405 | { |
| 3406 | handle_t *handle; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3407 | struct inode *inode; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3408 | int err, len = strlen(symname); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3409 | int credits; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 3410 | struct fscrypt_str disk_link; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3411 | |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 3412 | if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb)))) |
| 3413 | return -EIO; |
| 3414 | |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3415 | err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize, |
| 3416 | &disk_link); |
| 3417 | if (err) |
| 3418 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3419 | |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 3420 | err = dquot_initialize(dir); |
| 3421 | if (err) |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3422 | return err; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 3423 | |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3424 | if ((disk_link.len > EXT4_N_BLOCKS * 4)) { |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3425 | /* |
| 3426 | * For non-fast symlinks, we just allocate inode and put it on |
| 3427 | * orphan list in the first transaction => we need bitmap, |
Eric Sandeen | 8c20871 | 2011-08-11 09:54:31 -0500 | [diff] [blame] | 3428 | * group descriptor, sb, inode block, quota blocks, and |
| 3429 | * possibly selinux xattr blocks. |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3430 | */ |
Eric Sandeen | 8c20871 | 2011-08-11 09:54:31 -0500 | [diff] [blame] | 3431 | credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + |
| 3432 | EXT4_XATTR_TRANS_BLOCKS; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3433 | } 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 Kara | eb9cc7e | 2013-04-19 13:38:14 -0400 | [diff] [blame] | 3441 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3442 | } |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3443 | |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 3444 | inode = ext4_new_inode_start_handle(mnt_userns, dir, S_IFLNK|S_IRWXUGO, |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 3445 | &dentry->d_name, 0, NULL, |
| 3446 | EXT4_HT_DIR, credits); |
| 3447 | handle = ext4_journal_current_handle(); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3448 | if (IS_ERR(inode)) { |
| 3449 | if (handle) |
| 3450 | ext4_journal_stop(handle); |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3451 | return PTR_ERR(inode); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3452 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3453 | |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3454 | if (IS_ENCRYPTED(inode)) { |
| 3455 | err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 3456 | if (err) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3457 | goto err_drop_inode; |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 3458 | inode->i_op = &ext4_encrypted_symlink_inode_operations; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | if ((disk_link.len > EXT4_N_BLOCKS * 4)) { |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3462 | if (!IS_ENCRYPTED(inode)) |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 3463 | inode->i_op = &ext4_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3464 | inode_nohighmem(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3465 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3466 | /* |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3467 | * 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3475 | */ |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3476 | drop_nlink(inode); |
| 3477 | err = ext4_orphan_add(handle, inode); |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3478 | if (handle) |
| 3479 | ext4_journal_stop(handle); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3480 | handle = NULL; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3481 | if (err) |
| 3482 | goto err_drop_inode; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3483 | err = __page_symlink(inode, disk_link.name, disk_link.len, 1); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3484 | 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'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 3490 | handle = ext4_journal_start(dir, EXT4_HT_DIR, |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3491 | 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'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3495 | handle = NULL; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3496 | goto err_drop_inode; |
| 3497 | } |
Al Viro | 0ce8c010 | 2012-01-08 19:50:23 -0500 | [diff] [blame] | 3498 | set_nlink(inode, 1); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3499 | err = ext4_orphan_del(handle, inode); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3500 | if (err) |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3501 | goto err_drop_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3502 | } else { |
Aneesh Kumar K.V | e65187e | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3503 | /* clear the extent format for fast symlink */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 3504 | ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3505 | if (!IS_ENCRYPTED(inode)) { |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 3506 | inode->i_op = &ext4_fast_symlink_inode_operations; |
Al Viro | 75e7566 | 2015-05-02 10:13:58 -0400 | [diff] [blame] | 3507 | inode->i_link = (char *)&EXT4_I(inode)->i_data; |
| 3508 | } |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3509 | memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name, |
| 3510 | disk_link.len); |
| 3511 | inode->i_size = disk_link.len - 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3512 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3513 | EXT4_I(inode)->i_disksize = inode->i_size; |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 3514 | err = ext4_add_nondir(handle, dentry, &inode); |
Theodore Ts'o | 1139575 | 2013-02-09 16:27:09 -0500 | [diff] [blame] | 3515 | if (handle) |
| 3516 | ext4_journal_stop(handle); |
Jan Kara | 9b88f9f | 2019-11-05 17:44:11 +0100 | [diff] [blame] | 3517 | if (inode) |
| 3518 | iput(inode); |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3519 | goto out_free_encrypted_link; |
| 3520 | |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3521 | err_drop_inode: |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3522 | if (handle) |
| 3523 | ext4_journal_stop(handle); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 3524 | clear_nlink(inode); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3525 | unlock_new_inode(inode); |
| 3526 | iput(inode); |
Eric Biggers | 78e1060 | 2018-01-11 22:10:40 -0500 | [diff] [blame] | 3527 | out_free_encrypted_link: |
| 3528 | if (disk_link.name != (unsigned char *)symname) |
| 3529 | kfree(disk_link.name); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 3530 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3531 | } |
| 3532 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3533 | int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3534 | { |
| 3535 | handle_t *handle; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3536 | int err, retries = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3537 | retry: |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 3538 | handle = ext4_journal_start(dir, EXT4_HT_DIR, |
| 3539 | (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 3540 | EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3541 | if (IS_ERR(handle)) |
| 3542 | return PTR_ERR(handle); |
| 3543 | |
| 3544 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3545 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3546 | |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 3547 | inode->i_ctime = current_time(inode); |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 3548 | ext4_inc_count(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 3549 | ihold(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3550 | |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 3551 | err = ext4_add_entry(handle, dentry, inode); |
| 3552 | if (!err) { |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3553 | err = ext4_mark_inode_dirty(handle, inode); |
Al Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 3554 | /* 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 Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 3559 | d_instantiate(dentry, inode); |
Harshad Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 3560 | ext4_fc_track_link(handle, dentry); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 3561 | } else { |
| 3562 | drop_nlink(inode); |
| 3563 | iput(inode); |
| 3564 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3565 | ext4_journal_stop(handle); |
| 3566 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3567 | goto retry; |
| 3568 | return err; |
| 3569 | } |
| 3570 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3571 | static 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 Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 3594 | |
| 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 | */ |
| 3600 | static 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'o | 4e19d6b | 2019-06-20 21:19:02 -0400 | [diff] [blame] | 3609 | /* 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'o | dc6982f | 2013-02-14 23:59:26 -0500 | [diff] [blame] | 3613 | if (IS_ERR(bh)) { |
| 3614 | *retval = PTR_ERR(bh); |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 3615 | 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3626 | |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3627 | struct ext4_renament { |
| 3628 | struct inode *dir; |
| 3629 | struct dentry *dentry; |
| 3630 | struct inode *inode; |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3631 | bool is_dir; |
| 3632 | int dir_nlink_delta; |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3633 | |
| 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 Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3645 | static 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. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3655 | return -EFSCORRUPTED; |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3656 | BUFFER_TRACE(ent->dir_bh, "get_write_access"); |
| 3657 | return ext4_journal_get_write_access(handle, ent->dir_bh); |
| 3658 | } |
| 3659 | |
| 3660 | static 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'o | f036adb | 2019-06-21 15:49:26 -0400 | [diff] [blame] | 3673 | retval = ext4_handle_dirty_dirblock(handle, ent->inode, |
| 3674 | ent->dir_bh); |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3675 | } |
| 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 | |
| 3686 | static int ext4_setent(handle_t *handle, struct ext4_renament *ent, |
| 3687 | unsigned ino, unsigned file_type) |
| 3688 | { |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3689 | int retval, retval2; |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3690 | |
| 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. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3696 | if (ext4_has_feature_filetype(ent->dir->i_sb)) |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3697 | ent->de->file_type = file_type; |
Jeff Layton | ee73f9a | 2018-01-09 08:21:39 -0500 | [diff] [blame] | 3698 | inode_inc_iversion(ent->dir); |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3699 | ent->dir->i_ctime = ent->dir->i_mtime = |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 3700 | current_time(ent->dir); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3701 | retval = ext4_mark_inode_dirty(handle, ent->dir); |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3702 | BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata"); |
| 3703 | if (!ent->inlined) { |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3704 | 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 Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3708 | } |
| 3709 | } |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3710 | return retval; |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3711 | } |
| 3712 | |
zhangyi (F) | b7ff91f | 2021-03-03 21:17:02 +0800 | [diff] [blame] | 3713 | static 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 Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3738 | static 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'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 3746 | if (IS_ERR(bh)) |
| 3747 | return PTR_ERR(bh); |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3748 | if (bh) { |
| 3749 | retval = ext4_delete_entry(handle, dir, de, bh); |
| 3750 | brelse(bh); |
| 3751 | } |
| 3752 | return retval; |
| 3753 | } |
| 3754 | |
Darrick J. Wong | d80d448 | 2014-08-27 18:40:09 -0400 | [diff] [blame] | 3755 | static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent, |
| 3756 | int force_reread) |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3757 | { |
| 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. Wong | d80d448 | 2014-08-27 18:40:09 -0400 | [diff] [blame] | 3768 | ent->de->name_len) || |
| 3769 | force_reread) { |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3770 | 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 Dilger | b03a2f7 | 2015-06-15 14:50:26 -0400 | [diff] [blame] | 3781 | ext4_warning_inode(ent->dir, |
| 3782 | "Deleting old file: nlink %d, error=%d", |
| 3783 | ent->dir->i_nlink, retval); |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3784 | } |
| 3785 | } |
| 3786 | |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3787 | static 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 Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 3791 | ext4_dec_count(ent->dir); |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3792 | else |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 3793 | ext4_inc_count(ent->dir); |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3794 | ext4_mark_inode_dirty(handle, ent->dir); |
| 3795 | } |
| 3796 | } |
| 3797 | |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 3798 | static struct inode *ext4_whiteout_for_rename(struct user_namespace *mnt_userns, |
| 3799 | struct ext4_renament *ent, |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3800 | 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); |
| 3812 | retry: |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 3813 | wh = ext4_new_inode_start_handle(mnt_userns, ent->dir, |
| 3814 | S_IFCHR | WHITEOUT_MODE, |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3815 | &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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3833 | /* |
| 3834 | * Anybody can rename anything with this: the permission checks are left to the |
| 3835 | * higher-level routines. |
Theodore Ts'o | 0e20270 | 2013-08-16 22:06:53 -0400 | [diff] [blame] | 3836 | * |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3840 | */ |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 3841 | static 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3844 | { |
Theodore Ts'o | 5b61de7 | 2013-08-16 22:06:14 -0400 | [diff] [blame] | 3845 | handle_t *handle = NULL; |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3846 | struct ext4_renament old = { |
| 3847 | .dir = old_dir, |
| 3848 | .dentry = old_dentry, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3849 | .inode = d_inode(old_dentry), |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3850 | }; |
| 3851 | struct ext4_renament new = { |
| 3852 | .dir = new_dir, |
| 3853 | .dentry = new_dentry, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3854 | .inode = d_inode(new_dentry), |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3855 | }; |
Darrick J. Wong | d80d448 | 2014-08-27 18:40:09 -0400 | [diff] [blame] | 3856 | int force_reread; |
Theodore Ts'o | 0e20270 | 2013-08-16 22:06:53 -0400 | [diff] [blame] | 3857 | int retval; |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3858 | struct inode *whiteout = NULL; |
| 3859 | int credits; |
| 3860 | u8 old_file_type; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3861 | |
Theodore Ts'o | b50282f | 2018-08-27 01:47:09 -0400 | [diff] [blame] | 3862 | 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 Xi | 040cb37 | 2016-01-08 16:01:21 -0500 | [diff] [blame] | 3868 | 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 Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 3873 | retval = dquot_initialize(old.dir); |
| 3874 | if (retval) |
| 3875 | return retval; |
| 3876 | retval = dquot_initialize(new.dir); |
| 3877 | if (retval) |
| 3878 | return retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3879 | |
| 3880 | /* Initialize quotas before so that eventual writes go |
| 3881 | * in separate transaction */ |
Jan Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 3882 | if (new.inode) { |
| 3883 | retval = dquot_initialize(new.inode); |
| 3884 | if (retval) |
| 3885 | return retval; |
| 3886 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3887 | |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3888 | old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL); |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 3889 | if (IS_ERR(old.bh)) |
| 3890 | return PTR_ERR(old.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3891 | /* |
| 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 Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3897 | retval = -ENOENT; |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3898 | if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino) |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 3899 | goto release_bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3900 | |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3901 | new.bh = ext4_find_entry(new.dir, &new.dentry->d_name, |
| 3902 | &new.de, &new.inlined); |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 3903 | if (IS_ERR(new.bh)) { |
| 3904 | retval = PTR_ERR(new.bh); |
Theodore Ts'o | a9cfcd6 | 2014-09-03 09:33:00 -0400 | [diff] [blame] | 3905 | new.bh = NULL; |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 3906 | goto release_bh; |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 3907 | } |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3908 | if (new.bh) { |
| 3909 | if (!new.inode) { |
| 3910 | brelse(new.bh); |
| 3911 | new.bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3912 | } |
| 3913 | } |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3914 | if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC)) |
| 3915 | ext4_alloc_da_blocks(old.inode); |
Theodore Ts'o | 5b61de7 | 2013-08-16 22:06:14 -0400 | [diff] [blame] | 3916 | |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3917 | 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 Khlebnikov | 7071b71 | 2015-04-02 16:32:15 -0400 | [diff] [blame] | 3921 | if (IS_ERR(handle)) { |
| 3922 | retval = PTR_ERR(handle); |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 3923 | goto release_bh; |
Konstantin Khlebnikov | 7071b71 | 2015-04-02 16:32:15 -0400 | [diff] [blame] | 3924 | } |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3925 | } else { |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 3926 | whiteout = ext4_whiteout_for_rename(mnt_userns, &old, credits, &handle); |
Konstantin Khlebnikov | 7071b71 | 2015-04-02 16:32:15 -0400 | [diff] [blame] | 3927 | if (IS_ERR(whiteout)) { |
| 3928 | retval = PTR_ERR(whiteout); |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 3929 | goto release_bh; |
Konstantin Khlebnikov | 7071b71 | 2015-04-02 16:32:15 -0400 | [diff] [blame] | 3930 | } |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3931 | } |
Theodore Ts'o | 5b61de7 | 2013-08-16 22:06:14 -0400 | [diff] [blame] | 3932 | |
yangerkun | 6b4b8e6 | 2021-01-05 14:28:57 +0800 | [diff] [blame] | 3933 | old_file_type = old.de->file_type; |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3934 | if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir)) |
Theodore Ts'o | 5b61de7 | 2013-08-16 22:06:14 -0400 | [diff] [blame] | 3935 | ext4_handle_sync(handle); |
| 3936 | |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3937 | if (S_ISDIR(old.inode->i_mode)) { |
| 3938 | if (new.inode) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3939 | retval = -ENOTEMPTY; |
Michael Halcrow | e875a2d | 2015-04-11 07:46:49 -0400 | [diff] [blame] | 3940 | if (!ext4_empty_dir(new.inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3941 | goto end_rename; |
Miklos Szeredi | 0d7d5d6 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3942 | } else { |
| 3943 | retval = -EMLINK; |
| 3944 | if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir)) |
| 3945 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3946 | } |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3947 | retval = ext4_rename_dir_prepare(handle, &old); |
Amir Goldstein | ef60789 | 2011-03-20 21:18:44 -0400 | [diff] [blame] | 3948 | if (retval) |
| 3949 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3950 | } |
Darrick J. Wong | d80d448 | 2014-08-27 18:40:09 -0400 | [diff] [blame] | 3951 | /* |
| 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 Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3960 | |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3961 | 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 Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3970 | retval = ext4_mark_inode_dirty(handle, whiteout); |
| 3971 | if (unlikely(retval)) |
| 3972 | goto end_rename; |
Harshad Shirwadkar | 8210bb2 | 2021-03-16 15:19:21 -0700 | [diff] [blame] | 3973 | |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3974 | } |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 3975 | if (!new.bh) { |
| 3976 | retval = ext4_add_entry(handle, new.dentry, old.inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3977 | if (retval) |
| 3978 | goto end_rename; |
| 3979 | } else { |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 3980 | retval = ext4_setent(handle, &new, |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3981 | old.inode->i_ino, old_file_type); |
Amir Goldstein | ef60789 | 2011-03-20 21:18:44 -0400 | [diff] [blame] | 3982 | if (retval) |
| 3983 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3984 | } |
Darrick J. Wong | d80d448 | 2014-08-27 18:40:09 -0400 | [diff] [blame] | 3985 | if (force_reread) |
| 3986 | force_reread = !ext4_test_inode_flag(new.dir, |
| 3987 | EXT4_INODE_INLINE_DATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3988 | |
| 3989 | /* |
| 3990 | * Like most other Unix systems, set the ctime for inodes on a |
| 3991 | * rename. |
| 3992 | */ |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 3993 | old.inode->i_ctime = current_time(old.inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 3994 | retval = ext4_mark_inode_dirty(handle, old.inode); |
| 3995 | if (unlikely(retval)) |
| 3996 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3997 | |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3998 | if (!whiteout) { |
| 3999 | /* |
| 4000 | * ok, that's it |
| 4001 | */ |
| 4002 | ext4_rename_delete(handle, &old, force_reread); |
| 4003 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4004 | |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4005 | if (new.inode) { |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 4006 | ext4_dec_count(new.inode); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4007 | new.inode->i_ctime = current_time(new.inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4008 | } |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4009 | old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir); |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4010 | ext4_update_dx_flag(old.dir); |
| 4011 | if (old.dir_bh) { |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4012 | retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino); |
| 4013 | if (retval) |
Theodore Ts'o | b409714 | 2011-01-10 12:46:59 -0500 | [diff] [blame] | 4014 | goto end_rename; |
Miklos Szeredi | bd1af14 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4015 | |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 4016 | ext4_dec_count(old.dir); |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4017 | if (new.inode) { |
Michael Halcrow | e875a2d | 2015-04-11 07:46:49 -0400 | [diff] [blame] | 4018 | /* checked ext4_empty_dir above, can't have another |
| 4019 | * parent, ext4_dec_count() won't work for many-linked |
| 4020 | * dirs */ |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4021 | clear_nlink(new.inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4022 | } else { |
Nikolay Borisov | 15ed285 | 2020-08-26 16:31:16 +0300 | [diff] [blame] | 4023 | ext4_inc_count(new.dir); |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4024 | ext4_update_dx_flag(new.dir); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4025 | retval = ext4_mark_inode_dirty(handle, new.dir); |
| 4026 | if (unlikely(retval)) |
| 4027 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4028 | } |
| 4029 | } |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4030 | retval = ext4_mark_inode_dirty(handle, old.dir); |
| 4031 | if (unlikely(retval)) |
| 4032 | goto end_rename; |
Harshad Shirwadkar | aa75f4d | 2020-10-15 13:37:57 -0700 | [diff] [blame] | 4033 | |
| 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 Shirwadkar | a80f7fc | 2020-11-05 19:58:53 -0800 | [diff] [blame] | 4044 | 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 Shirwadkar | 8210bb2 | 2021-03-16 15:19:21 -0700 | [diff] [blame] | 4047 | if (whiteout) |
| 4048 | __ext4_fc_track_create(handle, whiteout, old.dentry); |
Harshad Shirwadkar | aa75f4d | 2020-10-15 13:37:57 -0700 | [diff] [blame] | 4049 | } |
| 4050 | |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4051 | if (new.inode) { |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4052 | retval = ext4_mark_inode_dirty(handle, new.inode); |
| 4053 | if (unlikely(retval)) |
| 4054 | goto end_rename; |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4055 | if (!new.inode->i_nlink) |
| 4056 | ext4_orphan_add(handle, new.inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4057 | } |
| 4058 | retval = 0; |
| 4059 | |
| 4060 | end_rename: |
yangerkun | 6b4b8e6 | 2021-01-05 14:28:57 +0800 | [diff] [blame] | 4061 | if (whiteout) { |
| 4062 | if (retval) { |
zhangyi (F) | b7ff91f | 2021-03-03 21:17:02 +0800 | [diff] [blame] | 4063 | ext4_resetent(handle, &old, |
| 4064 | old.inode->i_ino, old_file_type); |
yangerkun | 6b4b8e6 | 2021-01-05 14:28:57 +0800 | [diff] [blame] | 4065 | drop_nlink(whiteout); |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 4066 | ext4_orphan_add(handle, whiteout); |
yangerkun | 6b4b8e6 | 2021-01-05 14:28:57 +0800 | [diff] [blame] | 4067 | } |
| 4068 | unlock_new_inode(whiteout); |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 4069 | ext4_journal_stop(handle); |
yangerkun | 6b4b8e6 | 2021-01-05 14:28:57 +0800 | [diff] [blame] | 4070 | iput(whiteout); |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 4071 | } else { |
| 4072 | ext4_journal_stop(handle); |
yangerkun | 6b4b8e6 | 2021-01-05 14:28:57 +0800 | [diff] [blame] | 4073 | } |
zhangyi (F) | 5dccdc5 | 2021-03-03 21:17:03 +0800 | [diff] [blame] | 4074 | release_bh: |
Miklos Szeredi | c0d268c | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4075 | brelse(old.dir_bh); |
| 4076 | brelse(old.bh); |
| 4077 | brelse(new.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4078 | return retval; |
| 4079 | } |
| 4080 | |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4081 | static 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 Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4088 | .inode = d_inode(old_dentry), |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4089 | }; |
| 4090 | struct ext4_renament new = { |
| 4091 | .dir = new_dir, |
| 4092 | .dentry = new_dentry, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4093 | .inode = d_inode(new_dentry), |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4094 | }; |
| 4095 | u8 new_file_type; |
| 4096 | int retval; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 4097 | struct timespec64 ctime; |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4098 | |
Li Xi | 040cb37 | 2016-01-08 16:01:21 -0500 | [diff] [blame] | 4099 | 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 Kara | a7cdade | 2015-06-29 16:22:54 +0200 | [diff] [blame] | 4107 | retval = dquot_initialize(old.dir); |
| 4108 | if (retval) |
| 4109 | return retval; |
| 4110 | retval = dquot_initialize(new.dir); |
| 4111 | if (retval) |
| 4112 | return retval; |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4113 | |
| 4114 | old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, |
| 4115 | &old.de, &old.inlined); |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 4116 | if (IS_ERR(old.bh)) |
| 4117 | return PTR_ERR(old.bh); |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4118 | /* |
| 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'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 4130 | if (IS_ERR(new.bh)) { |
| 4131 | retval = PTR_ERR(new.bh); |
Theodore Ts'o | a9cfcd6 | 2014-09-03 09:33:00 -0400 | [diff] [blame] | 4132 | new.bh = NULL; |
Theodore Ts'o | 36de928 | 2014-08-23 17:47:19 -0400 | [diff] [blame] | 4133 | goto end_rename; |
| 4134 | } |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4135 | |
| 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 Khlebnikov | 7071b71 | 2015-04-02 16:32:15 -0400 | [diff] [blame] | 4143 | if (IS_ERR(handle)) { |
| 4144 | retval = PTR_ERR(handle); |
| 4145 | handle = NULL; |
| 4146 | goto end_rename; |
| 4147 | } |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4148 | |
| 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 Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4191 | ctime = current_time(old.inode); |
| 4192 | old.inode->i_ctime = ctime; |
| 4193 | new.inode->i_ctime = ctime; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4194 | 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 Shirwadkar | aa75f4d | 2020-10-15 13:37:57 -0700 | [diff] [blame] | 4200 | ext4_fc_mark_ineligible(new.inode->i_sb, |
| 4201 | EXT4_FC_REASON_CROSS_RENAME); |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4202 | 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 | |
| 4216 | end_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 Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4226 | static int ext4_rename2(struct user_namespace *mnt_userns, |
| 4227 | struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4228 | struct inode *new_dir, struct dentry *new_dentry, |
| 4229 | unsigned int flags) |
| 4230 | { |
Eric Biggers | 07543d1 | 2017-10-18 20:21:57 -0400 | [diff] [blame] | 4231 | int err; |
| 4232 | |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 4233 | if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb)))) |
| 4234 | return -EIO; |
| 4235 | |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4236 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4237 | return -EINVAL; |
| 4238 | |
Eric Biggers | 07543d1 | 2017-10-18 20:21:57 -0400 | [diff] [blame] | 4239 | err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 4240 | flags); |
| 4241 | if (err) |
| 4242 | return err; |
| 4243 | |
Miklos Szeredi | bd42998 | 2014-04-01 17:08:44 +0200 | [diff] [blame] | 4244 | if (flags & RENAME_EXCHANGE) { |
| 4245 | return ext4_cross_rename(old_dir, old_dentry, |
| 4246 | new_dir, new_dentry); |
| 4247 | } |
Miklos Szeredi | cd808de | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4248 | |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 4249 | return ext4_rename(mnt_userns, old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4250 | } |
| 4251 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4252 | /* |
| 4253 | * directories can handle most operations... |
| 4254 | */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 4255 | const struct inode_operations ext4_dir_inode_operations = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4256 | .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 Viro | af51a2a | 2013-06-29 13:23:08 +0400 | [diff] [blame] | 4264 | .tmpfile = ext4_tmpfile, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4265 | .rename = ext4_rename2, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4266 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 4267 | .getattr = ext4_getattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4268 | .listxattr = ext4_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 4269 | .get_acl = ext4_get_acl, |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 4270 | .set_acl = ext4_set_acl, |
Aneesh Kumar K.V | abc8746 | 2009-05-02 22:54:32 -0400 | [diff] [blame] | 4271 | .fiemap = ext4_fiemap, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4272 | }; |
| 4273 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 4274 | const struct inode_operations ext4_special_inode_operations = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4275 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 4276 | .getattr = ext4_getattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4277 | .listxattr = ext4_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 4278 | .get_acl = ext4_get_acl, |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 4279 | .set_acl = ext4_set_acl, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4280 | }; |