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