1 /* $NetBSD: vfs_syscalls_20.c,v 1.33 2008/11/20 09:22:57 ad Exp $ */
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_20.c,v 1.33 2008/11/20 09:22:57 ad Exp $");
43 #include "opt_compat_netbsd.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
57 #include <sys/malloc.h>
58 #include <sys/dirent.h>
59 #include <sys/sysctl.h>
60 #include <sys/syscallargs.h>
61 #include <sys/kauth.h>
63 #include <compat/sys/mount.h>
65 #define MOUNTNO_NONE 0
66 #define MOUNTNO_UFS 1 /* UNIX "Fast" Filesystem */
67 #define MOUNTNO_NFS 2 /* Network Filesystem */
68 #define MOUNTNO_MFS 3 /* Memory Filesystem */
69 #define MOUNTNO_MSDOS 4 /* MSDOS Filesystem */
70 #define MOUNTNO_CD9660 5 /* iso9660 cdrom */
71 #define MOUNTNO_FDESC 6 /* /dev/fd filesystem */
72 #define MOUNTNO_KERNFS 7 /* kernel variable filesystem */
73 #define MOUNTNO_DEVFS 8 /* device node filesystem */
74 #define MOUNTNO_AFS 9 /* AFS 3.x */
79 { MOUNT_UFS
, MOUNTNO_UFS
},
80 { MOUNT_NFS
, MOUNTNO_NFS
},
81 { MOUNT_MFS
, MOUNTNO_MFS
},
82 { MOUNT_MSDOS
, MOUNTNO_MSDOS
},
83 { MOUNT_CD9660
, MOUNTNO_CD9660
},
84 { MOUNT_FDESC
, MOUNTNO_FDESC
},
85 { MOUNT_KERNFS
, MOUNTNO_KERNFS
},
86 { MOUNT_AFS
, MOUNTNO_AFS
},
90 vfs2fs(struct statfs12
*bfs
, const struct statvfs
*fs
)
95 ofs
.f_oflags
= (short)fs
->f_flag
;
97 for (i
= 0; i
< sizeof(nv
) / sizeof(nv
[0]); i
++) {
98 if (strcmp(nv
[i
].name
, fs
->f_fstypename
) == 0) {
99 ofs
.f_type
= nv
[i
].value
;
103 #define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
104 ofs
.f_bsize
= CLAMP(fs
->f_frsize
);
105 ofs
.f_iosize
= CLAMP(fs
->f_iosize
);
106 ofs
.f_blocks
= CLAMP(fs
->f_blocks
);
107 ofs
.f_bfree
= CLAMP(fs
->f_bfree
);
108 if (fs
->f_bfree
> fs
->f_bresvd
)
109 ofs
.f_bavail
= CLAMP(fs
->f_bfree
- fs
->f_bresvd
);
111 ofs
.f_bavail
= -CLAMP(fs
->f_bresvd
- fs
->f_bfree
);
112 ofs
.f_files
= CLAMP(fs
->f_files
);
113 ofs
.f_ffree
= CLAMP(fs
->f_ffree
);
114 ofs
.f_fsid
= fs
->f_fsidx
;
115 ofs
.f_owner
= fs
->f_owner
;
116 ofs
.f_flags
= (long)fs
->f_flag
;
117 ofs
.f_syncwrites
= CLAMP(fs
->f_syncwrites
);
118 ofs
.f_asyncwrites
= CLAMP(fs
->f_asyncwrites
);
119 (void)strncpy(ofs
.f_fstypename
, fs
->f_fstypename
,
120 sizeof(ofs
.f_fstypename
));
121 (void)strncpy(ofs
.f_mntonname
, fs
->f_mntonname
,
122 sizeof(ofs
.f_mntonname
));
123 (void)strncpy(ofs
.f_mntfromname
, fs
->f_mntfromname
,
124 sizeof(ofs
.f_mntfromname
));
126 return copyout(&ofs
, bfs
, sizeof(ofs
));
130 * Get filesystem statistics.
134 compat_20_sys_statfs(struct lwp
*l
, const struct compat_20_sys_statfs_args
*uap
, register_t
*retval
)
137 syscallarg(const char *) path;
138 syscallarg(struct statfs12 *) buf;
141 struct statvfs
*sbuf
;
145 error
= namei_simple_user(SCARG(uap
, path
),
146 NSM_FOLLOW_TRYEMULROOT
, &vp
);
152 sbuf
= malloc(sizeof(*sbuf
), M_TEMP
, M_WAITOK
);
153 if ((error
= dostatvfs(mp
, sbuf
, l
, 0, 1)) != 0)
156 error
= vfs2fs(SCARG(uap
, buf
), sbuf
);
164 * Get filesystem statistics.
168 compat_20_sys_fstatfs(struct lwp
*l
, const struct compat_20_sys_fstatfs_args
*uap
, register_t
*retval
)
172 syscallarg(struct statfs12 *) buf;
176 struct statvfs
*sbuf
;
179 /* fd_getvnode() will use the descriptor for us */
180 if ((error
= fd_getvnode(SCARG(uap
, fd
), &fp
)) != 0)
182 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
183 sbuf
= malloc(sizeof(*sbuf
), M_TEMP
, M_WAITOK
);
184 if ((error
= dostatvfs(mp
, sbuf
, l
, 0, 1)) != 0)
186 error
= vfs2fs(SCARG(uap
, buf
), sbuf
);
188 fd_putfile(SCARG(uap
, fd
));
195 * Get statistics on all filesystems.
198 compat_20_sys_getfsstat(struct lwp
*l
, const struct compat_20_sys_getfsstat_args
*uap
, register_t
*retval
)
201 syscallarg(struct statfs12 *) buf;
202 syscallarg(long) bufsize;
203 syscallarg(int) flags;
206 struct mount
*mp
, *nmp
;
207 struct statvfs
*sbuf
;
208 struct statfs12
*sfsp
;
209 size_t count
, maxcount
;
212 sbuf
= malloc(sizeof(*sbuf
), M_TEMP
, M_WAITOK
);
213 maxcount
= (size_t)SCARG(uap
, bufsize
) / sizeof(struct statfs12
);
214 sfsp
= SCARG(uap
, buf
);
215 mutex_enter(&mountlist_lock
);
217 for (mp
= CIRCLEQ_FIRST(&mountlist
); mp
!= (void *)&mountlist
;
219 if (vfs_busy(mp
, &nmp
)) {
222 if (sfsp
&& count
< maxcount
) {
223 error
= dostatvfs(mp
, sbuf
, l
, SCARG(uap
, flags
), 0);
225 vfs_unbusy(mp
, false, &nmp
);
228 error
= vfs2fs(sfsp
, sbuf
);
230 vfs_unbusy(mp
, false, NULL
);
234 root
|= strcmp(sbuf
->f_mntonname
, "/") == 0;
237 vfs_unbusy(mp
, false, &nmp
);
239 mutex_exit(&mountlist_lock
);
240 if (root
== 0 && l
->l_proc
->p_cwdi
->cwdi_rdir
) {
244 if ((error
= dostatvfs(l
->l_proc
->p_cwdi
->cwdi_rdir
->v_mount
,
245 sbuf
, l
, SCARG(uap
, flags
), 1)) != 0)
248 error
= vfs2fs(sfsp
, sbuf
);
251 if (sfsp
&& count
> maxcount
)
261 compat_20_sys_fhstatfs(struct lwp
*l
, const struct compat_20_sys_fhstatfs_args
*uap
, register_t
*retval
)
264 syscallarg(const struct compat_30_fhandle *) fhp;
265 syscallarg(struct statfs12 *) buf;
267 struct statvfs
*sbuf
;
268 struct compat_30_fhandle fh
;
276 if ((error
= kauth_authorize_system(l
->l_cred
,
277 KAUTH_SYSTEM_FILEHANDLE
, 0, NULL
, NULL
, NULL
)))
280 if ((error
= copyin(SCARG(uap
, fhp
), &fh
, sizeof(fh
))) != 0)
283 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
285 if ((error
= VFS_FHTOVP(mp
, (struct fid
*)&fh
.fh_fid
, &vp
)))
289 sbuf
= malloc(sizeof(*sbuf
), M_TEMP
, M_WAITOK
);
290 if ((error
= VFS_STATVFS(mp
, sbuf
)) != 0)
292 error
= vfs2fs(SCARG(uap
, buf
), sbuf
);