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