1 /* $NetBSD: linux_file64.c,v 1.59 2017/07/28 15:34:06 riastradh 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.59 2017/07/28 15:34:06 riastradh 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 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0); 84 lsp->lst_ino = bsp->st_ino; 85 lsp->lst_mode = (linux_mode_t)bsp->st_mode; 86 if (bsp->st_nlink >= (1 << 15)) 87 lsp->lst_nlink = (1 << 15) - 1; 88 else 89 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink; 90 lsp->lst_uid = bsp->st_uid; 91 lsp->lst_gid = bsp->st_gid; 92 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1); 93 lsp->lst_size = bsp->st_size; 94 lsp->lst_blksize = bsp->st_blksize; 95 lsp->lst_blocks = bsp->st_blocks; 96 lsp->lst_atime = bsp->st_atime; 97 lsp->lst_mtime = bsp->st_mtime; 98 lsp->lst_ctime = bsp->st_ctime; 99 # ifdef LINUX_STAT64_HAS_NSEC 100 lsp->lst_atime_nsec = bsp->st_atimensec; 101 lsp->lst_mtime_nsec = bsp->st_mtimensec; 102 lsp->lst_ctime_nsec = bsp->st_ctimensec; 103 # endif 104 # if LINUX_STAT64_HAS_BROKEN_ST_INO 105 lsp->__lst_ino = (linux_ino_t) bsp->st_ino; 106 # endif 107 } 108 109 /* 110 * The stat functions below are plain sailing. stat and lstat are handled 111 * by one function to avoid code duplication. 112 */ 113 int 114 linux_sys_fstat64(struct lwp *l, const struct linux_sys_fstat64_args *uap, register_t *retval) 115 { 116 /* { 117 syscallarg(int) fd; 118 syscallarg(struct linux_stat64 *) sp; 119 } */ 120 struct linux_stat64 tmplst; 121 struct stat tmpst; 122 int error; 123 124 error = do_sys_fstat(SCARG(uap, fd), &tmpst); 125 if (error != 0) 126 return error; 127 128 bsd_to_linux_stat(&tmpst, &tmplst); 129 130 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst); 131 } 132 133 static int 134 linux_do_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval, int flags) 135 { 136 struct linux_stat64 tmplst; 137 struct stat tmpst; 138 int error; 139 140 error = do_sys_stat(SCARG(uap, path), flags, &tmpst); 141 if (error != 0) 142 return error; 143 144 bsd_to_linux_stat(&tmpst, &tmplst); 145 146 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst); 147 } 148 149 int 150 linux_sys_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval) 151 { 152 /* { 153 syscallarg(const char *) path; 154 syscallarg(struct linux_stat64 *) sp; 155 } */ 156 157 return linux_do_stat64(l, uap, retval, FOLLOW); 158 } 159 160 int 161 linux_sys_lstat64(struct lwp *l, const struct linux_sys_lstat64_args *uap, register_t *retval) 162 { 163 /* { 164 syscallarg(const char *) path; 165 syscallarg(struct linux_stat64 *) sp; 166 } */ 167 168 return linux_do_stat64(l, (const void *)uap, retval, NOFOLLOW); 169 } 170 171 int 172 linux_sys_fstatat64(struct lwp *l, const struct linux_sys_fstatat64_args *uap, register_t *retval) 173 { 174 /* { 175 syscallarg(int) fd; 176 syscallarg(const char *) path; 177 syscallarg(struct linux_stat64 *) sp; 178 syscallarg(int) flag; 179 } */ 180 struct linux_stat64 tmplst; 181 struct stat tmpst; 182 int error, nd_flag; 183 184 if (SCARG(uap, flag) & LINUX_AT_SYMLINK_NOFOLLOW) 185 nd_flag = NOFOLLOW; 186 else 187 nd_flag = FOLLOW; 188 189 error = do_sys_statat(l, SCARG(uap, fd), SCARG(uap, path), nd_flag, &tmpst); 190 if (error != 0) 191 return error; 192 193 bsd_to_linux_stat(&tmpst, &tmplst); 194 195 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst); 196 } 197 198 #ifndef __alpha__ 199 int 200 linux_sys_truncate64(struct lwp *l, const struct linux_sys_truncate64_args *uap, register_t *retval) 201 { 202 /* { 203 syscallarg(const char *) path; 204 syscallarg(off_t) length; 205 } */ 206 struct sys_truncate_args ta; 207 208 /* Linux doesn't have the 'pad' pseudo-parameter */ 209 SCARG(&ta, path) = SCARG(uap, path); 210 SCARG(&ta, PAD) = 0; 211 SCARG(&ta, length) = SCARG(uap, length); 212 213 return sys_truncate(l, &ta, retval); 214 } 215 216 int 217 linux_sys_ftruncate64(struct lwp *l, const struct linux_sys_ftruncate64_args *uap, register_t *retval) 218 { 219 /* { 220 syscallarg(unsigned int) fd; 221 syscallarg(off_t) length; 222 } */ 223 struct sys_ftruncate_args ta; 224 225 /* Linux doesn't have the 'pad' pseudo-parameter */ 226 SCARG(&ta, fd) = SCARG(uap, fd); 227 SCARG(&ta, PAD) = 0; 228 SCARG(&ta, length) = SCARG(uap, length); 229 230 return sys_ftruncate(l, &ta, retval); 231 } 232 #endif /* __alpha__ */ 233 234 /* 235 * Linux 'readdir' call. This code is mostly taken from the 236 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 237 * an attempt has been made to keep it a little cleaner. 238 * 239 * The d_off field contains the offset of the next valid entry, 240 * unless the older Linux getdents(2), which used to have it set 241 * to the offset of the entry itself. This function also doesn't 242 * need to deal with the old count == 1 glibc problem. 243 * 244 * Read in BSD-style entries, convert them, and copy them out. 245 * 246 * Note that this doesn't handle union-mounted filesystems. 247 */ 248 int 249 linux_sys_getdents64(struct lwp *l, const struct linux_sys_getdents64_args *uap, register_t *retval) 250 { 251 /* { 252 syscallarg(int) fd; 253 syscallarg(struct linux_dirent64 *) dent; 254 syscallarg(unsigned int) count; 255 } */ 256 struct dirent *bdp; 257 struct vnode *vp; 258 char *inp, *tbuf; /* BSD-format */ 259 int len, reclen; /* BSD-format */ 260 char *outp; /* Linux-format */ 261 int resid, linux_reclen = 0; /* Linux-format */ 262 file_t *fp; 263 struct uio auio; 264 struct iovec aiov; 265 struct linux_dirent64 idb; 266 off_t off; /* true file offset */ 267 int buflen, error, eofflag, nbytes; 268 struct vattr va; 269 off_t *cookiebuf = NULL, *cookie; 270 int ncookies; 271 272 /* fd_getvnode() will use the descriptor for us */ 273 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 274 return (error); 275 276 if ((fp->f_flag & FREAD) == 0) { 277 error = EBADF; 278 goto out1; 279 } 280 281 vp = (struct vnode *)fp->f_data; 282 if (vp->v_type != VDIR) { 283 error = ENOTDIR; 284 goto out1; 285 } 286 287 vn_lock(vp, LK_SHARED | LK_RETRY); 288 error = VOP_GETATTR(vp, &va, l->l_cred); 289 VOP_UNLOCK(vp); 290 if (error) 291 goto out1; 292 293 nbytes = SCARG(uap, count); 294 buflen = min(MAXBSIZE, nbytes); 295 if (buflen < va.va_blocksize) 296 buflen = va.va_blocksize; 297 tbuf = malloc(buflen, M_TEMP, M_WAITOK); 298 299 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 300 off = fp->f_offset; 301 again: 302 aiov.iov_base = tbuf; 303 aiov.iov_len = buflen; 304 auio.uio_iov = &aiov; 305 auio.uio_iovcnt = 1; 306 auio.uio_rw = UIO_READ; 307 auio.uio_resid = buflen; 308 auio.uio_offset = off; 309 UIO_SETUP_SYSSPACE(&auio); 310 /* 311 * First we read into the malloc'ed buffer, then 312 * we massage it into user space, one record at a time. 313 */ 314 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 315 &ncookies); 316 if (error) 317 goto out; 318 319 inp = tbuf; 320 outp = (void *)SCARG(uap, dent); 321 resid = nbytes; 322 if ((len = buflen - auio.uio_resid) == 0) 323 goto eof; 324 325 for (cookie = cookiebuf; len > 0; len -= reclen) { 326 bdp = (struct dirent *)inp; 327 reclen = bdp->d_reclen; 328 if (reclen & 3) { 329 error = EIO; 330 goto out; 331 } 332 if (bdp->d_fileno == 0) { 333 inp += reclen; /* it is a hole; squish it out */ 334 if (cookie) 335 off = *cookie++; 336 else 337 off += reclen; 338 continue; 339 } 340 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); 341 if (reclen > len || resid < linux_reclen) { 342 /* entry too big for buffer, so just stop */ 343 outp++; 344 break; 345 } 346 if (cookie) 347 off = *cookie++; /* each entry points to next */ 348 else 349 off += reclen; 350 /* 351 * Massage in place to make a Linux-shaped dirent (otherwise 352 * we have to worry about touching user memory outside of 353 * the copyout() call). 354 */ 355 idb.d_ino = bdp->d_fileno; 356 idb.d_type = bdp->d_type; 357 idb.d_off = off; 358 idb.d_reclen = (u_short)linux_reclen; 359 memcpy(idb.d_name, bdp->d_name, MIN(sizeof(idb.d_name), 360 bdp->d_namlen + 1)); 361 if ((error = copyout((void *)&idb, outp, linux_reclen))) 362 goto out; 363 /* advance past this real entry */ 364 inp += reclen; 365 /* advance output past Linux-shaped entry */ 366 outp += linux_reclen; 367 resid -= linux_reclen; 368 } 369 370 /* if we squished out the whole block, try again */ 371 if (outp == (void *)SCARG(uap, dent)) { 372 if (cookiebuf) 373 free(cookiebuf, M_TEMP); 374 cookiebuf = NULL; 375 goto again; 376 } 377 fp->f_offset = off; /* update the vnode offset */ 378 379 eof: 380 *retval = nbytes - resid; 381 out: 382 VOP_UNLOCK(vp); 383 if (cookiebuf) 384 free(cookiebuf, M_TEMP); 385 free(tbuf, M_TEMP); 386 out1: 387 fd_putfile(SCARG(uap, fd)); 388 return error; 389 } 390