Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / include / lustre_net.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef _LUSTRE_NET_H
24 #define _LUSTRE_NET_H
25
26 #if defined(__linux__)
27 #include <linux/lustre_net.h>
28 #elif defined(__APPLE__)
29 #include <darwin/lustre_net.h>
30 #elif defined(__WINNT__)
31 #include <winnt/lustre_net.h>
32 #else
33 #error Unsupported operating system.
34 #endif
35
36 #include <libcfs/kp30.h>
37 // #include <obd.h>
38 #include <lnet/lnet.h>
39 #include <lustre/lustre_idl.h>
40 #include <lustre_ha.h>
41 #include <lustre_sec.h>
42 #include <lustre_import.h>
43 #include <lprocfs_status.h>
44 #include <lu_object.h>
45
46 /* MD flags we _always_ use */
47 #define PTLRPC_MD_OPTIONS  0
48
49 /* Define maxima for bulk I/O
50  * CAVEAT EMPTOR, with multinet (i.e. routers forwarding between networks)
51  * these limits are system wide and not interface-local. */
52 #define PTLRPC_MAX_BRW_BITS     LNET_MTU_BITS
53 #define PTLRPC_MAX_BRW_SIZE     (1<<LNET_MTU_BITS)
54 #define PTLRPC_MAX_BRW_PAGES    (PTLRPC_MAX_BRW_SIZE >> CFS_PAGE_SHIFT)
55
56 /* When PAGE_SIZE is a constant, we can check our arithmetic here with cpp! */
57 #ifdef __KERNEL__
58 # if ((PTLRPC_MAX_BRW_PAGES & (PTLRPC_MAX_BRW_PAGES - 1)) != 0)
59 #  error "PTLRPC_MAX_BRW_PAGES isn't a power of two"
60 # endif
61 # if (PTLRPC_MAX_BRW_SIZE != (PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE))
62 #  error "PTLRPC_MAX_BRW_SIZE isn't PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE"
63 # endif
64 # if (PTLRPC_MAX_BRW_SIZE > LNET_MTU)
65 #  error "PTLRPC_MAX_BRW_SIZE too big"
66 # endif
67 # if (PTLRPC_MAX_BRW_PAGES > LNET_MAX_IOV)
68 #  error "PTLRPC_MAX_BRW_PAGES too big"
69 # endif
70 #endif /* __KERNEL__ */
71
72 /* Size over which to OBD_VMALLOC() rather than OBD_ALLOC() service request
73  * buffers */
74 #define SVC_BUF_VMALLOC_THRESHOLD (2 * CFS_PAGE_SIZE)
75
76 /* The following constants determine how memory is used to buffer incoming
77  * service requests.
78  *
79  * ?_NBUFS              # buffers to allocate when growing the pool
80  * ?_BUFSIZE            # bytes in a single request buffer
81  * ?_MAXREQSIZE         # maximum request service will receive
82  *
83  * When fewer than ?_NBUFS/2 buffers are posted for receive, another chunk
84  * of ?_NBUFS is added to the pool.
85  *
86  * Messages larger than ?_MAXREQSIZE are dropped.  Request buffers are
87  * considered full when less than ?_MAXREQSIZE is left in them.
88  */
89
90 #define LDLM_THREADS_AUTO_MIN                                                 \
91         min((int)(num_online_cpus() * num_online_cpus() * 2), 8)
92 #define LDLM_THREADS_AUTO_MAX (LDLM_THREADS_AUTO_MIN * 16)
93 #define LDLM_BL_THREADS  LDLM_THREADS_AUTO_MIN
94 #define LDLM_NBUFS      (64 * num_online_cpus())
95 #define LDLM_BUFSIZE    (8 * 1024)
96 #define LDLM_MAXREQSIZE (5 * 1024)
97 #define LDLM_MAXREPSIZE (1024)
98
99 #define MDT_MIN_THREADS 2UL
100 #define MDT_MAX_THREADS 512UL
101 #define MDT_NUM_THREADS max(min_t(unsigned long, MDT_MAX_THREADS, \
102                                   num_physpages >> (25 - PAGE_SHIFT)), 2UL)
103 #define FLD_NUM_THREADS max(min_t(unsigned long, MDT_MAX_THREADS, \
104                                   num_physpages >> (25 - PAGE_SHIFT)), 2UL)
105 #define SEQ_NUM_THREADS max(min_t(unsigned long, MDT_MAX_THREADS, \
106                                   num_physpages >> (25 - PAGE_SHIFT)), 2UL)
107
108 /* Absolute limits */
109 #define MDS_THREADS_MIN 2
110 #define MDS_THREADS_MAX 512
111 #define MDS_THREADS_MIN_READPAGE 2
112 #define MDS_NBUFS       (64 * num_online_cpus())
113 #define MDS_BUFSIZE     (8 * 1024)
114 /* Assume file name length = FNAME_MAX = 256 (true for ext3).
115  *        path name length = PATH_MAX = 4096
116  *        LOV MD size max  = EA_MAX = 4000
117  * symlink:  FNAME_MAX + PATH_MAX  <- largest
118  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
119  * rename:   FNAME_MAX + FNAME_MAX
120  * open:     FNAME_MAX + EA_MAX
121  *
122  * MDS_MAXREQSIZE ~= 4736 bytes =
123  * lustre_msg + ldlm_request + mds_body + mds_rec_create + FNAME_MAX + PATH_MAX
124  * MDS_MAXREPSIZE ~= 8300 bytes = lustre_msg + llog_header
125  * or, for mds_close() and mds_reint_unlink() on a many-OST filesystem:
126  *      = 9210 bytes = lustre_msg + mds_body + 160 * (easize + cookiesize)
127  *
128  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
129  * except in the open case where there are a large number of OSTs in a LOV.
130  */
131 #define MDS_MAXREQSIZE  (5 * 1024)
132 #define MDS_MAXREPSIZE  max(9 * 1024, 280 + LOV_MAX_STRIPE_COUNT * 56)
133
134 /* FLD_MAXREQSIZE == lustre_msg + __u32 padding + ptlrpc_body + opc + md_fld */
135 #define FLD_MAXREQSIZE  (160)
136
137 /* FLD_MAXREPSIZE == lustre_msg + ptlrpc_body + md_fld */
138 #define FLD_MAXREPSIZE  (152)
139
140 /* SEQ_MAXREQSIZE == lustre_msg + __u32 padding + ptlrpc_body + opc + lu_range +
141  * __u32 padding */
142 #define SEQ_MAXREQSIZE  (160)
143
144 /* SEQ_MAXREPSIZE == lustre_msg + ptlrpc_body + lu_range */
145 #define SEQ_MAXREPSIZE  (152)
146
147 #define MGS_THREADS_AUTO_MIN 2
148 #define MGS_THREADS_AUTO_MAX 32
149 #define MGS_NBUFS       (64 * num_online_cpus())
150 #define MGS_BUFSIZE     (8 * 1024)
151 #define MGS_MAXREQSIZE  (7 * 1024)
152 #define MGS_MAXREPSIZE  (9 * 1024)
153
154 /* Absolute limits */
155 #define OSS_THREADS_MIN 2
156 #define OSS_THREADS_MAX 512
157 #define OST_NBUFS       (64 * num_online_cpus())
158 #define OST_BUFSIZE     (8 * 1024)
159 /* OST_MAXREQSIZE ~= 4768 bytes =
160  * lustre_msg + obdo + 16 * obd_ioobj + 256 * niobuf_remote
161  *
162  * - single object with 16 pages is 512 bytes
163  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
164  */
165 #define OST_MAXREQSIZE  (5 * 1024)
166 #define OST_MAXREPSIZE  (9 * 1024)
167
168 struct ptlrpc_connection {
169         struct list_head        c_link;
170         struct hlist_node       c_hash;
171         lnet_nid_t              c_self;
172         lnet_process_id_t       c_peer;
173         struct obd_uuid         c_remote_uuid;
174         atomic_t                c_refcount;
175 };
176
177 struct ptlrpc_client {
178         __u32                     cli_request_portal;
179         __u32                     cli_reply_portal;
180         char                     *cli_name;
181 };
182
183 /* state flags of requests */
184 /* XXX only ones left are those used by the bulk descs as well! */
185 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
186 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
187
188 #define REQ_MAX_ACK_LOCKS 8
189
190 union ptlrpc_async_args {
191         /* Scratchpad for passing args to completion interpreter. Users
192          * cast to the struct of their choosing, and LASSERT that this is
193          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
194          * a pointer to it here.  The pointer_arg ensures this struct is at
195          * least big enough for that. */
196         void      *pointer_arg[9];
197         __u64      space[4];
198 };
199
200 struct ptlrpc_request_set;
201 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
202
203 struct ptlrpc_request_set {
204         int               set_remaining; /* # uncompleted requests */
205         cfs_waitq_t       set_waitq;
206         cfs_waitq_t      *set_wakeup_ptr;
207         struct list_head  set_requests;
208         set_interpreter_func    set_interpret; /* completion callback */
209         void              *set_arg; /* completion context */
210         void              *set_countp; /* pointer to NOB counter in case 
211                                         * of directIO (bug11737) */
212         /* locked so that any old caller can communicate requests to
213          * the set holder who can then fold them into the lock-free set */
214         spinlock_t        set_new_req_lock;
215         struct list_head  set_new_requests;
216 };
217
218 struct ptlrpc_bulk_desc;
219
220 /*
221  * ptlrpc callback & work item stuff
222  */
223 struct ptlrpc_cb_id {
224         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
225         void    *cbid_arg;                      /* additional arg */
226 };
227
228 #define RS_MAX_LOCKS 4
229 #define RS_DEBUG     1
230
231 struct ptlrpc_reply_state {
232         struct ptlrpc_cb_id    rs_cb_id;
233         struct list_head       rs_list;
234         struct list_head       rs_exp_list;
235         struct list_head       rs_obd_list;
236 #if RS_DEBUG
237         struct list_head       rs_debug_list;
238 #endif
239         /* updates to following flag serialised by srv_request_lock */
240         unsigned int           rs_difficult:1;     /* ACK/commit stuff */
241         unsigned int           rs_scheduled:1;     /* being handled? */
242         unsigned int           rs_scheduled_ever:1;/* any schedule attempts? */
243         unsigned int           rs_handled:1;  /* been handled yet? */
244         unsigned int           rs_on_net:1;   /* reply_out_callback pending? */
245         unsigned int           rs_prealloc:1; /* rs from prealloc list */
246
247         int                    rs_size;
248         __u64                  rs_transno;
249         __u64                  rs_xid;
250         struct obd_export     *rs_export;
251         struct ptlrpc_service *rs_service;
252         lnet_handle_md_t       rs_md_h;
253         atomic_t               rs_refcount;
254
255         struct ptlrpc_svc_ctx *rs_svc_ctx;
256         struct lustre_msg     *rs_repbuf;       /* wrapper */
257         int                    rs_repbuf_len;   /* wrapper buf length */
258         int                    rs_repdata_len;  /* wrapper msg length */
259         struct lustre_msg     *rs_msg;          /* reply message */
260
261         /* locks awaiting client reply ACK */
262         int                    rs_nlocks;
263         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
264         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
265 };
266
267 struct ptlrpc_thread;
268
269 enum rq_phase {
270         RQ_PHASE_NEW         = 0xebc0de00,
271         RQ_PHASE_RPC         = 0xebc0de01,
272         RQ_PHASE_BULK        = 0xebc0de02,
273         RQ_PHASE_INTERPRET   = 0xebc0de03,
274         RQ_PHASE_COMPLETE    = 0xebc0de04,
275 };
276
277 struct ptlrpc_request_pool {
278         spinlock_t prp_lock;
279         struct list_head prp_req_list;    /* list of ptlrpc_request structs */
280         int prp_rq_size;
281         void (*prp_populate)(struct ptlrpc_request_pool *, int);
282 };
283
284 struct lu_context;
285 struct lu_env;
286
287 struct ptlrpc_request {
288         int rq_type; /* one of PTL_RPC_MSG_* */
289         struct list_head rq_list;
290         struct list_head rq_history_list;       /* server-side history */
291         __u64            rq_history_seq;        /* history sequence # */
292         int rq_status;
293         spinlock_t rq_lock;
294         /* client-side flags */
295         unsigned int rq_intr:1, rq_replied:1, rq_err:1,
296                 rq_timedout:1, rq_resend:1, rq_restart:1,
297                 /*
298                  * when ->rq_replay is set, request is kept by the client even
299                  * after server commits corresponding transaction. This is
300                  * used for operations that require sequence of multiple
301                  * requests to be replayed. The only example currently is file
302                  * open/close/dw/setattr. When last request in such a sequence
303                  * is committed, ->rq_replay is cleared on all requests in the
304                  * sequence.
305                  */
306                 rq_replay:1,
307                 /* this is the last request in the sequence. */
308                 rq_sequence:1,
309                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
310                 rq_no_delay:1, rq_net_err:1, rq_wait_ctx:1;
311         enum rq_phase rq_phase; /* one of RQ_PHASE_* */
312         atomic_t rq_refcount;   /* client-side refcount for SENT race */
313
314         struct ptlrpc_thread *rq_svc_thread; /* initial thread servicing req */
315
316         int rq_request_portal;  /* XXX FIXME bug 249 */
317         int rq_reply_portal;    /* XXX FIXME bug 249 */
318
319         int rq_nob_received; /* client-side # reply bytes actually received  */
320
321         int rq_reqlen;
322         struct lustre_msg *rq_reqmsg;
323
324         int rq_timeout;         /* time to wait for reply (seconds) */
325         int rq_replen;
326         struct lustre_msg *rq_repmsg;
327         __u64 rq_transno;
328         __u64 rq_xid;
329         struct list_head rq_replay_list;
330         struct list_head rq_mod_list;
331
332         struct ptlrpc_cli_ctx   *rq_cli_ctx;     /* client's half ctx */
333         struct ptlrpc_svc_ctx   *rq_svc_ctx;     /* server's half ctx */
334         struct list_head         rq_ctx_chain;   /* link to waited ctx */
335         ptlrpc_sec_flavor_t      rq_sec_flavor;  /* client & server */
336                                  /* client security flags */
337         unsigned int             rq_ctx_init:1,      /* context initiation */
338                                  rq_ctx_fini:1,      /* context destroy */
339                                  rq_bulk_read:1,     /* request bulk read */
340                                  rq_bulk_write:1,    /* request bulk write */
341                                  /* server authentication flags */
342                                  rq_auth_gss:1,      /* authenticated by gss */
343                                  rq_auth_remote:1,   /* authed as remote user */
344                                  rq_auth_usr_root:1, /* authed as root */
345                                  rq_auth_usr_mdt:1,  /* authed as mdt */
346                                  /* doesn't expect reply FIXME */
347                                  rq_no_reply:1;
348
349         uid_t                    rq_auth_uid;        /* authed uid */
350         uid_t                    rq_auth_mapped_uid; /* authed uid mapped to */
351
352         /* (server side), pointed directly into req buffer */
353         struct ptlrpc_user_desc *rq_user_desc;
354
355         /* various buffer pointers */
356         struct lustre_msg       *rq_reqbuf;      /* req wrapper */
357         int                      rq_reqbuf_len;  /* req wrapper buf len */
358         int                      rq_reqdata_len; /* req wrapper msg len */
359         struct lustre_msg       *rq_repbuf;      /* rep wrapper */
360         int                      rq_repbuf_len;  /* rep wrapper buf len */
361         int                      rq_repdata_len; /* rep wrapper msg len */
362         struct lustre_msg       *rq_clrbuf;      /* only in priv mode */
363         int                      rq_clrbuf_len;  /* only in priv mode */
364         int                      rq_clrdata_len; /* only in priv mode */
365
366         __u32 rq_req_swab_mask;
367         __u32 rq_rep_swab_mask;
368
369         int rq_import_generation;
370         enum lustre_imp_state rq_send_state;
371
372         /* client+server request */
373         lnet_handle_md_t     rq_req_md_h;
374         struct ptlrpc_cb_id  rq_req_cbid;
375
376         /* server-side... */
377         struct timeval       rq_arrival_time;       /* request arrival time */
378         struct ptlrpc_reply_state *rq_reply_state;  /* separated reply state */
379         struct ptlrpc_request_buffer_desc *rq_rqbd; /* incoming request buffer*/
380 #ifdef CRAY_XT3
381         __u32                rq_uid;            /* peer uid, used in MDS only */
382 #endif
383
384         /* client-only incoming reply */
385         lnet_handle_md_t     rq_reply_md_h;
386         cfs_waitq_t          rq_reply_waitq;
387         struct ptlrpc_cb_id  rq_reply_cbid;
388
389         lnet_nid_t           rq_self;
390         lnet_process_id_t    rq_peer;
391         struct obd_export   *rq_export;
392         struct obd_import   *rq_import;
393
394         void (*rq_replay_cb)(struct ptlrpc_request *);
395         void (*rq_commit_cb)(struct ptlrpc_request *);
396         void  *rq_cb_data;
397
398         struct ptlrpc_bulk_desc *rq_bulk;       /* client side bulk */
399         time_t rq_sent;                         /* when request sent, seconds,
400                                                  * or time when request should
401                                                  * be sent */
402         /* Multi-rpc bits */
403         struct list_head rq_set_chain;
404         struct ptlrpc_request_set *rq_set;
405         void *rq_interpret_reply;               /* Async completion handler */
406         union ptlrpc_async_args rq_async_args;  /* Async completion context */
407         struct ptlrpc_request_pool *rq_pool;    /* Pool if request from
408                                                    preallocated list */
409         struct lu_context           rq_session;
410 };
411
412 static inline void ptlrpc_close_replay_seq(struct ptlrpc_request *req)
413 {
414         spin_lock(&req->rq_lock);
415         req->rq_replay = 0;
416         req->rq_sequence = 1;
417         spin_unlock(&req->rq_lock);
418 }
419
420 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
421 {
422         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
423         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
424         req->rq_req_swab_mask |= 1 << index;
425 }
426
427 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
428 {
429         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
430         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
431         req->rq_rep_swab_mask |= 1 << index;
432 }
433
434 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
435
436         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
437         return req->rq_req_swab_mask & (1 << index);
438 }
439
440 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
441 {
442         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
443         return req->rq_rep_swab_mask & (1 << index);
444 }
445
446 static inline const char *
447 ptlrpc_rqphase2str(const struct ptlrpc_request *req)
448 {
449         switch (req->rq_phase) {
450         case RQ_PHASE_NEW:
451                 return "New";
452         case RQ_PHASE_RPC:
453                 return "Rpc";
454         case RQ_PHASE_BULK:
455                 return "Bulk";
456         case RQ_PHASE_INTERPRET:
457                 return "Interpret";
458         case RQ_PHASE_COMPLETE:
459                 return "Complete";
460         default:
461                 return "?Phase?";
462         }
463 }
464
465 /* Spare the preprocessor, spoil the bugs. */
466 #define FLAG(field, str) (field ? str : "")
467
468 #define DEBUG_REQ_FLAGS(req)                                                    \
469         ptlrpc_rqphase2str(req),                                                \
470         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                    \
471         FLAG(req->rq_err, "E"),                                                 \
472         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"),   \
473         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                  \
474         FLAG(req->rq_no_resend, "N"),                                           \
475         FLAG(req->rq_waiting, "W"),                                             \
476         FLAG(req->rq_wait_ctx, "C")
477
478 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s%s"
479
480 void _debug_req(struct ptlrpc_request *req, __u32 mask,
481                 struct libcfs_debug_msg_data *data, const char *fmt, ...)
482         __attribute__ ((format (printf, 4, 5)));
483
484 #define debug_req(cdls, level, req, file, func, line, fmt, a...)              \
485 do {                                                                          \
486         CHECK_STACK();                                                        \
487                                                                               \
488         if (((level) & D_CANTMASK) != 0 ||                                    \
489             ((libcfs_debug & (level)) != 0 &&                                 \
490              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {              \
491                 static struct libcfs_debug_msg_data _req_dbg_data =           \
492                 DEBUG_MSG_DATA_INIT(cdls, DEBUG_SUBSYSTEM, file, func, line); \
493                 _debug_req((req), (level), &_req_dbg_data, fmt, ##a);         \
494         }                                                                     \
495 } while(0)
496
497 /* for most callers (level is a constant) this is resolved at compile time */
498 #define DEBUG_REQ(level, req, fmt, args...)                                   \
499 do {                                                                          \
500         if ((level) & (D_ERROR | D_WARNING)) {                                \
501                 static cfs_debug_limit_state_t cdls;                          \
502                 debug_req(&cdls, level, req, __FILE__, __func__, __LINE__,    \
503                           "@@@ "fmt" ", ## args);                             \
504         } else                                                                \
505                 debug_req(NULL, level, req, __FILE__, __func__, __LINE__,     \
506                           "@@@ "fmt" ", ## args);                             \
507 } while (0)
508
509 struct ptlrpc_bulk_page {
510         struct list_head bp_link;
511         int              bp_buflen;
512         int              bp_pageoffset; /* offset within a page */
513         struct page     *bp_page;
514 };
515
516 #define BULK_GET_SOURCE   0
517 #define BULK_PUT_SINK     1
518 #define BULK_GET_SINK     2
519 #define BULK_PUT_SOURCE   3
520
521 struct ptlrpc_bulk_desc {
522         unsigned int bd_success:1;              /* completed successfully */
523         unsigned int bd_network_rw:1;           /* accessible to the network */
524         unsigned int bd_type:2;                 /* {put,get}{source,sink} */
525         unsigned int bd_registered:1;           /* client side */
526         spinlock_t   bd_lock;                   /* serialise with callback */
527         int bd_import_generation;
528         struct obd_export *bd_export;
529         struct obd_import *bd_import;
530         __u32 bd_portal;
531         struct ptlrpc_request *bd_req;          /* associated request */
532         cfs_waitq_t            bd_waitq;        /* server side only WQ */
533         int                    bd_iov_count;    /* # entries in bd_iov */
534         int                    bd_max_iov;      /* allocated size of bd_iov */
535         int                    bd_nob;          /* # bytes covered */
536         int                    bd_nob_transferred; /* # bytes GOT/PUT */
537
538         __u64                  bd_last_xid;
539
540         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
541         lnet_handle_md_t       bd_md_h;         /* associated MD */
542         lnet_nid_t             bd_sender;       /* stash event::sender */
543
544         cfs_page_t           **bd_enc_pages;
545 #if defined(__KERNEL__)
546         lnet_kiov_t            bd_iov[0];
547 #else
548         lnet_md_iovec_t        bd_iov[0];
549 #endif
550 };
551
552 struct ptlrpc_thread {
553
554         struct list_head t_link; /* active threads for service, from svc->srv_threads */
555
556         void *t_data;            /* thread-private data (preallocated memory) */
557         __u32 t_flags;
558
559         unsigned int t_id; /* service thread index, from ptlrpc_start_threads */
560         cfs_waitq_t t_ctl_waitq;
561         struct lu_env *t_env;
562 };
563
564 struct ptlrpc_request_buffer_desc {
565         struct list_head       rqbd_list;
566         struct list_head       rqbd_reqs;
567         struct ptlrpc_service *rqbd_service;
568         lnet_handle_md_t       rqbd_md_h;
569         int                    rqbd_refcount;
570         char                  *rqbd_buffer;
571         struct ptlrpc_cb_id    rqbd_cbid;
572         struct ptlrpc_request  rqbd_req;
573 };
574
575 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
576 typedef void (*svcreq_printfn_t)(void *, struct ptlrpc_request *);
577
578 struct ptlrpc_service {
579         struct list_head srv_list;              /* chain thru all services */
580         int              srv_max_req_size;      /* biggest request to receive */
581         int              srv_max_reply_size;    /* biggest reply to send */
582         int              srv_buf_size;          /* size of individual buffers */
583         int              srv_nbuf_per_group;    /* # buffers to allocate in 1 group */
584         int              srv_nbufs;             /* total # req buffer descs allocated */
585         int              srv_threads_min;       /* threads to start at SOW */
586         int              srv_threads_max;       /* thread upper limit */
587         int              srv_threads_started;   /* index of last started thread */
588         int              srv_threads_running;   /* # running threads */
589         int              srv_n_difficult_replies; /* # 'difficult' replies */
590         int              srv_n_active_reqs;     /* # reqs being served */
591         cfs_duration_t   srv_rqbd_timeout;      /* timeout before re-posting reqs, in tick */
592         int              srv_watchdog_timeout; /* soft watchdog timeout, in ms */
593         unsigned         srv_cpu_affinity:1;    /* bind threads to CPUs */
594
595         __u32            srv_req_portal;
596         __u32            srv_rep_portal;
597
598         int               srv_n_queued_reqs;    /* # reqs waiting to be served */
599         struct list_head  srv_request_queue;    /* reqs waiting for service */
600
601         struct list_head  srv_request_history;  /* request history */
602         __u64             srv_request_seq;      /* next request sequence # */
603         __u64             srv_request_max_cull_seq; /* highest seq culled from history */
604         svcreq_printfn_t  srv_request_history_print_fn; /* service-specific print fn */
605
606         struct list_head  srv_idle_rqbds;       /* request buffers to be reposted */
607         struct list_head  srv_active_rqbds;     /* req buffers receiving */
608         struct list_head  srv_history_rqbds;    /* request buffer history */
609         int               srv_nrqbd_receiving;  /* # posted request buffers */
610         int               srv_n_history_rqbds;  /* # request buffers in history */
611         int               srv_max_history_rqbds;/* max # request buffers in history */
612
613         atomic_t          srv_outstanding_replies;
614         struct list_head  srv_active_replies;   /* all the active replies */
615         struct list_head  srv_reply_queue;      /* replies waiting for service */
616
617         cfs_waitq_t       srv_waitq; /* all threads sleep on this. This
618                                       * wait-queue is signalled when new
619                                       * incoming request arrives and when
620                                       * difficult reply has to be handled. */
621
622         struct list_head   srv_threads;         /* service thread list */
623         svc_handler_t      srv_handler;
624
625         char *srv_name; /* only statically allocated strings here; we don't clean them */
626         char *srv_thread_name; /* only statically allocated strings here; we don't clean them */
627
628         spinlock_t               srv_lock;
629
630         cfs_proc_dir_entry_t    *srv_procroot;
631         struct lprocfs_stats    *srv_stats;
632
633         /* List of free reply_states */
634         struct list_head         srv_free_rs_list;
635         /* waitq to run, when adding stuff to srv_free_rs_list */
636         cfs_waitq_t              srv_free_rs_waitq;
637
638         /*
639          * Tags for lu_context associated with this thread, see struct
640          * lu_context.
641          */
642         __u32                    srv_ctx_tags;
643         /*
644          * if non-NULL called during thread creation (ptlrpc_start_thread())
645          * to initialize service specific per-thread state.
646          */
647         int (*srv_init)(struct ptlrpc_thread *thread);
648         /*
649          * if non-NULL called during thread shutdown (ptlrpc_main()) to
650          * destruct state created by ->srv_init().
651          */
652         void (*srv_done)(struct ptlrpc_thread *thread);
653
654         //struct ptlrpc_srv_ni srv_interfaces[0];
655 };
656
657 /* ptlrpc/events.c */
658 extern lnet_handle_eq_t ptlrpc_eq_h;
659 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
660                                lnet_process_id_t *peer, lnet_nid_t *self);
661 extern void request_out_callback (lnet_event_t *ev);
662 extern void reply_in_callback(lnet_event_t *ev);
663 extern void client_bulk_callback (lnet_event_t *ev);
664 extern void request_in_callback(lnet_event_t *ev);
665 extern void reply_out_callback(lnet_event_t *ev);
666 extern void server_bulk_callback (lnet_event_t *ev);
667
668 /* ptlrpc/connection.c */
669 void ptlrpc_dump_connections(void);
670 void ptlrpc_readdress_connection(struct ptlrpc_connection *, struct obd_uuid *);
671 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
672                                                 lnet_nid_t self, struct obd_uuid *uuid);
673 int ptlrpc_put_connection(struct ptlrpc_connection *c);
674 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
675 int ptlrpc_init_connection(void);
676 void ptlrpc_cleanup_connection(void);
677 extern lnet_pid_t ptl_get_pid(void);
678
679 /* ptlrpc/niobuf.c */
680 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
681 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
682 int ptlrpc_register_bulk(struct ptlrpc_request *req);
683 void ptlrpc_unregister_bulk (struct ptlrpc_request *req);
684
685 static inline int ptlrpc_bulk_active (struct ptlrpc_bulk_desc *desc)
686 {
687         int           rc;
688
689         spin_lock(&desc->bd_lock);
690         rc = desc->bd_network_rw;
691         spin_unlock(&desc->bd_lock);
692         return (rc);
693 }
694
695 int ptlrpc_send_reply(struct ptlrpc_request *req, int);
696 int ptlrpc_reply(struct ptlrpc_request *req);
697 int ptlrpc_error(struct ptlrpc_request *req);
698 void ptlrpc_resend_req(struct ptlrpc_request *request);
699 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
700 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd);
701
702 /* ptlrpc/client.c */
703 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
704                         struct ptlrpc_client *);
705 void ptlrpc_cleanup_client(struct obd_import *imp);
706 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
707
708 static inline int
709 ptlrpc_client_receiving_reply (struct ptlrpc_request *req)
710 {
711         int           rc;
712
713         spin_lock(&req->rq_lock);
714         rc = req->rq_receiving_reply;
715         spin_unlock(&req->rq_lock);
716         return (rc);
717 }
718
719 static inline int
720 ptlrpc_client_replied (struct ptlrpc_request *req)
721 {
722         int           rc;
723
724         spin_lock(&req->rq_lock);
725         rc = req->rq_replied;
726         spin_unlock(&req->rq_lock);
727         return (rc);
728 }
729
730 static inline void
731 ptlrpc_wake_client_req (struct ptlrpc_request *req)
732 {
733         if (req->rq_set == NULL)
734                 cfs_waitq_signal(&req->rq_reply_waitq);
735         else
736                 cfs_waitq_signal(&req->rq_set->set_waitq);
737 }
738
739 int ptlrpc_queue_wait(struct ptlrpc_request *req);
740 int ptlrpc_replay_req(struct ptlrpc_request *req);
741 void ptlrpc_unregister_reply(struct ptlrpc_request *req);
742 void ptlrpc_restart_req(struct ptlrpc_request *req);
743 void ptlrpc_abort_inflight(struct obd_import *imp);
744
745 struct ptlrpc_request_set *ptlrpc_prep_set(void);
746 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
747 int ptlrpc_check_set(struct ptlrpc_request_set *set);
748 int ptlrpc_set_wait(struct ptlrpc_request_set *);
749 int ptlrpc_expired_set(void *data);
750 void ptlrpc_interrupted_set(void *data);
751 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
752 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
753 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
754 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *,
755                             struct ptlrpc_request *);
756
757 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
758 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
759 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int, int,
760                                                 void (*populate_pool)(struct ptlrpc_request_pool *, int));
761 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
762                                        int opcode, int count, int *lengths,
763                                        char **bufs);
764 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
765                                              __u32 version, int opcode,
766                                             int count, int *lengths, char **bufs,
767                                             struct ptlrpc_request_pool *pool,
768                                             struct ptlrpc_cli_ctx *ctx);
769 void ptlrpc_free_req(struct ptlrpc_request *request);
770 void ptlrpc_req_finished(struct ptlrpc_request *request);
771 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
772 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
773 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
774                                                int npages, int type, int portal);
775 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
776                                               int npages, int type, int portal);
777 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
778 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
779                            cfs_page_t *page, int pageoffset, int len);
780 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
781                                       struct obd_import *imp);
782 __u64 ptlrpc_next_xid(void);
783 __u64 ptlrpc_sample_next_xid(void);
784 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
785
786 struct ptlrpc_service_conf {
787         int psc_nbufs;
788         int psc_bufsize;
789         int psc_max_req_size;
790         int psc_max_reply_size;
791         int psc_req_portal;
792         int psc_rep_portal;
793         int psc_watchdog_timeout; /* in ms */
794         int psc_min_threads;
795         int psc_max_threads;
796         __u32 psc_ctx_tags;
797 };
798
799 /* ptlrpc/service.c */
800 void ptlrpc_save_lock (struct ptlrpc_request *req,
801                        struct lustre_handle *lock, int mode);
802 void ptlrpc_commit_replies (struct obd_device *obd);
803 void ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs);
804 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
805                                             svc_handler_t h, char *name,
806                                             struct proc_dir_entry *proc_entry,
807                                             svcreq_printfn_t prntfn,
808                                             char *threadname);
809
810 struct ptlrpc_service *ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
811                                        int max_reply_size,
812                                        int req_portal, int rep_portal,
813                                        int watchdog_timeout, /* in ms */
814                                        svc_handler_t, char *name,
815                                        cfs_proc_dir_entry_t *proc_entry,
816                                        svcreq_printfn_t,
817                                        int min_threads, int max_threads,
818                                        char *threadname, __u32 ctx_tags);
819 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
820
821 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc);
822 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc);
823 int ptlrpc_unregister_service(struct ptlrpc_service *service);
824 int liblustre_check_services (void *arg);
825 void ptlrpc_daemonize(char *name);
826 int ptlrpc_service_health_check(struct ptlrpc_service *);
827
828
829 struct ptlrpc_svc_data {
830         char *name;
831         struct ptlrpc_service *svc;
832         struct ptlrpc_thread *thread;
833         struct obd_device *dev;
834 };
835
836 /* ptlrpc/import.c */
837 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid);
838 int ptlrpc_init_import(struct obd_import *imp);
839 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
840 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
841
842 /* ptlrpc/pack_generic.c */
843 int lustre_msg_swabbed(struct lustre_msg *msg);
844 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
845 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, int *lens,
846                         char **bufs);
847 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
848                         int *lens, char **bufs);
849 int lustre_pack_reply(struct ptlrpc_request *, int count, int *lens,
850                       char **bufs);
851 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
852                          int *lens, char **bufs);
853 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
854                       unsigned int newlen, int move_data);
855 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
856 int lustre_msg_size(__u32 magic, int count, int *lengths);
857 int lustre_msg_size_v2(int count, int *lengths);
858 int lustre_packed_msg_size(struct lustre_msg *msg);
859 int lustre_unpack_msg(struct lustre_msg *m, int len);
860 void *lustre_msg_buf_v1(void *msg, int n, int min_size);
861 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size);
862 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
863 int lustre_msg_buflen(struct lustre_msg *m, int n);
864 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
865 int lustre_msg_bufcount(struct lustre_msg *m);
866 char *lustre_msg_string (struct lustre_msg *m, int n, int max_len);
867 void *lustre_swab_buf(struct lustre_msg *, int n, int minlen, void *swabber);
868 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int n, int minlen,
869                          void *swabber);
870 void *lustre_swab_repbuf(struct ptlrpc_request *req, int n, int minlen,
871                          void *swabber);
872 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
873 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
874 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
875 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
876 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
877 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
878 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
879 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
880 __u32 lustre_msg_get_type(struct lustre_msg *msg);
881 __u32 lustre_msg_get_version(struct lustre_msg *msg);
882 void lustre_msg_add_version(struct lustre_msg *msg, int version);
883 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
884 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
885 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
886 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
887 __u64 lustre_msg_get_slv(struct lustre_msg *msg);
888 __u32 lustre_msg_get_limit(struct lustre_msg *msg);
889 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv);
890 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit);
891 int lustre_msg_get_status(struct lustre_msg *msg);
892 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
893 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
894 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
895 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
896 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
897 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
898 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
899 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
900 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
901 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
902
903 static inline void
904 lustre_shrink_reply(struct ptlrpc_request *req, int segment,
905                     unsigned int newlen, int move_data)
906 {
907         LASSERT(req->rq_reply_state);
908         LASSERT(req->rq_repmsg);
909         req->rq_replen = lustre_shrink_msg(req->rq_repmsg, segment,
910                                            newlen, move_data);
911 }
912
913 static inline void
914 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
915 {
916         LASSERT(atomic_read(&rs->rs_refcount) > 0);
917         atomic_inc(&rs->rs_refcount);
918 }
919
920 static inline void
921 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
922 {
923         LASSERT(atomic_read(&rs->rs_refcount) > 0);
924         if (atomic_dec_and_test(&rs->rs_refcount))
925                 lustre_free_reply_state(rs);
926 }
927
928 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
929 {
930         return lustre_msg_get_magic(req->rq_reqmsg);
931 }
932
933 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
934 {
935         switch (req->rq_reqmsg->lm_magic) {
936         case LUSTRE_MSG_MAGIC_V1:
937                 CERROR("function not supported for lustre_msg V1!\n");
938                 return -ENOTSUPP;
939         case LUSTRE_MSG_MAGIC_V2:
940                 return req->rq_reqmsg->lm_repsize;
941         default:
942                 LASSERTF(0, "incorrect message magic: %08x\n",
943                          req->rq_reqmsg->lm_magic);
944                 return -EFAULT;
945         }
946 }
947
948 static inline void
949 ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, int *lens)
950 {
951         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
952         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
953                 req->rq_reqmsg->lm_repsize = req->rq_replen;
954 }
955
956 /* ldlm/ldlm_lib.c */
957 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg);
958 int client_obd_cleanup(struct obd_device *obddev);
959 int client_connect_import(const struct lu_env *env,
960                           struct lustre_handle *conn, struct obd_device *obd,
961                           struct obd_uuid *cluuid, struct obd_connect_data *);
962 int client_disconnect_export(struct obd_export *exp);
963 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
964                            int priority);
965 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
966 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
967
968 /* ptlrpc/pinger.c */
969 int ptlrpc_pinger_add_import(struct obd_import *imp);
970 int ptlrpc_pinger_del_import(struct obd_import *imp);
971 cfs_time_t ptlrpc_suspend_wakeup_time(void);
972 #ifdef __KERNEL__
973 void ping_evictor_start(void);
974 void ping_evictor_stop(void);
975 #else
976 #define ping_evictor_start()    do {} while (0)
977 #define ping_evictor_stop()     do {} while (0)
978 #endif
979 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req);
980
981 /* ptlrpc/ptlrpcd.c */
982 void ptlrpcd_wake(struct ptlrpc_request *req);
983 void ptlrpcd_add_req(struct ptlrpc_request *req);
984 int ptlrpcd_addref(void);
985 void ptlrpcd_decref(void);
986
987 /* ptlrpc/lproc_ptlrpc.c */
988 const char* ll_opcode2str(__u32 opcode);
989 #ifdef LPROCFS
990 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
991 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
992 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes);
993 #else
994 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
995 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
996 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc,
997                                       int bytes) {}
998 #endif
999
1000 /* ptlrpc/llog_server.c */
1001 int llog_origin_handle_create(struct ptlrpc_request *req);
1002 int llog_origin_handle_destroy(struct ptlrpc_request *req);
1003 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
1004 int llog_origin_handle_next_block(struct ptlrpc_request *req);
1005 int llog_origin_handle_read_header(struct ptlrpc_request *req);
1006 int llog_origin_handle_close(struct ptlrpc_request *req);
1007 int llog_origin_handle_cancel(struct ptlrpc_request *req);
1008 int llog_catinfo(struct ptlrpc_request *req);
1009
1010 /* ptlrpc/llog_client.c */
1011 extern struct llog_operations llog_client_ops;
1012
1013 #endif