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/systm.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/socketvar.h> 40 #include <sys/stat.h> 41 #include <sys/vnode.h> 42 #include <sys/pipe.h> 43 44 #include <i386/linux/linux.h> 45 #include <i386/linux/linux_proto.h> 46 #include <i386/linux/linux_util.h> 47 48 struct linux_newstat { 49 u_short stat_dev; 50 u_short __pad1; 51 u_long stat_ino; 52 u_short stat_mode; 53 u_short stat_nlink; 54 u_short stat_uid; 55 u_short stat_gid; 56 u_short stat_rdev; 57 u_short __pad2; 58 u_long stat_size; 59 u_long stat_blksize; 60 u_long stat_blocks; 61 u_long stat_atime; 62 u_long __unused1; 63 u_long stat_mtime; 64 u_long __unused2; 65 u_long stat_ctime; 66 u_long __unused3; 67 u_long __unused4; 68 u_long __unused5; 69 }; 70 71 static int 72 newstat_copyout(struct stat *buf, void *ubuf) 73 { 74 struct linux_newstat tbuf; 75 76 tbuf.stat_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 77 tbuf.stat_ino = buf->st_ino; 78 tbuf.stat_mode = buf->st_mode; 79 tbuf.stat_nlink = buf->st_nlink; 80 tbuf.stat_uid = buf->st_uid; 81 tbuf.stat_gid = buf->st_gid; 82 tbuf.stat_rdev = buf->st_rdev; 83 tbuf.stat_size = buf->st_size; 84 tbuf.stat_atime = buf->st_atime; 85 tbuf.stat_mtime = buf->st_mtime; 86 tbuf.stat_ctime = buf->st_ctime; 87 tbuf.stat_blksize = buf->st_blksize; 88 tbuf.stat_blocks = buf->st_blocks; 89 90 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 91 } 92 93 int 94 linux_newstat(struct proc *p, struct linux_newstat_args *args) 95 { 96 struct stat buf; 97 struct nameidata nd; 98 int error; 99 caddr_t sg; 100 101 sg = stackgap_init(); 102 CHECKALTEXIST(p, &sg, args->path); 103 104 #ifdef DEBUG 105 printf("Linux-emul(%ld): newstat(%s, *)\n", (long)p->p_pid, 106 args->path); 107 #endif 108 109 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE, 110 args->path, p); 111 error = namei(&nd); 112 if (error) 113 return (error); 114 115 error = vn_stat(nd.ni_vp, &buf, p); 116 vput(nd.ni_vp); 117 if (error) 118 return (error); 119 120 return (newstat_copyout(&buf, args->buf)); 121 } 122 123 /* 124 * Get file status; this version does not follow links. 125 */ 126 int 127 linux_newlstat(p, uap) 128 struct proc *p; 129 struct linux_newlstat_args *uap; 130 { 131 int error; 132 struct vnode *vp; 133 struct stat sb; 134 struct nameidata nd; 135 caddr_t sg; 136 137 sg = stackgap_init(); 138 CHECKALTEXIST(p, &sg, uap->path); 139 140 #ifdef DEBUG 141 printf("Linux-emul(%ld): newlstat(%s, *)\n", (long)p->p_pid, 142 uap->path); 143 #endif 144 145 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE, 146 uap->path, p); 147 error = namei(&nd); 148 if (error) 149 return (error); 150 151 vp = nd.ni_vp; 152 error = vn_stat(vp, &sb, p); 153 vput(vp); 154 if (error) 155 return (error); 156 157 return (newstat_copyout(&sb, uap->buf)); 158 } 159 160 int 161 linux_newfstat(struct proc *p, struct linux_newfstat_args *args) 162 { 163 struct filedesc *fdp; 164 struct file *fp; 165 struct stat buf; 166 int error; 167 168 fdp = p->p_fd; 169 170 #ifdef DEBUG 171 printf("Linux-emul(%ld): newfstat(%d, *)\n", (long)p->p_pid, args->fd); 172 #endif 173 174 if ((unsigned)args->fd >= fdp->fd_nfiles || 175 (fp = fdp->fd_ofiles[args->fd]) == NULL) 176 return (EBADF); 177 178 switch (fp->f_type) { 179 case DTYPE_FIFO: 180 case DTYPE_VNODE: 181 error = vn_stat((struct vnode *)fp->f_data, &buf, p); 182 break; 183 case DTYPE_SOCKET: 184 error = soo_stat((struct socket *)fp->f_data, &buf); 185 break; 186 case DTYPE_PIPE: 187 error = pipe_stat((struct pipe *)fp->f_data, &buf); 188 break; 189 default: 190 panic("LINUX newfstat"); 191 } 192 193 if (!error) 194 error = newstat_copyout(&buf, args->buf); 195 196 return (error); 197 } 198 199 struct linux_statfs_buf { 200 long ftype; 201 long fbsize; 202 long fblocks; 203 long fbfree; 204 long fbavail; 205 long ffiles; 206 long fffree; 207 linux_fsid_t ffsid; 208 long fnamelen; 209 long fspare[6]; 210 }; 211 212 int 213 linux_statfs(struct proc *p, struct linux_statfs_args *args) 214 { 215 struct mount *mp; 216 struct nameidata *ndp; 217 struct statfs *bsd_statfs; 218 struct nameidata nd; 219 struct linux_statfs_buf linux_statfs_buf; 220 int error; 221 caddr_t sg; 222 223 sg = stackgap_init(); 224 CHECKALTEXIST(p, &sg, args->path); 225 226 #ifdef DEBUG 227 printf("Linux-emul(%d): statfs(%s, *)\n", p->p_pid, args->path); 228 #endif 229 ndp = &nd; 230 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, curproc); 231 error = namei(ndp); 232 if (error) 233 return error; 234 mp = ndp->ni_vp->v_mount; 235 bsd_statfs = &mp->mnt_stat; 236 vrele(ndp->ni_vp); 237 error = VFS_STATFS(mp, bsd_statfs, p); 238 if (error) 239 return error; 240 bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 241 linux_statfs_buf.ftype = bsd_statfs->f_type; 242 linux_statfs_buf.fbsize = bsd_statfs->f_bsize; 243 linux_statfs_buf.fblocks = bsd_statfs->f_blocks; 244 linux_statfs_buf.fbfree = bsd_statfs->f_bfree; 245 linux_statfs_buf.fbavail = bsd_statfs->f_bavail; 246 linux_statfs_buf.fffree = bsd_statfs->f_ffree; 247 linux_statfs_buf.ffiles = bsd_statfs->f_files; 248 linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0]; 249 linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1]; 250 linux_statfs_buf.fnamelen = MAXNAMLEN; 251 return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf, 252 sizeof(struct linux_statfs_buf)); 253 } 254 255 int 256 linux_fstatfs(struct proc *p, struct linux_fstatfs_args *args) 257 { 258 struct file *fp; 259 struct mount *mp; 260 struct statfs *bsd_statfs; 261 struct linux_statfs_buf linux_statfs_buf; 262 int error; 263 264 #ifdef DEBUG 265 printf("Linux-emul(%d): fstatfs(%d, *)\n", p->p_pid, args->fd); 266 #endif 267 error = getvnode(p->p_fd, args->fd, &fp); 268 if (error) 269 return error; 270 mp = ((struct vnode *)fp->f_data)->v_mount; 271 bsd_statfs = &mp->mnt_stat; 272 error = VFS_STATFS(mp, bsd_statfs, p); 273 if (error) 274 return error; 275 bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 276 linux_statfs_buf.ftype = bsd_statfs->f_type; 277 linux_statfs_buf.fbsize = bsd_statfs->f_bsize; 278 linux_statfs_buf.fblocks = bsd_statfs->f_blocks; 279 linux_statfs_buf.fbfree = bsd_statfs->f_bfree; 280 linux_statfs_buf.fbavail = bsd_statfs->f_bavail; 281 linux_statfs_buf.fffree = bsd_statfs->f_ffree; 282 linux_statfs_buf.ffiles = bsd_statfs->f_files; 283 linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0]; 284 linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1]; 285 linux_statfs_buf.fnamelen = MAXNAMLEN; 286 return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf, 287 sizeof(struct linux_statfs_buf)); 288 } 289