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