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