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