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