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); |
| 54 | return 0; |
| 55 | } |
| 56 | inode_dec_link_count(inode); |
| 57 | iput(inode); |
| 58 | return err; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Methods themselves. |
| 63 | */ |
| 64 | |
| 65 | static struct dentry * |
| 66 | nilfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 67 | { |
| 68 | struct inode *inode; |
| 69 | ino_t ino; |
| 70 | |
| 71 | if (dentry->d_name.len > NILFS_NAME_LEN) |
| 72 | return ERR_PTR(-ENAMETOOLONG); |
| 73 | |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 74 | ino = nilfs_inode_by_name(dir, &dentry->d_name); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 75 | inode = NULL; |
| 76 | if (ino) { |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 77 | inode = nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 78 | if (IS_ERR(inode)) |
| 79 | return ERR_CAST(inode); |
| 80 | } |
| 81 | return d_splice_alias(inode, dentry); |
| 82 | } |
| 83 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 84 | /* |
| 85 | * By the time this is called, we already have created |
| 86 | * the directory cache entry for the new file, but it |
| 87 | * is so far negative - it has no inode. |
| 88 | * |
| 89 | * If the create succeeds, we fill in the inode information |
| 90 | * with d_instantiate(). |
| 91 | */ |
| 92 | static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 93 | struct nameidata *nd) |
| 94 | { |
| 95 | struct inode *inode; |
| 96 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 97 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 98 | |
| 99 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 100 | if (err) |
| 101 | return err; |
| 102 | inode = nilfs_new_inode(dir, mode); |
| 103 | err = PTR_ERR(inode); |
| 104 | if (!IS_ERR(inode)) { |
| 105 | inode->i_op = &nilfs_file_inode_operations; |
| 106 | inode->i_fop = &nilfs_file_operations; |
| 107 | inode->i_mapping->a_ops = &nilfs_aops; |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 108 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 109 | err = nilfs_add_nondir(dentry, inode); |
| 110 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 111 | if (!err) |
| 112 | err = nilfs_transaction_commit(dir->i_sb); |
| 113 | else |
| 114 | nilfs_transaction_abort(dir->i_sb); |
| 115 | |
| 116 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int |
| 120 | nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) |
| 121 | { |
| 122 | struct inode *inode; |
| 123 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 124 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 125 | |
| 126 | if (!new_valid_dev(rdev)) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 130 | if (err) |
| 131 | return err; |
| 132 | inode = nilfs_new_inode(dir, mode); |
| 133 | err = PTR_ERR(inode); |
| 134 | if (!IS_ERR(inode)) { |
| 135 | init_special_inode(inode, inode->i_mode, rdev); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 136 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 137 | err = nilfs_add_nondir(dentry, inode); |
| 138 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 139 | if (!err) |
| 140 | err = nilfs_transaction_commit(dir->i_sb); |
| 141 | else |
| 142 | nilfs_transaction_abort(dir->i_sb); |
| 143 | |
| 144 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int nilfs_symlink(struct inode *dir, struct dentry *dentry, |
| 148 | const char *symname) |
| 149 | { |
| 150 | struct nilfs_transaction_info ti; |
| 151 | struct super_block *sb = dir->i_sb; |
| 152 | unsigned l = strlen(symname)+1; |
| 153 | struct inode *inode; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 154 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 155 | |
| 156 | if (l > sb->s_blocksize) |
| 157 | return -ENAMETOOLONG; |
| 158 | |
| 159 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 160 | if (err) |
| 161 | return err; |
| 162 | |
| 163 | inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 164 | err = PTR_ERR(inode); |
| 165 | if (IS_ERR(inode)) |
| 166 | goto out; |
| 167 | |
| 168 | /* slow symlink */ |
| 169 | inode->i_op = &nilfs_symlink_inode_operations; |
| 170 | inode->i_mapping->a_ops = &nilfs_aops; |
| 171 | err = page_symlink(inode, symname, l); |
| 172 | if (err) |
| 173 | goto out_fail; |
| 174 | |
| 175 | /* mark_inode_dirty(inode); */ |
Jiro SEKIBA | 9ca941d | 2009-11-27 19:41:06 +0900 | [diff] [blame] | 176 | /* page_symlink() do this */ |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 177 | |
| 178 | err = nilfs_add_nondir(dentry, inode); |
| 179 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 180 | if (!err) |
| 181 | err = nilfs_transaction_commit(dir->i_sb); |
| 182 | else |
| 183 | nilfs_transaction_abort(dir->i_sb); |
| 184 | |
| 185 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 186 | |
| 187 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 188 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 189 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 190 | iput(inode); |
| 191 | goto out; |
| 192 | } |
| 193 | |
| 194 | static int nilfs_link(struct dentry *old_dentry, struct inode *dir, |
| 195 | struct dentry *dentry) |
| 196 | { |
| 197 | struct inode *inode = old_dentry->d_inode; |
| 198 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 199 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 200 | |
| 201 | if (inode->i_nlink >= NILFS_LINK_MAX) |
| 202 | return -EMLINK; |
| 203 | |
| 204 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 205 | if (err) |
| 206 | return err; |
| 207 | |
| 208 | inode->i_ctime = CURRENT_TIME; |
| 209 | inode_inc_link_count(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 210 | ihold(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 211 | |
| 212 | err = nilfs_add_nondir(dentry, inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 213 | if (!err) |
| 214 | err = nilfs_transaction_commit(dir->i_sb); |
| 215 | else |
| 216 | nilfs_transaction_abort(dir->i_sb); |
| 217 | |
| 218 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 222 | { |
| 223 | struct inode *inode; |
| 224 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 225 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 226 | |
| 227 | if (dir->i_nlink >= NILFS_LINK_MAX) |
| 228 | return -EMLINK; |
| 229 | |
| 230 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 231 | if (err) |
| 232 | return err; |
| 233 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 234 | inc_nlink(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 235 | |
| 236 | inode = nilfs_new_inode(dir, S_IFDIR | mode); |
| 237 | err = PTR_ERR(inode); |
| 238 | if (IS_ERR(inode)) |
| 239 | goto out_dir; |
| 240 | |
| 241 | inode->i_op = &nilfs_dir_inode_operations; |
| 242 | inode->i_fop = &nilfs_dir_operations; |
| 243 | inode->i_mapping->a_ops = &nilfs_aops; |
| 244 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 245 | inc_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 246 | |
| 247 | err = nilfs_make_empty(inode, dir); |
| 248 | if (err) |
| 249 | goto out_fail; |
| 250 | |
| 251 | err = nilfs_add_link(dentry, inode); |
| 252 | if (err) |
| 253 | goto out_fail; |
| 254 | |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 255 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 256 | d_instantiate(dentry, inode); |
| 257 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 258 | if (!err) |
| 259 | err = nilfs_transaction_commit(dir->i_sb); |
| 260 | else |
| 261 | nilfs_transaction_abort(dir->i_sb); |
| 262 | |
| 263 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 264 | |
| 265 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 266 | drop_nlink(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 267 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 268 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 269 | iput(inode); |
| 270 | out_dir: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 271 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 272 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 273 | goto out; |
| 274 | } |
| 275 | |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 276 | static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 277 | { |
| 278 | struct inode *inode; |
| 279 | struct nilfs_dir_entry *de; |
| 280 | struct page *page; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 281 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 282 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 283 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 284 | de = nilfs_find_entry(dir, &dentry->d_name, &page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 285 | if (!de) |
| 286 | goto out; |
| 287 | |
| 288 | inode = dentry->d_inode; |
| 289 | err = -EIO; |
| 290 | if (le64_to_cpu(de->inode) != inode->i_ino) |
| 291 | goto out; |
| 292 | |
| 293 | if (!inode->i_nlink) { |
| 294 | nilfs_warning(inode->i_sb, __func__, |
| 295 | "deleting nonexistent file (%lu), %d\n", |
| 296 | inode->i_ino, inode->i_nlink); |
| 297 | inode->i_nlink = 1; |
| 298 | } |
| 299 | err = nilfs_delete_entry(de, page); |
| 300 | if (err) |
| 301 | goto out; |
| 302 | |
| 303 | inode->i_ctime = dir->i_ctime; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 304 | drop_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 305 | err = 0; |
| 306 | out: |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 307 | return err; |
| 308 | } |
| 309 | |
| 310 | static int nilfs_unlink(struct inode *dir, struct dentry *dentry) |
| 311 | { |
| 312 | struct nilfs_transaction_info ti; |
| 313 | int err; |
| 314 | |
| 315 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 316 | if (err) |
| 317 | return err; |
| 318 | |
| 319 | err = nilfs_do_unlink(dir, dentry); |
| 320 | |
| 321 | if (!err) { |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 322 | nilfs_mark_inode_dirty(dir); |
| 323 | nilfs_mark_inode_dirty(dentry->d_inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 324 | err = nilfs_transaction_commit(dir->i_sb); |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 325 | } else |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 326 | nilfs_transaction_abort(dir->i_sb); |
| 327 | |
| 328 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int nilfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 332 | { |
| 333 | struct inode *inode = dentry->d_inode; |
| 334 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 335 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 336 | |
| 337 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 338 | if (err) |
| 339 | return err; |
| 340 | |
| 341 | err = -ENOTEMPTY; |
| 342 | if (nilfs_empty_dir(inode)) { |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 343 | err = nilfs_do_unlink(dir, dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 344 | if (!err) { |
| 345 | inode->i_size = 0; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 346 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 347 | nilfs_mark_inode_dirty(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 348 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 349 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 352 | if (!err) |
| 353 | err = nilfs_transaction_commit(dir->i_sb); |
| 354 | else |
| 355 | nilfs_transaction_abort(dir->i_sb); |
| 356 | |
| 357 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 361 | struct inode *new_dir, struct dentry *new_dentry) |
| 362 | { |
| 363 | struct inode *old_inode = old_dentry->d_inode; |
| 364 | struct inode *new_inode = new_dentry->d_inode; |
| 365 | struct page *dir_page = NULL; |
| 366 | struct nilfs_dir_entry *dir_de = NULL; |
| 367 | struct page *old_page; |
| 368 | struct nilfs_dir_entry *old_de; |
| 369 | struct nilfs_transaction_info ti; |
| 370 | int err; |
| 371 | |
| 372 | err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1); |
| 373 | if (unlikely(err)) |
| 374 | return err; |
| 375 | |
| 376 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 377 | 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] | 378 | if (!old_de) |
| 379 | goto out; |
| 380 | |
| 381 | if (S_ISDIR(old_inode->i_mode)) { |
| 382 | err = -EIO; |
| 383 | dir_de = nilfs_dotdot(old_inode, &dir_page); |
| 384 | if (!dir_de) |
| 385 | goto out_old; |
| 386 | } |
| 387 | |
| 388 | if (new_inode) { |
| 389 | struct page *new_page; |
| 390 | struct nilfs_dir_entry *new_de; |
| 391 | |
| 392 | err = -ENOTEMPTY; |
| 393 | if (dir_de && !nilfs_empty_dir(new_inode)) |
| 394 | goto out_dir; |
| 395 | |
| 396 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 397 | 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] | 398 | if (!new_de) |
| 399 | goto out_dir; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 400 | inc_nlink(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 401 | nilfs_set_link(new_dir, new_de, new_page, old_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 402 | nilfs_mark_inode_dirty(new_dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 403 | new_inode->i_ctime = CURRENT_TIME; |
| 404 | if (dir_de) |
| 405 | drop_nlink(new_inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 406 | drop_nlink(new_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 407 | nilfs_mark_inode_dirty(new_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 408 | } else { |
| 409 | if (dir_de) { |
| 410 | err = -EMLINK; |
| 411 | if (new_dir->i_nlink >= NILFS_LINK_MAX) |
| 412 | goto out_dir; |
| 413 | } |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 414 | inc_nlink(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 415 | err = nilfs_add_link(new_dentry, old_inode); |
| 416 | if (err) { |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 417 | drop_nlink(old_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 418 | nilfs_mark_inode_dirty(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 419 | goto out_dir; |
| 420 | } |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 421 | if (dir_de) { |
| 422 | inc_nlink(new_dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 423 | nilfs_mark_inode_dirty(new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 424 | } |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Like most other Unix systems, set the ctime for inodes on a |
| 429 | * rename. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 430 | */ |
| 431 | old_inode->i_ctime = CURRENT_TIME; |
| 432 | |
| 433 | nilfs_delete_entry(old_de, old_page); |
Jiro SEKIBA | 43f8bc2 | 2009-11-27 19:41:07 +0900 | [diff] [blame] | 434 | drop_nlink(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 435 | |
| 436 | if (dir_de) { |
| 437 | nilfs_set_link(old_inode, dir_de, dir_page, new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 438 | drop_nlink(old_dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 439 | } |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 440 | nilfs_mark_inode_dirty(old_dir); |
| 441 | nilfs_mark_inode_dirty(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 442 | |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 443 | err = nilfs_transaction_commit(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 444 | return err; |
| 445 | |
| 446 | out_dir: |
| 447 | if (dir_de) { |
| 448 | kunmap(dir_page); |
| 449 | page_cache_release(dir_page); |
| 450 | } |
| 451 | out_old: |
| 452 | kunmap(old_page); |
| 453 | page_cache_release(old_page); |
| 454 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 455 | nilfs_transaction_abort(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 456 | return err; |
| 457 | } |
| 458 | |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 459 | /* |
| 460 | * Export operations |
| 461 | */ |
| 462 | static struct dentry *nilfs_get_parent(struct dentry *child) |
| 463 | { |
| 464 | unsigned long ino; |
| 465 | struct inode *inode; |
| 466 | struct qstr dotdot = {.name = "..", .len = 2}; |
| 467 | struct nilfs_root *root; |
| 468 | |
| 469 | ino = nilfs_inode_by_name(child->d_inode, &dotdot); |
| 470 | if (!ino) |
| 471 | return ERR_PTR(-ENOENT); |
| 472 | |
| 473 | root = NILFS_I(child->d_inode)->i_root; |
| 474 | |
| 475 | inode = nilfs_iget(child->d_inode->i_sb, root, ino); |
| 476 | if (IS_ERR(inode)) |
| 477 | return ERR_CAST(inode); |
| 478 | |
| 479 | return d_obtain_alias(inode); |
| 480 | } |
| 481 | |
| 482 | static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno, |
| 483 | u64 ino, u32 gen) |
| 484 | { |
| 485 | struct nilfs_root *root; |
| 486 | struct inode *inode; |
| 487 | |
| 488 | if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO) |
| 489 | return ERR_PTR(-ESTALE); |
| 490 | |
| 491 | root = nilfs_lookup_root(NILFS_SB(sb)->s_nilfs, cno); |
| 492 | if (!root) |
| 493 | return ERR_PTR(-ESTALE); |
| 494 | |
| 495 | inode = nilfs_iget(sb, root, ino); |
| 496 | nilfs_put_root(root); |
| 497 | |
| 498 | if (IS_ERR(inode)) |
| 499 | return ERR_CAST(inode); |
| 500 | if (gen && inode->i_generation != gen) { |
| 501 | iput(inode); |
| 502 | return ERR_PTR(-ESTALE); |
| 503 | } |
| 504 | return d_obtain_alias(inode); |
| 505 | } |
| 506 | |
| 507 | static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh, |
| 508 | int fh_len, int fh_type) |
| 509 | { |
| 510 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 511 | |
| 512 | if ((fh_len != NILFS_FID_SIZE_NON_CONNECTABLE && |
| 513 | fh_len != NILFS_FID_SIZE_CONNECTABLE) || |
| 514 | (fh_type != FILEID_NILFS_WITH_PARENT && |
| 515 | fh_type != FILEID_NILFS_WITHOUT_PARENT)) |
| 516 | return NULL; |
| 517 | |
| 518 | return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen); |
| 519 | } |
| 520 | |
| 521 | static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh, |
| 522 | int fh_len, int fh_type) |
| 523 | { |
| 524 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 525 | |
| 526 | if (fh_len != NILFS_FID_SIZE_CONNECTABLE || |
| 527 | fh_type != FILEID_NILFS_WITH_PARENT) |
| 528 | return NULL; |
| 529 | |
| 530 | return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen); |
| 531 | } |
| 532 | |
| 533 | static int nilfs_encode_fh(struct dentry *dentry, __u32 *fh, int *lenp, |
| 534 | int connectable) |
| 535 | { |
| 536 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 537 | struct inode *inode = dentry->d_inode; |
| 538 | struct nilfs_root *root = NILFS_I(inode)->i_root; |
| 539 | int type; |
| 540 | |
| 541 | if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE || |
| 542 | (connectable && *lenp < NILFS_FID_SIZE_CONNECTABLE)) |
| 543 | return 255; |
| 544 | |
| 545 | fid->cno = root->cno; |
| 546 | fid->ino = inode->i_ino; |
| 547 | fid->gen = inode->i_generation; |
| 548 | |
| 549 | if (connectable && !S_ISDIR(inode->i_mode)) { |
| 550 | struct inode *parent; |
| 551 | |
| 552 | spin_lock(&dentry->d_lock); |
| 553 | parent = dentry->d_parent->d_inode; |
| 554 | fid->parent_ino = parent->i_ino; |
| 555 | fid->parent_gen = parent->i_generation; |
| 556 | spin_unlock(&dentry->d_lock); |
| 557 | |
| 558 | type = FILEID_NILFS_WITH_PARENT; |
| 559 | *lenp = NILFS_FID_SIZE_CONNECTABLE; |
| 560 | } else { |
| 561 | type = FILEID_NILFS_WITHOUT_PARENT; |
| 562 | *lenp = NILFS_FID_SIZE_NON_CONNECTABLE; |
| 563 | } |
| 564 | |
| 565 | return type; |
| 566 | } |
| 567 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 568 | const struct inode_operations nilfs_dir_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 569 | .create = nilfs_create, |
| 570 | .lookup = nilfs_lookup, |
| 571 | .link = nilfs_link, |
| 572 | .unlink = nilfs_unlink, |
| 573 | .symlink = nilfs_symlink, |
| 574 | .mkdir = nilfs_mkdir, |
| 575 | .rmdir = nilfs_rmdir, |
| 576 | .mknod = nilfs_mknod, |
| 577 | .rename = nilfs_rename, |
| 578 | .setattr = nilfs_setattr, |
| 579 | .permission = nilfs_permission, |
Ryusuke Konishi | 622daaff | 2010-12-26 16:38:43 +0900 | [diff] [blame] | 580 | .fiemap = nilfs_fiemap, |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 583 | const struct inode_operations nilfs_special_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 584 | .setattr = nilfs_setattr, |
| 585 | .permission = nilfs_permission, |
| 586 | }; |
| 587 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 588 | const struct inode_operations nilfs_symlink_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 589 | .readlink = generic_readlink, |
| 590 | .follow_link = page_follow_link_light, |
| 591 | .put_link = page_put_link, |
Ryusuke Konishi | dc3d3b8 | 2010-08-15 23:33:57 +0900 | [diff] [blame] | 592 | .permission = nilfs_permission, |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 593 | }; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 594 | |
| 595 | const struct export_operations nilfs_export_ops = { |
| 596 | .encode_fh = nilfs_encode_fh, |
| 597 | .fh_to_dentry = nilfs_fh_to_dentry, |
| 598 | .fh_to_parent = nilfs_fh_to_parent, |
| 599 | .get_parent = nilfs_get_parent, |
| 600 | }; |