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