dir.c 42.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
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
/* * This file is part of UBIFS.
 *
 * Copyright (C) 2006-2008 Nokia Corporation.
 * Copyright (C) 2006, 2007 University of Szeged, Hungary
 *
 * 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 已提交
47
static int inherit_flags(const struct inode *dir, umode_t mode)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
{
	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.
 */
76
struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
A
Al Viro 已提交
77
			      umode_t mode)
78
{
79
	int err;
80 81
	struct inode *inode;
	struct ubifs_inode *ui;
82 83
	bool encrypted = false;

84
	if (IS_ENCRYPTED(dir)) {
85 86 87 88 89 90 91 92 93 94 95
		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;
	}
96 97 98 99 100 101 102 103 104 105 106 107

	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.
	 */
108
	inode->i_flags |= S_NOCMTIME;
109

110
	inode_init_owner(inode, dir, mode);
111
	inode->i_mtime = inode->i_atime = inode->i_ctime =
112
			 current_time(inode);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	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;
134
		encrypted = false;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		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);
153
			ubifs_err(c, "out of inode numbers");
154 155 156 157
			make_bad_inode(inode);
			iput(inode);
			return ERR_PTR(-EINVAL);
		}
158
		ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
A
Artem Bityutskiy 已提交
159
			   (unsigned long)c->highest_inum, INUM_WATERMARK);
160 161 162 163 164 165 166 167 168 169 170 171
	}

	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);
172 173 174 175 176 177 178 179 180 181 182

	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);
		}
	}

183 184 185
	return inode;
}

186 187
static int dbg_check_name(const struct ubifs_info *c,
			  const struct ubifs_dent_node *dent,
188
			  const struct fscrypt_name *nm)
189
{
190
	if (!dbg_is_chk_gen(c))
191
		return 0;
192
	if (le16_to_cpu(dent->nlen) != fname_len(nm))
193
		return -EINVAL;
194
	if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
195 196 197 198 199
		return -EINVAL;
	return 0;
}

static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
200
				   unsigned int flags)
201 202 203 204
{
	int err;
	union ubifs_key key;
	struct inode *inode = NULL;
A
Al Viro 已提交
205
	struct ubifs_dent_node *dent = NULL;
206
	struct ubifs_info *c = dir->i_sb->s_fs_info;
207
	struct fscrypt_name nm;
208

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

211 212 213
	err = fscrypt_prepare_lookup(dir, dentry, &nm);
	if (err == -ENOENT)
		return d_splice_alias(NULL, dentry);
214 215 216 217
	if (err)
		return ERR_PTR(err);

	if (fname_len(&nm) > UBIFS_MAX_NLEN) {
A
Al Viro 已提交
218 219
		inode = ERR_PTR(-ENAMETOOLONG);
		goto done;
220
	}
221 222

	dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
223
	if (!dent) {
A
Al Viro 已提交
224 225
		inode = ERR_PTR(-ENOMEM);
		goto done;
226
	}
227

228
	if (nm.hash) {
229 230
		ubifs_assert(c, fname_len(&nm) == 0);
		ubifs_assert(c, fname_name(&nm) == NULL);
231
		dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
232
		err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
233 234 235 236
	} else {
		dent_key_init(c, &key, dir->i_ino, &nm);
		err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
	}
237 238

	if (err) {
A
Al Viro 已提交
239
		if (err == -ENOENT)
240
			dbg_gen("not found");
A
Al Viro 已提交
241 242 243
		else
			inode = ERR_PTR(err);
		goto done;
244 245
	}

246
	if (dbg_check_name(c, dent, &nm)) {
A
Al Viro 已提交
247 248
		inode = ERR_PTR(-EINVAL);
		goto done;
249 250 251 252 253 254 255 256 257
	}

	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);
258
		ubifs_err(c, "dead directory entry '%pd', error %d",
A
Al Viro 已提交
259
			  dentry, err);
260
		ubifs_ro_mode(c, err);
A
Al Viro 已提交
261
		goto done;
262 263
	}

