1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 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. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 34 #include <sys/param.h> 35 #include <sys/dirent.h> 36 #include <sys/file.h> 37 #include <sys/filedesc.h> 38 #include <sys/proc.h> 39 #include <sys/malloc.h> 40 #include <sys/mount.h> 41 #include <sys/namei.h> 42 #include <sys/stat.h> 43 #include <sys/syscallsubr.h> 44 #include <sys/systm.h> 45 #include <sys/tty.h> 46 #include <sys/vnode.h> 47 #include <sys/conf.h> 48 #include <sys/fcntl.h> 49 50 #ifdef COMPAT_LINUX32 51 #include <machine/../linux32/linux.h> 52 #include <machine/../linux32/linux32_proto.h> 53 #else 54 #include <machine/../linux/linux.h> 55 #include <machine/../linux/linux_proto.h> 56 #endif 57 58 #include <compat/linux/linux_util.h> 59 #include <compat/linux/linux_file.h> 60 61 static void 62 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb) 63 { 64 int major, minor; 65 66 if (vp->v_type == VCHR && vp->v_rdev != NULL && 67 linux_driver_get_major_minor(vp->v_rdev->si_name, 68 &major, &minor) == 0) { 69 sb->st_rdev = (major << 8 | minor); 70 } 71 } 72 73 static int 74 linux_kern_statat(struct thread *td, int flag, int fd, char *path, 75 enum uio_seg pathseg, struct stat *sbp) 76 { 77 78 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, 79 translate_vnhook_major_minor)); 80 } 81 82 static int 83 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg, 84 struct stat *sbp) 85 { 86 87 return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp)); 88 } 89 90 static int 91 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, 92 struct stat *sbp) 93 { 94 95 return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, 96 pathseg, sbp)); 97 } 98 99 /* 100 * XXX: This was removed from newstat_copyout(), and almost identical 101 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced 102 * XXX: with something that does lookup and locking properly. 103 * XXX: When somebody fixes this: please try to avoid duplicating it. 104 */ 105 #if 0 106 static void 107 disk_foo(struct somestat *tbuf) 108 { 109 struct cdevsw *cdevsw; 110 struct cdev *dev; 111 112 /* Lie about disk drives which are character devices 113 * in FreeBSD but block devices under Linux. 114 */ 115 if (S_ISCHR(tbuf.st_mode) && 116 (dev = findcdev(buf->st_rdev)) != NULL) { 117 cdevsw = dev_refthread(dev); 118 if (cdevsw != NULL) { 119 if (cdevsw->d_flags & D_DISK) { 120 tbuf.st_mode &= ~S_IFMT; 121 tbuf.st_mode |= S_IFBLK; 122 123 /* XXX this may not be quite right */ 124 /* Map major number to 0 */ 125 tbuf.st_dev = minor(buf->st_dev) & 0xf; 126 tbuf.st_rdev = buf->st_rdev & 0xff; 127 } 128 dev_relthread(dev); 129 } 130 } 131 132 } 133 #endif 134 135 static void 136 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf) 137 { 138 struct file *fp; 139 struct vnode *vp; 140 int major, minor; 141 142 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) || 143 fget(td, fd, &fp) != 0) 144 return; 145 vp = fp->f_vnode; 146 if (vp != NULL && vp->v_rdev != NULL && 147 linux_driver_get_major_minor(vp->v_rdev->si_name, 148 &major, &minor) == 0) { 149 buf->st_rdev = (major << 8 | minor); 150 } else if (fp->f_type == DTYPE_PTS) { 151 struct tty *tp = fp->f_data; 152 153 /* Convert the numbers for the slave device. */ 154 if (linux_driver_get_major_minor(tp->t_dev->si_name, 155 &major, &minor) == 0) { 156 buf->st_rdev = (major << 8 | minor); 157 } 158 } 159 fdrop(fp, td); 160 } 161 162 static int 163 newstat_copyout(struct stat *buf, void *ubuf) 164 { 165 struct l_newstat tbuf; 166 167 bzero(&tbuf, sizeof(tbuf)); 168 tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8); 169 tbuf.st_ino = buf->st_ino; 170 tbuf.st_mode = buf->st_mode; 171 tbuf.st_nlink = buf->st_nlink; 172 tbuf.st_uid = buf->st_uid; 173 tbuf.st_gid = buf->st_gid; 174 tbuf.st_rdev = buf->st_rdev; 175 tbuf.st_size = buf->st_size; 176 tbuf.st_atim.tv_sec = buf->st_atim.tv_sec; 177 tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec; 178 tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec; 179 tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec; 180 tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec; 181 tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec; 182 tbuf.st_blksize = buf->st_blksize; 183 tbuf.st_blocks = buf->st_blocks; 184 185 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 186 } 187 188 int 189 linux_newstat(struct thread *td, struct linux_newstat_args *args) 190 { 191 struct stat buf; 192 char *path; 193 int error; 194 195 LCONVPATHEXIST(td, args->path, &path); 196 197 #ifdef DEBUG 198 if (ldebug(newstat)) 199 printf(ARGS(newstat, "%s, *"), path); 200 #endif 201 202 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf); 203 LFREEPATH(path); 204 if (error) 205 return (error); 206 return (newstat_copyout(&buf, args->buf)); 207 } 208 209 int 210 linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 211 { 212 struct stat sb; 213 char *path; 214 int error; 215 216 LCONVPATHEXIST(td, args->path, &path); 217 218 #ifdef DEBUG 219 if (ldebug(newlstat)) 220 printf(ARGS(newlstat, "%s, *"), path); 221 #endif 222 223 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb); 224 LFREEPATH(path); 225 if (error) 226 return (error); 227 return (newstat_copyout(&sb, args->buf)); 228 } 229 230 int 231 linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 232 { 233 struct stat buf; 234 int error; 235 236 #ifdef DEBUG 237 if (ldebug(newfstat)) 238 printf(ARGS(newfstat, "%d, *"), args->fd); 239 #endif 240 241 error = kern_fstat(td, args->fd, &buf); 242 translate_fd_major_minor(td, args->fd, &buf); 243 if (!error) 244 error = newstat_copyout(&buf, args->buf); 245 246 return (error); 247 } 248 249 static int 250 stat_copyout(struct stat *buf, void *ubuf) 251 { 252 struct l_stat lbuf; 253 254 bzero(&lbuf, sizeof(lbuf)); 255 lbuf.st_dev = buf->st_dev; 256 lbuf.st_ino = buf->st_ino; 257 lbuf.st_mode = buf->st_mode; 258 lbuf.st_nlink = buf->st_nlink; 259 lbuf.st_uid = buf->st_uid; 260 lbuf.st_gid = buf->st_gid; 261 lbuf.st_rdev = buf->st_rdev; 262 if (buf->st_size < (quad_t)1 << 32) 263 lbuf.st_size = buf->st_size; 264 else 265 lbuf.st_size = -2; 266 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec; 267 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec; 268 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec; 269 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec; 270 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec; 271 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec; 272 lbuf.st_blksize = buf->st_blksize; 273 lbuf.st_blocks = buf->st_blocks; 274 lbuf.st_flags = buf->st_flags; 275 lbuf.st_gen = buf->st_gen; 276 277 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 278 } 279 280 int 281 linux_stat(struct thread *td, struct linux_stat_args *args) 282 { 283 struct stat buf; 284 char *path; 285 int error; 286 287 LCONVPATHEXIST(td, args->path, &path); 288 289 #ifdef DEBUG 290 if (ldebug(stat)) 291 printf(ARGS(stat, "%s, *"), path); 292 #endif 293 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf); 294 if (error) { 295 LFREEPATH(path); 296 return (error); 297 } 298 LFREEPATH(path); 299 return(stat_copyout(&buf, args->up)); 300 } 301 302 int 303 linux_lstat(struct thread *td, struct linux_lstat_args *args) 304 { 305 struct stat buf; 306 char *path; 307 int error; 308 309 LCONVPATHEXIST(td, args->path, &path); 310 311 #ifdef DEBUG 312 if (ldebug(lstat)) 313 printf(ARGS(lstat, "%s, *"), path); 314 #endif 315 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf); 316 if (error) { 317 LFREEPATH(path); 318 return (error); 319 } 320 LFREEPATH(path); 321 return(stat_copyout(&buf, args->up)); 322 } 323 324 /* XXX - All fields of type l_int are defined as l_long on i386 */ 325 struct l_statfs { 326 l_int f_type; 327 l_int f_bsize; 328 l_int f_blocks; 329 l_int f_bfree; 330 l_int f_bavail; 331 l_int f_files; 332 l_int f_ffree; 333 l_fsid_t f_fsid; 334 l_int f_namelen; 335 l_int f_spare[6]; 336 }; 337 338 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 339 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 340 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 341 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 342 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 343 #define LINUX_NCP_SUPER_MAGIC 0x564cL 344 #define LINUX_NFS_SUPER_MAGIC 0x6969L 345 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 346 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 347 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 348 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 349 350 static long 351 bsd_to_linux_ftype(const char *fstypename) 352 { 353 int i; 354 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 355 {"ufs", LINUX_UFS_SUPER_MAGIC}, 356 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 357 {"nfs", LINUX_NFS_SUPER_MAGIC}, 358 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 359 {"procfs", LINUX_PROC_SUPER_MAGIC}, 360 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 361 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 362 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 363 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 364 {"coda", LINUX_CODA_SUPER_MAGIC}, 365 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 366 {NULL, 0L}}; 367 368 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 369 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 370 return (b2l_tbl[i].linux_type); 371 372 return (0L); 373 } 374 375 static void 376 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 377 { 378 379 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 380 linux_statfs->f_bsize = bsd_statfs->f_bsize; 381 linux_statfs->f_blocks = bsd_statfs->f_blocks; 382 linux_statfs->f_bfree = bsd_statfs->f_bfree; 383 linux_statfs->f_bavail = bsd_statfs->f_bavail; 384 linux_statfs->f_ffree = bsd_statfs->f_ffree; 385 linux_statfs->f_files = bsd_statfs->f_files; 386 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 387 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 388 linux_statfs->f_namelen = MAXNAMLEN; 389 } 390 391 int 392 linux_statfs(struct thread *td, struct linux_statfs_args *args) 393 { 394 struct l_statfs linux_statfs; 395 struct statfs bsd_statfs; 396 char *path; 397 int error; 398 399 LCONVPATHEXIST(td, args->path, &path); 400 401 #ifdef DEBUG 402 if (ldebug(statfs)) 403 printf(ARGS(statfs, "%s, *"), path); 404 #endif 405 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 406 LFREEPATH(path); 407 if (error) 408 return (error); 409 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 410 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 411 } 412 413 static void 414 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs) 415 { 416 417 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 418 linux_statfs->f_bsize = bsd_statfs->f_bsize; 419 linux_statfs->f_blocks = bsd_statfs->f_blocks; 420 linux_statfs->f_bfree = bsd_statfs->f_bfree; 421 linux_statfs->f_bavail = bsd_statfs->f_bavail; 422 linux_statfs->f_ffree = bsd_statfs->f_ffree; 423 linux_statfs->f_files = bsd_statfs->f_files; 424 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 425 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 426 linux_statfs->f_namelen = MAXNAMLEN; 427 } 428 429 int 430 linux_statfs64(struct thread *td, struct linux_statfs64_args *args) 431 { 432 struct l_statfs64 linux_statfs; 433 struct statfs bsd_statfs; 434 char *path; 435 int error; 436 437 if (args->bufsize != sizeof(struct l_statfs64)) 438 return EINVAL; 439 440 LCONVPATHEXIST(td, args->path, &path); 441 442 #ifdef DEBUG 443 if (ldebug(statfs64)) 444 printf(ARGS(statfs64, "%s, *"), path); 445 #endif 446 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 447 LFREEPATH(path); 448 if (error) 449 return (error); 450 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs); 451 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 452 } 453 454 int 455 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 456 { 457 struct l_statfs linux_statfs; 458 struct statfs bsd_statfs; 459 int error; 460 461 #ifdef DEBUG 462 if (ldebug(fstatfs)) 463 printf(ARGS(fstatfs, "%d, *"), args->fd); 464 #endif 465 error = kern_fstatfs(td, args->fd, &bsd_statfs); 466 if (error) 467 return error; 468 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 469 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 470 } 471 472 struct l_ustat 473 { 474 l_daddr_t f_tfree; 475 l_ino_t f_tinode; 476 char f_fname[6]; 477 char f_fpack[6]; 478 }; 479 480 int 481 linux_ustat(struct thread *td, struct linux_ustat_args *args) 482 { 483 #ifdef DEBUG 484 if (ldebug(ustat)) 485 printf(ARGS(ustat, "%d, *"), args->dev); 486 #endif 487 488 return (EOPNOTSUPP); 489 } 490 491 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 492 493 static int 494 stat64_copyout(struct stat *buf, void *ubuf) 495 { 496 struct l_stat64 lbuf; 497 498 bzero(&lbuf, sizeof(lbuf)); 499 lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8); 500 lbuf.st_ino = buf->st_ino; 501 lbuf.st_mode = buf->st_mode; 502 lbuf.st_nlink = buf->st_nlink; 503 lbuf.st_uid = buf->st_uid; 504 lbuf.st_gid = buf->st_gid; 505 lbuf.st_rdev = buf->st_rdev; 506 lbuf.st_size = buf->st_size; 507 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec; 508 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec; 509 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec; 510 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec; 511 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec; 512 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec; 513 lbuf.st_blksize = buf->st_blksize; 514 lbuf.st_blocks = buf->st_blocks; 515 516 /* 517 * The __st_ino field makes all the difference. In the Linux kernel 518 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 519 * but without the assignment to __st_ino the runtime linker refuses 520 * to mmap(2) any shared libraries. I guess it's broken alright :-) 521 */ 522 lbuf.__st_ino = buf->st_ino; 523 524 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 525 } 526 527 int 528 linux_stat64(struct thread *td, struct linux_stat64_args *args) 529 { 530 struct stat buf; 531 char *filename; 532 int error; 533 534 LCONVPATHEXIST(td, args->filename, &filename); 535 536 #ifdef DEBUG 537 if (ldebug(stat64)) 538 printf(ARGS(stat64, "%s, *"), filename); 539 #endif 540 541 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf); 542 LFREEPATH(filename); 543 if (error) 544 return (error); 545 return (stat64_copyout(&buf, args->statbuf)); 546 } 547 548 int 549 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 550 { 551 struct stat sb; 552 char *filename; 553 int error; 554 555 LCONVPATHEXIST(td, args->filename, &filename); 556 557 #ifdef DEBUG 558 if (ldebug(lstat64)) 559 printf(ARGS(lstat64, "%s, *"), args->filename); 560 #endif 561 562 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb); 563 LFREEPATH(filename); 564 if (error) 565 return (error); 566 return (stat64_copyout(&sb, args->statbuf)); 567 } 568 569 int 570 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 571 { 572 struct stat buf; 573 int error; 574 575 #ifdef DEBUG 576 if (ldebug(fstat64)) 577 printf(ARGS(fstat64, "%d, *"), args->fd); 578 #endif 579 580 error = kern_fstat(td, args->fd, &buf); 581 translate_fd_major_minor(td, args->fd, &buf); 582 if (!error) 583 error = stat64_copyout(&buf, args->statbuf); 584 585 return (error); 586 } 587 588 int 589 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args) 590 { 591 char *path; 592 int error, dfd, flag; 593 struct stat buf; 594 595 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 596 return (EINVAL); 597 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 598 AT_SYMLINK_NOFOLLOW : 0; 599 600 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 601 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 602 603 #ifdef DEBUG 604 if (ldebug(fstatat64)) 605 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag); 606 #endif 607 608 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf); 609 if (!error) 610 error = stat64_copyout(&buf, args->statbuf); 611 LFREEPATH(path); 612 613 return (error); 614 } 615 616 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 617