Whamcloud - gitweb
fix rawrpc, don't drop buffer before rpc really finished on wire, portals
[fs/lustre-release.git] / lustre / include / linux / 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 #ifdef __KERNEL__
27 #include <linux/version.h>
28 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
29 #include <linux/tqueue.h>
30 #else
31 #include <linux/workqueue.h>
32 #endif
33 #endif
34
35 #include <libcfs/kp30.h>
36 // #include <linux/obd.h>
37 #include <portals/p30.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_ha.h>
40 #include <linux/lustre_import.h>
41 #include <linux/lprocfs_status.h>
42 #include <linux/lustre_compat25.h>
43
44 /* MD flags we _always_ use */
45 #define PTLRPC_MD_OPTIONS  (PTL_MD_EVENT_START_DISABLE | \
46                             PTL_MD_LUSTRE_COMPLETION_SEMANTICS)
47
48 /* Define some large-ish maxima for bulk I/O 
49  * CAVEAT EMPTOR, with multinet (i.e. gateways forwarding between networks)
50  * these limits are system wide and not interface-local. */
51 #define PTLRPC_MAX_BRW_SIZE     (1 << 20)
52 #define PTLRPC_MAX_BRW_PAGES    512
53
54 /* ...reduce to fit... */
55
56 #if CRAY_PORTALS
57 /* include a cray header here if relevant
58  * NB liblustre SIZE/PAGES is affected too, but it merges contiguous
59  * chunks, so FTTB, it always used contiguous MDs */
60 #else
61 # include <portals/lib-types.h>
62 #endif
63
64 #if (defined(PTL_MTU) && (PTL_MTU < PTLRPC_MAX_BRW_SIZE))
65 # undef  PTLRPC_MAX_BRW_SIZE
66 # define PTLRPC_MAX_BRW_SIZE  PTL_MTU
67 #endif
68 #if (defined(PTL_MD_MAX_IOV) && (PTL_MD_MAX_IOV < PTLRPC_MAX_BRW_PAGES ))
69 # undef  PTLRPC_MAX_BRW_PAGES
70 # define PTLRPC_MAX_BRW_PAGES PTL_MD_MAX_IOV
71 #endif
72
73 /* ...and make consistent... */
74
75 #if (PTLRPC_MAX_BRW_SIZE > PTLRPC_MAX_BRW_PAGES * PAGE_SIZE)
76 # undef  PTLRPC_MAX_BRW_SIZE
77 # define PTLRPC_MAX_BRW_SIZE   (PTLRPC_MAX_BRW_PAGES * PAGE_SIZE)
78 #else
79 # undef  PTLRPC_MAX_BRW_PAGES
80 # define PTLRPC_MAX_BRW_PAGES  (PTLRPC_MAX_BRW_SIZE / PAGE_SIZE)
81 #endif
82
83 #if ((PTLRPC_MAX_BRW_PAGES & (PTLRPC_MAX_BRW_PAGES - 1)) != 0)
84 #error "PTLRPC_MAX_BRW_PAGES isn't a power of two"
85 #endif
86
87 /* Size over which to OBD_VMALLOC() rather than OBD_ALLOC() service request
88  * buffers */
89 #define SVC_BUF_VMALLOC_THRESHOLD (2*PAGE_SIZE)
90
91 /* The following constants determine how memory is used to buffer incoming
92  * service requests.
93  *
94  * ?_NBUFS              # buffers to allocate when growing the pool
95  * ?_BUFSIZE            # bytes in a single request buffer
96  * ?_MAXREQSIZE         # maximum request service will receive
97  *
98  * When fewer than ?_NBUFS/2 buffers are posted for receive, another chunk
99  * of ?_NBUFS is added to the pool.
100  *
101  * Messages larger than ?_MAXREQSIZE are dropped.  Request buffers are
102  * considered full when less than ?_MAXREQSIZE is left in them.
103  */
104
105 #define LDLM_NUM_THREADS        min(smp_num_cpus * smp_num_cpus * 8, 64)
106 #define LDLM_NBUFS       64
107 #define LDLM_BUFSIZE    (8 * 1024)
108 #define LDLM_MAXREQSIZE (5 * 1024)
109
110 #define MDT_MAX_THREADS 32UL
111 #define MDT_NUM_THREADS max(min_t(unsigned long, num_physpages / 8192, \
112                                   MDT_MAX_THREADS), 2UL)
113 #define MDS_NBUFS       (64 * smp_num_cpus)
114 #define MDS_BUFSIZE     (8 * 1024)
115 /* Assume file name length = FNAME_MAX = 256 (true for extN).
116  *        path name length = PATH_MAX = 4096
117  *        LOV MD size max  = EA_MAX = 4000
118  * symlink:  FNAME_MAX + PATH_MAX  <- largest
119  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
120  * rename:   FNAME_MAX + FNAME_MAX
121  * open:     FNAME_MAX + EA_MAX
122  *
123  * MDS_MAXREQSIZE ~= 4736 bytes =
124  * lustre_msg + ldlm_request + mds_body + mds_rec_create + FNAME_MAX + PATH_MAX
125  *
126  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
127  * except in the open case where there are a large number of OSTs in a LOV.
128  */
129 #define MDS_MAXREQSIZE  (5 * 1024)
130
131 #define OST_MAX_THREADS 36UL
132 #define OST_NUM_THREADS max(min_t(unsigned long, num_physpages / 8192, \
133                                   OST_MAX_THREADS), 2UL)
134 #define OST_NBUFS       (64 * smp_num_cpus)
135 #define OST_BUFSIZE     (8 * 1024)
136 /* OST_MAXREQSIZE ~= 1640 bytes =
137  * lustre_msg + obdo + 16 * obd_ioobj + 64 * niobuf_remote
138  *
139  * - single object with 16 pages is 512 bytes
140  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
141  */
142 #define OST_MAXREQSIZE  (5 * 1024)
143
144 #define PTLBD_NUM_THREADS        4
145 #define PTLBD_NBUFS      64 
146 #define PTLBD_BUFSIZE    (32 * 1024)
147 #define PTLBD_MAXREQSIZE 1024
148
149 struct ptlrpc_peer {
150         ptl_process_id_t  peer_id;
151         struct ptlrpc_ni *peer_ni;
152 };
153
154 struct ptlrpc_connection {
155         struct list_head        c_link;
156         struct ptlrpc_peer      c_peer;
157         struct obd_uuid         c_remote_uuid;
158         atomic_t                c_refcount;
159 };
160
161 struct ptlrpc_client {
162         __u32                     cli_request_portal;
163         __u32                     cli_reply_portal;
164         char                     *cli_name;
165 };
166
167 /* state flags of requests */
168 /* XXX only ones left are those used by the bulk descs as well! */
169 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
170 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
171
172 #define REQ_MAX_ACK_LOCKS 8
173
174 #define SWAB_PARANOIA 1
175 #if SWAB_PARANOIA
176 /* unpacking: assert idx not unpacked already */
177 #define LASSERT_REQSWAB(rq, idx)                                \
178 do {                                                            \
179         LASSERT ((idx) < sizeof ((rq)->rq_req_swab_mask) * 8);  \
180         LASSERT (((rq)->rq_req_swab_mask & (1 << (idx))) == 0); \
181         (rq)->rq_req_swab_mask |= (1 << (idx));                 \
182 } while (0)
183
184 #define LASSERT_REPSWAB(rq, idx)                                \
185 do {                                                            \
186         LASSERT ((idx) < sizeof ((rq)->rq_rep_swab_mask) * 8);  \
187         LASSERT (((rq)->rq_rep_swab_mask & (1 << (idx))) == 0); \
188         (rq)->rq_rep_swab_mask |= (1 << (idx));                 \
189 } while (0)
190
191 /* just looking: assert idx already unpacked */
192 #define LASSERT_REQSWABBED(rq, idx)                     \
193 LASSERT ((idx) < sizeof ((rq)->rq_req_swab_mask) * 8 && \
194          ((rq)->rq_req_swab_mask & (1 << (idx))) != 0)
195
196 #define LASSERT_REPSWABBED(rq, idx)                     \
197 LASSERT ((idx) < sizeof ((rq)->rq_rep_swab_mask) * 8 && \
198          ((rq)->rq_rep_swab_mask & (1 << (idx))) != 0)
199 #else
200 #define LASSERT_REQSWAB(rq, idx)
201 #define LASSERT_REPSWAB(rq, idx)
202 #define LASSERT_REQSWABBED(rq, idx)
203 #define LASSERT_REPSWABBED(rq, idx)
204 #endif
205
206 union ptlrpc_async_args {
207         /* Scratchpad for passing args to completion interpreter. Users
208          * cast to the struct of their choosing, and LASSERT that this is
209          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
210          * a pointer to it here.  The pointer_arg ensures this struct is at
211          * least big enough for that. */
212         void      *pointer_arg[9];
213         __u64      space[4];
214 };
215
216 struct ptlrpc_request_set;
217 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
218
219 struct ptlrpc_request_set {
220         int               set_remaining; /* # uncompleted requests */
221         wait_queue_head_t set_waitq;
222         wait_queue_head_t *set_wakeup_ptr;
223         struct list_head  set_requests;
224         set_interpreter_func    set_interpret; /* completion callback */
225         void              *set_arg; /* completion context */
226         /* locked so that any old caller can communicate requests to
227          * the set holder who can then fold them into the lock-free set */
228         spinlock_t        set_new_req_lock;
229         struct list_head  set_new_requests;
230 };
231
232 struct ptlrpc_bulk_desc;
233
234 /*
235  * ptlrpc callback & work item stuff
236  */
237 struct ptlrpc_cb_id {
238         void   (*cbid_fn)(ptl_event_t *ev);     /* specific callback fn */
239         void    *cbid_arg;                      /* additional arg */
240 };
241
242 struct ptlrpc_cred;
243 struct ptlrpc_svcsec;
244
245 #define RS_MAX_LOCKS 4
246 #define RS_DEBUG     1
247
248 struct ptlrpc_reply_state {
249         struct ptlrpc_cb_id   rs_cb_id;
250         struct list_head      rs_list;
251         struct list_head      rs_exp_list;
252         struct list_head      rs_obd_list;
253 #if RS_DEBUG
254         struct list_head      rs_debug_list;
255 #endif
256         /* updates to following flag serialised by srv_request_lock */
257         unsigned int          rs_difficult:1;   /* ACK/commit stuff */
258         unsigned int          rs_scheduled:1;   /* being handled? */
259         unsigned int          rs_scheduled_ever:1; /* any schedule attempts? */
260         unsigned int          rs_handled:1;     /* been handled yet? */
261         unsigned int          rs_on_net:1;      /* reply_out_callback pending? */
262
263         struct ptlrpc_svcsec *rs_svcsec;
264         char                 *rs_buf;           /* backend buffer */
265         int                   rs_buf_len;       /* backend buffer length */
266         char                 *rs_repbuf;        /* will be sent on wire */
267         int                   rs_repbuf_len;    /* max on-wire data length */
268         int                   rs_repdata_len;   /* actual on-wire data length */
269         struct lustre_msg    *rs_msg;           /* lustre msg pointer */
270         int                   rs_msg_len;       /* length of lustre msg */
271
272         __u64                 rs_transno;
273         __u64                 rs_xid;
274         struct obd_export    *rs_export;
275         struct ptlrpc_srv_ni *rs_srv_ni;
276         ptl_handle_md_t       rs_md_h;
277
278         /* locks awaiting client reply ACK */
279         int                   rs_nlocks;
280         struct lustre_handle  rs_locks[RS_MAX_LOCKS];
281         ldlm_mode_t           rs_modes[RS_MAX_LOCKS];
282         struct llog_create_locks *rs_llog_locks;
283 };
284
285 struct ptlrpc_request {
286         int rq_type; /* one of PTL_RPC_MSG_* */
287         struct list_head rq_list;
288         int rq_status;
289         spinlock_t rq_lock;
290         /* client-side flags */
291         unsigned int rq_intr:1, rq_replied:1, rq_err:1,
292                 rq_timedout:1, rq_resend:1, rq_restart:1, rq_replay:1,
293                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
294                 rq_no_delay:1, rq_net_err:1, rq_req_wrapped:1,
295                 rq_ptlrpcs_restart:1, rq_ptlrpcs_err:1;
296         int rq_phase;
297         /* client-side refcount for SENT race */
298         atomic_t rq_refcount;
299
300         int rq_request_portal; /* XXX FIXME bug 249 */
301         int rq_reply_portal; /* XXX FIXME bug 249 */
302
303         /* client-side # reply bytes actually received  */
304         int rq_nob_received;
305
306         int rq_reqlen;
307         struct lustre_msg *rq_reqmsg;
308
309         int rq_timeout;
310         int rq_replen;
311         struct lustre_msg *rq_repmsg;
312         __u64 rq_transno;
313         __u64 rq_xid;
314         struct list_head rq_replay_list;
315
316         struct ptlrpc_cred   *rq_cred;        /* client side credit */
317         struct ptlrpc_svcsec *rq_svcsec;      /* server side security */
318         /* XXX temporarily put here XXX */
319         void                 *rq_sec_svcdata; /* server security data */
320         unsigned int          rq_remote_realm;/* from remote realm */
321         uid_t                 rq_auth_uid;
322         uid_t                 rq_mapped_uid;
323
324         char *rq_reqbuf;       /* backend request buffer */
325         int   rq_reqbuf_len;   /* backend request buffer length */
326         int   rq_reqdata_len;  /* actual request data length */
327         char *rq_repbuf;       /* backend reply buffer */
328         int   rq_repbuf_len;   /* backend reply buffer length */
329         int   rq_repdata_len;  /* actual reply data length, not used yet */
330
331 #if SWAB_PARANOIA
332         __u32 rq_req_swab_mask;
333         __u32 rq_rep_swab_mask;
334 #endif
335
336         int rq_import_generation;
337         enum lustre_imp_state rq_send_state;
338
339         /* client+server request */
340         ptl_handle_md_t      rq_req_md_h;
341         struct ptlrpc_cb_id  rq_req_cbid;
342
343         /* client-side... */
344         struct timeval                     rq_rpcd_start;
345
346         /* server-side... */
347         struct timeval                     rq_arrival_time; /* request arrival time */
348         struct ptlrpc_reply_state         *rq_reply_state; /* separated reply state */
349         struct ptlrpc_request_buffer_desc *rq_rqbd; /* incoming request buffer */
350 #if CRAY_PORTALS
351         ptl_uid_t                          rq_uid; /* peer uid, used in MDS only */
352 #endif
353         
354         /* client-only incoming reply */
355         ptl_handle_md_t      rq_reply_md_h;
356         wait_queue_head_t    rq_reply_waitq;
357         struct ptlrpc_cb_id  rq_reply_cbid;
358         
359         struct ptlrpc_peer rq_peer; /* XXX see service.c can this be factored away? */
360         char               rq_peerstr[PTL_NALFMT_SIZE];
361         struct obd_export *rq_export;
362         struct obd_import *rq_import;
363         
364         void (*rq_replay_cb)(struct ptlrpc_request *);
365         void (*rq_commit_cb)(struct ptlrpc_request *);
366         void  *rq_cb_data;
367
368         struct ptlrpc_bulk_desc *rq_bulk;       /* client side bulk */
369         time_t rq_sent;                         /* when the request was sent */
370
371         struct ptlrpc_service   *rq_svc;
372
373         /* Multi-rpc bits */
374         struct list_head rq_set_chain;
375         struct ptlrpc_request_set *rq_set;
376         void *rq_interpret_reply;               /* Async completion handler */
377         union ptlrpc_async_args rq_async_args;  /* Async completion context */
378         void * rq_ptlrpcd_data;
379 };
380
381
382 #define RQ_PHASE_NEW           0xebc0de00
383 #define RQ_PHASE_RPC           0xebc0de01
384 #define RQ_PHASE_BULK          0xebc0de02
385 #define RQ_PHASE_INTERPRET     0xebc0de03
386 #define RQ_PHASE_COMPLETE      0xebc0de04
387
388 /* Spare the preprocessor, spoil the bugs. */
389 #define FLAG(field, str) (field ? str : "")
390
391 #define DEBUG_REQ_FLAGS(req)                                                    \
392         ((req->rq_phase == RQ_PHASE_NEW) ? "New" :                              \
393          (req->rq_phase == RQ_PHASE_RPC) ? "Rpc" :                              \
394          (req->rq_phase == RQ_PHASE_INTERPRET) ? "Interpret" :                  \
395          (req->rq_phase == RQ_PHASE_COMPLETE) ? "Complete" : "?phase?"),        \
396         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                    \
397         FLAG(req->rq_err, "E"),                                                 \
398         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"),   \
399         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                  \
400         FLAG(req->rq_no_resend, "N"),                                           \
401         FLAG(req->rq_waiting, "W")
402
403 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s"
404
405 #define DEBUG_REQ(level, req, fmt, args...)                                    \
406 do {                                                                           \
407 CDEBUG(level, "@@@ " fmt                                                       \
408        " req@%p x"LPD64"/t"LPD64" o%d->%s@%s:%d lens %d/%d ref %d fl "         \
409        REQ_FLAGS_FMT"/%x/%x rc %d/%d\n" , ## args, req, req->rq_xid,           \
410        req->rq_transno,                                                        \
411        req->rq_reqmsg ? req->rq_reqmsg->opc : -1,                              \
412        req->rq_import ? (char *)req->rq_import->imp_target_uuid.uuid : "<?>",  \
413        req->rq_import ?                                                        \
414           (char *)req->rq_import->imp_connection->c_remote_uuid.uuid : "<?>",  \
415        (req->rq_import && req->rq_import->imp_client) ?                        \
416            req->rq_import->imp_client->cli_request_portal : -1,                \
417        req->rq_reqlen, req->rq_replen,                                         \
418        atomic_read(&req->rq_refcount),                                         \
419        DEBUG_REQ_FLAGS(req),                                                   \
420        req->rq_reqmsg ? req->rq_reqmsg->flags : 0,                             \
421        req->rq_repmsg ? req->rq_repmsg->flags : 0,                             \
422        req->rq_status, req->rq_repmsg ? req->rq_repmsg->status : 0);           \
423 } while (0)
424
425 struct ptlrpc_bulk_page {
426         struct list_head bp_link;
427         int bp_buflen;
428         int bp_pageoffset;                      /* offset within a page */
429         struct page *bp_page;
430 };
431
432 #define BULK_GET_SOURCE   0
433 #define BULK_PUT_SINK     1
434 #define BULK_GET_SINK     2
435 #define BULK_PUT_SOURCE   3
436
437 struct ptlrpc_bulk_desc {
438         unsigned int bd_success:1;              /* completed successfully */
439         unsigned int bd_network_rw:1;           /* accessible to the network */
440         unsigned int bd_type:2;                 /* {put,get}{source,sink} */
441         unsigned int bd_registered:1;           /* client side */
442         spinlock_t   bd_lock;                   /* serialise with callback */
443         int bd_import_generation;
444         struct obd_export *bd_export;
445         struct obd_import *bd_import;
446         __u32 bd_portal;
447         struct ptlrpc_request *bd_req;          /* associated request */
448         wait_queue_head_t      bd_waitq;        /* server side only WQ */
449         int                    bd_iov_count;    /* # entries in bd_iov */
450         int                    bd_max_iov;      /* allocated size of bd_iov */
451         int                    bd_nob;          /* # bytes covered */
452         int                    bd_nob_transferred; /* # bytes GOT/PUT */
453
454         __u64                  bd_last_xid;
455
456         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
457         ptl_handle_md_t        bd_md_h;         /* associated MD */
458         
459 #if (!CRAY_PORTALS && defined(__KERNEL__))
460         ptl_kiov_t             bd_iov[0];
461 #else
462         ptl_md_iovec_t         bd_iov[0];
463 #endif
464 };
465
466 struct ptlrpc_thread {
467         struct list_head t_link;
468
469         __u32 t_flags;
470         wait_queue_head_t t_ctl_waitq;
471 };
472
473 struct ptlrpc_request_buffer_desc {
474         struct list_head       rqbd_list;
475         struct ptlrpc_srv_ni  *rqbd_srv_ni;
476         ptl_handle_md_t        rqbd_md_h;
477         int                    rqbd_refcount;
478         char                  *rqbd_buffer;
479         struct ptlrpc_cb_id    rqbd_cbid;
480         struct ptlrpc_request  rqbd_req;
481 };
482
483 /* event queues are per-ni, because one day we may get a hardware
484  * supported NAL that delivers events asynchonously wrt kernel portals
485  * into the eq.
486  */
487 struct ptlrpc_ni { /* Generic interface state */
488         char                   *pni_name;
489         int                     pni_number;
490         ptl_handle_ni_t         pni_ni_h;
491         ptl_handle_eq_t         pni_eq_h;
492 };
493
494 struct ptlrpc_srv_ni {
495         /* Interface-specific service state */
496         struct ptlrpc_service  *sni_service;    /* owning service */
497         struct ptlrpc_ni       *sni_ni;         /* network interface */
498         struct list_head        sni_active_rqbds;   /* req buffers receiving */
499         struct list_head        sni_active_replies; /* all the active replies */
500         int                     sni_nrqbd_receiving; /* # posted request buffers */
501 };
502
503 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
504
505 struct ptlrpc_service {
506         struct list_head srv_list;              /* chain thru all services */
507         int              srv_max_req_size;      /* biggest request to receive */
508         int              srv_buf_size;          /* size of individual buffers */
509         int              srv_nbuf_per_group;    /* # buffers to allocate in 1 group */
510         int              srv_nbufs;             /* total # req buffer descs allocated */
511         int              srv_nthreads;          /* # running threads */
512         int              srv_n_difficult_replies; /* # 'difficult' replies */
513         int              srv_n_active_reqs;     /* # reqs being served */
514         int              srv_rqbd_timeout;      /* timeout before re-posting reqs */
515         int              srv_watchdog_timeout; /* soft watchdog timeout, in ms */
516
517         __u32 srv_req_portal;
518         __u32 srv_rep_portal;
519
520         int               srv_n_queued_reqs;    /* # reqs waiting to be served */
521         struct list_head  srv_request_queue;    /* reqs waiting for service */
522
523         struct list_head  srv_idle_rqbds;       /* request buffers to be reposted */
524
525         atomic_t          srv_outstanding_replies;
526         struct list_head  srv_reply_queue;      /* replies waiting for service */
527
528         wait_queue_head_t srv_waitq; /* all threads sleep on this */
529
530         struct list_head   srv_threads;
531         struct obd_device *srv_obddev;
532         svc_handler_t      srv_handler;
533         
534         char *srv_name;  /* only statically allocated strings here; we don't clean them */
535
536         spinlock_t               srv_lock;
537
538         struct proc_dir_entry   *srv_procroot;
539         struct lprocfs_stats    *srv_stats;
540
541         struct ptlrpc_srv_ni srv_interfaces[0];
542 };
543
544 static inline char *ptlrpc_peernid2str(struct ptlrpc_peer *p, char *str)
545 {
546         LASSERT(p->peer_ni != NULL);
547         return (portals_nid2str(p->peer_ni->pni_number, p->peer_id.nid, str));
548 }
549
550 static inline char *ptlrpc_id2str(struct ptlrpc_peer *p, char *str)
551 {
552         LASSERT(p->peer_ni != NULL);
553         return (portals_id2str(p->peer_ni->pni_number, p->peer_id, str));
554 }
555
556
557 /* ptlrpc/events.c */
558 extern struct ptlrpc_ni ptlrpc_interfaces[];
559 extern int              ptlrpc_ninterfaces;
560 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid, struct ptlrpc_peer *peer);
561 extern void request_out_callback (ptl_event_t *ev);
562 extern void rawrpc_request_out_callback(ptl_event_t *ev);
563 extern void reply_in_callback(ptl_event_t *ev);
564 extern void client_bulk_callback (ptl_event_t *ev);
565 extern void request_in_callback(ptl_event_t *ev);
566 extern void reply_out_callback(ptl_event_t *ev);
567 extern void server_bulk_callback (ptl_event_t *ev);
568 extern int ptlrpc_default_nal(void);
569
570 /* ptlrpc/connection.c */
571 void ptlrpc_dump_connections(void);
572 void ptlrpc_readdress_connection(struct ptlrpc_connection *, struct obd_uuid *);
573 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
574                                                 struct obd_uuid *uuid);
575 int ptlrpc_put_connection(struct ptlrpc_connection *c);
576 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
577 void ptlrpc_init_connection(void);
578 void ptlrpc_cleanup_connection(void);
579 extern ptl_pid_t ptl_get_pid(void);
580
581 /* ptlrpc/niobuf.c */
582 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
583 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
584 int ptlrpc_register_bulk(struct ptlrpc_request *req);
585 void ptlrpc_unregister_bulk (struct ptlrpc_request *req);
586
587 static inline int ptlrpc_bulk_active (struct ptlrpc_bulk_desc *desc) 
588 {
589         unsigned long flags;
590         int           rc;
591
592         spin_lock_irqsave (&desc->bd_lock, flags);
593         rc = desc->bd_network_rw;
594         spin_unlock_irqrestore (&desc->bd_lock, flags);
595         return (rc);
596 }
597
598 int ptlrpc_send_reply(struct ptlrpc_request *req, int);
599 int ptlrpc_reply(struct ptlrpc_request *req);
600 int ptlrpc_error(struct ptlrpc_request *req);
601 void ptlrpc_resend_req(struct ptlrpc_request *request);
602 int ptl_send_rpc(struct ptlrpc_request *request);
603 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd);
604
605 struct ptlrpc_request * ptl_do_rawrpc(struct obd_import *imp,
606                                       char *reqbuf, int reqbuf_len, int reqlen,
607                                       char *repbuf, int repbuf_len,
608                                       int *replenp, int timeout, int *res);
609 int ptl_do_rawrpc_simple(struct obd_import *imp,
610                          char *reqbuf, int reqlen,
611                          char *repbuf, int *replenp);
612 void rawrpc_req_finished(struct ptlrpc_request *req);
613
614 /* ptlrpc/client.c */
615 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
616                         struct ptlrpc_client *);
617 void ptlrpc_cleanup_client(struct obd_import *imp);
618 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
619
620 static inline int
621 ptlrpc_client_receiving_reply (struct ptlrpc_request *req)
622 {
623         unsigned long flags;
624         int           rc;
625         
626         spin_lock_irqsave(&req->rq_lock, flags);
627         rc = req->rq_receiving_reply;
628         spin_unlock_irqrestore(&req->rq_lock, flags);
629         return (rc);
630 }
631
632 static inline int
633 ptlrpc_client_replied (struct ptlrpc_request *req)
634 {
635         unsigned long flags;
636         int           rc;
637         
638         spin_lock_irqsave(&req->rq_lock, flags);
639         rc = req->rq_replied;
640         spin_unlock_irqrestore(&req->rq_lock, flags);
641         return (rc);
642 }
643
644 static inline void
645 ptlrpc_wake_client_req (struct ptlrpc_request *req)
646 {
647         if (req->rq_set == NULL)
648                 wake_up(&req->rq_reply_waitq);
649         else
650                 wake_up(&req->rq_set->set_waitq);
651 }
652
653 int ptlrpc_queue_wait(struct ptlrpc_request *req);
654 int ptlrpc_replay_req(struct ptlrpc_request *req);
655 void ptlrpc_unregister_reply(struct ptlrpc_request *req);
656 void ptlrpc_restart_req(struct ptlrpc_request *req);
657 void ptlrpc_abort_inflight(struct obd_import *imp);
658
659 struct ptlrpc_request_set *ptlrpc_prep_set(void);
660 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
661 int ptlrpc_check_set(struct ptlrpc_request_set *set);
662 int ptlrpc_set_wait(struct ptlrpc_request_set *);
663 int ptlrpc_expired_set(void *data);
664 void ptlrpc_interrupted_set(void *data);
665 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
666 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
667 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
668 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *,
669                             struct ptlrpc_request *);
670
671 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
672                                        int opcode, int count, int *lengths,
673                                        char **bufs);
674 void ptlrpc_free_req(struct ptlrpc_request *request);
675 void ptlrpc_req_finished(struct ptlrpc_request *request);
676 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
677 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
678 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
679                                                int npages, int type, int portal);
680 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
681                                               int npages, int type, int portal);
682 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
683 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
684                            struct page *page, int pageoffset, int len);
685 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
686                                       struct obd_import *imp);
687 __u64 ptlrpc_next_xid(void);
688
689 /* ptlrpc/service.c */
690 void ptlrpc_require_repack (struct ptlrpc_request *req);
691 void ptlrpc_save_lock (struct ptlrpc_request *req, 
692                        struct lustre_handle *lock, int mode);
693 void ptlrpc_save_llog_lock (struct ptlrpc_request *req, 
694                             struct llog_create_locks *lcl);
695 void ptlrpc_commit_replies (struct obd_device *obd);
696 void ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs);
697 struct ptlrpc_service *ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
698                                        int req_portal, int rep_portal,
699                                        int watchdog_timeout, /* in ms */
700                                        svc_handler_t, char *name,
701                                        struct proc_dir_entry *proc_entry);
702 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
703 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
704                            int cnt, char *base_name);
705 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
706                         char *name);
707 int ptlrpc_unregister_service(struct ptlrpc_service *service);
708 int liblustre_check_services (void *arg);
709 void ptlrpc_daemonize(void);
710
711
712 struct ptlrpc_svc_data {
713         char *name;
714         struct ptlrpc_service *svc;
715         struct ptlrpc_thread *thread;
716         struct obd_device *dev;
717 };
718
719 /* ptlrpc/import.c */
720 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid);
721 int ptlrpc_init_import(struct obd_import *imp);
722 int ptlrpc_disconnect_import(struct obd_import *imp);
723 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
724
725 /* ptlrpc/pack_generic.c */
726 int lustre_msg_swabbed(struct lustre_msg *msg);
727 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
728 int lustre_secdesc_size(void);
729 void lustre_pack_secdesc(struct ptlrpc_request *req, int size);
730 int lustre_pack_request(struct ptlrpc_request *, int count, int *lens,
731                         char **bufs);
732 int lustre_pack_reply(struct ptlrpc_request *, int count, int *lens,
733                       char **bufs);
734 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
735 int lustre_msg_size(int count, int *lengths);
736 int lustre_unpack_msg(struct lustre_msg *m, int len);
737 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
738 char *lustre_msg_string (struct lustre_msg *m, int n, int max_len);
739 void *lustre_swab_buf(struct lustre_msg *, int n, int minlen, void *swabber);
740 void *lustre_swab_reqbuf (struct ptlrpc_request *req, int n, int minlen,
741                           void *swabber);
742 void *lustre_swab_repbuf (struct ptlrpc_request *req, int n, int minlen,
743                           void *swabber);
744
745 void lustre_init_msg (struct lustre_msg *msg, int count, 
746                       int *lens, char **bufs);
747 void *mdc_setattr_pack(struct lustre_msg *msg, int offset,
748                        struct mdc_op_data *data, struct iattr *iattr,
749                        void *ea, int ealen, void *ea2, int ea2len);
750 void *mdc_create_pack(struct lustre_msg *msg, int offset,
751                       struct mdc_op_data *op_data, __u32 mode,
752                       __u64 rdev, const void *data, int datalen);
753 void *mdc_unlink_pack(struct lustre_msg *msg, int offset,
754                       struct mdc_op_data *data);
755 void *mdc_link_pack(struct lustre_msg *msg, int offset,
756                     struct mdc_op_data *data);
757 void *mdc_rename_pack(struct lustre_msg *msg, int offset,
758                       struct mdc_op_data *data,
759                       const char *old, int oldlen,
760                       const char *new, int newlen);
761
762 /* lustre id helper functions and macros. */
763 static inline
764 void mdc_pack_id(struct lustre_id *id, obd_id ino, 
765                  __u32 gen, int type, __u64 mds, 
766                  __u64 fid)
767 {
768         LASSERT(id != NULL);
769
770         id->li_fid.lf_id = fid;
771         id->li_fid.lf_group = mds;
772         
773         id->li_stc.u.e3s.l3s_ino = ino;
774         id->li_stc.u.e3s.l3s_gen = gen;
775         id->li_stc.u.e3s.l3s_type = type;
776 }
777
778 #define id_ino(id)                              \
779         (id)->li_stc.u.e3s.l3s_ino
780
781 #define id_gen(id)                              \
782         (id)->li_stc.u.e3s.l3s_gen
783
784 #define id_type(id)                             \
785         (id)->li_stc.u.e3s.l3s_type
786
787 #define id_fid(id)                              \
788         (id)->li_fid.lf_id
789
790 #define id_group(id)                            \
791         (id)->li_fid.lf_group
792
793 #define id_version(id)                          \
794         (id)->li_fid.lf_version
795
796 #define id_assign_fid(id1, id2)                 \
797         ((id1)->li_fid = (id2)->li_fid)
798
799 #define id_assign_stc(id1, id2)                 \
800         ((id1)->li_stc = (id2)->li_stc)
801
802 #define id_equal(id1, id2)                      \
803         (id_ino((id1)) == id_ino((id2)) &&      \
804          id_gen((id1)) == id_gen((id2)) &&      \
805          id_fid((id1)) == id_fid((id2)) &&      \
806          id_group((id1)) == id_group(id2))
807
808 #define id_equal_fid(id1, id2)                  \
809         (id_fid((id1)) == id_fid((id2)) &&      \
810          id_group((id1)) == id_group((id2)))
811
812 #define id_equal_stc(id1, id2)                  \
813         (id_ino((id1)) == id_ino((id2)) &&      \
814          id_gen((id1)) == id_gen((id2)))
815
816 #define id_le_to_cpu(id)                                                \
817         do {                                                            \
818                 id_fid((id)) = le64_to_cpu(id_fid((id)));               \
819                 id_group((id)) = le64_to_cpu(id_group((id)));           \
820                 id_version((id)) = le32_to_cpu(id_version((id)));       \
821                 id_ino((id)) = le64_to_cpu(id_ino((id)));               \
822                 id_gen((id)) = le32_to_cpu(id_gen((id)));               \
823                 id_type((id)) = le32_to_cpu(id_type((id)));             \
824         } while (0)
825
826 #define id_cpu_to_le(id)                                                \
827         do {                                                            \
828                 id_fid((id)) = cpu_to_le64(id_fid((id)));               \
829                 id_group((id)) = cpu_to_le64(id_group((id)));           \
830                 id_version((id)) = cpu_to_le32(id_version((id)));       \
831                 id_ino((id)) = cpu_to_le64(id_ino((id)));               \
832                 id_gen((id)) = cpu_to_le32(id_gen((id)));               \
833                 id_type((id)) = cpu_to_le32(id_type((id)));             \
834         } while (0)
835
836 #ifdef __KERNEL__
837 static inline void
838 mdc_inode2id(struct lustre_id *id, struct inode *inode)
839 {
840         mdc_pack_id(id, inode->i_ino, inode->i_generation,
841                     (inode->i_mode & S_IFMT), 0, 0);
842 }
843
844 static inline void 
845 mdc_prepare_mdc_data(struct mdc_op_data *data, struct inode *i1,
846                      struct inode *i2, const char *name, int namelen,
847                      int mode)
848 {
849         LASSERT(i1);
850
851         mdc_inode2id(&data->id1, i1);
852         if (i2)
853                 mdc_inode2id(&data->id2, i2);
854
855         data->valid = 0;
856         data->name = name;
857         data->namelen = namelen;
858         data->create_mode = mode;
859         data->mod_time = LTIME_S(CURRENT_TIME);
860 }
861 #endif
862
863 /* ldlm/ldlm_lib.c */
864 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
865 int client_obd_cleanup(struct obd_device * obddev, int flags);
866 int client_connect_import(struct lustre_handle *conn, struct obd_device *obd,
867                           struct obd_uuid *cluuid,
868                           struct obd_connect_data *conn_data,
869                           unsigned long);
870 int client_disconnect_export(struct obd_export *exp, unsigned long);
871
872 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
873                            int priority);
874 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
875 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
876
877
878 /* ptlrpc/pinger.c */
879 int ptlrpc_pinger_add_import(struct obd_import *imp);
880 int ptlrpc_pinger_del_import(struct obd_import *imp);
881 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req);
882
883 /* ptlrpc/ptlrpcd.c */
884 void ptlrpcd_wake(struct ptlrpc_request *req);
885 void ptlrpcd_add_req(struct ptlrpc_request *req);
886 int ptlrpcd_addref(void);
887 void ptlrpcd_decref(void);
888
889 /* ptlrpc/lproc_ptlrpc.c */
890 #ifdef __KERNEL__
891 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev);
892 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obddev);
893 #else
894 #define ptlrpc_lprocfs_register_obd(param...) do{}while(0)
895 #define ptlrpc_lprocfs_unregister_obd(param...) do{}while(0)
896 #endif
897
898 /* ptlrpc/llog_server.c */
899 int llog_origin_handle_open(struct ptlrpc_request *req);
900 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
901 int llog_origin_handle_next_block(struct ptlrpc_request *req);
902 int llog_origin_handle_read_header(struct ptlrpc_request *req);
903 int llog_origin_handle_close(struct ptlrpc_request *req);
904 int llog_origin_handle_cancel(struct ptlrpc_request *req);
905 int llog_catinfo(struct ptlrpc_request *req);
906
907 /* ptlrpc/llog_client.c */
908 extern struct llog_operations llog_client_ops;
909
910 #endif