264
	if (IS_ENCRYPTED(dir) &&
265 266 267 268
	    (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);
A
Al Viro 已提交
269 270
		iput(inode);
		inode = ERR_PTR(-EPERM);
271 272
	}

273 274
done:
	kfree(dent);
275
	fscrypt_free_filename(&nm);
A
Al Viro 已提交
276
	return d_splice_alias(inode, dentry);
277 278
}

A
Al Viro 已提交
279
static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
280
			bool excl)
281 282 283 284 285 286
{
	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);
287 288
	struct fscrypt_name nm;
	int err, sz_change;
289 290 291 292 293 294

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

A
Al Viro 已提交
295 296
	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
		dentry, mode, dir->i_ino);
297 298 299 300 301

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

302 303 304 305 306 307
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
	if (err)
		goto out_budg;

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

308 309 310
	inode = ubifs_new_inode(c, dir, mode);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
311
		goto out_fname;
312 313
	}

314 315
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
316
		goto out_inode;
317

318 319 320 321
	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;
322
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
323 324 325 326 327
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
328
	fscrypt_free_filename(&nm);
329 330 331 332 333 334 335 336
	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);
337
out_inode:
338 339
	make_bad_inode(inode);
	iput(inode);
340 341
out_fname:
	fscrypt_free_filename(&nm);
342 343
out_budg:
	ubifs_release_budget(c, &req);
344
	ubifs_err(c, "cannot create regular file, error %d", err);
345 346 347
	return err;
}

348 349
static int do_tmpfile(struct inode *dir, struct dentry *dentry,
		      umode_t mode, struct inode **whiteout)
R
Richard Weinberger 已提交
350 351 352 353 354 355 356
{
	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;
357
	struct fscrypt_name nm;
R
Richard Weinberger 已提交
358 359 360 361 362 363 364 365 366

	/*
	 * 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);

367
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
R
Richard Weinberger 已提交
368 369 370
	if (err)
		return err;

371 372 373 374 375 376
	err = ubifs_budget_space(c, &req);
	if (err) {
		fscrypt_free_filename(&nm);
		return err;
	}

R
Richard Weinberger 已提交
377 378 379
	err = ubifs_budget_space(c, &ino_req);
	if (err) {
		ubifs_release_budget(c, &req);
380
		fscrypt_free_filename(&nm);
R
Richard Weinberger 已提交
381 382 383 384 385 386 387 388 389 390
		return err;
	}

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

391 392
	if (whiteout) {
		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
393
		ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
394 395
	}

R
Richard Weinberger 已提交
396 397 398 399 400 401
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
		goto out_inode;

	mutex_lock(&ui->ui_mutex);
	insert_inode_hash(inode);
402 403 404 405 406 407 408 409

	if (whiteout) {
		mark_inode_dirty(inode);
		drop_nlink(inode);
		*whiteout = inode;
	} else {
		d_tmpfile(dentry, inode);
	}
410
	ubifs_assert(c, ui->dirty);
411

R
Richard Weinberger 已提交
412 413 414 415
	instantiated = 1;
	mutex_unlock(&ui->ui_mutex);

	mutex_lock(&dir_ui->ui_mutex);
416
	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
R
Richard Weinberger 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	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);
435
	fscrypt_free_filename(&nm);
R
Richard Weinberger 已提交
436 437 438 439
	ubifs_err(c, "cannot create temporary file, error %d", err);
	return err;
}

440 441 442 443 444 445
static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
			 umode_t mode)
{
	return do_tmpfile(dir, dentry, mode, NULL);
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
/**
 * 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 已提交
493
static int ubifs_readdir(struct file *file, struct dir_context *ctx)
494
{
495
	int fstr_real_len = 0, err = 0;
496 497
	struct fscrypt_name nm;
	struct fscrypt_str fstr = {0};
498 499
	union ubifs_key key;
	struct ubifs_dent_node *dent;
A
Al Viro 已提交
500
	struct inode *dir = file_inode(file);
501
	struct ubifs_info *c = dir->i_sb->s_fs_info;
502
	bool encrypted = IS_ENCRYPTED(dir);
503

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

A
Al Viro 已提交
506
	if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
507 508 509 510 511 512
		/*
		 * The directory was seek'ed to a senseless position or there
		 * are no more entries.
		 */
		return 0;

513 514
	if (encrypted) {
		err = fscrypt_get_encryption_info(dir);
515
		if (err)
516 517 518 519 520 521 522 523 524
			return err;

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

		fstr_real_len = fstr.len;
	}

A
Artem Bityutskiy 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	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;

543
	/* File positions 0 and 1 correspond to "." and ".." */
A
Al Viro 已提交
544
	if (ctx->pos < 2) {
545
		ubifs_assert(c, !file->private_data);
546 547 548
		if (!dir_emit_dots(file, ctx)) {
			if (encrypted)
				fscrypt_fname_free_buffer(&fstr);
549
			return 0;
550
		}
551 552 553

		/* Find the first entry in TNC and save it */
		lowest_dent_key(c, &key, dir->i_ino);
554
		fname_len(&nm) = 0;
555 556 557 558 559 560
		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			err = PTR_ERR(dent);
			goto out;
		}

A
Al Viro 已提交
561
		ctx->pos = key_hash_flash(c, &dent->key);
562 563 564 565 566 567 568
		file->private_data = dent;
	}

	dent = file->private_data;
	if (!dent) {
		/*
		 * The directory was seek'ed to and is now readdir'ed.
A
Al Viro 已提交
569
		 * Find the entry corresponding to @ctx->pos or the closest one.
570
		 */
A
Al Viro 已提交
571
		dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
572
		fname_len(&nm) = 0;
573 574 575 576 577
		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			err = PTR_ERR(dent);
			goto out;
		}
