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