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