xref: /netbsd-src/sys/compat/linux32/common/linux32_stat.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: linux32_stat.c,v 1.16 2009/06/04 17:59:30 njoly 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.16 2009/06/04 17:59:30 njoly 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/linux_syscallargs.h>
69 
70 #include <compat/linux32/common/linux32_types.h>
71 #include <compat/linux32/common/linux32_signal.h>
72 #include <compat/linux32/common/linux32_machdep.h>
73 #include <compat/linux32/common/linux32_sysctl.h>
74 #include <compat/linux32/common/linux32_socketcall.h>
75 #include <compat/linux32/linux32_syscallargs.h>
76 
77 static inline void bsd_to_linux32_stat(struct stat *, struct linux32_stat *);
78 static inline void bsd_to_linux32_stat64(struct stat *, struct linux32_stat64 *);
79 
80 #define linux_fakedev(x,y) (x)
81 
82 static inline void
83 bsd_to_linux32_stat(struct stat *st, struct linux32_stat *st32)
84 {
85 	memset(st32, 0, sizeof(*st32));
86 	st32->lst_dev = linux_fakedev(st->st_dev, 0);
87 	st32->lst_ino = st->st_ino;
88 	st32->lst_mode = st->st_mode;
89 	if (st->st_nlink >= (1 << 15))
90 		st32->lst_nlink = (1 << 15) - 1;
91 	else
92 		st32->lst_nlink = st->st_nlink;
93 	st32->lst_uid = st->st_uid;
94 	st32->lst_gid = st->st_gid;
95 	st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
96 	st32->lst_size = st->st_size;
97 	st32->lst_blksize = st->st_blksize;
98 	st32->lst_blocks = st->st_blocks;
99 	st32->lst_atime = st->st_atime;
100 	st32->lst_mtime = st->st_mtime;
101 	st32->lst_ctime = st->st_ctime;
102 #ifdef LINUX32_STAT_HAS_NSEC
103 	st32->lst_atime_nsec = st->st_atimensec;
104 	st32->lst_mtime_nsec = st->st_mtimensec;
105 	st32->lst_ctime_nsec = st->st_ctimensec;
106 #endif
107 }
108 
109 static inline void
110 bsd_to_linux32_stat64(struct stat *st, struct linux32_stat64 *st32)
111 {
112 	memset(st32, 0, sizeof(*st32));
113 	st32->lst_dev = linux_fakedev(st->st_dev, 0);
114 	st32->lst_ino = st->st_ino;
115 	st32->lst_mode = st->st_mode;
116 	if (st->st_nlink >= (1 << 15))
117 		st32->lst_nlink = (1 << 15) - 1;
118 	else
119 		st32->lst_nlink = st->st_nlink;
120 	st32->lst_uid = st->st_uid;
121 	st32->lst_gid = st->st_gid;
122 	st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
123 	st32->lst_size = st->st_size;
124 	st32->lst_blksize = st->st_blksize;
125 	st32->lst_blocks = st->st_blocks;
126 	st32->lst_atime = st->st_atime;
127 	st32->lst_mtime = st->st_mtime;
128 	st32->lst_ctime = st->st_ctime;
129 #ifdef LINUX32_STAT64_HAS_NSEC
130 	st32->lst_atime_nsec = st->st_atimensec;
131 	st32->lst_mtime_nsec = st->st_mtimensec;
132 	st32->lst_ctime_nsec = st->st_ctimensec;
133 #endif
134 #ifdef LINUX32_STAT64_HAS_BROKEN_ST_INO
135 	st32->__lst_ino = st->st_ino;
136 #endif
137 }
138 
139 int
140 linux32_sys_stat(struct lwp *l, const struct linux32_sys_stat_args *uap, register_t *retval)
141 {
142 	/* {
143 	        syscallarg(netbsd32_charp) path;
144 	        syscallarg(linux32_statp) sp;
145 	} */
146 	int error;
147 	struct stat st;
148 	struct linux32_stat st32;
149 
150 	error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &st);
151 	if (error != 0)
152 		return error;
153 
154 	bsd_to_linux32_stat(&st, &st32);
155 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
156 }
157 
158 int
159 linux32_sys_lstat(struct lwp *l, const struct linux32_sys_lstat_args *uap, register_t *retval)
160 {
161 	/* {
162 	        syscallarg(netbsd32_charp) path;
163 	        syscallarg(linux32_statp) sp;
164 	} */
165 	int error;
166 	struct stat st;
167 	struct linux32_stat st32;
168 
169 	error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &st);
170 	if (error != 0)
171 		return error;
172 
173 	bsd_to_linux32_stat(&st, &st32);
174 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
175 }
176 
177 int
178 linux32_sys_fstat(struct lwp *l, const struct linux32_sys_fstat_args *uap, register_t *retval)
179 {
180 	/* {
181 	        syscallarg(int) fd;
182 	        syscallarg(linux32_statp) sp;
183 	} */
184 	int error;
185 	struct stat st;
186 	struct linux32_stat st32;
187 
188 	error = do_sys_fstat(SCARG(uap, fd), &st);
189 	if (error != 0)
190 		return error;
191 
192 	bsd_to_linux32_stat(&st, &st32);
193 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
194 }
195 
196 int
197 linux32_sys_stat64(struct lwp *l, const struct linux32_sys_stat64_args *uap, register_t *retval)
198 {
199 	/* {
200 	        syscallarg(netbsd32_charp) path;
201 	        syscallarg(linux32_stat64p) sp;
202 	} */
203 	int error;
204 	struct stat st;
205 	struct linux32_stat64 st32;
206 
207 	error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &st);
208 	if (error != 0)
209 		return error;
210 
211 	bsd_to_linux32_stat64(&st, &st32);
212 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
213 }
214 
215 int
216 linux32_sys_lstat64(struct lwp *l, const struct linux32_sys_lstat64_args *uap, register_t *retval)
217 {
218 	/* {
219 	        syscallarg(netbsd32_charp) path;
220 	        syscallarg(linux32_stat64p) sp;
221 	} */
222 	int error;
223 	struct stat st;
224 	struct linux32_stat64 st32;
225 
226 	error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &st);
227 	if (error != 0)
228 		return error;
229 
230 	bsd_to_linux32_stat64(&st, &st32);
231 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
232 }
233 
234 int
235 linux32_sys_fstat64(struct lwp *l, const struct linux32_sys_fstat64_args *uap, register_t *retval)
236 {
237 	/* {
238 	        syscallarg(int) fd;
239 	        syscallarg(linux32_stat64p) sp;
240 	} */
241 	int error;
242 	struct stat st;
243 	struct linux32_stat64 st32;
244 
245 	error = do_sys_fstat(SCARG(uap, fd), &st);
246 	if (error != 0)
247 		return error;
248 
249 	bsd_to_linux32_stat64(&st, &st32);
250 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
251 }
252