kernel - Force NFSv3 for diskless nfs mount
[dragonfly.git] / sys / kern / vfs_syscalls.c
blob031128041df97ef5909a85992774298f50eb84e9
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 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, "");
105 * Virtual File System System Calls
109 * Mount a file system.
111 * mount_args(char *type, char *path, int flags, caddr_t data)
113 * MPALMOSTSAFE
116 sys_mount(struct mount_args *uap)
118 struct thread *td = curthread;
119 struct vnode *vp;
120 struct nchandle nch;
121 struct mount *mp, *nullmp;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
124 int hasmount;
125 struct vattr va;
126 struct nlookupdata nd;
127 char fstypename[MFSNAMELEN];
128 struct ucred *cred;
130 get_mplock();
131 cred = td->td_ucred;
132 if (jailed(cred)) {
133 error = EPERM;
134 goto done;
136 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
137 goto done;
140 * Do not allow NFS export by non-root users.
142 if (uap->flags & MNT_EXPORTED) {
143 error = priv_check(td, PRIV_ROOT);
144 if (error)
145 goto done;
148 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
150 if (priv_check(td, PRIV_ROOT))
151 uap->flags |= MNT_NOSUID | MNT_NODEV;
154 * Lookup the requested path and extract the nch and vnode.
156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
157 if (error == 0) {
158 if ((error = nlookup(&nd)) == 0) {
159 if (nd.nl_nch.ncp->nc_vp == NULL)
160 error = ENOENT;
163 if (error) {
164 nlookup_done(&nd);
165 goto done;
169 * If the target filesystem is resolved via a nullfs mount, then
170 * nd.nl_nch.mount will be pointing to the nullfs mount structure
171 * instead of the target file system. We need it in case we are
172 * doing an update.
174 nullmp = nd.nl_nch.mount;
177 * Extract the locked+refd ncp and cleanup the nd structure
179 nch = nd.nl_nch;
180 cache_zero(&nd.nl_nch);
181 nlookup_done(&nd);
183 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
184 hasmount = 1;
185 else
186 hasmount = 0;
190 * now we have the locked ref'd nch and unreferenced vnode.
192 vp = nch.ncp->nc_vp;
193 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
194 cache_put(&nch);
195 goto done;
197 cache_unlock(&nch);
200 * Extract the file system type. We need to know this early, to take
201 * appropriate actions if we are dealing with a nullfs.
203 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
204 cache_drop(&nch);
205 vput(vp);
206 goto done;
210 * Now we have an unlocked ref'd nch and a locked ref'd vp
212 if (uap->flags & MNT_UPDATE) {
213 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
214 cache_drop(&nch);
215 vput(vp);
216 error = EINVAL;
217 goto done;
220 if (strncmp(fstypename, "null", 5) == 0) {
221 KKASSERT(nullmp);
222 mp = nullmp;
223 } else {
224 mp = vp->v_mount;
227 flag = mp->mnt_flag;
228 flag2 = mp->mnt_kern_flag;
230 * We only allow the filesystem to be reloaded if it
231 * is currently mounted read-only.
233 if ((uap->flags & MNT_RELOAD) &&
234 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
235 cache_drop(&nch);
236 vput(vp);
237 error = EOPNOTSUPP; /* Needs translation */
238 goto done;
241 * Only root, or the user that did the original mount is
242 * permitted to update it.
244 if (mp->mnt_stat.f_owner != cred->cr_uid &&
245 (error = priv_check(td, PRIV_ROOT))) {
246 cache_drop(&nch);
247 vput(vp);
248 goto done;
250 if (vfs_busy(mp, LK_NOWAIT)) {
251 cache_drop(&nch);
252 vput(vp);
253 error = EBUSY;
254 goto done;
256 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
257 cache_drop(&nch);
258 vfs_unbusy(mp);
259 vput(vp);
260 error = EBUSY;
261 goto done;
263 vsetflags(vp, VMOUNT);
264 mp->mnt_flag |=
265 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
266 vn_unlock(vp);
267 goto update;
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error = VOP_GETATTR(vp, &va)) ||
274 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
275 cache_drop(&nch);
276 vput(vp);
277 goto done;
279 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
280 cache_drop(&nch);
281 vput(vp);
282 goto done;
284 if (vp->v_type != VDIR) {
285 cache_drop(&nch);
286 vput(vp);
287 error = ENOTDIR;
288 goto done;
290 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
291 cache_drop(&nch);
292 vput(vp);
293 error = EPERM;
294 goto done;
296 vfsp = vfsconf_find_by_name(fstypename);
297 if (vfsp == NULL) {
298 linker_file_t lf;
300 /* Only load modules for root (very important!) */
301 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
302 cache_drop(&nch);
303 vput(vp);
304 goto done;
306 error = linker_load_file(fstypename, &lf);
307 if (error || lf == NULL) {
308 cache_drop(&nch);
309 vput(vp);
310 if (lf == NULL)
311 error = ENODEV;
312 goto done;
314 lf->userrefs++;
315 /* lookup again, see if the VFS was loaded */
316 vfsp = vfsconf_find_by_name(fstypename);
317 if (vfsp == NULL) {
318 lf->userrefs--;
319 linker_file_unload(lf);
320 cache_drop(&nch);
321 vput(vp);
322 error = ENODEV;
323 goto done;
326 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
327 cache_drop(&nch);
328 vput(vp);
329 error = EBUSY;
330 goto done;
332 vsetflags(vp, VMOUNT);
335 * Allocate and initialize the filesystem.
337 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
338 mount_init(mp);
339 vfs_busy(mp, LK_NOWAIT);
340 mp->mnt_op = vfsp->vfc_vfsops;
341 mp->mnt_vfc = vfsp;
342 vfsp->vfc_refcount++;
343 mp->mnt_stat.f_type = vfsp->vfc_typenum;
344 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
345 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
346 mp->mnt_stat.f_owner = cred->cr_uid;
347 vn_unlock(vp);
348 update:
350 * Set the mount level flags.
352 if (uap->flags & MNT_RDONLY)
353 mp->mnt_flag |= MNT_RDONLY;
354 else if (mp->mnt_flag & MNT_RDONLY)
355 mp->mnt_kern_flag |= MNTK_WANTRDWR;
356 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
357 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
358 MNT_NOSYMFOLLOW | MNT_IGNORE |
359 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
360 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
361 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
362 MNT_NOSYMFOLLOW | MNT_IGNORE |
363 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
365 * Mount the filesystem.
366 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
367 * get.
369 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
370 if (mp->mnt_flag & MNT_UPDATE) {
371 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
372 mp->mnt_flag &= ~MNT_RDONLY;
373 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
374 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
375 if (error) {
376 mp->mnt_flag = flag;
377 mp->mnt_kern_flag = flag2;
379 vfs_unbusy(mp);
380 vclrflags(vp, VMOUNT);
381 vrele(vp);
382 cache_drop(&nch);
383 goto done;
385 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
387 * Put the new filesystem on the mount list after root. The mount
388 * point gets its own mnt_ncmountpt (unless the VFS already set one
389 * up) which represents the root of the mount. The lookup code
390 * detects the mount point going forward and checks the root of
391 * the mount going backwards.
393 * It is not necessary to invalidate or purge the vnode underneath
394 * because elements under the mount will be given their own glue
395 * namecache record.
397 if (!error) {
398 if (mp->mnt_ncmountpt.ncp == NULL) {
400 * allocate, then unlock, but leave the ref intact
402 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
403 cache_unlock(&mp->mnt_ncmountpt);
405 mp->mnt_ncmounton = nch; /* inherits ref */
406 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
408 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
409 vclrflags(vp, VMOUNT);
410 mountlist_insert(mp, MNTINS_LAST);
411 vn_unlock(vp);
412 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
413 error = vfs_allocate_syncvnode(mp);
414 vfs_unbusy(mp);
415 error = VFS_START(mp, 0);
416 vrele(vp);
417 } else {
418 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
423 vclrflags(vp, VMOUNT);
424 mp->mnt_vfc->vfc_refcount--;
425 vfs_unbusy(mp);
426 kfree(mp, M_MOUNT);
427 cache_drop(&nch);
428 vput(vp);
430 done:
431 rel_mplock();
432 return (error);
436 * Scan all active processes to see if any of them have a current
437 * or root directory onto which the new filesystem has just been
438 * mounted. If so, replace them with the new mount point.
440 * The passed ncp is ref'd and locked (from the mount code) and
441 * must be associated with the vnode representing the root of the
442 * mount point.
444 struct checkdirs_info {
445 struct nchandle old_nch;
446 struct nchandle new_nch;
447 struct vnode *old_vp;
448 struct vnode *new_vp;
451 static int checkdirs_callback(struct proc *p, void *data);
453 static void
454 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
456 struct checkdirs_info info;
457 struct vnode *olddp;
458 struct vnode *newdp;
459 struct mount *mp;
462 * If the old mount point's vnode has a usecount of 1, it is not
463 * being held as a descriptor anywhere.
465 olddp = old_nch->ncp->nc_vp;
466 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
467 return;
470 * Force the root vnode of the new mount point to be resolved
471 * so we can update any matching processes.
473 mp = new_nch->mount;
474 if (VFS_ROOT(mp, &newdp))
475 panic("mount: lost mount");
476 cache_setunresolved(new_nch);
477 cache_setvp(new_nch, newdp);
480 * Special handling of the root node
482 if (rootvnode == olddp) {
483 vref(newdp);
484 vfs_cache_setroot(newdp, cache_hold(new_nch));
488 * Pass newdp separately so the callback does not have to access
489 * it via new_nch->ncp->nc_vp.
491 info.old_nch = *old_nch;
492 info.new_nch = *new_nch;
493 info.new_vp = newdp;
494 allproc_scan(checkdirs_callback, &info);
495 vput(newdp);
499 * NOTE: callback is not MP safe because the scanned process's filedesc
500 * structure can be ripped out from under us, amoung other things.
502 static int
503 checkdirs_callback(struct proc *p, void *data)
505 struct checkdirs_info *info = data;
506 struct filedesc *fdp;
507 struct nchandle ncdrop1;
508 struct nchandle ncdrop2;
509 struct vnode *vprele1;
510 struct vnode *vprele2;
512 if ((fdp = p->p_fd) != NULL) {
513 cache_zero(&ncdrop1);
514 cache_zero(&ncdrop2);
515 vprele1 = NULL;
516 vprele2 = NULL;
519 * MPUNSAFE - XXX fdp can be pulled out from under a
520 * foreign process.
522 * A shared filedesc is ok, we don't have to copy it
523 * because we are making this change globally.
525 spin_lock(&fdp->fd_spin);
526 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
527 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
528 vprele1 = fdp->fd_cdir;
529 vref(info->new_vp);
530 fdp->fd_cdir = info->new_vp;
531 ncdrop1 = fdp->fd_ncdir;
532 cache_copy(&info->new_nch, &fdp->fd_ncdir);
534 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
535 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
536 vprele2 = fdp->fd_rdir;
537 vref(info->new_vp);
538 fdp->fd_rdir = info->new_vp;
539 ncdrop2 = fdp->fd_nrdir;
540 cache_copy(&info->new_nch, &fdp->fd_nrdir);
542 spin_unlock(&fdp->fd_spin);
543 if (ncdrop1.ncp)
544 cache_drop(&ncdrop1);
545 if (ncdrop2.ncp)
546 cache_drop(&ncdrop2);
547 if (vprele1)
548 vrele(vprele1);
549 if (vprele2)
550 vrele(vprele2);
552 return(0);
556 * Unmount a file system.
558 * Note: unmount takes a path to the vnode mounted on as argument,
559 * not special file (as before).
561 * umount_args(char *path, int flags)
563 * MPALMOSTSAFE
566 sys_unmount(struct unmount_args *uap)
568 struct thread *td = curthread;
569 struct proc *p __debugvar = td->td_proc;
570 struct mount *mp = NULL;
571 struct nlookupdata nd;
572 int error;
574 KKASSERT(p);
575 get_mplock();
576 if (td->td_ucred->cr_prison != NULL) {
577 error = EPERM;
578 goto done;
580 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
581 goto done;
583 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
584 if (error == 0)
585 error = nlookup(&nd);
586 if (error)
587 goto out;
589 mp = nd.nl_nch.mount;
592 * Only root, or the user that did the original mount is
593 * permitted to unmount this filesystem.
595 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
596 (error = priv_check(td, PRIV_ROOT)))
597 goto out;
600 * Don't allow unmounting the root file system.
602 if (mp->mnt_flag & MNT_ROOTFS) {
603 error = EINVAL;
604 goto out;
608 * Must be the root of the filesystem
610 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
611 error = EINVAL;
612 goto out;
615 out:
616 nlookup_done(&nd);
617 if (error == 0)
618 error = dounmount(mp, uap->flags);
619 done:
620 rel_mplock();
621 return (error);
625 * Do the actual file system unmount.
627 static int
628 dounmount_interlock(struct mount *mp)
630 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
631 return (EBUSY);
632 mp->mnt_kern_flag |= MNTK_UNMOUNT;
633 return(0);
636 static int
637 unmount_allproc_cb(struct proc *p, void *arg)
639 struct mount *mp;
641 if (p->p_textnch.ncp == NULL)
642 return 0;
644 mp = (struct mount *)arg;
645 if (p->p_textnch.mount == mp)
646 cache_drop(&p->p_textnch);
648 return 0;
652 dounmount(struct mount *mp, int flags)
654 struct namecache *ncp;
655 struct nchandle nch;
656 struct vnode *vp;
657 int error;
658 int async_flag;
659 int lflags;
660 int freeok = 1;
663 * Exclusive access for unmounting purposes
665 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
666 return (error);
669 * Allow filesystems to detect that a forced unmount is in progress.
671 if (flags & MNT_FORCE)
672 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
673 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
674 error = lockmgr(&mp->mnt_lock, lflags);
675 if (error) {
676 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
677 if (mp->mnt_kern_flag & MNTK_MWAIT)
678 wakeup(mp);
679 return (error);
682 if (mp->mnt_flag & MNT_EXPUBLIC)
683 vfs_setpublicfs(NULL, NULL, NULL);
685 vfs_msync(mp, MNT_WAIT);
686 async_flag = mp->mnt_flag & MNT_ASYNC;
687 mp->mnt_flag &=~ MNT_ASYNC;
690 * If this filesystem isn't aliasing other filesystems,
691 * try to invalidate any remaining namecache entries and
692 * check the count afterwords.
694 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
695 cache_lock(&mp->mnt_ncmountpt);
696 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
697 cache_unlock(&mp->mnt_ncmountpt);
699 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
700 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
701 allproc_scan(&unmount_allproc_cb, mp);
704 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
705 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
707 if ((flags & MNT_FORCE) == 0) {
708 error = EBUSY;
709 mount_warning(mp, "Cannot unmount: "
710 "%d namecache "
711 "references still "
712 "present",
713 ncp->nc_refs - 1);
714 } else {
715 mount_warning(mp, "Forced unmount: "
716 "%d namecache "
717 "references still "
718 "present",
719 ncp->nc_refs - 1);
720 freeok = 0;
726 * nchandle records ref the mount structure. Expect a count of 1
727 * (our mount->mnt_ncmountpt).
729 if (mp->mnt_refs != 1) {
730 if ((flags & MNT_FORCE) == 0) {
731 mount_warning(mp, "Cannot unmount: "
732 "%d process references still "
733 "present", mp->mnt_refs);
734 error = EBUSY;
735 } else {
736 mount_warning(mp, "Forced unmount: "
737 "%d process references still "
738 "present", mp->mnt_refs);
739 freeok = 0;
744 * Decomission our special mnt_syncer vnode. This also stops
745 * the vnlru code. If we are unable to unmount we recommission
746 * the vnode.
748 if (error == 0) {
749 if ((vp = mp->mnt_syncer) != NULL) {
750 mp->mnt_syncer = NULL;
751 vrele(vp);
753 if (((mp->mnt_flag & MNT_RDONLY) ||
754 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
755 (flags & MNT_FORCE)) {
756 error = VFS_UNMOUNT(mp, flags);
759 if (error) {
760 if (mp->mnt_syncer == NULL)
761 vfs_allocate_syncvnode(mp);
762 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
763 mp->mnt_flag |= async_flag;
764 lockmgr(&mp->mnt_lock, LK_RELEASE);
765 if (mp->mnt_kern_flag & MNTK_MWAIT)
766 wakeup(mp);
767 return (error);
770 * Clean up any journals still associated with the mount after
771 * filesystem activity has ceased.
773 journal_remove_all_journals(mp,
774 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
776 mountlist_remove(mp);
779 * Remove any installed vnode ops here so the individual VFSs don't
780 * have to.
782 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
788 if (mp->mnt_ncmountpt.ncp != NULL) {
789 nch = mp->mnt_ncmountpt;
790 cache_zero(&mp->mnt_ncmountpt);
791 cache_clrmountpt(&nch);
792 cache_drop(&nch);
794 if (mp->mnt_ncmounton.ncp != NULL) {
795 nch = mp->mnt_ncmounton;
796 cache_zero(&mp->mnt_ncmounton);
797 cache_clrmountpt(&nch);
798 cache_drop(&nch);
801 mp->mnt_vfc->vfc_refcount--;
802 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
803 panic("unmount: dangling vnode");
804 lockmgr(&mp->mnt_lock, LK_RELEASE);
805 if (mp->mnt_kern_flag & MNTK_MWAIT)
806 wakeup(mp);
807 if (freeok)
808 kfree(mp, M_MOUNT);
809 return (0);
812 static
813 void
814 mount_warning(struct mount *mp, const char *ctl, ...)
816 char *ptr;
817 char *buf;
818 __va_list va;
820 __va_start(va, ctl);
821 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
822 kprintf("unmount(%s): ", ptr);
823 kvprintf(ctl, va);
824 kprintf("\n");
825 kfree(buf, M_TEMP);
826 } else {
827 kprintf("unmount(%p", mp);
828 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
829 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
830 kprintf("): ");
831 kvprintf(ctl, va);
832 kprintf("\n");
834 __va_end(va);
838 * Shim cache_fullpath() to handle the case where a process is chrooted into
839 * a subdirectory of a mount. In this case if the root mount matches the
840 * process root directory's mount we have to specify the process's root
841 * directory instead of the mount point, because the mount point might
842 * be above the root directory.
844 static
846 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
848 struct nchandle *nch;
850 if (p && p->p_fd->fd_nrdir.mount == mp)
851 nch = &p->p_fd->fd_nrdir;
852 else
853 nch = &mp->mnt_ncmountpt;
854 return(cache_fullpath(p, nch, rb, fb, 0));
858 * Sync each mounted filesystem.
861 #ifdef DEBUG
862 static int syncprt = 0;
863 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
864 #endif /* DEBUG */
866 static int sync_callback(struct mount *mp, void *data);
869 * MPALMOSTSAFE
872 sys_sync(struct sync_args *uap)
874 get_mplock();
875 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
876 #ifdef DEBUG
878 * print out buffer pool stat information on each sync() call.
880 if (syncprt)
881 vfs_bufstats();
882 #endif /* DEBUG */
883 rel_mplock();
884 return (0);
887 static
889 sync_callback(struct mount *mp, void *data __unused)
891 int asyncflag;
893 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
894 asyncflag = mp->mnt_flag & MNT_ASYNC;
895 mp->mnt_flag &= ~MNT_ASYNC;
896 vfs_msync(mp, MNT_NOWAIT);
897 VFS_SYNC(mp, MNT_NOWAIT);
898 mp->mnt_flag |= asyncflag;
900 return(0);
903 /* XXX PRISON: could be per prison flag */
904 static int prison_quotas;
905 #if 0
906 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
907 #endif
910 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
912 * Change filesystem quotas.
914 * MPALMOSTSAFE
917 sys_quotactl(struct quotactl_args *uap)
919 struct nlookupdata nd;
920 struct thread *td;
921 struct proc *p;
922 struct mount *mp;
923 int error;
925 get_mplock();
926 td = curthread;
927 p = td->td_proc;
928 if (td->td_ucred->cr_prison && !prison_quotas) {
929 error = EPERM;
930 goto done;
933 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
934 if (error == 0)
935 error = nlookup(&nd);
936 if (error == 0) {
937 mp = nd.nl_nch.mount;
938 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
939 uap->arg, nd.nl_cred);
941 nlookup_done(&nd);
942 done:
943 rel_mplock();
944 return (error);
948 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
949 * void *buf, int buflen)
951 * This function operates on a mount point and executes the specified
952 * operation using the specified control data, and possibly returns data.
954 * The actual number of bytes stored in the result buffer is returned, 0
955 * if none, otherwise an error is returned.
957 * MPALMOSTSAFE
960 sys_mountctl(struct mountctl_args *uap)
962 struct thread *td = curthread;
963 struct proc *p = td->td_proc;
964 struct file *fp;
965 void *ctl = NULL;
966 void *buf = NULL;
967 char *path = NULL;
968 int error;
971 * Sanity and permissions checks. We must be root.
973 KKASSERT(p);
974 if (td->td_ucred->cr_prison != NULL)
975 return (EPERM);
976 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
977 (error = priv_check(td, PRIV_ROOT)) != 0)
978 return (error);
981 * Argument length checks
983 if (uap->ctllen < 0 || uap->ctllen > 1024)
984 return (EINVAL);
985 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
986 return (EINVAL);
987 if (uap->path == NULL)
988 return (EINVAL);
991 * Allocate the necessary buffers and copyin data
993 path = objcache_get(namei_oc, M_WAITOK);
994 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
995 if (error)
996 goto done;
998 if (uap->ctllen) {
999 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1000 error = copyin(uap->ctl, ctl, uap->ctllen);
1001 if (error)
1002 goto done;
1004 if (uap->buflen)
1005 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1008 * Validate the descriptor
1010 if (uap->fd >= 0) {
1011 fp = holdfp(p->p_fd, uap->fd, -1);
1012 if (fp == NULL) {
1013 error = EBADF;
1014 goto done;
1016 } else {
1017 fp = NULL;
1021 * Execute the internal kernel function and clean up.
1023 get_mplock();
1024 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1025 rel_mplock();
1026 if (fp)
1027 fdrop(fp);
1028 if (error == 0 && uap->sysmsg_result > 0)
1029 error = copyout(buf, uap->buf, uap->sysmsg_result);
1030 done:
1031 if (path)
1032 objcache_put(namei_oc, path);
1033 if (ctl)
1034 kfree(ctl, M_TEMP);
1035 if (buf)
1036 kfree(buf, M_TEMP);
1037 return (error);
1041 * Execute a mount control operation by resolving the path to a mount point
1042 * and calling vop_mountctl().
1044 * Use the mount point from the nch instead of the vnode so nullfs mounts
1045 * can properly spike the VOP.
1048 kern_mountctl(const char *path, int op, struct file *fp,
1049 const void *ctl, int ctllen,
1050 void *buf, int buflen, int *res)
1052 struct vnode *vp;
1053 struct mount *mp;
1054 struct nlookupdata nd;
1055 int error;
1057 *res = 0;
1058 vp = NULL;
1059 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1060 if (error == 0)
1061 error = nlookup(&nd);
1062 if (error == 0)
1063 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1064 mp = nd.nl_nch.mount;
1065 nlookup_done(&nd);
1066 if (error)
1067 return (error);
1068 vn_unlock(vp);
1071 * Must be the root of the filesystem
1073 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1074 vrele(vp);
1075 return (EINVAL);
1077 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1078 buf, buflen, res);
1079 vrele(vp);
1080 return (error);
1084 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1086 struct thread *td = curthread;
1087 struct proc *p = td->td_proc;
1088 struct mount *mp;
1089 struct statfs *sp;
1090 char *fullpath, *freepath;
1091 int error;
1093 if ((error = nlookup(nd)) != 0)
1094 return (error);
1095 mp = nd->nl_nch.mount;
1096 sp = &mp->mnt_stat;
1097 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1098 return (error);
1100 error = mount_path(p, mp, &fullpath, &freepath);
1101 if (error)
1102 return(error);
1103 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1104 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1105 kfree(freepath, M_TEMP);
1107 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1108 bcopy(sp, buf, sizeof(*buf));
1109 /* Only root should have access to the fsid's. */
1110 if (priv_check(td, PRIV_ROOT))
1111 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1112 return (0);
1116 * statfs_args(char *path, struct statfs *buf)
1118 * Get filesystem statistics.
1120 * MPALMOSTSAFE
1123 sys_statfs(struct statfs_args *uap)
1125 struct nlookupdata nd;
1126 struct statfs buf;
1127 int error;
1129 get_mplock();
1130 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1131 if (error == 0)
1132 error = kern_statfs(&nd, &buf);
1133 nlookup_done(&nd);
1134 if (error == 0)
1135 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1136 rel_mplock();
1137 return (error);
1141 * MPALMOSTSAFE
1144 kern_fstatfs(int fd, struct statfs *buf)
1146 struct thread *td = curthread;
1147 struct proc *p = td->td_proc;
1148 struct file *fp;
1149 struct mount *mp;
1150 struct statfs *sp;
1151 char *fullpath, *freepath;
1152 int error;
1154 KKASSERT(p);
1155 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1156 return (error);
1157 get_mplock();
1158 mp = ((struct vnode *)fp->f_data)->v_mount;
1159 if (mp == NULL) {
1160 error = EBADF;
1161 goto done;
1163 if (fp->f_cred == NULL) {
1164 error = EINVAL;
1165 goto done;
1167 sp = &mp->mnt_stat;
1168 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1169 goto done;
1171 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1172 goto done;
1173 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1174 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1175 kfree(freepath, M_TEMP);
1177 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1178 bcopy(sp, buf, sizeof(*buf));
1180 /* Only root should have access to the fsid's. */
1181 if (priv_check(td, PRIV_ROOT))
1182 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1183 error = 0;
1184 done:
1185 rel_mplock();
1186 fdrop(fp);
1187 return (error);
1191 * fstatfs_args(int fd, struct statfs *buf)
1193 * Get filesystem statistics.
1195 * MPSAFE
1198 sys_fstatfs(struct fstatfs_args *uap)
1200 struct statfs buf;
1201 int error;
1203 error = kern_fstatfs(uap->fd, &buf);
1205 if (error == 0)
1206 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1207 return (error);
1211 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1213 struct mount *mp;
1214 struct statvfs *sp;
1215 int error;
1217 if ((error = nlookup(nd)) != 0)
1218 return (error);
1219 mp = nd->nl_nch.mount;
1220 sp = &mp->mnt_vstat;
1221 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1222 return (error);
1224 sp->f_flag = 0;
1225 if (mp->mnt_flag & MNT_RDONLY)
1226 sp->f_flag |= ST_RDONLY;
1227 if (mp->mnt_flag & MNT_NOSUID)
1228 sp->f_flag |= ST_NOSUID;
1229 bcopy(sp, buf, sizeof(*buf));
1230 return (0);
1234 * statfs_args(char *path, struct statfs *buf)
1236 * Get filesystem statistics.
1238 * MPALMOSTSAFE
1241 sys_statvfs(struct statvfs_args *uap)
1243 struct nlookupdata nd;
1244 struct statvfs buf;
1245 int error;
1247 get_mplock();
1248 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1249 if (error == 0)
1250 error = kern_statvfs(&nd, &buf);
1251 nlookup_done(&nd);
1252 if (error == 0)
1253 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1254 rel_mplock();
1255 return (error);
1259 kern_fstatvfs(int fd, struct statvfs *buf)
1261 struct thread *td = curthread;
1262 struct proc *p = td->td_proc;
1263 struct file *fp;
1264 struct mount *mp;
1265 struct statvfs *sp;
1266 int error;
1268 KKASSERT(p);
1269 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1270 return (error);
1271 mp = ((struct vnode *)fp->f_data)->v_mount;
1272 if (mp == NULL) {
1273 error = EBADF;
1274 goto done;
1276 if (fp->f_cred == NULL) {
1277 error = EINVAL;
1278 goto done;
1280 sp = &mp->mnt_vstat;
1281 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1282 goto done;
1284 sp->f_flag = 0;
1285 if (mp->mnt_flag & MNT_RDONLY)
1286 sp->f_flag |= ST_RDONLY;
1287 if (mp->mnt_flag & MNT_NOSUID)
1288 sp->f_flag |= ST_NOSUID;
1290 bcopy(sp, buf, sizeof(*buf));
1291 error = 0;
1292 done:
1293 fdrop(fp);
1294 return (error);
1298 * fstatfs_args(int fd, struct statfs *buf)
1300 * Get filesystem statistics.
1302 * MPALMOSTSAFE
1305 sys_fstatvfs(struct fstatvfs_args *uap)
1307 struct statvfs buf;
1308 int error;
1310 get_mplock();
1311 error = kern_fstatvfs(uap->fd, &buf);
1312 rel_mplock();
1314 if (error == 0)
1315 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1316 return (error);
1320 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1322 * Get statistics on all filesystems.
1325 struct getfsstat_info {
1326 struct statfs *sfsp;
1327 long count;
1328 long maxcount;
1329 int error;
1330 int flags;
1331 struct thread *td;
1334 static int getfsstat_callback(struct mount *, void *);
1337 * MPALMOSTSAFE
1340 sys_getfsstat(struct getfsstat_args *uap)
1342 struct thread *td = curthread;
1343 struct getfsstat_info info;
1345 bzero(&info, sizeof(info));
1347 info.maxcount = uap->bufsize / sizeof(struct statfs);
1348 info.sfsp = uap->buf;
1349 info.count = 0;
1350 info.flags = uap->flags;
1351 info.td = td;
1353 get_mplock();
1354 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1355 rel_mplock();
1356 if (info.sfsp && info.count > info.maxcount)
1357 uap->sysmsg_result = info.maxcount;
1358 else
1359 uap->sysmsg_result = info.count;
1360 return (info.error);
1363 static int
1364 getfsstat_callback(struct mount *mp, void *data)
1366 struct getfsstat_info *info = data;
1367 struct statfs *sp;
1368 char *freepath;
1369 char *fullpath;
1370 int error;
1372 if (info->sfsp && info->count < info->maxcount) {
1373 if (info->td->td_proc &&
1374 !chroot_visible_mnt(mp, info->td->td_proc)) {
1375 return(0);
1377 sp = &mp->mnt_stat;
1380 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1381 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1382 * overrides MNT_WAIT.
1384 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1385 (info->flags & MNT_WAIT)) &&
1386 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1387 return(0);
1389 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1391 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1392 if (error) {
1393 info->error = error;
1394 return(-1);
1396 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1397 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1398 kfree(freepath, M_TEMP);
1400 error = copyout(sp, info->sfsp, sizeof(*sp));
1401 if (error) {
1402 info->error = error;
1403 return (-1);
1405 ++info->sfsp;
1407 info->count++;
1408 return(0);
1412 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1413 long bufsize, int flags)
1415 * Get statistics on all filesystems.
1418 struct getvfsstat_info {
1419 struct statfs *sfsp;
1420 struct statvfs *vsfsp;
1421 long count;
1422 long maxcount;
1423 int error;
1424 int flags;
1425 struct thread *td;
1428 static int getvfsstat_callback(struct mount *, void *);
1431 * MPALMOSTSAFE
1434 sys_getvfsstat(struct getvfsstat_args *uap)
1436 struct thread *td = curthread;
1437 struct getvfsstat_info info;
1439 bzero(&info, sizeof(info));
1441 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1442 info.sfsp = uap->buf;
1443 info.vsfsp = uap->vbuf;
1444 info.count = 0;
1445 info.flags = uap->flags;
1446 info.td = td;
1448 get_mplock();
1449 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1450 if (info.vsfsp && info.count > info.maxcount)
1451 uap->sysmsg_result = info.maxcount;
1452 else
1453 uap->sysmsg_result = info.count;
1454 rel_mplock();
1455 return (info.error);
1458 static int
1459 getvfsstat_callback(struct mount *mp, void *data)
1461 struct getvfsstat_info *info = data;
1462 struct statfs *sp;
1463 struct statvfs *vsp;
1464 char *freepath;
1465 char *fullpath;
1466 int error;
1468 if (info->vsfsp && info->count < info->maxcount) {
1469 if (info->td->td_proc &&
1470 !chroot_visible_mnt(mp, info->td->td_proc)) {
1471 return(0);
1473 sp = &mp->mnt_stat;
1474 vsp = &mp->mnt_vstat;
1477 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1478 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1479 * overrides MNT_WAIT.
1481 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1482 (info->flags & MNT_WAIT)) &&
1483 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1484 return(0);
1486 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1488 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1489 (info->flags & MNT_WAIT)) &&
1490 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1491 return(0);
1493 vsp->f_flag = 0;
1494 if (mp->mnt_flag & MNT_RDONLY)
1495 vsp->f_flag |= ST_RDONLY;
1496 if (mp->mnt_flag & MNT_NOSUID)
1497 vsp->f_flag |= ST_NOSUID;
1499 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1500 if (error) {
1501 info->error = error;
1502 return(-1);
1504 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1505 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1506 kfree(freepath, M_TEMP);
1508 error = copyout(sp, info->sfsp, sizeof(*sp));
1509 if (error == 0)
1510 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1511 if (error) {
1512 info->error = error;
1513 return (-1);
1515 ++info->sfsp;
1516 ++info->vsfsp;
1518 info->count++;
1519 return(0);
1524 * fchdir_args(int fd)
1526 * Change current working directory to a given file descriptor.
1528 * MPALMOSTSAFE
1531 sys_fchdir(struct fchdir_args *uap)
1533 struct thread *td = curthread;
1534 struct proc *p = td->td_proc;
1535 struct filedesc *fdp = p->p_fd;
1536 struct vnode *vp, *ovp;
1537 struct mount *mp;
1538 struct file *fp;
1539 struct nchandle nch, onch, tnch;
1540 int error;
1542 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1543 return (error);
1544 get_mplock();
1545 vp = (struct vnode *)fp->f_data;
1546 vref(vp);
1547 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1548 if (fp->f_nchandle.ncp == NULL)
1549 error = ENOTDIR;
1550 else
1551 error = checkvp_chdir(vp, td);
1552 if (error) {
1553 vput(vp);
1554 goto done;
1556 cache_copy(&fp->f_nchandle, &nch);
1559 * If the ncp has become a mount point, traverse through
1560 * the mount point.
1563 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1564 (mp = cache_findmount(&nch)) != NULL
1566 error = nlookup_mp(mp, &tnch);
1567 if (error == 0) {
1568 cache_unlock(&tnch); /* leave ref intact */
1569 vput(vp);
1570 vp = tnch.ncp->nc_vp;
1571 error = vget(vp, LK_SHARED);
1572 KKASSERT(error == 0);
1573 cache_drop(&nch);
1574 nch = tnch;
1577 if (error == 0) {
1578 ovp = fdp->fd_cdir;
1579 onch = fdp->fd_ncdir;
1580 vn_unlock(vp); /* leave ref intact */
1581 fdp->fd_cdir = vp;
1582 fdp->fd_ncdir = nch;
1583 cache_drop(&onch);
1584 vrele(ovp);
1585 } else {
1586 cache_drop(&nch);
1587 vput(vp);
1589 fdrop(fp);
1590 done:
1591 rel_mplock();
1592 return (error);
1596 kern_chdir(struct nlookupdata *nd)
1598 struct thread *td = curthread;
1599 struct proc *p = td->td_proc;
1600 struct filedesc *fdp = p->p_fd;
1601 struct vnode *vp, *ovp;
1602 struct nchandle onch;
1603 int error;
1605 if ((error = nlookup(nd)) != 0)
1606 return (error);
1607 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1608 return (ENOENT);
1609 if ((error = vget(vp, LK_SHARED)) != 0)
1610 return (error);
1612 error = checkvp_chdir(vp, td);
1613 vn_unlock(vp);
1614 if (error == 0) {
1615 ovp = fdp->fd_cdir;
1616 onch = fdp->fd_ncdir;
1617 cache_unlock(&nd->nl_nch); /* leave reference intact */
1618 fdp->fd_ncdir = nd->nl_nch;
1619 fdp->fd_cdir = vp;
1620 cache_drop(&onch);
1621 vrele(ovp);
1622 cache_zero(&nd->nl_nch);
1623 } else {
1624 vrele(vp);
1626 return (error);
1630 * chdir_args(char *path)
1632 * Change current working directory (``.'').
1634 * MPALMOSTSAFE
1637 sys_chdir(struct chdir_args *uap)
1639 struct nlookupdata nd;
1640 int error;
1642 get_mplock();
1643 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1644 if (error == 0)
1645 error = kern_chdir(&nd);
1646 nlookup_done(&nd);
1647 rel_mplock();
1648 return (error);
1652 * Helper function for raised chroot(2) security function: Refuse if
1653 * any filedescriptors are open directories.
1655 static int
1656 chroot_refuse_vdir_fds(struct filedesc *fdp)
1658 struct vnode *vp;
1659 struct file *fp;
1660 int error;
1661 int fd;
1663 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1664 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1665 continue;
1666 vp = (struct vnode *)fp->f_data;
1667 if (vp->v_type != VDIR) {
1668 fdrop(fp);
1669 continue;
1671 fdrop(fp);
1672 return(EPERM);
1674 return (0);
1678 * This sysctl determines if we will allow a process to chroot(2) if it
1679 * has a directory open:
1680 * 0: disallowed for all processes.
1681 * 1: allowed for processes that were not already chroot(2)'ed.
1682 * 2: allowed for all processes.
1685 static int chroot_allow_open_directories = 1;
1687 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1688 &chroot_allow_open_directories, 0, "");
1691 * chroot to the specified namecache entry. We obtain the vp from the
1692 * namecache data. The passed ncp must be locked and referenced and will
1693 * remain locked and referenced on return.
1696 kern_chroot(struct nchandle *nch)
1698 struct thread *td = curthread;
1699 struct proc *p = td->td_proc;
1700 struct filedesc *fdp = p->p_fd;
1701 struct vnode *vp;
1702 int error;
1705 * Only privileged user can chroot
1707 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1708 if (error)
1709 return (error);
1712 * Disallow open directory descriptors (fchdir() breakouts).
1714 if (chroot_allow_open_directories == 0 ||
1715 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1716 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1717 return (error);
1719 if ((vp = nch->ncp->nc_vp) == NULL)
1720 return (ENOENT);
1722 if ((error = vget(vp, LK_SHARED)) != 0)
1723 return (error);
1726 * Check the validity of vp as a directory to change to and
1727 * associate it with rdir/jdir.
1729 error = checkvp_chdir(vp, td);
1730 vn_unlock(vp); /* leave reference intact */
1731 if (error == 0) {
1732 vrele(fdp->fd_rdir);
1733 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1734 cache_drop(&fdp->fd_nrdir);
1735 cache_copy(nch, &fdp->fd_nrdir);
1736 if (fdp->fd_jdir == NULL) {
1737 fdp->fd_jdir = vp;
1738 vref(fdp->fd_jdir);
1739 cache_copy(nch, &fdp->fd_njdir);
1741 } else {
1742 vrele(vp);
1744 return (error);
1748 * chroot_args(char *path)
1750 * Change notion of root (``/'') directory.
1752 * MPALMOSTSAFE
1755 sys_chroot(struct chroot_args *uap)
1757 struct thread *td __debugvar = curthread;
1758 struct nlookupdata nd;
1759 int error;
1761 KKASSERT(td->td_proc);
1762 get_mplock();
1763 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1764 if (error == 0) {
1765 nd.nl_flags |= NLC_EXEC;
1766 error = nlookup(&nd);
1767 if (error == 0)
1768 error = kern_chroot(&nd.nl_nch);
1770 nlookup_done(&nd);
1771 rel_mplock();
1772 return(error);
1776 sys_chroot_kernel(struct chroot_kernel_args *uap)
1778 struct thread *td = curthread;
1779 struct nlookupdata nd;
1780 struct nchandle *nch;
1781 struct vnode *vp;
1782 int error;
1784 get_mplock();
1785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1786 if (error)
1787 goto error_nond;
1789 error = nlookup(&nd);
1790 if (error)
1791 goto error_out;
1793 nch = &nd.nl_nch;
1795 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1796 if (error)
1797 goto error_out;
1799 if ((vp = nch->ncp->nc_vp) == NULL) {
1800 error = ENOENT;
1801 goto error_out;
1804 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1805 goto error_out;
1807 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1808 vfs_cache_setroot(vp, cache_hold(nch));
1810 error_out:
1811 nlookup_done(&nd);
1812 error_nond:
1813 rel_mplock();
1814 return(error);
1818 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1819 * determine whether it is legal to chdir to the vnode. The vnode's state
1820 * is not changed by this call.
1823 checkvp_chdir(struct vnode *vp, struct thread *td)
1825 int error;
1827 if (vp->v_type != VDIR)
1828 error = ENOTDIR;
1829 else
1830 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1831 return (error);
1835 * MPSAFE
1838 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1840 struct thread *td = curthread;
1841 struct proc *p = td->td_proc;
1842 struct lwp *lp = td->td_lwp;
1843 struct filedesc *fdp = p->p_fd;
1844 int cmode, flags;
1845 struct file *nfp;
1846 struct file *fp;
1847 struct vnode *vp;
1848 int type, indx, error;
1849 struct flock lf;
1851 if ((oflags & O_ACCMODE) == O_ACCMODE)
1852 return (EINVAL);
1853 flags = FFLAGS(oflags);
1854 error = falloc(lp, &nfp, NULL);
1855 if (error)
1856 return (error);
1857 fp = nfp;
1858 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1861 * XXX p_dupfd is a real mess. It allows a device to return a
1862 * file descriptor to be duplicated rather then doing the open
1863 * itself.
1865 lp->lwp_dupfd = -1;
1868 * Call vn_open() to do the lookup and assign the vnode to the
1869 * file pointer. vn_open() does not change the ref count on fp
1870 * and the vnode, on success, will be inherited by the file pointer
1871 * and unlocked.
1873 nd->nl_flags |= NLC_LOCKVP;
1874 error = vn_open(nd, fp, flags, cmode);
1875 nlookup_done(nd);
1876 if (error) {
1878 * handle special fdopen() case. bleh. dupfdopen() is
1879 * responsible for dropping the old contents of ofiles[indx]
1880 * if it succeeds.
1882 * Note that fsetfd() will add a ref to fp which represents
1883 * the fd_files[] assignment. We must still drop our
1884 * reference.
1886 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1887 if (fdalloc(p, 0, &indx) == 0) {
1888 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1889 if (error == 0) {
1890 *res = indx;
1891 fdrop(fp); /* our ref */
1892 return (0);
1894 fsetfd(fdp, NULL, indx);
1897 fdrop(fp); /* our ref */
1898 if (error == ERESTART)
1899 error = EINTR;
1900 return (error);
1904 * ref the vnode for ourselves so it can't be ripped out from under
1905 * is. XXX need an ND flag to request that the vnode be returned
1906 * anyway.
1908 * Reserve a file descriptor but do not assign it until the open
1909 * succeeds.
1911 vp = (struct vnode *)fp->f_data;
1912 vref(vp);
1913 if ((error = fdalloc(p, 0, &indx)) != 0) {
1914 fdrop(fp);
1915 vrele(vp);
1916 return (error);
1920 * If no error occurs the vp will have been assigned to the file
1921 * pointer.
1923 lp->lwp_dupfd = 0;
1925 if (flags & (O_EXLOCK | O_SHLOCK)) {
1926 lf.l_whence = SEEK_SET;
1927 lf.l_start = 0;
1928 lf.l_len = 0;
1929 if (flags & O_EXLOCK)
1930 lf.l_type = F_WRLCK;
1931 else
1932 lf.l_type = F_RDLCK;
1933 if (flags & FNONBLOCK)
1934 type = 0;
1935 else
1936 type = F_WAIT;
1938 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1940 * lock request failed. Clean up the reserved
1941 * descriptor.
1943 vrele(vp);
1944 fsetfd(fdp, NULL, indx);
1945 fdrop(fp);
1946 return (error);
1948 fp->f_flag |= FHASLOCK;
1950 #if 0
1952 * Assert that all regular file vnodes were created with a object.
1954 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1955 ("open: regular file has no backing object after vn_open"));
1956 #endif
1958 vrele(vp);
1961 * release our private reference, leaving the one associated with the
1962 * descriptor table intact.
1964 fsetfd(fdp, fp, indx);
1965 fdrop(fp);
1966 *res = indx;
1967 return (0);
1971 * open_args(char *path, int flags, int mode)
1973 * Check permissions, allocate an open file structure,
1974 * and call the device open routine if any.
1976 * MPALMOSTSAFE
1979 sys_open(struct open_args *uap)
1981 CACHE_MPLOCK_DECLARE;
1982 struct nlookupdata nd;
1983 int error;
1985 CACHE_GETMPLOCK1();
1986 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1987 if (error == 0) {
1988 error = kern_open(&nd, uap->flags,
1989 uap->mode, &uap->sysmsg_result);
1991 nlookup_done(&nd);
1992 CACHE_RELMPLOCK();
1993 return (error);
1997 * openat_args(int fd, char *path, int flags, int mode)
1999 * MPALMOSTSAFE
2002 sys_openat(struct openat_args *uap)
2004 CACHE_MPLOCK_DECLARE;
2005 struct nlookupdata nd;
2006 int error;
2007 struct file *fp;
2009 CACHE_GETMPLOCK1();
2010 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2011 if (error == 0) {
2012 error = kern_open(&nd, uap->flags, uap->mode,
2013 &uap->sysmsg_result);
2015 nlookup_done_at(&nd, fp);
2016 CACHE_RELMPLOCK();
2017 return (error);
2021 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2023 struct thread *td = curthread;
2024 struct proc *p = td->td_proc;
2025 struct vnode *vp;
2026 struct vattr vattr;
2027 int error;
2028 int whiteout = 0;
2030 KKASSERT(p);
2032 VATTR_NULL(&vattr);
2033 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2034 vattr.va_rmajor = rmajor;
2035 vattr.va_rminor = rminor;
2037 switch (mode & S_IFMT) {
2038 case S_IFMT: /* used by badsect to flag bad sectors */
2039 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2040 vattr.va_type = VBAD;
2041 break;
2042 case S_IFCHR:
2043 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2044 vattr.va_type = VCHR;
2045 break;
2046 case S_IFBLK:
2047 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2048 vattr.va_type = VBLK;
2049 break;
2050 case S_IFWHT:
2051 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2052 whiteout = 1;
2053 break;
2054 case S_IFDIR: /* special directories support for HAMMER */
2055 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2056 vattr.va_type = VDIR;
2057 break;
2058 default:
2059 error = EINVAL;
2060 break;
2063 if (error)
2064 return (error);
2066 bwillinode(1);
2067 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2068 if ((error = nlookup(nd)) != 0)
2069 return (error);
2070 if (nd->nl_nch.ncp->nc_vp)
2071 return (EEXIST);
2072 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2073 return (error);
2075 if (whiteout) {
2076 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2077 nd->nl_cred, NAMEI_CREATE);
2078 } else {
2079 vp = NULL;
2080 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2081 &vp, nd->nl_cred, &vattr);
2082 if (error == 0)
2083 vput(vp);
2085 return (error);
2089 * mknod_args(char *path, int mode, int dev)
2091 * Create a special file.
2093 * MPALMOSTSAFE
2096 sys_mknod(struct mknod_args *uap)
2098 struct nlookupdata nd;
2099 int error;
2101 get_mplock();
2102 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2103 if (error == 0) {
2104 error = kern_mknod(&nd, uap->mode,
2105 umajor(uap->dev), uminor(uap->dev));
2107 nlookup_done(&nd);
2108 rel_mplock();
2109 return (error);
2113 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2115 * Create a special file. The path is relative to the directory associated
2116 * with fd.
2118 * MPALMOSTSAFE
2121 sys_mknodat(struct mknodat_args *uap)
2123 struct nlookupdata nd;
2124 struct file *fp;
2125 int error;
2127 get_mplock();
2128 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2129 if (error == 0) {
2130 error = kern_mknod(&nd, uap->mode,
2131 umajor(uap->dev), uminor(uap->dev));
2133 nlookup_done_at(&nd, fp);
2134 rel_mplock();
2135 return (error);
2139 kern_mkfifo(struct nlookupdata *nd, int mode)
2141 struct thread *td = curthread;
2142 struct proc *p = td->td_proc;
2143 struct vattr vattr;
2144 struct vnode *vp;
2145 int error;
2147 bwillinode(1);
2149 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2150 if ((error = nlookup(nd)) != 0)
2151 return (error);
2152 if (nd->nl_nch.ncp->nc_vp)
2153 return (EEXIST);
2154 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2155 return (error);
2157 VATTR_NULL(&vattr);
2158 vattr.va_type = VFIFO;
2159 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2160 vp = NULL;
2161 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2162 if (error == 0)
2163 vput(vp);
2164 return (error);
2168 * mkfifo_args(char *path, int mode)
2170 * Create a named pipe.
2172 * MPALMOSTSAFE
2175 sys_mkfifo(struct mkfifo_args *uap)
2177 struct nlookupdata nd;
2178 int error;
2180 get_mplock();
2181 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2182 if (error == 0)
2183 error = kern_mkfifo(&nd, uap->mode);
2184 nlookup_done(&nd);
2185 rel_mplock();
2186 return (error);
2190 * mkfifoat_args(int fd, char *path, mode_t mode)
2192 * Create a named pipe. The path is relative to the directory associated
2193 * with fd.
2195 * MPALMOSTSAFE
2198 sys_mkfifoat(struct mkfifoat_args *uap)
2200 struct nlookupdata nd;
2201 struct file *fp;
2202 int error;
2204 get_mplock();
2205 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2206 if (error == 0)
2207 error = kern_mkfifo(&nd, uap->mode);
2208 nlookup_done_at(&nd, fp);
2209 rel_mplock();
2210 return (error);
2213 static int hardlink_check_uid = 0;
2214 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2215 &hardlink_check_uid, 0,
2216 "Unprivileged processes cannot create hard links to files owned by other "
2217 "users");
2218 static int hardlink_check_gid = 0;
2219 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2220 &hardlink_check_gid, 0,
2221 "Unprivileged processes cannot create hard links to files owned by other "
2222 "groups");
2224 static int
2225 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2227 struct vattr va;
2228 int error;
2231 * Shortcut if disabled
2233 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2234 return (0);
2237 * Privileged user can always hardlink
2239 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2240 return (0);
2243 * Otherwise only if the originating file is owned by the
2244 * same user or group. Note that any group is allowed if
2245 * the file is owned by the caller.
2247 error = VOP_GETATTR(vp, &va);
2248 if (error != 0)
2249 return (error);
2251 if (hardlink_check_uid) {
2252 if (cred->cr_uid != va.va_uid)
2253 return (EPERM);
2256 if (hardlink_check_gid) {
2257 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2258 return (EPERM);
2261 return (0);
2265 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2267 struct thread *td = curthread;
2268 struct vnode *vp;
2269 int error;
2272 * Lookup the source and obtained a locked vnode.
2274 * You may only hardlink a file which you have write permission
2275 * on or which you own.
2277 * XXX relookup on vget failure / race ?
2279 bwillinode(1);
2280 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2281 if ((error = nlookup(nd)) != 0)
2282 return (error);
2283 vp = nd->nl_nch.ncp->nc_vp;
2284 KKASSERT(vp != NULL);
2285 if (vp->v_type == VDIR)
2286 return (EPERM); /* POSIX */
2287 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2288 return (error);
2289 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2290 return (error);
2293 * Unlock the source so we can lookup the target without deadlocking
2294 * (XXX vp is locked already, possible other deadlock?). The target
2295 * must not exist.
2297 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2298 nd->nl_flags &= ~NLC_NCPISLOCKED;
2299 cache_unlock(&nd->nl_nch);
2300 vn_unlock(vp);
2302 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2303 if ((error = nlookup(linknd)) != 0) {
2304 vrele(vp);
2305 return (error);
2307 if (linknd->nl_nch.ncp->nc_vp) {
2308 vrele(vp);
2309 return (EEXIST);
2311 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2312 vrele(vp);
2313 return (error);
2317 * Finally run the new API VOP.
2319 error = can_hardlink(vp, td, td->td_ucred);
2320 if (error == 0) {
2321 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2322 vp, linknd->nl_cred);
2324 vput(vp);
2325 return (error);
2329 * link_args(char *path, char *link)
2331 * Make a hard file link.
2333 * MPALMOSTSAFE
2336 sys_link(struct link_args *uap)
2338 struct nlookupdata nd, linknd;
2339 int error;
2341 get_mplock();
2342 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2343 if (error == 0) {
2344 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2345 if (error == 0)
2346 error = kern_link(&nd, &linknd);
2347 nlookup_done(&linknd);
2349 nlookup_done(&nd);
2350 rel_mplock();
2351 return (error);
2355 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2357 struct vattr vattr;
2358 struct vnode *vp;
2359 struct vnode *dvp;
2360 int error;
2362 bwillinode(1);
2363 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2364 if ((error = nlookup(nd)) != 0)
2365 return (error);
2366 if (nd->nl_nch.ncp->nc_vp)
2367 return (EEXIST);
2368 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2369 return (error);
2370 dvp = nd->nl_dvp;
2371 VATTR_NULL(&vattr);
2372 vattr.va_mode = mode;
2373 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2374 if (error == 0)
2375 vput(vp);
2376 return (error);
2380 * symlink(char *path, char *link)
2382 * Make a symbolic link.
2384 * MPALMOSTSAFE
2387 sys_symlink(struct symlink_args *uap)
2389 struct thread *td = curthread;
2390 struct nlookupdata nd;
2391 char *path;
2392 int error;
2393 int mode;
2395 path = objcache_get(namei_oc, M_WAITOK);
2396 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2397 if (error == 0) {
2398 get_mplock();
2399 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2400 if (error == 0) {
2401 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2402 error = kern_symlink(&nd, path, mode);
2404 nlookup_done(&nd);
2405 rel_mplock();
2407 objcache_put(namei_oc, path);
2408 return (error);
2412 * symlinkat_args(char *path1, int fd, char *path2)
2414 * Make a symbolic link. The path2 argument is relative to the directory
2415 * associated with fd.
2417 * MPALMOSTSAFE
2420 sys_symlinkat(struct symlinkat_args *uap)
2422 struct thread *td = curthread;
2423 struct nlookupdata nd;
2424 struct file *fp;
2425 char *path1;
2426 int error;
2427 int mode;
2429 path1 = objcache_get(namei_oc, M_WAITOK);
2430 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2431 if (error == 0) {
2432 get_mplock();
2433 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2434 UIO_USERSPACE, 0);
2435 if (error == 0) {
2436 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2437 error = kern_symlink(&nd, path1, mode);
2439 nlookup_done_at(&nd, fp);
2440 rel_mplock();
2442 objcache_put(namei_oc, path1);
2443 return (error);
2447 * undelete_args(char *path)
2449 * Delete a whiteout from the filesystem.
2451 * MPALMOSTSAFE
2454 sys_undelete(struct undelete_args *uap)
2456 struct nlookupdata nd;
2457 int error;
2459 get_mplock();
2460 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2461 bwillinode(1);
2462 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2463 if (error == 0)
2464 error = nlookup(&nd);
2465 if (error == 0)
2466 error = ncp_writechk(&nd.nl_nch);
2467 if (error == 0) {
2468 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2469 NAMEI_DELETE);
2471 nlookup_done(&nd);
2472 rel_mplock();
2473 return (error);
2477 kern_unlink(struct nlookupdata *nd)
2479 int error;
2481 bwillinode(1);
2482 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2483 if ((error = nlookup(nd)) != 0)
2484 return (error);
2485 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2486 return (error);
2487 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2488 return (error);
2492 * unlink_args(char *path)
2494 * Delete a name from the filesystem.
2496 * MPALMOSTSAFE
2499 sys_unlink(struct unlink_args *uap)
2501 struct nlookupdata nd;
2502 int error;
2504 get_mplock();
2505 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2506 if (error == 0)
2507 error = kern_unlink(&nd);
2508 nlookup_done(&nd);
2509 rel_mplock();
2510 return (error);
2515 * unlinkat_args(int fd, char *path, int flags)
2517 * Delete the file or directory entry pointed to by fd/path.
2519 * MPALMOSTSAFE
2522 sys_unlinkat(struct unlinkat_args *uap)
2524 struct nlookupdata nd;
2525 struct file *fp;
2526 int error;
2528 if (uap->flags & ~AT_REMOVEDIR)
2529 return (EINVAL);
2531 get_mplock();
2532 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2533 if (error == 0) {
2534 if (uap->flags & AT_REMOVEDIR)
2535 error = kern_rmdir(&nd);
2536 else
2537 error = kern_unlink(&nd);
2539 nlookup_done_at(&nd, fp);
2540 rel_mplock();
2541 return (error);
2545 * MPALMOSTSAFE
2548 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2550 struct thread *td = curthread;
2551 struct proc *p = td->td_proc;
2552 struct file *fp;
2553 struct vnode *vp;
2554 struct vattr vattr;
2555 off_t new_offset;
2556 int error;
2558 fp = holdfp(p->p_fd, fd, -1);
2559 if (fp == NULL)
2560 return (EBADF);
2561 if (fp->f_type != DTYPE_VNODE) {
2562 error = ESPIPE;
2563 goto done;
2565 vp = (struct vnode *)fp->f_data;
2567 switch (whence) {
2568 case L_INCR:
2569 spin_lock(&fp->f_spin);
2570 new_offset = fp->f_offset + offset;
2571 error = 0;
2572 break;
2573 case L_XTND:
2574 get_mplock();
2575 error = VOP_GETATTR(vp, &vattr);
2576 rel_mplock();
2577 spin_lock(&fp->f_spin);
2578 new_offset = offset + vattr.va_size;
2579 break;
2580 case L_SET:
2581 new_offset = offset;
2582 error = 0;
2583 spin_lock(&fp->f_spin);
2584 break;
2585 default:
2586 new_offset = 0;
2587 error = EINVAL;
2588 spin_lock(&fp->f_spin);
2589 break;
2593 * Validate the seek position. Negative offsets are not allowed
2594 * for regular files or directories.
2596 * Normally we would also not want to allow negative offsets for
2597 * character and block-special devices. However kvm addresses
2598 * on 64 bit architectures might appear to be negative and must
2599 * be allowed.
2601 if (error == 0) {
2602 if (new_offset < 0 &&
2603 (vp->v_type == VREG || vp->v_type == VDIR)) {
2604 error = EINVAL;
2605 } else {
2606 fp->f_offset = new_offset;
2609 *res = fp->f_offset;
2610 spin_unlock(&fp->f_spin);
2611 done:
2612 fdrop(fp);
2613 return (error);
2617 * lseek_args(int fd, int pad, off_t offset, int whence)
2619 * Reposition read/write file offset.
2621 * MPSAFE
2624 sys_lseek(struct lseek_args *uap)
2626 int error;
2628 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2629 &uap->sysmsg_offset);
2631 return (error);
2635 * Check if current process can access given file. amode is a bitmask of *_OK
2636 * access bits. flags is a bitmask of AT_* flags.
2639 kern_access(struct nlookupdata *nd, int amode, int flags)
2641 struct vnode *vp;
2642 int error, mode;
2644 if (flags & ~AT_EACCESS)
2645 return (EINVAL);
2646 if ((error = nlookup(nd)) != 0)
2647 return (error);
2648 retry:
2649 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2650 if (error)
2651 return (error);
2653 /* Flags == 0 means only check for existence. */
2654 if (amode) {
2655 mode = 0;
2656 if (amode & R_OK)
2657 mode |= VREAD;
2658 if (amode & W_OK)
2659 mode |= VWRITE;
2660 if (amode & X_OK)
2661 mode |= VEXEC;
2662 if ((mode & VWRITE) == 0 ||
2663 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2664 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2667 * If the file handle is stale we have to re-resolve the
2668 * entry. This is a hack at the moment.
2670 if (error == ESTALE) {
2671 vput(vp);
2672 cache_setunresolved(&nd->nl_nch);
2673 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2674 if (error == 0) {
2675 vp = NULL;
2676 goto retry;
2678 return(error);
2681 vput(vp);
2682 return (error);
2686 * access_args(char *path, int flags)
2688 * Check access permissions.
2690 * MPALMOSTSAFE
2693 sys_access(struct access_args *uap)
2695 struct nlookupdata nd;
2696 int error;
2698 get_mplock();
2699 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2700 if (error == 0)
2701 error = kern_access(&nd, uap->flags, 0);
2702 nlookup_done(&nd);
2703 rel_mplock();
2704 return (error);
2709 * faccessat_args(int fd, char *path, int amode, int flags)
2711 * Check access permissions.
2713 * MPALMOSTSAFE
2716 sys_faccessat(struct faccessat_args *uap)
2718 struct nlookupdata nd;
2719 struct file *fp;
2720 int error;
2722 get_mplock();
2723 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2724 NLC_FOLLOW);
2725 if (error == 0)
2726 error = kern_access(&nd, uap->amode, uap->flags);
2727 nlookup_done_at(&nd, fp);
2728 rel_mplock();
2729 return (error);
2734 * MPSAFE
2737 kern_stat(struct nlookupdata *nd, struct stat *st)
2739 int error;
2740 struct vnode *vp;
2741 thread_t td;
2743 if ((error = nlookup(nd)) != 0)
2744 return (error);
2745 again:
2746 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2747 return (ENOENT);
2749 td = curthread;
2750 if ((error = vget(vp, LK_SHARED)) != 0)
2751 return (error);
2752 error = vn_stat(vp, st, nd->nl_cred);
2755 * If the file handle is stale we have to re-resolve the entry. This
2756 * is a hack at the moment.
2758 if (error == ESTALE) {
2759 vput(vp);
2760 cache_setunresolved(&nd->nl_nch);
2761 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2762 if (error == 0)
2763 goto again;
2764 } else {
2765 vput(vp);
2767 return (error);
2771 * stat_args(char *path, struct stat *ub)
2773 * Get file status; this version follows links.
2775 * MPSAFE
2778 sys_stat(struct stat_args *uap)
2780 CACHE_MPLOCK_DECLARE;
2781 struct nlookupdata nd;
2782 struct stat st;
2783 int error;
2785 CACHE_GETMPLOCK1();
2786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787 if (error == 0) {
2788 error = kern_stat(&nd, &st);
2789 if (error == 0)
2790 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2792 nlookup_done(&nd);
2793 CACHE_RELMPLOCK();
2794 return (error);
2798 * lstat_args(char *path, struct stat *ub)
2800 * Get file status; this version does not follow links.
2802 * MPALMOSTSAFE
2805 sys_lstat(struct lstat_args *uap)
2807 CACHE_MPLOCK_DECLARE;
2808 struct nlookupdata nd;
2809 struct stat st;
2810 int error;
2812 CACHE_GETMPLOCK1();
2813 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2814 if (error == 0) {
2815 error = kern_stat(&nd, &st);
2816 if (error == 0)
2817 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2819 nlookup_done(&nd);
2820 CACHE_RELMPLOCK();
2821 return (error);
2825 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2827 * Get status of file pointed to by fd/path.
2829 * MPALMOSTSAFE
2832 sys_fstatat(struct fstatat_args *uap)
2834 CACHE_MPLOCK_DECLARE;
2835 struct nlookupdata nd;
2836 struct stat st;
2837 int error;
2838 int flags;
2839 struct file *fp;
2841 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2842 return (EINVAL);
2844 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2846 CACHE_GETMPLOCK1();
2847 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2848 UIO_USERSPACE, flags);
2849 if (error == 0) {
2850 error = kern_stat(&nd, &st);
2851 if (error == 0)
2852 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2854 nlookup_done_at(&nd, fp);
2855 CACHE_RELMPLOCK();
2856 return (error);
2860 * pathconf_Args(char *path, int name)
2862 * Get configurable pathname variables.
2864 * MPALMOSTSAFE
2867 sys_pathconf(struct pathconf_args *uap)
2869 struct nlookupdata nd;
2870 struct vnode *vp;
2871 int error;
2873 vp = NULL;
2874 get_mplock();
2875 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2876 if (error == 0)
2877 error = nlookup(&nd);
2878 if (error == 0)
2879 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2880 nlookup_done(&nd);
2881 if (error == 0) {
2882 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2883 vput(vp);
2885 rel_mplock();
2886 return (error);
2890 * XXX: daver
2891 * kern_readlink isn't properly split yet. There is a copyin burried
2892 * in VOP_READLINK().
2895 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2897 struct thread *td = curthread;
2898 struct vnode *vp;
2899 struct iovec aiov;
2900 struct uio auio;
2901 int error;
2903 if ((error = nlookup(nd)) != 0)
2904 return (error);
2905 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2906 if (error)
2907 return (error);
2908 if (vp->v_type != VLNK) {
2909 error = EINVAL;
2910 } else {
2911 aiov.iov_base = buf;
2912 aiov.iov_len = count;
2913 auio.uio_iov = &aiov;
2914 auio.uio_iovcnt = 1;
2915 auio.uio_offset = 0;
2916 auio.uio_rw = UIO_READ;
2917 auio.uio_segflg = UIO_USERSPACE;
2918 auio.uio_td = td;
2919 auio.uio_resid = count;
2920 error = VOP_READLINK(vp, &auio, td->td_ucred);
2922 vput(vp);
2923 *res = count - auio.uio_resid;
2924 return (error);
2928 * readlink_args(char *path, char *buf, int count)
2930 * Return target name of a symbolic link.
2932 * MPALMOSTSAFE
2935 sys_readlink(struct readlink_args *uap)
2937 struct nlookupdata nd;
2938 int error;
2940 get_mplock();
2941 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2942 if (error == 0) {
2943 error = kern_readlink(&nd, uap->buf, uap->count,
2944 &uap->sysmsg_result);
2946 nlookup_done(&nd);
2947 rel_mplock();
2948 return (error);
2952 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2954 * Return target name of a symbolic link. The path is relative to the
2955 * directory associated with fd.
2957 * MPALMOSTSAFE
2960 sys_readlinkat(struct readlinkat_args *uap)
2962 struct nlookupdata nd;
2963 struct file *fp;
2964 int error;
2966 get_mplock();
2967 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2968 if (error == 0) {
2969 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2970 &uap->sysmsg_result);
2972 nlookup_done_at(&nd, fp);
2973 rel_mplock();
2974 return (error);
2977 static int
2978 setfflags(struct vnode *vp, int flags)
2980 struct thread *td = curthread;
2981 int error;
2982 struct vattr vattr;
2985 * Prevent non-root users from setting flags on devices. When
2986 * a device is reused, users can retain ownership of the device
2987 * if they are allowed to set flags and programs assume that
2988 * chown can't fail when done as root.
2990 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2991 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2992 return (error);
2995 * note: vget is required for any operation that might mod the vnode
2996 * so VINACTIVE is properly cleared.
2998 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2999 VATTR_NULL(&vattr);
3000 vattr.va_flags = flags;
3001 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3002 vput(vp);
3004 return (error);
3008 * chflags(char *path, int flags)
3010 * Change flags of a file given a path name.
3012 * MPALMOSTSAFE
3015 sys_chflags(struct chflags_args *uap)
3017 struct nlookupdata nd;
3018 struct vnode *vp;
3019 int error;
3021 vp = NULL;
3022 get_mplock();
3023 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3024 if (error == 0)
3025 error = nlookup(&nd);
3026 if (error == 0)
3027 error = ncp_writechk(&nd.nl_nch);
3028 if (error == 0)
3029 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3030 nlookup_done(&nd);
3031 if (error == 0) {
3032 error = setfflags(vp, uap->flags);
3033 vrele(vp);
3035 rel_mplock();
3036 return (error);
3040 * lchflags(char *path, int flags)
3042 * Change flags of a file given a path name, but don't follow symlinks.
3044 * MPALMOSTSAFE
3047 sys_lchflags(struct lchflags_args *uap)
3049 struct nlookupdata nd;
3050 struct vnode *vp;
3051 int error;
3053 vp = NULL;
3054 get_mplock();
3055 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3056 if (error == 0)
3057 error = nlookup(&nd);
3058 if (error == 0)
3059 error = ncp_writechk(&nd.nl_nch);
3060 if (error == 0)
3061 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3062 nlookup_done(&nd);
3063 if (error == 0) {
3064 error = setfflags(vp, uap->flags);
3065 vrele(vp);
3067 rel_mplock();
3068 return (error);
3072 * fchflags_args(int fd, int flags)
3074 * Change flags of a file given a file descriptor.
3076 * MPALMOSTSAFE
3079 sys_fchflags(struct fchflags_args *uap)
3081 struct thread *td = curthread;
3082 struct proc *p = td->td_proc;
3083 struct file *fp;
3084 int error;
3086 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3087 return (error);
3088 get_mplock();
3089 if (fp->f_nchandle.ncp)
3090 error = ncp_writechk(&fp->f_nchandle);
3091 if (error == 0)
3092 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3093 rel_mplock();
3094 fdrop(fp);
3095 return (error);
3098 static int
3099 setfmode(struct vnode *vp, int mode)
3101 struct thread *td = curthread;
3102 int error;
3103 struct vattr vattr;
3106 * note: vget is required for any operation that might mod the vnode
3107 * so VINACTIVE is properly cleared.
3109 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3110 VATTR_NULL(&vattr);
3111 vattr.va_mode = mode & ALLPERMS;
3112 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3113 vput(vp);
3115 return error;
3119 kern_chmod(struct nlookupdata *nd, int mode)
3121 struct vnode *vp;
3122 int error;
3124 if ((error = nlookup(nd)) != 0)
3125 return (error);
3126 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3127 return (error);
3128 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3129 error = setfmode(vp, mode);
3130 vrele(vp);
3131 return (error);
3135 * chmod_args(char *path, int mode)
3137 * Change mode of a file given path name.
3139 * MPALMOSTSAFE
3142 sys_chmod(struct chmod_args *uap)
3144 struct nlookupdata nd;
3145 int error;
3147 get_mplock();
3148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3149 if (error == 0)
3150 error = kern_chmod(&nd, uap->mode);
3151 nlookup_done(&nd);
3152 rel_mplock();
3153 return (error);
3157 * lchmod_args(char *path, int mode)
3159 * Change mode of a file given path name (don't follow links.)
3161 * MPALMOSTSAFE
3164 sys_lchmod(struct lchmod_args *uap)
3166 struct nlookupdata nd;
3167 int error;
3169 get_mplock();
3170 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3171 if (error == 0)
3172 error = kern_chmod(&nd, uap->mode);
3173 nlookup_done(&nd);
3174 rel_mplock();
3175 return (error);
3179 * fchmod_args(int fd, int mode)
3181 * Change mode of a file given a file descriptor.
3183 * MPALMOSTSAFE
3186 sys_fchmod(struct fchmod_args *uap)
3188 struct thread *td = curthread;
3189 struct proc *p = td->td_proc;
3190 struct file *fp;
3191 int error;
3193 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3194 return (error);
3195 get_mplock();
3196 if (fp->f_nchandle.ncp)
3197 error = ncp_writechk(&fp->f_nchandle);
3198 if (error == 0)
3199 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3200 rel_mplock();
3201 fdrop(fp);
3202 return (error);
3206 * fchmodat_args(char *path, int mode)
3208 * Change mode of a file pointed to by fd/path.
3210 * MPALMOSTSAFE
3213 sys_fchmodat(struct fchmodat_args *uap)
3215 struct nlookupdata nd;
3216 struct file *fp;
3217 int error;
3218 int flags;
3220 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3221 return (EINVAL);
3222 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3224 get_mplock();
3225 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3226 UIO_USERSPACE, flags);
3227 if (error == 0)
3228 error = kern_chmod(&nd, uap->mode);
3229 nlookup_done_at(&nd, fp);
3230 rel_mplock();
3231 return (error);
3234 static int
3235 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3237 struct thread *td = curthread;
3238 int error;
3239 struct vattr vattr;
3242 * note: vget is required for any operation that might mod the vnode
3243 * so VINACTIVE is properly cleared.
3245 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3246 VATTR_NULL(&vattr);
3247 vattr.va_uid = uid;
3248 vattr.va_gid = gid;
3249 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3250 vput(vp);
3252 return error;
3256 kern_chown(struct nlookupdata *nd, int uid, int gid)
3258 struct vnode *vp;
3259 int error;
3261 if ((error = nlookup(nd)) != 0)
3262 return (error);
3263 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3264 return (error);
3265 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3266 error = setfown(vp, uid, gid);
3267 vrele(vp);
3268 return (error);
3272 * chown(char *path, int uid, int gid)
3274 * Set ownership given a path name.
3276 * MPALMOSTSAFE
3279 sys_chown(struct chown_args *uap)
3281 struct nlookupdata nd;
3282 int error;
3284 get_mplock();
3285 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3286 if (error == 0)
3287 error = kern_chown(&nd, uap->uid, uap->gid);
3288 nlookup_done(&nd);
3289 rel_mplock();
3290 return (error);
3294 * lchown_args(char *path, int uid, int gid)
3296 * Set ownership given a path name, do not cross symlinks.
3298 * MPALMOSTSAFE
3301 sys_lchown(struct lchown_args *uap)
3303 struct nlookupdata nd;
3304 int error;
3306 get_mplock();
3307 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3308 if (error == 0)
3309 error = kern_chown(&nd, uap->uid, uap->gid);
3310 nlookup_done(&nd);
3311 rel_mplock();
3312 return (error);
3316 * fchown_args(int fd, int uid, int gid)
3318 * Set ownership given a file descriptor.
3320 * MPALMOSTSAFE
3323 sys_fchown(struct fchown_args *uap)
3325 struct thread *td = curthread;
3326 struct proc *p = td->td_proc;
3327 struct file *fp;
3328 int error;
3330 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3331 return (error);
3332 get_mplock();
3333 if (fp->f_nchandle.ncp)
3334 error = ncp_writechk(&fp->f_nchandle);
3335 if (error == 0)
3336 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3337 rel_mplock();
3338 fdrop(fp);
3339 return (error);
3343 * fchownat(int fd, char *path, int uid, int gid, int flags)
3345 * Set ownership of file pointed to by fd/path.
3347 * MPALMOSTSAFE
3350 sys_fchownat(struct fchownat_args *uap)
3352 struct nlookupdata nd;
3353 struct file *fp;
3354 int error;
3355 int flags;
3357 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3358 return (EINVAL);
3359 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3361 get_mplock();
3362 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3363 UIO_USERSPACE, flags);
3364 if (error == 0)
3365 error = kern_chown(&nd, uap->uid, uap->gid);
3366 nlookup_done_at(&nd, fp);
3367 rel_mplock();
3368 return (error);
3372 static int
3373 getutimes(const struct timeval *tvp, struct timespec *tsp)
3375 struct timeval tv[2];
3377 if (tvp == NULL) {
3378 microtime(&tv[0]);
3379 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3380 tsp[1] = tsp[0];
3381 } else {
3382 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3383 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3385 return 0;
3388 static int
3389 setutimes(struct vnode *vp, struct vattr *vattr,
3390 const struct timespec *ts, int nullflag)
3392 struct thread *td = curthread;
3393 int error;
3395 VATTR_NULL(vattr);
3396 vattr->va_atime = ts[0];
3397 vattr->va_mtime = ts[1];
3398 if (nullflag)
3399 vattr->va_vaflags |= VA_UTIMES_NULL;
3400 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3402 return error;
3406 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3408 struct timespec ts[2];
3409 struct vnode *vp;
3410 struct vattr vattr;
3411 int error;
3413 if ((error = getutimes(tptr, ts)) != 0)
3414 return (error);
3417 * NOTE: utimes() succeeds for the owner even if the file
3418 * is not user-writable.
3420 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3422 if ((error = nlookup(nd)) != 0)
3423 return (error);
3424 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3425 return (error);
3426 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3427 return (error);
3430 * note: vget is required for any operation that might mod the vnode
3431 * so VINACTIVE is properly cleared.
3433 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3434 error = vget(vp, LK_EXCLUSIVE);
3435 if (error == 0) {
3436 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3437 vput(vp);
3440 vrele(vp);
3441 return (error);
3445 * utimes_args(char *path, struct timeval *tptr)
3447 * Set the access and modification times of a file.
3449 * MPALMOSTSAFE
3452 sys_utimes(struct utimes_args *uap)
3454 struct timeval tv[2];
3455 struct nlookupdata nd;
3456 int error;
3458 if (uap->tptr) {
3459 error = copyin(uap->tptr, tv, sizeof(tv));
3460 if (error)
3461 return (error);
3463 get_mplock();
3464 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3465 if (error == 0)
3466 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3467 nlookup_done(&nd);
3468 rel_mplock();
3469 return (error);
3473 * lutimes_args(char *path, struct timeval *tptr)
3475 * Set the access and modification times of a file.
3477 * MPALMOSTSAFE
3480 sys_lutimes(struct lutimes_args *uap)
3482 struct timeval tv[2];
3483 struct nlookupdata nd;
3484 int error;
3486 if (uap->tptr) {
3487 error = copyin(uap->tptr, tv, sizeof(tv));
3488 if (error)
3489 return (error);
3491 get_mplock();
3492 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3493 if (error == 0)
3494 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3495 nlookup_done(&nd);
3496 rel_mplock();
3497 return (error);
3501 * Set utimes on a file descriptor. The creds used to open the
3502 * file are used to determine whether the operation is allowed
3503 * or not.
3506 kern_futimes(int fd, struct timeval *tptr)
3508 struct thread *td = curthread;
3509 struct proc *p = td->td_proc;
3510 struct timespec ts[2];
3511 struct file *fp;
3512 struct vnode *vp;
3513 struct vattr vattr;
3514 int error;
3516 error = getutimes(tptr, ts);
3517 if (error)
3518 return (error);
3519 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3520 return (error);
3521 if (fp->f_nchandle.ncp)
3522 error = ncp_writechk(&fp->f_nchandle);
3523 if (error == 0) {
3524 vp = fp->f_data;
3525 error = vget(vp, LK_EXCLUSIVE);
3526 if (error == 0) {
3527 error = VOP_GETATTR(vp, &vattr);
3528 if (error == 0) {
3529 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3530 fp->f_cred);
3532 if (error == 0) {
3533 error = setutimes(vp, &vattr, ts,
3534 (tptr == NULL));
3536 vput(vp);
3539 fdrop(fp);
3540 return (error);
3544 * futimes_args(int fd, struct timeval *tptr)
3546 * Set the access and modification times of a file.
3548 * MPALMOSTSAFE
3551 sys_futimes(struct futimes_args *uap)
3553 struct timeval tv[2];
3554 int error;
3556 if (uap->tptr) {
3557 error = copyin(uap->tptr, tv, sizeof(tv));
3558 if (error)
3559 return (error);
3561 get_mplock();
3562 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3563 rel_mplock();
3565 return (error);
3569 kern_truncate(struct nlookupdata *nd, off_t length)
3571 struct vnode *vp;
3572 struct vattr vattr;
3573 int error;
3575 if (length < 0)
3576 return(EINVAL);
3577 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3578 if ((error = nlookup(nd)) != 0)
3579 return (error);
3580 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3581 return (error);
3582 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3583 return (error);
3584 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3585 vrele(vp);
3586 return (error);
3588 if (vp->v_type == VDIR) {
3589 error = EISDIR;
3590 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3591 VATTR_NULL(&vattr);
3592 vattr.va_size = length;
3593 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3595 vput(vp);
3596 return (error);
3600 * truncate(char *path, int pad, off_t length)
3602 * Truncate a file given its path name.
3604 * MPALMOSTSAFE
3607 sys_truncate(struct truncate_args *uap)
3609 struct nlookupdata nd;
3610 int error;
3612 get_mplock();
3613 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3614 if (error == 0)
3615 error = kern_truncate(&nd, uap->length);
3616 nlookup_done(&nd);
3617 rel_mplock();
3618 return error;
3622 kern_ftruncate(int fd, off_t length)
3624 struct thread *td = curthread;
3625 struct proc *p = td->td_proc;
3626 struct vattr vattr;
3627 struct vnode *vp;
3628 struct file *fp;
3629 int error;
3631 if (length < 0)
3632 return(EINVAL);
3633 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3634 return (error);
3635 if (fp->f_nchandle.ncp) {
3636 error = ncp_writechk(&fp->f_nchandle);
3637 if (error)
3638 goto done;
3640 if ((fp->f_flag & FWRITE) == 0) {
3641 error = EINVAL;
3642 goto done;
3644 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3645 error = EINVAL;
3646 goto done;
3648 vp = (struct vnode *)fp->f_data;
3649 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3650 if (vp->v_type == VDIR) {
3651 error = EISDIR;
3652 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3653 VATTR_NULL(&vattr);
3654 vattr.va_size = length;
3655 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3657 vn_unlock(vp);
3658 done:
3659 fdrop(fp);
3660 return (error);
3664 * ftruncate_args(int fd, int pad, off_t length)
3666 * Truncate a file given a file descriptor.
3668 * MPALMOSTSAFE
3671 sys_ftruncate(struct ftruncate_args *uap)
3673 int error;
3675 get_mplock();
3676 error = kern_ftruncate(uap->fd, uap->length);
3677 rel_mplock();
3679 return (error);
3683 * fsync(int fd)
3685 * Sync an open file.
3687 * MPALMOSTSAFE
3690 sys_fsync(struct fsync_args *uap)
3692 struct thread *td = curthread;
3693 struct proc *p = td->td_proc;
3694 struct vnode *vp;
3695 struct file *fp;
3696 vm_object_t obj;
3697 int error;
3699 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3700 return (error);
3701 get_mplock();
3702 vp = (struct vnode *)fp->f_data;
3703 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3704 if ((obj = vp->v_object) != NULL)
3705 vm_object_page_clean(obj, 0, 0, 0);
3706 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3707 if (error == 0 && vp->v_mount)
3708 error = buf_fsync(vp);
3709 vn_unlock(vp);
3710 rel_mplock();
3711 fdrop(fp);
3713 return (error);
3717 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3719 struct nchandle fnchd;
3720 struct nchandle tnchd;
3721 struct namecache *ncp;
3722 struct vnode *fdvp;
3723 struct vnode *tdvp;
3724 struct mount *mp;
3725 int error;
3727 bwillinode(1);
3728 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3729 if ((error = nlookup(fromnd)) != 0)
3730 return (error);
3731 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3732 return (ENOENT);
3733 fnchd.mount = fromnd->nl_nch.mount;
3734 cache_hold(&fnchd);
3737 * unlock the source nch so we can lookup the target nch without
3738 * deadlocking. The target may or may not exist so we do not check
3739 * for a target vp like kern_mkdir() and other creation functions do.
3741 * The source and target directories are ref'd and rechecked after
3742 * everything is relocked to determine if the source or target file
3743 * has been renamed.
3745 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3746 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3747 cache_unlock(&fromnd->nl_nch);
3749 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3750 if ((error = nlookup(tond)) != 0) {
3751 cache_drop(&fnchd);
3752 return (error);
3754 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3755 cache_drop(&fnchd);
3756 return (ENOENT);
3758 tnchd.mount = tond->nl_nch.mount;
3759 cache_hold(&tnchd);
3762 * If the source and target are the same there is nothing to do
3764 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3765 cache_drop(&fnchd);
3766 cache_drop(&tnchd);
3767 return (0);
3771 * Mount points cannot be renamed or overwritten
3773 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3774 NCF_ISMOUNTPT
3776 cache_drop(&fnchd);
3777 cache_drop(&tnchd);
3778 return (EINVAL);
3782 * Relock the source ncp. cache_relock() will deal with any
3783 * deadlocks against the already-locked tond and will also
3784 * make sure both are resolved.
3786 * NOTE AFTER RELOCKING: The source or target ncp may have become
3787 * invalid while they were unlocked, nc_vp and nc_mount could
3788 * be NULL.
3790 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3791 &tond->nl_nch, tond->nl_cred);
3792 fromnd->nl_flags |= NLC_NCPISLOCKED;
3795 * make sure the parent directories linkages are the same
3797 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3798 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3799 cache_drop(&fnchd);
3800 cache_drop(&tnchd);
3801 return (ENOENT);
3805 * Both the source and target must be within the same filesystem and
3806 * in the same filesystem as their parent directories within the
3807 * namecache topology.
3809 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3811 mp = fnchd.mount;
3812 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3813 mp != tond->nl_nch.mount) {
3814 cache_drop(&fnchd);
3815 cache_drop(&tnchd);
3816 return (EXDEV);
3820 * Make sure the mount point is writable
3822 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3823 cache_drop(&fnchd);
3824 cache_drop(&tnchd);
3825 return (error);
3829 * If the target exists and either the source or target is a directory,
3830 * then both must be directories.
3832 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3833 * have become NULL.
3835 if (tond->nl_nch.ncp->nc_vp) {
3836 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3837 error = ENOENT;
3838 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3839 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3840 error = ENOTDIR;
3841 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3842 error = EISDIR;
3847 * You cannot rename a source into itself or a subdirectory of itself.
3848 * We check this by travsersing the target directory upwards looking
3849 * for a match against the source.
3851 * XXX MPSAFE
3853 if (error == 0) {
3854 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3855 if (fromnd->nl_nch.ncp == ncp) {
3856 error = EINVAL;
3857 break;
3862 cache_drop(&fnchd);
3863 cache_drop(&tnchd);
3866 * Even though the namespaces are different, they may still represent
3867 * hardlinks to the same file. The filesystem might have a hard time
3868 * with this so we issue a NREMOVE of the source instead of a NRENAME
3869 * when we detect the situation.
3871 if (error == 0) {
3872 fdvp = fromnd->nl_dvp;
3873 tdvp = tond->nl_dvp;
3874 if (fdvp == NULL || tdvp == NULL) {
3875 error = EPERM;
3876 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3877 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3878 fromnd->nl_cred);
3879 } else {
3880 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3881 fdvp, tdvp, tond->nl_cred);
3884 return (error);
3888 * rename_args(char *from, char *to)
3890 * Rename files. Source and destination must either both be directories,
3891 * or both not be directories. If target is a directory, it must be empty.
3893 * MPALMOSTSAFE
3896 sys_rename(struct rename_args *uap)
3898 struct nlookupdata fromnd, tond;
3899 int error;
3901 get_mplock();
3902 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3903 if (error == 0) {
3904 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3905 if (error == 0)
3906 error = kern_rename(&fromnd, &tond);
3907 nlookup_done(&tond);
3909 nlookup_done(&fromnd);
3910 rel_mplock();
3911 return (error);
3915 * renameat_args(int oldfd, char *old, int newfd, char *new)
3917 * Rename files using paths relative to the directories associated with
3918 * oldfd and newfd. Source and destination must either both be directories,
3919 * or both not be directories. If target is a directory, it must be empty.
3921 * MPALMOSTSAFE
3924 sys_renameat(struct renameat_args *uap)
3926 struct nlookupdata oldnd, newnd;
3927 struct file *oldfp, *newfp;
3928 int error;
3930 get_mplock();
3931 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3932 UIO_USERSPACE, 0);
3933 if (error == 0) {
3934 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3935 UIO_USERSPACE, 0);
3936 if (error == 0)
3937 error = kern_rename(&oldnd, &newnd);
3938 nlookup_done_at(&newnd, newfp);
3940 nlookup_done_at(&oldnd, oldfp);
3941 rel_mplock();
3942 return (error);
3946 kern_mkdir(struct nlookupdata *nd, int mode)
3948 struct thread *td = curthread;
3949 struct proc *p = td->td_proc;
3950 struct vnode *vp;
3951 struct vattr vattr;
3952 int error;
3954 bwillinode(1);
3955 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3956 if ((error = nlookup(nd)) != 0)
3957 return (error);
3959 if (nd->nl_nch.ncp->nc_vp)
3960 return (EEXIST);
3961 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3962 return (error);
3963 VATTR_NULL(&vattr);
3964 vattr.va_type = VDIR;
3965 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3967 vp = NULL;
3968 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3969 if (error == 0)
3970 vput(vp);
3971 return (error);
3975 * mkdir_args(char *path, int mode)
3977 * Make a directory file.
3979 * MPALMOSTSAFE
3982 sys_mkdir(struct mkdir_args *uap)
3984 struct nlookupdata nd;
3985 int error;
3987 get_mplock();
3988 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3989 if (error == 0)
3990 error = kern_mkdir(&nd, uap->mode);
3991 nlookup_done(&nd);
3992 rel_mplock();
3993 return (error);
3997 * mkdirat_args(int fd, char *path, mode_t mode)
3999 * Make a directory file. The path is relative to the directory associated
4000 * with fd.
4002 * MPALMOSTSAFE
4005 sys_mkdirat(struct mkdirat_args *uap)
4007 struct nlookupdata nd;
4008 struct file *fp;
4009 int error;
4011 get_mplock();
4012 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4013 if (error == 0)
4014 error = kern_mkdir(&nd, uap->mode);
4015 nlookup_done_at(&nd, fp);
4016 rel_mplock();
4017 return (error);
4021 kern_rmdir(struct nlookupdata *nd)
4023 int error;
4025 bwillinode(1);
4026 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4027 if ((error = nlookup(nd)) != 0)
4028 return (error);
4031 * Do not allow directories representing mount points to be
4032 * deleted, even if empty. Check write perms on mount point
4033 * in case the vnode is aliased (aka nullfs).
4035 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4036 return (EINVAL);
4037 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4038 return (error);
4039 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4040 return (error);
4044 * rmdir_args(char *path)
4046 * Remove a directory file.
4048 * MPALMOSTSAFE
4051 sys_rmdir(struct rmdir_args *uap)
4053 struct nlookupdata nd;
4054 int error;
4056 get_mplock();
4057 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4058 if (error == 0)
4059 error = kern_rmdir(&nd);
4060 nlookup_done(&nd);
4061 rel_mplock();
4062 return (error);
4066 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4067 enum uio_seg direction)
4069 struct thread *td = curthread;
4070 struct proc *p = td->td_proc;
4071 struct vnode *vp;
4072 struct file *fp;
4073 struct uio auio;
4074 struct iovec aiov;
4075 off_t loff;
4076 int error, eofflag;
4078 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4079 return (error);
4080 if ((fp->f_flag & FREAD) == 0) {
4081 error = EBADF;
4082 goto done;
4084 vp = (struct vnode *)fp->f_data;
4085 unionread:
4086 if (vp->v_type != VDIR) {
4087 error = EINVAL;
4088 goto done;
4090 aiov.iov_base = buf;
4091 aiov.iov_len = count;
4092 auio.uio_iov = &aiov;
4093 auio.uio_iovcnt = 1;
4094 auio.uio_rw = UIO_READ;
4095 auio.uio_segflg = direction;
4096 auio.uio_td = td;
4097 auio.uio_resid = count;
4098 loff = auio.uio_offset = fp->f_offset;
4099 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4100 fp->f_offset = auio.uio_offset;
4101 if (error)
4102 goto done;
4103 if (count == auio.uio_resid) {
4104 if (union_dircheckp) {
4105 error = union_dircheckp(td, &vp, fp);
4106 if (error == -1)
4107 goto unionread;
4108 if (error)
4109 goto done;
4111 #if 0
4112 if ((vp->v_flag & VROOT) &&
4113 (vp->v_mount->mnt_flag & MNT_UNION)) {
4114 struct vnode *tvp = vp;
4115 vp = vp->v_mount->mnt_vnodecovered;
4116 vref(vp);
4117 fp->f_data = vp;
4118 fp->f_offset = 0;
4119 vrele(tvp);
4120 goto unionread;
4122 #endif
4126 * WARNING! *basep may not be wide enough to accomodate the
4127 * seek offset. XXX should we hack this to return the upper 32 bits
4128 * for offsets greater then 4G?
4130 if (basep) {
4131 *basep = (long)loff;
4133 *res = count - auio.uio_resid;
4134 done:
4135 fdrop(fp);
4136 return (error);
4140 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4142 * Read a block of directory entries in a file system independent format.
4144 * MPALMOSTSAFE
4147 sys_getdirentries(struct getdirentries_args *uap)
4149 long base;
4150 int error;
4152 get_mplock();
4153 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4154 &uap->sysmsg_result, UIO_USERSPACE);
4155 rel_mplock();
4157 if (error == 0 && uap->basep)
4158 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4159 return (error);
4163 * getdents_args(int fd, char *buf, size_t count)
4165 * MPALMOSTSAFE
4168 sys_getdents(struct getdents_args *uap)
4170 int error;
4172 get_mplock();
4173 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4174 &uap->sysmsg_result, UIO_USERSPACE);
4175 rel_mplock();
4177 return (error);
4181 * Set the mode mask for creation of filesystem nodes.
4183 * umask(int newmask)
4185 * MPSAFE
4188 sys_umask(struct umask_args *uap)
4190 struct thread *td = curthread;
4191 struct proc *p = td->td_proc;
4192 struct filedesc *fdp;
4194 fdp = p->p_fd;
4195 uap->sysmsg_result = fdp->fd_cmask;
4196 fdp->fd_cmask = uap->newmask & ALLPERMS;
4197 return (0);
4201 * revoke(char *path)
4203 * Void all references to file by ripping underlying filesystem
4204 * away from vnode.
4206 * MPALMOSTSAFE
4209 sys_revoke(struct revoke_args *uap)
4211 struct nlookupdata nd;
4212 struct vattr vattr;
4213 struct vnode *vp;
4214 struct ucred *cred;
4215 int error;
4217 vp = NULL;
4218 get_mplock();
4219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4220 if (error == 0)
4221 error = nlookup(&nd);
4222 if (error == 0)
4223 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4224 cred = crhold(nd.nl_cred);
4225 nlookup_done(&nd);
4226 if (error == 0) {
4227 if (error == 0)
4228 error = VOP_GETATTR(vp, &vattr);
4229 if (error == 0 && cred->cr_uid != vattr.va_uid)
4230 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4231 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4232 if (vcount(vp) > 0)
4233 error = vrevoke(vp, cred);
4234 } else if (error == 0) {
4235 error = vrevoke(vp, cred);
4237 vrele(vp);
4239 if (cred)
4240 crfree(cred);
4241 rel_mplock();
4242 return (error);
4246 * getfh_args(char *fname, fhandle_t *fhp)
4248 * Get (NFS) file handle
4250 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4251 * mount. This allows nullfs mounts to be explicitly exported.
4253 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4255 * nullfs mounts of subdirectories are not safe. That is, it will
4256 * work, but you do not really have protection against access to
4257 * the related parent directories.
4259 * MPALMOSTSAFE
4262 sys_getfh(struct getfh_args *uap)
4264 struct thread *td = curthread;
4265 struct nlookupdata nd;
4266 fhandle_t fh;
4267 struct vnode *vp;
4268 struct mount *mp;
4269 int error;
4272 * Must be super user
4274 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4275 return (error);
4277 vp = NULL;
4278 get_mplock();
4279 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4280 if (error == 0)
4281 error = nlookup(&nd);
4282 if (error == 0)
4283 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4284 mp = nd.nl_nch.mount;
4285 nlookup_done(&nd);
4286 if (error == 0) {
4287 bzero(&fh, sizeof(fh));
4288 fh.fh_fsid = mp->mnt_stat.f_fsid;
4289 error = VFS_VPTOFH(vp, &fh.fh_fid);
4290 vput(vp);
4291 if (error == 0)
4292 error = copyout(&fh, uap->fhp, sizeof(fh));
4294 rel_mplock();
4295 return (error);
4299 * fhopen_args(const struct fhandle *u_fhp, int flags)
4301 * syscall for the rpc.lockd to use to translate a NFS file handle into
4302 * an open descriptor.
4304 * warning: do not remove the priv_check() call or this becomes one giant
4305 * security hole.
4307 * MPALMOSTSAFE
4310 sys_fhopen(struct fhopen_args *uap)
4312 struct thread *td = curthread;
4313 struct filedesc *fdp = td->td_proc->p_fd;
4314 struct mount *mp;
4315 struct vnode *vp;
4316 struct fhandle fhp;
4317 struct vattr vat;
4318 struct vattr *vap = &vat;
4319 struct flock lf;
4320 int fmode, mode, error, type;
4321 struct file *nfp;
4322 struct file *fp;
4323 int indx;
4326 * Must be super user
4328 error = priv_check(td, PRIV_ROOT);
4329 if (error)
4330 return (error);
4332 fmode = FFLAGS(uap->flags);
4335 * Why not allow a non-read/write open for our lockd?
4337 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4338 return (EINVAL);
4339 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4340 if (error)
4341 return(error);
4344 * Find the mount point
4346 get_mplock();
4347 mp = vfs_getvfs(&fhp.fh_fsid);
4348 if (mp == NULL) {
4349 error = ESTALE;
4350 goto done;
4352 /* now give me my vnode, it gets returned to me locked */
4353 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4354 if (error)
4355 goto done;
4357 * from now on we have to make sure not
4358 * to forget about the vnode
4359 * any error that causes an abort must vput(vp)
4360 * just set error = err and 'goto bad;'.
4364 * from vn_open
4366 if (vp->v_type == VLNK) {
4367 error = EMLINK;
4368 goto bad;
4370 if (vp->v_type == VSOCK) {
4371 error = EOPNOTSUPP;
4372 goto bad;
4374 mode = 0;
4375 if (fmode & (FWRITE | O_TRUNC)) {
4376 if (vp->v_type == VDIR) {
4377 error = EISDIR;
4378 goto bad;
4380 error = vn_writechk(vp, NULL);
4381 if (error)
4382 goto bad;
4383 mode |= VWRITE;
4385 if (fmode & FREAD)
4386 mode |= VREAD;
4387 if (mode) {
4388 error = VOP_ACCESS(vp, mode, td->td_ucred);
4389 if (error)
4390 goto bad;
4392 if (fmode & O_TRUNC) {
4393 vn_unlock(vp); /* XXX */
4394 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4395 VATTR_NULL(vap);
4396 vap->va_size = 0;
4397 error = VOP_SETATTR(vp, vap, td->td_ucred);
4398 if (error)
4399 goto bad;
4403 * VOP_OPEN needs the file pointer so it can potentially override
4404 * it.
4406 * WARNING! no f_nchandle will be associated when fhopen()ing a
4407 * directory. XXX
4409 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4410 goto bad;
4411 fp = nfp;
4413 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4414 if (error) {
4416 * setting f_ops this way prevents VOP_CLOSE from being
4417 * called or fdrop() releasing the vp from v_data. Since
4418 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4420 fp->f_ops = &badfileops;
4421 fp->f_data = NULL;
4422 goto bad_drop;
4426 * The fp is given its own reference, we still have our ref and lock.
4428 * Assert that all regular files must be created with a VM object.
4430 if (vp->v_type == VREG && vp->v_object == NULL) {
4431 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4432 goto bad_drop;
4436 * The open was successful. Handle any locking requirements.
4438 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4439 lf.l_whence = SEEK_SET;
4440 lf.l_start = 0;
4441 lf.l_len = 0;
4442 if (fmode & O_EXLOCK)
4443 lf.l_type = F_WRLCK;
4444 else
4445 lf.l_type = F_RDLCK;
4446 if (fmode & FNONBLOCK)
4447 type = 0;
4448 else
4449 type = F_WAIT;
4450 vn_unlock(vp);
4451 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4453 * release our private reference.
4455 fsetfd(fdp, NULL, indx);
4456 fdrop(fp);
4457 vrele(vp);
4458 goto done;
4460 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4461 fp->f_flag |= FHASLOCK;
4465 * Clean up. Associate the file pointer with the previously
4466 * reserved descriptor and return it.
4468 vput(vp);
4469 rel_mplock();
4470 fsetfd(fdp, fp, indx);
4471 fdrop(fp);
4472 uap->sysmsg_result = indx;
4473 return (0);
4475 bad_drop:
4476 fsetfd(fdp, NULL, indx);
4477 fdrop(fp);
4478 bad:
4479 vput(vp);
4480 done:
4481 rel_mplock();
4482 return (error);
4486 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4488 * MPALMOSTSAFE
4491 sys_fhstat(struct fhstat_args *uap)
4493 struct thread *td = curthread;
4494 struct stat sb;
4495 fhandle_t fh;
4496 struct mount *mp;
4497 struct vnode *vp;
4498 int error;
4501 * Must be super user
4503 error = priv_check(td, PRIV_ROOT);
4504 if (error)
4505 return (error);
4507 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4508 if (error)
4509 return (error);
4511 get_mplock();
4512 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4513 error = ESTALE;
4514 if (error == 0) {
4515 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4516 error = vn_stat(vp, &sb, td->td_ucred);
4517 vput(vp);
4520 rel_mplock();
4521 if (error == 0)
4522 error = copyout(&sb, uap->sb, sizeof(sb));
4523 return (error);
4527 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4529 * MPALMOSTSAFE
4532 sys_fhstatfs(struct fhstatfs_args *uap)
4534 struct thread *td = curthread;
4535 struct proc *p = td->td_proc;
4536 struct statfs *sp;
4537 struct mount *mp;
4538 struct vnode *vp;
4539 struct statfs sb;
4540 char *fullpath, *freepath;
4541 fhandle_t fh;
4542 int error;
4545 * Must be super user
4547 if ((error = priv_check(td, PRIV_ROOT)))
4548 return (error);
4550 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4551 return (error);
4553 get_mplock();
4555 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4556 error = ESTALE;
4557 goto done;
4559 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4560 error = ESTALE;
4561 goto done;
4564 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4565 goto done;
4566 mp = vp->v_mount;
4567 sp = &mp->mnt_stat;
4568 vput(vp);
4569 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4570 goto done;
4572 error = mount_path(p, mp, &fullpath, &freepath);
4573 if (error)
4574 goto done;
4575 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4576 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4577 kfree(freepath, M_TEMP);
4579 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4580 if (priv_check(td, PRIV_ROOT)) {
4581 bcopy(sp, &sb, sizeof(sb));
4582 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4583 sp = &sb;
4585 error = copyout(sp, uap->buf, sizeof(*sp));
4586 done:
4587 rel_mplock();
4588 return (error);
4592 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4594 * MPALMOSTSAFE
4597 sys_fhstatvfs(struct fhstatvfs_args *uap)
4599 struct thread *td = curthread;
4600 struct proc *p = td->td_proc;
4601 struct statvfs *sp;
4602 struct mount *mp;
4603 struct vnode *vp;
4604 fhandle_t fh;
4605 int error;
4608 * Must be super user
4610 if ((error = priv_check(td, PRIV_ROOT)))
4611 return (error);
4613 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4614 return (error);
4616 get_mplock();
4618 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4619 error = ESTALE;
4620 goto done;
4622 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4623 error = ESTALE;
4624 goto done;
4627 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4628 goto done;
4629 mp = vp->v_mount;
4630 sp = &mp->mnt_vstat;
4631 vput(vp);
4632 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4633 goto done;
4635 sp->f_flag = 0;
4636 if (mp->mnt_flag & MNT_RDONLY)
4637 sp->f_flag |= ST_RDONLY;
4638 if (mp->mnt_flag & MNT_NOSUID)
4639 sp->f_flag |= ST_NOSUID;
4640 error = copyout(sp, uap->buf, sizeof(*sp));
4641 done:
4642 rel_mplock();
4643 return (error);
4648 * Syscall to push extended attribute configuration information into the
4649 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4650 * a command (int cmd), and attribute name and misc data. For now, the
4651 * attribute name is left in userspace for consumption by the VFS_op.
4652 * It will probably be changed to be copied into sysspace by the
4653 * syscall in the future, once issues with various consumers of the
4654 * attribute code have raised their hands.
4656 * Currently this is used only by UFS Extended Attributes.
4658 * MPALMOSTSAFE
4661 sys_extattrctl(struct extattrctl_args *uap)
4663 struct nlookupdata nd;
4664 struct vnode *vp;
4665 char attrname[EXTATTR_MAXNAMELEN];
4666 int error;
4667 size_t size;
4669 get_mplock();
4671 attrname[0] = 0;
4672 vp = NULL;
4673 error = 0;
4675 if (error == 0 && uap->filename) {
4676 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4677 NLC_FOLLOW);
4678 if (error == 0)
4679 error = nlookup(&nd);
4680 if (error == 0)
4681 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4682 nlookup_done(&nd);
4685 if (error == 0 && uap->attrname) {
4686 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4687 &size);
4690 if (error == 0) {
4691 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4692 if (error == 0)
4693 error = nlookup(&nd);
4694 if (error == 0)
4695 error = ncp_writechk(&nd.nl_nch);
4696 if (error == 0) {
4697 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4698 uap->attrnamespace,
4699 uap->attrname, nd.nl_cred);
4701 nlookup_done(&nd);
4704 rel_mplock();
4706 return (error);
4710 * Syscall to get a named extended attribute on a file or directory.
4712 * MPALMOSTSAFE
4715 sys_extattr_set_file(struct extattr_set_file_args *uap)
4717 char attrname[EXTATTR_MAXNAMELEN];
4718 struct nlookupdata nd;
4719 struct vnode *vp;
4720 struct uio auio;
4721 struct iovec aiov;
4722 int error;
4724 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4725 if (error)
4726 return (error);
4728 vp = NULL;
4729 get_mplock();
4731 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4732 if (error == 0)
4733 error = nlookup(&nd);
4734 if (error == 0)
4735 error = ncp_writechk(&nd.nl_nch);
4736 if (error == 0)
4737 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4738 if (error) {
4739 nlookup_done(&nd);
4740 rel_mplock();
4741 return (error);
4744 bzero(&auio, sizeof(auio));
4745 aiov.iov_base = uap->data;
4746 aiov.iov_len = uap->nbytes;
4747 auio.uio_iov = &aiov;
4748 auio.uio_iovcnt = 1;
4749 auio.uio_offset = 0;
4750 auio.uio_resid = uap->nbytes;
4751 auio.uio_rw = UIO_WRITE;
4752 auio.uio_td = curthread;
4754 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4755 &auio, nd.nl_cred);
4757 vput(vp);
4758 nlookup_done(&nd);
4759 rel_mplock();
4760 return (error);
4764 * Syscall to get a named extended attribute on a file or directory.
4766 * MPALMOSTSAFE
4769 sys_extattr_get_file(struct extattr_get_file_args *uap)
4771 char attrname[EXTATTR_MAXNAMELEN];
4772 struct nlookupdata nd;
4773 struct uio auio;
4774 struct iovec aiov;
4775 struct vnode *vp;
4776 int error;
4778 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4779 if (error)
4780 return (error);
4782 vp = NULL;
4783 get_mplock();
4785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4786 if (error == 0)
4787 error = nlookup(&nd);
4788 if (error == 0)
4789 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4790 if (error) {
4791 nlookup_done(&nd);
4792 rel_mplock();
4793 return (error);
4796 bzero(&auio, sizeof(auio));
4797 aiov.iov_base = uap->data;
4798 aiov.iov_len = uap->nbytes;
4799 auio.uio_iov = &aiov;
4800 auio.uio_iovcnt = 1;
4801 auio.uio_offset = 0;
4802 auio.uio_resid = uap->nbytes;
4803 auio.uio_rw = UIO_READ;
4804 auio.uio_td = curthread;
4806 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4807 &auio, nd.nl_cred);
4808 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4810 vput(vp);
4811 nlookup_done(&nd);
4812 rel_mplock();
4813 return(error);
4817 * Syscall to delete a named extended attribute from a file or directory.
4818 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4820 * MPALMOSTSAFE
4823 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4825 char attrname[EXTATTR_MAXNAMELEN];
4826 struct nlookupdata nd;
4827 struct vnode *vp;
4828 int error;
4830 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4831 if (error)
4832 return(error);
4834 get_mplock();
4835 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4836 if (error == 0)
4837 error = nlookup(&nd);
4838 if (error == 0)
4839 error = ncp_writechk(&nd.nl_nch);
4840 if (error == 0) {
4841 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4842 if (error == 0) {
4843 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4844 attrname, NULL, nd.nl_cred);
4845 vput(vp);
4848 nlookup_done(&nd);
4849 rel_mplock();
4850 return(error);
4854 * Determine if the mount is visible to the process.
4856 static int
4857 chroot_visible_mnt(struct mount *mp, struct proc *p)
4859 struct nchandle nch;
4862 * Traverse from the mount point upwards. If we hit the process
4863 * root then the mount point is visible to the process.
4865 nch = mp->mnt_ncmountpt;
4866 while (nch.ncp) {
4867 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4868 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4869 return(1);
4871 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4872 nch = nch.mount->mnt_ncmounton;
4873 } else {
4874 nch.ncp = nch.ncp->nc_parent;
4879 * If the mount point is not visible to the process, but the
4880 * process root is in a subdirectory of the mount, return
4881 * TRUE anyway.
4883 if (p->p_fd->fd_nrdir.mount == mp)
4884 return(1);
4886 return(0);