Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/fs/ext4/namei.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 1992, 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *  from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *  linux/fs/minix/namei.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *  Copyright (C) 1991, 1992  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *  Big-endian to little-endian byte-swapping/bitmaps by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *        David S. Miller (davem@caip.rutgers.edu), 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *  Directory entry file type support and forward compatibility hooks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *	for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *  Hash Tree Directory indexing (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *	Daniel Phillips, 2001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *  Hash Tree Directory indexing porting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *	Christopher Li, 2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *  Hash Tree Directory indexing cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *	Theodore Ts'o, 2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/unicode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <trace/events/ext4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * define how far ahead to read directories while searching them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define NAMEI_RA_CHUNKS  2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define NAMEI_RA_BLOCKS  4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define NAMEI_RA_SIZE	     (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) static struct buffer_head *ext4_append(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 					struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 					ext4_lblk_t *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		     ((inode->i_size >> 10) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		      EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		return ERR_PTR(-ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	*block = inode->i_size >> inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	inode->i_size += inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	EXT4_I(inode)->i_disksize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	BUFFER_TRACE(bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	err = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		ext4_std_error(inode->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static int ext4_dx_csum_verify(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			       struct ext4_dir_entry *dirent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * Hints to ext4_read_dirblock regarding whether we expect a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * block being read to be an index block, or a block containing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * directory entries (and if the latter, whether it was found via a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * logical block in an htree index block).  This is used to control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * what sort of sanity checkinig ext4_read_dirblock() will do on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * directory block read from the storage device.  EITHER will means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * the caller doesn't know what kind of directory block will be read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * so no specific verification will be done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	EITHER, INDEX, DIRENT, DIRENT_HTREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) } dirblock_type_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define ext4_read_dirblock(inode, block, type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	__ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 						ext4_lblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 						dirblock_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 						const char *func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 						unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct ext4_dir_entry *dirent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	int is_dx_block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		bh = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		bh = ext4_bread(NULL, inode, block, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		__ext4_warning(inode->i_sb, func, line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			       "inode #%lu: lblock %lu: comm %s: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 			       "error %ld reading directory block",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 			       inode->i_ino, (unsigned long)block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			       current->comm, PTR_ERR(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	if (!bh && (type == INDEX || type == DIRENT_HTREE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		ext4_error_inode(inode, func, line, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 				 "Directory hole found for htree %s block",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 				 (type == INDEX) ? "index" : "leaf");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	dirent = (struct ext4_dir_entry *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	/* Determine whether or not we have an index block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	if (is_dx(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		if (block == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 			is_dx_block = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		else if (ext4_rec_len_from_disk(dirent->rec_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 						inode->i_sb->s_blocksize) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 			 inode->i_sb->s_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			is_dx_block = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	if (!is_dx_block && type == INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		ext4_error_inode(inode, func, line, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		       "directory leaf block found instead of index block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	if (!ext4_has_metadata_csum(inode->i_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	    buffer_verified(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	 * An empty leaf block can get mistaken for a index block; for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	 * this reason, we can only check the index checksum when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	 * caller is sure it should be an index block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	if (is_dx_block && type == INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		if (ext4_dx_csum_verify(inode, dirent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		    !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			ext4_error_inode_err(inode, func, line, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 					     EFSBADCRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 					     "Directory index failed checksum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			return ERR_PTR(-EFSBADCRC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	if (!is_dx_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		if (ext4_dirblock_csum_verify(inode, bh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		    !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			ext4_error_inode_err(inode, func, line, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 					     EFSBADCRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 					     "Directory block failed checksum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			return ERR_PTR(-EFSBADCRC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) #ifndef assert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) #define assert(test) J_ASSERT(test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) #ifdef DX_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) #define dxtrace(command) command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) #define dxtrace(command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) struct fake_dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	__le32 inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	__le16 rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	u8 name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	u8 file_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) struct dx_countlimit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	__le16 limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	__le16 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) struct dx_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	__le32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	__le32 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * dx_root_info is laid out so that if it should somehow get overlaid by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * dirent the two low bits of the hash version will be zero.  Therefore, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  * hash version mod 4 should never be 0.  Sincerely, the paranoia department.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) struct dx_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	struct fake_dirent dot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	char dot_name[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	struct fake_dirent dotdot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	char dotdot_name[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct dx_root_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		__le32 reserved_zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		u8 hash_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		u8 info_length; /* 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		u8 indirect_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		u8 unused_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	struct dx_entry	entries[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) struct dx_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	struct fake_dirent fake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	struct dx_entry	entries[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) struct dx_frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	struct dx_entry *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	struct dx_entry *at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) struct dx_map_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	u16 offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	u16 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * This goes at the end of each htree block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) struct dx_tail {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	u32 dt_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	__le32 dt_checksum;	/* crc32c(uuid+inum+dirblock) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static inline unsigned dx_get_hash(struct dx_entry *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) static void dx_set_hash(struct dx_entry *entry, unsigned value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) static unsigned dx_get_count(struct dx_entry *entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) static unsigned dx_get_limit(struct dx_entry *entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) static void dx_set_count(struct dx_entry *entries, unsigned value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) static void dx_set_limit(struct dx_entry *entries, unsigned value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) static unsigned dx_node_limit(struct inode *dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) static struct dx_frame *dx_probe(struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 				 struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 				 struct dx_hash_info *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 				 struct dx_frame *frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static void dx_release(struct dx_frame *frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		       unsigned blocksize, struct dx_hash_info *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		       struct dx_map_entry map[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static void dx_sort_map(struct dx_map_entry *map, unsigned count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) static struct ext4_dir_entry_2 *dx_move_dirents(struct inode *dir, char *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 					char *to, struct dx_map_entry *offsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 					int count, unsigned int blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 						unsigned int blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) static void dx_insert_block(struct dx_frame *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 					u32 hash, ext4_lblk_t block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) static int ext4_htree_next_block(struct inode *dir, __u32 hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 				 struct dx_frame *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 				 struct dx_frame *frames,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 				 __u32 *start_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		struct ext4_dir_entry_2 **res_dir, ext4_lblk_t *lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			     struct inode *dir, struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) /* checksumming functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) void ext4_initialize_dirent_tail(struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 				 unsigned int blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	memset(t, 0, sizeof(struct ext4_dir_entry_tail));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	t->det_rec_len = ext4_rec_len_to_disk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			sizeof(struct ext4_dir_entry_tail), blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	t->det_reserved_ft = EXT4_FT_DIR_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) /* Walk through a dirent block to find a checksum "dirent" at the tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 						   struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct ext4_dir_entry_tail *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) #ifdef PARANOID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	struct ext4_dir_entry *d, *top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	d = (struct ext4_dir_entry *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	top = (struct ext4_dir_entry *)(bh->b_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		(EXT4_BLOCK_SIZE(inode->i_sb) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		 sizeof(struct ext4_dir_entry_tail)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	while (d < top && d->rec_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		d = (struct ext4_dir_entry *)(((void *)d) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		    le16_to_cpu(d->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	if (d != top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	t = (struct ext4_dir_entry_tail *)d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (t->det_reserved_zero1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	    le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	    t->det_reserved_zero2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	    t->det_reserved_ft != EXT4_FT_DIR_CSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	return cpu_to_le32(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) #define warn_no_space_for_csum(inode)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	__warn_no_space_for_csum((inode), __func__, __LINE__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) static void __warn_no_space_for_csum(struct inode *inode, const char *func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 				     unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	__ext4_warning_inode(inode, func, line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		"No space for directory leaf checksum. Please run e2fsck -D.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct ext4_dir_entry_tail *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	if (!ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	t = get_dirent_tail(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		warn_no_space_for_csum(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 						  (char *)t - bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) static void ext4_dirblock_csum_set(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 				 struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	struct ext4_dir_entry_tail *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (!ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	t = get_dirent_tail(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		warn_no_space_for_csum(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 					     (char *)t - bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) int ext4_handle_dirty_dirblock(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			       struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			       struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	ext4_dirblock_csum_set(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return ext4_handle_dirty_metadata(handle, inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 					       struct ext4_dir_entry *dirent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 					       int *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	struct ext4_dir_entry *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	struct dx_root_info *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	int count_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		count_offset = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	else if (le16_to_cpu(dirent->rec_len) == 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		if (le16_to_cpu(dp->rec_len) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		    EXT4_BLOCK_SIZE(inode->i_sb) - 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		root = (struct dx_root_info *)(((void *)dp + 12));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		if (root->reserved_zero ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		    root->info_length != sizeof(struct dx_root_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		count_offset = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	if (offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		*offset = count_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	return (struct dx_countlimit *)(((void *)dirent) + count_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			   int count_offset, int count, struct dx_tail *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	__u32 dummy_csum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	int offset = offsetof(struct dx_tail, dt_checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	size = count_offset + (count * sizeof(struct dx_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	return cpu_to_le32(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) static int ext4_dx_csum_verify(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			       struct ext4_dir_entry *dirent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	struct dx_countlimit *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	struct dx_tail *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	int count_offset, limit, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (!ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	c = get_dx_countlimit(inode, dirent, &count_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		EXT4_ERROR_INODE(inode, "dir seems corrupt?  Run e2fsck -D.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	limit = le16_to_cpu(c->limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	count = le16_to_cpu(c->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	if (count_offset + (limit * sizeof(struct dx_entry)) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	    EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		warn_no_space_for_csum(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 					    count, t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	struct dx_countlimit *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	struct dx_tail *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	int count_offset, limit, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (!ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	c = get_dx_countlimit(inode, dirent, &count_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		EXT4_ERROR_INODE(inode, "dir seems corrupt?  Run e2fsck -D.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	limit = le16_to_cpu(c->limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	count = le16_to_cpu(c->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (count_offset + (limit * sizeof(struct dx_entry)) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	    EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		warn_no_space_for_csum(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static inline int ext4_handle_dirty_dx_node(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 					    struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 					    struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	return ext4_handle_dirty_metadata(handle, inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527)  * p is at least 6 bytes before the end of page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) static inline struct ext4_dir_entry_2 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	return (struct ext4_dir_entry_2 *)((char *)p +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		ext4_rec_len_from_disk(p->rec_len, blocksize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537)  * Future: use high four bits of block for coalesce-on-delete flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538)  * Mask them off for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	return le32_to_cpu(entry->block) & 0x0fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	entry->block = cpu_to_le32(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) static inline unsigned dx_get_hash(struct dx_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	return le32_to_cpu(entry->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	entry->hash = cpu_to_le32(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) static inline unsigned dx_get_count(struct dx_entry *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	return le16_to_cpu(((struct dx_countlimit *) entries)->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) static inline unsigned dx_get_limit(struct dx_entry *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) static inline void dx_set_count(struct dx_entry *entries, unsigned value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	unsigned int entry_space = dir->i_sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			ext4_dir_rec_len(1, NULL) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			ext4_dir_rec_len(2, NULL) - infosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (ext4_has_metadata_csum(dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		entry_space -= sizeof(struct dx_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	return entry_space / sizeof(struct dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) static inline unsigned dx_node_limit(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	unsigned int entry_space = dir->i_sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			ext4_dir_rec_len(0, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if (ext4_has_metadata_csum(dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		entry_space -= sizeof(struct dx_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	return entry_space / sizeof(struct dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * Debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) #ifdef DX_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) static void dx_show_index(char * label, struct dx_entry *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	int i, n = dx_get_count (entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	printk(KERN_DEBUG "%s index", label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	for (i = 0; i < n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		printk(KERN_CONT " %x->%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		       i ? dx_get_hash(entries + i) : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		       (unsigned long)dx_get_block(entries + i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) struct stats
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	unsigned names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	unsigned space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	unsigned bcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) static struct stats dx_show_leaf(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 				struct dx_hash_info *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 				struct ext4_dir_entry_2 *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 				int size, int show_names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	unsigned names = 0, space = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	char *base = (char *) de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	struct dx_hash_info h = *hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	printk("names: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	while ((char *) de < base + size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		if (de->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			if (show_names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 				int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 				char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 				struct fscrypt_str fname_crypto_str =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 					FSTR_INIT(NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 				int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 				name  = de->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 				len = de->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 				if (!IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 					/* Directory is not encrypted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 					ext4fs_dirhash(dir, de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 						de->name_len, &h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 					printk("%*.s:(U)%x.%u ", len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 					       name, h.hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 					       (unsigned) ((char *) de
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 							   - base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 					struct fscrypt_str de_name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 						FSTR_INIT(name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 					/* Directory is encrypted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 					res = fscrypt_fname_alloc_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 						len, &fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 					if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 						printk(KERN_WARNING "Error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 							"allocating crypto "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 							"buffer--skipping "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 							"crypto\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 					res = fscrypt_fname_disk_to_usr(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 						0, 0, &de_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 						&fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 					if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 						printk(KERN_WARNING "Error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 							"converting filename "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 							"from disk to usr"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 							"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 						name = "??";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 						len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 					} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 						name = fname_crypto_str.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 						len = fname_crypto_str.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 					if (IS_CASEFOLDED(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 						h.hash = EXT4_DIRENT_HASH(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 					else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 						ext4fs_dirhash(dir, de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 						       de->name_len, &h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 					printk("%*.s:(E)%x.%u ", len, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 					       h.hash, (unsigned) ((char *) de
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 								   - base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 					fscrypt_fname_free_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 							&fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 				int len = de->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 				char *name = de->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				ext4fs_dirhash(dir, de->name, de->name_len, &h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 				printk("%*.s:%x.%u ", len, name, h.hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				       (unsigned) ((char *) de - base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			space += ext4_dir_rec_len(de->name_len, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			names++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		de = ext4_next_entry(de, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	printk(KERN_CONT "(%i)\n", names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	return (struct stats) { names, space, 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 			     struct dx_entry *entries, int levels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	unsigned blocksize = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	unsigned count = dx_get_count(entries), names = 0, space = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	unsigned bcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	printk("%i indexed blocks...\n", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	for (i = 0; i < count; i++, entries++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		ext4_lblk_t block = dx_get_block(entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		ext4_lblk_t hash  = i ? dx_get_hash(entries): 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		struct stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		printk("%s%3u:%03u hash %8x/%8x ",levels?"":"   ", i, block, hash, range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		bh = ext4_bread(NULL,dir, block, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		if (!bh || IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		stats = levels?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		   dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		   dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			bh->b_data, blocksize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		names += stats.names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		space += stats.space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		bcount += stats.bcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (bcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		       levels ? "" : "   ", names, space/bcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		       (space/bcount)*100/blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	return (struct stats) { names, space, bcount};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) #endif /* DX_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748)  * Probe for a directory leaf block to search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750)  * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751)  * error in the directory index, and the caller should fall back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * searching the directory normally.  The callers of dx_probe **MUST**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  * check for this error code, and make sure it never gets reflected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * back to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static struct dx_frame *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) dx_probe(struct ext4_filename *fname, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	unsigned count, indirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	struct dx_entry *at, *entries, *p, *q, *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	struct dx_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	struct dx_frame *frame = frame_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	frame->bh = ext4_read_dirblock(dir, 0, INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (IS_ERR(frame->bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		return (struct dx_frame *) frame->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	root = (struct dx_root *) frame->bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (root->info.hash_version != DX_HASH_TEA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	    root->info.hash_version != DX_HASH_HALF_MD4 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	    root->info.hash_version != DX_HASH_LEGACY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	    root->info.hash_version != DX_HASH_SIPHASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		ext4_warning_inode(dir, "Unrecognised inode hash code %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 				   root->info.hash_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (ext4_hash_in_dirent(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		if (root->info.hash_version != DX_HASH_SIPHASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			ext4_warning_inode(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 				"Hash in dirent, but hash is not SIPHASH");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		if (root->info.hash_version == DX_HASH_SIPHASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			ext4_warning_inode(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				"Hash code is SIPHASH, but hash not in dirent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		hinfo = &fname->hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	hinfo->hash_version = root->info.hash_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (hinfo->hash_version <= DX_HASH_TEA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	/* hash is already computed for encrypted casefolded directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (fname && fname_name(fname) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 				!(IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	hash = hinfo->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (root->info.unused_flags & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 				   root->info.unused_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	indirect = root->info.indirect_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		ext4_warning(dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			     "Directory (ino: %lu) htree depth %#06x exceed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			     "supported value", dir->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			     ext4_dir_htree_level(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			ext4_warning(dir->i_sb, "Enable large directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 						"feature to access it");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	entries = (struct dx_entry *)(((char *)&root->info) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 				      root->info.info_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (dx_get_limit(entries) != dx_root_limit(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 						   root->info.info_length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 				   dx_get_limit(entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 				   dx_root_limit(dir, root->info.info_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	dxtrace(printk("Look up %x", hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		count = dx_get_count(entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		if (!count || count > dx_get_limit(entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			ext4_warning_inode(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 					   "dx entry: count %u beyond limit %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 					   count, dx_get_limit(entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		p = entries + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		q = entries + count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		while (p <= q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			m = p + (q - p) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			dxtrace(printk(KERN_CONT "."));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			if (dx_get_hash(m) > hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 				q = m - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 				p = m + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (0) { // linear search cross check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			unsigned n = count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			at = entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			while (n--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 				dxtrace(printk(KERN_CONT ","));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 				if (dx_get_hash(++at) > hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 				{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 					at--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			assert (at == p - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		at = p - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		dxtrace(printk(KERN_CONT " %x->%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			       at == entries ? 0 : dx_get_hash(at),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			       dx_get_block(at)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		frame->entries = entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		frame->at = at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		if (!indirect--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			return frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		if (IS_ERR(frame->bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			ret_err = (struct dx_frame *) frame->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			frame->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		entries = ((struct dx_node *) frame->bh->b_data)->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		if (dx_get_limit(entries) != dx_node_limit(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			ext4_warning_inode(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 				"dx entry: limit %u != node limit %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				dx_get_limit(entries), dx_node_limit(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	while (frame >= frame_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		brelse(frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		frame--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		ext4_warning_inode(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			"Corrupt directory, running e2fsck is recommended");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	return ret_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) static void dx_release(struct dx_frame *frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct dx_root_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	unsigned int indirect_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (frames[0].bh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	info = &((struct dx_root *)frames[0].bh->b_data)->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	/* save local copy, "info" may be freed after brelse() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	indirect_levels = info->indirect_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	for (i = 0; i <= indirect_levels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		if (frames[i].bh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		brelse(frames[i].bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		frames[i].bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * This function increments the frame pointer to search the next leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  * block, and reads in the necessary intervening nodes if the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * should be necessary.  Whether or not the search is necessary is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  * controlled by the hash parameter.  If the hash value is even, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * the search is only continued if the next block starts with that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * hash value.  This is used if we are searching for a specific file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  * This function returns 1 if the caller should continue to search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  * or 0 if it should not.  If there is an error reading one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  * index blocks, it will a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942)  * If start_hash is non-null, it will be filled in with the starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943)  * hash of the next page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static int ext4_htree_next_block(struct inode *dir, __u32 hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 				 struct dx_frame *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				 struct dx_frame *frames,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 				 __u32 *start_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	struct dx_frame *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	int num_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	__u32 bhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	p = frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	 * Find the next leaf page by incrementing the frame pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	 * If we run out of entries in the interior node, loop around and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	 * increment pointer in the parent node.  When we break out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	 * this loop, num_frames indicates the number of interior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	 * nodes need to be read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		if (++(p->at) < p->entries + dx_get_count(p->entries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		if (p == frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		num_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		p--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	 * If the hash is 1, then continue only if the next page has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	 * continuation hash of any value.  This is used for readdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	 * handling.  Otherwise, check to see if the hash matches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	 * desired contiuation hash.  If it doesn't, return since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	 * there's no point to read in the successive index pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	bhash = dx_get_hash(p->at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (start_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		*start_hash = bhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if ((hash & 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		if ((bhash & ~1) != hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * If the hash is HASH_NB_ALWAYS, we always go to the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * block so no check is necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	while (num_frames--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		brelse(p->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		p->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  * This function fills a red-black tree with information from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)  * directory block.  It returns the number directory entries loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * into the tree.  If there is an error it is returned in err.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) static int htree_dirblock_to_tree(struct file *dir_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 				  struct inode *dir, ext4_lblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 				  struct dx_hash_info *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 				  __u32 start_hash, __u32 start_minor_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	struct ext4_dir_entry_2 *de, *top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	int err = 0, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	int csum = ext4_has_metadata_csum(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 							(unsigned long)block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	de = (struct ext4_dir_entry_2 *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	/* csum entries are not larger in the casefolded encrypted case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	top = (struct ext4_dir_entry_2 *) ((char *) de +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 					   dir->i_sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 					   ext4_dir_rec_len(0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 							   csum ? NULL : dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	/* Check if the directory is encrypted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		err = fscrypt_prepare_readdir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 						 &fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		if (ext4_check_dir_entry(dir, NULL, de, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 				bh->b_data, bh->b_size, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 				(block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 					 + ((char *)de - bh->b_data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			/* silently ignore the rest of the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		if (ext4_hash_in_dirent(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			if (de->name_len && de->inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				hinfo->hash = EXT4_DIRENT_HASH(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 				hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 				hinfo->hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 				hinfo->minor_hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		if ((hinfo->hash < start_hash) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		    ((hinfo->hash == start_hash) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		     (hinfo->minor_hash < start_minor_hash)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		if (de->inode == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		if (!IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			tmp_str.name = de->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			tmp_str.len = de->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			err = ext4_htree_store_dirent(dir_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 				   hinfo->hash, hinfo->minor_hash, de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 				   &tmp_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			int save_len = fname_crypto_str.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			struct fscrypt_str de_name = FSTR_INIT(de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 								de->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			/* Directory is encrypted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 					hinfo->minor_hash, &de_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 					&fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 				count = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			err = ext4_htree_store_dirent(dir_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				   hinfo->hash, hinfo->minor_hash, de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 					&fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			fname_crypto_str.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		if (err != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			count = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	fscrypt_fname_free_buffer(&fname_crypto_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  * This function fills a red-black tree with information from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  * directory.  We start scanning the directory in hash order, starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  * at start_hash and start_minor_hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)  * This function returns the number of entries inserted into the tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)  * or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			 __u32 start_minor_hash, __u32 *next_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	struct dx_hash_info hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	ext4_lblk_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	int ret, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	__u32 hashval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	struct fscrypt_str tmp_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		       start_hash, start_minor_hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	dir = file_inode(dir_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		if (ext4_hash_in_dirent(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			hinfo.hash_version = DX_HASH_SIPHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			hinfo.hash_version =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 					EXT4_SB(dir->i_sb)->s_def_hash_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		if (hinfo.hash_version <= DX_HASH_TEA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			hinfo.hash_version +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 				EXT4_SB(dir->i_sb)->s_hash_unsigned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (ext4_has_inline_data(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			int has_inline_data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			count = ext4_inlinedir_to_tree(dir_file, dir, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 						       &hinfo, start_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 						       start_minor_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 						       &has_inline_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			if (has_inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 				*next_hash = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 				return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 					       start_hash, start_minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		*next_hash = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	hinfo.hash = start_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	hinfo.minor_hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	frame = dx_probe(NULL, dir, &hinfo, frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (IS_ERR(frame))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		return PTR_ERR(frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	/* Add '.' and '..' from the htree header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (!start_hash && !start_minor_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		tmp_str.name = de->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		tmp_str.len = de->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		err = ext4_htree_store_dirent(dir_file, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 					      de, &tmp_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		de = ext4_next_entry(de, dir->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		tmp_str.name = de->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		tmp_str.len = de->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		err = ext4_htree_store_dirent(dir_file, 2, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 					      de, &tmp_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		if (fatal_signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			err = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		block = dx_get_block(frame->at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 					     start_hash, start_minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		count += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		hashval = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 					    frame, frames, &hashval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		*next_hash = hashval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		 * Stop if:  (a) there are no more entries, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		 * (b) we have inserted at least one entry and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		 * next hash value is not a continuation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		if ((ret == 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		    (count && ((hashval & 1) == 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	dx_release(frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		       "next hash: %x\n", count, *next_hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	dx_release(frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	return (err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) static inline int search_dirblock(struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 				  struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 				  struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 				  ext4_lblk_t lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				  unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				  struct ext4_dir_entry_2 **res_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			       fname, lblk, offset, res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  * Directory block splitting, compacting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)  * Create map of hash values, offsets, and sizes, stored at end of block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  * Returns number of entries mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		       unsigned blocksize, struct dx_hash_info *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		       struct dx_map_entry *map_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	char *base = (char *) de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	struct dx_hash_info h = *hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	while ((char *) de < base + blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		if (de->name_len && de->inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			if (ext4_hash_in_dirent(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				h.hash = EXT4_DIRENT_HASH(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 				ext4fs_dirhash(dir, de->name, de->name_len, &h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			map_tail--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 			map_tail->hash = h.hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			map_tail->offs = ((char *) de - base)>>2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			map_tail->size = le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		/* XXX: do we need to check rec_len == 0 case? -Chris */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		de = ext4_next_entry(de, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) /* Sort map by hash value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) static void dx_sort_map (struct dx_map_entry *map, unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	struct dx_map_entry *p, *q, *top = map + count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	int more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	/* Combsort until bubble sort doesn't suck */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	while (count > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		count = count*10/13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		if (count - 9 < 2) /* 9, 10 -> 11 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 			count = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		for (p = top, q = p - count; q >= map; p--, q--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			if (p->hash < q->hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 				swap(*p, *q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	/* Garden variety bubble sort */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		more = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		q = top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		while (q-- > map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			if (q[1].hash >= q[0].hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 			swap(*(q+1), *q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 			more = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	} while(more);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	struct dx_entry *entries = frame->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	struct dx_entry *old = frame->at, *new = old + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	int count = dx_get_count(entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	assert(count < dx_get_limit(entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	assert(old < entries + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	dx_set_hash(new, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	dx_set_block(new, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	dx_set_count(entries, count + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)  * Test whether a case-insensitive directory entry matches the filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)  * being searched for.  If quick is set, assume the name being looked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)  * is already in the casefolded form.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)  * Returns: 0 if the directory entry matches, more than 0 if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)  * doesn't match or less than zero on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			   u8 *de_name, size_t de_name_len, bool quick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	const struct super_block *sb = parent->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	const struct unicode_map *um = sb->s_encoding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	struct qstr entry = QSTR_INIT(de_name, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	if (IS_ENCRYPTED(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		const struct fscrypt_str encrypted_name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 				FSTR_INIT(de_name, de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		if (!decrypted_name.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		ret = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 						&decrypted_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		entry.name = decrypted_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		entry.len = decrypted_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	if (quick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		ret = utf8_strncasecmp_folded(um, name, &entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		ret = utf8_strncasecmp(um, name, &entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		/* Handle invalid character sequence as either an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		 * or as an opaque byte sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		if (sb_has_strict_encoding(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		else if (name->len != entry.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 			ret = !!memcmp(name->name, entry.name, entry.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	kfree(decrypted_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 				  struct ext4_filename *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	struct fscrypt_str *cf_name = &name->cf_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	struct dx_hash_info *hinfo = &name->hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		cf_name->name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (!cf_name->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	len = utf8_casefold(dir->i_sb->s_encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			    iname, cf_name->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			    EXT4_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (len <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		kfree(cf_name->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		cf_name->name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	cf_name->len = (unsigned) len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	if (!IS_ENCRYPTED(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	hinfo->hash_version = DX_HASH_SIPHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	hinfo->seed = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	if (cf_name->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		ext4fs_dirhash(dir, cf_name->name, cf_name->len, hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		ext4fs_dirhash(dir, iname->name, iname->len, hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  * Test whether a directory entry matches the filename being searched for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  * Return: %true if the directory entry matches, otherwise %false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) static bool ext4_match(struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			      const struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 			      struct ext4_dir_entry_2 *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct fscrypt_name f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (!de->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	f.usr_fname = fname->usr_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	f.disk_name = fname->disk_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	f.crypto_buf = fname->crypto_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	    (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		if (fname->cf_name.name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			struct qstr cf = {.name = fname->cf_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 					  .len = fname->cf_name.len};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			if (IS_ENCRYPTED(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 				if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 					fname->hinfo.minor_hash !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 						EXT4_DIRENT_MINOR_HASH(de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			return !ext4_ci_compare(parent, &cf, de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 							de->name_len, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		return !ext4_ci_compare(parent, fname->usr_fname, de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 						de->name_len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	return fscrypt_match_name(&f, de->name, de->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)  * Returns 0 if not found, -1 on failure, and 1 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		    struct inode *dir, struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		    ext4_lblk_t lblk, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		    struct ext4_dir_entry_2 **res_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	struct ext4_dir_entry_2 * de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	char * dlimit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	int de_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	de = (struct ext4_dir_entry_2 *)search_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	dlimit = search_buf + buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	while ((char *) de < dlimit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		/* this code is executed quadratically often */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		/* do minimal checking `by hand' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		if ((char *) de + de->name_len <= dlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		    ext4_match(dir, fname, de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 			/* found a match - just to be sure, do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 			 * a full check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 			if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 						 buf_size, lblk, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 			*res_dir = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		/* prevent looping on a bad block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		de_len = ext4_rec_len_from_disk(de->rec_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 						dir->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		if (de_len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		offset += de_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			       struct ext4_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	if (!is_dx(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (block == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	if (de->inode == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	    ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			sb->s_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  *	__ext4_find_entry()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  * finds an entry in the specified directory with the wanted name. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  * returns the cache buffer in which the entry was found, and the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  * itself (as a parameter - res_dir). It does NOT read the inode of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)  * entry - you'll have to do that yourself if you want to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)  * The returned buffer_head has ->b_count elevated.  The caller is expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513)  * to brelse() it when appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) static struct buffer_head *__ext4_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 					     struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 					     struct ext4_dir_entry_2 **res_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 					     int *inlined, ext4_lblk_t *lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	struct buffer_head *bh_use[NAMEI_RA_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	struct buffer_head *bh, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	ext4_lblk_t start, block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	const u8 *name = fname->usr_fname->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	size_t ra_max = 0;	/* Number of bh's in the readahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 				   buffer, bh_use[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	size_t ra_ptr = 0;	/* Current index into readahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 				   buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	ext4_lblk_t  nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	int i, namelen, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	*res_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	namelen = fname->usr_fname->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	if (namelen > EXT4_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	if (ext4_has_inline_data(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		int has_inline_data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		ret = ext4_find_inline_entry(dir, fname, res_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 					     &has_inline_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		if (lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 			*lblk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		if (has_inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			if (inlined)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 				*inlined = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	if ((namelen <= 2) && (name[0] == '.') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	    (name[1] == '.' || name[1] == '\0')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		 * "." or ".." will only be in the first block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		 * NFS may look up ".."; "." should be handled by the VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		block = start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		nblocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (is_dx(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		ret = ext4_dx_find_entry(dir, fname, res_dir, lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		 * On success, or if the error was file not found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		 * return.  Otherwise, fall back to doing a search the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		 * old fashioned way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			       "falling back\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	if (!nblocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	start = EXT4_I(dir)->i_dir_start_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	if (start >= nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	block = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		 * We deal with the read-ahead logic here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		if (ra_ptr >= ra_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			/* Refill the readahead buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			ra_ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 			if (block < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 				ra_max = start - block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 				ra_max = nblocks - block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			ra_max = min(ra_max, ARRAY_SIZE(bh_use));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			retval = ext4_bread_batch(dir, block, ra_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 						  false /* wait */, bh_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 			if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 				ret = ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 				ra_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 				goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		if ((bh = bh_use[ra_ptr++]) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			EXT4_ERROR_INODE_ERR(dir, EIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 					     "reading directory lblock %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 					     (unsigned long) block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 			ret = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 			goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		if (!buffer_verified(bh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		    !is_dx_internal_node(dir, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 					 (struct ext4_dir_entry *)bh->b_data) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		    !ext4_dirblock_csum_verify(dir, bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 					     "checksumming directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 					     "block %lu", (unsigned long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			ret = ERR_PTR(-EFSBADCRC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 			goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		i = search_dirblock(bh, dir, fname, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 			    block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		if (i == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 			if (lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 				*lblk = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 			EXT4_I(dir)->i_dir_start_lookup = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			ret = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 				goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		if (++block >= nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	} while (block != start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	 * If the directory has grown while we were searching, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	 * search the last part of the directory before giving up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	block = nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	if (block < nblocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) cleanup_and_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	/* Clean up the read-ahead blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	for (; ra_ptr < ra_max; ra_ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		brelse(bh_use[ra_ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) static struct buffer_head *ext4_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 					   const struct qstr *d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 					   struct ext4_dir_entry_2 **res_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 					   int *inlined, ext4_lblk_t *lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	struct ext4_filename fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	bh = __ext4_find_entry(dir, &fname, res_dir, inlined, lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	ext4_fname_free_filename(&fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) static struct buffer_head *ext4_lookup_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 					     struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 					     struct ext4_dir_entry_2 **res_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	struct ext4_filename fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	err = ext4_fname_prepare_lookup(dir, dentry, &fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	generic_set_encrypted_ci_d_ops(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	bh = __ext4_find_entry(dir, &fname, res_dir, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	ext4_fname_free_filename(&fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 			struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 			struct ext4_dir_entry_2 **res_dir, ext4_lblk_t *lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	struct super_block * sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	ext4_lblk_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	*res_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	frame = dx_probe(fname, dir, NULL, frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	if (IS_ERR(frame))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		return (struct buffer_head *) frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		block = dx_get_block(frame->at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		if (lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 			*lblk = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		retval = search_dirblock(bh, dir, fname, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 					 block << EXT4_BLOCK_SIZE_BITS(sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 					 res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		if (retval == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			goto success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		if (retval == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			bh = ERR_PTR(ERR_BAD_DX_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		/* Check to see if we should continue to search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 					       frames, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 			ext4_warning_inode(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 				"error %d reading directory index block",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 				retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			bh = ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	} while (retval == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	dx_release(frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	if (dentry->d_name.len > EXT4_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	bh = ext4_lookup_entry(dir, dentry, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		return ERR_CAST(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	if (bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		__u32 ino = le32_to_cpu(de->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		if (!ext4_valid_inum(dir->i_sb, ino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 			EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 			return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		if (unlikely(ino == dir->i_ino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 			EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 					 dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 			return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		if (inode == ERR_PTR(-ESTALE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 			EXT4_ERROR_INODE(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 					 "deleted inode referenced: %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 					 ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		    !fscrypt_has_permitted_context(dir, inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 			ext4_warning(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 				     "Inconsistent encryption contexts: %lu/%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 				     dir->i_ino, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			return ERR_PTR(-EPERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	if (!inode && IS_CASEFOLDED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		/* Eventually we want to call d_add_ci(dentry, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		 * for negative dentries in the encoding case as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		 * well.  For now, prevent the negative dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		 * from being cached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) struct dentry *ext4_get_parent(struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	__u32 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	static const struct qstr dotdot = QSTR_INIT("..", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	struct ext4_dir_entry_2 * de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		return ERR_CAST(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	ino = le32_to_cpu(de->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	if (!ext4_valid_inum(child->d_sb, ino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		EXT4_ERROR_INODE(d_inode(child),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 				 "bad parent inode number: %u", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)  * Move count entries from end of map between two memory locations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)  * Returns pointer to last entry moved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) static struct ext4_dir_entry_2 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) dx_move_dirents(struct inode *dir, char *from, char *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		struct dx_map_entry *map, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		unsigned blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	unsigned rec_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 						(from + (map->offs<<2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		rec_len = ext4_dir_rec_len(de->name_len, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		memcpy (to, de, rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		((struct ext4_dir_entry_2 *) to)->rec_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 				ext4_rec_len_to_disk(rec_len, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		de->inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		map++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		to += rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	return (struct ext4_dir_entry_2 *) (to - rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)  * Compact each dir entry in the range to the minimal rec_len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)  * Returns pointer to last entry in range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 							unsigned int blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	unsigned rec_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	prev = to = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	while ((char*)de < base + blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		next = ext4_next_entry(de, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 		if (de->inode && de->name_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			rec_len = ext4_dir_rec_len(de->name_len, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 			if (de > to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 				memmove(to, de, rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 			to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			prev = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 			to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		de = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	return prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  * Split a full leaf block to make room for a new dir entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)  * Allocate a new block, and move entries so that they are approx. equally full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)  * Returns pointer to de in block into which the new entry will be inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 			struct buffer_head **bh, struct dx_frame *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			struct dx_hash_info *hinfo, ext4_lblk_t *newblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	unsigned blocksize = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	unsigned count, continued;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	struct buffer_head *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	u32 hash2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	struct dx_map_entry *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	char *data1 = (*bh)->b_data, *data2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	unsigned split, move, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	struct ext4_dir_entry_2 *de = NULL, *de2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	int	csum_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	int	err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	if (ext4_has_metadata_csum(dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		csum_size = sizeof(struct ext4_dir_entry_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	bh2 = ext4_append(handle, dir, newblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	if (IS_ERR(bh2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		brelse(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 		*bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		return (struct ext4_dir_entry_2 *) bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	BUFFER_TRACE(*bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	err = ext4_journal_get_write_access(handle, *bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	BUFFER_TRACE(frame->bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	err = ext4_journal_get_write_access(handle, frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 		goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	data2 = bh2->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	/* create map in the end of data2 block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	map = (struct dx_map_entry *) (data2 + blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 			     blocksize, hinfo, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	map -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	dx_sort_map(map, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	/* Ensure that neither split block is over half full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	move = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	for (i = count-1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		/* is more than half of this entry in 2nd half of the block? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		if (size + map[i].size/2 > blocksize/2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		size += map[i].size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		move++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	 * map index at which we will split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	 * If the sum of active entries didn't exceed half the block size, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	 * split it in half by count; each resulting block will have at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	 * half the space free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	if (i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		split = count - move;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		split = count/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	hash2 = map[split].hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	continued = hash2 == map[split - 1].hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 			(unsigned long)dx_get_block(frame->at),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 					hash2, split, count-split));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	/* Fancy dance to stay within two buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 			      blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	de = dx_pack_dirents(dir, data1, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 					   (char *) de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 					   blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 					    (char *) de2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 					    blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	if (csum_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		ext4_initialize_dirent_tail(*bh, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		ext4_initialize_dirent_tail(bh2, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 			blocksize, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 			blocksize, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	/* Which block gets the new entry? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	if (hinfo->hash >= hash2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		swap(*bh, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		de = de2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	dx_insert_block(frame, hash2 + continued, *newblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	err = ext4_handle_dirty_dirblock(handle, dir, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	dxtrace(dx_show_index("frame", frame->entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	return de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) journal_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	brelse(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	*bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	ext4_std_error(dir->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) int ext4_find_dest_de(struct inode *dir, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		      ext4_lblk_t lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		      struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		      void *buf, int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		      struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		      struct ext4_dir_entry_2 **dest_de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	int nlen, rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	unsigned int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	char *top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	de = (struct ext4_dir_entry_2 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	top = buf + buf_size - reclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	while ((char *) de <= top) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		if (ext4_check_dir_entry(dir, NULL, de, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 					 buf, buf_size, lblk, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		if (ext4_match(dir, fname, de))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		nlen = ext4_dir_rec_len(de->name_len, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		if ((de->inode ? rlen - nlen : rlen) >= reclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		offset += rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	if ((char *) de > top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	*dest_de = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) void ext4_insert_dentry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 			struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 			struct ext4_dir_entry_2 *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			struct ext4_filename *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	int nlen, rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	nlen = ext4_dir_rec_len(de->name_len, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	if (de->inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		struct ext4_dir_entry_2 *de1 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 			(struct ext4_dir_entry_2 *)((char *)de + nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 		de = de1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	de->file_type = EXT4_FT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	de->inode = cpu_to_le32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	ext4_set_de_type(inode->i_sb, de, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	de->name_len = fname_len(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	memcpy(de->name, fname_name(fname), fname_len(fname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	if (ext4_hash_in_dirent(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		struct dx_hash_info *hinfo = &fname->hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		EXT4_DIRENT_HASHES(de)->minor_hash =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 						cpu_to_le32(hinfo->minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)  * Add a new entry into a directory (leaf) block.  If de is non-NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)  * it points to a directory entry which is guaranteed to be large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)  * enough for new directory entry.  If de is NULL, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)  * add_dirent_to_buf will attempt search the directory block for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)  * space.  It will return -ENOSPC if no space is available, and -EIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  * and -EEXIST if directory entry already exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			     struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			     struct inode *inode, struct ext4_dir_entry_2 *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			     ext4_lblk_t blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 			     struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	unsigned int	blocksize = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	int		csum_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	int		err, err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	if (ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 		csum_size = sizeof(struct ext4_dir_entry_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	if (!de) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		err = ext4_find_dest_de(dir, inode, blk, bh, bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 					blocksize - csum_size, fname, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	BUFFER_TRACE(bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	err = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		ext4_std_error(dir->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	/* By now the buffer is marked for journaling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	ext4_insert_dentry(dir, inode, de, blocksize, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	 * XXX shouldn't update any times until successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	 * completion of syscall, but too many callers depend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	 * on this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	 * XXX similarly, too many callers depend on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	 * ext4_new_inode() setting the times, but error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	 * recovery deletes the inode, so the worst that can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	 * happen is that the times are slightly out of date
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	 * and/or different from the directory change time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	ext4_update_dx_flag(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	err2 = ext4_mark_inode_dirty(handle, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	err = ext4_handle_dirty_dirblock(handle, dir, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		ext4_std_error(dir->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	return err ? err : err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)  * This converts a one block unindexed directory to a 3 block indexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)  * directory, and adds the dentry to the indexed directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 			    struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 			    struct inode *inode, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	struct buffer_head *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	struct dx_root	*root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	struct dx_frame	frames[EXT4_HTREE_LEVEL], *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	struct dx_entry *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	struct ext4_dir_entry_2	*de, *de2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	char		*data2, *top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	unsigned	len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	int		retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	unsigned	blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	ext4_lblk_t  block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	struct fake_dirent *fde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	int csum_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	if (ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		csum_size = sizeof(struct ext4_dir_entry_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	blocksize =  dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	BUFFER_TRACE(bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	retval = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		ext4_std_error(dir->i_sb, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	root = (struct dx_root *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	/* The 0th block becomes the root, move the dirents out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	fde = &root->dotdot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	de = (struct ext4_dir_entry_2 *)((char *)fde +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 		ext4_rec_len_from_disk(fde->rec_len, blocksize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	if ((char *) de >= (((char *) root) + blocksize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 		EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	len = ((char *) root) + (blocksize - csum_size) - (char *) de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	/* Allocate new block for the 0th block's dirents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	bh2 = ext4_append(handle, dir, &block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	if (IS_ERR(bh2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 		return PTR_ERR(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	data2 = bh2->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	memcpy(data2, de, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	de = (struct ext4_dir_entry_2 *) data2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	top = data2 + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		de = de2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 					   (char *) de, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	if (csum_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		ext4_initialize_dirent_tail(bh2, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	/* Initialize the root; the dot dirents already exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	de = (struct ext4_dir_entry_2 *) (&root->dotdot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	de->rec_len = ext4_rec_len_to_disk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 			blocksize - ext4_dir_rec_len(2, NULL), blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	memset (&root->info, 0, sizeof(root->info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	root->info.info_length = sizeof(root->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	if (ext4_hash_in_dirent(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 		root->info.hash_version = DX_HASH_SIPHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		root->info.hash_version =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 				EXT4_SB(dir->i_sb)->s_def_hash_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	entries = root->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	dx_set_block(entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	dx_set_count(entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	/* Initialize as for dx_probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	fname->hinfo.hash_version = root->info.hash_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	if (fname->hinfo.hash_version <= DX_HASH_TEA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 		fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	/* casefolded encrypted hashes are computed on fname setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	if (!ext4_hash_in_dirent(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		ext4fs_dirhash(dir, fname_name(fname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 				fname_len(fname), &fname->hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	memset(frames, 0, sizeof(frames));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	frame = frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	frame->entries = entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	frame->at = entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	frame->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		goto out_frames;	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 		goto out_frames;	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	de = do_split(handle, dir, &bh2, frame, &fname->hinfo, &block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	if (IS_ERR(de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		retval = PTR_ERR(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 		goto out_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	retval = add_dirent_to_buf(handle, fname, dir, inode, de, block, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) out_frames:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	 * Even if the block split failed, we have to properly write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	 * out all the changes we did so far. Otherwise we can end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	 * with corrupted filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		ext4_mark_inode_dirty(handle, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	dx_release(frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268)  *	ext4_add_entry()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270)  * adds a file entry to the specified directory, using the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271)  * semantics as ext4_find_entry(). It returns NULL if it failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273)  * NOTE!! The inode part of 'de' is left at 0 - which means you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274)  * may not sleep between calling this and putting something into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275)  * the entry, as someone else might have used it while you slept.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			  struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	struct inode *dir = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	struct ext4_filename fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	int	retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	int	dx_fallback=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	unsigned blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	ext4_lblk_t block, blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	int	csum_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	if (ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		csum_size = sizeof(struct ext4_dir_entry_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	blocksize = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	if (!dentry->d_name.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	if (fscrypt_is_nokey_name(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 		return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	    sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	if (ext4_has_inline_data(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 		retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 		if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 		if (retval == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	if (is_dx(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 		retval = ext4_dx_add_entry(handle, &fname, dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		if (!retval || (retval != ERR_BAD_DX_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		/* Can we just ignore htree data? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		if (ext4_has_metadata_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 			EXT4_ERROR_INODE(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 				"Directory has corrupted htree index.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 			retval = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 		ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 		dx_fallback++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 		retval = ext4_mark_inode_dirty(handle, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 		if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	blocks = dir->i_size >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	for (block = 0; block < blocks; block++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 		bh = ext4_read_dirblock(dir, block, DIRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 		if (bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 			bh = ext4_bread(handle, dir, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 					EXT4_GET_BLOCKS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 			goto add_to_new_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 			retval = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 			bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		retval = add_dirent_to_buf(handle, &fname, dir, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 					   NULL, block, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		if (retval != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		if (blocks == 1 && !dx_fallback &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		    ext4_has_feature_dir_index(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 			retval = make_indexed_dir(handle, &fname, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 						  inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 			bh = NULL; /* make_indexed_dir releases bh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	bh = ext4_append(handle, dir, &block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) add_to_new_block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 		retval = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 		bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	de = (struct ext4_dir_entry_2 *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	de->inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	if (csum_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		ext4_initialize_dirent_tail(bh, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	retval = add_dirent_to_buf(handle, &fname, dir, inode, de, block, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	ext4_fname_free_filename(&fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	if (retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390)  * Returns 0 for success, or a negative error value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 			     struct inode *dir, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	struct dx_entry *entries, *at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	int restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	restart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	frame = dx_probe(fname, dir, NULL, frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	if (IS_ERR(frame))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 		return PTR_ERR(frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	entries = frame->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	at = frame->at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	lblk = dx_get_block(frame->at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	bh = ext4_read_dirblock(dir, lblk, DIRENT_HTREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		err = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	BUFFER_TRACE(bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	err = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 		goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	err = add_dirent_to_buf(handle, fname, dir, inode, NULL, lblk, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	if (err != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	/* Block full, should compress but for now just split */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		       dx_get_count(entries), dx_get_limit(entries)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	/* Need to split index? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	if (dx_get_count(entries) == dx_get_limit(entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		ext4_lblk_t newblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		int levels = frame - frames + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		unsigned int icount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		int add_level = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 		struct dx_entry *entries2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		struct dx_node *node2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 		struct buffer_head *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 		while (frame > frames) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 			if (dx_get_count((frame - 1)->entries) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 			    dx_get_limit((frame - 1)->entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 				add_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 			frame--; /* split higher index block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 			at = frame->at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 			entries = frame->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 			restart = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 		if (add_level && levels == ext4_dir_htree_level(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 			ext4_warning(sb, "Directory (ino: %lu) index full, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 					 "reach max htree level :%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 					 dir->i_ino, levels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 			if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 				ext4_warning(sb, "Large directory feature is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 						 "not enabled on this "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 						 "filesystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 			err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		icount = dx_get_count(entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		bh2 = ext4_append(handle, dir, &newblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 		if (IS_ERR(bh2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 			err = PTR_ERR(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 		node2 = (struct dx_node *)(bh2->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		entries2 = node2->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 		memset(&node2->fake, 0, sizeof(struct fake_dirent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 							   sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		BUFFER_TRACE(frame->bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		err = ext4_journal_get_write_access(handle, frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 			goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 		if (!add_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 			unsigned icount1 = icount/2, icount2 = icount - icount1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 			unsigned hash2 = dx_get_hash(entries + icount1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 			dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 				       icount1, icount2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 			BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 			err = ext4_journal_get_write_access(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 							     (frame - 1)->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 				goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 			memcpy((char *) entries2, (char *) (entries + icount1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 			       icount2 * sizeof(struct dx_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 			dx_set_count(entries, icount1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 			dx_set_count(entries2, icount2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 			dx_set_limit(entries2, dx_node_limit(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 			/* Which index block gets the new entry? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 			if (at - entries >= icount1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 				frame->at = at = at - entries - icount1 + entries2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 				frame->entries = entries = entries2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 				swap(frame->bh, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 			dx_insert_block((frame - 1), hash2, newblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			dxtrace(dx_show_index("node", frame->entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 			dxtrace(dx_show_index("node",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 			       ((struct dx_node *) bh2->b_data)->entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 			err = ext4_handle_dirty_dx_node(handle, dir, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 				goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 			brelse (bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 			err = ext4_handle_dirty_dx_node(handle, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 						   (frame - 1)->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 				goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 			err = ext4_handle_dirty_dx_node(handle, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 							frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 			if (restart || err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 				goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 			struct dx_root *dxroot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 			memcpy((char *) entries2, (char *) entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 			       icount * sizeof(struct dx_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 			dx_set_limit(entries2, dx_node_limit(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 			/* Set up root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 			dx_set_count(entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 			dx_set_block(entries + 0, newblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 			dxroot = (struct dx_root *)frames[0].bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 			dxroot->info.indirect_levels += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 			dxtrace(printk(KERN_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 				       "Creating %d level index...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 				       dxroot->info.indirect_levels));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 			err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 				goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 			err = ext4_handle_dirty_dx_node(handle, dir, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 			brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 			restart = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 			goto journal_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	de = do_split(handle, dir, &bh, frame, &fname->hinfo, &lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	if (IS_ERR(de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 		err = PTR_ERR(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	err = add_dirent_to_buf(handle, fname, dir, inode, de, lblk, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) journal_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	dx_release(frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	/* @restart is true means htree-path has been changed, we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	 * repeat dx_probe() to find out valid htree-path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	if (restart && err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565)  * ext4_generic_delete_entry deletes a directory entry by merging it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566)  * with the previous entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) int ext4_generic_delete_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 			      struct ext4_dir_entry_2 *de_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 			      ext4_lblk_t lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 			      struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			      void *entry_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			      int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 			      int csum_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	struct ext4_dir_entry_2 *de, *pde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	unsigned int blocksize = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	pde = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 	de = (struct ext4_dir_entry_2 *)entry_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	while (i < buf_size - csum_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 		if (ext4_check_dir_entry(dir, NULL, de, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 					 entry_buf, buf_size, lblk, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 		if (de == de_del)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 			if (pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 				pde->rec_len = ext4_rec_len_to_disk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 					ext4_rec_len_from_disk(pde->rec_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 							       blocksize) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 					ext4_rec_len_from_disk(de->rec_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 							       blocksize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 					blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 				de->inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 			inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 		i += ext4_rec_len_from_disk(de->rec_len, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 		pde = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 		de = ext4_next_entry(de, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) static int ext4_delete_entry(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 			     struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 			     struct ext4_dir_entry_2 *de_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 			     ext4_lblk_t lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 			     struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	int err, csum_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	if (ext4_has_inline_data(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		int has_inline_data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		err = ext4_delete_inline_entry(handle, dir, de_del, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 					       &has_inline_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		if (has_inline_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	if (ext4_has_metadata_csum(dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		csum_size = sizeof(struct ext4_dir_entry_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	BUFFER_TRACE(bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	err = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	err = ext4_generic_delete_entry(dir, de_del, lblk, bh, bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 					dir->i_sb->s_blocksize, csum_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	err = ext4_handle_dirty_dirblock(handle, dir, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	if (err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		ext4_std_error(dir->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649)  * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650)  * since this indicates that nlinks count was previously 1 to avoid overflowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651)  * the 16-bit i_links_count field on disk.  Directories with i_nlink == 1 mean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652)  * that subdirectory link counts are not being maintained accurately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654)  * The caller has already checked for i_nlink overflow in case the DIR_LINK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655)  * feature is not enabled and returned -EMLINK.  The is_dx() check is a proxy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656)  * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657)  * on regular files) and to avoid creating huge/slow non-HTREE directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) static void ext4_inc_count(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	if (is_dx(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	    (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 		set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668)  * If a directory had nlink == 1, then we should let it be 1. This indicates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669)  * directory has >EXT4_LINK_MAX subdirs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) static void ext4_dec_count(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679)  * Add non-directory inode to a directory. On success, the inode reference is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680)  * consumed by dentry is instantiation. This is also indicated by clearing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681)  * *inodep pointer. On failure, the caller is responsible for dropping the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682)  * inode reference in the safe context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) static int ext4_add_nondir(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 		struct dentry *dentry, struct inode **inodep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	struct inode *dir = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	struct inode *inode = *inodep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	int err = ext4_add_entry(handle, dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 		err = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 		if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 			ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 		d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 		*inodep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705)  * By the time this is called, we already have created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706)  * the directory cache entry for the new file, but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707)  * is so far negative - it has no inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709)  * If the create succeeds, we fill in the inode information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710)  * with d_instantiate().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		       bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	int err, credits, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	err = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 					    NULL, EXT4_HT_DIR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	if (!IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 		inode->i_op = &ext4_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 		inode->i_fop = &ext4_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 		ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 		err = ext4_add_nondir(handle, dentry, &inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 			ext4_fc_track_create(handle, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	if (!IS_ERR_OR_NULL(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) static int ext4_mknod(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 		      umode_t mode, dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	int err, credits, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	err = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 					    NULL, EXT4_HT_DIR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	if (!IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 		init_special_inode(inode, inode->i_mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 		inode->i_op = &ext4_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 		err = ext4_add_nondir(handle, dentry, &inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 			ext4_fc_track_create(handle, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	if (!IS_ERR_OR_NULL(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	int err, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	err = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	inode = ext4_new_inode_start_handle(dir, mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 					    NULL, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 					    EXT4_HT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 			EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 			  4 + EXT4_XATTR_TRANS_BLOCKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	if (!IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 		inode->i_op = &ext4_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 		inode->i_fop = &ext4_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 		ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 		d_tmpfile(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 		err = ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 			goto err_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) err_unlock_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 			  struct ext4_dir_entry_2 *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 			  int blocksize, int csum_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 			  unsigned int parent_ino, int dotdot_real_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	de->inode = cpu_to_le32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	de->name_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 					   blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	strcpy(de->name, ".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	ext4_set_de_type(inode->i_sb, de, S_IFDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	de = ext4_next_entry(de, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	de->inode = cpu_to_le32(parent_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	de->name_len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	if (!dotdot_real_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 		de->rec_len = ext4_rec_len_to_disk(blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 					(csum_size + ext4_dir_rec_len(1, NULL)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 					blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 		de->rec_len = ext4_rec_len_to_disk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 					ext4_dir_rec_len(de->name_len, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 					blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	strcpy(de->name, "..");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	ext4_set_de_type(inode->i_sb, de, S_IFDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	return ext4_next_entry(de, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) int ext4_init_new_dir(handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 			     struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	struct buffer_head *dir_block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	ext4_lblk_t block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	unsigned int blocksize = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	int csum_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	if (ext4_has_metadata_csum(dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 		csum_size = sizeof(struct ext4_dir_entry_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 		err = ext4_try_create_inline_dir(handle, dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 		if (err < 0 && err != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 	inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 	dir_block = ext4_append(handle, inode, &block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	if (IS_ERR(dir_block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 		return PTR_ERR(dir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	de = (struct ext4_dir_entry_2 *)dir_block->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 	set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 	if (csum_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 		ext4_initialize_dirent_tail(dir_block, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 	set_buffer_verified(dir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	brelse(dir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	int err, err2 = 0, credits, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	if (EXT4_DIR_LINK_MAX(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 		return -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	err = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 	credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 					    &dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 					    0, NULL, EXT4_HT_DIR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 	handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		goto out_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 	inode->i_op = &ext4_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	inode->i_fop = &ext4_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	err = ext4_init_new_dir(handle, dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 		goto out_clear_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 	err = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		err = ext4_add_entry(handle, dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) out_clear_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 		clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 		ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 		err2 = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		if (unlikely(err2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 			err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 		goto out_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 	ext4_inc_count(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	ext4_update_dx_flag(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	err = ext4_mark_inode_dirty(handle, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 		goto out_clear_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 	ext4_fc_track_create(handle, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) out_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) out_retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956)  * routine to check that the specified directory is empty (for rmdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) bool ext4_empty_dir(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	if (ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		int has_inline_data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 		ret = empty_inline_dir(inode, &has_inline_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 		if (has_inline_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 	if (inode->i_size < ext4_dir_rec_len(1, NULL) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 					ext4_dir_rec_len(2, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 		EXT4_ERROR_INODE(inode, "invalid size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 	/* The first directory block must not be a hole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	 * so treat it as DIRENT_HTREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	de = (struct ext4_dir_entry_2 *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 				 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	    le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 		ext4_warning_inode(inode, "directory missing '.'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 	de = ext4_next_entry(de, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 	if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 				 offset) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	    le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 		ext4_warning_inode(inode, "directory missing '..'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 	while (offset < inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 		if (!(offset & (sb->s_blocksize - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 			unsigned int lblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 			lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 			bh = ext4_read_dirblock(inode, lblock, EITHER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 			if (bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 				offset += sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 			if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		de = (struct ext4_dir_entry_2 *) (bh->b_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 					(offset & (sb->s_blocksize - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 		if (ext4_check_dir_entry(inode, NULL, de, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 					 bh->b_data, bh->b_size, 0, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 			offset = (offset | (sb->s_blocksize - 1)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		if (le32_to_cpu(de->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 		offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)  * ext4_orphan_add() links an unlinked or truncated inode into a list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037)  * such inodes, starting at the superblock, in case we crash before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038)  * file is closed/deleted, or in case the inode truncate spans multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039)  * transactions and the last transaction is not recovered after a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041)  * At filesystem recovery time, we walk this list deleting unlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)  * inodes and truncating linked inodes in ext4_orphan_cleanup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044)  * Orphan list manipulation functions must be called under i_mutex unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045)  * we are just creating the inode or deleting it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) int ext4_orphan_add(handle_t *handle, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	int err = 0, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	bool dirty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	if (!sbi->s_journal || is_bad_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 		     !inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	 * Exit early if inode already is on orphan list. This is a big speedup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	 * since we don't have to contend on the global s_orphan_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	if (!list_empty(&EXT4_I(inode)->i_orphan))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	 * Orphan handling is only valid for files with data blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	 * being truncated, or files being unlinked. Note that we either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	 * hold i_mutex, or the inode can not be referenced from outside,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 	 * so i_nlink should not be bumped due to race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 		  S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	BUFFER_TRACE(sbi->s_sbh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	err = ext4_journal_get_write_access(handle, sbi->s_sbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	err = ext4_reserve_inode_write(handle, inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	mutex_lock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	 * Due to previous errors inode may be already a part of on-disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	 * orphan list. If so skip on-disk list modification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 	    (le32_to_cpu(sbi->s_es->s_inodes_count))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 		/* Insert this inode at the head of the on-disk orphan list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 		NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 		sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 		dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	if (dirty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 		err = ext4_handle_dirty_super(handle, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 		rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 			err = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 			 * We have to remove inode from in-memory list if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 			 * addition to on disk orphan list failed. Stray orphan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 			 * list entries can cause panics at unmount time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 			mutex_lock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 			list_del_init(&EXT4_I(inode)->i_orphan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 			mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 		brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 	jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	jbd_debug(4, "orphan inode %lu will point to %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 			inode->i_ino, NEXT_ORPHAN(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127)  * ext4_orphan_del() removes an unlinked or truncated inode from the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128)  * of such inodes stored on disk, because it is finally being cleaned up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) int ext4_orphan_del(handle_t *handle, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 	struct list_head *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	__u32 ino_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 	if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 		     !inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	/* Do this quick check before taking global s_orphan_lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	if (list_empty(&ei->i_orphan))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	if (handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 		/* Grab inode buffer early before taking global s_orphan_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 		err = ext4_reserve_inode_write(handle, inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 	mutex_lock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 	prev = ei->i_orphan.prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 	list_del_init(&ei->i_orphan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	/* If we're on an error path, we may not have a valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 	 * transaction handle with which to update the orphan list on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 	 * disk, but we still need to remove the inode from the linked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	 * list in memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 	if (!handle || err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 		mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 	ino_next = NEXT_ORPHAN(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	if (prev == &sbi->s_orphan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 		jbd_debug(4, "superblock will point to %u\n", ino_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 		BUFFER_TRACE(sbi->s_sbh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 		err = ext4_journal_get_write_access(handle, sbi->s_sbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 			mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 			goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 		sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 		mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 		err = ext4_handle_dirty_super(handle, inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 		struct ext4_iloc iloc2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 		struct inode *i_prev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 			&list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 		jbd_debug(4, "orphan inode %lu will point to %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 			  i_prev->i_ino, ino_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 		err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 			mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 			goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 		NEXT_ORPHAN(i_prev) = ino_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 		err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 		mutex_unlock(&sbi->s_orphan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 		goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	NEXT_ORPHAN(inode) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	err = ext4_mark_iloc_dirty(handle, inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	ext4_std_error(inode->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) out_brelse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 	/* Initialize quotas before so that eventual writes go in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	 * separate transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 	retval = dquot_initialize(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 	bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL, &lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 		return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 	inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	retval = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	if (le32_to_cpu(de->inode) != inode->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	retval = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	if (!ext4_empty_dir(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 	handle = ext4_journal_start(dir, EXT4_HT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 				    EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 		retval = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 		handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	retval = ext4_delete_entry(handle, dir, de, lblk, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	if (!EXT4_DIR_LINK_EMPTY(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 		ext4_warning_inode(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 			     "empty directory '%.*s' has too many links (%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 			     dentry->d_name.len, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 			     inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 	/* There's no need to set i_disksize: the fact that i_nlink is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	 * zero will ensure that the right thing happens during any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	 * recovery. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 	retval = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 	ext4_dec_count(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 	ext4_update_dx_flag(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	ext4_fc_track_unlink(handle, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 	retval = ext4_mark_inode_dirty(handle, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 	/* VFS negative dentries are incompatible with Encoding and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 	 * Case-insensitiveness. Eventually we'll want avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 	 * invalidating the dentries here, alongside with returning the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 	 * negative dentries at ext4_lookup(), when it is better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 	 * supported by the VFS for the CI case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 	if (IS_CASEFOLDED(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 		d_invalidate(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) end_rmdir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) int __ext4_unlink(handle_t *handle, struct inode *dir, const struct qstr *d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 		  struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 	int retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 	int skip_remove_dentry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 	bh = ext4_find_entry(dir, d_name, &de, NULL, &lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 		return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 	if (le32_to_cpu(de->inode) != inode->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 		 * It's okay if we find dont find dentry which matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 		 * the inode. That's because it might have gotten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 		 * renamed to a different inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 		if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 			skip_remove_dentry = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	if (!skip_remove_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 		retval = ext4_delete_entry(handle, dir, de, lblk, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 		dir->i_ctime = dir->i_mtime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 		ext4_update_dx_flag(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 		retval = ext4_mark_inode_dirty(handle, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 		retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	if (inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 		ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 				   d_name->len, d_name->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 	if (!inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 		ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 	retval = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) static int ext4_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 	trace_ext4_unlink_enter(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 	 * Initialize quotas before so that eventual writes go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 	 * in separate transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 		goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 	retval = dquot_initialize(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 		goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	handle = ext4_journal_start(dir, EXT4_HT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 				    EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 		retval = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 		goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 	retval = __ext4_unlink(handle, dir, &dentry->d_name, d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 	if (!retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 		ext4_fc_track_unlink(handle, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	/* VFS negative dentries are incompatible with Encoding and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	 * Case-insensitiveness. Eventually we'll want avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 	 * invalidating the dentries here, alongside with returning the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 	 * negative dentries at ext4_lookup(), when it is  better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 	 * supported by the VFS for the CI case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 	if (IS_CASEFOLDED(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 		d_invalidate(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) out_trace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 	trace_ext4_unlink_exit(dentry, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) static int ext4_symlink(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 			struct dentry *dentry, const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 	int err, len = strlen(symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 	int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	struct fscrypt_str disk_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 				      &disk_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	err = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 	if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 		 * For non-fast symlinks, we just allocate inode and put it on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 		 * orphan list in the first transaction => we need bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 		 * group descriptor, sb, inode block, quota blocks, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 		 * possibly selinux xattr blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 		credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 			  EXT4_XATTR_TRANS_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 		 * Fast symlink. We have to add entry to directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 		 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 		 * allocate new inode (bitmap, group descriptor, inode block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 		 * quota blocks, sb is already counted in previous macros).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 		credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 			  EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 					    &dentry->d_name, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 					    EXT4_HT_DIR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 	handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 		if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 		return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 	if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 		err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 			goto err_drop_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 		inode->i_op = &ext4_encrypted_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 		if (!IS_ENCRYPTED(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 			inode->i_op = &ext4_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 		inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 		ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 		 * We cannot call page_symlink() with transaction started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 		 * because it calls into ext4_write_begin() which can wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 		 * for transaction commit if we are running out of space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 		 * and thus we deadlock. So we have to stop transaction now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 		 * and restart it when symlink contents is written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 		 * 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 		 * To keep fs consistent in case of crash, we have to put inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 		 * to orphan list in the mean time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 		err = ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 		if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 		handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 			goto err_drop_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 		err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 			goto err_drop_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 		 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 		 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 		handle = ext4_journal_start(dir, EXT4_HT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 				EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 				EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 		if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 			err = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 			handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 			goto err_drop_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 		set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 		err = ext4_orphan_del(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 			goto err_drop_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 		/* clear the extent format for fast symlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 		ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 		if (!IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 			inode->i_op = &ext4_fast_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 			inode->i_link = (char *)&EXT4_I(inode)->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 		       disk_link.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 		inode->i_size = disk_link.len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 	EXT4_I(inode)->i_disksize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	err = ext4_add_nondir(handle, dentry, &inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 	goto out_free_encrypted_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) err_drop_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) out_free_encrypted_link:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 	if (disk_link.name != (unsigned char *)symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 		kfree(disk_link.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 	int err, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	handle = ext4_journal_start(dir, EXT4_HT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 		(EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 		 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	if (IS_ERR(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 		return PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 	if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 	ext4_inc_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 	err = ext4_add_entry(handle, dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 		err = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 		/* this can happen only for tmpfile being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 		 * linked the first time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 		if (inode->i_nlink == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 			ext4_orphan_del(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 		d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 		ext4_fc_track_link(handle, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) static int ext4_link(struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 		     struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 	struct inode *inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 	if (inode->i_nlink >= EXT4_LINK_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 		return -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 	err = fscrypt_prepare_link(old_dentry, dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 	    (!projid_eq(EXT4_I(dir)->i_projid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 			EXT4_I(old_dentry->d_inode)->i_projid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 		return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 	err = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	return __ext4_link(dir, inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600)  * Try to find buffer head where contains the parent block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601)  * It should be the inode block if it is inlined or the 1st block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602)  * if it is a normal dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 					struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 					int *retval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 					struct ext4_dir_entry_2 **parent_de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 					int *inlined)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 	if (!ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 		/* The first directory block must not be a hole, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 		 * treat it as DIRENT_HTREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 		bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 			*retval = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 		*parent_de = ext4_next_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 					(struct ext4_dir_entry_2 *)bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 					inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 	*inlined = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	return ext4_get_first_inline_block(inode, parent_de, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) struct ext4_renament {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 	struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 	bool is_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 	int dir_nlink_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 	/* entry for "dentry" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 	int inlined;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 	/* entry for ".." in inode if it's a directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 	struct buffer_head *dir_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	struct ext4_dir_entry_2 *parent_de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 	int dir_inlined;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 	ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 					      &retval, &ent->parent_de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 					      &ent->dir_inlined);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	if (!ent->dir_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 	if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 	BUFFER_TRACE(ent->dir_bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 	return ext4_journal_get_write_access(handle, ent->dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 				  unsigned dir_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 	ent->parent_de->inode = cpu_to_le32(dir_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 	BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 	if (!ent->dir_inlined) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 		if (is_dx(ent->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 			retval = ext4_handle_dirty_dx_node(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 							   ent->inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 							   ent->dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 			retval = ext4_handle_dirty_dirblock(handle, ent->inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 							    ent->dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 		retval = ext4_mark_inode_dirty(handle, ent->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 		ext4_std_error(ent->dir->i_sb, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 		       unsigned ino, unsigned file_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	int retval, retval2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	BUFFER_TRACE(ent->bh, "get write access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 	retval = ext4_journal_get_write_access(handle, ent->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 	ent->de->inode = cpu_to_le32(ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 	if (ext4_has_feature_filetype(ent->dir->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 		ent->de->file_type = file_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 	inode_inc_iversion(ent->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 	ent->dir->i_ctime = ent->dir->i_mtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 		current_time(ent->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 	retval = ext4_mark_inode_dirty(handle, ent->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 	BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 	if (!ent->inlined) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 		retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 		if (unlikely(retval2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 			ext4_std_error(ent->dir->i_sb, retval2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 			return retval2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 			  unsigned ino, unsigned file_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	struct ext4_renament old = *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	 * old->de could have moved from under us during make indexed dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 	 * so the old->de may no longer valid and need to find it again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	 * before reset old inode info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 				 NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 	if (IS_ERR(old.bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 		retval = PTR_ERR(old.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 	if (!old.bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 		retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 		ext4_std_error(old.dir->i_sb, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 	ext4_setent(handle, &old, ino, file_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 	brelse(old.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 				  const struct qstr *d_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	int retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	struct ext4_dir_entry_2 *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	bh = ext4_find_entry(dir, d_name, &de, NULL, &lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 		return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	if (bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 		retval = ext4_delete_entry(handle, dir, de, lblk, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 			       int force_reread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 	 * ent->de could have moved from under us during htree split, so make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 	 * sure that we are deleting the right entry.  We might also be pointing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 	 * to a stale entry in the unused part of ent->bh so just checking inum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 	 * and the name isn't enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 	if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 	    ent->de->name_len != ent->dentry->d_name.len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	    strncmp(ent->de->name, ent->dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 		    ent->de->name_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	    force_reread) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 		retval = ext4_find_delete_entry(handle, ent->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 						&ent->dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 		retval = ext4_delete_entry(handle, ent->dir, ent->de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 						ent->lblk, ent->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 		if (retval == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 			retval = ext4_find_delete_entry(handle, ent->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 							&ent->dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 		ext4_warning_inode(ent->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 				   "Deleting old file: nlink %d, error=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 				   ent->dir->i_nlink, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 	if (ent->dir_nlink_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 		if (ent->dir_nlink_delta == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 			ext4_dec_count(ent->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 			ext4_inc_count(ent->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 		ext4_mark_inode_dirty(handle, ent->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 					      int credits, handle_t **h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 	struct inode *wh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 	int retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 	 * for inode block, sb block, group summaries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 	 * and inode bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 	credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 		    EXT4_XATTR_TRANS_BLOCKS + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 	wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 					 &ent->dentry->d_name, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 					 EXT4_HT_DIR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 	handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 	if (IS_ERR(wh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 		if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 		if (PTR_ERR(wh) == -ENOSPC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 		    ext4_should_retry_alloc(ent->dir->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 		*h = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 		init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 		wh->i_op = &ext4_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 	return wh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840)  * Anybody can rename anything with this: the permission checks are left to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841)  * higher-level routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843)  * n.b.  old_{dentry,inode) refers to the source dentry/inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844)  * while new_{dentry,inode) refers to the destination dentry/inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845)  * This comes from rename(const char *oldpath, const char *newpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 		       struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 		       unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 	struct ext4_renament old = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 		.dir = old_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 		.dentry = old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 		.inode = d_inode(old_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 	struct ext4_renament new = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		.dir = new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 		.dentry = new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 		.inode = d_inode(new_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 	int force_reread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 	struct inode *whiteout = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 	int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 	u8 old_file_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 	if (new.inode && new.inode->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 		EXT4_ERROR_INODE(new.inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 				 "target of rename is already freed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 	if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	    (!projid_eq(EXT4_I(new_dir)->i_projid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 			EXT4_I(old_dentry->d_inode)->i_projid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 		return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 	retval = dquot_initialize(old.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 	retval = dquot_initialize(new.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	/* Initialize quotas before so that eventual writes go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 	 * in separate transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 	if (new.inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 		retval = dquot_initialize(new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 				&old.lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 	if (IS_ERR(old.bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 		return PTR_ERR(old.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	 *  Check for inode number is _not_ due to possible IO errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 	 *  We might rmdir the source, keep it as pwd of some process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 	 *  and merrily kill the link to whatever was created under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 	 *  same name. Goodbye sticky bit ;-<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 	retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 	if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 		goto release_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 	new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 				 &new.de, &new.inlined, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 	if (IS_ERR(new.bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 		retval = PTR_ERR(new.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 		new.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 		goto release_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 	if (new.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 		if (!new.inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 			brelse(new.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 			new.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 	if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 		ext4_alloc_da_blocks(old.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 	credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 	if (!(flags & RENAME_WHITEOUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 		handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 		if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 			retval = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 			goto release_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 		whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 		if (IS_ERR(whiteout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 			retval = PTR_ERR(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 			goto release_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 	old_file_type = old.de->file_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 	if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 	if (S_ISDIR(old.inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 		if (new.inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 			retval = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 			if (!ext4_empty_dir(new.inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 				goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 			retval = -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 			if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 				goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 		retval = ext4_rename_dir_prepare(handle, &old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 	 * If we're renaming a file within an inline_data dir and adding or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 	 * setting the new dirent causes a conversion from inline_data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 	 * extents/blockmap, we need to force the dirent delete code to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	 * re-read the directory, or else we end up trying to delete a dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 	 * from what is now the extent tree root (or a block map).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 	force_reread = (new.dir->i_ino == old.dir->i_ino &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 			ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 	if (whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 		 * Do this before adding a new entry, so the old entry is sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 		 * to be still pointing to the valid old entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 		retval = ext4_setent(handle, &old, whiteout->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 				     EXT4_FT_CHRDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 		retval = ext4_mark_inode_dirty(handle, whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 		if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 	if (!new.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 		retval = ext4_add_entry(handle, new.dentry, old.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 		retval = ext4_setent(handle, &new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 				     old.inode->i_ino, old_file_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 	if (force_reread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 		force_reread = !ext4_test_inode_flag(new.dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 						     EXT4_INODE_INLINE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 	 * Like most other Unix systems, set the ctime for inodes on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 	 * rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	old.inode->i_ctime = current_time(old.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	retval = ext4_mark_inode_dirty(handle, old.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 	if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 	if (!whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 		 * ok, that's it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 		ext4_rename_delete(handle, &old, force_reread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 	if (new.inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 		ext4_dec_count(new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 		new.inode->i_ctime = current_time(new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 	old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	ext4_update_dx_flag(old.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 	if (old.dir_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 		retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 		ext4_dec_count(old.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 		if (new.inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 			/* checked ext4_empty_dir above, can't have another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 			 * parent, ext4_dec_count() won't work for many-linked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 			 * dirs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 			clear_nlink(new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 			ext4_inc_count(new.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 			ext4_update_dx_flag(new.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 			retval = ext4_mark_inode_dirty(handle, new.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 			if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 				goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	retval = ext4_mark_inode_dirty(handle, old.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 	if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 	if (S_ISDIR(old.inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 		 * We disable fast commits here that's because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 		 * replay code is not yet capable of changing dot dot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 		 * dirents in directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 		ext4_fc_mark_ineligible(old.inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 			EXT4_FC_REASON_RENAME_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 		if (new.inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 			ext4_fc_track_unlink(handle, new.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 		__ext4_fc_track_link(handle, old.inode, new.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 		__ext4_fc_track_unlink(handle, old.inode, old.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 		if (whiteout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 			__ext4_fc_track_create(handle, whiteout, old.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 	if (new.inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 		retval = ext4_mark_inode_dirty(handle, new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) 		if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 		if (!new.inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) 			ext4_orphan_add(handle, new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) end_rename:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	if (whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 			ext4_resetent(handle, &old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 				      old.inode->i_ino, old_file_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 			drop_nlink(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 			ext4_orphan_add(handle, whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 		unlock_new_inode(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 		iput(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) release_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 	brelse(old.dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 	brelse(old.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 	brelse(new.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 			     struct inode *new_dir, struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 	struct ext4_renament old = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 		.dir = old_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 		.dentry = old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 		.inode = d_inode(old_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 	struct ext4_renament new = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 		.dir = new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 		.dentry = new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 		.inode = d_inode(new_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	u8 new_file_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 	struct timespec64 ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 	if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 	     !projid_eq(EXT4_I(new_dir)->i_projid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 			EXT4_I(old_dentry->d_inode)->i_projid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 	    (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 	     !projid_eq(EXT4_I(old_dir)->i_projid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 			EXT4_I(new_dentry->d_inode)->i_projid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) 		return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) 	retval = dquot_initialize(old.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 	retval = dquot_initialize(new.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 				 &old.de, &old.inlined, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 	if (IS_ERR(old.bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 		return PTR_ERR(old.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 	 *  Check for inode number is _not_ due to possible IO errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 	 *  We might rmdir the source, keep it as pwd of some process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	 *  and merrily kill the link to whatever was created under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 	 *  same name. Goodbye sticky bit ;-<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 	retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 	if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 	new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 				 &new.de, &new.inlined, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	if (IS_ERR(new.bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 		retval = PTR_ERR(new.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 		new.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 	/* RENAME_EXCHANGE case: old *and* new must both exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 	if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 	handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 		(2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 		 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 		retval = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 		handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 	if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 	if (S_ISDIR(old.inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 		old.is_dir = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 		retval = ext4_rename_dir_prepare(handle, &old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 	if (S_ISDIR(new.inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 		new.is_dir = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 		retval = ext4_rename_dir_prepare(handle, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 	 * Other than the special case of overwriting a directory, parents'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 	 * nlink only needs to be modified if this is a cross directory rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 	if (old.dir != new.dir && old.is_dir != new.is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 		old.dir_nlink_delta = old.is_dir ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 		new.dir_nlink_delta = -old.dir_nlink_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 		retval = -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 		if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 		    (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 	new_file_type = new.de->file_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 	retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 	retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 	 * Like most other Unix systems, set the ctime for inodes on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	 * rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 	ctime = current_time(old.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 	old.inode->i_ctime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 	new.inode->i_ctime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 	retval = ext4_mark_inode_dirty(handle, old.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 	if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	retval = ext4_mark_inode_dirty(handle, new.inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	if (unlikely(retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 		goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 	ext4_fc_mark_ineligible(new.inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 				EXT4_FC_REASON_CROSS_RENAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 	if (old.dir_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 		retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	if (new.dir_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 		retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 			goto end_rename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 	ext4_update_dir_count(handle, &old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 	ext4_update_dir_count(handle, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) end_rename:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 	brelse(old.dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 	brelse(new.dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	brelse(old.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 	brelse(new.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 			struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 			unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 				     flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 	if (flags & RENAME_EXCHANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 		return ext4_cross_rename(old_dir, old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 					 new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 	return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259)  * directories can handle most operations...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) const struct inode_operations ext4_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 	.create		= ext4_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 	.lookup		= ext4_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	.link		= ext4_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	.unlink		= ext4_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	.symlink	= ext4_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	.mkdir		= ext4_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	.rmdir		= ext4_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 	.mknod		= ext4_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	.tmpfile	= ext4_tmpfile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 	.rename		= ext4_rename2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 	.setattr	= ext4_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 	.getattr	= ext4_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	.listxattr	= ext4_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	.get_acl	= ext4_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	.set_acl	= ext4_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 	.fiemap         = ext4_fiemap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) const struct inode_operations ext4_special_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 	.setattr	= ext4_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	.getattr	= ext4_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 	.listxattr	= ext4_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	.get_acl	= ext4_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 	.set_acl	= ext4_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) };