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/jail.h> 41 #include <sys/mac.h> 42 #include <sys/malloc.h> 43 #include <sys/mount.h> 44 #include <sys/namei.h> 45 #include <sys/stat.h> 46 #include <sys/syscallsubr.h> 47 #include <sys/systm.h> 48 #include <sys/vnode.h> 49 #include <sys/conf.h> 50 #include <sys/fcntl.h> 51 52 #ifdef COMPAT_LINUX32 53 #include <machine/../linux32/linux.h> 54 #include <machine/../linux32/linux32_proto.h> 55 #else 56 #include <machine/../linux/linux.h> 57 #include <machine/../linux/linux_proto.h> 58 #endif 59 60 #include <compat/linux/linux_util.h> 61 62 /* 63 * XXX: This was removed from newstat_copyout(), and almost identical 64 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced 65 * XXX: with something that does lookup and locking properly. 66 * XXX: When somebody fixes this: please try to avoid duplicating it. 67 */ 68 #if 0 69 static void 70 disk_foo(struct somestat *tbuf) 71 { 72 struct cdevsw *cdevsw; 73 struct cdev *dev; 74 75 /* Lie about disk drives which are character devices 76 * in FreeBSD but block devices under Linux. 77 */ 78 if (S_ISCHR(tbuf.st_mode) && 79 (dev = findcdev(buf->st_rdev)) != NULL) { 80 cdevsw = dev_refthread(dev); 81 if (cdevsw != NULL) { 82 if (cdevsw->d_flags & D_DISK) { 83 tbuf.st_mode &= ~S_IFMT; 84 tbuf.st_mode |= S_IFBLK; 85 86 /* XXX this may not be quite right */ 87 /* Map major number to 0 */ 88 tbuf.st_dev = uminor(buf->st_dev) & 0xf; 89 tbuf.st_rdev = buf->st_rdev & 0xff; 90 } 91 dev_relthread(dev); 92 } 93 } 94 95 } 96 #endif 97 98 static void 99 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf) 100 { 101 struct file *fp; 102 int error; 103 int major, minor; 104 105 if ((error = fget(td, fd, &fp)) != 0) 106 return; 107 if (fp->f_vnode) { 108 if (fp->f_vnode->v_type == VCHR 109 || fp->f_vnode->v_type == VBLK) { 110 if (fp->f_vnode->v_un.vu_cdev) { 111 if (linux_driver_get_major_minor( 112 fp->f_vnode->v_un.vu_cdev->si_name, 113 &major, &minor) == 0) { 114 buf->st_rdev = (major << 8 | minor); 115 } 116 } 117 } 118 } 119 fdrop(fp, td); 120 } 121 122 static void 123 translate_path_major_minor(struct thread *td, char *path, struct stat *buf) 124 { 125 struct proc *p = td->td_proc; 126 struct filedesc *fdp = p->p_fd; 127 struct file *fp; 128 int fd; 129 int temp; 130 131 temp = td->td_retval[0]; 132 if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0) 133 return; 134 fd = td->td_retval[0]; 135 td->td_retval[0] = temp; 136 translate_fd_major_minor(td, fd, buf); 137 FILEDESC_LOCK(fdp); 138 fp = fdp->fd_ofiles[fd]; 139 FILEDESC_UNLOCK(fdp); 140 fdclose(fdp, fdp->fd_ofiles[fd], fd, td); 141 } 142 143 static int 144 newstat_copyout(struct stat *buf, void *ubuf) 145 { 146 struct l_newstat tbuf; 147 148 bzero(&tbuf, sizeof(tbuf)); 149 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 150 tbuf.st_ino = buf->st_ino; 151 tbuf.st_mode = buf->st_mode; 152 tbuf.st_nlink = buf->st_nlink; 153 tbuf.st_uid = buf->st_uid; 154 tbuf.st_gid = buf->st_gid; 155 tbuf.st_rdev = buf->st_rdev; 156 tbuf.st_size = buf->st_size; 157 tbuf.st_atime = buf->st_atime; 158 tbuf.st_mtime = buf->st_mtime; 159 tbuf.st_ctime = buf->st_ctime; 160 tbuf.st_blksize = buf->st_blksize; 161 tbuf.st_blocks = buf->st_blocks; 162 163 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 164 } 165 166 int 167 linux_newstat(struct thread *td, struct linux_newstat_args *args) 168 { 169 struct stat buf; 170 char *path; 171 int error; 172 173 LCONVPATHEXIST(td, args->path, &path); 174 175 #ifdef DEBUG 176 if (ldebug(newstat)) 177 printf(ARGS(newstat, "%s, *"), path); 178 #endif 179 180 error = kern_stat(td, path, UIO_SYSSPACE, &buf); 181 if (!error && strlen(path) > strlen("/dev/pts/") && 182 !strncmp(path, "/dev/pts/", strlen("/dev/pts/")) 183 && path[9] >= '0' && path[9] <= '9') { 184 /* 185 * Linux checks major and minors of the slave device to make 186 * sure it's a pty device, so let's make him believe it is. 187 */ 188 buf.st_rdev = (136 << 8); 189 } 190 191 translate_path_major_minor(td, path, &buf); 192 193 LFREEPATH(path); 194 if (error) 195 return (error); 196 return (newstat_copyout(&buf, args->buf)); 197 } 198 199 int 200 linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 201 { 202 struct stat sb; 203 char *path; 204 int error; 205 206 LCONVPATHEXIST(td, args->path, &path); 207 208 #ifdef DEBUG 209 if (ldebug(newlstat)) 210 printf(ARGS(newlstat, "%s, *"), path); 211 #endif 212 213 error = kern_lstat(td, path, UIO_SYSSPACE, &sb); 214 translate_path_major_minor(td, path, &sb); 215 LFREEPATH(path); 216 if (error) 217 return (error); 218 return (newstat_copyout(&sb, args->buf)); 219 } 220 221 int 222 linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 223 { 224 struct stat buf; 225 int error; 226 227 #ifdef DEBUG 228 if (ldebug(newfstat)) 229 printf(ARGS(newfstat, "%d, *"), args->fd); 230 #endif 231 232 error = kern_fstat(td, args->fd, &buf); 233 translate_fd_major_minor(td, args->fd, &buf); 234 if (!error) 235 error = newstat_copyout(&buf, args->buf); 236 237 return (error); 238 } 239 240 static int 241 stat_copyout(struct stat *buf, void *ubuf) 242 { 243 struct l_stat lbuf; 244 245 bzero(&lbuf, sizeof(lbuf)); 246 lbuf.st_dev = buf->st_dev; 247 lbuf.st_ino = buf->st_ino; 248 lbuf.st_mode = buf->st_mode; 249 lbuf.st_nlink = buf->st_nlink; 250 lbuf.st_uid = buf->st_uid; 251 lbuf.st_gid = buf->st_gid; 252 lbuf.st_rdev = buf->st_rdev; 253 if (buf->st_size < (quad_t)1 << 32) 254 lbuf.st_size = buf->st_size; 255 else 256 lbuf.st_size = -2; 257 lbuf.st_atime = buf->st_atime; 258 lbuf.st_mtime = buf->st_mtime; 259 lbuf.st_ctime = buf->st_ctime; 260 lbuf.st_blksize = buf->st_blksize; 261 lbuf.st_blocks = buf->st_blocks; 262 lbuf.st_flags = buf->st_flags; 263 lbuf.st_gen = buf->st_gen; 264 265 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 266 } 267 268 int 269 linux_stat(struct thread *td, struct linux_stat_args *args) 270 { 271 struct stat buf; 272 int error; 273 #ifdef DEBUG 274 if (ldebug(stat)) 275 printf(ARGS(stat, "%s, *"), args->path); 276 #endif 277 error = kern_stat(td, args->path, UIO_SYSSPACE, &buf); 278 if (error) 279 return (error); 280 translate_path_major_minor(td, args->path, &buf); 281 return(stat_copyout(&buf, args->up)); 282 } 283 284 int 285 linux_lstat(struct thread *td, struct linux_lstat_args *args) 286 { 287 struct stat buf; 288 int error; 289 290 #ifdef DEBUG 291 if (ldebug(lstat)) 292 printf(ARGS(lstat, "%s, *"), args->path); 293 #endif 294 error = kern_lstat(td, args->path, UIO_SYSSPACE, &buf); 295 if (error) 296 return (error); 297 translate_path_major_minor(td, args->path, &buf); 298 return(stat_copyout(&buf, args->up)); 299 } 300 301 /* XXX - All fields of type l_int are defined as l_long on i386 */ 302 struct l_statfs { 303 l_int f_type; 304 l_int f_bsize; 305 l_int f_blocks; 306 l_int f_bfree; 307 l_int f_bavail; 308 l_int f_files; 309 l_int f_ffree; 310 l_fsid_t f_fsid; 311 l_int f_namelen; 312 l_int f_spare[6]; 313 }; 314 315 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 316 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 317 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 318 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 319 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 320 #define LINUX_NCP_SUPER_MAGIC 0x564cL 321 #define LINUX_NFS_SUPER_MAGIC 0x6969L 322 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 323 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 324 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 325 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 326 327 static long 328 bsd_to_linux_ftype(const char *fstypename) 329 { 330 int i; 331 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 332 {"ufs", LINUX_UFS_SUPER_MAGIC}, 333 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 334 {"nfs", LINUX_NFS_SUPER_MAGIC}, 335 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 336 {"procfs", LINUX_PROC_SUPER_MAGIC}, 337 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 338 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 339 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 340 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 341 {"coda", LINUX_CODA_SUPER_MAGIC}, 342 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 343 {NULL, 0L}}; 344 345 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 346 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 347 return (b2l_tbl[i].linux_type); 348 349 return (0L); 350 } 351 352 static void 353 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 354 { 355 356 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 357 linux_statfs->f_bsize = bsd_statfs->f_bsize; 358 linux_statfs->f_blocks = bsd_statfs->f_blocks; 359 linux_statfs->f_bfree = bsd_statfs->f_bfree; 360 linux_statfs->f_bavail = bsd_statfs->f_bavail; 361 linux_statfs->f_ffree = bsd_statfs->f_ffree; 362 linux_statfs->f_files = bsd_statfs->f_files; 363 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 364 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 365 linux_statfs->f_namelen = MAXNAMLEN; 366 } 367 368 int 369 linux_statfs(struct thread *td, struct linux_statfs_args *args) 370 { 371 struct l_statfs linux_statfs; 372 struct statfs bsd_statfs; 373 char *path; 374 int error; 375 376 LCONVPATHEXIST(td, args->path, &path); 377 378 #ifdef DEBUG 379 if (ldebug(statfs)) 380 printf(ARGS(statfs, "%s, *"), path); 381 #endif 382 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 383 LFREEPATH(path); 384 if (error) 385 return (error); 386 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 387 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 388 } 389 390 int 391 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 392 { 393 struct l_statfs linux_statfs; 394 struct statfs bsd_statfs; 395 int error; 396 397 #ifdef DEBUG 398 if (ldebug(fstatfs)) 399 printf(ARGS(fstatfs, "%d, *"), args->fd); 400 #endif 401 error = kern_fstatfs(td, args->fd, &bsd_statfs); 402 if (error) 403 return error; 404 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 405 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 406 } 407 408 struct l_ustat 409 { 410 l_daddr_t f_tfree; 411 l_ino_t f_tinode; 412 char f_fname[6]; 413 char f_fpack[6]; 414 }; 415 416 int 417 linux_ustat(struct thread *td, struct linux_ustat_args *args) 418 { 419 #ifdef DEBUG 420 if (ldebug(ustat)) 421 printf(ARGS(ustat, "%d, *"), args->dev); 422 #endif 423 424 return (EOPNOTSUPP); 425 } 426 427 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 428 429 static int 430 stat64_copyout(struct stat *buf, void *ubuf) 431 { 432 struct l_stat64 lbuf; 433 434 bzero(&lbuf, sizeof(lbuf)); 435 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 436 lbuf.st_ino = buf->st_ino; 437 lbuf.st_mode = buf->st_mode; 438 lbuf.st_nlink = buf->st_nlink; 439 lbuf.st_uid = buf->st_uid; 440 lbuf.st_gid = buf->st_gid; 441 lbuf.st_rdev = buf->st_rdev; 442 lbuf.st_size = buf->st_size; 443 lbuf.st_atime = buf->st_atime; 444 lbuf.st_mtime = buf->st_mtime; 445 lbuf.st_ctime = buf->st_ctime; 446 lbuf.st_blksize = buf->st_blksize; 447 lbuf.st_blocks = buf->st_blocks; 448 449 /* 450 * The __st_ino field makes all the difference. In the Linux kernel 451 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 452 * but without the assignment to __st_ino the runtime linker refuses 453 * to mmap(2) any shared libraries. I guess it's broken alright :-) 454 */ 455 lbuf.__st_ino = buf->st_ino; 456 457 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 458 } 459 460 int 461 linux_stat64(struct thread *td, struct linux_stat64_args *args) 462 { 463 struct stat buf; 464 char *filename; 465 int error; 466 467 LCONVPATHEXIST(td, args->filename, &filename); 468 469 #ifdef DEBUG 470 if (ldebug(stat64)) 471 printf(ARGS(stat64, "%s, *"), filename); 472 #endif 473 474 error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 475 if (!error && strlen(filename) > strlen("/dev/pts/") && 476 !strncmp(filename, "/dev/pts/", strlen("/dev/pts/")) 477 && filename[9] >= '0' && filename[9] <= '9') { 478 /* 479 * Linux checks major and minors of the slave device to make 480 * sure it's a pty deivce, so let's make him believe it is. 481 */ 482 buf.st_rdev = (136 << 8); 483 } 484 485 translate_path_major_minor(td, filename, &buf); 486 487 LFREEPATH(filename); 488 if (error) 489 return (error); 490 return (stat64_copyout(&buf, args->statbuf)); 491 } 492 493 int 494 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 495 { 496 struct stat sb; 497 char *filename; 498 int error; 499 500 LCONVPATHEXIST(td, args->filename, &filename); 501 502 #ifdef DEBUG 503 if (ldebug(lstat64)) 504 printf(ARGS(lstat64, "%s, *"), args->filename); 505 #endif 506 507 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 508 translate_path_major_minor(td, filename, &sb); 509 LFREEPATH(filename); 510 if (error) 511 return (error); 512 return (stat64_copyout(&sb, args->statbuf)); 513 } 514 515 int 516 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 517 { 518 struct stat buf; 519 int error; 520 521 #ifdef DEBUG 522 if (ldebug(fstat64)) 523 printf(ARGS(fstat64, "%d, *"), args->fd); 524 #endif 525 526 error = kern_fstat(td, args->fd, &buf); 527 translate_fd_major_minor(td, args->fd, &buf); 528 if (!error) 529 error = stat64_copyout(&buf, args->statbuf); 530 531 return (error); 532 } 533 534 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 535