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