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