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