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