4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/ptlrpcd.c
39 /** \defgroup ptlrpcd PortalRPC daemon
41 * ptlrpcd is a special thread with its own set where other user might add
42 * requests when they don't want to wait for their completion.
43 * PtlRPCD will take care of sending such requests and then processing their
44 * replies and calling completion callbacks as necessary.
45 * The callbacks are called directly from ptlrpcd context.
46 * It is important to never significantly block (esp. on RPCs!) within such
47 * completion handler or a deadlock might occur where ptlrpcd enters some
48 * callback that attempts to send another RPC and wait for it to return,
49 * during which time ptlrpcd is completely blocked, so e.g. if import
50 * fails, recovery cannot progress because connection requests are also
56 #define DEBUG_SUBSYSTEM S_RPC
58 #include <linux/kthread.h>
59 #include <libcfs/libcfs.h>
60 #include <lustre_net.h>
61 #include <lustre_lib.h>
62 #include <lustre_ha.h>
63 #include <obd_class.h> /* for obd_zombie */
64 #include <obd_support.h> /* for OBD_FAIL_CHECK */
65 #include <cl_object.h> /* cl_env_{get,put}() */
66 #include <lprocfs_status.h>
68 #include "ptlrpc_internal.h"
70 /* One of these per CPT. */
78 struct ptlrpcd_ctl pd_threads[0];
82 * max_ptlrpcds is obsolete, but retained to ensure that the kernel
83 * module will load on a system where it has been tuned.
84 * A value other than 0 implies it was tuned, in which case the value
85 * is used to derive a setting for ptlrpcd_per_cpt_max.
87 static int max_ptlrpcds;
88 CFS_MODULE_PARM(max_ptlrpcds, "i", int, 0644,
89 "Max ptlrpcd thread count to be started.");
92 * ptlrpcd_bind_policy is obsolete, but retained to ensure that
93 * the kernel module will load on a system where it has been tuned.
94 * A value other than 0 implies it was tuned, in which case the value
95 * is used to derive a setting for ptlrpcd_partner_group_size.
97 static int ptlrpcd_bind_policy;
98 CFS_MODULE_PARM(ptlrpcd_bind_policy, "i", int, 0644,
99 "Ptlrpcd threads binding mode (obsolete).");
102 * ptlrpcd_per_cpt_max: The maximum number of ptlrpcd threads to run
105 static int ptlrpcd_per_cpt_max;
106 CFS_MODULE_PARM(ptlrpcd_per_cpt_max, "i", int, 0644,
107 "Max ptlrpcd thread count to be started per cpt.");
110 * ptlrpcd_partner_group_size: The desired number of threads in each
111 * ptlrpcd partner thread group. Default is 2, corresponding to the
112 * old PDB_POLICY_PAIR. A negative value makes all ptlrpcd threads in
113 * a CPT partners of each other.
115 static int ptlrpcd_partner_group_size;
116 CFS_MODULE_PARM(ptlrpcd_partner_group_size, "i", int, 0644,
117 "Number of ptlrpcd threads in a partner group.");
120 * ptlrpcd_cpts: A CPT string describing the CPU partitions that
121 * ptlrpcd threads should run on. Used to make ptlrpcd threads run on
122 * a subset of all CPTs.
126 * run ptlrpcd threads only on CPT 2.
130 * run ptlrpcd threads on CPTs 0, 1, 2, and 3.
132 * ptlrpcd_cpts=[0-3,5,7]
133 * run ptlrpcd threads on CPTS 0, 1, 2, 3, 5, and 7.
135 static char *ptlrpcd_cpts;
136 CFS_MODULE_PARM(ptlrpcd_cpts, "s", charp, 0644,
137 "CPU partitions ptlrpcd threads should run in");
139 /* ptlrpcds_cpt_idx maps cpt numbers to an index in the ptlrpcds array. */
140 static int *ptlrpcds_cpt_idx;
142 /* ptlrpcds_num is the number of entries in the ptlrpcds array. */
143 static int ptlrpcds_num;
144 static struct ptlrpcd **ptlrpcds;
147 * In addition to the regular thread pool above, there is a single
148 * global recovery thread. Recovery isn't critical for performance,
149 * and doesn't block, but must always be able to proceed, and it is
150 * possible that all normal ptlrpcd threads are blocked. Hence the
151 * need for a dedicated thread.
153 static struct ptlrpcd_ctl ptlrpcd_rcv;
155 struct mutex ptlrpcd_mutex;
156 static int ptlrpcd_users = 0;
158 void ptlrpcd_wake(struct ptlrpc_request *req)
160 struct ptlrpc_request_set *set = req->rq_set;
162 LASSERT(set != NULL);
163 wake_up(&set->set_waitq);
165 EXPORT_SYMBOL(ptlrpcd_wake);
167 static struct ptlrpcd_ctl *
168 ptlrpcd_select_pc(struct ptlrpc_request *req)
174 if (req != NULL && req->rq_send_state != LUSTRE_IMP_FULL)
177 cpt = cfs_cpt_current(cfs_cpt_table, 1);
178 if (ptlrpcds_cpt_idx == NULL)
181 idx = ptlrpcds_cpt_idx[cpt];
184 /* We do not care whether it is strict load balance. */
186 if (++idx == pd->pd_nthreads)
190 return &pd->pd_threads[idx];
194 * Move all request from an existing request set to the ptlrpcd queue.
195 * All requests from the set must be in phase RQ_PHASE_NEW.
197 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set)
199 struct list_head *tmp, *pos;
200 struct ptlrpcd_ctl *pc;
201 struct ptlrpc_request_set *new;
204 pc = ptlrpcd_select_pc(NULL);
207 list_for_each_safe(pos, tmp, &set->set_requests) {
208 struct ptlrpc_request *req =
209 list_entry(pos, struct ptlrpc_request,
212 LASSERT(req->rq_phase == RQ_PHASE_NEW);
214 req->rq_queued_time = cfs_time_current();
217 spin_lock(&new->set_new_req_lock);
218 list_splice_init(&set->set_requests, &new->set_new_requests);
219 i = atomic_read(&set->set_remaining);
220 count = atomic_add_return(i, &new->set_new_count);
221 atomic_set(&set->set_remaining, 0);
222 spin_unlock(&new->set_new_req_lock);
224 wake_up(&new->set_waitq);
226 /* XXX: It maybe unnecessary to wakeup all the partners. But to
227 * guarantee the async RPC can be processed ASAP, we have
228 * no other better choice. It maybe fixed in future. */
229 for (i = 0; i < pc->pc_npartners; i++)
230 wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
235 * Return transferred RPCs count.
237 static int ptlrpcd_steal_rqset(struct ptlrpc_request_set *des,
238 struct ptlrpc_request_set *src)
240 struct list_head *tmp, *pos;
241 struct ptlrpc_request *req;
244 spin_lock(&src->set_new_req_lock);
245 if (likely(!list_empty(&src->set_new_requests))) {
246 list_for_each_safe(pos, tmp, &src->set_new_requests) {
247 req = list_entry(pos, struct ptlrpc_request,
251 list_splice_init(&src->set_new_requests,
253 rc = atomic_read(&src->set_new_count);
254 atomic_add(rc, &des->set_remaining);
255 atomic_set(&src->set_new_count, 0);
257 spin_unlock(&src->set_new_req_lock);
262 * Requests that are added to the ptlrpcd queue are sent via
263 * ptlrpcd_check->ptlrpc_check_set().
265 void ptlrpcd_add_req(struct ptlrpc_request *req)
267 struct ptlrpcd_ctl *pc;
270 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
272 spin_lock(&req->rq_lock);
273 if (req->rq_invalid_rqset) {
274 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(5),
275 back_to_sleep, NULL);
277 req->rq_invalid_rqset = 0;
278 spin_unlock(&req->rq_lock);
279 l_wait_event(req->rq_set_waitq, (req->rq_set == NULL), &lwi);
280 } else if (req->rq_set) {
281 /* If we have a vaid "rq_set", just reuse it to avoid double
283 LASSERT(req->rq_phase == RQ_PHASE_NEW);
284 LASSERT(req->rq_send_state == LUSTRE_IMP_REPLAY);
286 /* ptlrpc_check_set will decrease the count */
287 atomic_inc(&req->rq_set->set_remaining);
288 spin_unlock(&req->rq_lock);
289 wake_up(&req->rq_set->set_waitq);
292 spin_unlock(&req->rq_lock);
295 pc = ptlrpcd_select_pc(req);
297 DEBUG_REQ(D_INFO, req, "add req [%p] to pc [%s:%d]",
298 req, pc->pc_name, pc->pc_index);
300 ptlrpc_set_add_new_req(pc, req);
302 EXPORT_SYMBOL(ptlrpcd_add_req);
304 static inline void ptlrpc_reqset_get(struct ptlrpc_request_set *set)
306 atomic_inc(&set->set_refcount);
310 * Check if there is more work to do on ptlrpcd set.
313 static int ptlrpcd_check(struct lu_env *env, struct ptlrpcd_ctl *pc)
315 struct list_head *tmp, *pos;
316 struct ptlrpc_request *req;
317 struct ptlrpc_request_set *set = pc->pc_set;
322 if (atomic_read(&set->set_new_count)) {
323 spin_lock(&set->set_new_req_lock);
324 if (likely(!list_empty(&set->set_new_requests))) {
325 list_splice_init(&set->set_new_requests,
327 atomic_add(atomic_read(&set->set_new_count),
328 &set->set_remaining);
329 atomic_set(&set->set_new_count, 0);
331 * Need to calculate its timeout.
335 spin_unlock(&set->set_new_req_lock);
338 /* We should call lu_env_refill() before handling new requests to make
339 * sure that env key the requests depending on really exists.
341 rc2 = lu_env_refill(env);
344 * XXX This is very awkward situation, because
345 * execution can neither continue (request
346 * interpreters assume that env is set up), nor repeat
347 * the loop (as this potentially results in a tight
348 * loop of -ENOMEM's).
350 * Fortunately, refill only ever does something when
351 * new modules are loaded, i.e., early during boot up.
353 CERROR("Failure to refill session: %d\n", rc2);
357 if (atomic_read(&set->set_remaining))
358 rc |= ptlrpc_check_set(env, set);
360 /* NB: ptlrpc_check_set has already moved complted request at the
361 * head of seq::set_requests */
362 list_for_each_safe(pos, tmp, &set->set_requests) {
363 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
364 if (req->rq_phase != RQ_PHASE_COMPLETE)
367 list_del_init(&req->rq_set_chain);
369 ptlrpc_req_finished(req);
374 * If new requests have been added, make sure to wake up.
376 rc = atomic_read(&set->set_new_count);
378 /* If we have nothing to do, check whether we can take some
379 * work from our partner threads. */
380 if (rc == 0 && pc->pc_npartners > 0) {
381 struct ptlrpcd_ctl *partner;
382 struct ptlrpc_request_set *ps;
383 int first = pc->pc_cursor;
386 partner = pc->pc_partners[pc->pc_cursor++];
387 if (pc->pc_cursor >= pc->pc_npartners)
392 spin_lock(&partner->pc_lock);
393 ps = partner->pc_set;
395 spin_unlock(&partner->pc_lock);
399 ptlrpc_reqset_get(ps);
400 spin_unlock(&partner->pc_lock);
402 if (atomic_read(&ps->set_new_count)) {
403 rc = ptlrpcd_steal_rqset(set, ps);
405 CDEBUG(D_RPCTRACE, "transfer %d"
406 " async RPCs [%d->%d]\n",
407 rc, partner->pc_index,
410 ptlrpc_reqset_put(ps);
411 } while (rc == 0 && pc->pc_cursor != first);
419 * Main ptlrpcd thread.
420 * ptlrpc's code paths like to execute in process context, so we have this
421 * thread which spins on a set which contains the rpcs and sends them.
424 static int ptlrpcd(void *arg)
426 struct ptlrpcd_ctl *pc = arg;
427 struct ptlrpc_request_set *set;
428 struct lu_context ses = { 0 };
429 struct lu_env env = { .le_ses = &ses };
436 if (cfs_cpt_bind(cfs_cpt_table, pc->pc_cpt) != 0)
437 CWARN("Failed to bind %s on CPT %d\n", pc->pc_name, pc->pc_cpt);
440 * Allocate the request set after the thread has been bound
441 * above. This is safe because no requests will be queued
442 * until all ptlrpcd threads have confirmed that they have
443 * successfully started.
445 set = ptlrpc_prep_set();
447 GOTO(failed, rc = -ENOMEM);
448 spin_lock(&pc->pc_lock);
450 spin_unlock(&pc->pc_lock);
452 /* Both client and server (MDT/OST) may use the environment. */
453 rc = lu_context_init(&env.le_ctx, LCT_MD_THREAD |
460 rc = lu_context_init(env.le_ses, LCT_SESSION |
464 lu_context_fini(&env.le_ctx);
468 complete(&pc->pc_starting);
471 * This mainloop strongly resembles ptlrpc_set_wait() except that our
472 * set never completes. ptlrpcd_check() calls ptlrpc_check_set() when
473 * there are requests in the set. New requests come in on the set's
474 * new_req_list and ptlrpcd_check() moves them into the set.
477 struct l_wait_info lwi;
480 timeout = ptlrpc_set_next_timeout(set);
481 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
482 ptlrpc_expired_set, set);
484 lu_context_enter(&env.le_ctx);
485 lu_context_enter(env.le_ses);
486 l_wait_event(set->set_waitq, ptlrpcd_check(&env, pc), &lwi);
487 lu_context_exit(&env.le_ctx);
488 lu_context_exit(env.le_ses);
491 * Abort inflight rpcs for forced stop case.
493 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
494 if (test_bit(LIOD_FORCE, &pc->pc_flags))
495 ptlrpc_abort_set(set);
500 * Let's make one more loop to make sure that ptlrpcd_check()
501 * copied all raced new rpcs into the set so we can kill them.
506 * Wait for inflight requests to drain.
508 if (!list_empty(&set->set_requests))
509 ptlrpc_set_wait(set);
510 lu_context_fini(&env.le_ctx);
511 lu_context_fini(env.le_ses);
513 complete(&pc->pc_finishing);
519 complete(&pc->pc_starting);
523 static void ptlrpcd_ctl_init(struct ptlrpcd_ctl *pc, int index, int cpt)
527 pc->pc_index = index;
529 init_completion(&pc->pc_starting);
530 init_completion(&pc->pc_finishing);
531 spin_lock_init(&pc->pc_lock);
534 /* Recovery thread. */
535 snprintf(pc->pc_name, sizeof(pc->pc_name), "ptlrpcd_rcv");
537 /* Regular thread. */
538 snprintf(pc->pc_name, sizeof(pc->pc_name),
539 "ptlrpcd_%02d_%02d", cpt, index);
545 /* XXX: We want multiple CPU cores to share the async RPC load. So we
546 * start many ptlrpcd threads. We also want to reduce the ptlrpcd
547 * overhead caused by data transfer cross-CPU cores. So we bind
548 * all ptlrpcd threads to a CPT, in the expectation that CPTs
549 * will be defined in a way that matches these boundaries. Within
550 * a CPT a ptlrpcd thread can be scheduled on any available core.
552 * Each ptlrpcd thread has its own request queue. This can cause
553 * response delay if the thread is already busy. To help with
554 * this we define partner threads: these are other threads bound
555 * to the same CPT which will check for work in each other's
556 * request queues if they have no work to do.
558 * The desired number of partner threads can be tuned by setting
559 * ptlrpcd_partner_group_size. The default is to create pairs of
562 static int ptlrpcd_partners(struct ptlrpcd *pd, int index)
564 struct ptlrpcd_ctl *pc;
565 struct ptlrpcd_ctl **ppc;
571 LASSERT(index >= 0 && index < pd->pd_nthreads);
572 pc = &pd->pd_threads[index];
573 pc->pc_npartners = pd->pd_groupsize - 1;
575 if (pc->pc_npartners <= 0)
578 OBD_CPT_ALLOC(pc->pc_partners, cfs_cpt_table, pc->pc_cpt,
579 sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
580 if (pc->pc_partners == NULL) {
581 pc->pc_npartners = 0;
582 GOTO(out, rc = -ENOMEM);
585 first = index - index % pd->pd_groupsize;
586 ppc = pc->pc_partners;
587 for (i = first; i < first + pd->pd_groupsize; i++) {
589 *ppc++ = &pd->pd_threads[i];
595 int ptlrpcd_start(struct ptlrpcd_ctl *pc)
597 struct task_struct *task;
602 * Do not allow starting a second thread for one pc.
604 if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
605 CWARN("Starting second thread (%s) for same pc %p\n",
611 * So far only "client" ptlrpcd uses an environment. In the future,
612 * ptlrpcd thread (or a thread-set) has to be given an argument,
613 * describing its "scope".
615 rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
619 task = kthread_run(ptlrpcd, pc, pc->pc_name);
621 GOTO(out_set, rc = PTR_ERR(task));
623 wait_for_completion(&pc->pc_starting);
631 if (pc->pc_set != NULL) {
632 struct ptlrpc_request_set *set = pc->pc_set;
634 spin_lock(&pc->pc_lock);
636 spin_unlock(&pc->pc_lock);
637 ptlrpc_set_destroy(set);
639 lu_context_fini(&pc->pc_env.le_ctx);
641 clear_bit(LIOD_START, &pc->pc_flags);
645 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
649 if (!test_bit(LIOD_START, &pc->pc_flags)) {
650 CWARN("Thread for pc %p was not started\n", pc);
654 set_bit(LIOD_STOP, &pc->pc_flags);
656 set_bit(LIOD_FORCE, &pc->pc_flags);
657 wake_up(&pc->pc_set->set_waitq);
663 void ptlrpcd_free(struct ptlrpcd_ctl *pc)
665 struct ptlrpc_request_set *set = pc->pc_set;
668 if (!test_bit(LIOD_START, &pc->pc_flags)) {
669 CWARN("Thread for pc %p was not started\n", pc);
673 wait_for_completion(&pc->pc_finishing);
674 lu_context_fini(&pc->pc_env.le_ctx);
676 spin_lock(&pc->pc_lock);
678 spin_unlock(&pc->pc_lock);
679 ptlrpc_set_destroy(set);
681 clear_bit(LIOD_START, &pc->pc_flags);
682 clear_bit(LIOD_STOP, &pc->pc_flags);
683 clear_bit(LIOD_FORCE, &pc->pc_flags);
686 if (pc->pc_npartners > 0) {
687 LASSERT(pc->pc_partners != NULL);
689 OBD_FREE(pc->pc_partners,
690 sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
691 pc->pc_partners = NULL;
693 pc->pc_npartners = 0;
698 static void ptlrpcd_fini(void)
705 if (ptlrpcds != NULL) {
706 for (i = 0; i < ptlrpcds_num; i++) {
707 if (ptlrpcds[i] == NULL)
709 for (j = 0; j < ptlrpcds[i]->pd_nthreads; j++)
710 ptlrpcd_stop(&ptlrpcds[i]->pd_threads[j], 0);
711 for (j = 0; j < ptlrpcds[i]->pd_nthreads; j++)
712 ptlrpcd_free(&ptlrpcds[i]->pd_threads[j]);
713 OBD_FREE(ptlrpcds[i], ptlrpcds[i]->pd_size);
716 OBD_FREE(ptlrpcds, sizeof(ptlrpcds[0]) * ptlrpcds_num);
720 ptlrpcd_stop(&ptlrpcd_rcv, 0);
721 ptlrpcd_free(&ptlrpcd_rcv);
723 if (ptlrpcds_cpt_idx != NULL) {
724 ncpts = cfs_cpt_number(cfs_cpt_table);
725 OBD_FREE(ptlrpcds_cpt_idx, ncpts * sizeof(ptlrpcds_cpt_idx[0]));
726 ptlrpcds_cpt_idx = NULL;
732 static int ptlrpcd_init(void)
740 struct cfs_cpt_table *cptable;
748 * Determine the CPTs that ptlrpcd threads will run on.
750 cptable = cfs_cpt_table;
751 ncpts = cfs_cpt_number(cptable);
752 if (ptlrpcd_cpts != NULL) {
753 struct cfs_expr_list *el;
755 size = ncpts * sizeof(ptlrpcds_cpt_idx[0]);
756 OBD_ALLOC(ptlrpcds_cpt_idx, size);
757 if (ptlrpcds_cpt_idx == NULL)
758 GOTO(out, rc = -ENOMEM);
760 rc = cfs_expr_list_parse(ptlrpcd_cpts,
761 strlen(ptlrpcd_cpts),
764 CERROR("%s: invalid CPT pattern string: %s",
765 "ptlrpcd_cpts", ptlrpcd_cpts);
766 GOTO(out, rc = -EINVAL);
769 rc = cfs_expr_list_values(el, ncpts, &cpts);
770 cfs_expr_list_free(el);
772 CERROR("%s: failed to parse CPT array %s: %d\n",
773 "ptlrpcd_cpts", ptlrpcd_cpts, rc);
780 * Create the cpt-to-index map. When there is no match
781 * in the cpt table, pick a cpt at random. This could
782 * be changed to take the topology of the system into
785 for (cpt = 0; cpt < ncpts; cpt++) {
786 for (i = 0; i < rc; i++)
791 ptlrpcds_cpt_idx[cpt] = i;
794 cfs_expr_list_values_free(cpts, rc);
797 ptlrpcds_num = ncpts;
799 size = ncpts * sizeof(ptlrpcds[0]);
800 OBD_ALLOC(ptlrpcds, size);
801 if (ptlrpcds == NULL)
802 GOTO(out, rc = -ENOMEM);
805 * The max_ptlrpcds parameter is obsolete, but do something
806 * sane if it has been tuned, and complain if
807 * ptlrpcd_per_cpt_max has also been tuned.
809 if (max_ptlrpcds != 0) {
810 CWARN("max_ptlrpcds is obsolete.\n");
811 if (ptlrpcd_per_cpt_max == 0) {
812 ptlrpcd_per_cpt_max = max_ptlrpcds / ncpts;
813 /* Round up if there is a remainder. */
814 if (max_ptlrpcds % ncpts != 0)
815 ptlrpcd_per_cpt_max++;
816 CWARN("Setting ptlrpcd_per_cpt_max = %d\n",
817 ptlrpcd_per_cpt_max);
819 CWARN("ptlrpd_per_cpt_max is also set!\n");
824 * The ptlrpcd_bind_policy parameter is obsolete, but do
825 * something sane if it has been tuned, and complain if
826 * ptlrpcd_partner_group_size is also tuned.
828 if (ptlrpcd_bind_policy != 0) {
829 CWARN("ptlrpcd_bind_policy is obsolete.\n");
830 if (ptlrpcd_partner_group_size == 0) {
831 switch (ptlrpcd_bind_policy) {
832 case 1: /* PDB_POLICY_NONE */
833 case 2: /* PDB_POLICY_FULL */
834 ptlrpcd_partner_group_size = 1;
836 case 3: /* PDB_POLICY_PAIR */
837 ptlrpcd_partner_group_size = 2;
839 case 4: /* PDB_POLICY_NEIGHBOR */
841 ptlrpcd_partner_group_size = -1; /* CPT */
843 ptlrpcd_partner_group_size = 3; /* Triplets */
846 default: /* Illegal value, use the default. */
847 ptlrpcd_partner_group_size = 2;
850 CWARN("Setting ptlrpcd_partner_group_size = %d\n",
851 ptlrpcd_partner_group_size);
853 CWARN("ptlrpcd_partner_group_size is also set!\n");
857 if (ptlrpcd_partner_group_size == 0)
858 ptlrpcd_partner_group_size = 2;
859 else if (ptlrpcd_partner_group_size < 0)
860 ptlrpcd_partner_group_size = -1;
861 else if (ptlrpcd_per_cpt_max > 0 &&
862 ptlrpcd_partner_group_size > ptlrpcd_per_cpt_max)
863 ptlrpcd_partner_group_size = ptlrpcd_per_cpt_max;
866 * Start the recovery thread first.
868 set_bit(LIOD_RECOVERY, &ptlrpcd_rcv.pc_flags);
869 ptlrpcd_ctl_init(&ptlrpcd_rcv, -1, CFS_CPT_ANY);
870 rc = ptlrpcd_start(&ptlrpcd_rcv);
874 for (i = 0; i < ncpts; i++) {
880 nthreads = cfs_cpt_weight(cptable, cpt);
881 if (ptlrpcd_per_cpt_max > 0 && ptlrpcd_per_cpt_max < nthreads)
882 nthreads = ptlrpcd_per_cpt_max;
886 if (ptlrpcd_partner_group_size <= 0) {
887 groupsize = nthreads;
888 } else if (nthreads <= ptlrpcd_partner_group_size) {
889 groupsize = nthreads;
891 groupsize = ptlrpcd_partner_group_size;
892 if (nthreads % groupsize != 0)
893 nthreads += groupsize - (nthreads % groupsize);
896 size = offsetof(struct ptlrpcd, pd_threads[nthreads]);
897 OBD_CPT_ALLOC(pd, cptable, cpt, size);
899 GOTO(out, rc = -ENOMEM);
904 pd->pd_nthreads = nthreads;
905 pd->pd_groupsize = groupsize;
909 * The ptlrpcd threads in a partner group can access
910 * each other's struct ptlrpcd_ctl, so these must be
911 * initialized before any thead is started.
913 for (j = 0; j < nthreads; j++) {
914 ptlrpcd_ctl_init(&pd->pd_threads[j], j, cpt);
915 rc = ptlrpcd_partners(pd, j);
920 /* XXX: We start nthreads ptlrpc daemons on this cpt.
921 * Each of them can process any non-recovery
922 * async RPC to improve overall async RPC
925 * But there are some issues with async I/O RPCs
926 * and async non-I/O RPCs processed in the same
927 * set under some cases. The ptlrpcd may be
928 * blocked by some async I/O RPC(s), then will
929 * cause other async non-I/O RPC(s) can not be
932 * Maybe we should distinguish blocked async RPCs
933 * from non-blocked async RPCs, and process them
934 * in different ptlrpcd sets to avoid unnecessary
935 * dependency. But how to distribute async RPCs
936 * load among all the ptlrpc daemons becomes
939 for (j = 0; j < nthreads; j++) {
940 rc = ptlrpcd_start(&pd->pd_threads[j]);
952 int ptlrpcd_addref(void)
957 mutex_lock(&ptlrpcd_mutex);
958 if (++ptlrpcd_users == 1) {
963 mutex_unlock(&ptlrpcd_mutex);
966 EXPORT_SYMBOL(ptlrpcd_addref);
968 void ptlrpcd_decref(void)
970 mutex_lock(&ptlrpcd_mutex);
971 if (--ptlrpcd_users == 0)
973 mutex_unlock(&ptlrpcd_mutex);
975 EXPORT_SYMBOL(ptlrpcd_decref);