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