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