Whamcloud - gitweb
LU-9019 ldlm: migrate the rest of the code to 64 bit time
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpcd.c
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/ptlrpcd.c
33  */
34
35 /** \defgroup ptlrpcd PortalRPC daemon
36  *
37  * ptlrpcd is a special thread with its own set where other user might add
38  * requests when they don't want to wait for their completion.
39  * PtlRPCD will take care of sending such requests and then processing their
40  * replies and calling completion callbacks as necessary.
41  * The callbacks are called directly from ptlrpcd context.
42  * It is important to never significantly block (esp. on RPCs!) within such
43  * completion handler or a deadlock might occur where ptlrpcd enters some
44  * callback that attempts to send another RPC and wait for it to return,
45  * during which time ptlrpcd is completely blocked, so e.g. if import
46  * fails, recovery cannot progress because connection requests are also
47  * sent by ptlrpcd.
48  *
49  * @{
50  */
51
52 #define DEBUG_SUBSYSTEM S_RPC
53
54 #include <linux/kthread.h>
55 #include <libcfs/libcfs.h>
56 #include <lustre_net.h>
57 #include <lustre_lib.h>
58 #include <lustre_ha.h>
59 #include <obd_class.h>   /* for obd_zombie */
60 #include <obd_support.h> /* for OBD_FAIL_CHECK */
61 #include <cl_object.h> /* cl_env_{get,put}() */
62 #include <lprocfs_status.h>
63
64 #include "ptlrpc_internal.h"
65
66 /* One of these per CPT. */
67 struct ptlrpcd {
68         int                     pd_size;
69         int                     pd_index;
70         int                     pd_cpt;
71         int                     pd_cursor;
72         int                     pd_nthreads;
73         int                     pd_groupsize;
74         struct ptlrpcd_ctl      pd_threads[0];
75 };
76
77 /*
78  * max_ptlrpcds is obsolete, but retained to ensure that the kernel
79  * module will load on a system where it has been tuned.
80  * A value other than 0 implies it was tuned, in which case the value
81  * is used to derive a setting for ptlrpcd_per_cpt_max.
82  */
83 static int max_ptlrpcds;
84 module_param(max_ptlrpcds, int, 0644);
85 MODULE_PARM_DESC(max_ptlrpcds,
86                  "Max ptlrpcd thread count to be started (obsolete).");
87
88 /*
89  * ptlrpcd_bind_policy is obsolete, but retained to ensure that
90  * the kernel module will load on a system where it has been tuned.
91  * A value other than 0 implies it was tuned, in which case the value
92  * is used to derive a setting for ptlrpcd_partner_group_size.
93  */
94 static int ptlrpcd_bind_policy;
95 module_param(ptlrpcd_bind_policy, int, 0644);
96 MODULE_PARM_DESC(ptlrpcd_bind_policy,
97                  "Ptlrpcd threads binding mode (obsolete).");
98
99 /*
100  * ptlrpcd_per_cpt_max: The maximum number of ptlrpcd threads to run
101  * in a CPT.
102  */
103 static int ptlrpcd_per_cpt_max;
104 module_param(ptlrpcd_per_cpt_max, int, 0644);
105 MODULE_PARM_DESC(ptlrpcd_per_cpt_max,
106                  "Max ptlrpcd thread count to be started per CPT.");
107
108 /*
109  * ptlrpcd_partner_group_size: The desired number of threads in each
110  * ptlrpcd partner thread group. Default is 2, corresponding to the
111  * old PDB_POLICY_PAIR. A negative value makes all ptlrpcd threads in
112  * a CPT partners of each other.
113  */
114 static int ptlrpcd_partner_group_size;
115 module_param(ptlrpcd_partner_group_size, int, 0644);
116 MODULE_PARM_DESC(ptlrpcd_partner_group_size,
117                  "Number of ptlrpcd threads in a partner group.");
118
119 /*
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.
123  *
124  * ptlrpcd_cpts=2
125  * ptlrpcd_cpts=[2]
126  *   run ptlrpcd threads only on CPT 2.
127  *
128  * ptlrpcd_cpts=0-3
129  * ptlrpcd_cpts=[0-3]
130  *   run ptlrpcd threads on CPTs 0, 1, 2, and 3.
131  *
132  * ptlrpcd_cpts=[0-3,5,7]
133  *   run ptlrpcd threads on CPTS 0, 1, 2, 3, 5, and 7.
134  */
135 static char *ptlrpcd_cpts;
136 module_param(ptlrpcd_cpts, charp, 0644);
137 MODULE_PARM_DESC(ptlrpcd_cpts,
138                  "CPU partitions ptlrpcd threads should run in");
139
140 /* ptlrpcds_cpt_idx maps cpt numbers to an index in the ptlrpcds array. */
141 static int              *ptlrpcds_cpt_idx;
142
143 /* ptlrpcds_num is the number of entries in the ptlrpcds array. */
144 static int              ptlrpcds_num;
145 static struct ptlrpcd   **ptlrpcds;
146
147 /*
148  * In addition to the regular thread pool above, there is a single
149  * global recovery thread. Recovery isn't critical for performance,
150  * and doesn't block, but must always be able to proceed, and it is
151  * possible that all normal ptlrpcd threads are blocked. Hence the
152  * need for a dedicated thread.
153  */
154 static struct ptlrpcd_ctl ptlrpcd_rcv;
155
156 struct mutex ptlrpcd_mutex;
157 static int ptlrpcd_users = 0;
158
159 void ptlrpcd_wake(struct ptlrpc_request *req)
160 {
161         struct ptlrpc_request_set *set = req->rq_set;
162
163         LASSERT(set != NULL);
164         wake_up(&set->set_waitq);
165 }
166 EXPORT_SYMBOL(ptlrpcd_wake);
167
168 static struct ptlrpcd_ctl *
169 ptlrpcd_select_pc(struct ptlrpc_request *req)
170 {
171         struct ptlrpcd  *pd;
172         int             cpt;
173         int             idx;
174
175         if (req != NULL && req->rq_send_state != LUSTRE_IMP_FULL)
176                 return &ptlrpcd_rcv;
177
178         cpt = cfs_cpt_current(cfs_cpt_table, 1);
179         if (ptlrpcds_cpt_idx == NULL)
180                 idx = cpt;
181         else
182                 idx = ptlrpcds_cpt_idx[cpt];
183         pd = ptlrpcds[idx];
184
185         /* We do not care whether it is strict load balance. */
186         idx = pd->pd_cursor;
187         if (++idx == pd->pd_nthreads)
188                 idx = 0;
189         pd->pd_cursor = idx;
190
191         return &pd->pd_threads[idx];
192 }
193
194 /**
195  * Move all request from an existing request set to the ptlrpcd queue.
196  * All requests from the set must be in phase RQ_PHASE_NEW.
197  */
198 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set)
199 {
200         struct list_head *tmp, *pos;
201         struct ptlrpcd_ctl *pc;
202         struct ptlrpc_request_set *new;
203         int count, i;
204
205         pc = ptlrpcd_select_pc(NULL);
206         new = pc->pc_set;
207
208         list_for_each_safe(pos, tmp, &set->set_requests) {
209                 struct ptlrpc_request *req =
210                         list_entry(pos, struct ptlrpc_request,
211                                    rq_set_chain);
212
213                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
214                 req->rq_set = new;
215                 req->rq_queued_time = ktime_get_seconds();
216         }
217
218         spin_lock(&new->set_new_req_lock);
219         list_splice_init(&set->set_requests, &new->set_new_requests);
220         i = atomic_read(&set->set_remaining);
221         count = atomic_add_return(i, &new->set_new_count);
222         atomic_set(&set->set_remaining, 0);
223         spin_unlock(&new->set_new_req_lock);
224         if (count == i) {
225                 wake_up(&new->set_waitq);
226
227                 /* XXX: It maybe unnecessary to wakeup all the partners. But to
228                  *      guarantee the async RPC can be processed ASAP, we have
229                  *      no other better choice. It maybe fixed in future. */
230                 for (i = 0; i < pc->pc_npartners; i++)
231                         wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
232         }
233 }
234
235 /**
236  * Return transferred RPCs count.
237  */
238 static int ptlrpcd_steal_rqset(struct ptlrpc_request_set *des,
239                                struct ptlrpc_request_set *src)
240 {
241         struct list_head *tmp, *pos;
242         struct ptlrpc_request *req;
243         int rc = 0;
244
245         spin_lock(&src->set_new_req_lock);
246         if (likely(!list_empty(&src->set_new_requests))) {
247                 list_for_each_safe(pos, tmp, &src->set_new_requests) {
248                         req = list_entry(pos, struct ptlrpc_request,
249                                          rq_set_chain);
250                         req->rq_set = des;
251                 }
252                 list_splice_init(&src->set_new_requests,
253                                  &des->set_requests);
254                 rc = atomic_read(&src->set_new_count);
255                 atomic_add(rc, &des->set_remaining);
256                 atomic_set(&src->set_new_count, 0);
257         }
258         spin_unlock(&src->set_new_req_lock);
259         return rc;
260 }
261
262 /**
263  * Requests that are added to the ptlrpcd queue are sent via
264  * ptlrpcd_check->ptlrpc_check_set().
265  */
266 void ptlrpcd_add_req(struct ptlrpc_request *req)
267 {
268         struct ptlrpcd_ctl *pc;
269
270         if (req->rq_reqmsg)
271                 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
272
273         spin_lock(&req->rq_lock);
274         if (req->rq_invalid_rqset) {
275                 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(5),
276                                                      back_to_sleep, NULL);
277
278                 req->rq_invalid_rqset = 0;
279                 spin_unlock(&req->rq_lock);
280                 l_wait_event(req->rq_set_waitq, (req->rq_set == NULL), &lwi);
281         } else if (req->rq_set) {
282                 /* If we have a vaid "rq_set", just reuse it to avoid double
283                  * linked. */
284                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
285                 LASSERT(req->rq_send_state == LUSTRE_IMP_REPLAY);
286
287                 /* ptlrpc_check_set will decrease the count */
288                 atomic_inc(&req->rq_set->set_remaining);
289                 spin_unlock(&req->rq_lock);
290                 wake_up(&req->rq_set->set_waitq);
291                 return;
292         } else {
293                 spin_unlock(&req->rq_lock);
294         }
295
296         pc = ptlrpcd_select_pc(req);
297
298         DEBUG_REQ(D_INFO, req, "add req [%p] to pc [%s:%d]",
299                   req, pc->pc_name, pc->pc_index);
300
301         ptlrpc_set_add_new_req(pc, req);
302 }
303 EXPORT_SYMBOL(ptlrpcd_add_req);
304
305 static inline void ptlrpc_reqset_get(struct ptlrpc_request_set *set)
306 {
307         atomic_inc(&set->set_refcount);
308 }
309
310 /**
311  * Check if there is more work to do on ptlrpcd set.
312  * Returns 1 if yes.
313  */
314 static int ptlrpcd_check(struct lu_env *env, struct ptlrpcd_ctl *pc)
315 {
316         struct list_head *tmp, *pos;
317         struct ptlrpc_request *req;
318         struct ptlrpc_request_set *set = pc->pc_set;
319         int rc = 0;
320         int rc2;
321         ENTRY;
322
323         if (atomic_read(&set->set_new_count)) {
324                 spin_lock(&set->set_new_req_lock);
325                 if (likely(!list_empty(&set->set_new_requests))) {
326                         list_splice_init(&set->set_new_requests,
327                                              &set->set_requests);
328                         atomic_add(atomic_read(&set->set_new_count),
329                                    &set->set_remaining);
330                         atomic_set(&set->set_new_count, 0);
331                         /*
332                          * Need to calculate its timeout.
333                          */
334                         rc = 1;
335                 }
336                 spin_unlock(&set->set_new_req_lock);
337         }
338
339         /* We should call lu_env_refill() before handling new requests to make
340          * sure that env key the requests depending on really exists.
341          */
342         rc2 = lu_env_refill(env);
343         if (rc2 != 0) {
344                 /*
345                  * XXX This is very awkward situation, because
346                  * execution can neither continue (request
347                  * interpreters assume that env is set up), nor repeat
348                  * the loop (as this potentially results in a tight
349                  * loop of -ENOMEM's).
350                  *
351                  * Fortunately, refill only ever does something when
352                  * new modules are loaded, i.e., early during boot up.
353                  */
354                 CERROR("Failure to refill session: %d\n", rc2);
355                 RETURN(rc);
356         }
357
358         if (atomic_read(&set->set_remaining))
359                 rc |= ptlrpc_check_set(env, set);
360
361         /* NB: ptlrpc_check_set has already moved complted request at the
362          * head of seq::set_requests */
363         list_for_each_safe(pos, tmp, &set->set_requests) {
364                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
365                 if (req->rq_phase != RQ_PHASE_COMPLETE)
366                         break;
367
368                 list_del_init(&req->rq_set_chain);
369                 req->rq_set = NULL;
370                 ptlrpc_req_finished(req);
371         }
372
373         if (rc == 0) {
374                 /*
375                  * If new requests have been added, make sure to wake up.
376                  */
377                 rc = atomic_read(&set->set_new_count);
378
379                 /* If we have nothing to do, check whether we can take some
380                  * work from our partner threads. */
381                 if (rc == 0 && pc->pc_npartners > 0) {
382                         struct ptlrpcd_ctl *partner;
383                         struct ptlrpc_request_set *ps;
384                         int first = pc->pc_cursor;
385
386                         do {
387                                 partner = pc->pc_partners[pc->pc_cursor++];
388                                 if (pc->pc_cursor >= pc->pc_npartners)
389                                         pc->pc_cursor = 0;
390                                 if (partner == NULL)
391                                         continue;
392
393                                 spin_lock(&partner->pc_lock);
394                                 ps = partner->pc_set;
395                                 if (ps == NULL) {
396                                         spin_unlock(&partner->pc_lock);
397                                         continue;
398                                 }
399
400                                 ptlrpc_reqset_get(ps);
401                                 spin_unlock(&partner->pc_lock);
402
403                                 if (atomic_read(&ps->set_new_count)) {
404                                         rc = ptlrpcd_steal_rqset(set, ps);
405                                         if (rc > 0)
406                                                 CDEBUG(D_RPCTRACE, "transfer %d"
407                                                        " async RPCs [%d->%d]\n",
408                                                        rc, partner->pc_index,
409                                                        pc->pc_index);
410                                 }
411                                 ptlrpc_reqset_put(ps);
412                         } while (rc == 0 && pc->pc_cursor != first);
413                 }
414         }
415
416         RETURN(rc || test_bit(LIOD_STOP, &pc->pc_flags));
417 }
418
419 /**
420  * Main ptlrpcd thread.
421  * ptlrpc's code paths like to execute in process context, so we have this
422  * thread which spins on a set which contains the rpcs and sends them.
423  *
424  */
425 static int ptlrpcd(void *arg)
426 {
427         struct ptlrpcd_ctl              *pc = arg;
428         struct ptlrpc_request_set       *set;
429         struct lu_context               ses = { 0 };
430         struct lu_env                   env = { .le_ses = &ses };
431         int                             rc = 0;
432         int                             exit = 0;
433         ENTRY;
434
435         unshare_fs_struct();
436
437         if (cfs_cpt_bind(cfs_cpt_table, pc->pc_cpt) != 0)
438                 CWARN("Failed to bind %s on CPT %d\n", pc->pc_name, pc->pc_cpt);
439
440         /*
441          * Allocate the request set after the thread has been bound
442          * above. This is safe because no requests will be queued
443          * until all ptlrpcd threads have confirmed that they have
444          * successfully started.
445          */
446         set = ptlrpc_prep_set();
447         if (set == NULL)
448                 GOTO(failed, rc = -ENOMEM);
449         spin_lock(&pc->pc_lock);
450         pc->pc_set = set;
451         spin_unlock(&pc->pc_lock);
452
453         /* Both client and server (MDT/OST) may use the environment. */
454         rc = lu_context_init(&env.le_ctx, LCT_MD_THREAD |
455                                           LCT_DT_THREAD |
456                                           LCT_CL_THREAD |
457                                           LCT_REMEMBER  |
458                                           LCT_NOREF);
459         if (rc != 0)
460                 GOTO(failed, rc);
461         rc = lu_context_init(env.le_ses, LCT_SESSION  |
462                                          LCT_REMEMBER |
463                                          LCT_NOREF);
464         if (rc != 0) {
465                 lu_context_fini(&env.le_ctx);
466                 GOTO(failed, rc);
467         }
468
469         complete(&pc->pc_starting);
470
471         /*
472          * This mainloop strongly resembles ptlrpc_set_wait() except that our
473          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
474          * there are requests in the set. New requests come in on the set's
475          * new_req_list and ptlrpcd_check() moves them into the set.
476          */
477         do {
478                 struct l_wait_info lwi;
479                 time64_t timeout;
480
481                 timeout = ptlrpc_set_next_timeout(set);
482                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout),
483                                 ptlrpc_expired_set, set);
484
485                 lu_context_enter(&env.le_ctx);
486                 lu_context_enter(env.le_ses);
487                 l_wait_event(set->set_waitq, ptlrpcd_check(&env, pc), &lwi);
488                 lu_context_exit(&env.le_ctx);
489                 lu_context_exit(env.le_ses);
490
491                 /*
492                  * Abort inflight rpcs for forced stop case.
493                  */
494                 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
495                         if (test_bit(LIOD_FORCE, &pc->pc_flags))
496                                 ptlrpc_abort_set(set);
497                         exit++;
498                 }
499
500                 /*
501                  * Let's make one more loop to make sure that ptlrpcd_check()
502                  * copied all raced new rpcs into the set so we can kill them.
503                  */
504         } while (exit < 2);
505
506         /*
507          * Wait for inflight requests to drain.
508          */
509         if (!list_empty(&set->set_requests))
510                 ptlrpc_set_wait(set);
511         lu_context_fini(&env.le_ctx);
512         lu_context_fini(env.le_ses);
513
514         complete(&pc->pc_finishing);
515
516         return 0;
517
518 failed:
519         pc->pc_error = rc;
520         complete(&pc->pc_starting);
521         RETURN(rc);
522 }
523
524 static void ptlrpcd_ctl_init(struct ptlrpcd_ctl *pc, int index, int cpt)
525 {
526         ENTRY;
527
528         pc->pc_index = index;
529         pc->pc_cpt = cpt;
530         init_completion(&pc->pc_starting);
531         init_completion(&pc->pc_finishing);
532         spin_lock_init(&pc->pc_lock);
533
534         if (index < 0) {
535                 /* Recovery thread. */
536                 snprintf(pc->pc_name, sizeof(pc->pc_name), "ptlrpcd_rcv");
537         } else {
538                 /* Regular thread. */
539                 snprintf(pc->pc_name, sizeof(pc->pc_name),
540                          "ptlrpcd_%02d_%02d", cpt, index);
541         }
542
543         EXIT;
544 }
545
546 /* XXX: We want multiple CPU cores to share the async RPC load. So we
547  *      start many ptlrpcd threads. We also want to reduce the ptlrpcd
548  *      overhead caused by data transfer cross-CPU cores. So we bind
549  *      all ptlrpcd threads to a CPT, in the expectation that CPTs
550  *      will be defined in a way that matches these boundaries. Within
551  *      a CPT a ptlrpcd thread can be scheduled on any available core.
552  *
553  *      Each ptlrpcd thread has its own request queue. This can cause
554  *      response delay if the thread is already busy. To help with
555  *      this we define partner threads: these are other threads bound
556  *      to the same CPT which will check for work in each other's
557  *      request queues if they have no work to do.
558  *
559  *      The desired number of partner threads can be tuned by setting
560  *      ptlrpcd_partner_group_size. The default is to create pairs of
561  *      partner threads.
562  */
563 static int ptlrpcd_partners(struct ptlrpcd *pd, int index)
564 {
565         struct ptlrpcd_ctl      *pc;
566         struct ptlrpcd_ctl      **ppc;
567         int                     first;
568         int                     i;
569         int                     rc = 0;
570         ENTRY;
571
572         LASSERT(index >= 0 && index < pd->pd_nthreads);
573         pc = &pd->pd_threads[index];
574         pc->pc_npartners = pd->pd_groupsize - 1;
575
576         if (pc->pc_npartners <= 0)
577                 GOTO(out, rc);
578
579         OBD_CPT_ALLOC(pc->pc_partners, cfs_cpt_table, pc->pc_cpt,
580                       sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
581         if (pc->pc_partners == NULL) {
582                 pc->pc_npartners = 0;
583                 GOTO(out, rc = -ENOMEM);
584         }
585
586         first = index - index % pd->pd_groupsize;
587         ppc = pc->pc_partners;
588         for (i = first; i < first + pd->pd_groupsize; i++) {
589                 if (i != index)
590                         *ppc++ = &pd->pd_threads[i];
591         }
592 out:
593         RETURN(rc);
594 }
595
596 int ptlrpcd_start(struct ptlrpcd_ctl *pc)
597 {
598         struct task_struct      *task;
599         int                     rc = 0;
600         ENTRY;
601
602         /*
603          * Do not allow starting a second thread for one pc.
604          */
605         if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
606                 CWARN("Starting second thread (%s) for same pc %p\n",
607                       pc->pc_name, pc);
608                 RETURN(0);
609         }
610
611         task = kthread_run(ptlrpcd, pc, pc->pc_name);
612         if (IS_ERR(task))
613                 GOTO(out_set, rc = PTR_ERR(task));
614
615         wait_for_completion(&pc->pc_starting);
616         rc = pc->pc_error;
617         if (rc != 0)
618                 GOTO(out_set, rc);
619
620         RETURN(0);
621
622 out_set:
623         if (pc->pc_set != NULL) {
624                 struct ptlrpc_request_set *set = pc->pc_set;
625
626                 spin_lock(&pc->pc_lock);
627                 pc->pc_set = NULL;
628                 spin_unlock(&pc->pc_lock);
629                 ptlrpc_set_destroy(set);
630         }
631         clear_bit(LIOD_START, &pc->pc_flags);
632         RETURN(rc);
633 }
634
635 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
636 {
637         ENTRY;
638
639         if (!test_bit(LIOD_START, &pc->pc_flags)) {
640                 CWARN("Thread for pc %p was not started\n", pc);
641                 goto out;
642         }
643
644         set_bit(LIOD_STOP, &pc->pc_flags);
645         if (force)
646                 set_bit(LIOD_FORCE, &pc->pc_flags);
647         wake_up(&pc->pc_set->set_waitq);
648
649 out:
650         EXIT;
651 }
652
653 void ptlrpcd_free(struct ptlrpcd_ctl *pc)
654 {
655         struct ptlrpc_request_set *set = pc->pc_set;
656         ENTRY;
657
658         if (!test_bit(LIOD_START, &pc->pc_flags)) {
659                 CWARN("Thread for pc %p was not started\n", pc);
660                 goto out;
661         }
662
663         wait_for_completion(&pc->pc_finishing);
664
665         spin_lock(&pc->pc_lock);
666         pc->pc_set = NULL;
667         spin_unlock(&pc->pc_lock);
668         ptlrpc_set_destroy(set);
669
670         clear_bit(LIOD_START, &pc->pc_flags);
671         clear_bit(LIOD_STOP, &pc->pc_flags);
672         clear_bit(LIOD_FORCE, &pc->pc_flags);
673
674 out:
675         if (pc->pc_npartners > 0) {
676                 LASSERT(pc->pc_partners != NULL);
677
678                 OBD_FREE(pc->pc_partners,
679                          sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
680                 pc->pc_partners = NULL;
681         }
682         pc->pc_npartners = 0;
683         pc->pc_error = 0;
684         EXIT;
685 }
686
687 static void ptlrpcd_fini(void)
688 {
689         int     i;
690         int     j;
691         int     ncpts;
692         ENTRY;
693
694         if (ptlrpcds != NULL) {
695                 for (i = 0; i < ptlrpcds_num; i++) {
696                         if (ptlrpcds[i] == NULL)
697                                 break;
698                         for (j = 0; j < ptlrpcds[i]->pd_nthreads; j++)
699                                 ptlrpcd_stop(&ptlrpcds[i]->pd_threads[j], 0);
700                         for (j = 0; j < ptlrpcds[i]->pd_nthreads; j++)
701                                 ptlrpcd_free(&ptlrpcds[i]->pd_threads[j]);
702                         OBD_FREE(ptlrpcds[i], ptlrpcds[i]->pd_size);
703                         ptlrpcds[i] = NULL;
704                 }
705                 OBD_FREE(ptlrpcds, sizeof(ptlrpcds[0]) * ptlrpcds_num);
706         }
707         ptlrpcds_num = 0;
708
709         ptlrpcd_stop(&ptlrpcd_rcv, 0);
710         ptlrpcd_free(&ptlrpcd_rcv);
711
712         if (ptlrpcds_cpt_idx != NULL) {
713                 ncpts = cfs_cpt_number(cfs_cpt_table);
714                 OBD_FREE(ptlrpcds_cpt_idx, ncpts * sizeof(ptlrpcds_cpt_idx[0]));
715                 ptlrpcds_cpt_idx = NULL;
716         }
717
718         EXIT;
719 }
720
721 static int ptlrpcd_init(void)
722 {
723         int                     nthreads;
724         int                     groupsize;
725         int                     size;
726         int                     i;
727         int                     j;
728         int                     rc = 0;
729         struct cfs_cpt_table    *cptable;
730         __u32                   *cpts = NULL;
731         int                     ncpts;
732         int                     cpt;
733         struct ptlrpcd          *pd;
734         ENTRY;
735
736         /*
737          * Determine the CPTs that ptlrpcd threads will run on.
738          */
739         cptable = cfs_cpt_table;
740         ncpts = cfs_cpt_number(cptable);
741         if (ptlrpcd_cpts != NULL) {
742                 struct cfs_expr_list    *el;
743
744                 size = ncpts * sizeof(ptlrpcds_cpt_idx[0]);
745                 OBD_ALLOC(ptlrpcds_cpt_idx, size);
746                 if (ptlrpcds_cpt_idx == NULL)
747                         GOTO(out, rc = -ENOMEM);
748
749                 rc = cfs_expr_list_parse(ptlrpcd_cpts,
750                                          strlen(ptlrpcd_cpts),
751                                          0, ncpts - 1, &el);
752                 if (rc != 0) {
753                         CERROR("%s: invalid CPT pattern string: %s",
754                                "ptlrpcd_cpts", ptlrpcd_cpts);
755                         GOTO(out, rc = -EINVAL);
756                 }
757
758                 rc = cfs_expr_list_values(el, ncpts, &cpts);
759                 cfs_expr_list_free(el);
760                 if (rc <= 0) {
761                         CERROR("%s: failed to parse CPT array %s: %d\n",
762                                "ptlrpcd_cpts", ptlrpcd_cpts, rc);
763                         if (rc == 0)
764                                 rc = -EINVAL;
765                         GOTO(out, rc);
766                 }
767
768                 /*
769                  * Create the cpt-to-index map. When there is no match
770                  * in the cpt table, pick a cpt at random. This could
771                  * be changed to take the topology of the system into
772                  * account.
773                  */
774                 for (cpt = 0; cpt < ncpts; cpt++) {
775                         for (i = 0; i < rc; i++)
776                                 if (cpts[i] == cpt)
777                                         break;
778                         if (i >= rc)
779                                 i = cpt % rc;
780                         ptlrpcds_cpt_idx[cpt] = i;
781                 }
782
783                 cfs_expr_list_values_free(cpts, rc);
784                 ncpts = rc;
785         }
786         ptlrpcds_num = ncpts;
787
788         size = ncpts * sizeof(ptlrpcds[0]);
789         OBD_ALLOC(ptlrpcds, size);
790         if (ptlrpcds == NULL)
791                 GOTO(out, rc = -ENOMEM);
792
793         /*
794          * The max_ptlrpcds parameter is obsolete, but do something
795          * sane if it has been tuned, and complain if
796          * ptlrpcd_per_cpt_max has also been tuned.
797          */
798         if (max_ptlrpcds != 0) {
799                 CWARN("max_ptlrpcds is obsolete.\n");
800                 if (ptlrpcd_per_cpt_max == 0) {
801                         ptlrpcd_per_cpt_max = max_ptlrpcds / ncpts;
802                         /* Round up if there is a remainder. */
803                         if (max_ptlrpcds % ncpts != 0)
804                                 ptlrpcd_per_cpt_max++;
805                         CWARN("Setting ptlrpcd_per_cpt_max = %d\n",
806                               ptlrpcd_per_cpt_max);
807                 } else {
808                         CWARN("ptlrpd_per_cpt_max is also set!\n");
809                 }
810         }
811
812         /*
813          * The ptlrpcd_bind_policy parameter is obsolete, but do
814          * something sane if it has been tuned, and complain if
815          * ptlrpcd_partner_group_size is also tuned.
816          */
817         if (ptlrpcd_bind_policy != 0) {
818                 CWARN("ptlrpcd_bind_policy is obsolete.\n");
819                 if (ptlrpcd_partner_group_size == 0) {
820                         switch (ptlrpcd_bind_policy) {
821                         case 1: /* PDB_POLICY_NONE */
822                         case 2: /* PDB_POLICY_FULL */
823                                 ptlrpcd_partner_group_size = 1;
824                                 break;
825                         case 3: /* PDB_POLICY_PAIR */
826                                 ptlrpcd_partner_group_size = 2;
827                                 break;
828                         case 4: /* PDB_POLICY_NEIGHBOR */
829 #ifdef CONFIG_NUMA
830                                 ptlrpcd_partner_group_size = -1; /* CPT */
831 #else
832                                 ptlrpcd_partner_group_size = 3; /* Triplets */
833 #endif
834                                 break;
835                         default: /* Illegal value, use the default. */
836                                 ptlrpcd_partner_group_size = 2;
837                                 break;
838                         }
839                         CWARN("Setting ptlrpcd_partner_group_size = %d\n",
840                               ptlrpcd_partner_group_size);
841                 } else {
842                         CWARN("ptlrpcd_partner_group_size is also set!\n");
843                 }
844         }
845
846         if (ptlrpcd_partner_group_size == 0)
847                 ptlrpcd_partner_group_size = 2;
848         else if (ptlrpcd_partner_group_size < 0)
849                 ptlrpcd_partner_group_size = -1;
850         else if (ptlrpcd_per_cpt_max > 0 &&
851                  ptlrpcd_partner_group_size > ptlrpcd_per_cpt_max)
852                 ptlrpcd_partner_group_size = ptlrpcd_per_cpt_max;
853
854         /*
855          * Start the recovery thread first.
856          */
857         set_bit(LIOD_RECOVERY, &ptlrpcd_rcv.pc_flags);
858         ptlrpcd_ctl_init(&ptlrpcd_rcv, -1, CFS_CPT_ANY);
859         rc = ptlrpcd_start(&ptlrpcd_rcv);
860         if (rc < 0)
861                 GOTO(out, rc);
862
863         for (i = 0; i < ncpts; i++) {
864                 if (cpts == NULL)
865                         cpt = i;
866                 else
867                         cpt = cpts[i];
868
869                 nthreads = cfs_cpt_weight(cptable, cpt);
870                 if (ptlrpcd_per_cpt_max > 0 && ptlrpcd_per_cpt_max < nthreads)
871                         nthreads = ptlrpcd_per_cpt_max;
872                 if (nthreads < 2)
873                         nthreads = 2;
874
875                 if (ptlrpcd_partner_group_size <= 0) {
876                         groupsize = nthreads;
877                 } else if (nthreads <= ptlrpcd_partner_group_size) {
878                         groupsize = nthreads;
879                 } else {
880                         groupsize = ptlrpcd_partner_group_size;
881                         if (nthreads % groupsize != 0)
882                                 nthreads += groupsize - (nthreads % groupsize);
883                 }
884
885                 size = offsetof(struct ptlrpcd, pd_threads[nthreads]);
886                 OBD_CPT_ALLOC(pd, cptable, cpt, size);
887                 if (!pd)
888                         GOTO(out, rc = -ENOMEM);
889                 pd->pd_size      = size;
890                 pd->pd_index     = i;
891                 pd->pd_cpt       = cpt;
892                 pd->pd_cursor    = 0;
893                 pd->pd_nthreads  = nthreads;
894                 pd->pd_groupsize = groupsize;
895                 ptlrpcds[i] = pd;
896
897                 /*
898                  * The ptlrpcd threads in a partner group can access
899                  * each other's struct ptlrpcd_ctl, so these must be
900                  * initialized before any thead is started.
901                  */
902                 for (j = 0; j < nthreads; j++) {
903                         ptlrpcd_ctl_init(&pd->pd_threads[j], j, cpt);
904                         rc = ptlrpcd_partners(pd, j);
905                         if (rc < 0)
906                                 GOTO(out, rc);
907                 }
908
909                 /* XXX: We start nthreads ptlrpc daemons on this cpt.
910                  *      Each of them can process any non-recovery
911                  *      async RPC to improve overall async RPC
912                  *      efficiency.
913                  *
914                  *      But there are some issues with async I/O RPCs
915                  *      and async non-I/O RPCs processed in the same
916                  *      set under some cases. The ptlrpcd may be
917                  *      blocked by some async I/O RPC(s), then will
918                  *      cause other async non-I/O RPC(s) can not be
919                  *      processed in time.
920                  *
921                  *      Maybe we should distinguish blocked async RPCs
922                  *      from non-blocked async RPCs, and process them
923                  *      in different ptlrpcd sets to avoid unnecessary
924                  *      dependency. But how to distribute async RPCs
925                  *      load among all the ptlrpc daemons becomes
926                  *      another trouble.
927                  */
928                 for (j = 0; j < nthreads; j++) {
929                         rc = ptlrpcd_start(&pd->pd_threads[j]);
930                         if (rc < 0)
931                                 GOTO(out, rc);
932                 }
933         }
934 out:
935         if (rc != 0)
936                 ptlrpcd_fini();
937
938         RETURN(rc);
939 }
940
941 int ptlrpcd_addref(void)
942 {
943         int rc = 0;
944         ENTRY;
945
946         mutex_lock(&ptlrpcd_mutex);
947         if (++ptlrpcd_users == 1) {
948                 rc = ptlrpcd_init();
949                 if (rc < 0)
950                         ptlrpcd_users--;
951         }
952         mutex_unlock(&ptlrpcd_mutex);
953         RETURN(rc);
954 }
955 EXPORT_SYMBOL(ptlrpcd_addref);
956
957 void ptlrpcd_decref(void)
958 {
959         mutex_lock(&ptlrpcd_mutex);
960         if (--ptlrpcd_users == 0)
961                 ptlrpcd_fini();
962         mutex_unlock(&ptlrpcd_mutex);
963 }
964 EXPORT_SYMBOL(ptlrpcd_decref);
965 /** @} ptlrpcd */