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