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