linux_file64.c revision 1.31
1/* $NetBSD: linux_file64.c,v 1.31 2006/02/09 19:18:56 manu 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.31 2006/02/09 19:18:56 manu 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/sa.h> 63#include <sys/syscallargs.h> 64 65#include <compat/linux/common/linux_types.h> 66#include <compat/linux/common/linux_signal.h> 67#include <compat/linux/common/linux_fcntl.h> 68#include <compat/linux/common/linux_util.h> 69#include <compat/linux/common/linux_machdep.h> 70#include <compat/linux/common/linux_dirent.h> 71 72#include <compat/linux/linux_syscallargs.h> 73 74#ifndef alpha 75 76# ifndef COMPAT_LINUX32 77 78static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *)); 79static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int)); 80 81/* 82 * Convert a NetBSD stat structure to a Linux stat structure. 83 * Only the order of the fields and the padding in the structure 84 * is different. linux_fakedev is a machine-dependent function 85 * which optionally converts device driver major/minor numbers 86 * (XXX horrible, but what can you do against code that compares 87 * things against constant major device numbers? sigh) 88 */ 89static void 90bsd_to_linux_stat(bsp, lsp) 91 struct stat *bsp; 92 struct linux_stat64 *lsp; 93{ 94 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0); 95 lsp->lst_ino = bsp->st_ino; 96 lsp->lst_mode = (linux_mode_t)bsp->st_mode; 97 if (bsp->st_nlink >= (1 << 15)) 98 lsp->lst_nlink = (1 << 15) - 1; 99 else 100 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink; 101 lsp->lst_uid = bsp->st_uid; 102 lsp->lst_gid = bsp->st_gid; 103 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1); 104 lsp->lst_size = bsp->st_size; 105 lsp->lst_blksize = bsp->st_blksize; 106 lsp->lst_blocks = bsp->st_blocks; 107 lsp->lst_atime = bsp->st_atime; 108 lsp->lst_mtime = bsp->st_mtime; 109 lsp->lst_ctime = bsp->st_ctime; 110# ifdef LINUX_STAT64_HAS_NSEC 111 lsp->lst_atime_nsec = bsp->st_atimensec; 112 lsp->lst_mtime_nsec = bsp->st_mtimensec; 113 lsp->lst_ctime_nsec = bsp->st_ctimensec; 114# endif 115# if LINUX_STAT64_HAS_BROKEN_ST_INO 116 lsp->__lst_ino = (linux_ino_t) bsp->st_ino; 117# endif 118} 119 120/* 121 * The stat functions below are plain sailing. stat and lstat are handled 122 * by one function to avoid code duplication. 123 */ 124int 125linux_sys_fstat64(l, v, retval) 126 struct lwp *l; 127 void *v; 128 register_t *retval; 129{ 130 struct linux_sys_fstat64_args /* { 131 syscallarg(int) fd; 132 syscallarg(struct linux_stat64 *) sp; 133 } */ *uap = v; 134 struct proc *p = l->l_proc; 135 struct sys___fstat30_args fsa; 136 struct linux_stat64 tmplst; 137 struct stat *st,tmpst; 138 caddr_t sg; 139 int error; 140 141 sg = stackgap_init(p, 0); 142 143 st = stackgap_alloc(p, &sg, sizeof (struct stat)); 144 145 SCARG(&fsa, fd) = SCARG(uap, fd); 146 SCARG(&fsa, sb) = st; 147 148 if ((error = sys___fstat30(l, &fsa, retval))) 149 return error; 150 151 if ((error = copyin(st, &tmpst, sizeof tmpst))) 152 return error; 153 154 bsd_to_linux_stat(&tmpst, &tmplst); 155 156 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst))) 157 return error; 158 159 return 0; 160} 161 162static int 163linux_do_stat64(l, v, retval, dolstat) 164 struct lwp *l; 165 void *v; 166 register_t *retval; 167 int dolstat; 168{ 169 struct proc *p = l->l_proc; 170 struct sys___stat30_args sa; 171 struct linux_stat64 tmplst; 172 struct stat *st, tmpst; 173 caddr_t sg; 174 int error; 175 struct linux_sys_stat64_args *uap = v; 176 177 sg = stackgap_init(p, 0); 178 st = stackgap_alloc(p, &sg, sizeof (struct stat)); 179 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path)); 180 181 SCARG(&sa, ub) = st; 182 SCARG(&sa, path) = SCARG(uap, path); 183 184 if ((error = (dolstat ? sys___lstat30(l, &sa, retval) : 185 sys___stat30(l, &sa, retval)))) 186 return error; 187 188 if ((error = copyin(st, &tmpst, sizeof tmpst))) 189 return error; 190 191 bsd_to_linux_stat(&tmpst, &tmplst); 192 193 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst))) 194 return error; 195 196 return 0; 197} 198 199int 200linux_sys_stat64(l, v, retval) 201 struct lwp *l; 202 void *v; 203 register_t *retval; 204{ 205 struct linux_sys_stat64_args /* { 206 syscallarg(const char *) path; 207 syscallarg(struct linux_stat64 *) sp; 208 } */ *uap = v; 209 210 return linux_do_stat64(l, uap, retval, 0); 211} 212 213int 214linux_sys_lstat64(l, v, retval) 215 struct lwp *l; 216 void *v; 217 register_t *retval; 218{ 219 struct linux_sys_lstat64_args /* { 220 syscallarg(const char *) path; 221 syscallarg(struct linux_stat64 *) sp; 222 } */ *uap = v; 223 224 return linux_do_stat64(l, uap, retval, 1); 225} 226 227int 228linux_sys_truncate64(l, v, retval) 229 struct lwp *l; 230 void *v; 231 register_t *retval; 232{ 233 struct linux_sys_truncate64_args /* { 234 syscallarg(const char *) path; 235 syscallarg(off_t) length; 236 } */ *uap = v; 237 struct sys_truncate_args ta; 238 struct proc *p = l->l_proc; 239 caddr_t sg = stackgap_init(p, 0); 240 241 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path)); 242 243 /* Linux doesn't have the 'pad' pseudo-parameter */ 244 SCARG(&ta, path) = SCARG(uap, path); 245 SCARG(&ta, pad) = 0; 246 SCARG(&ta, length) = SCARG(uap, length); 247 248 return sys_truncate(l, &ta, retval); 249} 250 251int 252linux_sys_ftruncate64(l, v, retval) 253 struct lwp *l; 254 void *v; 255 register_t *retval; 256{ 257 struct linux_sys_ftruncate64_args /* { 258 syscallarg(unsigned int) fd; 259 syscallarg(off_t) length; 260 } */ *uap = v; 261 struct sys_ftruncate_args ta; 262 263 /* Linux doesn't have the 'pad' pseudo-parameter */ 264 SCARG(&ta, fd) = SCARG(uap, fd); 265 SCARG(&ta, pad) = 0; 266 SCARG(&ta, length) = SCARG(uap, length); 267 268 return sys_ftruncate(l, &ta, retval); 269} 270# endif /* !COMPAT_LINUX32 */ 271 272# if !defined(__m68k__) && (!defined(__amd64__) || defined(COMPAT_LINUX32)) 273static void bsd_to_linux_flock64 __P((struct linux_flock64 *, 274 const struct flock *)); 275static void linux_to_bsd_flock64 __P((struct flock *, 276 const struct linux_flock64 *)); 277 278static void 279bsd_to_linux_flock64(lfp, bfp) 280 struct linux_flock64 *lfp; 281 const struct flock *bfp; 282{ 283 284 lfp->l_start = bfp->l_start; 285 lfp->l_len = bfp->l_len; 286 lfp->l_pid = bfp->l_pid; 287 lfp->l_whence = bfp->l_whence; 288 switch (bfp->l_type) { 289 case F_RDLCK: 290 lfp->l_type = LINUX_F_RDLCK; 291 break; 292 case F_UNLCK: 293 lfp->l_type = LINUX_F_UNLCK; 294 break; 295 case F_WRLCK: 296 lfp->l_type = LINUX_F_WRLCK; 297 break; 298 } 299} 300 301static void 302linux_to_bsd_flock64(bfp, lfp) 303 struct flock *bfp; 304 const struct linux_flock64 *lfp; 305{ 306 307 bfp->l_start = lfp->l_start; 308 bfp->l_len = lfp->l_len; 309 bfp->l_pid = lfp->l_pid; 310 bfp->l_whence = lfp->l_whence; 311 switch (lfp->l_type) { 312 case LINUX_F_RDLCK: 313 bfp->l_type = F_RDLCK; 314 break; 315 case LINUX_F_UNLCK: 316 bfp->l_type = F_UNLCK; 317 break; 318 case LINUX_F_WRLCK: 319 bfp->l_type = F_WRLCK; 320 break; 321 } 322} 323 324int 325linux_sys_fcntl64(l, v, retval) 326 struct lwp *l; 327 void *v; 328 register_t *retval; 329{ 330 struct linux_sys_fcntl64_args /* { 331 syscallarg(int) fd; 332 syscallarg(int) cmd; 333 syscallarg(void *) arg; 334 } */ *uap = v; 335 struct proc *p = l->l_proc; 336 struct sys_fcntl_args fca; 337 struct linux_flock64 lfl; 338 struct flock bfl, *bfp; 339 int error; 340 caddr_t sg; 341 void *arg = SCARG(uap, arg); 342 int cmd = SCARG(uap, cmd); 343 int fd = SCARG(uap, fd); 344 345 switch (cmd) { 346 case LINUX_F_GETLK64: 347 sg = stackgap_init(p, 0); 348 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp); 349 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0) 350 return error; 351 linux_to_bsd_flock64(&bfl, &lfl); 352 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0) 353 return error; 354 SCARG(&fca, fd) = fd; 355 SCARG(&fca, cmd) = F_GETLK; 356 SCARG(&fca, arg) = bfp; 357 if ((error = sys_fcntl(l, &fca, retval)) != 0) 358 return error; 359 if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0) 360 return error; 361 bsd_to_linux_flock64(&lfl, &bfl); 362 return copyout(&lfl, arg, sizeof lfl); 363 case LINUX_F_SETLK64: 364 case LINUX_F_SETLKW64: 365 cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW); 366 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0) 367 return error; 368 linux_to_bsd_flock64(&bfl, &lfl); 369 sg = stackgap_init(p, 0); 370 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp); 371 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0) 372 return error; 373 SCARG(&fca, fd) = fd; 374 SCARG(&fca, cmd) = cmd; 375 SCARG(&fca, arg) = bfp; 376 return sys_fcntl(l, &fca, retval); 377 default: 378 return linux_sys_fcntl(l, v, retval); 379 } 380} 381# endif /* !m69k && !amd64 && !COMPAT_LINUX32 */ 382 383#endif /* !alpha */ 384 385/* 386 * Linux 'readdir' call. This code is mostly taken from the 387 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 388 * an attempt has been made to keep it a little cleaner. 389 * 390 * The d_off field contains the offset of the next valid entry, 391 * unless the older Linux getdents(2), which used to have it set 392 * to the offset of the entry itself. This function also doesn't 393 * need to deal with the old count == 1 glibc problem. 394 * 395 * Read in BSD-style entries, convert them, and copy them out. 396 * 397 * Note that this doesn't handle union-mounted filesystems. 398 */ 399#ifndef COMPAT_LINUX32 400int 401linux_sys_getdents64(l, v, retval) 402 struct lwp *l; 403 void *v; 404 register_t *retval; 405{ 406 struct linux_sys_getdents_args /* { 407 syscallarg(int) fd; 408 syscallarg(struct linux_dirent64 *) dent; 409 syscallarg(unsigned int) count; 410 } */ *uap = v; 411 struct proc *p = l->l_proc; 412 struct dirent *bdp; 413 struct vnode *vp; 414 caddr_t inp, tbuf; /* BSD-format */ 415 int len, reclen; /* BSD-format */ 416 caddr_t outp; /* Linux-format */ 417 int resid, linux_reclen = 0; /* Linux-format */ 418 struct file *fp; 419 struct uio auio; 420 struct iovec aiov; 421 struct linux_dirent64 idb; 422 off_t off; /* true file offset */ 423 int buflen, error, eofflag, nbytes; 424 struct vattr va; 425 off_t *cookiebuf = NULL, *cookie; 426 int ncookies; 427 428 /* getvnode() will use the descriptor for us */ 429 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 430 return (error); 431 432 if ((fp->f_flag & FREAD) == 0) { 433 error = EBADF; 434 goto out1; 435 } 436 437 vp = (struct vnode *)fp->f_data; 438 if (vp->v_type != VDIR) { 439 error = EINVAL; 440 goto out1; 441 } 442 443 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, l))) 444 goto out1; 445 446 nbytes = SCARG(uap, count); 447 buflen = min(MAXBSIZE, nbytes); 448 if (buflen < va.va_blocksize) 449 buflen = va.va_blocksize; 450 tbuf = malloc(buflen, M_TEMP, M_WAITOK); 451 452 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 453 off = fp->f_offset; 454again: 455 aiov.iov_base = tbuf; 456 aiov.iov_len = buflen; 457 auio.uio_iov = &aiov; 458 auio.uio_iovcnt = 1; 459 auio.uio_rw = UIO_READ; 460 auio.uio_segflg = UIO_SYSSPACE; 461 auio.uio_lwp = NULL; 462 auio.uio_resid = buflen; 463 auio.uio_offset = off; 464 /* 465 * First we read into the malloc'ed buffer, then 466 * we massage it into user space, one record at a time. 467 */ 468 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 469 &ncookies); 470 if (error) 471 goto out; 472 473 inp = tbuf; 474 outp = (caddr_t)SCARG(uap, dent); 475 resid = nbytes; 476 if ((len = buflen - auio.uio_resid) == 0) 477 goto eof; 478 479 for (cookie = cookiebuf; len > 0; len -= reclen) { 480 bdp = (struct dirent *)inp; 481 reclen = bdp->d_reclen; 482 if (reclen & 3) 483 panic("linux_readdir"); 484 if (bdp->d_fileno == 0) { 485 inp += reclen; /* it is a hole; squish it out */ 486 if (cookie) 487 off = *cookie++; 488 else 489 off += reclen; 490 continue; 491 } 492 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); 493 if (reclen > len || resid < linux_reclen) { 494 /* entry too big for buffer, so just stop */ 495 outp++; 496 break; 497 } 498 if (cookie) 499 off = *cookie++; /* each entry points to next */ 500 else 501 off += reclen; 502 /* 503 * Massage in place to make a Linux-shaped dirent (otherwise 504 * we have to worry about touching user memory outside of 505 * the copyout() call). 506 */ 507 idb.d_ino = bdp->d_fileno; 508 idb.d_type = bdp->d_type; 509 idb.d_off = off; 510 idb.d_reclen = (u_short)linux_reclen; 511 strcpy(idb.d_name, bdp->d_name); 512 if ((error = copyout((caddr_t)&idb, outp, linux_reclen))) 513 goto out; 514 /* advance past this real entry */ 515 inp += reclen; 516 /* advance output past Linux-shaped entry */ 517 outp += linux_reclen; 518 resid -= linux_reclen; 519 } 520 521 /* if we squished out the whole block, try again */ 522 if (outp == (caddr_t)SCARG(uap, dent)) 523 goto again; 524 fp->f_offset = off; /* update the vnode offset */ 525 526eof: 527 *retval = nbytes - resid; 528out: 529 VOP_UNLOCK(vp, 0); 530 if (cookiebuf) 531 free(cookiebuf, M_TEMP); 532 free(tbuf, M_TEMP); 533out1: 534 FILE_UNUSE(fp, l); 535 return error; 536} 537#endif /* !COMPAT_LINUX32 */ 538