dir.c 43.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* * This file is part of UBIFS.
 *
 * Copyright (C) 2006-2008 Nokia Corporation.
 * Copyright (C) 2006, 2007 University of Szeged, Hungary
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Authors: Artem Bityutskiy (Битюцкий Артём)
 *          Adrian Hunter
 *          Zoltan Sogor
 */

/*
 * This file implements directory operations.
 *
 * All FS operations in this file allocate budget before writing anything to the
 * media. If they fail to allocate it, the error is returned. The only
 * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
 * if they unable to allocate the budget, because deletion %-ENOSPC failure is
 * not what users are usually ready to get. UBIFS budgeting subsystem has some
 * space reserved for these purposes.
 *
 * All operations in this file write all inodes which they change straight
 * away, instead of marking them dirty. For example, 'ubifs_link()' changes
 * @i_size of the parent inode and writes the parent inode together with the
 * target inode. This was done to simplify file-system recovery which would
 * otherwise be very difficult to do. The only exception is rename which marks
 * the re-named inode dirty (because its @i_ctime is updated) but does not
 * write it, but just marks it as dirty.
 */

#include "ubifs.h"

/**
 * inherit_flags - inherit flags of the parent inode.
 * @dir: parent inode
 * @mode: new inode mode flags
 *
 * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
 * parent directory inode @dir. UBIFS inodes inherit the following flags:
 * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
 *   sub-directory basis;
 * o %UBIFS_SYNC_FL - useful for the same reasons;
 * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
 *
 * This function returns the inherited flags.
 */
A
Al Viro 已提交
59
static int inherit_flags(const struct inode *dir, umode_t mode)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	int flags;
	const struct ubifs_inode *ui = ubifs_inode(dir);

	if (!S_ISDIR(dir->i_mode))
		/*
		 * The parent is not a directory, which means that an extended
		 * attribute inode is being created. No flags.
		 */
		return 0;

	flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
	if (!S_ISDIR(mode))
		/* The "DIRSYNC" flag only applies to directories */
		flags &= ~UBIFS_DIRSYNC_FL;
	return flags;
}

/**
 * ubifs_new_inode - allocate new UBIFS inode object.
 * @c: UBIFS file-system description object
 * @dir: parent directory inode
 * @mode: inode mode flags
 *
 * This function finds an unused inode number, allocates new inode and
 * initializes it. Returns new inode in case of success and an error code in
 * case of failure.
 */
88
struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
A
Al Viro 已提交
89
			      umode_t mode)
90
{
91
	int err;
92 93
	struct inode *inode;
	struct ubifs_inode *ui;
94 95 96 97 98 99 100 101 102 103 104 105 106 107
	bool encrypted = false;

	if (ubifs_crypt_is_encrypted(dir)) {
		err = fscrypt_get_encryption_info(dir);
		if (err) {
			ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
			return ERR_PTR(err);
		}

		if (!fscrypt_has_encryption_key(dir))
			return ERR_PTR(-EPERM);

		encrypted = true;
	}
108 109 110 111 112 113 114 115 116 117 118 119

	inode = new_inode(c->vfs_sb);
	ui = ubifs_inode(inode);
	if (!inode)
		return ERR_PTR(-ENOMEM);

	/*
	 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
	 * marking them dirty in file write path (see 'file_update_time()').
	 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
	 * to make budgeting work.
	 */
120
	inode->i_flags |= S_NOCMTIME;
121

122
	inode_init_owner(inode, dir, mode);
123
	inode->i_mtime = inode->i_atime = inode->i_ctime =
124
			 current_time(inode);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	inode->i_mapping->nrpages = 0;

	switch (mode & S_IFMT) {
	case S_IFREG:
		inode->i_mapping->a_ops = &ubifs_file_address_operations;
		inode->i_op = &ubifs_file_inode_operations;
		inode->i_fop = &ubifs_file_operations;
		break;
	case S_IFDIR:
		inode->i_op  = &ubifs_dir_inode_operations;
		inode->i_fop = &ubifs_dir_operations;
		inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
		break;
	case S_IFLNK:
		inode->i_op = &ubifs_symlink_inode_operations;
		break;
	case S_IFSOCK:
	case S_IFIFO:
	case S_IFBLK:
	case S_IFCHR:
		inode->i_op  = &ubifs_file_inode_operations;
		break;
	default:
		BUG();
	}

	ui->flags = inherit_flags(dir, mode);
	ubifs_set_inode_flags(inode);
	if (S_ISREG(mode))
		ui->compr_type = c->default_compr;
	else
		ui->compr_type = UBIFS_COMPR_NONE;
	ui->synced_i_size = 0;

	spin_lock(&c->cnt_lock);
	/* Inode number overflow is currently not supported */
	if (c->highest_inum >= INUM_WARN_WATERMARK) {
		if (c->highest_inum >= INUM_WATERMARK) {
			spin_unlock(&c->cnt_lock);
164
			ubifs_err(c, "out of inode numbers");
165 166 167 168
			make_bad_inode(inode);
			iput(inode);
			return ERR_PTR(-EINVAL);
		}
169
		ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
A
Artem Bityutskiy 已提交
170
			   (unsigned long)c->highest_inum, INUM_WATERMARK);
171 172 173 174 175 176 177 178 179 180 181 182
	}

	inode->i_ino = ++c->highest_inum;
	/*
	 * The creation sequence number remains with this inode for its
	 * lifetime. All nodes for this inode have a greater sequence number,
	 * and so it is possible to distinguish obsolete nodes belonging to a
	 * previous incarnation of the same inode number - for example, for the
	 * purpose of rebuilding the index.
	 */
	ui->creat_sqnum = ++c->max_sqnum;
	spin_unlock(&c->cnt_lock);
183 184 185 186 187 188 189 190 191 192 193

	if (encrypted) {
		err = fscrypt_inherit_context(dir, inode, &encrypted, true);
		if (err) {
			ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
			make_bad_inode(inode);
			iput(inode);
			return ERR_PTR(err);
		}
	}

194 195 196
	return inode;
}

197 198
static int dbg_check_name(const struct ubifs_info *c,
			  const struct ubifs_dent_node *dent,
199
			  const struct fscrypt_name *nm)
200
{
201
	if (!dbg_is_chk_gen(c))
202
		return 0;
203
	if (le16_to_cpu(dent->nlen) != fname_len(nm))
204
		return -EINVAL;
205
	if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
206 207 208 209 210
		return -EINVAL;
	return 0;
}

