Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / kern / vfs_syscalls.c
blobeec6025b4e9be9ea2388f0903e755cfd0db524a6
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/conf.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mountctl.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
54 #include <sys/file.h>
55 #include <sys/linker.h>
56 #include <sys/stat.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
59 #include <sys/proc.h>
60 #include <sys/priv.h>
61 #include <sys/jail.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/dirent.h>
65 #include <sys/extattr.h>
66 #include <sys/spinlock.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/objcache.h>
69 #include <sys/sysctl.h>
71 #include <sys/buf2.h>
72 #include <sys/file2.h>
73 #include <sys/spinlock2.h>
74 #include <sys/mplock2.h>
76 #include <vm/vm.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount *mp, const char *ctl, ...)
86 __printflike(2, 3);
87 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
88 static int checkvp_chdir (struct vnode *vn, struct thread *td);
89 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
90 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
91 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
92 static int getutimes (const struct timeval *, struct timespec *);
93 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
94 static int setfmode (struct vnode *, int);
95 static int setfflags (struct vnode *, int);
96 static int setutimes (struct vnode *, struct vattr *,
97 const struct timespec *, int);
98 static int usermount = 0; /* if 1, non-root can mount fs. */
100 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
102 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
103 "Allow non-root users to mount filesystems");
106 * Virtual File System System Calls
110 * Mount a file system.
112 * mount_args(char *type, char *path, int flags, caddr_t data)
114 * MPALMOSTSAFE
117 sys_mount(struct mount_args *uap)
119 struct thread *td = curthread;
120 struct vnode *vp;
121 struct nchandle nch;
122 struct mount *mp, *nullmp;
123 struct vfsconf *vfsp;
124 int error, flag = 0, flag2 = 0;
125 int hasmount;
126 struct vattr va;
127 struct nlookupdata nd;
128 char fstypename[MFSNAMELEN];
129 struct ucred *cred;
131 get_mplock();
132 cred = td->td_ucred;
133 if (jailed(cred)) {
134 error = EPERM;
135 goto done;
137 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
138 goto done;
141 * Do not allow NFS export by non-root users.
143 if (uap->flags & MNT_EXPORTED) {
144 error = priv_check(td, PRIV_ROOT);
145 if (error)
146 goto done;
149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
151 if (priv_check(td, PRIV_ROOT))
152 uap->flags |= MNT_NOSUID | MNT_NODEV;
155 * Lookup the requested path and extract the nch and vnode.
157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
158 if (error == 0) {
159 if ((error = nlookup(&nd)) == 0) {
160 if (nd.nl_nch.ncp->nc_vp == NULL)
161 error = ENOENT;
164 if (error) {
165 nlookup_done(&nd);
166 goto done;
170 * If the target filesystem is resolved via a nullfs mount, then
171 * nd.nl_nch.mount will be pointing to the nullfs mount structure
172 * instead of the target file system. We need it in case we are
173 * doing an update.
175 nullmp = nd.nl_nch.mount;
178 * Extract the locked+refd ncp and cleanup the nd structure
180 nch = nd.nl_nch;
181 cache_zero(&nd.nl_nch);
182 nlookup_done(&nd);
184 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
185 (mp = cache_findmount(&nch)) != NULL) {
186 cache_dropmount(mp);
187 hasmount = 1;
188 } else {
189 hasmount = 0;
194 * now we have the locked ref'd nch and unreferenced vnode.
196 vp = nch.ncp->nc_vp;
197 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
198 cache_put(&nch);
199 goto done;
201 cache_unlock(&nch);
204 * Extract the file system type. We need to know this early, to take
205 * appropriate actions if we are dealing with a nullfs.
207 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
208 cache_drop(&nch);
209 vput(vp);
210 goto done;
214 * Now we have an unlocked ref'd nch and a locked ref'd vp
216 if (uap->flags & MNT_UPDATE) {
217 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
218 cache_drop(&nch);
219 vput(vp);
220 error = EINVAL;
221 goto done;
224 if (strncmp(fstypename, "null", 5) == 0) {
225 KKASSERT(nullmp);
226 mp = nullmp;
227 } else {
228 mp = vp->v_mount;
231 flag = mp->mnt_flag;
232 flag2 = mp->mnt_kern_flag;
234 * We only allow the filesystem to be reloaded if it
235 * is currently mounted read-only.
237 if ((uap->flags & MNT_RELOAD) &&
238 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
239 cache_drop(&nch);
240 vput(vp);
241 error = EOPNOTSUPP; /* Needs translation */
242 goto done;
245 * Only root, or the user that did the original mount is
246 * permitted to update it.
248 if (mp->mnt_stat.f_owner != cred->cr_uid &&
249 (error = priv_check(td, PRIV_ROOT))) {
250 cache_drop(&nch);
251 vput(vp);
252 goto done;
254 if (vfs_busy(mp, LK_NOWAIT)) {
255 cache_drop(&nch);
256 vput(vp);
257 error = EBUSY;
258 goto done;
260 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
261 cache_drop(&nch);
262 vfs_unbusy(mp);
263 vput(vp);
264 error = EBUSY;
265 goto done;
267 vsetflags(vp, VMOUNT);
268 mp->mnt_flag |=
269 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
270 vn_unlock(vp);
271 goto update;
274 * If the user is not root, ensure that they own the directory
275 * onto which we are attempting to mount.
277 if ((error = VOP_GETATTR(vp, &va)) ||
278 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
279 cache_drop(&nch);
280 vput(vp);
281 goto done;
283 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
284 cache_drop(&nch);
285 vput(vp);
286 goto done;
288 if (vp->v_type != VDIR) {
289 cache_drop(&nch);
290 vput(vp);
291 error = ENOTDIR;
292 goto done;
294 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
295 cache_drop(&nch);
296 vput(vp);
297 error = EPERM;
298 goto done;
300 vfsp = vfsconf_find_by_name(fstypename);
301 if (vfsp == NULL) {
302 linker_file_t lf;
304 /* Only load modules for root (very important!) */
305 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
306 cache_drop(&nch);
307 vput(vp);
308 goto done;
310 error = linker_load_file(fstypename, &lf);
311 if (error || lf == NULL) {
312 cache_drop(&nch);
313 vput(vp);
314 if (lf == NULL)
315 error = ENODEV;
316 goto done;
318 lf->userrefs++;
319 /* lookup again, see if the VFS was loaded */
320 vfsp = vfsconf_find_by_name(fstypename);
321 if (vfsp == NULL) {
322 lf->userrefs--;
323 linker_file_unload(lf);
324 cache_drop(&nch);
325 vput(vp);
326 error = ENODEV;
327 goto done;
330 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
331 cache_drop(&nch);
332 vput(vp);
333 error = EBUSY;
334 goto done;
336 vsetflags(vp, VMOUNT);
339 * Allocate and initialize the filesystem.
341 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
342 mount_init(mp);
343 vfs_busy(mp, LK_NOWAIT);
344 mp->mnt_op = vfsp->vfc_vfsops;
345 mp->mnt_vfc = vfsp;
346 vfsp->vfc_refcount++;
347 mp->mnt_stat.f_type = vfsp->vfc_typenum;
348 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
349 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
350 mp->mnt_stat.f_owner = cred->cr_uid;
351 vn_unlock(vp);
352 update:
354 * Set the mount level flags.
356 if (uap->flags & MNT_RDONLY)
357 mp->mnt_flag |= MNT_RDONLY;
358 else if (mp->mnt_flag & MNT_RDONLY)
359 mp->mnt_kern_flag |= MNTK_WANTRDWR;
360 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
361 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
362 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
365 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
366 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
367 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
369 * Mount the filesystem.
370 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
371 * get.
373 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
374 if (mp->mnt_flag & MNT_UPDATE) {
375 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
376 mp->mnt_flag &= ~MNT_RDONLY;
377 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
378 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
379 if (error) {
380 mp->mnt_flag = flag;
381 mp->mnt_kern_flag = flag2;
383 vfs_unbusy(mp);
384 vclrflags(vp, VMOUNT);
385 vrele(vp);
386 cache_drop(&nch);
387 goto done;
389 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
399 * namecache record.
401 if (!error) {
402 if (mp->mnt_ncmountpt.ncp == NULL) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
407 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
410 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
412 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
413 vclrflags(vp, VMOUNT);
414 mountlist_insert(mp, MNTINS_LAST);
415 vn_unlock(vp);
416 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
417 error = vfs_allocate_syncvnode(mp);
418 vfs_unbusy(mp);
419 error = VFS_START(mp, 0);
420 vrele(vp);
421 } else {
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
427 vclrflags(vp, VMOUNT);
428 mp->mnt_vfc->vfc_refcount--;
429 vfs_unbusy(mp);
430 kfree(mp, M_MOUNT);
431 cache_drop(&nch);
432 vput(vp);
434 done:
435 rel_mplock();
436 return (error);
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * The passed ncp is ref'd and locked (from the mount code) and
445 * must be associated with the vnode representing the root of the
446 * mount point.
448 struct checkdirs_info {
449 struct nchandle old_nch;
450 struct nchandle new_nch;
451 struct vnode *old_vp;
452 struct vnode *new_vp;
455 static int checkdirs_callback(struct proc *p, void *data);
457 static void
458 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
460 struct checkdirs_info info;
461 struct vnode *olddp;
462 struct vnode *newdp;
463 struct mount *mp;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp = old_nch->ncp->nc_vp;
470 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
471 return;
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
477 mp = new_nch->mount;
478 if (VFS_ROOT(mp, &newdp))
479 panic("mount: lost mount");
480 cache_setunresolved(new_nch);
481 cache_setvp(new_nch, newdp);
484 * Special handling of the root node
486 if (rootvnode == olddp) {
487 vref(newdp);
488 vfs_cache_setroot(newdp, cache_hold(new_nch));
492 * Pass newdp separately so the callback does not have to access
493 * it via new_nch->ncp->nc_vp.
495 info.old_nch = *old_nch;
496 info.new_nch = *new_nch;
497 info.new_vp = newdp;
498 allproc_scan(checkdirs_callback, &info);
499 vput(newdp);
503 * NOTE: callback is not MP safe because the scanned process's filedesc
504 * structure can be ripped out from under us, amoung other things.
506 static int
507 checkdirs_callback(struct proc *p, void *data)
509 struct checkdirs_info *info = data;
510 struct filedesc *fdp;
511 struct nchandle ncdrop1;
512 struct nchandle ncdrop2;
513 struct vnode *vprele1;
514 struct vnode *vprele2;
516 if ((fdp = p->p_fd) != NULL) {
517 cache_zero(&ncdrop1);
518 cache_zero(&ncdrop2);
519 vprele1 = NULL;
520 vprele2 = NULL;
523 * MPUNSAFE - XXX fdp can be pulled out from under a
524 * foreign process.
526 * A shared filedesc is ok, we don't have to copy it
527 * because we are making this change globally.
529 spin_lock(&fdp->fd_spin);
530 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
531 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
532 vprele1 = fdp->fd_cdir;
533 vref(info->new_vp);
534 fdp->fd_cdir = info->new_vp;
535 ncdrop1 = fdp->fd_ncdir;
536 cache_copy(&info->new_nch, &fdp->fd_ncdir);
538 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
539 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
540 vprele2 = fdp->fd_rdir;
541 vref(info->new_vp);
542 fdp->fd_rdir = info->new_vp;
543 ncdrop2 = fdp->fd_nrdir;
544 cache_copy(&info->new_nch, &fdp->fd_nrdir);
546 spin_unlock(&fdp->fd_spin);
547 if (ncdrop1.ncp)
548 cache_drop(&ncdrop1);
549 if (ncdrop2.ncp)
550 cache_drop(&ncdrop2);
551 if (vprele1)
552 vrele(vprele1);
553 if (vprele2)
554 vrele(vprele2);
556 return(0);
560 * Unmount a file system.
562 * Note: unmount takes a path to the vnode mounted on as argument,
563 * not special file (as before).
565 * umount_args(char *path, int flags)
567 * MPALMOSTSAFE
570 sys_unmount(struct unmount_args *uap)
572 struct thread *td = curthread;
573 struct proc *p __debugvar = td->td_proc;
574 struct mount *mp = NULL;
575 struct nlookupdata nd;
576 int error;
578 KKASSERT(p);
579 get_mplock();
580 if (td->td_ucred->cr_prison != NULL) {
581 error = EPERM;
582 goto done;
584 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
585 goto done;
587 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
588 if (error == 0)
589 error = nlookup(&nd);
590 if (error)
591 goto out;
593 mp = nd.nl_nch.mount;
596 * Only root, or the user that did the original mount is
597 * permitted to unmount this filesystem.
599 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
600 (error = priv_check(td, PRIV_ROOT)))
601 goto out;
604 * Don't allow unmounting the root file system.
606 if (mp->mnt_flag & MNT_ROOTFS) {
607 error = EINVAL;
608 goto out;
612 * Must be the root of the filesystem
614 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
615 error = EINVAL;
616 goto out;
619 out:
620 nlookup_done(&nd);
621 if (error == 0)
622 error = dounmount(mp, uap->flags);
623 done:
624 rel_mplock();
625 return (error);
629 * Do the actual file system unmount.
631 static int
632 dounmount_interlock(struct mount *mp)
634 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
635 return (EBUSY);
636 mp->mnt_kern_flag |= MNTK_UNMOUNT;
637 return(0);
640 static int
641 unmount_allproc_cb(struct proc *p, void *arg)
643 struct mount *mp;
645 if (p->p_textnch.ncp == NULL)
646 return 0;
648 mp = (struct mount *)arg;
649 if (p->p_textnch.mount == mp)
650 cache_drop(&p->p_textnch);
652 return 0;
656 dounmount(struct mount *mp, int flags)
658 struct namecache *ncp;
659 struct nchandle nch;
660 struct vnode *vp;
661 int error;
662 int async_flag;
663 int lflags;
664 int freeok = 1;
666 lwkt_gettoken(&mntvnode_token);
668 * Exclusive access for unmounting purposes
670 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
671 goto out;
674 * Allow filesystems to detect that a forced unmount is in progress.
676 if (flags & MNT_FORCE)
677 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
678 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
679 error = lockmgr(&mp->mnt_lock, lflags);
680 if (error) {
681 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
682 if (mp->mnt_kern_flag & MNTK_MWAIT)
683 wakeup(mp);
684 goto out;
687 if (mp->mnt_flag & MNT_EXPUBLIC)
688 vfs_setpublicfs(NULL, NULL, NULL);
690 vfs_msync(mp, MNT_WAIT);
691 async_flag = mp->mnt_flag & MNT_ASYNC;
692 mp->mnt_flag &=~ MNT_ASYNC;
695 * If this filesystem isn't aliasing other filesystems,
696 * try to invalidate any remaining namecache entries and
697 * check the count afterwords.
699 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
700 cache_lock(&mp->mnt_ncmountpt);
701 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
702 cache_unlock(&mp->mnt_ncmountpt);
704 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
705 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
706 allproc_scan(&unmount_allproc_cb, mp);
709 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
710 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
712 if ((flags & MNT_FORCE) == 0) {
713 error = EBUSY;
714 mount_warning(mp, "Cannot unmount: "
715 "%d namecache "
716 "references still "
717 "present",
718 ncp->nc_refs - 1);
719 } else {
720 mount_warning(mp, "Forced unmount: "
721 "%d namecache "
722 "references still "
723 "present",
724 ncp->nc_refs - 1);
725 freeok = 0;
731 * nchandle records ref the mount structure. Expect a count of 1
732 * (our mount->mnt_ncmountpt).
734 if (mp->mnt_refs != 1) {
735 if ((flags & MNT_FORCE) == 0) {
736 mount_warning(mp, "Cannot unmount: "
737 "%d process references still "
738 "present", mp->mnt_refs);
739 error = EBUSY;
740 } else {
741 mount_warning(mp, "Forced unmount: "
742 "%d process references still "
743 "present", mp->mnt_refs);
744 freeok = 0;
749 * Decomission our special mnt_syncer vnode. This also stops
750 * the vnlru code. If we are unable to unmount we recommission
751 * the vnode.
753 if (error == 0) {
754 if ((vp = mp->mnt_syncer) != NULL) {
755 mp->mnt_syncer = NULL;
756 vrele(vp);
758 if (((mp->mnt_flag & MNT_RDONLY) ||
759 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
760 (flags & MNT_FORCE)) {
761 error = VFS_UNMOUNT(mp, flags);
764 if (error) {
765 if (mp->mnt_syncer == NULL)
766 vfs_allocate_syncvnode(mp);
767 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
768 mp->mnt_flag |= async_flag;
769 lockmgr(&mp->mnt_lock, LK_RELEASE);
770 if (mp->mnt_kern_flag & MNTK_MWAIT)
771 wakeup(mp);
772 goto out;
775 * Clean up any journals still associated with the mount after
776 * filesystem activity has ceased.
778 journal_remove_all_journals(mp,
779 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
781 mountlist_remove(mp);
784 * Remove any installed vnode ops here so the individual VFSs don't
785 * have to.
787 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
788 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
789 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
790 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
791 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
793 if (mp->mnt_ncmountpt.ncp != NULL) {
794 nch = mp->mnt_ncmountpt;
795 cache_zero(&mp->mnt_ncmountpt);
796 cache_clrmountpt(&nch);
797 cache_drop(&nch);
799 if (mp->mnt_ncmounton.ncp != NULL) {
800 nch = mp->mnt_ncmounton;
801 cache_zero(&mp->mnt_ncmounton);
802 cache_clrmountpt(&nch);
803 cache_drop(&nch);
806 mp->mnt_vfc->vfc_refcount--;
807 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
808 panic("unmount: dangling vnode");
809 lockmgr(&mp->mnt_lock, LK_RELEASE);
810 if (mp->mnt_kern_flag & MNTK_MWAIT)
811 wakeup(mp);
812 if (freeok)
813 kfree(mp, M_MOUNT);
814 error = 0;
815 out:
816 lwkt_reltoken(&mntvnode_token);
817 return (error);
820 static
821 void
822 mount_warning(struct mount *mp, const char *ctl, ...)
824 char *ptr;
825 char *buf;
826 __va_list va;
828 __va_start(va, ctl);
829 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
830 &ptr, &buf, 0) == 0) {
831 kprintf("unmount(%s): ", ptr);
832 kvprintf(ctl, va);
833 kprintf("\n");
834 kfree(buf, M_TEMP);
835 } else {
836 kprintf("unmount(%p", mp);
837 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
838 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
839 kprintf("): ");
840 kvprintf(ctl, va);
841 kprintf("\n");
843 __va_end(va);
847 * Shim cache_fullpath() to handle the case where a process is chrooted into
848 * a subdirectory of a mount. In this case if the root mount matches the
849 * process root directory's mount we have to specify the process's root
850 * directory instead of the mount point, because the mount point might
851 * be above the root directory.
853 static
855 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
857 struct nchandle *nch;
859 if (p && p->p_fd->fd_nrdir.mount == mp)
860 nch = &p->p_fd->fd_nrdir;
861 else
862 nch = &mp->mnt_ncmountpt;
863 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
867 * Sync each mounted filesystem.
870 #ifdef DEBUG
871 static int syncprt = 0;
872 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
873 #endif /* DEBUG */
875 static int sync_callback(struct mount *mp, void *data);
878 sys_sync(struct sync_args *uap)
880 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
881 #ifdef DEBUG
883 * print out buffer pool stat information on each sync() call.
885 if (syncprt)
886 vfs_bufstats();
887 #endif /* DEBUG */
888 return (0);
891 static
893 sync_callback(struct mount *mp, void *data __unused)
895 int asyncflag;
897 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
898 asyncflag = mp->mnt_flag & MNT_ASYNC;
899 mp->mnt_flag &= ~MNT_ASYNC;
900 vfs_msync(mp, MNT_NOWAIT);
901 VFS_SYNC(mp, MNT_NOWAIT);
902 mp->mnt_flag |= asyncflag;
904 return(0);
907 /* XXX PRISON: could be per prison flag */
908 static int prison_quotas;
909 #if 0
910 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
911 #endif
914 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
916 * Change filesystem quotas.
918 * MPALMOSTSAFE
921 sys_quotactl(struct quotactl_args *uap)
923 struct nlookupdata nd;
924 struct thread *td;
925 struct mount *mp;
926 int error;
928 get_mplock();
929 td = curthread;
930 if (td->td_ucred->cr_prison && !prison_quotas) {
931 error = EPERM;
932 goto done;
935 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
936 if (error == 0)
937 error = nlookup(&nd);
938 if (error == 0) {
939 mp = nd.nl_nch.mount;
940 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
941 uap->arg, nd.nl_cred);
943 nlookup_done(&nd);
944 done:
945 rel_mplock();
946 return (error);
950 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
951 * void *buf, int buflen)
953 * This function operates on a mount point and executes the specified
954 * operation using the specified control data, and possibly returns data.
956 * The actual number of bytes stored in the result buffer is returned, 0
957 * if none, otherwise an error is returned.
959 * MPALMOSTSAFE
962 sys_mountctl(struct mountctl_args *uap)
964 struct thread *td = curthread;
965 struct proc *p = td->td_proc;
966 struct file *fp;
967 void *ctl = NULL;
968 void *buf = NULL;
969 char *path = NULL;
970 int error;
973 * Sanity and permissions checks. We must be root.
975 KKASSERT(p);
976 if (td->td_ucred->cr_prison != NULL)
977 return (EPERM);
978 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
979 (error = priv_check(td, PRIV_ROOT)) != 0)
980 return (error);
983 * Argument length checks
985 if (uap->ctllen < 0 || uap->ctllen > 1024)
986 return (EINVAL);
987 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
988 return (EINVAL);
989 if (uap->path == NULL)
990 return (EINVAL);
993 * Allocate the necessary buffers and copyin data
995 path = objcache_get(namei_oc, M_WAITOK);
996 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
997 if (error)
998 goto done;
1000 if (uap->ctllen) {
1001 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1002 error = copyin(uap->ctl, ctl, uap->ctllen);
1003 if (error)
1004 goto done;
1006 if (uap->buflen)
1007 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1010 * Validate the descriptor
1012 if (uap->fd >= 0) {
1013 fp = holdfp(p->p_fd, uap->fd, -1);
1014 if (fp == NULL) {
1015 error = EBADF;
1016 goto done;
1018 } else {
1019 fp = NULL;
1023 * Execute the internal kernel function and clean up.
1025 get_mplock();
1026 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1027 rel_mplock();
1028 if (fp)
1029 fdrop(fp);
1030 if (error == 0 && uap->sysmsg_result > 0)
1031 error = copyout(buf, uap->buf, uap->sysmsg_result);
1032 done:
1033 if (path)
1034 objcache_put(namei_oc, path);
1035 if (ctl)
1036 kfree(ctl, M_TEMP);
1037 if (buf)
1038 kfree(buf, M_TEMP);
1039 return (error);
1043 * Execute a mount control operation by resolving the path to a mount point
1044 * and calling vop_mountctl().
1046 * Use the mount point from the nch instead of the vnode so nullfs mounts
1047 * can properly spike the VOP.
1050 kern_mountctl(const char *path, int op, struct file *fp,
1051 const void *ctl, int ctllen,
1052 void *buf, int buflen, int *res)
1054 struct vnode *vp;
1055 struct mount *mp;
1056 struct nlookupdata nd;
1057 int error;
1059 *res = 0;
1060 vp = NULL;
1061 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1062 if (error == 0)
1063 error = nlookup(&nd);
1064 if (error == 0)
1065 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1066 mp = nd.nl_nch.mount;
1067 nlookup_done(&nd);
1068 if (error)
1069 return (error);
1070 vn_unlock(vp);
1073 * Must be the root of the filesystem
1075 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1076 vrele(vp);
1077 return (EINVAL);
1079 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1080 buf, buflen, res);
1081 vrele(vp);
1082 return (error);
1086 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1088 struct thread *td = curthread;
1089 struct proc *p = td->td_proc;
1090 struct mount *mp;
1091 struct statfs *sp;
1092 char *fullpath, *freepath;
1093 int error;
1095 if ((error = nlookup(nd)) != 0)
1096 return (error);
1097 mp = nd->nl_nch.mount;
1098 sp = &mp->mnt_stat;
1099 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1100 return (error);
1102 error = mount_path(p, mp, &fullpath, &freepath);
1103 if (error)
1104 return(error);
1105 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1106 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1107 kfree(freepath, M_TEMP);
1109 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1110 bcopy(sp, buf, sizeof(*buf));
1111 /* Only root should have access to the fsid's. */
1112 if (priv_check(td, PRIV_ROOT))
1113 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1114 return (0);
1118 * statfs_args(char *path, struct statfs *buf)
1120 * Get filesystem statistics.
1123 sys_statfs(struct statfs_args *uap)
1125 struct nlookupdata nd;
1126 struct statfs buf;
1127 int error;
1129 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1130 if (error == 0)
1131 error = kern_statfs(&nd, &buf);
1132 nlookup_done(&nd);
1133 if (error == 0)
1134 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1135 return (error);
1139 kern_fstatfs(int fd, struct statfs *buf)
1141 struct thread *td = curthread;
1142 struct proc *p = td->td_proc;
1143 struct file *fp;
1144 struct mount *mp;
1145 struct statfs *sp;
1146 char *fullpath, *freepath;
1147 int error;
1149 KKASSERT(p);
1150 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1151 return (error);
1154 * Try to use mount info from any overlays rather than the
1155 * mount info for the underlying vnode, otherwise we will
1156 * fail when operating on null-mounted paths inside a chroot.
1158 if ((mp = fp->f_nchandle.mount) == NULL)
1159 mp = ((struct vnode *)fp->f_data)->v_mount;
1160 if (mp == NULL) {
1161 error = EBADF;
1162 goto done;
1164 if (fp->f_cred == NULL) {
1165 error = EINVAL;
1166 goto done;
1168 sp = &mp->mnt_stat;
1169 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1170 goto done;
1172 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1173 goto done;
1174 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1175 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1176 kfree(freepath, M_TEMP);
1178 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1179 bcopy(sp, buf, sizeof(*buf));
1181 /* Only root should have access to the fsid's. */
1182 if (priv_check(td, PRIV_ROOT))
1183 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1184 error = 0;
1185 done:
1186 fdrop(fp);
1187 return (error);
1191 * fstatfs_args(int fd, struct statfs *buf)
1193 * Get filesystem statistics.
1196 sys_fstatfs(struct fstatfs_args *uap)
1198 struct statfs buf;
1199 int error;
1201 error = kern_fstatfs(uap->fd, &buf);
1203 if (error == 0)
1204 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1205 return (error);
1209 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1211 struct mount *mp;
1212 struct statvfs *sp;
1213 int error;
1215 if ((error = nlookup(nd)) != 0)
1216 return (error);
1217 mp = nd->nl_nch.mount;
1218 sp = &mp->mnt_vstat;
1219 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1220 return (error);
1222 sp->f_flag = 0;
1223 if (mp->mnt_flag & MNT_RDONLY)
1224 sp->f_flag |= ST_RDONLY;
1225 if (mp->mnt_flag & MNT_NOSUID)
1226 sp->f_flag |= ST_NOSUID;
1227 bcopy(sp, buf, sizeof(*buf));
1228 return (0);
1232 * statfs_args(char *path, struct statfs *buf)
1234 * Get filesystem statistics.
1237 sys_statvfs(struct statvfs_args *uap)
1239 struct nlookupdata nd;
1240 struct statvfs buf;
1241 int error;
1243 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1244 if (error == 0)
1245 error = kern_statvfs(&nd, &buf);
1246 nlookup_done(&nd);
1247 if (error == 0)
1248 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1249 return (error);
1253 kern_fstatvfs(int fd, struct statvfs *buf)
1255 struct thread *td = curthread;
1256 struct proc *p = td->td_proc;
1257 struct file *fp;
1258 struct mount *mp;
1259 struct statvfs *sp;
1260 int error;
1262 KKASSERT(p);
1263 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1264 return (error);
1265 if ((mp = fp->f_nchandle.mount) == NULL)
1266 mp = ((struct vnode *)fp->f_data)->v_mount;
1267 if (mp == NULL) {
1268 error = EBADF;
1269 goto done;
1271 if (fp->f_cred == NULL) {
1272 error = EINVAL;
1273 goto done;
1275 sp = &mp->mnt_vstat;
1276 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1277 goto done;
1279 sp->f_flag = 0;
1280 if (mp->mnt_flag & MNT_RDONLY)
1281 sp->f_flag |= ST_RDONLY;
1282 if (mp->mnt_flag & MNT_NOSUID)
1283 sp->f_flag |= ST_NOSUID;
1285 bcopy(sp, buf, sizeof(*buf));
1286 error = 0;
1287 done:
1288 fdrop(fp);
1289 return (error);
1293 * fstatfs_args(int fd, struct statfs *buf)
1295 * Get filesystem statistics.
1298 sys_fstatvfs(struct fstatvfs_args *uap)
1300 struct statvfs buf;
1301 int error;
1303 error = kern_fstatvfs(uap->fd, &buf);
1305 if (error == 0)
1306 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1307 return (error);
1311 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1313 * Get statistics on all filesystems.
1316 struct getfsstat_info {
1317 struct statfs *sfsp;
1318 long count;
1319 long maxcount;
1320 int error;
1321 int flags;
1322 struct thread *td;
1325 static int getfsstat_callback(struct mount *, void *);
1328 sys_getfsstat(struct getfsstat_args *uap)
1330 struct thread *td = curthread;
1331 struct getfsstat_info info;
1333 bzero(&info, sizeof(info));
1335 info.maxcount = uap->bufsize / sizeof(struct statfs);
1336 info.sfsp = uap->buf;
1337 info.count = 0;
1338 info.flags = uap->flags;
1339 info.td = td;
1341 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1342 if (info.sfsp && info.count > info.maxcount)
1343 uap->sysmsg_result = info.maxcount;
1344 else
1345 uap->sysmsg_result = info.count;
1346 return (info.error);
1349 static int
1350 getfsstat_callback(struct mount *mp, void *data)
1352 struct getfsstat_info *info = data;
1353 struct statfs *sp;
1354 char *freepath;
1355 char *fullpath;
1356 int error;
1358 if (info->sfsp && info->count < info->maxcount) {
1359 if (info->td->td_proc &&
1360 !chroot_visible_mnt(mp, info->td->td_proc)) {
1361 return(0);
1363 sp = &mp->mnt_stat;
1366 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1367 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1368 * overrides MNT_WAIT.
1370 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1371 (info->flags & MNT_WAIT)) &&
1372 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1373 return(0);
1375 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1377 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1378 if (error) {
1379 info->error = error;
1380 return(-1);
1382 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1383 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1384 kfree(freepath, M_TEMP);
1386 error = copyout(sp, info->sfsp, sizeof(*sp));
1387 if (error) {
1388 info->error = error;
1389 return (-1);
1391 ++info->sfsp;
1393 info->count++;
1394 return(0);
1398 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1399 long bufsize, int flags)
1401 * Get statistics on all filesystems.
1404 struct getvfsstat_info {
1405 struct statfs *sfsp;
1406 struct statvfs *vsfsp;
1407 long count;
1408 long maxcount;
1409 int error;
1410 int flags;
1411 struct thread *td;
1414 static int getvfsstat_callback(struct mount *, void *);
1417 sys_getvfsstat(struct getvfsstat_args *uap)
1419 struct thread *td = curthread;
1420 struct getvfsstat_info info;
1422 bzero(&info, sizeof(info));
1424 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1425 info.sfsp = uap->buf;
1426 info.vsfsp = uap->vbuf;
1427 info.count = 0;
1428 info.flags = uap->flags;
1429 info.td = td;
1431 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1432 if (info.vsfsp && info.count > info.maxcount)
1433 uap->sysmsg_result = info.maxcount;
1434 else
1435 uap->sysmsg_result = info.count;
1436 return (info.error);
1439 static int
1440 getvfsstat_callback(struct mount *mp, void *data)
1442 struct getvfsstat_info *info = data;
1443 struct statfs *sp;
1444 struct statvfs *vsp;
1445 char *freepath;
1446 char *fullpath;
1447 int error;
1449 if (info->vsfsp && info->count < info->maxcount) {
1450 if (info->td->td_proc &&
1451 !chroot_visible_mnt(mp, info->td->td_proc)) {
1452 return(0);
1454 sp = &mp->mnt_stat;
1455 vsp = &mp->mnt_vstat;
1458 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1459 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1460 * overrides MNT_WAIT.
1462 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1463 (info->flags & MNT_WAIT)) &&
1464 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1465 return(0);
1467 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1469 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1470 (info->flags & MNT_WAIT)) &&
1471 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1472 return(0);
1474 vsp->f_flag = 0;
1475 if (mp->mnt_flag & MNT_RDONLY)
1476 vsp->f_flag |= ST_RDONLY;
1477 if (mp->mnt_flag & MNT_NOSUID)
1478 vsp->f_flag |= ST_NOSUID;
1480 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1481 if (error) {
1482 info->error = error;
1483 return(-1);
1485 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1486 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1487 kfree(freepath, M_TEMP);
1489 error = copyout(sp, info->sfsp, sizeof(*sp));
1490 if (error == 0)
1491 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1492 if (error) {
1493 info->error = error;
1494 return (-1);
1496 ++info->sfsp;
1497 ++info->vsfsp;
1499 info->count++;
1500 return(0);
1505 * fchdir_args(int fd)
1507 * Change current working directory to a given file descriptor.
1510 sys_fchdir(struct fchdir_args *uap)
1512 struct thread *td = curthread;
1513 struct proc *p = td->td_proc;
1514 struct filedesc *fdp = p->p_fd;
1515 struct vnode *vp, *ovp;
1516 struct mount *mp;
1517 struct file *fp;
1518 struct nchandle nch, onch, tnch;
1519 int error;
1521 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1522 return (error);
1523 lwkt_gettoken(&p->p_token);
1524 vp = (struct vnode *)fp->f_data;
1525 vref(vp);
1526 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1527 if (fp->f_nchandle.ncp == NULL)
1528 error = ENOTDIR;
1529 else
1530 error = checkvp_chdir(vp, td);
1531 if (error) {
1532 vput(vp);
1533 goto done;
1535 cache_copy(&fp->f_nchandle, &nch);
1538 * If the ncp has become a mount point, traverse through
1539 * the mount point.
1542 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1543 (mp = cache_findmount(&nch)) != NULL
1545 error = nlookup_mp(mp, &tnch);
1546 if (error == 0) {
1547 cache_unlock(&tnch); /* leave ref intact */
1548 vput(vp);
1549 vp = tnch.ncp->nc_vp;
1550 error = vget(vp, LK_SHARED);
1551 KKASSERT(error == 0);
1552 cache_drop(&nch);
1553 nch = tnch;
1555 cache_dropmount(mp);
1557 if (error == 0) {
1558 ovp = fdp->fd_cdir;
1559 onch = fdp->fd_ncdir;
1560 vn_unlock(vp); /* leave ref intact */
1561 fdp->fd_cdir = vp;
1562 fdp->fd_ncdir = nch;
1563 cache_drop(&onch);
1564 vrele(ovp);
1565 } else {
1566 cache_drop(&nch);
1567 vput(vp);
1569 fdrop(fp);
1570 done:
1571 lwkt_reltoken(&p->p_token);
1572 return (error);
1576 kern_chdir(struct nlookupdata *nd)
1578 struct thread *td = curthread;
1579 struct proc *p = td->td_proc;
1580 struct filedesc *fdp = p->p_fd;
1581 struct vnode *vp, *ovp;
1582 struct nchandle onch;
1583 int error;
1585 if ((error = nlookup(nd)) != 0)
1586 return (error);
1587 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1588 return (ENOENT);
1589 if ((error = vget(vp, LK_SHARED)) != 0)
1590 return (error);
1592 lwkt_gettoken(&p->p_token);
1593 error = checkvp_chdir(vp, td);
1594 vn_unlock(vp);
1595 if (error == 0) {
1596 ovp = fdp->fd_cdir;
1597 onch = fdp->fd_ncdir;
1598 cache_unlock(&nd->nl_nch); /* leave reference intact */
1599 fdp->fd_ncdir = nd->nl_nch;
1600 fdp->fd_cdir = vp;
1601 cache_drop(&onch);
1602 vrele(ovp);
1603 cache_zero(&nd->nl_nch);
1604 } else {
1605 vrele(vp);
1607 lwkt_reltoken(&p->p_token);
1608 return (error);
1612 * chdir_args(char *path)
1614 * Change current working directory (``.'').
1617 sys_chdir(struct chdir_args *uap)
1619 struct nlookupdata nd;
1620 int error;
1622 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1623 if (error == 0)
1624 error = kern_chdir(&nd);
1625 nlookup_done(&nd);
1626 return (error);
1630 * Helper function for raised chroot(2) security function: Refuse if
1631 * any filedescriptors are open directories.
1633 static int
1634 chroot_refuse_vdir_fds(struct filedesc *fdp)
1636 struct vnode *vp;
1637 struct file *fp;
1638 int error;
1639 int fd;
1641 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1642 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1643 continue;
1644 vp = (struct vnode *)fp->f_data;
1645 if (vp->v_type != VDIR) {
1646 fdrop(fp);
1647 continue;
1649 fdrop(fp);
1650 return(EPERM);
1652 return (0);
1656 * This sysctl determines if we will allow a process to chroot(2) if it
1657 * has a directory open:
1658 * 0: disallowed for all processes.
1659 * 1: allowed for processes that were not already chroot(2)'ed.
1660 * 2: allowed for all processes.
1663 static int chroot_allow_open_directories = 1;
1665 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1666 &chroot_allow_open_directories, 0, "");
1669 * chroot to the specified namecache entry. We obtain the vp from the
1670 * namecache data. The passed ncp must be locked and referenced and will
1671 * remain locked and referenced on return.
1674 kern_chroot(struct nchandle *nch)
1676 struct thread *td = curthread;
1677 struct proc *p = td->td_proc;
1678 struct filedesc *fdp = p->p_fd;
1679 struct vnode *vp;
1680 int error;
1683 * Only privileged user can chroot
1685 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1686 if (error)
1687 return (error);
1690 * Disallow open directory descriptors (fchdir() breakouts).
1692 if (chroot_allow_open_directories == 0 ||
1693 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1694 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1695 return (error);
1697 if ((vp = nch->ncp->nc_vp) == NULL)
1698 return (ENOENT);
1700 if ((error = vget(vp, LK_SHARED)) != 0)
1701 return (error);
1704 * Check the validity of vp as a directory to change to and
1705 * associate it with rdir/jdir.
1707 error = checkvp_chdir(vp, td);
1708 vn_unlock(vp); /* leave reference intact */
1709 if (error == 0) {
1710 vrele(fdp->fd_rdir);
1711 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1712 cache_drop(&fdp->fd_nrdir);
1713 cache_copy(nch, &fdp->fd_nrdir);
1714 if (fdp->fd_jdir == NULL) {
1715 fdp->fd_jdir = vp;
1716 vref(fdp->fd_jdir);
1717 cache_copy(nch, &fdp->fd_njdir);
1719 } else {
1720 vrele(vp);
1722 return (error);
1726 * chroot_args(char *path)
1728 * Change notion of root (``/'') directory.
1731 sys_chroot(struct chroot_args *uap)
1733 struct thread *td __debugvar = curthread;
1734 struct nlookupdata nd;
1735 int error;
1737 KKASSERT(td->td_proc);
1738 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1739 if (error == 0) {
1740 nd.nl_flags |= NLC_EXEC;
1741 error = nlookup(&nd);
1742 if (error == 0)
1743 error = kern_chroot(&nd.nl_nch);
1745 nlookup_done(&nd);
1746 return(error);
1750 sys_chroot_kernel(struct chroot_kernel_args *uap)
1752 struct thread *td = curthread;
1753 struct nlookupdata nd;
1754 struct nchandle *nch;
1755 struct vnode *vp;
1756 int error;
1758 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1759 if (error)
1760 goto error_nond;
1762 error = nlookup(&nd);
1763 if (error)
1764 goto error_out;
1766 nch = &nd.nl_nch;
1768 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1769 if (error)
1770 goto error_out;
1772 if ((vp = nch->ncp->nc_vp) == NULL) {
1773 error = ENOENT;
1774 goto error_out;
1777 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1778 goto error_out;
1780 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1781 get_mplock();
1782 vfs_cache_setroot(vp, cache_hold(nch));
1783 rel_mplock();
1785 error_out:
1786 nlookup_done(&nd);
1787 error_nond:
1788 return(error);
1792 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1793 * determine whether it is legal to chdir to the vnode. The vnode's state
1794 * is not changed by this call.
1797 checkvp_chdir(struct vnode *vp, struct thread *td)
1799 int error;
1801 if (vp->v_type != VDIR)
1802 error = ENOTDIR;
1803 else
1804 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1805 return (error);
1809 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1811 struct thread *td = curthread;
1812 struct proc *p = td->td_proc;
1813 struct lwp *lp = td->td_lwp;
1814 struct filedesc *fdp = p->p_fd;
1815 int cmode, flags;
1816 struct file *nfp;
1817 struct file *fp;
1818 struct vnode *vp;
1819 int type, indx, error = 0;
1820 struct flock lf;
1822 if ((oflags & O_ACCMODE) == O_ACCMODE)
1823 return (EINVAL);
1824 flags = FFLAGS(oflags);
1825 error = falloc(lp, &nfp, NULL);
1826 if (error)
1827 return (error);
1828 fp = nfp;
1829 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1832 * XXX p_dupfd is a real mess. It allows a device to return a
1833 * file descriptor to be duplicated rather then doing the open
1834 * itself.
1836 lp->lwp_dupfd = -1;
1839 * Call vn_open() to do the lookup and assign the vnode to the
1840 * file pointer. vn_open() does not change the ref count on fp
1841 * and the vnode, on success, will be inherited by the file pointer
1842 * and unlocked.
1844 nd->nl_flags |= NLC_LOCKVP;
1845 error = vn_open(nd, fp, flags, cmode);
1846 nlookup_done(nd);
1847 if (error) {
1849 * handle special fdopen() case. bleh. dupfdopen() is
1850 * responsible for dropping the old contents of ofiles[indx]
1851 * if it succeeds.
1853 * Note that fsetfd() will add a ref to fp which represents
1854 * the fd_files[] assignment. We must still drop our
1855 * reference.
1857 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1858 if (fdalloc(p, 0, &indx) == 0) {
1859 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1860 if (error == 0) {
1861 *res = indx;
1862 fdrop(fp); /* our ref */
1863 return (0);
1865 fsetfd(fdp, NULL, indx);
1868 fdrop(fp); /* our ref */
1869 if (error == ERESTART)
1870 error = EINTR;
1871 return (error);
1875 * ref the vnode for ourselves so it can't be ripped out from under
1876 * is. XXX need an ND flag to request that the vnode be returned
1877 * anyway.
1879 * Reserve a file descriptor but do not assign it until the open
1880 * succeeds.
1882 vp = (struct vnode *)fp->f_data;
1883 vref(vp);
1884 if ((error = fdalloc(p, 0, &indx)) != 0) {
1885 fdrop(fp);
1886 vrele(vp);
1887 return (error);
1891 * If no error occurs the vp will have been assigned to the file
1892 * pointer.
1894 lp->lwp_dupfd = 0;
1896 if (flags & (O_EXLOCK | O_SHLOCK)) {
1897 lf.l_whence = SEEK_SET;
1898 lf.l_start = 0;
1899 lf.l_len = 0;
1900 if (flags & O_EXLOCK)
1901 lf.l_type = F_WRLCK;
1902 else
1903 lf.l_type = F_RDLCK;
1904 if (flags & FNONBLOCK)
1905 type = 0;
1906 else
1907 type = F_WAIT;
1909 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1911 * lock request failed. Clean up the reserved
1912 * descriptor.
1914 vrele(vp);
1915 fsetfd(fdp, NULL, indx);
1916 fdrop(fp);
1917 return (error);
1919 fp->f_flag |= FHASLOCK;
1921 #if 0
1923 * Assert that all regular file vnodes were created with a object.
1925 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1926 ("open: regular file has no backing object after vn_open"));
1927 #endif
1929 vrele(vp);
1932 * release our private reference, leaving the one associated with the
1933 * descriptor table intact.
1935 fsetfd(fdp, fp, indx);
1936 fdrop(fp);
1937 *res = indx;
1938 if (oflags & O_CLOEXEC)
1939 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1940 return (error);
1944 * open_args(char *path, int flags, int mode)
1946 * Check permissions, allocate an open file structure,
1947 * and call the device open routine if any.
1950 sys_open(struct open_args *uap)
1952 struct nlookupdata nd;
1953 int error;
1955 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1956 if (error == 0) {
1957 error = kern_open(&nd, uap->flags,
1958 uap->mode, &uap->sysmsg_result);
1960 nlookup_done(&nd);
1961 return (error);
1965 * openat_args(int fd, char *path, int flags, int mode)
1968 sys_openat(struct openat_args *uap)
1970 struct nlookupdata nd;
1971 int error;
1972 struct file *fp;
1974 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1975 if (error == 0) {
1976 error = kern_open(&nd, uap->flags, uap->mode,
1977 &uap->sysmsg_result);
1979 nlookup_done_at(&nd, fp);
1980 return (error);
1984 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1986 struct thread *td = curthread;
1987 struct proc *p = td->td_proc;
1988 struct vnode *vp;
1989 struct vattr vattr;
1990 int error;
1991 int whiteout = 0;
1993 KKASSERT(p);
1995 VATTR_NULL(&vattr);
1996 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1997 vattr.va_rmajor = rmajor;
1998 vattr.va_rminor = rminor;
2000 switch (mode & S_IFMT) {
2001 case S_IFMT: /* used by badsect to flag bad sectors */
2002 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2003 vattr.va_type = VBAD;
2004 break;
2005 case S_IFCHR:
2006 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2007 vattr.va_type = VCHR;
2008 break;
2009 case S_IFBLK:
2010 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2011 vattr.va_type = VBLK;
2012 break;
2013 case S_IFWHT:
2014 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2015 whiteout = 1;
2016 break;
2017 case S_IFDIR: /* special directories support for HAMMER */
2018 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2019 vattr.va_type = VDIR;
2020 break;
2021 default:
2022 error = EINVAL;
2023 break;
2026 if (error)
2027 return (error);
2029 bwillinode(1);
2030 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2031 if ((error = nlookup(nd)) != 0)
2032 return (error);
2033 if (nd->nl_nch.ncp->nc_vp)
2034 return (EEXIST);
2035 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2036 return (error);
2038 if (whiteout) {
2039 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2040 nd->nl_cred, NAMEI_CREATE);
2041 } else {
2042 vp = NULL;
2043 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2044 &vp, nd->nl_cred, &vattr);
2045 if (error == 0)
2046 vput(vp);
2048 return (error);
2052 * mknod_args(char *path, int mode, int dev)
2054 * Create a special file.
2057 sys_mknod(struct mknod_args *uap)
2059 struct nlookupdata nd;
2060 int error;
2062 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2063 if (error == 0) {
2064 error = kern_mknod(&nd, uap->mode,
2065 umajor(uap->dev), uminor(uap->dev));
2067 nlookup_done(&nd);
2068 return (error);
2072 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2074 * Create a special file. The path is relative to the directory associated
2075 * with fd.
2078 sys_mknodat(struct mknodat_args *uap)
2080 struct nlookupdata nd;
2081 struct file *fp;
2082 int error;
2084 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2085 if (error == 0) {
2086 error = kern_mknod(&nd, uap->mode,
2087 umajor(uap->dev), uminor(uap->dev));
2089 nlookup_done_at(&nd, fp);
2090 return (error);
2094 kern_mkfifo(struct nlookupdata *nd, int mode)
2096 struct thread *td = curthread;
2097 struct proc *p = td->td_proc;
2098 struct vattr vattr;
2099 struct vnode *vp;
2100 int error;
2102 bwillinode(1);
2104 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2105 if ((error = nlookup(nd)) != 0)
2106 return (error);
2107 if (nd->nl_nch.ncp->nc_vp)
2108 return (EEXIST);
2109 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2110 return (error);
2112 VATTR_NULL(&vattr);
2113 vattr.va_type = VFIFO;
2114 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2115 vp = NULL;
2116 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2117 if (error == 0)
2118 vput(vp);
2119 return (error);
2123 * mkfifo_args(char *path, int mode)
2125 * Create a named pipe.
2128 sys_mkfifo(struct mkfifo_args *uap)
2130 struct nlookupdata nd;
2131 int error;
2133 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2134 if (error == 0)
2135 error = kern_mkfifo(&nd, uap->mode);
2136 nlookup_done(&nd);
2137 return (error);
2141 * mkfifoat_args(int fd, char *path, mode_t mode)
2143 * Create a named pipe. The path is relative to the directory associated
2144 * with fd.
2147 sys_mkfifoat(struct mkfifoat_args *uap)
2149 struct nlookupdata nd;
2150 struct file *fp;
2151 int error;
2153 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2154 if (error == 0)
2155 error = kern_mkfifo(&nd, uap->mode);
2156 nlookup_done_at(&nd, fp);
2157 return (error);
2160 static int hardlink_check_uid = 0;
2161 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2162 &hardlink_check_uid, 0,
2163 "Unprivileged processes cannot create hard links to files owned by other "
2164 "users");
2165 static int hardlink_check_gid = 0;
2166 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2167 &hardlink_check_gid, 0,
2168 "Unprivileged processes cannot create hard links to files owned by other "
2169 "groups");
2171 static int
2172 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2174 struct vattr va;
2175 int error;
2178 * Shortcut if disabled
2180 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2181 return (0);
2184 * Privileged user can always hardlink
2186 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2187 return (0);
2190 * Otherwise only if the originating file is owned by the
2191 * same user or group. Note that any group is allowed if
2192 * the file is owned by the caller.
2194 error = VOP_GETATTR(vp, &va);
2195 if (error != 0)
2196 return (error);
2198 if (hardlink_check_uid) {
2199 if (cred->cr_uid != va.va_uid)
2200 return (EPERM);
2203 if (hardlink_check_gid) {
2204 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2205 return (EPERM);
2208 return (0);
2212 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2214 struct thread *td = curthread;
2215 struct vnode *vp;
2216 int error;
2219 * Lookup the source and obtained a locked vnode.
2221 * You may only hardlink a file which you have write permission
2222 * on or which you own.
2224 * XXX relookup on vget failure / race ?
2226 bwillinode(1);
2227 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2228 if ((error = nlookup(nd)) != 0)
2229 return (error);
2230 vp = nd->nl_nch.ncp->nc_vp;
2231 KKASSERT(vp != NULL);
2232 if (vp->v_type == VDIR)
2233 return (EPERM); /* POSIX */
2234 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2235 return (error);
2236 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2237 return (error);
2240 * Unlock the source so we can lookup the target without deadlocking
2241 * (XXX vp is locked already, possible other deadlock?). The target
2242 * must not exist.
2244 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2245 nd->nl_flags &= ~NLC_NCPISLOCKED;
2246 cache_unlock(&nd->nl_nch);
2247 vn_unlock(vp);
2249 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2250 if ((error = nlookup(linknd)) != 0) {
2251 vrele(vp);
2252 return (error);
2254 if (linknd->nl_nch.ncp->nc_vp) {
2255 vrele(vp);
2256 return (EEXIST);
2258 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2259 vrele(vp);
2260 return (error);
2264 * Finally run the new API VOP.
2266 error = can_hardlink(vp, td, td->td_ucred);
2267 if (error == 0) {
2268 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2269 vp, linknd->nl_cred);
2271 vput(vp);
2272 return (error);
2276 * link_args(char *path, char *link)
2278 * Make a hard file link.
2281 sys_link(struct link_args *uap)
2283 struct nlookupdata nd, linknd;
2284 int error;
2286 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2287 if (error == 0) {
2288 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2289 if (error == 0)
2290 error = kern_link(&nd, &linknd);
2291 nlookup_done(&linknd);
2293 nlookup_done(&nd);
2294 return (error);
2298 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2300 * Make a hard file link. The path1 argument is relative to the directory
2301 * associated with fd1, and similarly the path2 argument is relative to
2302 * the directory associated with fd2.
2305 sys_linkat(struct linkat_args *uap)
2307 struct nlookupdata nd, linknd;
2308 struct file *fp1, *fp2;
2309 int error;
2311 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2312 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2313 if (error == 0) {
2314 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2315 uap->path2, UIO_USERSPACE, 0);
2316 if (error == 0)
2317 error = kern_link(&nd, &linknd);
2318 nlookup_done_at(&linknd, fp2);
2320 nlookup_done_at(&nd, fp1);
2321 return (error);
2325 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2327 struct vattr vattr;
2328 struct vnode *vp;
2329 struct vnode *dvp;
2330 int error;
2332 bwillinode(1);
2333 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2334 if ((error = nlookup(nd)) != 0)
2335 return (error);
2336 if (nd->nl_nch.ncp->nc_vp)
2337 return (EEXIST);
2338 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2339 return (error);
2340 dvp = nd->nl_dvp;
2341 VATTR_NULL(&vattr);
2342 vattr.va_mode = mode;
2343 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2344 if (error == 0)
2345 vput(vp);
2346 return (error);
2350 * symlink(char *path, char *link)
2352 * Make a symbolic link.
2355 sys_symlink(struct symlink_args *uap)
2357 struct thread *td = curthread;
2358 struct nlookupdata nd;
2359 char *path;
2360 int error;
2361 int mode;
2363 path = objcache_get(namei_oc, M_WAITOK);
2364 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2365 if (error == 0) {
2366 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2367 if (error == 0) {
2368 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2369 error = kern_symlink(&nd, path, mode);
2371 nlookup_done(&nd);
2373 objcache_put(namei_oc, path);
2374 return (error);
2378 * symlinkat_args(char *path1, int fd, char *path2)
2380 * Make a symbolic link. The path2 argument is relative to the directory
2381 * associated with fd.
2384 sys_symlinkat(struct symlinkat_args *uap)
2386 struct thread *td = curthread;
2387 struct nlookupdata nd;
2388 struct file *fp;
2389 char *path1;
2390 int error;
2391 int mode;
2393 path1 = objcache_get(namei_oc, M_WAITOK);
2394 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2395 if (error == 0) {
2396 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2397 UIO_USERSPACE, 0);
2398 if (error == 0) {
2399 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2400 error = kern_symlink(&nd, path1, mode);
2402 nlookup_done_at(&nd, fp);
2404 objcache_put(namei_oc, path1);
2405 return (error);
2409 * undelete_args(char *path)
2411 * Delete a whiteout from the filesystem.
2414 sys_undelete(struct undelete_args *uap)
2416 struct nlookupdata nd;
2417 int error;
2419 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2420 bwillinode(1);
2421 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2422 if (error == 0)
2423 error = nlookup(&nd);
2424 if (error == 0)
2425 error = ncp_writechk(&nd.nl_nch);
2426 if (error == 0) {
2427 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2428 NAMEI_DELETE);
2430 nlookup_done(&nd);
2431 return (error);
2435 kern_unlink(struct nlookupdata *nd)
2437 int error;
2439 bwillinode(1);
2440 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2441 if ((error = nlookup(nd)) != 0)
2442 return (error);
2443 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2444 return (error);
2445 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2446 return (error);
2450 * unlink_args(char *path)
2452 * Delete a name from the filesystem.
2455 sys_unlink(struct unlink_args *uap)
2457 struct nlookupdata nd;
2458 int error;
2460 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2461 if (error == 0)
2462 error = kern_unlink(&nd);
2463 nlookup_done(&nd);
2464 return (error);
2469 * unlinkat_args(int fd, char *path, int flags)
2471 * Delete the file or directory entry pointed to by fd/path.
2474 sys_unlinkat(struct unlinkat_args *uap)
2476 struct nlookupdata nd;
2477 struct file *fp;
2478 int error;
2480 if (uap->flags & ~AT_REMOVEDIR)
2481 return (EINVAL);
2483 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2484 if (error == 0) {
2485 if (uap->flags & AT_REMOVEDIR)
2486 error = kern_rmdir(&nd);
2487 else
2488 error = kern_unlink(&nd);
2490 nlookup_done_at(&nd, fp);
2491 return (error);
2495 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2497 struct thread *td = curthread;
2498 struct proc *p = td->td_proc;
2499 struct file *fp;
2500 struct vnode *vp;
2501 struct vattr vattr;
2502 off_t new_offset;
2503 int error;
2505 fp = holdfp(p->p_fd, fd, -1);
2506 if (fp == NULL)
2507 return (EBADF);
2508 if (fp->f_type != DTYPE_VNODE) {
2509 error = ESPIPE;
2510 goto done;
2512 vp = (struct vnode *)fp->f_data;
2514 switch (whence) {
2515 case L_INCR:
2516 spin_lock(&fp->f_spin);
2517 new_offset = fp->f_offset + offset;
2518 error = 0;
2519 break;
2520 case L_XTND:
2521 error = VOP_GETATTR(vp, &vattr);
2522 spin_lock(&fp->f_spin);
2523 new_offset = offset + vattr.va_size;
2524 break;
2525 case L_SET:
2526 new_offset = offset;
2527 error = 0;
2528 spin_lock(&fp->f_spin);
2529 break;
2530 default:
2531 new_offset = 0;
2532 error = EINVAL;
2533 spin_lock(&fp->f_spin);
2534 break;
2538 * Validate the seek position. Negative offsets are not allowed
2539 * for regular files or directories.
2541 * Normally we would also not want to allow negative offsets for
2542 * character and block-special devices. However kvm addresses
2543 * on 64 bit architectures might appear to be negative and must
2544 * be allowed.
2546 if (error == 0) {
2547 if (new_offset < 0 &&
2548 (vp->v_type == VREG || vp->v_type == VDIR)) {
2549 error = EINVAL;
2550 } else {
2551 fp->f_offset = new_offset;
2554 *res = fp->f_offset;
2555 spin_unlock(&fp->f_spin);
2556 done:
2557 fdrop(fp);
2558 return (error);
2562 * lseek_args(int fd, int pad, off_t offset, int whence)
2564 * Reposition read/write file offset.
2567 sys_lseek(struct lseek_args *uap)
2569 int error;
2571 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2572 &uap->sysmsg_offset);
2574 return (error);
2578 * Check if current process can access given file. amode is a bitmask of *_OK
2579 * access bits. flags is a bitmask of AT_* flags.
2582 kern_access(struct nlookupdata *nd, int amode, int flags)
2584 struct vnode *vp;
2585 int error, mode;
2587 if (flags & ~AT_EACCESS)
2588 return (EINVAL);
2589 if ((error = nlookup(nd)) != 0)
2590 return (error);
2591 retry:
2592 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2593 if (error)
2594 return (error);
2596 /* Flags == 0 means only check for existence. */
2597 if (amode) {
2598 mode = 0;
2599 if (amode & R_OK)
2600 mode |= VREAD;
2601 if (amode & W_OK)
2602 mode |= VWRITE;
2603 if (amode & X_OK)
2604 mode |= VEXEC;
2605 if ((mode & VWRITE) == 0 ||
2606 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2607 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2610 * If the file handle is stale we have to re-resolve the
2611 * entry. This is a hack at the moment.
2613 if (error == ESTALE) {
2614 vput(vp);
2615 cache_setunresolved(&nd->nl_nch);
2616 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2617 if (error == 0) {
2618 vp = NULL;
2619 goto retry;
2621 return(error);
2624 vput(vp);
2625 return (error);
2629 * access_args(char *path, int flags)
2631 * Check access permissions.
2634 sys_access(struct access_args *uap)
2636 struct nlookupdata nd;
2637 int error;
2639 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2640 if (error == 0)
2641 error = kern_access(&nd, uap->flags, 0);
2642 nlookup_done(&nd);
2643 return (error);
2648 * eaccess_args(char *path, int flags)
2650 * Check access permissions.
2653 sys_eaccess(struct eaccess_args *uap)
2655 struct nlookupdata nd;
2656 int error;
2658 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2659 if (error == 0)
2660 error = kern_access(&nd, uap->flags, AT_EACCESS);
2661 nlookup_done(&nd);
2662 return (error);
2667 * faccessat_args(int fd, char *path, int amode, int flags)
2669 * Check access permissions.
2672 sys_faccessat(struct faccessat_args *uap)
2674 struct nlookupdata nd;
2675 struct file *fp;
2676 int error;
2678 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2679 NLC_FOLLOW);
2680 if (error == 0)
2681 error = kern_access(&nd, uap->amode, uap->flags);
2682 nlookup_done_at(&nd, fp);
2683 return (error);
2688 kern_stat(struct nlookupdata *nd, struct stat *st)
2690 int error;
2691 struct vnode *vp;
2693 if ((error = nlookup(nd)) != 0)
2694 return (error);
2695 again:
2696 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2697 return (ENOENT);
2699 if ((error = vget(vp, LK_SHARED)) != 0)
2700 return (error);
2701 error = vn_stat(vp, st, nd->nl_cred);
2704 * If the file handle is stale we have to re-resolve the entry. This
2705 * is a hack at the moment.
2707 if (error == ESTALE) {
2708 vput(vp);
2709 cache_setunresolved(&nd->nl_nch);
2710 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2711 if (error == 0)
2712 goto again;
2713 } else {
2714 vput(vp);
2716 return (error);
2720 * stat_args(char *path, struct stat *ub)
2722 * Get file status; this version follows links.
2725 sys_stat(struct stat_args *uap)
2727 struct nlookupdata nd;
2728 struct stat st;
2729 int error;
2731 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2732 if (error == 0) {
2733 error = kern_stat(&nd, &st);
2734 if (error == 0)
2735 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2737 nlookup_done(&nd);
2738 return (error);
2742 * lstat_args(char *path, struct stat *ub)
2744 * Get file status; this version does not follow links.
2747 sys_lstat(struct lstat_args *uap)
2749 struct nlookupdata nd;
2750 struct stat st;
2751 int error;
2753 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2754 if (error == 0) {
2755 error = kern_stat(&nd, &st);
2756 if (error == 0)
2757 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2759 nlookup_done(&nd);
2760 return (error);
2764 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2766 * Get status of file pointed to by fd/path.
2769 sys_fstatat(struct fstatat_args *uap)
2771 struct nlookupdata nd;
2772 struct stat st;
2773 int error;
2774 int flags;
2775 struct file *fp;
2777 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2778 return (EINVAL);
2780 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2782 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2783 UIO_USERSPACE, flags);
2784 if (error == 0) {
2785 error = kern_stat(&nd, &st);
2786 if (error == 0)
2787 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2789 nlookup_done_at(&nd, fp);
2790 return (error);
2794 * pathconf_Args(char *path, int name)
2796 * Get configurable pathname variables.
2799 sys_pathconf(struct pathconf_args *uap)
2801 struct nlookupdata nd;
2802 struct vnode *vp;
2803 int error;
2805 vp = NULL;
2806 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2807 if (error == 0)
2808 error = nlookup(&nd);
2809 if (error == 0)
2810 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2811 nlookup_done(&nd);
2812 if (error == 0) {
2813 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2814 vput(vp);
2816 return (error);
2820 * XXX: daver
2821 * kern_readlink isn't properly split yet. There is a copyin burried
2822 * in VOP_READLINK().
2825 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2827 struct thread *td = curthread;
2828 struct vnode *vp;
2829 struct iovec aiov;
2830 struct uio auio;
2831 int error;
2833 if ((error = nlookup(nd)) != 0)
2834 return (error);
2835 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2836 if (error)
2837 return (error);
2838 if (vp->v_type != VLNK) {
2839 error = EINVAL;
2840 } else {
2841 aiov.iov_base = buf;
2842 aiov.iov_len = count;
2843 auio.uio_iov = &aiov;
2844 auio.uio_iovcnt = 1;
2845 auio.uio_offset = 0;
2846 auio.uio_rw = UIO_READ;
2847 auio.uio_segflg = UIO_USERSPACE;
2848 auio.uio_td = td;
2849 auio.uio_resid = count;
2850 error = VOP_READLINK(vp, &auio, td->td_ucred);
2852 vput(vp);
2853 *res = count - auio.uio_resid;
2854 return (error);
2858 * readlink_args(char *path, char *buf, int count)
2860 * Return target name of a symbolic link.
2863 sys_readlink(struct readlink_args *uap)
2865 struct nlookupdata nd;
2866 int error;
2868 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2869 if (error == 0) {
2870 error = kern_readlink(&nd, uap->buf, uap->count,
2871 &uap->sysmsg_result);
2873 nlookup_done(&nd);
2874 return (error);
2878 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2880 * Return target name of a symbolic link. The path is relative to the
2881 * directory associated with fd.
2884 sys_readlinkat(struct readlinkat_args *uap)
2886 struct nlookupdata nd;
2887 struct file *fp;
2888 int error;
2890 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2891 if (error == 0) {
2892 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2893 &uap->sysmsg_result);
2895 nlookup_done_at(&nd, fp);
2896 return (error);
2899 static int
2900 setfflags(struct vnode *vp, int flags)
2902 struct thread *td = curthread;
2903 int error;
2904 struct vattr vattr;
2907 * Prevent non-root users from setting flags on devices. When
2908 * a device is reused, users can retain ownership of the device
2909 * if they are allowed to set flags and programs assume that
2910 * chown can't fail when done as root.
2912 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2913 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2914 return (error);
2917 * note: vget is required for any operation that might mod the vnode
2918 * so VINACTIVE is properly cleared.
2920 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2921 VATTR_NULL(&vattr);
2922 vattr.va_flags = flags;
2923 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2924 vput(vp);
2926 return (error);
2930 * chflags(char *path, int flags)
2932 * Change flags of a file given a path name.
2935 sys_chflags(struct chflags_args *uap)
2937 struct nlookupdata nd;
2938 struct vnode *vp;
2939 int error;
2941 vp = NULL;
2942 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2943 if (error == 0)
2944 error = nlookup(&nd);
2945 if (error == 0)
2946 error = ncp_writechk(&nd.nl_nch);
2947 if (error == 0)
2948 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2949 nlookup_done(&nd);
2950 if (error == 0) {
2951 error = setfflags(vp, uap->flags);
2952 vrele(vp);
2954 return (error);
2958 * lchflags(char *path, int flags)
2960 * Change flags of a file given a path name, but don't follow symlinks.
2963 sys_lchflags(struct lchflags_args *uap)
2965 struct nlookupdata nd;
2966 struct vnode *vp;
2967 int error;
2969 vp = NULL;
2970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2971 if (error == 0)
2972 error = nlookup(&nd);
2973 if (error == 0)
2974 error = ncp_writechk(&nd.nl_nch);
2975 if (error == 0)
2976 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2977 nlookup_done(&nd);
2978 if (error == 0) {
2979 error = setfflags(vp, uap->flags);
2980 vrele(vp);
2982 return (error);
2986 * fchflags_args(int fd, int flags)
2988 * Change flags of a file given a file descriptor.
2991 sys_fchflags(struct fchflags_args *uap)
2993 struct thread *td = curthread;
2994 struct proc *p = td->td_proc;
2995 struct file *fp;
2996 int error;
2998 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2999 return (error);
3000 if (fp->f_nchandle.ncp)
3001 error = ncp_writechk(&fp->f_nchandle);
3002 if (error == 0)
3003 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3004 fdrop(fp);
3005 return (error);
3008 static int
3009 setfmode(struct vnode *vp, int mode)
3011 struct thread *td = curthread;
3012 int error;
3013 struct vattr vattr;
3016 * note: vget is required for any operation that might mod the vnode
3017 * so VINACTIVE is properly cleared.
3019 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3020 VATTR_NULL(&vattr);
3021 vattr.va_mode = mode & ALLPERMS;
3022 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3023 vput(vp);
3025 return error;
3029 kern_chmod(struct nlookupdata *nd, int mode)
3031 struct vnode *vp;
3032 int error;
3034 if ((error = nlookup(nd)) != 0)
3035 return (error);
3036 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3037 return (error);
3038 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3039 error = setfmode(vp, mode);
3040 vrele(vp);
3041 return (error);
3045 * chmod_args(char *path, int mode)
3047 * Change mode of a file given path name.
3050 sys_chmod(struct chmod_args *uap)
3052 struct nlookupdata nd;
3053 int error;
3055 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3056 if (error == 0)
3057 error = kern_chmod(&nd, uap->mode);
3058 nlookup_done(&nd);
3059 return (error);
3063 * lchmod_args(char *path, int mode)
3065 * Change mode of a file given path name (don't follow links.)
3068 sys_lchmod(struct lchmod_args *uap)
3070 struct nlookupdata nd;
3071 int error;
3073 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3074 if (error == 0)
3075 error = kern_chmod(&nd, uap->mode);
3076 nlookup_done(&nd);
3077 return (error);
3081 * fchmod_args(int fd, int mode)
3083 * Change mode of a file given a file descriptor.
3086 sys_fchmod(struct fchmod_args *uap)
3088 struct thread *td = curthread;
3089 struct proc *p = td->td_proc;
3090 struct file *fp;
3091 int error;
3093 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3094 return (error);
3095 if (fp->f_nchandle.ncp)
3096 error = ncp_writechk(&fp->f_nchandle);
3097 if (error == 0)
3098 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3099 fdrop(fp);
3100 return (error);
3104 * fchmodat_args(char *path, int mode)
3106 * Change mode of a file pointed to by fd/path.
3109 sys_fchmodat(struct fchmodat_args *uap)
3111 struct nlookupdata nd;
3112 struct file *fp;
3113 int error;
3114 int flags;
3116 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3117 return (EINVAL);
3118 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3120 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3121 UIO_USERSPACE, flags);
3122 if (error == 0)
3123 error = kern_chmod(&nd, uap->mode);
3124 nlookup_done_at(&nd, fp);
3125 return (error);
3128 static int
3129 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3131 struct thread *td = curthread;
3132 int error;
3133 struct vattr vattr;
3134 uid_t o_uid;
3135 gid_t o_gid;
3136 uint64_t size;
3139 * note: vget is required for any operation that might mod the vnode
3140 * so VINACTIVE is properly cleared.
3142 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3143 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3144 return error;
3145 o_uid = vattr.va_uid;
3146 o_gid = vattr.va_gid;
3147 size = vattr.va_size;
3149 VATTR_NULL(&vattr);
3150 vattr.va_uid = uid;
3151 vattr.va_gid = gid;
3152 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3153 vput(vp);
3156 if (error == 0) {
3157 if (uid == -1)
3158 uid = o_uid;
3159 if (gid == -1)
3160 gid = o_gid;
3161 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3162 VFS_ACCOUNT(mp, uid, gid, size);
3165 return error;
3169 kern_chown(struct nlookupdata *nd, int uid, int gid)
3171 struct vnode *vp;
3172 int error;
3174 if ((error = nlookup(nd)) != 0)
3175 return (error);
3176 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3177 return (error);
3178 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3179 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3180 vrele(vp);
3181 return (error);
3185 * chown(char *path, int uid, int gid)
3187 * Set ownership given a path name.
3190 sys_chown(struct chown_args *uap)
3192 struct nlookupdata nd;
3193 int error;
3195 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3196 if (error == 0)
3197 error = kern_chown(&nd, uap->uid, uap->gid);
3198 nlookup_done(&nd);
3199 return (error);
3203 * lchown_args(char *path, int uid, int gid)
3205 * Set ownership given a path name, do not cross symlinks.
3208 sys_lchown(struct lchown_args *uap)
3210 struct nlookupdata nd;
3211 int error;
3213 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3214 if (error == 0)
3215 error = kern_chown(&nd, uap->uid, uap->gid);
3216 nlookup_done(&nd);
3217 return (error);
3221 * fchown_args(int fd, int uid, int gid)
3223 * Set ownership given a file descriptor.
3226 sys_fchown(struct fchown_args *uap)
3228 struct thread *td = curthread;
3229 struct proc *p = td->td_proc;
3230 struct file *fp;
3231 int error;
3233 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3234 return (error);
3235 if (fp->f_nchandle.ncp)
3236 error = ncp_writechk(&fp->f_nchandle);
3237 if (error == 0)
3238 error = setfown(p->p_fd->fd_ncdir.mount,
3239 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3240 fdrop(fp);
3241 return (error);
3245 * fchownat(int fd, char *path, int uid, int gid, int flags)
3247 * Set ownership of file pointed to by fd/path.
3250 sys_fchownat(struct fchownat_args *uap)
3252 struct nlookupdata nd;
3253 struct file *fp;
3254 int error;
3255 int flags;
3257 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3258 return (EINVAL);
3259 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3261 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3262 UIO_USERSPACE, flags);
3263 if (error == 0)
3264 error = kern_chown(&nd, uap->uid, uap->gid);
3265 nlookup_done_at(&nd, fp);
3266 return (error);
3270 static int
3271 getutimes(const struct timeval *tvp, struct timespec *tsp)
3273 struct timeval tv[2];
3275 if (tvp == NULL) {
3276 microtime(&tv[0]);
3277 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3278 tsp[1] = tsp[0];
3279 } else {
3280 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3281 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3283 return 0;
3286 static int
3287 setutimes(struct vnode *vp, struct vattr *vattr,
3288 const struct timespec *ts, int nullflag)
3290 struct thread *td = curthread;
3291 int error;
3293 VATTR_NULL(vattr);
3294 vattr->va_atime = ts[0];
3295 vattr->va_mtime = ts[1];
3296 if (nullflag)
3297 vattr->va_vaflags |= VA_UTIMES_NULL;
3298 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3300 return error;
3304 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3306 struct timespec ts[2];
3307 struct vnode *vp;
3308 struct vattr vattr;
3309 int error;
3311 if ((error = getutimes(tptr, ts)) != 0)
3312 return (error);
3315 * NOTE: utimes() succeeds for the owner even if the file
3316 * is not user-writable.
3318 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3320 if ((error = nlookup(nd)) != 0)
3321 return (error);
3322 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3323 return (error);
3324 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3325 return (error);
3328 * note: vget is required for any operation that might mod the vnode
3329 * so VINACTIVE is properly cleared.
3331 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3332 error = vget(vp, LK_EXCLUSIVE);
3333 if (error == 0) {
3334 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3335 vput(vp);
3338 vrele(vp);
3339 return (error);
3343 * utimes_args(char *path, struct timeval *tptr)
3345 * Set the access and modification times of a file.
3348 sys_utimes(struct utimes_args *uap)
3350 struct timeval tv[2];
3351 struct nlookupdata nd;
3352 int error;
3354 if (uap->tptr) {
3355 error = copyin(uap->tptr, tv, sizeof(tv));
3356 if (error)
3357 return (error);
3359 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3360 if (error == 0)
3361 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3362 nlookup_done(&nd);
3363 return (error);
3367 * lutimes_args(char *path, struct timeval *tptr)
3369 * Set the access and modification times of a file.
3372 sys_lutimes(struct lutimes_args *uap)
3374 struct timeval tv[2];
3375 struct nlookupdata nd;
3376 int error;
3378 if (uap->tptr) {
3379 error = copyin(uap->tptr, tv, sizeof(tv));
3380 if (error)
3381 return (error);
3383 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3384 if (error == 0)
3385 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3386 nlookup_done(&nd);
3387 return (error);
3391 * Set utimes on a file descriptor. The creds used to open the
3392 * file are used to determine whether the operation is allowed
3393 * or not.
3396 kern_futimes(int fd, struct timeval *tptr)
3398 struct thread *td = curthread;
3399 struct proc *p = td->td_proc;
3400 struct timespec ts[2];
3401 struct file *fp;
3402 struct vnode *vp;
3403 struct vattr vattr;
3404 int error;
3406 error = getutimes(tptr, ts);
3407 if (error)
3408 return (error);
3409 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3410 return (error);
3411 if (fp->f_nchandle.ncp)
3412 error = ncp_writechk(&fp->f_nchandle);
3413 if (error == 0) {
3414 vp = fp->f_data;
3415 error = vget(vp, LK_EXCLUSIVE);
3416 if (error == 0) {
3417 error = VOP_GETATTR(vp, &vattr);
3418 if (error == 0) {
3419 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3420 fp->f_cred);
3422 if (error == 0) {
3423 error = setutimes(vp, &vattr, ts,
3424 (tptr == NULL));
3426 vput(vp);
3429 fdrop(fp);
3430 return (error);
3434 * futimes_args(int fd, struct timeval *tptr)
3436 * Set the access and modification times of a file.
3439 sys_futimes(struct futimes_args *uap)
3441 struct timeval tv[2];
3442 int error;
3444 if (uap->tptr) {
3445 error = copyin(uap->tptr, tv, sizeof(tv));
3446 if (error)
3447 return (error);
3449 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3451 return (error);
3455 kern_truncate(struct nlookupdata *nd, off_t length)
3457 struct vnode *vp;
3458 struct vattr vattr;
3459 int error;
3460 uid_t uid = 0;
3461 gid_t gid = 0;
3462 uint64_t old_size = 0;
3464 if (length < 0)
3465 return(EINVAL);
3466 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3467 if ((error = nlookup(nd)) != 0)
3468 return (error);
3469 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3470 return (error);
3471 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3472 return (error);
3473 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3474 vrele(vp);
3475 return (error);
3477 if (vp->v_type == VDIR) {
3478 error = EISDIR;
3479 goto done;
3481 if (vfs_quota_enabled) {
3482 error = VOP_GETATTR(vp, &vattr);
3483 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3484 uid = vattr.va_uid;
3485 gid = vattr.va_gid;
3486 old_size = vattr.va_size;
3489 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3490 VATTR_NULL(&vattr);
3491 vattr.va_size = length;
3492 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3493 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3495 done:
3496 vput(vp);
3497 return (error);
3501 * truncate(char *path, int pad, off_t length)
3503 * Truncate a file given its path name.
3506 sys_truncate(struct truncate_args *uap)
3508 struct nlookupdata nd;
3509 int error;
3511 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3512 if (error == 0)
3513 error = kern_truncate(&nd, uap->length);
3514 nlookup_done(&nd);
3515 return error;
3519 kern_ftruncate(int fd, off_t length)
3521 struct thread *td = curthread;
3522 struct proc *p = td->td_proc;
3523 struct vattr vattr;
3524 struct vnode *vp;
3525 struct file *fp;
3526 int error;
3527 uid_t uid = 0;
3528 gid_t gid = 0;
3529 uint64_t old_size = 0;
3530 struct mount *mp;
3532 if (length < 0)
3533 return(EINVAL);
3534 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3535 return (error);
3536 if (fp->f_nchandle.ncp) {
3537 error = ncp_writechk(&fp->f_nchandle);
3538 if (error)
3539 goto done;
3541 if ((fp->f_flag & FWRITE) == 0) {
3542 error = EINVAL;
3543 goto done;
3545 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3546 error = EINVAL;
3547 goto done;
3549 vp = (struct vnode *)fp->f_data;
3550 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3551 if (vp->v_type == VDIR) {
3552 error = EISDIR;
3553 goto done;
3556 if (vfs_quota_enabled) {
3557 error = VOP_GETATTR(vp, &vattr);
3558 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3559 uid = vattr.va_uid;
3560 gid = vattr.va_gid;
3561 old_size = vattr.va_size;
3564 if ((error = vn_writechk(vp, NULL)) == 0) {
3565 VATTR_NULL(&vattr);
3566 vattr.va_size = length;
3567 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3568 mp = vq_vptomp(vp);
3569 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3571 vn_unlock(vp);
3572 done:
3573 fdrop(fp);
3574 return (error);
3578 * ftruncate_args(int fd, int pad, off_t length)
3580 * Truncate a file given a file descriptor.
3583 sys_ftruncate(struct ftruncate_args *uap)
3585 int error;
3587 error = kern_ftruncate(uap->fd, uap->length);
3589 return (error);
3593 * fsync(int fd)
3595 * Sync an open file.
3598 sys_fsync(struct fsync_args *uap)
3600 struct thread *td = curthread;
3601 struct proc *p = td->td_proc;
3602 struct vnode *vp;
3603 struct file *fp;
3604 vm_object_t obj;
3605 int error;
3607 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3608 return (error);
3609 vp = (struct vnode *)fp->f_data;
3610 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3611 if ((obj = vp->v_object) != NULL)
3612 vm_object_page_clean(obj, 0, 0, 0);
3613 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3614 if (error == 0 && vp->v_mount)
3615 error = buf_fsync(vp);
3616 vn_unlock(vp);
3617 fdrop(fp);
3619 return (error);
3623 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3625 struct nchandle fnchd;
3626 struct nchandle tnchd;
3627 struct namecache *ncp;
3628 struct vnode *fdvp;
3629 struct vnode *tdvp;
3630 struct mount *mp;
3631 int error;
3633 bwillinode(1);
3634 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3635 if ((error = nlookup(fromnd)) != 0)
3636 return (error);
3637 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3638 return (ENOENT);
3639 fnchd.mount = fromnd->nl_nch.mount;
3640 cache_hold(&fnchd);
3643 * unlock the source nch so we can lookup the target nch without
3644 * deadlocking. The target may or may not exist so we do not check
3645 * for a target vp like kern_mkdir() and other creation functions do.
3647 * The source and target directories are ref'd and rechecked after
3648 * everything is relocked to determine if the source or target file
3649 * has been renamed.
3651 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3652 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3653 cache_unlock(&fromnd->nl_nch);
3655 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3656 if ((error = nlookup(tond)) != 0) {
3657 cache_drop(&fnchd);
3658 return (error);
3660 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3661 cache_drop(&fnchd);
3662 return (ENOENT);
3664 tnchd.mount = tond->nl_nch.mount;
3665 cache_hold(&tnchd);
3668 * If the source and target are the same there is nothing to do
3670 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3671 cache_drop(&fnchd);
3672 cache_drop(&tnchd);
3673 return (0);
3677 * Mount points cannot be renamed or overwritten
3679 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3680 NCF_ISMOUNTPT
3682 cache_drop(&fnchd);
3683 cache_drop(&tnchd);
3684 return (EINVAL);
3688 * Relock the source ncp. cache_relock() will deal with any
3689 * deadlocks against the already-locked tond and will also
3690 * make sure both are resolved.
3692 * NOTE AFTER RELOCKING: The source or target ncp may have become
3693 * invalid while they were unlocked, nc_vp and nc_mount could
3694 * be NULL.
3696 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3697 &tond->nl_nch, tond->nl_cred);
3698 fromnd->nl_flags |= NLC_NCPISLOCKED;
3701 * If either fromnd or tond are marked destroyed a ripout occured
3702 * out from under us and we must retry.
3704 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3705 fromnd->nl_nch.ncp->nc_vp == NULL ||
3706 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3707 kprintf("kern_rename: retry due to ripout on: "
3708 "\"%s\" -> \"%s\"\n",
3709 fromnd->nl_nch.ncp->nc_name,
3710 tond->nl_nch.ncp->nc_name);
3711 cache_drop(&fnchd);
3712 cache_drop(&tnchd);
3713 return (EAGAIN);
3717 * make sure the parent directories linkages are the same
3719 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3720 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3721 cache_drop(&fnchd);
3722 cache_drop(&tnchd);
3723 return (ENOENT);
3727 * Both the source and target must be within the same filesystem and
3728 * in the same filesystem as their parent directories within the
3729 * namecache topology.
3731 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3733 mp = fnchd.mount;
3734 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3735 mp != tond->nl_nch.mount) {
3736 cache_drop(&fnchd);
3737 cache_drop(&tnchd);
3738 return (EXDEV);
3742 * Make sure the mount point is writable
3744 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3745 cache_drop(&fnchd);
3746 cache_drop(&tnchd);
3747 return (error);
3751 * If the target exists and either the source or target is a directory,
3752 * then both must be directories.
3754 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3755 * have become NULL.
3757 if (tond->nl_nch.ncp->nc_vp) {
3758 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3759 error = ENOENT;
3760 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3761 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3762 error = ENOTDIR;
3763 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3764 error = EISDIR;
3769 * You cannot rename a source into itself or a subdirectory of itself.
3770 * We check this by travsersing the target directory upwards looking
3771 * for a match against the source.
3773 * XXX MPSAFE
3775 if (error == 0) {
3776 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3777 if (fromnd->nl_nch.ncp == ncp) {
3778 error = EINVAL;
3779 break;
3784 cache_drop(&fnchd);
3785 cache_drop(&tnchd);
3788 * Even though the namespaces are different, they may still represent
3789 * hardlinks to the same file. The filesystem might have a hard time
3790 * with this so we issue a NREMOVE of the source instead of a NRENAME
3791 * when we detect the situation.
3793 if (error == 0) {
3794 fdvp = fromnd->nl_dvp;
3795 tdvp = tond->nl_dvp;
3796 if (fdvp == NULL || tdvp == NULL) {
3797 error = EPERM;
3798 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3799 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3800 fromnd->nl_cred);
3801 } else {
3802 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3803 fdvp, tdvp, tond->nl_cred);
3806 return (error);
3810 * rename_args(char *from, char *to)
3812 * Rename files. Source and destination must either both be directories,
3813 * or both not be directories. If target is a directory, it must be empty.
3816 sys_rename(struct rename_args *uap)
3818 struct nlookupdata fromnd, tond;
3819 int error;
3821 do {
3822 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3823 if (error == 0) {
3824 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3825 if (error == 0)
3826 error = kern_rename(&fromnd, &tond);
3827 nlookup_done(&tond);
3829 nlookup_done(&fromnd);
3830 } while (error == EAGAIN);
3831 return (error);
3835 * renameat_args(int oldfd, char *old, int newfd, char *new)
3837 * Rename files using paths relative to the directories associated with
3838 * oldfd and newfd. Source and destination must either both be directories,
3839 * or both not be directories. If target is a directory, it must be empty.
3842 sys_renameat(struct renameat_args *uap)
3844 struct nlookupdata oldnd, newnd;
3845 struct file *oldfp, *newfp;
3846 int error;
3848 do {
3849 error = nlookup_init_at(&oldnd, &oldfp,
3850 uap->oldfd, uap->old,
3851 UIO_USERSPACE, 0);
3852 if (error == 0) {
3853 error = nlookup_init_at(&newnd, &newfp,
3854 uap->newfd, uap->new,
3855 UIO_USERSPACE, 0);
3856 if (error == 0)
3857 error = kern_rename(&oldnd, &newnd);
3858 nlookup_done_at(&newnd, newfp);
3860 nlookup_done_at(&oldnd, oldfp);
3861 } while (error == EAGAIN);
3862 return (error);
3866 kern_mkdir(struct nlookupdata *nd, int mode)
3868 struct thread *td = curthread;
3869 struct proc *p = td->td_proc;
3870 struct vnode *vp;
3871 struct vattr vattr;
3872 int error;
3874 bwillinode(1);
3875 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3876 if ((error = nlookup(nd)) != 0)
3877 return (error);
3879 if (nd->nl_nch.ncp->nc_vp)
3880 return (EEXIST);
3881 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3882 return (error);
3883 VATTR_NULL(&vattr);
3884 vattr.va_type = VDIR;
3885 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3887 vp = NULL;
3888 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3889 if (error == 0)
3890 vput(vp);
3891 return (error);
3895 * mkdir_args(char *path, int mode)
3897 * Make a directory file.
3900 sys_mkdir(struct mkdir_args *uap)
3902 struct nlookupdata nd;
3903 int error;
3905 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3906 if (error == 0)
3907 error = kern_mkdir(&nd, uap->mode);
3908 nlookup_done(&nd);
3909 return (error);
3913 * mkdirat_args(int fd, char *path, mode_t mode)
3915 * Make a directory file. The path is relative to the directory associated
3916 * with fd.
3919 sys_mkdirat(struct mkdirat_args *uap)
3921 struct nlookupdata nd;
3922 struct file *fp;
3923 int error;
3925 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3926 if (error == 0)
3927 error = kern_mkdir(&nd, uap->mode);
3928 nlookup_done_at(&nd, fp);
3929 return (error);
3933 kern_rmdir(struct nlookupdata *nd)
3935 int error;
3937 bwillinode(1);
3938 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3939 if ((error = nlookup(nd)) != 0)
3940 return (error);
3943 * Do not allow directories representing mount points to be
3944 * deleted, even if empty. Check write perms on mount point
3945 * in case the vnode is aliased (aka nullfs).
3947 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3948 return (EINVAL);
3949 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3950 return (error);
3951 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3952 return (error);
3956 * rmdir_args(char *path)
3958 * Remove a directory file.
3961 sys_rmdir(struct rmdir_args *uap)
3963 struct nlookupdata nd;
3964 int error;
3966 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3967 if (error == 0)
3968 error = kern_rmdir(&nd);
3969 nlookup_done(&nd);
3970 return (error);
3974 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3975 enum uio_seg direction)
3977 struct thread *td = curthread;
3978 struct proc *p = td->td_proc;
3979 struct vnode *vp;
3980 struct file *fp;
3981 struct uio auio;
3982 struct iovec aiov;
3983 off_t loff;
3984 int error, eofflag;
3986 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3987 return (error);
3988 if ((fp->f_flag & FREAD) == 0) {
3989 error = EBADF;
3990 goto done;
3992 vp = (struct vnode *)fp->f_data;
3993 unionread:
3994 if (vp->v_type != VDIR) {
3995 error = EINVAL;
3996 goto done;
3998 aiov.iov_base = buf;
3999 aiov.iov_len = count;
4000 auio.uio_iov = &aiov;
4001 auio.uio_iovcnt = 1;
4002 auio.uio_rw = UIO_READ;
4003 auio.uio_segflg = direction;
4004 auio.uio_td = td;
4005 auio.uio_resid = count;
4006 loff = auio.uio_offset = fp->f_offset;
4007 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4008 fp->f_offset = auio.uio_offset;
4009 if (error)
4010 goto done;
4011 if (count == auio.uio_resid) {
4012 if (union_dircheckp) {
4013 error = union_dircheckp(td, &vp, fp);
4014 if (error == -1)
4015 goto unionread;
4016 if (error)
4017 goto done;
4019 #if 0
4020 if ((vp->v_flag & VROOT) &&
4021 (vp->v_mount->mnt_flag & MNT_UNION)) {
4022 struct vnode *tvp = vp;
4023 vp = vp->v_mount->mnt_vnodecovered;
4024 vref(vp);
4025 fp->f_data = vp;
4026 fp->f_offset = 0;
4027 vrele(tvp);
4028 goto unionread;
4030 #endif
4034 * WARNING! *basep may not be wide enough to accomodate the
4035 * seek offset. XXX should we hack this to return the upper 32 bits
4036 * for offsets greater then 4G?
4038 if (basep) {
4039 *basep = (long)loff;
4041 *res = count - auio.uio_resid;
4042 done:
4043 fdrop(fp);
4044 return (error);
4048 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4050 * Read a block of directory entries in a file system independent format.
4053 sys_getdirentries(struct getdirentries_args *uap)
4055 long base;
4056 int error;
4058 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4059 &uap->sysmsg_result, UIO_USERSPACE);
4061 if (error == 0 && uap->basep)
4062 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4063 return (error);
4067 * getdents_args(int fd, char *buf, size_t count)
4070 sys_getdents(struct getdents_args *uap)
4072 int error;
4074 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4075 &uap->sysmsg_result, UIO_USERSPACE);
4077 return (error);
4081 * Set the mode mask for creation of filesystem nodes.
4083 * umask(int newmask)
4086 sys_umask(struct umask_args *uap)
4088 struct thread *td = curthread;
4089 struct proc *p = td->td_proc;
4090 struct filedesc *fdp;
4092 fdp = p->p_fd;
4093 uap->sysmsg_result = fdp->fd_cmask;
4094 fdp->fd_cmask = uap->newmask & ALLPERMS;
4095 return (0);
4099 * revoke(char *path)
4101 * Void all references to file by ripping underlying filesystem
4102 * away from vnode.
4105 sys_revoke(struct revoke_args *uap)
4107 struct nlookupdata nd;
4108 struct vattr vattr;
4109 struct vnode *vp;
4110 struct ucred *cred;
4111 int error;
4113 vp = NULL;
4114 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4115 if (error == 0)
4116 error = nlookup(&nd);
4117 if (error == 0)
4118 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4119 cred = crhold(nd.nl_cred);
4120 nlookup_done(&nd);
4121 if (error == 0) {
4122 if (error == 0)
4123 error = VOP_GETATTR(vp, &vattr);
4124 if (error == 0 && cred->cr_uid != vattr.va_uid)
4125 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4126 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4127 if (vcount(vp) > 0)
4128 error = vrevoke(vp, cred);
4129 } else if (error == 0) {
4130 error = vrevoke(vp, cred);
4132 vrele(vp);
4134 if (cred)
4135 crfree(cred);
4136 return (error);
4140 * getfh_args(char *fname, fhandle_t *fhp)
4142 * Get (NFS) file handle
4144 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4145 * mount. This allows nullfs mounts to be explicitly exported.
4147 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4149 * nullfs mounts of subdirectories are not safe. That is, it will
4150 * work, but you do not really have protection against access to
4151 * the related parent directories.
4154 sys_getfh(struct getfh_args *uap)
4156 struct thread *td = curthread;
4157 struct nlookupdata nd;
4158 fhandle_t fh;
4159 struct vnode *vp;
4160 struct mount *mp;
4161 int error;
4164 * Must be super user
4166 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4167 return (error);
4169 vp = NULL;
4170 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4171 if (error == 0)
4172 error = nlookup(&nd);
4173 if (error == 0)
4174 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4175 mp = nd.nl_nch.mount;
4176 nlookup_done(&nd);
4177 if (error == 0) {
4178 bzero(&fh, sizeof(fh));
4179 fh.fh_fsid = mp->mnt_stat.f_fsid;
4180 error = VFS_VPTOFH(vp, &fh.fh_fid);
4181 vput(vp);
4182 if (error == 0)
4183 error = copyout(&fh, uap->fhp, sizeof(fh));
4185 return (error);
4189 * fhopen_args(const struct fhandle *u_fhp, int flags)
4191 * syscall for the rpc.lockd to use to translate a NFS file handle into
4192 * an open descriptor.
4194 * warning: do not remove the priv_check() call or this becomes one giant
4195 * security hole.
4198 sys_fhopen(struct fhopen_args *uap)
4200 struct thread *td = curthread;
4201 struct filedesc *fdp = td->td_proc->p_fd;
4202 struct mount *mp;
4203 struct vnode *vp;
4204 struct fhandle fhp;
4205 struct vattr vat;
4206 struct vattr *vap = &vat;
4207 struct flock lf;
4208 int fmode, mode, error = 0, type;
4209 struct file *nfp;
4210 struct file *fp;
4211 int indx;
4214 * Must be super user
4216 error = priv_check(td, PRIV_ROOT);
4217 if (error)
4218 return (error);
4220 fmode = FFLAGS(uap->flags);
4223 * Why not allow a non-read/write open for our lockd?
4225 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4226 return (EINVAL);
4227 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4228 if (error)
4229 return(error);
4232 * Find the mount point
4234 mp = vfs_getvfs(&fhp.fh_fsid);
4235 if (mp == NULL) {
4236 error = ESTALE;
4237 goto done;
4239 /* now give me my vnode, it gets returned to me locked */
4240 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4241 if (error)
4242 goto done;
4244 * from now on we have to make sure not
4245 * to forget about the vnode
4246 * any error that causes an abort must vput(vp)
4247 * just set error = err and 'goto bad;'.
4251 * from vn_open
4253 if (vp->v_type == VLNK) {
4254 error = EMLINK;
4255 goto bad;
4257 if (vp->v_type == VSOCK) {
4258 error = EOPNOTSUPP;
4259 goto bad;
4261 mode = 0;
4262 if (fmode & (FWRITE | O_TRUNC)) {
4263 if (vp->v_type == VDIR) {
4264 error = EISDIR;
4265 goto bad;
4267 error = vn_writechk(vp, NULL);
4268 if (error)
4269 goto bad;
4270 mode |= VWRITE;
4272 if (fmode & FREAD)
4273 mode |= VREAD;
4274 if (mode) {
4275 error = VOP_ACCESS(vp, mode, td->td_ucred);
4276 if (error)
4277 goto bad;
4279 if (fmode & O_TRUNC) {
4280 vn_unlock(vp); /* XXX */
4281 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4282 VATTR_NULL(vap);
4283 vap->va_size = 0;
4284 error = VOP_SETATTR(vp, vap, td->td_ucred);
4285 if (error)
4286 goto bad;
4290 * VOP_OPEN needs the file pointer so it can potentially override
4291 * it.
4293 * WARNING! no f_nchandle will be associated when fhopen()ing a
4294 * directory. XXX
4296 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4297 goto bad;
4298 fp = nfp;
4300 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4301 if (error) {
4303 * setting f_ops this way prevents VOP_CLOSE from being
4304 * called or fdrop() releasing the vp from v_data. Since
4305 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4307 fp->f_ops = &badfileops;
4308 fp->f_data = NULL;
4309 goto bad_drop;
4313 * The fp is given its own reference, we still have our ref and lock.
4315 * Assert that all regular files must be created with a VM object.
4317 if (vp->v_type == VREG && vp->v_object == NULL) {
4318 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4319 goto bad_drop;
4323 * The open was successful. Handle any locking requirements.
4325 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4326 lf.l_whence = SEEK_SET;
4327 lf.l_start = 0;
4328 lf.l_len = 0;
4329 if (fmode & O_EXLOCK)
4330 lf.l_type = F_WRLCK;
4331 else
4332 lf.l_type = F_RDLCK;
4333 if (fmode & FNONBLOCK)
4334 type = 0;
4335 else
4336 type = F_WAIT;
4337 vn_unlock(vp);
4338 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4340 * release our private reference.
4342 fsetfd(fdp, NULL, indx);
4343 fdrop(fp);
4344 vrele(vp);
4345 goto done;
4347 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4348 fp->f_flag |= FHASLOCK;
4352 * Clean up. Associate the file pointer with the previously
4353 * reserved descriptor and return it.
4355 vput(vp);
4356 fsetfd(fdp, fp, indx);
4357 fdrop(fp);
4358 uap->sysmsg_result = indx;
4359 if (uap->flags & O_CLOEXEC)
4360 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4361 return (error);
4363 bad_drop:
4364 fsetfd(fdp, NULL, indx);
4365 fdrop(fp);
4366 bad:
4367 vput(vp);
4368 done:
4369 return (error);
4373 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4376 sys_fhstat(struct fhstat_args *uap)
4378 struct thread *td = curthread;
4379 struct stat sb;
4380 fhandle_t fh;
4381 struct mount *mp;
4382 struct vnode *vp;
4383 int error;
4386 * Must be super user
4388 error = priv_check(td, PRIV_ROOT);
4389 if (error)
4390 return (error);
4392 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4393 if (error)
4394 return (error);
4396 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4397 error = ESTALE;
4398 if (error == 0) {
4399 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4400 error = vn_stat(vp, &sb, td->td_ucred);
4401 vput(vp);
4404 if (error == 0)
4405 error = copyout(&sb, uap->sb, sizeof(sb));
4406 return (error);
4410 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4413 sys_fhstatfs(struct fhstatfs_args *uap)
4415 struct thread *td = curthread;
4416 struct proc *p = td->td_proc;
4417 struct statfs *sp;
4418 struct mount *mp;
4419 struct vnode *vp;
4420 struct statfs sb;
4421 char *fullpath, *freepath;
4422 fhandle_t fh;
4423 int error;
4426 * Must be super user
4428 if ((error = priv_check(td, PRIV_ROOT)))
4429 return (error);
4431 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4432 return (error);
4434 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4435 error = ESTALE;
4436 goto done;
4438 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4439 error = ESTALE;
4440 goto done;
4443 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4444 goto done;
4445 mp = vp->v_mount;
4446 sp = &mp->mnt_stat;
4447 vput(vp);
4448 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4449 goto done;
4451 error = mount_path(p, mp, &fullpath, &freepath);
4452 if (error)
4453 goto done;
4454 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4455 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4456 kfree(freepath, M_TEMP);
4458 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4459 if (priv_check(td, PRIV_ROOT)) {
4460 bcopy(sp, &sb, sizeof(sb));
4461 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4462 sp = &sb;
4464 error = copyout(sp, uap->buf, sizeof(*sp));
4465 done:
4466 return (error);
4470 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4473 sys_fhstatvfs(struct fhstatvfs_args *uap)
4475 struct thread *td = curthread;
4476 struct proc *p = td->td_proc;
4477 struct statvfs *sp;
4478 struct mount *mp;
4479 struct vnode *vp;
4480 fhandle_t fh;
4481 int error;
4484 * Must be super user
4486 if ((error = priv_check(td, PRIV_ROOT)))
4487 return (error);
4489 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4490 return (error);
4492 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4493 error = ESTALE;
4494 goto done;
4496 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4497 error = ESTALE;
4498 goto done;
4501 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4502 goto done;
4503 mp = vp->v_mount;
4504 sp = &mp->mnt_vstat;
4505 vput(vp);
4506 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4507 goto done;
4509 sp->f_flag = 0;
4510 if (mp->mnt_flag & MNT_RDONLY)
4511 sp->f_flag |= ST_RDONLY;
4512 if (mp->mnt_flag & MNT_NOSUID)
4513 sp->f_flag |= ST_NOSUID;
4514 error = copyout(sp, uap->buf, sizeof(*sp));
4515 done:
4516 return (error);
4521 * Syscall to push extended attribute configuration information into the
4522 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4523 * a command (int cmd), and attribute name and misc data. For now, the
4524 * attribute name is left in userspace for consumption by the VFS_op.
4525 * It will probably be changed to be copied into sysspace by the
4526 * syscall in the future, once issues with various consumers of the
4527 * attribute code have raised their hands.
4529 * Currently this is used only by UFS Extended Attributes.
4532 sys_extattrctl(struct extattrctl_args *uap)
4534 struct nlookupdata nd;
4535 struct vnode *vp;
4536 char attrname[EXTATTR_MAXNAMELEN];
4537 int error;
4538 size_t size;
4540 attrname[0] = 0;
4541 vp = NULL;
4542 error = 0;
4544 if (error == 0 && uap->filename) {
4545 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4546 NLC_FOLLOW);
4547 if (error == 0)
4548 error = nlookup(&nd);
4549 if (error == 0)
4550 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4551 nlookup_done(&nd);
4554 if (error == 0 && uap->attrname) {
4555 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4556 &size);
4559 if (error == 0) {
4560 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4561 if (error == 0)
4562 error = nlookup(&nd);
4563 if (error == 0)
4564 error = ncp_writechk(&nd.nl_nch);
4565 if (error == 0) {
4566 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4567 uap->attrnamespace,
4568 uap->attrname, nd.nl_cred);
4570 nlookup_done(&nd);
4573 return (error);
4577 * Syscall to get a named extended attribute on a file or directory.
4580 sys_extattr_set_file(struct extattr_set_file_args *uap)
4582 char attrname[EXTATTR_MAXNAMELEN];
4583 struct nlookupdata nd;
4584 struct vnode *vp;
4585 struct uio auio;
4586 struct iovec aiov;
4587 int error;
4589 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4590 if (error)
4591 return (error);
4593 vp = NULL;
4595 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4596 if (error == 0)
4597 error = nlookup(&nd);
4598 if (error == 0)
4599 error = ncp_writechk(&nd.nl_nch);
4600 if (error == 0)
4601 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4602 if (error) {
4603 nlookup_done(&nd);
4604 return (error);
4607 bzero(&auio, sizeof(auio));
4608 aiov.iov_base = uap->data;
4609 aiov.iov_len = uap->nbytes;
4610 auio.uio_iov = &aiov;
4611 auio.uio_iovcnt = 1;
4612 auio.uio_offset = 0;
4613 auio.uio_resid = uap->nbytes;
4614 auio.uio_rw = UIO_WRITE;
4615 auio.uio_td = curthread;
4617 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4618 &auio, nd.nl_cred);
4620 vput(vp);
4621 nlookup_done(&nd);
4622 return (error);
4626 * Syscall to get a named extended attribute on a file or directory.
4629 sys_extattr_get_file(struct extattr_get_file_args *uap)
4631 char attrname[EXTATTR_MAXNAMELEN];
4632 struct nlookupdata nd;
4633 struct uio auio;
4634 struct iovec aiov;
4635 struct vnode *vp;
4636 int error;
4638 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4639 if (error)
4640 return (error);
4642 vp = NULL;
4644 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4645 if (error == 0)
4646 error = nlookup(&nd);
4647 if (error == 0)
4648 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4649 if (error) {
4650 nlookup_done(&nd);
4651 return (error);
4654 bzero(&auio, sizeof(auio));
4655 aiov.iov_base = uap->data;
4656 aiov.iov_len = uap->nbytes;
4657 auio.uio_iov = &aiov;
4658 auio.uio_iovcnt = 1;
4659 auio.uio_offset = 0;
4660 auio.uio_resid = uap->nbytes;
4661 auio.uio_rw = UIO_READ;
4662 auio.uio_td = curthread;
4664 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4665 &auio, nd.nl_cred);
4666 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4668 vput(vp);
4669 nlookup_done(&nd);
4670 return(error);
4674 * Syscall to delete a named extended attribute from a file or directory.
4675 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4678 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4680 char attrname[EXTATTR_MAXNAMELEN];
4681 struct nlookupdata nd;
4682 struct vnode *vp;
4683 int error;
4685 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4686 if (error)
4687 return(error);
4689 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4690 if (error == 0)
4691 error = nlookup(&nd);
4692 if (error == 0)
4693 error = ncp_writechk(&nd.nl_nch);
4694 if (error == 0) {
4695 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4696 if (error == 0) {
4697 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4698 attrname, NULL, nd.nl_cred);
4699 vput(vp);
4702 nlookup_done(&nd);
4703 return(error);
4707 * Determine if the mount is visible to the process.
4709 static int
4710 chroot_visible_mnt(struct mount *mp, struct proc *p)
4712 struct nchandle nch;
4715 * Traverse from the mount point upwards. If we hit the process
4716 * root then the mount point is visible to the process.
4718 nch = mp->mnt_ncmountpt;
4719 while (nch.ncp) {
4720 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4721 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4722 return(1);
4724 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4725 nch = nch.mount->mnt_ncmounton;
4726 } else {
4727 nch.ncp = nch.ncp->nc_parent;
4732 * If the mount point is not visible to the process, but the
4733 * process root is in a subdirectory of the mount, return
4734 * TRUE anyway.
4736 if (p->p_fd->fd_nrdir.mount == mp)
4737 return(1);
4739 return(0);