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" |
| 43 | |
| 44 | |
| 45 | static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode) |
| 46 | { |
| 47 | int err = nilfs_add_link(dentry, inode); |
| 48 | if (!err) { |
| 49 | d_instantiate(dentry, inode); |
| 50 | return 0; |
| 51 | } |
| 52 | inode_dec_link_count(inode); |
| 53 | iput(inode); |
| 54 | return err; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Methods themselves. |
| 59 | */ |
| 60 | |
| 61 | static struct dentry * |
| 62 | nilfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 63 | { |
| 64 | struct inode *inode; |
| 65 | ino_t ino; |
| 66 | |
| 67 | if (dentry->d_name.len > NILFS_NAME_LEN) |
| 68 | return ERR_PTR(-ENAMETOOLONG); |
| 69 | |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 70 | ino = nilfs_inode_by_name(dir, &dentry->d_name); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 71 | inode = NULL; |
| 72 | if (ino) { |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame^] | 73 | inode = nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 74 | if (IS_ERR(inode)) |
| 75 | return ERR_CAST(inode); |
| 76 | } |
| 77 | return d_splice_alias(inode, dentry); |
| 78 | } |
| 79 | |
| 80 | struct dentry *nilfs_get_parent(struct dentry *child) |
| 81 | { |
| 82 | unsigned long ino; |
| 83 | struct inode *inode; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 84 | struct qstr dotdot = {.name = "..", .len = 2}; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 85 | |
| 86 | ino = nilfs_inode_by_name(child->d_inode, &dotdot); |
| 87 | if (!ino) |
| 88 | return ERR_PTR(-ENOENT); |
| 89 | |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame^] | 90 | inode = nilfs_iget(child->d_inode->i_sb, |
| 91 | NILFS_I(child->d_inode)->i_root, ino); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 92 | if (IS_ERR(inode)) |
| 93 | return ERR_CAST(inode); |
| 94 | return d_obtain_alias(inode); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * By the time this is called, we already have created |
| 99 | * the directory cache entry for the new file, but it |
| 100 | * is so far negative - it has no inode. |
| 101 | * |
| 102 | * If the create succeeds, we fill in the inode information |
| 103 | * with d_instantiate(). |
| 104 | */ |
| 105 | static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 106 | struct nameidata *nd) |
| 107 | { |
| 108 | struct inode *inode; |
| 109 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 110 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 111 | |
| 112 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 113 | if (err) |
| 114 | return err; |
| 115 | inode = nilfs_new_inode(dir, mode); |
| 116 | err = PTR_ERR(inode); |
| 117 | if (!IS_ERR(inode)) { |
| 118 | inode->i_op = &nilfs_file_inode_operations; |
| 119 | inode->i_fop = &nilfs_file_operations; |
| 120 | inode->i_mapping->a_ops = &nilfs_aops; |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 121 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 122 | err = nilfs_add_nondir(dentry, inode); |
| 123 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 124 | if (!err) |
| 125 | err = nilfs_transaction_commit(dir->i_sb); |
| 126 | else |
| 127 | nilfs_transaction_abort(dir->i_sb); |
| 128 | |
| 129 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int |
| 133 | nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) |
| 134 | { |
| 135 | struct inode *inode; |
| 136 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 137 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 138 | |
| 139 | if (!new_valid_dev(rdev)) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 143 | if (err) |
| 144 | return err; |
| 145 | inode = nilfs_new_inode(dir, mode); |
| 146 | err = PTR_ERR(inode); |
| 147 | if (!IS_ERR(inode)) { |
| 148 | init_special_inode(inode, inode->i_mode, rdev); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 149 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 150 | err = nilfs_add_nondir(dentry, inode); |
| 151 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 152 | if (!err) |
| 153 | err = nilfs_transaction_commit(dir->i_sb); |
| 154 | else |
| 155 | nilfs_transaction_abort(dir->i_sb); |
| 156 | |
| 157 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int nilfs_symlink(struct inode *dir, struct dentry *dentry, |
| 161 | const char *symname) |
| 162 | { |
| 163 | struct nilfs_transaction_info ti; |
| 164 | struct super_block *sb = dir->i_sb; |
| 165 | unsigned l = strlen(symname)+1; |
| 166 | struct inode *inode; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 167 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 168 | |
| 169 | if (l > sb->s_blocksize) |
| 170 | return -ENAMETOOLONG; |
| 171 | |
| 172 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 173 | if (err) |
| 174 | return err; |
| 175 | |
| 176 | inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 177 | err = PTR_ERR(inode); |
| 178 | if (IS_ERR(inode)) |
| 179 | goto out; |
| 180 | |
| 181 | /* slow symlink */ |
| 182 | inode->i_op = &nilfs_symlink_inode_operations; |
| 183 | inode->i_mapping->a_ops = &nilfs_aops; |
| 184 | err = page_symlink(inode, symname, l); |
| 185 | if (err) |
| 186 | goto out_fail; |
| 187 | |
| 188 | /* mark_inode_dirty(inode); */ |
Jiro SEKIBA | 9ca941d | 2009-11-27 19:41:06 +0900 | [diff] [blame] | 189 | /* page_symlink() do this */ |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 190 | |
| 191 | err = nilfs_add_nondir(dentry, inode); |
| 192 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 193 | if (!err) |
| 194 | err = nilfs_transaction_commit(dir->i_sb); |
| 195 | else |
| 196 | nilfs_transaction_abort(dir->i_sb); |
| 197 | |
| 198 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 199 | |
| 200 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 201 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 202 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 203 | iput(inode); |
| 204 | goto out; |
| 205 | } |
| 206 | |
| 207 | static int nilfs_link(struct dentry *old_dentry, struct inode *dir, |
| 208 | struct dentry *dentry) |
| 209 | { |
| 210 | struct inode *inode = old_dentry->d_inode; |
| 211 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 212 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 213 | |
| 214 | if (inode->i_nlink >= NILFS_LINK_MAX) |
| 215 | return -EMLINK; |
| 216 | |
| 217 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 218 | if (err) |
| 219 | return err; |
| 220 | |
| 221 | inode->i_ctime = CURRENT_TIME; |
| 222 | inode_inc_link_count(inode); |
| 223 | atomic_inc(&inode->i_count); |
| 224 | |
| 225 | err = nilfs_add_nondir(dentry, inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 226 | if (!err) |
| 227 | err = nilfs_transaction_commit(dir->i_sb); |
| 228 | else |
| 229 | nilfs_transaction_abort(dir->i_sb); |
| 230 | |
| 231 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 235 | { |
| 236 | struct inode *inode; |
| 237 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 238 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 239 | |
| 240 | if (dir->i_nlink >= NILFS_LINK_MAX) |
| 241 | return -EMLINK; |
| 242 | |
| 243 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 244 | if (err) |
| 245 | return err; |
| 246 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 247 | inc_nlink(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 248 | |
| 249 | inode = nilfs_new_inode(dir, S_IFDIR | mode); |
| 250 | err = PTR_ERR(inode); |
| 251 | if (IS_ERR(inode)) |
| 252 | goto out_dir; |
| 253 | |
| 254 | inode->i_op = &nilfs_dir_inode_operations; |
| 255 | inode->i_fop = &nilfs_dir_operations; |
| 256 | inode->i_mapping->a_ops = &nilfs_aops; |
| 257 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 258 | inc_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 259 | |
| 260 | err = nilfs_make_empty(inode, dir); |
| 261 | if (err) |
| 262 | goto out_fail; |
| 263 | |
| 264 | err = nilfs_add_link(dentry, inode); |
| 265 | if (err) |
| 266 | goto out_fail; |
| 267 | |
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 | d_instantiate(dentry, inode); |
| 270 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 271 | if (!err) |
| 272 | err = nilfs_transaction_commit(dir->i_sb); |
| 273 | else |
| 274 | nilfs_transaction_abort(dir->i_sb); |
| 275 | |
| 276 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 277 | |
| 278 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 279 | drop_nlink(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 280 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 281 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 282 | iput(inode); |
| 283 | out_dir: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 284 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 285 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 286 | goto out; |
| 287 | } |
| 288 | |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 289 | static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 290 | { |
| 291 | struct inode *inode; |
| 292 | struct nilfs_dir_entry *de; |
| 293 | struct page *page; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 294 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 295 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 296 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 297 | de = nilfs_find_entry(dir, &dentry->d_name, &page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 298 | if (!de) |
| 299 | goto out; |
| 300 | |
| 301 | inode = dentry->d_inode; |
| 302 | err = -EIO; |
| 303 | if (le64_to_cpu(de->inode) != inode->i_ino) |
| 304 | goto out; |
| 305 | |
| 306 | if (!inode->i_nlink) { |
| 307 | nilfs_warning(inode->i_sb, __func__, |
| 308 | "deleting nonexistent file (%lu), %d\n", |
| 309 | inode->i_ino, inode->i_nlink); |
| 310 | inode->i_nlink = 1; |
| 311 | } |
| 312 | err = nilfs_delete_entry(de, page); |
| 313 | if (err) |
| 314 | goto out; |
| 315 | |
| 316 | inode->i_ctime = dir->i_ctime; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 317 | drop_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 318 | err = 0; |
| 319 | out: |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 320 | return err; |
| 321 | } |
| 322 | |
| 323 | static int nilfs_unlink(struct inode *dir, struct dentry *dentry) |
| 324 | { |
| 325 | struct nilfs_transaction_info ti; |
| 326 | int err; |
| 327 | |
| 328 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 329 | if (err) |
| 330 | return err; |
| 331 | |
| 332 | err = nilfs_do_unlink(dir, dentry); |
| 333 | |
| 334 | if (!err) { |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 335 | nilfs_mark_inode_dirty(dir); |
| 336 | nilfs_mark_inode_dirty(dentry->d_inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 337 | err = nilfs_transaction_commit(dir->i_sb); |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 338 | } else |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 339 | nilfs_transaction_abort(dir->i_sb); |
| 340 | |
| 341 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static int nilfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 345 | { |
| 346 | struct inode *inode = dentry->d_inode; |
| 347 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 348 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 349 | |
| 350 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 351 | if (err) |
| 352 | return err; |
| 353 | |
| 354 | err = -ENOTEMPTY; |
| 355 | if (nilfs_empty_dir(inode)) { |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 356 | err = nilfs_do_unlink(dir, dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 357 | if (!err) { |
| 358 | inode->i_size = 0; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 359 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 360 | nilfs_mark_inode_dirty(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 361 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 362 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 365 | if (!err) |
| 366 | err = nilfs_transaction_commit(dir->i_sb); |
| 367 | else |
| 368 | nilfs_transaction_abort(dir->i_sb); |
| 369 | |
| 370 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 374 | struct inode *new_dir, struct dentry *new_dentry) |
| 375 | { |
| 376 | struct inode *old_inode = old_dentry->d_inode; |
| 377 | struct inode *new_inode = new_dentry->d_inode; |
| 378 | struct page *dir_page = NULL; |
| 379 | struct nilfs_dir_entry *dir_de = NULL; |
| 380 | struct page *old_page; |
| 381 | struct nilfs_dir_entry *old_de; |
| 382 | struct nilfs_transaction_info ti; |
| 383 | int err; |
| 384 | |
| 385 | err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1); |
| 386 | if (unlikely(err)) |
| 387 | return err; |
| 388 | |
| 389 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 390 | 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] | 391 | if (!old_de) |
| 392 | goto out; |
| 393 | |
| 394 | if (S_ISDIR(old_inode->i_mode)) { |
| 395 | err = -EIO; |
| 396 | dir_de = nilfs_dotdot(old_inode, &dir_page); |
| 397 | if (!dir_de) |
| 398 | goto out_old; |
| 399 | } |
| 400 | |
| 401 | if (new_inode) { |
| 402 | struct page *new_page; |
| 403 | struct nilfs_dir_entry *new_de; |
| 404 | |
| 405 | err = -ENOTEMPTY; |
| 406 | if (dir_de && !nilfs_empty_dir(new_inode)) |
| 407 | goto out_dir; |
| 408 | |
| 409 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 410 | 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] | 411 | if (!new_de) |
| 412 | goto out_dir; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 413 | inc_nlink(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 414 | nilfs_set_link(new_dir, new_de, new_page, old_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 415 | nilfs_mark_inode_dirty(new_dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 416 | new_inode->i_ctime = CURRENT_TIME; |
| 417 | if (dir_de) |
| 418 | drop_nlink(new_inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 419 | drop_nlink(new_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 420 | nilfs_mark_inode_dirty(new_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 421 | } else { |
| 422 | if (dir_de) { |
| 423 | err = -EMLINK; |
| 424 | if (new_dir->i_nlink >= NILFS_LINK_MAX) |
| 425 | goto out_dir; |
| 426 | } |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 427 | inc_nlink(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 428 | err = nilfs_add_link(new_dentry, old_inode); |
| 429 | if (err) { |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 430 | drop_nlink(old_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 431 | nilfs_mark_inode_dirty(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 432 | goto out_dir; |
| 433 | } |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 434 | if (dir_de) { |
| 435 | inc_nlink(new_dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 436 | nilfs_mark_inode_dirty(new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 437 | } |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Like most other Unix systems, set the ctime for inodes on a |
| 442 | * rename. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 443 | */ |
| 444 | old_inode->i_ctime = CURRENT_TIME; |
| 445 | |
| 446 | nilfs_delete_entry(old_de, old_page); |
Jiro SEKIBA | 43f8bc2 | 2009-11-27 19:41:07 +0900 | [diff] [blame] | 447 | drop_nlink(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 448 | |
| 449 | if (dir_de) { |
| 450 | nilfs_set_link(old_inode, dir_de, dir_page, new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 451 | drop_nlink(old_dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 452 | } |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 453 | nilfs_mark_inode_dirty(old_dir); |
| 454 | nilfs_mark_inode_dirty(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 455 | |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 456 | err = nilfs_transaction_commit(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 457 | return err; |
| 458 | |
| 459 | out_dir: |
| 460 | if (dir_de) { |
| 461 | kunmap(dir_page); |
| 462 | page_cache_release(dir_page); |
| 463 | } |
| 464 | out_old: |
| 465 | kunmap(old_page); |
| 466 | page_cache_release(old_page); |
| 467 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 468 | nilfs_transaction_abort(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 469 | return err; |
| 470 | } |
| 471 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 472 | const struct inode_operations nilfs_dir_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 473 | .create = nilfs_create, |
| 474 | .lookup = nilfs_lookup, |
| 475 | .link = nilfs_link, |
| 476 | .unlink = nilfs_unlink, |
| 477 | .symlink = nilfs_symlink, |
| 478 | .mkdir = nilfs_mkdir, |
| 479 | .rmdir = nilfs_rmdir, |
| 480 | .mknod = nilfs_mknod, |
| 481 | .rename = nilfs_rename, |
| 482 | .setattr = nilfs_setattr, |
| 483 | .permission = nilfs_permission, |
| 484 | }; |
| 485 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 486 | const struct inode_operations nilfs_special_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 487 | .setattr = nilfs_setattr, |
| 488 | .permission = nilfs_permission, |
| 489 | }; |
| 490 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 491 | const struct inode_operations nilfs_symlink_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 492 | .readlink = generic_readlink, |
| 493 | .follow_link = page_follow_link_light, |
| 494 | .put_link = page_put_link, |
| 495 | }; |