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