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