Merge from vendor branch BIND:
[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.23 2006/09/10 01:26:38 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/unistd.h>
45 #include <sys/vnode.h>
46 #include <sys/device.h>
47 #include <sys/file2.h>
48 #include <sys/kern_syscall.h>
49
50 #include <arch_linux/linux.h>
51 #include <arch_linux/linux_proto.h>
52 #include "linux_util.h"
53
54 static int
55 newstat_copyout(struct stat *buf, void *ubuf)
56 {
57         struct l_newstat tbuf;
58         cdev_t dev;
59         int error;
60
61         tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
62         tbuf.st_ino = buf->st_ino;
63         tbuf.st_mode = buf->st_mode;
64         tbuf.st_nlink = buf->st_nlink;
65         tbuf.st_uid = buf->st_uid;
66         tbuf.st_gid = buf->st_gid;
67         tbuf.st_rdev = buf->st_rdev;
68         tbuf.st_size = buf->st_size;
69         tbuf.st_atime = buf->st_atime;
70         tbuf.st_mtime = buf->st_mtime;
71         tbuf.st_ctime = buf->st_ctime;
72         tbuf.st_blksize = buf->st_blksize;
73         tbuf.st_blocks = buf->st_blocks;
74
75         /* Lie about disk drives which are character devices
76          * in FreeBSD but block devices under Linux.
77          */
78         if (S_ISCHR(tbuf.st_mode) &&
79             (dev = udev2dev(buf->st_rdev, 0)) != NOCDEV) {
80                 if (dev_is_good(dev) && (dev_dflags(dev) & 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         }
90
91         error = copyout(&tbuf, ubuf, sizeof(tbuf));
92         return (error);
93 }
94
95 int
96 sys_linux_newstat(struct linux_newstat_args *args)
97 {
98         struct stat buf;
99         struct nlookupdata nd;
100         char *path;
101         int error;
102
103         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
104         if (error)
105                 return (error);
106 #ifdef DEBUG
107         if (ldebug(newstat))
108                 printf(ARGS(newstat, "%s, *"), path);
109 #endif
110         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
111         if (error == 0) {
112                 error = kern_stat(&nd, &buf);
113                 if (error == 0)
114                         error = newstat_copyout(&buf, args->buf);
115                 nlookup_done(&nd);
116         }
117         linux_free_path(&path);
118         return (error);
119 }
120
121 int
122 sys_linux_newlstat(struct linux_newlstat_args *args)
123 {
124         struct stat sb;
125         struct nlookupdata nd;
126         char *path;
127         int error;
128
129         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
130         if (error)
131                 return (error);
132 #ifdef DEBUG
133         if (ldebug(newlstat))
134                 printf(ARGS(newlstat, "%s, *"), path);
135 #endif
136         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
137         if (error == 0) {
138                 error = kern_stat(&nd, &sb);
139                 if (error == 0)
140                         error = newstat_copyout(&sb, args->buf);
141                 nlookup_done(&nd);
142         }
143         linux_free_path(&path);
144         return (error);
145 }
146
147 int
148 sys_linux_newfstat(struct linux_newfstat_args *args)
149 {
150         struct stat buf;
151         int error;
152
153 #ifdef DEBUG
154         if (ldebug(newfstat))
155                 printf(ARGS(newfstat, "%d, *"), args->fd);
156 #endif
157         error = kern_fstat(args->fd, &buf);
158
159         if (error == 0)
160                 error = newstat_copyout(&buf, args->buf);
161         return (error);
162 }
163
164 /* XXX - All fields of type l_int are defined as l_long on i386 */
165 struct l_statfs {
166         l_int           f_type;
167         l_int           f_bsize;
168         l_int           f_blocks;
169         l_int           f_bfree;
170         l_int           f_bavail;
171         l_int           f_files;
172         l_int           f_ffree;
173         l_fsid_t        f_fsid;
174         l_int           f_namelen;
175         l_int           f_spare[6];
176 };
177
178 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
179 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
180 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
181 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
182 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
183 #define LINUX_NCP_SUPER_MAGIC   0x564cL
184 #define LINUX_NFS_SUPER_MAGIC   0x6969L
185 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
186 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
187 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
188
189 static long
190 bsd_to_linux_ftype(const char *fstypename)
191 {
192         int i;
193         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
194                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
195                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
196                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
197                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
198                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
199                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
200                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
201                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
202                 {"hpfs",    LINUX_HPFS_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, l_int namelen)
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 = namelen;
228
229         error = copyout(&linux_statfs, buf, sizeof(linux_statfs));
230         return (error);
231 }
232
233 int
234 sys_linux_statfs(struct linux_statfs_args *args)
235 {
236         struct statfs statfs;
237         struct nlookupdata nd;
238         char *path;
239         int error, namelen;
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         if (error == 0) {
252                 if (nd.nl_ncp->nc_vp != NULL)
253                         error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
254                 else
255                         error = EINVAL;
256         }
257         nlookup_done(&nd);
258         if (error == 0)
259                 error = statfs_copyout(&statfs, args->buf, (l_int)namelen);
260         linux_free_path(&path);
261         return (error);
262 }
263
264 int
265 sys_linux_fstatfs(struct linux_fstatfs_args *args)
266 {
267         struct proc *p = curthread->td_proc;
268         struct file *fp;
269         struct statfs statfs;
270         int error, namelen;
271
272 #ifdef DEBUG
273         if (ldebug(fstatfs))
274                 printf(ARGS(fstatfs, "%d, *"), args->fd);
275 #endif
276         if ((error = kern_fstatfs(args->fd, &statfs)) != 0)
277                 return (error);
278         if ((error = holdvnode(p->p_fd, args->fd, &fp)) != 0)
279                 return (error);
280         error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
281         fdrop(fp);
282         if (error == 0)
283                 error = statfs_copyout(&statfs, args->buf, (l_int)namelen);
284         return (error);
285 }
286
287 struct l_ustat 
288 {
289         l_daddr_t       f_tfree;
290         l_ino_t         f_tinode;
291         char            f_fname[6];
292         char            f_fpack[6];
293 };
294
295 int
296 sys_linux_ustat(struct linux_ustat_args *args)
297 {
298         struct l_ustat lu;
299         cdev_t dev;
300         struct vnode *vp;
301         struct statfs *stat;
302         int error;
303
304 #ifdef DEBUG
305         if (ldebug(ustat))
306                 printf(ARGS(ustat, "%d, *"), args->dev);
307 #endif
308
309         /*
310          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
311          * lu.f_tinode and lu.f_tfree are set from the device's super block.
312          */
313         bzero(&lu, sizeof(lu));
314
315         /*
316          * XXX - Don't return an error if we can't find a vnode for the
317          * device. Our cdev_t is 32-bits whereas Linux only has a 16-bits
318          * cdev_t. The dev_t that is used now may as well be a truncated
319          * cdev_t returned from previous syscalls. Just return a bzeroed
320          * ustat in that case.
321          */
322         dev = udev2dev(makeudev(args->dev >> 8, args->dev & 0xFF), 0);
323         if (dev != NOCDEV && vfinddev(dev, VCHR, &vp)) {
324                 if (vp->v_mount == NULL) {
325                         return (EINVAL);
326                 }
327                 stat = &(vp->v_mount->mnt_stat);
328                 error = VFS_STATFS(vp->v_mount, stat, curproc->p_ucred);
329                 if (error) {
330                         return (error);
331                 }
332                 lu.f_tfree = stat->f_bfree;
333                 lu.f_tinode = stat->f_ffree;
334         }
335         return (copyout(&lu, args->ubuf, sizeof(lu)));
336 }
337
338 #if defined(__i386__)
339
340 static int
341 stat64_copyout(struct stat *buf, void *ubuf)
342 {
343         struct l_stat64 lbuf;
344         int error;
345
346         bzero(&lbuf, sizeof(lbuf));
347         lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
348         lbuf.st_ino = buf->st_ino;
349         lbuf.st_mode = buf->st_mode;
350         lbuf.st_nlink = buf->st_nlink;
351         lbuf.st_uid = buf->st_uid;
352         lbuf.st_gid = buf->st_gid;
353         lbuf.st_rdev = buf->st_rdev;
354         lbuf.st_size = buf->st_size;
355         lbuf.st_atime = buf->st_atime;
356         lbuf.st_mtime = buf->st_mtime;
357         lbuf.st_ctime = buf->st_ctime;
358         lbuf.st_blksize = buf->st_blksize;
359         lbuf.st_blocks = buf->st_blocks;
360
361         /*
362          * The __st_ino field makes all the difference. In the Linux kernel
363          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
364          * but without the assignment to __st_ino the runtime linker refuses
365          * to mmap(2) any shared libraries. I guess it's broken alright :-)
366          */
367         lbuf.__st_ino = buf->st_ino;
368
369         error = copyout(&lbuf, ubuf, sizeof(lbuf));
370         return (error);
371 }
372
373 int
374 sys_linux_stat64(struct linux_stat64_args *args)
375 {
376         struct nlookupdata nd;
377         struct stat buf;
378         char *path;
379         int error;
380
381         error = linux_copyin_path(args->filename, &path, LINUX_PATH_EXISTS);
382         if (error)
383                 return (error);
384 #ifdef DEBUG
385         if (ldebug(stat64))
386                 printf(ARGS(stat64, "%s, *"), path);
387 #endif
388         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
389         if (error == 0) {
390                 error = kern_stat(&nd, &buf);
391                 if (error == 0)
392                         error = stat64_copyout(&buf, args->statbuf);
393                 nlookup_done(&nd);
394         }
395         linux_free_path(&path);
396         return (error);
397 }
398
399 int
400 sys_linux_lstat64(struct linux_lstat64_args *args)
401 {
402         struct nlookupdata nd;
403         struct stat sb;
404         char *path;
405         int error;
406
407         error = linux_copyin_path(args->filename, &path, LINUX_PATH_EXISTS);
408         if (error)
409                 return (error);
410 #ifdef DEBUG
411         if (ldebug(lstat64))
412                 printf(ARGS(lstat64, "%s, *"), path);
413 #endif
414         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
415         if (error == 0) {
416                 error = kern_stat(&nd, &sb);
417                 if (error == 0)
418                         error = stat64_copyout(&sb, args->statbuf);
419                 nlookup_done(&nd);
420         }
421         linux_free_path(&path);
422         return (error);
423 }
424
425 int
426 sys_linux_fstat64(struct linux_fstat64_args *args)
427 {
428         struct stat buf;
429         int error;
430
431 #ifdef DEBUG
432         if (ldebug(fstat64))
433                 printf(ARGS(fstat64, "%d, *"), args->fd);
434 #endif
435         error = kern_fstat(args->fd, &buf);
436
437         if (error == 0)
438                 error = stat64_copyout(&buf, args->statbuf);
439         return (error);
440 }
441
442 #endif /* __i386__ */