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