1 /* $NetBSD: udf_vfsops.c,v 1.38 2008/05/14 16:49:48 reinoud Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008 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: udf_vfsops.c,v 1.38 2008/05/14 16:49:48 reinoud Exp $"); 32 #endif /* not lint */ 33 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_quota.h" 37 #include "opt_compat_netbsd.h" 38 #include "opt_udf.h" 39 #endif 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/sysctl.h> 44 #include <sys/namei.h> 45 #include <sys/proc.h> 46 #include <sys/kernel.h> 47 #include <sys/vnode.h> 48 #include <miscfs/genfs/genfs.h> 49 #include <miscfs/specfs/specdev.h> 50 #include <sys/mount.h> 51 #include <sys/buf.h> 52 #include <sys/file.h> 53 #include <sys/device.h> 54 #include <sys/disklabel.h> 55 #include <sys/ioctl.h> 56 #include <sys/malloc.h> 57 #include <sys/dirent.h> 58 #include <sys/stat.h> 59 #include <sys/conf.h> 60 #include <sys/kauth.h> 61 #include <sys/module.h> 62 63 #include <fs/udf/ecma167-udf.h> 64 #include <fs/udf/udf_mount.h> 65 66 #include "udf.h" 67 #include "udf_subr.h" 68 #include "udf_bswap.h" 69 70 MODULE(MODULE_CLASS_VFS, udf, NULL); 71 72 #define VTOI(vnode) ((struct udf_node *) vnode->v_data) 73 74 /* verbose levels of the udf filingsystem */ 75 int udf_verbose = UDF_DEBUGGING; 76 77 /* malloc regions */ 78 MALLOC_JUSTDEFINE(M_UDFMNT, "UDF mount", "UDF mount structures"); 79 MALLOC_JUSTDEFINE(M_UDFVOLD, "UDF volspace", "UDF volume space descriptors"); 80 MALLOC_JUSTDEFINE(M_UDFTEMP, "UDF temp", "UDF scrap space"); 81 struct pool udf_node_pool; 82 83 /* supported functions predefined */ 84 VFS_PROTOS(udf); 85 86 87 /* internal functions */ 88 static int udf_mountfs(struct vnode *, struct mount *, struct lwp *, struct udf_args *); 89 90 91 /* --------------------------------------------------------------------- */ 92 93 /* predefine vnode-op list descriptor */ 94 extern const struct vnodeopv_desc udf_vnodeop_opv_desc; 95 96 const struct vnodeopv_desc * const udf_vnodeopv_descs[] = { 97 &udf_vnodeop_opv_desc, 98 NULL, 99 }; 100 101 102 /* vfsops descriptor linked in as anchor point for the filingsystem */ 103 struct vfsops udf_vfsops = { 104 MOUNT_UDF, /* vfs_name */ 105 sizeof (struct udf_args), 106 udf_mount, 107 udf_start, 108 udf_unmount, 109 udf_root, 110 (void *)eopnotsupp, /* vfs_quotactl */ 111 udf_statvfs, 112 udf_sync, 113 udf_vget, 114 udf_fhtovp, 115 udf_vptofh, 116 udf_init, 117 udf_reinit, 118 udf_done, 119 udf_mountroot, 120 udf_snapshot, 121 vfs_stdextattrctl, 122 (void *)eopnotsupp, /* vfs_suspendctl */ 123 genfs_renamelock_enter, 124 genfs_renamelock_exit, 125 (void *)eopnotsupp, 126 udf_vnodeopv_descs, 127 0, /* int vfs_refcount */ 128 { NULL, NULL, }, /* LIST_ENTRY(vfsops) */ 129 }; 130 131 /* --------------------------------------------------------------------- */ 132 133 /* file system starts here */ 134 void 135 udf_init(void) 136 { 137 size_t size; 138 139 malloc_type_attach(M_UDFMNT); 140 malloc_type_attach(M_UDFVOLD); 141 malloc_type_attach(M_UDFTEMP); 142 143 /* init hashtables and pools */ 144 size = sizeof(struct udf_node); 145 pool_init(&udf_node_pool, size, 0, 0, 0, "udf_node_pool", NULL, 146 IPL_NONE); 147 } 148 149 150 void 151 udf_reinit(void) 152 { 153 /* recreate hashtables */ 154 /* reinit pool? */ 155 } 156 157 158 void 159 udf_done(void) 160 { 161 /* remove hashtables and pools */ 162 pool_destroy(&udf_node_pool); 163 164 malloc_type_detach(M_UDFMNT); 165 malloc_type_detach(M_UDFVOLD); 166 malloc_type_detach(M_UDFTEMP); 167 } 168 169 170 static int 171 udf_modcmd(modcmd_t cmd, void *arg) 172 { 173 174 switch (cmd) { 175 case MODULE_CMD_INIT: 176 return vfs_attach(&udf_vfsops); 177 case MODULE_CMD_FINI: 178 return vfs_detach(&udf_vfsops); 179 default: 180 return ENOTTY; 181 } 182 } 183 184 /* --------------------------------------------------------------------- */ 185 186 int 187 udf_mountroot(void) 188 { 189 return EOPNOTSUPP; 190 } 191 192 /* --------------------------------------------------------------------- */ 193 194 #define MPFREE(a, lst) \ 195 if ((a)) free((a), lst); 196 static void 197 free_udf_mountinfo(struct mount *mp) 198 { 199 struct udf_mount *ump; 200 int i; 201 202 if (!mp) 203 return; 204 205 ump = VFSTOUDF(mp); 206 if (ump) { 207 /* clear our data */ 208 for (i = 0; i < UDF_ANCHORS; i++) 209 MPFREE(ump->anchors[i], M_UDFVOLD); 210 MPFREE(ump->primary_vol, M_UDFVOLD); 211 MPFREE(ump->logical_vol, M_UDFVOLD); 212 MPFREE(ump->unallocated, M_UDFVOLD); 213 MPFREE(ump->implementation, M_UDFVOLD); 214 MPFREE(ump->logvol_integrity, M_UDFVOLD); 215 for (i = 0; i < UDF_PARTITIONS; i++) { 216 MPFREE(ump->partitions[i], M_UDFVOLD); 217 MPFREE(ump->part_unalloc_dscr[i], M_UDFVOLD); 218 MPFREE(ump->part_freed_dscr[i], M_UDFVOLD); 219 } 220 MPFREE(ump->fileset_desc, M_UDFVOLD); 221 MPFREE(ump->sparing_table, M_UDFVOLD); 222 MPFREE(ump->metadata_bitmap.bits, M_UDFVOLD); 223 224 MPFREE(ump->la_node_ad_cpy, M_UDFMNT); 225 MPFREE(ump->la_pmapping, M_TEMP); 226 MPFREE(ump->la_lmapping, M_TEMP); 227 228 mutex_destroy(&ump->ihash_lock); 229 mutex_destroy(&ump->get_node_lock); 230 231 mutex_destroy(&ump->allocate_mutex); 232 MPFREE(ump->vat_table, M_UDFVOLD); 233 234 free(ump, M_UDFMNT); 235 } 236 } 237 #undef MPFREE 238 239 /* --------------------------------------------------------------------- */ 240 241 /* if the system nodes exist, release them */ 242 static void 243 udf_release_system_nodes(struct mount *mp) 244 { 245 struct udf_mount *ump = VFSTOUDF(mp); 246 int error; 247 248 /* if we haven't even got an ump, dont bother */ 249 if (!ump) 250 return; 251 252 /* VAT partition support */ 253 if (ump->vat_node) 254 vrele(ump->vat_node->vnode); 255 256 /* Metadata partition support */ 257 if (ump->metadata_node) 258 vrele(ump->metadata_node->vnode); 259 if (ump->metadatamirror_node) 260 vrele(ump->metadatamirror_node->vnode); 261 if (ump->metadatabitmap_node) 262 vrele(ump->metadatabitmap_node->vnode); 263 264 /* This flush should NOT write anything nor allow any node to remain */ 265 if ((error = vflush(ump->vfs_mountp, NULLVP, 0)) != 0) 266 panic("Failure to flush UDF system vnodes\n"); 267 } 268 269 270 int 271 udf_mount(struct mount *mp, const char *path, 272 void *data, size_t *data_len) 273 { 274 struct lwp *l = curlwp; 275 struct nameidata nd; 276 struct udf_args *args = data; 277 struct udf_mount *ump; 278 struct vnode *devvp; 279 int openflags, accessmode, error; 280 281 DPRINTF(CALL, ("udf_mount called\n")); 282 283 if (*data_len < sizeof *args) 284 return EINVAL; 285 286 if (mp->mnt_flag & MNT_GETARGS) { 287 /* request for the mount arguments */ 288 ump = VFSTOUDF(mp); 289 if (ump == NULL) 290 return EINVAL; 291 *args = ump->mount_args; 292 *data_len = sizeof *args; 293 return 0; 294 } 295 296 /* handle request for updating mount parameters */ 297 /* TODO can't update my mountpoint yet */ 298 if (mp->mnt_flag & MNT_UPDATE) { 299 return EOPNOTSUPP; 300 } 301 302 /* OK, so we are asked to mount the device */ 303 304 /* check/translate struct version */ 305 /* TODO sanity checking other mount arguments */ 306 if (args->version != 1) { 307 printf("mount_udf: unrecognized argument structure version\n"); 308 return EINVAL; 309 } 310 311 /* lookup name to get its vnode */ 312 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec); 313 error = namei(&nd); 314 if (error) 315 return error; 316 devvp = nd.ni_vp; 317 318 #ifdef DEBUG 319 if (udf_verbose & UDF_DEBUG_VOLUMES) 320 vprint("UDF mount, trying to mount \n", devvp); 321 #endif 322 323 /* check if its a block device specified */ 324 if (devvp->v_type != VBLK) { 325 vrele(devvp); 326 return ENOTBLK; 327 } 328 if (bdevsw_lookup(devvp->v_rdev) == NULL) { 329 vrele(devvp); 330 return ENXIO; 331 } 332 333 #ifndef UDF_READWRITE 334 /* force read-only for now */ 335 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 336 printf("Enable kernel option UDF_READWRITE for writing\n"); 337 vrele(devvp); 338 return EROFS; 339 } 340 #endif 341 342 /* 343 * If mount by non-root, then verify that user has necessary 344 * permissions on the device. 345 */ 346 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) { 347 accessmode = VREAD; 348 if ((mp->mnt_flag & MNT_RDONLY) == 0) 349 accessmode |= VWRITE; 350 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 351 error = VOP_ACCESS(devvp, accessmode, l->l_cred); 352 VOP_UNLOCK(devvp, 0); 353 if (error) { 354 vrele(devvp); 355 return error; 356 } 357 } 358 359 /* 360 * Open device and try to mount it! 361 */ 362 if (mp->mnt_flag & MNT_RDONLY) { 363 openflags = FREAD; 364 } else { 365 openflags = FREAD | FWRITE; 366 } 367 error = VOP_OPEN(devvp, openflags, FSCRED); 368 if (error == 0) { 369 /* opened ok, try mounting */ 370 error = udf_mountfs(devvp, mp, l, args); 371 if (error) { 372 udf_release_system_nodes(mp); 373 /* cleanup */ 374 udf_discstrat_finish(VFSTOUDF(mp)); 375 free_udf_mountinfo(mp); 376 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 377 (void) VOP_CLOSE(devvp, openflags, NOCRED); 378 VOP_UNLOCK(devvp, 0); 379 } 380 } 381 if (error) { 382 /* devvp is still locked */ 383 vrele(devvp); 384 return error; 385 } 386 387 /* register our mountpoint being on this device */ 388 devvp->v_specmountpoint = mp; 389 390 /* successfully mounted */ 391 DPRINTF(VOLUMES, ("udf_mount() successfull\n")); 392 393 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 394 mp->mnt_op->vfs_name, mp, l); 395 if (error) 396 return error; 397 398 /* If we're not opened read-only, open its logical volume */ 399 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 400 if ((error = udf_open_logvol(VFSTOUDF(mp))) != 0) { 401 printf( "mount_udf: can't open logical volume for " 402 "writing,downgrading access to read-only\n"); 403 mp->mnt_flag |= MNT_RDONLY; 404 /* FIXME we can't return error now on open failure */ 405 return 0; 406 } 407 } 408 409 return 0; 410 } 411 412 /* --------------------------------------------------------------------- */ 413 414 #ifdef DEBUG 415 static void 416 udf_unmount_sanity_check(struct mount *mp) 417 { 418 struct vnode *vp; 419 420 printf("On unmount, i found the following nodes:\n"); 421 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 422 vprint("", vp); 423 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) { 424 printf(" is locked\n"); 425 } 426 if (vp->v_usecount > 1) 427 printf(" more than one usecount %d\n", vp->v_usecount); 428 } 429 } 430 #endif 431 432 433 int 434 udf_unmount(struct mount *mp, int mntflags) 435 { 436 struct udf_mount *ump; 437 int error, flags, closeflags; 438 439 DPRINTF(CALL, ("udf_umount called\n")); 440 441 ump = VFSTOUDF(mp); 442 if (!ump) 443 panic("UDF unmount: empty ump\n"); 444 445 flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0; 446 /* TODO remove these paranoid functions */ 447 #ifdef DEBUG 448 if (udf_verbose & UDF_DEBUG_LOCKING) 449 udf_unmount_sanity_check(mp); 450 #endif 451 452 /* 453 * By specifying SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM. 454 * This hardly documented feature allows us to exempt certain files 455 * from being flushed. 456 */ 457 if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0) 458 return error; 459 460 /* update nodes and wait for completion of writeout of system nodes */ 461 udf_sync(mp, FSYNC_WAIT, NOCRED); 462 463 #ifdef DEBUG 464 if (udf_verbose & UDF_DEBUG_LOCKING) 465 udf_unmount_sanity_check(mp); 466 #endif 467 468 /* flush again, to check if we are still busy for something else */ 469 if ((error = vflush(ump->vfs_mountp, NULLVP, flags | SKIPSYSTEM)) != 0) 470 return error; 471 472 DPRINTF(VOLUMES, ("flush OK on unmount\n")); 473 474 /* close logical volume and close session if requested */ 475 if ((error = udf_close_logvol(ump, mntflags)) != 0) 476 return error; 477 478 #ifdef DEBUG 479 DPRINTF(VOLUMES, ("FINAL sanity check\n")); 480 if (udf_verbose & UDF_DEBUG_LOCKING) 481 udf_unmount_sanity_check(mp); 482 #endif 483 484 /* NOTE release system nodes should NOT write anything */ 485 udf_release_system_nodes(mp); 486 487 /* finalise disc strategy */ 488 udf_discstrat_finish(ump); 489 490 /* synchronise device caches */ 491 (void) udf_synchronise_caches(ump); 492 493 /* close device */ 494 DPRINTF(VOLUMES, ("closing device\n")); 495 if (mp->mnt_flag & MNT_RDONLY) { 496 closeflags = FREAD; 497 } else { 498 closeflags = FREAD | FWRITE; 499 } 500 501 /* devvp is still locked by us */ 502 vn_lock(ump->devvp, LK_EXCLUSIVE | LK_RETRY); 503 error = VOP_CLOSE(ump->devvp, closeflags, NOCRED); 504 if (error) 505 printf("Error during closure of device! error %d, " 506 "device might stay locked\n", error); 507 DPRINTF(VOLUMES, ("device close ok\n")); 508 509 /* clear our mount reference and release device node */ 510 ump->devvp->v_specmountpoint = NULL; 511 vput(ump->devvp); 512 513 /* free our ump */ 514 free_udf_mountinfo(mp); 515 516 /* free ump struct references */ 517 mp->mnt_data = NULL; 518 mp->mnt_flag &= ~MNT_LOCAL; 519 520 DPRINTF(VOLUMES, ("Fin unmount\n")); 521 return error; 522 } 523 524 /* --------------------------------------------------------------------- */ 525 526 /* 527 * Helper function of udf_mount() that actually mounts the disc. 528 */ 529 530 static int 531 udf_mountfs(struct vnode *devvp, struct mount *mp, 532 struct lwp *l, struct udf_args *args) 533 { 534 struct udf_mount *ump; 535 uint32_t sector_size, lb_size, bshift; 536 uint32_t logvol_integrity; 537 int num_anchors, error, lst; 538 539 /* flush out any old buffers remaining from a previous use. */ 540 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0))) 541 return error; 542 543 /* setup basic mount information */ 544 mp->mnt_data = NULL; 545 mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev; 546 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_UDF); 547 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 548 mp->mnt_stat.f_namemax = UDF_MAX_NAMELEN; 549 mp->mnt_flag |= MNT_LOCAL; 550 551 /* allocate udf part of mount structure; malloc always succeeds */ 552 ump = malloc(sizeof(struct udf_mount), M_UDFMNT, M_WAITOK | M_ZERO); 553 554 /* init locks */ 555 mutex_init(&ump->logvol_mutex, MUTEX_DEFAULT, IPL_NONE); 556 mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE); 557 mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE); 558 mutex_init(&ump->allocate_mutex, MUTEX_DEFAULT, IPL_NONE); 559 cv_init(&ump->dirtynodes_cv, "udfsync2"); 560 561 /* init `ino_t' to udf_node hash table and other lists */ 562 for (lst = 0; lst < UDF_INODE_HASHSIZE; lst++) { 563 LIST_INIT(&ump->udf_nodes[lst]); 564 } 565 566 /* set up linkage */ 567 mp->mnt_data = ump; 568 ump->vfs_mountp = mp; 569 570 /* set up arguments and device */ 571 ump->mount_args = *args; 572 ump->devvp = devvp; 573 if ((error = udf_update_discinfo(ump))) { 574 printf("UDF mount: error inspecting fs node\n"); 575 return error; 576 } 577 578 /* inspect sector size */ 579 sector_size = ump->discinfo.sector_size; 580 bshift = 1; 581 while ((1 << bshift) < sector_size) 582 bshift++; 583 if ((1 << bshift) != sector_size) { 584 printf("UDF mount: " 585 "hit NetBSD implementation fence on sector size\n"); 586 return EIO; 587 } 588 589 /* 590 * Inspect if we're asked to mount read-write on a non recordable or 591 * closed sequential disc. 592 */ 593 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 594 if ((ump->discinfo.mmc_cur & MMC_CAP_RECORDABLE) == 0) { 595 printf("UDF mount: disc is not recordable\n"); 596 return EROFS; 597 } 598 /* 599 * TODO if on sequential media and last session is closed, 600 * check for enough space to open/close new session 601 */ 602 } 603 604 /* initialise bootstrap disc strategy */ 605 ump->strategy = &udf_strat_bootstrap; 606 udf_discstrat_init(ump); 607 608 /* read all anchors to get volume descriptor sequence */ 609 num_anchors = udf_read_anchors(ump); 610 if (num_anchors == 0) 611 return EINVAL; 612 613 DPRINTF(VOLUMES, ("Read %d anchors on this disc, session %d\n", 614 num_anchors, args->sessionnr)); 615 616 /* read in volume descriptor sequence */ 617 if ((error = udf_read_vds_space(ump))) { 618 printf("UDF mount: error reading volume space\n"); 619 return error; 620 } 621 622 /* close down (direct) disc strategy */ 623 udf_discstrat_finish(ump); 624 625 /* check consistency and completeness */ 626 if ((error = udf_process_vds(ump))) { 627 printf( "UDF mount: disc not properly formatted" 628 "(bad VDS)\n"); 629 return error; 630 } 631 632 /* switch to new disc strategy */ 633 KASSERT(ump->strategy != &udf_strat_bootstrap); 634 udf_discstrat_init(ump); 635 636 /* initialise late allocation administration space */ 637 ump->la_lmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS, 638 M_TEMP, M_WAITOK); 639 ump->la_pmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS, 640 M_TEMP, M_WAITOK); 641 642 /* setup node cleanup extents copy space */ 643 lb_size = udf_rw32(ump->logical_vol->lb_size); 644 ump->la_node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS, 645 M_UDFMNT, M_WAITOK); 646 memset(ump->la_node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS); 647 648 /* setup rest of mount information */ 649 mp->mnt_data = ump; 650 651 /* bshift is allways equal to disc sector size */ 652 mp->mnt_dev_bshift = bshift; 653 mp->mnt_fs_bshift = bshift; 654 655 /* note that the mp info needs to be initialised for reading! */ 656 /* read vds support tables like VAT, sparable etc. */ 657 if ((error = udf_read_vds_tables(ump))) { 658 printf( "UDF mount: error in format or damaged disc " 659 "(VDS tables failing)\n"); 660 return error; 661 } 662 663 /* check if volume integrity is closed otherwise its dirty */ 664 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type); 665 if (logvol_integrity != UDF_INTEGRITY_CLOSED) { 666 printf("UDF mount: file system is not clean; "); 667 printf("please fsck(8)\n"); 668 return EPERM; 669 } 670 671 /* read root directory */ 672 if ((error = udf_read_rootdirs(ump))) { 673 printf( "UDF mount: " 674 "disc not properly formatted or damaged disc " 675 "(rootdirs failing)\n"); 676 return error; 677 } 678 679 /* do we have to set this? */ 680 devvp->v_specmountpoint = mp; 681 682 /* success! */ 683 return 0; 684 } 685 686 /* --------------------------------------------------------------------- */ 687 688 int 689 udf_start(struct mount *mp, int flags) 690 { 691 /* do we have to do something here? */ 692 return 0; 693 } 694 695 /* --------------------------------------------------------------------- */ 696 697 int 698 udf_root(struct mount *mp, struct vnode **vpp) 699 { 700 struct vnode *vp; 701 struct long_ad *dir_loc; 702 struct udf_mount *ump = VFSTOUDF(mp); 703 struct udf_node *root_dir; 704 int error; 705 706 DPRINTF(CALL, ("udf_root called\n")); 707 708 dir_loc = &ump->fileset_desc->rootdir_icb; 709 error = udf_get_node(ump, dir_loc, &root_dir); 710 711 if (!root_dir) 712 error = ENOENT; 713 if (error) 714 return error; 715 716 vp = root_dir->vnode; 717 root_dir->vnode->v_vflag |= VV_ROOT; 718 719 *vpp = vp; 720 return 0; 721 } 722 723 /* --------------------------------------------------------------------- */ 724 725 int 726 udf_statvfs(struct mount *mp, struct statvfs *sbp) 727 { 728 struct udf_mount *ump = VFSTOUDF(mp); 729 struct logvol_int_desc *lvid; 730 struct udf_logvol_info *impl; 731 uint64_t freeblks, sizeblks; 732 uint32_t *pos1, *pos2; 733 int part, num_part; 734 735 DPRINTF(CALL, ("udf_statvfs called\n")); 736 sbp->f_flag = mp->mnt_flag; 737 sbp->f_bsize = ump->discinfo.sector_size; 738 sbp->f_frsize = ump->discinfo.sector_size; 739 sbp->f_iosize = ump->discinfo.sector_size; 740 741 mutex_enter(&ump->allocate_mutex); 742 lvid = ump->logvol_integrity; 743 freeblks = sizeblks = 0; 744 745 /* Sequentials report free space directly (CD/DVD/BD-R) */ 746 KASSERT(lvid); 747 num_part = udf_rw32(lvid->num_part); 748 impl = (struct udf_logvol_info *) (lvid->tables + 2*num_part); 749 750 if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) { 751 /* XXX assumption at most two tracks open */ 752 freeblks = ump->data_track.free_blocks; 753 if (ump->data_track.tracknr != ump->metadata_track.tracknr) 754 freeblks += ump->metadata_track.free_blocks; 755 sizeblks = ump->discinfo.last_possible_lba; 756 } else { 757 /* free and used space for mountpoint based on logvol integrity */ 758 for (part=0; part < num_part; part++) { 759 pos1 = &lvid->tables[0] + part; 760 pos2 = &lvid->tables[0] + num_part + part; 761 if (udf_rw32(*pos1) != (uint32_t) -1) { 762 freeblks += udf_rw32(*pos1); 763 sizeblks += udf_rw32(*pos2); 764 } 765 } 766 } 767 freeblks -= ump->uncomitted_lb; 768 769 sbp->f_blocks = sizeblks; 770 sbp->f_bfree = freeblks; 771 sbp->f_files = 0; 772 if (impl) { 773 sbp->f_files = udf_rw32(impl->num_files); 774 sbp->f_files += udf_rw32(impl->num_directories); 775 } 776 777 /* XXX read only for now XXX */ 778 sbp->f_bavail = 0; 779 sbp->f_bresvd = 0; 780 781 /* tricky, next only aplies to ffs i think, so set to zero */ 782 sbp->f_ffree = 0; 783 sbp->f_favail = 0; 784 sbp->f_fresvd = 0; 785 786 mutex_exit(&ump->allocate_mutex); 787 788 copy_statvfs_info(sbp, mp); 789 return 0; 790 } 791 792 /* --------------------------------------------------------------------- */ 793 794 /* 795 * TODO what about writing out free space maps, lvid etc? only on `waitfor' 796 * i.e. explicit syncing by the user? 797 */ 798 799 int 800 udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 801 { 802 struct udf_mount *ump = VFSTOUDF(mp); 803 int error; 804 805 DPRINTF(CALL, ("udf_sync called\n")); 806 /* if called when mounted readonly, just ignore */ 807 if (mp->mnt_flag & MNT_RDONLY) 808 return 0; 809 810 if (ump->syncing && !waitfor) { 811 printf("UDF: skipping autosync\n"); 812 return 0; 813 } 814 815 /* get sync lock */ 816 ump->syncing = 1; 817 818 udf_do_sync(ump, cred, waitfor); 819 820 if (waitfor == MNT_WAIT) { 821 /* XXX lock for VAT en bitmaps? */ 822 /* metadata nodes are written synchronous */ 823 DPRINTF(CALL, ("udf_sync: syncing metadata\n")); 824 if (ump->lvclose & UDF_WRITE_VAT) 825 udf_writeout_vat(ump); 826 if (ump->lvclose & UDF_WRITE_PART_BITMAPS) { 827 error = udf_write_partition_spacetables(ump, waitfor); 828 if (error) { 829 printf( "udf_close_logvol: writeout of space " 830 "tables failed\n"); 831 } 832 ump->lvclose &= ~UDF_WRITE_PART_BITMAPS; 833 } 834 835 } 836 DPRINTF(CALL, ("end of udf_sync()\n")); 837 ump->syncing = 0; 838 839 return 0; 840 } 841 842 /* --------------------------------------------------------------------- */ 843 844 /* 845 * Get vnode for the file system type specific file id ino for the fs. Its 846 * used for reference to files by unique ID and for NFSv3. 847 * (optional) TODO lookup why some sources state NFSv3 848 */ 849 int 850 udf_vget(struct mount *mp, ino_t ino, 851 struct vnode **vpp) 852 { 853 DPRINTF(NOTIMPL, ("udf_vget called\n")); 854 return EOPNOTSUPP; 855 } 856 857 /* --------------------------------------------------------------------- */ 858 859 /* 860 * Lookup vnode for file handle specified 861 */ 862 int 863 udf_fhtovp(struct mount *mp, struct fid *fhp, 864 struct vnode **vpp) 865 { 866 DPRINTF(NOTIMPL, ("udf_fhtovp called\n")); 867 return EOPNOTSUPP; 868 } 869 870 /* --------------------------------------------------------------------- */ 871 872 /* 873 * Create an unique file handle. Its structure is opaque and won't be used by 874 * other subsystems. It should uniquely identify the file in the filingsystem 875 * and enough information to know if a file has been removed and/or resources 876 * have been recycled. 877 */ 878 int 879 udf_vptofh(struct vnode *vp, struct fid *fid, 880 size_t *fh_size) 881 { 882 DPRINTF(NOTIMPL, ("udf_vptofh called\n")); 883 return EOPNOTSUPP; 884 } 885 886 /* --------------------------------------------------------------------- */ 887 888 /* 889 * Create a filingsystem snapshot at the specified timestamp. Could be 890 * implemented by explicitly creating a new session or with spare room in the 891 * integrity descriptor space 892 */ 893 int 894 udf_snapshot(struct mount *mp, struct vnode *vp, 895 struct timespec *tm) 896 { 897 DPRINTF(NOTIMPL, ("udf_snapshot called\n")); 898 return EOPNOTSUPP; 899 } 900 901 /* --------------------------------------------------------------------- */ 902 903 /* 904 * If running a DEBUG kernel, provide an easy way to set the debug flags when 905 * running into a problem. 906 */ 907 908 #ifdef DEBUG 909 #define UDF_VERBOSE_SYSCTLOPT 1 910 911 SYSCTL_SETUP(sysctl_vfs_udf_setup, "sysctl vfs.udf subtree setup") 912 { 913 /* 914 * XXX the "24" below could be dynamic, thereby eliminating one 915 * more instance of the "number to vfs" mapping problem, but 916 * "24" is the order as taken from sys/mount.h 917 */ 918 919 sysctl_createv(clog, 0, NULL, NULL, 920 CTLFLAG_PERMANENT, 921 CTLTYPE_NODE, "vfs", NULL, 922 NULL, 0, NULL, 0, 923 CTL_VFS, CTL_EOL); 924 sysctl_createv(clog, 0, NULL, NULL, 925 CTLFLAG_PERMANENT, 926 CTLTYPE_NODE, "udf", 927 SYSCTL_DESCR("OSTA Universal File System"), 928 NULL, 0, NULL, 0, 929 CTL_VFS, 24, CTL_EOL); 930 931 sysctl_createv(clog, 0, NULL, NULL, 932 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 933 CTLTYPE_INT, "verbose", 934 SYSCTL_DESCR("Bitmask for filesystem debugging"), 935 NULL, 0, &udf_verbose, 0, 936 CTL_VFS, 24, UDF_VERBOSE_SYSCTLOPT, CTL_EOL); 937 } 938 939 #endif /* DEBUG */ 940 941