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