static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
211
				   unsigned int flags)
212 213 214 215 216 217
{
	int err;
	union ubifs_key key;
	struct inode *inode = NULL;
	struct ubifs_dent_node *dent;
	struct ubifs_info *c = dir->i_sb->s_fs_info;
218
	struct fscrypt_name nm;
219

A
Al Viro 已提交
220
	dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	if (ubifs_crypt_is_encrypted(dir)) {
		err = fscrypt_get_encryption_info(dir);

		/*
		 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
		 * created while the directory was encrypted and we
		 * have access to the key.
		 */
		if (fscrypt_has_encryption_key(dir))
			fscrypt_set_encrypted_dentry(dentry);
		fscrypt_set_d_op(dentry);
		if (err && err != -ENOKEY)
			return ERR_PTR(err);
	}

237 238 239 240 241 242 243 244
	err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
	if (err)
		return ERR_PTR(err);

	if (fname_len(&nm) > UBIFS_MAX_NLEN) {
		err = -ENAMETOOLONG;
		goto out_fname;
	}
245 246

	dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
247 248 249 250
	if (!dent) {
		err = -ENOMEM;
		goto out_fname;
	}
251

252 253 254 255
	if (nm.hash) {
		ubifs_assert(fname_len(&nm) == 0);
		ubifs_assert(fname_name(&nm) == NULL);
		dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
256
		err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
257 258 259 260
	} else {
		dent_key_init(c, &key, dir->i_ino, &nm);
		err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
	}
261 262

	if (err) {
A
Artem Bityutskiy 已提交
263
		if (err == -ENOENT) {
264 265 266
			dbg_gen("not found");
			goto done;
		}
267
		goto out_dent;
268 269
	}

270
	if (dbg_check_name(c, dent, &nm)) {
271
		err = -EINVAL;
272
		goto out_dent;
273 274 275 276 277 278 279 280 281
	}

	inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
	if (IS_ERR(inode)) {
		/*
		 * This should not happen. Probably the file-system needs
		 * checking.
		 */
		err = PTR_ERR(inode);
282
		ubifs_err(c, "dead directory entry '%pd', error %d",
A
Al Viro 已提交
283
			  dentry, err);
284
		ubifs_ro_mode(c, err);
285
		goto out_dent;
286 287
	}

288 289 290 291 292 293 294 295 296
	if (ubifs_crypt_is_encrypted(dir) &&
	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
	    !fscrypt_has_permitted_context(dir, inode)) {
		ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
			   dir->i_ino, inode->i_ino);
		err = -EPERM;
		goto out_inode;
	}

297 298
done:
	kfree(dent);
299
	fscrypt_free_filename(&nm);
300 301 302 303 304 305 306
	/*
	 * Note, d_splice_alias() would be required instead if we supported
	 * NFS.
	 */
	d_add(dentry, inode);
	return NULL;

307 308
out_inode:
	iput(inode);
309
out_dent:
310
	kfree(dent);
311 312
out_fname:
	fscrypt_free_filename(&nm);
313 314 315
	return ERR_PTR(err);
}

A
Al Viro 已提交
316
static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
317
			bool excl)
318 319 320 321 322 323
{
	struct inode *inode;
	struct ubifs_info *c = dir->i_sb->s_fs_info;
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
					.dirtied_ino = 1 };
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
324 325
	struct fscrypt_name nm;
	int err, sz_change;
326 327 328 329 330 331

	/*
	 * Budget request settings: new inode, new direntry, changing the
	 * parent directory inode.
	 */

A
Al Viro 已提交
332 333
	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
		dentry, mode, dir->i_ino);
334 335 336 337 338

	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

339 340 341 342 343 344
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
	if (err)
		goto out_budg;

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

345 346 347
	inode = ubifs_new_inode(c, dir, mode);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
348
		goto out_fname;
349 350
	}

351 352
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
353
		goto out_inode;
354

355 356 357 358
	mutex_lock(&dir_ui->ui_mutex);
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
359
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
360 361 362 363 364
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
365
	fscrypt_free_filename(&nm);
366 367 368 369 370 371 372 373
	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	mutex_unlock(&dir_ui->ui_mutex);
374
out_inode:
375 376
	make_bad_inode(inode);
	iput(inode);
377 378
out_fname:
	fscrypt_free_filename(&nm);
379 380
out_budg:
	ubifs_release_budget(c, &req);
381
	ubifs_err(c, "cannot create regular file, error %d", err);
382 383 384
	return err;
}

385 386
static int do_tmpfile(struct inode *dir, struct dentry *dentry,
		      umode_t mode, struct inode **whiteout)
R
Richard Weinberger 已提交
387 388 389 390 391 392 393
{
	struct inode *inode;
	struct ubifs_info *c = dir->i_sb->s_fs_info;
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
	struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
	struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
	int err, instantiated = 0;
394
	struct fscrypt_name nm;
R
Richard Weinberger 已提交
395 396 397 398 399 400 401 402 403

	/*
	 * Budget request settings: new dirty inode, new direntry,
	 * budget for dirtied inode will be released via writeback.
	 */

	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
		dentry, mode, dir->i_ino);

404
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
R
Richard Weinberger 已提交
405 406 407
	if (err)
		return err;

408 409 410 411 412 413
	err = ubifs_budget_space(c, &req);
	if (err) {
		fscrypt_free_filename(&nm);
		return err;
	}

R
Richard Weinberger 已提交
414 415 416
	err = ubifs_budget_space(c, &ino_req);
	if (err) {
		ubifs_release_budget(c, &req);
417
		fscrypt_free_filename(&nm);
R
Richard Weinberger 已提交
418 419 420 421 422 423 424 425 426 427
		return err;
	}

	inode = ubifs_new_inode(c, dir, mode);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out_budg;
	}
	ui = ubifs_inode(inode);

428 429 430 431 432
	if (whiteout) {
		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
		ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
	}

R
Richard Weinberger 已提交
433 434 435 436 437 438
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
		goto out_inode;

	mutex_lock(&ui->ui_mutex);
	insert_inode_hash(inode);
