mount - Finish refactoring mount -v and MOUNTCTL_MOUNTFLAGS
[dragonfly.git] / sys / kern / vfs_syscalls.c
blob5b6e0ef807a62acf03cd2227884319d09c5228d0
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.135 2008/11/11 00:55:49 pavalos Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/conf.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
55 #include <sys/file.h>
56 #include <sys/linker.h>
57 #include <sys/stat.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/proc.h>
61 #include <sys/priv.h>
62 #include <sys/jail.h>
63 #include <sys/namei.h>
64 #include <sys/nlookup.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
67 #include <sys/spinlock.h>
68 #include <sys/kern_syscall.h>
69 #include <sys/objcache.h>
70 #include <sys/sysctl.h>
72 #include <sys/buf2.h>
73 #include <sys/file2.h>
74 #include <sys/spinlock2.h>
76 #include <vm/vm.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount *mp, const char *ctl, ...);
86 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
87 static int checkvp_chdir (struct vnode *vn, struct thread *td);
88 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
89 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
90 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
91 static int getutimes (const struct timeval *, struct timespec *);
92 static int setfown (struct vnode *, uid_t, gid_t);
93 static int setfmode (struct vnode *, int);
94 static int setfflags (struct vnode *, int);
95 static int setutimes (struct vnode *, struct vattr *,
96 const struct timespec *, int);
97 static int usermount = 0; /* if 1, non-root can mount fs. */
99 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
101 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
104 * Virtual File System System Calls
108 * Mount a file system.
111 * mount_args(char *type, char *path, int flags, caddr_t data)
113 /* ARGSUSED */
115 sys_mount(struct mount_args *uap)
117 struct thread *td = curthread;
118 struct proc *p = td->td_proc;
119 struct vnode *vp;
120 struct nchandle nch;
121 struct mount *mp;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
124 int hasmount;
125 struct vattr va;
126 struct nlookupdata nd;
127 char fstypename[MFSNAMELEN];
128 struct ucred *cred = p->p_ucred;
130 KKASSERT(p);
131 if (jailed(cred))
132 return (EPERM);
133 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
134 return (error);
136 * Do not allow NFS export by non-root users.
138 if (uap->flags & MNT_EXPORTED) {
139 error = priv_check(td, PRIV_ROOT);
140 if (error)
141 return (error);
144 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
146 if (priv_check(td, PRIV_ROOT))
147 uap->flags |= MNT_NOSUID | MNT_NODEV;
150 * Lookup the requested path and extract the nch and vnode.
152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
153 if (error == 0) {
154 if ((error = nlookup(&nd)) == 0) {
155 if (nd.nl_nch.ncp->nc_vp == NULL)
156 error = ENOENT;
159 if (error) {
160 nlookup_done(&nd);
161 return (error);
165 * Extract the locked+refd ncp and cleanup the nd structure
167 nch = nd.nl_nch;
168 cache_zero(&nd.nl_nch);
169 nlookup_done(&nd);
171 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
172 hasmount = 1;
173 else
174 hasmount = 0;
178 * now we have the locked ref'd nch and unreferenced vnode.
180 vp = nch.ncp->nc_vp;
181 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
182 cache_put(&nch);
183 return (error);
185 cache_unlock(&nch);
188 * Now we have an unlocked ref'd nch and a locked ref'd vp
190 if (uap->flags & MNT_UPDATE) {
191 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
192 cache_drop(&nch);
193 vput(vp);
194 return (EINVAL);
196 mp = vp->v_mount;
197 flag = mp->mnt_flag;
198 flag2 = mp->mnt_kern_flag;
200 * We only allow the filesystem to be reloaded if it
201 * is currently mounted read-only.
203 if ((uap->flags & MNT_RELOAD) &&
204 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
205 cache_drop(&nch);
206 vput(vp);
207 return (EOPNOTSUPP); /* Needs translation */
210 * Only root, or the user that did the original mount is
211 * permitted to update it.
213 if (mp->mnt_stat.f_owner != cred->cr_uid &&
214 (error = priv_check(td, PRIV_ROOT))) {
215 cache_drop(&nch);
216 vput(vp);
217 return (error);
219 if (vfs_busy(mp, LK_NOWAIT)) {
220 cache_drop(&nch);
221 vput(vp);
222 return (EBUSY);
224 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
225 cache_drop(&nch);
226 vfs_unbusy(mp);
227 vput(vp);
228 return (EBUSY);
230 vp->v_flag |= VMOUNT;
231 mp->mnt_flag |=
232 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
233 vn_unlock(vp);
234 goto update;
237 * If the user is not root, ensure that they own the directory
238 * onto which we are attempting to mount.
240 if ((error = VOP_GETATTR(vp, &va)) ||
241 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
242 cache_drop(&nch);
243 vput(vp);
244 return (error);
246 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
247 cache_drop(&nch);
248 vput(vp);
249 return (error);
251 if (vp->v_type != VDIR) {
252 cache_drop(&nch);
253 vput(vp);
254 return (ENOTDIR);
256 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
257 cache_drop(&nch);
258 vput(vp);
259 return (EPERM);
261 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
262 cache_drop(&nch);
263 vput(vp);
264 return (error);
266 vfsp = vfsconf_find_by_name(fstypename);
267 if (vfsp == NULL) {
268 linker_file_t lf;
270 /* Only load modules for root (very important!) */
271 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
272 cache_drop(&nch);
273 vput(vp);
274 return error;
276 error = linker_load_file(fstypename, &lf, 0);
277 if (error || lf == NULL) {
278 cache_drop(&nch);
279 vput(vp);
280 if (lf == NULL)
281 error = ENODEV;
282 return error;
284 lf->userrefs++;
285 /* lookup again, see if the VFS was loaded */
286 vfsp = vfsconf_find_by_name(fstypename);
287 if (vfsp == NULL) {
288 lf->userrefs--;
289 linker_file_unload(lf);
290 cache_drop(&nch);
291 vput(vp);
292 return (ENODEV);
295 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
296 cache_drop(&nch);
297 vput(vp);
298 return (EBUSY);
300 vp->v_flag |= VMOUNT;
303 * Allocate and initialize the filesystem.
305 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
306 TAILQ_INIT(&mp->mnt_nvnodelist);
307 TAILQ_INIT(&mp->mnt_reservedvnlist);
308 TAILQ_INIT(&mp->mnt_jlist);
309 mp->mnt_nvnodelistsize = 0;
310 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
311 vfs_busy(mp, LK_NOWAIT);
312 mp->mnt_op = vfsp->vfc_vfsops;
313 mp->mnt_vfc = vfsp;
314 vfsp->vfc_refcount++;
315 mp->mnt_stat.f_type = vfsp->vfc_typenum;
316 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
317 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
318 mp->mnt_stat.f_owner = cred->cr_uid;
319 mp->mnt_iosize_max = DFLTPHYS;
320 vn_unlock(vp);
321 update:
323 * Set the mount level flags.
325 if (uap->flags & MNT_RDONLY)
326 mp->mnt_flag |= MNT_RDONLY;
327 else if (mp->mnt_flag & MNT_RDONLY)
328 mp->mnt_kern_flag |= MNTK_WANTRDWR;
329 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
330 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
331 MNT_NOSYMFOLLOW | MNT_IGNORE |
332 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
333 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
334 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
335 MNT_NOSYMFOLLOW | MNT_IGNORE |
336 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
338 * Mount the filesystem.
339 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
340 * get.
342 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
343 if (mp->mnt_flag & MNT_UPDATE) {
344 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
345 mp->mnt_flag &= ~MNT_RDONLY;
346 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
347 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
348 if (error) {
349 mp->mnt_flag = flag;
350 mp->mnt_kern_flag = flag2;
352 vfs_unbusy(mp);
353 vp->v_flag &= ~VMOUNT;
354 vrele(vp);
355 cache_drop(&nch);
356 return (error);
358 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
360 * Put the new filesystem on the mount list after root. The mount
361 * point gets its own mnt_ncmountpt (unless the VFS already set one
362 * up) which represents the root of the mount. The lookup code
363 * detects the mount point going forward and checks the root of
364 * the mount going backwards.
366 * It is not necessary to invalidate or purge the vnode underneath
367 * because elements under the mount will be given their own glue
368 * namecache record.
370 if (!error) {
371 if (mp->mnt_ncmountpt.ncp == NULL) {
373 * allocate, then unlock, but leave the ref intact
375 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
376 cache_unlock(&mp->mnt_ncmountpt);
378 mp->mnt_ncmounton = nch; /* inherits ref */
379 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
381 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
382 vp->v_flag &= ~VMOUNT;
383 mountlist_insert(mp, MNTINS_LAST);
384 vn_unlock(vp);
385 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
386 error = vfs_allocate_syncvnode(mp);
387 vfs_unbusy(mp);
388 error = VFS_START(mp, 0);
389 vrele(vp);
390 } else {
391 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
392 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
393 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
394 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
395 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
396 vp->v_flag &= ~VMOUNT;
397 mp->mnt_vfc->vfc_refcount--;
398 vfs_unbusy(mp);
399 kfree(mp, M_MOUNT);
400 cache_drop(&nch);
401 vput(vp);
403 return (error);
407 * Scan all active processes to see if any of them have a current
408 * or root directory onto which the new filesystem has just been
409 * mounted. If so, replace them with the new mount point.
411 * The passed ncp is ref'd and locked (from the mount code) and
412 * must be associated with the vnode representing the root of the
413 * mount point.
415 struct checkdirs_info {
416 struct nchandle old_nch;
417 struct nchandle new_nch;
418 struct vnode *old_vp;
419 struct vnode *new_vp;
422 static int checkdirs_callback(struct proc *p, void *data);
424 static void
425 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
427 struct checkdirs_info info;
428 struct vnode *olddp;
429 struct vnode *newdp;
430 struct mount *mp;
433 * If the old mount point's vnode has a usecount of 1, it is not
434 * being held as a descriptor anywhere.
436 olddp = old_nch->ncp->nc_vp;
437 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
438 return;
441 * Force the root vnode of the new mount point to be resolved
442 * so we can update any matching processes.
444 mp = new_nch->mount;
445 if (VFS_ROOT(mp, &newdp))
446 panic("mount: lost mount");
447 cache_setunresolved(new_nch);
448 cache_setvp(new_nch, newdp);
451 * Special handling of the root node
453 if (rootvnode == olddp) {
454 vref(newdp);
455 vfs_cache_setroot(newdp, cache_hold(new_nch));
459 * Pass newdp separately so the callback does not have to access
460 * it via new_nch->ncp->nc_vp.
462 info.old_nch = *old_nch;
463 info.new_nch = *new_nch;
464 info.new_vp = newdp;
465 allproc_scan(checkdirs_callback, &info);
466 vput(newdp);
470 * NOTE: callback is not MP safe because the scanned process's filedesc
471 * structure can be ripped out from under us, amoung other things.
473 static int
474 checkdirs_callback(struct proc *p, void *data)
476 struct checkdirs_info *info = data;
477 struct filedesc *fdp;
478 struct nchandle ncdrop1;
479 struct nchandle ncdrop2;
480 struct vnode *vprele1;
481 struct vnode *vprele2;
483 if ((fdp = p->p_fd) != NULL) {
484 cache_zero(&ncdrop1);
485 cache_zero(&ncdrop2);
486 vprele1 = NULL;
487 vprele2 = NULL;
490 * MPUNSAFE - XXX fdp can be pulled out from under a
491 * foreign process.
493 * A shared filedesc is ok, we don't have to copy it
494 * because we are making this change globally.
496 spin_lock_wr(&fdp->fd_spin);
497 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
498 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
499 vprele1 = fdp->fd_cdir;
500 vref(info->new_vp);
501 fdp->fd_cdir = info->new_vp;
502 ncdrop1 = fdp->fd_ncdir;
503 cache_copy(&info->new_nch, &fdp->fd_ncdir);
505 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
506 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
507 vprele2 = fdp->fd_rdir;
508 vref(info->new_vp);
509 fdp->fd_rdir = info->new_vp;
510 ncdrop2 = fdp->fd_nrdir;
511 cache_copy(&info->new_nch, &fdp->fd_nrdir);
513 spin_unlock_wr(&fdp->fd_spin);
514 if (ncdrop1.ncp)
515 cache_drop(&ncdrop1);
516 if (ncdrop2.ncp)
517 cache_drop(&ncdrop2);
518 if (vprele1)
519 vrele(vprele1);
520 if (vprele2)
521 vrele(vprele2);
523 return(0);
527 * Unmount a file system.
529 * Note: unmount takes a path to the vnode mounted on as argument,
530 * not special file (as before).
533 * umount_args(char *path, int flags)
535 /* ARGSUSED */
537 sys_unmount(struct unmount_args *uap)
539 struct thread *td = curthread;
540 struct proc *p = td->td_proc;
541 struct mount *mp = NULL;
542 int error;
543 struct nlookupdata nd;
545 KKASSERT(p);
546 if (p->p_ucred->cr_prison != NULL)
547 return (EPERM);
548 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
549 return (error);
551 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
552 if (error == 0)
553 error = nlookup(&nd);
554 if (error)
555 goto out;
557 mp = nd.nl_nch.mount;
560 * Only root, or the user that did the original mount is
561 * permitted to unmount this filesystem.
563 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
564 (error = priv_check(td, PRIV_ROOT)))
565 goto out;
568 * Don't allow unmounting the root file system.
570 if (mp->mnt_flag & MNT_ROOTFS) {
571 error = EINVAL;
572 goto out;
576 * Must be the root of the filesystem
578 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
579 error = EINVAL;
580 goto out;
583 out:
584 nlookup_done(&nd);
585 if (error)
586 return (error);
587 return (dounmount(mp, uap->flags));
591 * Do the actual file system unmount.
593 static int
594 dounmount_interlock(struct mount *mp)
596 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
597 return (EBUSY);
598 mp->mnt_kern_flag |= MNTK_UNMOUNT;
599 return(0);
603 dounmount(struct mount *mp, int flags)
605 struct namecache *ncp;
606 struct nchandle nch;
607 struct vnode *vp;
608 int error;
609 int async_flag;
610 int lflags;
611 int freeok = 1;
614 * Exclusive access for unmounting purposes
616 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
617 return (error);
620 * Allow filesystems to detect that a forced unmount is in progress.
622 if (flags & MNT_FORCE)
623 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
624 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
625 error = lockmgr(&mp->mnt_lock, lflags);
626 if (error) {
627 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
628 if (mp->mnt_kern_flag & MNTK_MWAIT)
629 wakeup(mp);
630 return (error);
633 if (mp->mnt_flag & MNT_EXPUBLIC)
634 vfs_setpublicfs(NULL, NULL, NULL);
636 vfs_msync(mp, MNT_WAIT);
637 async_flag = mp->mnt_flag & MNT_ASYNC;
638 mp->mnt_flag &=~ MNT_ASYNC;
641 * If this filesystem isn't aliasing other filesystems,
642 * try to invalidate any remaining namecache entries and
643 * check the count afterwords.
645 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
646 cache_lock(&mp->mnt_ncmountpt);
647 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
648 cache_unlock(&mp->mnt_ncmountpt);
650 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
651 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
653 if ((flags & MNT_FORCE) == 0) {
654 error = EBUSY;
655 mount_warning(mp, "Cannot unmount: "
656 "%d namecache "
657 "references still "
658 "present",
659 ncp->nc_refs - 1);
660 } else {
661 mount_warning(mp, "Forced unmount: "
662 "%d namecache "
663 "references still "
664 "present",
665 ncp->nc_refs - 1);
666 freeok = 0;
672 * nchandle records ref the mount structure. Expect a count of 1
673 * (our mount->mnt_ncmountpt).
675 if (mp->mnt_refs != 1) {
676 if ((flags & MNT_FORCE) == 0) {
677 mount_warning(mp, "Cannot unmount: "
678 "%d process references still "
679 "present", mp->mnt_refs);
680 error = EBUSY;
681 } else {
682 mount_warning(mp, "Forced unmount: "
683 "%d process references still "
684 "present", mp->mnt_refs);
685 freeok = 0;
690 * Decomission our special mnt_syncer vnode. This also stops
691 * the vnlru code. If we are unable to unmount we recommission
692 * the vnode.
694 if (error == 0) {
695 if ((vp = mp->mnt_syncer) != NULL) {
696 mp->mnt_syncer = NULL;
697 vrele(vp);
699 if (((mp->mnt_flag & MNT_RDONLY) ||
700 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
701 (flags & MNT_FORCE)) {
702 error = VFS_UNMOUNT(mp, flags);
705 if (error) {
706 if (mp->mnt_syncer == NULL)
707 vfs_allocate_syncvnode(mp);
708 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
709 mp->mnt_flag |= async_flag;
710 lockmgr(&mp->mnt_lock, LK_RELEASE);
711 if (mp->mnt_kern_flag & MNTK_MWAIT)
712 wakeup(mp);
713 return (error);
716 * Clean up any journals still associated with the mount after
717 * filesystem activity has ceased.
719 journal_remove_all_journals(mp,
720 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
722 mountlist_remove(mp);
725 * Remove any installed vnode ops here so the individual VFSs don't
726 * have to.
728 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
729 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
730 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
731 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
732 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
734 if (mp->mnt_ncmountpt.ncp != NULL) {
735 nch = mp->mnt_ncmountpt;
736 cache_zero(&mp->mnt_ncmountpt);
737 cache_clrmountpt(&nch);
738 cache_drop(&nch);
740 if (mp->mnt_ncmounton.ncp != NULL) {
741 nch = mp->mnt_ncmounton;
742 cache_zero(&mp->mnt_ncmounton);
743 cache_clrmountpt(&nch);
744 cache_drop(&nch);
747 mp->mnt_vfc->vfc_refcount--;
748 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
749 panic("unmount: dangling vnode");
750 lockmgr(&mp->mnt_lock, LK_RELEASE);
751 if (mp->mnt_kern_flag & MNTK_MWAIT)
752 wakeup(mp);
753 if (freeok)
754 kfree(mp, M_MOUNT);
755 return (0);
758 static
759 void
760 mount_warning(struct mount *mp, const char *ctl, ...)
762 char *ptr;
763 char *buf;
764 __va_list va;
766 __va_start(va, ctl);
767 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
768 kprintf("unmount(%s): ", ptr);
769 kvprintf(ctl, va);
770 kprintf("\n");
771 kfree(buf, M_TEMP);
772 } else {
773 kprintf("unmount(%p", mp);
774 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
775 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
776 kprintf("): ");
777 kvprintf(ctl, va);
778 kprintf("\n");
780 __va_end(va);
784 * Shim cache_fullpath() to handle the case where a process is chrooted into
785 * a subdirectory of a mount. In this case if the root mount matches the
786 * process root directory's mount we have to specify the process's root
787 * directory instead of the mount point, because the mount point might
788 * be above the root directory.
790 static
792 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
794 struct nchandle *nch;
796 if (p && p->p_fd->fd_nrdir.mount == mp)
797 nch = &p->p_fd->fd_nrdir;
798 else
799 nch = &mp->mnt_ncmountpt;
800 return(cache_fullpath(p, nch, rb, fb));
804 * Sync each mounted filesystem.
807 #ifdef DEBUG
808 static int syncprt = 0;
809 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
810 #endif /* DEBUG */
812 static int sync_callback(struct mount *mp, void *data);
814 /* ARGSUSED */
816 sys_sync(struct sync_args *uap)
818 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
819 #ifdef DEBUG
821 * print out buffer pool stat information on each sync() call.
823 if (syncprt)
824 vfs_bufstats();
825 #endif /* DEBUG */
826 return (0);
829 static
831 sync_callback(struct mount *mp, void *data __unused)
833 int asyncflag;
835 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
836 asyncflag = mp->mnt_flag & MNT_ASYNC;
837 mp->mnt_flag &= ~MNT_ASYNC;
838 vfs_msync(mp, MNT_NOWAIT);
839 VFS_SYNC(mp, MNT_NOWAIT);
840 mp->mnt_flag |= asyncflag;
842 return(0);
845 /* XXX PRISON: could be per prison flag */
846 static int prison_quotas;
847 #if 0
848 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
849 #endif
852 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
854 * Change filesystem quotas.
856 /* ARGSUSED */
858 sys_quotactl(struct quotactl_args *uap)
860 struct nlookupdata nd;
861 struct thread *td;
862 struct proc *p;
863 struct mount *mp;
864 int error;
866 td = curthread;
867 p = td->td_proc;
868 if (p->p_ucred->cr_prison && !prison_quotas)
869 return (EPERM);
871 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
872 if (error == 0)
873 error = nlookup(&nd);
874 if (error == 0) {
875 mp = nd.nl_nch.mount;
876 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
877 uap->arg, nd.nl_cred);
879 nlookup_done(&nd);
880 return (error);
884 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
885 * void *buf, int buflen)
887 * This function operates on a mount point and executes the specified
888 * operation using the specified control data, and possibly returns data.
890 * The actual number of bytes stored in the result buffer is returned, 0
891 * if none, otherwise an error is returned.
893 /* ARGSUSED */
895 sys_mountctl(struct mountctl_args *uap)
897 struct thread *td = curthread;
898 struct proc *p = td->td_proc;
899 struct file *fp;
900 void *ctl = NULL;
901 void *buf = NULL;
902 char *path = NULL;
903 int error;
906 * Sanity and permissions checks. We must be root.
908 KKASSERT(p);
909 if (p->p_ucred->cr_prison != NULL)
910 return (EPERM);
911 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
912 (error = priv_check(td, PRIV_ROOT)) != 0)
913 return (error);
916 * Argument length checks
918 if (uap->ctllen < 0 || uap->ctllen > 1024)
919 return (EINVAL);
920 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
921 return (EINVAL);
922 if (uap->path == NULL)
923 return (EINVAL);
926 * Allocate the necessary buffers and copyin data
928 path = objcache_get(namei_oc, M_WAITOK);
929 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
930 if (error)
931 goto done;
933 if (uap->ctllen) {
934 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
935 error = copyin(uap->ctl, ctl, uap->ctllen);
936 if (error)
937 goto done;
939 if (uap->buflen)
940 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
943 * Validate the descriptor
945 if (uap->fd >= 0) {
946 fp = holdfp(p->p_fd, uap->fd, -1);
947 if (fp == NULL) {
948 error = EBADF;
949 goto done;
951 } else {
952 fp = NULL;
956 * Execute the internal kernel function and clean up.
958 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
959 if (fp)
960 fdrop(fp);
961 if (error == 0 && uap->sysmsg_result > 0)
962 error = copyout(buf, uap->buf, uap->sysmsg_result);
963 done:
964 if (path)
965 objcache_put(namei_oc, path);
966 if (ctl)
967 kfree(ctl, M_TEMP);
968 if (buf)
969 kfree(buf, M_TEMP);
970 return (error);
974 * Execute a mount control operation by resolving the path to a mount point
975 * and calling vop_mountctl().
977 * Use the mount point from the nch instead of the vnode so nullfs mounts
978 * can properly spike the VOP.
981 kern_mountctl(const char *path, int op, struct file *fp,
982 const void *ctl, int ctllen,
983 void *buf, int buflen, int *res)
985 struct vnode *vp;
986 struct mount *mp;
987 struct nlookupdata nd;
988 int error;
990 *res = 0;
991 vp = NULL;
992 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
993 if (error == 0)
994 error = nlookup(&nd);
995 if (error == 0)
996 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
997 mp = nd.nl_nch.mount;
998 nlookup_done(&nd);
999 if (error)
1000 return (error);
1003 * Must be the root of the filesystem
1005 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1006 vput(vp);
1007 return (EINVAL);
1009 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1010 buf, buflen, res);
1011 vput(vp);
1012 return (error);
1016 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1018 struct thread *td = curthread;
1019 struct proc *p = td->td_proc;
1020 struct mount *mp;
1021 struct statfs *sp;
1022 char *fullpath, *freepath;
1023 int error;
1025 if ((error = nlookup(nd)) != 0)
1026 return (error);
1027 mp = nd->nl_nch.mount;
1028 sp = &mp->mnt_stat;
1029 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1030 return (error);
1032 error = mount_path(p, mp, &fullpath, &freepath);
1033 if (error)
1034 return(error);
1035 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1036 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1037 kfree(freepath, M_TEMP);
1039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1040 bcopy(sp, buf, sizeof(*buf));
1041 /* Only root should have access to the fsid's. */
1042 if (priv_check(td, PRIV_ROOT))
1043 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1044 return (0);
1048 * statfs_args(char *path, struct statfs *buf)
1050 * Get filesystem statistics.
1053 sys_statfs(struct statfs_args *uap)
1055 struct nlookupdata nd;
1056 struct statfs buf;
1057 int error;
1059 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1060 if (error == 0)
1061 error = kern_statfs(&nd, &buf);
1062 nlookup_done(&nd);
1063 if (error == 0)
1064 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1065 return (error);
1069 kern_fstatfs(int fd, struct statfs *buf)
1071 struct thread *td = curthread;
1072 struct proc *p = td->td_proc;
1073 struct file *fp;
1074 struct mount *mp;
1075 struct statfs *sp;
1076 char *fullpath, *freepath;
1077 int error;
1079 KKASSERT(p);
1080 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1081 return (error);
1082 mp = ((struct vnode *)fp->f_data)->v_mount;
1083 if (mp == NULL) {
1084 error = EBADF;
1085 goto done;
1087 if (fp->f_cred == NULL) {
1088 error = EINVAL;
1089 goto done;
1091 sp = &mp->mnt_stat;
1092 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1093 goto done;
1095 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1096 goto done;
1097 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1098 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1099 kfree(freepath, M_TEMP);
1101 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1102 bcopy(sp, buf, sizeof(*buf));
1104 /* Only root should have access to the fsid's. */
1105 if (priv_check(td, PRIV_ROOT))
1106 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1107 error = 0;
1108 done:
1109 fdrop(fp);
1110 return (error);
1114 * fstatfs_args(int fd, struct statfs *buf)
1116 * Get filesystem statistics.
1119 sys_fstatfs(struct fstatfs_args *uap)
1121 struct statfs buf;
1122 int error;
1124 error = kern_fstatfs(uap->fd, &buf);
1126 if (error == 0)
1127 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1128 return (error);
1132 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1134 struct mount *mp;
1135 struct statvfs *sp;
1136 int error;
1138 if ((error = nlookup(nd)) != 0)
1139 return (error);
1140 mp = nd->nl_nch.mount;
1141 sp = &mp->mnt_vstat;
1142 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1143 return (error);
1145 sp->f_flag = 0;
1146 if (mp->mnt_flag & MNT_RDONLY)
1147 sp->f_flag |= ST_RDONLY;
1148 if (mp->mnt_flag & MNT_NOSUID)
1149 sp->f_flag |= ST_NOSUID;
1150 bcopy(sp, buf, sizeof(*buf));
1151 return (0);
1155 * statfs_args(char *path, struct statfs *buf)
1157 * Get filesystem statistics.
1160 sys_statvfs(struct statvfs_args *uap)
1162 struct nlookupdata nd;
1163 struct statvfs buf;
1164 int error;
1166 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1167 if (error == 0)
1168 error = kern_statvfs(&nd, &buf);
1169 nlookup_done(&nd);
1170 if (error == 0)
1171 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1172 return (error);
1176 kern_fstatvfs(int fd, struct statvfs *buf)
1178 struct thread *td = curthread;
1179 struct proc *p = td->td_proc;
1180 struct file *fp;
1181 struct mount *mp;
1182 struct statvfs *sp;
1183 int error;
1185 KKASSERT(p);
1186 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1187 return (error);
1188 mp = ((struct vnode *)fp->f_data)->v_mount;
1189 if (mp == NULL) {
1190 error = EBADF;
1191 goto done;
1193 if (fp->f_cred == NULL) {
1194 error = EINVAL;
1195 goto done;
1197 sp = &mp->mnt_vstat;
1198 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1199 goto done;
1201 sp->f_flag = 0;
1202 if (mp->mnt_flag & MNT_RDONLY)
1203 sp->f_flag |= ST_RDONLY;
1204 if (mp->mnt_flag & MNT_NOSUID)
1205 sp->f_flag |= ST_NOSUID;
1207 bcopy(sp, buf, sizeof(*buf));
1208 error = 0;
1209 done:
1210 fdrop(fp);
1211 return (error);
1215 * fstatfs_args(int fd, struct statfs *buf)
1217 * Get filesystem statistics.
1220 sys_fstatvfs(struct fstatvfs_args *uap)
1222 struct statvfs buf;
1223 int error;
1225 error = kern_fstatvfs(uap->fd, &buf);
1227 if (error == 0)
1228 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1229 return (error);
1233 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1235 * Get statistics on all filesystems.
1238 struct getfsstat_info {
1239 struct statfs *sfsp;
1240 long count;
1241 long maxcount;
1242 int error;
1243 int flags;
1244 struct proc *p;
1247 static int getfsstat_callback(struct mount *, void *);
1249 /* ARGSUSED */
1251 sys_getfsstat(struct getfsstat_args *uap)
1253 struct thread *td = curthread;
1254 struct proc *p = td->td_proc;
1255 struct getfsstat_info info;
1257 bzero(&info, sizeof(info));
1259 info.maxcount = uap->bufsize / sizeof(struct statfs);
1260 info.sfsp = uap->buf;
1261 info.count = 0;
1262 info.flags = uap->flags;
1263 info.p = p;
1265 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1266 if (info.sfsp && info.count > info.maxcount)
1267 uap->sysmsg_result = info.maxcount;
1268 else
1269 uap->sysmsg_result = info.count;
1270 return (info.error);
1273 static int
1274 getfsstat_callback(struct mount *mp, void *data)
1276 struct getfsstat_info *info = data;
1277 struct statfs *sp;
1278 char *freepath;
1279 char *fullpath;
1280 int error;
1282 if (info->sfsp && info->count < info->maxcount) {
1283 if (info->p && !chroot_visible_mnt(mp, info->p))
1284 return(0);
1285 sp = &mp->mnt_stat;
1288 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1289 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1290 * overrides MNT_WAIT.
1292 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1293 (info->flags & MNT_WAIT)) &&
1294 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1295 return(0);
1297 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1299 error = mount_path(info->p, mp, &fullpath, &freepath);
1300 if (error) {
1301 info->error = error;
1302 return(-1);
1304 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1305 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1306 kfree(freepath, M_TEMP);
1308 error = copyout(sp, info->sfsp, sizeof(*sp));
1309 if (error) {
1310 info->error = error;
1311 return (-1);
1313 ++info->sfsp;
1315 info->count++;
1316 return(0);
1320 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1321 long bufsize, int flags)
1323 * Get statistics on all filesystems.
1326 struct getvfsstat_info {
1327 struct statfs *sfsp;
1328 struct statvfs *vsfsp;
1329 long count;
1330 long maxcount;
1331 int error;
1332 int flags;
1333 struct proc *p;
1336 static int getvfsstat_callback(struct mount *, void *);
1338 /* ARGSUSED */
1340 sys_getvfsstat(struct getvfsstat_args *uap)
1342 struct thread *td = curthread;
1343 struct proc *p = td->td_proc;
1344 struct getvfsstat_info info;
1346 bzero(&info, sizeof(info));
1348 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1349 info.sfsp = uap->buf;
1350 info.vsfsp = uap->vbuf;
1351 info.count = 0;
1352 info.flags = uap->flags;
1353 info.p = p;
1355 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1356 if (info.vsfsp && info.count > info.maxcount)
1357 uap->sysmsg_result = info.maxcount;
1358 else
1359 uap->sysmsg_result = info.count;
1360 return (info.error);
1363 static int
1364 getvfsstat_callback(struct mount *mp, void *data)
1366 struct getvfsstat_info *info = data;
1367 struct statfs *sp;
1368 struct statvfs *vsp;
1369 char *freepath;
1370 char *fullpath;
1371 int error;
1373 if (info->vsfsp && info->count < info->maxcount) {
1374 if (info->p && !chroot_visible_mnt(mp, info->p))
1375 return(0);
1376 sp = &mp->mnt_stat;
1377 vsp = &mp->mnt_vstat;
1380 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1381 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1382 * overrides MNT_WAIT.
1384 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1385 (info->flags & MNT_WAIT)) &&
1386 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1387 return(0);
1389 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1391 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1392 (info->flags & MNT_WAIT)) &&
1393 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1394 return(0);
1396 vsp->f_flag = 0;
1397 if (mp->mnt_flag & MNT_RDONLY)
1398 vsp->f_flag |= ST_RDONLY;
1399 if (mp->mnt_flag & MNT_NOSUID)
1400 vsp->f_flag |= ST_NOSUID;
1402 error = mount_path(info->p, mp, &fullpath, &freepath);
1403 if (error) {
1404 info->error = error;
1405 return(-1);
1407 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1408 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1409 kfree(freepath, M_TEMP);
1411 error = copyout(sp, info->sfsp, sizeof(*sp));
1412 if (error == 0)
1413 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1414 if (error) {
1415 info->error = error;
1416 return (-1);
1418 ++info->sfsp;
1419 ++info->vsfsp;
1421 info->count++;
1422 return(0);
1427 * fchdir_args(int fd)
1429 * Change current working directory to a given file descriptor.
1431 /* ARGSUSED */
1433 sys_fchdir(struct fchdir_args *uap)
1435 struct thread *td = curthread;
1436 struct proc *p = td->td_proc;
1437 struct filedesc *fdp = p->p_fd;
1438 struct vnode *vp, *ovp;
1439 struct mount *mp;
1440 struct file *fp;
1441 struct nchandle nch, onch, tnch;
1442 int error;
1444 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1445 return (error);
1446 vp = (struct vnode *)fp->f_data;
1447 vref(vp);
1448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1449 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1450 error = ENOTDIR;
1451 else
1452 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1453 if (error) {
1454 vput(vp);
1455 fdrop(fp);
1456 return (error);
1458 cache_copy(&fp->f_nchandle, &nch);
1461 * If the ncp has become a mount point, traverse through
1462 * the mount point.
1465 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1466 (mp = cache_findmount(&nch)) != NULL
1468 error = nlookup_mp(mp, &tnch);
1469 if (error == 0) {
1470 cache_unlock(&tnch); /* leave ref intact */
1471 vput(vp);
1472 vp = tnch.ncp->nc_vp;
1473 error = vget(vp, LK_SHARED);
1474 KKASSERT(error == 0);
1475 cache_drop(&nch);
1476 nch = tnch;
1479 if (error == 0) {
1480 ovp = fdp->fd_cdir;
1481 onch = fdp->fd_ncdir;
1482 vn_unlock(vp); /* leave ref intact */
1483 fdp->fd_cdir = vp;
1484 fdp->fd_ncdir = nch;
1485 cache_drop(&onch);
1486 vrele(ovp);
1487 } else {
1488 cache_drop(&nch);
1489 vput(vp);
1491 fdrop(fp);
1492 return (error);
1496 kern_chdir(struct nlookupdata *nd)
1498 struct thread *td = curthread;
1499 struct proc *p = td->td_proc;
1500 struct filedesc *fdp = p->p_fd;
1501 struct vnode *vp, *ovp;
1502 struct nchandle onch;
1503 int error;
1505 if ((error = nlookup(nd)) != 0)
1506 return (error);
1507 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1508 return (ENOENT);
1509 if ((error = vget(vp, LK_SHARED)) != 0)
1510 return (error);
1512 error = checkvp_chdir(vp, td);
1513 vn_unlock(vp);
1514 if (error == 0) {
1515 ovp = fdp->fd_cdir;
1516 onch = fdp->fd_ncdir;
1517 cache_unlock(&nd->nl_nch); /* leave reference intact */
1518 fdp->fd_ncdir = nd->nl_nch;
1519 fdp->fd_cdir = vp;
1520 cache_drop(&onch);
1521 vrele(ovp);
1522 cache_zero(&nd->nl_nch);
1523 } else {
1524 vrele(vp);
1526 return (error);
1530 * chdir_args(char *path)
1532 * Change current working directory (``.'').
1535 sys_chdir(struct chdir_args *uap)
1537 struct nlookupdata nd;
1538 int error;
1540 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1541 if (error == 0)
1542 error = kern_chdir(&nd);
1543 nlookup_done(&nd);
1544 return (error);
1548 * Helper function for raised chroot(2) security function: Refuse if
1549 * any filedescriptors are open directories.
1551 static int
1552 chroot_refuse_vdir_fds(struct filedesc *fdp)
1554 struct vnode *vp;
1555 struct file *fp;
1556 int error;
1557 int fd;
1559 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1560 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1561 continue;
1562 vp = (struct vnode *)fp->f_data;
1563 if (vp->v_type != VDIR) {
1564 fdrop(fp);
1565 continue;
1567 fdrop(fp);
1568 return(EPERM);
1570 return (0);
1574 * This sysctl determines if we will allow a process to chroot(2) if it
1575 * has a directory open:
1576 * 0: disallowed for all processes.
1577 * 1: allowed for processes that were not already chroot(2)'ed.
1578 * 2: allowed for all processes.
1581 static int chroot_allow_open_directories = 1;
1583 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1584 &chroot_allow_open_directories, 0, "");
1587 * chroot to the specified namecache entry. We obtain the vp from the
1588 * namecache data. The passed ncp must be locked and referenced and will
1589 * remain locked and referenced on return.
1592 kern_chroot(struct nchandle *nch)
1594 struct thread *td = curthread;
1595 struct proc *p = td->td_proc;
1596 struct filedesc *fdp = p->p_fd;
1597 struct vnode *vp;
1598 int error;
1601 * Only privileged user can chroot
1603 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1604 if (error)
1605 return (error);
1608 * Disallow open directory descriptors (fchdir() breakouts).
1610 if (chroot_allow_open_directories == 0 ||
1611 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1612 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1613 return (error);
1615 if ((vp = nch->ncp->nc_vp) == NULL)
1616 return (ENOENT);
1618 if ((error = vget(vp, LK_SHARED)) != 0)
1619 return (error);
1622 * Check the validity of vp as a directory to change to and
1623 * associate it with rdir/jdir.
1625 error = checkvp_chdir(vp, td);
1626 vn_unlock(vp); /* leave reference intact */
1627 if (error == 0) {
1628 vrele(fdp->fd_rdir);
1629 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1630 cache_drop(&fdp->fd_nrdir);
1631 cache_copy(nch, &fdp->fd_nrdir);
1632 if (fdp->fd_jdir == NULL) {
1633 fdp->fd_jdir = vp;
1634 vref(fdp->fd_jdir);
1635 cache_copy(nch, &fdp->fd_njdir);
1637 } else {
1638 vrele(vp);
1640 return (error);
1644 * chroot_args(char *path)
1646 * Change notion of root (``/'') directory.
1648 /* ARGSUSED */
1650 sys_chroot(struct chroot_args *uap)
1652 struct thread *td = curthread;
1653 struct nlookupdata nd;
1654 int error;
1656 KKASSERT(td->td_proc);
1657 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1658 if (error) {
1659 nlookup_done(&nd);
1660 return(error);
1662 nd.nl_flags |= NLC_EXEC;
1663 error = nlookup(&nd);
1664 if (error == 0)
1665 error = kern_chroot(&nd.nl_nch);
1666 nlookup_done(&nd);
1667 return(error);
1671 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1672 * determine whether it is legal to chdir to the vnode. The vnode's state
1673 * is not changed by this call.
1676 checkvp_chdir(struct vnode *vp, struct thread *td)
1678 int error;
1680 if (vp->v_type != VDIR)
1681 error = ENOTDIR;
1682 else
1683 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1684 return (error);
1688 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1690 struct thread *td = curthread;
1691 struct proc *p = td->td_proc;
1692 struct lwp *lp = td->td_lwp;
1693 struct filedesc *fdp = p->p_fd;
1694 int cmode, flags;
1695 struct file *nfp;
1696 struct file *fp;
1697 struct vnode *vp;
1698 int type, indx, error;
1699 struct flock lf;
1701 if ((oflags & O_ACCMODE) == O_ACCMODE)
1702 return (EINVAL);
1703 flags = FFLAGS(oflags);
1704 error = falloc(p, &nfp, NULL);
1705 if (error)
1706 return (error);
1707 fp = nfp;
1708 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1711 * XXX p_dupfd is a real mess. It allows a device to return a
1712 * file descriptor to be duplicated rather then doing the open
1713 * itself.
1715 lp->lwp_dupfd = -1;
1718 * Call vn_open() to do the lookup and assign the vnode to the
1719 * file pointer. vn_open() does not change the ref count on fp
1720 * and the vnode, on success, will be inherited by the file pointer
1721 * and unlocked.
1723 nd->nl_flags |= NLC_LOCKVP;
1724 error = vn_open(nd, fp, flags, cmode);
1725 nlookup_done(nd);
1726 if (error) {
1728 * handle special fdopen() case. bleh. dupfdopen() is
1729 * responsible for dropping the old contents of ofiles[indx]
1730 * if it succeeds.
1732 * Note that fsetfd() will add a ref to fp which represents
1733 * the fd_files[] assignment. We must still drop our
1734 * reference.
1736 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1737 if (fdalloc(p, 0, &indx) == 0) {
1738 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1739 if (error == 0) {
1740 *res = indx;
1741 fdrop(fp); /* our ref */
1742 return (0);
1744 fsetfd(p, NULL, indx);
1747 fdrop(fp); /* our ref */
1748 if (error == ERESTART)
1749 error = EINTR;
1750 return (error);
1754 * ref the vnode for ourselves so it can't be ripped out from under
1755 * is. XXX need an ND flag to request that the vnode be returned
1756 * anyway.
1758 * Reserve a file descriptor but do not assign it until the open
1759 * succeeds.
1761 vp = (struct vnode *)fp->f_data;
1762 vref(vp);
1763 if ((error = fdalloc(p, 0, &indx)) != 0) {
1764 fdrop(fp);
1765 vrele(vp);
1766 return (error);
1770 * If no error occurs the vp will have been assigned to the file
1771 * pointer.
1773 lp->lwp_dupfd = 0;
1775 if (flags & (O_EXLOCK | O_SHLOCK)) {
1776 lf.l_whence = SEEK_SET;
1777 lf.l_start = 0;
1778 lf.l_len = 0;
1779 if (flags & O_EXLOCK)
1780 lf.l_type = F_WRLCK;
1781 else
1782 lf.l_type = F_RDLCK;
1783 if (flags & FNONBLOCK)
1784 type = 0;
1785 else
1786 type = F_WAIT;
1788 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1790 * lock request failed. Clean up the reserved
1791 * descriptor.
1793 vrele(vp);
1794 fsetfd(p, NULL, indx);
1795 fdrop(fp);
1796 return (error);
1798 fp->f_flag |= FHASLOCK;
1800 #if 0
1802 * Assert that all regular file vnodes were created with a object.
1804 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1805 ("open: regular file has no backing object after vn_open"));
1806 #endif
1808 vrele(vp);
1811 * release our private reference, leaving the one associated with the
1812 * descriptor table intact.
1814 fsetfd(p, fp, indx);
1815 fdrop(fp);
1816 *res = indx;
1817 return (0);
1821 * open_args(char *path, int flags, int mode)
1823 * Check permissions, allocate an open file structure,
1824 * and call the device open routine if any.
1827 sys_open(struct open_args *uap)
1829 struct nlookupdata nd;
1830 int error;
1832 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1833 if (error == 0) {
1834 error = kern_open(&nd, uap->flags,
1835 uap->mode, &uap->sysmsg_result);
1837 nlookup_done(&nd);
1838 return (error);
1842 * openat_args(int fd, char *path, int flags, int mode)
1845 sys_openat(struct openat_args *uap)
1847 struct nlookupdata nd;
1848 int error;
1849 struct file *fp;
1851 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1852 if (error == 0) {
1853 error = kern_open(&nd, uap->flags, uap->mode,
1854 &uap->sysmsg_result);
1856 nlookup_done_at(&nd, fp);
1857 return (error);
1861 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1863 struct thread *td = curthread;
1864 struct proc *p = td->td_proc;
1865 struct vnode *vp;
1866 struct vattr vattr;
1867 int error;
1868 int whiteout = 0;
1870 KKASSERT(p);
1872 VATTR_NULL(&vattr);
1873 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1874 vattr.va_rmajor = rmajor;
1875 vattr.va_rminor = rminor;
1877 switch (mode & S_IFMT) {
1878 case S_IFMT: /* used by badsect to flag bad sectors */
1879 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1880 vattr.va_type = VBAD;
1881 break;
1882 case S_IFCHR:
1883 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1884 vattr.va_type = VCHR;
1885 break;
1886 case S_IFBLK:
1887 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1888 vattr.va_type = VBLK;
1889 break;
1890 case S_IFWHT:
1891 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1892 whiteout = 1;
1893 break;
1894 case S_IFDIR: /* special directories support for HAMMER */
1895 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1896 vattr.va_type = VDIR;
1897 break;
1898 default:
1899 error = EINVAL;
1900 break;
1903 if (error)
1904 return (error);
1906 bwillinode(1);
1907 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1908 if ((error = nlookup(nd)) != 0)
1909 return (error);
1910 if (nd->nl_nch.ncp->nc_vp)
1911 return (EEXIST);
1912 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1913 return (error);
1915 if (whiteout) {
1916 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1917 nd->nl_cred, NAMEI_CREATE);
1918 } else {
1919 vp = NULL;
1920 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1921 &vp, nd->nl_cred, &vattr);
1922 if (error == 0)
1923 vput(vp);
1925 return (error);
1929 * mknod_args(char *path, int mode, int dev)
1931 * Create a special file.
1934 sys_mknod(struct mknod_args *uap)
1936 struct nlookupdata nd;
1937 int error;
1939 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1940 if (error == 0) {
1941 error = kern_mknod(&nd, uap->mode,
1942 umajor(uap->dev), uminor(uap->dev));
1944 nlookup_done(&nd);
1945 return (error);
1949 kern_mkfifo(struct nlookupdata *nd, int mode)
1951 struct thread *td = curthread;
1952 struct proc *p = td->td_proc;
1953 struct vattr vattr;
1954 struct vnode *vp;
1955 int error;
1957 bwillinode(1);
1959 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1960 if ((error = nlookup(nd)) != 0)
1961 return (error);
1962 if (nd->nl_nch.ncp->nc_vp)
1963 return (EEXIST);
1964 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1965 return (error);
1967 VATTR_NULL(&vattr);
1968 vattr.va_type = VFIFO;
1969 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1970 vp = NULL;
1971 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1972 if (error == 0)
1973 vput(vp);
1974 return (error);
1978 * mkfifo_args(char *path, int mode)
1980 * Create a named pipe.
1983 sys_mkfifo(struct mkfifo_args *uap)
1985 struct nlookupdata nd;
1986 int error;
1988 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1989 if (error == 0)
1990 error = kern_mkfifo(&nd, uap->mode);
1991 nlookup_done(&nd);
1992 return (error);
1995 static int hardlink_check_uid = 0;
1996 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1997 &hardlink_check_uid, 0,
1998 "Unprivileged processes cannot create hard links to files owned by other "
1999 "users");
2000 static int hardlink_check_gid = 0;
2001 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2002 &hardlink_check_gid, 0,
2003 "Unprivileged processes cannot create hard links to files owned by other "
2004 "groups");
2006 static int
2007 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2009 struct vattr va;
2010 int error;
2013 * Shortcut if disabled
2015 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2016 return (0);
2019 * Privileged user can always hardlink
2021 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2022 return (0);
2025 * Otherwise only if the originating file is owned by the
2026 * same user or group. Note that any group is allowed if
2027 * the file is owned by the caller.
2029 error = VOP_GETATTR(vp, &va);
2030 if (error != 0)
2031 return (error);
2033 if (hardlink_check_uid) {
2034 if (cred->cr_uid != va.va_uid)
2035 return (EPERM);
2038 if (hardlink_check_gid) {
2039 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2040 return (EPERM);
2043 return (0);
2047 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2049 struct thread *td = curthread;
2050 struct vnode *vp;
2051 int error;
2054 * Lookup the source and obtained a locked vnode.
2056 * You may only hardlink a file which you have write permission
2057 * on or which you own.
2059 * XXX relookup on vget failure / race ?
2061 bwillinode(1);
2062 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2063 if ((error = nlookup(nd)) != 0)
2064 return (error);
2065 vp = nd->nl_nch.ncp->nc_vp;
2066 KKASSERT(vp != NULL);
2067 if (vp->v_type == VDIR)
2068 return (EPERM); /* POSIX */
2069 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2070 return (error);
2071 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2072 return (error);
2075 * Unlock the source so we can lookup the target without deadlocking
2076 * (XXX vp is locked already, possible other deadlock?). The target
2077 * must not exist.
2079 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2080 nd->nl_flags &= ~NLC_NCPISLOCKED;
2081 cache_unlock(&nd->nl_nch);
2083 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2084 if ((error = nlookup(linknd)) != 0) {
2085 vput(vp);
2086 return (error);
2088 if (linknd->nl_nch.ncp->nc_vp) {
2089 vput(vp);
2090 return (EEXIST);
2094 * Finally run the new API VOP.
2096 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2097 if (error == 0) {
2098 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2099 vp, linknd->nl_cred);
2101 vput(vp);
2102 return (error);
2106 * link_args(char *path, char *link)
2108 * Make a hard file link.
2111 sys_link(struct link_args *uap)
2113 struct nlookupdata nd, linknd;
2114 int error;
2116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2117 if (error == 0) {
2118 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2119 if (error == 0)
2120 error = kern_link(&nd, &linknd);
2121 nlookup_done(&linknd);
2123 nlookup_done(&nd);
2124 return (error);
2128 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2130 struct vattr vattr;
2131 struct vnode *vp;
2132 struct vnode *dvp;
2133 int error;
2135 bwillinode(1);
2136 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2137 if ((error = nlookup(nd)) != 0)
2138 return (error);
2139 if (nd->nl_nch.ncp->nc_vp)
2140 return (EEXIST);
2141 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2142 return (error);
2143 dvp = nd->nl_dvp;
2144 VATTR_NULL(&vattr);
2145 vattr.va_mode = mode;
2146 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2147 if (error == 0)
2148 vput(vp);
2149 return (error);
2153 * symlink(char *path, char *link)
2155 * Make a symbolic link.
2158 sys_symlink(struct symlink_args *uap)
2160 struct thread *td = curthread;
2161 struct nlookupdata nd;
2162 char *path;
2163 int error;
2164 int mode;
2166 path = objcache_get(namei_oc, M_WAITOK);
2167 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2168 if (error == 0) {
2169 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2170 if (error == 0) {
2171 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2172 error = kern_symlink(&nd, path, mode);
2174 nlookup_done(&nd);
2176 objcache_put(namei_oc, path);
2177 return (error);
2181 * undelete_args(char *path)
2183 * Delete a whiteout from the filesystem.
2185 /* ARGSUSED */
2187 sys_undelete(struct undelete_args *uap)
2189 struct nlookupdata nd;
2190 int error;
2192 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2193 bwillinode(1);
2194 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2195 if (error == 0)
2196 error = nlookup(&nd);
2197 if (error == 0)
2198 error = ncp_writechk(&nd.nl_nch);
2199 if (error == 0) {
2200 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2201 NAMEI_DELETE);
2203 nlookup_done(&nd);
2204 return (error);
2208 kern_unlink(struct nlookupdata *nd)
2210 int error;
2212 bwillinode(1);
2213 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2214 if ((error = nlookup(nd)) != 0)
2215 return (error);
2216 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2217 return (error);
2218 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2219 return (error);
2223 * unlink_args(char *path)
2225 * Delete a name from the filesystem.
2228 sys_unlink(struct unlink_args *uap)
2230 struct nlookupdata nd;
2231 int error;
2233 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2234 if (error == 0)
2235 error = kern_unlink(&nd);
2236 nlookup_done(&nd);
2237 return (error);
2241 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2243 struct thread *td = curthread;
2244 struct proc *p = td->td_proc;
2245 struct file *fp;
2246 struct vnode *vp;
2247 struct vattr vattr;
2248 off_t new_offset;
2249 int error;
2251 fp = holdfp(p->p_fd, fd, -1);
2252 if (fp == NULL)
2253 return (EBADF);
2254 if (fp->f_type != DTYPE_VNODE) {
2255 error = ESPIPE;
2256 goto done;
2258 vp = (struct vnode *)fp->f_data;
2260 switch (whence) {
2261 case L_INCR:
2262 new_offset = fp->f_offset + offset;
2263 error = 0;
2264 break;
2265 case L_XTND:
2266 error = VOP_GETATTR(vp, &vattr);
2267 new_offset = offset + vattr.va_size;
2268 break;
2269 case L_SET:
2270 new_offset = offset;
2271 error = 0;
2272 break;
2273 default:
2274 new_offset = 0;
2275 error = EINVAL;
2276 break;
2280 * Validate the seek position. Negative offsets are not allowed
2281 * for regular files or directories.
2283 * Normally we would also not want to allow negative offsets for
2284 * character and block-special devices. However kvm addresses
2285 * on 64 bit architectures might appear to be negative and must
2286 * be allowed.
2288 if (error == 0) {
2289 if (new_offset < 0 &&
2290 (vp->v_type == VREG || vp->v_type == VDIR)) {
2291 error = EINVAL;
2292 } else {
2293 fp->f_offset = new_offset;
2296 *res = fp->f_offset;
2297 done:
2298 fdrop(fp);
2299 return (error);
2303 * lseek_args(int fd, int pad, off_t offset, int whence)
2305 * Reposition read/write file offset.
2308 sys_lseek(struct lseek_args *uap)
2310 int error;
2312 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2313 &uap->sysmsg_offset);
2315 return (error);
2319 kern_access(struct nlookupdata *nd, int aflags)
2321 struct vnode *vp;
2322 int error, flags;
2324 if ((error = nlookup(nd)) != 0)
2325 return (error);
2326 retry:
2327 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2328 if (error)
2329 return (error);
2331 /* Flags == 0 means only check for existence. */
2332 if (aflags) {
2333 flags = 0;
2334 if (aflags & R_OK)
2335 flags |= VREAD;
2336 if (aflags & W_OK)
2337 flags |= VWRITE;
2338 if (aflags & X_OK)
2339 flags |= VEXEC;
2340 if ((flags & VWRITE) == 0 ||
2341 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2342 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2345 * If the file handle is stale we have to re-resolve the
2346 * entry. This is a hack at the moment.
2348 if (error == ESTALE) {
2349 vput(vp);
2350 cache_setunresolved(&nd->nl_nch);
2351 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2352 if (error == 0) {
2353 vp = NULL;
2354 goto retry;
2356 return(error);
2359 vput(vp);
2360 return (error);
2364 * access_args(char *path, int flags)
2366 * Check access permissions.
2369 sys_access(struct access_args *uap)
2371 struct nlookupdata nd;
2372 int error;
2374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2375 if (error == 0)
2376 error = kern_access(&nd, uap->flags);
2377 nlookup_done(&nd);
2378 return (error);
2382 kern_stat(struct nlookupdata *nd, struct stat *st)
2384 int error;
2385 struct vnode *vp;
2386 thread_t td;
2388 if ((error = nlookup(nd)) != 0)
2389 return (error);
2390 again:
2391 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2392 return (ENOENT);
2394 td = curthread;
2395 if ((error = vget(vp, LK_SHARED)) != 0)
2396 return (error);
2397 error = vn_stat(vp, st, nd->nl_cred);
2400 * If the file handle is stale we have to re-resolve the entry. This
2401 * is a hack at the moment.
2403 if (error == ESTALE) {
2404 vput(vp);
2405 cache_setunresolved(&nd->nl_nch);
2406 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2407 if (error == 0)
2408 goto again;
2409 } else {
2410 vput(vp);
2412 return (error);
2416 * stat_args(char *path, struct stat *ub)
2418 * Get file status; this version follows links.
2421 sys_stat(struct stat_args *uap)
2423 struct nlookupdata nd;
2424 struct stat st;
2425 int error;
2427 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2428 if (error == 0) {
2429 error = kern_stat(&nd, &st);
2430 if (error == 0)
2431 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2433 nlookup_done(&nd);
2434 return (error);
2438 * lstat_args(char *path, struct stat *ub)
2440 * Get file status; this version does not follow links.
2443 sys_lstat(struct lstat_args *uap)
2445 struct nlookupdata nd;
2446 struct stat st;
2447 int error;
2449 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2450 if (error == 0) {
2451 error = kern_stat(&nd, &st);
2452 if (error == 0)
2453 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2455 nlookup_done(&nd);
2456 return (error);
2460 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2462 * Get status of file pointed to by fd/path.
2465 sys_fstatat(struct fstatat_args *uap)
2467 struct nlookupdata nd;
2468 struct stat st;
2469 int error;
2470 int flags;
2471 struct file *fp;
2473 if (uap->flags & ~_AT_SYMLINK_MASK)
2474 return (EINVAL);
2476 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2478 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2479 UIO_USERSPACE, flags);
2480 if (error == 0) {
2481 error = kern_stat(&nd, &st);
2482 if (error == 0)
2483 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2485 nlookup_done_at(&nd, fp);
2486 return (error);
2490 * pathconf_Args(char *path, int name)
2492 * Get configurable pathname variables.
2494 /* ARGSUSED */
2496 sys_pathconf(struct pathconf_args *uap)
2498 struct nlookupdata nd;
2499 struct vnode *vp;
2500 int error;
2502 vp = NULL;
2503 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2504 if (error == 0)
2505 error = nlookup(&nd);
2506 if (error == 0)
2507 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2508 nlookup_done(&nd);
2509 if (error == 0) {
2510 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2511 vput(vp);
2513 return (error);
2517 * XXX: daver
2518 * kern_readlink isn't properly split yet. There is a copyin burried
2519 * in VOP_READLINK().
2522 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2524 struct thread *td = curthread;
2525 struct proc *p = td->td_proc;
2526 struct vnode *vp;
2527 struct iovec aiov;
2528 struct uio auio;
2529 int error;
2531 if ((error = nlookup(nd)) != 0)
2532 return (error);
2533 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2534 if (error)
2535 return (error);
2536 if (vp->v_type != VLNK) {
2537 error = EINVAL;
2538 } else {
2539 aiov.iov_base = buf;
2540 aiov.iov_len = count;
2541 auio.uio_iov = &aiov;
2542 auio.uio_iovcnt = 1;
2543 auio.uio_offset = 0;
2544 auio.uio_rw = UIO_READ;
2545 auio.uio_segflg = UIO_USERSPACE;
2546 auio.uio_td = td;
2547 auio.uio_resid = count;
2548 error = VOP_READLINK(vp, &auio, p->p_ucred);
2550 vput(vp);
2551 *res = count - auio.uio_resid;
2552 return (error);
2556 * readlink_args(char *path, char *buf, int count)
2558 * Return target name of a symbolic link.
2561 sys_readlink(struct readlink_args *uap)
2563 struct nlookupdata nd;
2564 int error;
2566 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2567 if (error == 0) {
2568 error = kern_readlink(&nd, uap->buf, uap->count,
2569 &uap->sysmsg_result);
2571 nlookup_done(&nd);
2572 return (error);
2575 static int
2576 setfflags(struct vnode *vp, int flags)
2578 struct thread *td = curthread;
2579 struct proc *p = td->td_proc;
2580 int error;
2581 struct vattr vattr;
2584 * Prevent non-root users from setting flags on devices. When
2585 * a device is reused, users can retain ownership of the device
2586 * if they are allowed to set flags and programs assume that
2587 * chown can't fail when done as root.
2589 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2590 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2591 return (error);
2594 * note: vget is required for any operation that might mod the vnode
2595 * so VINACTIVE is properly cleared.
2597 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2598 VATTR_NULL(&vattr);
2599 vattr.va_flags = flags;
2600 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2601 vput(vp);
2603 return (error);
2607 * chflags(char *path, int flags)
2609 * Change flags of a file given a path name.
2611 /* ARGSUSED */
2613 sys_chflags(struct chflags_args *uap)
2615 struct nlookupdata nd;
2616 struct vnode *vp;
2617 int error;
2619 vp = NULL;
2620 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2621 if (error == 0)
2622 error = nlookup(&nd);
2623 if (error == 0)
2624 error = ncp_writechk(&nd.nl_nch);
2625 if (error == 0)
2626 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2627 nlookup_done(&nd);
2628 if (error == 0) {
2629 error = setfflags(vp, uap->flags);
2630 vrele(vp);
2632 return (error);
2636 * lchflags(char *path, int flags)
2638 * Change flags of a file given a path name, but don't follow symlinks.
2640 /* ARGSUSED */
2642 sys_lchflags(struct lchflags_args *uap)
2644 struct nlookupdata nd;
2645 struct vnode *vp;
2646 int error;
2648 vp = NULL;
2649 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2650 if (error == 0)
2651 error = nlookup(&nd);
2652 if (error == 0)
2653 error = ncp_writechk(&nd.nl_nch);
2654 if (error == 0)
2655 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2656 nlookup_done(&nd);
2657 if (error == 0) {
2658 error = setfflags(vp, uap->flags);
2659 vrele(vp);
2661 return (error);
2665 * fchflags_args(int fd, int flags)
2667 * Change flags of a file given a file descriptor.
2669 /* ARGSUSED */
2671 sys_fchflags(struct fchflags_args *uap)
2673 struct thread *td = curthread;
2674 struct proc *p = td->td_proc;
2675 struct file *fp;
2676 int error;
2678 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2679 return (error);
2680 if (fp->f_nchandle.ncp)
2681 error = ncp_writechk(&fp->f_nchandle);
2682 if (error == 0)
2683 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2684 fdrop(fp);
2685 return (error);
2688 static int
2689 setfmode(struct vnode *vp, int mode)
2691 struct thread *td = curthread;
2692 struct proc *p = td->td_proc;
2693 int error;
2694 struct vattr vattr;
2697 * note: vget is required for any operation that might mod the vnode
2698 * so VINACTIVE is properly cleared.
2700 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2701 VATTR_NULL(&vattr);
2702 vattr.va_mode = mode & ALLPERMS;
2703 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2704 vput(vp);
2706 return error;
2710 kern_chmod(struct nlookupdata *nd, int mode)
2712 struct vnode *vp;
2713 int error;
2715 if ((error = nlookup(nd)) != 0)
2716 return (error);
2717 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2718 return (error);
2719 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2720 error = setfmode(vp, mode);
2721 vrele(vp);
2722 return (error);
2726 * chmod_args(char *path, int mode)
2728 * Change mode of a file given path name.
2730 /* ARGSUSED */
2732 sys_chmod(struct chmod_args *uap)
2734 struct nlookupdata nd;
2735 int error;
2737 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2738 if (error == 0)
2739 error = kern_chmod(&nd, uap->mode);
2740 nlookup_done(&nd);
2741 return (error);
2745 * lchmod_args(char *path, int mode)
2747 * Change mode of a file given path name (don't follow links.)
2749 /* ARGSUSED */
2751 sys_lchmod(struct lchmod_args *uap)
2753 struct nlookupdata nd;
2754 int error;
2756 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2757 if (error == 0)
2758 error = kern_chmod(&nd, uap->mode);
2759 nlookup_done(&nd);
2760 return (error);
2764 * fchmod_args(int fd, int mode)
2766 * Change mode of a file given a file descriptor.
2768 /* ARGSUSED */
2770 sys_fchmod(struct fchmod_args *uap)
2772 struct thread *td = curthread;
2773 struct proc *p = td->td_proc;
2774 struct file *fp;
2775 int error;
2777 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2778 return (error);
2779 if (fp->f_nchandle.ncp)
2780 error = ncp_writechk(&fp->f_nchandle);
2781 if (error == 0)
2782 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2783 fdrop(fp);
2784 return (error);
2788 * fchmodat_args(char *path, int mode)
2790 * Change mode of a file pointed to by fd/path.
2793 sys_fchmodat(struct fchmodat_args *uap)
2795 struct nlookupdata nd;
2796 struct file *fp;
2797 int error;
2798 int flags;
2800 if (uap->flags & ~_AT_SYMLINK_MASK)
2801 return (EINVAL);
2802 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2804 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2805 UIO_USERSPACE, flags);
2806 if (error == 0)
2807 error = kern_chmod(&nd, uap->mode);
2808 nlookup_done_at(&nd, fp);
2809 return (error);
2812 static int
2813 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2815 struct thread *td = curthread;
2816 struct proc *p = td->td_proc;
2817 int error;
2818 struct vattr vattr;
2821 * note: vget is required for any operation that might mod the vnode
2822 * so VINACTIVE is properly cleared.
2824 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2825 VATTR_NULL(&vattr);
2826 vattr.va_uid = uid;
2827 vattr.va_gid = gid;
2828 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2829 vput(vp);
2831 return error;
2835 kern_chown(struct nlookupdata *nd, int uid, int gid)
2837 struct vnode *vp;
2838 int error;
2840 if ((error = nlookup(nd)) != 0)
2841 return (error);
2842 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2843 return (error);
2844 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2845 error = setfown(vp, uid, gid);
2846 vrele(vp);
2847 return (error);
2851 * chown(char *path, int uid, int gid)
2853 * Set ownership given a path name.
2856 sys_chown(struct chown_args *uap)
2858 struct nlookupdata nd;
2859 int error;
2861 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2862 if (error == 0)
2863 error = kern_chown(&nd, uap->uid, uap->gid);
2864 nlookup_done(&nd);
2865 return (error);
2869 * lchown_args(char *path, int uid, int gid)
2871 * Set ownership given a path name, do not cross symlinks.
2874 sys_lchown(struct lchown_args *uap)
2876 struct nlookupdata nd;
2877 int error;
2879 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2880 if (error == 0)
2881 error = kern_chown(&nd, uap->uid, uap->gid);
2882 nlookup_done(&nd);
2883 return (error);
2887 * fchown_args(int fd, int uid, int gid)
2889 * Set ownership given a file descriptor.
2891 /* ARGSUSED */
2893 sys_fchown(struct fchown_args *uap)
2895 struct thread *td = curthread;
2896 struct proc *p = td->td_proc;
2897 struct file *fp;
2898 int error;
2900 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2901 return (error);
2902 if (fp->f_nchandle.ncp)
2903 error = ncp_writechk(&fp->f_nchandle);
2904 if (error == 0)
2905 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2906 fdrop(fp);
2907 return (error);
2911 * fchownat(int fd, char *path, int uid, int gid, int flags)
2913 * Set ownership of file pointed to by fd/path.
2916 sys_fchownat(struct fchownat_args *uap)
2918 struct nlookupdata nd;
2919 struct file *fp;
2920 int error;
2921 int flags;
2923 if (uap->flags & ~_AT_SYMLINK_MASK)
2924 return (EINVAL);
2925 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2927 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2928 UIO_USERSPACE, flags);
2929 if (error == 0)
2930 error = kern_chown(&nd, uap->uid, uap->gid);
2931 nlookup_done_at(&nd, fp);
2932 return (error);
2936 static int
2937 getutimes(const struct timeval *tvp, struct timespec *tsp)
2939 struct timeval tv[2];
2941 if (tvp == NULL) {
2942 microtime(&tv[0]);
2943 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2944 tsp[1] = tsp[0];
2945 } else {
2946 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2947 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2949 return 0;
2952 static int
2953 setutimes(struct vnode *vp, struct vattr *vattr,
2954 const struct timespec *ts, int nullflag)
2956 struct thread *td = curthread;
2957 struct proc *p = td->td_proc;
2958 int error;
2960 VATTR_NULL(vattr);
2961 vattr->va_atime = ts[0];
2962 vattr->va_mtime = ts[1];
2963 if (nullflag)
2964 vattr->va_vaflags |= VA_UTIMES_NULL;
2965 error = VOP_SETATTR(vp, vattr, p->p_ucred);
2967 return error;
2971 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2973 struct timespec ts[2];
2974 struct vnode *vp;
2975 struct vattr vattr;
2976 int error;
2978 if ((error = getutimes(tptr, ts)) != 0)
2979 return (error);
2982 * NOTE: utimes() succeeds for the owner even if the file
2983 * is not user-writable.
2985 nd->nl_flags |= NLC_OWN | NLC_WRITE;
2987 if ((error = nlookup(nd)) != 0)
2988 return (error);
2989 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2990 return (error);
2991 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2992 return (error);
2995 * note: vget is required for any operation that might mod the vnode
2996 * so VINACTIVE is properly cleared.
2998 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
2999 error = vget(vp, LK_EXCLUSIVE);
3000 if (error == 0) {
3001 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3002 vput(vp);
3005 vrele(vp);
3006 return (error);
3010 * utimes_args(char *path, struct timeval *tptr)
3012 * Set the access and modification times of a file.
3015 sys_utimes(struct utimes_args *uap)
3017 struct timeval tv[2];
3018 struct nlookupdata nd;
3019 int error;
3021 if (uap->tptr) {
3022 error = copyin(uap->tptr, tv, sizeof(tv));
3023 if (error)
3024 return (error);
3026 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3027 if (error == 0)
3028 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3029 nlookup_done(&nd);
3030 return (error);
3034 * lutimes_args(char *path, struct timeval *tptr)
3036 * Set the access and modification times of a file.
3039 sys_lutimes(struct lutimes_args *uap)
3041 struct timeval tv[2];
3042 struct nlookupdata nd;
3043 int error;
3045 if (uap->tptr) {
3046 error = copyin(uap->tptr, tv, sizeof(tv));
3047 if (error)
3048 return (error);
3050 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3051 if (error == 0)
3052 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3053 nlookup_done(&nd);
3054 return (error);
3058 * Set utimes on a file descriptor. The creds used to open the
3059 * file are used to determine whether the operation is allowed
3060 * or not.
3063 kern_futimes(int fd, struct timeval *tptr)
3065 struct thread *td = curthread;
3066 struct proc *p = td->td_proc;
3067 struct timespec ts[2];
3068 struct file *fp;
3069 struct vnode *vp;
3070 struct vattr vattr;
3071 int error;
3073 error = getutimes(tptr, ts);
3074 if (error)
3075 return (error);
3076 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3077 return (error);
3078 if (fp->f_nchandle.ncp)
3079 error = ncp_writechk(&fp->f_nchandle);
3080 if (error == 0) {
3081 vp = fp->f_data;
3082 error = vget(vp, LK_EXCLUSIVE);
3083 if (error == 0) {
3084 error = VOP_GETATTR(vp, &vattr);
3085 if (error == 0) {
3086 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3087 fp->f_cred);
3089 if (error == 0) {
3090 error = setutimes(vp, &vattr, ts,
3091 (tptr == NULL));
3093 vput(vp);
3096 fdrop(fp);
3097 return (error);
3101 * futimes_args(int fd, struct timeval *tptr)
3103 * Set the access and modification times of a file.
3106 sys_futimes(struct futimes_args *uap)
3108 struct timeval tv[2];
3109 int error;
3111 if (uap->tptr) {
3112 error = copyin(uap->tptr, tv, sizeof(tv));
3113 if (error)
3114 return (error);
3117 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3119 return (error);
3123 kern_truncate(struct nlookupdata *nd, off_t length)
3125 struct vnode *vp;
3126 struct vattr vattr;
3127 int error;
3129 if (length < 0)
3130 return(EINVAL);
3131 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3132 if ((error = nlookup(nd)) != 0)
3133 return (error);
3134 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3135 return (error);
3136 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3137 return (error);
3138 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3139 vrele(vp);
3140 return (error);
3142 if (vp->v_type == VDIR) {
3143 error = EISDIR;
3144 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3145 VATTR_NULL(&vattr);
3146 vattr.va_size = length;
3147 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3149 vput(vp);
3150 return (error);
3154 * truncate(char *path, int pad, off_t length)
3156 * Truncate a file given its path name.
3159 sys_truncate(struct truncate_args *uap)
3161 struct nlookupdata nd;
3162 int error;
3164 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3165 if (error == 0)
3166 error = kern_truncate(&nd, uap->length);
3167 nlookup_done(&nd);
3168 return error;
3172 kern_ftruncate(int fd, off_t length)
3174 struct thread *td = curthread;
3175 struct proc *p = td->td_proc;
3176 struct vattr vattr;
3177 struct vnode *vp;
3178 struct file *fp;
3179 int error;
3181 if (length < 0)
3182 return(EINVAL);
3183 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3184 return (error);
3185 if (fp->f_nchandle.ncp) {
3186 error = ncp_writechk(&fp->f_nchandle);
3187 if (error)
3188 goto done;
3190 if ((fp->f_flag & FWRITE) == 0) {
3191 error = EINVAL;
3192 goto done;
3194 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3195 error = EINVAL;
3196 goto done;
3198 vp = (struct vnode *)fp->f_data;
3199 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3200 if (vp->v_type == VDIR) {
3201 error = EISDIR;
3202 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3203 VATTR_NULL(&vattr);
3204 vattr.va_size = length;
3205 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3207 vn_unlock(vp);
3208 done:
3209 fdrop(fp);
3210 return (error);
3214 * ftruncate_args(int fd, int pad, off_t length)
3216 * Truncate a file given a file descriptor.
3219 sys_ftruncate(struct ftruncate_args *uap)
3221 int error;
3223 error = kern_ftruncate(uap->fd, uap->length);
3225 return (error);
3229 * fsync(int fd)
3231 * Sync an open file.
3233 /* ARGSUSED */
3235 sys_fsync(struct fsync_args *uap)
3237 struct thread *td = curthread;
3238 struct proc *p = td->td_proc;
3239 struct vnode *vp;
3240 struct file *fp;
3241 vm_object_t obj;
3242 int error;
3244 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3245 return (error);
3246 vp = (struct vnode *)fp->f_data;
3247 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3248 if ((obj = vp->v_object) != NULL)
3249 vm_object_page_clean(obj, 0, 0, 0);
3250 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3251 error = buf_fsync(vp);
3252 vn_unlock(vp);
3253 fdrop(fp);
3254 return (error);
3258 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3260 struct nchandle fnchd;
3261 struct nchandle tnchd;
3262 struct namecache *ncp;
3263 struct vnode *fdvp;
3264 struct vnode *tdvp;
3265 struct mount *mp;
3266 int error;
3268 bwillinode(1);
3269 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3270 if ((error = nlookup(fromnd)) != 0)
3271 return (error);
3272 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3273 return (ENOENT);
3274 fnchd.mount = fromnd->nl_nch.mount;
3275 cache_hold(&fnchd);
3278 * unlock the source nch so we can lookup the target nch without
3279 * deadlocking. The target may or may not exist so we do not check
3280 * for a target vp like kern_mkdir() and other creation functions do.
3282 * The source and target directories are ref'd and rechecked after
3283 * everything is relocked to determine if the source or target file
3284 * has been renamed.
3286 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3287 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3288 cache_unlock(&fromnd->nl_nch);
3290 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3291 if ((error = nlookup(tond)) != 0) {
3292 cache_drop(&fnchd);
3293 return (error);
3295 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3296 cache_drop(&fnchd);
3297 return (ENOENT);
3299 tnchd.mount = tond->nl_nch.mount;
3300 cache_hold(&tnchd);
3303 * If the source and target are the same there is nothing to do
3305 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3306 cache_drop(&fnchd);
3307 cache_drop(&tnchd);
3308 return (0);
3312 * Mount points cannot be renamed or overwritten
3314 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3315 NCF_ISMOUNTPT
3317 cache_drop(&fnchd);
3318 cache_drop(&tnchd);
3319 return (EINVAL);
3323 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3324 * may have become invalid while it was unlocked, nc_vp and nc_mount
3325 * could be NULL.
3327 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3328 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3329 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3330 cache_lock(&fromnd->nl_nch);
3331 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3332 } else {
3333 cache_unlock(&tond->nl_nch);
3334 cache_lock(&fromnd->nl_nch);
3335 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3336 cache_lock(&tond->nl_nch);
3337 cache_resolve(&tond->nl_nch, tond->nl_cred);
3339 fromnd->nl_flags |= NLC_NCPISLOCKED;
3342 * make sure the parent directories linkages are the same
3344 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3345 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3346 cache_drop(&fnchd);
3347 cache_drop(&tnchd);
3348 return (ENOENT);
3352 * Both the source and target must be within the same filesystem and
3353 * in the same filesystem as their parent directories within the
3354 * namecache topology.
3356 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3358 mp = fnchd.mount;
3359 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3360 mp != tond->nl_nch.mount) {
3361 cache_drop(&fnchd);
3362 cache_drop(&tnchd);
3363 return (EXDEV);
3367 * Make sure the mount point is writable
3369 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3370 cache_drop(&fnchd);
3371 cache_drop(&tnchd);
3372 return (error);
3376 * If the target exists and either the source or target is a directory,
3377 * then both must be directories.
3379 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3380 * have become NULL.
3382 if (tond->nl_nch.ncp->nc_vp) {
3383 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3384 error = ENOENT;
3385 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3386 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3387 error = ENOTDIR;
3388 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3389 error = EISDIR;
3394 * You cannot rename a source into itself or a subdirectory of itself.
3395 * We check this by travsersing the target directory upwards looking
3396 * for a match against the source.
3398 if (error == 0) {
3399 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3400 if (fromnd->nl_nch.ncp == ncp) {
3401 error = EINVAL;
3402 break;
3407 cache_drop(&fnchd);
3408 cache_drop(&tnchd);
3411 * Even though the namespaces are different, they may still represent
3412 * hardlinks to the same file. The filesystem might have a hard time
3413 * with this so we issue a NREMOVE of the source instead of a NRENAME
3414 * when we detect the situation.
3416 if (error == 0) {
3417 fdvp = fromnd->nl_dvp;
3418 tdvp = tond->nl_dvp;
3419 if (fdvp == NULL || tdvp == NULL) {
3420 error = EPERM;
3421 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3422 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3423 fromnd->nl_cred);
3424 } else {
3425 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3426 fdvp, tdvp, tond->nl_cred);
3429 return (error);
3433 * rename_args(char *from, char *to)
3435 * Rename files. Source and destination must either both be directories,
3436 * or both not be directories. If target is a directory, it must be empty.
3439 sys_rename(struct rename_args *uap)
3441 struct nlookupdata fromnd, tond;
3442 int error;
3444 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3445 if (error == 0) {
3446 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3447 if (error == 0)
3448 error = kern_rename(&fromnd, &tond);
3449 nlookup_done(&tond);
3451 nlookup_done(&fromnd);
3452 return (error);
3456 kern_mkdir(struct nlookupdata *nd, int mode)
3458 struct thread *td = curthread;
3459 struct proc *p = td->td_proc;
3460 struct vnode *vp;
3461 struct vattr vattr;
3462 int error;
3464 bwillinode(1);
3465 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3466 if ((error = nlookup(nd)) != 0)
3467 return (error);
3469 if (nd->nl_nch.ncp->nc_vp)
3470 return (EEXIST);
3471 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3472 return (error);
3473 VATTR_NULL(&vattr);
3474 vattr.va_type = VDIR;
3475 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3477 vp = NULL;
3478 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3479 if (error == 0)
3480 vput(vp);
3481 return (error);
3485 * mkdir_args(char *path, int mode)
3487 * Make a directory file.
3489 /* ARGSUSED */
3491 sys_mkdir(struct mkdir_args *uap)
3493 struct nlookupdata nd;
3494 int error;
3496 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3497 if (error == 0)
3498 error = kern_mkdir(&nd, uap->mode);
3499 nlookup_done(&nd);
3500 return (error);
3504 kern_rmdir(struct nlookupdata *nd)
3506 int error;
3508 bwillinode(1);
3509 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3510 if ((error = nlookup(nd)) != 0)
3511 return (error);
3514 * Do not allow directories representing mount points to be
3515 * deleted, even if empty. Check write perms on mount point
3516 * in case the vnode is aliased (aka nullfs).
3518 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3519 return (EINVAL);
3520 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3521 return (error);
3522 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3523 return (error);
3527 * rmdir_args(char *path)
3529 * Remove a directory file.
3531 /* ARGSUSED */
3533 sys_rmdir(struct rmdir_args *uap)
3535 struct nlookupdata nd;
3536 int error;
3538 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3539 if (error == 0)
3540 error = kern_rmdir(&nd);
3541 nlookup_done(&nd);
3542 return (error);
3546 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3547 enum uio_seg direction)
3549 struct thread *td = curthread;
3550 struct proc *p = td->td_proc;
3551 struct vnode *vp;
3552 struct file *fp;
3553 struct uio auio;
3554 struct iovec aiov;
3555 off_t loff;
3556 int error, eofflag;
3558 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3559 return (error);
3560 if ((fp->f_flag & FREAD) == 0) {
3561 error = EBADF;
3562 goto done;
3564 vp = (struct vnode *)fp->f_data;
3565 unionread:
3566 if (vp->v_type != VDIR) {
3567 error = EINVAL;
3568 goto done;
3570 aiov.iov_base = buf;
3571 aiov.iov_len = count;
3572 auio.uio_iov = &aiov;
3573 auio.uio_iovcnt = 1;
3574 auio.uio_rw = UIO_READ;
3575 auio.uio_segflg = direction;
3576 auio.uio_td = td;
3577 auio.uio_resid = count;
3578 loff = auio.uio_offset = fp->f_offset;
3579 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3580 fp->f_offset = auio.uio_offset;
3581 if (error)
3582 goto done;
3583 if (count == auio.uio_resid) {
3584 if (union_dircheckp) {
3585 error = union_dircheckp(td, &vp, fp);
3586 if (error == -1)
3587 goto unionread;
3588 if (error)
3589 goto done;
3591 #if 0
3592 if ((vp->v_flag & VROOT) &&
3593 (vp->v_mount->mnt_flag & MNT_UNION)) {
3594 struct vnode *tvp = vp;
3595 vp = vp->v_mount->mnt_vnodecovered;
3596 vref(vp);
3597 fp->f_data = vp;
3598 fp->f_offset = 0;
3599 vrele(tvp);
3600 goto unionread;
3602 #endif
3606 * WARNING! *basep may not be wide enough to accomodate the
3607 * seek offset. XXX should we hack this to return the upper 32 bits
3608 * for offsets greater then 4G?
3610 if (basep) {
3611 *basep = (long)loff;
3613 *res = count - auio.uio_resid;
3614 done:
3615 fdrop(fp);
3616 return (error);
3620 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3622 * Read a block of directory entries in a file system independent format.
3625 sys_getdirentries(struct getdirentries_args *uap)
3627 long base;
3628 int error;
3630 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3631 &uap->sysmsg_result, UIO_USERSPACE);
3633 if (error == 0 && uap->basep)
3634 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3635 return (error);
3639 * getdents_args(int fd, char *buf, size_t count)
3642 sys_getdents(struct getdents_args *uap)
3644 int error;
3646 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3647 &uap->sysmsg_result, UIO_USERSPACE);
3649 return (error);
3653 * umask(int newmask)
3655 * Set the mode mask for creation of filesystem nodes.
3657 * MP SAFE
3660 sys_umask(struct umask_args *uap)
3662 struct thread *td = curthread;
3663 struct proc *p = td->td_proc;
3664 struct filedesc *fdp;
3666 fdp = p->p_fd;
3667 uap->sysmsg_result = fdp->fd_cmask;
3668 fdp->fd_cmask = uap->newmask & ALLPERMS;
3669 return (0);
3673 * revoke(char *path)
3675 * Void all references to file by ripping underlying filesystem
3676 * away from vnode.
3678 /* ARGSUSED */
3680 sys_revoke(struct revoke_args *uap)
3682 struct nlookupdata nd;
3683 struct vattr vattr;
3684 struct vnode *vp;
3685 struct ucred *cred;
3686 int error;
3688 vp = NULL;
3689 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3690 if (error == 0)
3691 error = nlookup(&nd);
3692 if (error == 0)
3693 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3694 cred = crhold(nd.nl_cred);
3695 nlookup_done(&nd);
3696 if (error == 0) {
3697 if (error == 0)
3698 error = VOP_GETATTR(vp, &vattr);
3699 if (error == 0 && cred->cr_uid != vattr.va_uid)
3700 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3701 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3702 if (vcount(vp) > 0)
3703 error = vrevoke(vp, cred);
3704 } else if (error == 0) {
3705 error = vrevoke(vp, cred);
3707 vrele(vp);
3709 if (cred)
3710 crfree(cred);
3711 return (error);
3715 * getfh_args(char *fname, fhandle_t *fhp)
3717 * Get (NFS) file handle
3719 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3720 * mount. This allows nullfs mounts to be explicitly exported.
3722 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3724 * nullfs mounts of subdirectories are not safe. That is, it will
3725 * work, but you do not really have protection against access to
3726 * the related parent directories.
3729 sys_getfh(struct getfh_args *uap)
3731 struct thread *td = curthread;
3732 struct nlookupdata nd;
3733 fhandle_t fh;
3734 struct vnode *vp;
3735 struct mount *mp;
3736 int error;
3739 * Must be super user
3741 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3742 return (error);
3744 vp = NULL;
3745 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3746 if (error == 0)
3747 error = nlookup(&nd);
3748 if (error == 0)
3749 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3750 mp = nd.nl_nch.mount;
3751 nlookup_done(&nd);
3752 if (error == 0) {
3753 bzero(&fh, sizeof(fh));
3754 fh.fh_fsid = mp->mnt_stat.f_fsid;
3755 error = VFS_VPTOFH(vp, &fh.fh_fid);
3756 vput(vp);
3757 if (error == 0)
3758 error = copyout(&fh, uap->fhp, sizeof(fh));
3760 return (error);
3764 * fhopen_args(const struct fhandle *u_fhp, int flags)
3766 * syscall for the rpc.lockd to use to translate a NFS file handle into
3767 * an open descriptor.
3769 * warning: do not remove the priv_check() call or this becomes one giant
3770 * security hole.
3773 sys_fhopen(struct fhopen_args *uap)
3775 struct thread *td = curthread;
3776 struct proc *p = td->td_proc;
3777 struct mount *mp;
3778 struct vnode *vp;
3779 struct fhandle fhp;
3780 struct vattr vat;
3781 struct vattr *vap = &vat;
3782 struct flock lf;
3783 int fmode, mode, error, type;
3784 struct file *nfp;
3785 struct file *fp;
3786 int indx;
3789 * Must be super user
3791 error = priv_check(td, PRIV_ROOT);
3792 if (error)
3793 return (error);
3795 fmode = FFLAGS(uap->flags);
3796 /* why not allow a non-read/write open for our lockd? */
3797 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3798 return (EINVAL);
3799 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3800 if (error)
3801 return(error);
3802 /* find the mount point */
3803 mp = vfs_getvfs(&fhp.fh_fsid);
3804 if (mp == NULL)
3805 return (ESTALE);
3806 /* now give me my vnode, it gets returned to me locked */
3807 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3808 if (error)
3809 return (error);
3811 * from now on we have to make sure not
3812 * to forget about the vnode
3813 * any error that causes an abort must vput(vp)
3814 * just set error = err and 'goto bad;'.
3818 * from vn_open
3820 if (vp->v_type == VLNK) {
3821 error = EMLINK;
3822 goto bad;
3824 if (vp->v_type == VSOCK) {
3825 error = EOPNOTSUPP;
3826 goto bad;
3828 mode = 0;
3829 if (fmode & (FWRITE | O_TRUNC)) {
3830 if (vp->v_type == VDIR) {
3831 error = EISDIR;
3832 goto bad;
3834 error = vn_writechk(vp, NULL);
3835 if (error)
3836 goto bad;
3837 mode |= VWRITE;
3839 if (fmode & FREAD)
3840 mode |= VREAD;
3841 if (mode) {
3842 error = VOP_ACCESS(vp, mode, p->p_ucred);
3843 if (error)
3844 goto bad;
3846 if (fmode & O_TRUNC) {
3847 vn_unlock(vp); /* XXX */
3848 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3849 VATTR_NULL(vap);
3850 vap->va_size = 0;
3851 error = VOP_SETATTR(vp, vap, p->p_ucred);
3852 if (error)
3853 goto bad;
3857 * VOP_OPEN needs the file pointer so it can potentially override
3858 * it.
3860 * WARNING! no f_nchandle will be associated when fhopen()ing a
3861 * directory. XXX
3863 if ((error = falloc(p, &nfp, &indx)) != 0)
3864 goto bad;
3865 fp = nfp;
3867 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3868 if (error) {
3870 * setting f_ops this way prevents VOP_CLOSE from being
3871 * called or fdrop() releasing the vp from v_data. Since
3872 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3874 fp->f_ops = &badfileops;
3875 fp->f_data = NULL;
3876 goto bad_drop;
3880 * The fp is given its own reference, we still have our ref and lock.
3882 * Assert that all regular files must be created with a VM object.
3884 if (vp->v_type == VREG && vp->v_object == NULL) {
3885 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3886 goto bad_drop;
3890 * The open was successful. Handle any locking requirements.
3892 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3893 lf.l_whence = SEEK_SET;
3894 lf.l_start = 0;
3895 lf.l_len = 0;
3896 if (fmode & O_EXLOCK)
3897 lf.l_type = F_WRLCK;
3898 else
3899 lf.l_type = F_RDLCK;
3900 if (fmode & FNONBLOCK)
3901 type = 0;
3902 else
3903 type = F_WAIT;
3904 vn_unlock(vp);
3905 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3907 * release our private reference.
3909 fsetfd(p, NULL, indx);
3910 fdrop(fp);
3911 vrele(vp);
3912 return (error);
3914 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3915 fp->f_flag |= FHASLOCK;
3919 * Clean up. Associate the file pointer with the previously
3920 * reserved descriptor and return it.
3922 vput(vp);
3923 fsetfd(p, fp, indx);
3924 fdrop(fp);
3925 uap->sysmsg_result = indx;
3926 return (0);
3928 bad_drop:
3929 fsetfd(p, NULL, indx);
3930 fdrop(fp);
3931 bad:
3932 vput(vp);
3933 return (error);
3937 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3940 sys_fhstat(struct fhstat_args *uap)
3942 struct thread *td = curthread;
3943 struct stat sb;
3944 fhandle_t fh;
3945 struct mount *mp;
3946 struct vnode *vp;
3947 int error;
3950 * Must be super user
3952 error = priv_check(td, PRIV_ROOT);
3953 if (error)
3954 return (error);
3956 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3957 if (error)
3958 return (error);
3960 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3961 return (ESTALE);
3962 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3963 return (error);
3964 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3965 vput(vp);
3966 if (error)
3967 return (error);
3968 error = copyout(&sb, uap->sb, sizeof(sb));
3969 return (error);
3973 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3976 sys_fhstatfs(struct fhstatfs_args *uap)
3978 struct thread *td = curthread;
3979 struct proc *p = td->td_proc;
3980 struct statfs *sp;
3981 struct mount *mp;
3982 struct vnode *vp;
3983 struct statfs sb;
3984 char *fullpath, *freepath;
3985 fhandle_t fh;
3986 int error;
3989 * Must be super user
3991 if ((error = priv_check(td, PRIV_ROOT)))
3992 return (error);
3994 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3995 return (error);
3997 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3998 return (ESTALE);
4000 if (p != NULL && !chroot_visible_mnt(mp, p))
4001 return (ESTALE);
4003 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4004 return (error);
4005 mp = vp->v_mount;
4006 sp = &mp->mnt_stat;
4007 vput(vp);
4008 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4009 return (error);
4011 error = mount_path(p, mp, &fullpath, &freepath);
4012 if (error)
4013 return(error);
4014 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4015 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4016 kfree(freepath, M_TEMP);
4018 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4019 if (priv_check(td, PRIV_ROOT)) {
4020 bcopy(sp, &sb, sizeof(sb));
4021 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4022 sp = &sb;
4024 return (copyout(sp, uap->buf, sizeof(*sp)));
4028 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4031 sys_fhstatvfs(struct fhstatvfs_args *uap)
4033 struct thread *td = curthread;
4034 struct proc *p = td->td_proc;
4035 struct statvfs *sp;
4036 struct mount *mp;
4037 struct vnode *vp;
4038 fhandle_t fh;
4039 int error;
4042 * Must be super user
4044 if ((error = priv_check(td, PRIV_ROOT)))
4045 return (error);
4047 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4048 return (error);
4050 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4051 return (ESTALE);
4053 if (p != NULL && !chroot_visible_mnt(mp, p))
4054 return (ESTALE);
4056 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4057 return (error);
4058 mp = vp->v_mount;
4059 sp = &mp->mnt_vstat;
4060 vput(vp);
4061 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4062 return (error);
4064 sp->f_flag = 0;
4065 if (mp->mnt_flag & MNT_RDONLY)
4066 sp->f_flag |= ST_RDONLY;
4067 if (mp->mnt_flag & MNT_NOSUID)
4068 sp->f_flag |= ST_NOSUID;
4070 return (copyout(sp, uap->buf, sizeof(*sp)));
4075 * Syscall to push extended attribute configuration information into the
4076 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4077 * a command (int cmd), and attribute name and misc data. For now, the
4078 * attribute name is left in userspace for consumption by the VFS_op.
4079 * It will probably be changed to be copied into sysspace by the
4080 * syscall in the future, once issues with various consumers of the
4081 * attribute code have raised their hands.
4083 * Currently this is used only by UFS Extended Attributes.
4086 sys_extattrctl(struct extattrctl_args *uap)
4088 struct nlookupdata nd;
4089 struct mount *mp;
4090 struct vnode *vp;
4091 int error;
4093 vp = NULL;
4094 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4095 if (error == 0)
4096 error = nlookup(&nd);
4097 if (error == 0) {
4098 mp = nd.nl_nch.mount;
4099 error = VFS_EXTATTRCTL(mp, uap->cmd,
4100 uap->attrname, uap->arg,
4101 nd.nl_cred);
4103 nlookup_done(&nd);
4104 return (error);
4108 * Syscall to set a named extended attribute on a file or directory.
4109 * Accepts attribute name, and a uio structure pointing to the data to set.
4110 * The uio is consumed in the style of writev(). The real work happens
4111 * in VOP_SETEXTATTR().
4114 sys_extattr_set_file(struct extattr_set_file_args *uap)
4116 char attrname[EXTATTR_MAXNAMELEN];
4117 struct iovec aiov[UIO_SMALLIOV];
4118 struct iovec *needfree;
4119 struct nlookupdata nd;
4120 struct iovec *iov;
4121 struct vnode *vp;
4122 struct uio auio;
4123 u_int iovlen;
4124 u_int cnt;
4125 int error;
4126 int i;
4128 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4129 if (error)
4130 return (error);
4132 vp = NULL;
4133 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4134 if (error == 0)
4135 error = nlookup(&nd);
4136 if (error == 0)
4137 error = ncp_writechk(&nd.nl_nch);
4138 if (error == 0)
4139 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4140 if (error) {
4141 nlookup_done(&nd);
4142 return (error);
4145 needfree = NULL;
4146 iovlen = uap->iovcnt * sizeof(struct iovec);
4147 if (uap->iovcnt > UIO_SMALLIOV) {
4148 if (uap->iovcnt > UIO_MAXIOV) {
4149 error = EINVAL;
4150 goto done;
4152 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4153 needfree = iov;
4154 } else {
4155 iov = aiov;
4157 auio.uio_iov = iov;
4158 auio.uio_iovcnt = uap->iovcnt;
4159 auio.uio_rw = UIO_WRITE;
4160 auio.uio_segflg = UIO_USERSPACE;
4161 auio.uio_td = nd.nl_td;
4162 auio.uio_offset = 0;
4163 if ((error = copyin(uap->iovp, iov, iovlen)))
4164 goto done;
4165 auio.uio_resid = 0;
4166 for (i = 0; i < uap->iovcnt; i++) {
4167 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4168 error = EINVAL;
4169 goto done;
4171 auio.uio_resid += iov->iov_len;
4172 iov++;
4174 cnt = auio.uio_resid;
4175 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4176 cnt -= auio.uio_resid;
4177 uap->sysmsg_result = cnt;
4178 done:
4179 vput(vp);
4180 nlookup_done(&nd);
4181 if (needfree)
4182 FREE(needfree, M_IOV);
4183 return (error);
4187 * Syscall to get a named extended attribute on a file or directory.
4188 * Accepts attribute name, and a uio structure pointing to a buffer for the
4189 * data. The uio is consumed in the style of readv(). The real work
4190 * happens in VOP_GETEXTATTR();
4193 sys_extattr_get_file(struct extattr_get_file_args *uap)
4195 char attrname[EXTATTR_MAXNAMELEN];
4196 struct iovec aiov[UIO_SMALLIOV];
4197 struct iovec *needfree;
4198 struct nlookupdata nd;
4199 struct iovec *iov;
4200 struct vnode *vp;
4201 struct uio auio;
4202 u_int iovlen;
4203 u_int cnt;
4204 int error;
4205 int i;
4207 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4208 if (error)
4209 return (error);
4211 vp = NULL;
4212 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4213 if (error == 0)
4214 error = nlookup(&nd);
4215 if (error == 0)
4216 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4217 if (error) {
4218 nlookup_done(&nd);
4219 return (error);
4222 iovlen = uap->iovcnt * sizeof (struct iovec);
4223 needfree = NULL;
4224 if (uap->iovcnt > UIO_SMALLIOV) {
4225 if (uap->iovcnt > UIO_MAXIOV) {
4226 error = EINVAL;
4227 goto done;
4229 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4230 needfree = iov;
4231 } else {
4232 iov = aiov;
4234 auio.uio_iov = iov;
4235 auio.uio_iovcnt = uap->iovcnt;
4236 auio.uio_rw = UIO_READ;
4237 auio.uio_segflg = UIO_USERSPACE;
4238 auio.uio_td = nd.nl_td;
4239 auio.uio_offset = 0;
4240 if ((error = copyin(uap->iovp, iov, iovlen)))
4241 goto done;
4242 auio.uio_resid = 0;
4243 for (i = 0; i < uap->iovcnt; i++) {
4244 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4245 error = EINVAL;
4246 goto done;
4248 auio.uio_resid += iov->iov_len;
4249 iov++;
4251 cnt = auio.uio_resid;
4252 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4253 cnt -= auio.uio_resid;
4254 uap->sysmsg_result = cnt;
4255 done:
4256 vput(vp);
4257 nlookup_done(&nd);
4258 if (needfree)
4259 FREE(needfree, M_IOV);
4260 return(error);
4264 * Syscall to delete a named extended attribute from a file or directory.
4265 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4268 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4270 char attrname[EXTATTR_MAXNAMELEN];
4271 struct nlookupdata nd;
4272 struct vnode *vp;
4273 int error;
4275 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4276 if (error)
4277 return(error);
4279 vp = NULL;
4280 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4281 if (error == 0)
4282 error = nlookup(&nd);
4283 if (error == 0)
4284 error = ncp_writechk(&nd.nl_nch);
4285 if (error == 0)
4286 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4287 if (error) {
4288 nlookup_done(&nd);
4289 return (error);
4292 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4293 vput(vp);
4294 nlookup_done(&nd);
4295 return(error);
4299 * Determine if the mount is visible to the process.
4301 static int
4302 chroot_visible_mnt(struct mount *mp, struct proc *p)
4304 struct nchandle nch;
4307 * Traverse from the mount point upwards. If we hit the process
4308 * root then the mount point is visible to the process.
4310 nch = mp->mnt_ncmountpt;
4311 while (nch.ncp) {
4312 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4313 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4314 return(1);
4316 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4317 nch = nch.mount->mnt_ncmounton;
4318 } else {
4319 nch.ncp = nch.ncp->nc_parent;
4324 * If the mount point is not visible to the process, but the
4325 * process root is in a subdirectory of the mount, return
4326 * TRUE anyway.
4328 if (p->p_fd->fd_nrdir.mount == mp)
4329 return(1);
4331 return(0);