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