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