Whamcloud - gitweb
LU-812 compat: clean up mutex lock to use kernel mutex primitive
[fs/lustre-release.git] / lustre / include / lustre_import.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 (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38 /** \defgroup obd_import PtlRPC import definitions
39  * Imports are client-side representation of remote obd target.
40  *
41  * @{
42  */
43
44 #ifndef __IMPORT_H
45 #define __IMPORT_H
46
47 /** \defgroup export export
48  *
49  * @{
50  */
51
52 #include <lustre_handles.h>
53 #include <lustre/lustre_idl.h>
54
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         time_t           at_binstart;         /* bin start time */
67         unsigned int     at_hist[AT_BINS];    /* timeout history bins */
68         unsigned int     at_flags;
69         unsigned int     at_current;          /* current timeout value */
70         unsigned int     at_worst_ever;       /* worst-ever timeout value */
71         time_t           at_worst_time;       /* worst-ever timeout timestamp */
72         cfs_spinlock_t   at_lock;
73 };
74
75 struct ptlrpc_at_array {
76         cfs_list_t       *paa_reqs_array; /** array to hold requests */
77         __u32             paa_size;       /** the size of array */
78         __u32             paa_count;      /** the total count of reqs */
79         time_t            paa_deadline;   /** the earliest deadline of reqs */
80         __u32            *paa_reqs_count; /** the count of reqs in each entry */
81 };
82
83 #define IMP_AT_MAX_PORTALS 8
84 struct imp_at {
85         int                     iat_portal[IMP_AT_MAX_PORTALS];
86         struct adaptive_timeout iat_net_latency;
87         struct adaptive_timeout iat_service_estimate[IMP_AT_MAX_PORTALS];
88 };
89
90
91 /** @} */
92
93 /** Possible import states */
94 enum lustre_imp_state {
95         LUSTRE_IMP_CLOSED     = 1,
96         LUSTRE_IMP_NEW        = 2,
97         LUSTRE_IMP_DISCON     = 3,
98         LUSTRE_IMP_CONNECTING = 4,
99         LUSTRE_IMP_REPLAY     = 5,
100         LUSTRE_IMP_REPLAY_LOCKS = 6,
101         LUSTRE_IMP_REPLAY_WAIT  = 7,
102         LUSTRE_IMP_RECOVER    = 8,
103         LUSTRE_IMP_FULL       = 9,
104         LUSTRE_IMP_EVICTED    = 10,
105 };
106
107 /** Returns test string representation of numeric import state \a state */
108 static inline char * ptlrpc_import_state_name(enum lustre_imp_state state)
109 {
110         static char* import_state_names[] = {
111                 "<UNKNOWN>", "CLOSED",  "NEW", "DISCONN",
112                 "CONNECTING", "REPLAY", "REPLAY_LOCKS", "REPLAY_WAIT",
113                 "RECOVER", "FULL", "EVICTED",
114         };
115
116         LASSERT (state <= LUSTRE_IMP_EVICTED);
117         return import_state_names[state];
118 }
119
120 /**
121  * List of import event types
122  */
123 enum obd_import_event {
124         IMP_EVENT_DISCON     = 0x808001,
125         IMP_EVENT_INACTIVE   = 0x808002,
126         IMP_EVENT_INVALIDATE = 0x808003,
127         IMP_EVENT_ACTIVE     = 0x808004,
128         IMP_EVENT_OCD        = 0x808005,
129         IMP_EVENT_DEACTIVATE = 0x808006,
130         IMP_EVENT_ACTIVATE   = 0x808007,
131 };
132
133 /**
134  * Definition of import connection structure
135  */
136 struct obd_import_conn {
137         /** Item for linking connections together */
138         cfs_list_t                oic_item;
139         /** Pointer to actual PortalRPC connection */
140         struct ptlrpc_connection *oic_conn;
141         /** uuid of remote side */
142         struct obd_uuid           oic_uuid;
143         /**
144          * Time (64 bit jiffies) of last connection attempt on this connection
145          */
146         __u64                     oic_last_attempt;
147 };
148
149 /* state history */
150 #define IMP_STATE_HIST_LEN 16
151 struct import_state_hist {
152         enum lustre_imp_state ish_state;
153         time_t                ish_time;
154 };
155
156 /**
157  * Defintion of PortalRPC import structure.
158  * Imports are representing client-side view to remote target.
159  */
160 struct obd_import {
161         /** Local handle (== id) for this import. */
162         struct portals_handle     imp_handle;
163         /** Reference counter */
164         cfs_atomic_t              imp_refcount;
165         struct lustre_handle      imp_dlm_handle; /* client's ldlm export */
166         /** Currently active connection */
167         struct ptlrpc_connection *imp_connection;
168         /** PortalRPC client structure for this import */
169         struct ptlrpc_client     *imp_client;
170         /** List element for linking into pinger chain */
171         cfs_list_t                imp_pinger_chain;
172         /** List element for linking into chain for destruction */
173         cfs_list_t                imp_zombie_chain;
174
175         /**
176          * Lists of requests that are retained for replay, waiting for a reply,
177          * or waiting for recovery to complete, respectively.
178          * @{
179          */
180         cfs_list_t                imp_replay_list;
181         cfs_list_t                imp_sending_list;
182         cfs_list_t                imp_delayed_list;
183         /** @} */
184
185         /** obd device for this import */
186         struct obd_device        *imp_obd;
187
188         /**
189          * some seciruty-related fields
190          * @{
191          */
192         struct ptlrpc_sec        *imp_sec;
193         cfs_mutex_t               imp_sec_mutex;
194         cfs_time_t                imp_sec_expire;
195         /** @} */
196
197         /** Wait queue for those who need to wait for recovery completion */
198         cfs_waitq_t               imp_recovery_waitq;
199
200         /** Number of requests currently in-flight */
201         cfs_atomic_t              imp_inflight;
202         /** Number of requests currently unregistering */
203         cfs_atomic_t              imp_unregistering;
204         /** Number of replay requests inflight */
205         cfs_atomic_t              imp_replay_inflight;
206         /** Number of currently happening import invalidations */
207         cfs_atomic_t              imp_inval_count;
208         /** Numbner of request timeouts */
209         cfs_atomic_t              imp_timeouts;
210         /** Current import state */
211         enum lustre_imp_state     imp_state;
212         /** History of import states */
213         struct import_state_hist  imp_state_hist[IMP_STATE_HIST_LEN];
214         int                       imp_state_hist_idx;
215         /** Current import generation. Incremented on every reconnect */
216         int                       imp_generation;
217         /** Incremented every time we send reconnection request */
218         __u32                     imp_conn_cnt;
219        /** 
220         * \see ptlrpc_free_committed remembers imp_generation value here
221         * after a check to save on unnecessary replay list iterations
222         */
223         int                       imp_last_generation_checked;
224         /** Last tranno we replayed */
225         __u64                     imp_last_replay_transno;
226         /** Last transno committed on remote side */
227         __u64                     imp_peer_committed_transno;
228         /**
229          * \see ptlrpc_free_committed remembers last_transno since its last
230          * check here and if last_transno did not change since last run of
231          * ptlrpc_free_committed and import generation is the same, we can
232          * skip looking for requests to remove from replay list as optimisation
233          */
234         __u64                     imp_last_transno_checked;
235         /**
236          * Remote export handle. This is how remote side knows what export
237          * we are talking to. Filled from response to connect request
238          */
239         struct lustre_handle      imp_remote_handle;
240         /** When to perform next ping. time in jiffies. */
241         cfs_time_t                imp_next_ping;
242         /** When we last succesfully connected. time in 64bit jiffies */
243         __u64                     imp_last_success_conn;
244
245         /** List of all possible connection for import. */
246         cfs_list_t                imp_conn_list;
247         /**
248          * Current connection. \a imp_connection is imp_conn_current->oic_conn
249          */
250         struct obd_import_conn   *imp_conn_current;
251
252         /** Protects flags, level, generation, conn_cnt, *_list */
253         cfs_spinlock_t            imp_lock;
254
255         /* flags */
256         unsigned long             imp_no_timeout:1,       /* timeouts are disabled */
257                                   imp_invalid:1,          /* evicted */
258                                   imp_deactive:1,         /* administratively disabled */
259                                   imp_replayable:1,       /* try to recover the import */
260                                   imp_dlm_fake:1,         /* don't run recovery (timeout instead) */
261                                   imp_server_timeout:1,   /* use 1/2 timeout on MDS' OSCs */
262                                   imp_delayed_recovery:1, /* VBR: imp in delayed recovery */
263                                   imp_no_lock_replay:1,   /* VBR: if gap was found then no lock replays */
264                                   imp_vbr_failed:1,       /* recovery by versions was failed */
265                                   imp_force_verify:1,     /* force an immidiate ping */
266                                   imp_pingable:1,         /* pingable */
267                                   imp_resend_replay:1,    /* resend for replay */
268                                   imp_no_pinger_recover:1,/* disable normal recovery, for test only. */
269                                   imp_force_reconnect:1;  /* import must be reconnected instead of chouse new connection */
270         __u32                     imp_connect_op;
271         struct obd_connect_data   imp_connect_data;
272         __u64                     imp_connect_flags_orig;
273         int                       imp_connect_error;
274
275         __u32                     imp_msg_magic;
276         __u32                     imp_msghdr_flags;       /* adjusted based on server capability */
277
278         struct ptlrpc_request_pool *imp_rq_pool;          /* emergency request pool */
279
280         struct imp_at             imp_at;                 /* adaptive timeout data */
281         time_t                    imp_last_reply_time;    /* for health check */
282 };
283
284 typedef void (*obd_import_callback)(struct obd_import *imp, void *closure,
285                                     int event, void *event_arg, void *cb_data);
286
287 /**
288  * Structure for import observer.
289  * It is possible to register "observer" on an import and every time
290  * something happens to an import (like connect/evict/disconnect)
291  * obderver will get its callback called with event type
292  */
293 struct obd_import_observer {
294         cfs_list_t           oio_chain;
295         obd_import_callback  oio_cb;
296         void                *oio_cb_data;
297 };
298
299 void class_observe_import(struct obd_import *imp, obd_import_callback cb,
300                           void *cb_data);
301 void class_unobserve_import(struct obd_import *imp, obd_import_callback cb,
302                             void *cb_data);
303 void class_notify_import_observers(struct obd_import *imp, int event,
304                                    void *event_arg);
305
306 /* import.c */
307 static inline unsigned int at_est2timeout(unsigned int val)
308 {
309         /* add an arbitrary minimum: 125% +5 sec */
310         return (val + (val >> 2) + 5);
311 }
312
313 static inline unsigned int at_timeout2est(unsigned int val)
314 {
315         /* restore estimate value from timeout: e=4/5(t-5) */
316         LASSERT(val);
317         return (max((val << 2) / 5, 5U) - 4);
318 }
319
320 static inline void at_init(struct adaptive_timeout *at, int val, int flags) {
321         memset(at, 0, sizeof(*at));
322         at->at_current = val;
323         at->at_worst_ever = val;
324         at->at_worst_time = cfs_time_current_sec();
325         at->at_flags = flags;
326         cfs_spin_lock_init(&at->at_lock);
327 }
328 extern unsigned int at_min;
329 static inline int at_get(struct adaptive_timeout *at) {
330         return (at->at_current > at_min) ? at->at_current : at_min;
331 }
332 int at_measured(struct adaptive_timeout *at, unsigned int val);
333 int import_at_get_index(struct obd_import *imp, int portal);
334 extern unsigned int at_max;
335 #define AT_OFF (at_max == 0)
336
337 /* genops.c */
338 struct obd_export;
339 extern struct obd_import *class_exp2cliimp(struct obd_export *);
340 extern struct obd_import *class_conn2cliimp(struct lustre_handle *);
341
342 /** @} import */
343
344 #endif /* __IMPORT_H */
345
346 /** @} obd_import */