Whamcloud - gitweb
4d8341d1d32a912108e8e9ecd6150010c8e68c55
[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) 2011, 2012, Whamcloud, Inc.
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 /**
110  * The following constants determine how memory is used to buffer incoming
111  * service requests.
112  *
113  * ?_NBUFS              # buffers to allocate when growing the pool
114  * ?_BUFSIZE            # bytes in a single request buffer
115  * ?_MAXREQSIZE         # maximum request service will receive
116  *
117  * When fewer than ?_NBUFS/2 buffers are posted for receive, another chunk
118  * of ?_NBUFS is added to the pool.
119  *
120  * Messages larger than ?_MAXREQSIZE are dropped.  Request buffers are
121  * considered full when less than ?_MAXREQSIZE is left in them.
122  */
123 #define LDLM_THREADS_AUTO_MIN (2)
124 #define LDLM_THREADS_AUTO_MAX min_t(unsigned, cfs_num_online_cpus() * \
125                                   cfs_num_online_cpus() * 32, 128)
126 #define LDLM_BL_THREADS  LDLM_THREADS_AUTO_MIN
127 #define LDLM_NBUFS      (64 * cfs_num_online_cpus())
128 #define LDLM_BUFSIZE    (8 * 1024)
129 #define LDLM_MAXREQSIZE (5 * 1024)
130 #define LDLM_MAXREPSIZE (1024)
131
132 /** Absolute limits */
133 #define MDT_MIN_THREADS 2UL
134 #ifndef MDT_MAX_THREADS
135 #define MDT_MAX_THREADS 512UL
136 #endif
137 #define MDS_NBUFS       (64 * cfs_num_online_cpus())
138 /**
139  * Assume file name length = FNAME_MAX = 256 (true for ext3).
140  *        path name length = PATH_MAX = 4096
141  *        LOV MD size max  = EA_MAX = 48000 (2000 stripes)
142  * symlink:  FNAME_MAX + PATH_MAX  <- largest
143  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
144  * rename:   FNAME_MAX + FNAME_MAX
145  * open:     FNAME_MAX + EA_MAX
146  *
147  * MDS_MAXREQSIZE ~= 4736 bytes =
148  * lustre_msg + ldlm_request + mdt_body + mds_rec_create + FNAME_MAX + PATH_MAX
149  * MDS_MAXREPSIZE ~= 8300 bytes = lustre_msg + llog_header
150  * or, for mds_close() and mds_reint_unlink() on a many-OST filesystem:
151  *      = 9210 bytes = lustre_msg + mdt_body + 160 * (easize + cookiesize)
152  *
153  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
154  * except in the open case where there are a large number of OSTs in a LOV.
155  */
156 #define MDS_MAXREPSIZE  max(10 * 1024, 362 + LOV_MAX_STRIPE_COUNT * 56)
157 #define MDS_MAXREQSIZE  MDS_MAXREPSIZE
158
159 /** MDS_BUFSIZE = max_reqsize + max sptlrpc payload size */
160 #define MDS_BUFSIZE     (MDS_MAXREQSIZE + 1024)
161
162 /** FLD_MAXREQSIZE == lustre_msg + __u32 padding + ptlrpc_body + opc */
163 #define FLD_MAXREQSIZE  (160)
164
165 /** FLD_MAXREPSIZE == lustre_msg + ptlrpc_body */
166 #define FLD_MAXREPSIZE  (152)
167
168 /**
169  * SEQ_MAXREQSIZE == lustre_msg + __u32 padding + ptlrpc_body + opc + lu_range +
170  * __u32 padding */
171 #define SEQ_MAXREQSIZE  (160)
172
173 /** SEQ_MAXREPSIZE == lustre_msg + ptlrpc_body + lu_range */
174 #define SEQ_MAXREPSIZE  (152)
175
176 /** MGS threads must be >= 3, see bug 22458 comment #28 */
177 #define MGS_THREADS_AUTO_MIN 3
178 #define MGS_THREADS_AUTO_MAX 32
179 #define MGS_NBUFS       (64 * cfs_num_online_cpus())
180 #define MGS_BUFSIZE     (8 * 1024)
181 #define MGS_MAXREQSIZE  (7 * 1024)
182 #define MGS_MAXREPSIZE  (9 * 1024)
183
184 /** Absolute OSS limits */
185 #define OSS_THREADS_MIN 3       /* difficult replies, HPQ, others */
186 #define OSS_THREADS_MAX 512
187 #define OST_NBUFS       (64 * cfs_num_online_cpus())
188 #define OST_BUFSIZE     (8 * 1024)
189
190 /**
191  * OST_MAXREQSIZE ~= 4768 bytes =
192  * lustre_msg + obdo + 16 * obd_ioobj + 256 * niobuf_remote
193  *
194  * - single object with 16 pages is 512 bytes
195  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
196  */
197 #define OST_MAXREQSIZE  (5 * 1024)
198 #define OST_MAXREPSIZE  (9 * 1024)
199
200 /* Macro to hide a typecast. */
201 #define ptlrpc_req_async_args(req) ((void *)&req->rq_async_args)
202
203 /**
204  * Structure to single define portal connection.
205  */
206 struct ptlrpc_connection {
207         /** linkage for connections hash table */
208         cfs_hlist_node_t        c_hash;
209         /** Our own lnet nid for this connection */
210         lnet_nid_t              c_self;
211         /** Remote side nid for this connection */
212         lnet_process_id_t       c_peer;
213         /** UUID of the other side */
214         struct obd_uuid         c_remote_uuid;
215         /** reference counter for this connection */
216         cfs_atomic_t            c_refcount;
217 };
218
219 /** Client definition for PortalRPC */
220 struct ptlrpc_client {
221         /** What lnet portal does this client send messages to by default */
222         __u32                   cli_request_portal;
223         /** What portal do we expect replies on */
224         __u32                   cli_reply_portal;
225         /** Name of the client */
226         char                   *cli_name;
227 };
228
229 /** state flags of requests */
230 /* XXX only ones left are those used by the bulk descs as well! */
231 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
232 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
233
234 #define REQ_MAX_ACK_LOCKS 8
235
236 union ptlrpc_async_args {
237         /**
238          * Scratchpad for passing args to completion interpreter. Users
239          * cast to the struct of their choosing, and CLASSERT that this is
240          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
241          * a pointer to it here.  The pointer_arg ensures this struct is at
242          * least big enough for that.
243          */
244         void      *pointer_arg[11];
245         __u64      space[6];
246 };
247
248 struct ptlrpc_request_set;
249 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
250 typedef int (*set_producer_func)(struct ptlrpc_request_set *, void *);
251
252 /**
253  * Definition of request set structure.
254  * Request set is a list of requests (not necessary to the same target) that
255  * once populated with RPCs could be sent in parallel.
256  * There are two kinds of request sets. General purpose and with dedicated
257  * serving thread. Example of the latter is ptlrpcd set.
258  * For general purpose sets once request set started sending it is impossible
259  * to add new requests to such set.
260  * Provides a way to call "completion callbacks" when all requests in the set
261  * returned.
262  */
263 struct ptlrpc_request_set {
264         cfs_atomic_t          set_refcount;
265         /** number of in queue requests */
266         cfs_atomic_t          set_new_count;
267         /** number of uncompleted requests */
268         cfs_atomic_t          set_remaining;
269         /** wait queue to wait on for request events */
270         cfs_waitq_t           set_waitq;
271         cfs_waitq_t          *set_wakeup_ptr;
272         /** List of requests in the set */
273         cfs_list_t            set_requests;
274         /**
275          * List of completion callbacks to be called when the set is completed
276          * This is only used if \a set_interpret is NULL.
277          * Links struct ptlrpc_set_cbdata.
278          */
279         cfs_list_t            set_cblist;
280         /** Completion callback, if only one. */
281         set_interpreter_func  set_interpret;
282         /** opaq argument passed to completion \a set_interpret callback. */
283         void                 *set_arg;
284         /** rq_status of requests that have been freed already */
285         int                   set_rc;
286         /**
287          * Lock for \a set_new_requests manipulations
288          * locked so that any old caller can communicate requests to
289          * the set holder who can then fold them into the lock-free set
290          */
291         cfs_spinlock_t        set_new_req_lock;
292         /** List of new yet unsent requests. Only used with ptlrpcd now. */
293         cfs_list_t            set_new_requests;
294
295         /** Additional fields used by the flow control extension */
296         /** Maximum number of RPCs in flight */
297         int                   set_max_inflight;
298         /** Callback function used to generate RPCs */
299         set_producer_func     set_producer;
300         /** opaq argument passed to the producer callback */
301         void                 *set_producer_arg;
302 };
303
304 /**
305  * Description of a single ptrlrpc_set callback
306  */
307 struct ptlrpc_set_cbdata {
308         /** List linkage item */
309         cfs_list_t              psc_item;
310         /** Pointer to interpreting function */
311         set_interpreter_func    psc_interpret;
312         /** Opaq argument to pass to the callback */
313         void                   *psc_data;
314 };
315
316 struct ptlrpc_bulk_desc;
317
318 /**
319  * ptlrpc callback & work item stuff
320  */
321 struct ptlrpc_cb_id {
322         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
323         void    *cbid_arg;                      /* additional arg */
324 };
325
326 /** Maximum number of locks to fit into reply state */
327 #define RS_MAX_LOCKS 8
328 #define RS_DEBUG     0
329
330 /**
331  * Structure to define reply state on the server
332  * Reply state holds various reply message information. Also for "difficult"
333  * replies (rep-ack case) we store the state after sending reply and wait
334  * for the client to acknowledge the reception. In these cases locks could be
335  * added to the state for replay/failover consistency guarantees.
336  */
337 struct ptlrpc_reply_state {
338         /** Callback description */
339         struct ptlrpc_cb_id    rs_cb_id;
340         /** Linkage for list of all reply states in a system */
341         cfs_list_t             rs_list;
342         /** Linkage for list of all reply states on same export */
343         cfs_list_t             rs_exp_list;
344         /** Linkage for list of all reply states for same obd */
345         cfs_list_t             rs_obd_list;
346 #if RS_DEBUG
347         cfs_list_t             rs_debug_list;
348 #endif
349         /** A spinlock to protect the reply state flags */
350         cfs_spinlock_t         rs_lock;
351         /** Reply state flags */
352         unsigned long          rs_difficult:1;     /* ACK/commit stuff */
353         unsigned long          rs_no_ack:1;    /* no ACK, even for
354                                                   difficult requests */
355         unsigned long          rs_scheduled:1;     /* being handled? */
356         unsigned long          rs_scheduled_ever:1;/* any schedule attempts? */
357         unsigned long          rs_handled:1;  /* been handled yet? */
358         unsigned long          rs_on_net:1;   /* reply_out_callback pending? */
359         unsigned long          rs_prealloc:1; /* rs from prealloc list */
360         unsigned long          rs_committed:1;/* the transaction was committed
361                                                  and the rs was dispatched
362                                                  by ptlrpc_commit_replies */
363         /** Size of the state */
364         int                    rs_size;
365         /** opcode */
366         __u32                  rs_opc;
367         /** Transaction number */
368         __u64                  rs_transno;
369         /** xid */
370         __u64                  rs_xid;
371         struct obd_export     *rs_export;
372         struct ptlrpc_service *rs_service;
373         /** Lnet metadata handle for the reply */
374         lnet_handle_md_t       rs_md_h;
375         cfs_atomic_t           rs_refcount;
376
377         /** Context for the sevice thread */
378         struct ptlrpc_svc_ctx *rs_svc_ctx;
379         /** Reply buffer (actually sent to the client), encoded if needed */
380         struct lustre_msg     *rs_repbuf;       /* wrapper */
381         /** Size of the reply buffer */
382         int                    rs_repbuf_len;   /* wrapper buf length */
383         /** Size of the reply message */
384         int                    rs_repdata_len;  /* wrapper msg length */
385         /**
386          * Actual reply message. Its content is encrupted (if needed) to
387          * produce reply buffer for actual sending. In simple case
388          * of no network encryption we jus set \a rs_repbuf to \a rs_msg
389          */
390         struct lustre_msg     *rs_msg;          /* reply message */
391
392         /** Number of locks awaiting client ACK */
393         int                    rs_nlocks;
394         /** Handles of locks awaiting client reply ACK */
395         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
396         /** Lock modes of locks in \a rs_locks */
397         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
398 };
399
400 struct ptlrpc_thread;
401
402 /** RPC stages */
403 enum rq_phase {
404         RQ_PHASE_NEW            = 0xebc0de00,
405         RQ_PHASE_RPC            = 0xebc0de01,
406         RQ_PHASE_BULK           = 0xebc0de02,
407         RQ_PHASE_INTERPRET      = 0xebc0de03,
408         RQ_PHASE_COMPLETE       = 0xebc0de04,
409         RQ_PHASE_UNREGISTERING  = 0xebc0de05,
410         RQ_PHASE_UNDEFINED      = 0xebc0de06
411 };
412
413 /** Type of request interpreter call-back */
414 typedef int (*ptlrpc_interpterer_t)(const struct lu_env *env,
415                                     struct ptlrpc_request *req,
416                                     void *arg, int rc);
417
418 /**
419  * Definition of request pool structure.
420  * The pool is used to store empty preallocated requests for the case
421  * when we would actually need to send something without performing
422  * any allocations (to avoid e.g. OOM).
423  */
424 struct ptlrpc_request_pool {
425         /** Locks the list */
426         cfs_spinlock_t prp_lock;
427         /** list of ptlrpc_request structs */
428         cfs_list_t prp_req_list;
429         /** Maximum message size that would fit into a rquest from this pool */
430         int prp_rq_size;
431         /** Function to allocate more requests for this pool */
432         void (*prp_populate)(struct ptlrpc_request_pool *, int);
433 };
434
435 struct lu_context;
436 struct lu_env;
437
438 struct ldlm_lock;
439
440 /**
441  * Basic request prioritization operations structure.
442  * The whole idea is centered around locks and RPCs that might affect locks.
443  * When a lock is contended we try to give priority to RPCs that might lead
444  * to fastest release of that lock.
445  * Currently only implemented for OSTs only in a way that makes all
446  * IO and truncate RPCs that are coming from a locked region where a lock is
447  * contended a priority over other requests.
448  */
449 struct ptlrpc_hpreq_ops {
450         /**
451          * Check if the lock handle of the given lock is the same as
452          * taken from the request.
453          */
454         int  (*hpreq_lock_match)(struct ptlrpc_request *, struct ldlm_lock *);
455         /**
456          * Check if the request is a high priority one.
457          */
458         int  (*hpreq_check)(struct ptlrpc_request *);
459         /**
460          * Called after the request has been handled.
461          */
462         void (*hpreq_fini)(struct ptlrpc_request *);
463 };
464
465 /**
466  * Represents remote procedure call.
467  *
468  * This is a staple structure used by everybody wanting to send a request
469  * in Lustre.
470  */
471 struct ptlrpc_request {
472         /* Request type: one of PTL_RPC_MSG_* */
473         int rq_type;
474         /**
475          * Linkage item through which this request is included into
476          * sending/delayed lists on client and into rqbd list on server
477          */
478         cfs_list_t rq_list;
479         /**
480          * Server side list of incoming unserved requests sorted by arrival
481          * time.  Traversed from time to time to notice about to expire
482          * requests and sent back "early replies" to clients to let them
483          * know server is alive and well, just very busy to service their
484          * requests in time
485          */
486         cfs_list_t rq_timed_list;
487         /** server-side history, used for debuging purposes. */
488         cfs_list_t rq_history_list;
489         /** server-side per-export list */
490         cfs_list_t rq_exp_list;
491         /** server-side hp handlers */
492         struct ptlrpc_hpreq_ops *rq_ops;
493         /** history sequence # */
494         __u64 rq_history_seq;
495         /** the index of service's srv_at_array into which request is linked */
496         time_t rq_at_index;
497         /** Result of request processing */
498         int rq_status;
499         /** Lock to protect request flags and some other important bits, like
500          * rq_list
501          */
502         cfs_spinlock_t rq_lock;
503         /** client-side flags are serialized by rq_lock */
504         unsigned long rq_intr:1, rq_replied:1, rq_err:1,
505                 rq_timedout:1, rq_resend:1, rq_restart:1,
506                 /**
507                  * when ->rq_replay is set, request is kept by the client even
508                  * after server commits corresponding transaction. This is
509                  * used for operations that require sequence of multiple
510                  * requests to be replayed. The only example currently is file
511                  * open/close. When last request in such a sequence is
512                  * committed, ->rq_replay is cleared on all requests in the
513                  * sequence.
514                  */
515                 rq_replay:1,
516                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
517                 rq_no_delay:1, rq_net_err:1, rq_wait_ctx:1,
518                 rq_early:1, rq_must_unlink:1,
519                 rq_fake:1,          /* this fake req */
520                 rq_memalloc:1,      /* req originated from "kswapd" */
521                 /* server-side flags */
522                 rq_packed_final:1,  /* packed final reply */
523                 rq_hp:1,            /* high priority RPC */
524                 rq_at_linked:1,     /* link into service's srv_at_array */
525                 rq_reply_truncate:1,
526                 rq_committed:1,
527                 /* whether the "rq_set" is a valid one */
528                 rq_invalid_rqset:1,
529                 rq_generation_set:1;
530
531         enum rq_phase rq_phase; /* one of RQ_PHASE_* */
532         enum rq_phase rq_next_phase; /* one of RQ_PHASE_* to be used next */
533         cfs_atomic_t rq_refcount;/* client-side refcount for SENT race,
534                                     server-side refcounf for multiple replies */
535
536         /** initial thread servicing this request */
537         struct ptlrpc_thread *rq_svc_thread;
538
539         /** Portal to which this request would be sent */
540         int rq_request_portal;  /* XXX FIXME bug 249 */
541         /** Portal where to wait for reply and where reply would be sent */
542         int rq_reply_portal;    /* XXX FIXME bug 249 */
543
544         /**
545          * client-side:
546          * !rq_truncate : # reply bytes actually received,
547          *  rq_truncate : required repbuf_len for resend
548          */
549         int rq_nob_received;
550         /** Request length */
551         int rq_reqlen;
552          /** Request message - what client sent */
553         struct lustre_msg *rq_reqmsg;
554
555         /** Reply length */
556         int rq_replen;
557         /** Reply message - server response */
558         struct lustre_msg *rq_repmsg;
559         /** Transaction number */
560         __u64 rq_transno;
561         /** xid */
562         __u64 rq_xid;
563         /**
564          * List item to for replay list. Not yet commited requests get linked
565          * there.
566          * Also see \a rq_replay comment above.
567          */
568         cfs_list_t rq_replay_list;
569
570         /**
571          * security and encryption data
572          * @{ */
573         struct ptlrpc_cli_ctx   *rq_cli_ctx;     /**< client's half ctx */
574         struct ptlrpc_svc_ctx   *rq_svc_ctx;     /**< server's half ctx */
575         cfs_list_t               rq_ctx_chain;   /**< link to waited ctx */
576
577         struct sptlrpc_flavor    rq_flvr;        /**< for client & server */
578         enum lustre_sec_part     rq_sp_from;
579
580         unsigned long            /* client/server security flags */
581                                  rq_ctx_init:1,      /* context initiation */
582                                  rq_ctx_fini:1,      /* context destroy */
583                                  rq_bulk_read:1,     /* request bulk read */
584                                  rq_bulk_write:1,    /* request bulk write */
585                                  /* server authentication flags */
586                                  rq_auth_gss:1,      /* authenticated by gss */
587                                  rq_auth_remote:1,   /* authed as remote user */
588                                  rq_auth_usr_root:1, /* authed as root */
589                                  rq_auth_usr_mdt:1,  /* authed as mdt */
590                                  rq_auth_usr_ost:1,  /* authed as ost */
591                                  /* security tfm flags */
592                                  rq_pack_udesc:1,
593                                  rq_pack_bulk:1,
594                                  /* doesn't expect reply FIXME */
595                                  rq_no_reply:1,
596                                  rq_pill_init:1;     /* pill initialized */
597
598         uid_t                    rq_auth_uid;        /* authed uid */
599         uid_t                    rq_auth_mapped_uid; /* authed uid mapped to */
600
601         /* (server side), pointed directly into req buffer */
602         struct ptlrpc_user_desc *rq_user_desc;
603
604         /** early replies go to offset 0, regular replies go after that */
605         unsigned int             rq_reply_off;
606
607         /* various buffer pointers */
608         struct lustre_msg       *rq_reqbuf;      /* req wrapper */
609         int                      rq_reqbuf_len;  /* req wrapper buf len */
610         int                      rq_reqdata_len; /* req wrapper msg len */
611         char                    *rq_repbuf;      /* rep buffer */
612         int                      rq_repbuf_len;  /* rep buffer len */
613         struct lustre_msg       *rq_repdata;     /* rep wrapper msg */
614         int                      rq_repdata_len; /* rep wrapper msg len */
615         struct lustre_msg       *rq_clrbuf;      /* only in priv mode */
616         int                      rq_clrbuf_len;  /* only in priv mode */
617         int                      rq_clrdata_len; /* only in priv mode */
618
619         /** @} */
620
621         /** Fields that help to see if request and reply were swabbed or not */
622         __u32 rq_req_swab_mask;
623         __u32 rq_rep_swab_mask;
624
625         /** What was import generation when this request was sent */
626         int rq_import_generation;
627         enum lustre_imp_state rq_send_state;
628
629         /** how many early replies (for stats) */
630         int rq_early_count;
631
632         /** client+server request */
633         lnet_handle_md_t     rq_req_md_h;
634         struct ptlrpc_cb_id  rq_req_cbid;
635         /** optional time limit for send attempts */
636         cfs_duration_t       rq_delay_limit;
637         /** time request was first queued */
638         cfs_time_t           rq_queued_time;
639
640         /* server-side... */
641         /** request arrival time */
642         struct timeval       rq_arrival_time;
643         /** separated reply state */
644         struct ptlrpc_reply_state *rq_reply_state;
645         /** incoming request buffer */
646         struct ptlrpc_request_buffer_desc *rq_rqbd;
647 #ifdef CRAY_XT3
648         __u32                rq_uid;            /* peer uid, used in MDS only */
649 #endif
650
651         /** client-only incoming reply */
652         lnet_handle_md_t     rq_reply_md_h;
653         cfs_waitq_t          rq_reply_waitq;
654         struct ptlrpc_cb_id  rq_reply_cbid;
655
656         /** our LNet NID */
657         lnet_nid_t           rq_self;
658         /** Peer description (the other side) */
659         lnet_process_id_t    rq_peer;
660         /** Server-side, export on which request was received */
661         struct obd_export   *rq_export;
662         /** Client side, import where request is being sent */
663         struct obd_import   *rq_import;
664
665         /** Replay callback, called after request is replayed at recovery */
666         void (*rq_replay_cb)(struct ptlrpc_request *);
667         /**
668          * Commit callback, called when request is committed and about to be
669          * freed.
670          */
671         void (*rq_commit_cb)(struct ptlrpc_request *);
672         /** Opaq data for replay and commit callbacks. */
673         void  *rq_cb_data;
674
675         /** For bulk requests on client only: bulk descriptor */
676         struct ptlrpc_bulk_desc *rq_bulk;
677
678         /** client outgoing req */
679         /**
680          * when request/reply sent (secs), or time when request should be sent
681          */
682         time_t rq_sent;
683         /** time for request really sent out */
684         time_t rq_real_sent;
685
686         /** when request must finish. volatile
687          * so that servers' early reply updates to the deadline aren't
688          * kept in per-cpu cache */
689         volatile time_t rq_deadline;
690         /** when req reply unlink must finish. */
691         time_t rq_reply_deadline;
692         /** when req bulk unlink must finish. */
693         time_t rq_bulk_deadline;
694         /**
695          * service time estimate (secs) 
696          * If the requestsis not served by this time, it is marked as timed out.
697          */
698         int    rq_timeout;
699
700         /** Multi-rpc bits */
701         /** Link item for request set lists */
702         cfs_list_t  rq_set_chain;
703         /** Per-request waitq introduced by bug 21938 for recovery waiting */
704         cfs_waitq_t rq_set_waitq;
705         /** Link back to the request set */
706         struct ptlrpc_request_set *rq_set;
707         /** Async completion handler, called when reply is received */
708         ptlrpc_interpterer_t rq_interpret_reply;
709         /** Async completion context */
710         union ptlrpc_async_args rq_async_args;
711
712         /** Pool if request is from preallocated list */
713         struct ptlrpc_request_pool *rq_pool;
714
715         struct lu_context           rq_session;
716         struct lu_context           rq_recov_session;
717
718         /** request format description */
719         struct req_capsule          rq_pill;
720 };
721
722 /**
723  * Call completion handler for rpc if any, return it's status or original
724  * rc if there was no handler defined for this request.
725  */
726 static inline int ptlrpc_req_interpret(const struct lu_env *env,
727                                        struct ptlrpc_request *req, int rc)
728 {
729         if (req->rq_interpret_reply != NULL) {
730                 req->rq_status = req->rq_interpret_reply(env, req,
731                                                          &req->rq_async_args,
732                                                          rc);
733                 return req->rq_status;
734         }
735         return rc;
736 }
737
738 /**
739  * Returns 1 if request buffer at offset \a index was already swabbed
740  */
741 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
742 {
743         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
744         return req->rq_req_swab_mask & (1 << index);
745 }
746
747 /**
748  * Returns 1 if request reply buffer at offset \a index was already swabbed
749  */
750 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
751 {
752         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
753         return req->rq_rep_swab_mask & (1 << index);
754 }
755
756 /**
757  * Returns 1 if request needs to be swabbed into local cpu byteorder
758  */
759 static inline int ptlrpc_req_need_swab(struct ptlrpc_request *req)
760 {
761         return lustre_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
762 }
763
764 /**
765  * Returns 1 if request reply needs to be swabbed into local cpu byteorder
766  */
767 static inline int ptlrpc_rep_need_swab(struct ptlrpc_request *req)
768 {
769         return lustre_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
770 }
771
772 /**
773  * Mark request buffer at offset \a index that it was already swabbed
774  */
775 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
776 {
777         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
778         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
779         req->rq_req_swab_mask |= 1 << index;
780 }
781
782 /**
783  * Mark request reply buffer at offset \a index that it was already swabbed
784  */
785 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
786 {
787         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
788         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
789         req->rq_rep_swab_mask |= 1 << index;
790 }
791
792 /**
793  * Convert numerical request phase value \a phase into text string description
794  */
795 static inline const char *
796 ptlrpc_phase2str(enum rq_phase phase)
797 {
798         switch (phase) {
799         case RQ_PHASE_NEW:
800                 return "New";
801         case RQ_PHASE_RPC:
802                 return "Rpc";
803         case RQ_PHASE_BULK:
804                 return "Bulk";
805         case RQ_PHASE_INTERPRET:
806                 return "Interpret";
807         case RQ_PHASE_COMPLETE:
808                 return "Complete";
809         case RQ_PHASE_UNREGISTERING:
810                 return "Unregistering";
811         default:
812                 return "?Phase?";
813         }
814 }
815
816 /**
817  * Convert numerical request phase of the request \a req into text stringi
818  * description
819  */
820 static inline const char *
821 ptlrpc_rqphase2str(struct ptlrpc_request *req)
822 {
823         return ptlrpc_phase2str(req->rq_phase);
824 }
825
826 /**
827  * Debugging functions and helpers to print request structure into debug log
828  * @{
829  */ 
830 /* Spare the preprocessor, spoil the bugs. */
831 #define FLAG(field, str) (field ? str : "")
832
833 /** Convert bit flags into a string */
834 #define DEBUG_REQ_FLAGS(req)                                                    \
835         ptlrpc_rqphase2str(req),                                                \
836         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                    \
837         FLAG(req->rq_err, "E"),                                                 \
838         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"),   \
839         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                  \
840         FLAG(req->rq_no_resend, "N"),                                           \
841         FLAG(req->rq_waiting, "W"),                                             \
842         FLAG(req->rq_wait_ctx, "C"), FLAG(req->rq_hp, "H"),                     \
843         FLAG(req->rq_committed, "M")
844
845 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s%s%s%s"
846
847 void _debug_req(struct ptlrpc_request *req,
848                 struct libcfs_debug_msg_data *data, const char *fmt, ...)
849         __attribute__ ((format (printf, 3, 4)));
850
851 /**
852  * Helper that decides if we need to print request accordig to current debug
853  * level settings
854  */
855 #define debug_req(msgdata, mask, cdls, req, fmt, a...)                        \
856 do {                                                                          \
857         CFS_CHECK_STACK(msgdata, mask, cdls);                                 \
858                                                                               \
859         if (((mask) & D_CANTMASK) != 0 ||                                     \
860             ((libcfs_debug & (mask)) != 0 &&                                  \
861              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))                \
862                 _debug_req((req), msgdata, fmt, ##a);                         \
863 } while(0)
864
865 /**
866  * This is the debug print function you need to use to print request sturucture
867  * content into lustre debug log.
868  * for most callers (level is a constant) this is resolved at compile time */
869 #define DEBUG_REQ(level, req, fmt, args...)                                   \
870 do {                                                                          \
871         if ((level) & (D_ERROR | D_WARNING)) {                                \
872                 static cfs_debug_limit_state_t cdls;                          \
873                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, &cdls);            \
874                 debug_req(&msgdata, level, &cdls, req, "@@@ "fmt" ", ## args);\
875         } else {                                                              \
876                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, NULL);             \
877                 debug_req(&msgdata, level, NULL, req, "@@@ "fmt" ", ## args); \
878         }                                                                     \
879 } while (0)
880 /** @} */
881
882 /**
883  * Structure that defines a single page of a bulk transfer
884  */
885 struct ptlrpc_bulk_page {
886         /** Linkage to list of pages in a bulk */
887         cfs_list_t       bp_link;
888         /**
889          * Number of bytes in a page to transfer starting from \a bp_pageoffset
890          */
891         int              bp_buflen;
892         /** offset within a page */
893         int              bp_pageoffset;
894         /** The page itself */
895         struct page     *bp_page;
896 };
897
898 #define BULK_GET_SOURCE   0
899 #define BULK_PUT_SINK     1
900 #define BULK_GET_SINK     2
901 #define BULK_PUT_SOURCE   3
902
903 /**
904  * Definition of buk descriptor.
905  * Bulks are special "Two phase" RPCs where initial request message
906  * is sent first and it is followed bt a transfer (o receiving) of a large
907  * amount of data to be settled into pages referenced from the bulk descriptors.
908  * Bulks transfers (the actual data following the small requests) are done
909  * on separate LNet portals.
910  * In lustre we use bulk transfers for READ and WRITE transfers from/to OSTs.
911  *  Another user is readpage for MDT.
912  */
913 struct ptlrpc_bulk_desc {
914         /** completed successfully */
915         unsigned long bd_success:1;
916         /** accessible to the network (network io potentially in progress) */
917         unsigned long bd_network_rw:1;
918         /** {put,get}{source,sink} */
919         unsigned long bd_type:2;
920         /** client side */
921         unsigned long bd_registered:1;
922         /** For serialization with callback */
923         cfs_spinlock_t bd_lock;
924         /** Import generation when request for this bulk was sent */
925         int bd_import_generation;
926         /** Server side - export this bulk created for */
927         struct obd_export *bd_export;
928         /** Client side - import this bulk was sent on */
929         struct obd_import *bd_import;
930         /** LNet portal for this bulk */
931         __u32 bd_portal;
932         /** Back pointer to the request */
933         struct ptlrpc_request *bd_req;
934         cfs_waitq_t            bd_waitq;        /* server side only WQ */
935         int                    bd_iov_count;    /* # entries in bd_iov */
936         int                    bd_max_iov;      /* allocated size of bd_iov */
937         int                    bd_nob;          /* # bytes covered */
938         int                    bd_nob_transferred; /* # bytes GOT/PUT */
939
940         __u64                  bd_last_xid;
941
942         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
943         lnet_handle_md_t       bd_md_h;         /* associated MD */
944         lnet_nid_t             bd_sender;       /* stash event::sender */
945
946 #if defined(__KERNEL__)
947         /*
948          * encrypt iov, size is either 0 or bd_iov_count.
949          */
950         lnet_kiov_t           *bd_enc_iov;
951
952         lnet_kiov_t            bd_iov[0];
953 #else
954         lnet_md_iovec_t        bd_iov[0];
955 #endif
956 };
957
958 enum {
959         SVC_STOPPED     = 1 << 0,
960         SVC_STOPPING    = 1 << 1,
961         SVC_STARTING    = 1 << 2,
962         SVC_RUNNING     = 1 << 3,
963         SVC_EVENT       = 1 << 4,
964         SVC_SIGNAL      = 1 << 5,
965 };
966
967 /**
968  * Definition of server service thread structure
969  */
970 struct ptlrpc_thread {
971         /**
972          * List of active threads in svc->srv_threads
973          */
974         cfs_list_t t_link;
975         /**
976          * thread-private data (preallocated memory)
977          */
978         void *t_data;
979         __u32 t_flags;
980         /**
981          * service thread index, from ptlrpc_start_threads
982          */
983         unsigned int t_id;
984         /**
985          * service thread pid
986          */
987         pid_t t_pid; 
988         /**
989          * put watchdog in the structure per thread b=14840
990          */
991         struct lc_watchdog *t_watchdog;
992         /**
993          * the svc this thread belonged to b=18582
994          */
995         struct ptlrpc_service *t_svc;
996         cfs_waitq_t t_ctl_waitq;
997         struct lu_env *t_env;
998 };
999
1000 static inline int thread_is_init(struct ptlrpc_thread *thread)
1001 {
1002         return thread->t_flags == 0;
1003 }
1004
1005 static inline int thread_is_stopped(struct ptlrpc_thread *thread)
1006 {
1007         return !!(thread->t_flags & SVC_STOPPED);
1008 }
1009
1010 static inline int thread_is_stopping(struct ptlrpc_thread *thread)
1011 {
1012         return !!(thread->t_flags & SVC_STOPPING);
1013 }
1014
1015 static inline int thread_is_starting(struct ptlrpc_thread *thread)
1016 {
1017         return !!(thread->t_flags & SVC_STARTING);
1018 }
1019
1020 static inline int thread_is_running(struct ptlrpc_thread *thread)
1021 {
1022         return !!(thread->t_flags & SVC_RUNNING);
1023 }
1024
1025 static inline int thread_is_event(struct ptlrpc_thread *thread)
1026 {
1027         return !!(thread->t_flags & SVC_EVENT);
1028 }
1029
1030 static inline int thread_is_signal(struct ptlrpc_thread *thread)
1031 {
1032         return !!(thread->t_flags & SVC_SIGNAL);
1033 }
1034
1035 static inline void thread_clear_flags(struct ptlrpc_thread *thread, __u32 flags)
1036 {
1037         thread->t_flags &= ~flags;
1038 }
1039
1040 static inline void thread_set_flags(struct ptlrpc_thread *thread, __u32 flags)
1041 {
1042         thread->t_flags = flags;
1043 }
1044
1045 static inline void thread_add_flags(struct ptlrpc_thread *thread, __u32 flags)
1046 {
1047         thread->t_flags |= flags;
1048 }
1049
1050 static inline int thread_test_and_clear_flags(struct ptlrpc_thread *thread,
1051                                               __u32 flags)
1052 {
1053         if (thread->t_flags & flags) {
1054                 thread->t_flags &= ~flags;
1055                 return 1;
1056         }
1057         return 0;
1058 }
1059
1060 /**
1061  * Request buffer descriptor structure.
1062  * This is a structure that contains one posted request buffer for service.
1063  * Once data land into a buffer, event callback creates actual request and
1064  * notifies wakes one of the service threads to process new incoming request.
1065  * More than one request can fit into the buffer.
1066  */
1067 struct ptlrpc_request_buffer_desc {
1068         /** Link item for rqbds on a service */
1069         cfs_list_t             rqbd_list;
1070         /** History of requests for this buffer */
1071         cfs_list_t             rqbd_reqs;
1072         /** Back pointer to service for which this buffer is registered */
1073         struct ptlrpc_service *rqbd_service;
1074         /** LNet descriptor */
1075         lnet_handle_md_t       rqbd_md_h;
1076         int                    rqbd_refcount;
1077         /** The buffer itself */
1078         char                  *rqbd_buffer;
1079         struct ptlrpc_cb_id    rqbd_cbid;
1080         /**
1081          * This "embedded" request structure is only used for the
1082          * last request to fit into the buffer
1083          */
1084         struct ptlrpc_request  rqbd_req;
1085 };
1086
1087 typedef int  (*svc_handler_t)(struct ptlrpc_request *req);
1088
1089 struct ptlrpc_service_ops {
1090         /**
1091          * if non-NULL called during thread creation (ptlrpc_start_thread())
1092          * to initialize service specific per-thread state.
1093          */
1094         int             (*so_thr_init)(struct ptlrpc_thread *thr);
1095         /**
1096          * if non-NULL called during thread shutdown (ptlrpc_main()) to
1097          * destruct state created by ->srv_init().
1098          */
1099         void            (*so_thr_done)(struct ptlrpc_thread *thr);
1100         /**
1101          * Handler function for incoming requests for this service
1102          */
1103         int             (*so_req_handler)(struct ptlrpc_request *req);
1104         /**
1105          * function to determine priority of the request, it's called
1106          * on every new request
1107          */
1108         int             (*so_hpreq_handler)(struct ptlrpc_request *);
1109         /**
1110          * service-specific print fn
1111          */
1112         void            (*so_req_printer)(void *, struct ptlrpc_request *);
1113 };
1114
1115 #ifndef __cfs_cacheline_aligned
1116 /* NB: put it here for reducing patche dependence */
1117 # define __cfs_cacheline_aligned
1118 #endif
1119
1120 /**
1121  * How many high priority requests to serve before serving one normal
1122  * priority request
1123  */
1124 #define PTLRPC_SVC_HP_RATIO 10
1125
1126 /**
1127  * Definition of PortalRPC service.
1128  * The service is listening on a particular portal (like tcp port)
1129  * and perform actions for a specific server like IO service for OST
1130  * or general metadata service for MDS.
1131  *
1132  * ptlrpc service has four locks:
1133  * \a srv_lock
1134  *    serialize operations on rqbd and requests waiting for preprocess
1135  * \a srv_rq_lock
1136  *    serialize operations active requests sent to this portal
1137  * \a srv_at_lock
1138  *    serialize adaptive timeout stuff
1139  * \a srv_rs_lock
1140  *    serialize operations on RS list (reply states)
1141  *
1142  * We don't have any use-case to take two or more locks at the same time
1143  * for now, so there is no lock order issue.
1144  */
1145 struct ptlrpc_service {
1146         /** most often accessed fields */
1147         /** chain thru all services */
1148         cfs_list_t                      srv_list;
1149         /** service operations table */
1150         struct ptlrpc_service_ops       srv_ops;
1151         /** only statically allocated strings here; we don't clean them */
1152         char                           *srv_name;
1153         /** only statically allocated strings here; we don't clean them */
1154         char                           *srv_thread_name;
1155         /** service thread list */
1156         cfs_list_t                      srv_threads;
1157         /** threads to start at beginning of service */
1158         int                             srv_threads_min;
1159         /** thread upper limit */
1160         int                             srv_threads_max;
1161         /** always increasing number */
1162         unsigned                        srv_threads_next_id;
1163         /** # of starting threads */
1164         int                             srv_threads_starting;
1165         /** # running threads */
1166         int                             srv_threads_running;
1167
1168         /** Root of /proc dir tree for this service */
1169         cfs_proc_dir_entry_t           *srv_procroot;
1170         /** Pointer to statistic data for this service */
1171         struct lprocfs_stats           *srv_stats;
1172         /** # hp per lp reqs to handle */
1173         int                             srv_hpreq_ratio;
1174         /** biggest request to receive */
1175         int                             srv_max_req_size;
1176         /** biggest reply to send */
1177         int                             srv_max_reply_size;
1178         /** size of individual buffers */
1179         int                             srv_buf_size;
1180         /** # buffers to allocate in 1 group */
1181         int                             srv_nbuf_per_group;
1182         /** Local portal on which to receive requests */
1183         __u32                           srv_req_portal;
1184         /** Portal on the client to send replies to */
1185         __u32                           srv_rep_portal;
1186         /**
1187          * Tags for lu_context associated with this thread, see struct
1188          * lu_context.
1189          */
1190         __u32                           srv_ctx_tags;
1191         /** soft watchdog timeout multiplier */
1192         int                             srv_watchdog_factor;
1193         /** bind threads to CPUs */
1194         unsigned                        srv_cpu_affinity:1;
1195         /** under unregister_service */
1196         unsigned                        srv_is_stopping:1;
1197
1198         /**
1199          * serialize the following fields, used for protecting
1200          * rqbd list and incoming requests waiting for preprocess
1201          */
1202         cfs_spinlock_t                  srv_lock  __cfs_cacheline_aligned;
1203         /** incoming reqs */
1204         cfs_list_t                      srv_req_in_queue;
1205         /** total # req buffer descs allocated */
1206         int                             srv_nbufs;
1207         /** # posted request buffers */
1208         int                             srv_nrqbd_receiving;
1209         /** timeout before re-posting reqs, in tick */
1210         cfs_duration_t                  srv_rqbd_timeout;
1211         /** request buffers to be reposted */
1212         cfs_list_t                      srv_idle_rqbds;
1213         /** req buffers receiving */
1214         cfs_list_t                      srv_active_rqbds;
1215         /** request buffer history */
1216         cfs_list_t                      srv_history_rqbds;
1217         /** # request buffers in history */
1218         int                             srv_n_history_rqbds;
1219         /** max # request buffers in history */
1220         int                             srv_max_history_rqbds;
1221         /** request history */
1222         cfs_list_t                      srv_request_history;
1223         /** next request sequence # */
1224         __u64                           srv_request_seq;
1225         /** highest seq culled from history */
1226         __u64                           srv_request_max_cull_seq;
1227         /**
1228          * all threads sleep on this. This wait-queue is signalled when new
1229          * incoming request arrives and when difficult reply has to be handled.
1230          */
1231         cfs_waitq_t                     srv_waitq;
1232
1233         /**
1234          * serialize the following fields, used for processing requests
1235          * sent to this portal
1236          */
1237         cfs_spinlock_t                  srv_rq_lock __cfs_cacheline_aligned;
1238         /** # reqs in either of the queues below */
1239         /** reqs waiting for service */
1240         cfs_list_t                      srv_request_queue;
1241         /** high priority queue */
1242         cfs_list_t                      srv_request_hpq;
1243         /** # incoming reqs */
1244         int                             srv_n_queued_reqs;
1245         /** # reqs being served */
1246         int                             srv_n_active_reqs;
1247         /** # HPreqs being served */
1248         int                             srv_n_active_hpreq;
1249         /** # hp requests handled */
1250         int                             srv_hpreq_count;
1251
1252         /** AT stuff */
1253         /** @{ */
1254         /**
1255          * serialize the following fields, used for changes on
1256          * adaptive timeout
1257          */
1258         cfs_spinlock_t                  srv_at_lock __cfs_cacheline_aligned;
1259         /** estimated rpc service time */
1260         struct adaptive_timeout         srv_at_estimate;
1261         /** reqs waiting for replies */
1262         struct ptlrpc_at_array          srv_at_array;
1263         /** early reply timer */
1264         cfs_timer_t                     srv_at_timer;
1265         /** check early replies */
1266         unsigned                        srv_at_check;
1267         /** debug */
1268         cfs_time_t                      srv_at_checktime;
1269         /** @} */
1270
1271         /**
1272          * serialize the following fields, used for processing
1273          * replies for this portal
1274          */
1275         cfs_spinlock_t                  srv_rs_lock __cfs_cacheline_aligned;
1276         /** all the active replies */
1277         cfs_list_t                      srv_active_replies;
1278 #ifndef __KERNEL__
1279         /** replies waiting for service */
1280         cfs_list_t                      srv_reply_queue;
1281 #endif
1282         /** List of free reply_states */
1283         cfs_list_t                      srv_free_rs_list;
1284         /** waitq to run, when adding stuff to srv_free_rs_list */
1285         cfs_waitq_t                     srv_free_rs_waitq;
1286         /** # 'difficult' replies */
1287         cfs_atomic_t                    srv_n_difficult_replies;
1288         //struct ptlrpc_srv_ni srv_interfaces[0];
1289 };
1290
1291 /**
1292  * Declaration of ptlrpcd control structure
1293  */
1294 struct ptlrpcd_ctl {
1295         /**
1296          * Ptlrpc thread control flags (LIOD_START, LIOD_STOP, LIOD_FORCE)
1297          */
1298         unsigned long               pc_flags;
1299         /**
1300          * Thread lock protecting structure fields.
1301          */
1302         cfs_spinlock_t              pc_lock;
1303         /**
1304          * Start completion.
1305          */
1306         cfs_completion_t            pc_starting;
1307         /**
1308          * Stop completion.
1309          */
1310         cfs_completion_t            pc_finishing;
1311         /**
1312          * Thread requests set.
1313          */
1314         struct ptlrpc_request_set  *pc_set;
1315         /**
1316          * Thread name used in cfs_daemonize()
1317          */
1318         char                        pc_name[16];
1319         /**
1320          * Environment for request interpreters to run in.
1321          */
1322         struct lu_env               pc_env;
1323         /**
1324          * Index of ptlrpcd thread in the array.
1325          */
1326         int                         pc_index;
1327         /**
1328          * Number of the ptlrpcd's partners.
1329          */
1330         int                         pc_npartners;
1331         /**
1332          * Pointer to the array of partners' ptlrpcd_ctl structure.
1333          */
1334         struct ptlrpcd_ctl        **pc_partners;
1335         /**
1336          * Record the partner index to be processed next.
1337          */
1338         int                         pc_cursor;
1339 #ifndef __KERNEL__
1340         /**
1341          * Async rpcs flag to make sure that ptlrpcd_check() is called only
1342          * once.
1343          */
1344         int                         pc_recurred;
1345         /**
1346          * Currently not used.
1347          */
1348         void                       *pc_callback;
1349         /**
1350          * User-space async rpcs callback.
1351          */
1352         void                       *pc_wait_callback;
1353         /**
1354          * User-space check idle rpcs callback.
1355          */
1356         void                       *pc_idle_callback;
1357 #endif
1358 };
1359
1360 /* Bits for pc_flags */
1361 enum ptlrpcd_ctl_flags {
1362         /**
1363          * Ptlrpc thread start flag.
1364          */
1365         LIOD_START       = 1 << 0,
1366         /**
1367          * Ptlrpc thread stop flag.
1368          */
1369         LIOD_STOP        = 1 << 1,
1370         /**
1371          * Ptlrpc thread force flag (only stop force so far).
1372          * This will cause aborting any inflight rpcs handled
1373          * by thread if LIOD_STOP is specified.
1374          */
1375         LIOD_FORCE       = 1 << 2,
1376         /**
1377          * This is a recovery ptlrpc thread.
1378          */
1379         LIOD_RECOVERY    = 1 << 3,
1380         /**
1381          * The ptlrpcd is bound to some CPU core.
1382          */
1383         LIOD_BIND        = 1 << 4,
1384 };
1385
1386 /* ptlrpc/events.c */
1387 extern lnet_handle_eq_t ptlrpc_eq_h;
1388 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
1389                                lnet_process_id_t *peer, lnet_nid_t *self);
1390 /**
1391  * These callbacks are invoked by LNet when something happened to
1392  * underlying buffer
1393  * @{
1394  */
1395 extern void request_out_callback(lnet_event_t *ev);
1396 extern void reply_in_callback(lnet_event_t *ev);
1397 extern void client_bulk_callback(lnet_event_t *ev);
1398 extern void request_in_callback(lnet_event_t *ev);
1399 extern void reply_out_callback(lnet_event_t *ev);
1400 #ifdef HAVE_SERVER_SUPPORT
1401 extern void server_bulk_callback(lnet_event_t *ev);
1402 #endif
1403 /** @} */
1404
1405 /* ptlrpc/connection.c */
1406 struct ptlrpc_connection *ptlrpc_connection_get(lnet_process_id_t peer,
1407                                                 lnet_nid_t self,
1408                                                 struct obd_uuid *uuid);
1409 int ptlrpc_connection_put(struct ptlrpc_connection *c);
1410 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
1411 int ptlrpc_connection_init(void);
1412 void ptlrpc_connection_fini(void);
1413 extern lnet_pid_t ptl_get_pid(void);
1414
1415 /* ptlrpc/niobuf.c */
1416 /**
1417  * Actual interfacing with LNet to put/get/register/unregister stuff
1418  * @{
1419  */
1420 #ifdef HAVE_SERVER_SUPPORT
1421 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
1422                                               int npages, int type, int portal);
1423 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
1424 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
1425
1426 static inline int ptlrpc_server_bulk_active(struct ptlrpc_bulk_desc *desc)
1427 {
1428         int rc;
1429
1430         LASSERT(desc != NULL);
1431
1432         cfs_spin_lock(&desc->bd_lock);
1433         rc = desc->bd_network_rw;
1434         cfs_spin_unlock(&desc->bd_lock);
1435         return rc;
1436 }
1437 #endif
1438
1439 int ptlrpc_register_bulk(struct ptlrpc_request *req);
1440 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async);
1441
1442 static inline int ptlrpc_client_bulk_active(struct ptlrpc_request *req)
1443 {
1444         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
1445         int                      rc;
1446
1447         LASSERT(req != NULL);
1448
1449         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
1450             req->rq_bulk_deadline > cfs_time_current_sec())
1451                 return 1;
1452
1453         if (!desc)
1454                 return 0;
1455
1456         cfs_spin_lock(&desc->bd_lock);
1457         rc = desc->bd_network_rw;
1458         cfs_spin_unlock(&desc->bd_lock);
1459         return rc;
1460 }
1461
1462 #define PTLRPC_REPLY_MAYBE_DIFFICULT 0x01
1463 #define PTLRPC_REPLY_EARLY           0x02
1464 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags);
1465 int ptlrpc_reply(struct ptlrpc_request *req);
1466 int ptlrpc_send_error(struct ptlrpc_request *req, int difficult);
1467 int ptlrpc_error(struct ptlrpc_request *req);
1468 void ptlrpc_resend_req(struct ptlrpc_request *request);
1469 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req);
1470 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
1471 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd);
1472 /** @} */
1473
1474 /* ptlrpc/client.c */
1475 /**
1476  * Client-side portals API. Everything to send requests, receive replies,
1477  * request queues, request management, etc.
1478  * @{
1479  */
1480 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
1481                         struct ptlrpc_client *);
1482 void ptlrpc_cleanup_client(struct obd_import *imp);
1483 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
1484
1485 int ptlrpc_queue_wait(struct ptlrpc_request *req);
1486 int ptlrpc_replay_req(struct ptlrpc_request *req);
1487 int ptlrpc_unregister_reply(struct ptlrpc_request *req, int async);
1488 void ptlrpc_restart_req(struct ptlrpc_request *req);
1489 void ptlrpc_abort_inflight(struct obd_import *imp);
1490 void ptlrpc_cleanup_imp(struct obd_import *imp);
1491 void ptlrpc_abort_set(struct ptlrpc_request_set *set);
1492
1493 struct ptlrpc_request_set *ptlrpc_prep_set(void);
1494 struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
1495                                              void *arg);
1496 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
1497                       set_interpreter_func fn, void *data);
1498 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
1499 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set);
1500 int ptlrpc_set_wait(struct ptlrpc_request_set *);
1501 int ptlrpc_expired_set(void *data);
1502 void ptlrpc_interrupted_set(void *data);
1503 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
1504 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
1505 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
1506 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
1507                             struct ptlrpc_request *req);
1508
1509 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
1510 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
1511
1512 struct ptlrpc_request_pool *
1513 ptlrpc_init_rq_pool(int, int,
1514                     void (*populate_pool)(struct ptlrpc_request_pool *, int));
1515
1516 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req);
1517 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
1518                                             const struct req_format *format);
1519 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
1520                                             struct ptlrpc_request_pool *,
1521                                             const struct req_format *format);
1522 void ptlrpc_request_free(struct ptlrpc_request *request);
1523 int ptlrpc_request_pack(struct ptlrpc_request *request,
1524                         __u32 version, int opcode);
1525 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
1526                                                 const struct req_format *format,
1527                                                 __u32 version, int opcode);
1528 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
1529                              __u32 version, int opcode, char **bufs,
1530                              struct ptlrpc_cli_ctx *ctx);
1531 struct ptlrpc_request *ptlrpc_prep_fakereq(struct obd_import *imp,
1532                                            unsigned int timeout,
1533                                            ptlrpc_interpterer_t interpreter);
1534 void ptlrpc_fakereq_finished(struct ptlrpc_request *req);
1535
1536 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
1537                                        int opcode, int count, __u32 *lengths,
1538                                        char **bufs);
1539 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
1540                                              __u32 version, int opcode,
1541                                             int count, __u32 *lengths, char **bufs,
1542                                             struct ptlrpc_request_pool *pool);
1543 void ptlrpc_req_finished(struct ptlrpc_request *request);
1544 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
1545 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
1546 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
1547                                               int npages, int type, int portal);
1548 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
1549 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
1550                            cfs_page_t *page, int pageoffset, int len);
1551 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1552                                       struct obd_import *imp);
1553 __u64 ptlrpc_next_xid(void);
1554 __u64 ptlrpc_sample_next_xid(void);
1555 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
1556
1557 /* Set of routines to run a function in ptlrpcd context */
1558 void *ptlrpcd_alloc_work(struct obd_import *imp,
1559                          int (*cb)(const struct lu_env *, void *), void *data);
1560 void ptlrpcd_destroy_work(void *handler);
1561 int ptlrpcd_queue_work(void *handler);
1562
1563 /** @} */
1564 struct ptlrpc_service_buf_conf {
1565         /* nbufs is how many buffers to post */
1566         unsigned int                    bc_nbufs;
1567         /* buffer size to post */
1568         unsigned int                    bc_buf_size;
1569         /* portal to listed for requests on */
1570         unsigned int                    bc_req_portal;
1571         /* portal of where to send replies to */
1572         unsigned int                    bc_rep_portal;
1573         /* maximum request size to be accepted for this service */
1574         unsigned int                    bc_req_max_size;
1575         /* maximum reply size this service can ever send */
1576         unsigned int                    bc_rep_max_size;
1577 };
1578
1579 struct ptlrpc_service_thr_conf {
1580         /* threadname should be 8 characters or less - 6 will be added on */
1581         char                            *tc_thr_name;
1582         /* min number of service threads to start */
1583         unsigned int                    tc_nthrs_min;
1584         /* max number of service threads to start */
1585         unsigned int                    tc_nthrs_max;
1586         /* set NUMA node affinity for service threads */
1587         unsigned int                    tc_cpu_affinity;
1588         /* Tags for lu_context associated with service thread */
1589         __u32                           tc_ctx_tags;
1590 };
1591
1592 struct ptlrpc_service_conf {
1593         /* service name */
1594         char                            *psc_name;
1595         /* soft watchdog timeout multiplifier to print stuck service traces */
1596         unsigned int                    psc_watchdog_factor;
1597         /* buffer information */
1598         struct ptlrpc_service_buf_conf  psc_buf;
1599         /* thread information */
1600         struct ptlrpc_service_thr_conf  psc_thr;
1601         /* function table */
1602         struct ptlrpc_service_ops       psc_ops;
1603 };
1604
1605 /* ptlrpc/service.c */
1606 /**
1607  * Server-side services API. Register/unregister service, request state
1608  * management, service thread management
1609  *
1610  * @{
1611  */
1612 void ptlrpc_save_lock(struct ptlrpc_request *req,
1613                       struct lustre_handle *lock, int mode, int no_ack);
1614 void ptlrpc_commit_replies(struct obd_export *exp);
1615 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs);
1616 void ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs);
1617 struct ptlrpc_service *ptlrpc_register_service(
1618                                 struct ptlrpc_service_conf *conf,
1619                                 struct proc_dir_entry *proc_entry);
1620 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
1621
1622 int ptlrpc_start_threads(struct ptlrpc_service *svc);
1623 int ptlrpc_start_thread(struct ptlrpc_service *svc);
1624 int ptlrpc_unregister_service(struct ptlrpc_service *service);
1625 int liblustre_check_services(void *arg);
1626 void ptlrpc_daemonize(char *name);
1627 int ptlrpc_service_health_check(struct ptlrpc_service *);
1628 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req);
1629 void ptlrpc_server_drop_request(struct ptlrpc_request *req);
1630
1631 #ifdef __KERNEL__
1632 int ptlrpc_hr_init(void);
1633 void ptlrpc_hr_fini(void);
1634 #else
1635 # define ptlrpc_hr_init() (0)
1636 # define ptlrpc_hr_fini() do {} while(0)
1637 #endif
1638
1639 struct ptlrpc_svc_data {
1640         char *name;
1641         struct ptlrpc_service *svc;
1642         struct ptlrpc_thread *thread;
1643 };
1644 /** @} */
1645
1646 /* ptlrpc/import.c */
1647 /**
1648  * Import API
1649  * @{
1650  */
1651 int ptlrpc_connect_import(struct obd_import *imp);
1652 int ptlrpc_init_import(struct obd_import *imp);
1653 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
1654 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
1655 void deuuidify(char *uuid, const char *prefix, char **uuid_start,
1656                int *uuid_len);
1657
1658 /* ptlrpc/pack_generic.c */
1659 int ptlrpc_reconnect_import(struct obd_import *imp);
1660 /** @} */
1661
1662 /**
1663  * ptlrpc msg buffer and swab interface 
1664  *
1665  * @{
1666  */
1667 int ptlrpc_buf_need_swab(struct ptlrpc_request *req, const int inout,
1668                          int index);
1669 void ptlrpc_buf_set_swabbed(struct ptlrpc_request *req, const int inout,
1670                                 int index);
1671 int ptlrpc_unpack_rep_msg(struct ptlrpc_request *req, int len);
1672 int ptlrpc_unpack_req_msg(struct ptlrpc_request *req, int len);
1673
1674 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
1675 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
1676                         char **bufs);
1677 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
1678                         __u32 *lens, char **bufs);
1679 int lustre_pack_reply(struct ptlrpc_request *, int count, __u32 *lens,
1680                       char **bufs);
1681 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
1682                          __u32 *lens, char **bufs, int flags);
1683 #define LPRFL_EARLY_REPLY 1
1684 int lustre_pack_reply_flags(struct ptlrpc_request *, int count, __u32 *lens,
1685                             char **bufs, int flags);
1686 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
1687                       unsigned int newlen, int move_data);
1688 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
1689 int __lustre_unpack_msg(struct lustre_msg *m, int len);
1690 int lustre_msg_hdr_size(__u32 magic, int count);
1691 int lustre_msg_size(__u32 magic, int count, __u32 *lengths);
1692 int lustre_msg_size_v2(int count, __u32 *lengths);
1693 int lustre_packed_msg_size(struct lustre_msg *msg);
1694 int lustre_msg_early_size(void);
1695 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size);
1696 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
1697 int lustre_msg_buflen(struct lustre_msg *m, int n);
1698 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
1699 int lustre_msg_bufcount(struct lustre_msg *m);
1700 char *lustre_msg_string(struct lustre_msg *m, int n, int max_len);
1701 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg);
1702 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags);
1703 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
1704 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
1705 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
1706 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
1707 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
1708 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
1709 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
1710 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
1711 __u32 lustre_msg_get_type(struct lustre_msg *msg);
1712 __u32 lustre_msg_get_version(struct lustre_msg *msg);
1713 void lustre_msg_add_version(struct lustre_msg *msg, int version);
1714 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
1715 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
1716 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
1717 __u64 *lustre_msg_get_versions(struct lustre_msg *msg);
1718 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
1719 __u64 lustre_msg_get_slv(struct lustre_msg *msg);
1720 __u32 lustre_msg_get_limit(struct lustre_msg *msg);
1721 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv);
1722 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit);
1723 int lustre_msg_get_status(struct lustre_msg *msg);
1724 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
1725 int lustre_msg_is_v1(struct lustre_msg *msg);
1726 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
1727 __u32 lustre_msg_get_timeout(struct lustre_msg *msg);
1728 __u32 lustre_msg_get_service_time(struct lustre_msg *msg);
1729 char *lustre_msg_get_jobid(struct lustre_msg *msg);
1730 __u32 lustre_msg_get_cksum(struct lustre_msg *msg);
1731 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 0, 0)
1732 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg, int compat18);
1733 #else
1734 # warning "remove checksum compatibility support for b1_8"
1735 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg);
1736 #endif
1737 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
1738 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
1739 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
1740 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
1741 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
1742 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions);
1743 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
1744 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
1745 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
1746 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *sizes);
1747 void ptlrpc_request_set_replen(struct ptlrpc_request *req);
1748 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout);
1749 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time);
1750 void lustre_msg_set_jobid(struct lustre_msg *msg, char *jobid);
1751 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum);
1752
1753 static inline void
1754 lustre_shrink_reply(struct ptlrpc_request *req, int segment,
1755                     unsigned int newlen, int move_data)
1756 {
1757         LASSERT(req->rq_reply_state);
1758         LASSERT(req->rq_repmsg);
1759         req->rq_replen = lustre_shrink_msg(req->rq_repmsg, segment,
1760                                            newlen, move_data);
1761 }
1762 /** @} */
1763
1764 /** Change request phase of \a req to \a new_phase */
1765 static inline void
1766 ptlrpc_rqphase_move(struct ptlrpc_request *req, enum rq_phase new_phase)
1767 {
1768         if (req->rq_phase == new_phase)
1769                 return;
1770
1771         if (new_phase == RQ_PHASE_UNREGISTERING) {
1772                 req->rq_next_phase = req->rq_phase;
1773                 if (req->rq_import)
1774                         cfs_atomic_inc(&req->rq_import->imp_unregistering);
1775         }
1776
1777         if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1778                 if (req->rq_import)
1779                         cfs_atomic_dec(&req->rq_import->imp_unregistering);
1780         }
1781
1782         DEBUG_REQ(D_INFO, req, "move req \"%s\" -> \"%s\"",
1783                   ptlrpc_rqphase2str(req), ptlrpc_phase2str(new_phase));
1784
1785         req->rq_phase = new_phase;
1786 }
1787
1788 /**
1789  * Returns true if request \a req got early reply and hard deadline is not met 
1790  */
1791 static inline int
1792 ptlrpc_client_early(struct ptlrpc_request *req)
1793 {
1794         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1795             req->rq_reply_deadline > cfs_time_current_sec())
1796                 return 0;
1797         return req->rq_early;
1798 }
1799
1800 /**
1801  * Returns true if we got real reply from server for this request
1802  */
1803 static inline int
1804 ptlrpc_client_replied(struct ptlrpc_request *req)
1805 {
1806         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1807             req->rq_reply_deadline > cfs_time_current_sec())
1808                 return 0;
1809         return req->rq_replied;
1810 }
1811
1812 /** Returns true if request \a req is in process of receiving server reply */
1813 static inline int
1814 ptlrpc_client_recv(struct ptlrpc_request *req)
1815 {
1816         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1817             req->rq_reply_deadline > cfs_time_current_sec())
1818                 return 1;
1819         return req->rq_receiving_reply;
1820 }
1821
1822 static inline int
1823 ptlrpc_client_recv_or_unlink(struct ptlrpc_request *req)
1824 {
1825         int rc;
1826
1827         cfs_spin_lock(&req->rq_lock);
1828         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1829             req->rq_reply_deadline > cfs_time_current_sec()) {
1830                 cfs_spin_unlock(&req->rq_lock);
1831                 return 1;
1832         }
1833         rc = req->rq_receiving_reply || req->rq_must_unlink;
1834         cfs_spin_unlock(&req->rq_lock);
1835         return rc;
1836 }
1837
1838 static inline void
1839 ptlrpc_client_wake_req(struct ptlrpc_request *req)
1840 {
1841         if (req->rq_set == NULL)
1842                 cfs_waitq_signal(&req->rq_reply_waitq);
1843         else
1844                 cfs_waitq_signal(&req->rq_set->set_waitq);
1845 }
1846
1847 static inline void
1848 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
1849 {
1850         LASSERT(cfs_atomic_read(&rs->rs_refcount) > 0);
1851         cfs_atomic_inc(&rs->rs_refcount);
1852 }
1853
1854 static inline void
1855 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
1856 {
1857         LASSERT(cfs_atomic_read(&rs->rs_refcount) > 0);
1858         if (cfs_atomic_dec_and_test(&rs->rs_refcount))
1859                 lustre_free_reply_state(rs);
1860 }
1861
1862 /* Should only be called once per req */
1863 static inline void ptlrpc_req_drop_rs(struct ptlrpc_request *req)
1864 {
1865         if (req->rq_reply_state == NULL)
1866                 return; /* shouldn't occur */
1867         ptlrpc_rs_decref(req->rq_reply_state);
1868         req->rq_reply_state = NULL;
1869         req->rq_repmsg = NULL;
1870 }
1871
1872 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
1873 {
1874         return lustre_msg_get_magic(req->rq_reqmsg);
1875 }
1876
1877 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
1878 {
1879         switch (req->rq_reqmsg->lm_magic) {
1880         case LUSTRE_MSG_MAGIC_V2:
1881                 return req->rq_reqmsg->lm_repsize;
1882         default:
1883                 LASSERTF(0, "incorrect message magic: %08x\n",
1884                          req->rq_reqmsg->lm_magic);
1885                 return -EFAULT;
1886         }
1887 }
1888
1889 static inline int ptlrpc_send_limit_expired(struct ptlrpc_request *req)
1890 {
1891         if (req->rq_delay_limit != 0 &&
1892             cfs_time_before(cfs_time_add(req->rq_queued_time,
1893                                          cfs_time_seconds(req->rq_delay_limit)),
1894                             cfs_time_current())) {
1895                 return 1;
1896         }
1897         return 0;
1898 }
1899
1900 static inline int ptlrpc_no_resend(struct ptlrpc_request *req)
1901 {
1902         if (!req->rq_no_resend && ptlrpc_send_limit_expired(req)) {
1903                 cfs_spin_lock(&req->rq_lock);
1904                 req->rq_no_resend = 1;
1905                 cfs_spin_unlock(&req->rq_lock);
1906         }
1907         return req->rq_no_resend;
1908 }
1909
1910 /* ldlm/ldlm_lib.c */
1911 /**
1912  * Target client logic
1913  * @{
1914  */
1915 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg);
1916 int client_obd_cleanup(struct obd_device *obddev);
1917 int client_connect_import(const struct lu_env *env,
1918                           struct obd_export **exp, struct obd_device *obd,
1919                           struct obd_uuid *cluuid, struct obd_connect_data *,
1920                           void *localdata);
1921 int client_disconnect_export(struct obd_export *exp);
1922 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
1923                            int priority);
1924 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
1925 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
1926                             struct obd_uuid *uuid);
1927 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
1928 void client_destroy_import(struct obd_import *imp);
1929 /** @} */
1930
1931 #ifdef HAVE_SERVER_SUPPORT
1932 int server_disconnect_export(struct obd_export *exp);
1933 #endif
1934
1935 /* ptlrpc/pinger.c */
1936 /**
1937  * Pinger API (client side only)
1938  * @{
1939  */
1940 enum timeout_event {
1941         TIMEOUT_GRANT = 1
1942 };
1943 struct timeout_item;
1944 typedef int (*timeout_cb_t)(struct timeout_item *, void *);
1945 int ptlrpc_pinger_add_import(struct obd_import *imp);
1946 int ptlrpc_pinger_del_import(struct obd_import *imp);
1947 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
1948                               timeout_cb_t cb, void *data,
1949                               cfs_list_t *obd_list);
1950 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
1951                               enum timeout_event event);
1952 struct ptlrpc_request * ptlrpc_prep_ping(struct obd_import *imp);
1953 int ptlrpc_obd_ping(struct obd_device *obd);
1954 cfs_time_t ptlrpc_suspend_wakeup_time(void);
1955 #ifdef __KERNEL__
1956 void ping_evictor_start(void);
1957 void ping_evictor_stop(void);
1958 #else
1959 #define ping_evictor_start()    do {} while (0)
1960 #define ping_evictor_stop()     do {} while (0)
1961 #endif
1962 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req);
1963 /** @} */
1964
1965 /* ptlrpc daemon bind policy */
1966 typedef enum {
1967         /* all ptlrpcd threads are free mode */
1968         PDB_POLICY_NONE          = 1,
1969         /* all ptlrpcd threads are bound mode */
1970         PDB_POLICY_FULL          = 2,
1971         /* <free1 bound1> <free2 bound2> ... <freeN boundN> */
1972         PDB_POLICY_PAIR          = 3,
1973         /* <free1 bound1> <bound1 free2> ... <freeN boundN> <boundN free1>,
1974          * means each ptlrpcd[X] has two partners: thread[X-1] and thread[X+1].
1975          * If kernel supports NUMA, pthrpcd threads are binded and
1976          * grouped by NUMA node */
1977         PDB_POLICY_NEIGHBOR      = 4,
1978 } pdb_policy_t;
1979
1980 /* ptlrpc daemon load policy
1981  * It is caller's duty to specify how to push the async RPC into some ptlrpcd
1982  * queue, but it is not enforced, affected by "ptlrpcd_bind_policy". If it is
1983  * "PDB_POLICY_FULL", then the RPC will be processed by the selected ptlrpcd,
1984  * Otherwise, the RPC may be processed by the selected ptlrpcd or its partner,
1985  * depends on which is scheduled firstly, to accelerate the RPC processing. */
1986 typedef enum {
1987         /* on the same CPU core as the caller */
1988         PDL_POLICY_SAME         = 1,
1989         /* within the same CPU partition, but not the same core as the caller */
1990         PDL_POLICY_LOCAL        = 2,
1991         /* round-robin on all CPU cores, but not the same core as the caller */
1992         PDL_POLICY_ROUND        = 3,
1993         /* the specified CPU core is preferred, but not enforced */
1994         PDL_POLICY_PREFERRED    = 4,
1995 } pdl_policy_t;
1996
1997 /* ptlrpc/ptlrpcd.c */
1998 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force);
1999 void ptlrpcd_wake(struct ptlrpc_request *req);
2000 void ptlrpcd_add_req(struct ptlrpc_request *req, pdl_policy_t policy, int idx);
2001 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set);
2002 int ptlrpcd_addref(void);
2003 void ptlrpcd_decref(void);
2004
2005 /* ptlrpc/lproc_ptlrpc.c */
2006 /**
2007  * procfs output related functions
2008  * @{
2009  */
2010 const char* ll_opcode2str(__u32 opcode);
2011 #ifdef LPROCFS
2012 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
2013 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
2014 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes);
2015 #else
2016 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
2017 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
2018 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes) {}
2019 #endif
2020 /** @} */
2021
2022 /* ptlrpc/llog_server.c */
2023 int llog_origin_handle_create(struct ptlrpc_request *req);
2024 int llog_origin_handle_destroy(struct ptlrpc_request *req);
2025 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
2026 int llog_origin_handle_next_block(struct ptlrpc_request *req);
2027 int llog_origin_handle_read_header(struct ptlrpc_request *req);
2028 int llog_origin_handle_close(struct ptlrpc_request *req);
2029 int llog_origin_handle_cancel(struct ptlrpc_request *req);
2030 int llog_catinfo(struct ptlrpc_request *req);
2031
2032 /* ptlrpc/llog_client.c */
2033 extern struct llog_operations llog_client_ops;
2034
2035 /** @} net */
2036
2037 #endif
2038 /** @} PtlRPC */