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