Merge from vendor branch HEIMDAL:
[dragonfly.git] / sys / vfs / fdesc / fdesc_vfsops.c
1 /*
2  * Copyright (c) 1992, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)fdesc_vfsops.c      8.4 (Berkeley) 1/21/94
37  *
38  * $FreeBSD: src/sys/miscfs/fdesc/fdesc_vfsops.c,v 1.22.2.3 2002/08/23 17:42:39 njl Exp $
39  * $DragonFly: src/sys/vfs/fdesc/fdesc_vfsops.c,v 1.11 2004/12/17 00:18:18 dillon Exp $
40  */
41
42 /*
43  * /dev/fd Filesystem
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57
58 #include "fdesc.h"
59
60 extern struct vnodeopv_entry_desc fdesc_vnodeop_entries[];
61
62 static MALLOC_DEFINE(M_FDESCMNT, "FDESC mount", "FDESC mount structure");
63
64 static int      fdesc_mount (struct mount *mp, char *path, caddr_t data,
65                                  struct thread *td);
66 static int      fdesc_unmount (struct mount *mp, int mntflags,
67                                    struct thread *td);
68 static int      fdesc_statfs (struct mount *mp, struct statfs *sbp,
69                                   struct thread *td);
70   
71 /*
72  * Mount the per-process file descriptors (/dev/fd)
73  */
74 static int
75 fdesc_mount(struct mount *mp, char *path, caddr_t data, struct thread *td)
76 {
77         int error = 0;
78         struct fdescmount *fmp;
79         struct vnode *rvp;
80         size_t size;
81
82         if (path == NULL)
83                 panic("fdesc_mount: cannot mount as root");
84
85         /*
86          * Update is a no-op
87          */
88         if (mp->mnt_flag & MNT_UPDATE)
89                 return (EOPNOTSUPP);
90
91         vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, fdesc_vnodeop_entries);
92
93         error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp, td);
94         if (error)
95                 return (error);
96
97         MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
98                                 M_FDESCMNT, M_WAITOK);  /* XXX */
99         rvp->v_type = VDIR;
100         rvp->v_flag |= VROOT;
101         fmp->f_root = rvp;
102         /* XXX -- don't mark as local to work around fts() problems */
103         /*mp->mnt_flag |= MNT_LOCAL;*/
104         mp->mnt_data = (qaddr_t) fmp;
105         vfs_getnewfsid(mp);
106
107         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
108         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
109         bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
110         bcopy("fdesc", mp->mnt_stat.f_mntfromname, sizeof("fdesc"));
111         (void)fdesc_statfs(mp, &mp->mnt_stat, td);
112         return (0);
113 }
114
115 static int
116 fdesc_unmount(struct mount *mp, int mntflags, struct thread *td)
117 {
118         int error;
119         int flags = 0;
120
121         if (mntflags & MNT_FORCE)
122                 flags |= FORCECLOSE;
123
124         /*
125          * Clear out buffer cache.  I don't think we
126          * ever get anything cached at this level at the
127          * moment, but who knows...
128          *
129          * There is 1 extra root vnode reference corresponding
130          * to f_root.
131          */
132         if ((error = vflush(mp, 1, flags)) != 0)
133                 return (error);
134
135         /*
136          * Finally, throw away the fdescmount structure
137          */
138         free(mp->mnt_data, M_FDESCMNT); /* XXX */
139         mp->mnt_data = 0;
140
141         return (0);
142 }
143
144 int
145 fdesc_root(struct mount *mp, struct vnode **vpp)
146 {
147         struct thread *td = curthread;  /* XXX */
148         struct vnode *vp;
149
150         /*
151          * Return locked reference to root.
152          */
153         vp = VFSTOFDESC(mp)->f_root;
154         vref(vp);
155         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
156         *vpp = vp;
157         return (0);
158 }
159
160 static int
161 fdesc_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
162 {
163         struct proc *p = td->td_proc;
164         struct filedesc *fdp;
165         int lim;
166         int i;
167         int last;
168         int freefd;
169
170         KKASSERT(p);
171
172         /*
173          * Compute number of free file descriptors.
174          * [ Strange results will ensue if the open file
175          * limit is ever reduced below the current number
176          * of open files... ]
177          */
178         lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
179         fdp = p->p_fd;
180         last = min(fdp->fd_nfiles, lim);
181         freefd = 0;
182         for (i = fdp->fd_freefile; i < last; i++)
183                 if (fdp->fd_ofiles[i] == NULL)
184                         freefd++;
185
186         /*
187          * Adjust for the fact that the fdesc array may not
188          * have been fully allocated yet.
189          */
190         if (fdp->fd_nfiles < lim)
191                 freefd += (lim - fdp->fd_nfiles);
192
193         sbp->f_flags = 0;
194         sbp->f_bsize = DEV_BSIZE;
195         sbp->f_iosize = DEV_BSIZE;
196         sbp->f_blocks = 2;              /* 1K to keep df happy */
197         sbp->f_bfree = 0;
198         sbp->f_bavail = 0;
199         sbp->f_files = lim + 1;         /* Allow for "." */
200         sbp->f_ffree = freefd;          /* See comments above */
201         if (sbp != &mp->mnt_stat) {
202                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
203                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
204                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
205                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
206         }
207         return (0);
208 }
209
210 static struct vfsops fdesc_vfsops = {
211         fdesc_mount,
212         vfs_stdstart,
213         fdesc_unmount,
214         fdesc_root,
215         vfs_stdquotactl,
216         fdesc_statfs,
217         vfs_stdsync,
218         vfs_stdvget,
219         vfs_stdfhtovp,
220         vfs_stdcheckexp,
221         vfs_stdvptofh,
222         fdesc_init,
223         vfs_stduninit,
224         vfs_stdextattrctl,
225 };
226
227 VFS_SET(fdesc_vfsops, fdesc, VFCF_SYNTHETIC);