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