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