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 file *fp; 126 int fd; 127 int temp; 128 129 temp = td->td_retval[0]; 130 if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0) 131 return; 132 fd = td->td_retval[0]; 133 td->td_retval[0] = temp; 134 translate_fd_major_minor(td, fd, buf); 135 fget(td, fd, &fp); 136 closef(fp, td); 137 } 138 139 static int 140 newstat_copyout(struct stat *buf, void *ubuf) 141 { 142 struct l_newstat tbuf; 143 144 bzero(&tbuf, sizeof(tbuf)); 145 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 146 tbuf.st_ino = buf->st_ino; 147 tbuf.st_mode = buf->st_mode; 148 tbuf.st_nlink = buf->st_nlink; 149 tbuf.st_uid = buf->st_uid; 150 tbuf.st_gid = buf->st_gid; 151 tbuf.st_rdev = buf->st_rdev; 152 tbuf.st_size = buf->st_size; 153 tbuf.st_atime = buf->st_atime; 154 tbuf.st_mtime = buf->st_mtime; 155 tbuf.st_ctime = buf->st_ctime; 156 tbuf.st_blksize = buf->st_blksize; 157 tbuf.st_blocks = buf->st_blocks; 158 159 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 160 } 161 162 int 163 linux_newstat(struct thread *td, struct linux_newstat_args *args) 164 { 165 struct stat buf; 166 char *path; 167 int error; 168 169 LCONVPATHEXIST(td, args->path, &path); 170 171 #ifdef DEBUG 172 if (ldebug(newstat)) 173 printf(ARGS(newstat, "%s, *"), path); 174 #endif 175 176 error = kern_stat(td, path, UIO_SYSSPACE, &buf); 177 if (!error && strlen(path) > strlen("/dev/pts/") && 178 !strncmp(path, "/dev/pts/", strlen("/dev/pts/")) 179 && path[9] >= '0' && path[9] <= '9') { 180 /* 181 * Linux checks major and minors of the slave device to make 182 * sure it's a pty device, so let's make him believe it is. 183 */ 184 buf.st_rdev = (136 << 8); 185 } 186 187 translate_path_major_minor(td, path, &buf); 188 189 LFREEPATH(path); 190 if (error) 191 return (error); 192 return (newstat_copyout(&buf, args->buf)); 193 } 194 195 int 196 linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 197 { 198 struct stat sb; 199 char *path; 200 int error; 201 202 LCONVPATHEXIST(td, args->path, &path); 203 204 #ifdef DEBUG 205 if (ldebug(newlstat)) 206 printf(ARGS(newlstat, "%s, *"), path); 207 #endif 208 209 error = kern_lstat(td, path, UIO_SYSSPACE, &sb); 210 translate_path_major_minor(td, path, &sb); 211 LFREEPATH(path); 212 if (error) 213 return (error); 214 return (newstat_copyout(&sb, args->buf)); 215 } 216 217 int 218 linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 219 { 220 struct stat buf; 221 int error; 222 223 #ifdef DEBUG 224 if (ldebug(newfstat)) 225 printf(ARGS(newfstat, "%d, *"), args->fd); 226 #endif 227 228 error = kern_fstat(td, args->fd, &buf); 229 translate_fd_major_minor(td, args->fd, &buf); 230 if (!error) 231 error = newstat_copyout(&buf, args->buf); 232 233 return (error); 234 } 235 236 #ifndef __alpha__ 237 static int 238 stat_copyout(struct stat *buf, void *ubuf) 239 { 240 struct l_stat lbuf; 241 242 bzero(&lbuf, sizeof(lbuf)); 243 lbuf.st_dev = buf->st_dev; 244 lbuf.st_ino = buf->st_ino; 245 lbuf.st_mode = buf->st_mode; 246 lbuf.st_nlink = buf->st_nlink; 247 lbuf.st_uid = buf->st_uid; 248 lbuf.st_gid = buf->st_gid; 249 lbuf.st_rdev = buf->st_rdev; 250 if (buf->st_size < (quad_t)1 << 32) 251 lbuf.st_size = buf->st_size; 252 else 253 lbuf.st_size = -2; 254 lbuf.st_atime = buf->st_atime; 255 lbuf.st_mtime = buf->st_mtime; 256 lbuf.st_ctime = buf->st_ctime; 257 lbuf.st_blksize = buf->st_blksize; 258 lbuf.st_blocks = buf->st_blocks; 259 lbuf.st_flags = buf->st_flags; 260 lbuf.st_gen = buf->st_gen; 261 262 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 263 } 264 265 int 266 linux_stat(struct thread *td, struct linux_stat_args *args) 267 { 268 struct stat buf; 269 int error; 270 #ifdef DEBUG 271 if (ldebug(stat)) 272 printf(ARGS(stat, "%s, *"), args->path); 273 #endif 274 error = kern_stat(td, args->path, UIO_SYSSPACE, &buf); 275 if (error) 276 return (error); 277 translate_path_major_minor(td, args->path, &buf); 278 return(stat_copyout(&buf, args->up)); 279 } 280 281 int 282 linux_lstat(struct thread *td, struct linux_lstat_args *args) 283 { 284 struct stat buf; 285 int error; 286 287 #ifdef DEBUG 288 if (ldebug(lstat)) 289 printf(ARGS(lstat, "%s, *"), args->path); 290 #endif 291 error = kern_lstat(td, args->path, UIO_SYSSPACE, &buf); 292 if (error) 293 return (error); 294 translate_path_major_minor(td, args->path, &buf); 295 return(stat_copyout(&buf, args->up)); 296 } 297 #endif 298 299 /* XXX - All fields of type l_int are defined as l_long on i386 */ 300 struct l_statfs { 301 l_int f_type; 302 l_int f_bsize; 303 l_int f_blocks; 304 l_int f_bfree; 305 l_int f_bavail; 306 l_int f_files; 307 l_int f_ffree; 308 l_fsid_t f_fsid; 309 l_int f_namelen; 310 l_int f_spare[6]; 311 }; 312 313 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 314 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 315 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 316 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 317 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 318 #define LINUX_NCP_SUPER_MAGIC 0x564cL 319 #define LINUX_NFS_SUPER_MAGIC 0x6969L 320 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 321 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 322 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 323 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 324 325 static long 326 bsd_to_linux_ftype(const char *fstypename) 327 { 328 int i; 329 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 330 {"ufs", LINUX_UFS_SUPER_MAGIC}, 331 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 332 {"nfs", LINUX_NFS_SUPER_MAGIC}, 333 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 334 {"procfs", LINUX_PROC_SUPER_MAGIC}, 335 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 336 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 337 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 338 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 339 {"coda", LINUX_CODA_SUPER_MAGIC}, 340 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 341 {NULL, 0L}}; 342 343 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 344 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 345 return (b2l_tbl[i].linux_type); 346 347 return (0L); 348 } 349 350 static void 351 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 352 { 353 354 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 355 linux_statfs->f_bsize = bsd_statfs->f_bsize; 356 linux_statfs->f_blocks = bsd_statfs->f_blocks; 357 linux_statfs->f_bfree = bsd_statfs->f_bfree; 358 linux_statfs->f_bavail = bsd_statfs->f_bavail; 359 linux_statfs->f_ffree = bsd_statfs->f_ffree; 360 linux_statfs->f_files = bsd_statfs->f_files; 361 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 362 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 363 linux_statfs->f_namelen = MAXNAMLEN; 364 } 365 366 int 367 linux_statfs(struct thread *td, struct linux_statfs_args *args) 368 { 369 struct l_statfs linux_statfs; 370 struct statfs bsd_statfs; 371 char *path; 372 int error; 373 374 LCONVPATHEXIST(td, args->path, &path); 375 376 #ifdef DEBUG 377 if (ldebug(statfs)) 378 printf(ARGS(statfs, "%s, *"), path); 379 #endif 380 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 381 LFREEPATH(path); 382 if (error) 383 return (error); 384 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 385 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 386 } 387 388 int 389 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 390 { 391 struct l_statfs linux_statfs; 392 struct statfs bsd_statfs; 393 int error; 394 395 #ifdef DEBUG 396 if (ldebug(fstatfs)) 397 printf(ARGS(fstatfs, "%d, *"), args->fd); 398 #endif 399 error = kern_fstatfs(td, args->fd, &bsd_statfs); 400 if (error) 401 return error; 402 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 403 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 404 } 405 406 struct l_ustat 407 { 408 l_daddr_t f_tfree; 409 l_ino_t f_tinode; 410 char f_fname[6]; 411 char f_fpack[6]; 412 }; 413 414 int 415 linux_ustat(struct thread *td, struct linux_ustat_args *args) 416 { 417 #ifdef DEBUG 418 if (ldebug(ustat)) 419 printf(ARGS(ustat, "%d, *"), args->dev); 420 #endif 421 422 return (EOPNOTSUPP); 423 } 424 425 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 426 427 static int 428 stat64_copyout(struct stat *buf, void *ubuf) 429 { 430 struct l_stat64 lbuf; 431 432 bzero(&lbuf, sizeof(lbuf)); 433 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 434 lbuf.st_ino = buf->st_ino; 435 lbuf.st_mode = buf->st_mode; 436 lbuf.st_nlink = buf->st_nlink; 437 lbuf.st_uid = buf->st_uid; 438 lbuf.st_gid = buf->st_gid; 439 lbuf.st_rdev = buf->st_rdev; 440 lbuf.st_size = buf->st_size; 441 lbuf.st_atime = buf->st_atime; 442 lbuf.st_mtime = buf->st_mtime; 443 lbuf.st_ctime = buf->st_ctime; 444 lbuf.st_blksize = buf->st_blksize; 445 lbuf.st_blocks = buf->st_blocks; 446 447 /* 448 * The __st_ino field makes all the difference. In the Linux kernel 449 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 450 * but without the assignment to __st_ino the runtime linker refuses 451 * to mmap(2) any shared libraries. I guess it's broken alright :-) 452 */ 453 lbuf.__st_ino = buf->st_ino; 454 455 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 456 } 457 458 int 459 linux_stat64(struct thread *td, struct linux_stat64_args *args) 460 { 461 struct stat buf; 462 char *filename; 463 int error; 464 465 LCONVPATHEXIST(td, args->filename, &filename); 466 467 #ifdef DEBUG 468 if (ldebug(stat64)) 469 printf(ARGS(stat64, "%s, *"), filename); 470 #endif 471 472 error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 473 if (!error && strlen(filename) > strlen("/dev/pts/") && 474 !strncmp(filename, "/dev/pts/", strlen("/dev/pts/")) 475 && filename[9] >= '0' && filename[9] <= '9') { 476 /* 477 * Linux checks major and minors of the slave device to make 478 * sure it's a pty deivce, so let's make him believe it is. 479 */ 480 buf.st_rdev = (136 << 8); 481 } 482 483 translate_path_major_minor(td, filename, &buf); 484 485 LFREEPATH(filename); 486 if (error) 487 return (error); 488 return (stat64_copyout(&buf, args->statbuf)); 489 } 490 491 int 492 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 493 { 494 struct stat sb; 495 char *filename; 496 int error; 497 498 LCONVPATHEXIST(td, args->filename, &filename); 499 500 #ifdef DEBUG 501 if (ldebug(lstat64)) 502 printf(ARGS(lstat64, "%s, *"), args->filename); 503 #endif 504 505 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 506 translate_path_major_minor(td, filename, &sb); 507 LFREEPATH(filename); 508 if (error) 509 return (error); 510 return (stat64_copyout(&sb, args->statbuf)); 511 } 512 513 int 514 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 515 { 516 struct stat buf; 517 int error; 518 519 #ifdef DEBUG 520 if (ldebug(fstat64)) 521 printf(ARGS(fstat64, "%d, *"), args->fd); 522 #endif 523 524 error = kern_fstat(td, args->fd, &buf); 525 translate_fd_major_minor(td, args->fd, &buf); 526 if (!error) 527 error = stat64_copyout(&buf, args->statbuf); 528 529 return (error); 530 } 531 532 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 533