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