vfs_syscalls_20.c revision 1.8 1 1.8 elad /* $NetBSD: vfs_syscalls_20.c,v 1.8 2006/05/14 21:24:49 elad 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.8 elad __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_20.c,v 1.8 2006/05/14 21:24:49 elad Exp $");
41 1.1 christos
42 1.1 christos #include "opt_compat_netbsd.h"
43 1.1 christos #include "opt_compat_43.h"
44 1.1 christos #include "fss.h"
45 1.1 christos
46 1.1 christos #include <sys/param.h>
47 1.1 christos #include <sys/systm.h>
48 1.1 christos #include <sys/namei.h>
49 1.1 christos #include <sys/filedesc.h>
50 1.1 christos #include <sys/kernel.h>
51 1.1 christos #include <sys/file.h>
52 1.1 christos #include <sys/stat.h>
53 1.1 christos #include <sys/vnode.h>
54 1.1 christos #include <sys/mount.h>
55 1.1 christos #include <sys/proc.h>
56 1.1 christos #include <sys/uio.h>
57 1.1 christos #include <sys/malloc.h>
58 1.1 christos #include <sys/dirent.h>
59 1.1 christos #include <sys/sysctl.h>
60 1.1 christos #include <sys/sa.h>
61 1.1 christos #include <sys/syscallargs.h>
62 1.8 elad #include <sys/kauth.h>
63 1.1 christos
64 1.5 christos #include <compat/sys/mount.h>
65 1.5 christos
66 1.1 christos #ifdef COMPAT_09
67 1.1 christos #define MOUNTNO_NONE 0
68 1.1 christos #define MOUNTNO_UFS 1 /* UNIX "Fast" Filesystem */
69 1.1 christos #define MOUNTNO_NFS 2 /* Network Filesystem */
70 1.1 christos #define MOUNTNO_MFS 3 /* Memory Filesystem */
71 1.1 christos #define MOUNTNO_MSDOS 4 /* MSDOS Filesystem */
72 1.1 christos #define MOUNTNO_CD9660 5 /* iso9660 cdrom */
73 1.1 christos #define MOUNTNO_FDESC 6 /* /dev/fd filesystem */
74 1.4 perry #define MOUNTNO_KERNFS 7 /* kernel variable filesystem */
75 1.1 christos #define MOUNTNO_DEVFS 8 /* device node filesystem */
76 1.1 christos #define MOUNTNO_AFS 9 /* AFS 3.x */
77 1.1 christos static const struct {
78 1.1 christos const char *name;
79 1.1 christos const int value;
80 1.1 christos } nv[] = {
81 1.1 christos { MOUNT_UFS, MOUNTNO_UFS },
82 1.1 christos { MOUNT_NFS, MOUNTNO_NFS },
83 1.1 christos { MOUNT_MFS, MOUNTNO_MFS },
84 1.1 christos { MOUNT_MSDOS, MOUNTNO_MSDOS },
85 1.1 christos { MOUNT_CD9660, MOUNTNO_CD9660 },
86 1.1 christos { MOUNT_FDESC, MOUNTNO_FDESC },
87 1.1 christos { MOUNT_KERNFS, MOUNTNO_KERNFS },
88 1.1 christos { MOUNT_AFS, MOUNTNO_AFS },
89 1.1 christos };
90 1.1 christos #endif
91 1.1 christos
92 1.1 christos static int
93 1.4 perry vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
94 1.1 christos {
95 1.1 christos struct statfs12 ofs;
96 1.1 christos #ifdef COMPAT_09
97 1.1 christos int i = 0;
98 1.1 christos ofs.f_type = 0;
99 1.1 christos ofs.f_oflags = (short)fs->f_flag;
100 1.1 christos
101 1.1 christos for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
102 1.1 christos if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
103 1.1 christos ofs.f_type = nv[i].value;
104 1.1 christos break;
105 1.1 christos }
106 1.1 christos }
107 1.1 christos #else
108 1.1 christos ofs.f_type = 0;
109 1.1 christos #endif
110 1.1 christos #define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
111 1.1 christos ofs.f_bsize = CLAMP(fs->f_frsize);
112 1.1 christos ofs.f_iosize = CLAMP(fs->f_iosize);
113 1.1 christos ofs.f_blocks = CLAMP(fs->f_blocks);
114 1.1 christos ofs.f_bfree = CLAMP(fs->f_bfree);
115 1.1 christos if (fs->f_bfree > fs->f_bresvd)
116 1.1 christos ofs.f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
117 1.1 christos else
118 1.1 christos ofs.f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
119 1.1 christos ofs.f_files = CLAMP(fs->f_files);
120 1.1 christos ofs.f_ffree = CLAMP(fs->f_ffree);
121 1.1 christos ofs.f_fsid = fs->f_fsidx;
122 1.1 christos ofs.f_owner = fs->f_owner;
123 1.1 christos ofs.f_flags = (long)fs->f_flag;
124 1.1 christos ofs.f_syncwrites = CLAMP(fs->f_syncwrites);
125 1.1 christos ofs.f_asyncwrites = CLAMP(fs->f_asyncwrites);
126 1.1 christos (void)strncpy(ofs.f_fstypename, fs->f_fstypename,
127 1.1 christos sizeof(ofs.f_fstypename));
128 1.1 christos (void)strncpy(ofs.f_mntonname, fs->f_mntonname,
129 1.1 christos sizeof(ofs.f_mntonname));
130 1.1 christos (void)strncpy(ofs.f_mntfromname, fs->f_mntfromname,
131 1.1 christos sizeof(ofs.f_mntfromname));
132 1.1 christos
133 1.1 christos return copyout(&ofs, bfs, sizeof(ofs));
134 1.1 christos }
135 1.1 christos
136 1.1 christos /*
137 1.1 christos * Get filesystem statistics.
138 1.1 christos */
139 1.1 christos /* ARGSUSED */
140 1.1 christos int
141 1.1 christos compat_20_sys_statfs(l, v, retval)
142 1.1 christos struct lwp *l;
143 1.1 christos void *v;
144 1.1 christos register_t *retval;
145 1.1 christos {
146 1.1 christos struct compat_20_sys_statfs_args /* {
147 1.1 christos syscallarg(const char *) path;
148 1.1 christos syscallarg(struct statfs12 *) buf;
149 1.1 christos } */ *uap = v;
150 1.1 christos struct mount *mp;
151 1.2 christos struct statvfs *sbuf;
152 1.2 christos int error = 0;
153 1.1 christos struct nameidata nd;
154 1.1 christos
155 1.6 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
156 1.1 christos if ((error = namei(&nd)) != 0)
157 1.1 christos return error;
158 1.2 christos
159 1.1 christos mp = nd.ni_vp->v_mount;
160 1.1 christos vrele(nd.ni_vp);
161 1.2 christos
162 1.2 christos sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
163 1.6 christos if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
164 1.2 christos goto done;
165 1.2 christos
166 1.3 christos error = vfs2fs(SCARG(uap, buf), sbuf);
167 1.2 christos done:
168 1.2 christos free(sbuf, M_TEMP);
169 1.2 christos return error;
170 1.1 christos }
171 1.1 christos
172 1.1 christos /*
173 1.1 christos * Get filesystem statistics.
174 1.1 christos */
175 1.1 christos /* ARGSUSED */
176 1.1 christos int
177 1.1 christos compat_20_sys_fstatfs(l, v, retval)
178 1.1 christos struct lwp *l;
179 1.1 christos void *v;
180 1.1 christos register_t *retval;
181 1.1 christos {
182 1.1 christos struct compat_20_sys_fstatfs_args /* {
183 1.1 christos syscallarg(int) fd;
184 1.1 christos syscallarg(struct statfs12 *) buf;
185 1.1 christos } */ *uap = v;
186 1.1 christos struct proc *p = l->l_proc;
187 1.1 christos struct file *fp;
188 1.1 christos struct mount *mp;
189 1.1 christos struct statvfs sbuf;
190 1.1 christos int error;
191 1.1 christos
192 1.1 christos /* getvnode() will use the descriptor for us */
193 1.1 christos if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
194 1.1 christos return (error);
195 1.1 christos mp = ((struct vnode *)fp->f_data)->v_mount;
196 1.6 christos if ((error = dostatvfs(mp, &sbuf, l, 0, 1)) != 0)
197 1.1 christos goto out;
198 1.1 christos error = vfs2fs(SCARG(uap, buf), &sbuf);
199 1.1 christos out:
200 1.6 christos FILE_UNUSE(fp, l);
201 1.1 christos return error;
202 1.1 christos }
203 1.1 christos
204 1.1 christos
205 1.1 christos /*
206 1.1 christos * Get statistics on all filesystems.
207 1.1 christos */
208 1.1 christos int
209 1.1 christos compat_20_sys_getfsstat(l, v, retval)
210 1.1 christos struct lwp *l;
211 1.1 christos void *v;
212 1.1 christos register_t *retval;
213 1.1 christos {
214 1.1 christos struct compat_20_sys_getfsstat_args /* {
215 1.1 christos syscallarg(struct statfs12 *) buf;
216 1.1 christos syscallarg(long) bufsize;
217 1.1 christos syscallarg(int) flags;
218 1.1 christos } */ *uap = v;
219 1.1 christos int root = 0;
220 1.1 christos struct mount *mp, *nmp;
221 1.1 christos struct statvfs sbuf;
222 1.1 christos struct statfs12 *sfsp;
223 1.1 christos size_t count, maxcount;
224 1.1 christos int error = 0;
225 1.1 christos
226 1.1 christos maxcount = (size_t)SCARG(uap, bufsize) / sizeof(struct statfs12);
227 1.1 christos sfsp = SCARG(uap, buf);
228 1.1 christos simple_lock(&mountlist_slock);
229 1.1 christos count = 0;
230 1.1 christos for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
231 1.1 christos mp = nmp) {
232 1.1 christos if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
233 1.1 christos nmp = CIRCLEQ_NEXT(mp, mnt_list);
234 1.1 christos continue;
235 1.1 christos }
236 1.1 christos if (sfsp && count < maxcount) {
237 1.6 christos error = dostatvfs(mp, &sbuf, l, SCARG(uap, flags), 0);
238 1.1 christos if (error) {
239 1.1 christos simple_lock(&mountlist_slock);
240 1.1 christos nmp = CIRCLEQ_NEXT(mp, mnt_list);
241 1.1 christos vfs_unbusy(mp);
242 1.1 christos continue;
243 1.1 christos }
244 1.1 christos error = vfs2fs(sfsp, &sbuf);
245 1.1 christos if (error) {
246 1.1 christos vfs_unbusy(mp);
247 1.1 christos return (error);
248 1.1 christos }
249 1.1 christos sfsp++;
250 1.1 christos root |= strcmp(sbuf.f_mntonname, "/") == 0;
251 1.1 christos }
252 1.1 christos count++;
253 1.1 christos simple_lock(&mountlist_slock);
254 1.1 christos nmp = CIRCLEQ_NEXT(mp, mnt_list);
255 1.1 christos vfs_unbusy(mp);
256 1.1 christos }
257 1.1 christos simple_unlock(&mountlist_slock);
258 1.6 christos if (root == 0 && l->l_proc->p_cwdi->cwdi_rdir) {
259 1.1 christos /*
260 1.1 christos * fake a root entry
261 1.1 christos */
262 1.6 christos if ((error = dostatvfs(l->l_proc->p_cwdi->cwdi_rdir->v_mount, &sbuf, l,
263 1.1 christos SCARG(uap, flags), 1)) != 0)
264 1.1 christos return error;
265 1.1 christos if (sfsp)
266 1.1 christos error = vfs2fs(sfsp, &sbuf);
267 1.1 christos count++;
268 1.1 christos }
269 1.1 christos if (sfsp && count > maxcount)
270 1.1 christos *retval = maxcount;
271 1.1 christos else
272 1.1 christos *retval = count;
273 1.1 christos return error;
274 1.1 christos }
275 1.1 christos
276 1.1 christos int
277 1.1 christos compat_20_sys_fhstatfs(l, v, retval)
278 1.1 christos struct lwp *l;
279 1.1 christos void *v;
280 1.1 christos register_t *retval;
281 1.1 christos {
282 1.1 christos struct compat_20_sys_fhstatfs_args /*
283 1.1 christos syscallarg(const fhandle_t *) fhp;
284 1.1 christos syscallarg(struct statfs12 *) buf;
285 1.1 christos } */ *uap = v;
286 1.1 christos struct proc *p = l->l_proc;
287 1.1 christos struct statvfs sbuf;
288 1.1 christos fhandle_t fh;
289 1.1 christos struct mount *mp;
290 1.1 christos struct vnode *vp;
291 1.1 christos int error;
292 1.1 christos
293 1.1 christos /*
294 1.1 christos * Must be super user
295 1.1 christos */
296 1.8 elad if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)))
297 1.1 christos return (error);
298 1.1 christos
299 1.1 christos if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
300 1.1 christos return (error);
301 1.1 christos
302 1.1 christos if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
303 1.1 christos return (ESTALE);
304 1.1 christos if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
305 1.1 christos return (error);
306 1.1 christos mp = vp->v_mount;
307 1.1 christos vput(vp);
308 1.6 christos if ((error = VFS_STATVFS(mp, &sbuf, l)) != 0)
309 1.1 christos return (error);
310 1.1 christos return vfs2fs(SCARG(uap, buf), &sbuf);
311 1.1 christos }
312