Whamcloud - gitweb
b87cf91740c413c697eacd2eafac8c81e471c079
[fs/lustre-release.git] / lustre / include / lustre_net.h
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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 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 /** \defgroup PtlRPC Portal RPC and networking module.
37  *
38  * PortalRPC is the layer used by rest of lustre code to achieve network
39  * communications: establish connections with corresponding export and import
40  * states, listen for a service, send and receive RPCs.
41  * PortalRPC also includes base recovery framework: packet resending and
42  * replaying, reconnections, pinger.
43  *
44  * PortalRPC utilizes LNet as its transport layer.
45  *
46  * @{
47  */
48
49
50 #ifndef _LUSTRE_NET_H
51 #define _LUSTRE_NET_H
52
53 /** \defgroup net net
54  *
55  * @{
56  */
57
58 #if defined(__linux__)
59 #include <linux/lustre_net.h>
60 #elif defined(__APPLE__)
61 #include <darwin/lustre_net.h>
62 #elif defined(__WINNT__)
63 #include <winnt/lustre_net.h>
64 #else
65 #error Unsupported operating system.
66 #endif
67
68 #include <libcfs/libcfs.h>
69 // #include <obd.h>
70 #include <lnet/lnet.h>
71 #include <lustre/lustre_idl.h>
72 #include <lustre_ha.h>
73 #include <lustre_sec.h>
74 #include <lustre_import.h>
75 #include <lprocfs_status.h>
76 #include <lu_object.h>
77 #include <lustre_req_layout.h>
78
79 #include <obd_support.h>
80 #include <lustre_ver.h>
81
82 /* MD flags we _always_ use */
83 #define PTLRPC_MD_OPTIONS  0
84
85 /**
86  * Define maxima for bulk I/O
87  * CAVEAT EMPTOR, with multinet (i.e. routers forwarding between networks)
88  * these limits are system wide and not interface-local. */
89 #define PTLRPC_MAX_BRW_BITS     LNET_MTU_BITS
90 #define PTLRPC_MAX_BRW_SIZE     (1<<LNET_MTU_BITS)
91 #define PTLRPC_MAX_BRW_PAGES    (PTLRPC_MAX_BRW_SIZE >> CFS_PAGE_SHIFT)
92
93 /* When PAGE_SIZE is a constant, we can check our arithmetic here with cpp! */
94 #ifdef __KERNEL__
95 # if ((PTLRPC_MAX_BRW_PAGES & (PTLRPC_MAX_BRW_PAGES - 1)) != 0)
96 #  error "PTLRPC_MAX_BRW_PAGES isn't a power of two"
97 # endif
98 # if (PTLRPC_MAX_BRW_SIZE != (PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE))
99 #  error "PTLRPC_MAX_BRW_SIZE isn't PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE"
100 # endif
101 # if (PTLRPC_MAX_BRW_SIZE > LNET_MTU)
102 #  error "PTLRPC_MAX_BRW_SIZE too big"
103 # endif
104 # if (PTLRPC_MAX_BRW_PAGES > LNET_MAX_IOV)
105 #  error "PTLRPC_MAX_BRW_PAGES too big"
106 # endif
107 #endif /* __KERNEL__ */
108
109 #define PTLRPC_NTHRS_INIT       2
110
111 /**
112  * Buffer Constants
113  *
114  * Constants determine how memory is used to buffer incoming service requests.
115  *
116  * ?_NBUFS              # buffers to allocate when growing the pool
117  * ?_BUFSIZE            # bytes in a single request buffer
118  * ?_MAXREQSIZE         # maximum request service will receive
119  *
120  * When fewer than ?_NBUFS/2 buffers are posted for receive, another chunk
121  * of ?_NBUFS is added to the pool.
122  *
123  * Messages larger than ?_MAXREQSIZE are dropped.  Request buffers are
124  * considered full when less than ?_MAXREQSIZE is left in them.
125  */
126 /**
127  * Thread Constants
128  *
129  * Constants determine how threads are created for ptlrpc service.
130  *
131  * ?_NTHRS_INIT         # threads to create for each service partition on
132  *                        initializing. If it's non-affinity service and
133  *                        there is only one partition, it's the overall #
134  *                        threads for the service while initializing.
135  * ?_NTHRS_BASE         # threads should be created at least for each
136  *                        ptlrpc partition to keep the service healthy.
137  *                        It's the low-water mark of threads upper-limit
138  *                        for each partition.
139  * ?_THR_FACTOR         # threads can be added on threads upper-limit for
140  *                        each CPU core. This factor is only for reference,
141  *                        we might decrease value of factor if number of cores
142  *                        per CPT is above a limit.
143  * ?_NTHRS_MAX          # overall threads can be created for a service,
144  *                        it's a soft limit because if service is running
145  *                        on machine with hundreds of cores and tens of
146  *                        CPU partitions, we need to guarantee each partition
147  *                        has ?_NTHRS_BASE threads, which means total threads
148  *                        will be ?_NTHRS_BASE * number_of_cpts which can
149  *                        exceed ?_NTHRS_MAX.
150  *
151  * Examples
152  *
153  * #define MDS_NTHRS_INIT       2
154  * #define MDS_NTHRS_BASE       64
155  * #define MDS_NTHRS_FACTOR     8
156  * #define MDS_NTHRS_MAX        1024
157  *
158  * Example 1):
159  * ---------------------------------------------------------------------
160  * Server(A) has 16 cores, user configured it to 4 partitions so each
161  * partition has 4 cores, then actual number of service threads on each
162  * partition is:
163  *     MDS_NTHRS_BASE(64) + cores(4) * MDS_NTHRS_FACTOR(8) = 96
164  *
165  * Total number of threads for the service is:
166  *     96 * partitions(4) = 384
167  *
168  * Example 2):
169  * ---------------------------------------------------------------------
170  * Server(B) has 32 cores, user configured it to 4 partitions so each
171  * partition has 8 cores, then actual number of service threads on each
172  * partition is:
173  *     MDS_NTHRS_BASE(64) + cores(8) * MDS_NTHRS_FACTOR(8) = 128
174  *
175  * Total number of threads for the service is:
176  *     128 * partitions(4) = 512
177  *
178  * Example 3):
179  * ---------------------------------------------------------------------
180  * Server(B) has 96 cores, user configured it to 8 partitions so each
181  * partition has 12 cores, then actual number of service threads on each
182  * partition is:
183  *     MDS_NTHRS_BASE(64) + cores(12) * MDS_NTHRS_FACTOR(8) = 160
184  *
185  * Total number of threads for the service is:
186  *     160 * partitions(8) = 1280
187  *
188  * However, it's above the soft limit MDS_NTHRS_MAX, so we choose this number
189  * as upper limit of threads number for each partition:
190  *     MDS_NTHRS_MAX(1024) / partitions(8) = 128
191  *
192  * Example 4):
193  * ---------------------------------------------------------------------
194  * Server(C) have a thousand of cores and user configured it to 32 partitions
195  *     MDS_NTHRS_BASE(64) * 32 = 2048
196  *
197  * which is already above soft limit MDS_NTHRS_MAX(1024), but we still need
198  * to guarantee that each partition has at least MDS_NTHRS_BASE(64) threads
199  * to keep service healthy, so total number of threads will just be 2048.
200  *
201  * NB: we don't suggest to choose server with that many cores because backend
202  *     filesystem itself, buffer cache, or underlying network stack might
203  *     have some SMP scalability issues at that large scale.
204  *
205  *     If user already has a fat machine with hundreds or thousands of cores,
206  *     there are two choices for configuration:
207  *     a) create CPU table from subset of all CPUs and run Lustre on
208  *        top of this subset
209  *     b) bind service threads on a few partitions, see modparameters of
210  *        MDS and OSS for details
211 *
212  * NB: these calculations (and examples below) are simplified to help
213  *     understanding, the real implementation is a little more complex,
214  *     please see ptlrpc_server_nthreads_check() for details.
215  *
216  */
217
218  /*
219   * LDLM threads constants:
220   *
221   * Given 8 as factor and 24 as base threads number
222   *
223   * example 1)
224   * On 4-core machine we will have 24 + 8 * 4 = 56 threads.
225   *
226   * example 2)
227   * On 8-core machine with 2 partitions we will have 24 + 4 * 8 = 56
228   * threads for each partition and total threads number will be 112.
229   *
230   * example 3)
231   * On 64-core machine with 8 partitions we will need LDLM_NTHRS_BASE(24)
232   * threads for each partition to keep service healthy, so total threads
233   * number should be 24 * 8 = 192.
234   *
235   * So with these constants, threads number wil be at the similar level
236   * of old versions, unless target machine has over a hundred cores
237   */
238 #define LDLM_THR_FACTOR         8
239 #define LDLM_NTHRS_INIT         PTLRPC_NTHRS_INIT
240 #define LDLM_NTHRS_BASE         24
241 #define LDLM_NTHRS_MAX          (cfs_num_online_cpus() == 1 ? 64 : 128)
242
243 #define LDLM_BL_THREADS  LDLM_NTHRS_AUTO_INIT
244 #define LDLM_NBUFS      (64 * cfs_num_online_cpus())
245 #define LDLM_BUFSIZE    (8 * 1024)
246 #define LDLM_MAXREQSIZE (5 * 1024)
247 #define LDLM_MAXREPSIZE (1024)
248
249  /*
250   * MDS threads constants:
251   *
252   * Please see examples in "Thread Constants", MDS threads number will be at
253   * the comparable level of old versions, unless the server has many cores.
254   */
255 #ifndef MDS_MAX_THREADS
256 #define MDS_MAX_THREADS         1024
257 #define MDS_MAX_OTHR_THREADS    256
258
259 #else /* MDS_MAX_THREADS */
260 #if MDS_MAX_THREADS < PTLRPC_NTHRS_INIT
261 #undef MDS_MAX_THREADS
262 #define MDS_MAX_THREADS PTLRPC_NTHRS_INIT
263 #endif
264 #define MDS_MAX_OTHR_THREADS    max(PTLRPC_NTHRS_INIT, MDS_MAX_THREADS / 2)
265 #endif
266
267 /* default service */
268 #define MDS_THR_FACTOR          8
269 #define MDS_NTHRS_INIT          PTLRPC_NTHRS_INIT
270 #define MDS_NTHRS_MAX           MDS_MAX_THREADS
271 #define MDS_NTHRS_BASE          min(64, MDS_NTHRS_MAX)
272
273 /* read-page service */
274 #define MDS_RDPG_THR_FACTOR     4
275 #define MDS_RDPG_NTHRS_INIT     PTLRPC_NTHRS_INIT
276 #define MDS_RDPG_NTHRS_MAX      MDS_MAX_OTHR_THREADS
277 #define MDS_RDPG_NTHRS_BASE     min(48, MDS_RDPG_NTHRS_MAX)
278
279 /* these should be removed when we remove setattr service in the future */
280 #define MDS_SETA_THR_FACTOR     4
281 #define MDS_SETA_NTHRS_INIT     PTLRPC_NTHRS_INIT
282 #define MDS_SETA_NTHRS_MAX      MDS_MAX_OTHR_THREADS
283 #define MDS_SETA_NTHRS_BASE     min(48, MDS_SETA_NTHRS_MAX)
284
285 /* non-affinity threads */
286 #define MDS_OTHR_NTHRS_INIT     PTLRPC_NTHRS_INIT
287 #define MDS_OTHR_NTHRS_MAX      MDS_MAX_OTHR_THREADS
288
289 #define MDS_NBUFS               (64 * cfs_num_online_cpus())
290 /**
291  * Assume file name length = FNAME_MAX = 256 (true for ext3).
292  *        path name length = PATH_MAX = 4096
293  *        LOV MD size max  = EA_MAX = 48000 (2000 stripes)
294  * symlink:  FNAME_MAX + PATH_MAX  <- largest
295  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
296  * rename:   FNAME_MAX + FNAME_MAX
297  * open:     FNAME_MAX + EA_MAX
298  *
299  * MDS_MAXREQSIZE ~= 4736 bytes =
300  * lustre_msg + ldlm_request + mdt_body + mds_rec_create + FNAME_MAX + PATH_MAX
301  * MDS_MAXREPSIZE ~= 8300 bytes = lustre_msg + llog_header
302  * or, for mds_close() and mds_reint_unlink() on a many-OST filesystem:
303  *      = 9210 bytes = lustre_msg + mdt_body + 160 * (easize + cookiesize)
304  *
305  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
306  * except in the open case where there are a large number of OSTs in a LOV.
307  */
308 #define MDS_MAXREPSIZE  max(10 * 1024, 362 + LOV_MAX_STRIPE_COUNT * 56)
309 #define MDS_MAXREQSIZE  MDS_MAXREPSIZE
310
311 /** MDS_BUFSIZE = max_reqsize + max sptlrpc payload size */
312 #define MDS_BUFSIZE     (MDS_MAXREQSIZE + 1024)
313
314 /** FLD_MAXREQSIZE == lustre_msg + __u32 padding + ptlrpc_body + opc */
315 #define FLD_MAXREQSIZE  (160)
316
317 /** FLD_MAXREPSIZE == lustre_msg + ptlrpc_body */
318 #define FLD_MAXREPSIZE  (152)
319
320 /**
321  * SEQ_MAXREQSIZE == lustre_msg + __u32 padding + ptlrpc_body + opc + lu_range +
322  * __u32 padding */
323 #define SEQ_MAXREQSIZE  (160)
324
325 /** SEQ_MAXREPSIZE == lustre_msg + ptlrpc_body + lu_range */
326 #define SEQ_MAXREPSIZE  (152)
327
328 /** MGS threads must be >= 3, see bug 22458 comment #28 */
329 #define MGS_NTHRS_INIT  (PTLRPC_NTHRS_INIT + 1)
330 #define MGS_NTHRS_MAX   32
331
332 #define MGS_NBUFS       (64 * cfs_num_online_cpus())
333 #define MGS_BUFSIZE     (8 * 1024)
334 #define MGS_MAXREQSIZE  (7 * 1024)
335 #define MGS_MAXREPSIZE  (9 * 1024)
336
337  /*
338   * OSS threads constants:
339   *
340   * Given 8 as factor and 64 as base threads number
341   *
342   * example 1):
343   * On 8-core server configured to 2 partitions, we will have
344   * 64 + 8 * 4 = 96 threads for each partition, 192 total threads.
345   *
346   * example 2):
347   * On 32-core machine configured to 4 partitions, we will have
348   * 64 + 8 * 8 = 112 threads for each partition, so total threads number
349   * will be 112 * 4 = 448.
350   *
351   * example 3):
352   * On 64-core machine configured to 4 partitions, we will have
353   * 64 + 16 * 8 = 192 threads for each partition, so total threads number
354   * will be 192 * 4 = 768 which is above limit OSS_NTHRS_MAX(512), so we
355   * cut off the value to OSS_NTHRS_MAX(512) / 4 which is 128 threads
356   * for each partition.
357   *
358   * So we can see that with these constants, threads number wil be at the
359   * similar level of old versions, unless the server has many cores.
360   */
361  /* depress threads factor for VM with small memory size */
362 #define OSS_THR_FACTOR          min_t(int, 8, \
363                                 CFS_NUM_CACHEPAGES >> (28 - CFS_PAGE_SHIFT))
364 #define OSS_NTHRS_INIT          (PTLRPC_NTHRS_INIT + 1)
365 #define OSS_NTHRS_BASE          64
366 #define OSS_NTHRS_MAX           512
367
368 /* threads for handling "create" request */
369 #define OSS_CR_THR_FACTOR       1
370 #define OSS_CR_NTHRS_INIT       PTLRPC_NTHRS_INIT
371 #define OSS_CR_NTHRS_BASE       8
372 #define OSS_CR_NTHRS_MAX        64
373
374 #define OST_NBUFS       (64 * cfs_num_online_cpus())
375 #define OST_BUFSIZE     (8 * 1024)
376
377 /**
378  * OST_MAXREQSIZE ~= 4768 bytes =
379  * lustre_msg + obdo + 16 * obd_ioobj + 256 * niobuf_remote
380  *
381  * - single object with 16 pages is 512 bytes
382  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
383  */
384 #define OST_MAXREQSIZE  (5 * 1024)
385 #define OST_MAXREPSIZE  (9 * 1024)
386
387 /* Macro to hide a typecast. */
388 #define ptlrpc_req_async_args(req) ((void *)&req->rq_async_args)
389
390 /**
391  * Structure to single define portal connection.
392  */
393 struct ptlrpc_connection {
394         /** linkage for connections hash table */
395         cfs_hlist_node_t        c_hash;
396         /** Our own lnet nid for this connection */
397         lnet_nid_t              c_self;
398         /** Remote side nid for this connection */
399         lnet_process_id_t       c_peer;
400         /** UUID of the other side */
401         struct obd_uuid         c_remote_uuid;
402         /** reference counter for this connection */
403         cfs_atomic_t            c_refcount;
404 };
405
406 /** Client definition for PortalRPC */
407 struct ptlrpc_client {
408         /** What lnet portal does this client send messages to by default */
409         __u32                   cli_request_portal;
410         /** What portal do we expect replies on */
411         __u32                   cli_reply_portal;
412         /** Name of the client */
413         char                   *cli_name;
414 };
415
416 /** state flags of requests */
417 /* XXX only ones left are those used by the bulk descs as well! */
418 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
419 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
420
421 #define REQ_MAX_ACK_LOCKS 8
422
423 union ptlrpc_async_args {
424         /**
425          * Scratchpad for passing args to completion interpreter. Users
426          * cast to the struct of their choosing, and CLASSERT that this is
427          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
428          * a pointer to it here.  The pointer_arg ensures this struct is at
429          * least big enough for that.
430          */
431         void      *pointer_arg[11];
432         __u64      space[7];
433 };
434
435 struct ptlrpc_request_set;
436 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
437 typedef int (*set_producer_func)(struct ptlrpc_request_set *, void *);
438
439 /**
440  * Definition of request set structure.
441  * Request set is a list of requests (not necessary to the same target) that
442  * once populated with RPCs could be sent in parallel.
443  * There are two kinds of request sets. General purpose and with dedicated
444  * serving thread. Example of the latter is ptlrpcd set.
445  * For general purpose sets once request set started sending it is impossible
446  * to add new requests to such set.
447  * Provides a way to call "completion callbacks" when all requests in the set
448  * returned.
449  */
450 struct ptlrpc_request_set {
451         cfs_atomic_t          set_refcount;
452         /** number of in queue requests */
453         cfs_atomic_t          set_new_count;
454         /** number of uncompleted requests */
455         cfs_atomic_t          set_remaining;
456         /** wait queue to wait on for request events */
457         cfs_waitq_t           set_waitq;
458         cfs_waitq_t          *set_wakeup_ptr;
459         /** List of requests in the set */
460         cfs_list_t            set_requests;
461         /**
462          * List of completion callbacks to be called when the set is completed
463          * This is only used if \a set_interpret is NULL.
464          * Links struct ptlrpc_set_cbdata.
465          */
466         cfs_list_t            set_cblist;
467         /** Completion callback, if only one. */
468         set_interpreter_func  set_interpret;
469         /** opaq argument passed to completion \a set_interpret callback. */
470         void                 *set_arg;
471         /**
472          * Lock for \a set_new_requests manipulations
473          * locked so that any old caller can communicate requests to
474          * the set holder who can then fold them into the lock-free set
475          */
476         spinlock_t              set_new_req_lock;
477         /** List of new yet unsent requests. Only used with ptlrpcd now. */
478         cfs_list_t            set_new_requests;
479
480         /** rq_status of requests that have been freed already */
481         int                   set_rc;
482         /** Additional fields used by the flow control extension */
483         /** Maximum number of RPCs in flight */
484         int                   set_max_inflight;
485         /** Callback function used to generate RPCs */
486         set_producer_func     set_producer;
487         /** opaq argument passed to the producer callback */
488         void                 *set_producer_arg;
489 };
490
491 /**
492  * Description of a single ptrlrpc_set callback
493  */
494 struct ptlrpc_set_cbdata {
495         /** List linkage item */
496         cfs_list_t              psc_item;
497         /** Pointer to interpreting function */
498         set_interpreter_func    psc_interpret;
499         /** Opaq argument to pass to the callback */
500         void                   *psc_data;
501 };
502
503 struct ptlrpc_bulk_desc;
504 struct ptlrpc_service_part;
505 struct ptlrpc_service;
506
507 /**
508  * ptlrpc callback & work item stuff
509  */
510 struct ptlrpc_cb_id {
511         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
512         void    *cbid_arg;                      /* additional arg */
513 };
514
515 /** Maximum number of locks to fit into reply state */
516 #define RS_MAX_LOCKS 8
517 #define RS_DEBUG     0
518
519 /**
520  * Structure to define reply state on the server
521  * Reply state holds various reply message information. Also for "difficult"
522  * replies (rep-ack case) we store the state after sending reply and wait
523  * for the client to acknowledge the reception. In these cases locks could be
524  * added to the state for replay/failover consistency guarantees.
525  */
526 struct ptlrpc_reply_state {
527         /** Callback description */
528         struct ptlrpc_cb_id    rs_cb_id;
529         /** Linkage for list of all reply states in a system */
530         cfs_list_t             rs_list;
531         /** Linkage for list of all reply states on same export */
532         cfs_list_t             rs_exp_list;
533         /** Linkage for list of all reply states for same obd */
534         cfs_list_t             rs_obd_list;
535 #if RS_DEBUG
536         cfs_list_t             rs_debug_list;
537 #endif
538         /** A spinlock to protect the reply state flags */
539         spinlock_t              rs_lock;
540         /** Reply state flags */
541         unsigned long          rs_difficult:1;     /* ACK/commit stuff */
542         unsigned long          rs_no_ack:1;    /* no ACK, even for
543                                                   difficult requests */
544         unsigned long          rs_scheduled:1;     /* being handled? */
545         unsigned long          rs_scheduled_ever:1;/* any schedule attempts? */
546         unsigned long          rs_handled:1;  /* been handled yet? */
547         unsigned long          rs_on_net:1;   /* reply_out_callback pending? */
548         unsigned long          rs_prealloc:1; /* rs from prealloc list */
549         unsigned long          rs_committed:1;/* the transaction was committed
550                                                  and the rs was dispatched
551                                                  by ptlrpc_commit_replies */
552         /** Size of the state */
553         int                    rs_size;
554         /** opcode */
555         __u32                  rs_opc;
556         /** Transaction number */
557         __u64                  rs_transno;
558         /** xid */
559         __u64                  rs_xid;
560         struct obd_export     *rs_export;
561         struct ptlrpc_service_part *rs_svcpt;
562         /** Lnet metadata handle for the reply */
563         lnet_handle_md_t       rs_md_h;
564         cfs_atomic_t           rs_refcount;
565
566         /** Context for the sevice thread */
567         struct ptlrpc_svc_ctx *rs_svc_ctx;
568         /** Reply buffer (actually sent to the client), encoded if needed */
569         struct lustre_msg     *rs_repbuf;       /* wrapper */
570         /** Size of the reply buffer */
571         int                    rs_repbuf_len;   /* wrapper buf length */
572         /** Size of the reply message */
573         int                    rs_repdata_len;  /* wrapper msg length */
574         /**
575          * Actual reply message. Its content is encrupted (if needed) to
576          * produce reply buffer for actual sending. In simple case
577          * of no network encryption we jus set \a rs_repbuf to \a rs_msg
578          */
579         struct lustre_msg     *rs_msg;          /* reply message */
580
581         /** Number of locks awaiting client ACK */
582         int                    rs_nlocks;
583         /** Handles of locks awaiting client reply ACK */
584         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
585         /** Lock modes of locks in \a rs_locks */
586         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
587 };
588
589 struct ptlrpc_thread;
590
591 /** RPC stages */
592 enum rq_phase {
593         RQ_PHASE_NEW            = 0xebc0de00,
594         RQ_PHASE_RPC            = 0xebc0de01,
595         RQ_PHASE_BULK           = 0xebc0de02,
596         RQ_PHASE_INTERPRET      = 0xebc0de03,
597         RQ_PHASE_COMPLETE       = 0xebc0de04,
598         RQ_PHASE_UNREGISTERING  = 0xebc0de05,
599         RQ_PHASE_UNDEFINED      = 0xebc0de06
600 };
601
602 /** Type of request interpreter call-back */
603 typedef int (*ptlrpc_interpterer_t)(const struct lu_env *env,
604                                     struct ptlrpc_request *req,
605                                     void *arg, int rc);
606
607 /**
608  * Definition of request pool structure.
609  * The pool is used to store empty preallocated requests for the case
610  * when we would actually need to send something without performing
611  * any allocations (to avoid e.g. OOM).
612  */
613 struct ptlrpc_request_pool {
614         /** Locks the list */
615         spinlock_t prp_lock;
616         /** list of ptlrpc_request structs */
617         cfs_list_t prp_req_list;
618         /** Maximum message size that would fit into a rquest from this pool */
619         int prp_rq_size;
620         /** Function to allocate more requests for this pool */
621         void (*prp_populate)(struct ptlrpc_request_pool *, int);
622 };
623
624 struct lu_context;
625 struct lu_env;
626
627 struct ldlm_lock;
628
629 /**
630  * \defgroup nrs Network Request Scheduler
631  * @{
632  */
633 struct ptlrpc_nrs_policy;
634 struct ptlrpc_nrs_resource;
635 struct ptlrpc_nrs_request;
636
637 /**
638  * NRS control operations.
639  *
640  * These are common for all policies.
641  */
642 enum ptlrpc_nrs_ctl {
643         /**
644          * Activate the policy.
645          */
646         PTLRPC_NRS_CTL_START,
647         /**
648          * Reserved for multiple primary policies, which may be a possibility
649          * in the future.
650          */
651         PTLRPC_NRS_CTL_STOP,
652         /**
653          * Recycle resources for inactive policies.
654          */
655         PTLRPC_NRS_CTL_SHRINK,
656         /**
657          * Not a valid opcode.
658          */
659         PTLRPC_NRS_CTL_INVALID,
660         /**
661          * Policies can start using opcodes from this value and onwards for
662          * their own purposes; the assigned value itself is arbitrary.
663          */
664         PTLRPC_NRS_CTL_1ST_POL_SPEC = 0x20,
665 };
666
667 /**
668  * NRS policy operations.
669  *
670  * These determine the behaviour of a policy, and are called in response to
671  * NRS core events.
672  */
673 struct ptlrpc_nrs_pol_ops {
674         /**
675          * Called during policy registration; this operation is optional.
676          *
677          * \param[in] policy The policy being initialized
678          */
679         int     (*op_policy_init) (struct ptlrpc_nrs_policy *policy);
680         /**
681          * Called during policy unregistration; this operation is optional.
682          *
683          * \param[in] policy The policy being unregistered/finalized
684          */
685         void    (*op_policy_fini) (struct ptlrpc_nrs_policy *policy);
686         /**
687          * Called when activating a policy via lprocfs; policies allocate and
688          * initialize their resources here; this operation is optional.
689          *
690          * \param[in] policy The policy being started
691          *
692          * \see nrs_policy_start_locked()
693          */
694         int     (*op_policy_start) (struct ptlrpc_nrs_policy *policy);
695         /**
696          * Called when deactivating a policy via lprocfs; policies deallocate
697          * their resources here; this operation is optional
698          *
699          * \param[in] policy The policy being stopped
700          *
701          * \see nrs_policy_stop_final()
702          */
703         void    (*op_policy_stop) (struct ptlrpc_nrs_policy *policy);
704         /**
705          * Used for policy-specific operations; i.e. not generic ones like
706          * \e PTLRPC_NRS_CTL_START and \e PTLRPC_NRS_CTL_GET_INFO; analogous
707          * to an ioctl; this operation is optional.
708          *
709          * \param[in]     policy The policy carrying out operation \a opc
710          * \param[in]     opc    The command operation being carried out
711          * \param[in,out] arg    An generic buffer for communication between the
712          *                       user and the control operation
713          *
714          * \retval -ve error
715          * \retval   0 success
716          *
717          * \see ptlrpc_nrs_policy_control()
718          */
719         int     (*op_policy_ctl) (struct ptlrpc_nrs_policy *policy,
720                                   enum ptlrpc_nrs_ctl opc, void *arg);
721
722         /**
723          * Called when obtaining references to the resources of the resource
724          * hierarchy for a request that has arrived for handling at the PTLRPC
725          * service. Policies should return -ve for requests they do not wish
726          * to handle. This operation is mandatory.
727          *
728          * \param[in]  policy     The policy we're getting resources for.
729          * \param[in]  nrq        The request we are getting resources for.
730          * \param[in]  parent     The parent resource of the resource being
731          *                        requested; set to NULL if none.
732          * \param[out] resp       The resource is to be returned here; the
733          *                        fallback policy in an NRS head should
734          *                        \e always return a non-NULL pointer value.
735          * \param[in]  moving_req When set, signifies that this is an attempt
736          *                        to obtain resources for a request being moved
737          *                        to the high-priority NRS head by
738          *                        ldlm_lock_reorder_req().
739          *                        This implies two things:
740          *                        1. We are under obd_export::exp_rpc_lock and
741          *                        so should not sleep.
742          *                        2. We should not perform non-idempotent or can
743          *                        skip performing idempotent operations that
744          *                        were carried out when resources were first
745          *                        taken for the request when it was initialized
746          *                        in ptlrpc_nrs_req_initialize().
747          *
748          * \retval 0, +ve The level of the returned resource in the resource
749          *                hierarchy; currently only 0 (for a non-leaf resource)
750          *                and 1 (for a leaf resource) are supported by the
751          *                framework.
752          * \retval -ve    error
753          *
754          * \see ptlrpc_nrs_req_initialize()
755          * \see ptlrpc_nrs_hpreq_add_nolock()
756          * \see ptlrpc_nrs_req_hp_move()
757          */
758         int     (*op_res_get) (struct ptlrpc_nrs_policy *policy,
759                                struct ptlrpc_nrs_request *nrq,
760                                struct ptlrpc_nrs_resource *parent,
761                                struct ptlrpc_nrs_resource **resp,
762                                bool moving_req);
763         /**
764          * Called when releasing references taken for resources in the resource
765          * hierarchy for the request; this operation is optional.
766          *
767          * \param[in] policy   The policy the resource belongs to
768          * \param[in] res      The resource to be freed
769          *
770          * \see ptlrpc_nrs_req_finalize()
771          * \see ptlrpc_nrs_hpreq_add_nolock()
772          * \see ptlrpc_nrs_req_hp_move()
773          */
774         void    (*op_res_put) (struct ptlrpc_nrs_policy *policy,
775                                struct ptlrpc_nrs_resource *res);
776
777         /**
778          * Obtain a request for handling from the policy via polling; this
779          * operation is mandatory.
780          *
781          * \param[in] policy The policy to poll
782          *
783          * \retval NULL No erquest available for handling
784          * \retval valid-pointer The request polled for handling
785          *
786          * \see ptlrpc_nrs_req_poll_nolock()
787          */
788         struct ptlrpc_nrs_request *
789                 (*op_req_poll) (struct ptlrpc_nrs_policy *policy);
790         /**
791          * Called when attempting to add a request to a policy for later
792          * handling; this operation is mandatory.
793          *
794          * \param[in] policy The policy on which to enqueue \a nrq
795          * \param[in] nrq    The request to enqueue
796          *
797          * \retval 0    success
798          * \retval != 0 error
799          *
800          * \see ptlrpc_nrs_req_add_nolock()
801          */
802         int     (*op_req_enqueue) (struct ptlrpc_nrs_policy *policy,
803                                    struct ptlrpc_nrs_request *nrq);
804         /**
805          * Removes a request from the policy's set of pending requests. Normally
806          * called after a request has been polled successfully from the policy
807          * for handling; this operation is mandatory.
808          *
809          * \param[in] policy The policy the request \a nrq belongs to
810          * \param[in] nrq    The request to dequeue
811          *
812          * \see ptlrpc_nrs_req_del_nolock()
813          */
814         void    (*op_req_dequeue) (struct ptlrpc_nrs_policy *policy,
815                                    struct ptlrpc_nrs_request *nrq);
816         /**
817          * Called before carrying out the request; should not block. Could be
818          * used for job/resource control; this operation is optional.
819          *
820          * \param[in] policy The policy which is starting to handle request
821          *                   \a nrq
822          * \param[in] nrq    The request
823          *
824          * \pre spin_is_locked(&svcpt->scp_req_lock)
825          *
826          * \see ptlrpc_nrs_req_start_nolock()
827          */
828         void    (*op_req_start) (struct ptlrpc_nrs_policy *policy,
829                                  struct ptlrpc_nrs_request *nrq);
830         /**
831          * Called after the request being carried out. Could be used for
832          * job/resource control; this operation is optional.
833          *
834          * \param[in] policy The policy which is stopping to handle request
835          *                   \a nrq
836          * \param[in] nrq    The request
837          *
838          * \pre spin_is_locked(&svcpt->scp_req_lock)
839          *
840          * \see ptlrpc_nrs_req_stop_nolock()
841          */
842         void    (*op_req_stop) (struct ptlrpc_nrs_policy *policy,
843                                 struct ptlrpc_nrs_request *nrq);
844         /**
845          * Registers the policy's lprocfs interface with a PTLRPC service.
846          *
847          * \param[in] svc The service
848          *
849          * \retval 0    success
850          * \retval != 0 error
851          */
852         int     (*op_lprocfs_init) (struct ptlrpc_service *svc);
853         /**
854          * Unegisters the policy's lprocfs interface with a PTLRPC service.
855          *
856          * \param[in] svc The service
857          */
858         void    (*op_lprocfs_fini) (struct ptlrpc_service *svc);
859 };
860
861 /**
862  * Policy flags
863  */
864 enum nrs_policy_flags {
865         /**
866          * Fallback policy, use this flag only on a single supported policy per
867          * service. Do not use this flag for policies registering using
868          * ptlrpc_nrs_policy_register() (i.e. ones that are not in
869          * \e nrs_pols_builtin).
870          */
871         PTLRPC_NRS_FL_FALLBACK          = (1 << 0),
872         /**
873          * Start policy immediately after registering.
874          */
875         PTLRPC_NRS_FL_REG_START         = (1 << 1),
876         /**
877          * This is a polciy registering externally with NRS core, via
878          * ptlrpc_nrs_policy_register(), (i.e. one that is not in
879          * \e nrs_pols_builtin. Used to avoid ptlrpc_nrs_policy_register()
880          * racing with a policy start operation issued by the user via lprocfs.
881          */
882         PTLRPC_NRS_FL_REG_EXTERN        = (1 << 2),
883 };
884
885 /**
886  * NRS queue type.
887  *
888  * Denotes whether an NRS instance is for handling normal or high-priority
889  * RPCs, or whether an operation pertains to one or both of the NRS instances
890  * in a service.
891  */
892 enum ptlrpc_nrs_queue_type {
893         PTLRPC_NRS_QUEUE_REG,
894         PTLRPC_NRS_QUEUE_HP,
895         PTLRPC_NRS_QUEUE_BOTH,
896 };
897
898 /**
899  * NRS head
900  *
901  * A PTLRPC service has at least one NRS head instance for handling normal
902  * priority RPCs, and may optionally have a second NRS head instance for
903  * handling high-priority RPCs. Each NRS head maintains a list of available
904  * policies, of which one and only one policy is acting as the fallback policy,
905  * and optionally a different policy may be acting as the primary policy. For
906  * all RPCs handled by this NRS head instance, NRS core will first attempt to
907  * enqueue the RPC using the primary policy (if any). The fallback policy is
908  * used in the following cases:
909  * - when there was no primary policy in the
910  *   ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED state at the time the request
911  *   was initialized.
912  * - when the primary policy that was at the
913  *   ptlrpc_nrs_pol_state::PTLRPC_NRS_POL_STATE_STARTED state at the time the
914  *   RPC was initialized, denoted it did not wish, or for some other reason was
915  *   not able to handle the request, by returning a non-valid NRS resource
916  *   reference.
917  * - when the primary policy that was at the
918  *   ptlrpc_nrs_pol_state::PTLRPC_NRS_POL_STATE_STARTED state at the time the
919  *   RPC was initialized, fails later during the request enqueueing stage.
920  *
921  * \see nrs_resource_get_safe()
922  * \see nrs_request_enqueue()
923  */
924 struct ptlrpc_nrs {
925         spinlock_t                      nrs_lock;
926         /** XXX Possibly replace svcpt->scp_req_lock with another lock here. */
927         /**
928          * Linkage into nrs_core_heads_list
929          */
930         cfs_list_t                      nrs_heads;
931         /**
932          * List of registered policies
933          */
934         cfs_list_t                      nrs_policy_list;
935         /**
936          * List of policies with queued requests. Policies that have any
937          * outstanding requests are queued here, and this list is queried
938          * in a round-robin manner from NRS core when obtaining a request
939          * for handling. This ensures that requests from policies that at some
940          * point transition away from the
941          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED state are drained.
942          */
943         cfs_list_t                      nrs_policy_queued;
944         /**
945          * Service partition for this NRS head
946          */
947         struct ptlrpc_service_part     *nrs_svcpt;
948         /**
949          * Primary policy, which is the preferred policy for handling RPCs
950          */
951         struct ptlrpc_nrs_policy       *nrs_policy_primary;
952         /**
953          * Fallback policy, which is the backup policy for handling RPCs
954          */
955         struct ptlrpc_nrs_policy       *nrs_policy_fallback;
956         /**
957          * This NRS head handles either HP or regular requests
958          */
959         enum ptlrpc_nrs_queue_type      nrs_queue_type;
960         /**
961          * # queued requests from all policies in this NRS head
962          */
963         unsigned long                   nrs_req_queued;
964         /**
965          * # scheduled requests from all policies in this NRS head
966          */
967         unsigned long                   nrs_req_started;
968         /**
969          * # policies on this NRS
970          * TODO: Can we avoid having this?
971          */
972         unsigned                        nrs_num_pols;
973         /**
974          * This NRS head is in progress of starting a policy
975          */
976         unsigned                        nrs_policy_starting:1;
977         /**
978          * In progress of shutting down the whole NRS head; used during
979          * unregistration
980          */
981         unsigned                        nrs_stopping:1;
982 };
983
984 #define NRS_POL_NAME_MAX                16
985
986 /**
987  * NRS policy registering descriptor
988  *
989  * Is used to hold a description of a policy that can be passed to NRS core in
990  * order to register the policy with NRS heads in different PTLRPC services.
991  */
992 struct ptlrpc_nrs_pol_desc {
993         /**
994          * Human-readable policy name
995          */
996         char                            pd_name[NRS_POL_NAME_MAX];
997         /**
998          * NRS operations for this policy
999          */
1000         struct ptlrpc_nrs_pol_ops      *pd_ops;
1001         /**
1002          * Service Compatibility function; this determines whether a policy is
1003          * adequate for handling RPCs of a particular PTLRPC service.
1004          *
1005          * XXX:This should give the same result during policy
1006          * registration and unregistration, and for all partitions of a
1007          * service; so the result should not depend on temporal service
1008          * or other properties, that may influence the result.
1009          */
1010         bool    (*pd_compat) (struct ptlrpc_service *svc,
1011                               const struct ptlrpc_nrs_pol_desc *desc);
1012         /**
1013          * Optionally set for policies that support a single ptlrpc service,
1014          * i.e. ones that have \a pd_compat set to nrs_policy_compat_one()
1015          */
1016         char                           *pd_compat_svc_name;
1017         /**
1018          * Bitmask of nrs_policy_flags
1019          */
1020         unsigned                        pd_flags;
1021         /**
1022          * Link into nrs_core::nrs_policies
1023          */
1024         cfs_list_t                      pd_list;
1025 };
1026
1027 /**
1028  * NRS policy state
1029  *
1030  * Policies transition from one state to the other during their lifetime
1031  */
1032 enum ptlrpc_nrs_pol_state {
1033         /**
1034          * Not a valid policy state.
1035          */
1036         NRS_POL_STATE_INVALID,
1037         /**
1038          * For now, this state is used exclusively for policies that register
1039          * externally to NRS core, i.e. ones that do so via
1040          * ptlrpc_nrs_policy_register() and are not part of nrs_pols_builtin;
1041          * it is used to prevent a race condition between the policy registering
1042          * with more than one service partition while service is operational,
1043          * and the user starting the policy via lprocfs.
1044          *
1045          * \see nrs_pol_make_avail()
1046          */
1047         NRS_POL_STATE_UNAVAIL,
1048         /**
1049          * Policies are at this state either at the start of their life, or
1050          * transition here when the user selects a different policy to act
1051          * as the primary one.
1052          */
1053         NRS_POL_STATE_STOPPED,
1054         /**
1055          * Policy is progress of stopping
1056          */
1057         NRS_POL_STATE_STOPPING,
1058         /**
1059          * Policy is in progress of starting
1060          */
1061         NRS_POL_STATE_STARTING,
1062         /**
1063          * A policy is in this state in two cases:
1064          * - it is the fallback policy, which is always in this state.
1065          * - it has been activated by the user; i.e. it is the primary policy,
1066          */
1067         NRS_POL_STATE_STARTED,
1068 };
1069
1070 /**
1071  * NRS policy information
1072  *
1073  * Used for obtaining information for the status of a policy via lprocfs
1074  */
1075 struct ptlrpc_nrs_pol_info {
1076         /**
1077          * Policy name
1078          */
1079         char                            pi_name[NRS_POL_NAME_MAX];
1080         /**
1081          * Current policy state
1082          */
1083         enum ptlrpc_nrs_pol_state       pi_state;
1084         /**
1085          * # RPCs enqueued for later dispatching by the policy
1086          */
1087         long                            pi_req_queued;
1088         /**
1089          * # RPCs started for dispatch by the policy
1090          */
1091         long                            pi_req_started;
1092         /**
1093          * Is this a fallback policy?
1094          */
1095         unsigned                        pi_fallback:1;
1096 };
1097
1098 /**
1099  * NRS policy
1100  *
1101  * There is one instance of this for each policy in each NRS head of each
1102  * PTLRPC service partition.
1103  */
1104 struct ptlrpc_nrs_policy {
1105         /**
1106          * Linkage into the NRS head's list of policies,
1107          * ptlrpc_nrs:nrs_policy_list
1108          */
1109         cfs_list_t                      pol_list;
1110         /**
1111          * Linkage into the NRS head's list of policies with enqueued
1112          * requests ptlrpc_nrs:nrs_policy_queued
1113          */
1114         cfs_list_t                      pol_list_queued;
1115         /**
1116          * Current state of this policy
1117          */
1118         enum ptlrpc_nrs_pol_state       pol_state;
1119         /**
1120          * Bitmask of nrs_policy_flags
1121          */
1122         unsigned                        pol_flags;
1123         /**
1124          * # RPCs enqueued for later dispatching by the policy
1125          */
1126         long                            pol_req_queued;
1127         /**
1128          * # RPCs started for dispatch by the policy
1129          */
1130         long                            pol_req_started;
1131         /**
1132          * Usage Reference count taken on the policy instance
1133          */
1134         long                            pol_ref;
1135         /**
1136          * The NRS head this policy has been created at
1137          */
1138         struct ptlrpc_nrs              *pol_nrs;
1139         /**
1140          * NRS operations for this policy; points to ptlrpc_nrs_pol_desc::pd_ops
1141          */
1142         struct ptlrpc_nrs_pol_ops      *pol_ops;
1143         /**
1144          * Private policy data; varies by policy type
1145          */
1146         void                           *pol_private;
1147         /**
1148          * Human-readable policy name; point to ptlrpc_nrs_pol_desc::pd_name
1149          */
1150         char                           *pol_name;
1151 };
1152
1153 /**
1154  * NRS resource
1155  *
1156  * Resources are embedded into two types of NRS entities:
1157  * - Inside NRS policies, in the policy's private data in
1158  *   ptlrpc_nrs_policy::pol_private
1159  * - In objects that act as prime-level scheduling entities in different NRS
1160  *   policies; e.g. on a policy that performs round robin or similar order
1161  *   scheduling across client NIDs, there would be one NRS resource per unique
1162  *   client NID. On a policy which performs round robin scheduling across
1163  *   backend filesystem objects, there would be one resource associated with
1164  *   each of the backend filesystem objects partaking in the scheduling
1165  *   performed by the policy.
1166  *
1167  * NRS resources share a parent-child relationship, in which resources embedded
1168  * in policy instances are the parent entities, with all scheduling entities
1169  * a policy schedules across being the children, thus forming a simple resource
1170  * hierarchy. This hierarchy may be extended with one or more levels in the
1171  * future if the ability to have more than one primary policy is added.
1172  *
1173  * Upon request initialization, references to the then active NRS policies are
1174  * taken and used to later handle the dispatching of the request with one of
1175  * these policies.
1176  *
1177  * \see nrs_resource_get_safe()
1178  * \see ptlrpc_nrs_req_add()
1179  */
1180 struct ptlrpc_nrs_resource {
1181         /**
1182          * This NRS resource's parent; is NULL for resources embedded in NRS
1183          * policy instances; i.e. those are top-level ones.
1184          */
1185         struct ptlrpc_nrs_resource     *res_parent;
1186         /**
1187          * The policy associated with this resource.
1188          */
1189         struct ptlrpc_nrs_policy       *res_policy;
1190 };
1191
1192 enum {
1193         NRS_RES_FALLBACK,
1194         NRS_RES_PRIMARY,
1195         NRS_RES_MAX
1196 };
1197
1198 /* \name fifo
1199  *
1200  * FIFO policy
1201  *
1202  * This policy is a logical wrapper around previous, non-NRS functionality.
1203  * It dispatches RPCs in the same order as they arrive from the network. This
1204  * policy is currently used as the fallback policy, and the only enabled policy
1205  * on all NRS heads of all PTLRPC service partitions.
1206  * @{
1207  */
1208
1209 /**
1210  * Private data structure for the FIFO policy
1211  */
1212 struct nrs_fifo_head {
1213         /**
1214          * Resource object for policy instance.
1215          */
1216         struct ptlrpc_nrs_resource      fh_res;
1217         /**
1218          * List of queued requests.
1219          */
1220         cfs_list_t                      fh_list;
1221         /**
1222          * For debugging purposes.
1223          */
1224         __u64                           fh_sequence;
1225 };
1226
1227 struct nrs_fifo_req {
1228         /** request header, must be the first member of structure */
1229         cfs_list_t              fr_list;
1230         __u64                   fr_sequence;
1231 };
1232
1233 /** @} fifo */
1234
1235 /**
1236  * NRS request
1237  *
1238  * Instances of this object exist embedded within ptlrpc_request; the main
1239  * purpose of this object is to hold references to the request's resources
1240  * for the lifetime of the request, and to hold properties that policies use
1241  * use for determining the request's scheduling priority.
1242  * */
1243 struct ptlrpc_nrs_request {
1244         /**
1245          * The request's resource hierarchy.
1246          */
1247         struct ptlrpc_nrs_resource     *nr_res_ptrs[NRS_RES_MAX];
1248         /**
1249          * Index into ptlrpc_nrs_request::nr_res_ptrs of the resource of the
1250          * policy that was used to enqueue the request.
1251          *
1252          * \see nrs_request_enqueue()
1253          */
1254         unsigned                        nr_res_idx;
1255         unsigned                        nr_initialized:1;
1256         unsigned                        nr_enqueued:1;
1257         unsigned                        nr_dequeued:1;
1258         unsigned                        nr_started:1;
1259         unsigned                        nr_finalized:1;
1260         cfs_binheap_node_t              nr_node;
1261
1262         /**
1263          * Policy-specific fields, used for determining a request's scheduling
1264          * priority, and other supporting functionality.
1265          */
1266         union {
1267                 /**
1268                  * Fields for the FIFO policy
1269                  */
1270                 struct nrs_fifo_req     fifo;
1271         } nr_u;
1272         /**
1273          * Externally-registering policies may want to use this to allocate
1274          * their own request properties.
1275          */
1276         void                           *ext;
1277 };
1278
1279 /** @} nrs */
1280
1281 /**
1282  * Basic request prioritization operations structure.
1283  * The whole idea is centered around locks and RPCs that might affect locks.
1284  * When a lock is contended we try to give priority to RPCs that might lead
1285  * to fastest release of that lock.
1286  * Currently only implemented for OSTs only in a way that makes all
1287  * IO and truncate RPCs that are coming from a locked region where a lock is
1288  * contended a priority over other requests.
1289  */
1290 struct ptlrpc_hpreq_ops {
1291         /**
1292          * Check if the lock handle of the given lock is the same as
1293          * taken from the request.
1294          */
1295         int  (*hpreq_lock_match)(struct ptlrpc_request *, struct ldlm_lock *);
1296         /**
1297          * Check if the request is a high priority one.
1298          */
1299         int  (*hpreq_check)(struct ptlrpc_request *);
1300         /**
1301          * Called after the request has been handled.
1302          */
1303         void (*hpreq_fini)(struct ptlrpc_request *);
1304 };
1305
1306 /**
1307  * Represents remote procedure call.
1308  *
1309  * This is a staple structure used by everybody wanting to send a request
1310  * in Lustre.
1311  */
1312 struct ptlrpc_request {
1313         /* Request type: one of PTL_RPC_MSG_* */
1314         int rq_type;
1315         /** Result of request processing */
1316         int rq_status;
1317         /**
1318          * Linkage item through which this request is included into
1319          * sending/delayed lists on client and into rqbd list on server
1320          */
1321         cfs_list_t rq_list;
1322         /**
1323          * Server side list of incoming unserved requests sorted by arrival
1324          * time.  Traversed from time to time to notice about to expire
1325          * requests and sent back "early replies" to clients to let them
1326          * know server is alive and well, just very busy to service their
1327          * requests in time
1328          */
1329         cfs_list_t rq_timed_list;
1330         /** server-side history, used for debuging purposes. */
1331         cfs_list_t rq_history_list;
1332         /** server-side per-export list */
1333         cfs_list_t rq_exp_list;
1334         /** server-side hp handlers */
1335         struct ptlrpc_hpreq_ops *rq_ops;
1336
1337         /** initial thread servicing this request */
1338         struct ptlrpc_thread *rq_svc_thread;
1339
1340         /** history sequence # */
1341         __u64 rq_history_seq;
1342         /** \addtogroup  nrs
1343          * @{
1344          */
1345         /** stub for NRS request */
1346         struct ptlrpc_nrs_request rq_nrq;
1347         /** @} nrs */
1348         /** the index of service's srv_at_array into which request is linked */
1349         time_t rq_at_index;
1350         /** Lock to protect request flags and some other important bits, like
1351          * rq_list
1352          */
1353         spinlock_t rq_lock;
1354         /** client-side flags are serialized by rq_lock */
1355         unsigned int rq_intr:1, rq_replied:1, rq_err:1,
1356                 rq_timedout:1, rq_resend:1, rq_restart:1,
1357                 /**
1358                  * when ->rq_replay is set, request is kept by the client even
1359                  * after server commits corresponding transaction. This is
1360                  * used for operations that require sequence of multiple
1361                  * requests to be replayed. The only example currently is file
1362                  * open/close. When last request in such a sequence is
1363                  * committed, ->rq_replay is cleared on all requests in the
1364                  * sequence.
1365                  */
1366                 rq_replay:1,
1367                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
1368                 rq_no_delay:1, rq_net_err:1, rq_wait_ctx:1,
1369                 rq_early:1, rq_must_unlink:1,
1370                 rq_memalloc:1,      /* req originated from "kswapd" */
1371                 /* server-side flags */
1372                 rq_packed_final:1,  /* packed final reply */
1373                 rq_hp:1,            /* high priority RPC */
1374                 rq_at_linked:1,     /* link into service's srv_at_array */
1375                 rq_reply_truncate:1,
1376                 rq_committed:1,
1377                 /* whether the "rq_set" is a valid one */
1378                 rq_invalid_rqset:1,
1379                 rq_generation_set:1,
1380                 /* do not resend request on -EINPROGRESS */
1381                 rq_no_retry_einprogress:1,
1382                 /* allow the req to be sent if the import is in recovery
1383                  * status */
1384                 rq_allow_replay:1;
1385
1386         unsigned int rq_nr_resend;
1387
1388         enum rq_phase rq_phase; /* one of RQ_PHASE_* */
1389         enum rq_phase rq_next_phase; /* one of RQ_PHASE_* to be used next */
1390         cfs_atomic_t rq_refcount;/* client-side refcount for SENT race,
1391                                     server-side refcounf for multiple replies */
1392
1393         /** Portal to which this request would be sent */
1394         short rq_request_portal;  /* XXX FIXME bug 249 */
1395         /** Portal where to wait for reply and where reply would be sent */
1396         short rq_reply_portal;    /* XXX FIXME bug 249 */
1397
1398         /**
1399          * client-side:
1400          * !rq_truncate : # reply bytes actually received,
1401          *  rq_truncate : required repbuf_len for resend
1402          */
1403         int rq_nob_received;
1404         /** Request length */
1405         int rq_reqlen;
1406         /** Reply length */
1407         int rq_replen;
1408         /** Request message - what client sent */
1409         struct lustre_msg *rq_reqmsg;
1410         /** Reply message - server response */
1411         struct lustre_msg *rq_repmsg;
1412         /** Transaction number */
1413         __u64 rq_transno;
1414         /** xid */
1415         __u64 rq_xid;
1416         /**
1417          * List item to for replay list. Not yet commited requests get linked
1418          * there.
1419          * Also see \a rq_replay comment above.
1420          */
1421         cfs_list_t rq_replay_list;
1422
1423         /**
1424          * security and encryption data
1425          * @{ */
1426         struct ptlrpc_cli_ctx   *rq_cli_ctx;     /**< client's half ctx */
1427         struct ptlrpc_svc_ctx   *rq_svc_ctx;     /**< server's half ctx */
1428         cfs_list_t               rq_ctx_chain;   /**< link to waited ctx */
1429
1430         struct sptlrpc_flavor    rq_flvr;        /**< for client & server */
1431         enum lustre_sec_part     rq_sp_from;
1432
1433         /* client/server security flags */
1434         unsigned int
1435                                  rq_ctx_init:1,      /* context initiation */
1436                                  rq_ctx_fini:1,      /* context destroy */
1437                                  rq_bulk_read:1,     /* request bulk read */
1438                                  rq_bulk_write:1,    /* request bulk write */
1439                                  /* server authentication flags */
1440                                  rq_auth_gss:1,      /* authenticated by gss */
1441                                  rq_auth_remote:1,   /* authed as remote user */
1442                                  rq_auth_usr_root:1, /* authed as root */
1443                                  rq_auth_usr_mdt:1,  /* authed as mdt */
1444                                  rq_auth_usr_ost:1,  /* authed as ost */
1445                                  /* security tfm flags */
1446                                  rq_pack_udesc:1,
1447                                  rq_pack_bulk:1,
1448                                  /* doesn't expect reply FIXME */
1449                                  rq_no_reply:1,
1450                                  rq_pill_init:1;     /* pill initialized */
1451
1452         uid_t                    rq_auth_uid;        /* authed uid */
1453         uid_t                    rq_auth_mapped_uid; /* authed uid mapped to */
1454
1455         /* (server side), pointed directly into req buffer */
1456         struct ptlrpc_user_desc *rq_user_desc;
1457
1458         /* various buffer pointers */
1459         struct lustre_msg       *rq_reqbuf;      /* req wrapper */
1460         char                    *rq_repbuf;      /* rep buffer */
1461         struct lustre_msg       *rq_repdata;     /* rep wrapper msg */
1462         struct lustre_msg       *rq_clrbuf;      /* only in priv mode */
1463         int                      rq_reqbuf_len;  /* req wrapper buf len */
1464         int                      rq_reqdata_len; /* req wrapper msg len */
1465         int                      rq_repbuf_len;  /* rep buffer len */
1466         int                      rq_repdata_len; /* rep wrapper msg len */
1467         int                      rq_clrbuf_len;  /* only in priv mode */
1468         int                      rq_clrdata_len; /* only in priv mode */
1469
1470         /** early replies go to offset 0, regular replies go after that */
1471         unsigned int             rq_reply_off;
1472
1473         /** @} */
1474
1475         /** Fields that help to see if request and reply were swabbed or not */
1476         __u32 rq_req_swab_mask;
1477         __u32 rq_rep_swab_mask;
1478
1479         /** What was import generation when this request was sent */
1480         int rq_import_generation;
1481         enum lustre_imp_state rq_send_state;
1482
1483         /** how many early replies (for stats) */
1484         int rq_early_count;
1485
1486         /** client+server request */
1487         lnet_handle_md_t     rq_req_md_h;
1488         struct ptlrpc_cb_id  rq_req_cbid;
1489         /** optional time limit for send attempts */
1490         cfs_duration_t       rq_delay_limit;
1491         /** time request was first queued */
1492         cfs_time_t           rq_queued_time;
1493
1494         /* server-side... */
1495         /** request arrival time */
1496         struct timeval       rq_arrival_time;
1497         /** separated reply state */
1498         struct ptlrpc_reply_state *rq_reply_state;
1499         /** incoming request buffer */
1500         struct ptlrpc_request_buffer_desc *rq_rqbd;
1501
1502         /** client-only incoming reply */
1503         lnet_handle_md_t     rq_reply_md_h;
1504         cfs_waitq_t          rq_reply_waitq;
1505         struct ptlrpc_cb_id  rq_reply_cbid;
1506
1507         /** our LNet NID */
1508         lnet_nid_t           rq_self;
1509         /** Peer description (the other side) */
1510         lnet_process_id_t    rq_peer;
1511         /** Server-side, export on which request was received */
1512         struct obd_export   *rq_export;
1513         /** Client side, import where request is being sent */
1514         struct obd_import   *rq_import;
1515
1516         /** Replay callback, called after request is replayed at recovery */
1517         void (*rq_replay_cb)(struct ptlrpc_request *);
1518         /**
1519          * Commit callback, called when request is committed and about to be
1520          * freed.
1521          */
1522         void (*rq_commit_cb)(struct ptlrpc_request *);
1523         /** Opaq data for replay and commit callbacks. */
1524         void  *rq_cb_data;
1525
1526         /** For bulk requests on client only: bulk descriptor */
1527         struct ptlrpc_bulk_desc *rq_bulk;
1528
1529         /** client outgoing req */
1530         /**
1531          * when request/reply sent (secs), or time when request should be sent
1532          */
1533         time_t rq_sent;
1534         /** time for request really sent out */
1535         time_t rq_real_sent;
1536
1537         /** when request must finish. volatile
1538          * so that servers' early reply updates to the deadline aren't
1539          * kept in per-cpu cache */
1540         volatile time_t rq_deadline;
1541         /** when req reply unlink must finish. */
1542         time_t rq_reply_deadline;
1543         /** when req bulk unlink must finish. */
1544         time_t rq_bulk_deadline;
1545         /**
1546          * service time estimate (secs) 
1547          * If the requestsis not served by this time, it is marked as timed out.
1548          */
1549         int    rq_timeout;
1550
1551         /** Multi-rpc bits */
1552         /** Per-request waitq introduced by bug 21938 for recovery waiting */
1553         cfs_waitq_t rq_set_waitq;
1554         /** Link item for request set lists */
1555         cfs_list_t  rq_set_chain;
1556         /** Link back to the request set */
1557         struct ptlrpc_request_set *rq_set;
1558         /** Async completion handler, called when reply is received */
1559         ptlrpc_interpterer_t rq_interpret_reply;
1560         /** Async completion context */
1561         union ptlrpc_async_args rq_async_args;
1562
1563         /** Pool if request is from preallocated list */
1564         struct ptlrpc_request_pool *rq_pool;
1565
1566         struct lu_context           rq_session;
1567         struct lu_context           rq_recov_session;
1568
1569         /** request format description */
1570         struct req_capsule          rq_pill;
1571 };
1572
1573 /**
1574  * Call completion handler for rpc if any, return it's status or original
1575  * rc if there was no handler defined for this request.
1576  */
1577 static inline int ptlrpc_req_interpret(const struct lu_env *env,
1578                                        struct ptlrpc_request *req, int rc)
1579 {
1580         if (req->rq_interpret_reply != NULL) {
1581                 req->rq_status = req->rq_interpret_reply(env, req,
1582                                                          &req->rq_async_args,
1583                                                          rc);
1584                 return req->rq_status;
1585         }
1586         return rc;
1587 }
1588
1589 /** \addtogroup  nrs
1590  * @{
1591  */
1592 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_desc *desc);
1593 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_desc *desc);
1594 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req);
1595 void nrs_policy_get_info_locked(struct ptlrpc_nrs_policy *policy,
1596                                 struct ptlrpc_nrs_pol_info *info);
1597
1598 /*
1599  * Can the request be moved from the regular NRS head to the high-priority NRS
1600  * head (of the same PTLRPC service partition), if any?
1601  *
1602  * For a reliable result, this should be checked under svcpt->scp_req lock.
1603  */
1604 static inline bool
1605 ptlrpc_nrs_req_can_move(struct ptlrpc_request *req)
1606 {
1607         struct ptlrpc_nrs_request *nrq = &req->rq_nrq;
1608
1609         /**
1610          * LU-898: Check ptlrpc_nrs_request::nr_enqueued to make sure the
1611          * request has been enqueued first, and ptlrpc_nrs_request::nr_started
1612          * to make sure it has not been scheduled yet (analogous to previous
1613          * (non-NRS) checking of !list_empty(&ptlrpc_request::rq_list).
1614          */
1615         return nrq->nr_enqueued && !nrq->nr_started && !req->rq_hp;
1616 }
1617 /** @} nrs */
1618
1619 /**
1620  * Returns 1 if request buffer at offset \a index was already swabbed
1621  */
1622 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
1623 {
1624         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
1625         return req->rq_req_swab_mask & (1 << index);
1626 }
1627
1628 /**
1629  * Returns 1 if request reply buffer at offset \a index was already swabbed
1630  */
1631 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
1632 {
1633         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
1634         return req->rq_rep_swab_mask & (1 << index);
1635 }
1636
1637 /**
1638  * Returns 1 if request needs to be swabbed into local cpu byteorder
1639  */
1640 static inline int ptlrpc_req_need_swab(struct ptlrpc_request *req)
1641 {
1642         return lustre_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1643 }
1644
1645 /**
1646  * Returns 1 if request reply needs to be swabbed into local cpu byteorder
1647  */
1648 static inline int ptlrpc_rep_need_swab(struct ptlrpc_request *req)
1649 {
1650         return lustre_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1651 }
1652
1653 /**
1654  * Mark request buffer at offset \a index that it was already swabbed
1655  */
1656 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
1657 {
1658         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
1659         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
1660         req->rq_req_swab_mask |= 1 << index;
1661 }
1662
1663 /**
1664  * Mark request reply buffer at offset \a index that it was already swabbed
1665  */
1666 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
1667 {
1668         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
1669         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
1670         req->rq_rep_swab_mask |= 1 << index;
1671 }
1672
1673 /**
1674  * Convert numerical request phase value \a phase into text string description
1675  */
1676 static inline const char *
1677 ptlrpc_phase2str(enum rq_phase phase)
1678 {
1679         switch (phase) {
1680         case RQ_PHASE_NEW:
1681                 return "New";
1682         case RQ_PHASE_RPC:
1683                 return "Rpc";
1684         case RQ_PHASE_BULK:
1685                 return "Bulk";
1686         case RQ_PHASE_INTERPRET:
1687                 return "Interpret";
1688         case RQ_PHASE_COMPLETE:
1689                 return "Complete";
1690         case RQ_PHASE_UNREGISTERING:
1691                 return "Unregistering";
1692         default:
1693                 return "?Phase?";
1694         }
1695 }
1696
1697 /**
1698  * Convert numerical request phase of the request \a req into text stringi
1699  * description
1700  */
1701 static inline const char *
1702 ptlrpc_rqphase2str(struct ptlrpc_request *req)
1703 {
1704         return ptlrpc_phase2str(req->rq_phase);
1705 }
1706
1707 /**
1708  * Debugging functions and helpers to print request structure into debug log
1709  * @{
1710  */ 
1711 /* Spare the preprocessor, spoil the bugs. */
1712 #define FLAG(field, str) (field ? str : "")
1713
1714 /** Convert bit flags into a string */
1715 #define DEBUG_REQ_FLAGS(req)                                                    \
1716         ptlrpc_rqphase2str(req),                                                \
1717         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                    \
1718         FLAG(req->rq_err, "E"),                                                 \
1719         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"),   \
1720         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                  \
1721         FLAG(req->rq_no_resend, "N"),                                           \
1722         FLAG(req->rq_waiting, "W"),                                             \
1723         FLAG(req->rq_wait_ctx, "C"), FLAG(req->rq_hp, "H"),                     \
1724         FLAG(req->rq_committed, "M")
1725
1726 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s%s%s%s"
1727
1728 void _debug_req(struct ptlrpc_request *req,
1729                 struct libcfs_debug_msg_data *data, const char *fmt, ...)
1730         __attribute__ ((format (printf, 3, 4)));
1731
1732 /**
1733  * Helper that decides if we need to print request accordig to current debug
1734  * level settings
1735  */
1736 #define debug_req(msgdata, mask, cdls, req, fmt, a...)                        \
1737 do {                                                                          \
1738         CFS_CHECK_STACK(msgdata, mask, cdls);                                 \
1739                                                                               \
1740         if (((mask) & D_CANTMASK) != 0 ||                                     \
1741             ((libcfs_debug & (mask)) != 0 &&                                  \
1742              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))                \
1743                 _debug_req((req), msgdata, fmt, ##a);                         \
1744 } while(0)
1745
1746 /**
1747  * This is the debug print function you need to use to print request sturucture
1748  * content into lustre debug log.
1749  * for most callers (level is a constant) this is resolved at compile time */
1750 #define DEBUG_REQ(level, req, fmt, args...)                                   \
1751 do {                                                                          \
1752         if ((level) & (D_ERROR | D_WARNING)) {                                \
1753                 static cfs_debug_limit_state_t cdls;                          \
1754                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, &cdls);            \
1755                 debug_req(&msgdata, level, &cdls, req, "@@@ "fmt" ", ## args);\
1756         } else {                                                              \
1757                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, NULL);             \
1758                 debug_req(&msgdata, level, NULL, req, "@@@ "fmt" ", ## args); \
1759         }                                                                     \
1760 } while (0)
1761 /** @} */
1762
1763 /**
1764  * Structure that defines a single page of a bulk transfer
1765  */
1766 struct ptlrpc_bulk_page {
1767         /** Linkage to list of pages in a bulk */
1768         cfs_list_t       bp_link;
1769         /**
1770          * Number of bytes in a page to transfer starting from \a bp_pageoffset
1771          */
1772         int              bp_buflen;
1773         /** offset within a page */
1774         int              bp_pageoffset;
1775         /** The page itself */
1776         struct page     *bp_page;
1777 };
1778
1779 #define BULK_GET_SOURCE   0
1780 #define BULK_PUT_SINK     1
1781 #define BULK_GET_SINK     2
1782 #define BULK_PUT_SOURCE   3
1783
1784 /**
1785  * Definition of buk descriptor.
1786  * Bulks are special "Two phase" RPCs where initial request message
1787  * is sent first and it is followed bt a transfer (o receiving) of a large
1788  * amount of data to be settled into pages referenced from the bulk descriptors.
1789  * Bulks transfers (the actual data following the small requests) are done
1790  * on separate LNet portals.
1791  * In lustre we use bulk transfers for READ and WRITE transfers from/to OSTs.
1792  *  Another user is readpage for MDT.
1793  */
1794 struct ptlrpc_bulk_desc {
1795         /** completed successfully */
1796         unsigned long bd_success:1;
1797         /** accessible to the network (network io potentially in progress) */
1798         unsigned long bd_network_rw:1;
1799         /** {put,get}{source,sink} */
1800         unsigned long bd_type:2;
1801         /** client side */
1802         unsigned long bd_registered:1;
1803         /** For serialization with callback */
1804         spinlock_t bd_lock;
1805         /** Import generation when request for this bulk was sent */
1806         int bd_import_generation;
1807         /** Server side - export this bulk created for */
1808         struct obd_export *bd_export;
1809         /** Client side - import this bulk was sent on */
1810         struct obd_import *bd_import;
1811         /** LNet portal for this bulk */
1812         __u32 bd_portal;
1813         /** Back pointer to the request */
1814         struct ptlrpc_request *bd_req;
1815         cfs_waitq_t            bd_waitq;        /* server side only WQ */
1816         int                    bd_iov_count;    /* # entries in bd_iov */
1817         int                    bd_max_iov;      /* allocated size of bd_iov */
1818         int                    bd_nob;          /* # bytes covered */
1819         int                    bd_nob_transferred; /* # bytes GOT/PUT */
1820
1821         __u64                  bd_last_xid;
1822
1823         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
1824         lnet_handle_md_t       bd_md_h;         /* associated MD */
1825         lnet_nid_t             bd_sender;       /* stash event::sender */
1826
1827 #if defined(__KERNEL__)
1828         /*
1829          * encrypt iov, size is either 0 or bd_iov_count.
1830          */
1831         lnet_kiov_t           *bd_enc_iov;
1832
1833         lnet_kiov_t            bd_iov[0];
1834 #else
1835         lnet_md_iovec_t        bd_iov[0];
1836 #endif
1837 };
1838
1839 enum {
1840         SVC_STOPPED     = 1 << 0,
1841         SVC_STOPPING    = 1 << 1,
1842         SVC_STARTING    = 1 << 2,
1843         SVC_RUNNING     = 1 << 3,
1844         SVC_EVENT       = 1 << 4,
1845         SVC_SIGNAL      = 1 << 5,
1846 };
1847
1848 #define PTLRPC_THR_NAME_LEN             32
1849 /**
1850  * Definition of server service thread structure
1851  */
1852 struct ptlrpc_thread {
1853         /**
1854          * List of active threads in svc->srv_threads
1855          */
1856         cfs_list_t t_link;
1857         /**
1858          * thread-private data (preallocated memory)
1859          */
1860         void *t_data;
1861         __u32 t_flags;
1862         /**
1863          * service thread index, from ptlrpc_start_threads
1864          */
1865         unsigned int t_id;
1866         /**
1867          * service thread pid
1868          */
1869         pid_t t_pid; 
1870         /**
1871          * put watchdog in the structure per thread b=14840
1872          */
1873         struct lc_watchdog *t_watchdog;
1874         /**
1875          * the svc this thread belonged to b=18582
1876          */
1877         struct ptlrpc_service_part      *t_svcpt;
1878         cfs_waitq_t                     t_ctl_waitq;
1879         struct lu_env                   *t_env;
1880         char                            t_name[PTLRPC_THR_NAME_LEN];
1881 };
1882
1883 static inline int thread_is_init(struct ptlrpc_thread *thread)
1884 {
1885         return thread->t_flags == 0;
1886 }
1887
1888 static inline int thread_is_stopped(struct ptlrpc_thread *thread)
1889 {
1890         return !!(thread->t_flags & SVC_STOPPED);
1891 }
1892
1893 static inline int thread_is_stopping(struct ptlrpc_thread *thread)
1894 {
1895         return !!(thread->t_flags & SVC_STOPPING);
1896 }
1897
1898 static inline int thread_is_starting(struct ptlrpc_thread *thread)
1899 {
1900         return !!(thread->t_flags & SVC_STARTING);
1901 }
1902
1903 static inline int thread_is_running(struct ptlrpc_thread *thread)
1904 {
1905         return !!(thread->t_flags & SVC_RUNNING);
1906 }
1907
1908 static inline int thread_is_event(struct ptlrpc_thread *thread)
1909 {
1910         return !!(thread->t_flags & SVC_EVENT);
1911 }
1912
1913 static inline int thread_is_signal(struct ptlrpc_thread *thread)
1914 {
1915         return !!(thread->t_flags & SVC_SIGNAL);
1916 }
1917
1918 static inline void thread_clear_flags(struct ptlrpc_thread *thread, __u32 flags)
1919 {
1920         thread->t_flags &= ~flags;
1921 }
1922
1923 static inline void thread_set_flags(struct ptlrpc_thread *thread, __u32 flags)
1924 {
1925         thread->t_flags = flags;
1926 }
1927
1928 static inline void thread_add_flags(struct ptlrpc_thread *thread, __u32 flags)
1929 {
1930         thread->t_flags |= flags;
1931 }
1932
1933 static inline int thread_test_and_clear_flags(struct ptlrpc_thread *thread,
1934                                               __u32 flags)
1935 {
1936         if (thread->t_flags & flags) {
1937                 thread->t_flags &= ~flags;
1938                 return 1;
1939         }
1940         return 0;
1941 }
1942
1943 /**
1944  * Request buffer descriptor structure.
1945  * This is a structure that contains one posted request buffer for service.
1946  * Once data land into a buffer, event callback creates actual request and
1947  * notifies wakes one of the service threads to process new incoming request.
1948  * More than one request can fit into the buffer.
1949  */
1950 struct ptlrpc_request_buffer_desc {
1951         /** Link item for rqbds on a service */
1952         cfs_list_t             rqbd_list;
1953         /** History of requests for this buffer */
1954         cfs_list_t             rqbd_reqs;
1955         /** Back pointer to service for which this buffer is registered */
1956         struct ptlrpc_service_part *rqbd_svcpt;
1957         /** LNet descriptor */
1958         lnet_handle_md_t       rqbd_md_h;
1959         int                    rqbd_refcount;
1960         /** The buffer itself */
1961         char                  *rqbd_buffer;
1962         struct ptlrpc_cb_id    rqbd_cbid;
1963         /**
1964          * This "embedded" request structure is only used for the
1965          * last request to fit into the buffer
1966          */
1967         struct ptlrpc_request  rqbd_req;
1968 };
1969
1970 typedef int  (*svc_handler_t)(struct ptlrpc_request *req);
1971
1972 struct ptlrpc_service_ops {
1973         /**
1974          * if non-NULL called during thread creation (ptlrpc_start_thread())
1975          * to initialize service specific per-thread state.
1976          */
1977         int             (*so_thr_init)(struct ptlrpc_thread *thr);
1978         /**
1979          * if non-NULL called during thread shutdown (ptlrpc_main()) to
1980          * destruct state created by ->srv_init().
1981          */
1982         void            (*so_thr_done)(struct ptlrpc_thread *thr);
1983         /**
1984          * Handler function for incoming requests for this service
1985          */
1986         int             (*so_req_handler)(struct ptlrpc_request *req);
1987         /**
1988          * function to determine priority of the request, it's called
1989          * on every new request
1990          */
1991         int             (*so_hpreq_handler)(struct ptlrpc_request *);
1992         /**
1993          * service-specific print fn
1994          */
1995         void            (*so_req_printer)(void *, struct ptlrpc_request *);
1996 };
1997
1998 #ifndef __cfs_cacheline_aligned
1999 /* NB: put it here for reducing patche dependence */
2000 # define __cfs_cacheline_aligned
2001 #endif
2002
2003 /**
2004  * How many high priority requests to serve before serving one normal
2005  * priority request
2006  */
2007 #define PTLRPC_SVC_HP_RATIO 10
2008
2009 /**
2010  * Definition of PortalRPC service.
2011  * The service is listening on a particular portal (like tcp port)
2012  * and perform actions for a specific server like IO service for OST
2013  * or general metadata service for MDS.
2014  */
2015 struct ptlrpc_service {
2016         /** serialize /proc operations */
2017         spinlock_t                      srv_lock;
2018         /** most often accessed fields */
2019         /** chain thru all services */
2020         cfs_list_t                      srv_list;
2021         /** service operations table */
2022         struct ptlrpc_service_ops       srv_ops;
2023         /** only statically allocated strings here; we don't clean them */
2024         char                           *srv_name;
2025         /** only statically allocated strings here; we don't clean them */
2026         char                           *srv_thread_name;
2027         /** service thread list */
2028         cfs_list_t                      srv_threads;
2029         /** threads # should be created for each partition on initializing */
2030         int                             srv_nthrs_cpt_init;
2031         /** limit of threads number for each partition */
2032         int                             srv_nthrs_cpt_limit;
2033         /** Root of /proc dir tree for this service */
2034         cfs_proc_dir_entry_t           *srv_procroot;
2035         /** Pointer to statistic data for this service */
2036         struct lprocfs_stats           *srv_stats;
2037         /** # hp per lp reqs to handle */
2038         int                             srv_hpreq_ratio;
2039         /** biggest request to receive */
2040         int                             srv_max_req_size;
2041         /** biggest reply to send */
2042         int                             srv_max_reply_size;
2043         /** size of individual buffers */
2044         int                             srv_buf_size;
2045         /** # buffers to allocate in 1 group */
2046         int                             srv_nbuf_per_group;
2047         /** Local portal on which to receive requests */
2048         __u32                           srv_req_portal;
2049         /** Portal on the client to send replies to */
2050         __u32                           srv_rep_portal;
2051         /**
2052          * Tags for lu_context associated with this thread, see struct
2053          * lu_context.
2054          */
2055         __u32                           srv_ctx_tags;
2056         /** soft watchdog timeout multiplier */
2057         int                             srv_watchdog_factor;
2058         /** under unregister_service */
2059         unsigned                        srv_is_stopping:1;
2060
2061         /** max # request buffers in history per partition */
2062         int                             srv_hist_nrqbds_cpt_max;
2063         /** number of CPTs this service bound on */
2064         int                             srv_ncpts;
2065         /** CPTs array this service bound on */
2066         __u32                           *srv_cpts;
2067         /** 2^srv_cptab_bits >= cfs_cpt_numbert(srv_cptable) */
2068         int                             srv_cpt_bits;
2069         /** CPT table this service is running over */
2070         struct cfs_cpt_table            *srv_cptable;
2071         /**
2072          * partition data for ptlrpc service
2073          */
2074         struct ptlrpc_service_part      *srv_parts[0];
2075 };
2076
2077 /**
2078  * Definition of PortalRPC service partition data.
2079  * Although a service only has one instance of it right now, but we
2080  * will have multiple instances very soon (instance per CPT).
2081  *
2082  * it has four locks:
2083  * \a scp_lock
2084  *    serialize operations on rqbd and requests waiting for preprocess
2085  * \a scp_req_lock
2086  *    serialize operations active requests sent to this portal
2087  * \a scp_at_lock
2088  *    serialize adaptive timeout stuff
2089  * \a scp_rep_lock
2090  *    serialize operations on RS list (reply states)
2091  *
2092  * We don't have any use-case to take two or more locks at the same time
2093  * for now, so there is no lock order issue.
2094  */
2095 struct ptlrpc_service_part {
2096         /** back reference to owner */
2097         struct ptlrpc_service           *scp_service __cfs_cacheline_aligned;
2098         /* CPT id, reserved */
2099         int                             scp_cpt;
2100         /** always increasing number */
2101         int                             scp_thr_nextid;
2102         /** # of starting threads */
2103         int                             scp_nthrs_starting;
2104         /** # of stopping threads, reserved for shrinking threads */
2105         int                             scp_nthrs_stopping;
2106         /** # running threads */
2107         int                             scp_nthrs_running;
2108         /** service threads list */
2109         cfs_list_t                      scp_threads;
2110
2111         /**
2112          * serialize the following fields, used for protecting
2113          * rqbd list and incoming requests waiting for preprocess,
2114          * threads starting & stopping are also protected by this lock.
2115          */
2116         spinlock_t                      scp_lock  __cfs_cacheline_aligned;
2117         /** total # req buffer descs allocated */
2118         int                             scp_nrqbds_total;
2119         /** # posted request buffers for receiving */
2120         int                             scp_nrqbds_posted;
2121         /** in progress of allocating rqbd */
2122         int                             scp_rqbd_allocating;
2123         /** # incoming reqs */
2124         int                             scp_nreqs_incoming;
2125         /** request buffers to be reposted */
2126         cfs_list_t                      scp_rqbd_idle;
2127         /** req buffers receiving */
2128         cfs_list_t                      scp_rqbd_posted;
2129         /** incoming reqs */
2130         cfs_list_t                      scp_req_incoming;
2131         /** timeout before re-posting reqs, in tick */
2132         cfs_duration_t                  scp_rqbd_timeout;
2133         /**
2134          * all threads sleep on this. This wait-queue is signalled when new
2135          * incoming request arrives and when difficult reply has to be handled.
2136          */
2137         cfs_waitq_t                     scp_waitq;
2138
2139         /** request history */
2140         cfs_list_t                      scp_hist_reqs;
2141         /** request buffer history */
2142         cfs_list_t                      scp_hist_rqbds;
2143         /** # request buffers in history */
2144         int                             scp_hist_nrqbds;
2145         /** sequence number for request */
2146         __u64                           scp_hist_seq;
2147         /** highest seq culled from history */
2148         __u64                           scp_hist_seq_culled;
2149
2150         /**
2151          * serialize the following fields, used for processing requests
2152          * sent to this portal
2153          */
2154         spinlock_t                      scp_req_lock __cfs_cacheline_aligned;
2155         /** # reqs in either of the NRS heads below */
2156         /** # reqs being served */
2157         int                             scp_nreqs_active;
2158         /** # HPreqs being served */
2159         int                             scp_nhreqs_active;
2160         /** # hp requests handled */
2161         int                             scp_hreq_count;
2162
2163         /** NRS head for regular requests */
2164         struct ptlrpc_nrs               scp_nrs_reg;
2165         /** NRS head for HP requests; this is only valid for services that can
2166          *  handle HP requests */
2167         struct ptlrpc_nrs              *scp_nrs_hp;
2168
2169         /** AT stuff */
2170         /** @{ */
2171         /**
2172          * serialize the following fields, used for changes on
2173          * adaptive timeout
2174          */
2175         spinlock_t                      scp_at_lock __cfs_cacheline_aligned;
2176         /** estimated rpc service time */
2177         struct adaptive_timeout         scp_at_estimate;
2178         /** reqs waiting for replies */
2179         struct ptlrpc_at_array          scp_at_array;
2180         /** early reply timer */
2181         cfs_timer_t                     scp_at_timer;
2182         /** debug */
2183         cfs_time_t                      scp_at_checktime;
2184         /** check early replies */
2185         unsigned                        scp_at_check;
2186         /** @} */
2187
2188         /**
2189          * serialize the following fields, used for processing
2190          * replies for this portal
2191          */
2192         spinlock_t                      scp_rep_lock __cfs_cacheline_aligned;
2193         /** all the active replies */
2194         cfs_list_t                      scp_rep_active;
2195 #ifndef __KERNEL__
2196         /** replies waiting for service */
2197         cfs_list_t                      scp_rep_queue;
2198 #endif
2199         /** List of free reply_states */
2200         cfs_list_t                      scp_rep_idle;
2201         /** waitq to run, when adding stuff to srv_free_rs_list */
2202         cfs_waitq_t                     scp_rep_waitq;
2203         /** # 'difficult' replies */
2204         cfs_atomic_t                    scp_nreps_difficult;
2205 };
2206
2207 #define ptlrpc_service_for_each_part(part, i, svc)                      \
2208         for (i = 0;                                                     \
2209              i < (svc)->srv_ncpts &&                                    \
2210              (svc)->srv_parts != NULL &&                                \
2211              ((part) = (svc)->srv_parts[i]) != NULL; i++)
2212
2213 /**
2214  * Declaration of ptlrpcd control structure
2215  */
2216 struct ptlrpcd_ctl {
2217         /**
2218          * Ptlrpc thread control flags (LIOD_START, LIOD_STOP, LIOD_FORCE)
2219          */
2220         unsigned long                   pc_flags;
2221         /**
2222          * Thread lock protecting structure fields.
2223          */
2224         spinlock_t                      pc_lock;
2225         /**
2226          * Start completion.
2227          */
2228         struct completion               pc_starting;
2229         /**
2230          * Stop completion.
2231          */
2232         struct completion               pc_finishing;
2233         /**
2234          * Thread requests set.
2235          */
2236         struct ptlrpc_request_set  *pc_set;
2237         /**
2238          * Thread name used in cfs_daemonize()
2239          */
2240         char                        pc_name[16];
2241         /**
2242          * Environment for request interpreters to run in.
2243          */
2244         struct lu_env               pc_env;
2245         /**
2246          * Index of ptlrpcd thread in the array.
2247          */
2248         int                         pc_index;
2249         /**
2250          * Number of the ptlrpcd's partners.
2251          */
2252         int                         pc_npartners;
2253         /**
2254          * Pointer to the array of partners' ptlrpcd_ctl structure.
2255          */
2256         struct ptlrpcd_ctl        **pc_partners;
2257         /**
2258          * Record the partner index to be processed next.
2259          */
2260         int                         pc_cursor;
2261 #ifndef __KERNEL__
2262         /**
2263          * Async rpcs flag to make sure that ptlrpcd_check() is called only
2264          * once.
2265          */
2266         int                         pc_recurred;
2267         /**
2268          * Currently not used.
2269          */
2270         void                       *pc_callback;
2271         /**
2272          * User-space async rpcs callback.
2273          */
2274         void                       *pc_wait_callback;
2275         /**
2276          * User-space check idle rpcs callback.
2277          */
2278         void                       *pc_idle_callback;
2279 #endif
2280 };
2281
2282 /* Bits for pc_flags */
2283 enum ptlrpcd_ctl_flags {
2284         /**
2285          * Ptlrpc thread start flag.
2286          */
2287         LIOD_START       = 1 << 0,
2288         /**
2289          * Ptlrpc thread stop flag.
2290          */
2291         LIOD_STOP        = 1 << 1,
2292         /**
2293          * Ptlrpc thread force flag (only stop force so far).
2294          * This will cause aborting any inflight rpcs handled
2295          * by thread if LIOD_STOP is specified.
2296          */
2297         LIOD_FORCE       = 1 << 2,
2298         /**
2299          * This is a recovery ptlrpc thread.
2300          */
2301         LIOD_RECOVERY    = 1 << 3,
2302         /**
2303          * The ptlrpcd is bound to some CPU core.
2304          */
2305         LIOD_BIND        = 1 << 4,
2306 };
2307
2308 /**
2309  * \addtogroup nrs
2310  * @{
2311  *
2312  * Service compatibility function; policy is compatible with all services.
2313  *
2314  * \param[in] svc  The service the policy is attempting to register with.
2315  * \param[in] desc The policy descriptor
2316  *
2317  * \retval true The policy is compatible with the NRS head
2318  *
2319  * \see ptlrpc_nrs_pol_desc::pd_compat()
2320  */
2321 static inline bool
2322 nrs_policy_compat_all(struct ptlrpc_service *svc,
2323                       const struct ptlrpc_nrs_pol_desc *desc)
2324 {
2325         return true;
2326 }
2327
2328 /**
2329  * Service compatibility function; policy is compatible with only a specific
2330  * service which is identified by its human-readable name at
2331  * ptlrpc_service::srv_name.
2332  *
2333  * \param[in] svc  The service the policy is attempting to register with.
2334  * \param[in] desc The policy descriptor
2335  *
2336  * \retval false The policy is not compatible with the NRS head
2337  * \retval true  The policy is compatible with the NRS head
2338  *
2339  * \see ptlrpc_nrs_pol_desc::pd_compat()
2340  */
2341 static inline bool
2342 nrs_policy_compat_one(struct ptlrpc_service *svc,
2343                       const struct ptlrpc_nrs_pol_desc *desc)
2344 {
2345         LASSERT(desc->pd_compat_svc_name != NULL);
2346         return strcmp(svc->srv_name, desc->pd_compat_svc_name) == 0;
2347 }
2348
2349 /** @} nrs */
2350
2351 /* ptlrpc/events.c */
2352 extern lnet_handle_eq_t ptlrpc_eq_h;
2353 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
2354                                lnet_process_id_t *peer, lnet_nid_t *self);
2355 /**
2356  * These callbacks are invoked by LNet when something happened to
2357  * underlying buffer
2358  * @{
2359  */
2360 extern void request_out_callback(lnet_event_t *ev);
2361 extern void reply_in_callback(lnet_event_t *ev);
2362 extern void client_bulk_callback(lnet_event_t *ev);
2363 extern void request_in_callback(lnet_event_t *ev);
2364 extern void reply_out_callback(lnet_event_t *ev);
2365 #ifdef HAVE_SERVER_SUPPORT
2366 extern void server_bulk_callback(lnet_event_t *ev);
2367 #endif
2368 /** @} */
2369
2370 /* ptlrpc/connection.c */
2371 struct ptlrpc_connection *ptlrpc_connection_get(lnet_process_id_t peer,
2372                                                 lnet_nid_t self,
2373                                                 struct obd_uuid *uuid);
2374 int ptlrpc_connection_put(struct ptlrpc_connection *c);
2375 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
2376 int ptlrpc_connection_init(void);
2377 void ptlrpc_connection_fini(void);
2378 extern lnet_pid_t ptl_get_pid(void);
2379
2380 /* ptlrpc/niobuf.c */
2381 /**
2382  * Actual interfacing with LNet to put/get/register/unregister stuff
2383  * @{
2384  */
2385 #ifdef HAVE_SERVER_SUPPORT
2386 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
2387                                               int npages, int type, int portal);
2388 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
2389 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
2390
2391 static inline int ptlrpc_server_bulk_active(struct ptlrpc_bulk_desc *desc)
2392 {
2393         int rc;
2394
2395         LASSERT(desc != NULL);
2396
2397         spin_lock(&desc->bd_lock);
2398         rc = desc->bd_network_rw;
2399         spin_unlock(&desc->bd_lock);
2400         return rc;
2401 }
2402 #endif
2403
2404 int ptlrpc_register_bulk(struct ptlrpc_request *req);
2405 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async);
2406
2407 static inline int ptlrpc_client_bulk_active(struct ptlrpc_request *req)
2408 {
2409         struct ptlrpc_bulk_desc *desc;
2410         int                      rc;
2411
2412         LASSERT(req != NULL);
2413         desc = req->rq_bulk;
2414
2415         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
2416             req->rq_bulk_deadline > cfs_time_current_sec())
2417                 return 1;
2418
2419         if (!desc)
2420                 return 0;
2421
2422         spin_lock(&desc->bd_lock);
2423         rc = desc->bd_network_rw;
2424         spin_unlock(&desc->bd_lock);
2425         return rc;
2426 }
2427
2428 #define PTLRPC_REPLY_MAYBE_DIFFICULT 0x01
2429 #define PTLRPC_REPLY_EARLY           0x02
2430 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags);
2431 int ptlrpc_reply(struct ptlrpc_request *req);
2432 int ptlrpc_send_error(struct ptlrpc_request *req, int difficult);
2433 int ptlrpc_error(struct ptlrpc_request *req);
2434 void ptlrpc_resend_req(struct ptlrpc_request *request);
2435 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req);
2436 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
2437 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd);
2438 /** @} */
2439
2440 /* ptlrpc/client.c */
2441 /**
2442  * Client-side portals API. Everything to send requests, receive replies,
2443  * request queues, request management, etc.
2444  * @{
2445  */
2446 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
2447                         struct ptlrpc_client *);
2448 void ptlrpc_cleanup_client(struct obd_import *imp);
2449 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
2450
2451 int ptlrpc_queue_wait(struct ptlrpc_request *req);
2452 int ptlrpc_replay_req(struct ptlrpc_request *req);
2453 int ptlrpc_unregister_reply(struct ptlrpc_request *req, int async);
2454 void ptlrpc_restart_req(struct ptlrpc_request *req);
2455 void ptlrpc_abort_inflight(struct obd_import *imp);
2456 void ptlrpc_cleanup_imp(struct obd_import *imp);
2457 void ptlrpc_abort_set(struct ptlrpc_request_set *set);
2458
2459 struct ptlrpc_request_set *ptlrpc_prep_set(void);
2460 struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
2461                                              void *arg);
2462 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
2463                       set_interpreter_func fn, void *data);
2464 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
2465 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set);
2466 int ptlrpc_set_wait(struct ptlrpc_request_set *);
2467 int ptlrpc_expired_set(void *data);
2468 void ptlrpc_interrupted_set(void *data);
2469 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
2470 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
2471 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
2472 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
2473                             struct ptlrpc_request *req);
2474
2475 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
2476 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
2477
2478 struct ptlrpc_request_pool *
2479 ptlrpc_init_rq_pool(int, int,
2480                     void (*populate_pool)(struct ptlrpc_request_pool *, int));
2481
2482 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req);
2483 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
2484                                             const struct req_format *format);
2485 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
2486                                             struct ptlrpc_request_pool *,
2487                                             const struct req_format *format);
2488 void ptlrpc_request_free(struct ptlrpc_request *request);
2489 int ptlrpc_request_pack(struct ptlrpc_request *request,
2490                         __u32 version, int opcode);
2491 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
2492                                                 const struct req_format *format,
2493                                                 __u32 version, int opcode);
2494 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
2495                              __u32 version, int opcode, char **bufs,
2496                              struct ptlrpc_cli_ctx *ctx);
2497 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
2498                                        int opcode, int count, __u32 *lengths,
2499                                        char **bufs);
2500 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
2501                                              __u32 version, int opcode,
2502                                             int count, __u32 *lengths, char **bufs,
2503                                             struct ptlrpc_request_pool *pool);
2504 void ptlrpc_req_finished(struct ptlrpc_request *request);
2505 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
2506 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
2507 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
2508                                               int npages, int type, int portal);
2509 void __ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk, int pin);
2510 static inline void ptlrpc_free_bulk_pin(struct ptlrpc_bulk_desc *bulk)
2511 {
2512         __ptlrpc_free_bulk(bulk, 1);
2513 }
2514 static inline void ptlrpc_free_bulk_nopin(struct ptlrpc_bulk_desc *bulk)
2515 {
2516         __ptlrpc_free_bulk(bulk, 0);
2517 }
2518 void __ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
2519                              cfs_page_t *page, int pageoffset, int len, int);
2520 static inline void ptlrpc_prep_bulk_page_pin(struct ptlrpc_bulk_desc *desc,
2521                                              cfs_page_t *page, int pageoffset,
2522                                              int len)
2523 {
2524         __ptlrpc_prep_bulk_page(desc, page, pageoffset, len, 1);
2525 }
2526
2527 static inline void ptlrpc_prep_bulk_page_nopin(struct ptlrpc_bulk_desc *desc,
2528                                                cfs_page_t *page, int pageoffset,
2529                                                int len)
2530 {
2531         __ptlrpc_prep_bulk_page(desc, page, pageoffset, len, 0);
2532 }
2533
2534 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2535                                       struct obd_import *imp);
2536 __u64 ptlrpc_next_xid(void);
2537 __u64 ptlrpc_sample_next_xid(void);
2538 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
2539
2540 /* Set of routines to run a function in ptlrpcd context */
2541 void *ptlrpcd_alloc_work(struct obd_import *imp,
2542                          int (*cb)(const struct lu_env *, void *), void *data);
2543 void ptlrpcd_destroy_work(void *handler);
2544 int ptlrpcd_queue_work(void *handler);
2545
2546 /** @} */
2547 struct ptlrpc_service_buf_conf {
2548         /* nbufs is how many buffers to post */
2549         unsigned int                    bc_nbufs;
2550         /* buffer size to post */
2551         unsigned int                    bc_buf_size;
2552         /* portal to listed for requests on */
2553         unsigned int                    bc_req_portal;
2554         /* portal of where to send replies to */
2555         unsigned int                    bc_rep_portal;
2556         /* maximum request size to be accepted for this service */
2557         unsigned int                    bc_req_max_size;
2558         /* maximum reply size this service can ever send */
2559         unsigned int                    bc_rep_max_size;
2560 };
2561
2562 struct ptlrpc_service_thr_conf {
2563         /* threadname should be 8 characters or less - 6 will be added on */
2564         char                            *tc_thr_name;
2565         /* threads increasing factor for each CPU */
2566         unsigned int                    tc_thr_factor;
2567         /* service threads # to start on each partition while initializing */
2568         unsigned int                    tc_nthrs_init;
2569         /*
2570          * low water of threads # upper-limit on each partition while running,
2571          * service availability may be impacted if threads number is lower
2572          * than this value. It can be ZERO if the service doesn't require
2573          * CPU affinity or there is only one partition.
2574          */
2575         unsigned int                    tc_nthrs_base;
2576         /* "soft" limit for total threads number */
2577         unsigned int                    tc_nthrs_max;
2578         /* user specified threads number, it will be validated due to
2579          * other members of this structure. */
2580         unsigned int                    tc_nthrs_user;
2581         /* set NUMA node affinity for service threads */
2582         unsigned int                    tc_cpu_affinity;
2583         /* Tags for lu_context associated with service thread */
2584         __u32                           tc_ctx_tags;
2585 };
2586
2587 struct ptlrpc_service_cpt_conf {
2588         struct cfs_cpt_table            *cc_cptable;
2589         /* string pattern to describe CPTs for a service */
2590         char                            *cc_pattern;
2591 };
2592
2593 struct ptlrpc_service_conf {
2594         /* service name */
2595         char                            *psc_name;
2596         /* soft watchdog timeout multiplifier to print stuck service traces */
2597         unsigned int                    psc_watchdog_factor;
2598         /* buffer information */
2599         struct ptlrpc_service_buf_conf  psc_buf;
2600         /* thread information */
2601         struct ptlrpc_service_thr_conf  psc_thr;
2602         /* CPU partition information */
2603         struct ptlrpc_service_cpt_conf  psc_cpt;
2604         /* function table */
2605         struct ptlrpc_service_ops       psc_ops;
2606 };
2607
2608 /* ptlrpc/service.c */
2609 /**
2610  * Server-side services API. Register/unregister service, request state
2611  * management, service thread management
2612  *
2613  * @{
2614  */
2615 void ptlrpc_save_lock(struct ptlrpc_request *req,
2616                       struct lustre_handle *lock, int mode, int no_ack);
2617 void ptlrpc_commit_replies(struct obd_export *exp);
2618 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs);
2619 void ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs);
2620 int ptlrpc_hpreq_handler(struct ptlrpc_request *req);
2621 struct ptlrpc_service *ptlrpc_register_service(
2622                                 struct ptlrpc_service_conf *conf,
2623                                 struct proc_dir_entry *proc_entry);
2624 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
2625
2626 int ptlrpc_start_threads(struct ptlrpc_service *svc);
2627 int ptlrpc_unregister_service(struct ptlrpc_service *service);
2628 int liblustre_check_services(void *arg);
2629 void ptlrpc_daemonize(char *name);
2630 int ptlrpc_service_health_check(struct ptlrpc_service *);
2631 void ptlrpc_server_drop_request(struct ptlrpc_request *req);
2632
2633 #ifdef __KERNEL__
2634 int ptlrpc_hr_init(void);
2635 void ptlrpc_hr_fini(void);
2636 #else
2637 # define ptlrpc_hr_init() (0)
2638 # define ptlrpc_hr_fini() do {} while(0)
2639 #endif
2640
2641 /** @} */
2642
2643 /* ptlrpc/import.c */
2644 /**
2645  * Import API
2646  * @{
2647  */
2648 int ptlrpc_connect_import(struct obd_import *imp);
2649 int ptlrpc_init_import(struct obd_import *imp);
2650 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
2651 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
2652 void deuuidify(char *uuid, const char *prefix, char **uuid_start,
2653                int *uuid_len);
2654
2655 /* ptlrpc/pack_generic.c */
2656 int ptlrpc_reconnect_import(struct obd_import *imp);
2657 /** @} */
2658
2659 /**
2660  * ptlrpc msg buffer and swab interface 
2661  *
2662  * @{
2663  */
2664 int ptlrpc_buf_need_swab(struct ptlrpc_request *req, const int inout,
2665                          int index);
2666 void ptlrpc_buf_set_swabbed(struct ptlrpc_request *req, const int inout,
2667                                 int index);
2668 int ptlrpc_unpack_rep_msg(struct ptlrpc_request *req, int len);
2669 int ptlrpc_unpack_req_msg(struct ptlrpc_request *req, int len);
2670
2671 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
2672 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
2673                         char **bufs);
2674 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
2675                         __u32 *lens, char **bufs);
2676 int lustre_pack_reply(struct ptlrpc_request *, int count, __u32 *lens,
2677                       char **bufs);
2678 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
2679                          __u32 *lens, char **bufs, int flags);
2680 #define LPRFL_EARLY_REPLY 1
2681 int lustre_pack_reply_flags(struct ptlrpc_request *, int count, __u32 *lens,
2682                             char **bufs, int flags);
2683 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
2684                       unsigned int newlen, int move_data);
2685 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
2686 int __lustre_unpack_msg(struct lustre_msg *m, int len);
2687 int lustre_msg_hdr_size(__u32 magic, int count);
2688 int lustre_msg_size(__u32 magic, int count, __u32 *lengths);
2689 int lustre_msg_size_v2(int count, __u32 *lengths);
2690 int lustre_packed_msg_size(struct lustre_msg *msg);
2691 int lustre_msg_early_size(void);
2692 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size);
2693 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
2694 int lustre_msg_buflen(struct lustre_msg *m, int n);
2695 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
2696 int lustre_msg_bufcount(struct lustre_msg *m);
2697 char *lustre_msg_string(struct lustre_msg *m, int n, int max_len);
2698 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg);
2699 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags);
2700 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
2701 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
2702 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
2703 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
2704 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
2705 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
2706 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
2707 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
2708 __u32 lustre_msg_get_type(struct lustre_msg *msg);
2709 __u32 lustre_msg_get_version(struct lustre_msg *msg);
2710 void lustre_msg_add_version(struct lustre_msg *msg, int version);
2711 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
2712 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
2713 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
2714 __u64 *lustre_msg_get_versions(struct lustre_msg *msg);
2715 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
2716 __u64 lustre_msg_get_slv(struct lustre_msg *msg);
2717 __u32 lustre_msg_get_limit(struct lustre_msg *msg);
2718 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv);
2719 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit);
2720 int lustre_msg_get_status(struct lustre_msg *msg);
2721 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
2722 int lustre_msg_is_v1(struct lustre_msg *msg);
2723 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
2724 __u32 lustre_msg_get_timeout(struct lustre_msg *msg);
2725 __u32 lustre_msg_get_service_time(struct lustre_msg *msg);
2726 char *lustre_msg_get_jobid(struct lustre_msg *msg);
2727 __u32 lustre_msg_get_cksum(struct lustre_msg *msg);
2728 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
2729 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg, int compat18);
2730 #else
2731 # warning "remove checksum compatibility support for b1_8"
2732 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg);
2733 #endif
2734 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
2735 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
2736 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
2737 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
2738 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
2739 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions);
2740 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
2741 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
2742 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
2743 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *sizes);
2744 void ptlrpc_request_set_replen(struct ptlrpc_request *req);
2745 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout);
2746 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time);
2747 void lustre_msg_set_jobid(struct lustre_msg *msg, char *jobid);
2748 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum);
2749
2750 static inline void
2751 lustre_shrink_reply(struct ptlrpc_request *req, int segment,
2752                     unsigned int newlen, int move_data)
2753 {
2754         LASSERT(req->rq_reply_state);
2755         LASSERT(req->rq_repmsg);
2756         req->rq_replen = lustre_shrink_msg(req->rq_repmsg, segment,
2757                                            newlen, move_data);
2758 }
2759 /** @} */
2760
2761 /** Change request phase of \a req to \a new_phase */
2762 static inline void
2763 ptlrpc_rqphase_move(struct ptlrpc_request *req, enum rq_phase new_phase)
2764 {
2765         if (req->rq_phase == new_phase)
2766                 return;
2767
2768         if (new_phase == RQ_PHASE_UNREGISTERING) {
2769                 req->rq_next_phase = req->rq_phase;
2770                 if (req->rq_import)
2771                         cfs_atomic_inc(&req->rq_import->imp_unregistering);
2772         }
2773
2774         if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
2775                 if (req->rq_import)
2776                         cfs_atomic_dec(&req->rq_import->imp_unregistering);
2777         }
2778
2779         DEBUG_REQ(D_INFO, req, "move req \"%s\" -> \"%s\"",
2780                   ptlrpc_rqphase2str(req), ptlrpc_phase2str(new_phase));
2781
2782         req->rq_phase = new_phase;
2783 }
2784
2785 /**
2786  * Returns true if request \a req got early reply and hard deadline is not met 
2787  */
2788 static inline int
2789 ptlrpc_client_early(struct ptlrpc_request *req)
2790 {
2791         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2792             req->rq_reply_deadline > cfs_time_current_sec())
2793                 return 0;
2794         return req->rq_early;
2795 }
2796
2797 /**
2798  * Returns true if we got real reply from server for this request
2799  */
2800 static inline int
2801 ptlrpc_client_replied(struct ptlrpc_request *req)
2802 {
2803         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2804             req->rq_reply_deadline > cfs_time_current_sec())
2805                 return 0;
2806         return req->rq_replied;
2807 }
2808
2809 /** Returns true if request \a req is in process of receiving server reply */
2810 static inline int
2811 ptlrpc_client_recv(struct ptlrpc_request *req)
2812 {
2813         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2814             req->rq_reply_deadline > cfs_time_current_sec())
2815                 return 1;
2816         return req->rq_receiving_reply;
2817 }
2818
2819 static inline int
2820 ptlrpc_client_recv_or_unlink(struct ptlrpc_request *req)
2821 {
2822         int rc;
2823
2824         spin_lock(&req->rq_lock);
2825         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2826             req->rq_reply_deadline > cfs_time_current_sec()) {
2827                 spin_unlock(&req->rq_lock);
2828                 return 1;
2829         }
2830         rc = req->rq_receiving_reply || req->rq_must_unlink;
2831         spin_unlock(&req->rq_lock);
2832         return rc;
2833 }
2834
2835 static inline void
2836 ptlrpc_client_wake_req(struct ptlrpc_request *req)
2837 {
2838         if (req->rq_set == NULL)
2839                 cfs_waitq_signal(&req->rq_reply_waitq);
2840         else
2841                 cfs_waitq_signal(&req->rq_set->set_waitq);
2842 }
2843
2844 static inline void
2845 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
2846 {
2847         LASSERT(cfs_atomic_read(&rs->rs_refcount) > 0);
2848         cfs_atomic_inc(&rs->rs_refcount);
2849 }
2850
2851 static inline void
2852 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
2853 {
2854         LASSERT(cfs_atomic_read(&rs->rs_refcount) > 0);
2855         if (cfs_atomic_dec_and_test(&rs->rs_refcount))
2856                 lustre_free_reply_state(rs);
2857 }
2858
2859 /* Should only be called once per req */
2860 static inline void ptlrpc_req_drop_rs(struct ptlrpc_request *req)
2861 {
2862         if (req->rq_reply_state == NULL)
2863                 return; /* shouldn't occur */
2864         ptlrpc_rs_decref(req->rq_reply_state);
2865         req->rq_reply_state = NULL;
2866         req->rq_repmsg = NULL;
2867 }
2868
2869 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
2870 {
2871         return lustre_msg_get_magic(req->rq_reqmsg);
2872 }
2873
2874 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
2875 {
2876         switch (req->rq_reqmsg->lm_magic) {
2877         case LUSTRE_MSG_MAGIC_V2:
2878                 return req->rq_reqmsg->lm_repsize;
2879         default:
2880                 LASSERTF(0, "incorrect message magic: %08x\n",
2881                          req->rq_reqmsg->lm_magic);
2882                 return -EFAULT;
2883         }
2884 }
2885
2886 static inline int ptlrpc_send_limit_expired(struct ptlrpc_request *req)
2887 {
2888         if (req->rq_delay_limit != 0 &&
2889             cfs_time_before(cfs_time_add(req->rq_queued_time,
2890                                          cfs_time_seconds(req->rq_delay_limit)),
2891                             cfs_time_current())) {
2892                 return 1;
2893         }
2894         return 0;
2895 }
2896
2897 static inline int ptlrpc_no_resend(struct ptlrpc_request *req)
2898 {
2899         if (!req->rq_no_resend && ptlrpc_send_limit_expired(req)) {
2900                 spin_lock(&req->rq_lock);
2901                 req->rq_no_resend = 1;
2902                 spin_unlock(&req->rq_lock);
2903         }
2904         return req->rq_no_resend;
2905 }
2906
2907 static inline int
2908 ptlrpc_server_get_timeout(struct ptlrpc_service_part *svcpt)
2909 {
2910         int at = AT_OFF ? 0 : at_get(&svcpt->scp_at_estimate);
2911
2912         return svcpt->scp_service->srv_watchdog_factor *
2913                max_t(int, at, obd_timeout);
2914 }
2915
2916 static inline struct ptlrpc_service *
2917 ptlrpc_req2svc(struct ptlrpc_request *req)
2918 {
2919         LASSERT(req->rq_rqbd != NULL);
2920         return req->rq_rqbd->rqbd_svcpt->scp_service;
2921 }
2922
2923 /* ldlm/ldlm_lib.c */
2924 /**
2925  * Target client logic
2926  * @{
2927  */
2928 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg);
2929 int client_obd_cleanup(struct obd_device *obddev);
2930 int client_connect_import(const struct lu_env *env,
2931                           struct obd_export **exp, struct obd_device *obd,
2932                           struct obd_uuid *cluuid, struct obd_connect_data *,
2933                           void *localdata);
2934 int client_disconnect_export(struct obd_export *exp);
2935 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
2936                            int priority);
2937 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
2938 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
2939                             struct obd_uuid *uuid);
2940 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
2941 void client_destroy_import(struct obd_import *imp);
2942 /** @} */
2943
2944 #ifdef HAVE_SERVER_SUPPORT
2945 int server_disconnect_export(struct obd_export *exp);
2946 #endif
2947
2948 /* ptlrpc/pinger.c */
2949 /**
2950  * Pinger API (client side only)
2951  * @{
2952  */
2953 enum timeout_event {
2954         TIMEOUT_GRANT = 1
2955 };
2956 struct timeout_item;
2957 typedef int (*timeout_cb_t)(struct timeout_item *, void *);
2958 int ptlrpc_pinger_add_import(struct obd_import *imp);
2959 int ptlrpc_pinger_del_import(struct obd_import *imp);
2960 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
2961                               timeout_cb_t cb, void *data,
2962                               cfs_list_t *obd_list);
2963 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
2964                               enum timeout_event event);
2965 struct ptlrpc_request * ptlrpc_prep_ping(struct obd_import *imp);
2966 int ptlrpc_obd_ping(struct obd_device *obd);
2967 cfs_time_t ptlrpc_suspend_wakeup_time(void);
2968 #ifdef __KERNEL__
2969 void ping_evictor_start(void);
2970 void ping_evictor_stop(void);
2971 #else
2972 #define ping_evictor_start()    do {} while (0)
2973 #define ping_evictor_stop()     do {} while (0)
2974 #endif
2975 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req);
2976 /** @} */
2977
2978 /* ptlrpc daemon bind policy */
2979 typedef enum {
2980         /* all ptlrpcd threads are free mode */
2981         PDB_POLICY_NONE          = 1,
2982         /* all ptlrpcd threads are bound mode */
2983         PDB_POLICY_FULL          = 2,
2984         /* <free1 bound1> <free2 bound2> ... <freeN boundN> */
2985         PDB_POLICY_PAIR          = 3,
2986         /* <free1 bound1> <bound1 free2> ... <freeN boundN> <boundN free1>,
2987          * means each ptlrpcd[X] has two partners: thread[X-1] and thread[X+1].
2988          * If kernel supports NUMA, pthrpcd threads are binded and
2989          * grouped by NUMA node */
2990         PDB_POLICY_NEIGHBOR      = 4,
2991 } pdb_policy_t;
2992
2993 /* ptlrpc daemon load policy
2994  * It is caller's duty to specify how to push the async RPC into some ptlrpcd
2995  * queue, but it is not enforced, affected by "ptlrpcd_bind_policy". If it is
2996  * "PDB_POLICY_FULL", then the RPC will be processed by the selected ptlrpcd,
2997  * Otherwise, the RPC may be processed by the selected ptlrpcd or its partner,
2998  * depends on which is scheduled firstly, to accelerate the RPC processing. */
2999 typedef enum {
3000         /* on the same CPU core as the caller */
3001         PDL_POLICY_SAME         = 1,
3002         /* within the same CPU partition, but not the same core as the caller */
3003         PDL_POLICY_LOCAL        = 2,
3004         /* round-robin on all CPU cores, but not the same core as the caller */
3005         PDL_POLICY_ROUND        = 3,
3006         /* the specified CPU core is preferred, but not enforced */
3007         PDL_POLICY_PREFERRED    = 4,
3008 } pdl_policy_t;
3009
3010 /* ptlrpc/ptlrpcd.c */
3011 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force);
3012 void ptlrpcd_wake(struct ptlrpc_request *req);
3013 void ptlrpcd_add_req(struct ptlrpc_request *req, pdl_policy_t policy, int idx);
3014 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set);
3015 int ptlrpcd_addref(void);
3016 void ptlrpcd_decref(void);
3017
3018 /* ptlrpc/lproc_ptlrpc.c */
3019 /**
3020  * procfs output related functions
3021  * @{
3022  */
3023 const char* ll_opcode2str(__u32 opcode);
3024 #ifdef LPROCFS
3025 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
3026 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
3027 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes);
3028 #else
3029 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
3030 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
3031 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes) {}
3032 #endif
3033 /** @} */
3034
3035 /* ptlrpc/llog_server.c */
3036 int llog_origin_handle_open(struct ptlrpc_request *req);
3037 int llog_origin_handle_destroy(struct ptlrpc_request *req);
3038 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
3039 int llog_origin_handle_next_block(struct ptlrpc_request *req);
3040 int llog_origin_handle_read_header(struct ptlrpc_request *req);
3041 int llog_origin_handle_close(struct ptlrpc_request *req);
3042 int llog_origin_handle_cancel(struct ptlrpc_request *req);
3043
3044 /* ptlrpc/llog_client.c */
3045 extern struct llog_operations llog_client_ops;
3046
3047 /** @} net */
3048
3049 #endif
3050 /** @} PtlRPC */