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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LOV
39 #include <libcfs/libcfs.h>
41 #include <obd_class.h>
42 #include <lustre/lustre_idl.h>
44 #include "lov_internal.h"
46 static void lov_init_set(struct lov_request_set *set)
49 atomic_set(&set->set_completes, 0);
50 atomic_set(&set->set_success, 0);
51 INIT_LIST_HEAD(&set->set_list);
54 static void lov_finish_set(struct lov_request_set *set)
56 struct list_head *pos, *n;
57 struct lov_request *req;
61 list_for_each_safe(pos, n, &set->set_list) {
62 req = list_entry(pos, struct lov_request, rq_link);
63 list_del_init(&req->rq_link);
65 if (req->rq_oi.oi_osfs != NULL)
66 OBD_FREE_PTR(req->rq_oi.oi_osfs);
76 lov_update_set(struct lov_request_set *set, struct lov_request *req, int rc)
78 atomic_inc(&set->set_completes);
80 atomic_inc(&set->set_success);
84 lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
86 list_add_tail(&req->rq_link, &set->set_list);
91 static int lov_check_set(struct lov_obd *lov, int idx)
94 mutex_lock(&lov->lov_lock);
96 if (lov->lov_tgts[idx] == NULL ||
97 lov->lov_tgts[idx]->ltd_active ||
98 (lov->lov_tgts[idx]->ltd_exp != NULL &&
99 class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
102 mutex_unlock(&lov->lov_lock);
106 /* Check if the OSC connection exists and is active.
107 * If the OSC has not yet had a chance to connect to the OST the first time,
108 * wait once for it to connect instead of returning an error.
110 static int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
112 wait_queue_head_t waitq;
113 struct l_wait_info lwi;
114 struct lov_tgt_desc *tgt;
117 mutex_lock(&lov->lov_lock);
119 tgt = lov->lov_tgts[ost_idx];
121 if (unlikely(tgt == NULL))
124 if (likely(tgt->ltd_active))
127 if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
130 mutex_unlock(&lov->lov_lock);
132 init_waitqueue_head(&waitq);
133 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
134 cfs_time_seconds(1), NULL, NULL);
136 rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
143 mutex_unlock(&lov->lov_lock);
147 #define LOV_U64_MAX ((__u64)~0ULL)
148 #define LOV_SUM_MAX(tot, add) \
150 if ((tot) + (add) < (tot)) \
151 (tot) = LOV_U64_MAX; \
157 lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs, int success)
162 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
164 if (osfs->os_files != LOV_U64_MAX)
165 lov_do_div64(osfs->os_files, expected_stripes);
166 if (osfs->os_ffree != LOV_U64_MAX)
167 lov_do_div64(osfs->os_ffree, expected_stripes);
169 spin_lock(&obd->obd_osfs_lock);
170 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
171 obd->obd_osfs_age = cfs_time_current_64();
172 spin_unlock(&obd->obd_osfs_lock);
179 int lov_fini_statfs_set(struct lov_request_set *set)
187 if (atomic_read(&set->set_completes)) {
188 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
189 atomic_read(&set->set_success));
198 lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
201 int shift = 0, quit = 0;
205 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
207 if (osfs->os_bsize != lov_sfs->os_bsize) {
208 /* assume all block sizes are always powers of 2 */
209 /* get the bits difference */
210 tmp = osfs->os_bsize | lov_sfs->os_bsize;
211 for (shift = 0; shift <= 64; ++shift) {
223 if (osfs->os_bsize < lov_sfs->os_bsize) {
224 osfs->os_bsize = lov_sfs->os_bsize;
226 osfs->os_bfree >>= shift;
227 osfs->os_bavail >>= shift;
228 osfs->os_blocks >>= shift;
229 } else if (shift != 0) {
230 lov_sfs->os_bfree >>= shift;
231 lov_sfs->os_bavail >>= shift;
232 lov_sfs->os_blocks >>= shift;
235 /* Sandia requested that df (and so, statfs) only
236 returned minimal available space on
237 a single OST, so people would be able to
238 write this much data guaranteed. */
239 if (osfs->os_bavail > lov_sfs->os_bavail) {
240 /* Presumably if new bavail is smaller,
241 new bfree is bigger as well */
242 osfs->os_bfree = lov_sfs->os_bfree;
243 osfs->os_bavail = lov_sfs->os_bavail;
246 osfs->os_bfree += lov_sfs->os_bfree;
247 osfs->os_bavail += lov_sfs->os_bavail;
249 osfs->os_blocks += lov_sfs->os_blocks;
250 /* XXX not sure about this one - depends on policy.
251 * - could be minimum if we always stripe on all OBDs
252 * (but that would be wrong for any other policy,
253 * if one of the OBDs has no more objects left)
254 * - could be sum if we stripe whole objects
255 * - could be average, just to give a nice number
257 * To give a "reasonable" (if not wholly accurate)
258 * number, we divide the total number of free objects
259 * by expected stripe count (watch out for overflow).
261 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
262 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
266 /* The callback for osc_statfs_async that finilizes a request info when a
267 * response is received. */
268 static int cb_statfs_update(void *cookie, int rc)
270 struct obd_info *oinfo = cookie;
271 struct lov_request *lovreq;
272 struct lov_request_set *set;
273 struct obd_statfs *osfs, *lov_sfs;
275 struct lov_tgt_desc *tgt;
276 struct obd_device *lovobd, *tgtobd;
280 lovreq = container_of(oinfo, struct lov_request, rq_oi);
281 set = lovreq->rq_rqset;
282 lovobd = set->set_obd;
283 lov = &lovobd->u.lov;
284 osfs = set->set_oi->oi_osfs;
285 lov_sfs = oinfo->oi_osfs;
286 success = atomic_read(&set->set_success);
287 /* XXX: the same is done in lov_update_common_set, however
288 lovset->set_exp is not initialized. */
289 lov_update_set(set, lovreq, rc);
294 tgt = lov->lov_tgts[lovreq->rq_idx];
295 if (!tgt || !tgt->ltd_active)
296 GOTO(out_update, rc);
298 tgtobd = class_exp2obd(tgt->ltd_exp);
299 spin_lock(&tgtobd->obd_osfs_lock);
300 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
301 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
302 tgtobd->obd_osfs_age = cfs_time_current_64();
303 spin_unlock(&tgtobd->obd_osfs_lock);
306 lov_update_statfs(osfs, lov_sfs, success);
313 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
314 struct lov_request_set **reqset)
316 struct lov_request_set *set;
317 struct lov_obd *lov = &obd->u.lov;
321 OBD_ALLOC(set, sizeof(*set));
329 /* We only get block data from the OBD */
330 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
331 struct lov_request *req;
333 if (lov->lov_tgts[i] == NULL ||
334 (oinfo->oi_flags & OBD_STATFS_NODELAY &&
335 !lov->lov_tgts[i]->ltd_active)) {
336 CDEBUG(D_HA, "lov idx %d inactive\n", i);
340 /* skip targets that have been explicitely disabled by the
342 if (!lov->lov_tgts[i]->ltd_exp) {
343 CDEBUG(D_HA, "lov idx %d administratively disabled\n",
348 if (!lov->lov_tgts[i]->ltd_active)
349 lov_check_and_wait_active(lov, i);
351 OBD_ALLOC(req, sizeof(*req));
353 GOTO(out_set, rc = -ENOMEM);
355 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
356 if (req->rq_oi.oi_osfs == NULL) {
357 OBD_FREE(req, sizeof(*req));
358 GOTO(out_set, rc = -ENOMEM);
362 req->rq_oi.oi_cb_up = cb_statfs_update;
363 req->rq_oi.oi_flags = oinfo->oi_flags;
365 lov_set_add_req(req, set);
368 GOTO(out_set, rc = -EIO);
372 lov_fini_statfs_set(set);