dir.c revision 2b1844a8c934723134ee1ff313e51d0d281cdef1
11e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/* * This file is part of UBIFS.
21e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
31e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation.
41e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * Copyright (C) 2006, 2007 University of Szeged, Hungary
51e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
61e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This program is free software; you can redistribute it and/or modify it
71e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * under the terms of the GNU General Public License version 2 as published by
81e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * the Free Software Foundation.
91e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * more details.
141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * You should have received a copy of the GNU General Public License along with
161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём)
201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *          Adrian Hunter
211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *          Zoltan Sogor
221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/*
251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This file implements directory operations.
261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * All FS operations in this file allocate budget before writing anything to the
281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * media. If they fail to allocate it, the error is returned. The only
291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * if they unable to allocate the budget, because deletion %-ENOSPC failure is
311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * not what users are usually ready to get. UBIFS budgeting subsystem has some
321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * space reserved for these purposes.
331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * All operations in this file write all inodes which they change straight
351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * away, instead of marking them dirty. For example, 'ubifs_link()' changes
361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @i_size of the parent inode and writes the parent inode together with the
371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * target inode. This was done to simplify file-system recovery which would
381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * otherwise be very difficult to do. The only exception is rename which marks
391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * the re-named inode dirty (because its @i_ctime is updated) but does not
401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * write it, but just marks it as dirty.
411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#include "ubifs.h"
441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * inherit_flags - inherit flags of the parent inode.
471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @dir: parent inode
481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @mode: new inode mode flags
491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * parent directory inode @dir. UBIFS inodes inherit the following flags:
521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *   sub-directory basis;
541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * o %UBIFS_SYNC_FL - useful for the same reasons;
551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This function returns the inherited flags.
581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int inherit_flags(const struct inode *dir, int mode)
601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int flags;
621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	const struct ubifs_inode *ui = ubifs_inode(dir);
631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!S_ISDIR(dir->i_mode))
651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * The parent is not a directory, which means that an extended
671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * attribute inode is being created. No flags.
681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return 0;
701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!S_ISDIR(mode))
731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* The "DIRSYNC" flag only applies to directories */
741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		flags &= ~UBIFS_DIRSYNC_FL;
751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return flags;
761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * ubifs_new_inode - allocate new UBIFS inode object.
801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @c: UBIFS file-system description object
811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @dir: parent directory inode
821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @mode: inode mode flags
831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This function finds an unused inode number, allocates new inode and
851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * initializes it. Returns new inode in case of success and an error code in
861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * case of failure.
871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystruct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			      int mode)
901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui;
931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = new_inode(c->vfs_sb);
951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui = ubifs_inode(inode);
961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!inode)
971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return ERR_PTR(-ENOMEM);
981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
1001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
1011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * marking them dirty in file write path (see 'file_update_time()').
1021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
1031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * to make budgeting work.
1041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10512e776a0882def45e7ee50918016968b392ac7bdArtem Bityutskiy	inode->i_flags |= S_NOCMTIME;
1061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
107abf5d08acaca942b49daf0ed87a9deddc94b183aDmitry Monakhov	inode_init_owner(inode, dir, mode);
1081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_mtime = inode->i_atime = inode->i_ctime =
1091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 ubifs_current_time(inode);
1101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_mapping->nrpages = 0;
1111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/* Disable readahead */
1121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_mapping->backing_dev_info = &c->bdi;
1131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	switch (mode & S_IFMT) {
1151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFREG:
1161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_mapping->a_ops = &ubifs_file_address_operations;
1171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_op = &ubifs_file_inode_operations;
1181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_fop = &ubifs_file_operations;
1191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		break;
1201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFDIR:
1211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_op  = &ubifs_dir_inode_operations;
1221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_fop = &ubifs_dir_operations;
1231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
1241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		break;
1251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFLNK:
1261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_op = &ubifs_symlink_inode_operations;
1271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		break;
1281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFSOCK:
1291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFIFO:
1301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFBLK:
1311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case S_IFCHR:
1321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_op  = &ubifs_file_inode_operations;
1331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		break;
1341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	default:
1351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		BUG();
1361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
1371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->flags = inherit_flags(dir, mode);
1391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_set_inode_flags(inode);
1401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (S_ISREG(mode))
1411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ui->compr_type = c->default_compr;
1421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	else
1431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ui->compr_type = UBIFS_COMPR_NONE;
1441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->synced_i_size = 0;
1451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	spin_lock(&c->cnt_lock);
1471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/* Inode number overflow is currently not supported */
1481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (c->highest_inum >= INUM_WARN_WATERMARK) {
1491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (c->highest_inum >= INUM_WATERMARK) {
1501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			spin_unlock(&c->cnt_lock);
1511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			ubifs_err("out of inode numbers");
1521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			make_bad_inode(inode);
1531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			iput(inode);
1541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return ERR_PTR(-EINVAL);
1551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
1561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_warn("running out of inode numbers (current %lu, max %d)",
157e84461ad9c4f0ff91ab8553596acdb7bf5522df4Artem Bityutskiy			   (unsigned long)c->highest_inum, INUM_WATERMARK);
1581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
1591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ino = ++c->highest_inum;
1611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
1621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * The creation sequence number remains with this inode for its
1631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * lifetime. All nodes for this inode have a greater sequence number,
1641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * and so it is possible to distinguish obsolete nodes belonging to a
1651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * previous incarnation of the same inode number - for example, for the
1661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * purpose of rebuilding the index.
1671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
1681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->creat_sqnum = ++c->max_sqnum;
1691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	spin_unlock(&c->cnt_lock);
1701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return inode;
1711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
1721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#ifdef CONFIG_UBIFS_FS_DEBUG
1741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
175bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiystatic int dbg_check_name(const struct ubifs_info *c,
176bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy			  const struct ubifs_dent_node *dent,
177bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy			  const struct qstr *nm)
1781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
1792b1844a8c934723134ee1ff313e51d0d281cdef1Artem Bityutskiy	if (!dbg_is_chk_gen(c))
1801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return 0;
1811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (le16_to_cpu(dent->nlen) != nm->len)
1821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -EINVAL;
1831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (memcmp(dent->name, nm->name, nm->len))
1841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -EINVAL;
1851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
1861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
1871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#else
1891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
190bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy#define dbg_check_name(c, dent, nm) 0
1911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#endif
1931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
1951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				   struct nameidata *nd)
1961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
1971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err;
1981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_key key;
1991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = NULL;
2001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_dent_node *dent;
2011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
2021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("'%.*s' in dir ino %lu",
2041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.len, dentry->d_name.name, dir->i_ino);
2051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (dentry->d_name.len > UBIFS_MAX_NLEN)
2071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return ERR_PTR(-ENAMETOOLONG);
2081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
2101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!dent)
2111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return ERR_PTR(-ENOMEM);
2121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
2141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
2161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
217720b499c806200d06f4f22c668d46db784117089Artem Bityutskiy		if (err == -ENOENT) {
2181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			dbg_gen("not found");
2191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto done;
2201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
2211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out;
2221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
224bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy	if (dbg_check_name(c, dent, &dentry->d_name)) {
2251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = -EINVAL;
2261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out;
2271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
2301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
2311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
2321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * This should not happen. Probably the file-system needs
2331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * checking.
2341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
2351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
2361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_err("dead directory entry '%.*s', error %d",
2371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			  dentry->d_name.len, dentry->d_name.name, err);
2381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_ro_mode(c, err);
2391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out;
2401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiydone:
2431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(dent);
2441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
2451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Note, d_splice_alias() would be required instead if we supported
2461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * NFS.
2471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
2481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_add(dentry, inode);
2491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return NULL;
2501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout:
2521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(dent);
2531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return ERR_PTR(err);
2541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
2551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_create(struct inode *dir, struct dentry *dentry, int mode,
2571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			struct nameidata *nd)
2581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
2591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
2601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
2611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
2621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
2631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy					.dirtied_ino = 1 };
2641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
2651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
2671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry, changing the
2681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * parent directory inode.
2691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
2701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
2721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
2731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
2751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
2761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
2771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, mode);
2791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
2801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
2811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
2821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
2851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
2861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
2871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
2881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
2891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
2901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
2911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
2921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
2941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
2951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
2961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
2971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
2991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
3001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
3011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
3021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
3031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
3041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
3051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
3061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_err("cannot create regular file, error %d", err);
3071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
3081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
3091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
3111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * vfs_dent_type - get VFS directory entry type.
3121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @type: UBIFS directory entry type
3131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
3141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This function converts UBIFS directory entry type into VFS directory entry
3151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * type.
3161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
3171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic unsigned int vfs_dent_type(uint8_t type)
3181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
3191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	switch (type) {
3201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_REG:
3211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_REG;
3221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_DIR:
3231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_DIR;
3241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_LNK:
3251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_LNK;
3261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_BLK:
3271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_BLK;
3281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_CHR:
3291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_CHR;
3301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_FIFO:
3311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_FIFO;
3321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_SOCK:
3331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_SOCK;
3341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	default:
3351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		BUG();
3361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
3371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
3381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
3391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/*
3411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * The classical Unix view for directory is that it is a linear array of
3421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * (name, inode number) entries. Linux/VFS assumes this model as well.
3431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * Particularly, 'readdir()' call wants us to return a directory entry offset
3441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * which later may be used to continue 'readdir()'ing the directory or to
3451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
3461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * model because directory entries are identified by keys, which may collide.
3471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
3481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * UBIFS uses directory entry hash value for directory offsets, so
3491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * 'seekdir()'/'telldir()' may not always work because of possible key
3501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
3511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * properly by means of saving full directory entry name in the private field
3521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * of the file description object.
3531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
3541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This means that UBIFS cannot support NFS which requires full
3551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * 'seekdir()'/'telldir()' support.
3561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
3571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_readdir(struct file *file, void *dirent, filldir_t filldir)
3581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
3591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, over = 0;
3601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct qstr nm;
3611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_key key;
3621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_dent_node *dent;
3631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *dir = file->f_path.dentry->d_inode;
3641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
3651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
3671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
3691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
3701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * The directory was seek'ed to a senseless position or there
3711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * are no more entries.
3721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
3731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return 0;
3741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/* File positions 0 and 1 correspond to "." and ".." */
3761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (file->f_pos == 0) {
3771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_assert(!file->private_data);
3781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		over = filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR);
3791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (over)
3801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return 0;
3811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->f_pos = 1;
3821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
3831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (file->f_pos == 1) {
3851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_assert(!file->private_data);
3861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		over = filldir(dirent, "..", 2, 1,
3871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			       parent_ino(file->f_path.dentry), DT_DIR);
3881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (over)
3891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return 0;
3901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* Find the first entry in TNC and save it */
3921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		lowest_dent_key(c, &key, dir->i_ino);
3931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.name = NULL;
3941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent = ubifs_tnc_next_ent(c, &key, &nm);
3951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (IS_ERR(dent)) {
3961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = PTR_ERR(dent);
3971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto out;
3981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
3991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->f_pos = key_hash_flash(c, &dent->key);
4011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->private_data = dent;
4021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent = file->private_data;
4051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!dent) {
4061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
4071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * The directory was seek'ed to and is now readdir'ed.
4081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * Find the entry corresponding to @file->f_pos or the
4091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * closest one.
4101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
4111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
4121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.name = NULL;
4131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent = ubifs_tnc_next_ent(c, &key, &nm);
4141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (IS_ERR(dent)) {
4151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = PTR_ERR(dent);
4161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto out;
4171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
4181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->f_pos = key_hash_flash(c, &dent->key);
4191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->private_data = dent;
4201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	while (1) {
4231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dbg_gen("feed '%s', ino %llu, new f_pos %#x",
4247424bac82ff3bd956ea04101550e01bdae17284dAlexander Beregalov			dent->name, (unsigned long long)le64_to_cpu(dent->inum),
4251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			key_hash_flash(c, &dent->key));
4260ecb9529a4d47825778e7b0d226eb36019252a9dHarvey Harrison		ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
4270ecb9529a4d47825778e7b0d226eb36019252a9dHarvey Harrison			     ubifs_inode(dir)->creat_sqnum);
4281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.len = le16_to_cpu(dent->nlen);
4301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		over = filldir(dirent, dent->name, nm.len, file->f_pos,
4311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			       le64_to_cpu(dent->inum),
4321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			       vfs_dent_type(dent->type));
4331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (over)
4341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return 0;
4351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* Switch to the next entry */
4371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		key_read(c, &dent->key, &key);
4381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.name = dent->name;
4391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent = ubifs_tnc_next_ent(c, &key, &nm);
4401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (IS_ERR(dent)) {
4411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = PTR_ERR(dent);
4421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto out;
4431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
4441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(file->private_data);
4461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->f_pos = key_hash_flash(c, &dent->key);
4471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->private_data = dent;
4481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		cond_resched();
4491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout:
4521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err != -ENOENT) {
4531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_err("cannot find next direntry, error %d", err);
4541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
4551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(file->private_data);
4581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	file->private_data = NULL;
4591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	file->f_pos = 2;
4601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
4611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/* If a directory is seeked, we have to free saved readdir() state */
4641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic loff_t ubifs_dir_llseek(struct file *file, loff_t offset, int origin)
4651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
4661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(file->private_data);
4671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	file->private_data = NULL;
4681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return generic_file_llseek(file, offset, origin);
4691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/* Free saved readdir() state when the directory is closed */
4721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_dir_release(struct inode *dir, struct file *file)
4731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
4741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(file->private_data);
4751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	file->private_data = NULL;
4761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
4771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
48082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * lock_2_inodes - a wrapper for locking two UBIFS inodes.
4811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
4821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
48382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy *
48482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * We do not implement any tricks to guarantee strict lock ordering, because
48582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * VFS has already done it for us on the @i_mutex. So this is just a simple
48682c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * wrapper function.
4871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
4881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void lock_2_inodes(struct inode *inode1, struct inode *inode2)
4891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
49082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
49182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
4921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
49582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
4961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
4971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
4981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
4991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
5001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
5011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
50282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
5031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
5041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_link(struct dentry *old_dentry, struct inode *dir,
5061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		      struct dentry *dentry)
5071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
5081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
5091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = old_dentry->d_inode;
5101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui = ubifs_inode(inode);
5111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
5121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
5131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
514dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
5151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
5171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new direntry, changing the target inode,
5181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * changing the parent inode.
5191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
5201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu",
5221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.len, dentry->d_name.name, inode->i_ino,
5231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_nlink, dir->i_ino);
52482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&dir->i_mutex));
52582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&inode->i_mutex));
5268b3884a841f398f6e0a0411d6929d8d9381bb265Hunter Adrian
527d808efb407e1a2cf83a8d21411157195f26bdef9Artem Bityutskiy	err = dbg_check_synced_i_size(c, inode);
5281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
5301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
5321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
5341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_2_inodes(dir, inode);
5361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
5377de9c6ee3ecffd99e1628e81a5ea5468f7581a1fAl Viro	ihold(inode);
5381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ctime = ubifs_current_time(inode);
5391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
5401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
5411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
5421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
5431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
5451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
5461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
5481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
5491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
5501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
5521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
5531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
5541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(inode);
5551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
5561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
5571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
5581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
5591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
5601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_unlink(struct inode *dir, struct dentry *dentry)
5621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
5631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
5641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = dentry->d_inode;
5651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
5661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
5671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, budgeted = 1;
5681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
5691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
5711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: deletion direntry, deletion inode (+1 for
5721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * @dirtied_ino), changing the parent directory inode. If budgeting
5731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * fails, go ahead anyway because we have extra space reserved for
5741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * deletions.
5751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
5761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu",
5781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.len, dentry->d_name.name, inode->i_ino,
5791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_nlink, dir->i_ino);
58082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&dir->i_mutex));
58182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&inode->i_mutex));
582d808efb407e1a2cf83a8d21411157195f26bdef9Artem Bityutskiy	err = dbg_check_synced_i_size(c, inode);
5831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
5851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
5871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
5881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err != -ENOSPC)
5891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return err;
5901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		budgeted = 0;
5911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
5921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_2_inodes(dir, inode);
5941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ctime = ubifs_current_time(dir);
5951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(inode);
5961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
5971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
5981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
5991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
6001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
6011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
6021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
6031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
6051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
6061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	else {
6071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* We've deleted something - clean the "no space" flags */
608b137545c44fc0c80fb778abb0c582bda5601e8f8Artem Bityutskiy		c->bi.nospace = c->bi.nospace_rp = 0;
6091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		smp_wmb();
6101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
6121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
6141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
6151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
6161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
6171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
6181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
6191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
6201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
6211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
6221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
6241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * check_dir_empty - check if a directory is empty or not.
6251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @c: UBIFS file-system description object
6261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @dir: VFS inode object of the directory to check
6271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
6281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This function checks if directory @dir is empty. Returns zero if the
6291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
6301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * in case of of errors.
6311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
6321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int check_dir_empty(struct ubifs_info *c, struct inode *dir)
6331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
6341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct qstr nm = { .name = NULL };
6351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_dent_node *dent;
6361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_key key;
6371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err;
6381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lowest_dent_key(c, &key, dir->i_ino);
6401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent = ubifs_tnc_next_ent(c, &key, &nm);
6411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(dent)) {
6421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(dent);
6431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err == -ENOENT)
6441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = 0;
6451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else {
6461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(dent);
6471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = -ENOTEMPTY;
6481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
6501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
6511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
6531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
6541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
6551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = dentry->d_inode;
6561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
6571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, budgeted = 1;
6581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
6591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
6601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
6621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: deletion direntry, deletion inode and
6631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * changing the parent inode. If budgeting fails, go ahead anyway
6641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * because we have extra space reserved for deletions.
6651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
6661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len,
6681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.name, inode->i_ino, dir->i_ino);
66982c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&dir->i_mutex));
67082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&inode->i_mutex));
6711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = check_dir_empty(c, dentry->d_inode);
6721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
6731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
6741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
6761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
6771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err != -ENOSPC)
6781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return err;
6791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		budgeted = 0;
6801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_2_inodes(dir, inode);
6831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ctime = ubifs_current_time(dir);
6841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	clear_nlink(inode);
6851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(dir);
6861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
6871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
6881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
6891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
6901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
6911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
6921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
6931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
6951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
6961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	else {
6971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* We've deleted something - clean the "no space" flags */
698b137545c44fc0c80fb778abb0c582bda5601e8f8Artem Bityutskiy		c->bi.nospace = c->bi.nospace_rp = 0;
6991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		smp_wmb();
7001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
7021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
7041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
7051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
7061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(dir);
7071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
7081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
7091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
7101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
7111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
7121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
7131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
7141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
7161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
7171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
7181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
7191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
7201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
721182854b46f9feb6f1b03abe747bb2beeebf2adb0Artem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
7221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
7241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry and changing parent
7251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * directory inode.
7261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
7271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
7291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
7301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
7321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
7331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
7341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
7361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
7371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
7381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
7391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
7421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
7431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
7441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(dir);
7451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
7461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
7471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
7481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
7491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
7501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_err("cannot create directory, error %d", err);
7511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
7521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
7541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
7561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
7571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
7581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
7601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
7611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
7621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(dir);
7631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
7641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
7651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
7661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
7671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
7681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
7691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
7701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_mknod(struct inode *dir, struct dentry *dentry,
7721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		       int mode, dev_t rdev)
7731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
7741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
7751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui;
7761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
7771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
7781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_dev_desc *dev = NULL;
7791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
7801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, devlen = 0;
7811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
782dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.new_ino_d = ALIGN(devlen, 8),
783dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.dirtied_ino = 1 };
7841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
7861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry and changing parent
7871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * directory inode.
7881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
7891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s' in dir ino %lu",
7911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.len, dentry->d_name.name, dir->i_ino);
7921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!new_valid_dev(rdev))
7941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -EINVAL;
7951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (S_ISBLK(mode) || S_ISCHR(mode)) {
7971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
7981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (!dev)
7991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return -ENOMEM;
8001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		devlen = ubifs_encode_dev(dev, rdev);
8011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
8041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
8051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(dev);
8061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
8071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, mode);
8101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
8111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(dev);
8121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
8131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
8141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	init_special_inode(inode, inode->i_mode, rdev);
8171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_size = ubifs_inode(inode)->ui_size = devlen;
8181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui = ubifs_inode(inode);
8191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data = dev;
8201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data_len = devlen;
8211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
8231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
8241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
8251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
8261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
8271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
8281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
8291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
8301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
8321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
8331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
8341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
8351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
8371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
8381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
8391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
8401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
8411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
8421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
8431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
8441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
8451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
8461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_symlink(struct inode *dir, struct dentry *dentry,
8481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 const char *symname)
8491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
8501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
8511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui;
8521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
8531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
8541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, len = strlen(symname);
8551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
8561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
857dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.new_ino_d = ALIGN(len, 8),
858dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.dirtied_ino = 1 };
8591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
8611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry and changing parent
8621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * directory inode.
8631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
8641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s', target '%s' in dir ino %lu", dentry->d_name.len,
8661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dentry->d_name.name, symname, dir->i_ino);
8671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (len > UBIFS_MAX_INO_DATA)
8691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -ENAMETOOLONG;
8701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
8721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
8731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
8741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
8761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
8771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
8781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
8791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui = ubifs_inode(inode);
8821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data = kmalloc(len + 1, GFP_NOFS);
8831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!ui->data) {
8841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = -ENOMEM;
8851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_inode;
8861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	memcpy(ui->data, symname, len);
8891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	((char *)ui->data)[len] = '\0';
8901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
8911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * The terminating zero byte is not written to the flash media and it
8921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * is put just to make later in-memory string processing simpler. Thus,
8931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * data length is @len, not @len + %1.
8941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
8951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data_len = len;
8961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_size = ubifs_inode(inode)->ui_size = len;
8971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
8991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
9001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
9011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
9021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
9031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
9041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
9051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
9061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
9081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
9091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
9101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
9111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
9131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
9141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
9151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
9161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_inode:
9171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
9181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
9191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
9201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
9211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
9221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
9231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
92582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * lock_3_inodes - a wrapper for locking three UBIFS inodes.
9261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
9271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
9281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode3: third inode
9291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
93082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * This function is used for 'ubifs_rename()' and @inode1 may be the same as
93182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * @inode2 whereas @inode3 may be %NULL.
93282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy *
93382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * We do not implement any tricks to guarantee strict lock ordering, because
93482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * VFS has already done it for us on the @i_mutex. So this is just a simple
93582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * wrapper function.
9361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
9371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void lock_3_inodes(struct inode *inode1, struct inode *inode2,
9381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			  struct inode *inode3)
9391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
94082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
94182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (inode2 != inode1)
94282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
94382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (inode3)
94482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
9451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
9461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
94882c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename.
9491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
9501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
9511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode3: third inode
9521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
9531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void unlock_3_inodes(struct inode *inode1, struct inode *inode2,
9541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			    struct inode *inode3)
9551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
9561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (inode3)
9571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
95882c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (inode1 != inode2)
95982c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
96082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
9611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
9621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
9641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			struct inode *new_dir, struct dentry *new_dentry)
9651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
9661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
9671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *old_inode = old_dentry->d_inode;
9681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *new_inode = new_dentry->d_inode;
9691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
9701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, release, sync = 0, move = (new_dir != old_dir);
9711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int is_dir = S_ISDIR(old_inode->i_mode);
9721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int unlink = !!new_inode;
9731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
9741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
9751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
9761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy					.dirtied_ino = 3 };
9771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
978dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy			.dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
9791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct timespec time;
9801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
9821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: deletion direntry, new direntry, removing
9831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * the old inode, and changing old and new parent directory inodes.
9841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 *
9851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * However, this operation also marks the target inode as dirty and
9861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * does not write it, so we allocate budget for the target inode
9871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * separately.
9881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
9891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dbg_gen("dent '%.*s' ino %lu in dir ino %lu to dent '%.*s' in "
9911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		"dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name,
9921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len,
9931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_dentry->d_name.name, new_dir->i_ino);
99482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
99582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
99682c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (unlink)
99782c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
99882c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy
9991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (unlink && is_dir) {
10011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = check_dir_empty(c, new_inode);
10021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err)
10031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return err;
10041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
10071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
10081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
10091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &ino_req);
10101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
10111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
10121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
10131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_3_inodes(old_dir, new_dir, new_inode);
10161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
10181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Like most other Unix systems, set the @i_ctime for inodes on a
10191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * rename.
10201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	time = ubifs_current_time(old_dir);
10221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_inode->i_ctime = time;
10231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/* We must adjust parent link count when renaming directories */
10251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (is_dir) {
10261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (move) {
10271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			/*
10281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @old_dir loses a link because we are moving
10291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @old_inode to a different directory.
10301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 */
10311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			drop_nlink(old_dir);
10321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			/*
10331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @new_dir only gains a link if we are not also
10341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * overwriting an existing directory.
10351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 */
10361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (!unlink)
10371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				inc_nlink(new_dir);
10381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		} else {
10391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			/*
10401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @old_inode is not moving to a different directory,
10411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * but @old_dir still loses a link if we are
10421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * overwriting an existing directory.
10431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 */
10441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (unlink)
10451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				drop_nlink(old_dir);
10461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
10471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_dir->i_size -= old_sz;
10501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
10511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_dir->i_mtime = old_dir->i_ctime = time;
10521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	new_dir->i_mtime = new_dir->i_ctime = time;
10531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
10551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * And finally, if we unlinked a direntry which happened to have the
10561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * same name as the moved direntry, we have to decrement @i_nlink of
10571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * the unlinked inode and change its ctime.
10581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (unlink) {
10601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
10611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * Directories cannot have hard-links, so if this is a
10621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * directory, decrement its @i_nlink twice because an empty
10631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * directory has @i_nlink 2.
10641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
10651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (is_dir)
10661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			drop_nlink(new_inode);
10671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_inode->i_ctime = time;
10681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		drop_nlink(new_inode);
10691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else {
10701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_dir->i_size += new_sz;
10711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
10721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
10751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
10761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * is dirty, because this will be done later on at the end of
10771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * 'ubifs_rename()'.
10781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_SYNC(old_inode)) {
10801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
10811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (unlink && IS_SYNC(new_inode))
10821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			sync = 1;
10831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry,
10851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			       sync);
10861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
10871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
10881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_3_inodes(old_dir, new_dir, new_inode);
10901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
10911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&old_inode_ui->ui_mutex);
10931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	release = old_inode_ui->dirty;
10941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mark_inode_dirty_sync(old_inode);
10951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&old_inode_ui->ui_mutex);
10961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (release)
10981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &ino_req);
10991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_SYNC(old_inode))
1100a9185b41a4f84971b930c519f0c63bd450c4810dChristoph Hellwig		err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
11011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
11021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
11041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (unlink) {
11051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (is_dir)
11061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			inc_nlink(new_inode);
11071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inc_nlink(new_inode);
11081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else {
11091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_dir->i_size -= new_sz;
11101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
11111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
11121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_dir->i_size += old_sz;
11131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
11141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (is_dir) {
11151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (move) {
11161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			inc_nlink(old_dir);
11171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (!unlink)
11181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				drop_nlink(new_dir);
11191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		} else {
11201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (unlink)
11211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				inc_nlink(old_dir);
11221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
11231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
11241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_3_inodes(old_dir, new_dir, new_inode);
11251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &ino_req);
11261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
11271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
11281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
11291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyint ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
11311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		  struct kstat *stat)
11321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
11331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	loff_t size;
11341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = dentry->d_inode;
11351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui = ubifs_inode(inode);
11361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&ui->ui_mutex);
11381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->dev = inode->i_sb->s_dev;
11391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->ino = inode->i_ino;
11401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->mode = inode->i_mode;
11411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->nlink = inode->i_nlink;
11421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->uid = inode->i_uid;
11431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->gid = inode->i_gid;
11441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->rdev = inode->i_rdev;
11451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->atime = inode->i_atime;
11461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->mtime = inode->i_mtime;
11471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->ctime = inode->i_ctime;
11481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->blksize = UBIFS_BLOCK_SIZE;
11491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->size = ui->ui_size;
11501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
11521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Unfortunately, the 'stat()' system call was designed for block
11531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * device based file systems, and it is not appropriate for UBIFS,
11541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * because UBIFS does not have notion of "block". For example, it is
11551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * difficult to tell how many block a directory takes - it actually
11561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * takes less than 300 bytes, but we have to round it to block size,
11571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * which introduces large mistake. This makes utilities like 'du' to
11581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * report completely senseless numbers. This is the reason why UBIFS
11591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * goes the same way as JFFS2 - it reports zero blocks for everything
11601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * but regular files, which makes more sense than reporting completely
11611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * wrong sizes.
11621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
11631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (S_ISREG(inode->i_mode)) {
11641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		size = ui->xattr_size;
11651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		size += stat->size;
11661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		size = ALIGN(size, UBIFS_BLOCK_SIZE);
11671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
11681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * Note, user-space expects 512-byte blocks count irrespectively
11691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * of what was reported in @stat->size.
11701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
11711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		stat->blocks = size >> 9;
11721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else
11731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		stat->blocks = 0;
11741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&ui->ui_mutex);
11751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
11761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
11771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1178e8b815663b1bfd9c255af5176604ec0eafdf6ed7Artem Bityutskiyconst struct inode_operations ubifs_dir_inode_operations = {
11791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.lookup      = ubifs_lookup,
11801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.create      = ubifs_create,
11811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.link        = ubifs_link,
11821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.symlink     = ubifs_symlink,
11831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.unlink      = ubifs_unlink,
11841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.mkdir       = ubifs_mkdir,
11851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.rmdir       = ubifs_rmdir,
11861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.mknod       = ubifs_mknod,
11871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.rename      = ubifs_rename,
11881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.setattr     = ubifs_setattr,
11891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.getattr     = ubifs_getattr,
11901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#ifdef CONFIG_UBIFS_FS_XATTR
11911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.setxattr    = ubifs_setxattr,
11921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.getxattr    = ubifs_getxattr,
11931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.listxattr   = ubifs_listxattr,
11941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.removexattr = ubifs_removexattr,
11951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#endif
11961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy};
11971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1198e8b815663b1bfd9c255af5176604ec0eafdf6ed7Artem Bityutskiyconst struct file_operations ubifs_dir_operations = {
11991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.llseek         = ubifs_dir_llseek,
12001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.release        = ubifs_dir_release,
12011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.read           = generic_read_dir,
12021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.readdir        = ubifs_readdir,
12031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.fsync          = ubifs_fsync,
12041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.unlocked_ioctl = ubifs_ioctl,
12051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#ifdef CONFIG_COMPAT
12061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.compat_ioctl   = ubifs_compat_ioctl,
12071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#endif
12081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy};
1209