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