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