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