iso639: Add Montenegrin.
[dragonfly.git] / sys / kern / vfs_syscalls.c
blob6dcf0389de315997f6df15ff36ceeef0aa3a9aa4
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
35 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/linker.h>
52 #include <sys/stat.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/proc.h>
56 #include <sys/priv.h>
57 #include <sys/jail.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
67 #include <sys/buf2.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
71 #include <vm/vm.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
78 static void mount_warning(struct mount *mp, const char *ctl, ...)
79 __printflike(2, 3);
80 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
81 static int checkvp_chdir (struct vnode *vn, struct thread *td);
82 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
83 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
84 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
85 static int getutimes (struct timeval *, struct timespec *);
86 static int getutimens (const struct timespec *, struct timespec *, int *);
87 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, struct vattr *,
91 const struct timespec *, int);
92 static int usermount = 0; /* if 1, non-root can mount fs. */
94 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
95 "Allow non-root users to mount filesystems");
98 * Virtual File System System Calls
102 * Mount a file system.
104 * mount_args(char *type, char *path, int flags, caddr_t data)
106 * MPALMOSTSAFE
109 sys_mount(struct mount_args *uap)
111 struct thread *td = curthread;
112 struct vnode *vp;
113 struct nchandle nch;
114 struct mount *mp, *nullmp;
115 struct vfsconf *vfsp;
116 int error, flag = 0, flag2 = 0;
117 int hasmount;
118 struct vattr va;
119 struct nlookupdata nd;
120 char fstypename[MFSNAMELEN];
121 struct ucred *cred;
123 cred = td->td_ucred;
124 if (jailed(cred)) {
125 error = EPERM;
126 goto done;
128 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
129 goto done;
132 * Do not allow NFS export by non-root users.
134 if (uap->flags & MNT_EXPORTED) {
135 error = priv_check(td, PRIV_ROOT);
136 if (error)
137 goto done;
140 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142 if (priv_check(td, PRIV_ROOT))
143 uap->flags |= MNT_NOSUID | MNT_NODEV;
146 * Lookup the requested path and extract the nch and vnode.
148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
149 if (error == 0) {
150 if ((error = nlookup(&nd)) == 0) {
151 if (nd.nl_nch.ncp->nc_vp == NULL)
152 error = ENOENT;
155 if (error) {
156 nlookup_done(&nd);
157 goto done;
161 * If the target filesystem is resolved via a nullfs mount, then
162 * nd.nl_nch.mount will be pointing to the nullfs mount structure
163 * instead of the target file system. We need it in case we are
164 * doing an update.
166 nullmp = nd.nl_nch.mount;
169 * Extract the locked+refd ncp and cleanup the nd structure
171 nch = nd.nl_nch;
172 cache_zero(&nd.nl_nch);
173 nlookup_done(&nd);
175 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
176 (mp = cache_findmount(&nch)) != NULL) {
177 cache_dropmount(mp);
178 hasmount = 1;
179 } else {
180 hasmount = 0;
185 * now we have the locked ref'd nch and unreferenced vnode.
187 vp = nch.ncp->nc_vp;
188 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
189 cache_put(&nch);
190 goto done;
192 cache_unlock(&nch);
195 * Extract the file system type. We need to know this early, to take
196 * appropriate actions if we are dealing with a nullfs.
198 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
199 cache_drop(&nch);
200 vput(vp);
201 goto done;
205 * Now we have an unlocked ref'd nch and a locked ref'd vp
207 if (uap->flags & MNT_UPDATE) {
208 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
209 cache_drop(&nch);
210 vput(vp);
211 error = EINVAL;
212 goto done;
215 if (strncmp(fstypename, "null", 5) == 0) {
216 KKASSERT(nullmp);
217 mp = nullmp;
218 } else {
219 mp = vp->v_mount;
222 flag = mp->mnt_flag;
223 flag2 = mp->mnt_kern_flag;
225 * We only allow the filesystem to be reloaded if it
226 * is currently mounted read-only.
228 if ((uap->flags & MNT_RELOAD) &&
229 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
230 cache_drop(&nch);
231 vput(vp);
232 error = EOPNOTSUPP; /* Needs translation */
233 goto done;
236 * Only root, or the user that did the original mount is
237 * permitted to update it.
239 if (mp->mnt_stat.f_owner != cred->cr_uid &&
240 (error = priv_check(td, PRIV_ROOT))) {
241 cache_drop(&nch);
242 vput(vp);
243 goto done;
245 if (vfs_busy(mp, LK_NOWAIT)) {
246 cache_drop(&nch);
247 vput(vp);
248 error = EBUSY;
249 goto done;
251 if (hasmount) {
252 cache_drop(&nch);
253 vfs_unbusy(mp);
254 vput(vp);
255 error = EBUSY;
256 goto done;
258 mp->mnt_flag |=
259 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
260 lwkt_gettoken(&mp->mnt_token);
261 vn_unlock(vp);
262 vfsp = mp->mnt_vfc;
263 goto update;
267 * If the user is not root, ensure that they own the directory
268 * onto which we are attempting to mount.
270 if ((error = VOP_GETATTR(vp, &va)) ||
271 (va.va_uid != cred->cr_uid &&
272 (error = priv_check(td, PRIV_ROOT)))) {
273 cache_drop(&nch);
274 vput(vp);
275 goto done;
277 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
278 cache_drop(&nch);
279 vput(vp);
280 goto done;
282 if (vp->v_type != VDIR) {
283 cache_drop(&nch);
284 vput(vp);
285 error = ENOTDIR;
286 goto done;
288 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
289 cache_drop(&nch);
290 vput(vp);
291 error = EPERM;
292 goto done;
294 vfsp = vfsconf_find_by_name(fstypename);
295 if (vfsp == NULL) {
296 linker_file_t lf;
298 /* Only load modules for root (very important!) */
299 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
300 cache_drop(&nch);
301 vput(vp);
302 goto done;
304 error = linker_load_file(fstypename, &lf);
305 if (error || lf == NULL) {
306 cache_drop(&nch);
307 vput(vp);
308 if (lf == NULL)
309 error = ENODEV;
310 goto done;
312 lf->userrefs++;
313 /* lookup again, see if the VFS was loaded */
314 vfsp = vfsconf_find_by_name(fstypename);
315 if (vfsp == NULL) {
316 lf->userrefs--;
317 linker_file_unload(lf);
318 cache_drop(&nch);
319 vput(vp);
320 error = ENODEV;
321 goto done;
324 if (hasmount) {
325 cache_drop(&nch);
326 vput(vp);
327 error = EBUSY;
328 goto done;
332 * Allocate and initialize the filesystem.
334 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
335 mount_init(mp);
336 vfs_busy(mp, LK_NOWAIT);
337 mp->mnt_op = vfsp->vfc_vfsops;
338 mp->mnt_vfc = vfsp;
339 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
340 vfsp->vfc_refcount++;
341 mp->mnt_stat.f_type = vfsp->vfc_typenum;
342 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
343 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
344 mp->mnt_stat.f_owner = cred->cr_uid;
345 lwkt_gettoken(&mp->mnt_token);
346 vn_unlock(vp);
347 update:
349 * (per-mount token acquired at this point)
351 * Set the mount level flags.
353 if (uap->flags & MNT_RDONLY)
354 mp->mnt_flag |= MNT_RDONLY;
355 else if (mp->mnt_flag & MNT_RDONLY)
356 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
359 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
361 MNT_AUTOMOUNTED);
362 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
363 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
364 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
365 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
366 MNT_AUTOMOUNTED);
369 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
370 * This way the initial VFS_MOUNT() call will also be MPSAFE.
372 if (vfsp->vfc_flags & VFCF_MPSAFE)
373 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
376 * Mount the filesystem.
377 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
378 * get.
380 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
381 if (mp->mnt_flag & MNT_UPDATE) {
382 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
383 mp->mnt_flag &= ~MNT_RDONLY;
384 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
385 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
386 if (error) {
387 mp->mnt_flag = flag;
388 mp->mnt_kern_flag = flag2;
390 lwkt_reltoken(&mp->mnt_token);
391 vfs_unbusy(mp);
392 vrele(vp);
393 cache_drop(&nch);
394 goto done;
396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
399 * Put the new filesystem on the mount list after root. The mount
400 * point gets its own mnt_ncmountpt (unless the VFS already set one
401 * up) which represents the root of the mount. The lookup code
402 * detects the mount point going forward and checks the root of
403 * the mount going backwards.
405 * It is not necessary to invalidate or purge the vnode underneath
406 * because elements under the mount will be given their own glue
407 * namecache record.
409 if (!error) {
410 if (mp->mnt_ncmountpt.ncp == NULL) {
412 * Allocate, then unlock, but leave the ref intact.
413 * This is the mnt_refs (1) that we will retain
414 * through to the unmount.
416 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
417 cache_unlock(&mp->mnt_ncmountpt);
419 vn_unlock(vp);
420 mp->mnt_ncmounton = nch; /* inherits ref */
421 cache_lock(&nch);
422 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
423 cache_unlock(&nch);
424 cache_ismounting(mp);
425 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
427 mountlist_insert(mp, MNTINS_LAST);
428 vn_unlock(vp);
429 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
430 error = vfs_allocate_syncvnode(mp);
431 lwkt_reltoken(&mp->mnt_token);
432 vfs_unbusy(mp);
433 error = VFS_START(mp, 0);
434 vrele(vp);
435 KNOTE(&fs_klist, VQ_MOUNT);
436 } else {
437 vn_syncer_thr_stop(mp);
438 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
439 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
440 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
441 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
442 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
443 mp->mnt_vfc->vfc_refcount--;
444 lwkt_reltoken(&mp->mnt_token);
445 vfs_unbusy(mp);
446 kfree(mp, M_MOUNT);
447 cache_drop(&nch);
448 vput(vp);
450 done:
451 return (error);
455 * Scan all active processes to see if any of them have a current
456 * or root directory onto which the new filesystem has just been
457 * mounted. If so, replace them with the new mount point.
459 * Both old_nch and new_nch are ref'd on call but not locked.
460 * new_nch must be temporarily locked so it can be associated with the
461 * vnode representing the root of the mount point.
463 struct checkdirs_info {
464 struct nchandle old_nch;
465 struct nchandle new_nch;
466 struct vnode *old_vp;
467 struct vnode *new_vp;
470 static int checkdirs_callback(struct proc *p, void *data);
472 static void
473 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
475 struct checkdirs_info info;
476 struct vnode *olddp;
477 struct vnode *newdp;
478 struct mount *mp;
481 * If the old mount point's vnode has a usecount of 1, it is not
482 * being held as a descriptor anywhere.
484 olddp = old_nch->ncp->nc_vp;
485 if (olddp == NULL || VREFCNT(olddp) == 1)
486 return;
489 * Force the root vnode of the new mount point to be resolved
490 * so we can update any matching processes.
492 mp = new_nch->mount;
493 if (VFS_ROOT(mp, &newdp))
494 panic("mount: lost mount");
495 vn_unlock(newdp);
496 cache_lock(new_nch);
497 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
498 cache_setunresolved(new_nch);
499 cache_setvp(new_nch, newdp);
500 cache_unlock(new_nch);
503 * Special handling of the root node
505 if (rootvnode == olddp) {
506 vref(newdp);
507 vfs_cache_setroot(newdp, cache_hold(new_nch));
511 * Pass newdp separately so the callback does not have to access
512 * it via new_nch->ncp->nc_vp.
514 info.old_nch = *old_nch;
515 info.new_nch = *new_nch;
516 info.new_vp = newdp;
517 allproc_scan(checkdirs_callback, &info, 0);
518 vput(newdp);
522 * NOTE: callback is not MP safe because the scanned process's filedesc
523 * structure can be ripped out from under us, amoung other things.
525 static int
526 checkdirs_callback(struct proc *p, void *data)
528 struct checkdirs_info *info = data;
529 struct filedesc *fdp;
530 struct nchandle ncdrop1;
531 struct nchandle ncdrop2;
532 struct vnode *vprele1;
533 struct vnode *vprele2;
535 if ((fdp = p->p_fd) != NULL) {
536 cache_zero(&ncdrop1);
537 cache_zero(&ncdrop2);
538 vprele1 = NULL;
539 vprele2 = NULL;
542 * MPUNSAFE - XXX fdp can be pulled out from under a
543 * foreign process.
545 * A shared filedesc is ok, we don't have to copy it
546 * because we are making this change globally.
548 spin_lock(&fdp->fd_spin);
549 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
550 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
551 vprele1 = fdp->fd_cdir;
552 vref(info->new_vp);
553 fdp->fd_cdir = info->new_vp;
554 ncdrop1 = fdp->fd_ncdir;
555 cache_copy(&info->new_nch, &fdp->fd_ncdir);
557 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
558 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
559 vprele2 = fdp->fd_rdir;
560 vref(info->new_vp);
561 fdp->fd_rdir = info->new_vp;
562 ncdrop2 = fdp->fd_nrdir;
563 cache_copy(&info->new_nch, &fdp->fd_nrdir);
565 spin_unlock(&fdp->fd_spin);
566 if (ncdrop1.ncp)
567 cache_drop(&ncdrop1);
568 if (ncdrop2.ncp)
569 cache_drop(&ncdrop2);
570 if (vprele1)
571 vrele(vprele1);
572 if (vprele2)
573 vrele(vprele2);
575 return(0);
579 * Unmount a file system.
581 * Note: unmount takes a path to the vnode mounted on as argument,
582 * not special file (as before).
584 * umount_args(char *path, int flags)
586 * MPALMOSTSAFE
589 sys_unmount(struct unmount_args *uap)
591 struct thread *td = curthread;
592 struct proc *p __debugvar = td->td_proc;
593 struct mount *mp = NULL;
594 struct nlookupdata nd;
595 int error;
597 KKASSERT(p);
598 if (td->td_ucred->cr_prison != NULL) {
599 error = EPERM;
600 goto done;
602 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
603 goto done;
605 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
606 if (error == 0)
607 error = nlookup(&nd);
608 if (error)
609 goto out;
611 mp = nd.nl_nch.mount;
614 * Only root, or the user that did the original mount is
615 * permitted to unmount this filesystem.
617 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
618 (error = priv_check(td, PRIV_ROOT)))
619 goto out;
622 * Don't allow unmounting the root file system.
624 if (mp->mnt_flag & MNT_ROOTFS) {
625 error = EINVAL;
626 goto out;
630 * Must be the root of the filesystem
632 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
633 error = EINVAL;
634 goto out;
638 * If no error try to issue the unmount. We lose our cache
639 * ref when we call nlookup_done so we must hold the mount point
640 * to prevent use-after-free races.
642 out:
643 if (error == 0) {
644 mount_hold(mp);
645 nlookup_done(&nd);
646 error = dounmount(mp, uap->flags);
647 mount_drop(mp);
648 } else {
649 nlookup_done(&nd);
651 done:
652 return (error);
656 * Do the actual file system unmount (interlocked against the mountlist
657 * token and mp->mnt_token).
659 static int
660 dounmount_interlock(struct mount *mp)
662 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
663 return (EBUSY);
664 mp->mnt_kern_flag |= MNTK_UNMOUNT;
665 return(0);
668 static int
669 unmount_allproc_cb(struct proc *p, void *arg)
671 struct mount *mp;
673 if (p->p_textnch.ncp == NULL)
674 return 0;
676 mp = (struct mount *)arg;
677 if (p->p_textnch.mount == mp)
678 cache_drop(&p->p_textnch);
680 return 0;
684 * The guts of the unmount code. The mount owns one ref and one hold
685 * count. If we successfully interlock the unmount, those refs are ours.
686 * (The ref is from mnt_ncmountpt).
689 dounmount(struct mount *mp, int flags)
691 struct namecache *ncp;
692 struct nchandle nch;
693 struct vnode *vp;
694 int error;
695 int async_flag;
696 int lflags;
697 int freeok = 1;
698 int retry;
700 lwkt_gettoken(&mp->mnt_token);
703 * Exclusive access for unmounting purposes.
705 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
706 goto out;
709 * We now 'own' the last mp->mnt_refs
711 * Allow filesystems to detect that a forced unmount is in progress.
713 if (flags & MNT_FORCE)
714 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
715 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
716 error = lockmgr(&mp->mnt_lock, lflags);
717 if (error) {
718 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
719 if (mp->mnt_kern_flag & MNTK_MWAIT) {
720 mp->mnt_kern_flag &= ~MNTK_MWAIT;
721 wakeup(mp);
723 goto out;
726 if (mp->mnt_flag & MNT_EXPUBLIC)
727 vfs_setpublicfs(NULL, NULL, NULL);
729 vfs_msync(mp, MNT_WAIT);
730 async_flag = mp->mnt_flag & MNT_ASYNC;
731 mp->mnt_flag &=~ MNT_ASYNC;
734 * If this filesystem isn't aliasing other filesystems,
735 * try to invalidate any remaining namecache entries and
736 * check the count afterwords.
738 * We own the last mnt_refs by owning mnt_ncmountpt.
740 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
741 cache_lock(&mp->mnt_ncmountpt);
742 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
743 cache_unlock(&mp->mnt_ncmountpt);
745 cache_clearmntcache();
746 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
747 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
748 allproc_scan(&unmount_allproc_cb, mp, 0);
751 cache_clearmntcache();
752 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
753 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
755 if ((flags & MNT_FORCE) == 0) {
756 error = EBUSY;
757 mount_warning(mp, "Cannot unmount: "
758 "%d namecache "
759 "references still "
760 "present",
761 ncp->nc_refs - 1);
762 } else {
763 mount_warning(mp, "Forced unmount: "
764 "%d namecache "
765 "references still "
766 "present",
767 ncp->nc_refs - 1);
768 freeok = 0;
774 * Decomission our special mnt_syncer vnode. This also stops
775 * the vnlru code. If we are unable to unmount we recommission
776 * the vnode.
778 * Then sync the filesystem.
780 if ((vp = mp->mnt_syncer) != NULL) {
781 mp->mnt_syncer = NULL;
782 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
783 vrele(vp);
785 if ((mp->mnt_flag & MNT_RDONLY) == 0)
786 VFS_SYNC(mp, MNT_WAIT);
789 * nchandle records ref the mount structure. Expect a count of 1
790 * (our mount->mnt_ncmountpt).
792 * Scans can get temporary refs on a mountpoint (thought really
793 * heavy duty stuff like cache_findmount() do not).
795 if (mp->mnt_refs != 1)
796 cache_clearmntcache();
797 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
798 cache_unmounting(mp);
799 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
800 cache_clearmntcache();
802 if (mp->mnt_refs != 1) {
803 if ((flags & MNT_FORCE) == 0) {
804 mount_warning(mp, "Cannot unmount: "
805 "%d mount refs still present",
806 mp->mnt_refs - 1);
807 error = EBUSY;
808 } else {
809 mount_warning(mp, "Forced unmount: "
810 "%d mount refs still present",
811 mp->mnt_refs - 1);
812 freeok = 0;
817 * So far so good, sync the filesystem once more and
818 * call the VFS unmount code if the sync succeeds.
820 if (error == 0) {
821 if (mp->mnt_flag & MNT_RDONLY) {
822 error = VFS_UNMOUNT(mp, flags);
823 } else {
824 error = VFS_SYNC(mp, MNT_WAIT);
825 if ((error == 0) ||
826 (error == EOPNOTSUPP) || /* No sync */
827 (flags & MNT_FORCE)) {
828 error = VFS_UNMOUNT(mp, flags);
834 * If an error occurred we can still recover, restoring the
835 * syncer vnode and misc flags.
837 if (error) {
838 if (mp->mnt_syncer == NULL)
839 vfs_allocate_syncvnode(mp);
840 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
841 mp->mnt_flag |= async_flag;
842 lockmgr(&mp->mnt_lock, LK_RELEASE);
843 if (mp->mnt_kern_flag & MNTK_MWAIT) {
844 mp->mnt_kern_flag &= ~MNTK_MWAIT;
845 wakeup(mp);
847 goto out;
850 * Clean up any journals still associated with the mount after
851 * filesystem activity has ceased.
853 journal_remove_all_journals(mp,
854 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
856 mountlist_remove(mp);
859 * Remove any installed vnode ops here so the individual VFSs don't
860 * have to.
862 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
864 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
865 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
866 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
867 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
868 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
870 if (mp->mnt_ncmountpt.ncp != NULL) {
871 nch = mp->mnt_ncmountpt;
872 cache_zero(&mp->mnt_ncmountpt);
873 cache_clrmountpt(&nch);
874 cache_drop(&nch);
876 if (mp->mnt_ncmounton.ncp != NULL) {
877 cache_unmounting(mp);
878 nch = mp->mnt_ncmounton;
879 cache_zero(&mp->mnt_ncmounton);
880 cache_clrmountpt(&nch);
881 cache_drop(&nch);
884 mp->mnt_vfc->vfc_refcount--;
885 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
886 panic("unmount: dangling vnode");
889 * Release the lock
891 lockmgr(&mp->mnt_lock, LK_RELEASE);
892 if (mp->mnt_kern_flag & MNTK_MWAIT) {
893 mp->mnt_kern_flag &= ~MNTK_MWAIT;
894 wakeup(mp);
898 * If we reach here and freeok != 0 we must free the mount.
899 * mnt_refs should already have dropped to 0, so if it is not
900 * zero we must cycle the caches and wait.
902 * When we are satisfied that the mount has disconnected we can
903 * drop the hold on the mp that represented the mount (though the
904 * caller might actually have another, so the caller's drop may
905 * do the actual free).
907 if (freeok) {
908 if (mp->mnt_refs > 0)
909 cache_clearmntcache();
910 while (mp->mnt_refs > 0) {
911 cache_unmounting(mp);
912 wakeup(mp);
913 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
914 cache_clearmntcache();
916 lwkt_reltoken(&mp->mnt_token);
917 mount_drop(mp);
918 mp = NULL;
920 error = 0;
921 KNOTE(&fs_klist, VQ_UNMOUNT);
922 out:
923 if (mp)
924 lwkt_reltoken(&mp->mnt_token);
925 return (error);
928 static
929 void
930 mount_warning(struct mount *mp, const char *ctl, ...)
932 char *ptr;
933 char *buf;
934 __va_list va;
936 __va_start(va, ctl);
937 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
938 &ptr, &buf, 0) == 0) {
939 kprintf("unmount(%s): ", ptr);
940 kvprintf(ctl, va);
941 kprintf("\n");
942 kfree(buf, M_TEMP);
943 } else {
944 kprintf("unmount(%p", mp);
945 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
946 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
947 kprintf("): ");
948 kvprintf(ctl, va);
949 kprintf("\n");
951 __va_end(va);
955 * Shim cache_fullpath() to handle the case where a process is chrooted into
956 * a subdirectory of a mount. In this case if the root mount matches the
957 * process root directory's mount we have to specify the process's root
958 * directory instead of the mount point, because the mount point might
959 * be above the root directory.
961 static
963 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
965 struct nchandle *nch;
967 if (p && p->p_fd->fd_nrdir.mount == mp)
968 nch = &p->p_fd->fd_nrdir;
969 else
970 nch = &mp->mnt_ncmountpt;
971 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
975 * Sync each mounted filesystem.
978 #ifdef DEBUG
979 static int syncprt = 0;
980 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
981 #endif /* DEBUG */
983 static int sync_callback(struct mount *mp, void *data);
986 sys_sync(struct sync_args *uap)
988 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
989 return (0);
992 static
994 sync_callback(struct mount *mp, void *data __unused)
996 int asyncflag;
998 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
999 lwkt_gettoken(&mp->mnt_token);
1000 asyncflag = mp->mnt_flag & MNT_ASYNC;
1001 mp->mnt_flag &= ~MNT_ASYNC;
1002 lwkt_reltoken(&mp->mnt_token);
1003 vfs_msync(mp, MNT_NOWAIT);
1004 VFS_SYNC(mp, MNT_NOWAIT);
1005 lwkt_gettoken(&mp->mnt_token);
1006 mp->mnt_flag |= asyncflag;
1007 lwkt_reltoken(&mp->mnt_token);
1009 return(0);
1012 /* XXX PRISON: could be per prison flag */
1013 static int prison_quotas;
1014 #if 0
1015 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1016 #endif
1019 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1021 * Change filesystem quotas.
1023 * MPALMOSTSAFE
1026 sys_quotactl(struct quotactl_args *uap)
1028 struct nlookupdata nd;
1029 struct thread *td;
1030 struct mount *mp;
1031 int error;
1033 td = curthread;
1034 if (td->td_ucred->cr_prison && !prison_quotas) {
1035 error = EPERM;
1036 goto done;
1039 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1040 if (error == 0)
1041 error = nlookup(&nd);
1042 if (error == 0) {
1043 mp = nd.nl_nch.mount;
1044 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1045 uap->arg, nd.nl_cred);
1047 nlookup_done(&nd);
1048 done:
1049 return (error);
1053 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1054 * void *buf, int buflen)
1056 * This function operates on a mount point and executes the specified
1057 * operation using the specified control data, and possibly returns data.
1059 * The actual number of bytes stored in the result buffer is returned, 0
1060 * if none, otherwise an error is returned.
1062 * MPALMOSTSAFE
1065 sys_mountctl(struct mountctl_args *uap)
1067 struct thread *td = curthread;
1068 struct proc *p = td->td_proc;
1069 struct file *fp;
1070 void *ctl = NULL;
1071 void *buf = NULL;
1072 char *path = NULL;
1073 int error;
1076 * Sanity and permissions checks. We must be root.
1078 KKASSERT(p);
1079 if (td->td_ucred->cr_prison != NULL)
1080 return (EPERM);
1081 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1082 (error = priv_check(td, PRIV_ROOT)) != 0)
1083 return (error);
1086 * Argument length checks
1088 if (uap->ctllen < 0 || uap->ctllen > 1024)
1089 return (EINVAL);
1090 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1091 return (EINVAL);
1092 if (uap->path == NULL)
1093 return (EINVAL);
1096 * Allocate the necessary buffers and copyin data
1098 path = objcache_get(namei_oc, M_WAITOK);
1099 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1100 if (error)
1101 goto done;
1103 if (uap->ctllen) {
1104 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1105 error = copyin(uap->ctl, ctl, uap->ctllen);
1106 if (error)
1107 goto done;
1109 if (uap->buflen)
1110 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1113 * Validate the descriptor
1115 if (uap->fd >= 0) {
1116 fp = holdfp(p->p_fd, uap->fd, -1);
1117 if (fp == NULL) {
1118 error = EBADF;
1119 goto done;
1121 } else {
1122 fp = NULL;
1126 * Execute the internal kernel function and clean up.
1128 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1129 if (fp)
1130 fdrop(fp);
1131 if (error == 0 && uap->sysmsg_result > 0)
1132 error = copyout(buf, uap->buf, uap->sysmsg_result);
1133 done:
1134 if (path)
1135 objcache_put(namei_oc, path);
1136 if (ctl)
1137 kfree(ctl, M_TEMP);
1138 if (buf)
1139 kfree(buf, M_TEMP);
1140 return (error);
1144 * Execute a mount control operation by resolving the path to a mount point
1145 * and calling vop_mountctl().
1147 * Use the mount point from the nch instead of the vnode so nullfs mounts
1148 * can properly spike the VOP.
1151 kern_mountctl(const char *path, int op, struct file *fp,
1152 const void *ctl, int ctllen,
1153 void *buf, int buflen, int *res)
1155 struct vnode *vp;
1156 struct nlookupdata nd;
1157 struct nchandle nch;
1158 struct mount *mp;
1159 int error;
1161 *res = 0;
1162 vp = NULL;
1163 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1164 if (error)
1165 return (error);
1166 error = nlookup(&nd);
1167 if (error) {
1168 nlookup_done(&nd);
1169 return (error);
1171 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1172 if (error) {
1173 nlookup_done(&nd);
1174 return (error);
1178 * Yes, all this is needed to use the nch.mount below, because
1179 * we must maintain a ref on the mount to avoid ripouts (e.g.
1180 * due to heavy mount/unmount use by synth or poudriere).
1182 nch = nd.nl_nch;
1183 cache_zero(&nd.nl_nch);
1184 cache_unlock(&nch);
1185 nlookup_done(&nd);
1186 vn_unlock(vp);
1188 mp = nch.mount;
1191 * Must be the root of the filesystem
1193 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1194 cache_drop(&nch);
1195 vrele(vp);
1196 return (EINVAL);
1198 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1199 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1200 path);
1201 cache_drop(&nch);
1202 vrele(vp);
1203 return (EINVAL);
1205 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1206 buf, buflen, res);
1207 vrele(vp);
1208 cache_drop(&nch);
1210 return (error);
1214 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1216 struct thread *td = curthread;
1217 struct proc *p = td->td_proc;
1218 struct mount *mp;
1219 struct statfs *sp;
1220 char *fullpath, *freepath;
1221 int error;
1223 if ((error = nlookup(nd)) != 0)
1224 return (error);
1225 mp = nd->nl_nch.mount;
1226 sp = &mp->mnt_stat;
1227 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1228 return (error);
1230 error = mount_path(p, mp, &fullpath, &freepath);
1231 if (error)
1232 return(error);
1233 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1234 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1235 kfree(freepath, M_TEMP);
1237 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1238 bcopy(sp, buf, sizeof(*buf));
1239 /* Only root should have access to the fsid's. */
1240 if (priv_check(td, PRIV_ROOT))
1241 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1242 return (0);
1246 * statfs_args(char *path, struct statfs *buf)
1248 * Get filesystem statistics.
1251 sys_statfs(struct statfs_args *uap)
1253 struct nlookupdata nd;
1254 struct statfs buf;
1255 int error;
1257 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1258 if (error == 0)
1259 error = kern_statfs(&nd, &buf);
1260 nlookup_done(&nd);
1261 if (error == 0)
1262 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1263 return (error);
1267 kern_fstatfs(int fd, struct statfs *buf)
1269 struct thread *td = curthread;
1270 struct proc *p = td->td_proc;
1271 struct file *fp;
1272 struct mount *mp;
1273 struct statfs *sp;
1274 char *fullpath, *freepath;
1275 int error;
1277 KKASSERT(p);
1278 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1279 return (error);
1282 * Try to use mount info from any overlays rather than the
1283 * mount info for the underlying vnode, otherwise we will
1284 * fail when operating on null-mounted paths inside a chroot.
1286 if ((mp = fp->f_nchandle.mount) == NULL)
1287 mp = ((struct vnode *)fp->f_data)->v_mount;
1288 if (mp == NULL) {
1289 error = EBADF;
1290 goto done;
1292 if (fp->f_cred == NULL) {
1293 error = EINVAL;
1294 goto done;
1296 sp = &mp->mnt_stat;
1297 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1298 goto done;
1300 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1301 goto done;
1302 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1303 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1304 kfree(freepath, M_TEMP);
1306 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1307 bcopy(sp, buf, sizeof(*buf));
1309 /* Only root should have access to the fsid's. */
1310 if (priv_check(td, PRIV_ROOT))
1311 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1312 error = 0;
1313 done:
1314 fdrop(fp);
1315 return (error);
1319 * fstatfs_args(int fd, struct statfs *buf)
1321 * Get filesystem statistics.
1324 sys_fstatfs(struct fstatfs_args *uap)
1326 struct statfs buf;
1327 int error;
1329 error = kern_fstatfs(uap->fd, &buf);
1331 if (error == 0)
1332 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1333 return (error);
1337 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1339 struct mount *mp;
1340 struct statvfs *sp;
1341 int error;
1343 if ((error = nlookup(nd)) != 0)
1344 return (error);
1345 mp = nd->nl_nch.mount;
1346 sp = &mp->mnt_vstat;
1347 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1348 return (error);
1350 sp->f_flag = 0;
1351 if (mp->mnt_flag & MNT_RDONLY)
1352 sp->f_flag |= ST_RDONLY;
1353 if (mp->mnt_flag & MNT_NOSUID)
1354 sp->f_flag |= ST_NOSUID;
1355 bcopy(sp, buf, sizeof(*buf));
1356 return (0);
1360 * statfs_args(char *path, struct statfs *buf)
1362 * Get filesystem statistics.
1365 sys_statvfs(struct statvfs_args *uap)
1367 struct nlookupdata nd;
1368 struct statvfs buf;
1369 int error;
1371 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1372 if (error == 0)
1373 error = kern_statvfs(&nd, &buf);
1374 nlookup_done(&nd);
1375 if (error == 0)
1376 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1377 return (error);
1381 kern_fstatvfs(int fd, struct statvfs *buf)
1383 struct thread *td = curthread;
1384 struct proc *p = td->td_proc;
1385 struct file *fp;
1386 struct mount *mp;
1387 struct statvfs *sp;
1388 int error;
1390 KKASSERT(p);
1391 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1392 return (error);
1393 if ((mp = fp->f_nchandle.mount) == NULL)
1394 mp = ((struct vnode *)fp->f_data)->v_mount;
1395 if (mp == NULL) {
1396 error = EBADF;
1397 goto done;
1399 if (fp->f_cred == NULL) {
1400 error = EINVAL;
1401 goto done;
1403 sp = &mp->mnt_vstat;
1404 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1405 goto done;
1407 sp->f_flag = 0;
1408 if (mp->mnt_flag & MNT_RDONLY)
1409 sp->f_flag |= ST_RDONLY;
1410 if (mp->mnt_flag & MNT_NOSUID)
1411 sp->f_flag |= ST_NOSUID;
1413 bcopy(sp, buf, sizeof(*buf));
1414 error = 0;
1415 done:
1416 fdrop(fp);
1417 return (error);
1421 * fstatfs_args(int fd, struct statfs *buf)
1423 * Get filesystem statistics.
1426 sys_fstatvfs(struct fstatvfs_args *uap)
1428 struct statvfs buf;
1429 int error;
1431 error = kern_fstatvfs(uap->fd, &buf);
1433 if (error == 0)
1434 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1435 return (error);
1439 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1441 * Get statistics on all filesystems.
1444 struct getfsstat_info {
1445 struct statfs *sfsp;
1446 long count;
1447 long maxcount;
1448 int error;
1449 int flags;
1450 struct thread *td;
1453 static int getfsstat_callback(struct mount *, void *);
1456 sys_getfsstat(struct getfsstat_args *uap)
1458 struct thread *td = curthread;
1459 struct getfsstat_info info;
1461 bzero(&info, sizeof(info));
1463 info.maxcount = uap->bufsize / sizeof(struct statfs);
1464 info.sfsp = uap->buf;
1465 info.count = 0;
1466 info.flags = uap->flags;
1467 info.td = td;
1469 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1470 if (info.sfsp && info.count > info.maxcount)
1471 uap->sysmsg_result = info.maxcount;
1472 else
1473 uap->sysmsg_result = info.count;
1474 return (info.error);
1477 static int
1478 getfsstat_callback(struct mount *mp, void *data)
1480 struct getfsstat_info *info = data;
1481 struct statfs *sp;
1482 char *freepath;
1483 char *fullpath;
1484 int error;
1486 if (info->sfsp && info->count < info->maxcount) {
1487 if (info->td->td_proc &&
1488 !chroot_visible_mnt(mp, info->td->td_proc)) {
1489 return(0);
1491 sp = &mp->mnt_stat;
1494 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1495 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1496 * overrides MNT_WAIT.
1498 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1499 (info->flags & MNT_WAIT)) &&
1500 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1501 return(0);
1503 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1505 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1506 if (error) {
1507 info->error = error;
1508 return(-1);
1510 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1511 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1512 kfree(freepath, M_TEMP);
1514 error = copyout(sp, info->sfsp, sizeof(*sp));
1515 if (error) {
1516 info->error = error;
1517 return (-1);
1519 ++info->sfsp;
1521 info->count++;
1522 return(0);
1526 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1527 long bufsize, int flags)
1529 * Get statistics on all filesystems.
1532 struct getvfsstat_info {
1533 struct statfs *sfsp;
1534 struct statvfs *vsfsp;
1535 long count;
1536 long maxcount;
1537 int error;
1538 int flags;
1539 struct thread *td;
1542 static int getvfsstat_callback(struct mount *, void *);
1545 sys_getvfsstat(struct getvfsstat_args *uap)
1547 struct thread *td = curthread;
1548 struct getvfsstat_info info;
1550 bzero(&info, sizeof(info));
1552 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1553 info.sfsp = uap->buf;
1554 info.vsfsp = uap->vbuf;
1555 info.count = 0;
1556 info.flags = uap->flags;
1557 info.td = td;
1559 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1560 if (info.vsfsp && info.count > info.maxcount)
1561 uap->sysmsg_result = info.maxcount;
1562 else
1563 uap->sysmsg_result = info.count;
1564 return (info.error);
1567 static int
1568 getvfsstat_callback(struct mount *mp, void *data)
1570 struct getvfsstat_info *info = data;
1571 struct statfs *sp;
1572 struct statvfs *vsp;
1573 char *freepath;
1574 char *fullpath;
1575 int error;
1577 if (info->vsfsp && info->count < info->maxcount) {
1578 if (info->td->td_proc &&
1579 !chroot_visible_mnt(mp, info->td->td_proc)) {
1580 return(0);
1582 sp = &mp->mnt_stat;
1583 vsp = &mp->mnt_vstat;
1586 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1587 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1588 * overrides MNT_WAIT.
1590 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1591 (info->flags & MNT_WAIT)) &&
1592 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1593 return(0);
1595 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1597 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1598 (info->flags & MNT_WAIT)) &&
1599 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1600 return(0);
1602 vsp->f_flag = 0;
1603 if (mp->mnt_flag & MNT_RDONLY)
1604 vsp->f_flag |= ST_RDONLY;
1605 if (mp->mnt_flag & MNT_NOSUID)
1606 vsp->f_flag |= ST_NOSUID;
1608 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1609 if (error) {
1610 info->error = error;
1611 return(-1);
1613 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1614 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1615 kfree(freepath, M_TEMP);
1617 error = copyout(sp, info->sfsp, sizeof(*sp));
1618 if (error == 0)
1619 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1620 if (error) {
1621 info->error = error;
1622 return (-1);
1624 ++info->sfsp;
1625 ++info->vsfsp;
1627 info->count++;
1628 return(0);
1633 * fchdir_args(int fd)
1635 * Change current working directory to a given file descriptor.
1638 sys_fchdir(struct fchdir_args *uap)
1640 struct thread *td = curthread;
1641 struct proc *p = td->td_proc;
1642 struct filedesc *fdp = p->p_fd;
1643 struct vnode *vp, *ovp;
1644 struct mount *mp;
1645 struct file *fp;
1646 struct nchandle nch, onch, tnch;
1647 int error;
1649 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1650 return (error);
1651 lwkt_gettoken(&p->p_token);
1652 vp = (struct vnode *)fp->f_data;
1653 vref(vp);
1654 vn_lock(vp, LK_SHARED | LK_RETRY);
1655 if (fp->f_nchandle.ncp == NULL)
1656 error = ENOTDIR;
1657 else
1658 error = checkvp_chdir(vp, td);
1659 if (error) {
1660 vput(vp);
1661 goto done;
1663 cache_copy(&fp->f_nchandle, &nch);
1666 * If the ncp has become a mount point, traverse through
1667 * the mount point.
1670 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1671 (mp = cache_findmount(&nch)) != NULL
1673 error = nlookup_mp(mp, &tnch);
1674 if (error == 0) {
1675 cache_unlock(&tnch); /* leave ref intact */
1676 vput(vp);
1677 vp = tnch.ncp->nc_vp;
1678 error = vget(vp, LK_SHARED);
1679 KKASSERT(error == 0);
1680 cache_drop(&nch);
1681 nch = tnch;
1683 cache_dropmount(mp);
1685 if (error == 0) {
1686 spin_lock(&fdp->fd_spin);
1687 ovp = fdp->fd_cdir;
1688 onch = fdp->fd_ncdir;
1689 fdp->fd_cdir = vp;
1690 fdp->fd_ncdir = nch;
1691 spin_unlock(&fdp->fd_spin);
1692 vn_unlock(vp); /* leave ref intact */
1693 cache_drop(&onch);
1694 vrele(ovp);
1695 } else {
1696 cache_drop(&nch);
1697 vput(vp);
1699 fdrop(fp);
1700 done:
1701 lwkt_reltoken(&p->p_token);
1702 return (error);
1706 kern_chdir(struct nlookupdata *nd)
1708 struct thread *td = curthread;
1709 struct proc *p = td->td_proc;
1710 struct filedesc *fdp = p->p_fd;
1711 struct vnode *vp, *ovp;
1712 struct nchandle onch;
1713 int error;
1715 nd->nl_flags |= NLC_SHAREDLOCK;
1716 if ((error = nlookup(nd)) != 0)
1717 return (error);
1718 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1719 return (ENOENT);
1720 if ((error = vget(vp, LK_SHARED)) != 0)
1721 return (error);
1723 lwkt_gettoken(&p->p_token);
1724 error = checkvp_chdir(vp, td);
1725 vn_unlock(vp);
1726 if (error == 0) {
1727 spin_lock(&fdp->fd_spin);
1728 ovp = fdp->fd_cdir;
1729 onch = fdp->fd_ncdir;
1730 fdp->fd_ncdir = nd->nl_nch;
1731 fdp->fd_cdir = vp;
1732 spin_unlock(&fdp->fd_spin);
1733 cache_unlock(&nd->nl_nch); /* leave reference intact */
1734 cache_drop(&onch);
1735 vrele(ovp);
1736 cache_zero(&nd->nl_nch);
1737 } else {
1738 vrele(vp);
1740 lwkt_reltoken(&p->p_token);
1741 return (error);
1745 * chdir_args(char *path)
1747 * Change current working directory (``.'').
1750 sys_chdir(struct chdir_args *uap)
1752 struct nlookupdata nd;
1753 int error;
1755 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1756 if (error == 0)
1757 error = kern_chdir(&nd);
1758 nlookup_done(&nd);
1759 return (error);
1763 * Helper function for raised chroot(2) security function: Refuse if
1764 * any filedescriptors are open directories.
1766 static int
1767 chroot_refuse_vdir_fds(struct filedesc *fdp)
1769 struct vnode *vp;
1770 struct file *fp;
1771 int error;
1772 int fd;
1774 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1775 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1776 continue;
1777 vp = (struct vnode *)fp->f_data;
1778 if (vp->v_type != VDIR) {
1779 fdrop(fp);
1780 continue;
1782 fdrop(fp);
1783 return(EPERM);
1785 return (0);
1789 * This sysctl determines if we will allow a process to chroot(2) if it
1790 * has a directory open:
1791 * 0: disallowed for all processes.
1792 * 1: allowed for processes that were not already chroot(2)'ed.
1793 * 2: allowed for all processes.
1796 static int chroot_allow_open_directories = 1;
1798 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1799 &chroot_allow_open_directories, 0, "");
1802 * chroot to the specified namecache entry. We obtain the vp from the
1803 * namecache data. The passed ncp must be locked and referenced and will
1804 * remain locked and referenced on return.
1807 kern_chroot(struct nchandle *nch)
1809 struct thread *td = curthread;
1810 struct proc *p = td->td_proc;
1811 struct filedesc *fdp = p->p_fd;
1812 struct vnode *vp;
1813 int error;
1816 * Only privileged user can chroot
1818 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1819 if (error)
1820 return (error);
1823 * Disallow open directory descriptors (fchdir() breakouts).
1825 if (chroot_allow_open_directories == 0 ||
1826 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1827 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1828 return (error);
1830 if ((vp = nch->ncp->nc_vp) == NULL)
1831 return (ENOENT);
1833 if ((error = vget(vp, LK_SHARED)) != 0)
1834 return (error);
1837 * Check the validity of vp as a directory to change to and
1838 * associate it with rdir/jdir.
1840 error = checkvp_chdir(vp, td);
1841 vn_unlock(vp); /* leave reference intact */
1842 if (error == 0) {
1843 lwkt_gettoken(&p->p_token);
1844 vrele(fdp->fd_rdir);
1845 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1846 cache_drop(&fdp->fd_nrdir);
1847 cache_copy(nch, &fdp->fd_nrdir);
1848 if (fdp->fd_jdir == NULL) {
1849 fdp->fd_jdir = vp;
1850 vref(fdp->fd_jdir);
1851 cache_copy(nch, &fdp->fd_njdir);
1853 if ((p->p_flags & P_DIDCHROOT) == 0) {
1854 p->p_flags |= P_DIDCHROOT;
1855 if (p->p_depth <= 65535 - 32)
1856 p->p_depth += 32;
1858 lwkt_reltoken(&p->p_token);
1859 } else {
1860 vrele(vp);
1862 return (error);
1866 * chroot_args(char *path)
1868 * Change notion of root (``/'') directory.
1871 sys_chroot(struct chroot_args *uap)
1873 struct thread *td __debugvar = curthread;
1874 struct nlookupdata nd;
1875 int error;
1877 KKASSERT(td->td_proc);
1878 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1879 if (error == 0) {
1880 nd.nl_flags |= NLC_EXEC;
1881 error = nlookup(&nd);
1882 if (error == 0)
1883 error = kern_chroot(&nd.nl_nch);
1885 nlookup_done(&nd);
1886 return(error);
1890 sys_chroot_kernel(struct chroot_kernel_args *uap)
1892 struct thread *td = curthread;
1893 struct nlookupdata nd;
1894 struct nchandle *nch;
1895 struct vnode *vp;
1896 int error;
1898 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1899 if (error)
1900 goto error_nond;
1902 error = nlookup(&nd);
1903 if (error)
1904 goto error_out;
1906 nch = &nd.nl_nch;
1908 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1909 if (error)
1910 goto error_out;
1912 if ((vp = nch->ncp->nc_vp) == NULL) {
1913 error = ENOENT;
1914 goto error_out;
1917 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1918 goto error_out;
1920 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1921 vfs_cache_setroot(vp, cache_hold(nch));
1923 error_out:
1924 nlookup_done(&nd);
1925 error_nond:
1926 return(error);
1930 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1931 * determine whether it is legal to chdir to the vnode. The vnode's state
1932 * is not changed by this call.
1934 static int
1935 checkvp_chdir(struct vnode *vp, struct thread *td)
1937 int error;
1939 if (vp->v_type != VDIR)
1940 error = ENOTDIR;
1941 else
1942 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1943 return (error);
1947 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1949 struct thread *td = curthread;
1950 struct proc *p = td->td_proc;
1951 struct lwp *lp = td->td_lwp;
1952 struct filedesc *fdp = p->p_fd;
1953 int cmode, flags;
1954 struct file *nfp;
1955 struct file *fp;
1956 struct vnode *vp;
1957 int type, indx, error = 0;
1958 struct flock lf;
1960 if ((oflags & O_ACCMODE) == O_ACCMODE)
1961 return (EINVAL);
1962 flags = FFLAGS(oflags);
1963 error = falloc(lp, &nfp, NULL);
1964 if (error)
1965 return (error);
1966 fp = nfp;
1967 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1970 * XXX p_dupfd is a real mess. It allows a device to return a
1971 * file descriptor to be duplicated rather then doing the open
1972 * itself.
1974 lp->lwp_dupfd = -1;
1977 * Call vn_open() to do the lookup and assign the vnode to the
1978 * file pointer. vn_open() does not change the ref count on fp
1979 * and the vnode, on success, will be inherited by the file pointer
1980 * and unlocked.
1982 * Request a shared lock on the vnode if possible.
1984 * Executable binaries can race VTEXT against O_RDWR opens, so
1985 * use an exclusive lock for O_RDWR opens as well.
1987 * NOTE: We need a flag to separate terminal vnode locking from
1988 * parent locking. O_CREAT needs parent locking, but O_TRUNC
1989 * and O_RDWR only need to lock the terminal vnode exclusively.
1991 nd->nl_flags |= NLC_LOCKVP;
1992 if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
1993 nd->nl_flags |= NLC_SHAREDLOCK;
1995 error = vn_open(nd, fp, flags, cmode);
1996 nlookup_done(nd);
1998 if (error) {
2000 * handle special fdopen() case. bleh. dupfdopen() is
2001 * responsible for dropping the old contents of ofiles[indx]
2002 * if it succeeds.
2004 * Note that fsetfd() will add a ref to fp which represents
2005 * the fd_files[] assignment. We must still drop our
2006 * reference.
2008 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2009 if (fdalloc(p, 0, &indx) == 0) {
2010 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
2011 if (error == 0) {
2012 *res = indx;
2013 fdrop(fp); /* our ref */
2014 return (0);
2016 fsetfd(fdp, NULL, indx);
2019 fdrop(fp); /* our ref */
2020 if (error == ERESTART)
2021 error = EINTR;
2022 return (error);
2026 * ref the vnode for ourselves so it can't be ripped out from under
2027 * is. XXX need an ND flag to request that the vnode be returned
2028 * anyway.
2030 * Reserve a file descriptor but do not assign it until the open
2031 * succeeds.
2033 vp = (struct vnode *)fp->f_data;
2034 vref(vp);
2035 if ((error = fdalloc(p, 0, &indx)) != 0) {
2036 fdrop(fp);
2037 vrele(vp);
2038 return (error);
2042 * If no error occurs the vp will have been assigned to the file
2043 * pointer.
2045 lp->lwp_dupfd = 0;
2047 if (flags & (O_EXLOCK | O_SHLOCK)) {
2048 lf.l_whence = SEEK_SET;
2049 lf.l_start = 0;
2050 lf.l_len = 0;
2051 if (flags & O_EXLOCK)
2052 lf.l_type = F_WRLCK;
2053 else
2054 lf.l_type = F_RDLCK;
2055 if (flags & FNONBLOCK)
2056 type = 0;
2057 else
2058 type = F_WAIT;
2060 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2062 * lock request failed. Clean up the reserved
2063 * descriptor.
2065 vrele(vp);
2066 fsetfd(fdp, NULL, indx);
2067 fdrop(fp);
2068 return (error);
2070 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2072 #if 0
2074 * Assert that all regular file vnodes were created with a object.
2076 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2077 ("open: regular file has no backing object after vn_open"));
2078 #endif
2080 vrele(vp);
2083 * release our private reference, leaving the one associated with the
2084 * descriptor table intact.
2086 if (oflags & O_CLOEXEC)
2087 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2088 fsetfd(fdp, fp, indx);
2089 fdrop(fp);
2090 *res = indx;
2091 return (error);
2095 * open_args(char *path, int flags, int mode)
2097 * Check permissions, allocate an open file structure,
2098 * and call the device open routine if any.
2101 sys_open(struct open_args *uap)
2103 struct nlookupdata nd;
2104 int error;
2106 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2107 if (error == 0) {
2108 error = kern_open(&nd, uap->flags,
2109 uap->mode, &uap->sysmsg_result);
2111 nlookup_done(&nd);
2112 return (error);
2116 * openat_args(int fd, char *path, int flags, int mode)
2119 sys_openat(struct openat_args *uap)
2121 struct nlookupdata nd;
2122 int error;
2123 struct file *fp;
2125 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2126 if (error == 0) {
2127 error = kern_open(&nd, uap->flags, uap->mode,
2128 &uap->sysmsg_result);
2130 nlookup_done_at(&nd, fp);
2131 return (error);
2135 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2137 struct thread *td = curthread;
2138 struct proc *p = td->td_proc;
2139 struct vnode *vp;
2140 struct vattr vattr;
2141 int error;
2142 int whiteout = 0;
2144 KKASSERT(p);
2146 VATTR_NULL(&vattr);
2147 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2148 vattr.va_rmajor = rmajor;
2149 vattr.va_rminor = rminor;
2151 switch (mode & S_IFMT) {
2152 case S_IFMT: /* used by badsect to flag bad sectors */
2153 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2154 vattr.va_type = VBAD;
2155 break;
2156 case S_IFCHR:
2157 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2158 vattr.va_type = VCHR;
2159 break;
2160 case S_IFBLK:
2161 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2162 vattr.va_type = VBLK;
2163 break;
2164 case S_IFWHT:
2165 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2166 whiteout = 1;
2167 break;
2168 case S_IFDIR: /* special directories support for HAMMER */
2169 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2170 vattr.va_type = VDIR;
2171 break;
2172 default:
2173 error = EINVAL;
2174 break;
2177 if (error)
2178 return (error);
2180 bwillinode(1);
2181 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2182 if ((error = nlookup(nd)) != 0)
2183 return (error);
2184 if (nd->nl_nch.ncp->nc_vp)
2185 return (EEXIST);
2186 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2187 return (error);
2189 if (whiteout) {
2190 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2191 nd->nl_cred, NAMEI_CREATE);
2192 } else {
2193 vp = NULL;
2194 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2195 &vp, nd->nl_cred, &vattr);
2196 if (error == 0)
2197 vput(vp);
2199 return (error);
2203 * mknod_args(char *path, int mode, int dev)
2205 * Create a special file.
2208 sys_mknod(struct mknod_args *uap)
2210 struct nlookupdata nd;
2211 int error;
2213 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2214 if (error == 0) {
2215 error = kern_mknod(&nd, uap->mode,
2216 umajor(uap->dev), uminor(uap->dev));
2218 nlookup_done(&nd);
2219 return (error);
2223 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2225 * Create a special file. The path is relative to the directory associated
2226 * with fd.
2229 sys_mknodat(struct mknodat_args *uap)
2231 struct nlookupdata nd;
2232 struct file *fp;
2233 int error;
2235 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2236 if (error == 0) {
2237 error = kern_mknod(&nd, uap->mode,
2238 umajor(uap->dev), uminor(uap->dev));
2240 nlookup_done_at(&nd, fp);
2241 return (error);
2245 kern_mkfifo(struct nlookupdata *nd, int mode)
2247 struct thread *td = curthread;
2248 struct proc *p = td->td_proc;
2249 struct vattr vattr;
2250 struct vnode *vp;
2251 int error;
2253 bwillinode(1);
2255 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2256 if ((error = nlookup(nd)) != 0)
2257 return (error);
2258 if (nd->nl_nch.ncp->nc_vp)
2259 return (EEXIST);
2260 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2261 return (error);
2263 VATTR_NULL(&vattr);
2264 vattr.va_type = VFIFO;
2265 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2266 vp = NULL;
2267 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2268 if (error == 0)
2269 vput(vp);
2270 return (error);
2274 * mkfifo_args(char *path, int mode)
2276 * Create a named pipe.
2279 sys_mkfifo(struct mkfifo_args *uap)
2281 struct nlookupdata nd;
2282 int error;
2284 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2285 if (error == 0)
2286 error = kern_mkfifo(&nd, uap->mode);
2287 nlookup_done(&nd);
2288 return (error);
2292 * mkfifoat_args(int fd, char *path, mode_t mode)
2294 * Create a named pipe. The path is relative to the directory associated
2295 * with fd.
2298 sys_mkfifoat(struct mkfifoat_args *uap)
2300 struct nlookupdata nd;
2301 struct file *fp;
2302 int error;
2304 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2305 if (error == 0)
2306 error = kern_mkfifo(&nd, uap->mode);
2307 nlookup_done_at(&nd, fp);
2308 return (error);
2311 static int hardlink_check_uid = 0;
2312 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2313 &hardlink_check_uid, 0,
2314 "Unprivileged processes cannot create hard links to files owned by other "
2315 "users");
2316 static int hardlink_check_gid = 0;
2317 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2318 &hardlink_check_gid, 0,
2319 "Unprivileged processes cannot create hard links to files owned by other "
2320 "groups");
2322 static int
2323 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2325 struct vattr va;
2326 int error;
2329 * Shortcut if disabled
2331 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2332 return (0);
2335 * Privileged user can always hardlink
2337 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2338 return (0);
2341 * Otherwise only if the originating file is owned by the
2342 * same user or group. Note that any group is allowed if
2343 * the file is owned by the caller.
2345 error = VOP_GETATTR(vp, &va);
2346 if (error != 0)
2347 return (error);
2349 if (hardlink_check_uid) {
2350 if (cred->cr_uid != va.va_uid)
2351 return (EPERM);
2354 if (hardlink_check_gid) {
2355 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2356 return (EPERM);
2359 return (0);
2363 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2365 struct thread *td = curthread;
2366 struct vnode *vp;
2367 int error;
2370 * Lookup the source and obtained a locked vnode.
2372 * You may only hardlink a file which you have write permission
2373 * on or which you own.
2375 * XXX relookup on vget failure / race ?
2377 bwillinode(1);
2378 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2379 if ((error = nlookup(nd)) != 0)
2380 return (error);
2381 vp = nd->nl_nch.ncp->nc_vp;
2382 KKASSERT(vp != NULL);
2383 if (vp->v_type == VDIR)
2384 return (EPERM); /* POSIX */
2385 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2386 return (error);
2387 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2388 return (error);
2391 * Unlock the source so we can lookup the target without deadlocking
2392 * (XXX vp is locked already, possible other deadlock?). The target
2393 * must not exist.
2395 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2396 nd->nl_flags &= ~NLC_NCPISLOCKED;
2397 cache_unlock(&nd->nl_nch);
2398 vn_unlock(vp);
2400 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2401 if ((error = nlookup(linknd)) != 0) {
2402 vrele(vp);
2403 return (error);
2405 if (linknd->nl_nch.ncp->nc_vp) {
2406 vrele(vp);
2407 return (EEXIST);
2409 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2410 if (error) {
2411 vrele(vp);
2412 return (error);
2416 * Finally run the new API VOP.
2418 error = can_hardlink(vp, td, td->td_ucred);
2419 if (error == 0) {
2420 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2421 vp, linknd->nl_cred);
2423 vput(vp);
2424 return (error);
2428 * link_args(char *path, char *link)
2430 * Make a hard file link.
2433 sys_link(struct link_args *uap)
2435 struct nlookupdata nd, linknd;
2436 int error;
2438 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2439 if (error == 0) {
2440 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2441 if (error == 0)
2442 error = kern_link(&nd, &linknd);
2443 nlookup_done(&linknd);
2445 nlookup_done(&nd);
2446 return (error);
2450 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2452 * Make a hard file link. The path1 argument is relative to the directory
2453 * associated with fd1, and similarly the path2 argument is relative to
2454 * the directory associated with fd2.
2457 sys_linkat(struct linkat_args *uap)
2459 struct nlookupdata nd, linknd;
2460 struct file *fp1, *fp2;
2461 int error;
2463 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2464 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2465 if (error == 0) {
2466 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2467 uap->path2, UIO_USERSPACE, 0);
2468 if (error == 0)
2469 error = kern_link(&nd, &linknd);
2470 nlookup_done_at(&linknd, fp2);
2472 nlookup_done_at(&nd, fp1);
2473 return (error);
2477 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2479 struct vattr vattr;
2480 struct vnode *vp;
2481 struct vnode *dvp;
2482 int error;
2484 bwillinode(1);
2485 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2486 if ((error = nlookup(nd)) != 0)
2487 return (error);
2488 if (nd->nl_nch.ncp->nc_vp)
2489 return (EEXIST);
2490 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2491 return (error);
2492 dvp = nd->nl_dvp;
2493 VATTR_NULL(&vattr);
2494 vattr.va_mode = mode;
2495 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2496 if (error == 0)
2497 vput(vp);
2498 return (error);
2502 * symlink(char *path, char *link)
2504 * Make a symbolic link.
2507 sys_symlink(struct symlink_args *uap)
2509 struct thread *td = curthread;
2510 struct nlookupdata nd;
2511 char *path;
2512 int error;
2513 int mode;
2515 path = objcache_get(namei_oc, M_WAITOK);
2516 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2517 if (error == 0) {
2518 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2519 if (error == 0) {
2520 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2521 error = kern_symlink(&nd, path, mode);
2523 nlookup_done(&nd);
2525 objcache_put(namei_oc, path);
2526 return (error);
2530 * symlinkat_args(char *path1, int fd, char *path2)
2532 * Make a symbolic link. The path2 argument is relative to the directory
2533 * associated with fd.
2536 sys_symlinkat(struct symlinkat_args *uap)
2538 struct thread *td = curthread;
2539 struct nlookupdata nd;
2540 struct file *fp;
2541 char *path1;
2542 int error;
2543 int mode;
2545 path1 = objcache_get(namei_oc, M_WAITOK);
2546 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2547 if (error == 0) {
2548 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2549 UIO_USERSPACE, 0);
2550 if (error == 0) {
2551 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2552 error = kern_symlink(&nd, path1, mode);
2554 nlookup_done_at(&nd, fp);
2556 objcache_put(namei_oc, path1);
2557 return (error);
2561 * undelete_args(char *path)
2563 * Delete a whiteout from the filesystem.
2566 sys_undelete(struct undelete_args *uap)
2568 struct nlookupdata nd;
2569 int error;
2571 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2572 bwillinode(1);
2573 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2574 if (error == 0)
2575 error = nlookup(&nd);
2576 if (error == 0)
2577 error = ncp_writechk(&nd.nl_nch);
2578 if (error == 0) {
2579 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2580 NAMEI_DELETE);
2582 nlookup_done(&nd);
2583 return (error);
2587 kern_unlink(struct nlookupdata *nd)
2589 int error;
2591 bwillinode(1);
2592 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2593 if ((error = nlookup(nd)) != 0)
2594 return (error);
2595 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2596 return (error);
2597 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2598 return (error);
2602 * unlink_args(char *path)
2604 * Delete a name from the filesystem.
2607 sys_unlink(struct unlink_args *uap)
2609 struct nlookupdata nd;
2610 int error;
2612 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2613 if (error == 0)
2614 error = kern_unlink(&nd);
2615 nlookup_done(&nd);
2616 return (error);
2621 * unlinkat_args(int fd, char *path, int flags)
2623 * Delete the file or directory entry pointed to by fd/path.
2626 sys_unlinkat(struct unlinkat_args *uap)
2628 struct nlookupdata nd;
2629 struct file *fp;
2630 int error;
2632 if (uap->flags & ~AT_REMOVEDIR)
2633 return (EINVAL);
2635 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2636 if (error == 0) {
2637 if (uap->flags & AT_REMOVEDIR)
2638 error = kern_rmdir(&nd);
2639 else
2640 error = kern_unlink(&nd);
2642 nlookup_done_at(&nd, fp);
2643 return (error);
2647 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2649 struct thread *td = curthread;
2650 struct proc *p = td->td_proc;
2651 struct file *fp;
2652 struct vnode *vp;
2653 struct vattr vattr;
2654 off_t new_offset;
2655 int error;
2657 fp = holdfp(p->p_fd, fd, -1);
2658 if (fp == NULL)
2659 return (EBADF);
2660 if (fp->f_type != DTYPE_VNODE) {
2661 error = ESPIPE;
2662 goto done;
2664 vp = (struct vnode *)fp->f_data;
2666 switch (whence) {
2667 case L_INCR:
2668 spin_lock(&fp->f_spin);
2669 new_offset = fp->f_offset + offset;
2670 error = 0;
2671 break;
2672 case L_XTND:
2673 error = VOP_GETATTR(vp, &vattr);
2674 spin_lock(&fp->f_spin);
2675 new_offset = offset + vattr.va_size;
2676 break;
2677 case L_SET:
2678 new_offset = offset;
2679 error = 0;
2680 spin_lock(&fp->f_spin);
2681 break;
2682 default:
2683 new_offset = 0;
2684 error = EINVAL;
2685 spin_lock(&fp->f_spin);
2686 break;
2690 * Validate the seek position. Negative offsets are not allowed
2691 * for regular files or directories.
2693 * Normally we would also not want to allow negative offsets for
2694 * character and block-special devices. However kvm addresses
2695 * on 64 bit architectures might appear to be negative and must
2696 * be allowed.
2698 if (error == 0) {
2699 if (new_offset < 0 &&
2700 (vp->v_type == VREG || vp->v_type == VDIR)) {
2701 error = EINVAL;
2702 } else {
2703 fp->f_offset = new_offset;
2706 *res = fp->f_offset;
2707 spin_unlock(&fp->f_spin);
2708 done:
2709 fdrop(fp);
2710 return (error);
2714 * lseek_args(int fd, int pad, off_t offset, int whence)
2716 * Reposition read/write file offset.
2719 sys_lseek(struct lseek_args *uap)
2721 int error;
2723 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2724 &uap->sysmsg_offset);
2726 return (error);
2730 * Check if current process can access given file. amode is a bitmask of *_OK
2731 * access bits. flags is a bitmask of AT_* flags.
2734 kern_access(struct nlookupdata *nd, int amode, int flags)
2736 struct vnode *vp;
2737 int error, mode;
2739 if (flags & ~AT_EACCESS)
2740 return (EINVAL);
2741 nd->nl_flags |= NLC_SHAREDLOCK;
2742 if ((error = nlookup(nd)) != 0)
2743 return (error);
2744 retry:
2745 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2746 if (error)
2747 return (error);
2749 /* Flags == 0 means only check for existence. */
2750 if (amode) {
2751 mode = 0;
2752 if (amode & R_OK)
2753 mode |= VREAD;
2754 if (amode & W_OK)
2755 mode |= VWRITE;
2756 if (amode & X_OK)
2757 mode |= VEXEC;
2758 if ((mode & VWRITE) == 0 ||
2759 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2760 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2763 * If the file handle is stale we have to re-resolve the
2764 * entry with the ncp held exclusively. This is a hack
2765 * at the moment.
2767 if (error == ESTALE) {
2768 vput(vp);
2769 cache_unlock(&nd->nl_nch);
2770 cache_lock(&nd->nl_nch);
2771 cache_setunresolved(&nd->nl_nch);
2772 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2773 if (error == 0) {
2774 vp = NULL;
2775 goto retry;
2777 return(error);
2780 vput(vp);
2781 return (error);
2785 * access_args(char *path, int flags)
2787 * Check access permissions.
2790 sys_access(struct access_args *uap)
2792 struct nlookupdata nd;
2793 int error;
2795 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2796 if (error == 0)
2797 error = kern_access(&nd, uap->flags, 0);
2798 nlookup_done(&nd);
2799 return (error);
2804 * eaccess_args(char *path, int flags)
2806 * Check access permissions.
2809 sys_eaccess(struct eaccess_args *uap)
2811 struct nlookupdata nd;
2812 int error;
2814 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2815 if (error == 0)
2816 error = kern_access(&nd, uap->flags, AT_EACCESS);
2817 nlookup_done(&nd);
2818 return (error);
2823 * faccessat_args(int fd, char *path, int amode, int flags)
2825 * Check access permissions.
2828 sys_faccessat(struct faccessat_args *uap)
2830 struct nlookupdata nd;
2831 struct file *fp;
2832 int error;
2834 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2835 NLC_FOLLOW);
2836 if (error == 0)
2837 error = kern_access(&nd, uap->amode, uap->flags);
2838 nlookup_done_at(&nd, fp);
2839 return (error);
2843 kern_stat(struct nlookupdata *nd, struct stat *st)
2845 int error;
2846 struct vnode *vp;
2848 nd->nl_flags |= NLC_SHAREDLOCK;
2849 if ((error = nlookup(nd)) != 0)
2850 return (error);
2851 again:
2852 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2853 return (ENOENT);
2855 if ((error = vget(vp, LK_SHARED)) != 0)
2856 return (error);
2857 error = vn_stat(vp, st, nd->nl_cred);
2860 * If the file handle is stale we have to re-resolve the
2861 * entry with the ncp held exclusively. This is a hack
2862 * at the moment.
2864 if (error == ESTALE) {
2865 vput(vp);
2866 cache_unlock(&nd->nl_nch);
2867 cache_lock(&nd->nl_nch);
2868 cache_setunresolved(&nd->nl_nch);
2869 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2870 if (error == 0)
2871 goto again;
2872 } else {
2873 vput(vp);
2875 return (error);
2879 * stat_args(char *path, struct stat *ub)
2881 * Get file status; this version follows links.
2884 sys_stat(struct stat_args *uap)
2886 struct nlookupdata nd;
2887 struct stat st;
2888 int error;
2890 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2891 if (error == 0) {
2892 error = kern_stat(&nd, &st);
2893 if (error == 0)
2894 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2896 nlookup_done(&nd);
2897 return (error);
2901 * lstat_args(char *path, struct stat *ub)
2903 * Get file status; this version does not follow links.
2906 sys_lstat(struct lstat_args *uap)
2908 struct nlookupdata nd;
2909 struct stat st;
2910 int error;
2912 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2913 if (error == 0) {
2914 error = kern_stat(&nd, &st);
2915 if (error == 0)
2916 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2918 nlookup_done(&nd);
2919 return (error);
2923 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2925 * Get status of file pointed to by fd/path.
2928 sys_fstatat(struct fstatat_args *uap)
2930 struct nlookupdata nd;
2931 struct stat st;
2932 int error;
2933 int flags;
2934 struct file *fp;
2936 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2937 return (EINVAL);
2939 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2941 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2942 UIO_USERSPACE, flags);
2943 if (error == 0) {
2944 error = kern_stat(&nd, &st);
2945 if (error == 0)
2946 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2948 nlookup_done_at(&nd, fp);
2949 return (error);
2952 static int
2953 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2955 struct nlookupdata nd;
2956 struct vnode *vp;
2957 int error;
2959 vp = NULL;
2960 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2961 if (error == 0)
2962 error = nlookup(&nd);
2963 if (error == 0)
2964 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2965 nlookup_done(&nd);
2966 if (error == 0) {
2967 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2968 vput(vp);
2970 return (error);
2974 * pathconf_Args(char *path, int name)
2976 * Get configurable pathname variables.
2979 sys_pathconf(struct pathconf_args *uap)
2981 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2982 &uap->sysmsg_reg));
2986 * lpathconf_Args(char *path, int name)
2988 * Get configurable pathname variables, but don't follow symlinks.
2991 sys_lpathconf(struct lpathconf_args *uap)
2993 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2997 * XXX: daver
2998 * kern_readlink isn't properly split yet. There is a copyin burried
2999 * in VOP_READLINK().
3002 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3004 struct thread *td = curthread;
3005 struct vnode *vp;
3006 struct iovec aiov;
3007 struct uio auio;
3008 int error;
3010 nd->nl_flags |= NLC_SHAREDLOCK;
3011 if ((error = nlookup(nd)) != 0)
3012 return (error);
3013 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3014 if (error)
3015 return (error);
3016 if (vp->v_type != VLNK) {
3017 error = EINVAL;
3018 } else {
3019 aiov.iov_base = buf;
3020 aiov.iov_len = count;
3021 auio.uio_iov = &aiov;
3022 auio.uio_iovcnt = 1;
3023 auio.uio_offset = 0;
3024 auio.uio_rw = UIO_READ;
3025 auio.uio_segflg = UIO_USERSPACE;
3026 auio.uio_td = td;
3027 auio.uio_resid = count;
3028 error = VOP_READLINK(vp, &auio, td->td_ucred);
3030 vput(vp);
3031 *res = count - auio.uio_resid;
3032 return (error);
3036 * readlink_args(char *path, char *buf, int count)
3038 * Return target name of a symbolic link.
3041 sys_readlink(struct readlink_args *uap)
3043 struct nlookupdata nd;
3044 int error;
3046 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3047 if (error == 0) {
3048 error = kern_readlink(&nd, uap->buf, uap->count,
3049 &uap->sysmsg_result);
3051 nlookup_done(&nd);
3052 return (error);
3056 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3058 * Return target name of a symbolic link. The path is relative to the
3059 * directory associated with fd.
3062 sys_readlinkat(struct readlinkat_args *uap)
3064 struct nlookupdata nd;
3065 struct file *fp;
3066 int error;
3068 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3069 if (error == 0) {
3070 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3071 &uap->sysmsg_result);
3073 nlookup_done_at(&nd, fp);
3074 return (error);
3077 static int
3078 setfflags(struct vnode *vp, int flags)
3080 struct thread *td = curthread;
3081 int error;
3082 struct vattr vattr;
3085 * Prevent non-root users from setting flags on devices. When
3086 * a device is reused, users can retain ownership of the device
3087 * if they are allowed to set flags and programs assume that
3088 * chown can't fail when done as root.
3090 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3091 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3092 return (error);
3095 * note: vget is required for any operation that might mod the vnode
3096 * so VINACTIVE is properly cleared.
3098 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3099 VATTR_NULL(&vattr);
3100 vattr.va_flags = flags;
3101 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3102 vput(vp);
3104 return (error);
3108 * chflags(char *path, int flags)
3110 * Change flags of a file given a path name.
3113 sys_chflags(struct chflags_args *uap)
3115 struct nlookupdata nd;
3116 struct vnode *vp;
3117 int error;
3119 vp = NULL;
3120 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3121 if (error == 0)
3122 error = nlookup(&nd);
3123 if (error == 0)
3124 error = ncp_writechk(&nd.nl_nch);
3125 if (error == 0)
3126 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3127 nlookup_done(&nd);
3128 if (error == 0) {
3129 error = setfflags(vp, uap->flags);
3130 vrele(vp);
3132 return (error);
3136 * lchflags(char *path, int flags)
3138 * Change flags of a file given a path name, but don't follow symlinks.
3141 sys_lchflags(struct lchflags_args *uap)
3143 struct nlookupdata nd;
3144 struct vnode *vp;
3145 int error;
3147 vp = NULL;
3148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3149 if (error == 0)
3150 error = nlookup(&nd);
3151 if (error == 0)
3152 error = ncp_writechk(&nd.nl_nch);
3153 if (error == 0)
3154 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3155 nlookup_done(&nd);
3156 if (error == 0) {
3157 error = setfflags(vp, uap->flags);
3158 vrele(vp);
3160 return (error);
3164 * fchflags_args(int fd, int flags)
3166 * Change flags of a file given a file descriptor.
3169 sys_fchflags(struct fchflags_args *uap)
3171 struct thread *td = curthread;
3172 struct proc *p = td->td_proc;
3173 struct file *fp;
3174 int error;
3176 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3177 return (error);
3178 if (fp->f_nchandle.ncp)
3179 error = ncp_writechk(&fp->f_nchandle);
3180 if (error == 0)
3181 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3182 fdrop(fp);
3183 return (error);
3187 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3188 * change flags given a pathname relative to a filedescriptor
3190 int sys_chflagsat(struct chflagsat_args *uap)
3192 struct nlookupdata nd;
3193 struct vnode *vp;
3194 struct file *fp;
3195 int error;
3196 int lookupflags;
3198 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3199 return (EINVAL);
3201 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3203 vp = NULL;
3204 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3205 if (error == 0)
3206 error = nlookup(&nd);
3207 if (error == 0)
3208 error = ncp_writechk(&nd.nl_nch);
3209 if (error == 0)
3210 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3211 nlookup_done_at(&nd, fp);
3212 if (error == 0) {
3213 error = setfflags(vp, uap->flags);
3214 vrele(vp);
3216 return (error);
3220 static int
3221 setfmode(struct vnode *vp, int mode)
3223 struct thread *td = curthread;
3224 int error;
3225 struct vattr vattr;
3228 * note: vget is required for any operation that might mod the vnode
3229 * so VINACTIVE is properly cleared.
3231 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3232 VATTR_NULL(&vattr);
3233 vattr.va_mode = mode & ALLPERMS;
3234 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3235 cache_inval_wxok(vp);
3236 vput(vp);
3238 return error;
3242 kern_chmod(struct nlookupdata *nd, int mode)
3244 struct vnode *vp;
3245 int error;
3247 if ((error = nlookup(nd)) != 0)
3248 return (error);
3249 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3250 return (error);
3251 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3252 error = setfmode(vp, mode);
3253 vrele(vp);
3254 return (error);
3258 * chmod_args(char *path, int mode)
3260 * Change mode of a file given path name.
3263 sys_chmod(struct chmod_args *uap)
3265 struct nlookupdata nd;
3266 int error;
3268 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3269 if (error == 0)
3270 error = kern_chmod(&nd, uap->mode);
3271 nlookup_done(&nd);
3272 return (error);
3276 * lchmod_args(char *path, int mode)
3278 * Change mode of a file given path name (don't follow links.)
3281 sys_lchmod(struct lchmod_args *uap)
3283 struct nlookupdata nd;
3284 int error;
3286 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3287 if (error == 0)
3288 error = kern_chmod(&nd, uap->mode);
3289 nlookup_done(&nd);
3290 return (error);
3294 * fchmod_args(int fd, int mode)
3296 * Change mode of a file given a file descriptor.
3299 sys_fchmod(struct fchmod_args *uap)
3301 struct thread *td = curthread;
3302 struct proc *p = td->td_proc;
3303 struct file *fp;
3304 int error;
3306 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3307 return (error);
3308 if (fp->f_nchandle.ncp)
3309 error = ncp_writechk(&fp->f_nchandle);
3310 if (error == 0)
3311 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3312 fdrop(fp);
3313 return (error);
3317 * fchmodat_args(char *path, int mode)
3319 * Change mode of a file pointed to by fd/path.
3322 sys_fchmodat(struct fchmodat_args *uap)
3324 struct nlookupdata nd;
3325 struct file *fp;
3326 int error;
3327 int flags;
3329 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3330 return (EINVAL);
3331 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3333 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3334 UIO_USERSPACE, flags);
3335 if (error == 0)
3336 error = kern_chmod(&nd, uap->mode);
3337 nlookup_done_at(&nd, fp);
3338 return (error);
3341 static int
3342 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3344 struct thread *td = curthread;
3345 int error;
3346 struct vattr vattr;
3347 uid_t o_uid;
3348 gid_t o_gid;
3349 uint64_t size;
3352 * note: vget is required for any operation that might mod the vnode
3353 * so VINACTIVE is properly cleared.
3355 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3356 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3357 return error;
3358 o_uid = vattr.va_uid;
3359 o_gid = vattr.va_gid;
3360 size = vattr.va_size;
3362 VATTR_NULL(&vattr);
3363 vattr.va_uid = uid;
3364 vattr.va_gid = gid;
3365 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3366 vput(vp);
3369 if (error == 0) {
3370 if (uid == -1)
3371 uid = o_uid;
3372 if (gid == -1)
3373 gid = o_gid;
3374 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3375 VFS_ACCOUNT(mp, uid, gid, size);
3378 return error;
3382 kern_chown(struct nlookupdata *nd, int uid, int gid)
3384 struct vnode *vp;
3385 int error;
3387 if ((error = nlookup(nd)) != 0)
3388 return (error);
3389 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3390 return (error);
3391 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3392 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3393 vrele(vp);
3394 return (error);
3398 * chown(char *path, int uid, int gid)
3400 * Set ownership given a path name.
3403 sys_chown(struct chown_args *uap)
3405 struct nlookupdata nd;
3406 int error;
3408 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3409 if (error == 0)
3410 error = kern_chown(&nd, uap->uid, uap->gid);
3411 nlookup_done(&nd);
3412 return (error);
3416 * lchown_args(char *path, int uid, int gid)
3418 * Set ownership given a path name, do not cross symlinks.
3421 sys_lchown(struct lchown_args *uap)
3423 struct nlookupdata nd;
3424 int error;
3426 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3427 if (error == 0)
3428 error = kern_chown(&nd, uap->uid, uap->gid);
3429 nlookup_done(&nd);
3430 return (error);
3434 * fchown_args(int fd, int uid, int gid)
3436 * Set ownership given a file descriptor.
3439 sys_fchown(struct fchown_args *uap)
3441 struct thread *td = curthread;
3442 struct proc *p = td->td_proc;
3443 struct file *fp;
3444 int error;
3446 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3447 return (error);
3448 if (fp->f_nchandle.ncp)
3449 error = ncp_writechk(&fp->f_nchandle);
3450 if (error == 0)
3451 error = setfown(p->p_fd->fd_ncdir.mount,
3452 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3453 fdrop(fp);
3454 return (error);
3458 * fchownat(int fd, char *path, int uid, int gid, int flags)
3460 * Set ownership of file pointed to by fd/path.
3463 sys_fchownat(struct fchownat_args *uap)
3465 struct nlookupdata nd;
3466 struct file *fp;
3467 int error;
3468 int flags;
3470 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3471 return (EINVAL);
3472 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3474 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3475 UIO_USERSPACE, flags);
3476 if (error == 0)
3477 error = kern_chown(&nd, uap->uid, uap->gid);
3478 nlookup_done_at(&nd, fp);
3479 return (error);
3483 static int
3484 getutimes(struct timeval *tvp, struct timespec *tsp)
3486 struct timeval tv[2];
3487 int error;
3489 if (tvp == NULL) {
3490 microtime(&tv[0]);
3491 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3492 tsp[1] = tsp[0];
3493 } else {
3494 if ((error = itimerfix(tvp)) != 0)
3495 return (error);
3496 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3497 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3499 return 0;
3502 static int
3503 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3505 struct timespec tsnow;
3506 int error;
3508 *nullflag = 0;
3509 nanotime(&tsnow);
3510 if (ts == NULL) {
3511 newts[0] = tsnow;
3512 newts[1] = tsnow;
3513 *nullflag = 1;
3514 return (0);
3517 newts[0] = ts[0];
3518 newts[1] = ts[1];
3519 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3520 return (0);
3521 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3522 *nullflag = 1;
3524 if (newts[0].tv_nsec == UTIME_OMIT)
3525 newts[0].tv_sec = VNOVAL;
3526 else if (newts[0].tv_nsec == UTIME_NOW)
3527 newts[0] = tsnow;
3528 else if ((error = itimespecfix(&newts[0])) != 0)
3529 return (error);
3531 if (newts[1].tv_nsec == UTIME_OMIT)
3532 newts[1].tv_sec = VNOVAL;
3533 else if (newts[1].tv_nsec == UTIME_NOW)
3534 newts[1] = tsnow;
3535 else if ((error = itimespecfix(&newts[1])) != 0)
3536 return (error);
3538 return (0);
3541 static int
3542 setutimes(struct vnode *vp, struct vattr *vattr,
3543 const struct timespec *ts, int nullflag)
3545 struct thread *td = curthread;
3546 int error;
3548 VATTR_NULL(vattr);
3549 vattr->va_atime = ts[0];
3550 vattr->va_mtime = ts[1];
3551 if (nullflag)
3552 vattr->va_vaflags |= VA_UTIMES_NULL;
3553 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3555 return error;
3559 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3561 struct timespec ts[2];
3562 int error;
3564 if (tptr) {
3565 if ((error = getutimes(tptr, ts)) != 0)
3566 return (error);
3568 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3569 return (error);
3573 * utimes_args(char *path, struct timeval *tptr)
3575 * Set the access and modification times of a file.
3578 sys_utimes(struct utimes_args *uap)
3580 struct timeval tv[2];
3581 struct nlookupdata nd;
3582 int error;
3584 if (uap->tptr) {
3585 error = copyin(uap->tptr, tv, sizeof(tv));
3586 if (error)
3587 return (error);
3589 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3590 if (error == 0)
3591 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3592 nlookup_done(&nd);
3593 return (error);
3597 * lutimes_args(char *path, struct timeval *tptr)
3599 * Set the access and modification times of a file.
3602 sys_lutimes(struct lutimes_args *uap)
3604 struct timeval tv[2];
3605 struct nlookupdata nd;
3606 int error;
3608 if (uap->tptr) {
3609 error = copyin(uap->tptr, tv, sizeof(tv));
3610 if (error)
3611 return (error);
3613 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3614 if (error == 0)
3615 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3616 nlookup_done(&nd);
3617 return (error);
3621 * Set utimes on a file descriptor. The creds used to open the
3622 * file are used to determine whether the operation is allowed
3623 * or not.
3626 kern_futimens(int fd, struct timespec *ts)
3628 struct thread *td = curthread;
3629 struct proc *p = td->td_proc;
3630 struct timespec newts[2];
3631 struct file *fp;
3632 struct vnode *vp;
3633 struct vattr vattr;
3634 int nullflag;
3635 int error;
3637 error = getutimens(ts, newts, &nullflag);
3638 if (error)
3639 return (error);
3640 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3641 return (error);
3642 if (fp->f_nchandle.ncp)
3643 error = ncp_writechk(&fp->f_nchandle);
3644 if (error == 0) {
3645 vp = fp->f_data;
3646 error = vget(vp, LK_EXCLUSIVE);
3647 if (error == 0) {
3648 error = VOP_GETATTR(vp, &vattr);
3649 if (error == 0) {
3650 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3651 fp->f_cred);
3653 if (error == 0) {
3654 error = setutimes(vp, &vattr, newts, nullflag);
3656 vput(vp);
3659 fdrop(fp);
3660 return (error);
3664 * futimens_args(int fd, struct timespec *ts)
3666 * Set the access and modification times of a file.
3669 sys_futimens(struct futimens_args *uap)
3671 struct timespec ts[2];
3672 int error;
3674 if (uap->ts) {
3675 error = copyin(uap->ts, ts, sizeof(ts));
3676 if (error)
3677 return (error);
3679 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3680 return (error);
3684 kern_futimes(int fd, struct timeval *tptr)
3686 struct timespec ts[2];
3687 int error;
3689 if (tptr) {
3690 if ((error = getutimes(tptr, ts)) != 0)
3691 return (error);
3693 error = kern_futimens(fd, tptr ? ts : NULL);
3694 return (error);
3698 * futimes_args(int fd, struct timeval *tptr)
3700 * Set the access and modification times of a file.
3703 sys_futimes(struct futimes_args *uap)
3705 struct timeval tv[2];
3706 int error;
3708 if (uap->tptr) {
3709 error = copyin(uap->tptr, tv, sizeof(tv));
3710 if (error)
3711 return (error);
3713 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3714 return (error);
3718 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3720 struct timespec newts[2];
3721 struct vnode *vp;
3722 struct vattr vattr;
3723 int nullflag;
3724 int error;
3726 if (flags & ~AT_SYMLINK_NOFOLLOW)
3727 return (EINVAL);
3729 error = getutimens(ts, newts, &nullflag);
3730 if (error)
3731 return (error);
3733 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3734 if ((error = nlookup(nd)) != 0)
3735 return (error);
3736 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3737 return (error);
3738 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3739 return (error);
3740 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3741 error = vget(vp, LK_EXCLUSIVE);
3742 if (error == 0) {
3743 error = setutimes(vp, &vattr, newts, nullflag);
3744 vput(vp);
3747 vrele(vp);
3748 return (error);
3752 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3754 * Set file access and modification times of a file.
3757 sys_utimensat(struct utimensat_args *uap)
3759 struct timespec ts[2];
3760 struct nlookupdata nd;
3761 struct file *fp;
3762 int error;
3763 int flags;
3765 if (uap->ts) {
3766 error = copyin(uap->ts, ts, sizeof(ts));
3767 if (error)
3768 return (error);
3771 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3772 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3773 UIO_USERSPACE, flags);
3774 if (error == 0)
3775 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3776 nlookup_done_at(&nd, fp);
3777 return (error);
3781 kern_truncate(struct nlookupdata *nd, off_t length)
3783 struct vnode *vp;
3784 struct vattr vattr;
3785 int error;
3786 uid_t uid = 0;
3787 gid_t gid = 0;
3788 uint64_t old_size = 0;
3790 if (length < 0)
3791 return(EINVAL);
3792 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3793 if ((error = nlookup(nd)) != 0)
3794 return (error);
3795 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3796 return (error);
3797 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3798 return (error);
3799 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3800 if (error) {
3801 vrele(vp);
3802 return (error);
3804 if (vp->v_type == VDIR) {
3805 error = EISDIR;
3806 goto done;
3808 if (vfs_quota_enabled) {
3809 error = VOP_GETATTR(vp, &vattr);
3810 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3811 uid = vattr.va_uid;
3812 gid = vattr.va_gid;
3813 old_size = vattr.va_size;
3816 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3817 VATTR_NULL(&vattr);
3818 vattr.va_size = length;
3819 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3820 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3822 done:
3823 vput(vp);
3824 return (error);
3828 * truncate(char *path, int pad, off_t length)
3830 * Truncate a file given its path name.
3833 sys_truncate(struct truncate_args *uap)
3835 struct nlookupdata nd;
3836 int error;
3838 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3839 if (error == 0)
3840 error = kern_truncate(&nd, uap->length);
3841 nlookup_done(&nd);
3842 return error;
3846 kern_ftruncate(int fd, off_t length)
3848 struct thread *td = curthread;
3849 struct proc *p = td->td_proc;
3850 struct vattr vattr;
3851 struct vnode *vp;
3852 struct file *fp;
3853 int error;
3854 uid_t uid = 0;
3855 gid_t gid = 0;
3856 uint64_t old_size = 0;
3857 struct mount *mp;
3859 if (length < 0)
3860 return(EINVAL);
3861 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3862 return (error);
3863 if (fp->f_nchandle.ncp) {
3864 error = ncp_writechk(&fp->f_nchandle);
3865 if (error)
3866 goto done;
3868 if ((fp->f_flag & FWRITE) == 0) {
3869 error = EINVAL;
3870 goto done;
3872 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3873 error = EINVAL;
3874 goto done;
3876 vp = (struct vnode *)fp->f_data;
3877 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3878 if (vp->v_type == VDIR) {
3879 error = EISDIR;
3880 vn_unlock(vp);
3881 goto done;
3884 if (vfs_quota_enabled) {
3885 error = VOP_GETATTR(vp, &vattr);
3886 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3887 uid = vattr.va_uid;
3888 gid = vattr.va_gid;
3889 old_size = vattr.va_size;
3892 if ((error = vn_writechk(vp, NULL)) == 0) {
3893 VATTR_NULL(&vattr);
3894 vattr.va_size = length;
3895 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3896 mp = vq_vptomp(vp);
3897 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3899 vn_unlock(vp);
3900 done:
3901 fdrop(fp);
3902 return (error);
3906 * ftruncate_args(int fd, int pad, off_t length)
3908 * Truncate a file given a file descriptor.
3911 sys_ftruncate(struct ftruncate_args *uap)
3913 int error;
3915 error = kern_ftruncate(uap->fd, uap->length);
3917 return (error);
3921 * fsync(int fd)
3923 * Sync an open file.
3926 sys_fsync(struct fsync_args *uap)
3928 struct thread *td = curthread;
3929 struct proc *p = td->td_proc;
3930 struct vnode *vp;
3931 struct file *fp;
3932 vm_object_t obj;
3933 int error;
3935 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3936 return (error);
3937 vp = (struct vnode *)fp->f_data;
3938 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3939 if ((obj = vp->v_object) != NULL) {
3940 if (vp->v_mount == NULL ||
3941 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3942 vm_object_page_clean(obj, 0, 0, 0);
3945 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3946 if (error == 0 && vp->v_mount)
3947 error = buf_fsync(vp);
3948 vn_unlock(vp);
3949 fdrop(fp);
3951 return (error);
3955 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3957 struct nchandle fnchd;
3958 struct nchandle tnchd;
3959 struct namecache *ncp;
3960 struct vnode *fdvp;
3961 struct vnode *tdvp;
3962 struct mount *mp;
3963 int error;
3964 u_int fncp_gen;
3965 u_int tncp_gen;
3967 bwillinode(1);
3968 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3969 if ((error = nlookup(fromnd)) != 0)
3970 return (error);
3971 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3972 return (ENOENT);
3973 fnchd.mount = fromnd->nl_nch.mount;
3974 cache_hold(&fnchd);
3977 * unlock the source nch so we can lookup the target nch without
3978 * deadlocking. The target may or may not exist so we do not check
3979 * for a target vp like kern_mkdir() and other creation functions do.
3981 * The source and target directories are ref'd and rechecked after
3982 * everything is relocked to determine if the source or target file
3983 * has been renamed.
3985 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3986 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3988 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3990 cache_unlock(&fromnd->nl_nch);
3992 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3993 if ((error = nlookup(tond)) != 0) {
3994 cache_drop(&fnchd);
3995 return (error);
3997 tncp_gen = tond->nl_nch.ncp->nc_generation;
3999 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4000 cache_drop(&fnchd);
4001 return (ENOENT);
4003 tnchd.mount = tond->nl_nch.mount;
4004 cache_hold(&tnchd);
4007 * If the source and target are the same there is nothing to do
4009 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4010 cache_drop(&fnchd);
4011 cache_drop(&tnchd);
4012 return (0);
4016 * Mount points cannot be renamed or overwritten
4018 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4019 NCF_ISMOUNTPT
4021 cache_drop(&fnchd);
4022 cache_drop(&tnchd);
4023 return (EINVAL);
4027 * Relock the source ncp. cache_relock() will deal with any
4028 * deadlocks against the already-locked tond and will also
4029 * make sure both are resolved.
4031 * NOTE AFTER RELOCKING: The source or target ncp may have become
4032 * invalid while they were unlocked, nc_vp and nc_mount could
4033 * be NULL.
4035 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4036 &tond->nl_nch, tond->nl_cred);
4037 fromnd->nl_flags |= NLC_NCPISLOCKED;
4040 * If the namecache generation changed for either fromnd or tond,
4041 * we must retry.
4043 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4044 tond->nl_nch.ncp->nc_generation != tncp_gen) {
4045 kprintf("kern_rename: retry due to gen on: "
4046 "\"%s\" -> \"%s\"\n",
4047 fromnd->nl_nch.ncp->nc_name,
4048 tond->nl_nch.ncp->nc_name);
4049 cache_drop(&fnchd);
4050 cache_drop(&tnchd);
4051 return (EAGAIN);
4055 * If either fromnd or tond are marked destroyed a ripout occured
4056 * out from under us and we must retry.
4058 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4059 fromnd->nl_nch.ncp->nc_vp == NULL ||
4060 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4061 kprintf("kern_rename: retry due to ripout on: "
4062 "\"%s\" -> \"%s\"\n",
4063 fromnd->nl_nch.ncp->nc_name,
4064 tond->nl_nch.ncp->nc_name);
4065 cache_drop(&fnchd);
4066 cache_drop(&tnchd);
4067 return (EAGAIN);
4071 * Make sure the parent directories linkages are the same.
4072 * XXX shouldn't be needed any more w/ generation check above.
4074 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4075 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4076 cache_drop(&fnchd);
4077 cache_drop(&tnchd);
4078 return (ENOENT);
4082 * Both the source and target must be within the same filesystem and
4083 * in the same filesystem as their parent directories within the
4084 * namecache topology.
4086 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4088 mp = fnchd.mount;
4089 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4090 mp != tond->nl_nch.mount) {
4091 cache_drop(&fnchd);
4092 cache_drop(&tnchd);
4093 return (EXDEV);
4097 * Make sure the mount point is writable
4099 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4100 cache_drop(&fnchd);
4101 cache_drop(&tnchd);
4102 return (error);
4106 * If the target exists and either the source or target is a directory,
4107 * then both must be directories.
4109 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4110 * have become NULL.
4112 if (tond->nl_nch.ncp->nc_vp) {
4113 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4114 error = ENOENT;
4115 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4116 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4117 error = ENOTDIR;
4118 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4119 error = EISDIR;
4124 * You cannot rename a source into itself or a subdirectory of itself.
4125 * We check this by travsersing the target directory upwards looking
4126 * for a match against the source.
4128 * XXX MPSAFE
4130 if (error == 0) {
4131 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4132 if (fromnd->nl_nch.ncp == ncp) {
4133 error = EINVAL;
4134 break;
4139 cache_drop(&fnchd);
4140 cache_drop(&tnchd);
4143 * Even though the namespaces are different, they may still represent
4144 * hardlinks to the same file. The filesystem might have a hard time
4145 * with this so we issue a NREMOVE of the source instead of a NRENAME
4146 * when we detect the situation.
4148 if (error == 0) {
4149 fdvp = fromnd->nl_dvp;
4150 tdvp = tond->nl_dvp;
4151 if (fdvp == NULL || tdvp == NULL) {
4152 error = EPERM;
4153 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4154 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4155 fromnd->nl_cred);
4156 } else {
4157 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4158 fdvp, tdvp, tond->nl_cred);
4161 return (error);
4165 * rename_args(char *from, char *to)
4167 * Rename files. Source and destination must either both be directories,
4168 * or both not be directories. If target is a directory, it must be empty.
4171 sys_rename(struct rename_args *uap)
4173 struct nlookupdata fromnd, tond;
4174 int error;
4176 do {
4177 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4178 if (error == 0) {
4179 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4180 if (error == 0)
4181 error = kern_rename(&fromnd, &tond);
4182 nlookup_done(&tond);
4184 nlookup_done(&fromnd);
4185 } while (error == EAGAIN);
4186 return (error);
4190 * renameat_args(int oldfd, char *old, int newfd, char *new)
4192 * Rename files using paths relative to the directories associated with
4193 * oldfd and newfd. Source and destination must either both be directories,
4194 * or both not be directories. If target is a directory, it must be empty.
4197 sys_renameat(struct renameat_args *uap)
4199 struct nlookupdata oldnd, newnd;
4200 struct file *oldfp, *newfp;
4201 int error;
4203 do {
4204 error = nlookup_init_at(&oldnd, &oldfp,
4205 uap->oldfd, uap->old,
4206 UIO_USERSPACE, 0);
4207 if (error == 0) {
4208 error = nlookup_init_at(&newnd, &newfp,
4209 uap->newfd, uap->new,
4210 UIO_USERSPACE, 0);
4211 if (error == 0)
4212 error = kern_rename(&oldnd, &newnd);
4213 nlookup_done_at(&newnd, newfp);
4215 nlookup_done_at(&oldnd, oldfp);
4216 } while (error == EAGAIN);
4217 return (error);
4221 kern_mkdir(struct nlookupdata *nd, int mode)
4223 struct thread *td = curthread;
4224 struct proc *p = td->td_proc;
4225 struct vnode *vp;
4226 struct vattr vattr;
4227 int error;
4229 bwillinode(1);
4230 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4231 if ((error = nlookup(nd)) != 0)
4232 return (error);
4234 if (nd->nl_nch.ncp->nc_vp)
4235 return (EEXIST);
4236 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4237 return (error);
4238 VATTR_NULL(&vattr);
4239 vattr.va_type = VDIR;
4240 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4242 vp = NULL;
4243 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4244 if (error == 0)
4245 vput(vp);
4246 return (error);
4250 * mkdir_args(char *path, int mode)
4252 * Make a directory file.
4255 sys_mkdir(struct mkdir_args *uap)
4257 struct nlookupdata nd;
4258 int error;
4260 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4261 if (error == 0)
4262 error = kern_mkdir(&nd, uap->mode);
4263 nlookup_done(&nd);
4264 return (error);
4268 * mkdirat_args(int fd, char *path, mode_t mode)
4270 * Make a directory file. The path is relative to the directory associated
4271 * with fd.
4274 sys_mkdirat(struct mkdirat_args *uap)
4276 struct nlookupdata nd;
4277 struct file *fp;
4278 int error;
4280 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4281 if (error == 0)
4282 error = kern_mkdir(&nd, uap->mode);
4283 nlookup_done_at(&nd, fp);
4284 return (error);
4288 kern_rmdir(struct nlookupdata *nd)
4290 int error;
4292 bwillinode(1);
4293 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4294 if ((error = nlookup(nd)) != 0)
4295 return (error);
4298 * Do not allow directories representing mount points to be
4299 * deleted, even if empty. Check write perms on mount point
4300 * in case the vnode is aliased (aka nullfs).
4302 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4303 return (EBUSY);
4304 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4305 return (error);
4306 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4307 return (error);
4311 * rmdir_args(char *path)
4313 * Remove a directory file.
4316 sys_rmdir(struct rmdir_args *uap)
4318 struct nlookupdata nd;
4319 int error;
4321 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4322 if (error == 0)
4323 error = kern_rmdir(&nd);
4324 nlookup_done(&nd);
4325 return (error);
4329 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4330 enum uio_seg direction)
4332 struct thread *td = curthread;
4333 struct proc *p = td->td_proc;
4334 struct vnode *vp;
4335 struct file *fp;
4336 struct uio auio;
4337 struct iovec aiov;
4338 off_t loff;
4339 int error, eofflag;
4341 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4342 return (error);
4343 if ((fp->f_flag & FREAD) == 0) {
4344 error = EBADF;
4345 goto done;
4347 vp = (struct vnode *)fp->f_data;
4348 if (vp->v_type != VDIR) {
4349 error = EINVAL;
4350 goto done;
4352 aiov.iov_base = buf;
4353 aiov.iov_len = count;
4354 auio.uio_iov = &aiov;
4355 auio.uio_iovcnt = 1;
4356 auio.uio_rw = UIO_READ;
4357 auio.uio_segflg = direction;
4358 auio.uio_td = td;
4359 auio.uio_resid = count;
4360 loff = auio.uio_offset = fp->f_offset;
4361 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4362 fp->f_offset = auio.uio_offset;
4363 if (error)
4364 goto done;
4367 * WARNING! *basep may not be wide enough to accomodate the
4368 * seek offset. XXX should we hack this to return the upper 32 bits
4369 * for offsets greater then 4G?
4371 if (basep) {
4372 *basep = (long)loff;
4374 *res = count - auio.uio_resid;
4375 done:
4376 fdrop(fp);
4377 return (error);
4381 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4383 * Read a block of directory entries in a file system independent format.
4386 sys_getdirentries(struct getdirentries_args *uap)
4388 long base;
4389 int error;
4391 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4392 &uap->sysmsg_result, UIO_USERSPACE);
4394 if (error == 0 && uap->basep)
4395 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4396 return (error);
4400 * getdents_args(int fd, char *buf, size_t count)
4403 sys_getdents(struct getdents_args *uap)
4405 int error;
4407 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4408 &uap->sysmsg_result, UIO_USERSPACE);
4410 return (error);
4414 * Set the mode mask for creation of filesystem nodes.
4416 * umask(int newmask)
4419 sys_umask(struct umask_args *uap)
4421 struct thread *td = curthread;
4422 struct proc *p = td->td_proc;
4423 struct filedesc *fdp;
4425 fdp = p->p_fd;
4426 uap->sysmsg_result = fdp->fd_cmask;
4427 fdp->fd_cmask = uap->newmask & ALLPERMS;
4428 return (0);
4432 * revoke(char *path)
4434 * Void all references to file by ripping underlying filesystem
4435 * away from vnode.
4438 sys_revoke(struct revoke_args *uap)
4440 struct nlookupdata nd;
4441 struct vattr vattr;
4442 struct vnode *vp;
4443 struct ucred *cred;
4444 int error;
4446 vp = NULL;
4447 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4448 if (error == 0)
4449 error = nlookup(&nd);
4450 if (error == 0)
4451 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4452 cred = crhold(nd.nl_cred);
4453 nlookup_done(&nd);
4454 if (error == 0) {
4455 if (error == 0)
4456 error = VOP_GETATTR(vp, &vattr);
4457 if (error == 0 && cred->cr_uid != vattr.va_uid)
4458 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4459 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4460 if (vcount(vp) > 0)
4461 error = vrevoke(vp, cred);
4462 } else if (error == 0) {
4463 error = vrevoke(vp, cred);
4465 vrele(vp);
4467 if (cred)
4468 crfree(cred);
4469 return (error);
4473 * getfh_args(char *fname, fhandle_t *fhp)
4475 * Get (NFS) file handle
4477 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4478 * mount. This allows nullfs mounts to be explicitly exported.
4480 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4482 * nullfs mounts of subdirectories are not safe. That is, it will
4483 * work, but you do not really have protection against access to
4484 * the related parent directories.
4487 sys_getfh(struct getfh_args *uap)
4489 struct thread *td = curthread;
4490 struct nlookupdata nd;
4491 fhandle_t fh;
4492 struct vnode *vp;
4493 struct mount *mp;
4494 int error;
4497 * Must be super user
4499 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4500 return (error);
4502 vp = NULL;
4503 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4504 if (error == 0)
4505 error = nlookup(&nd);
4506 if (error == 0)
4507 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4508 mp = nd.nl_nch.mount;
4509 nlookup_done(&nd);
4510 if (error == 0) {
4511 bzero(&fh, sizeof(fh));
4512 fh.fh_fsid = mp->mnt_stat.f_fsid;
4513 error = VFS_VPTOFH(vp, &fh.fh_fid);
4514 vput(vp);
4515 if (error == 0)
4516 error = copyout(&fh, uap->fhp, sizeof(fh));
4518 return (error);
4522 * fhopen_args(const struct fhandle *u_fhp, int flags)
4524 * syscall for the rpc.lockd to use to translate a NFS file handle into
4525 * an open descriptor.
4527 * warning: do not remove the priv_check() call or this becomes one giant
4528 * security hole.
4531 sys_fhopen(struct fhopen_args *uap)
4533 struct thread *td = curthread;
4534 struct filedesc *fdp = td->td_proc->p_fd;
4535 struct mount *mp;
4536 struct vnode *vp;
4537 struct fhandle fhp;
4538 struct vattr vat;
4539 struct vattr *vap = &vat;
4540 struct flock lf;
4541 int fmode, mode, error = 0, type;
4542 struct file *nfp;
4543 struct file *fp;
4544 int indx;
4547 * Must be super user
4549 error = priv_check(td, PRIV_ROOT);
4550 if (error)
4551 return (error);
4553 fmode = FFLAGS(uap->flags);
4556 * Why not allow a non-read/write open for our lockd?
4558 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4559 return (EINVAL);
4560 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4561 if (error)
4562 return(error);
4565 * Find the mount point
4567 mp = vfs_getvfs(&fhp.fh_fsid);
4568 if (mp == NULL) {
4569 error = ESTALE;
4570 goto done2;
4572 /* now give me my vnode, it gets returned to me locked */
4573 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4574 if (error)
4575 goto done;
4577 * from now on we have to make sure not
4578 * to forget about the vnode
4579 * any error that causes an abort must vput(vp)
4580 * just set error = err and 'goto bad;'.
4584 * from vn_open
4586 if (vp->v_type == VLNK) {
4587 error = EMLINK;
4588 goto bad;
4590 if (vp->v_type == VSOCK) {
4591 error = EOPNOTSUPP;
4592 goto bad;
4594 mode = 0;
4595 if (fmode & (FWRITE | O_TRUNC)) {
4596 if (vp->v_type == VDIR) {
4597 error = EISDIR;
4598 goto bad;
4600 error = vn_writechk(vp, NULL);
4601 if (error)
4602 goto bad;
4603 mode |= VWRITE;
4605 if (fmode & FREAD)
4606 mode |= VREAD;
4607 if (mode) {
4608 error = VOP_ACCESS(vp, mode, td->td_ucred);
4609 if (error)
4610 goto bad;
4612 if (fmode & O_TRUNC) {
4613 vn_unlock(vp); /* XXX */
4614 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4615 VATTR_NULL(vap);
4616 vap->va_size = 0;
4617 error = VOP_SETATTR(vp, vap, td->td_ucred);
4618 if (error)
4619 goto bad;
4623 * VOP_OPEN needs the file pointer so it can potentially override
4624 * it.
4626 * WARNING! no f_nchandle will be associated when fhopen()ing a
4627 * directory. XXX
4629 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4630 goto bad;
4631 fp = nfp;
4633 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4634 if (error) {
4636 * setting f_ops this way prevents VOP_CLOSE from being
4637 * called or fdrop() releasing the vp from v_data. Since
4638 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4640 fp->f_ops = &badfileops;
4641 fp->f_data = NULL;
4642 goto bad_drop;
4646 * The fp is given its own reference, we still have our ref and lock.
4648 * Assert that all regular files must be created with a VM object.
4650 if (vp->v_type == VREG && vp->v_object == NULL) {
4651 kprintf("fhopen: regular file did not "
4652 "have VM object: %p\n",
4653 vp);
4654 goto bad_drop;
4658 * The open was successful. Handle any locking requirements.
4660 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4661 lf.l_whence = SEEK_SET;
4662 lf.l_start = 0;
4663 lf.l_len = 0;
4664 if (fmode & O_EXLOCK)
4665 lf.l_type = F_WRLCK;
4666 else
4667 lf.l_type = F_RDLCK;
4668 if (fmode & FNONBLOCK)
4669 type = 0;
4670 else
4671 type = F_WAIT;
4672 vn_unlock(vp);
4673 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4674 &lf, type)) != 0) {
4676 * release our private reference.
4678 fsetfd(fdp, NULL, indx);
4679 fdrop(fp);
4680 vrele(vp);
4681 goto done;
4683 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4684 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4688 * Clean up. Associate the file pointer with the previously
4689 * reserved descriptor and return it.
4691 vput(vp);
4692 if (uap->flags & O_CLOEXEC)
4693 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4694 fsetfd(fdp, fp, indx);
4695 fdrop(fp);
4696 uap->sysmsg_result = indx;
4697 mount_drop(mp);
4699 return (error);
4701 bad_drop:
4702 fsetfd(fdp, NULL, indx);
4703 fdrop(fp);
4704 bad:
4705 vput(vp);
4706 done:
4707 mount_drop(mp);
4708 done2:
4709 return (error);
4713 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4716 sys_fhstat(struct fhstat_args *uap)
4718 struct thread *td = curthread;
4719 struct stat sb;
4720 fhandle_t fh;
4721 struct mount *mp;
4722 struct vnode *vp;
4723 int error;
4726 * Must be super user
4728 error = priv_check(td, PRIV_ROOT);
4729 if (error)
4730 return (error);
4732 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4733 if (error)
4734 return (error);
4736 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4737 error = ESTALE;
4738 if (error == 0) {
4739 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4740 error = vn_stat(vp, &sb, td->td_ucred);
4741 vput(vp);
4744 if (error == 0)
4745 error = copyout(&sb, uap->sb, sizeof(sb));
4746 if (mp)
4747 mount_drop(mp);
4749 return (error);
4753 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4756 sys_fhstatfs(struct fhstatfs_args *uap)
4758 struct thread *td = curthread;
4759 struct proc *p = td->td_proc;
4760 struct statfs *sp;
4761 struct mount *mp;
4762 struct vnode *vp;
4763 struct statfs sb;
4764 char *fullpath, *freepath;
4765 fhandle_t fh;
4766 int error;
4769 * Must be super user
4771 if ((error = priv_check(td, PRIV_ROOT)))
4772 return (error);
4774 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4775 return (error);
4777 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4778 error = ESTALE;
4779 goto done;
4781 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4782 error = ESTALE;
4783 goto done;
4786 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4787 goto done;
4788 mp = vp->v_mount;
4789 sp = &mp->mnt_stat;
4790 vput(vp);
4791 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4792 goto done;
4794 error = mount_path(p, mp, &fullpath, &freepath);
4795 if (error)
4796 goto done;
4797 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4798 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4799 kfree(freepath, M_TEMP);
4801 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4802 if (priv_check(td, PRIV_ROOT)) {
4803 bcopy(sp, &sb, sizeof(sb));
4804 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4805 sp = &sb;
4807 error = copyout(sp, uap->buf, sizeof(*sp));
4808 done:
4809 if (mp)
4810 mount_drop(mp);
4812 return (error);
4816 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4819 sys_fhstatvfs(struct fhstatvfs_args *uap)
4821 struct thread *td = curthread;
4822 struct proc *p = td->td_proc;
4823 struct statvfs *sp;
4824 struct mount *mp;
4825 struct vnode *vp;
4826 fhandle_t fh;
4827 int error;
4830 * Must be super user
4832 if ((error = priv_check(td, PRIV_ROOT)))
4833 return (error);
4835 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4836 return (error);
4838 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4839 error = ESTALE;
4840 goto done;
4842 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4843 error = ESTALE;
4844 goto done;
4847 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4848 goto done;
4849 mp = vp->v_mount;
4850 sp = &mp->mnt_vstat;
4851 vput(vp);
4852 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4853 goto done;
4855 sp->f_flag = 0;
4856 if (mp->mnt_flag & MNT_RDONLY)
4857 sp->f_flag |= ST_RDONLY;
4858 if (mp->mnt_flag & MNT_NOSUID)
4859 sp->f_flag |= ST_NOSUID;
4860 error = copyout(sp, uap->buf, sizeof(*sp));
4861 done:
4862 if (mp)
4863 mount_drop(mp);
4864 return (error);
4869 * Syscall to push extended attribute configuration information into the
4870 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4871 * a command (int cmd), and attribute name and misc data. For now, the
4872 * attribute name is left in userspace for consumption by the VFS_op.
4873 * It will probably be changed to be copied into sysspace by the
4874 * syscall in the future, once issues with various consumers of the
4875 * attribute code have raised their hands.
4877 * Currently this is used only by UFS Extended Attributes.
4880 sys_extattrctl(struct extattrctl_args *uap)
4882 struct nlookupdata nd;
4883 struct vnode *vp;
4884 char attrname[EXTATTR_MAXNAMELEN];
4885 int error;
4886 size_t size;
4888 attrname[0] = 0;
4889 vp = NULL;
4890 error = 0;
4892 if (error == 0 && uap->filename) {
4893 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4894 NLC_FOLLOW);
4895 if (error == 0)
4896 error = nlookup(&nd);
4897 if (error == 0)
4898 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4899 nlookup_done(&nd);
4902 if (error == 0 && uap->attrname) {
4903 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4904 &size);
4907 if (error == 0) {
4908 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4909 if (error == 0)
4910 error = nlookup(&nd);
4911 if (error == 0)
4912 error = ncp_writechk(&nd.nl_nch);
4913 if (error == 0) {
4914 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4915 uap->attrnamespace,
4916 uap->attrname, nd.nl_cred);
4918 nlookup_done(&nd);
4921 return (error);
4925 * Syscall to get a named extended attribute on a file or directory.
4928 sys_extattr_set_file(struct extattr_set_file_args *uap)
4930 char attrname[EXTATTR_MAXNAMELEN];
4931 struct nlookupdata nd;
4932 struct vnode *vp;
4933 struct uio auio;
4934 struct iovec aiov;
4935 int error;
4937 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4938 if (error)
4939 return (error);
4941 vp = NULL;
4943 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4944 if (error == 0)
4945 error = nlookup(&nd);
4946 if (error == 0)
4947 error = ncp_writechk(&nd.nl_nch);
4948 if (error == 0)
4949 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4950 if (error) {
4951 nlookup_done(&nd);
4952 return (error);
4955 bzero(&auio, sizeof(auio));
4956 aiov.iov_base = uap->data;
4957 aiov.iov_len = uap->nbytes;
4958 auio.uio_iov = &aiov;
4959 auio.uio_iovcnt = 1;
4960 auio.uio_offset = 0;
4961 auio.uio_resid = uap->nbytes;
4962 auio.uio_rw = UIO_WRITE;
4963 auio.uio_td = curthread;
4965 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4966 &auio, nd.nl_cred);
4968 vput(vp);
4969 nlookup_done(&nd);
4970 return (error);
4974 * Syscall to get a named extended attribute on a file or directory.
4977 sys_extattr_get_file(struct extattr_get_file_args *uap)
4979 char attrname[EXTATTR_MAXNAMELEN];
4980 struct nlookupdata nd;
4981 struct uio auio;
4982 struct iovec aiov;
4983 struct vnode *vp;
4984 int error;
4986 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4987 if (error)
4988 return (error);
4990 vp = NULL;
4992 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4993 if (error == 0)
4994 error = nlookup(&nd);
4995 if (error == 0)
4996 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4997 if (error) {
4998 nlookup_done(&nd);
4999 return (error);
5002 bzero(&auio, sizeof(auio));
5003 aiov.iov_base = uap->data;
5004 aiov.iov_len = uap->nbytes;
5005 auio.uio_iov = &aiov;
5006 auio.uio_iovcnt = 1;
5007 auio.uio_offset = 0;
5008 auio.uio_resid = uap->nbytes;
5009 auio.uio_rw = UIO_READ;
5010 auio.uio_td = curthread;
5012 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5013 &auio, nd.nl_cred);
5014 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5016 vput(vp);
5017 nlookup_done(&nd);
5018 return(error);
5022 * Syscall to delete a named extended attribute from a file or directory.
5023 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5026 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5028 char attrname[EXTATTR_MAXNAMELEN];
5029 struct nlookupdata nd;
5030 struct vnode *vp;
5031 int error;
5033 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5034 if (error)
5035 return(error);
5037 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5038 if (error == 0)
5039 error = nlookup(&nd);
5040 if (error == 0)
5041 error = ncp_writechk(&nd.nl_nch);
5042 if (error == 0) {
5043 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5044 if (error == 0) {
5045 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5046 attrname, NULL, nd.nl_cred);
5047 vput(vp);
5050 nlookup_done(&nd);
5051 return(error);
5055 * Determine if the mount is visible to the process.
5057 static int
5058 chroot_visible_mnt(struct mount *mp, struct proc *p)
5060 struct nchandle nch;
5063 * Traverse from the mount point upwards. If we hit the process
5064 * root then the mount point is visible to the process.
5066 nch = mp->mnt_ncmountpt;
5067 while (nch.ncp) {
5068 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5069 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5070 return(1);
5072 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5073 nch = nch.mount->mnt_ncmounton;
5074 } else {
5075 nch.ncp = nch.ncp->nc_parent;
5080 * If the mount point is not visible to the process, but the
5081 * process root is in a subdirectory of the mount, return
5082 * TRUE anyway.
5084 if (p->p_fd->fd_nrdir.mount == mp)
5085 return(1);
5087 return(0);