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