xref: /freebsd/sys/compat/linux/linux_stats.c (revision b8aa2713423518bc3d708a08b52433096de96e9b)
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_compat.h"
33 #include "opt_mac.h"
34 
35 #include <sys/param.h>
36 #include <sys/dirent.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/proc.h>
40 #include <sys/jail.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/stat.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/systm.h>
47 #include <sys/vnode.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 
51 #ifdef COMPAT_LINUX32
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
54 #else
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
57 #endif
58 
59 #include <compat/linux/linux_util.h>
60 
61 #include <security/mac/mac_framework.h>
62 
63 /*
64  * XXX: This was removed from newstat_copyout(), and almost identical
65  * XXX: code was in stat64_copyout().  findcdev() needs to be replaced
66  * XXX: with something that does lookup and locking properly.
67  * XXX: When somebody fixes this: please try to avoid duplicating it.
68  */
69 #if 0
70 static void
71 disk_foo(struct somestat *tbuf)
72 {
73 	struct cdevsw *cdevsw;
74 	struct cdev *dev;
75 
76 	/* Lie about disk drives which are character devices
77 	 * in FreeBSD but block devices under Linux.
78 	 */
79 	if (S_ISCHR(tbuf.st_mode) &&
80 	    (dev = findcdev(buf->st_rdev)) != NULL) {
81 		cdevsw = dev_refthread(dev);
82 		if (cdevsw != NULL) {
83 			if (cdevsw->d_flags & D_DISK) {
84 				tbuf.st_mode &= ~S_IFMT;
85 				tbuf.st_mode |= S_IFBLK;
86 
87 				/* XXX this may not be quite right */
88 				/* Map major number to 0 */
89 				tbuf.st_dev = uminor(buf->st_dev) & 0xf;
90 				tbuf.st_rdev = buf->st_rdev & 0xff;
91 			}
92 			dev_relthread(dev);
93 		}
94 	}
95 
96 }
97 #endif
98 
99 static void
100 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
101 {
102 	struct file *fp;
103 	int error;
104 	int major, minor;
105 
106 	if ((error = fget(td, fd, &fp)) != 0)
107 		return;
108 	if (fp->f_vnode) {
109 		if (fp->f_vnode->v_type == VCHR
110 		    || fp->f_vnode->v_type == VBLK) {
111 			if (fp->f_vnode->v_un.vu_cdev) {
112 				if (linux_driver_get_major_minor(
113 				    fp->f_vnode->v_un.vu_cdev->si_name,
114 				    &major, &minor) == 0) {
115 					buf->st_rdev = (major << 8 | minor);
116 				}
117 			}
118 		}
119 	}
120 	fdrop(fp, td);
121 }
122 
123 static void
124 translate_path_major_minor(struct thread *td, char *path, struct stat *buf)
125 {
126 	struct proc *p = td->td_proc;
127 	struct filedesc *fdp = p->p_fd;
128 	struct file *fp;
129 	int fd;
130 	int temp;
131 
132 	temp = td->td_retval[0];
133 	if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0)
134 		return;
135 	fd = td->td_retval[0];
136 	td->td_retval[0] = temp;
137 	translate_fd_major_minor(td, fd, buf);
138 	FILEDESC_LOCK(fdp);
139 	fp = fdp->fd_ofiles[fd];
140 	FILEDESC_UNLOCK(fdp);
141 	fdclose(fdp, fdp->fd_ofiles[fd], fd, td);
142 }
143 
144 static int
145 newstat_copyout(struct stat *buf, void *ubuf)
146 {
147 	struct l_newstat tbuf;
148 
149 	bzero(&tbuf, sizeof(tbuf));
150 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
151 	tbuf.st_ino = buf->st_ino;
152 	tbuf.st_mode = buf->st_mode;
153 	tbuf.st_nlink = buf->st_nlink;
154 	tbuf.st_uid = buf->st_uid;
155 	tbuf.st_gid = buf->st_gid;
156 	tbuf.st_rdev = buf->st_rdev;
157 	tbuf.st_size = buf->st_size;
158 	tbuf.st_atime = buf->st_atime;
159 	tbuf.st_mtime = buf->st_mtime;
160 	tbuf.st_ctime = buf->st_ctime;
161 	tbuf.st_blksize = buf->st_blksize;
162 	tbuf.st_blocks = buf->st_blocks;
163 
164 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
165 }
166 
167 int
168 linux_newstat(struct thread *td, struct linux_newstat_args *args)
169 {
170 	struct stat buf;
171 	char *path;
172 	int error;
173 
174 	LCONVPATHEXIST(td, args->path, &path);
175 
176 #ifdef DEBUG
177 	if (ldebug(newstat))
178 		printf(ARGS(newstat, "%s, *"), path);
179 #endif
180 
181 	error = kern_stat(td, path, UIO_SYSSPACE, &buf);
182 	  if (!error && strlen(path) > strlen("/dev/pts/") &&
183 	      !strncmp(path, "/dev/pts/", strlen("/dev/pts/"))
184 	      && path[9] >= '0' && path[9] <= '9') {
185 		  /*
186 		   * Linux checks major and minors of the slave device to make
187 		   * sure it's a pty device, so let's make him believe it is.
188 		   */
189 		  buf.st_rdev = (136 << 8);
190 	  }
191 
192 	translate_path_major_minor(td, path, &buf);
193 
194 	LFREEPATH(path);
195 	if (error)
196 		return (error);
197 	return (newstat_copyout(&buf, args->buf));
198 }
199 
200 int
201 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
202 {
203 	struct stat sb;
204 	char *path;
205 	int error;
206 
207 	LCONVPATHEXIST(td, args->path, &path);
208 
209 #ifdef DEBUG
210 	if (ldebug(newlstat))
211 		printf(ARGS(newlstat, "%s, *"), path);
212 #endif
213 
214 	error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
215 	translate_path_major_minor(td, path, &sb);
216 	LFREEPATH(path);
217 	if (error)
218 		return (error);
219 	return (newstat_copyout(&sb, args->buf));
220 }
221 
222 int
223 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
224 {
225 	struct stat buf;
226 	int error;
227 
228 #ifdef DEBUG
229 	if (ldebug(newfstat))
230 		printf(ARGS(newfstat, "%d, *"), args->fd);
231 #endif
232 
233 	error = kern_fstat(td, args->fd, &buf);
234 	translate_fd_major_minor(td, args->fd, &buf);
235 	if (!error)
236 		error = newstat_copyout(&buf, args->buf);
237 
238 	return (error);
239 }
240 
241 static int
242 stat_copyout(struct stat *buf, void *ubuf)
243 {
244 	struct l_stat lbuf;
245 
246 	bzero(&lbuf, sizeof(lbuf));
247 	lbuf.st_dev = buf->st_dev;
248 	lbuf.st_ino = buf->st_ino;
249 	lbuf.st_mode = buf->st_mode;
250 	lbuf.st_nlink = buf->st_nlink;
251 	lbuf.st_uid = buf->st_uid;
252 	lbuf.st_gid = buf->st_gid;
253 	lbuf.st_rdev = buf->st_rdev;
254 	if (buf->st_size < (quad_t)1 << 32)
255 		lbuf.st_size = buf->st_size;
256 	else
257 		lbuf.st_size = -2;
258 	lbuf.st_atime = buf->st_atime;
259 	lbuf.st_mtime = buf->st_mtime;
260 	lbuf.st_ctime = buf->st_ctime;
261 	lbuf.st_blksize = buf->st_blksize;
262 	lbuf.st_blocks = buf->st_blocks;
263 	lbuf.st_flags = buf->st_flags;
264 	lbuf.st_gen = buf->st_gen;
265 
266 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
267 }
268 
269 int
270 linux_stat(struct thread *td, struct linux_stat_args *args)
271 {
272 	struct stat buf;
273 	int error;
274 #ifdef DEBUG
275 	if (ldebug(stat))
276 	printf(ARGS(stat, "%s, *"), args->path);
277 #endif
278 	error = kern_stat(td, args->path, UIO_SYSSPACE, &buf);
279 	if (error)
280 		return (error);
281 	translate_path_major_minor(td, args->path, &buf);
282 	return(stat_copyout(&buf, args->up));
283 }
284 
285 int
286 linux_lstat(struct thread *td, struct linux_lstat_args *args)
287 {
288 	struct stat buf;
289 	int error;
290 
291 #ifdef DEBUG
292 	if (ldebug(lstat))
293 	printf(ARGS(lstat, "%s, *"), args->path);
294 #endif
295 	error = kern_lstat(td, args->path, UIO_SYSSPACE, &buf);
296 	if (error)
297 		return (error);
298 	translate_path_major_minor(td, args->path, &buf);
299 	return(stat_copyout(&buf, args->up));
300 }
301 
302 /* XXX - All fields of type l_int are defined as l_long on i386 */
303 struct l_statfs {
304 	l_int		f_type;
305 	l_int		f_bsize;
306 	l_int		f_blocks;
307 	l_int		f_bfree;
308 	l_int		f_bavail;
309 	l_int		f_files;
310 	l_int		f_ffree;
311 	l_fsid_t	f_fsid;
312 	l_int		f_namelen;
313 	l_int		f_spare[6];
314 };
315 
316 struct l_statfs64 {
317 	l_int		f_type;
318 	l_int		f_bsize;
319 	uint64_t	f_blocks;
320 	uint64_t	f_bfree;
321 	uint64_t	f_bavail;
322 	uint64_t	f_files;
323 	uint64_t	f_ffree;
324 	l_fsid_t	f_fsid;
325 	l_int		f_namelen;
326 	l_int		f_spare[6];
327 };
328 
329 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
330 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
331 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
332 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
333 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
334 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
335 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
336 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
337 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
338 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
339 #define LINUX_DEVFS_SUPER_MAGIC	0x1373L
340 
341 static long
342 bsd_to_linux_ftype(const char *fstypename)
343 {
344 	int i;
345 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
346 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
347 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
348 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
349 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
350 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
351 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
352 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
353 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
354 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
355 		{"coda",    LINUX_CODA_SUPER_MAGIC},
356 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
357 		{NULL,      0L}};
358 
359 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
360 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
361 			return (b2l_tbl[i].linux_type);
362 
363 	return (0L);
364 }
365 
366 static void
367 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
368 {
369 
370 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
371 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
372 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
373 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
374 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
375 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
376 	linux_statfs->f_files = bsd_statfs->f_files;
377 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
378 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
379 	linux_statfs->f_namelen = MAXNAMLEN;
380 }
381 
382 int
383 linux_statfs(struct thread *td, struct linux_statfs_args *args)
384 {
385 	struct l_statfs linux_statfs;
386 	struct statfs bsd_statfs;
387 	char *path;
388 	int error;
389 
390 	LCONVPATHEXIST(td, args->path, &path);
391 
392 #ifdef DEBUG
393 	if (ldebug(statfs))
394 		printf(ARGS(statfs, "%s, *"), path);
395 #endif
396 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
397 	LFREEPATH(path);
398 	if (error)
399 		return (error);
400 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
401 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
402 }
403 
404 static void
405 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
406 {
407 
408 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
409 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
410 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
411 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
412 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
413 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
414 	linux_statfs->f_files = bsd_statfs->f_files;
415 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
416 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
417 	linux_statfs->f_namelen = MAXNAMLEN;
418 }
419 
420 int
421 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
422 {
423 	struct l_statfs64 linux_statfs;
424 	struct statfs bsd_statfs;
425 	char *path;
426 	int error;
427 
428 	LCONVPATHEXIST(td, args->path, &path);
429 
430 #ifdef DEBUG
431 	if (ldebug(statfs64))
432 		printf(ARGS(statfs64, "%s, *"), path);
433 #endif
434 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
435 	LFREEPATH(path);
436 	if (error)
437 		return (error);
438 	bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
439 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
440 }
441 
442 int
443 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
444 {
445 	struct l_statfs linux_statfs;
446 	struct statfs bsd_statfs;
447 	int error;
448 
449 #ifdef DEBUG
450 	if (ldebug(fstatfs))
451 		printf(ARGS(fstatfs, "%d, *"), args->fd);
452 #endif
453 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
454 	if (error)
455 		return error;
456 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
457 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
458 }
459 
460 struct l_ustat
461 {
462 	l_daddr_t	f_tfree;
463 	l_ino_t		f_tinode;
464 	char		f_fname[6];
465 	char		f_fpack[6];
466 };
467 
468 int
469 linux_ustat(struct thread *td, struct linux_ustat_args *args)
470 {
471 #ifdef DEBUG
472 	if (ldebug(ustat))
473 		printf(ARGS(ustat, "%d, *"), args->dev);
474 #endif
475 
476 	return (EOPNOTSUPP);
477 }
478 
479 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
480 
481 static int
482 stat64_copyout(struct stat *buf, void *ubuf)
483 {
484 	struct l_stat64 lbuf;
485 
486 	bzero(&lbuf, sizeof(lbuf));
487 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
488 	lbuf.st_ino = buf->st_ino;
489 	lbuf.st_mode = buf->st_mode;
490 	lbuf.st_nlink = buf->st_nlink;
491 	lbuf.st_uid = buf->st_uid;
492 	lbuf.st_gid = buf->st_gid;
493 	lbuf.st_rdev = buf->st_rdev;
494 	lbuf.st_size = buf->st_size;
495 	lbuf.st_atime = buf->st_atime;
496 	lbuf.st_mtime = buf->st_mtime;
497 	lbuf.st_ctime = buf->st_ctime;
498 	lbuf.st_blksize = buf->st_blksize;
499 	lbuf.st_blocks = buf->st_blocks;
500 
501 	/*
502 	 * The __st_ino field makes all the difference. In the Linux kernel
503 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
504 	 * but without the assignment to __st_ino the runtime linker refuses
505 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
506 	 */
507 	lbuf.__st_ino = buf->st_ino;
508 
509 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
510 }
511 
512 int
513 linux_stat64(struct thread *td, struct linux_stat64_args *args)
514 {
515 	struct stat buf;
516 	char *filename;
517 	int error;
518 
519 	LCONVPATHEXIST(td, args->filename, &filename);
520 
521 #ifdef DEBUG
522 	if (ldebug(stat64))
523 		printf(ARGS(stat64, "%s, *"), filename);
524 #endif
525 
526 	error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
527 	if (!error && strlen(filename) > strlen("/dev/pts/") &&
528 	    !strncmp(filename, "/dev/pts/", strlen("/dev/pts/"))
529 	    && filename[9] >= '0' && filename[9] <= '9') {
530 		/*
531 		 * Linux checks major and minors of the slave device to make
532 		 * sure it's a pty deivce, so let's make him believe it is.
533 		 */
534 		buf.st_rdev = (136 << 8);
535 	}
536 
537 	translate_path_major_minor(td, filename, &buf);
538 
539 	LFREEPATH(filename);
540 	if (error)
541 		return (error);
542 	return (stat64_copyout(&buf, args->statbuf));
543 }
544 
545 int
546 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
547 {
548 	struct stat sb;
549 	char *filename;
550 	int error;
551 
552 	LCONVPATHEXIST(td, args->filename, &filename);
553 
554 #ifdef DEBUG
555 	if (ldebug(lstat64))
556 		printf(ARGS(lstat64, "%s, *"), args->filename);
557 #endif
558 
559 	error = kern_lstat(td, filename, UIO_SYSSPACE, &sb);
560 	translate_path_major_minor(td, filename, &sb);
561 	LFREEPATH(filename);
562 	if (error)
563 		return (error);
564 	return (stat64_copyout(&sb, args->statbuf));
565 }
566 
567 int
568 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
569 {
570 	struct stat buf;
571 	int error;
572 
573 #ifdef DEBUG
574 	if (ldebug(fstat64))
575 		printf(ARGS(fstat64, "%d, *"), args->fd);
576 #endif
577 
578 	error = kern_fstat(td, args->fd, &buf);
579 	translate_fd_major_minor(td, args->fd, &buf);
580 	if (!error)
581 		error = stat64_copyout(&buf, args->statbuf);
582 
583 	return (error);
584 }
585 
586 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
587