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.12 2005/02/02 21:34:18 joerg 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/dirent.h>
82 #include <sys/fcntl.h>
83 #include <sys/module.h>
84 #include <sys/kernel.h>
85 #include <sys/malloc.h>
86 #include <sys/mount.h>
87 #include <sys/nlookup.h>
89 #include <sys/queue.h>
90 #include <sys/vnode.h>
92 #include <vfs/udf/ecma167-udf.h>
93 #include <vfs/udf/osta.h>
94 #include <vfs/udf/udf.h>
95 #include <vfs/udf/udf_mount.h>
97 extern struct vnodeopv_entry_desc udf_vnodeop_entries[];
99 MALLOC_DEFINE(M_UDFNODE, "UDF node", "UDF node structure");
100 MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
101 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
103 static int udf_mount(struct mount *, char *, caddr_t, struct thread *);
104 static int udf_unmount(struct mount *, int, struct thread *);
105 static int udf_root(struct mount *, struct vnode **);
106 static int udf_statfs(struct mount *, struct statfs *, struct thread *);
107 static int udf_fhtovp(struct mount *, 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 = {
128 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
130 MODULE_VERSION(udf, 1);
132 static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
135 udf_mount(struct mount *mp, char *path, caddr_t data, struct thread *td)
137 struct vnode *devvp; /* vnode of the mount device */
138 struct udf_args args;
139 struct udf_mnt *imp = 0;
142 struct nlookupdata nd;
144 if ((mp->mnt_flag & MNT_RDONLY) == 0)
148 * No root filesystem support. Probably not a big deal, since the
149 * bootloader doesn't understand UDF.
151 if (mp->mnt_flag & MNT_ROOTFS)
154 if ((error = copyin(data, (caddr_t)&args, sizeof(struct udf_args))))
157 if (mp->mnt_flag & MNT_UPDATE) {
158 imp = VFSTOUDFFS(mp);
159 if (args.fspec == NULL)
160 return(vfs_export(mp, &imp->im_export, &args.export));
163 /* Check that the mount device exists */
165 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
167 error = nlookup(&nd);
169 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
174 if (vn_isdisk(devvp, &error) == 0) {
179 /* Check the access rights on the mount device */
180 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
181 error = VOP_ACCESS(devvp, VREAD, td->td_proc->p_ucred, td);
188 VOP_UNLOCK(devvp, 0, td);
190 if ((error = udf_mountfs(devvp, mp, td))) {
195 imp = VFSTOUDFFS(mp);
197 imp->im_flags = args.flags;
199 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
200 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
201 udf_statfs(mp, &mp->mnt_stat, td);
206 * Check the descriptor tag for both the correct id and correct checksum.
207 * Return zero if all is good, EINVAL if not.
210 udf_checktag(struct desc_tag *tag, uint16_t id)
213 uint8_t i, cksum = 0;
215 itag = (uint8_t *)tag;
220 for (i = 0; i < 15; i++)
221 cksum = cksum + itag[i];
222 cksum = cksum - itag[4];
224 if (cksum == tag->cksum)
231 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td)
233 struct buf *bp = NULL;
234 struct anchor_vdp avdp;
235 struct udf_mnt *udfmp = NULL;
236 struct part_desc *pd;
237 struct logvol_desc *lvd;
238 struct fileset_desc *fsd;
239 struct file_entry *root_fentry;
241 uint32_t sector, size, mvds_start, mvds_end;
242 uint32_t fsd_offset = 0;
243 uint16_t part_num = 0, fsd_part = 0;
244 int error = EINVAL, needclose = 0;
245 int logvol_found = 0, part_found = 0, fsd_found = 0;
249 * Disallow multiple mounts of the same device. Flush the buffer
250 * cache for the device.
252 if ((error = vfs_mountedon(devvp)))
254 if (count_udev(devvp->v_udev) > 0)
256 if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
259 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
260 error = VOP_OPEN(devvp, FREAD, FSCRED, NULL, td);
261 VOP_UNLOCK(devvp, 0, td);
267 udfmp = malloc(sizeof(*udfmp), M_UDFMOUNT, M_WAITOK | M_ZERO);
269 mp->mnt_data = (qaddr_t)udfmp;
270 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
271 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
272 mp->mnt_maxsymlinklen = 0;
273 mp->mnt_flag |= MNT_LOCAL;
274 udfmp->im_mountp = mp;
276 udfmp->im_devvp = devvp;
278 bsize = 2048; /* XXX Should probe the media for it's size */
281 * Get the Anchor Volume Descriptor Pointer from sector 256.
282 * XXX Should also check sector n - 256, n, and 512.
285 if ((error = bread(devvp, sector * btodb(bsize), bsize, &bp)) != 0)
287 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
290 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
295 * Extract the Partition Descriptor and Logical Volume Descriptor
296 * from the Volume Descriptor Sequence.
297 * XXX Should we care about the partition type right now?
298 * XXX What about multiple partitions?
300 mvds_start = avdp.main_vds_ex.loc;
301 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
302 for (sector = mvds_start; sector < mvds_end; sector++) {
303 if ((error = bread(devvp, sector * btodb(bsize), bsize,
305 printf("Can't read sector %d of VDS\n", sector);
308 lvd = (struct logvol_desc *)bp->b_data;
309 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
310 udfmp->bsize = lvd->lb_size;
311 udfmp->bmask = udfmp->bsize - 1;
312 udfmp->bshift = ffs(udfmp->bsize) - 1;
313 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
314 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
315 if (udf_find_partmaps(udfmp, lvd))
319 pd = (struct part_desc *)bp->b_data;
320 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
322 part_num = pd->part_num;
323 udfmp->part_len = pd->part_len;
324 udfmp->part_start = pd->start_loc;
329 if ((part_found) && (logvol_found))
333 if (!part_found || !logvol_found) {
338 if (fsd_part != part_num) {
339 printf("FSD does not lie within the partition!\n");
346 * Grab the Fileset Descriptor
347 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
348 * me in the right direction here.
350 sector = udfmp->part_start + fsd_offset;
351 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
352 printf("Cannot read sector %d of FSD\n", sector);
355 fsd = (struct fileset_desc *)bp->b_data;
356 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
358 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
359 sizeof(struct long_ad));
366 printf("Couldn't find the fsd\n");
371 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, udf_vnodeop_entries);
374 * Find the file entry for the root directory.
376 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
377 size = udfmp->root_icb.len;
378 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
379 printf("Cannot read sector %d\n", sector);
383 root_fentry = (struct file_entry *)bp->b_data;
384 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
385 printf("Invalid root file entry!\n");
392 lwkt_token_init(&udfmp->hash_token);
393 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
399 free(udfmp, M_UDFMOUNT);
403 VOP_CLOSE(devvp, FREAD, td);
408 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
410 struct udf_mnt *udfmp;
411 int error, flags = 0;
413 udfmp = VFSTOUDFFS(mp);
415 if (mntflags & MNT_FORCE)
418 if ((error = vflush(mp, 0, flags)))
421 udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
422 error = VOP_CLOSE(udfmp->im_devvp, FREAD, td);
423 vrele(udfmp->im_devvp);
426 free(udfmp->s_table, M_UDFMOUNT);
428 free(udfmp->hashtbl, M_UDFMOUNT);
429 free(udfmp, M_UDFMOUNT);
431 mp->mnt_data = (qaddr_t)0;
432 mp->mnt_flag &= ~MNT_LOCAL;
438 udf_root(struct mount *mp, struct vnode **vpp)
440 struct udf_mnt *udfmp;
445 udfmp = VFSTOUDFFS(mp);
447 id = udf_getid(&udfmp->root_icb);
449 error = udf_vget(mp, id, vpp);
461 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
463 struct udf_mnt *udfmp;
465 udfmp = VFSTOUDFFS(mp);
467 sbp->f_bsize = udfmp->bsize;
468 sbp->f_iosize = udfmp->bsize;
469 sbp->f_blocks = udfmp->part_len;
474 if (sbp != &mp->mnt_stat) {
475 sbp->f_type = mp->mnt_vfc->vfc_typenum;
476 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
483 udf_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
487 struct udf_mnt *udfmp;
490 struct udf_node *unode;
491 struct file_entry *fe;
492 int error, sector, size;
495 udfmp = VFSTOUDFFS(mp);
497 /* See if we already have this in the cache */
498 if ((error = udf_hashlookup(udfmp, ino, vpp)) != 0)
505 * Allocate memory and check the tag id's before grabbing a new
506 * vnode, since it's hard to roll back if there is a problem.
508 unode = malloc(sizeof(*unode), M_UDFNODE, M_WAITOK | M_ZERO);
511 * Copy in the file entry. Per the spec, the size can only be 1 block.
513 sector = ino + udfmp->part_start;
514 devvp = udfmp->im_devvp;
515 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
516 printf("Cannot read sector %d\n", sector);
517 free(unode, M_UDFNODE);
521 fe = (struct file_entry *)bp->b_data;
522 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
523 printf("Invalid file entry!\n");
524 free(unode, M_UDFNODE);
528 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
529 unode->fentry = malloc(size, M_UDFFENTRY, M_WAITOK | M_ZERO);
531 bcopy(bp->b_data, unode->fentry, size);
536 if ((error = udf_allocv(mp, &vp))) {
537 printf("Error from udf_allocv\n");
538 free(unode, M_UDFNODE);
543 unode->hash_id = ino;
544 unode->i_devvp = udfmp->im_devvp;
545 unode->i_dev = udfmp->im_dev;
546 unode->udfmp = udfmp;
548 vref(udfmp->im_devvp);
551 switch (unode->fentry->icbtag.file_type) {
578 * Locked and refd vnode returned
593 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
599 ifhp = (struct ifid *)fhp;
601 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
611 udf_vptofh (struct vnode *vp, struct fid *fhp)
613 struct udf_node *node;
617 ifhp = (struct ifid *)fhp;
618 ifhp->ifid_len = sizeof(struct ifid);
619 ifhp->ifid_ino = node->hash_id;
625 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
627 union udf_pmap *pmap;
628 struct part_map_spare *pms;
629 struct regid *pmap_id;
631 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
632 int ptype, psize, error;
635 for (i = 0; i < lvd->n_pm; i++) {
636 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
637 ptype = pmap->data[0];
638 psize = pmap->data[1];
639 if (((ptype != 1) && (ptype != 2)) ||
640 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
641 printf("Invalid partition map found\n");
646 /* Type 1 map. We don't care */
650 /* Type 2 map. Gotta find out the details */
651 pmap_id = (struct regid *)&pmap->data[4];
652 bzero(®id_id[0], UDF_REGID_ID_SIZE);
653 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE);
655 if (bcmp(®id_id[0], "*UDF Sparable Partition",
656 UDF_REGID_ID_SIZE)) {
657 printf("Unsupported partition map: %s\n", ®id_id[0]);
662 udfmp->s_table = malloc(pms->st_size, M_UDFMOUNT,
664 if (udfmp->s_table == NULL)
667 /* Calculate the number of sectors per packet. */
668 /* XXX Logical or physical? */
669 udfmp->p_sectors = pms->packet_len / udfmp->bsize;
672 * XXX If reading the first Sparing Table fails, should look
675 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
679 printf("Failed to read Sparing Table at sector %d\n",
683 bcopy(bp->b_data, udfmp->s_table, pms->st_size);
686 if (udf_checktag(&udfmp->s_table->tag, 0)) {
687 printf("Invalid sparing table found\n");
691 /* See how many valid entries there are here. The list is
692 * supposed to be sorted. 0xfffffff0 and higher are not valid
694 for (i = 0; i < udfmp->s_table->rt_l; i++) {
695 udfmp->s_table_entries = i;
696 if (udfmp->s_table->entries[i].org >= 0xfffffff0)