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); 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); 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); 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, mp); 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 386 bzero(&lbuf, sizeof(lbuf)); 387 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 388 lbuf.st_ino = buf->st_ino; 389 lbuf.st_mode = buf->st_mode; 390 lbuf.st_nlink = buf->st_nlink; 391 lbuf.st_uid = buf->st_uid; 392 lbuf.st_gid = buf->st_gid; 393 lbuf.st_rdev = buf->st_rdev; 394 lbuf.st_size = buf->st_size; 395 lbuf.st_atime = buf->st_atime; 396 lbuf.st_mtime = buf->st_mtime; 397 lbuf.st_ctime = buf->st_ctime; 398 lbuf.st_blksize = buf->st_blksize; 399 lbuf.st_blocks = buf->st_blocks; 400 401 /* 402 * The __st_ino field makes all the difference. In the Linux kernel 403 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 404 * but without the assignment to __st_ino the runtime linker refuses 405 * to mmap(2) any shared libraries. I guess it's broken alright :-) 406 */ 407 lbuf.__st_ino = buf->st_ino; 408 409 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 410 } 411 412 int 413 linux_stat64(struct thread *td, struct linux_stat64_args *args) 414 { 415 struct stat buf; 416 struct nameidata nd; 417 int error; 418 caddr_t sg; 419 420 sg = stackgap_init(); 421 CHECKALTEXIST(td, &sg, args->filename); 422 423 #ifdef DEBUG 424 if (ldebug(stat64)) 425 printf(ARGS(stat64, "%s, *"), args->filename); 426 #endif 427 428 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE, 429 args->filename, td); 430 error = namei(&nd); 431 if (error) 432 return (error); 433 NDFREE(&nd, NDF_ONLY_PNBUF); 434 435 error = vn_stat(nd.ni_vp, &buf, td); 436 vput(nd.ni_vp); 437 if (error) 438 return (error); 439 440 return (stat64_copyout(&buf, args->statbuf)); 441 } 442 443 int 444 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 445 { 446 int error; 447 struct stat sb; 448 struct nameidata nd; 449 caddr_t sg; 450 451 sg = stackgap_init(); 452 CHECKALTEXIST(td, &sg, args->filename); 453 454 #ifdef DEBUG 455 if (ldebug(lstat64)) 456 printf(ARGS(lstat64, "%s, *"), args->filename); 457 #endif 458 459 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE, 460 args->filename, td); 461 error = namei(&nd); 462 if (error) 463 return (error); 464 NDFREE(&nd, NDF_ONLY_PNBUF); 465 466 error = vn_stat(nd.ni_vp, &sb, td); 467 vput(nd.ni_vp); 468 if (error) 469 return (error); 470 471 return (stat64_copyout(&sb, args->statbuf)); 472 } 473 474 int 475 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 476 { 477 struct filedesc *fdp; 478 struct file *fp; 479 struct stat buf; 480 int error; 481 482 #ifdef DEBUG 483 if (ldebug(fstat64)) 484 printf(ARGS(fstat64, "%d, *"), args->fd); 485 #endif 486 487 fdp = td->td_proc->p_fd; 488 if ((unsigned)args->fd >= fdp->fd_nfiles || 489 (fp = fdp->fd_ofiles[args->fd]) == NULL) 490 return (EBADF); 491 492 error = fo_stat(fp, &buf, td); 493 if (!error) 494 error = stat64_copyout(&buf, args->statbuf); 495 496 return (error); 497 } 498 499 #endif /* __i386__ */ 500