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