1 /* This file contains the table with device <-> driver mappings. It also
2 * contains some routines to dynamically add and/ or remove device drivers
12 #include <minix/callnr.h>
15 /* The order of the entries in the table determines the mapping between major
16 * device numbers and device drivers. Character and block devices
17 * can be intermixed at random. The ordering determines the device numbers in
18 * /dev. Note that the major device numbers used in /dev are NOT the same as
19 * the process numbers of the device drivers. See <minix/dmap.h> for mappings.
22 struct dmap dmap
[NR_DEVICES
];
24 /*===========================================================================*
26 *===========================================================================*/
27 void lock_dmap(struct dmap
*dp
)
30 struct worker_thread
*org_self
;
34 assert(dp
->dmap_driver
!= NONE
);
36 org_self
= worker_suspend();
38 if ((r
= mutex_lock(&dp
->dmap_lock
)) != 0)
39 panic("unable to get a lock on dmap: %d\n", r
);
41 worker_resume(org_self
);
44 /*===========================================================================*
46 *===========================================================================*/
47 void unlock_dmap(struct dmap
*dp
)
54 if ((r
= mutex_unlock(&dp
->dmap_lock
)) != 0)
55 panic("unable to unlock dmap lock: %d\n", r
);
58 /*===========================================================================*
60 *===========================================================================*/
61 static int map_driver(const char label
[LABEL_MAX
], devmajor_t major
,
64 /* Add a new device driver mapping in the dmap table. If the proc_nr is set to
65 * NONE, we're supposed to unmap it.
70 /* Get pointer to device entry in the dmap table. */
71 if (major
< 0 || major
>= NR_DEVICES
) return(ENODEV
);
74 /* Check if we're supposed to unmap it. */
75 if (proc_nr_e
== NONE
) {
76 /* Even when a driver is now unmapped and is shortly to be mapped in
77 * due to recovery, invalidate associated filps if they're character
78 * special files. More sophisticated recovery mechanisms which would
79 * reduce the need to invalidate files are possible, but would require
80 * cooperation of the driver and more recovery framework between RS,
83 invalidate_filp_by_char_major(major
);
84 dp
->dmap_driver
= NONE
;
90 if (len
+1 > sizeof(dp
->dmap_label
)) {
91 printf("VFS: map_driver: label too long: %d\n", len
);
94 strlcpy(dp
->dmap_label
, label
, sizeof(dp
->dmap_label
));
97 /* Store driver I/O routines based on type of device */
98 dp
->dmap_driver
= proc_nr_e
;
103 /*===========================================================================*
105 *===========================================================================*/
106 int do_mapdriver(void)
108 /* Create a device->driver mapping. RS will tell us which major is driven by
109 * this driver, what type of device it is (regular, TTY, asynchronous, clone,
110 * etc), and its label. This label is registered with DS, and allows us to
111 * retrieve the driver's endpoint.
118 char label
[LABEL_MAX
];
121 /* Only RS can map drivers. */
122 if (who_e
!= RS_PROC_NR
) return(EPERM
);
124 label_vir
= job_m_in
.m_lsys_vfs_mapdriver
.label
;
125 label_len
= job_m_in
.m_lsys_vfs_mapdriver
.labellen
;
126 major
= job_m_in
.m_lsys_vfs_mapdriver
.major
;
129 if (label_len
> sizeof(label
)) { /* Can we store this label? */
130 printf("VFS: do_mapdriver: label too long\n");
133 r
= sys_vircopy(who_e
, label_vir
, SELF
, (vir_bytes
) label
, label_len
,
136 printf("VFS: do_mapdriver: sys_vircopy failed: %d\n", r
);
139 if (label
[label_len
-1] != '\0') {
140 printf("VFS: do_mapdriver: label not null-terminated\n");
144 /* Now we know how the driver is called, fetch its endpoint */
145 r
= ds_retrieve_label_endpt(label
, &endpoint
);
147 printf("VFS: do_mapdriver: label '%s' unknown\n", label
);
151 /* Process is a service */
152 if (isokendpt(endpoint
, &slot
) != OK
) {
153 printf("VFS: can't map driver to unknown endpoint %d\n", endpoint
);
157 rfp
->fp_flags
|= FP_SRV_PROC
;
159 /* Try to update device mapping. */
160 return map_driver(label
, major
, endpoint
);
163 /*===========================================================================*
164 * dmap_unmap_by_endpt *
165 *===========================================================================*/
166 void dmap_unmap_by_endpt(endpoint_t proc_e
)
168 /* Lookup driver in dmap table by endpoint and unmap it */
172 for (major
= 0; major
< NR_DEVICES
; major
++) {
173 if (dmap_driver_match(proc_e
, major
)) {
174 /* Found driver; overwrite it with a NULL entry */
175 if ((r
= map_driver(NULL
, major
, NONE
)) != OK
) {
176 printf("VFS: unmapping driver %d for major %d failed:"
177 " %d\n", proc_e
, major
, r
);
183 /*===========================================================================*
185 *===========================================================================*/
186 int map_service(struct rprocpub
*rpub
)
188 /* Map a new service by storing its device driver properties. */
192 if (IS_RPUB_BOOT_USR(rpub
)) return(OK
);
194 /* Process is a service */
195 if (isokendpt(rpub
->endpoint
, &slot
) != OK
) {
196 printf("VFS: can't map service with unknown endpoint %d\n",
201 rfp
->fp_flags
|= FP_SRV_PROC
;
203 /* Not a driver, nothing more to do. */
204 if (rpub
->dev_nr
== NO_DEV
) return(OK
);
207 r
= map_driver(rpub
->label
, rpub
->dev_nr
, rpub
->endpoint
);
208 if(r
!= OK
) return(r
);
213 /*===========================================================================*
215 *===========================================================================*/
218 /* Initialize the device mapping table. */
221 memset(dmap
, 0, sizeof(dmap
));
223 for (i
= 0; i
< NR_DEVICES
; i
++) {
224 dmap
[i
].dmap_driver
= NONE
;
225 dmap
[i
].dmap_servicing
= INVALID_THREAD
;
226 if (mutex_init(&dmap
[i
].dmap_lock
, NULL
) != 0)
227 panic("unable to initialize dmap lock");
230 /* CTTY_MAJOR is a special case, which is handled by VFS itself. */
231 if (map_driver("vfs", CTTY_MAJOR
, CTTY_ENDPT
) != OK
)
232 panic("map_driver(CTTY_MAJOR) failed");
235 /*===========================================================================*
236 * dmap_driver_match *
237 *===========================================================================*/
238 int dmap_driver_match(endpoint_t proc
, devmajor_t major
)
240 if (major
< 0 || major
>= NR_DEVICES
) return(0);
241 if (dmap
[major
].dmap_driver
!= NONE
&& dmap
[major
].dmap_driver
== proc
)
247 /*===========================================================================*
249 *===========================================================================*/
251 get_dmap_by_major(devmajor_t major
)
253 if (major
< 0 || major
>= NR_DEVICES
) return(NULL
);
254 if (dmap
[major
].dmap_driver
== NONE
) return(NULL
);
255 return(&dmap
[major
]);
258 /*===========================================================================*
260 *===========================================================================*/
261 void dmap_endpt_up(endpoint_t proc_e
, int is_blk
)
263 /* A device driver with endpoint proc_e has been restarted. Go tell everyone
264 * that might be blocking on it that this device is 'up'.
268 struct worker_thread
*worker
;
270 if (proc_e
== NONE
) return;
272 for (major
= 0; major
< NR_DEVICES
; major
++) {
273 if ((dp
= get_dmap_by_major(major
)) == NULL
) continue;
274 if (dp
->dmap_driver
== proc_e
) {
276 if (dp
->dmap_recovering
) {
277 printf("VFS: driver recovery failure for"
278 " major %d\n", major
);
279 if (dp
->dmap_servicing
!= INVALID_THREAD
) {
280 worker
= worker_get(dp
->dmap_servicing
);
283 dp
->dmap_recovering
= 0;
286 dp
->dmap_recovering
= 1;
288 dp
->dmap_recovering
= 0;
290 if (dp
->dmap_servicing
!= INVALID_THREAD
) {
291 worker
= worker_get(dp
->dmap_servicing
);
294 invalidate_filp_by_char_major(major
);
300 /*===========================================================================*
302 *===========================================================================*/
303 struct dmap
*get_dmap(endpoint_t proc_e
)
305 /* See if 'proc_e' endpoint belongs to a valid dmap entry. If so, return a
309 for (major
= 0; major
< NR_DEVICES
; major
++)
310 if (dmap_driver_match(proc_e
, major
))
311 return(&dmap
[major
]);