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