0d6cf9efa4d0ed1764dcc14d132c4f0930ca721d
[dragonfly.git] / sys / emulation / linux / linux_stats.c
blob0d6cf9efa4d0ed1764dcc14d132c4f0930ca721d
1 /*-
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * All rights reserved.
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 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.27 2008/09/28 05:08:16 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/dirent.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/proc.h>
39 #include <sys/mount.h>
40 #include <sys/nlookup.h>
41 #include <sys/stat.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/unistd.h>
45 #include <sys/vnode.h>
46 #include <sys/device.h>
47 #include <sys/kern_syscall.h>
49 #include <sys/file2.h>
50 #include <sys/mplock2.h>
52 #include <arch_linux/linux.h>
53 #include <arch_linux/linux_proto.h>
54 #include "linux_util.h"
56 static int
57 newstat_copyout(struct stat *buf, void *ubuf)
59 struct l_newstat tbuf;
60 cdev_t dev;
61 int error;
63 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
64 tbuf.st_ino = INO64TO32(buf->st_ino);
65 tbuf.st_mode = buf->st_mode;
66 tbuf.st_nlink = buf->st_nlink;
67 tbuf.st_uid = buf->st_uid;
68 tbuf.st_gid = buf->st_gid;
69 tbuf.st_rdev = buf->st_rdev;
70 tbuf.st_size = buf->st_size;
71 tbuf.st_atime = buf->st_atime;
72 tbuf.st_mtime = buf->st_mtime;
73 tbuf.st_ctime = buf->st_ctime;
74 tbuf.st_blksize = buf->st_blksize;
75 tbuf.st_blocks = buf->st_blocks;
77 /* Lie about disk drives which are character devices
78 * in FreeBSD but block devices under Linux.
80 if (S_ISCHR(tbuf.st_mode) &&
81 (dev = udev2dev(buf->st_rdev, 0)) != NULL) {
82 if (dev_is_good(dev) && (dev_dflags(dev) & D_DISK)) {
83 tbuf.st_mode &= ~S_IFMT;
84 tbuf.st_mode |= S_IFBLK;
86 /* XXX this may not be quite right */
87 /* Map major number to 0 */
88 tbuf.st_dev = uminor(buf->st_dev) & 0xf;
89 tbuf.st_rdev = buf->st_rdev & 0xff;
93 error = copyout(&tbuf, ubuf, sizeof(tbuf));
94 return (error);
98 * MPALMOSTSAFE
101 sys_linux_newstat(struct linux_newstat_args *args)
103 struct stat buf;
104 struct nlookupdata nd;
105 char *path;
106 int error;
108 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
109 if (error)
110 return (error);
111 #ifdef DEBUG
112 if (ldebug(newstat))
113 kprintf(ARGS(newstat, "%s, *"), path);
114 #endif
115 get_mplock();
116 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
117 if (error == 0) {
118 error = kern_stat(&nd, &buf);
119 if (error == 0)
120 error = newstat_copyout(&buf, args->buf);
121 nlookup_done(&nd);
123 rel_mplock();
124 linux_free_path(&path);
125 return (error);
129 * MPALMOSTSAFE
132 sys_linux_newlstat(struct linux_newlstat_args *args)
134 struct stat sb;
135 struct nlookupdata nd;
136 char *path;
137 int error;
139 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
140 if (error)
141 return (error);
142 #ifdef DEBUG
143 if (ldebug(newlstat))
144 kprintf(ARGS(newlstat, "%s, *"), path);
145 #endif
146 get_mplock();
147 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
148 if (error == 0) {
149 error = kern_stat(&nd, &sb);
150 if (error == 0)
151 error = newstat_copyout(&sb, args->buf);
152 nlookup_done(&nd);
154 rel_mplock();
155 linux_free_path(&path);
156 return (error);
160 * MPALMOSTSAFE
163 sys_linux_newfstat(struct linux_newfstat_args *args)
165 struct stat buf;
166 int error;
168 #ifdef DEBUG
169 if (ldebug(newfstat))
170 kprintf(ARGS(newfstat, "%d, *"), args->fd);
171 #endif
172 get_mplock();
173 error = kern_fstat(args->fd, &buf);
174 rel_mplock();
176 if (error == 0)
177 error = newstat_copyout(&buf, args->buf);
178 return (error);
181 /* XXX - All fields of type l_int are defined as l_long on i386 */
182 struct l_statfs {
183 l_int f_type;
184 l_int f_bsize;
185 l_int f_blocks;
186 l_int f_bfree;
187 l_int f_bavail;
188 l_int f_files;
189 l_int f_ffree;
190 l_fsid_t f_fsid;
191 l_int f_namelen;
192 l_int f_spare[6];
195 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
196 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
197 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
198 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
199 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
200 #define LINUX_NCP_SUPER_MAGIC 0x564cL
201 #define LINUX_NFS_SUPER_MAGIC 0x6969L
202 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
203 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
204 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
206 static long
207 bsd_to_linux_ftype(const char *fstypename)
209 int i;
210 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
211 {"ufs", LINUX_UFS_SUPER_MAGIC},
212 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
213 {"nfs", LINUX_NFS_SUPER_MAGIC},
214 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
215 {"procfs", LINUX_PROC_SUPER_MAGIC},
216 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
217 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
218 {"nwfs", LINUX_NCP_SUPER_MAGIC},
219 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
220 {NULL, 0L}};
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);
226 return (0L);
229 static int
230 statfs_copyout(struct statfs *statfs, struct l_statfs_buf *buf, l_int namelen)
232 struct l_statfs linux_statfs;
233 int error;
235 linux_statfs.f_type = bsd_to_linux_ftype(statfs->f_fstypename);
236 linux_statfs.f_bsize = statfs->f_bsize;
237 linux_statfs.f_blocks = statfs->f_blocks;
238 linux_statfs.f_bfree = statfs->f_bfree;
239 linux_statfs.f_bavail = statfs->f_bavail;
240 linux_statfs.f_ffree = statfs->f_ffree;
241 linux_statfs.f_files = statfs->f_files;
242 linux_statfs.f_fsid.val[0] = statfs->f_fsid.val[0];
243 linux_statfs.f_fsid.val[1] = statfs->f_fsid.val[1];
244 linux_statfs.f_namelen = namelen;
246 error = copyout(&linux_statfs, buf, sizeof(linux_statfs));
247 return (error);
251 * MPALMOSTSAFE
254 sys_linux_statfs(struct linux_statfs_args *args)
256 struct statfs statfs;
257 struct nlookupdata nd;
258 char *path;
259 int error, namelen;
261 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
262 if (error)
263 return (error);
264 #ifdef DEBUG
265 if (ldebug(statfs))
266 kprintf(ARGS(statfs, "%s, *"), path);
267 #endif
268 get_mplock();
269 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
270 if (error == 0)
271 error = kern_statfs(&nd, &statfs);
272 if (error == 0) {
273 if (nd.nl_nch.ncp->nc_vp != NULL)
274 error = vn_get_namelen(nd.nl_nch.ncp->nc_vp, &namelen);
275 else
276 error = EINVAL;
278 nlookup_done(&nd);
279 rel_mplock();
280 if (error == 0)
281 error = statfs_copyout(&statfs, args->buf, (l_int)namelen);
282 linux_free_path(&path);
283 return (error);
287 * MPALMOSTSAFE
290 sys_linux_fstatfs(struct linux_fstatfs_args *args)
292 struct proc *p = curthread->td_proc;
293 struct file *fp;
294 struct statfs statfs;
295 int error, namelen;
297 #ifdef DEBUG
298 if (ldebug(fstatfs))
299 kprintf(ARGS(fstatfs, "%d, *"), args->fd);
300 #endif
301 get_mplock();
302 if ((error = kern_fstatfs(args->fd, &statfs)) != 0)
303 return (error);
304 if ((error = holdvnode(p->p_fd, args->fd, &fp)) != 0)
305 return (error);
306 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
307 rel_mplock();
308 fdrop(fp);
309 if (error == 0)
310 error = statfs_copyout(&statfs, args->buf, (l_int)namelen);
311 return (error);
314 struct l_ustat
316 l_daddr_t f_tfree;
317 l_ino_t f_tinode;
318 char f_fname[6];
319 char f_fpack[6];
323 * MPALMOSTSAFE
326 sys_linux_ustat(struct linux_ustat_args *args)
328 struct thread *td = curthread;
329 struct l_ustat lu;
330 cdev_t dev;
331 struct vnode *vp;
332 struct statfs *stat;
333 int error;
335 #ifdef DEBUG
336 if (ldebug(ustat))
337 kprintf(ARGS(ustat, "%d, *"), args->dev);
338 #endif
341 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
342 * lu.f_tinode and lu.f_tfree are set from the device's super block.
344 bzero(&lu, sizeof(lu));
347 * XXX - Don't return an error if we can't find a vnode for the
348 * device. Our cdev_t is 32-bits whereas Linux only has a 16-bits
349 * cdev_t. The dev_t that is used now may as well be a truncated
350 * cdev_t returned from previous syscalls. Just return a bzeroed
351 * ustat in that case.
353 get_mplock();
354 dev = udev2dev(makeudev(args->dev >> 8, args->dev & 0xFF), 0);
355 if (dev != NULL && vfinddev(dev, VCHR, &vp)) {
356 if (vp->v_mount == NULL) {
357 vrele(vp);
358 error = EINVAL;
359 goto done;
361 stat = &(vp->v_mount->mnt_stat);
362 error = VFS_STATFS(vp->v_mount, stat, td->td_ucred);
363 vrele(vp);
364 if (error == 0) {
365 lu.f_tfree = stat->f_bfree;
366 lu.f_tinode = stat->f_ffree;
368 } else {
369 error = 0;
371 done:
372 rel_mplock();
373 if (error == 0)
374 error = copyout(&lu, args->ubuf, sizeof(lu));
375 return (error);
378 #if defined(__i386__)
380 static int
381 stat64_copyout(struct stat *buf, void *ubuf)
383 struct l_stat64 lbuf;
384 int error;
386 bzero(&lbuf, sizeof(lbuf));
387 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
388 lbuf.st_ino = buf->st_ino;
389 lbuf.st_mode = buf->st_mode;
390 lbuf.st_nlink = buf->st_nlink;
391 lbuf.st_uid = buf->st_uid;
392 lbuf.st_gid = buf->st_gid;
393 lbuf.st_rdev = buf->st_rdev;
394 lbuf.st_size = buf->st_size;
395 lbuf.st_atime = buf->st_atime;
396 lbuf.st_mtime = buf->st_mtime;
397 lbuf.st_ctime = buf->st_ctime;
398 lbuf.st_blksize = buf->st_blksize;
399 lbuf.st_blocks = buf->st_blocks;
402 * The __st_ino field makes all the difference. In the Linux kernel
403 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
404 * but without the assignment to __st_ino the runtime linker refuses
405 * to mmap(2) any shared libraries. I guess it's broken alright :-)
407 lbuf.__st_ino = INO64TO32(buf->st_ino);
409 error = copyout(&lbuf, ubuf, sizeof(lbuf));
410 return (error);
414 * MPALMOSTSAFE
417 sys_linux_stat64(struct linux_stat64_args *args)
419 struct nlookupdata nd;
420 struct stat buf;
421 char *path;
422 int error;
424 error = linux_copyin_path(args->filename, &path, LINUX_PATH_EXISTS);
425 if (error)
426 return (error);
427 #ifdef DEBUG
428 if (ldebug(stat64))
429 kprintf(ARGS(stat64, "%s, *"), path);
430 #endif
431 get_mplock();
432 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
433 if (error == 0) {
434 error = kern_stat(&nd, &buf);
435 nlookup_done(&nd);
437 rel_mplock();
438 if (error == 0)
439 error = stat64_copyout(&buf, args->statbuf);
440 linux_free_path(&path);
441 return (error);
445 * MPALMOSTSAFE
448 sys_linux_lstat64(struct linux_lstat64_args *args)
450 struct nlookupdata nd;
451 struct stat sb;
452 char *path;
453 int error;
455 error = linux_copyin_path(args->filename, &path, LINUX_PATH_EXISTS);
456 if (error)
457 return (error);
458 #ifdef DEBUG
459 if (ldebug(lstat64))
460 kprintf(ARGS(lstat64, "%s, *"), path);
461 #endif
462 get_mplock();
463 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
464 if (error == 0) {
465 error = kern_stat(&nd, &sb);
466 nlookup_done(&nd);
468 rel_mplock();
469 if (error == 0)
470 error = stat64_copyout(&sb, args->statbuf);
471 linux_free_path(&path);
472 return (error);
476 * MPALMOSTSAFE
479 sys_linux_fstat64(struct linux_fstat64_args *args)
481 struct stat buf;
482 int error;
484 #ifdef DEBUG
485 if (ldebug(fstat64))
486 kprintf(ARGS(fstat64, "%d, *"), args->fd);
487 #endif
488 get_mplock();
489 error = kern_fstat(args->fd, &buf);
490 rel_mplock();
492 if (error == 0)
493 error = stat64_copyout(&buf, args->statbuf);
494 return (error);
497 #endif /* __i386__ */