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