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