Consolidate the file descriptor destruction code used when a newly created
[dragonfly.git] / sys / kern / vfs_syscalls.c
CommitLineData
984263bc
MD
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 $
fa541be6 40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.90 2006/05/19 05:15:35 dillon Exp $
984263bc
MD
41 */
42
984263bc
MD
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/buf.h>
16df2c74 46#include <sys/conf.h>
984263bc
MD
47#include <sys/sysent.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
432b8263 50#include <sys/mountctl.h>
984263bc 51#include <sys/sysproto.h>
984263bc
MD
52#include <sys/filedesc.h>
53#include <sys/kernel.h>
54#include <sys/fcntl.h>
55#include <sys/file.h>
56#include <sys/linker.h>
57#include <sys/stat.h>
58#include <sys/unistd.h>
59#include <sys/vnode.h>
60#include <sys/proc.h>
dadab5e9 61#include <sys/namei.h>
21739618 62#include <sys/nlookup.h>
984263bc
MD
63#include <sys/dirent.h>
64#include <sys/extattr.h>
8f6f8622 65#include <sys/kern_syscall.h>
984263bc
MD
66
67#include <machine/limits.h>
1f2de5d4 68#include <vfs/union/union.h>
984263bc
MD
69#include <sys/sysctl.h>
70#include <vm/vm.h>
71#include <vm/vm_object.h>
72#include <vm/vm_zone.h>
73#include <vm/vm_page.h>
74
dadab5e9
MD
75#include <sys/file2.h>
76
e24b948e 77static int checkvp_chdir (struct vnode *vn, struct thread *td);
21739618 78static void checkdirs (struct vnode *olddp, struct namecache *ncp);
402ed7e1 79static int chroot_refuse_vdir_fds (struct filedesc *fdp);
75ffff0d 80static int chroot_visible_mnt(struct mount *mp, struct proc *p);
402ed7e1
RG
81static int getutimes (const struct timeval *, struct timespec *);
82static int setfown (struct vnode *, uid_t, gid_t);
83static int setfmode (struct vnode *, int);
84static int setfflags (struct vnode *, int);
85static int setutimes (struct vnode *, const struct timespec *, int);
984263bc
MD
86static int usermount = 0; /* if 1, non-root can mount fs. */
87
402ed7e1 88int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
984263bc
MD
89
90SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
91
92/*
93 * Virtual File System System Calls
94 */
95
96/*
97 * Mount a file system.
98 */
41c20dac
MD
99/*
100 * mount_args(char *type, char *path, int flags, caddr_t data)
101 */
984263bc
MD
102/* ARGSUSED */
103int
41c20dac 104mount(struct mount_args *uap)
984263bc 105{
dadab5e9
MD
106 struct thread *td = curthread;
107 struct proc *p = td->td_proc;
984263bc 108 struct vnode *vp;
21739618 109 struct namecache *ncp;
984263bc
MD
110 struct mount *mp;
111 struct vfsconf *vfsp;
112 int error, flag = 0, flag2 = 0;
113 struct vattr va;
21739618 114 struct nlookupdata nd;
984263bc 115 char fstypename[MFSNAMELEN];
21739618 116 struct nlcomponent nlc;
acde96db 117 struct ucred *cred = p->p_ucred;
984263bc 118
cd0d3e17 119 KKASSERT(p);
acde96db 120 if (cred->cr_prison != NULL)
84c3a710 121 return (EPERM);
dadab5e9 122 if (usermount == 0 && (error = suser(td)))
984263bc
MD
123 return (error);
124 /*
125 * Do not allow NFS export by non-root users.
126 */
ab2eb4eb 127 if (uap->flags & MNT_EXPORTED) {
dadab5e9 128 error = suser(td);
984263bc
MD
129 if (error)
130 return (error);
131 }
132 /*
133 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
134 */
dadab5e9 135 if (suser(td))
ab2eb4eb 136 uap->flags |= MNT_NOSUID | MNT_NODEV;
21739618 137
984263bc 138 /*
21739618 139 * Lookup the requested path and extract the ncp and vnode.
984263bc 140 */
21739618
MD
141 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
142 if (error == 0) {
143 if ((error = nlookup(&nd)) == 0) {
144 if (nd.nl_ncp->nc_vp == NULL)
145 error = ENOENT;
146 }
147 }
148 if (error) {
149 nlookup_done(&nd);
984263bc 150 return (error);
21739618
MD
151 }
152
153 /*
154 * Extract the locked+refd ncp and cleanup the nd structure
155 */
156 ncp = nd.nl_ncp;
157 nd.nl_ncp = NULL;
158 nlookup_done(&nd);
159
160 /*
161 * now we have the locked ref'd ncp and unreferenced vnode.
162 */
163 vp = ncp->nc_vp;
87de5057 164 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
21739618
MD
165 cache_put(ncp);
166 return (error);
167 }
168 cache_unlock(ncp);
169
170 /*
171 * Now we have an unlocked ref'd ncp and a locked ref'd vp
172 */
ab2eb4eb 173 if (uap->flags & MNT_UPDATE) {
984263bc 174 if ((vp->v_flag & VROOT) == 0) {
21739618 175 cache_drop(ncp);
984263bc
MD
176 vput(vp);
177 return (EINVAL);
178 }
179 mp = vp->v_mount;
180 flag = mp->mnt_flag;
181 flag2 = mp->mnt_kern_flag;
182 /*
183 * We only allow the filesystem to be reloaded if it
184 * is currently mounted read-only.
185 */
ab2eb4eb 186 if ((uap->flags & MNT_RELOAD) &&
984263bc 187 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
21739618 188 cache_drop(ncp);
984263bc
MD
189 vput(vp);
190 return (EOPNOTSUPP); /* Needs translation */
191 }
192 /*
193 * Only root, or the user that did the original mount is
194 * permitted to update it.
195 */
acde96db 196 if (mp->mnt_stat.f_owner != cred->cr_uid &&
dadab5e9 197 (error = suser(td))) {
21739618 198 cache_drop(ncp);
984263bc
MD
199 vput(vp);
200 return (error);
201 }
f9642f56 202 if (vfs_busy(mp, LK_NOWAIT)) {
21739618 203 cache_drop(ncp);
984263bc
MD
204 vput(vp);
205 return (EBUSY);
206 }
984263bc
MD
207 if ((vp->v_flag & VMOUNT) != 0 ||
208 vp->v_mountedhere != NULL) {
21739618 209 cache_drop(ncp);
f9642f56 210 vfs_unbusy(mp);
984263bc
MD
211 vput(vp);
212 return (EBUSY);
213 }
214 vp->v_flag |= VMOUNT;
984263bc 215 mp->mnt_flag |=
ab2eb4eb 216 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
ca466bae 217 VOP_UNLOCK(vp, 0);
984263bc
MD
218 goto update;
219 }
220 /*
221 * If the user is not root, ensure that they own the directory
222 * onto which we are attempting to mount.
223 */
87de5057 224 if ((error = VOP_GETATTR(vp, &va)) ||
acde96db 225 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
21739618 226 cache_drop(ncp);
984263bc
MD
227 vput(vp);
228 return (error);
229 }
87de5057 230 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
21739618 231 cache_drop(ncp);
984263bc
MD
232 vput(vp);
233 return (error);
234 }
235 if (vp->v_type != VDIR) {
21739618 236 cache_drop(ncp);
984263bc
MD
237 vput(vp);
238 return (ENOTDIR);
239 }
ab2eb4eb 240 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
21739618 241 cache_drop(ncp);
984263bc
MD
242 vput(vp);
243 return (error);
244 }
21739618 245 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
984263bc
MD
246 if (!strcmp(vfsp->vfc_name, fstypename))
247 break;
21739618 248 }
984263bc
MD
249 if (vfsp == NULL) {
250 linker_file_t lf;
251
252 /* Only load modules for root (very important!) */
dadab5e9 253 if ((error = suser(td)) != 0) {
21739618 254 cache_drop(ncp);
984263bc
MD
255 vput(vp);
256 return error;
257 }
258 error = linker_load_file(fstypename, &lf);
259 if (error || lf == NULL) {
21739618 260 cache_drop(ncp);
984263bc
MD
261 vput(vp);
262 if (lf == NULL)
263 error = ENODEV;
264 return error;
265 }
266 lf->userrefs++;
267 /* lookup again, see if the VFS was loaded */
21739618 268 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
984263bc
MD
269 if (!strcmp(vfsp->vfc_name, fstypename))
270 break;
21739618 271 }
984263bc
MD
272 if (vfsp == NULL) {
273 lf->userrefs--;
274 linker_file_unload(lf);
21739618 275 cache_drop(ncp);
984263bc
MD
276 vput(vp);
277 return (ENODEV);
278 }
279 }
984263bc
MD
280 if ((vp->v_flag & VMOUNT) != 0 ||
281 vp->v_mountedhere != NULL) {
21739618 282 cache_drop(ncp);
984263bc
MD
283 vput(vp);
284 return (EBUSY);
285 }
286 vp->v_flag |= VMOUNT;
984263bc
MD
287
288 /*
289 * Allocate and initialize the filesystem.
290 */
0961aa92 291 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
984263bc
MD
292 TAILQ_INIT(&mp->mnt_nvnodelist);
293 TAILQ_INIT(&mp->mnt_reservedvnlist);
2281065e 294 TAILQ_INIT(&mp->mnt_jlist);
984263bc 295 mp->mnt_nvnodelistsize = 0;
f2770c70 296 lockinit(&mp->mnt_lock, "vfslock", 0, LK_NOPAUSE);
f9642f56 297 vfs_busy(mp, LK_NOWAIT);
984263bc
MD
298 mp->mnt_op = vfsp->vfc_vfsops;
299 mp->mnt_vfc = vfsp;
300 vfsp->vfc_refcount++;
301 mp->mnt_stat.f_type = vfsp->vfc_typenum;
302 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
303 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
304 mp->mnt_vnodecovered = vp;
acde96db 305 mp->mnt_stat.f_owner = cred->cr_uid;
984263bc 306 mp->mnt_iosize_max = DFLTPHYS;
ca466bae 307 VOP_UNLOCK(vp, 0);
984263bc
MD
308update:
309 /*
310 * Set the mount level flags.
311 */
ab2eb4eb 312 if (uap->flags & MNT_RDONLY)
984263bc
MD
313 mp->mnt_flag |= MNT_RDONLY;
314 else if (mp->mnt_flag & MNT_RDONLY)
315 mp->mnt_kern_flag |= MNTK_WANTRDWR;
316 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
317 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
318 MNT_NOSYMFOLLOW | MNT_IGNORE |
319 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
ab2eb4eb 320 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
984263bc
MD
321 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
322 MNT_NOSYMFOLLOW | MNT_IGNORE |
323 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
324 /*
325 * Mount the filesystem.
326 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
fad57d0e 327 * get.
984263bc 328 */
acde96db 329 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
984263bc
MD
330 if (mp->mnt_flag & MNT_UPDATE) {
331 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
332 mp->mnt_flag &= ~MNT_RDONLY;
333 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
334 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
335 if (error) {
336 mp->mnt_flag = flag;
337 mp->mnt_kern_flag = flag2;
338 }
f9642f56 339 vfs_unbusy(mp);
984263bc 340 vp->v_flag &= ~VMOUNT;
984263bc 341 vrele(vp);
21739618 342 cache_drop(ncp);
984263bc
MD
343 return (error);
344 }
ca466bae 345 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc 346 /*
21739618
MD
347 * Put the new filesystem on the mount list after root. The mount
348 * point gets its own mnt_ncp which is a special ncp linking the
349 * vnode-under to the root of the new mount. The lookup code
350 * detects the mount point going forward and detects the special
351 * mnt_ncp via NCP_MOUNTPT going backwards.
8e005a45
MD
352 *
353 * It is not necessary to invalidate or purge the vnode underneath
354 * because elements under the mount will be given their own glue
355 * namecache record.
984263bc 356 */
984263bc 357 if (!error) {
21739618
MD
358 nlc.nlc_nameptr = "";
359 nlc.nlc_namelen = 0;
360 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
2386d5b6 361 cache_setunresolved(mp->mnt_ncp);
21739618 362 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
646a1cda 363 mp->mnt_ncp->nc_mount = mp;
21739618
MD
364 cache_drop(ncp);
365 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
984263bc
MD
366 vp->v_flag &= ~VMOUNT;
367 vp->v_mountedhere = mp;
861905fb 368 mountlist_insert(mp, MNTINS_LAST);
21739618
MD
369 checkdirs(vp, mp->mnt_ncp);
370 cache_unlock(mp->mnt_ncp); /* leave ref intact */
ca466bae 371 VOP_UNLOCK(vp, 0);
41a01a4d 372 error = vfs_allocate_syncvnode(mp);
f9642f56 373 vfs_unbusy(mp);
acde96db 374 if ((error = VFS_START(mp, 0)) != 0)
984263bc
MD
375 vrele(vp);
376 } else {
6ddb7618
MD
377 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
378 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
379 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
0961aa92
MD
380 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
381 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
984263bc 382 vp->v_flag &= ~VMOUNT;
984263bc 383 mp->mnt_vfc->vfc_refcount--;
f9642f56 384 vfs_unbusy(mp);
e38462a2 385 free(mp, M_MOUNT);
21739618 386 cache_drop(ncp);
984263bc
MD
387 vput(vp);
388 }
389 return (error);
390}
391
392/*
393 * Scan all active processes to see if any of them have a current
394 * or root directory onto which the new filesystem has just been
395 * mounted. If so, replace them with the new mount point.
21739618
MD
396 *
397 * The passed ncp is ref'd and locked (from the mount code) and
398 * must be associated with the vnode representing the root of the
399 * mount point.
984263bc
MD
400 */
401static void
21739618 402checkdirs(struct vnode *olddp, struct namecache *ncp)
984263bc
MD
403{
404 struct filedesc *fdp;
405 struct vnode *newdp;
690a3127 406 struct mount *mp;
984263bc
MD
407 struct proc *p;
408
409 if (olddp->v_usecount == 1)
410 return;
690a3127
MD
411 mp = olddp->v_mountedhere;
412 if (VFS_ROOT(mp, &newdp))
984263bc 413 panic("mount: lost mount");
21739618
MD
414 cache_setvp(ncp, newdp);
415
416 if (rootvnode == olddp) {
417 vref(newdp);
418 vfs_cache_setroot(newdp, cache_hold(ncp));
419 }
420
f62004ad 421 FOREACH_PROC_IN_SYSTEM(p) {
984263bc
MD
422 fdp = p->p_fd;
423 if (fdp->fd_cdir == olddp) {
424 vrele(fdp->fd_cdir);
597aea93 425 vref(newdp);
984263bc 426 fdp->fd_cdir = newdp;
690a3127 427 cache_drop(fdp->fd_ncdir);
21739618 428 fdp->fd_ncdir = cache_hold(ncp);
984263bc
MD
429 }
430 if (fdp->fd_rdir == olddp) {
431 vrele(fdp->fd_rdir);
597aea93 432 vref(newdp);
984263bc 433 fdp->fd_rdir = newdp;
690a3127 434 cache_drop(fdp->fd_nrdir);
21739618 435 fdp->fd_nrdir = cache_hold(ncp);
984263bc
MD
436 }
437 }
984263bc
MD
438 vput(newdp);
439}
440
441/*
442 * Unmount a file system.
443 *
444 * Note: unmount takes a path to the vnode mounted on as argument,
445 * not special file (as before).
446 */
41c20dac
MD
447/*
448 * umount_args(char *path, int flags)
449 */
984263bc
MD
450/* ARGSUSED */
451int
41c20dac 452unmount(struct unmount_args *uap)
984263bc 453{
dadab5e9
MD
454 struct thread *td = curthread;
455 struct proc *p = td->td_proc;
d2293868 456 struct mount *mp = NULL;
984263bc 457 int error;
fad57d0e 458 struct nlookupdata nd;
984263bc 459
dadab5e9 460 KKASSERT(p);
84c3a710
HP
461 if (p->p_ucred->cr_prison != NULL)
462 return (EPERM);
463 if (usermount == 0 && (error = suser(td)))
464 return (error);
465
ab2eb4eb 466 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
467 if (error == 0)
468 error = nlookup(&nd);
fad57d0e 469 if (error)
d2293868 470 goto out;
fad57d0e 471
d2293868 472 mp = nd.nl_ncp->nc_mount;
984263bc
MD
473
474 /*
475 * Only root, or the user that did the original mount is
476 * permitted to unmount this filesystem.
477 */
478 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
d2293868
MD
479 (error = suser(td)))
480 goto out;
984263bc
MD
481
482 /*
483 * Don't allow unmounting the root file system.
484 */
485 if (mp->mnt_flag & MNT_ROOTFS) {
d2293868
MD
486 error = EINVAL;
487 goto out;
984263bc
MD
488 }
489
490 /*
491 * Must be the root of the filesystem
492 */
d2293868
MD
493 if (! (nd.nl_ncp->nc_flag & NCF_MOUNTPT)) {
494 error = EINVAL;
495 goto out;
984263bc 496 }
d2293868
MD
497
498out:
499 nlookup_done(&nd);
500 if (error)
501 return (error);
acde96db 502 return (dounmount(mp, uap->flags));
984263bc
MD
503}
504
505/*
506 * Do the actual file system unmount.
507 */
861905fb
MD
508static int
509dounmount_interlock(struct mount *mp)
510{
511 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
512 return (EBUSY);
513 mp->mnt_kern_flag |= MNTK_UNMOUNT;
514 return(0);
515}
516
984263bc 517int
acde96db 518dounmount(struct mount *mp, int flags)
984263bc
MD
519{
520 struct vnode *coveredvp;
521 int error;
522 int async_flag;
2df9419c 523 int lflags;
984263bc 524
861905fb
MD
525 /*
526 * Exclusive access for unmounting purposes
527 */
528 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
529 return (error);
530
531 /*
532 * Allow filesystems to detect that a forced unmount is in progress.
533 */
984263bc
MD
534 if (flags & MNT_FORCE)
535 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
2df9419c 536 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
df4f70a6 537 error = lockmgr(&mp->mnt_lock, lflags);
984263bc
MD
538 if (error) {
539 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
540 if (mp->mnt_kern_flag & MNTK_MWAIT)
e38462a2 541 wakeup(mp);
984263bc
MD
542 return (error);
543 }
544
545 if (mp->mnt_flag & MNT_EXPUBLIC)
546 vfs_setpublicfs(NULL, NULL, NULL);
547
548 vfs_msync(mp, MNT_WAIT);
549 async_flag = mp->mnt_flag & MNT_ASYNC;
550 mp->mnt_flag &=~ MNT_ASYNC;
551 cache_purgevfs(mp); /* remove cache entries for this file sys */
552 if (mp->mnt_syncer != NULL)
553 vrele(mp->mnt_syncer);
554 if (((mp->mnt_flag & MNT_RDONLY) ||
87de5057 555 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
984263bc 556 (flags & MNT_FORCE))
acde96db 557 error = VFS_UNMOUNT(mp, flags);
984263bc 558 if (error) {
41a01a4d
MD
559 if (mp->mnt_syncer == NULL)
560 vfs_allocate_syncvnode(mp);
984263bc
MD
561 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
562 mp->mnt_flag |= async_flag;
df4f70a6 563 lockmgr(&mp->mnt_lock, LK_RELEASE);
984263bc 564 if (mp->mnt_kern_flag & MNTK_MWAIT)
e38462a2 565 wakeup(mp);
984263bc
MD
566 return (error);
567 }
432b8263
MD
568 /*
569 * Clean up any journals still associated with the mount after
570 * filesystem activity has ceased.
571 */
572 journal_remove_all_journals(mp,
573 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
574
861905fb 575 mountlist_remove(mp);
0961aa92
MD
576
577 /*
578 * Remove any installed vnode ops here so the individual VFSs don't
579 * have to.
580 */
6ddb7618
MD
581 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
582 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
583 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
0961aa92
MD
584 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
585 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
586
984263bc 587 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
7ea21ed1 588 coveredvp->v_mountedhere = NULL;
984263bc 589 vrele(coveredvp);
21739618
MD
590 cache_drop(mp->mnt_ncp);
591 mp->mnt_ncp = NULL;
984263bc
MD
592 }
593 mp->mnt_vfc->vfc_refcount--;
594 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
595 panic("unmount: dangling vnode");
df4f70a6 596 lockmgr(&mp->mnt_lock, LK_RELEASE);
984263bc 597 if (mp->mnt_kern_flag & MNTK_MWAIT)
e38462a2
DR
598 wakeup(mp);
599 free(mp, M_MOUNT);
984263bc
MD
600 return (0);
601}
602
603/*
604 * Sync each mounted filesystem.
605 */
984263bc
MD
606
607#ifdef DEBUG
608static int syncprt = 0;
609SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
6bdbb368 610#endif /* DEBUG */
984263bc 611
861905fb
MD
612static int sync_callback(struct mount *mp, void *data);
613
984263bc
MD
614/* ARGSUSED */
615int
41c20dac 616sync(struct sync_args *uap)
984263bc 617{
861905fb 618 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
6bdbb368 619#ifdef DEBUG
861905fb
MD
620 /*
621 * print out buffer pool stat information on each sync() call.
622 */
984263bc
MD
623 if (syncprt)
624 vfs_bufstats();
6bdbb368 625#endif /* DEBUG */
984263bc
MD
626 return (0);
627}
628
861905fb
MD
629static
630int
631sync_callback(struct mount *mp, void *data __unused)
632{
633 int asyncflag;
634
635 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
636 asyncflag = mp->mnt_flag & MNT_ASYNC;
637 mp->mnt_flag &= ~MNT_ASYNC;
638 vfs_msync(mp, MNT_NOWAIT);
87de5057 639 VFS_SYNC(mp, MNT_NOWAIT);
861905fb
MD
640 mp->mnt_flag |= asyncflag;
641 }
642 return(0);
643}
644
984263bc
MD
645/* XXX PRISON: could be per prison flag */
646static int prison_quotas;
647#if 0
648SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
649#endif
650
651/*
41c20dac
MD
652 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
653 *
984263bc
MD
654 * Change filesystem quotas.
655 */
984263bc
MD
656/* ARGSUSED */
657int
41c20dac 658quotactl(struct quotactl_args *uap)
984263bc 659{
fad57d0e
MD
660 struct nlookupdata nd;
661 struct thread *td;
662 struct proc *p;
41c20dac 663 struct mount *mp;
984263bc 664 int error;
984263bc 665
fad57d0e
MD
666 td = curthread;
667 p = td->td_proc;
41c20dac 668 if (p->p_ucred->cr_prison && !prison_quotas)
984263bc 669 return (EPERM);
fad57d0e 670
ab2eb4eb 671 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
672 if (error == 0)
673 error = nlookup(&nd);
674 if (error == 0) {
675 mp = nd.nl_ncp->nc_mount;
ab2eb4eb 676 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
acde96db 677 uap->arg, nd.nl_cred);
fad57d0e
MD
678 }
679 nlookup_done(&nd);
680 return (error);
984263bc
MD
681}
682
949ecb9b 683/*
2281065e 684 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
949ecb9b
MD
685 * void *buf, int buflen)
686 *
687 * This function operates on a mount point and executes the specified
688 * operation using the specified control data, and possibly returns data.
689 *
690 * The actual number of bytes stored in the result buffer is returned, 0
691 * if none, otherwise an error is returned.
692 */
693/* ARGSUSED */
694int
695mountctl(struct mountctl_args *uap)
696{
697 struct thread *td = curthread;
698 struct proc *p = td->td_proc;
2281065e 699 struct file *fp;
949ecb9b
MD
700 void *ctl = NULL;
701 void *buf = NULL;
702 char *path = NULL;
703 int error;
704
705 /*
706 * Sanity and permissions checks. We must be root.
707 */
708 KKASSERT(p);
709 if (p->p_ucred->cr_prison != NULL)
710 return (EPERM);
711 if ((error = suser(td)) != 0)
712 return (error);
713
714 /*
715 * Argument length checks
716 */
39b13188 717 if (uap->ctllen < 0 || uap->ctllen > 1024)
949ecb9b 718 return (EINVAL);
39b13188 719 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
949ecb9b
MD
720 return (EINVAL);
721 if (uap->path == NULL)
722 return (EINVAL);
723
724 /*
725 * Allocate the necessary buffers and copyin data
726 */
727 path = zalloc(namei_zone);
728 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
729 if (error)
730 goto done;
731
732 if (uap->ctllen) {
733 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
734 error = copyin(uap->ctl, ctl, uap->ctllen);
735 if (error)
736 goto done;
737 }
738 if (uap->buflen)
739 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
740
2281065e
MD
741 /*
742 * Validate the descriptor
743 */
fa541be6
MD
744 fp = holdfp(p->p_fd, uap->fd, -1);
745 if (fp == NULL) {
2281065e
MD
746 error = EBADF;
747 goto done;
748 }
2281065e 749
949ecb9b
MD
750 /*
751 * Execute the internal kernel function and clean up.
752 */
2281065e
MD
753 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
754 if (fp)
9f87144f 755 fdrop(fp);
949ecb9b
MD
756 if (error == 0 && uap->sysmsg_result > 0)
757 error = copyout(buf, uap->buf, uap->sysmsg_result);
758done:
759 if (path)
760 zfree(namei_zone, path);
761 if (ctl)
762 free(ctl, M_TEMP);
763 if (buf)
764 free(buf, M_TEMP);
765 return (error);
766}
767
768/*
769 * Execute a mount control operation by resolving the path to a mount point
770 * and calling vop_mountctl().
771 */
772int
2281065e
MD
773kern_mountctl(const char *path, int op, struct file *fp,
774 const void *ctl, int ctllen,
949ecb9b
MD
775 void *buf, int buflen, int *res)
776{
949ecb9b
MD
777 struct vnode *vp;
778 struct mount *mp;
779 struct nlookupdata nd;
780 int error;
781
782 *res = 0;
783 vp = NULL;
784 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
785 if (error == 0)
786 error = nlookup(&nd);
787 if (error == 0)
788 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
789 nlookup_done(&nd);
790 if (error)
791 return (error);
792
793 mp = vp->v_mount;
794
795 /*
796 * Must be the root of the filesystem
797 */
798 if ((vp->v_flag & VROOT) == 0) {
799 vput(vp);
800 return (EINVAL);
801 }
2281065e 802 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
949ecb9b
MD
803 buf, buflen, res);
804 vput(vp);
805 return (error);
806}
807
984263bc 808int
fad57d0e 809kern_statfs(struct nlookupdata *nd, struct statfs *buf)
984263bc 810{
dadab5e9 811 struct thread *td = curthread;
75ffff0d 812 struct proc *p = td->td_proc;
41c20dac
MD
813 struct mount *mp;
814 struct statfs *sp;
75ffff0d 815 char *fullpath, *freepath;
984263bc 816 int error;
984263bc 817
fad57d0e 818 if ((error = nlookup(nd)) != 0)
984263bc 819 return (error);
fad57d0e 820 mp = nd->nl_ncp->nc_mount;
984263bc 821 sp = &mp->mnt_stat;
acde96db 822 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
984263bc 823 return (error);
75ffff0d
JS
824
825 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
826 if (error)
827 return(error);
828 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
829 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
830 free(freepath, M_TEMP);
831
984263bc 832 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
9697c509
DRJ
833 bcopy(sp, buf, sizeof(*buf));
834 /* Only root should have access to the fsid's. */
835 if (suser(td))
836 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
837 return (0);
984263bc
MD
838}
839
840/*
9697c509 841 * statfs_args(char *path, struct statfs *buf)
41c20dac 842 *
984263bc
MD
843 * Get filesystem statistics.
844 */
984263bc 845int
9697c509
DRJ
846statfs(struct statfs_args *uap)
847{
fad57d0e 848 struct nlookupdata nd;
9697c509
DRJ
849 struct statfs buf;
850 int error;
851
fad57d0e
MD
852 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
853 if (error == 0)
854 error = kern_statfs(&nd, &buf);
855 nlookup_done(&nd);
9697c509
DRJ
856 if (error == 0)
857 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
858 return (error);
859}
860
861int
862kern_fstatfs(int fd, struct statfs *buf)
984263bc 863{
dadab5e9
MD
864 struct thread *td = curthread;
865 struct proc *p = td->td_proc;
984263bc
MD
866 struct file *fp;
867 struct mount *mp;
1fd87d54 868 struct statfs *sp;
75ffff0d 869 char *fullpath, *freepath;
984263bc 870 int error;
984263bc 871
dadab5e9 872 KKASSERT(p);
9697c509
DRJ
873 error = getvnode(p->p_fd, fd, &fp);
874 if (error)
984263bc
MD
875 return (error);
876 mp = ((struct vnode *)fp->f_data)->v_mount;
877 if (mp == NULL)
878 return (EBADF);
acde96db
MD
879 if (fp->f_cred == NULL)
880 return (EINVAL);
984263bc 881 sp = &mp->mnt_stat;
acde96db 882 error = VFS_STATFS(mp, sp, fp->f_cred);
984263bc
MD
883 if (error)
884 return (error);
75ffff0d
JS
885
886 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
887 if (error)
888 return(error);
889 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
890 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
891 free(freepath, M_TEMP);
892
984263bc 893 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
9697c509 894 bcopy(sp, buf, sizeof(*buf));
75ffff0d 895
9697c509
DRJ
896 /* Only root should have access to the fsid's. */
897 if (suser(td))
898 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
899 return (0);
900}
901
902/*
903 * fstatfs_args(int fd, struct statfs *buf)
904 *
905 * Get filesystem statistics.
906 */
907int
908fstatfs(struct fstatfs_args *uap)
909{
910 struct statfs buf;
911 int error;
912
913 error = kern_fstatfs(uap->fd, &buf);
914
915 if (error == 0)
916 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
917 return (error);
984263bc
MD
918}
919
920/*
41c20dac
MD
921 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
922 *
984263bc
MD
923 * Get statistics on all filesystems.
924 */
861905fb
MD
925
926struct getfsstat_info {
927 struct statfs *sfsp;
928 long count;
929 long maxcount;
930 int error;
931 int flags;
932 int is_chrooted;
861905fb
MD
933 struct proc *p;
934};
935
936static int getfsstat_callback(struct mount *, void *);
937
41c20dac 938/* ARGSUSED */
984263bc 939int
41c20dac 940getfsstat(struct getfsstat_args *uap)
984263bc 941{
dadab5e9 942 struct thread *td = curthread;
75ffff0d 943 struct proc *p = td->td_proc;
861905fb 944 struct getfsstat_info info;
75ffff0d 945
861905fb 946 bzero(&info, sizeof(info));
75ffff0d 947 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
861905fb
MD
948 info.is_chrooted = 1;
949 else
950 info.is_chrooted = 0;
951
952 info.maxcount = uap->bufsize / sizeof(struct statfs);
953 info.sfsp = uap->buf;
954 info.count = 0;
955 info.flags = uap->flags;
861905fb
MD
956 info.p = p;
957
958 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
959 if (info.sfsp && info.count > info.maxcount)
960 uap->sysmsg_result = info.maxcount;
75ffff0d 961 else
861905fb
MD
962 uap->sysmsg_result = info.count;
963 return (info.error);
964}
965
966static int
967getfsstat_callback(struct mount *mp, void *data)
968{
969 struct getfsstat_info *info = data;
970 struct statfs *sp;
971 char *freepath;
972 char *fullpath;
973 int error;
974
975 if (info->sfsp && info->count < info->maxcount) {
976 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
977 return(0);
978 sp = &mp->mnt_stat;
979
980 /*
981 * If MNT_NOWAIT or MNT_LAZY is specified, do not
982 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
983 * overrides MNT_WAIT.
984 */
985 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
986 (info->flags & MNT_WAIT)) &&
acde96db 987 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
861905fb
MD
988 return(0);
989 }
990 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
991
992 error = cache_fullpath(info->p, mp->mnt_ncp,
993 &fullpath, &freepath);
994 if (error) {
995 info->error = error;
996 return(-1);
984263bc 997 }
861905fb
MD
998 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
999 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1000 free(freepath, M_TEMP);
1001
1002 error = copyout(sp, info->sfsp, sizeof(*sp));
1003 if (error) {
1004 info->error = error;
1005 return (-1);
984263bc 1006 }
861905fb 1007 ++info->sfsp;
984263bc 1008 }
861905fb
MD
1009 info->count++;
1010 return(0);
984263bc
MD
1011}
1012
1013/*
41c20dac
MD
1014 * fchdir_args(int fd)
1015 *
984263bc
MD
1016 * Change current working directory to a given file descriptor.
1017 */
984263bc
MD
1018/* ARGSUSED */
1019int
41c20dac 1020fchdir(struct fchdir_args *uap)
984263bc 1021{
dadab5e9
MD
1022 struct thread *td = curthread;
1023 struct proc *p = td->td_proc;
41c20dac 1024 struct filedesc *fdp = p->p_fd;
21739618 1025 struct vnode *vp, *ovp;
984263bc
MD
1026 struct mount *mp;
1027 struct file *fp;
21739618
MD
1028 struct namecache *ncp, *oncp;
1029 struct namecache *nct;
984263bc
MD
1030 int error;
1031
ab2eb4eb 1032 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
984263bc
MD
1033 return (error);
1034 vp = (struct vnode *)fp->f_data;
597aea93 1035 vref(vp);
ca466bae 1036 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
21739618 1037 if (vp->v_type != VDIR || fp->f_ncp == NULL)
984263bc
MD
1038 error = ENOTDIR;
1039 else
87de5057 1040 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
984263bc
MD
1041 if (error) {
1042 vput(vp);
1043 return (error);
1044 }
21739618
MD
1045 ncp = cache_hold(fp->f_ncp);
1046 while (!error && (mp = vp->v_mountedhere) != NULL) {
1047 error = nlookup_mp(mp, &nct);
1048 if (error == 0) {
1049 cache_unlock(nct); /* leave ref intact */
1050 vput(vp);
1051 vp = nct->nc_vp;
87de5057 1052 error = vget(vp, LK_SHARED);
21739618
MD
1053 KKASSERT(error == 0);
1054 cache_drop(ncp);
1055 ncp = nct;
1056 }
1057 }
1058 if (error == 0) {
1059 ovp = fdp->fd_cdir;
1060 oncp = fdp->fd_ncdir;
ca466bae 1061 VOP_UNLOCK(vp, 0); /* leave ref intact */
21739618
MD
1062 fdp->fd_cdir = vp;
1063 fdp->fd_ncdir = ncp;
1064 cache_drop(oncp);
1065 vrele(ovp);
1066 } else {
1067 cache_drop(ncp);
1068 vput(vp);
1069 }
1070 return (error);
984263bc
MD
1071}
1072
9697c509 1073int
21739618 1074kern_chdir(struct nlookupdata *nd)
9697c509
DRJ
1075{
1076 struct thread *td = curthread;
1077 struct proc *p = td->td_proc;
1078 struct filedesc *fdp = p->p_fd;
21739618
MD
1079 struct vnode *vp, *ovp;
1080 struct namecache *oncp;
9697c509
DRJ
1081 int error;
1082
21739618 1083 if ((error = nlookup(nd)) != 0)
9697c509 1084 return (error);
21739618
MD
1085 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1086 return (ENOENT);
87de5057 1087 if ((error = vget(vp, LK_SHARED)) != 0)
21739618
MD
1088 return (error);
1089
1090 error = checkvp_chdir(vp, td);
ca466bae 1091 VOP_UNLOCK(vp, 0);
21739618
MD
1092 if (error == 0) {
1093 ovp = fdp->fd_cdir;
1094 oncp = fdp->fd_ncdir;
1095 cache_unlock(nd->nl_ncp); /* leave reference intact */
1096 fdp->fd_ncdir = nd->nl_ncp;
1097 fdp->fd_cdir = vp;
1098 cache_drop(oncp);
1099 vrele(ovp);
1100 nd->nl_ncp = NULL;
1101 } else {
1102 vrele(vp);
e24b948e 1103 }
e24b948e 1104 return (error);
9697c509
DRJ
1105}
1106
984263bc 1107/*
41c20dac
MD
1108 * chdir_args(char *path)
1109 *
984263bc
MD
1110 * Change current working directory (``.'').
1111 */
984263bc 1112int
41c20dac 1113chdir(struct chdir_args *uap)
984263bc 1114{
21739618 1115 struct nlookupdata nd;
9697c509 1116 int error;
984263bc 1117
21739618 1118 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e 1119 if (error == 0)
21739618 1120 error = kern_chdir(&nd);
fad57d0e 1121 nlookup_done(&nd);
9697c509 1122 return (error);
984263bc
MD
1123}
1124
1125/*
1126 * Helper function for raised chroot(2) security function: Refuse if
1127 * any filedescriptors are open directories.
1128 */
1129static int
1130chroot_refuse_vdir_fds(fdp)
1131 struct filedesc *fdp;
1132{
1133 struct vnode *vp;
1134 struct file *fp;
1135 int error;
1136 int fd;
1137
1138 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1139 error = getvnode(fdp, fd, &fp);
1140 if (error)
1141 continue;
1142 vp = (struct vnode *)fp->f_data;
1143 if (vp->v_type != VDIR)
1144 continue;
1145 return(EPERM);
1146 }
1147 return (0);
1148}
1149
1150/*
1151 * This sysctl determines if we will allow a process to chroot(2) if it
1152 * has a directory open:
1153 * 0: disallowed for all processes.
1154 * 1: allowed for processes that were not already chroot(2)'ed.
1155 * 2: allowed for all processes.
1156 */
1157
1158static int chroot_allow_open_directories = 1;
1159
1160SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1161 &chroot_allow_open_directories, 0, "");
1162
e24b948e 1163/*
21739618
MD
1164 * chroot to the specified namecache entry. We obtain the vp from the
1165 * namecache data. The passed ncp must be locked and referenced and will
1166 * remain locked and referenced on return.
e24b948e 1167 */
53dd6631
JS
1168int
1169kern_chroot(struct namecache *ncp)
e24b948e
MD
1170{
1171 struct thread *td = curthread;
1172 struct proc *p = td->td_proc;
1173 struct filedesc *fdp = p->p_fd;
21739618 1174 struct vnode *vp;
e24b948e
MD
1175 int error;
1176
1177 /*
1178 * Only root can chroot
1179 */
1180 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1181 return (error);
1182
1183 /*
1184 * Disallow open directory descriptors (fchdir() breakouts).
1185 */
1186 if (chroot_allow_open_directories == 0 ||
1187 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1188 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1189 return (error);
1190 }
21739618
MD
1191 if ((vp = ncp->nc_vp) == NULL)
1192 return (ENOENT);
1193
87de5057 1194 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 1195 return (error);
e24b948e
MD
1196
1197 /*
1198 * Check the validity of vp as a directory to change to and
1199 * associate it with rdir/jdir.
1200 */
21739618 1201 error = checkvp_chdir(vp, td);
ca466bae 1202 VOP_UNLOCK(vp, 0); /* leave reference intact */
21739618 1203 if (error == 0) {
e24b948e 1204 vrele(fdp->fd_rdir);
21739618 1205 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
690a3127 1206 cache_drop(fdp->fd_nrdir);
21739618 1207 fdp->fd_nrdir = cache_hold(ncp);
e24b948e
MD
1208 if (fdp->fd_jdir == NULL) {
1209 fdp->fd_jdir = vp;
597aea93 1210 vref(fdp->fd_jdir);
21739618 1211 fdp->fd_njdir = cache_hold(ncp);
e24b948e 1212 }
21739618
MD
1213 } else {
1214 vrele(vp);
e24b948e
MD
1215 }
1216 return (error);
1217}
1218
984263bc 1219/*
41c20dac
MD
1220 * chroot_args(char *path)
1221 *
984263bc
MD
1222 * Change notion of root (``/'') directory.
1223 */
984263bc
MD
1224/* ARGSUSED */
1225int
41c20dac 1226chroot(struct chroot_args *uap)
984263bc 1227{
dadab5e9 1228 struct thread *td = curthread;
21739618 1229 struct nlookupdata nd;
e24b948e 1230 int error;
984263bc 1231
e24b948e 1232 KKASSERT(td->td_proc);
21739618 1233 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
53dd6631
JS
1234 if (error) {
1235 nlookup_done(&nd);
1236 return(error);
1237 }
1238 error = nlookup(&nd);
1239 if (error == 0)
1240 error = kern_chroot(nd.nl_ncp);
fad57d0e 1241 nlookup_done(&nd);
53dd6631 1242 return(error);
984263bc
MD
1243}
1244
1245/*
e24b948e
MD
1246 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1247 * determine whether it is legal to chdir to the vnode. The vnode's state
1248 * is not changed by this call.
984263bc 1249 */
e24b948e
MD
1250int
1251checkvp_chdir(struct vnode *vp, struct thread *td)
984263bc 1252{
984263bc
MD
1253 int error;
1254
984263bc
MD
1255 if (vp->v_type != VDIR)
1256 error = ENOTDIR;
1257 else
87de5057 1258 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
984263bc
MD
1259 return (error);
1260}
1261
984263bc 1262int
fad57d0e 1263kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
984263bc 1264{
dadab5e9
MD
1265 struct thread *td = curthread;
1266 struct proc *p = td->td_proc;
fbfe4e7d 1267 struct lwp *lp = td->td_lwp;
41c20dac 1268 struct filedesc *fdp = p->p_fd;
9697c509 1269 int cmode, flags;
984263bc 1270 struct file *nfp;
fad57d0e
MD
1271 struct file *fp;
1272 struct vnode *vp;
984263bc
MD
1273 int type, indx, error;
1274 struct flock lf;
984263bc 1275
984263bc
MD
1276 if ((oflags & O_ACCMODE) == O_ACCMODE)
1277 return (EINVAL);
1278 flags = FFLAGS(oflags);
fad57d0e 1279 error = falloc(p, &nfp, NULL);
984263bc
MD
1280 if (error)
1281 return (error);
1282 fp = nfp;
9697c509 1283 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
fad57d0e 1284
984263bc 1285 /*
fad57d0e
MD
1286 * XXX p_dupfd is a real mess. It allows a device to return a
1287 * file descriptor to be duplicated rather then doing the open
1288 * itself.
984263bc 1289 */
fbfe4e7d 1290 lp->lwp_dupfd = -1;
21739618
MD
1291
1292 /*
fad57d0e
MD
1293 * Call vn_open() to do the lookup and assign the vnode to the
1294 * file pointer. vn_open() does not change the ref count on fp
1295 * and the vnode, on success, will be inherited by the file pointer
1296 * and unlocked.
21739618 1297 */
fad57d0e
MD
1298 nd->nl_flags |= NLC_LOCKVP;
1299 error = vn_open(nd, fp, flags, cmode);
1300 nlookup_done(nd);
984263bc 1301 if (error) {
984263bc
MD
1302 /*
1303 * handle special fdopen() case. bleh. dupfdopen() is
1304 * responsible for dropping the old contents of ofiles[indx]
1305 * if it succeeds.
fad57d0e
MD
1306 *
1307 * Note that if fsetfd() succeeds it will add a ref to fp
0679adc4 1308 * which represents the fd_files[] assignment. We must still
fad57d0e 1309 * drop our reference.
984263bc 1310 */
fbfe4e7d 1311 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
fad57d0e 1312 if (fsetfd(p, fp, &indx) == 0) {
fbfe4e7d 1313 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
fad57d0e
MD
1314 if (error == 0) {
1315 *res = indx;
9f87144f 1316 fdrop(fp); /* our ref */
fad57d0e
MD
1317 return (0);
1318 }
fa541be6 1319 fdealloc(p, fp, indx);
fad57d0e 1320 }
984263bc 1321 }
9f87144f 1322 fdrop(fp); /* our ref */
984263bc
MD
1323 if (error == ERESTART)
1324 error = EINTR;
1325 return (error);
1326 }
fad57d0e
MD
1327
1328 /*
1329 * ref the vnode for ourselves so it can't be ripped out from under
1330 * is. XXX need an ND flag to request that the vnode be returned
1331 * anyway.
1332 */
1333 vp = (struct vnode *)fp->f_data;
1334 vref(vp);
1335 if ((error = fsetfd(p, fp, &indx)) != 0) {
9f87144f 1336 fdrop(fp);
fad57d0e
MD
1337 vrele(vp);
1338 return (error);
1339 }
1340
1341 /*
1342 * If no error occurs the vp will have been assigned to the file
1343 * pointer.
1344 */
fbfe4e7d 1345 lp->lwp_dupfd = 0;
984263bc
MD
1346
1347 /*
1348 * There should be 2 references on the file, one from the descriptor
1349 * table, and one for us.
1350 *
1351 * Handle the case where someone closed the file (via its file
1352 * descriptor) while we were blocked. The end result should look
1353 * like opening the file succeeded but it was immediately closed.
1354 */
1355 if (fp->f_count == 1) {
0679adc4 1356 KASSERT(fdp->fd_files[indx].fp != fp,
984263bc 1357 ("Open file descriptor lost all refs"));
fad57d0e 1358 vrele(vp);
87de5057 1359 fo_close(fp);
9f87144f 1360 fdrop(fp);
9697c509 1361 *res = indx;
984263bc
MD
1362 return 0;
1363 }
1364
984263bc
MD
1365 if (flags & (O_EXLOCK | O_SHLOCK)) {
1366 lf.l_whence = SEEK_SET;
1367 lf.l_start = 0;
1368 lf.l_len = 0;
1369 if (flags & O_EXLOCK)
1370 lf.l_type = F_WRLCK;
1371 else
1372 lf.l_type = F_RDLCK;
71c18fe3
MD
1373 if (flags & FNONBLOCK)
1374 type = 0;
1375 else
1376 type = F_WAIT;
fad57d0e 1377
984263bc
MD
1378 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1379 /*
1380 * lock request failed. Normally close the descriptor
1381 * but handle the case where someone might have dup()d
1382 * it when we weren't looking. One reference is
1383 * owned by the descriptor array, the other by us.
1384 */
fad57d0e 1385 vrele(vp);
fa541be6 1386 fdealloc(p, fp, indx);
9f87144f 1387 fdrop(fp);
984263bc
MD
1388 return (error);
1389 }
984263bc
MD
1390 fp->f_flag |= FHASLOCK;
1391 }
7540ab49
MD
1392
1393#if 0
1394 /*
1395 * Assert that all regular file vnodes were created with a object.
1396 */
1397 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1398 ("open: regular file has no backing object after vn_open"));
1399#endif
984263bc 1400
fad57d0e 1401 vrele(vp);
21739618 1402
984263bc
MD
1403 /*
1404 * release our private reference, leaving the one associated with the
1405 * descriptor table intact.
1406 */
9f87144f 1407 fdrop(fp);
9697c509 1408 *res = indx;
984263bc
MD
1409 return (0);
1410}
1411
984263bc 1412/*
9697c509 1413 * open_args(char *path, int flags, int mode)
41c20dac 1414 *
9697c509
DRJ
1415 * Check permissions, allocate an open file structure,
1416 * and call the device open routine if any.
984263bc 1417 */
984263bc 1418int
9697c509 1419open(struct open_args *uap)
984263bc 1420{
fad57d0e 1421 struct nlookupdata nd;
9697c509
DRJ
1422 int error;
1423
fad57d0e
MD
1424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1425 if (error == 0) {
1426 error = kern_open(&nd, uap->flags,
1427 uap->mode, &uap->sysmsg_result);
1428 }
1429 nlookup_done(&nd);
9697c509 1430 return (error);
984263bc 1431}
984263bc 1432
984263bc 1433int
fad57d0e 1434kern_mknod(struct nlookupdata *nd, int mode, int dev)
984263bc 1435{
fad57d0e 1436 struct namecache *ncp;
dadab5e9
MD
1437 struct thread *td = curthread;
1438 struct proc *p = td->td_proc;
41c20dac 1439 struct vnode *vp;
984263bc
MD
1440 struct vattr vattr;
1441 int error;
1442 int whiteout = 0;
984263bc 1443
dadab5e9
MD
1444 KKASSERT(p);
1445
9697c509 1446 switch (mode & S_IFMT) {
984263bc
MD
1447 case S_IFCHR:
1448 case S_IFBLK:
dadab5e9 1449 error = suser(td);
984263bc
MD
1450 break;
1451 default:
dadab5e9 1452 error = suser_cred(p->p_ucred, PRISON_ROOT);
984263bc
MD
1453 break;
1454 }
1455 if (error)
1456 return (error);
fad57d0e 1457
984263bc 1458 bwillwrite();
fad57d0e
MD
1459 nd->nl_flags |= NLC_CREATE;
1460 if ((error = nlookup(nd)) != 0)
984263bc 1461 return (error);
fad57d0e
MD
1462 ncp = nd->nl_ncp;
1463 if (ncp->nc_vp)
1464 return (EEXIST);
984263bc 1465
fad57d0e
MD
1466 VATTR_NULL(&vattr);
1467 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1468 vattr.va_rdev = dev;
1469 whiteout = 0;
1470
1471 switch (mode & S_IFMT) {
1472 case S_IFMT: /* used by badsect to flag bad sectors */
1473 vattr.va_type = VBAD;
1474 break;
1475 case S_IFCHR:
1476 vattr.va_type = VCHR;
1477 break;
1478 case S_IFBLK:
1479 vattr.va_type = VBLK;
1480 break;
1481 case S_IFWHT:
1482 whiteout = 1;
1483 break;
1484 default:
1485 error = EINVAL;
1486 break;
984263bc 1487 }
9697c509 1488 if (error == 0) {
fad57d0e
MD
1489 if (whiteout) {
1490 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1491 } else {
1492 vp = NULL;
1493 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
984263bc 1494 if (error == 0)
fad57d0e 1495 vput(vp);
984263bc 1496 }
984263bc 1497 }
9697c509
DRJ
1498 return (error);
1499}
1500
1501/*
1502 * mknod_args(char *path, int mode, int dev)
1503 *
1504 * Create a special file.
1505 */
1506int
1507mknod(struct mknod_args *uap)
1508{
fad57d0e 1509 struct nlookupdata nd;
9697c509
DRJ
1510 int error;
1511
fad57d0e
MD
1512 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1513 if (error == 0)
1514 error = kern_mknod(&nd, uap->mode, uap->dev);
1515 nlookup_done(&nd);
984263bc
MD
1516 return (error);
1517}
1518
984263bc 1519int
fad57d0e 1520kern_mkfifo(struct nlookupdata *nd, int mode)
984263bc 1521{
fad57d0e 1522 struct namecache *ncp;
dadab5e9
MD
1523 struct thread *td = curthread;
1524 struct proc *p = td->td_proc;
984263bc 1525 struct vattr vattr;
fad57d0e 1526 struct vnode *vp;
984263bc 1527 int error;
984263bc
MD
1528
1529 bwillwrite();
fad57d0e
MD
1530
1531 nd->nl_flags |= NLC_CREATE;
1532 if ((error = nlookup(nd)) != 0)
984263bc 1533 return (error);
fad57d0e
MD
1534 ncp = nd->nl_ncp;
1535 if (ncp->nc_vp)
984263bc 1536 return (EEXIST);
fad57d0e 1537
984263bc
MD
1538 VATTR_NULL(&vattr);
1539 vattr.va_type = VFIFO;
136178b3 1540 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
fad57d0e
MD
1541 vp = NULL;
1542 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
984263bc 1543 if (error == 0)
fad57d0e 1544 vput(vp);
136178b3
DRJ
1545 return (error);
1546}
1547
1548/*
1549 * mkfifo_args(char *path, int mode)
1550 *
1551 * Create a named pipe.
1552 */
1553int
1554mkfifo(struct mkfifo_args *uap)
1555{
fad57d0e 1556 struct nlookupdata nd;
136178b3
DRJ
1557 int error;
1558
fad57d0e
MD
1559 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1560 if (error == 0)
1561 error = kern_mkfifo(&nd, uap->mode);
1562 nlookup_done(&nd);
984263bc
MD
1563 return (error);
1564}
1565
8c40f81e
MD
1566static int hardlink_check_uid = 0;
1567SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1568 &hardlink_check_uid, 0,
1569 "Unprivileged processes cannot create hard links to files owned by other "
1570 "users");
1571static int hardlink_check_gid = 0;
1572SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1573 &hardlink_check_gid, 0,
1574 "Unprivileged processes cannot create hard links to files owned by other "
1575 "groups");
1576
1577static int
1578can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1579{
1580 struct vattr va;
1581 int error;
1582
1583 /*
1584 * Shortcut if disabled
1585 */
1586 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1587 return (0);
1588
1589 /*
1590 * root cred can always hardlink
1591 */
1592 if (suser_cred(cred, PRISON_ROOT) == 0)
1593 return (0);
1594
1595 /*
1596 * Otherwise only if the originating file is owned by the
1597 * same user or group. Note that any group is allowed if
1598 * the file is owned by the caller.
1599 */
87de5057 1600 error = VOP_GETATTR(vp, &va);
8c40f81e
MD
1601 if (error != 0)
1602 return (error);
1603
1604 if (hardlink_check_uid) {
1605 if (cred->cr_uid != va.va_uid)
1606 return (EPERM);
1607 }
1608
1609 if (hardlink_check_gid) {
1610 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1611 return (EPERM);
1612 }
1613
1614 return (0);
1615}
1616
984263bc 1617int
fad57d0e 1618kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
984263bc 1619{
dadab5e9 1620 struct thread *td = curthread;
41c20dac 1621 struct vnode *vp;
984263bc
MD
1622 int error;
1623
fad57d0e
MD
1624 /*
1625 * Lookup the source and obtained a locked vnode.
1626 *
1627 * XXX relookup on vget failure / race ?
1628 */
984263bc 1629 bwillwrite();
fad57d0e 1630 if ((error = nlookup(nd)) != 0)
984263bc 1631 return (error);
fad57d0e
MD
1632 vp = nd->nl_ncp->nc_vp;
1633 KKASSERT(vp != NULL);
984263bc 1634 if (vp->v_type == VDIR)
fad57d0e 1635 return (EPERM); /* POSIX */
87de5057 1636 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
fad57d0e
MD
1637 return (error);
1638
1639 /*
1640 * Unlock the source so we can lookup the target without deadlocking
1641 * (XXX vp is locked already, possible other deadlock?). The target
1642 * must not exist.
1643 */
1644 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1645 nd->nl_flags &= ~NLC_NCPISLOCKED;
1646 cache_unlock(nd->nl_ncp);
1647
1648 linknd->nl_flags |= NLC_CREATE;
1649 if ((error = nlookup(linknd)) != 0) {
1650 vput(vp);
1651 return (error);
984263bc 1652 }
fad57d0e
MD
1653 if (linknd->nl_ncp->nc_vp) {
1654 vput(vp);
1655 return (EEXIST);
1656 }
1657
1658 /*
1659 * Finally run the new API VOP.
1660 */
8c40f81e
MD
1661 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1662 if (error == 0)
1663 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
fad57d0e 1664 vput(vp);
984263bc
MD
1665 return (error);
1666}
1667
1668/*
9697c509 1669 * link_args(char *path, char *link)
41c20dac 1670 *
9697c509 1671 * Make a hard file link.
984263bc 1672 */
984263bc 1673int
9697c509
DRJ
1674link(struct link_args *uap)
1675{
fad57d0e 1676 struct nlookupdata nd, linknd;
9697c509
DRJ
1677 int error;
1678
fad57d0e
MD
1679 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1680 if (error == 0) {
1681 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1682 if (error == 0)
1683 error = kern_link(&nd, &linknd);
1684 nlookup_done(&linknd);
1685 }
1686 nlookup_done(&nd);
9697c509
DRJ
1687 return (error);
1688}
1689
1690int
fad57d0e 1691kern_symlink(struct nlookupdata *nd, char *path, int mode)
984263bc 1692{
fad57d0e 1693 struct namecache *ncp;
984263bc 1694 struct vattr vattr;
fad57d0e 1695 struct vnode *vp;
984263bc 1696 int error;
984263bc 1697
984263bc 1698 bwillwrite();
fad57d0e
MD
1699 nd->nl_flags |= NLC_CREATE;
1700 if ((error = nlookup(nd)) != 0)
9697c509 1701 return (error);
fad57d0e
MD
1702 ncp = nd->nl_ncp;
1703 if (ncp->nc_vp)
9697c509 1704 return (EEXIST);
fad57d0e 1705
984263bc 1706 VATTR_NULL(&vattr);
fad57d0e
MD
1707 vattr.va_mode = mode;
1708 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
984263bc 1709 if (error == 0)
fad57d0e 1710 vput(vp);
9697c509
DRJ
1711 return (error);
1712}
1713
1714/*
1715 * symlink(char *path, char *link)
1716 *
1717 * Make a symbolic link.
1718 */
1719int
1720symlink(struct symlink_args *uap)
1721{
1722 struct thread *td = curthread;
fad57d0e 1723 struct nlookupdata nd;
9697c509
DRJ
1724 char *path;
1725 int error;
fad57d0e 1726 int mode;
9697c509
DRJ
1727
1728 path = zalloc(namei_zone);
1729 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
e24b948e 1730 if (error == 0) {
fad57d0e
MD
1731 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1732 if (error == 0) {
1733 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1734 error = kern_symlink(&nd, path, mode);
1735 }
1736 nlookup_done(&nd);
e24b948e 1737 }
984263bc
MD
1738 zfree(namei_zone, path);
1739 return (error);
1740}
1741
1742/*
41c20dac
MD
1743 * undelete_args(char *path)
1744 *
984263bc
MD
1745 * Delete a whiteout from the filesystem.
1746 */
1747/* ARGSUSED */
1748int
41c20dac 1749undelete(struct undelete_args *uap)
984263bc 1750{
fad57d0e 1751 struct nlookupdata nd;
984263bc 1752 int error;
984263bc 1753
ab2eb4eb 1754 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
984263bc 1755 bwillwrite();
fad57d0e
MD
1756 nd.nl_flags |= NLC_DELETE;
1757 if (error == 0)
1758 error = nlookup(&nd);
b8997912 1759 if (error == 0)
fad57d0e
MD
1760 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1761 nlookup_done(&nd);
984263bc
MD
1762 return (error);
1763}
1764
984263bc 1765int
fad57d0e 1766kern_unlink(struct nlookupdata *nd)
984263bc 1767{
fad57d0e 1768 struct namecache *ncp;
984263bc 1769 int error;
984263bc
MD
1770
1771 bwillwrite();
fad57d0e
MD
1772 nd->nl_flags |= NLC_DELETE;
1773 if ((error = nlookup(nd)) != 0)
984263bc 1774 return (error);
fad57d0e
MD
1775 ncp = nd->nl_ncp;
1776 error = VOP_NREMOVE(ncp, nd->nl_cred);
984263bc
MD
1777 return (error);
1778}
1779
1780/*
9697c509 1781 * unlink_args(char *path)
41c20dac 1782 *
9697c509 1783 * Delete a name from the filesystem.
984263bc 1784 */
984263bc 1785int
9697c509
DRJ
1786unlink(struct unlink_args *uap)
1787{
fad57d0e 1788 struct nlookupdata nd;
9697c509
DRJ
1789 int error;
1790
fad57d0e
MD
1791 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1792 if (error == 0)
1793 error = kern_unlink(&nd);
1794 nlookup_done(&nd);
9697c509
DRJ
1795 return (error);
1796}
1797
1798int
5a3fe67d 1799kern_lseek(int fd, off_t offset, int whence, off_t *res)
984263bc 1800{
dadab5e9
MD
1801 struct thread *td = curthread;
1802 struct proc *p = td->td_proc;
41c20dac
MD
1803 struct filedesc *fdp = p->p_fd;
1804 struct file *fp;
984263bc
MD
1805 struct vattr vattr;
1806 int error;
1807
b463648b 1808 if ((u_int)fd >= fdp->fd_nfiles ||
0679adc4 1809 (fp = fdp->fd_files[fd].fp) == NULL)
984263bc
MD
1810 return (EBADF);
1811 if (fp->f_type != DTYPE_VNODE)
1812 return (ESPIPE);
9697c509 1813 switch (whence) {
984263bc 1814 case L_INCR:
9697c509 1815 fp->f_offset += offset;
984263bc
MD
1816 break;
1817 case L_XTND:
87de5057 1818 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
984263bc
MD
1819 if (error)
1820 return (error);
9697c509 1821 fp->f_offset = offset + vattr.va_size;
984263bc
MD
1822 break;
1823 case L_SET:
9697c509 1824 fp->f_offset = offset;
984263bc
MD
1825 break;
1826 default:
1827 return (EINVAL);
1828 }
9697c509 1829 *res = fp->f_offset;
984263bc
MD
1830 return (0);
1831}
1832
984263bc 1833/*
9697c509 1834 * lseek_args(int fd, int pad, off_t offset, int whence)
41c20dac 1835 *
9697c509 1836 * Reposition read/write file offset.
984263bc 1837 */
984263bc 1838int
9697c509 1839lseek(struct lseek_args *uap)
984263bc 1840{
984263bc
MD
1841 int error;
1842
9697c509 1843 error = kern_lseek(uap->fd, uap->offset, uap->whence,
5a3fe67d 1844 &uap->sysmsg_offset);
9697c509 1845
984263bc
MD
1846 return (error);
1847}
984263bc 1848
984263bc 1849int
fad57d0e 1850kern_access(struct nlookupdata *nd, int aflags)
984263bc 1851{
41c20dac 1852 struct vnode *vp;
984263bc 1853 int error, flags;
984263bc 1854
fad57d0e
MD
1855 if ((error = nlookup(nd)) != 0)
1856 return (error);
806dcf9a 1857retry:
fad57d0e 1858 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 1859 if (error)
fad57d0e 1860 return (error);
984263bc
MD
1861
1862 /* Flags == 0 means only check for existence. */
9697c509 1863 if (aflags) {
984263bc 1864 flags = 0;
9697c509 1865 if (aflags & R_OK)
984263bc 1866 flags |= VREAD;
9697c509 1867 if (aflags & W_OK)
984263bc 1868 flags |= VWRITE;
9697c509 1869 if (aflags & X_OK)
984263bc
MD
1870 flags |= VEXEC;
1871 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
87de5057 1872 error = VOP_ACCESS(vp, flags, nd->nl_cred);
806dcf9a
MD
1873
1874 /*
1875 * If the file handle is stale we have to re-resolve the
1876 * entry. This is a hack at the moment.
1877 */
1878 if (error == ESTALE) {
1879 cache_setunresolved(nd->nl_ncp);
1880 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1881 if (error == 0) {
1882 vput(vp);
1883 vp = NULL;
1884 goto retry;
1885 }
1886 }
984263bc 1887 }
984263bc 1888 vput(vp);
984263bc
MD
1889 return (error);
1890}
1891
984263bc 1892/*
9697c509 1893 * access_args(char *path, int flags)
41c20dac 1894 *
9697c509 1895 * Check access permissions.
984263bc 1896 */
984263bc 1897int
9697c509 1898access(struct access_args *uap)
984263bc 1899{
fad57d0e 1900 struct nlookupdata nd;
9697c509 1901 int error;
984263bc 1902
fad57d0e
MD
1903 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1904 if (error == 0)
1905 error = kern_access(&nd, uap->flags);
1906 nlookup_done(&nd);
984263bc
MD
1907 return (error);
1908}
1909
984263bc 1910int
21739618 1911kern_stat(struct nlookupdata *nd, struct stat *st)
984263bc 1912{
984263bc 1913 int error;
21739618
MD
1914 struct vnode *vp;
1915 thread_t td;
984263bc 1916
21739618 1917 if ((error = nlookup(nd)) != 0)
984263bc 1918 return (error);
806dcf9a 1919again:
21739618
MD
1920 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1921 return (ENOENT);
1922
1923 td = curthread;
87de5057 1924 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 1925 return (error);
87de5057 1926 error = vn_stat(vp, st, nd->nl_cred);
806dcf9a
MD
1927
1928 /*
1929 * If the file handle is stale we have to re-resolve the entry. This
1930 * is a hack at the moment.
1931 */
1932 if (error == ESTALE) {
1933 cache_setunresolved(nd->nl_ncp);
1934 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1935 if (error == 0) {
1936 vput(vp);
1937 goto again;
1938 }
1939 }
21739618 1940 vput(vp);
984263bc
MD
1941 return (error);
1942}
1943
984263bc 1944/*
41c20dac
MD
1945 * stat_args(char *path, struct stat *ub)
1946 *
984263bc
MD
1947 * Get file status; this version follows links.
1948 */
984263bc 1949int
41c20dac 1950stat(struct stat_args *uap)
984263bc 1951{
21739618 1952 struct nlookupdata nd;
9697c509
DRJ
1953 struct stat st;
1954 int error;
984263bc 1955
21739618
MD
1956 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1957 if (error == 0) {
1958 error = kern_stat(&nd, &st);
1959 if (error == 0)
1960 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 1961 }
fad57d0e 1962 nlookup_done(&nd);
984263bc
MD
1963 return (error);
1964}
1965
1966/*
41c20dac
MD
1967 * lstat_args(char *path, struct stat *ub)
1968 *
984263bc
MD
1969 * Get file status; this version does not follow links.
1970 */
984263bc 1971int
41c20dac 1972lstat(struct lstat_args *uap)
984263bc 1973{
21739618 1974 struct nlookupdata nd;
9697c509
DRJ
1975 struct stat st;
1976 int error;
984263bc 1977
21739618
MD
1978 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1979 if (error == 0) {
1980 error = kern_stat(&nd, &st);
1981 if (error == 0)
1982 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 1983 }
fad57d0e 1984 nlookup_done(&nd);
984263bc
MD
1985 return (error);
1986}
1987
984263bc 1988/*
41c20dac
MD
1989 * pathconf_Args(char *path, int name)
1990 *
984263bc
MD
1991 * Get configurable pathname variables.
1992 */
984263bc
MD
1993/* ARGSUSED */
1994int
41c20dac 1995pathconf(struct pathconf_args *uap)
984263bc 1996{
fad57d0e
MD
1997 struct nlookupdata nd;
1998 struct vnode *vp;
984263bc 1999 int error;
984263bc 2000
fad57d0e 2001 vp = NULL;
ab2eb4eb 2002 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2003 if (error == 0)
2004 error = nlookup(&nd);
2005 if (error == 0)
2006 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2007 nlookup_done(&nd);
2008 if (error == 0) {
ab2eb4eb 2009 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
fad57d0e
MD
2010 vput(vp);
2011 }
984263bc
MD
2012 return (error);
2013}
2014
bbab27f6
DRJ
2015/*
2016 * XXX: daver
2017 * kern_readlink isn't properly split yet. There is a copyin burried
2018 * in VOP_READLINK().
2019 */
984263bc 2020int
fad57d0e 2021kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
984263bc 2022{
dadab5e9
MD
2023 struct thread *td = curthread;
2024 struct proc *p = td->td_proc;
41c20dac 2025 struct vnode *vp;
984263bc
MD
2026 struct iovec aiov;
2027 struct uio auio;
2028 int error;
984263bc 2029
fad57d0e
MD
2030 if ((error = nlookup(nd)) != 0)
2031 return (error);
2032 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2033 if (error)
984263bc 2034 return (error);
fad57d0e 2035 if (vp->v_type != VLNK) {
984263bc 2036 error = EINVAL;
fad57d0e 2037 } else {
9697c509
DRJ
2038 aiov.iov_base = buf;
2039 aiov.iov_len = count;
984263bc
MD
2040 auio.uio_iov = &aiov;
2041 auio.uio_iovcnt = 1;
2042 auio.uio_offset = 0;
2043 auio.uio_rw = UIO_READ;
2044 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 2045 auio.uio_td = td;
9697c509 2046 auio.uio_resid = count;
984263bc
MD
2047 error = VOP_READLINK(vp, &auio, p->p_ucred);
2048 }
2049 vput(vp);
9697c509
DRJ
2050 *res = count - auio.uio_resid;
2051 return (error);
2052}
2053
2054/*
2055 * readlink_args(char *path, char *buf, int count)
2056 *
2057 * Return target name of a symbolic link.
2058 */
2059int
2060readlink(struct readlink_args *uap)
2061{
fad57d0e 2062 struct nlookupdata nd;
9697c509
DRJ
2063 int error;
2064
fad57d0e
MD
2065 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2066 if (error == 0) {
2067 error = kern_readlink(&nd, uap->buf, uap->count,
2068 &uap->sysmsg_result);
2069 }
2070 nlookup_done(&nd);
984263bc
MD
2071 return (error);
2072}
2073
2074static int
41c20dac 2075setfflags(struct vnode *vp, int flags)
984263bc 2076{
dadab5e9
MD
2077 struct thread *td = curthread;
2078 struct proc *p = td->td_proc;
984263bc
MD
2079 int error;
2080 struct vattr vattr;
2081
2082 /*
2083 * Prevent non-root users from setting flags on devices. When
2084 * a device is reused, users can retain ownership of the device
2085 * if they are allowed to set flags and programs assume that
2086 * chown can't fail when done as root.
2087 */
2088 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
dadab5e9 2089 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
984263bc
MD
2090 return (error);
2091
186fccd6
MD
2092 /*
2093 * note: vget is required for any operation that might mod the vnode
2094 * so VINACTIVE is properly cleared.
2095 */
87de5057 2096 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2097 VATTR_NULL(&vattr);
2098 vattr.va_flags = flags;
87de5057 2099 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2100 vput(vp);
2101 }
984263bc
MD
2102 return (error);
2103}
2104
2105/*
41c20dac
MD
2106 * chflags(char *path, int flags)
2107 *
984263bc
MD
2108 * Change flags of a file given a path name.
2109 */
984263bc
MD
2110/* ARGSUSED */
2111int
41c20dac 2112chflags(struct chflags_args *uap)
984263bc 2113{
fad57d0e
MD
2114 struct nlookupdata nd;
2115 struct vnode *vp;
984263bc 2116 int error;
984263bc 2117
fad57d0e 2118 vp = NULL;
ab2eb4eb 2119 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2120 /* XXX Add NLC flag indicating modifying operation? */
2121 if (error == 0)
2122 error = nlookup(&nd);
2123 if (error == 0)
2124 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2125 nlookup_done(&nd);
2126 if (error == 0) {
ab2eb4eb 2127 error = setfflags(vp, uap->flags);
fad57d0e
MD
2128 vrele(vp);
2129 }
2130 return (error);
984263bc
MD
2131}
2132
2133/*
41c20dac
MD
2134 * fchflags_args(int fd, int flags)
2135 *
984263bc
MD
2136 * Change flags of a file given a file descriptor.
2137 */
984263bc
MD
2138/* ARGSUSED */
2139int
41c20dac 2140fchflags(struct fchflags_args *uap)
984263bc 2141{
dadab5e9
MD
2142 struct thread *td = curthread;
2143 struct proc *p = td->td_proc;
984263bc
MD
2144 struct file *fp;
2145 int error;
2146
ab2eb4eb 2147 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2148 return (error);
ab2eb4eb 2149 return setfflags((struct vnode *) fp->f_data, uap->flags);
984263bc
MD
2150}
2151
2152static int
41c20dac 2153setfmode(struct vnode *vp, int mode)
984263bc 2154{
dadab5e9
MD
2155 struct thread *td = curthread;
2156 struct proc *p = td->td_proc;
984263bc
MD
2157 int error;
2158 struct vattr vattr;
2159
186fccd6
MD
2160 /*
2161 * note: vget is required for any operation that might mod the vnode
2162 * so VINACTIVE is properly cleared.
2163 */
87de5057 2164 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2165 VATTR_NULL(&vattr);
2166 vattr.va_mode = mode & ALLPERMS;
87de5057 2167 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2168 vput(vp);
2169 }
984263bc
MD
2170 return error;
2171}
2172
9697c509 2173int
fad57d0e 2174kern_chmod(struct nlookupdata *nd, int mode)
9697c509 2175{
fad57d0e 2176 struct vnode *vp;
9697c509
DRJ
2177 int error;
2178
fad57d0e
MD
2179 /* XXX Add NLC flag indicating modifying operation? */
2180 if ((error = nlookup(nd)) != 0)
9697c509 2181 return (error);
fad57d0e
MD
2182 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2183 return (error);
2184 error = setfmode(vp, mode);
2185 vrele(vp);
2186 return (error);
9697c509
DRJ
2187}
2188
984263bc 2189/*
41c20dac
MD
2190 * chmod_args(char *path, int mode)
2191 *
984263bc
MD
2192 * Change mode of a file given path name.
2193 */
984263bc
MD
2194/* ARGSUSED */
2195int
41c20dac 2196chmod(struct chmod_args *uap)
984263bc 2197{
fad57d0e 2198 struct nlookupdata nd;
9697c509 2199 int error;
984263bc 2200
fad57d0e
MD
2201 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2202 if (error == 0)
2203 error = kern_chmod(&nd, uap->mode);
2204 nlookup_done(&nd);
9697c509 2205 return (error);
984263bc
MD
2206}
2207
2208/*
41c20dac
MD
2209 * lchmod_args(char *path, int mode)
2210 *
984263bc
MD
2211 * Change mode of a file given path name (don't follow links.)
2212 */
984263bc
MD
2213/* ARGSUSED */
2214int
41c20dac 2215lchmod(struct lchmod_args *uap)
984263bc 2216{
fad57d0e 2217 struct nlookupdata nd;
984263bc 2218 int error;
984263bc 2219
fad57d0e
MD
2220 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2221 if (error == 0)
2222 error = kern_chmod(&nd, uap->mode);
2223 nlookup_done(&nd);
2224 return (error);
984263bc
MD
2225}
2226
2227/*
41c20dac
MD
2228 * fchmod_args(int fd, int mode)
2229 *
984263bc
MD
2230 * Change mode of a file given a file descriptor.
2231 */
984263bc
MD
2232/* ARGSUSED */
2233int
41c20dac 2234fchmod(struct fchmod_args *uap)
984263bc 2235{
dadab5e9
MD
2236 struct thread *td = curthread;
2237 struct proc *p = td->td_proc;
984263bc
MD
2238 struct file *fp;
2239 int error;
2240
ab2eb4eb 2241 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2242 return (error);
ab2eb4eb 2243 return setfmode((struct vnode *)fp->f_data, uap->mode);
984263bc
MD
2244}
2245
2246static int
41c20dac 2247setfown(struct vnode *vp, uid_t uid, gid_t gid)
984263bc 2248{
dadab5e9
MD
2249 struct thread *td = curthread;
2250 struct proc *p = td->td_proc;
984263bc
MD
2251 int error;
2252 struct vattr vattr;
2253
186fccd6
MD
2254 /*
2255 * note: vget is required for any operation that might mod the vnode
2256 * so VINACTIVE is properly cleared.
2257 */
87de5057 2258 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2259 VATTR_NULL(&vattr);
2260 vattr.va_uid = uid;
2261 vattr.va_gid = gid;
87de5057 2262 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2263 vput(vp);
2264 }
984263bc
MD
2265 return error;
2266}
2267
9697c509 2268int
fad57d0e 2269kern_chown(struct nlookupdata *nd, int uid, int gid)
9697c509 2270{
fad57d0e 2271 struct vnode *vp;
9697c509
DRJ
2272 int error;
2273
fad57d0e
MD
2274 /* XXX Add NLC flag indicating modifying operation? */
2275 if ((error = nlookup(nd)) != 0)
9697c509 2276 return (error);
fad57d0e
MD
2277 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2278 return (error);
2279 error = setfown(vp, uid, gid);
2280 vrele(vp);
9697c509
DRJ
2281 return (error);
2282}
2283
984263bc 2284/*
41c20dac
MD
2285 * chown(char *path, int uid, int gid)
2286 *
984263bc
MD
2287 * Set ownership given a path name.
2288 */
984263bc 2289int
41c20dac 2290chown(struct chown_args *uap)
984263bc 2291{
fad57d0e 2292 struct nlookupdata nd;
9697c509
DRJ
2293 int error;
2294
fad57d0e
MD
2295 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2296 if (error == 0)
2297 error = kern_chown(&nd, uap->uid, uap->gid);
2298 nlookup_done(&nd);
984263bc
MD
2299 return (error);
2300}
2301
2302/*
41c20dac
MD
2303 * lchown_args(char *path, int uid, int gid)
2304 *
984263bc
MD
2305 * Set ownership given a path name, do not cross symlinks.
2306 */
984263bc 2307int
41c20dac 2308lchown(struct lchown_args *uap)
984263bc 2309{
fad57d0e 2310 struct nlookupdata nd;
984263bc 2311 int error;
9697c509 2312
fad57d0e
MD
2313 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2314 if (error == 0)
2315 error = kern_chown(&nd, uap->uid, uap->gid);
2316 nlookup_done(&nd);
984263bc
MD
2317 return (error);
2318}
2319
2320/*
41c20dac
MD
2321 * fchown_args(int fd, int uid, int gid)
2322 *
984263bc
MD
2323 * Set ownership given a file descriptor.
2324 */
984263bc
MD
2325/* ARGSUSED */
2326int
41c20dac 2327fchown(struct fchown_args *uap)
984263bc 2328{
dadab5e9
MD
2329 struct thread *td = curthread;
2330 struct proc *p = td->td_proc;
984263bc
MD
2331 struct file *fp;
2332 int error;
2333
ab2eb4eb 2334 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2335 return (error);
41c20dac 2336 return setfown((struct vnode *)fp->f_data,
ab2eb4eb 2337 uap->uid, uap->gid);
984263bc
MD
2338}
2339
2340static int
9697c509 2341getutimes(const struct timeval *tvp, struct timespec *tsp)
984263bc
MD
2342{
2343 struct timeval tv[2];
984263bc 2344
9697c509 2345 if (tvp == NULL) {
984263bc
MD
2346 microtime(&tv[0]);
2347 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2348 tsp[1] = tsp[0];
2349 } else {
9697c509
DRJ
2350 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2351 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
984263bc
MD
2352 }
2353 return 0;
2354}
2355
2356static int
41c20dac 2357setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
984263bc 2358{
dadab5e9
MD
2359 struct thread *td = curthread;
2360 struct proc *p = td->td_proc;
984263bc
MD
2361 int error;
2362 struct vattr vattr;
2363
186fccd6
MD
2364 /*
2365 * note: vget is required for any operation that might mod the vnode
2366 * so VINACTIVE is properly cleared.
2367 */
87de5057 2368 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2369 VATTR_NULL(&vattr);
2370 vattr.va_atime = ts[0];
2371 vattr.va_mtime = ts[1];
2372 if (nullflag)
2373 vattr.va_vaflags |= VA_UTIMES_NULL;
87de5057 2374 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2375 vput(vp);
2376 }
984263bc
MD
2377 return error;
2378}
2379
9697c509 2380int
fad57d0e 2381kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
9697c509
DRJ
2382{
2383 struct timespec ts[2];
fad57d0e 2384 struct vnode *vp;
9697c509
DRJ
2385 int error;
2386
fad57d0e 2387 if ((error = getutimes(tptr, ts)) != 0)
9697c509 2388 return (error);
fad57d0e
MD
2389 /* XXX Add NLC flag indicating modifying operation? */
2390 if ((error = nlookup(nd)) != 0)
9697c509 2391 return (error);
fad57d0e
MD
2392 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2393 return (error);
2394 error = setutimes(vp, ts, tptr == NULL);
2395 vrele(vp);
9697c509
DRJ
2396 return (error);
2397}
2398
984263bc 2399/*
41c20dac
MD
2400 * utimes_args(char *path, struct timeval *tptr)
2401 *
984263bc
MD
2402 * Set the access and modification times of a file.
2403 */
984263bc 2404int
41c20dac 2405utimes(struct utimes_args *uap)
984263bc 2406{
9697c509 2407 struct timeval tv[2];
fad57d0e 2408 struct nlookupdata nd;
9697c509
DRJ
2409 int error;
2410
2411 if (uap->tptr) {
2412 error = copyin(uap->tptr, tv, sizeof(tv));
2413 if (error)
2414 return (error);
2415 }
fad57d0e
MD
2416 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2417 if (error == 0)
2418 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2419 nlookup_done(&nd);
984263bc
MD
2420 return (error);
2421}
2422
2423/*
41c20dac
MD
2424 * lutimes_args(char *path, struct timeval *tptr)
2425 *
984263bc
MD
2426 * Set the access and modification times of a file.
2427 */
984263bc 2428int
41c20dac 2429lutimes(struct lutimes_args *uap)
984263bc 2430{
9697c509 2431 struct timeval tv[2];
fad57d0e 2432 struct nlookupdata nd;
9697c509
DRJ
2433 int error;
2434
2435 if (uap->tptr) {
2436 error = copyin(uap->tptr, tv, sizeof(tv));
2437 if (error)
2438 return (error);
2439 }
fad57d0e
MD
2440 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2441 if (error == 0)
2442 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2443 nlookup_done(&nd);
984263bc
MD
2444 return (error);
2445}
2446
984263bc 2447int
9697c509 2448kern_futimes(int fd, struct timeval *tptr)
984263bc 2449{
dadab5e9
MD
2450 struct thread *td = curthread;
2451 struct proc *p = td->td_proc;
984263bc
MD
2452 struct timespec ts[2];
2453 struct file *fp;
984263bc
MD
2454 int error;
2455
9697c509
DRJ
2456 error = getutimes(tptr, ts);
2457 if (error)
984263bc 2458 return (error);
9697c509
DRJ
2459 error = getvnode(p->p_fd, fd, &fp);
2460 if (error)
984263bc 2461 return (error);
9697c509
DRJ
2462 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2463 return (error);
984263bc
MD
2464}
2465
2466/*
9697c509 2467 * futimes_args(int fd, struct timeval *tptr)
41c20dac 2468 *
9697c509 2469 * Set the access and modification times of a file.
984263bc 2470 */
984263bc 2471int
9697c509
DRJ
2472futimes(struct futimes_args *uap)
2473{
2474 struct timeval tv[2];
2475 int error;
2476
2477 if (uap->tptr) {
2478 error = copyin(uap->tptr, tv, sizeof(tv));
2479 if (error)
2480 return (error);
2481 }
2482
b651c485 2483 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
9697c509
DRJ
2484
2485 return (error);
2486}
2487
2488int
fad57d0e 2489kern_truncate(struct nlookupdata *nd, off_t length)
984263bc 2490{
41c20dac 2491 struct vnode *vp;
984263bc
MD
2492 struct vattr vattr;
2493 int error;
984263bc 2494
9697c509 2495 if (length < 0)
984263bc 2496 return(EINVAL);
fad57d0e
MD
2497 /* XXX Add NLC flag indicating modifying operation? */
2498 if ((error = nlookup(nd)) != 0)
984263bc 2499 return (error);
fad57d0e
MD
2500 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2501 return (error);
ca466bae 2502 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
fad57d0e
MD
2503 vrele(vp);
2504 return (error);
2505 }
2506 if (vp->v_type == VDIR) {
984263bc 2507 error = EISDIR;
fad57d0e 2508 } else if ((error = vn_writechk(vp)) == 0 &&
87de5057 2509 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
984263bc 2510 VATTR_NULL(&vattr);
9697c509 2511 vattr.va_size = length;
87de5057 2512 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
984263bc
MD
2513 }
2514 vput(vp);
2515 return (error);
2516}
2517
9697c509
DRJ
2518/*
2519 * truncate(char *path, int pad, off_t length)
2520 *
2521 * Truncate a file given its path name.
2522 */
2523int
2524truncate(struct truncate_args *uap)
2525{
fad57d0e 2526 struct nlookupdata nd;
9697c509
DRJ
2527 int error;
2528
fad57d0e
MD
2529 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2530 if (error == 0)
2531 error = kern_truncate(&nd, uap->length);
2532 nlookup_done(&nd);
9697c509
DRJ
2533 return error;
2534}
2535
984263bc 2536int
8f6f8622 2537kern_ftruncate(int fd, off_t length)
984263bc 2538{
dadab5e9
MD
2539 struct thread *td = curthread;
2540 struct proc *p = td->td_proc;
984263bc
MD
2541 struct vattr vattr;
2542 struct vnode *vp;
2543 struct file *fp;
2544 int error;
2545
8f6f8622 2546 if (length < 0)
984263bc 2547 return(EINVAL);
8f6f8622 2548 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2549 return (error);
2550 if ((fp->f_flag & FWRITE) == 0)
2551 return (EINVAL);
2552 vp = (struct vnode *)fp->f_data;
ca466bae 2553 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
2554 if (vp->v_type == VDIR)
2555 error = EISDIR;
2556 else if ((error = vn_writechk(vp)) == 0) {
2557 VATTR_NULL(&vattr);
8f6f8622 2558 vattr.va_size = length;
87de5057 2559 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
984263bc 2560 }
ca466bae 2561 VOP_UNLOCK(vp, 0);
984263bc
MD
2562 return (error);
2563}
2564
984263bc 2565/*
8f6f8622 2566 * ftruncate_args(int fd, int pad, off_t length)
41c20dac 2567 *
8f6f8622 2568 * Truncate a file given a file descriptor.
984263bc 2569 */
984263bc 2570int
8f6f8622 2571ftruncate(struct ftruncate_args *uap)
984263bc 2572{
8f6f8622 2573 int error;
984263bc 2574
8f6f8622
DRJ
2575 error = kern_ftruncate(uap->fd, uap->length);
2576
2577 return (error);
984263bc
MD
2578}
2579
984263bc 2580/*
41c20dac
MD
2581 * fsync(int fd)
2582 *
984263bc
MD
2583 * Sync an open file.
2584 */
984263bc
MD
2585/* ARGSUSED */
2586int
41c20dac 2587fsync(struct fsync_args *uap)
984263bc 2588{
dadab5e9
MD
2589 struct thread *td = curthread;
2590 struct proc *p = td->td_proc;
41c20dac 2591 struct vnode *vp;
984263bc
MD
2592 struct file *fp;
2593 vm_object_t obj;
2594 int error;
2595
ab2eb4eb 2596 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc
MD
2597 return (error);
2598 vp = (struct vnode *)fp->f_data;
ca466bae 2599 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7540ab49 2600 if ((obj = vp->v_object) != NULL)
984263bc 2601 vm_object_page_clean(obj, 0, 0, 0);
87de5057 2602 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
984263bc
MD
2603 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2604 bioops.io_fsync)
2605 error = (*bioops.io_fsync)(vp);
ca466bae 2606 VOP_UNLOCK(vp, 0);
984263bc
MD
2607 return (error);
2608}
2609
984263bc 2610int
fad57d0e 2611kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
984263bc 2612{
fad57d0e
MD
2613 struct namecache *fncpd;
2614 struct namecache *tncpd;
2615 struct namecache *ncp;
2616 struct mount *mp;
984263bc
MD
2617 int error;
2618
2619 bwillwrite();
fad57d0e 2620 if ((error = nlookup(fromnd)) != 0)
984263bc 2621 return (error);
fad57d0e
MD
2622 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2623 return (ENOENT);
2624 cache_hold(fncpd);
2625
2626 /*
2627 * unlock the source ncp so we can lookup the target ncp without
2628 * deadlocking. The target may or may not exist so we do not check
2629 * for a target vp like kern_mkdir() and other creation functions do.
2630 *
2631 * The source and target directories are ref'd and rechecked after
2632 * everything is relocked to determine if the source or target file
2633 * has been renamed.
2634 */
2635 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2636 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2637 cache_unlock(fromnd->nl_ncp);
2638
2639 tond->nl_flags |= NLC_CREATE;
2640 if ((error = nlookup(tond)) != 0) {
2641 cache_drop(fncpd);
2642 return (error);
2643 }
2644 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2645 cache_drop(fncpd);
2646 return (ENOENT);
2647 }
2648 cache_hold(tncpd);
2649
2650 /*
2651 * If the source and target are the same there is nothing to do
2652 */
2653 if (fromnd->nl_ncp == tond->nl_ncp) {
2654 cache_drop(fncpd);
2655 cache_drop(tncpd);
2656 return (0);
2657 }
2658
2659 /*
aa40af33
MD
2660 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2661 * may have become invalid while it was unlocked, nc_vp and nc_mount
2662 * could be NULL.
fad57d0e
MD
2663 */
2664 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2665 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2666 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2667 cache_lock(fromnd->nl_ncp);
2668 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2669 } else {
2670 cache_unlock(tond->nl_ncp);
2671 cache_lock(fromnd->nl_ncp);
2672 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2673 cache_lock(tond->nl_ncp);
2674 cache_resolve(tond->nl_ncp, tond->nl_cred);
2675 }
2676 fromnd->nl_flags |= NLC_NCPISLOCKED;
2677
2678 /*
2679 * make sure the parent directories linkages are the same
2680 */
2681 if (fncpd != fromnd->nl_ncp->nc_parent ||
2682 tncpd != tond->nl_ncp->nc_parent) {
2683 cache_drop(fncpd);
2684 cache_drop(tncpd);
2685 return (ENOENT);
2686 }
2687
2688 /*
2689 * Both the source and target must be within the same filesystem and
2690 * in the same filesystem as their parent directories within the
2691 * namecache topology.
aa40af33
MD
2692 *
2693 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
fad57d0e
MD
2694 */
2695 mp = fncpd->nc_mount;
2696 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2697 mp != tond->nl_ncp->nc_mount) {
2698 cache_drop(fncpd);
2699 cache_drop(tncpd);
2700 return (EXDEV);
2701 }
2702
2703 /*
2704 * If the target exists and either the source or target is a directory,
2705 * then both must be directories.
aa40af33
MD
2706 *
2707 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2708 * become NULL.
fad57d0e
MD
2709 */
2710 if (tond->nl_ncp->nc_vp) {
aa40af33
MD
2711 if (fromnd->nl_ncp->nc_vp == NULL) {
2712 error = ENOENT;
2713 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
fad57d0e
MD
2714 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2715 error = ENOTDIR;
2716 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
984263bc 2717 error = EISDIR;
984263bc
MD
2718 }
2719 }
fad57d0e 2720
984263bc 2721 /*
fad57d0e
MD
2722 * You cannot rename a source into itself or a subdirectory of itself.
2723 * We check this by travsersing the target directory upwards looking
2724 * for a match against the source.
984263bc 2725 */
fad57d0e
MD
2726 if (error == 0) {
2727 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2728 if (fromnd->nl_ncp == ncp) {
2729 error = EINVAL;
2730 break;
2731 }
984263bc 2732 }
fad57d0e
MD
2733 }
2734
2735 cache_drop(fncpd);
2736 cache_drop(tncpd);
bc8fd9e0
MD
2737
2738 /*
2739 * Even though the namespaces are different, they may still represent
2740 * hardlinks to the same file. The filesystem might have a hard time
2741 * with this so we issue a NREMOVE of the source instead of a NRENAME
2742 * when we detect the situation.
2743 */
2744 if (error == 0) {
2745 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2746 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2747 } else {
2748 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2749 tond->nl_cred);
2750 }
2751 }
984263bc
MD
2752 return (error);
2753}
2754
2755/*
9697c509 2756 * rename_args(char *from, char *to)
41c20dac 2757 *
9697c509
DRJ
2758 * Rename files. Source and destination must either both be directories,
2759 * or both not be directories. If target is a directory, it must be empty.
984263bc 2760 */
984263bc 2761int
9697c509
DRJ
2762rename(struct rename_args *uap)
2763{
fad57d0e 2764 struct nlookupdata fromnd, tond;
9697c509
DRJ
2765 int error;
2766
fad57d0e
MD
2767 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2768 if (error == 0) {
2769 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2770 if (error == 0)
2771 error = kern_rename(&fromnd, &tond);
2772 nlookup_done(&tond);
2773 }
2774 nlookup_done(&fromnd);
9697c509
DRJ
2775 return (error);
2776}
2777
2778int
fad57d0e 2779kern_mkdir(struct nlookupdata *nd, int mode)
984263bc 2780{
dadab5e9
MD
2781 struct thread *td = curthread;
2782 struct proc *p = td->td_proc;
fad57d0e 2783 struct namecache *ncp;
41c20dac 2784 struct vnode *vp;
984263bc
MD
2785 struct vattr vattr;
2786 int error;
984263bc
MD
2787
2788 bwillwrite();
fad57d0e
MD
2789 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2790 if ((error = nlookup(nd)) != 0)
984263bc 2791 return (error);
fad57d0e
MD
2792
2793 ncp = nd->nl_ncp;
2794 if (ncp->nc_vp)
984263bc 2795 return (EEXIST);
fad57d0e 2796
984263bc
MD
2797 VATTR_NULL(&vattr);
2798 vattr.va_type = VDIR;
9697c509 2799 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
fad57d0e
MD
2800
2801 vp = NULL;
2802 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
9697c509 2803 if (error == 0)
fad57d0e 2804 vput(vp);
984263bc
MD
2805 return (error);
2806}
2807
2808/*
9697c509 2809 * mkdir_args(char *path, int mode)
41c20dac 2810 *
9697c509 2811 * Make a directory file.
984263bc 2812 */
984263bc
MD
2813/* ARGSUSED */
2814int
9697c509
DRJ
2815mkdir(struct mkdir_args *uap)
2816{
fad57d0e 2817 struct nlookupdata nd;
9697c509
DRJ
2818 int error;
2819
fad57d0e
MD
2820 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2821 if (error == 0)
2822 error = kern_mkdir(&nd, uap->mode);
2823 nlookup_done(&nd);
9697c509
DRJ
2824 return (error);
2825}
2826
2827int
fad57d0e 2828kern_rmdir(struct nlookupdata *nd)
984263bc 2829{
fad57d0e 2830 struct namecache *ncp;
984263bc 2831 int error;
984263bc
MD
2832
2833 bwillwrite();
fad57d0e
MD
2834 nd->nl_flags |= NLC_DELETE;
2835 if ((error = nlookup(nd)) != 0)
984263bc 2836 return (error);
fad57d0e
MD
2837
2838 ncp = nd->nl_ncp;
2839 error = VOP_NRMDIR(ncp, nd->nl_cred);
984263bc
MD
2840 return (error);
2841}
2842
984263bc 2843/*
9697c509 2844 * rmdir_args(char *path)
41c20dac 2845 *
9697c509 2846 * Remove a directory file.
984263bc 2847 */
9697c509 2848/* ARGSUSED */
984263bc 2849int
9697c509 2850rmdir(struct rmdir_args *uap)
984263bc 2851{
fad57d0e 2852 struct nlookupdata nd;
9697c509
DRJ
2853 int error;
2854
fad57d0e
MD
2855 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2856 if (error == 0)
2857 error = kern_rmdir(&nd);
2858 nlookup_done(&nd);
984263bc
MD
2859 return (error);
2860}
984263bc 2861
984263bc 2862int
53b02c48
JS
2863kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2864 enum uio_seg direction)
984263bc 2865{
dadab5e9
MD
2866 struct thread *td = curthread;
2867 struct proc *p = td->td_proc;
984263bc
MD
2868 struct vnode *vp;
2869 struct file *fp;
2870 struct uio auio;
2871 struct iovec aiov;
2872 long loff;
2873 int error, eofflag;
2874
9697c509 2875 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2876 return (error);
2877 if ((fp->f_flag & FREAD) == 0)
2878 return (EBADF);
2879 vp = (struct vnode *)fp->f_data;
2880unionread:
2881 if (vp->v_type != VDIR)
2882 return (EINVAL);
9697c509
DRJ
2883 aiov.iov_base = buf;
2884 aiov.iov_len = count;
984263bc
MD
2885 auio.uio_iov = &aiov;
2886 auio.uio_iovcnt = 1;
2887 auio.uio_rw = UIO_READ;
53b02c48 2888 auio.uio_segflg = direction;
dadab5e9 2889 auio.uio_td = td;
9697c509 2890 auio.uio_resid = count;
ca466bae
MD
2891 /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2892 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
2893 loff = auio.uio_offset = fp->f_offset;
2894 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2895 fp->f_offset = auio.uio_offset;
ca466bae 2896 VOP_UNLOCK(vp, 0);
984263bc
MD
2897 if (error)
2898 return (error);
9697c509 2899 if (count == auio.uio_resid) {
984263bc 2900 if (union_dircheckp) {
dadab5e9 2901 error = union_dircheckp(td, &vp, fp);
984263bc
MD
2902 if (error == -1)
2903 goto unionread;
2904 if (error)
2905 return (error);
2906 }
2907 if ((vp->v_flag & VROOT) &&
2908 (vp->v_mount->mnt_flag & MNT_UNION)) {
2909 struct vnode *tvp = vp;
2910 vp = vp->v_mount->mnt_vnodecovered;
597aea93 2911 vref(vp);
fbb4eeab 2912 fp->f_data = vp;
984263bc
MD
2913 fp->f_offset = 0;
2914 vrele(tvp);
2915 goto unionread;
2916 }
2917 }
9697c509
DRJ
2918 if (basep) {
2919 *basep = loff;
984263bc 2920 }
9697c509
DRJ
2921 *res = count - auio.uio_resid;
2922 return (error);
2923}
2924
2925/*
2926 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2927 *
2928 * Read a block of directory entries in a file system independent format.
2929 */
2930int
2931getdirentries(struct getdirentries_args *uap)
2932{
2933 long base;
2934 int error;
2935
2936 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
53b02c48 2937 &uap->sysmsg_result, UIO_USERSPACE);
9697c509
DRJ
2938
2939 if (error == 0)
2940 error = copyout(&base, uap->basep, sizeof(*uap->basep));
984263bc
MD
2941 return (error);
2942}
41c20dac
MD
2943
2944/*
2945 * getdents_args(int fd, char *buf, size_t count)
2946 */
984263bc 2947int
41c20dac 2948getdents(struct getdents_args *uap)
984263bc 2949{
9697c509
DRJ
2950 int error;
2951
2952 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
53b02c48 2953 &uap->sysmsg_result, UIO_USERSPACE);
41c20dac 2954
9697c509 2955 return (error);
984263bc
MD
2956}
2957
2958/*
41c20dac
MD
2959 * umask(int newmask)
2960 *
984263bc
MD
2961 * Set the mode mask for creation of filesystem nodes.
2962 *
2963 * MP SAFE
2964 */
984263bc 2965int
41c20dac 2966umask(struct umask_args *uap)
984263bc 2967{
dadab5e9
MD
2968 struct thread *td = curthread;
2969 struct proc *p = td->td_proc;
41c20dac 2970 struct filedesc *fdp;
984263bc
MD
2971
2972 fdp = p->p_fd;
c7114eea 2973 uap->sysmsg_result = fdp->fd_cmask;
ab2eb4eb 2974 fdp->fd_cmask = uap->newmask & ALLPERMS;
984263bc
MD
2975 return (0);
2976}
2977
2978/*
41c20dac
MD
2979 * revoke(char *path)
2980 *
984263bc
MD
2981 * Void all references to file by ripping underlying filesystem
2982 * away from vnode.
2983 */
984263bc
MD
2984/* ARGSUSED */
2985int
41c20dac 2986revoke(struct revoke_args *uap)
984263bc 2987{
fad57d0e 2988 struct nlookupdata nd;
984263bc 2989 struct vattr vattr;
fad57d0e
MD
2990 struct vnode *vp;
2991 struct ucred *cred;
984263bc 2992 int error;
984263bc 2993
fad57d0e 2994 vp = NULL;
ab2eb4eb 2995 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2996 if (error == 0)
2997 error = nlookup(&nd);
2998 if (error == 0)
2999 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3000 cred = crhold(nd.nl_cred);
3001 nlookup_done(&nd);
3002 if (error == 0) {
3003 if (vp->v_type != VCHR && vp->v_type != VBLK)
3004 error = EINVAL;
3005 if (error == 0)
87de5057 3006 error = VOP_GETATTR(vp, &vattr);
fad57d0e
MD
3007 if (error == 0 && cred->cr_uid != vattr.va_uid)
3008 error = suser_cred(cred, PRISON_ROOT);
3009 if (error == 0 && count_udev(vp->v_udev) > 0) {
3010 if ((error = vx_lock(vp)) == 0) {
3011 VOP_REVOKE(vp, REVOKEALL);
3012 vx_unlock(vp);
3013 }
5fd012e0 3014 }
fad57d0e 3015 vrele(vp);
5fd012e0 3016 }
ac3d60cb
MD
3017 if (cred)
3018 crfree(cred);
984263bc
MD
3019 return (error);
3020}
3021
3022/*
3023 * Convert a user file descriptor to a kernel file entry.
3024 */
3025int
41c20dac 3026getvnode(struct filedesc *fdp, int fd, struct file **fpp)
984263bc
MD
3027{
3028 struct file *fp;
3029
3030 if ((u_int)fd >= fdp->fd_nfiles ||
0679adc4 3031 (fp = fdp->fd_files[fd].fp) == NULL)
984263bc
MD
3032 return (EBADF);
3033 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3034 return (EINVAL);
3035 *fpp = fp;
3036 return (0);
3037}
3038/*
41c20dac
MD
3039 * getfh_args(char *fname, fhandle_t *fhp)
3040 *
984263bc
MD
3041 * Get (NFS) file handle
3042 */
984263bc 3043int
41c20dac 3044getfh(struct getfh_args *uap)
984263bc 3045{
dadab5e9 3046 struct thread *td = curthread;
fad57d0e 3047 struct nlookupdata nd;
984263bc 3048 fhandle_t fh;
41c20dac 3049 struct vnode *vp;
984263bc
MD
3050 int error;
3051
3052 /*
3053 * Must be super user
3054 */
fad57d0e 3055 if ((error = suser(td)) != 0)
984263bc 3056 return (error);
fad57d0e
MD
3057
3058 vp = NULL;
3059 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3060 if (error == 0)
3061 error = nlookup(&nd);
3062 if (error == 0)
3063 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3064 nlookup_done(&nd);
3065 if (error == 0) {
3066 bzero(&fh, sizeof(fh));
3067 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3068 error = VFS_VPTOFH(vp, &fh.fh_fid);
3069 vput(vp);
3070 if (error == 0)
3071 error = copyout(&fh, uap->fhp, sizeof(fh));
3072 }
984263bc
MD
3073 return (error);
3074}
3075
3076/*
41c20dac
MD
3077 * fhopen_args(const struct fhandle *u_fhp, int flags)
3078 *
984263bc
MD
3079 * syscall for the rpc.lockd to use to translate a NFS file handle into
3080 * an open descriptor.
3081 *
3082 * warning: do not remove the suser() call or this becomes one giant
3083 * security hole.
3084 */
984263bc 3085int
41c20dac 3086fhopen(struct fhopen_args *uap)
984263bc 3087{
dadab5e9
MD
3088 struct thread *td = curthread;
3089 struct proc *p = td->td_proc;
984263bc
MD
3090 struct mount *mp;
3091 struct vnode *vp;
3092 struct fhandle fhp;
3093 struct vattr vat;
3094 struct vattr *vap = &vat;
3095 struct flock lf;
984263bc
MD
3096 int fmode, mode, error, type;
3097 struct file *nfp;
fad57d0e 3098 struct file *fp;
984263bc
MD
3099 int indx;
3100
3101 /*
3102 * Must be super user
3103 */
dadab5e9 3104 error = suser(td);
984263bc
MD
3105 if (error)
3106 return (error);
3107
ab2eb4eb 3108 fmode = FFLAGS(uap->flags);
984263bc
MD
3109 /* why not allow a non-read/write open for our lockd? */
3110 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3111 return (EINVAL);
ab2eb4eb 3112 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
984263bc
MD
3113 if (error)
3114 return(error);
3115 /* find the mount point */
3116 mp = vfs_getvfs(&fhp.fh_fsid);
3117 if (mp == NULL)
3118 return (ESTALE);
3119 /* now give me my vnode, it gets returned to me locked */
3120 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3121 if (error)
3122 return (error);
3123 /*
3124 * from now on we have to make sure not
3125 * to forget about the vnode
3126 * any error that causes an abort must vput(vp)
3127 * just set error = err and 'goto bad;'.
3128 */
3129
3130 /*
3131 * from vn_open
3132 */
3133 if (vp->v_type == VLNK) {
3134 error = EMLINK;
3135 goto bad;
3136 }
3137 if (vp->v_type == VSOCK) {
3138 error = EOPNOTSUPP;
3139 goto bad;
3140 }
3141 mode = 0;
3142 if (fmode & (FWRITE | O_TRUNC)) {
3143 if (vp->v_type == VDIR) {
3144 error = EISDIR;
3145 goto bad;
3146 }
3147 error = vn_writechk(vp);
3148 if (error)
3149 goto bad;
3150 mode |= VWRITE;
3151 }
3152 if (fmode & FREAD)
3153 mode |= VREAD;
3154 if (mode) {
87de5057 3155 error = VOP_ACCESS(vp, mode, p->p_ucred);
984263bc
MD
3156 if (error)
3157 goto bad;
3158 }
3159 if (fmode & O_TRUNC) {
ca466bae
MD
3160 VOP_UNLOCK(vp, 0); /* XXX */
3161 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
984263bc
MD
3162 VATTR_NULL(vap);
3163 vap->va_size = 0;
87de5057 3164 error = VOP_SETATTR(vp, vap, p->p_ucred);
984263bc
MD
3165 if (error)
3166 goto bad;
3167 }
fad57d0e 3168
984263bc 3169 /*
fad57d0e
MD
3170 * VOP_OPEN needs the file pointer so it can potentially override
3171 * it.
3172 *
3173 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3174 * XXX
984263bc 3175 */
fad57d0e
MD
3176 if ((error = falloc(p, &nfp, NULL)) != 0)
3177 goto bad;
3178 fp = nfp;
3179
87de5057 3180 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
fad57d0e
MD
3181 if (error) {
3182 /*
3183 * setting f_ops this way prevents VOP_CLOSE from being
3184 * called or fdrop() releasing the vp from v_data. Since
3185 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3186 */
3187 fp->f_ops = &badfileops;
3188 fp->f_data = NULL;
9f87144f 3189 fdrop(fp);
fad57d0e 3190 goto bad;
984263bc 3191 }
fad57d0e
MD
3192
3193 /*
8ddc6004
MD
3194 * The fp is given its own reference, we still have our ref and lock.
3195 *
7540ab49 3196 * Assert that all regular files must be created with a VM object.
984263bc 3197 */
7540ab49
MD
3198 if (vp->v_type == VREG && vp->v_object == NULL) {
3199 printf("fhopen: regular file did not have VM object: %p\n", vp);
9f87144f 3200 fdrop(fp);
7540ab49 3201 goto bad;
fad57d0e 3202 }
984263bc 3203
fad57d0e
MD
3204 /*
3205 * The open was successful, associate it with a file descriptor.
3206 */
3207 if ((error = fsetfd(p, fp, &indx)) != 0) {
9f87144f 3208 fdrop(fp);
984263bc
MD
3209 goto bad;
3210 }
984263bc 3211
984263bc
MD
3212 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3213 lf.l_whence = SEEK_SET;
3214 lf.l_start = 0;
3215 lf.l_len = 0;
3216 if (fmode & O_EXLOCK)
3217 lf.l_type = F_WRLCK;
3218 else
3219 lf.l_type = F_RDLCK;
71c18fe3
MD
3220 if (fmode & FNONBLOCK)
3221 type = 0;
3222 else
3223 type = F_WAIT;
ca466bae 3224 VOP_UNLOCK(vp, 0);
984263bc
MD
3225 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3226 /*
3227 * lock request failed. Normally close the descriptor
3228 * but handle the case where someone might have dup()d
3229 * or close()d it when we weren't looking.
3230 */
fa541be6 3231 fdealloc(p, fp, indx);
984263bc
MD
3232
3233 /*
3234 * release our private reference.
3235 */
9f87144f 3236 fdrop(fp);
fad57d0e 3237 vrele(vp);
984263bc
MD
3238 return (error);
3239 }
ca466bae 3240 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
3241 fp->f_flag |= FHASLOCK;
3242 }
984263bc 3243
fad57d0e 3244 vput(vp);
9f87144f 3245 fdrop(fp);
c7114eea 3246 uap->sysmsg_result = indx;
984263bc
MD
3247 return (0);
3248
3249bad:
3250 vput(vp);
3251 return (error);
3252}
3253
41c20dac
MD
3254/*
3255 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3256 */
984263bc 3257int
41c20dac 3258fhstat(struct fhstat_args *uap)
984263bc 3259{
dadab5e9 3260 struct thread *td = curthread;
984263bc
MD
3261 struct stat sb;
3262 fhandle_t fh;
3263 struct mount *mp;
3264 struct vnode *vp;
3265 int error;
3266
3267 /*
3268 * Must be super user
3269 */
dadab5e9 3270 error = suser(td);
984263bc
MD
3271 if (error)
3272 return (error);
3273
ab2eb4eb