Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / darwin / darwin_mount.c
blob453664040c64e72f85b04e53e92a108ecb9fd402
1 /* $NetBSD: darwin_mount.c,v 1.19 2009/01/11 02:45:47 christos Exp $ */
3 /*-
4 * Copyright (c) 2003, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: darwin_mount.c,v 1.19 2009/01/11 02:45:47 christos Exp $");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mount.h>
39 #include <sys/proc.h>
40 #include <sys/vnode.h>
41 #include <sys/namei.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/syscallargs.h>
46 #include <compat/sys/signal.h>
48 #include <compat/mach/mach_types.h>
49 #include <compat/mach/mach_vm.h>
51 #include <compat/darwin/darwin_types.h>
52 #include <compat/darwin/darwin_audit.h>
53 #include <compat/darwin/darwin_mount.h>
54 #include <compat/darwin/darwin_syscallargs.h>
56 static void native_to_darwin_statvfs(const struct statvfs *,
57 struct darwin_statfs *);
59 int
60 darwin_sys_fstatfs(struct lwp *l, const struct darwin_sys_fstatfs_args *uap, register_t *retval)
62 /* {
63 syscallarg(int) fd;
64 syscallarg(struct darwin_statfs *) buf;
65 } */
66 file_t *fp;
67 struct mount *mp;
68 struct statvfs *bs;
69 struct darwin_statfs ds;
70 int error;
72 /* fd_getvnode() will use the descriptor for us */
73 if ((error = fd_getvnode(SCARG(uap, fd), &fp)))
74 return (error);
76 mp = ((struct vnode *)fp->f_data)->v_mount;
77 bs = &mp->mnt_stat;
79 if ((error = VFS_STATVFS(mp, bs)) != 0)
80 goto out;
82 native_to_darwin_statvfs(bs, &ds);
84 error = copyout(&ds, SCARG(uap, buf), sizeof(ds));
86 out:
87 fd_putfile(SCARG(uap, fd));
88 return (error);
91 int
92 darwin_sys_getfsstat(struct lwp *l, const struct darwin_sys_getfsstat_args *uap, register_t *retval)
94 /* {
95 syscallarg(struct darwin_statfs *) buf;
96 syscallarg(long) bufsize;
97 syscallarg(int) flags;
98 } */
99 struct mount *mp, *nmp;
100 struct statvfs *bs;
101 struct darwin_statfs ds;
102 struct darwin_statfs *uds;
103 long count, maxcount, error;
105 maxcount = SCARG(uap, bufsize) / sizeof(struct darwin_statfs);
106 uds = SCARG(uap, buf);
108 for (count = 0, mp = mountlist.cqh_first;
109 mp != (void *)&mountlist; mp = nmp) {
110 nmp = mp->mnt_list.cqe_next;
112 if ((uds != NULL) && (count < maxcount)) {
113 bs = &mp->mnt_stat;
115 if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
116 (SCARG(uap, flags) & MNT_WAIT)) &&
117 (error = VFS_STATVFS(mp, bs)))
118 continue;
120 native_to_darwin_statvfs(bs, &ds);
122 if ((error = copyout(&ds, uds, sizeof(*uds))) != 0)
123 return error;
124 uds++;
126 count++;
129 if ((uds != NULL) && (count > maxcount))
130 *retval = maxcount;
131 else
132 *retval = count;
134 return 0;
138 darwin_sys_statfs(struct lwp *l, const struct darwin_sys_statfs_args *uap, register_t *retval)
140 /* {
141 syscallarg(char *) path;
142 syscallarg(struct statfs *) buf;
143 } */
144 struct mount *mp;
145 struct statvfs *bs;
146 struct darwin_statfs ds;
147 struct vnode *vp;
148 int error;
150 error = namei_simple_user(SCARG(uap, path),
151 NSM_FOLLOW_TRYEMULROOT, &vp);
152 if (error != 0)
153 return error;
155 mp = vp->v_mount;
156 bs = &mp->mnt_stat;
157 vrele(vp);
159 if ((error = VFS_STATVFS(mp, bs)) != 0)
160 return error;
162 native_to_darwin_statvfs(bs, &ds);
164 error = copyout(&ds, SCARG(uap, buf), sizeof(ds));
166 return error;
170 static void
171 native_to_darwin_statvfs(const struct statvfs *bs, struct darwin_statfs *ds)
173 long dflags = 0;
174 long sflags = bs->f_flag & MNT_VISFLAGMASK;
176 if (sflags|MNT_RDONLY)
177 dflags |= DARWIN_MNT_RDONLY;
178 if (sflags|MNT_SYNCHRONOUS)
179 dflags |= DARWIN_MNT_SYNCHRONOUS;
180 if (sflags|MNT_NOEXEC)
181 dflags |= DARWIN_MNT_NOEXEC;
182 if (sflags|MNT_NOSUID)
183 dflags |= DARWIN_MNT_NOSUID;
184 if (sflags|MNT_NODEV)
185 dflags |= DARWIN_MNT_NODEV;
186 if (sflags|MNT_UNION)
187 dflags |= DARWIN_MNT_UNION;
188 if (sflags|MNT_ASYNC)
189 dflags |= DARWIN_MNT_ASYNC;
190 if (sflags|MNT_IGNORE)
191 dflags |= DARWIN_MNT_DONTBROWSE;
193 ds->f_otype = 0;
194 ds->f_oflags = dflags & 0xffff;
195 ds->f_bsize = bs->f_bsize;
196 ds->f_iosize = bs->f_iosize;
197 ds->f_blocks = bs->f_blocks;
198 ds->f_bfree = bs->f_bfree;
199 ds->f_bavail = bs->f_bavail;
200 ds->f_files = bs->f_files;
201 ds->f_ffree = bs->f_ffree;
202 (void)memcpy(&ds->f_fsid, &bs->f_fsidx, sizeof(ds->f_fsid));
203 ds->f_owner = bs->f_owner;
204 ds->f_reserved1 = 0;
205 ds->f_type = 0;
206 ds->f_flags = dflags;
207 (void)strlcpy(ds->f_fstypename, bs->f_fstypename, DARWIN_MFSNAMELEN);
208 (void)strlcpy(ds->f_mntonname, bs->f_mntonname, DARWIN_MNAMELEN);
209 (void)strlcpy(ds->f_mntfromname, bs->f_mntfromname, DARWIN_MNAMELEN);
211 return;