xref: /freebsd/sys/compat/linux/linux_stats.c (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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 /*
66  * XXX: This was removed from newstat_copyout(), and almost identical
67  * XXX: code was in stat64_copyout().  findcdev() needs to be replaced
68  * XXX: with something that does lookup and locking properly.
69  * XXX: When somebody fixes this: please try to avoid duplicating it.
70  */
71 #if 0
72 static void
73 disk_foo(struct somestat *tbuf)
74 {
75 	struct cdevsw *cdevsw;
76 	struct cdev *dev;
77 
78 	/* Lie about disk drives which are character devices
79 	 * in FreeBSD but block devices under Linux.
80 	 */
81 	if (S_ISCHR(tbuf.st_mode) &&
82 	    (dev = findcdev(buf->st_rdev)) != NULL) {
83 		cdevsw = dev_refthread(dev);
84 		if (cdevsw != NULL) {
85 			if (cdevsw->d_flags & D_DISK) {
86 				tbuf.st_mode &= ~S_IFMT;
87 				tbuf.st_mode |= S_IFBLK;
88 
89 				/* XXX this may not be quite right */
90 				/* Map major number to 0 */
91 				tbuf.st_dev = uminor(buf->st_dev) & 0xf;
92 				tbuf.st_rdev = buf->st_rdev & 0xff;
93 			}
94 			dev_relthread(dev);
95 		}
96 	}
97 
98 }
99 #endif
100 
101 static void
102 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
103 {
104 	struct file *fp;
105 	int major, minor;
106 
107 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
108 	    fget(td, fd, &fp) != 0)
109 		return;
110 	if (fp->f_vnode != NULL &&
111 	    fp->f_vnode->v_un.vu_cdev != NULL &&
112 	    linux_driver_get_major_minor(fp->f_vnode->v_un.vu_cdev->si_name,
113 					 &major, &minor) == 0) {
114 		buf->st_rdev = (major << 8 | minor);
115 	} else if (fp->f_type == DTYPE_PTS) {
116 		struct tty *tp = fp->f_data;
117 
118 		/* Convert the numbers for the slave device. */
119 		if (linux_driver_get_major_minor(tp->t_dev->si_name,
120 					 &major, &minor) == 0) {
121 			buf->st_rdev = (major << 8 | minor);
122 		}
123 	}
124 	fdrop(fp, td);
125 }
126 
127 static void
128 translate_path_major_minor_at(struct thread *td, char *path,
129     struct stat *buf, int dfd)
130 {
131 	struct proc *p = td->td_proc;
132 	struct filedesc *fdp = p->p_fd;
133 	int fd;
134 	int temp;
135 
136 	if (!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode))
137 		return;
138 	temp = td->td_retval[0];
139 	if (kern_openat(td, dfd, path, UIO_SYSSPACE, O_RDONLY, 0) != 0)
140 		return;
141 	fd = td->td_retval[0];
142 	td->td_retval[0] = temp;
143 	translate_fd_major_minor(td, fd, buf);
144 	fdclose(fdp, fdp->fd_ofiles[fd], fd, td);
145 }
146 
147 static inline void
148 translate_path_major_minor(struct thread *td, char *path, struct stat *buf)
149 {
150 	translate_path_major_minor_at(td, path, buf, AT_FDCWD);
151 }
152 
153 static int
154 newstat_copyout(struct stat *buf, void *ubuf)
155 {
156 	struct l_newstat tbuf;
157 
158 	bzero(&tbuf, sizeof(tbuf));
159 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
160 	tbuf.st_ino = buf->st_ino;
161 	tbuf.st_mode = buf->st_mode;
162 	tbuf.st_nlink = buf->st_nlink;
163 	tbuf.st_uid = buf->st_uid;
164 	tbuf.st_gid = buf->st_gid;
165 	tbuf.st_rdev = buf->st_rdev;
166 	tbuf.st_size = buf->st_size;
167 	tbuf.st_atime = buf->st_atime;
168 	tbuf.st_mtime = buf->st_mtime;
169 	tbuf.st_ctime = buf->st_ctime;
170 	tbuf.st_blksize = buf->st_blksize;
171 	tbuf.st_blocks = buf->st_blocks;
172 
173 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
174 }
175 
176 int
177 linux_newstat(struct thread *td, struct linux_newstat_args *args)
178 {
179 	struct stat buf;
180 	char *path;
181 	int error;
182 
183 	LCONVPATHEXIST(td, args->path, &path);
184 
185 #ifdef DEBUG
186 	if (ldebug(newstat))
187 		printf(ARGS(newstat, "%s, *"), path);
188 #endif
189 
190 	error = kern_stat(td, path, UIO_SYSSPACE, &buf);
191 	if (!error)
192 		translate_path_major_minor(td, path, &buf);
193 	LFREEPATH(path);
194 	if (error)
195 		return (error);
196 	return (newstat_copyout(&buf, args->buf));
197 }
198 
199 int
200 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
201 {
202 	struct stat sb;
203 	char *path;
204 	int error;
205 
206 	LCONVPATHEXIST(td, args->path, &path);
207 
208 #ifdef DEBUG
209 	if (ldebug(newlstat))
210 		printf(ARGS(newlstat, "%s, *"), path);
211 #endif
212 
213 	error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
214 	if (!error)
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 	char *path;
274 	int error;
275 
276 	LCONVPATHEXIST(td, args->path, &path);
277 
278 #ifdef DEBUG
279 	if (ldebug(stat))
280 		printf(ARGS(stat, "%s, *"), path);
281 #endif
282 	error = kern_stat(td, path, UIO_SYSSPACE, &buf);
283 	if (error) {
284 		LFREEPATH(path);
285 		return (error);
286 	}
287 	translate_path_major_minor(td, path, &buf);
288 	LFREEPATH(path);
289 	return(stat_copyout(&buf, args->up));
290 }
291 
292 int
293 linux_lstat(struct thread *td, struct linux_lstat_args *args)
294 {
295 	struct stat buf;
296 	char *path;
297 	int error;
298 
299 	LCONVPATHEXIST(td, args->path, &path);
300 
301 #ifdef DEBUG
302 	if (ldebug(lstat))
303 		printf(ARGS(lstat, "%s, *"), path);
304 #endif
305 	error = kern_lstat(td, path, UIO_SYSSPACE, &buf);
306 	if (error) {
307 		LFREEPATH(path);
308 		return (error);
309 	}
310 	translate_path_major_minor(td, path, &buf);
311 	LFREEPATH(path);
312 	return(stat_copyout(&buf, args->up));
313 }
314 
315 /* XXX - All fields of type l_int are defined as l_long on i386 */
316 struct l_statfs {
317 	l_int		f_type;
318 	l_int		f_bsize;
319 	l_int		f_blocks;
320 	l_int		f_bfree;
321 	l_int		f_bavail;
322 	l_int		f_files;
323 	l_int		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 	if (args->bufsize != sizeof(struct l_statfs64))
429 		return EINVAL;
430 
431 	LCONVPATHEXIST(td, args->path, &path);
432 
433 #ifdef DEBUG
434 	if (ldebug(statfs64))
435 		printf(ARGS(statfs64, "%s, *"), path);
436 #endif
437 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
438 	LFREEPATH(path);
439 	if (error)
440 		return (error);
441 	bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
442 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
443 }
444 
445 int
446 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
447 {
448 	struct l_statfs linux_statfs;
449 	struct statfs bsd_statfs;
450 	int error;
451 
452 #ifdef DEBUG
453 	if (ldebug(fstatfs))
454 		printf(ARGS(fstatfs, "%d, *"), args->fd);
455 #endif
456 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
457 	if (error)
458 		return error;
459 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
460 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
461 }
462 
463 struct l_ustat
464 {
465 	l_daddr_t	f_tfree;
466 	l_ino_t		f_tinode;
467 	char		f_fname[6];
468 	char		f_fpack[6];
469 };
470 
471 int
472 linux_ustat(struct thread *td, struct linux_ustat_args *args)
473 {
474 #ifdef DEBUG
475 	if (ldebug(ustat))
476 		printf(ARGS(ustat, "%d, *"), args->dev);
477 #endif
478 
479 	return (EOPNOTSUPP);
480 }
481 
482 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
483 
484 static int
485 stat64_copyout(struct stat *buf, void *ubuf)
486 {
487 	struct l_stat64 lbuf;
488 
489 	bzero(&lbuf, sizeof(lbuf));
490 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
491 	lbuf.st_ino = buf->st_ino;
492 	lbuf.st_mode = buf->st_mode;
493 	lbuf.st_nlink = buf->st_nlink;
494 	lbuf.st_uid = buf->st_uid;
495 	lbuf.st_gid = buf->st_gid;
496 	lbuf.st_rdev = buf->st_rdev;
497 	lbuf.st_size = buf->st_size;
498 	lbuf.st_atime = buf->st_atime;
499 	lbuf.st_mtime = buf->st_mtime;
500 	lbuf.st_ctime = buf->st_ctime;
501 	lbuf.st_blksize = buf->st_blksize;
502 	lbuf.st_blocks = buf->st_blocks;
503 
504 	/*
505 	 * The __st_ino field makes all the difference. In the Linux kernel
506 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
507 	 * but without the assignment to __st_ino the runtime linker refuses
508 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
509 	 */
510 	lbuf.__st_ino = buf->st_ino;
511 
512 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
513 }
514 
515 int
516 linux_stat64(struct thread *td, struct linux_stat64_args *args)
517 {
518 	struct stat buf;
519 	char *filename;
520 	int error;
521 
522 	LCONVPATHEXIST(td, args->filename, &filename);
523 
524 #ifdef DEBUG
525 	if (ldebug(stat64))
526 		printf(ARGS(stat64, "%s, *"), filename);
527 #endif
528 
529 	error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
530 	if (!error)
531 		translate_path_major_minor(td, filename, &buf);
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 int
581 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
582 {
583 	char *path;
584 	int error, dfd, flag;
585 	struct stat buf;
586 
587 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
588 		return (EINVAL);
589 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
590 	    AT_SYMLINK_NOFOLLOW : 0;
591 
592 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
593 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
594 
595 #ifdef DEBUG
596 	if (ldebug(fstatat64))
597 		printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
598 #endif
599 
600 	error = kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
601 	translate_path_major_minor_at(td, args->pathname, &buf, dfd);
602 	if (!error)
603 		error = stat64_copyout(&buf, args->statbuf);
604 	LFREEPATH(path);
605 
606 	return (error);
607 }
608 
609 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
610