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