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