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