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