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, 2013, 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
40 #include <libcfs/libcfs.h>
42 #include <liblustre.h>
45 #include <obd_class.h>
46 #include <lustre/lustre_idl.h>
48 #include "lov_internal.h"
50 static void lov_init_set(struct lov_request_set *set)
53 atomic_set(&set->set_completes, 0);
54 atomic_set(&set->set_success, 0);
55 atomic_set(&set->set_finish_checked, 0);
57 INIT_LIST_HEAD(&set->set_list);
58 atomic_set(&set->set_refcount, 1);
59 init_waitqueue_head(&set->set_waitq);
62 void lov_finish_set(struct lov_request_set *set)
64 struct list_head *pos, *n;
65 struct lov_request *req;
69 list_for_each_safe(pos, n, &set->set_list) {
70 req = list_entry(pos, struct lov_request, rq_link);
71 list_del_init(&req->rq_link);
73 if (req->rq_oi.oi_oa != NULL)
74 OBDO_FREE(req->rq_oi.oi_oa);
76 if (req->rq_oi.oi_osfs != NULL)
77 OBD_FREE_PTR(req->rq_oi.oi_osfs);
86 int lov_set_finished(struct lov_request_set *set, int idempotent)
88 int completes = atomic_read(&set->set_completes);
90 CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
92 if (completes == set->set_count) {
95 if (atomic_inc_return(&set->set_finish_checked) == 1)
101 void lov_update_set(struct lov_request_set *set,
102 struct lov_request *req, int rc)
104 req->rq_complete = 1;
107 atomic_inc(&set->set_completes);
109 atomic_inc(&set->set_success);
111 wake_up(&set->set_waitq);
114 int lov_update_common_set(struct lov_request_set *set,
115 struct lov_request *req, int rc)
117 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
120 lov_update_set(set, req, rc);
122 /* grace error on inactive ost */
123 if (rc && !(lov->lov_tgts[req->rq_idx] &&
124 lov->lov_tgts[req->rq_idx]->ltd_active))
127 /* FIXME in raid1 regime, should return 0 */
131 void lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
133 list_add_tail(&req->rq_link, &set->set_list);
138 static int lov_check_set(struct lov_obd *lov, int idx)
141 mutex_lock(&lov->lov_lock);
143 if (lov->lov_tgts[idx] == NULL ||
144 lov->lov_tgts[idx]->ltd_active ||
145 (lov->lov_tgts[idx]->ltd_exp != NULL &&
146 class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
149 mutex_unlock(&lov->lov_lock);
153 /* Check if the OSC connection exists and is active.
154 * If the OSC has not yet had a chance to connect to the OST the first time,
155 * wait once for it to connect instead of returning an error.
157 int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
159 wait_queue_head_t waitq;
160 struct l_wait_info lwi;
161 struct lov_tgt_desc *tgt;
164 mutex_lock(&lov->lov_lock);
166 tgt = lov->lov_tgts[ost_idx];
168 if (unlikely(tgt == NULL))
171 if (likely(tgt->ltd_active))
174 if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
177 mutex_unlock(&lov->lov_lock);
179 init_waitqueue_head(&waitq);
180 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
181 cfs_time_seconds(1), NULL, NULL);
183 rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
190 mutex_unlock(&lov->lov_lock);
194 static int common_attr_done(struct lov_request_set *set)
196 struct list_head *pos;
197 struct lov_request *req;
199 int rc = 0, attrset = 0;
202 LASSERT(set->set_oi != NULL);
204 if (set->set_oi->oi_oa == NULL)
207 if (!atomic_read(&set->set_success))
212 GOTO(out, rc = -ENOMEM);
214 list_for_each(pos, &set->set_list) {
215 req = list_entry(pos, struct lov_request, rq_link);
217 if (!req->rq_complete || req->rq_rc)
219 if (req->rq_oi.oi_oa->o_valid == 0) /* inactive stripe */
221 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
222 req->rq_oi.oi_oa->o_valid,
223 set->set_oi->oi_md, req->rq_stripe, &attrset);
226 CERROR("No stripes had valid attrs\n");
229 if ((set->set_oi->oi_oa->o_valid & OBD_MD_FLEPOCH) &&
230 (set->set_oi->oi_md->lsm_stripe_count != attrset)) {
231 /* When we take attributes of some epoch, we require all the
232 * ost to be active. */
233 CERROR("Not all the stripes had valid attrs\n");
234 GOTO(out, rc = -EIO);
237 tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
238 memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
246 int lov_fini_getattr_set(struct lov_request_set *set)
253 LASSERT(set->set_exp);
254 if (atomic_read(&set->set_completes))
255 rc = common_attr_done(set);
262 /* The callback for osc_getattr_async that finilizes a request info when a
263 * response is received. */
264 static int cb_getattr_update(void *cookie, int rc)
266 struct obd_info *oinfo = cookie;
267 struct lov_request *lovreq;
268 lovreq = container_of(oinfo, struct lov_request, rq_oi);
269 return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
272 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
273 struct lov_request_set **reqset)
275 struct lov_request_set *set;
276 struct lov_obd *lov = &exp->exp_obd->u.lov;
280 OBD_ALLOC(set, sizeof(*set));
288 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
289 struct lov_oinfo *loi;
290 struct lov_request *req;
292 loi = oinfo->oi_md->lsm_oinfo[i];
293 if (lov_oinfo_is_dummy(loi))
296 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
297 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
298 if (oinfo->oi_oa->o_valid & OBD_MD_FLEPOCH)
299 /* SOM requires all the OSTs to be active. */
300 GOTO(out_set, rc = -EIO);
304 OBD_ALLOC(req, sizeof(*req));
306 GOTO(out_set, rc = -ENOMEM);
309 req->rq_idx = loi->loi_ost_idx;
311 OBDO_ALLOC(req->rq_oi.oi_oa);
312 if (req->rq_oi.oi_oa == NULL) {
313 OBD_FREE(req, sizeof(*req));
314 GOTO(out_set, rc = -ENOMEM);
316 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
317 sizeof(*req->rq_oi.oi_oa));
318 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
319 req->rq_oi.oi_cb_up = cb_getattr_update;
320 req->rq_oi.oi_capa = oinfo->oi_capa;
322 lov_set_add_req(req, set);
325 GOTO(out_set, rc = -EIO);
329 lov_fini_getattr_set(set);
333 int lov_fini_destroy_set(struct lov_request_set *set)
339 LASSERT(set->set_exp);
340 if (atomic_read(&set->set_completes)) {
341 /* FIXME update qos data here */
349 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
350 struct obdo *src_oa, struct lov_stripe_md *lsm,
351 struct obd_trans_info *oti,
352 struct lov_request_set **reqset)
354 struct lov_request_set *set;
355 struct lov_obd *lov = &exp->exp_obd->u.lov;
359 OBD_ALLOC(set, sizeof(*set));
366 set->set_oi->oi_md = lsm;
367 set->set_oi->oi_oa = src_oa;
368 if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
369 set->set_cookies = oti->oti_logcookies;
371 for (i = 0; i < lsm->lsm_stripe_count; i++) {
372 struct lov_oinfo *loi;
373 struct lov_request *req;
375 loi = lsm->lsm_oinfo[i];
376 if (lov_oinfo_is_dummy(loi))
379 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
380 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
384 OBD_ALLOC(req, sizeof(*req));
386 GOTO(out_set, rc = -ENOMEM);
389 req->rq_idx = loi->loi_ost_idx;
391 OBDO_ALLOC(req->rq_oi.oi_oa);
392 if (req->rq_oi.oi_oa == NULL) {
393 OBD_FREE(req, sizeof(*req));
394 GOTO(out_set, rc = -ENOMEM);
396 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
397 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
398 lov_set_add_req(req, set);
401 GOTO(out_set, rc = -EIO);
405 lov_fini_destroy_set(set);
409 int lov_fini_setattr_set(struct lov_request_set *set)
416 LASSERT(set->set_exp);
417 if (atomic_read(&set->set_completes)) {
418 rc = common_attr_done(set);
419 /* FIXME update qos data here */
426 int lov_update_setattr_set(struct lov_request_set *set,
427 struct lov_request *req, int rc)
429 struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
430 struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
433 lov_update_set(set, req, rc);
435 /* grace error on inactive ost */
436 if (rc && !(lov->lov_tgts[req->rq_idx] &&
437 lov->lov_tgts[req->rq_idx]->ltd_active))
441 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
442 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
443 req->rq_oi.oi_oa->o_ctime;
444 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
445 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
446 req->rq_oi.oi_oa->o_mtime;
447 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
448 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
449 req->rq_oi.oi_oa->o_atime;
455 /* The callback for osc_setattr_async that finilizes a request info when a
456 * response is received. */
457 static int cb_setattr_update(void *cookie, int rc)
459 struct obd_info *oinfo = cookie;
460 struct lov_request *lovreq;
461 lovreq = container_of(oinfo, struct lov_request, rq_oi);
462 return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
465 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
466 struct obd_trans_info *oti,
467 struct lov_request_set **reqset)
469 struct lov_request_set *set;
470 struct lov_obd *lov = &exp->exp_obd->u.lov;
474 OBD_ALLOC(set, sizeof(*set));
481 if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
482 set->set_cookies = oti->oti_logcookies;
484 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
485 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
486 struct lov_request *req;
488 if (lov_oinfo_is_dummy(loi))
491 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
492 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
496 OBD_ALLOC(req, sizeof(*req));
498 GOTO(out_set, rc = -ENOMEM);
500 req->rq_idx = loi->loi_ost_idx;
502 OBDO_ALLOC(req->rq_oi.oi_oa);
503 if (req->rq_oi.oi_oa == NULL) {
504 OBD_FREE(req, sizeof(*req));
505 GOTO(out_set, rc = -ENOMEM);
507 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
508 sizeof(*req->rq_oi.oi_oa));
509 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
510 req->rq_oi.oi_oa->o_stripe_idx = i;
511 req->rq_oi.oi_cb_up = cb_setattr_update;
512 req->rq_oi.oi_capa = oinfo->oi_capa;
514 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
515 int off = lov_stripe_offset(oinfo->oi_md,
516 oinfo->oi_oa->o_size, i,
517 &req->rq_oi.oi_oa->o_size);
519 if (off < 0 && req->rq_oi.oi_oa->o_size)
520 req->rq_oi.oi_oa->o_size--;
522 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
523 i, req->rq_oi.oi_oa->o_size,
524 oinfo->oi_oa->o_size);
526 lov_set_add_req(req, set);
529 GOTO(out_set, rc = -EIO);
533 lov_fini_setattr_set(set);
537 #define LOV_U64_MAX ((__u64)~0ULL)
538 #define LOV_SUM_MAX(tot, add) \
540 if ((tot) + (add) < (tot)) \
541 (tot) = LOV_U64_MAX; \
546 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
551 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
553 if (osfs->os_files != LOV_U64_MAX)
554 lov_do_div64(osfs->os_files, expected_stripes);
555 if (osfs->os_ffree != LOV_U64_MAX)
556 lov_do_div64(osfs->os_ffree, expected_stripes);
558 spin_lock(&obd->obd_osfs_lock);
559 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
560 obd->obd_osfs_age = cfs_time_current_64();
561 spin_unlock(&obd->obd_osfs_lock);
568 int lov_fini_statfs_set(struct lov_request_set *set)
576 if (atomic_read(&set->set_completes)) {
577 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
578 atomic_read(&set->set_success));
584 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
587 int shift = 0, quit = 0;
591 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
593 if (osfs->os_bsize != lov_sfs->os_bsize) {
594 /* assume all block sizes are always powers of 2 */
595 /* get the bits difference */
596 tmp = osfs->os_bsize | lov_sfs->os_bsize;
597 for (shift = 0; shift <= 64; ++shift) {
609 if (osfs->os_bsize < lov_sfs->os_bsize) {
610 osfs->os_bsize = lov_sfs->os_bsize;
612 osfs->os_bfree >>= shift;
613 osfs->os_bavail >>= shift;
614 osfs->os_blocks >>= shift;
615 } else if (shift != 0) {
616 lov_sfs->os_bfree >>= shift;
617 lov_sfs->os_bavail >>= shift;
618 lov_sfs->os_blocks >>= shift;
621 /* Sandia requested that df (and so, statfs) only
622 returned minimal available space on
623 a single OST, so people would be able to
624 write this much data guaranteed. */
625 if (osfs->os_bavail > lov_sfs->os_bavail) {
626 /* Presumably if new bavail is smaller,
627 new bfree is bigger as well */
628 osfs->os_bfree = lov_sfs->os_bfree;
629 osfs->os_bavail = lov_sfs->os_bavail;
632 osfs->os_bfree += lov_sfs->os_bfree;
633 osfs->os_bavail += lov_sfs->os_bavail;
635 osfs->os_blocks += lov_sfs->os_blocks;
636 /* XXX not sure about this one - depends on policy.
637 * - could be minimum if we always stripe on all OBDs
638 * (but that would be wrong for any other policy,
639 * if one of the OBDs has no more objects left)
640 * - could be sum if we stripe whole objects
641 * - could be average, just to give a nice number
643 * To give a "reasonable" (if not wholly accurate)
644 * number, we divide the total number of free objects
645 * by expected stripe count (watch out for overflow).
647 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
648 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
652 /* The callback for osc_statfs_async that finilizes a request info when a
653 * response is received. */
654 static int cb_statfs_update(void *cookie, int rc)
656 struct obd_info *oinfo = cookie;
657 struct lov_request *lovreq;
658 struct lov_request_set *set;
659 struct obd_statfs *osfs, *lov_sfs;
661 struct lov_tgt_desc *tgt;
662 struct obd_device *lovobd, *tgtobd;
666 lovreq = container_of(oinfo, struct lov_request, rq_oi);
667 set = lovreq->rq_rqset;
668 lovobd = set->set_obd;
669 lov = &lovobd->u.lov;
670 osfs = set->set_oi->oi_osfs;
671 lov_sfs = oinfo->oi_osfs;
672 success = atomic_read(&set->set_success);
673 /* XXX: the same is done in lov_update_common_set, however
674 lovset->set_exp is not initialized. */
675 lov_update_set(set, lovreq, rc);
680 tgt = lov->lov_tgts[lovreq->rq_idx];
681 if (!tgt || !tgt->ltd_active)
682 GOTO(out_update, rc);
684 tgtobd = class_exp2obd(tgt->ltd_exp);
685 spin_lock(&tgtobd->obd_osfs_lock);
686 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
687 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
688 tgtobd->obd_osfs_age = cfs_time_current_64();
689 spin_unlock(&tgtobd->obd_osfs_lock);
692 lov_update_statfs(osfs, lov_sfs, success);
696 if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
697 lov_set_finished(set, 0)) {
698 lov_statfs_interpret(NULL, set, set->set_count !=
699 atomic_read(&set->set_success));
705 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
706 struct lov_request_set **reqset)
708 struct lov_request_set *set;
709 struct lov_obd *lov = &obd->u.lov;
713 OBD_ALLOC(set, sizeof(*set));
721 /* We only get block data from the OBD */
722 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
723 struct lov_request *req;
725 if (lov->lov_tgts[i] == NULL ||
726 (oinfo->oi_flags & OBD_STATFS_NODELAY &&
727 !lov->lov_tgts[i]->ltd_active)) {
728 CDEBUG(D_HA, "lov idx %d inactive\n", i);
732 if (!lov->lov_tgts[i]->ltd_active)
733 lov_check_and_wait_active(lov, i);
735 /* skip targets that have been explicitely disabled by the
737 if (!lov->lov_tgts[i]->ltd_exp) {
738 CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
742 OBD_ALLOC(req, sizeof(*req));
744 GOTO(out_set, rc = -ENOMEM);
746 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
747 if (req->rq_oi.oi_osfs == NULL) {
748 OBD_FREE(req, sizeof(*req));
749 GOTO(out_set, rc = -ENOMEM);
753 req->rq_oi.oi_cb_up = cb_statfs_update;
754 req->rq_oi.oi_flags = oinfo->oi_flags;
756 lov_set_add_req(req, set);
759 GOTO(out_set, rc = -EIO);
763 lov_fini_statfs_set(set);