vfs_syscalls.c revision 1.468 1 /* $NetBSD: vfs_syscalls.c,v 1.468 2013/10/17 18:01:11 njoly Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
66 */
67
68 /*
69 * Virtual File System System Calls
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.468 2013/10/17 18:01:11 njoly Exp $");
74
75 #ifdef _KERNEL_OPT
76 #include "opt_fileassoc.h"
77 #include "veriexec.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/namei.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/fcntl.h>
87 #include <sys/stat.h>
88 #include <sys/vnode.h>
89 #include <sys/mount.h>
90 #include <sys/proc.h>
91 #include <sys/uio.h>
92 #include <sys/kmem.h>
93 #include <sys/dirent.h>
94 #include <sys/sysctl.h>
95 #include <sys/syscallargs.h>
96 #include <sys/vfs_syscalls.h>
97 #include <sys/quota.h>
98 #include <sys/quotactl.h>
99 #include <sys/ktrace.h>
100 #ifdef FILEASSOC
101 #include <sys/fileassoc.h>
102 #endif /* FILEASSOC */
103 #include <sys/extattr.h>
104 #include <sys/verified_exec.h>
105 #include <sys/kauth.h>
106 #include <sys/atomic.h>
107 #include <sys/module.h>
108 #include <sys/buf.h>
109
110 #include <miscfs/genfs/genfs.h>
111 #include <miscfs/syncfs/syncfs.h>
112 #include <miscfs/specfs/specdev.h>
113
114 #include <nfs/rpcv2.h>
115 #include <nfs/nfsproto.h>
116 #include <nfs/nfs.h>
117 #include <nfs/nfs_var.h>
118
119 static int change_flags(struct vnode *, u_long, struct lwp *);
120 static int change_mode(struct vnode *, int, struct lwp *l);
121 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
122 static int do_open(lwp_t *, struct vnode *, struct pathbuf *, int, int, int *);
123 static int do_sys_openat(lwp_t *, int, const char *, int, int, int *);
124 static int do_sys_mknodat(struct lwp *, int, const char *, mode_t,
125 dev_t, register_t *, enum uio_seg);
126 static int do_sys_mkdirat(struct lwp *l, int, const char *, mode_t,
127 enum uio_seg);
128 static int do_sys_mkfifoat(struct lwp *, int, const char *, mode_t);
129 static int do_sys_chmodat(struct lwp *, int, const char *, int, int);
130 static int do_sys_chownat(struct lwp *, int, const char *, uid_t, gid_t, int);
131 static int do_sys_accessat(struct lwp *, int, const char *, int ,int);
132 static int do_sys_symlinkat(struct lwp *, const char *, int, const char *,
133 enum uio_seg);
134 static int do_sys_linkat(struct lwp *, int, const char *, int, const char *,
135 int, register_t *);
136 static int do_sys_renameat(struct lwp *l, int, const char *, int, const char *,
137 enum uio_seg, int);
138 static int do_sys_readlinkat(struct lwp *, int, const char *, char *,
139 size_t, register_t *);
140 static int do_sys_unlinkat(struct lwp *, int, const char *, int, enum uio_seg);
141
142 static int fd_nameiat(struct lwp *, int, struct nameidata *);
143 static int fd_nameiat_simple_user(struct lwp *, int, const char *,
144 namei_simple_flags_t, struct vnode **);
145
146
147 /*
148 * This table is used to maintain compatibility with 4.3BSD
149 * and NetBSD 0.9 mount syscalls - and possibly other systems.
150 * Note, the order is important!
151 *
152 * Do not modify this table. It should only contain filesystems
153 * supported by NetBSD 0.9 and 4.3BSD.
154 */
155 const char * const mountcompatnames[] = {
156 NULL, /* 0 = MOUNT_NONE */
157 MOUNT_FFS, /* 1 = MOUNT_UFS */
158 MOUNT_NFS, /* 2 */
159 MOUNT_MFS, /* 3 */
160 MOUNT_MSDOS, /* 4 */
161 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
162 MOUNT_FDESC, /* 6 */
163 MOUNT_KERNFS, /* 7 */
164 NULL, /* 8 = MOUNT_DEVFS */
165 MOUNT_AFS, /* 9 */
166 };
167
168 const int nmountcompatnames = __arraycount(mountcompatnames);
169
170 static int
171 fd_nameiat(struct lwp *l, int fdat, struct nameidata *ndp)
172 {
173 file_t *dfp;
174 int error;
175
176 if (fdat != AT_FDCWD) {
177 if ((error = fd_getvnode(fdat, &dfp)) != 0)
178 goto out;
179
180 NDAT(ndp, dfp->f_data);
181 }
182
183 error = namei(ndp);
184
185 if (fdat != AT_FDCWD)
186 fd_putfile(fdat);
187 out:
188 return error;
189 }
190
191 static int
192 fd_nameiat_simple_user(struct lwp *l, int fdat, const char *path,
193 namei_simple_flags_t sflags, struct vnode **vp_ret)
194 {
195 file_t *dfp;
196 struct vnode *dvp;
197 int error;
198
199 if (fdat != AT_FDCWD) {
200 if ((error = fd_getvnode(fdat, &dfp)) != 0)
201 goto out;
202
203 dvp = dfp->f_data;
204 } else {
205 dvp = NULL;
206 }
207
208 error = nameiat_simple_user(dvp, path, sflags, vp_ret);
209
210 if (fdat != AT_FDCWD)
211 fd_putfile(fdat);
212 out:
213 return error;
214 }
215
216 static int
217 open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
218 {
219 int error;
220
221 fp->f_flag = flags & FMASK;
222 fp->f_type = DTYPE_VNODE;
223 fp->f_ops = &vnops;
224 fp->f_data = vp;
225
226 if (flags & (O_EXLOCK | O_SHLOCK)) {
227 struct flock lf;
228 int type;
229
230 lf.l_whence = SEEK_SET;
231 lf.l_start = 0;
232 lf.l_len = 0;
233 if (flags & O_EXLOCK)
234 lf.l_type = F_WRLCK;
235 else
236 lf.l_type = F_RDLCK;
237 type = F_FLOCK;
238 if ((flags & FNONBLOCK) == 0)
239 type |= F_WAIT;
240 VOP_UNLOCK(vp);
241 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
242 if (error) {
243 (void) vn_close(vp, fp->f_flag, fp->f_cred);
244 fd_abort(l->l_proc, fp, indx);
245 return error;
246 }
247 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
248 atomic_or_uint(&fp->f_flag, FHASLOCK);
249 }
250 if (flags & O_CLOEXEC)
251 fd_set_exclose(l, indx, true);
252 return 0;
253 }
254
255 static int
256 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
257 void *data, size_t *data_len)
258 {
259 struct mount *mp;
260 int error = 0, saved_flags;
261
262 mp = vp->v_mount;
263 saved_flags = mp->mnt_flag;
264
265 /* We can operate only on VV_ROOT nodes. */
266 if ((vp->v_vflag & VV_ROOT) == 0) {
267 error = EINVAL;
268 goto out;
269 }
270
271 /*
272 * We only allow the filesystem to be reloaded if it
273 * is currently mounted read-only. Additionally, we
274 * prevent read-write to read-only downgrades.
275 */
276 if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
277 (mp->mnt_flag & MNT_RDONLY) == 0 &&
278 (mp->mnt_iflag & IMNT_CAN_RWTORO) == 0) {
279 error = EOPNOTSUPP; /* Needs translation */
280 goto out;
281 }
282
283 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
284 KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
285 if (error)
286 goto out;
287
288 if (vfs_busy(mp, NULL)) {
289 error = EPERM;
290 goto out;
291 }
292
293 mutex_enter(&mp->mnt_updating);
294
295 mp->mnt_flag &= ~MNT_OP_FLAGS;
296 mp->mnt_flag |= flags & MNT_OP_FLAGS;
297
298 /*
299 * Set the mount level flags.
300 */
301 if (flags & MNT_RDONLY)
302 mp->mnt_flag |= MNT_RDONLY;
303 else if (mp->mnt_flag & MNT_RDONLY)
304 mp->mnt_iflag |= IMNT_WANTRDWR;
305 mp->mnt_flag &= ~MNT_BASIC_FLAGS;
306 mp->mnt_flag |= flags & MNT_BASIC_FLAGS;
307 error = VFS_MOUNT(mp, path, data, data_len);
308
309 if (error && data != NULL) {
310 int error2;
311
312 /*
313 * Update failed; let's try and see if it was an
314 * export request. For compat with 3.0 and earlier.
315 */
316 error2 = vfs_hooks_reexport(mp, path, data);
317
318 /*
319 * Only update error code if the export request was
320 * understood but some problem occurred while
321 * processing it.
322 */
323 if (error2 != EJUSTRETURN)
324 error = error2;
325 }
326
327 if (mp->mnt_iflag & IMNT_WANTRDWR)
328 mp->mnt_flag &= ~MNT_RDONLY;
329 if (error)
330 mp->mnt_flag = saved_flags;
331 mp->mnt_flag &= ~MNT_OP_FLAGS;
332 mp->mnt_iflag &= ~IMNT_WANTRDWR;
333 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
334 if (mp->mnt_syncer == NULL)
335 error = vfs_allocate_syncvnode(mp);
336 } else {
337 if (mp->mnt_syncer != NULL)
338 vfs_deallocate_syncvnode(mp);
339 }
340 mutex_exit(&mp->mnt_updating);
341 vfs_unbusy(mp, false, NULL);
342
343 if ((error == 0) && !(saved_flags & MNT_EXTATTR) &&
344 (flags & MNT_EXTATTR)) {
345 if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_START,
346 NULL, 0, NULL) != 0) {
347 printf("%s: failed to start extattr, error = %d",
348 mp->mnt_stat.f_mntonname, error);
349 mp->mnt_flag &= ~MNT_EXTATTR;
350 }
351 }
352
353 if ((error == 0) && (saved_flags & MNT_EXTATTR) &&
354 !(flags & MNT_EXTATTR)) {
355 if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_STOP,
356 NULL, 0, NULL) != 0) {
357 printf("%s: failed to stop extattr, error = %d",
358 mp->mnt_stat.f_mntonname, error);
359 mp->mnt_flag |= MNT_RDONLY;
360 }
361 }
362 out:
363 return (error);
364 }
365
366 static int
367 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
368 {
369 char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
370 int error;
371
372 /* Copy file-system type from userspace. */
373 error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
374 if (error) {
375 /*
376 * Historically, filesystem types were identified by numbers.
377 * If we get an integer for the filesystem type instead of a
378 * string, we check to see if it matches one of the historic
379 * filesystem types.
380 */
381 u_long fsindex = (u_long)fstype;
382 if (fsindex >= nmountcompatnames ||
383 mountcompatnames[fsindex] == NULL)
384 return ENODEV;
385 strlcpy(fstypename, mountcompatnames[fsindex],
386 sizeof(fstypename));
387 }
388
389 /* Accept `ufs' as an alias for `ffs', for compatibility. */
390 if (strcmp(fstypename, "ufs") == 0)
391 fstypename[0] = 'f';
392
393 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
394 return 0;
395
396 /* If we can autoload a vfs module, try again */
397 (void)module_autoload(fstypename, MODULE_CLASS_VFS);
398
399 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
400 return 0;
401
402 return ENODEV;
403 }
404
405 static int
406 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
407 void *data, size_t *data_len)
408 {
409 struct mount *mp;
410 int error;
411
412 /* If MNT_GETARGS is specified, it should be the only flag. */
413 if (flags & ~MNT_GETARGS)
414 return EINVAL;
415
416 mp = vp->v_mount;
417
418 /* XXX: probably some notion of "can see" here if we want isolation. */
419 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
420 KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
421 if (error)
422 return error;
423
424 if ((vp->v_vflag & VV_ROOT) == 0)
425 return EINVAL;
426
427 if (vfs_busy(mp, NULL))
428 return EPERM;
429
430 mutex_enter(&mp->mnt_updating);
431 mp->mnt_flag &= ~MNT_OP_FLAGS;
432 mp->mnt_flag |= MNT_GETARGS;
433 error = VFS_MOUNT(mp, path, data, data_len);
434 mp->mnt_flag &= ~MNT_OP_FLAGS;
435 mutex_exit(&mp->mnt_updating);
436
437 vfs_unbusy(mp, false, NULL);
438 return (error);
439 }
440
441 int
442 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
443 {
444 /* {
445 syscallarg(const char *) type;
446 syscallarg(const char *) path;
447 syscallarg(int) flags;
448 syscallarg(void *) data;
449 syscallarg(size_t) data_len;
450 } */
451
452 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
453 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
454 SCARG(uap, data_len), retval);
455 }
456
457 int
458 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
459 const char *path, int flags, void *data, enum uio_seg data_seg,
460 size_t data_len, register_t *retval)
461 {
462 struct vnode *vp;
463 void *data_buf = data;
464 bool vfsopsrele = false;
465 int error;
466
467 /* XXX: The calling convention of this routine is totally bizarre */
468 if (vfsops)
469 vfsopsrele = true;
470
471 /*
472 * Get vnode to be covered
473 */
474 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
475 if (error != 0) {
476 vp = NULL;
477 goto done;
478 }
479
480 if (vfsops == NULL) {
481 if (flags & (MNT_GETARGS | MNT_UPDATE)) {
482 vfsops = vp->v_mount->mnt_op;
483 } else {
484 /* 'type' is userspace */
485 error = mount_get_vfsops(type, &vfsops);
486 if (error != 0)
487 goto done;
488 vfsopsrele = true;
489 }
490 }
491
492 if (data != NULL && data_seg == UIO_USERSPACE) {
493 if (data_len == 0) {
494 /* No length supplied, use default for filesystem */
495 data_len = vfsops->vfs_min_mount_data;
496 if (data_len > VFS_MAX_MOUNT_DATA) {
497 error = EINVAL;
498 goto done;
499 }
500 /*
501 * Hopefully a longer buffer won't make copyin() fail.
502 * For compatibility with 3.0 and earlier.
503 */
504 if (flags & MNT_UPDATE
505 && data_len < sizeof (struct mnt_export_args30))
506 data_len = sizeof (struct mnt_export_args30);
507 }
508 data_buf = kmem_alloc(data_len, KM_SLEEP);
509
510 /* NFS needs the buffer even for mnt_getargs .... */
511 error = copyin(data, data_buf, data_len);
512 if (error != 0)
513 goto done;
514 }
515
516 if (flags & MNT_GETARGS) {
517 if (data_len == 0) {
518 error = EINVAL;
519 goto done;
520 }
521 error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
522 if (error != 0)
523 goto done;
524 if (data_seg == UIO_USERSPACE)
525 error = copyout(data_buf, data, data_len);
526 *retval = data_len;
527 } else if (flags & MNT_UPDATE) {
528 error = mount_update(l, vp, path, flags, data_buf, &data_len);
529 } else {
530 /* Locking is handled internally in mount_domount(). */
531 KASSERT(vfsopsrele == true);
532 error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
533 &data_len);
534 vfsopsrele = false;
535 }
536
537 done:
538 if (vfsopsrele)
539 vfs_delref(vfsops);
540 if (vp != NULL) {
541 vrele(vp);
542 }
543 if (data_buf != data)
544 kmem_free(data_buf, data_len);
545 return (error);
546 }
547
548 /*
549 * Unmount a file system.
550 *
551 * Note: unmount takes a path to the vnode mounted on as argument,
552 * not special file (as before).
553 */
554 /* ARGSUSED */
555 int
556 sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
557 {
558 /* {
559 syscallarg(const char *) path;
560 syscallarg(int) flags;
561 } */
562 struct vnode *vp;
563 struct mount *mp;
564 int error;
565 struct pathbuf *pb;
566 struct nameidata nd;
567
568 error = pathbuf_copyin(SCARG(uap, path), &pb);
569 if (error) {
570 return error;
571 }
572
573 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
574 if ((error = namei(&nd)) != 0) {
575 pathbuf_destroy(pb);
576 return error;
577 }
578 vp = nd.ni_vp;
579 pathbuf_destroy(pb);
580
581 mp = vp->v_mount;
582 atomic_inc_uint(&mp->mnt_refcnt);
583 VOP_UNLOCK(vp);
584
585 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
586 KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
587 if (error) {
588 vrele(vp);
589 vfs_destroy(mp);
590 return (error);
591 }
592
593 /*
594 * Don't allow unmounting the root file system.
595 */
596 if (mp->mnt_flag & MNT_ROOTFS) {
597 vrele(vp);
598 vfs_destroy(mp);
599 return (EINVAL);
600 }
601
602 /*
603 * Must be the root of the filesystem
604 */
605 if ((vp->v_vflag & VV_ROOT) == 0) {
606 vrele(vp);
607 vfs_destroy(mp);
608 return (EINVAL);
609 }
610
611 vrele(vp);
612 error = dounmount(mp, SCARG(uap, flags), l);
613 vfs_destroy(mp);
614 return error;
615 }
616
617 /*
618 * Sync each mounted filesystem.
619 */
620 #ifdef DEBUG
621 int syncprt = 0;
622 struct ctldebug debug0 = { "syncprt", &syncprt };
623 #endif
624
625 void
626 do_sys_sync(struct lwp *l)
627 {
628 struct mount *mp, *nmp;
629 int asyncflag;
630
631 mutex_enter(&mountlist_lock);
632 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
633 mp = nmp) {
634 if (vfs_busy(mp, &nmp)) {
635 continue;
636 }
637 mutex_enter(&mp->mnt_updating);
638 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
639 asyncflag = mp->mnt_flag & MNT_ASYNC;
640 mp->mnt_flag &= ~MNT_ASYNC;
641 VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
642 if (asyncflag)
643 mp->mnt_flag |= MNT_ASYNC;
644 }
645 mutex_exit(&mp->mnt_updating);
646 vfs_unbusy(mp, false, &nmp);
647 }
648 mutex_exit(&mountlist_lock);
649 #ifdef DEBUG
650 if (syncprt)
651 vfs_bufstats();
652 #endif /* DEBUG */
653 }
654
655 /* ARGSUSED */
656 int
657 sys_sync(struct lwp *l, const void *v, register_t *retval)
658 {
659 do_sys_sync(l);
660 return (0);
661 }
662
663
664 /*
665 * Access or change filesystem quotas.
666 *
667 * (this is really 14 different calls bundled into one)
668 */
669
670 static int
671 do_sys_quotactl_stat(struct mount *mp, struct quotastat *info_u)
672 {
673 struct quotastat info_k;
674 int error;
675
676 /* ensure any padding bytes are cleared */
677 memset(&info_k, 0, sizeof(info_k));
678
679 error = vfs_quotactl_stat(mp, &info_k);
680 if (error) {
681 return error;
682 }
683
684 return copyout(&info_k, info_u, sizeof(info_k));
685 }
686
687 static int
688 do_sys_quotactl_idtypestat(struct mount *mp, int idtype,
689 struct quotaidtypestat *info_u)
690 {
691 struct quotaidtypestat info_k;
692 int error;
693
694 /* ensure any padding bytes are cleared */
695 memset(&info_k, 0, sizeof(info_k));
696
697 error = vfs_quotactl_idtypestat(mp, idtype, &info_k);
698 if (error) {
699 return error;
700 }
701
702 return copyout(&info_k, info_u, sizeof(info_k));
703 }
704
705 static int
706 do_sys_quotactl_objtypestat(struct mount *mp, int objtype,
707 struct quotaobjtypestat *info_u)
708 {
709 struct quotaobjtypestat info_k;
710 int error;
711
712 /* ensure any padding bytes are cleared */
713 memset(&info_k, 0, sizeof(info_k));
714
715 error = vfs_quotactl_objtypestat(mp, objtype, &info_k);
716 if (error) {
717 return error;
718 }
719
720 return copyout(&info_k, info_u, sizeof(info_k));
721 }
722
723 static int
724 do_sys_quotactl_get(struct mount *mp, const struct quotakey *key_u,
725 struct quotaval *val_u)
726 {
727 struct quotakey key_k;
728 struct quotaval val_k;
729 int error;
730
731 /* ensure any padding bytes are cleared */
732 memset(&val_k, 0, sizeof(val_k));
733
734 error = copyin(key_u, &key_k, sizeof(key_k));
735 if (error) {
736 return error;
737 }
738
739 error = vfs_quotactl_get(mp, &key_k, &val_k);
740 if (error) {
741 return error;
742 }
743
744 return copyout(&val_k, val_u, sizeof(val_k));
745 }
746
747 static int
748 do_sys_quotactl_put(struct mount *mp, const struct quotakey *key_u,
749 const struct quotaval *val_u)
750 {
751 struct quotakey key_k;
752 struct quotaval val_k;
753 int error;
754
755 error = copyin(key_u, &key_k, sizeof(key_k));
756 if (error) {
757 return error;
758 }
759
760 error = copyin(val_u, &val_k, sizeof(val_k));
761 if (error) {
762 return error;
763 }
764
765 return vfs_quotactl_put(mp, &key_k, &val_k);
766 }
767
768 static int
769 do_sys_quotactl_delete(struct mount *mp, const struct quotakey *key_u)
770 {
771 struct quotakey key_k;
772 int error;
773
774 error = copyin(key_u, &key_k, sizeof(key_k));
775 if (error) {
776 return error;
777 }
778
779 return vfs_quotactl_delete(mp, &key_k);
780 }
781
782 static int
783 do_sys_quotactl_cursoropen(struct mount *mp, struct quotakcursor *cursor_u)
784 {
785 struct quotakcursor cursor_k;
786 int error;
787
788 /* ensure any padding bytes are cleared */
789 memset(&cursor_k, 0, sizeof(cursor_k));
790
791 error = vfs_quotactl_cursoropen(mp, &cursor_k);
792 if (error) {
793 return error;
794 }
795
796 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
797 }
798
799 static int
800 do_sys_quotactl_cursorclose(struct mount *mp, struct quotakcursor *cursor_u)
801 {
802 struct quotakcursor cursor_k;
803 int error;
804
805 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
806 if (error) {
807 return error;
808 }
809
810 return vfs_quotactl_cursorclose(mp, &cursor_k);
811 }
812
813 static int
814 do_sys_quotactl_cursorskipidtype(struct mount *mp,
815 struct quotakcursor *cursor_u, int idtype)
816 {
817 struct quotakcursor cursor_k;
818 int error;
819
820 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
821 if (error) {
822 return error;
823 }
824
825 error = vfs_quotactl_cursorskipidtype(mp, &cursor_k, idtype);
826 if (error) {
827 return error;
828 }
829
830 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
831 }
832
833 static int
834 do_sys_quotactl_cursorget(struct mount *mp, struct quotakcursor *cursor_u,
835 struct quotakey *keys_u, struct quotaval *vals_u, unsigned maxnum,
836 unsigned *ret_u)
837 {
838 #define CGET_STACK_MAX 8
839 struct quotakcursor cursor_k;
840 struct quotakey stackkeys[CGET_STACK_MAX];
841 struct quotaval stackvals[CGET_STACK_MAX];
842 struct quotakey *keys_k;
843 struct quotaval *vals_k;
844 unsigned ret_k;
845 int error;
846
847 if (maxnum > 128) {
848 maxnum = 128;
849 }
850
851 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
852 if (error) {
853 return error;
854 }
855
856 if (maxnum <= CGET_STACK_MAX) {
857 keys_k = stackkeys;
858 vals_k = stackvals;
859 /* ensure any padding bytes are cleared */
860 memset(keys_k, 0, maxnum * sizeof(keys_k[0]));
861 memset(vals_k, 0, maxnum * sizeof(vals_k[0]));
862 } else {
863 keys_k = kmem_zalloc(maxnum * sizeof(keys_k[0]), KM_SLEEP);
864 vals_k = kmem_zalloc(maxnum * sizeof(vals_k[0]), KM_SLEEP);
865 }
866
867 error = vfs_quotactl_cursorget(mp, &cursor_k, keys_k, vals_k, maxnum,
868 &ret_k);
869 if (error) {
870 goto fail;
871 }
872
873 error = copyout(keys_k, keys_u, ret_k * sizeof(keys_k[0]));
874 if (error) {
875 goto fail;
876 }
877
878 error = copyout(vals_k, vals_u, ret_k * sizeof(vals_k[0]));
879 if (error) {
880 goto fail;
881 }
882
883 error = copyout(&ret_k, ret_u, sizeof(ret_k));
884 if (error) {
885 goto fail;
886 }
887
888 /* do last to maximize the chance of being able to recover a failure */
889 error = copyout(&cursor_k, cursor_u, sizeof(cursor_k));
890
891 fail:
892 if (keys_k != stackkeys) {
893 kmem_free(keys_k, maxnum * sizeof(keys_k[0]));
894 }
895 if (vals_k != stackvals) {
896 kmem_free(vals_k, maxnum * sizeof(vals_k[0]));
897 }
898 return error;
899 }
900
901 static int
902 do_sys_quotactl_cursoratend(struct mount *mp, struct quotakcursor *cursor_u,
903 int *ret_u)
904 {
905 struct quotakcursor cursor_k;
906 int ret_k;
907 int error;
908
909 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
910 if (error) {
911 return error;
912 }
913
914 error = vfs_quotactl_cursoratend(mp, &cursor_k, &ret_k);
915 if (error) {
916 return error;
917 }
918
919 error = copyout(&ret_k, ret_u, sizeof(ret_k));
920 if (error) {
921 return error;
922 }
923
924 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
925 }
926
927 static int
928 do_sys_quotactl_cursorrewind(struct mount *mp, struct quotakcursor *cursor_u)
929 {
930 struct quotakcursor cursor_k;
931 int error;
932
933 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
934 if (error) {
935 return error;
936 }
937
938 error = vfs_quotactl_cursorrewind(mp, &cursor_k);
939 if (error) {
940 return error;
941 }
942
943 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
944 }
945
946 static int
947 do_sys_quotactl_quotaon(struct mount *mp, int idtype, const char *path_u)
948 {
949 char *path_k;
950 int error;
951
952 /* XXX this should probably be a struct pathbuf */
953 path_k = PNBUF_GET();
954 error = copyin(path_u, path_k, PATH_MAX);
955 if (error) {
956 PNBUF_PUT(path_k);
957 return error;
958 }
959
960 error = vfs_quotactl_quotaon(mp, idtype, path_k);
961
962 PNBUF_PUT(path_k);
963 return error;
964 }
965
966 static int
967 do_sys_quotactl_quotaoff(struct mount *mp, int idtype)
968 {
969 return vfs_quotactl_quotaoff(mp, idtype);
970 }
971
972 int
973 do_sys_quotactl(const char *path_u, const struct quotactl_args *args)
974 {
975 struct mount *mp;
976 struct vnode *vp;
977 int error;
978
979 error = namei_simple_user(path_u, NSM_FOLLOW_TRYEMULROOT, &vp);
980 if (error != 0)
981 return (error);
982 mp = vp->v_mount;
983
984 switch (args->qc_op) {
985 case QUOTACTL_STAT:
986 error = do_sys_quotactl_stat(mp, args->u.stat.qc_info);
987 break;
988 case QUOTACTL_IDTYPESTAT:
989 error = do_sys_quotactl_idtypestat(mp,
990 args->u.idtypestat.qc_idtype,
991 args->u.idtypestat.qc_info);
992 break;
993 case QUOTACTL_OBJTYPESTAT:
994 error = do_sys_quotactl_objtypestat(mp,
995 args->u.objtypestat.qc_objtype,
996 args->u.objtypestat.qc_info);
997 break;
998 case QUOTACTL_GET:
999 error = do_sys_quotactl_get(mp,
1000 args->u.get.qc_key,
1001 args->u.get.qc_val);
1002 break;
1003 case QUOTACTL_PUT:
1004 error = do_sys_quotactl_put(mp,
1005 args->u.put.qc_key,
1006 args->u.put.qc_val);
1007 break;
1008 case QUOTACTL_DELETE:
1009 error = do_sys_quotactl_delete(mp, args->u.delete.qc_key);
1010 break;
1011 case QUOTACTL_CURSOROPEN:
1012 error = do_sys_quotactl_cursoropen(mp,
1013 args->u.cursoropen.qc_cursor);
1014 break;
1015 case QUOTACTL_CURSORCLOSE:
1016 error = do_sys_quotactl_cursorclose(mp,
1017 args->u.cursorclose.qc_cursor);
1018 break;
1019 case QUOTACTL_CURSORSKIPIDTYPE:
1020 error = do_sys_quotactl_cursorskipidtype(mp,
1021 args->u.cursorskipidtype.qc_cursor,
1022 args->u.cursorskipidtype.qc_idtype);
1023 break;
1024 case QUOTACTL_CURSORGET:
1025 error = do_sys_quotactl_cursorget(mp,
1026 args->u.cursorget.qc_cursor,
1027 args->u.cursorget.qc_keys,
1028 args->u.cursorget.qc_vals,
1029 args->u.cursorget.qc_maxnum,
1030 args->u.cursorget.qc_ret);
1031 break;
1032 case QUOTACTL_CURSORATEND:
1033 error = do_sys_quotactl_cursoratend(mp,
1034 args->u.cursoratend.qc_cursor,
1035 args->u.cursoratend.qc_ret);
1036 break;
1037 case QUOTACTL_CURSORREWIND:
1038 error = do_sys_quotactl_cursorrewind(mp,
1039 args->u.cursorrewind.qc_cursor);
1040 break;
1041 case QUOTACTL_QUOTAON:
1042 error = do_sys_quotactl_quotaon(mp,
1043 args->u.quotaon.qc_idtype,
1044 args->u.quotaon.qc_quotafile);
1045 break;
1046 case QUOTACTL_QUOTAOFF:
1047 error = do_sys_quotactl_quotaoff(mp,
1048 args->u.quotaoff.qc_idtype);
1049 break;
1050 default:
1051 error = EINVAL;
1052 break;
1053 }
1054
1055 vrele(vp);
1056 return error;
1057 }
1058
1059 /* ARGSUSED */
1060 int
1061 sys___quotactl(struct lwp *l, const struct sys___quotactl_args *uap,
1062 register_t *retval)
1063 {
1064 /* {
1065 syscallarg(const char *) path;
1066 syscallarg(struct quotactl_args *) args;
1067 } */
1068 struct quotactl_args args;
1069 int error;
1070
1071 error = copyin(SCARG(uap, args), &args, sizeof(args));
1072 if (error) {
1073 return error;
1074 }
1075
1076 return do_sys_quotactl(SCARG(uap, path), &args);
1077 }
1078
1079 int
1080 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
1081 int root)
1082 {
1083 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
1084 int error = 0;
1085
1086 /*
1087 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1088 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
1089 * overrides MNT_NOWAIT.
1090 */
1091 if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
1092 (flags != MNT_WAIT && flags != 0)) {
1093 memcpy(sp, &mp->mnt_stat, sizeof(*sp));
1094 goto done;
1095 }
1096
1097 /* Get the filesystem stats now */
1098 memset(sp, 0, sizeof(*sp));
1099 if ((error = VFS_STATVFS(mp, sp)) != 0) {
1100 return error;
1101 }
1102
1103 if (cwdi->cwdi_rdir == NULL)
1104 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
1105 done:
1106 if (cwdi->cwdi_rdir != NULL) {
1107 size_t len;
1108 char *bp;
1109 char c;
1110 char *path = PNBUF_GET();
1111
1112 bp = path + MAXPATHLEN;
1113 *--bp = '\0';
1114 rw_enter(&cwdi->cwdi_lock, RW_READER);
1115 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
1116 MAXPATHLEN / 2, 0, l);
1117 rw_exit(&cwdi->cwdi_lock);
1118 if (error) {
1119 PNBUF_PUT(path);
1120 return error;
1121 }
1122 len = strlen(bp);
1123 if (len != 1) {
1124 /*
1125 * for mount points that are below our root, we can see
1126 * them, so we fix up the pathname and return them. The
1127 * rest we cannot see, so we don't allow viewing the
1128 * data.
1129 */
1130 if (strncmp(bp, sp->f_mntonname, len) == 0 &&
1131 ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
1132 (void)strlcpy(sp->f_mntonname,
1133 c == '\0' ? "/" : &sp->f_mntonname[len],
1134 sizeof(sp->f_mntonname));
1135 } else {
1136 if (root)
1137 (void)strlcpy(sp->f_mntonname, "/",
1138 sizeof(sp->f_mntonname));
1139 else
1140 error = EPERM;
1141 }
1142 }
1143 PNBUF_PUT(path);
1144 }
1145 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1146 return error;
1147 }
1148
1149 /*
1150 * Get filesystem statistics by path.
1151 */
1152 int
1153 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
1154 {
1155 struct mount *mp;
1156 int error;
1157 struct vnode *vp;
1158
1159 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
1160 if (error != 0)
1161 return error;
1162 mp = vp->v_mount;
1163 error = dostatvfs(mp, sb, l, flags, 1);
1164 vrele(vp);
1165 return error;
1166 }
1167
1168 /* ARGSUSED */
1169 int
1170 sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
1171 {
1172 /* {
1173 syscallarg(const char *) path;
1174 syscallarg(struct statvfs *) buf;
1175 syscallarg(int) flags;
1176 } */
1177 struct statvfs *sb;
1178 int error;
1179
1180 sb = STATVFSBUF_GET();
1181 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
1182 if (error == 0)
1183 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1184 STATVFSBUF_PUT(sb);
1185 return error;
1186 }
1187
1188 /*
1189 * Get filesystem statistics by fd.
1190 */
1191 int
1192 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
1193 {
1194 file_t *fp;
1195 struct mount *mp;
1196 int error;
1197
1198 /* fd_getvnode() will use the descriptor for us */
1199 if ((error = fd_getvnode(fd, &fp)) != 0)
1200 return (error);
1201 mp = ((struct vnode *)fp->f_data)->v_mount;
1202 error = dostatvfs(mp, sb, curlwp, flags, 1);
1203 fd_putfile(fd);
1204 return error;
1205 }
1206
1207 /* ARGSUSED */
1208 int
1209 sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
1210 {
1211 /* {
1212 syscallarg(int) fd;
1213 syscallarg(struct statvfs *) buf;
1214 syscallarg(int) flags;
1215 } */
1216 struct statvfs *sb;
1217 int error;
1218
1219 sb = STATVFSBUF_GET();
1220 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
1221 if (error == 0)
1222 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1223 STATVFSBUF_PUT(sb);
1224 return error;
1225 }
1226
1227
1228 /*
1229 * Get statistics on all filesystems.
1230 */
1231 int
1232 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
1233 int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
1234 register_t *retval)
1235 {
1236 int root = 0;
1237 struct proc *p = l->l_proc;
1238 struct mount *mp, *nmp;
1239 struct statvfs *sb;
1240 size_t count, maxcount;
1241 int error = 0;
1242
1243 sb = STATVFSBUF_GET();
1244 maxcount = bufsize / entry_sz;
1245 mutex_enter(&mountlist_lock);
1246 count = 0;
1247 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
1248 mp = nmp) {
1249 if (vfs_busy(mp, &nmp)) {
1250 continue;
1251 }
1252 if (sfsp && count < maxcount) {
1253 error = dostatvfs(mp, sb, l, flags, 0);
1254 if (error) {
1255 vfs_unbusy(mp, false, &nmp);
1256 error = 0;
1257 continue;
1258 }
1259 error = copyfn(sb, sfsp, entry_sz);
1260 if (error) {
1261 vfs_unbusy(mp, false, NULL);
1262 goto out;
1263 }
1264 sfsp = (char *)sfsp + entry_sz;
1265 root |= strcmp(sb->f_mntonname, "/") == 0;
1266 }
1267 count++;
1268 vfs_unbusy(mp, false, &nmp);
1269 }
1270 mutex_exit(&mountlist_lock);
1271
1272 if (root == 0 && p->p_cwdi->cwdi_rdir) {
1273 /*
1274 * fake a root entry
1275 */
1276 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
1277 sb, l, flags, 1);
1278 if (error != 0)
1279 goto out;
1280 if (sfsp) {
1281 error = copyfn(sb, sfsp, entry_sz);
1282 if (error != 0)
1283 goto out;
1284 }
1285 count++;
1286 }
1287 if (sfsp && count > maxcount)
1288 *retval = maxcount;
1289 else
1290 *retval = count;
1291 out:
1292 STATVFSBUF_PUT(sb);
1293 return error;
1294 }
1295
1296 int
1297 sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval)
1298 {
1299 /* {
1300 syscallarg(struct statvfs *) buf;
1301 syscallarg(size_t) bufsize;
1302 syscallarg(int) flags;
1303 } */
1304
1305 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
1306 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
1307 }
1308
1309 /*
1310 * Change current working directory to a given file descriptor.
1311 */
1312 /* ARGSUSED */
1313 int
1314 sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
1315 {
1316 /* {
1317 syscallarg(int) fd;
1318 } */
1319 struct proc *p = l->l_proc;
1320 struct cwdinfo *cwdi;
1321 struct vnode *vp, *tdp;
1322 struct mount *mp;
1323 file_t *fp;
1324 int error, fd;
1325
1326 /* fd_getvnode() will use the descriptor for us */
1327 fd = SCARG(uap, fd);
1328 if ((error = fd_getvnode(fd, &fp)) != 0)
1329 return (error);
1330 vp = fp->f_data;
1331
1332 vref(vp);
1333 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1334 if (vp->v_type != VDIR)
1335 error = ENOTDIR;
1336 else
1337 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1338 if (error) {
1339 vput(vp);
1340 goto out;
1341 }
1342 while ((mp = vp->v_mountedhere) != NULL) {
1343 error = vfs_busy(mp, NULL);
1344 vput(vp);
1345 if (error != 0)
1346 goto out;
1347 error = VFS_ROOT(mp, &tdp);
1348 vfs_unbusy(mp, false, NULL);
1349 if (error)
1350 goto out;
1351 vp = tdp;
1352 }
1353 VOP_UNLOCK(vp);
1354
1355 /*
1356 * Disallow changing to a directory not under the process's
1357 * current root directory (if there is one).
1358 */
1359 cwdi = p->p_cwdi;
1360 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1361 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
1362 vrele(vp);
1363 error = EPERM; /* operation not permitted */
1364 } else {
1365 vrele(cwdi->cwdi_cdir);
1366 cwdi->cwdi_cdir = vp;
1367 }
1368 rw_exit(&cwdi->cwdi_lock);
1369
1370 out:
1371 fd_putfile(fd);
1372 return (error);
1373 }
1374
1375 /*
1376 * Change this process's notion of the root directory to a given file
1377 * descriptor.
1378 */
1379 int
1380 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
1381 {
1382 struct proc *p = l->l_proc;
1383 struct vnode *vp;
1384 file_t *fp;
1385 int error, fd = SCARG(uap, fd);
1386
1387 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1388 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
1389 return error;
1390 /* fd_getvnode() will use the descriptor for us */
1391 if ((error = fd_getvnode(fd, &fp)) != 0)
1392 return error;
1393 vp = fp->f_data;
1394 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1395 if (vp->v_type != VDIR)
1396 error = ENOTDIR;
1397 else
1398 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1399 VOP_UNLOCK(vp);
1400 if (error)
1401 goto out;
1402 vref(vp);
1403
1404 change_root(p->p_cwdi, vp, l);
1405
1406 out:
1407 fd_putfile(fd);
1408 return (error);
1409 }
1410
1411 /*
1412 * Change current working directory (``.'').
1413 */
1414 /* ARGSUSED */
1415 int
1416 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
1417 {
1418 /* {
1419 syscallarg(const char *) path;
1420 } */
1421 struct proc *p = l->l_proc;
1422 struct cwdinfo *cwdi;
1423 int error;
1424 struct vnode *vp;
1425
1426 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
1427 &vp, l)) != 0)
1428 return (error);
1429 cwdi = p->p_cwdi;
1430 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1431 vrele(cwdi->cwdi_cdir);
1432 cwdi->cwdi_cdir = vp;
1433 rw_exit(&cwdi->cwdi_lock);
1434 return (0);
1435 }
1436
1437 /*
1438 * Change notion of root (``/'') directory.
1439 */
1440 /* ARGSUSED */
1441 int
1442 sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
1443 {
1444 /* {
1445 syscallarg(const char *) path;
1446 } */
1447 struct proc *p = l->l_proc;
1448 int error;
1449 struct vnode *vp;
1450
1451 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1452 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
1453 return (error);
1454 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
1455 &vp, l)) != 0)
1456 return (error);
1457
1458 change_root(p->p_cwdi, vp, l);
1459
1460 return (0);
1461 }
1462
1463 /*
1464 * Common routine for chroot and fchroot.
1465 * NB: callers need to properly authorize the change root operation.
1466 */
1467 void
1468 change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l)
1469 {
1470 struct proc *p = l->l_proc;
1471 kauth_cred_t ncred;
1472
1473 ncred = kauth_cred_alloc();
1474
1475 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1476 if (cwdi->cwdi_rdir != NULL)
1477 vrele(cwdi->cwdi_rdir);
1478 cwdi->cwdi_rdir = vp;
1479
1480 /*
1481 * Prevent escaping from chroot by putting the root under
1482 * the working directory. Silently chdir to / if we aren't
1483 * already there.
1484 */
1485 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1486 /*
1487 * XXX would be more failsafe to change directory to a
1488 * deadfs node here instead
1489 */
1490 vrele(cwdi->cwdi_cdir);
1491 vref(vp);
1492 cwdi->cwdi_cdir = vp;
1493 }
1494 rw_exit(&cwdi->cwdi_lock);
1495
1496 /* Get a write lock on the process credential. */
1497 proc_crmod_enter();
1498
1499 kauth_cred_clone(p->p_cred, ncred);
1500 kauth_proc_chroot(ncred, p->p_cwdi);
1501
1502 /* Broadcast our credentials to the process and other LWPs. */
1503 proc_crmod_leave(ncred, p->p_cred, true);
1504 }
1505
1506 /*
1507 * Common routine for chroot and chdir.
1508 * XXX "where" should be enum uio_seg
1509 */
1510 int
1511 chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l)
1512 {
1513 struct pathbuf *pb;
1514 struct nameidata nd;
1515 int error;
1516
1517 error = pathbuf_maybe_copyin(path, where, &pb);
1518 if (error) {
1519 return error;
1520 }
1521 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1522 if ((error = namei(&nd)) != 0) {
1523 pathbuf_destroy(pb);
1524 return error;
1525 }
1526 *vpp = nd.ni_vp;
1527 pathbuf_destroy(pb);
1528
1529 if ((*vpp)->v_type != VDIR)
1530 error = ENOTDIR;
1531 else
1532 error = VOP_ACCESS(*vpp, VEXEC, l->l_cred);
1533
1534 if (error)
1535 vput(*vpp);
1536 else
1537 VOP_UNLOCK(*vpp);
1538 return (error);
1539 }
1540
1541 /*
1542 * Internals of sys_open - path has already been converted into a pathbuf
1543 * (so we can easily reuse this function from other parts of the kernel,
1544 * like posix_spawn post-processing).
1545 */
1546 static int
1547 do_open(lwp_t *l, struct vnode *dvp, struct pathbuf *pb, int open_flags,
1548 int open_mode, int *fd)
1549 {
1550 struct proc *p = l->l_proc;
1551 struct cwdinfo *cwdi = p->p_cwdi;
1552 file_t *fp;
1553 struct vnode *vp;
1554 int flags, cmode;
1555 int indx, error;
1556 struct nameidata nd;
1557
1558 if (open_flags & O_SEARCH) {
1559 open_flags &= ~(int)O_SEARCH;
1560 }
1561
1562 flags = FFLAGS(open_flags);
1563 if ((flags & (FREAD | FWRITE)) == 0)
1564 return EINVAL;
1565
1566 if ((error = fd_allocfile(&fp, &indx)) != 0) {
1567 return error;
1568 }
1569
1570 /* We're going to read cwdi->cwdi_cmask unlocked here. */
1571 cmode = ((open_mode &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1572 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, pb);
1573 if (dvp != NULL)
1574 NDAT(&nd, dvp);
1575
1576 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1577 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1578 fd_abort(p, fp, indx);
1579 if ((error == EDUPFD || error == EMOVEFD) &&
1580 l->l_dupfd >= 0 && /* XXX from fdopen */
1581 (error =
1582 fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) {
1583 *fd = indx;
1584 return 0;
1585 }
1586 if (error == ERESTART)
1587 error = EINTR;
1588 return error;
1589 }
1590
1591 l->l_dupfd = 0;
1592 vp = nd.ni_vp;
1593
1594 if ((error = open_setfp(l, fp, vp, indx, flags)))
1595 return error;
1596
1597 VOP_UNLOCK(vp);
1598 *fd = indx;
1599 fd_affix(p, fp, indx);
1600 return 0;
1601 }
1602
1603 int
1604 fd_open(const char *path, int open_flags, int open_mode, int *fd)
1605 {
1606 struct pathbuf *pb;
1607 int error, oflags;
1608
1609 oflags = FFLAGS(open_flags);
1610 if ((oflags & (FREAD | FWRITE)) == 0)
1611 return EINVAL;
1612
1613 pb = pathbuf_create(path);
1614 if (pb == NULL)
1615 return ENOMEM;
1616
1617 error = do_open(curlwp, NULL, pb, open_flags, open_mode, fd);
1618 pathbuf_destroy(pb);
1619
1620 return error;
1621 }
1622
1623 /*
1624 * Check permissions, allocate an open file structure,
1625 * and call the device open routine if any.
1626 */
1627 static int
1628 do_sys_openat(lwp_t *l, int fdat, const char *path, int flags,
1629 int mode, int *fd)
1630 {
1631 file_t *dfp = NULL;
1632 struct vnode *dvp = NULL;
1633 struct pathbuf *pb;
1634 int error;
1635
1636 error = pathbuf_copyin(path, &pb);
1637 if (error)
1638 return error;
1639
1640 if (fdat != AT_FDCWD) {
1641 /* fd_getvnode() will use the descriptor for us */
1642 if ((error = fd_getvnode(fdat, &dfp)) != 0)
1643 goto out;
1644
1645 dvp = dfp->f_data;
1646 }
1647
1648 error = do_open(l, dvp, pb, flags, mode, fd);
1649
1650 if (dfp != NULL)
1651 fd_putfile(fdat);
1652 out:
1653 pathbuf_destroy(pb);
1654 return error;
1655 }
1656
1657 int
1658 sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
1659 {
1660 /* {
1661 syscallarg(const char *) path;
1662 syscallarg(int) flags;
1663 syscallarg(int) mode;
1664 } */
1665 int error;
1666 int fd;
1667
1668 error = do_sys_openat(l, AT_FDCWD, SCARG(uap, path),
1669 SCARG(uap, flags), SCARG(uap, mode), &fd);
1670
1671 if (error == 0)
1672 *retval = fd;
1673
1674 return error;
1675 }
1676
1677 int
1678 sys_openat(struct lwp *l, const struct sys_openat_args *uap, register_t *retval)
1679 {
1680 /* {
1681 syscallarg(int) fd;
1682 syscallarg(const char *) path;
1683 syscallarg(int) oflags;
1684 syscallarg(int) mode;
1685 } */
1686 int error;
1687 int fd;
1688
1689 error = do_sys_openat(l, SCARG(uap, fd), SCARG(uap, path),
1690 SCARG(uap, oflags), SCARG(uap, mode), &fd);
1691
1692 if (error == 0)
1693 *retval = fd;
1694
1695 return error;
1696 }
1697
1698 static void
1699 vfs__fhfree(fhandle_t *fhp)
1700 {
1701 size_t fhsize;
1702
1703 if (fhp == NULL) {
1704 return;
1705 }
1706 fhsize = FHANDLE_SIZE(fhp);
1707 kmem_free(fhp, fhsize);
1708 }
1709
1710 /*
1711 * vfs_composefh: compose a filehandle.
1712 */
1713
1714 int
1715 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1716 {
1717 struct mount *mp;
1718 struct fid *fidp;
1719 int error;
1720 size_t needfhsize;
1721 size_t fidsize;
1722
1723 mp = vp->v_mount;
1724 fidp = NULL;
1725 if (*fh_size < FHANDLE_SIZE_MIN) {
1726 fidsize = 0;
1727 } else {
1728 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1729 if (fhp != NULL) {
1730 memset(fhp, 0, *fh_size);
1731 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1732 fidp = &fhp->fh_fid;
1733 }
1734 }
1735 error = VFS_VPTOFH(vp, fidp, &fidsize);
1736 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1737 if (error == 0 && *fh_size < needfhsize) {
1738 error = E2BIG;
1739 }
1740 *fh_size = needfhsize;
1741 return error;
1742 }
1743
1744 int
1745 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1746 {
1747 struct mount *mp;
1748 fhandle_t *fhp;
1749 size_t fhsize;
1750 size_t fidsize;
1751 int error;
1752
1753 *fhpp = NULL;
1754 mp = vp->v_mount;
1755 fidsize = 0;
1756 error = VFS_VPTOFH(vp, NULL, &fidsize);
1757 KASSERT(error != 0);
1758 if (error != E2BIG) {
1759 goto out;
1760 }
1761 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1762 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1763 if (fhp == NULL) {
1764 error = ENOMEM;
1765 goto out;
1766 }
1767 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1768 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1769 if (error == 0) {
1770 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1771 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1772 *fhpp = fhp;
1773 } else {
1774 kmem_free(fhp, fhsize);
1775 }
1776 out:
1777 return error;
1778 }
1779
1780 void
1781 vfs_composefh_free(fhandle_t *fhp)
1782 {
1783
1784 vfs__fhfree(fhp);
1785 }
1786
1787 /*
1788 * vfs_fhtovp: lookup a vnode by a filehandle.
1789 */
1790
1791 int
1792 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1793 {
1794 struct mount *mp;
1795 int error;
1796
1797 *vpp = NULL;
1798 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1799 if (mp == NULL) {
1800 error = ESTALE;
1801 goto out;
1802 }
1803 if (mp->mnt_op->vfs_fhtovp == NULL) {
1804 error = EOPNOTSUPP;
1805 goto out;
1806 }
1807 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1808 out:
1809 return error;
1810 }
1811
1812 /*
1813 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1814 * the needed size.
1815 */
1816
1817 int
1818 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1819 {
1820 fhandle_t *fhp;
1821 int error;
1822
1823 *fhpp = NULL;
1824 if (fhsize > FHANDLE_SIZE_MAX) {
1825 return EINVAL;
1826 }
1827 if (fhsize < FHANDLE_SIZE_MIN) {
1828 return EINVAL;
1829 }
1830 again:
1831 fhp = kmem_alloc(fhsize, KM_SLEEP);
1832 if (fhp == NULL) {
1833 return ENOMEM;
1834 }
1835 error = copyin(ufhp, fhp, fhsize);
1836 if (error == 0) {
1837 /* XXX this check shouldn't be here */
1838 if (FHANDLE_SIZE(fhp) == fhsize) {
1839 *fhpp = fhp;
1840 return 0;
1841 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1842 /*
1843 * a kludge for nfsv2 padded handles.
1844 */
1845 size_t sz;
1846
1847 sz = FHANDLE_SIZE(fhp);
1848 kmem_free(fhp, fhsize);
1849 fhsize = sz;
1850 goto again;
1851 } else {
1852 /*
1853 * userland told us wrong size.
1854 */
1855 error = EINVAL;
1856 }
1857 }
1858 kmem_free(fhp, fhsize);
1859 return error;
1860 }
1861
1862 void
1863 vfs_copyinfh_free(fhandle_t *fhp)
1864 {
1865
1866 vfs__fhfree(fhp);
1867 }
1868
1869 /*
1870 * Get file handle system call
1871 */
1872 int
1873 sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
1874 {
1875 /* {
1876 syscallarg(char *) fname;
1877 syscallarg(fhandle_t *) fhp;
1878 syscallarg(size_t *) fh_size;
1879 } */
1880 struct vnode *vp;
1881 fhandle_t *fh;
1882 int error;
1883 struct pathbuf *pb;
1884 struct nameidata nd;
1885 size_t sz;
1886 size_t usz;
1887
1888 /*
1889 * Must be super user
1890 */
1891 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1892 0, NULL, NULL, NULL);
1893 if (error)
1894 return (error);
1895
1896 error = pathbuf_copyin(SCARG(uap, fname), &pb);
1897 if (error) {
1898 return error;
1899 }
1900 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1901 error = namei(&nd);
1902 if (error) {
1903 pathbuf_destroy(pb);
1904 return error;
1905 }
1906 vp = nd.ni_vp;
1907 pathbuf_destroy(pb);
1908
1909 error = vfs_composefh_alloc(vp, &fh);
1910 vput(vp);
1911 if (error != 0) {
1912 goto out;
1913 }
1914 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1915 if (error != 0) {
1916 goto out;
1917 }
1918 sz = FHANDLE_SIZE(fh);
1919 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1920 if (error != 0) {
1921 goto out;
1922 }
1923 if (usz >= sz) {
1924 error = copyout(fh, SCARG(uap, fhp), sz);
1925 } else {
1926 error = E2BIG;
1927 }
1928 out:
1929 vfs_composefh_free(fh);
1930 return (error);
1931 }
1932
1933 /*
1934 * Open a file given a file handle.
1935 *
1936 * Check permissions, allocate an open file structure,
1937 * and call the device open routine if any.
1938 */
1939
1940 int
1941 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1942 register_t *retval)
1943 {
1944 file_t *fp;
1945 struct vnode *vp = NULL;
1946 kauth_cred_t cred = l->l_cred;
1947 file_t *nfp;
1948 int indx, error = 0;
1949 struct vattr va;
1950 fhandle_t *fh;
1951 int flags;
1952 proc_t *p;
1953
1954 p = curproc;
1955
1956 /*
1957 * Must be super user
1958 */
1959 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1960 0, NULL, NULL, NULL)))
1961 return (error);
1962
1963 if (oflags & O_SEARCH) {
1964 oflags &= ~(int)O_SEARCH;
1965 }
1966
1967 flags = FFLAGS(oflags);
1968 if ((flags & (FREAD | FWRITE)) == 0)
1969 return (EINVAL);
1970 if ((flags & O_CREAT))
1971 return (EINVAL);
1972 if ((error = fd_allocfile(&nfp, &indx)) != 0)
1973 return (error);
1974 fp = nfp;
1975 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1976 if (error != 0) {
1977 goto bad;
1978 }
1979 error = vfs_fhtovp(fh, &vp);
1980 if (error != 0) {
1981 goto bad;
1982 }
1983
1984 /* Now do an effective vn_open */
1985
1986 if (vp->v_type == VSOCK) {
1987 error = EOPNOTSUPP;
1988 goto bad;
1989 }
1990 error = vn_openchk(vp, cred, flags);
1991 if (error != 0)
1992 goto bad;
1993 if (flags & O_TRUNC) {
1994 VOP_UNLOCK(vp); /* XXX */
1995 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1996 vattr_null(&va);
1997 va.va_size = 0;
1998 error = VOP_SETATTR(vp, &va, cred);
1999 if (error)
2000 goto bad;
2001 }
2002 if ((error = VOP_OPEN(vp, flags, cred)) != 0)
2003 goto bad;
2004 if (flags & FWRITE) {
2005 mutex_enter(vp->v_interlock);
2006 vp->v_writecount++;
2007 mutex_exit(vp->v_interlock);
2008 }
2009
2010 /* done with modified vn_open, now finish what sys_open does. */
2011 if ((error = open_setfp(l, fp, vp, indx, flags)))
2012 return error;
2013
2014 VOP_UNLOCK(vp);
2015 *retval = indx;
2016 fd_affix(p, fp, indx);
2017 vfs_copyinfh_free(fh);
2018 return (0);
2019
2020 bad:
2021 fd_abort(p, fp, indx);
2022 if (vp != NULL)
2023 vput(vp);
2024 vfs_copyinfh_free(fh);
2025 return (error);
2026 }
2027
2028 int
2029 sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
2030 {
2031 /* {
2032 syscallarg(const void *) fhp;
2033 syscallarg(size_t) fh_size;
2034 syscallarg(int) flags;
2035 } */
2036
2037 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
2038 SCARG(uap, flags), retval);
2039 }
2040
2041 int
2042 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
2043 {
2044 int error;
2045 fhandle_t *fh;
2046 struct vnode *vp;
2047
2048 /*
2049 * Must be super user
2050 */
2051 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
2052 0, NULL, NULL, NULL)))
2053 return (error);
2054
2055 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
2056 if (error != 0)
2057 return error;
2058
2059 error = vfs_fhtovp(fh, &vp);
2060 vfs_copyinfh_free(fh);
2061 if (error != 0)
2062 return error;
2063
2064 error = vn_stat(vp, sb);
2065 vput(vp);
2066 return error;
2067 }
2068
2069
2070 /* ARGSUSED */
2071 int
2072 sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval)
2073 {
2074 /* {
2075 syscallarg(const void *) fhp;
2076 syscallarg(size_t) fh_size;
2077 syscallarg(struct stat *) sb;
2078 } */
2079 struct stat sb;
2080 int error;
2081
2082 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
2083 if (error)
2084 return error;
2085 return copyout(&sb, SCARG(uap, sb), sizeof(sb));
2086 }
2087
2088 int
2089 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
2090 int flags)
2091 {
2092 fhandle_t *fh;
2093 struct mount *mp;
2094 struct vnode *vp;
2095 int error;
2096
2097 /*
2098 * Must be super user
2099 */
2100 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
2101 0, NULL, NULL, NULL)))
2102 return error;
2103
2104 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
2105 if (error != 0)
2106 return error;
2107
2108 error = vfs_fhtovp(fh, &vp);
2109 vfs_copyinfh_free(fh);
2110 if (error != 0)
2111 return error;
2112
2113 mp = vp->v_mount;
2114 error = dostatvfs(mp, sb, l, flags, 1);
2115 vput(vp);
2116 return error;
2117 }
2118
2119 /* ARGSUSED */
2120 int
2121 sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval)
2122 {
2123 /* {
2124 syscallarg(const void *) fhp;
2125 syscallarg(size_t) fh_size;
2126 syscallarg(struct statvfs *) buf;
2127 syscallarg(int) flags;
2128 } */
2129 struct statvfs *sb = STATVFSBUF_GET();
2130 int error;
2131
2132 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
2133 SCARG(uap, flags));
2134 if (error == 0)
2135 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
2136 STATVFSBUF_PUT(sb);
2137 return error;
2138 }
2139
2140 /*
2141 * Create a special file.
2142 */
2143 /* ARGSUSED */
2144 int
2145 sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap,
2146 register_t *retval)
2147 {
2148 /* {
2149 syscallarg(const char *) path;
2150 syscallarg(mode_t) mode;
2151 syscallarg(dev_t) dev;
2152 } */
2153 return do_sys_mknodat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode),
2154 SCARG(uap, dev), retval, UIO_USERSPACE);
2155 }
2156
2157 int
2158 sys_mknodat(struct lwp *l, const struct sys_mknodat_args *uap,
2159 register_t *retval)
2160 {
2161 /* {
2162 syscallarg(int) fd;
2163 syscallarg(const char *) path;
2164 syscallarg(mode_t) mode;
2165 syscallarg(int) pad;
2166 syscallarg(dev_t) dev;
2167 } */
2168
2169 return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
2170 SCARG(uap, mode), SCARG(uap, dev), retval, UIO_USERSPACE);
2171 }
2172
2173 int
2174 do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev,
2175 register_t *retval, enum uio_seg seg)
2176 {
2177 return do_sys_mknodat(l, AT_FDCWD, pathname, mode, dev, retval, seg);
2178 }
2179
2180 int
2181 do_sys_mknodat(struct lwp *l, int fdat, const char *pathname, mode_t mode,
2182 dev_t dev, register_t *retval, enum uio_seg seg)
2183 {
2184 struct proc *p = l->l_proc;
2185 struct vnode *vp;
2186 struct vattr vattr;
2187 int error, optype;
2188 struct pathbuf *pb;
2189 struct nameidata nd;
2190 const char *pathstring;
2191
2192 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
2193 0, NULL, NULL, NULL)) != 0)
2194 return (error);
2195
2196 optype = VOP_MKNOD_DESCOFFSET;
2197
2198 error = pathbuf_maybe_copyin(pathname, seg, &pb);
2199 if (error) {
2200 return error;
2201 }
2202 pathstring = pathbuf_stringcopy_get(pb);
2203 if (pathstring == NULL) {
2204 pathbuf_destroy(pb);
2205 return ENOMEM;
2206 }
2207
2208 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
2209
2210 if ((error = fd_nameiat(l, fdat, &nd)) != 0)
2211 goto out;
2212 vp = nd.ni_vp;
2213
2214 if (vp != NULL)
2215 error = EEXIST;
2216 else {
2217 vattr_null(&vattr);
2218 /* We will read cwdi->cwdi_cmask unlocked. */
2219 vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
2220 vattr.va_rdev = dev;
2221
2222 switch (mode & S_IFMT) {
2223 case S_IFMT: /* used by badsect to flag bad sectors */
2224 vattr.va_type = VBAD;
2225 break;
2226 case S_IFCHR:
2227 vattr.va_type = VCHR;
2228 break;
2229 case S_IFBLK:
2230 vattr.va_type = VBLK;
2231 break;
2232 case S_IFWHT:
2233 optype = VOP_WHITEOUT_DESCOFFSET;
2234 break;
2235 case S_IFREG:
2236 #if NVERIEXEC > 0
2237 error = veriexec_openchk(l, nd.ni_vp, pathstring,
2238 O_CREAT);
2239 #endif /* NVERIEXEC > 0 */
2240 vattr.va_type = VREG;
2241 vattr.va_rdev = VNOVAL;
2242 optype = VOP_CREATE_DESCOFFSET;
2243 break;
2244 default:
2245 error = EINVAL;
2246 break;
2247 }
2248 }
2249 if (error == 0 && optype == VOP_MKNOD_DESCOFFSET
2250 && vattr.va_rdev == VNOVAL)
2251 error = EINVAL;
2252 if (!error) {
2253 switch (optype) {
2254 case VOP_WHITEOUT_DESCOFFSET:
2255 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
2256 if (error)
2257 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2258 vput(nd.ni_dvp);
2259 break;
2260
2261 case VOP_MKNOD_DESCOFFSET:
2262 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
2263 &nd.ni_cnd, &vattr);
2264 if (error == 0)
2265 vput(nd.ni_vp);
2266 break;
2267
2268 case VOP_CREATE_DESCOFFSET:
2269 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
2270 &nd.ni_cnd, &vattr);
2271 if (error == 0)
2272 vput(nd.ni_vp);
2273 break;
2274 }
2275 } else {
2276 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2277 if (nd.ni_dvp == vp)
2278 vrele(nd.ni_dvp);
2279 else
2280 vput(nd.ni_dvp);
2281 if (vp)
2282 vrele(vp);
2283 }
2284 out:
2285 pathbuf_stringcopy_put(pb, pathstring);
2286 pathbuf_destroy(pb);
2287 return (error);
2288 }
2289
2290 /*
2291 * Create a named pipe.
2292 */
2293 /* ARGSUSED */
2294 int
2295 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
2296 {
2297 /* {
2298 syscallarg(const char *) path;
2299 syscallarg(int) mode;
2300 } */
2301 return do_sys_mkfifoat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode));
2302 }
2303
2304 int
2305 sys_mkfifoat(struct lwp *l, const struct sys_mkfifoat_args *uap,
2306 register_t *retval)
2307 {
2308 /* {
2309 syscallarg(int) fd;
2310 syscallarg(const char *) path;
2311 syscallarg(int) mode;
2312 } */
2313
2314 return do_sys_mkfifoat(l, SCARG(uap, fd), SCARG(uap, path),
2315 SCARG(uap, mode));
2316 }
2317
2318 static int
2319 do_sys_mkfifoat(struct lwp *l, int fdat, const char *path, mode_t mode)
2320 {
2321 struct proc *p = l->l_proc;
2322 struct vattr vattr;
2323 int error;
2324 struct pathbuf *pb;
2325 struct nameidata nd;
2326
2327 error = pathbuf_copyin(path, &pb);
2328 if (error) {
2329 return error;
2330 }
2331 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
2332
2333 if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
2334 pathbuf_destroy(pb);
2335 return error;
2336 }
2337 if (nd.ni_vp != NULL) {
2338 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2339 if (nd.ni_dvp == nd.ni_vp)
2340 vrele(nd.ni_dvp);
2341 else
2342 vput(nd.ni_dvp);
2343 vrele(nd.ni_vp);
2344 pathbuf_destroy(pb);
2345 return (EEXIST);
2346 }
2347 vattr_null(&vattr);
2348 vattr.va_type = VFIFO;
2349 /* We will read cwdi->cwdi_cmask unlocked. */
2350 vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
2351 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2352 if (error == 0)
2353 vput(nd.ni_vp);
2354 pathbuf_destroy(pb);
2355 return (error);
2356 }
2357
2358 /*
2359 * Make a hard file link.
2360 */
2361 /* ARGSUSED */
2362 static int
2363 do_sys_linkat(struct lwp *l, int fdpath, const char *path, int fdlink,
2364 const char *link, int follow, register_t *retval)
2365 {
2366 struct vnode *vp;
2367 struct pathbuf *linkpb;
2368 struct nameidata nd;
2369 namei_simple_flags_t ns_flags;
2370 int error;
2371
2372 if (follow & AT_SYMLINK_FOLLOW)
2373 ns_flags = NSM_FOLLOW_TRYEMULROOT;
2374 else
2375 ns_flags = NSM_NOFOLLOW_TRYEMULROOT;
2376
2377 error = fd_nameiat_simple_user(l, fdpath, path, ns_flags, &vp);
2378 if (error != 0)
2379 return (error);
2380 error = pathbuf_copyin(link, &linkpb);
2381 if (error) {
2382 goto out1;
2383 }
2384 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
2385 if ((error = fd_nameiat(l, fdlink, &nd)) != 0)
2386 goto out2;
2387 if (nd.ni_vp) {
2388 error = EEXIST;
2389 goto abortop;
2390 }
2391 /* Prevent hard links on directories. */
2392 if (vp->v_type == VDIR) {
2393 error = EPERM;
2394 goto abortop;
2395 }
2396 /* Prevent cross-mount operation. */
2397 if (nd.ni_dvp->v_mount != vp->v_mount) {
2398 error = EXDEV;
2399 goto abortop;
2400 }
2401 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2402 out2:
2403 pathbuf_destroy(linkpb);
2404 out1:
2405 vrele(vp);
2406 return (error);
2407 abortop:
2408 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2409 if (nd.ni_dvp == nd.ni_vp)
2410 vrele(nd.ni_dvp);
2411 else
2412 vput(nd.ni_dvp);
2413 if (nd.ni_vp != NULL)
2414 vrele(nd.ni_vp);
2415 goto out2;
2416 }
2417
2418 int
2419 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
2420 {
2421 /* {
2422 syscallarg(const char *) path;
2423 syscallarg(const char *) link;
2424 } */
2425 const char *path = SCARG(uap, path);
2426 const char *link = SCARG(uap, link);
2427
2428 return do_sys_linkat(l, AT_FDCWD, path, AT_FDCWD, link,
2429 AT_SYMLINK_FOLLOW, retval);
2430 }
2431
2432 int
2433 sys_linkat(struct lwp *l, const struct sys_linkat_args *uap,
2434 register_t *retval)
2435 {
2436 /* {
2437 syscallarg(int) fd1;
2438 syscallarg(const char *) name1;
2439 syscallarg(int) fd2;
2440 syscallarg(const char *) name2;
2441 syscallarg(int) flags;
2442 } */
2443 int fd1 = SCARG(uap, fd1);
2444 const char *name1 = SCARG(uap, name1);
2445 int fd2 = SCARG(uap, fd2);
2446 const char *name2 = SCARG(uap, name2);
2447 int follow;
2448
2449 follow = SCARG(uap, flags) & AT_SYMLINK_FOLLOW;
2450
2451 return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
2452 }
2453
2454
2455 int
2456 do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
2457 {
2458 return do_sys_symlinkat(NULL, patharg, AT_FDCWD, link, seg);
2459 }
2460
2461 static int
2462 do_sys_symlinkat(struct lwp *l, const char *patharg, int fdat,
2463 const char *link, enum uio_seg seg)
2464 {
2465 struct proc *p = curproc;
2466 struct vattr vattr;
2467 char *path;
2468 int error;
2469 struct pathbuf *linkpb;
2470 struct nameidata nd;
2471
2472 KASSERT(l != NULL || fdat == AT_FDCWD);
2473
2474 path = PNBUF_GET();
2475 if (seg == UIO_USERSPACE) {
2476 if ((error = copyinstr(patharg, path, MAXPATHLEN, NULL)) != 0)
2477 goto out1;
2478 if ((error = pathbuf_copyin(link, &linkpb)) != 0)
2479 goto out1;
2480 } else {
2481 KASSERT(strlen(patharg) < MAXPATHLEN);
2482 strcpy(path, patharg);
2483 linkpb = pathbuf_create(link);
2484 if (linkpb == NULL) {
2485 error = ENOMEM;
2486 goto out1;
2487 }
2488 }
2489 ktrkuser("symlink-target", path, strlen(path));
2490
2491 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
2492 if ((error = fd_nameiat(l, fdat, &nd)) != 0)
2493 goto out2;
2494 if (nd.ni_vp) {
2495 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2496 if (nd.ni_dvp == nd.ni_vp)
2497 vrele(nd.ni_dvp);
2498 else
2499 vput(nd.ni_dvp);
2500 vrele(nd.ni_vp);
2501 error = EEXIST;
2502 goto out2;
2503 }
2504 vattr_null(&vattr);
2505 vattr.va_type = VLNK;
2506 /* We will read cwdi->cwdi_cmask unlocked. */
2507 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
2508 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
2509 if (error == 0)
2510 vput(nd.ni_vp);
2511 out2:
2512 pathbuf_destroy(linkpb);
2513 out1:
2514 PNBUF_PUT(path);
2515 return (error);
2516 }
2517
2518 /*
2519 * Make a symbolic link.
2520 */
2521 /* ARGSUSED */
2522 int
2523 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
2524 {
2525 /* {
2526 syscallarg(const char *) path;
2527 syscallarg(const char *) link;
2528 } */
2529
2530 return do_sys_symlinkat(l, SCARG(uap, path), AT_FDCWD, SCARG(uap, link),
2531 UIO_USERSPACE);
2532 }
2533
2534 int
2535 sys_symlinkat(struct lwp *l, const struct sys_symlinkat_args *uap,
2536 register_t *retval)
2537 {
2538 /* {
2539 syscallarg(const char *) path1;
2540 syscallarg(int) fd;
2541 syscallarg(const char *) path2;
2542 } */
2543
2544 return do_sys_symlinkat(l, SCARG(uap, path1), SCARG(uap, fd),
2545 SCARG(uap, path2), UIO_USERSPACE);
2546 }
2547
2548 /*
2549 * Delete a whiteout from the filesystem.
2550 */
2551 /* ARGSUSED */
2552 int
2553 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
2554 {
2555 /* {
2556 syscallarg(const char *) path;
2557 } */
2558 int error;
2559 struct pathbuf *pb;
2560 struct nameidata nd;
2561
2562 error = pathbuf_copyin(SCARG(uap, path), &pb);
2563 if (error) {
2564 return error;
2565 }
2566
2567 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
2568 error = namei(&nd);
2569 if (error) {
2570 pathbuf_destroy(pb);
2571 return (error);
2572 }
2573
2574 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2575 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2576 if (nd.ni_dvp == nd.ni_vp)
2577 vrele(nd.ni_dvp);
2578 else
2579 vput(nd.ni_dvp);
2580 if (nd.ni_vp)
2581 vrele(nd.ni_vp);
2582 pathbuf_destroy(pb);
2583 return (EEXIST);
2584 }
2585 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
2586 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2587 vput(nd.ni_dvp);
2588 pathbuf_destroy(pb);
2589 return (error);
2590 }
2591
2592 /*
2593 * Delete a name from the filesystem.
2594 */
2595 /* ARGSUSED */
2596 int
2597 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
2598 {
2599 /* {
2600 syscallarg(const char *) path;
2601 } */
2602
2603 return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path), 0, UIO_USERSPACE);
2604 }
2605
2606 int
2607 sys_unlinkat(struct lwp *l, const struct sys_unlinkat_args *uap,
2608 register_t *retval)
2609 {
2610 /* {
2611 syscallarg(int) fd;
2612 syscallarg(const char *) path;
2613 syscallarg(int) flag;
2614 } */
2615
2616 return do_sys_unlinkat(l, SCARG(uap, fd), SCARG(uap, path),
2617 SCARG(uap, flag), UIO_USERSPACE);
2618 }
2619
2620 int
2621 do_sys_unlink(const char *arg, enum uio_seg seg)
2622 {
2623 return do_sys_unlinkat(NULL, AT_FDCWD, arg, 0, seg);
2624 }
2625
2626 static int
2627 do_sys_unlinkat(struct lwp *l, int fdat, const char *arg, int flags,
2628 enum uio_seg seg)
2629 {
2630 struct vnode *vp;
2631 int error;
2632 struct pathbuf *pb;
2633 struct nameidata nd;
2634 const char *pathstring;
2635
2636 KASSERT(l != NULL || fdat == AT_FDCWD);
2637
2638 error = pathbuf_maybe_copyin(arg, seg, &pb);
2639 if (error) {
2640 return error;
2641 }
2642 pathstring = pathbuf_stringcopy_get(pb);
2643 if (pathstring == NULL) {
2644 pathbuf_destroy(pb);
2645 return ENOMEM;
2646 }
2647
2648 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
2649 if ((error = fd_nameiat(l, fdat, &nd)) != 0)
2650 goto out;
2651 vp = nd.ni_vp;
2652
2653 /*
2654 * The root of a mounted filesystem cannot be deleted.
2655 */
2656 if ((vp->v_vflag & VV_ROOT) != 0) {
2657 error = EBUSY;
2658 goto abort;
2659 }
2660
2661 if ((vp->v_type == VDIR) && (vp->v_mountedhere != NULL)) {
2662 error = EBUSY;
2663 goto abort;
2664 }
2665
2666 /*
2667 * No rmdir "." please.
2668 */
2669 if (nd.ni_dvp == vp) {
2670 error = EINVAL;
2671 goto abort;
2672 }
2673
2674 /*
2675 * AT_REMOVEDIR is required to remove a directory
2676 */
2677 if (vp->v_type == VDIR) {
2678 if (!(flags & AT_REMOVEDIR)) {
2679 error = EPERM;
2680 goto abort;
2681 } else {
2682 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2683 goto out;
2684 }
2685 }
2686
2687 /*
2688 * Starting here we only deal with non directories.
2689 */
2690 if (flags & AT_REMOVEDIR) {
2691 error = ENOTDIR;
2692 goto abort;
2693 }
2694
2695
2696 #if NVERIEXEC > 0
2697 /* Handle remove requests for veriexec entries. */
2698 if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
2699 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2700 if (nd.ni_dvp == vp)
2701 vrele(nd.ni_dvp);
2702 else
2703 vput(nd.ni_dvp);
2704 vput(vp);
2705 goto out;
2706 }
2707 #endif /* NVERIEXEC > 0 */
2708
2709 #ifdef FILEASSOC
2710 (void)fileassoc_file_delete(vp);
2711 #endif /* FILEASSOC */
2712 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2713 goto out;
2714
2715 abort:
2716 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2717 if (nd.ni_dvp == vp)
2718 vrele(nd.ni_dvp);
2719 else
2720 vput(nd.ni_dvp);
2721 vput(vp);
2722
2723 out:
2724 pathbuf_stringcopy_put(pb, pathstring);
2725 pathbuf_destroy(pb);
2726 return (error);
2727 }
2728
2729 /*
2730 * Reposition read/write file offset.
2731 */
2732 int
2733 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
2734 {
2735 /* {
2736 syscallarg(int) fd;
2737 syscallarg(int) pad;
2738 syscallarg(off_t) offset;
2739 syscallarg(int) whence;
2740 } */
2741 kauth_cred_t cred = l->l_cred;
2742 file_t *fp;
2743 struct vnode *vp;
2744 struct vattr vattr;
2745 off_t newoff;
2746 int error, fd;
2747
2748 fd = SCARG(uap, fd);
2749
2750 if ((fp = fd_getfile(fd)) == NULL)
2751 return (EBADF);
2752
2753 vp = fp->f_data;
2754 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2755 error = ESPIPE;
2756 goto out;
2757 }
2758
2759 switch (SCARG(uap, whence)) {
2760 case SEEK_CUR:
2761 newoff = fp->f_offset + SCARG(uap, offset);
2762 break;
2763 case SEEK_END:
2764 vn_lock(vp, LK_SHARED | LK_RETRY);
2765 error = VOP_GETATTR(vp, &vattr, cred);
2766 VOP_UNLOCK(vp);
2767 if (error) {
2768 goto out;
2769 }
2770 newoff = SCARG(uap, offset) + vattr.va_size;
2771 break;
2772 case SEEK_SET:
2773 newoff = SCARG(uap, offset);
2774 break;
2775 default:
2776 error = EINVAL;
2777 goto out;
2778 }
2779 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2780 *(off_t *)retval = fp->f_offset = newoff;
2781 }
2782 out:
2783 fd_putfile(fd);
2784 return (error);
2785 }
2786
2787 /*
2788 * Positional read system call.
2789 */
2790 int
2791 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
2792 {
2793 /* {
2794 syscallarg(int) fd;
2795 syscallarg(void *) buf;
2796 syscallarg(size_t) nbyte;
2797 syscallarg(off_t) offset;
2798 } */
2799 file_t *fp;
2800 struct vnode *vp;
2801 off_t offset;
2802 int error, fd = SCARG(uap, fd);
2803
2804 if ((fp = fd_getfile(fd)) == NULL)
2805 return (EBADF);
2806
2807 if ((fp->f_flag & FREAD) == 0) {
2808 fd_putfile(fd);
2809 return (EBADF);
2810 }
2811
2812 vp = fp->f_data;
2813 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2814 error = ESPIPE;
2815 goto out;
2816 }
2817
2818 offset = SCARG(uap, offset);
2819
2820 /*
2821 * XXX This works because no file systems actually
2822 * XXX take any action on the seek operation.
2823 */
2824 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2825 goto out;
2826
2827 /* dofileread() will unuse the descriptor for us */
2828 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2829 &offset, 0, retval));
2830
2831 out:
2832 fd_putfile(fd);
2833 return (error);
2834 }
2835
2836 /*
2837 * Positional scatter read system call.
2838 */
2839 int
2840 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
2841 {
2842 /* {
2843 syscallarg(int) fd;
2844 syscallarg(const struct iovec *) iovp;
2845 syscallarg(int) iovcnt;
2846 syscallarg(off_t) offset;
2847 } */
2848 off_t offset = SCARG(uap, offset);
2849
2850 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
2851 SCARG(uap, iovcnt), &offset, 0, retval);
2852 }
2853
2854 /*
2855 * Positional write system call.
2856 */
2857 int
2858 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
2859 {
2860 /* {
2861 syscallarg(int) fd;
2862 syscallarg(const void *) buf;
2863 syscallarg(size_t) nbyte;
2864 syscallarg(off_t) offset;
2865 } */
2866 file_t *fp;
2867 struct vnode *vp;
2868 off_t offset;
2869 int error, fd = SCARG(uap, fd);
2870
2871 if ((fp = fd_getfile(fd)) == NULL)
2872 return (EBADF);
2873
2874 if ((fp->f_flag & FWRITE) == 0) {
2875 fd_putfile(fd);
2876 return (EBADF);
2877 }
2878
2879 vp = fp->f_data;
2880 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2881 error = ESPIPE;
2882 goto out;
2883 }
2884
2885 offset = SCARG(uap, offset);
2886
2887 /*
2888 * XXX This works because no file systems actually
2889 * XXX take any action on the seek operation.
2890 */
2891 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2892 goto out;
2893
2894 /* dofilewrite() will unuse the descriptor for us */
2895 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2896 &offset, 0, retval));
2897
2898 out:
2899 fd_putfile(fd);
2900 return (error);
2901 }
2902
2903 /*
2904 * Positional gather write system call.
2905 */
2906 int
2907 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
2908 {
2909 /* {
2910 syscallarg(int) fd;
2911 syscallarg(const struct iovec *) iovp;
2912 syscallarg(int) iovcnt;
2913 syscallarg(off_t) offset;
2914 } */
2915 off_t offset = SCARG(uap, offset);
2916
2917 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
2918 SCARG(uap, iovcnt), &offset, 0, retval);
2919 }
2920
2921 /*
2922 * Check access permissions.
2923 */
2924 int
2925 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
2926 {
2927 /* {
2928 syscallarg(const char *) path;
2929 syscallarg(int) flags;
2930 } */
2931
2932 return do_sys_accessat(l, AT_FDCWD, SCARG(uap, path),
2933 SCARG(uap, flags), 0);
2934 }
2935
2936 static int
2937 do_sys_accessat(struct lwp *l, int fdat, const char *path,
2938 int mode, int flags)
2939 {
2940 kauth_cred_t cred;
2941 struct vnode *vp;
2942 int error, nd_flag, vmode;
2943 struct pathbuf *pb;
2944 struct nameidata nd;
2945
2946 CTASSERT(F_OK == 0);
2947 if ((mode & ~(R_OK | W_OK | X_OK)) != 0) {
2948 /* nonsense mode */
2949 return EINVAL;
2950 }
2951
2952 nd_flag = FOLLOW | LOCKLEAF | TRYEMULROOT;
2953 if (flags & AT_SYMLINK_NOFOLLOW)
2954 nd_flag &= ~FOLLOW;
2955
2956 error = pathbuf_copyin(path, &pb);
2957 if (error)
2958 return error;
2959
2960 NDINIT(&nd, LOOKUP, nd_flag, pb);
2961
2962 /* Override default credentials */
2963 cred = kauth_cred_dup(l->l_cred);
2964 if (!(flags & AT_EACCESS)) {
2965 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2966 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2967 }
2968 nd.ni_cnd.cn_cred = cred;
2969
2970 if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
2971 pathbuf_destroy(pb);
2972 goto out;
2973 }
2974 vp = nd.ni_vp;
2975 pathbuf_destroy(pb);
2976
2977 /* Flags == 0 means only check for existence. */
2978 if (mode) {
2979 vmode = 0;
2980 if (mode & R_OK)
2981 vmode |= VREAD;
2982 if (mode & W_OK)
2983 vmode |= VWRITE;
2984 if (mode & X_OK)
2985 vmode |= VEXEC;
2986
2987 error = VOP_ACCESS(vp, vmode, cred);
2988 if (!error && (vmode & VWRITE))
2989 error = vn_writechk(vp);
2990 }
2991 vput(vp);
2992 out:
2993 kauth_cred_free(cred);
2994 return (error);
2995 }
2996
2997 int
2998 sys_faccessat(struct lwp *l, const struct sys_faccessat_args *uap,
2999 register_t *retval)
3000 {
3001 /* {
3002 syscallarg(int) fd;
3003 syscallarg(const char *) path;
3004 syscallarg(int) amode;
3005 syscallarg(int) flag;
3006 } */
3007
3008 return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
3009 SCARG(uap, amode), SCARG(uap, flag));
3010 }
3011
3012 /*
3013 * Common code for all sys_stat functions, including compat versions.
3014 */
3015 int
3016 do_sys_stat(const char *userpath, unsigned int nd_flag,
3017 struct stat *sb)
3018 {
3019 return do_sys_statat(NULL, AT_FDCWD, userpath, nd_flag, sb);
3020 }
3021
3022 int
3023 do_sys_statat(struct lwp *l, int fdat, const char *userpath,
3024 unsigned int nd_flag, struct stat *sb)
3025 {
3026 int error;
3027 struct pathbuf *pb;
3028 struct nameidata nd;
3029
3030 KASSERT(l != NULL || fdat == AT_FDCWD);
3031
3032 error = pathbuf_copyin(userpath, &pb);
3033 if (error) {
3034 return error;
3035 }
3036
3037 NDINIT(&nd, LOOKUP, nd_flag | LOCKLEAF | TRYEMULROOT, pb);
3038
3039 error = fd_nameiat(l, fdat, &nd);
3040 if (error != 0) {
3041 pathbuf_destroy(pb);
3042 return error;
3043 }
3044 error = vn_stat(nd.ni_vp, sb);
3045 vput(nd.ni_vp);
3046 pathbuf_destroy(pb);
3047 return error;
3048 }
3049
3050 /*
3051 * Get file status; this version follows links.
3052 */
3053 /* ARGSUSED */
3054 int
3055 sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval)
3056 {
3057 /* {
3058 syscallarg(const char *) path;
3059 syscallarg(struct stat *) ub;
3060 } */
3061 struct stat sb;
3062 int error;
3063
3064 error = do_sys_statat(l, AT_FDCWD, SCARG(uap, path), FOLLOW, &sb);
3065 if (error)
3066 return error;
3067 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
3068 }
3069
3070 /*
3071 * Get file status; this version does not follow links.
3072 */
3073 /* ARGSUSED */
3074 int
3075 sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval)
3076 {
3077 /* {
3078 syscallarg(const char *) path;
3079 syscallarg(struct stat *) ub;
3080 } */
3081 struct stat sb;
3082 int error;
3083
3084 error = do_sys_statat(l, AT_FDCWD, SCARG(uap, path), NOFOLLOW, &sb);
3085 if (error)
3086 return error;
3087 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
3088 }
3089
3090 int
3091 sys_fstatat(struct lwp *l, const struct sys_fstatat_args *uap,
3092 register_t *retval)
3093 {
3094 /* {
3095 syscallarg(int) fd;
3096 syscallarg(const char *) path;
3097 syscallarg(struct stat *) buf;
3098 syscallarg(int) flag;
3099 } */
3100 unsigned int nd_flag;
3101 struct stat sb;
3102 int error;
3103
3104 if (SCARG(uap, flag) & AT_SYMLINK_NOFOLLOW)
3105 nd_flag = NOFOLLOW;
3106 else
3107 nd_flag = FOLLOW;
3108
3109 error = do_sys_statat(l, SCARG(uap, fd), SCARG(uap, path), nd_flag,
3110 &sb);
3111 if (error)
3112 return error;
3113 return copyout(&sb, SCARG(uap, buf), sizeof(sb));
3114 }
3115
3116 /*
3117 * Get configurable pathname variables.
3118 */
3119 /* ARGSUSED */
3120 int
3121 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
3122 {
3123 /* {
3124 syscallarg(const char *) path;
3125 syscallarg(int) name;
3126 } */
3127 int error;
3128 struct pathbuf *pb;
3129 struct nameidata nd;
3130
3131 error = pathbuf_copyin(SCARG(uap, path), &pb);
3132 if (error) {
3133 return error;
3134 }
3135 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
3136 if ((error = namei(&nd)) != 0) {
3137 pathbuf_destroy(pb);
3138 return (error);
3139 }
3140 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
3141 vput(nd.ni_vp);
3142 pathbuf_destroy(pb);
3143 return (error);
3144 }
3145
3146 /*
3147 * Return target name of a symbolic link.
3148 */
3149 /* ARGSUSED */
3150 int
3151 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap,
3152 register_t *retval)
3153 {
3154 /* {
3155 syscallarg(const char *) path;
3156 syscallarg(char *) buf;
3157 syscallarg(size_t) count;
3158 } */
3159 return do_sys_readlinkat(l, AT_FDCWD, SCARG(uap, path),
3160 SCARG(uap, buf), SCARG(uap, count), retval);
3161 }
3162
3163 static int
3164 do_sys_readlinkat(struct lwp *l, int fdat, const char *path, char *buf,
3165 size_t count, register_t *retval)
3166 {
3167 struct vnode *vp;
3168 struct iovec aiov;
3169 struct uio auio;
3170 int error;
3171 struct pathbuf *pb;
3172 struct nameidata nd;
3173
3174 error = pathbuf_copyin(path, &pb);
3175 if (error) {
3176 return error;
3177 }
3178 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
3179 if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
3180 pathbuf_destroy(pb);
3181 return error;
3182 }
3183 vp = nd.ni_vp;
3184 pathbuf_destroy(pb);
3185 if (vp->v_type != VLNK)
3186 error = EINVAL;
3187 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
3188 (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
3189 aiov.iov_base = buf;
3190 aiov.iov_len = count;
3191 auio.uio_iov = &aiov;
3192 auio.uio_iovcnt = 1;
3193 auio.uio_offset = 0;
3194 auio.uio_rw = UIO_READ;
3195 KASSERT(l == curlwp);
3196 auio.uio_vmspace = l->l_proc->p_vmspace;
3197 auio.uio_resid = count;
3198 if ((error = VOP_READLINK(vp, &auio, l->l_cred)) == 0)
3199 *retval = count - auio.uio_resid;
3200 }
3201 vput(vp);
3202 return (error);
3203 }
3204
3205 int
3206 sys_readlinkat(struct lwp *l, const struct sys_readlinkat_args *uap,
3207 register_t *retval)
3208 {
3209 /* {
3210 syscallarg(int) fd;
3211 syscallarg(const char *) path;
3212 syscallarg(char *) buf;
3213 syscallarg(size_t) bufsize;
3214 } */
3215
3216 return do_sys_readlinkat(l, SCARG(uap, fd), SCARG(uap, path),
3217 SCARG(uap, buf), SCARG(uap, bufsize), retval);
3218 }
3219
3220 /*
3221 * Change flags of a file given a path name.
3222 */
3223 /* ARGSUSED */
3224 int
3225 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
3226 {
3227 /* {
3228 syscallarg(const char *) path;
3229 syscallarg(u_long) flags;
3230 } */
3231 struct vnode *vp;
3232 int error;
3233
3234 error = namei_simple_user(SCARG(uap, path),
3235 NSM_FOLLOW_TRYEMULROOT, &vp);
3236 if (error != 0)
3237 return (error);
3238 error = change_flags(vp, SCARG(uap, flags), l);
3239 vput(vp);
3240 return (error);
3241 }
3242
3243 /*
3244 * Change flags of a file given a file descriptor.
3245 */
3246 /* ARGSUSED */
3247 int
3248 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
3249 {
3250 /* {
3251 syscallarg(int) fd;
3252 syscallarg(u_long) flags;
3253 } */
3254 struct vnode *vp;
3255 file_t *fp;
3256 int error;
3257
3258 /* fd_getvnode() will use the descriptor for us */
3259 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3260 return (error);
3261 vp = fp->f_data;
3262 error = change_flags(vp, SCARG(uap, flags), l);
3263 VOP_UNLOCK(vp);
3264 fd_putfile(SCARG(uap, fd));
3265 return (error);
3266 }
3267
3268 /*
3269 * Change flags of a file given a path name; this version does
3270 * not follow links.
3271 */
3272 int
3273 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
3274 {
3275 /* {
3276 syscallarg(const char *) path;
3277 syscallarg(u_long) flags;
3278 } */
3279 struct vnode *vp;
3280 int error;
3281
3282 error = namei_simple_user(SCARG(uap, path),
3283 NSM_NOFOLLOW_TRYEMULROOT, &vp);
3284 if (error != 0)
3285 return (error);
3286 error = change_flags(vp, SCARG(uap, flags), l);
3287 vput(vp);
3288 return (error);
3289 }
3290
3291 /*
3292 * Common routine to change flags of a file.
3293 */
3294 int
3295 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
3296 {
3297 struct vattr vattr;
3298 int error;
3299
3300 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3301
3302 vattr_null(&vattr);
3303 vattr.va_flags = flags;
3304 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3305
3306 return (error);
3307 }
3308
3309 /*
3310 * Change mode of a file given path name; this version follows links.
3311 */
3312 /* ARGSUSED */
3313 int
3314 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
3315 {
3316 /* {
3317 syscallarg(const char *) path;
3318 syscallarg(int) mode;
3319 } */
3320 return do_sys_chmodat(l, AT_FDCWD, SCARG(uap, path),
3321 SCARG(uap, mode), 0);
3322 }
3323
3324 static int
3325 do_sys_chmodat(struct lwp *l, int fdat, const char *path, int mode, int flags)
3326 {
3327 int error;
3328 struct vnode *vp;
3329 namei_simple_flags_t ns_flag;
3330
3331 if (flags & AT_SYMLINK_NOFOLLOW)
3332 ns_flag = NSM_NOFOLLOW_TRYEMULROOT;
3333 else
3334 ns_flag = NSM_FOLLOW_TRYEMULROOT;
3335
3336 error = fd_nameiat_simple_user(l, fdat, path, ns_flag, &vp);
3337 if (error != 0)
3338 return error;
3339
3340 error = change_mode(vp, mode, l);
3341
3342 vrele(vp);
3343
3344 return (error);
3345 }
3346
3347 /*
3348 * Change mode of a file given a file descriptor.
3349 */
3350 /* ARGSUSED */
3351 int
3352 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
3353 {
3354 /* {
3355 syscallarg(int) fd;
3356 syscallarg(int) mode;
3357 } */
3358 file_t *fp;
3359 int error;
3360
3361 /* fd_getvnode() will use the descriptor for us */
3362 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3363 return (error);
3364 error = change_mode(fp->f_data, SCARG(uap, mode), l);
3365 fd_putfile(SCARG(uap, fd));
3366 return (error);
3367 }
3368
3369 int
3370 sys_fchmodat(struct lwp *l, const struct sys_fchmodat_args *uap,
3371 register_t *retval)
3372 {
3373 /* {
3374 syscallarg(int) fd;
3375 syscallarg(const char *) path;
3376 syscallarg(int) mode;
3377 syscallarg(int) flag;
3378 } */
3379
3380 return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
3381 SCARG(uap, mode), SCARG(uap, flag));
3382 }
3383
3384 /*
3385 * Change mode of a file given path name; this version does not follow links.
3386 */
3387 /* ARGSUSED */
3388 int
3389 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
3390 {
3391 /* {
3392 syscallarg(const char *) path;
3393 syscallarg(int) mode;
3394 } */
3395 int error;
3396 struct vnode *vp;
3397
3398 error = namei_simple_user(SCARG(uap, path),
3399 NSM_NOFOLLOW_TRYEMULROOT, &vp);
3400 if (error != 0)
3401 return (error);
3402
3403 error = change_mode(vp, SCARG(uap, mode), l);
3404
3405 vrele(vp);
3406 return (error);
3407 }
3408
3409 /*
3410 * Common routine to set mode given a vnode.
3411 */
3412 static int
3413 change_mode(struct vnode *vp, int mode, struct lwp *l)
3414 {
3415 struct vattr vattr;
3416 int error;
3417
3418 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3419 vattr_null(&vattr);
3420 vattr.va_mode = mode & ALLPERMS;
3421 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3422 VOP_UNLOCK(vp);
3423 return (error);
3424 }
3425
3426 /*
3427 * Set ownership given a path name; this version follows links.
3428 */
3429 /* ARGSUSED */
3430 int
3431 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
3432 {
3433 /* {
3434 syscallarg(const char *) path;
3435 syscallarg(uid_t) uid;
3436 syscallarg(gid_t) gid;
3437 } */
3438 return do_sys_chownat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap,uid),
3439 SCARG(uap, gid), 0);
3440 }
3441
3442 static int
3443 do_sys_chownat(struct lwp *l, int fdat, const char *path, uid_t uid,
3444 gid_t gid, int flags)
3445 {
3446 int error;
3447 struct vnode *vp;
3448 namei_simple_flags_t ns_flag;
3449
3450 if (flags & AT_SYMLINK_NOFOLLOW)
3451 ns_flag = NSM_NOFOLLOW_TRYEMULROOT;
3452 else
3453 ns_flag = NSM_FOLLOW_TRYEMULROOT;
3454
3455 error = fd_nameiat_simple_user(l, fdat, path, ns_flag, &vp);
3456 if (error != 0)
3457 return error;
3458
3459 error = change_owner(vp, uid, gid, l, 0);
3460
3461 vrele(vp);
3462
3463 return (error);
3464 }
3465
3466 /*
3467 * Set ownership given a path name; this version follows links.
3468 * Provides POSIX semantics.
3469 */
3470 /* ARGSUSED */
3471 int
3472 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
3473 {
3474 /* {
3475 syscallarg(const char *) path;
3476 syscallarg(uid_t) uid;
3477 syscallarg(gid_t) gid;
3478 } */
3479 int error;
3480 struct vnode *vp;
3481
3482 error = namei_simple_user(SCARG(uap, path),
3483 NSM_FOLLOW_TRYEMULROOT, &vp);
3484 if (error != 0)
3485 return (error);
3486
3487 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
3488
3489 vrele(vp);
3490 return (error);
3491 }
3492
3493 /*
3494 * Set ownership given a file descriptor.
3495 */
3496 /* ARGSUSED */
3497 int
3498 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
3499 {
3500 /* {
3501 syscallarg(int) fd;
3502 syscallarg(uid_t) uid;
3503 syscallarg(gid_t) gid;
3504 } */
3505 int error;
3506 file_t *fp;
3507
3508 /* fd_getvnode() will use the descriptor for us */
3509 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3510 return (error);
3511 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
3512 l, 0);
3513 fd_putfile(SCARG(uap, fd));
3514 return (error);
3515 }
3516
3517 int
3518 sys_fchownat(struct lwp *l, const struct sys_fchownat_args *uap,
3519 register_t *retval)
3520 {
3521 /* {
3522 syscallarg(int) fd;
3523 syscallarg(const char *) path;
3524 syscallarg(uid_t) owner;
3525 syscallarg(gid_t) group;
3526 syscallarg(int) flag;
3527 } */
3528
3529 return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
3530 SCARG(uap, owner), SCARG(uap, group),
3531 SCARG(uap, flag));
3532 }
3533
3534 /*
3535 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
3536 */
3537 /* ARGSUSED */
3538 int
3539 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
3540 {
3541 /* {
3542 syscallarg(int) fd;
3543 syscallarg(uid_t) uid;
3544 syscallarg(gid_t) gid;
3545 } */
3546 int error;
3547 file_t *fp;
3548
3549 /* fd_getvnode() will use the descriptor for us */
3550 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3551 return (error);
3552 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
3553 l, 1);
3554 fd_putfile(SCARG(uap, fd));
3555 return (error);
3556 }
3557
3558 /*
3559 * Set ownership given a path name; this version does not follow links.
3560 */
3561 /* ARGSUSED */
3562 int
3563 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
3564 {
3565 /* {
3566 syscallarg(const char *) path;
3567 syscallarg(uid_t) uid;
3568 syscallarg(gid_t) gid;
3569 } */
3570 int error;
3571 struct vnode *vp;
3572
3573 error = namei_simple_user(SCARG(uap, path),
3574 NSM_NOFOLLOW_TRYEMULROOT, &vp);
3575 if (error != 0)
3576 return (error);
3577
3578 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
3579
3580 vrele(vp);
3581 return (error);
3582 }
3583
3584 /*
3585 * Set ownership given a path name; this version does not follow links.
3586 * Provides POSIX/XPG semantics.
3587 */
3588 /* ARGSUSED */
3589 int
3590 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
3591 {
3592 /* {
3593 syscallarg(const char *) path;
3594 syscallarg(uid_t) uid;
3595 syscallarg(gid_t) gid;
3596 } */
3597 int error;
3598 struct vnode *vp;
3599
3600 error = namei_simple_user(SCARG(uap, path),
3601 NSM_NOFOLLOW_TRYEMULROOT, &vp);
3602 if (error != 0)
3603 return (error);
3604
3605 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
3606
3607 vrele(vp);
3608 return (error);
3609 }
3610
3611 /*
3612 * Common routine to set ownership given a vnode.
3613 */
3614 static int
3615 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
3616 int posix_semantics)
3617 {
3618 struct vattr vattr;
3619 mode_t newmode;
3620 int error;
3621
3622 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3623 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
3624 goto out;
3625
3626 #define CHANGED(x) ((int)(x) != -1)
3627 newmode = vattr.va_mode;
3628 if (posix_semantics) {
3629 /*
3630 * POSIX/XPG semantics: if the caller is not the super-user,
3631 * clear set-user-id and set-group-id bits. Both POSIX and
3632 * the XPG consider the behaviour for calls by the super-user
3633 * implementation-defined; we leave the set-user-id and set-
3634 * group-id settings intact in that case.
3635 */
3636 if (vattr.va_mode & S_ISUID) {
3637 if (kauth_authorize_vnode(l->l_cred,
3638 KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
3639 newmode &= ~S_ISUID;
3640 }
3641 if (vattr.va_mode & S_ISGID) {
3642 if (kauth_authorize_vnode(l->l_cred,
3643 KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
3644 newmode &= ~S_ISGID;
3645 }
3646 } else {
3647 /*
3648 * NetBSD semantics: when changing owner and/or group,
3649 * clear the respective bit(s).
3650 */
3651 if (CHANGED(uid))
3652 newmode &= ~S_ISUID;
3653 if (CHANGED(gid))
3654 newmode &= ~S_ISGID;
3655 }
3656 /* Update va_mode iff altered. */
3657 if (vattr.va_mode == newmode)
3658 newmode = VNOVAL;
3659
3660 vattr_null(&vattr);
3661 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
3662 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
3663 vattr.va_mode = newmode;
3664 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3665 #undef CHANGED
3666
3667 out:
3668 VOP_UNLOCK(vp);
3669 return (error);
3670 }
3671
3672 /*
3673 * Set the access and modification times given a path name; this
3674 * version follows links.
3675 */
3676 /* ARGSUSED */
3677 int
3678 sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap,
3679 register_t *retval)
3680 {
3681 /* {
3682 syscallarg(const char *) path;
3683 syscallarg(const struct timeval *) tptr;
3684 } */
3685
3686 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
3687 SCARG(uap, tptr), UIO_USERSPACE);
3688 }
3689
3690 /*
3691 * Set the access and modification times given a file descriptor.
3692 */
3693 /* ARGSUSED */
3694 int
3695 sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap,
3696 register_t *retval)
3697 {
3698 /* {
3699 syscallarg(int) fd;
3700 syscallarg(const struct timeval *) tptr;
3701 } */
3702 int error;
3703 file_t *fp;
3704
3705 /* fd_getvnode() will use the descriptor for us */
3706 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3707 return (error);
3708 error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr),
3709 UIO_USERSPACE);
3710 fd_putfile(SCARG(uap, fd));
3711 return (error);
3712 }
3713
3714 int
3715 sys_futimens(struct lwp *l, const struct sys_futimens_args *uap,
3716 register_t *retval)
3717 {
3718 /* {
3719 syscallarg(int) fd;
3720 syscallarg(const struct timespec *) tptr;
3721 } */
3722 int error;
3723 file_t *fp;
3724
3725 /* fd_getvnode() will use the descriptor for us */
3726 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3727 return (error);
3728 error = do_sys_utimensat(l, AT_FDCWD, fp->f_data, NULL, 0,
3729 SCARG(uap, tptr), UIO_USERSPACE);
3730 fd_putfile(SCARG(uap, fd));
3731 return (error);
3732 }
3733
3734 /*
3735 * Set the access and modification times given a path name; this
3736 * version does not follow links.
3737 */
3738 int
3739 sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap,
3740 register_t *retval)
3741 {
3742 /* {
3743 syscallarg(const char *) path;
3744 syscallarg(const struct timeval *) tptr;
3745 } */
3746
3747 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
3748 SCARG(uap, tptr), UIO_USERSPACE);
3749 }
3750
3751 int
3752 sys_utimensat(struct lwp *l, const struct sys_utimensat_args *uap,
3753 register_t *retval)
3754 {
3755 /* {
3756 syscallarg(int) fd;
3757 syscallarg(const char *) path;
3758 syscallarg(const struct timespec *) tptr;
3759 syscallarg(int) flag;
3760 } */
3761 int follow;
3762 const struct timespec *tptr;
3763 int error;
3764
3765 tptr = SCARG(uap, tptr);
3766 follow = (SCARG(uap, flag) & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3767
3768 error = do_sys_utimensat(l, SCARG(uap, fd), NULL,
3769 SCARG(uap, path), follow, tptr, UIO_USERSPACE);
3770
3771 return error;
3772 }
3773
3774 /*
3775 * Common routine to set access and modification times given a vnode.
3776 */
3777 int
3778 do_sys_utimens(struct lwp *l, struct vnode *vp, const char *path, int flag,
3779 const struct timespec *tptr, enum uio_seg seg)
3780 {
3781 return do_sys_utimensat(l, AT_FDCWD, vp, path, flag, tptr, seg);
3782 }
3783
3784 int
3785 do_sys_utimensat(struct lwp *l, int fdat, struct vnode *vp,
3786 const char *path, int flag, const struct timespec *tptr, enum uio_seg seg)
3787 {
3788 struct vattr vattr;
3789 int error, dorele = 0;
3790 namei_simple_flags_t sflags;
3791 bool vanull, setbirthtime;
3792 struct timespec ts[2];
3793
3794 KASSERT(l != NULL || fdat == AT_FDCWD);
3795
3796 /*
3797 * I have checked all callers and they pass either FOLLOW,
3798 * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW
3799 * is 0. More to the point, they don't pass anything else.
3800 * Let's keep it that way at least until the namei interfaces
3801 * are fully sanitized.
3802 */
3803 KASSERT(flag == NOFOLLOW || flag == FOLLOW);
3804 sflags = (flag == FOLLOW) ?
3805 NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT;
3806
3807 if (tptr == NULL) {
3808 vanull = true;
3809 nanotime(&ts[0]);
3810 ts[1] = ts[0];
3811 } else {
3812 vanull = false;
3813 if (seg != UIO_SYSSPACE) {
3814 error = copyin(tptr, ts, sizeof (ts));
3815 if (error != 0)
3816 return error;
3817 } else {
3818 ts[0] = tptr[0];
3819 ts[1] = tptr[1];
3820 }
3821 }
3822
3823 if (ts[0].tv_nsec == UTIME_NOW) {
3824 nanotime(&ts[0]);
3825 if (ts[1].tv_nsec == UTIME_NOW) {
3826 vanull = true;
3827 ts[1] = ts[0];
3828 }
3829 } else if (ts[1].tv_nsec == UTIME_NOW)
3830 nanotime(&ts[1]);
3831
3832 if (vp == NULL) {
3833 /* note: SEG describes TPTR, not PATH; PATH is always user */
3834 error = fd_nameiat_simple_user(l, fdat, path, sflags, &vp);
3835 if (error != 0)
3836 return error;
3837 dorele = 1;
3838 }
3839
3840 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3841 setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
3842 timespeccmp(&ts[1], &vattr.va_birthtime, <));
3843 vattr_null(&vattr);
3844
3845 if (ts[0].tv_nsec != UTIME_OMIT)
3846 vattr.va_atime = ts[0];
3847
3848 if (ts[1].tv_nsec != UTIME_OMIT) {
3849 vattr.va_mtime = ts[1];
3850 if (setbirthtime)
3851 vattr.va_birthtime = ts[1];
3852 }
3853
3854 if (vanull)
3855 vattr.va_vaflags |= VA_UTIMES_NULL;
3856 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3857 VOP_UNLOCK(vp);
3858
3859 if (dorele != 0)
3860 vrele(vp);
3861
3862 return error;
3863 }
3864
3865 int
3866 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
3867 const struct timeval *tptr, enum uio_seg seg)
3868 {
3869 struct timespec ts[2];
3870 struct timespec *tsptr = NULL;
3871 int error;
3872
3873 if (tptr != NULL) {
3874 struct timeval tv[2];
3875
3876 if (seg != UIO_SYSSPACE) {
3877 error = copyin(tptr, tv, sizeof (tv));
3878 if (error != 0)
3879 return error;
3880 tptr = tv;
3881 }
3882
3883 if ((tv[0].tv_usec == UTIME_NOW) ||
3884 (tv[0].tv_usec == UTIME_OMIT))
3885 ts[0].tv_nsec = tv[0].tv_usec;
3886 else
3887 TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
3888
3889 if ((tv[1].tv_usec == UTIME_NOW) ||
3890 (tv[1].tv_usec == UTIME_OMIT))
3891 ts[1].tv_nsec = tv[1].tv_usec;
3892 else
3893 TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
3894
3895 tsptr = &ts[0];
3896 }
3897
3898 return do_sys_utimens(l, vp, path, flag, tsptr, UIO_SYSSPACE);
3899 }
3900
3901 /*
3902 * Truncate a file given its path name.
3903 */
3904 /* ARGSUSED */
3905 int
3906 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
3907 {
3908 /* {
3909 syscallarg(const char *) path;
3910 syscallarg(int) pad;
3911 syscallarg(off_t) length;
3912 } */
3913 struct vnode *vp;
3914 struct vattr vattr;
3915 int error;
3916
3917 error = namei_simple_user(SCARG(uap, path),
3918 NSM_FOLLOW_TRYEMULROOT, &vp);
3919 if (error != 0)
3920 return (error);
3921 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3922 if (vp->v_type == VDIR)
3923 error = EISDIR;
3924 else if ((error = vn_writechk(vp)) == 0 &&
3925 (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
3926 vattr_null(&vattr);
3927 vattr.va_size = SCARG(uap, length);
3928 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3929 }
3930 vput(vp);
3931 return (error);
3932 }
3933
3934 /*
3935 * Truncate a file given a file descriptor.
3936 */
3937 /* ARGSUSED */
3938 int
3939 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
3940 {
3941 /* {
3942 syscallarg(int) fd;
3943 syscallarg(int) pad;
3944 syscallarg(off_t) length;
3945 } */
3946 struct vattr vattr;
3947 struct vnode *vp;
3948 file_t *fp;
3949 int error;
3950
3951 /* fd_getvnode() will use the descriptor for us */
3952 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3953 return (error);
3954 if ((fp->f_flag & FWRITE) == 0) {
3955 error = EINVAL;
3956 goto out;
3957 }
3958 vp = fp->f_data;
3959 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3960 if (vp->v_type == VDIR)
3961 error = EISDIR;
3962 else if ((error = vn_writechk(vp)) == 0) {
3963 vattr_null(&vattr);
3964 vattr.va_size = SCARG(uap, length);
3965 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3966 }
3967 VOP_UNLOCK(vp);
3968 out:
3969 fd_putfile(SCARG(uap, fd));
3970 return (error);
3971 }
3972
3973 /*
3974 * Sync an open file.
3975 */
3976 /* ARGSUSED */
3977 int
3978 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
3979 {
3980 /* {
3981 syscallarg(int) fd;
3982 } */
3983 struct vnode *vp;
3984 file_t *fp;
3985 int error;
3986
3987 /* fd_getvnode() will use the descriptor for us */
3988 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3989 return (error);
3990 vp = fp->f_data;
3991 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3992 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3993 VOP_UNLOCK(vp);
3994 fd_putfile(SCARG(uap, fd));
3995 return (error);
3996 }
3997
3998 /*
3999 * Sync a range of file data. API modeled after that found in AIX.
4000 *
4001 * FDATASYNC indicates that we need only save enough metadata to be able
4002 * to re-read the written data. Note we duplicate AIX's requirement that
4003 * the file be open for writing.
4004 */
4005 /* ARGSUSED */
4006 int
4007 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
4008 {
4009 /* {
4010 syscallarg(int) fd;
4011 syscallarg(int) flags;
4012 syscallarg(off_t) start;
4013 syscallarg(off_t) length;
4014 } */
4015 struct vnode *vp;
4016 file_t *fp;
4017 int flags, nflags;
4018 off_t s, e, len;
4019 int error;
4020
4021 /* fd_getvnode() will use the descriptor for us */
4022 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4023 return (error);
4024
4025 if ((fp->f_flag & FWRITE) == 0) {
4026 error = EBADF;
4027 goto out;
4028 }
4029
4030 flags = SCARG(uap, flags);
4031 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
4032 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
4033 error = EINVAL;
4034 goto out;
4035 }
4036 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
4037 if (flags & FDATASYNC)
4038 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
4039 else
4040 nflags = FSYNC_WAIT;
4041 if (flags & FDISKSYNC)
4042 nflags |= FSYNC_CACHE;
4043
4044 len = SCARG(uap, length);
4045 /* If length == 0, we do the whole file, and s = e = 0 will do that */
4046 if (len) {
4047 s = SCARG(uap, start);
4048 e = s + len;
4049 if (e < s) {
4050 error = EINVAL;
4051 goto out;
4052 }
4053 } else {
4054 e = 0;
4055 s = 0;
4056 }
4057
4058 vp = fp->f_data;
4059 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4060 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
4061 VOP_UNLOCK(vp);
4062 out:
4063 fd_putfile(SCARG(uap, fd));
4064 return (error);
4065 }
4066
4067 /*
4068 * Sync the data of an open file.
4069 */
4070 /* ARGSUSED */
4071 int
4072 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
4073 {
4074 /* {
4075 syscallarg(int) fd;
4076 } */
4077 struct vnode *vp;
4078 file_t *fp;
4079 int error;
4080
4081 /* fd_getvnode() will use the descriptor for us */
4082 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4083 return (error);
4084 if ((fp->f_flag & FWRITE) == 0) {
4085 fd_putfile(SCARG(uap, fd));
4086 return (EBADF);
4087 }
4088 vp = fp->f_data;
4089 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4090 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
4091 VOP_UNLOCK(vp);
4092 fd_putfile(SCARG(uap, fd));
4093 return (error);
4094 }
4095
4096 /*
4097 * Rename files, (standard) BSD semantics frontend.
4098 */
4099 /* ARGSUSED */
4100 int
4101 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
4102 {
4103 /* {
4104 syscallarg(const char *) from;
4105 syscallarg(const char *) to;
4106 } */
4107
4108 return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
4109 SCARG(uap, to), UIO_USERSPACE, 0));
4110 }
4111
4112 int
4113 sys_renameat(struct lwp *l, const struct sys_renameat_args *uap,
4114 register_t *retval)
4115 {
4116 /* {
4117 syscallarg(int) fromfd;
4118 syscallarg(const char *) from;
4119 syscallarg(int) tofd;
4120 syscallarg(const char *) to;
4121 } */
4122
4123 return (do_sys_renameat(l, SCARG(uap, fromfd), SCARG(uap, from),
4124 SCARG(uap, tofd), SCARG(uap, to), UIO_USERSPACE, 0));
4125 }
4126
4127 /*
4128 * Rename files, POSIX semantics frontend.
4129 */
4130 /* ARGSUSED */
4131 int
4132 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
4133 {
4134 /* {
4135 syscallarg(const char *) from;
4136 syscallarg(const char *) to;
4137 } */
4138
4139 return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
4140 SCARG(uap, to), UIO_USERSPACE, 1));
4141 }
4142
4143 /*
4144 * Rename files. Source and destination must either both be directories,
4145 * or both not be directories. If target is a directory, it must be empty.
4146 * If `from' and `to' refer to the same object, the value of the `retain'
4147 * argument is used to determine whether `from' will be
4148 *
4149 * (retain == 0) deleted unless `from' and `to' refer to the same
4150 * object in the file system's name space (BSD).
4151 * (retain == 1) always retained (POSIX).
4152 *
4153 * XXX Synchronize with nfsrv_rename in nfs_serv.c.
4154 */
4155 int
4156 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
4157 {
4158 return do_sys_renameat(NULL, AT_FDCWD, from, AT_FDCWD, to, seg, retain);
4159 }
4160
4161 static int
4162 do_sys_renameat(struct lwp *l, int fromfd, const char *from, int tofd,
4163 const char *to, enum uio_seg seg, int retain)
4164 {
4165 struct pathbuf *fpb, *tpb;
4166 struct nameidata fnd, tnd;
4167 struct vnode *fdvp, *fvp;
4168 struct vnode *tdvp, *tvp;
4169 struct mount *mp, *tmp;
4170 int error;
4171
4172 KASSERT(l != NULL || (fromfd == AT_FDCWD && tofd == AT_FDCWD));
4173
4174 error = pathbuf_maybe_copyin(from, seg, &fpb);
4175 if (error)
4176 goto out0;
4177 KASSERT(fpb != NULL);
4178
4179 error = pathbuf_maybe_copyin(to, seg, &tpb);
4180 if (error)
4181 goto out1;
4182 KASSERT(tpb != NULL);
4183
4184 /*
4185 * Lookup from.
4186 *
4187 * XXX LOCKPARENT is wrong because we don't actually want it
4188 * locked yet, but (a) namei is insane, and (b) VOP_RENAME is
4189 * insane, so for the time being we need to leave it like this.
4190 */
4191 NDINIT(&fnd, DELETE, (LOCKPARENT | TRYEMULROOT | INRENAME), fpb);
4192 if ((error = fd_nameiat(l, fromfd, &fnd)) != 0)
4193 goto out2;
4194
4195 /*
4196 * Pull out the important results of the lookup, fdvp and fvp.
4197 * Of course, fvp is bogus because we're about to unlock fdvp.
4198 */
4199 fdvp = fnd.ni_dvp;
4200 fvp = fnd.ni_vp;
4201 KASSERT(fdvp != NULL);
4202 KASSERT(fvp != NULL);
4203 KASSERT((fdvp == fvp) || (VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE));
4204
4205 /*
4206 * Make sure neither fdvp nor fvp is locked.
4207 */
4208 if (fdvp != fvp)
4209 VOP_UNLOCK(fdvp);
4210 /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4211 /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4212
4213 /*
4214 * Reject renaming `.' and `..'. Can't do this until after
4215 * namei because we need namei's parsing to find the final
4216 * component name. (namei should just leave us with the final
4217 * component name and not look it up itself, but anyway...)
4218 *
4219 * This was here before because we used to relookup from
4220 * instead of to and relookup requires the caller to check
4221 * this, but now file systems may depend on this check, so we
4222 * must retain it until the file systems are all rototilled.
4223 */
4224 if (((fnd.ni_cnd.cn_namelen == 1) &&
4225 (fnd.ni_cnd.cn_nameptr[0] == '.')) ||
4226 ((fnd.ni_cnd.cn_namelen == 2) &&
4227 (fnd.ni_cnd.cn_nameptr[0] == '.') &&
4228 (fnd.ni_cnd.cn_nameptr[1] == '.'))) {
4229 error = EINVAL; /* XXX EISDIR? */
4230 goto abort0;
4231 }
4232
4233 /*
4234 * Lookup to.
4235 *
4236 * XXX LOCKPARENT is wrong, but...insanity, &c. Also, using
4237 * fvp here to decide whether to add CREATEDIR is a load of
4238 * bollocks because fvp might be the wrong node by now, since
4239 * fdvp is unlocked.
4240 *
4241 * XXX Why not pass CREATEDIR always?
4242 */
4243 NDINIT(&tnd, RENAME,
4244 (LOCKPARENT | NOCACHE | TRYEMULROOT | INRENAME |
4245 ((fvp->v_type == VDIR)? CREATEDIR : 0)),
4246 tpb);
4247 if ((error = fd_nameiat(l, tofd, &tnd)) != 0)
4248 goto abort0;
4249
4250 /*
4251 * Pull out the important results of the lookup, tdvp and tvp.
4252 * Of course, tvp is bogus because we're about to unlock tdvp.
4253 */
4254 tdvp = tnd.ni_dvp;
4255 tvp = tnd.ni_vp;
4256 KASSERT(tdvp != NULL);
4257 KASSERT((tdvp == tvp) || (VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE));
4258
4259 /*
4260 * Make sure neither tdvp nor tvp is locked.
4261 */
4262 if (tdvp != tvp)
4263 VOP_UNLOCK(tdvp);
4264 /* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
4265 /* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
4266
4267 /*
4268 * Reject renaming onto `.' or `..'. relookup is unhappy with
4269 * these, which is why we must do this here. Once upon a time
4270 * we relooked up from instead of to, and consequently didn't
4271 * need this check, but now that we relookup to instead of
4272 * from, we need this; and we shall need it forever forward
4273 * until the VOP_RENAME protocol changes, because file systems
4274 * will no doubt begin to depend on this check.
4275 */
4276 if (((tnd.ni_cnd.cn_namelen == 1) &&
4277 (tnd.ni_cnd.cn_nameptr[0] == '.')) ||
4278 ((tnd.ni_cnd.cn_namelen == 2) &&
4279 (tnd.ni_cnd.cn_nameptr[0] == '.') &&
4280 (tnd.ni_cnd.cn_nameptr[1] == '.'))) {
4281 error = EINVAL; /* XXX EISDIR? */
4282 goto abort1;
4283 }
4284
4285 /*
4286 * Get the mount point. If the file system has been unmounted,
4287 * which it may be because we're not holding any vnode locks,
4288 * then v_mount will be NULL. We're not really supposed to
4289 * read v_mount without holding the vnode lock, but since we
4290 * have fdvp referenced, if fdvp->v_mount changes then at worst
4291 * it will be set to NULL, not changed to another mount point.
4292 * And, of course, since it is up to the file system to
4293 * determine the real lock order, we can't lock both fdvp and
4294 * tdvp at the same time.
4295 */
4296 mp = fdvp->v_mount;
4297 if (mp == NULL) {
4298 error = ENOENT;
4299 goto abort1;
4300 }
4301
4302 /*
4303 * Make sure the mount points match. Again, although we don't
4304 * hold any vnode locks, the v_mount fields may change -- but
4305 * at worst they will change to NULL, so this will never become
4306 * a cross-device rename, because we hold vnode references.
4307 *
4308 * XXX Because nothing is locked and the compiler may reorder
4309 * things here, unmounting the file system at an inopportune
4310 * moment may cause rename to fail with ENXDEV when it really
4311 * should fail with ENOENT.
4312 */
4313 tmp = tdvp->v_mount;
4314 if (tmp == NULL) {
4315 error = ENOENT;
4316 goto abort1;
4317 }
4318
4319 if (mp != tmp) {
4320 error = EXDEV;
4321 goto abort1;
4322 }
4323
4324 /*
4325 * Take the vfs rename lock to avoid cross-directory screw cases.
4326 * Nothing is locked currently, so taking this lock is safe.
4327 */
4328 error = VFS_RENAMELOCK_ENTER(mp);
4329 if (error)
4330 goto abort1;
4331
4332 /*
4333 * Now fdvp, fvp, tdvp, and (if nonnull) tvp are referenced,
4334 * and nothing is locked except for the vfs rename lock.
4335 *
4336 * The next step is a little rain dance to conform to the
4337 * insane lock protocol, even though it does nothing to ward
4338 * off race conditions.
4339 *
4340 * We need tdvp and tvp to be locked. However, because we have
4341 * unlocked tdvp in order to hold no locks while we take the
4342 * vfs rename lock, tvp may be wrong here, and we can't safely
4343 * lock it even if the sensible file systems will just unlock
4344 * it straight away. Consequently, we must lock tdvp and then
4345 * relookup tvp to get it locked.
4346 *
4347 * Finally, because the VOP_RENAME protocol is brain-damaged
4348 * and various file systems insanely depend on the semantics of
4349 * this brain damage, the lookup of to must be the last lookup
4350 * before VOP_RENAME.
4351 */
4352 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
4353 error = relookup(tdvp, &tnd.ni_vp, &tnd.ni_cnd, 0);
4354 if (error)
4355 goto abort2;
4356
4357 /*
4358 * Drop the old tvp and pick up the new one -- which might be
4359 * the same, but that doesn't matter to us. After this, tdvp
4360 * and tvp should both be locked.
4361 */
4362 if (tvp != NULL)
4363 vrele(tvp);
4364 tvp = tnd.ni_vp;
4365 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
4366 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
4367
4368 /*
4369 * The old do_sys_rename had various consistency checks here
4370 * involving fvp and tvp. fvp is bogus already here, and tvp
4371 * will become bogus soon in any sensible file system, so the
4372 * only purpose in putting these checks here is to give lip
4373 * service to these screw cases and to acknowledge that they
4374 * exist, not actually to handle them, but here you go
4375 * anyway...
4376 */
4377
4378 /*
4379 * Acknowledge that directories and non-directories aren't
4380 * suposed to mix.
4381 */
4382 if (tvp != NULL) {
4383 if ((fvp->v_type == VDIR) && (tvp->v_type != VDIR)) {
4384 error = ENOTDIR;
4385 goto abort3;
4386 } else if ((fvp->v_type != VDIR) && (tvp->v_type == VDIR)) {
4387 error = EISDIR;
4388 goto abort3;
4389 }
4390 }
4391
4392 /*
4393 * Acknowledge some random screw case, among the dozens that
4394 * might arise.
4395 */
4396 if (fvp == tdvp) {
4397 error = EINVAL;
4398 goto abort3;
4399 }
4400
4401 /*
4402 * Acknowledge that POSIX has a wacky screw case.
4403 *
4404 * XXX Eventually the retain flag needs to be passed on to
4405 * VOP_RENAME.
4406 */
4407 if (fvp == tvp) {
4408 if (retain) {
4409 error = 0;
4410 goto abort3;
4411 } else if ((fdvp == tdvp) &&
4412 (fnd.ni_cnd.cn_namelen == tnd.ni_cnd.cn_namelen) &&
4413 (0 == memcmp(fnd.ni_cnd.cn_nameptr, tnd.ni_cnd.cn_nameptr,
4414 fnd.ni_cnd.cn_namelen))) {
4415 error = 0;
4416 goto abort3;
4417 }
4418 }
4419
4420 /*
4421 * Make sure veriexec can screw us up. (But a race can screw
4422 * up veriexec, of course -- remember, fvp and (soon) tvp are
4423 * bogus.)
4424 */
4425 #if NVERIEXEC > 0
4426 {
4427 char *f1, *f2;
4428 size_t f1_len;
4429 size_t f2_len;
4430
4431 f1_len = fnd.ni_cnd.cn_namelen + 1;
4432 f1 = kmem_alloc(f1_len, KM_SLEEP);
4433 strlcpy(f1, fnd.ni_cnd.cn_nameptr, f1_len);
4434
4435 f2_len = tnd.ni_cnd.cn_namelen + 1;
4436 f2 = kmem_alloc(f2_len, KM_SLEEP);
4437 strlcpy(f2, tnd.ni_cnd.cn_nameptr, f2_len);
4438
4439 error = veriexec_renamechk(curlwp, fvp, f1, tvp, f2);
4440
4441 kmem_free(f1, f1_len);
4442 kmem_free(f2, f2_len);
4443
4444 if (error)
4445 goto abort3;
4446 }
4447 #endif /* NVERIEXEC > 0 */
4448
4449 /*
4450 * All ready. Incant the rename vop.
4451 */
4452 /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4453 /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4454 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
4455 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
4456 error = VOP_RENAME(fdvp, fvp, &fnd.ni_cnd, tdvp, tvp, &tnd.ni_cnd);
4457
4458 /*
4459 * VOP_RENAME releases fdvp, fvp, tdvp, and tvp, and unlocks
4460 * tdvp and tvp. But we can't assert any of that.
4461 */
4462 /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4463 /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4464 /* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
4465 /* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
4466
4467 /*
4468 * So all we have left to do is to drop the rename lock and
4469 * destroy the pathbufs.
4470 */
4471 VFS_RENAMELOCK_EXIT(mp);
4472 goto out2;
4473
4474 abort3: if ((tvp != NULL) && (tvp != tdvp))
4475 VOP_UNLOCK(tvp);
4476 abort2: VOP_UNLOCK(tdvp);
4477 VFS_RENAMELOCK_EXIT(mp);
4478 abort1: VOP_ABORTOP(tdvp, &tnd.ni_cnd);
4479 vrele(tdvp);
4480 if (tvp != NULL)
4481 vrele(tvp);
4482 abort0: VOP_ABORTOP(fdvp, &fnd.ni_cnd);
4483 vrele(fdvp);
4484 vrele(fvp);
4485 out2: pathbuf_destroy(tpb);
4486 out1: pathbuf_destroy(fpb);
4487 out0: return error;
4488 }
4489
4490 /*
4491 * Make a directory file.
4492 */
4493 /* ARGSUSED */
4494 int
4495 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
4496 {
4497 /* {
4498 syscallarg(const char *) path;
4499 syscallarg(int) mode;
4500 } */
4501
4502 return do_sys_mkdirat(l, AT_FDCWD, SCARG(uap, path),
4503 SCARG(uap, mode), UIO_USERSPACE);
4504 }
4505
4506 int
4507 sys_mkdirat(struct lwp *l, const struct sys_mkdirat_args *uap,
4508 register_t *retval)
4509 {
4510 /* {
4511 syscallarg(int) fd;
4512 syscallarg(const char *) path;
4513 syscallarg(int) mode;
4514 } */
4515
4516 return do_sys_mkdirat(l, SCARG(uap, fd), SCARG(uap, path),
4517 SCARG(uap, mode), UIO_USERSPACE);
4518 }
4519
4520
4521 int
4522 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
4523 {
4524 return do_sys_mkdirat(NULL, AT_FDCWD, path, mode, UIO_USERSPACE);
4525 }
4526
4527 static int
4528 do_sys_mkdirat(struct lwp *l, int fdat, const char *path, mode_t mode,
4529 enum uio_seg seg)
4530 {
4531 struct proc *p = curlwp->l_proc;
4532 struct vnode *vp;
4533 struct vattr vattr;
4534 int error;
4535 struct pathbuf *pb;
4536 struct nameidata nd;
4537
4538 KASSERT(l != NULL || fdat == AT_FDCWD);
4539
4540 /* XXX bollocks, should pass in a pathbuf */
4541 error = pathbuf_maybe_copyin(path, seg, &pb);
4542 if (error) {
4543 return error;
4544 }
4545
4546 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
4547
4548 if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
4549 pathbuf_destroy(pb);
4550 return (error);
4551 }
4552 vp = nd.ni_vp;
4553 if (vp != NULL) {
4554 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
4555 if (nd.ni_dvp == vp)
4556 vrele(nd.ni_dvp);
4557 else
4558 vput(nd.ni_dvp);
4559 vrele(vp);
4560 pathbuf_destroy(pb);
4561 return (EEXIST);
4562 }
4563 vattr_null(&vattr);
4564 vattr.va_type = VDIR;
4565 /* We will read cwdi->cwdi_cmask unlocked. */
4566 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
4567 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
4568 if (!error)
4569 vput(nd.ni_vp);
4570 pathbuf_destroy(pb);
4571 return (error);
4572 }
4573
4574 /*
4575 * Remove a directory file.
4576 */
4577 /* ARGSUSED */
4578 int
4579 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
4580 {
4581 return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path),
4582 AT_REMOVEDIR, UIO_USERSPACE);
4583 }
4584
4585 /*
4586 * Read a block of directory entries in a file system independent format.
4587 */
4588 int
4589 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
4590 {
4591 /* {
4592 syscallarg(int) fd;
4593 syscallarg(char *) buf;
4594 syscallarg(size_t) count;
4595 } */
4596 file_t *fp;
4597 int error, done;
4598
4599 /* fd_getvnode() will use the descriptor for us */
4600 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4601 return (error);
4602 if ((fp->f_flag & FREAD) == 0) {
4603 error = EBADF;
4604 goto out;
4605 }
4606 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
4607 SCARG(uap, count), &done, l, 0, 0);
4608 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
4609 *retval = done;
4610 out:
4611 fd_putfile(SCARG(uap, fd));
4612 return (error);
4613 }
4614
4615 /*
4616 * Set the mode mask for creation of filesystem nodes.
4617 */
4618 int
4619 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
4620 {
4621 /* {
4622 syscallarg(mode_t) newmask;
4623 } */
4624 struct proc *p = l->l_proc;
4625 struct cwdinfo *cwdi;
4626
4627 /*
4628 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
4629 * important is that we serialize changes to the mask. The
4630 * rw_exit() will issue a write memory barrier on our behalf,
4631 * and force the changes out to other CPUs (as it must use an
4632 * atomic operation, draining the local CPU's store buffers).
4633 */
4634 cwdi = p->p_cwdi;
4635 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
4636 *retval = cwdi->cwdi_cmask;
4637 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
4638 rw_exit(&cwdi->cwdi_lock);
4639
4640 return (0);
4641 }
4642
4643 int
4644 dorevoke(struct vnode *vp, kauth_cred_t cred)
4645 {
4646 struct vattr vattr;
4647 int error, fs_decision;
4648
4649 vn_lock(vp, LK_SHARED | LK_RETRY);
4650 error = VOP_GETATTR(vp, &vattr, cred);
4651 VOP_UNLOCK(vp);
4652 if (error != 0)
4653 return error;
4654 fs_decision = (kauth_cred_geteuid(cred) == vattr.va_uid) ? 0 : EPERM;
4655 error = kauth_authorize_vnode(cred, KAUTH_VNODE_REVOKE, vp, NULL,
4656 fs_decision);
4657 if (!error)
4658 VOP_REVOKE(vp, REVOKEALL);
4659 return (error);
4660 }
4661
4662 /*
4663 * Void all references to file by ripping underlying filesystem
4664 * away from vnode.
4665 */
4666 /* ARGSUSED */
4667 int
4668 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
4669 {
4670 /* {
4671 syscallarg(const char *) path;
4672 } */
4673 struct vnode *vp;
4674 int error;
4675
4676 error = namei_simple_user(SCARG(uap, path),
4677 NSM_FOLLOW_TRYEMULROOT, &vp);
4678 if (error != 0)
4679 return (error);
4680 error = dorevoke(vp, l->l_cred);
4681 vrele(vp);
4682 return (error);
4683 }
4684