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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/8.3/sys/compat/linux/linux_stats.c 219666 2011-03-15 13:19:26Z netchild $");
31
32 #include "opt_compat.h"
33
34 #include <sys/param.h>
35 #include <sys/dirent.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/proc.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/stat.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/systm.h>
45 #include <sys/tty.h>
46 #include <sys/vnode.h>
47 #include <sys/conf.h>
48 #include <sys/fcntl.h>
49
50 #ifdef COMPAT_LINUX32
51 #include <machine/../linux32/linux.h>
52 #include <machine/../linux32/linux32_proto.h>
53 #else
54 #include <machine/../linux/linux.h>
55 #include <machine/../linux/linux_proto.h>
56 #endif
57
58 #include <compat/linux/linux_util.h>
59 #include <compat/linux/linux_file.h>
60
61 #include <security/mac/mac_framework.h>
62
63 #define LINUX_SHMFS_MAGIC 0x01021994
64
65 static void
66 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
67 {
68 int major, minor;
69
70 if (vp->v_type == VCHR && vp->v_rdev != NULL &&
71 linux_driver_get_major_minor(vp->v_rdev->si_name,
72 &major, &minor) == 0) {
73 sb->st_rdev = (major << 8 | minor);
74 }
75 }
76
77 static int
78 linux_kern_statat(struct thread *td, int flag, int fd, char *path,
79 enum uio_seg pathseg, struct stat *sbp)
80 {
81
82 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp,
83 translate_vnhook_major_minor));
84 }
85
86 static int
87 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
88 struct stat *sbp)
89 {
90
91 return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
92 }
93
94 static int
95 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
96 struct stat *sbp)
97 {
98
99 return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
100 pathseg, sbp));
101 }
102
103 /*
104 * XXX: This was removed from newstat_copyout(), and almost identical
105 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced
106 * XXX: with something that does lookup and locking properly.
107 * XXX: When somebody fixes this: please try to avoid duplicating it.
108 */
109 #if 0
110 static void
111 disk_foo(struct somestat *tbuf)
112 {
113 struct cdevsw *cdevsw;
114 struct cdev *dev;
115
116 /* Lie about disk drives which are character devices
117 * in FreeBSD but block devices under Linux.
118 */
119 if (S_ISCHR(tbuf.st_mode) &&
120 (dev = findcdev(buf->st_rdev)) != NULL) {
121 cdevsw = dev_refthread(dev);
122 if (cdevsw != NULL) {
123 if (cdevsw->d_flags & D_DISK) {
124 tbuf.st_mode &= ~S_IFMT;
125 tbuf.st_mode |= S_IFBLK;
126
127 /* XXX this may not be quite right */
128 /* Map major number to 0 */
129 tbuf.st_dev = minor(buf->st_dev) & 0xf;
130 tbuf.st_rdev = buf->st_rdev & 0xff;
131 }
132 dev_relthread(dev);
133 }
134 }
135
136 }
137 #endif
138
139 static void
140 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
141 {
142 struct file *fp;
143 struct vnode *vp;
144 int major, minor;
145
146 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
147 fget(td, fd, &fp) != 0)
148 return;
149 vp = fp->f_vnode;
150 if (vp != NULL && vp->v_rdev != NULL &&
151 linux_driver_get_major_minor(vp->v_rdev->si_name,
152 &major, &minor) == 0) {
153 buf->st_rdev = (major << 8 | minor);
154 } else if (fp->f_type == DTYPE_PTS) {
155 struct tty *tp = fp->f_data;
156
157 /* Convert the numbers for the slave device. */
158 if (linux_driver_get_major_minor(tp->t_dev->si_name,
159 &major, &minor) == 0) {
160 buf->st_rdev = (major << 8 | minor);
161 }
162 }
163 fdrop(fp, td);
164 }
165
166 static int
167 newstat_copyout(struct stat *buf, void *ubuf)
168 {
169 struct l_newstat tbuf;
170
171 bzero(&tbuf, sizeof(tbuf));
172 tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
173 tbuf.st_ino = buf->st_ino;
174 tbuf.st_mode = buf->st_mode;
175 tbuf.st_nlink = buf->st_nlink;
176 tbuf.st_uid = buf->st_uid;
177 tbuf.st_gid = buf->st_gid;
178 tbuf.st_rdev = buf->st_rdev;
179 tbuf.st_size = buf->st_size;
180 tbuf.st_atime = buf->st_atime;
181 tbuf.st_mtime = buf->st_mtime;
182 tbuf.st_ctime = buf->st_ctime;
183 tbuf.st_blksize = buf->st_blksize;
184 tbuf.st_blocks = buf->st_blocks;
185
186 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
187 }
188
189 int
190 linux_newstat(struct thread *td, struct linux_newstat_args *args)
191 {
192 struct stat buf;
193 char *path;
194 int error;
195
196 LCONVPATHEXIST(td, args->path, &path);
197
198 #ifdef DEBUG
199 if (ldebug(newstat))
200 printf(ARGS(newstat, "%s, *"), path);
201 #endif
202
203 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
204 LFREEPATH(path);
205 if (error)
206 return (error);
207 return (newstat_copyout(&buf, args->buf));
208 }
209
210 int
211 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
212 {
213 struct stat sb;
214 char *path;
215 int error;
216
217 LCONVPATHEXIST(td, args->path, &path);
218
219 #ifdef DEBUG
220 if (ldebug(newlstat))
221 printf(ARGS(newlstat, "%s, *"), path);
222 #endif
223
224 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
225 LFREEPATH(path);
226 if (error)
227 return (error);
228 return (newstat_copyout(&sb, args->buf));
229 }
230
231 int
232 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
233 {
234 struct stat buf;
235 int error;
236
237 #ifdef DEBUG
238 if (ldebug(newfstat))
239 printf(ARGS(newfstat, "%d, *"), args->fd);
240 #endif
241
242 error = kern_fstat(td, args->fd, &buf);
243 translate_fd_major_minor(td, args->fd, &buf);
244 if (!error)
245 error = newstat_copyout(&buf, args->buf);
246
247 return (error);
248 }
249
250 static int
251 stat_copyout(struct stat *buf, void *ubuf)
252 {
253 struct l_stat lbuf;
254
255 bzero(&lbuf, sizeof(lbuf));
256 lbuf.st_dev = buf->st_dev;
257 lbuf.st_ino = buf->st_ino;
258 lbuf.st_mode = buf->st_mode;
259 lbuf.st_nlink = buf->st_nlink;
260 lbuf.st_uid = buf->st_uid;
261 lbuf.st_gid = buf->st_gid;
262 lbuf.st_rdev = buf->st_rdev;
263 if (buf->st_size < (quad_t)1 << 32)
264 lbuf.st_size = buf->st_size;
265 else
266 lbuf.st_size = -2;
267 lbuf.st_atime = buf->st_atime;
268 lbuf.st_mtime = buf->st_mtime;
269 lbuf.st_ctime = buf->st_ctime;
270 lbuf.st_blksize = buf->st_blksize;
271 lbuf.st_blocks = buf->st_blocks;
272 lbuf.st_flags = buf->st_flags;
273 lbuf.st_gen = buf->st_gen;
274
275 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
276 }
277
278 int
279 linux_stat(struct thread *td, struct linux_stat_args *args)
280 {
281 struct stat buf;
282 char *path;
283 int error;
284
285 LCONVPATHEXIST(td, args->path, &path);
286
287 #ifdef DEBUG
288 if (ldebug(stat))
289 printf(ARGS(stat, "%s, *"), path);
290 #endif
291 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
292 if (error) {
293 LFREEPATH(path);
294 return (error);
295 }
296 LFREEPATH(path);
297 return(stat_copyout(&buf, args->up));
298 }
299
300 int
301 linux_lstat(struct thread *td, struct linux_lstat_args *args)
302 {
303 struct stat buf;
304 char *path;
305 int error;
306
307 LCONVPATHEXIST(td, args->path, &path);
308
309 #ifdef DEBUG
310 if (ldebug(lstat))
311 printf(ARGS(lstat, "%s, *"), path);
312 #endif
313 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
314 if (error) {
315 LFREEPATH(path);
316 return (error);
317 }
318 LFREEPATH(path);
319 return(stat_copyout(&buf, args->up));
320 }
321
322 /* XXX - All fields of type l_int are defined as l_long on i386 */
323 struct l_statfs {
324 l_int f_type;
325 l_int f_bsize;
326 l_int f_blocks;
327 l_int f_bfree;
328 l_int f_bavail;
329 l_int f_files;
330 l_int f_ffree;
331 l_fsid_t f_fsid;
332 l_int f_namelen;
333 l_int f_spare[6];
334 };
335
336 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
337 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
338 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
339 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
340 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
341 #define LINUX_NCP_SUPER_MAGIC 0x564cL
342 #define LINUX_NFS_SUPER_MAGIC 0x6969L
343 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
344 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
345 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
346 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
347
348 static long
349 bsd_to_linux_ftype(const char *fstypename)
350 {
351 int i;
352 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
353 {"ufs", LINUX_UFS_SUPER_MAGIC},
354 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
355 {"nfs", LINUX_NFS_SUPER_MAGIC},
356 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
357 {"procfs", LINUX_PROC_SUPER_MAGIC},
358 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
359 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
360 {"nwfs", LINUX_NCP_SUPER_MAGIC},
361 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
362 {"coda", LINUX_CODA_SUPER_MAGIC},
363 {"devfs", LINUX_DEVFS_SUPER_MAGIC},
364 {NULL, 0L}};
365
366 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
367 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
368 return (b2l_tbl[i].linux_type);
369
370 return (0L);
371 }
372
373 static void
374 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
375 {
376
377 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
378 linux_statfs->f_bsize = bsd_statfs->f_bsize;
379 linux_statfs->f_blocks = bsd_statfs->f_blocks;
380 linux_statfs->f_bfree = bsd_statfs->f_bfree;
381 linux_statfs->f_bavail = bsd_statfs->f_bavail;
382 linux_statfs->f_ffree = bsd_statfs->f_ffree;
383 linux_statfs->f_files = bsd_statfs->f_files;
384 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
385 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
386 linux_statfs->f_namelen = MAXNAMLEN;
387 }
388
389 int
390 linux_statfs(struct thread *td, struct linux_statfs_args *args)
391 {
392 struct l_statfs linux_statfs;
393 struct statfs bsd_statfs;
394 char *path;
395 int error, dev_shm;
396
397 LCONVPATHEXIST(td, args->path, &path);
398
399 #ifdef DEBUG
400 if (ldebug(statfs))
401 printf(ARGS(statfs, "%s, *"), path);
402 #endif
403 dev_shm = 0;
404 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
405 if (strncmp(path, "/dev/shm", sizeof("/dev/shm") - 1) == 0)
406 dev_shm = (path[8] == '\0'
407 || (path[8] == '/' && path[9] == '\0'));
408 LFREEPATH(path);
409 if (error)
410 return (error);
411 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
412 if (dev_shm)
413 linux_statfs.f_type = LINUX_SHMFS_MAGIC;
414 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
415 }
416
417 static void
418 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
419 {
420
421 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
422 linux_statfs->f_bsize = bsd_statfs->f_bsize;
423 linux_statfs->f_blocks = bsd_statfs->f_blocks;
424 linux_statfs->f_bfree = bsd_statfs->f_bfree;
425 linux_statfs->f_bavail = bsd_statfs->f_bavail;
426 linux_statfs->f_ffree = bsd_statfs->f_ffree;
427 linux_statfs->f_files = bsd_statfs->f_files;
428 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
429 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
430 linux_statfs->f_namelen = MAXNAMLEN;
431 }
432
433 int
434 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
435 {
436 struct l_statfs64 linux_statfs;
437 struct statfs bsd_statfs;
438 char *path;
439 int error;
440
441 if (args->bufsize != sizeof(struct l_statfs64))
442 return EINVAL;
443
444 LCONVPATHEXIST(td, args->path, &path);
445
446 #ifdef DEBUG
447 if (ldebug(statfs64))
448 printf(ARGS(statfs64, "%s, *"), path);
449 #endif
450 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
451 LFREEPATH(path);
452 if (error)
453 return (error);
454 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
455 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
456 }
457
458 int
459 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
460 {
461 struct l_statfs linux_statfs;
462 struct statfs bsd_statfs;
463 int error;
464
465 #ifdef DEBUG
466 if (ldebug(fstatfs))
467 printf(ARGS(fstatfs, "%d, *"), args->fd);
468 #endif
469 error = kern_fstatfs(td, args->fd, &bsd_statfs);
470 if (error)
471 return error;
472 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
473 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
474 }
475
476 struct l_ustat
477 {
478 l_daddr_t f_tfree;
479 l_ino_t f_tinode;
480 char f_fname[6];
481 char f_fpack[6];
482 };
483
484 int
485 linux_ustat(struct thread *td, struct linux_ustat_args *args)
486 {
487 #ifdef DEBUG
488 if (ldebug(ustat))
489 printf(ARGS(ustat, "%d, *"), args->dev);
490 #endif
491
492 return (EOPNOTSUPP);
493 }
494
495 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
496
497 static int
498 stat64_copyout(struct stat *buf, void *ubuf)
499 {
500 struct l_stat64 lbuf;
501
502 bzero(&lbuf, sizeof(lbuf));
503 lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
504 lbuf.st_ino = buf->st_ino;
505 lbuf.st_mode = buf->st_mode;
506 lbuf.st_nlink = buf->st_nlink;
507 lbuf.st_uid = buf->st_uid;
508 lbuf.st_gid = buf->st_gid;
509 lbuf.st_rdev = buf->st_rdev;
510 lbuf.st_size = buf->st_size;
511 lbuf.st_atime = buf->st_atime;
512 lbuf.st_mtime = buf->st_mtime;
513 lbuf.st_ctime = buf->st_ctime;
514 lbuf.st_blksize = buf->st_blksize;
515 lbuf.st_blocks = buf->st_blocks;
516
517 /*
518 * The __st_ino field makes all the difference. In the Linux kernel
519 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
520 * but without the assignment to __st_ino the runtime linker refuses
521 * to mmap(2) any shared libraries. I guess it's broken alright :-)
522 */
523 lbuf.__st_ino = buf->st_ino;
524
525 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
526 }
527
528 int
529 linux_stat64(struct thread *td, struct linux_stat64_args *args)
530 {
531 struct stat buf;
532 char *filename;
533 int error;
534
535 LCONVPATHEXIST(td, args->filename, &filename);
536
537 #ifdef DEBUG
538 if (ldebug(stat64))
539 printf(ARGS(stat64, "%s, *"), filename);
540 #endif
541
542 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
543 LFREEPATH(filename);
544 if (error)
545 return (error);
546 return (stat64_copyout(&buf, args->statbuf));
547 }
548
549 int
550 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
551 {
552 struct stat sb;
553 char *filename;
554 int error;
555
556 LCONVPATHEXIST(td, args->filename, &filename);
557
558 #ifdef DEBUG
559 if (ldebug(lstat64))
560 printf(ARGS(lstat64, "%s, *"), args->filename);
561 #endif
562
563 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
564 LFREEPATH(filename);
565 if (error)
566 return (error);
567 return (stat64_copyout(&sb, args->statbuf));
568 }
569
570 int
571 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
572 {
573 struct stat buf;
574 int error;
575
576 #ifdef DEBUG
577 if (ldebug(fstat64))
578 printf(ARGS(fstat64, "%d, *"), args->fd);
579 #endif
580
581 error = kern_fstat(td, args->fd, &buf);
582 translate_fd_major_minor(td, args->fd, &buf);
583 if (!error)
584 error = stat64_copyout(&buf, args->statbuf);
585
586 return (error);
587 }
588
589 int
590 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
591 {
592 char *path;
593 int error, dfd, flag;
594 struct stat buf;
595
596 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
597 return (EINVAL);
598 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
599 AT_SYMLINK_NOFOLLOW : 0;
600
601 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
602 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
603
604 #ifdef DEBUG
605 if (ldebug(fstatat64))
606 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
607 #endif
608
609 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
610 if (!error)
611 error = stat64_copyout(&buf, args->statbuf);
612 LFREEPATH(path);
613
614 return (error);
615 }
616
617 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
Cache object: 4a14251ab36dd50576505119c1c9d10d
|