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