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