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         void              *set_countp; /* pointer to NOB counter in case 
188                                         * of directIO (bug11737) */
189         /* locked so that any old caller can communicate requests to
190          * the set holder who can then fold them into the lock-free set */
191         spinlock_t        set_new_req_lock;
192         struct list_head  set_new_requests;
193 };
194
195 struct ptlrpc_set_cbdata {
196         struct list_head        psc_item;
197         set_interpreter_func    psc_interpret;
198         void                   *psc_data;
199 };
200
201 struct ptlrpc_bulk_desc;
202
203 /*
204  * ptlrpc callback & work item stuff
205  */
206 struct ptlrpc_cb_id {
207         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
208         void    *cbid_arg;                      /* additional arg */
209 };
210
211 #define RS_MAX_LOCKS 4
212 #define RS_DEBUG     1
213
214 struct ptlrpc_reply_state {
215         struct ptlrpc_cb_id    rs_cb_id;
216         struct list_head       rs_list;
217         struct list_head       rs_exp_list;
218         struct list_head       rs_obd_list;
219 #if RS_DEBUG
220         struct list_head       rs_debug_list;
221 #endif
222         /* updates to following flag serialised by srv_request_lock */
223         unsigned int           rs_difficult:1;     /* ACK/commit stuff */
224         unsigned int           rs_scheduled:1;     /* being handled? */
225         unsigned int           rs_scheduled_ever:1;/* any schedule attempts? */
226         unsigned int           rs_handled:1;  /* been handled yet? */
227         unsigned int           rs_on_net:1;   /* reply_out_callback pending? */
228         unsigned int           rs_prealloc:1; /* rs from prealloc list */
229
230         int                    rs_size;
231         __u64                  rs_transno;
232         __u64                  rs_xid;
233         struct obd_export     *rs_export;
234         struct ptlrpc_service *rs_service;
235         lnet_handle_md_t       rs_md_h;
236         atomic_t               rs_refcount;
237
238         /* locks awaiting client reply ACK */
239         int                    rs_nlocks;
240         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
241         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
242         /* last member: variable sized reply message */
243         struct lustre_msg     *rs_msg;
244 };
245
246 struct ptlrpc_thread;
247
248 enum rq_phase {
249         RQ_PHASE_NEW         = 0xebc0de00,
250         RQ_PHASE_RPC         = 0xebc0de01,
251         RQ_PHASE_BULK        = 0xebc0de02,
252         RQ_PHASE_INTERPRET   = 0xebc0de03,
253         RQ_PHASE_COMPLETE    = 0xebc0de04,
254 };
255
256 struct ptlrpc_request_pool {
257         spinlock_t prp_lock;
258         struct list_head prp_req_list;    /* list of ptlrpc_request structs */
259         int prp_rq_size;
260         void (*prp_populate)(struct ptlrpc_request_pool *, int);
261 };
262
263 struct ptlrpc_request {
264         int rq_type; /* one of PTL_RPC_MSG_* */
265         struct list_head rq_list;
266         struct list_head rq_timed_list;         /* server-side early replies */
267         struct list_head rq_history_list;       /* server-side history */
268         __u64            rq_history_seq;        /* history sequence # */
269         int rq_status;
270         spinlock_t rq_lock;
271         /* client-side flags are serialized by rq_lock */
272         unsigned int rq_intr:1, rq_replied:1, rq_err:1,
273                 rq_timedout:1, rq_resend:1, rq_restart:1,
274                 /*
275                  * when ->rq_replay is set, request is kept by the client even
276                  * after server commits corresponding transaction. This is
277                  * used for operations that require sequence of multiple
278                  * requests to be replayed. The only example currently is file
279                  * open/close. When last request in such a sequence is
280                  * committed, ->rq_replay is cleared on all requests in the
281                  * sequence.
282                  */
283                 rq_replay:1,
284                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
285                 rq_no_delay:1, rq_net_err:1, rq_early:1, rq_must_unlink:1,
286                 /* server-side flags */
287                 rq_packed_final:1,  /* packed final reply */
288                 rq_sent_final:1;    /* stop sending early replies */
289         enum rq_phase rq_phase; /* one of RQ_PHASE_* */
290         atomic_t rq_refcount;   /* client-side refcount for SENT race,
291                                    server-side refcounf for multiple replies */
292
293         struct ptlrpc_thread *rq_svc_thread; /* initial thread servicing req */
294
295         int rq_request_portal;  /* XXX FIXME bug 249 */
296         int rq_reply_portal;    /* XXX FIXME bug 249 */
297
298         int rq_nob_received; /* client-side # reply bytes actually received  */
299
300         int rq_reqlen;
301         struct lustre_msg *rq_reqmsg;
302
303         int rq_replen;
304         struct lustre_msg *rq_repbuf; /* client only, buf may be bigger than msg */
305         struct lustre_msg *rq_repmsg;
306         __u64 rq_transno;
307         __u64 rq_xid;
308         struct list_head rq_replay_list;
309
310         __u32 rq_req_swab_mask;
311         __u32 rq_rep_swab_mask;
312
313         int rq_import_generation;
314         enum lustre_imp_state rq_send_state;
315
316         int rq_early_count;           /* how many early replies (for stats) */
317
318         /* client+server request */
319         lnet_handle_md_t     rq_req_md_h;
320         struct ptlrpc_cb_id  rq_req_cbid;
321
322         /* server-side... */
323         struct timeval       rq_arrival_time;       /* request arrival time */
324         struct ptlrpc_reply_state *rq_reply_state;  /* separated reply state */
325         struct ptlrpc_request_buffer_desc *rq_rqbd; /* incoming request buffer*/
326 #ifdef CRAY_XT3
327         __u32                rq_uid;            /* peer uid, used in MDS only */
328 #endif
329
330         /* client-only incoming reply */
331         lnet_handle_md_t     rq_reply_md_h;
332         cfs_waitq_t          rq_reply_waitq;
333         struct ptlrpc_cb_id  rq_reply_cbid;
334
335         lnet_nid_t           rq_self;
336         lnet_process_id_t    rq_peer;
337         struct obd_export   *rq_export;
338         struct obd_import   *rq_import;
339
340         void (*rq_replay_cb)(struct ptlrpc_request *);
341         void (*rq_commit_cb)(struct ptlrpc_request *);
342         void  *rq_cb_data;
343
344         struct ptlrpc_bulk_desc *rq_bulk;       /* client side bulk */
345         /* client outgoing req */
346         time_t rq_sent;                         /* when request sent, seconds, 
347                                                  * or time when request should
348                                                  * be sent */
349         volatile time_t rq_deadline;     /* when request must finish. volatile
350                so that servers' early reply updates to the deadline aren't 
351                kept in per-cpu cache */
352         int    rq_timeout;               /* service time estimate (secs) */
353
354         /* Multi-rpc bits */
355         struct list_head rq_set_chain;
356         struct ptlrpc_request_set *rq_set;
357         void *rq_interpret_reply;               /* Async completion handler */
358         union ptlrpc_async_args rq_async_args;  /* Async completion context */
359         struct ptlrpc_request_pool *rq_pool;    /* Pool if request from
360                                                    preallocated list */
361 };
362
363 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
364 {
365         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
366         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
367         req->rq_req_swab_mask |= 1 << index;
368 }
369
370 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
371 {
372         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
373         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
374         req->rq_rep_swab_mask |= 1 << index;
375 }
376
377 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
378
379         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
380         return req->rq_req_swab_mask & (1 << index);
381 }
382
383 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
384 {
385         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
386         return req->rq_rep_swab_mask & (1 << index);
387 }
388
389 static inline const char *
390 ptlrpc_rqphase2str(struct ptlrpc_request *req)
391 {
392         switch (req->rq_phase) {
393         case RQ_PHASE_NEW:
394                 return "New";
395         case RQ_PHASE_RPC:
396                 return "Rpc";
397         case RQ_PHASE_BULK:
398                 return "Bulk";
399         case RQ_PHASE_INTERPRET:
400                 return "Interpret";
401         case RQ_PHASE_COMPLETE:
402                 return "Complete";
403         default:
404                 return "?Phase?";
405         }
406 }
407
408 /* Spare the preprocessor, spoil the bugs. */
409 #define FLAG(field, str) (field ? str : "")
410
411 #define DEBUG_REQ_FLAGS(req)                                                  \
412         ptlrpc_rqphase2str(req),                                              \
413         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                  \
414         FLAG(req->rq_err, "E"),                                               \
415         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"), \
416         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                \
417         FLAG(req->rq_no_resend, "N"),                                         \
418         FLAG(req->rq_waiting, "W")
419
420 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s"
421
422 void _debug_req(struct ptlrpc_request *req, __u32 mask,
423                 struct libcfs_debug_msg_data *data, const char *fmt, ...)
424         __attribute__ ((format (printf, 4, 5)));
425
426 #define debug_req(cdls, level, req, file, func, line, fmt, a...)              \
427 do {                                                                          \
428         CHECK_STACK();                                                        \
429                                                                               \
430         if (((level) & D_CANTMASK) != 0 ||                                    \
431             ((libcfs_debug & (level)) != 0 &&                                 \
432              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {              \
433                 static struct libcfs_debug_msg_data _req_dbg_data =           \
434                 DEBUG_MSG_DATA_INIT(cdls, DEBUG_SUBSYSTEM, file, func, line); \
435                 _debug_req((req), (level), &_req_dbg_data, fmt, ##a);         \
436         }                                                                     \
437 } while(0)
438
439 /* for most callers (level is a constant) this is resolved at compile time */
440 #define DEBUG_REQ(level, req, fmt, args...)                                   \
441 do {                                                                          \
442         if ((level) & (D_ERROR | D_WARNING)) {                                \
443             static cfs_debug_limit_state_t cdls;                              \
444             debug_req(&cdls, level, req, __FILE__, __func__, __LINE__,        \
445                       "@@@ "fmt" ", ## args);                                 \
446         } else                                                                \
447             debug_req(NULL, level, req, __FILE__, __func__, __LINE__,         \
448                       "@@@ "fmt" ", ## args);                                 \
449 } while (0)
450
451 struct ptlrpc_bulk_page {
452         struct list_head bp_link;
453         int bp_buflen;
454         int bp_pageoffset;                      /* offset within a page */
455         struct page *bp_page;
456 };
457
458 #define BULK_GET_SOURCE   0
459 #define BULK_PUT_SINK     1
460 #define BULK_GET_SINK     2
461 #define BULK_PUT_SOURCE   3
462
463 struct ptlrpc_bulk_desc {
464         unsigned int bd_success:1;              /* completed successfully */
465         unsigned int bd_network_rw:1;           /* accessible to the network */
466         unsigned int bd_type:2;                 /* {put,get}{source,sink} */
467         unsigned int bd_registered:1;           /* client side */
468         spinlock_t   bd_lock;                   /* serialise with callback */
469         int bd_import_generation;
470         struct obd_export *bd_export;
471         struct obd_import *bd_import;
472         __u32 bd_portal;
473         struct ptlrpc_request *bd_req;          /* associated request */
474         cfs_waitq_t            bd_waitq;        /* server side only WQ */
475         int                    bd_iov_count;    /* # entries in bd_iov */
476         int                    bd_max_iov;      /* allocated size of bd_iov */
477         int                    bd_nob;          /* # bytes covered */
478         int                    bd_nob_transferred; /* # bytes GOT/PUT */
479
480         __u64                  bd_last_xid;
481
482         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
483         lnet_handle_md_t       bd_md_h;         /* associated MD */
484         lnet_nid_t             bd_sender;       /* stash event::sender */
485
486 #if defined(__KERNEL__)
487         lnet_kiov_t             bd_iov[0];
488 #else
489         lnet_md_iovec_t         bd_iov[0];
490 #endif
491 };
492
493 struct ptlrpc_thread {
494
495         struct list_head t_link; /* active threads for service, from svc->srv_threads */
496
497         void *t_data;            /* thread-private data (preallocated memory) */
498         __u32 t_flags;
499
500         unsigned int t_id; /* service thread index, from ptlrpc_start_threads */
501         cfs_waitq_t t_ctl_waitq;
502 };
503
504 struct ptlrpc_request_buffer_desc {
505         struct list_head       rqbd_list;
506         struct list_head       rqbd_reqs;
507         struct ptlrpc_service *rqbd_service;
508         lnet_handle_md_t       rqbd_md_h;
509         int                    rqbd_refcount;
510         char                  *rqbd_buffer;
511         struct ptlrpc_cb_id    rqbd_cbid;
512         struct ptlrpc_request  rqbd_req;
513 };
514
515 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
516 typedef void (*svcreq_printfn_t)(void *, struct ptlrpc_request *);
517
518 struct ptlrpc_service {
519         struct list_head srv_list;              /* chain thru all services */
520         int              srv_max_req_size;      /* biggest request to receive */
521         int              srv_max_reply_size;    /* biggest reply to send */
522         int              srv_buf_size;          /* size of individual buffers */
523         int              srv_nbuf_per_group;    /* # buffers to allocate in 1 group */
524         int              srv_nbufs;             /* total # req buffer descs allocated */
525         int              srv_threads_min;       /* threads to start at SOW */
526         int              srv_threads_max;       /* thread upper limit */
527         int              srv_threads_started;   /* index of last started thread */
528         int              srv_threads_running;   /* # running threads */
529         int              srv_n_difficult_replies; /* # 'difficult' replies */
530         int              srv_n_active_reqs;     /* # reqs being served */
531         cfs_duration_t   srv_rqbd_timeout;      /* timeout before re-posting reqs, in tick */
532         int              srv_watchdog_factor;   /* soft watchdog timeout mutiplier */
533         unsigned         srv_cpu_affinity:1;    /* bind threads to CPUs */
534         unsigned         srv_at_check:1;        /* check early replies */
535
536         __u32            srv_req_portal;
537         __u32            srv_rep_portal;
538         
539         /* AT stuff */
540         struct adaptive_timeout srv_at_estimate;/* estimated rpc service time */
541         spinlock_t        srv_at_lock;
542         struct list_head  srv_at_list;          /* reqs waiting for replies */
543         cfs_timer_t       srv_at_timer;         /* early reply timer */
544
545         int               srv_n_queued_reqs;    /* # reqs in either of the queues below */
546         struct list_head  srv_req_in_queue;     /* incoming reqs */
547         struct list_head  srv_request_queue;    /* reqs waiting for service */
548
549         struct list_head  srv_request_history;  /* request history */
550         __u64             srv_request_seq;      /* next request sequence # */
551         __u64             srv_request_max_cull_seq; /* highest seq culled from history */
552         svcreq_printfn_t  srv_request_history_print_fn; /* service-specific print fn */
553
554         struct list_head  srv_idle_rqbds;       /* request buffers to be reposted */
555         struct list_head  srv_active_rqbds;     /* req buffers receiving */
556         struct list_head  srv_history_rqbds;    /* request buffer history */
557         int               srv_nrqbd_receiving;  /* # posted request buffers */
558         int               srv_n_history_rqbds;  /* # request buffers in history */
559         int               srv_max_history_rqbds;/* max # request buffers in history */
560
561         atomic_t          srv_outstanding_replies;
562         struct list_head  srv_active_replies;   /* all the active replies */
563         struct list_head  srv_reply_queue;      /* replies waiting for service */
564
565         cfs_waitq_t       srv_waitq; /* all threads sleep on this. This
566                                       * wait-queue is signalled when new
567                                       * incoming request arrives and when
568                                       * difficult reply has to be handled. */
569
570         struct list_head   srv_threads;         /* service thread list */
571         svc_handler_t      srv_handler;
572
573         char *srv_name;  /* only statically allocated strings here; we don't clean them */
574         char *srv_thread_name;  /* only statically allocated strings here; we don't clean them */
575
576         spinlock_t               srv_lock;
577
578         cfs_proc_dir_entry_t    *srv_procroot;
579         struct lprocfs_stats    *srv_stats;
580
581         /* List of free reply_states */
582         struct list_head         srv_free_rs_list;
583         /* waitq to run, when adding stuff to srv_free_rs_list */
584         cfs_waitq_t              srv_free_rs_waitq;
585         
586         /*
587          * if non-NULL called during thread creation (ptlrpc_start_thread())
588          * to initialize service specific per-thread state.
589          */
590         int (*srv_init)(struct ptlrpc_thread *thread);
591         /*
592          * if non-NULL called during thread shutdown (ptlrpc_main()) to
593          * destruct state created by ->srv_init().
594          */
595         void (*srv_done)(struct ptlrpc_thread *thread);
596
597         //struct ptlrpc_srv_ni srv_interfaces[0];
598 };
599
600 /* ptlrpc/events.c */
601 extern lnet_handle_eq_t ptlrpc_eq_h;
602 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
603                                lnet_process_id_t *peer, lnet_nid_t *self);
604 extern void request_out_callback (lnet_event_t *ev);
605 extern void reply_in_callback(lnet_event_t *ev);
606 extern void client_bulk_callback (lnet_event_t *ev);
607 extern void request_in_callback(lnet_event_t *ev);
608 extern void reply_out_callback(lnet_event_t *ev);
609 extern void server_bulk_callback (lnet_event_t *ev);
610
611 /* ptlrpc/connection.c */
612 void ptlrpc_dump_connections(void);
613 void ptlrpc_readdress_connection(struct ptlrpc_connection *, struct obd_uuid *);
614 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
615                                                 lnet_nid_t self, struct obd_uuid *uuid);
616 int ptlrpc_put_connection(struct ptlrpc_connection *c);
617 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
618 int ptlrpc_init_connection(void);
619 void ptlrpc_cleanup_connection(void);
620 extern lnet_pid_t ptl_get_pid(void);
621
622 /* ptlrpc/niobuf.c */
623 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
624 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
625 int ptlrpc_register_bulk(struct ptlrpc_request *req);
626 void ptlrpc_unregister_bulk (struct ptlrpc_request *req);
627
628 static inline int ptlrpc_bulk_active (struct ptlrpc_bulk_desc *desc)
629 {
630         int           rc;
631
632         spin_lock(&desc->bd_lock);
633         rc = desc->bd_network_rw;
634         spin_unlock(&desc->bd_lock);
635         return (rc);
636 }
637
638 #define PTLRPC_REPLY_MAYBE_DIFFICULT 0x01
639 #define PTLRPC_REPLY_EARLY           0x02
640 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags);
641 int ptlrpc_reply(struct ptlrpc_request *req);
642 int ptlrpc_error(struct ptlrpc_request *req);
643 void ptlrpc_resend_req(struct ptlrpc_request *request);
644 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req);
645 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
646 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd);
647
648 /* ptlrpc/client.c */
649 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
650                         struct ptlrpc_client *);
651 void ptlrpc_cleanup_client(struct obd_import *imp);
652 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
653
654 static inline int
655 ptlrpc_client_recv_or_unlink (struct ptlrpc_request *req)
656 {
657         int           rc;
658
659         spin_lock(&req->rq_lock);
660         rc = req->rq_receiving_reply || req->rq_must_unlink;
661         spin_unlock(&req->rq_lock);
662         return (rc);
663 }
664
665 static inline void
666 ptlrpc_wake_client_req (struct ptlrpc_request *req)
667 {
668         if (req->rq_set == NULL)
669                 cfs_waitq_signal(&req->rq_reply_waitq);
670         else
671                 cfs_waitq_signal(&req->rq_set->set_waitq);
672 }
673
674 int ptlrpc_queue_wait(struct ptlrpc_request *req);
675 int ptlrpc_replay_req(struct ptlrpc_request *req);
676 void ptlrpc_unregister_reply(struct ptlrpc_request *req);
677 void ptlrpc_restart_req(struct ptlrpc_request *req);
678 void ptlrpc_abort_inflight(struct obd_import *imp);
679
680 struct ptlrpc_request_set *ptlrpc_prep_set(void);
681 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
682                       set_interpreter_func fn, void *data);
683 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
684 int ptlrpc_check_set(struct ptlrpc_request_set *set);
685 int ptlrpc_set_wait(struct ptlrpc_request_set *);
686 int ptlrpc_expired_set(void *data);
687 void ptlrpc_interrupted_set(void *data);
688 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
689 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
690 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
691 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *,
692                             struct ptlrpc_request *);
693
694 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
695 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
696 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int, int,
697                                                 void (*populate_pool)(struct ptlrpc_request_pool *, int));
698 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req);
699 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
700                                        int opcode, int count, int *lengths,
701                                        char **bufs);
702 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
703                                              __u32 version, int opcode,
704                                             int count, int *lengths, char **bufs,
705                                             struct ptlrpc_request_pool *pool);
706 void ptlrpc_free_req(struct ptlrpc_request *request);
707 void ptlrpc_req_finished(struct ptlrpc_request *request);
708 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
709 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
710 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
711                                                int npages, int type, int portal);
712 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
713                                               int npages, int type, int portal);
714 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
715 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
716                            cfs_page_t *page, int pageoffset, int len);
717 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
718                                       struct obd_import *imp);
719 __u64 ptlrpc_next_xid(void);
720 __u64 ptlrpc_sample_next_xid(void);
721 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
722
723 /* ptlrpc/service.c */
724 void ptlrpc_save_lock (struct ptlrpc_request *req,
725                        struct lustre_handle *lock, int mode);
726 void ptlrpc_commit_replies (struct obd_device *obd);
727 void ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs);
728 struct ptlrpc_service *ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
729                                        int max_reply_size,
730                                        int req_portal, int rep_portal,
731                                        int watchdog_factor,
732                                        svc_handler_t, char *name,
733                                        cfs_proc_dir_entry_t *proc_entry,
734                                        svcreq_printfn_t, 
735                                        int min_threads, int max_threads,
736                                        char *threadname);
737 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
738
739 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc);
740 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc);
741 int ptlrpc_unregister_service(struct ptlrpc_service *service);
742 int liblustre_check_services (void *arg);
743 void ptlrpc_daemonize(char *name);
744 int ptlrpc_service_health_check(struct ptlrpc_service *);
745
746
747 struct ptlrpc_svc_data {
748         char *name;
749         struct ptlrpc_service *svc;
750         struct ptlrpc_thread *thread;
751         struct obd_device *dev;
752 };
753
754 /* ptlrpc/import.c */
755 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid);
756 int ptlrpc_init_import(struct obd_import *imp);
757 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
758 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
759 void ptlrpc_import_setasync(struct obd_import *imp, int count);
760
761 /* ptlrpc/pack_generic.c */
762 int lustre_msg_swabbed(struct lustre_msg *msg);
763 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
764 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
765                         int *lens, char **bufs);
766 int lustre_pack_reply(struct ptlrpc_request *, int count, int *lens,
767                       char **bufs);
768 #define LPRFL_EARLY_REPLY 1
769 int lustre_pack_reply_flags(struct ptlrpc_request *, int count, int *lens,
770                             char **bufs, int flags);
771 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
772                          unsigned int newlen, int move_data);
773 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
774 int lustre_msg_size(__u32 magic, int count, int *lengths);
775 int lustre_msg_early_size(void);
776 int lustre_unpack_msg(struct lustre_msg *m, int len);
777 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
778 int lustre_msg_buflen(struct lustre_msg *m, int n);
779 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
780 int lustre_msg_bufcount(struct lustre_msg *m);
781 char *lustre_msg_string (struct lustre_msg *m, int n, int max_len);
782 void *lustre_swab_buf(struct lustre_msg *, int n, int minlen, void *swabber);
783 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int n, int minlen,
784                          void *swabber);
785 void *lustre_swab_repbuf(struct ptlrpc_request *req, int n, int minlen,
786                          void *swabber);
787 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
788 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
789 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
790 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
791 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
792 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
793 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
794 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
795 __u32 lustre_msg_get_type(struct lustre_msg *msg);
796 __u32 lustre_msg_get_version(struct lustre_msg *msg);
797 void lustre_msg_add_version(struct lustre_msg *msg, int version);
798 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
799 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
800 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
801 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
802 __u64 lustre_msg_get_slv(struct lustre_msg *msg);
803 __u32 lustre_msg_get_limit(struct lustre_msg *msg);
804 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv);
805 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit);
806 int   lustre_msg_get_status(struct lustre_msg *msg);
807 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
808 int lustre_msg_is_v1(struct lustre_msg *msg);
809 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
810 __u32 lustre_msg_get_timeout(struct lustre_msg *msg);
811 __u32 lustre_msg_get_service_time(struct lustre_msg *msg);
812 __u32 lustre_msg_get_cksum(struct lustre_msg *msg);
813 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg);
814 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
815 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
816 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
817 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
818 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
819 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
820 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
821 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
822 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout);
823 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time);
824 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum);
825
826 static inline void
827 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
828 {
829         LASSERT(atomic_read(&rs->rs_refcount) > 0);
830         atomic_inc(&rs->rs_refcount);
831 }
832
833 static inline void
834 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
835 {
836         LASSERT(atomic_read(&rs->rs_refcount) > 0);
837         if (atomic_dec_and_test(&rs->rs_refcount))
838                 lustre_free_reply_state(rs);
839 }
840
841 /* Should only be called once per req */
842 static inline void ptlrpc_req_drop_rs(struct ptlrpc_request *req)
843 {
844         if (req->rq_reply_state == NULL) 
845                 return; /* shouldn't occur */
846         ptlrpc_rs_decref(req->rq_reply_state);
847         req->rq_reply_state = NULL;
848         req->rq_repmsg = NULL;
849 }
850
851 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
852 {
853         return lustre_msg_get_magic(req->rq_reqmsg);
854 }
855
856 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
857 {
858         switch (req->rq_reqmsg->lm_magic) {
859         case LUSTRE_MSG_MAGIC_V1:
860                 CERROR("function not supported for lustre_msg V1!\n");
861                 return -ENOTSUPP;
862         case LUSTRE_MSG_MAGIC_V2:
863                 return req->rq_reqmsg->lm_repsize;
864         default:
865                 LASSERTF(0, "incorrect message magic: %08x\n",
866                          req->rq_reqmsg->lm_magic);
867                 return -EFAULT;
868         }
869 }
870
871 static inline void
872 ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, int *lens)
873 {
874         int size = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
875         
876         req->rq_replen = size + lustre_msg_early_size();
877         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
878                 req->rq_reqmsg->lm_repsize = size;
879 }
880
881 /* ldlm/ldlm_lib.c */
882 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
883 int client_obd_cleanup(struct obd_device * obddev);
884 int client_connect_import(struct lustre_handle *conn, struct obd_device *obd,
885                           struct obd_uuid *cluuid, struct obd_connect_data *,
886                           void *localdata);
887 int client_disconnect_export(struct obd_export *exp);
888 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
889                            int priority);
890 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
891 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
892
893 /* ptlrpc/pinger.c */
894 int ptlrpc_pinger_add_import(struct obd_import *imp);
895 int ptlrpc_pinger_del_import(struct obd_import *imp);
896 #ifdef __KERNEL__
897 void ping_evictor_start(void);
898 void ping_evictor_stop(void);
899 #else
900 #define ping_evictor_start()    do {} while (0)
901 #define ping_evictor_stop()     do {} while (0)
902 #endif
903
904 /* ptlrpc/ptlrpcd.c */
905 void ptlrpcd_wake(struct ptlrpc_request *req);
906 void ptlrpcd_add_req(struct ptlrpc_request *req);
907 int ptlrpcd_addref(void);
908 void ptlrpcd_decref(void);
909
910 /* ptlrpc/lproc_ptlrpc.c */
911 const char* ll_opcode2str(__u32 opcode);
912 #ifdef LPROCFS
913 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
914 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
915 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes);
916 #else
917 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
918 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
919 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc,
920                                       int bytes) {}
921 #endif
922
923 /* ptlrpc/llog_server.c */
924 int llog_origin_handle_create(struct ptlrpc_request *req);
925 int llog_origin_handle_destroy(struct ptlrpc_request *req);
926 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
927 int llog_origin_handle_next_block(struct ptlrpc_request *req);
928 int llog_origin_handle_read_header(struct ptlrpc_request *req);
929 int llog_origin_handle_close(struct ptlrpc_request *req);
930 int llog_origin_handle_cancel(struct ptlrpc_request *req);
931 int llog_catinfo(struct ptlrpc_request *req);
932
933 /* ptlrpc/llog_client.c */
934 extern struct llog_operations llog_client_ops;
935
936 #endif