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