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