Whamcloud - gitweb
land b1_5 onto HEAD
[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 min((int)(smp_num_cpus * smp_num_cpus * 2), 8)
89 #define LDLM_THREADS_AUTO_MAX (LDLM_THREADS_AUTO_MIN * 16)
90 #define LDLM_BL_THREADS  LDLM_THREADS_AUTO_MIN
91 #define LDLM_NBUFS      (64 * smp_num_cpus)
92 #define LDLM_BUFSIZE    (8 * 1024)
93 #define LDLM_MAXREQSIZE (5 * 1024)
94 #define LDLM_MAXREPSIZE (1024)
95
96 /* Absolute limits */
97 #define MDS_THREADS_MIN 2UL
98 #define MDS_THREADS_MAX 512UL
99 /* Dynamic thread limits */
100 #define MDS_THREADS_AUTO_MIN max(MDS_THREADS_MIN, min(32UL, \
101                              smp_num_cpus * num_physpages >> (27 - CFS_PAGE_SHIFT)))
102 #define MDS_THREADS_AUTO_MAX min(MDS_THREADS_MAX, MDS_THREADS_AUTO_MIN * 4)
103 #define MDS_THREADS_MIN_READPAGE 2
104 #define MDS_NBUFS       (64 * smp_num_cpus)
105 #define MDS_BUFSIZE     (8 * 1024)
106 /* Assume file name length = FNAME_MAX = 256 (true for ext3).
107  *        path name length = PATH_MAX = 4096
108  *        LOV MD size max  = EA_MAX = 4000
109  * symlink:  FNAME_MAX + PATH_MAX  <- largest
110  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
111  * rename:   FNAME_MAX + FNAME_MAX
112  * open:     FNAME_MAX + EA_MAX
113  *
114  * MDS_MAXREQSIZE ~= 4736 bytes =
115  * lustre_msg + ldlm_request + mds_body + mds_rec_create + FNAME_MAX + PATH_MAX
116  * MDS_MAXREPSIZE ~= 8300 bytes = lustre_msg + llog_header
117  * or, for mds_close() and mds_reint_unlink() on a many-OST filesystem:
118  *      = 9210 bytes = lustre_msg + mds_body + 160 * (easize + cookiesize)
119  *
120  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
121  * except in the open case where there are a large number of OSTs in a LOV.
122  */
123 #define MDS_MAXREQSIZE  (5 * 1024)
124 #define MDS_MAXREPSIZE  max(9 * 1024, 280 + LOV_MAX_STRIPE_COUNT * 56)
125
126 #define MGS_THREADS_AUTO_MAX 128UL
127 #define MGS_THREADS_AUTO_MIN MDS_THREADS_AUTO_MIN
128 #define MGS_NBUFS       (64 * smp_num_cpus)
129 #define MGS_BUFSIZE     (8 * 1024)
130 #define MGS_MAXREQSIZE  (8 * 1024)
131 #define MGS_MAXREPSIZE  (9 * 1024)
132
133 /* Absolute limits */
134 #define OSS_THREADS_MIN 2UL
135 #define OSS_THREADS_MAX 512UL
136 /* Dynamic thread limits */
137 #define OSS_THREADS_AUTO_MIN max(OSS_THREADS_MIN, \
138                              smp_num_cpus * num_physpages >> (27 - CFS_PAGE_SHIFT))
139 #define OSS_THREADS_AUTO_MAX min(OSS_THREADS_MAX, OSS_THREADS_AUTO_MIN * 4)
140 #define OST_NBUFS       (64 * smp_num_cpus)
141 #define OST_BUFSIZE     (8 * 1024)
142 /* OST_MAXREQSIZE ~= 4768 bytes =
143  * lustre_msg + obdo + 16 * obd_ioobj + 256 * niobuf_remote
144  *
145  * - single object with 16 pages is 512 bytes
146  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
147  */
148 #define OST_MAXREQSIZE  (5 * 1024)
149 #define OST_MAXREPSIZE  (9 * 1024)
150
151 struct ptlrpc_connection {
152         struct list_head        c_link;
153         lnet_nid_t              c_self;
154         lnet_process_id_t       c_peer;
155         struct obd_uuid         c_remote_uuid;
156         atomic_t                c_refcount;
157 };
158
159 struct ptlrpc_client {
160         __u32                     cli_request_portal;
161         __u32                     cli_reply_portal;
162         char                     *cli_name;
163 };
164
165 /* state flags of requests */
166 /* XXX only ones left are those used by the bulk descs as well! */
167 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
168 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
169
170 #define REQ_MAX_ACK_LOCKS 8
171
172 union ptlrpc_async_args {
173         /* Scratchpad for passing args to completion interpreter. Users
174          * cast to the struct of their choosing, and LASSERT that this is
175          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
176          * a pointer to it here.  The pointer_arg ensures this struct is at
177          * least big enough for that. */
178         void      *pointer_arg[9];
179         __u64      space[4];
180 };
181
182 struct ptlrpc_request_set;
183 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
184
185 struct ptlrpc_request_set {
186         int               set_remaining; /* # uncompleted requests */
187         cfs_waitq_t       set_waitq;
188         cfs_waitq_t      *set_wakeup_ptr;
189         struct list_head  set_requests;
190         set_interpreter_func    set_interpret; /* completion callback */
191         void              *set_arg; /* completion context */
192         /* locked so that any old caller can communicate requests to
193          * the set holder who can then fold them into the lock-free set */
194         spinlock_t        set_new_req_lock;
195         struct list_head  set_new_requests;
196 };
197
198 struct ptlrpc_bulk_desc;
199
200 /*
201  * ptlrpc callback & work item stuff
202  */
203 struct ptlrpc_cb_id {
204         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
205         void    *cbid_arg;                      /* additional arg */
206 };
207
208 #define RS_MAX_LOCKS 4
209 #define RS_DEBUG     1
210
211 struct ptlrpc_reply_state {
212         struct ptlrpc_cb_id    rs_cb_id;
213         struct list_head       rs_list;
214         struct list_head       rs_exp_list;
215         struct list_head       rs_obd_list;
216 #if RS_DEBUG
217         struct list_head       rs_debug_list;
218 #endif
219         /* updates to following flag serialised by srv_request_lock */
220         unsigned int           rs_difficult:1;     /* ACK/commit stuff */
221         unsigned int           rs_scheduled:1;     /* being handled? */
222         unsigned int           rs_scheduled_ever:1;/* any schedule attempts? */
223         unsigned int           rs_handled:1;  /* been handled yet? */
224         unsigned int           rs_on_net:1;   /* reply_out_callback pending? */
225         unsigned int           rs_prealloc:1; /* rs from prealloc list */
226
227         int                    rs_size;
228         __u64                  rs_transno;
229         __u64                  rs_xid;
230         struct obd_export     *rs_export;
231         struct ptlrpc_service *rs_service;
232         lnet_handle_md_t       rs_md_h;
233         atomic_t               rs_refcount;
234
235         /* locks awaiting client reply ACK */
236         int                    rs_nlocks;
237         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
238         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
239         /* last member: variable sized reply message */
240         struct lustre_msg     *rs_msg;
241 };
242
243 struct ptlrpc_thread;
244
245 enum rq_phase {
246         RQ_PHASE_NEW         = 0xebc0de00,
247         RQ_PHASE_RPC         = 0xebc0de01,
248         RQ_PHASE_BULK        = 0xebc0de02,
249         RQ_PHASE_INTERPRET   = 0xebc0de03,
250         RQ_PHASE_COMPLETE    = 0xebc0de04,
251 };
252
253 struct ptlrpc_request_pool {
254         spinlock_t prp_lock;
255         struct list_head prp_req_list;    /* list of ptlrpc_request structs */
256         int prp_rq_size;
257         void (*prp_populate)(struct ptlrpc_request_pool *, int);
258 };
259
260 struct ptlrpc_request {
261         int rq_type; /* one of PTL_RPC_MSG_* */
262         struct list_head rq_list;
263         struct list_head rq_history_list;       /* server-side history */
264         __u64            rq_history_seq;        /* history sequence # */
265         int rq_status;
266         spinlock_t rq_lock;
267         /* client-side flags */
268         unsigned int rq_intr:1, rq_replied:1, rq_err:1,
269                 rq_timedout:1, rq_resend:1, rq_restart:1,
270                 /*
271                  * when ->rq_replay is set, request is kept by the client even
272                  * after server commits corresponding transaction. This is
273                  * used for operations that require sequence of multiple
274                  * requests to be replayed. The only example currently is file
275                  * open/close. When last request in such a sequence is
276                  * committed, ->rq_replay is cleared on all requests in the
277                  * sequence.
278                  */
279                 rq_replay:1,
280                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
281                 rq_no_delay:1, rq_net_err:1;
282         enum rq_phase rq_phase; /* one of RQ_PHASE_* */
283         atomic_t rq_refcount;   /* client-side refcount for SENT race */
284
285         struct ptlrpc_thread *rq_svc_thread; /* initial thread servicing req */
286
287         int rq_request_portal;  /* XXX FIXME bug 249 */
288         int rq_reply_portal;    /* XXX FIXME bug 249 */
289
290         int rq_nob_received; /* client-side # reply bytes actually received  */
291
292         int rq_reqlen;
293         struct lustre_msg *rq_reqmsg;
294
295         int rq_timeout;         /* time to wait for reply (seconds) */
296         int rq_replen;
297         struct lustre_msg *rq_repmsg;
298         __u64 rq_transno;
299         __u64 rq_xid;
300         struct list_head rq_replay_list;
301
302         __u32 rq_req_swab_mask;
303         __u32 rq_rep_swab_mask;
304
305         int rq_import_generation;
306         enum lustre_imp_state rq_send_state;
307
308         /* client+server request */
309         lnet_handle_md_t     rq_req_md_h;
310         struct ptlrpc_cb_id  rq_req_cbid;
311
312         /* server-side... */
313         struct timeval       rq_arrival_time;       /* request arrival time */
314         struct ptlrpc_reply_state *rq_reply_state;  /* separated reply state */
315         struct ptlrpc_request_buffer_desc *rq_rqbd; /* incoming request buffer*/
316 #ifdef CRAY_XT3
317         __u32                rq_uid;            /* peer uid, used in MDS only */
318 #endif
319
320         /* client-only incoming reply */
321         lnet_handle_md_t     rq_reply_md_h;
322         cfs_waitq_t          rq_reply_waitq;
323         struct ptlrpc_cb_id  rq_reply_cbid;
324
325         lnet_nid_t           rq_self;
326         lnet_process_id_t    rq_peer;
327         struct obd_export   *rq_export;
328         struct obd_import   *rq_import;
329
330         void (*rq_replay_cb)(struct ptlrpc_request *);
331         void (*rq_commit_cb)(struct ptlrpc_request *);
332         void  *rq_cb_data;
333
334         struct ptlrpc_bulk_desc *rq_bulk;       /* client side bulk */
335         time_t rq_sent;                         /* when request sent, seconds */
336
337         /* Multi-rpc bits */
338         struct list_head rq_set_chain;
339         struct ptlrpc_request_set *rq_set;
340         void *rq_interpret_reply;               /* Async completion handler */
341         union ptlrpc_async_args rq_async_args;  /* Async completion context */
342         void *rq_ptlrpcd_data;
343         struct ptlrpc_request_pool *rq_pool;    /* Pool if request from
344                                                    preallocated list */
345 };
346
347 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
348 {
349         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
350         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
351         req->rq_req_swab_mask |= 1 << index;
352 }
353
354 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
355 {
356         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
357         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
358         req->rq_rep_swab_mask |= 1 << index;
359 }
360
361 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
362
363         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
364         return req->rq_req_swab_mask & (1 << index);
365 }
366
367 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
368 {
369         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
370         return req->rq_rep_swab_mask & (1 << index);
371 }
372
373 #define SWAB_PARANOIA 1
374
375 #if SWAB_PARANOIA
376 /* unpacking: assert idx not unpacked already */
377 #define LASSERT_REQSWAB(rq, idx) lustre_set_req_swabbed(rq, idx)
378 #define LASSERT_REPSWAB(rq, idx) lustre_set_rep_swabbed(rq, idx)
379
380 /* just looking: assert idx already unpacked */
381 #define LASSERT_REQSWABBED(rq, idx) LASSERT(lustre_req_swabbed(rq, idx))
382 #define LASSERT_REPSWABBED(rq, idx) LASSERT(lustre_rep_swabbed(rq, idx))
383
384 #else
385 #define LASSERT_REQSWAB(rq, idx)
386 #define LASSERT_REPSWAB(rq, idx)
387 #define LASSERT_REQSWABBED(rq, idx)
388 #define LASSERT_REPSWABBED(rq, idx)
389 #endif
390
391 static inline const char *
392 ptlrpc_rqphase2str(struct ptlrpc_request *req)
393 {
394         switch (req->rq_phase) {
395         case RQ_PHASE_NEW:
396                 return "New";
397         case RQ_PHASE_RPC:
398                 return "Rpc";
399         case RQ_PHASE_BULK:
400                 return "Bulk";
401         case RQ_PHASE_INTERPRET:
402                 return "Interpret";
403         case RQ_PHASE_COMPLETE:
404                 return "Complete";
405         default:
406                 return "?Phase?";
407         }
408 }
409
410 /* Spare the preprocessor, spoil the bugs. */
411 #define FLAG(field, str) (field ? str : "")
412
413 #define DEBUG_REQ_FLAGS(req)                                                    \
414         ptlrpc_rqphase2str(req),                                                \
415         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                    \
416         FLAG(req->rq_err, "E"),                                                 \
417         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"),   \
418         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                  \
419         FLAG(req->rq_no_resend, "N"),                                           \
420         FLAG(req->rq_waiting, "W")
421
422 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s"
423
424 void _debug_req(struct ptlrpc_request *req, __u32 mask,
425                struct libcfs_debug_msg_data *data, const char *fmt, ...);
426
427 #define debug_req(cdls, level, req, file, func, line, fmt, a...)                \
428 do {                                                                            \
429         CHECK_STACK();                                                          \
430                                                                                 \
431         if (((level) & D_CANTMASK) != 0 ||                                      \
432             ((libcfs_debug & (level)) != 0 &&                                   \
433              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {                \
434                 static struct libcfs_debug_msg_data _req_dbg_data =             \
435                 DEBUG_MSG_DATA_INIT(cdls, DEBUG_SUBSYSTEM, file, func, line);   \
436                 _debug_req((req), (level), &_req_dbg_data, fmt, ##a);           \
437         }                                                                       \
438 } while(0)
439
440 /* for most callers (level is a constant) this is resolved at compile time */
441 #define DEBUG_REQ(level, req, fmt, args...)                                    \
442 do {                                                                           \
443         if ((level) & (D_ERROR | D_WARNING)) {                                 \
444                 static cfs_debug_limit_state_t cdls;                           \
445                 debug_req(&cdls, level, req, __FILE__, __func__, __LINE__,     \
446                           "@@@ "fmt, ## args);                                 \
447         } else                                                                 \
448                 debug_req(NULL, level, req, __FILE__, __func__, __LINE__,      \
449                           "@@@ "fmt, ## args);                                 \
450 } while (0)
451
452 struct ptlrpc_bulk_page {
453         struct list_head bp_link;
454         int bp_buflen;
455         int bp_pageoffset;                      /* offset within a page */
456         struct page *bp_page;
457 };
458
459 #define BULK_GET_SOURCE   0
460 #define BULK_PUT_SINK     1
461 #define BULK_GET_SINK     2
462 #define BULK_PUT_SOURCE   3
463
464 struct ptlrpc_bulk_desc {
465         unsigned int bd_success:1;              /* completed successfully */
466         unsigned int bd_network_rw:1;           /* accessible to the network */
467         unsigned int bd_type:2;                 /* {put,get}{source,sink} */
468         unsigned int bd_registered:1;           /* client side */
469         spinlock_t   bd_lock;                   /* serialise with callback */
470         int bd_import_generation;
471         struct obd_export *bd_export;
472         struct obd_import *bd_import;
473         __u32 bd_portal;
474         struct ptlrpc_request *bd_req;          /* associated request */
475         cfs_waitq_t            bd_waitq;        /* server side only WQ */
476         int                    bd_iov_count;    /* # entries in bd_iov */
477         int                    bd_max_iov;      /* allocated size of bd_iov */
478         int                    bd_nob;          /* # bytes covered */
479         int                    bd_nob_transferred; /* # bytes GOT/PUT */
480
481         __u64                  bd_last_xid;
482
483         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
484         lnet_handle_md_t        bd_md_h;         /* associated MD */
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_timeout;  /* soft watchdog timeout, in ms */
533         unsigned         srv_cpu_affinity:1;    /* bind threads to CPUs */
534
535         __u32            srv_req_portal;
536         __u32            srv_rep_portal;
537
538         int               srv_n_queued_reqs;    /* # reqs waiting to be served */
539         struct list_head  srv_request_queue;    /* reqs waiting for service */
540
541         struct list_head  srv_request_history;  /* request history */
542         __u64             srv_request_seq;      /* next request sequence # */
543         __u64             srv_request_max_cull_seq; /* highest seq culled from history */
544         svcreq_printfn_t  srv_request_history_print_fn; /* service-specific print fn */
545
546         struct list_head  srv_idle_rqbds;       /* request buffers to be reposted */
547         struct list_head  srv_active_rqbds;     /* req buffers receiving */
548         struct list_head  srv_history_rqbds;    /* request buffer history */
549         int               srv_nrqbd_receiving;  /* # posted request buffers */
550         int               srv_n_history_rqbds;  /* # request buffers in history */
551         int               srv_max_history_rqbds;/* max # request buffers in history */
552
553         atomic_t          srv_outstanding_replies;
554         struct list_head  srv_active_replies;   /* all the active replies */
555         struct list_head  srv_reply_queue;      /* replies waiting for service */
556
557         cfs_waitq_t       srv_waitq; /* all threads sleep on this. This
558                                       * wait-queue is signalled when new
559                                       * incoming request arrives and when
560                                       * difficult reply has to be handled. */
561
562         struct list_head   srv_threads;         /* service thread list */
563         svc_handler_t      srv_handler;
564
565         char *srv_name;  /* only statically allocated strings here; we don't clean them */
566         char *srv_thread_name;  /* only statically allocated strings here; we don't clean them */
567
568         spinlock_t               srv_lock;
569
570         cfs_proc_dir_entry_t    *srv_procroot;
571         struct lprocfs_stats    *srv_stats;
572
573         /* List of free reply_states */
574         struct list_head         srv_free_rs_list;
575         /* waitq to run, when adding stuff to srv_free_rs_list */
576         cfs_waitq_t              srv_free_rs_waitq;
577         
578         /*
579          * if non-NULL called during thread creation (ptlrpc_start_thread())
580          * to initialize service specific per-thread state.
581          */
582         int (*srv_init)(struct ptlrpc_thread *thread);
583         /*
584          * if non-NULL called during thread shutdown (ptlrpc_main()) to
585          * destruct state created by ->srv_init().
586          */
587         void (*srv_done)(struct ptlrpc_thread *thread);
588
589         //struct ptlrpc_srv_ni srv_interfaces[0];
590 };
591
592 /* ptlrpc/events.c */
593 extern lnet_handle_eq_t ptlrpc_eq_h;
594 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
595                                lnet_process_id_t *peer, lnet_nid_t *self);
596 extern void request_out_callback (lnet_event_t *ev);
597 extern void reply_in_callback(lnet_event_t *ev);
598 extern void client_bulk_callback (lnet_event_t *ev);
599 extern void request_in_callback(lnet_event_t *ev);
600 extern void reply_out_callback(lnet_event_t *ev);
601 extern void server_bulk_callback (lnet_event_t *ev);
602
603 /* ptlrpc/connection.c */
604 void ptlrpc_dump_connections(void);
605 void ptlrpc_readdress_connection(struct ptlrpc_connection *, struct obd_uuid *);
606 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
607                                                 lnet_nid_t self, struct obd_uuid *uuid);
608 int ptlrpc_put_connection(struct ptlrpc_connection *c);
609 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
610 void ptlrpc_init_connection(void);
611 void ptlrpc_cleanup_connection(void);
612 extern lnet_pid_t ptl_get_pid(void);
613
614 /* ptlrpc/niobuf.c */
615 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
616 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
617 int ptlrpc_register_bulk(struct ptlrpc_request *req);
618 void ptlrpc_unregister_bulk (struct ptlrpc_request *req);
619
620 static inline int ptlrpc_bulk_active (struct ptlrpc_bulk_desc *desc)
621 {
622         int           rc;
623
624         spin_lock(&desc->bd_lock);
625         rc = desc->bd_network_rw;
626         spin_unlock(&desc->bd_lock);
627         return (rc);
628 }
629
630 int ptlrpc_send_reply(struct ptlrpc_request *req, int);
631 int ptlrpc_reply(struct ptlrpc_request *req);
632 int ptlrpc_error(struct ptlrpc_request *req);
633 void ptlrpc_resend_req(struct ptlrpc_request *request);
634 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
635 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd);
636
637 /* ptlrpc/client.c */
638 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
639                         struct ptlrpc_client *);
640 void ptlrpc_cleanup_client(struct obd_import *imp);
641 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
642
643 static inline int
644 ptlrpc_client_receiving_reply (struct ptlrpc_request *req)
645 {
646         int           rc;
647
648         spin_lock(&req->rq_lock);
649         rc = req->rq_receiving_reply;
650         spin_unlock(&req->rq_lock);
651         return (rc);
652 }
653
654 static inline int
655 ptlrpc_client_replied (struct ptlrpc_request *req)
656 {
657         int           rc;
658
659         spin_lock(&req->rq_lock);
660         rc = req->rq_replied;
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_next_timeout(struct ptlrpc_request_set *);
682 int ptlrpc_check_set(struct ptlrpc_request_set *set);
683 int ptlrpc_set_wait(struct ptlrpc_request_set *);
684 int ptlrpc_expired_set(void *data);
685 void ptlrpc_interrupted_set(void *data);
686 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
687 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
688 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
689 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *,
690                             struct ptlrpc_request *);
691
692 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
693 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
694 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int, int,
695                                                 void (*populate_pool)(struct ptlrpc_request_pool *, int));
696 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
697                                        int opcode, int count, int *lengths,
698                                        char **bufs);
699 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
700                                              __u32 version, int opcode,
701                                             int count, int *lengths, char **bufs,
702                                             struct ptlrpc_request_pool *pool);
703 void ptlrpc_free_req(struct ptlrpc_request *request);
704 void ptlrpc_req_finished(struct ptlrpc_request *request);
705 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
706 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
707 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
708                                                int npages, int type, int portal);
709 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
710                                               int npages, int type, int portal);
711 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
712 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
713                            cfs_page_t *page, int pageoffset, int len);
714 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
715                                       struct obd_import *imp);
716 __u64 ptlrpc_next_xid(void);
717 __u64 ptlrpc_sample_next_xid(void);
718 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
719
720 /* ptlrpc/service.c */
721 void ptlrpc_save_lock (struct ptlrpc_request *req,
722                        struct lustre_handle *lock, int mode);
723 void ptlrpc_commit_replies (struct obd_device *obd);
724 void ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs);
725 struct ptlrpc_service *ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
726                                        int max_reply_size,
727                                        int req_portal, int rep_portal,
728                                        int watchdog_timeout, /* in ms */
729                                        svc_handler_t, char *name,
730                                        cfs_proc_dir_entry_t *proc_entry,
731                                        svcreq_printfn_t, 
732                                        int min_threads, int max_threads,
733                                        char *threadname);
734 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
735
736 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc);
737 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc);
738 int ptlrpc_unregister_service(struct ptlrpc_service *service);
739 int liblustre_check_services (void *arg);
740 void ptlrpc_daemonize(char *name);
741 int ptlrpc_service_health_check(struct ptlrpc_service *);
742
743
744 struct ptlrpc_svc_data {
745         char *name;
746         struct ptlrpc_service *svc;
747         struct ptlrpc_thread *thread;
748         struct obd_device *dev;
749 };
750
751 /* ptlrpc/import.c */
752 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid);
753 int ptlrpc_init_import(struct obd_import *imp);
754 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
755 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
756
757 /* ptlrpc/pack_generic.c */
758 int lustre_msg_swabbed(struct lustre_msg *msg);
759 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
760 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
761                         int *lens, char **bufs);
762 int lustre_pack_reply(struct ptlrpc_request *, int count, int *lens,
763                       char **bufs);
764 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
765                          unsigned int newlen, int move_data);
766 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
767 int lustre_msg_size(__u32 magic, int count, int *lengths);
768 int lustre_unpack_msg(struct lustre_msg *m, int len);
769 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
770 int lustre_msg_buflen(struct lustre_msg *m, int n);
771 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
772 int lustre_msg_bufcount(struct lustre_msg *m);
773 char *lustre_msg_string (struct lustre_msg *m, int n, int max_len);
774 void *lustre_swab_buf(struct lustre_msg *, int n, int minlen, void *swabber);
775 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int n, int minlen,
776                          void *swabber);
777 void *lustre_swab_repbuf(struct ptlrpc_request *req, int n, int minlen,
778                          void *swabber);
779 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
780 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
781 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
782 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
783 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
784 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
785 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
786 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
787 __u32 lustre_msg_get_type(struct lustre_msg *msg);
788 __u32 lustre_msg_get_version(struct lustre_msg *msg);
789 void lustre_msg_add_version(struct lustre_msg *msg, int version);
790 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
791 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
792 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
793 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
794 __u32 lustre_msg_get_status(struct lustre_msg *msg);
795 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
796 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
797 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
798 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
799 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
800 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
801 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
802 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
803 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
804 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
805
806 static inline void
807 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
808 {
809         LASSERT(atomic_read(&rs->rs_refcount) > 0);
810         atomic_inc(&rs->rs_refcount);
811 }
812
813 static inline void
814 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
815 {
816         LASSERT(atomic_read(&rs->rs_refcount) > 0);
817         if (atomic_dec_and_test(&rs->rs_refcount))
818                 lustre_free_reply_state(rs);
819 }
820
821 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
822 {
823         return lustre_msg_get_magic(req->rq_reqmsg);
824 }
825
826 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
827 {
828         switch (req->rq_reqmsg->lm_magic) {
829         case LUSTRE_MSG_MAGIC_V1:
830                 CERROR("function not supported for lustre_msg V1!\n");
831                 return -ENOTSUPP;
832         case LUSTRE_MSG_MAGIC_V2:
833                 return req->rq_reqmsg->lm_repsize;
834         default:
835                 LASSERTF(0, "incorrect message magic: %08x\n",
836                          req->rq_reqmsg->lm_magic);
837                 return -EFAULT;
838         }
839 }
840
841 static inline void
842 ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, int *lens)
843 {
844         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
845         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
846                 req->rq_reqmsg->lm_repsize = req->rq_replen;
847 }
848
849 /* ldlm/ldlm_lib.c */
850 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
851 int client_obd_cleanup(struct obd_device * obddev);
852 int client_connect_import(struct lustre_handle *conn, struct obd_device *obd,
853                           struct obd_uuid *cluuid, struct obd_connect_data *);
854 int client_disconnect_export(struct obd_export *exp);
855 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
856                            int priority);
857 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
858 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
859
860 /* ptlrpc/pinger.c */
861 int ptlrpc_pinger_add_import(struct obd_import *imp);
862 int ptlrpc_pinger_del_import(struct obd_import *imp);
863 #ifdef __KERNEL__
864 void ping_evictor_start(void);
865 void ping_evictor_stop(void);
866 #else
867 #define ping_evictor_start()    do {} while (0)
868 #define ping_evictor_stop()     do {} while (0)
869 #endif
870
871 /* ptlrpc/ptlrpcd.c */
872 void ptlrpcd_wake(struct ptlrpc_request *req);
873 void ptlrpcd_add_req(struct ptlrpc_request *req);
874 int ptlrpcd_addref(void);
875 void ptlrpcd_decref(void);
876
877 /* ptlrpc/lproc_ptlrpc.c */
878 #ifdef LPROCFS
879 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
880 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
881 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes);
882 #else
883 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
884 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
885 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc,
886                                       int bytes) {}
887 #endif
888
889 /* ptlrpc/llog_server.c */
890 int llog_origin_handle_create(struct ptlrpc_request *req);
891 int llog_origin_handle_destroy(struct ptlrpc_request *req);
892 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
893 int llog_origin_handle_next_block(struct ptlrpc_request *req);
894 int llog_origin_handle_read_header(struct ptlrpc_request *req);
895 int llog_origin_handle_close(struct ptlrpc_request *req);
896 int llog_origin_handle_cancel(struct ptlrpc_request *req);
897 int llog_catinfo(struct ptlrpc_request *req);
898
899 /* ptlrpc/llog_client.c */
900 extern struct llog_operations llog_client_ops;
901
902 #endif