1 /* $NetBSD: nilfs_vfsops.c,v 1.9 2012/03/13 18:40:38 elad 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.9 2012/03/13 18:40:38 elad 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 = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 611 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode)); 612 VOP_UNLOCK(devvp); 613 if (error) { 614 vrele(devvp); 615 return error; 616 } 617 618 /* 619 * Open device read-write; TODO how about upgrading later when needed? 620 */ 621 openflags = FREAD | FWRITE; 622 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 623 error = VOP_OPEN(devvp, openflags, FSCRED); 624 VOP_UNLOCK(devvp); 625 if (error) { 626 vrele(devvp); 627 return error; 628 } 629 630 /* opened ok, try mounting */ 631 nilfsdev = malloc(sizeof(*nilfsdev), M_NILFSMNT, M_WAITOK | M_ZERO); 632 633 /* initialise */ 634 nilfsdev->refcnt = 1; 635 nilfsdev->devvp = devvp; 636 nilfsdev->uncomitted_bl = 0; 637 cv_init(&nilfsdev->sync_cv, "nilfssyn"); 638 STAILQ_INIT(&nilfsdev->mounts); 639 640 /* register nilfs_device in list */ 641 SLIST_INSERT_HEAD(&nilfs_devices, nilfsdev, next_device); 642 643 /* get our device's size */ 644 error = getdisksize(devvp, &psize, &secsize); 645 if (error) { 646 /* remove all our information */ 647 nilfs_unmount_device(nilfsdev); 648 return EINVAL; 649 } 650 651 nilfsdev->devsize = psize * secsize; 652 653 /* connect to the head for most recent files XXX really pass mp and args? */ 654 error = nilfs_mount_base(nilfsdev, mp, args); 655 if (error) { 656 /* remove all our information */ 657 nilfs_unmount_device(nilfsdev); 658 return EINVAL; 659 } 660 661 *nilfsdev_p = nilfsdev; 662 DPRINTF(VOLUMES, ("NILFS device mounted ok\n")); 663 664 return 0; 665 } 666 667 668 static int 669 nilfs_mount_checkpoint(struct nilfs_mount *ump) 670 { 671 struct nilfs_cpfile_header *cphdr; 672 struct nilfs_checkpoint *cp; 673 struct nilfs_inode ifile_inode; 674 struct nilfs_node *cp_node; 675 struct buf *bp; 676 uint64_t ncp, nsn, fcpno, blocknr, last_cno; 677 uint32_t off, dlen; 678 int cp_per_block, error; 679 680 DPRINTF(VOLUMES, ("mount_nilfs: trying to mount checkpoint number " 681 "%"PRIu64"\n", ump->mount_args.cpno)); 682 683 cp_node = ump->nilfsdev->cp_node; 684 685 /* get cpfile header from 1st block of cp file */ 686 error = nilfs_bread(cp_node, 0, NOCRED, 0, &bp); 687 if (error) 688 return error; 689 cphdr = (struct nilfs_cpfile_header *) bp->b_data; 690 ncp = nilfs_rw64(cphdr->ch_ncheckpoints); 691 nsn = nilfs_rw64(cphdr->ch_nsnapshots); 692 693 brelse(bp, BC_AGE); 694 695 DPRINTF(VOLUMES, ("mount_nilfs: checkpoint header read in\n")); 696 DPRINTF(VOLUMES, ("\tNumber of checkpoints %"PRIu64"\n", ncp)); 697 DPRINTF(VOLUMES, ("\tNumber of snapshots %"PRIu64"\n", nsn)); 698 699 /* read in our specified checkpoint */ 700 dlen = nilfs_rw16(ump->nilfsdev->super.s_checkpoint_size); 701 cp_per_block = ump->nilfsdev->blocksize / dlen; 702 703 fcpno = ump->mount_args.cpno + NILFS_CPFILE_FIRST_CHECKPOINT_OFFSET -1; 704 blocknr = fcpno / cp_per_block; 705 off = (fcpno % cp_per_block) * dlen; 706 707 error = nilfs_bread(cp_node, blocknr, NOCRED, 0, &bp); 708 if (error) { 709 printf("mount_nilfs: couldn't read cp block %"PRIu64"\n", 710 fcpno); 711 return EINVAL; 712 } 713 714 /* needs to be a valid checkpoint */ 715 cp = (struct nilfs_checkpoint *) ((uint8_t *) bp->b_data + off); 716 if (cp->cp_flags & NILFS_CHECKPOINT_INVALID) { 717 printf("mount_nilfs: checkpoint marked invalid\n"); 718 brelse(bp, BC_AGE); 719 return EINVAL; 720 } 721 722 /* is this really the checkpoint we want? */ 723 if (nilfs_rw64(cp->cp_cno) != ump->mount_args.cpno) { 724 printf("mount_nilfs: checkpoint file corrupt? " 725 "expected cpno %"PRIu64", found cpno %"PRIu64"\n", 726 ump->mount_args.cpno, nilfs_rw64(cp->cp_cno)); 727 brelse(bp, BC_AGE); 728 return EINVAL; 729 } 730 731 /* check if its a snapshot ! */ 732 last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno); 733 if (ump->mount_args.cpno != last_cno) { 734 /* only allow snapshots if not mounting on the last cp */ 735 if ((cp->cp_flags & NILFS_CHECKPOINT_SNAPSHOT) == 0) { 736 printf( "mount_nilfs: checkpoint %"PRIu64" is not a " 737 "snapshot\n", ump->mount_args.cpno); 738 brelse(bp, BC_AGE); 739 return EINVAL; 740 } 741 } 742 743 ifile_inode = cp->cp_ifile_inode; 744 brelse(bp, BC_AGE); 745 746 /* get ifile inode */ 747 error = nilfs_get_node_raw(ump->nilfsdev, NULL, NILFS_IFILE_INO, 748 &ifile_inode, &ump->ifile_node); 749 if (error) { 750 printf("mount_nilfs: can't read ifile node\n"); 751 return EINVAL; 752 } 753 NILFS_SET_SYSTEMFILE(ump->ifile_node->vnode); 754 755 /* get root node? */ 756 757 return 0; 758 } 759 760 761 static int 762 nilfs_stop_writing(struct nilfs_mount *ump) 763 { 764 /* readonly mounts won't write */ 765 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) 766 return 0; 767 768 DPRINTF(CALL, ("nilfs_stop_writing called for RW mount\n")); 769 770 /* TODO writeout super_block? */ 771 /* XXX no support for writing yet anyway */ 772 return 0; 773 } 774 775 776 /* --------------------------------------------------------------------- */ 777 778 779 780 #define MPFREE(a, lst) \ 781 if ((a)) free((a), lst); 782 static void 783 free_nilfs_mountinfo(struct mount *mp) 784 { 785 struct nilfs_mount *ump = VFSTONILFS(mp); 786 787 if (ump == NULL) 788 return; 789 790 mutex_destroy(&ump->ihash_lock); 791 mutex_destroy(&ump->get_node_lock); 792 MPFREE(ump, M_NILFSMNT); 793 } 794 #undef MPFREE 795 796 int 797 nilfs_mount(struct mount *mp, const char *path, 798 void *data, size_t *data_len) 799 { 800 struct nilfs_args *args = data; 801 struct nilfs_device *nilfsdev; 802 struct nilfs_mount *ump; 803 struct vnode *devvp; 804 int lst, error; 805 806 DPRINTF(VFSCALL, ("nilfs_mount called\n")); 807 808 if (*data_len < sizeof *args) 809 return EINVAL; 810 811 if (mp->mnt_flag & MNT_GETARGS) { 812 /* request for the mount arguments */ 813 ump = VFSTONILFS(mp); 814 if (ump == NULL) 815 return EINVAL; 816 *args = ump->mount_args; 817 *data_len = sizeof *args; 818 return 0; 819 } 820 821 /* check/translate struct version */ 822 if (args->version != 1) { 823 printf("mount_nilfs: unrecognized argument structure version\n"); 824 return EINVAL; 825 } 826 /* TODO sanity checking other mount arguments */ 827 828 /* handle request for updating mount parameters */ 829 if (mp->mnt_flag & MNT_UPDATE) { 830 /* TODO can't update my mountpoint yet */ 831 return EOPNOTSUPP; 832 } 833 834 /* lookup name to get its vnode */ 835 error = namei_simple_user(args->fspec, NSM_FOLLOW_NOEMULROOT, &devvp); 836 if (error) 837 return error; 838 839 #ifdef DEBUG 840 if (nilfs_verbose & NILFS_DEBUG_VOLUMES) 841 vprint("NILFS mount, trying to mount \n", devvp); 842 #endif 843 844 error = nilfs_mount_device(devvp, mp, args, &nilfsdev); 845 if (error) 846 return error; 847 848 /* 849 * Create a nilfs_mount on the specified checkpoint. Note that only 850 * ONE RW mount point can exist and it needs to have the highest 851 * checkpoint nr. If mounting RW and its not on the last checkpoint we 852 * need to invalidate all checkpoints that follow!!! This is an 853 * advanced option. 854 */ 855 856 /* setup basic mountpoint structure */ 857 mp->mnt_data = NULL; 858 mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev; 859 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NILFS); 860 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 861 mp->mnt_stat.f_namemax = NILFS_NAME_LEN; 862 mp->mnt_flag |= MNT_LOCAL; 863 864 /* XXX can't enable MPSAFE yet since genfs barfs on bad CV */ 865 // mp->mnt_iflag |= IMNT_MPSAFE; 866 867 /* set our dev and fs units */ 868 mp->mnt_dev_bshift = nilfs_rw32(nilfsdev->super.s_log_block_size) + 10; 869 mp->mnt_fs_bshift = mp->mnt_dev_bshift; 870 871 /* allocate nilfs part of mount structure; malloc always succeeds */ 872 ump = malloc(sizeof(struct nilfs_mount), M_NILFSMNT, M_WAITOK | M_ZERO); 873 874 /* init locks */ 875 mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE); 876 mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE); 877 878 /* init our hash table for inode lookup */ 879 for (lst = 0; lst < NILFS_INODE_HASHSIZE; lst++) { 880 LIST_INIT(&ump->nilfs_nodes[lst]); 881 } 882 883 /* set up linkage */ 884 mp->mnt_data = ump; 885 ump->vfs_mountp = mp; 886 ump->nilfsdev = nilfsdev; 887 888 #if 0 889 #ifndef NILFS_READWRITE 890 /* force read-only for now */ 891 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 892 printf( "Enable kernel/module option NILFS_READWRITE for " 893 "writing, downgrading access to read-only\n"); 894 mp->mnt_flag |= MNT_RDONLY; 895 } 896 #endif 897 #endif 898 899 /* DONT register our nilfs mountpoint on our vfs mountpoint */ 900 devvp->v_specmountpoint = NULL; 901 #if 0 902 if (devvp->v_specmountpoint == NULL) 903 devvp->v_specmountpoint = mp; 904 if ((mp->mnt_flag & MNT_RDONLY) == 0) 905 devvp->v_specmountpoint = mp; 906 #endif 907 908 /* add our mountpoint */ 909 STAILQ_INSERT_TAIL(&nilfsdev->mounts, ump, next_mount); 910 911 /* get our selected checkpoint */ 912 if (args->cpno == 0) 913 args->cpno = nilfsdev->last_cno; 914 args->cpno = MIN(args->cpno, nilfsdev->last_cno); 915 916 /* setting up other parameters */ 917 ump->mount_args = *args; 918 error = nilfs_mount_checkpoint(ump); 919 if (error) { 920 nilfs_unmount(mp, MNT_FORCE); 921 return error; 922 } 923 924 /* set VFS info */ 925 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 926 mp->mnt_op->vfs_name, mp, curlwp); 927 if (error) { 928 nilfs_unmount(mp, MNT_FORCE); 929 return error; 930 } 931 932 /* successfully mounted */ 933 DPRINTF(VOLUMES, ("nilfs_mount() successfull\n")); 934 935 return 0; 936 } 937 938 /* --------------------------------------------------------------------- */ 939 940 941 /* remove our mountpoint and if its the last reference, remove our device */ 942 int 943 nilfs_unmount(struct mount *mp, int mntflags) 944 { 945 struct nilfs_device *nilfsdev; 946 struct nilfs_mount *ump; 947 int error, flags; 948 949 DPRINTF(VFSCALL, ("nilfs_umount called\n")); 950 951 ump = VFSTONILFS(mp); 952 if (!ump) 953 panic("NILFS unmount: empty ump\n"); 954 nilfsdev = ump->nilfsdev; 955 956 /* 957 * Flush all nodes associated to this mountpoint. By specifying 958 * SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM. This hardly 959 * documented feature allows us to exempt certain files from being 960 * flushed. 961 */ 962 flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0; 963 if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0) 964 return error; 965 966 /* if we're the write mount, we ought to close the writing session */ 967 error = nilfs_stop_writing(ump); 968 if (error) 969 return error; 970 971 if (ump->ifile_node) 972 NILFS_UNSET_SYSTEMFILE(ump->ifile_node->vnode); 973 nilfs_dispose_node(&ump->ifile_node); 974 975 /* remove our mount point */ 976 STAILQ_REMOVE(&nilfsdev->mounts, ump, nilfs_mount, next_mount); 977 free_nilfs_mountinfo(mp); 978 979 /* free ump struct references */ 980 mp->mnt_data = NULL; 981 mp->mnt_flag &= ~MNT_LOCAL; 982 983 /* unmount the device itself when we're the last one */ 984 nilfs_unmount_device(nilfsdev); 985 986 DPRINTF(VOLUMES, ("Fin unmount\n")); 987 return error; 988 } 989 990 /* --------------------------------------------------------------------- */ 991 992 int 993 nilfs_start(struct mount *mp, int flags) 994 { 995 /* do we have to do something here? */ 996 return 0; 997 } 998 999 /* --------------------------------------------------------------------- */ 1000 1001 int 1002 nilfs_root(struct mount *mp, struct vnode **vpp) 1003 { 1004 struct nilfs_mount *ump = VFSTONILFS(mp); 1005 struct nilfs_node *node; 1006 int error; 1007 1008 DPRINTF(NODE, ("nilfs_root called\n")); 1009 1010 error = nilfs_get_node(ump, NILFS_ROOT_INO, &node); 1011 if (node) { 1012 *vpp = node->vnode; 1013 KASSERT(node->vnode->v_vflag & VV_ROOT); 1014 } 1015 1016 DPRINTF(NODE, ("nilfs_root finished\n")); 1017 return error; 1018 } 1019 1020 /* --------------------------------------------------------------------- */ 1021 1022 int 1023 nilfs_statvfs(struct mount *mp, struct statvfs *sbp) 1024 { 1025 struct nilfs_mount *ump = VFSTONILFS(mp); 1026 uint32_t blocksize; 1027 1028 DPRINTF(VFSCALL, ("nilfs_statvfs called\n")); 1029 1030 blocksize = ump->nilfsdev->blocksize; 1031 sbp->f_flag = mp->mnt_flag; 1032 sbp->f_bsize = blocksize; 1033 sbp->f_frsize = blocksize; 1034 sbp->f_iosize = blocksize; 1035 1036 copy_statvfs_info(sbp, mp); 1037 return 0; 1038 } 1039 1040 /* --------------------------------------------------------------------- */ 1041 1042 int 1043 nilfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 1044 { 1045 // struct nilfs_mount *ump = VFSTONILFS(mp); 1046 1047 DPRINTF(VFSCALL, ("nilfs_sync called\n")); 1048 /* if called when mounted readonly, just ignore */ 1049 if (mp->mnt_flag & MNT_RDONLY) 1050 return 0; 1051 1052 DPRINTF(VFSCALL, ("end of nilfs_sync()\n")); 1053 1054 return 0; 1055 } 1056 1057 /* --------------------------------------------------------------------- */ 1058 1059 /* 1060 * Get vnode for the file system type specific file id ino for the fs. Its 1061 * used for reference to files by unique ID and for NFSv3. 1062 * (optional) TODO lookup why some sources state NFSv3 1063 */ 1064 int 1065 nilfs_vget(struct mount *mp, ino_t ino, 1066 struct vnode **vpp) 1067 { 1068 DPRINTF(NOTIMPL, ("nilfs_vget called\n")); 1069 return EOPNOTSUPP; 1070 } 1071 1072 /* --------------------------------------------------------------------- */ 1073 1074 /* 1075 * Lookup vnode for file handle specified 1076 */ 1077 int 1078 nilfs_fhtovp(struct mount *mp, struct fid *fhp, 1079 struct vnode **vpp) 1080 { 1081 DPRINTF(NOTIMPL, ("nilfs_fhtovp called\n")); 1082 return EOPNOTSUPP; 1083 } 1084 1085 /* --------------------------------------------------------------------- */ 1086 1087 /* 1088 * Create an unique file handle. Its structure is opaque and won't be used by 1089 * other subsystems. It should uniquely identify the file in the filingsystem 1090 * and enough information to know if a file has been removed and/or resources 1091 * have been recycled. 1092 */ 1093 int 1094 nilfs_vptofh(struct vnode *vp, struct fid *fid, 1095 size_t *fh_size) 1096 { 1097 DPRINTF(NOTIMPL, ("nilfs_vptofh called\n")); 1098 return EOPNOTSUPP; 1099 } 1100 1101 /* --------------------------------------------------------------------- */ 1102 1103 /* 1104 * Create a file system snapshot at the specified timestamp. 1105 */ 1106 int 1107 nilfs_snapshot(struct mount *mp, struct vnode *vp, 1108 struct timespec *tm) 1109 { 1110 DPRINTF(NOTIMPL, ("nilfs_snapshot called\n")); 1111 return EOPNOTSUPP; 1112 } 1113 1114 /* --------------------------------------------------------------------- */ 1115