Mostly fix nullfs. There are still namespace race issues between
[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 $
ac3d60cb 40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.75 2005/12/02 17:29:45 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;
fbfe4e7d 1274 struct lwp *lp = td->td_lwp;
41c20dac 1275 struct filedesc *fdp = p->p_fd;
9697c509 1276 int cmode, flags;
984263bc 1277 struct file *nfp;
fad57d0e
MD
1278 struct file *fp;
1279 struct vnode *vp;
984263bc
MD
1280 int type, indx, error;
1281 struct flock lf;
984263bc 1282
984263bc
MD
1283 if ((oflags & O_ACCMODE) == O_ACCMODE)
1284 return (EINVAL);
1285 flags = FFLAGS(oflags);
fad57d0e 1286 error = falloc(p, &nfp, NULL);
984263bc
MD
1287 if (error)
1288 return (error);
1289 fp = nfp;
9697c509 1290 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
fad57d0e 1291
984263bc 1292 /*
fad57d0e
MD
1293 * XXX p_dupfd is a real mess. It allows a device to return a
1294 * file descriptor to be duplicated rather then doing the open
1295 * itself.
984263bc 1296 */
fbfe4e7d 1297 lp->lwp_dupfd = -1;
21739618
MD
1298
1299 /*
fad57d0e
MD
1300 * Call vn_open() to do the lookup and assign the vnode to the
1301 * file pointer. vn_open() does not change the ref count on fp
1302 * and the vnode, on success, will be inherited by the file pointer
1303 * and unlocked.
21739618 1304 */
fad57d0e
MD
1305 nd->nl_flags |= NLC_LOCKVP;
1306 error = vn_open(nd, fp, flags, cmode);
1307 nlookup_done(nd);
984263bc 1308 if (error) {
984263bc
MD
1309 /*
1310 * handle special fdopen() case. bleh. dupfdopen() is
1311 * responsible for dropping the old contents of ofiles[indx]
1312 * if it succeeds.
fad57d0e
MD
1313 *
1314 * Note that if fsetfd() succeeds it will add a ref to fp
0679adc4 1315 * which represents the fd_files[] assignment. We must still
fad57d0e 1316 * drop our reference.
984263bc 1317 */
fbfe4e7d 1318 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
fad57d0e 1319 if (fsetfd(p, fp, &indx) == 0) {
fbfe4e7d 1320 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
fad57d0e
MD
1321 if (error == 0) {
1322 *res = indx;
1323 fdrop(fp, td); /* our ref */
1324 return (0);
1325 }
0679adc4 1326 if (fdp->fd_files[indx].fp == fp) {
69908319 1327 funsetfd(fdp, indx);
0679adc4 1328 fdrop(fp, td); /* fd_files[] ref */
fad57d0e
MD
1329 }
1330 }
984263bc 1331 }
fad57d0e 1332 fdrop(fp, td); /* our ref */
984263bc
MD
1333 if (error == ERESTART)
1334 error = EINTR;
1335 return (error);
1336 }
fad57d0e
MD
1337
1338 /*
1339 * ref the vnode for ourselves so it can't be ripped out from under
1340 * is. XXX need an ND flag to request that the vnode be returned
1341 * anyway.
1342 */
1343 vp = (struct vnode *)fp->f_data;
1344 vref(vp);
1345 if ((error = fsetfd(p, fp, &indx)) != 0) {
1346 fdrop(fp, td);
1347 vrele(vp);
1348 return (error);
1349 }
1350
1351 /*
1352 * If no error occurs the vp will have been assigned to the file
1353 * pointer.
1354 */
fbfe4e7d 1355 lp->lwp_dupfd = 0;
984263bc
MD
1356
1357 /*
1358 * There should be 2 references on the file, one from the descriptor
1359 * table, and one for us.
1360 *
1361 * Handle the case where someone closed the file (via its file
1362 * descriptor) while we were blocked. The end result should look
1363 * like opening the file succeeded but it was immediately closed.
1364 */
1365 if (fp->f_count == 1) {
0679adc4 1366 KASSERT(fdp->fd_files[indx].fp != fp,
984263bc 1367 ("Open file descriptor lost all refs"));
fad57d0e
MD
1368 vrele(vp);
1369 fo_close(fp, td);
dadab5e9 1370 fdrop(fp, td);
9697c509 1371 *res = indx;
984263bc
MD
1372 return 0;
1373 }
1374
984263bc
MD
1375 if (flags & (O_EXLOCK | O_SHLOCK)) {
1376 lf.l_whence = SEEK_SET;
1377 lf.l_start = 0;
1378 lf.l_len = 0;
1379 if (flags & O_EXLOCK)
1380 lf.l_type = F_WRLCK;
1381 else
1382 lf.l_type = F_RDLCK;
1383 type = F_FLOCK;
1384 if ((flags & FNONBLOCK) == 0)
1385 type |= F_WAIT;
fad57d0e 1386
984263bc
MD
1387 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1388 /*
1389 * lock request failed. Normally close the descriptor
1390 * but handle the case where someone might have dup()d
1391 * it when we weren't looking. One reference is
1392 * owned by the descriptor array, the other by us.
1393 */
fad57d0e 1394 vrele(vp);
0679adc4 1395 if (fdp->fd_files[indx].fp == fp) {
69908319 1396 funsetfd(fdp, indx);
dadab5e9 1397 fdrop(fp, td);
984263bc 1398 }
dadab5e9 1399 fdrop(fp, td);
984263bc
MD
1400 return (error);
1401 }
984263bc
MD
1402 fp->f_flag |= FHASLOCK;
1403 }
1404 /* assert that vn_open created a backing object if one is needed */
1405 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1406 ("open: vmio vnode has no backing object after vn_open"));
984263bc 1407
fad57d0e 1408 vrele(vp);
21739618 1409
984263bc
MD
1410 /*
1411 * release our private reference, leaving the one associated with the
1412 * descriptor table intact.
1413 */
dadab5e9 1414 fdrop(fp, td);
9697c509 1415 *res = indx;
984263bc
MD
1416 return (0);
1417}
1418
984263bc 1419/*
9697c509 1420 * open_args(char *path, int flags, int mode)
41c20dac 1421 *
9697c509
DRJ
1422 * Check permissions, allocate an open file structure,
1423 * and call the device open routine if any.
984263bc 1424 */
984263bc 1425int
9697c509 1426open(struct open_args *uap)
984263bc 1427{
fad57d0e 1428 struct nlookupdata nd;
9697c509
DRJ
1429 int error;
1430
fad57d0e
MD
1431 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1432 if (error == 0) {
1433 error = kern_open(&nd, uap->flags,
1434 uap->mode, &uap->sysmsg_result);
1435 }
1436 nlookup_done(&nd);
9697c509 1437 return (error);
984263bc 1438}
984263bc 1439
984263bc 1440int
fad57d0e 1441kern_mknod(struct nlookupdata *nd, int mode, int dev)
984263bc 1442{
fad57d0e 1443 struct namecache *ncp;
dadab5e9
MD
1444 struct thread *td = curthread;
1445 struct proc *p = td->td_proc;
41c20dac 1446 struct vnode *vp;
984263bc
MD
1447 struct vattr vattr;
1448 int error;
1449 int whiteout = 0;
984263bc 1450
dadab5e9
MD
1451 KKASSERT(p);
1452
9697c509 1453 switch (mode & S_IFMT) {
984263bc
MD
1454 case S_IFCHR:
1455 case S_IFBLK:
dadab5e9 1456 error = suser(td);
984263bc
MD
1457 break;
1458 default:
dadab5e9 1459 error = suser_cred(p->p_ucred, PRISON_ROOT);
984263bc
MD
1460 break;
1461 }
1462 if (error)
1463 return (error);
fad57d0e 1464
984263bc 1465 bwillwrite();
fad57d0e
MD
1466 nd->nl_flags |= NLC_CREATE;
1467 if ((error = nlookup(nd)) != 0)
984263bc 1468 return (error);
fad57d0e
MD
1469 ncp = nd->nl_ncp;
1470 if (ncp->nc_vp)
1471 return (EEXIST);
984263bc 1472
fad57d0e
MD
1473 VATTR_NULL(&vattr);
1474 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1475 vattr.va_rdev = dev;
1476 whiteout = 0;
1477
1478 switch (mode & S_IFMT) {
1479 case S_IFMT: /* used by badsect to flag bad sectors */
1480 vattr.va_type = VBAD;
1481 break;
1482 case S_IFCHR:
1483 vattr.va_type = VCHR;
1484 break;
1485 case S_IFBLK:
1486 vattr.va_type = VBLK;
1487 break;
1488 case S_IFWHT:
1489 whiteout = 1;
1490 break;
1491 default:
1492 error = EINVAL;
1493 break;
984263bc 1494 }
9697c509 1495 if (error == 0) {
fad57d0e
MD
1496 if (whiteout) {
1497 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1498 } else {
1499 vp = NULL;
1500 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
984263bc 1501 if (error == 0)
fad57d0e 1502 vput(vp);
984263bc 1503 }
984263bc 1504 }
9697c509
DRJ
1505 return (error);
1506}
1507
1508/*
1509 * mknod_args(char *path, int mode, int dev)
1510 *
1511 * Create a special file.
1512 */
1513int
1514mknod(struct mknod_args *uap)
1515{
fad57d0e 1516 struct nlookupdata nd;
9697c509
DRJ
1517 int error;
1518
fad57d0e
MD
1519 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1520 if (error == 0)
1521 error = kern_mknod(&nd, uap->mode, uap->dev);
1522 nlookup_done(&nd);
984263bc
MD
1523 return (error);
1524}
1525
984263bc 1526int
fad57d0e 1527kern_mkfifo(struct nlookupdata *nd, int mode)
984263bc 1528{
fad57d0e 1529 struct namecache *ncp;
dadab5e9
MD
1530 struct thread *td = curthread;
1531 struct proc *p = td->td_proc;
984263bc 1532 struct vattr vattr;
fad57d0e 1533 struct vnode *vp;
984263bc 1534 int error;
984263bc
MD
1535
1536 bwillwrite();
fad57d0e
MD
1537
1538 nd->nl_flags |= NLC_CREATE;
1539 if ((error = nlookup(nd)) != 0)
984263bc 1540 return (error);
fad57d0e
MD
1541 ncp = nd->nl_ncp;
1542 if (ncp->nc_vp)
984263bc 1543 return (EEXIST);
fad57d0e 1544
984263bc
MD
1545 VATTR_NULL(&vattr);
1546 vattr.va_type = VFIFO;
136178b3 1547 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
fad57d0e
MD
1548 vp = NULL;
1549 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
984263bc 1550 if (error == 0)
fad57d0e 1551 vput(vp);
136178b3
DRJ
1552 return (error);
1553}
1554
1555/*
1556 * mkfifo_args(char *path, int mode)
1557 *
1558 * Create a named pipe.
1559 */
1560int
1561mkfifo(struct mkfifo_args *uap)
1562{
fad57d0e 1563 struct nlookupdata nd;
136178b3
DRJ
1564 int error;
1565
fad57d0e
MD
1566 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1567 if (error == 0)
1568 error = kern_mkfifo(&nd, uap->mode);
1569 nlookup_done(&nd);
984263bc
MD
1570 return (error);
1571}
1572
8c40f81e
MD
1573static int hardlink_check_uid = 0;
1574SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1575 &hardlink_check_uid, 0,
1576 "Unprivileged processes cannot create hard links to files owned by other "
1577 "users");
1578static int hardlink_check_gid = 0;
1579SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1580 &hardlink_check_gid, 0,
1581 "Unprivileged processes cannot create hard links to files owned by other "
1582 "groups");
1583
1584static int
1585can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1586{
1587 struct vattr va;
1588 int error;
1589
1590 /*
1591 * Shortcut if disabled
1592 */
1593 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1594 return (0);
1595
1596 /*
1597 * root cred can always hardlink
1598 */
1599 if (suser_cred(cred, PRISON_ROOT) == 0)
1600 return (0);
1601
1602 /*
1603 * Otherwise only if the originating file is owned by the
1604 * same user or group. Note that any group is allowed if
1605 * the file is owned by the caller.
1606 */
1607 error = VOP_GETATTR(vp, &va, td);
1608 if (error != 0)
1609 return (error);
1610
1611 if (hardlink_check_uid) {
1612 if (cred->cr_uid != va.va_uid)
1613 return (EPERM);
1614 }
1615
1616 if (hardlink_check_gid) {
1617 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1618 return (EPERM);
1619 }
1620
1621 return (0);
1622}
1623
984263bc 1624int
fad57d0e 1625kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
984263bc 1626{
dadab5e9 1627 struct thread *td = curthread;
41c20dac 1628 struct vnode *vp;
984263bc
MD
1629 int error;
1630
fad57d0e
MD
1631 /*
1632 * Lookup the source and obtained a locked vnode.
1633 *
1634 * XXX relookup on vget failure / race ?
1635 */
984263bc 1636 bwillwrite();
fad57d0e 1637 if ((error = nlookup(nd)) != 0)
984263bc 1638 return (error);
fad57d0e
MD
1639 vp = nd->nl_ncp->nc_vp;
1640 KKASSERT(vp != NULL);
984263bc 1641 if (vp->v_type == VDIR)
fad57d0e
MD
1642 return (EPERM); /* POSIX */
1643 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1644 return (error);
1645
1646 /*
1647 * Unlock the source so we can lookup the target without deadlocking
1648 * (XXX vp is locked already, possible other deadlock?). The target
1649 * must not exist.
1650 */
1651 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1652 nd->nl_flags &= ~NLC_NCPISLOCKED;
1653 cache_unlock(nd->nl_ncp);
1654
1655 linknd->nl_flags |= NLC_CREATE;
1656 if ((error = nlookup(linknd)) != 0) {
1657 vput(vp);
1658 return (error);
984263bc 1659 }
fad57d0e
MD
1660 if (linknd->nl_ncp->nc_vp) {
1661 vput(vp);
1662 return (EEXIST);
1663 }
1664
1665 /*
1666 * Finally run the new API VOP.
1667 */
8c40f81e
MD
1668 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1669 if (error == 0)
1670 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
fad57d0e 1671 vput(vp);
984263bc
MD
1672 return (error);
1673}
1674
1675/*
9697c509 1676 * link_args(char *path, char *link)
41c20dac 1677 *
9697c509 1678 * Make a hard file link.
984263bc 1679 */
984263bc 1680int
9697c509
DRJ
1681link(struct link_args *uap)
1682{
fad57d0e 1683 struct nlookupdata nd, linknd;
9697c509
DRJ
1684 int error;
1685
fad57d0e
MD
1686 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1687 if (error == 0) {
1688 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1689 if (error == 0)
1690 error = kern_link(&nd, &linknd);
1691 nlookup_done(&linknd);
1692 }
1693 nlookup_done(&nd);
9697c509
DRJ
1694 return (error);
1695}
1696
1697int
fad57d0e 1698kern_symlink(struct nlookupdata *nd, char *path, int mode)
984263bc 1699{
fad57d0e 1700 struct namecache *ncp;
984263bc 1701 struct vattr vattr;
fad57d0e 1702 struct vnode *vp;
984263bc 1703 int error;
984263bc 1704
984263bc 1705 bwillwrite();
fad57d0e
MD
1706 nd->nl_flags |= NLC_CREATE;
1707 if ((error = nlookup(nd)) != 0)
9697c509 1708 return (error);
fad57d0e
MD
1709 ncp = nd->nl_ncp;
1710 if (ncp->nc_vp)
9697c509 1711 return (EEXIST);
fad57d0e 1712
984263bc 1713 VATTR_NULL(&vattr);
fad57d0e
MD
1714 vattr.va_mode = mode;
1715 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
984263bc 1716 if (error == 0)
fad57d0e 1717 vput(vp);
9697c509
DRJ
1718 return (error);
1719}
1720
1721/*
1722 * symlink(char *path, char *link)
1723 *
1724 * Make a symbolic link.
1725 */
1726int
1727symlink(struct symlink_args *uap)
1728{
1729 struct thread *td = curthread;
fad57d0e 1730 struct nlookupdata nd;
9697c509
DRJ
1731 char *path;
1732 int error;
fad57d0e 1733 int mode;
9697c509
DRJ
1734
1735 path = zalloc(namei_zone);
1736 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
e24b948e 1737 if (error == 0) {
fad57d0e
MD
1738 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1739 if (error == 0) {
1740 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1741 error = kern_symlink(&nd, path, mode);
1742 }
1743 nlookup_done(&nd);
e24b948e 1744 }
984263bc
MD
1745 zfree(namei_zone, path);
1746 return (error);
1747}
1748
1749/*
41c20dac
MD
1750 * undelete_args(char *path)
1751 *
984263bc
MD
1752 * Delete a whiteout from the filesystem.
1753 */
1754/* ARGSUSED */
1755int
41c20dac 1756undelete(struct undelete_args *uap)
984263bc 1757{
fad57d0e 1758 struct nlookupdata nd;
984263bc 1759 int error;
984263bc 1760
ab2eb4eb 1761 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
984263bc 1762 bwillwrite();
fad57d0e
MD
1763 nd.nl_flags |= NLC_DELETE;
1764 if (error == 0)
1765 error = nlookup(&nd);
b8997912 1766 if (error == 0)
fad57d0e
MD
1767 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1768 nlookup_done(&nd);
984263bc
MD
1769 return (error);
1770}
1771
984263bc 1772int
fad57d0e 1773kern_unlink(struct nlookupdata *nd)
984263bc 1774{
fad57d0e 1775 struct namecache *ncp;
984263bc 1776 int error;
984263bc
MD
1777
1778 bwillwrite();
fad57d0e
MD
1779 nd->nl_flags |= NLC_DELETE;
1780 if ((error = nlookup(nd)) != 0)
984263bc 1781 return (error);
fad57d0e
MD
1782 ncp = nd->nl_ncp;
1783 error = VOP_NREMOVE(ncp, nd->nl_cred);
984263bc
MD
1784 return (error);
1785}
1786
1787/*
9697c509 1788 * unlink_args(char *path)
41c20dac 1789 *
9697c509 1790 * Delete a name from the filesystem.
984263bc 1791 */
984263bc 1792int
9697c509
DRJ
1793unlink(struct unlink_args *uap)
1794{
fad57d0e 1795 struct nlookupdata nd;
9697c509
DRJ
1796 int error;
1797
fad57d0e
MD
1798 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1799 if (error == 0)
1800 error = kern_unlink(&nd);
1801 nlookup_done(&nd);
9697c509
DRJ
1802 return (error);
1803}
1804
1805int
5a3fe67d 1806kern_lseek(int fd, off_t offset, int whence, off_t *res)
984263bc 1807{
dadab5e9
MD
1808 struct thread *td = curthread;
1809 struct proc *p = td->td_proc;
41c20dac
MD
1810 struct filedesc *fdp = p->p_fd;
1811 struct file *fp;
984263bc
MD
1812 struct vattr vattr;
1813 int error;
1814
b463648b 1815 if ((u_int)fd >= fdp->fd_nfiles ||
0679adc4 1816 (fp = fdp->fd_files[fd].fp) == NULL)
984263bc
MD
1817 return (EBADF);
1818 if (fp->f_type != DTYPE_VNODE)
1819 return (ESPIPE);
9697c509 1820 switch (whence) {
984263bc 1821 case L_INCR:
9697c509 1822 fp->f_offset += offset;
984263bc
MD
1823 break;
1824 case L_XTND:
3b568787 1825 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
984263bc
MD
1826 if (error)
1827 return (error);
9697c509 1828 fp->f_offset = offset + vattr.va_size;
984263bc
MD
1829 break;
1830 case L_SET:
9697c509 1831 fp->f_offset = offset;
984263bc
MD
1832 break;
1833 default:
1834 return (EINVAL);
1835 }
9697c509 1836 *res = fp->f_offset;
984263bc
MD
1837 return (0);
1838}
1839
984263bc 1840/*
9697c509 1841 * lseek_args(int fd, int pad, off_t offset, int whence)
41c20dac 1842 *
9697c509 1843 * Reposition read/write file offset.
984263bc 1844 */
984263bc 1845int
9697c509 1846lseek(struct lseek_args *uap)
984263bc 1847{
984263bc
MD
1848 int error;
1849
9697c509 1850 error = kern_lseek(uap->fd, uap->offset, uap->whence,
5a3fe67d 1851 &uap->sysmsg_offset);
9697c509 1852
984263bc
MD
1853 return (error);
1854}
984263bc 1855
984263bc 1856int
fad57d0e 1857kern_access(struct nlookupdata *nd, int aflags)
984263bc 1858{
dadab5e9 1859 struct thread *td = curthread;
41c20dac 1860 struct vnode *vp;
984263bc 1861 int error, flags;
984263bc 1862
fad57d0e
MD
1863 if ((error = nlookup(nd)) != 0)
1864 return (error);
806dcf9a 1865retry:
fad57d0e 1866 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 1867 if (error)
fad57d0e 1868 return (error);
984263bc
MD
1869
1870 /* Flags == 0 means only check for existence. */
9697c509 1871 if (aflags) {
984263bc 1872 flags = 0;
9697c509 1873 if (aflags & R_OK)
984263bc 1874 flags |= VREAD;
9697c509 1875 if (aflags & W_OK)
984263bc 1876 flags |= VWRITE;
9697c509 1877 if (aflags & X_OK)
984263bc
MD
1878 flags |= VEXEC;
1879 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
fad57d0e 1880 error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
806dcf9a
MD
1881
1882 /*
1883 * If the file handle is stale we have to re-resolve the
1884 * entry. This 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 vp = NULL;
1892 goto retry;
1893 }
1894 }
984263bc 1895 }
984263bc 1896 vput(vp);
984263bc
MD
1897 return (error);
1898}
1899
984263bc 1900/*
9697c509 1901 * access_args(char *path, int flags)
41c20dac 1902 *
9697c509 1903 * Check access permissions.
984263bc 1904 */
984263bc 1905int
9697c509 1906access(struct access_args *uap)
984263bc 1907{
fad57d0e 1908 struct nlookupdata nd;
9697c509 1909 int error;
984263bc 1910
fad57d0e
MD
1911 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1912 if (error == 0)
1913 error = kern_access(&nd, uap->flags);
1914 nlookup_done(&nd);
984263bc
MD
1915 return (error);
1916}
1917
984263bc 1918int
21739618 1919kern_stat(struct nlookupdata *nd, struct stat *st)
984263bc 1920{
984263bc 1921 int error;
21739618
MD
1922 struct vnode *vp;
1923 thread_t td;
984263bc 1924
21739618 1925 if ((error = nlookup(nd)) != 0)
984263bc 1926 return (error);
806dcf9a 1927again:
21739618
MD
1928 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1929 return (ENOENT);
1930
1931 td = curthread;
5fd012e0 1932 if ((error = vget(vp, LK_SHARED, td)) != 0)
21739618
MD
1933 return (error);
1934 error = vn_stat(vp, st, td);
806dcf9a
MD
1935
1936 /*
1937 * If the file handle is stale we have to re-resolve the entry. This
1938 * is a hack at the moment.
1939 */
1940 if (error == ESTALE) {
1941 cache_setunresolved(nd->nl_ncp);
1942 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1943 if (error == 0) {
1944 vput(vp);
1945 goto again;
1946 }
1947 }
21739618 1948 vput(vp);
984263bc
MD
1949 return (error);
1950}
1951
984263bc 1952/*
41c20dac
MD
1953 * stat_args(char *path, struct stat *ub)
1954 *
984263bc
MD
1955 * Get file status; this version follows links.
1956 */
984263bc 1957int
41c20dac 1958stat(struct stat_args *uap)
984263bc 1959{
21739618 1960 struct nlookupdata nd;
9697c509
DRJ
1961 struct stat st;
1962 int error;
984263bc 1963
21739618
MD
1964 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1965 if (error == 0) {
1966 error = kern_stat(&nd, &st);
1967 if (error == 0)
1968 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 1969 }
fad57d0e 1970 nlookup_done(&nd);
984263bc
MD
1971 return (error);
1972}
1973
1974/*
41c20dac
MD
1975 * lstat_args(char *path, struct stat *ub)
1976 *
984263bc
MD
1977 * Get file status; this version does not follow links.
1978 */
984263bc 1979int
41c20dac 1980lstat(struct lstat_args *uap)
984263bc 1981{
21739618 1982 struct nlookupdata nd;
9697c509
DRJ
1983 struct stat st;
1984 int error;
984263bc 1985
21739618
MD
1986 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1987 if (error == 0) {
1988 error = kern_stat(&nd, &st);
1989 if (error == 0)
1990 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 1991 }
fad57d0e 1992 nlookup_done(&nd);
984263bc
MD
1993 return (error);
1994}
1995
984263bc 1996/*
41c20dac
MD
1997 * pathconf_Args(char *path, int name)
1998 *
984263bc
MD
1999 * Get configurable pathname variables.
2000 */
984263bc
MD
2001/* ARGSUSED */
2002int
41c20dac 2003pathconf(struct pathconf_args *uap)
984263bc 2004{
fad57d0e
MD
2005 struct nlookupdata nd;
2006 struct vnode *vp;
984263bc 2007 int error;
984263bc 2008
fad57d0e 2009 vp = NULL;
ab2eb4eb 2010 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2011 if (error == 0)
2012 error = nlookup(&nd);
2013 if (error == 0)
2014 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2015 nlookup_done(&nd);
2016 if (error == 0) {
ab2eb4eb 2017 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
fad57d0e
MD
2018 vput(vp);
2019 }
984263bc
MD
2020 return (error);
2021}
2022
bbab27f6
DRJ
2023/*
2024 * XXX: daver
2025 * kern_readlink isn't properly split yet. There is a copyin burried
2026 * in VOP_READLINK().
2027 */
984263bc 2028int
fad57d0e 2029kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
984263bc 2030{
dadab5e9
MD
2031 struct thread *td = curthread;
2032 struct proc *p = td->td_proc;
41c20dac 2033 struct vnode *vp;
984263bc
MD
2034 struct iovec aiov;
2035 struct uio auio;
2036 int error;
984263bc 2037
fad57d0e
MD
2038 if ((error = nlookup(nd)) != 0)
2039 return (error);
2040 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2041 if (error)
984263bc 2042 return (error);
fad57d0e 2043 if (vp->v_type != VLNK) {
984263bc 2044 error = EINVAL;
fad57d0e 2045 } else {
9697c509
DRJ
2046 aiov.iov_base = buf;
2047 aiov.iov_len = count;
984263bc
MD
2048 auio.uio_iov = &aiov;
2049 auio.uio_iovcnt = 1;
2050 auio.uio_offset = 0;
2051 auio.uio_rw = UIO_READ;
2052 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 2053 auio.uio_td = td;
9697c509 2054 auio.uio_resid = count;
984263bc
MD
2055 error = VOP_READLINK(vp, &auio, p->p_ucred);
2056 }
2057 vput(vp);
9697c509
DRJ
2058 *res = count - auio.uio_resid;
2059 return (error);
2060}
2061
2062/*
2063 * readlink_args(char *path, char *buf, int count)
2064 *
2065 * Return target name of a symbolic link.
2066 */
2067int
2068readlink(struct readlink_args *uap)
2069{
fad57d0e 2070 struct nlookupdata nd;
9697c509
DRJ
2071 int error;
2072
fad57d0e
MD
2073 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2074 if (error == 0) {
2075 error = kern_readlink(&nd, uap->buf, uap->count,
2076 &uap->sysmsg_result);
2077 }
2078 nlookup_done(&nd);
984263bc
MD
2079 return (error);
2080}
2081
2082static int
41c20dac 2083setfflags(struct vnode *vp, int flags)
984263bc 2084{
dadab5e9
MD
2085 struct thread *td = curthread;
2086 struct proc *p = td->td_proc;
984263bc
MD
2087 int error;
2088 struct vattr vattr;
2089
2090 /*
2091 * Prevent non-root users from setting flags on devices. When
2092 * a device is reused, users can retain ownership of the device
2093 * if they are allowed to set flags and programs assume that
2094 * chown can't fail when done as root.
2095 */
2096 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
dadab5e9 2097 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
984263bc
MD
2098 return (error);
2099
186fccd6
MD
2100 /*
2101 * note: vget is required for any operation that might mod the vnode
2102 * so VINACTIVE is properly cleared.
2103 */
dadab5e9 2104 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
186fccd6
MD
2105 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2106 VATTR_NULL(&vattr);
2107 vattr.va_flags = flags;
2108 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2109 vput(vp);
2110 }
984263bc
MD
2111 return (error);
2112}
2113
2114/*
41c20dac
MD
2115 * chflags(char *path, int flags)
2116 *
984263bc
MD
2117 * Change flags of a file given a path name.
2118 */
984263bc
MD
2119/* ARGSUSED */
2120int
41c20dac 2121chflags(struct chflags_args *uap)
984263bc 2122{
fad57d0e
MD
2123 struct nlookupdata nd;
2124 struct vnode *vp;
984263bc 2125 int error;
984263bc 2126
fad57d0e 2127 vp = NULL;
ab2eb4eb 2128 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2129 /* XXX Add NLC flag indicating modifying operation? */
2130 if (error == 0)
2131 error = nlookup(&nd);
2132 if (error == 0)
2133 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2134 nlookup_done(&nd);
2135 if (error == 0) {
ab2eb4eb 2136 error = setfflags(vp, uap->flags);
fad57d0e
MD
2137 vrele(vp);
2138 }
2139 return (error);
984263bc
MD
2140}
2141
2142/*
41c20dac
MD
2143 * fchflags_args(int fd, int flags)
2144 *
984263bc
MD
2145 * Change flags of a file given a file descriptor.
2146 */
984263bc
MD
2147/* ARGSUSED */
2148int
41c20dac 2149fchflags(struct fchflags_args *uap)
984263bc 2150{
dadab5e9
MD
2151 struct thread *td = curthread;
2152 struct proc *p = td->td_proc;
984263bc
MD
2153 struct file *fp;
2154 int error;
2155
ab2eb4eb 2156 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2157 return (error);
ab2eb4eb 2158 return setfflags((struct vnode *) fp->f_data, uap->flags);
984263bc
MD
2159}
2160
2161static int
41c20dac 2162setfmode(struct vnode *vp, int mode)
984263bc 2163{
dadab5e9
MD
2164 struct thread *td = curthread;
2165 struct proc *p = td->td_proc;
984263bc
MD
2166 int error;
2167 struct vattr vattr;
2168
186fccd6
MD
2169 /*
2170 * note: vget is required for any operation that might mod the vnode
2171 * so VINACTIVE is properly cleared.
2172 */
dadab5e9 2173 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
186fccd6
MD
2174 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2175 VATTR_NULL(&vattr);
2176 vattr.va_mode = mode & ALLPERMS;
2177 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2178 vput(vp);
2179 }
984263bc
MD
2180 return error;
2181}
2182
9697c509 2183int
fad57d0e 2184kern_chmod(struct nlookupdata *nd, int mode)
9697c509 2185{
fad57d0e 2186 struct vnode *vp;
9697c509
DRJ
2187 int error;
2188
fad57d0e
MD
2189 /* XXX Add NLC flag indicating modifying operation? */
2190 if ((error = nlookup(nd)) != 0)
9697c509 2191 return (error);
fad57d0e
MD
2192 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2193 return (error);
2194 error = setfmode(vp, mode);
2195 vrele(vp);
2196 return (error);
9697c509
DRJ
2197}
2198
984263bc 2199/*
41c20dac
MD
2200 * chmod_args(char *path, int mode)
2201 *
984263bc
MD
2202 * Change mode of a file given path name.
2203 */
984263bc
MD
2204/* ARGSUSED */
2205int
41c20dac 2206chmod(struct chmod_args *uap)
984263bc 2207{
fad57d0e 2208 struct nlookupdata nd;
9697c509 2209 int error;
984263bc 2210
fad57d0e
MD
2211 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2212 if (error == 0)
2213 error = kern_chmod(&nd, uap->mode);
2214 nlookup_done(&nd);
9697c509 2215 return (error);
984263bc
MD
2216}
2217
2218/*
41c20dac
MD
2219 * lchmod_args(char *path, int mode)
2220 *
984263bc
MD
2221 * Change mode of a file given path name (don't follow links.)
2222 */
984263bc
MD
2223/* ARGSUSED */
2224int
41c20dac 2225lchmod(struct lchmod_args *uap)
984263bc 2226{
fad57d0e 2227 struct nlookupdata nd;
984263bc 2228 int error;
984263bc 2229
fad57d0e
MD
2230 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2231 if (error == 0)
2232 error = kern_chmod(&nd, uap->mode);
2233 nlookup_done(&nd);
2234 return (error);
984263bc
MD
2235}
2236
2237/*
41c20dac
MD
2238 * fchmod_args(int fd, int mode)
2239 *
984263bc
MD
2240 * Change mode of a file given a file descriptor.
2241 */
984263bc
MD
2242/* ARGSUSED */
2243int
41c20dac 2244fchmod(struct fchmod_args *uap)
984263bc 2245{
dadab5e9
MD
2246 struct thread *td = curthread;
2247 struct proc *p = td->td_proc;
984263bc
MD
2248 struct file *fp;
2249 int error;
2250
ab2eb4eb 2251 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2252 return (error);
ab2eb4eb 2253 return setfmode((struct vnode *)fp->f_data, uap->mode);
984263bc
MD
2254}
2255
2256static int
41c20dac 2257setfown(struct vnode *vp, uid_t uid, gid_t gid)
984263bc 2258{
dadab5e9
MD
2259 struct thread *td = curthread;
2260 struct proc *p = td->td_proc;
984263bc
MD
2261 int error;
2262 struct vattr vattr;
2263
186fccd6
MD
2264 /*
2265 * note: vget is required for any operation that might mod the vnode
2266 * so VINACTIVE is properly cleared.
2267 */
dadab5e9 2268 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
186fccd6
MD
2269 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2270 VATTR_NULL(&vattr);
2271 vattr.va_uid = uid;
2272 vattr.va_gid = gid;
2273 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2274 vput(vp);
2275 }
984263bc
MD
2276 return error;
2277}
2278
9697c509 2279int
fad57d0e 2280kern_chown(struct nlookupdata *nd, int uid, int gid)
9697c509 2281{
fad57d0e 2282 struct vnode *vp;
9697c509
DRJ
2283 int error;
2284
fad57d0e
MD
2285 /* XXX Add NLC flag indicating modifying operation? */
2286 if ((error = nlookup(nd)) != 0)
9697c509 2287 return (error);
fad57d0e
MD
2288 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2289 return (error);
2290 error = setfown(vp, uid, gid);
2291 vrele(vp);
9697c509
DRJ
2292 return (error);
2293}
2294
984263bc 2295/*
41c20dac
MD
2296 * chown(char *path, int uid, int gid)
2297 *
984263bc
MD
2298 * Set ownership given a path name.
2299 */
984263bc 2300int
41c20dac 2301chown(struct chown_args *uap)
984263bc 2302{
fad57d0e 2303 struct nlookupdata nd;
9697c509
DRJ
2304 int error;
2305
fad57d0e
MD
2306 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2307 if (error == 0)
2308 error = kern_chown(&nd, uap->uid, uap->gid);
2309 nlookup_done(&nd);
984263bc
MD
2310 return (error);
2311}
2312
2313/*
41c20dac
MD
2314 * lchown_args(char *path, int uid, int gid)
2315 *
984263bc
MD
2316 * Set ownership given a path name, do not cross symlinks.
2317 */
984263bc 2318int
41c20dac 2319lchown(struct lchown_args *uap)
984263bc 2320{
fad57d0e 2321 struct nlookupdata nd;
984263bc 2322 int error;
9697c509 2323
fad57d0e
MD
2324 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2325 if (error == 0)
2326 error = kern_chown(&nd, uap->uid, uap->gid);
2327 nlookup_done(&nd);
984263bc
MD
2328 return (error);
2329}
2330
2331/*
41c20dac
MD
2332 * fchown_args(int fd, int uid, int gid)
2333 *
984263bc
MD
2334 * Set ownership given a file descriptor.
2335 */
984263bc
MD
2336/* ARGSUSED */
2337int
41c20dac 2338fchown(struct fchown_args *uap)
984263bc 2339{
dadab5e9
MD
2340 struct thread *td = curthread;
2341 struct proc *p = td->td_proc;
984263bc
MD
2342 struct file *fp;
2343 int error;
2344
ab2eb4eb 2345 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2346 return (error);
41c20dac 2347 return setfown((struct vnode *)fp->f_data,
ab2eb4eb 2348 uap->uid, uap->gid);
984263bc
MD
2349}
2350
2351static int
9697c509 2352getutimes(const struct timeval *tvp, struct timespec *tsp)
984263bc
MD
2353{
2354 struct timeval tv[2];
984263bc 2355
9697c509 2356 if (tvp == NULL) {
984263bc
MD
2357 microtime(&tv[0]);
2358 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2359 tsp[1] = tsp[0];
2360 } else {
9697c509
DRJ
2361 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2362 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
984263bc
MD
2363 }
2364 return 0;
2365}
2366
2367static int
41c20dac 2368setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
984263bc 2369{
dadab5e9
MD
2370 struct thread *td = curthread;
2371 struct proc *p = td->td_proc;
984263bc
MD
2372 int error;
2373 struct vattr vattr;
2374
186fccd6
MD
2375 /*
2376 * note: vget is required for any operation that might mod the vnode
2377 * so VINACTIVE is properly cleared.
2378 */
dadab5e9 2379 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
186fccd6
MD
2380 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2381 VATTR_NULL(&vattr);
2382 vattr.va_atime = ts[0];
2383 vattr.va_mtime = ts[1];
2384 if (nullflag)
2385 vattr.va_vaflags |= VA_UTIMES_NULL;
2386 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2387 vput(vp);
2388 }
984263bc
MD
2389 return error;
2390}
2391
9697c509 2392int
fad57d0e 2393kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
9697c509
DRJ
2394{
2395 struct timespec ts[2];
fad57d0e 2396 struct vnode *vp;
9697c509
DRJ
2397 int error;
2398
fad57d0e 2399 if ((error = getutimes(tptr, ts)) != 0)
9697c509 2400 return (error);
fad57d0e
MD
2401 /* XXX Add NLC flag indicating modifying operation? */
2402 if ((error = nlookup(nd)) != 0)
9697c509 2403 return (error);
fad57d0e
MD
2404 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2405 return (error);
2406 error = setutimes(vp, ts, tptr == NULL);
2407 vrele(vp);
9697c509
DRJ
2408 return (error);
2409}
2410
984263bc 2411/*
41c20dac
MD
2412 * utimes_args(char *path, struct timeval *tptr)
2413 *
984263bc
MD
2414 * Set the access and modification times of a file.
2415 */
984263bc 2416int
41c20dac 2417utimes(struct utimes_args *uap)
984263bc 2418{
9697c509 2419 struct timeval tv[2];
fad57d0e 2420 struct nlookupdata nd;
9697c509
DRJ
2421 int error;
2422
2423 if (uap->tptr) {
2424 error = copyin(uap->tptr, tv, sizeof(tv));
2425 if (error)
2426 return (error);
2427 }
fad57d0e
MD
2428 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2429 if (error == 0)
2430 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2431 nlookup_done(&nd);
984263bc
MD
2432 return (error);
2433}
2434
2435/*
41c20dac
MD
2436 * lutimes_args(char *path, struct timeval *tptr)
2437 *
984263bc
MD
2438 * Set the access and modification times of a file.
2439 */
984263bc 2440int
41c20dac 2441lutimes(struct lutimes_args *uap)
984263bc 2442{
9697c509 2443 struct timeval tv[2];
fad57d0e 2444 struct nlookupdata nd;
9697c509
DRJ
2445 int error;
2446
2447 if (uap->tptr) {
2448 error = copyin(uap->tptr, tv, sizeof(tv));
2449 if (error)
2450 return (error);
2451 }
fad57d0e
MD
2452 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2453 if (error == 0)
2454 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2455 nlookup_done(&nd);
984263bc
MD
2456 return (error);
2457}
2458
984263bc 2459int
9697c509 2460kern_futimes(int fd, struct timeval *tptr)
984263bc 2461{
dadab5e9
MD
2462 struct thread *td = curthread;
2463 struct proc *p = td->td_proc;
984263bc
MD
2464 struct timespec ts[2];
2465 struct file *fp;
984263bc
MD
2466 int error;
2467
9697c509
DRJ
2468 error = getutimes(tptr, ts);
2469 if (error)
984263bc 2470 return (error);
9697c509
DRJ
2471 error = getvnode(p->p_fd, fd, &fp);
2472 if (error)
984263bc 2473 return (error);
9697c509
DRJ
2474 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2475 return (error);
984263bc
MD
2476}
2477
2478/*
9697c509 2479 * futimes_args(int fd, struct timeval *tptr)
41c20dac 2480 *
9697c509 2481 * Set the access and modification times of a file.
984263bc 2482 */
984263bc 2483int
9697c509
DRJ
2484futimes(struct futimes_args *uap)
2485{
2486 struct timeval tv[2];
2487 int error;
2488
2489 if (uap->tptr) {
2490 error = copyin(uap->tptr, tv, sizeof(tv));
2491 if (error)
2492 return (error);
2493 }
2494
b651c485 2495 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
9697c509
DRJ
2496
2497 return (error);
2498}
2499
2500int
fad57d0e 2501kern_truncate(struct nlookupdata *nd, off_t length)
984263bc 2502{
41c20dac 2503 struct vnode *vp;
984263bc
MD
2504 struct vattr vattr;
2505 int error;
984263bc 2506
9697c509 2507 if (length < 0)
984263bc 2508 return(EINVAL);
fad57d0e
MD
2509 /* XXX Add NLC flag indicating modifying operation? */
2510 if ((error = nlookup(nd)) != 0)
984263bc 2511 return (error);
fad57d0e
MD
2512 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2513 return (error);
2514 VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE);
2515 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) {
2516 vrele(vp);
2517 return (error);
2518 }
2519 if (vp->v_type == VDIR) {
984263bc 2520 error = EISDIR;
fad57d0e
MD
2521 } else if ((error = vn_writechk(vp)) == 0 &&
2522 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
984263bc 2523 VATTR_NULL(&vattr);
9697c509 2524 vattr.va_size = length;
fad57d0e 2525 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
984263bc
MD
2526 }
2527 vput(vp);
2528 return (error);
2529}
2530
9697c509
DRJ
2531/*
2532 * truncate(char *path, int pad, off_t length)
2533 *
2534 * Truncate a file given its path name.
2535 */
2536int
2537truncate(struct truncate_args *uap)
2538{
fad57d0e 2539 struct nlookupdata nd;
9697c509
DRJ
2540 int error;
2541
fad57d0e
MD
2542 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2543 if (error == 0)
2544 error = kern_truncate(&nd, uap->length);
2545 nlookup_done(&nd);
9697c509
DRJ
2546 return error;
2547}
2548
984263bc 2549int
8f6f8622 2550kern_ftruncate(int fd, off_t length)
984263bc 2551{
dadab5e9
MD
2552 struct thread *td = curthread;
2553 struct proc *p = td->td_proc;
984263bc
MD
2554 struct vattr vattr;
2555 struct vnode *vp;
2556 struct file *fp;
2557 int error;
2558
8f6f8622 2559 if (length < 0)
984263bc 2560 return(EINVAL);
8f6f8622 2561 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2562 return (error);
2563 if ((fp->f_flag & FWRITE) == 0)
2564 return (EINVAL);
2565 vp = (struct vnode *)fp->f_data;
dadab5e9 2566 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
5fd012e0 2567 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2568 if (vp->v_type == VDIR)
2569 error = EISDIR;
2570 else if ((error = vn_writechk(vp)) == 0) {
2571 VATTR_NULL(&vattr);
8f6f8622 2572 vattr.va_size = length;
dadab5e9 2573 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
984263bc 2574 }
5fd012e0 2575 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2576 return (error);
2577}
2578
984263bc 2579/*
8f6f8622 2580 * ftruncate_args(int fd, int pad, off_t length)
41c20dac 2581 *
8f6f8622 2582 * Truncate a file given a file descriptor.
984263bc 2583 */
984263bc 2584int
8f6f8622 2585ftruncate(struct ftruncate_args *uap)
984263bc 2586{
8f6f8622 2587 int error;
984263bc 2588
8f6f8622
DRJ
2589 error = kern_ftruncate(uap->fd, uap->length);
2590
2591 return (error);
984263bc
MD
2592}
2593
984263bc 2594/*
41c20dac
MD
2595 * fsync(int fd)
2596 *
984263bc
MD
2597 * Sync an open file.
2598 */
984263bc
MD
2599/* ARGSUSED */
2600int
41c20dac 2601fsync(struct fsync_args *uap)
984263bc 2602{
dadab5e9
MD
2603 struct thread *td = curthread;
2604 struct proc *p = td->td_proc;
41c20dac 2605 struct vnode *vp;
984263bc
MD
2606 struct file *fp;
2607 vm_object_t obj;
2608 int error;
2609
ab2eb4eb 2610 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc
MD
2611 return (error);
2612 vp = (struct vnode *)fp->f_data;
5fd012e0 2613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2614 if (VOP_GETVOBJECT(vp, &obj) == 0)
2615 vm_object_page_clean(obj, 0, 0, 0);
3b568787 2616 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
984263bc
MD
2617 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2618 bioops.io_fsync)
2619 error = (*bioops.io_fsync)(vp);
5fd012e0 2620 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2621 return (error);
2622}
2623
984263bc 2624int
fad57d0e 2625kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
984263bc 2626{
fad57d0e
MD
2627 struct namecache *fncpd;
2628 struct namecache *tncpd;
2629 struct namecache *ncp;
2630 struct mount *mp;
984263bc
MD
2631 int error;
2632
2633 bwillwrite();
fad57d0e 2634 if ((error = nlookup(fromnd)) != 0)
984263bc 2635 return (error);
fad57d0e
MD
2636 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2637 return (ENOENT);
2638 cache_hold(fncpd);
2639
2640 /*
2641 * unlock the source ncp so we can lookup the target ncp without
2642 * deadlocking. The target may or may not exist so we do not check
2643 * for a target vp like kern_mkdir() and other creation functions do.
2644 *
2645 * The source and target directories are ref'd and rechecked after
2646 * everything is relocked to determine if the source or target file
2647 * has been renamed.
2648 */
2649 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2650 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2651 cache_unlock(fromnd->nl_ncp);
2652
2653 tond->nl_flags |= NLC_CREATE;
2654 if ((error = nlookup(tond)) != 0) {
2655 cache_drop(fncpd);
2656 return (error);
2657 }
2658 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2659 cache_drop(fncpd);
2660 return (ENOENT);
2661 }
2662 cache_hold(tncpd);
2663
2664 /*
2665 * If the source and target are the same there is nothing to do
2666 */
2667 if (fromnd->nl_ncp == tond->nl_ncp) {
2668 cache_drop(fncpd);
2669 cache_drop(tncpd);
2670 return (0);
2671 }
2672
2673 /*
aa40af33
MD
2674 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2675 * may have become invalid while it was unlocked, nc_vp and nc_mount
2676 * could be NULL.
fad57d0e
MD
2677 */
2678 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2679 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2680 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2681 cache_lock(fromnd->nl_ncp);
2682 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2683 } else {
2684 cache_unlock(tond->nl_ncp);
2685 cache_lock(fromnd->nl_ncp);
2686 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2687 cache_lock(tond->nl_ncp);
2688 cache_resolve(tond->nl_ncp, tond->nl_cred);
2689 }
2690 fromnd->nl_flags |= NLC_NCPISLOCKED;
2691
2692 /*
2693 * make sure the parent directories linkages are the same
2694 */
2695 if (fncpd != fromnd->nl_ncp->nc_parent ||
2696 tncpd != tond->nl_ncp->nc_parent) {
2697 cache_drop(fncpd);
2698 cache_drop(tncpd);
2699 return (ENOENT);
2700 }
2701
2702 /*
2703 * Both the source and target must be within the same filesystem and
2704 * in the same filesystem as their parent directories within the
2705 * namecache topology.
aa40af33
MD
2706 *
2707 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
fad57d0e
MD
2708 */
2709 mp = fncpd->nc_mount;
2710 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2711 mp != tond->nl_ncp->nc_mount) {
2712 cache_drop(fncpd);
2713 cache_drop(tncpd);
2714 return (EXDEV);
2715 }
2716
2717 /*
2718 * If the target exists and either the source or target is a directory,
2719 * then both must be directories.
aa40af33
MD
2720 *
2721 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2722 * become NULL.
fad57d0e
MD
2723 */
2724 if (tond->nl_ncp->nc_vp) {
aa40af33
MD
2725 if (fromnd->nl_ncp->nc_vp == NULL) {
2726 error = ENOENT;
2727 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
fad57d0e
MD
2728 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2729 error = ENOTDIR;
2730 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
984263bc 2731 error = EISDIR;
984263bc
MD
2732 }
2733 }
fad57d0e 2734
984263bc 2735 /*
fad57d0e
MD
2736 * You cannot rename a source into itself or a subdirectory of itself.
2737 * We check this by travsersing the target directory upwards looking
2738 * for a match against the source.
984263bc 2739 */
fad57d0e
MD
2740 if (error == 0) {
2741 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2742 if (fromnd->nl_ncp == ncp) {
2743 error = EINVAL;
2744 break;
2745 }
984263bc 2746 }
fad57d0e
MD
2747 }
2748
2749 cache_drop(fncpd);
2750 cache_drop(tncpd);
bc8fd9e0
MD
2751
2752 /*
2753 * Even though the namespaces are different, they may still represent
2754 * hardlinks to the same file. The filesystem might have a hard time
2755 * with this so we issue a NREMOVE of the source instead of a NRENAME
2756 * when we detect the situation.
2757 */
2758 if (error == 0) {
2759 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2760 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2761 } else {
2762 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2763 tond->nl_cred);
2764 }
2765 }
984263bc
MD
2766 return (error);
2767}
2768
2769/*
9697c509 2770 * rename_args(char *from, char *to)
41c20dac 2771 *
9697c509
DRJ
2772 * Rename files. Source and destination must either both be directories,
2773 * or both not be directories. If target is a directory, it must be empty.
984263bc 2774 */
984263bc 2775int
9697c509
DRJ
2776rename(struct rename_args *uap)
2777{
fad57d0e 2778 struct nlookupdata fromnd, tond;
9697c509
DRJ
2779 int error;
2780
fad57d0e
MD
2781 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2782 if (error == 0) {
2783 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2784 if (error == 0)
2785 error = kern_rename(&fromnd, &tond);
2786 nlookup_done(&tond);
2787 }
2788 nlookup_done(&fromnd);
9697c509
DRJ
2789 return (error);
2790}
2791
2792int
fad57d0e 2793kern_mkdir(struct nlookupdata *nd, int mode)
984263bc 2794{
dadab5e9
MD
2795 struct thread *td = curthread;
2796 struct proc *p = td->td_proc;
fad57d0e 2797 struct namecache *ncp;
41c20dac 2798 struct vnode *vp;
984263bc
MD
2799 struct vattr vattr;
2800 int error;
984263bc
MD
2801
2802 bwillwrite();
fad57d0e
MD
2803 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2804 if ((error = nlookup(nd)) != 0)
984263bc 2805 return (error);
fad57d0e
MD
2806
2807 ncp = nd->nl_ncp;
2808 if (ncp->nc_vp)
984263bc 2809 return (EEXIST);
fad57d0e 2810
984263bc
MD
2811 VATTR_NULL(&vattr);
2812 vattr.va_type = VDIR;
9697c509 2813 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
fad57d0e
MD
2814
2815 vp = NULL;
2816 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
9697c509 2817 if (error == 0)
fad57d0e 2818 vput(vp);
984263bc
MD
2819 return (error);
2820}
2821
2822/*
9697c509 2823 * mkdir_args(char *path, int mode)
41c20dac 2824 *
9697c509 2825 * Make a directory file.
984263bc 2826 */
984263bc
MD
2827/* ARGSUSED */
2828int
9697c509
DRJ
2829mkdir(struct mkdir_args *uap)
2830{
fad57d0e 2831 struct nlookupdata nd;
9697c509
DRJ
2832 int error;
2833
fad57d0e
MD
2834 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2835 if (error == 0)
2836 error = kern_mkdir(&nd, uap->mode);
2837 nlookup_done(&nd);
9697c509
DRJ
2838 return (error);
2839}
2840
2841int
fad57d0e 2842kern_rmdir(struct nlookupdata *nd)
984263bc 2843{
fad57d0e 2844 struct namecache *ncp;
984263bc 2845 int error;
984263bc
MD
2846
2847 bwillwrite();
fad57d0e
MD
2848 nd->nl_flags |= NLC_DELETE;
2849 if ((error = nlookup(nd)) != 0)
984263bc 2850 return (error);
fad57d0e
MD
2851
2852 ncp = nd->nl_ncp;
2853 error = VOP_NRMDIR(ncp, nd->nl_cred);
984263bc
MD
2854 return (error);
2855}
2856
984263bc 2857/*
9697c509 2858 * rmdir_args(char *path)
41c20dac 2859 *
9697c509 2860 * Remove a directory file.
984263bc 2861 */
9697c509 2862/* ARGSUSED */
984263bc 2863int
9697c509 2864rmdir(struct rmdir_args *uap)
984263bc 2865{
fad57d0e 2866 struct nlookupdata nd;
9697c509
DRJ
2867 int error;
2868
fad57d0e
MD
2869 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2870 if (error == 0)
2871 error = kern_rmdir(&nd);
2872 nlookup_done(&nd);
984263bc
MD
2873 return (error);
2874}
984263bc 2875
984263bc 2876int
53b02c48
JS
2877kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2878 enum uio_seg direction)
984263bc 2879{
dadab5e9
MD
2880 struct thread *td = curthread;
2881 struct proc *p = td->td_proc;
984263bc
MD
2882 struct vnode *vp;
2883 struct file *fp;
2884 struct uio auio;
2885 struct iovec aiov;
2886 long loff;
2887 int error, eofflag;
2888
9697c509 2889 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2890 return (error);
2891 if ((fp->f_flag & FREAD) == 0)
2892 return (EBADF);
2893 vp = (struct vnode *)fp->f_data;
2894unionread:
2895 if (vp->v_type != VDIR)
2896 return (EINVAL);
9697c509
DRJ
2897 aiov.iov_base = buf;
2898 aiov.iov_len = count;
984263bc
MD
2899 auio.uio_iov = &aiov;
2900 auio.uio_iovcnt = 1;
2901 auio.uio_rw = UIO_READ;
53b02c48 2902 auio.uio_segflg = direction;
dadab5e9 2903 auio.uio_td = td;
9697c509 2904 auio.uio_resid = count;
5fd012e0
MD
2905 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2906 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2907 loff = auio.uio_offset = fp->f_offset;
2908 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2909 fp->f_offset = auio.uio_offset;
5fd012e0 2910 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2911 if (error)
2912 return (error);
9697c509 2913 if (count == auio.uio_resid) {
984263bc 2914 if (union_dircheckp) {
dadab5e9 2915 error = union_dircheckp(td, &vp, fp);
984263bc
MD
2916 if (error == -1)
2917 goto unionread;
2918 if (error)
2919 return (error);
2920 }
2921 if ((vp->v_flag & VROOT) &&
2922 (vp->v_mount->mnt_flag & MNT_UNION)) {
2923 struct vnode *tvp = vp;
2924 vp = vp->v_mount->mnt_vnodecovered;
597aea93 2925 vref(vp);
fbb4eeab 2926 fp->f_data = vp;
984263bc
MD
2927 fp->f_offset = 0;
2928 vrele(tvp);
2929 goto unionread;
2930 }
2931 }
9697c509
DRJ
2932 if (basep) {
2933 *basep = loff;
984263bc 2934 }
9697c509
DRJ
2935 *res = count - auio.uio_resid;
2936 return (error);
2937}
2938
2939/*
2940 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2941 *
2942 * Read a block of directory entries in a file system independent format.
2943 */
2944int
2945getdirentries(struct getdirentries_args *uap)
2946{
2947 long base;
2948 int error;
2949
2950 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
53b02c48 2951 &uap->sysmsg_result, UIO_USERSPACE);
9697c509
DRJ
2952
2953 if (error == 0)
2954 error = copyout(&base, uap->basep, sizeof(*uap->basep));
984263bc
MD
2955 return (error);
2956}
41c20dac
MD
2957
2958/*
2959 * getdents_args(int fd, char *buf, size_t count)
2960 */
984263bc 2961int
41c20dac 2962getdents(struct getdents_args *uap)
984263bc 2963{
9697c509
DRJ
2964 int error;
2965
2966 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
53b02c48 2967 &uap->sysmsg_result, UIO_USERSPACE);
41c20dac 2968
9697c509 2969 return (error);
984263bc
MD
2970}
2971
2972/*
41c20dac
MD
2973 * umask(int newmask)
2974 *
984263bc
MD
2975 * Set the mode mask for creation of filesystem nodes.
2976 *
2977 * MP SAFE
2978 */
984263bc 2979int
41c20dac 2980umask(struct umask_args *uap)
984263bc 2981{
dadab5e9
MD
2982 struct thread *td = curthread;
2983 struct proc *p = td->td_proc;
41c20dac 2984 struct filedesc *fdp;
984263bc
MD
2985
2986 fdp = p->p_fd;
c7114eea 2987 uap->sysmsg_result = fdp->fd_cmask;
ab2eb4eb 2988 fdp->fd_cmask = uap->newmask & ALLPERMS;
984263bc
MD
2989 return (0);
2990}
2991
2992/*
41c20dac
MD
2993 * revoke(char *path)
2994 *
984263bc
MD
2995 * Void all references to file by ripping underlying filesystem
2996 * away from vnode.
2997 */
984263bc
MD
2998/* ARGSUSED */
2999int
41c20dac 3000revoke(struct revoke_args *uap)
984263bc 3001{
dadab5e9 3002 struct thread *td = curthread;
fad57d0e 3003 struct nlookupdata nd;
984263bc 3004 struct vattr vattr;
fad57d0e
MD
3005 struct vnode *vp;
3006 struct ucred *cred;
984263bc 3007 int error;
984263bc 3008
fad57d0e 3009 vp = NULL;
ab2eb4eb 3010 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
3011 if (error == 0)
3012 error = nlookup(&nd);
3013 if (error == 0)
3014 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3015 cred = crhold(nd.nl_cred);
3016 nlookup_done(&nd);
3017 if (error == 0) {
3018 if (vp->v_type != VCHR && vp->v_type != VBLK)
3019 error = EINVAL;
3020 if (error == 0)
3021 error = VOP_GETATTR(vp, &vattr, td);
3022 if (error == 0 && cred->cr_uid != vattr.va_uid)
3023 error = suser_cred(cred, PRISON_ROOT);
3024 if (error == 0 && count_udev(vp->v_udev) > 0) {
3025 if ((error = vx_lock(vp)) == 0) {
3026 VOP_REVOKE(vp, REVOKEALL);
3027 vx_unlock(vp);
3028 }
5fd012e0 3029 }
fad57d0e 3030 vrele(vp);
5fd012e0 3031 }
ac3d60cb
MD
3032 if (cred)
3033 crfree(cred);
984263bc
MD
3034 return (error);
3035}
3036
3037/*
3038 * Convert a user file descriptor to a kernel file entry.
3039 */
3040int
41c20dac 3041getvnode(struct filedesc *fdp, int fd, struct file **fpp)
984263bc
MD
3042{
3043 struct file *fp;
3044
3045 if ((u_int)fd >= fdp->fd_nfiles ||
0679adc4 3046 (fp = fdp->fd_files[fd].fp) == NULL)
984263bc
MD
3047 return (EBADF);
3048 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3049 return (EINVAL);
3050 *fpp = fp;
3051 return (0);
3052}
3053/*
41c20dac
MD
3054 * getfh_args(char *fname, fhandle_t *fhp)
3055 *
984263bc
MD
3056 * Get (NFS) file handle
3057 */
984263bc 3058int
41c20dac 3059getfh(struct getfh_args *uap)
984263bc 3060{
dadab5e9 3061 struct thread *td = curthread;
fad57d0e 3062 struct nlookupdata nd;
984263bc 3063 fhandle_t fh;
41c20dac 3064 struct vnode *vp;
984263bc
MD
3065 int error;
3066
3067 /*
3068 * Must be super user
3069 */
fad57d0e 3070 if ((error = suser(td)) != 0)
984263bc 3071 return (error);
fad57d0e
MD
3072
3073 vp = NULL;
3074 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3075 if (error == 0)
3076 error = nlookup(&nd);
3077 if (error == 0)
3078 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3079 nlookup_done(&nd);
3080 if (error == 0) {
3081 bzero(&fh, sizeof(fh));
3082 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3083 error = VFS_VPTOFH(vp, &fh.fh_fid);
3084 vput(vp);
3085 if (error == 0)
3086 error = copyout(&fh, uap->fhp, sizeof(fh));
3087 }
984263bc
MD
3088 return (error);
3089}
3090
3091/*
41c20dac
MD
3092 * fhopen_args(const struct fhandle *u_fhp, int flags)
3093 *
984263bc
MD
3094 * syscall for the rpc.lockd to use to translate a NFS file handle into
3095 * an open descriptor.
3096 *
3097 * warning: do not remove the suser() call or this becomes one giant
3098 * security hole.
3099 */
984263bc 3100int
41c20dac 3101fhopen(struct fhopen_args *uap)
984263bc 3102{
dadab5e9
MD
3103 struct thread *td = curthread;
3104 struct proc *p = td->td_proc;
984263bc
MD
3105 struct mount *mp;
3106 struct vnode *vp;
3107 struct fhandle fhp;
3108 struct vattr vat;
3109 struct vattr *vap = &vat;
3110 struct flock lf;
41c20dac 3111 struct filedesc *fdp = p->p_fd;
984263bc
MD
3112 int fmode, mode, error, type;
3113 struct file *nfp;
fad57d0e 3114 struct file *fp;
984263bc
MD
3115 int indx;
3116
3117 /*
3118 * Must be super user
3119 */
dadab5e9 3120 error = suser(td);
984263bc
MD
3121 if (error)
3122 return (error);
3123
ab2eb4eb 3124 fmode = FFLAGS(uap->flags);
984263bc
MD
3125 /* why not allow a non-read/write open for our lockd? */
3126 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3127 return (EINVAL);
ab2eb4eb 3128 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
984263bc
MD
3129 if (error)
3130 return(error);
3131 /* find the mount point */
3132 mp = vfs_getvfs(&fhp.fh_fsid);
3133 if (mp == NULL)
3134 return (ESTALE);
3135 /* now give me my vnode, it gets returned to me locked */
3136 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3137 if (error)
3138 return (error);
3139 /*
3140 * from now on we have to make sure not
3141 * to forget about the vnode
3142 * any error that causes an abort must vput(vp)
3143 * just set error = err and 'goto bad;'.
3144 */
3145
3146 /*
3147 * from vn_open
3148 */
3149 if (vp->v_type == VLNK) {
3150 error = EMLINK;
3151 goto bad;
3152 }
3153 if (vp->v_type == VSOCK) {
3154 error = EOPNOTSUPP;
3155 goto bad;
3156 }
3157 mode = 0;
3158 if (fmode & (FWRITE | O_TRUNC)) {
3159 if (vp->v_type == VDIR) {
3160 error = EISDIR;
3161 goto bad;
3162 }
3163 error = vn_writechk(vp);
3164 if (error)
3165 goto bad;
3166 mode |= VWRITE;
3167 }
3168 if (fmode & FREAD)
3169 mode |= VREAD;
3170 if (mode) {
dadab5e9 3171 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
984263bc
MD
3172 if (error)
3173 goto bad;
3174 }
3175 if (fmode & O_TRUNC) {
5fd012e0 3176 VOP_UNLOCK(vp, 0, td); /* XXX */
dadab5e9 3177 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
5fd012e0 3178 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
984263bc
MD
3179 VATTR_NULL(vap);
3180 vap->va_size = 0;
dadab5e9 3181 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
984263bc
MD
3182 if (error)
3183 goto bad;
3184 }
fad57d0e 3185
984263bc 3186 /*
fad57d0e
MD
3187 * VOP_OPEN needs the file pointer so it can potentially override
3188 * it.
3189 *
3190 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3191 * XXX
984263bc 3192 */
fad57d0e
MD
3193 if ((error = falloc(p, &nfp, NULL)) != 0)
3194 goto bad;
3195 fp = nfp;
3196
fbb4eeab 3197 fp->f_type = DTYPE_VNODE;
fad57d0e
MD
3198 fp->f_flag = fmode & FMASK;
3199 fp->f_ops = &vnode_fileops;
fbb4eeab 3200 fp->f_data = vp;
fad57d0e
MD
3201
3202 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
3203 if (error) {
3204 /*
3205 * setting f_ops this way prevents VOP_CLOSE from being
3206 * called or fdrop() releasing the vp from v_data. Since
3207 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3208 */
3209 fp->f_ops = &badfileops;
3210 fp->f_data = NULL;
3211 fdrop(fp, td);
3212 goto bad;
984263bc
MD
3213 }
3214 if (fmode & FWRITE)
3215 vp->v_writecount++;
3216
3217 /*
fad57d0e
MD
3218 * The fp now owns a reference on the vnode. We still have our own
3219 * ref+lock.
3220 */
3221 vref(vp);
3222
3223 /*
3224 * Make sure that a VM object is created for VMIO support. If this
3225 * fails just fdrop() normally to clean up.
984263bc 3226 */
fad57d0e
MD
3227 if (vn_canvmio(vp) == TRUE) {
3228 if ((error = vfs_object_create(vp, td)) != 0) {
3229 fdrop(fp, td);
3230 goto bad;
3231 }
3232 }
984263bc 3233
fad57d0e
MD
3234 /*
3235 * The open was successful, associate it with a file descriptor.
3236 */
3237 if ((error = fsetfd(p, fp, &indx)) != 0) {
984263bc
MD
3238 if (fmode & FWRITE)
3239 vp->v_writecount--;
fad57d0e 3240 fdrop(fp, td);
984263bc
MD
3241 goto bad;
3242 }
984263bc 3243
984263bc
MD
3244 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3245 lf.l_whence = SEEK_SET;
3246 lf.l_start = 0;
3247 lf.l_len = 0;
3248 if (fmode & O_EXLOCK)
3249 lf.l_type = F_WRLCK;
3250 else
3251 lf.l_type = F_RDLCK;
3252 type = F_FLOCK;
3253 if ((fmode & FNONBLOCK) == 0)
3254 type |= F_WAIT;
5fd012e0 3255 VOP_UNLOCK(vp, 0, td);
984263bc
MD
3256 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3257 /*
3258 * lock request failed. Normally close the descriptor
3259 * but handle the case where someone might have dup()d
3260 * or close()d it when we weren't looking.
3261 */
0679adc4 3262 if (fdp->fd_files[indx].fp == fp) {
69908319 3263 funsetfd(fdp, indx);
dadab5e9 3264 fdrop(fp, td);
984263bc
MD
3265 }
3266
3267 /*
3268 * release our private reference.
3269 */
dadab5e9 3270 fdrop(fp, td);
fad57d0e 3271 vrele(vp);
984263bc
MD
3272 return (error);
3273 }
5fd012e0 3274 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
3275 fp->f_flag |= FHASLOCK;
3276 }
3277 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3b568787 3278 vfs_object_create(vp, td);
984263bc 3279
fad57d0e 3280 vput(vp);
dadab5e9 3281 fdrop(fp, td);
c7114eea 3282 uap->sysmsg_result = indx;
984263bc
MD
3283 return (0);
3284
3285bad:
3286 vput(vp);
3287 return (error);
3288}
3289
41c20dac
MD
3290/*
3291 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3292 */
984263bc 3293int
41c20dac 3294fhstat(struct fhstat_args *uap)
984263bc 3295{
dadab5e9 3296 struct thread *td = curthread;
984263bc
MD
3297 struct stat sb;
3298 fhandle_t fh;
3299 struct mount *mp;
3300 struct vnode *vp;
3301 int error;
3302
3303 /*
3304 * Must be super user
330