439 440 441 442 443 444 445 446

	if (whiteout) {
		mark_inode_dirty(inode);
		drop_nlink(inode);
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
R
Richard Weinberger 已提交
447
	ubifs_assert(ui->dirty);
448

R
Richard Weinberger 已提交
449 450 451 452
	instantiated = 1;
	mutex_unlock(&ui->ui_mutex);

	mutex_lock(&dir_ui->ui_mutex);
453
	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
R
Richard Weinberger 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);

	return 0;

out_cancel:
	mutex_unlock(&dir_ui->ui_mutex);
out_inode:
	make_bad_inode(inode);
	if (!instantiated)
		iput(inode);
out_budg:
	ubifs_release_budget(c, &req);
	if (!instantiated)
		ubifs_release_budget(c, &ino_req);
472
	fscrypt_free_filename(&nm);
R
Richard Weinberger 已提交
473 474 475 476
	ubifs_err(c, "cannot create temporary file, error %d", err);
	return err;
}

477 478 479 480 481 482
static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
			 umode_t mode)
{
	return do_tmpfile(dir, dentry, mode, NULL);
}

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
/**
 * vfs_dent_type - get VFS directory entry type.
 * @type: UBIFS directory entry type
 *
 * This function converts UBIFS directory entry type into VFS directory entry
 * type.
 */
static unsigned int vfs_dent_type(uint8_t type)
{
	switch (type) {
	case UBIFS_ITYPE_REG:
		return DT_REG;
	case UBIFS_ITYPE_DIR:
		return DT_DIR;
	case UBIFS_ITYPE_LNK:
		return DT_LNK;
	case UBIFS_ITYPE_BLK:
		return DT_BLK;
	case UBIFS_ITYPE_CHR:
		return DT_CHR;
	case UBIFS_ITYPE_FIFO:
		return DT_FIFO;
	case UBIFS_ITYPE_SOCK:
		return DT_SOCK;
	default:
		BUG();
	}
	return 0;
}

/*
 * The classical Unix view for directory is that it is a linear array of
 * (name, inode number) entries. Linux/VFS assumes this model as well.
 * Particularly, 'readdir()' call wants us to return a directory entry offset
 * which later may be used to continue 'readdir()'ing the directory or to
 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
 * model because directory entries are identified by keys, which may collide.
 *
 * UBIFS uses directory entry hash value for directory offsets, so
 * 'seekdir()'/'telldir()' may not always work because of possible key
 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
 * properly by means of saving full directory entry name in the private field
 * of the file description object.
 *
 * This means that UBIFS cannot support NFS which requires full
 * 'seekdir()'/'telldir()' support.
 */
A
Al Viro 已提交
530
static int ubifs_readdir(struct file *file, struct dir_context *ctx)
531
{
532
	int fstr_real_len = 0, err = 0;
533 534
	struct fscrypt_name nm;
	struct fscrypt_str fstr = {0};
535 536
	union ubifs_key key;
	struct ubifs_dent_node *dent;
A
Al Viro 已提交
537
	struct inode *dir = file_inode(file);
538
	struct ubifs_info *c = dir->i_sb->s_fs_info;
539
	bool encrypted = ubifs_crypt_is_encrypted(dir);
540

A
Al Viro 已提交
541
	dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
542

A
Al Viro 已提交
543
	if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
544 545 546 547 548 549
		/*
		 * The directory was seek'ed to a senseless position or there
		 * are no more entries.
		 */
		return 0;

550 551 552 553 554 555 556 557 558 559 560 561
	if (encrypted) {
		err = fscrypt_get_encryption_info(dir);
		if (err && err != -ENOKEY)
			return err;

		err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
		if (err)
			return err;

		fstr_real_len = fstr.len;
	}

A
Artem Bityutskiy 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	if (file->f_version == 0) {
		/*
		 * The file was seek'ed, which means that @file->private_data
		 * is now invalid. This may also be just the first
		 * 'ubifs_readdir()' invocation, in which case
		 * @file->private_data is NULL, and the below code is
		 * basically a no-op.
		 */
		kfree(file->private_data);
		file->private_data = NULL;
	}

	/*
	 * 'generic_file_llseek()' unconditionally sets @file->f_version to
	 * zero, and we use this for detecting whether the file was seek'ed.
	 */
	file->f_version = 1;

580
	/* File positions 0 and 1 correspond to "." and ".." */
A
Al Viro 已提交
581
	if (ctx->pos < 2) {
582
		ubifs_assert(!file->private_data);
583 584 585
		if (!dir_emit_dots(file, ctx)) {
			if (encrypted)
				fscrypt_fname_free_buffer(&fstr);
586
			return 0;
587
		}
588 589 590

		/* Find the first entry in TNC and save it */
		lowest_dent_key(c, &key, dir->i_ino);
591
		fname_len(&nm) = 0;
592 593 594 595 596 597
		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			err = PTR_ERR(dent);
			goto out;
		}

A
Al Viro 已提交
598
		ctx->pos = key_hash_flash(c, &dent->key);
599 600 601 602 603 604 605
		file->private_data = dent;
	}

	dent = file->private_data;
	if (!dent) {
		/*
		 * The directory was seek'ed to and is now readdir'ed.
A
Al Viro 已提交
606
		 * Find the entry corresponding to @ctx->pos or the closest one.
607
		 */
A
Al Viro 已提交
608
		dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
609
		fname_len(&nm) = 0;
610 611 612 613 614
		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			err = PTR_ERR(dent);
			goto out;
		}
A
Al Viro 已提交
615
		ctx->pos = key_hash_flash(c, &dent->key);
616 617 618 619
		file->private_data = dent;
	}

	while (1) {
620 621
		dbg_gen("ino %llu, new f_pos %#x",
			(unsigned long long)le64_to_cpu(dent->inum),
622
			key_hash_flash(c, &dent->key));
623 624
		ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
			     ubifs_inode(dir)->creat_sqnum);
625

626 627 628 629 630 631
		fname_len(&nm) = le16_to_cpu(dent->nlen);
		fname_name(&nm) = dent->name;

		if (encrypted) {
			fstr.len = fstr_real_len;

632 633 634 635
			err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
							&dent->key),
							le32_to_cpu(dent->cookie),
							&nm.disk_name, &fstr);
636
			if (err)
637 638 639 640 641 642 643
				goto out;
		} else {
			fstr.len = fname_len(&nm);
			fstr.name = fname_name(&nm);
		}

		if (!dir_emit(ctx, fstr.name, fstr.len,
644
			       le64_to_cpu(dent->inum),
645 646 647
			       vfs_dent_type(dent->type))) {
			if (encrypted)
				fscrypt_fname_free_buffer(&fstr);
648
			return 0;
649
		}
