1 /*
2 * Copyright (c) 1993 Jan-Simon Pendry
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
34 *
35 * $FreeBSD: src/sys/miscfs/procfs/procfs_vfsops.c,v 1.32.2.1 2001/10/15 20:42:01 des Exp $
36 */
37
38 /*
39 * procfs VFS interface
40 */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
48 #include <vfs/procfs/procfs.h>
49
50 extern struct vop_ops procfs_vnode_vops;
51
52 static int procfs_mount (struct mount *mp, char *path, caddr_t data,
53 struct ucred *cred);
54 static int procfs_statfs (struct mount *mp, struct statfs *sbp,
55 struct ucred *cred);
56 static int procfs_unmount (struct mount *mp, int mntflags);
57
58 /*
59 * VFS Operations.
60 *
61 * mount system call
62 */
63 /* ARGSUSED */
64 static int
procfs_mount(struct mount * mp,char * path,caddr_t data,struct ucred * cred)65 procfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
66 {
67 size_t size;
68 int error;
69
70 if (mp->mnt_flag & MNT_UPDATE)
71 return (EOPNOTSUPP);
72
73 if (mp->mnt_vfc->vfc_refcount == 1 && (error = at_exit(procfs_exit))) {
74 kprintf("procfs: cannot register procfs_exit with at_exit\n");
75 return(error);
76 }
77
78 mp->mnt_flag |= MNT_LOCAL;
79 mp->mnt_kern_flag |= MNTK_NOSTKMNT;
80 mp->mnt_kern_flag |= MNTK_QUICKHALT; /* no teardown needed on halt */
81 mp->mnt_data = NULL;
82 vfs_getnewfsid(mp);
83
84 size = sizeof("procfs") - 1;
85 bcopy("procfs", mp->mnt_stat.f_mntfromname, size);
86 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
87 procfs_statfs(mp, &mp->mnt_stat, cred);
88 vfs_add_vnodeops(mp, &procfs_vnode_vops, &mp->mnt_vn_norm_ops);
89
90 return (0);
91 }
92
93 /*
94 * unmount system call
95 */
96 static int
procfs_unmount(struct mount * mp,int mntflags)97 procfs_unmount(struct mount *mp, int mntflags)
98 {
99 int error;
100 int flags = 0;
101
102 if (mntflags & MNT_FORCE)
103 flags |= FORCECLOSE;
104
105 error = vflush(mp, 0, flags);
106 if (error)
107 return (error);
108
109 if (mp->mnt_vfc->vfc_refcount == 1)
110 rm_at_exit(procfs_exit);
111
112 return (0);
113 }
114
115 /*
116 * Sets *vpp to the root procfs vnode, referenced and exclusively locked.
117 */
118 int
procfs_root(struct mount * mp,struct vnode ** vpp)119 procfs_root(struct mount *mp, struct vnode **vpp)
120 {
121 return (procfs_allocvp(mp, vpp, 0, Proot));
122 }
123
124 /*
125 * Get file system statistics.
126 */
127 static int
procfs_statfs(struct mount * mp,struct statfs * sbp,struct ucred * cred)128 procfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
129 {
130 sbp->f_bsize = PAGE_SIZE;
131 sbp->f_iosize = PAGE_SIZE;
132 sbp->f_blocks = 1; /* avoid divide by zero in some df's */
133 sbp->f_bfree = 0;
134 sbp->f_bavail = 0;
135 sbp->f_files = maxproc; /* approx */
136 sbp->f_ffree = maxproc - nprocs; /* approx */
137
138 if (sbp != &mp->mnt_stat) {
139 sbp->f_type = mp->mnt_vfc->vfc_typenum;
140 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
141 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
142 }
143
144 return (0);
145 }
146
147 static struct vfsops procfs_vfsops = {
148 .vfs_flags = 0,
149 .vfs_mount = procfs_mount,
150 .vfs_unmount = procfs_unmount,
151 .vfs_root = procfs_root,
152 .vfs_statfs = procfs_statfs,
153 };
154
155 VFS_SET(procfs_vfsops, procfs, VFCF_SYNTHETIC | VFCF_MPSAFE);
156 MODULE_VERSION(procfs, 1);
157