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