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