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