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