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