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 #include "opt_mac.h" 34 35 #include <sys/param.h> 36 #include <sys/dirent.h> 37 #include <sys/file.h> 38 #include <sys/filedesc.h> 39 #include <sys/proc.h> 40 #include <sys/malloc.h> 41 #include <sys/mount.h> 42 #include <sys/namei.h> 43 #include <sys/stat.h> 44 #include <sys/syscallsubr.h> 45 #include <sys/systm.h> 46 #include <sys/tty.h> 47 #include <sys/vnode.h> 48 #include <sys/conf.h> 49 #include <sys/fcntl.h> 50 51 #ifdef COMPAT_LINUX32 52 #include <machine/../linux32/linux.h> 53 #include <machine/../linux32/linux32_proto.h> 54 #else 55 #include <machine/../linux/linux.h> 56 #include <machine/../linux/linux_proto.h> 57 #endif 58 59 #include <compat/linux/linux_util.h> 60 #include <compat/linux/linux_file.h> 61 62 #include <security/mac/mac_framework.h> 63 64 static void 65 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb) 66 { 67 int major, minor; 68 69 if (vp->v_type == VCHR && vp->v_rdev != NULL && 70 linux_driver_get_major_minor(vp->v_rdev->si_name, 71 &major, &minor) == 0) { 72 sb->st_rdev = (major << 8 | minor); 73 } 74 } 75 76 static int 77 linux_kern_statat(struct thread *td, int flag, int fd, char *path, 78 enum uio_seg pathseg, struct stat *sbp) 79 { 80 81 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, 82 translate_vnhook_major_minor)); 83 } 84 85 static int 86 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg, 87 struct stat *sbp) 88 { 89 90 return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp)); 91 } 92 93 static int 94 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, 95 struct stat *sbp) 96 { 97 98 return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, 99 pathseg, sbp)); 100 } 101 102 /* 103 * XXX: This was removed from newstat_copyout(), and almost identical 104 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced 105 * XXX: with something that does lookup and locking properly. 106 * XXX: When somebody fixes this: please try to avoid duplicating it. 107 */ 108 #if 0 109 static void 110 disk_foo(struct somestat *tbuf) 111 { 112 struct cdevsw *cdevsw; 113 struct cdev *dev; 114 115 /* Lie about disk drives which are character devices 116 * in FreeBSD but block devices under Linux. 117 */ 118 if (S_ISCHR(tbuf.st_mode) && 119 (dev = findcdev(buf->st_rdev)) != NULL) { 120 cdevsw = dev_refthread(dev); 121 if (cdevsw != NULL) { 122 if (cdevsw->d_flags & D_DISK) { 123 tbuf.st_mode &= ~S_IFMT; 124 tbuf.st_mode |= S_IFBLK; 125 126 /* XXX this may not be quite right */ 127 /* Map major number to 0 */ 128 tbuf.st_dev = minor(buf->st_dev) & 0xf; 129 tbuf.st_rdev = buf->st_rdev & 0xff; 130 } 131 dev_relthread(dev); 132 } 133 } 134 135 } 136 #endif 137 138 static void 139 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf) 140 { 141 struct file *fp; 142 struct vnode *vp; 143 int major, minor; 144 145 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) || 146 fget(td, fd, &fp) != 0) 147 return; 148 vp = fp->f_vnode; 149 if (vp != NULL && vp->v_rdev != NULL && 150 linux_driver_get_major_minor(vp->v_rdev->si_name, 151 &major, &minor) == 0) { 152 buf->st_rdev = (major << 8 | minor); 153 } else if (fp->f_type == DTYPE_PTS) { 154 struct tty *tp = fp->f_data; 155 156 /* Convert the numbers for the slave device. */ 157 if (linux_driver_get_major_minor(tp->t_dev->si_name, 158 &major, &minor) == 0) { 159 buf->st_rdev = (major << 8 | minor); 160 } 161 } 162 fdrop(fp, td); 163 } 164 165 static int 166 newstat_copyout(struct stat *buf, void *ubuf) 167 { 168 struct l_newstat tbuf; 169 170 bzero(&tbuf, sizeof(tbuf)); 171 tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8); 172 tbuf.st_ino = buf->st_ino; 173 tbuf.st_mode = buf->st_mode; 174 tbuf.st_nlink = buf->st_nlink; 175 tbuf.st_uid = buf->st_uid; 176 tbuf.st_gid = buf->st_gid; 177 tbuf.st_rdev = buf->st_rdev; 178 tbuf.st_size = buf->st_size; 179 tbuf.st_atime = buf->st_atime; 180 tbuf.st_mtime = buf->st_mtime; 181 tbuf.st_ctime = buf->st_ctime; 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_atime = buf->st_atime; 267 lbuf.st_mtime = buf->st_mtime; 268 lbuf.st_ctime = buf->st_ctime; 269 lbuf.st_blksize = buf->st_blksize; 270 lbuf.st_blocks = buf->st_blocks; 271 lbuf.st_flags = buf->st_flags; 272 lbuf.st_gen = buf->st_gen; 273 274 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 275 } 276 277 int 278 linux_stat(struct thread *td, struct linux_stat_args *args) 279 { 280 struct stat buf; 281 char *path; 282 int error; 283 284 LCONVPATHEXIST(td, args->path, &path); 285 286 #ifdef DEBUG 287 if (ldebug(stat)) 288 printf(ARGS(stat, "%s, *"), path); 289 #endif 290 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf); 291 if (error) { 292 LFREEPATH(path); 293 return (error); 294 } 295 LFREEPATH(path); 296 return(stat_copyout(&buf, args->up)); 297 } 298 299 int 300 linux_lstat(struct thread *td, struct linux_lstat_args *args) 301 { 302 struct stat buf; 303 char *path; 304 int error; 305 306 LCONVPATHEXIST(td, args->path, &path); 307 308 #ifdef DEBUG 309 if (ldebug(lstat)) 310 printf(ARGS(lstat, "%s, *"), path); 311 #endif 312 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf); 313 if (error) { 314 LFREEPATH(path); 315 return (error); 316 } 317 LFREEPATH(path); 318 return(stat_copyout(&buf, args->up)); 319 } 320 321 /* XXX - All fields of type l_int are defined as l_long on i386 */ 322 struct l_statfs { 323 l_int f_type; 324 l_int f_bsize; 325 l_int f_blocks; 326 l_int f_bfree; 327 l_int f_bavail; 328 l_int f_files; 329 l_int f_ffree; 330 l_fsid_t f_fsid; 331 l_int f_namelen; 332 l_int f_spare[6]; 333 }; 334 335 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 336 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 337 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 338 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 339 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 340 #define LINUX_NCP_SUPER_MAGIC 0x564cL 341 #define LINUX_NFS_SUPER_MAGIC 0x6969L 342 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 343 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 344 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 345 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 346 347 static long 348 bsd_to_linux_ftype(const char *fstypename) 349 { 350 int i; 351 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 352 {"ufs", LINUX_UFS_SUPER_MAGIC}, 353 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 354 {"nfs", LINUX_NFS_SUPER_MAGIC}, 355 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 356 {"procfs", LINUX_PROC_SUPER_MAGIC}, 357 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 358 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 359 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 360 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 361 {"coda", LINUX_CODA_SUPER_MAGIC}, 362 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 363 {NULL, 0L}}; 364 365 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 366 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 367 return (b2l_tbl[i].linux_type); 368 369 return (0L); 370 } 371 372 static void 373 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 374 { 375 376 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 377 linux_statfs->f_bsize = bsd_statfs->f_bsize; 378 linux_statfs->f_blocks = bsd_statfs->f_blocks; 379 linux_statfs->f_bfree = bsd_statfs->f_bfree; 380 linux_statfs->f_bavail = bsd_statfs->f_bavail; 381 linux_statfs->f_ffree = bsd_statfs->f_ffree; 382 linux_statfs->f_files = bsd_statfs->f_files; 383 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 384 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 385 linux_statfs->f_namelen = MAXNAMLEN; 386 } 387 388 int 389 linux_statfs(struct thread *td, struct linux_statfs_args *args) 390 { 391 struct l_statfs linux_statfs; 392 struct statfs bsd_statfs; 393 char *path; 394 int error; 395 396 LCONVPATHEXIST(td, args->path, &path); 397 398 #ifdef DEBUG 399 if (ldebug(statfs)) 400 printf(ARGS(statfs, "%s, *"), path); 401 #endif 402 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 403 LFREEPATH(path); 404 if (error) 405 return (error); 406 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 407 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 408 } 409 410 static void 411 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs) 412 { 413 414 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 415 linux_statfs->f_bsize = bsd_statfs->f_bsize; 416 linux_statfs->f_blocks = bsd_statfs->f_blocks; 417 linux_statfs->f_bfree = bsd_statfs->f_bfree; 418 linux_statfs->f_bavail = bsd_statfs->f_bavail; 419 linux_statfs->f_ffree = bsd_statfs->f_ffree; 420 linux_statfs->f_files = bsd_statfs->f_files; 421 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 422 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 423 linux_statfs->f_namelen = MAXNAMLEN; 424 } 425 426 int 427 linux_statfs64(struct thread *td, struct linux_statfs64_args *args) 428 { 429 struct l_statfs64 linux_statfs; 430 struct statfs bsd_statfs; 431 char *path; 432 int error; 433 434 if (args->bufsize != sizeof(struct l_statfs64)) 435 return EINVAL; 436 437 LCONVPATHEXIST(td, args->path, &path); 438 439 #ifdef DEBUG 440 if (ldebug(statfs64)) 441 printf(ARGS(statfs64, "%s, *"), path); 442 #endif 443 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 444 LFREEPATH(path); 445 if (error) 446 return (error); 447 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs); 448 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 449 } 450 451 int 452 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 453 { 454 struct l_statfs linux_statfs; 455 struct statfs bsd_statfs; 456 int error; 457 458 #ifdef DEBUG 459 if (ldebug(fstatfs)) 460 printf(ARGS(fstatfs, "%d, *"), args->fd); 461 #endif 462 error = kern_fstatfs(td, args->fd, &bsd_statfs); 463 if (error) 464 return error; 465 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 466 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 467 } 468 469 struct l_ustat 470 { 471 l_daddr_t f_tfree; 472 l_ino_t f_tinode; 473 char f_fname[6]; 474 char f_fpack[6]; 475 }; 476 477 int 478 linux_ustat(struct thread *td, struct linux_ustat_args *args) 479 { 480 #ifdef DEBUG 481 if (ldebug(ustat)) 482 printf(ARGS(ustat, "%d, *"), args->dev); 483 #endif 484 485 return (EOPNOTSUPP); 486 } 487 488 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 489 490 static int 491 stat64_copyout(struct stat *buf, void *ubuf) 492 { 493 struct l_stat64 lbuf; 494 495 bzero(&lbuf, sizeof(lbuf)); 496 lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8); 497 lbuf.st_ino = buf->st_ino; 498 lbuf.st_mode = buf->st_mode; 499 lbuf.st_nlink = buf->st_nlink; 500 lbuf.st_uid = buf->st_uid; 501 lbuf.st_gid = buf->st_gid; 502 lbuf.st_rdev = buf->st_rdev; 503 lbuf.st_size = buf->st_size; 504 lbuf.st_atime = buf->st_atime; 505 lbuf.st_mtime = buf->st_mtime; 506 lbuf.st_ctime = buf->st_ctime; 507 lbuf.st_blksize = buf->st_blksize; 508 lbuf.st_blocks = buf->st_blocks; 509 510 /* 511 * The __st_ino field makes all the difference. In the Linux kernel 512 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 513 * but without the assignment to __st_ino the runtime linker refuses 514 * to mmap(2) any shared libraries. I guess it's broken alright :-) 515 */ 516 lbuf.__st_ino = buf->st_ino; 517 518 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 519 } 520 521 int 522 linux_stat64(struct thread *td, struct linux_stat64_args *args) 523 { 524 struct stat buf; 525 char *filename; 526 int error; 527 528 LCONVPATHEXIST(td, args->filename, &filename); 529 530 #ifdef DEBUG 531 if (ldebug(stat64)) 532 printf(ARGS(stat64, "%s, *"), filename); 533 #endif 534 535 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf); 536 LFREEPATH(filename); 537 if (error) 538 return (error); 539 return (stat64_copyout(&buf, args->statbuf)); 540 } 541 542 int 543 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 544 { 545 struct stat sb; 546 char *filename; 547 int error; 548 549 LCONVPATHEXIST(td, args->filename, &filename); 550 551 #ifdef DEBUG 552 if (ldebug(lstat64)) 553 printf(ARGS(lstat64, "%s, *"), args->filename); 554 #endif 555 556 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb); 557 LFREEPATH(filename); 558 if (error) 559 return (error); 560 return (stat64_copyout(&sb, args->statbuf)); 561 } 562 563 int 564 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 565 { 566 struct stat buf; 567 int error; 568 569 #ifdef DEBUG 570 if (ldebug(fstat64)) 571 printf(ARGS(fstat64, "%d, *"), args->fd); 572 #endif 573 574 error = kern_fstat(td, args->fd, &buf); 575 translate_fd_major_minor(td, args->fd, &buf); 576 if (!error) 577 error = stat64_copyout(&buf, args->statbuf); 578 579 return (error); 580 } 581 582 int 583 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args) 584 { 585 char *path; 586 int error, dfd, flag; 587 struct stat buf; 588 589 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 590 return (EINVAL); 591 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 592 AT_SYMLINK_NOFOLLOW : 0; 593 594 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 595 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 596 597 #ifdef DEBUG 598 if (ldebug(fstatat64)) 599 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag); 600 #endif 601 602 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf); 603 if (!error) 604 error = stat64_copyout(&buf, args->statbuf); 605 LFREEPATH(path); 606 607 return (error); 608 } 609 610 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 611