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