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