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