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.24 2004/12/17 00:18:22 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/nlookup.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 extern struct vnodeopv_entry_desc hpfs_vnodeop_entries[];
63 #if defined(__DragonFly__)
64 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
65 MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure");
68 static int hpfs_root (struct mount *, struct vnode **);
69 static int hpfs_statfs (struct mount *, struct statfs *,
71 static int hpfs_unmount (struct mount *, int, struct thread *);
72 static int hpfs_vget (struct mount *mp, ino_t ino,
74 static int hpfs_mountfs (struct vnode *, struct mount *,
75 struct hpfs_args *, struct thread *);
76 static int hpfs_vptofh (struct vnode *, struct fid *);
77 static int hpfs_fhtovp (struct mount *, struct fid *,
80 #if !defined(__DragonFly__)
81 static int hpfs_quotactl (struct mount *, int, uid_t, caddr_t,
83 static int hpfs_start (struct mount *, int, struct proc *);
84 static int hpfs_sync (struct mount *, int, struct ucred *,
88 #if defined(__DragonFly__)
90 static int hpfs_mount (struct mount *, char *, caddr_t, struct thread *);
91 static int hpfs_init (struct vfsconf *);
92 static int hpfs_checkexp (struct mount *, struct sockaddr *,
93 int *, struct ucred **);
94 #else /* defined(__NetBSD__) */
95 static int hpfs_mount (struct mount *, const char *, void *,
96 struct nlookupdata *, struct proc *);
97 static void hpfs_init (void);
98 static int hpfs_mountroot (void);
99 static int hpfs_sysctl (int *, u_int, void *, size_t *, void *,
100 size_t, struct proc *);
101 static int hpfs_checkexp (struct mount *, struct mbuf *,
102 int *, struct ucred **);
107 hpfs_checkexp(struct mount *mp,
108 #if defined(__DragonFly__)
109 struct sockaddr *nam,
110 #else /* defined(__NetBSD__) */
113 int *exflagsp, struct ucred **credanonp)
116 struct hpfsmount *hpm = VFSTOHPFS(mp);
119 * Get the export permission structure for this <mp, client> tuple.
121 np = vfs_export_lookup(mp, &hpm->hpm_export, nam);
125 *exflagsp = np->netc_exflags;
126 *credanonp = &np->netc_anon;
130 #if !defined(__DragonFly__)
133 hpfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
134 size_t newlen, struct thread *td)
146 #if defined(__DragonFly__)
148 hpfs_init(struct vfsconf *vcp)
149 #else /* defined(__NetBSD__) */
154 dprintf(("hpfs_init():\n"));
157 #if defined(__DragonFly__)
163 hpfs_mount(struct mount *mp,
164 #if defined(__DragonFly__)
165 char *path, caddr_t data,
166 #else /* defined(__NetBSD__) */
167 const char *path, void *data,
174 struct hpfs_args args;
175 struct hpfsmount *hpmp = 0;
176 struct nlookupdata nd;
178 dprintf(("hpfs_mount():\n"));
181 * Mounting non-root file system or updating a file system
185 /* copy in user arguments*/
186 error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
188 goto error_1; /* can't get arguments*/
191 * If updating, check whether changing from read-only to
192 * read/write; if there is no device name, that's all we do.
194 if (mp->mnt_flag & MNT_UPDATE) {
195 dprintf(("hpfs_mount: MNT_UPDATE: "));
197 hpmp = VFSTOHPFS(mp);
199 if (args.fspec == 0) {
200 dprintf(("export 0x%x\n",args.export.ex_flags));
201 error = vfs_export(mp, &hpmp->hpm_export, &args.export);
203 printf("hpfs_mount: vfs_export failed %d\n",
208 dprintf(("name [FAILED]\n"));
216 * Not an update, or updating the name: look up the name
217 * and verify that it refers to a sensible block device.
220 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
222 error = nlookup(&nd);
224 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
229 #if defined(__DragonFly__)
230 if (!vn_isdisk(devvp, &error))
232 #else /* defined(__NetBSD__) */
233 if (devvp->v_type != VBLK) {
237 if (umajor(devvp->v_udev) >= nblkdev) {
250 * Since this is a new mount, we want the names for
251 * the device and the mount point copied in. If an
252 * error occurs, the mountpoint is discarded by the
255 /* Save "last mounted on" info for mount point (NULL pad)*/
256 copyinstr( path, /* mount point*/
257 mp->mnt_stat.f_mntonname, /* save area*/
258 MNAMELEN - 1, /* max size*/
259 &size); /* real size*/
260 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
262 /* Save "mounted from" info for mount point (NULL pad)*/
263 copyinstr( args.fspec, /* device name*/
264 mp->mnt_stat.f_mntfromname, /* save area*/
265 MNAMELEN - 1, /* max size*/
266 &size); /* real size*/
267 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
269 error = hpfs_mountfs(devvp, mp, &args, td);
274 * Initialize FS stat information in mount struct; uses both
275 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
277 * This code is common to root and non-root mounts
279 VFS_STATFS(mp, &mp->mnt_stat, td);
282 error_2: /* error with devvp held*/
284 /* release devvp before failing*/
287 error_1: /* no state to back out*/
294 * Common code for mount and mountroot
297 hpfs_mountfs(struct vnode *devvp, struct mount *mp, struct hpfs_args *argsp,
300 int error, ncount, ronly;
303 struct hpfsmount *hpmp;
304 struct buf *bp = NULL;
308 dprintf(("hpfs_mountfs():\n"));
310 * Disallow multiple mounts of the same device.
311 * Disallow mounting of a device that is currently in use
312 * (except for root, which might share swap device for miniroot).
313 * Flush out any old buffers remaining from a previous use.
315 error = vfs_mountedon(devvp);
318 ncount = count_udev(devvp->v_udev);
319 #if defined(__DragonFly__)
326 #if defined(__DragonFly__)
327 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
328 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
329 VOP__UNLOCK(devvp, 0, td);
331 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
336 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
337 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
338 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
339 VOP__UNLOCK(devvp, 0, td);
347 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK);
348 bzero(hpmp, sizeof(struct hpfsmount));
350 /* Read in SuperBlock */
351 error = bread(devvp, SUBLOCK, SUSIZE, &bp);
354 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
355 brelse(bp); bp = NULL;
357 /* Read in SpareBlock */
358 error = bread(devvp, SPBLOCK, SPSIZE, &bp);
361 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
362 brelse(bp); bp = NULL;
368 if (sup->su_magic != SU_MAGIC) {
369 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
373 if (spp->sp_magic != SP_MAGIC) {
374 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
379 mp->mnt_data = (qaddr_t)hpmp;
380 hpmp->hpm_devvp = devvp;
383 hpmp->hpm_uid = argsp->uid;
384 hpmp->hpm_gid = argsp->gid;
385 hpmp->hpm_mode = argsp->mode;
387 error = hpfs_bminit(hpmp);
391 error = hpfs_cpinit(hpmp, argsp);
396 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, hpfs_vnodeop_entries);
398 error = hpfs_root(mp, &vp);
407 #if defined(__DragonFly__)
408 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
409 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
411 mp->mnt_stat.f_fsid.val[0] = (long)dev;
412 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_HPFS);
414 mp->mnt_maxsymlinklen = 0;
415 mp->mnt_flag |= MNT_LOCAL;
416 dev->si_mountpoint = mp;
422 mp->mnt_data = (qaddr_t)NULL;
423 #if defined(__DragonFly__)
424 dev->si_mountpoint = NULL;
426 devvp->v_specflags &= ~SI_MOUNTEDON;
428 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
432 #if !defined(__DragonFly__)
434 hpfs_start(struct mount *mp, int flags, struct thread *td)
441 hpfs_unmount(struct mount *mp, int mntflags, struct thread *td)
443 int error, flags, ronly;
444 struct hpfsmount *hpmp = VFSTOHPFS(mp);
446 dprintf(("hpfs_unmount():\n"));
448 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
451 if(mntflags & MNT_FORCE)
454 dprintf(("hpfs_unmount: vflushing...\n"));
456 error = vflush(mp, 0, flags);
458 printf("hpfs_unmount: vflush failed: %d\n",error);
462 #if defined(__DragonFly__)
463 hpmp->hpm_devvp->v_rdev->si_mountpoint = NULL;
465 hpmp->hpm_devvp->v_specflags &= ~SI_MOUNTEDON;
468 vinvalbuf(hpmp->hpm_devvp, V_SAVE, td, 0, 0);
469 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
471 vrele(hpmp->hpm_devvp);
473 dprintf(("hpfs_umount: freeing memory...\n"));
476 mp->mnt_data = (qaddr_t)0;
477 mp->mnt_flag &= ~MNT_LOCAL;
478 FREE(hpmp, M_HPFSMNT);
484 hpfs_root(struct mount *mp, struct vnode **vpp)
487 struct hpfsmount *hpmp = VFSTOHPFS(mp);
489 dprintf(("hpfs_root():\n"));
490 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp);
492 printf("hpfs_root: VFS_VGET failed: %d\n",error);
500 hpfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
502 struct hpfsmount *hpmp = VFSTOHPFS(mp);
504 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
505 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
507 #if defined(__DragonFly__)
508 sbp->f_type = mp->mnt_vfc->vfc_typenum;
509 #else /* defined(__NetBSD__) */
512 sbp->f_bsize = DEV_BSIZE;
513 sbp->f_iosize = DEV_BSIZE;
514 sbp->f_blocks = hpmp->hpm_su.su_btotal;
515 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
518 if (sbp != &mp->mnt_stat) {
519 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
520 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
521 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
522 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
524 sbp->f_flags = mp->mnt_flag;
529 #if !defined(__DragonFly__)
531 hpfs_sync(struct mount *mp, int waitfor, struct ucred *cred,
538 hpfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
541 printf("hpfs_quotactl():\n");
548 hpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
551 struct hpfid *hpfhp = (struct hpfid *)fhp;
554 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) {
558 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
559 * with HPFS, we don't need to check anything else for now */
566 hpfs_vptofh(struct vnode *vp, struct fid *fhp)
568 struct hpfsnode *hpp;
572 hpfhp = (struct hpfid *)fhp;
573 hpfhp->hpfid_len = sizeof(struct hpfid);
574 hpfhp->hpfid_ino = hpp->h_no;
575 /* hpfhp->hpfid_gen = hpp->h_gen; */
580 hpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
582 struct hpfsmount *hpmp = VFSTOHPFS(mp);
586 struct thread *td = curthread; /* XXX */
589 dprintf(("hpfs_vget(0x%x): ",ino));
595 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, td)) != NULL) {
596 dprintf(("hashed\n"));
601 * We have to lock node creation for a while,
602 * but then we have to call getnewvnode(),
603 * this may cause hpfs_reclaim() to be called,
604 * this may need to VOP_VGET() parent dir for
605 * update reasons, and if parent is not in
606 * hash, we have to lock node creation...
607 * To solve this, we MALLOC, getnewvnode and init while
608 * not locked (probability of node appearence
609 * at that time is little, and anyway - we'll
612 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
615 error = getnewvnode(VT_HPFS, hpmp->hpm_mp, &vp, VLKTIMEOUT, 0);
617 printf("hpfs_vget: can't get new vnode\n");
622 dprintf(("prenew "));
626 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
629 lwkt_token_init(&hp->h_interlock);
631 hp->h_flag = H_INVAL;
635 hp->h_dev = hpmp->hpm_dev;
636 hp->h_uid = hpmp->hpm_uid;
637 hp->h_gid = hpmp->hpm_uid;
638 hp->h_mode = hpmp->hpm_mode;
639 hp->h_devvp = hpmp->hpm_devvp;
643 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, td)) != NULL) {
644 dprintf(("hashed2\n"));
648 } while(LOCKMGR(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
652 LOCKMGR(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
654 error = bread(hpmp->hpm_devvp, ino, FNODESIZE, &bp);
656 printf("hpfs_vget: can't read ino %d\n",ino);
660 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
663 if (hp->h_fn.fn_magic != FN_MAGIC) {
664 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
669 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
670 hp->h_flag &= ~H_INVAL;
672 /* Return the locked and refd vnode */
678 #if defined(__DragonFly__)
679 static struct vfsops hpfs_vfsops = {
695 VFS_SET(hpfs_vfsops, hpfs, 0);
696 #else /* defined(__NetBSD__) */
697 extern struct vnodeopv_desc hpfs_vnodeop_opv_desc;
699 struct vnodeopv_desc *hpfs_vnodeopv_descs[] = {
700 &hpfs_vnodeop_opv_desc,
704 struct vfsops hpfs_vfsops = {