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 <lustre/lustre_idl.h>
40 #include "lov_internal.h"
42 static void lov_init_set(struct lov_request_set *set)
45 atomic_set(&set->set_completes, 0);
46 atomic_set(&set->set_success, 0);
47 INIT_LIST_HEAD(&set->set_list);
50 static void lov_finish_set(struct lov_request_set *set)
52 struct list_head *pos, *n;
53 struct lov_request *req;
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);
61 if (req->rq_oi.oi_osfs != NULL)
62 OBD_FREE_PTR(req->rq_oi.oi_osfs);
72 lov_update_set(struct lov_request_set *set, struct lov_request *req, int rc)
74 atomic_inc(&set->set_completes);
76 atomic_inc(&set->set_success);
80 lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
82 list_add_tail(&req->rq_link, &set->set_list);
87 static int lov_check_set(struct lov_obd *lov, int idx)
90 mutex_lock(&lov->lov_lock);
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))
98 mutex_unlock(&lov->lov_lock);
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.
106 static int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
108 wait_queue_head_t waitq;
109 struct l_wait_info lwi;
110 struct lov_tgt_desc *tgt;
113 mutex_lock(&lov->lov_lock);
115 tgt = lov->lov_tgts[ost_idx];
117 if (unlikely(tgt == NULL))
120 if (likely(tgt->ltd_active))
123 if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
126 mutex_unlock(&lov->lov_lock);
128 init_waitqueue_head(&waitq);
129 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
130 cfs_time_seconds(1), NULL, NULL);
132 rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
139 mutex_unlock(&lov->lov_lock);
143 #define LOV_U64_MAX ((__u64)~0ULL)
144 #define LOV_SUM_MAX(tot, add) \
146 if ((tot) + (add) < (tot)) \
147 (tot) = LOV_U64_MAX; \
153 lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs, int success)
158 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
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);
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);
175 int lov_fini_statfs_set(struct lov_request_set *set)
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));
194 lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
197 int shift = 0, quit = 0;
201 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
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) {
219 if (osfs->os_bsize < lov_sfs->os_bsize) {
220 osfs->os_bsize = lov_sfs->os_bsize;
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;
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;
242 osfs->os_bfree += lov_sfs->os_bfree;
243 osfs->os_bavail += lov_sfs->os_bavail;
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
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).
257 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
258 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
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)
266 struct obd_info *oinfo = cookie;
267 struct lov_request *lovreq;
268 struct lov_request_set *set;
269 struct obd_statfs *osfs, *lov_sfs;
271 struct lov_tgt_desc *tgt;
272 struct obd_device *lovobd, *tgtobd;
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);
290 tgt = lov->lov_tgts[lovreq->rq_idx];
291 if (!tgt || !tgt->ltd_active)
292 GOTO(out_update, rc);
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);
302 lov_update_statfs(osfs, lov_sfs, success);
309 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
310 struct lov_request_set **reqset)
312 struct lov_request_set *set;
313 struct lov_obd *lov = &obd->u.lov;
317 OBD_ALLOC(set, sizeof(*set));
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;
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);
336 /* skip targets that have been explicitely disabled by the
338 if (!lov->lov_tgts[i]->ltd_exp) {
339 CDEBUG(D_HA, "lov idx %d administratively disabled\n",
344 if (!lov->lov_tgts[i]->ltd_active)
345 lov_check_and_wait_active(lov, i);
347 OBD_ALLOC(req, sizeof(*req));
349 GOTO(out_set, rc = -ENOMEM);
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);
358 req->rq_oi.oi_cb_up = cb_statfs_update;
359 req->rq_oi.oi_flags = oinfo->oi_flags;
361 lov_set_add_req(req, set);
364 GOTO(out_set, rc = -EIO);
368 lov_fini_statfs_set(set);