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