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