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