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