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