Whamcloud - gitweb
242c160bed0f2bca22d411afe05b02397c5bb796
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef _LUSTRE_NET_H
38 #define _LUSTRE_NET_H
39
40 #if defined(__linux__)
41 #include <linux/lustre_net.h>
42 #elif defined(__APPLE__)
43 #include <darwin/lustre_net.h>
44 #elif defined(__WINNT__)
45 #include <winnt/lustre_net.h>
46 #else
47 #error Unsupported operating system.
48 #endif
49
50 #include <libcfs/kp30.h>
51 // #include <obd.h>
52 #include <lnet/lnet.h>
53 #include <lustre/lustre_idl.h>
54 #include <lustre_ha.h>
55 #include <lustre_import.h>
56 #include <lprocfs_status.h>
57
58 #include <obd_support.h>
59
60 /* MD flags we _always_ use */
61 #define PTLRPC_MD_OPTIONS  0
62
63 /* Define maxima for bulk I/O
64  * CAVEAT EMPTOR, with multinet (i.e. routers forwarding between networks)
65  * these limits are system wide and not interface-local. */
66 #define PTLRPC_MAX_BRW_BITS     LNET_MTU_BITS
67 #define PTLRPC_MAX_BRW_SIZE     (1<<LNET_MTU_BITS)
68 #define PTLRPC_MAX_BRW_PAGES    (PTLRPC_MAX_BRW_SIZE >> CFS_PAGE_SHIFT)
69
70 /* When PAGE_SIZE is a constant, we can check our arithmetic here with cpp! */
71 #ifdef __KERNEL__
72 # if ((PTLRPC_MAX_BRW_PAGES & (PTLRPC_MAX_BRW_PAGES - 1)) != 0)
73 #  error "PTLRPC_MAX_BRW_PAGES isn't a power of two"
74 # endif
75 # if (PTLRPC_MAX_BRW_SIZE != (PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE))
76 #  error "PTLRPC_MAX_BRW_SIZE isn't PTLRPC_MAX_BRW_PAGES * CFS_PAGE_SIZE"
77 # endif
78 # if (PTLRPC_MAX_BRW_SIZE > LNET_MTU)
79 #  error "PTLRPC_MAX_BRW_SIZE too big"
80 # endif
81 # if (PTLRPC_MAX_BRW_PAGES > LNET_MAX_IOV)
82 #  error "PTLRPC_MAX_BRW_PAGES too big"
83 # endif
84 #endif /* __KERNEL__ */
85
86 /* Size over which to OBD_VMALLOC() rather than OBD_ALLOC() service request
87  * buffers */
88 #define SVC_BUF_VMALLOC_THRESHOLD (2 * CFS_PAGE_SIZE)
89
90 /* The following constants determine how memory is used to buffer incoming
91  * service requests.
92  *
93  * ?_NBUFS              # buffers to allocate when growing the pool
94  * ?_BUFSIZE            # bytes in a single request buffer
95  * ?_MAXREQSIZE         # maximum request service will receive
96  *
97  * When fewer than ?_NBUFS/2 buffers are posted for receive, another chunk
98  * of ?_NBUFS is added to the pool.
99  *
100  * Messages larger than ?_MAXREQSIZE are dropped.  Request buffers are
101  * considered full when less than ?_MAXREQSIZE is left in them.
102  */
103
104 #define LDLM_THREADS_AUTO_MIN                                                 \
105         min((int)(num_online_cpus() * num_online_cpus() * 2), 8)
106 #define LDLM_THREADS_AUTO_MAX (LDLM_THREADS_AUTO_MIN * 16)
107 #define LDLM_BL_THREADS  LDLM_THREADS_AUTO_MIN
108 #define LDLM_NBUFS      (64 * num_online_cpus())
109 #define LDLM_BUFSIZE    (8 * 1024)
110 #define LDLM_MAXREQSIZE (5 * 1024)
111 #define LDLM_MAXREPSIZE (1024)
112
113 /* Absolute limits */
114 #define MDS_THREADS_MIN 2
115 #define MDS_THREADS_MAX 512
116 #define MDS_THREADS_MIN_READPAGE 2
117 #define MDS_NBUFS       (64 * num_online_cpus())
118 #define MDS_BUFSIZE     (8 * 1024)
119 /* Assume file name length = FNAME_MAX = 256 (true for ext3).
120  *        path name length = PATH_MAX = 4096
121  *        LOV MD size max  = EA_MAX = 4000
122  * symlink:  FNAME_MAX + PATH_MAX  <- largest
123  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
124  * rename:   FNAME_MAX + FNAME_MAX
125  * open:     FNAME_MAX + EA_MAX
126  *
127  * MDS_MAXREQSIZE ~= 4736 bytes =
128  * lustre_msg + ldlm_request + mds_body + mds_rec_create + FNAME_MAX + PATH_MAX
129  * MDS_MAXREPSIZE ~= 8300 bytes = lustre_msg + llog_header
130  * or, for mds_close() and mds_reint_unlink() on a many-OST filesystem:
131  *      = 9210 bytes = lustre_msg + mds_body + 160 * (easize + cookiesize)
132  *
133  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
134  * except in the open case where there are a large number of OSTs in a LOV.
135  */
136 #define MDS_MAXREQSIZE  (5 * 1024)
137 #define MDS_MAXREPSIZE  max(9 * 1024, 280 + LOV_MAX_STRIPE_COUNT * 56)
138
139 #define MGS_THREADS_AUTO_MIN 2
140 #define MGS_THREADS_AUTO_MAX 32
141 #define MGS_NBUFS       (64 * num_online_cpus())
142 #define MGS_BUFSIZE     (8 * 1024)
143 #define MGS_MAXREQSIZE  (8 * 1024)
144 #define MGS_MAXREPSIZE  (9 * 1024)
145
146 /* Absolute limits */
147 #define OSS_THREADS_MIN 3       /* difficult replies, HPQ, others */
148 #define OSS_THREADS_MAX 512
149 #define OST_NBUFS       (64 * num_online_cpus())
150 #define OST_BUFSIZE     (8 * 1024)
151 /* OST_MAXREQSIZE ~= 4768 bytes =
152  * lustre_msg + obdo + 16 * obd_ioobj + 256 * niobuf_remote
153  *
154  * - single object with 16 pages is 512 bytes
155  * - OST_MAXREQSIZE must be at least 1 page of cookies plus some spillover
156  */
157 #define OST_MAXREQSIZE  (5 * 1024)
158 #define OST_MAXREPSIZE  (9 * 1024)
159
160 /* Macro to hide a typecast. */
161 #define ptlrpc_req_async_args(req) ((void *)&req->rq_async_args)
162
163 struct ptlrpc_connection {
164         struct hlist_node       c_hash;
165         lnet_nid_t              c_self;
166         lnet_process_id_t       c_peer;
167         struct obd_uuid         c_remote_uuid;
168         atomic_t                c_refcount;
169 };
170
171 struct ptlrpc_client {
172         __u32                     cli_request_portal;
173         __u32                     cli_reply_portal;
174         char                     *cli_name;
175 };
176
177 /* state flags of requests */
178 /* XXX only ones left are those used by the bulk descs as well! */
179 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
180 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
181
182 #define REQ_MAX_ACK_LOCKS 8
183
184 union ptlrpc_async_args {
185         /* Scratchpad for passing args to completion interpreter. Users
186          * cast to the struct of their choosing, and LASSERT that this is
187          * big enough.  For _tons_ of context, OBD_ALLOC a struct and store
188          * a pointer to it here.  The pointer_arg ensures this struct is at
189          * least big enough for that. */
190         void      *pointer_arg[9];
191         __u64      space[4];
192 };
193
194 struct ptlrpc_request_set;
195 typedef int (*set_interpreter_func)(struct ptlrpc_request_set *, void *, int);
196
197 struct ptlrpc_request_set {
198         int               set_remaining; /* # uncompleted requests */
199         cfs_waitq_t       set_waitq;
200         cfs_waitq_t      *set_wakeup_ptr;
201         struct list_head  set_requests;
202         struct list_head  set_cblist; /* list of completion callbacks */
203         set_interpreter_func    set_interpret; /* completion callback */
204         void              *set_arg; /* completion context */
205         /* locked so that any old caller can communicate requests to
206          * the set holder who can then fold them into the lock-free set */
207         spinlock_t        set_new_req_lock;
208         struct list_head  set_new_requests;
209 };
210
211 struct ptlrpc_set_cbdata {
212         struct list_head        psc_item;
213         set_interpreter_func    psc_interpret;
214         void                   *psc_data;
215 };
216
217 struct ptlrpc_bulk_desc;
218
219 /*
220  * ptlrpc callback & work item stuff
221  */
222 struct ptlrpc_cb_id {
223         void   (*cbid_fn)(lnet_event_t *ev);     /* specific callback fn */
224         void    *cbid_arg;                      /* additional arg */
225 };
226
227 #define RS_MAX_LOCKS 4
228 #define RS_DEBUG     1
229
230 struct ptlrpc_reply_state {
231         struct ptlrpc_cb_id    rs_cb_id;
232         struct list_head       rs_list;
233         struct list_head       rs_exp_list;
234         struct list_head       rs_obd_list;
235 #if RS_DEBUG
236         struct list_head       rs_debug_list;
237 #endif
238         /* updates to following flag serialised by srv_request_lock */
239         unsigned long          rs_difficult:1;     /* ACK/commit stuff */
240         unsigned long          rs_scheduled:1;     /* being handled? */
241         unsigned long          rs_scheduled_ever:1;/* any schedule attempts? */
242         unsigned long          rs_handled:1;  /* been handled yet? */
243         unsigned long          rs_on_net:1;   /* reply_out_callback pending? */
244         unsigned long          rs_prealloc:1; /* rs from prealloc list */
245
246         int                    rs_size;
247         __u64                  rs_transno;
248         __u64                  rs_xid;
249         struct obd_export     *rs_export;
250         struct ptlrpc_service *rs_service;
251         lnet_handle_md_t       rs_md_h;
252         atomic_t               rs_refcount;
253
254         /* locks awaiting client reply ACK */
255         int                    rs_nlocks;
256         struct lustre_handle   rs_locks[RS_MAX_LOCKS];
257         ldlm_mode_t            rs_modes[RS_MAX_LOCKS];
258         /* last member: variable sized reply message */
259         struct lustre_msg     *rs_msg;
260 };
261
262 struct ptlrpc_thread;
263
264 enum rq_phase {
265         RQ_PHASE_NEW            = 0xebc0de00,
266         RQ_PHASE_RPC            = 0xebc0de01,
267         RQ_PHASE_BULK           = 0xebc0de02,
268         RQ_PHASE_INTERPRET      = 0xebc0de03,
269         RQ_PHASE_COMPLETE       = 0xebc0de04,
270         RQ_PHASE_UNREGISTERING  = 0xebc0de05,
271         RQ_PHASE_UNDEFINED      = 0xebc0de06
272 };
273
274 struct ptlrpc_request_pool {
275         spinlock_t prp_lock;
276         struct list_head prp_req_list;    /* list of ptlrpc_request structs */
277         int prp_rq_size;
278         void (*prp_populate)(struct ptlrpc_request_pool *, int);
279 };
280
281 struct ldlm_lock;
282
283 struct ptlrpc_hpreq_ops {
284         /**
285          * Check if the lock handle of the given lock is the same as
286          * taken from the request.
287          */
288         int  (*hpreq_lock_match)(struct ptlrpc_request *, struct ldlm_lock *);
289         /**
290          * Check if the request is a high priority one.
291          */
292         int  (*hpreq_check)(struct ptlrpc_request *);
293 };
294
295 struct ptlrpc_request {
296         int rq_type; /* one of PTL_RPC_MSG_* */
297         struct list_head rq_list;
298         struct list_head rq_timed_list;         /* server-side early replies */
299         struct list_head rq_history_list;       /* server-side history */
300         struct list_head rq_exp_list;           /* server-side per-export list */
301         struct ptlrpc_hpreq_ops *rq_ops;        /* server-side hp handlers */
302
303         __u64            rq_history_seq;        /* history sequence # */
304         /* the index of service's srv_at_array into which request is linked */
305         time_t rq_at_index;
306         int rq_status;
307         spinlock_t rq_lock;
308         /* client-side flags are serialized by rq_lock */
309         unsigned long rq_intr:1, rq_replied:1, rq_err:1,
310                 rq_timedout:1, rq_resend:1, rq_restart:1,
311                 /*
312                  * when ->rq_replay is set, request is kept by the client even
313                  * after server commits corresponding transaction. This is
314                  * used for operations that require sequence of multiple
315                  * requests to be replayed. The only example currently is file
316                  * open/close. When last request in such a sequence is
317                  * committed, ->rq_replay is cleared on all requests in the
318                  * sequence.
319                  */
320                 rq_replay:1,
321                 rq_no_resend:1, rq_waiting:1, rq_receiving_reply:1,
322                 rq_no_delay:1, rq_net_err:1, rq_early:1, rq_must_unlink:1,
323                 /* server-side flags */
324                 rq_packed_final:1,  /* packed final reply */
325                 rq_sent_final:1,    /* stop sending early replies */
326                 rq_hp:1,            /* high priority RPC */
327                 rq_at_linked:1;     /* link into service's srv_at_array */
328         enum rq_phase rq_phase;     /* one of RQ_PHASE_* */
329         enum rq_phase rq_next_phase; /* one of RQ_PHASE_* to be used next */
330         atomic_t rq_refcount;   /* client-side refcount for SENT race,
331                                    server-side refcounf for multiple replies */
332
333         struct ptlrpc_thread *rq_svc_thread; /* initial thread servicing req */
334
335         int rq_request_portal;  /* XXX FIXME bug 249 */
336         int rq_reply_portal;    /* XXX FIXME bug 249 */
337
338         int rq_nob_received; /* client-side # reply bytes actually received  */
339
340         int rq_reqlen;
341         struct lustre_msg *rq_reqmsg;
342
343         int rq_replen;
344         struct lustre_msg *rq_repbuf; /* client only, buf may be bigger than msg */
345         struct lustre_msg *rq_repmsg;
346         __u64 rq_transno;
347         __u64 rq_xid;
348         struct list_head rq_replay_list;
349
350         __u32 rq_req_swab_mask;
351         __u32 rq_rep_swab_mask;
352
353         int rq_import_generation;
354         enum lustre_imp_state rq_send_state;
355
356         int rq_early_count;           /* how many early replies (for stats) */
357
358         /* client+server request */
359         lnet_handle_md_t     rq_req_md_h;
360         struct ptlrpc_cb_id  rq_req_cbid;
361
362         /* server-side... */
363         struct timeval       rq_arrival_time;       /* request arrival time */
364         struct ptlrpc_reply_state *rq_reply_state;  /* separated reply state */
365         struct ptlrpc_request_buffer_desc *rq_rqbd; /* incoming request buffer*/
366 #ifdef CRAY_XT3
367         __u32                rq_uid;            /* peer uid, used in MDS only */
368 #endif
369
370         /* client-only incoming reply */
371         lnet_handle_md_t     rq_reply_md_h;
372         cfs_waitq_t          rq_reply_waitq;
373         struct ptlrpc_cb_id  rq_reply_cbid;
374
375         lnet_nid_t           rq_self;
376         lnet_process_id_t    rq_peer;
377         struct obd_export   *rq_export;
378         struct obd_import   *rq_import;
379
380         void (*rq_replay_cb)(struct ptlrpc_request *);
381         void (*rq_commit_cb)(struct ptlrpc_request *);
382         void  *rq_cb_data;
383
384         struct ptlrpc_bulk_desc *rq_bulk;       /* client side bulk */
385         /* client outgoing req */
386         time_t rq_sent;                         /* when request sent, seconds,
387                                                  * or time when request should
388                                                  * be sent */
389         volatile time_t rq_deadline;     /* when request must finish. volatile
390                so that servers' early reply updates to the deadline aren't
391                kept in per-cpu cache */
392         time_t rq_reply_deadline;        /* when req reply unlink must finish. */
393         time_t rq_bulk_deadline;         /* when req bulk unlink must finish. */
394         int    rq_timeout;               /* service time estimate (secs) */
395
396         /* Multi-rpc bits */
397         struct list_head rq_set_chain;
398         struct ptlrpc_request_set *rq_set;
399         void *rq_interpret_reply;               /* Async completion handler */
400         union ptlrpc_async_args rq_async_args;  /* Async completion context */
401         struct ptlrpc_request_pool *rq_pool;    /* Pool if request from
402                                                    preallocated list */
403 };
404
405 static inline void lustre_set_req_swabbed(struct ptlrpc_request *req, int index)
406 {
407         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
408         LASSERT((req->rq_req_swab_mask & (1 << index)) == 0);
409         req->rq_req_swab_mask |= 1 << index;
410 }
411
412 static inline void lustre_set_rep_swabbed(struct ptlrpc_request *req, int index)
413 {
414         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
415         LASSERT((req->rq_rep_swab_mask & (1 << index)) == 0);
416         req->rq_rep_swab_mask |= 1 << index;
417 }
418
419 static inline int lustre_req_swabbed(struct ptlrpc_request *req, int index)
420 {
421         LASSERT(index < sizeof(req->rq_req_swab_mask) * 8);
422         return req->rq_req_swab_mask & (1 << index);
423 }
424
425 static inline int lustre_rep_swabbed(struct ptlrpc_request *req, int index)
426 {
427         LASSERT(index < sizeof(req->rq_rep_swab_mask) * 8);
428         return req->rq_rep_swab_mask & (1 << index);
429 }
430
431 static inline int lustre_req_need_swab(struct ptlrpc_request *req)
432 {
433         return req->rq_req_swab_mask & (1 << MSG_PTLRPC_HEADER_OFF);
434 }
435
436 static inline int lustre_rep_need_swab(struct ptlrpc_request *req)
437 {
438         return req->rq_rep_swab_mask & (1 << MSG_PTLRPC_HEADER_OFF);
439 }
440
441 static inline const char *
442 ptlrpc_phase2str(enum rq_phase phase)
443 {
444         switch (phase) {
445         case RQ_PHASE_NEW:
446                 return "New";
447         case RQ_PHASE_RPC:
448                 return "Rpc";
449         case RQ_PHASE_BULK:
450                 return "Bulk";
451         case RQ_PHASE_INTERPRET:
452                 return "Interpret";
453         case RQ_PHASE_COMPLETE:
454                 return "Complete";
455         case RQ_PHASE_UNREGISTERING:
456                 return "Unregistering";
457         default:
458                 return "?Phase?";
459         }
460 }
461
462 static inline const char *
463 ptlrpc_rqphase2str(struct ptlrpc_request *req)
464 {
465         return ptlrpc_phase2str(req->rq_phase);
466 }
467
468 /* Spare the preprocessor, spoil the bugs. */
469 #define FLAG(field, str) (field ? str : "")
470
471 #define DEBUG_REQ_FLAGS(req)                                                  \
472         ptlrpc_rqphase2str(req),                                              \
473         FLAG(req->rq_intr, "I"), FLAG(req->rq_replied, "R"),                  \
474         FLAG(req->rq_err, "E"),                                               \
475         FLAG(req->rq_timedout, "X") /* eXpired */, FLAG(req->rq_resend, "S"), \
476         FLAG(req->rq_restart, "T"), FLAG(req->rq_replay, "P"),                \
477         FLAG(req->rq_no_resend, "N"),                                         \
478         FLAG(req->rq_waiting, "W"), FLAG(req->rq_hp, "H")
479
480 #define REQ_FLAGS_FMT "%s:%s%s%s%s%s%s%s%s%s%s"
481
482 void _debug_req(struct ptlrpc_request *req, __u32 mask,
483                 struct libcfs_debug_msg_data *data, const char *fmt, ...)
484         __attribute__ ((format (printf, 4, 5)));
485
486 #define debug_req(cdls, level, req, file, func, line, fmt, a...)              \
487 do {                                                                          \
488         CHECK_STACK();                                                        \
489                                                                               \
490         if (((level) & D_CANTMASK) != 0 ||                                    \
491             ((libcfs_debug & (level)) != 0 &&                                 \
492              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {              \
493                 static struct libcfs_debug_msg_data _req_dbg_data =           \
494                 DEBUG_MSG_DATA_INIT(cdls, DEBUG_SUBSYSTEM, file, func, line); \
495                 _debug_req((req), (level), &_req_dbg_data, fmt, ##a);         \
496         }                                                                     \
497 } while(0)
498
499 /* for most callers (level is a constant) this is resolved at compile time */
500 #define DEBUG_REQ(level, req, fmt, args...)                                   \
501 do {                                                                          \
502         if ((level) & (D_ERROR | D_WARNING)) {                                \
503             static cfs_debug_limit_state_t cdls;                              \
504             debug_req(&cdls, level, req, __FILE__, __func__, __LINE__,        \
505                       "@@@ "fmt" ", ## args);                                 \
506         } else                                                                \
507             debug_req(NULL, level, req, __FILE__, __func__, __LINE__,         \
508                       "@@@ "fmt" ", ## args);                                 \
509 } while (0)
510
511 struct ptlrpc_bulk_page {
512         struct list_head bp_link;
513         int bp_buflen;
514         int bp_pageoffset;                      /* offset within a page */
515         struct page *bp_page;
516 };
517
518 #define BULK_GET_SOURCE   0
519 #define BULK_PUT_SINK     1
520 #define BULK_GET_SINK     2
521 #define BULK_PUT_SOURCE   3
522
523 struct ptlrpc_bulk_desc {
524         unsigned long bd_success:1;              /* completed successfully */
525         unsigned long bd_network_rw:1;           /* accessible to the network */
526         unsigned long bd_type:2;                 /* {put,get}{source,sink} */
527         unsigned long bd_registered:1;           /* client side */
528         spinlock_t   bd_lock;                   /* serialise with callback */
529         int bd_import_generation;
530         struct obd_export *bd_export;
531         struct obd_import *bd_import;
532         __u32 bd_portal;
533         struct ptlrpc_request *bd_req;          /* associated request */
534         cfs_waitq_t            bd_waitq;        /* server side only WQ */
535         int                    bd_iov_count;    /* # entries in bd_iov */
536         int                    bd_max_iov;      /* allocated size of bd_iov */
537         int                    bd_nob;          /* # bytes covered */
538         int                    bd_nob_transferred; /* # bytes GOT/PUT */
539
540         __u64                  bd_last_xid;
541
542         struct ptlrpc_cb_id    bd_cbid;         /* network callback info */
543         lnet_handle_md_t       bd_md_h;         /* associated MD */
544         lnet_nid_t             bd_sender;       /* stash event::sender */
545
546 #if defined(__KERNEL__)
547         lnet_kiov_t             bd_iov[0];
548 #else
549         lnet_md_iovec_t         bd_iov[0];
550 #endif
551 };
552
553 struct ptlrpc_thread {
554
555         struct list_head t_link; /* active threads in svc->srv_threads */
556
557         void *t_data;            /* thread-private data (preallocated memory) */
558         __u32 t_flags;
559
560         unsigned int t_id; /* service thread index, from ptlrpc_start_threads */
561         struct lc_watchdog *t_watchdog; /* put watchdog in the structure per
562                                          * thread b=14840 */
563         cfs_waitq_t t_ctl_waitq;
564 };
565
566 struct ptlrpc_request_buffer_desc {
567         struct list_head       rqbd_list;
568         struct list_head       rqbd_reqs;
569         struct ptlrpc_service *rqbd_service;
570         lnet_handle_md_t       rqbd_md_h;
571         int                    rqbd_refcount;
572         char                  *rqbd_buffer;
573         struct ptlrpc_cb_id    rqbd_cbid;
574         struct ptlrpc_request  rqbd_req;
575 };
576
577 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
578 typedef void (*svcreq_printfn_t)(void *, struct ptlrpc_request *);
579 typedef int (*svc_hpreq_handler_t)(struct ptlrpc_request *);
580
581 #define PTLRPC_SVC_HP_RATIO 10
582
583 struct ptlrpc_service {
584         struct list_head srv_list;              /* chain thru all services */
585         int              srv_max_req_size;      /* biggest request to receive */
586         int              srv_max_reply_size;    /* biggest reply to send */
587         int              srv_buf_size;          /* size of individual buffers */
588         int              srv_nbuf_per_group;    /* # buffers to allocate in 1 group */
589         int              srv_nbufs;             /* total # req buffer descs allocated */
590         int              srv_threads_min;       /* threads to start at SOW */
591         int              srv_threads_max;       /* thread upper limit */
592         int              srv_threads_started;   /* index of last started thread */
593         int              srv_threads_running;   /* # running threads */
594         int              srv_n_difficult_replies; /* # 'difficult' replies */
595         int              srv_n_active_reqs;     /* # reqs being served */
596         int              srv_n_hpreq;           /* # HPreqs being served */
597         cfs_duration_t   srv_rqbd_timeout;      /* timeout before re-posting reqs, in tick */
598         int              srv_watchdog_factor;   /* soft watchdog timeout mutiplier */
599         unsigned         srv_cpu_affinity:1;    /* bind threads to CPUs */
600         unsigned         srv_at_check:1;        /* check early replies */
601         cfs_time_t       srv_at_checktime;      /* debug */
602
603         __u32            srv_req_portal;
604         __u32            srv_rep_portal;
605
606         /* AT stuff */
607         struct adaptive_timeout srv_at_estimate;/* estimated rpc service time */
608         spinlock_t        srv_at_lock;
609         struct ptlrpc_at_array  srv_at_array;   /* reqs waiting for replies */
610         cfs_timer_t       srv_at_timer;         /* early reply timer */
611
612         int               srv_n_queued_reqs;    /* # reqs in either of the queues below */
613         int               srv_hpreq_count;      /* # hp requests handled */
614         int               srv_hpreq_ratio;      /* # hp per lp reqs to handle */
615         struct list_head  srv_req_in_queue;     /* incoming reqs */
616         struct list_head  srv_request_queue;    /* reqs waiting for service */
617         struct list_head  srv_request_hpq;      /* high priority queue */
618
619         struct list_head  srv_request_history;  /* request history */
620         __u64             srv_request_seq;      /* next request sequence # */
621         __u64             srv_request_max_cull_seq; /* highest seq culled from history */
622         svcreq_printfn_t  srv_request_history_print_fn; /* service-specific print fn */
623
624         struct list_head  srv_idle_rqbds;       /* request buffers to be reposted */
625         struct list_head  srv_active_rqbds;     /* req buffers receiving */
626         struct list_head  srv_history_rqbds;    /* request buffer history */
627         int               srv_nrqbd_receiving;  /* # posted request buffers */
628         int               srv_n_history_rqbds;  /* # request buffers in history */
629         int               srv_max_history_rqbds;/* max # request buffers in history */
630
631         atomic_t          srv_outstanding_replies;
632         struct list_head  srv_active_replies;   /* all the active replies */
633         struct list_head  srv_reply_queue;      /* replies waiting for service */
634
635         cfs_waitq_t       srv_waitq; /* all threads sleep on this. This
636                                       * wait-queue is signalled when new
637                                       * incoming request arrives and when
638                                       * difficult reply has to be handled. */
639
640         struct list_head   srv_threads;         /* service thread list */
641         svc_handler_t      srv_handler;
642         svc_hpreq_handler_t srv_hpreq_handler;  /* hp request handler */
643
644         char *srv_name;  /* only statically allocated strings here; we don't clean them */
645         char *srv_thread_name;  /* only statically allocated strings here; we don't clean them */
646
647         spinlock_t               srv_lock;
648
649         cfs_proc_dir_entry_t    *srv_procroot;
650         struct lprocfs_stats    *srv_stats;
651
652         /* List of free reply_states */
653         struct list_head         srv_free_rs_list;
654         /* waitq to run, when adding stuff to srv_free_rs_list */
655         cfs_waitq_t              srv_free_rs_waitq;
656
657         /*
658          * if non-NULL called during thread creation (ptlrpc_start_thread())
659          * to initialize service specific per-thread state.
660          */
661         int (*srv_init)(struct ptlrpc_thread *thread);
662         /*
663          * if non-NULL called during thread shutdown (ptlrpc_main()) to
664          * destruct state created by ->srv_init().
665          */
666         void (*srv_done)(struct ptlrpc_thread *thread);
667
668         //struct ptlrpc_srv_ni srv_interfaces[0];
669 };
670
671 struct ptlrpcd_ctl {
672         /**
673          * Ptlrpc thread control flags (LIOD_START, LIOD_STOP, LIOD_FORCE)
674          */
675         unsigned long               pc_flags;
676         /**
677          * Thread lock protecting structure fields.
678          */
679         spinlock_t                  pc_lock;
680         /**
681          * Start completion.
682          */
683         struct completion           pc_starting;
684         /**
685          * Stop completion.
686          */
687         struct completion           pc_finishing;
688         /**
689          * Thread requests set.
690          */
691         struct ptlrpc_request_set  *pc_set;
692         /**
693          * Thread name used in cfs_daemonize()
694          */
695         char                        pc_name[16];
696 #ifndef __KERNEL__
697         /**
698          * Async rpcs flag to make sure that ptlrpcd_check() is called only
699          * once.
700          */
701         int                         pc_recurred;
702         /**
703          * Currently not used.
704          */
705         void                       *pc_callback;
706         /**
707          * User-space async rpcs callback.
708          */
709         void                       *pc_wait_callback;
710         /**
711          * User-space check idle rpcs callback.
712          */
713         void                       *pc_idle_callback;
714 #endif
715 };
716
717 /* Bits for pc_flags */
718 enum ptlrpcd_ctl_flags {
719         /**
720          * Ptlrpc thread start flag.
721          */
722         LIOD_START       = 1 << 0,
723         /**
724          * Ptlrpc thread stop flag.
725          */
726         LIOD_STOP        = 1 << 1,
727         /**
728          * Ptlrpc thread force flag (only stop force so far).
729          * This will cause aborting any inflight rpcs handled
730          * by thread if LIOD_STOP is specified.
731          */
732         LIOD_FORCE       = 1 << 2
733 };
734
735 /* ptlrpc/events.c */
736 extern lnet_handle_eq_t ptlrpc_eq_h;
737 extern int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
738                                lnet_process_id_t *peer, lnet_nid_t *self);
739 extern void request_out_callback (lnet_event_t *ev);
740 extern void reply_in_callback(lnet_event_t *ev);
741 extern void client_bulk_callback (lnet_event_t *ev);
742 extern void request_in_callback(lnet_event_t *ev);
743 extern void reply_out_callback(lnet_event_t *ev);
744 extern void server_bulk_callback (lnet_event_t *ev);
745
746 /* ptlrpc/connection.c */
747 struct ptlrpc_connection *ptlrpc_connection_get(lnet_process_id_t peer,
748                                                 lnet_nid_t self,
749                                                 struct obd_uuid *uuid);
750 int ptlrpc_connection_put(struct ptlrpc_connection *c);
751 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
752 int ptlrpc_connection_init(void);
753 void ptlrpc_connection_fini(void);
754 extern lnet_pid_t ptl_get_pid(void);
755
756 /* ptlrpc/niobuf.c */
757 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc);
758 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc);
759 int ptlrpc_register_bulk(struct ptlrpc_request *req);
760 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async);
761
762 static inline int ptlrpc_server_bulk_active(struct ptlrpc_bulk_desc *desc)
763 {
764         int rc;
765
766         LASSERT(desc != NULL);
767
768         spin_lock(&desc->bd_lock);
769         rc = desc->bd_network_rw;
770         spin_unlock(&desc->bd_lock);
771         return rc;
772 }
773
774 static inline int ptlrpc_client_bulk_active(struct ptlrpc_request *req)
775 {
776         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
777         int                      rc;
778
779         LASSERT(req != NULL);
780
781         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
782             req->rq_bulk_deadline > cfs_time_current_sec())
783                 return 1;
784
785         if (!desc)
786                 return 0;
787
788         spin_lock(&desc->bd_lock);
789         rc = desc->bd_network_rw;
790         spin_unlock(&desc->bd_lock);
791         return rc;
792 }
793
794 #define PTLRPC_REPLY_MAYBE_DIFFICULT 0x01
795 #define PTLRPC_REPLY_EARLY           0x02
796 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags);
797 int ptlrpc_reply(struct ptlrpc_request *req);
798 int ptlrpc_send_error(struct ptlrpc_request *req, int difficult);
799 int ptlrpc_error(struct ptlrpc_request *req);
800 void ptlrpc_resend_req(struct ptlrpc_request *request);
801 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req);
802 int ptl_send_rpc(struct ptlrpc_request *request, int noreply);
803 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd);
804
805 /* ptlrpc/client.c */
806 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
807                         struct ptlrpc_client *);
808 void ptlrpc_cleanup_client(struct obd_import *imp);
809 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
810
811 int ptlrpc_queue_wait(struct ptlrpc_request *req);
812 int ptlrpc_replay_req(struct ptlrpc_request *req);
813 int ptlrpc_unregister_reply(struct ptlrpc_request *req, int async);
814 void ptlrpc_restart_req(struct ptlrpc_request *req);
815 void ptlrpc_abort_inflight(struct obd_import *imp);
816 void ptlrpc_cleanup_imp(struct obd_import *imp);
817 void ptlrpc_abort_set(struct ptlrpc_request_set *set);
818
819 struct ptlrpc_request_set *ptlrpc_prep_set(void);
820 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
821                       set_interpreter_func fn, void *data);
822 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
823 int ptlrpc_check_set(struct ptlrpc_request_set *set);
824 int ptlrpc_set_wait(struct ptlrpc_request_set *);
825 int ptlrpc_expired_set(void *data);
826 void ptlrpc_interrupted_set(void *data);
827 void ptlrpc_mark_interrupted(struct ptlrpc_request *req);
828 void ptlrpc_set_destroy(struct ptlrpc_request_set *);
829 void ptlrpc_set_add_req(struct ptlrpc_request_set *, struct ptlrpc_request *);
830 int ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
831                            struct ptlrpc_request *req);
832
833 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool);
834 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq);
835
836 struct ptlrpc_request_pool *
837 ptlrpc_init_rq_pool(int, int,
838                     void (*populate_pool)(struct ptlrpc_request_pool *, int));
839
840 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req);
841 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
842                                        int opcode, int count, __u32 *lengths,
843                                        char **bufs);
844 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp,
845                                              __u32 version, int opcode,
846                                             int count, __u32 *lengths, char **bufs,
847                                             struct ptlrpc_request_pool *pool);
848 void ptlrpc_req_finished(struct ptlrpc_request *request);
849 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request);
850 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
851 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
852                                                int npages, int type, int portal);
853 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
854                                               int npages, int type, int portal);
855 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
856 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
857                            cfs_page_t *page, int pageoffset, int len);
858 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
859                                       struct obd_import *imp);
860 __u64 ptlrpc_next_xid(void);
861 __u64 ptlrpc_sample_next_xid(void);
862 __u64 ptlrpc_req_xid(struct ptlrpc_request *request);
863
864 /* ptlrpc/service.c */
865 void ptlrpc_save_lock (struct ptlrpc_request *req,
866                        struct lustre_handle *lock, int mode);
867 void ptlrpc_commit_replies (struct obd_export *exp);
868 void ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs);
869 struct ptlrpc_service *ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
870                                        int max_reply_size,
871                                        int req_portal, int rep_portal,
872                                        int watchdog_factor,
873                                        svc_handler_t, char *name,
874                                        cfs_proc_dir_entry_t *proc_entry,
875                                        svcreq_printfn_t,
876                                        int min_threads, int max_threads,
877                                        char *threadname, svc_hpreq_handler_t);
878 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
879
880 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc);
881 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc);
882 int ptlrpc_unregister_service(struct ptlrpc_service *service);
883 int liblustre_check_services (void *arg);
884 void ptlrpc_daemonize(char *name);
885 int ptlrpc_service_health_check(struct ptlrpc_service *);
886 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req);
887
888
889 struct ptlrpc_svc_data {
890         char *name;
891         struct ptlrpc_service *svc;
892         struct ptlrpc_thread *thread;
893         struct obd_device *dev;
894 };
895
896 /* ptlrpc/import.c */
897 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid);
898 int ptlrpc_init_import(struct obd_import *imp);
899 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose);
900 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
901 void ptlrpc_import_setasync(struct obd_import *imp, int count);
902 int ptlrpc_reconnect_import(struct obd_import *imp);
903
904 /* ptlrpc/pack_generic.c */
905 int lustre_msg_swabbed(struct lustre_msg *msg);
906 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version);
907 int lustre_pack_request(struct ptlrpc_request *, __u32 magic, int count,
908                         __u32 *lens, char **bufs);
909 int lustre_pack_reply(struct ptlrpc_request *, int count, __u32 *lens,
910                       char **bufs);
911 #define LPRFL_EARLY_REPLY 1
912 int lustre_pack_reply_flags(struct ptlrpc_request *, int count, __u32 *lens,
913                             char **bufs, int flags);
914 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
915                          unsigned int newlen, int move_data);
916 void lustre_free_reply_state(struct ptlrpc_reply_state *rs);
917 int lustre_msg_size(__u32 magic, int count, __u32 *lengths);
918 int lustre_packed_msg_size(struct lustre_msg *msg);
919 int lustre_msg_early_size(struct ptlrpc_request *req);
920 int lustre_unpack_msg(struct lustre_msg *m, int len);
921 void *lustre_msg_buf(struct lustre_msg *m, int n, int minlen);
922 int lustre_msg_buflen(struct lustre_msg *m, int n);
923 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len);
924 int lustre_msg_bufcount(struct lustre_msg *m);
925 char *lustre_msg_string (struct lustre_msg *m, int n, int max_len);
926 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int n, int minlen,
927                          void *swabber);
928 void *lustre_swab_repbuf(struct ptlrpc_request *req, int n, int minlen,
929                          void *swabber);
930 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg);
931 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags);
932 __u32 lustre_msg_get_flags(struct lustre_msg *msg);
933 void lustre_msg_add_flags(struct lustre_msg *msg, int flags);
934 void lustre_msg_set_flags(struct lustre_msg *msg, int flags);
935 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags);
936 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg);
937 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags);
938 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags);
939 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg);
940 __u32 lustre_msg_get_type(struct lustre_msg *msg);
941 __u32 lustre_msg_get_version(struct lustre_msg *msg);
942 void lustre_msg_add_version(struct lustre_msg *msg, int version);
943 __u32 lustre_msg_get_opc(struct lustre_msg *msg);
944 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg);
945 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg);
946 __u64 *lustre_msg_get_versions(struct lustre_msg *msg);
947 __u64 lustre_msg_get_transno(struct lustre_msg *msg);
948 __u64 lustre_msg_get_slv(struct lustre_msg *msg);
949 __u32 lustre_msg_get_limit(struct lustre_msg *msg);
950 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv);
951 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit);
952 int   lustre_msg_get_status(struct lustre_msg *msg);
953 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg);
954 int lustre_msg_is_v1(struct lustre_msg *msg);
955 __u32 lustre_msg_get_magic(struct lustre_msg *msg);
956 __u32 lustre_msg_get_timeout(struct lustre_msg *msg);
957 __u32 lustre_msg_get_service_time(struct lustre_msg *msg);
958 __u32 lustre_msg_get_cksum(struct lustre_msg *msg);
959 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg);
960 void lustre_msg_set_handle(struct lustre_msg *msg,struct lustre_handle *handle);
961 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type);
962 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc);
963 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid);
964 void lustre_msg_set_last_committed(struct lustre_msg *msg,__u64 last_committed);
965 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions);
966 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno);
967 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status);
968 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt);
969 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout);
970 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time);
971 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum);
972
973 static inline void
974 ptlrpc_rqphase_move(struct ptlrpc_request *req, enum rq_phase new_phase)
975 {
976         if (req->rq_phase == new_phase)
977                 return;
978
979         if (new_phase == RQ_PHASE_UNREGISTERING) {
980                 req->rq_next_phase = req->rq_phase;
981                 if (req->rq_import)
982                         atomic_inc(&req->rq_import->imp_unregistering);
983         }
984
985         if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
986                 if (req->rq_import)
987                         atomic_dec(&req->rq_import->imp_unregistering);
988         }
989
990         DEBUG_REQ(D_RPCTRACE, req, "move req \"%s\" -> \"%s\"",
991                   ptlrpc_rqphase2str(req), ptlrpc_phase2str(new_phase));
992
993         req->rq_phase = new_phase;
994 }
995
996 static inline int
997 ptlrpc_client_early(struct ptlrpc_request *req)
998 {
999         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1000             req->rq_reply_deadline > cfs_time_current_sec())
1001                 return 0;
1002         return req->rq_early;
1003 }
1004
1005 static inline int
1006 ptlrpc_client_replied(struct ptlrpc_request *req)
1007 {
1008         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1009             req->rq_reply_deadline > cfs_time_current_sec())
1010                 return 0;
1011         return req->rq_replied;
1012 }
1013
1014 static inline int
1015 ptlrpc_client_recv(struct ptlrpc_request *req)
1016 {
1017         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1018             req->rq_reply_deadline > cfs_time_current_sec())
1019                 return 1;
1020         return req->rq_receiving_reply;
1021 }
1022
1023 static inline int
1024 ptlrpc_client_recv_or_unlink(struct ptlrpc_request *req)
1025 {
1026         int rc;
1027
1028         spin_lock(&req->rq_lock);
1029         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1030             req->rq_reply_deadline > cfs_time_current_sec()) {
1031                 spin_unlock(&req->rq_lock);
1032                 return 1;
1033         }
1034         rc = req->rq_receiving_reply || req->rq_must_unlink;
1035         spin_unlock(&req->rq_lock);
1036         return rc;
1037 }
1038
1039 static inline void
1040 ptlrpc_client_wake_req(struct ptlrpc_request *req)
1041 {
1042         if (req->rq_set == NULL)
1043                 cfs_waitq_signal(&req->rq_reply_waitq);
1044         else
1045                 cfs_waitq_signal(&req->rq_set->set_waitq);
1046 }
1047
1048 static inline void
1049 ptlrpc_rs_addref(struct ptlrpc_reply_state *rs)
1050 {
1051         LASSERT(atomic_read(&rs->rs_refcount) > 0);
1052         atomic_inc(&rs->rs_refcount);
1053 }
1054
1055 static inline void
1056 ptlrpc_rs_decref(struct ptlrpc_reply_state *rs)
1057 {
1058         LASSERT(atomic_read(&rs->rs_refcount) > 0);
1059         if (atomic_dec_and_test(&rs->rs_refcount))
1060                 lustre_free_reply_state(rs);
1061 }
1062
1063 /* Should only be called once per req */
1064 static inline void ptlrpc_req_drop_rs(struct ptlrpc_request *req)
1065 {
1066         if (req->rq_reply_state == NULL)
1067                 return; /* shouldn't occur */
1068         ptlrpc_rs_decref(req->rq_reply_state);
1069         req->rq_reply_state = NULL;
1070         req->rq_repmsg = NULL;
1071 }
1072
1073 static inline __u32 lustre_request_magic(struct ptlrpc_request *req)
1074 {
1075         return lustre_msg_get_magic(req->rq_reqmsg);
1076 }
1077
1078 static inline int ptlrpc_req_get_repsize(struct ptlrpc_request *req)
1079 {
1080         switch (req->rq_reqmsg->lm_magic) {
1081         case LUSTRE_MSG_MAGIC_V1:
1082                 CERROR("function not supported for lustre_msg V1!\n");
1083                 return -ENOTSUPP;
1084         case LUSTRE_MSG_MAGIC_V2:
1085                 return req->rq_reqmsg->lm_repsize;
1086         default:
1087                 LASSERTF(0, "incorrect message magic: %08x\n",
1088                          req->rq_reqmsg->lm_magic);
1089                 return -EFAULT;
1090         }
1091 }
1092
1093 static inline void
1094 ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *lens)
1095 {
1096         int size = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
1097
1098         req->rq_replen = size + lustre_msg_early_size(req);
1099         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1100                 req->rq_reqmsg->lm_repsize = size;
1101 }
1102
1103 /* ldlm/ldlm_lib.c */
1104 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
1105 int client_obd_cleanup(struct obd_device * obddev);
1106 int client_connect_import(struct lustre_handle *conn, struct obd_device *obd,
1107                           struct obd_uuid *cluuid, struct obd_connect_data *,
1108                           void *localdata);
1109 int client_disconnect_export(struct obd_export *exp);
1110 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
1111                            int priority);
1112 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid);
1113 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid);
1114
1115 /* ptlrpc/pinger.c */
1116 enum timeout_event {
1117         TIMEOUT_GRANT = 1
1118 };
1119 struct timeout_item;
1120 typedef int (*timeout_cb_t)(struct timeout_item *, void *);
1121 int ptlrpc_pinger_add_import(struct obd_import *imp);
1122 int ptlrpc_pinger_del_import(struct obd_import *imp);
1123 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
1124                               timeout_cb_t cb, void *data,
1125                               struct list_head *obd_list);
1126 int ptlrpc_del_timeout_client(struct list_head *obd_list);
1127 struct ptlrpc_request * ptlrpc_prep_ping(struct obd_import *imp);
1128 int ptlrpc_obd_ping(struct obd_device *obd);
1129 #ifdef __KERNEL__
1130 void ping_evictor_start(void);
1131 void ping_evictor_stop(void);
1132 #else
1133 #define ping_evictor_start()    do {} while (0)
1134 #define ping_evictor_stop()     do {} while (0)
1135 #endif
1136
1137 /* ptlrpc/ptlrpcd.c */
1138 int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc);
1139 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force);
1140 void ptlrpcd_wake(struct ptlrpc_request *req);
1141 void ptlrpcd_add_req(struct ptlrpc_request *req);
1142 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set);
1143 int ptlrpcd_addref(void);
1144 void ptlrpcd_decref(void);
1145
1146 /* ptlrpc/lproc_ptlrpc.c */
1147 const char* ll_opcode2str(__u32 opcode);
1148 #ifdef LPROCFS
1149 void ptlrpc_lprocfs_register_obd(struct obd_device *obd);
1150 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd);
1151 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes);
1152 #else
1153 static inline void ptlrpc_lprocfs_register_obd(struct obd_device *obd) {}
1154 static inline void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd) {}
1155 static inline void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes) {}
1156 #endif
1157
1158 /* ptlrpc/llog_server.c */
1159 int llog_origin_handle_create(struct ptlrpc_request *req);
1160 int llog_origin_handle_destroy(struct ptlrpc_request *req);
1161 int llog_origin_handle_prev_block(struct ptlrpc_request *req);
1162 int llog_origin_handle_next_block(struct ptlrpc_request *req);
1163 int llog_origin_handle_read_header(struct ptlrpc_request *req);
1164 int llog_origin_handle_close(struct ptlrpc_request *req);
1165 int llog_origin_handle_cancel(struct ptlrpc_request *req);
1166 int llog_catinfo(struct ptlrpc_request *req);
1167
1168 /* ptlrpc/llog_client.c */
1169 extern struct llog_operations llog_client_ops;
1170
1171 #endif