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