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