Mop up remains of the ibcs2/streams/svr4 removal:
[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 $
228b401d 40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.91 2006/05/19 07:33:45 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 1803 struct file *fp;
984263bc
MD
1804 struct vattr vattr;
1805 int error;
1806
228b401d
MD
1807 fp = holdfp(p->p_fd, fd, -1);
1808 if (fp == NULL)
984263bc 1809 return (EBADF);
228b401d
MD
1810 if (fp->f_type != DTYPE_VNODE) {
1811 error = ESPIPE;
1812 goto done;
1813 }
1814
9697c509 1815 switch (whence) {
984263bc 1816 case L_INCR:
9697c509 1817 fp->f_offset += offset;
228b401d 1818 error = 0;
984263bc
MD
1819 break;
1820 case L_XTND:
228b401d
MD
1821 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1822 if (error == 0)
1823 fp->f_offset = offset + vattr.va_size;
984263bc
MD
1824 break;
1825 case L_SET:
9697c509 1826 fp->f_offset = offset;
228b401d 1827 error = 0;
984263bc
MD
1828 break;
1829 default:
228b401d
MD
1830 error = EINVAL;
1831 break;
984263bc 1832 }
9697c509 1833 *res = fp->f_offset;
228b401d
MD
1834done:
1835 fdrop(fp);
1836 return (error);
984263bc
MD
1837}
1838
984263bc 1839/*
9697c509 1840 * lseek_args(int fd, int pad, off_t offset, int whence)
41c20dac 1841 *
9697c509 1842 * Reposition read/write file offset.
984263bc 1843 */
984263bc 1844int
9697c509 1845lseek(struct lseek_args *uap)
984263bc 1846{
984263bc
MD
1847 int error;
1848
9697c509 1849 error = kern_lseek(uap->fd, uap->offset, uap->whence,
5a3fe67d 1850 &uap->sysmsg_offset);
9697c509 1851
984263bc
MD
1852 return (error);
1853}
984263bc 1854
984263bc 1855int
fad57d0e 1856kern_access(struct nlookupdata *nd, int aflags)
984263bc 1857{
41c20dac 1858 struct vnode *vp;
984263bc 1859 int error, flags;
984263bc 1860
fad57d0e
MD
1861 if ((error = nlookup(nd)) != 0)
1862 return (error);
806dcf9a 1863retry:
fad57d0e 1864 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 1865 if (error)
fad57d0e 1866 return (error);
984263bc
MD
1867
1868 /* Flags == 0 means only check for existence. */
9697c509 1869 if (aflags) {
984263bc 1870 flags = 0;
9697c509 1871 if (aflags & R_OK)
984263bc 1872 flags |= VREAD;
9697c509 1873 if (aflags & W_OK)
984263bc 1874 flags |= VWRITE;
9697c509 1875 if (aflags & X_OK)
984263bc
MD
1876 flags |= VEXEC;
1877 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
87de5057 1878 error = VOP_ACCESS(vp, flags, nd->nl_cred);
806dcf9a
MD
1879
1880 /*
1881 * If the file handle is stale we have to re-resolve the
1882 * entry. This is a hack at the moment.
1883 */
1884 if (error == ESTALE) {
1885 cache_setunresolved(nd->nl_ncp);
1886 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1887 if (error == 0) {
1888 vput(vp);
1889 vp = NULL;
1890 goto retry;
1891 }
1892 }
984263bc 1893 }
984263bc 1894 vput(vp);
984263bc
MD
1895 return (error);
1896}
1897
984263bc 1898/*
9697c509 1899 * access_args(char *path, int flags)
41c20dac 1900 *
9697c509 1901 * Check access permissions.
984263bc 1902 */
984263bc 1903int
9697c509 1904access(struct access_args *uap)
984263bc 1905{
fad57d0e 1906 struct nlookupdata nd;
9697c509 1907 int error;
984263bc 1908
fad57d0e
MD
1909 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1910 if (error == 0)
1911 error = kern_access(&nd, uap->flags);
1912 nlookup_done(&nd);
984263bc
MD
1913 return (error);
1914}
1915
984263bc 1916int
21739618 1917kern_stat(struct nlookupdata *nd, struct stat *st)
984263bc 1918{
984263bc 1919 int error;
21739618
MD
1920 struct vnode *vp;
1921 thread_t td;
984263bc 1922
21739618 1923 if ((error = nlookup(nd)) != 0)
984263bc 1924 return (error);
806dcf9a 1925again:
21739618
MD
1926 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1927 return (ENOENT);
1928
1929 td = curthread;
87de5057 1930 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 1931 return (error);
87de5057 1932 error = vn_stat(vp, st, nd->nl_cred);
806dcf9a
MD
1933
1934 /*
1935 * If the file handle is stale we have to re-resolve the entry. This
1936 * is a hack at the moment.
1937 */
1938 if (error == ESTALE) {
1939 cache_setunresolved(nd->nl_ncp);
1940 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1941 if (error == 0) {
1942 vput(vp);
1943 goto again;
1944 }
1945 }
21739618 1946 vput(vp);
984263bc
MD
1947 return (error);
1948}
1949
984263bc 1950/*
41c20dac
MD
1951 * stat_args(char *path, struct stat *ub)
1952 *
984263bc
MD
1953 * Get file status; this version follows links.
1954 */
984263bc 1955int
41c20dac 1956stat(struct stat_args *uap)
984263bc 1957{
21739618 1958 struct nlookupdata nd;
9697c509
DRJ
1959 struct stat st;
1960 int error;
984263bc 1961
21739618
MD
1962 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1963 if (error == 0) {
1964 error = kern_stat(&nd, &st);
1965 if (error == 0)
1966 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 1967 }
fad57d0e 1968 nlookup_done(&nd);
984263bc
MD
1969 return (error);
1970}
1971
1972/*
41c20dac
MD
1973 * lstat_args(char *path, struct stat *ub)
1974 *
984263bc
MD
1975 * Get file status; this version does not follow links.
1976 */
984263bc 1977int
41c20dac 1978lstat(struct lstat_args *uap)
984263bc 1979{
21739618 1980 struct nlookupdata nd;
9697c509
DRJ
1981 struct stat st;
1982 int error;
984263bc 1983
21739618
MD
1984 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1985 if (error == 0) {
1986 error = kern_stat(&nd, &st);
1987 if (error == 0)
1988 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 1989 }
fad57d0e 1990 nlookup_done(&nd);
984263bc
MD
1991 return (error);
1992}
1993
984263bc 1994/*
41c20dac
MD
1995 * pathconf_Args(char *path, int name)
1996 *
984263bc
MD
1997 * Get configurable pathname variables.
1998 */
984263bc
MD
1999/* ARGSUSED */
2000int
41c20dac 2001pathconf(struct pathconf_args *uap)
984263bc 2002{
fad57d0e
MD
2003 struct nlookupdata nd;
2004 struct vnode *vp;
984263bc 2005 int error;
984263bc 2006
fad57d0e 2007 vp = NULL;
ab2eb4eb 2008 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2009 if (error == 0)
2010 error = nlookup(&nd);
2011 if (error == 0)
2012 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2013 nlookup_done(&nd);
2014 if (error == 0) {
ab2eb4eb 2015 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
fad57d0e
MD
2016 vput(vp);
2017 }
984263bc
MD
2018 return (error);
2019}
2020
bbab27f6
DRJ
2021/*
2022 * XXX: daver
2023 * kern_readlink isn't properly split yet. There is a copyin burried
2024 * in VOP_READLINK().
2025 */
984263bc 2026int
fad57d0e 2027kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
984263bc 2028{
dadab5e9
MD
2029 struct thread *td = curthread;
2030 struct proc *p = td->td_proc;
41c20dac 2031 struct vnode *vp;
984263bc
MD
2032 struct iovec aiov;
2033 struct uio auio;
2034 int error;
984263bc 2035
fad57d0e
MD
2036 if ((error = nlookup(nd)) != 0)
2037 return (error);
2038 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2039 if (error)
984263bc 2040 return (error);
fad57d0e 2041 if (vp->v_type != VLNK) {
984263bc 2042 error = EINVAL;
fad57d0e 2043 } else {
9697c509
DRJ
2044 aiov.iov_base = buf;
2045 aiov.iov_len = count;
984263bc
MD
2046 auio.uio_iov = &aiov;
2047 auio.uio_iovcnt = 1;
2048 auio.uio_offset = 0;
2049 auio.uio_rw = UIO_READ;
2050 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 2051 auio.uio_td = td;
9697c509 2052 auio.uio_resid = count;
984263bc
MD
2053 error = VOP_READLINK(vp, &auio, p->p_ucred);
2054 }
2055 vput(vp);
9697c509
DRJ
2056 *res = count - auio.uio_resid;
2057 return (error);
2058}
2059
2060/*
2061 * readlink_args(char *path, char *buf, int count)
2062 *
2063 * Return target name of a symbolic link.
2064 */
2065int
2066readlink(struct readlink_args *uap)
2067{
fad57d0e 2068 struct nlookupdata nd;
9697c509
DRJ
2069 int error;
2070
fad57d0e
MD
2071 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2072 if (error == 0) {
2073 error = kern_readlink(&nd, uap->buf, uap->count,
2074 &uap->sysmsg_result);
2075 }
2076 nlookup_done(&nd);
984263bc
MD
2077 return (error);
2078}
2079
2080static int
41c20dac 2081setfflags(struct vnode *vp, int flags)
984263bc 2082{
dadab5e9
MD
2083 struct thread *td = curthread;
2084 struct proc *p = td->td_proc;
984263bc
MD
2085 int error;
2086 struct vattr vattr;
2087
2088 /*
2089 * Prevent non-root users from setting flags on devices. When
2090 * a device is reused, users can retain ownership of the device
2091 * if they are allowed to set flags and programs assume that
2092 * chown can't fail when done as root.
2093 */
2094 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
dadab5e9 2095 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
984263bc
MD
2096 return (error);
2097
186fccd6
MD
2098 /*
2099 * note: vget is required for any operation that might mod the vnode
2100 * so VINACTIVE is properly cleared.
2101 */
87de5057 2102 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2103 VATTR_NULL(&vattr);
2104 vattr.va_flags = flags;
87de5057 2105 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2106 vput(vp);
2107 }
984263bc
MD
2108 return (error);
2109}
2110
2111/*
41c20dac
MD
2112 * chflags(char *path, int flags)
2113 *
984263bc
MD
2114 * Change flags of a file given a path name.
2115 */
984263bc
MD
2116/* ARGSUSED */
2117int
41c20dac 2118chflags(struct chflags_args *uap)
984263bc 2119{
fad57d0e
MD
2120 struct nlookupdata nd;
2121 struct vnode *vp;
984263bc 2122 int error;
984263bc 2123
fad57d0e 2124 vp = NULL;
ab2eb4eb 2125 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2126 /* XXX Add NLC flag indicating modifying operation? */
2127 if (error == 0)
2128 error = nlookup(&nd);
2129 if (error == 0)
2130 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2131 nlookup_done(&nd);
2132 if (error == 0) {
ab2eb4eb 2133 error = setfflags(vp, uap->flags);
fad57d0e
MD
2134 vrele(vp);
2135 }
2136 return (error);
984263bc
MD
2137}
2138
2139/*
41c20dac
MD
2140 * fchflags_args(int fd, int flags)
2141 *
984263bc
MD
2142 * Change flags of a file given a file descriptor.
2143 */
984263bc
MD
2144/* ARGSUSED */
2145int
41c20dac 2146fchflags(struct fchflags_args *uap)
984263bc 2147{
dadab5e9
MD
2148 struct thread *td = curthread;
2149 struct proc *p = td->td_proc;
984263bc
MD
2150 struct file *fp;
2151 int error;
2152
ab2eb4eb 2153 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2154 return (error);
ab2eb4eb 2155 return setfflags((struct vnode *) fp->f_data, uap->flags);
984263bc
MD
2156}
2157
2158static int
41c20dac 2159setfmode(struct vnode *vp, int mode)
984263bc 2160{
dadab5e9
MD
2161 struct thread *td = curthread;
2162 struct proc *p = td->td_proc;
984263bc
MD
2163 int error;
2164 struct vattr vattr;
2165
186fccd6
MD
2166 /*
2167 * note: vget is required for any operation that might mod the vnode
2168 * so VINACTIVE is properly cleared.
2169 */
87de5057 2170 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2171 VATTR_NULL(&vattr);
2172 vattr.va_mode = mode & ALLPERMS;
87de5057 2173 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2174 vput(vp);
2175 }
984263bc
MD
2176 return error;
2177}
2178
9697c509 2179int
fad57d0e 2180kern_chmod(struct nlookupdata *nd, int mode)
9697c509 2181{
fad57d0e 2182 struct vnode *vp;
9697c509
DRJ
2183 int error;
2184
fad57d0e
MD
2185 /* XXX Add NLC flag indicating modifying operation? */
2186 if ((error = nlookup(nd)) != 0)
9697c509 2187 return (error);
fad57d0e
MD
2188 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2189 return (error);
2190 error = setfmode(vp, mode);
2191 vrele(vp);
2192 return (error);
9697c509
DRJ
2193}
2194
984263bc 2195/*
41c20dac
MD
2196 * chmod_args(char *path, int mode)
2197 *
984263bc
MD
2198 * Change mode of a file given path name.
2199 */
984263bc
MD
2200/* ARGSUSED */
2201int
41c20dac 2202chmod(struct chmod_args *uap)
984263bc 2203{
fad57d0e 2204 struct nlookupdata nd;
9697c509 2205 int error;
984263bc 2206
fad57d0e
MD
2207 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2208 if (error == 0)
2209 error = kern_chmod(&nd, uap->mode);
2210 nlookup_done(&nd);
9697c509 2211 return (error);
984263bc
MD
2212}
2213
2214/*
41c20dac
MD
2215 * lchmod_args(char *path, int mode)
2216 *
984263bc
MD
2217 * Change mode of a file given path name (don't follow links.)
2218 */
984263bc
MD
2219/* ARGSUSED */
2220int
41c20dac 2221lchmod(struct lchmod_args *uap)
984263bc 2222{
fad57d0e 2223 struct nlookupdata nd;
984263bc 2224 int error;
984263bc 2225
fad57d0e
MD
2226 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2227 if (error == 0)
2228 error = kern_chmod(&nd, uap->mode);
2229 nlookup_done(&nd);
2230 return (error);
984263bc
MD
2231}
2232
2233/*
41c20dac
MD
2234 * fchmod_args(int fd, int mode)
2235 *
984263bc
MD
2236 * Change mode of a file given a file descriptor.
2237 */
984263bc
MD
2238/* ARGSUSED */
2239int
41c20dac 2240fchmod(struct fchmod_args *uap)
984263bc 2241{
dadab5e9
MD
2242 struct thread *td = curthread;
2243 struct proc *p = td->td_proc;
984263bc
MD
2244 struct file *fp;
2245 int error;
2246
ab2eb4eb 2247 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2248 return (error);
ab2eb4eb 2249 return setfmode((struct vnode *)fp->f_data, uap->mode);
984263bc
MD
2250}
2251
2252static int
41c20dac 2253setfown(struct vnode *vp, uid_t uid, gid_t gid)
984263bc 2254{
dadab5e9
MD
2255 struct thread *td = curthread;
2256 struct proc *p = td->td_proc;
984263bc
MD
2257 int error;
2258 struct vattr vattr;
2259
186fccd6
MD
2260 /*
2261 * note: vget is required for any operation that might mod the vnode
2262 * so VINACTIVE is properly cleared.
2263 */
87de5057 2264 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2265 VATTR_NULL(&vattr);
2266 vattr.va_uid = uid;
2267 vattr.va_gid = gid;
87de5057 2268 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2269 vput(vp);
2270 }
984263bc
MD
2271 return error;
2272}
2273
9697c509 2274int
fad57d0e 2275kern_chown(struct nlookupdata *nd, int uid, int gid)
9697c509 2276{
fad57d0e 2277 struct vnode *vp;
9697c509
DRJ
2278 int error;
2279
fad57d0e
MD
2280 /* XXX Add NLC flag indicating modifying operation? */
2281 if ((error = nlookup(nd)) != 0)
9697c509 2282 return (error);
fad57d0e
MD
2283 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2284 return (error);
2285 error = setfown(vp, uid, gid);
2286 vrele(vp);
9697c509
DRJ
2287 return (error);
2288}
2289
984263bc 2290/*
41c20dac
MD
2291 * chown(char *path, int uid, int gid)
2292 *
984263bc
MD
2293 * Set ownership given a path name.
2294 */
984263bc 2295int
41c20dac 2296chown(struct chown_args *uap)
984263bc 2297{
fad57d0e 2298 struct nlookupdata nd;
9697c509
DRJ
2299 int error;
2300
fad57d0e
MD
2301 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2302 if (error == 0)
2303 error = kern_chown(&nd, uap->uid, uap->gid);
2304 nlookup_done(&nd);
984263bc
MD
2305 return (error);
2306}
2307
2308/*
41c20dac
MD
2309 * lchown_args(char *path, int uid, int gid)
2310 *
984263bc
MD
2311 * Set ownership given a path name, do not cross symlinks.
2312 */
984263bc 2313int
41c20dac 2314lchown(struct lchown_args *uap)
984263bc 2315{
fad57d0e 2316 struct nlookupdata nd;
984263bc 2317 int error;
9697c509 2318
fad57d0e
MD
2319 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2320 if (error == 0)
2321 error = kern_chown(&nd, uap->uid, uap->gid);
2322 nlookup_done(&nd);
984263bc
MD
2323 return (error);
2324}
2325
2326/*
41c20dac
MD
2327 * fchown_args(int fd, int uid, int gid)
2328 *
984263bc
MD
2329 * Set ownership given a file descriptor.
2330 */
984263bc
MD
2331/* ARGSUSED */
2332int
41c20dac 2333fchown(struct fchown_args *uap)
984263bc 2334{
dadab5e9
MD
2335 struct thread *td = curthread;
2336 struct proc *p = td->td_proc;
984263bc
MD
2337 struct file *fp;
2338 int error;
2339
ab2eb4eb 2340 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2341 return (error);
41c20dac 2342 return setfown((struct vnode *)fp->f_data,
ab2eb4eb 2343 uap->uid, uap->gid);
984263bc
MD
2344}
2345
2346static int
9697c509 2347getutimes(const struct timeval *tvp, struct timespec *tsp)
984263bc
MD
2348{
2349 struct timeval tv[2];
984263bc 2350
9697c509 2351 if (tvp == NULL) {
984263bc
MD
2352 microtime(&tv[0]);
2353 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2354 tsp[1] = tsp[0];
2355 } else {
9697c509
DRJ
2356 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2357 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
984263bc
MD
2358 }
2359 return 0;
2360}
2361
2362static int
41c20dac 2363setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
984263bc 2364{
dadab5e9
MD
2365 struct thread *td = curthread;
2366 struct proc *p = td->td_proc;
984263bc
MD
2367 int error;
2368 struct vattr vattr;
2369
186fccd6
MD
2370 /*
2371 * note: vget is required for any operation that might mod the vnode
2372 * so VINACTIVE is properly cleared.
2373 */
87de5057 2374 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2375 VATTR_NULL(&vattr);
2376 vattr.va_atime = ts[0];
2377 vattr.va_mtime = ts[1];
2378 if (nullflag)
2379 vattr.va_vaflags |= VA_UTIMES_NULL;
87de5057 2380 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2381 vput(vp);
2382 }
984263bc
MD
2383 return error;
2384}
2385
9697c509 2386int
fad57d0e 2387kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
9697c509
DRJ
2388{
2389 struct timespec ts[2];
fad57d0e 2390 struct vnode *vp;
9697c509
DRJ
2391 int error;
2392
fad57d0e 2393 if ((error = getutimes(tptr, ts)) != 0)
9697c509 2394 return (error);
fad57d0e
MD
2395 /* XXX Add NLC flag indicating modifying operation? */
2396 if ((error = nlookup(nd)) != 0)
9697c509 2397 return (error);
fad57d0e
MD
2398 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2399 return (error);
2400 error = setutimes(vp, ts, tptr == NULL);
2401 vrele(vp);
9697c509
DRJ
2402 return (error);
2403}
2404
984263bc 2405/*
41c20dac
MD
2406 * utimes_args(char *path, struct timeval *tptr)
2407 *
984263bc
MD
2408 * Set the access and modification times of a file.
2409 */
984263bc 2410int
41c20dac 2411utimes(struct utimes_args *uap)
984263bc 2412{
9697c509 2413 struct timeval tv[2];
fad57d0e 2414 struct nlookupdata nd;
9697c509
DRJ
2415 int error;
2416
2417 if (uap->tptr) {
2418 error = copyin(uap->tptr, tv, sizeof(tv));
2419 if (error)
2420 return (error);
2421 }
fad57d0e
MD
2422 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2423 if (error == 0)
2424 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2425 nlookup_done(&nd);
984263bc
MD
2426 return (error);
2427}
2428
2429/*
41c20dac
MD
2430 * lutimes_args(char *path, struct timeval *tptr)
2431 *
984263bc
MD
2432 * Set the access and modification times of a file.
2433 */
984263bc 2434int
41c20dac 2435lutimes(struct lutimes_args *uap)
984263bc 2436{
9697c509 2437 struct timeval tv[2];
fad57d0e 2438 struct nlookupdata nd;
9697c509
DRJ
2439 int error;
2440
2441 if (uap->tptr) {
2442 error = copyin(uap->tptr, tv, sizeof(tv));
2443 if (error)
2444 return (error);
2445 }
fad57d0e
MD
2446 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2447 if (error == 0)
2448 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2449 nlookup_done(&nd);
984263bc
MD
2450 return (error);
2451}
2452
984263bc 2453int
9697c509 2454kern_futimes(int fd, struct timeval *tptr)
984263bc 2455{
dadab5e9
MD
2456 struct thread *td = curthread;
2457 struct proc *p = td->td_proc;
984263bc
MD
2458 struct timespec ts[2];
2459 struct file *fp;
984263bc
MD
2460 int error;
2461
9697c509
DRJ
2462 error = getutimes(tptr, ts);
2463 if (error)
984263bc 2464 return (error);
9697c509
DRJ
2465 error = getvnode(p->p_fd, fd, &fp);
2466 if (error)
984263bc 2467 return (error);
9697c509
DRJ
2468 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2469 return (error);
984263bc
MD
2470}
2471
2472/*
9697c509 2473 * futimes_args(int fd, struct timeval *tptr)
41c20dac 2474 *
9697c509 2475 * Set the access and modification times of a file.
984263bc 2476 */
984263bc 2477int
9697c509
DRJ
2478futimes(struct futimes_args *uap)
2479{
2480 struct timeval tv[2];
2481 int error;
2482
2483 if (uap->tptr) {
2484 error = copyin(uap->tptr, tv, sizeof(tv));
2485 if (error)
2486 return (error);
2487 }
2488
b651c485 2489 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
9697c509
DRJ
2490
2491 return (error);
2492}
2493
2494int
fad57d0e 2495kern_truncate(struct nlookupdata *nd, off_t length)
984263bc 2496{
41c20dac 2497 struct vnode *vp;
984263bc
MD
2498 struct vattr vattr;
2499 int error;
984263bc 2500
9697c509 2501 if (length < 0)
984263bc 2502 return(EINVAL);
fad57d0e
MD
2503 /* XXX Add NLC flag indicating modifying operation? */
2504 if ((error = nlookup(nd)) != 0)
984263bc 2505 return (error);
fad57d0e
MD
2506 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2507 return (error);
ca466bae 2508 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
fad57d0e
MD
2509 vrele(vp);
2510 return (error);
2511 }
2512 if (vp->v_type == VDIR) {
984263bc 2513 error = EISDIR;
fad57d0e 2514 } else if ((error = vn_writechk(vp)) == 0 &&
87de5057 2515 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
984263bc 2516 VATTR_NULL(&vattr);
9697c509 2517 vattr.va_size = length;
87de5057 2518 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
984263bc
MD
2519 }
2520 vput(vp);
2521 return (error);
2522}
2523
9697c509
DRJ
2524/*
2525 * truncate(char *path, int pad, off_t length)
2526 *
2527 * Truncate a file given its path name.
2528 */
2529int
2530truncate(struct truncate_args *uap)
2531{
fad57d0e 2532 struct nlookupdata nd;
9697c509
DRJ
2533 int error;
2534
fad57d0e
MD
2535 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2536 if (error == 0)
2537 error = kern_truncate(&nd, uap->length);
2538 nlookup_done(&nd);
9697c509
DRJ
2539 return error;
2540}
2541
984263bc 2542int
8f6f8622 2543kern_ftruncate(int fd, off_t length)
984263bc 2544{
dadab5e9
MD
2545 struct thread *td = curthread;
2546 struct proc *p = td->td_proc;
984263bc
MD
2547 struct vattr vattr;
2548 struct vnode *vp;
2549 struct file *fp;
2550 int error;
2551
8f6f8622 2552 if (length < 0)
984263bc 2553 return(EINVAL);
8f6f8622 2554 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2555 return (error);
2556 if ((fp->f_flag & FWRITE) == 0)
2557 return (EINVAL);
2558 vp = (struct vnode *)fp->f_data;
ca466bae 2559 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
2560 if (vp->v_type == VDIR)
2561 error = EISDIR;
2562 else if ((error = vn_writechk(vp)) == 0) {
2563 VATTR_NULL(&vattr);
8f6f8622 2564 vattr.va_size = length;
87de5057 2565 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
984263bc 2566 }
ca466bae 2567 VOP_UNLOCK(vp, 0);
984263bc
MD
2568 return (error);
2569}
2570
984263bc 2571/*
8f6f8622 2572 * ftruncate_args(int fd, int pad, off_t length)
41c20dac 2573 *
8f6f8622 2574 * Truncate a file given a file descriptor.
984263bc 2575 */
984263bc 2576int
8f6f8622 2577ftruncate(struct ftruncate_args *uap)
984263bc 2578{
8f6f8622 2579 int error;
984263bc 2580
8f6f8622
DRJ
2581 error = kern_ftruncate(uap->fd, uap->length);
2582
2583 return (error);
984263bc
MD
2584}
2585
984263bc 2586/*
41c20dac
MD
2587 * fsync(int fd)
2588 *
984263bc
MD
2589 * Sync an open file.
2590 */
984263bc
MD
2591/* ARGSUSED */
2592int
41c20dac 2593fsync(struct fsync_args *uap)
984263bc 2594{
dadab5e9
MD
2595 struct thread *td = curthread;
2596 struct proc *p = td->td_proc;
41c20dac 2597 struct vnode *vp;
984263bc
MD
2598 struct file *fp;
2599 vm_object_t obj;
2600 int error;
2601
ab2eb4eb 2602 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc
MD
2603 return (error);
2604 vp = (struct vnode *)fp->f_data;
ca466bae 2605 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7540ab49 2606 if ((obj = vp->v_object) != NULL)
984263bc 2607 vm_object_page_clean(obj, 0, 0, 0);
87de5057 2608 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
984263bc
MD
2609 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2610 bioops.io_fsync)
2611 error = (*bioops.io_fsync)(vp);
ca466bae 2612 VOP_UNLOCK(vp, 0);
984263bc
MD
2613 return (error);
2614}
2615
984263bc 2616int
fad57d0e 2617kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
984263bc 2618{
fad57d0e
MD
2619 struct namecache *fncpd;
2620 struct namecache *tncpd;
2621 struct namecache *ncp;
2622 struct mount *mp;
984263bc
MD
2623 int error;
2624
2625 bwillwrite();
fad57d0e 2626 if ((error = nlookup(fromnd)) != 0)
984263bc 2627 return (error);
fad57d0e
MD
2628 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2629 return (ENOENT);
2630 cache_hold(fncpd);
2631
2632 /*
2633 * unlock the source ncp so we can lookup the target ncp without
2634 * deadlocking. The target may or may not exist so we do not check
2635 * for a target vp like kern_mkdir() and other creation functions do.
2636 *
2637 * The source and target directories are ref'd and rechecked after
2638 * everything is relocked to determine if the source or target file
2639 * has been renamed.
2640 */
2641 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2642 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2643 cache_unlock(fromnd->nl_ncp);
2644
2645 tond->nl_flags |= NLC_CREATE;
2646 if ((error = nlookup(tond)) != 0) {
2647 cache_drop(fncpd);
2648 return (error);
2649 }
2650 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2651 cache_drop(fncpd);
2652 return (ENOENT);
2653 }
2654 cache_hold(tncpd);
2655
2656 /*
2657 * If the source and target are the same there is nothing to do
2658 */
2659 if (fromnd->nl_ncp == tond->nl_ncp) {
2660 cache_drop(fncpd);
2661 cache_drop(tncpd);
2662 return (0);
2663 }
2664
2665 /*
aa40af33
MD
2666 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2667 * may have become invalid while it was unlocked, nc_vp and nc_mount
2668 * could be NULL.
fad57d0e
MD
2669 */
2670 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2671 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2672 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2673 cache_lock(fromnd->nl_ncp);
2674 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2675 } else {
2676 cache_unlock(tond->nl_ncp);
2677 cache_lock(fromnd->nl_ncp);
2678 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2679 cache_lock(tond->nl_ncp);
2680 cache_resolve(tond->nl_ncp, tond->nl_cred);
2681 }
2682 fromnd->nl_flags |= NLC_NCPISLOCKED;
2683
2684 /*
2685 * make sure the parent directories linkages are the same
2686 */
2687 if (fncpd != fromnd->nl_ncp->nc_parent ||
2688 tncpd != tond->nl_ncp->nc_parent) {
2689 cache_drop(fncpd);
2690 cache_drop(tncpd);
2691 return (ENOENT);
2692 }
2693
2694 /*
2695 * Both the source and target must be within the same filesystem and
2696 * in the same filesystem as their parent directories within the
2697 * namecache topology.
aa40af33
MD
2698 *
2699 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
fad57d0e
MD
2700 */
2701 mp = fncpd->nc_mount;
2702 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2703 mp != tond->nl_ncp->nc_mount) {
2704 cache_drop(fncpd);
2705 cache_drop(tncpd);
2706 return (EXDEV);
2707 }
2708
2709 /*
2710 * If the target exists and either the source or target is a directory,
2711 * then both must be directories.
aa40af33
MD
2712 *
2713 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2714 * become NULL.
fad57d0e
MD
2715 */
2716 if (tond->nl_ncp->nc_vp) {
aa40af33
MD
2717 if (fromnd->nl_ncp->nc_vp == NULL) {
2718 error = ENOENT;
2719 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
fad57d0e
MD
2720 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2721 error = ENOTDIR;
2722 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
984263bc 2723 error = EISDIR;
984263bc
MD
2724 }
2725 }
fad57d0e 2726
984263bc 2727 /*
fad57d0e
MD
2728 * You cannot rename a source into itself or a subdirectory of itself.
2729 * We check this by travsersing the target directory upwards looking
2730 * for a match against the source.
984263bc 2731 */
fad57d0e
MD
2732 if (error == 0) {
2733 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2734 if (fromnd->nl_ncp == ncp) {
2735 error = EINVAL;
2736 break;
2737 }
984263bc 2738 }
fad57d0e
MD
2739 }
2740
2741 cache_drop(fncpd);
2742 cache_drop(tncpd);
bc8fd9e0
MD
2743
2744 /*
2745 * Even though the namespaces are different, they may still represent
2746 * hardlinks to the same file. The filesystem might have a hard time
2747 * with this so we issue a NREMOVE of the source instead of a NRENAME
2748 * when we detect the situation.
2749 */
2750 if (error == 0) {
2751 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2752 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2753 } else {
2754 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2755 tond->nl_cred);
2756 }
2757 }
984263bc
MD
2758 return (error);
2759}
2760
2761/*
9697c509 2762 * rename_args(char *from, char *to)
41c20dac 2763 *
9697c509
DRJ
2764 * Rename files. Source and destination must either both be directories,
2765 * or both not be directories. If target is a directory, it must be empty.
984263bc 2766 */
984263bc 2767int
9697c509
DRJ
2768rename(struct rename_args *uap)
2769{
fad57d0e 2770 struct nlookupdata fromnd, tond;
9697c509
DRJ
2771 int error;
2772
fad57d0e
MD
2773 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2774 if (error == 0) {
2775 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2776 if (error == 0)
2777 error = kern_rename(&fromnd, &tond);
2778 nlookup_done(&tond);
2779 }
2780 nlookup_done(&fromnd);
9697c509
DRJ
2781 return (error);
2782}
2783
2784int
fad57d0e 2785kern_mkdir(struct nlookupdata *nd, int mode)
984263bc 2786{
dadab5e9
MD
2787 struct thread *td = curthread;
2788 struct proc *p = td->td_proc;
fad57d0e 2789 struct namecache *ncp;
41c20dac 2790 struct vnode *vp;
984263bc
MD
2791 struct vattr vattr;
2792 int error;
984263bc
MD
2793
2794 bwillwrite();
fad57d0e
MD
2795 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2796 if ((error = nlookup(nd)) != 0)
984263bc 2797 return (error);
fad57d0e
MD
2798
2799 ncp = nd->nl_ncp;
2800 if (ncp->nc_vp)
984263bc 2801 return (EEXIST);
fad57d0e 2802
984263bc
MD
2803 VATTR_NULL(&vattr);
2804 vattr.va_type = VDIR;
9697c509 2805 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
fad57d0e
MD
2806
2807 vp = NULL;
2808 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
9697c509 2809 if (error == 0)
fad57d0e 2810 vput(vp);
984263bc
MD
2811 return (error);
2812}
2813
2814/*
9697c509 2815 * mkdir_args(char *path, int mode)
41c20dac 2816 *
9697c509 2817 * Make a directory file.
984263bc 2818 */
984263bc
MD
2819/* ARGSUSED */
2820int
9697c509
DRJ
2821mkdir(struct mkdir_args *uap)
2822{
fad57d0e 2823 struct nlookupdata nd;
9697c509
DRJ
2824 int error;
2825
fad57d0e
MD
2826 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2827 if (error == 0)
2828 error = kern_mkdir(&nd, uap->mode);
2829 nlookup_done(&nd);
9697c509
DRJ
2830 return (error);
2831}
2832
2833int
fad57d0e 2834kern_rmdir(struct nlookupdata *nd)
984263bc 2835{
fad57d0e 2836 struct namecache *ncp;
984263bc 2837 int error;
984263bc
MD
2838
2839 bwillwrite();
fad57d0e
MD
2840 nd->nl_flags |= NLC_DELETE;
2841 if ((error = nlookup(nd)) != 0)
984263bc 2842 return (error);
fad57d0e
MD
2843
2844 ncp = nd->nl_ncp;
2845 error = VOP_NRMDIR(ncp, nd->nl_cred);
984263bc
MD
2846 return (error);
2847}
2848
984263bc 2849/*
9697c509 2850 * rmdir_args(char *path)
41c20dac 2851 *
9697c509 2852 * Remove a directory file.
984263bc 2853 */
9697c509 2854/* ARGSUSED */
984263bc 2855int
9697c509 2856rmdir(struct rmdir_args *uap)
984263bc 2857{
fad57d0e 2858 struct nlookupdata nd;
9697c509
DRJ
2859 int error;
2860
fad57d0e
MD
2861 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2862 if (error == 0)
2863 error = kern_rmdir(&nd);
2864 nlookup_done(&nd);
984263bc
MD
2865 return (error);
2866}
984263bc 2867
984263bc 2868int
53b02c48
JS
2869kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2870 enum uio_seg direction)
984263bc 2871{
dadab5e9
MD
2872 struct thread *td = curthread;
2873 struct proc *p = td->td_proc;
984263bc
MD
2874 struct vnode *vp;
2875 struct file *fp;
2876 struct uio auio;
2877 struct iovec aiov;
2878 long loff;
2879 int error, eofflag;
2880
9697c509 2881 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2882 return (error);
2883 if ((fp->f_flag & FREAD) == 0)
2884 return (EBADF);
2885 vp = (struct vnode *)fp->f_data;
2886unionread:
2887 if (vp->v_type != VDIR)
2888 return (EINVAL);
9697c509
DRJ
2889 aiov.iov_base = buf;
2890 aiov.iov_len = count;
984263bc
MD
2891 auio.uio_iov = &aiov;
2892 auio.uio_iovcnt = 1;
2893 auio.uio_rw = UIO_READ;
53b02c48 2894 auio.uio_segflg = direction;
dadab5e9 2895 auio.uio_td = td;
9697c509 2896 auio.uio_resid = count;
ca466bae
MD
2897 /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2898 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
2899 loff = auio.uio_offset = fp->f_offset;
2900 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2901 fp->f_offset = auio.uio_offset;
ca466bae 2902 VOP_UNLOCK(vp, 0);
984263bc
MD
2903 if (error)
2904 return (error);
9697c509 2905 if (count == auio.uio_resid) {
984263bc 2906 if (union_dircheckp) {
dadab5e9 2907 error = union_dircheckp(td, &vp, fp);
984263bc
MD
2908 if (error == -1)
2909 goto unionread;
2910 if (error)
2911 return (error);
2912 }
2913 if ((vp->v_flag & VROOT) &&
2914 (vp->v_mount->mnt_flag & MNT_UNION)) {
2915 struct vnode *tvp = vp;
2916 vp = vp->v_mount->mnt_vnodecovered;
597aea93 2917 vref(vp);
fbb4eeab 2918 fp->f_data = vp;
984263bc
MD
2919 fp->f_offset = 0;
2920 vrele(tvp);
2921 goto unionread;
2922 }
2923 }
9697c509
DRJ
2924 if (basep) {
2925 *basep = loff;
984263bc 2926 }
9697c509
DRJ
2927 *res = count - auio.uio_resid;
2928 return (error);
2929}
2930
2931/*
2932 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2933 *
2934 * Read a block of directory entries in a file system independent format.
2935 */
2936int
2937getdirentries(struct getdirentries_args *uap)
2938{
2939 long base;
2940 int error;
2941
2942 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
53b02c48 2943 &uap->sysmsg_result, UIO_USERSPACE);
9697c509
DRJ
2944
2945 if (error == 0)
2946 error = copyout(&base, uap->basep, sizeof(*uap->basep));
984263bc
MD
2947 return (error);
2948}
41c20dac
MD
2949
2950/*
2951 * getdents_args(int fd, char *buf, size_t count)
2952 */
984263bc 2953int
41c20dac 2954getdents(struct getdents_args *uap)
984263bc 2955{
9697c509
DRJ
2956 int error;
2957
2958 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
53b02c48 2959 &uap->sysmsg_result, UIO_USERSPACE);
41c20dac 2960
9697c509 2961 return (error);
984263bc
MD
2962}
2963
2964/*
41c20dac
MD
2965 * umask(int newmask)
2966 *
984263bc
MD
2967 * Set the mode mask for creation of filesystem nodes.
2968 *
2969 * MP SAFE
2970 */
984263bc 2971int
41c20dac 2972umask(struct umask_args *uap)
984263bc 2973{
dadab5e9
MD
2974 struct thread *td = curthread;
2975 struct proc *p = td->td_proc;
41c20dac 2976 struct filedesc *fdp;
984263bc
MD
2977
2978 fdp = p->p_fd;
c7114eea 2979 uap->sysmsg_result = fdp->fd_cmask;
ab2eb4eb 2980 fdp->fd_cmask = uap->newmask & ALLPERMS;
984263bc
MD
2981 return (0);
2982}
2983
2984/*
41c20dac
MD
2985 * revoke(char *path)
2986 *
984263bc
MD
2987 * Void all references to file by ripping underlying filesystem
2988 * away from vnode.
2989 */
984263bc
MD
2990/* ARGSUSED */
2991int
41c20dac 2992revoke(struct revoke_args *uap)
984263bc 2993{
fad57d0e 2994 struct nlookupdata nd;
984263bc 2995 struct vattr vattr;
fad57d0e
MD
2996 struct vnode *vp;
2997 struct ucred *cred;
984263bc 2998 int error;
984263bc 2999
fad57d0e 3000 vp = NULL;
ab2eb4eb 3001 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
3002 if (error == 0)
3003 error = nlookup(&nd);
3004 if (error == 0)
3005 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3006 cred = crhold(nd.nl_cred);
3007 nlookup_done(&nd);
3008 if (error == 0) {
3009 if (vp->v_type != VCHR && vp->v_type != VBLK)
3010 error = EINVAL;
3011 if (error == 0)
87de5057 3012 error = VOP_GETATTR(vp, &vattr);
fad57d0e
MD
3013 if (error == 0 && cred->cr_uid != vattr.va_uid)
3014 error = suser_cred(cred, PRISON_ROOT);
3015 if (error == 0 && count_udev(vp->v_udev) > 0) {
3016 if ((error = vx_lock(vp)) == 0) {
3017 VOP_REVOKE(vp, REVOKEALL);
3018 vx_unlock(vp);
3019 }
5fd012e0 3020 }
fad57d0e 3021 vrele(vp);
5fd012e0 3022 }
ac3d60cb
MD
3023 if (cred)
3024 crfree(cred);
984263bc
MD
3025 return (error);
3026}
3027
984263bc 3028/*
41c20dac
MD
3029 * getfh_args(char *fname, fhandle_t *fhp)
3030 *
984263bc
MD
3031 * Get (NFS) file handle
3032 */
984263bc 3033int
41c20dac 3034getfh(struct getfh_args *uap)
984263bc 3035{
dadab5e9 3036 struct thread *td = curthread;
fad57d0e 3037 struct nlookupdata nd;
984263bc 3038 fhandle_t fh;
41c20dac 3039 struct vnode *vp;
984263bc
MD
3040 int error;
3041
3042 /*
3043 * Must be super user
3044 */
fad57d0e 3045 if ((error = suser(td)) != 0)
984263bc 3046 return (error);
fad57d0e
MD
3047
3048 vp = NULL;
3049 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3050 if (error == 0)
3051 error = nlookup(&nd);
3052 if (error == 0)
3053 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3054 nlookup_done(&nd);
3055 if (error == 0) {
3056 bzero(&fh, sizeof(fh));
3057 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3058 error = VFS_VPTOFH(vp, &fh.fh_fid);
3059 vput(vp);
3060 if (error == 0)
3061 error = copyout(&fh, uap->fhp, sizeof(fh));
3062 }
984263bc
MD
3063 return (error);
3064}
3065
3066/*
41c20dac
MD
3067 * fhopen_args(const struct fhandle *u_fhp, int flags)
3068 *
984263bc
MD
3069 * syscall for the rpc.lockd to use to translate a NFS file handle into
3070 * an open descriptor.
3071 *
3072 * warning: do not remove the suser() call or this becomes one giant
3073 * security hole.
3074 */
984263bc 3075int
41c20dac 3076fhopen(struct fhopen_args *uap)
984263bc 3077{
dadab5e9
MD
3078 struct thread *td = curthread;
3079 struct proc *p = td->td_proc;
984263bc
MD
3080 struct mount *mp;
3081 struct vnode *vp;
3082 struct fhandle fhp;
3083 struct vattr vat;
3084 struct vattr *vap = &vat;
3085 struct flock lf;
984263bc
MD
3086 int fmode, mode, error, type;
3087 struct file *nfp;
fad57d0e 3088 struct file *fp;
984263bc
MD
3089 int indx;
3090
3091 /*
3092 * Must be super user
3093 */
dadab5e9 3094 error = suser(td);
984263bc
MD
3095 if (error)
3096 return (error);
3097
ab2eb4eb 3098 fmode = FFLAGS(uap->flags);
984263bc
MD
3099 /* why not allow a non-read/write open for our lockd? */
3100 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3101 return (EINVAL);
ab2eb4eb 3102 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
984263bc
MD
3103 if (error)
3104 return(error);
3105 /* find the mount point */
3106 mp = vfs_getvfs(&fhp.fh_fsid);
3107 if (mp == NULL)
3108 return (ESTALE);
3109 /* now give me my vnode, it gets returned to me locked */
3110 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3111 if (error)
3112 return (error);
3113 /*
3114 * from now on we have to make sure not
3115 * to forget about the vnode
3116 * any error that causes an abort must vput(vp)
3117 * just set error = err and 'goto bad;'.
3118 */
3119
3120 /*
3121 * from vn_open
3122 */
3123 if (vp->v_type == VLNK) {
3124 error = EMLINK;
3125 goto bad;
3126 }
3127 if (vp->v_type == VSOCK) {
3128 error = EOPNOTSUPP;
3129 goto bad;
3130 }
3131 mode = 0;
3132 if (fmode & (FWRITE | O_TRUNC)) {
3133 if (vp->v_type == VDIR) {
3134 error = EISDIR;
3135 goto bad;
3136 }
3137 error = vn_writechk(vp);
3138 if (error)
3139 goto bad;
3140 mode |= VWRITE;
3141 }
3142 if (fmode & FREAD)
3143 mode |= VREAD;
3144 if (mode) {
87de5057 3145 error = VOP_ACCESS(vp, mode, p->p_ucred);
984263bc
MD
3146 if (error)
3147 goto bad;
3148 }
3149 if (fmode & O_TRUNC) {
ca466bae
MD
3150 VOP_UNLOCK(vp, 0); /* XXX */
3151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
984263bc
MD
3152 VATTR_NULL(vap);
3153 vap->va_size = 0;
87de5057 3154 error = VOP_SETATTR(vp, vap, p->p_ucred);
984263bc
MD
3155 if (error)
3156 goto bad;
3157 }
fad57d0e 3158
984263bc 3159 /*
fad57d0e
MD
3160 * VOP_OPEN needs the file pointer so it can potentially override
3161 * it.
3162 *
3163 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3164 * XXX
984263bc 3165 */
fad57d0e
MD
3166 if ((error = falloc(p, &nfp, NULL)) != 0)
3167 goto bad;
3168 fp = nfp;
3169
87de5057 3170 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
fad57d0e
MD
3171 if (error) {
3172 /*
3173 * setting f_ops this way prevents VOP_CLOSE from being
3174 * called or fdrop() releasing the vp from v_data. Since
3175 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3176 */
3177 fp->f_ops = &badfileops;
3178 fp->f_data = NULL;
9f87144f 3179 fdrop(fp);
fad57d0e 3180 goto bad;
984263bc 3181 }
fad57d0e
MD
3182
3183 /*
8ddc6004
MD
3184 * The fp is given its own reference, we still have our ref and lock.
3185 *
7540ab49 3186 * Assert that all regular files must be created with a VM object.
984263bc 3187 */
7540ab49
MD
3188 if (vp->v_type == VREG && vp->v_object == NULL) {
3189 printf("fhopen: regular file did not have VM object: %p\n", vp);
9f87144f 3190 fdrop(fp);
7540ab49 3191 goto bad;
fad57d0e 3192 }
984263bc 3193
fad57d0e
MD
3194 /*
3195 * The open was successful, associate it with a file descriptor.
3196 */
3197 if ((error = fsetfd(p, fp, &indx)) != 0) {
9f87144f 3198 fdrop(fp);
984263bc
MD
3199 goto bad;
3200 }
984263bc 3201
984263bc
MD
3202 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3203 lf.l_whence = SEEK_SET;
3204 lf.l_start = 0;
3205 lf.l_len = 0;
3206 if (fmode & O_EXLOCK)
3207 lf.l_type = F_WRLCK;
3208 else
3209 lf.l_type = F_RDLCK;
71c18fe3
MD
3210 if (fmode & FNONBLOCK)
3211 type = 0;
3212 else
3213 type = F_WAIT;
ca466bae 3214 VOP_UNLOCK(vp, 0);
984263bc
MD
3215 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3216 /*
3217 * lock request failed. Normally close the descriptor
3218 * but handle the case where someone might have dup()d
3219 * or close()d it when we weren't looking.
3220 */
fa541be6 3221 fdealloc(p, fp, indx);
984263bc
MD
3222
3223 /*
3224 * release our private reference.
3225 */
9f87144f 3226 fdrop(fp);
fad57d0e 3227 vrele(vp);
984263bc
MD
3228 return (error);
3229 }
ca466bae 3230 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
3231 fp->f_flag |= FHASLOCK;
3232 }
984263bc 3233
fad57d0e 3234 vput(vp);
9f87144f 3235 fdrop(fp);
c7114eea 3236 uap->sysmsg_result = indx;
984263bc
MD
3237 return (0);
3238
3239bad:
3240 vput(vp);
3241 return (error);
3242}
3243
41c20dac
MD
3244/*
3245 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3246 */
984263bc 3247int
41c20dac 3248fhstat(struct fhstat_args *uap)
984263bc 3249{
dadab5e9 3250 struct thread *td = curthread;
984263bc
MD
3251 struct stat sb;
3252 fhandle_t fh;
3253 struct mount *mp;
3254 struct vnode *vp;
3255 int error;
3256
3257 /*
3258 * Must be super user
3259 */
dadab5e9 3260 error = suser(td);
984263bc
MD
3261 if (error)
3262 return (error);
3263
ab2eb4eb 3264 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
984263bc
MD
3265 if (error)
3266 return (error);
3267
3268 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3269 return (ESTALE);
3270 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3271 return (error);
87de5057 3272 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
984263bc
MD