Whamcloud - gitweb
LU-7236 ptlrpc: idle connections can disconnect
[fs/lustre-release.git] / lustre / lov / lov_request.c
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) 2005, 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
33 #define DEBUG_SUBSYSTEM S_LOV
34
35 #include <libcfs/libcfs.h>
36
37 #include <obd_class.h>
38 #include "lov_internal.h"
39
40 static void lov_init_set(struct lov_request_set *set)
41 {
42         set->set_count = 0;
43         atomic_set(&set->set_completes, 0);
44         atomic_set(&set->set_success, 0);
45         INIT_LIST_HEAD(&set->set_list);
46 }
47
48 static void lov_finish_set(struct lov_request_set *set)
49 {
50         struct list_head *pos, *n;
51         struct lov_request *req;
52         ENTRY;
53
54         LASSERT(set != NULL);
55         list_for_each_safe(pos, n, &set->set_list) {
56                 req = list_entry(pos, struct lov_request, rq_link);
57                 list_del_init(&req->rq_link);
58
59                 if (req->rq_oi.oi_osfs != NULL)
60                         OBD_FREE_PTR(req->rq_oi.oi_osfs);
61
62                 OBD_FREE_PTR(req);
63         }
64
65         OBD_FREE_PTR(set);
66         EXIT;
67 }
68
69 static void
70 lov_update_set(struct lov_request_set *set, struct lov_request *req, int rc)
71 {
72         atomic_inc(&set->set_completes);
73         if (rc == 0)
74                 atomic_inc(&set->set_success);
75 }
76
77 static void
78 lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
79 {
80         list_add_tail(&req->rq_link, &set->set_list);
81         set->set_count++;
82         req->rq_rqset = set;
83 }
84
85 static int lov_check_set(struct lov_obd *lov, int idx)
86 {
87         int rc = 0;
88         mutex_lock(&lov->lov_lock);
89
90         if (lov->lov_tgts[idx] == NULL ||
91             lov->lov_tgts[idx]->ltd_active ||
92             (lov->lov_tgts[idx]->ltd_exp != NULL &&
93              class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
94                 rc = 1;
95
96         mutex_unlock(&lov->lov_lock);
97         return rc;
98 }
99
100 /* Check if the OSC connection exists and is active.
101  * If the OSC has not yet had a chance to connect to the OST the first time,
102  * wait once for it to connect instead of returning an error.
103  */
104 static int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
105 {
106         wait_queue_head_t waitq;
107         struct l_wait_info lwi;
108         struct lov_tgt_desc *tgt;
109         struct obd_import *imp = NULL;
110         int rc = 0;
111
112         mutex_lock(&lov->lov_lock);
113
114         tgt = lov->lov_tgts[ost_idx];
115
116         if (unlikely(tgt == NULL))
117                 GOTO(out, rc = 0);
118
119         if (likely(tgt->ltd_active))
120                 GOTO(out, rc = 1);
121
122         if (tgt->ltd_exp)
123                 imp = class_exp2cliimp(tgt->ltd_exp);
124         if (imp && imp->imp_connect_tried)
125                 GOTO(out, rc = 0);
126         if (imp && imp->imp_state == LUSTRE_IMP_IDLE)
127                 GOTO(out, rc = 0);
128
129         mutex_unlock(&lov->lov_lock);
130
131         init_waitqueue_head(&waitq);
132         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
133                                    cfs_time_seconds(1), NULL, NULL);
134
135         rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
136         if (tgt->ltd_active)
137                 return 1;
138
139         return 0;
140
141 out:
142         mutex_unlock(&lov->lov_lock);
143         return rc;
144 }
145
146 #define LOV_U64_MAX ((__u64)~0ULL)
147 #define LOV_SUM_MAX(tot, add)                                           \
148         do {                                                            \
149                 if ((tot) + (add) < (tot))                              \
150                         (tot) = LOV_U64_MAX;                            \
151                 else                                                    \
152                         (tot) += (add);                                 \
153         } while(0)
154
155 static int
156 lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs, int success)
157 {
158         ENTRY;
159
160         if (success) {
161                 __u32 expected_stripes = lov_get_stripe_count(&obd->u.lov,
162                                                               LOV_MAGIC, 0);
163                 if (osfs->os_files != LOV_U64_MAX)
164                         lov_do_div64(osfs->os_files, expected_stripes);
165                 if (osfs->os_ffree != LOV_U64_MAX)
166                         lov_do_div64(osfs->os_ffree, expected_stripes);
167
168                 spin_lock(&obd->obd_osfs_lock);
169                 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
170                 obd->obd_osfs_age = ktime_get_seconds();
171                 spin_unlock(&obd->obd_osfs_lock);
172                 RETURN(0);
173         }
174
175         RETURN(-EIO);
176 }
177
178 int lov_fini_statfs_set(struct lov_request_set *set)
179 {
180         int rc = 0;
181         ENTRY;
182
183         if (set == NULL)
184                 RETURN(0);
185
186         if (atomic_read(&set->set_completes)) {
187                 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
188                                      atomic_read(&set->set_success));
189         }
190
191         lov_finish_set(set);
192
193         RETURN(rc);
194 }
195
196 static void
197 lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
198                   int success)
199 {
200         int shift = 0, quit = 0;
201         __u64 tmp;
202
203         if (success == 0) {
204                 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
205         } else {
206                 if (osfs->os_bsize != lov_sfs->os_bsize) {
207                         /* assume all block sizes are always powers of 2 */
208                         /* get the bits difference */
209                         tmp = osfs->os_bsize | lov_sfs->os_bsize;
210                         for (shift = 0; shift <= 64; ++shift) {
211                                 if (tmp & 1) {
212                                         if (quit)
213                                                 break;
214                                         else
215                                                 quit = 1;
216                                         shift = 0;
217                                 }
218                                 tmp >>= 1;
219                         }
220                 }
221
222                 if (osfs->os_bsize < lov_sfs->os_bsize) {
223                         osfs->os_bsize = lov_sfs->os_bsize;
224
225                         osfs->os_bfree  >>= shift;
226                         osfs->os_bavail >>= shift;
227                         osfs->os_blocks >>= shift;
228                 } else if (shift != 0) {
229                         lov_sfs->os_bfree  >>= shift;
230                         lov_sfs->os_bavail >>= shift;
231                         lov_sfs->os_blocks >>= shift;
232                 }
233 #ifdef MIN_DF
234                 /* Sandia requested that df (and so, statfs) only
235                    returned minimal available space on
236                    a single OST, so people would be able to
237                    write this much data guaranteed. */
238                 if (osfs->os_bavail > lov_sfs->os_bavail) {
239                         /* Presumably if new bavail is smaller,
240                            new bfree is bigger as well */
241                         osfs->os_bfree = lov_sfs->os_bfree;
242                         osfs->os_bavail = lov_sfs->os_bavail;
243                 }
244 #else
245                 osfs->os_bfree += lov_sfs->os_bfree;
246                 osfs->os_bavail += lov_sfs->os_bavail;
247 #endif
248                 osfs->os_blocks += lov_sfs->os_blocks;
249                 /* XXX not sure about this one - depends on policy.
250                  *   - could be minimum if we always stripe on all OBDs
251                  *     (but that would be wrong for any other policy,
252                  *     if one of the OBDs has no more objects left)
253                  *   - could be sum if we stripe whole objects
254                  *   - could be average, just to give a nice number
255                  *
256                  * To give a "reasonable" (if not wholly accurate)
257                  * number, we divide the total number of free objects
258                  * by expected stripe count (watch out for overflow).
259                  */
260                 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
261                 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
262         }
263 }
264
265 /* The callback for osc_statfs_async that finilizes a request info when a
266  * response is received. */
267 static int cb_statfs_update(void *cookie, int rc)
268 {
269         struct obd_info *oinfo = cookie;
270         struct lov_request *lovreq;
271         struct lov_request_set *set;
272         struct obd_statfs *osfs, *lov_sfs;
273         struct lov_obd *lov;
274         struct lov_tgt_desc *tgt;
275         struct obd_device *lovobd, *tgtobd;
276         int success;
277         ENTRY;
278
279         lovreq = container_of(oinfo, struct lov_request, rq_oi);
280         set = lovreq->rq_rqset;
281         lovobd = set->set_obd;
282         lov = &lovobd->u.lov;
283         osfs = set->set_oi->oi_osfs;
284         lov_sfs = oinfo->oi_osfs;
285         success = atomic_read(&set->set_success);
286         /* XXX: the same is done in lov_update_common_set, however
287            lovset->set_exp is not initialized. */
288         lov_update_set(set, lovreq, rc);
289         if (rc)
290                 GOTO(out, rc);
291
292         obd_getref(lovobd);
293         tgt = lov->lov_tgts[lovreq->rq_idx];
294         if (!tgt || !tgt->ltd_active)
295                 GOTO(out_update, rc);
296
297         tgtobd = class_exp2obd(tgt->ltd_exp);
298         spin_lock(&tgtobd->obd_osfs_lock);
299         memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
300         if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
301                 tgtobd->obd_osfs_age = ktime_get_seconds();
302         spin_unlock(&tgtobd->obd_osfs_lock);
303
304 out_update:
305         lov_update_statfs(osfs, lov_sfs, success);
306         obd_putref(lovobd);
307
308 out:
309         RETURN(0);
310 }
311
312 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
313                         struct lov_request_set **reqset)
314 {
315         struct lov_request_set *set;
316         struct lov_obd *lov = &obd->u.lov;
317         int rc = 0, i;
318         ENTRY;
319
320         OBD_ALLOC(set, sizeof(*set));
321         if (set == NULL)
322                 RETURN(-ENOMEM);
323         lov_init_set(set);
324
325         set->set_obd = obd;
326         set->set_oi = oinfo;
327
328         /* We only get block data from the OBD */
329         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
330                 struct lov_tgt_desc *ltd = lov->lov_tgts[i];
331                 struct lov_request *req;
332
333                 if (ltd == NULL) {
334                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
335                         continue;
336                 }
337
338                 /* skip targets that have been explicitely disabled by the
339                  * administrator */
340                 if (!ltd->ltd_exp) {
341                         CDEBUG(D_HA, "lov idx %d administratively disabled\n",
342                                i);
343                         continue;
344                 }
345
346                 if (oinfo->oi_flags & OBD_STATFS_NODELAY &&
347                     class_exp2cliimp(ltd->ltd_exp)->imp_state !=
348                     LUSTRE_IMP_IDLE && !ltd->ltd_active) {
349                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
350                         continue;
351                 }
352
353                 if (!ltd->ltd_active)
354                         lov_check_and_wait_active(lov, i);
355
356                 OBD_ALLOC(req, sizeof(*req));
357                 if (req == NULL)
358                         GOTO(out_set, rc = -ENOMEM);
359
360                 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
361                 if (req->rq_oi.oi_osfs == NULL) {
362                         OBD_FREE(req, sizeof(*req));
363                         GOTO(out_set, rc = -ENOMEM);
364                 }
365
366                 req->rq_idx = i;
367                 req->rq_oi.oi_cb_up = cb_statfs_update;
368                 req->rq_oi.oi_flags = oinfo->oi_flags;
369
370                 lov_set_add_req(req, set);
371         }
372         if (!set->set_count)
373                 GOTO(out_set, rc = -EIO);
374         *reqset = set;
375         RETURN(rc);
376 out_set:
377         lov_fini_statfs_set(set);
378         RETURN(rc);
379 }