Create the kern_fstat() and kern_ftruncate() in-kernel syscalls.
[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.10 2003/10/21 01:05:09 daver 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 #include <sys/kern_syscall.h>
48
49 #include <arch_linux/linux.h>
50 #include <arch_linux/linux_proto.h>
51 #include "linux_util.h"
52
53 static int
54 newstat_copyout(struct stat *buf, void *ubuf)
55 {
56         struct l_newstat tbuf;
57         dev_t dev;
58
59         tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
60         tbuf.st_ino = buf->st_ino;
61         tbuf.st_mode = buf->st_mode;
62         tbuf.st_nlink = buf->st_nlink;
63         tbuf.st_uid = buf->st_uid;
64         tbuf.st_gid = buf->st_gid;
65         tbuf.st_rdev = buf->st_rdev;
66         tbuf.st_size = buf->st_size;
67         tbuf.st_atime = buf->st_atime;
68         tbuf.st_mtime = buf->st_mtime;
69         tbuf.st_ctime = buf->st_ctime;
70         tbuf.st_blksize = buf->st_blksize;
71         tbuf.st_blocks = buf->st_blocks;
72
73         /* Lie about disk drives which are character devices
74          * in FreeBSD but block devices under Linux.
75          */
76         if (S_ISCHR(tbuf.st_mode) &&
77             (dev = udev2dev(buf->st_rdev, 0)) != NODEV) {
78                 if (dev_dport(dev) != NULL && (dev_dflags(dev) & 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, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
110             UIO_USERSPACE, 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, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
142             UIO_USERSPACE, 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 stat buf;
160         int error;
161
162 #ifdef DEBUG
163         if (ldebug(newfstat))
164                 printf(ARGS(newfstat, "%d, *"), args->fd);
165 #endif
166         error = kern_fstat(args->fd, &buf);
167
168         if (error == 0)
169                 error = newstat_copyout(&buf, args->buf);
170         return (error);
171 }
172
173 /* XXX - All fields of type l_int are defined as l_long on i386 */
174 struct l_statfs {
175         l_int           f_type;
176         l_int           f_bsize;
177         l_int           f_blocks;
178         l_int           f_bfree;
179         l_int           f_bavail;
180         l_int           f_files;
181         l_int           f_ffree;
182         l_fsid_t        f_fsid;
183         l_int           f_namelen;
184         l_int           f_spare[6];
185 };
186
187 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
188 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
189 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
190 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
191 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
192 #define LINUX_NCP_SUPER_MAGIC   0x564cL
193 #define LINUX_NFS_SUPER_MAGIC   0x6969L
194 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
195 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
196 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
197
198 static long
199 bsd_to_linux_ftype(const char *fstypename)
200 {
201         int i;
202         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
203                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
204                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
205                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
206                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
207                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
208                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
209                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
210                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
211                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
212                 {"coda",    LINUX_CODA_SUPER_MAGIC},
213                 {NULL,      0L}};
214
215         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
216                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
217                         return (b2l_tbl[i].linux_type);
218
219         return (0L);
220 }
221
222 int
223 linux_statfs(struct linux_statfs_args *args)
224 {
225         struct thread *td = curthread;
226         struct mount *mp;
227         struct nameidata *ndp;
228         struct statfs *bsd_statfs;
229         struct nameidata nd;
230         struct l_statfs linux_statfs;
231         int error;
232         caddr_t sg;
233
234         sg = stackgap_init();
235         CHECKALTEXIST(&sg, args->path);
236
237 #ifdef DEBUG
238         if (ldebug(statfs))
239                 printf(ARGS(statfs, "%s, *"), args->path);
240 #endif
241         ndp = &nd;
242         NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args->path, td);
243         error = namei(ndp);
244         if (error)
245                 return error;
246         NDFREE(ndp, NDF_ONLY_PNBUF);
247         mp = ndp->ni_vp->v_mount;
248         bsd_statfs = &mp->mnt_stat;
249         vrele(ndp->ni_vp);
250         error = VFS_STATFS(mp, bsd_statfs, td);
251         if (error)
252                 return error;
253         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
254         linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
255         linux_statfs.f_bsize = bsd_statfs->f_bsize;
256         linux_statfs.f_blocks = bsd_statfs->f_blocks;
257         linux_statfs.f_bfree = bsd_statfs->f_bfree;
258         linux_statfs.f_bavail = bsd_statfs->f_bavail;
259         linux_statfs.f_ffree = bsd_statfs->f_ffree;
260         linux_statfs.f_files = bsd_statfs->f_files;
261         linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
262         linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
263         linux_statfs.f_namelen = MAXNAMLEN;
264         return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
265             sizeof(linux_statfs));
266 }
267
268 int
269 linux_fstatfs(struct linux_fstatfs_args *args)
270 {
271         struct thread *td = curthread;
272         struct proc *p = td->td_proc;
273         struct file *fp;
274         struct mount *mp;
275         struct statfs *bsd_statfs;
276         struct l_statfs linux_statfs;
277         int error;
278
279         KKASSERT(p);
280
281 #ifdef DEBUG
282         if (ldebug(fstatfs))
283                 printf(ARGS(fstatfs, "%d, *"), args->fd);
284 #endif
285         error = getvnode(p->p_fd, args->fd, &fp);
286         if (error)
287                 return error;
288         mp = ((struct vnode *)fp->f_data)->v_mount;
289         bsd_statfs = &mp->mnt_stat;
290         error = VFS_STATFS(mp, bsd_statfs, td);
291         if (error)
292                 return error;
293         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
294         linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
295         linux_statfs.f_bsize = bsd_statfs->f_bsize;
296         linux_statfs.f_blocks = bsd_statfs->f_blocks;
297         linux_statfs.f_bfree = bsd_statfs->f_bfree;
298         linux_statfs.f_bavail = bsd_statfs->f_bavail;
299         linux_statfs.f_ffree = bsd_statfs->f_ffree;
300         linux_statfs.f_files = bsd_statfs->f_files;
301         linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
302         linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
303         linux_statfs.f_namelen = MAXNAMLEN;
304         return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
305             sizeof(linux_statfs));
306 }
307
308 struct l_ustat 
309 {
310         l_daddr_t       f_tfree;
311         l_ino_t         f_tinode;
312         char            f_fname[6];
313         char            f_fpack[6];
314 };
315
316 int
317 linux_ustat(struct linux_ustat_args *args)
318 {
319         struct thread *td = curthread;
320         struct l_ustat lu;
321         dev_t dev;
322         struct vnode *vp;
323         struct statfs *stat;
324         int error;
325
326 #ifdef DEBUG
327         if (ldebug(ustat))
328                 printf(ARGS(ustat, "%d, *"), args->dev);
329 #endif
330
331         /*
332          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
333          * lu.f_tinode and lu.f_tfree are set from the device's super block.
334          */
335         bzero(&lu, sizeof(lu));
336
337         /*
338          * XXX - Don't return an error if we can't find a vnode for the
339          * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
340          * dev_t. The dev_t that is used now may as well be a truncated
341          * dev_t returned from previous syscalls. Just return a bzeroed
342          * ustat in that case.
343          */
344         dev = makedev(args->dev >> 8, args->dev & 0xFF);
345         if (vfinddev(dev, VCHR, &vp)) {
346                 if (vp->v_mount == NULL)
347                         return (EINVAL);
348                 stat = &(vp->v_mount->mnt_stat);
349                 error = VFS_STATFS(vp->v_mount, stat, td);
350                 if (error)
351                         return (error);
352
353                 lu.f_tfree = stat->f_bfree;
354                 lu.f_tinode = stat->f_ffree;
355         }
356
357         return (copyout(&lu, args->ubuf, sizeof(lu)));
358 }
359
360 #if defined(__i386__)
361
362 static int
363 stat64_copyout(struct stat *buf, void *ubuf)
364 {
365         struct l_stat64 lbuf;
366
367         bzero(&lbuf, sizeof(lbuf));
368         lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
369         lbuf.st_ino = buf->st_ino;
370         lbuf.st_mode = buf->st_mode;
371         lbuf.st_nlink = buf->st_nlink;
372         lbuf.st_uid = buf->st_uid;
373         lbuf.st_gid = buf->st_gid;
374         lbuf.st_rdev = buf->st_rdev;
375         lbuf.st_size = buf->st_size;
376         lbuf.st_atime = buf->st_atime;
377         lbuf.st_mtime = buf->st_mtime;
378         lbuf.st_ctime = buf->st_ctime;
379         lbuf.st_blksize = buf->st_blksize;
380         lbuf.st_blocks = buf->st_blocks;
381
382         /*
383          * The __st_ino field makes all the difference. In the Linux kernel
384          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
385          * but without the assignment to __st_ino the runtime linker refuses
386          * to mmap(2) any shared libraries. I guess it's broken alright :-)
387          */
388         lbuf.__st_ino = buf->st_ino;
389
390         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
391 }
392
393 int
394 linux_stat64(struct linux_stat64_args *args)
395 {
396         struct thread *td = curthread;
397         struct stat buf;
398         struct nameidata nd;
399         int error;
400         caddr_t sg;
401
402         sg = stackgap_init();
403         CHECKALTEXIST(&sg, args->filename);
404
405 #ifdef DEBUG
406         if (ldebug(stat64))
407                 printf(ARGS(stat64, "%s, *"), args->filename);
408 #endif
409
410         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
411                 UIO_USERSPACE, args->filename, td);
412         error = namei(&nd);
413         if (error)
414                 return (error);
415         NDFREE(&nd, NDF_ONLY_PNBUF);
416
417         error = vn_stat(nd.ni_vp, &buf, td);
418         vput(nd.ni_vp);
419         if (error)
420                 return (error);
421
422         return (stat64_copyout(&buf, args->statbuf));
423 }
424
425 int
426 linux_lstat64(struct linux_lstat64_args *args)
427 {
428         struct thread *td = curthread;
429         int error;
430         struct stat sb;
431         struct nameidata nd;
432         caddr_t sg;
433
434         sg = stackgap_init();
435         CHECKALTEXIST(&sg, args->filename);
436
437 #ifdef DEBUG
438         if (ldebug(lstat64))
439                 printf(ARGS(lstat64, "%s, *"), args->filename);
440 #endif
441
442         NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
443                 UIO_USERSPACE, args->filename, td);
444         error = namei(&nd);
445         if (error)
446                 return (error);
447         NDFREE(&nd, NDF_ONLY_PNBUF); 
448
449         error = vn_stat(nd.ni_vp, &sb, td);
450         vput(nd.ni_vp);
451         if (error)
452                 return (error);
453
454         return (stat64_copyout(&sb, args->statbuf));
455 }
456
457 int
458 linux_fstat64(struct linux_fstat64_args *args)
459 {
460         struct stat buf;
461         int error;
462
463 #ifdef DEBUG
464         if (ldebug(fstat64))
465                 printf(ARGS(fstat64, "%d, *"), args->fd);
466 #endif
467         error = kern_fstat(args->fd, &buf);
468
469         if (error == 0)
470                 error = stat64_copyout(&buf, args->statbuf);
471         return (error);
472 }
473
474 #endif /* __i386__ */