linux_file64.c revision 1.12
1/* $NetBSD: linux_file64.c,v 1.12 2002/05/10 14:51:26 tron Exp $ */ 2 3/*- 4 * Copyright (c) 1995, 1998, 2000 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39/* 40 * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones. 41 */ 42 43#include <sys/cdefs.h> 44__KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.12 2002/05/10 14:51:26 tron Exp $"); 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/namei.h> 49#include <sys/proc.h> 50#include <sys/dirent.h> 51#include <sys/file.h> 52#include <sys/stat.h> 53#include <sys/filedesc.h> 54#include <sys/ioctl.h> 55#include <sys/kernel.h> 56#include <sys/mount.h> 57#include <sys/malloc.h> 58#include <sys/vnode.h> 59#include <sys/tty.h> 60#include <sys/conf.h> 61 62#include <sys/syscallargs.h> 63 64#include <compat/linux/common/linux_types.h> 65#include <compat/linux/common/linux_signal.h> 66#include <compat/linux/common/linux_fcntl.h> 67#include <compat/linux/common/linux_util.h> 68#include <compat/linux/common/linux_machdep.h> 69#include <compat/linux/common/linux_dirent.h> 70 71#include <compat/linux/linux_syscallargs.h> 72 73static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *)); 74static int linux_do_stat64 __P((struct proc *, void *, register_t *, int)); 75 76/* 77 * Convert a NetBSD stat structure to a Linux stat structure. 78 * Only the order of the fields and the padding in the structure 79 * is different. linux_fakedev is a machine-dependent function 80 * which optionally converts device driver major/minor numbers 81 * (XXX horrible, but what can you do against code that compares 82 * things against constant major device numbers? sigh) 83 */ 84static void 85bsd_to_linux_stat(bsp, lsp) 86 struct stat *bsp; 87 struct linux_stat64 *lsp; 88{ 89 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0); 90 lsp->lst_ino = bsp->st_ino; 91 lsp->lst_mode = (linux_mode_t)bsp->st_mode; 92 if (bsp->st_nlink >= (1 << 15)) 93 lsp->lst_nlink = (1 << 15) - 1; 94 else 95 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink; 96 lsp->lst_uid = bsp->st_uid; 97 lsp->lst_gid = bsp->st_gid; 98 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1); 99 lsp->lst_size = bsp->st_size; 100 lsp->lst_blksize = bsp->st_blksize; 101 lsp->lst_blocks = bsp->st_blocks; 102 lsp->lst_atime = bsp->st_atime; 103 lsp->lst_mtime = bsp->st_mtime; 104 lsp->lst_ctime = bsp->st_ctime; 105 lsp->lst_ino64 = bsp->st_ino; 106} 107 108/* 109 * The stat functions below are plain sailing. stat and lstat are handled 110 * by one function to avoid code duplication. 111 */ 112int 113linux_sys_fstat64(p, v, retval) 114 struct proc *p; 115 void *v; 116 register_t *retval; 117{ 118 struct linux_sys_fstat64_args /* { 119 syscallarg(int) fd; 120 syscallarg(struct linux_stat64 *) sp; 121 } */ *uap = v; 122 struct sys___fstat13_args fsa; 123 struct linux_stat64 tmplst; 124 struct stat *st,tmpst; 125 caddr_t sg; 126 int error; 127 128 sg = stackgap_init(p, 0); 129 130 st = stackgap_alloc(p, &sg, sizeof (struct stat)); 131 132 SCARG(&fsa, fd) = SCARG(uap, fd); 133 SCARG(&fsa, sb) = st; 134 135 if ((error = sys___fstat13(p, &fsa, retval))) 136 return error; 137 138 if ((error = copyin(st, &tmpst, sizeof tmpst))) 139 return error; 140 141 bsd_to_linux_stat(&tmpst, &tmplst); 142 143 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst))) 144 return error; 145 146 return 0; 147} 148 149static int 150linux_do_stat64(p, v, retval, dolstat) 151 struct proc *p; 152 void *v; 153 register_t *retval; 154 int dolstat; 155{ 156 struct sys___stat13_args sa; 157 struct linux_stat64 tmplst; 158 struct stat *st, tmpst; 159 caddr_t sg; 160 int error; 161 struct linux_sys_stat64_args *uap = v; 162 163 sg = stackgap_init(p, 0); 164 st = stackgap_alloc(p, &sg, sizeof (struct stat)); 165 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 166 167 SCARG(&sa, ub) = st; 168 SCARG(&sa, path) = SCARG(uap, path); 169 170 if ((error = (dolstat ? sys___lstat13(p, &sa, retval) : 171 sys___stat13(p, &sa, retval)))) 172 return error; 173 174 if ((error = copyin(st, &tmpst, sizeof tmpst))) 175 return error; 176 177 bsd_to_linux_stat(&tmpst, &tmplst); 178 179 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst))) 180 return error; 181 182 return 0; 183} 184 185int 186linux_sys_stat64(p, v, retval) 187 struct proc *p; 188 void *v; 189 register_t *retval; 190{ 191 struct linux_sys_stat64_args /* { 192 syscallarg(const char *) path; 193 syscallarg(struct linux_stat64 *) sp; 194 } */ *uap = v; 195 196 return linux_do_stat64(p, uap, retval, 0); 197} 198 199int 200linux_sys_lstat64(p, v, retval) 201 struct proc *p; 202 void *v; 203 register_t *retval; 204{ 205 struct linux_sys_lstat64_args /* { 206 syscallarg(const char *) path; 207 syscallarg(struct linux_stat64 *) sp; 208 } */ *uap = v; 209 210 return linux_do_stat64(p, uap, retval, 1); 211} 212 213int 214linux_sys_truncate64(p, v, retval) 215 struct proc *p; 216 void *v; 217 register_t *retval; 218{ 219 struct linux_sys_truncate64_args /* { 220 syscallarg(const char *) path; 221 syscallarg(off_t) length; 222 } */ *uap = v; 223 caddr_t sg = stackgap_init(p, 0); 224 225 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 226 227 return sys_truncate(p, uap, retval); 228} 229 230#if defined(__mips__) || defined(__i386__) /* powerpc could use it too */ 231static void bsd_to_linux_flock64 __P((struct linux_flock64 *, 232 const struct flock *)); 233static void linux_to_bsd_flock64 __P((struct flock *, 234 const struct linux_flock64 *)); 235 236static void 237bsd_to_linux_flock64(lfp, bfp) 238 struct linux_flock64 *lfp; 239 const struct flock *bfp; 240{ 241 242 lfp->l_start = bfp->l_start; 243 lfp->l_len = bfp->l_len; 244 lfp->l_pid = bfp->l_pid; 245 lfp->l_whence = bfp->l_whence; 246 switch (bfp->l_type) { 247 case F_RDLCK: 248 lfp->l_type = LINUX_F_RDLCK; 249 break; 250 case F_UNLCK: 251 lfp->l_type = LINUX_F_UNLCK; 252 break; 253 case F_WRLCK: 254 lfp->l_type = LINUX_F_WRLCK; 255 break; 256 } 257} 258 259static void 260linux_to_bsd_flock64(bfp, lfp) 261 struct flock *bfp; 262 const struct linux_flock64 *lfp; 263{ 264 265 bfp->l_start = lfp->l_start; 266 bfp->l_len = lfp->l_len; 267 bfp->l_pid = lfp->l_pid; 268 bfp->l_whence = lfp->l_whence; 269 switch (lfp->l_type) { 270 case LINUX_F_RDLCK: 271 bfp->l_type = F_RDLCK; 272 break; 273 case LINUX_F_UNLCK: 274 bfp->l_type = F_UNLCK; 275 break; 276 case LINUX_F_WRLCK: 277 bfp->l_type = F_WRLCK; 278 break; 279 } 280} 281int 282linux_sys_fcntl64(p, v, retval) 283 struct proc *p; 284 void *v; 285 register_t *retval; 286{ 287 struct linux_sys_fcntl64_args /* { 288 syscallarg(int) fd; 289 syscallarg(int) cmd; 290 syscallarg(void *) arg; 291 } */ *uap = v; 292 struct sys_fcntl_args fca; 293 struct linux_flock64 lfl; 294 struct flock bfl, *bfp; 295 int error; 296 caddr_t sg; 297 void *arg = SCARG(uap, arg); 298 int cmd = SCARG(uap, cmd); 299 int fd = SCARG(uap, fd); 300 301 switch (cmd) { 302 case LINUX_F_GETLK64: 303 sg = stackgap_init(p, 0); 304 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp); 305 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0) 306 return error; 307 linux_to_bsd_flock64(&bfl, &lfl); 308 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0) 309 return error; 310 SCARG(&fca, fd) = fd; 311 SCARG(&fca, cmd) = F_GETLK; 312 SCARG(&fca, arg) = bfp; 313 if ((error = sys_fcntl(p, &fca, retval)) != 0) 314 return error; 315 if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0) 316 return error; 317 bsd_to_linux_flock64(&lfl, &bfl); 318 return copyout(&lfl, arg, sizeof lfl); 319 case LINUX_F_SETLK64: 320 case LINUX_F_SETLKW64: 321 cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW); 322 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0) 323 return error; 324 linux_to_bsd_flock64(&bfl, &lfl); 325 sg = stackgap_init(p, 0); 326 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp); 327 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0) 328 return error; 329 SCARG(&fca, fd) = fd; 330 SCARG(&fca, cmd) = cmd; 331 SCARG(&fca, arg) = bfp; 332 return sys_fcntl(p, &fca, retval); 333 default: 334 return linux_sys_fcntl(p, v, retval); 335 } 336 337 return error; 338} 339 340/* 341 * Linux 'readdir' call. This code is mostly taken from the 342 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 343 * an attempt has been made to keep it a little cleaner (failing 344 * miserably, because of the cruft needed if count 1 is passed). 345 * 346 * The d_off field should contain the offset of the next valid entry, 347 * but in Linux it has the offset of the entry itself. We emulate 348 * that bug here. 349 * 350 * Read in BSD-style entries, convert them, and copy them out. 351 * 352 * Note that this doesn't handle union-mounted filesystems. 353 */ 354int 355linux_sys_getdents64(p, v, retval) 356 struct proc *p; 357 void *v; 358 register_t *retval; 359{ 360 struct linux_sys_getdents_args /* { 361 syscallarg(int) fd; 362 syscallarg(struct linux_dirent64 *) dent; 363 syscallarg(unsigned int) count; 364 } */ *uap = v; 365 struct dirent *bdp; 366 struct vnode *vp; 367 caddr_t inp, buf; /* BSD-format */ 368 int len, reclen; /* BSD-format */ 369 caddr_t outp; /* Linux-format */ 370 int resid, linux_reclen = 0; /* Linux-format */ 371 struct file *fp; 372 struct uio auio; 373 struct iovec aiov; 374 struct linux_dirent64 idb; 375 off_t off; /* true file offset */ 376 int buflen, error, eofflag, nbytes, oldcall; 377 struct vattr va; 378 off_t *cookiebuf = NULL, *cookie; 379 int ncookies; 380 381 /* getvnode() will use the descriptor for us */ 382 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 383 return (error); 384 385 if ((fp->f_flag & FREAD) == 0) { 386 error = EBADF; 387 goto out1; 388 } 389 390 vp = (struct vnode *)fp->f_data; 391 if (vp->v_type != VDIR) { 392 error = EINVAL; 393 goto out1; 394 } 395 396 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) 397 goto out1; 398 399 nbytes = SCARG(uap, count); 400 if (nbytes == 1) { /* emulating old, broken behaviour */ 401 nbytes = sizeof (idb); 402 buflen = max(va.va_blocksize, nbytes); 403 oldcall = 1; 404 } else { 405 buflen = min(MAXBSIZE, nbytes); 406 if (buflen < va.va_blocksize) 407 buflen = va.va_blocksize; 408 oldcall = 0; 409 } 410 buf = malloc(buflen, M_TEMP, M_WAITOK); 411 412 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 413 off = fp->f_offset; 414again: 415 aiov.iov_base = buf; 416 aiov.iov_len = buflen; 417 auio.uio_iov = &aiov; 418 auio.uio_iovcnt = 1; 419 auio.uio_rw = UIO_READ; 420 auio.uio_segflg = UIO_SYSSPACE; 421 auio.uio_procp = p; 422 auio.uio_resid = buflen; 423 auio.uio_offset = off; 424 /* 425 * First we read into the malloc'ed buffer, then 426 * we massage it into user space, one record at a time. 427 */ 428 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 429 &ncookies); 430 if (error) 431 goto out; 432 433 inp = buf; 434 outp = (caddr_t)SCARG(uap, dent); 435 resid = nbytes; 436 if ((len = buflen - auio.uio_resid) == 0) 437 goto eof; 438 439 for (cookie = cookiebuf; len > 0; len -= reclen) { 440 bdp = (struct dirent *)inp; 441 reclen = bdp->d_reclen; 442 if (reclen & 3) 443 panic("linux_readdir"); 444 if (bdp->d_fileno == 0) { 445 inp += reclen; /* it is a hole; squish it out */ 446 off = *cookie++; 447 continue; 448 } 449 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); 450 if (reclen > len || resid < linux_reclen) { 451 /* entry too big for buffer, so just stop */ 452 outp++; 453 break; 454 } 455 /* 456 * Massage in place to make a Linux-shaped dirent (otherwise 457 * we have to worry about touching user memory outside of 458 * the copyout() call). 459 */ 460 idb.d_ino = bdp->d_fileno; 461 idb.d_type = bdp->d_type; 462 /* 463 * The old readdir() call misuses the offset and reclen fields. 464 */ 465 if (oldcall) { 466 idb.d_off = linux_reclen; 467 idb.d_reclen = (u_short)bdp->d_namlen; 468 } else { 469 idb.d_off = off; 470 idb.d_reclen = (u_short)linux_reclen; 471 } 472 strcpy(idb.d_name, bdp->d_name); 473 if ((error = copyout((caddr_t)&idb, outp, linux_reclen))) 474 goto out; 475 /* advance past this real entry */ 476 inp += reclen; 477 off = *cookie++; /* each entry points to itself */ 478 /* advance output past Linux-shaped entry */ 479 outp += linux_reclen; 480 resid -= linux_reclen; 481 if (oldcall) 482 break; 483 } 484 485 /* if we squished out the whole block, try again */ 486 if (outp == (caddr_t)SCARG(uap, dent)) 487 goto again; 488 fp->f_offset = off; /* update the vnode offset */ 489 490 if (oldcall) 491 nbytes = resid + linux_reclen; 492 493eof: 494 *retval = nbytes - resid; 495out: 496 VOP_UNLOCK(vp, 0); 497 if (cookiebuf) 498 free(cookiebuf, M_TEMP); 499 free(buf, M_TEMP); 500out1: 501 FILE_UNUSE(fp, p); 502 return error; 503} 504#endif /* __mips__ || __i386__ */ 505