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, 2012, 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 "../../include/linux/libcfs/libcfs.h"
41 #include "../include/obd_class.h"
42 #include "../include/lustre/lustre_idl.h"
43 #include "lov_internal.h"
45 static void lov_init_set(struct lov_request_set *set)
48 atomic_set(&set->set_completes, 0);
49 atomic_set(&set->set_success, 0);
50 atomic_set(&set->set_finish_checked, 0);
51 set->set_cookies = NULL;
52 INIT_LIST_HEAD(&set->set_list);
53 atomic_set(&set->set_refcount, 1);
54 init_waitqueue_head(&set->set_waitq);
55 spin_lock_init(&set->set_lock);
58 void lov_finish_set(struct lov_request_set *set)
60 struct list_head *pos, *n;
63 list_for_each_safe(pos, n, &set->set_list) {
64 struct lov_request *req = list_entry(pos,
67 list_del_init(&req->rq_link);
70 kmem_cache_free(obdo_cachep, req->rq_oi.oi_oa);
71 kfree(req->rq_oi.oi_osfs);
77 int lov_set_finished(struct lov_request_set *set, int idempotent)
79 int completes = atomic_read(&set->set_completes);
81 CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
83 if (completes == set->set_count) {
86 if (atomic_inc_return(&set->set_finish_checked) == 1)
92 void lov_update_set(struct lov_request_set *set,
93 struct lov_request *req, int rc)
98 atomic_inc(&set->set_completes);
100 atomic_inc(&set->set_success);
102 wake_up(&set->set_waitq);
105 int lov_update_common_set(struct lov_request_set *set,
106 struct lov_request *req, int rc)
108 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
110 lov_update_set(set, req, rc);
112 /* grace error on inactive ost */
113 if (rc && !(lov->lov_tgts[req->rq_idx] &&
114 lov->lov_tgts[req->rq_idx]->ltd_active))
117 /* FIXME in raid1 regime, should return 0 */
121 void lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
123 list_add_tail(&req->rq_link, &set->set_list);
128 static int lov_check_set(struct lov_obd *lov, int idx)
131 struct lov_tgt_desc *tgt;
133 mutex_lock(&lov->lov_lock);
134 tgt = lov->lov_tgts[idx];
135 rc = !tgt || tgt->ltd_active ||
137 class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried);
138 mutex_unlock(&lov->lov_lock);
143 /* Check if the OSC connection exists and is active.
144 * If the OSC has not yet had a chance to connect to the OST the first time,
145 * wait once for it to connect instead of returning an error.
147 int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
149 wait_queue_head_t waitq;
150 struct l_wait_info lwi;
151 struct lov_tgt_desc *tgt;
154 mutex_lock(&lov->lov_lock);
156 tgt = lov->lov_tgts[ost_idx];
158 if (unlikely(tgt == NULL)) {
163 if (likely(tgt->ltd_active)) {
168 if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried) {
173 mutex_unlock(&lov->lov_lock);
175 init_waitqueue_head(&waitq);
176 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
177 cfs_time_seconds(1), NULL, NULL);
179 rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
180 if (tgt != NULL && tgt->ltd_active)
186 mutex_unlock(&lov->lov_lock);
190 static int common_attr_done(struct lov_request_set *set)
192 struct list_head *pos;
193 struct lov_request *req;
195 int rc = 0, attrset = 0;
197 LASSERT(set->set_oi != NULL);
199 if (set->set_oi->oi_oa == NULL)
202 if (!atomic_read(&set->set_success))
205 tmp_oa = kmem_cache_alloc(obdo_cachep, GFP_NOFS | __GFP_ZERO);
206 if (tmp_oa == NULL) {
211 list_for_each(pos, &set->set_list) {
212 req = list_entry(pos, struct lov_request, rq_link);
214 if (!req->rq_complete || req->rq_rc)
216 if (req->rq_oi.oi_oa->o_valid == 0) /* inactive stripe */
218 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
219 req->rq_oi.oi_oa->o_valid,
220 set->set_oi->oi_md, req->rq_stripe, &attrset);
223 CERROR("No stripes had valid attrs\n");
226 if ((set->set_oi->oi_oa->o_valid & OBD_MD_FLEPOCH) &&
227 (set->set_oi->oi_md->lsm_stripe_count != attrset)) {
228 /* When we take attributes of some epoch, we require all the
229 * ost to be active. */
230 CERROR("Not all the stripes had valid attrs\n");
235 tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
236 memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
239 kmem_cache_free(obdo_cachep, tmp_oa);
244 int lov_fini_getattr_set(struct lov_request_set *set)
250 LASSERT(set->set_exp);
251 if (atomic_read(&set->set_completes))
252 rc = common_attr_done(set);
259 /* The callback for osc_getattr_async that finalizes a request info when a
260 * response is received. */
261 static int cb_getattr_update(void *cookie, int rc)
263 struct obd_info *oinfo = cookie;
264 struct lov_request *lovreq;
266 lovreq = container_of(oinfo, struct lov_request, rq_oi);
267 return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
270 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
271 struct lov_request_set **reqset)
273 struct lov_request_set *set;
274 struct lov_obd *lov = &exp->exp_obd->u.lov;
277 set = kzalloc(sizeof(*set), GFP_NOFS);
285 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
286 struct lov_oinfo *loi;
287 struct lov_request *req;
289 loi = oinfo->oi_md->lsm_oinfo[i];
290 if (lov_oinfo_is_dummy(loi))
293 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
294 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
295 if (oinfo->oi_oa->o_valid & OBD_MD_FLEPOCH) {
296 /* SOM requires all the OSTs to be active. */
303 req = kzalloc(sizeof(*req), GFP_NOFS);
310 req->rq_idx = loi->loi_ost_idx;
312 req->rq_oi.oi_oa = kmem_cache_alloc(obdo_cachep,
313 GFP_NOFS | __GFP_ZERO);
314 if (req->rq_oi.oi_oa == NULL) {
319 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
320 sizeof(*req->rq_oi.oi_oa));
321 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
322 req->rq_oi.oi_cb_up = cb_getattr_update;
324 lov_set_add_req(req, set);
326 if (!set->set_count) {
333 lov_fini_getattr_set(set);
337 int lov_fini_destroy_set(struct lov_request_set *set)
341 LASSERT(set->set_exp);
342 if (atomic_read(&set->set_completes)) {
343 /* FIXME update qos data here */
351 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
352 struct obdo *src_oa, struct lov_stripe_md *lsm,
353 struct obd_trans_info *oti,
354 struct lov_request_set **reqset)
356 struct lov_request_set *set;
357 struct lov_obd *lov = &exp->exp_obd->u.lov;
360 set = kzalloc(sizeof(*set), GFP_NOFS);
367 set->set_oi->oi_md = lsm;
368 set->set_oi->oi_oa = src_oa;
370 if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
371 set->set_cookies = oti->oti_logcookies;
373 for (i = 0; i < lsm->lsm_stripe_count; i++) {
374 struct lov_oinfo *loi;
375 struct lov_request *req;
377 loi = lsm->lsm_oinfo[i];
378 if (lov_oinfo_is_dummy(loi))
381 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
382 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
386 req = kzalloc(sizeof(*req), GFP_NOFS);
393 req->rq_idx = loi->loi_ost_idx;
395 req->rq_oi.oi_oa = kmem_cache_alloc(obdo_cachep,
396 GFP_NOFS | __GFP_ZERO);
397 if (req->rq_oi.oi_oa == NULL) {
402 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
403 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
404 lov_set_add_req(req, set);
406 if (!set->set_count) {
413 lov_fini_destroy_set(set);
417 int lov_fini_setattr_set(struct lov_request_set *set)
423 LASSERT(set->set_exp);
424 if (atomic_read(&set->set_completes)) {
425 rc = common_attr_done(set);
426 /* FIXME update qos data here */
433 int lov_update_setattr_set(struct lov_request_set *set,
434 struct lov_request *req, int rc)
436 struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
437 struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
439 lov_update_set(set, req, rc);
441 /* grace error on inactive ost */
442 if (rc && !(lov->lov_tgts[req->rq_idx] &&
443 lov->lov_tgts[req->rq_idx]->ltd_active))
447 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
448 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
449 req->rq_oi.oi_oa->o_ctime;
450 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
451 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
452 req->rq_oi.oi_oa->o_mtime;
453 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
454 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
455 req->rq_oi.oi_oa->o_atime;
461 /* The callback for osc_setattr_async that finalizes a request info when a
462 * response is received. */
463 static int cb_setattr_update(void *cookie, int rc)
465 struct obd_info *oinfo = cookie;
466 struct lov_request *lovreq;
468 lovreq = container_of(oinfo, struct lov_request, rq_oi);
469 return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
472 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
473 struct obd_trans_info *oti,
474 struct lov_request_set **reqset)
476 struct lov_request_set *set;
477 struct lov_obd *lov = &exp->exp_obd->u.lov;
480 set = kzalloc(sizeof(*set), GFP_NOFS);
488 if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
489 set->set_cookies = oti->oti_logcookies;
491 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
492 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
493 struct lov_request *req;
495 if (lov_oinfo_is_dummy(loi))
498 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
499 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
503 req = kzalloc(sizeof(*req), GFP_NOFS);
509 req->rq_idx = loi->loi_ost_idx;
511 req->rq_oi.oi_oa = kmem_cache_alloc(obdo_cachep,
512 GFP_NOFS | __GFP_ZERO);
513 if (req->rq_oi.oi_oa == NULL) {
518 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
519 sizeof(*req->rq_oi.oi_oa));
520 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
521 req->rq_oi.oi_oa->o_stripe_idx = i;
522 req->rq_oi.oi_cb_up = cb_setattr_update;
524 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
525 int off = lov_stripe_offset(oinfo->oi_md,
526 oinfo->oi_oa->o_size, i,
527 &req->rq_oi.oi_oa->o_size);
529 if (off < 0 && req->rq_oi.oi_oa->o_size)
530 req->rq_oi.oi_oa->o_size--;
532 CDEBUG(D_INODE, "stripe %d has size %llu/%llu\n",
533 i, req->rq_oi.oi_oa->o_size,
534 oinfo->oi_oa->o_size);
536 lov_set_add_req(req, set);
538 if (!set->set_count) {
545 lov_fini_setattr_set(set);
549 #define LOV_U64_MAX ((__u64)~0ULL)
550 #define LOV_SUM_MAX(tot, add) \
552 if ((tot) + (add) < (tot)) \
553 (tot) = LOV_U64_MAX; \
558 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,
562 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
564 if (osfs->os_files != LOV_U64_MAX)
565 lov_do_div64(osfs->os_files, expected_stripes);
566 if (osfs->os_ffree != LOV_U64_MAX)
567 lov_do_div64(osfs->os_ffree, expected_stripes);
569 spin_lock(&obd->obd_osfs_lock);
570 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
571 obd->obd_osfs_age = cfs_time_current_64();
572 spin_unlock(&obd->obd_osfs_lock);
579 int lov_fini_statfs_set(struct lov_request_set *set)
586 if (atomic_read(&set->set_completes)) {
587 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
588 atomic_read(&set->set_success));
594 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
597 int shift = 0, quit = 0;
601 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
603 if (osfs->os_bsize != lov_sfs->os_bsize) {
604 /* assume all block sizes are always powers of 2 */
605 /* get the bits difference */
606 tmp = osfs->os_bsize | lov_sfs->os_bsize;
607 for (shift = 0; shift <= 64; ++shift) {
618 if (osfs->os_bsize < lov_sfs->os_bsize) {
619 osfs->os_bsize = lov_sfs->os_bsize;
621 osfs->os_bfree >>= shift;
622 osfs->os_bavail >>= shift;
623 osfs->os_blocks >>= shift;
624 } else if (shift != 0) {
625 lov_sfs->os_bfree >>= shift;
626 lov_sfs->os_bavail >>= shift;
627 lov_sfs->os_blocks >>= shift;
629 osfs->os_bfree += lov_sfs->os_bfree;
630 osfs->os_bavail += lov_sfs->os_bavail;
631 osfs->os_blocks += lov_sfs->os_blocks;
632 /* XXX not sure about this one - depends on policy.
633 * - could be minimum if we always stripe on all OBDs
634 * (but that would be wrong for any other policy,
635 * if one of the OBDs has no more objects left)
636 * - could be sum if we stripe whole objects
637 * - could be average, just to give a nice number
639 * To give a "reasonable" (if not wholly accurate)
640 * number, we divide the total number of free objects
641 * by expected stripe count (watch out for overflow).
643 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
644 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
648 /* The callback for osc_statfs_async that finalizes a request info when a
649 * response is received. */
650 static int cb_statfs_update(void *cookie, int rc)
652 struct obd_info *oinfo = cookie;
653 struct lov_request *lovreq;
654 struct lov_request_set *set;
655 struct obd_statfs *osfs, *lov_sfs;
657 struct lov_tgt_desc *tgt;
658 struct obd_device *lovobd, *tgtobd;
661 lovreq = container_of(oinfo, struct lov_request, rq_oi);
662 set = lovreq->rq_rqset;
663 lovobd = set->set_obd;
664 lov = &lovobd->u.lov;
665 osfs = set->set_oi->oi_osfs;
666 lov_sfs = oinfo->oi_osfs;
667 success = atomic_read(&set->set_success);
668 /* XXX: the same is done in lov_update_common_set, however
669 lovset->set_exp is not initialized. */
670 lov_update_set(set, lovreq, rc);
675 tgt = lov->lov_tgts[lovreq->rq_idx];
676 if (!tgt || !tgt->ltd_active)
679 tgtobd = class_exp2obd(tgt->ltd_exp);
680 spin_lock(&tgtobd->obd_osfs_lock);
681 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
682 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
683 tgtobd->obd_osfs_age = cfs_time_current_64();
684 spin_unlock(&tgtobd->obd_osfs_lock);
687 lov_update_statfs(osfs, lov_sfs, success);
691 if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
692 lov_set_finished(set, 0)) {
693 lov_statfs_interpret(NULL, set, set->set_count !=
694 atomic_read(&set->set_success));
700 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
701 struct lov_request_set **reqset)
703 struct lov_request_set *set;
704 struct lov_obd *lov = &obd->u.lov;
707 set = kzalloc(sizeof(*set), GFP_NOFS);
715 /* We only get block data from the OBD */
716 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
717 struct lov_request *req;
719 if (lov->lov_tgts[i] == NULL ||
720 (!lov_check_and_wait_active(lov, i) &&
721 (oinfo->oi_flags & OBD_STATFS_NODELAY))) {
722 CDEBUG(D_HA, "lov idx %d inactive\n", i);
726 /* skip targets that have been explicitly disabled by the
728 if (!lov->lov_tgts[i]->ltd_exp) {
729 CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
733 req = kzalloc(sizeof(*req), GFP_NOFS);
739 req->rq_oi.oi_osfs = kzalloc(sizeof(*req->rq_oi.oi_osfs),
741 if (!req->rq_oi.oi_osfs) {
748 req->rq_oi.oi_cb_up = cb_statfs_update;
749 req->rq_oi.oi_flags = oinfo->oi_flags;
751 lov_set_add_req(req, set);
753 if (!set->set_count) {
760 lov_fini_statfs_set(set);