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