A
Al Viro 已提交
578
		ctx->pos = key_hash_flash(c, &dent->key);
579 580 581 582
		file->private_data = dent;
	}

	while (1) {
583 584
		dbg_gen("ino %llu, new f_pos %#x",
			(unsigned long long)le64_to_cpu(dent->inum),
585
			key_hash_flash(c, &dent->key));
586
		ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
587
			     ubifs_inode(dir)->creat_sqnum);
588

589 590 591 592 593 594
		fname_len(&nm) = le16_to_cpu(dent->nlen);
		fname_name(&nm) = dent->name;

		if (encrypted) {
			fstr.len = fstr_real_len;

595 596 597 598
			err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
							&dent->key),
							le32_to_cpu(dent->cookie),
							&nm.disk_name, &fstr);
599
			if (err)
600 601 602 603 604 605 606
				goto out;
		} else {
			fstr.len = fname_len(&nm);
			fstr.name = fname_name(&nm);
		}

		if (!dir_emit(ctx, fstr.name, fstr.len,
607
			       le64_to_cpu(dent->inum),
608 609 610
			       vfs_dent_type(dent->type))) {
			if (encrypted)
				fscrypt_fname_free_buffer(&fstr);
611
			return 0;
612
		}
613 614 615 616 617 618 619 620 621 622

		/* 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 已提交
623
		ctx->pos = key_hash_flash(c, &dent->key);
624 625 626 627 628
		file->private_data = dent;
		cond_resched();
	}

out:
629 630 631
	kfree(file->private_data);
	file->private_data = NULL;

632 633 634
	if (encrypted)
		fscrypt_fname_free_buffer(&fstr);

635
	if (err != -ENOENT)
636
		ubifs_err(c, "cannot find next direntry, error %d", err);
637 638 639 640 641 642 643 644
	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;

645

A
Artem Bityutskiy 已提交
646
	/* 2 is a special value indicating that there are no more direntries */
A
Al Viro 已提交
647
	ctx->pos = 2;
648
	return err;
649 650 651 652 653 654 655 656 657 658 659
}

/* 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 已提交
660
 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
661 662
 * @inode1: first inode
 * @inode2: second inode
A
Artem Bityutskiy 已提交
663 664 665 666
 *
 * 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.
667 668 669
 */
static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
{
A
Artem Bityutskiy 已提交
670 671
	mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
	mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
672 673 674
}

