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