Whamcloud - gitweb
c4760d970e2349cc24afa258cb6ac38fcbe3757f
[fs/lustre-release.git] / lustre / include / lustre_import.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 /** \defgroup obd_import PtlRPC import definitions
33  * Imports are client-side representation of remote obd target.
34  *
35  * @{
36  */
37
38 #ifndef __IMPORT_H
39 #define __IMPORT_H
40
41 /** \defgroup export export
42  *
43  * @{
44  */
45 #include <linux/atomic.h>
46 #include <linux/list.h>
47 #include <linux/mutex.h>
48 #include <linux/refcount.h>
49 #include <linux/spinlock.h>
50 #include <linux/time.h>
51 #include <linux/types.h>
52 #include <linux/workqueue.h>
53 #include <libcfs/libcfs.h>
54 #include <uapi/linux/lustre/lustre_idl.h>
55
56 /**
57  * Adaptive Timeout stuff
58  *
59  * @{
60  */
61 #define D_ADAPTTO D_OTHER
62 #define AT_BINS 4                  /* "bin" means "N seconds of history" */
63 #define AT_FLG_NOHIST 0x1          /* use last reported value only */
64
65 struct adaptive_timeout {
66         time64_t        at_binstart;         /* bin start time */
67         unsigned int    at_hist[AT_BINS];    /* timeout history bins */
68         unsigned int    at_flags;
69         timeout_t       at_current_timeout;     /* current timeout value */
70         timeout_t       at_worst_timeout_ever;  /* worst-ever timeout delta
71                                                  * value
72                                                  */
73         time64_t        at_worst_timestamp;     /* worst-ever timeout
74                                                  * timestamp
75                                                  */
76         spinlock_t      at_lock;
77 };
78
79 enum lustre_at_flags {
80         LATF_SKIP       = 0x0,
81         LATF_STATS      = 0x1,
82 };
83
84 struct ptlrpc_at_array {
85         struct list_head *paa_reqs_array; /** array to hold requests */
86         __u32             paa_size;       /** the size of array */
87         __u32             paa_count;      /** the total count of reqs */
88         time64_t          paa_deadline;   /** the earliest deadline of reqs */
89         __u32            *paa_reqs_count; /** the count of reqs in each entry */
90 };
91
92 #define IMP_AT_MAX_PORTALS 8
93 struct imp_at {
94         int                     iat_portal[IMP_AT_MAX_PORTALS];
95         struct adaptive_timeout iat_net_latency;
96         struct adaptive_timeout iat_service_estimate[IMP_AT_MAX_PORTALS];
97 };
98
99
100 /** @} */
101
102 /** Possible import states */
103 enum lustre_imp_state {
104         LUSTRE_IMP_CLOSED     = 1,
105         LUSTRE_IMP_NEW        = 2,
106         LUSTRE_IMP_DISCON     = 3,
107         LUSTRE_IMP_CONNECTING = 4,
108         LUSTRE_IMP_REPLAY     = 5,
109         LUSTRE_IMP_REPLAY_LOCKS = 6,
110         LUSTRE_IMP_REPLAY_WAIT  = 7,
111         LUSTRE_IMP_RECOVER    = 8,
112         LUSTRE_IMP_FULL       = 9,
113         LUSTRE_IMP_EVICTED    = 10,
114         LUSTRE_IMP_IDLE       = 11,
115         LUSTRE_IMP_LAST
116 };
117
118 /** Returns test string representation of numeric import state \a state */
119 static inline const char *ptlrpc_import_state_name(enum lustre_imp_state state)
120 {
121         static const char * const import_state_names[] = {
122                 "<UNKNOWN>", "CLOSED",  "NEW", "DISCONN",
123                 "CONNECTING", "REPLAY", "REPLAY_LOCKS", "REPLAY_WAIT",
124                 "RECOVER", "FULL", "EVICTED", "IDLE",
125         };
126
127         LASSERT(state < LUSTRE_IMP_LAST);
128         return import_state_names[state];
129 }
130
131 /**
132  * List of import event types
133  */
134 enum obd_import_event {
135         IMP_EVENT_DISCON     = 0x808001,
136         IMP_EVENT_INACTIVE   = 0x808002,
137         IMP_EVENT_INVALIDATE = 0x808003,
138         IMP_EVENT_ACTIVE     = 0x808004,
139         IMP_EVENT_OCD        = 0x808005,
140         IMP_EVENT_DEACTIVATE = 0x808006,
141         IMP_EVENT_ACTIVATE   = 0x808007,
142 };
143
144 /**
145  * Definition of import connection structure
146  */
147 struct obd_import_conn {
148         /** Item for linking connections together */
149         struct list_head          oic_item;
150         /** Pointer to actual PortalRPC connection */
151         struct ptlrpc_connection *oic_conn;
152         /** uuid of remote side */
153         struct obd_uuid           oic_uuid;
154         /**
155          * Time (64 bit seconds) of last connection attempt on this connection
156          */
157         time64_t                  oic_last_attempt;
158 };
159
160 /* state history */
161 #define IMP_STATE_HIST_LEN 16
162 struct import_state_hist {
163         enum lustre_imp_state   ish_state;
164         time64_t                ish_time;
165 };
166
167 /**
168  * Defintion of PortalRPC import structure.
169  * Imports are representing client-side view to remote target.
170  */
171 struct obd_import {
172         /** Reference counter */
173         refcount_t                imp_refcount;
174         struct lustre_handle      imp_dlm_handle; /* client's ldlm export */
175         /** Currently active connection */
176         struct ptlrpc_connection *imp_connection;
177         /** PortalRPC client structure for this import */
178         struct ptlrpc_client     *imp_client;
179         /** List element for linking into pinger chain */
180         struct list_head          imp_pinger_chain;
181         /** work struct for destruction of import */
182         struct work_struct        imp_zombie_work;
183
184         /**
185          * Lists of requests that are retained for replay, waiting for a reply,
186          * or waiting for recovery to complete, respectively.
187          * @{
188          */
189         struct list_head        imp_replay_list;
190         struct list_head        imp_sending_list;
191         struct list_head        imp_delayed_list;
192         /** @} */
193
194         /**
195          * List of requests that are retained for committed open replay. Once
196          * open is committed, open replay request will be moved from the
197          * imp_replay_list into the imp_committed_list.
198          * The imp_replay_cursor is for accelerating searching during replay.
199          * @{
200          */
201         struct list_head        imp_committed_list;
202         struct list_head        *imp_replay_cursor;
203         /** @} */
204
205         /** List of not replied requests */
206         struct list_head        imp_unreplied_list;
207         /** Known maximal replied XID */
208         __u64                   imp_known_replied_xid;
209
210         /** obd device for this import */
211         struct obd_device       *imp_obd;
212
213         /**
214          * some seciruty-related fields
215          * @{
216          */
217         struct ptlrpc_sec        *imp_sec;
218         rwlock_t                  imp_sec_lock;
219         time64_t                imp_sec_expire;
220         pid_t                     imp_sec_refpid;
221         /** @} */
222
223         /** Wait queue for those who need to wait for recovery completion */
224         wait_queue_head_t         imp_recovery_waitq;
225
226         /** Number of requests allocated */
227         atomic_t                  imp_reqs;
228         /** Number of requests currently in-flight */
229         atomic_t                  imp_inflight;
230         /** Number of requests currently unregistering */
231         atomic_t                  imp_unregistering;
232         /** Number of replay requests inflight */
233         atomic_t                  imp_replay_inflight;
234         /** In-flight replays rate control */
235         wait_queue_head_t         imp_replay_waitq;
236
237         /** Number of currently happening import invalidations */
238         atomic_t                  imp_inval_count;
239         /** Numbner of request timeouts */
240         atomic_t                  imp_timeouts;
241         /** Current import state */
242         enum lustre_imp_state     imp_state;
243         /** Last replay state */
244         enum lustre_imp_state     imp_replay_state;
245         /** History of import states */
246         struct import_state_hist  imp_state_hist[IMP_STATE_HIST_LEN];
247         int                       imp_state_hist_idx;
248         /** Current import generation. Incremented on every reconnect */
249         int                       imp_generation;
250         /** Idle connection initiated at this generation */
251         int                       imp_initiated_at;
252         /** Incremented every time we send reconnection request */
253         __u32                     imp_conn_cnt;
254        /** 
255         * \see ptlrpc_free_committed remembers imp_generation value here
256         * after a check to save on unnecessary replay list iterations
257         */
258         int                       imp_last_generation_checked;
259         /** Last tranno we replayed */
260         __u64                     imp_last_replay_transno;
261         /** Last transno committed on remote side */
262         __u64                     imp_peer_committed_transno;
263         /**
264          * \see ptlrpc_free_committed remembers last_transno since its last
265          * check here and if last_transno did not change since last run of
266          * ptlrpc_free_committed and import generation is the same, we can
267          * skip looking for requests to remove from replay list as optimisation
268          */
269         __u64                     imp_last_transno_checked;
270         /**
271          * Remote export handle. This is how remote side knows what export
272          * we are talking to. Filled from response to connect request
273          */
274         struct lustre_handle      imp_remote_handle;
275         /** When to perform next ping. time in jiffies. */
276         time64_t                imp_next_ping;
277         /** When we last successfully connected. time in 64bit jiffies */
278         time64_t                imp_last_success_conn;
279
280         /** List of all possible connection for import. */
281         struct list_head        imp_conn_list;
282         /**
283          * Current connection. \a imp_connection is imp_conn_current->oic_conn
284          */
285         struct obd_import_conn   *imp_conn_current;
286
287         /** Protects flags, level, generation, conn_cnt, *_list */
288         spinlock_t                imp_lock;
289
290         /* flags */
291         unsigned long             imp_invalid:1,    /* evicted */
292                                   /* administratively disabled */
293                                   imp_deactive:1,
294                                   /* try to recover the import */
295                                   imp_replayable:1,
296                                   /* don't run recovery (timeout instead) */
297                                   imp_dlm_fake:1,
298                                   /* use 1/2 timeout on MDS' OSCs */
299                                   imp_server_timeout:1,
300                                   /* VBR: imp in delayed recovery */
301                                   imp_delayed_recovery:1,
302                                   /* recovery by versions was failed */
303                                   imp_vbr_failed:1,
304                                   /* force an immidiate ping */
305                                   imp_force_verify:1,
306                                   /* force a scheduled ping */
307                                   imp_force_next_verify:1,
308                                   /* pingable */
309                                   imp_pingable:1,
310                                   /* resend for replay */
311                                   imp_resend_replay:1,
312                                   /* disable normal recovery, for test only. */
313                                   imp_no_pinger_recover:1,
314                                   /* import must be reconnected instead of
315                                    * chouse new connection */
316                                   imp_force_reconnect:1,
317                                   /* import has tried to connect with server */
318                                   imp_connect_tried:1,
319                                   /* connected but not FULL yet */
320                                   imp_connected:1,
321                                   /* grant shrink disabled */
322                                   imp_grant_shrink_disabled:1,
323                                   /* to supress LCONSOLE() at conn.restore */
324                                   imp_was_idle:1;
325         u32                       imp_connect_op;
326         u32                       imp_idle_timeout;
327         u32                       imp_idle_debug;
328         struct obd_connect_data   imp_connect_data;
329         __u64                     imp_connect_flags_orig;
330         __u64                     imp_connect_flags2_orig;
331         int                       imp_connect_error;
332
333         enum lustre_msg_magic   imp_msg_magic;
334                                 /* adjusted based on server capability */
335         enum lustre_msghdr      imp_msghdr_flags;
336
337                                 /* adaptive timeout data */
338         struct imp_at           imp_at;
339         time64_t                imp_last_reply_time;    /* for health check */
340 };
341
342 /* import.c : adaptive timeout handling.
343  *
344  * Lustre tracks how long RPCs take to complete. This information is reported
345  * back to clients who utilize the information to estimate the time needed
346  * for future requests and set appropriate RPC timeouts. Minimum and maximum
347  * service times can be configured via the at_min and at_max kernel module
348  * parameters, respectively.
349  *
350  * Since this information is transmitted between nodes the timeouts are in
351  * seconds not jiffies which can vary from node to node. To avoid confusion
352  * the timeout is handled in timeout_t (s32) instead of time64_t or
353  * long (jiffies).
354  */
355 static inline timeout_t at_est2timeout(timeout_t timeout)
356 {
357         /* add an arbitrary minimum: 125% +5 sec */
358         return timeout + (timeout >> 2) + 5;
359 }
360
361 static inline timeout_t at_timeout2est(timeout_t timeout)
362 {
363         /* restore estimate value from timeout: e=4/5(t-5) */
364         LASSERT(timeout > 0);
365         return max((timeout << 2) / 5, 5) - 4;
366 }
367
368 static inline void at_reset_nolock(struct adaptive_timeout *at,
369                                    timeout_t timeout)
370 {
371         at->at_current_timeout = timeout;
372         at->at_worst_timeout_ever = timeout;
373         at->at_worst_timestamp = ktime_get_real_seconds();
374 }
375
376 static inline void at_reset(struct adaptive_timeout *at, timeout_t timeout)
377 {
378         spin_lock(&at->at_lock);
379         at_reset_nolock(at, timeout);
380         spin_unlock(&at->at_lock);
381 }
382
383 static inline void at_init(struct adaptive_timeout *at, timeout_t timeout,
384                            int flags)
385 {
386         memset(at, 0, sizeof(*at));
387         spin_lock_init(&at->at_lock);
388         at->at_flags = flags;
389         at_reset(at, timeout);
390 }
391
392 static inline void at_reinit(struct adaptive_timeout *at, timeout_t timeout,
393                              int flags)
394 {
395         spin_lock(&at->at_lock);
396         at->at_binstart = 0;
397         memset(at->at_hist, 0, sizeof(at->at_hist));
398         at->at_flags = flags;
399         at_reset_nolock(at, timeout);
400         spin_unlock(&at->at_lock);
401 }
402
403 extern unsigned int at_min;
404 extern unsigned int at_max;
405 #define AT_OFF (at_max == 0)
406
407 static inline timeout_t at_get(struct adaptive_timeout *at)
408 {
409         return (at->at_current_timeout > at_min) ?
410                 at->at_current_timeout : at_min;
411 }
412
413 timeout_t at_measured(struct adaptive_timeout *at, timeout_t timeout);
414 int import_at_get_index(struct obd_import *imp, int portal);
415
416 /* genops.c */
417 struct obd_export;
418 extern struct obd_import *class_exp2cliimp(struct obd_export *);
419
420 /** @} import */
421
422 #endif /* __IMPORT_H */
423
424 /** @} obd_import */