Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / sys / kern / vfs_syscalls.c
blobf888def8c4613d24a2586a6e483d28abc6ecdb42
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/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/dirent.h>
65 #include <sys/extattr.h>
66 #include <sys/spinlock.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/objcache.h>
69 #include <sys/sysctl.h>
71 #include <sys/buf2.h>
72 #include <sys/file2.h>
73 #include <sys/spinlock2.h>
75 #include <vm/vm.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
79 #include <machine/limits.h>
80 #include <machine/stdarg.h>
82 #include <vfs/union/union.h>
84 static void mount_warning(struct mount *mp, const char *ctl, ...);
85 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
86 static int checkvp_chdir (struct vnode *vn, struct thread *td);
87 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
88 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
89 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
90 static int getutimes (const struct timeval *, struct timespec *);
91 static int setfown (struct vnode *, uid_t, gid_t);
92 static int setfmode (struct vnode *, int);
93 static int setfflags (struct vnode *, int);
94 static int setutimes (struct vnode *, const struct timespec *, int);
95 static int usermount = 0; /* if 1, non-root can mount fs. */
97 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
99 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
102 * Virtual File System System Calls
106 * Mount a file system.
109 * mount_args(char *type, char *path, int flags, caddr_t data)
111 /* ARGSUSED */
113 sys_mount(struct mount_args *uap)
115 struct thread *td = curthread;
116 struct proc *p = td->td_proc;
117 struct vnode *vp;
118 struct nchandle nch;
119 struct mount *mp;
120 struct vfsconf *vfsp;
121 int error, flag = 0, flag2 = 0;
122 int hasmount;
123 struct vattr va;
124 struct nlookupdata nd;
125 char fstypename[MFSNAMELEN];
126 struct ucred *cred = p->p_ucred;
128 KKASSERT(p);
129 if (jailed(cred))
130 return (EPERM);
131 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
132 return (error);
134 * Do not allow NFS export by non-root users.
136 if (uap->flags & MNT_EXPORTED) {
137 error = priv_check(td, PRIV_ROOT);
138 if (error)
139 return (error);
142 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
144 if (priv_check(td, PRIV_ROOT))
145 uap->flags |= MNT_NOSUID | MNT_NODEV;
148 * Lookup the requested path and extract the nch and vnode.
150 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
151 if (error == 0) {
152 if ((error = nlookup(&nd)) == 0) {
153 if (nd.nl_nch.ncp->nc_vp == NULL)
154 error = ENOENT;
157 if (error) {
158 nlookup_done(&nd);
159 return (error);
163 * Extract the locked+refd ncp and cleanup the nd structure
165 nch = nd.nl_nch;
166 cache_zero(&nd.nl_nch);
167 nlookup_done(&nd);
169 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
170 hasmount = 1;
171 else
172 hasmount = 0;
176 * now we have the locked ref'd nch and unreferenced vnode.
178 vp = nch.ncp->nc_vp;
179 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
180 cache_put(&nch);
181 return (error);
183 cache_unlock(&nch);
186 * Now we have an unlocked ref'd nch and a locked ref'd vp
188 if (uap->flags & MNT_UPDATE) {
189 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
190 cache_drop(&nch);
191 vput(vp);
192 return (EINVAL);
194 mp = vp->v_mount;
195 flag = mp->mnt_flag;
196 flag2 = mp->mnt_kern_flag;
198 * We only allow the filesystem to be reloaded if it
199 * is currently mounted read-only.
201 if ((uap->flags & MNT_RELOAD) &&
202 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
203 cache_drop(&nch);
204 vput(vp);
205 return (EOPNOTSUPP); /* Needs translation */
208 * Only root, or the user that did the original mount is
209 * permitted to update it.
211 if (mp->mnt_stat.f_owner != cred->cr_uid &&
212 (error = priv_check(td, PRIV_ROOT))) {
213 cache_drop(&nch);
214 vput(vp);
215 return (error);
217 if (vfs_busy(mp, LK_NOWAIT)) {
218 cache_drop(&nch);
219 vput(vp);
220 return (EBUSY);
222 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
223 cache_drop(&nch);
224 vfs_unbusy(mp);
225 vput(vp);
226 return (EBUSY);
228 vp->v_flag |= VMOUNT;
229 mp->mnt_flag |=
230 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
231 vn_unlock(vp);
232 goto update;
235 * If the user is not root, ensure that they own the directory
236 * onto which we are attempting to mount.
238 if ((error = VOP_GETATTR(vp, &va)) ||
239 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
240 cache_drop(&nch);
241 vput(vp);
242 return (error);
244 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
245 cache_drop(&nch);
246 vput(vp);
247 return (error);
249 if (vp->v_type != VDIR) {
250 cache_drop(&nch);
251 vput(vp);
252 return (ENOTDIR);
254 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
255 cache_drop(&nch);
256 vput(vp);
257 return (EPERM);
259 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
260 cache_drop(&nch);
261 vput(vp);
262 return (error);
264 vfsp = vfsconf_find_by_name(fstypename);
265 if (vfsp == NULL) {
266 linker_file_t lf;
268 /* Only load modules for root (very important!) */
269 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
270 cache_drop(&nch);
271 vput(vp);
272 return error;
274 error = linker_load_file(fstypename, &lf);
275 if (error || lf == NULL) {
276 cache_drop(&nch);
277 vput(vp);
278 if (lf == NULL)
279 error = ENODEV;
280 return error;
282 lf->userrefs++;
283 /* lookup again, see if the VFS was loaded */
284 vfsp = vfsconf_find_by_name(fstypename);
285 if (vfsp == NULL) {
286 lf->userrefs--;
287 linker_file_unload(lf);
288 cache_drop(&nch);
289 vput(vp);
290 return (ENODEV);
293 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
294 cache_drop(&nch);
295 vput(vp);
296 return (EBUSY);
298 vp->v_flag |= VMOUNT;
301 * Allocate and initialize the filesystem.
303 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
304 TAILQ_INIT(&mp->mnt_nvnodelist);
305 TAILQ_INIT(&mp->mnt_reservedvnlist);
306 TAILQ_INIT(&mp->mnt_jlist);
307 mp->mnt_nvnodelistsize = 0;
308 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
309 vfs_busy(mp, LK_NOWAIT);
310 mp->mnt_op = vfsp->vfc_vfsops;
311 mp->mnt_vfc = vfsp;
312 vfsp->vfc_refcount++;
313 mp->mnt_stat.f_type = vfsp->vfc_typenum;
314 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
315 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
316 mp->mnt_stat.f_owner = cred->cr_uid;
317 mp->mnt_iosize_max = DFLTPHYS;
318 vn_unlock(vp);
319 update:
321 * Set the mount level flags.
323 if (uap->flags & MNT_RDONLY)
324 mp->mnt_flag |= MNT_RDONLY;
325 else if (mp->mnt_flag & MNT_RDONLY)
326 mp->mnt_kern_flag |= MNTK_WANTRDWR;
327 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
328 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
329 MNT_NOSYMFOLLOW | MNT_IGNORE |
330 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
331 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
332 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
333 MNT_NOSYMFOLLOW | MNT_IGNORE |
334 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
336 * Mount the filesystem.
337 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
338 * get.
340 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
341 if (mp->mnt_flag & MNT_UPDATE) {
342 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
343 mp->mnt_flag &= ~MNT_RDONLY;
344 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
345 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
346 if (error) {
347 mp->mnt_flag = flag;
348 mp->mnt_kern_flag = flag2;
350 vfs_unbusy(mp);
351 vp->v_flag &= ~VMOUNT;
352 vrele(vp);
353 cache_drop(&nch);
354 return (error);
356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
358 * Put the new filesystem on the mount list after root. The mount
359 * point gets its own mnt_ncmountpt (unless the VFS already set one
360 * up) which represents the root of the mount. The lookup code
361 * detects the mount point going forward and checks the root of
362 * the mount going backwards.
364 * It is not necessary to invalidate or purge the vnode underneath
365 * because elements under the mount will be given their own glue
366 * namecache record.
368 if (!error) {
369 if (mp->mnt_ncmountpt.ncp == NULL) {
371 * allocate, then unlock, but leave the ref intact
373 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
374 cache_unlock(&mp->mnt_ncmountpt);
376 mp->mnt_ncmounton = nch; /* inherits ref */
377 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
379 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
380 vp->v_flag &= ~VMOUNT;
381 mountlist_insert(mp, MNTINS_LAST);
382 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
383 vn_unlock(vp);
384 error = vfs_allocate_syncvnode(mp);
385 vfs_unbusy(mp);
386 error = VFS_START(mp, 0);
387 vrele(vp);
388 } else {
389 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
390 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
391 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
392 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
393 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
394 vp->v_flag &= ~VMOUNT;
395 mp->mnt_vfc->vfc_refcount--;
396 vfs_unbusy(mp);
397 kfree(mp, M_MOUNT);
398 cache_drop(&nch);
399 vput(vp);
401 return (error);
405 * Scan all active processes to see if any of them have a current
406 * or root directory onto which the new filesystem has just been
407 * mounted. If so, replace them with the new mount point.
409 * The passed ncp is ref'd and locked (from the mount code) and
410 * must be associated with the vnode representing the root of the
411 * mount point.
413 struct checkdirs_info {
414 struct nchandle old_nch;
415 struct nchandle new_nch;
416 struct vnode *old_vp;
417 struct vnode *new_vp;
420 static int checkdirs_callback(struct proc *p, void *data);
422 static void
423 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
425 struct checkdirs_info info;
426 struct vnode *olddp;
427 struct vnode *newdp;
428 struct mount *mp;
431 * If the old mount point's vnode has a usecount of 1, it is not
432 * being held as a descriptor anywhere.
434 olddp = old_nch->ncp->nc_vp;
435 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
436 return;
439 * Force the root vnode of the new mount point to be resolved
440 * so we can update any matching processes.
442 mp = new_nch->mount;
443 if (VFS_ROOT(mp, &newdp))
444 panic("mount: lost mount");
445 cache_setunresolved(new_nch);
446 cache_setvp(new_nch, newdp);
449 * Special handling of the root node
451 if (rootvnode == olddp) {
452 vref(newdp);
453 vfs_cache_setroot(newdp, cache_hold(new_nch));
457 * Pass newdp separately so the callback does not have to access
458 * it via new_nch->ncp->nc_vp.
460 info.old_nch = *old_nch;
461 info.new_nch = *new_nch;
462 info.new_vp = newdp;
463 allproc_scan(checkdirs_callback, &info);
464 vput(newdp);
468 * NOTE: callback is not MP safe because the scanned process's filedesc
469 * structure can be ripped out from under us, amoung other things.
471 static int
472 checkdirs_callback(struct proc *p, void *data)
474 struct checkdirs_info *info = data;
475 struct filedesc *fdp;
476 struct nchandle ncdrop1;
477 struct nchandle ncdrop2;
478 struct vnode *vprele1;
479 struct vnode *vprele2;
481 if ((fdp = p->p_fd) != NULL) {
482 cache_zero(&ncdrop1);
483 cache_zero(&ncdrop2);
484 vprele1 = NULL;
485 vprele2 = NULL;
488 * MPUNSAFE - XXX fdp can be pulled out from under a
489 * foreign process.
491 * A shared filedesc is ok, we don't have to copy it
492 * because we are making this change globally.
494 spin_lock_wr(&fdp->fd_spin);
495 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
496 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
497 vprele1 = fdp->fd_cdir;
498 vref(info->new_vp);
499 fdp->fd_cdir = info->new_vp;
500 ncdrop1 = fdp->fd_ncdir;
501 cache_copy(&info->new_nch, &fdp->fd_ncdir);
503 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
504 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
505 vprele2 = fdp->fd_rdir;
506 vref(info->new_vp);
507 fdp->fd_rdir = info->new_vp;
508 ncdrop2 = fdp->fd_nrdir;
509 cache_copy(&info->new_nch, &fdp->fd_nrdir);
511 spin_unlock_wr(&fdp->fd_spin);
512 if (ncdrop1.ncp)
513 cache_drop(&ncdrop1);
514 if (ncdrop2.ncp)
515 cache_drop(&ncdrop2);
516 if (vprele1)
517 vrele(vprele1);
518 if (vprele2)
519 vrele(vprele2);
521 return(0);
525 * Unmount a file system.
527 * Note: unmount takes a path to the vnode mounted on as argument,
528 * not special file (as before).
531 * umount_args(char *path, int flags)
533 /* ARGSUSED */
535 sys_unmount(struct unmount_args *uap)
537 struct thread *td = curthread;
538 struct proc *p = td->td_proc;
539 struct mount *mp = NULL;
540 int error;
541 struct nlookupdata nd;
543 KKASSERT(p);
544 if (p->p_ucred->cr_prison != NULL)
545 return (EPERM);
546 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
547 return (error);
549 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
550 if (error == 0)
551 error = nlookup(&nd);
552 if (error)
553 goto out;
555 mp = nd.nl_nch.mount;
558 * Only root, or the user that did the original mount is
559 * permitted to unmount this filesystem.
561 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
562 (error = priv_check(td, PRIV_ROOT)))
563 goto out;
566 * Don't allow unmounting the root file system.
568 if (mp->mnt_flag & MNT_ROOTFS) {
569 error = EINVAL;
570 goto out;
574 * Must be the root of the filesystem
576 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
577 error = EINVAL;
578 goto out;
581 out:
582 nlookup_done(&nd);
583 if (error)
584 return (error);
585 return (dounmount(mp, uap->flags));
589 * Do the actual file system unmount.
591 static int
592 dounmount_interlock(struct mount *mp)
594 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
595 return (EBUSY);
596 mp->mnt_kern_flag |= MNTK_UNMOUNT;
597 return(0);
601 dounmount(struct mount *mp, int flags)
603 struct namecache *ncp;
604 struct nchandle nch;
605 struct vnode *vp;
606 int error;
607 int async_flag;
608 int lflags;
609 int freeok = 1;
612 * Exclusive access for unmounting purposes
614 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
615 return (error);
618 * Allow filesystems to detect that a forced unmount is in progress.
620 if (flags & MNT_FORCE)
621 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
622 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
623 error = lockmgr(&mp->mnt_lock, lflags);
624 if (error) {
625 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
626 if (mp->mnt_kern_flag & MNTK_MWAIT)
627 wakeup(mp);
628 return (error);
631 if (mp->mnt_flag & MNT_EXPUBLIC)
632 vfs_setpublicfs(NULL, NULL, NULL);
634 vfs_msync(mp, MNT_WAIT);
635 async_flag = mp->mnt_flag & MNT_ASYNC;
636 mp->mnt_flag &=~ MNT_ASYNC;
639 * If this filesystem isn't aliasing other filesystems,
640 * try to invalidate any remaining namecache entries and
641 * check the count afterwords.
643 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
644 cache_lock(&mp->mnt_ncmountpt);
645 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
646 cache_unlock(&mp->mnt_ncmountpt);
648 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
649 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
651 if ((flags & MNT_FORCE) == 0) {
652 error = EBUSY;
653 mount_warning(mp, "Cannot unmount: "
654 "%d namecache "
655 "references still "
656 "present",
657 ncp->nc_refs - 1);
658 } else {
659 mount_warning(mp, "Forced unmount: "
660 "%d namecache "
661 "references still "
662 "present",
663 ncp->nc_refs - 1);
664 freeok = 0;
670 * nchandle records ref the mount structure. Expect a count of 1
671 * (our mount->mnt_ncmountpt).
673 if (mp->mnt_refs != 1) {
674 if ((flags & MNT_FORCE) == 0) {
675 mount_warning(mp, "Cannot unmount: "
676 "%d process references still "
677 "present", mp->mnt_refs);
678 error = EBUSY;
679 } else {
680 mount_warning(mp, "Forced unmount: "
681 "%d process references still "
682 "present", mp->mnt_refs);
683 freeok = 0;
688 * Decomission our special mnt_syncer vnode. This also stops
689 * the vnlru code. If we are unable to unmount we recommission
690 * the vnode.
692 if (error == 0) {
693 if ((vp = mp->mnt_syncer) != NULL) {
694 mp->mnt_syncer = NULL;
695 vrele(vp);
697 if (((mp->mnt_flag & MNT_RDONLY) ||
698 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
699 (flags & MNT_FORCE)) {
700 error = VFS_UNMOUNT(mp, flags);
703 if (error) {
704 if (mp->mnt_syncer == NULL)
705 vfs_allocate_syncvnode(mp);
706 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
707 mp->mnt_flag |= async_flag;
708 lockmgr(&mp->mnt_lock, LK_RELEASE);
709 if (mp->mnt_kern_flag & MNTK_MWAIT)
710 wakeup(mp);
711 return (error);
714 * Clean up any journals still associated with the mount after
715 * filesystem activity has ceased.
717 journal_remove_all_journals(mp,
718 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
720 mountlist_remove(mp);
723 * Remove any installed vnode ops here so the individual VFSs don't
724 * have to.
726 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
727 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
728 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
729 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
730 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
732 if (mp->mnt_ncmountpt.ncp != NULL) {
733 nch = mp->mnt_ncmountpt;
734 cache_zero(&mp->mnt_ncmountpt);
735 cache_clrmountpt(&nch);
736 cache_drop(&nch);
738 if (mp->mnt_ncmounton.ncp != NULL) {
739 nch = mp->mnt_ncmounton;
740 cache_zero(&mp->mnt_ncmounton);
741 cache_clrmountpt(&nch);
742 cache_drop(&nch);
745 mp->mnt_vfc->vfc_refcount--;
746 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
747 panic("unmount: dangling vnode");
748 lockmgr(&mp->mnt_lock, LK_RELEASE);
749 if (mp->mnt_kern_flag & MNTK_MWAIT)
750 wakeup(mp);
751 if (freeok)
752 kfree(mp, M_MOUNT);
753 return (0);
756 static
757 void
758 mount_warning(struct mount *mp, const char *ctl, ...)
760 char *ptr;
761 char *buf;
762 __va_list va;
764 __va_start(va, ctl);
765 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
766 kprintf("unmount(%s): ", ptr);
767 kvprintf(ctl, va);
768 kprintf("\n");
769 kfree(buf, M_TEMP);
770 } else {
771 kprintf("unmount(%p", mp);
772 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
773 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
774 kprintf("): ");
775 kvprintf(ctl, va);
776 kprintf("\n");
778 __va_end(va);
782 * Shim cache_fullpath() to handle the case where a process is chrooted into
783 * a subdirectory of a mount. In this case if the root mount matches the
784 * process root directory's mount we have to specify the process's root
785 * directory instead of the mount point, because the mount point might
786 * be above the root directory.
788 static
790 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
792 struct nchandle *nch;
794 if (p && p->p_fd->fd_nrdir.mount == mp)
795 nch = &p->p_fd->fd_nrdir;
796 else
797 nch = &mp->mnt_ncmountpt;
798 return(cache_fullpath(p, nch, rb, fb));
802 * Sync each mounted filesystem.
805 #ifdef DEBUG
806 static int syncprt = 0;
807 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
808 #endif /* DEBUG */
810 static int sync_callback(struct mount *mp, void *data);
812 /* ARGSUSED */
814 sys_sync(struct sync_args *uap)
816 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
817 #ifdef DEBUG
819 * print out buffer pool stat information on each sync() call.
821 if (syncprt)
822 vfs_bufstats();
823 #endif /* DEBUG */
824 return (0);
827 static
829 sync_callback(struct mount *mp, void *data __unused)
831 int asyncflag;
833 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
834 asyncflag = mp->mnt_flag & MNT_ASYNC;
835 mp->mnt_flag &= ~MNT_ASYNC;
836 vfs_msync(mp, MNT_NOWAIT);
837 VFS_SYNC(mp, MNT_NOWAIT);
838 mp->mnt_flag |= asyncflag;
840 return(0);
843 /* XXX PRISON: could be per prison flag */
844 static int prison_quotas;
845 #if 0
846 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
847 #endif
850 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
852 * Change filesystem quotas.
854 /* ARGSUSED */
856 sys_quotactl(struct quotactl_args *uap)
858 struct nlookupdata nd;
859 struct thread *td;
860 struct proc *p;
861 struct mount *mp;
862 int error;
864 td = curthread;
865 p = td->td_proc;
866 if (p->p_ucred->cr_prison && !prison_quotas)
867 return (EPERM);
869 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
870 if (error == 0)
871 error = nlookup(&nd);
872 if (error == 0) {
873 mp = nd.nl_nch.mount;
874 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
875 uap->arg, nd.nl_cred);
877 nlookup_done(&nd);
878 return (error);
882 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
883 * void *buf, int buflen)
885 * This function operates on a mount point and executes the specified
886 * operation using the specified control data, and possibly returns data.
888 * The actual number of bytes stored in the result buffer is returned, 0
889 * if none, otherwise an error is returned.
891 /* ARGSUSED */
893 sys_mountctl(struct mountctl_args *uap)
895 struct thread *td = curthread;
896 struct proc *p = td->td_proc;
897 struct file *fp;
898 void *ctl = NULL;
899 void *buf = NULL;
900 char *path = NULL;
901 int error;
904 * Sanity and permissions checks. We must be root.
906 KKASSERT(p);
907 if (p->p_ucred->cr_prison != NULL)
908 return (EPERM);
909 if ((error = priv_check(td, PRIV_ROOT)) != 0)
910 return (error);
913 * Argument length checks
915 if (uap->ctllen < 0 || uap->ctllen > 1024)
916 return (EINVAL);
917 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
918 return (EINVAL);
919 if (uap->path == NULL)
920 return (EINVAL);
923 * Allocate the necessary buffers and copyin data
925 path = objcache_get(namei_oc, M_WAITOK);
926 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
927 if (error)
928 goto done;
930 if (uap->ctllen) {
931 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
932 error = copyin(uap->ctl, ctl, uap->ctllen);
933 if (error)
934 goto done;
936 if (uap->buflen)
937 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
940 * Validate the descriptor
942 if (uap->fd >= 0) {
943 fp = holdfp(p->p_fd, uap->fd, -1);
944 if (fp == NULL) {
945 error = EBADF;
946 goto done;
948 } else {
949 fp = NULL;
953 * Execute the internal kernel function and clean up.
955 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
956 if (fp)
957 fdrop(fp);
958 if (error == 0 && uap->sysmsg_result > 0)
959 error = copyout(buf, uap->buf, uap->sysmsg_result);
960 done:
961 if (path)
962 objcache_put(namei_oc, path);
963 if (ctl)
964 kfree(ctl, M_TEMP);
965 if (buf)
966 kfree(buf, M_TEMP);
967 return (error);
971 * Execute a mount control operation by resolving the path to a mount point
972 * and calling vop_mountctl().
974 * Use the mount point from the nch instead of the vnode so nullfs mounts
975 * can properly spike the VOP.
978 kern_mountctl(const char *path, int op, struct file *fp,
979 const void *ctl, int ctllen,
980 void *buf, int buflen, int *res)
982 struct vnode *vp;
983 struct mount *mp;
984 struct nlookupdata nd;
985 int error;
987 *res = 0;
988 vp = NULL;
989 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
990 if (error == 0)
991 error = nlookup(&nd);
992 if (error == 0)
993 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
994 mp = nd.nl_nch.mount;
995 nlookup_done(&nd);
996 if (error)
997 return (error);
1000 * Must be the root of the filesystem
1002 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1003 vput(vp);
1004 return (EINVAL);
1006 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1007 buf, buflen, res);
1008 vput(vp);
1009 return (error);
1013 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1015 struct thread *td = curthread;
1016 struct proc *p = td->td_proc;
1017 struct mount *mp;
1018 struct statfs *sp;
1019 char *fullpath, *freepath;
1020 int error;
1022 if ((error = nlookup(nd)) != 0)
1023 return (error);
1024 mp = nd->nl_nch.mount;
1025 sp = &mp->mnt_stat;
1026 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1027 return (error);
1029 error = mount_path(p, mp, &fullpath, &freepath);
1030 if (error)
1031 return(error);
1032 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1033 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1034 kfree(freepath, M_TEMP);
1036 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1037 bcopy(sp, buf, sizeof(*buf));
1038 /* Only root should have access to the fsid's. */
1039 if (priv_check(td, PRIV_ROOT))
1040 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1041 return (0);
1045 * statfs_args(char *path, struct statfs *buf)
1047 * Get filesystem statistics.
1050 sys_statfs(struct statfs_args *uap)
1052 struct nlookupdata nd;
1053 struct statfs buf;
1054 int error;
1056 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1057 if (error == 0)
1058 error = kern_statfs(&nd, &buf);
1059 nlookup_done(&nd);
1060 if (error == 0)
1061 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1062 return (error);
1066 kern_fstatfs(int fd, struct statfs *buf)
1068 struct thread *td = curthread;
1069 struct proc *p = td->td_proc;
1070 struct file *fp;
1071 struct mount *mp;
1072 struct statfs *sp;
1073 char *fullpath, *freepath;
1074 int error;
1076 KKASSERT(p);
1077 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1078 return (error);
1079 mp = ((struct vnode *)fp->f_data)->v_mount;
1080 if (mp == NULL) {
1081 error = EBADF;
1082 goto done;
1084 if (fp->f_cred == NULL) {
1085 error = EINVAL;
1086 goto done;
1088 sp = &mp->mnt_stat;
1089 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1090 goto done;
1092 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1093 goto done;
1094 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1095 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1096 kfree(freepath, M_TEMP);
1098 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1099 bcopy(sp, buf, sizeof(*buf));
1101 /* Only root should have access to the fsid's. */
1102 if (priv_check(td, PRIV_ROOT))
1103 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1104 error = 0;
1105 done:
1106 fdrop(fp);
1107 return (error);
1111 * fstatfs_args(int fd, struct statfs *buf)
1113 * Get filesystem statistics.
1116 sys_fstatfs(struct fstatfs_args *uap)
1118 struct statfs buf;
1119 int error;
1121 error = kern_fstatfs(uap->fd, &buf);
1123 if (error == 0)
1124 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1125 return (error);
1129 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1131 struct mount *mp;
1132 struct statvfs *sp;
1133 int error;
1135 if ((error = nlookup(nd)) != 0)
1136 return (error);
1137 mp = nd->nl_nch.mount;
1138 sp = &mp->mnt_vstat;
1139 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1140 return (error);
1142 sp->f_flag = 0;
1143 if (mp->mnt_flag & MNT_RDONLY)
1144 sp->f_flag |= ST_RDONLY;
1145 if (mp->mnt_flag & MNT_NOSUID)
1146 sp->f_flag |= ST_NOSUID;
1147 bcopy(sp, buf, sizeof(*buf));
1148 return (0);
1152 * statfs_args(char *path, struct statfs *buf)
1154 * Get filesystem statistics.
1157 sys_statvfs(struct statvfs_args *uap)
1159 struct nlookupdata nd;
1160 struct statvfs buf;
1161 int error;
1163 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1164 if (error == 0)
1165 error = kern_statvfs(&nd, &buf);
1166 nlookup_done(&nd);
1167 if (error == 0)
1168 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1169 return (error);
1173 kern_fstatvfs(int fd, struct statvfs *buf)
1175 struct thread *td = curthread;
1176 struct proc *p = td->td_proc;
1177 struct file *fp;
1178 struct mount *mp;
1179 struct statvfs *sp;
1180 int error;
1182 KKASSERT(p);
1183 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1184 return (error);
1185 mp = ((struct vnode *)fp->f_data)->v_mount;
1186 if (mp == NULL) {
1187 error = EBADF;
1188 goto done;
1190 if (fp->f_cred == NULL) {
1191 error = EINVAL;
1192 goto done;
1194 sp = &mp->mnt_vstat;
1195 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1196 goto done;
1198 sp->f_flag = 0;
1199 if (mp->mnt_flag & MNT_RDONLY)
1200 sp->f_flag |= ST_RDONLY;
1201 if (mp->mnt_flag & MNT_NOSUID)
1202 sp->f_flag |= ST_NOSUID;
1204 bcopy(sp, buf, sizeof(*buf));
1205 error = 0;
1206 done:
1207 fdrop(fp);
1208 return (error);
1212 * fstatfs_args(int fd, struct statfs *buf)
1214 * Get filesystem statistics.
1217 sys_fstatvfs(struct fstatvfs_args *uap)
1219 struct statvfs buf;
1220 int error;
1222 error = kern_fstatvfs(uap->fd, &buf);
1224 if (error == 0)
1225 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1226 return (error);
1230 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1232 * Get statistics on all filesystems.
1235 struct getfsstat_info {
1236 struct statfs *sfsp;
1237 long count;
1238 long maxcount;
1239 int error;
1240 int flags;
1241 struct proc *p;
1244 static int getfsstat_callback(struct mount *, void *);
1246 /* ARGSUSED */
1248 sys_getfsstat(struct getfsstat_args *uap)
1250 struct thread *td = curthread;
1251 struct proc *p = td->td_proc;
1252 struct getfsstat_info info;
1254 bzero(&info, sizeof(info));
1256 info.maxcount = uap->bufsize / sizeof(struct statfs);
1257 info.sfsp = uap->buf;
1258 info.count = 0;
1259 info.flags = uap->flags;
1260 info.p = p;
1262 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1263 if (info.sfsp && info.count > info.maxcount)
1264 uap->sysmsg_result = info.maxcount;
1265 else
1266 uap->sysmsg_result = info.count;
1267 return (info.error);
1270 static int
1271 getfsstat_callback(struct mount *mp, void *data)
1273 struct getfsstat_info *info = data;
1274 struct statfs *sp;
1275 char *freepath;
1276 char *fullpath;
1277 int error;
1279 if (info->sfsp && info->count < info->maxcount) {
1280 if (info->p && !chroot_visible_mnt(mp, info->p))
1281 return(0);
1282 sp = &mp->mnt_stat;
1285 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1286 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1287 * overrides MNT_WAIT.
1289 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1290 (info->flags & MNT_WAIT)) &&
1291 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1292 return(0);
1294 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1296 error = mount_path(info->p, mp, &fullpath, &freepath);
1297 if (error) {
1298 info->error = error;
1299 return(-1);
1301 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1302 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1303 kfree(freepath, M_TEMP);
1305 error = copyout(sp, info->sfsp, sizeof(*sp));
1306 if (error) {
1307 info->error = error;
1308 return (-1);
1310 ++info->sfsp;
1312 info->count++;
1313 return(0);
1317 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1318 long bufsize, int flags)
1320 * Get statistics on all filesystems.
1323 struct getvfsstat_info {
1324 struct statfs *sfsp;
1325 struct statvfs *vsfsp;
1326 long count;
1327 long maxcount;
1328 int error;
1329 int flags;
1330 struct proc *p;
1333 static int getvfsstat_callback(struct mount *, void *);
1335 /* ARGSUSED */
1337 sys_getvfsstat(struct getvfsstat_args *uap)
1339 struct thread *td = curthread;
1340 struct proc *p = td->td_proc;
1341 struct getvfsstat_info info;
1343 bzero(&info, sizeof(info));
1345 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1346 info.sfsp = uap->buf;
1347 info.vsfsp = uap->vbuf;
1348 info.count = 0;
1349 info.flags = uap->flags;
1350 info.p = p;
1352 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1353 if (info.vsfsp && info.count > info.maxcount)
1354 uap->sysmsg_result = info.maxcount;
1355 else
1356 uap->sysmsg_result = info.count;
1357 return (info.error);
1360 static int
1361 getvfsstat_callback(struct mount *mp, void *data)
1363 struct getvfsstat_info *info = data;
1364 struct statfs *sp;
1365 struct statvfs *vsp;
1366 char *freepath;
1367 char *fullpath;
1368 int error;
1370 if (info->vsfsp && info->count < info->maxcount) {
1371 if (info->p && !chroot_visible_mnt(mp, info->p))
1372 return(0);
1373 sp = &mp->mnt_stat;
1374 vsp = &mp->mnt_vstat;
1377 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1378 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1379 * overrides MNT_WAIT.
1381 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1382 (info->flags & MNT_WAIT)) &&
1383 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1384 return(0);
1386 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1388 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1389 (info->flags & MNT_WAIT)) &&
1390 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1391 return(0);
1393 vsp->f_flag = 0;
1394 if (mp->mnt_flag & MNT_RDONLY)
1395 vsp->f_flag |= ST_RDONLY;
1396 if (mp->mnt_flag & MNT_NOSUID)
1397 vsp->f_flag |= ST_NOSUID;
1399 error = mount_path(info->p, mp, &fullpath, &freepath);
1400 if (error) {
1401 info->error = error;
1402 return(-1);
1404 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1405 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1406 kfree(freepath, M_TEMP);
1408 error = copyout(sp, info->sfsp, sizeof(*sp));
1409 if (error == 0)
1410 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1411 if (error) {
1412 info->error = error;
1413 return (-1);
1415 ++info->sfsp;
1416 ++info->vsfsp;
1418 info->count++;
1419 return(0);
1424 * fchdir_args(int fd)
1426 * Change current working directory to a given file descriptor.
1428 /* ARGSUSED */
1430 sys_fchdir(struct fchdir_args *uap)
1432 struct thread *td = curthread;
1433 struct proc *p = td->td_proc;
1434 struct filedesc *fdp = p->p_fd;
1435 struct vnode *vp, *ovp;
1436 struct mount *mp;
1437 struct file *fp;
1438 struct nchandle nch, onch, tnch;
1439 int error;
1441 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1442 return (error);
1443 vp = (struct vnode *)fp->f_data;
1444 vref(vp);
1445 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1446 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1447 error = ENOTDIR;
1448 else
1449 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1450 if (error) {
1451 vput(vp);
1452 fdrop(fp);
1453 return (error);
1455 cache_copy(&fp->f_nchandle, &nch);
1458 * If the ncp has become a mount point, traverse through
1459 * the mount point.
1462 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1463 (mp = cache_findmount(&nch)) != NULL
1465 error = nlookup_mp(mp, &tnch);
1466 if (error == 0) {
1467 cache_unlock(&tnch); /* leave ref intact */
1468 vput(vp);
1469 vp = tnch.ncp->nc_vp;
1470 error = vget(vp, LK_SHARED);
1471 KKASSERT(error == 0);
1472 cache_drop(&nch);
1473 nch = tnch;
1476 if (error == 0) {
1477 ovp = fdp->fd_cdir;
1478 onch = fdp->fd_ncdir;
1479 vn_unlock(vp); /* leave ref intact */
1480 fdp->fd_cdir = vp;
1481 fdp->fd_ncdir = nch;
1482 cache_drop(&onch);
1483 vrele(ovp);
1484 } else {
1485 cache_drop(&nch);
1486 vput(vp);
1488 fdrop(fp);
1489 return (error);
1493 kern_chdir(struct nlookupdata *nd)
1495 struct thread *td = curthread;
1496 struct proc *p = td->td_proc;
1497 struct filedesc *fdp = p->p_fd;
1498 struct vnode *vp, *ovp;
1499 struct nchandle onch;
1500 int error;
1502 if ((error = nlookup(nd)) != 0)
1503 return (error);
1504 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1505 return (ENOENT);
1506 if ((error = vget(vp, LK_SHARED)) != 0)
1507 return (error);
1509 error = checkvp_chdir(vp, td);
1510 vn_unlock(vp);
1511 if (error == 0) {
1512 ovp = fdp->fd_cdir;
1513 onch = fdp->fd_ncdir;
1514 cache_unlock(&nd->nl_nch); /* leave reference intact */
1515 fdp->fd_ncdir = nd->nl_nch;
1516 fdp->fd_cdir = vp;
1517 cache_drop(&onch);
1518 vrele(ovp);
1519 cache_zero(&nd->nl_nch);
1520 } else {
1521 vrele(vp);
1523 return (error);
1527 * chdir_args(char *path)
1529 * Change current working directory (``.'').
1532 sys_chdir(struct chdir_args *uap)
1534 struct nlookupdata nd;
1535 int error;
1537 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1538 if (error == 0)
1539 error = kern_chdir(&nd);
1540 nlookup_done(&nd);
1541 return (error);
1545 * Helper function for raised chroot(2) security function: Refuse if
1546 * any filedescriptors are open directories.
1548 static int
1549 chroot_refuse_vdir_fds(struct filedesc *fdp)
1551 struct vnode *vp;
1552 struct file *fp;
1553 int error;
1554 int fd;
1556 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1557 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1558 continue;
1559 vp = (struct vnode *)fp->f_data;
1560 if (vp->v_type != VDIR) {
1561 fdrop(fp);
1562 continue;
1564 fdrop(fp);
1565 return(EPERM);
1567 return (0);
1571 * This sysctl determines if we will allow a process to chroot(2) if it
1572 * has a directory open:
1573 * 0: disallowed for all processes.
1574 * 1: allowed for processes that were not already chroot(2)'ed.
1575 * 2: allowed for all processes.
1578 static int chroot_allow_open_directories = 1;
1580 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1581 &chroot_allow_open_directories, 0, "");
1584 * chroot to the specified namecache entry. We obtain the vp from the
1585 * namecache data. The passed ncp must be locked and referenced and will
1586 * remain locked and referenced on return.
1589 kern_chroot(struct nchandle *nch)
1591 struct thread *td = curthread;
1592 struct proc *p = td->td_proc;
1593 struct filedesc *fdp = p->p_fd;
1594 struct vnode *vp;
1595 int error;
1598 * Only root can chroot
1600 if ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0)
1601 return (error);
1604 * Disallow open directory descriptors (fchdir() breakouts).
1606 if (chroot_allow_open_directories == 0 ||
1607 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1608 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1609 return (error);
1611 if ((vp = nch->ncp->nc_vp) == NULL)
1612 return (ENOENT);
1614 if ((error = vget(vp, LK_SHARED)) != 0)
1615 return (error);
1618 * Check the validity of vp as a directory to change to and
1619 * associate it with rdir/jdir.
1621 error = checkvp_chdir(vp, td);
1622 vn_unlock(vp); /* leave reference intact */
1623 if (error == 0) {
1624 vrele(fdp->fd_rdir);
1625 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1626 cache_drop(&fdp->fd_nrdir);
1627 cache_copy(nch, &fdp->fd_nrdir);
1628 if (fdp->fd_jdir == NULL) {
1629 fdp->fd_jdir = vp;
1630 vref(fdp->fd_jdir);
1631 cache_copy(nch, &fdp->fd_njdir);
1633 } else {
1634 vrele(vp);
1636 return (error);
1640 * chroot_args(char *path)
1642 * Change notion of root (``/'') directory.
1644 /* ARGSUSED */
1646 sys_chroot(struct chroot_args *uap)
1648 struct thread *td = curthread;
1649 struct nlookupdata nd;
1650 int error;
1652 KKASSERT(td->td_proc);
1653 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1654 if (error) {
1655 nlookup_done(&nd);
1656 return(error);
1658 error = nlookup(&nd);
1659 if (error == 0)
1660 error = kern_chroot(&nd.nl_nch);
1661 nlookup_done(&nd);
1662 return(error);
1666 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1667 * determine whether it is legal to chdir to the vnode. The vnode's state
1668 * is not changed by this call.
1671 checkvp_chdir(struct vnode *vp, struct thread *td)
1673 int error;
1675 if (vp->v_type != VDIR)
1676 error = ENOTDIR;
1677 else
1678 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1679 return (error);
1683 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1685 struct thread *td = curthread;
1686 struct proc *p = td->td_proc;
1687 struct lwp *lp = td->td_lwp;
1688 struct filedesc *fdp = p->p_fd;
1689 int cmode, flags;
1690 struct file *nfp;
1691 struct file *fp;
1692 struct vnode *vp;
1693 int type, indx, error;
1694 struct flock lf;
1696 if ((oflags & O_ACCMODE) == O_ACCMODE)
1697 return (EINVAL);
1698 flags = FFLAGS(oflags);
1699 error = falloc(p, &nfp, NULL);
1700 if (error)
1701 return (error);
1702 fp = nfp;
1703 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1706 * XXX p_dupfd is a real mess. It allows a device to return a
1707 * file descriptor to be duplicated rather then doing the open
1708 * itself.
1710 lp->lwp_dupfd = -1;
1713 * Call vn_open() to do the lookup and assign the vnode to the
1714 * file pointer. vn_open() does not change the ref count on fp
1715 * and the vnode, on success, will be inherited by the file pointer
1716 * and unlocked.
1718 nd->nl_flags |= NLC_LOCKVP;
1719 error = vn_open(nd, fp, flags, cmode);
1720 nlookup_done(nd);
1721 if (error) {
1723 * handle special fdopen() case. bleh. dupfdopen() is
1724 * responsible for dropping the old contents of ofiles[indx]
1725 * if it succeeds.
1727 * Note that fsetfd() will add a ref to fp which represents
1728 * the fd_files[] assignment. We must still drop our
1729 * reference.
1731 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1732 if (fdalloc(p, 0, &indx) == 0) {
1733 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1734 if (error == 0) {
1735 *res = indx;
1736 fdrop(fp); /* our ref */
1737 return (0);
1739 fsetfd(p, NULL, indx);
1742 fdrop(fp); /* our ref */
1743 if (error == ERESTART)
1744 error = EINTR;
1745 return (error);
1749 * ref the vnode for ourselves so it can't be ripped out from under
1750 * is. XXX need an ND flag to request that the vnode be returned
1751 * anyway.
1753 * Reserve a file descriptor but do not assign it until the open
1754 * succeeds.
1756 vp = (struct vnode *)fp->f_data;
1757 vref(vp);
1758 if ((error = fdalloc(p, 0, &indx)) != 0) {
1759 fdrop(fp);
1760 vrele(vp);
1761 return (error);
1765 * If no error occurs the vp will have been assigned to the file
1766 * pointer.
1768 lp->lwp_dupfd = 0;
1770 if (flags & (O_EXLOCK | O_SHLOCK)) {
1771 lf.l_whence = SEEK_SET;
1772 lf.l_start = 0;
1773 lf.l_len = 0;
1774 if (flags & O_EXLOCK)
1775 lf.l_type = F_WRLCK;
1776 else
1777 lf.l_type = F_RDLCK;
1778 if (flags & FNONBLOCK)
1779 type = 0;
1780 else
1781 type = F_WAIT;
1783 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1785 * lock request failed. Clean up the reserved
1786 * descriptor.
1788 vrele(vp);
1789 fsetfd(p, NULL, indx);
1790 fdrop(fp);
1791 return (error);
1793 fp->f_flag |= FHASLOCK;
1795 #if 0
1797 * Assert that all regular file vnodes were created with a object.
1799 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1800 ("open: regular file has no backing object after vn_open"));
1801 #endif
1803 vrele(vp);
1806 * release our private reference, leaving the one associated with the
1807 * descriptor table intact.
1809 fsetfd(p, fp, indx);
1810 fdrop(fp);
1811 *res = indx;
1812 return (0);
1816 * open_args(char *path, int flags, int mode)
1818 * Check permissions, allocate an open file structure,
1819 * and call the device open routine if any.
1822 sys_open(struct open_args *uap)
1824 struct nlookupdata nd;
1825 int error;
1827 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1828 if (error == 0) {
1829 error = kern_open(&nd, uap->flags,
1830 uap->mode, &uap->sysmsg_result);
1832 nlookup_done(&nd);
1833 return (error);
1837 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1839 struct thread *td = curthread;
1840 struct proc *p = td->td_proc;
1841 struct vnode *vp;
1842 struct vattr vattr;
1843 int error;
1844 int whiteout = 0;
1846 KKASSERT(p);
1848 switch (mode & S_IFMT) {
1849 case S_IFCHR:
1850 case S_IFBLK:
1851 error = priv_check(td, PRIV_ROOT);
1852 break;
1853 default:
1854 error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT);
1855 break;
1857 if (error)
1858 return (error);
1860 bwillinode(1);
1861 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1862 if ((error = nlookup(nd)) != 0)
1863 return (error);
1864 if (nd->nl_nch.ncp->nc_vp)
1865 return (EEXIST);
1866 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1867 return (error);
1869 VATTR_NULL(&vattr);
1870 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1871 vattr.va_rmajor = rmajor;
1872 vattr.va_rminor = rminor;
1873 whiteout = 0;
1875 switch (mode & S_IFMT) {
1876 case S_IFMT: /* used by badsect to flag bad sectors */
1877 vattr.va_type = VBAD;
1878 break;
1879 case S_IFCHR:
1880 vattr.va_type = VCHR;
1881 break;
1882 case S_IFBLK:
1883 vattr.va_type = VBLK;
1884 break;
1885 case S_IFWHT:
1886 whiteout = 1;
1887 break;
1888 case S_IFDIR:
1889 /* special directories support for HAMMER */
1890 vattr.va_type = VDIR;
1891 break;
1892 default:
1893 error = EINVAL;
1894 break;
1896 if (error == 0) {
1897 if (whiteout) {
1898 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1899 nd->nl_cred, NAMEI_CREATE);
1900 } else {
1901 vp = NULL;
1902 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1903 &vp, nd->nl_cred, &vattr);
1904 if (error == 0)
1905 vput(vp);
1908 return (error);
1912 * mknod_args(char *path, int mode, int dev)
1914 * Create a special file.
1917 sys_mknod(struct mknod_args *uap)
1919 struct nlookupdata nd;
1920 int error;
1922 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1923 if (error == 0) {
1924 error = kern_mknod(&nd, uap->mode,
1925 umajor(uap->dev), uminor(uap->dev));
1927 nlookup_done(&nd);
1928 return (error);
1932 kern_mkfifo(struct nlookupdata *nd, int mode)
1934 struct thread *td = curthread;
1935 struct proc *p = td->td_proc;
1936 struct vattr vattr;
1937 struct vnode *vp;
1938 int error;
1940 bwillinode(1);
1942 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1943 if ((error = nlookup(nd)) != 0)
1944 return (error);
1945 if (nd->nl_nch.ncp->nc_vp)
1946 return (EEXIST);
1947 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1948 return (error);
1950 VATTR_NULL(&vattr);
1951 vattr.va_type = VFIFO;
1952 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1953 vp = NULL;
1954 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1955 if (error == 0)
1956 vput(vp);
1957 return (error);
1961 * mkfifo_args(char *path, int mode)
1963 * Create a named pipe.
1966 sys_mkfifo(struct mkfifo_args *uap)
1968 struct nlookupdata nd;
1969 int error;
1971 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1972 if (error == 0)
1973 error = kern_mkfifo(&nd, uap->mode);
1974 nlookup_done(&nd);
1975 return (error);
1978 static int hardlink_check_uid = 0;
1979 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1980 &hardlink_check_uid, 0,
1981 "Unprivileged processes cannot create hard links to files owned by other "
1982 "users");
1983 static int hardlink_check_gid = 0;
1984 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1985 &hardlink_check_gid, 0,
1986 "Unprivileged processes cannot create hard links to files owned by other "
1987 "groups");
1989 static int
1990 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1992 struct vattr va;
1993 int error;
1996 * Shortcut if disabled
1998 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1999 return (0);
2002 * root cred can always hardlink
2004 if (priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT) == 0)
2005 return (0);
2008 * Otherwise only if the originating file is owned by the
2009 * same user or group. Note that any group is allowed if
2010 * the file is owned by the caller.
2012 error = VOP_GETATTR(vp, &va);
2013 if (error != 0)
2014 return (error);
2016 if (hardlink_check_uid) {
2017 if (cred->cr_uid != va.va_uid)
2018 return (EPERM);
2021 if (hardlink_check_gid) {
2022 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2023 return (EPERM);
2026 return (0);
2030 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2032 struct thread *td = curthread;
2033 struct vnode *vp;
2034 int error;
2037 * Lookup the source and obtained a locked vnode.
2039 * XXX relookup on vget failure / race ?
2041 bwillinode(1);
2042 if ((error = nlookup(nd)) != 0)
2043 return (error);
2044 vp = nd->nl_nch.ncp->nc_vp;
2045 KKASSERT(vp != NULL);
2046 if (vp->v_type == VDIR)
2047 return (EPERM); /* POSIX */
2048 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2049 return (error);
2050 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2051 return (error);
2054 * Unlock the source so we can lookup the target without deadlocking
2055 * (XXX vp is locked already, possible other deadlock?). The target
2056 * must not exist.
2058 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2059 nd->nl_flags &= ~NLC_NCPISLOCKED;
2060 cache_unlock(&nd->nl_nch);
2062 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2063 if ((error = nlookup(linknd)) != 0) {
2064 vput(vp);
2065 return (error);
2067 if (linknd->nl_nch.ncp->nc_vp) {
2068 vput(vp);
2069 return (EEXIST);
2073 * Finally run the new API VOP.
2075 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2076 if (error == 0) {
2077 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2078 vp, linknd->nl_cred);
2080 vput(vp);
2081 return (error);
2085 * link_args(char *path, char *link)
2087 * Make a hard file link.
2090 sys_link(struct link_args *uap)
2092 struct nlookupdata nd, linknd;
2093 int error;
2095 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2096 if (error == 0) {
2097 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2098 if (error == 0)
2099 error = kern_link(&nd, &linknd);
2100 nlookup_done(&linknd);
2102 nlookup_done(&nd);
2103 return (error);
2107 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2109 struct vattr vattr;
2110 struct vnode *vp;
2111 struct vnode *dvp;
2112 int error;
2114 bwillinode(1);
2115 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2116 if ((error = nlookup(nd)) != 0)
2117 return (error);
2118 if (nd->nl_nch.ncp->nc_vp)
2119 return (EEXIST);
2120 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2121 return (error);
2122 dvp = nd->nl_dvp;
2123 VATTR_NULL(&vattr);
2124 vattr.va_mode = mode;
2125 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2126 if (error == 0)
2127 vput(vp);
2128 return (error);
2132 * symlink(char *path, char *link)
2134 * Make a symbolic link.
2137 sys_symlink(struct symlink_args *uap)
2139 struct thread *td = curthread;
2140 struct nlookupdata nd;
2141 char *path;
2142 int error;
2143 int mode;
2145 path = objcache_get(namei_oc, M_WAITOK);
2146 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2147 if (error == 0) {
2148 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2149 if (error == 0) {
2150 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2151 error = kern_symlink(&nd, path, mode);
2153 nlookup_done(&nd);
2155 objcache_put(namei_oc, path);
2156 return (error);
2160 * undelete_args(char *path)
2162 * Delete a whiteout from the filesystem.
2164 /* ARGSUSED */
2166 sys_undelete(struct undelete_args *uap)
2168 struct nlookupdata nd;
2169 int error;
2171 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2172 bwillinode(1);
2173 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2174 if (error == 0)
2175 error = nlookup(&nd);
2176 if (error == 0)
2177 error = ncp_writechk(&nd.nl_nch);
2178 if (error == 0) {
2179 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2180 NAMEI_DELETE);
2182 nlookup_done(&nd);
2183 return (error);
2187 kern_unlink(struct nlookupdata *nd)
2189 int error;
2191 bwillinode(1);
2192 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2193 if ((error = nlookup(nd)) != 0)
2194 return (error);
2195 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2196 return (error);
2197 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2198 return (error);
2202 * unlink_args(char *path)
2204 * Delete a name from the filesystem.
2207 sys_unlink(struct unlink_args *uap)
2209 struct nlookupdata nd;
2210 int error;
2212 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2213 if (error == 0)
2214 error = kern_unlink(&nd);
2215 nlookup_done(&nd);
2216 return (error);
2220 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2222 struct thread *td = curthread;
2223 struct proc *p = td->td_proc;
2224 struct file *fp;
2225 struct vnode *vp;
2226 struct vattr vattr;
2227 off_t new_offset;
2228 int error;
2230 fp = holdfp(p->p_fd, fd, -1);
2231 if (fp == NULL)
2232 return (EBADF);
2233 if (fp->f_type != DTYPE_VNODE) {
2234 error = ESPIPE;
2235 goto done;
2237 vp = (struct vnode *)fp->f_data;
2239 switch (whence) {
2240 case L_INCR:
2241 new_offset = fp->f_offset + offset;
2242 error = 0;
2243 break;
2244 case L_XTND:
2245 error = VOP_GETATTR(vp, &vattr);
2246 new_offset = offset + vattr.va_size;
2247 break;
2248 case L_SET:
2249 new_offset = offset;
2250 error = 0;
2251 break;
2252 default:
2253 new_offset = 0;
2254 error = EINVAL;
2255 break;
2259 * Validate the seek position. Negative offsets are not allowed
2260 * for regular files, block specials, or directories.
2262 if (error == 0) {
2263 if (new_offset < 0 &&
2264 (vp->v_type == VREG || vp->v_type == VDIR ||
2265 vp->v_type == VCHR || vp->v_type == VBLK)) {
2266 error = EINVAL;
2267 } else {
2268 fp->f_offset = new_offset;
2271 *res = fp->f_offset;
2272 done:
2273 fdrop(fp);
2274 return (error);
2278 * lseek_args(int fd, int pad, off_t offset, int whence)
2280 * Reposition read/write file offset.
2283 sys_lseek(struct lseek_args *uap)
2285 int error;
2287 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2288 &uap->sysmsg_offset);
2290 return (error);
2294 kern_access(struct nlookupdata *nd, int aflags)
2296 struct vnode *vp;
2297 int error, flags;
2299 if ((error = nlookup(nd)) != 0)
2300 return (error);
2301 retry:
2302 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2303 if (error)
2304 return (error);
2306 /* Flags == 0 means only check for existence. */
2307 if (aflags) {
2308 flags = 0;
2309 if (aflags & R_OK)
2310 flags |= VREAD;
2311 if (aflags & W_OK)
2312 flags |= VWRITE;
2313 if (aflags & X_OK)
2314 flags |= VEXEC;
2315 if ((flags & VWRITE) == 0 ||
2316 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2317 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2320 * If the file handle is stale we have to re-resolve the
2321 * entry. This is a hack at the moment.
2323 if (error == ESTALE) {
2324 vput(vp);
2325 cache_setunresolved(&nd->nl_nch);
2326 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2327 if (error == 0) {
2328 vp = NULL;
2329 goto retry;
2331 return(error);
2334 vput(vp);
2335 return (error);
2339 * access_args(char *path, int flags)
2341 * Check access permissions.
2344 sys_access(struct access_args *uap)
2346 struct nlookupdata nd;
2347 int error;
2349 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2350 if (error == 0)
2351 error = kern_access(&nd, uap->flags);
2352 nlookup_done(&nd);
2353 return (error);
2357 kern_stat(struct nlookupdata *nd, struct stat *st)
2359 int error;
2360 struct vnode *vp;
2361 thread_t td;
2363 if ((error = nlookup(nd)) != 0)
2364 return (error);
2365 again:
2366 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2367 return (ENOENT);
2369 td = curthread;
2370 if ((error = vget(vp, LK_SHARED)) != 0)
2371 return (error);
2372 error = vn_stat(vp, st, nd->nl_cred);
2375 * If the file handle is stale we have to re-resolve the entry. This
2376 * is a hack at the moment.
2378 if (error == ESTALE) {
2379 vput(vp);
2380 cache_setunresolved(&nd->nl_nch);
2381 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2382 if (error == 0)
2383 goto again;
2384 } else {
2385 vput(vp);
2387 return (error);
2391 * stat_args(char *path, struct stat *ub)
2393 * Get file status; this version follows links.
2396 sys_stat(struct stat_args *uap)
2398 struct nlookupdata nd;
2399 struct stat st;
2400 int error;
2402 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2403 if (error == 0) {
2404 error = kern_stat(&nd, &st);
2405 if (error == 0)
2406 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2408 nlookup_done(&nd);
2409 return (error);
2413 * lstat_args(char *path, struct stat *ub)
2415 * Get file status; this version does not follow links.
2418 sys_lstat(struct lstat_args *uap)
2420 struct nlookupdata nd;
2421 struct stat st;
2422 int error;
2424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2425 if (error == 0) {
2426 error = kern_stat(&nd, &st);
2427 if (error == 0)
2428 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2430 nlookup_done(&nd);
2431 return (error);
2435 * pathconf_Args(char *path, int name)
2437 * Get configurable pathname variables.
2439 /* ARGSUSED */
2441 sys_pathconf(struct pathconf_args *uap)
2443 struct nlookupdata nd;
2444 struct vnode *vp;
2445 int error;
2447 vp = NULL;
2448 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2449 if (error == 0)
2450 error = nlookup(&nd);
2451 if (error == 0)
2452 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2453 nlookup_done(&nd);
2454 if (error == 0) {
2455 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2456 vput(vp);
2458 return (error);
2462 * XXX: daver
2463 * kern_readlink isn't properly split yet. There is a copyin burried
2464 * in VOP_READLINK().
2467 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2469 struct thread *td = curthread;
2470 struct proc *p = td->td_proc;
2471 struct vnode *vp;
2472 struct iovec aiov;
2473 struct uio auio;
2474 int error;
2476 if ((error = nlookup(nd)) != 0)
2477 return (error);
2478 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2479 if (error)
2480 return (error);
2481 if (vp->v_type != VLNK) {
2482 error = EINVAL;
2483 } else {
2484 aiov.iov_base = buf;
2485 aiov.iov_len = count;
2486 auio.uio_iov = &aiov;
2487 auio.uio_iovcnt = 1;
2488 auio.uio_offset = 0;
2489 auio.uio_rw = UIO_READ;
2490 auio.uio_segflg = UIO_USERSPACE;
2491 auio.uio_td = td;
2492 auio.uio_resid = count;
2493 error = VOP_READLINK(vp, &auio, p->p_ucred);
2495 vput(vp);
2496 *res = count - auio.uio_resid;
2497 return (error);
2501 * readlink_args(char *path, char *buf, int count)
2503 * Return target name of a symbolic link.
2506 sys_readlink(struct readlink_args *uap)
2508 struct nlookupdata nd;
2509 int error;
2511 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2512 if (error == 0) {
2513 error = kern_readlink(&nd, uap->buf, uap->count,
2514 &uap->sysmsg_result);
2516 nlookup_done(&nd);
2517 return (error);
2520 static int
2521 setfflags(struct vnode *vp, int flags)
2523 struct thread *td = curthread;
2524 struct proc *p = td->td_proc;
2525 int error;
2526 struct vattr vattr;
2529 * Prevent non-root users from setting flags on devices. When
2530 * a device is reused, users can retain ownership of the device
2531 * if they are allowed to set flags and programs assume that
2532 * chown can't fail when done as root.
2534 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2535 ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0))
2536 return (error);
2539 * note: vget is required for any operation that might mod the vnode
2540 * so VINACTIVE is properly cleared.
2542 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2543 VATTR_NULL(&vattr);
2544 vattr.va_flags = flags;
2545 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2546 vput(vp);
2548 return (error);
2552 * chflags(char *path, int flags)
2554 * Change flags of a file given a path name.
2556 /* ARGSUSED */
2558 sys_chflags(struct chflags_args *uap)
2560 struct nlookupdata nd;
2561 struct vnode *vp;
2562 int error;
2564 vp = NULL;
2565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2566 /* XXX Add NLC flag indicating modifying operation? */
2567 if (error == 0)
2568 error = nlookup(&nd);
2569 if (error == 0)
2570 error = ncp_writechk(&nd.nl_nch);
2571 if (error == 0)
2572 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2573 nlookup_done(&nd);
2574 if (error == 0) {
2575 error = setfflags(vp, uap->flags);
2576 vrele(vp);
2578 return (error);
2582 * lchflags(char *path, int flags)
2584 * Change flags of a file given a path name, but don't follow symlinks.
2586 /* ARGSUSED */
2588 sys_lchflags(struct lchflags_args *uap)
2590 struct nlookupdata nd;
2591 struct vnode *vp;
2592 int error;
2594 vp = NULL;
2595 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2596 /* XXX Add NLC flag indicating modifying operation? */
2597 if (error == 0)
2598 error = nlookup(&nd);
2599 if (error == 0)
2600 error = ncp_writechk(&nd.nl_nch);
2601 if (error == 0)
2602 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2603 nlookup_done(&nd);
2604 if (error == 0) {
2605 error = setfflags(vp, uap->flags);
2606 vrele(vp);
2608 return (error);
2612 * fchflags_args(int fd, int flags)
2614 * Change flags of a file given a file descriptor.
2616 /* ARGSUSED */
2618 sys_fchflags(struct fchflags_args *uap)
2620 struct thread *td = curthread;
2621 struct proc *p = td->td_proc;
2622 struct file *fp;
2623 int error;
2625 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2626 return (error);
2627 if (fp->f_nchandle.ncp)
2628 error = ncp_writechk(&fp->f_nchandle);
2629 if (error == 0)
2630 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2631 fdrop(fp);
2632 return (error);
2635 static int
2636 setfmode(struct vnode *vp, int mode)
2638 struct thread *td = curthread;
2639 struct proc *p = td->td_proc;
2640 int error;
2641 struct vattr vattr;
2644 * note: vget is required for any operation that might mod the vnode
2645 * so VINACTIVE is properly cleared.
2647 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2648 VATTR_NULL(&vattr);
2649 vattr.va_mode = mode & ALLPERMS;
2650 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2651 vput(vp);
2653 return error;
2657 kern_chmod(struct nlookupdata *nd, int mode)
2659 struct vnode *vp;
2660 int error;
2662 /* XXX Add NLC flag indicating modifying operation? */
2663 if ((error = nlookup(nd)) != 0)
2664 return (error);
2665 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2666 return (error);
2667 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2668 error = setfmode(vp, mode);
2669 vrele(vp);
2670 return (error);
2674 * chmod_args(char *path, int mode)
2676 * Change mode of a file given path name.
2678 /* ARGSUSED */
2680 sys_chmod(struct chmod_args *uap)
2682 struct nlookupdata nd;
2683 int error;
2685 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2686 if (error == 0)
2687 error = kern_chmod(&nd, uap->mode);
2688 nlookup_done(&nd);
2689 return (error);
2693 * lchmod_args(char *path, int mode)
2695 * Change mode of a file given path name (don't follow links.)
2697 /* ARGSUSED */
2699 sys_lchmod(struct lchmod_args *uap)
2701 struct nlookupdata nd;
2702 int error;
2704 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2705 if (error == 0)
2706 error = kern_chmod(&nd, uap->mode);
2707 nlookup_done(&nd);
2708 return (error);
2712 * fchmod_args(int fd, int mode)
2714 * Change mode of a file given a file descriptor.
2716 /* ARGSUSED */
2718 sys_fchmod(struct fchmod_args *uap)
2720 struct thread *td = curthread;
2721 struct proc *p = td->td_proc;
2722 struct file *fp;
2723 int error;
2725 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2726 return (error);
2727 if (fp->f_nchandle.ncp)
2728 error = ncp_writechk(&fp->f_nchandle);
2729 if (error == 0)
2730 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2731 fdrop(fp);
2732 return (error);
2735 static int
2736 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2738 struct thread *td = curthread;
2739 struct proc *p = td->td_proc;
2740 int error;
2741 struct vattr vattr;
2744 * note: vget is required for any operation that might mod the vnode
2745 * so VINACTIVE is properly cleared.
2747 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2748 VATTR_NULL(&vattr);
2749 vattr.va_uid = uid;
2750 vattr.va_gid = gid;
2751 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2752 vput(vp);
2754 return error;
2758 kern_chown(struct nlookupdata *nd, int uid, int gid)
2760 struct vnode *vp;
2761 int error;
2763 /* XXX Add NLC flag indicating modifying operation? */
2764 if ((error = nlookup(nd)) != 0)
2765 return (error);
2766 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2767 return (error);
2768 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2769 error = setfown(vp, uid, gid);
2770 vrele(vp);
2771 return (error);
2775 * chown(char *path, int uid, int gid)
2777 * Set ownership given a path name.
2780 sys_chown(struct chown_args *uap)
2782 struct nlookupdata nd;
2783 int error;
2785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2786 if (error == 0)
2787 error = kern_chown(&nd, uap->uid, uap->gid);
2788 nlookup_done(&nd);
2789 return (error);
2793 * lchown_args(char *path, int uid, int gid)
2795 * Set ownership given a path name, do not cross symlinks.
2798 sys_lchown(struct lchown_args *uap)
2800 struct nlookupdata nd;
2801 int error;
2803 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2804 if (error == 0)
2805 error = kern_chown(&nd, uap->uid, uap->gid);
2806 nlookup_done(&nd);
2807 return (error);
2811 * fchown_args(int fd, int uid, int gid)
2813 * Set ownership given a file descriptor.
2815 /* ARGSUSED */
2817 sys_fchown(struct fchown_args *uap)
2819 struct thread *td = curthread;
2820 struct proc *p = td->td_proc;
2821 struct file *fp;
2822 int error;
2824 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2825 return (error);
2826 if (fp->f_nchandle.ncp)
2827 error = ncp_writechk(&fp->f_nchandle);
2828 if (error == 0)
2829 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2830 fdrop(fp);
2831 return (error);
2834 static int
2835 getutimes(const struct timeval *tvp, struct timespec *tsp)
2837 struct timeval tv[2];
2839 if (tvp == NULL) {
2840 microtime(&tv[0]);
2841 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2842 tsp[1] = tsp[0];
2843 } else {
2844 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2845 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2847 return 0;
2850 static int
2851 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2853 struct thread *td = curthread;
2854 struct proc *p = td->td_proc;
2855 int error;
2856 struct vattr vattr;
2859 * note: vget is required for any operation that might mod the vnode
2860 * so VINACTIVE is properly cleared.
2862 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2863 VATTR_NULL(&vattr);
2864 vattr.va_atime = ts[0];
2865 vattr.va_mtime = ts[1];
2866 if (nullflag)
2867 vattr.va_vaflags |= VA_UTIMES_NULL;
2868 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2869 vput(vp);
2871 return error;
2875 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2877 struct timespec ts[2];
2878 struct vnode *vp;
2879 int error;
2881 if ((error = getutimes(tptr, ts)) != 0)
2882 return (error);
2883 /* XXX Add NLC flag indicating modifying operation? */
2884 if ((error = nlookup(nd)) != 0)
2885 return (error);
2886 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2887 return (error);
2888 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2889 return (error);
2890 error = setutimes(vp, ts, tptr == NULL);
2891 vrele(vp);
2892 return (error);
2896 * utimes_args(char *path, struct timeval *tptr)
2898 * Set the access and modification times of a file.
2901 sys_utimes(struct utimes_args *uap)
2903 struct timeval tv[2];
2904 struct nlookupdata nd;
2905 int error;
2907 if (uap->tptr) {
2908 error = copyin(uap->tptr, tv, sizeof(tv));
2909 if (error)
2910 return (error);
2912 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2913 if (error == 0)
2914 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2915 nlookup_done(&nd);
2916 return (error);
2920 * lutimes_args(char *path, struct timeval *tptr)
2922 * Set the access and modification times of a file.
2925 sys_lutimes(struct lutimes_args *uap)
2927 struct timeval tv[2];
2928 struct nlookupdata nd;
2929 int error;
2931 if (uap->tptr) {
2932 error = copyin(uap->tptr, tv, sizeof(tv));
2933 if (error)
2934 return (error);
2936 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2937 if (error == 0)
2938 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2939 nlookup_done(&nd);
2940 return (error);
2944 kern_futimes(int fd, struct timeval *tptr)
2946 struct thread *td = curthread;
2947 struct proc *p = td->td_proc;
2948 struct timespec ts[2];
2949 struct file *fp;
2950 int error;
2952 error = getutimes(tptr, ts);
2953 if (error)
2954 return (error);
2955 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2956 return (error);
2957 if (fp->f_nchandle.ncp)
2958 error = ncp_writechk(&fp->f_nchandle);
2959 if (error == 0)
2960 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2961 fdrop(fp);
2962 return (error);
2966 * futimes_args(int fd, struct timeval *tptr)
2968 * Set the access and modification times of a file.
2971 sys_futimes(struct futimes_args *uap)
2973 struct timeval tv[2];
2974 int error;
2976 if (uap->tptr) {
2977 error = copyin(uap->tptr, tv, sizeof(tv));
2978 if (error)
2979 return (error);
2982 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2984 return (error);
2988 kern_truncate(struct nlookupdata *nd, off_t length)
2990 struct vnode *vp;
2991 struct vattr vattr;
2992 int error;
2994 if (length < 0)
2995 return(EINVAL);
2996 /* XXX Add NLC flag indicating modifying operation? */
2997 if ((error = nlookup(nd)) != 0)
2998 return (error);
2999 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3000 return (error);
3001 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3002 return (error);
3003 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3004 vrele(vp);
3005 return (error);
3007 if (vp->v_type == VDIR) {
3008 error = EISDIR;
3009 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
3010 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
3011 VATTR_NULL(&vattr);
3012 vattr.va_size = length;
3013 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3015 vput(vp);
3016 return (error);
3020 * truncate(char *path, int pad, off_t length)
3022 * Truncate a file given its path name.
3025 sys_truncate(struct truncate_args *uap)
3027 struct nlookupdata nd;
3028 int error;
3030 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3031 if (error == 0)
3032 error = kern_truncate(&nd, uap->length);
3033 nlookup_done(&nd);
3034 return error;
3038 kern_ftruncate(int fd, off_t length)
3040 struct thread *td = curthread;
3041 struct proc *p = td->td_proc;
3042 struct vattr vattr;
3043 struct vnode *vp;
3044 struct file *fp;
3045 int error;
3047 if (length < 0)
3048 return(EINVAL);
3049 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3050 return (error);
3051 if (fp->f_nchandle.ncp) {
3052 error = ncp_writechk(&fp->f_nchandle);
3053 if (error)
3054 goto done;
3056 if ((fp->f_flag & FWRITE) == 0) {
3057 error = EINVAL;
3058 goto done;
3060 vp = (struct vnode *)fp->f_data;
3061 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3062 if (vp->v_type == VDIR) {
3063 error = EISDIR;
3064 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3065 VATTR_NULL(&vattr);
3066 vattr.va_size = length;
3067 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3069 vn_unlock(vp);
3070 done:
3071 fdrop(fp);
3072 return (error);
3076 * ftruncate_args(int fd, int pad, off_t length)
3078 * Truncate a file given a file descriptor.
3081 sys_ftruncate(struct ftruncate_args *uap)
3083 int error;
3085 error = kern_ftruncate(uap->fd, uap->length);
3087 return (error);
3091 * fsync(int fd)
3093 * Sync an open file.
3095 /* ARGSUSED */
3097 sys_fsync(struct fsync_args *uap)
3099 struct thread *td = curthread;
3100 struct proc *p = td->td_proc;
3101 struct vnode *vp;
3102 struct file *fp;
3103 vm_object_t obj;
3104 int error;
3106 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3107 return (error);
3108 vp = (struct vnode *)fp->f_data;
3109 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3110 if ((obj = vp->v_object) != NULL)
3111 vm_object_page_clean(obj, 0, 0, 0);
3112 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3113 error = buf_fsync(vp);
3114 vn_unlock(vp);
3115 fdrop(fp);
3116 return (error);
3120 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3122 struct nchandle fnchd;
3123 struct nchandle tnchd;
3124 struct namecache *ncp;
3125 struct vnode *fdvp;
3126 struct vnode *tdvp;
3127 struct mount *mp;
3128 int error;
3130 bwillinode(1);
3131 fromnd->nl_flags |= NLC_REFDVP;
3132 if ((error = nlookup(fromnd)) != 0)
3133 return (error);
3134 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3135 return (ENOENT);
3136 fnchd.mount = fromnd->nl_nch.mount;
3137 cache_hold(&fnchd);
3140 * unlock the source nch so we can lookup the target nch without
3141 * deadlocking. The target may or may not exist so we do not check
3142 * for a target vp like kern_mkdir() and other creation functions do.
3144 * The source and target directories are ref'd and rechecked after
3145 * everything is relocked to determine if the source or target file
3146 * has been renamed.
3148 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3149 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3150 cache_unlock(&fromnd->nl_nch);
3152 tond->nl_flags |= NLC_CREATE | NLC_REFDVP;
3153 if ((error = nlookup(tond)) != 0) {
3154 cache_drop(&fnchd);
3155 return (error);
3157 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3158 cache_drop(&fnchd);
3159 return (ENOENT);
3161 tnchd.mount = tond->nl_nch.mount;
3162 cache_hold(&tnchd);
3165 * If the source and target are the same there is nothing to do
3167 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3168 cache_drop(&fnchd);
3169 cache_drop(&tnchd);
3170 return (0);
3174 * Mount points cannot be renamed or overwritten
3176 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3177 NCF_ISMOUNTPT
3179 cache_drop(&fnchd);
3180 cache_drop(&tnchd);
3181 return (EINVAL);
3185 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3186 * may have become invalid while it was unlocked, nc_vp and nc_mount
3187 * could be NULL.
3189 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3190 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3191 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3192 cache_lock(&fromnd->nl_nch);
3193 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3194 } else {
3195 cache_unlock(&tond->nl_nch);
3196 cache_lock(&fromnd->nl_nch);
3197 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3198 cache_lock(&tond->nl_nch);
3199 cache_resolve(&tond->nl_nch, tond->nl_cred);
3201 fromnd->nl_flags |= NLC_NCPISLOCKED;
3204 * make sure the parent directories linkages are the same
3206 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3207 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3208 cache_drop(&fnchd);
3209 cache_drop(&tnchd);
3210 return (ENOENT);
3214 * Both the source and target must be within the same filesystem and
3215 * in the same filesystem as their parent directories within the
3216 * namecache topology.
3218 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3220 mp = fnchd.mount;
3221 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3222 mp != tond->nl_nch.mount) {
3223 cache_drop(&fnchd);
3224 cache_drop(&tnchd);
3225 return (EXDEV);
3229 * Make sure the mount point is writable
3231 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3232 cache_drop(&fnchd);
3233 cache_drop(&tnchd);
3234 return (error);
3238 * If the target exists and either the source or target is a directory,
3239 * then both must be directories.
3241 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3242 * have become NULL.
3244 if (tond->nl_nch.ncp->nc_vp) {
3245 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3246 error = ENOENT;
3247 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3248 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3249 error = ENOTDIR;
3250 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3251 error = EISDIR;
3256 * You cannot rename a source into itself or a subdirectory of itself.
3257 * We check this by travsersing the target directory upwards looking
3258 * for a match against the source.
3260 if (error == 0) {
3261 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3262 if (fromnd->nl_nch.ncp == ncp) {
3263 error = EINVAL;
3264 break;
3269 cache_drop(&fnchd);
3270 cache_drop(&tnchd);
3273 * Even though the namespaces are different, they may still represent
3274 * hardlinks to the same file. The filesystem might have a hard time
3275 * with this so we issue a NREMOVE of the source instead of a NRENAME
3276 * when we detect the situation.
3278 if (error == 0) {
3279 fdvp = fromnd->nl_dvp;
3280 tdvp = tond->nl_dvp;
3281 if (fdvp == NULL || tdvp == NULL) {
3282 error = EPERM;
3283 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3284 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3285 fromnd->nl_cred);
3286 } else {
3287 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3288 fdvp, tdvp, tond->nl_cred);
3291 return (error);
3295 * rename_args(char *from, char *to)
3297 * Rename files. Source and destination must either both be directories,
3298 * or both not be directories. If target is a directory, it must be empty.
3301 sys_rename(struct rename_args *uap)
3303 struct nlookupdata fromnd, tond;
3304 int error;
3306 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3307 if (error == 0) {
3308 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3309 if (error == 0)
3310 error = kern_rename(&fromnd, &tond);
3311 nlookup_done(&tond);
3313 nlookup_done(&fromnd);
3314 return (error);
3318 kern_mkdir(struct nlookupdata *nd, int mode)
3320 struct thread *td = curthread;
3321 struct proc *p = td->td_proc;
3322 struct vnode *vp;
3323 struct vattr vattr;
3324 int error;
3326 bwillinode(1);
3327 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3328 if ((error = nlookup(nd)) != 0)
3329 return (error);
3331 if (nd->nl_nch.ncp->nc_vp)
3332 return (EEXIST);
3333 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3334 return (error);
3335 VATTR_NULL(&vattr);
3336 vattr.va_type = VDIR;
3337 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3339 vp = NULL;
3340 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3341 if (error == 0)
3342 vput(vp);
3343 return (error);
3347 * mkdir_args(char *path, int mode)
3349 * Make a directory file.
3351 /* ARGSUSED */
3353 sys_mkdir(struct mkdir_args *uap)
3355 struct nlookupdata nd;
3356 int error;
3358 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3359 if (error == 0)
3360 error = kern_mkdir(&nd, uap->mode);
3361 nlookup_done(&nd);
3362 return (error);
3366 kern_rmdir(struct nlookupdata *nd)
3368 int error;
3370 bwillinode(1);
3371 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3372 if ((error = nlookup(nd)) != 0)
3373 return (error);
3376 * Do not allow directories representing mount points to be
3377 * deleted, even if empty. Check write perms on mount point
3378 * in case the vnode is aliased (aka nullfs).
3380 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3381 return (EINVAL);
3382 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3383 return (error);
3384 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3385 return (error);
3389 * rmdir_args(char *path)
3391 * Remove a directory file.
3393 /* ARGSUSED */
3395 sys_rmdir(struct rmdir_args *uap)
3397 struct nlookupdata nd;
3398 int error;
3400 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3401 if (error == 0)
3402 error = kern_rmdir(&nd);
3403 nlookup_done(&nd);
3404 return (error);
3408 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3409 enum uio_seg direction)
3411 struct thread *td = curthread;
3412 struct proc *p = td->td_proc;
3413 struct vnode *vp;
3414 struct file *fp;
3415 struct uio auio;
3416 struct iovec aiov;
3417 off_t loff;
3418 int error, eofflag;
3420 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3421 return (error);
3422 if ((fp->f_flag & FREAD) == 0) {
3423 error = EBADF;
3424 goto done;
3426 vp = (struct vnode *)fp->f_data;
3427 unionread:
3428 if (vp->v_type != VDIR) {
3429 error = EINVAL;
3430 goto done;
3432 aiov.iov_base = buf;
3433 aiov.iov_len = count;
3434 auio.uio_iov = &aiov;
3435 auio.uio_iovcnt = 1;
3436 auio.uio_rw = UIO_READ;
3437 auio.uio_segflg = direction;
3438 auio.uio_td = td;
3439 auio.uio_resid = count;
3440 loff = auio.uio_offset = fp->f_offset;
3441 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3442 fp->f_offset = auio.uio_offset;
3443 if (error)
3444 goto done;
3445 if (count == auio.uio_resid) {
3446 if (union_dircheckp) {
3447 error = union_dircheckp(td, &vp, fp);
3448 if (error == -1)
3449 goto unionread;
3450 if (error)
3451 goto done;
3453 #if 0
3454 if ((vp->v_flag & VROOT) &&
3455 (vp->v_mount->mnt_flag & MNT_UNION)) {
3456 struct vnode *tvp = vp;
3457 vp = vp->v_mount->mnt_vnodecovered;
3458 vref(vp);
3459 fp->f_data = vp;
3460 fp->f_offset = 0;
3461 vrele(tvp);
3462 goto unionread;
3464 #endif
3468 * WARNING! *basep may not be wide enough to accomodate the
3469 * seek offset. XXX should we hack this to return the upper 32 bits
3470 * for offsets greater then 4G?
3472 if (basep) {
3473 *basep = (long)loff;
3475 *res = count - auio.uio_resid;
3476 done:
3477 fdrop(fp);
3478 return (error);
3482 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3484 * Read a block of directory entries in a file system independent format.
3487 sys_getdirentries(struct getdirentries_args *uap)
3489 long base;
3490 int error;
3492 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3493 &uap->sysmsg_result, UIO_USERSPACE);
3495 if (error == 0 && uap->basep)
3496 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3497 return (error);
3501 * getdents_args(int fd, char *buf, size_t count)
3504 sys_getdents(struct getdents_args *uap)
3506 int error;
3508 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3509 &uap->sysmsg_result, UIO_USERSPACE);
3511 return (error);
3515 * umask(int newmask)
3517 * Set the mode mask for creation of filesystem nodes.
3519 * MP SAFE
3522 sys_umask(struct umask_args *uap)
3524 struct thread *td = curthread;
3525 struct proc *p = td->td_proc;
3526 struct filedesc *fdp;
3528 fdp = p->p_fd;
3529 uap->sysmsg_result = fdp->fd_cmask;
3530 fdp->fd_cmask = uap->newmask & ALLPERMS;
3531 return (0);
3535 * revoke(char *path)
3537 * Void all references to file by ripping underlying filesystem
3538 * away from vnode.
3540 /* ARGSUSED */
3542 sys_revoke(struct revoke_args *uap)
3544 struct nlookupdata nd;
3545 struct vattr vattr;
3546 struct vnode *vp;
3547 struct ucred *cred;
3548 int error;
3550 vp = NULL;
3551 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3552 if (error == 0)
3553 error = nlookup(&nd);
3554 if (error == 0)
3555 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3556 cred = crhold(nd.nl_cred);
3557 nlookup_done(&nd);
3558 if (error == 0) {
3559 if (vp->v_type != VCHR && vp->v_type != VBLK)
3560 error = EINVAL;
3561 if (error == 0)
3562 error = VOP_GETATTR(vp, &vattr);
3563 if (error == 0 && cred->cr_uid != vattr.va_uid)
3564 error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
3565 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3566 error = 0;
3567 vx_lock(vp);
3568 VOP_REVOKE(vp, REVOKEALL);
3569 vx_unlock(vp);
3571 vrele(vp);
3573 if (cred)
3574 crfree(cred);
3575 return (error);
3579 * getfh_args(char *fname, fhandle_t *fhp)
3581 * Get (NFS) file handle
3583 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3584 * mount. This allows nullfs mounts to be explicitly exported.
3586 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3588 * nullfs mounts of subdirectories are not safe. That is, it will
3589 * work, but you do not really have protection against access to
3590 * the related parent directories.
3593 sys_getfh(struct getfh_args *uap)
3595 struct thread *td = curthread;
3596 struct nlookupdata nd;
3597 fhandle_t fh;
3598 struct vnode *vp;
3599 struct mount *mp;
3600 int error;
3603 * Must be super user
3605 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3606 return (error);
3608 vp = NULL;
3609 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3610 if (error == 0)
3611 error = nlookup(&nd);
3612 if (error == 0)
3613 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3614 mp = nd.nl_nch.mount;
3615 nlookup_done(&nd);
3616 if (error == 0) {
3617 bzero(&fh, sizeof(fh));
3618 fh.fh_fsid = mp->mnt_stat.f_fsid;
3619 error = VFS_VPTOFH(vp, &fh.fh_fid);
3620 vput(vp);
3621 if (error == 0)
3622 error = copyout(&fh, uap->fhp, sizeof(fh));
3624 return (error);
3628 * fhopen_args(const struct fhandle *u_fhp, int flags)
3630 * syscall for the rpc.lockd to use to translate a NFS file handle into
3631 * an open descriptor.
3633 * warning: do not remove the priv_check() call or this becomes one giant
3634 * security hole.
3637 sys_fhopen(struct fhopen_args *uap)
3639 struct thread *td = curthread;
3640 struct proc *p = td->td_proc;
3641 struct mount *mp;
3642 struct vnode *vp;
3643 struct fhandle fhp;
3644 struct vattr vat;
3645 struct vattr *vap = &vat;
3646 struct flock lf;
3647 int fmode, mode, error, type;
3648 struct file *nfp;
3649 struct file *fp;
3650 int indx;
3653 * Must be super user
3655 error = priv_check(td, PRIV_ROOT);
3656 if (error)
3657 return (error);
3659 fmode = FFLAGS(uap->flags);
3660 /* why not allow a non-read/write open for our lockd? */
3661 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3662 return (EINVAL);
3663 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3664 if (error)
3665 return(error);
3666 /* find the mount point */
3667 mp = vfs_getvfs(&fhp.fh_fsid);
3668 if (mp == NULL)
3669 return (ESTALE);
3670 /* now give me my vnode, it gets returned to me locked */
3671 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3672 if (error)
3673 return (error);
3675 * from now on we have to make sure not
3676 * to forget about the vnode
3677 * any error that causes an abort must vput(vp)
3678 * just set error = err and 'goto bad;'.
3682 * from vn_open
3684 if (vp->v_type == VLNK) {
3685 error = EMLINK;
3686 goto bad;
3688 if (vp->v_type == VSOCK) {
3689 error = EOPNOTSUPP;
3690 goto bad;
3692 mode = 0;
3693 if (fmode & (FWRITE | O_TRUNC)) {
3694 if (vp->v_type == VDIR) {
3695 error = EISDIR;
3696 goto bad;
3698 error = vn_writechk(vp, NULL);
3699 if (error)
3700 goto bad;
3701 mode |= VWRITE;
3703 if (fmode & FREAD)
3704 mode |= VREAD;
3705 if (mode) {
3706 error = VOP_ACCESS(vp, mode, p->p_ucred);
3707 if (error)
3708 goto bad;
3710 if (fmode & O_TRUNC) {
3711 vn_unlock(vp); /* XXX */
3712 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3713 VATTR_NULL(vap);
3714 vap->va_size = 0;
3715 error = VOP_SETATTR(vp, vap, p->p_ucred);
3716 if (error)
3717 goto bad;
3721 * VOP_OPEN needs the file pointer so it can potentially override
3722 * it.
3724 * WARNING! no f_nchandle will be associated when fhopen()ing a
3725 * directory. XXX
3727 if ((error = falloc(p, &nfp, &indx)) != 0)
3728 goto bad;
3729 fp = nfp;
3731 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3732 if (error) {
3734 * setting f_ops this way prevents VOP_CLOSE from being
3735 * called or fdrop() releasing the vp from v_data. Since
3736 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3738 fp->f_ops = &badfileops;
3739 fp->f_data = NULL;
3740 goto bad_drop;
3744 * The fp is given its own reference, we still have our ref and lock.
3746 * Assert that all regular files must be created with a VM object.
3748 if (vp->v_type == VREG && vp->v_object == NULL) {
3749 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3750 goto bad_drop;
3754 * The open was successful. Handle any locking requirements.
3756 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3757 lf.l_whence = SEEK_SET;
3758 lf.l_start = 0;
3759 lf.l_len = 0;
3760 if (fmode & O_EXLOCK)
3761 lf.l_type = F_WRLCK;
3762 else
3763 lf.l_type = F_RDLCK;
3764 if (fmode & FNONBLOCK)
3765 type = 0;
3766 else
3767 type = F_WAIT;
3768 vn_unlock(vp);
3769 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3771 * release our private reference.
3773 fsetfd(p, NULL, indx);
3774 fdrop(fp);
3775 vrele(vp);
3776 return (error);
3778 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3779 fp->f_flag |= FHASLOCK;
3783 * Clean up. Associate the file pointer with the previously
3784 * reserved descriptor and return it.
3786 vput(vp);
3787 fsetfd(p, fp, indx);
3788 fdrop(fp);
3789 uap->sysmsg_result = indx;
3790 return (0);
3792 bad_drop:
3793 fsetfd(p, NULL, indx);
3794 fdrop(fp);
3795 bad:
3796 vput(vp);
3797 return (error);
3801 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3804 sys_fhstat(struct fhstat_args *uap)
3806 struct thread *td = curthread;
3807 struct stat sb;
3808 fhandle_t fh;
3809 struct mount *mp;
3810 struct vnode *vp;
3811 int error;
3814 * Must be super user
3816 error = priv_check(td, PRIV_ROOT);
3817 if (error)
3818 return (error);
3820 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3821 if (error)
3822 return (error);
3824 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3825 return (ESTALE);
3826 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3827 return (error);
3828 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3829 vput(vp);
3830 if (error)
3831 return (error);
3832 error = copyout(&sb, uap->sb, sizeof(sb));
3833 return (error);
3837 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3840 sys_fhstatfs(struct fhstatfs_args *uap)
3842 struct thread *td = curthread;
3843 struct proc *p = td->td_proc;
3844 struct statfs *sp;
3845 struct mount *mp;
3846 struct vnode *vp;
3847 struct statfs sb;
3848 char *fullpath, *freepath;
3849 fhandle_t fh;
3850 int error;
3853 * Must be super user
3855 if ((error = priv_check(td, PRIV_ROOT)))
3856 return (error);
3858 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3859 return (error);
3861 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3862 return (ESTALE);
3864 if (p != NULL && !chroot_visible_mnt(mp, p))
3865 return (ESTALE);
3867 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3868 return (error);
3869 mp = vp->v_mount;
3870 sp = &mp->mnt_stat;
3871 vput(vp);
3872 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3873 return (error);
3875 error = mount_path(p, mp, &fullpath, &freepath);
3876 if (error)
3877 return(error);
3878 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3879 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3880 kfree(freepath, M_TEMP);
3882 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3883 if (priv_check(td, PRIV_ROOT)) {
3884 bcopy(sp, &sb, sizeof(sb));
3885 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3886 sp = &sb;
3888 return (copyout(sp, uap->buf, sizeof(*sp)));
3892 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3895 sys_fhstatvfs(struct fhstatvfs_args *uap)
3897 struct thread *td = curthread;
3898 struct proc *p = td->td_proc;
3899 struct statvfs *sp;
3900 struct mount *mp;
3901 struct vnode *vp;
3902 fhandle_t fh;
3903 int error;
3906 * Must be super user
3908 if ((error = priv_check(td, PRIV_ROOT)))
3909 return (error);
3911 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3912 return (error);
3914 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3915 return (ESTALE);
3917 if (p != NULL && !chroot_visible_mnt(mp, p))
3918 return (ESTALE);
3920 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3921 return (error);
3922 mp = vp->v_mount;
3923 sp = &mp->mnt_vstat;
3924 vput(vp);
3925 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3926 return (error);
3928 sp->f_flag = 0;
3929 if (mp->mnt_flag & MNT_RDONLY)
3930 sp->f_flag |= ST_RDONLY;
3931 if (mp->mnt_flag & MNT_NOSUID)
3932 sp->f_flag |= ST_NOSUID;
3934 return (copyout(sp, uap->buf, sizeof(*sp)));
3939 * Syscall to push extended attribute configuration information into the
3940 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3941 * a command (int cmd), and attribute name and misc data. For now, the
3942 * attribute name is left in userspace for consumption by the VFS_op.
3943 * It will probably be changed to be copied into sysspace by the
3944 * syscall in the future, once issues with various consumers of the
3945 * attribute code have raised their hands.
3947 * Currently this is used only by UFS Extended Attributes.
3950 sys_extattrctl(struct extattrctl_args *uap)
3952 struct nlookupdata nd;
3953 struct mount *mp;
3954 struct vnode *vp;
3955 int error;
3957 vp = NULL;
3958 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3959 if (error == 0)
3960 error = nlookup(&nd);
3961 if (error == 0) {
3962 mp = nd.nl_nch.mount;
3963 error = VFS_EXTATTRCTL(mp, uap->cmd,
3964 uap->attrname, uap->arg,
3965 nd.nl_cred);
3967 nlookup_done(&nd);
3968 return (error);
3972 * Syscall to set a named extended attribute on a file or directory.
3973 * Accepts attribute name, and a uio structure pointing to the data to set.
3974 * The uio is consumed in the style of writev(). The real work happens
3975 * in VOP_SETEXTATTR().
3978 sys_extattr_set_file(struct extattr_set_file_args *uap)
3980 char attrname[EXTATTR_MAXNAMELEN];
3981 struct iovec aiov[UIO_SMALLIOV];
3982 struct iovec *needfree;
3983 struct nlookupdata nd;
3984 struct iovec *iov;
3985 struct vnode *vp;
3986 struct uio auio;
3987 u_int iovlen;
3988 u_int cnt;
3989 int error;
3990 int i;
3992 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3993 if (error)
3994 return (error);
3996 vp = NULL;
3997 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3998 if (error == 0)
3999 error = nlookup(&nd);
4000 if (error == 0)
4001 error = ncp_writechk(&nd.nl_nch);
4002 if (error == 0)
4003 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4004 if (error) {
4005 nlookup_done(&nd);
4006 return (error);
4009 needfree = NULL;
4010 iovlen = uap->iovcnt * sizeof(struct iovec);
4011 if (uap->iovcnt > UIO_SMALLIOV) {
4012 if (uap->iovcnt > UIO_MAXIOV) {
4013 error = EINVAL;
4014 goto done;
4016 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4017 needfree = iov;
4018 } else {
4019 iov = aiov;
4021 auio.uio_iov = iov;
4022 auio.uio_iovcnt = uap->iovcnt;
4023 auio.uio_rw = UIO_WRITE;
4024 auio.uio_segflg = UIO_USERSPACE;
4025 auio.uio_td = nd.nl_td;
4026 auio.uio_offset = 0;
4027 if ((error = copyin(uap->iovp, iov, iovlen)))
4028 goto done;
4029 auio.uio_resid = 0;
4030 for (i = 0; i < uap->iovcnt; i++) {
4031 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4032 error = EINVAL;
4033 goto done;
4035 auio.uio_resid += iov->iov_len;
4036 iov++;
4038 cnt = auio.uio_resid;
4039 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4040 cnt -= auio.uio_resid;
4041 uap->sysmsg_result = cnt;
4042 done:
4043 vput(vp);
4044 nlookup_done(&nd);
4045 if (needfree)
4046 FREE(needfree, M_IOV);
4047 return (error);
4051 * Syscall to get a named extended attribute on a file or directory.
4052 * Accepts attribute name, and a uio structure pointing to a buffer for the
4053 * data. The uio is consumed in the style of readv(). The real work
4054 * happens in VOP_GETEXTATTR();
4057 sys_extattr_get_file(struct extattr_get_file_args *uap)
4059 char attrname[EXTATTR_MAXNAMELEN];
4060 struct iovec aiov[UIO_SMALLIOV];
4061 struct iovec *needfree;
4062 struct nlookupdata nd;
4063 struct iovec *iov;
4064 struct vnode *vp;
4065 struct uio auio;
4066 u_int iovlen;
4067 u_int cnt;
4068 int error;
4069 int i;
4071 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4072 if (error)
4073 return (error);
4075 vp = NULL;
4076 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4077 if (error == 0)
4078 error = nlookup(&nd);
4079 if (error == 0)
4080 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4081 if (error) {
4082 nlookup_done(&nd);
4083 return (error);
4086 iovlen = uap->iovcnt * sizeof (struct iovec);
4087 needfree = NULL;
4088 if (uap->iovcnt > UIO_SMALLIOV) {
4089 if (uap->iovcnt > UIO_MAXIOV) {
4090 error = EINVAL;
4091 goto done;
4093 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4094 needfree = iov;
4095 } else {
4096 iov = aiov;
4098 auio.uio_iov = iov;
4099 auio.uio_iovcnt = uap->iovcnt;
4100 auio.uio_rw = UIO_READ;
4101 auio.uio_segflg = UIO_USERSPACE;
4102 auio.uio_td = nd.nl_td;
4103 auio.uio_offset = 0;
4104 if ((error = copyin(uap->iovp, iov, iovlen)))
4105 goto done;
4106 auio.uio_resid = 0;
4107 for (i = 0; i < uap->iovcnt; i++) {
4108 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4109 error = EINVAL;
4110 goto done;
4112 auio.uio_resid += iov->iov_len;
4113 iov++;
4115 cnt = auio.uio_resid;
4116 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4117 cnt -= auio.uio_resid;
4118 uap->sysmsg_result = cnt;
4119 done:
4120 vput(vp);
4121 nlookup_done(&nd);
4122 if (needfree)
4123 FREE(needfree, M_IOV);
4124 return(error);
4128 * Syscall to delete a named extended attribute from a file or directory.
4129 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4132 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4134 char attrname[EXTATTR_MAXNAMELEN];
4135 struct nlookupdata nd;
4136 struct vnode *vp;
4137 int error;
4139 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4140 if (error)
4141 return(error);
4143 vp = NULL;
4144 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4145 if (error == 0)
4146 error = nlookup(&nd);
4147 if (error == 0)
4148 error = ncp_writechk(&nd.nl_nch);
4149 if (error == 0)
4150 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4151 if (error) {
4152 nlookup_done(&nd);
4153 return (error);
4156 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4157 vput(vp);
4158 nlookup_done(&nd);
4159 return(error);
4163 * Determine if the mount is visible to the process.
4165 static int
4166 chroot_visible_mnt(struct mount *mp, struct proc *p)
4168 struct nchandle nch;
4171 * Traverse from the mount point upwards. If we hit the process
4172 * root then the mount point is visible to the process.
4174 nch = mp->mnt_ncmountpt;
4175 while (nch.ncp) {
4176 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4177 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4178 return(1);
4180 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4181 nch = nch.mount->mnt_ncmounton;
4182 } else {
4183 nch.ncp = nch.ncp->nc_parent;
4188 * If the mount point is not visible to the process, but the
4189 * process root is in a subdirectory of the mount, return
4190 * TRUE anyway.
4192 if (p->p_fd->fd_nrdir.mount == mp)
4193 return(1);
4195 return(0);