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>
44 #include <vm/vm_param.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_extern.h>
49 #include <fs/hpfs/hpfs.h>
50 #include <fs/hpfs/hpfsmount.h>
51 #include <fs/hpfs/hpfs_subr.h>
53 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
54 MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure");
58 static int hpfs_mountfs(register struct vnode *, struct mount *,
59 struct hpfs_args *, struct thread *);
61 static vfs_init_t hpfs_init;
62 static vfs_uninit_t hpfs_uninit;
63 static vfs_fhtovp_t hpfs_fhtovp;
64 static vfs_vget_t hpfs_vget;
65 static vfs_omount_t hpfs_omount;
66 static vfs_root_t hpfs_root;
67 static vfs_statfs_t hpfs_statfs;
68 static vfs_unmount_t hpfs_unmount;
69 static vfs_vptofh_t hpfs_vptofh;
75 dprintf(("hpfs_init():\n"));
99 struct hpfs_args args;
100 struct hpfsmount *hpmp = 0;
101 struct nameidata ndp;
103 dprintf(("hpfs_omount():\n"));
106 * Mounting non-root filesystem or updating a filesystem
110 /* copy in user arguments*/
111 err = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
113 goto error_1; /* can't get arguments*/
116 * If updating, check whether changing from read-only to
117 * read/write; if there is no device name, that's all we do.
119 if (mp->mnt_flag & MNT_UPDATE) {
120 dprintf(("hpfs_omount: MNT_UPDATE: "));
122 hpmp = VFSTOHPFS(mp);
124 if (args.fspec == 0) {
125 dprintf(("export 0x%x\n",args.export.ex_flags));
126 err = vfs_export(mp, &args.export);
128 printf("hpfs_omount: vfs_export failed %d\n",
133 dprintf(("name [FAILED]\n"));
141 * Not an update, or updating the name: look up the name
142 * and verify that it refers to a sensible block device.
144 NDINIT(&ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
147 /* can't get devvp!*/
153 if (!vn_isdisk(devvp, &err))
163 * Since this is a new mount, we want the names for
164 * the device and the mount point copied in. If an
165 * error occurs, the mountpoint is discarded by the
166 * upper level code. Note that vfs_omount() handles
167 * copying the mountpoint f_mntonname for us, so we
168 * don't have to do it here unless we want to set it
169 * to something other than "path" for some rason.
171 /* Save "mounted from" info for mount point (NULL pad)*/
172 copyinstr( args.fspec, /* device name*/
173 mp->mnt_stat.f_mntfromname, /* save area*/
174 MNAMELEN - 1, /* max size*/
175 &size); /* real size*/
176 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
178 err = hpfs_mountfs(devvp, mp, &args, td);
183 * Initialize FS stat information in mount struct; uses both
184 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
186 * This code is common to root and non-root mounts
188 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
193 error_2: /* error with devvp held*/
195 /* release devvp before failing*/
198 error_1: /* no state to back out*/
199 /* XXX: Missing NDFREE(&ndp, ...) */
206 * Common code for mount and mountroot
209 hpfs_mountfs(devvp, mp, argsp, td)
210 register struct vnode *devvp;
212 struct hpfs_args *argsp;
215 int error, ncount, ronly;
218 struct hpfsmount *hpmp;
219 struct buf *bp = NULL;
221 struct cdev *dev = devvp->v_rdev;
223 dprintf(("hpfs_mountfs():\n"));
225 * Disallow multiple mounts of the same device.
226 * Disallow mounting of a device that is currently in use
227 * (except for root, which might share swap device for miniroot).
228 * Flush out any old buffers remaining from a previous use.
230 error = vfs_mountedon(devvp);
233 ncount = vcount(devvp);
239 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
240 error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0);
241 VOP_UNLOCK(devvp, 0, td);
245 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
246 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
247 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td, -1);
248 VOP_UNLOCK(devvp, 0, td);
255 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK | M_ZERO);
257 /* Read in SuperBlock */
258 error = bread(devvp, SUBLOCK, SUSIZE, NOCRED, &bp);
261 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
262 brelse(bp); bp = NULL;
264 /* Read in SpareBlock */
265 error = bread(devvp, SPBLOCK, SPSIZE, NOCRED, &bp);
268 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
269 brelse(bp); bp = NULL;
275 if (sup->su_magic != SU_MAGIC) {
276 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
280 if (spp->sp_magic != SP_MAGIC) {
281 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
286 mp->mnt_data = (qaddr_t)hpmp;
287 hpmp->hpm_devvp = devvp;
288 hpmp->hpm_dev = devvp->v_rdev;
290 hpmp->hpm_uid = argsp->uid;
291 hpmp->hpm_gid = argsp->gid;
292 hpmp->hpm_mode = argsp->mode;
294 error = hpfs_bminit(hpmp);
298 error = hpfs_cpinit(hpmp, argsp);
304 error = hpfs_root(mp, &vp, td);
313 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
314 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
315 mp->mnt_maxsymlinklen = 0;
316 mp->mnt_flag |= MNT_LOCAL;
317 devvp->v_rdev->si_mountpoint = mp;
323 mp->mnt_data = (qaddr_t)NULL;
324 devvp->v_rdev->si_mountpoint = NULL;
325 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, td);
335 int error, flags, ronly;
336 register struct hpfsmount *hpmp = VFSTOHPFS(mp);
338 dprintf(("hpfs_unmount():\n"));
340 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
343 if(mntflags & MNT_FORCE)
346 dprintf(("hpfs_unmount: vflushing...\n"));
348 error = vflush(mp, 0, flags, td);
350 printf("hpfs_unmount: vflush failed: %d\n",error);
354 hpmp->hpm_devvp->v_rdev->si_mountpoint = NULL;
356 vinvalbuf(hpmp->hpm_devvp, V_SAVE, NOCRED, td, 0, 0);
357 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE,
360 vrele(hpmp->hpm_devvp);
362 dprintf(("hpfs_umount: freeing memory...\n"));
365 mp->mnt_data = (qaddr_t)0;
366 mp->mnt_flag &= ~MNT_LOCAL;
367 FREE(hpmp, M_HPFSMNT);
379 struct hpfsmount *hpmp = VFSTOHPFS(mp);
381 dprintf(("hpfs_root():\n"));
382 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, LK_EXCLUSIVE, vpp);
384 printf("hpfs_root: VFS_VGET failed: %d\n",error);
397 struct hpfsmount *hpmp = VFSTOHPFS(mp);
399 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
400 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
402 sbp->f_type = mp->mnt_vfc->vfc_typenum;
403 sbp->f_bsize = DEV_BSIZE;
404 sbp->f_iosize = DEV_BSIZE;
405 sbp->f_blocks = hpmp->hpm_su.su_btotal;
406 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
409 if (sbp != &mp->mnt_stat) {
410 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
411 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
412 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
413 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
415 sbp->f_flags = mp->mnt_flag;
428 struct hpfid *hpfhp = (struct hpfid *)fhp;
431 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
435 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
436 * with HPFS, we don't need to check anything else for now */
447 register struct hpfsnode *hpp;
448 register struct hpfid *hpfhp;
451 hpfhp = (struct hpfid *)fhp;
452 hpfhp->hpfid_len = sizeof(struct hpfid);
453 hpfhp->hpfid_ino = hpp->h_no;
454 /* hpfhp->hpfid_gen = hpp->h_gen; */
465 struct hpfsmount *hpmp = VFSTOHPFS(mp);
469 struct thread *td = curthread; /* XXX */
472 dprintf(("hpfs_vget(0x%x): ",ino));
478 if ((error = hpfs_hphashvget(hpmp->hpm_dev, ino, flags, vpp, td)) != 0)
481 dprintf(("hashed\n"));
486 * We have to lock node creation for a while,
487 * but then we have to call getnewvnode(),
488 * this may cause hpfs_reclaim() to be called,
489 * this may need to VOP_VGET() parent dir for
490 * update reasons, and if parent is not in
491 * hash, we have to lock node creation...
492 * To solve this, we MALLOC, getnewvnode and init while
493 * not locked (probability of node appearence
494 * at that time is little, and anyway - we'll
497 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
500 error = getnewvnode("hpfs", hpmp->hpm_mp, hpfs_vnodeop_p, &vp);
502 printf("hpfs_vget: can't get new vnode\n");
507 dprintf(("prenew "));
511 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
512 vp->v_vflag |= VV_ROOT;
515 mtx_init(&hp->h_interlock, "hpfsnode interlock", NULL, MTX_DEF);
517 hp->h_flag = H_INVAL;
521 hp->h_dev = hpmp->hpm_dev;
522 hp->h_uid = hpmp->hpm_uid;
523 hp->h_gid = hpmp->hpm_uid;
524 hp->h_mode = hpmp->hpm_mode;
525 hp->h_devvp = hpmp->hpm_devvp;
528 error = vn_lock(vp, LK_EXCLUSIVE, td);
536 hpfs_hphashvget(hpmp->hpm_dev, ino, flags, vpp, td))) {
541 dprintf(("hashed2\n"));
545 } while(lockmgr(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
549 lockmgr(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
551 error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp);
553 printf("hpfs_vget: can't read ino %d\n",ino);
557 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
560 if (hp->h_fn.fn_magic != FN_MAGIC) {
561 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
566 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
567 hp->h_flag &= ~H_INVAL;
574 static struct vfsops hpfs_vfsops = {
575 .vfs_fhtovp = hpfs_fhtovp,
576 .vfs_init = hpfs_init,
577 .vfs_omount = hpfs_omount,
578 .vfs_root = hpfs_root,
579 .vfs_statfs = hpfs_statfs,
580 .vfs_uninit = hpfs_uninit,
581 .vfs_unmount = hpfs_unmount,
582 .vfs_vget = hpfs_vget,
583 .vfs_vptofh = hpfs_vptofh,
585 VFS_SET(hpfs_vfsops, hpfs, 0);