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