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 struct l_ustat lu; 289 struct cdev *dev; 290 struct vnode *vp; 291 struct statfs *stat; 292 int error; 293 294 #ifdef DEBUG 295 if (ldebug(ustat)) 296 printf(ARGS(ustat, "%d, *"), args->dev); 297 #endif 298 299 /* 300 * lu.f_fname and lu.f_fpack are not used. They are always zeroed. 301 * lu.f_tinode and lu.f_tfree are set from the device's super block. 302 */ 303 bzero(&lu, sizeof(lu)); 304 305 /* 306 * XXX - Don't return an error if we can't find a vnode for the 307 * device. Our struct cdev *is 32-bits whereas Linux only has a 16-bits 308 * struct cdev *. The struct cdev *that is used now may as well be a truncated 309 * struct cdev *returned from previous syscalls. Just return a bzeroed 310 * ustat in that case. 311 * 312 * XXX: findcdev() SHALL not be used this way. Somebody (TM) will 313 * have to find a better way. It may be that we should stick 314 * a dev_t into struct mount, and walk the mountlist for a 315 * perfect match and failing that try again looking for a 316 * minor-truncated match. 317 */ 318 dev = findcdev(makedev(args->dev >> 8, args->dev & 0xFF)); 319 if (dev != NULL && vfinddev(dev, &vp)) { 320 if (vp->v_mount == NULL) 321 return (EINVAL); 322 #ifdef MAC 323 error = mac_check_mount_stat(td->td_ucred, vp->v_mount); 324 if (error) 325 return (error); 326 #endif 327 stat = &(vp->v_mount->mnt_stat); 328 error = VFS_STATFS(vp->v_mount, stat, td); 329 if (error) 330 return (error); 331 332 lu.f_tfree = stat->f_bfree; 333 lu.f_tinode = stat->f_ffree; 334 } 335 336 return (copyout(&lu, args->ubuf, sizeof(lu))); 337 } 338 339 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 340 341 static int 342 stat64_copyout(struct stat *buf, void *ubuf) 343 { 344 struct l_stat64 lbuf; 345 struct cdevsw *cdevsw; 346 struct cdev *dev; 347 348 bzero(&lbuf, sizeof(lbuf)); 349 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 350 lbuf.st_ino = buf->st_ino; 351 lbuf.st_mode = buf->st_mode; 352 lbuf.st_nlink = buf->st_nlink; 353 lbuf.st_uid = buf->st_uid; 354 lbuf.st_gid = buf->st_gid; 355 lbuf.st_rdev = buf->st_rdev; 356 lbuf.st_size = buf->st_size; 357 lbuf.st_atime = buf->st_atime; 358 lbuf.st_mtime = buf->st_mtime; 359 lbuf.st_ctime = buf->st_ctime; 360 lbuf.st_blksize = buf->st_blksize; 361 lbuf.st_blocks = buf->st_blocks; 362 363 /* Lie about disk drives which are character devices 364 * in FreeBSD but block devices under Linux. 365 */ 366 if (S_ISCHR(lbuf.st_mode) && 367 (dev = findcdev(buf->st_rdev)) != NULL) { 368 cdevsw = dev_refthread(dev); 369 if (cdevsw != NULL) { 370 if (cdevsw->d_flags & D_DISK) { 371 lbuf.st_mode &= ~S_IFMT; 372 lbuf.st_mode |= S_IFBLK; 373 374 /* XXX this may not be quite right */ 375 /* Map major number to 0 */ 376 lbuf.st_dev = uminor(buf->st_dev) & 0xf; 377 lbuf.st_rdev = buf->st_rdev & 0xff; 378 } 379 dev_relthread(dev); 380 } 381 } 382 383 /* 384 * The __st_ino field makes all the difference. In the Linux kernel 385 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 386 * but without the assignment to __st_ino the runtime linker refuses 387 * to mmap(2) any shared libraries. I guess it's broken alright :-) 388 */ 389 lbuf.__st_ino = buf->st_ino; 390 391 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 392 } 393 394 int 395 linux_stat64(struct thread *td, struct linux_stat64_args *args) 396 { 397 struct stat buf; 398 char *filename; 399 int error; 400 401 LCONVPATHEXIST(td, args->filename, &filename); 402 403 #ifdef DEBUG 404 if (ldebug(stat64)) 405 printf(ARGS(stat64, "%s, *"), filename); 406 #endif 407 408 error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 409 LFREEPATH(filename); 410 if (error) 411 return (error); 412 return (stat64_copyout(&buf, args->statbuf)); 413 } 414 415 int 416 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 417 { 418 struct stat sb; 419 char *filename; 420 int error; 421 422 LCONVPATHEXIST(td, args->filename, &filename); 423 424 #ifdef DEBUG 425 if (ldebug(lstat64)) 426 printf(ARGS(lstat64, "%s, *"), args->filename); 427 #endif 428 429 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 430 LFREEPATH(filename); 431 if (error) 432 return (error); 433 return (stat64_copyout(&sb, args->statbuf)); 434 } 435 436 int 437 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 438 { 439 struct stat buf; 440 int error; 441 442 #ifdef DEBUG 443 if (ldebug(fstat64)) 444 printf(ARGS(fstat64, "%d, *"), args->fd); 445 #endif 446 447 error = kern_fstat(td, args->fd, &buf); 448 if (!error) 449 error = stat64_copyout(&buf, args->statbuf); 450 451 return (error); 452 } 453 454 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 455