Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:19 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/dirent.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/proc.h>
38 #include <sys/mount.h>
39 #include <sys/namei.h>
40 #include <sys/stat.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
43 #include <sys/vnode.h>
44
45 #include <machine/../linux/linux.h>
46 #include <machine/../linux/linux_proto.h>
47 #include <compat/linux/linux_util.h>
48
49 static int
50 newstat_copyout(struct stat *buf, void *ubuf)
51 {
52         struct l_newstat tbuf;
53         struct cdevsw *cdevsw;
54         dev_t dev;
55
56         tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
57         tbuf.st_ino = buf->st_ino;
58         tbuf.st_mode = buf->st_mode;
59         tbuf.st_nlink = buf->st_nlink;
60         tbuf.st_uid = buf->st_uid;
61         tbuf.st_gid = buf->st_gid;
62         tbuf.st_rdev = buf->st_rdev;
63         tbuf.st_size = buf->st_size;
64         tbuf.st_atime = buf->st_atime;
65         tbuf.st_mtime = buf->st_mtime;
66         tbuf.st_ctime = buf->st_ctime;
67         tbuf.st_blksize = buf->st_blksize;
68         tbuf.st_blocks = buf->st_blocks;
69
70         /* Lie about disk drives which are character devices
71          * in FreeBSD but block devices under Linux.
72          */
73         if (S_ISCHR(tbuf.st_mode) &&
74             (dev = udev2dev(buf->st_rdev, 0)) != NODEV) {
75                 cdevsw = devsw(dev);
76                 if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
77                         tbuf.st_mode &= ~S_IFMT;
78                         tbuf.st_mode |= S_IFBLK;
79
80                         /* XXX this may not be quite right */
81                         /* Map major number to 0 */
82                         tbuf.st_dev = uminor(buf->st_dev) & 0xf;
83                         tbuf.st_rdev = buf->st_rdev & 0xff;
84                 }
85         }
86
87         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
88 }
89
90 int
91 linux_newstat(struct proc *p, struct linux_newstat_args *args)
92 {
93         struct stat buf;
94         struct nameidata nd;
95         int error;
96         caddr_t sg;
97
98         sg = stackgap_init();
99         CHECKALTEXIST(p, &sg, args->path);
100
101 #ifdef DEBUG
102         if (ldebug(newstat))
103                 printf(ARGS(newstat, "%s, *"), args->path);
104 #endif
105
106         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
107             args->path, p);
108         error = namei(&nd);
109         if (error)
110                 return (error);
111         NDFREE(&nd, NDF_ONLY_PNBUF);
112
113         error = vn_stat(nd.ni_vp, &buf, p);
114         vput(nd.ni_vp);
115         if (error)
116                 return (error);
117
118         return (newstat_copyout(&buf, args->buf));
119 }
120
121 int
122 linux_newlstat(struct proc *p, struct linux_newlstat_args *args)
123 {
124         int error;
125         struct stat sb;
126         struct nameidata nd;
127         caddr_t sg;
128
129         sg = stackgap_init();
130         CHECKALTEXIST(p, &sg, args->path);
131
132 #ifdef DEBUG
133         if (ldebug(newlstat))
134                 printf(ARGS(newlstat, "%s, *"), args->path);
135 #endif
136
137         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
138             args->path, p);
139         error = namei(&nd);
140         if (error)
141                 return (error);
142         NDFREE(&nd, NDF_ONLY_PNBUF); 
143
144         error = vn_stat(nd.ni_vp, &sb, p);
145         vput(nd.ni_vp);
146         if (error)
147                 return (error);
148
149         return (newstat_copyout(&sb, args->buf));
150 }
151
152 int
153 linux_newfstat(struct proc *p, struct linux_newfstat_args *args)
154 {
155         struct filedesc *fdp;
156         struct file *fp;
157         struct stat buf;
158         int error;
159
160 #ifdef DEBUG
161         if (ldebug(newfstat))
162                 printf(ARGS(newfstat, "%d, *"), args->fd);
163 #endif
164
165         fdp = p->p_fd;
166         if ((unsigned)args->fd >= fdp->fd_nfiles ||
167             (fp = fdp->fd_ofiles[args->fd]) == NULL)
168                 return (EBADF);
169
170         error = fo_stat(fp, &buf, p);
171         if (!error)
172                 error = newstat_copyout(&buf, args->buf);
173
174         return (error);
175 }
176
177 /* XXX - All fields of type l_int are defined as l_long on i386 */
178 struct l_statfs {
179         l_int           f_type;
180         l_int           f_bsize;
181         l_int           f_blocks;
182         l_int           f_bfree;
183         l_int           f_bavail;
184         l_int           f_files;
185         l_int           f_ffree;
186         l_fsid_t        f_fsid;
187         l_int           f_namelen;
188         l_int           f_spare[6];
189 };
190
191 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
192 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
193 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
194 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
195 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
196 #define LINUX_NCP_SUPER_MAGIC   0x564cL
197 #define LINUX_NFS_SUPER_MAGIC   0x6969L
198 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
199 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
200 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
201
202 static long
203 bsd_to_linux_ftype(const char *fstypename)
204 {
205         int i;
206         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
207                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
208                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
209                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
210                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
211                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
212                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
213                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
214                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
215                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
216                 {"coda",    LINUX_CODA_SUPER_MAGIC},
217                 {NULL,      0L}};
218
219         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
220                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
221                         return (b2l_tbl[i].linux_type);
222
223         return (0L);
224 }
225
226 int
227 linux_statfs(struct proc *p, struct linux_statfs_args *args)
228 {
229         struct mount *mp;
230         struct nameidata *ndp;
231         struct statfs *bsd_statfs;
232         struct nameidata nd;
233         struct l_statfs linux_statfs;
234         int error;
235         caddr_t sg;
236
237         sg = stackgap_init();
238         CHECKALTEXIST(p, &sg, args->path);
239
240 #ifdef DEBUG
241         if (ldebug(statfs))
242                 printf(ARGS(statfs, "%s, *"), args->path);
243 #endif
244         ndp = &nd;
245         NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, curproc);
246         error = namei(ndp);
247         if (error)
248                 return error;
249         NDFREE(ndp, NDF_ONLY_PNBUF);
250         mp = ndp->ni_vp->v_mount;
251         bsd_statfs = &mp->mnt_stat;
252         vrele(ndp->ni_vp);
253         error = VFS_STATFS(mp, bsd_statfs, p);
254         if (error)
255                 return error;
256         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
257         linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
258         linux_statfs.f_bsize = bsd_statfs->f_bsize;
259         linux_statfs.f_blocks = bsd_statfs->f_blocks;
260         linux_statfs.f_bfree = bsd_statfs->f_bfree;
261         linux_statfs.f_bavail = bsd_statfs->f_bavail;
262         linux_statfs.f_ffree = bsd_statfs->f_ffree;
263         linux_statfs.f_files = bsd_statfs->f_files;
264         linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
265         linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
266         linux_statfs.f_namelen = MAXNAMLEN;
267         return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
268             sizeof(linux_statfs));
269 }
270
271 int
272 linux_fstatfs(struct proc *p, struct linux_fstatfs_args *args)
273 {
274         struct file *fp;
275         struct mount *mp;
276         struct statfs *bsd_statfs;
277         struct l_statfs linux_statfs;
278         int error;
279
280 #ifdef DEBUG
281         if (ldebug(fstatfs))
282                 printf(ARGS(fstatfs, "%d, *"), args->fd);
283 #endif
284         error = getvnode(p->p_fd, args->fd, &fp);
285         if (error)
286                 return error;
287         mp = ((struct vnode *)fp->f_data)->v_mount;
288         bsd_statfs = &mp->mnt_stat;
289         error = VFS_STATFS(mp, bsd_statfs, p);
290         if (error)
291                 return error;
292         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
293         linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
294         linux_statfs.f_bsize = bsd_statfs->f_bsize;
295         linux_statfs.f_blocks = bsd_statfs->f_blocks;
296         linux_statfs.f_bfree = bsd_statfs->f_bfree;
297         linux_statfs.f_bavail = bsd_statfs->f_bavail;
298         linux_statfs.f_ffree = bsd_statfs->f_ffree;
299         linux_statfs.f_files = bsd_statfs->f_files;
300         linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
301         linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
302         linux_statfs.f_namelen = MAXNAMLEN;
303         return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
304             sizeof(linux_statfs));
305 }
306
307 struct l_ustat 
308 {
309         l_daddr_t       f_tfree;
310         l_ino_t         f_tinode;
311         char            f_fname[6];
312         char            f_fpack[6];
313 };
314
315 int
316 linux_ustat(struct proc *p, struct linux_ustat_args *args)
317 {
318         struct l_ustat lu;
319         dev_t dev;
320         struct vnode *vp;
321         struct statfs *stat;
322         int error;
323
324 #ifdef DEBUG
325         if (ldebug(ustat))
326                 printf(ARGS(ustat, "%d, *"), args->dev);
327 #endif
328
329         /*
330          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
331          * lu.f_tinode and lu.f_tfree are set from the device's super block.
332          */
333         bzero(&lu, sizeof(lu));
334
335         /*
336          * XXX - Don't return an error if we can't find a vnode for the
337          * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
338          * dev_t. The dev_t that is used now may as well be a truncated
339          * dev_t returned from previous syscalls. Just return a bzeroed
340          * ustat in that case.
341          */
342         dev = makedev(args->dev >> 8, args->dev & 0xFF);
343         if (vfinddev(dev, VCHR, &vp)) {
344                 if (vp->v_mount == NULL)
345                         return (EINVAL);
346                 stat = &(vp->v_mount->mnt_stat);
347                 error = VFS_STATFS(vp->v_mount, stat, p);
348                 if (error)
349                         return (error);
350
351                 lu.f_tfree = stat->f_bfree;
352                 lu.f_tinode = stat->f_ffree;
353         }
354
355         return (copyout(&lu, args->ubuf, sizeof(lu)));
356 }
357
358 #if defined(__i386__)
359
360 static int
361 stat64_copyout(struct stat *buf, void *ubuf)
362 {
363         struct l_stat64 lbuf;
364
365         bzero(&lbuf, sizeof(lbuf));
366         lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
367         lbuf.st_ino = buf->st_ino;
368         lbuf.st_mode = buf->st_mode;
369         lbuf.st_nlink = buf->st_nlink;
370         lbuf.st_uid = buf->st_uid;
371         lbuf.st_gid = buf->st_gid;
372         lbuf.st_rdev = buf->st_rdev;
373         lbuf.st_size = buf->st_size;
374         lbuf.st_atime = buf->st_atime;
375         lbuf.st_mtime = buf->st_mtime;
376         lbuf.st_ctime = buf->st_ctime;
377         lbuf.st_blksize = buf->st_blksize;
378         lbuf.st_blocks = buf->st_blocks;
379
380         /*
381          * The __st_ino field makes all the difference. In the Linux kernel
382          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
383          * but without the assignment to __st_ino the runtime linker refuses
384          * to mmap(2) any shared libraries. I guess it's broken alright :-)
385          */
386         lbuf.__st_ino = buf->st_ino;
387
388         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
389 }
390
391 int
392 linux_stat64(struct proc *p, struct linux_stat64_args *args)
393 {
394         struct stat buf;
395         struct nameidata nd;
396         int error;
397         caddr_t sg;
398
399         sg = stackgap_init();
400         CHECKALTEXIST(p, &sg, args->filename);
401
402 #ifdef DEBUG
403         if (ldebug(stat64))
404                 printf(ARGS(stat64, "%s, *"), args->filename);
405 #endif
406
407         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
408             args->filename, p);
409         error = namei(&nd);
410         if (error)
411                 return (error);
412         NDFREE(&nd, NDF_ONLY_PNBUF);
413
414         error = vn_stat(nd.ni_vp, &buf, p);
415         vput(nd.ni_vp);
416         if (error)
417                 return (error);
418
419         return (stat64_copyout(&buf, args->statbuf));
420 }
421
422 int
423 linux_lstat64(struct proc *p, struct linux_lstat64_args *args)
424 {
425         int error;
426         struct stat sb;
427         struct nameidata nd;
428         caddr_t sg;
429
430         sg = stackgap_init();
431         CHECKALTEXIST(p, &sg, args->filename);
432
433 #ifdef DEBUG
434         if (ldebug(lstat64))
435                 printf(ARGS(lstat64, "%s, *"), args->filename);
436 #endif
437
438         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
439             args->filename, p);
440         error = namei(&nd);
441         if (error)
442                 return (error);
443         NDFREE(&nd, NDF_ONLY_PNBUF); 
444
445         error = vn_stat(nd.ni_vp, &sb, p);
446         vput(nd.ni_vp);
447         if (error)
448                 return (error);
449
450         return (stat64_copyout(&sb, args->statbuf));
451 }
452
453 int
454 linux_fstat64(struct proc *p, struct linux_fstat64_args *args)
455 {
456         struct filedesc *fdp;
457         struct file *fp;
458         struct stat buf;
459         int error;
460
461 #ifdef DEBUG
462         if (ldebug(fstat64))
463                 printf(ARGS(fstat64, "%d, *"), args->fd);
464 #endif
465
466         fdp = p->p_fd;
467         if ((unsigned)args->fd >= fdp->fd_nfiles ||
468             (fp = fdp->fd_ofiles[args->fd]) == NULL)
469                 return (EBADF);
470
471         error = fo_stat(fp, &buf, p);
472         if (!error)
473                 error = stat64_copyout(&buf, args->statbuf);
474
475         return (error);
476 }
477
478 #endif /* __i386__ */