6f4cec2b490d4363fc93fbf569aa5833f268adc6
[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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)vfs_syscalls.c      8.13 (Berkeley) 4/15/94
35  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/linker.h>
52 #include <sys/stat.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/proc.h>
56 #include <sys/priv.h>
57 #include <sys/jail.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
66
67 #include <sys/buf2.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
77
78 static void mount_warning(struct mount *mp, const char *ctl, ...)
79                 __printflike(2, 3);
80 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
81 static int checkvp_chdir (struct vnode *vn, struct thread *td);
82 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
83 static int chroot_refuse_vdir_fds (thread_t td, struct filedesc *fdp);
84 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
85 static int getutimes (struct timeval *, struct timespec *);
86 static int getutimens (const struct timespec *, struct timespec *, int *);
87 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, struct vattr *,
91                         const struct timespec *, int);
92 static int      usermount = 0;  /* if 1, non-root can mount fs. */
93
94 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
95     "Allow non-root users to mount filesystems");
96
97 /*
98  * Virtual File System System Calls
99  */
100
101 /*
102  * Mount a file system.
103  *
104  * mount_args(char *type, char *path, int flags, caddr_t data)
105  *
106  * MPALMOSTSAFE
107  */
108 int
109 sys_mount(struct mount_args *uap)
110 {
111         struct thread *td = curthread;
112         struct vnode *vp;
113         struct nchandle nch;
114         struct mount *mp, *nullmp;
115         struct vfsconf *vfsp;
116         int error, flag = 0, flag2 = 0;
117         int hasmount;
118         struct vattr va;
119         struct nlookupdata nd;
120         char fstypename[MFSNAMELEN];
121         struct ucred *cred;
122
123         cred = td->td_ucred;
124         if (jailed(cred)) {
125                 error = EPERM;
126                 goto done;
127         }
128         if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
129                 goto done;
130
131         /*
132          * Do not allow NFS export by non-root users.
133          */
134         if (uap->flags & MNT_EXPORTED) {
135                 error = priv_check(td, PRIV_ROOT);
136                 if (error)
137                         goto done;
138         }
139         /*
140          * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
141          */
142         if (priv_check(td, PRIV_ROOT)) 
143                 uap->flags |= MNT_NOSUID | MNT_NODEV;
144
145         /*
146          * Lookup the requested path and extract the nch and vnode.
147          */
148         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
149         if (error == 0) {
150                 if ((error = nlookup(&nd)) == 0) {
151                         if (nd.nl_nch.ncp->nc_vp == NULL)
152                                 error = ENOENT;
153                 }
154         }
155         if (error) {
156                 nlookup_done(&nd);
157                 goto done;
158         }
159
160         /*
161          * If the target filesystem is resolved via a nullfs mount, then
162          * nd.nl_nch.mount will be pointing to the nullfs mount structure
163          * instead of the target file system. We need it in case we are
164          * doing an update.
165          */
166         nullmp = nd.nl_nch.mount;
167
168         /*
169          * Extract the locked+refd ncp and cleanup the nd structure
170          */
171         nch = nd.nl_nch;
172         cache_zero(&nd.nl_nch);
173         nlookup_done(&nd);
174
175         if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
176             (mp = cache_findmount(&nch)) != NULL) {
177                 cache_dropmount(mp);
178                 hasmount = 1;
179         } else {
180                 hasmount = 0;
181         }
182
183
184         /*
185          * now we have the locked ref'd nch and unreferenced vnode.
186          */
187         vp = nch.ncp->nc_vp;
188         if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
189                 cache_put(&nch);
190                 goto done;
191         }
192         cache_unlock(&nch);
193
194         /*
195          * Extract the file system type. We need to know this early, to take
196          * appropriate actions if we are dealing with a nullfs.
197          */
198         if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
199                 cache_drop(&nch);
200                 vput(vp);
201                 goto done;
202         }
203
204         /*
205          * Now we have an unlocked ref'd nch and a locked ref'd vp
206          */
207         if (uap->flags & MNT_UPDATE) {
208                 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
209                         cache_drop(&nch);
210                         vput(vp);
211                         error = EINVAL;
212                         goto done;
213                 }
214
215                 if (strncmp(fstypename, "null", 5) == 0) {
216                         KKASSERT(nullmp);
217                         mp = nullmp;
218                 } else {
219                         mp = vp->v_mount;
220                 }
221
222                 flag = mp->mnt_flag;
223                 flag2 = mp->mnt_kern_flag;
224                 /*
225                  * We only allow the filesystem to be reloaded if it
226                  * is currently mounted read-only.
227                  */
228                 if ((uap->flags & MNT_RELOAD) &&
229                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
230                         cache_drop(&nch);
231                         vput(vp);
232                         error = EOPNOTSUPP;     /* Needs translation */
233                         goto done;
234                 }
235                 /*
236                  * Only root, or the user that did the original mount is
237                  * permitted to update it.
238                  */
239                 if (mp->mnt_stat.f_owner != cred->cr_uid &&
240                     (error = priv_check(td, PRIV_ROOT))) {
241                         cache_drop(&nch);
242                         vput(vp);
243                         goto done;
244                 }
245                 if (vfs_busy(mp, LK_NOWAIT)) {
246                         cache_drop(&nch);
247                         vput(vp);
248                         error = EBUSY;
249                         goto done;
250                 }
251                 if (hasmount) {
252                         cache_drop(&nch);
253                         vfs_unbusy(mp);
254                         vput(vp);
255                         error = EBUSY;
256                         goto done;
257                 }
258                 mp->mnt_flag |=
259                     uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
260                 lwkt_gettoken(&mp->mnt_token);
261                 vn_unlock(vp);
262                 vfsp = mp->mnt_vfc;
263                 goto update;
264         }
265
266         /*
267          * If the user is not root, ensure that they own the directory
268          * onto which we are attempting to mount.
269          */
270         if ((error = VOP_GETATTR(vp, &va)) ||
271             (va.va_uid != cred->cr_uid &&
272              (error = priv_check(td, PRIV_ROOT)))) {
273                 cache_drop(&nch);
274                 vput(vp);
275                 goto done;
276         }
277         if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
278                 cache_drop(&nch);
279                 vput(vp);
280                 goto done;
281         }
282         if (vp->v_type != VDIR) {
283                 cache_drop(&nch);
284                 vput(vp);
285                 error = ENOTDIR;
286                 goto done;
287         }
288         if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
289                 cache_drop(&nch);
290                 vput(vp);
291                 error = EPERM;
292                 goto done;
293         }
294         vfsp = vfsconf_find_by_name(fstypename);
295         if (vfsp == NULL) {
296                 linker_file_t lf;
297
298                 /* Only load modules for root (very important!) */
299                 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
300                         cache_drop(&nch);
301                         vput(vp);
302                         goto done;
303                 }
304                 error = linker_load_file(fstypename, &lf);
305                 if (error || lf == NULL) {
306                         cache_drop(&nch);
307                         vput(vp);
308                         if (lf == NULL)
309                                 error = ENODEV;
310                         goto done;
311                 }
312                 lf->userrefs++;
313                 /* lookup again, see if the VFS was loaded */
314                 vfsp = vfsconf_find_by_name(fstypename);
315                 if (vfsp == NULL) {
316                         lf->userrefs--;
317                         linker_file_unload(lf);
318                         cache_drop(&nch);
319                         vput(vp);
320                         error = ENODEV;
321                         goto done;
322                 }
323         }
324         if (hasmount) {
325                 cache_drop(&nch);
326                 vput(vp);
327                 error = EBUSY;
328                 goto done;
329         }
330
331         /*
332          * Allocate and initialize the filesystem.
333          */
334         mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
335         mount_init(mp);
336         vfs_busy(mp, LK_NOWAIT);
337         mp->mnt_op = vfsp->vfc_vfsops;
338         mp->mnt_vfc = vfsp;
339         mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
340         vfsp->vfc_refcount++;
341         mp->mnt_stat.f_type = vfsp->vfc_typenum;
342         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
343         strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
344         mp->mnt_stat.f_owner = cred->cr_uid;
345         lwkt_gettoken(&mp->mnt_token);
346         vn_unlock(vp);
347 update:
348         /*
349          * (per-mount token acquired at this point)
350          *
351          * Set the mount level flags.
352          */
353         if (uap->flags & MNT_RDONLY)
354                 mp->mnt_flag |= MNT_RDONLY;
355         else if (mp->mnt_flag & MNT_RDONLY)
356                 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357         mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358             MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
359             MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
360             MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
361             MNT_AUTOMOUNTED);
362         mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
363             MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
364             MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
365             MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
366             MNT_AUTOMOUNTED);
367
368         /*
369          * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
370          * This way the initial VFS_MOUNT() call will also be MPSAFE.
371          */
372         if (vfsp->vfc_flags & VFCF_MPSAFE)
373                 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
374
375         /*
376          * Mount the filesystem.
377          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
378          * get. 
379          */
380         error = VFS_MOUNT(mp, uap->path, uap->data, cred);
381         if (mp->mnt_flag & MNT_UPDATE) {
382                 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
383                         mp->mnt_flag &= ~MNT_RDONLY;
384                 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
385                 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
386                 if (error) {
387                         mp->mnt_flag = flag;
388                         mp->mnt_kern_flag = flag2;
389                 }
390                 lwkt_reltoken(&mp->mnt_token);
391                 vfs_unbusy(mp);
392                 vrele(vp);
393                 cache_drop(&nch);
394                 goto done;
395         }
396         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
397
398         /*
399          * Put the new filesystem on the mount list after root.  The mount
400          * point gets its own mnt_ncmountpt (unless the VFS already set one
401          * up) which represents the root of the mount.  The lookup code
402          * detects the mount point going forward and checks the root of
403          * the mount going backwards.
404          *
405          * It is not necessary to invalidate or purge the vnode underneath
406          * because elements under the mount will be given their own glue
407          * namecache record.
408          */
409         if (!error) {
410                 if (mp->mnt_ncmountpt.ncp == NULL) {
411                         /* 
412                          * Allocate, then unlock, but leave the ref intact.
413                          * This is the mnt_refs (1) that we will retain
414                          * through to the unmount.
415                          */
416                         cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
417                         cache_unlock(&mp->mnt_ncmountpt);
418                 }
419                 vn_unlock(vp);
420                 mp->mnt_ncmounton = nch;                /* inherits ref */
421                 cache_lock(&nch);
422                 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
423                 cache_unlock(&nch);
424                 cache_ismounting(mp);
425                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
426
427                 mountlist_insert(mp, MNTINS_LAST);
428                 vn_unlock(vp);
429                 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
430                 error = vfs_allocate_syncvnode(mp);
431                 lwkt_reltoken(&mp->mnt_token);
432                 vfs_unbusy(mp);
433                 error = VFS_START(mp, 0);
434                 vrele(vp);
435                 KNOTE(&fs_klist, VQ_MOUNT);
436         } else {
437                 vn_syncer_thr_stop(mp);
438                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
439                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
440                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
441                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
442                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
443                 mp->mnt_vfc->vfc_refcount--;
444                 lwkt_reltoken(&mp->mnt_token);
445                 vfs_unbusy(mp);
446                 kfree(mp, M_MOUNT);
447                 cache_drop(&nch);
448                 vput(vp);
449         }
450 done:
451         return (error);
452 }
453
454 /*
455  * Scan all active processes to see if any of them have a current
456  * or root directory onto which the new filesystem has just been
457  * mounted. If so, replace them with the new mount point.
458  *
459  * Both old_nch and new_nch are ref'd on call but not locked.
460  * new_nch must be temporarily locked so it can be associated with the
461  * vnode representing the root of the mount point.
462  */
463 struct checkdirs_info {
464         struct nchandle old_nch;
465         struct nchandle new_nch;
466         struct vnode *old_vp;
467         struct vnode *new_vp;
468 };
469
470 static int checkdirs_callback(struct proc *p, void *data);
471
472 static void
473 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
474 {
475         struct checkdirs_info info;
476         struct vnode *olddp;
477         struct vnode *newdp;
478         struct mount *mp;
479
480         /*
481          * If the old mount point's vnode has a usecount of 1, it is not
482          * being held as a descriptor anywhere.
483          */
484         olddp = old_nch->ncp->nc_vp;
485         if (olddp == NULL || VREFCNT(olddp) == 1)
486                 return;
487
488         /*
489          * Force the root vnode of the new mount point to be resolved
490          * so we can update any matching processes.
491          */
492         mp = new_nch->mount;
493         if (VFS_ROOT(mp, &newdp))
494                 panic("mount: lost mount");
495         vn_unlock(newdp);
496         cache_lock(new_nch);
497         vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
498         cache_setunresolved(new_nch);
499         cache_setvp(new_nch, newdp);
500         cache_unlock(new_nch);
501
502         /*
503          * Special handling of the root node
504          */
505         if (rootvnode == olddp) {
506                 vref(newdp);
507                 vfs_cache_setroot(newdp, cache_hold(new_nch));
508         }
509
510         /*
511          * Pass newdp separately so the callback does not have to access
512          * it via new_nch->ncp->nc_vp.
513          */
514         info.old_nch = *old_nch;
515         info.new_nch = *new_nch;
516         info.new_vp = newdp;
517         allproc_scan(checkdirs_callback, &info, 0);
518         vput(newdp);
519 }
520
521 /*
522  * NOTE: callback is not MP safe because the scanned process's filedesc
523  * structure can be ripped out from under us, amoung other things.
524  */
525 static int
526 checkdirs_callback(struct proc *p, void *data)
527 {
528         struct checkdirs_info *info = data;
529         struct filedesc *fdp;
530         struct nchandle ncdrop1;
531         struct nchandle ncdrop2;
532         struct vnode *vprele1;
533         struct vnode *vprele2;
534
535         if ((fdp = p->p_fd) != NULL) {
536                 cache_zero(&ncdrop1);
537                 cache_zero(&ncdrop2);
538                 vprele1 = NULL;
539                 vprele2 = NULL;
540
541                 /*
542                  * MPUNSAFE - XXX fdp can be pulled out from under a
543                  * foreign process.
544                  *
545                  * A shared filedesc is ok, we don't have to copy it
546                  * because we are making this change globally.
547                  */
548                 spin_lock(&fdp->fd_spin);
549                 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
550                     fdp->fd_ncdir.ncp == info->old_nch.ncp) {
551                         vprele1 = fdp->fd_cdir;
552                         vref(info->new_vp);
553                         fdp->fd_cdir = info->new_vp;
554                         ncdrop1 = fdp->fd_ncdir;
555                         cache_copy(&info->new_nch, &fdp->fd_ncdir);
556                 }
557                 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
558                     fdp->fd_nrdir.ncp == info->old_nch.ncp) {
559                         vprele2 = fdp->fd_rdir;
560                         vref(info->new_vp);
561                         fdp->fd_rdir = info->new_vp;
562                         ncdrop2 = fdp->fd_nrdir;
563                         cache_copy(&info->new_nch, &fdp->fd_nrdir);
564                 }
565                 spin_unlock(&fdp->fd_spin);
566                 if (ncdrop1.ncp)
567                         cache_drop(&ncdrop1);
568                 if (ncdrop2.ncp)
569                         cache_drop(&ncdrop2);
570                 if (vprele1)
571                         vrele(vprele1);
572                 if (vprele2)
573                         vrele(vprele2);
574         }
575         return(0);
576 }
577
578 /*
579  * Unmount a file system.
580  *
581  * Note: unmount takes a path to the vnode mounted on as argument,
582  * not special file (as before).
583  *
584  * umount_args(char *path, int flags)
585  *
586  * MPALMOSTSAFE
587  */
588 int
589 sys_unmount(struct unmount_args *uap)
590 {
591         struct thread *td = curthread;
592         struct proc *p __debugvar = td->td_proc;
593         struct mount *mp = NULL;
594         struct nlookupdata nd;
595         int error;
596
597         KKASSERT(p);
598         if (td->td_ucred->cr_prison != NULL) {
599                 error = EPERM;
600                 goto done;
601         }
602         if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
603                 goto done;
604
605         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
606         if (error == 0)
607                 error = nlookup(&nd);
608         if (error)
609                 goto out;
610
611         mp = nd.nl_nch.mount;
612
613         /*
614          * Only root, or the user that did the original mount is
615          * permitted to unmount this filesystem.
616          */
617         if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
618             (error = priv_check(td, PRIV_ROOT)))
619                 goto out;
620
621         /*
622          * Don't allow unmounting the root file system.
623          */
624         if (mp->mnt_flag & MNT_ROOTFS) {
625                 error = EINVAL;
626                 goto out;
627         }
628
629         /*
630          * Must be the root of the filesystem
631          */
632         if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
633                 error = EINVAL;
634                 goto out;
635         }
636
637         /*
638          * If no error try to issue the unmount.  We lose our cache
639          * ref when we call nlookup_done so we must hold the mount point
640          * to prevent use-after-free races.
641          */
642 out:
643         if (error == 0) {
644                 mount_hold(mp);
645                 nlookup_done(&nd);
646                 error = dounmount(mp, uap->flags, 0);
647                 mount_drop(mp);
648         } else {
649                 nlookup_done(&nd);
650         }
651 done:
652         return (error);
653 }
654
655 /*
656  * Do the actual file system unmount (interlocked against the mountlist
657  * token and mp->mnt_token).
658  */
659 static int
660 dounmount_interlock(struct mount *mp)
661 {
662         if (mp->mnt_kern_flag & MNTK_UNMOUNT)
663                 return (EBUSY);
664         mp->mnt_kern_flag |= MNTK_UNMOUNT;
665         return(0);
666 }
667
668 static int
669 unmount_allproc_cb(struct proc *p, void *arg)
670 {
671         struct mount *mp;
672
673         if (p->p_textnch.ncp == NULL)
674                 return 0;
675
676         mp = (struct mount *)arg;
677         if (p->p_textnch.mount == mp)
678                 cache_drop(&p->p_textnch);
679
680         return 0;
681 }
682
683 /*
684  * The guts of the unmount code.  The mount owns one ref and one hold
685  * count.  If we successfully interlock the unmount, those refs are ours.
686  * (The ref is from mnt_ncmountpt).
687  *
688  * When halting we shortcut certain mount types such as devfs by not actually
689  * issuing the VFS_SYNC() or VFS_UNMOUNT().  They are still disconnected
690  * from the mountlist so higher-level filesytems can unmount cleanly.
691  *
692  * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
693  */
694 int
695 dounmount(struct mount *mp, int flags, int halting)
696 {
697         struct namecache *ncp;
698         struct nchandle nch;
699         struct vnode *vp;
700         int error;
701         int async_flag;
702         int lflags;
703         int freeok = 1;
704         int retry;
705         int quickhalt;
706
707         lwkt_gettoken(&mp->mnt_token);
708
709         /*
710          * When halting, certain mount points can essentially just
711          * be unhooked and otherwise ignored.
712          */
713         if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
714                 quickhalt = 1;
715                 freeok = 0;
716         } else {
717                 quickhalt = 0;
718         }
719
720
721         /*
722          * Exclusive access for unmounting purposes.
723          */
724         if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
725                 goto out;
726
727         /*
728          * We now 'own' the last mp->mnt_refs
729          *
730          * Allow filesystems to detect that a forced unmount is in progress.
731          */
732         if (flags & MNT_FORCE)
733                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
734         lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
735         error = lockmgr(&mp->mnt_lock, lflags);
736         if (error) {
737                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
738                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
739                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
740                         wakeup(mp);
741                 }
742                 goto out;
743         }
744
745         if (mp->mnt_flag & MNT_EXPUBLIC)
746                 vfs_setpublicfs(NULL, NULL, NULL);
747
748         vfs_msync(mp, MNT_WAIT);
749         async_flag = mp->mnt_flag & MNT_ASYNC;
750         mp->mnt_flag &=~ MNT_ASYNC;
751
752         /*
753          * If this filesystem isn't aliasing other filesystems,
754          * try to invalidate any remaining namecache entries and
755          * check the count afterwords.
756          *
757          * We own the last mnt_refs by owning mnt_ncmountpt.
758          */
759         if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
760                 cache_lock(&mp->mnt_ncmountpt);
761                 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
762                 cache_unlock(&mp->mnt_ncmountpt);
763
764                 cache_clearmntcache();
765                 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
766                     (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
767                         allproc_scan(&unmount_allproc_cb, mp, 0);
768                 }
769
770                 cache_clearmntcache();
771                 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
772                     (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
773
774                         if ((flags & MNT_FORCE) == 0) {
775                                 error = EBUSY;
776                                 mount_warning(mp, "Cannot unmount: "
777                                                   "%d namecache "
778                                                   "references still "
779                                                   "present",
780                                                   ncp->nc_refs - 1);
781                         } else {
782                                 mount_warning(mp, "Forced unmount: "
783                                                   "%d namecache "
784                                                   "references still "
785                                                   "present",
786                                                   ncp->nc_refs - 1);
787                                 freeok = 0;
788                         }
789                 }
790         }
791
792         /*
793          * Decomission our special mnt_syncer vnode.  This also stops
794          * the vnlru code.  If we are unable to unmount we recommission
795          * the vnode.
796          *
797          * Then sync the filesystem.
798          */
799         if ((vp = mp->mnt_syncer) != NULL) {
800                 mp->mnt_syncer = NULL;
801                 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
802                 vrele(vp);
803         }
804
805         if (quickhalt == 0) {
806                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
807                         VFS_SYNC(mp, MNT_WAIT);
808         }
809
810         /*
811          * nchandle records ref the mount structure.  Expect a count of 1
812          * (our mount->mnt_ncmountpt).
813          *
814          * Scans can get temporary refs on a mountpoint (thought really
815          * heavy duty stuff like cache_findmount() do not).
816          */
817         if (mp->mnt_refs != 1)
818                 cache_clearmntcache();
819         for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
820                 cache_unmounting(mp);
821                 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
822                 cache_clearmntcache();
823         }
824         if (mp->mnt_refs != 1) {
825                 if ((flags & MNT_FORCE) == 0) {
826                         mount_warning(mp, "Cannot unmount: "
827                                           "%d mount refs still present",
828                                           mp->mnt_refs - 1);
829                         error = EBUSY;
830                 } else {
831                         mount_warning(mp, "Forced unmount: "
832                                           "%d mount refs still present",
833                                           mp->mnt_refs - 1);
834                         freeok = 0;
835                 }
836         }
837
838         /*
839          * So far so good, sync the filesystem once more and
840          * call the VFS unmount code if the sync succeeds.
841          */
842         if (error == 0 && quickhalt == 0) {
843                 if (mp->mnt_flag & MNT_RDONLY) {
844                         error = VFS_UNMOUNT(mp, flags);
845                 } else {
846                         error = VFS_SYNC(mp, MNT_WAIT);
847                         if ((error == 0) ||
848                             (error == EOPNOTSUPP) || /* No sync */
849                             (flags & MNT_FORCE)) {
850                                 error = VFS_UNMOUNT(mp, flags);
851                         }
852                 }
853         }
854
855         /*
856          * If an error occurred we can still recover, restoring the
857          * syncer vnode and misc flags.
858          */
859         if (error) {
860                 if (mp->mnt_syncer == NULL)
861                         vfs_allocate_syncvnode(mp);
862                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
863                 mp->mnt_flag |= async_flag;
864                 lockmgr(&mp->mnt_lock, LK_RELEASE);
865                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
866                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
867                         wakeup(mp);
868                 }
869                 goto out;
870         }
871         /*
872          * Clean up any journals still associated with the mount after
873          * filesystem activity has ceased.
874          */
875         journal_remove_all_journals(mp, 
876             ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
877
878         mountlist_remove(mp);
879
880         /*
881          * Remove any installed vnode ops here so the individual VFSs don't
882          * have to.
883          *
884          * mnt_refs should go to zero when we scrap mnt_ncmountpt.
885          *
886          * When quickhalting we have to keep these intact because the
887          * underlying vnodes have not been destroyed, and some might be
888          * dirty.
889          */
890         if (quickhalt == 0) {
891                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
892                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
893                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
894                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
895                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
896         }
897
898         if (mp->mnt_ncmountpt.ncp != NULL) {
899                 nch = mp->mnt_ncmountpt;
900                 cache_zero(&mp->mnt_ncmountpt);
901                 cache_clrmountpt(&nch);
902                 cache_drop(&nch);
903         }
904         if (mp->mnt_ncmounton.ncp != NULL) {
905                 cache_unmounting(mp);
906                 nch = mp->mnt_ncmounton;
907                 cache_zero(&mp->mnt_ncmounton);
908                 cache_clrmountpt(&nch);
909                 cache_drop(&nch);
910         }
911
912         mp->mnt_vfc->vfc_refcount--;
913
914         /*
915          * If not quickhalting the mount, we expect there to be no
916          * vnodes left.
917          */
918         if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
919                 panic("unmount: dangling vnode");
920
921         /*
922          * Release the lock
923          */
924         lockmgr(&mp->mnt_lock, LK_RELEASE);
925         if (mp->mnt_kern_flag & MNTK_MWAIT) {
926                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
927                 wakeup(mp);
928         }
929
930         /*
931          * If we reach here and freeok != 0 we must free the mount.
932          * mnt_refs should already have dropped to 0, so if it is not
933          * zero we must cycle the caches and wait.
934          *
935          * When we are satisfied that the mount has disconnected we can
936          * drop the hold on the mp that represented the mount (though the
937          * caller might actually have another, so the caller's drop may
938          * do the actual free).
939          */
940         if (freeok) {
941                 if (mp->mnt_refs > 0)
942                         cache_clearmntcache();
943                 while (mp->mnt_refs > 0) {
944                         cache_unmounting(mp);
945                         wakeup(mp);
946                         tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
947                         cache_clearmntcache();
948                 }
949                 lwkt_reltoken(&mp->mnt_token);
950                 mount_drop(mp);
951                 mp = NULL;
952         } else {
953                 cache_clearmntcache();
954         }
955         error = 0;
956         KNOTE(&fs_klist, VQ_UNMOUNT);
957 out:
958         if (mp)
959                 lwkt_reltoken(&mp->mnt_token);
960         return (error);
961 }
962
963 static
964 void
965 mount_warning(struct mount *mp, const char *ctl, ...)
966 {
967         char *ptr;
968         char *buf;
969         __va_list va;
970
971         __va_start(va, ctl);
972         if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
973                            &ptr, &buf, 0) == 0) {
974                 kprintf("unmount(%s): ", ptr);
975                 kvprintf(ctl, va);
976                 kprintf("\n");
977                 kfree(buf, M_TEMP);
978         } else {
979                 kprintf("unmount(%p", mp);
980                 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
981                         kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
982                 kprintf("): ");
983                 kvprintf(ctl, va);
984                 kprintf("\n");
985         }
986         __va_end(va);
987 }
988
989 /*
990  * Shim cache_fullpath() to handle the case where a process is chrooted into
991  * a subdirectory of a mount.  In this case if the root mount matches the
992  * process root directory's mount we have to specify the process's root
993  * directory instead of the mount point, because the mount point might
994  * be above the root directory.
995  */
996 static
997 int
998 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
999 {
1000         struct nchandle *nch;
1001
1002         if (p && p->p_fd->fd_nrdir.mount == mp)
1003                 nch = &p->p_fd->fd_nrdir;
1004         else
1005                 nch = &mp->mnt_ncmountpt;
1006         return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1007 }
1008
1009 /*
1010  * Sync each mounted filesystem.
1011  */
1012
1013 #ifdef DEBUG
1014 static int syncprt = 0;
1015 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1016 #endif /* DEBUG */
1017
1018 static int sync_callback(struct mount *mp, void *data);
1019
1020 int
1021 sys_sync(struct sync_args *uap)
1022 {
1023         mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1024         return (0);
1025 }
1026
1027 static
1028 int
1029 sync_callback(struct mount *mp, void *data __unused)
1030 {
1031         int asyncflag;
1032
1033         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1034                 lwkt_gettoken(&mp->mnt_token);
1035                 asyncflag = mp->mnt_flag & MNT_ASYNC;
1036                 mp->mnt_flag &= ~MNT_ASYNC;
1037                 lwkt_reltoken(&mp->mnt_token);
1038                 vfs_msync(mp, MNT_NOWAIT);
1039                 VFS_SYNC(mp, MNT_NOWAIT);
1040                 lwkt_gettoken(&mp->mnt_token);
1041                 mp->mnt_flag |= asyncflag;
1042                 lwkt_reltoken(&mp->mnt_token);
1043         }
1044         return(0);
1045 }
1046
1047 /* XXX PRISON: could be per prison flag */
1048 static int prison_quotas;
1049 #if 0
1050 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1051 #endif
1052
1053 /*
1054  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1055  *
1056  * Change filesystem quotas.
1057  *
1058  * MPALMOSTSAFE
1059  */
1060 int
1061 sys_quotactl(struct quotactl_args *uap)
1062 {
1063         struct nlookupdata nd;
1064         struct thread *td;
1065         struct mount *mp;
1066         int error;
1067
1068         td = curthread;
1069         if (td->td_ucred->cr_prison && !prison_quotas) {
1070                 error = EPERM;
1071                 goto done;
1072         }
1073
1074         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1075         if (error == 0)
1076                 error = nlookup(&nd);
1077         if (error == 0) {
1078                 mp = nd.nl_nch.mount;
1079                 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1080                                     uap->arg, nd.nl_cred);
1081         }
1082         nlookup_done(&nd);
1083 done:
1084         return (error);
1085 }
1086
1087 /*
1088  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1089  *              void *buf, int buflen)
1090  *
1091  * This function operates on a mount point and executes the specified
1092  * operation using the specified control data, and possibly returns data.
1093  *
1094  * The actual number of bytes stored in the result buffer is returned, 0
1095  * if none, otherwise an error is returned.
1096  *
1097  * MPALMOSTSAFE
1098  */
1099 int
1100 sys_mountctl(struct mountctl_args *uap)
1101 {
1102         struct thread *td = curthread;
1103         struct file *fp;
1104         void *ctl = NULL;
1105         void *buf = NULL;
1106         char *path = NULL;
1107         int error;
1108
1109         /*
1110          * Sanity and permissions checks.  We must be root.
1111          */
1112         if (td->td_ucred->cr_prison != NULL)
1113                 return (EPERM);
1114         if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1115             (error = priv_check(td, PRIV_ROOT)) != 0)
1116                 return (error);
1117
1118         /*
1119          * Argument length checks
1120          */
1121         if (uap->ctllen < 0 || uap->ctllen > 1024)
1122                 return (EINVAL);
1123         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1124                 return (EINVAL);
1125         if (uap->path == NULL)
1126                 return (EINVAL);
1127
1128         /*
1129          * Allocate the necessary buffers and copyin data
1130          */
1131         path = objcache_get(namei_oc, M_WAITOK);
1132         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1133         if (error)
1134                 goto done;
1135
1136         if (uap->ctllen) {
1137                 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1138                 error = copyin(uap->ctl, ctl, uap->ctllen);
1139                 if (error)
1140                         goto done;
1141         }
1142         if (uap->buflen)
1143                 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1144
1145         /*
1146          * Validate the descriptor
1147          */
1148         if (uap->fd >= 0) {
1149                 fp = holdfp(td, uap->fd, -1);
1150                 if (fp == NULL) {
1151                         error = EBADF;
1152                         goto done;
1153                 }
1154         } else {
1155                 fp = NULL;
1156         }
1157
1158         /*
1159          * Execute the internal kernel function and clean up.
1160          */
1161         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1162                               buf, uap->buflen, &uap->sysmsg_result);
1163         if (fp)
1164                 dropfp(td, uap->fd, fp);
1165         if (error == 0 && uap->sysmsg_result > 0)
1166                 error = copyout(buf, uap->buf, uap->sysmsg_result);
1167 done:
1168         if (path)
1169                 objcache_put(namei_oc, path);
1170         if (ctl)
1171                 kfree(ctl, M_TEMP);
1172         if (buf)
1173                 kfree(buf, M_TEMP);
1174         return (error);
1175 }
1176
1177 /*
1178  * Execute a mount control operation by resolving the path to a mount point
1179  * and calling vop_mountctl().  
1180  *
1181  * Use the mount point from the nch instead of the vnode so nullfs mounts
1182  * can properly spike the VOP.
1183  */
1184 int
1185 kern_mountctl(const char *path, int op, struct file *fp, 
1186                 const void *ctl, int ctllen, 
1187                 void *buf, int buflen, int *res)
1188 {
1189         struct vnode *vp;
1190         struct nlookupdata nd;
1191         struct nchandle nch;
1192         struct mount *mp;
1193         int error;
1194
1195         *res = 0;
1196         vp = NULL;
1197         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1198         if (error)
1199                 return (error);
1200         error = nlookup(&nd);
1201         if (error) {
1202                 nlookup_done(&nd);
1203                 return (error);
1204         }
1205         error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1206         if (error) {
1207                 nlookup_done(&nd);
1208                 return (error);
1209         }
1210
1211         /*
1212          * Yes, all this is needed to use the nch.mount below, because
1213          * we must maintain a ref on the mount to avoid ripouts (e.g.
1214          * due to heavy mount/unmount use by synth or poudriere).
1215          */
1216         nch = nd.nl_nch;
1217         cache_zero(&nd.nl_nch);
1218         cache_unlock(&nch);
1219         nlookup_done(&nd);
1220         vn_unlock(vp);
1221
1222         mp = nch.mount;
1223
1224         /*
1225          * Must be the root of the filesystem
1226          */
1227         if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1228                 cache_drop(&nch);
1229                 vrele(vp);
1230                 return (EINVAL);
1231         }
1232         if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1233                 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1234                         path);
1235                 cache_drop(&nch);
1236                 vrele(vp);
1237                 return (EINVAL);
1238         }
1239         error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1240                              buf, buflen, res);
1241         vrele(vp);
1242         cache_drop(&nch);
1243
1244         return (error);
1245 }
1246
1247 int
1248 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1249 {
1250         struct thread *td = curthread;
1251         struct proc *p = td->td_proc;
1252         struct mount *mp;
1253         struct statfs *sp;
1254         char *fullpath, *freepath;
1255         int error;
1256
1257         if ((error = nlookup(nd)) != 0)
1258                 return (error);
1259         mp = nd->nl_nch.mount;
1260         sp = &mp->mnt_stat;
1261         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1262                 return (error);
1263
1264         error = mount_path(p, mp, &fullpath, &freepath);
1265         if (error)
1266                 return(error);
1267         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1268         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1269         kfree(freepath, M_TEMP);
1270
1271         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1272         bcopy(sp, buf, sizeof(*buf));
1273         /* Only root should have access to the fsid's. */
1274         if (priv_check(td, PRIV_ROOT))
1275                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1276         return (0);
1277 }
1278
1279 /*
1280  * statfs_args(char *path, struct statfs *buf)
1281  *
1282  * Get filesystem statistics.
1283  */
1284 int
1285 sys_statfs(struct statfs_args *uap)
1286 {
1287         struct nlookupdata nd;
1288         struct statfs buf;
1289         int error;
1290
1291         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1292         if (error == 0)
1293                 error = kern_statfs(&nd, &buf);
1294         nlookup_done(&nd);
1295         if (error == 0)
1296                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1297         return (error);
1298 }
1299
1300 int
1301 kern_fstatfs(int fd, struct statfs *buf)
1302 {
1303         struct thread *td = curthread;
1304         struct proc *p = td->td_proc;
1305         struct file *fp;
1306         struct mount *mp;
1307         struct statfs *sp;
1308         char *fullpath, *freepath;
1309         int error;
1310
1311         KKASSERT(p);
1312         if ((error = holdvnode(td, fd, &fp)) != 0)
1313                 return (error);
1314
1315         /*
1316          * Try to use mount info from any overlays rather than the
1317          * mount info for the underlying vnode, otherwise we will
1318          * fail when operating on null-mounted paths inside a chroot.
1319          */
1320         if ((mp = fp->f_nchandle.mount) == NULL)
1321                 mp = ((struct vnode *)fp->f_data)->v_mount;
1322         if (mp == NULL) {
1323                 error = EBADF;
1324                 goto done;
1325         }
1326         if (fp->f_cred == NULL) {
1327                 error = EINVAL;
1328                 goto done;
1329         }
1330         sp = &mp->mnt_stat;
1331         if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1332                 goto done;
1333
1334         if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1335                 goto done;
1336         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1337         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1338         kfree(freepath, M_TEMP);
1339
1340         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1341         bcopy(sp, buf, sizeof(*buf));
1342
1343         /* Only root should have access to the fsid's. */
1344         if (priv_check(td, PRIV_ROOT))
1345                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1346         error = 0;
1347 done:
1348         fdrop(fp);
1349         return (error);
1350 }
1351
1352 /*
1353  * fstatfs_args(int fd, struct statfs *buf)
1354  *
1355  * Get filesystem statistics.
1356  */
1357 int
1358 sys_fstatfs(struct fstatfs_args *uap)
1359 {
1360         struct statfs buf;
1361         int error;
1362
1363         error = kern_fstatfs(uap->fd, &buf);
1364
1365         if (error == 0)
1366                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1367         return (error);
1368 }
1369
1370 int
1371 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1372 {
1373         struct mount *mp;
1374         struct statvfs *sp;
1375         int error;
1376
1377         if ((error = nlookup(nd)) != 0)
1378                 return (error);
1379         mp = nd->nl_nch.mount;
1380         sp = &mp->mnt_vstat;
1381         if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1382                 return (error);
1383
1384         sp->f_flag = 0;
1385         if (mp->mnt_flag & MNT_RDONLY)
1386                 sp->f_flag |= ST_RDONLY;
1387         if (mp->mnt_flag & MNT_NOSUID)
1388                 sp->f_flag |= ST_NOSUID;
1389         bcopy(sp, buf, sizeof(*buf));
1390         return (0);
1391 }
1392
1393 /*
1394  * statfs_args(char *path, struct statfs *buf)
1395  *
1396  * Get filesystem statistics.
1397  */
1398 int
1399 sys_statvfs(struct statvfs_args *uap)
1400 {
1401         struct nlookupdata nd;
1402         struct statvfs buf;
1403         int error;
1404
1405         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1406         if (error == 0)
1407                 error = kern_statvfs(&nd, &buf);
1408         nlookup_done(&nd);
1409         if (error == 0)
1410                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1411         return (error);
1412 }
1413
1414 int
1415 kern_fstatvfs(int fd, struct statvfs *buf)
1416 {
1417         struct thread *td = curthread;
1418         struct file *fp;
1419         struct mount *mp;
1420         struct statvfs *sp;
1421         int error;
1422
1423         if ((error = holdvnode(td, fd, &fp)) != 0)
1424                 return (error);
1425         if ((mp = fp->f_nchandle.mount) == NULL)
1426                 mp = ((struct vnode *)fp->f_data)->v_mount;
1427         if (mp == NULL) {
1428                 error = EBADF;
1429                 goto done;
1430         }
1431         if (fp->f_cred == NULL) {
1432                 error = EINVAL;
1433                 goto done;
1434         }
1435         sp = &mp->mnt_vstat;
1436         if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1437                 goto done;
1438
1439         sp->f_flag = 0;
1440         if (mp->mnt_flag & MNT_RDONLY)
1441                 sp->f_flag |= ST_RDONLY;
1442         if (mp->mnt_flag & MNT_NOSUID)
1443                 sp->f_flag |= ST_NOSUID;
1444
1445         bcopy(sp, buf, sizeof(*buf));
1446         error = 0;
1447 done:
1448         fdrop(fp);
1449         return (error);
1450 }
1451
1452 /*
1453  * fstatfs_args(int fd, struct statfs *buf)
1454  *
1455  * Get filesystem statistics.
1456  */
1457 int
1458 sys_fstatvfs(struct fstatvfs_args *uap)
1459 {
1460         struct statvfs buf;
1461         int error;
1462
1463         error = kern_fstatvfs(uap->fd, &buf);
1464
1465         if (error == 0)
1466                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1467         return (error);
1468 }
1469
1470 /*
1471  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1472  *
1473  * Get statistics on all filesystems.
1474  */
1475
1476 struct getfsstat_info {
1477         struct statfs *sfsp;
1478         long count;
1479         long maxcount;
1480         int error;
1481         int flags;
1482         struct thread *td;
1483 };
1484
1485 static int getfsstat_callback(struct mount *, void *);
1486
1487 int
1488 sys_getfsstat(struct getfsstat_args *uap)
1489 {
1490         struct thread *td = curthread;
1491         struct getfsstat_info info;
1492
1493         bzero(&info, sizeof(info));
1494
1495         info.maxcount = uap->bufsize / sizeof(struct statfs);
1496         info.sfsp = uap->buf;
1497         info.count = 0;
1498         info.flags = uap->flags;
1499         info.td = td;
1500
1501         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1502         if (info.sfsp && info.count > info.maxcount)
1503                 uap->sysmsg_result = info.maxcount;
1504         else
1505                 uap->sysmsg_result = info.count;
1506         return (info.error);
1507 }
1508
1509 static int
1510 getfsstat_callback(struct mount *mp, void *data)
1511 {
1512         struct getfsstat_info *info = data;
1513         struct statfs *sp;
1514         char *freepath;
1515         char *fullpath;
1516         int error;
1517
1518         if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1519                 return(0);
1520
1521         if (info->sfsp && info->count < info->maxcount) {
1522                 sp = &mp->mnt_stat;
1523
1524                 /*
1525                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1526                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1527                  * overrides MNT_WAIT.
1528                  */
1529                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1530                     (info->flags & MNT_WAIT)) &&
1531                     (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1532                         return(0);
1533                 }
1534                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1535
1536                 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1537                 if (error) {
1538                         info->error = error;
1539                         return(-1);
1540                 }
1541                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1542                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1543                 kfree(freepath, M_TEMP);
1544
1545                 error = copyout(sp, info->sfsp, sizeof(*sp));
1546                 if (error) {
1547                         info->error = error;
1548                         return (-1);
1549                 }
1550                 ++info->sfsp;
1551         }
1552         info->count++;
1553         return(0);
1554 }
1555
1556 /*
1557  * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1558                    long bufsize, int flags)
1559  *
1560  * Get statistics on all filesystems.
1561  */
1562
1563 struct getvfsstat_info {
1564         struct statfs *sfsp;
1565         struct statvfs *vsfsp;
1566         long count;
1567         long maxcount;
1568         int error;
1569         int flags;
1570         struct thread *td;
1571 };
1572
1573 static int getvfsstat_callback(struct mount *, void *);
1574
1575 int
1576 sys_getvfsstat(struct getvfsstat_args *uap)
1577 {
1578         struct thread *td = curthread;
1579         struct getvfsstat_info info;
1580
1581         bzero(&info, sizeof(info));
1582
1583         info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1584         info.sfsp = uap->buf;
1585         info.vsfsp = uap->vbuf;
1586         info.count = 0;
1587         info.flags = uap->flags;
1588         info.td = td;
1589
1590         mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1591         if (info.vsfsp && info.count > info.maxcount)
1592                 uap->sysmsg_result = info.maxcount;
1593         else
1594                 uap->sysmsg_result = info.count;
1595         return (info.error);
1596 }
1597
1598 static int
1599 getvfsstat_callback(struct mount *mp, void *data)
1600 {
1601         struct getvfsstat_info *info = data;
1602         struct statfs *sp;
1603         struct statvfs *vsp;
1604         char *freepath;
1605         char *fullpath;
1606         int error;
1607
1608         if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1609                 return(0);
1610
1611         if (info->vsfsp && info->count < info->maxcount) {
1612                 sp = &mp->mnt_stat;
1613                 vsp = &mp->mnt_vstat;
1614
1615                 /*
1616                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1617                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1618                  * overrides MNT_WAIT.
1619                  */
1620                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1621                     (info->flags & MNT_WAIT)) &&
1622                     (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1623                         return(0);
1624                 }
1625                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1626
1627                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1628                     (info->flags & MNT_WAIT)) &&
1629                     (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1630                         return(0);
1631                 }
1632                 vsp->f_flag = 0;
1633                 if (mp->mnt_flag & MNT_RDONLY)
1634                         vsp->f_flag |= ST_RDONLY;
1635                 if (mp->mnt_flag & MNT_NOSUID)
1636                         vsp->f_flag |= ST_NOSUID;
1637
1638                 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1639                 if (error) {
1640                         info->error = error;
1641                         return(-1);
1642                 }
1643                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1644                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1645                 kfree(freepath, M_TEMP);
1646
1647                 error = copyout(sp, info->sfsp, sizeof(*sp));
1648                 if (error == 0)
1649                         error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1650                 if (error) {
1651                         info->error = error;
1652                         return (-1);
1653                 }
1654                 ++info->sfsp;
1655                 ++info->vsfsp;
1656         }
1657         info->count++;
1658         return(0);
1659 }
1660
1661
1662 /*
1663  * fchdir_args(int fd)
1664  *
1665  * Change current working directory to a given file descriptor.
1666  */
1667 int
1668 sys_fchdir(struct fchdir_args *uap)
1669 {
1670         struct thread *td = curthread;
1671         struct proc *p = td->td_proc;
1672         struct filedesc *fdp = p->p_fd;
1673         struct vnode *vp, *ovp;
1674         struct mount *mp;
1675         struct file *fp;
1676         struct nchandle nch, onch, tnch;
1677         int error;
1678
1679         if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1680                 return (error);
1681         lwkt_gettoken(&p->p_token);
1682         vp = (struct vnode *)fp->f_data;
1683         vref(vp);
1684         vn_lock(vp, LK_SHARED | LK_RETRY);
1685         if (fp->f_nchandle.ncp == NULL)
1686                 error = ENOTDIR;
1687         else
1688                 error = checkvp_chdir(vp, td);
1689         if (error) {
1690                 vput(vp);
1691                 goto done;
1692         }
1693         cache_copy(&fp->f_nchandle, &nch);
1694
1695         /*
1696          * If the ncp has become a mount point, traverse through
1697          * the mount point.
1698          */
1699
1700         while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1701                (mp = cache_findmount(&nch)) != NULL
1702         ) {
1703                 error = nlookup_mp(mp, &tnch);
1704                 if (error == 0) {
1705                         cache_unlock(&tnch);    /* leave ref intact */
1706                         vput(vp);
1707                         vp = tnch.ncp->nc_vp;
1708                         error = vget(vp, LK_SHARED);
1709                         KKASSERT(error == 0);
1710                         cache_drop(&nch);
1711                         nch = tnch;
1712                 }
1713                 cache_dropmount(mp);
1714         }
1715         if (error == 0) {
1716                 spin_lock(&fdp->fd_spin);
1717                 ovp = fdp->fd_cdir;
1718                 onch = fdp->fd_ncdir;
1719                 fdp->fd_cdir = vp;
1720                 fdp->fd_ncdir = nch;
1721                 spin_unlock(&fdp->fd_spin);
1722                 vn_unlock(vp);          /* leave ref intact */
1723                 cache_drop(&onch);
1724                 vrele(ovp);
1725         } else {
1726                 cache_drop(&nch);
1727                 vput(vp);
1728         }
1729         fdrop(fp);
1730 done:
1731         lwkt_reltoken(&p->p_token);
1732         return (error);
1733 }
1734
1735 int
1736 kern_chdir(struct nlookupdata *nd)
1737 {
1738         struct thread *td = curthread;
1739         struct proc *p = td->td_proc;
1740         struct filedesc *fdp = p->p_fd;
1741         struct vnode *vp, *ovp;
1742         struct nchandle onch;
1743         int error;
1744
1745         nd->nl_flags |= NLC_SHAREDLOCK;
1746         if ((error = nlookup(nd)) != 0)
1747                 return (error);
1748         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1749                 return (ENOENT);
1750         if ((error = vget(vp, LK_SHARED)) != 0)
1751                 return (error);
1752
1753         lwkt_gettoken(&p->p_token);
1754         error = checkvp_chdir(vp, td);
1755         vn_unlock(vp);
1756         if (error == 0) {
1757                 spin_lock(&fdp->fd_spin);
1758                 ovp = fdp->fd_cdir;
1759                 onch = fdp->fd_ncdir;
1760                 fdp->fd_ncdir = nd->nl_nch;
1761                 fdp->fd_cdir = vp;
1762                 spin_unlock(&fdp->fd_spin);
1763                 cache_unlock(&nd->nl_nch);      /* leave reference intact */
1764                 cache_drop(&onch);
1765                 vrele(ovp);
1766                 cache_zero(&nd->nl_nch);
1767         } else {
1768                 vrele(vp);
1769         }
1770         lwkt_reltoken(&p->p_token);
1771         return (error);
1772 }
1773
1774 /*
1775  * chdir_args(char *path)
1776  *
1777  * Change current working directory (``.'').
1778  */
1779 int
1780 sys_chdir(struct chdir_args *uap)
1781 {
1782         struct nlookupdata nd;
1783         int error;
1784
1785         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1786         if (error == 0)
1787                 error = kern_chdir(&nd);
1788         nlookup_done(&nd);
1789         return (error);
1790 }
1791
1792 /*
1793  * Helper function for raised chroot(2) security function:  Refuse if
1794  * any filedescriptors are open directories.
1795  */
1796 static int
1797 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1798 {
1799         struct vnode *vp;
1800         struct file *fp;
1801         int error;
1802         int fd;
1803
1804         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1805                 if ((error = holdvnode(td, fd, &fp)) != 0)
1806                         continue;
1807                 vp = (struct vnode *)fp->f_data;
1808                 if (vp->v_type != VDIR) {
1809                         fdrop(fp);
1810                         continue;
1811                 }
1812                 fdrop(fp);
1813                 return(EPERM);
1814         }
1815         return (0);
1816 }
1817
1818 /*
1819  * This sysctl determines if we will allow a process to chroot(2) if it
1820  * has a directory open:
1821  *      0: disallowed for all processes.
1822  *      1: allowed for processes that were not already chroot(2)'ed.
1823  *      2: allowed for all processes.
1824  */
1825
1826 static int chroot_allow_open_directories = 1;
1827
1828 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1829      &chroot_allow_open_directories, 0, "");
1830
1831 /*
1832  * chroot to the specified namecache entry.  We obtain the vp from the
1833  * namecache data.  The passed ncp must be locked and referenced and will
1834  * remain locked and referenced on return.
1835  */
1836 int
1837 kern_chroot(struct nchandle *nch)
1838 {
1839         struct thread *td = curthread;
1840         struct proc *p = td->td_proc;
1841         struct filedesc *fdp = p->p_fd;
1842         struct vnode *vp;
1843         int error;
1844
1845         /*
1846          * Only privileged user can chroot
1847          */
1848         error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1849         if (error)
1850                 return (error);
1851
1852         /*
1853          * Disallow open directory descriptors (fchdir() breakouts).
1854          */
1855         if (chroot_allow_open_directories == 0 ||
1856            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1857                 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1858                         return (error);
1859         }
1860         if ((vp = nch->ncp->nc_vp) == NULL)
1861                 return (ENOENT);
1862
1863         if ((error = vget(vp, LK_SHARED)) != 0)
1864                 return (error);
1865
1866         /*
1867          * Check the validity of vp as a directory to change to and 
1868          * associate it with rdir/jdir.
1869          */
1870         error = checkvp_chdir(vp, td);
1871         vn_unlock(vp);                  /* leave reference intact */
1872         if (error == 0) {
1873                 lwkt_gettoken(&p->p_token);
1874                 vrele(fdp->fd_rdir);
1875                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1876                 cache_drop(&fdp->fd_nrdir);
1877                 cache_copy(nch, &fdp->fd_nrdir);
1878                 if (fdp->fd_jdir == NULL) {
1879                         fdp->fd_jdir = vp;
1880                         vref(fdp->fd_jdir);
1881                         cache_copy(nch, &fdp->fd_njdir);
1882                 }
1883                 if ((p->p_flags & P_DIDCHROOT) == 0) {
1884                         p->p_flags |= P_DIDCHROOT;
1885                         if (p->p_depth <= 65535 - 32)
1886                                 p->p_depth += 32;
1887                 }
1888                 lwkt_reltoken(&p->p_token);
1889         } else {
1890                 vrele(vp);
1891         }
1892         return (error);
1893 }
1894
1895 /*
1896  * chroot_args(char *path)
1897  *
1898  * Change notion of root (``/'') directory.
1899  */
1900 int
1901 sys_chroot(struct chroot_args *uap)
1902 {
1903         struct thread *td __debugvar = curthread;
1904         struct nlookupdata nd;
1905         int error;
1906
1907         KKASSERT(td->td_proc);
1908         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1909         if (error == 0) {
1910                 nd.nl_flags |= NLC_EXEC;
1911                 error = nlookup(&nd);
1912                 if (error == 0)
1913                         error = kern_chroot(&nd.nl_nch);
1914         }
1915         nlookup_done(&nd);
1916         return(error);
1917 }
1918
1919 int
1920 sys_chroot_kernel(struct chroot_kernel_args *uap)
1921 {
1922         struct thread *td = curthread;
1923         struct nlookupdata nd;
1924         struct nchandle *nch;
1925         struct vnode *vp;
1926         int error;
1927
1928         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1929         if (error)
1930                 goto error_nond;
1931
1932         error = nlookup(&nd);
1933         if (error)
1934                 goto error_out;
1935
1936         nch = &nd.nl_nch;
1937
1938         error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1939         if (error)
1940                 goto error_out;
1941
1942         if ((vp = nch->ncp->nc_vp) == NULL) {
1943                 error = ENOENT;
1944                 goto error_out;
1945         }
1946
1947         if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1948                 goto error_out;
1949
1950         kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1951         vfs_cache_setroot(vp, cache_hold(nch));
1952
1953 error_out:
1954         nlookup_done(&nd);
1955 error_nond:
1956         return(error);
1957 }
1958
1959 /*
1960  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1961  * determine whether it is legal to chdir to the vnode.  The vnode's state
1962  * is not changed by this call.
1963  */
1964 static int
1965 checkvp_chdir(struct vnode *vp, struct thread *td)
1966 {
1967         int error;
1968
1969         if (vp->v_type != VDIR)
1970                 error = ENOTDIR;
1971         else
1972                 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1973         return (error);
1974 }
1975
1976 int
1977 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1978 {
1979         struct thread *td = curthread;
1980         struct proc *p = td->td_proc;
1981         struct lwp *lp = td->td_lwp;
1982         struct filedesc *fdp = p->p_fd;
1983         int cmode, flags;
1984         struct file *nfp;
1985         struct file *fp;
1986         struct vnode *vp;
1987         int type, indx, error = 0;
1988         struct flock lf;
1989
1990         if ((oflags & O_ACCMODE) == O_ACCMODE)
1991                 return (EINVAL);
1992         flags = FFLAGS(oflags);
1993         error = falloc(lp, &nfp, NULL);
1994         if (error)
1995                 return (error);
1996         fp = nfp;
1997         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1998
1999         /*
2000          * XXX p_dupfd is a real mess.  It allows a device to return a
2001          * file descriptor to be duplicated rather then doing the open
2002          * itself.
2003          */
2004         lp->lwp_dupfd = -1;
2005
2006         /*
2007          * Call vn_open() to do the lookup and assign the vnode to the 
2008          * file pointer.  vn_open() does not change the ref count on fp
2009          * and the vnode, on success, will be inherited by the file pointer
2010          * and unlocked.
2011          *
2012          * Request a shared lock on the vnode if possible.
2013          *
2014          * Executable binaries can race VTEXT against O_RDWR opens, so
2015          * use an exclusive lock for O_RDWR opens as well.
2016          *
2017          * NOTE: We need a flag to separate terminal vnode locking from
2018          *       parent locking.  O_CREAT needs parent locking, but O_TRUNC
2019          *       and O_RDWR only need to lock the terminal vnode exclusively.
2020          */
2021         nd->nl_flags |= NLC_LOCKVP;
2022         if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
2023                 nd->nl_flags |= NLC_SHAREDLOCK;
2024
2025         error = vn_open(nd, fp, flags, cmode);
2026         nlookup_done(nd);
2027
2028         if (error) {
2029                 /*
2030                  * handle special fdopen() case.  bleh.  dupfdopen() is
2031                  * responsible for dropping the old contents of ofiles[indx]
2032                  * if it succeeds.
2033                  *
2034                  * Note that fsetfd() will add a ref to fp which represents
2035                  * the fd_files[] assignment.  We must still drop our
2036                  * reference.
2037                  */
2038                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2039                         if (fdalloc(p, 0, &indx) == 0) {
2040                                 error = dupfdopen(td, indx, lp->lwp_dupfd, flags, error);
2041                                 if (error == 0) {
2042                                         *res = indx;
2043                                         fdrop(fp);      /* our ref */
2044                                         return (0);
2045                                 }
2046                                 fsetfd(fdp, NULL, indx);
2047                         }
2048                 }
2049                 fdrop(fp);      /* our ref */
2050                 if (error == ERESTART)
2051                         error = EINTR;
2052                 return (error);
2053         }
2054
2055         /*
2056          * ref the vnode for ourselves so it can't be ripped out from under
2057          * is.  XXX need an ND flag to request that the vnode be returned
2058          * anyway.
2059          *
2060          * Reserve a file descriptor but do not assign it until the open
2061          * succeeds.
2062          */
2063         vp = (struct vnode *)fp->f_data;
2064         vref(vp);
2065         if ((error = fdalloc(p, 0, &indx)) != 0) {
2066                 fdrop(fp);
2067                 vrele(vp);
2068                 return (error);
2069         }
2070
2071         /*
2072          * If no error occurs the vp will have been assigned to the file
2073          * pointer.
2074          */
2075         lp->lwp_dupfd = 0;
2076
2077         if (flags & (O_EXLOCK | O_SHLOCK)) {
2078                 lf.l_whence = SEEK_SET;
2079                 lf.l_start = 0;
2080                 lf.l_len = 0;
2081                 if (flags & O_EXLOCK)
2082                         lf.l_type = F_WRLCK;
2083                 else
2084                         lf.l_type = F_RDLCK;
2085                 if (flags & FNONBLOCK)
2086                         type = 0;
2087                 else
2088                         type = F_WAIT;
2089
2090                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2091                         /*
2092                          * lock request failed.  Clean up the reserved
2093                          * descriptor.
2094                          */
2095                         vrele(vp);
2096                         fsetfd(fdp, NULL, indx);
2097                         fdrop(fp);
2098                         return (error);
2099                 }
2100                 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2101         }
2102 #if 0
2103         /*
2104          * Assert that all regular file vnodes were created with a object.
2105          */
2106         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2107                 ("open: regular file has no backing object after vn_open"));
2108 #endif
2109
2110         vrele(vp);
2111
2112         /*
2113          * release our private reference, leaving the one associated with the
2114          * descriptor table intact.
2115          */
2116         if (oflags & O_CLOEXEC)
2117                 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2118         fsetfd(fdp, fp, indx);
2119         fdrop(fp);
2120         *res = indx;
2121
2122         return (error);
2123 }
2124
2125 /*
2126  * open_args(char *path, int flags, int mode)
2127  *
2128  * Check permissions, allocate an open file structure,
2129  * and call the device open routine if any.
2130  */
2131 int
2132 sys_open(struct open_args *uap)
2133 {
2134         struct nlookupdata nd;
2135         int error;
2136
2137         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2138         if (error == 0) {
2139                 error = kern_open(&nd, uap->flags,
2140                                     uap->mode, &uap->sysmsg_result);
2141         }
2142         nlookup_done(&nd);
2143         return (error);
2144 }
2145
2146 /*
2147  * openat_args(int fd, char *path, int flags, int mode)
2148  */
2149 int
2150 sys_openat(struct openat_args *uap)
2151 {
2152         struct nlookupdata nd;
2153         int error;
2154         struct file *fp;
2155
2156         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2157         if (error == 0) {
2158                 error = kern_open(&nd, uap->flags, uap->mode, 
2159                                         &uap->sysmsg_result);
2160         }
2161         nlookup_done_at(&nd, fp);
2162         return (error);
2163 }
2164
2165 int
2166 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2167 {
2168         struct thread *td = curthread;
2169         struct proc *p = td->td_proc;
2170         struct vnode *vp;
2171         struct vattr vattr;
2172         int error;
2173         int whiteout = 0;
2174
2175         KKASSERT(p);
2176
2177         VATTR_NULL(&vattr);
2178         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2179         vattr.va_rmajor = rmajor;
2180         vattr.va_rminor = rminor;
2181
2182         switch (mode & S_IFMT) {
2183         case S_IFMT:    /* used by badsect to flag bad sectors */
2184                 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2185                 vattr.va_type = VBAD;
2186                 break;
2187         case S_IFCHR:
2188                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2189                 vattr.va_type = VCHR;
2190                 break;
2191         case S_IFBLK:
2192                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2193                 vattr.va_type = VBLK;
2194                 break;
2195         case S_IFWHT:
2196                 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2197                 whiteout = 1;
2198                 break;
2199         case S_IFDIR:   /* special directories support for HAMMER */
2200                 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2201                 vattr.va_type = VDIR;
2202                 break;
2203         default:
2204                 error = EINVAL;
2205                 break;
2206         }
2207
2208         if (error)
2209                 return (error);
2210
2211         bwillinode(1);
2212         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2213         if ((error = nlookup(nd)) != 0)
2214                 return (error);
2215         if (nd->nl_nch.ncp->nc_vp)
2216                 return (EEXIST);
2217         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2218                 return (error);
2219
2220         if (whiteout) {
2221                 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2222                                       nd->nl_cred, NAMEI_CREATE);
2223         } else {
2224                 vp = NULL;
2225                 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2226                                    &vp, nd->nl_cred, &vattr);
2227                 if (error == 0)
2228                         vput(vp);
2229         }
2230         return (error);
2231 }
2232
2233 /*
2234  * mknod_args(char *path, int mode, int dev)
2235  *
2236  * Create a special file.
2237  */
2238 int
2239 sys_mknod(struct mknod_args *uap)
2240 {
2241         struct nlookupdata nd;
2242         int error;
2243
2244         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2245         if (error == 0) {
2246                 error = kern_mknod(&nd, uap->mode,
2247                                    umajor(uap->dev), uminor(uap->dev));
2248         }
2249         nlookup_done(&nd);
2250         return (error);
2251 }
2252
2253 /*
2254  * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2255  *
2256  * Create a special file.  The path is relative to the directory associated
2257  * with fd.
2258  */
2259 int
2260 sys_mknodat(struct mknodat_args *uap)
2261 {
2262         struct nlookupdata nd;
2263         struct file *fp;
2264         int error;
2265
2266         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2267         if (error == 0) {
2268                 error = kern_mknod(&nd, uap->mode,
2269                                    umajor(uap->dev), uminor(uap->dev));
2270         }
2271         nlookup_done_at(&nd, fp);
2272         return (error);
2273 }
2274
2275 int
2276 kern_mkfifo(struct nlookupdata *nd, int mode)
2277 {
2278         struct thread *td = curthread;
2279         struct proc *p = td->td_proc;
2280         struct vattr vattr;
2281         struct vnode *vp;
2282         int error;
2283
2284         bwillinode(1);
2285
2286         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2287         if ((error = nlookup(nd)) != 0)
2288                 return (error);
2289         if (nd->nl_nch.ncp->nc_vp)
2290                 return (EEXIST);
2291         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2292                 return (error);
2293
2294         VATTR_NULL(&vattr);
2295         vattr.va_type = VFIFO;
2296         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2297         vp = NULL;
2298         error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2299         if (error == 0)
2300                 vput(vp);
2301         return (error);
2302 }
2303
2304 /*
2305  * mkfifo_args(char *path, int mode)
2306  *
2307  * Create a named pipe.
2308  */
2309 int
2310 sys_mkfifo(struct mkfifo_args *uap)
2311 {
2312         struct nlookupdata nd;
2313         int error;
2314
2315         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2316         if (error == 0)
2317                 error = kern_mkfifo(&nd, uap->mode);
2318         nlookup_done(&nd);
2319         return (error);
2320 }
2321
2322 /*
2323  * mkfifoat_args(int fd, char *path, mode_t mode)
2324  *
2325  * Create a named pipe.  The path is relative to the directory associated
2326  * with fd.
2327  */
2328 int
2329 sys_mkfifoat(struct mkfifoat_args *uap)
2330 {
2331         struct nlookupdata nd;
2332         struct file *fp;
2333         int error;
2334
2335         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2336         if (error == 0)
2337                 error = kern_mkfifo(&nd, uap->mode);
2338         nlookup_done_at(&nd, fp);
2339         return (error);
2340 }
2341
2342 static int hardlink_check_uid = 0;
2343 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2344     &hardlink_check_uid, 0, 
2345     "Unprivileged processes cannot create hard links to files owned by other "
2346     "users");
2347 static int hardlink_check_gid = 0;
2348 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2349     &hardlink_check_gid, 0,
2350     "Unprivileged processes cannot create hard links to files owned by other "
2351     "groups");
2352
2353 static int
2354 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2355 {
2356         struct vattr va;
2357         int error;
2358
2359         /*
2360          * Shortcut if disabled
2361          */
2362         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2363                 return (0);
2364
2365         /*
2366          * Privileged user can always hardlink
2367          */
2368         if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2369                 return (0);
2370
2371         /*
2372          * Otherwise only if the originating file is owned by the
2373          * same user or group.  Note that any group is allowed if
2374          * the file is owned by the caller.
2375          */
2376         error = VOP_GETATTR(vp, &va);
2377         if (error != 0)
2378                 return (error);
2379         
2380         if (hardlink_check_uid) {
2381                 if (cred->cr_uid != va.va_uid)
2382                         return (EPERM);
2383         }
2384         
2385         if (hardlink_check_gid) {
2386                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2387                         return (EPERM);
2388         }
2389
2390         return (0);
2391 }
2392
2393 int
2394 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2395 {
2396         struct thread *td = curthread;
2397         struct vnode *vp;
2398         int error;
2399
2400         /*
2401          * Lookup the source and obtained a locked vnode.
2402          *
2403          * You may only hardlink a file which you have write permission
2404          * on or which you own.
2405          *
2406          * XXX relookup on vget failure / race ?
2407          */
2408         bwillinode(1);
2409         nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2410         if ((error = nlookup(nd)) != 0)
2411                 return (error);
2412         vp = nd->nl_nch.ncp->nc_vp;
2413         KKASSERT(vp != NULL);
2414         if (vp->v_type == VDIR)
2415                 return (EPERM);         /* POSIX */
2416         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2417                 return (error);
2418         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2419                 return (error);
2420
2421         /*
2422          * Unlock the source so we can lookup the target without deadlocking
2423          * (XXX vp is locked already, possible other deadlock?).  The target
2424          * must not exist.
2425          */
2426         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2427         nd->nl_flags &= ~NLC_NCPISLOCKED;
2428         cache_unlock(&nd->nl_nch);
2429         vn_unlock(vp);
2430
2431         linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2432         if ((error = nlookup(linknd)) != 0) {
2433                 vrele(vp);
2434                 return (error);
2435         }
2436         if (linknd->nl_nch.ncp->nc_vp) {
2437                 vrele(vp);
2438                 return (EEXIST);
2439         }
2440         VFS_MODIFYING(vp->v_mount);
2441         error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2442         if (error) {
2443                 vrele(vp);
2444                 return (error);
2445         }
2446
2447         /*
2448          * Finally run the new API VOP.
2449          */
2450         error = can_hardlink(vp, td, td->td_ucred);
2451         if (error == 0) {
2452                 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2453                                   vp, linknd->nl_cred);
2454         }
2455         vput(vp);
2456         return (error);
2457 }
2458
2459 /*
2460  * link_args(char *path, char *link)
2461  *
2462  * Make a hard file link.
2463  */
2464 int
2465 sys_link(struct link_args *uap)
2466 {
2467         struct nlookupdata nd, linknd;
2468         int error;
2469
2470         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2471         if (error == 0) {
2472                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2473                 if (error == 0)
2474                         error = kern_link(&nd, &linknd);
2475                 nlookup_done(&linknd);
2476         }
2477         nlookup_done(&nd);
2478         return (error);
2479 }
2480
2481 /*
2482  * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2483  *
2484  * Make a hard file link. The path1 argument is relative to the directory
2485  * associated with fd1, and similarly the path2 argument is relative to
2486  * the directory associated with fd2.
2487  */
2488 int
2489 sys_linkat(struct linkat_args *uap)
2490 {
2491         struct nlookupdata nd, linknd;
2492         struct file *fp1, *fp2;
2493         int error;
2494
2495         error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2496             (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2497         if (error == 0) {
2498                 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2499                     uap->path2, UIO_USERSPACE, 0);
2500                 if (error == 0)
2501                         error = kern_link(&nd, &linknd);
2502                 nlookup_done_at(&linknd, fp2);
2503         }
2504         nlookup_done_at(&nd, fp1);
2505         return (error);
2506 }
2507
2508 int
2509 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2510 {
2511         struct vattr vattr;
2512         struct vnode *vp;
2513         struct vnode *dvp;
2514         int error;
2515
2516         bwillinode(1);
2517         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2518         if ((error = nlookup(nd)) != 0)
2519                 return (error);
2520         if (nd->nl_nch.ncp->nc_vp)
2521                 return (EEXIST);
2522         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2523                 return (error);
2524         dvp = nd->nl_dvp;
2525         VATTR_NULL(&vattr);
2526         vattr.va_mode = mode;
2527         error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2528         if (error == 0)
2529                 vput(vp);
2530         return (error);
2531 }
2532
2533 /*
2534  * symlink(char *path, char *link)
2535  *
2536  * Make a symbolic link.
2537  */
2538 int
2539 sys_symlink(struct symlink_args *uap)
2540 {
2541         struct thread *td = curthread;
2542         struct nlookupdata nd;
2543         char *path;
2544         int error;
2545         int mode;
2546
2547         path = objcache_get(namei_oc, M_WAITOK);
2548         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2549         if (error == 0) {
2550                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2551                 if (error == 0) {
2552                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2553                         error = kern_symlink(&nd, path, mode);
2554                 }
2555                 nlookup_done(&nd);
2556         }
2557         objcache_put(namei_oc, path);
2558         return (error);
2559 }
2560
2561 /*
2562  * symlinkat_args(char *path1, int fd, char *path2)
2563  *
2564  * Make a symbolic link.  The path2 argument is relative to the directory
2565  * associated with fd.
2566  */
2567 int
2568 sys_symlinkat(struct symlinkat_args *uap)
2569 {
2570         struct thread *td = curthread;
2571         struct nlookupdata nd;
2572         struct file *fp;
2573         char *path1;
2574         int error;
2575         int mode;
2576
2577         path1 = objcache_get(namei_oc, M_WAITOK);
2578         error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2579         if (error == 0) {
2580                 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2581                     UIO_USERSPACE, 0);
2582                 if (error == 0) {
2583                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2584                         error = kern_symlink(&nd, path1, mode);
2585                 }
2586                 nlookup_done_at(&nd, fp);
2587         }
2588         objcache_put(namei_oc, path1);
2589         return (error);
2590 }
2591
2592 /*
2593  * undelete_args(char *path)
2594  *
2595  * Delete a whiteout from the filesystem.
2596  */
2597 int
2598 sys_undelete(struct undelete_args *uap)
2599 {
2600         struct nlookupdata nd;
2601         int error;
2602
2603         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2604         bwillinode(1);
2605         nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2606         if (error == 0)
2607                 error = nlookup(&nd);
2608         if (error == 0)
2609                 error = ncp_writechk(&nd.nl_nch);
2610         if (error == 0) {
2611                 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2612                                       NAMEI_DELETE);
2613         }
2614         nlookup_done(&nd);
2615         return (error);
2616 }
2617
2618 int
2619 kern_unlink(struct nlookupdata *nd)
2620 {
2621         int error;
2622
2623         bwillinode(1);
2624         nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2625         if ((error = nlookup(nd)) != 0)
2626                 return (error);
2627         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2628                 return (error);
2629         error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2630         return (error);
2631 }
2632
2633 /*
2634  * unlink_args(char *path)
2635  *
2636  * Delete a name from the filesystem.
2637  */
2638 int
2639 sys_unlink(struct unlink_args *uap)
2640 {
2641         struct nlookupdata nd;
2642         int error;
2643
2644         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2645         if (error == 0)
2646                 error = kern_unlink(&nd);
2647         nlookup_done(&nd);
2648         return (error);
2649 }
2650
2651
2652 /*
2653  * unlinkat_args(int fd, char *path, int flags)
2654  *
2655  * Delete the file or directory entry pointed to by fd/path.
2656  */
2657 int
2658 sys_unlinkat(struct unlinkat_args *uap)
2659 {
2660         struct nlookupdata nd;
2661         struct file *fp;
2662         int error;
2663
2664         if (uap->flags & ~AT_REMOVEDIR)
2665                 return (EINVAL);
2666
2667         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2668         if (error == 0) {
2669                 if (uap->flags & AT_REMOVEDIR)
2670                         error = kern_rmdir(&nd);
2671                 else
2672                         error = kern_unlink(&nd);
2673         }
2674         nlookup_done_at(&nd, fp);
2675         return (error);
2676 }
2677
2678 int
2679 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2680 {
2681         struct thread *td = curthread;
2682         struct file *fp;
2683         struct vnode *vp;
2684         struct vattr vattr;
2685         off_t new_offset;
2686         int error;
2687
2688         fp = holdfp(td, fd, -1);
2689         if (fp == NULL)
2690                 return (EBADF);
2691         if (fp->f_type != DTYPE_VNODE) {
2692                 error = ESPIPE;
2693                 goto done;
2694         }
2695         vp = (struct vnode *)fp->f_data;
2696
2697         switch (whence) {
2698         case L_INCR:
2699                 spin_lock(&fp->f_spin);
2700                 new_offset = fp->f_offset + offset;
2701                 error = 0;
2702                 break;
2703         case L_XTND:
2704                 error = VOP_GETATTR(vp, &vattr);
2705                 spin_lock(&fp->f_spin);
2706                 new_offset = offset + vattr.va_size;
2707                 break;
2708         case L_SET:
2709                 new_offset = offset;
2710                 error = 0;
2711                 spin_lock(&fp->f_spin);
2712                 break;
2713         default:
2714                 new_offset = 0;
2715                 error = EINVAL;
2716                 spin_lock(&fp->f_spin);
2717                 break;
2718         }
2719
2720         /*
2721          * Validate the seek position.  Negative offsets are not allowed
2722          * for regular files or directories.
2723          *
2724          * Normally we would also not want to allow negative offsets for
2725          * character and block-special devices.  However kvm addresses
2726          * on 64 bit architectures might appear to be negative and must
2727          * be allowed.
2728          */
2729         if (error == 0) {
2730                 if (new_offset < 0 &&
2731                     (vp->v_type == VREG || vp->v_type == VDIR)) {
2732                         error = EINVAL;
2733                 } else {
2734                         fp->f_offset = new_offset;
2735                 }
2736         }
2737         *res = fp->f_offset;
2738         spin_unlock(&fp->f_spin);
2739 done:
2740         dropfp(td, fd, fp);
2741
2742         return (error);
2743 }
2744
2745 /*
2746  * lseek_args(int fd, int pad, off_t offset, int whence)
2747  *
2748  * Reposition read/write file offset.
2749  */
2750 int
2751 sys_lseek(struct lseek_args *uap)
2752 {
2753         int error;
2754
2755         error = kern_lseek(uap->fd, uap->offset, uap->whence,
2756                            &uap->sysmsg_offset);
2757
2758         return (error);
2759 }
2760
2761 /*
2762  * Check if current process can access given file.  amode is a bitmask of *_OK
2763  * access bits.  flags is a bitmask of AT_* flags.
2764  */
2765 int
2766 kern_access(struct nlookupdata *nd, int amode, int flags)
2767 {
2768         struct vnode *vp;
2769         int error, mode;
2770
2771         if (flags & ~AT_EACCESS)
2772                 return (EINVAL);
2773         nd->nl_flags |= NLC_SHAREDLOCK;
2774         if ((error = nlookup(nd)) != 0)
2775                 return (error);
2776 retry:
2777         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2778         if (error)
2779                 return (error);
2780
2781         /* Flags == 0 means only check for existence. */
2782         if (amode) {
2783                 mode = 0;
2784                 if (amode & R_OK)
2785                         mode |= VREAD;
2786                 if (amode & W_OK)
2787                         mode |= VWRITE;
2788                 if (amode & X_OK)
2789                         mode |= VEXEC;
2790                 if ((mode & VWRITE) == 0 || 
2791                     (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2792                         error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2793
2794                 /*
2795                  * If the file handle is stale we have to re-resolve the
2796                  * entry with the ncp held exclusively.  This is a hack
2797                  * at the moment.
2798                  */
2799                 if (error == ESTALE) {
2800                         vput(vp);
2801                         cache_unlock(&nd->nl_nch);
2802                         cache_lock(&nd->nl_nch);
2803                         cache_setunresolved(&nd->nl_nch);
2804                         error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2805                         if (error == 0) {
2806                                 vp = NULL;
2807                                 goto retry;
2808                         }
2809                         return(error);
2810                 }
2811         }
2812         vput(vp);
2813         return (error);
2814 }
2815
2816 /*
2817  * access_args(char *path, int flags)
2818  *
2819  * Check access permissions.
2820  */
2821 int
2822 sys_access(struct access_args *uap)
2823 {
2824         struct nlookupdata nd;
2825         int error;
2826
2827         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2828         if (error == 0)
2829                 error = kern_access(&nd, uap->flags, 0);
2830         nlookup_done(&nd);
2831         return (error);
2832 }
2833
2834
2835 /*
2836  * eaccess_args(char *path, int flags)
2837  *
2838  * Check access permissions.
2839  */
2840 int
2841 sys_eaccess(struct eaccess_args *uap)
2842 {
2843         struct nlookupdata nd;
2844         int error;
2845
2846         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2847         if (error == 0)
2848                 error = kern_access(&nd, uap->flags, AT_EACCESS);
2849         nlookup_done(&nd);
2850         return (error);
2851 }
2852
2853
2854 /*
2855  * faccessat_args(int fd, char *path, int amode, int flags)
2856  *
2857  * Check access permissions.
2858  */
2859 int
2860 sys_faccessat(struct faccessat_args *uap)
2861 {
2862         struct nlookupdata nd;
2863         struct file *fp;
2864         int error;
2865
2866         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 
2867                                 NLC_FOLLOW);
2868         if (error == 0)
2869                 error = kern_access(&nd, uap->amode, uap->flags);
2870         nlookup_done_at(&nd, fp);
2871         return (error);
2872 }
2873
2874 int
2875 kern_stat(struct nlookupdata *nd, struct stat *st)
2876 {
2877         int error;
2878         struct vnode *vp;
2879
2880         nd->nl_flags |= NLC_SHAREDLOCK;
2881         if ((error = nlookup(nd)) != 0)
2882                 return (error);
2883 again:
2884         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2885                 return (ENOENT);
2886
2887         if ((error = vget(vp, LK_SHARED)) != 0)
2888                 return (error);
2889         error = vn_stat(vp, st, nd->nl_cred);
2890
2891         /*
2892          * If the file handle is stale we have to re-resolve the
2893          * entry with the ncp held exclusively.  This is a hack
2894          * at the moment.
2895          */
2896         if (error == ESTALE) {
2897                 vput(vp);
2898                 cache_unlock(&nd->nl_nch);
2899                 cache_lock(&nd->nl_nch);
2900                 cache_setunresolved(&nd->nl_nch);
2901                 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2902                 if (error == 0)
2903                         goto again;
2904         } else {
2905                 vput(vp);
2906         }
2907         return (error);
2908 }
2909
2910 /*
2911  * stat_args(char *path, struct stat *ub)
2912  *
2913  * Get file status; this version follows links.
2914  */
2915 int
2916 sys_stat(struct stat_args *uap)
2917 {
2918         struct nlookupdata nd;
2919         struct stat st;
2920         int error;
2921
2922         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2923         if (error == 0) {
2924                 error = kern_stat(&nd, &st);
2925                 if (error == 0)
2926                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2927         }
2928         nlookup_done(&nd);
2929         return (error);
2930 }
2931
2932 /*
2933  * lstat_args(char *path, struct stat *ub)
2934  *
2935  * Get file status; this version does not follow links.
2936  */
2937 int
2938 sys_lstat(struct lstat_args *uap)
2939 {
2940         struct nlookupdata nd;
2941         struct stat st;
2942         int error;
2943
2944         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2945         if (error == 0) {
2946                 error = kern_stat(&nd, &st);
2947                 if (error == 0)
2948                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2949         }
2950         nlookup_done(&nd);
2951         return (error);
2952 }
2953
2954 /*
2955  * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2956  *
2957  * Get status of file pointed to by fd/path.
2958  */
2959 int
2960 sys_fstatat(struct fstatat_args *uap)
2961 {
2962         struct nlookupdata nd;
2963         struct stat st;
2964         int error;
2965         int flags;
2966         struct file *fp;
2967
2968         if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2969                 return (EINVAL);
2970
2971         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2972
2973         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
2974                                 UIO_USERSPACE, flags);
2975         if (error == 0) {
2976                 error = kern_stat(&nd, &st);
2977                 if (error == 0)
2978                         error = copyout(&st, uap->sb, sizeof(*uap->sb));
2979         }
2980         nlookup_done_at(&nd, fp);
2981         return (error);
2982 }
2983
2984 static int
2985 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2986 {
2987         struct nlookupdata nd;
2988         struct vnode *vp;
2989         int error;
2990
2991         vp = NULL;
2992         error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2993         if (error == 0)
2994                 error = nlookup(&nd);
2995         if (error == 0)
2996                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2997         nlookup_done(&nd);
2998         if (error == 0) {
2999                 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3000                 vput(vp);
3001         }
3002         return (error);
3003 }
3004
3005 /*
3006  * pathconf_Args(char *path, int name)
3007  *
3008  * Get configurable pathname variables.
3009  */
3010 int
3011 sys_pathconf(struct pathconf_args *uap)
3012 {
3013         return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3014                 &uap->sysmsg_reg));
3015 }
3016
3017 /*
3018  * lpathconf_Args(char *path, int name)
3019  *
3020  * Get configurable pathname variables, but don't follow symlinks.
3021  */
3022 int
3023 sys_lpathconf(struct lpathconf_args *uap)
3024 {
3025         return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
3026 }
3027
3028 /*
3029  * XXX: daver
3030  * kern_readlink isn't properly split yet.  There is a copyin burried
3031  * in VOP_READLINK().
3032  */
3033 int
3034 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3035 {
3036         struct thread *td = curthread;
3037         struct vnode *vp;
3038         struct iovec aiov;
3039         struct uio auio;
3040         int error;
3041
3042         nd->nl_flags |= NLC_SHAREDLOCK;
3043         if ((error = nlookup(nd)) != 0)
3044                 return (error);
3045         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3046         if (error)
3047                 return (error);
3048         if (vp->v_type != VLNK) {
3049                 error = EINVAL;
3050         } else {
3051                 aiov.iov_base = buf;
3052                 aiov.iov_len = count;
3053                 auio.uio_iov = &aiov;
3054                 auio.uio_iovcnt = 1;
3055                 auio.uio_offset = 0;
3056                 auio.uio_rw = UIO_READ;
3057                 auio.uio_segflg = UIO_USERSPACE;
3058                 auio.uio_td = td;
3059                 auio.uio_resid = count;
3060                 error = VOP_READLINK(vp, &auio, td->td_ucred);
3061         }
3062         vput(vp);
3063         *res = count - auio.uio_resid;
3064         return (error);
3065 }
3066
3067 /*
3068  * readlink_args(char *path, char *buf, int count)
3069  *
3070  * Return target name of a symbolic link.
3071  */
3072 int
3073 sys_readlink(struct readlink_args *uap)
3074 {
3075         struct nlookupdata nd;
3076         int error;
3077
3078         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3079         if (error == 0) {
3080                 error = kern_readlink(&nd, uap->buf, uap->count,
3081                                         &uap->sysmsg_result);
3082         }
3083         nlookup_done(&nd);
3084         return (error);
3085 }
3086
3087 /*
3088  * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3089  *
3090  * Return target name of a symbolic link.  The path is relative to the
3091  * directory associated with fd.
3092  */
3093 int
3094 sys_readlinkat(struct readlinkat_args *uap)
3095 {
3096         struct nlookupdata nd;
3097         struct file *fp;
3098         int error;
3099
3100         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3101         if (error == 0) {
3102                 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3103                                         &uap->sysmsg_result);
3104         }
3105         nlookup_done_at(&nd, fp);
3106         return (error);
3107 }
3108
3109 static int
3110 setfflags(struct vnode *vp, int flags)
3111 {
3112         struct thread *td = curthread;
3113         int error;
3114         struct vattr vattr;
3115
3116         /*
3117          * Prevent non-root users from setting flags on devices.  When
3118          * a device is reused, users can retain ownership of the device
3119          * if they are allowed to set flags and programs assume that
3120          * chown can't fail when done as root.
3121          */
3122         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
3123             ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3124                 return (error);
3125
3126         /*
3127          * note: vget is required for any operation that might mod the vnode
3128          * so VINACTIVE is properly cleared.
3129          */
3130         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3131                 VATTR_NULL(&vattr);
3132                 vattr.va_flags = flags;
3133                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3134                 vput(vp);
3135         }
3136         return (error);
3137 }
3138
3139 /*
3140  * chflags(char *path, int flags)
3141  *
3142  * Change flags of a file given a path name.
3143  */
3144 int
3145 sys_chflags(struct chflags_args *uap)
3146 {
3147         struct nlookupdata nd;
3148         struct vnode *vp;
3149         int error;
3150
3151         vp = NULL;
3152         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3153         if (error == 0)
3154                 error = nlookup(&nd);
3155         if (error == 0)
3156                 error = ncp_writechk(&nd.nl_nch);
3157         if (error == 0)
3158                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3159         nlookup_done(&nd);
3160         if (error == 0) {
3161                 error = setfflags(vp, uap->flags);
3162                 vrele(vp);
3163         }
3164         return (error);
3165 }
3166
3167 /*
3168  * lchflags(char *path, int flags)
3169  *
3170  * Change flags of a file given a path name, but don't follow symlinks.
3171  */
3172 int
3173 sys_lchflags(struct lchflags_args *uap)
3174 {
3175         struct nlookupdata nd;
3176         struct vnode *vp;
3177         int error;
3178
3179         vp = NULL;
3180         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3181         if (error == 0)
3182                 error = nlookup(&nd);
3183         if (error == 0)
3184                 error = ncp_writechk(&nd.nl_nch);
3185         if (error == 0)
3186                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3187         nlookup_done(&nd);
3188         if (error == 0) {
3189                 error = setfflags(vp, uap->flags);
3190                 vrele(vp);
3191         }
3192         return (error);
3193 }
3194
3195 /*
3196  * fchflags_args(int fd, int flags)
3197  *
3198  * Change flags of a file given a file descriptor.
3199  */
3200 int
3201 sys_fchflags(struct fchflags_args *uap)
3202 {
3203         struct thread *td = curthread;
3204         struct file *fp;
3205         int error;
3206
3207         if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3208                 return (error);
3209         if (fp->f_nchandle.ncp)
3210                 error = ncp_writechk(&fp->f_nchandle);
3211         if (error == 0)
3212                 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3213         fdrop(fp);
3214         return (error);
3215 }
3216
3217 /*
3218  * chflagsat_args(int fd, const char *path, int flags, int atflags)
3219  * change flags given a pathname relative to a filedescriptor
3220  */
3221 int sys_chflagsat(struct chflagsat_args *uap)
3222 {
3223         struct nlookupdata nd;
3224         struct vnode *vp;
3225         struct file *fp;
3226         int error;
3227         int lookupflags;
3228
3229         if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3230                 return (EINVAL);
3231
3232         lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3233
3234         vp = NULL;
3235         error = nlookup_init_at(&nd, &fp, uap->fd,  uap->path, UIO_USERSPACE, lookupflags);
3236         if (error == 0)
3237                 error = nlookup(&nd);
3238         if (error == 0)
3239                 error = ncp_writechk(&nd.nl_nch);
3240         if (error == 0)
3241                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3242         nlookup_done_at(&nd, fp);
3243         if (error == 0) {
3244                 error = setfflags(vp, uap->flags);
3245                 vrele(vp);
3246         }
3247         return (error);
3248 }
3249
3250
3251 static int
3252 setfmode(struct vnode *vp, int mode)
3253 {
3254         struct thread *td = curthread;
3255         int error;
3256         struct vattr vattr;
3257
3258         /*
3259          * note: vget is required for any operation that might mod the vnode
3260          * so VINACTIVE is properly cleared.
3261          */
3262         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3263                 VATTR_NULL(&vattr);
3264                 vattr.va_mode = mode & ALLPERMS;
3265                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3266                 cache_inval_wxok(vp);
3267                 vput(vp);
3268         }
3269         return error;
3270 }
3271
3272 int
3273 kern_chmod(struct nlookupdata *nd, int mode)
3274 {
3275         struct vnode *vp;
3276         int error;
3277
3278         if ((error = nlookup(nd)) != 0)
3279                 return (error);
3280         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3281                 return (error);
3282         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3283                 error = setfmode(vp, mode);
3284         vrele(vp);
3285         return (error);
3286 }
3287
3288 /*
3289  * chmod_args(char *path, int mode)
3290  *
3291  * Change mode of a file given path name.
3292  */
3293 int
3294 sys_chmod(struct chmod_args *uap)
3295 {
3296         struct nlookupdata nd;
3297         int error;
3298
3299         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3300         if (error == 0)
3301                 error = kern_chmod(&nd, uap->mode);
3302         nlookup_done(&nd);
3303         return (error);
3304 }
3305
3306 /*
3307  * lchmod_args(char *path, int mode)
3308  *
3309  * Change mode of a file given path name (don't follow links.)
3310  */
3311 int
3312 sys_lchmod(struct lchmod_args *uap)
3313 {
3314         struct nlookupdata nd;
3315         int error;
3316
3317         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3318         if (error == 0)
3319                 error = kern_chmod(&nd, uap->mode);
3320         nlookup_done(&nd);
3321         return (error);
3322 }
3323
3324 /*
3325  * fchmod_args(int fd, int mode)
3326  *
3327  * Change mode of a file given a file descriptor.
3328  */
3329 int
3330 sys_fchmod(struct fchmod_args *uap)
3331 {
3332         struct thread *td = curthread;
3333         struct file *fp;
3334         int error;
3335
3336         if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3337                 return (error);
3338         if (fp->f_nchandle.ncp)
3339                 error = ncp_writechk(&fp->f_nchandle);
3340         if (error == 0)
3341                 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3342         fdrop(fp);
3343         return (error);
3344 }
3345
3346 /*
3347  * fchmodat_args(char *path, int mode)
3348  *
3349  * Change mode of a file pointed to by fd/path.
3350  */
3351 int
3352 sys_fchmodat(struct fchmodat_args *uap)
3353 {
3354         struct nlookupdata nd;
3355         struct file *fp;
3356         int error;
3357         int flags;
3358
3359         if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3360                 return (EINVAL);
3361         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3362
3363         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
3364                                 UIO_USERSPACE, flags);
3365         if (error == 0)
3366                 error = kern_chmod(&nd, uap->mode);
3367         nlookup_done_at(&nd, fp);
3368         return (error);
3369 }
3370
3371 static int
3372 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3373 {
3374         struct thread *td = curthread;
3375         int error;
3376         struct vattr vattr;
3377         uid_t o_uid;
3378         gid_t o_gid;
3379         uint64_t size;
3380
3381         /*
3382          * note: vget is required for any operation that might mod the vnode
3383          * so VINACTIVE is properly cleared.
3384          */
3385         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3386                 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3387                         return error;
3388                 o_uid = vattr.va_uid;
3389                 o_gid = vattr.va_gid;
3390                 size = vattr.va_size;
3391
3392                 VATTR_NULL(&vattr);
3393                 vattr.va_uid = uid;
3394                 vattr.va_gid = gid;
3395                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3396                 vput(vp);
3397         }
3398
3399         if (error == 0) {
3400                 if (uid == -1)
3401                         uid = o_uid;
3402                 if (gid == -1)
3403                         gid = o_gid;
3404                 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3405                 VFS_ACCOUNT(mp,   uid,   gid,  size);
3406         }
3407
3408         return error;
3409 }
3410
3411 int
3412 kern_chown(struct nlookupdata *nd, int uid, int gid)
3413 {
3414         struct vnode *vp;
3415         int error;
3416
3417         if ((error = nlookup(nd)) != 0)
3418                 return (error);
3419         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3420                 return (error);
3421         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3422                 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3423         vrele(vp);
3424         return (error);
3425 }
3426
3427 /*
3428  * chown(char *path, int uid, int gid)
3429  *
3430  * Set ownership given a path name.
3431  */
3432 int
3433 sys_chown(struct chown_args *uap)
3434 {
3435         struct nlookupdata nd;
3436         int error;
3437
3438         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3439         if (error == 0)
3440                 error = kern_chown(&nd, uap->uid, uap->gid);
3441         nlookup_done(&nd);
3442         return (error);
3443 }
3444
3445 /*
3446  * lchown_args(char *path, int uid, int gid)
3447  *
3448  * Set ownership given a path name, do not cross symlinks.
3449  */
3450 int
3451 sys_lchown(struct lchown_args *uap)
3452 {
3453         struct nlookupdata nd;
3454         int error;
3455
3456         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3457         if (error == 0)
3458                 error = kern_chown(&nd, uap->uid, uap->gid);
3459         nlookup_done(&nd);
3460         return (error);
3461 }
3462
3463 /*
3464  * fchown_args(int fd, int uid, int gid)
3465  *
3466  * Set ownership given a file descriptor.
3467  */
3468 int
3469 sys_fchown(struct fchown_args *uap)
3470 {
3471         struct thread *td = curthread;
3472         struct proc *p = td->td_proc;
3473         struct file *fp;
3474         int error;
3475
3476         if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3477                 return (error);
3478         if (fp->f_nchandle.ncp)
3479                 error = ncp_writechk(&fp->f_nchandle);
3480         if (error == 0)
3481                 error = setfown(p->p_fd->fd_ncdir.mount,
3482                         (struct vnode *)fp->f_data, uap->uid, uap->gid);
3483         fdrop(fp);
3484         return (error);
3485 }
3486
3487 /*
3488  * fchownat(int fd, char *path, int uid, int gid, int flags)
3489  *
3490  * Set ownership of file pointed to by fd/path.
3491  */
3492 int
3493 sys_fchownat(struct fchownat_args *uap)
3494 {
3495         struct nlookupdata nd;
3496         struct file *fp;
3497         int error;
3498         int flags;
3499
3500         if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3501                 return (EINVAL);
3502         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3503
3504         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
3505                                 UIO_USERSPACE, flags);
3506         if (error == 0)
3507                 error = kern_chown(&nd, uap->uid, uap->gid);
3508         nlookup_done_at(&nd, fp);
3509         return (error);
3510 }
3511
3512
3513 static int
3514 getutimes(struct timeval *tvp, struct timespec *tsp)
3515 {
3516         struct timeval tv[2];
3517         int error;
3518
3519         if (tvp == NULL) {
3520                 microtime(&tv[0]);
3521                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3522                 tsp[1] = tsp[0];
3523         } else {
3524                 if ((error = itimerfix(tvp)) != 0)
3525                         return (error);
3526                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3527                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3528         }
3529         return 0;
3530 }
3531
3532 static int
3533 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3534 {
3535         struct timespec tsnow;
3536         int error;
3537
3538         *nullflag = 0;
3539         nanotime(&tsnow);
3540         if (ts == NULL) {
3541                 newts[0] = tsnow;
3542                 newts[1] = tsnow;
3543                 *nullflag = 1;
3544                 return (0);
3545         }
3546
3547         newts[0] = ts[0];
3548         newts[1] = ts[1];
3549         if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3550                 return (0);
3551         if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3552                 *nullflag = 1;
3553
3554         if (newts[0].tv_nsec == UTIME_OMIT)
3555                 newts[0].tv_sec = VNOVAL;
3556         else if (newts[0].tv_nsec == UTIME_NOW)
3557                 newts[0] = tsnow;
3558         else if ((error = itimespecfix(&newts[0])) != 0)
3559                 return (error);
3560
3561         if (newts[1].tv_nsec == UTIME_OMIT)
3562                 newts[1].tv_sec = VNOVAL;
3563         else if (newts[1].tv_nsec == UTIME_NOW)
3564                 newts[1] = tsnow;
3565         else if ((error = itimespecfix(&newts[1])) != 0)
3566                 return (error);
3567
3568         return (0);
3569 }
3570
3571 static int
3572 setutimes(struct vnode *vp, struct vattr *vattr,
3573           const struct timespec *ts, int nullflag)
3574 {
3575         struct thread *td = curthread;
3576         int error;
3577
3578         VATTR_NULL(vattr);
3579         vattr->va_atime = ts[0];
3580         vattr->va_mtime = ts[1];
3581         if (nullflag)
3582                 vattr->va_vaflags |= VA_UTIMES_NULL;
3583         error = VOP_SETATTR(vp, vattr, td->td_ucred);
3584
3585         return error;
3586 }
3587
3588 int
3589 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3590 {
3591         struct timespec ts[2];
3592         int error;
3593
3594         if (tptr) {
3595                 if ((error = getutimes(tptr, ts)) != 0)
3596                         return (error);
3597         }
3598         error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3599         return (error);
3600 }
3601
3602 /*
3603  * utimes_args(char *path, struct timeval *tptr)
3604  *
3605  * Set the access and modification times of a file.
3606  */
3607 int
3608 sys_utimes(struct utimes_args *uap)
3609 {
3610         struct timeval tv[2];
3611         struct nlookupdata nd;
3612         int error;
3613
3614         if (uap->tptr) {
3615                 error = copyin(uap->tptr, tv, sizeof(tv));
3616                 if (error)
3617                         return (error);
3618         }
3619         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3620         if (error == 0)
3621                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3622         nlookup_done(&nd);
3623         return (error);
3624 }
3625
3626 /*
3627  * lutimes_args(char *path, struct timeval *tptr)
3628  *
3629  * Set the access and modification times of a file.
3630  */
3631 int
3632 sys_lutimes(struct lutimes_args *uap)
3633 {
3634         struct timeval tv[2];
3635         struct nlookupdata nd;
3636         int error;
3637
3638         if (uap->tptr) {
3639                 error = copyin(uap->tptr, tv, sizeof(tv));
3640                 if (error)
3641                         return (error);
3642         }
3643         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3644         if (error == 0)
3645                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3646         nlookup_done(&nd);
3647         return (error);
3648 }
3649
3650 /*
3651  * Set utimes on a file descriptor.  The creds used to open the
3652  * file are used to determine whether the operation is allowed
3653  * or not.
3654  */
3655 int
3656 kern_futimens(int fd, struct timespec *ts)
3657 {
3658         struct thread *td = curthread;
3659         struct timespec newts[2];
3660         struct file *fp;
3661         struct vnode *vp;
3662         struct vattr vattr;
3663         int nullflag;
3664         int error;
3665
3666         error = getutimens(ts, newts, &nullflag);
3667         if (error)
3668                 return (error);
3669         if ((error = holdvnode(td, fd, &fp)) != 0)
3670                 return (error);
3671         if (fp->f_nchandle.ncp)
3672                 error = ncp_writechk(&fp->f_nchandle);
3673         if (error == 0) {
3674                 vp = fp->f_data;
3675                 error = vget(vp, LK_EXCLUSIVE);
3676                 if (error == 0) {
3677                         error = VOP_GETATTR(vp, &vattr);
3678                         if (error == 0) {
3679                                 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3680                                                    fp->f_cred);
3681                         }
3682                         if (error == 0) {
3683                                 error = setutimes(vp, &vattr, newts, nullflag);
3684                         }
3685                         vput(vp);
3686                 }
3687         }
3688         fdrop(fp);
3689         return (error);
3690 }
3691
3692 /*
3693  * futimens_args(int fd, struct timespec *ts)
3694  *
3695  * Set the access and modification times of a file.
3696  */
3697 int
3698 sys_futimens(struct futimens_args *uap)
3699 {
3700         struct timespec ts[2];
3701         int error;
3702
3703         if (uap->ts) {
3704                 error = copyin(uap->ts, ts, sizeof(ts));
3705                 if (error)
3706                         return (error);
3707         }
3708         error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3709         return (error);
3710 }
3711
3712 int
3713 kern_futimes(int fd, struct timeval *tptr)
3714 {
3715         struct timespec ts[2];
3716         int error;
3717
3718         if (tptr) {
3719                 if ((error = getutimes(tptr, ts)) != 0)
3720                         return (error);
3721         }
3722         error = kern_futimens(fd, tptr ? ts : NULL);
3723         return (error);
3724 }
3725
3726 /*
3727  * futimes_args(int fd, struct timeval *tptr)
3728  *
3729  * Set the access and modification times of a file.
3730  */
3731 int
3732 sys_futimes(struct futimes_args *uap)
3733 {
3734         struct timeval tv[2];
3735         int error;
3736
3737         if (uap->tptr) {
3738                 error = copyin(uap->tptr, tv, sizeof(tv));
3739                 if (error)
3740                         return (error);
3741         }
3742         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3743         return (error);
3744 }
3745
3746 int
3747 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3748 {
3749         struct timespec newts[2];
3750         struct vnode *vp;
3751         struct vattr vattr;
3752         int nullflag;
3753         int error;
3754
3755         if (flags & ~AT_SYMLINK_NOFOLLOW)
3756                 return (EINVAL);
3757
3758         error = getutimens(ts, newts, &nullflag);
3759         if (error)
3760                 return (error);
3761
3762         nd->nl_flags |= NLC_OWN | NLC_WRITE;
3763         if ((error = nlookup(nd)) != 0)
3764                 return (error);
3765         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3766                 return (error);
3767         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3768                 return (error);
3769         if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3770                 error = vget(vp, LK_EXCLUSIVE);
3771                 if (error == 0) {
3772                         error = setutimes(vp, &vattr, newts, nullflag);
3773                         vput(vp);
3774                 }
3775         }
3776         vrele(vp);
3777         return (error);
3778 }
3779
3780 /*
3781  * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3782  *
3783  * Set file access and modification times of a file.
3784  */
3785 int
3786 sys_utimensat(struct utimensat_args *uap)
3787 {
3788         struct timespec ts[2];
3789         struct nlookupdata nd;
3790         struct file *fp;
3791         int error;
3792         int flags;
3793
3794         if (uap->ts) {
3795                 error = copyin(uap->ts, ts, sizeof(ts));
3796                 if (error)
3797                         return (error);
3798         }
3799
3800         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3801         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3802                                 UIO_USERSPACE, flags);
3803         if (error == 0)
3804                 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3805         nlookup_done_at(&nd, fp);
3806         return (error);
3807 }
3808
3809 int
3810 kern_truncate(struct nlookupdata *nd, off_t length)
3811 {
3812         struct vnode *vp;
3813         struct vattr vattr;
3814         int error;
3815         uid_t uid = 0;
3816         gid_t gid = 0;
3817         uint64_t old_size = 0;
3818
3819         if (length < 0)
3820                 return(EINVAL);
3821         nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3822         if ((error = nlookup(nd)) != 0)
3823                 return (error);
3824         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3825                 return (error);
3826         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3827                 return (error);
3828         error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3829         if (error) {
3830                 vrele(vp);
3831                 return (error);
3832         }
3833         if (vp->v_type == VDIR) {
3834                 error = EISDIR;
3835                 goto done;
3836         }
3837         if (vfs_quota_enabled) {
3838                 error = VOP_GETATTR(vp, &vattr);
3839                 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3840                 uid = vattr.va_uid;
3841                 gid = vattr.va_gid;
3842                 old_size = vattr.va_size;
3843         }
3844
3845         if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3846                 VATTR_NULL(&vattr);
3847                 vattr.va_size = length;
3848                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3849                 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3850         }
3851 done:
3852         vput(vp);
3853         return (error);
3854 }
3855
3856 /*
3857  * truncate(char *path, int pad, off_t length)
3858  *
3859  * Truncate a file given its path name.
3860  */
3861 int
3862 sys_truncate(struct truncate_args *uap)
3863 {
3864         struct nlookupdata nd;
3865         int error;
3866
3867         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3868         if (error == 0)
3869                 error = kern_truncate(&nd, uap->length);
3870         nlookup_done(&nd);
3871         return error;
3872 }
3873
3874 int
3875 kern_ftruncate(int fd, off_t length)
3876 {
3877         struct thread *td = curthread;
3878         struct vattr vattr;
3879         struct vnode *vp;
3880         struct file *fp;
3881         int error;
3882         uid_t uid = 0;
3883         gid_t gid = 0;
3884         uint64_t old_size = 0;
3885         struct mount *mp;
3886
3887         if (length < 0)
3888                 return(EINVAL);
3889         if ((error = holdvnode(td, fd, &fp)) != 0)
3890                 return (error);
3891         if (fp->f_nchandle.ncp) {
3892                 error = ncp_writechk(&fp->f_nchandle);
3893                 if (error)
3894                         goto done;
3895         }
3896         if ((fp->f_flag & FWRITE) == 0) {
3897                 error = EINVAL;
3898                 goto done;
3899         }
3900         if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3901                 error = EINVAL;
3902                 goto done;
3903         }
3904         vp = (struct vnode *)fp->f_data;
3905         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3906         if (vp->v_type == VDIR) {
3907                 error = EISDIR;
3908                 vn_unlock(vp);
3909                 goto done;
3910         }
3911
3912         if (vfs_quota_enabled) {
3913                 error = VOP_GETATTR(vp, &vattr);
3914                 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3915                 uid = vattr.va_uid;
3916                 gid = vattr.va_gid;
3917                 old_size = vattr.va_size;
3918         }
3919
3920         if ((error = vn_writechk(vp, NULL)) == 0) {
3921                 VATTR_NULL(&vattr);
3922                 vattr.va_size = length;
3923                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3924                 mp = vq_vptomp(vp);
3925                 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3926         }
3927         vn_unlock(vp);
3928 done:
3929         fdrop(fp);
3930         return (error);
3931 }
3932
3933 /*
3934  * ftruncate_args(int fd, int pad, off_t length)
3935  *
3936  * Truncate a file given a file descriptor.
3937  */
3938 int
3939 sys_ftruncate(struct ftruncate_args *uap)
3940 {
3941         int error;
3942
3943         error = kern_ftruncate(uap->fd, uap->length);
3944
3945         return (error);
3946 }
3947
3948 /*
3949  * fsync(int fd)
3950  *
3951  * Sync an open file.
3952  */
3953 int
3954 sys_fsync(struct fsync_args *uap)
3955 {
3956         struct thread *td = curthread;
3957         struct vnode *vp;
3958         struct file *fp;
3959         vm_object_t obj;
3960         int error;
3961
3962         if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3963                 return (error);
3964         vp = (struct vnode *)fp->f_data;
3965         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3966         if ((obj = vp->v_object) != NULL) {
3967                 if (vp->v_mount == NULL ||
3968                     (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3969                         vm_object_page_clean(obj, 0, 0, 0);
3970                 }
3971         }
3972         error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3973         if (error == 0 && vp->v_mount)
3974                 error = buf_fsync(vp);
3975         vn_unlock(vp);
3976         fdrop(fp);
3977
3978         return (error);
3979 }
3980
3981 int
3982 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3983 {
3984         struct nchandle fnchd;
3985         struct nchandle tnchd;
3986         struct namecache *ncp;
3987         struct vnode *fdvp;
3988         struct vnode *tdvp;
3989         struct mount *mp;
3990         int error;
3991         u_int fncp_gen;
3992         u_int tncp_gen;
3993
3994         bwillinode(1);
3995         fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3996         if ((error = nlookup(fromnd)) != 0)
3997                 return (error);
3998         if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3999                 return (ENOENT);
4000         fnchd.mount = fromnd->nl_nch.mount;
4001         cache_hold(&fnchd);
4002
4003         /*
4004          * unlock the source nch so we can lookup the target nch without
4005          * deadlocking.  The target may or may not exist so we do not check
4006          * for a target vp like kern_mkdir() and other creation functions do.
4007          *
4008          * The source and target directories are ref'd and rechecked after
4009          * everything is relocked to determine if the source or target file
4010          * has been renamed.
4011          */
4012         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4013         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4014
4015         fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4016
4017         cache_unlock(&fromnd->nl_nch);
4018
4019         tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4020         if ((error = nlookup(tond)) != 0) {
4021                 cache_drop(&fnchd);
4022                 return (error);
4023         }
4024         tncp_gen = tond->nl_nch.ncp->nc_generation;
4025
4026         if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4027                 cache_drop(&fnchd);
4028                 return (ENOENT);
4029         }
4030         tnchd.mount = tond->nl_nch.mount;
4031         cache_hold(&tnchd);
4032
4033         /*
4034          * If the source and target are the same there is nothing to do
4035          */
4036         if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4037                 cache_drop(&fnchd);
4038                 cache_drop(&tnchd);
4039                 return (0);
4040         }
4041
4042         /*
4043          * Mount points cannot be renamed or overwritten
4044          */
4045         if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4046             NCF_ISMOUNTPT
4047         ) {
4048                 cache_drop(&fnchd);
4049                 cache_drop(&tnchd);
4050                 return (EINVAL);
4051         }
4052
4053         /*
4054          * Relock the source ncp.  cache_relock() will deal with any
4055          * deadlocks against the already-locked tond and will also
4056          * make sure both are resolved.
4057          *
4058          * NOTE AFTER RELOCKING: The source or target ncp may have become
4059          * invalid while they were unlocked, nc_vp and nc_mount could
4060          * be NULL.
4061          */
4062         cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4063                      &tond->nl_nch, tond->nl_cred);
4064         fromnd->nl_flags |= NLC_NCPISLOCKED;
4065
4066         /*
4067          * If the namecache generation changed for either fromnd or tond,
4068          * we must retry.
4069          */
4070         if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4071             tond->nl_nch.ncp->nc_generation != tncp_gen) {
4072                 kprintf("kern_rename: retry due to gen on: "
4073                         "\"%s\" -> \"%s\"\n",
4074                         fromnd->nl_nch.ncp->nc_name,
4075                         tond->nl_nch.ncp->nc_name);
4076                 cache_drop(&fnchd);
4077                 cache_drop(&tnchd);
4078                 return (EAGAIN);
4079         }
4080
4081         /*
4082          * If either fromnd or tond are marked destroyed a ripout occured
4083          * out from under us and we must retry.
4084          */
4085         if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4086             fromnd->nl_nch.ncp->nc_vp == NULL ||
4087             (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4088                 kprintf("kern_rename: retry due to ripout on: "
4089                         "\"%s\" -> \"%s\"\n",
4090                         fromnd->nl_nch.ncp->nc_name,
4091                         tond->nl_nch.ncp->nc_name);
4092                 cache_drop(&fnchd);
4093                 cache_drop(&tnchd);
4094                 return (EAGAIN);
4095         }
4096
4097         /*
4098          * Make sure the parent directories linkages are the same.
4099          * XXX shouldn't be needed any more w/ generation check above.
4100          */
4101         if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4102             tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4103                 cache_drop(&fnchd);
4104                 cache_drop(&tnchd);
4105                 return (ENOENT);
4106         }
4107
4108         /*
4109          * Both the source and target must be within the same filesystem and
4110          * in the same filesystem as their parent directories within the
4111          * namecache topology.
4112          *
4113          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4114          */
4115         mp = fnchd.mount;
4116         if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4117             mp != tond->nl_nch.mount) {
4118                 cache_drop(&fnchd);
4119                 cache_drop(&tnchd);
4120                 return (EXDEV);
4121         }
4122
4123         /*
4124          * Make sure the mount point is writable
4125          */
4126         if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4127                 cache_drop(&fnchd);
4128                 cache_drop(&tnchd);
4129                 return (error);
4130         }
4131
4132         /*
4133          * If the target exists and either the source or target is a directory,
4134          * then both must be directories.
4135          *
4136          * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4137          * have become NULL.
4138          */
4139         if (tond->nl_nch.ncp->nc_vp) {
4140                 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4141                         error = ENOENT;
4142                 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4143                         if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4144                                 error = ENOTDIR;
4145                 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4146                         error = EISDIR;
4147                 }
4148         }
4149
4150         /*
4151          * You cannot rename a source into itself or a subdirectory of itself.
4152          * We check this by travsersing the target directory upwards looking
4153          * for a match against the source.
4154          *
4155          * XXX MPSAFE
4156          */
4157         if (error == 0) {
4158                 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4159                         if (fromnd->nl_nch.ncp == ncp) {
4160                                 error = EINVAL;
4161                                 break;
4162                         }
4163                 }
4164         }
4165
4166         cache_drop(&fnchd);
4167         cache_drop(&tnchd);
4168
4169         /*
4170          * Even though the namespaces are different, they may still represent
4171          * hardlinks to the same file.  The filesystem might have a hard time
4172          * with this so we issue a NREMOVE of the source instead of a NRENAME
4173          * when we detect the situation.
4174          */
4175         if (error == 0) {
4176                 fdvp = fromnd->nl_dvp;
4177                 tdvp = tond->nl_dvp;
4178                 if (fdvp == NULL || tdvp == NULL) {
4179                         error = EPERM;
4180                 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4181                         error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4182                                             fromnd->nl_cred);
4183                 } else {
4184                         error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, 
4185                                             fdvp, tdvp, tond->nl_cred);
4186                 }
4187         }
4188         return (error);
4189 }
4190
4191 /*
4192  * rename_args(char *from, char *to)
4193  *
4194  * Rename files.  Source and destination must either both be directories,
4195  * or both not be directories.  If target is a directory, it must be empty.
4196  */
4197 int
4198 sys_rename(struct rename_args *uap)
4199 {
4200         struct nlookupdata fromnd, tond;
4201         int error;
4202
4203         do {
4204                 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4205                 if (error == 0) {
4206                         error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4207                         if (error == 0)
4208                                 error = kern_rename(&fromnd, &tond);
4209                         nlookup_done(&tond);
4210                 }
4211                 nlookup_done(&fromnd);
4212         } while (error == EAGAIN);
4213         return (error);
4214 }
4215
4216 /*
4217  * renameat_args(int oldfd, char *old, int newfd, char *new)
4218  *
4219  * Rename files using paths relative to the directories associated with
4220  * oldfd and newfd.  Source and destination must either both be directories,
4221  * or both not be directories.  If target is a directory, it must be empty.
4222  */
4223 int
4224 sys_renameat(struct renameat_args *uap)
4225 {
4226         struct nlookupdata oldnd, newnd;
4227         struct file *oldfp, *newfp;
4228         int error;
4229
4230         do {
4231                 error = nlookup_init_at(&oldnd, &oldfp,
4232                                         uap->oldfd, uap->old,
4233                                         UIO_USERSPACE, 0);
4234                 if (error == 0) {
4235                         error = nlookup_init_at(&newnd, &newfp,
4236                                                 uap->newfd, uap->new,
4237                                                 UIO_USERSPACE, 0);
4238                         if (error == 0)
4239                                 error = kern_rename(&oldnd, &newnd);
4240                         nlookup_done_at(&newnd, newfp);
4241                 }
4242                 nlookup_done_at(&oldnd, oldfp);
4243         } while (error == EAGAIN);
4244         return (error);
4245 }
4246
4247 int
4248 kern_mkdir(struct nlookupdata *nd, int mode)
4249 {
4250         struct thread *td = curthread;
4251         struct proc *p = td->td_proc;
4252         struct vnode *vp;
4253         struct vattr vattr;
4254         int error;
4255
4256         bwillinode(1);
4257         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4258         if ((error = nlookup(nd)) != 0)
4259                 return (error);
4260
4261         if (nd->nl_nch.ncp->nc_vp)
4262                 return (EEXIST);
4263         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4264                 return (error);
4265         VATTR_NULL(&vattr);
4266         vattr.va_type = VDIR;
4267         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4268
4269         vp = NULL;
4270         error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4271         if (error == 0)
4272                 vput(vp);
4273         return (error);
4274 }
4275
4276 /*
4277  * mkdir_args(char *path, int mode)
4278  *
4279  * Make a directory file.
4280  */
4281 int
4282 sys_mkdir(struct mkdir_args *uap)
4283 {
4284         struct nlookupdata nd;
4285         int error;
4286
4287         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4288         if (error == 0)
4289                 error = kern_mkdir(&nd, uap->mode);
4290         nlookup_done(&nd);
4291         return (error);
4292 }
4293
4294 /*
4295  * mkdirat_args(int fd, char *path, mode_t mode)
4296  *
4297  * Make a directory file.  The path is relative to the directory associated
4298  * with fd.
4299  */
4300 int
4301 sys_mkdirat(struct mkdirat_args *uap)
4302 {
4303         struct nlookupdata nd;
4304         struct file *fp;
4305         int error;
4306
4307         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4308         if (error == 0)
4309                 error = kern_mkdir(&nd, uap->mode);
4310         nlookup_done_at(&nd, fp);
4311         return (error);
4312 }
4313
4314 int
4315 kern_rmdir(struct nlookupdata *nd)
4316 {
4317         int error;
4318
4319         bwillinode(1);
4320         nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4321         if ((error = nlookup(nd)) != 0)
4322                 return (error);
4323
4324         /*
4325          * Do not allow directories representing mount points to be
4326          * deleted, even if empty.  Check write perms on mount point
4327          * in case the vnode is aliased (aka nullfs).
4328          */
4329         if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4330                 return (EBUSY);
4331         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4332                 return (error);
4333         error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4334         return (error);
4335 }
4336
4337 /*
4338  * rmdir_args(char *path)
4339  *
4340  * Remove a directory file.
4341  */
4342 int
4343 sys_rmdir(struct rmdir_args *uap)
4344 {
4345         struct nlookupdata nd;
4346         int error;
4347
4348         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4349         if (error == 0)
4350                 error = kern_rmdir(&nd);
4351         nlookup_done(&nd);
4352         return (error);
4353 }
4354
4355 int
4356 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4357                    enum uio_seg direction)
4358 {
4359         struct thread *td = curthread;
4360         struct vnode *vp;
4361         struct file *fp;
4362         struct uio auio;
4363         struct iovec aiov;
4364         off_t loff;
4365         int error, eofflag;
4366
4367         if ((error = holdvnode(td, fd, &fp)) != 0)
4368                 return (error);
4369         if ((fp->f_flag & FREAD) == 0) {
4370                 error = EBADF;
4371                 goto done;
4372         }
4373         vp = (struct vnode *)fp->f_data;
4374         if (vp->v_type != VDIR) {
4375                 error = EINVAL;
4376                 goto done;
4377         }
4378         aiov.iov_base = buf;
4379         aiov.iov_len = count;
4380         auio.uio_iov = &aiov;
4381         auio.uio_iovcnt = 1;
4382         auio.uio_rw = UIO_READ;
4383         auio.uio_segflg = direction;
4384         auio.uio_td = td;
4385         auio.uio_resid = count;
4386         loff = auio.uio_offset = fp->f_offset;
4387         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4388         fp->f_offset = auio.uio_offset;
4389         if (error)
4390                 goto done;
4391
4392         /*
4393          * WARNING!  *basep may not be wide enough to accomodate the
4394          * seek offset.   XXX should we hack this to return the upper 32 bits
4395          * for offsets greater then 4G?
4396          */
4397         if (basep) {
4398                 *basep = (long)loff;
4399         }
4400         *res = count - auio.uio_resid;
4401 done:
4402         fdrop(fp);
4403         return (error);
4404 }
4405
4406 /*
4407  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4408  *
4409  * Read a block of directory entries in a file system independent format.
4410  */
4411 int
4412 sys_getdirentries(struct getdirentries_args *uap)
4413 {
4414         long base;
4415         int error;
4416
4417         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4418                                    &uap->sysmsg_result, UIO_USERSPACE);
4419
4420         if (error == 0 && uap->basep)
4421                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4422         return (error);
4423 }
4424
4425 /*
4426  * getdents_args(int fd, char *buf, size_t count)
4427  */
4428 int
4429 sys_getdents(struct getdents_args *uap)
4430 {
4431         int error;
4432
4433         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4434                                    &uap->sysmsg_result, UIO_USERSPACE);
4435
4436         return (error);
4437 }
4438
4439 /*
4440  * Set the mode mask for creation of filesystem nodes.
4441  *
4442  * umask(int newmask)
4443  */
4444 int
4445 sys_umask(struct umask_args *uap)
4446 {
4447         struct thread *td = curthread;
4448         struct proc *p = td->td_proc;
4449         struct filedesc *fdp;
4450
4451         fdp = p->p_fd;
4452         uap->sysmsg_result = fdp->fd_cmask;
4453         fdp->fd_cmask = uap->newmask & ALLPERMS;
4454         return (0);
4455 }
4456
4457 /*
4458  * revoke(char *path)
4459  *
4460  * Void all references to file by ripping underlying filesystem
4461  * away from vnode.
4462  */
4463 int
4464 sys_revoke(struct revoke_args *uap)
4465 {
4466         struct nlookupdata nd;
4467         struct vattr vattr;
4468         struct vnode *vp;
4469         struct ucred *cred;
4470         int error;
4471
4472         vp = NULL;
4473         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4474         if (error == 0)
4475                 error = nlookup(&nd);
4476         if (error == 0)
4477                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4478         cred = crhold(nd.nl_cred);
4479         nlookup_done(&nd);
4480         if (error == 0) {
4481                 if (error == 0)
4482                         error = VOP_GETATTR(vp, &vattr);
4483                 if (error == 0 && cred->cr_uid != vattr.va_uid)
4484                         error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4485                 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4486                         if (vcount(vp) > 0)
4487                                 error = vrevoke(vp, cred);
4488                 } else if (error == 0) {
4489                         error = vrevoke(vp, cred);
4490                 }
4491                 vrele(vp);
4492         }
4493         if (cred)
4494                 crfree(cred);
4495         return (error);
4496 }
4497
4498 /*
4499  * getfh_args(char *fname, fhandle_t *fhp)
4500  *
4501  * Get (NFS) file handle
4502  *
4503  * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4504  * mount.  This allows nullfs mounts to be explicitly exported. 
4505  *
4506  * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4507  *
4508  *          nullfs mounts of subdirectories are not safe.  That is, it will
4509  *          work, but you do not really have protection against access to
4510  *          the related parent directories.
4511  */
4512 int
4513 sys_getfh(struct getfh_args *uap)
4514 {
4515         struct thread *td = curthread;
4516         struct nlookupdata nd;
4517         fhandle_t fh;
4518         struct vnode *vp;
4519         struct mount *mp;
4520         int error;
4521
4522         /*
4523          * Must be super user
4524          */
4525         if ((error = priv_check(td, PRIV_ROOT)) != 0)
4526                 return (error);
4527
4528         vp = NULL;
4529         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4530         if (error == 0)
4531                 error = nlookup(&nd);
4532         if (error == 0)
4533                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4534         mp = nd.nl_nch.mount;
4535         nlookup_done(&nd);
4536         if (error == 0) {
4537                 bzero(&fh, sizeof(fh));
4538                 fh.fh_fsid = mp->mnt_stat.f_fsid;
4539                 error = VFS_VPTOFH(vp, &fh.fh_fid);
4540                 vput(vp);
4541                 if (error == 0)
4542                         error = copyout(&fh, uap->fhp, sizeof(fh));
4543         }
4544         return (error);
4545 }
4546
4547 /*
4548  * fhopen_args(const struct fhandle *u_fhp, int flags)
4549  *
4550  * syscall for the rpc.lockd to use to translate a NFS file handle into
4551  * an open descriptor.
4552  *
4553  * warning: do not remove the priv_check() call or this becomes one giant
4554  * security hole.
4555  */
4556 int
4557 sys_fhopen(struct fhopen_args *uap)
4558 {
4559         struct thread *td = curthread;
4560         struct filedesc *fdp = td->td_proc->p_fd;
4561         struct mount *mp;
4562         struct vnode *vp;
4563         struct fhandle fhp;
4564         struct vattr vat;
4565         struct vattr *vap = &vat;
4566         struct flock lf;
4567         int fmode, mode, error = 0, type;
4568         struct file *nfp; 
4569         struct file *fp;
4570         int indx;
4571
4572         /*
4573          * Must be super user
4574          */
4575         error = priv_check(td, PRIV_ROOT);
4576         if (error)
4577                 return (error);
4578
4579         fmode = FFLAGS(uap->flags);
4580
4581         /*
4582          * Why not allow a non-read/write open for our lockd?
4583          */
4584         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4585                 return (EINVAL);
4586         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4587         if (error)
4588                 return(error);
4589
4590         /*
4591          * Find the mount point
4592          */
4593         mp = vfs_getvfs(&fhp.fh_fsid);
4594         if (mp == NULL) {
4595                 error = ESTALE;
4596                 goto done2;
4597         }
4598         /* now give me my vnode, it gets returned to me locked */
4599         error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4600         if (error)
4601                 goto done;
4602         /*
4603          * from now on we have to make sure not
4604          * to forget about the vnode
4605          * any error that causes an abort must vput(vp) 
4606          * just set error = err and 'goto bad;'.
4607          */
4608
4609         /* 
4610          * from vn_open 
4611          */
4612         if (vp->v_type == VLNK) {
4613                 error = EMLINK;
4614                 goto bad;
4615         }
4616         if (vp->v_type == VSOCK) {
4617                 error = EOPNOTSUPP;
4618                 goto bad;
4619         }
4620         mode = 0;
4621         if (fmode & (FWRITE | O_TRUNC)) {
4622                 if (vp->v_type == VDIR) {
4623                         error = EISDIR;
4624                         goto bad;
4625                 }
4626                 error = vn_writechk(vp, NULL);
4627                 if (error)
4628                         goto bad;
4629                 mode |= VWRITE;
4630         }
4631         if (fmode & FREAD)
4632                 mode |= VREAD;
4633         if (mode) {
4634                 error = VOP_ACCESS(vp, mode, td->td_ucred);
4635                 if (error)
4636                         goto bad;
4637         }
4638         if (fmode & O_TRUNC) {
4639                 vn_unlock(vp);                          /* XXX */
4640                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
4641                 VATTR_NULL(vap);
4642                 vap->va_size = 0;
4643                 error = VOP_SETATTR(vp, vap, td->td_ucred);
4644                 if (error)
4645                         goto bad;
4646         }
4647
4648         /*
4649          * VOP_OPEN needs the file pointer so it can potentially override
4650          * it.
4651          *
4652          * WARNING! no f_nchandle will be associated when fhopen()ing a
4653          * directory.  XXX
4654          */
4655         if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4656                 goto bad;
4657         fp = nfp;
4658
4659         error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4660         if (error) {
4661                 /*
4662                  * setting f_ops this way prevents VOP_CLOSE from being
4663                  * called or fdrop() releasing the vp from v_data.   Since
4664                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
4665                  */
4666                 fp->f_ops = &badfileops;
4667                 fp->f_data = NULL;
4668                 goto bad_drop;
4669         }
4670
4671         /*
4672          * The fp is given its own reference, we still have our ref and lock.
4673          *
4674          * Assert that all regular files must be created with a VM object.
4675          */
4676         if (vp->v_type == VREG && vp->v_object == NULL) {
4677                 kprintf("fhopen: regular file did not "
4678                         "have VM object: %p\n",
4679                         vp);
4680                 goto bad_drop;
4681         }
4682
4683         /*
4684          * The open was successful.  Handle any locking requirements.
4685          */
4686         if (fmode & (O_EXLOCK | O_SHLOCK)) {
4687                 lf.l_whence = SEEK_SET;
4688                 lf.l_start = 0;
4689                 lf.l_len = 0;
4690                 if (fmode & O_EXLOCK)
4691                         lf.l_type = F_WRLCK;
4692                 else
4693                         lf.l_type = F_RDLCK;
4694                 if (fmode & FNONBLOCK)
4695                         type = 0;
4696                 else
4697                         type = F_WAIT;
4698                 vn_unlock(vp);
4699                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4700                                          &lf, type)) != 0) {
4701                         /*
4702                          * release our private reference.
4703                          */
4704                         fsetfd(fdp, NULL, indx);
4705                         fdrop(fp);
4706                         vrele(vp);
4707                         goto done;
4708                 }
4709                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4710                 atomic_set_int(&fp->f_flag, FHASLOCK);  /* race ok */
4711         }
4712
4713         /*
4714          * Clean up.  Associate the file pointer with the previously
4715          * reserved descriptor and return it.
4716          */
4717         vput(vp);
4718         if (uap->flags & O_CLOEXEC)
4719                 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4720         fsetfd(fdp, fp, indx);
4721         fdrop(fp);
4722         uap->sysmsg_result = indx;
4723         mount_drop(mp);
4724
4725         return (error);
4726
4727 bad_drop:
4728         fsetfd(fdp, NULL, indx);
4729         fdrop(fp);
4730 bad:
4731         vput(vp);
4732 done:
4733         mount_drop(mp);
4734 done2:
4735         return (error);
4736 }
4737
4738 /*
4739  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4740  */
4741 int
4742 sys_fhstat(struct fhstat_args *uap)
4743 {
4744         struct thread *td = curthread;
4745         struct stat sb;
4746         fhandle_t fh;
4747         struct mount *mp;
4748         struct vnode *vp;
4749         int error;
4750
4751         /*
4752          * Must be super user
4753          */
4754         error = priv_check(td, PRIV_ROOT);
4755         if (error)
4756                 return (error);
4757         
4758         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4759         if (error)
4760                 return (error);
4761
4762         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4763                 error = ESTALE;
4764         if (error == 0) {
4765                 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4766                         error = vn_stat(vp, &sb, td->td_ucred);
4767                         vput(vp);
4768                 }
4769         }
4770         if (error == 0)
4771                 error = copyout(&sb, uap->sb, sizeof(sb));
4772         if (mp)
4773                 mount_drop(mp);
4774
4775         return (error);
4776 }
4777
4778 /*
4779  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4780  */
4781 int
4782 sys_fhstatfs(struct fhstatfs_args *uap)
4783 {
4784         struct thread *td = curthread;
4785         struct proc *p = td->td_proc;
4786         struct statfs *sp;
4787         struct mount *mp;
4788         struct vnode *vp;
4789         struct statfs sb;
4790         char *fullpath, *freepath;
4791         fhandle_t fh;
4792         int error;
4793
4794         /*
4795          * Must be super user
4796          */
4797         if ((error = priv_check(td, PRIV_ROOT)))
4798                 return (error);
4799
4800         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4801                 return (error);
4802
4803         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4804                 error = ESTALE;
4805                 goto done;
4806         }
4807         if (p != NULL && !chroot_visible_mnt(mp, p)) {
4808                 error = ESTALE;
4809                 goto done;
4810         }
4811
4812         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4813                 goto done;
4814         mp = vp->v_mount;
4815         sp = &mp->mnt_stat;
4816         vput(vp);
4817         if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4818                 goto done;
4819
4820         error = mount_path(p, mp, &fullpath, &freepath);
4821         if (error)
4822                 goto done;
4823         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4824         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4825         kfree(freepath, M_TEMP);
4826
4827         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4828         if (priv_check(td, PRIV_ROOT)) {
4829                 bcopy(sp, &sb, sizeof(sb));
4830                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4831                 sp = &sb;
4832         }
4833         error = copyout(sp, uap->buf, sizeof(*sp));
4834 done:
4835         if (mp)
4836                 mount_drop(mp);
4837
4838         return (error);
4839 }
4840
4841 /*
4842  * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4843  */
4844 int
4845 sys_fhstatvfs(struct fhstatvfs_args *uap)
4846 {
4847         struct thread *td = curthread;
4848         struct proc *p = td->td_proc;
4849         struct statvfs *sp;
4850         struct mount *mp;
4851         struct vnode *vp;
4852         fhandle_t fh;
4853         int error;
4854
4855         /*
4856          * Must be super user
4857          */
4858         if ((error = priv_check(td, PRIV_ROOT)))
4859                 return (error);
4860
4861         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4862                 return (error);
4863
4864         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4865                 error = ESTALE;
4866                 goto done;
4867         }
4868         if (p != NULL && !chroot_visible_mnt(mp, p)) {
4869                 error = ESTALE;
4870                 goto done;
4871         }
4872
4873         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4874                 goto done;
4875         mp = vp->v_mount;
4876         sp = &mp->mnt_vstat;
4877         vput(vp);
4878         if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4879                 goto done;
4880
4881         sp->f_flag = 0;
4882         if (mp->mnt_flag & MNT_RDONLY)
4883                 sp->f_flag |= ST_RDONLY;
4884         if (mp->mnt_flag & MNT_NOSUID)
4885                 sp->f_flag |= ST_NOSUID;
4886         error = copyout(sp, uap->buf, sizeof(*sp));
4887 done:
4888         if (mp)
4889                 mount_drop(mp);
4890         return (error);
4891 }
4892
4893
4894 /*
4895  * Syscall to push extended attribute configuration information into the
4896  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
4897  * a command (int cmd), and attribute name and misc data.  For now, the
4898  * attribute name is left in userspace for consumption by the VFS_op.
4899  * It will probably be changed to be copied into sysspace by the
4900  * syscall in the future, once issues with various consumers of the
4901  * attribute code have raised their hands.
4902  *
4903  * Currently this is used only by UFS Extended Attributes.
4904  */
4905 int
4906 sys_extattrctl(struct extattrctl_args *uap)
4907 {
4908         struct nlookupdata nd;
4909         struct vnode *vp;
4910         char attrname[EXTATTR_MAXNAMELEN];
4911         int error;
4912         size_t size;
4913
4914         attrname[0] = 0;
4915         vp = NULL;
4916         error = 0;
4917
4918         if (error == 0 && uap->filename) {
4919                 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4920                                      NLC_FOLLOW);
4921                 if (error == 0)
4922                         error = nlookup(&nd);
4923                 if (error == 0)
4924                         error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4925                 nlookup_done(&nd);
4926         }
4927
4928         if (error == 0 && uap->attrname) {
4929                 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4930                                   &size);
4931         }
4932
4933         if (error == 0) {
4934                 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4935                 if (error == 0)
4936                         error = nlookup(&nd);
4937                 if (error == 0)
4938                         error = ncp_writechk(&nd.nl_nch);
4939                 if (error == 0) {
4940                         error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4941                                                uap->attrnamespace,
4942                                                uap->attrname, nd.nl_cred);
4943                 }
4944                 nlookup_done(&nd);
4945         }
4946
4947         return (error);
4948 }
4949
4950 /*
4951  * Syscall to get a named extended attribute on a file or directory.
4952  */
4953 int
4954 sys_extattr_set_file(struct extattr_set_file_args *uap)
4955 {
4956         char attrname[EXTATTR_MAXNAMELEN];
4957         struct nlookupdata nd;
4958         struct vnode *vp;
4959         struct uio auio;
4960         struct iovec aiov;
4961         int error;
4962
4963         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4964         if (error)
4965                 return (error);
4966
4967         vp = NULL;
4968
4969         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4970         if (error == 0)
4971                 error = nlookup(&nd);
4972         if (error == 0)
4973                 error = ncp_writechk(&nd.nl_nch);
4974         if (error == 0)
4975                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4976         if (error) {
4977                 nlookup_done(&nd);
4978                 return (error);
4979         }
4980
4981         bzero(&auio, sizeof(auio));
4982         aiov.iov_base = uap->data;
4983         aiov.iov_len = uap->nbytes;
4984         auio.uio_iov = &aiov;
4985         auio.uio_iovcnt = 1;
4986         auio.uio_offset = 0;
4987         auio.uio_resid = uap->nbytes;
4988         auio.uio_rw = UIO_WRITE;
4989         auio.uio_td = curthread;
4990
4991         error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4992                                &auio, nd.nl_cred);
4993
4994         vput(vp);
4995         nlookup_done(&nd);
4996         return (error);
4997 }
4998
4999 /*
5000  * Syscall to get a named extended attribute on a file or directory.
5001  */
5002 int
5003 sys_extattr_get_file(struct extattr_get_file_args *uap)
5004 {
5005         char attrname[EXTATTR_MAXNAMELEN];
5006         struct nlookupdata nd;
5007         struct uio auio;
5008         struct iovec aiov;
5009         struct vnode *vp;
5010         int error;
5011
5012         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5013         if (error)
5014                 return (error);
5015
5016         vp = NULL;
5017
5018         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5019         if (error == 0)
5020                 error = nlookup(&nd);
5021         if (error == 0)
5022                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5023         if (error) {
5024                 nlookup_done(&nd);
5025                 return (error);
5026         }
5027
5028         bzero(&auio, sizeof(auio));
5029         aiov.iov_base = uap->data;
5030         aiov.iov_len = uap->nbytes;
5031         auio.uio_iov = &aiov;
5032         auio.uio_iovcnt = 1;
5033         auio.uio_offset = 0;
5034         auio.uio_resid = uap->nbytes;
5035         auio.uio_rw = UIO_READ;
5036         auio.uio_td = curthread;
5037
5038         error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5039                                 &auio, nd.nl_cred);
5040         uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5041
5042         vput(vp);
5043         nlookup_done(&nd);
5044         return(error);
5045 }
5046
5047 /*
5048  * Syscall to delete a named extended attribute from a file or directory.
5049  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
5050  */
5051 int
5052 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5053 {
5054         char attrname[EXTATTR_MAXNAMELEN];
5055         struct nlookupdata nd;
5056         struct vnode *vp;
5057         int error;
5058
5059         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5060         if (error)
5061                 return(error);
5062
5063         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5064         if (error == 0)
5065                 error = nlookup(&nd);
5066         if (error == 0)
5067                 error = ncp_writechk(&nd.nl_nch);
5068         if (error == 0) {
5069                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5070                 if (error == 0) {
5071                         error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5072                                                attrname, NULL, nd.nl_cred);
5073                         vput(vp);
5074                 }
5075         }
5076         nlookup_done(&nd);
5077         return(error);
5078 }
5079
5080 /*
5081  * Determine if the mount is visible to the process.
5082  */
5083 static int
5084 chroot_visible_mnt(struct mount *mp, struct proc *p)
5085 {
5086         struct nchandle nch;
5087
5088         /*
5089          * Traverse from the mount point upwards.  If we hit the process
5090          * root then the mount point is visible to the process.
5091          */
5092         nch = mp->mnt_ncmountpt;
5093         while (nch.ncp) {
5094                 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5095                     nch.ncp == p->p_fd->fd_nrdir.ncp) {
5096                         return(1);
5097                 }
5098                 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5099                         nch = nch.mount->mnt_ncmounton;
5100                 } else {
5101                         nch.ncp = nch.ncp->nc_parent;
5102                 }
5103         }
5104
5105         /*
5106          * If the mount point is not visible to the process, but the
5107          * process root is in a subdirectory of the mount, return
5108          * TRUE anyway.
5109          */
5110         if (p->p_fd->fd_nrdir.mount == mp)
5111                 return(1);
5112
5113         return(0);
5114 }
5115