make vfs & filesystems use failable copying
[minix3.git] / lib / libbdev / ipc.c
blobed92ebf7ae7201c0f197a3b978ce9e7127e25f03
1 /* libbdev - IPC and recovery functions */
3 #include <minix/drivers.h>
4 #include <minix/bdev.h>
5 #include <assert.h>
7 #include "const.h"
8 #include "type.h"
9 #include "proto.h"
11 static void bdev_cancel(dev_t dev)
13 /* Recovering the driver for the given device has failed repeatedly. Mark it as
14 * permanently unusable, and clean up any associated calls and resources.
16 bdev_call_t *call, *next;
18 printf("bdev: giving up on major %d\n", major(dev));
20 /* Cancel all pending asynchronous requests. */
21 call = NULL;
23 while ((call = bdev_call_iter_maj(dev, call, &next)) != NULL)
24 bdev_callback_asyn(call, EDEADSRCDST);
26 /* Mark the driver as unusable. */
27 bdev_driver_clear(dev);
30 static int bdev_recover(dev_t dev, int update_endpt)
32 /* The IPC subsystem has signaled an error communicating to the driver
33 * associated with the given device. Try to recover. If 'update_endpt' is set,
34 * we need to find the new endpoint of the driver first. Return TRUE iff
35 * recovery has been successful.
37 bdev_call_t *call, *next;
38 endpoint_t endpt;
39 int r, active, nr_tries;
41 /* Only print output if there is something to recover. Some drivers may be
42 * shut down and later restarted legitimately, and if they were not in use
43 * while that happened, there is no need to flood the console with messages.
45 active = bdev_minor_is_open(dev) || bdev_call_iter_maj(dev, NULL, &next);
47 if (active)
48 printf("bdev: recovering from a driver restart on major %d\n",
49 major(dev));
51 for (nr_tries = 0; nr_tries < RECOVER_TRIES; nr_tries++) {
52 /* First update the endpoint, if necessary. */
53 if (update_endpt)
54 (void) bdev_driver_update(dev);
56 if ((endpt = bdev_driver_get(dev)) == NONE)
57 break;
59 /* If anything goes wrong, update the endpoint again next time. */
60 update_endpt = TRUE;
62 /* Reopen all minor devices on the new driver. */
63 if ((r = bdev_minor_reopen(dev)) != OK) {
64 /* If the driver died again, we may give it another try. */
65 if (r == EDEADSRCDST)
66 continue;
68 /* If another error occurred, we cannot continue using the
69 * driver as is, but we also cannot force it to restart.
71 break;
74 /* Resend all asynchronous requests. */
75 call = NULL;
77 while ((call = bdev_call_iter_maj(dev, call, &next)) != NULL) {
78 /* It is not strictly necessary that we manage to reissue all
79 * asynchronous requests successfully. We can fail them on an
80 * individual basis here, without affecting the overall
81 * recovery. Note that we will never get new IPC failures here.
83 if ((r = bdev_restart_asyn(call)) != OK)
84 bdev_callback_asyn(call, r);
87 /* Recovery seems successful. We can now reissue the current
88 * synchronous request (if any), and continue normal operation.
90 if (active)
91 printf("bdev: recovery successful, new driver at %d\n", endpt);
93 return TRUE;
96 /* Recovery failed repeatedly. Give up on this driver. */
97 bdev_cancel(dev);
99 return FALSE;
102 void bdev_update(dev_t dev, char *label)
104 /* Set the endpoint for a driver. Perform recovery if necessary.
106 endpoint_t endpt, old_endpt;
108 old_endpt = bdev_driver_get(dev);
110 endpt = bdev_driver_set(dev, label);
112 /* If updating the driver causes an endpoint change, we need to perform
113 * recovery, but not update the endpoint yet again.
115 if (old_endpt != NONE && old_endpt != endpt)
116 bdev_recover(dev, FALSE /*update_endpt*/);
119 int bdev_senda(dev_t dev, const message *m_orig, bdev_id_t id)
121 /* Send an asynchronous request for the given device. This function will never
122 * get any new IPC errors sending to the driver. If sending an asynchronous
123 * request fails, we will find out through other ways later.
125 endpoint_t endpt;
126 message m;
127 int r;
129 /* If we have no usable driver endpoint, fail instantly. */
130 if ((endpt = bdev_driver_get(dev)) == NONE)
131 return EDEADSRCDST;
133 m = *m_orig;
134 m.BDEV_ID = id;
136 r = asynsend(endpt, &m);
138 if (r != OK)
139 printf("bdev: asynsend to driver (%d) failed (%d)\n", endpt, r);
141 return r;
144 int bdev_sendrec(dev_t dev, const message *m_orig)
146 /* Send a synchronous request for the given device, and wait for the reply.
147 * Return ERESTART if the caller should try to reissue the request.
149 endpoint_t endpt;
150 message m;
151 int r;
153 /* If we have no usable driver endpoint, fail instantly. */
154 if ((endpt = bdev_driver_get(dev)) == NONE)
155 return EDEADSRCDST;
157 /* Send the request and block until we receive a reply. */
158 m = *m_orig;
159 m.BDEV_ID = NO_ID;
161 r = ipc_sendrec(endpt, &m);
163 /* If communication failed, the driver has died. We assume it will be
164 * restarted soon after, so we attempt recovery. Upon success, we let the
165 * caller reissue the synchronous request.
167 if (r == EDEADSRCDST) {
168 if (!bdev_recover(dev, TRUE /*update_endpt*/))
169 return EDEADSRCDST;
171 return ERESTART;
174 if (r != OK) {
175 printf("bdev: IPC to driver (%d) failed (%d)\n", endpt, r);
176 return r;
179 if (m.m_type != BDEV_REPLY) {
180 printf("bdev: driver (%d) sent weird response (%d)\n",
181 endpt, m.m_type);
182 return EINVAL;
185 /* The protocol contract states that no asynchronous reply can satisfy a
186 * synchronous SENDREC call, so we can never get an asynchronous reply here.
188 if (m.BDEV_ID != NO_ID) {
189 printf("bdev: driver (%d) sent invalid ID (%ld)\n", endpt, m.BDEV_ID);
190 return EINVAL;
193 /* Unless the caller is misusing libbdev, we will only get ERESTART if we
194 * have managed to resend a raw block I/O request to the driver after a
195 * restart, but before VFS has had a chance to reopen the associated device
196 * first. This is highly exceptional, and hard to deal with correctly. We
197 * take the easiest route: sleep for a while so that VFS can reopen the
198 * device, and then resend the request. If the call keeps failing, the caller
199 * will eventually give up.
201 if (m.BDEV_STATUS == ERESTART) {
202 printf("bdev: got ERESTART from driver (%d), sleeping for reopen\n",
203 endpt);
205 micro_delay(1000);
207 return ERESTART;
210 /* Return the result of our request. */
211 return m.BDEV_STATUS;
214 static int bdev_receive(dev_t dev, message *m)
216 /* Receive one valid message.
218 endpoint_t endpt;
219 int r, nr_tries = 0;
221 for (;;) {
222 /* Retrieve and check the driver endpoint on every try, as it will
223 * change with each driver restart.
225 if ((endpt = bdev_driver_get(dev)) == NONE)
226 return EDEADSRCDST;
228 r = sef_receive(endpt, m);
230 if (r == EDEADSRCDST) {
231 /* If we reached the maximum number of retries, give up. */
232 if (++nr_tries == DRIVER_TRIES)
233 break;
235 /* Attempt recovery. If successful, all asynchronous requests
236 * will have been resent, and we can retry receiving a reply.
238 if (!bdev_recover(dev, TRUE /*update_endpt*/))
239 return EDEADSRCDST;
241 continue;
244 if (r != OK) {
245 printf("bdev: IPC to driver (%d) failed (%d)\n", endpt, r);
247 return r;
250 if (m->m_type != BDEV_REPLY) {
251 printf("bdev: driver (%d) sent weird response (%d)\n",
252 endpt, m->m_type);
253 return EINVAL;
256 /* The caller is responsible for checking the ID and status. */
257 return OK;
260 /* All tries failed, even though all recovery attempts succeeded. In this
261 * case, we let the caller recheck whether it wants to keep calling us,
262 * returning ERESTART to indicate we can be called again but did not actually
263 * receive a message.
265 return ERESTART;
268 void bdev_reply_asyn(message *m)
270 /* A reply has come in from a disk driver.
272 bdev_call_t *call;
273 endpoint_t endpt;
274 bdev_id_t id;
275 int r;
277 /* This is a requirement for the caller. */
278 assert(m->m_type == BDEV_REPLY);
280 /* Get the corresponding asynchronous call structure. */
281 id = m->BDEV_ID;
283 if ((call = bdev_call_get(id)) == NULL) {
284 printf("bdev: driver (%d) replied to unknown request (%ld)\n",
285 m->m_source, m->BDEV_ID);
286 return;
289 /* Make sure the reply was sent from the right endpoint. */
290 endpt = bdev_driver_get(call->dev);
292 if (m->m_source != endpt) {
293 /* If the endpoint is NONE, this may be a stray reply. */
294 if (endpt != NONE)
295 printf("bdev: driver (%d) replied to request not sent to it\n",
296 m->m_source);
297 return;
300 /* See the ERESTART comment in bdev_sendrec(). */
301 if (m->BDEV_STATUS == ERESTART) {
302 printf("bdev: got ERESTART from driver (%d), sleeping for reopen\n",
303 endpt);
305 micro_delay(1000);
307 if ((r = bdev_restart_asyn(call)) != OK)
308 bdev_callback_asyn(call, r);
310 return;
313 bdev_callback_asyn(call, m->BDEV_STATUS);
316 int bdev_wait_asyn(bdev_id_t id)
318 /* Wait for an asynchronous request to complete.
320 bdev_call_t *call;
321 dev_t dev;
322 message m;
323 int r;
325 if ((call = bdev_call_get(id)) == NULL)
326 return ENOENT;
328 dev = call->dev;
330 do {
331 if ((r = bdev_receive(dev, &m)) != OK && r != ERESTART)
332 return r;
334 /* Processing the reply will free up the call structure as a side
335 * effect. If we repeatedly get ERESTART, we will repeatedly resend the
336 * asynchronous request, which will then eventually hit the retry limit
337 * and we will break out of the loop.
339 if (r == OK)
340 bdev_reply_asyn(&m);
342 } while (bdev_call_get(id) != NULL);
344 return OK;