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