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