2 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@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 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/vnode.h>
37 #include <sys/mount.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
43 #include <geom/geom.h>
44 #include <geom/geom_vfs.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_object.h>
50 #include <vm/vm_extern.h>
52 #include <fs/hpfs/hpfs.h>
53 #include <fs/hpfs/hpfsmount.h>
54 #include <fs/hpfs/hpfs_subr.h>
56 MALLOC_DEFINE(M_HPFSMNT, "hpfs_mount", "HPFS mount structure");
57 MALLOC_DEFINE(M_HPFSNO, "hpfs_node", "HPFS node structure");
61 static int hpfs_mountfs(register struct vnode *, struct mount *,
64 static vfs_fhtovp_t hpfs_fhtovp;
65 static vfs_vget_t hpfs_vget;
66 static vfs_cmount_t hpfs_cmount;
67 static vfs_mount_t hpfs_mount;
68 static vfs_root_t hpfs_root;
69 static vfs_statfs_t hpfs_statfs;
70 static vfs_unmount_t hpfs_unmount;
78 struct hpfs_args args;
79 struct export_args exp;
82 error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
85 vfs_oexport_conv(&args.export, &exp);
87 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
88 ma = mount_arg(ma, "export", &exp, sizeof(exp));
89 ma = mount_argf(ma, "uid", "%d", args.uid);
90 ma = mount_argf(ma, "gid", "%d", args.gid);
91 ma = mount_argf(ma, "mode", "%d", args.mode);
92 if (args.flags & HPFSMNT_TABLES) {
93 ma = mount_arg(ma, "d2u", args.d2u, sizeof args.d2u);
94 ma = mount_arg(ma, "u2d", args.u2d, sizeof args.u2d);
97 error = kernel_mount(ma, flags);
102 static const char *hpfs_opts[] = {
103 "from", "export", "uid", "gid", "mode", "d2u", "u2d", NULL
107 hpfs_mount (struct mount *mp)
112 struct nameidata ndp;
113 struct export_args export;
117 dprintf(("hpfs_omount():\n"));
120 * Mounting non-root filesystem or updating a filesystem
123 if (vfs_filteropt(mp->mnt_optnew, hpfs_opts))
126 from = vfs_getopts(mp->mnt_optnew, "from", &error);
131 * If updating, check whether changing from read-only to
132 * read/write; if there is no device name, that's all we do.
134 if (mp->mnt_flag & MNT_UPDATE) {
135 dprintf(("hpfs_omount: MNT_UPDATE: "));
138 error = vfs_copyopt(mp->mnt_optnew, "export",
139 &export, sizeof export);
142 dprintf(("export 0x%x\n",args.export.ex_flags));
143 err = vfs_export(mp, &export);
145 printf("hpfs_omount: vfs_export failed %d\n",
150 dprintf(("name [FAILED]\n"));
158 * Not an update, or updating the name: look up the name
159 * and verify that it refers to a sensible block device.
161 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
164 /* can't get devvp!*/
170 if (!vn_isdisk(devvp, &err)) {
182 * Since this is a new mount, we want the names for
183 * the device and the mount point copied in. If an
184 * error occurs, the mountpoint is discarded by the
185 * upper level code. Note that vfs_omount() handles
186 * copying the mountpoint f_mntonname for us, so we
187 * don't have to do it here unless we want to set it
188 * to something other than "path" for some rason.
190 /* Save "mounted from" info for mount point (NULL pad)*/
191 vfs_mountedfrom(mp, from);
193 err = hpfs_mountfs(devvp, mp, td);
201 error_1: /* no state to back out*/
202 /* XXX: Missing NDFREE(&ndp, ...) */
209 * Common code for mount and mountroot
212 hpfs_mountfs(devvp, mp, td)
213 register struct vnode *devvp;
220 struct hpfsmount *hpmp;
221 struct buf *bp = NULL;
223 struct cdev *dev = devvp->v_rdev;
224 struct g_consumer *cp;
227 if (mp->mnt_flag & MNT_ROOTFS)
229 dprintf(("hpfs_mountfs():\n"));
230 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
231 /* XXX: use VOP_ACCESS to check FS perms */
234 error = g_vfs_open(devvp, &cp, "hpfs", ronly ? 0 : 1);
237 VOP_UNLOCK(devvp, 0);
241 bo = &devvp->v_bufobj;
243 bo->bo_ops = g_vfs_bufops;
248 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK | M_ZERO);
253 /* Read in SuperBlock */
254 error = bread(devvp, SUBLOCK, SUSIZE, NOCRED, &bp);
257 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
258 brelse(bp); bp = NULL;
260 /* Read in SpareBlock */
261 error = bread(devvp, SPBLOCK, SPSIZE, NOCRED, &bp);
264 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
265 brelse(bp); bp = NULL;
271 if (sup->su_magic != SU_MAGIC) {
272 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
276 if (spp->sp_magic != SP_MAGIC) {
277 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
283 hpmp->hpm_devvp = devvp;
284 hpmp->hpm_dev = devvp->v_rdev;
286 if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
288 if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
290 if (vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v) == 1)
293 error = hpfs_bminit(hpmp);
297 error = hpfs_cpinit(mp, hpmp);
303 error = hpfs_root(mp, LK_EXCLUSIVE, &vp);
312 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
313 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
314 mp->mnt_maxsymlinklen = 0;
316 mp->mnt_flag |= MNT_LOCAL;
338 register struct hpfsmount *hpmp = VFSTOHPFS(mp);
340 dprintf(("hpfs_unmount():\n"));
343 if(mntflags & MNT_FORCE)
346 dprintf(("hpfs_unmount: vflushing...\n"));
348 error = vflush(mp, 0, flags, curthread);
350 printf("hpfs_unmount: vflush failed: %d\n",error);
354 vinvalbuf(hpmp->hpm_devvp, V_SAVE, 0, 0);
357 g_vfs_close(hpmp->hpm_cp);
360 vrele(hpmp->hpm_devvp);
362 dprintf(("hpfs_umount: freeing memory...\n"));
367 mp->mnt_flag &= ~MNT_LOCAL;
369 free(hpmp, M_HPFSMNT);
381 struct hpfsmount *hpmp = VFSTOHPFS(mp);
383 dprintf(("hpfs_root():\n"));
384 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, LK_EXCLUSIVE, vpp);
386 printf("hpfs_root: VFS_VGET failed: %d\n",error);
398 struct hpfsmount *hpmp = VFSTOHPFS(mp);
400 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
401 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
403 sbp->f_type = mp->mnt_vfc->vfc_typenum;
404 sbp->f_bsize = DEV_BSIZE;
405 sbp->f_iosize = DEV_BSIZE;
406 sbp->f_blocks = hpmp->hpm_su.su_btotal;
407 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
410 sbp->f_flags = mp->mnt_flag;
424 struct hpfid *hpfhp = (struct hpfid *)fhp;
427 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
431 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
432 * with HPFS, we don't need to check anything else for now */
445 struct hpfsmount *hpmp = VFSTOHPFS(mp);
451 dprintf(("hpfs_vget(0x%x): ",ino));
453 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
454 if (error || *vpp != NULL)
462 * We have to lock node creation for a while,
463 * but then we have to call getnewvnode(),
464 * this may cause hpfs_reclaim() to be called,
465 * this may need to VOP_VGET() parent dir for
466 * update reasons, and if parent is not in
467 * hash, we have to lock node creation...
468 * To solve this, we MALLOC, getnewvnode and init while
469 * not locked (probability of node appearence
470 * at that time is little, and anyway - we'll
473 hp = malloc(sizeof(struct hpfsnode),
476 error = getnewvnode("hpfs", mp, &hpfs_vnodeops, &vp);
478 printf("hpfs_vget: can't get new vnode\n");
483 dprintf(("prenew "));
487 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
488 vp->v_vflag |= VV_ROOT;
491 mtx_init(&hp->h_interlock, "hpfsnode interlock", NULL, MTX_DEF);
493 hp->h_flag = H_INVAL;
497 hp->h_dev = hpmp->hpm_dev;
498 hp->h_uid = hpmp->hpm_uid;
499 hp->h_gid = hpmp->hpm_uid;
500 hp->h_mode = hpmp->hpm_mode;
501 hp->h_devvp = hpmp->hpm_devvp;
503 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
504 error = insmntque(vp, mp);
509 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
510 if (error || *vpp != NULL)
513 error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp);
515 printf("hpfs_vget: can't read ino %d\n",ino);
519 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
522 if (hp->h_fn.fn_magic != FN_MAGIC) {
523 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
528 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
529 hp->h_flag &= ~H_INVAL;
536 static struct vfsops hpfs_vfsops = {
537 .vfs_fhtovp = hpfs_fhtovp,
538 .vfs_cmount = hpfs_cmount,
539 .vfs_mount = hpfs_mount,
540 .vfs_root = hpfs_root,
541 .vfs_statfs = hpfs_statfs,
542 .vfs_unmount = hpfs_unmount,
543 .vfs_vget = hpfs_vget,
545 VFS_SET(hpfs_vfsops, hpfs, 0);