/**
A
Artem Bityutskiy 已提交
675
 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
676 677 678 679 680 681
 * @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 已提交
682
	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
683 684 685 686 687 688
}

static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
		      struct dentry *dentry)
{
	struct ubifs_info *c = dir->i_sb->s_fs_info;
689
	struct inode *inode = d_inode(old_dentry);
690 691 692 693
	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,
694
				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
695
	struct fscrypt_name nm;
696 697 698 699 700 701

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

A
Al Viro 已提交
702 703
	dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
		dentry, inode->i_ino,
704
		inode->i_nlink, dir->i_ino);
705 706
	ubifs_assert(c, inode_is_locked(dir));
	ubifs_assert(c, inode_is_locked(inode));
707

708 709 710
	err = fscrypt_prepare_link(old_dentry, dir, dentry);
	if (err)
		return err;
711 712

	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
713 714 715
	if (err)
		return err;

716 717 718 719
	err = dbg_check_synced_i_size(c, inode);
	if (err)
		goto out_fname;

720 721
	err = ubifs_budget_space(c, &req);
	if (err)
722
		goto out_fname;
723 724

	lock_2_inodes(dir, inode);
725 726 727 728 729

	/* 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);

730
	inc_nlink(inode);
A
Al Viro 已提交
731
	ihold(inode);
732
	inode->i_ctime = current_time(inode);
733 734 735
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
736
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
737 738 739 740 741 742
	if (err)
		goto out_cancel;
	unlock_2_inodes(dir, inode);

	ubifs_release_budget(c, &req);
	d_instantiate(dentry, inode);
743
	fscrypt_free_filename(&nm);
744 745 746 747 748 749
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	drop_nlink(inode);
750 751
	if (inode->i_nlink == 0)
		ubifs_add_orphan(c, inode->i_ino);
752 753 754
	unlock_2_inodes(dir, inode);
	ubifs_release_budget(c, &req);
	iput(inode);
755 756
out_fname:
	fscrypt_free_filename(&nm);
757 758 759 760 761 762
	return err;
}

static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
{
	struct ubifs_info *c = dir->i_sb->s_fs_info;
763
	struct inode *inode = d_inode(dentry);
764
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
765
	int err, sz_change, budgeted = 1;
766
	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
767
	unsigned int saved_nlink = inode->i_nlink;
768
	struct fscrypt_name nm;
769 770 771 772 773 774 775 776

	/*
	 * 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 已提交
777 778
	dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
		dentry, inode->i_ino,
779
		inode->i_nlink, dir->i_ino);
780 781 782 783 784

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

785 786 787 788
	err = ubifs_purge_xattrs(inode);
	if (err)
		return err;

789 790
	sz_change = CALC_DENT_SIZE(fname_len(&nm));

791 792
	ubifs_assert(c, inode_is_locked(dir));
	ubifs_assert(c, inode_is_locked(inode));
793
	err = dbg_check_synced_i_size(c, inode);
794
	if (err)
795
		goto out_fname;
796 797 798 799

	err = ubifs_budget_space(c, &req);
	if (err) {
		if (err != -ENOSPC)
800
			goto out_fname;
801 802 803 804
		budgeted = 0;
	}

	lock_2_inodes(dir, inode);
805
	inode->i_ctime = current_time(dir);
806 807 808 809
	drop_nlink(inode);
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	dir->i_mtime = dir->i_ctime = inode->i_ctime;
810
	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
811 812 813 814 815 816 817 818
	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 */
819
		c->bi.nospace = c->bi.nospace_rp = 0;
820 821
		smp_wmb();
	}
822
	fscrypt_free_filename(&nm);
823 824 825 826 827
	return 0;

out_cancel:
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
828
	set_nlink(inode, saved_nlink);
829 830 831
	unlock_2_inodes(dir, inode);
	if (budgeted)
		ubifs_release_budget(c, &req);
832 833
out_fname:
	fscrypt_free_filename(&nm);
834 835 836 837 838 839 840 841 842 843 844
	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.
 */
