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