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