xref: /freebsd/sys/compat/linux/linux_stats.c (revision 6949461a180c90c17d6f0adb3ff1ef76e3415bc3)
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_mac.h"
33 
34 #include <sys/param.h>
35 #include <sys/conf.h>
36 #include <sys/dirent.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/proc.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/stat.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 
48 #include "opt_compat.h"
49 
50 #if !COMPAT_LINUX32
51 #include <machine/../linux/linux.h>
52 #include <machine/../linux/linux_proto.h>
53 #else
54 #include <machine/../linux32/linux.h>
55 #include <machine/../linux32/linux32_proto.h>
56 #endif
57 
58 #include <compat/linux/linux_util.h>
59 
60 static int
61 newstat_copyout(struct stat *buf, void *ubuf)
62 {
63 	struct l_newstat tbuf;
64 	struct cdevsw *cdevsw;
65 	struct cdev *dev;
66 
67 	bzero(&tbuf, sizeof(tbuf));
68 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
69 	tbuf.st_ino = buf->st_ino;
70 	tbuf.st_mode = buf->st_mode;
71 	tbuf.st_nlink = buf->st_nlink;
72 	tbuf.st_uid = buf->st_uid;
73 	tbuf.st_gid = buf->st_gid;
74 	tbuf.st_rdev = buf->st_rdev;
75 	tbuf.st_size = buf->st_size;
76 	tbuf.st_atime = buf->st_atime;
77 	tbuf.st_mtime = buf->st_mtime;
78 	tbuf.st_ctime = buf->st_ctime;
79 	tbuf.st_blksize = buf->st_blksize;
80 	tbuf.st_blocks = buf->st_blocks;
81 
82 	/* Lie about disk drives which are character devices
83 	 * in FreeBSD but block devices under Linux.
84 	 */
85 	if (S_ISCHR(tbuf.st_mode) &&
86 	    (dev = findcdev(buf->st_rdev)) != NULL) {
87 		cdevsw = dev_refthread(dev);
88 		if (cdevsw != NULL) {
89 			if (cdevsw->d_flags & D_DISK) {
90 				tbuf.st_mode &= ~S_IFMT;
91 				tbuf.st_mode |= S_IFBLK;
92 
93 				/* XXX this may not be quite right */
94 				/* Map major number to 0 */
95 				tbuf.st_dev = uminor(buf->st_dev) & 0xf;
96 				tbuf.st_rdev = buf->st_rdev & 0xff;
97 			}
98 			dev_relthread(dev);
99 		}
100 	}
101 
102 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
103 }
104 
105 int
106 linux_newstat(struct thread *td, struct linux_newstat_args *args)
107 {
108 	struct stat buf;
109 	struct nameidata nd;
110 	char *path;
111 	int error;
112 
113 	LCONVPATHEXIST(td, args->path, &path);
114 
115 #ifdef DEBUG
116 	if (ldebug(newstat))
117 		printf(ARGS(newstat, "%s, *"), path);
118 #endif
119 
120 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path, td);
121 	error = namei(&nd);
122 	LFREEPATH(path);
123 	if (error)
124 		return (error);
125 	NDFREE(&nd, NDF_ONLY_PNBUF);
126 
127 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
128 	vput(nd.ni_vp);
129 	if (error)
130 		return (error);
131 
132 	return (newstat_copyout(&buf, args->buf));
133 }
134 
135 int
136 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
137 {
138 	int error;
139 	struct stat sb;
140 	struct nameidata nd;
141 	char *path;
142 
143 	LCONVPATHEXIST(td, args->path, &path);
144 
145 #ifdef DEBUG
146 	if (ldebug(newlstat))
147 		printf(ARGS(newlstat, "%s, *"), path);
148 #endif
149 
150 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path,
151 	    td);
152 	error = namei(&nd);
153 	LFREEPATH(path);
154 	if (error)
155 		return (error);
156 	NDFREE(&nd, NDF_ONLY_PNBUF);
157 
158 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
159 	vput(nd.ni_vp);
160 	if (error)
161 		return (error);
162 
163 	return (newstat_copyout(&sb, args->buf));
164 }
165 
166 int
167 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
168 {
169 	struct file *fp;
170 	struct stat buf;
171 	int error;
172 
173 #ifdef DEBUG
174 	if (ldebug(newfstat))
175 		printf(ARGS(newfstat, "%d, *"), args->fd);
176 #endif
177 
178 	if ((error = fget(td, args->fd, &fp)) != 0)
179 		return (error);
180 
181 	error = fo_stat(fp, &buf, td->td_ucred, td);
182 	fdrop(fp, td);
183 	if (!error)
184 		error = newstat_copyout(&buf, args->buf);
185 
186 	return (error);
187 }
188 
189 /* XXX - All fields of type l_int are defined as l_long on i386 */
190 struct l_statfs {
191 	l_int		f_type;
192 	l_int		f_bsize;
193 	l_int		f_blocks;
194 	l_int		f_bfree;
195 	l_int		f_bavail;
196 	l_int		f_files;
197 	l_int		f_ffree;
198 	l_fsid_t	f_fsid;
199 	l_int		f_namelen;
200 	l_int		f_spare[6];
201 };
202 
203 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
204 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
205 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
206 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
207 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
208 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
209 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
210 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
211 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
212 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
213 
214 static long
215 bsd_to_linux_ftype(const char *fstypename)
216 {
217 	int i;
218 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
219 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
220 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
221 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
222 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
223 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
224 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
225 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
226 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
227 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
228 		{"coda",    LINUX_CODA_SUPER_MAGIC},
229 		{NULL,      0L}};
230 
231 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
232 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
233 			return (b2l_tbl[i].linux_type);
234 
235 	return (0L);
236 }
237 
238 int
239 linux_statfs(struct thread *td, struct linux_statfs_args *args)
240 {
241 	struct mount *mp;
242 	struct nameidata *ndp;
243 	struct statfs *bsd_statfs;
244 	struct nameidata nd;
245 	struct l_statfs linux_statfs;
246 	char *path;
247 	int error;
248 
249 	LCONVPATHEXIST(td, args->path, &path);
250 
251 #ifdef DEBUG
252 	if (ldebug(statfs))
253 		printf(ARGS(statfs, "%s, *"), path);
254 #endif
255 	ndp = &nd;
256 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
257 	error = namei(ndp);
258 	LFREEPATH(path);
259 	if (error)
260 		return error;
261 	NDFREE(ndp, NDF_ONLY_PNBUF);
262 	mp = ndp->ni_vp->v_mount;
263 	bsd_statfs = &mp->mnt_stat;
264 	vrele(ndp->ni_vp);
265 #ifdef MAC
266 	error = mac_check_mount_stat(td->td_ucred, mp);
267 	if (error)
268 		return (error);
269 #endif
270 	error = VFS_STATFS(mp, bsd_statfs, td);
271 	if (error)
272 		return error;
273 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
274 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
275 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
276 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
277 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
278 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
279 	linux_statfs.f_ffree = bsd_statfs->f_ffree;
280 	linux_statfs.f_files = bsd_statfs->f_files;
281 	if (suser(td)) {
282 		linux_statfs.f_fsid.val[0] = 0;
283 		linux_statfs.f_fsid.val[1] = 0;
284 	} else {
285 		linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
286 		linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
287 	}
288 	linux_statfs.f_namelen = MAXNAMLEN;
289 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
290 }
291 
292 int
293 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
294 {
295 	struct file *fp;
296 	struct mount *mp;
297 	struct statfs *bsd_statfs;
298 	struct l_statfs linux_statfs;
299 	int error;
300 
301 #ifdef DEBUG
302 	if (ldebug(fstatfs))
303 		printf(ARGS(fstatfs, "%d, *"), args->fd);
304 #endif
305 	error = getvnode(td->td_proc->p_fd, args->fd, &fp);
306 	if (error)
307 		return error;
308 	mp = fp->f_vnode->v_mount;
309 #ifdef MAC
310 	error = mac_check_mount_stat(td->td_ucred, mp);
311 	if (error) {
312 		fdrop(fp, td);
313 		return (error);
314 	}
315 #endif
316 	bsd_statfs = &mp->mnt_stat;
317 	error = VFS_STATFS(mp, bsd_statfs, td);
318 	if (error) {
319 		fdrop(fp, td);
320 		return error;
321 	}
322 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
323 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
324 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
325 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
326 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
327 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
328 	linux_statfs.f_ffree = bsd_statfs->f_ffree;
329 	linux_statfs.f_files = bsd_statfs->f_files;
330 	if (suser(td)) {
331 		linux_statfs.f_fsid.val[0] = 0;
332 		linux_statfs.f_fsid.val[1] = 0;
333 	} else {
334 		linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
335 		linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
336 	}
337 	linux_statfs.f_namelen = MAXNAMLEN;
338 	error = copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
339 	fdrop(fp, td);
340 	return error;
341 }
342 
343 struct l_ustat
344 {
345 	l_daddr_t	f_tfree;
346 	l_ino_t		f_tinode;
347 	char		f_fname[6];
348 	char		f_fpack[6];
349 };
350 
351 int
352 linux_ustat(struct thread *td, struct linux_ustat_args *args)
353 {
354 	struct l_ustat lu;
355 	struct cdev *dev;
356 	struct vnode *vp;
357 	struct statfs *stat;
358 	int error;
359 
360 #ifdef DEBUG
361 	if (ldebug(ustat))
362 		printf(ARGS(ustat, "%d, *"), args->dev);
363 #endif
364 
365 	/*
366 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
367 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
368 	 */
369 	bzero(&lu, sizeof(lu));
370 
371 	/*
372 	 * XXX - Don't return an error if we can't find a vnode for the
373 	 * device. Our struct cdev *is 32-bits whereas Linux only has a 16-bits
374 	 * struct cdev *. The struct cdev *that is used now may as well be a truncated
375 	 * struct cdev *returned from previous syscalls. Just return a bzeroed
376 	 * ustat in that case.
377 	 *
378 	 * XXX: findcdev() SHALL not be used this way.  Somebody (TM) will
379 	 *	have to find a better way.  It may be that we should stick
380 	 *	a dev_t into struct mount, and walk the mountlist for a
381 	 *	perfect match and failing that try again looking for a
382 	 *	minor-truncated match.
383 	 */
384 	dev = findcdev(makedev(args->dev >> 8, args->dev & 0xFF));
385 	if (dev != NULL && vfinddev(dev, &vp)) {
386 		if (vp->v_mount == NULL)
387 			return (EINVAL);
388 #ifdef MAC
389 		error = mac_check_mount_stat(td->td_ucred, vp->v_mount);
390 		if (error)
391 			return (error);
392 #endif
393 		stat = &(vp->v_mount->mnt_stat);
394 		error = VFS_STATFS(vp->v_mount, stat, td);
395 		if (error)
396 			return (error);
397 
398 		lu.f_tfree = stat->f_bfree;
399 		lu.f_tinode = stat->f_ffree;
400 	}
401 
402 	return (copyout(&lu, args->ubuf, sizeof(lu)));
403 }
404 
405 #if defined(__i386__) || (defined(__amd64__) && COMPAT_LINUX32)
406 
407 static int
408 stat64_copyout(struct stat *buf, void *ubuf)
409 {
410 	struct l_stat64 lbuf;
411 	struct cdevsw *cdevsw;
412 	struct cdev *dev;
413 
414 	bzero(&lbuf, sizeof(lbuf));
415 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
416 	lbuf.st_ino = buf->st_ino;
417 	lbuf.st_mode = buf->st_mode;
418 	lbuf.st_nlink = buf->st_nlink;
419 	lbuf.st_uid = buf->st_uid;
420 	lbuf.st_gid = buf->st_gid;
421 	lbuf.st_rdev = buf->st_rdev;
422 	lbuf.st_size = buf->st_size;
423 	lbuf.st_atime = buf->st_atime;
424 	lbuf.st_mtime = buf->st_mtime;
425 	lbuf.st_ctime = buf->st_ctime;
426 	lbuf.st_blksize = buf->st_blksize;
427 	lbuf.st_blocks = buf->st_blocks;
428 
429 	/* Lie about disk drives which are character devices
430 	 * in FreeBSD but block devices under Linux.
431 	 */
432 	if (S_ISCHR(lbuf.st_mode) &&
433 	    (dev = findcdev(buf->st_rdev)) != NULL) {
434 		cdevsw = dev_refthread(dev);
435 		if (cdevsw != NULL) {
436 			if (cdevsw->d_flags & D_DISK) {
437 				lbuf.st_mode &= ~S_IFMT;
438 				lbuf.st_mode |= S_IFBLK;
439 
440 				/* XXX this may not be quite right */
441 				/* Map major number to 0 */
442 				lbuf.st_dev = uminor(buf->st_dev) & 0xf;
443 				lbuf.st_rdev = buf->st_rdev & 0xff;
444 			}
445 			dev_relthread(dev);
446 		}
447 	}
448 
449 	/*
450 	 * The __st_ino field makes all the difference. In the Linux kernel
451 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
452 	 * but without the assignment to __st_ino the runtime linker refuses
453 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
454 	 */
455 	lbuf.__st_ino = buf->st_ino;
456 
457 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
458 }
459 
460 int
461 linux_stat64(struct thread *td, struct linux_stat64_args *args)
462 {
463 	struct stat buf;
464 	struct nameidata nd;
465 	int error;
466 	char *filename;
467 
468 	LCONVPATHEXIST(td, args->filename, &filename);
469 
470 #ifdef DEBUG
471 	if (ldebug(stat64))
472 		printf(ARGS(stat64, "%s, *"), filename);
473 #endif
474 
475 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
476 	    td);
477 	error = namei(&nd);
478 	LFREEPATH(filename);
479 	if (error)
480 		return (error);
481 	NDFREE(&nd, NDF_ONLY_PNBUF);
482 
483 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
484 	vput(nd.ni_vp);
485 	if (error)
486 		return (error);
487 
488 	return (stat64_copyout(&buf, args->statbuf));
489 }
490 
491 int
492 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
493 {
494 	int error;
495 	struct stat sb;
496 	struct nameidata nd;
497 	char *filename;
498 
499 	LCONVPATHEXIST(td, args->filename, &filename);
500 
501 #ifdef DEBUG
502 	if (ldebug(lstat64))
503 		printf(ARGS(lstat64, "%s, *"), args->filename);
504 #endif
505 
506 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
507 	    td);
508 	error = namei(&nd);
509 	LFREEPATH(filename);
510 	if (error)
511 		return (error);
512 	NDFREE(&nd, NDF_ONLY_PNBUF);
513 
514 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
515 	vput(nd.ni_vp);
516 	if (error)
517 		return (error);
518 
519 	return (stat64_copyout(&sb, args->statbuf));
520 }
521 
522 int
523 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
524 {
525 	struct filedesc *fdp;
526 	struct file *fp;
527 	struct stat buf;
528 	int error;
529 
530 #ifdef DEBUG
531 	if (ldebug(fstat64))
532 		printf(ARGS(fstat64, "%d, *"), args->fd);
533 #endif
534 
535 	fdp = td->td_proc->p_fd;
536 	if ((unsigned)args->fd >= fdp->fd_nfiles ||
537 	    (fp = fdp->fd_ofiles[args->fd]) == NULL)
538 		return (EBADF);
539 
540 	error = fo_stat(fp, &buf, td->td_ucred, td);
541 	if (!error)
542 		error = stat64_copyout(&buf, args->statbuf);
543 
544 	return (error);
545 }
546 
547 #endif /* __i386__ || __amd64__ */
548