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