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