650 651 652 653 654 655 656 657 658 659

		/* Switch to the next entry */
		key_read(c, &dent->key, &key);
		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			err = PTR_ERR(dent);
			goto out;
		}

		kfree(file->private_data);
A
Al Viro 已提交
660
		ctx->pos = key_hash_flash(c, &dent->key);
661 662 663 664 665
		file->private_data = dent;
		cond_resched();
	}

out:
666 667 668
	kfree(file->private_data);
	file->private_data = NULL;

669 670 671
	if (encrypted)
		fscrypt_fname_free_buffer(&fstr);

672
	if (err != -ENOENT)
673
		ubifs_err(c, "cannot find next direntry, error %d", err);
674 675 676 677 678 679 680 681
	else
		/*
		 * -ENOENT is a non-fatal error in this context, the TNC uses
		 * it to indicate that the cursor moved past the current directory
		 * and readdir() has to stop.
		 */
		err = 0;

682

A
Artem Bityutskiy 已提交
683
	/* 2 is a special value indicating that there are no more direntries */
A
Al Viro 已提交
684
	ctx->pos = 2;
685
	return err;
686 687 688 689 690 691 692 693 694 695 696
}

/* Free saved readdir() state when the directory is closed */
static int ubifs_dir_release(struct inode *dir, struct file *file)
{
	kfree(file->private_data);
	file->private_data = NULL;
	return 0;
}

/**
A
Artem Bityutskiy 已提交
697
 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
698 699
 * @inode1: first inode
 * @inode2: second inode
A
Artem Bityutskiy 已提交
700 701 702 703
 *
 * We do not implement any tricks to guarantee strict lock ordering, because
 * VFS has already done it for us on the @i_mutex. So this is just a simple
 * wrapper function.
704 705 706
 */
static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
{
A
Artem Bityutskiy 已提交
707 708
	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
	mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
709 710 711
}

/**
A
Artem Bityutskiy 已提交
712
 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
713 714 715 716 717 718
 * @inode1: first inode
 * @inode2: second inode
 */
static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
{
	mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
A
Artem Bityutskiy 已提交
719
	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
720 721 722 723 724 725
}

static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
		      struct dentry *dentry)
{
	struct ubifs_info *c = dir->i_sb->s_fs_info;
726
	struct inode *inode = d_inode(old_dentry);
727 728 729 730
	struct ubifs_inode *ui = ubifs_inode(inode);
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
	struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
731
				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
732
	struct fscrypt_name nm;
733 734 735 736 737 738

	/*
	 * Budget request settings: new direntry, changing the target inode,
	 * changing the parent inode.
	 */

A
Al Viro 已提交
739 740
	dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
		dentry, inode->i_ino,
741
		inode->i_nlink, dir->i_ino);
A
Al Viro 已提交
742 743
	ubifs_assert(inode_is_locked(dir));
	ubifs_assert(inode_is_locked(inode));
744

745 746 747
	if (ubifs_crypt_is_encrypted(dir) &&
	    !fscrypt_has_permitted_context(dir, inode))
		return -EPERM;
748 749

	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
750 751 752
	if (err)
		return err;

753 754 755 756
	err = dbg_check_synced_i_size(c, inode);
	if (err)
		goto out_fname;

757 758
	err = ubifs_budget_space(c, &req);
	if (err)
759
		goto out_fname;
760 761

	lock_2_inodes(dir, inode);
762 763 764 765 766

	/* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
	if (inode->i_nlink == 0)
		ubifs_delete_orphan(c, inode->i_ino);

767
	inc_nlink(inode);
A
Al Viro 已提交
768
	ihold(inode);
769
	inode->i_ctime = current_time(inode);
770 771 772
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
773
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
774 775 776 777 778 779
	if (err)
		goto out_cancel;
	unlock_2_inodes(dir, inode);

	ubifs_release_budget(c, &req);
	d_instantiate(dentry, inode);
780
	fscrypt_free_filename(&nm);
781 782 783 784 785 786
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	drop_nlink(inode);
787 788
	if (inode->i_nlink == 0)
		ubifs_add_orphan(c, inode->i_ino);
789 790 791
	unlock_2_inodes(dir, inode);
	ubifs_release_budget(c, &req);
	iput(inode);
792 793
out_fname:
	fscrypt_free_filename(&nm);
794 795 796 797 798 799
	return err;
}

static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
{
	struct ubifs_info *c = dir->i_sb->s_fs_info;
800
	struct inode *inode = d_inode(dentry);
801
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
802
	int err, sz_change, budgeted = 1;
803
	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
804
	unsigned int saved_nlink = inode->i_nlink;
805
	struct fscrypt_name nm;
806 807 808 809 810 811 812 813

	/*
	 * Budget request settings: deletion direntry, deletion inode (+1 for
	 * @dirtied_ino), changing the parent directory inode. If budgeting
	 * fails, go ahead anyway because we have extra space reserved for
	 * deletions.
	 */

A
Al Viro 已提交
814 815
	dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
		dentry, inode->i_ino,
816
		inode->i_nlink, dir->i_ino);
817 818 819 820 821 822 823 824 825 826 827 828 829

	if (ubifs_crypt_is_encrypted(dir)) {
		err = fscrypt_get_encryption_info(dir);
		if (err && err != -ENOKEY)
			return err;
	}

	err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
	if (err)
		return err;

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

A
Al Viro 已提交
830 831
	ubifs_assert(inode_is_locked(dir));
	ubifs_assert(inode_is_locked(inode));
832
	err = dbg_check_synced_i_size(c, inode);
833
	if (err)
834
		goto out_fname;
835 836 837 838

	err = ubifs_budget_space(c, &req);
	if (err) {
		if (err != -ENOSPC)
839
			goto out_fname;
840 841 842 843
		budgeted = 0;
	}

	lock_2_inodes(dir, inode);
844
	inode->i_ctime = current_time(dir);
845 846 847 848
	drop_nlink(inode);
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
849
	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
850 851 852 853 854 855 856 857
	if (err)
		goto out_cancel;
	unlock_2_inodes(dir, inode);

	if (budgeted)
		ubifs_release_budget(c, &req);
	else {
		/* We've deleted something - clean the "no space" flags */
858
		c->bi.nospace = c->bi.nospace_rp = 0;
859 860
		smp_wmb();
	}
