Merge branch 'net80211-update' of git://leaf.dragonflybsd.org/~rpaulo/dragonfly into...
[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.27 2008/09/28 05:08:16 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/nlookup.h>
41 #include <sys/stat.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/unistd.h>
45 #include <sys/vnode.h>
46 #include <sys/device.h>
47 #include <sys/kern_syscall.h>
48
49 #include <sys/file2.h>
50 #include <sys/mplock2.h>
51
52 #include <arch_linux/linux.h>
53 #include <arch_linux/linux_proto.h>
54 #include "linux_util.h"
55
56 static int
57 newstat_copyout(struct stat *buf, void *ubuf)
58 {
59         struct l_newstat tbuf;
60         int error;
61
62         bzero(&tbuf, sizeof(tbuf));
63         tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
64         tbuf.st_ino = INO64TO32(buf->st_ino);
65         tbuf.st_mode = buf->st_mode;
66         tbuf.st_nlink = buf->st_nlink;
67         tbuf.st_uid = buf->st_uid;
68         tbuf.st_gid = buf->st_gid;
69         tbuf.st_rdev = buf->st_rdev;
70         tbuf.st_size = buf->st_size;
71         tbuf.st_atime = buf->st_atime;
72         tbuf.st_mtime = buf->st_mtime;
73         tbuf.st_ctime = buf->st_ctime;
74         tbuf.st_blksize = buf->st_blksize;
75         tbuf.st_blocks = buf->st_blocks;
76
77         error = copyout(&tbuf, ubuf, sizeof(tbuf));
78         return (error);
79 }
80
81 /*
82  * MPALMOSTSAFE
83  */
84 int
85 sys_linux_newstat(struct linux_newstat_args *args)
86 {
87         struct stat buf;
88         struct nlookupdata nd;
89         char *path;
90         int error;
91
92         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
93         if (error)
94                 return (error);
95 #ifdef DEBUG
96         if (ldebug(newstat))
97                 kprintf(ARGS(newstat, "%s, *"), path);
98 #endif
99         get_mplock();
100         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
101         if (error == 0) {
102                 error = kern_stat(&nd, &buf);
103                 if (error == 0)
104                         error = newstat_copyout(&buf, args->buf);
105                 nlookup_done(&nd);
106         }
107         rel_mplock();
108         linux_free_path(&path);
109         return (error);
110 }
111
112 /*
113  * MPALMOSTSAFE
114  */
115 int
116 sys_linux_newlstat(struct linux_newlstat_args *args)
117 {
118         struct stat sb;
119         struct nlookupdata nd;
120         char *path;
121         int error;
122
123         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
124         if (error)
125                 return (error);
126 #ifdef DEBUG
127         if (ldebug(newlstat))
128                 kprintf(ARGS(newlstat, "%s, *"), path);
129 #endif
130         get_mplock();
131         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
132         if (error == 0) {
133                 error = kern_stat(&nd, &sb);
134                 if (error == 0)
135                         error = newstat_copyout(&sb, args->buf);
136                 nlookup_done(&nd);
137         }
138         rel_mplock();
139         linux_free_path(&path);
140         return (error);
141 }
142
143 /*
144  * MPALMOSTSAFE
145  */
146 int
147 sys_linux_newfstat(struct linux_newfstat_args *args)
148 {
149         struct stat buf;
150         int error;
151
152 #ifdef DEBUG
153         if (ldebug(newfstat))
154                 kprintf(ARGS(newfstat, "%d, *"), args->fd);
155 #endif
156         get_mplock();
157         error = kern_fstat(args->fd, &buf);
158         rel_mplock();
159
160         if (error == 0)
161                 error = newstat_copyout(&buf, args->buf);
162         return (error);
163 }
164
165 /* XXX - All fields of type l_int are defined as l_long on i386 */
166 struct l_statfs {
167         l_int           f_type;
168         l_int           f_bsize;
169         l_int           f_blocks;
170         l_int           f_bfree;
171         l_int           f_bavail;
172         l_int           f_files;
173         l_int           f_ffree;
174         l_fsid_t        f_fsid;
175         l_int           f_namelen;
176         l_int           f_spare[6];
177 };
178
179 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
180 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
181 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
182 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
183 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
184 #define LINUX_NCP_SUPER_MAGIC   0x564cL
185 #define LINUX_NFS_SUPER_MAGIC   0x6969L
186 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
187 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
188 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
189
190 static long
191 bsd_to_linux_ftype(const char *fstypename)
192 {
193         int i;
194         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
195                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
196                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
197                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
198                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
199                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
200                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
201                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
202                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
203                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
204                 {NULL,      0L}};
205
206         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
207                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
208                         return (b2l_tbl[i].linux_type);
209
210         return (0L);
211 }
212
213 static int
214 statfs_copyout(struct statfs *statfs, struct l_statfs_buf *buf, l_int namelen)
215 {
216         struct l_statfs linux_statfs;
217         int error;
218
219         linux_statfs.f_type = bsd_to_linux_ftype(statfs->f_fstypename);
220         linux_statfs.f_bsize = statfs->f_bsize;
221         linux_statfs.f_blocks = statfs->f_blocks;
222         linux_statfs.f_bfree = statfs->f_bfree;
223         linux_statfs.f_bavail = statfs->f_bavail;
224         linux_statfs.f_ffree = statfs->f_ffree;
225         linux_statfs.f_files = statfs->f_files;
226         linux_statfs.f_fsid.val[0] = statfs->f_fsid.val[0];
227         linux_statfs.f_fsid.val[1] = statfs->f_fsid.val[1];
228         linux_statfs.f_namelen = namelen;
229
230         error = copyout(&linux_statfs, buf, sizeof(linux_statfs));
231         return (error);
232 }
233
234 /*
235  * MPALMOSTSAFE
236  */
237 int
238 sys_linux_statfs(struct linux_statfs_args *args)
239 {
240         struct statfs statfs;
241         struct nlookupdata nd;
242         char *path;
243         int error, namelen;
244
245         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
246         if (error)
247                 return (error);
248 #ifdef DEBUG
249         if (ldebug(statfs))
250                 kprintf(ARGS(statfs, "%s, *"), path);
251 #endif
252         get_mplock();
253         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
254         if (error == 0)
255                 error = kern_statfs(&nd, &statfs);
256         if (error == 0) {
257                 if (nd.nl_nch.ncp->nc_vp != NULL)
258                         error = vn_get_namelen(nd.nl_nch.ncp->nc_vp, &namelen);
259                 else
260                         error = EINVAL;
261         }
262         nlookup_done(&nd);
263         rel_mplock();
264         if (error == 0)
265                 error = statfs_copyout(&statfs, args->buf, (l_int)namelen);
266         linux_free_path(&path);
267         return (error);
268 }
269
270 /*
271  * MPALMOSTSAFE
272  */
273 int
274 sys_linux_fstatfs(struct linux_fstatfs_args *args)
275 {
276         struct proc *p = curthread->td_proc;
277         struct file *fp;
278         struct statfs statfs;
279         int error, namelen;
280
281 #ifdef DEBUG
282         if (ldebug(fstatfs))
283                 kprintf(ARGS(fstatfs, "%d, *"), args->fd);
284 #endif
285         get_mplock();
286         if ((error = kern_fstatfs(args->fd, &statfs)) != 0)
287                 return (error);
288         if ((error = holdvnode(p->p_fd, args->fd, &fp)) != 0)
289                 return (error);
290         error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
291         rel_mplock();
292         fdrop(fp);
293         if (error == 0)
294                 error = statfs_copyout(&statfs, args->buf, (l_int)namelen);
295         return (error);
296 }
297
298 struct l_ustat 
299 {
300         l_daddr_t       f_tfree;
301         l_ino_t         f_tinode;
302         char            f_fname[6];
303         char            f_fpack[6];
304 };
305
306 /*
307  * MPALMOSTSAFE
308  */
309 int
310 sys_linux_ustat(struct linux_ustat_args *args)
311 {
312         struct thread *td = curthread;
313         struct l_ustat lu;
314         cdev_t dev;
315         struct vnode *vp;
316         struct statfs *stat;
317         int error;
318
319 #ifdef DEBUG
320         if (ldebug(ustat))
321                 kprintf(ARGS(ustat, "%d, *"), args->dev);
322 #endif
323
324         /*
325          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
326          * lu.f_tinode and lu.f_tfree are set from the device's super block.
327          */
328         bzero(&lu, sizeof(lu));
329
330         /*
331          * XXX - Don't return an error if we can't find a vnode for the
332          * device. Our cdev_t is 32-bits whereas Linux only has a 16-bits
333          * cdev_t. The dev_t that is used now may as well be a truncated
334          * cdev_t returned from previous syscalls. Just return a bzeroed
335          * ustat in that case.
336          */
337         get_mplock();
338         dev = udev2dev(makeudev(args->dev >> 8, args->dev & 0xFF), 0);
339         if (dev != NULL && vfinddev(dev, VCHR, &vp)) {
340                 if (vp->v_mount == NULL) {
341                         vrele(vp);
342                         error = EINVAL;
343                         goto done;
344                 }
345                 stat = &(vp->v_mount->mnt_stat);
346                 error = VFS_STATFS(vp->v_mount, stat, td->td_ucred);
347                 vrele(vp);
348                 if (error == 0) {
349                         lu.f_tfree = stat->f_bfree;
350                         lu.f_tinode = stat->f_ffree;
351                 }
352         } else {
353                 error = 0;
354         }
355 done:
356         rel_mplock();
357         if (error == 0)
358                 error = copyout(&lu, args->ubuf, sizeof(lu));
359         return (error);
360 }
361
362 #if defined(__i386__)
363
364 static int
365 stat64_copyout(struct stat *buf, void *ubuf)
366 {
367         struct l_stat64 lbuf;
368         int error;
369
370         bzero(&lbuf, sizeof(lbuf));
371         lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
372         lbuf.st_ino = INO64TO32(buf->st_ino);
373         lbuf.st_mode = buf->st_mode;
374         lbuf.st_nlink = buf->st_nlink;
375         lbuf.st_uid = buf->st_uid;
376         lbuf.st_gid = buf->st_gid;
377         lbuf.st_rdev = buf->st_rdev;
378         lbuf.st_size = buf->st_size;
379         lbuf.st_atime = buf->st_atime;
380         lbuf.st_mtime = buf->st_mtime;
381         lbuf.st_ctime = buf->st_ctime;
382         lbuf.st_blksize = buf->st_blksize;
383         lbuf.st_blocks = buf->st_blocks;
384
385         /*
386          * The __st_ino field makes all the difference. In the Linux kernel
387          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
388          * but without the assignment to __st_ino the runtime linker refuses
389          * to mmap(2) any shared libraries. I guess it's broken alright :-)
390          */
391         lbuf.__st_ino = INO64TO32(buf->st_ino);
392
393         error = copyout(&lbuf, ubuf, sizeof(lbuf));
394         return (error);
395 }
396
397 /*
398  * MPALMOSTSAFE
399  */
400 int
401 sys_linux_stat64(struct linux_stat64_args *args)
402 {
403         struct nlookupdata nd;
404         struct stat buf;
405         char *path;
406         int error;
407
408         error = linux_copyin_path(args->filename, &path, LINUX_PATH_EXISTS);
409         if (error)
410                 return (error);
411 #ifdef DEBUG
412         if (ldebug(stat64))
413                 kprintf(ARGS(stat64, "%s, *"), path);
414 #endif
415         get_mplock();
416         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
417         if (error == 0) {
418                 error = kern_stat(&nd, &buf);
419                 nlookup_done(&nd);
420         }
421         rel_mplock();
422         if (error == 0)
423                 error = stat64_copyout(&buf, args->statbuf);
424         linux_free_path(&path);
425         return (error);
426 }
427
428 /*
429  * MPALMOSTSAFE
430  */
431 int
432 sys_linux_lstat64(struct linux_lstat64_args *args)
433 {
434         struct nlookupdata nd;
435         struct stat sb;
436         char *path;
437         int error;
438
439         error = linux_copyin_path(args->filename, &path, LINUX_PATH_EXISTS);
440         if (error)
441                 return (error);
442 #ifdef DEBUG
443         if (ldebug(lstat64))
444                 kprintf(ARGS(lstat64, "%s, *"), path);
445 #endif
446         get_mplock();
447         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
448         if (error == 0) {
449                 error = kern_stat(&nd, &sb);
450                 nlookup_done(&nd);
451         }
452         rel_mplock();
453         if (error == 0)
454                 error = stat64_copyout(&sb, args->statbuf);
455         linux_free_path(&path);
456         return (error);
457 }
458
459 /*
460  * MPALMOSTSAFE
461  */
462 int
463 sys_linux_fstat64(struct linux_fstat64_args *args)
464 {
465         struct stat buf;
466         int error;
467
468 #ifdef DEBUG
469         if (ldebug(fstat64))
470                 kprintf(ARGS(fstat64, "%d, *"), args->fd);
471 #endif
472         get_mplock();
473         error = kern_fstat(args->fd, &buf);
474         rel_mplock();
475
476         if (error == 0)
477                 error = stat64_copyout(&buf, args->statbuf);
478         return (error);
479 }
480
481 int
482 sys_linux_fstatat64(struct linux_fstatat64_args *args)
483 {
484         CACHE_MPLOCK_DECLARE;
485         struct nlookupdata nd;
486         struct file *fp;
487         struct stat st;
488         char *path;
489         int error, flags, dfd;
490
491         if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
492                 return (EINVAL);
493
494         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
495         if (error)
496                 return (error);
497 #ifdef DEBUG
498         if (ldebug(fstatat64))
499                 kprintf(ARGS(fstatat64, "%s"), path);
500 #endif
501         kprintf(ARGS(fstatat64, "%s"), path);
502         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
503         flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
504
505         CACHE_GETMPLOCK1();
506         error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
507         if (error == 0) {
508                 error = kern_stat(&nd, &st);
509                 if (error == 0)
510                         error = stat64_copyout(&st, args->statbuf);
511         }
512         nlookup_done_at(&nd, fp);
513         CACHE_RELMPLOCK();
514         linux_free_path(&path);
515         return (error);
516 }
517
518
519 #endif /* __i386__ */