845
int ubifs_check_dir_empty(struct inode *dir)
846
{
847
	struct ubifs_info *c = dir->i_sb->s_fs_info;
848
	struct fscrypt_name nm = { 0 };
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
	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;
869
	struct inode *inode = d_inode(dentry);
870
	int err, sz_change, budgeted = 1;
871 872
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
873
	struct fscrypt_name nm;
874 875 876 877 878 879 880

	/*
	 * 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 已提交
881 882
	dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
		inode->i_ino, dir->i_ino);
883 884
	ubifs_assert(c, inode_is_locked(dir));
	ubifs_assert(c, inode_is_locked(inode));
885
	err = ubifs_check_dir_empty(d_inode(dentry));
886 887 888
	if (err)
		return err;

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

893 894 895 896
	err = ubifs_purge_xattrs(inode);
	if (err)
		return err;

897 898
	sz_change = CALC_DENT_SIZE(fname_len(&nm));

899 900 901
	err = ubifs_budget_space(c, &req);
	if (err) {
		if (err != -ENOSPC)
902
			goto out_fname;
903 904 905 906
		budgeted = 0;
	}

	lock_2_inodes(dir, inode);
907
	inode->i_ctime = current_time(dir);
908 909 910 911 912
	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;
913
	err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
914 915 916 917 918 919 920 921
	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 */
922
		c->bi.nospace = c->bi.nospace_rp = 0;
923 924
		smp_wmb();
	}
925
	fscrypt_free_filename(&nm);
926 927 928 929 930 931
	return 0;

out_cancel:
	dir->i_size += sz_change;
	dir_ui->ui_size = dir->i_size;
	inc_nlink(dir);
932
	set_nlink(inode, 2);
933 934 935
	unlock_2_inodes(dir, inode);
	if (budgeted)
		ubifs_release_budget(c, &req);
936 937
out_fname:
	fscrypt_free_filename(&nm);
938 939 940
	return err;
}

941
static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
942 943 944 945
{
	struct inode *inode;
	struct ubifs_inode *dir_ui = ubifs_inode(dir);
	struct ubifs_info *c = dir->i_sb->s_fs_info;
946
	int err, sz_change;
947
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
948
	struct fscrypt_name nm;
949 950 951 952 953 954

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

A
Al Viro 已提交
955 956
	dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
		dentry, mode, dir->i_ino);
957 958 959 960 961

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

962 963 964 965 966 967
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
	if (err)
		goto out_budg;

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

968 969 970
	inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
971
		goto out_fname;
972 973
	}

974 975
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
976
		goto out_inode;
977

978 979 980 981 982 983 984
	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;
985
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
986
	if (err) {
987
		ubifs_err(c, "cannot create directory, error %d", err);
988 989 990 991 992 993
		goto out_cancel;
	}
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	d_instantiate(dentry, inode);
994
	fscrypt_free_filename(&nm);
995 996 997 998 999 1000 1001
	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);
1002
out_inode:
1003 1004
	make_bad_inode(inode);
	iput(inode);
1005 1006
out_fname:
	fscrypt_free_filename(&nm);
1007 1008 1009 1010 1011 1012
out_budg:
	ubifs_release_budget(c, &req);
	return err;
}

static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
1013
		       umode_t mode, dev_t rdev)
1014 1015 1016 1017 1018 1019
{
	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;
1020
	int sz_change;
1021 1022
	int err, devlen = 0;
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1023
					.dirtied_ino = 1 };
1024
	struct fscrypt_name nm;
1025 1026 1027 1028 1029 1030

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

A
Al Viro 已提交
1031
	dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
1032 1033 1034 1035 1036 1037 1038 1039

	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);
	}

1040
	req.new_ino_d = ALIGN(devlen, 8);
1041 1042 1043 1044 1045 1046
	err = ubifs_budget_space(c, &req);
	if (err) {
		kfree(dev);
		return err;
	}

1047
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1048 1049
	if (err) {
		kfree(dev);
1050
		goto out_budg;
1051
	}
1052 1053 1054

	sz_change = CALC_DENT_SIZE(fname_len(&nm));

