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