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