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