priv: Use PRIV_VFS_LINK
[dragonfly.git] / sys / kern / vfs_syscalls.c
blob954e90ae85a8402df36f5d9b44756b7ee7dc4a37
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);
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 ((error = priv_check(td, PRIV_ROOT)) != 0)
912 return (error);
915 * Argument length checks
917 if (uap->ctllen < 0 || uap->ctllen > 1024)
918 return (EINVAL);
919 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
920 return (EINVAL);
921 if (uap->path == NULL)
922 return (EINVAL);
925 * Allocate the necessary buffers and copyin data
927 path = objcache_get(namei_oc, M_WAITOK);
928 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
929 if (error)
930 goto done;
932 if (uap->ctllen) {
933 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
934 error = copyin(uap->ctl, ctl, uap->ctllen);
935 if (error)
936 goto done;
938 if (uap->buflen)
939 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
942 * Validate the descriptor
944 if (uap->fd >= 0) {
945 fp = holdfp(p->p_fd, uap->fd, -1);
946 if (fp == NULL) {
947 error = EBADF;
948 goto done;
950 } else {
951 fp = NULL;
955 * Execute the internal kernel function and clean up.
957 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
958 if (fp)
959 fdrop(fp);
960 if (error == 0 && uap->sysmsg_result > 0)
961 error = copyout(buf, uap->buf, uap->sysmsg_result);
962 done:
963 if (path)
964 objcache_put(namei_oc, path);
965 if (ctl)
966 kfree(ctl, M_TEMP);
967 if (buf)
968 kfree(buf, M_TEMP);
969 return (error);
973 * Execute a mount control operation by resolving the path to a mount point
974 * and calling vop_mountctl().
976 * Use the mount point from the nch instead of the vnode so nullfs mounts
977 * can properly spike the VOP.
980 kern_mountctl(const char *path, int op, struct file *fp,
981 const void *ctl, int ctllen,
982 void *buf, int buflen, int *res)
984 struct vnode *vp;
985 struct mount *mp;
986 struct nlookupdata nd;
987 int error;
989 *res = 0;
990 vp = NULL;
991 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
992 if (error == 0)
993 error = nlookup(&nd);
994 if (error == 0)
995 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
996 mp = nd.nl_nch.mount;
997 nlookup_done(&nd);
998 if (error)
999 return (error);
1002 * Must be the root of the filesystem
1004 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1005 vput(vp);
1006 return (EINVAL);
1008 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1009 buf, buflen, res);
1010 vput(vp);
1011 return (error);
1015 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1017 struct thread *td = curthread;
1018 struct proc *p = td->td_proc;
1019 struct mount *mp;
1020 struct statfs *sp;
1021 char *fullpath, *freepath;
1022 int error;
1024 if ((error = nlookup(nd)) != 0)
1025 return (error);
1026 mp = nd->nl_nch.mount;
1027 sp = &mp->mnt_stat;
1028 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1029 return (error);
1031 error = mount_path(p, mp, &fullpath, &freepath);
1032 if (error)
1033 return(error);
1034 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1035 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1036 kfree(freepath, M_TEMP);
1038 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1039 bcopy(sp, buf, sizeof(*buf));
1040 /* Only root should have access to the fsid's. */
1041 if (priv_check(td, PRIV_ROOT))
1042 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1043 return (0);
1047 * statfs_args(char *path, struct statfs *buf)
1049 * Get filesystem statistics.
1052 sys_statfs(struct statfs_args *uap)
1054 struct nlookupdata nd;
1055 struct statfs buf;
1056 int error;
1058 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1059 if (error == 0)
1060 error = kern_statfs(&nd, &buf);
1061 nlookup_done(&nd);
1062 if (error == 0)
1063 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1064 return (error);
1068 kern_fstatfs(int fd, struct statfs *buf)
1070 struct thread *td = curthread;
1071 struct proc *p = td->td_proc;
1072 struct file *fp;
1073 struct mount *mp;
1074 struct statfs *sp;
1075 char *fullpath, *freepath;
1076 int error;
1078 KKASSERT(p);
1079 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1080 return (error);
1081 mp = ((struct vnode *)fp->f_data)->v_mount;
1082 if (mp == NULL) {
1083 error = EBADF;
1084 goto done;
1086 if (fp->f_cred == NULL) {
1087 error = EINVAL;
1088 goto done;
1090 sp = &mp->mnt_stat;
1091 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1092 goto done;
1094 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1095 goto done;
1096 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1097 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1098 kfree(freepath, M_TEMP);
1100 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1101 bcopy(sp, buf, sizeof(*buf));
1103 /* Only root should have access to the fsid's. */
1104 if (priv_check(td, PRIV_ROOT))
1105 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1106 error = 0;
1107 done:
1108 fdrop(fp);
1109 return (error);
1113 * fstatfs_args(int fd, struct statfs *buf)
1115 * Get filesystem statistics.
1118 sys_fstatfs(struct fstatfs_args *uap)
1120 struct statfs buf;
1121 int error;
1123 error = kern_fstatfs(uap->fd, &buf);
1125 if (error == 0)
1126 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1127 return (error);
1131 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1133 struct mount *mp;
1134 struct statvfs *sp;
1135 int error;
1137 if ((error = nlookup(nd)) != 0)
1138 return (error);
1139 mp = nd->nl_nch.mount;
1140 sp = &mp->mnt_vstat;
1141 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1142 return (error);
1144 sp->f_flag = 0;
1145 if (mp->mnt_flag & MNT_RDONLY)
1146 sp->f_flag |= ST_RDONLY;
1147 if (mp->mnt_flag & MNT_NOSUID)
1148 sp->f_flag |= ST_NOSUID;
1149 bcopy(sp, buf, sizeof(*buf));
1150 return (0);
1154 * statfs_args(char *path, struct statfs *buf)
1156 * Get filesystem statistics.
1159 sys_statvfs(struct statvfs_args *uap)
1161 struct nlookupdata nd;
1162 struct statvfs buf;
1163 int error;
1165 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1166 if (error == 0)
1167 error = kern_statvfs(&nd, &buf);
1168 nlookup_done(&nd);
1169 if (error == 0)
1170 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1171 return (error);
1175 kern_fstatvfs(int fd, struct statvfs *buf)
1177 struct thread *td = curthread;
1178 struct proc *p = td->td_proc;
1179 struct file *fp;
1180 struct mount *mp;
1181 struct statvfs *sp;
1182 int error;
1184 KKASSERT(p);
1185 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1186 return (error);
1187 mp = ((struct vnode *)fp->f_data)->v_mount;
1188 if (mp == NULL) {
1189 error = EBADF;
1190 goto done;
1192 if (fp->f_cred == NULL) {
1193 error = EINVAL;
1194 goto done;
1196 sp = &mp->mnt_vstat;
1197 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1198 goto done;
1200 sp->f_flag = 0;
1201 if (mp->mnt_flag & MNT_RDONLY)
1202 sp->f_flag |= ST_RDONLY;
1203 if (mp->mnt_flag & MNT_NOSUID)
1204 sp->f_flag |= ST_NOSUID;
1206 bcopy(sp, buf, sizeof(*buf));
1207 error = 0;
1208 done:
1209 fdrop(fp);
1210 return (error);
1214 * fstatfs_args(int fd, struct statfs *buf)
1216 * Get filesystem statistics.
1219 sys_fstatvfs(struct fstatvfs_args *uap)
1221 struct statvfs buf;
1222 int error;
1224 error = kern_fstatvfs(uap->fd, &buf);
1226 if (error == 0)
1227 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1228 return (error);
1232 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1234 * Get statistics on all filesystems.
1237 struct getfsstat_info {
1238 struct statfs *sfsp;
1239 long count;
1240 long maxcount;
1241 int error;
1242 int flags;
1243 struct proc *p;
1246 static int getfsstat_callback(struct mount *, void *);
1248 /* ARGSUSED */
1250 sys_getfsstat(struct getfsstat_args *uap)
1252 struct thread *td = curthread;
1253 struct proc *p = td->td_proc;
1254 struct getfsstat_info info;
1256 bzero(&info, sizeof(info));
1258 info.maxcount = uap->bufsize / sizeof(struct statfs);
1259 info.sfsp = uap->buf;
1260 info.count = 0;
1261 info.flags = uap->flags;
1262 info.p = p;
1264 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1265 if (info.sfsp && info.count > info.maxcount)
1266 uap->sysmsg_result = info.maxcount;
1267 else
1268 uap->sysmsg_result = info.count;
1269 return (info.error);
1272 static int
1273 getfsstat_callback(struct mount *mp, void *data)
1275 struct getfsstat_info *info = data;
1276 struct statfs *sp;
1277 char *freepath;
1278 char *fullpath;
1279 int error;
1281 if (info->sfsp && info->count < info->maxcount) {
1282 if (info->p && !chroot_visible_mnt(mp, info->p))
1283 return(0);
1284 sp = &mp->mnt_stat;
1287 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1288 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1289 * overrides MNT_WAIT.
1291 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1292 (info->flags & MNT_WAIT)) &&
1293 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1294 return(0);
1296 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1298 error = mount_path(info->p, mp, &fullpath, &freepath);
1299 if (error) {
1300 info->error = error;
1301 return(-1);
1303 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1304 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1305 kfree(freepath, M_TEMP);
1307 error = copyout(sp, info->sfsp, sizeof(*sp));
1308 if (error) {
1309 info->error = error;
1310 return (-1);
1312 ++info->sfsp;
1314 info->count++;
1315 return(0);
1319 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1320 long bufsize, int flags)
1322 * Get statistics on all filesystems.
1325 struct getvfsstat_info {
1326 struct statfs *sfsp;
1327 struct statvfs *vsfsp;
1328 long count;
1329 long maxcount;
1330 int error;
1331 int flags;
1332 struct proc *p;
1335 static int getvfsstat_callback(struct mount *, void *);
1337 /* ARGSUSED */
1339 sys_getvfsstat(struct getvfsstat_args *uap)
1341 struct thread *td = curthread;
1342 struct proc *p = td->td_proc;
1343 struct getvfsstat_info info;
1345 bzero(&info, sizeof(info));
1347 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1348 info.sfsp = uap->buf;
1349 info.vsfsp = uap->vbuf;
1350 info.count = 0;
1351 info.flags = uap->flags;
1352 info.p = p;
1354 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1355 if (info.vsfsp && info.count > info.maxcount)
1356 uap->sysmsg_result = info.maxcount;
1357 else
1358 uap->sysmsg_result = info.count;
1359 return (info.error);
1362 static int
1363 getvfsstat_callback(struct mount *mp, void *data)
1365 struct getvfsstat_info *info = data;
1366 struct statfs *sp;
1367 struct statvfs *vsp;
1368 char *freepath;
1369 char *fullpath;
1370 int error;
1372 if (info->vsfsp && info->count < info->maxcount) {
1373 if (info->p && !chroot_visible_mnt(mp, info->p))
1374 return(0);
1375 sp = &mp->mnt_stat;
1376 vsp = &mp->mnt_vstat;
1379 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1380 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1381 * overrides MNT_WAIT.
1383 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1384 (info->flags & MNT_WAIT)) &&
1385 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1386 return(0);
1388 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1390 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1391 (info->flags & MNT_WAIT)) &&
1392 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1393 return(0);
1395 vsp->f_flag = 0;
1396 if (mp->mnt_flag & MNT_RDONLY)
1397 vsp->f_flag |= ST_RDONLY;
1398 if (mp->mnt_flag & MNT_NOSUID)
1399 vsp->f_flag |= ST_NOSUID;
1401 error = mount_path(info->p, mp, &fullpath, &freepath);
1402 if (error) {
1403 info->error = error;
1404 return(-1);
1406 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1407 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1408 kfree(freepath, M_TEMP);
1410 error = copyout(sp, info->sfsp, sizeof(*sp));
1411 if (error == 0)
1412 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1413 if (error) {
1414 info->error = error;
1415 return (-1);
1417 ++info->sfsp;
1418 ++info->vsfsp;
1420 info->count++;
1421 return(0);
1426 * fchdir_args(int fd)
1428 * Change current working directory to a given file descriptor.
1430 /* ARGSUSED */
1432 sys_fchdir(struct fchdir_args *uap)
1434 struct thread *td = curthread;
1435 struct proc *p = td->td_proc;
1436 struct filedesc *fdp = p->p_fd;
1437 struct vnode *vp, *ovp;
1438 struct mount *mp;
1439 struct file *fp;
1440 struct nchandle nch, onch, tnch;
1441 int error;
1443 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1444 return (error);
1445 vp = (struct vnode *)fp->f_data;
1446 vref(vp);
1447 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1448 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1449 error = ENOTDIR;
1450 else
1451 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1452 if (error) {
1453 vput(vp);
1454 fdrop(fp);
1455 return (error);
1457 cache_copy(&fp->f_nchandle, &nch);
1460 * If the ncp has become a mount point, traverse through
1461 * the mount point.
1464 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1465 (mp = cache_findmount(&nch)) != NULL
1467 error = nlookup_mp(mp, &tnch);
1468 if (error == 0) {
1469 cache_unlock(&tnch); /* leave ref intact */
1470 vput(vp);
1471 vp = tnch.ncp->nc_vp;
1472 error = vget(vp, LK_SHARED);
1473 KKASSERT(error == 0);
1474 cache_drop(&nch);
1475 nch = tnch;
1478 if (error == 0) {
1479 ovp = fdp->fd_cdir;
1480 onch = fdp->fd_ncdir;
1481 vn_unlock(vp); /* leave ref intact */
1482 fdp->fd_cdir = vp;
1483 fdp->fd_ncdir = nch;
1484 cache_drop(&onch);
1485 vrele(ovp);
1486 } else {
1487 cache_drop(&nch);
1488 vput(vp);
1490 fdrop(fp);
1491 return (error);
1495 kern_chdir(struct nlookupdata *nd)
1497 struct thread *td = curthread;
1498 struct proc *p = td->td_proc;
1499 struct filedesc *fdp = p->p_fd;
1500 struct vnode *vp, *ovp;
1501 struct nchandle onch;
1502 int error;
1504 if ((error = nlookup(nd)) != 0)
1505 return (error);
1506 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1507 return (ENOENT);
1508 if ((error = vget(vp, LK_SHARED)) != 0)
1509 return (error);
1511 error = checkvp_chdir(vp, td);
1512 vn_unlock(vp);
1513 if (error == 0) {
1514 ovp = fdp->fd_cdir;
1515 onch = fdp->fd_ncdir;
1516 cache_unlock(&nd->nl_nch); /* leave reference intact */
1517 fdp->fd_ncdir = nd->nl_nch;
1518 fdp->fd_cdir = vp;
1519 cache_drop(&onch);
1520 vrele(ovp);
1521 cache_zero(&nd->nl_nch);
1522 } else {
1523 vrele(vp);
1525 return (error);
1529 * chdir_args(char *path)
1531 * Change current working directory (``.'').
1534 sys_chdir(struct chdir_args *uap)
1536 struct nlookupdata nd;
1537 int error;
1539 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1540 if (error == 0)
1541 error = kern_chdir(&nd);
1542 nlookup_done(&nd);
1543 return (error);
1547 * Helper function for raised chroot(2) security function: Refuse if
1548 * any filedescriptors are open directories.
1550 static int
1551 chroot_refuse_vdir_fds(struct filedesc *fdp)
1553 struct vnode *vp;
1554 struct file *fp;
1555 int error;
1556 int fd;
1558 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1559 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1560 continue;
1561 vp = (struct vnode *)fp->f_data;
1562 if (vp->v_type != VDIR) {
1563 fdrop(fp);
1564 continue;
1566 fdrop(fp);
1567 return(EPERM);
1569 return (0);
1573 * This sysctl determines if we will allow a process to chroot(2) if it
1574 * has a directory open:
1575 * 0: disallowed for all processes.
1576 * 1: allowed for processes that were not already chroot(2)'ed.
1577 * 2: allowed for all processes.
1580 static int chroot_allow_open_directories = 1;
1582 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1583 &chroot_allow_open_directories, 0, "");
1586 * chroot to the specified namecache entry. We obtain the vp from the
1587 * namecache data. The passed ncp must be locked and referenced and will
1588 * remain locked and referenced on return.
1591 kern_chroot(struct nchandle *nch)
1593 struct thread *td = curthread;
1594 struct proc *p = td->td_proc;
1595 struct filedesc *fdp = p->p_fd;
1596 struct vnode *vp;
1597 int error;
1600 * Only privileged user can chroot
1602 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1603 if (error)
1604 return (error);
1607 * Disallow open directory descriptors (fchdir() breakouts).
1609 if (chroot_allow_open_directories == 0 ||
1610 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1611 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1612 return (error);
1614 if ((vp = nch->ncp->nc_vp) == NULL)
1615 return (ENOENT);
1617 if ((error = vget(vp, LK_SHARED)) != 0)
1618 return (error);
1621 * Check the validity of vp as a directory to change to and
1622 * associate it with rdir/jdir.
1624 error = checkvp_chdir(vp, td);
1625 vn_unlock(vp); /* leave reference intact */
1626 if (error == 0) {
1627 vrele(fdp->fd_rdir);
1628 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1629 cache_drop(&fdp->fd_nrdir);
1630 cache_copy(nch, &fdp->fd_nrdir);
1631 if (fdp->fd_jdir == NULL) {
1632 fdp->fd_jdir = vp;
1633 vref(fdp->fd_jdir);
1634 cache_copy(nch, &fdp->fd_njdir);
1636 } else {
1637 vrele(vp);
1639 return (error);
1643 * chroot_args(char *path)
1645 * Change notion of root (``/'') directory.
1647 /* ARGSUSED */
1649 sys_chroot(struct chroot_args *uap)
1651 struct thread *td = curthread;
1652 struct nlookupdata nd;
1653 int error;
1655 KKASSERT(td->td_proc);
1656 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1657 if (error) {
1658 nlookup_done(&nd);
1659 return(error);
1661 nd.nl_flags |= NLC_EXEC;
1662 error = nlookup(&nd);
1663 if (error == 0)
1664 error = kern_chroot(&nd.nl_nch);
1665 nlookup_done(&nd);
1666 return(error);
1670 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1671 * determine whether it is legal to chdir to the vnode. The vnode's state
1672 * is not changed by this call.
1675 checkvp_chdir(struct vnode *vp, struct thread *td)
1677 int error;
1679 if (vp->v_type != VDIR)
1680 error = ENOTDIR;
1681 else
1682 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1683 return (error);
1687 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1689 struct thread *td = curthread;
1690 struct proc *p = td->td_proc;
1691 struct lwp *lp = td->td_lwp;
1692 struct filedesc *fdp = p->p_fd;
1693 int cmode, flags;
1694 struct file *nfp;
1695 struct file *fp;
1696 struct vnode *vp;
1697 int type, indx, error;
1698 struct flock lf;
1700 if ((oflags & O_ACCMODE) == O_ACCMODE)
1701 return (EINVAL);
1702 flags = FFLAGS(oflags);
1703 error = falloc(p, &nfp, NULL);
1704 if (error)
1705 return (error);
1706 fp = nfp;
1707 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1710 * XXX p_dupfd is a real mess. It allows a device to return a
1711 * file descriptor to be duplicated rather then doing the open
1712 * itself.
1714 lp->lwp_dupfd = -1;
1717 * Call vn_open() to do the lookup and assign the vnode to the
1718 * file pointer. vn_open() does not change the ref count on fp
1719 * and the vnode, on success, will be inherited by the file pointer
1720 * and unlocked.
1722 nd->nl_flags |= NLC_LOCKVP;
1723 error = vn_open(nd, fp, flags, cmode);
1724 nlookup_done(nd);
1725 if (error) {
1727 * handle special fdopen() case. bleh. dupfdopen() is
1728 * responsible for dropping the old contents of ofiles[indx]
1729 * if it succeeds.
1731 * Note that fsetfd() will add a ref to fp which represents
1732 * the fd_files[] assignment. We must still drop our
1733 * reference.
1735 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1736 if (fdalloc(p, 0, &indx) == 0) {
1737 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1738 if (error == 0) {
1739 *res = indx;
1740 fdrop(fp); /* our ref */
1741 return (0);
1743 fsetfd(p, NULL, indx);
1746 fdrop(fp); /* our ref */
1747 if (error == ERESTART)
1748 error = EINTR;
1749 return (error);
1753 * ref the vnode for ourselves so it can't be ripped out from under
1754 * is. XXX need an ND flag to request that the vnode be returned
1755 * anyway.
1757 * Reserve a file descriptor but do not assign it until the open
1758 * succeeds.
1760 vp = (struct vnode *)fp->f_data;
1761 vref(vp);
1762 if ((error = fdalloc(p, 0, &indx)) != 0) {
1763 fdrop(fp);
1764 vrele(vp);
1765 return (error);
1769 * If no error occurs the vp will have been assigned to the file
1770 * pointer.
1772 lp->lwp_dupfd = 0;
1774 if (flags & (O_EXLOCK | O_SHLOCK)) {
1775 lf.l_whence = SEEK_SET;
1776 lf.l_start = 0;
1777 lf.l_len = 0;
1778 if (flags & O_EXLOCK)
1779 lf.l_type = F_WRLCK;
1780 else
1781 lf.l_type = F_RDLCK;
1782 if (flags & FNONBLOCK)
1783 type = 0;
1784 else
1785 type = F_WAIT;
1787 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1789 * lock request failed. Clean up the reserved
1790 * descriptor.
1792 vrele(vp);
1793 fsetfd(p, NULL, indx);
1794 fdrop(fp);
1795 return (error);
1797 fp->f_flag |= FHASLOCK;
1799 #if 0
1801 * Assert that all regular file vnodes were created with a object.
1803 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1804 ("open: regular file has no backing object after vn_open"));
1805 #endif
1807 vrele(vp);
1810 * release our private reference, leaving the one associated with the
1811 * descriptor table intact.
1813 fsetfd(p, fp, indx);
1814 fdrop(fp);
1815 *res = indx;
1816 return (0);
1820 * open_args(char *path, int flags, int mode)
1822 * Check permissions, allocate an open file structure,
1823 * and call the device open routine if any.
1826 sys_open(struct open_args *uap)
1828 struct nlookupdata nd;
1829 int error;
1831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1832 if (error == 0) {
1833 error = kern_open(&nd, uap->flags,
1834 uap->mode, &uap->sysmsg_result);
1836 nlookup_done(&nd);
1837 return (error);
1841 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1843 struct thread *td = curthread;
1844 struct proc *p = td->td_proc;
1845 struct vnode *vp;
1846 struct vattr vattr;
1847 int error;
1848 int whiteout = 0;
1850 KKASSERT(p);
1852 VATTR_NULL(&vattr);
1853 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1854 vattr.va_rmajor = rmajor;
1855 vattr.va_rminor = rminor;
1857 switch (mode & S_IFMT) {
1858 case S_IFMT: /* used by badsect to flag bad sectors */
1859 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1860 vattr.va_type = VBAD;
1861 break;
1862 case S_IFCHR:
1863 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1864 vattr.va_type = VCHR;
1865 break;
1866 case S_IFBLK:
1867 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1868 vattr.va_type = VBLK;
1869 break;
1870 case S_IFWHT:
1871 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1872 whiteout = 1;
1873 break;
1874 case S_IFDIR: /* special directories support for HAMMER */
1875 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1876 vattr.va_type = VDIR;
1877 break;
1878 default:
1879 error = EINVAL;
1880 break;
1883 if (error)
1884 return (error);
1886 bwillinode(1);
1887 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1888 if ((error = nlookup(nd)) != 0)
1889 return (error);
1890 if (nd->nl_nch.ncp->nc_vp)
1891 return (EEXIST);
1892 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1893 return (error);
1895 if (whiteout) {
1896 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1897 nd->nl_cred, NAMEI_CREATE);
1898 } else {
1899 vp = NULL;
1900 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1901 &vp, nd->nl_cred, &vattr);
1902 if (error == 0)
1903 vput(vp);
1905 return (error);
1909 * mknod_args(char *path, int mode, int dev)
1911 * Create a special file.
1914 sys_mknod(struct mknod_args *uap)
1916 struct nlookupdata nd;
1917 int error;
1919 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1920 if (error == 0) {
1921 error = kern_mknod(&nd, uap->mode,
1922 umajor(uap->dev), uminor(uap->dev));
1924 nlookup_done(&nd);
1925 return (error);
1929 kern_mkfifo(struct nlookupdata *nd, int mode)
1931 struct thread *td = curthread;
1932 struct proc *p = td->td_proc;
1933 struct vattr vattr;
1934 struct vnode *vp;
1935 int error;
1937 bwillinode(1);
1939 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1940 if ((error = nlookup(nd)) != 0)
1941 return (error);
1942 if (nd->nl_nch.ncp->nc_vp)
1943 return (EEXIST);
1944 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1945 return (error);
1947 VATTR_NULL(&vattr);
1948 vattr.va_type = VFIFO;
1949 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1950 vp = NULL;
1951 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1952 if (error == 0)
1953 vput(vp);
1954 return (error);
1958 * mkfifo_args(char *path, int mode)
1960 * Create a named pipe.
1963 sys_mkfifo(struct mkfifo_args *uap)
1965 struct nlookupdata nd;
1966 int error;
1968 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1969 if (error == 0)
1970 error = kern_mkfifo(&nd, uap->mode);
1971 nlookup_done(&nd);
1972 return (error);
1975 static int hardlink_check_uid = 0;
1976 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1977 &hardlink_check_uid, 0,
1978 "Unprivileged processes cannot create hard links to files owned by other "
1979 "users");
1980 static int hardlink_check_gid = 0;
1981 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1982 &hardlink_check_gid, 0,
1983 "Unprivileged processes cannot create hard links to files owned by other "
1984 "groups");
1986 static int
1987 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1989 struct vattr va;
1990 int error;
1993 * Shortcut if disabled
1995 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1996 return (0);
1999 * Privileged user can always hardlink
2001 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2002 return (0);
2005 * Otherwise only if the originating file is owned by the
2006 * same user or group. Note that any group is allowed if
2007 * the file is owned by the caller.
2009 error = VOP_GETATTR(vp, &va);
2010 if (error != 0)
2011 return (error);
2013 if (hardlink_check_uid) {
2014 if (cred->cr_uid != va.va_uid)
2015 return (EPERM);
2018 if (hardlink_check_gid) {
2019 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2020 return (EPERM);
2023 return (0);
2027 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2029 struct thread *td = curthread;
2030 struct vnode *vp;
2031 int error;
2034 * Lookup the source and obtained a locked vnode.
2036 * You may only hardlink a file which you have write permission
2037 * on or which you own.
2039 * XXX relookup on vget failure / race ?
2041 bwillinode(1);
2042 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2043 if ((error = nlookup(nd)) != 0)
2044 return (error);
2045 vp = nd->nl_nch.ncp->nc_vp;
2046 KKASSERT(vp != NULL);
2047 if (vp->v_type == VDIR)
2048 return (EPERM); /* POSIX */
2049 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2050 return (error);
2051 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2052 return (error);
2055 * Unlock the source so we can lookup the target without deadlocking
2056 * (XXX vp is locked already, possible other deadlock?). The target
2057 * must not exist.
2059 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2060 nd->nl_flags &= ~NLC_NCPISLOCKED;
2061 cache_unlock(&nd->nl_nch);
2063 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2064 if ((error = nlookup(linknd)) != 0) {
2065 vput(vp);
2066 return (error);
2068 if (linknd->nl_nch.ncp->nc_vp) {
2069 vput(vp);
2070 return (EEXIST);
2074 * Finally run the new API VOP.
2076 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2077 if (error == 0) {
2078 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2079 vp, linknd->nl_cred);
2081 vput(vp);
2082 return (error);
2086 * link_args(char *path, char *link)
2088 * Make a hard file link.
2091 sys_link(struct link_args *uap)
2093 struct nlookupdata nd, linknd;
2094 int error;
2096 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2097 if (error == 0) {
2098 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2099 if (error == 0)
2100 error = kern_link(&nd, &linknd);
2101 nlookup_done(&linknd);
2103 nlookup_done(&nd);
2104 return (error);
2108 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2110 struct vattr vattr;
2111 struct vnode *vp;
2112 struct vnode *dvp;
2113 int error;
2115 bwillinode(1);
2116 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2117 if ((error = nlookup(nd)) != 0)
2118 return (error);
2119 if (nd->nl_nch.ncp->nc_vp)
2120 return (EEXIST);
2121 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2122 return (error);
2123 dvp = nd->nl_dvp;
2124 VATTR_NULL(&vattr);
2125 vattr.va_mode = mode;
2126 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2127 if (error == 0)
2128 vput(vp);
2129 return (error);
2133 * symlink(char *path, char *link)
2135 * Make a symbolic link.
2138 sys_symlink(struct symlink_args *uap)
2140 struct thread *td = curthread;
2141 struct nlookupdata nd;
2142 char *path;
2143 int error;
2144 int mode;
2146 path = objcache_get(namei_oc, M_WAITOK);
2147 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2148 if (error == 0) {
2149 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2150 if (error == 0) {
2151 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2152 error = kern_symlink(&nd, path, mode);
2154 nlookup_done(&nd);
2156 objcache_put(namei_oc, path);
2157 return (error);
2161 * undelete_args(char *path)
2163 * Delete a whiteout from the filesystem.
2165 /* ARGSUSED */
2167 sys_undelete(struct undelete_args *uap)
2169 struct nlookupdata nd;
2170 int error;
2172 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2173 bwillinode(1);
2174 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2175 if (error == 0)
2176 error = nlookup(&nd);
2177 if (error == 0)
2178 error = ncp_writechk(&nd.nl_nch);
2179 if (error == 0) {
2180 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2181 NAMEI_DELETE);
2183 nlookup_done(&nd);
2184 return (error);
2188 kern_unlink(struct nlookupdata *nd)
2190 int error;
2192 bwillinode(1);
2193 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2194 if ((error = nlookup(nd)) != 0)
2195 return (error);
2196 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2197 return (error);
2198 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2199 return (error);
2203 * unlink_args(char *path)
2205 * Delete a name from the filesystem.
2208 sys_unlink(struct unlink_args *uap)
2210 struct nlookupdata nd;
2211 int error;
2213 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2214 if (error == 0)
2215 error = kern_unlink(&nd);
2216 nlookup_done(&nd);
2217 return (error);
2221 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2223 struct thread *td = curthread;
2224 struct proc *p = td->td_proc;
2225 struct file *fp;
2226 struct vnode *vp;
2227 struct vattr vattr;
2228 off_t new_offset;
2229 int error;
2231 fp = holdfp(p->p_fd, fd, -1);
2232 if (fp == NULL)
2233 return (EBADF);
2234 if (fp->f_type != DTYPE_VNODE) {
2235 error = ESPIPE;
2236 goto done;
2238 vp = (struct vnode *)fp->f_data;
2240 switch (whence) {
2241 case L_INCR:
2242 new_offset = fp->f_offset + offset;
2243 error = 0;
2244 break;
2245 case L_XTND:
2246 error = VOP_GETATTR(vp, &vattr);
2247 new_offset = offset + vattr.va_size;
2248 break;
2249 case L_SET:
2250 new_offset = offset;
2251 error = 0;
2252 break;
2253 default:
2254 new_offset = 0;
2255 error = EINVAL;
2256 break;
2260 * Validate the seek position. Negative offsets are not allowed
2261 * for regular files or directories.
2263 * Normally we would also not want to allow negative offsets for
2264 * character and block-special devices. However kvm addresses
2265 * on 64 bit architectures might appear to be negative and must
2266 * be allowed.
2268 if (error == 0) {
2269 if (new_offset < 0 &&
2270 (vp->v_type == VREG || vp->v_type == VDIR)) {
2271 error = EINVAL;
2272 } else {
2273 fp->f_offset = new_offset;
2276 *res = fp->f_offset;
2277 done:
2278 fdrop(fp);
2279 return (error);
2283 * lseek_args(int fd, int pad, off_t offset, int whence)
2285 * Reposition read/write file offset.
2288 sys_lseek(struct lseek_args *uap)
2290 int error;
2292 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2293 &uap->sysmsg_offset);
2295 return (error);
2299 kern_access(struct nlookupdata *nd, int aflags)
2301 struct vnode *vp;
2302 int error, flags;
2304 if ((error = nlookup(nd)) != 0)
2305 return (error);
2306 retry:
2307 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2308 if (error)
2309 return (error);
2311 /* Flags == 0 means only check for existence. */
2312 if (aflags) {
2313 flags = 0;
2314 if (aflags & R_OK)
2315 flags |= VREAD;
2316 if (aflags & W_OK)
2317 flags |= VWRITE;
2318 if (aflags & X_OK)
2319 flags |= VEXEC;
2320 if ((flags & VWRITE) == 0 ||
2321 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2322 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2325 * If the file handle is stale we have to re-resolve the
2326 * entry. This is a hack at the moment.
2328 if (error == ESTALE) {
2329 vput(vp);
2330 cache_setunresolved(&nd->nl_nch);
2331 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2332 if (error == 0) {
2333 vp = NULL;
2334 goto retry;
2336 return(error);
2339 vput(vp);
2340 return (error);
2344 * access_args(char *path, int flags)
2346 * Check access permissions.
2349 sys_access(struct access_args *uap)
2351 struct nlookupdata nd;
2352 int error;
2354 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2355 if (error == 0)
2356 error = kern_access(&nd, uap->flags);
2357 nlookup_done(&nd);
2358 return (error);
2362 kern_stat(struct nlookupdata *nd, struct stat *st)
2364 int error;
2365 struct vnode *vp;
2366 thread_t td;
2368 if ((error = nlookup(nd)) != 0)
2369 return (error);
2370 again:
2371 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2372 return (ENOENT);
2374 td = curthread;
2375 if ((error = vget(vp, LK_SHARED)) != 0)
2376 return (error);
2377 error = vn_stat(vp, st, nd->nl_cred);
2380 * If the file handle is stale we have to re-resolve the entry. This
2381 * is a hack at the moment.
2383 if (error == ESTALE) {
2384 vput(vp);
2385 cache_setunresolved(&nd->nl_nch);
2386 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2387 if (error == 0)
2388 goto again;
2389 } else {
2390 vput(vp);
2392 return (error);
2396 * stat_args(char *path, struct stat *ub)
2398 * Get file status; this version follows links.
2401 sys_stat(struct stat_args *uap)
2403 struct nlookupdata nd;
2404 struct stat st;
2405 int error;
2407 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2408 if (error == 0) {
2409 error = kern_stat(&nd, &st);
2410 if (error == 0)
2411 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2413 nlookup_done(&nd);
2414 return (error);
2418 * lstat_args(char *path, struct stat *ub)
2420 * Get file status; this version does not follow links.
2423 sys_lstat(struct lstat_args *uap)
2425 struct nlookupdata nd;
2426 struct stat st;
2427 int error;
2429 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2430 if (error == 0) {
2431 error = kern_stat(&nd, &st);
2432 if (error == 0)
2433 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2435 nlookup_done(&nd);
2436 return (error);
2440 * pathconf_Args(char *path, int name)
2442 * Get configurable pathname variables.
2444 /* ARGSUSED */
2446 sys_pathconf(struct pathconf_args *uap)
2448 struct nlookupdata nd;
2449 struct vnode *vp;
2450 int error;
2452 vp = NULL;
2453 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2454 if (error == 0)
2455 error = nlookup(&nd);
2456 if (error == 0)
2457 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2458 nlookup_done(&nd);
2459 if (error == 0) {
2460 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2461 vput(vp);
2463 return (error);
2467 * XXX: daver
2468 * kern_readlink isn't properly split yet. There is a copyin burried
2469 * in VOP_READLINK().
2472 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2474 struct thread *td = curthread;
2475 struct proc *p = td->td_proc;
2476 struct vnode *vp;
2477 struct iovec aiov;
2478 struct uio auio;
2479 int error;
2481 if ((error = nlookup(nd)) != 0)
2482 return (error);
2483 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2484 if (error)
2485 return (error);
2486 if (vp->v_type != VLNK) {
2487 error = EINVAL;
2488 } else {
2489 aiov.iov_base = buf;
2490 aiov.iov_len = count;
2491 auio.uio_iov = &aiov;
2492 auio.uio_iovcnt = 1;
2493 auio.uio_offset = 0;
2494 auio.uio_rw = UIO_READ;
2495 auio.uio_segflg = UIO_USERSPACE;
2496 auio.uio_td = td;
2497 auio.uio_resid = count;
2498 error = VOP_READLINK(vp, &auio, p->p_ucred);
2500 vput(vp);
2501 *res = count - auio.uio_resid;
2502 return (error);
2506 * readlink_args(char *path, char *buf, int count)
2508 * Return target name of a symbolic link.
2511 sys_readlink(struct readlink_args *uap)
2513 struct nlookupdata nd;
2514 int error;
2516 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2517 if (error == 0) {
2518 error = kern_readlink(&nd, uap->buf, uap->count,
2519 &uap->sysmsg_result);
2521 nlookup_done(&nd);
2522 return (error);
2525 static int
2526 setfflags(struct vnode *vp, int flags)
2528 struct thread *td = curthread;
2529 struct proc *p = td->td_proc;
2530 int error;
2531 struct vattr vattr;
2534 * Prevent non-root users from setting flags on devices. When
2535 * a device is reused, users can retain ownership of the device
2536 * if they are allowed to set flags and programs assume that
2537 * chown can't fail when done as root.
2539 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2540 ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0))
2541 return (error);
2544 * note: vget is required for any operation that might mod the vnode
2545 * so VINACTIVE is properly cleared.
2547 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2548 VATTR_NULL(&vattr);
2549 vattr.va_flags = flags;
2550 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2551 vput(vp);
2553 return (error);
2557 * chflags(char *path, int flags)
2559 * Change flags of a file given a path name.
2561 /* ARGSUSED */
2563 sys_chflags(struct chflags_args *uap)
2565 struct nlookupdata nd;
2566 struct vnode *vp;
2567 int error;
2569 vp = NULL;
2570 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2571 if (error == 0)
2572 error = nlookup(&nd);
2573 if (error == 0)
2574 error = ncp_writechk(&nd.nl_nch);
2575 if (error == 0)
2576 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2577 nlookup_done(&nd);
2578 if (error == 0) {
2579 error = setfflags(vp, uap->flags);
2580 vrele(vp);
2582 return (error);
2586 * lchflags(char *path, int flags)
2588 * Change flags of a file given a path name, but don't follow symlinks.
2590 /* ARGSUSED */
2592 sys_lchflags(struct lchflags_args *uap)
2594 struct nlookupdata nd;
2595 struct vnode *vp;
2596 int error;
2598 vp = NULL;
2599 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2600 if (error == 0)
2601 error = nlookup(&nd);
2602 if (error == 0)
2603 error = ncp_writechk(&nd.nl_nch);
2604 if (error == 0)
2605 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2606 nlookup_done(&nd);
2607 if (error == 0) {
2608 error = setfflags(vp, uap->flags);
2609 vrele(vp);
2611 return (error);
2615 * fchflags_args(int fd, int flags)
2617 * Change flags of a file given a file descriptor.
2619 /* ARGSUSED */
2621 sys_fchflags(struct fchflags_args *uap)
2623 struct thread *td = curthread;
2624 struct proc *p = td->td_proc;
2625 struct file *fp;
2626 int error;
2628 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2629 return (error);
2630 if (fp->f_nchandle.ncp)
2631 error = ncp_writechk(&fp->f_nchandle);
2632 if (error == 0)
2633 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2634 fdrop(fp);
2635 return (error);
2638 static int
2639 setfmode(struct vnode *vp, int mode)
2641 struct thread *td = curthread;
2642 struct proc *p = td->td_proc;
2643 int error;
2644 struct vattr vattr;
2647 * note: vget is required for any operation that might mod the vnode
2648 * so VINACTIVE is properly cleared.
2650 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2651 VATTR_NULL(&vattr);
2652 vattr.va_mode = mode & ALLPERMS;
2653 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2654 vput(vp);
2656 return error;
2660 kern_chmod(struct nlookupdata *nd, int mode)
2662 struct vnode *vp;
2663 int error;
2665 if ((error = nlookup(nd)) != 0)
2666 return (error);
2667 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2668 return (error);
2669 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2670 error = setfmode(vp, mode);
2671 vrele(vp);
2672 return (error);
2676 * chmod_args(char *path, int mode)
2678 * Change mode of a file given path name.
2680 /* ARGSUSED */
2682 sys_chmod(struct chmod_args *uap)
2684 struct nlookupdata nd;
2685 int error;
2687 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2688 if (error == 0)
2689 error = kern_chmod(&nd, uap->mode);
2690 nlookup_done(&nd);
2691 return (error);
2695 * lchmod_args(char *path, int mode)
2697 * Change mode of a file given path name (don't follow links.)
2699 /* ARGSUSED */
2701 sys_lchmod(struct lchmod_args *uap)
2703 struct nlookupdata nd;
2704 int error;
2706 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2707 if (error == 0)
2708 error = kern_chmod(&nd, uap->mode);
2709 nlookup_done(&nd);
2710 return (error);
2714 * fchmod_args(int fd, int mode)
2716 * Change mode of a file given a file descriptor.
2718 /* ARGSUSED */
2720 sys_fchmod(struct fchmod_args *uap)
2722 struct thread *td = curthread;
2723 struct proc *p = td->td_proc;
2724 struct file *fp;
2725 int error;
2727 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2728 return (error);
2729 if (fp->f_nchandle.ncp)
2730 error = ncp_writechk(&fp->f_nchandle);
2731 if (error == 0)
2732 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2733 fdrop(fp);
2734 return (error);
2737 static int
2738 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2740 struct thread *td = curthread;
2741 struct proc *p = td->td_proc;
2742 int error;
2743 struct vattr vattr;
2746 * note: vget is required for any operation that might mod the vnode
2747 * so VINACTIVE is properly cleared.
2749 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2750 VATTR_NULL(&vattr);
2751 vattr.va_uid = uid;
2752 vattr.va_gid = gid;
2753 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2754 vput(vp);
2756 return error;
2760 kern_chown(struct nlookupdata *nd, int uid, int gid)
2762 struct vnode *vp;
2763 int error;
2765 if ((error = nlookup(nd)) != 0)
2766 return (error);
2767 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2768 return (error);
2769 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2770 error = setfown(vp, uid, gid);
2771 vrele(vp);
2772 return (error);
2776 * chown(char *path, int uid, int gid)
2778 * Set ownership given a path name.
2781 sys_chown(struct chown_args *uap)
2783 struct nlookupdata nd;
2784 int error;
2786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787 if (error == 0)
2788 error = kern_chown(&nd, uap->uid, uap->gid);
2789 nlookup_done(&nd);
2790 return (error);
2794 * lchown_args(char *path, int uid, int gid)
2796 * Set ownership given a path name, do not cross symlinks.
2799 sys_lchown(struct lchown_args *uap)
2801 struct nlookupdata nd;
2802 int error;
2804 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2805 if (error == 0)
2806 error = kern_chown(&nd, uap->uid, uap->gid);
2807 nlookup_done(&nd);
2808 return (error);
2812 * fchown_args(int fd, int uid, int gid)
2814 * Set ownership given a file descriptor.
2816 /* ARGSUSED */
2818 sys_fchown(struct fchown_args *uap)
2820 struct thread *td = curthread;
2821 struct proc *p = td->td_proc;
2822 struct file *fp;
2823 int error;
2825 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2826 return (error);
2827 if (fp->f_nchandle.ncp)
2828 error = ncp_writechk(&fp->f_nchandle);
2829 if (error == 0)
2830 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2831 fdrop(fp);
2832 return (error);
2835 static int
2836 getutimes(const struct timeval *tvp, struct timespec *tsp)
2838 struct timeval tv[2];
2840 if (tvp == NULL) {
2841 microtime(&tv[0]);
2842 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2843 tsp[1] = tsp[0];
2844 } else {
2845 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2846 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2848 return 0;
2851 static int
2852 setutimes(struct vnode *vp, struct vattr *vattr,
2853 const struct timespec *ts, int nullflag)
2855 struct thread *td = curthread;
2856 struct proc *p = td->td_proc;
2857 int error;
2859 VATTR_NULL(vattr);
2860 vattr->va_atime = ts[0];
2861 vattr->va_mtime = ts[1];
2862 if (nullflag)
2863 vattr->va_vaflags |= VA_UTIMES_NULL;
2864 error = VOP_SETATTR(vp, vattr, p->p_ucred);
2866 return error;
2870 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2872 struct timespec ts[2];
2873 struct vnode *vp;
2874 struct vattr vattr;
2875 int error;
2877 if ((error = getutimes(tptr, ts)) != 0)
2878 return (error);
2881 * NOTE: utimes() succeeds for the owner even if the file
2882 * is not user-writable.
2884 nd->nl_flags |= NLC_OWN | NLC_WRITE;
2886 if ((error = nlookup(nd)) != 0)
2887 return (error);
2888 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2889 return (error);
2890 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2891 return (error);
2894 * note: vget is required for any operation that might mod the vnode
2895 * so VINACTIVE is properly cleared.
2897 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
2898 error = vget(vp, LK_EXCLUSIVE);
2899 if (error == 0) {
2900 error = setutimes(vp, &vattr, ts, (tptr == NULL));
2901 vput(vp);
2904 vrele(vp);
2905 return (error);
2909 * utimes_args(char *path, struct timeval *tptr)
2911 * Set the access and modification times of a file.
2914 sys_utimes(struct utimes_args *uap)
2916 struct timeval tv[2];
2917 struct nlookupdata nd;
2918 int error;
2920 if (uap->tptr) {
2921 error = copyin(uap->tptr, tv, sizeof(tv));
2922 if (error)
2923 return (error);
2925 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2926 if (error == 0)
2927 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2928 nlookup_done(&nd);
2929 return (error);
2933 * lutimes_args(char *path, struct timeval *tptr)
2935 * Set the access and modification times of a file.
2938 sys_lutimes(struct lutimes_args *uap)
2940 struct timeval tv[2];
2941 struct nlookupdata nd;
2942 int error;
2944 if (uap->tptr) {
2945 error = copyin(uap->tptr, tv, sizeof(tv));
2946 if (error)
2947 return (error);
2949 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2950 if (error == 0)
2951 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2952 nlookup_done(&nd);
2953 return (error);
2957 * Set utimes on a file descriptor. The creds used to open the
2958 * file are used to determine whether the operation is allowed
2959 * or not.
2962 kern_futimes(int fd, struct timeval *tptr)
2964 struct thread *td = curthread;
2965 struct proc *p = td->td_proc;
2966 struct timespec ts[2];
2967 struct file *fp;
2968 struct vnode *vp;
2969 struct vattr vattr;
2970 int error;
2972 error = getutimes(tptr, ts);
2973 if (error)
2974 return (error);
2975 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2976 return (error);
2977 if (fp->f_nchandle.ncp)
2978 error = ncp_writechk(&fp->f_nchandle);
2979 if (error == 0) {
2980 vp = fp->f_data;
2981 error = vget(vp, LK_EXCLUSIVE);
2982 if (error == 0) {
2983 error = VOP_GETATTR(vp, &vattr);
2984 if (error == 0) {
2985 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
2986 fp->f_cred);
2988 if (error == 0) {
2989 error = setutimes(vp, &vattr, ts,
2990 (tptr == NULL));
2992 vput(vp);
2995 fdrop(fp);
2996 return (error);
3000 * futimes_args(int fd, struct timeval *tptr)
3002 * Set the access and modification times of a file.
3005 sys_futimes(struct futimes_args *uap)
3007 struct timeval tv[2];
3008 int error;
3010 if (uap->tptr) {
3011 error = copyin(uap->tptr, tv, sizeof(tv));
3012 if (error)
3013 return (error);
3016 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3018 return (error);
3022 kern_truncate(struct nlookupdata *nd, off_t length)
3024 struct vnode *vp;
3025 struct vattr vattr;
3026 int error;
3028 if (length < 0)
3029 return(EINVAL);
3030 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3031 if ((error = nlookup(nd)) != 0)
3032 return (error);
3033 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3034 return (error);
3035 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3036 return (error);
3037 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3038 vrele(vp);
3039 return (error);
3041 if (vp->v_type == VDIR) {
3042 error = EISDIR;
3043 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3044 VATTR_NULL(&vattr);
3045 vattr.va_size = length;
3046 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3048 vput(vp);
3049 return (error);
3053 * truncate(char *path, int pad, off_t length)
3055 * Truncate a file given its path name.
3058 sys_truncate(struct truncate_args *uap)
3060 struct nlookupdata nd;
3061 int error;
3063 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3064 if (error == 0)
3065 error = kern_truncate(&nd, uap->length);
3066 nlookup_done(&nd);
3067 return error;
3071 kern_ftruncate(int fd, off_t length)
3073 struct thread *td = curthread;
3074 struct proc *p = td->td_proc;
3075 struct vattr vattr;
3076 struct vnode *vp;
3077 struct file *fp;
3078 int error;
3080 if (length < 0)
3081 return(EINVAL);
3082 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3083 return (error);
3084 if (fp->f_nchandle.ncp) {
3085 error = ncp_writechk(&fp->f_nchandle);
3086 if (error)
3087 goto done;
3089 if ((fp->f_flag & FWRITE) == 0) {
3090 error = EINVAL;
3091 goto done;
3093 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3094 error = EINVAL;
3095 goto done;
3097 vp = (struct vnode *)fp->f_data;
3098 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3099 if (vp->v_type == VDIR) {
3100 error = EISDIR;
3101 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3102 VATTR_NULL(&vattr);
3103 vattr.va_size = length;
3104 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3106 vn_unlock(vp);
3107 done:
3108 fdrop(fp);
3109 return (error);
3113 * ftruncate_args(int fd, int pad, off_t length)
3115 * Truncate a file given a file descriptor.
3118 sys_ftruncate(struct ftruncate_args *uap)
3120 int error;
3122 error = kern_ftruncate(uap->fd, uap->length);
3124 return (error);
3128 * fsync(int fd)
3130 * Sync an open file.
3132 /* ARGSUSED */
3134 sys_fsync(struct fsync_args *uap)
3136 struct thread *td = curthread;
3137 struct proc *p = td->td_proc;
3138 struct vnode *vp;
3139 struct file *fp;
3140 vm_object_t obj;
3141 int error;
3143 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3144 return (error);
3145 vp = (struct vnode *)fp->f_data;
3146 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3147 if ((obj = vp->v_object) != NULL)
3148 vm_object_page_clean(obj, 0, 0, 0);
3149 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3150 error = buf_fsync(vp);
3151 vn_unlock(vp);
3152 fdrop(fp);
3153 return (error);
3157 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3159 struct nchandle fnchd;
3160 struct nchandle tnchd;
3161 struct namecache *ncp;
3162 struct vnode *fdvp;
3163 struct vnode *tdvp;
3164 struct mount *mp;
3165 int error;
3167 bwillinode(1);
3168 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3169 if ((error = nlookup(fromnd)) != 0)
3170 return (error);
3171 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3172 return (ENOENT);
3173 fnchd.mount = fromnd->nl_nch.mount;
3174 cache_hold(&fnchd);
3177 * unlock the source nch so we can lookup the target nch without
3178 * deadlocking. The target may or may not exist so we do not check
3179 * for a target vp like kern_mkdir() and other creation functions do.
3181 * The source and target directories are ref'd and rechecked after
3182 * everything is relocked to determine if the source or target file
3183 * has been renamed.
3185 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3186 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3187 cache_unlock(&fromnd->nl_nch);
3189 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3190 if ((error = nlookup(tond)) != 0) {
3191 cache_drop(&fnchd);
3192 return (error);
3194 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3195 cache_drop(&fnchd);
3196 return (ENOENT);
3198 tnchd.mount = tond->nl_nch.mount;
3199 cache_hold(&tnchd);
3202 * If the source and target are the same there is nothing to do
3204 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3205 cache_drop(&fnchd);
3206 cache_drop(&tnchd);
3207 return (0);
3211 * Mount points cannot be renamed or overwritten
3213 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3214 NCF_ISMOUNTPT
3216 cache_drop(&fnchd);
3217 cache_drop(&tnchd);
3218 return (EINVAL);
3222 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3223 * may have become invalid while it was unlocked, nc_vp and nc_mount
3224 * could be NULL.
3226 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3227 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3228 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3229 cache_lock(&fromnd->nl_nch);
3230 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3231 } else {
3232 cache_unlock(&tond->nl_nch);
3233 cache_lock(&fromnd->nl_nch);
3234 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3235 cache_lock(&tond->nl_nch);
3236 cache_resolve(&tond->nl_nch, tond->nl_cred);
3238 fromnd->nl_flags |= NLC_NCPISLOCKED;
3241 * make sure the parent directories linkages are the same
3243 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3244 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3245 cache_drop(&fnchd);
3246 cache_drop(&tnchd);
3247 return (ENOENT);
3251 * Both the source and target must be within the same filesystem and
3252 * in the same filesystem as their parent directories within the
3253 * namecache topology.
3255 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3257 mp = fnchd.mount;
3258 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3259 mp != tond->nl_nch.mount) {
3260 cache_drop(&fnchd);
3261 cache_drop(&tnchd);
3262 return (EXDEV);
3266 * Make sure the mount point is writable
3268 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3269 cache_drop(&fnchd);
3270 cache_drop(&tnchd);
3271 return (error);
3275 * If the target exists and either the source or target is a directory,
3276 * then both must be directories.
3278 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3279 * have become NULL.
3281 if (tond->nl_nch.ncp->nc_vp) {
3282 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3283 error = ENOENT;
3284 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3285 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3286 error = ENOTDIR;
3287 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3288 error = EISDIR;
3293 * You cannot rename a source into itself or a subdirectory of itself.
3294 * We check this by travsersing the target directory upwards looking
3295 * for a match against the source.
3297 if (error == 0) {
3298 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3299 if (fromnd->nl_nch.ncp == ncp) {
3300 error = EINVAL;
3301 break;
3306 cache_drop(&fnchd);
3307 cache_drop(&tnchd);
3310 * Even though the namespaces are different, they may still represent
3311 * hardlinks to the same file. The filesystem might have a hard time
3312 * with this so we issue a NREMOVE of the source instead of a NRENAME
3313 * when we detect the situation.
3315 if (error == 0) {
3316 fdvp = fromnd->nl_dvp;
3317 tdvp = tond->nl_dvp;
3318 if (fdvp == NULL || tdvp == NULL) {
3319 error = EPERM;
3320 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3321 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3322 fromnd->nl_cred);
3323 } else {
3324 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3325 fdvp, tdvp, tond->nl_cred);
3328 return (error);
3332 * rename_args(char *from, char *to)
3334 * Rename files. Source and destination must either both be directories,
3335 * or both not be directories. If target is a directory, it must be empty.
3338 sys_rename(struct rename_args *uap)
3340 struct nlookupdata fromnd, tond;
3341 int error;
3343 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3344 if (error == 0) {
3345 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3346 if (error == 0)
3347 error = kern_rename(&fromnd, &tond);
3348 nlookup_done(&tond);
3350 nlookup_done(&fromnd);
3351 return (error);
3355 kern_mkdir(struct nlookupdata *nd, int mode)
3357 struct thread *td = curthread;
3358 struct proc *p = td->td_proc;
3359 struct vnode *vp;
3360 struct vattr vattr;
3361 int error;
3363 bwillinode(1);
3364 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3365 if ((error = nlookup(nd)) != 0)
3366 return (error);
3368 if (nd->nl_nch.ncp->nc_vp)
3369 return (EEXIST);
3370 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3371 return (error);
3372 VATTR_NULL(&vattr);
3373 vattr.va_type = VDIR;
3374 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3376 vp = NULL;
3377 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3378 if (error == 0)
3379 vput(vp);
3380 return (error);
3384 * mkdir_args(char *path, int mode)
3386 * Make a directory file.
3388 /* ARGSUSED */
3390 sys_mkdir(struct mkdir_args *uap)
3392 struct nlookupdata nd;
3393 int error;
3395 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3396 if (error == 0)
3397 error = kern_mkdir(&nd, uap->mode);
3398 nlookup_done(&nd);
3399 return (error);
3403 kern_rmdir(struct nlookupdata *nd)
3405 int error;
3407 bwillinode(1);
3408 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3409 if ((error = nlookup(nd)) != 0)
3410 return (error);
3413 * Do not allow directories representing mount points to be
3414 * deleted, even if empty. Check write perms on mount point
3415 * in case the vnode is aliased (aka nullfs).
3417 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3418 return (EINVAL);
3419 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3420 return (error);
3421 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3422 return (error);
3426 * rmdir_args(char *path)
3428 * Remove a directory file.
3430 /* ARGSUSED */
3432 sys_rmdir(struct rmdir_args *uap)
3434 struct nlookupdata nd;
3435 int error;
3437 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3438 if (error == 0)
3439 error = kern_rmdir(&nd);
3440 nlookup_done(&nd);
3441 return (error);
3445 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3446 enum uio_seg direction)
3448 struct thread *td = curthread;
3449 struct proc *p = td->td_proc;
3450 struct vnode *vp;
3451 struct file *fp;
3452 struct uio auio;
3453 struct iovec aiov;
3454 off_t loff;
3455 int error, eofflag;
3457 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3458 return (error);
3459 if ((fp->f_flag & FREAD) == 0) {
3460 error = EBADF;
3461 goto done;
3463 vp = (struct vnode *)fp->f_data;
3464 unionread:
3465 if (vp->v_type != VDIR) {
3466 error = EINVAL;
3467 goto done;
3469 aiov.iov_base = buf;
3470 aiov.iov_len = count;
3471 auio.uio_iov = &aiov;
3472 auio.uio_iovcnt = 1;
3473 auio.uio_rw = UIO_READ;
3474 auio.uio_segflg = direction;
3475 auio.uio_td = td;
3476 auio.uio_resid = count;
3477 loff = auio.uio_offset = fp->f_offset;
3478 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3479 fp->f_offset = auio.uio_offset;
3480 if (error)
3481 goto done;
3482 if (count == auio.uio_resid) {
3483 if (union_dircheckp) {
3484 error = union_dircheckp(td, &vp, fp);
3485 if (error == -1)
3486 goto unionread;
3487 if (error)
3488 goto done;
3490 #if 0
3491 if ((vp->v_flag & VROOT) &&
3492 (vp->v_mount->mnt_flag & MNT_UNION)) {
3493 struct vnode *tvp = vp;
3494 vp = vp->v_mount->mnt_vnodecovered;
3495 vref(vp);
3496 fp->f_data = vp;
3497 fp->f_offset = 0;
3498 vrele(tvp);
3499 goto unionread;
3501 #endif
3505 * WARNING! *basep may not be wide enough to accomodate the
3506 * seek offset. XXX should we hack this to return the upper 32 bits
3507 * for offsets greater then 4G?
3509 if (basep) {
3510 *basep = (long)loff;
3512 *res = count - auio.uio_resid;
3513 done:
3514 fdrop(fp);
3515 return (error);
3519 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3521 * Read a block of directory entries in a file system independent format.
3524 sys_getdirentries(struct getdirentries_args *uap)
3526 long base;
3527 int error;
3529 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3530 &uap->sysmsg_result, UIO_USERSPACE);
3532 if (error == 0 && uap->basep)
3533 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3534 return (error);
3538 * getdents_args(int fd, char *buf, size_t count)
3541 sys_getdents(struct getdents_args *uap)
3543 int error;
3545 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3546 &uap->sysmsg_result, UIO_USERSPACE);
3548 return (error);
3552 * umask(int newmask)
3554 * Set the mode mask for creation of filesystem nodes.
3556 * MP SAFE
3559 sys_umask(struct umask_args *uap)
3561 struct thread *td = curthread;
3562 struct proc *p = td->td_proc;
3563 struct filedesc *fdp;
3565 fdp = p->p_fd;
3566 uap->sysmsg_result = fdp->fd_cmask;
3567 fdp->fd_cmask = uap->newmask & ALLPERMS;
3568 return (0);
3572 * revoke(char *path)
3574 * Void all references to file by ripping underlying filesystem
3575 * away from vnode.
3577 /* ARGSUSED */
3579 sys_revoke(struct revoke_args *uap)
3581 struct nlookupdata nd;
3582 struct vattr vattr;
3583 struct vnode *vp;
3584 struct ucred *cred;
3585 int error;
3587 vp = NULL;
3588 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3589 if (error == 0)
3590 error = nlookup(&nd);
3591 if (error == 0)
3592 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3593 cred = crhold(nd.nl_cred);
3594 nlookup_done(&nd);
3595 if (error == 0) {
3596 if (error == 0)
3597 error = VOP_GETATTR(vp, &vattr);
3598 if (error == 0 && cred->cr_uid != vattr.va_uid)
3599 error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
3600 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3601 if (count_udev(vp->v_umajor, vp->v_uminor) > 0)
3602 error = vrevoke(vp, cred);
3603 } else if (error == 0) {
3604 error = vrevoke(vp, cred);
3606 vrele(vp);
3608 if (cred)
3609 crfree(cred);
3610 return (error);
3614 * getfh_args(char *fname, fhandle_t *fhp)
3616 * Get (NFS) file handle
3618 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3619 * mount. This allows nullfs mounts to be explicitly exported.
3621 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3623 * nullfs mounts of subdirectories are not safe. That is, it will
3624 * work, but you do not really have protection against access to
3625 * the related parent directories.
3628 sys_getfh(struct getfh_args *uap)
3630 struct thread *td = curthread;
3631 struct nlookupdata nd;
3632 fhandle_t fh;
3633 struct vnode *vp;
3634 struct mount *mp;
3635 int error;
3638 * Must be super user
3640 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3641 return (error);
3643 vp = NULL;
3644 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3645 if (error == 0)
3646 error = nlookup(&nd);
3647 if (error == 0)
3648 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3649 mp = nd.nl_nch.mount;
3650 nlookup_done(&nd);
3651 if (error == 0) {
3652 bzero(&fh, sizeof(fh));
3653 fh.fh_fsid = mp->mnt_stat.f_fsid;
3654 error = VFS_VPTOFH(vp, &fh.fh_fid);
3655 vput(vp);
3656 if (error == 0)
3657 error = copyout(&fh, uap->fhp, sizeof(fh));
3659 return (error);
3663 * fhopen_args(const struct fhandle *u_fhp, int flags)
3665 * syscall for the rpc.lockd to use to translate a NFS file handle into
3666 * an open descriptor.
3668 * warning: do not remove the priv_check() call or this becomes one giant
3669 * security hole.
3672 sys_fhopen(struct fhopen_args *uap)
3674 struct thread *td = curthread;
3675 struct proc *p = td->td_proc;
3676 struct mount *mp;
3677 struct vnode *vp;
3678 struct fhandle fhp;
3679 struct vattr vat;
3680 struct vattr *vap = &vat;
3681 struct flock lf;
3682 int fmode, mode, error, type;
3683 struct file *nfp;
3684 struct file *fp;
3685 int indx;
3688 * Must be super user
3690 error = priv_check(td, PRIV_ROOT);
3691 if (error)
3692 return (error);
3694 fmode = FFLAGS(uap->flags);
3695 /* why not allow a non-read/write open for our lockd? */
3696 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3697 return (EINVAL);
3698 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3699 if (error)
3700 return(error);
3701 /* find the mount point */
3702 mp = vfs_getvfs(&fhp.fh_fsid);
3703 if (mp == NULL)
3704 return (ESTALE);
3705 /* now give me my vnode, it gets returned to me locked */
3706 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3707 if (error)
3708 return (error);
3710 * from now on we have to make sure not
3711 * to forget about the vnode
3712 * any error that causes an abort must vput(vp)
3713 * just set error = err and 'goto bad;'.
3717 * from vn_open
3719 if (vp->v_type == VLNK) {
3720 error = EMLINK;
3721 goto bad;
3723 if (vp->v_type == VSOCK) {
3724 error = EOPNOTSUPP;
3725 goto bad;
3727 mode = 0;
3728 if (fmode & (FWRITE | O_TRUNC)) {
3729 if (vp->v_type == VDIR) {
3730 error = EISDIR;
3731 goto bad;
3733 error = vn_writechk(vp, NULL);
3734 if (error)
3735 goto bad;
3736 mode |= VWRITE;
3738 if (fmode & FREAD)
3739 mode |= VREAD;
3740 if (mode) {
3741 error = VOP_ACCESS(vp, mode, p->p_ucred);
3742 if (error)
3743 goto bad;
3745 if (fmode & O_TRUNC) {
3746 vn_unlock(vp); /* XXX */
3747 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3748 VATTR_NULL(vap);
3749 vap->va_size = 0;
3750 error = VOP_SETATTR(vp, vap, p->p_ucred);
3751 if (error)
3752 goto bad;
3756 * VOP_OPEN needs the file pointer so it can potentially override
3757 * it.
3759 * WARNING! no f_nchandle will be associated when fhopen()ing a
3760 * directory. XXX
3762 if ((error = falloc(p, &nfp, &indx)) != 0)
3763 goto bad;
3764 fp = nfp;
3766 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3767 if (error) {
3769 * setting f_ops this way prevents VOP_CLOSE from being
3770 * called or fdrop() releasing the vp from v_data. Since
3771 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3773 fp->f_ops = &badfileops;
3774 fp->f_data = NULL;
3775 goto bad_drop;
3779 * The fp is given its own reference, we still have our ref and lock.
3781 * Assert that all regular files must be created with a VM object.
3783 if (vp->v_type == VREG && vp->v_object == NULL) {
3784 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3785 goto bad_drop;
3789 * The open was successful. Handle any locking requirements.
3791 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3792 lf.l_whence = SEEK_SET;
3793 lf.l_start = 0;
3794 lf.l_len = 0;
3795 if (fmode & O_EXLOCK)
3796 lf.l_type = F_WRLCK;
3797 else
3798 lf.l_type = F_RDLCK;
3799 if (fmode & FNONBLOCK)
3800 type = 0;
3801 else
3802 type = F_WAIT;
3803 vn_unlock(vp);
3804 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3806 * release our private reference.
3808 fsetfd(p, NULL, indx);
3809 fdrop(fp);
3810 vrele(vp);
3811 return (error);
3813 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3814 fp->f_flag |= FHASLOCK;
3818 * Clean up. Associate the file pointer with the previously
3819 * reserved descriptor and return it.
3821 vput(vp);
3822 fsetfd(p, fp, indx);
3823 fdrop(fp);
3824 uap->sysmsg_result = indx;
3825 return (0);
3827 bad_drop:
3828 fsetfd(p, NULL, indx);
3829 fdrop(fp);
3830 bad:
3831 vput(vp);
3832 return (error);
3836 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3839 sys_fhstat(struct fhstat_args *uap)
3841 struct thread *td = curthread;
3842 struct stat sb;
3843 fhandle_t fh;
3844 struct mount *mp;
3845 struct vnode *vp;
3846 int error;
3849 * Must be super user
3851 error = priv_check(td, PRIV_ROOT);
3852 if (error)
3853 return (error);
3855 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3856 if (error)
3857 return (error);
3859 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3860 return (ESTALE);
3861 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3862 return (error);
3863 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3864 vput(vp);
3865 if (error)
3866 return (error);
3867 error = copyout(&sb, uap->sb, sizeof(sb));
3868 return (error);
3872 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3875 sys_fhstatfs(struct fhstatfs_args *uap)
3877 struct thread *td = curthread;
3878 struct proc *p = td->td_proc;
3879 struct statfs *sp;
3880 struct mount *mp;
3881 struct vnode *vp;
3882 struct statfs sb;
3883 char *fullpath, *freepath;
3884 fhandle_t fh;
3885 int error;
3888 * Must be super user
3890 if ((error = priv_check(td, PRIV_ROOT)))
3891 return (error);
3893 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3894 return (error);
3896 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3897 return (ESTALE);
3899 if (p != NULL && !chroot_visible_mnt(mp, p))
3900 return (ESTALE);
3902 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3903 return (error);
3904 mp = vp->v_mount;
3905 sp = &mp->mnt_stat;
3906 vput(vp);
3907 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3908 return (error);
3910 error = mount_path(p, mp, &fullpath, &freepath);
3911 if (error)
3912 return(error);
3913 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3914 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3915 kfree(freepath, M_TEMP);
3917 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3918 if (priv_check(td, PRIV_ROOT)) {
3919 bcopy(sp, &sb, sizeof(sb));
3920 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3921 sp = &sb;
3923 return (copyout(sp, uap->buf, sizeof(*sp)));
3927 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3930 sys_fhstatvfs(struct fhstatvfs_args *uap)
3932 struct thread *td = curthread;
3933 struct proc *p = td->td_proc;
3934 struct statvfs *sp;
3935 struct mount *mp;
3936 struct vnode *vp;
3937 fhandle_t fh;
3938 int error;
3941 * Must be super user
3943 if ((error = priv_check(td, PRIV_ROOT)))
3944 return (error);
3946 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3947 return (error);
3949 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3950 return (ESTALE);
3952 if (p != NULL && !chroot_visible_mnt(mp, p))
3953 return (ESTALE);
3955 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3956 return (error);
3957 mp = vp->v_mount;
3958 sp = &mp->mnt_vstat;
3959 vput(vp);
3960 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3961 return (error);
3963 sp->f_flag = 0;
3964 if (mp->mnt_flag & MNT_RDONLY)
3965 sp->f_flag |= ST_RDONLY;
3966 if (mp->mnt_flag & MNT_NOSUID)
3967 sp->f_flag |= ST_NOSUID;
3969 return (copyout(sp, uap->buf, sizeof(*sp)));
3974 * Syscall to push extended attribute configuration information into the
3975 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3976 * a command (int cmd), and attribute name and misc data. For now, the
3977 * attribute name is left in userspace for consumption by the VFS_op.
3978 * It will probably be changed to be copied into sysspace by the
3979 * syscall in the future, once issues with various consumers of the
3980 * attribute code have raised their hands.
3982 * Currently this is used only by UFS Extended Attributes.
3985 sys_extattrctl(struct extattrctl_args *uap)
3987 struct nlookupdata nd;
3988 struct mount *mp;
3989 struct vnode *vp;
3990 int error;
3992 vp = NULL;
3993 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3994 if (error == 0)
3995 error = nlookup(&nd);
3996 if (error == 0) {
3997 mp = nd.nl_nch.mount;
3998 error = VFS_EXTATTRCTL(mp, uap->cmd,
3999 uap->attrname, uap->arg,
4000 nd.nl_cred);
4002 nlookup_done(&nd);
4003 return (error);
4007 * Syscall to set a named extended attribute on a file or directory.
4008 * Accepts attribute name, and a uio structure pointing to the data to set.
4009 * The uio is consumed in the style of writev(). The real work happens
4010 * in VOP_SETEXTATTR().
4013 sys_extattr_set_file(struct extattr_set_file_args *uap)
4015 char attrname[EXTATTR_MAXNAMELEN];
4016 struct iovec aiov[UIO_SMALLIOV];
4017 struct iovec *needfree;
4018 struct nlookupdata nd;
4019 struct iovec *iov;
4020 struct vnode *vp;
4021 struct uio auio;
4022 u_int iovlen;
4023 u_int cnt;
4024 int error;
4025 int i;
4027 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4028 if (error)
4029 return (error);
4031 vp = NULL;
4032 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4033 if (error == 0)
4034 error = nlookup(&nd);
4035 if (error == 0)
4036 error = ncp_writechk(&nd.nl_nch);
4037 if (error == 0)
4038 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4039 if (error) {
4040 nlookup_done(&nd);
4041 return (error);
4044 needfree = NULL;
4045 iovlen = uap->iovcnt * sizeof(struct iovec);
4046 if (uap->iovcnt > UIO_SMALLIOV) {
4047 if (uap->iovcnt > UIO_MAXIOV) {
4048 error = EINVAL;
4049 goto done;
4051 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4052 needfree = iov;
4053 } else {
4054 iov = aiov;
4056 auio.uio_iov = iov;
4057 auio.uio_iovcnt = uap->iovcnt;
4058 auio.uio_rw = UIO_WRITE;
4059 auio.uio_segflg = UIO_USERSPACE;
4060 auio.uio_td = nd.nl_td;
4061 auio.uio_offset = 0;
4062 if ((error = copyin(uap->iovp, iov, iovlen)))
4063 goto done;
4064 auio.uio_resid = 0;
4065 for (i = 0; i < uap->iovcnt; i++) {
4066 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4067 error = EINVAL;
4068 goto done;
4070 auio.uio_resid += iov->iov_len;
4071 iov++;
4073 cnt = auio.uio_resid;
4074 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4075 cnt -= auio.uio_resid;
4076 uap->sysmsg_result = cnt;
4077 done:
4078 vput(vp);
4079 nlookup_done(&nd);
4080 if (needfree)
4081 FREE(needfree, M_IOV);
4082 return (error);
4086 * Syscall to get a named extended attribute on a file or directory.
4087 * Accepts attribute name, and a uio structure pointing to a buffer for the
4088 * data. The uio is consumed in the style of readv(). The real work
4089 * happens in VOP_GETEXTATTR();
4092 sys_extattr_get_file(struct extattr_get_file_args *uap)
4094 char attrname[EXTATTR_MAXNAMELEN];
4095 struct iovec aiov[UIO_SMALLIOV];
4096 struct iovec *needfree;
4097 struct nlookupdata nd;
4098 struct iovec *iov;
4099 struct vnode *vp;
4100 struct uio auio;
4101 u_int iovlen;
4102 u_int cnt;
4103 int error;
4104 int i;
4106 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4107 if (error)
4108 return (error);
4110 vp = NULL;
4111 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4112 if (error == 0)
4113 error = nlookup(&nd);
4114 if (error == 0)
4115 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4116 if (error) {
4117 nlookup_done(&nd);
4118 return (error);
4121 iovlen = uap->iovcnt * sizeof (struct iovec);
4122 needfree = NULL;
4123 if (uap->iovcnt > UIO_SMALLIOV) {
4124 if (uap->iovcnt > UIO_MAXIOV) {
4125 error = EINVAL;
4126 goto done;
4128 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4129 needfree = iov;
4130 } else {
4131 iov = aiov;
4133 auio.uio_iov = iov;
4134 auio.uio_iovcnt = uap->iovcnt;
4135 auio.uio_rw = UIO_READ;
4136 auio.uio_segflg = UIO_USERSPACE;
4137 auio.uio_td = nd.nl_td;
4138 auio.uio_offset = 0;
4139 if ((error = copyin(uap->iovp, iov, iovlen)))
4140 goto done;
4141 auio.uio_resid = 0;
4142 for (i = 0; i < uap->iovcnt; i++) {
4143 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4144 error = EINVAL;
4145 goto done;
4147 auio.uio_resid += iov->iov_len;
4148 iov++;
4150 cnt = auio.uio_resid;
4151 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4152 cnt -= auio.uio_resid;
4153 uap->sysmsg_result = cnt;
4154 done:
4155 vput(vp);
4156 nlookup_done(&nd);
4157 if (needfree)
4158 FREE(needfree, M_IOV);
4159 return(error);
4163 * Syscall to delete a named extended attribute from a file or directory.
4164 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4167 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4169 char attrname[EXTATTR_MAXNAMELEN];
4170 struct nlookupdata nd;
4171 struct vnode *vp;
4172 int error;
4174 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4175 if (error)
4176 return(error);
4178 vp = NULL;
4179 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4180 if (error == 0)
4181 error = nlookup(&nd);
4182 if (error == 0)
4183 error = ncp_writechk(&nd.nl_nch);
4184 if (error == 0)
4185 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4186 if (error) {
4187 nlookup_done(&nd);
4188 return (error);
4191 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4192 vput(vp);
4193 nlookup_done(&nd);
4194 return(error);
4198 * Determine if the mount is visible to the process.
4200 static int
4201 chroot_visible_mnt(struct mount *mp, struct proc *p)
4203 struct nchandle nch;
4206 * Traverse from the mount point upwards. If we hit the process
4207 * root then the mount point is visible to the process.
4209 nch = mp->mnt_ncmountpt;
4210 while (nch.ncp) {
4211 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4212 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4213 return(1);
4215 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4216 nch = nch.mount->mnt_ncmounton;
4217 } else {
4218 nch.ncp = nch.ncp->nc_parent;
4223 * If the mount point is not visible to the process, but the
4224 * process root is in a subdirectory of the mount, return
4225 * TRUE anyway.
4227 if (p->p_fd->fd_nrdir.mount == mp)
4228 return(1);
4230 return(0);