Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / sys / kern / vfs_syscalls.c
blob5fcc497a684a7506c4dc9adb4eaeda1bcdf57c54
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);
2242 * unlinkat_args(int fd, char *path, int flags)
2244 * Delete the file or directory entry pointed to by fd/path.
2247 sys_unlinkat(struct unlinkat_args *uap)
2249 struct nlookupdata nd;
2250 struct file *fp;
2251 int error;
2253 if (uap->flags & ~AT_REMOVEDIR)
2254 return (EINVAL);
2256 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2257 if (error == 0) {
2258 if (uap->flags & AT_REMOVEDIR)
2259 error = kern_rmdir(&nd);
2260 else
2261 error = kern_unlink(&nd);
2263 nlookup_done_at(&nd, fp);
2264 return (error);
2268 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2270 struct thread *td = curthread;
2271 struct proc *p = td->td_proc;
2272 struct file *fp;
2273 struct vnode *vp;
2274 struct vattr vattr;
2275 off_t new_offset;
2276 int error;
2278 fp = holdfp(p->p_fd, fd, -1);
2279 if (fp == NULL)
2280 return (EBADF);
2281 if (fp->f_type != DTYPE_VNODE) {
2282 error = ESPIPE;
2283 goto done;
2285 vp = (struct vnode *)fp->f_data;
2287 switch (whence) {
2288 case L_INCR:
2289 new_offset = fp->f_offset + offset;
2290 error = 0;
2291 break;
2292 case L_XTND:
2293 error = VOP_GETATTR(vp, &vattr);
2294 new_offset = offset + vattr.va_size;
2295 break;
2296 case L_SET:
2297 new_offset = offset;
2298 error = 0;
2299 break;
2300 default:
2301 new_offset = 0;
2302 error = EINVAL;
2303 break;
2307 * Validate the seek position. Negative offsets are not allowed
2308 * for regular files or directories.
2310 * Normally we would also not want to allow negative offsets for
2311 * character and block-special devices. However kvm addresses
2312 * on 64 bit architectures might appear to be negative and must
2313 * be allowed.
2315 if (error == 0) {
2316 if (new_offset < 0 &&
2317 (vp->v_type == VREG || vp->v_type == VDIR)) {
2318 error = EINVAL;
2319 } else {
2320 fp->f_offset = new_offset;
2323 *res = fp->f_offset;
2324 done:
2325 fdrop(fp);
2326 return (error);
2330 * lseek_args(int fd, int pad, off_t offset, int whence)
2332 * Reposition read/write file offset.
2335 sys_lseek(struct lseek_args *uap)
2337 int error;
2339 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2340 &uap->sysmsg_offset);
2342 return (error);
2346 kern_access(struct nlookupdata *nd, int aflags)
2348 struct vnode *vp;
2349 int error, flags;
2351 if ((error = nlookup(nd)) != 0)
2352 return (error);
2353 retry:
2354 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2355 if (error)
2356 return (error);
2358 /* Flags == 0 means only check for existence. */
2359 if (aflags) {
2360 flags = 0;
2361 if (aflags & R_OK)
2362 flags |= VREAD;
2363 if (aflags & W_OK)
2364 flags |= VWRITE;
2365 if (aflags & X_OK)
2366 flags |= VEXEC;
2367 if ((flags & VWRITE) == 0 ||
2368 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2369 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2372 * If the file handle is stale we have to re-resolve the
2373 * entry. This is a hack at the moment.
2375 if (error == ESTALE) {
2376 vput(vp);
2377 cache_setunresolved(&nd->nl_nch);
2378 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2379 if (error == 0) {
2380 vp = NULL;
2381 goto retry;
2383 return(error);
2386 vput(vp);
2387 return (error);
2391 * access_args(char *path, int flags)
2393 * Check access permissions.
2396 sys_access(struct access_args *uap)
2398 struct nlookupdata nd;
2399 int error;
2401 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2402 if (error == 0)
2403 error = kern_access(&nd, uap->flags);
2404 nlookup_done(&nd);
2405 return (error);
2409 kern_stat(struct nlookupdata *nd, struct stat *st)
2411 int error;
2412 struct vnode *vp;
2413 thread_t td;
2415 if ((error = nlookup(nd)) != 0)
2416 return (error);
2417 again:
2418 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2419 return (ENOENT);
2421 td = curthread;
2422 if ((error = vget(vp, LK_SHARED)) != 0)
2423 return (error);
2424 error = vn_stat(vp, st, nd->nl_cred);
2427 * If the file handle is stale we have to re-resolve the entry. This
2428 * is a hack at the moment.
2430 if (error == ESTALE) {
2431 vput(vp);
2432 cache_setunresolved(&nd->nl_nch);
2433 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2434 if (error == 0)
2435 goto again;
2436 } else {
2437 vput(vp);
2439 return (error);
2443 * stat_args(char *path, struct stat *ub)
2445 * Get file status; this version follows links.
2448 sys_stat(struct stat_args *uap)
2450 struct nlookupdata nd;
2451 struct stat st;
2452 int error;
2454 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2455 if (error == 0) {
2456 error = kern_stat(&nd, &st);
2457 if (error == 0)
2458 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2460 nlookup_done(&nd);
2461 return (error);
2465 * lstat_args(char *path, struct stat *ub)
2467 * Get file status; this version does not follow links.
2470 sys_lstat(struct lstat_args *uap)
2472 struct nlookupdata nd;
2473 struct stat st;
2474 int error;
2476 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2477 if (error == 0) {
2478 error = kern_stat(&nd, &st);
2479 if (error == 0)
2480 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2482 nlookup_done(&nd);
2483 return (error);
2487 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2489 * Get status of file pointed to by fd/path.
2492 sys_fstatat(struct fstatat_args *uap)
2494 struct nlookupdata nd;
2495 struct stat st;
2496 int error;
2497 int flags;
2498 struct file *fp;
2500 if (uap->flags & ~_AT_SYMLINK_MASK)
2501 return (EINVAL);
2503 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2505 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2506 UIO_USERSPACE, flags);
2507 if (error == 0) {
2508 error = kern_stat(&nd, &st);
2509 if (error == 0)
2510 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2512 nlookup_done_at(&nd, fp);
2513 return (error);
2517 * pathconf_Args(char *path, int name)
2519 * Get configurable pathname variables.
2521 /* ARGSUSED */
2523 sys_pathconf(struct pathconf_args *uap)
2525 struct nlookupdata nd;
2526 struct vnode *vp;
2527 int error;
2529 vp = NULL;
2530 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2531 if (error == 0)
2532 error = nlookup(&nd);
2533 if (error == 0)
2534 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2535 nlookup_done(&nd);
2536 if (error == 0) {
2537 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2538 vput(vp);
2540 return (error);
2544 * XXX: daver
2545 * kern_readlink isn't properly split yet. There is a copyin burried
2546 * in VOP_READLINK().
2549 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2551 struct thread *td = curthread;
2552 struct proc *p = td->td_proc;
2553 struct vnode *vp;
2554 struct iovec aiov;
2555 struct uio auio;
2556 int error;
2558 if ((error = nlookup(nd)) != 0)
2559 return (error);
2560 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2561 if (error)
2562 return (error);
2563 if (vp->v_type != VLNK) {
2564 error = EINVAL;
2565 } else {
2566 aiov.iov_base = buf;
2567 aiov.iov_len = count;
2568 auio.uio_iov = &aiov;
2569 auio.uio_iovcnt = 1;
2570 auio.uio_offset = 0;
2571 auio.uio_rw = UIO_READ;
2572 auio.uio_segflg = UIO_USERSPACE;
2573 auio.uio_td = td;
2574 auio.uio_resid = count;
2575 error = VOP_READLINK(vp, &auio, p->p_ucred);
2577 vput(vp);
2578 *res = count - auio.uio_resid;
2579 return (error);
2583 * readlink_args(char *path, char *buf, int count)
2585 * Return target name of a symbolic link.
2588 sys_readlink(struct readlink_args *uap)
2590 struct nlookupdata nd;
2591 int error;
2593 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2594 if (error == 0) {
2595 error = kern_readlink(&nd, uap->buf, uap->count,
2596 &uap->sysmsg_result);
2598 nlookup_done(&nd);
2599 return (error);
2602 static int
2603 setfflags(struct vnode *vp, int flags)
2605 struct thread *td = curthread;
2606 struct proc *p = td->td_proc;
2607 int error;
2608 struct vattr vattr;
2611 * Prevent non-root users from setting flags on devices. When
2612 * a device is reused, users can retain ownership of the device
2613 * if they are allowed to set flags and programs assume that
2614 * chown can't fail when done as root.
2616 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2617 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2618 return (error);
2621 * note: vget is required for any operation that might mod the vnode
2622 * so VINACTIVE is properly cleared.
2624 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2625 VATTR_NULL(&vattr);
2626 vattr.va_flags = flags;
2627 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2628 vput(vp);
2630 return (error);
2634 * chflags(char *path, int flags)
2636 * Change flags of a file given a path name.
2638 /* ARGSUSED */
2640 sys_chflags(struct chflags_args *uap)
2642 struct nlookupdata nd;
2643 struct vnode *vp;
2644 int error;
2646 vp = NULL;
2647 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2648 if (error == 0)
2649 error = nlookup(&nd);
2650 if (error == 0)
2651 error = ncp_writechk(&nd.nl_nch);
2652 if (error == 0)
2653 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2654 nlookup_done(&nd);
2655 if (error == 0) {
2656 error = setfflags(vp, uap->flags);
2657 vrele(vp);
2659 return (error);
2663 * lchflags(char *path, int flags)
2665 * Change flags of a file given a path name, but don't follow symlinks.
2667 /* ARGSUSED */
2669 sys_lchflags(struct lchflags_args *uap)
2671 struct nlookupdata nd;
2672 struct vnode *vp;
2673 int error;
2675 vp = NULL;
2676 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2677 if (error == 0)
2678 error = nlookup(&nd);
2679 if (error == 0)
2680 error = ncp_writechk(&nd.nl_nch);
2681 if (error == 0)
2682 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2683 nlookup_done(&nd);
2684 if (error == 0) {
2685 error = setfflags(vp, uap->flags);
2686 vrele(vp);
2688 return (error);
2692 * fchflags_args(int fd, int flags)
2694 * Change flags of a file given a file descriptor.
2696 /* ARGSUSED */
2698 sys_fchflags(struct fchflags_args *uap)
2700 struct thread *td = curthread;
2701 struct proc *p = td->td_proc;
2702 struct file *fp;
2703 int error;
2705 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2706 return (error);
2707 if (fp->f_nchandle.ncp)
2708 error = ncp_writechk(&fp->f_nchandle);
2709 if (error == 0)
2710 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2711 fdrop(fp);
2712 return (error);
2715 static int
2716 setfmode(struct vnode *vp, int mode)
2718 struct thread *td = curthread;
2719 struct proc *p = td->td_proc;
2720 int error;
2721 struct vattr vattr;
2724 * note: vget is required for any operation that might mod the vnode
2725 * so VINACTIVE is properly cleared.
2727 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2728 VATTR_NULL(&vattr);
2729 vattr.va_mode = mode & ALLPERMS;
2730 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2731 vput(vp);
2733 return error;
2737 kern_chmod(struct nlookupdata *nd, int mode)
2739 struct vnode *vp;
2740 int error;
2742 if ((error = nlookup(nd)) != 0)
2743 return (error);
2744 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2745 return (error);
2746 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2747 error = setfmode(vp, mode);
2748 vrele(vp);
2749 return (error);
2753 * chmod_args(char *path, int mode)
2755 * Change mode of a file given path name.
2757 /* ARGSUSED */
2759 sys_chmod(struct chmod_args *uap)
2761 struct nlookupdata nd;
2762 int error;
2764 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2765 if (error == 0)
2766 error = kern_chmod(&nd, uap->mode);
2767 nlookup_done(&nd);
2768 return (error);
2772 * lchmod_args(char *path, int mode)
2774 * Change mode of a file given path name (don't follow links.)
2776 /* ARGSUSED */
2778 sys_lchmod(struct lchmod_args *uap)
2780 struct nlookupdata nd;
2781 int error;
2783 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2784 if (error == 0)
2785 error = kern_chmod(&nd, uap->mode);
2786 nlookup_done(&nd);
2787 return (error);
2791 * fchmod_args(int fd, int mode)
2793 * Change mode of a file given a file descriptor.
2795 /* ARGSUSED */
2797 sys_fchmod(struct fchmod_args *uap)
2799 struct thread *td = curthread;
2800 struct proc *p = td->td_proc;
2801 struct file *fp;
2802 int error;
2804 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2805 return (error);
2806 if (fp->f_nchandle.ncp)
2807 error = ncp_writechk(&fp->f_nchandle);
2808 if (error == 0)
2809 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2810 fdrop(fp);
2811 return (error);
2815 * fchmodat_args(char *path, int mode)
2817 * Change mode of a file pointed to by fd/path.
2820 sys_fchmodat(struct fchmodat_args *uap)
2822 struct nlookupdata nd;
2823 struct file *fp;
2824 int error;
2825 int flags;
2827 if (uap->flags & ~_AT_SYMLINK_MASK)
2828 return (EINVAL);
2829 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2831 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2832 UIO_USERSPACE, flags);
2833 if (error == 0)
2834 error = kern_chmod(&nd, uap->mode);
2835 nlookup_done_at(&nd, fp);
2836 return (error);
2839 static int
2840 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2842 struct thread *td = curthread;
2843 struct proc *p = td->td_proc;
2844 int error;
2845 struct vattr vattr;
2848 * note: vget is required for any operation that might mod the vnode
2849 * so VINACTIVE is properly cleared.
2851 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2852 VATTR_NULL(&vattr);
2853 vattr.va_uid = uid;
2854 vattr.va_gid = gid;
2855 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2856 vput(vp);
2858 return error;
2862 kern_chown(struct nlookupdata *nd, int uid, int gid)
2864 struct vnode *vp;
2865 int error;
2867 if ((error = nlookup(nd)) != 0)
2868 return (error);
2869 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2870 return (error);
2871 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2872 error = setfown(vp, uid, gid);
2873 vrele(vp);
2874 return (error);
2878 * chown(char *path, int uid, int gid)
2880 * Set ownership given a path name.
2883 sys_chown(struct chown_args *uap)
2885 struct nlookupdata nd;
2886 int error;
2888 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2889 if (error == 0)
2890 error = kern_chown(&nd, uap->uid, uap->gid);
2891 nlookup_done(&nd);
2892 return (error);
2896 * lchown_args(char *path, int uid, int gid)
2898 * Set ownership given a path name, do not cross symlinks.
2901 sys_lchown(struct lchown_args *uap)
2903 struct nlookupdata nd;
2904 int error;
2906 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2907 if (error == 0)
2908 error = kern_chown(&nd, uap->uid, uap->gid);
2909 nlookup_done(&nd);
2910 return (error);
2914 * fchown_args(int fd, int uid, int gid)
2916 * Set ownership given a file descriptor.
2918 /* ARGSUSED */
2920 sys_fchown(struct fchown_args *uap)
2922 struct thread *td = curthread;
2923 struct proc *p = td->td_proc;
2924 struct file *fp;
2925 int error;
2927 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2928 return (error);
2929 if (fp->f_nchandle.ncp)
2930 error = ncp_writechk(&fp->f_nchandle);
2931 if (error == 0)
2932 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2933 fdrop(fp);
2934 return (error);
2938 * fchownat(int fd, char *path, int uid, int gid, int flags)
2940 * Set ownership of file pointed to by fd/path.
2943 sys_fchownat(struct fchownat_args *uap)
2945 struct nlookupdata nd;
2946 struct file *fp;
2947 int error;
2948 int flags;
2950 if (uap->flags & ~_AT_SYMLINK_MASK)
2951 return (EINVAL);
2952 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2954 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2955 UIO_USERSPACE, flags);
2956 if (error == 0)
2957 error = kern_chown(&nd, uap->uid, uap->gid);
2958 nlookup_done_at(&nd, fp);
2959 return (error);
2963 static int
2964 getutimes(const struct timeval *tvp, struct timespec *tsp)
2966 struct timeval tv[2];
2968 if (tvp == NULL) {
2969 microtime(&tv[0]);
2970 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2971 tsp[1] = tsp[0];
2972 } else {
2973 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2974 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2976 return 0;
2979 static int
2980 setutimes(struct vnode *vp, struct vattr *vattr,
2981 const struct timespec *ts, int nullflag)
2983 struct thread *td = curthread;
2984 struct proc *p = td->td_proc;
2985 int error;
2987 VATTR_NULL(vattr);
2988 vattr->va_atime = ts[0];
2989 vattr->va_mtime = ts[1];
2990 if (nullflag)
2991 vattr->va_vaflags |= VA_UTIMES_NULL;
2992 error = VOP_SETATTR(vp, vattr, p->p_ucred);
2994 return error;
2998 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3000 struct timespec ts[2];
3001 struct vnode *vp;
3002 struct vattr vattr;
3003 int error;
3005 if ((error = getutimes(tptr, ts)) != 0)
3006 return (error);
3009 * NOTE: utimes() succeeds for the owner even if the file
3010 * is not user-writable.
3012 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3014 if ((error = nlookup(nd)) != 0)
3015 return (error);
3016 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3017 return (error);
3018 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3019 return (error);
3022 * note: vget is required for any operation that might mod the vnode
3023 * so VINACTIVE is properly cleared.
3025 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3026 error = vget(vp, LK_EXCLUSIVE);
3027 if (error == 0) {
3028 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3029 vput(vp);
3032 vrele(vp);
3033 return (error);
3037 * utimes_args(char *path, struct timeval *tptr)
3039 * Set the access and modification times of a file.
3042 sys_utimes(struct utimes_args *uap)
3044 struct timeval tv[2];
3045 struct nlookupdata nd;
3046 int error;
3048 if (uap->tptr) {
3049 error = copyin(uap->tptr, tv, sizeof(tv));
3050 if (error)
3051 return (error);
3053 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3054 if (error == 0)
3055 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3056 nlookup_done(&nd);
3057 return (error);
3061 * lutimes_args(char *path, struct timeval *tptr)
3063 * Set the access and modification times of a file.
3066 sys_lutimes(struct lutimes_args *uap)
3068 struct timeval tv[2];
3069 struct nlookupdata nd;
3070 int error;
3072 if (uap->tptr) {
3073 error = copyin(uap->tptr, tv, sizeof(tv));
3074 if (error)
3075 return (error);
3077 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3078 if (error == 0)
3079 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3080 nlookup_done(&nd);
3081 return (error);
3085 * Set utimes on a file descriptor. The creds used to open the
3086 * file are used to determine whether the operation is allowed
3087 * or not.
3090 kern_futimes(int fd, struct timeval *tptr)
3092 struct thread *td = curthread;
3093 struct proc *p = td->td_proc;
3094 struct timespec ts[2];
3095 struct file *fp;
3096 struct vnode *vp;
3097 struct vattr vattr;
3098 int error;
3100 error = getutimes(tptr, ts);
3101 if (error)
3102 return (error);
3103 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3104 return (error);
3105 if (fp->f_nchandle.ncp)
3106 error = ncp_writechk(&fp->f_nchandle);
3107 if (error == 0) {
3108 vp = fp->f_data;
3109 error = vget(vp, LK_EXCLUSIVE);
3110 if (error == 0) {
3111 error = VOP_GETATTR(vp, &vattr);
3112 if (error == 0) {
3113 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3114 fp->f_cred);
3116 if (error == 0) {
3117 error = setutimes(vp, &vattr, ts,
3118 (tptr == NULL));
3120 vput(vp);
3123 fdrop(fp);
3124 return (error);
3128 * futimes_args(int fd, struct timeval *tptr)
3130 * Set the access and modification times of a file.
3133 sys_futimes(struct futimes_args *uap)
3135 struct timeval tv[2];
3136 int error;
3138 if (uap->tptr) {
3139 error = copyin(uap->tptr, tv, sizeof(tv));
3140 if (error)
3141 return (error);
3144 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3146 return (error);
3150 kern_truncate(struct nlookupdata *nd, off_t length)
3152 struct vnode *vp;
3153 struct vattr vattr;
3154 int error;
3156 if (length < 0)
3157 return(EINVAL);
3158 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3159 if ((error = nlookup(nd)) != 0)
3160 return (error);
3161 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3162 return (error);
3163 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3164 return (error);
3165 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3166 vrele(vp);
3167 return (error);
3169 if (vp->v_type == VDIR) {
3170 error = EISDIR;
3171 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3172 VATTR_NULL(&vattr);
3173 vattr.va_size = length;
3174 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3176 vput(vp);
3177 return (error);
3181 * truncate(char *path, int pad, off_t length)
3183 * Truncate a file given its path name.
3186 sys_truncate(struct truncate_args *uap)
3188 struct nlookupdata nd;
3189 int error;
3191 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3192 if (error == 0)
3193 error = kern_truncate(&nd, uap->length);
3194 nlookup_done(&nd);
3195 return error;
3199 kern_ftruncate(int fd, off_t length)
3201 struct thread *td = curthread;
3202 struct proc *p = td->td_proc;
3203 struct vattr vattr;
3204 struct vnode *vp;
3205 struct file *fp;
3206 int error;
3208 if (length < 0)
3209 return(EINVAL);
3210 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3211 return (error);
3212 if (fp->f_nchandle.ncp) {
3213 error = ncp_writechk(&fp->f_nchandle);
3214 if (error)
3215 goto done;
3217 if ((fp->f_flag & FWRITE) == 0) {
3218 error = EINVAL;
3219 goto done;
3221 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3222 error = EINVAL;
3223 goto done;
3225 vp = (struct vnode *)fp->f_data;
3226 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3227 if (vp->v_type == VDIR) {
3228 error = EISDIR;
3229 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3230 VATTR_NULL(&vattr);
3231 vattr.va_size = length;
3232 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3234 vn_unlock(vp);
3235 done:
3236 fdrop(fp);
3237 return (error);
3241 * ftruncate_args(int fd, int pad, off_t length)
3243 * Truncate a file given a file descriptor.
3246 sys_ftruncate(struct ftruncate_args *uap)
3248 int error;
3250 error = kern_ftruncate(uap->fd, uap->length);
3252 return (error);
3256 * fsync(int fd)
3258 * Sync an open file.
3260 /* ARGSUSED */
3262 sys_fsync(struct fsync_args *uap)
3264 struct thread *td = curthread;
3265 struct proc *p = td->td_proc;
3266 struct vnode *vp;
3267 struct file *fp;
3268 vm_object_t obj;
3269 int error;
3271 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3272 return (error);
3273 vp = (struct vnode *)fp->f_data;
3274 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3275 if ((obj = vp->v_object) != NULL)
3276 vm_object_page_clean(obj, 0, 0, 0);
3277 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3278 error = buf_fsync(vp);
3279 vn_unlock(vp);
3280 fdrop(fp);
3281 return (error);
3285 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3287 struct nchandle fnchd;
3288 struct nchandle tnchd;
3289 struct namecache *ncp;
3290 struct vnode *fdvp;
3291 struct vnode *tdvp;
3292 struct mount *mp;
3293 int error;
3295 bwillinode(1);
3296 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3297 if ((error = nlookup(fromnd)) != 0)
3298 return (error);
3299 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3300 return (ENOENT);
3301 fnchd.mount = fromnd->nl_nch.mount;
3302 cache_hold(&fnchd);
3305 * unlock the source nch so we can lookup the target nch without
3306 * deadlocking. The target may or may not exist so we do not check
3307 * for a target vp like kern_mkdir() and other creation functions do.
3309 * The source and target directories are ref'd and rechecked after
3310 * everything is relocked to determine if the source or target file
3311 * has been renamed.
3313 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3314 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3315 cache_unlock(&fromnd->nl_nch);
3317 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3318 if ((error = nlookup(tond)) != 0) {
3319 cache_drop(&fnchd);
3320 return (error);
3322 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3323 cache_drop(&fnchd);
3324 return (ENOENT);
3326 tnchd.mount = tond->nl_nch.mount;
3327 cache_hold(&tnchd);
3330 * If the source and target are the same there is nothing to do
3332 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3333 cache_drop(&fnchd);
3334 cache_drop(&tnchd);
3335 return (0);
3339 * Mount points cannot be renamed or overwritten
3341 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3342 NCF_ISMOUNTPT
3344 cache_drop(&fnchd);
3345 cache_drop(&tnchd);
3346 return (EINVAL);
3350 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3351 * may have become invalid while it was unlocked, nc_vp and nc_mount
3352 * could be NULL.
3354 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3355 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3356 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3357 cache_lock(&fromnd->nl_nch);
3358 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3359 } else {
3360 cache_unlock(&tond->nl_nch);
3361 cache_lock(&fromnd->nl_nch);
3362 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3363 cache_lock(&tond->nl_nch);
3364 cache_resolve(&tond->nl_nch, tond->nl_cred);
3366 fromnd->nl_flags |= NLC_NCPISLOCKED;
3369 * make sure the parent directories linkages are the same
3371 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3372 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3373 cache_drop(&fnchd);
3374 cache_drop(&tnchd);
3375 return (ENOENT);
3379 * Both the source and target must be within the same filesystem and
3380 * in the same filesystem as their parent directories within the
3381 * namecache topology.
3383 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3385 mp = fnchd.mount;
3386 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3387 mp != tond->nl_nch.mount) {
3388 cache_drop(&fnchd);
3389 cache_drop(&tnchd);
3390 return (EXDEV);
3394 * Make sure the mount point is writable
3396 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3397 cache_drop(&fnchd);
3398 cache_drop(&tnchd);
3399 return (error);
3403 * If the target exists and either the source or target is a directory,
3404 * then both must be directories.
3406 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3407 * have become NULL.
3409 if (tond->nl_nch.ncp->nc_vp) {
3410 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3411 error = ENOENT;
3412 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3413 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3414 error = ENOTDIR;
3415 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3416 error = EISDIR;
3421 * You cannot rename a source into itself or a subdirectory of itself.
3422 * We check this by travsersing the target directory upwards looking
3423 * for a match against the source.
3425 if (error == 0) {
3426 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3427 if (fromnd->nl_nch.ncp == ncp) {
3428 error = EINVAL;
3429 break;
3434 cache_drop(&fnchd);
3435 cache_drop(&tnchd);
3438 * Even though the namespaces are different, they may still represent
3439 * hardlinks to the same file. The filesystem might have a hard time
3440 * with this so we issue a NREMOVE of the source instead of a NRENAME
3441 * when we detect the situation.
3443 if (error == 0) {
3444 fdvp = fromnd->nl_dvp;
3445 tdvp = tond->nl_dvp;
3446 if (fdvp == NULL || tdvp == NULL) {
3447 error = EPERM;
3448 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3449 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3450 fromnd->nl_cred);
3451 } else {
3452 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3453 fdvp, tdvp, tond->nl_cred);
3456 return (error);
3460 * rename_args(char *from, char *to)
3462 * Rename files. Source and destination must either both be directories,
3463 * or both not be directories. If target is a directory, it must be empty.
3466 sys_rename(struct rename_args *uap)
3468 struct nlookupdata fromnd, tond;
3469 int error;
3471 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3472 if (error == 0) {
3473 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3474 if (error == 0)
3475 error = kern_rename(&fromnd, &tond);
3476 nlookup_done(&tond);
3478 nlookup_done(&fromnd);
3479 return (error);
3483 kern_mkdir(struct nlookupdata *nd, int mode)
3485 struct thread *td = curthread;
3486 struct proc *p = td->td_proc;
3487 struct vnode *vp;
3488 struct vattr vattr;
3489 int error;
3491 bwillinode(1);
3492 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3493 if ((error = nlookup(nd)) != 0)
3494 return (error);
3496 if (nd->nl_nch.ncp->nc_vp)
3497 return (EEXIST);
3498 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3499 return (error);
3500 VATTR_NULL(&vattr);
3501 vattr.va_type = VDIR;
3502 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3504 vp = NULL;
3505 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3506 if (error == 0)
3507 vput(vp);
3508 return (error);
3512 * mkdir_args(char *path, int mode)
3514 * Make a directory file.
3516 /* ARGSUSED */
3518 sys_mkdir(struct mkdir_args *uap)
3520 struct nlookupdata nd;
3521 int error;
3523 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3524 if (error == 0)
3525 error = kern_mkdir(&nd, uap->mode);
3526 nlookup_done(&nd);
3527 return (error);
3531 kern_rmdir(struct nlookupdata *nd)
3533 int error;
3535 bwillinode(1);
3536 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3537 if ((error = nlookup(nd)) != 0)
3538 return (error);
3541 * Do not allow directories representing mount points to be
3542 * deleted, even if empty. Check write perms on mount point
3543 * in case the vnode is aliased (aka nullfs).
3545 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3546 return (EINVAL);
3547 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3548 return (error);
3549 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3550 return (error);
3554 * rmdir_args(char *path)
3556 * Remove a directory file.
3558 /* ARGSUSED */
3560 sys_rmdir(struct rmdir_args *uap)
3562 struct nlookupdata nd;
3563 int error;
3565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3566 if (error == 0)
3567 error = kern_rmdir(&nd);
3568 nlookup_done(&nd);
3569 return (error);
3573 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3574 enum uio_seg direction)
3576 struct thread *td = curthread;
3577 struct proc *p = td->td_proc;
3578 struct vnode *vp;
3579 struct file *fp;
3580 struct uio auio;
3581 struct iovec aiov;
3582 off_t loff;
3583 int error, eofflag;
3585 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3586 return (error);
3587 if ((fp->f_flag & FREAD) == 0) {
3588 error = EBADF;
3589 goto done;
3591 vp = (struct vnode *)fp->f_data;
3592 unionread:
3593 if (vp->v_type != VDIR) {
3594 error = EINVAL;
3595 goto done;
3597 aiov.iov_base = buf;
3598 aiov.iov_len = count;
3599 auio.uio_iov = &aiov;
3600 auio.uio_iovcnt = 1;
3601 auio.uio_rw = UIO_READ;
3602 auio.uio_segflg = direction;
3603 auio.uio_td = td;
3604 auio.uio_resid = count;
3605 loff = auio.uio_offset = fp->f_offset;
3606 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3607 fp->f_offset = auio.uio_offset;
3608 if (error)
3609 goto done;
3610 if (count == auio.uio_resid) {
3611 if (union_dircheckp) {
3612 error = union_dircheckp(td, &vp, fp);
3613 if (error == -1)
3614 goto unionread;
3615 if (error)
3616 goto done;
3618 #if 0
3619 if ((vp->v_flag & VROOT) &&
3620 (vp->v_mount->mnt_flag & MNT_UNION)) {
3621 struct vnode *tvp = vp;
3622 vp = vp->v_mount->mnt_vnodecovered;
3623 vref(vp);
3624 fp->f_data = vp;
3625 fp->f_offset = 0;
3626 vrele(tvp);
3627 goto unionread;
3629 #endif
3633 * WARNING! *basep may not be wide enough to accomodate the
3634 * seek offset. XXX should we hack this to return the upper 32 bits
3635 * for offsets greater then 4G?
3637 if (basep) {
3638 *basep = (long)loff;
3640 *res = count - auio.uio_resid;
3641 done:
3642 fdrop(fp);
3643 return (error);
3647 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3649 * Read a block of directory entries in a file system independent format.
3652 sys_getdirentries(struct getdirentries_args *uap)
3654 long base;
3655 int error;
3657 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3658 &uap->sysmsg_result, UIO_USERSPACE);
3660 if (error == 0 && uap->basep)
3661 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3662 return (error);
3666 * getdents_args(int fd, char *buf, size_t count)
3669 sys_getdents(struct getdents_args *uap)
3671 int error;
3673 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3674 &uap->sysmsg_result, UIO_USERSPACE);
3676 return (error);
3680 * umask(int newmask)
3682 * Set the mode mask for creation of filesystem nodes.
3684 * MP SAFE
3687 sys_umask(struct umask_args *uap)
3689 struct thread *td = curthread;
3690 struct proc *p = td->td_proc;
3691 struct filedesc *fdp;
3693 fdp = p->p_fd;
3694 uap->sysmsg_result = fdp->fd_cmask;
3695 fdp->fd_cmask = uap->newmask & ALLPERMS;
3696 return (0);
3700 * revoke(char *path)
3702 * Void all references to file by ripping underlying filesystem
3703 * away from vnode.
3705 /* ARGSUSED */
3707 sys_revoke(struct revoke_args *uap)
3709 struct nlookupdata nd;
3710 struct vattr vattr;
3711 struct vnode *vp;
3712 struct ucred *cred;
3713 int error;
3715 vp = NULL;
3716 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3717 if (error == 0)
3718 error = nlookup(&nd);
3719 if (error == 0)
3720 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3721 cred = crhold(nd.nl_cred);
3722 nlookup_done(&nd);
3723 if (error == 0) {
3724 if (error == 0)
3725 error = VOP_GETATTR(vp, &vattr);
3726 if (error == 0 && cred->cr_uid != vattr.va_uid)
3727 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3728 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3729 if (vcount(vp) > 0)
3730 error = vrevoke(vp, cred);
3731 } else if (error == 0) {
3732 error = vrevoke(vp, cred);
3734 vrele(vp);
3736 if (cred)
3737 crfree(cred);
3738 return (error);
3742 * getfh_args(char *fname, fhandle_t *fhp)
3744 * Get (NFS) file handle
3746 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3747 * mount. This allows nullfs mounts to be explicitly exported.
3749 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3751 * nullfs mounts of subdirectories are not safe. That is, it will
3752 * work, but you do not really have protection against access to
3753 * the related parent directories.
3756 sys_getfh(struct getfh_args *uap)
3758 struct thread *td = curthread;
3759 struct nlookupdata nd;
3760 fhandle_t fh;
3761 struct vnode *vp;
3762 struct mount *mp;
3763 int error;
3766 * Must be super user
3768 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3769 return (error);
3771 vp = NULL;
3772 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3773 if (error == 0)
3774 error = nlookup(&nd);
3775 if (error == 0)
3776 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3777 mp = nd.nl_nch.mount;
3778 nlookup_done(&nd);
3779 if (error == 0) {
3780 bzero(&fh, sizeof(fh));
3781 fh.fh_fsid = mp->mnt_stat.f_fsid;
3782 error = VFS_VPTOFH(vp, &fh.fh_fid);
3783 vput(vp);
3784 if (error == 0)
3785 error = copyout(&fh, uap->fhp, sizeof(fh));
3787 return (error);
3791 * fhopen_args(const struct fhandle *u_fhp, int flags)
3793 * syscall for the rpc.lockd to use to translate a NFS file handle into
3794 * an open descriptor.
3796 * warning: do not remove the priv_check() call or this becomes one giant
3797 * security hole.
3800 sys_fhopen(struct fhopen_args *uap)
3802 struct thread *td = curthread;
3803 struct proc *p = td->td_proc;
3804 struct mount *mp;
3805 struct vnode *vp;
3806 struct fhandle fhp;
3807 struct vattr vat;
3808 struct vattr *vap = &vat;
3809 struct flock lf;
3810 int fmode, mode, error, type;
3811 struct file *nfp;
3812 struct file *fp;
3813 int indx;
3816 * Must be super user
3818 error = priv_check(td, PRIV_ROOT);
3819 if (error)
3820 return (error);
3822 fmode = FFLAGS(uap->flags);
3823 /* why not allow a non-read/write open for our lockd? */
3824 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3825 return (EINVAL);
3826 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3827 if (error)
3828 return(error);
3829 /* find the mount point */
3830 mp = vfs_getvfs(&fhp.fh_fsid);
3831 if (mp == NULL)
3832 return (ESTALE);
3833 /* now give me my vnode, it gets returned to me locked */
3834 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3835 if (error)
3836 return (error);
3838 * from now on we have to make sure not
3839 * to forget about the vnode
3840 * any error that causes an abort must vput(vp)
3841 * just set error = err and 'goto bad;'.
3845 * from vn_open
3847 if (vp->v_type == VLNK) {
3848 error = EMLINK;
3849 goto bad;
3851 if (vp->v_type == VSOCK) {
3852 error = EOPNOTSUPP;
3853 goto bad;
3855 mode = 0;
3856 if (fmode & (FWRITE | O_TRUNC)) {
3857 if (vp->v_type == VDIR) {
3858 error = EISDIR;
3859 goto bad;
3861 error = vn_writechk(vp, NULL);
3862 if (error)
3863 goto bad;
3864 mode |= VWRITE;
3866 if (fmode & FREAD)
3867 mode |= VREAD;
3868 if (mode) {
3869 error = VOP_ACCESS(vp, mode, p->p_ucred);
3870 if (error)
3871 goto bad;
3873 if (fmode & O_TRUNC) {
3874 vn_unlock(vp); /* XXX */
3875 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3876 VATTR_NULL(vap);
3877 vap->va_size = 0;
3878 error = VOP_SETATTR(vp, vap, p->p_ucred);
3879 if (error)
3880 goto bad;
3884 * VOP_OPEN needs the file pointer so it can potentially override
3885 * it.
3887 * WARNING! no f_nchandle will be associated when fhopen()ing a
3888 * directory. XXX
3890 if ((error = falloc(p, &nfp, &indx)) != 0)
3891 goto bad;
3892 fp = nfp;
3894 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3895 if (error) {
3897 * setting f_ops this way prevents VOP_CLOSE from being
3898 * called or fdrop() releasing the vp from v_data. Since
3899 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3901 fp->f_ops = &badfileops;
3902 fp->f_data = NULL;
3903 goto bad_drop;
3907 * The fp is given its own reference, we still have our ref and lock.
3909 * Assert that all regular files must be created with a VM object.
3911 if (vp->v_type == VREG && vp->v_object == NULL) {
3912 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3913 goto bad_drop;
3917 * The open was successful. Handle any locking requirements.
3919 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3920 lf.l_whence = SEEK_SET;
3921 lf.l_start = 0;
3922 lf.l_len = 0;
3923 if (fmode & O_EXLOCK)
3924 lf.l_type = F_WRLCK;
3925 else
3926 lf.l_type = F_RDLCK;
3927 if (fmode & FNONBLOCK)
3928 type = 0;
3929 else
3930 type = F_WAIT;
3931 vn_unlock(vp);
3932 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3934 * release our private reference.
3936 fsetfd(p, NULL, indx);
3937 fdrop(fp);
3938 vrele(vp);
3939 return (error);
3941 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3942 fp->f_flag |= FHASLOCK;
3946 * Clean up. Associate the file pointer with the previously
3947 * reserved descriptor and return it.
3949 vput(vp);
3950 fsetfd(p, fp, indx);
3951 fdrop(fp);
3952 uap->sysmsg_result = indx;
3953 return (0);
3955 bad_drop:
3956 fsetfd(p, NULL, indx);
3957 fdrop(fp);
3958 bad:
3959 vput(vp);
3960 return (error);
3964 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3967 sys_fhstat(struct fhstat_args *uap)
3969 struct thread *td = curthread;
3970 struct stat sb;
3971 fhandle_t fh;
3972 struct mount *mp;
3973 struct vnode *vp;
3974 int error;
3977 * Must be super user
3979 error = priv_check(td, PRIV_ROOT);
3980 if (error)
3981 return (error);
3983 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3984 if (error)
3985 return (error);
3987 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3988 return (ESTALE);
3989 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3990 return (error);
3991 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3992 vput(vp);
3993 if (error)
3994 return (error);
3995 error = copyout(&sb, uap->sb, sizeof(sb));
3996 return (error);
4000 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4003 sys_fhstatfs(struct fhstatfs_args *uap)
4005 struct thread *td = curthread;
4006 struct proc *p = td->td_proc;
4007 struct statfs *sp;
4008 struct mount *mp;
4009 struct vnode *vp;
4010 struct statfs sb;
4011 char *fullpath, *freepath;
4012 fhandle_t fh;
4013 int error;
4016 * Must be super user
4018 if ((error = priv_check(td, PRIV_ROOT)))
4019 return (error);
4021 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4022 return (error);
4024 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4025 return (ESTALE);
4027 if (p != NULL && !chroot_visible_mnt(mp, p))
4028 return (ESTALE);
4030 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4031 return (error);
4032 mp = vp->v_mount;
4033 sp = &mp->mnt_stat;
4034 vput(vp);
4035 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4036 return (error);
4038 error = mount_path(p, mp, &fullpath, &freepath);
4039 if (error)
4040 return(error);
4041 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4042 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4043 kfree(freepath, M_TEMP);
4045 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4046 if (priv_check(td, PRIV_ROOT)) {
4047 bcopy(sp, &sb, sizeof(sb));
4048 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4049 sp = &sb;
4051 return (copyout(sp, uap->buf, sizeof(*sp)));
4055 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4058 sys_fhstatvfs(struct fhstatvfs_args *uap)
4060 struct thread *td = curthread;
4061 struct proc *p = td->td_proc;
4062 struct statvfs *sp;
4063 struct mount *mp;
4064 struct vnode *vp;
4065 fhandle_t fh;
4066 int error;
4069 * Must be super user
4071 if ((error = priv_check(td, PRIV_ROOT)))
4072 return (error);
4074 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4075 return (error);
4077 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4078 return (ESTALE);
4080 if (p != NULL && !chroot_visible_mnt(mp, p))
4081 return (ESTALE);
4083 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4084 return (error);
4085 mp = vp->v_mount;
4086 sp = &mp->mnt_vstat;
4087 vput(vp);
4088 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4089 return (error);
4091 sp->f_flag = 0;
4092 if (mp->mnt_flag & MNT_RDONLY)
4093 sp->f_flag |= ST_RDONLY;
4094 if (mp->mnt_flag & MNT_NOSUID)
4095 sp->f_flag |= ST_NOSUID;
4097 return (copyout(sp, uap->buf, sizeof(*sp)));
4102 * Syscall to push extended attribute configuration information into the
4103 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4104 * a command (int cmd), and attribute name and misc data. For now, the
4105 * attribute name is left in userspace for consumption by the VFS_op.
4106 * It will probably be changed to be copied into sysspace by the
4107 * syscall in the future, once issues with various consumers of the
4108 * attribute code have raised their hands.
4110 * Currently this is used only by UFS Extended Attributes.
4113 sys_extattrctl(struct extattrctl_args *uap)
4115 struct nlookupdata nd;
4116 struct mount *mp;
4117 struct vnode *vp;
4118 int error;
4120 vp = NULL;
4121 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4122 if (error == 0)
4123 error = nlookup(&nd);
4124 if (error == 0) {
4125 mp = nd.nl_nch.mount;
4126 error = VFS_EXTATTRCTL(mp, uap->cmd,
4127 uap->attrname, uap->arg,
4128 nd.nl_cred);
4130 nlookup_done(&nd);
4131 return (error);
4135 * Syscall to set a named extended attribute on a file or directory.
4136 * Accepts attribute name, and a uio structure pointing to the data to set.
4137 * The uio is consumed in the style of writev(). The real work happens
4138 * in VOP_SETEXTATTR().
4141 sys_extattr_set_file(struct extattr_set_file_args *uap)
4143 char attrname[EXTATTR_MAXNAMELEN];
4144 struct iovec aiov[UIO_SMALLIOV];
4145 struct iovec *needfree;
4146 struct nlookupdata nd;
4147 struct iovec *iov;
4148 struct vnode *vp;
4149 struct uio auio;
4150 u_int iovlen;
4151 u_int cnt;
4152 int error;
4153 int i;
4155 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4156 if (error)
4157 return (error);
4159 vp = NULL;
4160 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4161 if (error == 0)
4162 error = nlookup(&nd);
4163 if (error == 0)
4164 error = ncp_writechk(&nd.nl_nch);
4165 if (error == 0)
4166 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4167 if (error) {
4168 nlookup_done(&nd);
4169 return (error);
4172 needfree = NULL;
4173 iovlen = uap->iovcnt * sizeof(struct iovec);
4174 if (uap->iovcnt > UIO_SMALLIOV) {
4175 if (uap->iovcnt > UIO_MAXIOV) {
4176 error = EINVAL;
4177 goto done;
4179 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4180 needfree = iov;
4181 } else {
4182 iov = aiov;
4184 auio.uio_iov = iov;
4185 auio.uio_iovcnt = uap->iovcnt;
4186 auio.uio_rw = UIO_WRITE;
4187 auio.uio_segflg = UIO_USERSPACE;
4188 auio.uio_td = nd.nl_td;
4189 auio.uio_offset = 0;
4190 if ((error = copyin(uap->iovp, iov, iovlen)))
4191 goto done;
4192 auio.uio_resid = 0;
4193 for (i = 0; i < uap->iovcnt; i++) {
4194 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4195 error = EINVAL;
4196 goto done;
4198 auio.uio_resid += iov->iov_len;
4199 iov++;
4201 cnt = auio.uio_resid;
4202 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4203 cnt -= auio.uio_resid;
4204 uap->sysmsg_result = cnt;
4205 done:
4206 vput(vp);
4207 nlookup_done(&nd);
4208 if (needfree)
4209 FREE(needfree, M_IOV);
4210 return (error);
4214 * Syscall to get a named extended attribute on a file or directory.
4215 * Accepts attribute name, and a uio structure pointing to a buffer for the
4216 * data. The uio is consumed in the style of readv(). The real work
4217 * happens in VOP_GETEXTATTR();
4220 sys_extattr_get_file(struct extattr_get_file_args *uap)
4222 char attrname[EXTATTR_MAXNAMELEN];
4223 struct iovec aiov[UIO_SMALLIOV];
4224 struct iovec *needfree;
4225 struct nlookupdata nd;
4226 struct iovec *iov;
4227 struct vnode *vp;
4228 struct uio auio;
4229 u_int iovlen;
4230 u_int cnt;
4231 int error;
4232 int i;
4234 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4235 if (error)
4236 return (error);
4238 vp = NULL;
4239 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4240 if (error == 0)
4241 error = nlookup(&nd);
4242 if (error == 0)
4243 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4244 if (error) {
4245 nlookup_done(&nd);
4246 return (error);
4249 iovlen = uap->iovcnt * sizeof (struct iovec);
4250 needfree = NULL;
4251 if (uap->iovcnt > UIO_SMALLIOV) {
4252 if (uap->iovcnt > UIO_MAXIOV) {
4253 error = EINVAL;
4254 goto done;
4256 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4257 needfree = iov;
4258 } else {
4259 iov = aiov;
4261 auio.uio_iov = iov;
4262 auio.uio_iovcnt = uap->iovcnt;
4263 auio.uio_rw = UIO_READ;
4264 auio.uio_segflg = UIO_USERSPACE;
4265 auio.uio_td = nd.nl_td;
4266 auio.uio_offset = 0;
4267 if ((error = copyin(uap->iovp, iov, iovlen)))
4268 goto done;
4269 auio.uio_resid = 0;
4270 for (i = 0; i < uap->iovcnt; i++) {
4271 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4272 error = EINVAL;
4273 goto done;
4275 auio.uio_resid += iov->iov_len;
4276 iov++;
4278 cnt = auio.uio_resid;
4279 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4280 cnt -= auio.uio_resid;
4281 uap->sysmsg_result = cnt;
4282 done:
4283 vput(vp);
4284 nlookup_done(&nd);
4285 if (needfree)
4286 FREE(needfree, M_IOV);
4287 return(error);
4291 * Syscall to delete a named extended attribute from a file or directory.
4292 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4295 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4297 char attrname[EXTATTR_MAXNAMELEN];
4298 struct nlookupdata nd;
4299 struct vnode *vp;
4300 int error;
4302 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4303 if (error)
4304 return(error);
4306 vp = NULL;
4307 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4308 if (error == 0)
4309 error = nlookup(&nd);
4310 if (error == 0)
4311 error = ncp_writechk(&nd.nl_nch);
4312 if (error == 0)
4313 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4314 if (error) {
4315 nlookup_done(&nd);
4316 return (error);
4319 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4320 vput(vp);
4321 nlookup_done(&nd);
4322 return(error);
4326 * Determine if the mount is visible to the process.
4328 static int
4329 chroot_visible_mnt(struct mount *mp, struct proc *p)
4331 struct nchandle nch;
4334 * Traverse from the mount point upwards. If we hit the process
4335 * root then the mount point is visible to the process.
4337 nch = mp->mnt_ncmountpt;
4338 while (nch.ncp) {
4339 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4340 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4341 return(1);
4343 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4344 nch = nch.mount->mnt_ncmounton;
4345 } else {
4346 nch.ncp = nch.ncp->nc_parent;
4351 * If the mount point is not visible to the process, but the
4352 * process root is in a subdirectory of the mount, return
4353 * TRUE anyway.
4355 if (p->p_fd->fd_nrdir.mount == mp)
4356 return(1);
4358 return(0);