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