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