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