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