2 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* Implement the VFS side of things */
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
47 * n: Anchor Volume Descriptor Pointer
50 * |-->Main Volume Descriptor Sequence
53 * | |-->Logical Volume Descriptor
55 * |-->Partition Descriptor |
58 * |-->Fileset Descriptor
61 * |-->Root Dir File Entry
73 #include <sys/types.h>
74 #include <sys/param.h>
75 #include <sys/systm.h>
80 #include <sys/dirent.h>
81 #include <sys/fcntl.h>
82 #include <sys/iconv.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mount.h>
86 #include <sys/namei.h>
89 #include <sys/queue.h>
90 #include <sys/vnode.h>
91 #include <sys/endian.h>
93 #include <geom/geom.h>
94 #include <geom/geom_vfs.h>
98 #include <fs/udf/ecma167-udf.h>
99 #include <fs/udf/osta.h>
100 #include <fs/udf/udf.h>
101 #include <fs/udf/udf_mount.h>
103 static MALLOC_DEFINE(M_UDFMOUNT, "udf_mount", "UDF mount structure");
104 MALLOC_DEFINE(M_UDFFENTRY, "udf_fentry", "UDF file entry structure");
106 struct iconv_functions *udf_iconv = NULL;
109 uma_zone_t udf_zone_trans = NULL;
110 uma_zone_t udf_zone_node = NULL;
111 uma_zone_t udf_zone_ds = NULL;
113 static vfs_init_t udf_init;
114 static vfs_uninit_t udf_uninit;
115 static vfs_mount_t udf_mount;
116 static vfs_root_t udf_root;
117 static vfs_statfs_t udf_statfs;
118 static vfs_unmount_t udf_unmount;
119 static vfs_fhtovp_t udf_fhtovp;
121 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
123 static struct vfsops udf_vfsops = {
124 .vfs_fhtovp = udf_fhtovp,
125 .vfs_init = udf_init,
126 .vfs_mount = udf_mount,
127 .vfs_root = udf_root,
128 .vfs_statfs = udf_statfs,
129 .vfs_uninit = udf_uninit,
130 .vfs_unmount = udf_unmount,
131 .vfs_vget = udf_vget,
133 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
135 MODULE_VERSION(udf, 1);
137 static int udf_mountfs(struct vnode *, struct mount *);
140 udf_init(struct vfsconf *foo)
144 * This code used to pre-allocate a certain number of pages for each
145 * pool, reducing the need to grow the zones later on. UMA doesn't
146 * advertise any such functionality, unfortunately =-<
148 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
149 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
151 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
152 NULL, NULL, NULL, NULL, 0, 0);
154 udf_zone_ds = uma_zcreate("UDF Dirstream zone",
155 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
157 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
158 (udf_zone_ds == NULL)) {
159 printf("Cannot create allocation zones.\n");
167 udf_uninit(struct vfsconf *foo)
170 if (udf_zone_trans != NULL) {
171 uma_zdestroy(udf_zone_trans);
172 udf_zone_trans = NULL;
175 if (udf_zone_node != NULL) {
176 uma_zdestroy(udf_zone_node);
177 udf_zone_node = NULL;
180 if (udf_zone_ds != NULL) {
181 uma_zdestroy(udf_zone_ds);
189 udf_mount(struct mount *mp, struct thread *td)
191 struct vnode *devvp; /* vnode of the mount device */
192 struct udf_mnt *imp = 0;
193 struct vfsoptlist *opts;
194 char *fspec, *cs_disk, *cs_local;
195 int error, len, *udf_flags;
196 struct nameidata nd, *ndp = &nd;
198 opts = mp->mnt_optnew;
201 * Unconditionally mount as read-only.
204 mp->mnt_flag |= MNT_RDONLY;
208 * No root filesystem support. Probably not a big deal, since the
209 * bootloader doesn't understand UDF.
211 if (mp->mnt_flag & MNT_ROOTFS)
215 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
216 if (!error && fspec[len - 1] != '\0')
219 if (mp->mnt_flag & MNT_UPDATE) {
223 /* Check that the mount device exists */
226 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
227 if ((error = namei(ndp)))
229 NDFREE(ndp, NDF_ONLY_PNBUF);
232 if (vn_isdisk(devvp, &error) == 0) {
237 /* Check the access rights on the mount device */
238 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
240 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
246 if ((error = udf_mountfs(devvp, mp))) {
251 imp = VFSTOUDFFS(mp);
254 error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
255 if (error || len != sizeof(int))
257 imp->im_flags = *udf_flags;
259 if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
261 error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
262 if (!error && cs_disk[len - 1] != '\0')
265 error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
266 if (!error && cs_local[len - 1] != '\0')
268 udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
270 udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
274 vfs_mountedfrom(mp, fspec);
279 * Check the descriptor tag for both the correct id and correct checksum.
280 * Return zero if all is good, EINVAL if not.
283 udf_checktag(struct desc_tag *tag, uint16_t id)
286 uint8_t i, cksum = 0;
288 itag = (uint8_t *)tag;
290 if (le16toh(tag->id) != id)
293 for (i = 0; i < 16; i++)
294 cksum = cksum + itag[i];
295 cksum = cksum - itag[4];
297 if (cksum == tag->cksum)
304 udf_mountfs(struct vnode *devvp, struct mount *mp) {
305 struct buf *bp = NULL;
306 struct anchor_vdp avdp;
307 struct udf_mnt *udfmp = NULL;
308 struct part_desc *pd;
309 struct logvol_desc *lvd;
310 struct fileset_desc *fsd;
311 struct file_entry *root_fentry;
312 uint32_t sector, size, mvds_start, mvds_end;
313 uint32_t logical_secsize;
314 uint32_t fsd_offset = 0;
315 uint16_t part_num = 0, fsd_part = 0;
317 int logvol_found = 0, part_found = 0, fsd_found = 0;
319 struct g_consumer *cp;
324 error = g_vfs_open(devvp, &cp, "udf", 0);
327 VOP_UNLOCK(devvp, 0);
331 bo = &devvp->v_bufobj;
333 /* XXX: should be M_WAITOK */
334 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
337 printf("Cannot allocate UDF mount struct\n");
342 mp->mnt_data = udfmp;
343 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
344 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
346 mp->mnt_flag |= MNT_LOCAL;
348 udfmp->im_mountp = mp;
349 udfmp->im_dev = devvp->v_rdev;
350 udfmp->im_devvp = devvp;
351 udfmp->im_d2l = NULL;
356 udfmp->im_l2d = NULL;
359 * The UDF specification defines a logical sectorsize of 2048
362 logical_secsize = 2048;
364 if (((logical_secsize % cp->provider->sectorsize) != 0) ||
365 (logical_secsize < cp->provider->sectorsize)) {
374 bsize = cp->provider->sectorsize;
377 * Get the Anchor Volume Descriptor Pointer from sector 256.
378 * XXX Should also check sector n - 256, n, and 512.
381 if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
384 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
387 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
392 * Extract the Partition Descriptor and Logical Volume Descriptor
393 * from the Volume Descriptor Sequence.
394 * XXX Should we care about the partition type right now?
395 * XXX What about multiple partitions?
397 mvds_start = le32toh(avdp.main_vds_ex.loc);
398 mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
399 for (sector = mvds_start; sector < mvds_end; sector++) {
400 if ((error = bread(devvp, sector * btodb(logical_secsize),
401 bsize, NOCRED, &bp)) != 0) {
402 printf("Can't read sector %d of VDS\n", sector);
405 lvd = (struct logvol_desc *)bp->b_data;
406 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
407 udfmp->bsize = le32toh(lvd->lb_size);
408 udfmp->bmask = udfmp->bsize - 1;
409 udfmp->bshift = ffs(udfmp->bsize) - 1;
410 fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
411 fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
412 if (udf_find_partmaps(udfmp, lvd))
416 pd = (struct part_desc *)bp->b_data;
417 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
419 part_num = le16toh(pd->part_num);
420 udfmp->part_len = le32toh(pd->part_len);
421 udfmp->part_start = le32toh(pd->start_loc);
426 if ((part_found) && (logvol_found))
430 if (!part_found || !logvol_found) {
435 if (fsd_part != part_num) {
436 printf("FSD does not lie within the partition!\n");
443 * Grab the Fileset Descriptor
444 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
445 * me in the right direction here.
447 sector = udfmp->part_start + fsd_offset;
448 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
449 printf("Cannot read sector %d of FSD\n", sector);
452 fsd = (struct fileset_desc *)bp->b_data;
453 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
455 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
456 sizeof(struct long_ad));
463 printf("Couldn't find the fsd\n");
469 * Find the file entry for the root directory.
471 sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
472 size = le32toh(udfmp->root_icb.len);
473 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
474 printf("Cannot read sector %d\n", sector);
478 root_fentry = (struct file_entry *)bp->b_data;
479 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
480 printf("Invalid root file entry!\n");
491 FREE(udfmp, M_UDFMOUNT);
503 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
505 struct udf_mnt *udfmp;
506 int error, flags = 0;
508 udfmp = VFSTOUDFFS(mp);
510 if (mntflags & MNT_FORCE)
513 if ((error = vflush(mp, 0, flags, td)))
516 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
518 udf_iconv->close(udfmp->im_d2l);
521 udf_iconv->close(udfmp->im_l2d);
527 g_vfs_close(udfmp->im_cp);
530 vrele(udfmp->im_devvp);
532 if (udfmp->s_table != NULL)
533 FREE(udfmp->s_table, M_UDFMOUNT);
535 FREE(udfmp, M_UDFMOUNT);
539 mp->mnt_flag &= ~MNT_LOCAL;
546 udf_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
548 struct udf_mnt *udfmp;
553 udfmp = VFSTOUDFFS(mp);
555 id = udf_getid(&udfmp->root_icb);
557 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
562 vp->v_vflag |= VV_ROOT;
568 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
570 struct udf_mnt *udfmp;
572 udfmp = VFSTOUDFFS(mp);
574 sbp->f_bsize = udfmp->bsize;
575 sbp->f_iosize = udfmp->bsize;
576 sbp->f_blocks = udfmp->part_len;
585 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
589 struct udf_mnt *udfmp;
592 struct udf_node *unode;
593 struct file_entry *fe;
594 int error, sector, size;
596 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
597 if (error || *vpp != NULL)
601 udfmp = VFSTOUDFFS(mp);
603 unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
605 if ((error = udf_allocv(mp, &vp, td))) {
606 printf("Error from udf_allocv\n");
607 uma_zfree(udf_zone_node, unode);
612 unode->hash_id = ino;
613 unode->udfmp = udfmp;
616 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
617 error = insmntque(vp, mp);
619 uma_zfree(udf_zone_node, unode);
622 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
623 if (error || *vpp != NULL)
627 * Copy in the file entry. Per the spec, the size can only be 1 block.
629 sector = ino + udfmp->part_start;
630 devvp = udfmp->im_devvp;
631 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
632 printf("Cannot read sector %d\n", sector);
640 fe = (struct file_entry *)bp->b_data;
641 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
642 printf("Invalid file entry!\n");
649 size = UDF_FENTRY_SIZE + le32toh(fe->l_ea) + le32toh(fe->l_ad);
650 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
652 if (unode->fentry == NULL) {
653 printf("Cannot allocate file entry block\n");
661 bcopy(bp->b_data, unode->fentry, size);
666 switch (unode->fentry->icbtag.file_type) {
698 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
706 ifhp = (struct ifid *)fhp;
708 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
714 fsize = le64toh(np->fentry->inf_len);
717 vnode_create_vobject(*vpp, fsize, curthread);
722 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
724 struct part_map_spare *pms;
725 struct regid *pmap_id;
727 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
728 int i, k, ptype, psize, error;
729 uint8_t *pmap = (uint8_t *) &lvd->maps[0];
731 for (i = 0; i < le32toh(lvd->n_pm); i++) {
734 if (((ptype != 1) && (ptype != 2)) ||
735 ((psize != UDF_PMAP_TYPE1_SIZE) &&
736 (psize != UDF_PMAP_TYPE2_SIZE))) {
737 printf("Invalid partition map found\n");
742 /* Type 1 map. We don't care */
743 pmap += UDF_PMAP_TYPE1_SIZE;
747 /* Type 2 map. Gotta find out the details */
748 pmap_id = (struct regid *)&pmap[4];
749 bzero(®id_id[0], UDF_REGID_ID_SIZE);
750 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE);
752 if (bcmp(®id_id[0], "*UDF Sparable Partition",
753 UDF_REGID_ID_SIZE)) {
754 printf("Unsupported partition map: %s\n", ®id_id[0]);
758 pms = (struct part_map_spare *)pmap;
759 pmap += UDF_PMAP_TYPE2_SIZE;
760 MALLOC(udfmp->s_table, struct udf_sparing_table *,
761 le32toh(pms->st_size), M_UDFMOUNT, M_NOWAIT | M_ZERO);
762 if (udfmp->s_table == NULL)
765 /* Calculate the number of sectors per packet. */
766 /* XXX Logical or physical? */
767 udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
770 * XXX If reading the first Sparing Table fails, should look
773 if ((error = udf_readlblks(udfmp, le32toh(pms->st_loc[0]),
774 le32toh(pms->st_size), &bp)) != 0) {
777 printf("Failed to read Sparing Table at sector %d\n",
778 le32toh(pms->st_loc[0]));
779 FREE(udfmp->s_table, M_UDFMOUNT);
782 bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
785 if (udf_checktag(&udfmp->s_table->tag, 0)) {
786 printf("Invalid sparing table found\n");
787 FREE(udfmp->s_table, M_UDFMOUNT);
791 /* See how many valid entries there are here. The list is
792 * supposed to be sorted. 0xfffffff0 and higher are not valid
794 for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
795 udfmp->s_table_entries = k;
796 if (le32toh(udfmp->s_table->entries[k].org) >=