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