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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2006-2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2006, 2007 University of Szeged, Hungary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Authors: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *          Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *          Zoltan Sogor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * This file implements directory operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * All FS operations in this file allocate budget before writing anything to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * media. If they fail to allocate it, the error is returned. The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * if they unable to allocate the budget, because deletion %-ENOSPC failure is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * not what users are usually ready to get. UBIFS budgeting subsystem has some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * space reserved for these purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * All operations in this file write all inodes which they change straight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * away, instead of marking them dirty. For example, 'ubifs_link()' changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * @i_size of the parent inode and writes the parent inode together with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * target inode. This was done to simplify file-system recovery which would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * otherwise be very difficult to do. The only exception is rename which marks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * the re-named inode dirty (because its @i_ctime is updated) but does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * write it, but just marks it as dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * inherit_flags - inherit flags of the parent inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * @dir: parent inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * @mode: new inode mode flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * parent directory inode @dir. UBIFS inodes inherit the following flags:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  *   sub-directory basis;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * o %UBIFS_SYNC_FL - useful for the same reasons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * This function returns the inherited flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) static int inherit_flags(const struct inode *dir, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	const struct ubifs_inode *ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	if (!S_ISDIR(dir->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 		 * The parent is not a directory, which means that an extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		 * attribute inode is being created. No flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	if (!S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		/* The "DIRSYNC" flag only applies to directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		flags &= ~UBIFS_DIRSYNC_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * ubifs_new_inode - allocate new UBIFS inode object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * @dir: parent directory inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * @mode: inode mode flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * This function finds an unused inode number, allocates new inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * initializes it. Returns new inode in case of success and an error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 			      umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	struct ubifs_inode *ui;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	bool encrypted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	inode = new_inode(c->vfs_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	 * marking them dirty in file write path (see 'file_update_time()').
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	 * to make budgeting work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	inode->i_flags |= S_NOCMTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	inode_init_owner(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	inode->i_mtime = inode->i_atime = inode->i_ctime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			 current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	inode->i_mapping->nrpages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		goto out_iput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	switch (mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		inode->i_mapping->a_ops = &ubifs_file_address_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		inode->i_op = &ubifs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		inode->i_fop = &ubifs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		inode->i_op  = &ubifs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		inode->i_fop = &ubifs_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		inode->i_op = &ubifs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	case S_IFSOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	case S_IFIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	case S_IFBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	case S_IFCHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		inode->i_op  = &ubifs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	ui->flags = inherit_flags(dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	ubifs_set_inode_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (S_ISREG(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		ui->compr_type = c->default_compr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		ui->compr_type = UBIFS_COMPR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	ui->synced_i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	spin_lock(&c->cnt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	/* Inode number overflow is currently not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (c->highest_inum >= INUM_WARN_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		if (c->highest_inum >= INUM_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			spin_unlock(&c->cnt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 			ubifs_err(c, "out of inode numbers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 			goto out_iput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 			   (unsigned long)c->highest_inum, INUM_WATERMARK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	inode->i_ino = ++c->highest_inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	 * The creation sequence number remains with this inode for its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	 * lifetime. All nodes for this inode have a greater sequence number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	 * and so it is possible to distinguish obsolete nodes belonging to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	 * previous incarnation of the same inode number - for example, for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	 * purpose of rebuilding the index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	ui->creat_sqnum = ++c->max_sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	spin_unlock(&c->cnt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (encrypted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		err = fscrypt_set_context(inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			ubifs_err(c, "fscrypt_set_context failed: %i", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			goto out_iput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) out_iput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) static int dbg_check_name(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 			  const struct ubifs_dent_node *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 			  const struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (!dbg_is_chk_gen(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (le16_to_cpu(dent->nlen) != fname_len(nm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 				   unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	struct ubifs_dent_node *dent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	err = fscrypt_prepare_lookup(dir, dentry, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	generic_set_encrypted_ci_d_ops(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		return d_splice_alias(NULL, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	if (fname_len(&nm) > UBIFS_MAX_NLEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		inode = ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	if (!dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		inode = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	if (fname_name(&nm) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		if (nm.hash & ~UBIFS_S_KEY_HASH_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			goto done; /* ENOENT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		dent_key_init(c, &key, dir->i_ino, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			dbg_gen("not found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 			inode = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (dbg_check_name(c, dent, &nm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		inode = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		goto done;
^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) 	inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		 * This should not happen. Probably the file-system needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		 * checking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		ubifs_err(c, "dead directory entry '%pd', error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			  dentry, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		ubifs_ro_mode(c, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	if (IS_ENCRYPTED(dir) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	    !fscrypt_has_permitted_context(dir, inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			   dir->i_ino, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		inode = ERR_PTR(-EPERM);
^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) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	kfree(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) static int ubifs_prepare_create(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 				struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (fscrypt_is_nokey_name(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	return fscrypt_setup_filename(dir, &dentry->d_name, 0, nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 			bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 					.dirtied_ino = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	int err, sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 * Budget request settings: new inode, new direntry, changing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	 * parent directory inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		dentry, mode, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	err = ubifs_prepare_create(dir, dentry, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	sz_change = CALC_DENT_SIZE(fname_len(&nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	inode = ubifs_new_inode(c, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	err = ubifs_init_security(dir, inode, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	mutex_lock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) out_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	ubifs_err(c, "cannot create regular file, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) static int do_tmpfile(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		      umode_t mode, struct inode **whiteout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	int err, instantiated = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	 * Budget request settings: new dirty inode, new direntry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	 * budget for dirtied inode will be released via writeback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		dentry, mode, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	err = ubifs_budget_space(c, &ino_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	inode = ubifs_new_inode(c, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	err = ubifs_init_security(dir, inode, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		*whiteout = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		d_tmpfile(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	ubifs_assert(c, ui->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	instantiated = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	mutex_lock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) out_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (!instantiated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	else if (whiteout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		iput(*whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	if (!instantiated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		ubifs_release_budget(c, &ino_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	ubifs_err(c, "cannot create temporary file, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			 umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	return do_tmpfile(dir, dentry, mode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * vfs_dent_type - get VFS directory entry type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * @type: UBIFS directory entry type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * This function converts UBIFS directory entry type into VFS directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static unsigned int vfs_dent_type(uint8_t type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	case UBIFS_ITYPE_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		return DT_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	case UBIFS_ITYPE_DIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		return DT_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	case UBIFS_ITYPE_LNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return DT_LNK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	case UBIFS_ITYPE_BLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		return DT_BLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	case UBIFS_ITYPE_CHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		return DT_CHR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	case UBIFS_ITYPE_FIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		return DT_FIFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	case UBIFS_ITYPE_SOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		return DT_SOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * The classical Unix view for directory is that it is a linear array of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * (name, inode number) entries. Linux/VFS assumes this model as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * Particularly, 'readdir()' call wants us to return a directory entry offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * which later may be used to continue 'readdir()'ing the directory or to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * model because directory entries are identified by keys, which may collide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * UBIFS uses directory entry hash value for directory offsets, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  * 'seekdir()'/'telldir()' may not always work because of possible key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  * properly by means of saving full directory entry name in the private field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  * of the file description object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * This means that UBIFS cannot support NFS which requires full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  * 'seekdir()'/'telldir()' support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) static int ubifs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	int fstr_real_len = 0, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct fscrypt_str fstr = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct ubifs_dent_node *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	struct inode *dir = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	bool encrypted = IS_ENCRYPTED(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		 * The directory was seek'ed to a senseless position or there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		 * are no more entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	if (encrypted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		err = fscrypt_prepare_readdir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		fstr_real_len = fstr.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	if (file->f_version == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		 * The file was seek'ed, which means that @file->private_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		 * is now invalid. This may also be just the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		 * 'ubifs_readdir()' invocation, in which case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		 * @file->private_data is NULL, and the below code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		 * basically a no-op.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * 'generic_file_llseek()' unconditionally sets @file->f_version to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 * zero, and we use this for detecting whether the file was seek'ed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	file->f_version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	/* File positions 0 and 1 correspond to "." and ".." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (ctx->pos < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		ubifs_assert(c, !file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		if (!dir_emit_dots(file, ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			if (encrypted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 				fscrypt_fname_free_buffer(&fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		/* Find the first entry in TNC and save it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		lowest_dent_key(c, &key, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		fname_len(&nm) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		dent = ubifs_tnc_next_ent(c, &key, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			err = PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		ctx->pos = key_hash_flash(c, &dent->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		file->private_data = dent;
^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) 	dent = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (!dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		 * The directory was seek'ed to and is now readdir'ed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		 * Find the entry corresponding to @ctx->pos or the closest one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		fname_len(&nm) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		dent = ubifs_tnc_next_ent(c, &key, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			err = PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		ctx->pos = key_hash_flash(c, &dent->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		file->private_data = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		dbg_gen("ino %llu, new f_pos %#x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			(unsigned long long)le64_to_cpu(dent->inum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			key_hash_flash(c, &dent->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			     ubifs_inode(dir)->creat_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		fname_len(&nm) = le16_to_cpu(dent->nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		fname_name(&nm) = dent->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		if (encrypted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			fstr.len = fstr_real_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 							&dent->key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 							le32_to_cpu(dent->cookie),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 							&nm.disk_name, &fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			fstr.len = fname_len(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			fstr.name = fname_name(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		if (!dir_emit(ctx, fstr.name, fstr.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			       le64_to_cpu(dent->inum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			       vfs_dent_type(dent->type))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			if (encrypted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 				fscrypt_fname_free_buffer(&fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		/* Switch to the next entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		key_read(c, &dent->key, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		dent = ubifs_tnc_next_ent(c, &key, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			err = PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		ctx->pos = key_hash_flash(c, &dent->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		file->private_data = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (encrypted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		fscrypt_fname_free_buffer(&fstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		ubifs_err(c, "cannot find next direntry, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		 * -ENOENT is a non-fatal error in this context, the TNC uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		 * it to indicate that the cursor moved past the current directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		 * and readdir() has to stop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	/* 2 is a special value indicating that there are no more direntries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	ctx->pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) /* Free saved readdir() state when the directory is closed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) static int ubifs_dir_release(struct inode *dir, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  * lock_2_inodes - a wrapper for locking two UBIFS inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * @inode1: first inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * @inode2: second inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  * We do not implement any tricks to guarantee strict lock ordering, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  * VFS has already done it for us on the @i_mutex. So this is just a simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  * wrapper function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682)  * @inode1: first inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683)  * @inode2: second inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		      struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	struct inode *inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * Budget request settings: new direntry, changing the target inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 * changing the parent inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		dentry, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		inode->i_nlink, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	ubifs_assert(c, inode_is_locked(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	ubifs_assert(c, inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	err = fscrypt_prepare_link(old_dentry, dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	err = dbg_check_synced_i_size(c, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	lock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	/* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		ubifs_delete_orphan(c, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	unlock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		ubifs_add_orphan(c, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	unlock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	int err, sz_change, budgeted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	unsigned int saved_nlink = inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	 * Budget request settings: deletion direntry, deletion inode (+1 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 * @dirtied_ino), changing the parent directory inode. If budgeting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	 * fails, go ahead anyway because we have extra space reserved for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	 * deletions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		dentry, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		inode->i_nlink, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	err = ubifs_purge_xattrs(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	sz_change = CALC_DENT_SIZE(fname_len(&nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	ubifs_assert(c, inode_is_locked(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	ubifs_assert(c, inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	err = dbg_check_synced_i_size(c, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		if (err != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		budgeted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	lock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	inode->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	unlock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	if (budgeted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		/* We've deleted something - clean the "no space" flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		c->bi.nospace = c->bi.nospace_rp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	set_nlink(inode, saved_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	unlock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if (budgeted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * check_dir_empty - check if a directory is empty or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * @dir: VFS inode object of the directory to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  * This function checks if directory @dir is empty. Returns zero if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * in case of of errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) int ubifs_check_dir_empty(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct fscrypt_name nm = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	struct ubifs_dent_node *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	lowest_dent_key(c, &key, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	dent = ubifs_tnc_next_ent(c, &key, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		err = PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		kfree(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		err = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	return err;
^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) static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	int err, sz_change, budgeted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	 * Budget request settings: deletion direntry, deletion inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	 * changing the parent inode. If budgeting fails, go ahead anyway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	 * because we have extra space reserved for deletions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		inode->i_ino, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	ubifs_assert(c, inode_is_locked(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	ubifs_assert(c, inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	err = ubifs_check_dir_empty(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	err = ubifs_purge_xattrs(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	sz_change = CALC_DENT_SIZE(fname_len(&nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		if (err != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		budgeted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	lock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	inode->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	drop_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	unlock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if (budgeted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		/* We've deleted something - clean the "no space" flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		c->bi.nospace = c->bi.nospace_rp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	inc_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	unlock_2_inodes(dir, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (budgeted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	int err, sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	 * Budget request settings: new inode, new direntry and changing parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	 * directory inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		dentry, mode, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	err = ubifs_prepare_create(dir, dentry, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	sz_change = CALC_DENT_SIZE(fname_len(&nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	err = ubifs_init_security(dir, inode, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	mutex_lock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	inc_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		ubifs_err(c, "cannot create directory, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	drop_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) out_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		       umode_t mode, dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	struct ubifs_inode *ui;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	union ubifs_dev_desc *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	int sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	int err, devlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 					.dirtied_ino = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	 * Budget request settings: new inode, new direntry and changing parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	 * directory inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (S_ISBLK(mode) || S_ISCHR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		devlen = ubifs_encode_dev(dev, rdev);
^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) 	req.new_ino_d = ALIGN(devlen, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	err = ubifs_prepare_create(dir, dentry, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	sz_change = CALC_DENT_SIZE(fname_len(&nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	inode = ubifs_new_inode(c, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	init_special_inode(inode, inode->i_mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	inode->i_size = ubifs_inode(inode)->ui_size = devlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	ui->data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	ui->data_len = devlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	err = ubifs_init_security(dir, inode, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	mutex_lock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) out_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	return err;
^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) static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			 const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	struct ubifs_inode *ui;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	struct ubifs_inode *dir_ui = ubifs_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	struct ubifs_info *c = dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	int err, sz_change, len = strlen(symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct fscrypt_str disk_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 					.new_ino_d = ALIGN(len, 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 					.dirtied_ino = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		symname, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				      &disk_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	 * Budget request settings: new inode, new direntry and changing parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	 * directory inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	err = ubifs_prepare_create(dir, dentry, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	sz_change = CALC_DENT_SIZE(fname_len(&nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	ui->data = kmalloc(disk_link.len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (!ui->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		disk_link.name = ui->data; /* encrypt directly into ui->data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		memcpy(ui->data, disk_link.name, disk_link.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		inode->i_link = ui->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	 * The terminating zero byte is not written to the flash media and it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	 * is put just to make later in-memory string processing simpler. Thus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	 * data length is @disk_link.len - 1, not @disk_link.len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	ui->data_len = disk_link.len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	err = ubifs_init_security(dir, inode, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		goto out_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	mutex_lock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	dir->i_size += sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	dir->i_mtime = dir->i_ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	goto out_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	dir->i_size -= sz_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	dir_ui->ui_size = dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	mutex_unlock(&dir_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) out_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) out_fname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	fscrypt_free_filename(&nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)  * lock_4_inodes - a wrapper for locking three UBIFS inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  * @inode1: first inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  * @inode2: second inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  * @inode3: third inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  * @inode4: fouth inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  * This function is used for 'ubifs_rename()' and @inode1 may be the same as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  * @inode2 whereas @inode3 and @inode4 may be %NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)  * We do not implement any tricks to guarantee strict lock ordering, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)  * VFS has already done it for us on the @i_mutex. So this is just a simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)  * wrapper function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			  struct inode *inode3, struct inode *inode4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	if (inode2 != inode1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (inode3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	if (inode4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  * @inode1: first inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  * @inode2: second inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  * @inode3: third inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  * @inode4: fouth inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			    struct inode *inode3, struct inode *inode4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	if (inode4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (inode3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (inode1 != inode2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		     struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		     unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	struct inode *old_inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	struct inode *new_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct inode *whiteout = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	struct ubifs_inode *whiteout_ui = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	int err, release, sync = 0, move = (new_dir != old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	int is_dir = S_ISDIR(old_inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	int unlink = !!new_inode, new_sz, old_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 					.dirtied_ino = 3 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			.dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	struct timespec64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	unsigned int saved_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	struct fscrypt_name old_nm, new_nm;
^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) 	 * Budget request settings: deletion direntry, new direntry, removing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	 * the old inode, and changing old and new parent directory inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	 * However, this operation also marks the target inode as dirty and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	 * does not write it, so we allocate budget for the target inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	 * separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		old_dentry, old_inode->i_ino, old_dir->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		new_dentry, new_dir->i_ino, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	if (unlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		ubifs_assert(c, inode_is_locked(new_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		err = ubifs_purge_xattrs(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	if (unlink && is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		err = ubifs_check_dir_empty(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		fscrypt_free_filename(&old_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		fscrypt_free_filename(&old_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		fscrypt_free_filename(&new_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	err = ubifs_budget_space(c, &ino_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		fscrypt_free_filename(&old_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		fscrypt_free_filename(&new_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (flags & RENAME_WHITEOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		union ubifs_dev_desc *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		struct ubifs_budget_req wht_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		spin_lock(&whiteout->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		whiteout->i_state |= I_LINKABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		spin_unlock(&whiteout->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		whiteout_ui = ubifs_inode(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		whiteout_ui->data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		ubifs_assert(c, !whiteout_ui->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		memset(&wht_req, 0, sizeof(struct ubifs_budget_req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		wht_req.dirtied_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		wht_req.dirtied_ino_d = ALIGN(whiteout_ui->data_len, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		 * To avoid deadlock between space budget (holds ui_mutex and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		 * waits wb work) and writeback work(waits ui_mutex), do space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		 * budget before ubifs inodes locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		err = ubifs_budget_space(c, &wht_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			iput(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		/* Add the old_dentry size to the old_dir size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		old_sz -= CALC_DENT_SIZE(fname_len(&old_nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	 * Like most other Unix systems, set the @i_ctime for inodes on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	 * rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	time = current_time(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	old_inode->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	/* We must adjust parent link count when renaming directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	if (is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		if (move) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			 * @old_dir loses a link because we are moving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			 * @old_inode to a different directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			drop_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 			 * @new_dir only gains a link if we are not also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			 * overwriting an existing directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			if (!unlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 				inc_nlink(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			 * @old_inode is not moving to a different directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			 * but @old_dir still loses a link if we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			 * overwriting an existing directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 			if (unlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 				drop_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	old_dir->i_size -= old_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	old_dir->i_mtime = old_dir->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	new_dir->i_mtime = new_dir->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 * And finally, if we unlinked a direntry which happened to have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * same name as the moved direntry, we have to decrement @i_nlink of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 * the unlinked inode and change its ctime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	if (unlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		 * Directories cannot have hard-links, so if this is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		 * directory, just clear @i_nlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		saved_nlink = new_inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		if (is_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 			clear_nlink(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			drop_nlink(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		new_inode->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		new_dir->i_size += new_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	 * is dirty, because this will be done later on at the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	 * 'ubifs_rename()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (IS_SYNC(old_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		if (unlink && IS_SYNC(new_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 			sync = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	if (whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		inc_nlink(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		mark_inode_dirty(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		spin_lock(&whiteout->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		whiteout->i_state &= ~I_LINKABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		spin_unlock(&whiteout->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		iput(whiteout);
^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) 	err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			       new_inode, &new_nm, whiteout, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	mutex_lock(&old_inode_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	release = old_inode_ui->dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	mark_inode_dirty_sync(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	mutex_unlock(&old_inode_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	if (release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		ubifs_release_budget(c, &ino_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	if (IS_SYNC(old_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	fscrypt_free_filename(&old_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	fscrypt_free_filename(&new_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	if (unlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		set_nlink(new_inode, saved_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		new_dir->i_size -= new_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	old_dir->i_size += old_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		if (move) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 			inc_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 			if (!unlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 				drop_nlink(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			if (unlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 				inc_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	if (whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		drop_nlink(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		iput(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) out_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	ubifs_release_budget(c, &ino_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	fscrypt_free_filename(&old_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	fscrypt_free_filename(&new_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	return err;
^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) static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 			struct inode *new_dir, struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 				.dirtied_ino = 2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct inode *fst_inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct inode *snd_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct timespec64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	struct fscrypt_name fst_nm, snd_nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	ubifs_assert(c, fst_inode && snd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		fscrypt_free_filename(&fst_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	lock_4_inodes(old_dir, new_dir, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	time = current_time(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	fst_inode->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	snd_inode->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	old_dir->i_mtime = old_dir->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	new_dir->i_mtime = new_dir->i_ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	if (old_dir != new_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			inc_nlink(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			drop_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			drop_nlink(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			inc_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 				snd_inode, &snd_nm, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	unlock_4_inodes(old_dir, new_dir, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	fscrypt_free_filename(&fst_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	fscrypt_free_filename(&snd_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 			struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	ubifs_assert(c, inode_is_locked(old_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	ubifs_assert(c, inode_is_locked(new_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 				     flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	if (flags & RENAME_EXCHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) int ubifs_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		  u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	if (ui->flags & UBIFS_APPEND_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		stat->attributes |= STATX_ATTR_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	if (ui->flags & UBIFS_COMPR_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		stat->attributes |= STATX_ATTR_COMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	if (ui->flags & UBIFS_CRYPT_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		stat->attributes |= STATX_ATTR_ENCRYPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	if (ui->flags & UBIFS_IMMUTABLE_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		stat->attributes |= STATX_ATTR_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	stat->attributes_mask |= (STATX_ATTR_APPEND |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 				STATX_ATTR_COMPRESSED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 				STATX_ATTR_ENCRYPTED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				STATX_ATTR_IMMUTABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	generic_fillattr(inode, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	stat->blksize = UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	stat->size = ui->ui_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	 * Unfortunately, the 'stat()' system call was designed for block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	 * device based file systems, and it is not appropriate for UBIFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	 * because UBIFS does not have notion of "block". For example, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	 * difficult to tell how many block a directory takes - it actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	 * takes less than 300 bytes, but we have to round it to block size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	 * which introduces large mistake. This makes utilities like 'du' to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	 * report completely senseless numbers. This is the reason why UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	 * goes the same way as JFFS2 - it reports zero blocks for everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	 * but regular files, which makes more sense than reporting completely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	 * wrong sizes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		size = ui->xattr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		size += stat->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		size = ALIGN(size, UBIFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		 * Note, user-space expects 512-byte blocks count irrespectively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		 * of what was reported in @stat->size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		stat->blocks = size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		stat->blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) const struct inode_operations ubifs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	.lookup      = ubifs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	.create      = ubifs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	.link        = ubifs_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	.symlink     = ubifs_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	.unlink      = ubifs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	.mkdir       = ubifs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	.rmdir       = ubifs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	.mknod       = ubifs_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	.rename      = ubifs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	.setattr     = ubifs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	.getattr     = ubifs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) #ifdef CONFIG_UBIFS_FS_XATTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	.listxattr   = ubifs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	.update_time = ubifs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	.tmpfile     = ubifs_tmpfile,
^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) const struct file_operations ubifs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	.llseek         = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	.release        = ubifs_dir_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	.read           = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	.iterate_shared = ubifs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	.fsync          = ubifs_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	.unlocked_ioctl = ubifs_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	.compat_ioctl   = ubifs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) };