vfs_syscalls_20.c revision 1.31 1 1.31 ad /* $NetBSD: vfs_syscalls_20.c,v 1.31 2008/06/24 11:18:15 ad Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1989, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos * (c) UNIX System Laboratories, Inc.
7 1.1 christos * All or some portions of this file are derived from material licensed
8 1.1 christos * to the University of California by American Telephone and Telegraph
9 1.1 christos * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.1 christos * the permission of UNIX System Laboratories, Inc.
11 1.1 christos *
12 1.1 christos * Redistribution and use in source and binary forms, with or without
13 1.1 christos * modification, are permitted provided that the following conditions
14 1.1 christos * are met:
15 1.1 christos * 1. Redistributions of source code must retain the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer.
17 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 christos * notice, this list of conditions and the following disclaimer in the
19 1.1 christos * documentation and/or other materials provided with the distribution.
20 1.1 christos * 3. Neither the name of the University nor the names of its contributors
21 1.1 christos * may be used to endorse or promote products derived from this software
22 1.1 christos * without specific prior written permission.
23 1.1 christos *
24 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 christos * SUCH DAMAGE.
35 1.1 christos *
36 1.1 christos * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <sys/cdefs.h>
40 1.31 ad __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_20.c,v 1.31 2008/06/24 11:18:15 ad Exp $");
41 1.1 christos
42 1.1 christos #include "opt_compat_netbsd.h"
43 1.1 christos
44 1.1 christos #include <sys/param.h>
45 1.1 christos #include <sys/systm.h>
46 1.1 christos #include <sys/namei.h>
47 1.1 christos #include <sys/filedesc.h>
48 1.1 christos #include <sys/kernel.h>
49 1.1 christos #include <sys/file.h>
50 1.1 christos #include <sys/stat.h>
51 1.1 christos #include <sys/vnode.h>
52 1.1 christos #include <sys/mount.h>
53 1.1 christos #include <sys/proc.h>
54 1.1 christos #include <sys/uio.h>
55 1.1 christos #include <sys/malloc.h>
56 1.1 christos #include <sys/dirent.h>
57 1.1 christos #include <sys/sysctl.h>
58 1.1 christos #include <sys/syscallargs.h>
59 1.8 elad #include <sys/kauth.h>
60 1.1 christos
61 1.5 christos #include <compat/sys/mount.h>
62 1.5 christos
63 1.1 christos #ifdef COMPAT_09
64 1.1 christos #define MOUNTNO_NONE 0
65 1.1 christos #define MOUNTNO_UFS 1 /* UNIX "Fast" Filesystem */
66 1.1 christos #define MOUNTNO_NFS 2 /* Network Filesystem */
67 1.1 christos #define MOUNTNO_MFS 3 /* Memory Filesystem */
68 1.1 christos #define MOUNTNO_MSDOS 4 /* MSDOS Filesystem */
69 1.1 christos #define MOUNTNO_CD9660 5 /* iso9660 cdrom */
70 1.1 christos #define MOUNTNO_FDESC 6 /* /dev/fd filesystem */
71 1.4 perry #define MOUNTNO_KERNFS 7 /* kernel variable filesystem */
72 1.1 christos #define MOUNTNO_DEVFS 8 /* device node filesystem */
73 1.1 christos #define MOUNTNO_AFS 9 /* AFS 3.x */
74 1.1 christos static const struct {
75 1.1 christos const char *name;
76 1.1 christos const int value;
77 1.1 christos } nv[] = {
78 1.1 christos { MOUNT_UFS, MOUNTNO_UFS },
79 1.1 christos { MOUNT_NFS, MOUNTNO_NFS },
80 1.1 christos { MOUNT_MFS, MOUNTNO_MFS },
81 1.1 christos { MOUNT_MSDOS, MOUNTNO_MSDOS },
82 1.1 christos { MOUNT_CD9660, MOUNTNO_CD9660 },
83 1.1 christos { MOUNT_FDESC, MOUNTNO_FDESC },
84 1.1 christos { MOUNT_KERNFS, MOUNTNO_KERNFS },
85 1.1 christos { MOUNT_AFS, MOUNTNO_AFS },
86 1.1 christos };
87 1.1 christos #endif
88 1.1 christos
89 1.1 christos static int
90 1.4 perry vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
91 1.1 christos {
92 1.1 christos struct statfs12 ofs;
93 1.1 christos #ifdef COMPAT_09
94 1.1 christos int i = 0;
95 1.1 christos ofs.f_type = 0;
96 1.1 christos ofs.f_oflags = (short)fs->f_flag;
97 1.1 christos
98 1.1 christos for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
99 1.1 christos if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
100 1.1 christos ofs.f_type = nv[i].value;
101 1.1 christos break;
102 1.1 christos }
103 1.1 christos }
104 1.1 christos #else
105 1.1 christos ofs.f_type = 0;
106 1.1 christos #endif
107 1.1 christos #define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
108 1.1 christos ofs.f_bsize = CLAMP(fs->f_frsize);
109 1.1 christos ofs.f_iosize = CLAMP(fs->f_iosize);
110 1.1 christos ofs.f_blocks = CLAMP(fs->f_blocks);
111 1.1 christos ofs.f_bfree = CLAMP(fs->f_bfree);
112 1.1 christos if (fs->f_bfree > fs->f_bresvd)
113 1.1 christos ofs.f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
114 1.1 christos else
115 1.1 christos ofs.f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
116 1.1 christos ofs.f_files = CLAMP(fs->f_files);
117 1.1 christos ofs.f_ffree = CLAMP(fs->f_ffree);
118 1.1 christos ofs.f_fsid = fs->f_fsidx;
119 1.1 christos ofs.f_owner = fs->f_owner;
120 1.1 christos ofs.f_flags = (long)fs->f_flag;
121 1.1 christos ofs.f_syncwrites = CLAMP(fs->f_syncwrites);
122 1.1 christos ofs.f_asyncwrites = CLAMP(fs->f_asyncwrites);
123 1.1 christos (void)strncpy(ofs.f_fstypename, fs->f_fstypename,
124 1.1 christos sizeof(ofs.f_fstypename));
125 1.1 christos (void)strncpy(ofs.f_mntonname, fs->f_mntonname,
126 1.1 christos sizeof(ofs.f_mntonname));
127 1.1 christos (void)strncpy(ofs.f_mntfromname, fs->f_mntfromname,
128 1.1 christos sizeof(ofs.f_mntfromname));
129 1.1 christos
130 1.1 christos return copyout(&ofs, bfs, sizeof(ofs));
131 1.1 christos }
132 1.1 christos
133 1.1 christos /*
134 1.1 christos * Get filesystem statistics.
135 1.1 christos */
136 1.1 christos /* ARGSUSED */
137 1.1 christos int
138 1.23 dsl compat_20_sys_statfs(struct lwp *l, const struct compat_20_sys_statfs_args *uap, register_t *retval)
139 1.1 christos {
140 1.23 dsl /* {
141 1.1 christos syscallarg(const char *) path;
142 1.1 christos syscallarg(struct statfs12 *) buf;
143 1.23 dsl } */
144 1.1 christos struct mount *mp;
145 1.2 christos struct statvfs *sbuf;
146 1.2 christos int error = 0;
147 1.1 christos struct nameidata nd;
148 1.1 christos
149 1.22 pooka NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
150 1.22 pooka SCARG(uap, path));
151 1.1 christos if ((error = namei(&nd)) != 0)
152 1.1 christos return error;
153 1.2 christos
154 1.1 christos mp = nd.ni_vp->v_mount;
155 1.2 christos
156 1.2 christos sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
157 1.6 christos if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
158 1.2 christos goto done;
159 1.2 christos
160 1.3 christos error = vfs2fs(SCARG(uap, buf), sbuf);
161 1.2 christos done:
162 1.24 ad vrele(nd.ni_vp);
163 1.2 christos free(sbuf, M_TEMP);
164 1.2 christos return error;
165 1.1 christos }
166 1.1 christos
167 1.1 christos /*
168 1.1 christos * Get filesystem statistics.
169 1.1 christos */
170 1.1 christos /* ARGSUSED */
171 1.1 christos int
172 1.23 dsl compat_20_sys_fstatfs(struct lwp *l, const struct compat_20_sys_fstatfs_args *uap, register_t *retval)
173 1.1 christos {
174 1.23 dsl /* {
175 1.1 christos syscallarg(int) fd;
176 1.1 christos syscallarg(struct statfs12 *) buf;
177 1.23 dsl } */
178 1.1 christos struct file *fp;
179 1.1 christos struct mount *mp;
180 1.9 christos struct statvfs *sbuf;
181 1.1 christos int error;
182 1.1 christos
183 1.31 ad /* fd_getvnode() will use the descriptor for us */
184 1.31 ad if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
185 1.1 christos return (error);
186 1.1 christos mp = ((struct vnode *)fp->f_data)->v_mount;
187 1.9 christos sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
188 1.9 christos if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
189 1.1 christos goto out;
190 1.9 christos error = vfs2fs(SCARG(uap, buf), sbuf);
191 1.1 christos out:
192 1.26 ad fd_putfile(SCARG(uap, fd));
193 1.9 christos free(sbuf, M_TEMP);
194 1.1 christos return error;
195 1.1 christos }
196 1.1 christos
197 1.1 christos
198 1.1 christos /*
199 1.1 christos * Get statistics on all filesystems.
200 1.1 christos */
201 1.1 christos int
202 1.23 dsl compat_20_sys_getfsstat(struct lwp *l, const struct compat_20_sys_getfsstat_args *uap, register_t *retval)
203 1.1 christos {
204 1.23 dsl /* {
205 1.1 christos syscallarg(struct statfs12 *) buf;
206 1.1 christos syscallarg(long) bufsize;
207 1.1 christos syscallarg(int) flags;
208 1.23 dsl } */
209 1.1 christos int root = 0;
210 1.1 christos struct mount *mp, *nmp;
211 1.9 christos struct statvfs *sbuf;
212 1.1 christos struct statfs12 *sfsp;
213 1.1 christos size_t count, maxcount;
214 1.1 christos int error = 0;
215 1.1 christos
216 1.10 drochner sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
217 1.1 christos maxcount = (size_t)SCARG(uap, bufsize) / sizeof(struct statfs12);
218 1.1 christos sfsp = SCARG(uap, buf);
219 1.18 ad mutex_enter(&mountlist_lock);
220 1.1 christos count = 0;
221 1.1 christos for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
222 1.1 christos mp = nmp) {
223 1.30 ad if (vfs_busy(mp, &nmp)) {
224 1.1 christos continue;
225 1.1 christos }
226 1.1 christos if (sfsp && count < maxcount) {
227 1.9 christos error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 0);
228 1.1 christos if (error) {
229 1.28 ad vfs_unbusy(mp, false, &nmp);
230 1.1 christos continue;
231 1.1 christos }
232 1.9 christos error = vfs2fs(sfsp, sbuf);
233 1.1 christos if (error) {
234 1.28 ad vfs_unbusy(mp, false, NULL);
235 1.9 christos goto out;
236 1.1 christos }
237 1.1 christos sfsp++;
238 1.9 christos root |= strcmp(sbuf->f_mntonname, "/") == 0;
239 1.1 christos }
240 1.1 christos count++;
241 1.28 ad vfs_unbusy(mp, false, &nmp);
242 1.1 christos }
243 1.18 ad mutex_exit(&mountlist_lock);
244 1.6 christos if (root == 0 && l->l_proc->p_cwdi->cwdi_rdir) {
245 1.1 christos /*
246 1.1 christos * fake a root entry
247 1.1 christos */
248 1.10 drochner if ((error = dostatvfs(l->l_proc->p_cwdi->cwdi_rdir->v_mount,
249 1.10 drochner sbuf, l, SCARG(uap, flags), 1)) != 0)
250 1.9 christos goto out;
251 1.1 christos if (sfsp)
252 1.9 christos error = vfs2fs(sfsp, sbuf);
253 1.1 christos count++;
254 1.1 christos }
255 1.1 christos if (sfsp && count > maxcount)
256 1.1 christos *retval = maxcount;
257 1.1 christos else
258 1.1 christos *retval = count;
259 1.9 christos out:
260 1.9 christos free(sbuf, M_TEMP);
261 1.1 christos return error;
262 1.1 christos }
263 1.1 christos
264 1.1 christos int
265 1.23 dsl compat_20_sys_fhstatfs(struct lwp *l, const struct compat_20_sys_fhstatfs_args *uap, register_t *retval)
266 1.1 christos {
267 1.23 dsl /* {
268 1.11 martin syscallarg(const struct compat_30_fhandle *) fhp;
269 1.1 christos syscallarg(struct statfs12 *) buf;
270 1.23 dsl } */
271 1.9 christos struct statvfs *sbuf;
272 1.11 martin struct compat_30_fhandle fh;
273 1.1 christos struct mount *mp;
274 1.1 christos struct vnode *vp;
275 1.1 christos int error;
276 1.1 christos
277 1.1 christos /*
278 1.1 christos * Must be super user
279 1.1 christos */
280 1.14 elad if ((error = kauth_authorize_system(l->l_cred,
281 1.14 elad KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
282 1.1 christos return (error);
283 1.1 christos
284 1.11 martin if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
285 1.1 christos return (error);
286 1.1 christos
287 1.1 christos if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
288 1.1 christos return (ESTALE);
289 1.11 martin if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
290 1.1 christos return (error);
291 1.1 christos mp = vp->v_mount;
292 1.24 ad VOP_UNLOCK(vp, 0);
293 1.9 christos sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
294 1.20 pooka if ((error = VFS_STATVFS(mp, sbuf)) != 0)
295 1.9 christos goto out;
296 1.9 christos error = vfs2fs(SCARG(uap, buf), sbuf);
297 1.9 christos out:
298 1.24 ad vrele(vp);
299 1.9 christos free(sbuf, M_TEMP);
300 1.9 christos return error;
301 1.1 christos }
302