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