1055 1056 1057 1058
	inode = ubifs_new_inode(c, dir, mode);
	if (IS_ERR(inode)) {
		kfree(dev);
		err = PTR_ERR(inode);
1059
		goto out_fname;
1060 1061 1062 1063 1064 1065 1066 1067
	}

	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;

1068 1069
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
1070
		goto out_inode;
1071

1072 1073 1074 1075
	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;
1076
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1077 1078 1079 1080 1081 1082 1083
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
1084
	fscrypt_free_filename(&nm);
1085 1086 1087 1088 1089 1090
	return 0;

out_cancel:
	dir->i_size -= sz_change;
	dir_ui->ui_size = dir->i_size;
	mutex_unlock(&dir_ui->ui_mutex);
1091
out_inode:
1092 1093
	make_bad_inode(inode);
	iput(inode);
1094 1095
out_fname:
	fscrypt_free_filename(&nm);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
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;
1108
	int err, sz_change, len = strlen(symname);
1109
	struct fscrypt_str disk_link;
1110
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1111 1112
					.new_ino_d = ALIGN(len, 8),
					.dirtied_ino = 1 };
1113 1114
	struct fscrypt_name nm;

1115 1116
	dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
		symname, dir->i_ino);
1117

1118 1119 1120 1121
	err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
				      &disk_link);
	if (err)
		return err;
1122 1123 1124 1125 1126 1127 1128 1129 1130

	/*
	 * Budget request settings: new inode, new direntry and changing parent
	 * directory inode.
	 */
	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

1131 1132 1133 1134
	err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
	if (err)
		goto out_budg;

1135 1136
	sz_change = CALC_DENT_SIZE(fname_len(&nm));

1137 1138 1139
	inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
1140
		goto out_fname;
1141 1142 1143
	}

	ui = ubifs_inode(inode);
1144
	ui->data = kmalloc(disk_link.len, GFP_NOFS);
1145 1146 1147 1148 1149
	if (!ui->data) {
		err = -ENOMEM;
		goto out_inode;
	}

1150 1151 1152
	if (IS_ENCRYPTED(inode)) {
		disk_link.name = ui->data; /* encrypt directly into ui->data */
		err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
1153
		if (err)
1154 1155
			goto out_inode;
	} else {
1156
		memcpy(ui->data, disk_link.name, disk_link.len);
1157 1158 1159
		inode->i_link = ui->data;
	}

1160 1161 1162
	/*
	 * 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,
1163
	 * data length is @disk_link.len - 1, not @disk_link.len.
1164
	 */
1165 1166
	ui->data_len = disk_link.len - 1;
	inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
1167

1168 1169
	err = ubifs_init_security(dir, inode, &dentry->d_name);
	if (err)
1170
		goto out_inode;
1171

1172 1173 1174 1175
	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;
1176
	err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1177 1178 1179 1180 1181 1182
	if (err)
		goto out_cancel;
	mutex_unlock(&dir_ui->ui_mutex);

	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
1183 1184
	err = 0;
	goto out_fname;
1185 1186 1187 1188 1189 1190 1191 1192

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);
1193 1194
out_fname:
	fscrypt_free_filename(&nm);
1195 1196 1197 1198 1199 1200
out_budg:
	ubifs_release_budget(c, &req);
	return err;
}

/**
1201
 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1202 1203 1204
 * @inode1: first inode
 * @inode2: second inode
 * @inode3: third inode
1205
 * @inode4: fouth inode
1206
 *
A
Artem Bityutskiy 已提交
1207
 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1208
 * @inode2 whereas @inode3 and @inode4 may be %NULL.
A
Artem Bityutskiy 已提交
1209 1210 1211 1212
 *
 * 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.
1213
 */
1214 1215
static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
			  struct inode *inode3, struct inode *inode4)
1216
{
A
Artem Bityutskiy 已提交
1217 1218 1219 1220 1221
	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);
1222 1223
	if (inode4)
		mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1224 1225 1226
}

/**
1227
 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1228 1229 1230
 * @inode1: first inode
 * @inode2: second inode
 * @inode3: third inode
1231
 * @inode4: fouth inode
1232
 */
