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