861
	fscrypt_free_filename(&nm);
862 863 864 865 866
	return 0;

out_cancel:
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
867
	set_nlink(inode, saved_nlink);
868 869 870
	unlock_2_inodes(dir, inode);
	if (budgeted)
		ubifs_release_budget(c, &req);
871 872
out_fname:
	fscrypt_free_filename(&nm);
873 874 875 876 877 878 879 880 881 882 883
	return err;
}

/**
 * check_dir_empty - check if a directory is empty or not.
 * @dir: VFS inode object of the directory to check
 *
 * This function checks if directory @dir is empty. Returns zero if the
 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
 * in case of of errors.
 */
884
int ubifs_check_dir_empty(struct inode *dir)
885
{
886
	struct ubifs_info *c = dir->i_sb->s_fs_info;
887
	struct fscrypt_name nm = { 0 };
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
	struct ubifs_dent_node *dent;
	union ubifs_key key;
	int err;

	lowest_dent_key(c, &key, dir->i_ino);
	dent = ubifs_tnc_next_ent(c, &key, &nm);
	if (IS_ERR(dent)) {
		err = PTR_ERR(dent);
		if (err == -ENOENT)
			err = 0;
	} else {
		kfree(dent);
		err = -ENOTEMPTY;
	}
	return err;
}

static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
{
	struct ubifs_info *c = dir->i_sb->s_fs_info;
908
	struct inode *inode = d_inode(dentry);
909
	int err, sz_change, budgeted = 1;
910 911
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
912
	struct fscrypt_name nm;
913 914 915 916 917 918 919

	/*
	 * Budget request settings: deletion direntry, deletion inode and
	 * changing the parent inode. If budgeting fails, go ahead anyway
	 * because we have extra space reserved for deletions.
	 */

A
Al Viro 已提交
920 921
	dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
		inode->i_ino, dir->i_ino);
A
Al Viro 已提交
922 923
	ubifs_assert(inode_is_locked(dir));
	ubifs_assert(inode_is_locked(inode));
924
	err = ubifs_check_dir_empty(d_inode(dentry));
925 926 927
	if (err)
		return err;

928 929 930 931 932 933 934 935 936 937 938 939
	if (ubifs_crypt_is_encrypted(dir)) {
		err = fscrypt_get_encryption_info(dir);
		if (err && err != -ENOKEY)
			return err;
	}

	err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
	if (err)
		return err;

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

940 941 942
	err = ubifs_budget_space(c, &req);
	if (err) {
		if (err != -ENOSPC)
943
			goto out_fname;
944 945 946 947
		budgeted = 0;
	}

	lock_2_inodes(dir, inode);
948
	inode->i_ctime = current_time(dir);
949 950 951 952 953
	clear_nlink(inode);
	drop_nlink(dir);
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
954
	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
955 956 957 958 959 960 961 962
	if (err)
		goto out_cancel;
	unlock_2_inodes(dir, inode);

	if (budgeted)
		ubifs_release_budget(c, &req);
	else {
		/* We've deleted something - clean the "no space" flags */
963
		c->bi.nospace = c->bi.nospace_rp = 0;
964 965
		smp_wmb();
	}
966
	fscrypt_free_filename(&nm);
967 968 969 970 971 972
	return 0;

out_cancel:
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	inc_nlink(dir);
973
	set_nlink(inode, 2);
974 975 976
	unlock_2_inodes(dir, inode);
	if (budgeted)
		ubifs_release_budget(c, &req);
977 978
out_fname:
	fscrypt_free_filename(&nm);
979 980 981
	return err;
}

982
static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
983 984 985 986
{
	struct inode *inode;
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	struct ubifs_info *c = dir->i_sb->s_fs_info;
987
	int err, sz_change;
988
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
989
	struct fscrypt_name nm;
990 991 992 993 994 995

	/*
	 * Budget request settings: new inode, new direntry and changing parent
	 * directory inode.
	 */

A
Al Viro 已提交
996 997
	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
		dentry, mode, dir->i_ino);
998 999 1000 1001 1002

	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

1003 1004 1005 1006 1007 1008
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
	if (err)
		goto out_budg;

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

1009 1010 1011
	inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
1012
		goto out_fname;
1013 1014
	}

1015 1016
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
1017
		goto out_inode;
1018

1019 1020 1021 1022 1023 1024 1025
	mutex_lock(&dir_ui->ui_mutex);
	insert_inode_hash(inode);
	inc_nlink(inode);
	inc_nlink(dir);
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
1026
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1027
	if (err) {
1028
		ubifs_err(c, "cannot create directory, error %d", err);
1029 1030 1031 1032 1033 1034
		goto out_cancel;
	}
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	d_instantiate(dentry, inode);
1035
	fscrypt_free_filename(&nm);
1036 1037 1038 1039 1040 1041 1042
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	drop_nlink(dir);
	mutex_unlock(&dir_ui->ui_mutex);
1043
out_inode:
1044 1045
	make_bad_inode(inode);
	iput(inode);
1046 1047
out_fname:
	fscrypt_free_filename(&nm);
1048 1049 1050 1051 1052 1053
out_budg:
	ubifs_release_budget(c, &req);
	return err;
}

static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
1054
		       umode_t mode, dev_t rdev)
1055 1056 1057 1058 1059 1060
{
	struct inode *inode;
	struct ubifs_inode *ui;
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	struct ubifs_info *c = dir->i_sb->s_fs_info;
	union ubifs_dev_desc *dev = NULL;
1061
	int sz_change;
1062 1063
	int err, devlen = 0;
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1064
					.dirtied_ino = 1 };
1065
	struct fscrypt_name nm;
1066 1067 1068 1069 1070 1071

	/*
	 * Budget request settings: new inode, new direntry and changing parent
	 * directory inode.
	 */

A
Al Viro 已提交
1072
	dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
1073 1074 1075 1076 1077 1078 1079 1080

	if (S_ISBLK(mode) || S_ISCHR(mode)) {
		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
		if (!dev)
			return -ENOMEM;
		devlen = ubifs_encode_dev(dev, rdev);
	}

1081
	req.new_ino_d = ALIGN(devlen, 8);
1082 1083 1084 1085 1086 1087
	err = ubifs_budget_space(c, &req);
	if (err) {
		kfree(dev);
		return err;
	}

