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