]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/miscfs/devfs/devfs_vfsops.c
Distribute and statizice a lot of the malloc M_* types.
[FreeBSD/FreeBSD.git] / sys / miscfs / devfs / devfs_vfsops.c
1 /*
2  *  Written by Julian Elischer (julian@DIALix.oz.au)
3  *
4  *      $Header: /home/ncvs/src/sys/miscfs/devfs/devfs_vfsops.c,v 1.20 1997/08/16 19:15:10 wollman Exp $
5  *
6  *
7  */
8
9 #include <sys/param.h>
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
12 #include <sys/vnode.h>
13 #include <sys/mount.h>
14 #include <sys/malloc.h>
15
16 #include <miscfs/devfs/devfsdefs.h>
17
18 MALLOC_DEFINE(M_DEVFSMNT, "DEVFS mount", "DEVFS mount structure");
19
20 static int devfs_statfs( struct mount *mp, struct statfs *sbp, struct proc *p);
21 static int mountdevfs( struct mount *mp, struct proc *p);
22
23 static int
24 devfs_init(struct vfsconf *vfsp)
25 {
26         /*
27          * fill in the missing members on the "hidden" mount
28          */
29         dev_root->dnp->dvm->mount->mnt_op  = vfsp->vfc_vfsops; 
30         dev_root->dnp->dvm->mount->mnt_vfc = vfsp;
31
32         /* Mark a reference for the "invisible" blueprint mount */
33         dev_root->dnp->dvm->mount->mnt_vfc->vfc_refcount++;
34
35         printf("DEVFS: ready to run\n");
36         return 0; /*XXX*/
37 }
38
39 /*
40  *  mp   - pointer to 'mount' structure
41  *  path - addr in user space of mount point (ie /usr or whatever)
42  *  data - addr in user space of mount params including the
43  *         name of the block special file to treat as a filesystem.
44  *  ndp  - namei data pointer
45  *  p    - proc pointer
46  * devfs is special in that it doesn't require any device to be mounted..
47  * It makes up it's data as it goes along.
48  * it must be mounted during single user.. until it is, only std{in/out/err}
49  * and the root filesystem are available.
50  */
51 /*proto*/
52 int
53 devfs_mount(struct mount *mp, char *path, caddr_t data,
54             struct nameidata *ndp, struct proc *p)
55 {
56         struct devfsmount *devfs_mp_p;  /* devfs specific mount control block   */
57         int error;
58         u_int size;
59
60 DBPRINT(("mount "));
61 /*
62  *  If they just want to update, we don't need to do anything.
63  */
64         if (mp->mnt_flag & MNT_UPDATE)
65         {
66                 return 0;
67         }
68
69 /*
70  *  Well, it's not an update, it's a real mount request.
71  *  Time to get dirty.
72  * HERE we should check to see if we are already mounted here.
73  */
74         if(error =  mountdevfs( mp, p))
75                 return (error);
76
77 /*
78  *  Copy in the name of the directory the filesystem
79  *  is to be mounted on.
80  *  And we clear the remainder of the character strings
81  *  to be tidy.
82  *  Then, we try to fill in the filesystem stats structure
83  *  as best we can with whatever we can think of at the time
84  */
85         devfs_mp_p = (struct devfsmount *)mp->mnt_data;
86         if(devfs_up_and_going) {
87                 copyinstr(path, (caddr_t)mp->mnt_stat.f_mntonname,
88                         sizeof(mp->mnt_stat.f_mntonname)-1, &size);
89                 bzero(mp->mnt_stat.f_mntonname + size,
90                         sizeof(mp->mnt_stat.f_mntonname) - size);
91         } else {
92                 bcopy("dummy_mount", (caddr_t)mp->mnt_stat.f_mntonname,12);
93         }
94         bzero(mp->mnt_stat.f_mntfromname , MNAMELEN );
95         bcopy("devfs",mp->mnt_stat.f_mntfromname, 5);
96         (void)devfs_statfs(mp, &mp->mnt_stat, p);
97         return 0;
98 }
99
100 static int
101 mountdevfs( struct mount *mp, struct proc *p)
102 {
103         int error = 0;
104         struct devfsmount *devfs_mp_p;
105
106
107         devfs_mp_p = (struct devfsmount *)malloc(sizeof *devfs_mp_p,
108                                                 M_DEVFSMNT, M_WAITOK);
109         bzero(devfs_mp_p,sizeof(*devfs_mp_p));
110         devfs_mp_p->mount = mp;
111
112 /*
113  *  Fill out some fields
114  */
115         mp->mnt_data = (qaddr_t)devfs_mp_p;
116         mp->mnt_stat.f_type = MOUNT_DEVFS;
117         mp->mnt_stat.f_fsid.val[0] = (long)devfs_mp_p;
118         mp->mnt_stat.f_fsid.val[1] = MOUNT_DEVFS;
119         mp->mnt_flag |= MNT_LOCAL;
120
121         if(error = dev_dup_plane(devfs_mp_p))
122         {
123                 mp->mnt_data = (qaddr_t)0;
124                 free((caddr_t)devfs_mp_p, M_DEVFSMNT);
125         }
126         return error;
127 }
128
129 static int
130 devfs_start(struct mount *mp, int flags, struct proc *p)
131 {
132 DBPRINT(("start "));
133         return 0;
134 }
135
136 /*
137  *  Unmount the filesystem described by mp.
138  */
139 static int
140 devfs_unmount( struct mount *mp, int mntflags, struct proc *p)
141 {
142         struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
143         int flags = 0;
144         int error;
145         
146         if (mntflags & MNT_FORCE) {
147                 flags |= FORCECLOSE;
148         }
149         error = vflush(mp, NULLVP, flags);
150         if (error)
151                 return error;
152
153 DBPRINT(("unmount "));
154         devfs_free_plane(devfs_mp_p);
155         free((caddr_t)devfs_mp_p, M_DEVFSMNT);
156         mp->mnt_data = (qaddr_t)0;
157         mp->mnt_flag &= ~MNT_LOCAL;
158
159         return 0;
160 }
161
162 /* return the address of the root vnode  in *vpp */
163 static int
164 devfs_root(struct mount *mp, struct vnode **vpp)
165 {
166         struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data);
167
168 DBPRINT(("root "));
169         devfs_dntovn(devfs_mp_p->plane_root->dnp,vpp);
170         return 0;
171 }
172
173 static int
174 devfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
175                struct proc *p)
176 {
177 DBPRINT(("quotactl "));
178         return EOPNOTSUPP;
179 }
180
181 static int
182 devfs_statfs( struct mount *mp, struct statfs *sbp, struct proc *p)
183 {
184         struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
185
186 /*
187  *  Fill in the stat block.
188  */
189 DBPRINT(("statfs "));
190         sbp->f_type   = MOUNT_DEVFS;
191         sbp->f_flags  = 0;              /* XXX */
192         sbp->f_bsize  = 128;
193         sbp->f_iosize = 1024;   /* XXX*/
194         sbp->f_blocks = 128;
195         sbp->f_bfree  = 0;
196         sbp->f_bavail = 0;
197         sbp->f_files  = 128;
198         sbp->f_ffree  = 0;              /* what to put in here? */
199         sbp->f_fsid.val[0] = (long)devfs_mp_p;
200         sbp->f_fsid.val[1] = MOUNT_DEVFS;
201
202 /*
203  *  Copy the mounted on and mounted from names into
204  *  the passed in stat block, if it is not the one
205  *  in the mount structure.
206  */
207         if (sbp != &mp->mnt_stat) {
208                 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
209                         (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
210                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
211                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
212         }
213         return 0;
214 }
215
216 /*
217  * Go through the disk queues to initiate sandbagged IO;
218  * go through the inodes to write those that have been modified;
219  * initiate the writing of the super block if it has been modified.
220  *
221  * Note: we are always called with the filesystem marked `MPBUSY'.
222  */
223 static int
224 devfs_sync(struct mount *mp, int waitfor,struct ucred *cred,struct proc *p)
225 {
226         register struct vnode *vp, *nvp;
227         struct timeval tv;
228         int error, allerror = 0;
229
230 DBPRINT(("sync "));
231
232         /*
233          * Write back modified superblock.
234          * Consistency check that the superblock
235          * is still in the buffer cache.
236          */
237         /*
238          * Write back each (modified) inode.
239          */
240 loop:
241         for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
242                 /*
243                  * If the vnode that we are about to sync is no longer
244                  * associated with this mount point, start over.
245                  */
246                 if (vp->v_mount != mp)
247                         goto loop;
248                 nvp = vp->v_mntvnodes.le_next;
249                 if (VOP_ISLOCKED(vp))
250                         continue;
251                 if ( vp->v_dirtyblkhd.lh_first == NULL)
252                         continue;
253                 if (vp->v_type == VBLK) {
254                         if (vget(vp, LK_EXCLUSIVE, p))
255                                 goto loop;
256                         error = VOP_FSYNC(vp, cred, waitfor, p);
257                         if (error)
258                                 allerror = error;
259                         vput(vp);
260                 }
261 #ifdef NOTYET
262                 else {
263                         tv = time;
264                         /* VOP_UPDATE(vp, &tv, &tv, waitfor == MNT_WAIT); */
265                         VOP_UPDATE(vp, &tv, &tv, 0);
266                 }
267 #endif
268         }
269         /*
270          * Force stale file system control information to be flushed.
271          *( except that htat makes no sense with devfs
272          */
273         return (allerror);
274 }
275
276 static int
277 devfs_vget(struct mount *mp, ino_t ino,struct vnode **vpp)
278 {
279 DBPRINT(("vget "));
280         return EOPNOTSUPP;
281 }
282
283 /*************************************************************
284  * The concept of exporting a kernel generated devfs is stupid
285  * So don't handle filehandles
286  */
287
288 static int
289 devfs_fhtovp (struct mount *mp, struct fid *fhp, struct sockaddr *nam,
290               struct vnode **vpp, int *exflagsp, struct ucred **credanonp)
291 {
292 DBPRINT(("fhtovp "));
293         return (EINVAL);
294 }
295
296
297 static int
298 devfs_vptofh (struct vnode *vp, struct fid *fhp)
299 {
300 DBPRINT(("vptofh "));
301         return (EINVAL);
302 }
303
304 static struct vfsops devfs_vfsops = {
305         devfs_mount,
306         devfs_start,
307         devfs_unmount,
308         devfs_root,
309         devfs_quotactl,
310         devfs_statfs,
311         devfs_sync,
312         devfs_vget,
313         devfs_fhtovp,
314         devfs_vptofh,
315         devfs_init
316 };
317
318 VFS_SET(devfs_vfsops, devfs, MOUNT_DEVFS, 0);