Whamcloud - gitweb
LU-13004 ptlrpc: Allow BULK_BUF_KIOV to accept a kvec
[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, 2017, 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                 /*
228                  * XXX: It maybe unnecessary to wakeup all the partners. But to
229                  *      guarantee the async RPC can be processed ASAP, we have
230                  *      no other better choice. It maybe fixed in future.
231                  */
232                 for (i = 0; i < pc->pc_npartners; i++)
233                         wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
234         }
235 }
236
237 /**
238  * Return transferred RPCs count.
239  */
240 static int ptlrpcd_steal_rqset(struct ptlrpc_request_set *des,
241                                struct ptlrpc_request_set *src)
242 {
243         struct list_head *tmp, *pos;
244         struct ptlrpc_request *req;
245         int rc = 0;
246
247         spin_lock(&src->set_new_req_lock);
248         if (likely(!list_empty(&src->set_new_requests))) {
249                 list_for_each_safe(pos, tmp, &src->set_new_requests) {
250                         req = list_entry(pos, struct ptlrpc_request,
251                                          rq_set_chain);
252                         req->rq_set = des;
253                 }
254                 list_splice_init(&src->set_new_requests,
255                                  &des->set_requests);
256                 rc = atomic_read(&src->set_new_count);
257                 atomic_add(rc, &des->set_remaining);
258                 atomic_set(&src->set_new_count, 0);
259         }
260         spin_unlock(&src->set_new_req_lock);
261         return rc;
262 }
263
264 /**
265  * Requests that are added to the ptlrpcd queue are sent via
266  * ptlrpcd_check->ptlrpc_check_set().
267  */
268 void ptlrpcd_add_req(struct ptlrpc_request *req)
269 {
270         struct ptlrpcd_ctl *pc;
271
272         if (req->rq_reqmsg)
273                 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
274
275         spin_lock(&req->rq_lock);
276         if (req->rq_invalid_rqset) {
277                 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(5),
278                                                      back_to_sleep, NULL);
279
280                 req->rq_invalid_rqset = 0;
281                 spin_unlock(&req->rq_lock);
282                 l_wait_event(req->rq_set_waitq, (req->rq_set == NULL), &lwi);
283         } else if (req->rq_set) {
284                 /*
285                  * If we have a vaid "rq_set", just reuse it to avoid double
286                  * linked.
287                  */
288                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
289                 LASSERT(req->rq_send_state == LUSTRE_IMP_REPLAY);
290
291                 /* ptlrpc_check_set will decrease the count */
292                 atomic_inc(&req->rq_set->set_remaining);
293                 spin_unlock(&req->rq_lock);
294                 wake_up(&req->rq_set->set_waitq);
295                 return;
296         } else {
297                 spin_unlock(&req->rq_lock);
298         }
299
300         pc = ptlrpcd_select_pc(req);
301
302         DEBUG_REQ(D_INFO, req, "add req [%p] to pc [%s+%d]",
303                   req, pc->pc_name, pc->pc_index);
304
305         ptlrpc_set_add_new_req(pc, req);
306 }
307 EXPORT_SYMBOL(ptlrpcd_add_req);
308
309 static inline void ptlrpc_reqset_get(struct ptlrpc_request_set *set)
310 {
311         atomic_inc(&set->set_refcount);
312 }
313
314 /**
315  * Check if there is more work to do on ptlrpcd set.
316  * Returns 1 if yes.
317  */
318 static int ptlrpcd_check(struct lu_env *env, struct ptlrpcd_ctl *pc)
319 {
320         struct list_head *tmp, *pos;
321         struct ptlrpc_request *req;
322         struct ptlrpc_request_set *set = pc->pc_set;
323         int rc = 0;
324         int rc2;
325
326         ENTRY;
327
328         if (atomic_read(&set->set_new_count)) {
329                 spin_lock(&set->set_new_req_lock);
330                 if (likely(!list_empty(&set->set_new_requests))) {
331                         list_splice_init(&set->set_new_requests,
332                                              &set->set_requests);
333                         atomic_add(atomic_read(&set->set_new_count),
334                                    &set->set_remaining);
335                         atomic_set(&set->set_new_count, 0);
336                         /*
337                          * Need to calculate its timeout.
338                          */
339                         rc = 1;
340                 }
341                 spin_unlock(&set->set_new_req_lock);
342         }
343
344         /*
345          * We should call lu_env_refill() before handling new requests to make
346          * sure that env key the requests depending on really exists.
347          */
348         rc2 = lu_env_refill(env);
349         if (rc2 != 0) {
350                 /*
351                  * XXX This is very awkward situation, because
352                  * execution can neither continue (request
353                  * interpreters assume that env is set up), nor repeat
354                  * the loop (as this potentially results in a tight
355                  * loop of -ENOMEM's).
356                  *
357                  * Fortunately, refill only ever does something when
358                  * new modules are loaded, i.e., early during boot up.
359                  */
360                 CERROR("Failure to refill session: %d\n", rc2);
361                 RETURN(rc);
362         }
363
364         if (atomic_read(&set->set_remaining))
365                 rc |= ptlrpc_check_set(env, set);
366
367         /*
368          * NB: ptlrpc_check_set has already moved complted request at the
369          * head of seq::set_requests
370          */
371         list_for_each_safe(pos, tmp, &set->set_requests) {
372                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
373                 if (req->rq_phase != RQ_PHASE_COMPLETE)
374                         break;
375
376                 list_del_init(&req->rq_set_chain);
377                 req->rq_set = NULL;
378                 ptlrpc_req_finished(req);
379         }
380
381         if (rc == 0) {
382                 /*
383                  * If new requests have been added, make sure to wake up.
384                  */
385                 rc = atomic_read(&set->set_new_count);
386
387                 /*
388                  * If we have nothing to do, check whether we can take some
389                  * work from our partner threads.
390                  */
391                 if (rc == 0 && pc->pc_npartners > 0) {
392                         struct ptlrpcd_ctl *partner;
393                         struct ptlrpc_request_set *ps;
394                         int first = pc->pc_cursor;
395
396                         do {
397                                 partner = pc->pc_partners[pc->pc_cursor++];
398                                 if (pc->pc_cursor >= pc->pc_npartners)
399                                         pc->pc_cursor = 0;
400                                 if (partner == NULL)
401                                         continue;
402
403                                 spin_lock(&partner->pc_lock);
404                                 ps = partner->pc_set;
405                                 if (ps == NULL) {
406                                         spin_unlock(&partner->pc_lock);
407                                         continue;
408                                 }
409
410                                 ptlrpc_reqset_get(ps);
411                                 spin_unlock(&partner->pc_lock);
412
413                                 if (atomic_read(&ps->set_new_count)) {
414                                         rc = ptlrpcd_steal_rqset(set, ps);
415                                         if (rc > 0)
416                                                 CDEBUG(D_RPCTRACE,
417                                                        "transfer %d async RPCs [%d->%d]\n",
418                                                        rc, partner->pc_index,
419                                                        pc->pc_index);
420                                 }
421                                 ptlrpc_reqset_put(ps);
422                         } while (rc == 0 && pc->pc_cursor != first);
423                 }
424         }
425
426         RETURN(rc || test_bit(LIOD_STOP, &pc->pc_flags));
427 }
428
429 /**
430  * Main ptlrpcd thread.
431  * ptlrpc's code paths like to execute in process context, so we have this
432  * thread which spins on a set which contains the rpcs and sends them.
433  */
434 static int ptlrpcd(void *arg)
435 {
436         struct ptlrpcd_ctl              *pc = arg;
437         struct ptlrpc_request_set       *set;
438         struct lu_context               ses = { 0 };
439         struct lu_env                   env = { .le_ses = &ses };
440         int                             rc = 0;
441         int                             exit = 0;
442
443         ENTRY;
444
445         unshare_fs_struct();
446
447         if (cfs_cpt_bind(cfs_cpt_table, pc->pc_cpt) != 0)
448                 CWARN("Failed to bind %s on CPT %d\n", pc->pc_name, pc->pc_cpt);
449
450         /*
451          * Allocate the request set after the thread has been bound
452          * above. This is safe because no requests will be queued
453          * until all ptlrpcd threads have confirmed that they have
454          * successfully started.
455          */
456         set = ptlrpc_prep_set();
457         if (set == NULL)
458                 GOTO(failed, rc = -ENOMEM);
459         spin_lock(&pc->pc_lock);
460         pc->pc_set = set;
461         spin_unlock(&pc->pc_lock);
462
463         /* Both client and server (MDT/OST) may use the environment. */
464         rc = lu_context_init(&env.le_ctx, LCT_MD_THREAD |
465                                           LCT_DT_THREAD |
466                                           LCT_CL_THREAD |
467                                           LCT_REMEMBER  |
468                                           LCT_NOREF);
469         if (rc != 0)
470                 GOTO(failed, rc);
471         rc = lu_context_init(env.le_ses, LCT_SESSION  |
472                                          LCT_REMEMBER |
473                                          LCT_NOREF);
474         if (rc != 0) {
475                 lu_context_fini(&env.le_ctx);
476                 GOTO(failed, rc);
477         }
478
479         complete(&pc->pc_starting);
480
481         /*
482          * This mainloop strongly resembles ptlrpc_set_wait() except that our
483          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
484          * there are requests in the set. New requests come in on the set's
485          * new_req_list and ptlrpcd_check() moves them into the set.
486          */
487         do {
488                 struct l_wait_info lwi;
489                 time64_t timeout;
490
491                 timeout = ptlrpc_set_next_timeout(set);
492                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout),
493                                 ptlrpc_expired_set, set);
494
495                 lu_context_enter(&env.le_ctx);
496                 lu_context_enter(env.le_ses);
497                 l_wait_event(set->set_waitq, ptlrpcd_check(&env, pc), &lwi);
498                 lu_context_exit(&env.le_ctx);
499                 lu_context_exit(env.le_ses);
500
501                 /*
502                  * Abort inflight rpcs for forced stop case.
503                  */
504                 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
505                         if (test_bit(LIOD_FORCE, &pc->pc_flags))
506                                 ptlrpc_abort_set(set);
507                         exit++;
508                 }
509
510                 /*
511                  * Let's make one more loop to make sure that ptlrpcd_check()
512                  * copied all raced new rpcs into the set so we can kill them.
513                  */
514         } while (exit < 2);
515
516         /*
517          * Wait for inflight requests to drain.
518          */
519         if (!list_empty(&set->set_requests))
520                 ptlrpc_set_wait(&env, set);
521         lu_context_fini(&env.le_ctx);
522         lu_context_fini(env.le_ses);
523
524         complete(&pc->pc_finishing);
525
526         return 0;
527
528 failed:
529         pc->pc_error = rc;
530         complete(&pc->pc_starting);
531         RETURN(rc);
532 }
533
534 static void ptlrpcd_ctl_init(struct ptlrpcd_ctl *pc, int index, int cpt)
535 {
536         ENTRY;
537
538         pc->pc_index = index;
539         pc->pc_cpt = cpt;
540         init_completion(&pc->pc_starting);
541         init_completion(&pc->pc_finishing);
542         spin_lock_init(&pc->pc_lock);
543
544         if (index < 0) {
545                 /* Recovery thread. */
546                 snprintf(pc->pc_name, sizeof(pc->pc_name), "ptlrpcd_rcv");
547         } else {
548                 /* Regular thread. */
549                 snprintf(pc->pc_name, sizeof(pc->pc_name),
550                          "ptlrpcd_%02d_%02d", cpt, index);
551         }
552
553         EXIT;
554 }
555
556 /* XXX: We want multiple CPU cores to share the async RPC load. So we
557  *      start many ptlrpcd threads. We also want to reduce the ptlrpcd
558  *      overhead caused by data transfer cross-CPU cores. So we bind
559  *      all ptlrpcd threads to a CPT, in the expectation that CPTs
560  *      will be defined in a way that matches these boundaries. Within
561  *      a CPT a ptlrpcd thread can be scheduled on any available core.
562  *
563  *      Each ptlrpcd thread has its own request queue. This can cause
564  *      response delay if the thread is already busy. To help with
565  *      this we define partner threads: these are other threads bound
566  *      to the same CPT which will check for work in each other's
567  *      request queues if they have no work to do.
568  *
569  *      The desired number of partner threads can be tuned by setting
570  *      ptlrpcd_partner_group_size. The default is to create pairs of
571  *      partner threads.
572  */
573 static int ptlrpcd_partners(struct ptlrpcd *pd, int index)
574 {
575         struct ptlrpcd_ctl      *pc;
576         struct ptlrpcd_ctl      **ppc;
577         int                     first;
578         int                     i;
579         int                     rc = 0;
580
581         ENTRY;
582
583         LASSERT(index >= 0 && index < pd->pd_nthreads);
584         pc = &pd->pd_threads[index];
585         pc->pc_npartners = pd->pd_groupsize - 1;
586
587         if (pc->pc_npartners <= 0)
588                 GOTO(out, rc);
589
590         OBD_CPT_ALLOC(pc->pc_partners, cfs_cpt_table, pc->pc_cpt,
591                       sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
592         if (pc->pc_partners == NULL) {
593                 pc->pc_npartners = 0;
594                 GOTO(out, rc = -ENOMEM);
595         }
596
597         first = index - index % pd->pd_groupsize;
598         ppc = pc->pc_partners;
599         for (i = first; i < first + pd->pd_groupsize; i++) {
600                 if (i != index)
601                         *ppc++ = &pd->pd_threads[i];
602         }
603 out:
604         RETURN(rc);
605 }
606
607 int ptlrpcd_start(struct ptlrpcd_ctl *pc)
608 {
609         struct task_struct      *task;
610         int                     rc = 0;
611
612         ENTRY;
613
614         /*
615          * Do not allow starting a second thread for one pc.
616          */
617         if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
618                 CWARN("Starting second thread (%s) for same pc %p\n",
619                       pc->pc_name, pc);
620                 RETURN(0);
621         }
622
623         task = kthread_run(ptlrpcd, pc, pc->pc_name);
624         if (IS_ERR(task))
625                 GOTO(out_set, rc = PTR_ERR(task));
626
627         wait_for_completion(&pc->pc_starting);
628         rc = pc->pc_error;
629         if (rc != 0)
630                 GOTO(out_set, rc);
631
632         RETURN(0);
633
634 out_set:
635         if (pc->pc_set != NULL) {
636                 struct ptlrpc_request_set *set = pc->pc_set;
637
638                 spin_lock(&pc->pc_lock);
639                 pc->pc_set = NULL;
640                 spin_unlock(&pc->pc_lock);
641                 ptlrpc_set_destroy(set);
642         }
643         clear_bit(LIOD_START, &pc->pc_flags);
644         RETURN(rc);
645 }
646
647 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
648 {
649         ENTRY;
650
651         if (!test_bit(LIOD_START, &pc->pc_flags)) {
652                 CWARN("Thread for pc %p was not started\n", pc);
653                 goto out;
654         }
655
656         set_bit(LIOD_STOP, &pc->pc_flags);
657         if (force)
658                 set_bit(LIOD_FORCE, &pc->pc_flags);
659         wake_up(&pc->pc_set->set_waitq);
660
661 out:
662         EXIT;
663 }
664
665 void ptlrpcd_free(struct ptlrpcd_ctl *pc)
666 {
667         struct ptlrpc_request_set *set = pc->pc_set;
668
669         ENTRY;
670
671         if (!test_bit(LIOD_START, &pc->pc_flags)) {
672                 CWARN("Thread for pc %p was not started\n", pc);
673                 goto out;
674         }
675
676         wait_for_completion(&pc->pc_finishing);
677
678         spin_lock(&pc->pc_lock);
679         pc->pc_set = NULL;
680         spin_unlock(&pc->pc_lock);
681         ptlrpc_set_destroy(set);
682
683         clear_bit(LIOD_START, &pc->pc_flags);
684         clear_bit(LIOD_STOP, &pc->pc_flags);
685         clear_bit(LIOD_FORCE, &pc->pc_flags);
686
687 out:
688         if (pc->pc_npartners > 0) {
689                 LASSERT(pc->pc_partners != NULL);
690
691                 OBD_FREE(pc->pc_partners,
692                          sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
693                 pc->pc_partners = NULL;
694         }
695         pc->pc_npartners = 0;
696         pc->pc_error = 0;
697         EXIT;
698 }
699
700 static void ptlrpcd_fini(void)
701 {
702         int     i;
703         int     j;
704         int     ncpts;
705
706         ENTRY;
707
708         if (ptlrpcds != NULL) {
709                 for (i = 0; i < ptlrpcds_num; i++) {
710                         if (ptlrpcds[i] == NULL)
711                                 break;
712                         for (j = 0; j < ptlrpcds[i]->pd_nthreads; j++)
713                                 ptlrpcd_stop(&ptlrpcds[i]->pd_threads[j], 0);
714                         for (j = 0; j < ptlrpcds[i]->pd_nthreads; j++)
715                                 ptlrpcd_free(&ptlrpcds[i]->pd_threads[j]);
716                         OBD_FREE(ptlrpcds[i], ptlrpcds[i]->pd_size);
717                         ptlrpcds[i] = NULL;
718                 }
719                 OBD_FREE(ptlrpcds, sizeof(ptlrpcds[0]) * ptlrpcds_num);
720         }
721         ptlrpcds_num = 0;
722
723         ptlrpcd_stop(&ptlrpcd_rcv, 0);
724         ptlrpcd_free(&ptlrpcd_rcv);
725
726         if (ptlrpcds_cpt_idx != NULL) {
727                 ncpts = cfs_cpt_number(cfs_cpt_table);
728                 OBD_FREE(ptlrpcds_cpt_idx, ncpts * sizeof(ptlrpcds_cpt_idx[0]));
729                 ptlrpcds_cpt_idx = NULL;
730         }
731
732         EXIT;
733 }
734
735 static int ptlrpcd_init(void)
736 {
737         int                     nthreads;
738         int                     groupsize;
739         int                     size;
740         int                     i;
741         int                     j;
742         int                     rc = 0;
743         struct cfs_cpt_table    *cptable;
744         __u32                   *cpts = NULL;
745         int                     ncpts;
746         int                     cpt;
747         struct ptlrpcd          *pd;
748
749         ENTRY;
750
751         /*
752          * Determine the CPTs that ptlrpcd threads will run on.
753          */
754         cptable = cfs_cpt_table;
755         ncpts = cfs_cpt_number(cptable);
756         if (ptlrpcd_cpts != NULL) {
757                 struct cfs_expr_list *el;
758
759                 size = ncpts * sizeof(ptlrpcds_cpt_idx[0]);
760                 OBD_ALLOC(ptlrpcds_cpt_idx, size);
761                 if (ptlrpcds_cpt_idx == NULL)
762                         GOTO(out, rc = -ENOMEM);
763
764                 rc = cfs_expr_list_parse(ptlrpcd_cpts,
765                                          strlen(ptlrpcd_cpts),
766                                          0, ncpts - 1, &el);
767                 if (rc != 0) {
768                         CERROR("%s: invalid CPT pattern string: %s",
769                                "ptlrpcd_cpts", ptlrpcd_cpts);
770                         GOTO(out, rc = -EINVAL);
771                 }
772
773                 rc = cfs_expr_list_values(el, ncpts, &cpts);
774                 cfs_expr_list_free(el);
775                 if (rc <= 0) {
776                         CERROR("%s: failed to parse CPT array %s: %d\n",
777                                "ptlrpcd_cpts", ptlrpcd_cpts, rc);
778                         if (rc == 0)
779                                 rc = -EINVAL;
780                         GOTO(out, rc);
781                 }
782
783                 /*
784                  * Create the cpt-to-index map. When there is no match
785                  * in the cpt table, pick a cpt at random. This could
786                  * be changed to take the topology of the system into
787                  * account.
788                  */
789                 for (cpt = 0; cpt < ncpts; cpt++) {
790                         for (i = 0; i < rc; i++)
791                                 if (cpts[i] == cpt)
792                                         break;
793                         if (i >= rc)
794                                 i = cpt % rc;
795                         ptlrpcds_cpt_idx[cpt] = i;
796                 }
797
798                 cfs_expr_list_values_free(cpts, rc);
799                 ncpts = rc;
800         }
801         ptlrpcds_num = ncpts;
802
803         size = ncpts * sizeof(ptlrpcds[0]);
804         OBD_ALLOC(ptlrpcds, size);
805         if (ptlrpcds == NULL)
806                 GOTO(out, rc = -ENOMEM);
807
808         /*
809          * The max_ptlrpcds parameter is obsolete, but do something
810          * sane if it has been tuned, and complain if
811          * ptlrpcd_per_cpt_max has also been tuned.
812          */
813         if (max_ptlrpcds != 0) {
814                 CWARN("max_ptlrpcds is obsolete.\n");
815                 if (ptlrpcd_per_cpt_max == 0) {
816                         ptlrpcd_per_cpt_max = max_ptlrpcds / ncpts;
817                         /* Round up if there is a remainder. */
818                         if (max_ptlrpcds % ncpts != 0)
819                                 ptlrpcd_per_cpt_max++;
820                         CWARN("Setting ptlrpcd_per_cpt_max = %d\n",
821                               ptlrpcd_per_cpt_max);
822                 } else {
823                         CWARN("ptlrpd_per_cpt_max is also set!\n");
824                 }
825         }
826
827         /*
828          * The ptlrpcd_bind_policy parameter is obsolete, but do
829          * something sane if it has been tuned, and complain if
830          * ptlrpcd_partner_group_size is also tuned.
831          */
832         if (ptlrpcd_bind_policy != 0) {
833                 CWARN("ptlrpcd_bind_policy is obsolete.\n");
834                 if (ptlrpcd_partner_group_size == 0) {
835                         switch (ptlrpcd_bind_policy) {
836                         case 1: /* PDB_POLICY_NONE */
837                         case 2: /* PDB_POLICY_FULL */
838                                 ptlrpcd_partner_group_size = 1;
839                                 break;
840                         case 3: /* PDB_POLICY_PAIR */
841                                 ptlrpcd_partner_group_size = 2;
842                                 break;
843                         case 4: /* PDB_POLICY_NEIGHBOR */
844 #ifdef CONFIG_NUMA
845                                 ptlrpcd_partner_group_size = -1; /* CPT */
846 #else
847                                 ptlrpcd_partner_group_size = 3; /* Triplets */
848 #endif
849                                 break;
850                         default: /* Illegal value, use the default. */
851                                 ptlrpcd_partner_group_size = 2;
852                                 break;
853                         }
854                         CWARN("Setting ptlrpcd_partner_group_size = %d\n",
855                               ptlrpcd_partner_group_size);
856                 } else {
857                         CWARN("ptlrpcd_partner_group_size is also set!\n");
858                 }
859         }
860
861         if (ptlrpcd_partner_group_size == 0)
862                 ptlrpcd_partner_group_size = 2;
863         else if (ptlrpcd_partner_group_size < 0)
864                 ptlrpcd_partner_group_size = -1;
865         else if (ptlrpcd_per_cpt_max > 0 &&
866                  ptlrpcd_partner_group_size > ptlrpcd_per_cpt_max)
867                 ptlrpcd_partner_group_size = ptlrpcd_per_cpt_max;
868
869         /*
870          * Start the recovery thread first.
871          */
872         set_bit(LIOD_RECOVERY, &ptlrpcd_rcv.pc_flags);
873         ptlrpcd_ctl_init(&ptlrpcd_rcv, -1, CFS_CPT_ANY);
874         rc = ptlrpcd_start(&ptlrpcd_rcv);
875         if (rc < 0)
876                 GOTO(out, rc);
877
878         for (i = 0; i < ncpts; i++) {
879                 if (cpts == NULL)
880                         cpt = i;
881                 else
882                         cpt = cpts[i];
883
884                 nthreads = cfs_cpt_weight(cptable, cpt);
885                 if (ptlrpcd_per_cpt_max > 0 && ptlrpcd_per_cpt_max < nthreads)
886                         nthreads = ptlrpcd_per_cpt_max;
887                 if (nthreads < 2)
888                         nthreads = 2;
889
890                 if (ptlrpcd_partner_group_size <= 0) {
891                         groupsize = nthreads;
892                 } else if (nthreads <= ptlrpcd_partner_group_size) {
893                         groupsize = nthreads;
894                 } else {
895                         groupsize = ptlrpcd_partner_group_size;
896                         if (nthreads % groupsize != 0)
897                                 nthreads += groupsize - (nthreads % groupsize);
898                 }
899
900                 size = offsetof(struct ptlrpcd, pd_threads[nthreads]);
901                 OBD_CPT_ALLOC(pd, cptable, cpt, size);
902
903                 if (!pd)
904                         GOTO(out, rc = -ENOMEM);
905                 pd->pd_size      = size;
906                 pd->pd_index     = i;
907                 pd->pd_cpt       = cpt;
908                 pd->pd_cursor    = 0;
909                 pd->pd_nthreads  = nthreads;
910                 pd->pd_groupsize = groupsize;
911                 ptlrpcds[i] = pd;
912
913                 /*
914                  * The ptlrpcd threads in a partner group can access
915                  * each other's struct ptlrpcd_ctl, so these must be
916                  * initialized before any thead is started.
917                  */
918                 for (j = 0; j < nthreads; j++) {
919                         ptlrpcd_ctl_init(&pd->pd_threads[j], j, cpt);
920                         rc = ptlrpcd_partners(pd, j);
921                         if (rc < 0)
922                                 GOTO(out, rc);
923                 }
924
925                 /* XXX: We start nthreads ptlrpc daemons on this cpt.
926                  *      Each of them can process any non-recovery
927                  *      async RPC to improve overall async RPC
928                  *      efficiency.
929                  *
930                  *      But there are some issues with async I/O RPCs
931                  *      and async non-I/O RPCs processed in the same
932                  *      set under some cases. The ptlrpcd may be
933                  *      blocked by some async I/O RPC(s), then will
934                  *      cause other async non-I/O RPC(s) can not be
935                  *      processed in time.
936                  *
937                  *      Maybe we should distinguish blocked async RPCs
938                  *      from non-blocked async RPCs, and process them
939                  *      in different ptlrpcd sets to avoid unnecessary
940                  *      dependency. But how to distribute async RPCs
941                  *      load among all the ptlrpc daemons becomes
942                  *      another trouble.
943                  */
944                 for (j = 0; j < nthreads; j++) {
945                         rc = ptlrpcd_start(&pd->pd_threads[j]);
946                         if (rc < 0)
947                                 GOTO(out, rc);
948                 }
949         }
950 out:
951         if (rc != 0)
952                 ptlrpcd_fini();
953
954         RETURN(rc);
955 }
956
957 int ptlrpcd_addref(void)
958 {
959         int rc = 0;
960
961         ENTRY;
962
963         mutex_lock(&ptlrpcd_mutex);
964         if (++ptlrpcd_users == 1) {
965                 rc = ptlrpcd_init();
966                 if (rc < 0)
967                         ptlrpcd_users--;
968         }
969         mutex_unlock(&ptlrpcd_mutex);
970         RETURN(rc);
971 }
972 EXPORT_SYMBOL(ptlrpcd_addref);
973
974 void ptlrpcd_decref(void)
975 {
976         mutex_lock(&ptlrpcd_mutex);
977         if (--ptlrpcd_users == 0)
978                 ptlrpcd_fini();
979         mutex_unlock(&ptlrpcd_mutex);
980 }
981 EXPORT_SYMBOL(ptlrpcd_decref);
982 /** @} ptlrpcd */