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