1 /* $NetBSD: nilfs_vfsops.c,v 1.8 2011/11/14 18:35:13 hannken Exp $ */ 2 3 /* 4 * Copyright (c) 2008, 2009 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __KERNEL_RCSID(0, "$NetBSD: nilfs_vfsops.c,v 1.8 2011/11/14 18:35:13 hannken Exp $"); 32 #endif /* not lint */ 33 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_compat_netbsd.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/sysctl.h> 42 #include <sys/namei.h> 43 #include <sys/proc.h> 44 #include <sys/kernel.h> 45 #include <sys/vnode.h> 46 #include <miscfs/genfs/genfs.h> 47 #include <miscfs/specfs/specdev.h> 48 #include <sys/mount.h> 49 #include <sys/buf.h> 50 #include <sys/file.h> 51 #include <sys/device.h> 52 #include <sys/disklabel.h> 53 #include <sys/ioctl.h> 54 #include <sys/malloc.h> 55 #include <sys/dirent.h> 56 #include <sys/stat.h> 57 #include <sys/conf.h> 58 #include <sys/kauth.h> 59 #include <sys/module.h> 60 61 #include <fs/nilfs/nilfs_mount.h> 62 #include <sys/dirhash.h> 63 64 65 #include "nilfs.h" 66 #include "nilfs_subr.h" 67 #include "nilfs_bswap.h" 68 69 MODULE(MODULE_CLASS_VFS, nilfs, NULL); 70 71 #define VTOI(vnode) ((struct nilfs_node *) vnode->v_data) 72 73 #define NILFS_SET_SYSTEMFILE(vp) { \ 74 /* XXXAD Is the vnode locked? */ \ 75 (vp)->v_vflag |= VV_SYSTEM; \ 76 vref(vp); \ 77 vput(vp); } 78 79 #define NILFS_UNSET_SYSTEMFILE(vp) { \ 80 /* XXXAD Is the vnode locked? */ \ 81 (vp)->v_vflag &= ~VV_SYSTEM; \ 82 vrele(vp); } 83 84 85 /* verbose levels of the nilfs filingsystem */ 86 int nilfs_verbose = NILFS_DEBUGGING; 87 88 /* malloc regions */ 89 MALLOC_JUSTDEFINE(M_NILFSMNT, "NILFS mount", "NILFS mount structures"); 90 MALLOC_JUSTDEFINE(M_NILFSTEMP, "NILFS temp", "NILFS scrap space"); 91 struct pool nilfs_node_pool; 92 93 /* globals */ 94 struct _nilfs_devices nilfs_devices; 95 static struct sysctllog *nilfs_sysctl_log; 96 97 /* supported functions predefined */ 98 VFS_PROTOS(nilfs); 99 100 101 /* --------------------------------------------------------------------- */ 102 103 /* predefine vnode-op list descriptor */ 104 extern const struct vnodeopv_desc nilfs_vnodeop_opv_desc; 105 106 const struct vnodeopv_desc * const nilfs_vnodeopv_descs[] = { 107 &nilfs_vnodeop_opv_desc, 108 NULL, 109 }; 110 111 112 /* vfsops descriptor linked in as anchor point for the filingsystem */ 113 struct vfsops nilfs_vfsops = { 114 MOUNT_NILFS, /* vfs_name */ 115 sizeof (struct nilfs_args), 116 nilfs_mount, 117 nilfs_start, 118 nilfs_unmount, 119 nilfs_root, 120 (void *)eopnotsupp, /* vfs_quotactl */ 121 nilfs_statvfs, 122 nilfs_sync, 123 nilfs_vget, 124 nilfs_fhtovp, 125 nilfs_vptofh, 126 nilfs_init, 127 nilfs_reinit, 128 nilfs_done, 129 nilfs_mountroot, 130 nilfs_snapshot, 131 vfs_stdextattrctl, 132 (void *)eopnotsupp, /* vfs_suspendctl */ 133 genfs_renamelock_enter, 134 genfs_renamelock_exit, 135 (void *)eopnotsupp, /* vfs_full_fsync */ 136 nilfs_vnodeopv_descs, 137 0, /* int vfs_refcount */ 138 { NULL, NULL, }, /* LIST_ENTRY(vfsops) */ 139 }; 140 141 /* --------------------------------------------------------------------- */ 142 143 /* file system starts here */ 144 void 145 nilfs_init(void) 146 { 147 size_t size; 148 149 /* setup memory types */ 150 malloc_type_attach(M_NILFSMNT); 151 malloc_type_attach(M_NILFSTEMP); 152 153 /* init device lists */ 154 SLIST_INIT(&nilfs_devices); 155 156 /* init node pools */ 157 size = sizeof(struct nilfs_node); 158 pool_init(&nilfs_node_pool, size, 0, 0, 0, 159 "nilfs_node_pool", NULL, IPL_NONE); 160 } 161 162 163 void 164 nilfs_reinit(void) 165 { 166 /* nothing to do */ 167 } 168 169 170 void 171 nilfs_done(void) 172 { 173 /* remove pools */ 174 pool_destroy(&nilfs_node_pool); 175 176 malloc_type_detach(M_NILFSMNT); 177 malloc_type_detach(M_NILFSTEMP); 178 } 179 180 /* 181 * If running a DEBUG kernel, provide an easy way to set the debug flags when 182 * running into a problem. 183 */ 184 #define NILFS_VERBOSE_SYSCTLOPT 1 185 186 static int 187 nilfs_modcmd(modcmd_t cmd, void *arg) 188 { 189 const struct sysctlnode *node; 190 int error; 191 192 switch (cmd) { 193 case MODULE_CMD_INIT: 194 error = vfs_attach(&nilfs_vfsops); 195 if (error != 0) 196 break; 197 /* 198 * XXX the "30" below could be dynamic, thereby eliminating one 199 * more instance of the "number to vfs" mapping problem, but 200 * "30" is the order as taken from sys/mount.h 201 */ 202 sysctl_createv(&nilfs_sysctl_log, 0, NULL, NULL, 203 CTLFLAG_PERMANENT, 204 CTLTYPE_NODE, "vfs", NULL, 205 NULL, 0, NULL, 0, 206 CTL_VFS, CTL_EOL); 207 sysctl_createv(&nilfs_sysctl_log, 0, NULL, &node, 208 CTLFLAG_PERMANENT, 209 CTLTYPE_NODE, "nilfs", 210 SYSCTL_DESCR("NTT's NILFSv2"), 211 NULL, 0, NULL, 0, 212 CTL_VFS, 30, CTL_EOL); 213 #ifdef DEBUG 214 sysctl_createv(&nilfs_sysctl_log, 0, NULL, &node, 215 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 216 CTLTYPE_INT, "verbose", 217 SYSCTL_DESCR("Bitmask for filesystem debugging"), 218 NULL, 0, &nilfs_verbose, 0, 219 CTL_VFS, 30, NILFS_VERBOSE_SYSCTLOPT, CTL_EOL); 220 #endif 221 break; 222 case MODULE_CMD_FINI: 223 error = vfs_detach(&nilfs_vfsops); 224 if (error != 0) 225 break; 226 sysctl_teardown(&nilfs_sysctl_log); 227 break; 228 default: 229 error = ENOTTY; 230 break; 231 } 232 233 return (error); 234 } 235 236 /* --------------------------------------------------------------------- */ 237 238 int 239 nilfs_mountroot(void) 240 { 241 return EOPNOTSUPP; 242 } 243 244 /* --------------------------------------------------------------------- */ 245 246 /* system nodes */ 247 static int 248 nilfs_create_system_nodes(struct nilfs_device *nilfsdev) 249 { 250 int error; 251 252 error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_DAT_INO, 253 &nilfsdev->super_root.sr_dat, &nilfsdev->dat_node); 254 if (error) 255 goto errorout; 256 257 error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_CPFILE_INO, 258 &nilfsdev->super_root.sr_cpfile, &nilfsdev->cp_node); 259 if (error) 260 goto errorout; 261 262 error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_SUFILE_INO, 263 &nilfsdev->super_root.sr_sufile, &nilfsdev->su_node); 264 if (error) 265 goto errorout; 266 267 NILFS_SET_SYSTEMFILE(nilfsdev->dat_node->vnode); 268 NILFS_SET_SYSTEMFILE(nilfsdev->cp_node->vnode); 269 NILFS_SET_SYSTEMFILE(nilfsdev->su_node->vnode); 270 271 return 0; 272 errorout: 273 nilfs_dispose_node(&nilfsdev->dat_node); 274 nilfs_dispose_node(&nilfsdev->cp_node); 275 nilfs_dispose_node(&nilfsdev->su_node); 276 277 return error; 278 } 279 280 281 static void 282 nilfs_release_system_nodes(struct nilfs_device *nilfsdev) 283 { 284 if (!nilfsdev) 285 return; 286 if (nilfsdev->refcnt > 0) 287 return; 288 289 if (nilfsdev->dat_node) 290 NILFS_UNSET_SYSTEMFILE(nilfsdev->dat_node->vnode); 291 if (nilfsdev->cp_node) 292 NILFS_UNSET_SYSTEMFILE(nilfsdev->cp_node->vnode); 293 if (nilfsdev->su_node) 294 NILFS_UNSET_SYSTEMFILE(nilfsdev->su_node->vnode); 295 296 nilfs_dispose_node(&nilfsdev->dat_node); 297 nilfs_dispose_node(&nilfsdev->cp_node); 298 nilfs_dispose_node(&nilfsdev->su_node); 299 } 300 301 302 /* --------------------------------------------------------------------- */ 303 304 static int 305 nilfs_check_superblock_crc(struct nilfs_super_block *super) 306 { 307 uint32_t super_crc, comp_crc; 308 309 /* check super block magic */ 310 if (nilfs_rw16(super->s_magic) != NILFS_SUPER_MAGIC) 311 return 0; 312 313 /* preserve crc */ 314 super_crc = nilfs_rw32(super->s_sum); 315 316 /* calculate */ 317 super->s_sum = 0; 318 comp_crc = crc32_le(nilfs_rw32(super->s_crc_seed), 319 (uint8_t *) super, nilfs_rw16(super->s_bytes)); 320 321 /* restore */ 322 super->s_sum = nilfs_rw32(super_crc); 323 324 /* check CRC */ 325 return (super_crc == comp_crc); 326 } 327 328 329 330 static int 331 nilfs_read_superblock(struct nilfs_device *nilfsdev) 332 { 333 struct nilfs_super_block *super, tmp_super; 334 struct buf *bp; 335 uint64_t sb1off, sb2off; 336 uint64_t last_cno1, last_cno2; 337 uint64_t dev_blk; 338 int dev_bsize, dev_blks; 339 int sb1ok, sb2ok, swp; 340 int error; 341 342 sb1off = NILFS_SB_OFFSET_BYTES; 343 sb2off = NILFS_SB2_OFFSET_BYTES(nilfsdev->devsize); 344 345 dev_bsize = 1 << nilfsdev->devvp->v_mount->mnt_fs_bshift; 346 347 /* read our superblock regardless of backing device blocksize */ 348 dev_blk = 0; 349 dev_blks = (sb1off + dev_bsize -1)/dev_bsize; 350 error = bread(nilfsdev->devvp, dev_blk, dev_blks * dev_bsize, NOCRED, 0, &bp); 351 if (error) { 352 brelse(bp, BC_AGE); 353 return error; 354 } 355 356 /* copy read-in super block at the offset */ 357 super = &nilfsdev->super; 358 memcpy(super, (uint8_t *) bp->b_data + NILFS_SB_OFFSET_BYTES, 359 sizeof(struct nilfs_super_block)); 360 brelse(bp, BC_AGE); 361 362 /* read our 2nd superblock regardless of backing device blocksize */ 363 dev_blk = sb2off / dev_bsize; 364 dev_blks = 2; /* assumption max one dev_bsize */ 365 error = bread(nilfsdev->devvp, dev_blk, dev_blks * dev_bsize, NOCRED, 0, &bp); 366 if (error) { 367 brelse(bp, BC_AGE); 368 return error; 369 } 370 371 /* copy read-in superblock2 at the offset */ 372 super = &nilfsdev->super2; 373 memcpy(super, (uint8_t *) bp->b_data + NILFS_SB_OFFSET_BYTES, 374 sizeof(struct nilfs_super_block)); 375 brelse(bp, BC_AGE); 376 377 sb1ok = nilfs_check_superblock_crc(&nilfsdev->super); 378 sb2ok = nilfs_check_superblock_crc(&nilfsdev->super2); 379 380 last_cno1 = nilfs_rw64(nilfsdev->super.s_last_cno); 381 last_cno2 = nilfs_rw64(nilfsdev->super2.s_last_cno); 382 swp = sb2ok && (last_cno2 > last_cno1); 383 384 if (swp) { 385 printf("nilfs warning: broken superblock, using spare\n"); 386 tmp_super = nilfsdev->super2; 387 nilfsdev->super2 = nilfsdev->super; /* why preserve? */ 388 nilfsdev->super = tmp_super; 389 } 390 391 if (!sb1ok && !sb2ok) { 392 printf("nilfs: no valid superblocks found\n"); 393 return EINVAL; 394 } 395 396 return 0; 397 } 398 399 400 /* XXX NOTHING from the system nodes should need to be written here */ 401 static void 402 nilfs_unmount_base(struct nilfs_device *nilfsdev) 403 { 404 int error; 405 406 if (!nilfsdev) 407 return; 408 409 /* remove all our information */ 410 error = vinvalbuf(nilfsdev->devvp, 0, FSCRED, curlwp, 0, 0); 411 KASSERT(error == 0); 412 413 /* release the device's system nodes */ 414 nilfs_release_system_nodes(nilfsdev); 415 416 /* TODO writeout super_block? */ 417 } 418 419 420 static int 421 nilfs_mount_base(struct nilfs_device *nilfsdev, 422 struct mount *mp, struct nilfs_args *args) 423 { 424 struct lwp *l = curlwp; 425 uint64_t last_pseg, last_cno, last_seq; 426 uint32_t log_blocksize; 427 int error; 428 429 /* flush out any old buffers remaining from a previous use. */ 430 if ((error = vinvalbuf(nilfsdev->devvp, V_SAVE, l->l_cred, l, 0, 0))) 431 return error; 432 433 /* read in our superblock */ 434 error = nilfs_read_superblock(nilfsdev); 435 if (error) { 436 printf("nilfs_mount: can't read in super block : %d\n", error); 437 return error; 438 } 439 440 /* get our blocksize */ 441 log_blocksize = nilfs_rw32(nilfsdev->super.s_log_block_size); 442 nilfsdev->blocksize = (uint64_t) 1 << (log_blocksize + 10); 443 /* TODO check superblock's blocksize limits */ 444 445 /* calculate dat structure parameters */ 446 nilfs_calc_mdt_consts(nilfsdev, &nilfsdev->dat_mdt, 447 nilfs_rw16(nilfsdev->super.s_dat_entry_size)); 448 nilfs_calc_mdt_consts(nilfsdev, &nilfsdev->ifile_mdt, 449 nilfs_rw16(nilfsdev->super.s_inode_size)); 450 451 DPRINTF(VOLUMES, ("nilfs_mount: accepted super block\n")); 452 453 /* search for the super root and roll forward when needed */ 454 nilfs_search_super_root(nilfsdev); 455 456 nilfsdev->mount_state = nilfs_rw16(nilfsdev->super.s_state); 457 if (nilfsdev->mount_state != NILFS_VALID_FS) { 458 printf("FS is seriously damaged, needs repairing\n"); 459 printf("aborting mount\n"); 460 return EINVAL; 461 } 462 463 /* 464 * FS should be ok now. The superblock and the last segsum could be 465 * updated from the repair so extract running values again. 466 */ 467 last_pseg = nilfs_rw64(nilfsdev->super.s_last_pseg); /*blknr */ 468 last_cno = nilfs_rw64(nilfsdev->super.s_last_cno); 469 last_seq = nilfs_rw64(nilfsdev->super.s_last_seq); 470 471 nilfsdev->last_seg_seq = last_seq; 472 nilfsdev->last_seg_num = nilfs_get_segnum_of_block(nilfsdev, last_pseg); 473 nilfsdev->next_seg_num = nilfs_get_segnum_of_block(nilfsdev, 474 nilfs_rw64(nilfsdev->last_segsum.ss_next)); 475 nilfsdev->last_cno = last_cno; 476 477 DPRINTF(VOLUMES, ("nilfs_mount: accepted super root\n")); 478 479 /* create system vnodes for DAT, CP and SEGSUM */ 480 error = nilfs_create_system_nodes(nilfsdev); 481 if (error) 482 nilfs_unmount_base(nilfsdev); 483 return error; 484 } 485 486 487 static void 488 nilfs_unmount_device(struct nilfs_device *nilfsdev) 489 { 490 int error; 491 492 /* is there anything? */ 493 if (nilfsdev == NULL) 494 return; 495 496 /* remove the device only if we're the last reference */ 497 nilfsdev->refcnt--; 498 if (nilfsdev->refcnt >= 1) 499 return; 500 501 /* unmount our base */ 502 nilfs_unmount_base(nilfsdev); 503 504 /* remove from our device list */ 505 SLIST_REMOVE(&nilfs_devices, nilfsdev, nilfs_device, next_device); 506 507 /* close device */ 508 DPRINTF(VOLUMES, ("closing device\n")); 509 510 /* remove our mount reference before closing device */ 511 nilfsdev->devvp->v_specmountpoint = NULL; 512 513 /* devvp is still locked by us */ 514 vn_lock(nilfsdev->devvp, LK_EXCLUSIVE | LK_RETRY); 515 error = VOP_CLOSE(nilfsdev->devvp, FREAD | FWRITE, NOCRED); 516 if (error) 517 printf("Error during closure of device! error %d, " 518 "device might stay locked\n", error); 519 DPRINTF(VOLUMES, ("device close ok\n")); 520 521 /* clear our mount reference and release device node */ 522 vput(nilfsdev->devvp); 523 524 /* free our device info */ 525 free(nilfsdev, M_NILFSMNT); 526 } 527 528 529 static int 530 nilfs_check_mounts(struct nilfs_device *nilfsdev, struct mount *mp, 531 struct nilfs_args *args) 532 { 533 struct nilfs_mount *ump; 534 uint64_t last_cno; 535 536 /* no double-mounting of the same checkpoint */ 537 STAILQ_FOREACH(ump, &nilfsdev->mounts, next_mount) { 538 if (ump->mount_args.cpno == args->cpno) 539 return EBUSY; 540 } 541 542 /* allow readonly mounts without questioning here */ 543 if (mp->mnt_flag & MNT_RDONLY) 544 return 0; 545 546 /* readwrite mount you want */ 547 STAILQ_FOREACH(ump, &nilfsdev->mounts, next_mount) { 548 /* only one RW mount on this device! */ 549 if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY)==0) 550 return EROFS; 551 /* RDONLY on last mountpoint is device busy */ 552 last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno); 553 if (ump->mount_args.cpno == last_cno) 554 return EBUSY; 555 } 556 557 /* OK for now */ 558 return 0; 559 } 560 561 562 static int 563 nilfs_mount_device(struct vnode *devvp, struct mount *mp, struct nilfs_args *args, 564 struct nilfs_device **nilfsdev_p) 565 { 566 uint64_t psize; 567 unsigned secsize; 568 struct nilfs_device *nilfsdev; 569 struct lwp *l = curlwp; 570 int openflags, accessmode, error; 571 572 DPRINTF(VOLUMES, ("Mounting NILFS device\n")); 573 574 /* lookup device in our nilfs_mountpoints */ 575 *nilfsdev_p = NULL; 576 SLIST_FOREACH(nilfsdev, &nilfs_devices, next_device) 577 if (nilfsdev->devvp == devvp) 578 break; 579 580 if (nilfsdev) { 581 DPRINTF(VOLUMES, ("device already mounted\n")); 582 error = nilfs_check_mounts(nilfsdev, mp, args); 583 if (error) 584 return error; 585 nilfsdev->refcnt++; 586 *nilfsdev_p = nilfsdev; 587 return 0; 588 } 589 590 DPRINTF(VOLUMES, ("no previous mounts on this device, mounting device\n")); 591 592 /* check if its a block device specified */ 593 if (devvp->v_type != VBLK) { 594 vrele(devvp); 595 return ENOTBLK; 596 } 597 if (bdevsw_lookup(devvp->v_rdev) == NULL) { 598 vrele(devvp); 599 return ENXIO; 600 } 601 602 /* 603 * If mount by non-root, then verify that user has necessary 604 * permissions on the device. 605 */ 606 accessmode = VREAD; 607 if ((mp->mnt_flag & MNT_RDONLY) == 0) 608 accessmode |= VWRITE; 609 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 610 error = genfs_can_mount(devvp, accessmode, l->l_cred); 611 VOP_UNLOCK(devvp); 612 if (error) { 613 vrele(devvp); 614 return error; 615 } 616 617 /* 618 * Open device read-write; TODO how about upgrading later when needed? 619 */ 620 openflags = FREAD | FWRITE; 621 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 622 error = VOP_OPEN(devvp, openflags, FSCRED); 623 VOP_UNLOCK(devvp); 624 if (error) { 625 vrele(devvp); 626 return error; 627 } 628 629 /* opened ok, try mounting */ 630 nilfsdev = malloc(sizeof(*nilfsdev), M_NILFSMNT, M_WAITOK | M_ZERO); 631 632 /* initialise */ 633 nilfsdev->refcnt = 1; 634 nilfsdev->devvp = devvp; 635 nilfsdev->uncomitted_bl = 0; 636 cv_init(&nilfsdev->sync_cv, "nilfssyn"); 637 STAILQ_INIT(&nilfsdev->mounts); 638 639 /* register nilfs_device in list */ 640 SLIST_INSERT_HEAD(&nilfs_devices, nilfsdev, next_device); 641 642 /* get our device's size */ 643 error = getdisksize(devvp, &psize, &secsize); 644 if (error) { 645 /* remove all our information */ 646 nilfs_unmount_device(nilfsdev); 647 return EINVAL; 648 } 649 650 nilfsdev->devsize = psize * secsize; 651 652 /* connect to the head for most recent files XXX really pass mp and args? */ 653 error = nilfs_mount_base(nilfsdev, mp, args); 654 if (error) { 655 /* remove all our information */ 656 nilfs_unmount_device(nilfsdev); 657 return EINVAL; 658 } 659 660 *nilfsdev_p = nilfsdev; 661 DPRINTF(VOLUMES, ("NILFS device mounted ok\n")); 662 663 return 0; 664 } 665 666 667 static int 668 nilfs_mount_checkpoint(struct nilfs_mount *ump) 669 { 670 struct nilfs_cpfile_header *cphdr; 671 struct nilfs_checkpoint *cp; 672 struct nilfs_inode ifile_inode; 673 struct nilfs_node *cp_node; 674 struct buf *bp; 675 uint64_t ncp, nsn, fcpno, blocknr, last_cno; 676 uint32_t off, dlen; 677 int cp_per_block, error; 678 679 DPRINTF(VOLUMES, ("mount_nilfs: trying to mount checkpoint number " 680 "%"PRIu64"\n", ump->mount_args.cpno)); 681 682 cp_node = ump->nilfsdev->cp_node; 683 684 /* get cpfile header from 1st block of cp file */ 685 error = nilfs_bread(cp_node, 0, NOCRED, 0, &bp); 686 if (error) 687 return error; 688 cphdr = (struct nilfs_cpfile_header *) bp->b_data; 689 ncp = nilfs_rw64(cphdr->ch_ncheckpoints); 690 nsn = nilfs_rw64(cphdr->ch_nsnapshots); 691 692 brelse(bp, BC_AGE); 693 694 DPRINTF(VOLUMES, ("mount_nilfs: checkpoint header read in\n")); 695 DPRINTF(VOLUMES, ("\tNumber of checkpoints %"PRIu64"\n", ncp)); 696 DPRINTF(VOLUMES, ("\tNumber of snapshots %"PRIu64"\n", nsn)); 697 698 /* read in our specified checkpoint */ 699 dlen = nilfs_rw16(ump->nilfsdev->super.s_checkpoint_size); 700 cp_per_block = ump->nilfsdev->blocksize / dlen; 701 702 fcpno = ump->mount_args.cpno + NILFS_CPFILE_FIRST_CHECKPOINT_OFFSET -1; 703 blocknr = fcpno / cp_per_block; 704 off = (fcpno % cp_per_block) * dlen; 705 706 error = nilfs_bread(cp_node, blocknr, NOCRED, 0, &bp); 707 if (error) { 708 printf("mount_nilfs: couldn't read cp block %"PRIu64"\n", 709 fcpno); 710 return EINVAL; 711 } 712 713 /* needs to be a valid checkpoint */ 714 cp = (struct nilfs_checkpoint *) ((uint8_t *) bp->b_data + off); 715 if (cp->cp_flags & NILFS_CHECKPOINT_INVALID) { 716 printf("mount_nilfs: checkpoint marked invalid\n"); 717 brelse(bp, BC_AGE); 718 return EINVAL; 719 } 720 721 /* is this really the checkpoint we want? */ 722 if (nilfs_rw64(cp->cp_cno) != ump->mount_args.cpno) { 723 printf("mount_nilfs: checkpoint file corrupt? " 724 "expected cpno %"PRIu64", found cpno %"PRIu64"\n", 725 ump->mount_args.cpno, nilfs_rw64(cp->cp_cno)); 726 brelse(bp, BC_AGE); 727 return EINVAL; 728 } 729 730 /* check if its a snapshot ! */ 731 last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno); 732 if (ump->mount_args.cpno != last_cno) { 733 /* only allow snapshots if not mounting on the last cp */ 734 if ((cp->cp_flags & NILFS_CHECKPOINT_SNAPSHOT) == 0) { 735 printf( "mount_nilfs: checkpoint %"PRIu64" is not a " 736 "snapshot\n", ump->mount_args.cpno); 737 brelse(bp, BC_AGE); 738 return EINVAL; 739 } 740 } 741 742 ifile_inode = cp->cp_ifile_inode; 743 brelse(bp, BC_AGE); 744 745 /* get ifile inode */ 746 error = nilfs_get_node_raw(ump->nilfsdev, NULL, NILFS_IFILE_INO, 747 &ifile_inode, &ump->ifile_node); 748 if (error) { 749 printf("mount_nilfs: can't read ifile node\n"); 750 return EINVAL; 751 } 752 NILFS_SET_SYSTEMFILE(ump->ifile_node->vnode); 753 754 /* get root node? */ 755 756 return 0; 757 } 758 759 760 static int 761 nilfs_stop_writing(struct nilfs_mount *ump) 762 { 763 /* readonly mounts won't write */ 764 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) 765 return 0; 766 767 DPRINTF(CALL, ("nilfs_stop_writing called for RW mount\n")); 768 769 /* TODO writeout super_block? */ 770 /* XXX no support for writing yet anyway */ 771 return 0; 772 } 773 774 775 /* --------------------------------------------------------------------- */ 776 777 778 779 #define MPFREE(a, lst) \ 780 if ((a)) free((a), lst); 781 static void 782 free_nilfs_mountinfo(struct mount *mp) 783 { 784 struct nilfs_mount *ump = VFSTONILFS(mp); 785 786 if (ump == NULL) 787 return; 788 789 mutex_destroy(&ump->ihash_lock); 790 mutex_destroy(&ump->get_node_lock); 791 MPFREE(ump, M_NILFSMNT); 792 } 793 #undef MPFREE 794 795 int 796 nilfs_mount(struct mount *mp, const char *path, 797 void *data, size_t *data_len) 798 { 799 struct nilfs_args *args = data; 800 struct nilfs_device *nilfsdev; 801 struct nilfs_mount *ump; 802 struct vnode *devvp; 803 int lst, error; 804 805 DPRINTF(VFSCALL, ("nilfs_mount called\n")); 806 807 if (*data_len < sizeof *args) 808 return EINVAL; 809 810 if (mp->mnt_flag & MNT_GETARGS) { 811 /* request for the mount arguments */ 812 ump = VFSTONILFS(mp); 813 if (ump == NULL) 814 return EINVAL; 815 *args = ump->mount_args; 816 *data_len = sizeof *args; 817 return 0; 818 } 819 820 /* check/translate struct version */ 821 if (args->version != 1) { 822 printf("mount_nilfs: unrecognized argument structure version\n"); 823 return EINVAL; 824 } 825 /* TODO sanity checking other mount arguments */ 826 827 /* handle request for updating mount parameters */ 828 if (mp->mnt_flag & MNT_UPDATE) { 829 /* TODO can't update my mountpoint yet */ 830 return EOPNOTSUPP; 831 } 832 833 /* lookup name to get its vnode */ 834 error = namei_simple_user(args->fspec, NSM_FOLLOW_NOEMULROOT, &devvp); 835 if (error) 836 return error; 837 838 #ifdef DEBUG 839 if (nilfs_verbose & NILFS_DEBUG_VOLUMES) 840 vprint("NILFS mount, trying to mount \n", devvp); 841 #endif 842 843 error = nilfs_mount_device(devvp, mp, args, &nilfsdev); 844 if (error) 845 return error; 846 847 /* 848 * Create a nilfs_mount on the specified checkpoint. Note that only 849 * ONE RW mount point can exist and it needs to have the highest 850 * checkpoint nr. If mounting RW and its not on the last checkpoint we 851 * need to invalidate all checkpoints that follow!!! This is an 852 * advanced option. 853 */ 854 855 /* setup basic mountpoint structure */ 856 mp->mnt_data = NULL; 857 mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev; 858 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NILFS); 859 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 860 mp->mnt_stat.f_namemax = NILFS_NAME_LEN; 861 mp->mnt_flag |= MNT_LOCAL; 862 863 /* XXX can't enable MPSAFE yet since genfs barfs on bad CV */ 864 // mp->mnt_iflag |= IMNT_MPSAFE; 865 866 /* set our dev and fs units */ 867 mp->mnt_dev_bshift = nilfs_rw32(nilfsdev->super.s_log_block_size) + 10; 868 mp->mnt_fs_bshift = mp->mnt_dev_bshift; 869 870 /* allocate nilfs part of mount structure; malloc always succeeds */ 871 ump = malloc(sizeof(struct nilfs_mount), M_NILFSMNT, M_WAITOK | M_ZERO); 872 873 /* init locks */ 874 mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE); 875 mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE); 876 877 /* init our hash table for inode lookup */ 878 for (lst = 0; lst < NILFS_INODE_HASHSIZE; lst++) { 879 LIST_INIT(&ump->nilfs_nodes[lst]); 880 } 881 882 /* set up linkage */ 883 mp->mnt_data = ump; 884 ump->vfs_mountp = mp; 885 ump->nilfsdev = nilfsdev; 886 887 #if 0 888 #ifndef NILFS_READWRITE 889 /* force read-only for now */ 890 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 891 printf( "Enable kernel/module option NILFS_READWRITE for " 892 "writing, downgrading access to read-only\n"); 893 mp->mnt_flag |= MNT_RDONLY; 894 } 895 #endif 896 #endif 897 898 /* DONT register our nilfs mountpoint on our vfs mountpoint */ 899 devvp->v_specmountpoint = NULL; 900 #if 0 901 if (devvp->v_specmountpoint == NULL) 902 devvp->v_specmountpoint = mp; 903 if ((mp->mnt_flag & MNT_RDONLY) == 0) 904 devvp->v_specmountpoint = mp; 905 #endif 906 907 /* add our mountpoint */ 908 STAILQ_INSERT_TAIL(&nilfsdev->mounts, ump, next_mount); 909 910 /* get our selected checkpoint */ 911 if (args->cpno == 0) 912 args->cpno = nilfsdev->last_cno; 913 args->cpno = MIN(args->cpno, nilfsdev->last_cno); 914 915 /* setting up other parameters */ 916 ump->mount_args = *args; 917 error = nilfs_mount_checkpoint(ump); 918 if (error) { 919 nilfs_unmount(mp, MNT_FORCE); 920 return error; 921 } 922 923 /* set VFS info */ 924 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 925 mp->mnt_op->vfs_name, mp, curlwp); 926 if (error) { 927 nilfs_unmount(mp, MNT_FORCE); 928 return error; 929 } 930 931 /* successfully mounted */ 932 DPRINTF(VOLUMES, ("nilfs_mount() successfull\n")); 933 934 return 0; 935 } 936 937 /* --------------------------------------------------------------------- */ 938 939 940 /* remove our mountpoint and if its the last reference, remove our device */ 941 int 942 nilfs_unmount(struct mount *mp, int mntflags) 943 { 944 struct nilfs_device *nilfsdev; 945 struct nilfs_mount *ump; 946 int error, flags; 947 948 DPRINTF(VFSCALL, ("nilfs_umount called\n")); 949 950 ump = VFSTONILFS(mp); 951 if (!ump) 952 panic("NILFS unmount: empty ump\n"); 953 nilfsdev = ump->nilfsdev; 954 955 /* 956 * Flush all nodes associated to this mountpoint. By specifying 957 * SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM. This hardly 958 * documented feature allows us to exempt certain files from being 959 * flushed. 960 */ 961 flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0; 962 if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0) 963 return error; 964 965 /* if we're the write mount, we ought to close the writing session */ 966 error = nilfs_stop_writing(ump); 967 if (error) 968 return error; 969 970 if (ump->ifile_node) 971 NILFS_UNSET_SYSTEMFILE(ump->ifile_node->vnode); 972 nilfs_dispose_node(&ump->ifile_node); 973 974 /* remove our mount point */ 975 STAILQ_REMOVE(&nilfsdev->mounts, ump, nilfs_mount, next_mount); 976 free_nilfs_mountinfo(mp); 977 978 /* free ump struct references */ 979 mp->mnt_data = NULL; 980 mp->mnt_flag &= ~MNT_LOCAL; 981 982 /* unmount the device itself when we're the last one */ 983 nilfs_unmount_device(nilfsdev); 984 985 DPRINTF(VOLUMES, ("Fin unmount\n")); 986 return error; 987 } 988 989 /* --------------------------------------------------------------------- */ 990 991 int 992 nilfs_start(struct mount *mp, int flags) 993 { 994 /* do we have to do something here? */ 995 return 0; 996 } 997 998 /* --------------------------------------------------------------------- */ 999 1000 int 1001 nilfs_root(struct mount *mp, struct vnode **vpp) 1002 { 1003 struct nilfs_mount *ump = VFSTONILFS(mp); 1004 struct nilfs_node *node; 1005 int error; 1006 1007 DPRINTF(NODE, ("nilfs_root called\n")); 1008 1009 error = nilfs_get_node(ump, NILFS_ROOT_INO, &node); 1010 if (node) { 1011 *vpp = node->vnode; 1012 KASSERT(node->vnode->v_vflag & VV_ROOT); 1013 } 1014 1015 DPRINTF(NODE, ("nilfs_root finished\n")); 1016 return error; 1017 } 1018 1019 /* --------------------------------------------------------------------- */ 1020 1021 int 1022 nilfs_statvfs(struct mount *mp, struct statvfs *sbp) 1023 { 1024 struct nilfs_mount *ump = VFSTONILFS(mp); 1025 uint32_t blocksize; 1026 1027 DPRINTF(VFSCALL, ("nilfs_statvfs called\n")); 1028 1029 blocksize = ump->nilfsdev->blocksize; 1030 sbp->f_flag = mp->mnt_flag; 1031 sbp->f_bsize = blocksize; 1032 sbp->f_frsize = blocksize; 1033 sbp->f_iosize = blocksize; 1034 1035 copy_statvfs_info(sbp, mp); 1036 return 0; 1037 } 1038 1039 /* --------------------------------------------------------------------- */ 1040 1041 int 1042 nilfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 1043 { 1044 // struct nilfs_mount *ump = VFSTONILFS(mp); 1045 1046 DPRINTF(VFSCALL, ("nilfs_sync called\n")); 1047 /* if called when mounted readonly, just ignore */ 1048 if (mp->mnt_flag & MNT_RDONLY) 1049 return 0; 1050 1051 DPRINTF(VFSCALL, ("end of nilfs_sync()\n")); 1052 1053 return 0; 1054 } 1055 1056 /* --------------------------------------------------------------------- */ 1057 1058 /* 1059 * Get vnode for the file system type specific file id ino for the fs. Its 1060 * used for reference to files by unique ID and for NFSv3. 1061 * (optional) TODO lookup why some sources state NFSv3 1062 */ 1063 int 1064 nilfs_vget(struct mount *mp, ino_t ino, 1065 struct vnode **vpp) 1066 { 1067 DPRINTF(NOTIMPL, ("nilfs_vget called\n")); 1068 return EOPNOTSUPP; 1069 } 1070 1071 /* --------------------------------------------------------------------- */ 1072 1073 /* 1074 * Lookup vnode for file handle specified 1075 */ 1076 int 1077 nilfs_fhtovp(struct mount *mp, struct fid *fhp, 1078 struct vnode **vpp) 1079 { 1080 DPRINTF(NOTIMPL, ("nilfs_fhtovp called\n")); 1081 return EOPNOTSUPP; 1082 } 1083 1084 /* --------------------------------------------------------------------- */ 1085 1086 /* 1087 * Create an unique file handle. Its structure is opaque and won't be used by 1088 * other subsystems. It should uniquely identify the file in the filingsystem 1089 * and enough information to know if a file has been removed and/or resources 1090 * have been recycled. 1091 */ 1092 int 1093 nilfs_vptofh(struct vnode *vp, struct fid *fid, 1094 size_t *fh_size) 1095 { 1096 DPRINTF(NOTIMPL, ("nilfs_vptofh called\n")); 1097 return EOPNOTSUPP; 1098 } 1099 1100 /* --------------------------------------------------------------------- */ 1101 1102 /* 1103 * Create a file system snapshot at the specified timestamp. 1104 */ 1105 int 1106 nilfs_snapshot(struct mount *mp, struct vnode *vp, 1107 struct timespec *tm) 1108 { 1109 DPRINTF(NOTIMPL, ("nilfs_snapshot called\n")); 1110 return EOPNOTSUPP; 1111 } 1112 1113 /* --------------------------------------------------------------------- */ 1114