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