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