27 */ 28 29/* udf_vfsops.c */ 30/* Implement the VFS side of things */ 31 32/* 33 * Ok, here's how it goes. The UDF specs are pretty clear on how each data 34 * structure is made up, but not very clear on how they relate to each other. 35 * Here is the skinny... This demostrates a filesystem with one file in the 36 * root directory. Subdirectories are treated just as normal files, but they 37 * have File Id Descriptors of their children as their file data. As for the 38 * Anchor Volume Descriptor Pointer, it can exist in two of the following three 39 * places: sector 256, sector n (the max sector of the disk), or sector 40 * n - 256. It's a pretty good bet that one will exist at sector 256 though. 41 * One caveat is unclosed CD media. For that, sector 256 cannot be written, 42 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the 43 * media is closed. 44 * 45 * Sector: 46 * 256: 47 * n: Anchor Volume Descriptor Pointer 48 * n - 256: | 49 * | 50 * |-->Main Volume Descriptor Sequence 51 * | | 52 * | | 53 * | |-->Logical Volume Descriptor 54 * | | 55 * |-->Partition Descriptor | 56 * | | 57 * | | 58 * |-->Fileset Descriptor 59 * | 60 * | 61 * |-->Root Dir File Entry 62 * | 63 * | 64 * |-->File data: 65 * File Id Descriptor 66 * | 67 * | 68 * |-->File Entry 69 * | 70 * | 71 * |-->File data 72 */ 73#include <sys/types.h> 74#include <sys/param.h> 75#include <sys/systm.h> 76#include <sys/uio.h> 77#include <sys/bio.h> 78#include <sys/buf.h> 79#include <sys/conf.h> 80#include <sys/dirent.h> 81#include <sys/fcntl.h> 82#include <sys/kernel.h> 83#include <sys/malloc.h> 84#include <sys/mount.h> 85#include <sys/namei.h> 86#include <sys/proc.h> 87#include <sys/queue.h> 88#include <sys/vnode.h> 89 90#include <vm/uma.h> 91 92#include <fs/udf/ecma167-udf.h> 93#include <fs/udf/udf.h> 94#include <fs/udf/osta.h> 95 96MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure"); 97MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure"); 98 99/* Zones */ 100uma_zone_t udf_zone_trans = NULL; 101uma_zone_t udf_zone_node = NULL; 102uma_zone_t udf_zone_ds = NULL; 103 104static vfs_init_t udf_init; 105static vfs_uninit_t udf_uninit; 106static vfs_nmount_t udf_mount; 107static vfs_root_t udf_root; 108static vfs_statfs_t udf_statfs; 109static vfs_unmount_t udf_unmount; 110static vfs_fhtovp_t udf_fhtovp; 111static vfs_vptofh_t udf_vptofh; 112 113static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *); 114 115static struct vfsops udf_vfsops = { 116 .vfs_fhtovp = udf_fhtovp, 117 .vfs_init = udf_init, 118 .vfs_nmount = udf_mount, 119 .vfs_root = udf_root, 120 .vfs_statfs = udf_statfs, 121 .vfs_uninit = udf_uninit, 122 .vfs_unmount = udf_unmount, 123 .vfs_vget = udf_vget, 124 .vfs_vptofh = udf_vptofh, 125}; 126VFS_SET(udf_vfsops, udf, VFCF_READONLY); 127 128static int udf_mountfs(struct vnode *, struct mount *, struct thread *); 129 130static int 131udf_init(struct vfsconf *foo) 132{ 133 134 /* 135 * This code used to pre-allocate a certain number of pages for each 136 * pool, reducing the need to grow the zones later on. UMA doesn't 137 * advertise any such functionality, unfortunately =-< 138 */ 139 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN * 140 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0); 141 142 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node), 143 NULL, NULL, NULL, NULL, 0, 0); 144 145 udf_zone_ds = uma_zcreate("UDF Dirstream zone", 146 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0); 147 148 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) || 149 (udf_zone_ds == NULL)) { 150 printf("Cannot create allocation zones.\n"); 151 return (ENOMEM); 152 } 153 154 return 0; 155} 156 157static int 158udf_uninit(struct vfsconf *foo) 159{ 160 161 if (udf_zone_trans != NULL) { 162 uma_zdestroy(udf_zone_trans); 163 udf_zone_trans = NULL; 164 } 165 166 if (udf_zone_node != NULL) { 167 uma_zdestroy(udf_zone_node); 168 udf_zone_node = NULL; 169 } 170 171 if (udf_zone_ds != NULL) { 172 uma_zdestroy(udf_zone_ds); 173 udf_zone_ds = NULL; 174 } 175 176 return (0); 177} 178 179static int 180udf_mount(struct mount *mp, struct nameidata *ndp, struct thread *td) 181{ 182 struct vnode *devvp; /* vnode of the mount device */ 183 struct udf_mnt *imp = 0; 184 struct export_args *export; 185 struct vfsoptlist *opts; 186 char *fspec; 187 size_t size; 188 int error, len; 189 190 opts = mp->mnt_optnew; 191 192 if ((mp->mnt_flag & MNT_RDONLY) == 0) 193 return (EROFS); 194 195 /* 196 * No root filesystem support. Probably not a big deal, since the 197 * bootloader doesn't understand UDF. 198 */ 199 if (mp->mnt_flag & MNT_ROOTFS) 200 return (ENOTSUP); 201 202 fspec = NULL; 203 error = vfs_getopt(opts, "from", (void **)&fspec, &len); 204 if (!error && fspec[len - 1] != '\0') 205 return (EINVAL); 206 207 if (mp->mnt_flag & MNT_UPDATE) { 208 imp = VFSTOUDFFS(mp); 209 if (fspec == NULL) { 210 error = vfs_getopt(opts, "export", (void **)&export, 211 &len); 212 if (error || len != sizeof(struct export_args)) 213 return (EINVAL); 214 return (vfs_export(mp, export)); 215 } 216 } 217 218 /* Check that the mount device exists */ 219 if (fspec == NULL) 220 return (EINVAL); 221 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td); 222 if ((error = namei(ndp))) 223 return (error); 224 NDFREE(ndp, NDF_ONLY_PNBUF); 225 devvp = ndp->ni_vp; 226 227 if (vn_isdisk(devvp, &error) == 0) { 228 vrele(devvp); 229 return (error); 230 } 231 232 /* Check the access rights on the mount device */ 233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 234 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td); 235 if (error) 236 error = suser(td); 237 if (error) { 238 vput(devvp); 239 return (error); 240 } 241 VOP_UNLOCK(devvp, 0, td); 242 243 if ((error = udf_mountfs(devvp, mp, td))) { 244 vrele(devvp); 245 return (error); 246 } 247 248 imp = VFSTOUDFFS(mp); 249 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 250 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 251 udf_statfs(mp, &mp->mnt_stat, td); 252 return 0; 253}; 254 255/* 256 * Check the descriptor tag for both the correct id and correct checksum. 257 * Return zero if all is good, EINVAL if not. 258 */ 259int 260udf_checktag(struct desc_tag *tag, uint16_t id) 261{ 262 uint8_t *itag; 263 uint8_t i, cksum = 0; 264 265 itag = (uint8_t *)tag; 266 267 if (tag->id != id) 268 return (EINVAL); 269 270 for (i = 0; i < 15; i++) 271 cksum = cksum + itag[i]; 272 cksum = cksum - itag[4]; 273 274 if (cksum == tag->cksum) 275 return (0); 276 277 return (EINVAL); 278} 279 280static int 281udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) { 282 struct buf *bp = NULL; 283 struct anchor_vdp avdp; 284 struct udf_mnt *udfmp = NULL; 285 struct part_desc *pd; 286 struct logvol_desc *lvd; 287 struct fileset_desc *fsd; 288 struct file_entry *root_fentry; 289 uint32_t sector, size, mvds_start, mvds_end; 290 uint32_t fsd_offset = 0; 291 uint16_t part_num = 0, fsd_part = 0; 292 int error = EINVAL, needclose = 0; 293 int logvol_found = 0, part_found = 0, fsd_found = 0; 294 int bsize; 295 296 /* 297 * Disallow multiple mounts of the same device. Flush the buffer 298 * cache for the device. 299 */ 300 if ((error = vfs_mountedon(devvp))) 301 return (error); 302 if (vcount(devvp) > 1) 303 return (EBUSY); 304 if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0))) 305 return (error); 306 307 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 308 error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1); 309 VOP_UNLOCK(devvp, 0, td); 310 if (error) 311 return error; 312 needclose = 1; 313 314 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT, 315 M_NOWAIT | M_ZERO); 316 if (udfmp == NULL) { 317 printf("Cannot allocate UDF mount struct\n"); 318 error = ENOMEM; 319 goto bail; 320 } 321 322 mp->mnt_data = (qaddr_t)udfmp; 323 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev); 324 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 325 mp->mnt_flag |= MNT_LOCAL; 326 udfmp->im_mountp = mp; 327 udfmp->im_dev = devvp->v_rdev; 328 udfmp->im_devvp = devvp; 329 330 bsize = 2048; /* XXX Should probe the media for it's size */ 331 332 /* 333 * Get the Anchor Volume Descriptor Pointer from sector 256. 334 * XXX Should also check sector n - 256, n, and 512. 335 */ 336 sector = 256; 337 if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED, 338 &bp)) != 0) 339 goto bail; 340 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR))) 341 goto bail; 342 343 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp)); 344 brelse(bp); 345 bp = NULL; 346 347 /* 348 * Extract the Partition Descriptor and Logical Volume Descriptor 349 * from the Volume Descriptor Sequence. 350 * XXX Should we care about the partition type right now? 351 * XXX What about multiple partitions? 352 */ 353 mvds_start = avdp.main_vds_ex.loc; 354 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize; 355 for (sector = mvds_start; sector < mvds_end; sector++) { 356 if ((error = bread(devvp, sector * btodb(bsize), bsize, 357 NOCRED, &bp)) != 0) { 358 printf("Can't read sector %d of VDS\n", sector); 359 goto bail; 360 } 361 lvd = (struct logvol_desc *)bp->b_data; 362 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) { 363 udfmp->bsize = lvd->lb_size; 364 udfmp->bmask = udfmp->bsize - 1; 365 udfmp->bshift = ffs(udfmp->bsize) - 1; 366 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num; 367 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num; 368 if (udf_find_partmaps(udfmp, lvd)) 369 break; 370 logvol_found = 1; 371 } 372 pd = (struct part_desc *)bp->b_data; 373 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) { 374 part_found = 1; 375 part_num = pd->part_num; 376 udfmp->part_len = pd->part_len; 377 udfmp->part_start = pd->start_loc; 378 } 379 380 brelse(bp); 381 bp = NULL; 382 if ((part_found) && (logvol_found)) 383 break; 384 } 385 386 if (!part_found || !logvol_found) { 387 error = EINVAL; 388 goto bail; 389 } 390 391 if (fsd_part != part_num) { 392 printf("FSD does not lie within the partition!\n"); 393 error = EINVAL; 394 goto bail; 395 } 396 397 398 /* 399 * Grab the Fileset Descriptor 400 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing 401 * me in the right direction here. 402 */ 403 sector = udfmp->part_start + fsd_offset; 404 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) { 405 printf("Cannot read sector %d of FSD\n", sector); 406 goto bail; 407 } 408 fsd = (struct fileset_desc *)bp->b_data; 409 if (!udf_checktag(&fsd->tag, TAGID_FSD)) { 410 fsd_found = 1; 411 bcopy(&fsd->rootdir_icb, &udfmp->root_icb, 412 sizeof(struct long_ad)); 413 } 414 415 brelse(bp); 416 bp = NULL; 417 418 if (!fsd_found) { 419 printf("Couldn't find the fsd\n"); 420 error = EINVAL; 421 goto bail; 422 } 423 424 /* 425 * Find the file entry for the root directory. 426 */ 427 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start; 428 size = udfmp->root_icb.len; 429 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) { 430 printf("Cannot read sector %d\n", sector); 431 goto bail; 432 } 433 434 root_fentry = (struct file_entry *)bp->b_data; 435 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) { 436 printf("Invalid root file entry!\n"); 437 goto bail; 438 } 439 440 brelse(bp); 441 bp = NULL; 442 443 devvp->v_rdev->si_mountpoint = mp; 444 445 mtx_init(&udfmp->hash_mtx, "udf_hash", NULL, MTX_DEF); 446 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz); 447 448 return 0; 449 450bail: 451 if (udfmp != NULL) 452 FREE(udfmp, M_UDFMOUNT); 453 if (bp != NULL) 454 brelse(bp); 455 if (needclose) 456 VOP_CLOSE(devvp, FREAD, NOCRED, td); 457 return error; 458}; 459 460static int 461udf_unmount(struct mount *mp, int mntflags, struct thread *td) 462{ 463 struct udf_mnt *udfmp; 464 int error, flags = 0; 465 466 udfmp = VFSTOUDFFS(mp); 467 468 if (mntflags & MNT_FORCE) 469 flags |= FORCECLOSE; 470 471 if ((error = vflush(mp, 0, flags))) 472 return (error); 473 474 udfmp->im_devvp->v_rdev->si_mountpoint = NULL; 475 error = VOP_CLOSE(udfmp->im_devvp, FREAD, NOCRED, td); 476 vrele(udfmp->im_devvp); 477 478 if (udfmp->s_table != NULL) 479 FREE(udfmp->s_table, M_UDFMOUNT); 480 481 if (udfmp->hashtbl != NULL) 482 FREE(udfmp->hashtbl, M_UDFMOUNT); 483 484 FREE(udfmp, M_UDFMOUNT); 485 486 mp->mnt_data = (qaddr_t)0; 487 mp->mnt_flag &= ~MNT_LOCAL; 488 489 return (0); 490} 491 492static int 493udf_root(struct mount *mp, struct vnode **vpp) 494{ 495 struct udf_mnt *udfmp; 496 struct vnode *vp; 497 ino_t id; 498 int error; 499 500 udfmp = VFSTOUDFFS(mp); 501 502 id = udf_getid(&udfmp->root_icb); 503 504 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp); 505 if (error) 506 return error; 507 508 vp = *vpp; 509 vp->v_vflag |= VV_ROOT; 510 udfmp->root_vp = vp; 511 512 return (0); 513} 514 515static int 516udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 517{ 518 struct udf_mnt *udfmp; 519 520 udfmp = VFSTOUDFFS(mp); 521 522 sbp->f_bsize = udfmp->bsize; 523 sbp->f_iosize = udfmp->bsize; 524 sbp->f_blocks = udfmp->part_len; 525 sbp->f_bfree = 0; 526 sbp->f_bavail = 0; 527 sbp->f_files = 0; 528 sbp->f_ffree = 0; 529 if (sbp != &mp->mnt_stat) { 530 sbp->f_type = mp->mnt_vfc->vfc_typenum; 531 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 532 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 533 } 534 535 return 0; 536} 537 538int 539udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 540{ 541 struct buf *bp; 542 struct vnode *devvp; 543 struct udf_mnt *udfmp; 544 struct thread *td; 545 struct vnode *vp; 546 struct udf_node *unode; 547 struct file_entry *fe; 548 int error, sector, size; 549 550 td = curthread; 551 udfmp = VFSTOUDFFS(mp); 552 553 /* See if we already have this in the cache */ 554 if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0) 555 return (error); 556 if (*vpp != NULL) { 557 return (0); 558 } 559 560 /* 561 * Allocate memory and check the tag id's before grabbing a new 562 * vnode, since it's hard to roll back if there is a problem. 563 */ 564 unode = uma_zalloc(udf_zone_node, M_WAITOK); 565 if (unode == NULL) { 566 printf("Cannot allocate udf node\n"); 567 return (ENOMEM); 568 } 569 570 /* 571 * Copy in the file entry. Per the spec, the size can only be 1 block. 572 */ 573 sector = ino + udfmp->part_start; 574 devvp = udfmp->im_devvp; 575 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) { 576 printf("Cannot read sector %d\n", sector); 577 uma_zfree(udf_zone_node, unode); 578 return (error); 579 } 580 581 fe = (struct file_entry *)bp->b_data; 582 if (udf_checktag(&fe->tag, TAGID_FENTRY)) { 583 printf("Invalid file entry!\n"); 584 uma_zfree(udf_zone_node, unode); 585 brelse(bp); 586 return (ENOMEM); 587 } 588 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad; 589 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY, 590 M_NOWAIT | M_ZERO); 591 if (unode->fentry == NULL) { 592 printf("Cannot allocate file entry block\n"); 593 uma_zfree(udf_zone_node, unode); 594 brelse(bp); 595 return (ENOMEM); 596 } 597 598 bcopy(bp->b_data, unode->fentry, size); 599 600 brelse(bp); 601 bp = NULL; 602 603 if ((error = udf_allocv(mp, &vp, td))) { 604 printf("Error from udf_allocv\n"); 605 uma_zfree(udf_zone_node, unode); 606 return (error); 607 } 608 609 unode->i_vnode = vp; 610 unode->hash_id = ino; 611 unode->i_devvp = udfmp->im_devvp; 612 unode->i_dev = udfmp->im_dev; 613 unode->udfmp = udfmp; 614 vp->v_data = unode; 615 VREF(udfmp->im_devvp); 616 udf_hashins(unode); 617 618 switch (unode->fentry->icbtag.file_type) { 619 default: 620 vp->v_type = VBAD; 621 break; 622 case 4: 623 vp->v_type = VDIR; 624 break; 625 case 5: 626 vp->v_type = VREG; 627 break; 628 case 6: 629 vp->v_type = VBLK; 630 break; 631 case 7: 632 vp->v_type = VCHR; 633 break; 634 case 9: 635 vp->v_type = VFIFO; 636 break; 637 case 10: 638 vp->v_type = VSOCK; 639 break; 640 case 12: 641 vp->v_type = VLNK; 642 break; 643 } 644 *vpp = vp; 645 646 return (0); 647} 648 649struct ifid {
|