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