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