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
26 * $FreeBSD: src/sys/fs/hpfs/hpfs_vfsops.c,v 1.3.2.2 2001/12/25 01:44:45 dillon Exp $
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>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
43 #include <vm/vm_param.h>
44 #if defined(__NetBSD__)
45 #include <vm/vm_prot.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_extern.h>
51 #if defined(__NetBSD__)
52 #include <miscfs/specfs/specdev.h>
55 #include <fs/hpfs/hpfs.h>
56 #include <fs/hpfs/hpfsmount.h>
57 #include <fs/hpfs/hpfs_subr.h>
59 #if defined(__FreeBSD__)
60 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
61 MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure");
64 static int hpfs_root __P((struct mount *, struct vnode **));
65 static int hpfs_statfs __P((struct mount *, struct statfs *,
67 static int hpfs_unmount __P((struct mount *, int, struct proc *));
68 static int hpfs_vget __P((struct mount *mp, ino_t ino,
70 static int hpfs_mountfs __P((register struct vnode *, struct mount *,
71 struct hpfs_args *, struct proc *));
72 static int hpfs_vptofh __P((struct vnode *, struct fid *));
73 static int hpfs_fhtovp __P((struct mount *, struct fid *,
76 #if !defined(__FreeBSD__)
77 static int hpfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
79 static int hpfs_start __P((struct mount *, int, struct proc *));
80 static int hpfs_sync __P((struct mount *, int, struct ucred *,
84 #if defined(__FreeBSD__)
86 static int hpfs_mount __P((struct mount *, char *, caddr_t,
87 struct nameidata *, struct proc *));
88 static int hpfs_init __P((struct vfsconf *));
89 static int hpfs_checkexp __P((struct mount *, struct sockaddr *,
90 int *, struct ucred **));
91 #else /* defined(__NetBSD__) */
92 static int hpfs_mount __P((struct mount *, const char *, void *,
93 struct nameidata *, struct proc *));
94 static void hpfs_init __P((void));
95 static int hpfs_mountroot __P((void));
96 static int hpfs_sysctl __P((int *, u_int, void *, size_t *, void *,
97 size_t, struct proc *));
98 static int hpfs_checkexp __P((struct mount *, struct mbuf *,
99 int *, struct ucred **));
104 hpfs_checkexp(mp, nam, exflagsp, credanonp)
105 #if defined(__FreeBSD__)
106 register struct mount *mp;
107 struct sockaddr *nam;
109 struct ucred **credanonp;
110 #else /* defined(__NetBSD__) */
111 register struct mount *mp;
114 struct ucred **credanonp;
117 register struct netcred *np;
118 register struct hpfsmount *hpm = VFSTOHPFS(mp);
121 * Get the export permission structure for this <mp, client> tuple.
123 np = vfs_export_lookup(mp, &hpm->hpm_export, nam);
127 *exflagsp = np->netc_exflags;
128 *credanonp = &np->netc_anon;
132 #if !defined(__FreeBSD__)
135 hpfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
154 #if defined(__FreeBSD__)
157 struct vfsconf *vcp )
158 #else /* defined(__NetBSD__) */
163 dprintf(("hpfs_init():\n"));
166 #if defined(__FreeBSD__)
174 #if defined(__FreeBSD__)
177 #else /* defined(__NetBSD__) */
181 struct nameidata *ndp,
187 struct hpfs_args args;
188 struct hpfsmount *hpmp = 0;
190 dprintf(("hpfs_mount():\n"));
193 * Mounting non-root file system or updating a file system
197 /* copy in user arguments*/
198 err = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
200 goto error_1; /* can't get arguments*/
203 * If updating, check whether changing from read-only to
204 * read/write; if there is no device name, that's all we do.
206 if (mp->mnt_flag & MNT_UPDATE) {
207 dprintf(("hpfs_mount: MNT_UPDATE: "));
209 hpmp = VFSTOHPFS(mp);
211 if (args.fspec == 0) {
212 dprintf(("export 0x%x\n",args.export.ex_flags));
213 err = vfs_export(mp, &hpmp->hpm_export, &args.export);
215 printf("hpfs_mount: vfs_export failed %d\n",
220 dprintf(("name [FAILED]\n"));
228 * Not an update, or updating the name: look up the name
229 * and verify that it refers to a sensible block device.
231 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
234 /* can't get devvp!*/
240 #if defined(__FreeBSD__)
241 if (!vn_isdisk(devvp, &err))
243 #else /* defined(__NetBSD__) */
244 if (devvp->v_type != VBLK) {
248 if (major(devvp->v_rdev) >= nblkdev) {
261 * Since this is a new mount, we want the names for
262 * the device and the mount point copied in. If an
263 * error occurs, the mountpoint is discarded by the
266 /* Save "last mounted on" info for mount point (NULL pad)*/
267 copyinstr( path, /* mount point*/
268 mp->mnt_stat.f_mntonname, /* save area*/
269 MNAMELEN - 1, /* max size*/
270 &size); /* real size*/
271 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
273 /* Save "mounted from" info for mount point (NULL pad)*/
274 copyinstr( args.fspec, /* device name*/
275 mp->mnt_stat.f_mntfromname, /* save area*/
276 MNAMELEN - 1, /* max size*/
277 &size); /* real size*/
278 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
280 err = hpfs_mountfs(devvp, mp, &args, p);
285 * Initialize FS stat information in mount struct; uses both
286 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
288 * This code is common to root and non-root mounts
290 (void)VFS_STATFS(mp, &mp->mnt_stat, p);
295 error_2: /* error with devvp held*/
297 /* release devvp before failing*/
300 error_1: /* no state to back out*/
307 * Common code for mount and mountroot
310 hpfs_mountfs(devvp, mp, argsp, p)
311 register struct vnode *devvp;
313 struct hpfs_args *argsp;
316 int error, ncount, ronly;
319 struct hpfsmount *hpmp;
320 struct buf *bp = NULL;
322 dev_t dev = devvp->v_rdev;
324 dprintf(("hpfs_mountfs():\n"));
326 * Disallow multiple mounts of the same device.
327 * Disallow mounting of a device that is currently in use
328 * (except for root, which might share swap device for miniroot).
329 * Flush out any old buffers remaining from a previous use.
331 error = vfs_mountedon(devvp);
334 ncount = vcount(devvp);
335 #if defined(__FreeBSD__)
339 if (ncount > 1 && devvp != rootvp)
342 #if defined(__FreeBSD__)
343 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p);
344 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
345 VOP__UNLOCK(devvp, 0, p);
347 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
352 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
353 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p);
354 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
355 VOP__UNLOCK(devvp, 0, p);
362 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK);
363 bzero(hpmp, sizeof(struct hpfsmount));
365 /* Read in SuperBlock */
366 error = bread(devvp, SUBLOCK, SUSIZE, NOCRED, &bp);
369 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
370 brelse(bp); bp = NULL;
372 /* Read in SpareBlock */
373 error = bread(devvp, SPBLOCK, SPSIZE, NOCRED, &bp);
376 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
377 brelse(bp); bp = NULL;
383 if (sup->su_magic != SU_MAGIC) {
384 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
388 if (spp->sp_magic != SP_MAGIC) {
389 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
394 mp->mnt_data = (qaddr_t)hpmp;
395 hpmp->hpm_devvp = devvp;
396 hpmp->hpm_dev = devvp->v_rdev;
398 hpmp->hpm_uid = argsp->uid;
399 hpmp->hpm_gid = argsp->gid;
400 hpmp->hpm_mode = argsp->mode;
402 error = hpfs_bminit(hpmp);
406 error = hpfs_cpinit(hpmp, argsp);
412 error = hpfs_root(mp, &vp);
421 #if defined(__FreeBSD__)
422 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
423 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
425 mp->mnt_stat.f_fsid.val[0] = (long)dev;
426 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_HPFS);
428 mp->mnt_maxsymlinklen = 0;
429 mp->mnt_flag |= MNT_LOCAL;
430 devvp->v_specmountpoint = mp;
436 mp->mnt_data = (qaddr_t)NULL;
437 #if defined(__FreeBSD__)
438 devvp->v_specmountpoint = NULL;
440 devvp->v_specflags &= ~SI_MOUNTEDON;
442 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
446 #if !defined(__FreeBSD__)
463 int error, flags, ronly;
464 register struct hpfsmount *hpmp = VFSTOHPFS(mp);
466 dprintf(("hpfs_unmount():\n"));
468 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
471 if(mntflags & MNT_FORCE)
474 dprintf(("hpfs_unmount: vflushing...\n"));
476 error = vflush(mp, 0, flags);
478 printf("hpfs_unmount: vflush failed: %d\n",error);
482 #if defined(__FreeBSD__)
483 hpmp->hpm_devvp->v_specmountpoint = NULL;
485 hpmp->hpm_devvp->v_specflags &= ~SI_MOUNTEDON;
488 vinvalbuf(hpmp->hpm_devvp, V_SAVE, NOCRED, p, 0, 0);
489 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE,
492 vrele(hpmp->hpm_devvp);
494 dprintf(("hpfs_umount: freeing memory...\n"));
497 mp->mnt_data = (qaddr_t)0;
498 mp->mnt_flag &= ~MNT_LOCAL;
499 FREE(hpmp, M_HPFSMNT);
510 struct hpfsmount *hpmp = VFSTOHPFS(mp);
512 dprintf(("hpfs_root():\n"));
513 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp);
515 printf("hpfs_root: VFS_VGET failed: %d\n",error);
528 struct hpfsmount *hpmp = VFSTOHPFS(mp);
530 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
531 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
533 #if defined(__FreeBSD__)
534 sbp->f_type = mp->mnt_vfc->vfc_typenum;
535 #else /* defined(__NetBSD__) */
538 sbp->f_bsize = DEV_BSIZE;
539 sbp->f_iosize = DEV_BSIZE;
540 sbp->f_blocks = hpmp->hpm_su.su_btotal;
541 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
544 if (sbp != &mp->mnt_stat) {
545 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
546 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
547 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
548 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
550 sbp->f_flags = mp->mnt_flag;
555 #if !defined(__FreeBSD__)
574 printf("hpfs_quotactl():\n");
587 struct hpfid *hpfhp = (struct hpfid *)fhp;
590 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) {
594 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
595 * with HPFS, we don't need to check anything else for now */
606 register struct hpfsnode *hpp;
607 register struct hpfid *hpfhp;
610 hpfhp = (struct hpfid *)fhp;
611 hpfhp->hpfid_len = sizeof(struct hpfid);
612 hpfhp->hpfid_ino = hpp->h_no;
613 /* hpfhp->hpfid_gen = hpp->h_gen; */
623 struct hpfsmount *hpmp = VFSTOHPFS(mp);
627 struct proc *p = curproc; /* XXX */
630 dprintf(("hpfs_vget(0x%x): ",ino));
636 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, p)) != NULL) {
637 dprintf(("hashed\n"));
642 * We have to lock node creation for a while,
643 * but then we have to call getnewvnode(),
644 * this may cause hpfs_reclaim() to be called,
645 * this may need to VOP_VGET() parent dir for
646 * update reasons, and if parent is not in
647 * hash, we have to lock node creation...
648 * To solve this, we MALLOC, getnewvnode and init while
649 * not locked (probability of node appearence
650 * at that time is little, and anyway - we'll
653 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
656 error = getnewvnode(VT_HPFS, hpmp->hpm_mp, hpfs_vnodeop_p, &vp);
658 printf("hpfs_vget: can't get new vnode\n");
663 dprintf(("prenew "));
667 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
670 simple_lock_init(&hp->h_interlock);
671 lockinit(&hp->h_lock, PINOD, "hpnode", VLKTIMEOUT, 0);
673 hp->h_flag = H_INVAL;
677 hp->h_dev = hpmp->hpm_dev;
678 hp->h_uid = hpmp->hpm_uid;
679 hp->h_gid = hpmp->hpm_uid;
680 hp->h_mode = hpmp->hpm_mode;
681 hp->h_devvp = hpmp->hpm_devvp;
684 error = VN_LOCK(vp, LK_EXCLUSIVE, p);
691 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, p)) != NULL) {
692 dprintf(("hashed2\n"));
696 } while(LOCKMGR(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
700 LOCKMGR(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
702 error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp);
704 printf("hpfs_vget: can't read ino %d\n",ino);
708 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
711 if (hp->h_fn.fn_magic != FN_MAGIC) {
712 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
717 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
718 hp->h_flag &= ~H_INVAL;
725 #if defined(__FreeBSD__)
726 static struct vfsops hpfs_vfsops = {
742 VFS_SET(hpfs_vfsops, hpfs, 0);
743 #else /* defined(__NetBSD__) */
744 extern struct vnodeopv_desc hpfs_vnodeop_opv_desc;
746 struct vnodeopv_desc *hpfs_vnodeopv_descs[] = {
747 &hpfs_vnodeop_opv_desc,
751 struct vfsops hpfs_vfsops = {