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 */
59ad44be5c7820f5b8ce97292f4bcb3de73625c35bAl Virostatic int inherit_flags(const struct inode *dir, umode_t 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,
89ad44be5c7820f5b8ce97292f4bcb3de73625c35bAl Viro			      umode_t 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
173bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiystatic int dbg_check_name(const struct ubifs_info *c,
174bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy			  const struct ubifs_dent_node *dent,
175bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy			  const struct qstr *nm)
1761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
1772b1844a8c934723134ee1ff313e51d0d281cdef1Artem Bityutskiy	if (!dbg_is_chk_gen(c))
1781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return 0;
1791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (le16_to_cpu(dent->nlen) != nm->len)
1801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -EINVAL;
1811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (memcmp(dent->name, nm->name, nm->len))
1821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -EINVAL;
1831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
1841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
1851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
18700cd8dd3bf95f2cc8435b4cac01d9995635c6d0bAl Viro				   unsigned int flags)
1881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
1891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err;
1901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_key key;
1911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = NULL;
1921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_dent_node *dent;
1931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
1941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1954cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
1961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (dentry->d_name.len > UBIFS_MAX_NLEN)
1981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return ERR_PTR(-ENAMETOOLONG);
1991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
2011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!dent)
2021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return ERR_PTR(-ENOMEM);
2031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
2051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
2071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
208720b499c806200d06f4f22c668d46db784117089Artem Bityutskiy		if (err == -ENOENT) {
2091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			dbg_gen("not found");
2101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto done;
2111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
2121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out;
2131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
215bb2615d4d14777fd37e2b91bd99b92c0354813d1Artem Bityutskiy	if (dbg_check_name(c, dent, &dentry->d_name)) {
2161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = -EINVAL;
2171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out;
2181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
2211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
2221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
2231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * This should not happen. Probably the file-system needs
2241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * checking.
2251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
2261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
2274cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		ubifs_err("dead directory entry '%pd', error %d",
2284cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro			  dentry, err);
2291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_ro_mode(c, err);
2301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out;
2311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiydone:
2341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(dent);
2351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
2361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Note, d_splice_alias() would be required instead if we supported
2371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * NFS.
2381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
2391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_add(dentry, inode);
2401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return NULL;
2411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout:
2431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(dent);
2441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return ERR_PTR(err);
2451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
2461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2474acdaf27ebe2034c342f3be57ef49aed1ad885efAl Virostatic int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
248ebfc3b49a7ac25920cb5be5445f602e51d2ea559Al Viro			bool excl)
2491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
2501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
2511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
2521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
2531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
2541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy					.dirtied_ino = 1 };
2551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
2561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
2581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry, changing the
2591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * parent directory inode.
2601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
2611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2624cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
2634cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		dentry, mode, dir->i_ino);
2641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
2661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
2671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
2681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, mode);
2701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
2711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
2721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
2731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
2741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
2761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
2771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
2781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
2791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
2801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
2811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
2821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
2831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
2851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
2861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
2871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
2881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
2891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
2901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
2911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
2921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
2931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
2941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
2951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
2961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
2971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_err("cannot create regular file, error %d", err);
2981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
2991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
3001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
3021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * vfs_dent_type - get VFS directory entry type.
3031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @type: UBIFS directory entry type
3041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
3051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This function converts UBIFS directory entry type into VFS directory entry
3061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * type.
3071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
3081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic unsigned int vfs_dent_type(uint8_t type)
3091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
3101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	switch (type) {
3111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_REG:
3121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_REG;
3131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_DIR:
3141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_DIR;
3151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_LNK:
3161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_LNK;
3171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_BLK:
3181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_BLK;
3191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_CHR:
3201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_CHR;
3211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_FIFO:
3221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_FIFO;
3231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	case UBIFS_ITYPE_SOCK:
3241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return DT_SOCK;
3251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	default:
3261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		BUG();
3271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
3281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
3291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
3301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/*
3321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * The classical Unix view for directory is that it is a linear array of
3331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * (name, inode number) entries. Linux/VFS assumes this model as well.
3341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * Particularly, 'readdir()' call wants us to return a directory entry offset
3351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * which later may be used to continue 'readdir()'ing the directory or to
3361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
3371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * model because directory entries are identified by keys, which may collide.
3381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
3391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * UBIFS uses directory entry hash value for directory offsets, so
3401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * 'seekdir()'/'telldir()' may not always work because of possible key
3411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
3421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * properly by means of saving full directory entry name in the private field
3431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * of the file description object.
3441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
3451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This means that UBIFS cannot support NFS which requires full
3461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * 'seekdir()'/'telldir()' support.
3471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
34801122e06885f4aa220e7a7a31ce25e4876d159f2Al Virostatic int ubifs_readdir(struct file *file, struct dir_context *ctx)
3491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
35001122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	int err;
3511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct qstr nm;
3521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_key key;
3531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_dent_node *dent;
354496ad9aa8ef448058e36ca7a787c61f2e63f0f54Al Viro	struct inode *dir = file_inode(file);
3551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
3561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
35701122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
3581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
35901122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
3601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
3611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * The directory was seek'ed to a senseless position or there
3621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * are no more entries.
3631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
3641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return 0;
3651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
366605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	if (file->f_version == 0) {
367605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		/*
368605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		 * The file was seek'ed, which means that @file->private_data
369605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		 * is now invalid. This may also be just the first
370605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		 * 'ubifs_readdir()' invocation, in which case
371605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		 * @file->private_data is NULL, and the below code is
372605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		 * basically a no-op.
373605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		 */
374605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		kfree(file->private_data);
375605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy		file->private_data = NULL;
376605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	}
377605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy
378605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	/*
379605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	 * 'generic_file_llseek()' unconditionally sets @file->f_version to
380605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	 * zero, and we use this for detecting whether the file was seek'ed.
381605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	 */
382605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	file->f_version = 1;
383605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy
3841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/* File positions 0 and 1 correspond to "." and ".." */
38501122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	if (ctx->pos < 2) {
3861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_assert(!file->private_data);
38701122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		if (!dir_emit_dots(file, ctx))
3881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return 0;
3891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
3901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* Find the first entry in TNC and save it */
3911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		lowest_dent_key(c, &key, dir->i_ino);
3921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.name = NULL;
3931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent = ubifs_tnc_next_ent(c, &key, &nm);
3941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (IS_ERR(dent)) {
3951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = PTR_ERR(dent);
3961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto out;
3971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
3981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
39901122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		ctx->pos = key_hash_flash(c, &dent->key);
4001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->private_data = dent;
4011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent = file->private_data;
4041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!dent) {
4051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
4061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * The directory was seek'ed to and is now readdir'ed.
40701122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		 * Find the entry corresponding to @ctx->pos or the closest one.
4081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
40901122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
4101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.name = NULL;
4111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent = ubifs_tnc_next_ent(c, &key, &nm);
4121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (IS_ERR(dent)) {
4131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = PTR_ERR(dent);
4141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto out;
4151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
41601122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		ctx->pos = key_hash_flash(c, &dent->key);
4171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->private_data = dent;
4181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	while (1) {
4211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dbg_gen("feed '%s', ino %llu, new f_pos %#x",
4227424bac82ff3bd956ea04101550e01bdae17284dAlexander Beregalov			dent->name, (unsigned long long)le64_to_cpu(dent->inum),
4231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			key_hash_flash(c, &dent->key));
4240ecb9529a4d47825778e7b0d226eb36019252a9dHarvey Harrison		ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
4250ecb9529a4d47825778e7b0d226eb36019252a9dHarvey Harrison			     ubifs_inode(dir)->creat_sqnum);
4261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.len = le16_to_cpu(dent->nlen);
42801122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		if (!dir_emit(ctx, dent->name, nm.len,
4291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			       le64_to_cpu(dent->inum),
43001122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro			       vfs_dent_type(dent->type)))
4311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return 0;
4321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* Switch to the next entry */
4341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		key_read(c, &dent->key, &key);
4351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		nm.name = dent->name;
4361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dent = ubifs_tnc_next_ent(c, &key, &nm);
4371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (IS_ERR(dent)) {
4381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = PTR_ERR(dent);
4391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			goto out;
4401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
4411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(file->private_data);
44301122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro		ctx->pos = key_hash_flash(c, &dent->key);
4441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		file->private_data = dent;
4451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		cond_resched();
4461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout:
4491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err != -ENOENT) {
4501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_err("cannot find next direntry, error %d", err);
4511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
4521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
4531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(file->private_data);
4551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	file->private_data = NULL;
456605c912bb843c024b1ed173dc427cd5c08e5d54dArtem Bityutskiy	/* 2 is a special value indicating that there are no more direntries */
45701122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	ctx->pos = 2;
4581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
4591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/* Free saved readdir() state when the directory is closed */
4621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_dir_release(struct inode *dir, struct file *file)
4631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
4641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	kfree(file->private_data);
4651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	file->private_data = NULL;
4661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
4671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
47082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * lock_2_inodes - a wrapper for locking two UBIFS inodes.
4711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
4721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
47382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy *
47482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * We do not implement any tricks to guarantee strict lock ordering, because
47582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * VFS has already done it for us on the @i_mutex. So this is just a simple
47682c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * wrapper function.
4771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
4781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void lock_2_inodes(struct inode *inode1, struct inode *inode2)
4791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
48082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
48182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
4821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
48582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
4861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
4871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
4881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
4891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
4901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
4911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
49282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
4931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
4941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
4951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_link(struct dentry *old_dentry, struct inode *dir,
4961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		      struct dentry *dentry)
4971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
4981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
4991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = old_dentry->d_inode;
5001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui = ubifs_inode(inode);
5011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
5021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
5031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
504dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
5051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
5071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new direntry, changing the target inode,
5081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * changing the parent inode.
5091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
5101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5114cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
5124cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		dentry, inode->i_ino,
5131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_nlink, dir->i_ino);
51482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&dir->i_mutex));
51582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&inode->i_mutex));
5168b3884a841f398f6e0a0411d6929d8d9381bb265Hunter Adrian
517d808efb407e1a2cf83a8d21411157195f26bdef9Artem Bityutskiy	err = dbg_check_synced_i_size(c, inode);
5181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
5201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
5221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
5241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_2_inodes(dir, inode);
5261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
5277de9c6ee3ecffd99e1628e81a5ea5468f7581a1fAl Viro	ihold(inode);
5281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ctime = ubifs_current_time(inode);
5291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
5301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
5311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
5321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
5331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
5351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
5361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
5381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
5391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
5401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
5421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
5431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
5441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(inode);
5451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
5461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
5471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
5481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
5491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
5501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_unlink(struct inode *dir, struct dentry *dentry)
5521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
5531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
5541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = dentry->d_inode;
5551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
5561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
5571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, budgeted = 1;
5581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
559c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy	unsigned int saved_nlink = inode->i_nlink;
5601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
5621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: deletion direntry, deletion inode (+1 for
5631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * @dirtied_ino), changing the parent directory inode. If budgeting
5641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * fails, go ahead anyway because we have extra space reserved for
5651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * deletions.
5661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
5671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5684cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
5694cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		dentry, inode->i_ino,
5701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		inode->i_nlink, dir->i_ino);
57182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&dir->i_mutex));
57282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&inode->i_mutex));
573d808efb407e1a2cf83a8d21411157195f26bdef9Artem Bityutskiy	err = dbg_check_synced_i_size(c, inode);
5741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
5761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
5781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
5791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err != -ENOSPC)
5801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return err;
5811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		budgeted = 0;
5821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
5831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_2_inodes(dir, inode);
5851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ctime = ubifs_current_time(dir);
5861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(inode);
5871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
5881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
5891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
5901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
5911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
5921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
5931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
5941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
5951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
5961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
5971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	else {
5981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* We've deleted something - clean the "no space" flags */
599b137545c44fc0c80fb778abb0c582bda5601e8f8Artem Bityutskiy		c->bi.nospace = c->bi.nospace_rp = 0;
6001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		smp_wmb();
6011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
6031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
6051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
6061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
607c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy	set_nlink(inode, saved_nlink);
6081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
6091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
6101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
6111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
6121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
6131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
6151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * check_dir_empty - check if a directory is empty or not.
6161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @c: UBIFS file-system description object
6171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @dir: VFS inode object of the directory to check
6181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
6191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * This function checks if directory @dir is empty. Returns zero if the
6201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
6211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * in case of of errors.
6221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
6231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int check_dir_empty(struct ubifs_info *c, struct inode *dir)
6241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
6251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct qstr nm = { .name = NULL };
6261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_dent_node *dent;
6271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_key key;
6281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err;
6291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lowest_dent_key(c, &key, dir->i_ino);
6311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dent = ubifs_tnc_next_ent(c, &key, &nm);
6321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(dent)) {
6331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(dent);
6341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err == -ENOENT)
6351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			err = 0;
6361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else {
6371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(dent);
6381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = -ENOTEMPTY;
6391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
6411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
6421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
6441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
6451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
6461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = dentry->d_inode;
6471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
6481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, budgeted = 1;
6491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
6501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
6511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
6531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: deletion direntry, deletion inode and
6541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * changing the parent inode. If budgeting fails, go ahead anyway
6551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * because we have extra space reserved for deletions.
6561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
6571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6584cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
6594cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		inode->i_ino, dir->i_ino);
66082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&dir->i_mutex));
66182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&inode->i_mutex));
6621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = check_dir_empty(c, dentry->d_inode);
6631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
6641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
6651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
6671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
6681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err != -ENOSPC)
6691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return err;
6701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		budgeted = 0;
6711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_2_inodes(dir, inode);
6741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_ctime = ubifs_current_time(dir);
6751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	clear_nlink(inode);
6761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(dir);
6771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
6781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
6791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
6801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
6811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
6821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
6831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
6841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
6861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
6871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	else {
6881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/* We've deleted something - clean the "no space" flags */
689b137545c44fc0c80fb778abb0c582bda5601e8f8Artem Bityutskiy		c->bi.nospace = c->bi.nospace_rp = 0;
6901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		smp_wmb();
6911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
6921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
6931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
6941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
6951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
6961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
6971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(dir);
698c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy	set_nlink(inode, 2);
6991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_2_inodes(dir, inode);
7001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (budgeted)
7011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
7021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
7031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
7041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
70518bb1db3e7607e4a997d50991a6f9fa5b0f8722cAl Virostatic int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
7061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
7071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
7081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
7091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
7101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
711182854b46f9feb6f1b03abe747bb2beeebf2adb0Artem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
7121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
7141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry and changing parent
7151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * directory inode.
7161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
7171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7184cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
7194cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		dentry, mode, dir->i_ino);
7201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
7221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
7231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
7241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
7261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
7271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
7281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
7291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
7321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
7331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(inode);
7341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inc_nlink(dir);
7351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
7361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
7371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
7381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
7391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
7401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_err("cannot create directory, error %d", err);
7411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
7421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
7441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
7461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
7471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
7481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
7501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
7511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
7521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	drop_nlink(dir);
7531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
7541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
7551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
7561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
7571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
7581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
7591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
7601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_mknod(struct inode *dir, struct dentry *dentry,
7621a67aafb5f72a436ca044293309fa7e6351d6a35Al Viro		       umode_t mode, dev_t rdev)
7631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
7641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
7651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui;
7661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
7671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
7681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	union ubifs_dev_desc *dev = NULL;
7691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
7701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, devlen = 0;
7711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
772dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.new_ino_d = ALIGN(devlen, 8),
773dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.dirtied_ino = 1 };
7741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
7761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry and changing parent
7771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * directory inode.
7781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
7791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7804cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
7811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!new_valid_dev(rdev))
7831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -EINVAL;
7841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (S_ISBLK(mode) || S_ISCHR(mode)) {
7861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
7871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (!dev)
7881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return -ENOMEM;
7891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		devlen = ubifs_encode_dev(dev, rdev);
7901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
7931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
7941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(dev);
7951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
7961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
7971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
7981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, mode);
7991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
8001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		kfree(dev);
8011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
8021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
8031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	init_special_inode(inode, inode->i_mode, rdev);
8061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_size = ubifs_inode(inode)->ui_size = devlen;
8071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui = ubifs_inode(inode);
8081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data = dev;
8091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data_len = devlen;
8101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
8121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
8131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
8141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
8151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
8161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
8171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
8181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
8191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
8211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
8221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
8231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
8241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
8261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
8271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
8281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
8291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
8301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
8311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
8321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
8331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
8341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
8351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_symlink(struct inode *dir, struct dentry *dentry,
8371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 const char *symname)
8381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
8391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode;
8401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui;
8411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *dir_ui = ubifs_inode(dir);
8421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = dir->i_sb->s_fs_info;
8431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, len = strlen(symname);
8441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
8451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
846dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.new_ino_d = ALIGN(len, 8),
847dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy					.dirtied_ino = 1 };
8481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
8501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: new inode, new direntry and changing parent
8511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * directory inode.
8521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
8531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8544cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
8554cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		symname, dir->i_ino);
8561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (len > UBIFS_MAX_INO_DATA)
8581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return -ENAMETOOLONG;
8591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
8611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
8621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
8631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
8651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_ERR(inode)) {
8661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = PTR_ERR(inode);
8671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_budg;
8681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui = ubifs_inode(inode);
8711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data = kmalloc(len + 1, GFP_NOFS);
8721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (!ui->data) {
8731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = -ENOMEM;
8741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_inode;
8751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
8761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	memcpy(ui->data, symname, len);
8781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	((char *)ui->data)[len] = '\0';
8791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
8801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * The terminating zero byte is not written to the flash media and it
8811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * is put just to make later in-memory string processing simpler. Thus,
8821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * data length is @len, not @len + %1.
8831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
8841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ui->data_len = len;
8851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	inode->i_size = ubifs_inode(inode)->ui_size = len;
8861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&dir_ui->ui_mutex);
8881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size += sz_change;
8891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
8901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_mtime = dir->i_ctime = inode->i_ctime;
8911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
8921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
8931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
8941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
8951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
8961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
8971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	insert_inode_hash(inode);
8981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	d_instantiate(dentry, inode);
8991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
9001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
9021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir->i_size -= sz_change;
9031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	dir_ui->ui_size = dir->i_size;
9041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&dir_ui->ui_mutex);
9051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_inode:
9061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	make_bad_inode(inode);
9071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	iput(inode);
9081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_budg:
9091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
9101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
9111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
9121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
91482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * lock_3_inodes - a wrapper for locking three UBIFS inodes.
9151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
9161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
9171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode3: third inode
9181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy *
91982c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * This function is used for 'ubifs_rename()' and @inode1 may be the same as
92082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * @inode2 whereas @inode3 may be %NULL.
92182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy *
92282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * We do not implement any tricks to guarantee strict lock ordering, because
92382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * VFS has already done it for us on the @i_mutex. So this is just a simple
92482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * wrapper function.
9251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
9261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void lock_3_inodes(struct inode *inode1, struct inode *inode2,
9271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			  struct inode *inode3)
9281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
92982c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
93082c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (inode2 != inode1)
93182c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
93282c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (inode3)
93382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
9341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
9351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy/**
93782c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename.
9381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode1: first inode
9391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode2: second inode
9401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy * @inode3: third inode
9411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy */
9421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic void unlock_3_inodes(struct inode *inode1, struct inode *inode2,
9431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			    struct inode *inode3)
9441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
9451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (inode3)
9461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
94782c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (inode1 != inode2)
94882c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
94982c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
9501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
9511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiystatic int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
9531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			struct inode *new_dir, struct dentry *new_dentry)
9541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
9551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
9561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *old_inode = old_dentry->d_inode;
9571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *new_inode = new_dentry->d_inode;
9581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
9591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int err, release, sync = 0, move = (new_dir != old_dir);
9601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int is_dir = S_ISDIR(old_inode->i_mode);
9611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int unlink = !!new_inode;
9621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
9631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
9641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
9651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy					.dirtied_ino = 3 };
9661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
967dab4b4d2f915a65022343012a795f4ae4ae7e83cArtem Bityutskiy			.dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
9681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct timespec time;
969782759b9f5f5223e0962af60c3457c912fab755fAlexandre Pereira da Silva	unsigned int uninitialized_var(saved_nlink);
9701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
9721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Budget request settings: deletion direntry, new direntry, removing
9731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * the old inode, and changing old and new parent directory inodes.
9741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 *
9751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * However, this operation also marks the target inode as dirty and
9761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * does not write it, so we allocate budget for the target inode
9771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * separately.
9781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
9791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9804cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro	dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu",
9814cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		old_dentry, old_inode->i_ino, old_dir->i_ino,
9824cb2a01d8c4bd18a0bb292f32e9876b0c4461e12Al Viro		new_dentry, new_dir->i_ino);
98382c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
98482c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
98582c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy	if (unlink)
98682c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy		ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
98782c1593cad3dfc97661764c8bc62aa1a416e9ea8Artem Bityutskiy
9881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (unlink && is_dir) {
9901e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		err = check_dir_empty(c, new_inode);
9911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (err)
9921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			return err;
9931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
9941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
9951e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &req);
9961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
9971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
9981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_budget_space(c, &ino_req);
9991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err) {
10001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &req);
10011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		return err;
10021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	lock_3_inodes(old_dir, new_dir, new_inode);
10051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
10071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Like most other Unix systems, set the @i_ctime for inodes on a
10081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * rename.
10091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	time = ubifs_current_time(old_dir);
10111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_inode->i_ctime = time;
10121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/* We must adjust parent link count when renaming directories */
10141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (is_dir) {
10151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (move) {
10161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			/*
10171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @old_dir loses a link because we are moving
10181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @old_inode to a different directory.
10191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 */
10201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			drop_nlink(old_dir);
10211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			/*
10221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @new_dir only gains a link if we are not also
10231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * overwriting an existing directory.
10241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 */
10251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (!unlink)
10261e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				inc_nlink(new_dir);
10271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		} else {
10281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			/*
10291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * @old_inode is not moving to a different directory,
10301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * but @old_dir still loses a link if we are
10311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 * overwriting an existing directory.
10321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			 */
10331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (unlink)
10341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				drop_nlink(old_dir);
10351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
10361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_dir->i_size -= old_sz;
10391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
10401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_dir->i_mtime = old_dir->i_ctime = time;
10411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	new_dir->i_mtime = new_dir->i_ctime = time;
10421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
10441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * And finally, if we unlinked a direntry which happened to have the
10451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * same name as the moved direntry, we have to decrement @i_nlink of
10461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * the unlinked inode and change its ctime.
10471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (unlink) {
10491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
10501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * Directories cannot have hard-links, so if this is a
1051c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy		 * directory, just clear @i_nlink.
10521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
1053c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy		saved_nlink = new_inode->i_nlink;
10541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (is_dir)
1055c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy			clear_nlink(new_inode);
1056c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy		else
10571e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			drop_nlink(new_inode);
10581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_inode->i_ctime = time;
10591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else {
10601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_dir->i_size += new_sz;
10611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
10621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
10651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
10661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * is dirty, because this will be done later on at the end of
10671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * 'ubifs_rename()'.
10681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
10691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_SYNC(old_inode)) {
10701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
10711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (unlink && IS_SYNC(new_inode))
10721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			sync = 1;
10731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
10741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry,
10751e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			       sync);
10761e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (err)
10771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		goto out_cancel;
10781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10791e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_3_inodes(old_dir, new_dir, new_inode);
10801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
10811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&old_inode_ui->ui_mutex);
10831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	release = old_inode_ui->dirty;
10841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mark_inode_dirty_sync(old_inode);
10851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&old_inode_ui->ui_mutex);
10861e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10871e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (release)
10881e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_release_budget(c, &ino_req);
10891e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (IS_SYNC(old_inode))
1090a9185b41a4f84971b930c519f0c63bd450c4810dChristoph Hellwig		err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
10911e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
10921e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
10931e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyout_cancel:
10941e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (unlink) {
1095c43be1085f8480ab36d5c8c76a08e67bdf6d2e18Artem Bityutskiy		set_nlink(new_inode, saved_nlink);
10961e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else {
10971e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		new_dir->i_size -= new_sz;
10981e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
10991e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
11001e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	old_dir->i_size += old_sz;
11011e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
11021e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (is_dir) {
11031e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		if (move) {
11041e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			inc_nlink(old_dir);
11051e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (!unlink)
11061e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				drop_nlink(new_dir);
11071e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		} else {
11081e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy			if (unlink)
11091e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy				inc_nlink(old_dir);
11101e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		}
11111e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	}
11121e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	unlock_3_inodes(old_dir, new_dir, new_inode);
11131e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &ino_req);
11141e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	ubifs_release_budget(c, &req);
11151e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return err;
11161e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
11171e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11181e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiyint ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
11191e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		  struct kstat *stat)
11201e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy{
11211e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	loff_t size;
11221e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct inode *inode = dentry->d_inode;
11231e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	struct ubifs_inode *ui = ubifs_inode(inode);
11241e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11251e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_lock(&ui->ui_mutex);
11266d42e7e9f6d86ed4dfacde75a6cf515068f9749cAl Viro	generic_fillattr(inode, stat);
11271e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->blksize = UBIFS_BLOCK_SIZE;
11281e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	stat->size = ui->ui_size;
11291e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
11301e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	/*
11311e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * Unfortunately, the 'stat()' system call was designed for block
11321e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * device based file systems, and it is not appropriate for UBIFS,
11331e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * because UBIFS does not have notion of "block". For example, it is
11341e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * difficult to tell how many block a directory takes - it actually
11351e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * takes less than 300 bytes, but we have to round it to block size,
11361e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * which introduces large mistake. This makes utilities like 'du' to
11371e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * report completely senseless numbers. This is the reason why UBIFS
11381e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * goes the same way as JFFS2 - it reports zero blocks for everything
11391e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * but regular files, which makes more sense than reporting completely
11401e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 * wrong sizes.
11411e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	 */
11421e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	if (S_ISREG(inode->i_mode)) {
11431e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		size = ui->xattr_size;
11441e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		size += stat->size;
11451e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		size = ALIGN(size, UBIFS_BLOCK_SIZE);
11461e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		/*
11471e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * Note, user-space expects 512-byte blocks count irrespectively
11481e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 * of what was reported in @stat->size.
11491e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		 */
11501e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		stat->blocks = size >> 9;
11511e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	} else
11521e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy		stat->blocks = 0;
11531e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	mutex_unlock(&ui->ui_mutex);
11541e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	return 0;
11551e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy}
11561e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1157e8b815663b1bfd9c255af5176604ec0eafdf6ed7Artem Bityutskiyconst struct inode_operations ubifs_dir_inode_operations = {
11581e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.lookup      = ubifs_lookup,
11591e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.create      = ubifs_create,
11601e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.link        = ubifs_link,
11611e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.symlink     = ubifs_symlink,
11621e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.unlink      = ubifs_unlink,
11631e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.mkdir       = ubifs_mkdir,
11641e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.rmdir       = ubifs_rmdir,
11651e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.mknod       = ubifs_mknod,
11661e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.rename      = ubifs_rename,
11671e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.setattr     = ubifs_setattr,
11681e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.getattr     = ubifs_getattr,
11691e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.setxattr    = ubifs_setxattr,
11701e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.getxattr    = ubifs_getxattr,
11711e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.listxattr   = ubifs_listxattr,
11721e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.removexattr = ubifs_removexattr,
11731e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy};
11741e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy
1175e8b815663b1bfd9c255af5176604ec0eafdf6ed7Artem Bityutskiyconst struct file_operations ubifs_dir_operations = {
117601122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	.llseek         = generic_file_llseek,
11771e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.release        = ubifs_dir_release,
11781e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.read           = generic_read_dir,
117901122e06885f4aa220e7a7a31ce25e4876d159f2Al Viro	.iterate        = ubifs_readdir,
11801e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.fsync          = ubifs_fsync,
11811e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.unlocked_ioctl = ubifs_ioctl,
11821e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#ifdef CONFIG_COMPAT
11831e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy	.compat_ioctl   = ubifs_compat_ioctl,
11841e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy#endif
11851e51764a3c2ac05a23a22b2a95ddee4d9bffb16dArtem Bityutskiy};
1186