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