kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.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.6 2003/08/07 21:17: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/namei.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
48 #include <emulation/linux/machine/linux.h>
49 #include <emulation/linux/machine/linux_proto.h>
50 #include "linux_util.h"
51
52 static int
53 newstat_copyout(struct stat *buf, void *ubuf)
54 {
55         struct l_newstat tbuf;
56         dev_t dev;
57
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;
71
72         /* Lie about disk drives which are character devices
73          * in FreeBSD but block devices under Linux.
74          */
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;
80
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;
85                 }
86         }
87
88         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
89 }
90
91 int
92 linux_newstat(struct linux_newstat_args *args)
93 {
94         struct thread *td = curthread;
95         struct stat buf;
96         struct nameidata nd;
97         int error;
98         caddr_t sg;
99
100         sg = stackgap_init();
101         CHECKALTEXIST(&sg, args->path);
102
103 #ifdef DEBUG
104         if (ldebug(newstat))
105                 printf(ARGS(newstat, "%s, *"), args->path);
106 #endif
107
108         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
109             args->path, td);
110         error = namei(&nd);
111         if (error)
112                 return (error);
113         NDFREE(&nd, NDF_ONLY_PNBUF);
114
115         error = vn_stat(nd.ni_vp, &buf, td);
116         vput(nd.ni_vp);
117         if (error)
118                 return (error);
119
120         return (newstat_copyout(&buf, args->buf));
121 }
122
123 int
124 linux_newlstat(struct linux_newlstat_args *args)
125 {
126         struct thread *td = curthread;
127         int error;
128         struct stat sb;
129         struct nameidata nd;
130         caddr_t sg;
131
132         sg = stackgap_init();
133         CHECKALTEXIST(&sg, args->path);
134
135 #ifdef DEBUG
136         if (ldebug(newlstat))
137                 printf(ARGS(newlstat, "%s, *"), args->path);
138 #endif
139
140         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
141             args->path, td);
142         error = namei(&nd);
143         if (error)
144                 return (error);
145         NDFREE(&nd, NDF_ONLY_PNBUF); 
146
147         error = vn_stat(nd.ni_vp, &sb, td);
148         vput(nd.ni_vp);
149         if (error)
150                 return (error);
151
152         return (newstat_copyout(&sb, args->buf));
153 }
154
155 int
156 linux_newfstat(struct linux_newfstat_args *args)
157 {
158         struct thread *td = curthread;
159         struct proc *p = td->td_proc;
160         struct filedesc *fdp;
161         struct file *fp;
162         struct stat buf;
163         int error;
164
165         KKASSERT(p);
166
167 #ifdef DEBUG
168         if (ldebug(newfstat))
169                 printf(ARGS(newfstat, "%d, *"), args->fd);
170 #endif
171
172         fdp = p->p_fd;
173         if ((unsigned)args->fd >= fdp->fd_nfiles ||
174             (fp = fdp->fd_ofiles[args->fd]) == NULL)
175                 return (EBADF);
176
177         error = fo_stat(fp, &buf, td);
178         if (!error)
179                 error = newstat_copyout(&buf, args->buf);
180
181         return (error);
182 }
183
184 /* XXX - All fields of type l_int are defined as l_long on i386 */
185 struct l_statfs {
186         l_int           f_type;
187         l_int           f_bsize;
188         l_int           f_blocks;
189         l_int           f_bfree;
190         l_int           f_bavail;
191         l_int           f_files;
192         l_int           f_ffree;
193         l_fsid_t        f_fsid;
194         l_int           f_namelen;
195         l_int           f_spare[6];
196 };
197
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 */
208
209 static long
210 bsd_to_linux_ftype(const char *fstypename)
211 {
212         int i;
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},
224                 {NULL,      0L}};
225
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);
229
230         return (0L);
231 }
232
233 int
234 linux_statfs(struct linux_statfs_args *args)
235 {
236         struct thread *td = curthread;
237         struct mount *mp;
238         struct nameidata *ndp;
239         struct statfs *bsd_statfs;
240         struct nameidata nd;
241         struct l_statfs linux_statfs;
242         int error;
243         caddr_t sg;
244
245         sg = stackgap_init();
246         CHECKALTEXIST(&sg, args->path);
247
248 #ifdef DEBUG
249         if (ldebug(statfs))
250                 printf(ARGS(statfs, "%s, *"), args->path);
251 #endif
252         ndp = &nd;
253         NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, td);
254         error = namei(ndp);
255         if (error)
256                 return error;
257         NDFREE(ndp, NDF_ONLY_PNBUF);
258         mp = ndp->ni_vp->v_mount;
259         bsd_statfs = &mp->mnt_stat;
260         vrele(ndp->ni_vp);
261         error = VFS_STATFS(mp, bsd_statfs, td);
262         if (error)
263                 return error;
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));
277 }
278
279 int
280 linux_fstatfs(struct linux_fstatfs_args *args)
281 {
282         struct thread *td = curthread;
283         struct proc *p = td->td_proc;
284         struct file *fp;
285         struct mount *mp;
286         struct statfs *bsd_statfs;
287         struct l_statfs linux_statfs;
288         int error;
289
290         KKASSERT(p);
291
292 #ifdef DEBUG
293         if (ldebug(fstatfs))
294                 printf(ARGS(fstatfs, "%d, *"), args->fd);
295 #endif
296         error = getvnode(p->p_fd, args->fd, &fp);
297         if (error)
298                 return error;
299         mp = ((struct vnode *)fp->f_data)->v_mount;
300         bsd_statfs = &mp->mnt_stat;
301         error = VFS_STATFS(mp, bsd_statfs, td);
302         if (error)
303                 return error;
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));
317 }
318
319 struct l_ustat 
320 {
321         l_daddr_t       f_tfree;
322         l_ino_t         f_tinode;
323         char            f_fname[6];
324         char            f_fpack[6];
325 };
326
327 int
328 linux_ustat(struct linux_ustat_args *args)
329 {
330         struct thread *td = curthread;
331         struct l_ustat lu;
332         dev_t dev;
333         struct vnode *vp;
334         struct statfs *stat;
335         int error;
336
337 #ifdef DEBUG
338         if (ldebug(ustat))
339                 printf(ARGS(ustat, "%d, *"), args->dev);
340 #endif
341
342         /*
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.
345          */
346         bzero(&lu, sizeof(lu));
347
348         /*
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.
354          */
355         dev = makedev(args->dev >> 8, args->dev & 0xFF);
356         if (vfinddev(dev, VCHR, &vp)) {
357                 if (vp->v_mount == NULL)
358                         return (EINVAL);
359                 stat = &(vp->v_mount->mnt_stat);
360                 error = VFS_STATFS(vp->v_mount, stat, td);
361                 if (error)
362                         return (error);
363
364                 lu.f_tfree = stat->f_bfree;
365                 lu.f_tinode = stat->f_ffree;
366         }
367
368         return (copyout(&lu, args->ubuf, sizeof(lu)));
369 }
370
371 #if defined(__i386__)
372
373 static int
374 stat64_copyout(struct stat *buf, void *ubuf)
375 {
376         struct l_stat64 lbuf;
377
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;
392
393         /*
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 :-)
398          */
399         lbuf.__st_ino = buf->st_ino;
400
401         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
402 }
403
404 int
405 linux_stat64(struct linux_stat64_args *args)
406 {
407         struct thread *td = curthread;
408         struct stat buf;
409         struct nameidata nd;
410         int error;
411         caddr_t sg;
412
413         sg = stackgap_init();
414         CHECKALTEXIST(&sg, args->filename);
415
416 #ifdef DEBUG
417         if (ldebug(stat64))
418                 printf(ARGS(stat64, "%s, *"), args->filename);
419 #endif
420
421         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
422             args->filename, td);
423         error = namei(&nd);
424         if (error)
425                 return (error);
426         NDFREE(&nd, NDF_ONLY_PNBUF);
427
428         error = vn_stat(nd.ni_vp, &buf, td);
429         vput(nd.ni_vp);
430         if (error)
431                 return (error);
432
433         return (stat64_copyout(&buf, args->statbuf));
434 }
435
436 int
437 linux_lstat64(struct linux_lstat64_args *args)
438 {
439         struct thread *td = curthread;
440         int error;
441         struct stat sb;
442         struct nameidata nd;
443         caddr_t sg;
444
445         sg = stackgap_init();
446         CHECKALTEXIST(&sg, args->filename);
447
448 #ifdef DEBUG
449         if (ldebug(lstat64))
450                 printf(ARGS(lstat64, "%s, *"), args->filename);
451 #endif
452
453         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
454             args->filename, td);
455         error = namei(&nd);
456         if (error)
457                 return (error);
458         NDFREE(&nd, NDF_ONLY_PNBUF); 
459
460         error = vn_stat(nd.ni_vp, &sb, td);
461         vput(nd.ni_vp);
462         if (error)
463                 return (error);
464
465         return (stat64_copyout(&sb, args->statbuf));
466 }
467
468 int
469 linux_fstat64(struct linux_fstat64_args *args)
470 {
471         struct thread *td = curthread;
472         struct proc *p = td->td_proc;
473         struct filedesc *fdp;
474         struct file *fp;
475         struct stat buf;
476         int error;
477
478         KKASSERT(p);
479
480 #ifdef DEBUG
481         if (ldebug(fstat64))
482                 printf(ARGS(fstat64, "%d, *"), args->fd);
483 #endif
484
485         fdp = p->p_fd;
486         if ((unsigned)args->fd >= fdp->fd_nfiles ||
487             (fp = fdp->fd_ofiles[args->fd]) == NULL)
488                 return (EBADF);
489
490         error = fo_stat(fp, &buf, td);
491         if (!error)
492                 error = stat64_copyout(&buf, args->statbuf);
493
494         return (error);
495 }
496
497 #endif /* __i386__ */