GNU Linux-libre 4.9.308-gnu1
[releases.git] / fs / f2fs / namei.c
1 /*
2  * fs/f2fs/namei.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/pagemap.h>
14 #include <linux/sched.h>
15 #include <linux/ctype.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
18
19 #include "f2fs.h"
20 #include "node.h"
21 #include "xattr.h"
22 #include "acl.h"
23 #include <trace/events/f2fs.h>
24
25 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
26 {
27         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
28         nid_t ino;
29         struct inode *inode;
30         bool nid_free = false;
31         int err;
32
33         inode = new_inode(dir->i_sb);
34         if (!inode)
35                 return ERR_PTR(-ENOMEM);
36
37         f2fs_lock_op(sbi);
38         if (!alloc_nid(sbi, &ino)) {
39                 f2fs_unlock_op(sbi);
40                 err = -ENOSPC;
41                 goto fail;
42         }
43         f2fs_unlock_op(sbi);
44
45         inode_init_owner(inode, dir, mode);
46
47         inode->i_ino = ino;
48         inode->i_blocks = 0;
49         inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
50         inode->i_generation = sbi->s_next_generation++;
51
52         err = insert_inode_locked(inode);
53         if (err) {
54                 err = -EINVAL;
55                 nid_free = true;
56                 goto fail;
57         }
58
59         /* If the directory encrypted, then we should encrypt the inode. */
60         if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
61                 f2fs_set_encrypted_inode(inode);
62
63         set_inode_flag(inode, FI_NEW_INODE);
64
65         if (test_opt(sbi, INLINE_XATTR))
66                 set_inode_flag(inode, FI_INLINE_XATTR);
67         if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
68                 set_inode_flag(inode, FI_INLINE_DATA);
69         if (f2fs_may_inline_dentry(inode))
70                 set_inode_flag(inode, FI_INLINE_DENTRY);
71
72         f2fs_init_extent_tree(inode, NULL);
73
74         stat_inc_inline_xattr(inode);
75         stat_inc_inline_inode(inode);
76         stat_inc_inline_dir(inode);
77
78         trace_f2fs_new_inode(inode, 0);
79         return inode;
80
81 fail:
82         trace_f2fs_new_inode(inode, err);
83         make_bad_inode(inode);
84         if (nid_free)
85                 set_inode_flag(inode, FI_FREE_NID);
86         iput(inode);
87         return ERR_PTR(err);
88 }
89
90 static int is_multimedia_file(const unsigned char *s, const char *sub)
91 {
92         size_t slen = strlen(s);
93         size_t sublen = strlen(sub);
94         int i;
95
96         /*
97          * filename format of multimedia file should be defined as:
98          * "filename + '.' + extension + (optional: '.' + temp extension)".
99          */
100         if (slen < sublen + 2)
101                 return 0;
102
103         for (i = 1; i < slen - sublen; i++) {
104                 if (s[i] != '.')
105                         continue;
106                 if (!strncasecmp(s + i + 1, sub, sublen))
107                         return 1;
108         }
109
110         return 0;
111 }
112
113 /*
114  * Set multimedia files as cold files for hot/cold data separation
115  */
116 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
117                 const unsigned char *name)
118 {
119         int i;
120         __u8 (*extlist)[8] = sbi->raw_super->extension_list;
121
122         int count = le32_to_cpu(sbi->raw_super->extension_count);
123         for (i = 0; i < count; i++) {
124                 if (is_multimedia_file(name, extlist[i])) {
125                         file_set_cold(inode);
126                         break;
127                 }
128         }
129 }
130
131 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
132                                                 bool excl)
133 {
134         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
135         struct inode *inode;
136         nid_t ino = 0;
137         int err;
138
139         inode = f2fs_new_inode(dir, mode);
140         if (IS_ERR(inode))
141                 return PTR_ERR(inode);
142
143         if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
144                 set_cold_files(sbi, inode, dentry->d_name.name);
145
146         inode->i_op = &f2fs_file_inode_operations;
147         inode->i_fop = &f2fs_file_operations;
148         inode->i_mapping->a_ops = &f2fs_dblock_aops;
149         ino = inode->i_ino;
150
151         f2fs_balance_fs(sbi, true);
152
153         f2fs_lock_op(sbi);
154         err = f2fs_add_link(dentry, inode);
155         if (err)
156                 goto out;
157         f2fs_unlock_op(sbi);
158
159         alloc_nid_done(sbi, ino);
160
161         d_instantiate_new(dentry, inode);
162
163         if (IS_DIRSYNC(dir))
164                 f2fs_sync_fs(sbi->sb, 1);
165         return 0;
166 out:
167         handle_failed_inode(inode);
168         return err;
169 }
170
171 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
172                 struct dentry *dentry)
173 {
174         struct inode *inode = d_inode(old_dentry);
175         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
176         int err;
177
178         if (f2fs_encrypted_inode(dir) &&
179                         !fscrypt_has_permitted_context(dir, inode))
180                 return -EXDEV;
181
182         f2fs_balance_fs(sbi, true);
183
184         inode->i_ctime = current_time(inode);
185         ihold(inode);
186
187         set_inode_flag(inode, FI_INC_LINK);
188         f2fs_lock_op(sbi);
189         err = f2fs_add_link(dentry, inode);
190         if (err)
191                 goto out;
192         f2fs_unlock_op(sbi);
193
194         d_instantiate(dentry, inode);
195
196         if (IS_DIRSYNC(dir))
197                 f2fs_sync_fs(sbi->sb, 1);
198         return 0;
199 out:
200         clear_inode_flag(inode, FI_INC_LINK);
201         iput(inode);
202         f2fs_unlock_op(sbi);
203         return err;
204 }
205
206 struct dentry *f2fs_get_parent(struct dentry *child)
207 {
208         struct qstr dotdot = QSTR_INIT("..", 2);
209         struct page *page;
210         unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
211         if (!ino) {
212                 if (IS_ERR(page))
213                         return ERR_CAST(page);
214                 return ERR_PTR(-ENOENT);
215         }
216         return d_obtain_alias(f2fs_iget(child->d_sb, ino));
217 }
218
219 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
220 {
221         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
222         struct qstr dot = QSTR_INIT(".", 1);
223         struct qstr dotdot = QSTR_INIT("..", 2);
224         struct f2fs_dir_entry *de;
225         struct page *page;
226         int err = 0;
227
228         if (f2fs_readonly(sbi->sb)) {
229                 f2fs_msg(sbi->sb, KERN_INFO,
230                         "skip recovering inline_dots inode (ino:%lu, pino:%u) "
231                         "in readonly mountpoint", dir->i_ino, pino);
232                 return 0;
233         }
234
235         f2fs_balance_fs(sbi, true);
236
237         f2fs_lock_op(sbi);
238
239         de = f2fs_find_entry(dir, &dot, &page);
240         if (de) {
241                 f2fs_dentry_kunmap(dir, page);
242                 f2fs_put_page(page, 0);
243         } else if (IS_ERR(page)) {
244                 err = PTR_ERR(page);
245                 goto out;
246         } else {
247                 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
248                 if (err)
249                         goto out;
250         }
251
252         de = f2fs_find_entry(dir, &dotdot, &page);
253         if (de) {
254                 f2fs_dentry_kunmap(dir, page);
255                 f2fs_put_page(page, 0);
256         } else if (IS_ERR(page)) {
257                 err = PTR_ERR(page);
258         } else {
259                 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
260         }
261 out:
262         if (!err)
263                 clear_inode_flag(dir, FI_INLINE_DOTS);
264
265         f2fs_unlock_op(sbi);
266         return err;
267 }
268
269 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
270                 unsigned int flags)
271 {
272         struct inode *inode = NULL;
273         struct f2fs_dir_entry *de;
274         struct page *page;
275         nid_t ino;
276         int err = 0;
277         unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
278
279         if (f2fs_encrypted_inode(dir)) {
280                 int res = fscrypt_get_encryption_info(dir);
281
282                 /*
283                  * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
284                  * created while the directory was encrypted and we
285                  * don't have access to the key.
286                  */
287                 if (fscrypt_has_encryption_key(dir))
288                         fscrypt_set_encrypted_dentry(dentry);
289                 fscrypt_set_d_op(dentry);
290                 if (res && res != -ENOKEY)
291                         return ERR_PTR(res);
292         }
293
294         if (dentry->d_name.len > F2FS_NAME_LEN)
295                 return ERR_PTR(-ENAMETOOLONG);
296
297         de = f2fs_find_entry(dir, &dentry->d_name, &page);
298         if (!de) {
299                 if (IS_ERR(page))
300                         return (struct dentry *)page;
301                 return d_splice_alias(inode, dentry);
302         }
303
304         ino = le32_to_cpu(de->ino);
305         f2fs_dentry_kunmap(dir, page);
306         f2fs_put_page(page, 0);
307
308         inode = f2fs_iget(dir->i_sb, ino);
309         if (IS_ERR(inode))
310                 return ERR_CAST(inode);
311
312         if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
313                 err = __recover_dot_dentries(dir, root_ino);
314                 if (err)
315                         goto err_out;
316         }
317
318         if (f2fs_has_inline_dots(inode)) {
319                 err = __recover_dot_dentries(inode, dir->i_ino);
320                 if (err)
321                         goto err_out;
322         }
323         if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) &&
324                         (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
325                         !fscrypt_has_permitted_context(dir, inode)) {
326                 bool nokey = f2fs_encrypted_inode(inode) &&
327                         !fscrypt_has_encryption_key(inode);
328                 err = nokey ? -ENOKEY : -EPERM;
329                 goto err_out;
330         }
331         return d_splice_alias(inode, dentry);
332
333 err_out:
334         iput(inode);
335         return ERR_PTR(err);
336 }
337
338 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
339 {
340         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
341         struct inode *inode = d_inode(dentry);
342         struct f2fs_dir_entry *de;
343         struct page *page;
344         int err = -ENOENT;
345
346         trace_f2fs_unlink_enter(dir, dentry);
347
348         de = f2fs_find_entry(dir, &dentry->d_name, &page);
349         if (!de) {
350                 if (IS_ERR(page))
351                         err = PTR_ERR(page);
352                 goto fail;
353         }
354
355         f2fs_balance_fs(sbi, true);
356
357         f2fs_lock_op(sbi);
358         err = acquire_orphan_inode(sbi);
359         if (err) {
360                 f2fs_unlock_op(sbi);
361                 f2fs_dentry_kunmap(dir, page);
362                 f2fs_put_page(page, 0);
363                 goto fail;
364         }
365         f2fs_delete_entry(de, page, dir, inode);
366         f2fs_unlock_op(sbi);
367
368         if (IS_DIRSYNC(dir))
369                 f2fs_sync_fs(sbi->sb, 1);
370 fail:
371         trace_f2fs_unlink_exit(inode, err);
372         return err;
373 }
374
375 static const char *f2fs_get_link(struct dentry *dentry,
376                                  struct inode *inode,
377                                  struct delayed_call *done)
378 {
379         const char *link = page_get_link(dentry, inode, done);
380         if (!IS_ERR(link) && !*link) {
381                 /* this is broken symlink case */
382                 do_delayed_call(done);
383                 clear_delayed_call(done);
384                 link = ERR_PTR(-ENOENT);
385         }
386         return link;
387 }
388
389 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
390                                         const char *symname)
391 {
392         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
393         struct inode *inode;
394         size_t len = strlen(symname);
395         struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
396         struct fscrypt_symlink_data *sd = NULL;
397         int err;
398
399         if (f2fs_encrypted_inode(dir)) {
400                 err = fscrypt_get_encryption_info(dir);
401                 if (err)
402                         return err;
403
404                 if (!fscrypt_has_encryption_key(dir))
405                         return -ENOKEY;
406
407                 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
408                                 sizeof(struct fscrypt_symlink_data));
409         }
410
411         if (disk_link.len > dir->i_sb->s_blocksize)
412                 return -ENAMETOOLONG;
413
414         inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
415         if (IS_ERR(inode))
416                 return PTR_ERR(inode);
417
418         if (f2fs_encrypted_inode(inode))
419                 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
420         else
421                 inode->i_op = &f2fs_symlink_inode_operations;
422         inode_nohighmem(inode);
423         inode->i_mapping->a_ops = &f2fs_dblock_aops;
424
425         f2fs_balance_fs(sbi, true);
426
427         f2fs_lock_op(sbi);
428         err = f2fs_add_link(dentry, inode);
429         if (err)
430                 goto out;
431         f2fs_unlock_op(sbi);
432         alloc_nid_done(sbi, inode->i_ino);
433
434         if (f2fs_encrypted_inode(inode)) {
435                 struct qstr istr = QSTR_INIT(symname, len);
436                 struct fscrypt_str ostr;
437
438                 sd = kzalloc(disk_link.len, GFP_NOFS);
439                 if (!sd) {
440                         err = -ENOMEM;
441                         goto err_out;
442                 }
443
444                 err = fscrypt_get_encryption_info(inode);
445                 if (err)
446                         goto err_out;
447
448                 if (!fscrypt_has_encryption_key(inode)) {
449                         err = -ENOKEY;
450                         goto err_out;
451                 }
452
453                 ostr.name = sd->encrypted_path;
454                 ostr.len = disk_link.len;
455                 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
456                 if (err)
457                         goto err_out;
458
459                 sd->len = cpu_to_le16(ostr.len);
460                 disk_link.name = (char *)sd;
461         }
462
463         err = page_symlink(inode, disk_link.name, disk_link.len);
464
465 err_out:
466         d_instantiate_new(dentry, inode);
467
468         /*
469          * Let's flush symlink data in order to avoid broken symlink as much as
470          * possible. Nevertheless, fsyncing is the best way, but there is no
471          * way to get a file descriptor in order to flush that.
472          *
473          * Note that, it needs to do dir->fsync to make this recoverable.
474          * If the symlink path is stored into inline_data, there is no
475          * performance regression.
476          */
477         if (!err) {
478                 filemap_write_and_wait_range(inode->i_mapping, 0,
479                                                         disk_link.len - 1);
480
481                 if (IS_DIRSYNC(dir))
482                         f2fs_sync_fs(sbi->sb, 1);
483         } else {
484                 f2fs_unlink(dir, dentry);
485         }
486
487         kfree(sd);
488         return err;
489 out:
490         handle_failed_inode(inode);
491         return err;
492 }
493
494 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
495 {
496         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
497         struct inode *inode;
498         int err;
499
500         inode = f2fs_new_inode(dir, S_IFDIR | mode);
501         if (IS_ERR(inode))
502                 return PTR_ERR(inode);
503
504         inode->i_op = &f2fs_dir_inode_operations;
505         inode->i_fop = &f2fs_dir_operations;
506         inode->i_mapping->a_ops = &f2fs_dblock_aops;
507         mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
508
509         f2fs_balance_fs(sbi, true);
510
511         set_inode_flag(inode, FI_INC_LINK);
512         f2fs_lock_op(sbi);
513         err = f2fs_add_link(dentry, inode);
514         if (err)
515                 goto out_fail;
516         f2fs_unlock_op(sbi);
517
518         alloc_nid_done(sbi, inode->i_ino);
519
520         d_instantiate_new(dentry, inode);
521
522         if (IS_DIRSYNC(dir))
523                 f2fs_sync_fs(sbi->sb, 1);
524         return 0;
525
526 out_fail:
527         clear_inode_flag(inode, FI_INC_LINK);
528         handle_failed_inode(inode);
529         return err;
530 }
531
532 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
533 {
534         struct inode *inode = d_inode(dentry);
535         if (f2fs_empty_dir(inode))
536                 return f2fs_unlink(dir, dentry);
537         return -ENOTEMPTY;
538 }
539
540 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
541                                 umode_t mode, dev_t rdev)
542 {
543         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
544         struct inode *inode;
545         int err = 0;
546
547         inode = f2fs_new_inode(dir, mode);
548         if (IS_ERR(inode))
549                 return PTR_ERR(inode);
550
551         init_special_inode(inode, inode->i_mode, rdev);
552         inode->i_op = &f2fs_special_inode_operations;
553
554         f2fs_balance_fs(sbi, true);
555
556         f2fs_lock_op(sbi);
557         err = f2fs_add_link(dentry, inode);
558         if (err)
559                 goto out;
560         f2fs_unlock_op(sbi);
561
562         alloc_nid_done(sbi, inode->i_ino);
563
564         d_instantiate_new(dentry, inode);
565
566         if (IS_DIRSYNC(dir))
567                 f2fs_sync_fs(sbi->sb, 1);
568         return 0;
569 out:
570         handle_failed_inode(inode);
571         return err;
572 }
573
574 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
575                                         umode_t mode, struct inode **whiteout)
576 {
577         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
578         struct inode *inode;
579         int err;
580
581         inode = f2fs_new_inode(dir, mode);
582         if (IS_ERR(inode))
583                 return PTR_ERR(inode);
584
585         if (whiteout) {
586                 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
587                 inode->i_op = &f2fs_special_inode_operations;
588         } else {
589                 inode->i_op = &f2fs_file_inode_operations;
590                 inode->i_fop = &f2fs_file_operations;
591                 inode->i_mapping->a_ops = &f2fs_dblock_aops;
592         }
593
594         f2fs_balance_fs(sbi, true);
595
596         f2fs_lock_op(sbi);
597         err = acquire_orphan_inode(sbi);
598         if (err)
599                 goto out;
600
601         err = f2fs_do_tmpfile(inode, dir);
602         if (err)
603                 goto release_out;
604
605         /*
606          * add this non-linked tmpfile to orphan list, in this way we could
607          * remove all unused data of tmpfile after abnormal power-off.
608          */
609         add_orphan_inode(inode);
610         alloc_nid_done(sbi, inode->i_ino);
611
612         if (whiteout) {
613                 f2fs_i_links_write(inode, false);
614                 *whiteout = inode;
615         } else {
616                 d_tmpfile(dentry, inode);
617         }
618         /* link_count was changed by d_tmpfile as well. */
619         f2fs_unlock_op(sbi);
620         unlock_new_inode(inode);
621         return 0;
622
623 release_out:
624         release_orphan_inode(sbi);
625 out:
626         handle_failed_inode(inode);
627         return err;
628 }
629
630 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
631 {
632         if (f2fs_encrypted_inode(dir)) {
633                 int err = fscrypt_get_encryption_info(dir);
634                 if (err)
635                         return err;
636         }
637
638         return __f2fs_tmpfile(dir, dentry, mode, NULL);
639 }
640
641 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
642 {
643         return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
644 }
645
646 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
647                         struct inode *new_dir, struct dentry *new_dentry,
648                         unsigned int flags)
649 {
650         struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
651         struct inode *old_inode = d_inode(old_dentry);
652         struct inode *new_inode = d_inode(new_dentry);
653         struct inode *whiteout = NULL;
654         struct page *old_dir_page;
655         struct page *old_page, *new_page = NULL;
656         struct f2fs_dir_entry *old_dir_entry = NULL;
657         struct f2fs_dir_entry *old_entry;
658         struct f2fs_dir_entry *new_entry;
659         bool is_old_inline = f2fs_has_inline_dentry(old_dir);
660         int err = -ENOENT;
661
662         if ((f2fs_encrypted_inode(old_dir) &&
663                         !fscrypt_has_encryption_key(old_dir)) ||
664                         (f2fs_encrypted_inode(new_dir) &&
665                         !fscrypt_has_encryption_key(new_dir)))
666                 return -ENOKEY;
667
668         if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
669                         !fscrypt_has_permitted_context(new_dir, old_inode)) {
670                 err = -EXDEV;
671                 goto out;
672         }
673
674         old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
675         if (!old_entry) {
676                 if (IS_ERR(old_page))
677                         err = PTR_ERR(old_page);
678                 goto out;
679         }
680
681         if (S_ISDIR(old_inode->i_mode)) {
682                 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
683                 if (!old_dir_entry) {
684                         if (IS_ERR(old_dir_page))
685                                 err = PTR_ERR(old_dir_page);
686                         goto out_old;
687                 }
688         }
689
690         if (flags & RENAME_WHITEOUT) {
691                 err = f2fs_create_whiteout(old_dir, &whiteout);
692                 if (err)
693                         goto out_dir;
694         }
695
696         if (new_inode) {
697
698                 err = -ENOTEMPTY;
699                 if (old_dir_entry && !f2fs_empty_dir(new_inode))
700                         goto out_whiteout;
701
702                 err = -ENOENT;
703                 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
704                                                 &new_page);
705                 if (!new_entry) {
706                         if (IS_ERR(new_page))
707                                 err = PTR_ERR(new_page);
708                         goto out_whiteout;
709                 }
710
711                 f2fs_balance_fs(sbi, true);
712
713                 f2fs_lock_op(sbi);
714
715                 err = acquire_orphan_inode(sbi);
716                 if (err)
717                         goto put_out_dir;
718
719                 err = update_dent_inode(old_inode, new_inode,
720                                                 &new_dentry->d_name);
721                 if (err) {
722                         release_orphan_inode(sbi);
723                         goto put_out_dir;
724                 }
725
726                 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
727
728                 new_inode->i_ctime = current_time(new_inode);
729                 down_write(&F2FS_I(new_inode)->i_sem);
730                 if (old_dir_entry)
731                         f2fs_i_links_write(new_inode, false);
732                 f2fs_i_links_write(new_inode, false);
733                 up_write(&F2FS_I(new_inode)->i_sem);
734
735                 if (!new_inode->i_nlink)
736                         add_orphan_inode(new_inode);
737                 else
738                         release_orphan_inode(sbi);
739         } else {
740                 f2fs_balance_fs(sbi, true);
741
742                 f2fs_lock_op(sbi);
743
744                 err = f2fs_add_link(new_dentry, old_inode);
745                 if (err) {
746                         f2fs_unlock_op(sbi);
747                         goto out_whiteout;
748                 }
749
750                 if (old_dir_entry)
751                         f2fs_i_links_write(new_dir, true);
752
753                 /*
754                  * old entry and new entry can locate in the same inline
755                  * dentry in inode, when attaching new entry in inline dentry,
756                  * it could force inline dentry conversion, after that,
757                  * old_entry and old_page will point to wrong address, in
758                  * order to avoid this, let's do the check and update here.
759                  */
760                 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
761                         f2fs_put_page(old_page, 0);
762                         old_page = NULL;
763
764                         old_entry = f2fs_find_entry(old_dir,
765                                                 &old_dentry->d_name, &old_page);
766                         if (!old_entry) {
767                                 err = -ENOENT;
768                                 if (IS_ERR(old_page))
769                                         err = PTR_ERR(old_page);
770                                 f2fs_unlock_op(sbi);
771                                 goto out_whiteout;
772                         }
773                 }
774         }
775
776         down_write(&F2FS_I(old_inode)->i_sem);
777         file_lost_pino(old_inode);
778         if (new_inode && file_enc_name(new_inode))
779                 file_set_enc_name(old_inode);
780         up_write(&F2FS_I(old_inode)->i_sem);
781
782         old_inode->i_ctime = current_time(old_inode);
783         f2fs_mark_inode_dirty_sync(old_inode);
784
785         f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
786
787         if (whiteout) {
788                 whiteout->i_state |= I_LINKABLE;
789                 set_inode_flag(whiteout, FI_INC_LINK);
790                 err = f2fs_add_link(old_dentry, whiteout);
791                 if (err)
792                         goto put_out_dir;
793                 whiteout->i_state &= ~I_LINKABLE;
794                 iput(whiteout);
795         }
796
797         if (old_dir_entry) {
798                 if (old_dir != new_dir && !whiteout) {
799                         f2fs_set_link(old_inode, old_dir_entry,
800                                                 old_dir_page, new_dir);
801                 } else {
802                         f2fs_dentry_kunmap(old_inode, old_dir_page);
803                         f2fs_put_page(old_dir_page, 0);
804                 }
805                 f2fs_i_links_write(old_dir, false);
806         }
807
808         f2fs_unlock_op(sbi);
809
810         if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
811                 f2fs_sync_fs(sbi->sb, 1);
812         return 0;
813
814 put_out_dir:
815         f2fs_unlock_op(sbi);
816         if (new_page) {
817                 f2fs_dentry_kunmap(new_dir, new_page);
818                 f2fs_put_page(new_page, 0);
819         }
820 out_whiteout:
821         if (whiteout)
822                 iput(whiteout);
823 out_dir:
824         if (old_dir_entry) {
825                 f2fs_dentry_kunmap(old_inode, old_dir_page);
826                 f2fs_put_page(old_dir_page, 0);
827         }
828 out_old:
829         f2fs_dentry_kunmap(old_dir, old_page);
830         f2fs_put_page(old_page, 0);
831 out:
832         return err;
833 }
834
835 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
836                              struct inode *new_dir, struct dentry *new_dentry)
837 {
838         struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
839         struct inode *old_inode = d_inode(old_dentry);
840         struct inode *new_inode = d_inode(new_dentry);
841         struct page *old_dir_page, *new_dir_page;
842         struct page *old_page, *new_page;
843         struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
844         struct f2fs_dir_entry *old_entry, *new_entry;
845         int old_nlink = 0, new_nlink = 0;
846         int err = -ENOENT;
847
848         if ((f2fs_encrypted_inode(old_dir) &&
849                         !fscrypt_has_encryption_key(old_dir)) ||
850                         (f2fs_encrypted_inode(new_dir) &&
851                         !fscrypt_has_encryption_key(new_dir)))
852                 return -ENOKEY;
853
854         if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
855                         (old_dir != new_dir) &&
856                         (!fscrypt_has_permitted_context(new_dir, old_inode) ||
857                          !fscrypt_has_permitted_context(old_dir, new_inode)))
858                 return -EXDEV;
859
860         old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
861         if (!old_entry) {
862                 if (IS_ERR(old_page))
863                         err = PTR_ERR(old_page);
864                 goto out;
865         }
866
867         new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
868         if (!new_entry) {
869                 if (IS_ERR(new_page))
870                         err = PTR_ERR(new_page);
871                 goto out_old;
872         }
873
874         /* prepare for updating ".." directory entry info later */
875         if (old_dir != new_dir) {
876                 if (S_ISDIR(old_inode->i_mode)) {
877                         old_dir_entry = f2fs_parent_dir(old_inode,
878                                                         &old_dir_page);
879                         if (!old_dir_entry) {
880                                 if (IS_ERR(old_dir_page))
881                                         err = PTR_ERR(old_dir_page);
882                                 goto out_new;
883                         }
884                 }
885
886                 if (S_ISDIR(new_inode->i_mode)) {
887                         new_dir_entry = f2fs_parent_dir(new_inode,
888                                                         &new_dir_page);
889                         if (!new_dir_entry) {
890                                 if (IS_ERR(new_dir_page))
891                                         err = PTR_ERR(new_dir_page);
892                                 goto out_old_dir;
893                         }
894                 }
895         }
896
897         /*
898          * If cross rename between file and directory those are not
899          * in the same directory, we will inc nlink of file's parent
900          * later, so we should check upper boundary of its nlink.
901          */
902         if ((!old_dir_entry || !new_dir_entry) &&
903                                 old_dir_entry != new_dir_entry) {
904                 old_nlink = old_dir_entry ? -1 : 1;
905                 new_nlink = -old_nlink;
906                 err = -EMLINK;
907                 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
908                         (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
909                         goto out_new_dir;
910         }
911
912         f2fs_balance_fs(sbi, true);
913
914         f2fs_lock_op(sbi);
915
916         err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
917         if (err)
918                 goto out_unlock;
919         if (file_enc_name(new_inode))
920                 file_set_enc_name(old_inode);
921
922         err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
923         if (err)
924                 goto out_undo;
925         if (file_enc_name(old_inode))
926                 file_set_enc_name(new_inode);
927
928         /* update ".." directory entry info of old dentry */
929         if (old_dir_entry)
930                 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
931
932         /* update ".." directory entry info of new dentry */
933         if (new_dir_entry)
934                 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
935
936         /* update directory entry info of old dir inode */
937         f2fs_set_link(old_dir, old_entry, old_page, new_inode);
938
939         down_write(&F2FS_I(old_inode)->i_sem);
940         file_lost_pino(old_inode);
941         up_write(&F2FS_I(old_inode)->i_sem);
942
943         old_dir->i_ctime = current_time(old_dir);
944         if (old_nlink) {
945                 down_write(&F2FS_I(old_dir)->i_sem);
946                 f2fs_i_links_write(old_dir, old_nlink > 0);
947                 up_write(&F2FS_I(old_dir)->i_sem);
948         }
949         f2fs_mark_inode_dirty_sync(old_dir);
950
951         /* update directory entry info of new dir inode */
952         f2fs_set_link(new_dir, new_entry, new_page, old_inode);
953
954         down_write(&F2FS_I(new_inode)->i_sem);
955         file_lost_pino(new_inode);
956         up_write(&F2FS_I(new_inode)->i_sem);
957
958         new_dir->i_ctime = current_time(new_dir);
959         if (new_nlink) {
960                 down_write(&F2FS_I(new_dir)->i_sem);
961                 f2fs_i_links_write(new_dir, new_nlink > 0);
962                 up_write(&F2FS_I(new_dir)->i_sem);
963         }
964         f2fs_mark_inode_dirty_sync(new_dir);
965
966         f2fs_unlock_op(sbi);
967
968         if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
969                 f2fs_sync_fs(sbi->sb, 1);
970         return 0;
971 out_undo:
972         /*
973          * Still we may fail to recover name info of f2fs_inode here
974          * Drop it, once its name is set as encrypted
975          */
976         update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
977 out_unlock:
978         f2fs_unlock_op(sbi);
979 out_new_dir:
980         if (new_dir_entry) {
981                 f2fs_dentry_kunmap(new_inode, new_dir_page);
982                 f2fs_put_page(new_dir_page, 0);
983         }
984 out_old_dir:
985         if (old_dir_entry) {
986                 f2fs_dentry_kunmap(old_inode, old_dir_page);
987                 f2fs_put_page(old_dir_page, 0);
988         }
989 out_new:
990         f2fs_dentry_kunmap(new_dir, new_page);
991         f2fs_put_page(new_page, 0);
992 out_old:
993         f2fs_dentry_kunmap(old_dir, old_page);
994         f2fs_put_page(old_page, 0);
995 out:
996         return err;
997 }
998
999 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1000                         struct inode *new_dir, struct dentry *new_dentry,
1001                         unsigned int flags)
1002 {
1003         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1004                 return -EINVAL;
1005
1006         if (flags & RENAME_EXCHANGE) {
1007                 return f2fs_cross_rename(old_dir, old_dentry,
1008                                          new_dir, new_dentry);
1009         }
1010         /*
1011          * VFS has already handled the new dentry existence case,
1012          * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1013          */
1014         return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1015 }
1016
1017 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1018                                            struct inode *inode,
1019                                            struct delayed_call *done)
1020 {
1021         struct page *cpage = NULL;
1022         char *caddr, *paddr = NULL;
1023         struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
1024         struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
1025         struct fscrypt_symlink_data *sd;
1026         u32 max_size = inode->i_sb->s_blocksize;
1027         int res;
1028
1029         if (!dentry)
1030                 return ERR_PTR(-ECHILD);
1031
1032         res = fscrypt_get_encryption_info(inode);
1033         if (res)
1034                 return ERR_PTR(res);
1035
1036         cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1037         if (IS_ERR(cpage))
1038                 return ERR_CAST(cpage);
1039         caddr = page_address(cpage);
1040
1041         /* Symlink is encrypted */
1042         sd = (struct fscrypt_symlink_data *)caddr;
1043         cstr.name = sd->encrypted_path;
1044         cstr.len = le16_to_cpu(sd->len);
1045
1046         /* this is broken symlink case */
1047         if (unlikely(cstr.len == 0)) {
1048                 res = -ENOENT;
1049                 goto errout;
1050         }
1051
1052         if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
1053                 /* Symlink data on the disk is corrupted */
1054                 res = -EIO;
1055                 goto errout;
1056         }
1057         res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
1058         if (res)
1059                 goto errout;
1060
1061         res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1062         if (res)
1063                 goto errout;
1064
1065         /* this is broken symlink case */
1066         if (unlikely(pstr.name[0] == 0)) {
1067                 res = -ENOENT;
1068                 goto errout;
1069         }
1070
1071         paddr = pstr.name;
1072
1073         /* Null-terminate the name */
1074         paddr[pstr.len] = '\0';
1075
1076         put_page(cpage);
1077         set_delayed_call(done, kfree_link, paddr);
1078         return paddr;
1079 errout:
1080         fscrypt_fname_free_buffer(&pstr);
1081         put_page(cpage);
1082         return ERR_PTR(res);
1083 }
1084
1085 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1086         .readlink       = generic_readlink,
1087         .get_link       = f2fs_encrypted_get_link,
1088         .getattr        = f2fs_getattr,
1089         .setattr        = f2fs_setattr,
1090 #ifdef CONFIG_F2FS_FS_XATTR
1091         .listxattr      = f2fs_listxattr,
1092 #endif
1093 };
1094
1095 const struct inode_operations f2fs_dir_inode_operations = {
1096         .create         = f2fs_create,
1097         .lookup         = f2fs_lookup,
1098         .link           = f2fs_link,
1099         .unlink         = f2fs_unlink,
1100         .symlink        = f2fs_symlink,
1101         .mkdir          = f2fs_mkdir,
1102         .rmdir          = f2fs_rmdir,
1103         .mknod          = f2fs_mknod,
1104         .rename         = f2fs_rename2,
1105         .tmpfile        = f2fs_tmpfile,
1106         .getattr        = f2fs_getattr,
1107         .setattr        = f2fs_setattr,
1108         .get_acl        = f2fs_get_acl,
1109         .set_acl        = f2fs_set_acl,
1110 #ifdef CONFIG_F2FS_FS_XATTR
1111         .listxattr      = f2fs_listxattr,
1112 #endif
1113 };
1114
1115 const struct inode_operations f2fs_symlink_inode_operations = {
1116         .readlink       = generic_readlink,
1117         .get_link       = f2fs_get_link,
1118         .getattr        = f2fs_getattr,
1119         .setattr        = f2fs_setattr,
1120 #ifdef CONFIG_F2FS_FS_XATTR
1121         .listxattr      = f2fs_listxattr,
1122 #endif
1123 };
1124
1125 const struct inode_operations f2fs_special_inode_operations = {
1126         .getattr        = f2fs_getattr,
1127         .setattr        = f2fs_setattr,
1128         .get_acl        = f2fs_get_acl,
1129         .set_acl        = f2fs_set_acl,
1130 #ifdef CONFIG_F2FS_FS_XATTR
1131         .listxattr      = f2fs_listxattr,
1132 #endif
1133 };