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