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.32 2006/03/24 18:35:33 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>
43 #include <machine/inttypes.h>
46 #include <vm/vm_param.h>
47 #if defined(__NetBSD__)
48 #include <vm/vm_prot.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_extern.h>
55 #if defined(__NetBSD__)
56 #include <miscfs/specfs/specdev.h>
60 #include "hpfsmount.h"
61 #include "hpfs_subr.h"
63 extern struct vnodeopv_entry_desc hpfs_vnodeop_entries[];
65 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
66 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 *,
82 static int hpfs_mount (struct mount *, char *, caddr_t, struct thread *);
83 static int hpfs_init (struct vfsconf *);
84 static int hpfs_checkexp (struct mount *, struct sockaddr *,
85 int *, struct ucred **);
89 hpfs_checkexp(struct mount *mp,
91 int *exflagsp, struct ucred **credanonp)
94 struct hpfsmount *hpm = VFSTOHPFS(mp);
97 * Get the export permission structure for this <mp, client> tuple.
99 np = vfs_export_lookup(mp, &hpm->hpm_export, nam);
103 *exflagsp = np->netc_exflags;
104 *credanonp = &np->netc_anon;
109 hpfs_init(struct vfsconf *vcp)
111 dprintf(("hpfs_init():\n"));
118 hpfs_mount(struct mount *mp,
119 char *path, caddr_t data,
125 struct hpfs_args args;
126 struct hpfsmount *hpmp = 0;
127 struct nlookupdata nd;
129 dprintf(("hpfs_mount():\n"));
132 * Mounting non-root file system or updating a file system
136 /* copy in user arguments*/
137 error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
139 goto error_1; /* can't get arguments*/
142 * If updating, check whether changing from read-only to
143 * read/write; if there is no device name, that's all we do.
145 if (mp->mnt_flag & MNT_UPDATE) {
146 dprintf(("hpfs_mount: MNT_UPDATE: "));
148 hpmp = VFSTOHPFS(mp);
150 if (args.fspec == 0) {
151 dprintf(("export 0x%x\n",args.export.ex_flags));
152 error = vfs_export(mp, &hpmp->hpm_export, &args.export);
154 printf("hpfs_mount: vfs_export failed %d\n",
159 dprintf(("name [FAILED]\n"));
167 * Not an update, or updating the name: look up the name
168 * and verify that it refers to a sensible block device.
171 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
173 error = nlookup(&nd);
175 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
180 if (!vn_isdisk(devvp, &error))
189 /* Save "mounted from" info for mount point (NULL pad)*/
190 copyinstr( args.fspec, /* device name*/
191 mp->mnt_stat.f_mntfromname, /* save area*/
192 MNAMELEN - 1, /* max size*/
193 &size); /* real size*/
194 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
196 error = hpfs_mountfs(devvp, mp, &args, td);
201 * Initialize FS stat information in mount struct; uses
202 * mp->mnt_stat.f_mntfromname.
204 * This code is common to root and non-root mounts
206 VFS_STATFS(mp, &mp->mnt_stat, td);
209 error_2: /* error with devvp held*/
211 /* release devvp before failing*/
214 error_1: /* no state to back out*/
221 * Common code for mount and mountroot
224 hpfs_mountfs(struct vnode *devvp, struct mount *mp, struct hpfs_args *argsp,
227 int error, ncount, ronly;
230 struct hpfsmount *hpmp;
231 struct buf *bp = NULL;
235 dprintf(("hpfs_mountfs():\n"));
237 * Disallow multiple mounts of the same device.
238 * Disallow mounting of a device that is currently in use
239 * (except for root, which might share swap device for miniroot).
240 * Flush out any old buffers remaining from a previous use.
242 error = vfs_mountedon(devvp);
245 ncount = count_udev(devvp->v_udev);
251 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
252 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
253 VOP__UNLOCK(devvp, 0, td);
257 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
258 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
259 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
260 VOP__UNLOCK(devvp, 0, td);
268 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK);
269 bzero(hpmp, sizeof(struct hpfsmount));
271 /* Read in SuperBlock */
272 error = bread(devvp, dbtodoff(SUBLOCK), SUSIZE, &bp);
275 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
276 brelse(bp); bp = NULL;
278 /* Read in SpareBlock */
279 error = bread(devvp, dbtodoff(SPBLOCK), SPSIZE, &bp);
282 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
283 brelse(bp); bp = NULL;
289 if (sup->su_magic != SU_MAGIC) {
290 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
294 if (spp->sp_magic != SP_MAGIC) {
295 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
300 mp->mnt_data = (qaddr_t)hpmp;
301 hpmp->hpm_devvp = devvp;
304 hpmp->hpm_uid = argsp->uid;
305 hpmp->hpm_gid = argsp->gid;
306 hpmp->hpm_mode = argsp->mode;
308 error = hpfs_bminit(hpmp);
312 error = hpfs_cpinit(hpmp, argsp);
317 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops,
318 hpfs_vnodeop_entries, 0);
320 error = hpfs_root(mp, &vp);
329 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
330 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
331 mp->mnt_maxsymlinklen = 0;
332 mp->mnt_flag |= MNT_LOCAL;
333 dev->si_mountpoint = mp;
339 mp->mnt_data = (qaddr_t)NULL;
340 dev->si_mountpoint = NULL;
341 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
347 hpfs_unmount(struct mount *mp, int mntflags, struct thread *td)
349 int error, flags, ronly;
350 struct hpfsmount *hpmp = VFSTOHPFS(mp);
352 dprintf(("hpfs_unmount():\n"));
354 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
357 if(mntflags & MNT_FORCE)
360 dprintf(("hpfs_unmount: vflushing...\n"));
362 error = vflush(mp, 0, flags);
364 printf("hpfs_unmount: vflush failed: %d\n",error);
368 hpmp->hpm_devvp->v_rdev->si_mountpoint = NULL;
370 vinvalbuf(hpmp->hpm_devvp, V_SAVE, td, 0, 0);
371 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
373 vrele(hpmp->hpm_devvp);
375 dprintf(("hpfs_umount: freeing memory...\n"));
378 mp->mnt_data = (qaddr_t)0;
379 mp->mnt_flag &= ~MNT_LOCAL;
380 FREE(hpmp, M_HPFSMNT);
386 hpfs_root(struct mount *mp, struct vnode **vpp)
389 struct hpfsmount *hpmp = VFSTOHPFS(mp);
391 dprintf(("hpfs_root():\n"));
392 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp);
394 printf("hpfs_root: VFS_VGET failed: %d\n",error);
402 hpfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
404 struct hpfsmount *hpmp = VFSTOHPFS(mp);
406 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
407 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
409 sbp->f_type = mp->mnt_vfc->vfc_typenum;
410 sbp->f_bsize = DEV_BSIZE;
411 sbp->f_iosize = DEV_BSIZE;
412 sbp->f_blocks = hpmp->hpm_su.su_btotal;
413 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
416 if (sbp != &mp->mnt_stat) {
417 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
418 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
420 sbp->f_flags = mp->mnt_flag;
428 hpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
431 struct hpfid *hpfhp = (struct hpfid *)fhp;
434 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) {
438 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
439 * with HPFS, we don't need to check anything else for now */
446 hpfs_vptofh(struct vnode *vp, struct fid *fhp)
448 struct hpfsnode *hpp;
452 hpfhp = (struct hpfid *)fhp;
453 hpfhp->hpfid_len = sizeof(struct hpfid);
454 hpfhp->hpfid_ino = hpp->h_no;
455 /* hpfhp->hpfid_gen = hpp->h_gen; */
460 hpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
462 struct hpfsmount *hpmp = VFSTOHPFS(mp);
466 struct thread *td = curthread; /* XXX */
469 dprintf(("hpfs_vget(0x%x): ",ino));
475 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, td)) != NULL) {
476 dprintf(("hashed\n"));
481 * We have to lock node creation for a while,
482 * but then we have to call getnewvnode(),
483 * this may cause hpfs_reclaim() to be called,
484 * this may need to VOP_VGET() parent dir for
485 * update reasons, and if parent is not in
486 * hash, we have to lock node creation...
487 * To solve this, we MALLOC, getnewvnode and init while
488 * not locked (probability of node appearence
489 * at that time is little, and anyway - we'll
492 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
495 error = getnewvnode(VT_HPFS, hpmp->hpm_mp, &vp, VLKTIMEOUT, 0);
497 printf("hpfs_vget: can't get new vnode\n");
502 dprintf(("prenew "));
506 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
509 lwkt_token_init(&hp->h_interlock);
511 hp->h_flag = H_INVAL;
515 hp->h_dev = hpmp->hpm_dev;
516 hp->h_uid = hpmp->hpm_uid;
517 hp->h_gid = hpmp->hpm_uid;
518 hp->h_mode = hpmp->hpm_mode;
519 hp->h_devvp = hpmp->hpm_devvp;
523 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, td)) != NULL) {
524 dprintf(("hashed2\n"));
528 } while(LOCKMGR(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
532 LOCKMGR(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
534 error = bread(hpmp->hpm_devvp, dbtodoff(ino), FNODESIZE, &bp);
536 printf("hpfs_vget: can't read ino %"PRId64"\n",ino);
540 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
543 if (hp->h_fn.fn_magic != FN_MAGIC) {
544 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
549 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
550 hp->h_flag &= ~H_INVAL;
552 /* Return the locked and refd vnode */
558 static struct vfsops hpfs_vfsops = {
559 .vfs_mount = hpfs_mount,
560 .vfs_unmount = hpfs_unmount,
561 .vfs_root = hpfs_root,
562 .vfs_statfs = hpfs_statfs,
563 .vfs_sync = vfs_stdsync,
564 .vfs_vget = hpfs_vget,
565 .vfs_fhtovp = hpfs_fhtovp,
566 .vfs_checkexp = hpfs_checkexp,
567 .vfs_vptofh = hpfs_vptofh,
568 .vfs_init = hpfs_init,
569 .vfs_uninit = hpfs_hphash_uninit
572 VFS_SET(hpfs_vfsops, hpfs, 0);