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