xref: /freebsd/sys/compat/linux/linux_stats.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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 without 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 "opt_mac.h"
32 
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/dirent.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/proc.h>
39 #include <sys/mac.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/stat.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 
47 #include <machine/../linux/linux.h>
48 #include <machine/../linux/linux_proto.h>
49 
50 #include <compat/linux/linux_util.h>
51 
52 static int
53 newstat_copyout(struct stat *buf, void *ubuf)
54 {
55 	struct l_newstat tbuf;
56 	struct cdevsw *cdevsw;
57 	dev_t dev;
58 
59 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
60 	tbuf.st_ino = buf->st_ino;
61 	tbuf.st_mode = buf->st_mode;
62 	tbuf.st_nlink = buf->st_nlink;
63 	tbuf.st_uid = buf->st_uid;
64 	tbuf.st_gid = buf->st_gid;
65 	tbuf.st_rdev = buf->st_rdev;
66 	tbuf.st_size = buf->st_size;
67 	tbuf.st_atime = buf->st_atime;
68 	tbuf.st_mtime = buf->st_mtime;
69 	tbuf.st_ctime = buf->st_ctime;
70 	tbuf.st_blksize = buf->st_blksize;
71 	tbuf.st_blocks = buf->st_blocks;
72 
73 	/* Lie about disk drives which are character devices
74 	 * in FreeBSD but block devices under Linux.
75 	 */
76 	if (S_ISCHR(tbuf.st_mode) &&
77 	    (dev = udev2dev(buf->st_rdev, 0)) != NODEV) {
78 		cdevsw = devsw(dev);
79 		if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
80 			tbuf.st_mode &= ~S_IFMT;
81 			tbuf.st_mode |= S_IFBLK;
82 
83 			/* XXX this may not be quite right */
84 			/* Map major number to 0 */
85 			tbuf.st_dev = uminor(buf->st_dev) & 0xf;
86 			tbuf.st_rdev = buf->st_rdev & 0xff;
87 		}
88 	}
89 
90 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
91 }
92 
93 int
94 linux_newstat(struct thread *td, struct linux_newstat_args *args)
95 {
96 	struct stat buf;
97 	struct nameidata nd;
98 	char *path;
99 	int error;
100 
101 	LCONVPATHEXIST(td, args->path, &path);
102 
103 #ifdef DEBUG
104 	if (ldebug(newstat))
105 		printf(ARGS(newstat, "%s, *"), path);
106 #endif
107 
108 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path, td);
109 	error = namei(&nd);
110 	LFREEPATH(path);
111 	if (error)
112 		return (error);
113 	NDFREE(&nd, NDF_ONLY_PNBUF);
114 
115 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
116 	vput(nd.ni_vp);
117 	if (error)
118 		return (error);
119 
120 	return (newstat_copyout(&buf, args->buf));
121 }
122 
123 int
124 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
125 {
126 	int error;
127 	struct stat sb;
128 	struct nameidata nd;
129 	char *path;
130 
131 	LCONVPATHEXIST(td, args->path, &path);
132 
133 #ifdef DEBUG
134 	if (ldebug(newlstat))
135 		printf(ARGS(newlstat, "%s, *"), path);
136 #endif
137 
138 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path,
139 	    td);
140 	error = namei(&nd);
141 	LFREEPATH(path);
142 	if (error)
143 		return (error);
144 	NDFREE(&nd, NDF_ONLY_PNBUF);
145 
146 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
147 	vput(nd.ni_vp);
148 	if (error)
149 		return (error);
150 
151 	return (newstat_copyout(&sb, args->buf));
152 }
153 
154 int
155 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
156 {
157 	struct file *fp;
158 	struct stat buf;
159 	int error;
160 
161 #ifdef DEBUG
162 	if (ldebug(newfstat))
163 		printf(ARGS(newfstat, "%d, *"), args->fd);
164 #endif
165 
166 	if ((error = fget(td, args->fd, &fp)) != 0)
167 		return (error);
168 
169 	error = fo_stat(fp, &buf, td->td_ucred, td);
170 	fdrop(fp, td);
171 	if (!error)
172 		error = newstat_copyout(&buf, args->buf);
173 
174 	return (error);
175 }
176 
177 /* XXX - All fields of type l_int are defined as l_long on i386 */
178 struct l_statfs {
179 	l_int		f_type;
180 	l_int		f_bsize;
181 	l_int		f_blocks;
182 	l_int		f_bfree;
183 	l_int		f_bavail;
184 	l_int		f_files;
185 	l_int		f_ffree;
186 	l_fsid_t	f_fsid;
187 	l_int		f_namelen;
188 	l_int		f_spare[6];
189 };
190 
191 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
192 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
193 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
194 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
195 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
196 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
197 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
198 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
199 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
200 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
201 
202 static long
203 bsd_to_linux_ftype(const char *fstypename)
204 {
205 	int i;
206 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
207 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
208 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
209 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
210 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
211 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
212 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
213 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
214 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
215 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
216 		{"coda",    LINUX_CODA_SUPER_MAGIC},
217 		{NULL,      0L}};
218 
219 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
220 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
221 			return (b2l_tbl[i].linux_type);
222 
223 	return (0L);
224 }
225 
226 int
227 linux_statfs(struct thread *td, struct linux_statfs_args *args)
228 {
229 	struct mount *mp;
230 	struct nameidata *ndp;
231 	struct statfs *bsd_statfs;
232 	struct nameidata nd;
233 	struct l_statfs linux_statfs;
234 	char *path;
235 	int error;
236 
237 	LCONVPATHEXIST(td, args->path, &path);
238 
239 #ifdef DEBUG
240 	if (ldebug(statfs))
241 		printf(ARGS(statfs, "%s, *"), path);
242 #endif
243 	ndp = &nd;
244 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
245 	error = namei(ndp);
246 	LFREEPATH(path);
247 	if (error)
248 		return error;
249 	NDFREE(ndp, NDF_ONLY_PNBUF);
250 	mp = ndp->ni_vp->v_mount;
251 	bsd_statfs = &mp->mnt_stat;
252 	vrele(ndp->ni_vp);
253 #ifdef MAC
254 	error = mac_check_mount_stat(td->td_ucred, mp);
255 	if (error)
256 		return (error);
257 #endif
258 	error = VFS_STATFS(mp, bsd_statfs, td);
259 	if (error)
260 		return error;
261 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
262 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
263 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
264 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
265 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
266 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
267 	linux_statfs.f_ffree = bsd_statfs->f_ffree;
268 	linux_statfs.f_files = bsd_statfs->f_files;
269 	linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
270 	linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
271 	linux_statfs.f_namelen = MAXNAMLEN;
272 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
273 }
274 
275 int
276 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
277 {
278 	struct file *fp;
279 	struct mount *mp;
280 	struct statfs *bsd_statfs;
281 	struct l_statfs linux_statfs;
282 	int error;
283 
284 #ifdef DEBUG
285 	if (ldebug(fstatfs))
286 		printf(ARGS(fstatfs, "%d, *"), args->fd);
287 #endif
288 	error = getvnode(td->td_proc->p_fd, args->fd, &fp);
289 	if (error)
290 		return error;
291 	mp = ((struct vnode *)fp->f_data)->v_mount;
292 #ifdef MAC
293 	error = mac_check_mount_stat(td->td_ucred, mp);
294 	if (error) {
295 		fdrop(fp, td);
296 		return (error);
297 	}
298 #endif
299 	bsd_statfs = &mp->mnt_stat;
300 	error = VFS_STATFS(mp, bsd_statfs, td);
301 	if (error) {
302 		fdrop(fp, td);
303 		return error;
304 	}
305 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
306 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
307 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
308 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
309 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
310 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
311 	linux_statfs.f_ffree = bsd_statfs->f_ffree;
312 	linux_statfs.f_files = bsd_statfs->f_files;
313 	linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
314 	linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
315 	linux_statfs.f_namelen = MAXNAMLEN;
316 	error = copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
317 	fdrop(fp, td);
318 	return error;
319 }
320 
321 struct l_ustat
322 {
323 	l_daddr_t	f_tfree;
324 	l_ino_t		f_tinode;
325 	char		f_fname[6];
326 	char		f_fpack[6];
327 };
328 
329 int
330 linux_ustat(struct thread *td, struct linux_ustat_args *args)
331 {
332 	struct l_ustat lu;
333 	dev_t dev;
334 	struct vnode *vp;
335 	struct statfs *stat;
336 	int error;
337 
338 #ifdef DEBUG
339 	if (ldebug(ustat))
340 		printf(ARGS(ustat, "%d, *"), args->dev);
341 #endif
342 
343 	/*
344 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
345 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
346 	 */
347 	bzero(&lu, sizeof(lu));
348 
349 	/*
350 	 * XXX - Don't return an error if we can't find a vnode for the
351 	 * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
352 	 * dev_t. The dev_t that is used now may as well be a truncated
353 	 * dev_t returned from previous syscalls. Just return a bzeroed
354 	 * ustat in that case.
355 	 */
356 	dev = makedev(args->dev >> 8, args->dev & 0xFF);
357 	if (vfinddev(dev, VCHR, &vp)) {
358 		if (vp->v_mount == NULL)
359 			return (EINVAL);
360 #ifdef MAC
361 		error = mac_check_mount_stat(td->td_ucred, vp->v_mount);
362 		if (error)
363 			return (error);
364 #endif
365 		stat = &(vp->v_mount->mnt_stat);
366 		error = VFS_STATFS(vp->v_mount, stat, td);
367 		if (error)
368 			return (error);
369 
370 		lu.f_tfree = stat->f_bfree;
371 		lu.f_tinode = stat->f_ffree;
372 	}
373 
374 	return (copyout(&lu, args->ubuf, sizeof(lu)));
375 }
376 
377 #if defined(__i386__)
378 
379 static int
380 stat64_copyout(struct stat *buf, void *ubuf)
381 {
382 	struct l_stat64 lbuf;
383 
384 	bzero(&lbuf, sizeof(lbuf));
385 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
386 	lbuf.st_ino = buf->st_ino;
387 	lbuf.st_mode = buf->st_mode;
388 	lbuf.st_nlink = buf->st_nlink;
389 	lbuf.st_uid = buf->st_uid;
390 	lbuf.st_gid = buf->st_gid;
391 	lbuf.st_rdev = buf->st_rdev;
392 	lbuf.st_size = buf->st_size;
393 	lbuf.st_atime = buf->st_atime;
394 	lbuf.st_mtime = buf->st_mtime;
395 	lbuf.st_ctime = buf->st_ctime;
396 	lbuf.st_blksize = buf->st_blksize;
397 	lbuf.st_blocks = buf->st_blocks;
398 
399 	/*
400 	 * The __st_ino field makes all the difference. In the Linux kernel
401 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
402 	 * but without the assignment to __st_ino the runtime linker refuses
403 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
404 	 */
405 	lbuf.__st_ino = buf->st_ino;
406 
407 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
408 }
409 
410 int
411 linux_stat64(struct thread *td, struct linux_stat64_args *args)
412 {
413 	struct stat buf;
414 	struct nameidata nd;
415 	int error;
416 	char *filename;
417 
418 	LCONVPATHEXIST(td, args->filename, &filename);
419 
420 #ifdef DEBUG
421 	if (ldebug(stat64))
422 		printf(ARGS(stat64, "%s, *"), filename);
423 #endif
424 
425 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
426 	    td);
427 	error = namei(&nd);
428 	LFREEPATH(filename);
429 	if (error)
430 		return (error);
431 	NDFREE(&nd, NDF_ONLY_PNBUF);
432 
433 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
434 	vput(nd.ni_vp);
435 	if (error)
436 		return (error);
437 
438 	return (stat64_copyout(&buf, args->statbuf));
439 }
440 
441 int
442 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
443 {
444 	int error;
445 	struct stat sb;
446 	struct nameidata nd;
447 	char *filename;
448 
449 	LCONVPATHEXIST(td, args->filename, &filename);
450 
451 #ifdef DEBUG
452 	if (ldebug(lstat64))
453 		printf(ARGS(lstat64, "%s, *"), args->filename);
454 #endif
455 
456 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
457 	    td);
458 	error = namei(&nd);
459 	LFREEPATH(filename);
460 	if (error)
461 		return (error);
462 	NDFREE(&nd, NDF_ONLY_PNBUF);
463 
464 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
465 	vput(nd.ni_vp);
466 	if (error)
467 		return (error);
468 
469 	return (stat64_copyout(&sb, args->statbuf));
470 }
471 
472 int
473 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
474 {
475 	struct filedesc *fdp;
476 	struct file *fp;
477 	struct stat buf;
478 	int error;
479 
480 #ifdef DEBUG
481 	if (ldebug(fstat64))
482 		printf(ARGS(fstat64, "%d, *"), args->fd);
483 #endif
484 
485 	fdp = td->td_proc->p_fd;
486 	if ((unsigned)args->fd >= fdp->fd_nfiles ||
487 	    (fp = fdp->fd_ofiles[args->fd]) == NULL)
488 		return (EBADF);
489 
490 	error = fo_stat(fp, &buf, td->td_ucred, td);
491 	if (!error)
492 		error = stat64_copyout(&buf, args->statbuf);
493 
494 	return (error);
495 }
496 
497 #endif /* __i386__ */
498