Ryusuke Konishi | ae98043 | 2018-09-04 15:46:30 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 2 | /* |
Ryusuke Konishi | 94ee1d9 | 2021-11-08 18:35:01 -0800 | [diff] [blame] | 3 | * NILFS pathname lookup operations. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 6 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame] | 7 | * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 8 | */ |
| 9 | /* |
| 10 | * linux/fs/ext2/namei.c |
| 11 | * |
| 12 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 13 | * Remy Card (card@masi.ibp.fr) |
| 14 | * Laboratoire MASI - Institut Blaise Pascal |
| 15 | * Universite Pierre et Marie Curie (Paris VI) |
| 16 | * |
| 17 | * from |
| 18 | * |
| 19 | * linux/fs/minix/namei.c |
| 20 | * |
| 21 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 22 | * |
| 23 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 24 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 25 | */ |
| 26 | |
| 27 | #include <linux/pagemap.h> |
| 28 | #include "nilfs.h" |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 29 | #include "export.h" |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 30 | |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 31 | #define NILFS_FID_SIZE_NON_CONNECTABLE \ |
| 32 | (offsetof(struct nilfs_fid, parent_gen) / 4) |
| 33 | #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 34 | |
| 35 | static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode) |
| 36 | { |
| 37 | int err = nilfs_add_link(dentry, inode); |
Ryusuke Konishi | 4ad364c | 2016-05-23 16:23:25 -0700 | [diff] [blame] | 38 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 39 | if (!err) { |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 40 | d_instantiate_new(dentry, inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | inode_dec_link_count(inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 44 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 45 | iput(inode); |
| 46 | return err; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Methods themselves. |
| 51 | */ |
| 52 | |
| 53 | static struct dentry * |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 54 | nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 55 | { |
| 56 | struct inode *inode; |
| 57 | ino_t ino; |
| 58 | |
| 59 | if (dentry->d_name.len > NILFS_NAME_LEN) |
| 60 | return ERR_PTR(-ENAMETOOLONG); |
| 61 | |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 62 | ino = nilfs_inode_by_name(dir, &dentry->d_name); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 63 | inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 64 | return d_splice_alias(inode, dentry); |
| 65 | } |
| 66 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 67 | /* |
| 68 | * By the time this is called, we already have created |
| 69 | * the directory cache entry for the new file, but it |
| 70 | * is so far negative - it has no inode. |
| 71 | * |
| 72 | * If the create succeeds, we fill in the inode information |
| 73 | * with d_instantiate(). |
| 74 | */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 75 | static int nilfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 76 | struct dentry *dentry, umode_t mode, bool excl) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 77 | { |
| 78 | struct inode *inode; |
| 79 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 80 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 81 | |
| 82 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 83 | if (err) |
| 84 | return err; |
| 85 | inode = nilfs_new_inode(dir, mode); |
| 86 | err = PTR_ERR(inode); |
| 87 | if (!IS_ERR(inode)) { |
| 88 | inode->i_op = &nilfs_file_inode_operations; |
| 89 | inode->i_fop = &nilfs_file_operations; |
| 90 | inode->i_mapping->a_ops = &nilfs_aops; |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 91 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 92 | err = nilfs_add_nondir(dentry, inode); |
| 93 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 94 | if (!err) |
| 95 | err = nilfs_transaction_commit(dir->i_sb); |
| 96 | else |
| 97 | nilfs_transaction_abort(dir->i_sb); |
| 98 | |
| 99 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 103 | nilfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 104 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 105 | { |
| 106 | struct inode *inode; |
| 107 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 108 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 109 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 110 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 111 | if (err) |
| 112 | return err; |
| 113 | inode = nilfs_new_inode(dir, mode); |
| 114 | err = PTR_ERR(inode); |
| 115 | if (!IS_ERR(inode)) { |
| 116 | init_special_inode(inode, inode->i_mode, rdev); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 117 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 118 | err = nilfs_add_nondir(dentry, inode); |
| 119 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 120 | if (!err) |
| 121 | err = nilfs_transaction_commit(dir->i_sb); |
| 122 | else |
| 123 | nilfs_transaction_abort(dir->i_sb); |
| 124 | |
| 125 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 128 | static int nilfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 129 | struct dentry *dentry, const char *symname) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 130 | { |
| 131 | struct nilfs_transaction_info ti; |
| 132 | struct super_block *sb = dir->i_sb; |
Ryusuke Konishi | 0c6c44c | 2016-05-23 16:23:39 -0700 | [diff] [blame] | 133 | unsigned int l = strlen(symname) + 1; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 134 | struct inode *inode; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 135 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 136 | |
| 137 | if (l > sb->s_blocksize) |
| 138 | return -ENAMETOOLONG; |
| 139 | |
| 140 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 141 | if (err) |
| 142 | return err; |
| 143 | |
Ryusuke Konishi | 3147db8 | 2017-11-17 15:29:46 -0800 | [diff] [blame] | 144 | inode = nilfs_new_inode(dir, S_IFLNK | 0777); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 145 | err = PTR_ERR(inode); |
| 146 | if (IS_ERR(inode)) |
| 147 | goto out; |
| 148 | |
| 149 | /* slow symlink */ |
| 150 | inode->i_op = &nilfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 151 | inode_nohighmem(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 152 | inode->i_mapping->a_ops = &nilfs_aops; |
| 153 | err = page_symlink(inode, symname, l); |
| 154 | if (err) |
| 155 | goto out_fail; |
| 156 | |
| 157 | /* mark_inode_dirty(inode); */ |
Jiro SEKIBA | 9ca941d | 2009-11-27 19:41:06 +0900 | [diff] [blame] | 158 | /* page_symlink() do this */ |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 159 | |
| 160 | err = nilfs_add_nondir(dentry, inode); |
| 161 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 162 | if (!err) |
| 163 | err = nilfs_transaction_commit(dir->i_sb); |
| 164 | else |
| 165 | nilfs_transaction_abort(dir->i_sb); |
| 166 | |
| 167 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 168 | |
| 169 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 170 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 171 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 172 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 173 | iput(inode); |
| 174 | goto out; |
| 175 | } |
| 176 | |
| 177 | static int nilfs_link(struct dentry *old_dentry, struct inode *dir, |
| 178 | struct dentry *dentry) |
| 179 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 180 | struct inode *inode = d_inode(old_dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 181 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 182 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 183 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 184 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 185 | if (err) |
| 186 | return err; |
| 187 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 188 | inode->i_ctime = current_time(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 189 | inode_inc_link_count(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 190 | ihold(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 191 | |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 192 | err = nilfs_add_link(dentry, inode); |
| 193 | if (!err) { |
| 194 | d_instantiate(dentry, inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 195 | err = nilfs_transaction_commit(dir->i_sb); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 196 | } else { |
| 197 | inode_dec_link_count(inode); |
| 198 | iput(inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 199 | nilfs_transaction_abort(dir->i_sb); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 200 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 201 | |
| 202 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 205 | static int nilfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 206 | struct dentry *dentry, umode_t mode) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 207 | { |
| 208 | struct inode *inode; |
| 209 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 210 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 211 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 212 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 213 | if (err) |
| 214 | return err; |
| 215 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 216 | inc_nlink(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 217 | |
| 218 | inode = nilfs_new_inode(dir, S_IFDIR | mode); |
| 219 | err = PTR_ERR(inode); |
| 220 | if (IS_ERR(inode)) |
| 221 | goto out_dir; |
| 222 | |
| 223 | inode->i_op = &nilfs_dir_inode_operations; |
| 224 | inode->i_fop = &nilfs_dir_operations; |
| 225 | inode->i_mapping->a_ops = &nilfs_aops; |
| 226 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 227 | inc_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 228 | |
| 229 | err = nilfs_make_empty(inode, dir); |
| 230 | if (err) |
| 231 | goto out_fail; |
| 232 | |
| 233 | err = nilfs_add_link(dentry, inode); |
| 234 | if (err) |
| 235 | goto out_fail; |
| 236 | |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 237 | nilfs_mark_inode_dirty(inode); |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 238 | d_instantiate_new(dentry, inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 239 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 240 | if (!err) |
| 241 | err = nilfs_transaction_commit(dir->i_sb); |
| 242 | else |
| 243 | nilfs_transaction_abort(dir->i_sb); |
| 244 | |
| 245 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 246 | |
| 247 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 248 | drop_nlink(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 249 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 250 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 251 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 252 | iput(inode); |
| 253 | out_dir: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 254 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 255 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 256 | goto out; |
| 257 | } |
| 258 | |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 259 | static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 260 | { |
| 261 | struct inode *inode; |
| 262 | struct nilfs_dir_entry *de; |
| 263 | struct page *page; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 264 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 265 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 266 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 267 | de = nilfs_find_entry(dir, &dentry->d_name, &page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 268 | if (!de) |
| 269 | goto out; |
| 270 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 271 | inode = d_inode(dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 272 | err = -EIO; |
| 273 | if (le64_to_cpu(de->inode) != inode->i_ino) |
| 274 | goto out; |
| 275 | |
| 276 | if (!inode->i_nlink) { |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 277 | nilfs_warn(inode->i_sb, |
| 278 | "deleting nonexistent file (ino=%lu), %d", |
| 279 | inode->i_ino, inode->i_nlink); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 280 | set_nlink(inode, 1); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 281 | } |
| 282 | err = nilfs_delete_entry(de, page); |
| 283 | if (err) |
| 284 | goto out; |
| 285 | |
| 286 | inode->i_ctime = dir->i_ctime; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 287 | drop_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 288 | err = 0; |
| 289 | out: |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 290 | return err; |
| 291 | } |
| 292 | |
| 293 | static int nilfs_unlink(struct inode *dir, struct dentry *dentry) |
| 294 | { |
| 295 | struct nilfs_transaction_info ti; |
| 296 | int err; |
| 297 | |
| 298 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 299 | if (err) |
| 300 | return err; |
| 301 | |
| 302 | err = nilfs_do_unlink(dir, dentry); |
| 303 | |
| 304 | if (!err) { |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 305 | nilfs_mark_inode_dirty(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 306 | nilfs_mark_inode_dirty(d_inode(dentry)); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 307 | err = nilfs_transaction_commit(dir->i_sb); |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 308 | } else |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 309 | nilfs_transaction_abort(dir->i_sb); |
| 310 | |
| 311 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int nilfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 315 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 316 | struct inode *inode = d_inode(dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 317 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 318 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 319 | |
| 320 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 321 | if (err) |
| 322 | return err; |
| 323 | |
| 324 | err = -ENOTEMPTY; |
| 325 | if (nilfs_empty_dir(inode)) { |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 326 | err = nilfs_do_unlink(dir, dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 327 | if (!err) { |
| 328 | inode->i_size = 0; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 329 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 330 | nilfs_mark_inode_dirty(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 331 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 332 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 335 | if (!err) |
| 336 | err = nilfs_transaction_commit(dir->i_sb); |
| 337 | else |
| 338 | nilfs_transaction_abort(dir->i_sb); |
| 339 | |
| 340 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 343 | static int nilfs_rename(struct user_namespace *mnt_userns, |
| 344 | struct inode *old_dir, struct dentry *old_dentry, |
| 345 | struct inode *new_dir, struct dentry *new_dentry, |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 346 | unsigned int flags) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 347 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 348 | struct inode *old_inode = d_inode(old_dentry); |
| 349 | struct inode *new_inode = d_inode(new_dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 350 | struct page *dir_page = NULL; |
| 351 | struct nilfs_dir_entry *dir_de = NULL; |
| 352 | struct page *old_page; |
| 353 | struct nilfs_dir_entry *old_de; |
| 354 | struct nilfs_transaction_info ti; |
| 355 | int err; |
| 356 | |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 357 | if (flags & ~RENAME_NOREPLACE) |
| 358 | return -EINVAL; |
| 359 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 360 | err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1); |
| 361 | if (unlikely(err)) |
| 362 | return err; |
| 363 | |
| 364 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 365 | old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 366 | if (!old_de) |
| 367 | goto out; |
| 368 | |
| 369 | if (S_ISDIR(old_inode->i_mode)) { |
| 370 | err = -EIO; |
| 371 | dir_de = nilfs_dotdot(old_inode, &dir_page); |
| 372 | if (!dir_de) |
| 373 | goto out_old; |
| 374 | } |
| 375 | |
| 376 | if (new_inode) { |
| 377 | struct page *new_page; |
| 378 | struct nilfs_dir_entry *new_de; |
| 379 | |
| 380 | err = -ENOTEMPTY; |
| 381 | if (dir_de && !nilfs_empty_dir(new_inode)) |
| 382 | goto out_dir; |
| 383 | |
| 384 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 385 | new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 386 | if (!new_de) |
| 387 | goto out_dir; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 388 | nilfs_set_link(new_dir, new_de, new_page, old_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 389 | nilfs_mark_inode_dirty(new_dir); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 390 | new_inode->i_ctime = current_time(new_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 391 | if (dir_de) |
| 392 | drop_nlink(new_inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 393 | drop_nlink(new_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 394 | nilfs_mark_inode_dirty(new_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 395 | } else { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 396 | err = nilfs_add_link(new_dentry, old_inode); |
Al Viro | 30eb43d | 2011-03-02 12:01:13 -0500 | [diff] [blame] | 397 | if (err) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 398 | goto out_dir; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 399 | if (dir_de) { |
| 400 | inc_nlink(new_dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 401 | nilfs_mark_inode_dirty(new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 402 | } |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Like most other Unix systems, set the ctime for inodes on a |
| 407 | * rename. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 408 | */ |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 409 | old_inode->i_ctime = current_time(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 410 | |
| 411 | nilfs_delete_entry(old_de, old_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 412 | |
| 413 | if (dir_de) { |
| 414 | nilfs_set_link(old_inode, dir_de, dir_page, new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 415 | drop_nlink(old_dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 416 | } |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 417 | nilfs_mark_inode_dirty(old_dir); |
| 418 | nilfs_mark_inode_dirty(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 419 | |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 420 | err = nilfs_transaction_commit(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 421 | return err; |
| 422 | |
| 423 | out_dir: |
| 424 | if (dir_de) { |
| 425 | kunmap(dir_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 426 | put_page(dir_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 427 | } |
| 428 | out_old: |
| 429 | kunmap(old_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 430 | put_page(old_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 431 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 432 | nilfs_transaction_abort(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 433 | return err; |
| 434 | } |
| 435 | |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 436 | /* |
| 437 | * Export operations |
| 438 | */ |
| 439 | static struct dentry *nilfs_get_parent(struct dentry *child) |
| 440 | { |
| 441 | unsigned long ino; |
| 442 | struct inode *inode; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 443 | struct nilfs_root *root; |
| 444 | |
Al Viro | 80e5d1ff5 | 2021-04-15 19:46:50 -0400 | [diff] [blame] | 445 | ino = nilfs_inode_by_name(d_inode(child), &dotdot_name); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 446 | if (!ino) |
| 447 | return ERR_PTR(-ENOENT); |
| 448 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 449 | root = NILFS_I(d_inode(child))->i_root; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 450 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 451 | inode = nilfs_iget(child->d_sb, root, ino); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 452 | if (IS_ERR(inode)) |
| 453 | return ERR_CAST(inode); |
| 454 | |
| 455 | return d_obtain_alias(inode); |
| 456 | } |
| 457 | |
| 458 | static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno, |
| 459 | u64 ino, u32 gen) |
| 460 | { |
| 461 | struct nilfs_root *root; |
| 462 | struct inode *inode; |
| 463 | |
| 464 | if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO) |
| 465 | return ERR_PTR(-ESTALE); |
| 466 | |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 467 | root = nilfs_lookup_root(sb->s_fs_info, cno); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 468 | if (!root) |
| 469 | return ERR_PTR(-ESTALE); |
| 470 | |
| 471 | inode = nilfs_iget(sb, root, ino); |
| 472 | nilfs_put_root(root); |
| 473 | |
| 474 | if (IS_ERR(inode)) |
| 475 | return ERR_CAST(inode); |
| 476 | if (gen && inode->i_generation != gen) { |
| 477 | iput(inode); |
| 478 | return ERR_PTR(-ESTALE); |
| 479 | } |
| 480 | return d_obtain_alias(inode); |
| 481 | } |
| 482 | |
| 483 | static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh, |
| 484 | int fh_len, int fh_type) |
| 485 | { |
| 486 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 487 | |
NeilBrown | f73c2f1 | 2015-06-25 15:03:45 -0700 | [diff] [blame] | 488 | if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE || |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 489 | (fh_type != FILEID_NILFS_WITH_PARENT && |
| 490 | fh_type != FILEID_NILFS_WITHOUT_PARENT)) |
| 491 | return NULL; |
| 492 | |
| 493 | return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen); |
| 494 | } |
| 495 | |
| 496 | static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh, |
| 497 | int fh_len, int fh_type) |
| 498 | { |
| 499 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 500 | |
NeilBrown | f73c2f1 | 2015-06-25 15:03:45 -0700 | [diff] [blame] | 501 | if (fh_len < NILFS_FID_SIZE_CONNECTABLE || |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 502 | fh_type != FILEID_NILFS_WITH_PARENT) |
| 503 | return NULL; |
| 504 | |
| 505 | return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen); |
| 506 | } |
| 507 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 508 | static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp, |
| 509 | struct inode *parent) |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 510 | { |
| 511 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 512 | struct nilfs_root *root = NILFS_I(inode)->i_root; |
| 513 | int type; |
| 514 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 515 | if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) { |
| 516 | *lenp = NILFS_FID_SIZE_CONNECTABLE; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 517 | return FILEID_INVALID; |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 518 | } |
| 519 | if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) { |
| 520 | *lenp = NILFS_FID_SIZE_NON_CONNECTABLE; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 521 | return FILEID_INVALID; |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 522 | } |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 523 | |
| 524 | fid->cno = root->cno; |
| 525 | fid->ino = inode->i_ino; |
| 526 | fid->gen = inode->i_generation; |
| 527 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 528 | if (parent) { |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 529 | fid->parent_ino = parent->i_ino; |
| 530 | fid->parent_gen = parent->i_generation; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 531 | type = FILEID_NILFS_WITH_PARENT; |
| 532 | *lenp = NILFS_FID_SIZE_CONNECTABLE; |
| 533 | } else { |
| 534 | type = FILEID_NILFS_WITHOUT_PARENT; |
| 535 | *lenp = NILFS_FID_SIZE_NON_CONNECTABLE; |
| 536 | } |
| 537 | |
| 538 | return type; |
| 539 | } |
| 540 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 541 | const struct inode_operations nilfs_dir_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 542 | .create = nilfs_create, |
| 543 | .lookup = nilfs_lookup, |
| 544 | .link = nilfs_link, |
| 545 | .unlink = nilfs_unlink, |
| 546 | .symlink = nilfs_symlink, |
| 547 | .mkdir = nilfs_mkdir, |
| 548 | .rmdir = nilfs_rmdir, |
| 549 | .mknod = nilfs_mknod, |
| 550 | .rename = nilfs_rename, |
| 551 | .setattr = nilfs_setattr, |
| 552 | .permission = nilfs_permission, |
Ryusuke Konishi | 622daaff | 2010-12-26 16:38:43 +0900 | [diff] [blame] | 553 | .fiemap = nilfs_fiemap, |
Miklos Szeredi | 7c7c436 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 554 | .fileattr_get = nilfs_fileattr_get, |
| 555 | .fileattr_set = nilfs_fileattr_set, |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 556 | }; |
| 557 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 558 | const struct inode_operations nilfs_special_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 559 | .setattr = nilfs_setattr, |
| 560 | .permission = nilfs_permission, |
| 561 | }; |
| 562 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 563 | const struct inode_operations nilfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 564 | .get_link = page_get_link, |
Ryusuke Konishi | dc3d3b8 | 2010-08-15 23:33:57 +0900 | [diff] [blame] | 565 | .permission = nilfs_permission, |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 566 | }; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 567 | |
| 568 | const struct export_operations nilfs_export_ops = { |
| 569 | .encode_fh = nilfs_encode_fh, |
| 570 | .fh_to_dentry = nilfs_fh_to_dentry, |
| 571 | .fh_to_parent = nilfs_fh_to_parent, |
| 572 | .get_parent = nilfs_get_parent, |
| 573 | }; |