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