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