1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
30 #include <liblustre.h>
33 #include <obd_support.h>
34 #include <obd_class.h>
35 #include <lustre_lib.h>
36 #include <lustre_ha.h>
37 #include <lustre_import.h>
38 #include <lustre_req_layout.h>
40 #include "ptlrpc_internal.h"
42 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
43 struct ptlrpc_client *cl)
45 cl->cli_request_portal = req_portal;
46 cl->cli_reply_portal = rep_portal;
50 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
52 struct ptlrpc_connection *c;
54 lnet_process_id_t peer;
57 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
59 CERROR("cannot find peer %s!\n", uuid->uuid);
63 c = ptlrpc_get_connection(peer, self, uuid);
65 memcpy(c->c_remote_uuid.uuid,
66 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
69 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
74 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,
75 struct obd_uuid *uuid)
78 lnet_process_id_t peer;
81 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
83 CERROR("cannot find peer %s!\n", uuid->uuid);
92 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
94 struct ptlrpc_bulk_desc *desc;
96 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
100 spin_lock_init(&desc->bd_lock);
101 cfs_waitq_init(&desc->bd_waitq);
102 desc->bd_max_iov = npages;
103 desc->bd_iov_count = 0;
104 desc->bd_md_h = LNET_INVALID_HANDLE;
105 desc->bd_portal = portal;
106 desc->bd_type = type;
111 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
112 int npages, int type, int portal)
114 struct obd_import *imp = req->rq_import;
115 struct ptlrpc_bulk_desc *desc;
118 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
119 desc = new_bulk(npages, type, portal);
123 desc->bd_import_generation = req->rq_import_generation;
124 desc->bd_import = class_import_get(imp);
127 desc->bd_cbid.cbid_fn = client_bulk_callback;
128 desc->bd_cbid.cbid_arg = desc;
130 /* This makes req own desc, and free it when she frees herself */
136 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
137 int npages, int type, int portal)
139 struct obd_export *exp = req->rq_export;
140 struct ptlrpc_bulk_desc *desc;
143 LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
145 desc = new_bulk(npages, type, portal);
149 desc->bd_export = class_export_get(exp);
152 desc->bd_cbid.cbid_fn = server_bulk_callback;
153 desc->bd_cbid.cbid_arg = desc;
155 /* NB we don't assign rq_bulk here; server-side requests are
156 * re-used, and the handler frees the bulk desc explicitly. */
161 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
162 cfs_page_t *page, int pageoffset, int len)
164 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
165 LASSERT(page != NULL);
166 LASSERT(pageoffset >= 0);
168 LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
172 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
175 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
179 LASSERT(desc != NULL);
180 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
181 LASSERT(!desc->bd_network_rw); /* network hands off or */
182 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
184 sptlrpc_enc_pool_put_pages(desc);
187 class_export_put(desc->bd_export);
189 class_import_put(desc->bd_import);
191 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
192 bd_iov[desc->bd_max_iov]));
196 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
198 struct list_head *l, *tmp;
199 struct ptlrpc_request *req;
204 list_for_each_safe(l, tmp, &pool->prp_req_list) {
205 req = list_entry(l, struct ptlrpc_request, rq_list);
206 list_del(&req->rq_list);
207 LASSERT(req->rq_reqbuf);
208 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
209 OBD_FREE(req->rq_reqbuf, pool->prp_rq_size);
210 OBD_FREE(req, sizeof(*req));
212 OBD_FREE(pool, sizeof(*pool));
215 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
220 while (size < pool->prp_rq_size + SPTLRPC_MAX_PAYLOAD)
223 LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
224 "Trying to change pool size with nonempty pool "
225 "from %d to %d bytes\n", pool->prp_rq_size, size);
227 spin_lock(&pool->prp_lock);
228 pool->prp_rq_size = size;
229 for (i = 0; i < num_rq; i++) {
230 struct ptlrpc_request *req;
231 struct lustre_msg *msg;
233 spin_unlock(&pool->prp_lock);
234 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
237 OBD_ALLOC_GFP(msg, size, CFS_ALLOC_STD);
239 OBD_FREE(req, sizeof(struct ptlrpc_request));
242 req->rq_reqbuf = msg;
243 req->rq_reqbuf_len = size;
245 spin_lock(&pool->prp_lock);
246 list_add_tail(&req->rq_list, &pool->prp_req_list);
248 spin_unlock(&pool->prp_lock);
252 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
253 void (*populate_pool)(struct ptlrpc_request_pool *, int))
255 struct ptlrpc_request_pool *pool;
257 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
261 /* Request next power of two for the allocation, because internally
262 kernel would do exactly this */
264 spin_lock_init(&pool->prp_lock);
265 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
266 pool->prp_rq_size = msgsize;
267 pool->prp_populate = populate_pool;
269 populate_pool(pool, num_rq);
271 if (list_empty(&pool->prp_req_list)) {
272 /* have not allocated a single request for the pool */
273 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
279 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
281 struct ptlrpc_request *request;
282 struct lustre_msg *reqbuf;
287 spin_lock(&pool->prp_lock);
289 /* See if we have anything in a pool, and bail out if nothing,
290 * in writeout path, where this matters, this is safe to do, because
291 * nothing is lost in this case, and when some in-flight requests
292 * complete, this code will be called again. */
293 if (unlikely(list_empty(&pool->prp_req_list))) {
294 spin_unlock(&pool->prp_lock);
298 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
300 list_del(&request->rq_list);
301 spin_unlock(&pool->prp_lock);
303 LASSERT(request->rq_reqbuf);
304 LASSERT(request->rq_pool);
306 reqbuf = request->rq_reqbuf;
307 memset(request, 0, sizeof(*request));
308 request->rq_reqbuf = reqbuf;
309 request->rq_reqbuf_len = pool->prp_rq_size;
310 request->rq_pool = pool;
314 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
316 struct ptlrpc_request_pool *pool = request->rq_pool;
318 spin_lock(&pool->prp_lock);
319 LASSERT(list_empty(&request->rq_list));
320 list_add_tail(&request->rq_list, &pool->prp_req_list);
321 spin_unlock(&pool->prp_lock);
324 static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
325 __u32 version, int opcode,
326 int count, int *lengths, char **bufs,
327 struct ptlrpc_cli_ctx *ctx)
329 struct obd_import *imp = request->rq_import;
334 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
336 rc = sptlrpc_req_get_ctx(request);
341 sptlrpc_req_set_flavor(request, opcode);
343 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
346 LASSERT(!request->rq_pool);
350 lustre_msg_add_version(request->rq_reqmsg, version);
352 if (imp->imp_server_timeout)
353 request->rq_timeout = obd_timeout / 2;
355 request->rq_timeout = obd_timeout;
356 request->rq_send_state = LUSTRE_IMP_FULL;
357 request->rq_type = PTL_RPC_MSG_REQUEST;
358 request->rq_export = NULL;
360 request->rq_req_cbid.cbid_fn = request_out_callback;
361 request->rq_req_cbid.cbid_arg = request;
363 request->rq_reply_cbid.cbid_fn = reply_in_callback;
364 request->rq_reply_cbid.cbid_arg = request;
366 request->rq_phase = RQ_PHASE_NEW;
368 /* XXX FIXME bug 249 */
369 request->rq_request_portal = imp->imp_client->cli_request_portal;
370 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
372 spin_lock_init(&request->rq_lock);
373 CFS_INIT_LIST_HEAD(&request->rq_list);
374 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
375 CFS_INIT_LIST_HEAD(&request->rq_mod_list);
376 CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
377 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
378 CFS_INIT_LIST_HEAD(&request->rq_history_list);
379 cfs_waitq_init(&request->rq_reply_waitq);
380 request->rq_xid = ptlrpc_next_xid();
381 atomic_set(&request->rq_refcount, 1);
383 lustre_msg_set_opc(request->rq_reqmsg, opcode);
384 lustre_msg_set_flags(request->rq_reqmsg, 0);
388 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
390 class_import_put(imp);
394 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
395 __u32 version, int opcode, char **bufs,
396 struct ptlrpc_cli_ctx *ctx)
400 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
401 return __ptlrpc_request_bufs_pack(request, version, opcode, count,
402 request->rq_pill.rc_area[RCL_CLIENT],
405 EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
407 int ptlrpc_request_pack(struct ptlrpc_request *request,
408 __u32 version, int opcode)
410 return ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
414 struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
415 struct ptlrpc_request_pool *pool)
417 struct ptlrpc_request *request = NULL;
420 request = ptlrpc_prep_req_from_pool(pool);
423 OBD_ALLOC_PTR(request);
426 LASSERT((unsigned long)imp > 0x1000);
427 LASSERT(imp != LP_POISON);
428 LASSERT((unsigned long)imp->imp_client > 0x1000);
429 LASSERT(imp->imp_client != LP_POISON);
431 request->rq_import = class_import_get(imp);
433 CERROR("request allocation out of memory\n");
439 static struct ptlrpc_request *
440 ptlrpc_request_alloc_internal(struct obd_import *imp,
441 struct ptlrpc_request_pool * pool,
442 const struct req_format *format)
444 struct ptlrpc_request *request;
446 request = __ptlrpc_request_alloc(imp, pool);
450 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
451 req_capsule_set(&request->rq_pill, format);
455 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
456 const struct req_format *format)
458 return ptlrpc_request_alloc_internal(imp, NULL, format);
461 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
462 struct ptlrpc_request_pool * pool,
463 const struct req_format *format)
465 return ptlrpc_request_alloc_internal(imp, pool, format);
468 void ptlrpc_request_free(struct ptlrpc_request *request)
470 if (request->rq_pool)
471 __ptlrpc_free_req_to_pool(request);
473 OBD_FREE_PTR(request);
476 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
477 const struct req_format *format,
478 __u32 version, int opcode)
480 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
484 rc = ptlrpc_request_pack(req, version, opcode);
486 ptlrpc_request_free(req);
493 struct ptlrpc_request *
494 ptlrpc_prep_req_pool(struct obd_import *imp,
495 __u32 version, int opcode,
496 int count, int *lengths, char **bufs,
497 struct ptlrpc_request_pool *pool)
499 struct ptlrpc_request *request;
502 request = __ptlrpc_request_alloc(imp, pool);
506 rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
507 lengths, bufs, NULL);
509 ptlrpc_request_free(request);
515 struct ptlrpc_request *
516 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
517 int *lengths, char **bufs)
519 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
523 struct ptlrpc_request_set *ptlrpc_prep_set(void)
525 struct ptlrpc_request_set *set;
528 OBD_ALLOC(set, sizeof *set);
531 CFS_INIT_LIST_HEAD(&set->set_requests);
532 cfs_waitq_init(&set->set_waitq);
533 set->set_remaining = 0;
534 spin_lock_init(&set->set_new_req_lock);
535 CFS_INIT_LIST_HEAD(&set->set_new_requests);
536 CFS_INIT_LIST_HEAD(&set->set_cblist);
541 /* Finish with this set; opposite of prep_set. */
542 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
544 struct list_head *tmp;
545 struct list_head *next;
550 /* Requests on the set should either all be completed, or all be new */
551 expected_phase = (set->set_remaining == 0) ?
552 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
553 list_for_each (tmp, &set->set_requests) {
554 struct ptlrpc_request *req =
555 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
557 LASSERT(req->rq_phase == expected_phase);
561 LASSERT(set->set_remaining == 0 || set->set_remaining == n);
563 list_for_each_safe(tmp, next, &set->set_requests) {
564 struct ptlrpc_request *req =
565 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
566 list_del_init(&req->rq_set_chain);
568 LASSERT(req->rq_phase == expected_phase);
570 if (req->rq_phase == RQ_PHASE_NEW) {
572 if (req->rq_interpret_reply != NULL) {
573 int (*interpreter)(struct ptlrpc_request *,
575 req->rq_interpret_reply;
577 /* higher level (i.e. LOV) failed;
578 * let the sub reqs clean up */
579 req->rq_status = -EBADR;
580 interpreter(req, &req->rq_async_args,
583 set->set_remaining--;
587 ptlrpc_req_finished (req);
590 LASSERT(set->set_remaining == 0);
592 OBD_FREE(set, sizeof(*set));
596 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
597 set_interpreter_func fn, void *data)
599 struct ptlrpc_set_cbdata *cbdata;
601 OBD_ALLOC_PTR(cbdata);
605 cbdata->psc_interpret = fn;
606 cbdata->psc_data = data;
607 list_add_tail(&cbdata->psc_item, &set->set_cblist);
612 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
613 struct ptlrpc_request *req)
615 /* The set takes over the caller's request reference */
616 list_add_tail(&req->rq_set_chain, &set->set_requests);
618 set->set_remaining++;
620 atomic_inc(&req->rq_import->imp_inflight);
623 /* lock so many callers can add things, the context that owns the set
624 * is supposed to notice these and move them into the set proper. */
625 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
626 struct ptlrpc_request *req)
628 spin_lock(&set->set_new_req_lock);
629 /* The set takes over the caller's request reference */
630 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
632 spin_unlock(&set->set_new_req_lock);
636 * Based on the current state of the import, determine if the request
637 * can be sent, is an error, or should be delayed.
639 * Returns true if this request should be delayed. If false, and
640 * *status is set, then the request can not be sent and *status is the
641 * error code. If false and status is 0, then request can be sent.
643 * The imp->imp_lock must be held.
645 static int ptlrpc_import_delay_req(struct obd_import *imp,
646 struct ptlrpc_request *req, int *status)
651 LASSERT (status != NULL);
654 if (req->rq_ctx_init || req->rq_ctx_fini) {
655 /* always allow ctx init/fini rpc go through */
656 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
657 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
660 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
661 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
663 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
664 imp->imp_state == LUSTRE_IMP_CONNECTING) {
665 /* allow CONNECT even if import is invalid */ ;
666 if (atomic_read(&imp->imp_inval_count) != 0) {
667 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
671 } else if ((imp->imp_invalid && (!imp->imp_recon_bk)) ||
672 imp->imp_obd->obd_no_recov) {
673 /* If the import has been invalidated (such as by an OST
674 * failure), and if the import(MGC) tried all of its connection
675 * list (Bug 13464), the request must fail with -ESHUTDOWN.
676 * This indicates the requests should be discarded; an -EIO
677 * may result in a resend of the request. */
678 if (!imp->imp_deactive)
679 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
680 *status = -ESHUTDOWN; /* bz 12940 */
681 } else if (req->rq_import_generation != imp->imp_generation) {
682 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
684 } else if (req->rq_send_state != imp->imp_state) {
685 /* invalidate in progress - any requests should be drop */
686 if (atomic_read(&imp->imp_inval_count) != 0) {
687 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
689 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
690 *status = -EWOULDBLOCK;
699 static int ptlrpc_check_reply(struct ptlrpc_request *req)
704 /* serialise with network callback */
705 spin_lock(&req->rq_lock);
710 if (req->rq_net_err && !req->rq_timedout) {
711 spin_unlock(&req->rq_lock);
712 rc = ptlrpc_expire_one_request(req);
713 spin_lock(&req->rq_lock);
727 spin_unlock(&req->rq_lock);
728 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
732 static int ptlrpc_check_status(struct ptlrpc_request *req)
737 err = lustre_msg_get_status(req->rq_repmsg);
738 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
739 struct obd_import *imp = req->rq_import;
740 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
741 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
742 " with %s. The %s operation failed with %d\n",
743 libcfs_nid2str(imp->imp_connection->c_peer.nid),
744 ll_opcode2str(opc), err);
745 RETURN(err < 0 ? err : -EINVAL);
749 DEBUG_REQ(D_INFO, req, "status is %d", err);
750 } else if (err > 0) {
751 /* XXX: translate this error from net to host */
752 DEBUG_REQ(D_INFO, req, "status is %d", err);
758 static int after_reply(struct ptlrpc_request *req)
760 struct obd_import *imp = req->rq_import;
761 struct obd_device *obd = req->rq_import->imp_obd;
763 struct timeval work_start;
767 LASSERT(!req->rq_receiving_reply);
769 LASSERT(req->rq_nob_received <= req->rq_repbuf_len);
771 /* NB Until this point, the whole of the incoming message,
772 * including buflens, status etc is in the sender's byte order. */
774 /* Clear reply swab mask; this is a new reply in sender's byte order */
775 req->rq_rep_swab_mask = 0;
777 rc = sptlrpc_cli_unwrap_reply(req);
779 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
783 /* security layer unwrap might ask resend this request */
787 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
789 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
793 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
795 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
799 do_gettimeofday(&work_start);
800 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
801 if (obd->obd_svc_stats != NULL)
802 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
805 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
806 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
807 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
808 lustre_msg_get_type(req->rq_repmsg));
812 rc = ptlrpc_check_status(req);
813 imp->imp_connect_error = rc;
816 /* Either we've been evicted, or the server has failed for
817 * some reason. Try to reconnect, and if that fails, punt to
819 if (ll_rpc_recoverable_error(rc)) {
820 if (req->rq_send_state != LUSTRE_IMP_FULL ||
821 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
824 ptlrpc_request_handle_notconn(req);
828 /* Let's look if server send slv. Do it only for RPC with
830 if (imp->imp_obd->obd_namespace) {
831 /* Disconnect rpc is sent when namespace is already
832 * destroyed. Let's check this and will not try update
834 ldlm_cli_update_pool(req);
838 /* Store transno in reqmsg for replay. */
839 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
840 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
842 if (req->rq_import->imp_replayable) {
843 spin_lock(&imp->imp_lock);
844 /* no point in adding already-committed requests to the replay
845 * list, we will just remove them immediately. b=9829 */
846 if (req->rq_transno != 0 &&
848 lustre_msg_get_last_committed(req->rq_repmsg) ||
850 ptlrpc_retain_replayable_request(req, imp);
851 else if (req->rq_commit_cb != NULL) {
852 spin_unlock(&imp->imp_lock);
853 req->rq_commit_cb(req);
854 spin_lock(&imp->imp_lock);
857 /* Replay-enabled imports return commit-status information. */
858 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
859 imp->imp_peer_committed_transno =
860 lustre_msg_get_last_committed(req->rq_repmsg);
862 ptlrpc_free_committed(imp);
863 spin_unlock(&imp->imp_lock);
869 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
871 struct obd_import *imp;
875 LASSERT(req->rq_phase == RQ_PHASE_NEW);
876 if (req->rq_sent && (req->rq_sent > CURRENT_SECONDS))
879 req->rq_phase = RQ_PHASE_RPC;
881 imp = req->rq_import;
882 spin_lock(&imp->imp_lock);
884 req->rq_import_generation = imp->imp_generation;
886 if (ptlrpc_import_delay_req(imp, req, &rc)) {
887 spin_lock (&req->rq_lock);
889 spin_unlock (&req->rq_lock);
891 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
893 lustre_msg_get_status(req->rq_reqmsg) ,
894 ptlrpc_import_state_name(req->rq_send_state),
895 ptlrpc_import_state_name(imp->imp_state));
896 LASSERT(list_empty (&req->rq_list));
898 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
899 spin_unlock(&imp->imp_lock);
904 spin_unlock(&imp->imp_lock);
906 req->rq_phase = RQ_PHASE_INTERPRET;
910 /* XXX this is the same as ptlrpc_queue_wait */
911 LASSERT(list_empty(&req->rq_list));
912 list_add_tail(&req->rq_list, &imp->imp_sending_list);
913 spin_unlock(&imp->imp_lock);
915 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
917 rc = sptlrpc_req_refresh_ctx(req, -1);
923 /* here begins timeout counting */
924 req->rq_sent = CURRENT_SECONDS;
925 req->rq_wait_ctx = 1;
930 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
931 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
932 imp->imp_obd->obd_uuid.uuid,
933 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
934 libcfs_nid2str(imp->imp_connection->c_peer.nid),
935 lustre_msg_get_opc(req->rq_reqmsg));
937 rc = ptl_send_rpc(req, 0);
939 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
946 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
947 int ptlrpc_check_set(struct ptlrpc_request_set *set)
949 struct list_head *tmp;
950 int force_timer_recalc = 0;
953 if (set->set_remaining == 0)
956 list_for_each(tmp, &set->set_requests) {
957 struct ptlrpc_request *req =
958 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
959 struct obd_import *imp = req->rq_import;
962 if (req->rq_phase == RQ_PHASE_NEW &&
963 ptlrpc_send_new_req(req)) {
964 force_timer_recalc = 1;
966 /* delayed send - skip */
967 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
970 if (!(req->rq_phase == RQ_PHASE_RPC ||
971 req->rq_phase == RQ_PHASE_BULK ||
972 req->rq_phase == RQ_PHASE_INTERPRET ||
973 req->rq_phase == RQ_PHASE_COMPLETE)) {
974 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
978 if (req->rq_phase == RQ_PHASE_COMPLETE)
981 if (req->rq_phase == RQ_PHASE_INTERPRET)
982 GOTO(interpret, req->rq_status);
984 if (req->rq_net_err && !req->rq_timedout)
985 ptlrpc_expire_one_request(req);
988 ptlrpc_unregister_reply(req);
990 if (req->rq_status == 0)
991 req->rq_status = -EIO;
992 req->rq_phase = RQ_PHASE_INTERPRET;
994 spin_lock(&imp->imp_lock);
995 list_del_init(&req->rq_list);
996 spin_unlock(&imp->imp_lock);
998 GOTO(interpret, req->rq_status);
1001 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
1002 * will only be set after rq_timedout, but the oig waiting
1003 * path sets rq_intr irrespective of whether ptlrpcd has
1004 * seen a timeout. our policy is to only interpret
1005 * interrupted rpcs after they have timed out */
1006 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1007 req->rq_wait_ctx)) {
1008 /* NB could be on delayed list */
1009 ptlrpc_unregister_reply(req);
1010 req->rq_status = -EINTR;
1011 req->rq_phase = RQ_PHASE_INTERPRET;
1013 spin_lock(&imp->imp_lock);
1014 list_del_init(&req->rq_list);
1015 spin_unlock(&imp->imp_lock);
1017 GOTO(interpret, req->rq_status);
1020 if (req->rq_phase == RQ_PHASE_RPC) {
1021 if (req->rq_timedout || req->rq_resend ||
1022 req->rq_waiting || req->rq_wait_ctx) {
1025 /* rq_wait_ctx is only touched in ptlrpcd,
1026 * no lock needed here.
1028 if (req->rq_wait_ctx)
1031 ptlrpc_unregister_reply(req);
1033 spin_lock(&imp->imp_lock);
1035 if (ptlrpc_import_delay_req(imp, req, &status)){
1036 spin_unlock(&imp->imp_lock);
1040 list_del_init(&req->rq_list);
1042 req->rq_status = status;
1043 req->rq_phase = RQ_PHASE_INTERPRET;
1044 spin_unlock(&imp->imp_lock);
1045 GOTO(interpret, req->rq_status);
1047 if (req->rq_no_resend && !req->rq_wait_ctx) {
1048 req->rq_status = -ENOTCONN;
1049 req->rq_phase = RQ_PHASE_INTERPRET;
1050 spin_unlock(&imp->imp_lock);
1051 GOTO(interpret, req->rq_status);
1053 list_add_tail(&req->rq_list,
1054 &imp->imp_sending_list);
1056 spin_unlock(&imp->imp_lock);
1058 req->rq_waiting = 0;
1059 if (req->rq_resend) {
1060 lustre_msg_add_flags(req->rq_reqmsg,
1063 __u64 old_xid = req->rq_xid;
1065 ptlrpc_unregister_bulk (req);
1067 /* ensure previous bulk fails */
1068 req->rq_xid = ptlrpc_next_xid();
1069 CDEBUG(D_HA, "resend bulk "
1072 old_xid, req->rq_xid);
1076 status = sptlrpc_req_refresh_ctx(req, -1);
1079 req->rq_status = status;
1080 force_timer_recalc = 1;
1082 if (!req->rq_wait_ctx) {
1083 /* begins timeout counting */
1084 req->rq_sent = CURRENT_SECONDS;
1085 req->rq_wait_ctx = 1;
1090 req->rq_wait_ctx = 0;
1093 rc = ptl_send_rpc(req, 0);
1095 DEBUG_REQ(D_HA, req, "send failed (%d)",
1097 force_timer_recalc = 1;
1098 req->rq_net_err = 1;
1100 /* need to reset the timeout */
1101 force_timer_recalc = 1;
1104 /* Still waiting for a reply? */
1105 if (ptlrpc_client_receiving_reply(req))
1108 /* Did we actually receive a reply? */
1109 if (!ptlrpc_client_replied(req))
1112 spin_lock(&imp->imp_lock);
1113 list_del_init(&req->rq_list);
1114 spin_unlock(&imp->imp_lock);
1116 req->rq_status = after_reply(req);
1117 if (req->rq_resend) {
1118 /* Add this req to the delayed list so
1119 it can be errored if the import is
1120 evicted after recovery. */
1121 spin_lock(&imp->imp_lock);
1122 list_add_tail(&req->rq_list,
1123 &imp->imp_delayed_list);
1124 spin_unlock(&imp->imp_lock);
1128 /* If there is no bulk associated with this request,
1129 * then we're done and should let the interpreter
1130 * process the reply. Similarly if the RPC returned
1131 * an error, and therefore the bulk will never arrive.
1133 if (req->rq_bulk == NULL || req->rq_status != 0) {
1134 req->rq_phase = RQ_PHASE_INTERPRET;
1135 GOTO(interpret, req->rq_status);
1138 req->rq_phase = RQ_PHASE_BULK;
1141 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1142 if (ptlrpc_bulk_active(req->rq_bulk))
1145 if (!req->rq_bulk->bd_success) {
1146 /* The RPC reply arrived OK, but the bulk screwed
1147 * up! Dead wierd since the server told us the RPC
1148 * was good after getting the REPLY for her GET or
1149 * the ACK for her PUT. */
1150 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1154 req->rq_phase = RQ_PHASE_INTERPRET;
1157 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1158 LASSERT(!req->rq_receiving_reply);
1160 ptlrpc_unregister_reply(req);
1161 if (req->rq_bulk != NULL)
1162 ptlrpc_unregister_bulk (req);
1164 if (req->rq_interpret_reply != NULL) {
1165 int (*interpreter)(struct ptlrpc_request *,void *,int) =
1166 req->rq_interpret_reply;
1167 req->rq_status = interpreter(req, &req->rq_async_args,
1170 req->rq_phase = RQ_PHASE_COMPLETE;
1172 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1173 "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1174 imp->imp_obd->obd_uuid.uuid,
1175 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1176 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1177 lustre_msg_get_opc(req->rq_reqmsg));
1179 atomic_dec(&imp->imp_inflight);
1180 set->set_remaining--;
1181 cfs_waitq_signal(&imp->imp_recovery_waitq);
1184 /* If we hit an error, we want to recover promptly. */
1185 RETURN(set->set_remaining == 0 || force_timer_recalc);
1188 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
1190 struct obd_import *imp = req->rq_import;
1194 DEBUG_REQ(D_ERROR|D_NETERROR, req, "%s (sent at %lu, %lus ago)",
1195 req->rq_net_err ? "network error" : "timeout",
1196 (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
1198 if (imp != NULL && obd_debug_peer_on_timeout)
1199 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1201 spin_lock(&req->rq_lock);
1202 req->rq_timedout = 1;
1203 req->rq_wait_ctx = 0;
1204 spin_unlock(&req->rq_lock);
1206 ptlrpc_unregister_reply (req);
1208 if (obd_dump_on_timeout)
1209 libcfs_debug_dumplog();
1211 if (req->rq_bulk != NULL)
1212 ptlrpc_unregister_bulk (req);
1215 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1219 /* The DLM server doesn't want recovery run on its imports. */
1220 if (imp->imp_dlm_fake)
1223 /* If this request is for recovery or other primordial tasks,
1224 * then error it out here. */
1225 if (req->rq_ctx_init || req->rq_ctx_fini ||
1226 req->rq_send_state != LUSTRE_IMP_FULL ||
1227 imp->imp_obd->obd_no_recov) {
1228 spin_lock(&req->rq_lock);
1229 req->rq_status = -ETIMEDOUT;
1231 spin_unlock(&req->rq_lock);
1235 /* if request can't be resend we can't wait answer after timeout */
1236 if (req->rq_no_resend) {
1237 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1241 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1246 int ptlrpc_expired_set(void *data)
1248 struct ptlrpc_request_set *set = data;
1249 struct list_head *tmp;
1250 time_t now = CURRENT_SECONDS;
1253 LASSERT(set != NULL);
1255 /* A timeout expired; see which reqs it applies to... */
1256 list_for_each (tmp, &set->set_requests) {
1257 struct ptlrpc_request *req =
1258 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1260 /* request in-flight? */
1261 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1263 (req->rq_phase == RQ_PHASE_BULK)))
1266 if (req->rq_timedout || /* already dealt with */
1267 req->rq_sent + req->rq_timeout > now) /* not expired */
1270 /* deal with this guy */
1271 ptlrpc_expire_one_request (req);
1274 /* When waiting for a whole set, we always to break out of the
1275 * sleep so we can recalculate the timeout, or enable interrupts
1276 * iff everyone's timed out.
1281 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1283 spin_lock(&req->rq_lock);
1285 spin_unlock(&req->rq_lock);
1288 void ptlrpc_interrupted_set(void *data)
1290 struct ptlrpc_request_set *set = data;
1291 struct list_head *tmp;
1293 LASSERT(set != NULL);
1294 CERROR("INTERRUPTED SET %p\n", set);
1296 list_for_each(tmp, &set->set_requests) {
1297 struct ptlrpc_request *req =
1298 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1300 if (req->rq_phase != RQ_PHASE_RPC)
1303 ptlrpc_mark_interrupted(req);
1307 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1309 struct list_head *tmp;
1310 time_t now = CURRENT_SECONDS;
1313 struct ptlrpc_request *req;
1316 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1318 list_for_each(tmp, &set->set_requests) {
1319 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1321 /* request in-flight? */
1322 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1323 (req->rq_phase == RQ_PHASE_BULK) ||
1324 (req->rq_phase == RQ_PHASE_NEW)))
1327 if (req->rq_timedout) /* already timed out */
1330 if (req->rq_phase == RQ_PHASE_NEW)
1331 deadline = req->rq_sent;
1333 deadline = req->rq_sent + req->rq_timeout;
1335 if (deadline <= now) /* actually expired already */
1336 timeout = 1; /* ASAP */
1337 else if (timeout == 0 || timeout > deadline - now)
1338 timeout = deadline - now;
1343 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1345 struct list_head *tmp;
1346 struct ptlrpc_request *req;
1347 struct l_wait_info lwi;
1351 if (list_empty(&set->set_requests))
1354 list_for_each(tmp, &set->set_requests) {
1355 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1356 if (req->rq_phase == RQ_PHASE_NEW)
1357 (void)ptlrpc_send_new_req(req);
1361 timeout = ptlrpc_set_next_timeout(set);
1363 /* wait until all complete, interrupted, or an in-flight
1365 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
1367 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1369 ptlrpc_interrupted_set, set);
1370 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1372 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1374 /* -EINTR => all requests have been flagged rq_intr so next
1376 * -ETIMEOUTD => someone timed out. When all reqs have
1377 * timed out, signals are enabled allowing completion with
1379 * I don't really care if we go once more round the loop in
1380 * the error cases -eeb. */
1381 } while (rc != 0 || set->set_remaining != 0);
1383 LASSERT(set->set_remaining == 0);
1386 list_for_each(tmp, &set->set_requests) {
1387 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1389 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1390 if (req->rq_status != 0)
1391 rc = req->rq_status;
1394 if (set->set_interpret != NULL) {
1395 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1397 rc = interpreter (set, set->set_arg, rc);
1399 struct ptlrpc_set_cbdata *cbdata, *n;
1402 list_for_each_entry_safe(cbdata, n,
1403 &set->set_cblist, psc_item) {
1404 list_del_init(&cbdata->psc_item);
1405 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
1408 OBD_FREE_PTR(cbdata);
1415 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1418 if (request == NULL) {
1423 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1424 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1425 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1426 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1427 LASSERT(request->rq_cli_ctx);
1429 req_capsule_fini(&request->rq_pill);
1431 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1432 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1433 if (request->rq_import != NULL) {
1435 spin_lock(&request->rq_import->imp_lock);
1436 list_del_init(&request->rq_mod_list);
1437 list_del_init(&request->rq_replay_list);
1439 spin_unlock(&request->rq_import->imp_lock);
1441 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1443 if (atomic_read(&request->rq_refcount) != 0) {
1444 DEBUG_REQ(D_ERROR, request,
1445 "freeing request with nonzero refcount");
1449 if (request->rq_repbuf != NULL)
1450 sptlrpc_cli_free_repbuf(request);
1451 if (request->rq_export != NULL) {
1452 class_export_put(request->rq_export);
1453 request->rq_export = NULL;
1455 if (request->rq_import != NULL) {
1456 class_import_put(request->rq_import);
1457 request->rq_import = NULL;
1459 if (request->rq_bulk != NULL)
1460 ptlrpc_free_bulk(request->rq_bulk);
1462 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
1463 sptlrpc_cli_free_reqbuf(request);
1465 sptlrpc_req_put_ctx(request, !locked);
1467 if (request->rq_pool)
1468 __ptlrpc_free_req_to_pool(request);
1470 OBD_FREE(request, sizeof(*request));
1474 void ptlrpc_free_req(struct ptlrpc_request *request)
1476 __ptlrpc_free_req(request, 0);
1479 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1480 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1482 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1483 (void)__ptlrpc_req_finished(request, 1);
1486 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1489 if (request == NULL)
1492 if (request == LP_POISON ||
1493 request->rq_reqmsg == LP_POISON) {
1494 CERROR("dereferencing freed request (bug 575)\n");
1499 DEBUG_REQ(D_INFO, request, "refcount now %u",
1500 atomic_read(&request->rq_refcount) - 1);
1502 if (atomic_dec_and_test(&request->rq_refcount)) {
1503 __ptlrpc_free_req(request, locked);
1510 void ptlrpc_req_finished(struct ptlrpc_request *request)
1512 __ptlrpc_req_finished(request, 0);
1515 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1517 return request->rq_xid;
1519 EXPORT_SYMBOL(ptlrpc_req_xid);
1521 /* Disengage the client's reply buffer from the network
1522 * NB does _NOT_ unregister any client-side bulk.
1523 * IDEMPOTENT, but _not_ safe against concurrent callers.
1524 * The request owner (i.e. the thread doing the I/O) must call...
1526 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1530 struct l_wait_info lwi;
1532 LASSERT(!in_interrupt ()); /* might sleep */
1534 if (!ptlrpc_client_receiving_reply(request))
1537 LNetMDUnlink (request->rq_reply_md_h);
1539 /* We have to l_wait_event() whatever the result, to give liblustre
1540 * a chance to run reply_in_callback() */
1542 if (request->rq_set != NULL)
1543 wq = &request->rq_set->set_waitq;
1545 wq = &request->rq_reply_waitq;
1548 /* Network access will complete in finite time but the HUGE
1549 * timeout lets us CWARN for visibility of sluggish NALs */
1550 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1551 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1555 LASSERT (rc == -ETIMEDOUT);
1556 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1560 /* caller must hold imp->imp_lock */
1561 void ptlrpc_free_committed(struct obd_import *imp)
1563 struct list_head *tmp, *saved;
1564 struct ptlrpc_request *req;
1565 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1568 LASSERT(imp != NULL);
1570 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1573 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1574 imp->imp_generation == imp->imp_last_generation_checked) {
1575 CDEBUG(D_RPCTRACE, "%s: skip recheck: last_committed "LPU64"\n",
1576 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1580 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
1581 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1582 imp->imp_generation);
1583 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1584 imp->imp_last_generation_checked = imp->imp_generation;
1586 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1587 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1589 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1590 LASSERT(req != last_req);
1593 if (req->rq_import_generation < imp->imp_generation) {
1594 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
1598 if (req->rq_replay) {
1599 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
1603 /* not yet committed */
1604 if (req->rq_transno > imp->imp_peer_committed_transno) {
1605 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
1609 DEBUG_REQ(D_RPCTRACE, req, "commit (last_committed "LPU64")",
1610 imp->imp_peer_committed_transno);
1612 spin_lock(&req->rq_lock);
1614 spin_unlock(&req->rq_lock);
1615 if (req->rq_commit_cb != NULL)
1616 req->rq_commit_cb(req);
1617 list_del_init(&req->rq_replay_list);
1618 __ptlrpc_req_finished(req, 1);
1625 void ptlrpc_cleanup_client(struct obd_import *imp)
1632 void ptlrpc_resend_req(struct ptlrpc_request *req)
1634 DEBUG_REQ(D_HA, req, "going to resend");
1635 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1636 req->rq_status = -EAGAIN;
1638 spin_lock(&req->rq_lock);
1640 req->rq_net_err = 0;
1641 req->rq_timedout = 0;
1643 __u64 old_xid = req->rq_xid;
1645 /* ensure previous bulk fails */
1646 req->rq_xid = ptlrpc_next_xid();
1647 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1648 old_xid, req->rq_xid);
1650 ptlrpc_wake_client_req(req);
1651 spin_unlock(&req->rq_lock);
1654 /* XXX: this function and rq_status are currently unused */
1655 void ptlrpc_restart_req(struct ptlrpc_request *req)
1657 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1658 req->rq_status = -ERESTARTSYS;
1660 spin_lock(&req->rq_lock);
1661 req->rq_restart = 1;
1662 req->rq_timedout = 0;
1663 ptlrpc_wake_client_req(req);
1664 spin_unlock(&req->rq_lock);
1667 static int expired_request(void *data)
1669 struct ptlrpc_request *req = data;
1672 /* some failure can suspend regular timeouts */
1673 if (ptlrpc_check_suspend())
1676 RETURN(ptlrpc_expire_one_request(req));
1679 static void interrupted_request(void *data)
1681 struct ptlrpc_request *req = data;
1682 DEBUG_REQ(D_HA, req, "request interrupted");
1683 spin_lock(&req->rq_lock);
1685 spin_unlock(&req->rq_lock);
1688 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1691 atomic_inc(&req->rq_refcount);
1695 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1696 struct obd_import *imp)
1698 struct list_head *tmp;
1700 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1702 /* clear this for new requests that were resent as well
1703 as resent replayed requests. */
1704 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1706 /* don't re-add requests that have been replayed */
1707 if (!list_empty(&req->rq_replay_list))
1710 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1712 LASSERT(imp->imp_replayable);
1713 /* Balanced in ptlrpc_free_committed, usually. */
1714 ptlrpc_request_addref(req);
1715 list_for_each_prev(tmp, &imp->imp_replay_list) {
1716 struct ptlrpc_request *iter =
1717 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1719 /* We may have duplicate transnos if we create and then
1720 * open a file, or for closes retained if to match creating
1721 * opens, so use req->rq_xid as a secondary key.
1722 * (See bugs 684, 685, and 428.)
1723 * XXX no longer needed, but all opens need transnos!
1725 if (iter->rq_transno > req->rq_transno)
1728 if (iter->rq_transno == req->rq_transno) {
1729 LASSERT(iter->rq_xid != req->rq_xid);
1730 if (iter->rq_xid > req->rq_xid)
1734 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1738 list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1741 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1745 struct l_wait_info lwi;
1746 struct obd_import *imp = req->rq_import;
1747 cfs_duration_t timeout = 0;
1750 LASSERT(req->rq_set == NULL);
1751 LASSERT(!req->rq_receiving_reply);
1752 atomic_inc(&imp->imp_inflight);
1754 /* for distributed debugging */
1755 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1756 LASSERT(imp->imp_obd != NULL);
1757 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1758 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1759 imp->imp_obd->obd_uuid.uuid,
1760 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1761 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1762 lustre_msg_get_opc(req->rq_reqmsg));
1764 /* Mark phase here for a little debug help */
1765 req->rq_phase = RQ_PHASE_RPC;
1767 spin_lock(&imp->imp_lock);
1768 req->rq_import_generation = imp->imp_generation;
1770 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1771 list_del(&req->rq_list);
1773 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1774 spin_unlock(&imp->imp_lock);
1776 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1778 ptlrpc_import_state_name(req->rq_send_state),
1779 ptlrpc_import_state_name(imp->imp_state));
1780 lwi = LWI_INTR(interrupted_request, req);
1781 rc = l_wait_event(req->rq_reply_waitq,
1782 (req->rq_send_state == imp->imp_state ||
1783 req->rq_err || req->rq_intr),
1785 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1787 ptlrpc_import_state_name(imp->imp_state),
1788 ptlrpc_import_state_name(req->rq_send_state),
1789 req->rq_err, req->rq_intr);
1791 spin_lock(&imp->imp_lock);
1792 list_del_init(&req->rq_list);
1797 else if (req->rq_intr) {
1800 else if (req->rq_no_resend) {
1801 spin_unlock(&imp->imp_lock);
1802 GOTO(out, rc = -ETIMEDOUT);
1810 list_del_init(&req->rq_list);
1811 spin_unlock(&imp->imp_lock);
1812 req->rq_status = rc; // XXX this ok?
1816 if (req->rq_resend) {
1817 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1819 if (req->rq_bulk != NULL) {
1820 ptlrpc_unregister_bulk (req);
1822 /* bulk requests are supposed to be
1823 * idempotent, so we are free to bump the xid
1824 * here, which we need to do before
1825 * registering the bulk again (bug 6371).
1826 * print the old xid first for sanity.
1828 DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1829 req->rq_xid = ptlrpc_next_xid();
1832 DEBUG_REQ(D_HA, req, "resending: ");
1835 /* XXX this is the same as ptlrpc_set_wait */
1836 LASSERT(list_empty(&req->rq_list));
1837 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1838 spin_unlock(&imp->imp_lock);
1840 rc = sptlrpc_req_refresh_ctx(req, 0);
1843 /* we got fatal ctx refresh error, directly jump out
1844 * thus we can pass back the actual error code.
1846 spin_lock(&imp->imp_lock);
1847 list_del_init(&req->rq_list);
1848 spin_unlock(&imp->imp_lock);
1850 CERROR("Failed to refresh ctx of req %p: %d\n", req, rc);
1853 /* simulating we got error during send rpc */
1857 rc = ptl_send_rpc(req, 0);
1859 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1862 timeout = cfs_timeout_cap(cfs_time_seconds(req->rq_timeout));
1863 DEBUG_REQ(D_NET, req,
1864 "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
1867 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1869 rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1870 if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
1874 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1875 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1876 imp->imp_obd->obd_uuid.uuid,
1877 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1878 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1879 lustre_msg_get_opc(req->rq_reqmsg));
1881 spin_lock(&imp->imp_lock);
1882 list_del_init(&req->rq_list);
1883 spin_unlock(&imp->imp_lock);
1885 /* If the reply was received normally, this just grabs the spinlock
1886 * (ensuring the reply callback has returned), sees that
1887 * req->rq_receiving_reply is clear and returns. */
1888 ptlrpc_unregister_reply (req);
1891 GOTO(out, rc = -EIO);
1893 /* Resend if we need to, unless we were interrupted. */
1894 if (req->rq_resend && !req->rq_intr) {
1895 /* ...unless we were specifically told otherwise. */
1896 if (req->rq_no_resend)
1897 GOTO(out, rc = -ETIMEDOUT);
1898 spin_lock(&imp->imp_lock);
1903 /* Should only be interrupted if we timed out. */
1904 if (!req->rq_timedout)
1905 DEBUG_REQ(D_ERROR, req,
1906 "rq_intr set but rq_timedout not");
1907 GOTO(out, rc = -EINTR);
1910 if (req->rq_timedout) { /* non-recoverable timeout */
1911 GOTO(out, rc = -ETIMEDOUT);
1914 if (!req->rq_replied) {
1915 /* How can this be? -eeb */
1916 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1918 GOTO(out, rc = req->rq_status);
1921 rc = after_reply(req);
1922 /* NB may return +ve success rc */
1923 if (req->rq_resend) {
1924 spin_lock(&imp->imp_lock);
1929 if (req->rq_bulk != NULL) {
1931 /* success so far. Note that anything going wrong
1932 * with bulk now, is EXTREMELY strange, since the
1933 * server must have believed that the bulk
1934 * tranferred OK before she replied with success to
1936 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1937 brc = l_wait_event(req->rq_reply_waitq,
1938 !ptlrpc_bulk_active(req->rq_bulk),
1940 LASSERT(brc == 0 || brc == -ETIMEDOUT);
1942 LASSERT(brc == -ETIMEDOUT);
1943 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1945 } else if (!req->rq_bulk->bd_success) {
1946 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1951 ptlrpc_unregister_bulk (req);
1954 LASSERT(!req->rq_receiving_reply);
1955 req->rq_phase = RQ_PHASE_INTERPRET;
1957 atomic_dec(&imp->imp_inflight);
1958 cfs_waitq_signal(&imp->imp_recovery_waitq);
1962 struct ptlrpc_replay_async_args {
1964 int praa_old_status;
1967 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1968 void * data, int rc)
1970 struct ptlrpc_replay_async_args *aa = data;
1971 struct obd_import *imp = req->rq_import;
1974 atomic_dec(&imp->imp_replay_inflight);
1976 if (!req->rq_replied) {
1977 CERROR("request replay timed out, restarting recovery\n");
1978 GOTO(out, rc = -ETIMEDOUT);
1981 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
1982 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
1983 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
1984 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1986 /* The transno had better not change over replay. */
1987 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
1988 lustre_msg_get_transno(req->rq_repmsg));
1990 DEBUG_REQ(D_HA, req, "got rep");
1992 /* let the callback do fixups, possibly including in the request */
1993 if (req->rq_replay_cb)
1994 req->rq_replay_cb(req);
1996 if (req->rq_replied &&
1997 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
1998 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1999 lustre_msg_get_status(req->rq_repmsg),
2000 aa->praa_old_status);
2002 /* Put it back for re-replay. */
2003 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2007 * Errors while replay can set transno to 0, but
2008 * imp_last_replay_transno shouldn't be set to 0 anyway
2010 if (req->rq_transno > 0) {
2011 spin_lock(&imp->imp_lock);
2012 LASSERT(req->rq_transno <= imp->imp_last_replay_transno);
2013 imp->imp_last_replay_transno = req->rq_transno;
2014 spin_unlock(&imp->imp_lock);
2016 CERROR("Transno is 0 during replay!\n");
2017 /* continue with recovery */
2018 rc = ptlrpc_import_recovery_state_machine(imp);
2020 req->rq_send_state = aa->praa_old_state;
2023 /* this replay failed, so restart recovery */
2024 ptlrpc_connect_import(imp, NULL);
2029 int ptlrpc_replay_req(struct ptlrpc_request *req)
2031 struct ptlrpc_replay_async_args *aa;
2034 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2036 /* Not handling automatic bulk replay yet (or ever?) */
2037 LASSERT(req->rq_bulk == NULL);
2039 DEBUG_REQ(D_HA, req, "REPLAY");
2041 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
2042 aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
2043 memset(aa, 0, sizeof *aa);
2045 /* Prepare request to be resent with ptlrpcd */
2046 aa->praa_old_state = req->rq_send_state;
2047 req->rq_send_state = LUSTRE_IMP_REPLAY;
2048 req->rq_phase = RQ_PHASE_NEW;
2049 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2052 req->rq_interpret_reply = ptlrpc_replay_interpret;
2053 atomic_inc(&req->rq_import->imp_replay_inflight);
2054 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2056 ptlrpcd_add_req(req);
2060 void ptlrpc_abort_inflight(struct obd_import *imp)
2062 struct list_head *tmp, *n;
2065 /* Make sure that no new requests get processed for this import.
2066 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2067 * this flag and then putting requests on sending_list or delayed_list.
2069 spin_lock(&imp->imp_lock);
2071 /* XXX locking? Maybe we should remove each request with the list
2072 * locked? Also, how do we know if the requests on the list are
2073 * being freed at this time?
2075 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2076 struct ptlrpc_request *req =
2077 list_entry(tmp, struct ptlrpc_request, rq_list);
2079 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2081 spin_lock (&req->rq_lock);
2082 if (req->rq_import_generation < imp->imp_generation) {
2084 ptlrpc_wake_client_req(req);
2086 spin_unlock (&req->rq_lock);
2089 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2090 struct ptlrpc_request *req =
2091 list_entry(tmp, struct ptlrpc_request, rq_list);
2093 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2095 spin_lock (&req->rq_lock);
2096 if (req->rq_import_generation < imp->imp_generation) {
2098 ptlrpc_wake_client_req(req);
2100 spin_unlock (&req->rq_lock);
2103 /* Last chance to free reqs left on the replay list, but we
2104 * will still leak reqs that haven't committed. */
2105 if (imp->imp_replayable)
2106 ptlrpc_free_committed(imp);
2108 spin_unlock(&imp->imp_lock);
2113 static __u64 ptlrpc_last_xid = 0;
2114 spinlock_t ptlrpc_last_xid_lock;
2116 __u64 ptlrpc_next_xid(void)
2119 spin_lock(&ptlrpc_last_xid_lock);
2120 tmp = ++ptlrpc_last_xid;
2121 spin_unlock(&ptlrpc_last_xid_lock);
2125 __u64 ptlrpc_sample_next_xid(void)
2128 spin_lock(&ptlrpc_last_xid_lock);
2129 tmp = ptlrpc_last_xid + 1;
2130 spin_unlock(&ptlrpc_last_xid_lock);
2133 EXPORT_SYMBOL(ptlrpc_sample_next_xid);