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
26 * $FreeBSD: src/sys/fs/udf/udf_vfsops.c,v 1.16 2003/11/05 06:56:08 scottl Exp $
27 * $DragonFly: src/sys/vfs/udf/udf_vfsops.c,v 1.28 2008/09/17 21:44:25 dillon Exp $
31 /* Implement the VFS side of things */
34 * Ok, here's how it goes. The UDF specs are pretty clear on how each data
35 * structure is made up, but not very clear on how they relate to each other.
36 * Here is the skinny... This demostrates a filesystem with one file in the
37 * root directory. Subdirectories are treated just as normal files, but they
38 * have File Id Descriptors of their children as their file data. As for the
39 * Anchor Volume Descriptor Pointer, it can exist in two of the following three
40 * places: sector 256, sector n (the max sector of the disk), or sector
41 * n - 256. It's a pretty good bet that one will exist at sector 256 though.
42 * One caveat is unclosed CD media. For that, sector 256 cannot be written,
43 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
48 * n: Anchor Volume Descriptor Pointer
51 * |-->Main Volume Descriptor Sequence
54 * | |-->Logical Volume Descriptor
56 * |-->Partition Descriptor |
59 * |-->Fileset Descriptor
62 * |-->Root Dir File Entry
75 #include <sys/types.h>
76 #include <sys/param.h>
77 #include <sys/systm.h>
81 #include <sys/fcntl.h>
82 #include <sys/module.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mount.h>
86 #include <sys/nlookup.h>
88 #include <sys/queue.h>
89 #include <sys/vnode.h>
91 #include <vfs/udf/ecma167-udf.h>
92 #include <vfs/udf/osta.h>
93 #include <vfs/udf/udf.h>
94 #include <vfs/udf/udf_mount.h>
96 extern struct vop_ops udf_vnode_vops;
98 MALLOC_DEFINE(M_UDFNODE, "UDF node", "UDF node structure");
99 MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
100 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
102 static int udf_mount(struct mount *, char *, caddr_t, struct ucred *);
103 static int udf_unmount(struct mount *, int);
104 static int udf_root(struct mount *, struct vnode **);
105 static int udf_statfs(struct mount *, struct statfs *, struct ucred *);
106 static int udf_fhtovp(struct mount *, struct vnode *,
107 struct fid *, struct vnode **);
108 static int udf_vptofh(struct vnode *, struct fid *);
110 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
112 static struct vfsops udf_vfsops = {
113 .vfs_mount = udf_mount,
114 .vfs_unmount = udf_unmount,
115 .vfs_root = udf_root,
116 .vfs_statfs = udf_statfs,
117 .vfs_sync = vfs_stdsync,
118 .vfs_vget = udf_vget,
119 .vfs_fhtovp = udf_fhtovp,
120 .vfs_vptofh = udf_vptofh
122 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
124 MODULE_VERSION(udf, 1);
126 static int udf_mountfs(struct vnode *, struct mount *);
129 udf_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
131 struct vnode *devvp; /* vnode of the mount device */
132 struct udf_args args;
133 struct udf_mnt *imp = 0;
136 struct nlookupdata nd;
138 if ((mp->mnt_flag & MNT_RDONLY) == 0)
142 * No root filesystem support. Probably not a big deal, since the
143 * bootloader doesn't understand UDF.
145 if (mp->mnt_flag & MNT_ROOTFS)
148 if ((error = copyin(data, (caddr_t)&args, sizeof(struct udf_args))))
151 if (mp->mnt_flag & MNT_UPDATE) {
152 imp = VFSTOUDFFS(mp);
153 if (args.fspec == NULL)
154 return(vfs_export(mp, &imp->im_export, &args.export));
157 /* Check that the mount device exists */
159 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
161 error = nlookup(&nd);
163 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
168 if (vn_isdisk(devvp, &error) == 0) {
173 /* Check the access rights on the mount device */
174 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
175 error = VOP_ACCESS(devvp, VREAD, cred);
177 error = suser_cred(cred, 0);
184 if ((error = udf_mountfs(devvp, mp))) {
189 imp = VFSTOUDFFS(mp);
191 imp->im_flags = args.flags;
193 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
194 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
195 udf_statfs(mp, &mp->mnt_stat, cred);
200 * Check the descriptor tag for both the correct id and correct checksum.
201 * Return zero if all is good, EINVAL if not.
204 udf_checktag(struct desc_tag *tag, uint16_t id)
207 uint8_t i, cksum = 0;
209 itag = (uint8_t *)tag;
214 for (i = 0; i < 15; i++)
215 cksum = cksum + itag[i];
216 cksum = cksum - itag[4];
218 if (cksum == tag->cksum)
225 udf_mountfs(struct vnode *devvp, struct mount *mp)
227 struct buf *bp = NULL;
228 struct anchor_vdp avdp;
229 struct udf_mnt *udfmp = NULL;
230 struct part_desc *pd;
231 struct logvol_desc *lvd;
232 struct fileset_desc *fsd;
233 struct file_entry *root_fentry;
235 uint32_t sector, size, mvds_start, mvds_end;
236 uint32_t fsd_offset = 0;
237 uint16_t part_num = 0, fsd_part = 0;
238 int error = EINVAL, needclose = 0;
239 int logvol_found = 0, part_found = 0, fsd_found = 0;
243 * Disallow multiple mounts of the same device. Flush the buffer
244 * cache for the device.
246 if ((error = vfs_mountedon(devvp)))
248 if (count_udev(devvp->v_umajor, devvp->v_uminor) > 0)
250 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)))
253 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
254 error = VOP_OPEN(devvp, FREAD, FSCRED, NULL);
261 udfmp = kmalloc(sizeof(*udfmp), M_UDFMOUNT, M_WAITOK | M_ZERO);
263 mp->mnt_data = (qaddr_t)udfmp;
264 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
265 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
266 mp->mnt_maxsymlinklen = 0;
267 mp->mnt_flag |= MNT_LOCAL;
268 udfmp->im_mountp = mp;
270 udfmp->im_devvp = devvp;
272 bsize = 2048; /* XXX Should probe the media for it's size */
275 * Get the Anchor Volume Descriptor Pointer from sector 256.
276 * XXX Should also check sector n - 256, n, and 512.
279 if ((error = bread(devvp, (off_t)sector * bsize, bsize, &bp)) != 0)
281 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
284 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
289 * Extract the Partition Descriptor and Logical Volume Descriptor
290 * from the Volume Descriptor Sequence.
291 * XXX Should we care about the partition type right now?
292 * XXX What about multiple partitions?
294 mvds_start = avdp.main_vds_ex.loc;
295 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
296 for (sector = mvds_start; sector < mvds_end; sector++) {
297 if ((error = bread(devvp, (off_t)sector * bsize, bsize,
299 kprintf("Can't read sector %d of VDS\n", sector);
302 lvd = (struct logvol_desc *)bp->b_data;
303 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
304 udfmp->bsize = lvd->lb_size;
305 udfmp->bmask = udfmp->bsize - 1;
306 udfmp->bshift = ffs(udfmp->bsize) - 1;
307 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
308 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
309 if (udf_find_partmaps(udfmp, lvd))
313 pd = (struct part_desc *)bp->b_data;
314 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
316 part_num = pd->part_num;
317 udfmp->part_len = pd->part_len;
318 udfmp->part_start = pd->start_loc;
323 if ((part_found) && (logvol_found))
327 if (!part_found || !logvol_found) {
332 if (fsd_part != part_num) {
333 kprintf("FSD does not lie within the partition!\n");
340 * Grab the Fileset Descriptor
341 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
342 * me in the right direction here.
344 sector = udfmp->part_start + fsd_offset;
345 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
346 kprintf("Cannot read sector %d of FSD\n", sector);
349 fsd = (struct fileset_desc *)bp->b_data;
350 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
352 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
353 sizeof(struct long_ad));
360 kprintf("Couldn't find the fsd\n");
365 vfs_add_vnodeops(mp, &udf_vnode_vops, &mp->mnt_vn_norm_ops);
368 * Find the file entry for the root directory.
370 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
371 size = udfmp->root_icb.len;
372 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
373 kprintf("Cannot read sector %d\n", sector);
377 root_fentry = (struct file_entry *)bp->b_data;
378 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
379 kprintf("Invalid root file entry!\n");
386 lwkt_token_init(&udfmp->hash_token);
387 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
393 kfree(udfmp, M_UDFMOUNT);
397 VOP_CLOSE(devvp, FREAD);
402 udf_unmount(struct mount *mp, int mntflags)
404 struct udf_mnt *udfmp;
405 int error, flags = 0;
407 udfmp = VFSTOUDFFS(mp);
409 if (mntflags & MNT_FORCE)
412 if ((error = vflush(mp, 0, flags)))
415 udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
416 error = VOP_CLOSE(udfmp->im_devvp, FREAD);
417 vrele(udfmp->im_devvp);
420 kfree(udfmp->s_table, M_UDFMOUNT);
422 kfree(udfmp->hashtbl, M_UDFMOUNT);
423 kfree(udfmp, M_UDFMOUNT);
425 mp->mnt_data = (qaddr_t)0;
426 mp->mnt_flag &= ~MNT_LOCAL;
432 udf_root(struct mount *mp, struct vnode **vpp)
434 struct udf_mnt *udfmp;
439 udfmp = VFSTOUDFFS(mp);
441 id = udf_getid(&udfmp->root_icb);
443 error = udf_vget(mp, id, vpp);
455 udf_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
457 struct udf_mnt *udfmp;
459 udfmp = VFSTOUDFFS(mp);
461 sbp->f_bsize = udfmp->bsize;
462 sbp->f_iosize = udfmp->bsize;
463 sbp->f_blocks = udfmp->part_len;
468 if (sbp != &mp->mnt_stat) {
469 sbp->f_type = mp->mnt_vfc->vfc_typenum;
470 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
477 udf_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
481 struct udf_mnt *udfmp;
484 struct udf_node *unode;
485 struct file_entry *fe;
486 int error, sector, size;
489 udfmp = VFSTOUDFFS(mp);
491 /* See if we already have this in the cache */
492 if ((error = udf_hashlookup(udfmp, ino, vpp)) != 0)
499 * Allocate memory and check the tag id's before grabbing a new
500 * vnode, since it's hard to roll back if there is a problem.
502 unode = kmalloc(sizeof(*unode), M_UDFNODE, M_WAITOK | M_ZERO);
505 * Copy in the file entry. Per the spec, the size can only be 1 block.
507 sector = ino + udfmp->part_start;
508 devvp = udfmp->im_devvp;
509 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
510 kprintf("Cannot read sector %d\n", sector);
511 kfree(unode, M_UDFNODE);
515 fe = (struct file_entry *)bp->b_data;
516 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
517 kprintf("Invalid file entry!\n");
518 kfree(unode, M_UDFNODE);
522 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
523 unode->fentry = kmalloc(size, M_UDFFENTRY, M_WAITOK | M_ZERO);
525 bcopy(bp->b_data, unode->fentry, size);
530 if ((error = udf_allocv(mp, &vp))) {
531 kprintf("Error from udf_allocv\n");
532 kfree(unode, M_UDFNODE);
537 unode->hash_id = ino;
538 unode->i_devvp = udfmp->im_devvp;
539 unode->i_dev = udfmp->im_dev;
540 unode->udfmp = udfmp;
542 vref(udfmp->im_devvp);
545 switch (unode->fentry->icbtag.file_type) {
572 * Locked and refd vnode returned
587 udf_fhtovp(struct mount *mp, struct vnode *rootvp,
588 struct fid *fhp, struct vnode **vpp)
594 ifhp = (struct ifid *)fhp;
596 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
606 udf_vptofh (struct vnode *vp, struct fid *fhp)
608 struct udf_node *node;
612 ifhp = (struct ifid *)fhp;
613 ifhp->ifid_len = sizeof(struct ifid);
614 ifhp->ifid_ino = node->hash_id;
620 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
622 union udf_pmap *pmap;
623 struct part_map_spare *pms;
624 struct regid *pmap_id;
626 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
627 int ptype, psize, error;
630 for (i = 0; i < lvd->n_pm; i++) {
631 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
632 ptype = pmap->data[0];
633 psize = pmap->data[1];
634 if (((ptype != 1) && (ptype != 2)) ||
635 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
636 kprintf("Invalid partition map found\n");
641 /* Type 1 map. We don't care */
645 /* Type 2 map. Gotta find out the details */
646 pmap_id = (struct regid *)&pmap->data[4];
647 bzero(®id_id[0], UDF_REGID_ID_SIZE);
648 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE);
650 if (bcmp(®id_id[0], "*UDF Sparable Partition",
651 UDF_REGID_ID_SIZE)) {
652 kprintf("Unsupported partition map: %s\n", ®id_id[0]);
657 udfmp->s_table = kmalloc(pms->st_size, M_UDFMOUNT,
660 /* Calculate the number of sectors per packet. */
661 /* XXX Logical or physical? */
662 udfmp->p_sectors = pms->packet_len / udfmp->bsize;
665 * XXX If reading the first Sparing Table fails, should look
668 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
672 kprintf("Failed to read Sparing Table at sector %d\n",
676 bcopy(bp->b_data, udfmp->s_table, pms->st_size);
679 if (udf_checktag(&udfmp->s_table->tag, 0)) {
680 kprintf("Invalid sparing table found\n");
684 /* See how many valid entries there are here. The list is
685 * supposed to be sorted. 0xfffffff0 and higher are not valid
687 for (i = 0; i < udfmp->s_table->rt_l; i++) {
688 udfmp->s_table_entries = i;
689 if (udfmp->s_table->entries[i].org >= 0xfffffff0)