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