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