1233 1234
static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
			    struct inode *inode3, struct inode *inode4)
1235
{
1236 1237
	if (inode4)
		mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1238 1239
	if (inode3)
		mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
A
Artem Bityutskiy 已提交
1240 1241 1242
	if (inode1 != inode2)
		mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
	mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1243 1244
}

R
Richard Weinberger 已提交
1245 1246 1247
static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
		     struct inode *new_dir, struct dentry *new_dentry,
		     unsigned int flags)
1248 1249
{
	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1250 1251
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
1252
	struct inode *whiteout = NULL;
1253
	struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1254
	struct ubifs_inode *whiteout_ui = NULL;
1255 1256
	int err, release, sync = 0, move = (new_dir != old_dir);
	int is_dir = S_ISDIR(old_inode->i_mode);
1257
	int unlink = !!new_inode, new_sz, old_sz;
1258 1259 1260
	struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
					.dirtied_ino = 3 };
	struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1261
			.dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1262
	struct timespec64 time;
1263
	unsigned int uninitialized_var(saved_nlink);
1264
	struct fscrypt_name old_nm, new_nm;
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	/*
	 * 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.
	 */

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

1279
	if (unlink) {
1280
		ubifs_assert(c, inode_is_locked(new_inode));
A
Artem Bityutskiy 已提交
1281

1282 1283 1284 1285 1286
		err = ubifs_purge_xattrs(new_inode);
		if (err)
			return err;
	}

1287
	if (unlink && is_dir) {
1288
		err = ubifs_check_dir_empty(new_inode);
1289 1290 1291 1292
		if (err)
			return err;
	}

1293
	err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
1294 1295
	if (err)
		return err;
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311

	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;
	}
1312 1313
	err = ubifs_budget_space(c, &ino_req);
	if (err) {
1314 1315
		fscrypt_free_filename(&old_nm);
		fscrypt_free_filename(&new_nm);
1316 1317 1318 1319
		ubifs_release_budget(c, &req);
		return err;
	}

1320 1321 1322 1323 1324
	if (flags & RENAME_WHITEOUT) {
		union ubifs_dev_desc *dev = NULL;

		dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
		if (!dev) {
1325 1326
			err = -ENOMEM;
			goto out_release;
1327 1328 1329 1330 1331
		}

		err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
		if (err) {
			kfree(dev);
1332
			goto out_release;
1333 1334 1335 1336 1337 1338
		}

		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));
1339
		ubifs_assert(c, !whiteout_ui->dirty);
1340 1341 1342
	}

	lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1343 1344 1345 1346 1347

	/*
	 * Like most other Unix systems, set the @i_ctime for inodes on a
	 * rename.
	 */
1348
	time = current_time(old_dir);
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	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
1389
		 * directory, just clear @i_nlink.
1390
		 */
1391
		saved_nlink = new_inode->i_nlink;
1392
		if (is_dir)
1393 1394
			clear_nlink(new_inode);
		else
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
			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;
	}
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422

	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);
1423
			goto out_release;
1424 1425 1426 1427 1428 1429 1430 1431
		}

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

1432 1433
	err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
			       new_inode, &new_nm, whiteout, sync);
1434 1435 1436
	if (err)
		goto out_cancel;

1437
	unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	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))
1448
		err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
1449 1450 1451

	fscrypt_free_filename(&old_nm);
	fscrypt_free_filename(&new_nm);
1452 1453 1454 1455
	return err;

out_cancel:
	if (unlink) {
1456
		set_nlink(new_inode, saved_nlink);
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
	} 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);
		}
	}
1473 1474 1475 1476 1477
	if (whiteout) {
		drop_nlink(whiteout);
		iput(whiteout);
	}
	unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1478
out_release:
1479 1480
	ubifs_release_budget(c, &ino_req);
	ubifs_release_budget(c, &req);
1481 1482
	fscrypt_free_filename(&old_nm);
	fscrypt_free_filename(&new_nm);
1483 1484 1485
	return err;
}

