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