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