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