Whamcloud - gitweb
c364f405101f06c2b76347dfde0897ed28a2f219
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/ptlrpcd.c
37  */
38
39 /** \defgroup ptlrpcd PortalRPC daemon
40  *
41  * ptlrpcd is a special thread with its own set where other user might add
42  * requests when they don't want to wait for their completion.
43  * PtlRPCD will take care of sending such requests and then processing their
44  * replies and calling completion callbacks as necessary.
45  * The callbacks are called directly from ptlrpcd context.
46  * It is important to never significantly block (esp. on RPCs!) within such
47  * completion handler or a deadlock might occur where ptlrpcd enters some
48  * callback that attempts to send another RPC and wait for it to return,
49  * during which time ptlrpcd is completely blocked, so e.g. if import
50  * fails, recovery cannot progress because connection requests are also
51  * sent by ptlrpcd.
52  *
53  * @{
54  */
55
56 #define DEBUG_SUBSYSTEM S_RPC
57
58 #ifdef __KERNEL__
59 # include <libcfs/libcfs.h>
60 #else /* __KERNEL__ */
61 # include <liblustre.h>
62 # include <ctype.h>
63 #endif
64
65 #include <lustre_net.h>
66 # include <lustre_lib.h>
67
68 #include <lustre_ha.h>
69 #include <obd_class.h>   /* for obd_zombie */
70 #include <obd_support.h> /* for OBD_FAIL_CHECK */
71 #include <cl_object.h> /* cl_env_{get,put}() */
72 #include <lprocfs_status.h>
73
74 #include "ptlrpc_internal.h"
75
76 struct ptlrpcd {
77         int                pd_size;
78         int                pd_index;
79         int                pd_nthreads;
80         struct ptlrpcd_ctl pd_thread_rcv;
81         struct ptlrpcd_ctl pd_threads[0];
82 };
83
84 #ifdef __KERNEL__
85 static int max_ptlrpcds;
86 CFS_MODULE_PARM(max_ptlrpcds, "i", int, 0644,
87                 "Max ptlrpcd thread count to be started.");
88
89 static int ptlrpcd_bind_policy = PDB_POLICY_PAIR;
90 CFS_MODULE_PARM(ptlrpcd_bind_policy, "i", int, 0644,
91                 "Ptlrpcd threads binding mode.");
92 #endif
93 static struct ptlrpcd *ptlrpcds;
94
95 struct mutex ptlrpcd_mutex;
96 static int ptlrpcd_users = 0;
97
98 void ptlrpcd_wake(struct ptlrpc_request *req)
99 {
100         struct ptlrpc_request_set *rq_set = req->rq_set;
101
102         LASSERT(rq_set != NULL);
103
104         wake_up(&rq_set->set_waitq);
105 }
106 EXPORT_SYMBOL(ptlrpcd_wake);
107
108 static struct ptlrpcd_ctl *
109 ptlrpcd_select_pc(struct ptlrpc_request *req, pdl_policy_t policy, int index)
110 {
111         int idx = 0;
112
113         if (req != NULL && req->rq_send_state != LUSTRE_IMP_FULL)
114                 return &ptlrpcds->pd_thread_rcv;
115
116 #ifdef __KERNEL__
117         switch (policy) {
118         case PDL_POLICY_SAME:
119                 idx = smp_processor_id() % ptlrpcds->pd_nthreads;
120                 break;
121         case PDL_POLICY_LOCAL:
122                 /* Before CPU partition patches available, process it the same
123                  * as "PDL_POLICY_ROUND". */
124 # ifdef CFS_CPU_MODE_NUMA
125 # warning "fix this code to use new CPU partition APIs"
126 # endif
127                 /* Fall through to PDL_POLICY_ROUND until the CPU
128                  * CPU partition patches are available. */
129                 index = -1;
130         case PDL_POLICY_PREFERRED:
131                 if (index >= 0 && index < num_online_cpus()) {
132                         idx = index % ptlrpcds->pd_nthreads;
133                         break;
134                 }
135                 /* Fall through to PDL_POLICY_ROUND for bad index. */
136         default:
137                 /* Fall through to PDL_POLICY_ROUND for unknown policy. */
138         case PDL_POLICY_ROUND:
139                 /* We do not care whether it is strict load balance. */
140                 idx = ptlrpcds->pd_index + 1;
141                 if (idx == smp_processor_id())
142                         idx++;
143                 idx %= ptlrpcds->pd_nthreads;
144                 ptlrpcds->pd_index = idx;
145                 break;
146         }
147 #endif /* __KERNEL__ */
148
149         return &ptlrpcds->pd_threads[idx];
150 }
151
152 /**
153  * Move all request from an existing request set to the ptlrpcd queue.
154  * All requests from the set must be in phase RQ_PHASE_NEW.
155  */
156 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set)
157 {
158         cfs_list_t *tmp, *pos;
159 #ifdef __KERNEL__
160         struct ptlrpcd_ctl *pc;
161         struct ptlrpc_request_set *new;
162         int count, i;
163
164         pc = ptlrpcd_select_pc(NULL, PDL_POLICY_LOCAL, -1);
165         new = pc->pc_set;
166 #endif
167
168         cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
169                 struct ptlrpc_request *req =
170                         cfs_list_entry(pos, struct ptlrpc_request,
171                                        rq_set_chain);
172
173                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
174 #ifdef __KERNEL__
175                 req->rq_set = new;
176                 req->rq_queued_time = cfs_time_current();
177 #else
178                 cfs_list_del_init(&req->rq_set_chain);
179                 req->rq_set = NULL;
180                 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
181                 cfs_atomic_dec(&set->set_remaining);
182 #endif
183         }
184
185 #ifdef __KERNEL__
186         spin_lock(&new->set_new_req_lock);
187         cfs_list_splice_init(&set->set_requests, &new->set_new_requests);
188         i = cfs_atomic_read(&set->set_remaining);
189         count = cfs_atomic_add_return(i, &new->set_new_count);
190         cfs_atomic_set(&set->set_remaining, 0);
191         spin_unlock(&new->set_new_req_lock);
192         if (count == i) {
193                 wake_up(&new->set_waitq);
194
195                 /* XXX: It maybe unnecessary to wakeup all the partners. But to
196                  *      guarantee the async RPC can be processed ASAP, we have
197                  *      no other better choice. It maybe fixed in future. */
198                 for (i = 0; i < pc->pc_npartners; i++)
199                         wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
200         }
201 #endif
202 }
203 EXPORT_SYMBOL(ptlrpcd_add_rqset);
204
205 #ifdef __KERNEL__
206 /**
207  * Return transferred RPCs count.
208  */
209 static int ptlrpcd_steal_rqset(struct ptlrpc_request_set *des,
210                                struct ptlrpc_request_set *src)
211 {
212         cfs_list_t *tmp, *pos;
213         struct ptlrpc_request *req;
214         int rc = 0;
215
216         spin_lock(&src->set_new_req_lock);
217         if (likely(!cfs_list_empty(&src->set_new_requests))) {
218                 cfs_list_for_each_safe(pos, tmp, &src->set_new_requests) {
219                         req = cfs_list_entry(pos, struct ptlrpc_request,
220                                              rq_set_chain);
221                         req->rq_set = des;
222                 }
223                 cfs_list_splice_init(&src->set_new_requests,
224                                      &des->set_requests);
225                 rc = cfs_atomic_read(&src->set_new_count);
226                 cfs_atomic_add(rc, &des->set_remaining);
227                 cfs_atomic_set(&src->set_new_count, 0);
228         }
229         spin_unlock(&src->set_new_req_lock);
230         return rc;
231 }
232 #endif
233
234 /**
235  * Requests that are added to the ptlrpcd queue are sent via
236  * ptlrpcd_check->ptlrpc_check_set().
237  */
238 void ptlrpcd_add_req(struct ptlrpc_request *req, pdl_policy_t policy, int idx)
239 {
240         struct ptlrpcd_ctl *pc;
241
242         if (req->rq_reqmsg)
243                 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
244
245         spin_lock(&req->rq_lock);
246         if (req->rq_invalid_rqset) {
247                 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(5),
248                                                      back_to_sleep, NULL);
249
250                 req->rq_invalid_rqset = 0;
251                 spin_unlock(&req->rq_lock);
252                 l_wait_event(req->rq_set_waitq, (req->rq_set == NULL), &lwi);
253         } else if (req->rq_set) {
254                 /* If we have a vaid "rq_set", just reuse it to avoid double
255                  * linked. */
256                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
257                 LASSERT(req->rq_send_state == LUSTRE_IMP_REPLAY);
258
259                 /* ptlrpc_check_set will decrease the count */
260                 cfs_atomic_inc(&req->rq_set->set_remaining);
261                 spin_unlock(&req->rq_lock);
262                 wake_up(&req->rq_set->set_waitq);
263                 return;
264         } else {
265                 spin_unlock(&req->rq_lock);
266         }
267
268         pc = ptlrpcd_select_pc(req, policy, idx);
269
270         DEBUG_REQ(D_INFO, req, "add req [%p] to pc [%s:%d]",
271                   req, pc->pc_name, pc->pc_index);
272
273         ptlrpc_set_add_new_req(pc, req);
274 }
275 EXPORT_SYMBOL(ptlrpcd_add_req);
276
277 static inline void ptlrpc_reqset_get(struct ptlrpc_request_set *set)
278 {
279         cfs_atomic_inc(&set->set_refcount);
280 }
281
282 /**
283  * Check if there is more work to do on ptlrpcd set.
284  * Returns 1 if yes.
285  */
286 static int ptlrpcd_check(struct lu_env *env, struct ptlrpcd_ctl *pc)
287 {
288         cfs_list_t *tmp, *pos;
289         struct ptlrpc_request *req;
290         struct ptlrpc_request_set *set = pc->pc_set;
291         int rc = 0;
292         int rc2;
293         ENTRY;
294
295         if (cfs_atomic_read(&set->set_new_count)) {
296                 spin_lock(&set->set_new_req_lock);
297                 if (likely(!cfs_list_empty(&set->set_new_requests))) {
298                         cfs_list_splice_init(&set->set_new_requests,
299                                              &set->set_requests);
300                         cfs_atomic_add(cfs_atomic_read(&set->set_new_count),
301                                        &set->set_remaining);
302                         cfs_atomic_set(&set->set_new_count, 0);
303                         /*
304                          * Need to calculate its timeout.
305                          */
306                         rc = 1;
307                 }
308                 spin_unlock(&set->set_new_req_lock);
309         }
310
311         /* We should call lu_env_refill() before handling new requests to make
312          * sure that env key the requests depending on really exists.
313          */
314         rc2 = lu_env_refill(env);
315         if (rc2 != 0) {
316                 /*
317                  * XXX This is very awkward situation, because
318                  * execution can neither continue (request
319                  * interpreters assume that env is set up), nor repeat
320                  * the loop (as this potentially results in a tight
321                  * loop of -ENOMEM's).
322                  *
323                  * Fortunately, refill only ever does something when
324                  * new modules are loaded, i.e., early during boot up.
325                  */
326                 CERROR("Failure to refill session: %d\n", rc2);
327                 RETURN(rc);
328         }
329
330         if (cfs_atomic_read(&set->set_remaining))
331                 rc |= ptlrpc_check_set(env, set);
332
333         if (!cfs_list_empty(&set->set_requests)) {
334                 /*
335                  * XXX: our set never completes, so we prune the completed
336                  * reqs after each iteration. boy could this be smarter.
337                  */
338                 cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
339                         req = cfs_list_entry(pos, struct ptlrpc_request,
340                                              rq_set_chain);
341                         if (req->rq_phase != RQ_PHASE_COMPLETE)
342                                 continue;
343
344                         cfs_list_del_init(&req->rq_set_chain);
345                         req->rq_set = NULL;
346                         ptlrpc_req_finished(req);
347                 }
348         }
349
350         if (rc == 0) {
351                 /*
352                  * If new requests have been added, make sure to wake up.
353                  */
354                 rc = cfs_atomic_read(&set->set_new_count);
355
356 #ifdef __KERNEL__
357                 /* If we have nothing to do, check whether we can take some
358                  * work from our partner threads. */
359                 if (rc == 0 && pc->pc_npartners > 0) {
360                         struct ptlrpcd_ctl *partner;
361                         struct ptlrpc_request_set *ps;
362                         int first = pc->pc_cursor;
363
364                         do {
365                                 partner = pc->pc_partners[pc->pc_cursor++];
366                                 if (pc->pc_cursor >= pc->pc_npartners)
367                                         pc->pc_cursor = 0;
368                                 if (partner == NULL)
369                                         continue;
370
371                                 spin_lock(&partner->pc_lock);
372                                 ps = partner->pc_set;
373                                 if (ps == NULL) {
374                                         spin_unlock(&partner->pc_lock);
375                                         continue;
376                                 }
377
378                                 ptlrpc_reqset_get(ps);
379                                 spin_unlock(&partner->pc_lock);
380
381                                 if (cfs_atomic_read(&ps->set_new_count)) {
382                                         rc = ptlrpcd_steal_rqset(set, ps);
383                                         if (rc > 0)
384                                                 CDEBUG(D_RPCTRACE, "transfer %d"
385                                                        " async RPCs [%d->%d]\n",
386                                                         rc, partner->pc_index,
387                                                         pc->pc_index);
388                                 }
389                                 ptlrpc_reqset_put(ps);
390                         } while (rc == 0 && pc->pc_cursor != first);
391                 }
392 #endif
393         }
394
395         RETURN(rc);
396 }
397
398 #ifdef __KERNEL__
399 /**
400  * Main ptlrpcd thread.
401  * ptlrpc's code paths like to execute in process context, so we have this
402  * thread which spins on a set which contains the rpcs and sends them.
403  *
404  */
405 static int ptlrpcd(void *arg)
406 {
407         struct ptlrpcd_ctl *pc = arg;
408         struct ptlrpc_request_set *set = pc->pc_set;
409         struct lu_context ses = { 0 };
410         struct lu_env env = { .le_ses = &ses };
411         int rc, exit = 0;
412         ENTRY;
413
414         unshare_fs_struct();
415 #if defined(CONFIG_SMP)
416         if (test_bit(LIOD_BIND, &pc->pc_flags)) {
417                 int index = pc->pc_index;
418
419                 if (index >= 0 && index < num_possible_cpus()) {
420                         while (!cpu_online(index)) {
421                                 if (++index >= num_possible_cpus())
422                                         index = 0;
423                         }
424                         set_cpus_allowed_ptr(current,
425                                      cpumask_of_node(cpu_to_node(index)));
426                 }
427         }
428 #endif
429         /*
430          * XXX So far only "client" ptlrpcd uses an environment. In
431          * the future, ptlrpcd thread (or a thread-set) has to given
432          * an argument, describing its "scope".
433          */
434         rc = lu_context_init(&env.le_ctx,
435                              LCT_CL_THREAD|LCT_REMEMBER|LCT_NOREF);
436         if (rc == 0) {
437                 rc = lu_context_init(env.le_ses,
438                                      LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
439                 if (rc != 0)
440                         lu_context_fini(&env.le_ctx);
441         }
442         complete(&pc->pc_starting);
443
444         if (rc != 0)
445                 RETURN(rc);
446
447         /*
448          * This mainloop strongly resembles ptlrpc_set_wait() except that our
449          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
450          * there are requests in the set. New requests come in on the set's
451          * new_req_list and ptlrpcd_check() moves them into the set.
452          */
453         do {
454                 struct l_wait_info lwi;
455                 int timeout;
456
457                 timeout = ptlrpc_set_next_timeout(set);
458                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
459                                   ptlrpc_expired_set, set);
460
461                 lu_context_enter(&env.le_ctx);
462                 lu_context_enter(env.le_ses);
463                 l_wait_event(set->set_waitq, ptlrpcd_check(&env, pc), &lwi);
464                 lu_context_exit(&env.le_ctx);
465                 lu_context_exit(env.le_ses);
466
467                 /*
468                  * Abort inflight rpcs for forced stop case.
469                  */
470                 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
471                         if (test_bit(LIOD_FORCE, &pc->pc_flags))
472                                 ptlrpc_abort_set(set);
473                         exit++;
474                 }
475
476                 /*
477                  * Let's make one more loop to make sure that ptlrpcd_check()
478                  * copied all raced new rpcs into the set so we can kill them.
479                  */
480         } while (exit < 2);
481
482         /*
483          * Wait for inflight requests to drain.
484          */
485         if (!cfs_list_empty(&set->set_requests))
486                 ptlrpc_set_wait(set);
487         lu_context_fini(&env.le_ctx);
488         lu_context_fini(env.le_ses);
489
490         complete(&pc->pc_finishing);
491
492         return 0;
493 }
494
495 /* XXX: We want multiple CPU cores to share the async RPC load. So we start many
496  *      ptlrpcd threads. We also want to reduce the ptlrpcd overhead caused by
497  *      data transfer cross-CPU cores. So we bind ptlrpcd thread to specified
498  *      CPU core. But binding all ptlrpcd threads maybe cause response delay
499  *      because of some CPU core(s) busy with other loads.
500  *
501  *      For example: "ls -l", some async RPCs for statahead are assigned to
502  *      ptlrpcd_0, and ptlrpcd_0 is bound to CPU_0, but CPU_0 may be quite busy
503  *      with other non-ptlrpcd, like "ls -l" itself (we want to the "ls -l"
504  *      thread, statahead thread, and ptlrpcd thread can run in parallel), under
505  *      such case, the statahead async RPCs can not be processed in time, it is
506  *      unexpected. If ptlrpcd_0 can be re-scheduled on other CPU core, it may
507  *      be better. But it breaks former data transfer policy.
508  *
509  *      So we shouldn't be blind for avoiding the data transfer. We make some
510  *      compromise: divide the ptlrpcd threds pool into two parts. One part is
511  *      for bound mode, each ptlrpcd thread in this part is bound to some CPU
512  *      core. The other part is for free mode, all the ptlrpcd threads in the
513  *      part can be scheduled on any CPU core. We specify some partnership
514  *      between bound mode ptlrpcd thread(s) and free mode ptlrpcd thread(s),
515  *      and the async RPC load within the partners are shared.
516  *
517  *      It can partly avoid data transfer cross-CPU (if the bound mode ptlrpcd
518  *      thread can be scheduled in time), and try to guarantee the async RPC
519  *      processed ASAP (as long as the free mode ptlrpcd thread can be scheduled
520  *      on any CPU core).
521  *
522  *      As for how to specify the partnership between bound mode ptlrpcd
523  *      thread(s) and free mode ptlrpcd thread(s), the simplest way is to use
524  *      <free bound> pair. In future, we can specify some more complex
525  *      partnership based on the patches for CPU partition. But before such
526  *      patches are available, we prefer to use the simplest one.
527  */
528 # ifdef CFS_CPU_MODE_NUMA
529 # warning "fix ptlrpcd_bind() to use new CPU partition APIs"
530 # endif
531 static int ptlrpcd_bind(int index, int max)
532 {
533         struct ptlrpcd_ctl *pc;
534         int rc = 0;
535 #if defined(CONFIG_NUMA)
536         cpumask_t mask;
537 #endif
538         ENTRY;
539
540         LASSERT(index <= max - 1);
541         pc = &ptlrpcds->pd_threads[index];
542         switch (ptlrpcd_bind_policy) {
543         case PDB_POLICY_NONE:
544                 pc->pc_npartners = -1;
545                 break;
546         case PDB_POLICY_FULL:
547                 pc->pc_npartners = 0;
548                 set_bit(LIOD_BIND, &pc->pc_flags);
549                 break;
550         case PDB_POLICY_PAIR:
551                 LASSERT(max % 2 == 0);
552                 pc->pc_npartners = 1;
553                 break;
554         case PDB_POLICY_NEIGHBOR:
555 #if defined(CONFIG_NUMA)
556         {
557                 int i;
558                 mask = *cpumask_of_node(cpu_to_node(index));
559                 for (i = max; i < num_online_cpus(); i++)
560                         cpu_clear(i, mask);
561                 pc->pc_npartners = cpus_weight(mask) - 1;
562                 set_bit(LIOD_BIND, &pc->pc_flags);
563         }
564 #else
565                 LASSERT(max >= 3);
566                 pc->pc_npartners = 2;
567 #endif
568                 break;
569         default:
570                 CERROR("unknown ptlrpcd bind policy %d\n", ptlrpcd_bind_policy);
571                 rc = -EINVAL;
572         }
573
574         if (rc == 0 && pc->pc_npartners > 0) {
575                 OBD_ALLOC(pc->pc_partners,
576                           sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
577                 if (pc->pc_partners == NULL) {
578                         pc->pc_npartners = 0;
579                         rc = -ENOMEM;
580                 } else {
581                         switch (ptlrpcd_bind_policy) {
582                         case PDB_POLICY_PAIR:
583                                 if (index & 0x1) {
584                                         set_bit(LIOD_BIND, &pc->pc_flags);
585                                         pc->pc_partners[0] = &ptlrpcds->
586                                                 pd_threads[index - 1];
587                                         ptlrpcds->pd_threads[index - 1].
588                                                 pc_partners[0] = pc;
589                                 }
590                                 break;
591                         case PDB_POLICY_NEIGHBOR:
592 #if defined(CONFIG_NUMA)
593                         {
594                                 struct ptlrpcd_ctl *ppc;
595                                 int i, pidx;
596                                 /* partners are cores in the same NUMA node.
597                                  * setup partnership only with ptlrpcd threads
598                                  * that are already initialized
599                                  */
600                                 for (pidx = 0, i = 0; i < index; i++) {
601                                         if (cpu_isset(i, mask)) {
602                                                 ppc = &ptlrpcds->pd_threads[i];
603                                                 pc->pc_partners[pidx++] = ppc;
604                                                 ppc->pc_partners[ppc->
605                                                           pc_npartners++] = pc;
606                                         }
607                                 }
608                                 /* adjust number of partners to the number
609                                  * of partnership really setup */
610                                 pc->pc_npartners = pidx;
611                         }
612 #else
613                                 if (index & 0x1)
614                                         set_bit(LIOD_BIND, &pc->pc_flags);
615                                 if (index > 0) {
616                                         pc->pc_partners[0] = &ptlrpcds->
617                                                 pd_threads[index - 1];
618                                         ptlrpcds->pd_threads[index - 1].
619                                                 pc_partners[1] = pc;
620                                         if (index == max - 1) {
621                                                 pc->pc_partners[1] =
622                                                 &ptlrpcds->pd_threads[0];
623                                                 ptlrpcds->pd_threads[0].
624                                                 pc_partners[0] = pc;
625                                         }
626                                 }
627 #endif
628                                 break;
629                         }
630                 }
631         }
632
633         RETURN(rc);
634 }
635
636 #else /* !__KERNEL__ */
637
638 /**
639  * In liblustre we do not have separate threads, so this function
640  * is called from time to time all across common code to see
641  * if something needs to be processed on ptlrpcd set.
642  */
643 int ptlrpcd_check_async_rpcs(void *arg)
644 {
645         struct ptlrpcd_ctl *pc = arg;
646         int                 rc = 0;
647
648         /*
649          * Single threaded!!
650          */
651         pc->pc_recurred++;
652
653         if (pc->pc_recurred == 1) {
654                 rc = lu_env_refill(&pc->pc_env);
655                 if (rc == 0) {
656                         lu_context_enter(&pc->pc_env.le_ctx);
657                         rc = ptlrpcd_check(&pc->pc_env, pc);
658                         if (!rc)
659                                 ptlrpc_expired_set(pc->pc_set);
660                         /*
661                          * XXX: send replay requests.
662                          */
663                         if (test_bit(LIOD_RECOVERY, &pc->pc_flags))
664                                 rc = ptlrpcd_check(&pc->pc_env, pc);
665                         lu_context_exit(&pc->pc_env.le_ctx);
666                 }
667         }
668
669         pc->pc_recurred--;
670         return rc;
671 }
672
673 int ptlrpcd_idle(void *arg)
674 {
675         struct ptlrpcd_ctl *pc = arg;
676
677         return (cfs_atomic_read(&pc->pc_set->set_new_count) == 0 &&
678                 cfs_atomic_read(&pc->pc_set->set_remaining) == 0);
679 }
680
681 #endif
682
683 int ptlrpcd_start(int index, int max, const char *name, struct ptlrpcd_ctl *pc)
684 {
685         int rc;
686         ENTRY;
687
688         /*
689          * Do not allow start second thread for one pc.
690          */
691         if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
692                 CWARN("Starting second thread (%s) for same pc %p\n",
693                       name, pc);
694                 RETURN(0);
695         }
696
697         pc->pc_index = index;
698         init_completion(&pc->pc_starting);
699         init_completion(&pc->pc_finishing);
700         spin_lock_init(&pc->pc_lock);
701         strncpy(pc->pc_name, name, sizeof(pc->pc_name) - 1);
702         pc->pc_set = ptlrpc_prep_set();
703         if (pc->pc_set == NULL)
704                 GOTO(out, rc = -ENOMEM);
705
706 #ifndef __KERNEL__
707         pc->pc_wait_callback =
708                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
709                                                  &ptlrpcd_check_async_rpcs, pc);
710         pc->pc_idle_callback =
711                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
712                                                  &ptlrpcd_idle, pc);
713         RETURN(0);
714 #else
715         /*
716          * So far only "client" ptlrpcd uses an environment. In the future,
717          * ptlrpcd thread (or a thread-set) has to be given an argument,
718          * describing its "scope".
719          */
720         rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
721         if (rc != 0)
722                 GOTO(out_set, rc);
723
724         {
725                 struct task_struct *task;
726                 if (index >= 0) {
727                         rc = ptlrpcd_bind(index, max);
728                         if (rc < 0)
729                                 GOTO(out_env, rc);
730                 }
731
732                 task = kthread_run(ptlrpcd, pc, pc->pc_name);
733                 if (IS_ERR(task))
734                         GOTO(out_env, rc = PTR_ERR(task));
735
736                 wait_for_completion(&pc->pc_starting);
737         }
738         RETURN(0);
739
740 out_env:
741         lu_context_fini(&pc->pc_env.le_ctx);
742
743 out_set:
744         if (pc->pc_set != NULL) {
745                 struct ptlrpc_request_set *set = pc->pc_set;
746
747                 spin_lock(&pc->pc_lock);
748                 pc->pc_set = NULL;
749                 spin_unlock(&pc->pc_lock);
750                 ptlrpc_set_destroy(set);
751         }
752         clear_bit(LIOD_BIND, &pc->pc_flags);
753 #endif
754 out:
755         clear_bit(LIOD_START, &pc->pc_flags);
756         RETURN(rc);
757 }
758
759 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
760 {
761         ENTRY;
762
763         if (!test_bit(LIOD_START, &pc->pc_flags)) {
764                 CWARN("Thread for pc %p was not started\n", pc);
765                 goto out;
766         }
767
768         set_bit(LIOD_STOP, &pc->pc_flags);
769         if (force)
770                 set_bit(LIOD_FORCE, &pc->pc_flags);
771         wake_up(&pc->pc_set->set_waitq);
772
773 out:
774         EXIT;
775 }
776
777 void ptlrpcd_free(struct ptlrpcd_ctl *pc)
778 {
779         struct ptlrpc_request_set *set = pc->pc_set;
780         ENTRY;
781
782         if (!test_bit(LIOD_START, &pc->pc_flags)) {
783                 CWARN("Thread for pc %p was not started\n", pc);
784                 goto out;
785         }
786
787 #ifdef __KERNEL__
788         wait_for_completion(&pc->pc_finishing);
789 #else
790         liblustre_deregister_wait_callback(pc->pc_wait_callback);
791         liblustre_deregister_idle_callback(pc->pc_idle_callback);
792 #endif
793         lu_context_fini(&pc->pc_env.le_ctx);
794
795         spin_lock(&pc->pc_lock);
796         pc->pc_set = NULL;
797         spin_unlock(&pc->pc_lock);
798         ptlrpc_set_destroy(set);
799
800         clear_bit(LIOD_START, &pc->pc_flags);
801         clear_bit(LIOD_STOP, &pc->pc_flags);
802         clear_bit(LIOD_FORCE, &pc->pc_flags);
803         clear_bit(LIOD_BIND, &pc->pc_flags);
804
805 out:
806 #ifdef __KERNEL__
807         if (pc->pc_npartners > 0) {
808                 LASSERT(pc->pc_partners != NULL);
809
810                 OBD_FREE(pc->pc_partners,
811                          sizeof(struct ptlrpcd_ctl *) * pc->pc_npartners);
812                 pc->pc_partners = NULL;
813         }
814         pc->pc_npartners = 0;
815 #endif
816         EXIT;
817 }
818
819 static void ptlrpcd_fini(void)
820 {
821         int i;
822         ENTRY;
823
824         if (ptlrpcds != NULL) {
825                 for (i = 0; i < ptlrpcds->pd_nthreads; i++)
826                         ptlrpcd_stop(&ptlrpcds->pd_threads[i], 0);
827                 for (i = 0; i < ptlrpcds->pd_nthreads; i++)
828                         ptlrpcd_free(&ptlrpcds->pd_threads[i]);
829                 ptlrpcd_stop(&ptlrpcds->pd_thread_rcv, 0);
830                 ptlrpcd_free(&ptlrpcds->pd_thread_rcv);
831                 OBD_FREE(ptlrpcds, ptlrpcds->pd_size);
832                 ptlrpcds = NULL;
833         }
834
835         EXIT;
836 }
837
838 static int ptlrpcd_init(void)
839 {
840         int     nthreads = num_online_cpus();
841         char    name[16];
842         int     size, i = -1, j, rc = 0;
843         ENTRY;
844
845 #ifdef __KERNEL__
846         if (max_ptlrpcds > 0 && max_ptlrpcds < nthreads)
847                 nthreads = max_ptlrpcds;
848         if (nthreads < 2)
849                 nthreads = 2;
850         if (nthreads < 3 && ptlrpcd_bind_policy == PDB_POLICY_NEIGHBOR)
851                 ptlrpcd_bind_policy = PDB_POLICY_PAIR;
852         else if (nthreads % 2 != 0 && ptlrpcd_bind_policy == PDB_POLICY_PAIR)
853                 nthreads &= ~1; /* make sure it is even */
854 #else
855         nthreads = 1;
856 #endif
857
858         size = offsetof(struct ptlrpcd, pd_threads[nthreads]);
859         OBD_ALLOC(ptlrpcds, size);
860         if (ptlrpcds == NULL)
861                 GOTO(out, rc = -ENOMEM);
862
863         snprintf(name, 15, "ptlrpcd_rcv");
864         set_bit(LIOD_RECOVERY, &ptlrpcds->pd_thread_rcv.pc_flags);
865         rc = ptlrpcd_start(-1, nthreads, name, &ptlrpcds->pd_thread_rcv);
866         if (rc < 0)
867                 GOTO(out, rc);
868
869         /* XXX: We start nthreads ptlrpc daemons. Each of them can process any
870          *      non-recovery async RPC to improve overall async RPC efficiency.
871          *
872          *      But there are some issues with async I/O RPCs and async non-I/O
873          *      RPCs processed in the same set under some cases. The ptlrpcd may
874          *      be blocked by some async I/O RPC(s), then will cause other async
875          *      non-I/O RPC(s) can not be processed in time.
876          *
877          *      Maybe we should distinguish blocked async RPCs from non-blocked
878          *      async RPCs, and process them in different ptlrpcd sets to avoid
879          *      unnecessary dependency. But how to distribute async RPCs load
880          *      among all the ptlrpc daemons becomes another trouble. */
881         for (i = 0; i < nthreads; i++) {
882                 snprintf(name, 15, "ptlrpcd_%d", i);
883                 rc = ptlrpcd_start(i, nthreads, name, &ptlrpcds->pd_threads[i]);
884                 if (rc < 0)
885                         GOTO(out, rc);
886         }
887
888         ptlrpcds->pd_size = size;
889         ptlrpcds->pd_index = 0;
890         ptlrpcds->pd_nthreads = nthreads;
891
892 out:
893         if (rc != 0 && ptlrpcds != NULL) {
894                 for (j = 0; j <= i; j++)
895                         ptlrpcd_stop(&ptlrpcds->pd_threads[j], 0);
896                 for (j = 0; j <= i; j++)
897                         ptlrpcd_free(&ptlrpcds->pd_threads[j]);
898                 ptlrpcd_stop(&ptlrpcds->pd_thread_rcv, 0);
899                 ptlrpcd_free(&ptlrpcds->pd_thread_rcv);
900                 OBD_FREE(ptlrpcds, size);
901                 ptlrpcds = NULL;
902         }
903
904         RETURN(rc);
905 }
906
907 int ptlrpcd_addref(void)
908 {
909         int rc = 0;
910         ENTRY;
911
912         mutex_lock(&ptlrpcd_mutex);
913         if (++ptlrpcd_users == 1) {
914                 rc = ptlrpcd_init();
915                 if (rc < 0)
916                         ptlrpcd_users--;
917         }
918         mutex_unlock(&ptlrpcd_mutex);
919         RETURN(rc);
920 }
921 EXPORT_SYMBOL(ptlrpcd_addref);
922
923 void ptlrpcd_decref(void)
924 {
925         mutex_lock(&ptlrpcd_mutex);
926         if (--ptlrpcd_users == 0)
927                 ptlrpcd_fini();
928         mutex_unlock(&ptlrpcd_mutex);
929 }
930 EXPORT_SYMBOL(ptlrpcd_decref);
931 /** @} ptlrpcd */