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