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