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 *,
62 struct hpfs_args *, struct thread *);
64 static vfs_init_t hpfs_init;
65 static vfs_uninit_t hpfs_uninit;
66 static vfs_fhtovp_t hpfs_fhtovp;
67 static vfs_vget_t hpfs_vget;
68 static vfs_omount_t hpfs_omount;
69 static vfs_root_t hpfs_root;
70 static vfs_statfs_t hpfs_statfs;
71 static vfs_unmount_t hpfs_unmount;
72 static vfs_vptofh_t hpfs_vptofh;
78 dprintf(("hpfs_init():\n"));
102 struct hpfs_args args;
103 struct hpfsmount *hpmp = 0;
104 struct nameidata ndp;
106 dprintf(("hpfs_omount():\n"));
109 * Mounting non-root filesystem or updating a filesystem
113 /* copy in user arguments*/
114 err = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
116 goto error_1; /* can't get arguments*/
119 * If updating, check whether changing from read-only to
120 * read/write; if there is no device name, that's all we do.
122 if (mp->mnt_flag & MNT_UPDATE) {
123 dprintf(("hpfs_omount: MNT_UPDATE: "));
125 hpmp = VFSTOHPFS(mp);
127 if (args.fspec == 0) {
128 dprintf(("export 0x%x\n",args.export.ex_flags));
129 err = vfs_export(mp, &args.export);
131 printf("hpfs_omount: vfs_export failed %d\n",
136 dprintf(("name [FAILED]\n"));
144 * Not an update, or updating the name: look up the name
145 * and verify that it refers to a sensible block device.
147 NDINIT(&ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
150 /* can't get devvp!*/
156 if (!vn_isdisk(devvp, &err))
166 * Since this is a new mount, we want the names for
167 * the device and the mount point copied in. If an
168 * error occurs, the mountpoint is discarded by the
169 * upper level code. Note that vfs_omount() handles
170 * copying the mountpoint f_mntonname for us, so we
171 * don't have to do it here unless we want to set it
172 * to something other than "path" for some rason.
174 /* Save "mounted from" info for mount point (NULL pad)*/
175 copyinstr( args.fspec, /* device name*/
176 mp->mnt_stat.f_mntfromname, /* save area*/
177 MNAMELEN - 1, /* max size*/
178 &size); /* real size*/
179 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
181 err = hpfs_mountfs(devvp, mp, &args, td);
186 * Initialize FS stat information in mount struct; uses both
187 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
189 * This code is common to root and non-root mounts
191 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
196 error_2: /* error with devvp held*/
198 /* release devvp before failing*/
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, argsp, td)
213 register struct vnode *devvp;
215 struct hpfs_args *argsp;
221 struct hpfsmount *hpmp;
222 struct buf *bp = NULL;
224 struct cdev *dev = devvp->v_rdev;
225 struct g_consumer *cp;
228 if (mp->mnt_flag & MNT_ROOTFS)
230 dprintf(("hpfs_mountfs():\n"));
231 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
232 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
233 /* XXX: use VOP_ACCESS to check FS perms */
236 error = g_vfs_open(devvp, &cp, "hpfs", ronly ? 0 : 1);
239 VOP_UNLOCK(devvp, 0, td);
243 bo = &devvp->v_bufobj;
245 bo->bo_ops = g_vfs_bufops;
250 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK | M_ZERO);
255 /* Read in SuperBlock */
256 error = bread(devvp, SUBLOCK, SUSIZE, NOCRED, &bp);
259 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
260 brelse(bp); bp = NULL;
262 /* Read in SpareBlock */
263 error = bread(devvp, SPBLOCK, SPSIZE, NOCRED, &bp);
266 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
267 brelse(bp); bp = NULL;
273 if (sup->su_magic != SU_MAGIC) {
274 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
278 if (spp->sp_magic != SP_MAGIC) {
279 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
284 mp->mnt_data = (qaddr_t)hpmp;
285 hpmp->hpm_devvp = devvp;
286 hpmp->hpm_dev = devvp->v_rdev;
288 hpmp->hpm_uid = argsp->uid;
289 hpmp->hpm_gid = argsp->gid;
290 hpmp->hpm_mode = argsp->mode;
292 error = hpfs_bminit(hpmp);
296 error = hpfs_cpinit(hpmp, argsp);
302 error = hpfs_root(mp, &vp, td);
311 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
312 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
313 mp->mnt_maxsymlinklen = 0;
314 mp->mnt_flag |= MNT_LOCAL;
320 mp->mnt_data = (qaddr_t)NULL;
321 g_wither_geom_close(cp->geom, ENXIO);
331 int error, flags, ronly;
332 register struct hpfsmount *hpmp = VFSTOHPFS(mp);
334 dprintf(("hpfs_unmount():\n"));
336 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
339 if(mntflags & MNT_FORCE)
342 dprintf(("hpfs_unmount: vflushing...\n"));
344 error = vflush(mp, 0, flags, td);
346 printf("hpfs_unmount: vflush failed: %d\n",error);
350 vinvalbuf(hpmp->hpm_devvp, V_SAVE, NOCRED, td, 0, 0);
351 g_wither_geom_close(hpmp->hpm_cp->geom, ENXIO);
352 vrele(hpmp->hpm_devvp);
354 dprintf(("hpfs_umount: freeing memory...\n"));
357 mp->mnt_data = (qaddr_t)0;
358 mp->mnt_flag &= ~MNT_LOCAL;
359 FREE(hpmp, M_HPFSMNT);
371 struct hpfsmount *hpmp = VFSTOHPFS(mp);
373 dprintf(("hpfs_root():\n"));
374 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, LK_EXCLUSIVE, vpp);
376 printf("hpfs_root: VFS_VGET failed: %d\n",error);
389 struct hpfsmount *hpmp = VFSTOHPFS(mp);
391 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
392 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
394 sbp->f_type = mp->mnt_vfc->vfc_typenum;
395 sbp->f_bsize = DEV_BSIZE;
396 sbp->f_iosize = DEV_BSIZE;
397 sbp->f_blocks = hpmp->hpm_su.su_btotal;
398 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
401 sbp->f_flags = mp->mnt_flag;
414 struct hpfid *hpfhp = (struct hpfid *)fhp;
417 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
421 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
422 * with HPFS, we don't need to check anything else for now */
433 register struct hpfsnode *hpp;
434 register struct hpfid *hpfhp;
437 hpfhp = (struct hpfid *)fhp;
438 hpfhp->hpfid_len = sizeof(struct hpfid);
439 hpfhp->hpfid_ino = hpp->h_no;
440 /* hpfhp->hpfid_gen = hpp->h_gen; */
451 struct hpfsmount *hpmp = VFSTOHPFS(mp);
455 struct thread *td = curthread; /* XXX */
458 dprintf(("hpfs_vget(0x%x): ",ino));
464 if ((error = hpfs_hphashvget(hpmp->hpm_dev, ino, flags, vpp, td)) != 0)
467 dprintf(("hashed\n"));
472 * We have to lock node creation for a while,
473 * but then we have to call getnewvnode(),
474 * this may cause hpfs_reclaim() to be called,
475 * this may need to VOP_VGET() parent dir for
476 * update reasons, and if parent is not in
477 * hash, we have to lock node creation...
478 * To solve this, we MALLOC, getnewvnode and init while
479 * not locked (probability of node appearence
480 * at that time is little, and anyway - we'll
483 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
486 error = getnewvnode("hpfs", hpmp->hpm_mp, &hpfs_vnodeops, &vp);
488 printf("hpfs_vget: can't get new vnode\n");
493 dprintf(("prenew "));
497 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
498 vp->v_vflag |= VV_ROOT;
501 mtx_init(&hp->h_interlock, "hpfsnode interlock", NULL, MTX_DEF);
503 hp->h_flag = H_INVAL;
507 hp->h_dev = hpmp->hpm_dev;
508 hp->h_uid = hpmp->hpm_uid;
509 hp->h_gid = hpmp->hpm_uid;
510 hp->h_mode = hpmp->hpm_mode;
511 hp->h_devvp = hpmp->hpm_devvp;
514 error = vn_lock(vp, LK_EXCLUSIVE, td);
522 hpfs_hphashvget(hpmp->hpm_dev, ino, flags, vpp, td))) {
527 dprintf(("hashed2\n"));
531 } while(lockmgr(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
535 lockmgr(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
537 error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp);
539 printf("hpfs_vget: can't read ino %d\n",ino);
543 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
546 if (hp->h_fn.fn_magic != FN_MAGIC) {
547 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
552 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
553 hp->h_flag &= ~H_INVAL;
560 static struct vfsops hpfs_vfsops = {
561 .vfs_fhtovp = hpfs_fhtovp,
562 .vfs_init = hpfs_init,
563 .vfs_omount = hpfs_omount,
564 .vfs_root = hpfs_root,
565 .vfs_statfs = hpfs_statfs,
566 .vfs_uninit = hpfs_uninit,
567 .vfs_unmount = hpfs_unmount,
568 .vfs_vget = hpfs_vget,
569 .vfs_vptofh = hpfs_vptofh,
571 VFS_SET(hpfs_vfsops, hpfs, 0);