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