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