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