1088
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1089 1090
	if (err) {
		kfree(dev);
1091
		goto out_budg;
1092
	}
1093 1094 1095

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

1096 1097 1098 1099
	inode = ubifs_new_inode(c, dir, mode);
	if (IS_ERR(inode)) {
		kfree(dev);
		err = PTR_ERR(inode);
1100
		goto out_fname;
1101 1102 1103 1104 1105 1106 1107 1108
	}

	init_special_inode(inode, inode->i_mode, rdev);
	inode->i_size = ubifs_inode(inode)->ui_size = devlen;
	ui = ubifs_inode(inode);
	ui->data = dev;
	ui->data_len = devlen;

1109 1110
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
1111
		goto out_inode;
1112

1113 1114 1115 1116
	mutex_lock(&dir_ui->ui_mutex);
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
1117
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1118 1119 1120 1121 1122 1123 1124
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
1125
	fscrypt_free_filename(&nm);
1126 1127 1128 1129 1130 1131
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	mutex_unlock(&dir_ui->ui_mutex);
1132
out_inode:
1133 1134
	make_bad_inode(inode);
	iput(inode);
1135 1136
out_fname:
	fscrypt_free_filename(&nm);
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
out_budg:
	ubifs_release_budget(c, &req);
	return err;
}

static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
			 const char *symname)
{
	struct inode *inode;
	struct ubifs_inode *ui;
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	struct ubifs_info *c = dir->i_sb->s_fs_info;
	int err, len = strlen(symname);
1150 1151 1152
	int sz_change = CALC_DENT_SIZE(len);
	struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
	struct fscrypt_symlink_data *sd = NULL;
1153
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1154 1155
					.new_ino_d = ALIGN(len, 8),
					.dirtied_ino = 1 };
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	struct fscrypt_name nm;

	if (ubifs_crypt_is_encrypted(dir)) {
		err = fscrypt_get_encryption_info(dir);
		if (err)
			goto out_budg;

		if (!fscrypt_has_encryption_key(dir)) {
			err = -EPERM;
			goto out_budg;
		}

		disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
				sizeof(struct fscrypt_symlink_data));
	}
1171 1172 1173 1174 1175 1176

	/*
	 * Budget request settings: new inode, new direntry and changing parent
	 * directory inode.
	 */

A
Al Viro 已提交
1177 1178
	dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
		symname, dir->i_ino);
1179

1180
	if (disk_link.len > UBIFS_MAX_INO_DATA)
1181 1182 1183 1184 1185 1186
		return -ENAMETOOLONG;

	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

1187 1188 1189 1190
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
	if (err)
		goto out_budg;

1191 1192 1193
	inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
1194
		goto out_fname;
1195 1196 1197
	}

	ui = ubifs_inode(inode);
1198
	ui->data = kmalloc(disk_link.len, GFP_NOFS);
1199 1200 1201 1202 1203
	if (!ui->data) {
		err = -ENOMEM;
		goto out_inode;
	}

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	if (ubifs_crypt_is_encrypted(dir)) {
		struct qstr istr = QSTR_INIT(symname, len);
		struct fscrypt_str ostr;

		sd = kzalloc(disk_link.len, GFP_NOFS);
		if (!sd) {
			err = -ENOMEM;
			goto out_inode;
		}

		ostr.name = sd->encrypted_path;
		ostr.len = disk_link.len;

		err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
		if (err) {
			kfree(sd);
			goto out_inode;
		}

		sd->len = cpu_to_le16(ostr.len);
		disk_link.name = (char *)sd;
	} else {
		inode->i_link = ui->data;
	}

	memcpy(ui->data, disk_link.name, disk_link.len);
	((char *)ui->data)[disk_link.len - 1] = '\0';

1232 1233 1234 1235 1236
	/*
	 * The terminating zero byte is not written to the flash media and it
	 * is put just to make later in-memory string processing simpler. Thus,
	 * data length is @len, not @len + %1.
	 */
1237 1238
	ui->data_len = disk_link.len - 1;
	inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
1239

1240 1241
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
1242
		goto out_inode;
1243

1244 1245 1246 1247
	mutex_lock(&dir_ui->ui_mutex);
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
1248
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1249 1250 1251 1252 1253 1254 1255
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
1256
	fscrypt_free_filename(&nm);
1257 1258 1259 1260 1261 1262 1263 1264 1265
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	mutex_unlock(&dir_ui->ui_mutex);
out_inode:
	make_bad_inode(inode);
	iput(inode);
1266 1267
out_fname:
	fscrypt_free_filename(&nm);
1268 1269 1270 1271 1272 1273
out_budg:
	ubifs_release_budget(c, &req);
	return err;
}

/**
1274
 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1275 1276 1277
 * @inode1: first inode
 * @inode2: second inode
 * @inode3: third inode
1278
 * @inode4: fouth inode
1279
 *
A
Artem Bityutskiy 已提交
1280
 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1281
 * @inode2 whereas @inode3 and @inode4 may be %NULL.
A
Artem Bityutskiy 已提交
1282 1283 1284 1285
 *
 * We do not implement any tricks to guarantee strict lock ordering, because
 * VFS has already done it for us on the @i_mutex. So this is just a simple
 * wrapper function.
1286
 */
1287 1288
static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
			  struct inode *inode3, struct inode *inode4)
1289
{
A
Artem Bityutskiy 已提交
1290 1291 1292 1293 1294
	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
	if (inode2 != inode1)
		mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
	if (inode3)
		mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
1295 1296
	if (inode4)
		mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1297 1298 1299
}

/**
1300
 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1301 1302 1303
 * @inode1: first inode
 * @inode2: second inode
 * @inode3: third inode
1304
 * @inode4: fouth inode
1305
 */
1306 1307
static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
			    struct inode *inode3, struct inode *inode4)
1308
{
1309 1310
	if (inode4)
		mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1311 1312
	if (inode3)
		mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
A
Artem Bityutskiy 已提交
1313 1314 1315
	if (inode1 != inode2)
		mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1316 1317
}

R
Richard Weinberger 已提交
1318 1319 1320
static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
		     struct inode *new_dir, struct dentry *new_dentry,
		     unsigned int flags)
