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