xref: /dflybsd-src/sys/kern/vfs_syscalls.c (revision 7ec6eaed9e6eb69ae18f61d7496e7dfa5bade99f)
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.
9  *
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.
25  *
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.
37  *
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 $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/conf.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
55 #include <sys/file.h>
56 #include <sys/linker.h>
57 #include <sys/stat.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/proc.h>
61 #include <sys/priv.h>
62 #include <sys/jail.h>
63 #include <sys/namei.h>
64 #include <sys/nlookup.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
67 #include <sys/spinlock.h>
68 #include <sys/kern_syscall.h>
69 #include <sys/objcache.h>
70 #include <sys/sysctl.h>
71 
72 #include <sys/buf2.h>
73 #include <sys/file2.h>
74 #include <sys/spinlock2.h>
75 
76 #include <vm/vm.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
82 
83 #include <vfs/union/union.h>
84 
85 static void mount_warning(struct mount *mp, const char *ctl, ...);
86 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
87 static int checkvp_chdir (struct vnode *vn, struct thread *td);
88 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
89 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
90 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
91 static int getutimes (const struct timeval *, struct timespec *);
92 static int setfown (struct vnode *, uid_t, gid_t);
93 static int setfmode (struct vnode *, int);
94 static int setfflags (struct vnode *, int);
95 static int setutimes (struct vnode *, struct vattr *,
96 			const struct timespec *, int);
97 static int	usermount = 0;	/* if 1, non-root can mount fs. */
98 
99 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
100 
101 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
102 
103 /*
104  * Virtual File System System Calls
105  */
106 
107 /*
108  * Mount a file system.
109  */
110 /*
111  * mount_args(char *type, char *path, int flags, caddr_t data)
112  */
113 /* ARGSUSED */
114 int
115 sys_mount(struct mount_args *uap)
116 {
117 	struct thread *td = curthread;
118 	struct proc *p = td->td_proc;
119 	struct vnode *vp;
120 	struct nchandle nch;
121 	struct mount *mp;
122 	struct vfsconf *vfsp;
123 	int error, flag = 0, flag2 = 0;
124 	int hasmount;
125 	struct vattr va;
126 	struct nlookupdata nd;
127 	char fstypename[MFSNAMELEN];
128 	struct ucred *cred = p->p_ucred;
129 
130 	KKASSERT(p);
131 	if (jailed(cred))
132 		return (EPERM);
133 	if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
134 		return (error);
135 	/*
136 	 * Do not allow NFS export by non-root users.
137 	 */
138 	if (uap->flags & MNT_EXPORTED) {
139 		error = priv_check(td, PRIV_ROOT);
140 		if (error)
141 			return (error);
142 	}
143 	/*
144 	 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
145 	 */
146 	if (priv_check(td, PRIV_ROOT))
147 		uap->flags |= MNT_NOSUID | MNT_NODEV;
148 
149 	/*
150 	 * Lookup the requested path and extract the nch and vnode.
151 	 */
152 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
153 	if (error == 0) {
154 		if ((error = nlookup(&nd)) == 0) {
155 			if (nd.nl_nch.ncp->nc_vp == NULL)
156 				error = ENOENT;
157 		}
158 	}
159 	if (error) {
160 		nlookup_done(&nd);
161 		return (error);
162 	}
163 
164 	/*
165 	 * Extract the locked+refd ncp and cleanup the nd structure
166 	 */
167 	nch = nd.nl_nch;
168 	cache_zero(&nd.nl_nch);
169 	nlookup_done(&nd);
170 
171 	if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
172 		hasmount = 1;
173 	else
174 		hasmount = 0;
175 
176 
177 	/*
178 	 * now we have the locked ref'd nch and unreferenced vnode.
179 	 */
180 	vp = nch.ncp->nc_vp;
181 	if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
182 		cache_put(&nch);
183 		return (error);
184 	}
185 	cache_unlock(&nch);
186 
187 	/*
188 	 * Now we have an unlocked ref'd nch and a locked ref'd vp
189 	 */
190 	if (uap->flags & MNT_UPDATE) {
191 		if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
192 			cache_drop(&nch);
193 			vput(vp);
194 			return (EINVAL);
195 		}
196 		mp = vp->v_mount;
197 		flag = mp->mnt_flag;
198 		flag2 = mp->mnt_kern_flag;
199 		/*
200 		 * We only allow the filesystem to be reloaded if it
201 		 * is currently mounted read-only.
202 		 */
203 		if ((uap->flags & MNT_RELOAD) &&
204 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
205 			cache_drop(&nch);
206 			vput(vp);
207 			return (EOPNOTSUPP);	/* Needs translation */
208 		}
209 		/*
210 		 * Only root, or the user that did the original mount is
211 		 * permitted to update it.
212 		 */
213 		if (mp->mnt_stat.f_owner != cred->cr_uid &&
214 		    (error = priv_check(td, PRIV_ROOT))) {
215 			cache_drop(&nch);
216 			vput(vp);
217 			return (error);
218 		}
219 		if (vfs_busy(mp, LK_NOWAIT)) {
220 			cache_drop(&nch);
221 			vput(vp);
222 			return (EBUSY);
223 		}
224 		if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
225 			cache_drop(&nch);
226 			vfs_unbusy(mp);
227 			vput(vp);
228 			return (EBUSY);
229 		}
230 		vp->v_flag |= VMOUNT;
231 		mp->mnt_flag |=
232 		    uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
233 		vn_unlock(vp);
234 		goto update;
235 	}
236 	/*
237 	 * If the user is not root, ensure that they own the directory
238 	 * onto which we are attempting to mount.
239 	 */
240 	if ((error = VOP_GETATTR(vp, &va)) ||
241 	    (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
242 		cache_drop(&nch);
243 		vput(vp);
244 		return (error);
245 	}
246 	if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
247 		cache_drop(&nch);
248 		vput(vp);
249 		return (error);
250 	}
251 	if (vp->v_type != VDIR) {
252 		cache_drop(&nch);
253 		vput(vp);
254 		return (ENOTDIR);
255 	}
256 	if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
257 		cache_drop(&nch);
258 		vput(vp);
259 		return (EPERM);
260 	}
261 	if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
262 		cache_drop(&nch);
263 		vput(vp);
264 		return (error);
265 	}
266 	vfsp = vfsconf_find_by_name(fstypename);
267 	if (vfsp == NULL) {
268 		linker_file_t lf;
269 
270 		/* Only load modules for root (very important!) */
271 		if ((error = priv_check(td, PRIV_ROOT)) != 0) {
272 			cache_drop(&nch);
273 			vput(vp);
274 			return error;
275 		}
276 		error = linker_load_file(fstypename, &lf);
277 		if (error || lf == NULL) {
278 			cache_drop(&nch);
279 			vput(vp);
280 			if (lf == NULL)
281 				error = ENODEV;
282 			return error;
283 		}
284 		lf->userrefs++;
285 		/* lookup again, see if the VFS was loaded */
286 		vfsp = vfsconf_find_by_name(fstypename);
287 		if (vfsp == NULL) {
288 			lf->userrefs--;
289 			linker_file_unload(lf);
290 			cache_drop(&nch);
291 			vput(vp);
292 			return (ENODEV);
293 		}
294 	}
295 	if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
296 		cache_drop(&nch);
297 		vput(vp);
298 		return (EBUSY);
299 	}
300 	vp->v_flag |= VMOUNT;
301 
302 	/*
303 	 * Allocate and initialize the filesystem.
304 	 */
305 	mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
306 	TAILQ_INIT(&mp->mnt_nvnodelist);
307 	TAILQ_INIT(&mp->mnt_reservedvnlist);
308 	TAILQ_INIT(&mp->mnt_jlist);
309 	mp->mnt_nvnodelistsize = 0;
310 	lockinit(&mp->mnt_lock, "vfslock", 0, 0);
311 	vfs_busy(mp, LK_NOWAIT);
312 	mp->mnt_op = vfsp->vfc_vfsops;
313 	mp->mnt_vfc = vfsp;
314 	vfsp->vfc_refcount++;
315 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
316 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
317 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
318 	mp->mnt_stat.f_owner = cred->cr_uid;
319 	mp->mnt_iosize_max = DFLTPHYS;
320 	vn_unlock(vp);
321 update:
322 	/*
323 	 * Set the mount level flags.
324 	 */
325 	if (uap->flags & MNT_RDONLY)
326 		mp->mnt_flag |= MNT_RDONLY;
327 	else if (mp->mnt_flag & MNT_RDONLY)
328 		mp->mnt_kern_flag |= MNTK_WANTRDWR;
329 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
330 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
331 	    MNT_NOSYMFOLLOW | MNT_IGNORE |
332 	    MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
333 	mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
334 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
335 	    MNT_NOSYMFOLLOW | MNT_IGNORE |
336 	    MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
337 	/*
338 	 * Mount the filesystem.
339 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
340 	 * get.
341 	 */
342 	error = VFS_MOUNT(mp, uap->path, uap->data, cred);
343 	if (mp->mnt_flag & MNT_UPDATE) {
344 		if (mp->mnt_kern_flag & MNTK_WANTRDWR)
345 			mp->mnt_flag &= ~MNT_RDONLY;
346 		mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
347 		mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
348 		if (error) {
349 			mp->mnt_flag = flag;
350 			mp->mnt_kern_flag = flag2;
351 		}
352 		vfs_unbusy(mp);
353 		vp->v_flag &= ~VMOUNT;
354 		vrele(vp);
355 		cache_drop(&nch);
356 		return (error);
357 	}
358 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
359 	/*
360 	 * Put the new filesystem on the mount list after root.  The mount
361 	 * point gets its own mnt_ncmountpt (unless the VFS already set one
362 	 * up) which represents the root of the mount.  The lookup code
363 	 * detects the mount point going forward and checks the root of
364 	 * the mount going backwards.
365 	 *
366 	 * It is not necessary to invalidate or purge the vnode underneath
367 	 * because elements under the mount will be given their own glue
368 	 * namecache record.
369 	 */
370 	if (!error) {
371 		if (mp->mnt_ncmountpt.ncp == NULL) {
372 			/*
373 			 * allocate, then unlock, but leave the ref intact
374 			 */
375 			cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
376 			cache_unlock(&mp->mnt_ncmountpt);
377 		}
378 		mp->mnt_ncmounton = nch;		/* inherits ref */
379 		nch.ncp->nc_flag |= NCF_ISMOUNTPT;
380 
381 		/* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
382 		vp->v_flag &= ~VMOUNT;
383 		mountlist_insert(mp, MNTINS_LAST);
384 		vn_unlock(vp);
385 		checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
386 		error = vfs_allocate_syncvnode(mp);
387 		vfs_unbusy(mp);
388 		error = VFS_START(mp, 0);
389 		vrele(vp);
390 	} else {
391 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
392 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
393 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
394 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
395 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
396 		vp->v_flag &= ~VMOUNT;
397 		mp->mnt_vfc->vfc_refcount--;
398 		vfs_unbusy(mp);
399 		kfree(mp, M_MOUNT);
400 		cache_drop(&nch);
401 		vput(vp);
402 	}
403 	return (error);
404 }
405 
406 /*
407  * Scan all active processes to see if any of them have a current
408  * or root directory onto which the new filesystem has just been
409  * mounted. If so, replace them with the new mount point.
410  *
411  * The passed ncp is ref'd and locked (from the mount code) and
412  * must be associated with the vnode representing the root of the
413  * mount point.
414  */
415 struct checkdirs_info {
416 	struct nchandle old_nch;
417 	struct nchandle new_nch;
418 	struct vnode *old_vp;
419 	struct vnode *new_vp;
420 };
421 
422 static int checkdirs_callback(struct proc *p, void *data);
423 
424 static void
425 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
426 {
427 	struct checkdirs_info info;
428 	struct vnode *olddp;
429 	struct vnode *newdp;
430 	struct mount *mp;
431 
432 	/*
433 	 * If the old mount point's vnode has a usecount of 1, it is not
434 	 * being held as a descriptor anywhere.
435 	 */
436 	olddp = old_nch->ncp->nc_vp;
437 	if (olddp == NULL || olddp->v_sysref.refcnt == 1)
438 		return;
439 
440 	/*
441 	 * Force the root vnode of the new mount point to be resolved
442 	 * so we can update any matching processes.
443 	 */
444 	mp = new_nch->mount;
445 	if (VFS_ROOT(mp, &newdp))
446 		panic("mount: lost mount");
447 	cache_setunresolved(new_nch);
448 	cache_setvp(new_nch, newdp);
449 
450 	/*
451 	 * Special handling of the root node
452 	 */
453 	if (rootvnode == olddp) {
454 		vref(newdp);
455 		vfs_cache_setroot(newdp, cache_hold(new_nch));
456 	}
457 
458 	/*
459 	 * Pass newdp separately so the callback does not have to access
460 	 * it via new_nch->ncp->nc_vp.
461 	 */
462 	info.old_nch = *old_nch;
463 	info.new_nch = *new_nch;
464 	info.new_vp = newdp;
465 	allproc_scan(checkdirs_callback, &info);
466 	vput(newdp);
467 }
468 
469 /*
470  * NOTE: callback is not MP safe because the scanned process's filedesc
471  * structure can be ripped out from under us, amoung other things.
472  */
473 static int
474 checkdirs_callback(struct proc *p, void *data)
475 {
476 	struct checkdirs_info *info = data;
477 	struct filedesc *fdp;
478 	struct nchandle ncdrop1;
479 	struct nchandle ncdrop2;
480 	struct vnode *vprele1;
481 	struct vnode *vprele2;
482 
483 	if ((fdp = p->p_fd) != NULL) {
484 		cache_zero(&ncdrop1);
485 		cache_zero(&ncdrop2);
486 		vprele1 = NULL;
487 		vprele2 = NULL;
488 
489 		/*
490 		 * MPUNSAFE - XXX fdp can be pulled out from under a
491 		 * foreign process.
492 		 *
493 		 * A shared filedesc is ok, we don't have to copy it
494 		 * because we are making this change globally.
495 		 */
496 		spin_lock_wr(&fdp->fd_spin);
497 		if (fdp->fd_ncdir.mount == info->old_nch.mount &&
498 		    fdp->fd_ncdir.ncp == info->old_nch.ncp) {
499 			vprele1 = fdp->fd_cdir;
500 			vref(info->new_vp);
501 			fdp->fd_cdir = info->new_vp;
502 			ncdrop1 = fdp->fd_ncdir;
503 			cache_copy(&info->new_nch, &fdp->fd_ncdir);
504 		}
505 		if (fdp->fd_nrdir.mount == info->old_nch.mount &&
506 		    fdp->fd_nrdir.ncp == info->old_nch.ncp) {
507 			vprele2 = fdp->fd_rdir;
508 			vref(info->new_vp);
509 			fdp->fd_rdir = info->new_vp;
510 			ncdrop2 = fdp->fd_nrdir;
511 			cache_copy(&info->new_nch, &fdp->fd_nrdir);
512 		}
513 		spin_unlock_wr(&fdp->fd_spin);
514 		if (ncdrop1.ncp)
515 			cache_drop(&ncdrop1);
516 		if (ncdrop2.ncp)
517 			cache_drop(&ncdrop2);
518 		if (vprele1)
519 			vrele(vprele1);
520 		if (vprele2)
521 			vrele(vprele2);
522 	}
523 	return(0);
524 }
525 
526 /*
527  * Unmount a file system.
528  *
529  * Note: unmount takes a path to the vnode mounted on as argument,
530  * not special file (as before).
531  */
532 /*
533  * umount_args(char *path, int flags)
534  */
535 /* ARGSUSED */
536 int
537 sys_unmount(struct unmount_args *uap)
538 {
539 	struct thread *td = curthread;
540 	struct proc *p = td->td_proc;
541 	struct mount *mp = NULL;
542 	int error;
543 	struct nlookupdata nd;
544 
545 	KKASSERT(p);
546 	if (p->p_ucred->cr_prison != NULL)
547 		return (EPERM);
548 	if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
549 		return (error);
550 
551 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
552 	if (error == 0)
553 		error = nlookup(&nd);
554 	if (error)
555 		goto out;
556 
557 	mp = nd.nl_nch.mount;
558 
559 	/*
560 	 * Only root, or the user that did the original mount is
561 	 * permitted to unmount this filesystem.
562 	 */
563 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
564 	    (error = priv_check(td, PRIV_ROOT)))
565 		goto out;
566 
567 	/*
568 	 * Don't allow unmounting the root file system.
569 	 */
570 	if (mp->mnt_flag & MNT_ROOTFS) {
571 		error = EINVAL;
572 		goto out;
573 	}
574 
575 	/*
576 	 * Must be the root of the filesystem
577 	 */
578 	if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
579 		error = EINVAL;
580 		goto out;
581 	}
582 
583 out:
584 	nlookup_done(&nd);
585 	if (error)
586 		return (error);
587 	return (dounmount(mp, uap->flags));
588 }
589 
590 /*
591  * Do the actual file system unmount.
592  */
593 static int
594 dounmount_interlock(struct mount *mp)
595 {
596 	if (mp->mnt_kern_flag & MNTK_UNMOUNT)
597 		return (EBUSY);
598 	mp->mnt_kern_flag |= MNTK_UNMOUNT;
599 	return(0);
600 }
601 
602 int
603 dounmount(struct mount *mp, int flags)
604 {
605 	struct namecache *ncp;
606 	struct nchandle nch;
607 	struct vnode *vp;
608 	int error;
609 	int async_flag;
610 	int lflags;
611 	int freeok = 1;
612 
613 	/*
614 	 * Exclusive access for unmounting purposes
615 	 */
616 	if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
617 		return (error);
618 
619 	/*
620 	 * Allow filesystems to detect that a forced unmount is in progress.
621 	 */
622 	if (flags & MNT_FORCE)
623 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
624 	lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
625 	error = lockmgr(&mp->mnt_lock, lflags);
626 	if (error) {
627 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
628 		if (mp->mnt_kern_flag & MNTK_MWAIT)
629 			wakeup(mp);
630 		return (error);
631 	}
632 
633 	if (mp->mnt_flag & MNT_EXPUBLIC)
634 		vfs_setpublicfs(NULL, NULL, NULL);
635 
636 	vfs_msync(mp, MNT_WAIT);
637 	async_flag = mp->mnt_flag & MNT_ASYNC;
638 	mp->mnt_flag &=~ MNT_ASYNC;
639 
640 	/*
641 	 * If this filesystem isn't aliasing other filesystems,
642 	 * try to invalidate any remaining namecache entries and
643 	 * check the count afterwords.
644 	 */
645 	if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
646 		cache_lock(&mp->mnt_ncmountpt);
647 		cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
648 		cache_unlock(&mp->mnt_ncmountpt);
649 
650 		if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
651 		    (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
652 
653 			if ((flags & MNT_FORCE) == 0) {
654 				error = EBUSY;
655 				mount_warning(mp, "Cannot unmount: "
656 						  "%d namecache "
657 						  "references still "
658 						  "present",
659 						  ncp->nc_refs - 1);
660 			} else {
661 				mount_warning(mp, "Forced unmount: "
662 						  "%d namecache "
663 						  "references still "
664 						  "present",
665 						  ncp->nc_refs - 1);
666 				freeok = 0;
667 			}
668 		}
669 	}
670 
671 	/*
672 	 * nchandle records ref the mount structure.  Expect a count of 1
673 	 * (our mount->mnt_ncmountpt).
674 	 */
675 	if (mp->mnt_refs != 1) {
676 		if ((flags & MNT_FORCE) == 0) {
677 			mount_warning(mp, "Cannot unmount: "
678 					  "%d process references still "
679 					  "present", mp->mnt_refs);
680 			error = EBUSY;
681 		} else {
682 			mount_warning(mp, "Forced unmount: "
683 					  "%d process references still "
684 					  "present", mp->mnt_refs);
685 			freeok = 0;
686 		}
687 	}
688 
689 	/*
690 	 * Decomission our special mnt_syncer vnode.  This also stops
691 	 * the vnlru code.  If we are unable to unmount we recommission
692 	 * the vnode.
693 	 */
694 	if (error == 0) {
695 		if ((vp = mp->mnt_syncer) != NULL) {
696 			mp->mnt_syncer = NULL;
697 			vrele(vp);
698 		}
699 		if (((mp->mnt_flag & MNT_RDONLY) ||
700 		     (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
701 		    (flags & MNT_FORCE)) {
702 			error = VFS_UNMOUNT(mp, flags);
703 		}
704 	}
705 	if (error) {
706 		if (mp->mnt_syncer == NULL)
707 			vfs_allocate_syncvnode(mp);
708 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
709 		mp->mnt_flag |= async_flag;
710 		lockmgr(&mp->mnt_lock, LK_RELEASE);
711 		if (mp->mnt_kern_flag & MNTK_MWAIT)
712 			wakeup(mp);
713 		return (error);
714 	}
715 	/*
716 	 * Clean up any journals still associated with the mount after
717 	 * filesystem activity has ceased.
718 	 */
719 	journal_remove_all_journals(mp,
720 	    ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
721 
722 	mountlist_remove(mp);
723 
724 	/*
725 	 * Remove any installed vnode ops here so the individual VFSs don't
726 	 * have to.
727 	 */
728 	vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
729 	vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
730 	vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
731 	vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
732 	vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
733 
734 	if (mp->mnt_ncmountpt.ncp != NULL) {
735 		nch = mp->mnt_ncmountpt;
736 		cache_zero(&mp->mnt_ncmountpt);
737 		cache_clrmountpt(&nch);
738 		cache_drop(&nch);
739 	}
740 	if (mp->mnt_ncmounton.ncp != NULL) {
741 		nch = mp->mnt_ncmounton;
742 		cache_zero(&mp->mnt_ncmounton);
743 		cache_clrmountpt(&nch);
744 		cache_drop(&nch);
745 	}
746 
747 	mp->mnt_vfc->vfc_refcount--;
748 	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
749 		panic("unmount: dangling vnode");
750 	lockmgr(&mp->mnt_lock, LK_RELEASE);
751 	if (mp->mnt_kern_flag & MNTK_MWAIT)
752 		wakeup(mp);
753 	if (freeok)
754 		kfree(mp, M_MOUNT);
755 	return (0);
756 }
757 
758 static
759 void
760 mount_warning(struct mount *mp, const char *ctl, ...)
761 {
762 	char *ptr;
763 	char *buf;
764 	__va_list va;
765 
766 	__va_start(va, ctl);
767 	if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
768 		kprintf("unmount(%s): ", ptr);
769 		kvprintf(ctl, va);
770 		kprintf("\n");
771 		kfree(buf, M_TEMP);
772 	} else {
773 		kprintf("unmount(%p", mp);
774 		if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
775 			kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
776 		kprintf("): ");
777 		kvprintf(ctl, va);
778 		kprintf("\n");
779 	}
780 	__va_end(va);
781 }
782 
783 /*
784  * Shim cache_fullpath() to handle the case where a process is chrooted into
785  * a subdirectory of a mount.  In this case if the root mount matches the
786  * process root directory's mount we have to specify the process's root
787  * directory instead of the mount point, because the mount point might
788  * be above the root directory.
789  */
790 static
791 int
792 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
793 {
794 	struct nchandle *nch;
795 
796 	if (p && p->p_fd->fd_nrdir.mount == mp)
797 		nch = &p->p_fd->fd_nrdir;
798 	else
799 		nch = &mp->mnt_ncmountpt;
800 	return(cache_fullpath(p, nch, rb, fb));
801 }
802 
803 /*
804  * Sync each mounted filesystem.
805  */
806 
807 #ifdef DEBUG
808 static int syncprt = 0;
809 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
810 #endif /* DEBUG */
811 
812 static int sync_callback(struct mount *mp, void *data);
813 
814 /* ARGSUSED */
815 int
816 sys_sync(struct sync_args *uap)
817 {
818 	mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
819 #ifdef DEBUG
820 	/*
821 	 * print out buffer pool stat information on each sync() call.
822 	 */
823 	if (syncprt)
824 		vfs_bufstats();
825 #endif /* DEBUG */
826 	return (0);
827 }
828 
829 static
830 int
831 sync_callback(struct mount *mp, void *data __unused)
832 {
833 	int asyncflag;
834 
835 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
836 		asyncflag = mp->mnt_flag & MNT_ASYNC;
837 		mp->mnt_flag &= ~MNT_ASYNC;
838 		vfs_msync(mp, MNT_NOWAIT);
839 		VFS_SYNC(mp, MNT_NOWAIT);
840 		mp->mnt_flag |= asyncflag;
841 	}
842 	return(0);
843 }
844 
845 /* XXX PRISON: could be per prison flag */
846 static int prison_quotas;
847 #if 0
848 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
849 #endif
850 
851 /*
852  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
853  *
854  * Change filesystem quotas.
855  */
856 /* ARGSUSED */
857 int
858 sys_quotactl(struct quotactl_args *uap)
859 {
860 	struct nlookupdata nd;
861 	struct thread *td;
862 	struct proc *p;
863 	struct mount *mp;
864 	int error;
865 
866 	td = curthread;
867 	p = td->td_proc;
868 	if (p->p_ucred->cr_prison && !prison_quotas)
869 		return (EPERM);
870 
871 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
872 	if (error == 0)
873 		error = nlookup(&nd);
874 	if (error == 0) {
875 		mp = nd.nl_nch.mount;
876 		error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
877 				    uap->arg, nd.nl_cred);
878 	}
879 	nlookup_done(&nd);
880 	return (error);
881 }
882 
883 /*
884  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
885  *		void *buf, int buflen)
886  *
887  * This function operates on a mount point and executes the specified
888  * operation using the specified control data, and possibly returns data.
889  *
890  * The actual number of bytes stored in the result buffer is returned, 0
891  * if none, otherwise an error is returned.
892  */
893 /* ARGSUSED */
894 int
895 sys_mountctl(struct mountctl_args *uap)
896 {
897 	struct thread *td = curthread;
898 	struct proc *p = td->td_proc;
899 	struct file *fp;
900 	void *ctl = NULL;
901 	void *buf = NULL;
902 	char *path = NULL;
903 	int error;
904 
905 	/*
906 	 * Sanity and permissions checks.  We must be root.
907 	 */
908 	KKASSERT(p);
909 	if (p->p_ucred->cr_prison != NULL)
910 		return (EPERM);
911 	if ((error = priv_check(td, PRIV_ROOT)) != 0)
912 		return (error);
913 
914 	/*
915 	 * Argument length checks
916 	 */
917 	if (uap->ctllen < 0 || uap->ctllen > 1024)
918 		return (EINVAL);
919 	if (uap->buflen < 0 || uap->buflen > 16 * 1024)
920 		return (EINVAL);
921 	if (uap->path == NULL)
922 		return (EINVAL);
923 
924 	/*
925 	 * Allocate the necessary buffers and copyin data
926 	 */
927 	path = objcache_get(namei_oc, M_WAITOK);
928 	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
929 	if (error)
930 		goto done;
931 
932 	if (uap->ctllen) {
933 		ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
934 		error = copyin(uap->ctl, ctl, uap->ctllen);
935 		if (error)
936 			goto done;
937 	}
938 	if (uap->buflen)
939 		buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
940 
941 	/*
942 	 * Validate the descriptor
943 	 */
944 	if (uap->fd >= 0) {
945 		fp = holdfp(p->p_fd, uap->fd, -1);
946 		if (fp == NULL) {
947 			error = EBADF;
948 			goto done;
949 		}
950 	} else {
951 		fp = NULL;
952 	}
953 
954 	/*
955 	 * Execute the internal kernel function and clean up.
956 	 */
957 	error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
958 	if (fp)
959 		fdrop(fp);
960 	if (error == 0 && uap->sysmsg_result > 0)
961 		error = copyout(buf, uap->buf, uap->sysmsg_result);
962 done:
963 	if (path)
964 		objcache_put(namei_oc, path);
965 	if (ctl)
966 		kfree(ctl, M_TEMP);
967 	if (buf)
968 		kfree(buf, M_TEMP);
969 	return (error);
970 }
971 
972 /*
973  * Execute a mount control operation by resolving the path to a mount point
974  * and calling vop_mountctl().
975  *
976  * Use the mount point from the nch instead of the vnode so nullfs mounts
977  * can properly spike the VOP.
978  */
979 int
980 kern_mountctl(const char *path, int op, struct file *fp,
981 		const void *ctl, int ctllen,
982 		void *buf, int buflen, int *res)
983 {
984 	struct vnode *vp;
985 	struct mount *mp;
986 	struct nlookupdata nd;
987 	int error;
988 
989 	*res = 0;
990 	vp = NULL;
991 	error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
992 	if (error == 0)
993 		error = nlookup(&nd);
994 	if (error == 0)
995 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
996 	mp = nd.nl_nch.mount;
997 	nlookup_done(&nd);
998 	if (error)
999 		return (error);
1000 
1001 	/*
1002 	 * Must be the root of the filesystem
1003 	 */
1004 	if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1005 		vput(vp);
1006 		return (EINVAL);
1007 	}
1008 	error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1009 				buf, buflen, res);
1010 	vput(vp);
1011 	return (error);
1012 }
1013 
1014 int
1015 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1016 {
1017 	struct thread *td = curthread;
1018 	struct proc *p = td->td_proc;
1019 	struct mount *mp;
1020 	struct statfs *sp;
1021 	char *fullpath, *freepath;
1022 	int error;
1023 
1024 	if ((error = nlookup(nd)) != 0)
1025 		return (error);
1026 	mp = nd->nl_nch.mount;
1027 	sp = &mp->mnt_stat;
1028 	if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1029 		return (error);
1030 
1031 	error = mount_path(p, mp, &fullpath, &freepath);
1032 	if (error)
1033 		return(error);
1034 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1035 	strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1036 	kfree(freepath, M_TEMP);
1037 
1038 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1039 	bcopy(sp, buf, sizeof(*buf));
1040 	/* Only root should have access to the fsid's. */
1041 	if (priv_check(td, PRIV_ROOT))
1042 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1043 	return (0);
1044 }
1045 
1046 /*
1047  * statfs_args(char *path, struct statfs *buf)
1048  *
1049  * Get filesystem statistics.
1050  */
1051 int
1052 sys_statfs(struct statfs_args *uap)
1053 {
1054 	struct nlookupdata nd;
1055 	struct statfs buf;
1056 	int error;
1057 
1058 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1059 	if (error == 0)
1060 		error = kern_statfs(&nd, &buf);
1061 	nlookup_done(&nd);
1062 	if (error == 0)
1063 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1064 	return (error);
1065 }
1066 
1067 int
1068 kern_fstatfs(int fd, struct statfs *buf)
1069 {
1070 	struct thread *td = curthread;
1071 	struct proc *p = td->td_proc;
1072 	struct file *fp;
1073 	struct mount *mp;
1074 	struct statfs *sp;
1075 	char *fullpath, *freepath;
1076 	int error;
1077 
1078 	KKASSERT(p);
1079 	if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1080 		return (error);
1081 	mp = ((struct vnode *)fp->f_data)->v_mount;
1082 	if (mp == NULL) {
1083 		error = EBADF;
1084 		goto done;
1085 	}
1086 	if (fp->f_cred == NULL) {
1087 		error = EINVAL;
1088 		goto done;
1089 	}
1090 	sp = &mp->mnt_stat;
1091 	if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1092 		goto done;
1093 
1094 	if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1095 		goto done;
1096 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1097 	strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1098 	kfree(freepath, M_TEMP);
1099 
1100 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1101 	bcopy(sp, buf, sizeof(*buf));
1102 
1103 	/* Only root should have access to the fsid's. */
1104 	if (priv_check(td, PRIV_ROOT))
1105 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1106 	error = 0;
1107 done:
1108 	fdrop(fp);
1109 	return (error);
1110 }
1111 
1112 /*
1113  * fstatfs_args(int fd, struct statfs *buf)
1114  *
1115  * Get filesystem statistics.
1116  */
1117 int
1118 sys_fstatfs(struct fstatfs_args *uap)
1119 {
1120 	struct statfs buf;
1121 	int error;
1122 
1123 	error = kern_fstatfs(uap->fd, &buf);
1124 
1125 	if (error == 0)
1126 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1127 	return (error);
1128 }
1129 
1130 int
1131 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1132 {
1133 	struct mount *mp;
1134 	struct statvfs *sp;
1135 	int error;
1136 
1137 	if ((error = nlookup(nd)) != 0)
1138 		return (error);
1139 	mp = nd->nl_nch.mount;
1140 	sp = &mp->mnt_vstat;
1141 	if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1142 		return (error);
1143 
1144 	sp->f_flag = 0;
1145 	if (mp->mnt_flag & MNT_RDONLY)
1146 		sp->f_flag |= ST_RDONLY;
1147 	if (mp->mnt_flag & MNT_NOSUID)
1148 		sp->f_flag |= ST_NOSUID;
1149 	bcopy(sp, buf, sizeof(*buf));
1150 	return (0);
1151 }
1152 
1153 /*
1154  * statfs_args(char *path, struct statfs *buf)
1155  *
1156  * Get filesystem statistics.
1157  */
1158 int
1159 sys_statvfs(struct statvfs_args *uap)
1160 {
1161 	struct nlookupdata nd;
1162 	struct statvfs buf;
1163 	int error;
1164 
1165 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1166 	if (error == 0)
1167 		error = kern_statvfs(&nd, &buf);
1168 	nlookup_done(&nd);
1169 	if (error == 0)
1170 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1171 	return (error);
1172 }
1173 
1174 int
1175 kern_fstatvfs(int fd, struct statvfs *buf)
1176 {
1177 	struct thread *td = curthread;
1178 	struct proc *p = td->td_proc;
1179 	struct file *fp;
1180 	struct mount *mp;
1181 	struct statvfs *sp;
1182 	int error;
1183 
1184 	KKASSERT(p);
1185 	if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1186 		return (error);
1187 	mp = ((struct vnode *)fp->f_data)->v_mount;
1188 	if (mp == NULL) {
1189 		error = EBADF;
1190 		goto done;
1191 	}
1192 	if (fp->f_cred == NULL) {
1193 		error = EINVAL;
1194 		goto done;
1195 	}
1196 	sp = &mp->mnt_vstat;
1197 	if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1198 		goto done;
1199 
1200 	sp->f_flag = 0;
1201 	if (mp->mnt_flag & MNT_RDONLY)
1202 		sp->f_flag |= ST_RDONLY;
1203 	if (mp->mnt_flag & MNT_NOSUID)
1204 		sp->f_flag |= ST_NOSUID;
1205 
1206 	bcopy(sp, buf, sizeof(*buf));
1207 	error = 0;
1208 done:
1209 	fdrop(fp);
1210 	return (error);
1211 }
1212 
1213 /*
1214  * fstatfs_args(int fd, struct statfs *buf)
1215  *
1216  * Get filesystem statistics.
1217  */
1218 int
1219 sys_fstatvfs(struct fstatvfs_args *uap)
1220 {
1221 	struct statvfs buf;
1222 	int error;
1223 
1224 	error = kern_fstatvfs(uap->fd, &buf);
1225 
1226 	if (error == 0)
1227 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1228 	return (error);
1229 }
1230 
1231 /*
1232  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1233  *
1234  * Get statistics on all filesystems.
1235  */
1236 
1237 struct getfsstat_info {
1238 	struct statfs *sfsp;
1239 	long count;
1240 	long maxcount;
1241 	int error;
1242 	int flags;
1243 	struct proc *p;
1244 };
1245 
1246 static int getfsstat_callback(struct mount *, void *);
1247 
1248 /* ARGSUSED */
1249 int
1250 sys_getfsstat(struct getfsstat_args *uap)
1251 {
1252 	struct thread *td = curthread;
1253 	struct proc *p = td->td_proc;
1254 	struct getfsstat_info info;
1255 
1256 	bzero(&info, sizeof(info));
1257 
1258 	info.maxcount = uap->bufsize / sizeof(struct statfs);
1259 	info.sfsp = uap->buf;
1260 	info.count = 0;
1261 	info.flags = uap->flags;
1262 	info.p = p;
1263 
1264 	mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1265 	if (info.sfsp && info.count > info.maxcount)
1266 		uap->sysmsg_result = info.maxcount;
1267 	else
1268 		uap->sysmsg_result = info.count;
1269 	return (info.error);
1270 }
1271 
1272 static int
1273 getfsstat_callback(struct mount *mp, void *data)
1274 {
1275 	struct getfsstat_info *info = data;
1276 	struct statfs *sp;
1277 	char *freepath;
1278 	char *fullpath;
1279 	int error;
1280 
1281 	if (info->sfsp && info->count < info->maxcount) {
1282 		if (info->p && !chroot_visible_mnt(mp, info->p))
1283 			return(0);
1284 		sp = &mp->mnt_stat;
1285 
1286 		/*
1287 		 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1288 		 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1289 		 * overrides MNT_WAIT.
1290 		 */
1291 		if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1292 		    (info->flags & MNT_WAIT)) &&
1293 		    (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1294 			return(0);
1295 		}
1296 		sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1297 
1298 		error = mount_path(info->p, mp, &fullpath, &freepath);
1299 		if (error) {
1300 			info->error = error;
1301 			return(-1);
1302 		}
1303 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1304 		strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1305 		kfree(freepath, M_TEMP);
1306 
1307 		error = copyout(sp, info->sfsp, sizeof(*sp));
1308 		if (error) {
1309 			info->error = error;
1310 			return (-1);
1311 		}
1312 		++info->sfsp;
1313 	}
1314 	info->count++;
1315 	return(0);
1316 }
1317 
1318 /*
1319  * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1320 		   long bufsize, int flags)
1321  *
1322  * Get statistics on all filesystems.
1323  */
1324 
1325 struct getvfsstat_info {
1326 	struct statfs *sfsp;
1327 	struct statvfs *vsfsp;
1328 	long count;
1329 	long maxcount;
1330 	int error;
1331 	int flags;
1332 	struct proc *p;
1333 };
1334 
1335 static int getvfsstat_callback(struct mount *, void *);
1336 
1337 /* ARGSUSED */
1338 int
1339 sys_getvfsstat(struct getvfsstat_args *uap)
1340 {
1341 	struct thread *td = curthread;
1342 	struct proc *p = td->td_proc;
1343 	struct getvfsstat_info info;
1344 
1345 	bzero(&info, sizeof(info));
1346 
1347 	info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1348 	info.sfsp = uap->buf;
1349 	info.vsfsp = uap->vbuf;
1350 	info.count = 0;
1351 	info.flags = uap->flags;
1352 	info.p = p;
1353 
1354 	mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1355 	if (info.vsfsp && info.count > info.maxcount)
1356 		uap->sysmsg_result = info.maxcount;
1357 	else
1358 		uap->sysmsg_result = info.count;
1359 	return (info.error);
1360 }
1361 
1362 static int
1363 getvfsstat_callback(struct mount *mp, void *data)
1364 {
1365 	struct getvfsstat_info *info = data;
1366 	struct statfs *sp;
1367 	struct statvfs *vsp;
1368 	char *freepath;
1369 	char *fullpath;
1370 	int error;
1371 
1372 	if (info->vsfsp && info->count < info->maxcount) {
1373 		if (info->p && !chroot_visible_mnt(mp, info->p))
1374 			return(0);
1375 		sp = &mp->mnt_stat;
1376 		vsp = &mp->mnt_vstat;
1377 
1378 		/*
1379 		 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1380 		 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1381 		 * overrides MNT_WAIT.
1382 		 */
1383 		if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1384 		    (info->flags & MNT_WAIT)) &&
1385 		    (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1386 			return(0);
1387 		}
1388 		sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1389 
1390 		if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1391 		    (info->flags & MNT_WAIT)) &&
1392 		    (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1393 			return(0);
1394 		}
1395 		vsp->f_flag = 0;
1396 		if (mp->mnt_flag & MNT_RDONLY)
1397 			vsp->f_flag |= ST_RDONLY;
1398 		if (mp->mnt_flag & MNT_NOSUID)
1399 			vsp->f_flag |= ST_NOSUID;
1400 
1401 		error = mount_path(info->p, mp, &fullpath, &freepath);
1402 		if (error) {
1403 			info->error = error;
1404 			return(-1);
1405 		}
1406 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1407 		strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1408 		kfree(freepath, M_TEMP);
1409 
1410 		error = copyout(sp, info->sfsp, sizeof(*sp));
1411 		if (error == 0)
1412 			error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1413 		if (error) {
1414 			info->error = error;
1415 			return (-1);
1416 		}
1417 		++info->sfsp;
1418 		++info->vsfsp;
1419 	}
1420 	info->count++;
1421 	return(0);
1422 }
1423 
1424 
1425 /*
1426  * fchdir_args(int fd)
1427  *
1428  * Change current working directory to a given file descriptor.
1429  */
1430 /* ARGSUSED */
1431 int
1432 sys_fchdir(struct fchdir_args *uap)
1433 {
1434 	struct thread *td = curthread;
1435 	struct proc *p = td->td_proc;
1436 	struct filedesc *fdp = p->p_fd;
1437 	struct vnode *vp, *ovp;
1438 	struct mount *mp;
1439 	struct file *fp;
1440 	struct nchandle nch, onch, tnch;
1441 	int error;
1442 
1443 	if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1444 		return (error);
1445 	vp = (struct vnode *)fp->f_data;
1446 	vref(vp);
1447 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1448 	if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1449 		error = ENOTDIR;
1450 	else
1451 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1452 	if (error) {
1453 		vput(vp);
1454 		fdrop(fp);
1455 		return (error);
1456 	}
1457 	cache_copy(&fp->f_nchandle, &nch);
1458 
1459 	/*
1460 	 * If the ncp has become a mount point, traverse through
1461 	 * the mount point.
1462 	 */
1463 
1464 	while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1465 	       (mp = cache_findmount(&nch)) != NULL
1466 	) {
1467 		error = nlookup_mp(mp, &tnch);
1468 		if (error == 0) {
1469 			cache_unlock(&tnch);	/* leave ref intact */
1470 			vput(vp);
1471 			vp = tnch.ncp->nc_vp;
1472 			error = vget(vp, LK_SHARED);
1473 			KKASSERT(error == 0);
1474 			cache_drop(&nch);
1475 			nch = tnch;
1476 		}
1477 	}
1478 	if (error == 0) {
1479 		ovp = fdp->fd_cdir;
1480 		onch = fdp->fd_ncdir;
1481 		vn_unlock(vp);		/* leave ref intact */
1482 		fdp->fd_cdir = vp;
1483 		fdp->fd_ncdir = nch;
1484 		cache_drop(&onch);
1485 		vrele(ovp);
1486 	} else {
1487 		cache_drop(&nch);
1488 		vput(vp);
1489 	}
1490 	fdrop(fp);
1491 	return (error);
1492 }
1493 
1494 int
1495 kern_chdir(struct nlookupdata *nd)
1496 {
1497 	struct thread *td = curthread;
1498 	struct proc *p = td->td_proc;
1499 	struct filedesc *fdp = p->p_fd;
1500 	struct vnode *vp, *ovp;
1501 	struct nchandle onch;
1502 	int error;
1503 
1504 	if ((error = nlookup(nd)) != 0)
1505 		return (error);
1506 	if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1507 		return (ENOENT);
1508 	if ((error = vget(vp, LK_SHARED)) != 0)
1509 		return (error);
1510 
1511 	error = checkvp_chdir(vp, td);
1512 	vn_unlock(vp);
1513 	if (error == 0) {
1514 		ovp = fdp->fd_cdir;
1515 		onch = fdp->fd_ncdir;
1516 		cache_unlock(&nd->nl_nch);	/* leave reference intact */
1517 		fdp->fd_ncdir = nd->nl_nch;
1518 		fdp->fd_cdir = vp;
1519 		cache_drop(&onch);
1520 		vrele(ovp);
1521 		cache_zero(&nd->nl_nch);
1522 	} else {
1523 		vrele(vp);
1524 	}
1525 	return (error);
1526 }
1527 
1528 /*
1529  * chdir_args(char *path)
1530  *
1531  * Change current working directory (``.'').
1532  */
1533 int
1534 sys_chdir(struct chdir_args *uap)
1535 {
1536 	struct nlookupdata nd;
1537 	int error;
1538 
1539 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1540 	if (error == 0)
1541 		error = kern_chdir(&nd);
1542 	nlookup_done(&nd);
1543 	return (error);
1544 }
1545 
1546 /*
1547  * Helper function for raised chroot(2) security function:  Refuse if
1548  * any filedescriptors are open directories.
1549  */
1550 static int
1551 chroot_refuse_vdir_fds(struct filedesc *fdp)
1552 {
1553 	struct vnode *vp;
1554 	struct file *fp;
1555 	int error;
1556 	int fd;
1557 
1558 	for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1559 		if ((error = holdvnode(fdp, fd, &fp)) != 0)
1560 			continue;
1561 		vp = (struct vnode *)fp->f_data;
1562 		if (vp->v_type != VDIR) {
1563 			fdrop(fp);
1564 			continue;
1565 		}
1566 		fdrop(fp);
1567 		return(EPERM);
1568 	}
1569 	return (0);
1570 }
1571 
1572 /*
1573  * This sysctl determines if we will allow a process to chroot(2) if it
1574  * has a directory open:
1575  *	0: disallowed for all processes.
1576  *	1: allowed for processes that were not already chroot(2)'ed.
1577  *	2: allowed for all processes.
1578  */
1579 
1580 static int chroot_allow_open_directories = 1;
1581 
1582 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1583      &chroot_allow_open_directories, 0, "");
1584 
1585 /*
1586  * chroot to the specified namecache entry.  We obtain the vp from the
1587  * namecache data.  The passed ncp must be locked and referenced and will
1588  * remain locked and referenced on return.
1589  */
1590 int
1591 kern_chroot(struct nchandle *nch)
1592 {
1593 	struct thread *td = curthread;
1594 	struct proc *p = td->td_proc;
1595 	struct filedesc *fdp = p->p_fd;
1596 	struct vnode *vp;
1597 	int error;
1598 
1599 	/*
1600 	 * Only root can chroot
1601 	 */
1602 	if ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0)
1603 		return (error);
1604 
1605 	/*
1606 	 * Disallow open directory descriptors (fchdir() breakouts).
1607 	 */
1608 	if (chroot_allow_open_directories == 0 ||
1609 	   (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1610 		if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1611 			return (error);
1612 	}
1613 	if ((vp = nch->ncp->nc_vp) == NULL)
1614 		return (ENOENT);
1615 
1616 	if ((error = vget(vp, LK_SHARED)) != 0)
1617 		return (error);
1618 
1619 	/*
1620 	 * Check the validity of vp as a directory to change to and
1621 	 * associate it with rdir/jdir.
1622 	 */
1623 	error = checkvp_chdir(vp, td);
1624 	vn_unlock(vp);			/* leave reference intact */
1625 	if (error == 0) {
1626 		vrele(fdp->fd_rdir);
1627 		fdp->fd_rdir = vp;	/* reference inherited by fd_rdir */
1628 		cache_drop(&fdp->fd_nrdir);
1629 		cache_copy(nch, &fdp->fd_nrdir);
1630 		if (fdp->fd_jdir == NULL) {
1631 			fdp->fd_jdir = vp;
1632 			vref(fdp->fd_jdir);
1633 			cache_copy(nch, &fdp->fd_njdir);
1634 		}
1635 	} else {
1636 		vrele(vp);
1637 	}
1638 	return (error);
1639 }
1640 
1641 /*
1642  * chroot_args(char *path)
1643  *
1644  * Change notion of root (``/'') directory.
1645  */
1646 /* ARGSUSED */
1647 int
1648 sys_chroot(struct chroot_args *uap)
1649 {
1650 	struct thread *td = curthread;
1651 	struct nlookupdata nd;
1652 	int error;
1653 
1654 	KKASSERT(td->td_proc);
1655 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1656 	if (error) {
1657 		nlookup_done(&nd);
1658 		return(error);
1659 	}
1660 	nd.nl_flags |= NLC_EXEC;
1661 	error = nlookup(&nd);
1662 	if (error == 0)
1663 		error = kern_chroot(&nd.nl_nch);
1664 	nlookup_done(&nd);
1665 	return(error);
1666 }
1667 
1668 /*
1669  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1670  * determine whether it is legal to chdir to the vnode.  The vnode's state
1671  * is not changed by this call.
1672  */
1673 int
1674 checkvp_chdir(struct vnode *vp, struct thread *td)
1675 {
1676 	int error;
1677 
1678 	if (vp->v_type != VDIR)
1679 		error = ENOTDIR;
1680 	else
1681 		error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1682 	return (error);
1683 }
1684 
1685 int
1686 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1687 {
1688 	struct thread *td = curthread;
1689 	struct proc *p = td->td_proc;
1690 	struct lwp *lp = td->td_lwp;
1691 	struct filedesc *fdp = p->p_fd;
1692 	int cmode, flags;
1693 	struct file *nfp;
1694 	struct file *fp;
1695 	struct vnode *vp;
1696 	int type, indx, error;
1697 	struct flock lf;
1698 
1699 	if ((oflags & O_ACCMODE) == O_ACCMODE)
1700 		return (EINVAL);
1701 	flags = FFLAGS(oflags);
1702 	error = falloc(p, &nfp, NULL);
1703 	if (error)
1704 		return (error);
1705 	fp = nfp;
1706 	cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1707 
1708 	/*
1709 	 * XXX p_dupfd is a real mess.  It allows a device to return a
1710 	 * file descriptor to be duplicated rather then doing the open
1711 	 * itself.
1712 	 */
1713 	lp->lwp_dupfd = -1;
1714 
1715 	/*
1716 	 * Call vn_open() to do the lookup and assign the vnode to the
1717 	 * file pointer.  vn_open() does not change the ref count on fp
1718 	 * and the vnode, on success, will be inherited by the file pointer
1719 	 * and unlocked.
1720 	 */
1721 	nd->nl_flags |= NLC_LOCKVP;
1722 	error = vn_open(nd, fp, flags, cmode);
1723 	nlookup_done(nd);
1724 	if (error) {
1725 		/*
1726 		 * handle special fdopen() case.  bleh.  dupfdopen() is
1727 		 * responsible for dropping the old contents of ofiles[indx]
1728 		 * if it succeeds.
1729 		 *
1730 		 * Note that fsetfd() will add a ref to fp which represents
1731 		 * the fd_files[] assignment.  We must still drop our
1732 		 * reference.
1733 		 */
1734 		if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1735 			if (fdalloc(p, 0, &indx) == 0) {
1736 				error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1737 				if (error == 0) {
1738 					*res = indx;
1739 					fdrop(fp);	/* our ref */
1740 					return (0);
1741 				}
1742 				fsetfd(p, NULL, indx);
1743 			}
1744 		}
1745 		fdrop(fp);	/* our ref */
1746 		if (error == ERESTART)
1747 			error = EINTR;
1748 		return (error);
1749 	}
1750 
1751 	/*
1752 	 * ref the vnode for ourselves so it can't be ripped out from under
1753 	 * is.  XXX need an ND flag to request that the vnode be returned
1754 	 * anyway.
1755 	 *
1756 	 * Reserve a file descriptor but do not assign it until the open
1757 	 * succeeds.
1758 	 */
1759 	vp = (struct vnode *)fp->f_data;
1760 	vref(vp);
1761 	if ((error = fdalloc(p, 0, &indx)) != 0) {
1762 		fdrop(fp);
1763 		vrele(vp);
1764 		return (error);
1765 	}
1766 
1767 	/*
1768 	 * If no error occurs the vp will have been assigned to the file
1769 	 * pointer.
1770 	 */
1771 	lp->lwp_dupfd = 0;
1772 
1773 	if (flags & (O_EXLOCK | O_SHLOCK)) {
1774 		lf.l_whence = SEEK_SET;
1775 		lf.l_start = 0;
1776 		lf.l_len = 0;
1777 		if (flags & O_EXLOCK)
1778 			lf.l_type = F_WRLCK;
1779 		else
1780 			lf.l_type = F_RDLCK;
1781 		if (flags & FNONBLOCK)
1782 			type = 0;
1783 		else
1784 			type = F_WAIT;
1785 
1786 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1787 			/*
1788 			 * lock request failed.  Clean up the reserved
1789 			 * descriptor.
1790 			 */
1791 			vrele(vp);
1792 			fsetfd(p, NULL, indx);
1793 			fdrop(fp);
1794 			return (error);
1795 		}
1796 		fp->f_flag |= FHASLOCK;
1797 	}
1798 #if 0
1799 	/*
1800 	 * Assert that all regular file vnodes were created with a object.
1801 	 */
1802 	KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1803 		("open: regular file has no backing object after vn_open"));
1804 #endif
1805 
1806 	vrele(vp);
1807 
1808 	/*
1809 	 * release our private reference, leaving the one associated with the
1810 	 * descriptor table intact.
1811 	 */
1812 	fsetfd(p, fp, indx);
1813 	fdrop(fp);
1814 	*res = indx;
1815 	return (0);
1816 }
1817 
1818 /*
1819  * open_args(char *path, int flags, int mode)
1820  *
1821  * Check permissions, allocate an open file structure,
1822  * and call the device open routine if any.
1823  */
1824 int
1825 sys_open(struct open_args *uap)
1826 {
1827 	struct nlookupdata nd;
1828 	int error;
1829 
1830 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1831 	if (error == 0) {
1832 		error = kern_open(&nd, uap->flags,
1833 				    uap->mode, &uap->sysmsg_result);
1834 	}
1835 	nlookup_done(&nd);
1836 	return (error);
1837 }
1838 
1839 int
1840 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1841 {
1842 	struct thread *td = curthread;
1843 	struct proc *p = td->td_proc;
1844 	struct vnode *vp;
1845 	struct vattr vattr;
1846 	int error;
1847 	int whiteout = 0;
1848 
1849 	KKASSERT(p);
1850 
1851 	switch (mode & S_IFMT) {
1852 	case S_IFCHR:
1853 	case S_IFBLK:
1854 		error = priv_check(td, PRIV_ROOT);
1855 		break;
1856 	default:
1857 		error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT);
1858 		break;
1859 	}
1860 	if (error)
1861 		return (error);
1862 
1863 	bwillinode(1);
1864 	nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1865 	if ((error = nlookup(nd)) != 0)
1866 		return (error);
1867 	if (nd->nl_nch.ncp->nc_vp)
1868 		return (EEXIST);
1869 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1870 		return (error);
1871 
1872 	VATTR_NULL(&vattr);
1873 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1874 	vattr.va_rmajor = rmajor;
1875 	vattr.va_rminor = rminor;
1876 	whiteout = 0;
1877 
1878 	switch (mode & S_IFMT) {
1879 	case S_IFMT:	/* used by badsect to flag bad sectors */
1880 		vattr.va_type = VBAD;
1881 		break;
1882 	case S_IFCHR:
1883 		vattr.va_type = VCHR;
1884 		break;
1885 	case S_IFBLK:
1886 		vattr.va_type = VBLK;
1887 		break;
1888 	case S_IFWHT:
1889 		whiteout = 1;
1890 		break;
1891 	case S_IFDIR:
1892 		/* special directories support for HAMMER */
1893 		vattr.va_type = VDIR;
1894 		break;
1895 	default:
1896 		error = EINVAL;
1897 		break;
1898 	}
1899 	if (error == 0) {
1900 		if (whiteout) {
1901 			error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1902 					      nd->nl_cred, NAMEI_CREATE);
1903 		} else {
1904 			vp = NULL;
1905 			error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1906 					   &vp, nd->nl_cred, &vattr);
1907 			if (error == 0)
1908 				vput(vp);
1909 		}
1910 	}
1911 	return (error);
1912 }
1913 
1914 /*
1915  * mknod_args(char *path, int mode, int dev)
1916  *
1917  * Create a special file.
1918  */
1919 int
1920 sys_mknod(struct mknod_args *uap)
1921 {
1922 	struct nlookupdata nd;
1923 	int error;
1924 
1925 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1926 	if (error == 0) {
1927 		error = kern_mknod(&nd, uap->mode,
1928 				   umajor(uap->dev), uminor(uap->dev));
1929 	}
1930 	nlookup_done(&nd);
1931 	return (error);
1932 }
1933 
1934 int
1935 kern_mkfifo(struct nlookupdata *nd, int mode)
1936 {
1937 	struct thread *td = curthread;
1938 	struct proc *p = td->td_proc;
1939 	struct vattr vattr;
1940 	struct vnode *vp;
1941 	int error;
1942 
1943 	bwillinode(1);
1944 
1945 	nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1946 	if ((error = nlookup(nd)) != 0)
1947 		return (error);
1948 	if (nd->nl_nch.ncp->nc_vp)
1949 		return (EEXIST);
1950 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1951 		return (error);
1952 
1953 	VATTR_NULL(&vattr);
1954 	vattr.va_type = VFIFO;
1955 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1956 	vp = NULL;
1957 	error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1958 	if (error == 0)
1959 		vput(vp);
1960 	return (error);
1961 }
1962 
1963 /*
1964  * mkfifo_args(char *path, int mode)
1965  *
1966  * Create a named pipe.
1967  */
1968 int
1969 sys_mkfifo(struct mkfifo_args *uap)
1970 {
1971 	struct nlookupdata nd;
1972 	int error;
1973 
1974 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1975 	if (error == 0)
1976 		error = kern_mkfifo(&nd, uap->mode);
1977 	nlookup_done(&nd);
1978 	return (error);
1979 }
1980 
1981 static int hardlink_check_uid = 0;
1982 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1983     &hardlink_check_uid, 0,
1984     "Unprivileged processes cannot create hard links to files owned by other "
1985     "users");
1986 static int hardlink_check_gid = 0;
1987 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1988     &hardlink_check_gid, 0,
1989     "Unprivileged processes cannot create hard links to files owned by other "
1990     "groups");
1991 
1992 static int
1993 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1994 {
1995 	struct vattr va;
1996 	int error;
1997 
1998 	/*
1999 	 * Shortcut if disabled
2000 	 */
2001 	if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2002 		return (0);
2003 
2004 	/*
2005 	 * root cred can always hardlink
2006 	 */
2007 	if (priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT) == 0)
2008 		return (0);
2009 
2010 	/*
2011 	 * Otherwise only if the originating file is owned by the
2012 	 * same user or group.  Note that any group is allowed if
2013 	 * the file is owned by the caller.
2014 	 */
2015 	error = VOP_GETATTR(vp, &va);
2016 	if (error != 0)
2017 		return (error);
2018 
2019 	if (hardlink_check_uid) {
2020 		if (cred->cr_uid != va.va_uid)
2021 			return (EPERM);
2022 	}
2023 
2024 	if (hardlink_check_gid) {
2025 		if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2026 			return (EPERM);
2027 	}
2028 
2029 	return (0);
2030 }
2031 
2032 int
2033 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2034 {
2035 	struct thread *td = curthread;
2036 	struct vnode *vp;
2037 	int error;
2038 
2039 	/*
2040 	 * Lookup the source and obtained a locked vnode.
2041 	 *
2042 	 * You may only hardlink a file which you have write permission
2043 	 * on or which you own.
2044 	 *
2045 	 * XXX relookup on vget failure / race ?
2046 	 */
2047 	bwillinode(1);
2048 	nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2049 	if ((error = nlookup(nd)) != 0)
2050 		return (error);
2051 	vp = nd->nl_nch.ncp->nc_vp;
2052 	KKASSERT(vp != NULL);
2053 	if (vp->v_type == VDIR)
2054 		return (EPERM);		/* POSIX */
2055 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2056 		return (error);
2057 	if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2058 		return (error);
2059 
2060 	/*
2061 	 * Unlock the source so we can lookup the target without deadlocking
2062 	 * (XXX vp is locked already, possible other deadlock?).  The target
2063 	 * must not exist.
2064 	 */
2065 	KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2066 	nd->nl_flags &= ~NLC_NCPISLOCKED;
2067 	cache_unlock(&nd->nl_nch);
2068 
2069 	linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2070 	if ((error = nlookup(linknd)) != 0) {
2071 		vput(vp);
2072 		return (error);
2073 	}
2074 	if (linknd->nl_nch.ncp->nc_vp) {
2075 		vput(vp);
2076 		return (EEXIST);
2077 	}
2078 
2079 	/*
2080 	 * Finally run the new API VOP.
2081 	 */
2082 	error = can_hardlink(vp, td, td->td_proc->p_ucred);
2083 	if (error == 0) {
2084 		error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2085 				  vp, linknd->nl_cred);
2086 	}
2087 	vput(vp);
2088 	return (error);
2089 }
2090 
2091 /*
2092  * link_args(char *path, char *link)
2093  *
2094  * Make a hard file link.
2095  */
2096 int
2097 sys_link(struct link_args *uap)
2098 {
2099 	struct nlookupdata nd, linknd;
2100 	int error;
2101 
2102 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2103 	if (error == 0) {
2104 		error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2105 		if (error == 0)
2106 			error = kern_link(&nd, &linknd);
2107 		nlookup_done(&linknd);
2108 	}
2109 	nlookup_done(&nd);
2110 	return (error);
2111 }
2112 
2113 int
2114 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2115 {
2116 	struct vattr vattr;
2117 	struct vnode *vp;
2118 	struct vnode *dvp;
2119 	int error;
2120 
2121 	bwillinode(1);
2122 	nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2123 	if ((error = nlookup(nd)) != 0)
2124 		return (error);
2125 	if (nd->nl_nch.ncp->nc_vp)
2126 		return (EEXIST);
2127 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2128 		return (error);
2129 	dvp = nd->nl_dvp;
2130 	VATTR_NULL(&vattr);
2131 	vattr.va_mode = mode;
2132 	error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2133 	if (error == 0)
2134 		vput(vp);
2135 	return (error);
2136 }
2137 
2138 /*
2139  * symlink(char *path, char *link)
2140  *
2141  * Make a symbolic link.
2142  */
2143 int
2144 sys_symlink(struct symlink_args *uap)
2145 {
2146 	struct thread *td = curthread;
2147 	struct nlookupdata nd;
2148 	char *path;
2149 	int error;
2150 	int mode;
2151 
2152 	path = objcache_get(namei_oc, M_WAITOK);
2153 	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2154 	if (error == 0) {
2155 		error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2156 		if (error == 0) {
2157 			mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2158 			error = kern_symlink(&nd, path, mode);
2159 		}
2160 		nlookup_done(&nd);
2161 	}
2162 	objcache_put(namei_oc, path);
2163 	return (error);
2164 }
2165 
2166 /*
2167  * undelete_args(char *path)
2168  *
2169  * Delete a whiteout from the filesystem.
2170  */
2171 /* ARGSUSED */
2172 int
2173 sys_undelete(struct undelete_args *uap)
2174 {
2175 	struct nlookupdata nd;
2176 	int error;
2177 
2178 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2179 	bwillinode(1);
2180 	nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2181 	if (error == 0)
2182 		error = nlookup(&nd);
2183 	if (error == 0)
2184 		error = ncp_writechk(&nd.nl_nch);
2185 	if (error == 0) {
2186 		error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2187 				      NAMEI_DELETE);
2188 	}
2189 	nlookup_done(&nd);
2190 	return (error);
2191 }
2192 
2193 int
2194 kern_unlink(struct nlookupdata *nd)
2195 {
2196 	int error;
2197 
2198 	bwillinode(1);
2199 	nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2200 	if ((error = nlookup(nd)) != 0)
2201 		return (error);
2202 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2203 		return (error);
2204 	error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2205 	return (error);
2206 }
2207 
2208 /*
2209  * unlink_args(char *path)
2210  *
2211  * Delete a name from the filesystem.
2212  */
2213 int
2214 sys_unlink(struct unlink_args *uap)
2215 {
2216 	struct nlookupdata nd;
2217 	int error;
2218 
2219 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2220 	if (error == 0)
2221 		error = kern_unlink(&nd);
2222 	nlookup_done(&nd);
2223 	return (error);
2224 }
2225 
2226 int
2227 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2228 {
2229 	struct thread *td = curthread;
2230 	struct proc *p = td->td_proc;
2231 	struct file *fp;
2232 	struct vnode *vp;
2233 	struct vattr vattr;
2234 	off_t new_offset;
2235 	int error;
2236 
2237 	fp = holdfp(p->p_fd, fd, -1);
2238 	if (fp == NULL)
2239 		return (EBADF);
2240 	if (fp->f_type != DTYPE_VNODE) {
2241 		error = ESPIPE;
2242 		goto done;
2243 	}
2244 	vp = (struct vnode *)fp->f_data;
2245 
2246 	switch (whence) {
2247 	case L_INCR:
2248 		new_offset = fp->f_offset + offset;
2249 		error = 0;
2250 		break;
2251 	case L_XTND:
2252 		error = VOP_GETATTR(vp, &vattr);
2253 		new_offset = offset + vattr.va_size;
2254 		break;
2255 	case L_SET:
2256 		new_offset = offset;
2257 		error = 0;
2258 		break;
2259 	default:
2260 		new_offset = 0;
2261 		error = EINVAL;
2262 		break;
2263 	}
2264 
2265 	/*
2266 	 * Validate the seek position.  Negative offsets are not allowed
2267 	 * for regular files, block specials, or directories.
2268 	 */
2269 	if (error == 0) {
2270 		if (new_offset < 0 &&
2271 		    (vp->v_type == VREG || vp->v_type == VDIR ||
2272 		     vp->v_type == VCHR || vp->v_type == VBLK)) {
2273 			error = EINVAL;
2274 		} else {
2275 			fp->f_offset = new_offset;
2276 		}
2277 	}
2278 	*res = fp->f_offset;
2279 done:
2280 	fdrop(fp);
2281 	return (error);
2282 }
2283 
2284 /*
2285  * lseek_args(int fd, int pad, off_t offset, int whence)
2286  *
2287  * Reposition read/write file offset.
2288  */
2289 int
2290 sys_lseek(struct lseek_args *uap)
2291 {
2292 	int error;
2293 
2294 	error = kern_lseek(uap->fd, uap->offset, uap->whence,
2295 			   &uap->sysmsg_offset);
2296 
2297 	return (error);
2298 }
2299 
2300 int
2301 kern_access(struct nlookupdata *nd, int aflags)
2302 {
2303 	struct vnode *vp;
2304 	int error, flags;
2305 
2306 	if ((error = nlookup(nd)) != 0)
2307 		return (error);
2308 retry:
2309 	error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2310 	if (error)
2311 		return (error);
2312 
2313 	/* Flags == 0 means only check for existence. */
2314 	if (aflags) {
2315 		flags = 0;
2316 		if (aflags & R_OK)
2317 			flags |= VREAD;
2318 		if (aflags & W_OK)
2319 			flags |= VWRITE;
2320 		if (aflags & X_OK)
2321 			flags |= VEXEC;
2322 		if ((flags & VWRITE) == 0 ||
2323 		    (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2324 			error = VOP_ACCESS(vp, flags, nd->nl_cred);
2325 
2326 		/*
2327 		 * If the file handle is stale we have to re-resolve the
2328 		 * entry.  This is a hack at the moment.
2329 		 */
2330 		if (error == ESTALE) {
2331 			vput(vp);
2332 			cache_setunresolved(&nd->nl_nch);
2333 			error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2334 			if (error == 0) {
2335 				vp = NULL;
2336 				goto retry;
2337 			}
2338 			return(error);
2339 		}
2340 	}
2341 	vput(vp);
2342 	return (error);
2343 }
2344 
2345 /*
2346  * access_args(char *path, int flags)
2347  *
2348  * Check access permissions.
2349  */
2350 int
2351 sys_access(struct access_args *uap)
2352 {
2353 	struct nlookupdata nd;
2354 	int error;
2355 
2356 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2357 	if (error == 0)
2358 		error = kern_access(&nd, uap->flags);
2359 	nlookup_done(&nd);
2360 	return (error);
2361 }
2362 
2363 int
2364 kern_stat(struct nlookupdata *nd, struct stat *st)
2365 {
2366 	int error;
2367 	struct vnode *vp;
2368 	thread_t td;
2369 
2370 	if ((error = nlookup(nd)) != 0)
2371 		return (error);
2372 again:
2373 	if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2374 		return (ENOENT);
2375 
2376 	td = curthread;
2377 	if ((error = vget(vp, LK_SHARED)) != 0)
2378 		return (error);
2379 	error = vn_stat(vp, st, nd->nl_cred);
2380 
2381 	/*
2382 	 * If the file handle is stale we have to re-resolve the entry.  This
2383 	 * is a hack at the moment.
2384 	 */
2385 	if (error == ESTALE) {
2386 		vput(vp);
2387 		cache_setunresolved(&nd->nl_nch);
2388 		error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2389 		if (error == 0)
2390 			goto again;
2391 	} else {
2392 		vput(vp);
2393 	}
2394 	return (error);
2395 }
2396 
2397 /*
2398  * stat_args(char *path, struct stat *ub)
2399  *
2400  * Get file status; this version follows links.
2401  */
2402 int
2403 sys_stat(struct stat_args *uap)
2404 {
2405 	struct nlookupdata nd;
2406 	struct stat st;
2407 	int error;
2408 
2409 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2410 	if (error == 0) {
2411 		error = kern_stat(&nd, &st);
2412 		if (error == 0)
2413 			error = copyout(&st, uap->ub, sizeof(*uap->ub));
2414 	}
2415 	nlookup_done(&nd);
2416 	return (error);
2417 }
2418 
2419 /*
2420  * lstat_args(char *path, struct stat *ub)
2421  *
2422  * Get file status; this version does not follow links.
2423  */
2424 int
2425 sys_lstat(struct lstat_args *uap)
2426 {
2427 	struct nlookupdata nd;
2428 	struct stat st;
2429 	int error;
2430 
2431 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2432 	if (error == 0) {
2433 		error = kern_stat(&nd, &st);
2434 		if (error == 0)
2435 			error = copyout(&st, uap->ub, sizeof(*uap->ub));
2436 	}
2437 	nlookup_done(&nd);
2438 	return (error);
2439 }
2440 
2441 /*
2442  * pathconf_Args(char *path, int name)
2443  *
2444  * Get configurable pathname variables.
2445  */
2446 /* ARGSUSED */
2447 int
2448 sys_pathconf(struct pathconf_args *uap)
2449 {
2450 	struct nlookupdata nd;
2451 	struct vnode *vp;
2452 	int error;
2453 
2454 	vp = NULL;
2455 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2456 	if (error == 0)
2457 		error = nlookup(&nd);
2458 	if (error == 0)
2459 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2460 	nlookup_done(&nd);
2461 	if (error == 0) {
2462 		error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2463 		vput(vp);
2464 	}
2465 	return (error);
2466 }
2467 
2468 /*
2469  * XXX: daver
2470  * kern_readlink isn't properly split yet.  There is a copyin burried
2471  * in VOP_READLINK().
2472  */
2473 int
2474 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2475 {
2476 	struct thread *td = curthread;
2477 	struct proc *p = td->td_proc;
2478 	struct vnode *vp;
2479 	struct iovec aiov;
2480 	struct uio auio;
2481 	int error;
2482 
2483 	if ((error = nlookup(nd)) != 0)
2484 		return (error);
2485 	error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2486 	if (error)
2487 		return (error);
2488 	if (vp->v_type != VLNK) {
2489 		error = EINVAL;
2490 	} else {
2491 		aiov.iov_base = buf;
2492 		aiov.iov_len = count;
2493 		auio.uio_iov = &aiov;
2494 		auio.uio_iovcnt = 1;
2495 		auio.uio_offset = 0;
2496 		auio.uio_rw = UIO_READ;
2497 		auio.uio_segflg = UIO_USERSPACE;
2498 		auio.uio_td = td;
2499 		auio.uio_resid = count;
2500 		error = VOP_READLINK(vp, &auio, p->p_ucred);
2501 	}
2502 	vput(vp);
2503 	*res = count - auio.uio_resid;
2504 	return (error);
2505 }
2506 
2507 /*
2508  * readlink_args(char *path, char *buf, int count)
2509  *
2510  * Return target name of a symbolic link.
2511  */
2512 int
2513 sys_readlink(struct readlink_args *uap)
2514 {
2515 	struct nlookupdata nd;
2516 	int error;
2517 
2518 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2519 	if (error == 0) {
2520 		error = kern_readlink(&nd, uap->buf, uap->count,
2521 					&uap->sysmsg_result);
2522 	}
2523 	nlookup_done(&nd);
2524 	return (error);
2525 }
2526 
2527 static int
2528 setfflags(struct vnode *vp, int flags)
2529 {
2530 	struct thread *td = curthread;
2531 	struct proc *p = td->td_proc;
2532 	int error;
2533 	struct vattr vattr;
2534 
2535 	/*
2536 	 * Prevent non-root users from setting flags on devices.  When
2537 	 * a device is reused, users can retain ownership of the device
2538 	 * if they are allowed to set flags and programs assume that
2539 	 * chown can't fail when done as root.
2540 	 */
2541 	if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2542 	    ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0))
2543 		return (error);
2544 
2545 	/*
2546 	 * note: vget is required for any operation that might mod the vnode
2547 	 * so VINACTIVE is properly cleared.
2548 	 */
2549 	if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2550 		VATTR_NULL(&vattr);
2551 		vattr.va_flags = flags;
2552 		error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2553 		vput(vp);
2554 	}
2555 	return (error);
2556 }
2557 
2558 /*
2559  * chflags(char *path, int flags)
2560  *
2561  * Change flags of a file given a path name.
2562  */
2563 /* ARGSUSED */
2564 int
2565 sys_chflags(struct chflags_args *uap)
2566 {
2567 	struct nlookupdata nd;
2568 	struct vnode *vp;
2569 	int error;
2570 
2571 	vp = NULL;
2572 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2573 	if (error == 0)
2574 		error = nlookup(&nd);
2575 	if (error == 0)
2576 		error = ncp_writechk(&nd.nl_nch);
2577 	if (error == 0)
2578 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2579 	nlookup_done(&nd);
2580 	if (error == 0) {
2581 		error = setfflags(vp, uap->flags);
2582 		vrele(vp);
2583 	}
2584 	return (error);
2585 }
2586 
2587 /*
2588  * lchflags(char *path, int flags)
2589  *
2590  * Change flags of a file given a path name, but don't follow symlinks.
2591  */
2592 /* ARGSUSED */
2593 int
2594 sys_lchflags(struct lchflags_args *uap)
2595 {
2596 	struct nlookupdata nd;
2597 	struct vnode *vp;
2598 	int error;
2599 
2600 	vp = NULL;
2601 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2602 	if (error == 0)
2603 		error = nlookup(&nd);
2604 	if (error == 0)
2605 		error = ncp_writechk(&nd.nl_nch);
2606 	if (error == 0)
2607 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2608 	nlookup_done(&nd);
2609 	if (error == 0) {
2610 		error = setfflags(vp, uap->flags);
2611 		vrele(vp);
2612 	}
2613 	return (error);
2614 }
2615 
2616 /*
2617  * fchflags_args(int fd, int flags)
2618  *
2619  * Change flags of a file given a file descriptor.
2620  */
2621 /* ARGSUSED */
2622 int
2623 sys_fchflags(struct fchflags_args *uap)
2624 {
2625 	struct thread *td = curthread;
2626 	struct proc *p = td->td_proc;
2627 	struct file *fp;
2628 	int error;
2629 
2630 	if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2631 		return (error);
2632 	if (fp->f_nchandle.ncp)
2633 		error = ncp_writechk(&fp->f_nchandle);
2634 	if (error == 0)
2635 		error = setfflags((struct vnode *) fp->f_data, uap->flags);
2636 	fdrop(fp);
2637 	return (error);
2638 }
2639 
2640 static int
2641 setfmode(struct vnode *vp, int mode)
2642 {
2643 	struct thread *td = curthread;
2644 	struct proc *p = td->td_proc;
2645 	int error;
2646 	struct vattr vattr;
2647 
2648 	/*
2649 	 * note: vget is required for any operation that might mod the vnode
2650 	 * so VINACTIVE is properly cleared.
2651 	 */
2652 	if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2653 		VATTR_NULL(&vattr);
2654 		vattr.va_mode = mode & ALLPERMS;
2655 		error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2656 		vput(vp);
2657 	}
2658 	return error;
2659 }
2660 
2661 int
2662 kern_chmod(struct nlookupdata *nd, int mode)
2663 {
2664 	struct vnode *vp;
2665 	int error;
2666 
2667 	if ((error = nlookup(nd)) != 0)
2668 		return (error);
2669 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2670 		return (error);
2671 	if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2672 		error = setfmode(vp, mode);
2673 	vrele(vp);
2674 	return (error);
2675 }
2676 
2677 /*
2678  * chmod_args(char *path, int mode)
2679  *
2680  * Change mode of a file given path name.
2681  */
2682 /* ARGSUSED */
2683 int
2684 sys_chmod(struct chmod_args *uap)
2685 {
2686 	struct nlookupdata nd;
2687 	int error;
2688 
2689 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2690 	if (error == 0)
2691 		error = kern_chmod(&nd, uap->mode);
2692 	nlookup_done(&nd);
2693 	return (error);
2694 }
2695 
2696 /*
2697  * lchmod_args(char *path, int mode)
2698  *
2699  * Change mode of a file given path name (don't follow links.)
2700  */
2701 /* ARGSUSED */
2702 int
2703 sys_lchmod(struct lchmod_args *uap)
2704 {
2705 	struct nlookupdata nd;
2706 	int error;
2707 
2708 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2709 	if (error == 0)
2710 		error = kern_chmod(&nd, uap->mode);
2711 	nlookup_done(&nd);
2712 	return (error);
2713 }
2714 
2715 /*
2716  * fchmod_args(int fd, int mode)
2717  *
2718  * Change mode of a file given a file descriptor.
2719  */
2720 /* ARGSUSED */
2721 int
2722 sys_fchmod(struct fchmod_args *uap)
2723 {
2724 	struct thread *td = curthread;
2725 	struct proc *p = td->td_proc;
2726 	struct file *fp;
2727 	int error;
2728 
2729 	if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2730 		return (error);
2731 	if (fp->f_nchandle.ncp)
2732 		error = ncp_writechk(&fp->f_nchandle);
2733 	if (error == 0)
2734 		error = setfmode((struct vnode *)fp->f_data, uap->mode);
2735 	fdrop(fp);
2736 	return (error);
2737 }
2738 
2739 static int
2740 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2741 {
2742 	struct thread *td = curthread;
2743 	struct proc *p = td->td_proc;
2744 	int error;
2745 	struct vattr vattr;
2746 
2747 	/*
2748 	 * note: vget is required for any operation that might mod the vnode
2749 	 * so VINACTIVE is properly cleared.
2750 	 */
2751 	if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2752 		VATTR_NULL(&vattr);
2753 		vattr.va_uid = uid;
2754 		vattr.va_gid = gid;
2755 		error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2756 		vput(vp);
2757 	}
2758 	return error;
2759 }
2760 
2761 int
2762 kern_chown(struct nlookupdata *nd, int uid, int gid)
2763 {
2764 	struct vnode *vp;
2765 	int error;
2766 
2767 	if ((error = nlookup(nd)) != 0)
2768 		return (error);
2769 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2770 		return (error);
2771 	if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2772 		error = setfown(vp, uid, gid);
2773 	vrele(vp);
2774 	return (error);
2775 }
2776 
2777 /*
2778  * chown(char *path, int uid, int gid)
2779  *
2780  * Set ownership given a path name.
2781  */
2782 int
2783 sys_chown(struct chown_args *uap)
2784 {
2785 	struct nlookupdata nd;
2786 	int error;
2787 
2788 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2789 	if (error == 0)
2790 		error = kern_chown(&nd, uap->uid, uap->gid);
2791 	nlookup_done(&nd);
2792 	return (error);
2793 }
2794 
2795 /*
2796  * lchown_args(char *path, int uid, int gid)
2797  *
2798  * Set ownership given a path name, do not cross symlinks.
2799  */
2800 int
2801 sys_lchown(struct lchown_args *uap)
2802 {
2803 	struct nlookupdata nd;
2804 	int error;
2805 
2806 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2807 	if (error == 0)
2808 		error = kern_chown(&nd, uap->uid, uap->gid);
2809 	nlookup_done(&nd);
2810 	return (error);
2811 }
2812 
2813 /*
2814  * fchown_args(int fd, int uid, int gid)
2815  *
2816  * Set ownership given a file descriptor.
2817  */
2818 /* ARGSUSED */
2819 int
2820 sys_fchown(struct fchown_args *uap)
2821 {
2822 	struct thread *td = curthread;
2823 	struct proc *p = td->td_proc;
2824 	struct file *fp;
2825 	int error;
2826 
2827 	if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2828 		return (error);
2829 	if (fp->f_nchandle.ncp)
2830 		error = ncp_writechk(&fp->f_nchandle);
2831 	if (error == 0)
2832 		error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2833 	fdrop(fp);
2834 	return (error);
2835 }
2836 
2837 static int
2838 getutimes(const struct timeval *tvp, struct timespec *tsp)
2839 {
2840 	struct timeval tv[2];
2841 
2842 	if (tvp == NULL) {
2843 		microtime(&tv[0]);
2844 		TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2845 		tsp[1] = tsp[0];
2846 	} else {
2847 		TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2848 		TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2849 	}
2850 	return 0;
2851 }
2852 
2853 static int
2854 setutimes(struct vnode *vp, struct vattr *vattr,
2855 	  const struct timespec *ts, int nullflag)
2856 {
2857 	struct thread *td = curthread;
2858 	struct proc *p = td->td_proc;
2859 	int error;
2860 
2861 	VATTR_NULL(vattr);
2862 	vattr->va_atime = ts[0];
2863 	vattr->va_mtime = ts[1];
2864 	if (nullflag)
2865 		vattr->va_vaflags |= VA_UTIMES_NULL;
2866 	error = VOP_SETATTR(vp, vattr, p->p_ucred);
2867 
2868 	return error;
2869 }
2870 
2871 int
2872 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2873 {
2874 	struct timespec ts[2];
2875 	struct vnode *vp;
2876 	struct vattr vattr;
2877 	int error;
2878 
2879 	if ((error = getutimes(tptr, ts)) != 0)
2880 		return (error);
2881 
2882 	/*
2883 	 * NOTE: utimes() succeeds for the owner even if the file
2884 	 * is not user-writable.
2885 	 */
2886 	nd->nl_flags |= NLC_OWN | NLC_WRITE;
2887 
2888 	if ((error = nlookup(nd)) != 0)
2889 		return (error);
2890 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2891 		return (error);
2892 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2893 		return (error);
2894 
2895 	/*
2896 	 * note: vget is required for any operation that might mod the vnode
2897 	 * so VINACTIVE is properly cleared.
2898 	 */
2899 	if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
2900 		error = vget(vp, LK_EXCLUSIVE);
2901 		if (error == 0) {
2902 			error = setutimes(vp, &vattr, ts, (tptr == NULL));
2903 			vput(vp);
2904 		}
2905 	}
2906 	vrele(vp);
2907 	return (error);
2908 }
2909 
2910 /*
2911  * utimes_args(char *path, struct timeval *tptr)
2912  *
2913  * Set the access and modification times of a file.
2914  */
2915 int
2916 sys_utimes(struct utimes_args *uap)
2917 {
2918 	struct timeval tv[2];
2919 	struct nlookupdata nd;
2920 	int error;
2921 
2922 	if (uap->tptr) {
2923  		error = copyin(uap->tptr, tv, sizeof(tv));
2924 		if (error)
2925 			return (error);
2926 	}
2927 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2928 	if (error == 0)
2929 		error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2930 	nlookup_done(&nd);
2931 	return (error);
2932 }
2933 
2934 /*
2935  * lutimes_args(char *path, struct timeval *tptr)
2936  *
2937  * Set the access and modification times of a file.
2938  */
2939 int
2940 sys_lutimes(struct lutimes_args *uap)
2941 {
2942 	struct timeval tv[2];
2943 	struct nlookupdata nd;
2944 	int error;
2945 
2946 	if (uap->tptr) {
2947 		error = copyin(uap->tptr, tv, sizeof(tv));
2948 		if (error)
2949 			return (error);
2950 	}
2951 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2952 	if (error == 0)
2953 		error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2954 	nlookup_done(&nd);
2955 	return (error);
2956 }
2957 
2958 /*
2959  * Set utimes on a file descriptor.  The creds used to open the
2960  * file are used to determine whether the operation is allowed
2961  * or not.
2962  */
2963 int
2964 kern_futimes(int fd, struct timeval *tptr)
2965 {
2966 	struct thread *td = curthread;
2967 	struct proc *p = td->td_proc;
2968 	struct timespec ts[2];
2969 	struct file *fp;
2970 	struct vnode *vp;
2971 	struct vattr vattr;
2972 	int error;
2973 
2974 	error = getutimes(tptr, ts);
2975 	if (error)
2976 		return (error);
2977 	if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2978 		return (error);
2979 	if (fp->f_nchandle.ncp)
2980 		error = ncp_writechk(&fp->f_nchandle);
2981 	if (error == 0) {
2982 		vp = fp->f_data;
2983 		error = vget(vp, LK_EXCLUSIVE);
2984 		if (error == 0) {
2985 			error = VOP_GETATTR(vp, &vattr);
2986 			if (error == 0) {
2987 				error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
2988 						   fp->f_cred);
2989 			}
2990 			if (error == 0) {
2991 				error = setutimes(vp, &vattr, ts,
2992 						  (tptr == NULL));
2993 			}
2994 			vput(vp);
2995 		}
2996 	}
2997 	fdrop(fp);
2998 	return (error);
2999 }
3000 
3001 /*
3002  * futimes_args(int fd, struct timeval *tptr)
3003  *
3004  * Set the access and modification times of a file.
3005  */
3006 int
3007 sys_futimes(struct futimes_args *uap)
3008 {
3009 	struct timeval tv[2];
3010 	int error;
3011 
3012 	if (uap->tptr) {
3013 		error = copyin(uap->tptr, tv, sizeof(tv));
3014 		if (error)
3015 			return (error);
3016 	}
3017 
3018 	error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3019 
3020 	return (error);
3021 }
3022 
3023 int
3024 kern_truncate(struct nlookupdata *nd, off_t length)
3025 {
3026 	struct vnode *vp;
3027 	struct vattr vattr;
3028 	int error;
3029 
3030 	if (length < 0)
3031 		return(EINVAL);
3032 	nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3033 	if ((error = nlookup(nd)) != 0)
3034 		return (error);
3035 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3036 		return (error);
3037 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3038 		return (error);
3039 	if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3040 		vrele(vp);
3041 		return (error);
3042 	}
3043 	if (vp->v_type == VDIR) {
3044 		error = EISDIR;
3045 	} else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3046 		VATTR_NULL(&vattr);
3047 		vattr.va_size = length;
3048 		error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3049 	}
3050 	vput(vp);
3051 	return (error);
3052 }
3053 
3054 /*
3055  * truncate(char *path, int pad, off_t length)
3056  *
3057  * Truncate a file given its path name.
3058  */
3059 int
3060 sys_truncate(struct truncate_args *uap)
3061 {
3062 	struct nlookupdata nd;
3063 	int error;
3064 
3065 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3066 	if (error == 0)
3067 		error = kern_truncate(&nd, uap->length);
3068 	nlookup_done(&nd);
3069 	return error;
3070 }
3071 
3072 int
3073 kern_ftruncate(int fd, off_t length)
3074 {
3075 	struct thread *td = curthread;
3076 	struct proc *p = td->td_proc;
3077 	struct vattr vattr;
3078 	struct vnode *vp;
3079 	struct file *fp;
3080 	int error;
3081 
3082 	if (length < 0)
3083 		return(EINVAL);
3084 	if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3085 		return (error);
3086 	if (fp->f_nchandle.ncp) {
3087 		error = ncp_writechk(&fp->f_nchandle);
3088 		if (error)
3089 			goto done;
3090 	}
3091 	if ((fp->f_flag & FWRITE) == 0) {
3092 		error = EINVAL;
3093 		goto done;
3094 	}
3095 	if (fp->f_flag & FAPPENDONLY) {	/* inode was set s/uapnd */
3096 		error = EINVAL;
3097 		goto done;
3098 	}
3099 	vp = (struct vnode *)fp->f_data;
3100 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3101 	if (vp->v_type == VDIR) {
3102 		error = EISDIR;
3103 	} else if ((error = vn_writechk(vp, NULL)) == 0) {
3104 		VATTR_NULL(&vattr);
3105 		vattr.va_size = length;
3106 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3107 	}
3108 	vn_unlock(vp);
3109 done:
3110 	fdrop(fp);
3111 	return (error);
3112 }
3113 
3114 /*
3115  * ftruncate_args(int fd, int pad, off_t length)
3116  *
3117  * Truncate a file given a file descriptor.
3118  */
3119 int
3120 sys_ftruncate(struct ftruncate_args *uap)
3121 {
3122 	int error;
3123 
3124 	error = kern_ftruncate(uap->fd, uap->length);
3125 
3126 	return (error);
3127 }
3128 
3129 /*
3130  * fsync(int fd)
3131  *
3132  * Sync an open file.
3133  */
3134 /* ARGSUSED */
3135 int
3136 sys_fsync(struct fsync_args *uap)
3137 {
3138 	struct thread *td = curthread;
3139 	struct proc *p = td->td_proc;
3140 	struct vnode *vp;
3141 	struct file *fp;
3142 	vm_object_t obj;
3143 	int error;
3144 
3145 	if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3146 		return (error);
3147 	vp = (struct vnode *)fp->f_data;
3148 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3149 	if ((obj = vp->v_object) != NULL)
3150 		vm_object_page_clean(obj, 0, 0, 0);
3151 	if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3152 		error = buf_fsync(vp);
3153 	vn_unlock(vp);
3154 	fdrop(fp);
3155 	return (error);
3156 }
3157 
3158 int
3159 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3160 {
3161 	struct nchandle fnchd;
3162 	struct nchandle tnchd;
3163 	struct namecache *ncp;
3164 	struct vnode *fdvp;
3165 	struct vnode *tdvp;
3166 	struct mount *mp;
3167 	int error;
3168 
3169 	bwillinode(1);
3170 	fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3171 	if ((error = nlookup(fromnd)) != 0)
3172 		return (error);
3173 	if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3174 		return (ENOENT);
3175 	fnchd.mount = fromnd->nl_nch.mount;
3176 	cache_hold(&fnchd);
3177 
3178 	/*
3179 	 * unlock the source nch so we can lookup the target nch without
3180 	 * deadlocking.  The target may or may not exist so we do not check
3181 	 * for a target vp like kern_mkdir() and other creation functions do.
3182 	 *
3183 	 * The source and target directories are ref'd and rechecked after
3184 	 * everything is relocked to determine if the source or target file
3185 	 * has been renamed.
3186 	 */
3187 	KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3188 	fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3189 	cache_unlock(&fromnd->nl_nch);
3190 
3191 	tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3192 	if ((error = nlookup(tond)) != 0) {
3193 		cache_drop(&fnchd);
3194 		return (error);
3195 	}
3196 	if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3197 		cache_drop(&fnchd);
3198 		return (ENOENT);
3199 	}
3200 	tnchd.mount = tond->nl_nch.mount;
3201 	cache_hold(&tnchd);
3202 
3203 	/*
3204 	 * If the source and target are the same there is nothing to do
3205 	 */
3206 	if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3207 		cache_drop(&fnchd);
3208 		cache_drop(&tnchd);
3209 		return (0);
3210 	}
3211 
3212 	/*
3213 	 * Mount points cannot be renamed or overwritten
3214 	 */
3215 	if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3216 	    NCF_ISMOUNTPT
3217 	) {
3218 		cache_drop(&fnchd);
3219 		cache_drop(&tnchd);
3220 		return (EINVAL);
3221 	}
3222 
3223 	/*
3224 	 * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
3225 	 * may have become invalid while it was unlocked, nc_vp and nc_mount
3226 	 * could be NULL.
3227 	 */
3228 	if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3229 		cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3230 	} else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3231 		cache_lock(&fromnd->nl_nch);
3232 		cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3233 	} else {
3234 		cache_unlock(&tond->nl_nch);
3235 		cache_lock(&fromnd->nl_nch);
3236 		cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3237 		cache_lock(&tond->nl_nch);
3238 		cache_resolve(&tond->nl_nch, tond->nl_cred);
3239 	}
3240 	fromnd->nl_flags |= NLC_NCPISLOCKED;
3241 
3242 	/*
3243 	 * make sure the parent directories linkages are the same
3244 	 */
3245 	if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3246 	    tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3247 		cache_drop(&fnchd);
3248 		cache_drop(&tnchd);
3249 		return (ENOENT);
3250 	}
3251 
3252 	/*
3253 	 * Both the source and target must be within the same filesystem and
3254 	 * in the same filesystem as their parent directories within the
3255 	 * namecache topology.
3256 	 *
3257 	 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3258 	 */
3259 	mp = fnchd.mount;
3260 	if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3261 	    mp != tond->nl_nch.mount) {
3262 		cache_drop(&fnchd);
3263 		cache_drop(&tnchd);
3264 		return (EXDEV);
3265 	}
3266 
3267 	/*
3268 	 * Make sure the mount point is writable
3269 	 */
3270 	if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3271 		cache_drop(&fnchd);
3272 		cache_drop(&tnchd);
3273 		return (error);
3274 	}
3275 
3276 	/*
3277 	 * If the target exists and either the source or target is a directory,
3278 	 * then both must be directories.
3279 	 *
3280 	 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3281 	 * have become NULL.
3282 	 */
3283 	if (tond->nl_nch.ncp->nc_vp) {
3284 		if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3285 			error = ENOENT;
3286 		} else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3287 			if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3288 				error = ENOTDIR;
3289 		} else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3290 			error = EISDIR;
3291 		}
3292 	}
3293 
3294 	/*
3295 	 * You cannot rename a source into itself or a subdirectory of itself.
3296 	 * We check this by travsersing the target directory upwards looking
3297 	 * for a match against the source.
3298 	 */
3299 	if (error == 0) {
3300 		for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3301 			if (fromnd->nl_nch.ncp == ncp) {
3302 				error = EINVAL;
3303 				break;
3304 			}
3305 		}
3306 	}
3307 
3308 	cache_drop(&fnchd);
3309 	cache_drop(&tnchd);
3310 
3311 	/*
3312 	 * Even though the namespaces are different, they may still represent
3313 	 * hardlinks to the same file.  The filesystem might have a hard time
3314 	 * with this so we issue a NREMOVE of the source instead of a NRENAME
3315 	 * when we detect the situation.
3316 	 */
3317 	if (error == 0) {
3318 		fdvp = fromnd->nl_dvp;
3319 		tdvp = tond->nl_dvp;
3320 		if (fdvp == NULL || tdvp == NULL) {
3321 			error = EPERM;
3322 		} else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3323 			error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3324 					    fromnd->nl_cred);
3325 		} else {
3326 			error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3327 					    fdvp, tdvp, tond->nl_cred);
3328 		}
3329 	}
3330 	return (error);
3331 }
3332 
3333 /*
3334  * rename_args(char *from, char *to)
3335  *
3336  * Rename files.  Source and destination must either both be directories,
3337  * or both not be directories.  If target is a directory, it must be empty.
3338  */
3339 int
3340 sys_rename(struct rename_args *uap)
3341 {
3342 	struct nlookupdata fromnd, tond;
3343 	int error;
3344 
3345 	error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3346 	if (error == 0) {
3347 		error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3348 		if (error == 0)
3349 			error = kern_rename(&fromnd, &tond);
3350 		nlookup_done(&tond);
3351 	}
3352 	nlookup_done(&fromnd);
3353 	return (error);
3354 }
3355 
3356 int
3357 kern_mkdir(struct nlookupdata *nd, int mode)
3358 {
3359 	struct thread *td = curthread;
3360 	struct proc *p = td->td_proc;
3361 	struct vnode *vp;
3362 	struct vattr vattr;
3363 	int error;
3364 
3365 	bwillinode(1);
3366 	nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3367 	if ((error = nlookup(nd)) != 0)
3368 		return (error);
3369 
3370 	if (nd->nl_nch.ncp->nc_vp)
3371 		return (EEXIST);
3372 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3373 		return (error);
3374 	VATTR_NULL(&vattr);
3375 	vattr.va_type = VDIR;
3376 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3377 
3378 	vp = NULL;
3379 	error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3380 	if (error == 0)
3381 		vput(vp);
3382 	return (error);
3383 }
3384 
3385 /*
3386  * mkdir_args(char *path, int mode)
3387  *
3388  * Make a directory file.
3389  */
3390 /* ARGSUSED */
3391 int
3392 sys_mkdir(struct mkdir_args *uap)
3393 {
3394 	struct nlookupdata nd;
3395 	int error;
3396 
3397 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3398 	if (error == 0)
3399 		error = kern_mkdir(&nd, uap->mode);
3400 	nlookup_done(&nd);
3401 	return (error);
3402 }
3403 
3404 int
3405 kern_rmdir(struct nlookupdata *nd)
3406 {
3407 	int error;
3408 
3409 	bwillinode(1);
3410 	nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3411 	if ((error = nlookup(nd)) != 0)
3412 		return (error);
3413 
3414 	/*
3415 	 * Do not allow directories representing mount points to be
3416 	 * deleted, even if empty.  Check write perms on mount point
3417 	 * in case the vnode is aliased (aka nullfs).
3418 	 */
3419 	if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3420 		return (EINVAL);
3421 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3422 		return (error);
3423 	error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3424 	return (error);
3425 }
3426 
3427 /*
3428  * rmdir_args(char *path)
3429  *
3430  * Remove a directory file.
3431  */
3432 /* ARGSUSED */
3433 int
3434 sys_rmdir(struct rmdir_args *uap)
3435 {
3436 	struct nlookupdata nd;
3437 	int error;
3438 
3439 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3440 	if (error == 0)
3441 		error = kern_rmdir(&nd);
3442 	nlookup_done(&nd);
3443 	return (error);
3444 }
3445 
3446 int
3447 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3448     enum uio_seg direction)
3449 {
3450 	struct thread *td = curthread;
3451 	struct proc *p = td->td_proc;
3452 	struct vnode *vp;
3453 	struct file *fp;
3454 	struct uio auio;
3455 	struct iovec aiov;
3456 	off_t loff;
3457 	int error, eofflag;
3458 
3459 	if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3460 		return (error);
3461 	if ((fp->f_flag & FREAD) == 0) {
3462 		error = EBADF;
3463 		goto done;
3464 	}
3465 	vp = (struct vnode *)fp->f_data;
3466 unionread:
3467 	if (vp->v_type != VDIR) {
3468 		error = EINVAL;
3469 		goto done;
3470 	}
3471 	aiov.iov_base = buf;
3472 	aiov.iov_len = count;
3473 	auio.uio_iov = &aiov;
3474 	auio.uio_iovcnt = 1;
3475 	auio.uio_rw = UIO_READ;
3476 	auio.uio_segflg = direction;
3477 	auio.uio_td = td;
3478 	auio.uio_resid = count;
3479 	loff = auio.uio_offset = fp->f_offset;
3480 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3481 	fp->f_offset = auio.uio_offset;
3482 	if (error)
3483 		goto done;
3484 	if (count == auio.uio_resid) {
3485 		if (union_dircheckp) {
3486 			error = union_dircheckp(td, &vp, fp);
3487 			if (error == -1)
3488 				goto unionread;
3489 			if (error)
3490 				goto done;
3491 		}
3492 #if 0
3493 		if ((vp->v_flag & VROOT) &&
3494 		    (vp->v_mount->mnt_flag & MNT_UNION)) {
3495 			struct vnode *tvp = vp;
3496 			vp = vp->v_mount->mnt_vnodecovered;
3497 			vref(vp);
3498 			fp->f_data = vp;
3499 			fp->f_offset = 0;
3500 			vrele(tvp);
3501 			goto unionread;
3502 		}
3503 #endif
3504 	}
3505 
3506 	/*
3507 	 * WARNING!  *basep may not be wide enough to accomodate the
3508 	 * seek offset.   XXX should we hack this to return the upper 32 bits
3509 	 * for offsets greater then 4G?
3510 	 */
3511 	if (basep) {
3512 		*basep = (long)loff;
3513 	}
3514 	*res = count - auio.uio_resid;
3515 done:
3516 	fdrop(fp);
3517 	return (error);
3518 }
3519 
3520 /*
3521  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3522  *
3523  * Read a block of directory entries in a file system independent format.
3524  */
3525 int
3526 sys_getdirentries(struct getdirentries_args *uap)
3527 {
3528 	long base;
3529 	int error;
3530 
3531 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3532 				   &uap->sysmsg_result, UIO_USERSPACE);
3533 
3534 	if (error == 0 && uap->basep)
3535 		error = copyout(&base, uap->basep, sizeof(*uap->basep));
3536 	return (error);
3537 }
3538 
3539 /*
3540  * getdents_args(int fd, char *buf, size_t count)
3541  */
3542 int
3543 sys_getdents(struct getdents_args *uap)
3544 {
3545 	int error;
3546 
3547 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3548 	    &uap->sysmsg_result, UIO_USERSPACE);
3549 
3550 	return (error);
3551 }
3552 
3553 /*
3554  * umask(int newmask)
3555  *
3556  * Set the mode mask for creation of filesystem nodes.
3557  *
3558  * MP SAFE
3559  */
3560 int
3561 sys_umask(struct umask_args *uap)
3562 {
3563 	struct thread *td = curthread;
3564 	struct proc *p = td->td_proc;
3565 	struct filedesc *fdp;
3566 
3567 	fdp = p->p_fd;
3568 	uap->sysmsg_result = fdp->fd_cmask;
3569 	fdp->fd_cmask = uap->newmask & ALLPERMS;
3570 	return (0);
3571 }
3572 
3573 /*
3574  * revoke(char *path)
3575  *
3576  * Void all references to file by ripping underlying filesystem
3577  * away from vnode.
3578  */
3579 /* ARGSUSED */
3580 int
3581 sys_revoke(struct revoke_args *uap)
3582 {
3583 	struct nlookupdata nd;
3584 	struct vattr vattr;
3585 	struct vnode *vp;
3586 	struct ucred *cred;
3587 	int error;
3588 
3589 	vp = NULL;
3590 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3591 	if (error == 0)
3592 		error = nlookup(&nd);
3593 	if (error == 0)
3594 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3595 	cred = crhold(nd.nl_cred);
3596 	nlookup_done(&nd);
3597 	if (error == 0) {
3598 		if (error == 0)
3599 			error = VOP_GETATTR(vp, &vattr);
3600 		if (error == 0 && cred->cr_uid != vattr.va_uid)
3601 			error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
3602 		if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3603 			if (count_udev(vp->v_umajor, vp->v_uminor) > 0)
3604 				error = vrevoke(vp, cred);
3605 		} else if (error == 0) {
3606 			error = vrevoke(vp, cred);
3607 		}
3608 		vrele(vp);
3609 	}
3610 	if (cred)
3611 		crfree(cred);
3612 	return (error);
3613 }
3614 
3615 /*
3616  * getfh_args(char *fname, fhandle_t *fhp)
3617  *
3618  * Get (NFS) file handle
3619  *
3620  * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3621  * mount.  This allows nullfs mounts to be explicitly exported.
3622  *
3623  * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3624  *
3625  * 	    nullfs mounts of subdirectories are not safe.  That is, it will
3626  *	    work, but you do not really have protection against access to
3627  *	    the related parent directories.
3628  */
3629 int
3630 sys_getfh(struct getfh_args *uap)
3631 {
3632 	struct thread *td = curthread;
3633 	struct nlookupdata nd;
3634 	fhandle_t fh;
3635 	struct vnode *vp;
3636 	struct mount *mp;
3637 	int error;
3638 
3639 	/*
3640 	 * Must be super user
3641 	 */
3642 	if ((error = priv_check(td, PRIV_ROOT)) != 0)
3643 		return (error);
3644 
3645 	vp = NULL;
3646 	error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3647 	if (error == 0)
3648 		error = nlookup(&nd);
3649 	if (error == 0)
3650 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3651 	mp = nd.nl_nch.mount;
3652 	nlookup_done(&nd);
3653 	if (error == 0) {
3654 		bzero(&fh, sizeof(fh));
3655 		fh.fh_fsid = mp->mnt_stat.f_fsid;
3656 		error = VFS_VPTOFH(vp, &fh.fh_fid);
3657 		vput(vp);
3658 		if (error == 0)
3659 			error = copyout(&fh, uap->fhp, sizeof(fh));
3660 	}
3661 	return (error);
3662 }
3663 
3664 /*
3665  * fhopen_args(const struct fhandle *u_fhp, int flags)
3666  *
3667  * syscall for the rpc.lockd to use to translate a NFS file handle into
3668  * an open descriptor.
3669  *
3670  * warning: do not remove the priv_check() call or this becomes one giant
3671  * security hole.
3672  */
3673 int
3674 sys_fhopen(struct fhopen_args *uap)
3675 {
3676 	struct thread *td = curthread;
3677 	struct proc *p = td->td_proc;
3678 	struct mount *mp;
3679 	struct vnode *vp;
3680 	struct fhandle fhp;
3681 	struct vattr vat;
3682 	struct vattr *vap = &vat;
3683 	struct flock lf;
3684 	int fmode, mode, error, type;
3685 	struct file *nfp;
3686 	struct file *fp;
3687 	int indx;
3688 
3689 	/*
3690 	 * Must be super user
3691 	 */
3692 	error = priv_check(td, PRIV_ROOT);
3693 	if (error)
3694 		return (error);
3695 
3696 	fmode = FFLAGS(uap->flags);
3697 	/* why not allow a non-read/write open for our lockd? */
3698 	if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3699 		return (EINVAL);
3700 	error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3701 	if (error)
3702 		return(error);
3703 	/* find the mount point */
3704 	mp = vfs_getvfs(&fhp.fh_fsid);
3705 	if (mp == NULL)
3706 		return (ESTALE);
3707 	/* now give me my vnode, it gets returned to me locked */
3708 	error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3709 	if (error)
3710 		return (error);
3711  	/*
3712 	 * from now on we have to make sure not
3713 	 * to forget about the vnode
3714 	 * any error that causes an abort must vput(vp)
3715 	 * just set error = err and 'goto bad;'.
3716 	 */
3717 
3718 	/*
3719 	 * from vn_open
3720 	 */
3721 	if (vp->v_type == VLNK) {
3722 		error = EMLINK;
3723 		goto bad;
3724 	}
3725 	if (vp->v_type == VSOCK) {
3726 		error = EOPNOTSUPP;
3727 		goto bad;
3728 	}
3729 	mode = 0;
3730 	if (fmode & (FWRITE | O_TRUNC)) {
3731 		if (vp->v_type == VDIR) {
3732 			error = EISDIR;
3733 			goto bad;
3734 		}
3735 		error = vn_writechk(vp, NULL);
3736 		if (error)
3737 			goto bad;
3738 		mode |= VWRITE;
3739 	}
3740 	if (fmode & FREAD)
3741 		mode |= VREAD;
3742 	if (mode) {
3743 		error = VOP_ACCESS(vp, mode, p->p_ucred);
3744 		if (error)
3745 			goto bad;
3746 	}
3747 	if (fmode & O_TRUNC) {
3748 		vn_unlock(vp);				/* XXX */
3749 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
3750 		VATTR_NULL(vap);
3751 		vap->va_size = 0;
3752 		error = VOP_SETATTR(vp, vap, p->p_ucred);
3753 		if (error)
3754 			goto bad;
3755 	}
3756 
3757 	/*
3758 	 * VOP_OPEN needs the file pointer so it can potentially override
3759 	 * it.
3760 	 *
3761 	 * WARNING! no f_nchandle will be associated when fhopen()ing a
3762 	 * directory.  XXX
3763 	 */
3764 	if ((error = falloc(p, &nfp, &indx)) != 0)
3765 		goto bad;
3766 	fp = nfp;
3767 
3768 	error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3769 	if (error) {
3770 		/*
3771 		 * setting f_ops this way prevents VOP_CLOSE from being
3772 		 * called or fdrop() releasing the vp from v_data.   Since
3773 		 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3774 		 */
3775 		fp->f_ops = &badfileops;
3776 		fp->f_data = NULL;
3777 		goto bad_drop;
3778 	}
3779 
3780 	/*
3781 	 * The fp is given its own reference, we still have our ref and lock.
3782 	 *
3783 	 * Assert that all regular files must be created with a VM object.
3784 	 */
3785 	if (vp->v_type == VREG && vp->v_object == NULL) {
3786 		kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3787 		goto bad_drop;
3788 	}
3789 
3790 	/*
3791 	 * The open was successful.  Handle any locking requirements.
3792 	 */
3793 	if (fmode & (O_EXLOCK | O_SHLOCK)) {
3794 		lf.l_whence = SEEK_SET;
3795 		lf.l_start = 0;
3796 		lf.l_len = 0;
3797 		if (fmode & O_EXLOCK)
3798 			lf.l_type = F_WRLCK;
3799 		else
3800 			lf.l_type = F_RDLCK;
3801 		if (fmode & FNONBLOCK)
3802 			type = 0;
3803 		else
3804 			type = F_WAIT;
3805 		vn_unlock(vp);
3806 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3807 			/*
3808 			 * release our private reference.
3809 			 */
3810 			fsetfd(p, NULL, indx);
3811 			fdrop(fp);
3812 			vrele(vp);
3813 			return (error);
3814 		}
3815 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3816 		fp->f_flag |= FHASLOCK;
3817 	}
3818 
3819 	/*
3820 	 * Clean up.  Associate the file pointer with the previously
3821 	 * reserved descriptor and return it.
3822 	 */
3823 	vput(vp);
3824 	fsetfd(p, fp, indx);
3825 	fdrop(fp);
3826 	uap->sysmsg_result = indx;
3827 	return (0);
3828 
3829 bad_drop:
3830 	fsetfd(p, NULL, indx);
3831 	fdrop(fp);
3832 bad:
3833 	vput(vp);
3834 	return (error);
3835 }
3836 
3837 /*
3838  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3839  */
3840 int
3841 sys_fhstat(struct fhstat_args *uap)
3842 {
3843 	struct thread *td = curthread;
3844 	struct stat sb;
3845 	fhandle_t fh;
3846 	struct mount *mp;
3847 	struct vnode *vp;
3848 	int error;
3849 
3850 	/*
3851 	 * Must be super user
3852 	 */
3853 	error = priv_check(td, PRIV_ROOT);
3854 	if (error)
3855 		return (error);
3856 
3857 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3858 	if (error)
3859 		return (error);
3860 
3861 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3862 		return (ESTALE);
3863 	if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3864 		return (error);
3865 	error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3866 	vput(vp);
3867 	if (error)
3868 		return (error);
3869 	error = copyout(&sb, uap->sb, sizeof(sb));
3870 	return (error);
3871 }
3872 
3873 /*
3874  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3875  */
3876 int
3877 sys_fhstatfs(struct fhstatfs_args *uap)
3878 {
3879 	struct thread *td = curthread;
3880 	struct proc *p = td->td_proc;
3881 	struct statfs *sp;
3882 	struct mount *mp;
3883 	struct vnode *vp;
3884 	struct statfs sb;
3885 	char *fullpath, *freepath;
3886 	fhandle_t fh;
3887 	int error;
3888 
3889 	/*
3890 	 * Must be super user
3891 	 */
3892 	if ((error = priv_check(td, PRIV_ROOT)))
3893 		return (error);
3894 
3895 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3896 		return (error);
3897 
3898 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3899 		return (ESTALE);
3900 
3901 	if (p != NULL && !chroot_visible_mnt(mp, p))
3902 		return (ESTALE);
3903 
3904 	if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3905 		return (error);
3906 	mp = vp->v_mount;
3907 	sp = &mp->mnt_stat;
3908 	vput(vp);
3909 	if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3910 		return (error);
3911 
3912 	error = mount_path(p, mp, &fullpath, &freepath);
3913 	if (error)
3914 		return(error);
3915 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3916 	strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3917 	kfree(freepath, M_TEMP);
3918 
3919 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3920 	if (priv_check(td, PRIV_ROOT)) {
3921 		bcopy(sp, &sb, sizeof(sb));
3922 		sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3923 		sp = &sb;
3924 	}
3925 	return (copyout(sp, uap->buf, sizeof(*sp)));
3926 }
3927 
3928 /*
3929  * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3930  */
3931 int
3932 sys_fhstatvfs(struct fhstatvfs_args *uap)
3933 {
3934 	struct thread *td = curthread;
3935 	struct proc *p = td->td_proc;
3936 	struct statvfs *sp;
3937 	struct mount *mp;
3938 	struct vnode *vp;
3939 	fhandle_t fh;
3940 	int error;
3941 
3942 	/*
3943 	 * Must be super user
3944 	 */
3945 	if ((error = priv_check(td, PRIV_ROOT)))
3946 		return (error);
3947 
3948 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3949 		return (error);
3950 
3951 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3952 		return (ESTALE);
3953 
3954 	if (p != NULL && !chroot_visible_mnt(mp, p))
3955 		return (ESTALE);
3956 
3957 	if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3958 		return (error);
3959 	mp = vp->v_mount;
3960 	sp = &mp->mnt_vstat;
3961 	vput(vp);
3962 	if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3963 		return (error);
3964 
3965 	sp->f_flag = 0;
3966 	if (mp->mnt_flag & MNT_RDONLY)
3967 		sp->f_flag |= ST_RDONLY;
3968 	if (mp->mnt_flag & MNT_NOSUID)
3969 		sp->f_flag |= ST_NOSUID;
3970 
3971 	return (copyout(sp, uap->buf, sizeof(*sp)));
3972 }
3973 
3974 
3975 /*
3976  * Syscall to push extended attribute configuration information into the
3977  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3978  * a command (int cmd), and attribute name and misc data.  For now, the
3979  * attribute name is left in userspace for consumption by the VFS_op.
3980  * It will probably be changed to be copied into sysspace by the
3981  * syscall in the future, once issues with various consumers of the
3982  * attribute code have raised their hands.
3983  *
3984  * Currently this is used only by UFS Extended Attributes.
3985  */
3986 int
3987 sys_extattrctl(struct extattrctl_args *uap)
3988 {
3989 	struct nlookupdata nd;
3990 	struct mount *mp;
3991 	struct vnode *vp;
3992 	int error;
3993 
3994 	vp = NULL;
3995 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3996 	if (error == 0)
3997 		error = nlookup(&nd);
3998 	if (error == 0) {
3999 		mp = nd.nl_nch.mount;
4000 		error = VFS_EXTATTRCTL(mp, uap->cmd,
4001 				uap->attrname, uap->arg,
4002 				nd.nl_cred);
4003 	}
4004 	nlookup_done(&nd);
4005 	return (error);
4006 }
4007 
4008 /*
4009  * Syscall to set a named extended attribute on a file or directory.
4010  * Accepts attribute name, and a uio structure pointing to the data to set.
4011  * The uio is consumed in the style of writev().  The real work happens
4012  * in VOP_SETEXTATTR().
4013  */
4014 int
4015 sys_extattr_set_file(struct extattr_set_file_args *uap)
4016 {
4017 	char attrname[EXTATTR_MAXNAMELEN];
4018 	struct iovec aiov[UIO_SMALLIOV];
4019 	struct iovec *needfree;
4020 	struct nlookupdata nd;
4021 	struct iovec *iov;
4022 	struct vnode *vp;
4023 	struct uio auio;
4024 	u_int iovlen;
4025 	u_int cnt;
4026 	int error;
4027 	int i;
4028 
4029 	error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4030 	if (error)
4031 		return (error);
4032 
4033 	vp = NULL;
4034 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4035 	if (error == 0)
4036 		error = nlookup(&nd);
4037 	if (error == 0)
4038 		error = ncp_writechk(&nd.nl_nch);
4039 	if (error == 0)
4040 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4041 	if (error) {
4042 		nlookup_done(&nd);
4043 		return (error);
4044 	}
4045 
4046 	needfree = NULL;
4047 	iovlen = uap->iovcnt * sizeof(struct iovec);
4048 	if (uap->iovcnt > UIO_SMALLIOV) {
4049 		if (uap->iovcnt > UIO_MAXIOV) {
4050 			error = EINVAL;
4051 			goto done;
4052 		}
4053 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4054 		needfree = iov;
4055 	} else {
4056 		iov = aiov;
4057 	}
4058 	auio.uio_iov = iov;
4059 	auio.uio_iovcnt = uap->iovcnt;
4060 	auio.uio_rw = UIO_WRITE;
4061 	auio.uio_segflg = UIO_USERSPACE;
4062 	auio.uio_td = nd.nl_td;
4063 	auio.uio_offset = 0;
4064 	if ((error = copyin(uap->iovp, iov, iovlen)))
4065 		goto done;
4066 	auio.uio_resid = 0;
4067 	for (i = 0; i < uap->iovcnt; i++) {
4068 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
4069 			error = EINVAL;
4070 			goto done;
4071 		}
4072 		auio.uio_resid += iov->iov_len;
4073 		iov++;
4074 	}
4075 	cnt = auio.uio_resid;
4076 	error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4077 	cnt -= auio.uio_resid;
4078 	uap->sysmsg_result = cnt;
4079 done:
4080 	vput(vp);
4081 	nlookup_done(&nd);
4082 	if (needfree)
4083 		FREE(needfree, M_IOV);
4084 	return (error);
4085 }
4086 
4087 /*
4088  * Syscall to get a named extended attribute on a file or directory.
4089  * Accepts attribute name, and a uio structure pointing to a buffer for the
4090  * data.  The uio is consumed in the style of readv().  The real work
4091  * happens in VOP_GETEXTATTR();
4092  */
4093 int
4094 sys_extattr_get_file(struct extattr_get_file_args *uap)
4095 {
4096 	char attrname[EXTATTR_MAXNAMELEN];
4097 	struct iovec aiov[UIO_SMALLIOV];
4098 	struct iovec *needfree;
4099 	struct nlookupdata nd;
4100 	struct iovec *iov;
4101 	struct vnode *vp;
4102 	struct uio auio;
4103 	u_int iovlen;
4104 	u_int cnt;
4105 	int error;
4106 	int i;
4107 
4108 	error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4109 	if (error)
4110 		return (error);
4111 
4112 	vp = NULL;
4113 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4114 	if (error == 0)
4115 		error = nlookup(&nd);
4116 	if (error == 0)
4117 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4118 	if (error) {
4119 		nlookup_done(&nd);
4120 		return (error);
4121 	}
4122 
4123 	iovlen = uap->iovcnt * sizeof (struct iovec);
4124 	needfree = NULL;
4125 	if (uap->iovcnt > UIO_SMALLIOV) {
4126 		if (uap->iovcnt > UIO_MAXIOV) {
4127 			error = EINVAL;
4128 			goto done;
4129 		}
4130 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4131 		needfree = iov;
4132 	} else {
4133 		iov = aiov;
4134 	}
4135 	auio.uio_iov = iov;
4136 	auio.uio_iovcnt = uap->iovcnt;
4137 	auio.uio_rw = UIO_READ;
4138 	auio.uio_segflg = UIO_USERSPACE;
4139 	auio.uio_td = nd.nl_td;
4140 	auio.uio_offset = 0;
4141 	if ((error = copyin(uap->iovp, iov, iovlen)))
4142 		goto done;
4143 	auio.uio_resid = 0;
4144 	for (i = 0; i < uap->iovcnt; i++) {
4145 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
4146 			error = EINVAL;
4147 			goto done;
4148 		}
4149 		auio.uio_resid += iov->iov_len;
4150 		iov++;
4151 	}
4152 	cnt = auio.uio_resid;
4153 	error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4154 	cnt -= auio.uio_resid;
4155 	uap->sysmsg_result = cnt;
4156 done:
4157 	vput(vp);
4158 	nlookup_done(&nd);
4159 	if (needfree)
4160 		FREE(needfree, M_IOV);
4161 	return(error);
4162 }
4163 
4164 /*
4165  * Syscall to delete a named extended attribute from a file or directory.
4166  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
4167  */
4168 int
4169 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4170 {
4171 	char attrname[EXTATTR_MAXNAMELEN];
4172 	struct nlookupdata nd;
4173 	struct vnode *vp;
4174 	int error;
4175 
4176 	error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4177 	if (error)
4178 		return(error);
4179 
4180 	vp = NULL;
4181 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4182 	if (error == 0)
4183 		error = nlookup(&nd);
4184 	if (error == 0)
4185 		error = ncp_writechk(&nd.nl_nch);
4186 	if (error == 0)
4187 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4188 	if (error) {
4189 		nlookup_done(&nd);
4190 		return (error);
4191 	}
4192 
4193 	error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4194 	vput(vp);
4195 	nlookup_done(&nd);
4196 	return(error);
4197 }
4198 
4199 /*
4200  * Determine if the mount is visible to the process.
4201  */
4202 static int
4203 chroot_visible_mnt(struct mount *mp, struct proc *p)
4204 {
4205 	struct nchandle nch;
4206 
4207 	/*
4208 	 * Traverse from the mount point upwards.  If we hit the process
4209 	 * root then the mount point is visible to the process.
4210 	 */
4211 	nch = mp->mnt_ncmountpt;
4212 	while (nch.ncp) {
4213 		if (nch.mount == p->p_fd->fd_nrdir.mount &&
4214 		    nch.ncp == p->p_fd->fd_nrdir.ncp) {
4215 			return(1);
4216 		}
4217 		if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4218 			nch = nch.mount->mnt_ncmounton;
4219 		} else {
4220 			nch.ncp = nch.ncp->nc_parent;
4221 		}
4222 	}
4223 
4224 	/*
4225 	 * If the mount point is not visible to the process, but the
4226 	 * process root is in a subdirectory of the mount, return
4227 	 * TRUE anyway.
4228 	 */
4229 	if (p->p_fd->fd_nrdir.mount == mp)
4230 		return(1);
4231 
4232 	return(0);
4233 }
4234 
4235