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