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