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