vfs_syscalls.c revision 1.333.2.1 1 /* $NetBSD: vfs_syscalls.c,v 1.333.2.1 2007/12/04 13:03:22 ad Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.333.2.1 2007/12/04 13:03:22 ad Exp $");
41
42 #include "opt_compat_netbsd.h"
43 #include "opt_compat_43.h"
44 #include "opt_fileassoc.h"
45 #include "fss.h"
46 #include "veriexec.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/namei.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/file.h>
54 #include <sys/stat.h>
55 #include <sys/vnode.h>
56 #include <sys/mount.h>
57 #include <sys/proc.h>
58 #include <sys/uio.h>
59 #include <sys/malloc.h>
60 #include <sys/kmem.h>
61 #include <sys/dirent.h>
62 #include <sys/sysctl.h>
63 #include <sys/syscallargs.h>
64 #include <sys/vfs_syscalls.h>
65 #include <sys/ktrace.h>
66 #ifdef FILEASSOC
67 #include <sys/fileassoc.h>
68 #endif /* FILEASSOC */
69 #include <sys/verified_exec.h>
70 #include <sys/kauth.h>
71
72 #include <miscfs/genfs/genfs.h>
73 #include <miscfs/syncfs/syncfs.h>
74
75 #ifdef COMPAT_30
76 #include "opt_nfsserver.h"
77 #include <nfs/rpcv2.h>
78 #endif
79 #include <nfs/nfsproto.h>
80 #ifdef COMPAT_30
81 #include <nfs/nfs.h>
82 #include <nfs/nfs_var.h>
83 #endif
84
85 #if NFSS > 0
86 #include <dev/fssvar.h>
87 #endif
88
89 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
90
91 static int change_dir(struct nameidata *, struct lwp *);
92 static int change_flags(struct vnode *, u_long, struct lwp *);
93 static int change_mode(struct vnode *, int, struct lwp *l);
94 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
95 static int rename_files(const char *, const char *, struct lwp *, int);
96
97 void checkdirs(struct vnode *);
98
99 int dovfsusermount = 0;
100
101 /*
102 * Virtual File System System Calls
103 */
104
105 /*
106 * Mount a file system.
107 */
108
109 #if defined(COMPAT_09) || defined(COMPAT_43)
110 /*
111 * This table is used to maintain compatibility with 4.3BSD
112 * and NetBSD 0.9 mount syscalls. Note, the order is important!
113 *
114 * Do not modify this table. It should only contain filesystems
115 * supported by NetBSD 0.9 and 4.3BSD.
116 */
117 const char * const mountcompatnames[] = {
118 NULL, /* 0 = MOUNT_NONE */
119 MOUNT_FFS, /* 1 = MOUNT_UFS */
120 MOUNT_NFS, /* 2 */
121 MOUNT_MFS, /* 3 */
122 MOUNT_MSDOS, /* 4 */
123 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
124 MOUNT_FDESC, /* 6 */
125 MOUNT_KERNFS, /* 7 */
126 NULL, /* 8 = MOUNT_DEVFS */
127 MOUNT_AFS, /* 9 */
128 };
129 const int nmountcompatnames = sizeof(mountcompatnames) /
130 sizeof(mountcompatnames[0]);
131 #endif /* COMPAT_09 || COMPAT_43 */
132
133 static int
134 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
135 void *data, size_t *data_len)
136 {
137 struct mount *mp;
138 int error = 0, saved_flags;
139
140 mp = vp->v_mount;
141 saved_flags = mp->mnt_flag;
142
143 /* We can operate only on VV_ROOT nodes. */
144 if ((vp->v_vflag & VV_ROOT) == 0) {
145 error = EINVAL;
146 goto out;
147 }
148
149 /*
150 * We only allow the filesystem to be reloaded if it
151 * is currently mounted read-only.
152 */
153 if (flags & MNT_RELOAD && !(mp->mnt_flag & MNT_RDONLY)) {
154 error = EOPNOTSUPP; /* Needs translation */
155 goto out;
156 }
157
158 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
159 KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
160 if (error)
161 goto out;
162
163 if (vfs_busy(mp, LK_NOWAIT, 0)) {
164 error = EPERM;
165 goto out;
166 }
167
168 mp->mnt_flag &= ~MNT_OP_FLAGS;
169 mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
170
171 /*
172 * Set the mount level flags.
173 */
174 if (flags & MNT_RDONLY)
175 mp->mnt_flag |= MNT_RDONLY;
176 else if (mp->mnt_flag & MNT_RDONLY)
177 mp->mnt_iflag |= IMNT_WANTRDWR;
178 mp->mnt_flag &=
179 ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
180 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
181 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP);
182 mp->mnt_flag |= flags &
183 (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
184 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
185 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
186 MNT_IGNORE);
187
188 error = VFS_MOUNT(mp, path, data, data_len);
189
190 #if defined(COMPAT_30) && defined(NFSSERVER)
191 if (error && data != NULL) {
192 int error2;
193
194 /* Update failed; let's try and see if it was an
195 * export request. */
196 error2 = nfs_update_exports_30(mp, path, data, l);
197
198 /* Only update error code if the export request was
199 * understood but some problem occurred while
200 * processing it. */
201 if (error2 != EJUSTRETURN)
202 error = error2;
203 }
204 #endif
205 if (mp->mnt_iflag & IMNT_WANTRDWR)
206 mp->mnt_flag &= ~MNT_RDONLY;
207 if (error)
208 mp->mnt_flag = saved_flags;
209 mp->mnt_flag &= ~MNT_OP_FLAGS;
210 mp->mnt_iflag &= ~IMNT_WANTRDWR;
211 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
212 if (mp->mnt_syncer == NULL)
213 error = vfs_allocate_syncvnode(mp);
214 } else {
215 if (mp->mnt_syncer != NULL)
216 vfs_deallocate_syncvnode(mp);
217 }
218 vfs_unbusy(mp);
219
220 out:
221 return (error);
222 }
223
224 static int
225 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
226 {
227 char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
228 int error;
229
230 /* Copy file-system type from userspace. */
231 error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
232 if (error) {
233 #if defined(COMPAT_09) || defined(COMPAT_43)
234 /*
235 * Historically, filesystem types were identified by numbers.
236 * If we get an integer for the filesystem type instead of a
237 * string, we check to see if it matches one of the historic
238 * filesystem types.
239 */
240 u_long fsindex = (u_long)fstype;
241 if (fsindex >= nmountcompatnames ||
242 mountcompatnames[fsindex] == NULL)
243 return ENODEV;
244 strlcpy(fstypename, mountcompatnames[fsindex],
245 sizeof(fstypename));
246 #else
247 return error;
248 #endif
249 }
250
251 #ifdef COMPAT_10
252 /* Accept `ufs' as an alias for `ffs'. */
253 if (strcmp(fstypename, "ufs") == 0)
254 fstypename[0] = 'f';
255 #endif
256
257 if ((*vfsops = vfs_getopsbyname(fstypename)) == NULL)
258 return ENODEV;
259 return 0;
260 }
261
262 static int
263 mount_domount(struct lwp *l, struct vnode **vpp, struct vfsops *vfsops,
264 const char *path, int flags, void *data, size_t *data_len)
265 {
266 struct mount *mp = NULL;
267 struct vnode *vp = *vpp;
268 struct vattr va;
269 int error;
270
271 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
272 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
273 if (error)
274 return error;
275
276 /* Can't make a non-dir a mount-point (from here anyway). */
277 if (vp->v_type != VDIR)
278 return ENOTDIR;
279
280 /*
281 * If the user is not root, ensure that they own the directory
282 * onto which we are attempting to mount.
283 */
284 if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0 ||
285 (va.va_uid != kauth_cred_geteuid(l->l_cred) &&
286 (error = kauth_authorize_generic(l->l_cred,
287 KAUTH_GENERIC_ISSUSER, NULL)) != 0)) {
288 return error;
289 }
290
291 if (flags & MNT_EXPORTED)
292 return EINVAL;
293
294 if ((error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
295 return error;
296
297 /*
298 * Check if a file-system is not already mounted on this vnode.
299 */
300 if (vp->v_mountedhere != NULL)
301 return EBUSY;
302
303 mp = malloc(sizeof(*mp), M_MOUNT, M_WAITOK|M_ZERO);
304
305 mp->mnt_op = vfsops;
306
307 TAILQ_INIT(&mp->mnt_vnodelist);
308 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
309 mutex_init(&mp->mnt_mutex, MUTEX_DEFAULT, IPL_NONE);
310 (void)vfs_busy(mp, LK_NOWAIT, 0);
311
312 mp->mnt_vnodecovered = vp;
313 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
314 mp->mnt_unmounter = NULL;
315 mount_initspecific(mp);
316
317 /*
318 * The underlying file system may refuse the mount for
319 * various reasons. Allow the user to force it to happen.
320 *
321 * Set the mount level flags.
322 */
323 mp->mnt_flag = flags &
324 (MNT_FORCE | MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
325 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
326 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
327 MNT_IGNORE | MNT_RDONLY);
328
329 error = VFS_MOUNT(mp, path, data, data_len);
330 mp->mnt_flag &= ~MNT_OP_FLAGS;
331
332 /*
333 * Put the new filesystem on the mount list after root.
334 */
335 cache_purge(vp);
336 if (error != 0) {
337 vp->v_mountedhere = NULL;
338 mp->mnt_op->vfs_refcount--;
339 vfs_unbusy(mp);
340 vfs_destroy(mp);
341 return error;
342 }
343
344 mp->mnt_iflag &= ~IMNT_WANTRDWR;
345 vp->v_mountedhere = mp;
346 mutex_enter(&mountlist_lock);
347 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
348 mutex_exit(&mountlist_lock);
349 VOP_UNLOCK(vp, 0);
350 checkdirs(vp);
351 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
352 error = vfs_allocate_syncvnode(mp);
353 vfs_unbusy(mp);
354 (void) VFS_STATVFS(mp, &mp->mnt_stat);
355 error = VFS_START(mp, 0);
356 if (error)
357 vrele(vp);
358 *vpp = NULL;
359 return error;
360 }
361
362 static int
363 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
364 void *data, size_t *data_len)
365 {
366 struct mount *mp;
367 int error;
368
369 /* If MNT_GETARGS is specified, it should be the only flag. */
370 if (flags & ~MNT_GETARGS)
371 return EINVAL;
372
373 mp = vp->v_mount;
374
375 /* XXX: probably some notion of "can see" here if we want isolation. */
376 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
377 KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
378 if (error)
379 return error;
380
381 if ((vp->v_vflag & VV_ROOT) == 0)
382 return EINVAL;
383
384 if (vfs_busy(mp, LK_NOWAIT, 0))
385 return EPERM;
386
387 mp->mnt_flag &= ~MNT_OP_FLAGS;
388 mp->mnt_flag |= MNT_GETARGS;
389 error = VFS_MOUNT(mp, path, data, data_len);
390 mp->mnt_flag &= ~MNT_OP_FLAGS;
391
392 vfs_unbusy(mp);
393 return (error);
394 }
395
396 #ifdef COMPAT_40
397 /* ARGSUSED */
398 int
399 compat_40_sys_mount(struct lwp *l, void *v, register_t *retval)
400 {
401 struct compat_40_sys_mount_args /* {
402 syscallarg(const char *) type;
403 syscallarg(const char *) path;
404 syscallarg(int) flags;
405 syscallarg(void *) data;
406 } */ *uap = v;
407 register_t dummy;
408
409 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
410 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE, 0, &dummy);
411 }
412 #endif
413
414 int
415 sys___mount50(struct lwp *l, void *v, register_t *retval)
416 {
417 struct sys___mount50_args /* {
418 syscallarg(const char *) type;
419 syscallarg(const char *) path;
420 syscallarg(int) flags;
421 syscallarg(void *) data;
422 syscallarg(size_t) data_len;
423 } */ *uap = v;
424
425 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
426 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
427 SCARG(uap, data_len), retval);
428 }
429
430 int
431 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
432 const char *path, int flags, void *data, enum uio_seg data_seg,
433 size_t data_len, register_t *retval)
434 {
435 struct vnode *vp;
436 struct nameidata nd;
437 void *data_buf = data;
438 int error;
439
440 /*
441 * Get vnode to be covered
442 */
443 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path, l);
444 if ((error = namei(&nd)) != 0)
445 return (error);
446 vp = nd.ni_vp;
447
448 /*
449 * A lookup in VFS_MOUNT might result in an attempt to
450 * lock this vnode again, so make the lock recursive.
451 */
452 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
453
454 if (vfsops == NULL) {
455 if (flags & (MNT_GETARGS | MNT_UPDATE))
456 vfsops = vp->v_mount->mnt_op;
457 else {
458 /* 'type' is userspace */
459 error = mount_get_vfsops(type, &vfsops);
460 if (error != 0)
461 goto done;
462 }
463 }
464
465 if (data != NULL && data_seg == UIO_USERSPACE) {
466 if (data_len == 0) {
467 /* No length supplied, use default for filesystem */
468 data_len = vfsops->vfs_min_mount_data;
469 if (data_len > VFS_MAX_MOUNT_DATA) {
470 /* maybe a force loaded old LKM */
471 error = EINVAL;
472 goto done;
473 }
474 #ifdef COMPAT_30
475 /* Hopefully a longer buffer won't make copyin() fail */
476 if (flags & MNT_UPDATE
477 && data_len < sizeof (struct mnt_export_args30))
478 data_len = sizeof (struct mnt_export_args30);
479 #endif
480 }
481 data_buf = malloc(data_len, M_TEMP, M_WAITOK);
482
483 /* NFS needs the buffer even for mnt_getargs .... */
484 error = copyin(data, data_buf, data_len);
485 if (error != 0)
486 goto done;
487 }
488
489 if (flags & MNT_GETARGS) {
490 if (data_len == 0) {
491 error = EINVAL;
492 goto done;
493 }
494 error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
495 if (error != 0)
496 goto done;
497 if (data_seg == UIO_USERSPACE)
498 error = copyout(data_buf, data, data_len);
499 *retval = data_len;
500 } else if (flags & MNT_UPDATE) {
501 error = mount_update(l, vp, path, flags, data_buf, &data_len);
502 } else {
503 /* Locking is handled internally in mount_domount(). */
504 error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
505 &data_len);
506 }
507
508 done:
509 if (vp)
510 vput(vp);
511 if (data_buf != data)
512 free(data_buf, M_TEMP);
513 return (error);
514 }
515
516 /*
517 * Scan all active processes to see if any of them have a current
518 * or root directory onto which the new filesystem has just been
519 * mounted. If so, replace them with the new mount point.
520 */
521 void
522 checkdirs(struct vnode *olddp)
523 {
524 struct cwdinfo *cwdi;
525 struct vnode *newdp;
526 struct proc *p;
527
528 if (olddp->v_usecount == 1)
529 return;
530 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
531 panic("mount: lost mount");
532 mutex_enter(&proclist_lock);
533 PROCLIST_FOREACH(p, &allproc) {
534 cwdi = p->p_cwdi;
535 if (!cwdi)
536 continue;
537 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
538 if (cwdi->cwdi_cdir == olddp) {
539 vrele(cwdi->cwdi_cdir);
540 VREF(newdp);
541 cwdi->cwdi_cdir = newdp;
542 }
543 if (cwdi->cwdi_rdir == olddp) {
544 vrele(cwdi->cwdi_rdir);
545 VREF(newdp);
546 cwdi->cwdi_rdir = newdp;
547 }
548 rw_exit(&cwdi->cwdi_lock);
549 }
550 mutex_exit(&proclist_lock);
551 if (rootvnode == olddp) {
552 vrele(rootvnode);
553 VREF(newdp);
554 rootvnode = newdp;
555 }
556 vput(newdp);
557 }
558
559 /*
560 * Unmount a file system.
561 *
562 * Note: unmount takes a path to the vnode mounted on as argument,
563 * not special file (as before).
564 */
565 /* ARGSUSED */
566 int
567 sys_unmount(struct lwp *l, void *v, register_t *retval)
568 {
569 struct sys_unmount_args /* {
570 syscallarg(const char *) path;
571 syscallarg(int) flags;
572 } */ *uap = v;
573 struct vnode *vp;
574 struct mount *mp;
575 int error;
576 struct nameidata nd;
577
578 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
579 SCARG(uap, path), l);
580 if ((error = namei(&nd)) != 0)
581 return (error);
582 vp = nd.ni_vp;
583 mp = vp->v_mount;
584
585 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
586 KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
587 if (error) {
588 vput(vp);
589 return (error);
590 }
591
592 /*
593 * Don't allow unmounting the root file system.
594 */
595 if (mp->mnt_flag & MNT_ROOTFS) {
596 vput(vp);
597 return (EINVAL);
598 }
599
600 /*
601 * Must be the root of the filesystem
602 */
603 if ((vp->v_vflag & VV_ROOT) == 0) {
604 vput(vp);
605 return (EINVAL);
606 }
607 vput(vp);
608
609 /*
610 * XXX Freeze syncer. Must do this before locking the
611 * mount point. See dounmount() for details.
612 */
613 mutex_enter(&syncer_mutex);
614
615 if (vfs_busy(mp, 0, 0)) {
616 mutex_exit(&syncer_mutex);
617 return (EBUSY);
618 }
619
620 return (dounmount(mp, SCARG(uap, flags), l));
621 }
622
623 /*
624 * Do the actual file system unmount. File system is assumed to have been
625 * marked busy by the caller.
626 */
627 int
628 dounmount(struct mount *mp, int flags, struct lwp *l)
629 {
630 struct vnode *coveredvp;
631 int error;
632 int async;
633 int used_syncer;
634
635 #if NVERIEXEC > 0
636 error = veriexec_unmountchk(mp);
637 if (error)
638 return (error);
639 #endif /* NVERIEXEC > 0 */
640
641 mutex_enter(&mountlist_lock);
642 vfs_unbusy(mp);
643 used_syncer = (mp->mnt_syncer != NULL);
644
645 /*
646 * XXX Syncer must be frozen when we get here. This should really
647 * be done on a per-mountpoint basis, but especially the softdep
648 * code possibly called from the syncer doesn't exactly work on a
649 * per-mountpoint basis, so the softdep code would become a maze
650 * of vfs_busy() calls.
651 *
652 * The caller of dounmount() must acquire syncer_mutex because
653 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
654 * order, and we must preserve that order to avoid deadlock.
655 *
656 * So, if the file system did not use the syncer, now is
657 * the time to release the syncer_mutex.
658 */
659 if (used_syncer == 0)
660 mutex_exit(&syncer_mutex);
661
662 mp->mnt_iflag |= IMNT_UNMOUNT;
663 mp->mnt_unmounter = l;
664 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_lock);
665
666 async = mp->mnt_flag & MNT_ASYNC;
667 mp->mnt_flag &= ~MNT_ASYNC;
668 cache_purgevfs(mp); /* remove cache entries for this file sys */
669 if (mp->mnt_syncer != NULL)
670 vfs_deallocate_syncvnode(mp);
671 error = 0;
672 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
673 #if NFSS > 0
674 error = fss_umount_hook(mp, (flags & MNT_FORCE));
675 #endif
676 if (error == 0)
677 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
678 }
679 if (error == 0 || (flags & MNT_FORCE))
680 error = VFS_UNMOUNT(mp, flags);
681 if (error) {
682 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
683 (void) vfs_allocate_syncvnode(mp);
684 mutex_enter(&mountlist_lock);
685 mp->mnt_iflag &= ~IMNT_UNMOUNT;
686 mp->mnt_unmounter = NULL;
687 mp->mnt_flag |= async;
688 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
689 &mountlist_lock);
690 if (used_syncer)
691 mutex_exit(&syncer_mutex);
692 mutex_enter(&mp->mnt_mutex);
693 while (mp->mnt_wcnt > 0) {
694 wakeup(mp);
695 mtsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1",
696 0, &mp->mnt_mutex);
697 }
698 mutex_exit(&mp->mnt_mutex);
699 return (error);
700 }
701 vfs_scrubvnlist(mp);
702 mutex_enter(&mountlist_lock);
703 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
704 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP)
705 coveredvp->v_mountedhere = NULL;
706 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
707 panic("unmount: dangling vnode");
708 mp->mnt_iflag |= IMNT_GONE;
709 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_lock);
710 if (coveredvp != NULLVP)
711 vrele(coveredvp);
712 if (used_syncer)
713 mutex_exit(&syncer_mutex);
714 mutex_enter(&mp->mnt_mutex);
715 while (mp->mnt_wcnt > 0) {
716 wakeup(mp);
717 mtsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0, &mp->mnt_mutex);
718 }
719 mutex_exit(&mp->mnt_mutex);
720 vfs_hooks_unmount(mp);
721 vfs_delref(mp->mnt_op);
722 vfs_destroy(mp);
723 return (0);
724 }
725
726 /*
727 * Sync each mounted filesystem.
728 */
729 #ifdef DEBUG
730 int syncprt = 0;
731 struct ctldebug debug0 = { "syncprt", &syncprt };
732 #endif
733
734 /* ARGSUSED */
735 int
736 sys_sync(struct lwp *l, void *v, register_t *retval)
737 {
738 struct mount *mp, *nmp;
739 int asyncflag;
740
741 if (l == NULL)
742 l = &lwp0;
743
744 mutex_enter(&mountlist_lock);
745 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
746 if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
747 nmp = mp->mnt_list.cqe_prev;
748 continue;
749 }
750 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
751 asyncflag = mp->mnt_flag & MNT_ASYNC;
752 mp->mnt_flag &= ~MNT_ASYNC;
753 VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
754 if (asyncflag)
755 mp->mnt_flag |= MNT_ASYNC;
756 }
757 mutex_enter(&mountlist_lock);
758 nmp = mp->mnt_list.cqe_prev;
759 vfs_unbusy(mp);
760
761 }
762 mutex_exit(&mountlist_lock);
763 #ifdef DEBUG
764 if (syncprt)
765 vfs_bufstats();
766 #endif /* DEBUG */
767 return (0);
768 }
769
770 /*
771 * Change filesystem quotas.
772 */
773 /* ARGSUSED */
774 int
775 sys_quotactl(struct lwp *l, void *v, register_t *retval)
776 {
777 struct sys_quotactl_args /* {
778 syscallarg(const char *) path;
779 syscallarg(int) cmd;
780 syscallarg(int) uid;
781 syscallarg(void *) arg;
782 } */ *uap = v;
783 struct mount *mp;
784 int error;
785 struct nameidata nd;
786
787 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
788 SCARG(uap, path), l);
789 if ((error = namei(&nd)) != 0)
790 return (error);
791 mp = nd.ni_vp->v_mount;
792 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
793 SCARG(uap, arg));
794 vrele(nd.ni_vp);
795 return (error);
796 }
797
798 int
799 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
800 int root)
801 {
802 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
803 int error = 0;
804
805 /*
806 * If MNT_NOWAIT or MNT_LAZY is specified, do not
807 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
808 * overrides MNT_NOWAIT.
809 */
810 if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
811 (flags != MNT_WAIT && flags != 0)) {
812 memcpy(sp, &mp->mnt_stat, sizeof(*sp));
813 goto done;
814 }
815
816 /* Get the filesystem stats now */
817 memset(sp, 0, sizeof(*sp));
818 if ((error = VFS_STATVFS(mp, sp)) != 0) {
819 return error;
820 }
821
822 if (cwdi->cwdi_rdir == NULL)
823 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
824 done:
825 if (cwdi->cwdi_rdir != NULL) {
826 size_t len;
827 char *bp;
828 char *path = PNBUF_GET();
829
830 bp = path + MAXPATHLEN;
831 *--bp = '\0';
832 rw_enter(&cwdi->cwdi_lock, RW_READER);
833 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
834 MAXPATHLEN / 2, 0, l);
835 rw_exit(&cwdi->cwdi_lock);
836 if (error) {
837 PNBUF_PUT(path);
838 return error;
839 }
840 len = strlen(bp);
841 /*
842 * for mount points that are below our root, we can see
843 * them, so we fix up the pathname and return them. The
844 * rest we cannot see, so we don't allow viewing the
845 * data.
846 */
847 if (strncmp(bp, sp->f_mntonname, len) == 0) {
848 strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
849 sizeof(sp->f_mntonname));
850 if (sp->f_mntonname[0] == '\0')
851 (void)strlcpy(sp->f_mntonname, "/",
852 sizeof(sp->f_mntonname));
853 } else {
854 if (root)
855 (void)strlcpy(sp->f_mntonname, "/",
856 sizeof(sp->f_mntonname));
857 else
858 error = EPERM;
859 }
860 PNBUF_PUT(path);
861 }
862 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
863 return error;
864 }
865
866 /*
867 * Get filesystem statistics by path.
868 */
869 int
870 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
871 {
872 struct mount *mp;
873 int error;
874 struct nameidata nd;
875
876 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path, l);
877 if ((error = namei(&nd)) != 0)
878 return error;
879 mp = nd.ni_vp->v_mount;
880 error = dostatvfs(mp, sb, l, flags, 1);
881 vrele(nd.ni_vp);
882 return error;
883 }
884
885 /* ARGSUSED */
886 int
887 sys_statvfs1(struct lwp *l, void *v, register_t *retval)
888 {
889 struct sys_statvfs1_args /* {
890 syscallarg(const char *) path;
891 syscallarg(struct statvfs *) buf;
892 syscallarg(int) flags;
893 } */ *uap = v;
894 struct statvfs *sb;
895 int error;
896
897 sb = STATVFSBUF_GET();
898 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
899 if (error == 0)
900 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
901 STATVFSBUF_PUT(sb);
902 return error;
903 }
904
905 /*
906 * Get filesystem statistics by fd.
907 */
908 int
909 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
910 {
911 struct proc *p = l->l_proc;
912 struct file *fp;
913 struct mount *mp;
914 int error;
915
916 /* getvnode() will use the descriptor for us */
917 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
918 return (error);
919 mp = ((struct vnode *)fp->f_data)->v_mount;
920 error = dostatvfs(mp, sb, l, flags, 1);
921 FILE_UNUSE(fp, l);
922 return error;
923 }
924
925 /* ARGSUSED */
926 int
927 sys_fstatvfs1(struct lwp *l, void *v, register_t *retval)
928 {
929 struct sys_fstatvfs1_args /* {
930 syscallarg(int) fd;
931 syscallarg(struct statvfs *) buf;
932 syscallarg(int) flags;
933 } */ *uap = v;
934 struct statvfs *sb;
935 int error;
936
937 sb = STATVFSBUF_GET();
938 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
939 if (error == 0)
940 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
941 STATVFSBUF_PUT(sb);
942 return error;
943 }
944
945
946 /*
947 * Get statistics on all filesystems.
948 */
949 int
950 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
951 int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
952 register_t *retval)
953 {
954 int root = 0;
955 struct proc *p = l->l_proc;
956 struct mount *mp, *nmp;
957 struct statvfs *sb;
958 size_t count, maxcount;
959 int error = 0;
960
961 sb = STATVFSBUF_GET();
962 maxcount = bufsize / entry_sz;
963 mutex_enter(&mountlist_lock);
964 count = 0;
965 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
966 mp = nmp) {
967 if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
968 nmp = CIRCLEQ_NEXT(mp, mnt_list);
969 continue;
970 }
971 if (sfsp && count < maxcount) {
972 error = dostatvfs(mp, sb, l, flags, 0);
973 if (error) {
974 mutex_enter(&mountlist_lock);
975 nmp = CIRCLEQ_NEXT(mp, mnt_list);
976 vfs_unbusy(mp);
977 continue;
978 }
979 error = copyfn(sb, sfsp, entry_sz);
980 if (error) {
981 vfs_unbusy(mp);
982 goto out;
983 }
984 sfsp = (char *)sfsp + entry_sz;
985 root |= strcmp(sb->f_mntonname, "/") == 0;
986 }
987 count++;
988 mutex_enter(&mountlist_lock);
989 nmp = CIRCLEQ_NEXT(mp, mnt_list);
990 vfs_unbusy(mp);
991 }
992
993 mutex_exit(&mountlist_lock);
994 if (root == 0 && p->p_cwdi->cwdi_rdir) {
995 /*
996 * fake a root entry
997 */
998 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
999 sb, l, flags, 1);
1000 if (error != 0)
1001 goto out;
1002 if (sfsp)
1003 error = copyfn(sb, sfsp, entry_sz);
1004 count++;
1005 }
1006 if (sfsp && count > maxcount)
1007 *retval = maxcount;
1008 else
1009 *retval = count;
1010 out:
1011 STATVFSBUF_PUT(sb);
1012 return error;
1013 }
1014
1015 int
1016 sys_getvfsstat(struct lwp *l, void *v, register_t *retval)
1017 {
1018 struct sys_getvfsstat_args /* {
1019 syscallarg(struct statvfs *) buf;
1020 syscallarg(size_t) bufsize;
1021 syscallarg(int) flags;
1022 } */ *uap = v;
1023
1024 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
1025 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
1026 }
1027
1028 /*
1029 * Change current working directory to a given file descriptor.
1030 */
1031 /* ARGSUSED */
1032 int
1033 sys_fchdir(struct lwp *l, void *v, register_t *retval)
1034 {
1035 struct sys_fchdir_args /* {
1036 syscallarg(int) fd;
1037 } */ *uap = v;
1038 struct proc *p = l->l_proc;
1039 struct filedesc *fdp = p->p_fd;
1040 struct cwdinfo *cwdi;
1041 struct vnode *vp, *tdp;
1042 struct mount *mp;
1043 struct file *fp;
1044 int error;
1045
1046 /* getvnode() will use the descriptor for us */
1047 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
1048 return (error);
1049 vp = (struct vnode *)fp->f_data;
1050
1051 VREF(vp);
1052 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1053 if (vp->v_type != VDIR)
1054 error = ENOTDIR;
1055 else
1056 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1057 if (error) {
1058 vput(vp);
1059 goto out;
1060 }
1061 while ((mp = vp->v_mountedhere) != NULL) {
1062 if (vfs_busy(mp, 0, 0))
1063 continue;
1064
1065 vput(vp);
1066 error = VFS_ROOT(mp, &tdp);
1067 vfs_unbusy(mp);
1068 if (error)
1069 goto out;
1070 vp = tdp;
1071 }
1072 VOP_UNLOCK(vp, 0);
1073
1074 /*
1075 * Disallow changing to a directory not under the process's
1076 * current root directory (if there is one).
1077 */
1078 cwdi = p->p_cwdi;
1079 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1080 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
1081 vrele(vp);
1082 error = EPERM; /* operation not permitted */
1083 } else {
1084 vrele(cwdi->cwdi_cdir);
1085 cwdi->cwdi_cdir = vp;
1086 }
1087 rw_exit(&cwdi->cwdi_lock);
1088
1089 out:
1090 FILE_UNUSE(fp, l);
1091 return (error);
1092 }
1093
1094 /*
1095 * Change this process's notion of the root directory to a given file
1096 * descriptor.
1097 */
1098 int
1099 sys_fchroot(struct lwp *l, void *v, register_t *retval)
1100 {
1101 struct sys_fchroot_args *uap = v;
1102 struct proc *p = l->l_proc;
1103 struct filedesc *fdp = p->p_fd;
1104 struct cwdinfo *cwdi;
1105 struct vnode *vp;
1106 struct file *fp;
1107 int error;
1108
1109 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1110 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
1111 return error;
1112 /* getvnode() will use the descriptor for us */
1113 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
1114 return error;
1115 vp = (struct vnode *) fp->f_data;
1116 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1117 if (vp->v_type != VDIR)
1118 error = ENOTDIR;
1119 else
1120 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1121 VOP_UNLOCK(vp, 0);
1122 if (error)
1123 goto out;
1124 VREF(vp);
1125
1126 /*
1127 * Prevent escaping from chroot by putting the root under
1128 * the working directory. Silently chdir to / if we aren't
1129 * already there.
1130 */
1131 cwdi = p->p_cwdi;
1132 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1133 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1134 /*
1135 * XXX would be more failsafe to change directory to a
1136 * deadfs node here instead
1137 */
1138 vrele(cwdi->cwdi_cdir);
1139 VREF(vp);
1140 cwdi->cwdi_cdir = vp;
1141 }
1142
1143 if (cwdi->cwdi_rdir != NULL)
1144 vrele(cwdi->cwdi_rdir);
1145 cwdi->cwdi_rdir = vp;
1146 rw_exit(&cwdi->cwdi_lock);
1147
1148 out:
1149 FILE_UNUSE(fp, l);
1150 return (error);
1151 }
1152
1153 /*
1154 * Change current working directory (``.'').
1155 */
1156 /* ARGSUSED */
1157 int
1158 sys_chdir(struct lwp *l, void *v, register_t *retval)
1159 {
1160 struct sys_chdir_args /* {
1161 syscallarg(const char *) path;
1162 } */ *uap = v;
1163 struct proc *p = l->l_proc;
1164 struct cwdinfo *cwdi;
1165 int error;
1166 struct nameidata nd;
1167
1168 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1169 SCARG(uap, path), l);
1170 if ((error = change_dir(&nd, l)) != 0)
1171 return (error);
1172 cwdi = p->p_cwdi;
1173 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1174 vrele(cwdi->cwdi_cdir);
1175 cwdi->cwdi_cdir = nd.ni_vp;
1176 rw_exit(&cwdi->cwdi_lock);
1177 return (0);
1178 }
1179
1180 /*
1181 * Change notion of root (``/'') directory.
1182 */
1183 /* ARGSUSED */
1184 int
1185 sys_chroot(struct lwp *l, void *v, register_t *retval)
1186 {
1187 struct sys_chroot_args /* {
1188 syscallarg(const char *) path;
1189 } */ *uap = v;
1190 struct proc *p = l->l_proc;
1191 struct cwdinfo *cwdi;
1192 struct vnode *vp;
1193 int error;
1194 struct nameidata nd;
1195
1196 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1197 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
1198 return (error);
1199 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1200 SCARG(uap, path), l);
1201 if ((error = change_dir(&nd, l)) != 0)
1202 return (error);
1203
1204 cwdi = p->p_cwdi;
1205 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1206 if (cwdi->cwdi_rdir != NULL)
1207 vrele(cwdi->cwdi_rdir);
1208 vp = nd.ni_vp;
1209 cwdi->cwdi_rdir = vp;
1210
1211 /*
1212 * Prevent escaping from chroot by putting the root under
1213 * the working directory. Silently chdir to / if we aren't
1214 * already there.
1215 */
1216 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1217 /*
1218 * XXX would be more failsafe to change directory to a
1219 * deadfs node here instead
1220 */
1221 vrele(cwdi->cwdi_cdir);
1222 VREF(vp);
1223 cwdi->cwdi_cdir = vp;
1224 }
1225 rw_exit(&cwdi->cwdi_lock);
1226
1227 return (0);
1228 }
1229
1230 /*
1231 * Common routine for chroot and chdir.
1232 */
1233 static int
1234 change_dir(struct nameidata *ndp, struct lwp *l)
1235 {
1236 struct vnode *vp;
1237 int error;
1238
1239 if ((error = namei(ndp)) != 0)
1240 return (error);
1241 vp = ndp->ni_vp;
1242 if (vp->v_type != VDIR)
1243 error = ENOTDIR;
1244 else
1245 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1246
1247 if (error)
1248 vput(vp);
1249 else
1250 VOP_UNLOCK(vp, 0);
1251 return (error);
1252 }
1253
1254 /*
1255 * Check permissions, allocate an open file structure,
1256 * and call the device open routine if any.
1257 */
1258 int
1259 sys_open(struct lwp *l, void *v, register_t *retval)
1260 {
1261 struct sys_open_args /* {
1262 syscallarg(const char *) path;
1263 syscallarg(int) flags;
1264 syscallarg(int) mode;
1265 } */ *uap = v;
1266 struct proc *p = l->l_proc;
1267 struct cwdinfo *cwdi = p->p_cwdi;
1268 struct filedesc *fdp = p->p_fd;
1269 struct file *fp;
1270 struct vnode *vp;
1271 int flags, cmode;
1272 int type, indx, error;
1273 struct flock lf;
1274 struct nameidata nd;
1275
1276 flags = FFLAGS(SCARG(uap, flags));
1277 if ((flags & (FREAD | FWRITE)) == 0)
1278 return (EINVAL);
1279 /* falloc() will use the file descriptor for us */
1280 if ((error = falloc(l, &fp, &indx)) != 0)
1281 return (error);
1282 /* We're going to read cwdi->cwdi_cmask unlocked here. */
1283 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1284 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
1285 SCARG(uap, path), l);
1286 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1287 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1288 rw_enter(&fdp->fd_lock, RW_WRITER);
1289 FILE_UNUSE(fp, l);
1290 fdp->fd_ofiles[indx] = NULL;
1291 rw_exit(&fdp->fd_lock);
1292 ffree(fp);
1293 if ((error == EDUPFD || error == EMOVEFD) &&
1294 l->l_dupfd >= 0 && /* XXX from fdopen */
1295 (error =
1296 dupfdopen(l, indx, l->l_dupfd, flags, error)) == 0) {
1297 *retval = indx;
1298 return (0);
1299 }
1300 if (error == ERESTART)
1301 error = EINTR;
1302 fdremove(fdp, indx);
1303 return (error);
1304 }
1305
1306 l->l_dupfd = 0;
1307 vp = nd.ni_vp;
1308 fp->f_flag = flags & FMASK;
1309 fp->f_type = DTYPE_VNODE;
1310 fp->f_ops = &vnops;
1311 fp->f_data = vp;
1312 if (flags & (O_EXLOCK | O_SHLOCK)) {
1313 lf.l_whence = SEEK_SET;
1314 lf.l_start = 0;
1315 lf.l_len = 0;
1316 if (flags & O_EXLOCK)
1317 lf.l_type = F_WRLCK;
1318 else
1319 lf.l_type = F_RDLCK;
1320 type = F_FLOCK;
1321 if ((flags & FNONBLOCK) == 0)
1322 type |= F_WAIT;
1323 VOP_UNLOCK(vp, 0);
1324 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1325 if (error) {
1326 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1327 FILE_UNUSE(fp, l);
1328 ffree(fp);
1329 fdremove(fdp, indx);
1330 return (error);
1331 }
1332 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1333 fp->f_flag |= FHASLOCK;
1334 }
1335 VOP_UNLOCK(vp, 0);
1336 *retval = indx;
1337 FILE_SET_MATURE(fp);
1338 FILE_UNUSE(fp, l);
1339 return (0);
1340 }
1341
1342 static void
1343 vfs__fhfree(fhandle_t *fhp)
1344 {
1345 size_t fhsize;
1346
1347 if (fhp == NULL) {
1348 return;
1349 }
1350 fhsize = FHANDLE_SIZE(fhp);
1351 kmem_free(fhp, fhsize);
1352 }
1353
1354 /*
1355 * vfs_composefh: compose a filehandle.
1356 */
1357
1358 int
1359 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1360 {
1361 struct mount *mp;
1362 struct fid *fidp;
1363 int error;
1364 size_t needfhsize;
1365 size_t fidsize;
1366
1367 mp = vp->v_mount;
1368 fidp = NULL;
1369 if (*fh_size < FHANDLE_SIZE_MIN) {
1370 fidsize = 0;
1371 } else {
1372 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1373 if (fhp != NULL) {
1374 memset(fhp, 0, *fh_size);
1375 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1376 fidp = &fhp->fh_fid;
1377 }
1378 }
1379 error = VFS_VPTOFH(vp, fidp, &fidsize);
1380 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1381 if (error == 0 && *fh_size < needfhsize) {
1382 error = E2BIG;
1383 }
1384 *fh_size = needfhsize;
1385 return error;
1386 }
1387
1388 int
1389 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1390 {
1391 struct mount *mp;
1392 fhandle_t *fhp;
1393 size_t fhsize;
1394 size_t fidsize;
1395 int error;
1396
1397 *fhpp = NULL;
1398 mp = vp->v_mount;
1399 fidsize = 0;
1400 error = VFS_VPTOFH(vp, NULL, &fidsize);
1401 KASSERT(error != 0);
1402 if (error != E2BIG) {
1403 goto out;
1404 }
1405 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1406 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1407 if (fhp == NULL) {
1408 error = ENOMEM;
1409 goto out;
1410 }
1411 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1412 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1413 if (error == 0) {
1414 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1415 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1416 *fhpp = fhp;
1417 } else {
1418 kmem_free(fhp, fhsize);
1419 }
1420 out:
1421 return error;
1422 }
1423
1424 void
1425 vfs_composefh_free(fhandle_t *fhp)
1426 {
1427
1428 vfs__fhfree(fhp);
1429 }
1430
1431 /*
1432 * vfs_fhtovp: lookup a vnode by a filehandle.
1433 */
1434
1435 int
1436 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1437 {
1438 struct mount *mp;
1439 int error;
1440
1441 *vpp = NULL;
1442 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1443 if (mp == NULL) {
1444 error = ESTALE;
1445 goto out;
1446 }
1447 if (mp->mnt_op->vfs_fhtovp == NULL) {
1448 error = EOPNOTSUPP;
1449 goto out;
1450 }
1451 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1452 out:
1453 return error;
1454 }
1455
1456 /*
1457 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1458 * the needed size.
1459 */
1460
1461 int
1462 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1463 {
1464 fhandle_t *fhp;
1465 int error;
1466
1467 *fhpp = NULL;
1468 if (fhsize > FHANDLE_SIZE_MAX) {
1469 return EINVAL;
1470 }
1471 if (fhsize < FHANDLE_SIZE_MIN) {
1472 return EINVAL;
1473 }
1474 again:
1475 fhp = kmem_alloc(fhsize, KM_SLEEP);
1476 if (fhp == NULL) {
1477 return ENOMEM;
1478 }
1479 error = copyin(ufhp, fhp, fhsize);
1480 if (error == 0) {
1481 /* XXX this check shouldn't be here */
1482 if (FHANDLE_SIZE(fhp) == fhsize) {
1483 *fhpp = fhp;
1484 return 0;
1485 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1486 /*
1487 * a kludge for nfsv2 padded handles.
1488 */
1489 size_t sz;
1490
1491 sz = FHANDLE_SIZE(fhp);
1492 kmem_free(fhp, fhsize);
1493 fhsize = sz;
1494 goto again;
1495 } else {
1496 /*
1497 * userland told us wrong size.
1498 */
1499 error = EINVAL;
1500 }
1501 }
1502 kmem_free(fhp, fhsize);
1503 return error;
1504 }
1505
1506 void
1507 vfs_copyinfh_free(fhandle_t *fhp)
1508 {
1509
1510 vfs__fhfree(fhp);
1511 }
1512
1513 /*
1514 * Get file handle system call
1515 */
1516 int
1517 sys___getfh30(struct lwp *l, void *v, register_t *retval)
1518 {
1519 struct sys___getfh30_args /* {
1520 syscallarg(char *) fname;
1521 syscallarg(fhandle_t *) fhp;
1522 syscallarg(size_t *) fh_size;
1523 } */ *uap = v;
1524 struct vnode *vp;
1525 fhandle_t *fh;
1526 int error;
1527 struct nameidata nd;
1528 size_t sz;
1529 size_t usz;
1530
1531 /*
1532 * Must be super user
1533 */
1534 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1535 0, NULL, NULL, NULL);
1536 if (error)
1537 return (error);
1538 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1539 SCARG(uap, fname), l);
1540 error = namei(&nd);
1541 if (error)
1542 return (error);
1543 vp = nd.ni_vp;
1544 error = vfs_composefh_alloc(vp, &fh);
1545 vput(vp);
1546 if (error != 0) {
1547 goto out;
1548 }
1549 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1550 if (error != 0) {
1551 goto out;
1552 }
1553 sz = FHANDLE_SIZE(fh);
1554 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1555 if (error != 0) {
1556 goto out;
1557 }
1558 if (usz >= sz) {
1559 error = copyout(fh, SCARG(uap, fhp), sz);
1560 } else {
1561 error = E2BIG;
1562 }
1563 out:
1564 vfs_composefh_free(fh);
1565 return (error);
1566 }
1567
1568 /*
1569 * Open a file given a file handle.
1570 *
1571 * Check permissions, allocate an open file structure,
1572 * and call the device open routine if any.
1573 */
1574
1575 int
1576 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1577 register_t *retval)
1578 {
1579 struct filedesc *fdp = l->l_proc->p_fd;
1580 struct file *fp;
1581 struct vnode *vp = NULL;
1582 kauth_cred_t cred = l->l_cred;
1583 struct file *nfp;
1584 int type, indx, error=0;
1585 struct flock lf;
1586 struct vattr va;
1587 fhandle_t *fh;
1588 int flags;
1589
1590 /*
1591 * Must be super user
1592 */
1593 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1594 0, NULL, NULL, NULL)))
1595 return (error);
1596
1597 flags = FFLAGS(oflags);
1598 if ((flags & (FREAD | FWRITE)) == 0)
1599 return (EINVAL);
1600 if ((flags & O_CREAT))
1601 return (EINVAL);
1602 /* falloc() will use the file descriptor for us */
1603 if ((error = falloc(l, &nfp, &indx)) != 0)
1604 return (error);
1605 fp = nfp;
1606 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1607 if (error != 0) {
1608 goto bad;
1609 }
1610 error = vfs_fhtovp(fh, &vp);
1611 if (error != 0) {
1612 goto bad;
1613 }
1614
1615 /* Now do an effective vn_open */
1616
1617 if (vp->v_type == VSOCK) {
1618 error = EOPNOTSUPP;
1619 goto bad;
1620 }
1621 error = vn_openchk(vp, cred, flags);
1622 if (error != 0)
1623 goto bad;
1624 if (flags & O_TRUNC) {
1625 VOP_UNLOCK(vp, 0); /* XXX */
1626 VOP_LEASE(vp, cred, LEASE_WRITE);
1627 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1628 VATTR_NULL(&va);
1629 va.va_size = 0;
1630 error = VOP_SETATTR(vp, &va, cred);
1631 if (error)
1632 goto bad;
1633 }
1634 if ((error = VOP_OPEN(vp, flags, cred)) != 0)
1635 goto bad;
1636 if (flags & FWRITE)
1637 vp->v_writecount++;
1638
1639 /* done with modified vn_open, now finish what sys_open does. */
1640
1641 fp->f_flag = flags & FMASK;
1642 fp->f_type = DTYPE_VNODE;
1643 fp->f_ops = &vnops;
1644 fp->f_data = vp;
1645 if (flags & (O_EXLOCK | O_SHLOCK)) {
1646 lf.l_whence = SEEK_SET;
1647 lf.l_start = 0;
1648 lf.l_len = 0;
1649 if (flags & O_EXLOCK)
1650 lf.l_type = F_WRLCK;
1651 else
1652 lf.l_type = F_RDLCK;
1653 type = F_FLOCK;
1654 if ((flags & FNONBLOCK) == 0)
1655 type |= F_WAIT;
1656 VOP_UNLOCK(vp, 0);
1657 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1658 if (error) {
1659 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1660 FILE_UNUSE(fp, l);
1661 ffree(fp);
1662 fdremove(fdp, indx);
1663 return (error);
1664 }
1665 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1666 fp->f_flag |= FHASLOCK;
1667 }
1668 VOP_UNLOCK(vp, 0);
1669 *retval = indx;
1670 FILE_SET_MATURE(fp);
1671 FILE_UNUSE(fp, l);
1672 vfs_copyinfh_free(fh);
1673 return (0);
1674
1675 bad:
1676 FILE_UNUSE(fp, l);
1677 ffree(fp);
1678 fdremove(fdp, indx);
1679 if (vp != NULL)
1680 vput(vp);
1681 vfs_copyinfh_free(fh);
1682 return (error);
1683 }
1684
1685 int
1686 sys___fhopen40(struct lwp *l, void *v, register_t *retval)
1687 {
1688 struct sys___fhopen40_args /* {
1689 syscallarg(const void *) fhp;
1690 syscallarg(size_t) fh_size;
1691 syscallarg(int) flags;
1692 } */ *uap = v;
1693
1694 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
1695 SCARG(uap, flags), retval);
1696 }
1697
1698 int
1699 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
1700 {
1701 int error;
1702 fhandle_t *fh;
1703 struct vnode *vp;
1704
1705 /*
1706 * Must be super user
1707 */
1708 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1709 0, NULL, NULL, NULL)))
1710 return (error);
1711
1712 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1713 if (error != 0)
1714 return error;
1715
1716 error = vfs_fhtovp(fh, &vp);
1717 vfs_copyinfh_free(fh);
1718 if (error != 0)
1719 return error;
1720
1721 error = vn_stat(vp, sb, l);
1722 vput(vp);
1723 return error;
1724 }
1725
1726
1727 /* ARGSUSED */
1728 int
1729 sys___fhstat40(struct lwp *l, void *v, register_t *retval)
1730 {
1731 struct sys___fhstat40_args /* {
1732 syscallarg(const void *) fhp;
1733 syscallarg(size_t) fh_size;
1734 syscallarg(struct stat *) sb;
1735 } */ *uap = v;
1736 struct stat sb;
1737 int error;
1738
1739 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
1740 if (error)
1741 return error;
1742 return copyout(&sb, SCARG(uap, sb), sizeof(sb));
1743 }
1744
1745 int
1746 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
1747 int flags)
1748 {
1749 fhandle_t *fh;
1750 struct mount *mp;
1751 struct vnode *vp;
1752 int error;
1753
1754 /*
1755 * Must be super user
1756 */
1757 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1758 0, NULL, NULL, NULL)))
1759 return error;
1760
1761 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1762 if (error != 0)
1763 return error;
1764
1765 error = vfs_fhtovp(fh, &vp);
1766 vfs_copyinfh_free(fh);
1767 if (error != 0)
1768 return error;
1769
1770 mp = vp->v_mount;
1771 error = dostatvfs(mp, sb, l, flags, 1);
1772 vput(vp);
1773 return error;
1774 }
1775
1776 /* ARGSUSED */
1777 int
1778 sys___fhstatvfs140(struct lwp *l, void *v, register_t *retval)
1779 {
1780 struct sys___fhstatvfs140_args /* {
1781 syscallarg(const void *) fhp;
1782 syscallarg(size_t) fh_size;
1783 syscallarg(struct statvfs *) buf;
1784 syscallarg(int) flags;
1785 } */ *uap = v;
1786 struct statvfs *sb = STATVFSBUF_GET();
1787 int error;
1788
1789 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
1790 SCARG(uap, flags));
1791 if (error == 0)
1792 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1793 STATVFSBUF_PUT(sb);
1794 return error;
1795 }
1796
1797 /*
1798 * Create a special file.
1799 */
1800 /* ARGSUSED */
1801 int
1802 sys_mknod(struct lwp *l, void *v, register_t *retval)
1803 {
1804 struct sys_mknod_args /* {
1805 syscallarg(const char *) path;
1806 syscallarg(int) mode;
1807 syscallarg(int) dev;
1808 } */ *uap = v;
1809 struct proc *p = l->l_proc;
1810 struct vnode *vp;
1811 struct vattr vattr;
1812 int error, optype;
1813 struct nameidata nd;
1814 char *path;
1815 const char *cpath;
1816 enum uio_seg seg = UIO_USERSPACE;
1817
1818 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
1819 0, NULL, NULL, NULL)) != 0)
1820 return (error);
1821
1822 optype = VOP_MKNOD_DESCOFFSET;
1823
1824 VERIEXEC_PATH_GET(SCARG(uap, path), seg, cpath, path);
1825 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, seg, cpath, l);
1826
1827 if ((error = namei(&nd)) != 0)
1828 goto out;
1829 vp = nd.ni_vp;
1830 if (vp != NULL)
1831 error = EEXIST;
1832 else {
1833 VATTR_NULL(&vattr);
1834 /* We will read cwdi->cwdi_cmask unlocked. */
1835 vattr.va_mode =
1836 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1837 vattr.va_rdev = SCARG(uap, dev);
1838
1839 switch (SCARG(uap, mode) & S_IFMT) {
1840 case S_IFMT: /* used by badsect to flag bad sectors */
1841 vattr.va_type = VBAD;
1842 break;
1843 case S_IFCHR:
1844 vattr.va_type = VCHR;
1845 break;
1846 case S_IFBLK:
1847 vattr.va_type = VBLK;
1848 break;
1849 case S_IFWHT:
1850 optype = VOP_WHITEOUT_DESCOFFSET;
1851 break;
1852 case S_IFREG:
1853 #if NVERIEXEC > 0
1854 error = veriexec_openchk(l, nd.ni_vp, nd.ni_dirp,
1855 O_CREAT);
1856 #endif /* NVERIEXEC > 0 */
1857 vattr.va_type = VREG;
1858 vattr.va_rdev = VNOVAL;
1859 optype = VOP_CREATE_DESCOFFSET;
1860 break;
1861 default:
1862 error = EINVAL;
1863 break;
1864 }
1865 }
1866 if (!error) {
1867 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
1868 switch (optype) {
1869 case VOP_WHITEOUT_DESCOFFSET:
1870 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1871 if (error)
1872 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1873 vput(nd.ni_dvp);
1874 break;
1875
1876 case VOP_MKNOD_DESCOFFSET:
1877 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1878 &nd.ni_cnd, &vattr);
1879 if (error == 0)
1880 vput(nd.ni_vp);
1881 break;
1882
1883 case VOP_CREATE_DESCOFFSET:
1884 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
1885 &nd.ni_cnd, &vattr);
1886 if (error == 0)
1887 vput(nd.ni_vp);
1888 break;
1889 }
1890 } else {
1891 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1892 if (nd.ni_dvp == vp)
1893 vrele(nd.ni_dvp);
1894 else
1895 vput(nd.ni_dvp);
1896 if (vp)
1897 vrele(vp);
1898 }
1899 out:
1900 VERIEXEC_PATH_PUT(path);
1901 return (error);
1902 }
1903
1904 /*
1905 * Create a named pipe.
1906 */
1907 /* ARGSUSED */
1908 int
1909 sys_mkfifo(struct lwp *l, void *v, register_t *retval)
1910 {
1911 struct sys_mkfifo_args /* {
1912 syscallarg(const char *) path;
1913 syscallarg(int) mode;
1914 } */ *uap = v;
1915 struct proc *p = l->l_proc;
1916 struct vattr vattr;
1917 int error;
1918 struct nameidata nd;
1919
1920 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1921 if ((error = namei(&nd)) != 0)
1922 return (error);
1923 if (nd.ni_vp != NULL) {
1924 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1925 if (nd.ni_dvp == nd.ni_vp)
1926 vrele(nd.ni_dvp);
1927 else
1928 vput(nd.ni_dvp);
1929 vrele(nd.ni_vp);
1930 return (EEXIST);
1931 }
1932 VATTR_NULL(&vattr);
1933 vattr.va_type = VFIFO;
1934 /* We will read cwdi->cwdi_cmask unlocked. */
1935 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1936 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
1937 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1938 if (error == 0)
1939 vput(nd.ni_vp);
1940 return (error);
1941 }
1942
1943 /*
1944 * Make a hard file link.
1945 */
1946 /* ARGSUSED */
1947 int
1948 sys_link(struct lwp *l, void *v, register_t *retval)
1949 {
1950 struct sys_link_args /* {
1951 syscallarg(const char *) path;
1952 syscallarg(const char *) link;
1953 } */ *uap = v;
1954 struct vnode *vp;
1955 struct nameidata nd;
1956 int error;
1957
1958 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
1959 SCARG(uap, path), l);
1960 if ((error = namei(&nd)) != 0)
1961 return (error);
1962 vp = nd.ni_vp;
1963 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
1964 SCARG(uap, link), l);
1965 if ((error = namei(&nd)) != 0)
1966 goto out;
1967 if (nd.ni_vp) {
1968 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1969 if (nd.ni_dvp == nd.ni_vp)
1970 vrele(nd.ni_dvp);
1971 else
1972 vput(nd.ni_dvp);
1973 vrele(nd.ni_vp);
1974 error = EEXIST;
1975 goto out;
1976 }
1977 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
1978 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
1979 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1980 out:
1981 vrele(vp);
1982 return (error);
1983 }
1984
1985 /*
1986 * Make a symbolic link.
1987 */
1988 /* ARGSUSED */
1989 int
1990 sys_symlink(struct lwp *l, void *v, register_t *retval)
1991 {
1992 struct sys_symlink_args /* {
1993 syscallarg(const char *) path;
1994 syscallarg(const char *) link;
1995 } */ *uap = v;
1996 struct proc *p = l->l_proc;
1997 struct vattr vattr;
1998 char *path;
1999 int error;
2000 struct nameidata nd;
2001
2002 path = PNBUF_GET();
2003 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
2004 if (error)
2005 goto out;
2006 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
2007 SCARG(uap, link), l);
2008 if ((error = namei(&nd)) != 0)
2009 goto out;
2010 if (nd.ni_vp) {
2011 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2012 if (nd.ni_dvp == nd.ni_vp)
2013 vrele(nd.ni_dvp);
2014 else
2015 vput(nd.ni_dvp);
2016 vrele(nd.ni_vp);
2017 error = EEXIST;
2018 goto out;
2019 }
2020 VATTR_NULL(&vattr);
2021 vattr.va_type = VLNK;
2022 /* We will read cwdi->cwdi_cmask unlocked. */
2023 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
2024 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
2025 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
2026 if (error == 0)
2027 vput(nd.ni_vp);
2028 out:
2029 PNBUF_PUT(path);
2030 return (error);
2031 }
2032
2033 /*
2034 * Delete a whiteout from the filesystem.
2035 */
2036 /* ARGSUSED */
2037 int
2038 sys_undelete(struct lwp *l, void *v, register_t *retval)
2039 {
2040 struct sys_undelete_args /* {
2041 syscallarg(const char *) path;
2042 } */ *uap = v;
2043 int error;
2044 struct nameidata nd;
2045
2046 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT,
2047 UIO_USERSPACE, SCARG(uap, path), l);
2048 error = namei(&nd);
2049 if (error)
2050 return (error);
2051
2052 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2053 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2054 if (nd.ni_dvp == nd.ni_vp)
2055 vrele(nd.ni_dvp);
2056 else
2057 vput(nd.ni_dvp);
2058 if (nd.ni_vp)
2059 vrele(nd.ni_vp);
2060 return (EEXIST);
2061 }
2062 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
2063 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
2064 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2065 vput(nd.ni_dvp);
2066 return (error);
2067 }
2068
2069 /*
2070 * Delete a name from the filesystem.
2071 */
2072 /* ARGSUSED */
2073 int
2074 sys_unlink(struct lwp *l, void *v, register_t *retval)
2075 {
2076 struct sys_unlink_args /* {
2077 syscallarg(const char *) path;
2078 } */ *uap = v;
2079 struct vnode *vp;
2080 int error;
2081 struct nameidata nd;
2082 char *path;
2083 const char *cpath;
2084 enum uio_seg seg = UIO_USERSPACE;
2085
2086 VERIEXEC_PATH_GET(SCARG(uap, path), seg, cpath, path);
2087 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, seg, cpath, l);
2088
2089 if ((error = namei(&nd)) != 0)
2090 goto out;
2091 vp = nd.ni_vp;
2092
2093 /*
2094 * The root of a mounted filesystem cannot be deleted.
2095 */
2096 if (vp->v_vflag & VV_ROOT) {
2097 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2098 if (nd.ni_dvp == vp)
2099 vrele(nd.ni_dvp);
2100 else
2101 vput(nd.ni_dvp);
2102 vput(vp);
2103 error = EBUSY;
2104 goto out;
2105 }
2106
2107 #if NVERIEXEC > 0
2108 /* Handle remove requests for veriexec entries. */
2109 if ((error = veriexec_removechk(l, nd.ni_vp, nd.ni_dirp)) != 0) {
2110 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2111 if (nd.ni_dvp == vp)
2112 vrele(nd.ni_dvp);
2113 else
2114 vput(nd.ni_dvp);
2115 vput(vp);
2116 goto out;
2117 }
2118 #endif /* NVERIEXEC > 0 */
2119
2120 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
2121 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
2122 #ifdef FILEASSOC
2123 (void)fileassoc_file_delete(vp);
2124 #endif /* FILEASSOC */
2125 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2126 out:
2127 VERIEXEC_PATH_PUT(path);
2128 return (error);
2129 }
2130
2131 /*
2132 * Reposition read/write file offset.
2133 */
2134 int
2135 sys_lseek(struct lwp *l, void *v, register_t *retval)
2136 {
2137 struct sys_lseek_args /* {
2138 syscallarg(int) fd;
2139 syscallarg(int) pad;
2140 syscallarg(off_t) offset;
2141 syscallarg(int) whence;
2142 } */ *uap = v;
2143 struct proc *p = l->l_proc;
2144 kauth_cred_t cred = l->l_cred;
2145 struct filedesc *fdp = p->p_fd;
2146 struct file *fp;
2147 struct vnode *vp;
2148 struct vattr vattr;
2149 off_t newoff;
2150 int error;
2151
2152 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
2153 return (EBADF);
2154
2155 vp = (struct vnode *)fp->f_data;
2156 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2157 error = ESPIPE;
2158 mutex_exit(&fp->f_lock);
2159 goto out;
2160 }
2161
2162 switch (SCARG(uap, whence)) {
2163 case SEEK_CUR:
2164 newoff = fp->f_offset + SCARG(uap, offset);
2165 FILE_USE(fp);
2166 break;
2167 case SEEK_END:
2168 FILE_USE(fp);
2169 error = VOP_GETATTR(vp, &vattr, cred);
2170 if (error) {
2171 FILE_UNUSE(fp, l);
2172 goto out;
2173 }
2174 newoff = SCARG(uap, offset) + vattr.va_size;
2175 break;
2176 case SEEK_SET:
2177 FILE_USE(fp);
2178 newoff = SCARG(uap, offset);
2179 break;
2180 default:
2181 mutex_exit(&fp->f_lock);
2182 error = EINVAL;
2183 goto out;
2184 }
2185 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2186 mutex_enter(&fp->f_lock);
2187 *(off_t *)retval = fp->f_offset = newoff;
2188 mutex_exit(&fp->f_lock);
2189 }
2190 FILE_UNUSE(fp, l);
2191 out:
2192 return (error);
2193 }
2194
2195 /*
2196 * Positional read system call.
2197 */
2198 int
2199 sys_pread(struct lwp *l, void *v, register_t *retval)
2200 {
2201 struct sys_pread_args /* {
2202 syscallarg(int) fd;
2203 syscallarg(void *) buf;
2204 syscallarg(size_t) nbyte;
2205 syscallarg(off_t) offset;
2206 } */ *uap = v;
2207 struct proc *p = l->l_proc;
2208 struct filedesc *fdp = p->p_fd;
2209 struct file *fp;
2210 struct vnode *vp;
2211 off_t offset;
2212 int error, fd = SCARG(uap, fd);
2213
2214 if ((fp = fd_getfile(fdp, fd)) == NULL)
2215 return (EBADF);
2216
2217 if ((fp->f_flag & FREAD) == 0) {
2218 mutex_exit(&fp->f_lock);
2219 return (EBADF);
2220 }
2221
2222 FILE_USE(fp);
2223
2224 vp = (struct vnode *)fp->f_data;
2225 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2226 error = ESPIPE;
2227 goto out;
2228 }
2229
2230 offset = SCARG(uap, offset);
2231
2232 /*
2233 * XXX This works because no file systems actually
2234 * XXX take any action on the seek operation.
2235 */
2236 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2237 goto out;
2238
2239 /* dofileread() will unuse the descriptor for us */
2240 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2241 &offset, 0, retval));
2242
2243 out:
2244 FILE_UNUSE(fp, l);
2245 return (error);
2246 }
2247
2248 /*
2249 * Positional scatter read system call.
2250 */
2251 int
2252 sys_preadv(struct lwp *l, void *v, register_t *retval)
2253 {
2254 struct sys_preadv_args /* {
2255 syscallarg(int) fd;
2256 syscallarg(const struct iovec *) iovp;
2257 syscallarg(int) iovcnt;
2258 syscallarg(off_t) offset;
2259 } */ *uap = v;
2260
2261 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
2262 SCARG(uap, iovcnt), &SCARG(uap, offset), 0, retval);
2263 }
2264
2265 /*
2266 * Positional write system call.
2267 */
2268 int
2269 sys_pwrite(struct lwp *l, void *v, register_t *retval)
2270 {
2271 struct sys_pwrite_args /* {
2272 syscallarg(int) fd;
2273 syscallarg(const void *) buf;
2274 syscallarg(size_t) nbyte;
2275 syscallarg(off_t) offset;
2276 } */ *uap = v;
2277 struct proc *p = l->l_proc;
2278 struct filedesc *fdp = p->p_fd;
2279 struct file *fp;
2280 struct vnode *vp;
2281 off_t offset;
2282 int error, fd = SCARG(uap, fd);
2283
2284 if ((fp = fd_getfile(fdp, fd)) == NULL)
2285 return (EBADF);
2286
2287 if ((fp->f_flag & FWRITE) == 0) {
2288 mutex_exit(&fp->f_lock);
2289 return (EBADF);
2290 }
2291
2292 FILE_USE(fp);
2293
2294 vp = (struct vnode *)fp->f_data;
2295 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2296 error = ESPIPE;
2297 goto out;
2298 }
2299
2300 offset = SCARG(uap, offset);
2301
2302 /*
2303 * XXX This works because no file systems actually
2304 * XXX take any action on the seek operation.
2305 */
2306 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2307 goto out;
2308
2309 /* dofilewrite() will unuse the descriptor for us */
2310 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2311 &offset, 0, retval));
2312
2313 out:
2314 FILE_UNUSE(fp, l);
2315 return (error);
2316 }
2317
2318 /*
2319 * Positional gather write system call.
2320 */
2321 int
2322 sys_pwritev(struct lwp *l, void *v, register_t *retval)
2323 {
2324 struct sys_pwritev_args /* {
2325 syscallarg(int) fd;
2326 syscallarg(const struct iovec *) iovp;
2327 syscallarg(int) iovcnt;
2328 syscallarg(off_t) offset;
2329 } */ *uap = v;
2330
2331 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
2332 SCARG(uap, iovcnt), &SCARG(uap, offset), 0, retval);
2333 }
2334
2335 /*
2336 * Check access permissions.
2337 */
2338 int
2339 sys_access(struct lwp *l, void *v, register_t *retval)
2340 {
2341 struct sys_access_args /* {
2342 syscallarg(const char *) path;
2343 syscallarg(int) flags;
2344 } */ *uap = v;
2345 kauth_cred_t cred;
2346 struct vnode *vp;
2347 int error, flags;
2348 struct nameidata nd;
2349
2350 cred = kauth_cred_dup(l->l_cred);
2351 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2352 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2353 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2354 SCARG(uap, path), l);
2355 /* Override default credentials */
2356 nd.ni_cnd.cn_cred = cred;
2357 if ((error = namei(&nd)) != 0)
2358 goto out;
2359 vp = nd.ni_vp;
2360
2361 /* Flags == 0 means only check for existence. */
2362 if (SCARG(uap, flags)) {
2363 flags = 0;
2364 if (SCARG(uap, flags) & R_OK)
2365 flags |= VREAD;
2366 if (SCARG(uap, flags) & W_OK)
2367 flags |= VWRITE;
2368 if (SCARG(uap, flags) & X_OK)
2369 flags |= VEXEC;
2370
2371 error = VOP_ACCESS(vp, flags, cred);
2372 if (!error && (flags & VWRITE))
2373 error = vn_writechk(vp);
2374 }
2375 vput(vp);
2376 out:
2377 kauth_cred_free(cred);
2378 return (error);
2379 }
2380
2381 /*
2382 * Common code for all sys_stat functions, including compat versions.
2383 */
2384 int
2385 do_sys_stat(struct lwp *l, const char *path, unsigned int nd_flags,
2386 struct stat *sb)
2387 {
2388 int error;
2389 struct nameidata nd;
2390
2391 NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT,
2392 UIO_USERSPACE, path, l);
2393 error = namei(&nd);
2394 if (error != 0)
2395 return error;
2396 error = vn_stat(nd.ni_vp, sb, l);
2397 vput(nd.ni_vp);
2398 return error;
2399 }
2400
2401 /*
2402 * Get file status; this version follows links.
2403 */
2404 /* ARGSUSED */
2405 int
2406 sys___stat30(struct lwp *l, void *v, register_t *retval)
2407 {
2408 struct sys___stat30_args /* {
2409 syscallarg(const char *) path;
2410 syscallarg(struct stat *) ub;
2411 } */ *uap = v;
2412 struct stat sb;
2413 int error;
2414
2415 error = do_sys_stat(l, SCARG(uap, path), FOLLOW, &sb);
2416 if (error)
2417 return error;
2418 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2419 }
2420
2421 /*
2422 * Get file status; this version does not follow links.
2423 */
2424 /* ARGSUSED */
2425 int
2426 sys___lstat30(struct lwp *l, void *v, register_t *retval)
2427 {
2428 struct sys___lstat30_args /* {
2429 syscallarg(const char *) path;
2430 syscallarg(struct stat *) ub;
2431 } */ *uap = v;
2432 struct stat sb;
2433 int error;
2434
2435 error = do_sys_stat(l, SCARG(uap, path), NOFOLLOW, &sb);
2436 if (error)
2437 return error;
2438 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2439 }
2440
2441 /*
2442 * Get configurable pathname variables.
2443 */
2444 /* ARGSUSED */
2445 int
2446 sys_pathconf(struct lwp *l, void *v, register_t *retval)
2447 {
2448 struct sys_pathconf_args /* {
2449 syscallarg(const char *) path;
2450 syscallarg(int) name;
2451 } */ *uap = v;
2452 int error;
2453 struct nameidata nd;
2454
2455 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2456 SCARG(uap, path), l);
2457 if ((error = namei(&nd)) != 0)
2458 return (error);
2459 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2460 vput(nd.ni_vp);
2461 return (error);
2462 }
2463
2464 /*
2465 * Return target name of a symbolic link.
2466 */
2467 /* ARGSUSED */
2468 int
2469 sys_readlink(struct lwp *l, void *v, register_t *retval)
2470 {
2471 struct sys_readlink_args /* {
2472 syscallarg(const char *) path;
2473 syscallarg(char *) buf;
2474 syscallarg(size_t) count;
2475 } */ *uap = v;
2476 struct vnode *vp;
2477 struct iovec aiov;
2478 struct uio auio;
2479 int error;
2480 struct nameidata nd;
2481
2482 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2483 SCARG(uap, path), l);
2484 if ((error = namei(&nd)) != 0)
2485 return (error);
2486 vp = nd.ni_vp;
2487 if (vp->v_type != VLNK)
2488 error = EINVAL;
2489 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2490 (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
2491 aiov.iov_base = SCARG(uap, buf);
2492 aiov.iov_len = SCARG(uap, count);
2493 auio.uio_iov = &aiov;
2494 auio.uio_iovcnt = 1;
2495 auio.uio_offset = 0;
2496 auio.uio_rw = UIO_READ;
2497 KASSERT(l == curlwp);
2498 auio.uio_vmspace = l->l_proc->p_vmspace;
2499 auio.uio_resid = SCARG(uap, count);
2500 error = VOP_READLINK(vp, &auio, l->l_cred);
2501 }
2502 vput(vp);
2503 *retval = SCARG(uap, count) - auio.uio_resid;
2504 return (error);
2505 }
2506
2507 /*
2508 * Change flags of a file given a path name.
2509 */
2510 /* ARGSUSED */
2511 int
2512 sys_chflags(struct lwp *l, void *v, register_t *retval)
2513 {
2514 struct sys_chflags_args /* {
2515 syscallarg(const char *) path;
2516 syscallarg(u_long) flags;
2517 } */ *uap = v;
2518 struct vnode *vp;
2519 int error;
2520 struct nameidata nd;
2521
2522 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2523 SCARG(uap, path), l);
2524 if ((error = namei(&nd)) != 0)
2525 return (error);
2526 vp = nd.ni_vp;
2527 error = change_flags(vp, SCARG(uap, flags), l);
2528 vput(vp);
2529 return (error);
2530 }
2531
2532 /*
2533 * Change flags of a file given a file descriptor.
2534 */
2535 /* ARGSUSED */
2536 int
2537 sys_fchflags(struct lwp *l, void *v, register_t *retval)
2538 {
2539 struct sys_fchflags_args /* {
2540 syscallarg(int) fd;
2541 syscallarg(u_long) flags;
2542 } */ *uap = v;
2543 struct proc *p = l->l_proc;
2544 struct vnode *vp;
2545 struct file *fp;
2546 int error;
2547
2548 /* getvnode() will use the descriptor for us */
2549 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2550 return (error);
2551 vp = (struct vnode *)fp->f_data;
2552 error = change_flags(vp, SCARG(uap, flags), l);
2553 VOP_UNLOCK(vp, 0);
2554 FILE_UNUSE(fp, l);
2555 return (error);
2556 }
2557
2558 /*
2559 * Change flags of a file given a path name; this version does
2560 * not follow links.
2561 */
2562 int
2563 sys_lchflags(struct lwp *l, void *v, register_t *retval)
2564 {
2565 struct sys_lchflags_args /* {
2566 syscallarg(const char *) path;
2567 syscallarg(u_long) flags;
2568 } */ *uap = v;
2569 struct vnode *vp;
2570 int error;
2571 struct nameidata nd;
2572
2573 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2574 SCARG(uap, path), l);
2575 if ((error = namei(&nd)) != 0)
2576 return (error);
2577 vp = nd.ni_vp;
2578 error = change_flags(vp, SCARG(uap, flags), l);
2579 vput(vp);
2580 return (error);
2581 }
2582
2583 /*
2584 * Common routine to change flags of a file.
2585 */
2586 int
2587 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2588 {
2589 struct vattr vattr;
2590 int error;
2591
2592 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
2593 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2594 /*
2595 * Non-superusers cannot change the flags on devices, even if they
2596 * own them.
2597 */
2598 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
2599 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2600 goto out;
2601 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2602 error = EINVAL;
2603 goto out;
2604 }
2605 }
2606 VATTR_NULL(&vattr);
2607 vattr.va_flags = flags;
2608 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2609 out:
2610 return (error);
2611 }
2612
2613 /*
2614 * Change mode of a file given path name; this version follows links.
2615 */
2616 /* ARGSUSED */
2617 int
2618 sys_chmod(struct lwp *l, void *v, register_t *retval)
2619 {
2620 struct sys_chmod_args /* {
2621 syscallarg(const char *) path;
2622 syscallarg(int) mode;
2623 } */ *uap = v;
2624 int error;
2625 struct nameidata nd;
2626
2627 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2628 SCARG(uap, path), l);
2629 if ((error = namei(&nd)) != 0)
2630 return (error);
2631
2632 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2633
2634 vrele(nd.ni_vp);
2635 return (error);
2636 }
2637
2638 /*
2639 * Change mode of a file given a file descriptor.
2640 */
2641 /* ARGSUSED */
2642 int
2643 sys_fchmod(struct lwp *l, void *v, register_t *retval)
2644 {
2645 struct sys_fchmod_args /* {
2646 syscallarg(int) fd;
2647 syscallarg(int) mode;
2648 } */ *uap = v;
2649 struct proc *p = l->l_proc;
2650 struct file *fp;
2651 int error;
2652
2653 /* getvnode() will use the descriptor for us */
2654 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2655 return (error);
2656
2657 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), l);
2658 FILE_UNUSE(fp, l);
2659 return (error);
2660 }
2661
2662 /*
2663 * Change mode of a file given path name; this version does not follow links.
2664 */
2665 /* ARGSUSED */
2666 int
2667 sys_lchmod(struct lwp *l, void *v, register_t *retval)
2668 {
2669 struct sys_lchmod_args /* {
2670 syscallarg(const char *) path;
2671 syscallarg(int) mode;
2672 } */ *uap = v;
2673 int error;
2674 struct nameidata nd;
2675
2676 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2677 SCARG(uap, path), l);
2678 if ((error = namei(&nd)) != 0)
2679 return (error);
2680
2681 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2682
2683 vrele(nd.ni_vp);
2684 return (error);
2685 }
2686
2687 /*
2688 * Common routine to set mode given a vnode.
2689 */
2690 static int
2691 change_mode(struct vnode *vp, int mode, struct lwp *l)
2692 {
2693 struct vattr vattr;
2694 int error;
2695
2696 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
2697 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2698 VATTR_NULL(&vattr);
2699 vattr.va_mode = mode & ALLPERMS;
2700 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2701 VOP_UNLOCK(vp, 0);
2702 return (error);
2703 }
2704
2705 /*
2706 * Set ownership given a path name; this version follows links.
2707 */
2708 /* ARGSUSED */
2709 int
2710 sys_chown(struct lwp *l, void *v, register_t *retval)
2711 {
2712 struct sys_chown_args /* {
2713 syscallarg(const char *) path;
2714 syscallarg(uid_t) uid;
2715 syscallarg(gid_t) gid;
2716 } */ *uap = v;
2717 int error;
2718 struct nameidata nd;
2719
2720 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2721 SCARG(uap, path), l);
2722 if ((error = namei(&nd)) != 0)
2723 return (error);
2724
2725 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2726
2727 vrele(nd.ni_vp);
2728 return (error);
2729 }
2730
2731 /*
2732 * Set ownership given a path name; this version follows links.
2733 * Provides POSIX semantics.
2734 */
2735 /* ARGSUSED */
2736 int
2737 sys___posix_chown(struct lwp *l, void *v, register_t *retval)
2738 {
2739 struct sys_chown_args /* {
2740 syscallarg(const char *) path;
2741 syscallarg(uid_t) uid;
2742 syscallarg(gid_t) gid;
2743 } */ *uap = v;
2744 int error;
2745 struct nameidata nd;
2746
2747 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2748 SCARG(uap, path), l);
2749 if ((error = namei(&nd)) != 0)
2750 return (error);
2751
2752 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2753
2754 vrele(nd.ni_vp);
2755 return (error);
2756 }
2757
2758 /*
2759 * Set ownership given a file descriptor.
2760 */
2761 /* ARGSUSED */
2762 int
2763 sys_fchown(struct lwp *l, void *v, register_t *retval)
2764 {
2765 struct sys_fchown_args /* {
2766 syscallarg(int) fd;
2767 syscallarg(uid_t) uid;
2768 syscallarg(gid_t) gid;
2769 } */ *uap = v;
2770 struct proc *p = l->l_proc;
2771 int error;
2772 struct file *fp;
2773
2774 /* getvnode() will use the descriptor for us */
2775 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2776 return (error);
2777
2778 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2779 SCARG(uap, gid), l, 0);
2780 FILE_UNUSE(fp, l);
2781 return (error);
2782 }
2783
2784 /*
2785 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2786 */
2787 /* ARGSUSED */
2788 int
2789 sys___posix_fchown(struct lwp *l, void *v, register_t *retval)
2790 {
2791 struct sys_fchown_args /* {
2792 syscallarg(int) fd;
2793 syscallarg(uid_t) uid;
2794 syscallarg(gid_t) gid;
2795 } */ *uap = v;
2796 struct proc *p = l->l_proc;
2797 int error;
2798 struct file *fp;
2799
2800 /* getvnode() will use the descriptor for us */
2801 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2802 return (error);
2803
2804 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2805 SCARG(uap, gid), l, 1);
2806 FILE_UNUSE(fp, l);
2807 return (error);
2808 }
2809
2810 /*
2811 * Set ownership given a path name; this version does not follow links.
2812 */
2813 /* ARGSUSED */
2814 int
2815 sys_lchown(struct lwp *l, void *v, register_t *retval)
2816 {
2817 struct sys_lchown_args /* {
2818 syscallarg(const char *) path;
2819 syscallarg(uid_t) uid;
2820 syscallarg(gid_t) gid;
2821 } */ *uap = v;
2822 int error;
2823 struct nameidata nd;
2824
2825 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2826 SCARG(uap, path), l);
2827 if ((error = namei(&nd)) != 0)
2828 return (error);
2829
2830 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2831
2832 vrele(nd.ni_vp);
2833 return (error);
2834 }
2835
2836 /*
2837 * Set ownership given a path name; this version does not follow links.
2838 * Provides POSIX/XPG semantics.
2839 */
2840 /* ARGSUSED */
2841 int
2842 sys___posix_lchown(struct lwp *l, void *v, register_t *retval)
2843 {
2844 struct sys_lchown_args /* {
2845 syscallarg(const char *) path;
2846 syscallarg(uid_t) uid;
2847 syscallarg(gid_t) gid;
2848 } */ *uap = v;
2849 int error;
2850 struct nameidata nd;
2851
2852 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2853 SCARG(uap, path), l);
2854 if ((error = namei(&nd)) != 0)
2855 return (error);
2856
2857 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2858
2859 vrele(nd.ni_vp);
2860 return (error);
2861 }
2862
2863 /*
2864 * Common routine to set ownership given a vnode.
2865 */
2866 static int
2867 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2868 int posix_semantics)
2869 {
2870 struct vattr vattr;
2871 mode_t newmode;
2872 int error;
2873
2874 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
2875 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2876 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2877 goto out;
2878
2879 #define CHANGED(x) ((int)(x) != -1)
2880 newmode = vattr.va_mode;
2881 if (posix_semantics) {
2882 /*
2883 * POSIX/XPG semantics: if the caller is not the super-user,
2884 * clear set-user-id and set-group-id bits. Both POSIX and
2885 * the XPG consider the behaviour for calls by the super-user
2886 * implementation-defined; we leave the set-user-id and set-
2887 * group-id settings intact in that case.
2888 */
2889 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2890 NULL) != 0)
2891 newmode &= ~(S_ISUID | S_ISGID);
2892 } else {
2893 /*
2894 * NetBSD semantics: when changing owner and/or group,
2895 * clear the respective bit(s).
2896 */
2897 if (CHANGED(uid))
2898 newmode &= ~S_ISUID;
2899 if (CHANGED(gid))
2900 newmode &= ~S_ISGID;
2901 }
2902 /* Update va_mode iff altered. */
2903 if (vattr.va_mode == newmode)
2904 newmode = VNOVAL;
2905
2906 VATTR_NULL(&vattr);
2907 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2908 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2909 vattr.va_mode = newmode;
2910 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2911 #undef CHANGED
2912
2913 out:
2914 VOP_UNLOCK(vp, 0);
2915 return (error);
2916 }
2917
2918 /*
2919 * Set the access and modification times given a path name; this
2920 * version follows links.
2921 */
2922 /* ARGSUSED */
2923 int
2924 sys_utimes(struct lwp *l, void *v, register_t *retval)
2925 {
2926 struct sys_utimes_args /* {
2927 syscallarg(const char *) path;
2928 syscallarg(const struct timeval *) tptr;
2929 } */ *uap = v;
2930
2931 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
2932 SCARG(uap, tptr), UIO_USERSPACE);
2933 }
2934
2935 /*
2936 * Set the access and modification times given a file descriptor.
2937 */
2938 /* ARGSUSED */
2939 int
2940 sys_futimes(struct lwp *l, void *v, register_t *retval)
2941 {
2942 struct sys_futimes_args /* {
2943 syscallarg(int) fd;
2944 syscallarg(const struct timeval *) tptr;
2945 } */ *uap = v;
2946 int error;
2947 struct file *fp;
2948
2949 /* getvnode() will use the descriptor for us */
2950 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2951 return (error);
2952
2953 error = do_sys_utimes(l, fp->f_data, NULL, 0,
2954 SCARG(uap, tptr), UIO_USERSPACE);
2955
2956 FILE_UNUSE(fp, l);
2957 return (error);
2958 }
2959
2960 /*
2961 * Set the access and modification times given a path name; this
2962 * version does not follow links.
2963 */
2964 int
2965 sys_lutimes(struct lwp *l, void *v, register_t *retval)
2966 {
2967 struct sys_lutimes_args /* {
2968 syscallarg(const char *) path;
2969 syscallarg(const struct timeval *) tptr;
2970 } */ *uap = v;
2971
2972 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
2973 SCARG(uap, tptr), UIO_USERSPACE);
2974 }
2975
2976 /*
2977 * Common routine to set access and modification times given a vnode.
2978 */
2979 int
2980 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
2981 const struct timeval *tptr, enum uio_seg seg)
2982 {
2983 struct vattr vattr;
2984 struct nameidata nd;
2985 int error;
2986
2987 VATTR_NULL(&vattr);
2988 if (tptr == NULL) {
2989 nanotime(&vattr.va_atime);
2990 vattr.va_mtime = vattr.va_atime;
2991 vattr.va_vaflags |= VA_UTIMES_NULL;
2992 } else {
2993 struct timeval tv[2];
2994
2995 if (seg != UIO_SYSSPACE) {
2996 error = copyin(tptr, &tv, sizeof (tv));
2997 if (error != 0)
2998 return error;
2999 tptr = tv;
3000 }
3001 TIMEVAL_TO_TIMESPEC(tptr, &vattr.va_atime);
3002 TIMEVAL_TO_TIMESPEC(tptr + 1, &vattr.va_mtime);
3003 }
3004
3005 if (vp == NULL) {
3006 NDINIT(&nd, LOOKUP, flag | TRYEMULROOT, UIO_USERSPACE, path, l);
3007 if ((error = namei(&nd)) != 0)
3008 return (error);
3009 vp = nd.ni_vp;
3010 } else
3011 nd.ni_vp = NULL;
3012
3013 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
3014 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3015 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3016 VOP_UNLOCK(vp, 0);
3017
3018 if (nd.ni_vp != NULL)
3019 vrele(nd.ni_vp);
3020
3021 return (error);
3022 }
3023
3024 /*
3025 * Truncate a file given its path name.
3026 */
3027 /* ARGSUSED */
3028 int
3029 sys_truncate(struct lwp *l, void *v, register_t *retval)
3030 {
3031 struct sys_truncate_args /* {
3032 syscallarg(const char *) path;
3033 syscallarg(int) pad;
3034 syscallarg(off_t) length;
3035 } */ *uap = v;
3036 struct vnode *vp;
3037 struct vattr vattr;
3038 int error;
3039 struct nameidata nd;
3040
3041 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
3042 SCARG(uap, path), l);
3043 if ((error = namei(&nd)) != 0)
3044 return (error);
3045 vp = nd.ni_vp;
3046 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
3047 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3048 if (vp->v_type == VDIR)
3049 error = EISDIR;
3050 else if ((error = vn_writechk(vp)) == 0 &&
3051 (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
3052 VATTR_NULL(&vattr);
3053 vattr.va_size = SCARG(uap, length);
3054 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3055 }
3056 vput(vp);
3057 return (error);
3058 }
3059
3060 /*
3061 * Truncate a file given a file descriptor.
3062 */
3063 /* ARGSUSED */
3064 int
3065 sys_ftruncate(struct lwp *l, void *v, register_t *retval)
3066 {
3067 struct sys_ftruncate_args /* {
3068 syscallarg(int) fd;
3069 syscallarg(int) pad;
3070 syscallarg(off_t) length;
3071 } */ *uap = v;
3072 struct proc *p = l->l_proc;
3073 struct vattr vattr;
3074 struct vnode *vp;
3075 struct file *fp;
3076 int error;
3077
3078 /* getvnode() will use the descriptor for us */
3079 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3080 return (error);
3081 if ((fp->f_flag & FWRITE) == 0) {
3082 error = EINVAL;
3083 goto out;
3084 }
3085 vp = (struct vnode *)fp->f_data;
3086 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
3087 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3088 if (vp->v_type == VDIR)
3089 error = EISDIR;
3090 else if ((error = vn_writechk(vp)) == 0) {
3091 VATTR_NULL(&vattr);
3092 vattr.va_size = SCARG(uap, length);
3093 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3094 }
3095 VOP_UNLOCK(vp, 0);
3096 out:
3097 FILE_UNUSE(fp, l);
3098 return (error);
3099 }
3100
3101 /*
3102 * Sync an open file.
3103 */
3104 /* ARGSUSED */
3105 int
3106 sys_fsync(struct lwp *l, void *v, register_t *retval)
3107 {
3108 struct sys_fsync_args /* {
3109 syscallarg(int) fd;
3110 } */ *uap = v;
3111 struct proc *p = l->l_proc;
3112 struct vnode *vp;
3113 struct file *fp;
3114 int error;
3115
3116 /* getvnode() will use the descriptor for us */
3117 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3118 return (error);
3119 vp = (struct vnode *)fp->f_data;
3120 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3121 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3122 if (error == 0 && bioopsp != NULL &&
3123 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3124 (*bioopsp->io_fsync)(vp, 0);
3125 VOP_UNLOCK(vp, 0);
3126 FILE_UNUSE(fp, l);
3127 return (error);
3128 }
3129
3130 /*
3131 * Sync a range of file data. API modeled after that found in AIX.
3132 *
3133 * FDATASYNC indicates that we need only save enough metadata to be able
3134 * to re-read the written data. Note we duplicate AIX's requirement that
3135 * the file be open for writing.
3136 */
3137 /* ARGSUSED */
3138 int
3139 sys_fsync_range(struct lwp *l, void *v, register_t *retval)
3140 {
3141 struct sys_fsync_range_args /* {
3142 syscallarg(int) fd;
3143 syscallarg(int) flags;
3144 syscallarg(off_t) start;
3145 syscallarg(off_t) length;
3146 } */ *uap = v;
3147 struct proc *p = l->l_proc;
3148 struct vnode *vp;
3149 struct file *fp;
3150 int flags, nflags;
3151 off_t s, e, len;
3152 int error;
3153
3154 /* getvnode() will use the descriptor for us */
3155 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3156 return (error);
3157
3158 if ((fp->f_flag & FWRITE) == 0) {
3159 error = EBADF;
3160 goto out;
3161 }
3162
3163 flags = SCARG(uap, flags);
3164 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3165 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3166 error = EINVAL;
3167 goto out;
3168 }
3169 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3170 if (flags & FDATASYNC)
3171 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3172 else
3173 nflags = FSYNC_WAIT;
3174 if (flags & FDISKSYNC)
3175 nflags |= FSYNC_CACHE;
3176
3177 len = SCARG(uap, length);
3178 /* If length == 0, we do the whole file, and s = l = 0 will do that */
3179 if (len) {
3180 s = SCARG(uap, start);
3181 e = s + len;
3182 if (e < s) {
3183 error = EINVAL;
3184 goto out;
3185 }
3186 } else {
3187 e = 0;
3188 s = 0;
3189 }
3190
3191 vp = (struct vnode *)fp->f_data;
3192 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3193 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
3194
3195 if (error == 0 && bioopsp != NULL &&
3196 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3197 (*bioopsp->io_fsync)(vp, nflags);
3198
3199 VOP_UNLOCK(vp, 0);
3200 out:
3201 FILE_UNUSE(fp, l);
3202 return (error);
3203 }
3204
3205 /*
3206 * Sync the data of an open file.
3207 */
3208 /* ARGSUSED */
3209 int
3210 sys_fdatasync(struct lwp *l, void *v, register_t *retval)
3211 {
3212 struct sys_fdatasync_args /* {
3213 syscallarg(int) fd;
3214 } */ *uap = v;
3215 struct proc *p = l->l_proc;
3216 struct vnode *vp;
3217 struct file *fp;
3218 int error;
3219
3220 /* getvnode() will use the descriptor for us */
3221 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3222 return (error);
3223 if ((fp->f_flag & FWRITE) == 0) {
3224 FILE_UNUSE(fp, l);
3225 return (EBADF);
3226 }
3227 vp = (struct vnode *)fp->f_data;
3228 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3229 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
3230 VOP_UNLOCK(vp, 0);
3231 FILE_UNUSE(fp, l);
3232 return (error);
3233 }
3234
3235 /*
3236 * Rename files, (standard) BSD semantics frontend.
3237 */
3238 /* ARGSUSED */
3239 int
3240 sys_rename(struct lwp *l, void *v, register_t *retval)
3241 {
3242 struct sys_rename_args /* {
3243 syscallarg(const char *) from;
3244 syscallarg(const char *) to;
3245 } */ *uap = v;
3246
3247 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 0));
3248 }
3249
3250 /*
3251 * Rename files, POSIX semantics frontend.
3252 */
3253 /* ARGSUSED */
3254 int
3255 sys___posix_rename(struct lwp *l, void *v, register_t *retval)
3256 {
3257 struct sys___posix_rename_args /* {
3258 syscallarg(const char *) from;
3259 syscallarg(const char *) to;
3260 } */ *uap = v;
3261
3262 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 1));
3263 }
3264
3265 /*
3266 * Rename files. Source and destination must either both be directories,
3267 * or both not be directories. If target is a directory, it must be empty.
3268 * If `from' and `to' refer to the same object, the value of the `retain'
3269 * argument is used to determine whether `from' will be
3270 *
3271 * (retain == 0) deleted unless `from' and `to' refer to the same
3272 * object in the file system's name space (BSD).
3273 * (retain == 1) always retained (POSIX).
3274 */
3275 static int
3276 rename_files(const char *from, const char *to, struct lwp *l, int retain)
3277 {
3278 struct vnode *tvp, *fvp, *tdvp;
3279 struct nameidata fromnd, tond;
3280 struct proc *p;
3281 int error;
3282
3283 NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT,
3284 UIO_USERSPACE, from, l);
3285 if ((error = namei(&fromnd)) != 0)
3286 return (error);
3287 if (fromnd.ni_dvp != fromnd.ni_vp)
3288 VOP_UNLOCK(fromnd.ni_dvp, 0);
3289 fvp = fromnd.ni_vp;
3290 NDINIT(&tond, RENAME,
3291 LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT
3292 | (fvp->v_type == VDIR ? CREATEDIR : 0),
3293 UIO_USERSPACE, to, l);
3294 if ((error = namei(&tond)) != 0) {
3295 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3296 vrele(fromnd.ni_dvp);
3297 vrele(fvp);
3298 goto out1;
3299 }
3300 tdvp = tond.ni_dvp;
3301 tvp = tond.ni_vp;
3302
3303 if (tvp != NULL) {
3304 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3305 error = ENOTDIR;
3306 goto out;
3307 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3308 error = EISDIR;
3309 goto out;
3310 }
3311 }
3312
3313 if (fvp == tdvp)
3314 error = EINVAL;
3315
3316 /*
3317 * Source and destination refer to the same object.
3318 */
3319 if (fvp == tvp) {
3320 if (retain)
3321 error = -1;
3322 else if (fromnd.ni_dvp == tdvp &&
3323 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3324 !memcmp(fromnd.ni_cnd.cn_nameptr,
3325 tond.ni_cnd.cn_nameptr,
3326 fromnd.ni_cnd.cn_namelen))
3327 error = -1;
3328 }
3329
3330 #if NVERIEXEC > 0
3331 if (!error) {
3332 char *f1, *f2;
3333
3334 f1 = malloc(fromnd.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3335 strlcpy(f1, fromnd.ni_cnd.cn_nameptr, fromnd.ni_cnd.cn_namelen);
3336
3337 f2 = malloc(tond.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3338 strlcpy(f2, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen);
3339
3340 error = veriexec_renamechk(l, fvp, f1, tvp, f2);
3341
3342 free(f1, M_TEMP);
3343 free(f2, M_TEMP);
3344 }
3345 #endif /* NVERIEXEC > 0 */
3346
3347 out:
3348 p = l->l_proc;
3349 if (!error) {
3350 VOP_LEASE(tdvp, l->l_cred, LEASE_WRITE);
3351 if (fromnd.ni_dvp != tdvp)
3352 VOP_LEASE(fromnd.ni_dvp, l->l_cred, LEASE_WRITE);
3353 if (tvp) {
3354 VOP_LEASE(tvp, l->l_cred, LEASE_WRITE);
3355 }
3356 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3357 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3358 } else {
3359 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3360 if (tdvp == tvp)
3361 vrele(tdvp);
3362 else
3363 vput(tdvp);
3364 if (tvp)
3365 vput(tvp);
3366 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3367 vrele(fromnd.ni_dvp);
3368 vrele(fvp);
3369 }
3370 vrele(tond.ni_startdir);
3371 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3372 out1:
3373 if (fromnd.ni_startdir)
3374 vrele(fromnd.ni_startdir);
3375 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3376 return (error == -1 ? 0 : error);
3377 }
3378
3379 /*
3380 * Make a directory file.
3381 */
3382 /* ARGSUSED */
3383 int
3384 sys_mkdir(struct lwp *l, void *v, register_t *retval)
3385 {
3386 struct sys_mkdir_args /* {
3387 syscallarg(const char *) path;
3388 syscallarg(int) mode;
3389 } */ *uap = v;
3390 struct proc *p = l->l_proc;
3391 struct vnode *vp;
3392 struct vattr vattr;
3393 int error;
3394 struct nameidata nd;
3395
3396 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, UIO_USERSPACE,
3397 SCARG(uap, path), l);
3398 if ((error = namei(&nd)) != 0)
3399 return (error);
3400 vp = nd.ni_vp;
3401 if (vp != NULL) {
3402 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3403 if (nd.ni_dvp == vp)
3404 vrele(nd.ni_dvp);
3405 else
3406 vput(nd.ni_dvp);
3407 vrele(vp);
3408 return (EEXIST);
3409 }
3410 VATTR_NULL(&vattr);
3411 vattr.va_type = VDIR;
3412 /* We will read cwdi->cwdi_cmask unlocked. */
3413 vattr.va_mode =
3414 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3415 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
3416 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3417 if (!error)
3418 vput(nd.ni_vp);
3419 return (error);
3420 }
3421
3422 /*
3423 * Remove a directory file.
3424 */
3425 /* ARGSUSED */
3426 int
3427 sys_rmdir(struct lwp *l, void *v, register_t *retval)
3428 {
3429 struct sys_rmdir_args /* {
3430 syscallarg(const char *) path;
3431 } */ *uap = v;
3432 struct vnode *vp;
3433 int error;
3434 struct nameidata nd;
3435
3436 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
3437 SCARG(uap, path), l);
3438 if ((error = namei(&nd)) != 0)
3439 return (error);
3440 vp = nd.ni_vp;
3441 if (vp->v_type != VDIR) {
3442 error = ENOTDIR;
3443 goto out;
3444 }
3445 /*
3446 * No rmdir "." please.
3447 */
3448 if (nd.ni_dvp == vp) {
3449 error = EINVAL;
3450 goto out;
3451 }
3452 /*
3453 * The root of a mounted filesystem cannot be deleted.
3454 */
3455 if (vp->v_vflag & VV_ROOT) {
3456 error = EBUSY;
3457 goto out;
3458 }
3459 VOP_LEASE(nd.ni_dvp, l->l_cred, LEASE_WRITE);
3460 VOP_LEASE(vp, l->l_cred, LEASE_WRITE);
3461 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3462 return (error);
3463
3464 out:
3465 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3466 if (nd.ni_dvp == vp)
3467 vrele(nd.ni_dvp);
3468 else
3469 vput(nd.ni_dvp);
3470 vput(vp);
3471 return (error);
3472 }
3473
3474 /*
3475 * Read a block of directory entries in a file system independent format.
3476 */
3477 int
3478 sys___getdents30(struct lwp *l, void *v, register_t *retval)
3479 {
3480 struct sys___getdents30_args /* {
3481 syscallarg(int) fd;
3482 syscallarg(char *) buf;
3483 syscallarg(size_t) count;
3484 } */ *uap = v;
3485 struct proc *p = l->l_proc;
3486 struct file *fp;
3487 int error, done;
3488
3489 /* getvnode() will use the descriptor for us */
3490 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3491 return (error);
3492 if ((fp->f_flag & FREAD) == 0) {
3493 error = EBADF;
3494 goto out;
3495 }
3496 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3497 SCARG(uap, count), &done, l, 0, 0);
3498 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
3499 *retval = done;
3500 out:
3501 FILE_UNUSE(fp, l);
3502 return (error);
3503 }
3504
3505 /*
3506 * Set the mode mask for creation of filesystem nodes.
3507 */
3508 int
3509 sys_umask(struct lwp *l, void *v, register_t *retval)
3510 {
3511 struct sys_umask_args /* {
3512 syscallarg(mode_t) newmask;
3513 } */ *uap = v;
3514 struct proc *p = l->l_proc;
3515 struct cwdinfo *cwdi;
3516
3517 /*
3518 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
3519 * important is that we serialize changes to the mask. The
3520 * rw_exit() will issue a write memory barrier on our behalf,
3521 * and force the changes out to other CPUs (as it must use an
3522 * atomic operation, draining the local CPU's store buffers).
3523 */
3524 cwdi = p->p_cwdi;
3525 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
3526 *retval = cwdi->cwdi_cmask;
3527 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3528 rw_exit(&cwdi->cwdi_lock);
3529
3530 return (0);
3531 }
3532
3533 /*
3534 * Void all references to file by ripping underlying filesystem
3535 * away from vnode.
3536 */
3537 /* ARGSUSED */
3538 int
3539 sys_revoke(struct lwp *l, void *v, register_t *retval)
3540 {
3541 struct sys_revoke_args /* {
3542 syscallarg(const char *) path;
3543 } */ *uap = v;
3544 struct vnode *vp;
3545 struct vattr vattr;
3546 int error;
3547 bool revoke;
3548 struct nameidata nd;
3549
3550 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
3551 SCARG(uap, path), l);
3552 if ((error = namei(&nd)) != 0)
3553 return (error);
3554 vp = nd.ni_vp;
3555 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
3556 goto out;
3557 if (kauth_cred_geteuid(l->l_cred) != vattr.va_uid &&
3558 (error = kauth_authorize_generic(l->l_cred,
3559 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
3560 goto out;
3561 mutex_enter(&vp->v_interlock);
3562 revoke = (vp->v_usecount > 1 || (vp->v_iflag & (VI_ALIASED|VI_LAYER)));
3563 mutex_exit(&vp->v_interlock);
3564 if (revoke)
3565 VOP_REVOKE(vp, REVOKEALL);
3566 out:
3567 vrele(vp);
3568 return (error);
3569 }
3570
3571 /*
3572 * Convert a user file descriptor to a kernel file entry.
3573 */
3574 int
3575 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3576 {
3577 struct vnode *vp;
3578 struct file *fp;
3579
3580 if ((fp = fd_getfile(fdp, fd)) == NULL)
3581 return (EBADF);
3582
3583 FILE_USE(fp);
3584
3585 if (fp->f_type != DTYPE_VNODE) {
3586 FILE_UNUSE(fp, NULL);
3587 return (EINVAL);
3588 }
3589
3590 vp = (struct vnode *)fp->f_data;
3591 if (vp->v_type == VBAD) {
3592 FILE_UNUSE(fp, NULL);
3593 return (EBADF);
3594 }
3595
3596 *fpp = fp;
3597 return (0);
3598 }
3599