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