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