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