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