vfs_syscalls.c revision 1.478 1 /* $NetBSD: vfs_syscalls.c,v 1.478 2014/04/04 06:47:02 maxv 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.478 2014/04/04 06:47:02 maxv Exp $");
74
75 #ifdef _KERNEL_OPT
76 #include "opt_fileassoc.h"
77 #include "veriexec.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/namei.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/fcntl.h>
87 #include <sys/stat.h>
88 #include <sys/vnode.h>
89 #include <sys/mount.h>
90 #include <sys/proc.h>
91 #include <sys/uio.h>
92 #include <sys/kmem.h>
93 #include <sys/dirent.h>
94 #include <sys/sysctl.h>
95 #include <sys/syscallargs.h>
96 #include <sys/vfs_syscalls.h>
97 #include <sys/quota.h>
98 #include <sys/quotactl.h>
99 #include <sys/ktrace.h>
100 #ifdef FILEASSOC
101 #include <sys/fileassoc.h>
102 #endif /* FILEASSOC */
103 #include <sys/extattr.h>
104 #include <sys/verified_exec.h>
105 #include <sys/kauth.h>
106 #include <sys/atomic.h>
107 #include <sys/module.h>
108 #include <sys/buf.h>
109
110 #include <miscfs/genfs/genfs.h>
111 #include <miscfs/syncfs/syncfs.h>
112 #include <miscfs/specfs/specdev.h>
113
114 #include <nfs/rpcv2.h>
115 #include <nfs/nfsproto.h>
116 #include <nfs/nfs.h>
117 #include <nfs/nfs_var.h>
118
119 static int change_flags(struct vnode *, u_long, struct lwp *);
120 static int change_mode(struct vnode *, int, struct lwp *);
121 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
122 static int do_sys_openat(lwp_t *, int, const char *, int, int, int *);
123 static int do_sys_mkdirat(struct lwp *l, int, const char *, mode_t,
124 enum uio_seg);
125 static int do_sys_mkfifoat(struct lwp *, int, const char *, mode_t);
126 static int do_sys_symlinkat(struct lwp *, const char *, int, const char *,
127 enum uio_seg);
128 static int do_sys_renameat(struct lwp *l, int, const char *, int, const char *,
129 enum uio_seg, int);
130 static int do_sys_readlinkat(struct lwp *, int, const char *, char *,
131 size_t, register_t *);
132 static int do_sys_unlinkat(struct lwp *, int, const char *, int, enum uio_seg);
133
134 static int fd_nameiat(struct lwp *, int, struct nameidata *);
135 static int fd_nameiat_simple_user(struct lwp *, int, const char *,
136 namei_simple_flags_t, struct vnode **);
137
138
139 /*
140 * This table is used to maintain compatibility with 4.3BSD
141 * and NetBSD 0.9 mount syscalls - and possibly other systems.
142 * Note, the order is important!
143 *
144 * Do not modify this table. It should only contain filesystems
145 * supported by NetBSD 0.9 and 4.3BSD.
146 */
147 const char * const mountcompatnames[] = {
148 NULL, /* 0 = MOUNT_NONE */
149 MOUNT_FFS, /* 1 = MOUNT_UFS */
150 MOUNT_NFS, /* 2 */
151 MOUNT_MFS, /* 3 */
152 MOUNT_MSDOS, /* 4 */
153 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
154 MOUNT_FDESC, /* 6 */
155 MOUNT_KERNFS, /* 7 */
156 NULL, /* 8 = MOUNT_DEVFS */
157 MOUNT_AFS, /* 9 */
158 };
159
160 const int nmountcompatnames = __arraycount(mountcompatnames);
161
162 static int
163 fd_nameiat(struct lwp *l, int fdat, struct nameidata *ndp)
164 {
165 file_t *dfp;
166 int error;
167
168 if (fdat != AT_FDCWD) {
169 if ((error = fd_getvnode(fdat, &dfp)) != 0)
170 goto out;
171
172 NDAT(ndp, dfp->f_data);
173 }
174
175 error = namei(ndp);
176
177 if (fdat != AT_FDCWD)
178 fd_putfile(fdat);
179 out:
180 return error;
181 }
182
183 static int
184 fd_nameiat_simple_user(struct lwp *l, int fdat, const char *path,
185 namei_simple_flags_t sflags, struct vnode **vp_ret)
186 {
187 file_t *dfp;
188 struct vnode *dvp;
189 int error;
190
191 if (fdat != AT_FDCWD) {
192 if ((error = fd_getvnode(fdat, &dfp)) != 0)
193 goto out;
194
195 dvp = dfp->f_data;
196 } else {
197 dvp = NULL;
198 }
199
200 error = nameiat_simple_user(dvp, path, sflags, vp_ret);
201
202 if (fdat != AT_FDCWD)
203 fd_putfile(fdat);
204 out:
205 return error;
206 }
207
208 static int
209 open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
210 {
211 int error;
212
213 fp->f_flag = flags & FMASK;
214 fp->f_type = DTYPE_VNODE;
215 fp->f_ops = &vnops;
216 fp->f_data = vp;
217
218 if (flags & (O_EXLOCK | O_SHLOCK)) {
219 struct flock lf;
220 int type;
221
222 lf.l_whence = SEEK_SET;
223 lf.l_start = 0;
224 lf.l_len = 0;
225 if (flags & O_EXLOCK)
226 lf.l_type = F_WRLCK;
227 else
228 lf.l_type = F_RDLCK;
229 type = F_FLOCK;
230 if ((flags & FNONBLOCK) == 0)
231 type |= F_WAIT;
232 VOP_UNLOCK(vp);
233 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
234 if (error) {
235 (void) vn_close(vp, fp->f_flag, fp->f_cred);
236 fd_abort(l->l_proc, fp, indx);
237 return error;
238 }
239 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
240 atomic_or_uint(&fp->f_flag, FHASLOCK);
241 }
242 if (flags & O_CLOEXEC)
243 fd_set_exclose(l, indx, true);
244 return 0;
245 }
246
247 static int
248 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
249 void *data, size_t *data_len)
250 {
251 struct mount *mp;
252 int error = 0, saved_flags;
253
254 mp = vp->v_mount;
255 saved_flags = mp->mnt_flag;
256
257 /* We can operate only on VV_ROOT nodes. */
258 if ((vp->v_vflag & VV_ROOT) == 0) {
259 error = EINVAL;
260 goto out;
261 }
262
263 /*
264 * We only allow the filesystem to be reloaded if it
265 * is currently mounted read-only. Additionally, we
266 * prevent read-write to read-only downgrades.
267 */
268 if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
269 (mp->mnt_flag & MNT_RDONLY) == 0 &&
270 (mp->mnt_iflag & IMNT_CAN_RWTORO) == 0) {
271 error = EOPNOTSUPP; /* Needs translation */
272 goto out;
273 }
274
275 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
276 KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
277 if (error)
278 goto out;
279
280 if (vfs_busy(mp, NULL)) {
281 error = EPERM;
282 goto out;
283 }
284
285 mutex_enter(&mp->mnt_updating);
286
287 mp->mnt_flag &= ~MNT_OP_FLAGS;
288 mp->mnt_flag |= flags & MNT_OP_FLAGS;
289
290 /*
291 * Set the mount level flags.
292 */
293 if (flags & MNT_RDONLY)
294 mp->mnt_flag |= MNT_RDONLY;
295 else if (mp->mnt_flag & MNT_RDONLY)
296 mp->mnt_iflag |= IMNT_WANTRDWR;
297 mp->mnt_flag &= ~MNT_BASIC_FLAGS;
298 mp->mnt_flag |= flags & MNT_BASIC_FLAGS;
299 error = VFS_MOUNT(mp, path, data, data_len);
300
301 if (error && data != NULL) {
302 int error2;
303
304 /*
305 * Update failed; let's try and see if it was an
306 * export request. For compat with 3.0 and earlier.
307 */
308 error2 = vfs_hooks_reexport(mp, path, data);
309
310 /*
311 * Only update error code if the export request was
312 * understood but some problem occurred while
313 * processing it.
314 */
315 if (error2 != EJUSTRETURN)
316 error = error2;
317 }
318
319 if (mp->mnt_iflag & IMNT_WANTRDWR)
320 mp->mnt_flag &= ~MNT_RDONLY;
321 if (error)
322 mp->mnt_flag = saved_flags;
323 mp->mnt_flag &= ~MNT_OP_FLAGS;
324 mp->mnt_iflag &= ~IMNT_WANTRDWR;
325 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
326 if (mp->mnt_syncer == NULL)
327 error = vfs_allocate_syncvnode(mp);
328 } else {
329 if (mp->mnt_syncer != NULL)
330 vfs_deallocate_syncvnode(mp);
331 }
332 mutex_exit(&mp->mnt_updating);
333 vfs_unbusy(mp, false, NULL);
334
335 if ((error == 0) && !(saved_flags & MNT_EXTATTR) &&
336 (flags & MNT_EXTATTR)) {
337 if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_START,
338 NULL, 0, NULL) != 0) {
339 printf("%s: failed to start extattr, error = %d",
340 mp->mnt_stat.f_mntonname, error);
341 mp->mnt_flag &= ~MNT_EXTATTR;
342 }
343 }
344
345 if ((error == 0) && (saved_flags & MNT_EXTATTR) &&
346 !(flags & MNT_EXTATTR)) {
347 if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_STOP,
348 NULL, 0, NULL) != 0) {
349 printf("%s: failed to stop extattr, error = %d",
350 mp->mnt_stat.f_mntonname, error);
351 mp->mnt_flag |= MNT_RDONLY;
352 }
353 }
354 out:
355 return (error);
356 }
357
358 static int
359 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
360 {
361 char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
362 int error;
363
364 /* Copy file-system type from userspace. */
365 error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
366 if (error) {
367 /*
368 * Historically, filesystem types were identified by numbers.
369 * If we get an integer for the filesystem type instead of a
370 * string, we check to see if it matches one of the historic
371 * filesystem types.
372 */
373 u_long fsindex = (u_long)fstype;
374 if (fsindex >= nmountcompatnames ||
375 mountcompatnames[fsindex] == NULL)
376 return ENODEV;
377 strlcpy(fstypename, mountcompatnames[fsindex],
378 sizeof(fstypename));
379 }
380
381 /* Accept `ufs' as an alias for `ffs', for compatibility. */
382 if (strcmp(fstypename, "ufs") == 0)
383 fstypename[0] = 'f';
384
385 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
386 return 0;
387
388 /* If we can autoload a vfs module, try again */
389 (void)module_autoload(fstypename, MODULE_CLASS_VFS);
390
391 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
392 return 0;
393
394 return ENODEV;
395 }
396
397 static int
398 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
399 void *data, size_t *data_len)
400 {
401 struct mount *mp;
402 int error;
403
404 /* If MNT_GETARGS is specified, it should be the only flag. */
405 if (flags & ~MNT_GETARGS)
406 return EINVAL;
407
408 mp = vp->v_mount;
409
410 /* XXX: probably some notion of "can see" here if we want isolation. */
411 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
412 KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
413 if (error)
414 return error;
415
416 if ((vp->v_vflag & VV_ROOT) == 0)
417 return EINVAL;
418
419 if (vfs_busy(mp, NULL))
420 return EPERM;
421
422 mutex_enter(&mp->mnt_updating);
423 mp->mnt_flag &= ~MNT_OP_FLAGS;
424 mp->mnt_flag |= MNT_GETARGS;
425 error = VFS_MOUNT(mp, path, data, data_len);
426 mp->mnt_flag &= ~MNT_OP_FLAGS;
427 mutex_exit(&mp->mnt_updating);
428
429 vfs_unbusy(mp, false, NULL);
430 return (error);
431 }
432
433 int
434 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
435 {
436 /* {
437 syscallarg(const char *) type;
438 syscallarg(const char *) path;
439 syscallarg(int) flags;
440 syscallarg(void *) data;
441 syscallarg(size_t) data_len;
442 } */
443
444 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
445 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
446 SCARG(uap, data_len), retval);
447 }
448
449 int
450 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
451 const char *path, int flags, void *data, enum uio_seg data_seg,
452 size_t data_len, register_t *retval)
453 {
454 struct vnode *vp;
455 void *data_buf = data;
456 bool vfsopsrele = false;
457 int error;
458
459 /* XXX: The calling convention of this routine is totally bizarre */
460 if (vfsops)
461 vfsopsrele = true;
462
463 /*
464 * Get vnode to be covered
465 */
466 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
467 if (error != 0) {
468 vp = NULL;
469 goto done;
470 }
471
472 if (vfsops == NULL) {
473 if (flags & (MNT_GETARGS | MNT_UPDATE)) {
474 vfsops = vp->v_mount->mnt_op;
475 } else {
476 /* 'type' is userspace */
477 error = mount_get_vfsops(type, &vfsops);
478 if (error != 0)
479 goto done;
480 vfsopsrele = true;
481 }
482 }
483
484 if (data != NULL && data_seg == UIO_USERSPACE) {
485 if (data_len == 0) {
486 /* No length supplied, use default for filesystem */
487 data_len = vfsops->vfs_min_mount_data;
488
489 /*
490 * Hopefully a longer buffer won't make copyin() fail.
491 * For compatibility with 3.0 and earlier.
492 */
493 if (flags & MNT_UPDATE
494 && data_len < sizeof (struct mnt_export_args30))
495 data_len = sizeof (struct mnt_export_args30);
496 }
497 if (data_len > VFS_MAX_MOUNT_DATA) {
498 error = EINVAL;
499 goto done;
500 }
501 data_buf = kmem_alloc(data_len, KM_SLEEP);
502
503 /* NFS needs the buffer even for mnt_getargs .... */
504 error = copyin(data, data_buf, data_len);
505 if (error != 0)
506 goto done;
507 }
508
509 if (flags & MNT_GETARGS) {
510 if (data_len == 0) {
511 error = EINVAL;
512 goto done;
513 }
514 error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
515 if (error != 0)
516 goto done;
517 if (data_seg == UIO_USERSPACE)
518 error = copyout(data_buf, data, data_len);
519 *retval = data_len;
520 } else if (flags & MNT_UPDATE) {
521 error = mount_update(l, vp, path, flags, data_buf, &data_len);
522 } else {
523 /* Locking is handled internally in mount_domount(). */
524 KASSERT(vfsopsrele == true);
525 error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
526 &data_len);
527 vfsopsrele = false;
528 }
529
530 done:
531 if (vfsopsrele)
532 vfs_delref(vfsops);
533 if (vp != NULL) {
534 vrele(vp);
535 }
536 if (data_buf != data)
537 kmem_free(data_buf, data_len);
538 return (error);
539 }
540
541 /*
542 * Unmount a file system.
543 *
544 * Note: unmount takes a path to the vnode mounted on as argument,
545 * not special file (as before).
546 */
547 /* ARGSUSED */
548 int
549 sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
550 {
551 /* {
552 syscallarg(const char *) path;
553 syscallarg(int) flags;
554 } */
555 struct vnode *vp;
556 struct mount *mp;
557 int error;
558 struct pathbuf *pb;
559 struct nameidata nd;
560
561 error = pathbuf_copyin(SCARG(uap, path), &pb);
562 if (error) {
563 return error;
564 }
565
566 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
567 if ((error = namei(&nd)) != 0) {
568 pathbuf_destroy(pb);
569 return error;
570 }
571 vp = nd.ni_vp;
572 pathbuf_destroy(pb);
573
574 mp = vp->v_mount;
575 atomic_inc_uint(&mp->mnt_refcnt);
576 VOP_UNLOCK(vp);
577
578 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
579 KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
580 if (error) {
581 vrele(vp);
582 vfs_destroy(mp);
583 return (error);
584 }
585
586 /*
587 * Don't allow unmounting the root file system.
588 */
589 if (mp->mnt_flag & MNT_ROOTFS) {
590 vrele(vp);
591 vfs_destroy(mp);
592 return (EINVAL);
593 }
594
595 /*
596 * Must be the root of the filesystem
597 */
598 if ((vp->v_vflag & VV_ROOT) == 0) {
599 vrele(vp);
600 vfs_destroy(mp);
601 return (EINVAL);
602 }
603
604 vrele(vp);
605 error = dounmount(mp, SCARG(uap, flags), l);
606 vfs_destroy(mp);
607 return error;
608 }
609
610 /*
611 * Sync each mounted filesystem.
612 */
613 #ifdef DEBUG
614 int syncprt = 0;
615 struct ctldebug debug0 = { "syncprt", &syncprt };
616 #endif
617
618 void
619 do_sys_sync(struct lwp *l)
620 {
621 struct mount *mp, *nmp;
622 int asyncflag;
623
624 mutex_enter(&mountlist_lock);
625 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
626 if (vfs_busy(mp, &nmp)) {
627 continue;
628 }
629 mutex_enter(&mp->mnt_updating);
630 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
631 asyncflag = mp->mnt_flag & MNT_ASYNC;
632 mp->mnt_flag &= ~MNT_ASYNC;
633 VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
634 if (asyncflag)
635 mp->mnt_flag |= MNT_ASYNC;
636 }
637 mutex_exit(&mp->mnt_updating);
638 vfs_unbusy(mp, false, &nmp);
639 }
640 mutex_exit(&mountlist_lock);
641 #ifdef DEBUG
642 if (syncprt)
643 vfs_bufstats();
644 #endif /* DEBUG */
645 }
646
647 /* ARGSUSED */
648 int
649 sys_sync(struct lwp *l, const void *v, register_t *retval)
650 {
651 do_sys_sync(l);
652 return (0);
653 }
654
655
656 /*
657 * Access or change filesystem quotas.
658 *
659 * (this is really 14 different calls bundled into one)
660 */
661
662 static int
663 do_sys_quotactl_stat(struct mount *mp, struct quotastat *info_u)
664 {
665 struct quotastat info_k;
666 int error;
667
668 /* ensure any padding bytes are cleared */
669 memset(&info_k, 0, sizeof(info_k));
670
671 error = vfs_quotactl_stat(mp, &info_k);
672 if (error) {
673 return error;
674 }
675
676 return copyout(&info_k, info_u, sizeof(info_k));
677 }
678
679 static int
680 do_sys_quotactl_idtypestat(struct mount *mp, int idtype,
681 struct quotaidtypestat *info_u)
682 {
683 struct quotaidtypestat info_k;
684 int error;
685
686 /* ensure any padding bytes are cleared */
687 memset(&info_k, 0, sizeof(info_k));
688
689 error = vfs_quotactl_idtypestat(mp, idtype, &info_k);
690 if (error) {
691 return error;
692 }
693
694 return copyout(&info_k, info_u, sizeof(info_k));
695 }
696
697 static int
698 do_sys_quotactl_objtypestat(struct mount *mp, int objtype,
699 struct quotaobjtypestat *info_u)
700 {
701 struct quotaobjtypestat info_k;
702 int error;
703
704 /* ensure any padding bytes are cleared */
705 memset(&info_k, 0, sizeof(info_k));
706
707 error = vfs_quotactl_objtypestat(mp, objtype, &info_k);
708 if (error) {
709 return error;
710 }
711
712 return copyout(&info_k, info_u, sizeof(info_k));
713 }
714
715 static int
716 do_sys_quotactl_get(struct mount *mp, const struct quotakey *key_u,
717 struct quotaval *val_u)
718 {
719 struct quotakey key_k;
720 struct quotaval val_k;
721 int error;
722
723 /* ensure any padding bytes are cleared */
724 memset(&val_k, 0, sizeof(val_k));
725
726 error = copyin(key_u, &key_k, sizeof(key_k));
727 if (error) {
728 return error;
729 }
730
731 error = vfs_quotactl_get(mp, &key_k, &val_k);
732 if (error) {
733 return error;
734 }
735
736 return copyout(&val_k, val_u, sizeof(val_k));
737 }
738
739 static int
740 do_sys_quotactl_put(struct mount *mp, const struct quotakey *key_u,
741 const struct quotaval *val_u)
742 {
743 struct quotakey key_k;
744 struct quotaval val_k;
745 int error;
746
747 error = copyin(key_u, &key_k, sizeof(key_k));
748 if (error) {
749 return error;
750 }
751
752 error = copyin(val_u, &val_k, sizeof(val_k));
753 if (error) {
754 return error;
755 }
756
757 return vfs_quotactl_put(mp, &key_k, &val_k);
758 }
759
760 static int
761 do_sys_quotactl_delete(struct mount *mp, const struct quotakey *key_u)
762 {
763 struct quotakey key_k;
764 int error;
765
766 error = copyin(key_u, &key_k, sizeof(key_k));
767 if (error) {
768 return error;
769 }
770
771 return vfs_quotactl_delete(mp, &key_k);
772 }
773
774 static int
775 do_sys_quotactl_cursoropen(struct mount *mp, struct quotakcursor *cursor_u)
776 {
777 struct quotakcursor cursor_k;
778 int error;
779
780 /* ensure any padding bytes are cleared */
781 memset(&cursor_k, 0, sizeof(cursor_k));
782
783 error = vfs_quotactl_cursoropen(mp, &cursor_k);
784 if (error) {
785 return error;
786 }
787
788 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
789 }
790
791 static int
792 do_sys_quotactl_cursorclose(struct mount *mp, struct quotakcursor *cursor_u)
793 {
794 struct quotakcursor cursor_k;
795 int error;
796
797 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
798 if (error) {
799 return error;
800 }
801
802 return vfs_quotactl_cursorclose(mp, &cursor_k);
803 }
804
805 static int
806 do_sys_quotactl_cursorskipidtype(struct mount *mp,
807 struct quotakcursor *cursor_u, int idtype)
808 {
809 struct quotakcursor cursor_k;
810 int error;
811
812 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
813 if (error) {
814 return error;
815 }
816
817 error = vfs_quotactl_cursorskipidtype(mp, &cursor_k, idtype);
818 if (error) {
819 return error;
820 }
821
822 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
823 }
824
825 static int
826 do_sys_quotactl_cursorget(struct mount *mp, struct quotakcursor *cursor_u,
827 struct quotakey *keys_u, struct quotaval *vals_u, unsigned maxnum,
828 unsigned *ret_u)
829 {
830 #define CGET_STACK_MAX 8
831 struct quotakcursor cursor_k;
832 struct quotakey stackkeys[CGET_STACK_MAX];
833 struct quotaval stackvals[CGET_STACK_MAX];
834 struct quotakey *keys_k;
835 struct quotaval *vals_k;
836 unsigned ret_k;
837 int error;
838
839 if (maxnum > 128) {
840 maxnum = 128;
841 }
842
843 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
844 if (error) {
845 return error;
846 }
847
848 if (maxnum <= CGET_STACK_MAX) {
849 keys_k = stackkeys;
850 vals_k = stackvals;
851 /* ensure any padding bytes are cleared */
852 memset(keys_k, 0, maxnum * sizeof(keys_k[0]));
853 memset(vals_k, 0, maxnum * sizeof(vals_k[0]));
854 } else {
855 keys_k = kmem_zalloc(maxnum * sizeof(keys_k[0]), KM_SLEEP);
856 vals_k = kmem_zalloc(maxnum * sizeof(vals_k[0]), KM_SLEEP);
857 }
858
859 error = vfs_quotactl_cursorget(mp, &cursor_k, keys_k, vals_k, maxnum,
860 &ret_k);
861 if (error) {
862 goto fail;
863 }
864
865 error = copyout(keys_k, keys_u, ret_k * sizeof(keys_k[0]));
866 if (error) {
867 goto fail;
868 }
869
870 error = copyout(vals_k, vals_u, ret_k * sizeof(vals_k[0]));
871 if (error) {
872 goto fail;
873 }
874
875 error = copyout(&ret_k, ret_u, sizeof(ret_k));
876 if (error) {
877 goto fail;
878 }
879
880 /* do last to maximize the chance of being able to recover a failure */
881 error = copyout(&cursor_k, cursor_u, sizeof(cursor_k));
882
883 fail:
884 if (keys_k != stackkeys) {
885 kmem_free(keys_k, maxnum * sizeof(keys_k[0]));
886 }
887 if (vals_k != stackvals) {
888 kmem_free(vals_k, maxnum * sizeof(vals_k[0]));
889 }
890 return error;
891 }
892
893 static int
894 do_sys_quotactl_cursoratend(struct mount *mp, struct quotakcursor *cursor_u,
895 int *ret_u)
896 {
897 struct quotakcursor cursor_k;
898 int ret_k;
899 int error;
900
901 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
902 if (error) {
903 return error;
904 }
905
906 error = vfs_quotactl_cursoratend(mp, &cursor_k, &ret_k);
907 if (error) {
908 return error;
909 }
910
911 error = copyout(&ret_k, ret_u, sizeof(ret_k));
912 if (error) {
913 return error;
914 }
915
916 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
917 }
918
919 static int
920 do_sys_quotactl_cursorrewind(struct mount *mp, struct quotakcursor *cursor_u)
921 {
922 struct quotakcursor cursor_k;
923 int error;
924
925 error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
926 if (error) {
927 return error;
928 }
929
930 error = vfs_quotactl_cursorrewind(mp, &cursor_k);
931 if (error) {
932 return error;
933 }
934
935 return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
936 }
937
938 static int
939 do_sys_quotactl_quotaon(struct mount *mp, int idtype, const char *path_u)
940 {
941 char *path_k;
942 int error;
943
944 /* XXX this should probably be a struct pathbuf */
945 path_k = PNBUF_GET();
946 error = copyin(path_u, path_k, PATH_MAX);
947 if (error) {
948 PNBUF_PUT(path_k);
949 return error;
950 }
951
952 error = vfs_quotactl_quotaon(mp, idtype, path_k);
953
954 PNBUF_PUT(path_k);
955 return error;
956 }
957
958 static int
959 do_sys_quotactl_quotaoff(struct mount *mp, int idtype)
960 {
961 return vfs_quotactl_quotaoff(mp, idtype);
962 }
963
964 int
965 do_sys_quotactl(const char *path_u, const struct quotactl_args *args)
966 {
967 struct mount *mp;
968 struct vnode *vp;
969 int error;
970
971 error = namei_simple_user(path_u, NSM_FOLLOW_TRYEMULROOT, &vp);
972 if (error != 0)
973 return (error);
974 mp = vp->v_mount;
975
976 switch (args->qc_op) {
977 case QUOTACTL_STAT:
978 error = do_sys_quotactl_stat(mp, args->u.stat.qc_info);
979 break;
980 case QUOTACTL_IDTYPESTAT:
981 error = do_sys_quotactl_idtypestat(mp,
982 args->u.idtypestat.qc_idtype,
983 args->u.idtypestat.qc_info);
984 break;
985 case QUOTACTL_OBJTYPESTAT:
986 error = do_sys_quotactl_objtypestat(mp,
987 args->u.objtypestat.qc_objtype,
988 args->u.objtypestat.qc_info);
989 break;
990 case QUOTACTL_GET:
991 error = do_sys_quotactl_get(mp,
992 args->u.get.qc_key,
993 args->u.get.qc_val);
994 break;
995 case QUOTACTL_PUT:
996 error = do_sys_quotactl_put(mp,
997 args->u.put.qc_key,
998 args->u.put.qc_val);
999 break;
1000 case QUOTACTL_DELETE:
1001 error = do_sys_quotactl_delete(mp, args->u.delete.qc_key);
1002 break;
1003 case QUOTACTL_CURSOROPEN:
1004 error = do_sys_quotactl_cursoropen(mp,
1005 args->u.cursoropen.qc_cursor);
1006 break;
1007 case QUOTACTL_CURSORCLOSE:
1008 error = do_sys_quotactl_cursorclose(mp,
1009 args->u.cursorclose.qc_cursor);
1010 break;
1011 case QUOTACTL_CURSORSKIPIDTYPE:
1012 error = do_sys_quotactl_cursorskipidtype(mp,
1013 args->u.cursorskipidtype.qc_cursor,
1014 args->u.cursorskipidtype.qc_idtype);
1015 break;
1016 case QUOTACTL_CURSORGET:
1017 error = do_sys_quotactl_cursorget(mp,
1018 args->u.cursorget.qc_cursor,
1019 args->u.cursorget.qc_keys,
1020 args->u.cursorget.qc_vals,
1021 args->u.cursorget.qc_maxnum,
1022 args->u.cursorget.qc_ret);
1023 break;
1024 case QUOTACTL_CURSORATEND:
1025 error = do_sys_quotactl_cursoratend(mp,
1026 args->u.cursoratend.qc_cursor,
1027 args->u.cursoratend.qc_ret);
1028 break;
1029 case QUOTACTL_CURSORREWIND:
1030 error = do_sys_quotactl_cursorrewind(mp,
1031 args->u.cursorrewind.qc_cursor);
1032 break;
1033 case QUOTACTL_QUOTAON:
1034 error = do_sys_quotactl_quotaon(mp,
1035 args->u.quotaon.qc_idtype,
1036 args->u.quotaon.qc_quotafile);
1037 break;
1038 case QUOTACTL_QUOTAOFF:
1039 error = do_sys_quotactl_quotaoff(mp,
1040 args->u.quotaoff.qc_idtype);
1041 break;
1042 default:
1043 error = EINVAL;
1044 break;
1045 }
1046
1047 vrele(vp);
1048 return error;
1049 }
1050
1051 /* ARGSUSED */
1052 int
1053 sys___quotactl(struct lwp *l, const struct sys___quotactl_args *uap,
1054 register_t *retval)
1055 {
1056 /* {
1057 syscallarg(const char *) path;
1058 syscallarg(struct quotactl_args *) args;
1059 } */
1060 struct quotactl_args args;
1061 int error;
1062
1063 error = copyin(SCARG(uap, args), &args, sizeof(args));
1064 if (error) {
1065 return error;
1066 }
1067
1068 return do_sys_quotactl(SCARG(uap, path), &args);
1069 }
1070
1071 int
1072 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
1073 int root)
1074 {
1075 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
1076 int error = 0;
1077
1078 /*
1079 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1080 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
1081 * overrides MNT_NOWAIT.
1082 */
1083 if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
1084 (flags != MNT_WAIT && flags != 0)) {
1085 memcpy(sp, &mp->mnt_stat, sizeof(*sp));
1086 goto done;
1087 }
1088
1089 /* Get the filesystem stats now */
1090 memset(sp, 0, sizeof(*sp));
1091 if ((error = VFS_STATVFS(mp, sp)) != 0) {
1092 return error;
1093 }
1094
1095 if (cwdi->cwdi_rdir == NULL)
1096 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
1097 done:
1098 if (cwdi->cwdi_rdir != NULL) {
1099 size_t len;
1100 char *bp;
1101 char c;
1102 char *path = PNBUF_GET();
1103
1104 bp = path + MAXPATHLEN;
1105 *--bp = '\0';
1106 rw_enter(&cwdi->cwdi_lock, RW_READER);
1107 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
1108 MAXPATHLEN / 2, 0, l);
1109 rw_exit(&cwdi->cwdi_lock);
1110 if (error) {
1111 PNBUF_PUT(path);
1112 return error;
1113 }
1114 len = strlen(bp);
1115 if (len != 1) {
1116 /*
1117 * for mount points that are below our root, we can see
1118 * them, so we fix up the pathname and return them. The
1119 * rest we cannot see, so we don't allow viewing the
1120 * data.
1121 */
1122 if (strncmp(bp, sp->f_mntonname, len) == 0 &&
1123 ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
1124 (void)strlcpy(sp->f_mntonname,
1125 c == '\0' ? "/" : &sp->f_mntonname[len],
1126 sizeof(sp->f_mntonname));
1127 } else {
1128 if (root)
1129 (void)strlcpy(sp->f_mntonname, "/",
1130 sizeof(sp->f_mntonname));
1131 else
1132 error = EPERM;
1133 }
1134 }
1135 PNBUF_PUT(path);
1136 }
1137 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1138 return error;
1139 }
1140
1141 /*
1142 * Get filesystem statistics by path.
1143 */
1144 int
1145 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
1146 {
1147 struct mount *mp;
1148 int error;
1149 struct vnode *vp;
1150
1151 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
1152 if (error != 0)
1153 return error;
1154 mp = vp->v_mount;
1155 error = dostatvfs(mp, sb, l, flags, 1);
1156 vrele(vp);
1157 return error;
1158 }
1159
1160 /* ARGSUSED */
1161 int
1162 sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
1163 {
1164 /* {
1165 syscallarg(const char *) path;
1166 syscallarg(struct statvfs *) buf;
1167 syscallarg(int) flags;
1168 } */
1169 struct statvfs *sb;
1170 int error;
1171
1172 sb = STATVFSBUF_GET();
1173 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
1174 if (error == 0)
1175 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1176 STATVFSBUF_PUT(sb);
1177 return error;
1178 }
1179
1180 /*
1181 * Get filesystem statistics by fd.
1182 */
1183 int
1184 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
1185 {
1186 file_t *fp;
1187 struct mount *mp;
1188 int error;
1189
1190 /* fd_getvnode() will use the descriptor for us */
1191 if ((error = fd_getvnode(fd, &fp)) != 0)
1192 return (error);
1193 mp = ((struct vnode *)fp->f_data)->v_mount;
1194 error = dostatvfs(mp, sb, curlwp, flags, 1);
1195 fd_putfile(fd);
1196 return error;
1197 }
1198
1199 /* ARGSUSED */
1200 int
1201 sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
1202 {
1203 /* {
1204 syscallarg(int) fd;
1205 syscallarg(struct statvfs *) buf;
1206 syscallarg(int) flags;
1207 } */
1208 struct statvfs *sb;
1209 int error;
1210
1211 sb = STATVFSBUF_GET();
1212 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
1213 if (error == 0)
1214 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1215 STATVFSBUF_PUT(sb);
1216 return error;
1217 }
1218
1219
1220 /*
1221 * Get statistics on all filesystems.
1222 */
1223 int
1224 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
1225 int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
1226 register_t *retval)
1227 {
1228 int root = 0;
1229 struct proc *p = l->l_proc;
1230 struct mount *mp, *nmp;
1231 struct statvfs *sb;
1232 size_t count, maxcount;
1233 int error = 0;
1234
1235 sb = STATVFSBUF_GET();
1236 maxcount = bufsize / entry_sz;
1237 mutex_enter(&mountlist_lock);
1238 count = 0;
1239 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1240 if (vfs_busy(mp, &nmp)) {
1241 continue;
1242 }
1243 if (sfsp && count < maxcount) {
1244 error = dostatvfs(mp, sb, l, flags, 0);
1245 if (error) {
1246 vfs_unbusy(mp, false, &nmp);
1247 error = 0;
1248 continue;
1249 }
1250 error = copyfn(sb, sfsp, entry_sz);
1251 if (error) {
1252 vfs_unbusy(mp, false, NULL);
1253 goto out;
1254 }
1255 sfsp = (char *)sfsp + entry_sz;
1256 root |= strcmp(sb->f_mntonname, "/") == 0;
1257 }
1258 count++;
1259 vfs_unbusy(mp, false, &nmp);
1260 }
1261 mutex_exit(&mountlist_lock);
1262
1263 if (root == 0 && p->p_cwdi->cwdi_rdir) {
1264 /*
1265 * fake a root entry
1266 */
1267 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
1268 sb, l, flags, 1);
1269 if (error != 0)
1270 goto out;
1271 if (sfsp) {
1272 error = copyfn(sb, sfsp, entry_sz);
1273 if (error != 0)
1274 goto out;
1275 }
1276 count++;
1277 }
1278 if (sfsp && count > maxcount)
1279 *retval = maxcount;
1280 else
1281 *retval = count;
1282 out:
1283 STATVFSBUF_PUT(sb);
1284 return error;
1285 }
1286
1287 int
1288 sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval)
1289 {
1290 /* {
1291 syscallarg(struct statvfs *) buf;
1292 syscallarg(size_t) bufsize;
1293 syscallarg(int) flags;
1294 } */
1295
1296 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
1297 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
1298 }
1299
1300 /*
1301 * Change current working directory to a given file descriptor.
1302 */
1303 /* ARGSUSED */
1304 int
1305 sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
1306 {
1307 /* {
1308 syscallarg(int) fd;
1309 } */
1310 struct proc *p = l->l_proc;
1311 struct cwdinfo *cwdi;
1312 struct vnode *vp, *tdp;
1313 struct mount *mp;
1314 file_t *fp;
1315 int error, fd;
1316
1317 /* fd_getvnode() will use the descriptor for us */
1318 fd = SCARG(uap, fd);
1319 if ((error = fd_getvnode(fd, &fp)) != 0)
1320 return (error);
1321 vp = fp->f_data;
1322
1323 vref(vp);
1324 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1325 if (vp->v_type != VDIR)
1326 error = ENOTDIR;
1327 else
1328 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1329 if (error) {
1330 vput(vp);
1331 goto out;
1332 }
1333 while ((mp = vp->v_mountedhere) != NULL) {
1334 error = vfs_busy(mp, NULL);
1335 vput(vp);
1336 if (error != 0)
1337 goto out;
1338 error = VFS_ROOT(mp, &tdp);
1339 vfs_unbusy(mp, false, NULL);
1340 if (error)
1341 goto out;
1342 vp = tdp;
1343 }
1344 VOP_UNLOCK(vp);
1345
1346 /*
1347 * Disallow changing to a directory not under the process's
1348 * current root directory (if there is one).
1349 */
1350 cwdi = p->p_cwdi;
1351 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1352 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
1353 vrele(vp);
1354 error = EPERM; /* operation not permitted */
1355 } else {
1356 vrele(cwdi->cwdi_cdir);
1357 cwdi->cwdi_cdir = vp;
1358 }
1359 rw_exit(&cwdi->cwdi_lock);
1360
1361 out:
1362 fd_putfile(fd);
1363 return (error);
1364 }
1365
1366 /*
1367 * Change this process's notion of the root directory to a given file
1368 * descriptor.
1369 */
1370 int
1371 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
1372 {
1373 struct proc *p = l->l_proc;
1374 struct vnode *vp;
1375 file_t *fp;
1376 int error, fd = SCARG(uap, fd);
1377
1378 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1379 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
1380 return error;
1381 /* fd_getvnode() will use the descriptor for us */
1382 if ((error = fd_getvnode(fd, &fp)) != 0)
1383 return error;
1384 vp = fp->f_data;
1385 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1386 if (vp->v_type != VDIR)
1387 error = ENOTDIR;
1388 else
1389 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1390 VOP_UNLOCK(vp);
1391 if (error)
1392 goto out;
1393 vref(vp);
1394
1395 change_root(p->p_cwdi, vp, l);
1396
1397 out:
1398 fd_putfile(fd);
1399 return (error);
1400 }
1401
1402 /*
1403 * Change current working directory (``.'').
1404 */
1405 /* ARGSUSED */
1406 int
1407 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
1408 {
1409 /* {
1410 syscallarg(const char *) path;
1411 } */
1412 struct proc *p = l->l_proc;
1413 struct cwdinfo *cwdi;
1414 int error;
1415 struct vnode *vp;
1416
1417 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
1418 &vp, l)) != 0)
1419 return (error);
1420 cwdi = p->p_cwdi;
1421 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1422 vrele(cwdi->cwdi_cdir);
1423 cwdi->cwdi_cdir = vp;
1424 rw_exit(&cwdi->cwdi_lock);
1425 return (0);
1426 }
1427
1428 /*
1429 * Change notion of root (``/'') directory.
1430 */
1431 /* ARGSUSED */
1432 int
1433 sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
1434 {
1435 /* {
1436 syscallarg(const char *) path;
1437 } */
1438 struct proc *p = l->l_proc;
1439 int error;
1440 struct vnode *vp;
1441
1442 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1443 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
1444 return (error);
1445 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
1446 &vp, l)) != 0)
1447 return (error);
1448
1449 change_root(p->p_cwdi, vp, l);
1450
1451 return (0);
1452 }
1453
1454 /*
1455 * Common routine for chroot and fchroot.
1456 * NB: callers need to properly authorize the change root operation.
1457 */
1458 void
1459 change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l)
1460 {
1461 struct proc *p = l->l_proc;
1462 kauth_cred_t ncred;
1463
1464 ncred = kauth_cred_alloc();
1465
1466 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1467 if (cwdi->cwdi_rdir != NULL)
1468 vrele(cwdi->cwdi_rdir);
1469 cwdi->cwdi_rdir = vp;
1470
1471 /*
1472 * Prevent escaping from chroot by putting the root under
1473 * the working directory. Silently chdir to / if we aren't
1474 * already there.
1475 */
1476 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1477 /*
1478 * XXX would be more failsafe to change directory to a
1479 * deadfs node here instead
1480 */
1481 vrele(cwdi->cwdi_cdir);
1482 vref(vp);
1483 cwdi->cwdi_cdir = vp;
1484 }
1485 rw_exit(&cwdi->cwdi_lock);
1486
1487 /* Get a write lock on the process credential. */
1488 proc_crmod_enter();
1489
1490 kauth_cred_clone(p->p_cred, ncred);
1491 kauth_proc_chroot(ncred, p->p_cwdi);
1492
1493 /* Broadcast our credentials to the process and other LWPs. */
1494 proc_crmod_leave(ncred, p->p_cred, true);
1495 }
1496
1497 /*
1498 * Common routine for chroot and chdir.
1499 * XXX "where" should be enum uio_seg
1500 */
1501 int
1502 chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l)
1503 {
1504 struct pathbuf *pb;
1505 struct nameidata nd;
1506 int error;
1507
1508 error = pathbuf_maybe_copyin(path, where, &pb);
1509 if (error) {
1510 return error;
1511 }
1512 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1513 if ((error = namei(&nd)) != 0) {
1514 pathbuf_destroy(pb);
1515 return error;
1516 }
1517 *vpp = nd.ni_vp;
1518 pathbuf_destroy(pb);
1519
1520 if ((*vpp)->v_type != VDIR)
1521 error = ENOTDIR;
1522 else
1523 error = VOP_ACCESS(*vpp, VEXEC, l->l_cred);
1524
1525 if (error)
1526 vput(*vpp);
1527 else
1528 VOP_UNLOCK(*vpp);
1529 return (error);
1530 }
1531
1532 /*
1533 * Internals of sys_open - path has already been converted into a pathbuf
1534 * (so we can easily reuse this function from other parts of the kernel,
1535 * like posix_spawn post-processing).
1536 */
1537 int
1538 do_open(lwp_t *l, struct vnode *dvp, struct pathbuf *pb, int open_flags,
1539 int open_mode, int *fd)
1540 {
1541 struct proc *p = l->l_proc;
1542 struct cwdinfo *cwdi = p->p_cwdi;
1543 file_t *fp;
1544 struct vnode *vp;
1545 int flags, cmode;
1546 int indx, error;
1547 struct nameidata nd;
1548
1549 if (open_flags & O_SEARCH) {
1550 open_flags &= ~(int)O_SEARCH;
1551 }
1552
1553 flags = FFLAGS(open_flags);
1554 if ((flags & (FREAD | FWRITE)) == 0)
1555 return EINVAL;
1556
1557 if ((error = fd_allocfile(&fp, &indx)) != 0) {
1558 return error;
1559 }
1560
1561 /* We're going to read cwdi->cwdi_cmask unlocked here. */
1562 cmode = ((open_mode &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1563 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, pb);
1564 if (dvp != NULL)
1565 NDAT(&nd, dvp);
1566
1567 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1568 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1569 fd_abort(p, fp, indx);
1570 if ((error == EDUPFD || error == EMOVEFD) &&
1571 l->l_dupfd >= 0 && /* XXX from fdopen */
1572 (error =
1573 fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) {
1574 *fd = indx;
1575 return 0;
1576 }
1577 if (error == ERESTART)
1578 error = EINTR;
1579 return error;
1580 }
1581
1582 l->l_dupfd = 0;
1583 vp = nd.ni_vp;
1584
1585 if ((error = open_setfp(l, fp, vp, indx, flags)))
1586 return error;
1587
1588 VOP_UNLOCK(vp);
1589 *fd = indx;
1590 fd_affix(p, fp, indx);
1591 return 0;
1592 }
1593
1594 int
1595 fd_open(const char *path, int open_flags, int open_mode, int *fd)
1596 {
1597 struct pathbuf *pb;
1598 int error, oflags;
1599
1600 oflags = FFLAGS(open_flags);
1601 if ((oflags & (FREAD | FWRITE)) == 0)
1602 return EINVAL;
1603
1604 pb = pathbuf_create(path);
1605 if (pb == NULL)
1606 return ENOMEM;
1607
1608 error = do_open(curlwp, NULL, pb, open_flags, open_mode, fd);
1609 pathbuf_destroy(pb);
1610
1611 return error;
1612 }
1613
1614 /*
1615 * Check permissions, allocate an open file structure,
1616 * and call the device open routine if any.
1617 */
1618 static int
1619 do_sys_openat(lwp_t *l, int fdat, const char *path, int flags,
1620 int mode, int *fd)
1621 {
1622 file_t *dfp = NULL;
1623 struct vnode *dvp = NULL;
1624 struct pathbuf *pb;
1625 int error;
1626
1627 #ifdef COMPAT_10 /* XXX: and perhaps later */
1628 if (path == NULL) {
1629 pb = pathbuf_create(".");
1630 if (pb == NULL)
1631 return ENOMEM;
1632 } else
1633 #endif
1634 {
1635 error = pathbuf_copyin(path, &pb);
1636 if (error)
1637 return error;
1638 }
1639
1640 if (fdat != AT_FDCWD) {
1641 /* fd_getvnode() will use the descriptor for us */
1642 if ((error = fd_getvnode(fdat, &dfp)) != 0)
1643 goto out;
1644
1645 dvp = dfp->f_data;
1646 }
1647
1648 error = do_open(l, dvp, pb, flags, mode, fd);
1649
1650 if (dfp != NULL)
1651 fd_putfile(fdat);
1652 out:
1653 pathbuf_destroy(pb);
1654 return error;
1655 }
1656
1657 int
1658 sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
1659 {
1660 /* {
1661 syscallarg(const char *) path;
1662 syscallarg(int) flags;
1663 syscallarg(int) mode;
1664 } */
1665 int error;
1666 int fd;
1667
1668 error = do_sys_openat(l, AT_FDCWD, SCARG(uap, path),
1669 SCARG(uap, flags), SCARG(uap, mode), &fd);
1670
1671 if (error == 0)
1672 *retval = fd;
1673
1674 return error;
1675 }
1676
1677 int
1678 sys_openat(struct lwp *l, const struct sys_openat_args *uap, register_t *retval)
1679 {
1680 /* {
1681 syscallarg(int) fd;
1682 syscallarg(const char *) path;
1683 syscallarg(int) oflags;
1684 syscallarg(int) mode;
1685 } */
1686 int error;
1687 int fd;
1688
1689 error = do_sys_openat(l, SCARG(uap, fd), SCARG(uap, path),
1690 SCARG(uap, oflags), SCARG(uap, mode), &fd);
1691
1692 if (error == 0)
1693 *retval = fd;
1694
1695 return error;
1696 }
1697
1698 static void
1699 vfs__fhfree(fhandle_t *fhp)
1700 {
1701 size_t fhsize;
1702
1703 if (fhp == NULL) {
1704 return;
1705 }
1706 fhsize = FHANDLE_SIZE(fhp);
1707 kmem_free(fhp, fhsize);
1708 }
1709
1710 /*
1711 * vfs_composefh: compose a filehandle.
1712 */
1713
1714 int
1715 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1716 {
1717 struct mount *mp;
1718 struct fid *fidp;
1719 int error;
1720 size_t needfhsize;
1721 size_t fidsize;
1722
1723 mp = vp->v_mount;
1724 fidp = NULL;
1725 if (*fh_size < FHANDLE_SIZE_MIN) {
1726 fidsize = 0;
1727 } else {
1728 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1729 if (fhp != NULL) {
1730 memset(fhp, 0, *fh_size);
1731 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1732 fidp = &fhp->fh_fid;
1733 }
1734 }
1735 error = VFS_VPTOFH(vp, fidp, &fidsize);
1736 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1737 if (error == 0 && *fh_size < needfhsize) {
1738 error = E2BIG;
1739 }
1740 *fh_size = needfhsize;
1741 return error;
1742 }
1743
1744 int
1745 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1746 {
1747 struct mount *mp;
1748 fhandle_t *fhp;
1749 size_t fhsize;
1750 size_t fidsize;
1751 int error;
1752
1753 *fhpp = NULL;
1754 mp = vp->v_mount;
1755 fidsize = 0;
1756 error = VFS_VPTOFH(vp, NULL, &fidsize);
1757 KASSERT(error != 0);
1758 if (error != E2BIG) {
1759 goto out;
1760 }
1761 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1762 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1763 if (fhp == NULL) {
1764 error = ENOMEM;
1765 goto out;
1766 }
1767 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1768 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1769 if (error == 0) {
1770 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1771 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1772 *fhpp = fhp;
1773 } else {
1774 kmem_free(fhp, fhsize);
1775 }
1776 out:
1777 return error;
1778 }
1779
1780 void
1781 vfs_composefh_free(fhandle_t *fhp)
1782 {
1783
1784 vfs__fhfree(fhp);
1785 }
1786
1787 /*
1788 * vfs_fhtovp: lookup a vnode by a filehandle.
1789 */
1790
1791 int
1792 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1793 {
1794 struct mount *mp;
1795 int error;
1796
1797 *vpp = NULL;
1798 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1799 if (mp == NULL) {
1800 error = ESTALE;
1801 goto out;
1802 }
1803 if (mp->mnt_op->vfs_fhtovp == NULL) {
1804 error = EOPNOTSUPP;
1805 goto out;
1806 }
1807 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1808 out:
1809 return error;
1810 }
1811
1812 /*
1813 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1814 * the needed size.
1815 */
1816
1817 int
1818 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1819 {
1820 fhandle_t *fhp;
1821 int error;
1822
1823 *fhpp = NULL;
1824 if (fhsize > FHANDLE_SIZE_MAX) {
1825 return EINVAL;
1826 }
1827 if (fhsize < FHANDLE_SIZE_MIN) {
1828 return EINVAL;
1829 }
1830 again:
1831 fhp = kmem_alloc(fhsize, KM_SLEEP);
1832 if (fhp == NULL) {
1833 return ENOMEM;
1834 }
1835 error = copyin(ufhp, fhp, fhsize);
1836 if (error == 0) {
1837 /* XXX this check shouldn't be here */
1838 if (FHANDLE_SIZE(fhp) == fhsize) {
1839 *fhpp = fhp;
1840 return 0;
1841 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1842 /*
1843 * a kludge for nfsv2 padded handles.
1844 */
1845 size_t sz;
1846
1847 sz = FHANDLE_SIZE(fhp);
1848 kmem_free(fhp, fhsize);
1849 fhsize = sz;
1850 goto again;
1851 } else {
1852 /*
1853 * userland told us wrong size.
1854 */
1855 error = EINVAL;
1856 }
1857 }
1858 kmem_free(fhp, fhsize);
1859 return error;
1860 }
1861
1862 void
1863 vfs_copyinfh_free(fhandle_t *fhp)
1864 {
1865
1866 vfs__fhfree(fhp);
1867 }
1868
1869 /*
1870 * Get file handle system call
1871 */
1872 int
1873 sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
1874 {
1875 /* {
1876 syscallarg(char *) fname;
1877 syscallarg(fhandle_t *) fhp;
1878 syscallarg(size_t *) fh_size;
1879 } */
1880 struct vnode *vp;
1881 fhandle_t *fh;
1882 int error;
1883 struct pathbuf *pb;
1884 struct nameidata nd;
1885 size_t sz;
1886 size_t usz;
1887
1888 /*
1889 * Must be super user
1890 */
1891 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1892 0, NULL, NULL, NULL);
1893 if (error)
1894 return (error);
1895
1896 error = pathbuf_copyin(SCARG(uap, fname), &pb);
1897 if (error) {
1898 return error;
1899 }
1900 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1901 error = namei(&nd);
1902 if (error) {
1903 pathbuf_destroy(pb);
1904 return error;
1905 }
1906 vp = nd.ni_vp;
1907 pathbuf_destroy(pb);
1908
1909 error = vfs_composefh_alloc(vp, &fh);
1910 vput(vp);
1911 if (error != 0) {
1912 goto out;
1913 }
1914 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1915 if (error != 0) {
1916 goto out;
1917 }
1918 sz = FHANDLE_SIZE(fh);
1919 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1920 if (error != 0) {
1921 goto out;
1922 }
1923 if (usz >= sz) {
1924 error = copyout(fh, SCARG(uap, fhp), sz);
1925 } else {
1926 error = E2BIG;
1927 }
1928 out:
1929 vfs_composefh_free(fh);
1930 return (error);
1931 }
1932
1933 /*
1934 * Open a file given a file handle.
1935 *
1936 * Check permissions, allocate an open file structure,
1937 * and call the device open routine if any.
1938 */
1939
1940 int
1941 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1942 register_t *retval)
1943 {
1944 file_t *fp;
1945 struct vnode *vp = NULL;
1946 kauth_cred_t cred = l->l_cred;
1947 file_t *nfp;
1948 int indx, error = 0;
1949 struct vattr va;
1950 fhandle_t *fh;
1951 int flags;
1952 proc_t *p;
1953
1954 p = curproc;
1955
1956 /*
1957 * Must be super user
1958 */
1959 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1960 0, NULL, NULL, NULL)))
1961 return (error);
1962
1963 if (oflags & O_SEARCH) {
1964 oflags &= ~(int)O_SEARCH;
1965 }
1966
1967 flags = FFLAGS(oflags);
1968 if ((flags & (FREAD | FWRITE)) == 0)
1969 return (EINVAL);
1970 if ((flags & O_CREAT))
1971 return (EINVAL);
1972 if ((error = fd_allocfile(&nfp, &indx)) != 0)
1973 return (error);
1974 fp = nfp;
1975 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1976 if (error != 0) {
1977 goto bad;
1978 }
1979 error = vfs_fhtovp(fh, &vp);
1980 if (error != 0) {
1981 goto bad;
1982 }
1983
1984 /* Now do an effective vn_open */
1985
1986 if (vp->v_type == VSOCK) {
1987 error = EOPNOTSUPP;
1988 goto bad;
1989 }
1990 error = vn_openchk(vp, cred, flags);
1991 if (error != 0)
1992 goto bad;
1993 if (flags & O_TRUNC) {
1994 VOP_UNLOCK(vp); /* XXX */
1995 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1996 vattr_null(&va);
1997 va.va_size = 0;
1998 error = VOP_SETATTR(vp, &va, cred);
1999 if (error)
2000 goto bad;
2001 }
2002 if ((error = VOP_OPEN(vp, flags, cred)) != 0)
2003 goto bad;
2004 if (flags & FWRITE) {
2005 mutex_enter(vp->v_interlock);
2006 vp->v_writecount++;
2007 mutex_exit(vp->v_interlock);
2008 }
2009
2010 /* done with modified vn_open, now finish what sys_open does. */
2011 if ((error = open_setfp(l, fp, vp, indx, flags)))
2012 return error;
2013
2014 VOP_UNLOCK(vp);
2015 *retval = indx;
2016 fd_affix(p, fp, indx);
2017 vfs_copyinfh_free(fh);
2018 return (0);
2019
2020 bad:
2021 fd_abort(p, fp, indx);
2022 if (vp != NULL)
2023 vput(vp);
2024 vfs_copyinfh_free(fh);
2025 return (error);
2026 }
2027
2028 int
2029 sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
2030 {
2031 /* {
2032 syscallarg(const void *) fhp;
2033 syscallarg(size_t) fh_size;
2034 syscallarg(int) flags;
2035 } */
2036
2037 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
2038 SCARG(uap, flags), retval);
2039 }
2040
2041 int
2042 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
2043 {
2044 int error;
2045 fhandle_t *fh;
2046 struct vnode *vp;
2047
2048 /*
2049 * Must be super user
2050 */
2051 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
2052 0, NULL, NULL, NULL)))
2053 return (error);
2054
2055 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
2056 if (error != 0)
2057 return error;
2058
2059 error = vfs_fhtovp(fh, &vp);
2060 vfs_copyinfh_free(fh);
2061 if (error != 0)
2062 return error;
2063
2064 error = vn_stat(vp, sb);
2065 vput(vp);
2066 return error;
2067 }
2068
2069
2070 /* ARGSUSED */
2071 int
2072 sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval)
2073 {
2074 /* {
2075 syscallarg(const void *) fhp;
2076 syscallarg(size_t) fh_size;
2077 syscallarg(struct stat *) sb;
2078 } */
2079 struct stat sb;
2080 int error;
2081
2082 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
2083 if (error)
2084 return error;
2085 return copyout(&sb, SCARG(uap, sb), sizeof(sb));
2086 }
2087
2088 int
2089 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
2090 int flags)
2091 {
2092 fhandle_t *fh;
2093 struct mount *mp;
2094 struct vnode *vp;
2095 int error;
2096
2097 /*
2098 * Must be super user
2099 */
2100 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
2101 0, NULL, NULL, NULL)))
2102 return error;
2103
2104 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
2105 if (error != 0)
2106 return error;
2107
2108 error = vfs_fhtovp(fh, &vp);
2109 vfs_copyinfh_free(fh);
2110 if (error != 0)
2111 return error;
2112
2113 mp = vp->v_mount;
2114 error = dostatvfs(mp, sb, l, flags, 1);
2115 vput(vp);
2116 return error;
2117 }
2118
2119 /* ARGSUSED */
2120 int
2121 sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval)
2122 {
2123 /* {
2124 syscallarg(const void *) fhp;
2125 syscallarg(size_t) fh_size;
2126 syscallarg(struct statvfs *) buf;
2127 syscallarg(int) flags;
2128 } */
2129 struct statvfs *sb = STATVFSBUF_GET();
2130 int error;
2131
2132 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
2133 SCARG(uap, flags));
2134 if (error == 0)
2135 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
2136 STATVFSBUF_PUT(sb);
2137 return error;
2138 }
2139
2140 /*
2141 * Create a special file.
2142 */
2143 /* ARGSUSED */
2144 int
2145 sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap,
2146 register_t *retval)
2147 {
2148 /* {
2149 syscallarg(const char *) path;
2150 syscallarg(mode_t) mode;
2151 syscallarg(dev_t) dev;
2152 } */
2153 return do_sys_mknodat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode),
2154 SCARG(uap, dev), retval, UIO_USERSPACE);
2155 }
2156
2157 int
2158 sys_mknodat(struct lwp *l, const struct sys_mknodat_args *uap,
2159 register_t *retval)
2160 {
2161 /* {
2162 syscallarg(int) fd;
2163 syscallarg(const char *) path;
2164 syscallarg(mode_t) mode;
2165 syscallarg(int) pad;
2166 syscallarg(dev_t) dev;
2167 } */
2168
2169 return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
2170 SCARG(uap, mode), SCARG(uap, dev), retval, UIO_USERSPACE);
2171 }
2172
2173 int
2174 do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev,
2175 register_t *retval, enum uio_seg seg)
2176 {
2177 return do_sys_mknodat(l, AT_FDCWD, pathname, mode, dev, retval, seg);
2178 }
2179
2180 int
2181 do_sys_mknodat(struct lwp *l, int fdat, const char *pathname, mode_t mode,
2182 dev_t dev, register_t *retval, enum uio_seg seg)
2183 {
2184 struct proc *p = l->l_proc;
2185 struct vnode *vp;
2186 struct vattr vattr;
2187 int error, optype;
2188 struct pathbuf *pb;
2189 struct nameidata nd;
2190 const char *pathstring;
2191
2192 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
2193 0, NULL, NULL, NULL)) != 0)
2194 return (error);
2195
2196 optype = VOP_MKNOD_DESCOFFSET;
2197
2198 error = pathbuf_maybe_copyin(pathname, seg, &pb);
2199 if (error) {
2200 return error;
2201 }
2202 pathstring = pathbuf_stringcopy_get(pb);
2203 if (pathstring == NULL) {
2204 pathbuf_destroy(pb);
2205 return ENOMEM;
2206 }
2207
2208 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
2209
2210 if ((error = fd_nameiat(l, fdat, &nd)) != 0)
2211 goto out;
2212 vp = nd.ni_vp;
2213
2214 if (vp != NULL)
2215 error = EEXIST;
2216 else {
2217 vattr_null(&vattr);
2218 /* We will read cwdi->cwdi_cmask unlocked. */
2219 vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
2220 vattr.va_rdev = dev;
2221
2222 switch (mode & S_IFMT) {
2223 case S_IFMT: /* used by badsect to flag bad sectors */
2224 vattr.va_type = VBAD;
2225 break;
2226 case S_IFCHR:
2227 vattr.va_type = VCHR;
2228 break;
2229 case S_IFBLK:
2230 vattr.va_type = VBLK;
2231 break;
2232 case S_IFWHT:
2233 optype = VOP_WHITEOUT_DESCOFFSET;
2234 break;
2235 case S_IFREG:
2236 #if NVERIEXEC > 0
2237 error = veriexec_openchk(l, nd.ni_vp, pathstring,
2238 O_CREAT);
2239 #endif /* NVERIEXEC > 0 */
2240 vattr.va_type = VREG;
2241 vattr.va_rdev = VNOVAL;
2242 optype = VOP_CREATE_DESCOFFSET;
2243 break;
2244 default:
2245 error = EINVAL;
2246 break;
2247 }
2248 }
2249 if (error == 0 && optype == VOP_MKNOD_DESCOFFSET
2250 && vattr.va_rdev == VNOVAL)
2251 error = EINVAL;
2252 if (!error) {
2253 switch (optype) {
2254 case VOP_WHITEOUT_DESCOFFSET:
2255 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
2256 if (error)
2257 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2258 vput(nd.ni_dvp);
2259 break;
2260
2261 case VOP_MKNOD_DESCOFFSET:
2262 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
2263 &nd.ni_cnd, &vattr);
2264 if (error == 0)
2265 vrele(nd.ni_vp);
2266 vput(nd.ni_dvp);
2267 break;
2268
2269 case VOP_CREATE_DESCOFFSET:
2270 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
2271 &nd.ni_cnd, &vattr);
2272 if (error == 0)
2273 vrele(nd.ni_vp);
2274 vput(nd.ni_dvp);
2275 break;
2276 }
2277 } else {
2278 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2279 if (nd.ni_dvp == vp)
2280 vrele(nd.ni_dvp);
2281 else
2282 vput(nd.ni_dvp);
2283 if (vp)
2284 vrele(vp);
2285 }
2286 out:
2287 pathbuf_stringcopy_put(pb, pathstring);
2288 pathbuf_destroy(pb);
2289 return (error);
2290 }
2291
2292 /*
2293 * Create a named pipe.
2294 */
2295 /* ARGSUSED */
2296 int
2297 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
2298 {
2299 /* {
2300 syscallarg(const char *) path;
2301 syscallarg(int) mode;
2302 } */
2303 return do_sys_mkfifoat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode));
2304 }
2305
2306 int
2307 sys_mkfifoat(struct lwp *l, const struct sys_mkfifoat_args *uap,
2308 register_t *retval)
2309 {
2310 /* {
2311 syscallarg(int) fd;
2312 syscallarg(const char *) path;
2313 syscallarg(int) mode;
2314 } */
2315
2316 return do_sys_mkfifoat(l, SCARG(uap, fd), SCARG(uap, path),
2317 SCARG(uap, mode));
2318 }
2319
2320 static int
2321 do_sys_mkfifoat(struct lwp *l, int fdat, const char *path, mode_t mode)
2322 {
2323 struct proc *p = l->l_proc;
2324 struct vattr vattr;
2325 int error;
2326 struct pathbuf *pb;
2327 struct nameidata nd;
2328
2329 error = pathbuf_copyin(path, &pb);
2330 if (error) {
2331 return error;
2332 }
2333 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
2334
2335 if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
2336 pathbuf_destroy(pb);
2337 return error;
2338 }
2339 if (nd.ni_vp != NULL) {
2340 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2341 if (nd.ni_dvp == nd.ni_vp)
2342 vrele(nd.ni_dvp);
2343 else
2344 vput(nd.ni_dvp);
2345 vrele(nd.ni_vp);
2346 pathbuf_destroy(pb);
2347 return (EEXIST);
2348 }
2349 vattr_null(&vattr);
2350 vattr.va_type = VFIFO;
2351 /* We will read cwdi->cwdi_cmask unlocked. */
2352 vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
2353 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2354 if (error == 0)
2355 vrele(nd.ni_vp);
2356 vput(nd.ni_dvp);
2357 pathbuf_destroy(pb);
2358 return (error);
2359 }
2360
2361 /*
2362 * Make a hard file link.
2363 */
2364 /* ARGSUSED */
2365 int
2366 do_sys_linkat(struct lwp *l, int fdpath, const char *path, int fdlink,
2367 const char *link, int follow, register_t *retval)
2368 {
2369 struct vnode *vp;
2370 struct pathbuf *linkpb;
2371 struct nameidata nd;
2372 namei_simple_flags_t ns_flags;
2373 int error;
2374
2375 if (follow & AT_SYMLINK_FOLLOW)
2376 ns_flags = NSM_FOLLOW_TRYEMULROOT;
2377 else
2378 ns_flags = NSM_NOFOLLOW_TRYEMULROOT;
2379
2380 error = fd_nameiat_simple_user(l, fdpath, path, ns_flags, &vp);
2381 if (error != 0)
2382 return (error);
2383 error = pathbuf_copyin(link, &linkpb);
2384 if (error) {
2385 goto out1;
2386 }
2387 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
2388 if ((error = fd_nameiat(l, fdlink, &nd)) != 0)
2389 goto out2;
2390 if (nd.ni_vp) {
2391 error = EEXIST;
2392 goto abortop;
2393 }
2394 /* Prevent hard links on directories. */
2395 if (vp->v_type == VDIR) {
2396 error = EPERM;
2397 goto abortop;
2398 }
2399 /* Prevent cross-mount operation. */
2400 if (nd.ni_dvp->v_mount != vp->v_mount) {
2401 error = EXDEV;
2402 goto abortop;
2403 }
2404 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2405 out2:
2406 pathbuf_destroy(linkpb);
2407 out1:
2408 vrele(vp);
2409 return (error);
2410 abortop:
2411 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2412 if (nd.ni_dvp == nd.ni_vp)
2413 vrele(nd.ni_dvp);
2414 else
2415 vput(nd.ni_dvp);
2416 if (nd.ni_vp != NULL)
2417 vrele(nd.ni_vp);
2418 goto out2;
2419 }
2420
2421 int
2422 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
2423 {
2424 /* {
2425 syscallarg(const char *) path;
2426 syscallarg(const char *) link;
2427 } */
2428 const char *path = SCARG(uap, path);
2429 const char *link = SCARG(uap, link);
2430
2431 return do_sys_linkat(l, AT_FDCWD, path, AT_FDCWD, link,
2432 AT_SYMLINK_FOLLOW, retval);
2433 }
2434
2435 int
2436 sys_linkat(struct lwp *l, const struct sys_linkat_args *uap,
2437 register_t *retval)
2438 {
2439 /* {
2440 syscallarg(int) fd1;
2441 syscallarg(const char *) name1;
2442 syscallarg(int) fd2;
2443 syscallarg(const char *) name2;
2444 syscallarg(int) flags;
2445 } */
2446 int fd1 = SCARG(uap, fd1);
2447 const char *name1 = SCARG(uap, name1);
2448 int fd2 = SCARG(uap, fd2);
2449 const char *name2 = SCARG(uap, name2);
2450 int follow;
2451
2452 follow = SCARG(uap, flags) & AT_SYMLINK_FOLLOW;
2453
2454 return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
2455 }
2456
2457
2458 int
2459 do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
2460 {
2461 return do_sys_symlinkat(NULL, patharg, AT_FDCWD, link, seg);
2462 }
2463
2464 static int
2465 do_sys_symlinkat(struct lwp *l, const char *patharg, int fdat,
2466 const char *link, enum uio_seg seg)
2467 {
2468 struct proc *p = curproc;
2469 struct vattr vattr;
2470 char *path;
2471 int error;
2472 struct pathbuf *linkpb;
2473 struct nameidata nd;
2474
2475 KASSERT(l != NULL || fdat == AT_FDCWD);
2476
2477 path = PNBUF_GET();
2478 if (seg == UIO_USERSPACE) {
2479 if ((error = copyinstr(patharg, path, MAXPATHLEN, NULL)) != 0)
2480 goto out1;
2481 if ((error = pathbuf_copyin(link, &linkpb)) != 0)
2482 goto out1;
2483 } else {
2484 KASSERT(strlen(patharg) < MAXPATHLEN);
2485 strcpy(path, patharg);
2486 linkpb = pathbuf_create(link);
2487 if (linkpb == NULL) {
2488 error = ENOMEM;
2489 goto out1;
2490 }
2491 }
2492 ktrkuser("symlink-target", path, strlen(path));
2493
2494 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
2495 if ((error = fd_nameiat(l, fdat, &nd)) != 0)
2496 goto out2;
2497 if (nd.ni_vp) {
2498 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2499 if (nd.ni_dvp == nd.ni_vp)
2500 vrele(nd.ni_dvp);
2501 else
2502 vput(nd.ni_dvp);
2503 vrele(nd.ni_vp);
2504 error = EEXIST;
2505 goto out2;
2506 }
2507 vattr_null(&vattr);
2508 vattr.va_type = VLNK;
2509 /* We will read cwdi->cwdi_cmask unlocked. */
2510 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
2511 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
2512 if (error == 0)
2513 vrele(nd.ni_vp);
2514 vput(nd.ni_dvp);
2515 out2:
2516 pathbuf_destroy(linkpb);
2517 out1:
2518 PNBUF_PUT(path);
2519 return (error);
2520 }
2521
2522 /*
2523 * Make a symbolic link.
2524 */
2525 /* ARGSUSED */
2526 int
2527 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
2528 {
2529 /* {
2530 syscallarg(const char *) path;
2531 syscallarg(const char *) link;
2532 } */
2533
2534 return do_sys_symlinkat(l, SCARG(uap, path), AT_FDCWD, SCARG(uap, link),
2535 UIO_USERSPACE);
2536 }
2537
2538 int
2539 sys_symlinkat(struct lwp *l, const struct sys_symlinkat_args *uap,
2540 register_t *retval)
2541 {
2542 /* {
2543 syscallarg(const char *) path1;
2544 syscallarg(int) fd;
2545 syscallarg(const char *) path2;
2546 } */
2547
2548 return do_sys_symlinkat(l, SCARG(uap, path1), SCARG(uap, fd),
2549 SCARG(uap, path2), UIO_USERSPACE);
2550 }
2551
2552 /*
2553 * Delete a whiteout from the filesystem.
2554 */
2555 /* ARGSUSED */
2556 int
2557 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
2558 {
2559 /* {
2560 syscallarg(const char *) path;
2561 } */
2562 int error;
2563 struct pathbuf *pb;
2564 struct nameidata nd;
2565
2566 error = pathbuf_copyin(SCARG(uap, path), &pb);
2567 if (error) {
2568 return error;
2569 }
2570
2571 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
2572 error = namei(&nd);
2573 if (error) {
2574 pathbuf_destroy(pb);
2575 return (error);
2576 }
2577
2578 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2579 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2580 if (nd.ni_dvp == nd.ni_vp)
2581 vrele(nd.ni_dvp);
2582 else
2583 vput(nd.ni_dvp);
2584 if (nd.ni_vp)
2585 vrele(nd.ni_vp);
2586 pathbuf_destroy(pb);
2587 return (EEXIST);
2588 }
2589 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
2590 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2591 vput(nd.ni_dvp);
2592 pathbuf_destroy(pb);
2593 return (error);
2594 }
2595
2596 /*
2597 * Delete a name from the filesystem.
2598 */
2599 /* ARGSUSED */
2600 int
2601 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
2602 {
2603 /* {
2604 syscallarg(const char *) path;
2605 } */
2606
2607 return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path), 0, UIO_USERSPACE);
2608 }
2609
2610 int
2611 sys_unlinkat(struct lwp *l, const struct sys_unlinkat_args *uap,
2612 register_t *retval)
2613 {
2614 /* {
2615 syscallarg(int) fd;
2616 syscallarg(const char *) path;
2617 syscallarg(int) flag;
2618 } */
2619
2620 return do_sys_unlinkat(l, SCARG(uap, fd), SCARG(uap, path),
2621 SCARG(uap, flag), UIO_USERSPACE);
2622 }
2623
2624 int
2625 do_sys_unlink(const char *arg, enum uio_seg seg)
2626 {
2627 return do_sys_unlinkat(NULL, AT_FDCWD, arg, 0, seg);
2628 }
2629
2630 static int
2631 do_sys_unlinkat(struct lwp *l, int fdat, const char *arg, int flags,
2632 enum uio_seg seg)
2633 {
2634 struct vnode *vp;
2635 int error;
2636 struct pathbuf *pb;
2637 struct nameidata nd;
2638 const char *pathstring;
2639
2640 KASSERT(l != NULL || fdat == AT_FDCWD);
2641
2642 error = pathbuf_maybe_copyin(arg, seg, &pb);
2643 if (error) {
2644 return error;
2645 }
2646 pathstring = pathbuf_stringcopy_get(pb);
2647 if (pathstring == NULL) {
2648 pathbuf_destroy(pb);
2649 return ENOMEM;
2650 }
2651
2652 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
2653 if ((error = fd_nameiat(l, fdat, &nd)) != 0)
2654 goto out;
2655 vp = nd.ni_vp;
2656
2657 /*
2658 * The root of a mounted filesystem cannot be deleted.
2659 */
2660 if ((vp->v_vflag & VV_ROOT) != 0) {
2661 error = EBUSY;
2662 goto abort;
2663 }
2664
2665 if ((vp->v_type == VDIR) && (vp->v_mountedhere != NULL)) {
2666 error = EBUSY;
2667 goto abort;
2668 }
2669
2670 /*
2671 * No rmdir "." please.
2672 */
2673 if (nd.ni_dvp == vp) {
2674 error = EINVAL;
2675 goto abort;
2676 }
2677
2678 /*
2679 * AT_REMOVEDIR is required to remove a directory
2680 */
2681 if (vp->v_type == VDIR) {
2682 if (!(flags & AT_REMOVEDIR)) {
2683 error = EPERM;
2684 goto abort;
2685 } else {
2686 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2687 goto out;
2688 }
2689 }
2690
2691 /*
2692 * Starting here we only deal with non directories.
2693 */
2694 if (flags & AT_REMOVEDIR) {
2695 error = ENOTDIR;
2696 goto abort;
2697 }
2698
2699
2700 #if NVERIEXEC > 0
2701 /* Handle remove requests for veriexec entries. */
2702 if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
2703 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2704 if (nd.ni_dvp == vp)
2705 vrele(nd.ni_dvp);
2706 else
2707 vput(nd.ni_dvp);
2708 vput(vp);
2709 goto out;
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_data;
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_data;
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_data;
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_data;
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_data, 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_data, 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_data, 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_data, 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_data, 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 error = namei_simple_user(SCARG(uap, path),
3922 NSM_FOLLOW_TRYEMULROOT, &vp);
3923 if (error != 0)
3924 return (error);
3925 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3926 if (vp->v_type == VDIR)
3927 error = EISDIR;
3928 else if ((error = vn_writechk(vp)) == 0 &&
3929 (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
3930 vattr_null(&vattr);
3931 vattr.va_size = SCARG(uap, length);
3932 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3933 }
3934 vput(vp);
3935 return (error);
3936 }
3937
3938 /*
3939 * Truncate a file given a file descriptor.
3940 */
3941 /* ARGSUSED */
3942 int
3943 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
3944 {
3945 /* {
3946 syscallarg(int) fd;
3947 syscallarg(int) pad;
3948 syscallarg(off_t) length;
3949 } */
3950 struct vattr vattr;
3951 struct vnode *vp;
3952 file_t *fp;
3953 int error;
3954
3955 /* fd_getvnode() will use the descriptor for us */
3956 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3957 return (error);
3958 if ((fp->f_flag & FWRITE) == 0) {
3959 error = EINVAL;
3960 goto out;
3961 }
3962 vp = fp->f_data;
3963 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3964 if (vp->v_type == VDIR)
3965 error = EISDIR;
3966 else if ((error = vn_writechk(vp)) == 0) {
3967 vattr_null(&vattr);
3968 vattr.va_size = SCARG(uap, length);
3969 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3970 }
3971 VOP_UNLOCK(vp);
3972 out:
3973 fd_putfile(SCARG(uap, fd));
3974 return (error);
3975 }
3976
3977 /*
3978 * Sync an open file.
3979 */
3980 /* ARGSUSED */
3981 int
3982 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
3983 {
3984 /* {
3985 syscallarg(int) fd;
3986 } */
3987 struct vnode *vp;
3988 file_t *fp;
3989 int error;
3990
3991 /* fd_getvnode() will use the descriptor for us */
3992 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3993 return (error);
3994 vp = fp->f_data;
3995 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3996 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3997 VOP_UNLOCK(vp);
3998 fd_putfile(SCARG(uap, fd));
3999 return (error);
4000 }
4001
4002 /*
4003 * Sync a range of file data. API modeled after that found in AIX.
4004 *
4005 * FDATASYNC indicates that we need only save enough metadata to be able
4006 * to re-read the written data. Note we duplicate AIX's requirement that
4007 * the file be open for writing.
4008 */
4009 /* ARGSUSED */
4010 int
4011 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
4012 {
4013 /* {
4014 syscallarg(int) fd;
4015 syscallarg(int) flags;
4016 syscallarg(off_t) start;
4017 syscallarg(off_t) length;
4018 } */
4019 struct vnode *vp;
4020 file_t *fp;
4021 int flags, nflags;
4022 off_t s, e, len;
4023 int error;
4024
4025 /* fd_getvnode() will use the descriptor for us */
4026 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4027 return (error);
4028
4029 if ((fp->f_flag & FWRITE) == 0) {
4030 error = EBADF;
4031 goto out;
4032 }
4033
4034 flags = SCARG(uap, flags);
4035 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
4036 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
4037 error = EINVAL;
4038 goto out;
4039 }
4040 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
4041 if (flags & FDATASYNC)
4042 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
4043 else
4044 nflags = FSYNC_WAIT;
4045 if (flags & FDISKSYNC)
4046 nflags |= FSYNC_CACHE;
4047
4048 len = SCARG(uap, length);
4049 /* If length == 0, we do the whole file, and s = e = 0 will do that */
4050 if (len) {
4051 s = SCARG(uap, start);
4052 e = s + len;
4053 if (e < s) {
4054 error = EINVAL;
4055 goto out;
4056 }
4057 } else {
4058 e = 0;
4059 s = 0;
4060 }
4061
4062 vp = fp->f_data;
4063 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4064 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
4065 VOP_UNLOCK(vp);
4066 out:
4067 fd_putfile(SCARG(uap, fd));
4068 return (error);
4069 }
4070
4071 /*
4072 * Sync the data of an open file.
4073 */
4074 /* ARGSUSED */
4075 int
4076 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
4077 {
4078 /* {
4079 syscallarg(int) fd;
4080 } */
4081 struct vnode *vp;
4082 file_t *fp;
4083 int error;
4084
4085 /* fd_getvnode() will use the descriptor for us */
4086 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4087 return (error);
4088 if ((fp->f_flag & FWRITE) == 0) {
4089 fd_putfile(SCARG(uap, fd));
4090 return (EBADF);
4091 }
4092 vp = fp->f_data;
4093 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4094 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
4095 VOP_UNLOCK(vp);
4096 fd_putfile(SCARG(uap, fd));
4097 return (error);
4098 }
4099
4100 /*
4101 * Rename files, (standard) BSD semantics frontend.
4102 */
4103 /* ARGSUSED */
4104 int
4105 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
4106 {
4107 /* {
4108 syscallarg(const char *) from;
4109 syscallarg(const char *) to;
4110 } */
4111
4112 return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
4113 SCARG(uap, to), UIO_USERSPACE, 0));
4114 }
4115
4116 int
4117 sys_renameat(struct lwp *l, const struct sys_renameat_args *uap,
4118 register_t *retval)
4119 {
4120 /* {
4121 syscallarg(int) fromfd;
4122 syscallarg(const char *) from;
4123 syscallarg(int) tofd;
4124 syscallarg(const char *) to;
4125 } */
4126
4127 return (do_sys_renameat(l, SCARG(uap, fromfd), SCARG(uap, from),
4128 SCARG(uap, tofd), SCARG(uap, to), UIO_USERSPACE, 0));
4129 }
4130
4131 /*
4132 * Rename files, POSIX semantics frontend.
4133 */
4134 /* ARGSUSED */
4135 int
4136 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
4137 {
4138 /* {
4139 syscallarg(const char *) from;
4140 syscallarg(const char *) to;
4141 } */
4142
4143 return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
4144 SCARG(uap, to), UIO_USERSPACE, 1));
4145 }
4146
4147 /*
4148 * Rename files. Source and destination must either both be directories,
4149 * or both not be directories. If target is a directory, it must be empty.
4150 * If `from' and `to' refer to the same object, the value of the `retain'
4151 * argument is used to determine whether `from' will be
4152 *
4153 * (retain == 0) deleted unless `from' and `to' refer to the same
4154 * object in the file system's name space (BSD).
4155 * (retain == 1) always retained (POSIX).
4156 *
4157 * XXX Synchronize with nfsrv_rename in nfs_serv.c.
4158 */
4159 int
4160 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
4161 {
4162 return do_sys_renameat(NULL, AT_FDCWD, from, AT_FDCWD, to, seg, retain);
4163 }
4164
4165 static int
4166 do_sys_renameat(struct lwp *l, int fromfd, const char *from, int tofd,
4167 const char *to, enum uio_seg seg, int retain)
4168 {
4169 struct pathbuf *fpb, *tpb;
4170 struct nameidata fnd, tnd;
4171 struct vnode *fdvp, *fvp;
4172 struct vnode *tdvp, *tvp;
4173 struct mount *mp, *tmp;
4174 int error;
4175
4176 KASSERT(l != NULL || (fromfd == AT_FDCWD && tofd == AT_FDCWD));
4177
4178 error = pathbuf_maybe_copyin(from, seg, &fpb);
4179 if (error)
4180 goto out0;
4181 KASSERT(fpb != NULL);
4182
4183 error = pathbuf_maybe_copyin(to, seg, &tpb);
4184 if (error)
4185 goto out1;
4186 KASSERT(tpb != NULL);
4187
4188 /*
4189 * Lookup from.
4190 *
4191 * XXX LOCKPARENT is wrong because we don't actually want it
4192 * locked yet, but (a) namei is insane, and (b) VOP_RENAME is
4193 * insane, so for the time being we need to leave it like this.
4194 */
4195 NDINIT(&fnd, DELETE, (LOCKPARENT | TRYEMULROOT | INRENAME), fpb);
4196 if ((error = fd_nameiat(l, fromfd, &fnd)) != 0)
4197 goto out2;
4198
4199 /*
4200 * Pull out the important results of the lookup, fdvp and fvp.
4201 * Of course, fvp is bogus because we're about to unlock fdvp.
4202 */
4203 fdvp = fnd.ni_dvp;
4204 fvp = fnd.ni_vp;
4205 KASSERT(fdvp != NULL);
4206 KASSERT(fvp != NULL);
4207 KASSERT((fdvp == fvp) || (VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE));
4208
4209 /*
4210 * Make sure neither fdvp nor fvp is locked.
4211 */
4212 if (fdvp != fvp)
4213 VOP_UNLOCK(fdvp);
4214 /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4215 /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4216
4217 /*
4218 * Reject renaming `.' and `..'. Can't do this until after
4219 * namei because we need namei's parsing to find the final
4220 * component name. (namei should just leave us with the final
4221 * component name and not look it up itself, but anyway...)
4222 *
4223 * This was here before because we used to relookup from
4224 * instead of to and relookup requires the caller to check
4225 * this, but now file systems may depend on this check, so we
4226 * must retain it until the file systems are all rototilled.
4227 */
4228 if (((fnd.ni_cnd.cn_namelen == 1) &&
4229 (fnd.ni_cnd.cn_nameptr[0] == '.')) ||
4230 ((fnd.ni_cnd.cn_namelen == 2) &&
4231 (fnd.ni_cnd.cn_nameptr[0] == '.') &&
4232 (fnd.ni_cnd.cn_nameptr[1] == '.'))) {
4233 error = EINVAL; /* XXX EISDIR? */
4234 goto abort0;
4235 }
4236
4237 /*
4238 * Lookup to.
4239 *
4240 * XXX LOCKPARENT is wrong, but...insanity, &c. Also, using
4241 * fvp here to decide whether to add CREATEDIR is a load of
4242 * bollocks because fvp might be the wrong node by now, since
4243 * fdvp is unlocked.
4244 *
4245 * XXX Why not pass CREATEDIR always?
4246 */
4247 NDINIT(&tnd, RENAME,
4248 (LOCKPARENT | NOCACHE | TRYEMULROOT | INRENAME |
4249 ((fvp->v_type == VDIR)? CREATEDIR : 0)),
4250 tpb);
4251 if ((error = fd_nameiat(l, tofd, &tnd)) != 0)
4252 goto abort0;
4253
4254 /*
4255 * Pull out the important results of the lookup, tdvp and tvp.
4256 * Of course, tvp is bogus because we're about to unlock tdvp.
4257 */
4258 tdvp = tnd.ni_dvp;
4259 tvp = tnd.ni_vp;
4260 KASSERT(tdvp != NULL);
4261 KASSERT((tdvp == tvp) || (VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE));
4262
4263 /*
4264 * Make sure neither tdvp nor tvp is locked.
4265 */
4266 if (tdvp != tvp)
4267 VOP_UNLOCK(tdvp);
4268 /* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
4269 /* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
4270
4271 /*
4272 * Reject renaming onto `.' or `..'. relookup is unhappy with
4273 * these, which is why we must do this here. Once upon a time
4274 * we relooked up from instead of to, and consequently didn't
4275 * need this check, but now that we relookup to instead of
4276 * from, we need this; and we shall need it forever forward
4277 * until the VOP_RENAME protocol changes, because file systems
4278 * will no doubt begin to depend on this check.
4279 */
4280 if (((tnd.ni_cnd.cn_namelen == 1) &&
4281 (tnd.ni_cnd.cn_nameptr[0] == '.')) ||
4282 ((tnd.ni_cnd.cn_namelen == 2) &&
4283 (tnd.ni_cnd.cn_nameptr[0] == '.') &&
4284 (tnd.ni_cnd.cn_nameptr[1] == '.'))) {
4285 error = EINVAL; /* XXX EISDIR? */
4286 goto abort1;
4287 }
4288
4289 /*
4290 * Get the mount point. If the file system has been unmounted,
4291 * which it may be because we're not holding any vnode locks,
4292 * then v_mount will be NULL. We're not really supposed to
4293 * read v_mount without holding the vnode lock, but since we
4294 * have fdvp referenced, if fdvp->v_mount changes then at worst
4295 * it will be set to NULL, not changed to another mount point.
4296 * And, of course, since it is up to the file system to
4297 * determine the real lock order, we can't lock both fdvp and
4298 * tdvp at the same time.
4299 */
4300 mp = fdvp->v_mount;
4301 if (mp == NULL) {
4302 error = ENOENT;
4303 goto abort1;
4304 }
4305
4306 /*
4307 * Make sure the mount points match. Again, although we don't
4308 * hold any vnode locks, the v_mount fields may change -- but
4309 * at worst they will change to NULL, so this will never become
4310 * a cross-device rename, because we hold vnode references.
4311 *
4312 * XXX Because nothing is locked and the compiler may reorder
4313 * things here, unmounting the file system at an inopportune
4314 * moment may cause rename to fail with ENXDEV when it really
4315 * should fail with ENOENT.
4316 */
4317 tmp = tdvp->v_mount;
4318 if (tmp == NULL) {
4319 error = ENOENT;
4320 goto abort1;
4321 }
4322
4323 if (mp != tmp) {
4324 error = EXDEV;
4325 goto abort1;
4326 }
4327
4328 /*
4329 * Take the vfs rename lock to avoid cross-directory screw cases.
4330 * Nothing is locked currently, so taking this lock is safe.
4331 */
4332 error = VFS_RENAMELOCK_ENTER(mp);
4333 if (error)
4334 goto abort1;
4335
4336 /*
4337 * Now fdvp, fvp, tdvp, and (if nonnull) tvp are referenced,
4338 * and nothing is locked except for the vfs rename lock.
4339 *
4340 * The next step is a little rain dance to conform to the
4341 * insane lock protocol, even though it does nothing to ward
4342 * off race conditions.
4343 *
4344 * We need tdvp and tvp to be locked. However, because we have
4345 * unlocked tdvp in order to hold no locks while we take the
4346 * vfs rename lock, tvp may be wrong here, and we can't safely
4347 * lock it even if the sensible file systems will just unlock
4348 * it straight away. Consequently, we must lock tdvp and then
4349 * relookup tvp to get it locked.
4350 *
4351 * Finally, because the VOP_RENAME protocol is brain-damaged
4352 * and various file systems insanely depend on the semantics of
4353 * this brain damage, the lookup of to must be the last lookup
4354 * before VOP_RENAME.
4355 */
4356 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
4357 error = relookup(tdvp, &tnd.ni_vp, &tnd.ni_cnd, 0);
4358 if (error)
4359 goto abort2;
4360
4361 /*
4362 * Drop the old tvp and pick up the new one -- which might be
4363 * the same, but that doesn't matter to us. After this, tdvp
4364 * and tvp should both be locked.
4365 */
4366 if (tvp != NULL)
4367 vrele(tvp);
4368 tvp = tnd.ni_vp;
4369 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
4370 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
4371
4372 /*
4373 * The old do_sys_rename had various consistency checks here
4374 * involving fvp and tvp. fvp is bogus already here, and tvp
4375 * will become bogus soon in any sensible file system, so the
4376 * only purpose in putting these checks here is to give lip
4377 * service to these screw cases and to acknowledge that they
4378 * exist, not actually to handle them, but here you go
4379 * anyway...
4380 */
4381
4382 /*
4383 * Acknowledge that directories and non-directories aren't
4384 * suposed to mix.
4385 */
4386 if (tvp != NULL) {
4387 if ((fvp->v_type == VDIR) && (tvp->v_type != VDIR)) {
4388 error = ENOTDIR;
4389 goto abort3;
4390 } else if ((fvp->v_type != VDIR) && (tvp->v_type == VDIR)) {
4391 error = EISDIR;
4392 goto abort3;
4393 }
4394 }
4395
4396 /*
4397 * Acknowledge some random screw case, among the dozens that
4398 * might arise.
4399 */
4400 if (fvp == tdvp) {
4401 error = EINVAL;
4402 goto abort3;
4403 }
4404
4405 /*
4406 * Acknowledge that POSIX has a wacky screw case.
4407 *
4408 * XXX Eventually the retain flag needs to be passed on to
4409 * VOP_RENAME.
4410 */
4411 if (fvp == tvp) {
4412 if (retain) {
4413 error = 0;
4414 goto abort3;
4415 } else if ((fdvp == tdvp) &&
4416 (fnd.ni_cnd.cn_namelen == tnd.ni_cnd.cn_namelen) &&
4417 (0 == memcmp(fnd.ni_cnd.cn_nameptr, tnd.ni_cnd.cn_nameptr,
4418 fnd.ni_cnd.cn_namelen))) {
4419 error = 0;
4420 goto abort3;
4421 }
4422 }
4423
4424 /*
4425 * Make sure veriexec can screw us up. (But a race can screw
4426 * up veriexec, of course -- remember, fvp and (soon) tvp are
4427 * bogus.)
4428 */
4429 #if NVERIEXEC > 0
4430 {
4431 char *f1, *f2;
4432 size_t f1_len;
4433 size_t f2_len;
4434
4435 f1_len = fnd.ni_cnd.cn_namelen + 1;
4436 f1 = kmem_alloc(f1_len, KM_SLEEP);
4437 strlcpy(f1, fnd.ni_cnd.cn_nameptr, f1_len);
4438
4439 f2_len = tnd.ni_cnd.cn_namelen + 1;
4440 f2 = kmem_alloc(f2_len, KM_SLEEP);
4441 strlcpy(f2, tnd.ni_cnd.cn_nameptr, f2_len);
4442
4443 error = veriexec_renamechk(curlwp, fvp, f1, tvp, f2);
4444
4445 kmem_free(f1, f1_len);
4446 kmem_free(f2, f2_len);
4447
4448 if (error)
4449 goto abort3;
4450 }
4451 #endif /* NVERIEXEC > 0 */
4452
4453 /*
4454 * All ready. Incant the rename vop.
4455 */
4456 /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4457 /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4458 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
4459 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
4460 error = VOP_RENAME(fdvp, fvp, &fnd.ni_cnd, tdvp, tvp, &tnd.ni_cnd);
4461
4462 /*
4463 * VOP_RENAME releases fdvp, fvp, tdvp, and tvp, and unlocks
4464 * tdvp and tvp. But we can't assert any of that.
4465 */
4466 /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4467 /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4468 /* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
4469 /* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
4470
4471 /*
4472 * So all we have left to do is to drop the rename lock and
4473 * destroy the pathbufs.
4474 */
4475 VFS_RENAMELOCK_EXIT(mp);
4476 goto out2;
4477
4478 abort3: if ((tvp != NULL) && (tvp != tdvp))
4479 VOP_UNLOCK(tvp);
4480 abort2: VOP_UNLOCK(tdvp);
4481 VFS_RENAMELOCK_EXIT(mp);
4482 abort1: VOP_ABORTOP(tdvp, &tnd.ni_cnd);
4483 vrele(tdvp);
4484 if (tvp != NULL)
4485 vrele(tvp);
4486 abort0: VOP_ABORTOP(fdvp, &fnd.ni_cnd);
4487 vrele(fdvp);
4488 vrele(fvp);
4489 out2: pathbuf_destroy(tpb);
4490 out1: pathbuf_destroy(fpb);
4491 out0: return error;
4492 }
4493
4494 /*
4495 * Make a directory file.
4496 */
4497 /* ARGSUSED */
4498 int
4499 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
4500 {
4501 /* {
4502 syscallarg(const char *) path;
4503 syscallarg(int) mode;
4504 } */
4505
4506 return do_sys_mkdirat(l, AT_FDCWD, SCARG(uap, path),
4507 SCARG(uap, mode), UIO_USERSPACE);
4508 }
4509
4510 int
4511 sys_mkdirat(struct lwp *l, const struct sys_mkdirat_args *uap,
4512 register_t *retval)
4513 {
4514 /* {
4515 syscallarg(int) fd;
4516 syscallarg(const char *) path;
4517 syscallarg(int) mode;
4518 } */
4519
4520 return do_sys_mkdirat(l, SCARG(uap, fd), SCARG(uap, path),
4521 SCARG(uap, mode), UIO_USERSPACE);
4522 }
4523
4524
4525 int
4526 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
4527 {
4528 return do_sys_mkdirat(NULL, AT_FDCWD, path, mode, UIO_USERSPACE);
4529 }
4530
4531 static int
4532 do_sys_mkdirat(struct lwp *l, int fdat, const char *path, mode_t mode,
4533 enum uio_seg seg)
4534 {
4535 struct proc *p = curlwp->l_proc;
4536 struct vnode *vp;
4537 struct vattr vattr;
4538 int error;
4539 struct pathbuf *pb;
4540 struct nameidata nd;
4541
4542 KASSERT(l != NULL || fdat == AT_FDCWD);
4543
4544 /* XXX bollocks, should pass in a pathbuf */
4545 error = pathbuf_maybe_copyin(path, seg, &pb);
4546 if (error) {
4547 return error;
4548 }
4549
4550 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
4551
4552 if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
4553 pathbuf_destroy(pb);
4554 return (error);
4555 }
4556 vp = nd.ni_vp;
4557 if (vp != NULL) {
4558 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
4559 if (nd.ni_dvp == vp)
4560 vrele(nd.ni_dvp);
4561 else
4562 vput(nd.ni_dvp);
4563 vrele(vp);
4564 pathbuf_destroy(pb);
4565 return (EEXIST);
4566 }
4567 vattr_null(&vattr);
4568 vattr.va_type = VDIR;
4569 /* We will read cwdi->cwdi_cmask unlocked. */
4570 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
4571 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
4572 if (!error)
4573 vrele(nd.ni_vp);
4574 vput(nd.ni_dvp);
4575 pathbuf_destroy(pb);
4576 return (error);
4577 }
4578
4579 /*
4580 * Remove a directory file.
4581 */
4582 /* ARGSUSED */
4583 int
4584 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
4585 {
4586 return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path),
4587 AT_REMOVEDIR, UIO_USERSPACE);
4588 }
4589
4590 /*
4591 * Read a block of directory entries in a file system independent format.
4592 */
4593 int
4594 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
4595 {
4596 /* {
4597 syscallarg(int) fd;
4598 syscallarg(char *) buf;
4599 syscallarg(size_t) count;
4600 } */
4601 file_t *fp;
4602 int error, done;
4603
4604 /* fd_getvnode() will use the descriptor for us */
4605 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4606 return (error);
4607 if ((fp->f_flag & FREAD) == 0) {
4608 error = EBADF;
4609 goto out;
4610 }
4611 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
4612 SCARG(uap, count), &done, l, 0, 0);
4613 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
4614 *retval = done;
4615 out:
4616 fd_putfile(SCARG(uap, fd));
4617 return (error);
4618 }
4619
4620 /*
4621 * Set the mode mask for creation of filesystem nodes.
4622 */
4623 int
4624 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
4625 {
4626 /* {
4627 syscallarg(mode_t) newmask;
4628 } */
4629 struct proc *p = l->l_proc;
4630 struct cwdinfo *cwdi;
4631
4632 /*
4633 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
4634 * important is that we serialize changes to the mask. The
4635 * rw_exit() will issue a write memory barrier on our behalf,
4636 * and force the changes out to other CPUs (as it must use an
4637 * atomic operation, draining the local CPU's store buffers).
4638 */
4639 cwdi = p->p_cwdi;
4640 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
4641 *retval = cwdi->cwdi_cmask;
4642 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
4643 rw_exit(&cwdi->cwdi_lock);
4644
4645 return (0);
4646 }
4647
4648 int
4649 dorevoke(struct vnode *vp, kauth_cred_t cred)
4650 {
4651 struct vattr vattr;
4652 int error, fs_decision;
4653
4654 vn_lock(vp, LK_SHARED | LK_RETRY);
4655 error = VOP_GETATTR(vp, &vattr, cred);
4656 VOP_UNLOCK(vp);
4657 if (error != 0)
4658 return error;
4659 fs_decision = (kauth_cred_geteuid(cred) == vattr.va_uid) ? 0 : EPERM;
4660 error = kauth_authorize_vnode(cred, KAUTH_VNODE_REVOKE, vp, NULL,
4661 fs_decision);
4662 if (!error)
4663 VOP_REVOKE(vp, REVOKEALL);
4664 return (error);
4665 }
4666
4667 /*
4668 * Void all references to file by ripping underlying filesystem
4669 * away from vnode.
4670 */
4671 /* ARGSUSED */
4672 int
4673 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
4674 {
4675 /* {
4676 syscallarg(const char *) path;
4677 } */
4678 struct vnode *vp;
4679 int error;
4680
4681 error = namei_simple_user(SCARG(uap, path),
4682 NSM_FOLLOW_TRYEMULROOT, &vp);
4683 if (error != 0)
4684 return (error);
4685 error = dorevoke(vp, l->l_cred);
4686 vrele(vp);
4687 return (error);
4688 }
4689