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