Whamcloud - gitweb
LU-7236 ptlrpc: idle connections can disconnect
[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/workqueue.h>
46
47 #include <lustre_handles.h>
48 #include <uapi/linux/lustre/lustre_idl.h>
49
50 /**
51  * Adaptive Timeout stuff
52  *
53  * @{
54  */
55 #define D_ADAPTTO D_OTHER
56 #define AT_BINS 4                  /* "bin" means "N seconds of history" */
57 #define AT_FLG_NOHIST 0x1          /* use last reported value only */
58
59 struct adaptive_timeout {
60         time64_t        at_binstart;         /* bin start time */
61         unsigned int    at_hist[AT_BINS];    /* timeout history bins */
62         unsigned int    at_flags;
63         unsigned int    at_current;          /* current timeout value */
64         unsigned int    at_worst_ever;       /* worst-ever timeout value */
65         time64_t        at_worst_time;       /* worst-ever timeout timestamp */
66         spinlock_t      at_lock;
67 };
68
69 enum lustre_at_flags {
70         LATF_SKIP       = 0x0,
71         LATF_STATS      = 0x1,
72 };
73
74 struct ptlrpc_at_array {
75         struct list_head *paa_reqs_array; /** array to hold requests */
76         __u32             paa_size;       /** the size of array */
77         __u32             paa_count;      /** the total count of reqs */
78         time64_t          paa_deadline;   /** the earliest deadline of reqs */
79         __u32            *paa_reqs_count; /** the count of reqs in each entry */
80 };
81
82 #define IMP_AT_MAX_PORTALS 8
83 struct imp_at {
84         int                     iat_portal[IMP_AT_MAX_PORTALS];
85         struct adaptive_timeout iat_net_latency;
86         struct adaptive_timeout iat_service_estimate[IMP_AT_MAX_PORTALS];
87 };
88
89
90 /** @} */
91
92 /** Possible import states */
93 enum lustre_imp_state {
94         LUSTRE_IMP_CLOSED     = 1,
95         LUSTRE_IMP_NEW        = 2,
96         LUSTRE_IMP_DISCON     = 3,
97         LUSTRE_IMP_CONNECTING = 4,
98         LUSTRE_IMP_REPLAY     = 5,
99         LUSTRE_IMP_REPLAY_LOCKS = 6,
100         LUSTRE_IMP_REPLAY_WAIT  = 7,
101         LUSTRE_IMP_RECOVER    = 8,
102         LUSTRE_IMP_FULL       = 9,
103         LUSTRE_IMP_EVICTED    = 10,
104         LUSTRE_IMP_IDLE       = 11,
105         LUSTRE_IMP_LAST
106 };
107
108 /** Returns test string representation of numeric import state \a state */
109 static inline char * ptlrpc_import_state_name(enum lustre_imp_state state)
110 {
111         static char *import_state_names[] = {
112                 "<UNKNOWN>", "CLOSED",  "NEW", "DISCONN",
113                 "CONNECTING", "REPLAY", "REPLAY_LOCKS", "REPLAY_WAIT",
114                 "RECOVER", "FULL", "EVICTED", "IDLE",
115         };
116
117         LASSERT(state < LUSTRE_IMP_LAST);
118         return import_state_names[state];
119 }
120
121 /**
122  * List of import event types
123  */
124 enum obd_import_event {
125         IMP_EVENT_DISCON     = 0x808001,
126         IMP_EVENT_INACTIVE   = 0x808002,
127         IMP_EVENT_INVALIDATE = 0x808003,
128         IMP_EVENT_ACTIVE     = 0x808004,
129         IMP_EVENT_OCD        = 0x808005,
130         IMP_EVENT_DEACTIVATE = 0x808006,
131         IMP_EVENT_ACTIVATE   = 0x808007,
132 };
133
134 /**
135  * Definition of import connection structure
136  */
137 struct obd_import_conn {
138         /** Item for linking connections together */
139         struct list_head          oic_item;
140         /** Pointer to actual PortalRPC connection */
141         struct ptlrpc_connection *oic_conn;
142         /** uuid of remote side */
143         struct obd_uuid           oic_uuid;
144         /**
145          * Time (64 bit seconds) of last connection attempt on this connection
146          */
147         time64_t                  oic_last_attempt;
148 };
149
150 /* state history */
151 #define IMP_STATE_HIST_LEN 16
152 struct import_state_hist {
153         enum lustre_imp_state   ish_state;
154         time64_t                ish_time;
155 };
156
157 /**
158  * Defintion of PortalRPC import structure.
159  * Imports are representing client-side view to remote target.
160  */
161 struct obd_import {
162         /** Local handle (== id) for this import. */
163         struct portals_handle     imp_handle;
164         /** Reference counter */
165         atomic_t                  imp_refcount;
166         struct lustre_handle      imp_dlm_handle; /* client's ldlm export */
167         /** Currently active connection */
168         struct ptlrpc_connection *imp_connection;
169         /** PortalRPC client structure for this import */
170         struct ptlrpc_client     *imp_client;
171         /** List element for linking into pinger chain */
172         struct list_head          imp_pinger_chain;
173         /** work struct for destruction of import */
174         struct work_struct        imp_zombie_work;
175
176         /**
177          * Lists of requests that are retained for replay, waiting for a reply,
178          * or waiting for recovery to complete, respectively.
179          * @{
180          */
181         struct list_head        imp_replay_list;
182         struct list_head        imp_sending_list;
183         struct list_head        imp_delayed_list;
184         /** @} */
185
186         /**
187          * List of requests that are retained for committed open replay. Once
188          * open is committed, open replay request will be moved from the
189          * imp_replay_list into the imp_committed_list.
190          * The imp_replay_cursor is for accelerating searching during replay.
191          * @{
192          */
193         struct list_head        imp_committed_list;
194         struct list_head        *imp_replay_cursor;
195         /** @} */
196
197         /** List of not replied requests */
198         struct list_head        imp_unreplied_list;
199         /** Known maximal replied XID */
200         __u64                   imp_known_replied_xid;
201
202         /** obd device for this import */
203         struct obd_device       *imp_obd;
204
205         /**
206          * some seciruty-related fields
207          * @{
208          */
209         struct ptlrpc_sec        *imp_sec;
210         struct mutex              imp_sec_mutex;
211         time64_t                imp_sec_expire;
212         pid_t                     imp_sec_refpid;
213         /** @} */
214
215         /** Wait queue for those who need to wait for recovery completion */
216         wait_queue_head_t         imp_recovery_waitq;
217
218         /** Number of requests currently in-flight */
219         atomic_t                  imp_inflight;
220         /** Number of requests currently unregistering */
221         atomic_t                  imp_unregistering;
222         /** Number of replay requests inflight */
223         atomic_t                  imp_replay_inflight;
224         /** Number of currently happening import invalidations */
225         atomic_t                  imp_inval_count;
226         /** Numbner of request timeouts */
227         atomic_t                  imp_timeouts;
228         /** Current import state */
229         enum lustre_imp_state     imp_state;
230         /** Last replay state */
231         enum lustre_imp_state     imp_replay_state;
232         /** History of import states */
233         struct import_state_hist  imp_state_hist[IMP_STATE_HIST_LEN];
234         int                       imp_state_hist_idx;
235         /** Current import generation. Incremented on every reconnect */
236         int                       imp_generation;
237         /** Idle connection initiated at this generation */
238         int                       imp_initiated_at;
239         /** Incremented every time we send reconnection request */
240         __u32                     imp_conn_cnt;
241        /** 
242         * \see ptlrpc_free_committed remembers imp_generation value here
243         * after a check to save on unnecessary replay list iterations
244         */
245         int                       imp_last_generation_checked;
246         /** Last tranno we replayed */
247         __u64                     imp_last_replay_transno;
248         /** Last transno committed on remote side */
249         __u64                     imp_peer_committed_transno;
250         /**
251          * \see ptlrpc_free_committed remembers last_transno since its last
252          * check here and if last_transno did not change since last run of
253          * ptlrpc_free_committed and import generation is the same, we can
254          * skip looking for requests to remove from replay list as optimisation
255          */
256         __u64                     imp_last_transno_checked;
257         /**
258          * Remote export handle. This is how remote side knows what export
259          * we are talking to. Filled from response to connect request
260          */
261         struct lustre_handle      imp_remote_handle;
262         /** When to perform next ping. time in jiffies. */
263         time64_t                imp_next_ping;
264         /** When we last successfully connected. time in 64bit jiffies */
265         time64_t                imp_last_success_conn;
266
267         /** List of all possible connection for import. */
268         struct list_head        imp_conn_list;
269         /**
270          * Current connection. \a imp_connection is imp_conn_current->oic_conn
271          */
272         struct obd_import_conn   *imp_conn_current;
273
274         /** Protects flags, level, generation, conn_cnt, *_list */
275         spinlock_t                imp_lock;
276
277         /* flags */
278         unsigned long             imp_no_timeout:1, /* timeouts are disabled */
279                                   imp_invalid:1,    /* evicted */
280                                   /* administratively disabled */
281                                   imp_deactive:1,
282                                   /* try to recover the import */
283                                   imp_replayable:1,
284                                   /* don't run recovery (timeout instead) */
285                                   imp_dlm_fake:1,
286                                   /* use 1/2 timeout on MDS' OSCs */
287                                   imp_server_timeout:1,
288                                   /* VBR: imp in delayed recovery */
289                                   imp_delayed_recovery:1,
290                                   /* recovery by versions was failed */
291                                   imp_vbr_failed:1,
292                                   /* force an immidiate ping */
293                                   imp_force_verify:1,
294                                   /* force a scheduled ping */
295                                   imp_force_next_verify:1,
296                                   /* pingable */
297                                   imp_pingable:1,
298                                   /* resend for replay */
299                                   imp_resend_replay:1,
300                                   /* disable normal recovery, for test only. */
301                                   imp_no_pinger_recover:1,
302                                   /* import must be reconnected instead of
303                                    * chouse new connection */
304                                   imp_force_reconnect:1,
305                                   /* import has tried to connect with server */
306                                   imp_connect_tried:1,
307                                   /* connected but not FULL yet */
308                                   imp_connected:1;
309         __u32                     imp_connect_op;
310         __u32                     imp_idle_timeout;
311         struct obd_connect_data   imp_connect_data;
312         __u64                     imp_connect_flags_orig;
313         __u64                     imp_connect_flags2_orig;
314         int                       imp_connect_error;
315
316         enum lustre_msg_magic   imp_msg_magic;
317                                 /* adjusted based on server capability */
318         enum lustre_msghdr      imp_msghdr_flags;
319
320                                 /* adaptive timeout data */
321         struct imp_at           imp_at;
322         time64_t                imp_last_reply_time;    /* for health check */
323 };
324
325 /* import.c */
326 static inline unsigned int at_est2timeout(unsigned int val)
327 {
328         /* add an arbitrary minimum: 125% +5 sec */
329         return (val + (val >> 2) + 5);
330 }
331
332 static inline unsigned int at_timeout2est(unsigned int val)
333 {
334         /* restore estimate value from timeout: e=4/5(t-5) */
335         LASSERT(val);
336         return (max((val << 2) / 5, 5U) - 4);
337 }
338
339 static inline void at_reset_nolock(struct adaptive_timeout *at, int val)
340 {
341         at->at_current = val;
342         at->at_worst_ever = val;
343         at->at_worst_time = ktime_get_real_seconds();
344 }
345
346 static inline void at_reset(struct adaptive_timeout *at, int val)
347 {
348         spin_lock(&at->at_lock);
349         at_reset_nolock(at, val);
350         spin_unlock(&at->at_lock);
351 }
352
353 static inline void at_init(struct adaptive_timeout *at, int val, int flags) {
354         memset(at, 0, sizeof(*at));
355         spin_lock_init(&at->at_lock);
356         at->at_flags = flags;
357         at_reset(at, val);
358 }
359
360 static inline void at_reinit(struct adaptive_timeout *at, int val, int flags)
361 {
362         spin_lock(&at->at_lock);
363         at->at_binstart = 0;
364         memset(at->at_hist, 0, sizeof(at->at_hist));
365         at->at_flags = flags;
366         at_reset_nolock(at, val);
367         spin_unlock(&at->at_lock);
368 }
369
370 extern unsigned int at_min;
371 static inline int at_get(struct adaptive_timeout *at) {
372         return (at->at_current > at_min) ? at->at_current : at_min;
373 }
374 int at_measured(struct adaptive_timeout *at, unsigned int val);
375 int import_at_get_index(struct obd_import *imp, int portal);
376 extern unsigned int at_max;
377 #define AT_OFF (at_max == 0)
378
379 /* genops.c */
380 struct obd_export;
381 extern struct obd_import *class_exp2cliimp(struct obd_export *);
382 extern struct obd_import *class_conn2cliimp(struct lustre_handle *);
383
384 /** @} import */
385
386 #endif /* __IMPORT_H */
387
388 /** @} obd_import */