1486 1487 1488 1489 1490 1491 1492 1493 1494
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);
1495
	struct timespec64 time;
1496
	int err;
1497
	struct fscrypt_name fst_nm, snd_nm;
1498

1499
	ubifs_assert(c, fst_inode && snd_inode);
1500

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	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;
	}

1511 1512
	lock_4_inodes(old_dir, new_dir, NULL, NULL);

1513
	time = current_time(old_dir);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
	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);
		}
	}

1530 1531
	err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
				snd_inode, &snd_nm, sync);
1532 1533 1534 1535

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

1536 1537
	fscrypt_free_filename(&fst_nm);
	fscrypt_free_filename(&snd_nm);
1538 1539 1540
	return err;
}

R
Richard Weinberger 已提交
1541
static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1542 1543 1544
			struct inode *new_dir, struct dentry *new_dentry,
			unsigned int flags)
{
1545
	int err;
1546
	struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1547

1548 1549 1550
	if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
		return -EINVAL;

1551 1552
	ubifs_assert(c, inode_is_locked(old_dir));
	ubifs_assert(c, inode_is_locked(new_dir));
1553

1554 1555 1556 1557 1558
	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
				     flags);
	if (err)
		return err;

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

R
Richard Weinberger 已提交
1562
	return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1563 1564
}

1565 1566
int ubifs_getattr(const struct path *path, struct kstat *stat,
		  u32 request_mask, unsigned int flags)
1567 1568
{
	loff_t size;
1569
	struct inode *inode = d_inode(path->dentry);
1570 1571 1572
	struct ubifs_inode *ui = ubifs_inode(inode);

	mutex_lock(&ui->ui_mutex);
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587

	if (ui->flags & UBIFS_APPEND_FL)
		stat->attributes |= STATX_ATTR_APPEND;
	if (ui->flags & UBIFS_COMPR_FL)
		stat->attributes |= STATX_ATTR_COMPRESSED;
	if (ui->flags & UBIFS_CRYPT_FL)
		stat->attributes |= STATX_ATTR_ENCRYPTED;
	if (ui->flags & UBIFS_IMMUTABLE_FL)
		stat->attributes |= STATX_ATTR_IMMUTABLE;

	stat->attributes_mask |= (STATX_ATTR_APPEND |
				STATX_ATTR_COMPRESSED |
				STATX_ATTR_ENCRYPTED |
				STATX_ATTR_IMMUTABLE);

A
Al Viro 已提交
1588
	generic_fillattr(inode, stat);
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	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;
}

1619 1620
static int ubifs_dir_open(struct inode *dir, struct file *file)
{
1621
	if (IS_ENCRYPTED(dir))
1622 1623 1624 1625 1626
		return fscrypt_get_encryption_info(dir) ? -EACCES : 0;

	return 0;
}

A
Artem Bityutskiy 已提交
1627
const struct inode_operations ubifs_dir_inode_operations = {
1628 1629 1630 1631 1632 1633 1634 1635
	.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 已提交
1636
	.rename      = ubifs_rename,
1637 1638
	.setattr     = ubifs_setattr,
	.getattr     = ubifs_getattr,
1639
#ifdef CONFIG_UBIFS_FS_XATTR
1640
	.listxattr   = ubifs_listxattr,
1641
#endif
1642
	.update_time = ubifs_update_time,
R
Richard Weinberger 已提交
1643
	.tmpfile     = ubifs_tmpfile,
1644 1645
};

A
Artem Bityutskiy 已提交
1646
const struct file_operations ubifs_dir_operations = {
A
Al Viro 已提交
1647
	.llseek         = generic_file_llseek,
1648 1649
	.release        = ubifs_dir_release,
	.read           = generic_read_dir,
1650
	.iterate_shared = ubifs_readdir,
1651 1652
	.fsync          = ubifs_fsync,
	.unlocked_ioctl = ubifs_ioctl,
1653
	.open		= ubifs_dir_open,
1654 1655 1656 1657
#ifdef CONFIG_COMPAT
	.compat_ioctl   = ubifs_compat_ioctl,
#endif
};