xref: /freebsd/sys/compat/linux/linux_stats.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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 <i386/linux/linux.h>
44 #include <i386/linux/linux_proto.h>
45 #include <i386/linux/linux_util.h>
46 
47 #include <vm/vm_zone.h>
48 
49 struct linux_newstat {
50 	u_short	stat_dev;
51 	u_short	__pad1;
52 	u_long	stat_ino;
53 	u_short	stat_mode;
54 	u_short	stat_nlink;
55 	u_short	stat_uid;
56 	u_short	stat_gid;
57 	u_short	stat_rdev;
58 	u_short	__pad2;
59 	u_long	stat_size;
60 	u_long	stat_blksize;
61 	u_long	stat_blocks;
62 	u_long	stat_atime;
63 	u_long	__unused1;
64 	u_long	stat_mtime;
65 	u_long	__unused2;
66 	u_long	stat_ctime;
67 	u_long	__unused3;
68 	u_long	__unused4;
69 	u_long	__unused5;
70 };
71 
72 struct linux_ustat
73 {
74 	int	f_tfree;
75 	u_long	f_tinode;
76 	char	f_fname[6];
77 	char	f_fpack[6];
78 };
79 
80 static int
81 newstat_copyout(struct stat *buf, void *ubuf)
82 {
83 	struct linux_newstat tbuf;
84 
85 	tbuf.stat_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
86 	tbuf.stat_ino = buf->st_ino;
87 	tbuf.stat_mode = buf->st_mode;
88 	tbuf.stat_nlink = buf->st_nlink;
89 	tbuf.stat_uid = buf->st_uid;
90 	tbuf.stat_gid = buf->st_gid;
91 	tbuf.stat_rdev = buf->st_rdev;
92 	tbuf.stat_size = buf->st_size;
93 	tbuf.stat_atime = buf->st_atime;
94 	tbuf.stat_mtime = buf->st_mtime;
95 	tbuf.stat_ctime = buf->st_ctime;
96 	tbuf.stat_blksize = buf->st_blksize;
97 	tbuf.stat_blocks = buf->st_blocks;
98 
99 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
100 }
101 
102 int
103 linux_newstat(struct proc *p, struct linux_newstat_args *args)
104 {
105 	struct stat buf;
106 	struct nameidata nd;
107 	int error;
108 	caddr_t sg;
109 
110 	sg = stackgap_init();
111 	CHECKALTEXIST(p, &sg, args->path);
112 
113 #ifdef DEBUG
114 	printf("Linux-emul(%ld): newstat(%s, *)\n", (long)p->p_pid,
115 	       args->path);
116 #endif
117 
118 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
119 	       args->path, p);
120 	error = namei(&nd);
121 	if (error)
122 		return (error);
123 	NDFREE(&nd, NDF_ONLY_PNBUF);
124 
125 	error = vn_stat(nd.ni_vp, &buf, p);
126 	vput(nd.ni_vp);
127 	if (error)
128 		return (error);
129 
130 	return (newstat_copyout(&buf, args->buf));
131 }
132 
133 /*
134  * Get file status; this version does not follow links.
135  */
136 int
137 linux_newlstat(p, uap)
138 	struct proc *p;
139 	struct linux_newlstat_args *uap;
140 {
141 	int error;
142 	struct vnode *vp;
143 	struct stat sb;
144 	struct nameidata nd;
145 	caddr_t sg;
146 
147 	sg = stackgap_init();
148 	CHECKALTEXIST(p, &sg, uap->path);
149 
150 #ifdef DEBUG
151 	printf("Linux-emul(%ld): newlstat(%s, *)\n", (long)p->p_pid,
152 	       uap->path);
153 #endif
154 
155 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
156 	       uap->path, p);
157 	error = namei(&nd);
158 	if (error)
159 		return (error);
160 	NDFREE(&nd, NDF_ONLY_PNBUF);
161 
162 	vp = nd.ni_vp;
163 	error = vn_stat(vp, &sb, p);
164 	vput(vp);
165 	if (error)
166 		return (error);
167 
168 	return (newstat_copyout(&sb, uap->buf));
169 }
170 
171 int
172 linux_newfstat(struct proc *p, struct linux_newfstat_args *args)
173 {
174 	struct filedesc *fdp;
175 	struct file *fp;
176 	struct stat buf;
177 	int error;
178 
179 	fdp = p->p_fd;
180 
181 #ifdef DEBUG
182 	printf("Linux-emul(%ld): newfstat(%d, *)\n", (long)p->p_pid, args->fd);
183 #endif
184 
185 	if ((unsigned)args->fd >= fdp->fd_nfiles ||
186 	    (fp = fdp->fd_ofiles[args->fd]) == NULL)
187 		return (EBADF);
188 
189 	error = fo_stat(fp, &buf, p);
190 	if (!error)
191 		error = newstat_copyout(&buf, args->buf);
192 
193 	return (error);
194 }
195 
196 struct linux_statfs_buf {
197 	long ftype;
198 	long fbsize;
199 	long fblocks;
200 	long fbfree;
201 	long fbavail;
202 	long ffiles;
203 	long fffree;
204 	linux_fsid_t ffsid;
205 	long fnamelen;
206 	long fspare[6];
207 };
208 
209 int
210 linux_statfs(struct proc *p, struct linux_statfs_args *args)
211 {
212 	struct mount *mp;
213 	struct nameidata *ndp;
214 	struct statfs *bsd_statfs;
215 	struct nameidata nd;
216 	struct linux_statfs_buf linux_statfs_buf;
217 	int error;
218 	caddr_t sg;
219 
220 	sg = stackgap_init();
221 	CHECKALTEXIST(p, &sg, args->path);
222 
223 #ifdef DEBUG
224 	printf("Linux-emul(%d): statfs(%s, *)\n", p->p_pid, args->path);
225 #endif
226 	ndp = &nd;
227 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, curproc);
228 	error = namei(ndp);
229 	if (error)
230 		return error;
231 	NDFREE(ndp, NDF_ONLY_PNBUF);
232 	mp = ndp->ni_vp->v_mount;
233 	bsd_statfs = &mp->mnt_stat;
234 	vrele(ndp->ni_vp);
235 	error = VFS_STATFS(mp, bsd_statfs, p);
236 	if (error)
237 		return error;
238 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
239 	linux_statfs_buf.ftype = bsd_statfs->f_type;
240 	linux_statfs_buf.fbsize = bsd_statfs->f_bsize;
241 	linux_statfs_buf.fblocks = bsd_statfs->f_blocks;
242 	linux_statfs_buf.fbfree = bsd_statfs->f_bfree;
243 	linux_statfs_buf.fbavail = bsd_statfs->f_bavail;
244   	linux_statfs_buf.fffree = bsd_statfs->f_ffree;
245 	linux_statfs_buf.ffiles = bsd_statfs->f_files;
246 	linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0];
247 	linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1];
248 	linux_statfs_buf.fnamelen = MAXNAMLEN;
249 	return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf,
250 		       sizeof(struct linux_statfs_buf));
251 }
252 
253 int
254 linux_fstatfs(struct proc *p, struct linux_fstatfs_args *args)
255 {
256 	struct file *fp;
257 	struct mount *mp;
258 	struct statfs *bsd_statfs;
259 	struct linux_statfs_buf linux_statfs_buf;
260 	int error;
261 
262 #ifdef DEBUG
263 	printf("Linux-emul(%d): fstatfs(%d, *)\n", p->p_pid, args->fd);
264 #endif
265 	error = getvnode(p->p_fd, args->fd, &fp);
266 	if (error)
267 		return error;
268 	mp = ((struct vnode *)fp->f_data)->v_mount;
269 	bsd_statfs = &mp->mnt_stat;
270 	error = VFS_STATFS(mp, bsd_statfs, p);
271 	if (error)
272 		return error;
273 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
274 	linux_statfs_buf.ftype = bsd_statfs->f_type;
275 	linux_statfs_buf.fbsize = bsd_statfs->f_bsize;
276 	linux_statfs_buf.fblocks = bsd_statfs->f_blocks;
277 	linux_statfs_buf.fbfree = bsd_statfs->f_bfree;
278 	linux_statfs_buf.fbavail = bsd_statfs->f_bavail;
279   	linux_statfs_buf.fffree = bsd_statfs->f_ffree;
280 	linux_statfs_buf.ffiles = bsd_statfs->f_files;
281 	linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0];
282 	linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1];
283 	linux_statfs_buf.fnamelen = MAXNAMLEN;
284 	return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf,
285 		       sizeof(struct linux_statfs_buf));
286 }
287 
288 int
289 linux_ustat(p, uap)
290 	struct proc *p;
291 	struct linux_ustat_args *uap;
292 {
293 	struct linux_ustat lu;
294 	dev_t dev;
295 	struct vnode *vp;
296 	struct statfs *stat;
297 	int error;
298 
299 #ifdef DEBUG
300 	printf("Linux-emul(%ld): ustat(%d, *)\n", (long)p->p_pid, uap->dev);
301 #endif
302 
303 	/*
304 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
305 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
306 	 */
307 	bzero(&lu, sizeof(lu));
308 
309 	/*
310 	 * XXX - Don't return an error if we can't find a vnode for the
311 	 * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
312 	 * dev_t. The dev_t that is used now may as well be a truncated
313 	 * dev_t returned from previous syscalls. Just return a bzeroed
314 	 * ustat in that case.
315 	 */
316 	dev = makebdev(uap->dev >> 8, uap->dev & 0xFF);
317 	if (vfinddev(dev, VBLK, &vp)) {
318 		if (vp->v_mount == NULL)
319 			return (EINVAL);
320 		stat = &(vp->v_mount->mnt_stat);
321 		error = VFS_STATFS(vp->v_mount, stat, p);
322 		if (error)
323 			return (error);
324 
325 		lu.f_tfree = stat->f_bfree;
326 		lu.f_tinode = stat->f_ffree;
327 	}
328 
329 	return (copyout(&lu, uap->ubuf, sizeof(lu)));
330 }
331