xref: /netbsd-src/sys/compat/linux/common/linux_file.c (revision 4391d5e9d4f291db41e3b3ba26a01b5e51364aae)
1 /*	$NetBSD: linux_file.c,v 1.106 2013/11/18 01:32:52 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Functions in multiarch:
34  *	linux_sys_llseek	: linux_llseek.c
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.106 2013/11/18 01:32:52 chs Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/file.h>
45 #include <sys/fcntl.h>
46 #include <sys/stat.h>
47 #include <sys/filedesc.h>
48 #include <sys/ioctl.h>
49 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/malloc.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/tty.h>
55 #include <sys/socketvar.h>
56 #include <sys/conf.h>
57 #include <sys/pipe.h>
58 
59 #include <sys/syscallargs.h>
60 #include <sys/vfs_syscalls.h>
61 
62 #include <compat/linux/common/linux_types.h>
63 #include <compat/linux/common/linux_signal.h>
64 #include <compat/linux/common/linux_fcntl.h>
65 #include <compat/linux/common/linux_util.h>
66 #include <compat/linux/common/linux_machdep.h>
67 #include <compat/linux/common/linux_ipc.h>
68 #include <compat/linux/common/linux_sem.h>
69 
70 #include <compat/linux/linux_syscallargs.h>
71 
72 static int linux_to_bsd_ioflags(int);
73 static int bsd_to_linux_ioflags(int);
74 #ifndef __amd64__
75 static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
76 #endif
77 
78 conv_linux_flock(linux, flock)
79 
80 /*
81  * Some file-related calls are handled here. The usual flag conversion
82  * an structure conversion is done, and alternate emul path searching.
83  */
84 
85 /*
86  * The next two functions convert between the Linux and NetBSD values
87  * of the flags used in open(2) and fcntl(2).
88  */
89 static int
90 linux_to_bsd_ioflags(int lflags)
91 {
92 	int res = 0;
93 
94 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
95 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
96 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
97 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
98 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
99 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
100 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
101 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
102 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
103 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
104 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
105 	res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECTORY, O_DIRECTORY);
106 	res |= cvtto_bsd_mask(lflags, LINUX_O_CLOEXEC, O_CLOEXEC);
107 
108 	return res;
109 }
110 
111 static int
112 bsd_to_linux_ioflags(int bflags)
113 {
114 	int res = 0;
115 
116 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
117 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
118 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
119 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
120 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
121 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
122 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
123 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
124 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
125 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
126 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
127 	res |= cvtto_linux_mask(bflags, O_DIRECTORY, LINUX_O_DIRECTORY);
128 	res |= cvtto_linux_mask(bflags, O_CLOEXEC, LINUX_O_CLOEXEC);
129 
130 	return res;
131 }
132 
133 /*
134  * creat(2) is an obsolete function, but it's present as a Linux
135  * system call, so let's deal with it.
136  *
137  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
138  * in syscalls.master anyway so this doesn't have to be special cased.
139  *
140  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
141  */
142 int
143 linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
144 {
145 	/* {
146 		syscallarg(const char *) path;
147 		syscallarg(int) mode;
148 	} */
149 	struct sys_open_args oa;
150 
151 	SCARG(&oa, path) = SCARG(uap, path);
152 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
153 	SCARG(&oa, mode) = SCARG(uap, mode);
154 
155 	return sys_open(l, &oa, retval);
156 }
157 
158 static void
159 linux_open_ctty(struct lwp *l, int flags, int fd)
160 {
161 	struct proc *p = l->l_proc;
162 
163 	/*
164 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
165 	 * If we are a session leader, and we don't have a controlling
166 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
167 	 * this the controlling terminal.
168 	 */
169         if (!(flags & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
170                 file_t *fp;
171 
172 		fp = fd_getfile(fd);
173 
174                 /* ignore any error, just give it a try */
175                 if (fp != NULL) {
176 			if (fp->f_type == DTYPE_VNODE) {
177 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
178 			}
179 			fd_putfile(fd);
180 		}
181         }
182 }
183 
184 /*
185  * open(2). Take care of the different flag values, and let the
186  * NetBSD syscall do the real work. See if this operation
187  * gives the current process a controlling terminal.
188  * (XXX is this necessary?)
189  */
190 int
191 linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
192 {
193 	/* {
194 		syscallarg(const char *) path;
195 		syscallarg(int) flags;
196 		syscallarg(int) mode;
197 	} */
198 	int error, fl;
199 	struct sys_open_args boa;
200 
201 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
202 
203 	SCARG(&boa, path) = SCARG(uap, path);
204 	SCARG(&boa, flags) = fl;
205 	SCARG(&boa, mode) = SCARG(uap, mode);
206 
207 	if ((error = sys_open(l, &boa, retval)))
208 		return error;
209 
210 	linux_open_ctty(l, fl, *retval);
211 	return 0;
212 }
213 
214 int
215 linux_sys_openat(struct lwp *l, const struct linux_sys_openat_args *uap, register_t *retval)
216 {
217 	/* {
218 		syscallarg(const char *) path;
219 		syscallarg(int) flags;
220 		syscallarg(int) mode;
221 	} */
222 	int error, fl;
223 	struct sys_openat_args boa;
224 
225 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
226 
227 	SCARG(&boa, fd) = SCARG(uap, fd);
228 	SCARG(&boa, path) = SCARG(uap, path);
229 	SCARG(&boa, oflags) = fl;
230 	SCARG(&boa, mode) = SCARG(uap, mode);
231 
232 	if ((error = sys_openat(l, &boa, retval)))
233 		return error;
234 
235 	linux_open_ctty(l, fl, *retval);
236 	return 0;
237 }
238 
239 /*
240  * Most actions in the fcntl() call are straightforward; simply
241  * pass control to the NetBSD system call. A few commands need
242  * conversions after the actual system call has done its work,
243  * because the flag values and lock structure are different.
244  */
245 int
246 linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
247 {
248 	/* {
249 		syscallarg(int) fd;
250 		syscallarg(int) cmd;
251 		syscallarg(void *) arg;
252 	} */
253 	struct proc *p = l->l_proc;
254 	int fd, cmd, error;
255 	u_long val;
256 	void *arg;
257 	struct sys_fcntl_args fca;
258 	file_t *fp;
259 	struct vnode *vp;
260 	struct vattr va;
261 	long pgid;
262 	struct pgrp *pgrp;
263 	struct tty *tp;
264 
265 	fd = SCARG(uap, fd);
266 	cmd = SCARG(uap, cmd);
267 	arg = SCARG(uap, arg);
268 
269 	switch (cmd) {
270 
271 	case LINUX_F_DUPFD:
272 		cmd = F_DUPFD;
273 		break;
274 
275 	case LINUX_F_GETFD:
276 		cmd = F_GETFD;
277 		break;
278 
279 	case LINUX_F_SETFD:
280 		cmd = F_SETFD;
281 		break;
282 
283 	case LINUX_F_GETFL:
284 		SCARG(&fca, fd) = fd;
285 		SCARG(&fca, cmd) = F_GETFL;
286 		SCARG(&fca, arg) = arg;
287 		if ((error = sys_fcntl(l, &fca, retval)))
288 			return error;
289 		retval[0] = bsd_to_linux_ioflags(retval[0]);
290 		return 0;
291 
292 	case LINUX_F_SETFL: {
293 		file_t	*fp1 = NULL;
294 
295 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
296 		/*
297 		 * Linux seems to have same semantics for sending SIGIO to the
298 		 * read side of socket, but slightly different semantics
299 		 * for SIGIO to the write side.  Rather than sending the SIGIO
300 		 * every time it's possible to write (directly) more data, it
301 		 * only sends SIGIO if last write(2) failed due to insufficient
302 		 * memory to hold the data. This is compatible enough
303 		 * with NetBSD semantics to not do anything about the
304 		 * difference.
305 		 *
306 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
307 		 * ones and DTYPE_PIPE ones. For these, we don't set
308 		 * the underlying flags (we don't pass O_ASYNC flag down
309 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
310 		 * so that F_GETFL would report the ASYNC i/o is on.
311 		 */
312 		if (val & O_ASYNC) {
313 			if (((fp1 = fd_getfile(fd)) == NULL))
314 			    return (EBADF);
315 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
316 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
317 			    || (fp1->f_type == DTYPE_PIPE))
318 				val &= ~O_ASYNC;
319 			else {
320 				/* not a pipe, do not modify anything */
321 				fd_putfile(fd);
322 				fp1 = NULL;
323 			}
324 		}
325 
326 		SCARG(&fca, fd) = fd;
327 		SCARG(&fca, cmd) = F_SETFL;
328 		SCARG(&fca, arg) = (void *) val;
329 
330 		error = sys_fcntl(l, &fca, retval);
331 
332 		/* Now set the FASYNC flag for pipes */
333 		if (fp1) {
334 			if (!error) {
335 				mutex_enter(&fp1->f_lock);
336 				fp1->f_flag |= FASYNC;
337 				mutex_exit(&fp1->f_lock);
338 			}
339 			fd_putfile(fd);
340 		}
341 
342 		return (error);
343 	    }
344 
345 	case LINUX_F_GETLK:
346 		do_linux_getlk(fd, cmd, arg, linux, flock);
347 
348 	case LINUX_F_SETLK:
349 	case LINUX_F_SETLKW:
350 		do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
351 
352 	case LINUX_F_SETOWN:
353 	case LINUX_F_GETOWN:
354 		/*
355 		 * We need to route fcntl() for tty descriptors around normal
356 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
357 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
358 		 * this is not a problem.
359 		 */
360 		if ((fp = fd_getfile(fd)) == NULL)
361 			return EBADF;
362 
363 		/* Check it's a character device vnode */
364 		if (fp->f_type != DTYPE_VNODE
365 		    || (vp = (struct vnode *)fp->f_data) == NULL
366 		    || vp->v_type != VCHR) {
367 			fd_putfile(fd);
368 
369 	    not_tty:
370 			/* Not a tty, proceed with common fcntl() */
371 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
372 			break;
373 		}
374 
375 		vn_lock(vp, LK_SHARED | LK_RETRY);
376 		error = VOP_GETATTR(vp, &va, l->l_cred);
377 		VOP_UNLOCK(vp);
378 
379 		fd_putfile(fd);
380 
381 		if (error)
382 			return error;
383 
384 		if ((tp = cdev_tty(va.va_rdev)) == NULL)
385 			goto not_tty;
386 
387 		/* set tty pg_id appropriately */
388 		mutex_enter(proc_lock);
389 		if (cmd == LINUX_F_GETOWN) {
390 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
391 			mutex_exit(proc_lock);
392 			return 0;
393 		}
394 		if ((long)arg <= 0) {
395 			pgid = -(long)arg;
396 		} else {
397 			struct proc *p1 = proc_find((long)arg);
398 			if (p1 == NULL) {
399 				mutex_exit(proc_lock);
400 				return (ESRCH);
401 			}
402 			pgid = (long)p1->p_pgrp->pg_id;
403 		}
404 		pgrp = pgrp_find(pgid);
405 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
406 			mutex_exit(proc_lock);
407 			return EPERM;
408 		}
409 		tp->t_pgrp = pgrp;
410 		mutex_exit(proc_lock);
411 		return 0;
412 
413 	default:
414 		return EOPNOTSUPP;
415 	}
416 
417 	SCARG(&fca, fd) = fd;
418 	SCARG(&fca, cmd) = cmd;
419 	SCARG(&fca, arg) = arg;
420 
421 	return sys_fcntl(l, &fca, retval);
422 }
423 
424 #if !defined(__amd64__)
425 /*
426  * Convert a NetBSD stat structure to a Linux stat structure.
427  * Only the order of the fields and the padding in the structure
428  * is different. linux_fakedev is a machine-dependent function
429  * which optionally converts device driver major/minor numbers
430  * (XXX horrible, but what can you do against code that compares
431  * things against constant major device numbers? sigh)
432  */
433 static void
434 bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
435 {
436 
437 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
438 	lsp->lst_ino     = bsp->st_ino;
439 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
440 	if (bsp->st_nlink >= (1 << 15))
441 		lsp->lst_nlink = (1 << 15) - 1;
442 	else
443 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
444 	lsp->lst_uid     = bsp->st_uid;
445 	lsp->lst_gid     = bsp->st_gid;
446 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
447 	lsp->lst_size    = bsp->st_size;
448 	lsp->lst_blksize = bsp->st_blksize;
449 	lsp->lst_blocks  = bsp->st_blocks;
450 	lsp->lst_atime   = bsp->st_atime;
451 	lsp->lst_mtime   = bsp->st_mtime;
452 	lsp->lst_ctime   = bsp->st_ctime;
453 #ifdef LINUX_STAT_HAS_NSEC
454 	lsp->lst_atime_nsec   = bsp->st_atimensec;
455 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
456 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
457 #endif
458 }
459 
460 /*
461  * The stat functions below are plain sailing. stat and lstat are handled
462  * by one function to avoid code duplication.
463  */
464 int
465 linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
466 {
467 	/* {
468 		syscallarg(int) fd;
469 		syscallarg(linux_stat *) sp;
470 	} */
471 	struct linux_stat tmplst;
472 	struct stat tmpst;
473 	int error;
474 
475 	error = do_sys_fstat(SCARG(uap, fd), &tmpst);
476 	if (error != 0)
477 		return error;
478 	bsd_to_linux_stat(&tmpst, &tmplst);
479 
480 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
481 }
482 
483 static int
484 linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval, int flags)
485 {
486 	struct linux_stat tmplst;
487 	struct stat tmpst;
488 	int error;
489 
490 	error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
491 	if (error != 0)
492 		return error;
493 
494 	bsd_to_linux_stat(&tmpst, &tmplst);
495 
496 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
497 }
498 
499 int
500 linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
501 {
502 	/* {
503 		syscallarg(const char *) path;
504 		syscallarg(struct linux_stat *) sp;
505 	} */
506 
507 	return linux_stat1(uap, retval, FOLLOW);
508 }
509 
510 /* Note: this is "newlstat" in the Linux sources */
511 /*	(we don't bother with the old lstat currently) */
512 int
513 linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
514 {
515 	/* {
516 		syscallarg(const char *) path;
517 		syscallarg(struct linux_stat *) sp;
518 	} */
519 
520 	return linux_stat1((const void *)uap, retval, NOFOLLOW);
521 }
522 #endif /* !__amd64__ */
523 
524 /*
525  * The following syscalls are mostly here because of the alternate path check.
526  */
527 
528 int
529 linux_sys_linkat(struct lwp *l, const struct linux_sys_linkat_args *uap, register_t *retval)
530 {
531 	/* {
532 		syscallarg(int) fd1;
533 		syscallarg(const char *) name1;
534 		syscallarg(int) fd2;
535 		syscallarg(const char *) name2;
536 		syscallarg(int) flags;
537 	} */
538 	int fd1 = SCARG(uap, fd1);
539 	const char *name1 = SCARG(uap, name1);
540 	int fd2 = SCARG(uap, fd2);
541 	const char *name2 = SCARG(uap, name2);
542 	int follow;
543 
544 	follow = SCARG(uap, flags) & LINUX_AT_SYMLINK_FOLLOW;
545 
546 	return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
547 }
548 
549 static int
550 linux_unlink_dircheck(const char *path)
551 {
552 	struct nameidata nd;
553 	struct pathbuf *pb;
554 	int error;
555 
556 	/*
557 	 * Linux returns EISDIR if unlink(2) is called on a directory.
558 	 * We return EPERM in such cases. To emulate correct behaviour,
559 	 * check if the path points to directory and return EISDIR if this
560 	 * is the case.
561 	 *
562 	 * XXX this should really not copy in the path buffer twice...
563 	 */
564 	error = pathbuf_copyin(path, &pb);
565 	if (error) {
566 		return error;
567 	}
568 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
569 	if (namei(&nd) == 0) {
570 		struct stat sb;
571 
572 		if (vn_stat(nd.ni_vp, &sb) == 0
573 		    && S_ISDIR(sb.st_mode))
574 			error = EISDIR;
575 
576 		vput(nd.ni_vp);
577 	}
578 	pathbuf_destroy(pb);
579 	return error ? error : EPERM;
580 }
581 
582 int
583 linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
584 {
585 	/* {
586 		syscallarg(const char *) path;
587 	} */
588 	int error;
589 
590 	error = sys_unlink(l, (const void *)uap, retval);
591 	if (error == EPERM)
592 		error = linux_unlink_dircheck(SCARG(uap, path));
593 
594 	return error;
595 }
596 
597 int
598 linux_sys_unlinkat(struct lwp *l, const struct linux_sys_unlinkat_args *uap, register_t *retval)
599 {
600 	/* {
601 		syscallarg(int) fd;
602 		syscallarg(const char *) path;
603 		syscallarg(int) flag;
604 	} */
605 	struct sys_unlinkat_args ua;
606 	int error;
607 
608 	SCARG(&ua, fd) = SCARG(uap, fd);
609 	SCARG(&ua, path) = SCARG(uap, path);
610 	SCARG(&ua, flag) = linux_to_bsd_atflags(SCARG(uap, flag));
611 
612 	error = sys_unlinkat(l, &ua, retval);
613 	if (error == EPERM)
614 		error = linux_unlink_dircheck(SCARG(uap, path));
615 
616 	return error;
617 }
618 
619 int
620 linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
621 {
622 	/* {
623 		syscallarg(const char *) path;
624 		syscallarg(linux_umode_t) mode;
625 		syscallarg(unsigned) dev;
626 	} */
627 	struct linux_sys_mknodat_args ua;
628 
629 	SCARG(&ua, fd) = LINUX_AT_FDCWD;
630 	SCARG(&ua, path) = SCARG(uap, path);
631 	SCARG(&ua, mode) = SCARG(uap, mode);
632 	SCARG(&ua, dev) = SCARG(uap, dev);
633 
634 	return linux_sys_mknodat(l, &ua, retval);
635 }
636 
637 int
638 linux_sys_mknodat(struct lwp *l, const struct linux_sys_mknodat_args *uap, register_t *retval)
639 {
640 	/* {
641 		syscallarg(int) fd;
642 		syscallarg(const char *) path;
643 		syscallarg(linux_umode_t) mode;
644 		syscallarg(unsigned) dev;
645 	} */
646 
647 	/*
648 	 * BSD handles FIFOs separately
649 	 */
650 	if (S_ISFIFO(SCARG(uap, mode))) {
651 		struct sys_mkfifoat_args bma;
652 
653 		SCARG(&bma, fd) = SCARG(uap, fd);
654 		SCARG(&bma, path) = SCARG(uap, path);
655 		SCARG(&bma, mode) = SCARG(uap, mode);
656 		return sys_mkfifoat(l, &bma, retval);
657 	} else {
658 
659 		/*
660 		 * Linux device numbers uses 8 bits for minor and 8 bits
661 		 * for major. Due to how we map our major and minor,
662 		 * this just fits into our dev_t. Just mask off the
663 		 * upper 16bit to remove any random junk.
664 		 */
665 
666 		return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
667 		    SCARG(uap, mode), SCARG(uap, dev) & 0xffff, retval,
668 		    UIO_USERSPACE);
669 	}
670 }
671 
672 int
673 linux_sys_fchmodat(struct lwp *l, const struct linux_sys_fchmodat_args *uap, register_t *retval)
674 {
675 	/* {
676 		syscallarg(int) fd;
677 		syscallarg(const char *) path;
678 		syscallarg(linux_umode_t) mode;
679 	} */
680 
681 	return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
682 			      SCARG(uap, mode), AT_SYMLINK_FOLLOW);
683 }
684 
685 int
686 linux_sys_fchownat(struct lwp *l, const struct linux_sys_fchownat_args *uap, register_t *retval)
687 {
688 	/* {
689 		syscallarg(int) fd;
690 		syscallarg(const char *) path;
691 		syscallarg(uid_t) owner;
692 		syscallarg(gid_t) group;
693 		syscallarg(int) flag;
694 	} */
695 	int flag;
696 
697 	flag = linux_to_bsd_atflags(SCARG(uap, flag));
698 	return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
699 			      SCARG(uap, owner), SCARG(uap, group), flag);
700 }
701 
702 int
703 linux_sys_faccessat(struct lwp *l, const struct linux_sys_faccessat_args *uap, register_t *retval)
704 {
705 	/* {
706 		syscallarg(int) fd;
707 		syscallarg(const char *) path;
708 		syscallarg(int) amode;
709 	} */
710 
711 	return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
712 	     SCARG(uap, amode), AT_SYMLINK_FOLLOW);
713 }
714 
715 /*
716  * This is just fsync() for now (just as it is in the Linux kernel)
717  * Note: this is not implemented under Linux on Alpha and Arm
718  *	but should still be defined in our syscalls.master.
719  *	(syscall #148 on the arm)
720  */
721 int
722 linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
723 {
724 	/* {
725 		syscallarg(int) fd;
726 	} */
727 
728 	return sys_fsync(l, (const void *)uap, retval);
729 }
730 
731 /*
732  * pread(2).
733  */
734 int
735 linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
736 {
737 	/* {
738 		syscallarg(int) fd;
739 		syscallarg(void *) buf;
740 		syscallarg(size_t) nbyte;
741 		syscallarg(linux_off_t) offset;
742 	} */
743 	struct sys_pread_args pra;
744 
745 	SCARG(&pra, fd) = SCARG(uap, fd);
746 	SCARG(&pra, buf) = SCARG(uap, buf);
747 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
748 	SCARG(&pra, offset) = SCARG(uap, offset);
749 
750 	return sys_pread(l, &pra, retval);
751 }
752 
753 /*
754  * pwrite(2).
755  */
756 int
757 linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
758 {
759 	/* {
760 		syscallarg(int) fd;
761 		syscallarg(void *) buf;
762 		syscallarg(size_t) nbyte;
763 		syscallarg(linux_off_t) offset;
764 	} */
765 	struct sys_pwrite_args pra;
766 
767 	SCARG(&pra, fd) = SCARG(uap, fd);
768 	SCARG(&pra, buf) = SCARG(uap, buf);
769 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
770 	SCARG(&pra, offset) = SCARG(uap, offset);
771 
772 	return sys_pwrite(l, &pra, retval);
773 }
774 
775 int
776 linux_sys_dup3(struct lwp *l, const struct linux_sys_dup3_args *uap,
777     register_t *retval)
778 {
779 	/* {
780 		syscallarg(int) from;
781 		syscallarg(int) to;
782 		syscallarg(int) flags;
783 	} */
784 	int error;
785 	if ((error = sys_dup2(l, (const struct sys_dup2_args *)uap, retval)))
786 		return error;
787 
788 	if (SCARG(uap, flags) & LINUX_O_CLOEXEC)
789 		fd_set_exclose(l, SCARG(uap, to), true);
790 
791 	return 0;
792 }
793 
794 
795 int
796 linux_to_bsd_atflags(int lflags)
797 {
798 	int bflags = 0;
799 
800 	if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
801 		bflags |= AT_SYMLINK_NOFOLLOW;
802 	if (lflags & LINUX_AT_REMOVEDIR)
803 		bflags |= AT_REMOVEDIR;
804 	if (lflags & LINUX_AT_SYMLINK_FOLLOW)
805 		bflags |= AT_SYMLINK_FOLLOW;
806 
807 	return bflags;
808 }
809 
810 
811 #define LINUX_NOT_SUPPORTED(fun) \
812 int \
813 fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
814 { \
815 	return EOPNOTSUPP; \
816 }
817 
818 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
819 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
820 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
821 
822 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
823 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
824 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
825 
826 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
827 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
828 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
829 
830 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
831 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
832 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
833