1321 1322
{
	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1323 1324
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
1325
	struct inode *whiteout = NULL;
1326
	struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1327
	struct ubifs_inode *whiteout_ui = NULL;
1328 1329
	int err, release, sync = 0, move = (new_dir != old_dir);
	int is_dir = S_ISDIR(old_inode->i_mode);
1330
	int unlink = !!new_inode, new_sz, old_sz;
1331 1332 1333
	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
					.dirtied_ino = 3 };
	struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1334
			.dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1335
	struct timespec time;
1336
	unsigned int uninitialized_var(saved_nlink);
1337
	struct fscrypt_name old_nm, new_nm;
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

	/*
	 * Budget request settings: deletion direntry, new direntry, removing
	 * the old inode, and changing old and new parent directory inodes.
	 *
	 * However, this operation also marks the target inode as dirty and
	 * does not write it, so we allocate budget for the target inode
	 * separately.
	 */

1348
	dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
A
Al Viro 已提交
1349
		old_dentry, old_inode->i_ino, old_dir->i_ino,
1350 1351
		new_dentry, new_dir->i_ino, flags);

A
Artem Bityutskiy 已提交
1352
	if (unlink)
A
Al Viro 已提交
1353
		ubifs_assert(inode_is_locked(new_inode));
A
Artem Bityutskiy 已提交
1354

1355 1356 1357 1358 1359 1360
	if (old_dir != new_dir) {
		if (ubifs_crypt_is_encrypted(new_dir) &&
		    !fscrypt_has_permitted_context(new_dir, old_inode))
			return -EPERM;
	}

1361
	if (unlink && is_dir) {
1362
		err = ubifs_check_dir_empty(new_inode);
1363 1364 1365 1366
		if (err)
			return err;
	}

1367
	err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
1368 1369
	if (err)
		return err;
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

	err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
	if (err) {
		fscrypt_free_filename(&old_nm);
		return err;
	}

	new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
	old_sz = CALC_DENT_SIZE(fname_len(&old_nm));

	err = ubifs_budget_space(c, &req);
	if (err) {
		fscrypt_free_filename(&old_nm);
		fscrypt_free_filename(&new_nm);
		return err;
	}
1386 1387
	err = ubifs_budget_space(c, &ino_req);
	if (err) {
1388 1389
		fscrypt_free_filename(&old_nm);
		fscrypt_free_filename(&new_nm);
1390 1391 1392 1393
		ubifs_release_budget(c, &req);
		return err;
	}

1394 1395 1396 1397 1398
	if (flags & RENAME_WHITEOUT) {
		union ubifs_dev_desc *dev = NULL;

		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
		if (!dev) {
1399 1400
			err = -ENOMEM;
			goto out_release;
1401 1402 1403 1404 1405
		}

		err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
		if (err) {
			kfree(dev);
1406
			goto out_release;
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
		}

		whiteout->i_state |= I_LINKABLE;
		whiteout_ui = ubifs_inode(whiteout);
		whiteout_ui->data = dev;
		whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
		ubifs_assert(!whiteout_ui->dirty);
	}

	lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1417 1418 1419 1420 1421

	/*
	 * Like most other Unix systems, set the @i_ctime for inodes on a
	 * rename.
	 */
1422
	time = current_time(old_dir);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	old_inode->i_ctime = time;

	/* We must adjust parent link count when renaming directories */
	if (is_dir) {
		if (move) {
			/*
			 * @old_dir loses a link because we are moving
			 * @old_inode to a different directory.
			 */
			drop_nlink(old_dir);
			/*
			 * @new_dir only gains a link if we are not also
			 * overwriting an existing directory.
			 */
			if (!unlink)
				inc_nlink(new_dir);
		} else {
			/*
			 * @old_inode is not moving to a different directory,
			 * but @old_dir still loses a link if we are
			 * overwriting an existing directory.
			 */
			if (unlink)
				drop_nlink(old_dir);
		}
	}

	old_dir->i_size -= old_sz;
	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
	old_dir->i_mtime = old_dir->i_ctime = time;
	new_dir->i_mtime = new_dir->i_ctime = time;

	/*
	 * And finally, if we unlinked a direntry which happened to have the
	 * same name as the moved direntry, we have to decrement @i_nlink of
	 * the unlinked inode and change its ctime.
	 */
	if (unlink) {
		/*
		 * Directories cannot have hard-links, so if this is a
1463
		 * directory, just clear @i_nlink.
1464
		 */
1465
		saved_nlink = new_inode->i_nlink;
1466
		if (is_dir)
1467 1468
			clear_nlink(new_inode);
		else
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
			drop_nlink(new_inode);
		new_inode->i_ctime = time;
	} else {
		new_dir->i_size += new_sz;
		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
	}

	/*
	 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
	 * is dirty, because this will be done later on at the end of
	 * 'ubifs_rename()'.
	 */
	if (IS_SYNC(old_inode)) {
		sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
		if (unlink && IS_SYNC(new_inode))
			sync = 1;
	}
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

	if (whiteout) {
		struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
				.dirtied_ino_d = \
				ALIGN(ubifs_inode(whiteout)->data_len, 8) };

		err = ubifs_budget_space(c, &wht_req);
		if (err) {
			kfree(whiteout_ui->data);
			whiteout_ui->data_len = 0;
			iput(whiteout);
1497
			goto out_release;
1498 1499 1500 1501 1502 1503 1504 1505
		}

		inc_nlink(whiteout);
		mark_inode_dirty(whiteout);
		whiteout->i_state &= ~I_LINKABLE;
		iput(whiteout);
	}

1506 1507
	err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
			       new_inode, &new_nm, whiteout, sync);
1508 1509 1510
	if (err)
		goto out_cancel;

1511
	unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	ubifs_release_budget(c, &req);

	mutex_lock(&old_inode_ui->ui_mutex);
	release = old_inode_ui->dirty;
	mark_inode_dirty_sync(old_inode);
	mutex_unlock(&old_inode_ui->ui_mutex);

	if (release)
		ubifs_release_budget(c, &ino_req);
	if (IS_SYNC(old_inode))
1522
		err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
1523 1524 1525

	fscrypt_free_filename(&old_nm);
	fscrypt_free_filename(&new_nm);
1526 1527 1528 1529
	return err;

out_cancel:
	if (unlink) {
1530
		set_nlink(new_inode, saved_nlink);
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	} else {
		new_dir->i_size -= new_sz;
		ubifs_inode(new_dir)->ui_size = new_dir->i_size;
	}
	old_dir->i_size += old_sz;
	ubifs_inode(old_dir)->ui_size = old_dir->i_size;
	if (is_dir) {
		if (move) {
			inc_nlink(old_dir);
			if (!unlink)
				drop_nlink(new_dir);
		} else {
			if (unlink)
				inc_nlink(old_dir);
		}
	}
