1/* $NetBSD: linux32_stat.c,v 1.17 2013/11/18 01:32:52 chs Exp $ */
2
3/*-
4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
36__KERNEL_RCSID(0, "$NetBSD: linux32_stat.c,v 1.17 2013/11/18 01:32:52 chs Exp $");
37
38#include <sys/types.h>
39#include <sys/param.h>
40#include <sys/fstypes.h>
41#include <sys/signal.h>
42#include <sys/dirent.h>
43#include <sys/kernel.h>
44#include <sys/namei.h>
45#include <sys/fcntl.h>
46#include <sys/filedesc.h>
47#include <sys/select.h>
48#include <sys/proc.h>
49#include <sys/ucred.h>
50#include <sys/swap.h>
51#include <sys/vfs_syscalls.h>
52
53#include <machine/types.h>
54
55#include <sys/syscallargs.h>
56
57#include <compat/netbsd32/netbsd32.h>
58#include <compat/netbsd32/netbsd32_conv.h>
59#include <compat/netbsd32/netbsd32_syscallargs.h>
60
61#include <compat/linux/common/linux_types.h>
62#include <compat/linux/common/linux_signal.h>
63#include <compat/linux/common/linux_machdep.h>
64#include <compat/linux/common/linux_misc.h>
65#include <compat/linux/common/linux_oldolduname.h>
66#include <compat/linux/common/linux_ipc.h>
67#include <compat/linux/common/linux_sem.h>
68#include <compat/linux/common/linux_fcntl.h>
69#include <compat/linux/linux_syscallargs.h>
70
71#include <compat/linux32/common/linux32_types.h>
72#include <compat/linux32/common/linux32_signal.h>
73#include <compat/linux32/common/linux32_machdep.h>
74#include <compat/linux32/common/linux32_sysctl.h>
75#include <compat/linux32/common/linux32_socketcall.h>
76#include <compat/linux32/linux32_syscallargs.h>
77
78static inline void bsd_to_linux32_stat(struct stat *, struct linux32_stat *);
79static inline void bsd_to_linux32_stat64(struct stat *, struct linux32_stat64 *);
80
81#define linux_fakedev(x,y) (x)
82
83static inline void
84bsd_to_linux32_stat(struct stat *st, struct linux32_stat *st32)
85{
86 memset(st32, 0, sizeof(*st32));
87 st32->lst_dev = linux_fakedev(st->st_dev, 0);
88 st32->lst_ino = st->st_ino;
89 st32->lst_mode = st->st_mode;
90 if (st->st_nlink >= (1 << 15))
91 st32->lst_nlink = (1 << 15) - 1;
92 else
93 st32->lst_nlink = st->st_nlink;
94 st32->lst_uid = st->st_uid;
95 st32->lst_gid = st->st_gid;
96 st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
97 st32->lst_size = st->st_size;
98 st32->lst_blksize = st->st_blksize;
99 st32->lst_blocks = st->st_blocks;
100 st32->lst_atime = st->st_atime;
101 st32->lst_mtime = st->st_mtime;
102 st32->lst_ctime = st->st_ctime;
103#ifdef LINUX32_STAT_HAS_NSEC
104 st32->lst_atime_nsec = st->st_atimensec;
105 st32->lst_mtime_nsec = st->st_mtimensec;
106 st32->lst_ctime_nsec = st->st_ctimensec;
107#endif
108}
109
110static inline void
111bsd_to_linux32_stat64(struct stat *st, struct linux32_stat64 *st32)
112{
113 memset(st32, 0, sizeof(*st32));
114 st32->lst_dev = linux_fakedev(st->st_dev, 0);
115 st32->lst_ino = st->st_ino;
116 st32->lst_mode = st->st_mode;
117 if (st->st_nlink >= (1 << 15))
118 st32->lst_nlink = (1 << 15) - 1;
119 else
120 st32->lst_nlink = st->st_nlink;
121 st32->lst_uid = st->st_uid;
122 st32->lst_gid = st->st_gid;
123 st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
124 st32->lst_size = st->st_size;
125 st32->lst_blksize = st->st_blksize;
126 st32->lst_blocks = st->st_blocks;
127 st32->lst_atime = st->st_atime;
128 st32->lst_mtime = st->st_mtime;
129 st32->lst_ctime = st->st_ctime;
130#ifdef LINUX32_STAT64_HAS_NSEC
131 st32->lst_atime_nsec = st->st_atimensec;
132 st32->lst_mtime_nsec = st->st_mtimensec;
133 st32->lst_ctime_nsec = st->st_ctimensec;
134#endif
135#ifdef LINUX32_STAT64_HAS_BROKEN_ST_INO
136 st32->__lst_ino = st->st_ino;
137#endif
138}
139
140int
141linux32_sys_stat(struct lwp *l, const struct linux32_sys_stat_args *uap, register_t *retval)
142{
143 /* {
144 syscallarg(netbsd32_charp) path;
145 syscallarg(linux32_statp) sp;
146 } */
147 int error;
148 struct stat st;
149 struct linux32_stat st32;
150
151 error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &st);
152 if (error != 0)
153 return error;
154
155 bsd_to_linux32_stat(&st, &st32);
156 return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
157}
158
159int
160linux32_sys_lstat(struct lwp *l, const struct linux32_sys_lstat_args *uap, register_t *retval)
161{
162 /* {
163 syscallarg(netbsd32_charp) path;
164 syscallarg(linux32_statp) sp;
165 } */
166 int error;
167 struct stat st;
168 struct linux32_stat st32;
169
170 error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &st);
171 if (error != 0)
172 return error;
173
174 bsd_to_linux32_stat(&st, &st32);
175 return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
176}
177
178int
179linux32_sys_fstat(struct lwp *l, const struct linux32_sys_fstat_args *uap, register_t *retval)
180{
181 /* {
182 syscallarg(int) fd;
183 syscallarg(linux32_statp) sp;
184 } */
185 int error;
186 struct stat st;
187 struct linux32_stat st32;
188
189 error = do_sys_fstat(SCARG(uap, fd), &st);
190 if (error != 0)
191 return error;
192
193 bsd_to_linux32_stat(&st, &st32);
194 return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
195}
196
197int
198linux32_sys_stat64(struct lwp *l, const struct linux32_sys_stat64_args *uap, register_t *retval)
199{
200 /* {
201 syscallarg(netbsd32_charp) path;
202 syscallarg(linux32_stat64p) sp;
203 } */
204 int error;
205 struct stat st;
206 struct linux32_stat64 st32;
207
208 error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &st);
209 if (error != 0)
210 return error;
211
212 bsd_to_linux32_stat64(&st, &st32);
213 return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
214}
215
216int
217linux32_sys_lstat64(struct lwp *l, const struct linux32_sys_lstat64_args *uap, register_t *retval)
218{
219 /* {
220 syscallarg(netbsd32_charp) path;
221 syscallarg(linux32_stat64p) sp;
222 } */
223 int error;
224 struct stat st;
225 struct linux32_stat64 st32;
226
227 error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &st);
228 if (error != 0)
229 return error;
230
231 bsd_to_linux32_stat64(&st, &st32);
232 return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
233}
234
235int
236linux32_sys_fstat64(struct lwp *l, const struct linux32_sys_fstat64_args *uap, register_t *retval)
237{
238 /* {
239 syscallarg(int) fd;
240 syscallarg(linux32_stat64p) sp;
241 } */
242 int error;
243 struct stat st;
244 struct linux32_stat64 st32;
245
246 error = do_sys_fstat(SCARG(uap, fd), &st);
247 if (error != 0)
248 return error;
249
250 bsd_to_linux32_stat64(&st, &st32);
251 return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
252}
253
254int
255linux32_sys_fstatat64(struct lwp *l, const struct linux32_sys_fstatat64_args *uap, register_t *retval)
256{
257 /* {
258 syscallarg(int) fd;
259 syscallarg(netbsd32_charp) path;
260 syscallarg(linux32_stat64p) sp;
261 syscallarg(int) flag;
262 } */
263 int error, nd_flag;
264 struct stat st;
265 struct linux32_stat64 st32;
266
267 if (SCARG(uap, flag) & LINUX_AT_SYMLINK_NOFOLLOW)
268 nd_flag = NOFOLLOW;
269 else
270 nd_flag = FOLLOW;
271
272 error = do_sys_statat(l, SCARG(uap, fd), SCARG_P32(uap, path), nd_flag, &st);
273 if (error != 0)
274 return error;
275
276 bsd_to_linux32_stat64(&st, &st32);
277
278 return copyout(&st32, SCARG_P32(uap, sp), sizeof st32);
279}
280