2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 withough specific prior written permission
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.
28 * $FreeBSD: src/sys/compat/linux/linux_stats.c,v 1.22.2.3 2001/11/05 19:08:23 marcel Exp $
29 * $DragonFly: src/sys/emulation/linux/linux_stats.c,v 1.7 2003/08/15 06:32:51 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/dirent.h>
37 #include <sys/filedesc.h>
39 #include <sys/mount.h>
40 #include <sys/namei.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/vnode.h>
45 #include <sys/device.h>
46 #include <sys/file2.h>
48 #include <arch_linux/linux.h>
49 #include <arch_linux/linux_proto.h>
50 #include "linux_util.h"
53 newstat_copyout(struct stat *buf, void *ubuf)
55 struct l_newstat tbuf;
58 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
59 tbuf.st_ino = buf->st_ino;
60 tbuf.st_mode = buf->st_mode;
61 tbuf.st_nlink = buf->st_nlink;
62 tbuf.st_uid = buf->st_uid;
63 tbuf.st_gid = buf->st_gid;
64 tbuf.st_rdev = buf->st_rdev;
65 tbuf.st_size = buf->st_size;
66 tbuf.st_atime = buf->st_atime;
67 tbuf.st_mtime = buf->st_mtime;
68 tbuf.st_ctime = buf->st_ctime;
69 tbuf.st_blksize = buf->st_blksize;
70 tbuf.st_blocks = buf->st_blocks;
72 /* Lie about disk drives which are character devices
73 * in FreeBSD but block devices under Linux.
75 if (S_ISCHR(tbuf.st_mode) &&
76 (dev = udev2dev(buf->st_rdev, 0)) != NODEV) {
77 if (dev_dport(dev) != NULL && (dev_dflags(dev) & D_DISK)) {
78 tbuf.st_mode &= ~S_IFMT;
79 tbuf.st_mode |= S_IFBLK;
81 /* XXX this may not be quite right */
82 /* Map major number to 0 */
83 tbuf.st_dev = uminor(buf->st_dev) & 0xf;
84 tbuf.st_rdev = buf->st_rdev & 0xff;
88 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
92 linux_newstat(struct linux_newstat_args *args)
94 struct thread *td = curthread;
100 sg = stackgap_init();
101 CHECKALTEXIST(&sg, args->path);
105 printf(ARGS(newstat, "%s, *"), args->path);
108 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
113 NDFREE(&nd, NDF_ONLY_PNBUF);
115 error = vn_stat(nd.ni_vp, &buf, td);
120 return (newstat_copyout(&buf, args->buf));
124 linux_newlstat(struct linux_newlstat_args *args)
126 struct thread *td = curthread;
132 sg = stackgap_init();
133 CHECKALTEXIST(&sg, args->path);
136 if (ldebug(newlstat))
137 printf(ARGS(newlstat, "%s, *"), args->path);
140 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
145 NDFREE(&nd, NDF_ONLY_PNBUF);
147 error = vn_stat(nd.ni_vp, &sb, td);
152 return (newstat_copyout(&sb, args->buf));
156 linux_newfstat(struct linux_newfstat_args *args)
158 struct thread *td = curthread;
159 struct proc *p = td->td_proc;
160 struct filedesc *fdp;
168 if (ldebug(newfstat))
169 printf(ARGS(newfstat, "%d, *"), args->fd);
173 if ((unsigned)args->fd >= fdp->fd_nfiles ||
174 (fp = fdp->fd_ofiles[args->fd]) == NULL)
177 error = fo_stat(fp, &buf, td);
179 error = newstat_copyout(&buf, args->buf);
184 /* XXX - All fields of type l_int are defined as l_long on i386 */
198 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
199 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
200 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
201 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
202 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
203 #define LINUX_NCP_SUPER_MAGIC 0x564cL
204 #define LINUX_NFS_SUPER_MAGIC 0x6969L
205 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
206 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
207 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
210 bsd_to_linux_ftype(const char *fstypename)
213 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
214 {"ufs", LINUX_UFS_SUPER_MAGIC},
215 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
216 {"nfs", LINUX_NFS_SUPER_MAGIC},
217 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
218 {"procfs", LINUX_PROC_SUPER_MAGIC},
219 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
220 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
221 {"nwfs", LINUX_NCP_SUPER_MAGIC},
222 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
223 {"coda", LINUX_CODA_SUPER_MAGIC},
226 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
227 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
228 return (b2l_tbl[i].linux_type);
234 linux_statfs(struct linux_statfs_args *args)
236 struct thread *td = curthread;
238 struct nameidata *ndp;
239 struct statfs *bsd_statfs;
241 struct l_statfs linux_statfs;
245 sg = stackgap_init();
246 CHECKALTEXIST(&sg, args->path);
250 printf(ARGS(statfs, "%s, *"), args->path);
253 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, td);
257 NDFREE(ndp, NDF_ONLY_PNBUF);
258 mp = ndp->ni_vp->v_mount;
259 bsd_statfs = &mp->mnt_stat;
261 error = VFS_STATFS(mp, bsd_statfs, td);
264 bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
265 linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
266 linux_statfs.f_bsize = bsd_statfs->f_bsize;
267 linux_statfs.f_blocks = bsd_statfs->f_blocks;
268 linux_statfs.f_bfree = bsd_statfs->f_bfree;
269 linux_statfs.f_bavail = bsd_statfs->f_bavail;
270 linux_statfs.f_ffree = bsd_statfs->f_ffree;
271 linux_statfs.f_files = bsd_statfs->f_files;
272 linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
273 linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
274 linux_statfs.f_namelen = MAXNAMLEN;
275 return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
276 sizeof(linux_statfs));
280 linux_fstatfs(struct linux_fstatfs_args *args)
282 struct thread *td = curthread;
283 struct proc *p = td->td_proc;
286 struct statfs *bsd_statfs;
287 struct l_statfs linux_statfs;
294 printf(ARGS(fstatfs, "%d, *"), args->fd);
296 error = getvnode(p->p_fd, args->fd, &fp);
299 mp = ((struct vnode *)fp->f_data)->v_mount;
300 bsd_statfs = &mp->mnt_stat;
301 error = VFS_STATFS(mp, bsd_statfs, td);
304 bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
305 linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
306 linux_statfs.f_bsize = bsd_statfs->f_bsize;
307 linux_statfs.f_blocks = bsd_statfs->f_blocks;
308 linux_statfs.f_bfree = bsd_statfs->f_bfree;
309 linux_statfs.f_bavail = bsd_statfs->f_bavail;
310 linux_statfs.f_ffree = bsd_statfs->f_ffree;
311 linux_statfs.f_files = bsd_statfs->f_files;
312 linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
313 linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
314 linux_statfs.f_namelen = MAXNAMLEN;
315 return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
316 sizeof(linux_statfs));
328 linux_ustat(struct linux_ustat_args *args)
330 struct thread *td = curthread;
339 printf(ARGS(ustat, "%d, *"), args->dev);
343 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
344 * lu.f_tinode and lu.f_tfree are set from the device's super block.
346 bzero(&lu, sizeof(lu));
349 * XXX - Don't return an error if we can't find a vnode for the
350 * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
351 * dev_t. The dev_t that is used now may as well be a truncated
352 * dev_t returned from previous syscalls. Just return a bzeroed
353 * ustat in that case.
355 dev = makedev(args->dev >> 8, args->dev & 0xFF);
356 if (vfinddev(dev, VCHR, &vp)) {
357 if (vp->v_mount == NULL)
359 stat = &(vp->v_mount->mnt_stat);
360 error = VFS_STATFS(vp->v_mount, stat, td);
364 lu.f_tfree = stat->f_bfree;
365 lu.f_tinode = stat->f_ffree;
368 return (copyout(&lu, args->ubuf, sizeof(lu)));
371 #if defined(__i386__)
374 stat64_copyout(struct stat *buf, void *ubuf)
376 struct l_stat64 lbuf;
378 bzero(&lbuf, sizeof(lbuf));
379 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
380 lbuf.st_ino = buf->st_ino;
381 lbuf.st_mode = buf->st_mode;
382 lbuf.st_nlink = buf->st_nlink;
383 lbuf.st_uid = buf->st_uid;
384 lbuf.st_gid = buf->st_gid;
385 lbuf.st_rdev = buf->st_rdev;
386 lbuf.st_size = buf->st_size;
387 lbuf.st_atime = buf->st_atime;
388 lbuf.st_mtime = buf->st_mtime;
389 lbuf.st_ctime = buf->st_ctime;
390 lbuf.st_blksize = buf->st_blksize;
391 lbuf.st_blocks = buf->st_blocks;
394 * The __st_ino field makes all the difference. In the Linux kernel
395 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
396 * but without the assignment to __st_ino the runtime linker refuses
397 * to mmap(2) any shared libraries. I guess it's broken alright :-)
399 lbuf.__st_ino = buf->st_ino;
401 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
405 linux_stat64(struct linux_stat64_args *args)
407 struct thread *td = curthread;
413 sg = stackgap_init();
414 CHECKALTEXIST(&sg, args->filename);
418 printf(ARGS(stat64, "%s, *"), args->filename);
421 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
426 NDFREE(&nd, NDF_ONLY_PNBUF);
428 error = vn_stat(nd.ni_vp, &buf, td);
433 return (stat64_copyout(&buf, args->statbuf));
437 linux_lstat64(struct linux_lstat64_args *args)
439 struct thread *td = curthread;
445 sg = stackgap_init();
446 CHECKALTEXIST(&sg, args->filename);
450 printf(ARGS(lstat64, "%s, *"), args->filename);
453 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
458 NDFREE(&nd, NDF_ONLY_PNBUF);
460 error = vn_stat(nd.ni_vp, &sb, td);
465 return (stat64_copyout(&sb, args->statbuf));
469 linux_fstat64(struct linux_fstat64_args *args)
471 struct thread *td = curthread;
472 struct proc *p = td->td_proc;
473 struct filedesc *fdp;
482 printf(ARGS(fstat64, "%d, *"), args->fd);
486 if ((unsigned)args->fd >= fdp->fd_nfiles ||
487 (fp = fdp->fd_ofiles[args->fd]) == NULL)
490 error = fo_stat(fp, &buf, td);
492 error = stat64_copyout(&buf, args->statbuf);
497 #endif /* __i386__ */