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 $
27 * $DragonFly: src/sys/vfs/hpfs/hpfs_vfsops.c,v 1.10 2003/09/23 05:03:52 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/namei.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
44 #include <vm/vm_param.h>
45 #if defined(__NetBSD__)
46 #include <vm/vm_prot.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_object.h>
50 #include <vm/vm_extern.h>
53 #if defined(__NetBSD__)
54 #include <miscfs/specfs/specdev.h>
58 #include "hpfsmount.h"
59 #include "hpfs_subr.h"
61 #if defined(__FreeBSD__)
62 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
63 MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure");
66 static int hpfs_root (struct mount *, struct vnode **);
67 static int hpfs_statfs (struct mount *, struct statfs *,
69 static int hpfs_unmount (struct mount *, int, struct thread *);
70 static int hpfs_vget (struct mount *mp, ino_t ino,
72 static int hpfs_mountfs (struct vnode *, struct mount *,
73 struct hpfs_args *, struct thread *);
74 static int hpfs_vptofh (struct vnode *, struct fid *);
75 static int hpfs_fhtovp (struct mount *, struct fid *,
78 #if !defined(__FreeBSD__)
79 static int hpfs_quotactl (struct mount *, int, uid_t, caddr_t,
81 static int hpfs_start (struct mount *, int, struct proc *);
82 static int hpfs_sync (struct mount *, int, struct ucred *,
86 #if defined(__FreeBSD__)
88 static int hpfs_mount (struct mount *, char *, caddr_t,
89 struct nameidata *, struct thread *);
90 static int hpfs_init (struct vfsconf *);
91 static int hpfs_checkexp (struct mount *, struct sockaddr *,
92 int *, struct ucred **);
93 #else /* defined(__NetBSD__) */
94 static int hpfs_mount (struct mount *, const char *, void *,
95 struct nameidata *, struct proc *);
96 static void hpfs_init (void);
97 static int hpfs_mountroot (void);
98 static int hpfs_sysctl (int *, u_int, void *, size_t *, void *,
99 size_t, struct proc *);
100 static int hpfs_checkexp (struct mount *, struct mbuf *,
101 int *, struct ucred **);
106 hpfs_checkexp(mp, nam, exflagsp, credanonp)
107 #if defined(__FreeBSD__)
109 struct sockaddr *nam;
111 struct ucred **credanonp;
112 #else /* defined(__NetBSD__) */
116 struct ucred **credanonp;
120 struct hpfsmount *hpm = VFSTOHPFS(mp);
123 * Get the export permission structure for this <mp, client> tuple.
125 np = vfs_export_lookup(mp, &hpm->hpm_export, nam);
129 *exflagsp = np->netc_exflags;
130 *credanonp = &np->netc_anon;
134 #if !defined(__FreeBSD__)
137 hpfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, td)
156 #if defined(__FreeBSD__)
159 struct vfsconf *vcp )
160 #else /* defined(__NetBSD__) */
165 dprintf(("hpfs_init():\n"));
168 #if defined(__FreeBSD__)
176 #if defined(__FreeBSD__)
179 #else /* defined(__NetBSD__) */
183 struct nameidata *ndp,
189 struct hpfs_args args;
190 struct hpfsmount *hpmp = 0;
192 dprintf(("hpfs_mount():\n"));
195 * Mounting non-root file system or updating a file system
199 /* copy in user arguments*/
200 err = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
202 goto error_1; /* can't get arguments*/
205 * If updating, check whether changing from read-only to
206 * read/write; if there is no device name, that's all we do.
208 if (mp->mnt_flag & MNT_UPDATE) {
209 dprintf(("hpfs_mount: MNT_UPDATE: "));
211 hpmp = VFSTOHPFS(mp);
213 if (args.fspec == 0) {
214 dprintf(("export 0x%x\n",args.export.ex_flags));
215 err = vfs_export(mp, &hpmp->hpm_export, &args.export);
217 printf("hpfs_mount: vfs_export failed %d\n",
222 dprintf(("name [FAILED]\n"));
230 * Not an update, or updating the name: look up the name
231 * and verify that it refers to a sensible block device.
233 NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
236 /* can't get devvp!*/
242 #if defined(__FreeBSD__)
243 if (!vn_isdisk(devvp, &err))
245 #else /* defined(__NetBSD__) */
246 if (devvp->v_type != VBLK) {
250 if (major(devvp->v_rdev) >= nblkdev) {
263 * Since this is a new mount, we want the names for
264 * the device and the mount point copied in. If an
265 * error occurs, the mountpoint is discarded by the
268 /* Save "last mounted on" info for mount point (NULL pad)*/
269 copyinstr( path, /* mount point*/
270 mp->mnt_stat.f_mntonname, /* save area*/
271 MNAMELEN - 1, /* max size*/
272 &size); /* real size*/
273 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
275 /* Save "mounted from" info for mount point (NULL pad)*/
276 copyinstr( args.fspec, /* device name*/
277 mp->mnt_stat.f_mntfromname, /* save area*/
278 MNAMELEN - 1, /* max size*/
279 &size); /* real size*/
280 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
282 err = hpfs_mountfs(devvp, mp, &args, td);
287 * Initialize FS stat information in mount struct; uses both
288 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
290 * This code is common to root and non-root mounts
292 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
297 error_2: /* error with devvp held*/
299 /* release devvp before failing*/
302 error_1: /* no state to back out*/
309 * Common code for mount and mountroot
312 hpfs_mountfs(devvp, mp, argsp, td)
315 struct hpfs_args *argsp;
318 int error, ncount, ronly;
321 struct hpfsmount *hpmp;
322 struct buf *bp = NULL;
324 dev_t dev = devvp->v_rdev;
326 dprintf(("hpfs_mountfs():\n"));
328 * Disallow multiple mounts of the same device.
329 * Disallow mounting of a device that is currently in use
330 * (except for root, which might share swap device for miniroot).
331 * Flush out any old buffers remaining from a previous use.
333 error = vfs_mountedon(devvp);
336 ncount = vcount(devvp);
337 #if defined(__FreeBSD__)
341 if (ncount > 1 && devvp != rootvp)
344 #if defined(__FreeBSD__)
345 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
346 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
347 VOP__UNLOCK(devvp, 0, td);
349 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
354 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
355 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
356 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
357 VOP__UNLOCK(devvp, 0, td);
364 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK);
365 bzero(hpmp, sizeof(struct hpfsmount));
367 /* Read in SuperBlock */
368 error = bread(devvp, SUBLOCK, SUSIZE, &bp);
371 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
372 brelse(bp); bp = NULL;
374 /* Read in SpareBlock */
375 error = bread(devvp, SPBLOCK, SPSIZE, &bp);
378 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
379 brelse(bp); bp = NULL;
385 if (sup->su_magic != SU_MAGIC) {
386 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
390 if (spp->sp_magic != SP_MAGIC) {
391 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
396 mp->mnt_data = (qaddr_t)hpmp;
397 hpmp->hpm_devvp = devvp;
398 hpmp->hpm_dev = devvp->v_rdev;
400 hpmp->hpm_uid = argsp->uid;
401 hpmp->hpm_gid = argsp->gid;
402 hpmp->hpm_mode = argsp->mode;
404 error = hpfs_bminit(hpmp);
408 error = hpfs_cpinit(hpmp, argsp);
414 error = hpfs_root(mp, &vp);
423 #if defined(__FreeBSD__)
424 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
425 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
427 mp->mnt_stat.f_fsid.val[0] = (long)dev;
428 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_HPFS);
430 mp->mnt_maxsymlinklen = 0;
431 mp->mnt_flag |= MNT_LOCAL;
432 devvp->v_specmountpoint = mp;
438 mp->mnt_data = (qaddr_t)NULL;
439 #if defined(__FreeBSD__)
440 devvp->v_specmountpoint = NULL;
442 devvp->v_specflags &= ~SI_MOUNTEDON;
444 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
448 #if !defined(__FreeBSD__)
465 int error, flags, ronly;
466 struct hpfsmount *hpmp = VFSTOHPFS(mp);
468 dprintf(("hpfs_unmount():\n"));
470 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
473 if(mntflags & MNT_FORCE)
476 dprintf(("hpfs_unmount: vflushing...\n"));
478 error = vflush(mp, 0, flags);
480 printf("hpfs_unmount: vflush failed: %d\n",error);
484 #if defined(__FreeBSD__)
485 hpmp->hpm_devvp->v_specmountpoint = NULL;
487 hpmp->hpm_devvp->v_specflags &= ~SI_MOUNTEDON;
490 vinvalbuf(hpmp->hpm_devvp, V_SAVE, td, 0, 0);
491 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
493 vrele(hpmp->hpm_devvp);
495 dprintf(("hpfs_umount: freeing memory...\n"));
498 mp->mnt_data = (qaddr_t)0;
499 mp->mnt_flag &= ~MNT_LOCAL;
500 FREE(hpmp, M_HPFSMNT);
511 struct hpfsmount *hpmp = VFSTOHPFS(mp);
513 dprintf(("hpfs_root():\n"));
514 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp);
516 printf("hpfs_root: VFS_VGET failed: %d\n",error);
529 struct hpfsmount *hpmp = VFSTOHPFS(mp);
531 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
532 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
534 #if defined(__FreeBSD__)
535 sbp->f_type = mp->mnt_vfc->vfc_typenum;
536 #else /* defined(__NetBSD__) */
539 sbp->f_bsize = DEV_BSIZE;
540 sbp->f_iosize = DEV_BSIZE;
541 sbp->f_blocks = hpmp->hpm_su.su_btotal;
542 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
545 if (sbp != &mp->mnt_stat) {
546 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
547 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
548 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
549 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
551 sbp->f_flags = mp->mnt_flag;
556 #if !defined(__FreeBSD__)
575 printf("hpfs_quotactl():\n");
588 struct hpfid *hpfhp = (struct hpfid *)fhp;
591 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) {
595 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
596 * with HPFS, we don't need to check anything else for now */
607 struct hpfsnode *hpp;
611 hpfhp = (struct hpfid *)fhp;
612 hpfhp->hpfid_len = sizeof(struct hpfid);
613 hpfhp->hpfid_ino = hpp->h_no;
614 /* hpfhp->hpfid_gen = hpp->h_gen; */
624 struct hpfsmount *hpmp = VFSTOHPFS(mp);
628 struct thread *td = curthread; /* XXX */
631 dprintf(("hpfs_vget(0x%x): ",ino));
637 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, td)) != NULL) {
638 dprintf(("hashed\n"));
643 * We have to lock node creation for a while,
644 * but then we have to call getnewvnode(),
645 * this may cause hpfs_reclaim() to be called,
646 * this may need to VOP_VGET() parent dir for
647 * update reasons, and if parent is not in
648 * hash, we have to lock node creation...
649 * To solve this, we MALLOC, getnewvnode and init while
650 * not locked (probability of node appearence
651 * at that time is little, and anyway - we'll
654 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
657 error = getnewvnode(VT_HPFS, hpmp->hpm_mp, hpfs_vnodeop_p, &vp);
659 printf("hpfs_vget: can't get new vnode\n");
664 dprintf(("prenew "));
668 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
671 lwkt_inittoken(&hp->h_interlock);
672 lockinit(&hp->h_lock, 0, "hpnode", VLKTIMEOUT, 0);
674 hp->h_flag = H_INVAL;
678 hp->h_dev = hpmp->hpm_dev;
679 hp->h_uid = hpmp->hpm_uid;
680 hp->h_gid = hpmp->hpm_uid;
681 hp->h_mode = hpmp->hpm_mode;
682 hp->h_devvp = hpmp->hpm_devvp;
685 error = VN_LOCK(vp, LK_EXCLUSIVE, td);
692 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, td)) != NULL) {
693 dprintf(("hashed2\n"));
697 } while(LOCKMGR(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
701 LOCKMGR(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
703 error = bread(hpmp->hpm_devvp, ino, FNODESIZE, &bp);
705 printf("hpfs_vget: can't read ino %d\n",ino);
709 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
712 if (hp->h_fn.fn_magic != FN_MAGIC) {
713 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
718 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
719 hp->h_flag &= ~H_INVAL;
726 #if defined(__FreeBSD__)
727 static struct vfsops hpfs_vfsops = {
743 VFS_SET(hpfs_vfsops, hpfs, 0);
744 #else /* defined(__NetBSD__) */
745 extern struct vnodeopv_desc hpfs_vnodeop_opv_desc;
747 struct vnodeopv_desc *hpfs_vnodeopv_descs[] = {
748 &hpfs_vnodeop_opv_desc,
752 struct vfsops hpfs_vfsops = {