4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LOV
35 #include <libcfs/libcfs.h>
37 #include <obd_class.h>
38 #include "lov_internal.h"
40 static void lov_init_set(struct lov_request_set *set)
43 atomic_set(&set->set_completes, 0);
44 atomic_set(&set->set_success, 0);
45 INIT_LIST_HEAD(&set->set_list);
48 static void lov_finish_set(struct lov_request_set *set)
50 struct list_head *pos, *n;
51 struct lov_request *req;
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);
59 if (req->rq_oi.oi_osfs != NULL)
60 OBD_FREE_PTR(req->rq_oi.oi_osfs);
70 lov_update_set(struct lov_request_set *set, struct lov_request *req, int rc)
72 atomic_inc(&set->set_completes);
74 atomic_inc(&set->set_success);
78 lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
80 list_add_tail(&req->rq_link, &set->set_list);
85 static int lov_check_set(struct lov_obd *lov, int idx)
88 mutex_lock(&lov->lov_lock);
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))
96 mutex_unlock(&lov->lov_lock);
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.
104 static int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
106 wait_queue_head_t waitq;
107 struct l_wait_info lwi;
108 struct lov_tgt_desc *tgt;
111 mutex_lock(&lov->lov_lock);
113 tgt = lov->lov_tgts[ost_idx];
115 if (unlikely(tgt == NULL))
118 if (likely(tgt->ltd_active))
121 if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
124 mutex_unlock(&lov->lov_lock);
126 init_waitqueue_head(&waitq);
127 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
128 cfs_time_seconds(1), NULL, NULL);
130 rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
137 mutex_unlock(&lov->lov_lock);
141 #define LOV_U64_MAX ((__u64)~0ULL)
142 #define LOV_SUM_MAX(tot, add) \
144 if ((tot) + (add) < (tot)) \
145 (tot) = LOV_U64_MAX; \
151 lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs, int success)
156 __u32 expected_stripes = lov_get_stripe_count(&obd->u.lov,
158 if (osfs->os_files != LOV_U64_MAX)
159 lov_do_div64(osfs->os_files, expected_stripes);
160 if (osfs->os_ffree != LOV_U64_MAX)
161 lov_do_div64(osfs->os_ffree, expected_stripes);
163 spin_lock(&obd->obd_osfs_lock);
164 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
165 obd->obd_osfs_age = cfs_time_current_64();
166 spin_unlock(&obd->obd_osfs_lock);
173 int lov_fini_statfs_set(struct lov_request_set *set)
181 if (atomic_read(&set->set_completes)) {
182 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
183 atomic_read(&set->set_success));
192 lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
195 int shift = 0, quit = 0;
199 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
201 if (osfs->os_bsize != lov_sfs->os_bsize) {
202 /* assume all block sizes are always powers of 2 */
203 /* get the bits difference */
204 tmp = osfs->os_bsize | lov_sfs->os_bsize;
205 for (shift = 0; shift <= 64; ++shift) {
217 if (osfs->os_bsize < lov_sfs->os_bsize) {
218 osfs->os_bsize = lov_sfs->os_bsize;
220 osfs->os_bfree >>= shift;
221 osfs->os_bavail >>= shift;
222 osfs->os_blocks >>= shift;
223 } else if (shift != 0) {
224 lov_sfs->os_bfree >>= shift;
225 lov_sfs->os_bavail >>= shift;
226 lov_sfs->os_blocks >>= shift;
229 /* Sandia requested that df (and so, statfs) only
230 returned minimal available space on
231 a single OST, so people would be able to
232 write this much data guaranteed. */
233 if (osfs->os_bavail > lov_sfs->os_bavail) {
234 /* Presumably if new bavail is smaller,
235 new bfree is bigger as well */
236 osfs->os_bfree = lov_sfs->os_bfree;
237 osfs->os_bavail = lov_sfs->os_bavail;
240 osfs->os_bfree += lov_sfs->os_bfree;
241 osfs->os_bavail += lov_sfs->os_bavail;
243 osfs->os_blocks += lov_sfs->os_blocks;
244 /* XXX not sure about this one - depends on policy.
245 * - could be minimum if we always stripe on all OBDs
246 * (but that would be wrong for any other policy,
247 * if one of the OBDs has no more objects left)
248 * - could be sum if we stripe whole objects
249 * - could be average, just to give a nice number
251 * To give a "reasonable" (if not wholly accurate)
252 * number, we divide the total number of free objects
253 * by expected stripe count (watch out for overflow).
255 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
256 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
260 /* The callback for osc_statfs_async that finilizes a request info when a
261 * response is received. */
262 static int cb_statfs_update(void *cookie, int rc)
264 struct obd_info *oinfo = cookie;
265 struct lov_request *lovreq;
266 struct lov_request_set *set;
267 struct obd_statfs *osfs, *lov_sfs;
269 struct lov_tgt_desc *tgt;
270 struct obd_device *lovobd, *tgtobd;
274 lovreq = container_of(oinfo, struct lov_request, rq_oi);
275 set = lovreq->rq_rqset;
276 lovobd = set->set_obd;
277 lov = &lovobd->u.lov;
278 osfs = set->set_oi->oi_osfs;
279 lov_sfs = oinfo->oi_osfs;
280 success = atomic_read(&set->set_success);
281 /* XXX: the same is done in lov_update_common_set, however
282 lovset->set_exp is not initialized. */
283 lov_update_set(set, lovreq, rc);
288 tgt = lov->lov_tgts[lovreq->rq_idx];
289 if (!tgt || !tgt->ltd_active)
290 GOTO(out_update, rc);
292 tgtobd = class_exp2obd(tgt->ltd_exp);
293 spin_lock(&tgtobd->obd_osfs_lock);
294 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
295 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
296 tgtobd->obd_osfs_age = cfs_time_current_64();
297 spin_unlock(&tgtobd->obd_osfs_lock);
300 lov_update_statfs(osfs, lov_sfs, success);
307 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
308 struct lov_request_set **reqset)
310 struct lov_request_set *set;
311 struct lov_obd *lov = &obd->u.lov;
315 OBD_ALLOC(set, sizeof(*set));
323 /* We only get block data from the OBD */
324 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
325 struct lov_request *req;
327 if (lov->lov_tgts[i] == NULL ||
328 (oinfo->oi_flags & OBD_STATFS_NODELAY &&
329 !lov->lov_tgts[i]->ltd_active)) {
330 CDEBUG(D_HA, "lov idx %d inactive\n", i);
334 /* skip targets that have been explicitely disabled by the
336 if (!lov->lov_tgts[i]->ltd_exp) {
337 CDEBUG(D_HA, "lov idx %d administratively disabled\n",
342 if (!lov->lov_tgts[i]->ltd_active)
343 lov_check_and_wait_active(lov, i);
345 OBD_ALLOC(req, sizeof(*req));
347 GOTO(out_set, rc = -ENOMEM);
349 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
350 if (req->rq_oi.oi_osfs == NULL) {
351 OBD_FREE(req, sizeof(*req));
352 GOTO(out_set, rc = -ENOMEM);
356 req->rq_oi.oi_cb_up = cb_statfs_update;
357 req->rq_oi.oi_flags = oinfo->oi_flags;
359 lov_set_add_req(req, set);
362 GOTO(out_set, rc = -EIO);
366 lov_fini_statfs_set(set);