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 withough 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 <sys/param.h> 32 #include <sys/conf.h> 33 #include <sys/dirent.h> 34 #include <sys/file.h> 35 #include <sys/filedesc.h> 36 #include <sys/proc.h> 37 #include <sys/mount.h> 38 #include <sys/namei.h> 39 #include <sys/stat.h> 40 #include <sys/systm.h> 41 #include <sys/vnode.h> 42 43 #include <machine/../linux/linux.h> 44 #include <machine/../linux/linux_proto.h> 45 #include <compat/linux/linux_util.h> 46 47 #include <sys/sysctl.h> 48 49 struct linux_newstat { 50 #ifdef __alpha__ 51 u_int stat_dev; 52 u_int stat_ino; 53 u_int stat_mode; 54 u_int stat_nlink; 55 u_int stat_uid; 56 u_int stat_gid; 57 u_int stat_rdev; 58 long stat_size; 59 u_long stat_atime; 60 u_long stat_mtime; 61 u_long stat_ctime; 62 u_int stat_blksize; 63 int stat_blocks; 64 u_int stat_flags; 65 u_int stat_gen; 66 #else 67 u_short stat_dev; 68 u_short __pad1; 69 u_long stat_ino; 70 u_short stat_mode; 71 u_short stat_nlink; 72 u_short stat_uid; 73 u_short stat_gid; 74 u_short stat_rdev; 75 u_short __pad2; 76 u_long stat_size; 77 u_long stat_blksize; 78 u_long stat_blocks; 79 u_long stat_atime; 80 u_long __unused1; 81 u_long stat_mtime; 82 u_long __unused2; 83 u_long stat_ctime; 84 u_long __unused3; 85 u_long __unused4; 86 u_long __unused5; 87 #endif 88 }; 89 90 91 struct linux_ustat 92 { 93 int f_tfree; 94 u_long f_tinode; 95 char f_fname[6]; 96 char f_fpack[6]; 97 }; 98 99 static int 100 newstat_copyout(struct stat *buf, void *ubuf) 101 { 102 struct linux_newstat tbuf; 103 struct cdevsw *cdevsw; 104 dev_t dev; 105 106 tbuf.stat_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 107 tbuf.stat_ino = buf->st_ino; 108 tbuf.stat_mode = buf->st_mode; 109 tbuf.stat_nlink = buf->st_nlink; 110 tbuf.stat_uid = buf->st_uid; 111 tbuf.stat_gid = buf->st_gid; 112 tbuf.stat_rdev = buf->st_rdev; 113 tbuf.stat_size = buf->st_size; 114 tbuf.stat_atime = buf->st_atime; 115 tbuf.stat_mtime = buf->st_mtime; 116 tbuf.stat_ctime = buf->st_ctime; 117 tbuf.stat_blksize = buf->st_blksize; 118 tbuf.stat_blocks = buf->st_blocks; 119 120 /* Lie about disk drives which are character devices 121 * in FreeBSD but block devices under Linux. 122 */ 123 if (tbuf.stat_mode & S_IFCHR && 124 (dev = udev2dev(buf->st_rdev, 0)) != NODEV) { 125 cdevsw = devsw(dev); 126 if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) { 127 tbuf.stat_mode &= ~S_IFCHR; 128 tbuf.stat_mode |= S_IFBLK; 129 130 /* XXX this may not be quite right */ 131 /* Map major number to 0 */ 132 tbuf.stat_dev = uminor(buf->st_dev) & 0xf; 133 tbuf.stat_rdev = buf->st_rdev & 0xff; 134 } 135 } 136 137 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 138 } 139 140 int 141 linux_newstat(struct proc *p, struct linux_newstat_args *args) 142 { 143 struct stat buf; 144 struct nameidata nd; 145 int error; 146 caddr_t sg; 147 148 sg = stackgap_init(); 149 CHECKALTEXIST(p, &sg, args->path); 150 151 #ifdef DEBUG 152 printf("Linux-emul(%ld): newstat(%s, *)\n", (long)p->p_pid, 153 args->path); 154 #endif 155 156 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE, 157 args->path, p); 158 error = namei(&nd); 159 if (error) 160 return (error); 161 NDFREE(&nd, NDF_ONLY_PNBUF); 162 163 error = vn_stat(nd.ni_vp, &buf, p); 164 vput(nd.ni_vp); 165 if (error) 166 return (error); 167 168 return (newstat_copyout(&buf, args->buf)); 169 } 170 171 /* 172 * Get file status; this version does not follow links. 173 */ 174 int 175 linux_newlstat(p, uap) 176 struct proc *p; 177 struct linux_newlstat_args *uap; 178 { 179 int error; 180 struct vnode *vp; 181 struct stat sb; 182 struct nameidata nd; 183 caddr_t sg; 184 185 sg = stackgap_init(); 186 CHECKALTEXIST(p, &sg, uap->path); 187 188 #ifdef DEBUG 189 printf("Linux-emul(%ld): newlstat(%s, *)\n", (long)p->p_pid, 190 uap->path); 191 #endif 192 193 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE, 194 uap->path, p); 195 error = namei(&nd); 196 if (error) 197 return (error); 198 NDFREE(&nd, NDF_ONLY_PNBUF); 199 200 vp = nd.ni_vp; 201 error = vn_stat(vp, &sb, p); 202 vput(vp); 203 if (error) 204 return (error); 205 206 return (newstat_copyout(&sb, uap->buf)); 207 } 208 209 int 210 linux_newfstat(struct proc *p, struct linux_newfstat_args *args) 211 { 212 struct filedesc *fdp; 213 struct file *fp; 214 struct stat buf; 215 int error; 216 217 fdp = p->p_fd; 218 219 #ifdef DEBUG 220 printf("Linux-emul(%ld): newfstat(%d, *)\n", (long)p->p_pid, args->fd); 221 #endif 222 223 if ((unsigned)args->fd >= fdp->fd_nfiles || 224 (fp = fdp->fd_ofiles[args->fd]) == NULL) 225 return (EBADF); 226 227 error = fo_stat(fp, &buf, p); 228 if (!error) 229 error = newstat_copyout(&buf, args->buf); 230 231 return (error); 232 } 233 234 struct linux_statfs_buf { 235 int ftype; 236 int fbsize; 237 int fblocks; 238 int fbfree; 239 int fbavail; 240 int ffiles; 241 int fffree; 242 linux_fsid_t ffsid; 243 int fnamelen; 244 int fspare[6]; 245 }; 246 247 #ifndef VT_NWFS 248 #define VT_NWFS VT_TFS /* XXX - bug compatibility with sys/nwfs/nwfs_node.h */ 249 #endif 250 251 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 252 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 253 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 254 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 255 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 256 #define LINUX_NCP_SUPER_MAGIC 0x564cL 257 #define LINUX_NFS_SUPER_MAGIC 0x6969L 258 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 259 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 260 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 261 262 /* 263 * ext2fs uses the VT_UFS tag. A mounted ext2 filesystem will therefore 264 * be seen as an ufs/mfs filesystem. 265 */ 266 static long 267 bsd_to_linux_ftype(int tag) 268 { 269 270 switch (tag) { 271 case VT_CODA: 272 return (LINUX_CODA_SUPER_MAGIC); 273 case VT_HPFS: 274 return (LINUX_HPFS_SUPER_MAGIC); 275 case VT_ISOFS: 276 return (LINUX_ISOFS_SUPER_MAGIC); 277 case VT_MFS: 278 return (LINUX_UFS_SUPER_MAGIC); 279 case VT_MSDOSFS: 280 return (LINUX_MSDOS_SUPER_MAGIC); 281 case VT_NFS: 282 return (LINUX_NFS_SUPER_MAGIC); 283 case VT_NTFS: 284 return (LINUX_NTFS_SUPER_MAGIC); 285 case VT_NWFS: 286 return (LINUX_NCP_SUPER_MAGIC); 287 case VT_PROCFS: 288 return (LINUX_PROC_SUPER_MAGIC); 289 case VT_UFS: 290 return (LINUX_UFS_SUPER_MAGIC); 291 } 292 293 return (0L); 294 } 295 296 int 297 linux_statfs(struct proc *p, struct linux_statfs_args *args) 298 { 299 struct mount *mp; 300 struct nameidata *ndp; 301 struct statfs *bsd_statfs; 302 struct nameidata nd; 303 struct linux_statfs_buf linux_statfs_buf; 304 int error; 305 caddr_t sg; 306 307 sg = stackgap_init(); 308 CHECKALTEXIST(p, &sg, args->path); 309 310 #ifdef DEBUG 311 printf("Linux-emul(%d): statfs(%s, *)\n", p->p_pid, args->path); 312 #endif 313 ndp = &nd; 314 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, curproc); 315 error = namei(ndp); 316 if (error) 317 return error; 318 NDFREE(ndp, NDF_ONLY_PNBUF); 319 mp = ndp->ni_vp->v_mount; 320 bsd_statfs = &mp->mnt_stat; 321 vrele(ndp->ni_vp); 322 error = VFS_STATFS(mp, bsd_statfs, p); 323 if (error) 324 return error; 325 bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 326 linux_statfs_buf.ftype = bsd_to_linux_ftype(bsd_statfs->f_type); 327 linux_statfs_buf.fbsize = bsd_statfs->f_bsize; 328 linux_statfs_buf.fblocks = bsd_statfs->f_blocks; 329 linux_statfs_buf.fbfree = bsd_statfs->f_bfree; 330 linux_statfs_buf.fbavail = bsd_statfs->f_bavail; 331 linux_statfs_buf.fffree = bsd_statfs->f_ffree; 332 linux_statfs_buf.ffiles = bsd_statfs->f_files; 333 linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0]; 334 linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1]; 335 linux_statfs_buf.fnamelen = MAXNAMLEN; 336 return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf, 337 sizeof(struct linux_statfs_buf)); 338 } 339 340 int 341 linux_fstatfs(struct proc *p, struct linux_fstatfs_args *args) 342 { 343 struct file *fp; 344 struct mount *mp; 345 struct statfs *bsd_statfs; 346 struct linux_statfs_buf linux_statfs_buf; 347 int error; 348 349 #ifdef DEBUG 350 printf("Linux-emul(%d): fstatfs(%d, *)\n", p->p_pid, args->fd); 351 #endif 352 error = getvnode(p->p_fd, args->fd, &fp); 353 if (error) 354 return error; 355 mp = ((struct vnode *)fp->f_data)->v_mount; 356 bsd_statfs = &mp->mnt_stat; 357 error = VFS_STATFS(mp, bsd_statfs, p); 358 if (error) 359 return error; 360 bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 361 linux_statfs_buf.ftype = bsd_to_linux_ftype(bsd_statfs->f_type); 362 linux_statfs_buf.fbsize = bsd_statfs->f_bsize; 363 linux_statfs_buf.fblocks = bsd_statfs->f_blocks; 364 linux_statfs_buf.fbfree = bsd_statfs->f_bfree; 365 linux_statfs_buf.fbavail = bsd_statfs->f_bavail; 366 linux_statfs_buf.fffree = bsd_statfs->f_ffree; 367 linux_statfs_buf.ffiles = bsd_statfs->f_files; 368 linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0]; 369 linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1]; 370 linux_statfs_buf.fnamelen = MAXNAMLEN; 371 return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf, 372 sizeof(struct linux_statfs_buf)); 373 } 374 375 int 376 linux_ustat(p, uap) 377 struct proc *p; 378 struct linux_ustat_args *uap; 379 { 380 struct linux_ustat lu; 381 dev_t dev; 382 struct vnode *vp; 383 struct statfs *stat; 384 int error; 385 386 #ifdef DEBUG 387 printf("Linux-emul(%ld): ustat(%d, *)\n", (long)p->p_pid, uap->dev); 388 #endif 389 390 /* 391 * lu.f_fname and lu.f_fpack are not used. They are always zeroed. 392 * lu.f_tinode and lu.f_tfree are set from the device's super block. 393 */ 394 bzero(&lu, sizeof(lu)); 395 396 /* 397 * XXX - Don't return an error if we can't find a vnode for the 398 * device. Our dev_t is 32-bits whereas Linux only has a 16-bits 399 * dev_t. The dev_t that is used now may as well be a truncated 400 * dev_t returned from previous syscalls. Just return a bzeroed 401 * ustat in that case. 402 */ 403 dev = makedev(uap->dev >> 8, uap->dev & 0xFF); 404 if (vfinddev(dev, VCHR, &vp)) { 405 if (vp->v_mount == NULL) 406 return (EINVAL); 407 stat = &(vp->v_mount->mnt_stat); 408 error = VFS_STATFS(vp->v_mount, stat, p); 409 if (error) 410 return (error); 411 412 lu.f_tfree = stat->f_bfree; 413 lu.f_tinode = stat->f_ffree; 414 } 415 416 return (copyout(&lu, uap->ubuf, sizeof(lu))); 417 } 418