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