Whamcloud - gitweb
Branch b1_6
[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  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef _LUSTRE_NET_H
24 #define _LUSTRE_NET_H
25
26 #if defined(__linux__)
27 #include <linux/lustre_net.h>
28 #elif defined(__APPLE__)
29 #include <darwin/lustre_net.h>
30 #elif defined(__WINNT__)
31 #include <winnt/lustre_net.h>
32 #else
33 #error Unsupported operating system.
34 #endif
35
36 #include <libcfs/kp30.h>
37 // #include <obd.h>
38 #include <lnet/lnet.h>
39 #include <lustre/lustre_idl.h>
40 #include <lustre_ha.h>
41 #include <lustre_import.h>
42 #include <lprocfs_status.h>
43
44 /* MD flags we _always_ use */
45 #define PTLRPC_MD_OPTIONS  0
46
47 /* Define maxima for bulk I/O
48  * CAVEAT EMPTOR, with multinet (i.e. routers forwarding between networks)
49  * these limits are system wide and not interface-local. */
50 #define PTLRPC_MAX_BRW_BITS     LNET_MTU_BITS
51 #define PTLRPC_MAX_BRW_SIZE     (1<<LNET_MTU_BITS)
52 #define PTLRPC_MAX_BRW_PAGES    (PTLRPC_MAX_BRW_SIZE >> CFS_PAGE_SHIFT)
53
54 /* When PAGE_SIZE is a constant, we can check our arithmetic here with cpp! */
55 #ifdef __KERNEL__
56 # if ((PTLRPC_MAX_BRW_PAGES & (PTLRPC_MAX_BRW_PAGES - 1)) != 0)
57 #  error "PTLRPC_MAX_BRW_PAGES isn't a power of two"
58 # endif
59 # if (PTLRPC_MAX_BRW_SIZE != (PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE))
60 #  error "PTLRPC_MAX_BRW_SIZE isn't PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE"
61 # endif
62 # if (PTLRPC_MAX_BRW_SIZE > LNET_MTU)
63 #  error "PTLRPC_MAX_BRW_SIZE too big"
64 # endif
65 # if (PTLRPC_MAX_BRW_PAGES > LNET_MAX_IOV)
66 #  error "PTLRPC_MAX_BRW_PAGES too big"
67 # endif
68 #endif /* __KERNEL__ */
69
70 /* Size over which to OBD_VMALLOC() rather than OBD_ALLOC() service request
71  * buffers */
72 #define SVC_BUF_VMALLOC_THRESHOLD (2 * CFS_PAGE_SIZE)
73
74 /* The following constants determine how memory is used to buffer incoming
75  * service requests.
76  *
77  * ?_NBUFS              # buffers to allocate when growing the pool
78  * ?_BUFSIZE            # bytes in a single request buffer
79  * ?_MAXREQSIZE         # maximum request service will receive
80  *
81  * When fewer than ?_NBUFS/2 buffers are posted for receive, another chunk
82  * of ?_NBUFS is added to the pool.
83  *
84  * Messages larger than ?_MAXREQSIZE are dropped.  Request buffers are
85  * considered full when less than ?_MAXREQSIZE is left in them.
86  */
87
88 #define LDLM_THREADS_AUTO_MIN                                                 \
89         min((int)(num_online_cpus() * num_online_cpus() * 2), 8)
90 #define LDLM_THREADS_AUTO_MAX (LDLM_THREADS_AUTO_MIN * 16)
91 #define LDLM_BL_THREADS  LDLM_THREADS_AUTO_MIN
92 #define LDLM_NBUFS      (64 * num_online_cpus())
93 #define LDLM_BUFSIZE    (8 * 1024)
94 #define LDLM_MAXREQSIZE (5 * 1024)
95 #define LDLM_MAXREPSIZE (1024)
96
97 /* Absolute limits */
98 #define MDS_THREADS_MIN 2
99 #define MDS_THREADS_MAX 512
100 #define MDS_THREADS_MIN_READPAGE 2
101 #define MDS_NBUFS       (64 * num_online_cpus())
102 #define MDS_BUFSIZE     (8 * 1024)
103 /* Assume file name length = FNAME_MAX = 256 (true for ext3).
104  *        path name length = PATH_MAX = 4096
105  *        LOV MD size max  = EA_MAX = 4000
106  * symlink:  FNAME_MAX + PATH_MAX  <- largest
107  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
108  * rename:   FNAME_MAX + FNAME_MAX
109  * open:     FNAME_MAX + EA_MAX
110  *
111  * MDS_MAXREQSIZE ~= 4736 bytes =
112  * lustre_msg + ldlm_request + mds_body + mds_rec_create + FNAME_MAX + PATH_MAX
113  * MDS_MAXREPSIZE ~= 8300 bytes = lustre_msg + llog_header
114  * or, for mds_close() and mds_reint_unlink() on a many-OST filesystem:
115  *      = 9210 bytes = lustre_msg + mds_body + 160 * (easize + cookiesize)
116  *
117  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
118  * except in the open case where there are a large number of OSTs in a LOV.
119  */
120 #define MDS_MAXREQSIZE  (5 * 1024)
121 #define MDS_MAXREPSIZE  max(9 * 1024, 280 + LOV_MAX_STRIPE_COUNT * 56)
122
123 #define MGS_THREADS_AUTO_MIN 2
124 #define MGS_THREADS_AUTO_MAX 32
125 #define MGS_NBUFS       (64 * num_online_cpus())
126 #define MGS_BUFSIZE     (8 * 1024)
127 #define MGS_MAXREQSIZE  (8 * 1024)
128 #define MGS_MAXREPSIZE  (9 * 1024)
129
130 /* Absolute limits */
131 #define OSS_THREADS_MIN 2
132 #define OSS_THREADS_MAX 512
133 #define OST_NBUFS       (64 * num_online_cpus())
134 #define OST_BUFSIZE     (8 * 1024)
135 /* OST_MAXREQSIZE ~= 4768 bytes =
136  * lustre_msg + obdo + 16 * obd_ioobj + 256 * niobuf_remote
137  *
138  * - single object with 16 pages is 512 bytes
139  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
140  */
141 #define OST_MAXREQSIZE  (5 * 1024)
142 #define OST_MAXREPSIZE  (9 * 1024)
143
144 struct ptlrpc_connection {
145         struct list_head        c_link;
146         struct hlist_node       c_hash;
147         lnet_nid_t              c_self;
148         lnet_process_id_t       c_peer;
149         struct obd_uuid         c_remote_uuid;
150         atomic_t                c_refcount;
151 };
152
153 struct ptlrpc_client {
154         __u32                     cli_request_portal;
155         __u32                     cli_reply_portal;
156         char                     *cli_name;
157 };
158
159 /* state flags of requests */
160 /* XXX only ones left are those used by the bulk descs as well! */
161 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
162 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
163
164 #define REQ_MAX_ACK_LOCKS 8
165
166 union ptlrpc_async_args {
167         /* Scratchpad for passing args to completion interpreter. Users
168          * cast to the struct of their choosing, and LASSERT that this is
169          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
170          * a pointer to it here.  The pointer_arg ensures this struct is at
171          * least big enough for that. */
172         void      *pointer_arg[9];
173         __u64      space[4];
174 };
175
176 struct ptlrpc_request_set;
177 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
178
179 struct ptlrpc_request_set {
180         int               set_remaining; /* # uncompleted requests */
181         cfs_waitq_t       set_waitq;
182         cfs_waitq_t      *set_wakeup_ptr;
183         struct list_head  set_requests;
184         struct list_head  set_cblist; /* list of completion callbacks */
185         set_interpreter_func    set_interpret; /* completion callback */
186         void              *set_arg; /* completion context */
187         /* locked so that any old caller can communicate requests to
188          * the set holder who can then fold them into the lock-free set */
189         spinlock_t        set_new_req_lock;
190         struct list_head  set_new_requests;
191 };
192
193 struct ptlrpc_set_cbdata {
194         struct list_head        psc_item;
195         set_interpreter_func    psc_interpret;
196         void                   *psc_data;
197 };
198
199 struct ptlrpc_bulk_desc;
200
201 /*
202  * ptlrpc callback & work item stuff
203  */
204 struct ptlrpc_cb_id {
205         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
206         void    *cbid_arg;                      /* additional arg */
207 };
208
209 #define RS_MAX_LOCKS 4
210 #define RS_DEBUG     1
211
212 struct ptlrpc_reply_state {
213         struct ptlrpc_cb_id    rs_cb_id;
214         struct list_head       rs_list;
215         struct list_head       rs_exp_list;
216         struct list_head       rs_obd_list;
217 #if RS_DEBUG
218         struct list_head       rs_debug_list;
219 #endif
220         /* updates to following flag serialised by srv_request_lock */
221         unsigned int           rs_difficult:1;     /* ACK/commit stuff */
222         unsigned int           rs_scheduled:1;     /* being handled? */
223         unsigned int           rs_scheduled_ever:1;/* any schedule attempts? */
224         unsigned int           rs_handled:1;  /* been handled yet? */
225         unsigned int           rs_on_net:1;   /* reply_out_callback pending? */
226         unsigned int           rs_prealloc:1; /* rs from prealloc list */
227
228         int                    rs_size;
229         __u64                  rs_transno;
230         __u64                  rs_xid;
231         struct obd_export     *rs_export;
232         struct ptlrpc_service *rs_service;
233         lnet_handle_md_t       rs_md_h;
234         atomic_t               rs_refcount;
235
236         /* locks awaiting client reply ACK */
237         int                    rs_nlocks;
238         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
239         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
240         /* last member: variable sized reply message */
241         struct lustre_msg     *rs_msg;
242 };
243
244 struct ptlrpc_thread;
245
246 enum rq_phase {
247         RQ_PHASE_NEW         = 0xebc0de00,
248         RQ_PHASE_RPC         = 0xebc0de01,
249         RQ_PHASE_BULK        = 0xebc0de02,
250         RQ_PHASE_INTERPRET   = 0xebc0de03,
251         RQ_PHASE_COMPLETE    = 0xebc0de04,
252 };
253
254 struct ptlrpc_request_pool {
255         spinlock_t prp_lock;
256         struct list_head prp_req_list;    /* list of ptlrpc_request structs */
257         int prp_rq_size;
258         void (*prp_populate)(struct ptlrpc_request_pool *, int);
259 };
260
261 struct ptlrpc_request {
262         int rq_type; /* one of PTL_RPC_MSG_* */
263         struct list_head rq_list;
264         struct list_head rq_timed_list;         /* server-side early replies */
265         struct list_head rq_history_list;       /* server-side history */
266         __u64            rq_history_seq;        /* history sequence # */
267         int rq_status;
268         spinlock_t rq_lock;
269         /* client-side flags are serialized by rq_lock */
270         unsigned int rq_intr:1, rq_replied:1, rq_err:1,
271                 rq_timedout:1, rq_resend:1, rq_restart:1,
272                 /*
273                  * when ->rq_replay is set, request is kept by the client even
274                  * after server commits corresponding transaction. This is
275                  * used for operations that require sequence of multiple
276                  * requests to be replayed. The only example currently is file
277                  * open/close. When last request in such a sequence is
278                  * committed, ->rq_replay is cleared on all requests in the
279                  * sequence.
280                  */
281                 rq_replay:1,
282                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
283                 rq_no_delay:1, rq_net_err:1, rq_early:1, rq_must_unlink:1,
284                 /* server-side flags */
285                 rq_packed_final:1;  /* packed final reply */
286         enum rq_phase rq_phase; /* one of RQ_PHASE_* */
287         atomic_t rq_refcount;   /* client-side refcount for SENT race,
288                                    server-side refcounf for multiple replies */
289
290         struct ptlrpc_thread *rq_svc_thread; /* initial thread servicing req */
291
292         int rq_request_portal;  /* XXX FIXME bug 249 */
293         int rq_reply_portal;    /* XXX FIXME bug 249 */
294
295         int rq_nob_received; /* client-side # reply bytes actually received  */
296
297         int rq_reqlen;
298         struct lustre_msg *rq_reqmsg;
299
300         int rq_replen;
301         struct lustre_msg *rq_repbuf; /* client only, buf may be bigger than msg */
302         struct lustre_msg *rq_repmsg;
303         __u64 rq_transno;
304         __u64 rq_xid;
305         struct list_head rq_replay_list;
306
307         __u32 rq_req_swab_mask;
308         __u32 rq_rep_swab_mask;
309
310         int rq_import_generation;
311         enum lustre_imp_state rq_send_state;
312
313         int rq_early_count;           /* how many early replies (for stats) */
314
315         /* client+server request */
316         lnet_handle_md_t     rq_req_md_h;
317         struct ptlrpc_cb_id  rq_req_cbid;
318
319         /* server-side... */
320         struct timeval       rq_arrival_time;       /* request arrival time */
321         struct ptlrpc_reply_state *rq_reply_state;  /* separated reply state */
322         struct ptlrpc_request_buffer_desc *rq_rqbd; /* incoming request buffer*/
323 #ifdef CRAY_XT3
324         __u32                rq_uid;            /* peer uid, used in MDS only */
325 #endif
326
327         /* client-only incoming reply */
328         lnet_handle_md_t     rq_reply_md_h;
329         cfs_waitq_t          rq_reply_waitq;
330         struct ptlrpc_cb_id  rq_reply_cbid;
331
332         lnet_nid_t           rq_self;
333         lnet_process_id_t    rq_peer;
334         struct obd_export   *rq_export;
335         struct obd_import   *rq_import;
336
337         void (*rq_replay_cb)(struct ptlrpc_request *);
338         void (*rq_commit_cb)(struct ptlrpc_request *);
339         void  *rq_cb_data;
340
341         struct ptlrpc_bulk_desc *rq_bulk;       /* client side bulk */
342         /* client outgoing req */
343         time_t rq_sent;                         /* when request sent, seconds, 
344                                                  * or time when request should
345                                                  * be sent */
346         volatile time_t rq_deadline;     /* when request must finish. volatile
347                so that servers' early reply updates to the deadline aren't 
348                kept in per-cpu cache */
349         int    rq_timeout;               /* service time estimate (secs) */
350
351         /* Multi-rpc bits */
352         struct list_head rq_set_chain;
353         struct ptlrpc_request_set *rq_set;
354         void *rq_interpret_reply;               /* Async completion handler */
355         union ptlrpc_async_args rq_async_args;  /* Async completion context */
356         void *rq_ptlrpcd_data;
357         struct ptlrpc_request_pool *rq_pool;    /* Pool if request from
358                                                    preallocated list */
359 };
360
361 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
362 {
363         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
364         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
365         req->rq_req_swab_mask |= 1 << index;
366 }
367
368 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
369 {
370         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
371         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
372         req->rq_rep_swab_mask |= 1 << index;
373 }
374
375 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
376
377         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
378         return req->rq_req_swab_mask & (1 << index);
379 }
380
381 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
382 {
383         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
384         return req->rq_rep_swab_mask & (1 << index);
385 }
386
387 #define SWAB_PARANOIA 1
388
389 #if SWAB_PARANOIA
390 /* unpacking: assert idx not unpacked already */
391 #define LASSERT_REQSWAB(rq, idx) lustre_set_req_swabbed(rq, idx)
392 #define LASSERT_REPSWAB(rq, idx) lustre_set_rep_swabbed(rq, idx)
393
394 /* just looking: assert idx already unpacked */
395 #define LASSERT_REQSWABBED(rq, idx) LASSERT(lustre_req_swabbed(rq, idx))
396 #define LASSERT_REPSWABBED(rq, idx) LASSERT(lustre_rep_swabbed(rq, idx))
397
398 #else
399 #define LASSERT_REQSWAB(rq, idx)
400 #define LASSERT_REPSWAB(rq, idx)
401 #define LASSERT_REQSWABBED(rq, idx)
402 #define LASSERT_REPSWABBED(rq, idx)
403 #endif
404
405 static inline const char *
406 ptlrpc_rqphase2str(struct ptlrpc_request *req)
407 {
408         switch (req->rq_phase) {
409         case RQ_PHASE_NEW:
410                 return "New";
411         case RQ_PHASE_RPC:
412                 return "Rpc";
413         case RQ_PHASE_BULK:
414                 return "Bulk";
415         case RQ_PHASE_INTERPRET:
416                 return "Interpret";
417         case RQ_PHASE_COMPLETE:
418                 return "Complete";
419         default:
420                 return "?Phase?";
421         }
422 }
423
424 /* Spare the preprocessor, spoil the bugs. */
425 #define FLAG(field, str) (field ? str : "")
426
427 #define DEBUG_REQ_FLAGS(req)                                                  \
428         ptlrpc_rqphase2str(req),                                              \
429         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                  \
430         FLAG(req->rq_err, "E"),                                               \
431         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"), \
432         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                \
433         FLAG(req->rq_no_resend, "N"),                                         \
434         FLAG(req->rq_waiting, "W")
435
436 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s"
437
438 void _debug_req(struct ptlrpc_request *req, __u32 mask,
439                 struct libcfs_debug_msg_data *data, const char *fmt, ...)
440         __attribute__ ((format (printf, 4, 5)));
441
442 #define debug_req(cdls, level, req, file, func, line, fmt, a...)              \
443 do {                                                                          \
444         CHECK_STACK();                                                        \
445                                                                               \
446         if (((level) & D_CANTMASK) != 0 ||                                    \
447             ((libcfs_debug & (level)) != 0 &&                                 \
448              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {              \
449                 static struct libcfs_debug_msg_data _req_dbg_data =           \
450                 DEBUG_MSG_DATA_INIT(cdls, DEBUG_SUBSYSTEM, file, func, line); \
451                 _debug_req((req), (level), &_req_dbg_data, fmt, ##a);         \
452         }                                                                     \
453 } while(0)
454
455 /* for most callers (level is a constant) this is resolved at compile time */
456 #define DEBUG_REQ(level, req, fmt, args...)                                   \
457 do {                                                                          \
458         if ((level) & (D_ERROR | D_WARNING)) {                                \
459             static cfs_debug_limit_state_t cdls;                              \
460             debug_req(&cdls, level, req, __FILE__, __func__, __LINE__,        \
461                       "@@@ "fmt" ", ## args);                                 \
462         } else                                                                \
463             debug_req(NULL, level, req, __FILE__, __func__, __LINE__,         \
464                       "@@@ "fmt" ", ## args);                                 \
465 } while (0)
466
467 struct ptlrpc_bulk_page {
468         struct list_head bp_link;
469         int bp_buflen;
470         int bp_pageoffset;                      /* offset within a page */
471         struct page *bp_page;
472 };
473
474 #define BULK_GET_SOURCE   0
475 #define BULK_PUT_SINK     1
476 #define BULK_GET_SINK     2
477 #define BULK_PUT_SOURCE   3
478
479 struct ptlrpc_bulk_desc {
480         unsigned int bd_success:1;              /* completed successfully */
481         unsigned int bd_network_rw:1;           /* accessible to the network */
482         unsigned int bd_type:2;                 /* {put,get}{source,sink} */
483         unsigned int bd_registered:1;           /* client side */
484         spinlock_t   bd_lock;                   /* serialise with callback */
485         int bd_import_generation;
486         struct obd_export *bd_export;
487         struct obd_import *bd_import;
488         __u32 bd_portal;
489         struct ptlrpc_request *bd_req;          /* associated request */
490         cfs_waitq_t            bd_waitq;        /* server side only WQ */
491         int                    bd_iov_count;    /* # entries in bd_iov */
492         int                    bd_max_iov;      /* allocated size of bd_iov */
493         int                    bd_nob;          /* # bytes covered */
494         int                    bd_nob_transferred; /* # bytes GOT/PUT */
495
496         __u64                  bd_last_xid;
497
498         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
499         lnet_handle_md_t       bd_md_h;         /* associated MD */
500         lnet_nid_t             bd_sender;       /* stash event::sender */
501
502 #if defined(__KERNEL__)
503         lnet_kiov_t             bd_iov[0];
504 #else
505         lnet_md_iovec_t         bd_iov[0];
506 #endif
507 };
508
509 struct ptlrpc_thread {
510
511         struct list_head t_link; /* active threads for service, from svc->srv_threads */
512
513         void *t_data;            /* thread-private data (preallocated memory) */
514         __u32 t_flags;
515
516         unsigned int t_id; /* service thread index, from ptlrpc_start_threads */
517         cfs_waitq_t t_ctl_waitq;
518 };
519
520 struct ptlrpc_request_buffer_desc {
521         struct list_head       rqbd_list;
522         struct list_head       rqbd_reqs;
523         struct ptlrpc_service *rqbd_service;
524         lnet_handle_md_t       rqbd_md_h;
525         int                    rqbd_refcount;
526         char                  *rqbd_buffer;
527         struct ptlrpc_cb_id    rqbd_cbid;
528         struct ptlrpc_request  rqbd_req;
529 };
530
531 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
532 typedef void (*svcreq_printfn_t)(void *, struct ptlrpc_request *);
533
534 struct ptlrpc_service {
535         struct list_head srv_list;              /* chain thru all services */
536         int              srv_max_req_size;      /* biggest request to receive */
537         int              srv_max_reply_size;    /* biggest reply to send */
538         int              srv_buf_size;          /* size of individual buffers */
539         int              srv_nbuf_per_group;    /* # buffers to allocate in 1 group */
540         int              srv_nbufs;             /* total # req buffer descs allocated */
541         int              srv_threads_min;       /* threads to start at SOW */
542         int              srv_threads_max;       /* thread upper limit */
543         int              srv_threads_started;   /* index of last started thread */
544         int              srv_threads_running;   /* # running threads */
545         int              srv_n_difficult_replies; /* # 'difficult' replies */
546         int              srv_n_active_reqs;     /* # reqs being served */
547         cfs_duration_t   srv_rqbd_timeout;      /* timeout before re-posting reqs, in tick */
548         int              srv_watchdog_factor;   /* soft watchdog timeout mutiplier */
549         unsigned         srv_cpu_affinity:1;    /* bind threads to CPUs */
550         unsigned         srv_at_check:1;        /* check early replies */
551
552         __u32            srv_req_portal;
553         __u32            srv_rep_portal;
554         
555         /* AT stuff */
556         struct adaptive_timeout srv_at_estimate;/* estimated service time */
557         spinlock_t        srv_at_lock;
558         struct list_head  srv_at_list;          /* reqs waiting for replies */
559         cfs_timer_t       srv_at_timer;         /* early reply timer */
560
561         int               srv_n_queued_reqs;    /* # reqs in either of the queues below */
562         struct list_head  srv_req_in_queue;     /* incoming reqs */
563         struct list_head  srv_request_queue;    /* reqs waiting for service */
564
565         struct list_head  srv_request_history;  /* request history */
566         __u64             srv_request_seq;      /* next request sequence # */
567         __u64             srv_request_max_cull_seq; /* highest seq culled from history */
568         svcreq_printfn_t  srv_request_history_print_fn; /* service-specific print fn */
569
570         struct list_head  srv_idle_rqbds;       /* request buffers to be reposted */
571         struct list_head  srv_active_rqbds;     /* req buffers receiving */
572         struct list_head  srv_history_rqbds;    /* request buffer history */
573         int               srv_nrqbd_receiving;  /* # posted request buffers */
574         int               srv_n_history_rqbds;  /* # request buffers in history */
575         int               srv_max_history_rqbds;/* max # request buffers in history */
576
577         atomic_t          srv_outstanding_replies;
578         struct list_head  srv_active_replies;   /* all the active replies */
579         struct list_head  srv_reply_queue;      /* replies waiting for service */
580
581         cfs_waitq_t       srv_waitq; /* all threads sleep on this. This
582                                       * wait-queue is signalled when new
583                                       * incoming request arrives and when
584                                       * difficult reply has to be handled. */
585
586         struct list_head   srv_threads;         /* service thread list */
587         svc_handler_t      srv_handler;
588
589         char *srv_name;  /* only statically allocated strings here; we don't clean them */
590         char *srv_thread_name;  /* only statically allocated strings here; we don't clean them */
591
592         spinlock_t               srv_lock;
593
594         cfs_proc_dir_entry_t    *srv_procroot;
595         struct lprocfs_stats    *srv_stats;
596
597         /* List of free reply_states */
598         struct list_head         srv_free_rs_list;
599         /* waitq to run, when adding stuff to srv_free_rs_list */
600         cfs_waitq_t              srv_free_rs_waitq;
601         
602         /*
603          * if non-NULL called during thread creation (ptlrpc_start_thread())
604          * to initialize service specific per-thread state.
605          */
606         int (*srv_init)(struct ptlrpc_thread *thread);
607         /*
608          * if non-NULL called during thread shutdown (ptlrpc_main()) to
609          * destruct state created by ->srv_init().
610          */
611         void (*srv_done)(struct ptlrpc_thread *thread);
612
613         //struct ptlrpc_srv_ni srv_interfaces[0];
614 };
615
616 /* ptlrpc/events.c */
617 extern lnet_handle_eq_t ptlrpc_eq_h;
618 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
619                                lnet_process_id_t *peer, lnet_nid_t *self);
620 extern void request_out_callback (lnet_event_t *ev);
621 extern void reply_in_callback(lnet_event_t *ev);
622 extern void client_bulk_callback (lnet_event_t *ev);
623 extern void request_in_callback(lnet_event_t *ev);
624 extern void reply_out_callback(lnet_event_t *ev);
625 extern void server_bulk_callback (lnet_event_t *ev);
626
627 /* ptlrpc/connection.c */
628 void ptlrpc_dump_connections(void);
629 void ptlrpc_readdress_connection(struct ptlrpc_connection *, struct obd_uuid *);
630 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
631                                                 lnet_nid_t self, struct obd_uuid *uuid);
632 int ptlrpc_put_connection(struct ptlrpc_connection *c);
633 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
634 int ptlrpc_init_connection(void);
635 void ptlrpc_cleanup_connection(void);
636 extern lnet_pid_t ptl_get_pid(void);
637
638 /* ptlrpc/niobuf.c */
639 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
640 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
641 int ptlrpc_register_bulk(struct ptlrpc_request *req);
642 void ptlrpc_unregister_bulk (struct ptlrpc_request *req);
643
644 static inline int ptlrpc_bulk_active (struct ptlrpc_bulk_desc *desc)
645 {
646         int           rc;
647
648         spin_lock(&desc->bd_lock);
649         rc = desc->bd_network_rw;
650         spin_unlock(&desc->bd_lock);
651         return (rc);
652 }
653
654 #define PTLRPC_REPLY_MAYBE_DIFFICULT 0x01
655 #define PTLRPC_REPLY_EARLY           0x02
656 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags);
657 int ptlrpc_reply(struct ptlrpc_request *req);
658 int ptlrpc_error(struct ptlrpc_request *req);
659 void ptlrpc_resend_req(struct ptlrpc_request *request);
660 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req);
661 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
662 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd);
663
664 /* ptlrpc/client.c */
665 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
666                         struct ptlrpc_client *);
667 void ptlrpc_cleanup_client(struct obd_import *imp);
668 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
669
670 static inline int
671 ptlrpc_client_recv_or_unlink (struct ptlrpc_request *req)
672 {
673         int           rc;
674
675         spin_lock(&req->rq_lock);
676         rc = req->rq_receiving_reply || req->rq_must_unlink;
677         spin_unlock(&req->rq_lock);
678         return (rc);
679 }
680
681 static inline void
682 ptlrpc_wake_client_req (struct ptlrpc_request *req)
683 {
684         if (req->rq_set == NULL)
685                 cfs_waitq_signal(&req->rq_reply_waitq);
686         else
687                 cfs_waitq_signal(&req->rq_set->set_waitq);
688 }
689
690 int ptlrpc_queue_wait(struct ptlrpc_request *req);
691 int ptlrpc_replay_req(struct ptlrpc_request *req);
692 void ptlrpc_unregister_reply(struct ptlrpc_request *req);
693 void ptlrpc_restart_req(struct ptlrpc_request *req);
694 void ptlrpc_abort_inflight(struct obd_import *imp);
695
696 struct ptlrpc_request_set *ptlrpc_prep_set(void);
697 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
698                       set_interpreter_func fn, void *data);
699 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
700 int ptlrpc_check_set(struct ptlrpc_request_set *set);
701 int ptlrpc_set_wait(struct ptlrpc_request_set *);
702 int ptlrpc_expired_set(void *data);
703 void ptlrpc_interrupted_set(void *data);
704 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
705 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
706 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
707 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *,
708                             struct ptlrpc_request *);
709
710 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
711 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
712 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int, int,
713                                                 void (*populate_pool)(struct ptlrpc_request_pool *, int));
714 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
715                                        int opcode, int count, int *lengths,
716                                        char **bufs);
717 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
718                                              __u32 version, int opcode,
719                                             int count, int *lengths, char **bufs,
720                                             struct ptlrpc_request_pool *pool);
721 void ptlrpc_free_req(struct ptlrpc_request *request);
722 void ptlrpc_req_finished(struct ptlrpc_request *request);
723 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
724 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
725 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
726                                                int npages, int type, int portal);
727 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
728                                               int npages, int type, int portal);
729 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
730 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
731                            cfs_page_t *page, int pageoffset, int len);
732 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
733                                       struct obd_import *imp);
734 __u64 ptlrpc_next_xid(void);
735 __u64 ptlrpc_sample_next_xid(void);
736 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
737
738 /* ptlrpc/service.c */
739 void ptlrpc_save_lock (struct ptlrpc_request *req,
740                        struct lustre_handle *lock, int mode);
741 void ptlrpc_commit_replies (struct obd_device *obd);
742 void ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs);
743 struct ptlrpc_service *ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
744                                        int max_reply_size,
745                                        int req_portal, int rep_portal,
746                                        int watchdog_factor,
747                                        svc_handler_t, char *name,
748                                        cfs_proc_dir_entry_t *proc_entry,
749                                        svcreq_printfn_t, 
750                                        int min_threads, int max_threads,
751                                        char *threadname);
752 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
753
754 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc);
755 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc);
756 int ptlrpc_unregister_service(struct ptlrpc_service *service);
757 int liblustre_check_services (void *arg);
758 void ptlrpc_daemonize(char *name);
759 int ptlrpc_service_health_check(struct ptlrpc_service *);
760
761
762 struct ptlrpc_svc_data {
763         char *name;
764         struct ptlrpc_service *svc;
765         struct ptlrpc_thread *thread;
766         struct obd_device *dev;
767 };
768
769 /* ptlrpc/import.c */
770 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid);
771 int ptlrpc_init_import(struct obd_import *imp);
772 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
773 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
774 void ptlrpc_import_setasync(struct obd_import *imp, int count);
775
776 /* ptlrpc/pack_generic.c */
777 int lustre_msg_swabbed(struct lustre_msg *msg);
778 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
779 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
780                         int *lens, char **bufs);
781 int lustre_pack_reply(struct ptlrpc_request *, int count, int *lens,
782                       char **bufs);
783 #define LPRFL_EARLY_REPLY 1
784 int lustre_pack_reply_flags(struct ptlrpc_request *, int count, int *lens,
785                             char **bufs, int flags);
786 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
787                          unsigned int newlen, int move_data);
788 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
789 int lustre_msg_size(__u32 magic, int count, int *lengths);
790 int lustre_msg_early_size(void);
791 int lustre_unpack_msg(struct lustre_msg *m, int len);
792 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
793 int lustre_msg_buflen(struct lustre_msg *m, int n);
794 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
795 int lustre_msg_bufcount(struct lustre_msg *m);
796 char *lustre_msg_string (struct lustre_msg *m, int n, int max_len);
797 void *lustre_swab_buf(struct lustre_msg *, int n, int minlen, void *swabber);
798 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int n, int minlen,
799                          void *swabber);
800 void *lustre_swab_repbuf(struct ptlrpc_request *req, int n, int minlen,
801                          void *swabber);
802 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
803 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
804 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
805 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
806 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
807 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
808 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
809 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
810 __u32 lustre_msg_get_type(struct lustre_msg *msg);
811 __u32 lustre_msg_get_version(struct lustre_msg *msg);
812 void lustre_msg_add_version(struct lustre_msg *msg, int version);
813 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
814 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
815 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
816 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
817 __u64 lustre_msg_get_slv(struct lustre_msg *msg);
818 __u32 lustre_msg_get_limit(struct lustre_msg *msg);
819 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv);
820 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit);
821 int   lustre_msg_get_status(struct lustre_msg *msg);
822 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
823 int lustre_msg_is_v1(struct lustre_msg *msg);
824 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
825 __u32 lustre_msg_get_timeout(struct lustre_msg *msg);
826 __u32 lustre_msg_get_service_time(struct lustre_msg *msg);
827 __u32 lustre_msg_get_cksum(struct lustre_msg *msg);
828 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg);
829 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
830 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
831 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
832 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
833 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
834 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
835 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
836 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
837 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout);
838 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time);
839 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum);
840
841 static inline void
842 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
843 {
844         LASSERT(atomic_read(&rs->rs_refcount) > 0);
845         atomic_inc(&rs->rs_refcount);
846 }
847
848 static inline void
849 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
850 {
851         LASSERT(atomic_read(&rs->rs_refcount) > 0);
852         if (atomic_dec_and_test(&rs->rs_refcount))
853                 lustre_free_reply_state(rs);
854 }
855
856 /* Should only be called once per req */
857 static inline void ptlrpc_req_drop_rs(struct ptlrpc_request *req)
858 {
859         if (req->rq_reply_state == NULL) 
860                 return; /* shouldn't occur */
861         ptlrpc_rs_decref(req->rq_reply_state);
862         req->rq_reply_state = NULL;
863         req->rq_repmsg = NULL;
864 }
865
866 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
867 {
868         return lustre_msg_get_magic(req->rq_reqmsg);
869 }
870
871 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
872 {
873         switch (req->rq_reqmsg->lm_magic) {
874         case LUSTRE_MSG_MAGIC_V1:
875                 CERROR("function not supported for lustre_msg V1!\n");
876                 return -ENOTSUPP;
877         case LUSTRE_MSG_MAGIC_V2:
878                 return req->rq_reqmsg->lm_repsize;
879         default:
880                 LASSERTF(0, "incorrect message magic: %08x\n",
881                          req->rq_reqmsg->lm_magic);
882                 return -EFAULT;
883         }
884 }
885
886 static inline void
887 ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, int *lens)
888 {
889         int size = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
890         
891         req->rq_replen = size + lustre_msg_early_size();
892         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
893                 req->rq_reqmsg->lm_repsize = size;
894 }
895
896 /* ldlm/ldlm_lib.c */
897 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
898 int client_obd_cleanup(struct obd_device * obddev);
899 int client_connect_import(struct lustre_handle *conn, struct obd_device *obd,
900                           struct obd_uuid *cluuid, struct obd_connect_data *);
901 int client_disconnect_export(struct obd_export *exp);
902 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
903                            int priority);
904 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
905 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
906
907 /* ptlrpc/pinger.c */
908 int ptlrpc_pinger_add_import(struct obd_import *imp);
909 int ptlrpc_pinger_del_import(struct obd_import *imp);
910 #ifdef __KERNEL__
911 void ping_evictor_start(void);
912 void ping_evictor_stop(void);
913 #else
914 #define ping_evictor_start()    do {} while (0)
915 #define ping_evictor_stop()     do {} while (0)
916 #endif
917
918 /* ptlrpc/ptlrpcd.c */
919 void ptlrpcd_wake(struct ptlrpc_request *req);
920 void ptlrpcd_add_req(struct ptlrpc_request *req);
921 int ptlrpcd_addref(void);
922 void ptlrpcd_decref(void);
923
924 /* ptlrpc/lproc_ptlrpc.c */
925 const char* ll_opcode2str(__u32 opcode);
926 #ifdef LPROCFS
927 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
928 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
929 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes);
930 #else
931 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
932 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
933 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc,
934                                       int bytes) {}
935 #endif
936
937 /* ptlrpc/llog_server.c */
938 int llog_origin_handle_create(struct ptlrpc_request *req);
939 int llog_origin_handle_destroy(struct ptlrpc_request *req);
940 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
941 int llog_origin_handle_next_block(struct ptlrpc_request *req);
942 int llog_origin_handle_read_header(struct ptlrpc_request *req);
943 int llog_origin_handle_close(struct ptlrpc_request *req);
944 int llog_origin_handle_cancel(struct ptlrpc_request *req);
945 int llog_catinfo(struct ptlrpc_request *req);
946
947 /* ptlrpc/llog_client.c */
948 extern struct llog_operations llog_client_ops;
949
950 #endif