lustre/mdc: fix bad ERR_PTR usage in mdc_locks.c
[deliverable/linux.git] / drivers / staging / lustre / lustre / ptlrpc / client.c
CommitLineData
d7e09d03
PT
1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2012, Intel Corporation.
31 */
32/*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 */
36
37/** Implementation of client-side PortalRPC interfaces */
38
39#define DEBUG_SUBSYSTEM S_RPC
40
41#include <obd_support.h>
42#include <obd_class.h>
43#include <lustre_lib.h>
44#include <lustre_ha.h>
45#include <lustre_import.h>
46#include <lustre_req_layout.h>
47
48#include "ptlrpc_internal.h"
49
50static int ptlrpc_send_new_req(struct ptlrpc_request *req);
51
52/**
53 * Initialize passed in client structure \a cl.
54 */
55void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
56 struct ptlrpc_client *cl)
57{
58 cl->cli_request_portal = req_portal;
59 cl->cli_reply_portal = rep_portal;
60 cl->cli_name = name;
61}
62EXPORT_SYMBOL(ptlrpc_init_client);
63
64/**
930cef9a 65 * Return PortalRPC connection for remote uud \a uuid
d7e09d03
PT
66 */
67struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
68{
69 struct ptlrpc_connection *c;
70 lnet_nid_t self;
71 lnet_process_id_t peer;
72 int err;
73
74 /* ptlrpc_uuid_to_peer() initializes its 2nd parameter
75 * before accessing its values. */
76 /* coverity[uninit_use_in_call] */
77 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
78 if (err != 0) {
79 CNETERR("cannot find peer %s!\n", uuid->uuid);
80 return NULL;
81 }
82
83 c = ptlrpc_connection_get(peer, self, uuid);
84 if (c) {
85 memcpy(c->c_remote_uuid.uuid,
86 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
87 }
88
89 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
90
91 return c;
92}
93EXPORT_SYMBOL(ptlrpc_uuid_to_connection);
94
95/**
96 * Allocate and initialize new bulk descriptor on the sender.
97 * Returns pointer to the descriptor or NULL on error.
98 */
99struct ptlrpc_bulk_desc *ptlrpc_new_bulk(unsigned npages, unsigned max_brw,
100 unsigned type, unsigned portal)
101{
102 struct ptlrpc_bulk_desc *desc;
103 int i;
104
105 OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[npages]));
106 if (!desc)
107 return NULL;
108
109 spin_lock_init(&desc->bd_lock);
110 init_waitqueue_head(&desc->bd_waitq);
111 desc->bd_max_iov = npages;
112 desc->bd_iov_count = 0;
113 desc->bd_portal = portal;
114 desc->bd_type = type;
115 desc->bd_md_count = 0;
116 LASSERT(max_brw > 0);
117 desc->bd_md_max_brw = min(max_brw, PTLRPC_BULK_OPS_COUNT);
118 /* PTLRPC_BULK_OPS_COUNT is the compile-time transfer limit for this
119 * node. Negotiated ocd_brw_size will always be <= this number. */
120 for (i = 0; i < PTLRPC_BULK_OPS_COUNT; i++)
121 LNetInvalidateHandle(&desc->bd_mds[i]);
122
123 return desc;
124}
125
126/**
127 * Prepare bulk descriptor for specified outgoing request \a req that
128 * can fit \a npages * pages. \a type is bulk type. \a portal is where
129 * the bulk to be sent. Used on client-side.
930cef9a 130 * Returns pointer to newly allocated initialized bulk descriptor or NULL on
d7e09d03
PT
131 * error.
132 */
133struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
134 unsigned npages, unsigned max_brw,
135 unsigned type, unsigned portal)
136{
137 struct obd_import *imp = req->rq_import;
138 struct ptlrpc_bulk_desc *desc;
139
d7e09d03
PT
140 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
141 desc = ptlrpc_new_bulk(npages, max_brw, type, portal);
142 if (desc == NULL)
0a3bdb00 143 return NULL;
d7e09d03
PT
144
145 desc->bd_import_generation = req->rq_import_generation;
146 desc->bd_import = class_import_get(imp);
147 desc->bd_req = req;
148
149 desc->bd_cbid.cbid_fn = client_bulk_callback;
150 desc->bd_cbid.cbid_arg = desc;
151
152 /* This makes req own desc, and free it when she frees herself */
153 req->rq_bulk = desc;
154
155 return desc;
156}
157EXPORT_SYMBOL(ptlrpc_prep_bulk_imp);
158
159/**
160 * Add a page \a page to the bulk descriptor \a desc.
161 * Data to transfer in the page starts at offset \a pageoffset and
162 * amount of data to transfer from the page is \a len
163 */
164void __ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
165 struct page *page, int pageoffset, int len, int pin)
166{
167 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
168 LASSERT(page != NULL);
169 LASSERT(pageoffset >= 0);
170 LASSERT(len > 0);
171 LASSERT(pageoffset + len <= PAGE_CACHE_SIZE);
172
173 desc->bd_nob += len;
174
175 if (pin)
176 page_cache_get(page);
177
178 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
179}
180EXPORT_SYMBOL(__ptlrpc_prep_bulk_page);
181
182/**
183 * Uninitialize and free bulk descriptor \a desc.
184 * Works on bulk descriptors both from server and client side.
185 */
186void __ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc, int unpin)
187{
188 int i;
d7e09d03
PT
189
190 LASSERT(desc != NULL);
191 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
192 LASSERT(desc->bd_md_count == 0); /* network hands off */
193 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
194
195 sptlrpc_enc_pool_put_pages(desc);
196
197 if (desc->bd_export)
198 class_export_put(desc->bd_export);
199 else
200 class_import_put(desc->bd_import);
201
202 if (unpin) {
7b8633de 203 for (i = 0; i < desc->bd_iov_count; i++)
d7e09d03
PT
204 page_cache_release(desc->bd_iov[i].kiov_page);
205 }
206
207 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
208 bd_iov[desc->bd_max_iov]));
d7e09d03
PT
209}
210EXPORT_SYMBOL(__ptlrpc_free_bulk);
211
212/**
213 * Set server timelimit for this req, i.e. how long are we willing to wait
214 * for reply before timing out this request.
215 */
216void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
217{
218 __u32 serv_est;
219 int idx;
220 struct imp_at *at;
221
222 LASSERT(req->rq_import);
223
224 if (AT_OFF) {
225 /* non-AT settings */
226 /**
227 * \a imp_server_timeout means this is reverse import and
228 * we send (currently only) ASTs to the client and cannot afford
229 * to wait too long for the reply, otherwise the other client
230 * (because of which we are sending this request) would
231 * timeout waiting for us
232 */
233 req->rq_timeout = req->rq_import->imp_server_timeout ?
234 obd_timeout / 2 : obd_timeout;
235 } else {
236 at = &req->rq_import->imp_at;
237 idx = import_at_get_index(req->rq_import,
238 req->rq_request_portal);
239 serv_est = at_get(&at->iat_service_estimate[idx]);
240 req->rq_timeout = at_est2timeout(serv_est);
241 }
242 /* We could get even fancier here, using history to predict increased
243 loading... */
244
245 /* Let the server know what this RPC timeout is by putting it in the
246 reqmsg*/
247 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
248}
249EXPORT_SYMBOL(ptlrpc_at_set_req_timeout);
250
251/* Adjust max service estimate based on server value */
252static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
253 unsigned int serv_est)
254{
255 int idx;
256 unsigned int oldse;
257 struct imp_at *at;
258
259 LASSERT(req->rq_import);
260 at = &req->rq_import->imp_at;
261
262 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
263 /* max service estimates are tracked on the server side,
264 so just keep minimal history here */
265 oldse = at_measured(&at->iat_service_estimate[idx], serv_est);
266 if (oldse != 0)
267 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
268 "has changed from %d to %d\n",
269 req->rq_import->imp_obd->obd_name,req->rq_request_portal,
270 oldse, at_get(&at->iat_service_estimate[idx]));
271}
272
273/* Expected network latency per remote node (secs) */
274int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
275{
276 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
277}
278
279/* Adjust expected network latency */
280static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
281 unsigned int service_time)
282{
283 unsigned int nl, oldnl;
284 struct imp_at *at;
285 time_t now = cfs_time_current_sec();
286
287 LASSERT(req->rq_import);
288 at = &req->rq_import->imp_at;
289
290 /* Network latency is total time less server processing time */
291 nl = max_t(int, now - req->rq_sent - service_time, 0) +1/*st rounding*/;
292 if (service_time > now - req->rq_sent + 3 /* bz16408 */)
293 CWARN("Reported service time %u > total measured time "
294 CFS_DURATION_T"\n", service_time,
295 cfs_time_sub(now, req->rq_sent));
296
297 oldnl = at_measured(&at->iat_net_latency, nl);
298 if (oldnl != 0)
299 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
300 "has changed from %d to %d\n",
301 req->rq_import->imp_obd->obd_name,
302 obd_uuid2str(
303 &req->rq_import->imp_connection->c_remote_uuid),
304 oldnl, at_get(&at->iat_net_latency));
305}
306
307static int unpack_reply(struct ptlrpc_request *req)
308{
309 int rc;
310
311 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
312 rc = ptlrpc_unpack_rep_msg(req, req->rq_replen);
313 if (rc) {
314 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
315 return(-EPROTO);
316 }
317 }
318
319 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
320 if (rc) {
321 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
322 return(-EPROTO);
323 }
324 return 0;
325}
326
327/**
328 * Handle an early reply message, called with the rq_lock held.
329 * If anything goes wrong just ignore it - same as if it never happened
330 */
331static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req)
332{
333 struct ptlrpc_request *early_req;
334 time_t olddl;
335 int rc;
d7e09d03
PT
336
337 req->rq_early = 0;
338 spin_unlock(&req->rq_lock);
339
340 rc = sptlrpc_cli_unwrap_early_reply(req, &early_req);
341 if (rc) {
342 spin_lock(&req->rq_lock);
0a3bdb00 343 return rc;
d7e09d03
PT
344 }
345
346 rc = unpack_reply(early_req);
347 if (rc == 0) {
348 /* Expecting to increase the service time estimate here */
349 ptlrpc_at_adj_service(req,
350 lustre_msg_get_timeout(early_req->rq_repmsg));
351 ptlrpc_at_adj_net_latency(req,
352 lustre_msg_get_service_time(early_req->rq_repmsg));
353 }
354
355 sptlrpc_cli_finish_early_reply(early_req);
356
357 if (rc != 0) {
358 spin_lock(&req->rq_lock);
0a3bdb00 359 return rc;
d7e09d03
PT
360 }
361
362 /* Adjust the local timeout for this req */
363 ptlrpc_at_set_req_timeout(req);
364
365 spin_lock(&req->rq_lock);
366 olddl = req->rq_deadline;
367 /* server assumes it now has rq_timeout from when it sent the
368 * early reply, so client should give it at least that long. */
369 req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
370 ptlrpc_at_get_net_latency(req);
371
372 DEBUG_REQ(D_ADAPTTO, req,
373 "Early reply #%d, new deadline in "CFS_DURATION_T"s "
374 "("CFS_DURATION_T"s)", req->rq_early_count,
375 cfs_time_sub(req->rq_deadline, cfs_time_current_sec()),
376 cfs_time_sub(req->rq_deadline, olddl));
377
0a3bdb00 378 return rc;
d7e09d03
PT
379}
380
381/**
382 * Wind down request pool \a pool.
383 * Frees all requests from the pool too
384 */
385void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
386{
387 struct list_head *l, *tmp;
388 struct ptlrpc_request *req;
389
390 LASSERT(pool != NULL);
391
392 spin_lock(&pool->prp_lock);
393 list_for_each_safe(l, tmp, &pool->prp_req_list) {
394 req = list_entry(l, struct ptlrpc_request, rq_list);
395 list_del(&req->rq_list);
396 LASSERT(req->rq_reqbuf);
397 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
398 OBD_FREE_LARGE(req->rq_reqbuf, pool->prp_rq_size);
399 OBD_FREE(req, sizeof(*req));
400 }
401 spin_unlock(&pool->prp_lock);
402 OBD_FREE(pool, sizeof(*pool));
403}
404EXPORT_SYMBOL(ptlrpc_free_rq_pool);
405
406/**
407 * Allocates, initializes and adds \a num_rq requests to the pool \a pool
408 */
409void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
410{
411 int i;
412 int size = 1;
413
414 while (size < pool->prp_rq_size)
415 size <<= 1;
416
417 LASSERTF(list_empty(&pool->prp_req_list) ||
418 size == pool->prp_rq_size,
419 "Trying to change pool size with nonempty pool "
420 "from %d to %d bytes\n", pool->prp_rq_size, size);
421
422 spin_lock(&pool->prp_lock);
423 pool->prp_rq_size = size;
424 for (i = 0; i < num_rq; i++) {
425 struct ptlrpc_request *req;
426 struct lustre_msg *msg;
427
428 spin_unlock(&pool->prp_lock);
429 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
430 if (!req)
431 return;
432 OBD_ALLOC_LARGE(msg, size);
433 if (!msg) {
434 OBD_FREE(req, sizeof(struct ptlrpc_request));
435 return;
436 }
437 req->rq_reqbuf = msg;
438 req->rq_reqbuf_len = size;
439 req->rq_pool = pool;
440 spin_lock(&pool->prp_lock);
441 list_add_tail(&req->rq_list, &pool->prp_req_list);
442 }
443 spin_unlock(&pool->prp_lock);
444 return;
445}
446EXPORT_SYMBOL(ptlrpc_add_rqs_to_pool);
447
448/**
449 * Create and initialize new request pool with given attributes:
450 * \a num_rq - initial number of requests to create for the pool
451 * \a msgsize - maximum message size possible for requests in thid pool
452 * \a populate_pool - function to be called when more requests need to be added
453 * to the pool
454 * Returns pointer to newly created pool or NULL on error.
455 */
456struct ptlrpc_request_pool *
457ptlrpc_init_rq_pool(int num_rq, int msgsize,
458 void (*populate_pool)(struct ptlrpc_request_pool *, int))
459{
460 struct ptlrpc_request_pool *pool;
461
3949015e 462 OBD_ALLOC(pool, sizeof(struct ptlrpc_request_pool));
d7e09d03
PT
463 if (!pool)
464 return NULL;
465
466 /* Request next power of two for the allocation, because internally
467 kernel would do exactly this */
468
469 spin_lock_init(&pool->prp_lock);
470 INIT_LIST_HEAD(&pool->prp_req_list);
471 pool->prp_rq_size = msgsize + SPTLRPC_MAX_PAYLOAD;
472 pool->prp_populate = populate_pool;
473
474 populate_pool(pool, num_rq);
475
476 if (list_empty(&pool->prp_req_list)) {
477 /* have not allocated a single request for the pool */
3949015e 478 OBD_FREE(pool, sizeof(struct ptlrpc_request_pool));
d7e09d03
PT
479 pool = NULL;
480 }
481 return pool;
482}
483EXPORT_SYMBOL(ptlrpc_init_rq_pool);
484
485/**
486 * Fetches one request from pool \a pool
487 */
488static struct ptlrpc_request *
489ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
490{
491 struct ptlrpc_request *request;
492 struct lustre_msg *reqbuf;
493
494 if (!pool)
495 return NULL;
496
497 spin_lock(&pool->prp_lock);
498
499 /* See if we have anything in a pool, and bail out if nothing,
500 * in writeout path, where this matters, this is safe to do, because
501 * nothing is lost in this case, and when some in-flight requests
502 * complete, this code will be called again. */
503 if (unlikely(list_empty(&pool->prp_req_list))) {
504 spin_unlock(&pool->prp_lock);
505 return NULL;
506 }
507
508 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
509 rq_list);
510 list_del_init(&request->rq_list);
511 spin_unlock(&pool->prp_lock);
512
513 LASSERT(request->rq_reqbuf);
514 LASSERT(request->rq_pool);
515
516 reqbuf = request->rq_reqbuf;
517 memset(request, 0, sizeof(*request));
518 request->rq_reqbuf = reqbuf;
519 request->rq_reqbuf_len = pool->prp_rq_size;
520 request->rq_pool = pool;
521
522 return request;
523}
524
525/**
526 * Returns freed \a request to pool.
527 */
528static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
529{
530 struct ptlrpc_request_pool *pool = request->rq_pool;
531
532 spin_lock(&pool->prp_lock);
533 LASSERT(list_empty(&request->rq_list));
534 LASSERT(!request->rq_receiving_reply);
535 list_add_tail(&request->rq_list, &pool->prp_req_list);
536 spin_unlock(&pool->prp_lock);
537}
538
539static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
540 __u32 version, int opcode,
541 int count, __u32 *lengths, char **bufs,
542 struct ptlrpc_cli_ctx *ctx)
543{
544 struct obd_import *imp = request->rq_import;
545 int rc;
d7e09d03
PT
546
547 if (unlikely(ctx))
548 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
549 else {
550 rc = sptlrpc_req_get_ctx(request);
551 if (rc)
552 GOTO(out_free, rc);
553 }
554
555 sptlrpc_req_set_flavor(request, opcode);
556
557 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
558 lengths, bufs);
559 if (rc) {
560 LASSERT(!request->rq_pool);
561 GOTO(out_ctx, rc);
562 }
563
564 lustre_msg_add_version(request->rq_reqmsg, version);
565 request->rq_send_state = LUSTRE_IMP_FULL;
566 request->rq_type = PTL_RPC_MSG_REQUEST;
567 request->rq_export = NULL;
568
569 request->rq_req_cbid.cbid_fn = request_out_callback;
570 request->rq_req_cbid.cbid_arg = request;
571
572 request->rq_reply_cbid.cbid_fn = reply_in_callback;
573 request->rq_reply_cbid.cbid_arg = request;
574
575 request->rq_reply_deadline = 0;
576 request->rq_phase = RQ_PHASE_NEW;
577 request->rq_next_phase = RQ_PHASE_UNDEFINED;
578
579 request->rq_request_portal = imp->imp_client->cli_request_portal;
580 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
581
582 ptlrpc_at_set_req_timeout(request);
583
584 spin_lock_init(&request->rq_lock);
585 INIT_LIST_HEAD(&request->rq_list);
586 INIT_LIST_HEAD(&request->rq_timed_list);
587 INIT_LIST_HEAD(&request->rq_replay_list);
588 INIT_LIST_HEAD(&request->rq_ctx_chain);
589 INIT_LIST_HEAD(&request->rq_set_chain);
590 INIT_LIST_HEAD(&request->rq_history_list);
591 INIT_LIST_HEAD(&request->rq_exp_list);
592 init_waitqueue_head(&request->rq_reply_waitq);
593 init_waitqueue_head(&request->rq_set_waitq);
594 request->rq_xid = ptlrpc_next_xid();
595 atomic_set(&request->rq_refcount, 1);
596
597 lustre_msg_set_opc(request->rq_reqmsg, opcode);
598
0a3bdb00 599 return 0;
d7e09d03
PT
600out_ctx:
601 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
602out_free:
603 class_import_put(imp);
604 return rc;
605}
606
607int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
608 __u32 version, int opcode, char **bufs,
609 struct ptlrpc_cli_ctx *ctx)
610{
611 int count;
612
613 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
614 return __ptlrpc_request_bufs_pack(request, version, opcode, count,
615 request->rq_pill.rc_area[RCL_CLIENT],
616 bufs, ctx);
617}
618EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
619
620/**
621 * Pack request buffers for network transfer, performing necessary encryption
622 * steps if necessary.
623 */
624int ptlrpc_request_pack(struct ptlrpc_request *request,
625 __u32 version, int opcode)
626{
627 int rc;
628 rc = ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
629 if (rc)
630 return rc;
631
632 /* For some old 1.8 clients (< 1.8.7), they will LASSERT the size of
633 * ptlrpc_body sent from server equal to local ptlrpc_body size, so we
930cef9a 634 * have to send old ptlrpc_body to keep interoperability with these
d7e09d03
PT
635 * clients.
636 *
637 * Only three kinds of server->client RPCs so far:
638 * - LDLM_BL_CALLBACK
639 * - LDLM_CP_CALLBACK
640 * - LDLM_GL_CALLBACK
641 *
930cef9a 642 * XXX This should be removed whenever we drop the interoperability with
d7e09d03
PT
643 * the these old clients.
644 */
645 if (opcode == LDLM_BL_CALLBACK || opcode == LDLM_CP_CALLBACK ||
646 opcode == LDLM_GL_CALLBACK)
647 req_capsule_shrink(&request->rq_pill, &RMF_PTLRPC_BODY,
648 sizeof(struct ptlrpc_body_v2), RCL_CLIENT);
649
650 return rc;
651}
652EXPORT_SYMBOL(ptlrpc_request_pack);
653
654/**
655 * Helper function to allocate new request on import \a imp
656 * and possibly using existing request from pool \a pool if provided.
657 * Returns allocated request structure with import field filled or
658 * NULL on error.
659 */
660static inline
661struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
662 struct ptlrpc_request_pool *pool)
663{
664 struct ptlrpc_request *request = NULL;
665
666 if (pool)
667 request = ptlrpc_prep_req_from_pool(pool);
668
669 if (!request)
670 OBD_ALLOC_PTR(request);
671
672 if (request) {
673 LASSERTF((unsigned long)imp > 0x1000, "%p", imp);
674 LASSERT(imp != LP_POISON);
675 LASSERTF((unsigned long)imp->imp_client > 0x1000, "%p",
676 imp->imp_client);
677 LASSERT(imp->imp_client != LP_POISON);
678
679 request->rq_import = class_import_get(imp);
680 } else {
681 CERROR("request allocation out of memory\n");
682 }
683
684 return request;
685}
686
687/**
688 * Helper function for creating a request.
930cef9a 689 * Calls __ptlrpc_request_alloc to allocate new request structure and inits
d7e09d03
PT
690 * buffer structures according to capsule template \a format.
691 * Returns allocated request structure pointer or NULL on error.
692 */
693static struct ptlrpc_request *
694ptlrpc_request_alloc_internal(struct obd_import *imp,
695 struct ptlrpc_request_pool * pool,
696 const struct req_format *format)
697{
698 struct ptlrpc_request *request;
699
700 request = __ptlrpc_request_alloc(imp, pool);
701 if (request == NULL)
702 return NULL;
703
704 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
705 req_capsule_set(&request->rq_pill, format);
706 return request;
707}
708
709/**
710 * Allocate new request structure for import \a imp and initialize its
711 * buffer structure according to capsule template \a format.
712 */
713struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
714 const struct req_format *format)
715{
716 return ptlrpc_request_alloc_internal(imp, NULL, format);
717}
718EXPORT_SYMBOL(ptlrpc_request_alloc);
719
720/**
721 * Allocate new request structure for import \a imp from pool \a pool and
722 * initialize its buffer structure according to capsule template \a format.
723 */
724struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
725 struct ptlrpc_request_pool * pool,
726 const struct req_format *format)
727{
728 return ptlrpc_request_alloc_internal(imp, pool, format);
729}
730EXPORT_SYMBOL(ptlrpc_request_alloc_pool);
731
732/**
733 * For requests not from pool, free memory of the request structure.
734 * For requests obtained from a pool earlier, return request back to pool.
735 */
736void ptlrpc_request_free(struct ptlrpc_request *request)
737{
738 if (request->rq_pool)
739 __ptlrpc_free_req_to_pool(request);
740 else
741 OBD_FREE_PTR(request);
742}
743EXPORT_SYMBOL(ptlrpc_request_free);
744
745/**
930cef9a 746 * Allocate new request for operation \a opcode and immediately pack it for
d7e09d03
PT
747 * network transfer.
748 * Only used for simple requests like OBD_PING where the only important
749 * part of the request is operation itself.
750 * Returns allocated request or NULL on error.
751 */
752struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
753 const struct req_format *format,
754 __u32 version, int opcode)
755{
756 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
757 int rc;
758
759 if (req) {
760 rc = ptlrpc_request_pack(req, version, opcode);
761 if (rc) {
762 ptlrpc_request_free(req);
763 req = NULL;
764 }
765 }
766 return req;
767}
768EXPORT_SYMBOL(ptlrpc_request_alloc_pack);
769
770/**
930cef9a 771 * Prepare request (fetched from pool \a pool if not NULL) on import \a imp
d7e09d03
PT
772 * for operation \a opcode. Request would contain \a count buffers.
773 * Sizes of buffers are described in array \a lengths and buffers themselves
774 * are provided by a pointer \a bufs.
775 * Returns prepared request structure pointer or NULL on error.
776 */
777struct ptlrpc_request *
778ptlrpc_prep_req_pool(struct obd_import *imp,
779 __u32 version, int opcode,
780 int count, __u32 *lengths, char **bufs,
781 struct ptlrpc_request_pool *pool)
782{
783 struct ptlrpc_request *request;
784 int rc;
785
786 request = __ptlrpc_request_alloc(imp, pool);
787 if (!request)
788 return NULL;
789
790 rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
791 lengths, bufs, NULL);
792 if (rc) {
793 ptlrpc_request_free(request);
794 request = NULL;
795 }
796 return request;
797}
798EXPORT_SYMBOL(ptlrpc_prep_req_pool);
799
800/**
801 * Same as ptlrpc_prep_req_pool, but without pool
802 */
803struct ptlrpc_request *
804ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
805 __u32 *lengths, char **bufs)
806{
807 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
808 NULL);
809}
810EXPORT_SYMBOL(ptlrpc_prep_req);
811
812/**
813 * Allocate and initialize new request set structure.
814 * Returns a pointer to the newly allocated set structure or NULL on error.
815 */
816struct ptlrpc_request_set *ptlrpc_prep_set(void)
817{
818 struct ptlrpc_request_set *set;
819
ec83e611 820 OBD_ALLOC(set, sizeof(*set));
d7e09d03 821 if (!set)
0a3bdb00 822 return NULL;
d7e09d03
PT
823 atomic_set(&set->set_refcount, 1);
824 INIT_LIST_HEAD(&set->set_requests);
825 init_waitqueue_head(&set->set_waitq);
826 atomic_set(&set->set_new_count, 0);
827 atomic_set(&set->set_remaining, 0);
828 spin_lock_init(&set->set_new_req_lock);
829 INIT_LIST_HEAD(&set->set_new_requests);
830 INIT_LIST_HEAD(&set->set_cblist);
831 set->set_max_inflight = UINT_MAX;
832 set->set_producer = NULL;
833 set->set_producer_arg = NULL;
834 set->set_rc = 0;
835
0a3bdb00 836 return set;
d7e09d03
PT
837}
838EXPORT_SYMBOL(ptlrpc_prep_set);
839
840/**
841 * Allocate and initialize new request set structure with flow control
842 * extension. This extension allows to control the number of requests in-flight
843 * for the whole set. A callback function to generate requests must be provided
844 * and the request set will keep the number of requests sent over the wire to
845 * @max_inflight.
846 * Returns a pointer to the newly allocated set structure or NULL on error.
847 */
848struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
849 void *arg)
850
851{
852 struct ptlrpc_request_set *set;
853
854 set = ptlrpc_prep_set();
855 if (!set)
0a3bdb00 856 return NULL;
d7e09d03
PT
857
858 set->set_max_inflight = max;
859 set->set_producer = func;
860 set->set_producer_arg = arg;
861
0a3bdb00 862 return set;
d7e09d03
PT
863}
864EXPORT_SYMBOL(ptlrpc_prep_fcset);
865
866/**
867 * Wind down and free request set structure previously allocated with
868 * ptlrpc_prep_set.
869 * Ensures that all requests on the set have completed and removes
870 * all requests from the request list in a set.
871 * If any unsent request happen to be on the list, pretends that they got
872 * an error in flight and calls their completion handler.
873 */
874void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
875{
876 struct list_head *tmp;
877 struct list_head *next;
878 int expected_phase;
879 int n = 0;
d7e09d03
PT
880
881 /* Requests on the set should either all be completed, or all be new */
882 expected_phase = (atomic_read(&set->set_remaining) == 0) ?
883 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
3949015e 884 list_for_each(tmp, &set->set_requests) {
d7e09d03
PT
885 struct ptlrpc_request *req =
886 list_entry(tmp, struct ptlrpc_request,
887 rq_set_chain);
888
889 LASSERT(req->rq_phase == expected_phase);
890 n++;
891 }
892
893 LASSERTF(atomic_read(&set->set_remaining) == 0 ||
894 atomic_read(&set->set_remaining) == n, "%d / %d\n",
895 atomic_read(&set->set_remaining), n);
896
897 list_for_each_safe(tmp, next, &set->set_requests) {
898 struct ptlrpc_request *req =
899 list_entry(tmp, struct ptlrpc_request,
900 rq_set_chain);
901 list_del_init(&req->rq_set_chain);
902
903 LASSERT(req->rq_phase == expected_phase);
904
905 if (req->rq_phase == RQ_PHASE_NEW) {
906 ptlrpc_req_interpret(NULL, req, -EBADR);
907 atomic_dec(&set->set_remaining);
908 }
909
910 spin_lock(&req->rq_lock);
911 req->rq_set = NULL;
912 req->rq_invalid_rqset = 0;
913 spin_unlock(&req->rq_lock);
914
3949015e 915 ptlrpc_req_finished(req);
d7e09d03
PT
916 }
917
918 LASSERT(atomic_read(&set->set_remaining) == 0);
919
920 ptlrpc_reqset_put(set);
d7e09d03
PT
921}
922EXPORT_SYMBOL(ptlrpc_set_destroy);
923
924/**
925 * Add a callback function \a fn to the set.
926 * This function would be called when all requests on this set are completed.
927 * The function will be passed \a data argument.
928 */
929int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
930 set_interpreter_func fn, void *data)
931{
932 struct ptlrpc_set_cbdata *cbdata;
933
934 OBD_ALLOC_PTR(cbdata);
935 if (cbdata == NULL)
0a3bdb00 936 return -ENOMEM;
d7e09d03
PT
937
938 cbdata->psc_interpret = fn;
939 cbdata->psc_data = data;
940 list_add_tail(&cbdata->psc_item, &set->set_cblist);
941
0a3bdb00 942 return 0;
d7e09d03
PT
943}
944EXPORT_SYMBOL(ptlrpc_set_add_cb);
945
946/**
947 * Add a new request to the general purpose request set.
948 * Assumes request reference from the caller.
949 */
950void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
951 struct ptlrpc_request *req)
952{
953 LASSERT(list_empty(&req->rq_set_chain));
954
955 /* The set takes over the caller's request reference */
956 list_add_tail(&req->rq_set_chain, &set->set_requests);
957 req->rq_set = set;
958 atomic_inc(&set->set_remaining);
959 req->rq_queued_time = cfs_time_current();
960
961 if (req->rq_reqmsg != NULL)
962 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
963
964 if (set->set_producer != NULL)
965 /* If the request set has a producer callback, the RPC must be
966 * sent straight away */
967 ptlrpc_send_new_req(req);
968}
969EXPORT_SYMBOL(ptlrpc_set_add_req);
970
971/**
972 * Add a request to a request with dedicated server thread
973 * and wake the thread to make any necessary processing.
974 * Currently only used for ptlrpcd.
975 */
976void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
977 struct ptlrpc_request *req)
978{
979 struct ptlrpc_request_set *set = pc->pc_set;
980 int count, i;
981
982 LASSERT(req->rq_set == NULL);
983 LASSERT(test_bit(LIOD_STOP, &pc->pc_flags) == 0);
984
985 spin_lock(&set->set_new_req_lock);
986 /*
987 * The set takes over the caller's request reference.
988 */
989 req->rq_set = set;
990 req->rq_queued_time = cfs_time_current();
991 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
992 count = atomic_inc_return(&set->set_new_count);
993 spin_unlock(&set->set_new_req_lock);
994
995 /* Only need to call wakeup once for the first entry. */
996 if (count == 1) {
997 wake_up(&set->set_waitq);
998
999 /* XXX: It maybe unnecessary to wakeup all the partners. But to
1000 * guarantee the async RPC can be processed ASAP, we have
1001 * no other better choice. It maybe fixed in future. */
1002 for (i = 0; i < pc->pc_npartners; i++)
1003 wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
1004 }
1005}
1006EXPORT_SYMBOL(ptlrpc_set_add_new_req);
1007
1008/**
1009 * Based on the current state of the import, determine if the request
1010 * can be sent, is an error, or should be delayed.
1011 *
1012 * Returns true if this request should be delayed. If false, and
1013 * *status is set, then the request can not be sent and *status is the
1014 * error code. If false and status is 0, then request can be sent.
1015 *
1016 * The imp->imp_lock must be held.
1017 */
1018static int ptlrpc_import_delay_req(struct obd_import *imp,
1019 struct ptlrpc_request *req, int *status)
1020{
1021 int delay = 0;
d7e09d03 1022
3949015e 1023 LASSERT(status != NULL);
d7e09d03
PT
1024 *status = 0;
1025
1026 if (req->rq_ctx_init || req->rq_ctx_fini) {
1027 /* always allow ctx init/fini rpc go through */
1028 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
1029 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
1030 *status = -EIO;
1031 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1032 /* pings may safely race with umount */
1033 DEBUG_REQ(lustre_msg_get_opc(req->rq_reqmsg) == OBD_PING ?
1034 D_HA : D_ERROR, req, "IMP_CLOSED ");
1035 *status = -EIO;
1036 } else if (ptlrpc_send_limit_expired(req)) {
1037 /* probably doesn't need to be a D_ERROR after initial testing */
1038 DEBUG_REQ(D_ERROR, req, "send limit expired ");
1039 *status = -EIO;
1040 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
1041 imp->imp_state == LUSTRE_IMP_CONNECTING) {
7b8633de 1042 /* allow CONNECT even if import is invalid */
d7e09d03
PT
1043 if (atomic_read(&imp->imp_inval_count) != 0) {
1044 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1045 *status = -EIO;
1046 }
1047 } else if (imp->imp_invalid || imp->imp_obd->obd_no_recov) {
1048 if (!imp->imp_deactive)
1049 DEBUG_REQ(D_NET, req, "IMP_INVALID");
1050 *status = -ESHUTDOWN; /* bz 12940 */
1051 } else if (req->rq_import_generation != imp->imp_generation) {
1052 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
1053 *status = -EIO;
1054 } else if (req->rq_send_state != imp->imp_state) {
1055 /* invalidate in progress - any requests should be drop */
1056 if (atomic_read(&imp->imp_inval_count) != 0) {
1057 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1058 *status = -EIO;
1059 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
1060 *status = -EWOULDBLOCK;
1061 } else if (req->rq_allow_replay &&
1062 (imp->imp_state == LUSTRE_IMP_REPLAY ||
1063 imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS ||
1064 imp->imp_state == LUSTRE_IMP_REPLAY_WAIT ||
1065 imp->imp_state == LUSTRE_IMP_RECOVER)) {
1066 DEBUG_REQ(D_HA, req, "allow during recovery.\n");
1067 } else {
1068 delay = 1;
1069 }
1070 }
1071
0a3bdb00 1072 return delay;
d7e09d03
PT
1073}
1074
1075/**
930cef9a 1076 * Decide if the error message regarding provided request \a req
d7e09d03
PT
1077 * should be printed to the console or not.
1078 * Makes it's decision on request status and other properties.
1079 * Returns 1 to print error on the system console or 0 if not.
1080 */
1081static int ptlrpc_console_allow(struct ptlrpc_request *req)
1082{
1083 __u32 opc;
1084 int err;
1085
1086 LASSERT(req->rq_reqmsg != NULL);
1087 opc = lustre_msg_get_opc(req->rq_reqmsg);
1088
1089 /* Suppress particular reconnect errors which are to be expected. No
1090 * errors are suppressed for the initial connection on an import */
1091 if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
1092 (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
1093
1094 /* Suppress timed out reconnect requests */
1095 if (req->rq_timedout)
1096 return 0;
1097
1098 /* Suppress unavailable/again reconnect requests */
1099 err = lustre_msg_get_status(req->rq_repmsg);
1100 if (err == -ENODEV || err == -EAGAIN)
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
1107/**
1108 * Check request processing status.
1109 * Returns the status.
1110 */
1111static int ptlrpc_check_status(struct ptlrpc_request *req)
1112{
1113 int err;
d7e09d03
PT
1114
1115 err = lustre_msg_get_status(req->rq_repmsg);
1116 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1117 struct obd_import *imp = req->rq_import;
1118 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1119 if (ptlrpc_console_allow(req))
1120 LCONSOLE_ERROR_MSG(0x011, "%s: Communicating with %s,"
1121 " operation %s failed with %d.\n",
1122 imp->imp_obd->obd_name,
1123 libcfs_nid2str(
1124 imp->imp_connection->c_peer.nid),
1125 ll_opcode2str(opc), err);
0a3bdb00 1126 return err < 0 ? err : -EINVAL;
d7e09d03
PT
1127 }
1128
1129 if (err < 0) {
1130 DEBUG_REQ(D_INFO, req, "status is %d", err);
1131 } else if (err > 0) {
1132 /* XXX: translate this error from net to host */
1133 DEBUG_REQ(D_INFO, req, "status is %d", err);
1134 }
1135
0a3bdb00 1136 return err;
d7e09d03
PT
1137}
1138
1139/**
1140 * save pre-versions of objects into request for replay.
1141 * Versions are obtained from server reply.
1142 * used for VBR.
1143 */
1144static void ptlrpc_save_versions(struct ptlrpc_request *req)
1145{
1146 struct lustre_msg *repmsg = req->rq_repmsg;
1147 struct lustre_msg *reqmsg = req->rq_reqmsg;
1148 __u64 *versions = lustre_msg_get_versions(repmsg);
d7e09d03
PT
1149
1150 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1151 return;
1152
1153 LASSERT(versions);
1154 lustre_msg_set_versions(reqmsg, versions);
1155 CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
1156 versions[0], versions[1]);
d7e09d03
PT
1157}
1158
1159/**
1160 * Callback function called when client receives RPC reply for \a req.
1161 * Returns 0 on success or error code.
930cef9a 1162 * The return value would be assigned to req->rq_status by the caller
d7e09d03
PT
1163 * as request processing status.
1164 * This function also decides if the request needs to be saved for later replay.
1165 */
1166static int after_reply(struct ptlrpc_request *req)
1167{
1168 struct obd_import *imp = req->rq_import;
1169 struct obd_device *obd = req->rq_import->imp_obd;
1170 int rc;
1171 struct timeval work_start;
1172 long timediff;
d7e09d03
PT
1173
1174 LASSERT(obd != NULL);
1175 /* repbuf must be unlinked */
1176 LASSERT(!req->rq_receiving_reply && !req->rq_must_unlink);
1177
1178 if (req->rq_reply_truncate) {
1179 if (ptlrpc_no_resend(req)) {
1180 DEBUG_REQ(D_ERROR, req, "reply buffer overflow,"
1181 " expected: %d, actual size: %d",
1182 req->rq_nob_received, req->rq_repbuf_len);
0a3bdb00 1183 return -EOVERFLOW;
d7e09d03
PT
1184 }
1185
1186 sptlrpc_cli_free_repbuf(req);
1187 /* Pass the required reply buffer size (include
1188 * space for early reply).
1189 * NB: no need to roundup because alloc_repbuf
1190 * will roundup it */
1191 req->rq_replen = req->rq_nob_received;
1192 req->rq_nob_received = 0;
1193 req->rq_resend = 1;
0a3bdb00 1194 return 0;
d7e09d03
PT
1195 }
1196
1197 /*
1198 * NB Until this point, the whole of the incoming message,
1199 * including buflens, status etc is in the sender's byte order.
1200 */
1201 rc = sptlrpc_cli_unwrap_reply(req);
1202 if (rc) {
1203 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
0a3bdb00 1204 return rc;
d7e09d03
PT
1205 }
1206
1207 /*
1208 * Security layer unwrap might ask resend this request.
1209 */
1210 if (req->rq_resend)
0a3bdb00 1211 return 0;
d7e09d03
PT
1212
1213 rc = unpack_reply(req);
1214 if (rc)
0a3bdb00 1215 return rc;
d7e09d03
PT
1216
1217 /* retry indefinitely on EINPROGRESS */
1218 if (lustre_msg_get_status(req->rq_repmsg) == -EINPROGRESS &&
1219 ptlrpc_no_resend(req) == 0 && !req->rq_no_retry_einprogress) {
1220 time_t now = cfs_time_current_sec();
1221
1222 DEBUG_REQ(D_RPCTRACE, req, "Resending request on EINPROGRESS");
1223 req->rq_resend = 1;
1224 req->rq_nr_resend++;
1225
1226 /* allocate new xid to avoid reply reconstruction */
1227 if (!req->rq_bulk) {
1228 /* new xid is already allocated for bulk in
1229 * ptlrpc_check_set() */
1230 req->rq_xid = ptlrpc_next_xid();
1231 DEBUG_REQ(D_RPCTRACE, req, "Allocating new xid for "
1232 "resend on EINPROGRESS");
1233 }
1234
1235 /* Readjust the timeout for current conditions */
1236 ptlrpc_at_set_req_timeout(req);
1237 /* delay resend to give a chance to the server to get ready.
1238 * The delay is increased by 1s on every resend and is capped to
1239 * the current request timeout (i.e. obd_timeout if AT is off,
1240 * or AT service time x 125% + 5s, see at_est2timeout) */
1241 if (req->rq_nr_resend > req->rq_timeout)
1242 req->rq_sent = now + req->rq_timeout;
1243 else
1244 req->rq_sent = now + req->rq_nr_resend;
1245
0a3bdb00 1246 return 0;
d7e09d03
PT
1247 }
1248
1249 do_gettimeofday(&work_start);
1250 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1251 if (obd->obd_svc_stats != NULL) {
1252 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1253 timediff);
1254 ptlrpc_lprocfs_rpc_sent(req, timediff);
1255 }
1256
1257 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1258 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1259 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1260 lustre_msg_get_type(req->rq_repmsg));
0a3bdb00 1261 return -EPROTO;
d7e09d03
PT
1262 }
1263
1264 if (lustre_msg_get_opc(req->rq_reqmsg) != OBD_PING)
1265 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, cfs_fail_val);
1266 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1267 ptlrpc_at_adj_net_latency(req,
1268 lustre_msg_get_service_time(req->rq_repmsg));
1269
1270 rc = ptlrpc_check_status(req);
1271 imp->imp_connect_error = rc;
1272
1273 if (rc) {
1274 /*
1275 * Either we've been evicted, or the server has failed for
1276 * some reason. Try to reconnect, and if that fails, punt to
1277 * the upcall.
1278 */
1279 if (ll_rpc_recoverable_error(rc)) {
1280 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1281 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
0a3bdb00 1282 return rc;
d7e09d03
PT
1283 }
1284 ptlrpc_request_handle_notconn(req);
0a3bdb00 1285 return rc;
d7e09d03
PT
1286 }
1287 } else {
1288 /*
1289 * Let's look if server sent slv. Do it only for RPC with
1290 * rc == 0.
1291 */
1292 ldlm_cli_update_pool(req);
1293 }
1294
1295 /*
1296 * Store transno in reqmsg for replay.
1297 */
1298 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1299 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1300 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1301 }
1302
1303 if (imp->imp_replayable) {
1304 spin_lock(&imp->imp_lock);
1305 /*
1306 * No point in adding already-committed requests to the replay
1307 * list, we will just remove them immediately. b=9829
1308 */
1309 if (req->rq_transno != 0 &&
1310 (req->rq_transno >
1311 lustre_msg_get_last_committed(req->rq_repmsg) ||
1312 req->rq_replay)) {
1313 /** version recovery */
1314 ptlrpc_save_versions(req);
1315 ptlrpc_retain_replayable_request(req, imp);
1316 } else if (req->rq_commit_cb != NULL) {
1317 spin_unlock(&imp->imp_lock);
1318 req->rq_commit_cb(req);
1319 spin_lock(&imp->imp_lock);
1320 }
1321
1322 /*
1323 * Replay-enabled imports return commit-status information.
1324 */
1325 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
1326 imp->imp_peer_committed_transno =
1327 lustre_msg_get_last_committed(req->rq_repmsg);
1328 }
1329
1330 ptlrpc_free_committed(imp);
1331
1332 if (!list_empty(&imp->imp_replay_list)) {
1333 struct ptlrpc_request *last;
1334
1335 last = list_entry(imp->imp_replay_list.prev,
1336 struct ptlrpc_request,
1337 rq_replay_list);
1338 /*
1339 * Requests with rq_replay stay on the list even if no
1340 * commit is expected.
1341 */
1342 if (last->rq_transno > imp->imp_peer_committed_transno)
1343 ptlrpc_pinger_commit_expected(imp);
1344 }
1345
1346 spin_unlock(&imp->imp_lock);
1347 }
1348
0a3bdb00 1349 return rc;
d7e09d03
PT
1350}
1351
1352/**
1353 * Helper function to send request \a req over the network for the first time
1354 * Also adjusts request phase.
1355 * Returns 0 on success or error code.
1356 */
1357static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1358{
1359 struct obd_import *imp = req->rq_import;
1360 int rc;
d7e09d03
PT
1361
1362 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1363 if (req->rq_sent && (req->rq_sent > cfs_time_current_sec()) &&
1364 (!req->rq_generation_set ||
1365 req->rq_import_generation == imp->imp_generation))
0a3bdb00 1366 return 0;
d7e09d03
PT
1367
1368 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1369
1370 spin_lock(&imp->imp_lock);
1371
1372 if (!req->rq_generation_set)
1373 req->rq_import_generation = imp->imp_generation;
1374
1375 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1376 spin_lock(&req->rq_lock);
1377 req->rq_waiting = 1;
1378 spin_unlock(&req->rq_lock);
1379
1380 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1381 "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1382 ptlrpc_import_state_name(req->rq_send_state),
1383 ptlrpc_import_state_name(imp->imp_state));
1384 LASSERT(list_empty(&req->rq_list));
1385 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1386 atomic_inc(&req->rq_import->imp_inflight);
1387 spin_unlock(&imp->imp_lock);
0a3bdb00 1388 return 0;
d7e09d03
PT
1389 }
1390
1391 if (rc != 0) {
1392 spin_unlock(&imp->imp_lock);
1393 req->rq_status = rc;
1394 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
0a3bdb00 1395 return rc;
d7e09d03
PT
1396 }
1397
1398 LASSERT(list_empty(&req->rq_list));
1399 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1400 atomic_inc(&req->rq_import->imp_inflight);
1401 spin_unlock(&imp->imp_lock);
1402
1403 lustre_msg_set_status(req->rq_reqmsg, current_pid());
1404
1405 rc = sptlrpc_req_refresh_ctx(req, -1);
1406 if (rc) {
1407 if (req->rq_err) {
1408 req->rq_status = rc;
0a3bdb00 1409 return 1;
d7e09d03
PT
1410 } else {
1411 req->rq_wait_ctx = 1;
0a3bdb00 1412 return 0;
d7e09d03
PT
1413 }
1414 }
1415
1416 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1417 " %s:%s:%d:"LPU64":%s:%d\n", current_comm(),
1418 imp->imp_obd->obd_uuid.uuid,
1419 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1420 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1421 lustre_msg_get_opc(req->rq_reqmsg));
1422
1423 rc = ptl_send_rpc(req, 0);
1424 if (rc) {
1425 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1426 req->rq_net_err = 1;
0a3bdb00 1427 return rc;
d7e09d03 1428 }
0a3bdb00 1429 return 0;
d7e09d03
PT
1430}
1431
1432static inline int ptlrpc_set_producer(struct ptlrpc_request_set *set)
1433{
1434 int remaining, rc;
d7e09d03
PT
1435
1436 LASSERT(set->set_producer != NULL);
1437
1438 remaining = atomic_read(&set->set_remaining);
1439
1440 /* populate the ->set_requests list with requests until we
1441 * reach the maximum number of RPCs in flight for this set */
1442 while (atomic_read(&set->set_remaining) < set->set_max_inflight) {
1443 rc = set->set_producer(set, set->set_producer_arg);
1444 if (rc == -ENOENT) {
1445 /* no more RPC to produce */
1446 set->set_producer = NULL;
1447 set->set_producer_arg = NULL;
0a3bdb00 1448 return 0;
d7e09d03
PT
1449 }
1450 }
1451
0a3bdb00 1452 return (atomic_read(&set->set_remaining) - remaining);
d7e09d03
PT
1453}
1454
1455/**
1456 * this sends any unsent RPCs in \a set and returns 1 if all are sent
1457 * and no more replies are expected.
1458 * (it is possible to get less replies than requests sent e.g. due to timed out
1459 * requests or requests that we had trouble to send out)
1460 */
1461int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set)
1462{
1463 struct list_head *tmp, *next;
1464 int force_timer_recalc = 0;
d7e09d03
PT
1465
1466 if (atomic_read(&set->set_remaining) == 0)
0a3bdb00 1467 return 1;
d7e09d03
PT
1468
1469 list_for_each_safe(tmp, next, &set->set_requests) {
1470 struct ptlrpc_request *req =
1471 list_entry(tmp, struct ptlrpc_request,
1472 rq_set_chain);
1473 struct obd_import *imp = req->rq_import;
1474 int unregistered = 0;
1475 int rc = 0;
1476
1477 if (req->rq_phase == RQ_PHASE_NEW &&
1478 ptlrpc_send_new_req(req)) {
1479 force_timer_recalc = 1;
1480 }
1481
1482 /* delayed send - skip */
1483 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1484 continue;
1485
1486 /* delayed resend - skip */
1487 if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend &&
1488 req->rq_sent > cfs_time_current_sec())
1489 continue;
1490
1491 if (!(req->rq_phase == RQ_PHASE_RPC ||
1492 req->rq_phase == RQ_PHASE_BULK ||
1493 req->rq_phase == RQ_PHASE_INTERPRET ||
1494 req->rq_phase == RQ_PHASE_UNREGISTERING ||
1495 req->rq_phase == RQ_PHASE_COMPLETE)) {
1496 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1497 LBUG();
1498 }
1499
1500 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1501 LASSERT(req->rq_next_phase != req->rq_phase);
1502 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1503
1504 /*
1505 * Skip processing until reply is unlinked. We
1506 * can't return to pool before that and we can't
1507 * call interpret before that. We need to make
1508 * sure that all rdma transfers finished and will
1509 * not corrupt any data.
1510 */
1511 if (ptlrpc_client_recv_or_unlink(req) ||
1512 ptlrpc_client_bulk_active(req))
1513 continue;
1514
1515 /*
1516 * Turn fail_loc off to prevent it from looping
1517 * forever.
1518 */
1519 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1520 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK,
1521 OBD_FAIL_ONCE);
1522 }
1523 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1524 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK,
1525 OBD_FAIL_ONCE);
1526 }
1527
1528 /*
1529 * Move to next phase if reply was successfully
1530 * unlinked.
1531 */
1532 ptlrpc_rqphase_move(req, req->rq_next_phase);
1533 }
1534
1535 if (req->rq_phase == RQ_PHASE_COMPLETE)
1536 continue;
1537
1538 if (req->rq_phase == RQ_PHASE_INTERPRET)
1539 GOTO(interpret, req->rq_status);
1540
1541 /*
1542 * Note that this also will start async reply unlink.
1543 */
1544 if (req->rq_net_err && !req->rq_timedout) {
1545 ptlrpc_expire_one_request(req, 1);
1546
1547 /*
1548 * Check if we still need to wait for unlink.
1549 */
1550 if (ptlrpc_client_recv_or_unlink(req) ||
1551 ptlrpc_client_bulk_active(req))
1552 continue;
1553 /* If there is no need to resend, fail it now. */
1554 if (req->rq_no_resend) {
1555 if (req->rq_status == 0)
1556 req->rq_status = -EIO;
1557 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1558 GOTO(interpret, req->rq_status);
1559 } else {
1560 continue;
1561 }
1562 }
1563
1564 if (req->rq_err) {
1565 spin_lock(&req->rq_lock);
1566 req->rq_replied = 0;
1567 spin_unlock(&req->rq_lock);
1568 if (req->rq_status == 0)
1569 req->rq_status = -EIO;
1570 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1571 GOTO(interpret, req->rq_status);
1572 }
1573
1574 /* ptlrpc_set_wait->l_wait_event sets lwi_allow_intr
1575 * so it sets rq_intr regardless of individual rpc
1576 * timeouts. The synchronous IO waiting path sets
1577 * rq_intr irrespective of whether ptlrpcd
1578 * has seen a timeout. Our policy is to only interpret
1579 * interrupted rpcs after they have timed out, so we
1580 * need to enforce that here.
1581 */
1582
1583 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1584 req->rq_wait_ctx)) {
1585 req->rq_status = -EINTR;
1586 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1587 GOTO(interpret, req->rq_status);
1588 }
1589
1590 if (req->rq_phase == RQ_PHASE_RPC) {
1591 if (req->rq_timedout || req->rq_resend ||
1592 req->rq_waiting || req->rq_wait_ctx) {
1593 int status;
1594
1595 if (!ptlrpc_unregister_reply(req, 1))
1596 continue;
1597
1598 spin_lock(&imp->imp_lock);
cb68dd2d
KM
1599 if (ptlrpc_import_delay_req(imp, req,
1600 &status)) {
d7e09d03
PT
1601 /* put on delay list - only if we wait
1602 * recovery finished - before send */
1603 list_del_init(&req->rq_list);
1604 list_add_tail(&req->rq_list,
1605 &imp->
1606 imp_delayed_list);
1607 spin_unlock(&imp->imp_lock);
1608 continue;
1609 }
1610
1611 if (status != 0) {
1612 req->rq_status = status;
1613 ptlrpc_rqphase_move(req,
1614 RQ_PHASE_INTERPRET);
1615 spin_unlock(&imp->imp_lock);
1616 GOTO(interpret, req->rq_status);
1617 }
1618 if (ptlrpc_no_resend(req) &&
1619 !req->rq_wait_ctx) {
1620 req->rq_status = -ENOTCONN;
1621 ptlrpc_rqphase_move(req,
1622 RQ_PHASE_INTERPRET);
1623 spin_unlock(&imp->imp_lock);
1624 GOTO(interpret, req->rq_status);
1625 }
1626
1627 list_del_init(&req->rq_list);
1628 list_add_tail(&req->rq_list,
1629 &imp->imp_sending_list);
1630
1631 spin_unlock(&imp->imp_lock);
1632
1633 spin_lock(&req->rq_lock);
1634 req->rq_waiting = 0;
1635 spin_unlock(&req->rq_lock);
1636
1637 if (req->rq_timedout || req->rq_resend) {
1638 /* This is re-sending anyways,
1639 * let's mark req as resend. */
1640 spin_lock(&req->rq_lock);
1641 req->rq_resend = 1;
1642 spin_unlock(&req->rq_lock);
1643 if (req->rq_bulk) {
1644 __u64 old_xid;
1645
1646 if (!ptlrpc_unregister_bulk(req, 1))
1647 continue;
1648
1649 /* ensure previous bulk fails */
1650 old_xid = req->rq_xid;
1651 req->rq_xid = ptlrpc_next_xid();
1652 CDEBUG(D_HA, "resend bulk "
1653 "old x"LPU64
1654 " new x"LPU64"\n",
1655 old_xid, req->rq_xid);
1656 }
1657 }
1658 /*
1659 * rq_wait_ctx is only touched by ptlrpcd,
1660 * so no lock is needed here.
1661 */
1662 status = sptlrpc_req_refresh_ctx(req, -1);
1663 if (status) {
1664 if (req->rq_err) {
1665 req->rq_status = status;
1666 spin_lock(&req->rq_lock);
1667 req->rq_wait_ctx = 0;
1668 spin_unlock(&req->rq_lock);
1669 force_timer_recalc = 1;
1670 } else {
1671 spin_lock(&req->rq_lock);
1672 req->rq_wait_ctx = 1;
1673 spin_unlock(&req->rq_lock);
1674 }
1675
1676 continue;
1677 } else {
1678 spin_lock(&req->rq_lock);
1679 req->rq_wait_ctx = 0;
1680 spin_unlock(&req->rq_lock);
1681 }
1682
1683 rc = ptl_send_rpc(req, 0);
1684 if (rc) {
1685 DEBUG_REQ(D_HA, req,
1686 "send failed: rc = %d", rc);
1687 force_timer_recalc = 1;
1688 spin_lock(&req->rq_lock);
1689 req->rq_net_err = 1;
1690 spin_unlock(&req->rq_lock);
1691 }
1692 /* need to reset the timeout */
1693 force_timer_recalc = 1;
1694 }
1695
1696 spin_lock(&req->rq_lock);
1697
1698 if (ptlrpc_client_early(req)) {
1699 ptlrpc_at_recv_early_reply(req);
1700 spin_unlock(&req->rq_lock);
1701 continue;
1702 }
1703
1704 /* Still waiting for a reply? */
1705 if (ptlrpc_client_recv(req)) {
1706 spin_unlock(&req->rq_lock);
1707 continue;
1708 }
1709
1710 /* Did we actually receive a reply? */
1711 if (!ptlrpc_client_replied(req)) {
1712 spin_unlock(&req->rq_lock);
1713 continue;
1714 }
1715
1716 spin_unlock(&req->rq_lock);
1717
1718 /* unlink from net because we are going to
1719 * swab in-place of reply buffer */
1720 unregistered = ptlrpc_unregister_reply(req, 1);
1721 if (!unregistered)
1722 continue;
1723
1724 req->rq_status = after_reply(req);
1725 if (req->rq_resend)
1726 continue;
1727
1728 /* If there is no bulk associated with this request,
1729 * then we're done and should let the interpreter
1730 * process the reply. Similarly if the RPC returned
1731 * an error, and therefore the bulk will never arrive.
1732 */
1733 if (req->rq_bulk == NULL || req->rq_status < 0) {
1734 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1735 GOTO(interpret, req->rq_status);
1736 }
1737
1738 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1739 }
1740
1741 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1742 if (ptlrpc_client_bulk_active(req))
1743 continue;
1744
1745 if (req->rq_bulk->bd_failure) {
1746 /* The RPC reply arrived OK, but the bulk screwed
1747 * up! Dead weird since the server told us the RPC
1748 * was good after getting the REPLY for her GET or
1749 * the ACK for her PUT. */
1750 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1751 req->rq_status = -EIO;
1752 }
1753
1754 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1755
7f1d15a8 1756interpret:
d7e09d03
PT
1757 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1758
1759 /* This moves to "unregistering" phase we need to wait for
1760 * reply unlink. */
1761 if (!unregistered && !ptlrpc_unregister_reply(req, 1)) {
1762 /* start async bulk unlink too */
1763 ptlrpc_unregister_bulk(req, 1);
1764 continue;
1765 }
1766
1767 if (!ptlrpc_unregister_bulk(req, 1))
1768 continue;
1769
1770 /* When calling interpret receiving already should be
1771 * finished. */
1772 LASSERT(!req->rq_receiving_reply);
1773
1774 ptlrpc_req_interpret(env, req, req->rq_status);
1775
1776 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1777
1778 CDEBUG(req->rq_reqmsg != NULL ? D_RPCTRACE : 0,
1779 "Completed RPC pname:cluuid:pid:xid:nid:"
1780 "opc %s:%s:%d:"LPU64":%s:%d\n",
1781 current_comm(), imp->imp_obd->obd_uuid.uuid,
1782 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1783 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1784 lustre_msg_get_opc(req->rq_reqmsg));
1785
1786 spin_lock(&imp->imp_lock);
1787 /* Request already may be not on sending or delaying list. This
1788 * may happen in the case of marking it erroneous for the case
1789 * ptlrpc_import_delay_req(req, status) find it impossible to
1790 * allow sending this rpc and returns *status != 0. */
1791 if (!list_empty(&req->rq_list)) {
1792 list_del_init(&req->rq_list);
1793 atomic_dec(&imp->imp_inflight);
1794 }
1795 spin_unlock(&imp->imp_lock);
1796
1797 atomic_dec(&set->set_remaining);
1798 wake_up_all(&imp->imp_recovery_waitq);
1799
1800 if (set->set_producer) {
1801 /* produce a new request if possible */
1802 if (ptlrpc_set_producer(set) > 0)
1803 force_timer_recalc = 1;
1804
1805 /* free the request that has just been completed
1806 * in order not to pollute set->set_requests */
1807 list_del_init(&req->rq_set_chain);
1808 spin_lock(&req->rq_lock);
1809 req->rq_set = NULL;
1810 req->rq_invalid_rqset = 0;
1811 spin_unlock(&req->rq_lock);
1812
1813 /* record rq_status to compute the final status later */
1814 if (req->rq_status != 0)
1815 set->set_rc = req->rq_status;
1816 ptlrpc_req_finished(req);
1817 }
1818 }
1819
1820 /* If we hit an error, we want to recover promptly. */
0a3bdb00 1821 return atomic_read(&set->set_remaining) == 0 || force_timer_recalc;
d7e09d03
PT
1822}
1823EXPORT_SYMBOL(ptlrpc_check_set);
1824
1825/**
1826 * Time out request \a req. is \a async_unlink is set, that means do not wait
1827 * until LNet actually confirms network buffer unlinking.
1828 * Return 1 if we should give up further retrying attempts or 0 otherwise.
1829 */
1830int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1831{
1832 struct obd_import *imp = req->rq_import;
1833 int rc = 0;
d7e09d03
PT
1834
1835 spin_lock(&req->rq_lock);
1836 req->rq_timedout = 1;
1837 spin_unlock(&req->rq_lock);
1838
1839 DEBUG_REQ(D_WARNING, req, "Request sent has %s: [sent "CFS_DURATION_T
1840 "/real "CFS_DURATION_T"]",
1841 req->rq_net_err ? "failed due to network error" :
1842 ((req->rq_real_sent == 0 ||
1843 cfs_time_before(req->rq_real_sent, req->rq_sent) ||
1844 cfs_time_aftereq(req->rq_real_sent, req->rq_deadline)) ?
1845 "timed out for sent delay" : "timed out for slow reply"),
1846 req->rq_sent, req->rq_real_sent);
1847
1848 if (imp != NULL && obd_debug_peer_on_timeout)
1849 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1850
1851 ptlrpc_unregister_reply(req, async_unlink);
1852 ptlrpc_unregister_bulk(req, async_unlink);
1853
1854 if (obd_dump_on_timeout)
1855 libcfs_debug_dumplog();
1856
1857 if (imp == NULL) {
1858 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
0a3bdb00 1859 return 1;
d7e09d03
PT
1860 }
1861
1862 atomic_inc(&imp->imp_timeouts);
1863
1864 /* The DLM server doesn't want recovery run on its imports. */
1865 if (imp->imp_dlm_fake)
0a3bdb00 1866 return 1;
d7e09d03
PT
1867
1868 /* If this request is for recovery or other primordial tasks,
1869 * then error it out here. */
1870 if (req->rq_ctx_init || req->rq_ctx_fini ||
1871 req->rq_send_state != LUSTRE_IMP_FULL ||
1872 imp->imp_obd->obd_no_recov) {
1873 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1874 ptlrpc_import_state_name(req->rq_send_state),
1875 ptlrpc_import_state_name(imp->imp_state));
1876 spin_lock(&req->rq_lock);
1877 req->rq_status = -ETIMEDOUT;
1878 req->rq_err = 1;
1879 spin_unlock(&req->rq_lock);
0a3bdb00 1880 return 1;
d7e09d03
PT
1881 }
1882
1883 /* if a request can't be resent we can't wait for an answer after
1884 the timeout */
1885 if (ptlrpc_no_resend(req)) {
1886 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1887 rc = 1;
1888 }
1889
1890 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1891
0a3bdb00 1892 return rc;
d7e09d03
PT
1893}
1894
1895/**
1896 * Time out all uncompleted requests in request set pointed by \a data
1897 * Callback used when waiting on sets with l_wait_event.
1898 * Always returns 1.
1899 */
1900int ptlrpc_expired_set(void *data)
1901{
1902 struct ptlrpc_request_set *set = data;
1903 struct list_head *tmp;
1904 time_t now = cfs_time_current_sec();
d7e09d03
PT
1905
1906 LASSERT(set != NULL);
1907
1908 /*
1909 * A timeout expired. See which reqs it applies to...
1910 */
3949015e 1911 list_for_each(tmp, &set->set_requests) {
d7e09d03
PT
1912 struct ptlrpc_request *req =
1913 list_entry(tmp, struct ptlrpc_request,
1914 rq_set_chain);
1915
1916 /* don't expire request waiting for context */
1917 if (req->rq_wait_ctx)
1918 continue;
1919
1920 /* Request in-flight? */
1921 if (!((req->rq_phase == RQ_PHASE_RPC &&
1922 !req->rq_waiting && !req->rq_resend) ||
1923 (req->rq_phase == RQ_PHASE_BULK)))
1924 continue;
1925
1926 if (req->rq_timedout || /* already dealt with */
1927 req->rq_deadline > now) /* not expired */
1928 continue;
1929
1930 /* Deal with this guy. Do it asynchronously to not block
1931 * ptlrpcd thread. */
1932 ptlrpc_expire_one_request(req, 1);
1933 }
1934
1935 /*
1936 * When waiting for a whole set, we always break out of the
1937 * sleep so we can recalculate the timeout, or enable interrupts
1938 * if everyone's timed out.
1939 */
0a3bdb00 1940 return 1;
d7e09d03
PT
1941}
1942EXPORT_SYMBOL(ptlrpc_expired_set);
1943
1944/**
1945 * Sets rq_intr flag in \a req under spinlock.
1946 */
1947void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1948{
1949 spin_lock(&req->rq_lock);
1950 req->rq_intr = 1;
1951 spin_unlock(&req->rq_lock);
1952}
1953EXPORT_SYMBOL(ptlrpc_mark_interrupted);
1954
1955/**
1956 * Interrupts (sets interrupted flag) all uncompleted requests in
1957 * a set \a data. Callback for l_wait_event for interruptible waits.
1958 */
1959void ptlrpc_interrupted_set(void *data)
1960{
1961 struct ptlrpc_request_set *set = data;
1962 struct list_head *tmp;
1963
1964 LASSERT(set != NULL);
1965 CDEBUG(D_RPCTRACE, "INTERRUPTED SET %p\n", set);
1966
1967 list_for_each(tmp, &set->set_requests) {
1968 struct ptlrpc_request *req =
1969 list_entry(tmp, struct ptlrpc_request,
1970 rq_set_chain);
1971
1972 if (req->rq_phase != RQ_PHASE_RPC &&
1973 req->rq_phase != RQ_PHASE_UNREGISTERING)
1974 continue;
1975
1976 ptlrpc_mark_interrupted(req);
1977 }
1978}
1979EXPORT_SYMBOL(ptlrpc_interrupted_set);
1980
1981/**
1982 * Get the smallest timeout in the set; this does NOT set a timeout.
1983 */
1984int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1985{
1986 struct list_head *tmp;
1987 time_t now = cfs_time_current_sec();
1988 int timeout = 0;
1989 struct ptlrpc_request *req;
1990 int deadline;
d7e09d03
PT
1991
1992 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1993
1994 list_for_each(tmp, &set->set_requests) {
1995 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1996
1997 /*
1998 * Request in-flight?
1999 */
2000 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
2001 (req->rq_phase == RQ_PHASE_BULK) ||
2002 (req->rq_phase == RQ_PHASE_NEW)))
2003 continue;
2004
2005 /*
2006 * Already timed out.
2007 */
2008 if (req->rq_timedout)
2009 continue;
2010
2011 /*
2012 * Waiting for ctx.
2013 */
2014 if (req->rq_wait_ctx)
2015 continue;
2016
2017 if (req->rq_phase == RQ_PHASE_NEW)
2018 deadline = req->rq_sent;
2019 else if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend)
2020 deadline = req->rq_sent;
2021 else
2022 deadline = req->rq_sent + req->rq_timeout;
2023
2024 if (deadline <= now) /* actually expired already */
2025 timeout = 1; /* ASAP */
2026 else if (timeout == 0 || timeout > deadline - now)
2027 timeout = deadline - now;
2028 }
0a3bdb00 2029 return timeout;
d7e09d03
PT
2030}
2031EXPORT_SYMBOL(ptlrpc_set_next_timeout);
2032
2033/**
930cef9a 2034 * Send all unset request from the set and then wait until all
d7e09d03
PT
2035 * requests in the set complete (either get a reply, timeout, get an
2036 * error or otherwise be interrupted).
2037 * Returns 0 on success or error code otherwise.
2038 */
2039int ptlrpc_set_wait(struct ptlrpc_request_set *set)
2040{
2041 struct list_head *tmp;
2042 struct ptlrpc_request *req;
2043 struct l_wait_info lwi;
2044 int rc, timeout;
d7e09d03
PT
2045
2046 if (set->set_producer)
2047 (void)ptlrpc_set_producer(set);
2048 else
2049 list_for_each(tmp, &set->set_requests) {
2050 req = list_entry(tmp, struct ptlrpc_request,
2051 rq_set_chain);
2052 if (req->rq_phase == RQ_PHASE_NEW)
2053 (void)ptlrpc_send_new_req(req);
2054 }
2055
2056 if (list_empty(&set->set_requests))
0a3bdb00 2057 return 0;
d7e09d03
PT
2058
2059 do {
2060 timeout = ptlrpc_set_next_timeout(set);
2061
2062 /* wait until all complete, interrupted, or an in-flight
2063 * req times out */
2064 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
2065 set, timeout);
2066
2067 if (timeout == 0 && !cfs_signal_pending())
2068 /*
2069 * No requests are in-flight (ether timed out
2070 * or delayed), so we can allow interrupts.
2071 * We still want to block for a limited time,
2072 * so we allow interrupts during the timeout.
2073 */
2074 lwi = LWI_TIMEOUT_INTR_ALL(cfs_time_seconds(1),
2075 ptlrpc_expired_set,
2076 ptlrpc_interrupted_set, set);
2077 else
2078 /*
2079 * At least one request is in flight, so no
2080 * interrupts are allowed. Wait until all
2081 * complete, or an in-flight req times out.
2082 */
2083 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout? timeout : 1),
2084 ptlrpc_expired_set, set);
2085
2086 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(NULL, set), &lwi);
2087
2088 /* LU-769 - if we ignored the signal because it was already
2089 * pending when we started, we need to handle it now or we risk
2090 * it being ignored forever */
2091 if (rc == -ETIMEDOUT && !lwi.lwi_allow_intr &&
2092 cfs_signal_pending()) {
2093 sigset_t blocked_sigs =
2094 cfs_block_sigsinv(LUSTRE_FATAL_SIGS);
2095
2096 /* In fact we only interrupt for the "fatal" signals
2097 * like SIGINT or SIGKILL. We still ignore less
2098 * important signals since ptlrpc set is not easily
2099 * reentrant from userspace again */
2100 if (cfs_signal_pending())
2101 ptlrpc_interrupted_set(set);
2102 cfs_restore_sigs(blocked_sigs);
2103 }
2104
2105 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
2106
2107 /* -EINTR => all requests have been flagged rq_intr so next
2108 * check completes.
2109 * -ETIMEDOUT => someone timed out. When all reqs have
2110 * timed out, signals are enabled allowing completion with
2111 * EINTR.
2112 * I don't really care if we go once more round the loop in
2113 * the error cases -eeb. */
2114 if (rc == 0 && atomic_read(&set->set_remaining) == 0) {
2115 list_for_each(tmp, &set->set_requests) {
2116 req = list_entry(tmp, struct ptlrpc_request,
2117 rq_set_chain);
2118 spin_lock(&req->rq_lock);
2119 req->rq_invalid_rqset = 1;
2120 spin_unlock(&req->rq_lock);
2121 }
2122 }
2123 } while (rc != 0 || atomic_read(&set->set_remaining) != 0);
2124
2125 LASSERT(atomic_read(&set->set_remaining) == 0);
2126
2127 rc = set->set_rc; /* rq_status of already freed requests if any */
2128 list_for_each(tmp, &set->set_requests) {
2129 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2130
2131 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
2132 if (req->rq_status != 0)
2133 rc = req->rq_status;
2134 }
2135
2136 if (set->set_interpret != NULL) {
2137 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
2138 set->set_interpret;
2139 rc = interpreter (set, set->set_arg, rc);
2140 } else {
2141 struct ptlrpc_set_cbdata *cbdata, *n;
2142 int err;
2143
2144 list_for_each_entry_safe(cbdata, n,
2145 &set->set_cblist, psc_item) {
2146 list_del_init(&cbdata->psc_item);
2147 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
2148 if (err && !rc)
2149 rc = err;
2150 OBD_FREE_PTR(cbdata);
2151 }
2152 }
2153
0a3bdb00 2154 return rc;
d7e09d03
PT
2155}
2156EXPORT_SYMBOL(ptlrpc_set_wait);
2157
2158/**
930cef9a 2159 * Helper function for request freeing.
d7e09d03
PT
2160 * Called when request count reached zero and request needs to be freed.
2161 * Removes request from all sorts of sending/replay lists it might be on,
2162 * frees network buffers if any are present.
2163 * If \a locked is set, that means caller is already holding import imp_lock
2164 * and so we no longer need to reobtain it (for certain lists manipulations)
2165 */
2166static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
2167{
d7e09d03 2168 if (request == NULL) {
d7e09d03
PT
2169 return;
2170 }
2171
2172 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
2173 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
2174 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
2175 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
2176 LASSERTF(list_empty(&request->rq_exp_list), "req %p\n", request);
2177 LASSERTF(!request->rq_replay, "req %p\n", request);
2178
2179 req_capsule_fini(&request->rq_pill);
2180
2181 /* We must take it off the imp_replay_list first. Otherwise, we'll set
2182 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
2183 if (request->rq_import != NULL) {
2184 if (!locked)
2185 spin_lock(&request->rq_import->imp_lock);
2186 list_del_init(&request->rq_replay_list);
2187 if (!locked)
2188 spin_unlock(&request->rq_import->imp_lock);
2189 }
2190 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
2191
2192 if (atomic_read(&request->rq_refcount) != 0) {
2193 DEBUG_REQ(D_ERROR, request,
2194 "freeing request with nonzero refcount");
2195 LBUG();
2196 }
2197
2198 if (request->rq_repbuf != NULL)
2199 sptlrpc_cli_free_repbuf(request);
2200 if (request->rq_export != NULL) {
2201 class_export_put(request->rq_export);
2202 request->rq_export = NULL;
2203 }
2204 if (request->rq_import != NULL) {
2205 class_import_put(request->rq_import);
2206 request->rq_import = NULL;
2207 }
2208 if (request->rq_bulk != NULL)
2209 ptlrpc_free_bulk_pin(request->rq_bulk);
2210
2211 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
2212 sptlrpc_cli_free_reqbuf(request);
2213
2214 if (request->rq_cli_ctx)
2215 sptlrpc_req_put_ctx(request, !locked);
2216
2217 if (request->rq_pool)
2218 __ptlrpc_free_req_to_pool(request);
2219 else
2220 OBD_FREE(request, sizeof(*request));
d7e09d03
PT
2221}
2222
2223static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
2224/**
2225 * Drop one request reference. Must be called with import imp_lock held.
930cef9a 2226 * When reference count drops to zero, request is freed.
d7e09d03
PT
2227 */
2228void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
2229{
2230 LASSERT(spin_is_locked(&request->rq_import->imp_lock));
2231 (void)__ptlrpc_req_finished(request, 1);
2232}
2233EXPORT_SYMBOL(ptlrpc_req_finished_with_imp_lock);
2234
2235/**
2236 * Helper function
2237 * Drops one reference count for request \a request.
2238 * \a locked set indicates that caller holds import imp_lock.
930cef9a 2239 * Frees the request when reference count reaches zero.
d7e09d03
PT
2240 */
2241static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
2242{
d7e09d03 2243 if (request == NULL)
0a3bdb00 2244 return 1;
d7e09d03
PT
2245
2246 if (request == LP_POISON ||
2247 request->rq_reqmsg == LP_POISON) {
2248 CERROR("dereferencing freed request (bug 575)\n");
2249 LBUG();
0a3bdb00 2250 return 1;
d7e09d03
PT
2251 }
2252
2253 DEBUG_REQ(D_INFO, request, "refcount now %u",
2254 atomic_read(&request->rq_refcount) - 1);
2255
2256 if (atomic_dec_and_test(&request->rq_refcount)) {
2257 __ptlrpc_free_req(request, locked);
0a3bdb00 2258 return 1;
d7e09d03
PT
2259 }
2260
0a3bdb00 2261 return 0;
d7e09d03
PT
2262}
2263
2264/**
2265 * Drops one reference count for a request.
2266 */
2267void ptlrpc_req_finished(struct ptlrpc_request *request)
2268{
2269 __ptlrpc_req_finished(request, 0);
2270}
2271EXPORT_SYMBOL(ptlrpc_req_finished);
2272
2273/**
2274 * Returns xid of a \a request
2275 */
2276__u64 ptlrpc_req_xid(struct ptlrpc_request *request)
2277{
2278 return request->rq_xid;
2279}
2280EXPORT_SYMBOL(ptlrpc_req_xid);
2281
2282/**
2283 * Disengage the client's reply buffer from the network
2284 * NB does _NOT_ unregister any client-side bulk.
2285 * IDEMPOTENT, but _not_ safe against concurrent callers.
2286 * The request owner (i.e. the thread doing the I/O) must call...
2287 * Returns 0 on success or 1 if unregistering cannot be made.
2288 */
2289int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
2290{
2291 int rc;
2292 wait_queue_head_t *wq;
2293 struct l_wait_info lwi;
2294
2295 /*
2296 * Might sleep.
2297 */
2298 LASSERT(!in_interrupt());
2299
2300 /*
2301 * Let's setup deadline for reply unlink.
2302 */
2303 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2304 async && request->rq_reply_deadline == 0)
2305 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
2306
2307 /*
2308 * Nothing left to do.
2309 */
2310 if (!ptlrpc_client_recv_or_unlink(request))
0a3bdb00 2311 return 1;
d7e09d03
PT
2312
2313 LNetMDUnlink(request->rq_reply_md_h);
2314
2315 /*
2316 * Let's check it once again.
2317 */
2318 if (!ptlrpc_client_recv_or_unlink(request))
0a3bdb00 2319 return 1;
d7e09d03
PT
2320
2321 /*
2322 * Move to "Unregistering" phase as reply was not unlinked yet.
2323 */
2324 ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
2325
2326 /*
2327 * Do not wait for unlink to finish.
2328 */
2329 if (async)
0a3bdb00 2330 return 0;
d7e09d03
PT
2331
2332 /*
2333 * We have to l_wait_event() whatever the result, to give liblustre
2334 * a chance to run reply_in_callback(), and to make sure we've
2335 * unlinked before returning a req to the pool.
2336 */
2337 if (request->rq_set != NULL)
2338 wq = &request->rq_set->set_waitq;
2339 else
2340 wq = &request->rq_reply_waitq;
2341
2342 for (;;) {
2343 /* Network access will complete in finite time but the HUGE
2344 * timeout lets us CWARN for visibility of sluggish NALs */
2345 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2346 cfs_time_seconds(1), NULL, NULL);
2347 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
2348 &lwi);
2349 if (rc == 0) {
2350 ptlrpc_rqphase_move(request, request->rq_next_phase);
0a3bdb00 2351 return 1;
d7e09d03
PT
2352 }
2353
2354 LASSERT(rc == -ETIMEDOUT);
2355 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
2356 "rvcng=%d unlnk=%d", request->rq_receiving_reply,
2357 request->rq_must_unlink);
2358 }
0a3bdb00 2359 return 0;
d7e09d03
PT
2360}
2361EXPORT_SYMBOL(ptlrpc_unregister_reply);
2362
2363/**
2364 * Iterates through replay_list on import and prunes
2365 * all requests have transno smaller than last_committed for the
2366 * import and don't have rq_replay set.
930cef9a 2367 * Since requests are sorted in transno order, stops when meeting first
d7e09d03
PT
2368 * transno bigger than last_committed.
2369 * caller must hold imp->imp_lock
2370 */
2371void ptlrpc_free_committed(struct obd_import *imp)
2372{
2373 struct list_head *tmp, *saved;
2374 struct ptlrpc_request *req;
2375 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
d7e09d03
PT
2376
2377 LASSERT(imp != NULL);
2378
2379 LASSERT(spin_is_locked(&imp->imp_lock));
2380
2381
2382 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
2383 imp->imp_generation == imp->imp_last_generation_checked) {
2384 CDEBUG(D_INFO, "%s: skip recheck: last_committed "LPU64"\n",
2385 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
d7e09d03
PT
2386 return;
2387 }
2388 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
2389 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
2390 imp->imp_generation);
2391 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
2392 imp->imp_last_generation_checked = imp->imp_generation;
2393
2394 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
2395 req = list_entry(tmp, struct ptlrpc_request,
2396 rq_replay_list);
2397
2398 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
2399 LASSERT(req != last_req);
2400 last_req = req;
2401
2402 if (req->rq_transno == 0) {
2403 DEBUG_REQ(D_EMERG, req, "zero transno during replay");
2404 LBUG();
2405 }
2406 if (req->rq_import_generation < imp->imp_generation) {
2407 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
2408 GOTO(free_req, 0);
2409 }
2410
2411 if (req->rq_replay) {
2412 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
2413 continue;
2414 }
2415
2416 /* not yet committed */
2417 if (req->rq_transno > imp->imp_peer_committed_transno) {
2418 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
2419 break;
2420 }
2421
2422 DEBUG_REQ(D_INFO, req, "commit (last_committed "LPU64")",
2423 imp->imp_peer_committed_transno);
2424free_req:
2425 spin_lock(&req->rq_lock);
2426 req->rq_replay = 0;
2427 spin_unlock(&req->rq_lock);
2428 if (req->rq_commit_cb != NULL)
2429 req->rq_commit_cb(req);
2430 list_del_init(&req->rq_replay_list);
2431 __ptlrpc_req_finished(req, 1);
2432 }
d7e09d03
PT
2433}
2434
2435void ptlrpc_cleanup_client(struct obd_import *imp)
2436{
d7e09d03
PT
2437}
2438EXPORT_SYMBOL(ptlrpc_cleanup_client);
2439
2440/**
2441 * Schedule previously sent request for resend.
2442 * For bulk requests we assign new xid (to avoid problems with
2443 * lost replies and therefore several transfers landing into same buffer
2444 * from different sending attempts).
2445 */
2446void ptlrpc_resend_req(struct ptlrpc_request *req)
2447{
2448 DEBUG_REQ(D_HA, req, "going to resend");
2449 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
2450 req->rq_status = -EAGAIN;
2451
2452 spin_lock(&req->rq_lock);
2453 req->rq_resend = 1;
2454 req->rq_net_err = 0;
2455 req->rq_timedout = 0;
2456 if (req->rq_bulk) {
2457 __u64 old_xid = req->rq_xid;
2458
2459 /* ensure previous bulk fails */
2460 req->rq_xid = ptlrpc_next_xid();
2461 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
2462 old_xid, req->rq_xid);
2463 }
2464 ptlrpc_client_wake_req(req);
2465 spin_unlock(&req->rq_lock);
2466}
2467EXPORT_SYMBOL(ptlrpc_resend_req);
2468
2469/* XXX: this function and rq_status are currently unused */
2470void ptlrpc_restart_req(struct ptlrpc_request *req)
2471{
2472 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
2473 req->rq_status = -ERESTARTSYS;
2474
2475 spin_lock(&req->rq_lock);
2476 req->rq_restart = 1;
2477 req->rq_timedout = 0;
2478 ptlrpc_client_wake_req(req);
2479 spin_unlock(&req->rq_lock);
2480}
2481EXPORT_SYMBOL(ptlrpc_restart_req);
2482
2483/**
2484 * Grab additional reference on a request \a req
2485 */
2486struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
2487{
d7e09d03 2488 atomic_inc(&req->rq_refcount);
0a3bdb00 2489 return req;
d7e09d03
PT
2490}
2491EXPORT_SYMBOL(ptlrpc_request_addref);
2492
2493/**
2494 * Add a request to import replay_list.
2495 * Must be called under imp_lock
2496 */
2497void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2498 struct obd_import *imp)
2499{
2500 struct list_head *tmp;
2501
2502 LASSERT(spin_is_locked(&imp->imp_lock));
2503
2504 if (req->rq_transno == 0) {
2505 DEBUG_REQ(D_EMERG, req, "saving request with zero transno");
2506 LBUG();
2507 }
2508
2509 /* clear this for new requests that were resent as well
2510 as resent replayed requests. */
2511 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2512
2513 /* don't re-add requests that have been replayed */
2514 if (!list_empty(&req->rq_replay_list))
2515 return;
2516
2517 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
2518
2519 LASSERT(imp->imp_replayable);
2520 /* Balanced in ptlrpc_free_committed, usually. */
2521 ptlrpc_request_addref(req);
2522 list_for_each_prev(tmp, &imp->imp_replay_list) {
2523 struct ptlrpc_request *iter =
2524 list_entry(tmp, struct ptlrpc_request,
2525 rq_replay_list);
2526
2527 /* We may have duplicate transnos if we create and then
2528 * open a file, or for closes retained if to match creating
2529 * opens, so use req->rq_xid as a secondary key.
2530 * (See bugs 684, 685, and 428.)
2531 * XXX no longer needed, but all opens need transnos!
2532 */
2533 if (iter->rq_transno > req->rq_transno)
2534 continue;
2535
2536 if (iter->rq_transno == req->rq_transno) {
2537 LASSERT(iter->rq_xid != req->rq_xid);
2538 if (iter->rq_xid > req->rq_xid)
2539 continue;
2540 }
2541
2542 list_add(&req->rq_replay_list, &iter->rq_replay_list);
2543 return;
2544 }
2545
2546 list_add(&req->rq_replay_list, &imp->imp_replay_list);
2547}
2548EXPORT_SYMBOL(ptlrpc_retain_replayable_request);
2549
2550/**
2551 * Send request and wait until it completes.
2552 * Returns request processing status.
2553 */
2554int ptlrpc_queue_wait(struct ptlrpc_request *req)
2555{
2556 struct ptlrpc_request_set *set;
2557 int rc;
d7e09d03
PT
2558
2559 LASSERT(req->rq_set == NULL);
2560 LASSERT(!req->rq_receiving_reply);
2561
2562 set = ptlrpc_prep_set();
2563 if (set == NULL) {
2564 CERROR("Unable to allocate ptlrpc set.");
0a3bdb00 2565 return -ENOMEM;
d7e09d03
PT
2566 }
2567
2568 /* for distributed debugging */
2569 lustre_msg_set_status(req->rq_reqmsg, current_pid());
2570
2571 /* add a ref for the set (see comment in ptlrpc_set_add_req) */
2572 ptlrpc_request_addref(req);
2573 ptlrpc_set_add_req(set, req);
2574 rc = ptlrpc_set_wait(set);
2575 ptlrpc_set_destroy(set);
2576
0a3bdb00 2577 return rc;
d7e09d03
PT
2578}
2579EXPORT_SYMBOL(ptlrpc_queue_wait);
2580
2581struct ptlrpc_replay_async_args {
2582 int praa_old_state;
2583 int praa_old_status;
2584};
2585
2586/**
2587 * Callback used for replayed requests reply processing.
930cef9a 2588 * In case of successful reply calls registered request replay callback.
d7e09d03
PT
2589 * In case of error restart replay process.
2590 */
2591static int ptlrpc_replay_interpret(const struct lu_env *env,
2592 struct ptlrpc_request *req,
2593 void * data, int rc)
2594{
2595 struct ptlrpc_replay_async_args *aa = data;
2596 struct obd_import *imp = req->rq_import;
2597
d7e09d03
PT
2598 atomic_dec(&imp->imp_replay_inflight);
2599
2600 if (!ptlrpc_client_replied(req)) {
2601 CERROR("request replay timed out, restarting recovery\n");
2602 GOTO(out, rc = -ETIMEDOUT);
2603 }
2604
2605 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2606 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2607 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2608 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2609
2610 /** VBR: check version failure */
2611 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2612 /** replay was failed due to version mismatch */
2613 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2614 spin_lock(&imp->imp_lock);
2615 imp->imp_vbr_failed = 1;
2616 imp->imp_no_lock_replay = 1;
2617 spin_unlock(&imp->imp_lock);
2618 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2619 } else {
2620 /** The transno had better not change over replay. */
2621 LASSERTF(lustre_msg_get_transno(req->rq_reqmsg) ==
2622 lustre_msg_get_transno(req->rq_repmsg) ||
2623 lustre_msg_get_transno(req->rq_repmsg) == 0,
2624 LPX64"/"LPX64"\n",
2625 lustre_msg_get_transno(req->rq_reqmsg),
2626 lustre_msg_get_transno(req->rq_repmsg));
2627 }
2628
2629 spin_lock(&imp->imp_lock);
2630 /** if replays by version then gap occur on server, no trust to locks */
2631 if (lustre_msg_get_flags(req->rq_repmsg) & MSG_VERSION_REPLAY)
2632 imp->imp_no_lock_replay = 1;
2633 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2634 spin_unlock(&imp->imp_lock);
2635 LASSERT(imp->imp_last_replay_transno);
2636
2637 /* transaction number shouldn't be bigger than the latest replayed */
2638 if (req->rq_transno > lustre_msg_get_transno(req->rq_reqmsg)) {
2639 DEBUG_REQ(D_ERROR, req,
2640 "Reported transno "LPU64" is bigger than the "
2641 "replayed one: "LPU64, req->rq_transno,
2642 lustre_msg_get_transno(req->rq_reqmsg));
2643 GOTO(out, rc = -EINVAL);
2644 }
2645
2646 DEBUG_REQ(D_HA, req, "got rep");
2647
2648 /* let the callback do fixups, possibly including in the request */
2649 if (req->rq_replay_cb)
2650 req->rq_replay_cb(req);
2651
2652 if (ptlrpc_client_replied(req) &&
2653 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2654 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2655 lustre_msg_get_status(req->rq_repmsg),
2656 aa->praa_old_status);
2657 } else {
2658 /* Put it back for re-replay. */
2659 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2660 }
2661
2662 /*
2663 * Errors while replay can set transno to 0, but
2664 * imp_last_replay_transno shouldn't be set to 0 anyway
2665 */
2666 if (req->rq_transno == 0)
2667 CERROR("Transno is 0 during replay!\n");
2668
2669 /* continue with recovery */
2670 rc = ptlrpc_import_recovery_state_machine(imp);
2671 out:
2672 req->rq_send_state = aa->praa_old_state;
2673
2674 if (rc != 0)
2675 /* this replay failed, so restart recovery */
2676 ptlrpc_connect_import(imp);
2677
0a3bdb00 2678 return rc;
d7e09d03
PT
2679}
2680
2681/**
2682 * Prepares and queues request for replay.
2683 * Adds it to ptlrpcd queue for actual sending.
2684 * Returns 0 on success.
2685 */
2686int ptlrpc_replay_req(struct ptlrpc_request *req)
2687{
2688 struct ptlrpc_replay_async_args *aa;
d7e09d03
PT
2689
2690 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2691
3949015e 2692 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
d7e09d03 2693 aa = ptlrpc_req_async_args(req);
ec83e611 2694 memset(aa, 0, sizeof(*aa));
d7e09d03
PT
2695
2696 /* Prepare request to be resent with ptlrpcd */
2697 aa->praa_old_state = req->rq_send_state;
2698 req->rq_send_state = LUSTRE_IMP_REPLAY;
2699 req->rq_phase = RQ_PHASE_NEW;
2700 req->rq_next_phase = RQ_PHASE_UNDEFINED;
2701 if (req->rq_repmsg)
2702 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2703 req->rq_status = 0;
2704 req->rq_interpret_reply = ptlrpc_replay_interpret;
2705 /* Readjust the timeout for current conditions */
2706 ptlrpc_at_set_req_timeout(req);
2707
2708 /* Tell server the net_latency, so the server can calculate how long
2709 * it should wait for next replay */
2710 lustre_msg_set_service_time(req->rq_reqmsg,
2711 ptlrpc_at_get_net_latency(req));
2712 DEBUG_REQ(D_HA, req, "REPLAY");
2713
2714 atomic_inc(&req->rq_import->imp_replay_inflight);
2715 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2716
2717 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
0a3bdb00 2718 return 0;
d7e09d03
PT
2719}
2720EXPORT_SYMBOL(ptlrpc_replay_req);
2721
2722/**
2723 * Aborts all in-flight request on import \a imp sending and delayed lists
2724 */
2725void ptlrpc_abort_inflight(struct obd_import *imp)
2726{
2727 struct list_head *tmp, *n;
d7e09d03
PT
2728
2729 /* Make sure that no new requests get processed for this import.
2730 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2731 * this flag and then putting requests on sending_list or delayed_list.
2732 */
2733 spin_lock(&imp->imp_lock);
2734
2735 /* XXX locking? Maybe we should remove each request with the list
2736 * locked? Also, how do we know if the requests on the list are
2737 * being freed at this time?
2738 */
2739 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2740 struct ptlrpc_request *req =
2741 list_entry(tmp, struct ptlrpc_request, rq_list);
2742
2743 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2744
2745 spin_lock(&req->rq_lock);
2746 if (req->rq_import_generation < imp->imp_generation) {
2747 req->rq_err = 1;
2748 req->rq_status = -EIO;
2749 ptlrpc_client_wake_req(req);
2750 }
2751 spin_unlock(&req->rq_lock);
2752 }
2753
2754 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2755 struct ptlrpc_request *req =
2756 list_entry(tmp, struct ptlrpc_request, rq_list);
2757
2758 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2759
2760 spin_lock(&req->rq_lock);
2761 if (req->rq_import_generation < imp->imp_generation) {
2762 req->rq_err = 1;
2763 req->rq_status = -EIO;
2764 ptlrpc_client_wake_req(req);
2765 }
2766 spin_unlock(&req->rq_lock);
2767 }
2768
2769 /* Last chance to free reqs left on the replay list, but we
2770 * will still leak reqs that haven't committed. */
2771 if (imp->imp_replayable)
2772 ptlrpc_free_committed(imp);
2773
2774 spin_unlock(&imp->imp_lock);
d7e09d03
PT
2775}
2776EXPORT_SYMBOL(ptlrpc_abort_inflight);
2777
2778/**
2779 * Abort all uncompleted requests in request set \a set
2780 */
2781void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2782{
2783 struct list_head *tmp, *pos;
2784
2785 LASSERT(set != NULL);
2786
2787 list_for_each_safe(pos, tmp, &set->set_requests) {
2788 struct ptlrpc_request *req =
2789 list_entry(pos, struct ptlrpc_request,
2790 rq_set_chain);
2791
2792 spin_lock(&req->rq_lock);
2793 if (req->rq_phase != RQ_PHASE_RPC) {
2794 spin_unlock(&req->rq_lock);
2795 continue;
2796 }
2797
2798 req->rq_err = 1;
2799 req->rq_status = -EINTR;
2800 ptlrpc_client_wake_req(req);
2801 spin_unlock(&req->rq_lock);
2802 }
2803}
2804
2805static __u64 ptlrpc_last_xid;
2806static spinlock_t ptlrpc_last_xid_lock;
2807
2808/**
2809 * Initialize the XID for the node. This is common among all requests on
2810 * this node, and only requires the property that it is monotonically
2811 * increasing. It does not need to be sequential. Since this is also used
2812 * as the RDMA match bits, it is important that a single client NOT have
2813 * the same match bits for two different in-flight requests, hence we do
2814 * NOT want to have an XID per target or similar.
2815 *
2816 * To avoid an unlikely collision between match bits after a client reboot
2817 * (which would deliver old data into the wrong RDMA buffer) initialize
2818 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2819 * If the time is clearly incorrect, we instead use a 62-bit random number.
2820 * In the worst case the random number will overflow 1M RPCs per second in
2821 * 9133 years, or permutations thereof.
2822 */
2823#define YEAR_2004 (1ULL << 30)
2824void ptlrpc_init_xid(void)
2825{
2826 time_t now = cfs_time_current_sec();
2827
2828 spin_lock_init(&ptlrpc_last_xid_lock);
2829 if (now < YEAR_2004) {
2830 cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2831 ptlrpc_last_xid >>= 2;
2832 ptlrpc_last_xid |= (1ULL << 61);
2833 } else {
2834 ptlrpc_last_xid = (__u64)now << 20;
2835 }
2836
930cef9a 2837 /* Always need to be aligned to a power-of-two for multi-bulk BRW */
d7e09d03
PT
2838 CLASSERT((PTLRPC_BULK_OPS_COUNT & (PTLRPC_BULK_OPS_COUNT - 1)) == 0);
2839 ptlrpc_last_xid &= PTLRPC_BULK_OPS_MASK;
2840}
2841
2842/**
2843 * Increase xid and returns resulting new value to the caller.
2844 *
2845 * Multi-bulk BRW RPCs consume multiple XIDs for each bulk transfer, starting
2846 * at the returned xid, up to xid + PTLRPC_BULK_OPS_COUNT - 1. The BRW RPC
2847 * itself uses the last bulk xid needed, so the server can determine the
2848 * the number of bulk transfers from the RPC XID and a bitmask. The starting
2849 * xid must align to a power-of-two value.
2850 *
2851 * This is assumed to be true due to the initial ptlrpc_last_xid
2852 * value also being initialized to a power-of-two value. LU-1431
2853 */
2854__u64 ptlrpc_next_xid(void)
2855{
2856 __u64 next;
2857
2858 spin_lock(&ptlrpc_last_xid_lock);
2859 next = ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
2860 ptlrpc_last_xid = next;
2861 spin_unlock(&ptlrpc_last_xid_lock);
2862
2863 return next;
2864}
2865EXPORT_SYMBOL(ptlrpc_next_xid);
2866
2867/**
2868 * Get a glimpse at what next xid value might have been.
2869 * Returns possible next xid.
2870 */
2871__u64 ptlrpc_sample_next_xid(void)
2872{
2873#if BITS_PER_LONG == 32
2874 /* need to avoid possible word tearing on 32-bit systems */
2875 __u64 next;
2876
2877 spin_lock(&ptlrpc_last_xid_lock);
2878 next = ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
2879 spin_unlock(&ptlrpc_last_xid_lock);
2880
2881 return next;
2882#else
2883 /* No need to lock, since returned value is racy anyways */
2884 return ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
2885#endif
2886}
2887EXPORT_SYMBOL(ptlrpc_sample_next_xid);
2888
2889/**
2890 * Functions for operating ptlrpc workers.
2891 *
2892 * A ptlrpc work is a function which will be running inside ptlrpc context.
2893 * The callback shouldn't sleep otherwise it will block that ptlrpcd thread.
2894 *
2895 * 1. after a work is created, it can be used many times, that is:
2896 * handler = ptlrpcd_alloc_work();
2897 * ptlrpcd_queue_work();
2898 *
2899 * queue it again when necessary:
2900 * ptlrpcd_queue_work();
2901 * ptlrpcd_destroy_work();
2902 * 2. ptlrpcd_queue_work() can be called by multiple processes meanwhile, but
2903 * it will only be queued once in any time. Also as its name implies, it may
2904 * have delay before it really runs by ptlrpcd thread.
2905 */
2906struct ptlrpc_work_async_args {
2907 __u64 magic;
2908 int (*cb)(const struct lu_env *, void *);
2909 void *cbdata;
2910};
2911
2912#define PTLRPC_WORK_MAGIC 0x6655436b676f4f44ULL /* magic code */
2913
2914static int work_interpreter(const struct lu_env *env,
2915 struct ptlrpc_request *req, void *data, int rc)
2916{
2917 struct ptlrpc_work_async_args *arg = data;
2918
2919 LASSERT(arg->magic == PTLRPC_WORK_MAGIC);
2920 LASSERT(arg->cb != NULL);
2921
2922 return arg->cb(env, arg->cbdata);
2923}
2924
2925/**
2926 * Create a work for ptlrpc.
2927 */
2928void *ptlrpcd_alloc_work(struct obd_import *imp,
2929 int (*cb)(const struct lu_env *, void *), void *cbdata)
2930{
2931 struct ptlrpc_request *req = NULL;
2932 struct ptlrpc_work_async_args *args;
d7e09d03
PT
2933
2934 might_sleep();
2935
2936 if (cb == NULL)
0a3bdb00 2937 return ERR_PTR(-EINVAL);
d7e09d03
PT
2938
2939 /* copy some code from deprecated fakereq. */
2940 OBD_ALLOC_PTR(req);
2941 if (req == NULL) {
2942 CERROR("ptlrpc: run out of memory!\n");
0a3bdb00 2943 return ERR_PTR(-ENOMEM);
d7e09d03
PT
2944 }
2945
2946 req->rq_send_state = LUSTRE_IMP_FULL;
2947 req->rq_type = PTL_RPC_MSG_REQUEST;
2948 req->rq_import = class_import_get(imp);
2949 req->rq_export = NULL;
2950 req->rq_interpret_reply = work_interpreter;
2951 /* don't want reply */
2952 req->rq_receiving_reply = 0;
2953 req->rq_must_unlink = 0;
2954 req->rq_no_delay = req->rq_no_resend = 1;
2955
2956 spin_lock_init(&req->rq_lock);
2957 INIT_LIST_HEAD(&req->rq_list);
2958 INIT_LIST_HEAD(&req->rq_replay_list);
2959 INIT_LIST_HEAD(&req->rq_set_chain);
2960 INIT_LIST_HEAD(&req->rq_history_list);
2961 INIT_LIST_HEAD(&req->rq_exp_list);
2962 init_waitqueue_head(&req->rq_reply_waitq);
2963 init_waitqueue_head(&req->rq_set_waitq);
2964 atomic_set(&req->rq_refcount, 1);
2965
3949015e 2966 CLASSERT(sizeof(*args) <= sizeof(req->rq_async_args));
d7e09d03
PT
2967 args = ptlrpc_req_async_args(req);
2968 args->magic = PTLRPC_WORK_MAGIC;
2969 args->cb = cb;
2970 args->cbdata = cbdata;
2971
0a3bdb00 2972 return req;
d7e09d03
PT
2973}
2974EXPORT_SYMBOL(ptlrpcd_alloc_work);
2975
2976void ptlrpcd_destroy_work(void *handler)
2977{
2978 struct ptlrpc_request *req = handler;
2979
2980 if (req)
2981 ptlrpc_req_finished(req);
2982}
2983EXPORT_SYMBOL(ptlrpcd_destroy_work);
2984
2985int ptlrpcd_queue_work(void *handler)
2986{
2987 struct ptlrpc_request *req = handler;
2988
2989 /*
2990 * Check if the req is already being queued.
2991 *
2992 * Here comes a trick: it lacks a way of checking if a req is being
2993 * processed reliably in ptlrpc. Here I have to use refcount of req
2994 * for this purpose. This is okay because the caller should use this
2995 * req as opaque data. - Jinshan
2996 */
2997 LASSERT(atomic_read(&req->rq_refcount) > 0);
2998 if (atomic_read(&req->rq_refcount) > 1)
2999 return -EBUSY;
3000
3001 if (atomic_inc_return(&req->rq_refcount) > 2) { /* race */
3002 atomic_dec(&req->rq_refcount);
3003 return -EBUSY;
3004 }
3005
3006 /* re-initialize the req */
3007 req->rq_timeout = obd_timeout;
3008 req->rq_sent = cfs_time_current_sec();
3009 req->rq_deadline = req->rq_sent + req->rq_timeout;
3010 req->rq_reply_deadline = req->rq_deadline;
3011 req->rq_phase = RQ_PHASE_INTERPRET;
3012 req->rq_next_phase = RQ_PHASE_COMPLETE;
3013 req->rq_xid = ptlrpc_next_xid();
3014 req->rq_import_generation = req->rq_import->imp_generation;
3015
3016 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
3017 return 0;
3018}
3019EXPORT_SYMBOL(ptlrpcd_queue_work);
This page took 0.293645 seconds and 5 git commands to generate.