1547 1548 1549 1550 1551
	if (whiteout) {
		drop_nlink(whiteout);
		iput(whiteout);
	}
	unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1552
out_release:
1553 1554
	ubifs_release_budget(c, &ino_req);
	ubifs_release_budget(c, &req);
1555 1556
	fscrypt_free_filename(&old_nm);
	fscrypt_free_filename(&new_nm);
1557 1558 1559
	return err;
}

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
			struct inode *new_dir, struct dentry *new_dentry)
{
	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
				.dirtied_ino = 2 };
	int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
	struct inode *fst_inode = d_inode(old_dentry);
	struct inode *snd_inode = d_inode(new_dentry);
	struct timespec time;
	int err;
1571
	struct fscrypt_name fst_nm, snd_nm;
1572 1573 1574

	ubifs_assert(fst_inode && snd_inode);

1575 1576 1577 1578 1579 1580 1581
	if ((ubifs_crypt_is_encrypted(old_dir) ||
	    ubifs_crypt_is_encrypted(new_dir)) &&
	    (old_dir != new_dir) &&
	    (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
	     !fscrypt_has_permitted_context(old_dir, snd_inode)))
		return -EPERM;

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
	err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
	if (err)
		return err;

	err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
	if (err) {
		fscrypt_free_filename(&fst_nm);
		return err;
	}

1592 1593
	lock_4_inodes(old_dir, new_dir, NULL, NULL);

1594
	time = current_time(old_dir);
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	fst_inode->i_ctime = time;
	snd_inode->i_ctime = time;
	old_dir->i_mtime = old_dir->i_ctime = time;
	new_dir->i_mtime = new_dir->i_ctime = time;

	if (old_dir != new_dir) {
		if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
			inc_nlink(new_dir);
			drop_nlink(old_dir);
		}
		else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
			drop_nlink(new_dir);
			inc_nlink(old_dir);
		}
	}

1611 1612
	err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
				snd_inode, &snd_nm, sync);
1613 1614 1615 1616

	unlock_4_inodes(old_dir, new_dir, NULL, NULL);
	ubifs_release_budget(c, &req);

1617 1618
	fscrypt_free_filename(&fst_nm);
	fscrypt_free_filename(&snd_nm);
1619 1620 1621
	return err;
}

R
Richard Weinberger 已提交
1622
static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
{
	if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
		return -EINVAL;

	ubifs_assert(inode_is_locked(old_dir));
	ubifs_assert(inode_is_locked(new_dir));

	if (flags & RENAME_EXCHANGE)
		return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);

R
Richard Weinberger 已提交
1635
	return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1636 1637
}

1638 1639
int ubifs_getattr(const struct path *path, struct kstat *stat,
		  u32 request_mask, unsigned int flags)
1640 1641
{
	loff_t size;
1642
	struct inode *inode = d_inode(path->dentry);
1643 1644 1645
	struct ubifs_inode *ui = ubifs_inode(inode);

	mutex_lock(&ui->ui_mutex);
A
Al Viro 已提交
1646
	generic_fillattr(inode, stat);
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
	stat->blksize = UBIFS_BLOCK_SIZE;
	stat->size = ui->ui_size;

	/*
	 * Unfortunately, the 'stat()' system call was designed for block
	 * device based file systems, and it is not appropriate for UBIFS,
	 * because UBIFS does not have notion of "block". For example, it is
	 * difficult to tell how many block a directory takes - it actually
	 * takes less than 300 bytes, but we have to round it to block size,
	 * which introduces large mistake. This makes utilities like 'du' to
	 * report completely senseless numbers. This is the reason why UBIFS
	 * goes the same way as JFFS2 - it reports zero blocks for everything
	 * but regular files, which makes more sense than reporting completely
	 * wrong sizes.
	 */
	if (S_ISREG(inode->i_mode)) {
		size = ui->xattr_size;
		size += stat->size;
		size = ALIGN(size, UBIFS_BLOCK_SIZE);
		/*
		 * Note, user-space expects 512-byte blocks count irrespectively
		 * of what was reported in @stat->size.
		 */
		stat->blocks = size >> 9;
	} else
		stat->blocks = 0;
	mutex_unlock(&ui->ui_mutex);
	return 0;
}

1677 1678 1679 1680 1681 1682 1683 1684
static int ubifs_dir_open(struct inode *dir, struct file *file)
{
	if (ubifs_crypt_is_encrypted(dir))
		return fscrypt_get_encryption_info(dir) ? -EACCES : 0;

	return 0;
}

A
Artem Bityutskiy 已提交
1685
const struct inode_operations ubifs_dir_inode_operations = {
1686 1687 1688 1689 1690 1691 1692 1693
	.lookup      = ubifs_lookup,
	.create      = ubifs_create,
	.link        = ubifs_link,
	.symlink     = ubifs_symlink,
	.unlink      = ubifs_unlink,
	.mkdir       = ubifs_mkdir,
	.rmdir       = ubifs_rmdir,
	.mknod       = ubifs_mknod,
R
Richard Weinberger 已提交
1694
	.rename      = ubifs_rename,
1695 1696 1697
	.setattr     = ubifs_setattr,
	.getattr     = ubifs_getattr,
	.listxattr   = ubifs_listxattr,
1698 1699 1700
#ifdef CONFIG_UBIFS_ATIME_SUPPORT
	.update_time = ubifs_update_time,
#endif
R
Richard Weinberger 已提交
1701
	.tmpfile     = ubifs_tmpfile,
1702 1703
};

A
Artem Bityutskiy 已提交
1704
const struct file_operations ubifs_dir_operations = {
A
Al Viro 已提交
1705
	.llseek         = generic_file_llseek,
1706 1707
	.release        = ubifs_dir_release,
	.read           = generic_read_dir,
1708
	.iterate_shared = ubifs_readdir,
1709 1710
	.fsync          = ubifs_fsync,
	.unlocked_ioctl = ubifs_ioctl,
1711
	.open		= ubifs_dir_open,
1712 1713 1714 1715
#ifdef CONFIG_COMPAT
	.compat_ioctl   = ubifs_compat_ioctl,
#endif
};