linux_file64.c revision 1.63 1 /* $NetBSD: linux_file64.c,v 1.63 2021/09/21 09:24:15 rin Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
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.
18 *
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.
30 */
31
32 /*
33 * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.63 2021/09/21 09:24:15 rin Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/dirent.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/filedesc.h>
47 #include <sys/ioctl.h>
48 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <sys/namei.h>
52 #include <sys/vfs_syscalls.h>
53 #include <sys/vnode.h>
54 #include <sys/tty.h>
55 #include <sys/conf.h>
56
57 #include <sys/syscallargs.h>
58
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_fcntl.h>
62 #include <compat/linux/common/linux_util.h>
63 #include <compat/linux/common/linux_machdep.h>
64 #include <compat/linux/common/linux_dirent.h>
65 #include <compat/linux/common/linux_ipc.h>
66 #include <compat/linux/common/linux_sem.h>
67
68 #include <compat/linux/linux_syscallargs.h>
69
70 static void bsd_to_linux_stat(struct stat *, struct linux_stat64 *);
71
72 /*
73 * Convert a NetBSD stat structure to a Linux stat structure.
74 * Only the order of the fields and the padding in the structure
75 * is different. linux_fakedev is a machine-dependent function
76 * which optionally converts device driver major/minor numbers
77 * (XXX horrible, but what can you do against code that compares
78 * things against constant major device numbers? sigh)
79 */
80 static void
81 bsd_to_linux_stat(struct stat *bsp, struct linux_stat64 *lsp)
82 {
83 memset(lsp, 0, sizeof(*lsp));
84 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
85 lsp->lst_ino = bsp->st_ino;
86 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
87 if (bsp->st_nlink >= (1 << 15))
88 lsp->lst_nlink = (1 << 15) - 1;
89 else
90 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
91 lsp->lst_uid = bsp->st_uid;
92 lsp->lst_gid = bsp->st_gid;
93 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
94 lsp->lst_size = bsp->st_size;
95 lsp->lst_blksize = bsp->st_blksize;
96 lsp->lst_blocks = bsp->st_blocks;
97 lsp->lst_atime = bsp->st_atime;
98 lsp->lst_mtime = bsp->st_mtime;
99 lsp->lst_ctime = bsp->st_ctime;
100 # ifdef LINUX_STAT64_HAS_NSEC
101 lsp->lst_atime_nsec = bsp->st_atimensec;
102 lsp->lst_mtime_nsec = bsp->st_mtimensec;
103 lsp->lst_ctime_nsec = bsp->st_ctimensec;
104 # endif
105 # if LINUX_STAT64_HAS_BROKEN_ST_INO
106 lsp->__lst_ino = (linux_ino_t) bsp->st_ino;
107 # endif
108 }
109
110 /*
111 * The stat functions below are plain sailing. stat and lstat are handled
112 * by one function to avoid code duplication.
113 */
114 int
115 linux_sys_fstat64(struct lwp *l, const struct linux_sys_fstat64_args *uap, register_t *retval)
116 {
117 /* {
118 syscallarg(int) fd;
119 syscallarg(struct linux_stat64 *) sp;
120 } */
121 struct linux_stat64 tmplst;
122 struct stat tmpst;
123 int error;
124
125 error = do_sys_fstat(SCARG(uap, fd), &tmpst);
126 if (error != 0)
127 return error;
128
129 bsd_to_linux_stat(&tmpst, &tmplst);
130
131 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
132 }
133
134 static int
135 linux_do_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval, int flags)
136 {
137 struct linux_stat64 tmplst;
138 struct stat tmpst;
139 int error;
140
141 error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
142 if (error != 0)
143 return error;
144
145 bsd_to_linux_stat(&tmpst, &tmplst);
146
147 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
148 }
149
150 int
151 linux_sys_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval)
152 {
153 /* {
154 syscallarg(const char *) path;
155 syscallarg(struct linux_stat64 *) sp;
156 } */
157
158 return linux_do_stat64(l, uap, retval, FOLLOW);
159 }
160
161 int
162 linux_sys_lstat64(struct lwp *l, const struct linux_sys_lstat64_args *uap, register_t *retval)
163 {
164 /* {
165 syscallarg(const char *) path;
166 syscallarg(struct linux_stat64 *) sp;
167 } */
168
169 return linux_do_stat64(l, (const void *)uap, retval, NOFOLLOW);
170 }
171
172 int
173 linux_sys_fstatat64(struct lwp *l, const struct linux_sys_fstatat64_args *uap, register_t *retval)
174 {
175 /* {
176 syscallarg(int) fd;
177 syscallarg(const char *) path;
178 syscallarg(struct linux_stat64 *) sp;
179 syscallarg(int) flag;
180 } */
181 struct linux_stat64 tmplst;
182 struct stat tmpst;
183 struct vnode *vp;
184 int error, nd_flag, fd;
185 uint8_t c;
186
187 if (SCARG(uap, flag) & LINUX_AT_EMPTY_PATH) {
188 /*
189 * If path is null string:
190 */
191 error = ufetch_8(SCARG(uap, path), &c);
192 if (error != 0)
193 return error;
194 if (c == '\0') {
195 fd = SCARG(uap, fd);
196 if (fd == AT_FDCWD) {
197 /*
198 * operate on current directory
199 */
200 vp = l->l_proc->p_cwdi->cwdi_cdir;
201 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
202 error = vn_stat(vp, &tmpst);
203 VOP_UNLOCK(vp);
204 } else {
205 /*
206 * operate on fd
207 */
208 error = do_sys_fstat(fd, &tmpst);
209 }
210 if (error != 0)
211 return error;
212 goto done;
213 }
214 }
215
216 if (SCARG(uap, flag) & LINUX_AT_SYMLINK_NOFOLLOW)
217 nd_flag = NOFOLLOW;
218 else
219 nd_flag = FOLLOW;
220
221 error = do_sys_statat(l, SCARG(uap, fd), SCARG(uap, path), nd_flag, &tmpst);
222 if (error != 0)
223 return error;
224
225 done:
226 bsd_to_linux_stat(&tmpst, &tmplst);
227
228 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
229 }
230
231 #ifndef __alpha__
232 int
233 linux_sys_truncate64(struct lwp *l, const struct linux_sys_truncate64_args *uap, register_t *retval)
234 {
235 /* {
236 syscallarg(const char *) path;
237 syscallarg(off_t) length;
238 } */
239 struct sys_truncate_args ta;
240
241 /* Linux doesn't have the 'pad' pseudo-parameter */
242 SCARG(&ta, path) = SCARG(uap, path);
243 SCARG(&ta, PAD) = 0;
244 SCARG(&ta, length) = SCARG(uap, length);
245
246 return sys_truncate(l, &ta, retval);
247 }
248
249 int
250 linux_sys_ftruncate64(struct lwp *l, const struct linux_sys_ftruncate64_args *uap, register_t *retval)
251 {
252 /* {
253 syscallarg(unsigned int) fd;
254 syscallarg(off_t) length;
255 } */
256 struct sys_ftruncate_args ta;
257
258 /* Linux doesn't have the 'pad' pseudo-parameter */
259 SCARG(&ta, fd) = SCARG(uap, fd);
260 SCARG(&ta, PAD) = 0;
261 SCARG(&ta, length) = SCARG(uap, length);
262
263 return sys_ftruncate(l, &ta, retval);
264 }
265 #endif /* __alpha__ */
266
267 /*
268 * Linux 'readdir' call. This code is mostly taken from the
269 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
270 * an attempt has been made to keep it a little cleaner.
271 *
272 * The d_off field contains the offset of the next valid entry,
273 * unless the older Linux getdents(2), which used to have it set
274 * to the offset of the entry itself. This function also doesn't
275 * need to deal with the old count == 1 glibc problem.
276 *
277 * Read in BSD-style entries, convert them, and copy them out.
278 *
279 * Note that this doesn't handle union-mounted filesystems.
280 */
281 int
282 linux_sys_getdents64(struct lwp *l, const struct linux_sys_getdents64_args *uap, register_t *retval)
283 {
284 /* {
285 syscallarg(int) fd;
286 syscallarg(struct linux_dirent64 *) dent;
287 syscallarg(unsigned int) count;
288 } */
289 struct dirent *bdp;
290 struct vnode *vp;
291 char *inp, *tbuf; /* BSD-format */
292 int len, reclen; /* BSD-format */
293 char *outp; /* Linux-format */
294 int resid, linux_reclen = 0; /* Linux-format */
295 file_t *fp;
296 struct uio auio;
297 struct iovec aiov;
298 struct linux_dirent64 idb;
299 off_t off; /* true file offset */
300 int buflen, error, eofflag, nbytes;
301 struct vattr va;
302 off_t *cookiebuf = NULL, *cookie;
303 int ncookies;
304
305 /* fd_getvnode() will use the descriptor for us */
306 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
307 return (error);
308
309 if ((fp->f_flag & FREAD) == 0) {
310 error = EBADF;
311 goto out1;
312 }
313
314 vp = (struct vnode *)fp->f_data;
315 if (vp->v_type != VDIR) {
316 error = ENOTDIR;
317 goto out1;
318 }
319
320 vn_lock(vp, LK_SHARED | LK_RETRY);
321 error = VOP_GETATTR(vp, &va, l->l_cred);
322 VOP_UNLOCK(vp);
323 if (error)
324 goto out1;
325
326 nbytes = SCARG(uap, count);
327 buflen = uimin(MAXBSIZE, nbytes);
328 if (buflen < va.va_blocksize)
329 buflen = va.va_blocksize;
330 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
331
332 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
333 off = fp->f_offset;
334 again:
335 aiov.iov_base = tbuf;
336 aiov.iov_len = buflen;
337 auio.uio_iov = &aiov;
338 auio.uio_iovcnt = 1;
339 auio.uio_rw = UIO_READ;
340 auio.uio_resid = buflen;
341 auio.uio_offset = off;
342 UIO_SETUP_SYSSPACE(&auio);
343 /*
344 * First we read into the malloc'ed buffer, then
345 * we massage it into user space, one record at a time.
346 */
347 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
348 &ncookies);
349 if (error)
350 goto out;
351
352 inp = tbuf;
353 outp = (void *)SCARG(uap, dent);
354 resid = nbytes;
355 if ((len = buflen - auio.uio_resid) == 0)
356 goto eof;
357
358 for (cookie = cookiebuf; len > 0; len -= reclen) {
359 bdp = (struct dirent *)inp;
360 reclen = bdp->d_reclen;
361 if (reclen & 3) {
362 error = EIO;
363 goto out;
364 }
365 if (bdp->d_fileno == 0) {
366 inp += reclen; /* it is a hole; squish it out */
367 if (cookie)
368 off = *cookie++;
369 else
370 off += reclen;
371 continue;
372 }
373 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
374 if (reclen > len || resid < linux_reclen) {
375 /* entry too big for buffer, so just stop */
376 outp++;
377 break;
378 }
379 if (cookie)
380 off = *cookie++; /* each entry points to next */
381 else
382 off += reclen;
383 /*
384 * Massage in place to make a Linux-shaped dirent (otherwise
385 * we have to worry about touching user memory outside of
386 * the copyout() call).
387 */
388 memset(&idb, 0, sizeof(idb));
389 idb.d_ino = bdp->d_fileno;
390 idb.d_type = bdp->d_type;
391 idb.d_off = off;
392 idb.d_reclen = (u_short)linux_reclen;
393 memcpy(idb.d_name, bdp->d_name, MIN(sizeof(idb.d_name),
394 bdp->d_namlen + 1));
395 if ((error = copyout((void *)&idb, outp, linux_reclen)))
396 goto out;
397 /* advance past this real entry */
398 inp += reclen;
399 /* advance output past Linux-shaped entry */
400 outp += linux_reclen;
401 resid -= linux_reclen;
402 }
403
404 /* if we squished out the whole block, try again */
405 if (outp == (void *)SCARG(uap, dent)) {
406 if (cookiebuf)
407 free(cookiebuf, M_TEMP);
408 cookiebuf = NULL;
409 goto again;
410 }
411 fp->f_offset = off; /* update the vnode offset */
412
413 eof:
414 *retval = nbytes - resid;
415 out:
416 VOP_UNLOCK(vp);
417 if (cookiebuf)
418 free(cookiebuf, M_TEMP);
419 free(tbuf, M_TEMP);
420 out1:
421 fd_putfile(SCARG(uap, fd));
422 return error;
423 }
424