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