2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* Implement the VFS side of things */
35 * Ok, here's how it goes. The UDF specs are pretty clear on how each data
36 * structure is made up, but not very clear on how they relate to each other.
37 * Here is the skinny... This demostrates a filesystem with one file in the
38 * root directory. Subdirectories are treated just as normal files, but they
39 * have File Id Descriptors of their children as their file data. As for the
40 * Anchor Volume Descriptor Pointer, it can exist in two of the following three
41 * places: sector 256, sector n (the max sector of the disk), or sector
42 * n - 256. It's a pretty good bet that one will exist at sector 256 though.
43 * One caveat is unclosed CD media. For that, sector 256 cannot be written,
44 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
49 * n: Anchor Volume Descriptor Pointer
52 * |-->Main Volume Descriptor Sequence
55 * | |-->Logical Volume Descriptor
57 * |-->Partition Descriptor |
60 * |-->Fileset Descriptor
63 * |-->Root Dir File Entry
75 #include <sys/types.h>
76 #include <sys/param.h>
77 #include <sys/systm.h>
82 #include <sys/dirent.h>
83 #include <sys/fcntl.h>
84 #include <sys/iconv.h>
85 #include <sys/kernel.h>
86 #include <sys/malloc.h>
87 #include <sys/mount.h>
88 #include <sys/namei.h>
91 #include <sys/queue.h>
92 #include <sys/vnode.h>
93 #include <sys/endian.h>
95 #include <geom/geom.h>
96 #include <geom/geom_vfs.h>
100 #include <fs/udf/ecma167-udf.h>
101 #include <fs/udf/osta.h>
102 #include <fs/udf/udf.h>
103 #include <fs/udf/udf_mount.h>
105 static MALLOC_DEFINE(M_UDFMOUNT, "udf_mount", "UDF mount structure");
106 MALLOC_DEFINE(M_UDFFENTRY, "udf_fentry", "UDF file entry structure");
108 struct iconv_functions *udf_iconv = NULL;
111 uma_zone_t udf_zone_trans = NULL;
112 uma_zone_t udf_zone_node = NULL;
113 uma_zone_t udf_zone_ds = NULL;
115 static vfs_init_t udf_init;
116 static vfs_uninit_t udf_uninit;
117 static vfs_mount_t udf_mount;
118 static vfs_root_t udf_root;
119 static vfs_statfs_t udf_statfs;
120 static vfs_unmount_t udf_unmount;
121 static vfs_fhtovp_t udf_fhtovp;
123 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
125 static struct vfsops udf_vfsops = {
126 .vfs_fhtovp = udf_fhtovp,
127 .vfs_init = udf_init,
128 .vfs_mount = udf_mount,
129 .vfs_root = udf_root,
130 .vfs_statfs = udf_statfs,
131 .vfs_uninit = udf_uninit,
132 .vfs_unmount = udf_unmount,
133 .vfs_vget = udf_vget,
135 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
137 MODULE_VERSION(udf, 1);
139 static int udf_mountfs(struct vnode *, struct mount *);
142 udf_init(struct vfsconf *foo)
146 * This code used to pre-allocate a certain number of pages for each
147 * pool, reducing the need to grow the zones later on. UMA doesn't
148 * advertise any such functionality, unfortunately =-<
150 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
151 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
153 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
154 NULL, NULL, NULL, NULL, 0, 0);
156 udf_zone_ds = uma_zcreate("UDF Dirstream zone",
157 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
159 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
160 (udf_zone_ds == NULL)) {
161 printf("Cannot create allocation zones.\n");
169 udf_uninit(struct vfsconf *foo)
172 if (udf_zone_trans != NULL) {
173 uma_zdestroy(udf_zone_trans);
174 udf_zone_trans = NULL;
177 if (udf_zone_node != NULL) {
178 uma_zdestroy(udf_zone_node);
179 udf_zone_node = NULL;
182 if (udf_zone_ds != NULL) {
183 uma_zdestroy(udf_zone_ds);
191 udf_mount(struct mount *mp)
193 struct vnode *devvp; /* vnode of the mount device */
195 struct udf_mnt *imp = NULL;
196 struct vfsoptlist *opts;
197 char *fspec, *cs_disk, *cs_local;
198 int error, len, *udf_flags;
199 struct nameidata nd, *ndp = &nd;
202 opts = mp->mnt_optnew;
205 * Unconditionally mount as read-only.
208 mp->mnt_flag |= MNT_RDONLY;
212 * No root filesystem support. Probably not a big deal, since the
213 * bootloader doesn't understand UDF.
215 if (mp->mnt_flag & MNT_ROOTFS)
219 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
220 if (!error && fspec[len - 1] != '\0')
223 if (mp->mnt_flag & MNT_UPDATE) {
227 /* Check that the mount device exists */
230 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
231 if ((error = namei(ndp)))
233 NDFREE(ndp, NDF_ONLY_PNBUF);
236 if (!vn_isdisk_error(devvp, &error)) {
241 /* Check the access rights on the mount device */
242 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
244 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
250 if ((error = udf_mountfs(devvp, mp))) {
255 imp = VFSTOUDFFS(mp);
258 error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
259 if (error || len != sizeof(int))
261 imp->im_flags = *udf_flags;
263 if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
265 error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
266 if (!error && cs_disk[len - 1] != '\0')
269 error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
270 if (!error && cs_local[len - 1] != '\0')
272 udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
274 udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
278 vfs_mountedfrom(mp, fspec);
283 * Check the descriptor tag for both the correct id and correct checksum.
284 * Return zero if all is good, EINVAL if not.
287 udf_checktag(struct desc_tag *tag, uint16_t id)
290 uint8_t i, cksum = 0;
292 itag = (uint8_t *)tag;
294 if (le16toh(tag->id) != id)
297 for (i = 0; i < 16; i++)
298 cksum = cksum + itag[i];
299 cksum = cksum - itag[4];
301 if (cksum == tag->cksum)
308 udf_mountfs(struct vnode *devvp, struct mount *mp)
310 struct buf *bp = NULL;
312 struct anchor_vdp avdp;
313 struct udf_mnt *udfmp = NULL;
314 struct part_desc *pd;
315 struct logvol_desc *lvd;
316 struct fileset_desc *fsd;
317 struct file_entry *root_fentry;
318 uint32_t sector, size, mvds_start, mvds_end;
319 uint32_t logical_secsize;
320 uint32_t fsd_offset = 0;
321 uint16_t part_num = 0, fsd_part = 0;
323 int logvol_found = 0, part_found = 0, fsd_found = 0;
325 struct g_consumer *cp;
331 error = g_vfs_open(devvp, &cp, "udf", 0);
337 bo = &devvp->v_bufobj;
339 if (devvp->v_rdev->si_iosize_max != 0)
340 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
341 if (mp->mnt_iosize_max > maxphys)
342 mp->mnt_iosize_max = maxphys;
344 /* XXX: should be M_WAITOK */
345 udfmp = malloc(sizeof(struct udf_mnt), M_UDFMOUNT,
348 printf("Cannot allocate UDF mount struct\n");
353 mp->mnt_data = udfmp;
354 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
355 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
357 mp->mnt_flag |= MNT_LOCAL;
358 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
360 udfmp->im_mountp = mp;
362 udfmp->im_devvp = devvp;
363 udfmp->im_d2l = NULL;
368 udfmp->im_l2d = NULL;
371 * The UDF specification defines a logical sectorsize of 2048
374 logical_secsize = 2048;
376 if (((logical_secsize % cp->provider->sectorsize) != 0) ||
377 (logical_secsize < cp->provider->sectorsize)) {
382 bsize = cp->provider->sectorsize;
385 * Get the Anchor Volume Descriptor Pointer from sector 256.
386 * XXX Should also check sector n - 256, n, and 512.
389 if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
392 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
395 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
400 * Extract the Partition Descriptor and Logical Volume Descriptor
401 * from the Volume Descriptor Sequence.
402 * XXX Should we care about the partition type right now?
403 * XXX What about multiple partitions?
405 mvds_start = le32toh(avdp.main_vds_ex.loc);
406 mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
407 for (sector = mvds_start; sector < mvds_end; sector++) {
408 if ((error = bread(devvp, sector * btodb(logical_secsize),
409 bsize, NOCRED, &bp)) != 0) {
410 printf("Can't read sector %d of VDS\n", sector);
413 lvd = (struct logvol_desc *)bp->b_data;
414 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
415 udfmp->bsize = le32toh(lvd->lb_size);
416 udfmp->bmask = udfmp->bsize - 1;
417 udfmp->bshift = ffs(udfmp->bsize) - 1;
418 fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
419 fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
420 if (udf_find_partmaps(udfmp, lvd))
424 pd = (struct part_desc *)bp->b_data;
425 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
427 part_num = le16toh(pd->part_num);
428 udfmp->part_len = le32toh(pd->part_len);
429 udfmp->part_start = le32toh(pd->start_loc);
434 if ((part_found) && (logvol_found))
438 if (!part_found || !logvol_found) {
443 if (fsd_part != part_num) {
444 printf("FSD does not lie within the partition!\n");
450 * Grab the Fileset Descriptor
451 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
452 * me in the right direction here.
454 sector = udfmp->part_start + fsd_offset;
455 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
456 printf("Cannot read sector %d of FSD\n", sector);
459 fsd = (struct fileset_desc *)bp->b_data;
460 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
462 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
463 sizeof(struct long_ad));
470 printf("Couldn't find the fsd\n");
476 * Find the file entry for the root directory.
478 sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
479 size = le32toh(udfmp->root_icb.len);
480 if ((error = udf_readdevblks(udfmp, sector, size, &bp)) != 0) {
481 printf("Cannot read sector %d\n", sector);
485 root_fentry = (struct file_entry *)bp->b_data;
486 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
487 printf("Invalid root file entry!\n");
498 free(udfmp, M_UDFMOUNT);
511 udf_unmount(struct mount *mp, int mntflags)
513 struct udf_mnt *udfmp;
514 int error, flags = 0;
516 udfmp = VFSTOUDFFS(mp);
518 if (mntflags & MNT_FORCE)
521 if ((error = vflush(mp, 0, flags, curthread)))
524 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
526 udf_iconv->close(udfmp->im_d2l);
529 udf_iconv->close(udfmp->im_l2d);
534 g_vfs_close(udfmp->im_cp);
536 vrele(udfmp->im_devvp);
537 dev_rel(udfmp->im_dev);
539 if (udfmp->s_table != NULL)
540 free(udfmp->s_table, M_UDFMOUNT);
542 free(udfmp, M_UDFMOUNT);
546 mp->mnt_flag &= ~MNT_LOCAL;
553 udf_root(struct mount *mp, int flags, struct vnode **vpp)
555 struct udf_mnt *udfmp;
558 udfmp = VFSTOUDFFS(mp);
560 id = udf_getid(&udfmp->root_icb);
562 return (udf_vget(mp, id, flags, vpp));
566 udf_statfs(struct mount *mp, struct statfs *sbp)
568 struct udf_mnt *udfmp;
570 udfmp = VFSTOUDFFS(mp);
572 sbp->f_bsize = udfmp->bsize;
573 sbp->f_iosize = udfmp->bsize;
574 sbp->f_blocks = udfmp->part_len;
583 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
587 struct udf_mnt *udfmp;
590 struct udf_node *unode;
591 struct file_entry *fe;
593 int error, sector, size;
595 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
596 if (error || *vpp != NULL)
600 * We must promote to an exclusive lock for vnode creation. This
601 * can happen if lookup is passed LOCKSHARED.
603 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
604 flags &= ~LK_TYPE_MASK;
605 flags |= LK_EXCLUSIVE;
609 * We do not lock vnode creation as it is believed to be too
610 * expensive for such rare case as simultaneous creation of vnode
611 * for same ino by different processes. We just allow them to race
612 * and check later to decide who wins. Let the race begin!
616 udfmp = VFSTOUDFFS(mp);
618 unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
620 if ((error = udf_allocv(mp, &vp, td))) {
621 printf("Error from udf_allocv\n");
622 uma_zfree(udf_zone_node, unode);
627 unode->hash_id = ino;
628 unode->udfmp = udfmp;
631 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
632 error = insmntque(vp, mp);
634 uma_zfree(udf_zone_node, unode);
637 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
638 if (error || *vpp != NULL)
642 * Copy in the file entry. Per the spec, the size can only be 1 block.
644 sector = ino + udfmp->part_start;
645 devvp = udfmp->im_devvp;
646 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
647 printf("Cannot read sector %d\n", sector);
652 * File entry length validation.
654 fe = (struct file_entry *)bp->b_data;
655 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
656 printf("Invalid file entry!\n");
660 lea = le32toh(fe->l_ea);
661 lad = le32toh(fe->l_ad);
662 if (lea > udfmp->bsize || lad > udfmp->bsize) {
663 printf("Invalid EA and AD lengths %u, %u\n", lea, lad);
667 size = UDF_FENTRY_SIZE + lea + lad;
668 if (size > udfmp->bsize) {
669 printf("Invalid file entry size %u\n", size);
674 unode->fentry = malloc(size, M_UDFFENTRY, M_NOWAIT | M_ZERO);
675 if (unode->fentry == NULL) {
676 printf("Cannot allocate file entry block\n");
681 bcopy(bp->b_data, unode->fentry, size);
686 switch (unode->fentry->icbtag.file_type) {
704 vp->v_op = &udf_fifoops;
714 if (vp->v_type != VFIFO)
717 if (ino == udf_getid(&udfmp->root_icb))
718 vp->v_vflag |= VV_ROOT;
733 udf_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
741 ifhp = (struct ifid *)fhp;
743 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
749 fsize = le64toh(np->fentry->inf_len);
752 vnode_create_vobject(*vpp, fsize, curthread);
757 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
759 struct part_map_spare *pms;
760 struct regid *pmap_id;
762 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
763 int i, k, ptype, psize, error;
764 uint8_t *pmap = (uint8_t *) &lvd->maps[0];
766 for (i = 0; i < le32toh(lvd->n_pm); i++) {
769 if (((ptype != 1) && (ptype != 2)) ||
770 ((psize != UDF_PMAP_TYPE1_SIZE) &&
771 (psize != UDF_PMAP_TYPE2_SIZE))) {
772 printf("Invalid partition map found\n");
777 /* Type 1 map. We don't care */
778 pmap += UDF_PMAP_TYPE1_SIZE;
782 /* Type 2 map. Gotta find out the details */
783 pmap_id = (struct regid *)&pmap[4];
784 bzero(®id_id[0], UDF_REGID_ID_SIZE);
785 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE);
787 if (bcmp(®id_id[0], "*UDF Sparable Partition",
788 UDF_REGID_ID_SIZE)) {
789 printf("Unsupported partition map: %s\n", ®id_id[0]);
793 pms = (struct part_map_spare *)pmap;
794 pmap += UDF_PMAP_TYPE2_SIZE;
795 udfmp->s_table = malloc(le32toh(pms->st_size),
796 M_UDFMOUNT, M_NOWAIT | M_ZERO);
797 if (udfmp->s_table == NULL)
800 /* Calculate the number of sectors per packet. */
801 /* XXX Logical or physical? */
802 udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
805 * XXX If reading the first Sparing Table fails, should look
808 if ((error = udf_readdevblks(udfmp, le32toh(pms->st_loc[0]),
809 le32toh(pms->st_size), &bp)) != 0) {
812 printf("Failed to read Sparing Table at sector %d\n",
813 le32toh(pms->st_loc[0]));
814 free(udfmp->s_table, M_UDFMOUNT);
817 bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
820 if (udf_checktag(&udfmp->s_table->tag, 0)) {
821 printf("Invalid sparing table found\n");
822 free(udfmp->s_table, M_UDFMOUNT);
826 /* See how many valid entries there are here. The list is
827 * supposed to be sorted. 0xfffffff0 and higher are not valid
829 for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
830 udfmp->s_table_entries = k;
831 if (le32toh(udfmp->s_table->entries[k].org) >=