Whamcloud - gitweb
Replace usage of "page" with "bulk" where "page" was actually a "struct
[fs/lustre-release.git] / lustre / ptlrpc / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/lustre_ha.h>
26
27 void ptlrpc_init_client(struct recovd_obd *recovd, 
28                         int (*recover)(struct ptlrpc_client *recover),
29                         int req_portal,
30                         int rep_portal, struct ptlrpc_client *cl)
31 {
32         memset(cl, 0, sizeof(*cl));
33         cl->cli_recovd = recovd;
34         cl->cli_recover = recover;
35         if (recovd)
36                 recovd_cli_manage(recovd, cl);
37         cl->cli_obd = NULL;
38         cl->cli_request_portal = req_portal;
39         cl->cli_reply_portal = rep_portal;
40         INIT_LIST_HEAD(&cl->cli_delayed_head);
41         INIT_LIST_HEAD(&cl->cli_sending_head);
42         INIT_LIST_HEAD(&cl->cli_dying_head);
43         spin_lock_init(&cl->cli_lock);
44         sema_init(&cl->cli_rpc_sem, 32);
45 }
46
47 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
48 {
49         return req->rq_connection->c_remote_uuid;
50 }
51
52 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid)
53 {
54         struct ptlrpc_connection *c;
55         struct lustre_peer peer;
56         int err;
57
58         err = kportal_uuid_to_peer(uuid, &peer);
59         if (err != 0) {
60                 CERROR("cannot find peer %s!\n", uuid);
61                 return NULL;
62         }
63
64         c = ptlrpc_get_connection(&peer);
65         if (c)
66                 c->c_epoch++;
67
68         return c;
69 }
70
71 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn, char *uuid)
72 {
73         struct lustre_peer peer;
74         int err;
75
76         err = kportal_uuid_to_peer(uuid, &peer);
77         if (err != 0) {
78                 CERROR("cannot find peer %s!\n", uuid);
79                 return;
80         }
81         
82         memcpy(&conn->c_peer, &peer, sizeof(peer)); 
83         return;
84 }
85
86 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
87 {
88         struct ptlrpc_bulk_desc *bulk;
89
90         OBD_ALLOC(bulk, sizeof(*bulk));
91         if (bulk != NULL) {
92                 bulk->b_connection = ptlrpc_connection_addref(conn);
93                 init_waitqueue_head(&bulk->b_waitq);
94                 INIT_LIST_HEAD(&bulk->b_page_list);
95         }
96
97         return bulk;
98 }
99
100 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc)
101 {
102         struct ptlrpc_bulk_page *bulk;
103
104         OBD_ALLOC(bulk, sizeof(*bulk));
105         if (bulk != NULL) {
106                 bulk->b_desc = desc;
107                 ptl_set_inv_handle(&bulk->b_md_h);
108                 ptl_set_inv_handle(&bulk->b_me_h);
109                 list_add_tail(&bulk->b_link, &desc->b_page_list);
110                 desc->b_page_count++;
111         }
112         return bulk;
113 }
114
115 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk)
116 {
117         struct list_head *tmp, *next;
118         ENTRY;
119         if (bulk == NULL) {
120                 EXIT;
121                 return;
122         }
123
124         list_for_each_safe(tmp, next, &bulk->b_page_list) {
125                 struct ptlrpc_bulk_page *bulk;
126                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
127                 ptlrpc_free_bulk_page(bulk);
128         }
129
130         ptlrpc_put_connection(bulk->b_connection);
131
132         OBD_FREE(bulk, sizeof(*bulk));
133         EXIT;
134 }
135
136 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
137 {
138         ENTRY;
139         if (bulk == NULL) {
140                 EXIT;
141                 return;
142         }
143
144         list_del(&bulk->b_link);
145         bulk->b_desc->b_page_count--;
146         OBD_FREE(bulk, sizeof(*bulk));
147         EXIT;
148 }
149
150 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
151                                        struct ptlrpc_connection *conn,
152                                        int opcode, int count, int *lengths,
153                                        char **bufs)
154 {
155         struct ptlrpc_request *request;
156         int rc;
157         ENTRY;
158
159         OBD_ALLOC(request, sizeof(*request));
160         if (!request) {
161                 CERROR("request allocation out of memory\n");
162                 RETURN(NULL);
163         }
164
165         rc = lustre_pack_msg(count, lengths, bufs,
166                              &request->rq_reqlen, &request->rq_reqmsg);
167         if (rc) {
168                 CERROR("cannot pack request %d\n", rc);
169                 RETURN(NULL);
170         }
171
172         request->rq_type = PTL_RPC_TYPE_REQUEST;
173         request->rq_connection = ptlrpc_connection_addref(conn);
174
175         request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
176         request->rq_reqmsg->token = conn->c_remote_token;
177         request->rq_reqmsg->opc = HTON__u32(opcode);
178         request->rq_reqmsg->type = HTON__u32(PTL_RPC_MSG_REQUEST);
179         INIT_LIST_HEAD(&request->rq_list);
180
181         /* this will be dec()d once in req_finished, once in free_committed */
182         atomic_set(&request->rq_refcount, 2);
183
184         spin_lock(&conn->c_lock);
185         request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
186         request->rq_xid = conn->c_xid_out;
187         spin_unlock(&conn->c_lock);
188
189         request->rq_client = cl;
190
191         RETURN(request);
192 }
193
194 void ptlrpc_req_finished(struct ptlrpc_request *request)
195 {
196         if (request == NULL)
197                 return;
198
199         if (request->rq_repmsg != NULL) { 
200                 OBD_FREE(request->rq_repmsg, request->rq_replen);
201                 request->rq_repmsg = NULL;
202                 request->rq_reply_md.start = NULL; 
203         }
204
205         if (atomic_dec_and_test(&request->rq_refcount))
206                 ptlrpc_free_req(request);
207 }
208
209 void ptlrpc_free_req(struct ptlrpc_request *request)
210 {
211         if (request == NULL)
212                 return;
213
214         if (request->rq_repmsg != NULL)
215                 OBD_FREE(request->rq_repmsg, request->rq_replen);
216         if (request->rq_reqmsg != NULL)
217                 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
218
219         if (request->rq_client) {
220                 spin_lock(&request->rq_client->cli_lock);
221                 list_del_init(&request->rq_list);
222                 spin_unlock(&request->rq_client->cli_lock);
223         }
224
225         ptlrpc_put_connection(request->rq_connection);
226
227         OBD_FREE(request, sizeof(*request));
228 }
229
230 static int ptlrpc_check_reply(struct ptlrpc_request *req)
231 {
232         int rc = 0;
233
234         if (req->rq_repmsg != NULL) {
235                 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
236                 req->rq_flags |= PTL_RPC_FL_REPLIED;
237                 GOTO(out, rc = 1);
238         }
239
240         if (req->rq_flags & PTL_RPC_FL_RESEND) { 
241                 CERROR("-- RESEND --\n");
242                 GOTO(out, rc = 1);
243         }
244
245         if (req->rq_flags & PTL_RPC_FL_RECOVERY) { 
246                 CERROR("-- RESTART --\n");
247                 GOTO(out, rc = 1);
248         }
249
250
251         if (CURRENT_TIME - req->rq_time >= req->rq_timeout) {
252                 CERROR("-- REQ TIMEOUT --\n");
253                 /* clear the timeout */
254                 req->rq_timeout = 0;
255                 req->rq_connection->c_level = LUSTRE_CONN_RECOVD;
256                 req->rq_flags |= PTL_RPC_FL_TIMEOUT;
257                 if (req->rq_client && req->rq_client->cli_recovd)
258                         recovd_cli_fail(req->rq_client);
259                 if (req->rq_level < LUSTRE_CONN_FULL)
260                         rc = 1;
261                 else
262                         rc = 0;
263                 GOTO(out, rc);
264         }
265
266         if (req->rq_timeout) { 
267                 schedule_timeout(req->rq_timeout * HZ);
268         }
269
270         if (sigismember(&(current->pending.signal), SIGKILL) ||
271             sigismember(&(current->pending.signal), SIGTERM) ||
272             sigismember(&(current->pending.signal), SIGINT)) {
273                 req->rq_flags |= PTL_RPC_FL_INTR;
274                 GOTO(out, rc = 1);
275         }
276
277  out:
278         return rc;
279 }
280
281 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
282 {
283         ENTRY;
284
285         if (err != 0) {
286                 CERROR("err is %d\n", err);
287                 RETURN(err);
288         }
289
290         if (req == NULL) {
291                 CERROR("req == NULL\n");
292                 RETURN(-ENOMEM);
293         }
294
295         if (req->rq_repmsg == NULL) {
296                 CERROR("req->rq_repmsg == NULL\n");
297                 RETURN(-ENOMEM);
298         }
299
300         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
301                 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
302                 RETURN(-EINVAL);
303         }
304
305         if (req->rq_repmsg->status != 0) {
306                 CERROR("req->rq_repmsg->status is %d\n",
307                        req->rq_repmsg->status);
308                 /* XXX: translate this error from net to host */
309                 RETURN(req->rq_repmsg->status);
310         }
311
312         RETURN(0);
313 }
314
315 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
316 {
317         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
318         request->rq_reqmsg = NULL;
319         request->rq_reqlen = 0;
320 }
321
322 /* Abort this request and cleanup any resources associated with it. */
323 static int ptlrpc_abort(struct ptlrpc_request *request)
324 {
325         /* First remove the ME for the reply; in theory, this means
326          * that we can tear down the buffer safely. */
327         PtlMEUnlink(request->rq_reply_me_h);
328         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
329         request->rq_repmsg = NULL;
330         request->rq_replen = 0;
331         return 0;
332 }
333
334 /* caller must lock cli */
335 void ptlrpc_free_committed(struct ptlrpc_client *cli)
336 {
337         struct list_head *tmp, *saved;
338         struct ptlrpc_request *req;
339
340         list_for_each_safe(tmp, saved, &cli->cli_sending_head) {
341                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
342
343                 if ( (req->rq_flags & PTL_RPC_FL_REPLAY) ) { 
344                         CDEBUG(D_INFO, "Retaining request %Ld for replay\n",
345                                req->rq_xid);
346                         continue;
347                 }
348                         
349                 /* not yet committed */ 
350                 if (!req->rq_transno ||
351                     req->rq_transno > cli->cli_last_committed)
352                         break; 
353
354                 CDEBUG(D_INFO, "Marking request %Ld as committed ("
355                        "transno=%Lu, last_committed=%Lu\n", 
356                        req->rq_xid, req->rq_transno, 
357                        cli->cli_last_committed);
358                 if (atomic_dec_and_test(&req->rq_refcount)) {
359                         /* we do this to prevent free_req deadlock */
360                         list_del_init(&req->rq_list); 
361                         req->rq_client = NULL;
362                         ptlrpc_free_req(req);
363                 } else {
364                         list_del_init(&req->rq_list);
365                         list_add(&req->rq_list, &cli->cli_dying_head);
366                 }
367         }
368
369         EXIT;
370         return;
371 }
372
373 void ptlrpc_cleanup_client(struct ptlrpc_client *cli)
374 {
375         struct list_head *tmp, *saved;
376         struct ptlrpc_request *req;
377         ENTRY;
378
379         spin_lock(&cli->cli_lock);
380         list_for_each_safe(tmp, saved, &cli->cli_sending_head) {
381                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
382                 CDEBUG(D_INFO, "Cleaning req %p from sending list.\n", req);
383                 list_del_init(&req->rq_list);
384                 req->rq_client = NULL;
385                 ptlrpc_free_req(req); 
386         }
387         list_for_each_safe(tmp, saved, &cli->cli_dying_head) {
388                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
389                 CERROR("Request %p is on the dying list at cleanup!\n", req);
390                 list_del_init(&req->rq_list);
391                 req->rq_client = NULL;
392                 ptlrpc_free_req(req); 
393         }
394         spin_unlock(&cli->cli_lock);
395
396         EXIT;
397         return;
398 }
399
400 void ptlrpc_continue_req(struct ptlrpc_request *req)
401 {
402         ENTRY;
403         CDEBUG(D_INODE, "continue delayed request %Ld opc %d\n", 
404                req->rq_xid, req->rq_reqmsg->opc); 
405         wake_up_interruptible(&req->rq_wait_for_rep); 
406         EXIT;
407 }
408
409 void ptlrpc_resend_req(struct ptlrpc_request *req)
410 {
411         ENTRY;
412         CDEBUG(D_INODE, "resend request %Ld, opc %d\n", 
413                req->rq_xid, req->rq_reqmsg->opc);
414         req->rq_status = -EAGAIN;
415         req->rq_level = LUSTRE_CONN_RECOVD;
416         req->rq_flags |= PTL_RPC_FL_RESEND;
417         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
418         wake_up_interruptible(&req->rq_wait_for_rep);
419         EXIT;
420 }
421
422 void ptlrpc_restart_req(struct ptlrpc_request *req)
423 {
424         ENTRY;
425         CDEBUG(D_INODE, "restart completed request %Ld, opc %d\n", 
426                req->rq_xid, req->rq_reqmsg->opc);
427         req->rq_status = -ERESTARTSYS;
428         req->rq_flags |= PTL_RPC_FL_RECOVERY;
429         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
430         wake_up_interruptible(&req->rq_wait_for_rep);
431         EXIT;
432 }
433
434 int ptlrpc_queue_wait(struct ptlrpc_request *req)
435 {
436         int rc = 0;
437         struct ptlrpc_client *cli = req->rq_client;
438         ENTRY;
439
440         init_waitqueue_head(&req->rq_wait_for_rep);
441         CDEBUG(D_NET, "subsys: %s req %Ld opc %d level %d, conn level %d\n",
442                cli->cli_name, req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
443                req->rq_connection->c_level);
444
445         /* XXX probably both an import and connection level are needed */
446         if (req->rq_level > req->rq_connection->c_level) { 
447                 CERROR("process %d waiting for recovery\n", current->pid);
448                 spin_lock(&cli->cli_lock);
449                 list_del_init(&req->rq_list);
450                 list_add(&req->rq_list, cli->cli_delayed_head.prev); 
451                 spin_unlock(&cli->cli_lock);
452                 wait_event_interruptible
453                         (req->rq_wait_for_rep, 
454                          req->rq_level <= req->rq_connection->c_level);
455                 spin_lock(&cli->cli_lock);
456                 list_del_init(&req->rq_list);
457                 spin_unlock(&cli->cli_lock);
458                 CERROR("process %d resumed\n", current->pid);
459         }
460  resend:
461         req->rq_time = CURRENT_TIME;
462         req->rq_timeout = 30;
463         rc = ptl_send_rpc(req);
464         if (rc) {
465                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
466                 if ( rc > 0 ) 
467                         rc = -rc;
468                 ptlrpc_cleanup_request_buf(req);
469                 up(&cli->cli_rpc_sem);
470                 RETURN(-rc);
471         }
472
473         spin_lock(&cli->cli_lock);
474         list_del_init(&req->rq_list);
475         list_add(&req->rq_list, cli->cli_sending_head.prev);
476         spin_unlock(&cli->cli_lock);
477
478         CDEBUG(D_OTHER, "-- sleeping\n");
479         wait_event_interruptible(req->rq_wait_for_rep, 
480                                  ptlrpc_check_reply(req));
481         CDEBUG(D_OTHER, "-- done\n");
482
483         if (req->rq_flags & PTL_RPC_FL_RESEND) {
484                 req->rq_flags &= ~PTL_RPC_FL_RESEND;
485                 goto resend;
486         }
487
488         up(&cli->cli_rpc_sem);
489         if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
490                 GOTO(out, rc = -ETIMEDOUT);
491
492         if (req->rq_flags & PTL_RPC_FL_INTR) {
493                 /* Clean up the dangling reply buffers */
494                 ptlrpc_abort(req);
495                 GOTO(out, rc = -EINTR);
496         }
497
498         if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
499                 GOTO(out, rc = req->rq_status);
500
501         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
502         if (rc) {
503                 CERROR("unpack_rep failed: %d\n", rc);
504                 GOTO(out, rc);
505         }
506         CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
507         if (req->rq_repmsg->status == 0)
508                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
509                        req->rq_replen, req->rq_repmsg->status);
510
511         spin_lock(&cli->cli_lock);
512         cli->cli_last_rcvd = req->rq_repmsg->last_rcvd;
513         cli->cli_last_committed = req->rq_repmsg->last_committed;
514         ptlrpc_free_committed(cli); 
515         spin_unlock(&cli->cli_lock);
516
517         EXIT;
518  out:
519         return rc;
520 }
521
522 int ptlrpc_replay_req(struct ptlrpc_request *req)
523 {
524         int rc = 0;
525         struct ptlrpc_client *cli = req->rq_client;
526         ENTRY;
527
528         init_waitqueue_head(&req->rq_wait_for_rep);
529         CERROR("req %Ld opc %d level %d, conn level %d\n", 
530                req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
531                req->rq_connection->c_level);
532
533         req->rq_time = CURRENT_TIME;
534         req->rq_timeout = 3;
535         rc = ptl_send_rpc(req);
536         if (rc) {
537                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
538                 ptlrpc_cleanup_request_buf(req);
539                 up(&cli->cli_rpc_sem);
540                 RETURN(-rc);
541         }
542
543         CDEBUG(D_OTHER, "-- sleeping\n");
544         wait_event_interruptible(req->rq_wait_for_rep, 
545                                  ptlrpc_check_reply(req));
546         CDEBUG(D_OTHER, "-- done\n");
547
548         up(&cli->cli_rpc_sem);
549
550         if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
551                 CERROR("Unknown reason for wakeup\n");
552                 /* XXX Phil - I end up here when I kill obdctl */
553                 ptlrpc_abort(req);
554                 GOTO(out, rc = -EINTR);
555         }
556
557         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
558         if (rc) {
559                 CERROR("unpack_rep failed: %d\n", rc);
560                 GOTO(out, rc);
561         }
562
563         CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
564         if (req->rq_repmsg->status == 0)
565                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
566                        req->rq_replen, req->rq_repmsg->status);
567         else {
568                 CERROR("recovery failed: "); 
569                 CERROR("req %Ld opc %d level %d, conn level %d\n", 
570                        req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
571                        req->rq_connection->c_level);
572                 LBUG();
573         }
574
575  out:
576         RETURN(rc);
577 }