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>
48 #include <lustre/lustre_idl.h>
50 #include "lov_internal.h"
52 static void lov_init_set(struct lov_request_set *set)
55 cfs_atomic_set(&set->set_completes, 0);
56 cfs_atomic_set(&set->set_success, 0);
57 cfs_atomic_set(&set->set_finish_checked, 0);
59 CFS_INIT_LIST_HEAD(&set->set_list);
60 cfs_atomic_set(&set->set_refcount, 1);
61 init_waitqueue_head(&set->set_waitq);
64 void lov_finish_set(struct lov_request_set *set)
66 struct list_head *pos, *n;
67 struct lov_request *req;
71 list_for_each_safe(pos, n, &set->set_list) {
72 req = list_entry(pos, struct lov_request, rq_link);
73 list_del_init(&req->rq_link);
75 if (req->rq_oi.oi_oa != NULL)
76 OBDO_FREE(req->rq_oi.oi_oa);
78 if (req->rq_oi.oi_osfs != NULL)
79 OBD_FREE_PTR(req->rq_oi.oi_osfs);
88 int lov_set_finished(struct lov_request_set *set, int idempotent)
90 int completes = cfs_atomic_read(&set->set_completes);
92 CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
94 if (completes == set->set_count) {
97 if (cfs_atomic_inc_return(&set->set_finish_checked) == 1)
103 void lov_update_set(struct lov_request_set *set,
104 struct lov_request *req, int rc)
106 req->rq_complete = 1;
109 cfs_atomic_inc(&set->set_completes);
111 cfs_atomic_inc(&set->set_success);
113 wake_up(&set->set_waitq);
116 int lov_update_common_set(struct lov_request_set *set,
117 struct lov_request *req, int rc)
119 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
122 lov_update_set(set, req, rc);
124 /* grace error on inactive ost */
125 if (rc && !(lov->lov_tgts[req->rq_idx] &&
126 lov->lov_tgts[req->rq_idx]->ltd_active))
129 /* FIXME in raid1 regime, should return 0 */
133 void lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
135 cfs_list_add_tail(&req->rq_link, &set->set_list);
140 static int lov_check_set(struct lov_obd *lov, int idx)
143 mutex_lock(&lov->lov_lock);
145 if (lov->lov_tgts[idx] == NULL ||
146 lov->lov_tgts[idx]->ltd_active ||
147 (lov->lov_tgts[idx]->ltd_exp != NULL &&
148 class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
151 mutex_unlock(&lov->lov_lock);
155 /* Check if the OSC connection exists and is active.
156 * If the OSC has not yet had a chance to connect to the OST the first time,
157 * wait once for it to connect instead of returning an error.
159 int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
161 wait_queue_head_t waitq;
162 struct l_wait_info lwi;
163 struct lov_tgt_desc *tgt;
166 mutex_lock(&lov->lov_lock);
168 tgt = lov->lov_tgts[ost_idx];
170 if (unlikely(tgt == NULL))
173 if (likely(tgt->ltd_active))
176 if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
179 mutex_unlock(&lov->lov_lock);
181 init_waitqueue_head(&waitq);
182 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
183 cfs_time_seconds(1), NULL, NULL);
185 rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
192 mutex_unlock(&lov->lov_lock);
196 static int common_attr_done(struct lov_request_set *set)
199 struct lov_request *req;
201 int rc = 0, attrset = 0;
204 LASSERT(set->set_oi != NULL);
206 if (set->set_oi->oi_oa == NULL)
209 if (!cfs_atomic_read(&set->set_success))
214 GOTO(out, rc = -ENOMEM);
216 cfs_list_for_each (pos, &set->set_list) {
217 req = cfs_list_entry(pos, struct lov_request, rq_link);
219 if (!req->rq_complete || req->rq_rc)
221 if (req->rq_oi.oi_oa->o_valid == 0) /* inactive stripe */
223 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
224 req->rq_oi.oi_oa->o_valid,
225 set->set_oi->oi_md, req->rq_stripe, &attrset);
228 CERROR("No stripes had valid attrs\n");
231 if ((set->set_oi->oi_oa->o_valid & OBD_MD_FLEPOCH) &&
232 (set->set_oi->oi_md->lsm_stripe_count != attrset)) {
233 /* When we take attributes of some epoch, we require all the
234 * ost to be active. */
235 CERROR("Not all the stripes had valid attrs\n");
236 GOTO(out, rc = -EIO);
239 tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
240 memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
248 int lov_fini_getattr_set(struct lov_request_set *set)
255 LASSERT(set->set_exp);
256 if (cfs_atomic_read(&set->set_completes))
257 rc = common_attr_done(set);
264 /* The callback for osc_getattr_async that finilizes a request info when a
265 * response is received. */
266 static int cb_getattr_update(void *cookie, int rc)
268 struct obd_info *oinfo = cookie;
269 struct lov_request *lovreq;
270 lovreq = container_of(oinfo, struct lov_request, rq_oi);
271 return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
274 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
275 struct lov_request_set **reqset)
277 struct lov_request_set *set;
278 struct lov_obd *lov = &exp->exp_obd->u.lov;
282 OBD_ALLOC(set, sizeof(*set));
290 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
291 struct lov_oinfo *loi;
292 struct lov_request *req;
294 loi = oinfo->oi_md->lsm_oinfo[i];
295 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
296 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
297 if (oinfo->oi_oa->o_valid & OBD_MD_FLEPOCH)
298 /* SOM requires all the OSTs to be active. */
299 GOTO(out_set, rc = -EIO);
303 OBD_ALLOC(req, sizeof(*req));
305 GOTO(out_set, rc = -ENOMEM);
308 req->rq_idx = loi->loi_ost_idx;
310 OBDO_ALLOC(req->rq_oi.oi_oa);
311 if (req->rq_oi.oi_oa == NULL) {
312 OBD_FREE(req, sizeof(*req));
313 GOTO(out_set, rc = -ENOMEM);
315 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
316 sizeof(*req->rq_oi.oi_oa));
317 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
318 req->rq_oi.oi_cb_up = cb_getattr_update;
319 req->rq_oi.oi_capa = oinfo->oi_capa;
321 lov_set_add_req(req, set);
324 GOTO(out_set, rc = -EIO);
328 lov_fini_getattr_set(set);
332 int lov_fini_destroy_set(struct lov_request_set *set)
338 LASSERT(set->set_exp);
339 if (cfs_atomic_read(&set->set_completes)) {
340 /* FIXME update qos data here */
348 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
349 struct obdo *src_oa, struct lov_stripe_md *lsm,
350 struct obd_trans_info *oti,
351 struct lov_request_set **reqset)
353 struct lov_request_set *set;
354 struct lov_obd *lov = &exp->exp_obd->u.lov;
358 OBD_ALLOC(set, sizeof(*set));
365 set->set_oi->oi_md = lsm;
366 set->set_oi->oi_oa = src_oa;
367 if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
368 set->set_cookies = oti->oti_logcookies;
370 for (i = 0; i < lsm->lsm_stripe_count; i++) {
371 struct lov_oinfo *loi;
372 struct lov_request *req;
374 loi = lsm->lsm_oinfo[i];
375 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
376 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
380 OBD_ALLOC(req, sizeof(*req));
382 GOTO(out_set, rc = -ENOMEM);
385 req->rq_idx = loi->loi_ost_idx;
387 OBDO_ALLOC(req->rq_oi.oi_oa);
388 if (req->rq_oi.oi_oa == NULL) {
389 OBD_FREE(req, sizeof(*req));
390 GOTO(out_set, rc = -ENOMEM);
392 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
393 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
394 lov_set_add_req(req, set);
397 GOTO(out_set, rc = -EIO);
401 lov_fini_destroy_set(set);
405 int lov_fini_setattr_set(struct lov_request_set *set)
412 LASSERT(set->set_exp);
413 if (cfs_atomic_read(&set->set_completes)) {
414 rc = common_attr_done(set);
415 /* FIXME update qos data here */
422 int lov_update_setattr_set(struct lov_request_set *set,
423 struct lov_request *req, int rc)
425 struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
426 struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
429 lov_update_set(set, req, rc);
431 /* grace error on inactive ost */
432 if (rc && !(lov->lov_tgts[req->rq_idx] &&
433 lov->lov_tgts[req->rq_idx]->ltd_active))
437 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
438 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
439 req->rq_oi.oi_oa->o_ctime;
440 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
441 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
442 req->rq_oi.oi_oa->o_mtime;
443 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
444 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
445 req->rq_oi.oi_oa->o_atime;
451 /* The callback for osc_setattr_async that finilizes a request info when a
452 * response is received. */
453 static int cb_setattr_update(void *cookie, int rc)
455 struct obd_info *oinfo = cookie;
456 struct lov_request *lovreq;
457 lovreq = container_of(oinfo, struct lov_request, rq_oi);
458 return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
461 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
462 struct obd_trans_info *oti,
463 struct lov_request_set **reqset)
465 struct lov_request_set *set;
466 struct lov_obd *lov = &exp->exp_obd->u.lov;
470 OBD_ALLOC(set, sizeof(*set));
477 if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
478 set->set_cookies = oti->oti_logcookies;
480 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
481 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
482 struct lov_request *req;
484 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
485 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
489 OBD_ALLOC(req, sizeof(*req));
491 GOTO(out_set, rc = -ENOMEM);
493 req->rq_idx = loi->loi_ost_idx;
495 OBDO_ALLOC(req->rq_oi.oi_oa);
496 if (req->rq_oi.oi_oa == NULL) {
497 OBD_FREE(req, sizeof(*req));
498 GOTO(out_set, rc = -ENOMEM);
500 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
501 sizeof(*req->rq_oi.oi_oa));
502 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
503 req->rq_oi.oi_oa->o_stripe_idx = i;
504 req->rq_oi.oi_cb_up = cb_setattr_update;
505 req->rq_oi.oi_capa = oinfo->oi_capa;
507 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
508 int off = lov_stripe_offset(oinfo->oi_md,
509 oinfo->oi_oa->o_size, i,
510 &req->rq_oi.oi_oa->o_size);
512 if (off < 0 && req->rq_oi.oi_oa->o_size)
513 req->rq_oi.oi_oa->o_size--;
515 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
516 i, req->rq_oi.oi_oa->o_size,
517 oinfo->oi_oa->o_size);
519 lov_set_add_req(req, set);
522 GOTO(out_set, rc = -EIO);
526 lov_fini_setattr_set(set);
530 #define LOV_U64_MAX ((__u64)~0ULL)
531 #define LOV_SUM_MAX(tot, add) \
533 if ((tot) + (add) < (tot)) \
534 (tot) = LOV_U64_MAX; \
539 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
544 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
546 if (osfs->os_files != LOV_U64_MAX)
547 lov_do_div64(osfs->os_files, expected_stripes);
548 if (osfs->os_ffree != LOV_U64_MAX)
549 lov_do_div64(osfs->os_ffree, expected_stripes);
551 spin_lock(&obd->obd_osfs_lock);
552 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
553 obd->obd_osfs_age = cfs_time_current_64();
554 spin_unlock(&obd->obd_osfs_lock);
561 int lov_fini_statfs_set(struct lov_request_set *set)
569 if (cfs_atomic_read(&set->set_completes)) {
570 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
571 cfs_atomic_read(&set->set_success));
577 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
580 int shift = 0, quit = 0;
584 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
586 if (osfs->os_bsize != lov_sfs->os_bsize) {
587 /* assume all block sizes are always powers of 2 */
588 /* get the bits difference */
589 tmp = osfs->os_bsize | lov_sfs->os_bsize;
590 for (shift = 0; shift <= 64; ++shift) {
602 if (osfs->os_bsize < lov_sfs->os_bsize) {
603 osfs->os_bsize = lov_sfs->os_bsize;
605 osfs->os_bfree >>= shift;
606 osfs->os_bavail >>= shift;
607 osfs->os_blocks >>= shift;
608 } else if (shift != 0) {
609 lov_sfs->os_bfree >>= shift;
610 lov_sfs->os_bavail >>= shift;
611 lov_sfs->os_blocks >>= shift;
614 /* Sandia requested that df (and so, statfs) only
615 returned minimal available space on
616 a single OST, so people would be able to
617 write this much data guaranteed. */
618 if (osfs->os_bavail > lov_sfs->os_bavail) {
619 /* Presumably if new bavail is smaller,
620 new bfree is bigger as well */
621 osfs->os_bfree = lov_sfs->os_bfree;
622 osfs->os_bavail = lov_sfs->os_bavail;
625 osfs->os_bfree += lov_sfs->os_bfree;
626 osfs->os_bavail += lov_sfs->os_bavail;
628 osfs->os_blocks += lov_sfs->os_blocks;
629 /* XXX not sure about this one - depends on policy.
630 * - could be minimum if we always stripe on all OBDs
631 * (but that would be wrong for any other policy,
632 * if one of the OBDs has no more objects left)
633 * - could be sum if we stripe whole objects
634 * - could be average, just to give a nice number
636 * To give a "reasonable" (if not wholly accurate)
637 * number, we divide the total number of free objects
638 * by expected stripe count (watch out for overflow).
640 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
641 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
645 /* The callback for osc_statfs_async that finilizes a request info when a
646 * response is received. */
647 static int cb_statfs_update(void *cookie, int rc)
649 struct obd_info *oinfo = cookie;
650 struct lov_request *lovreq;
651 struct lov_request_set *set;
652 struct obd_statfs *osfs, *lov_sfs;
654 struct lov_tgt_desc *tgt;
655 struct obd_device *lovobd, *tgtobd;
659 lovreq = container_of(oinfo, struct lov_request, rq_oi);
660 set = lovreq->rq_rqset;
661 lovobd = set->set_obd;
662 lov = &lovobd->u.lov;
663 osfs = set->set_oi->oi_osfs;
664 lov_sfs = oinfo->oi_osfs;
665 success = cfs_atomic_read(&set->set_success);
666 /* XXX: the same is done in lov_update_common_set, however
667 lovset->set_exp is not initialized. */
668 lov_update_set(set, lovreq, rc);
673 tgt = lov->lov_tgts[lovreq->rq_idx];
674 if (!tgt || !tgt->ltd_active)
675 GOTO(out_update, rc);
677 tgtobd = class_exp2obd(tgt->ltd_exp);
678 spin_lock(&tgtobd->obd_osfs_lock);
679 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
680 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
681 tgtobd->obd_osfs_age = cfs_time_current_64();
682 spin_unlock(&tgtobd->obd_osfs_lock);
685 lov_update_statfs(osfs, lov_sfs, success);
689 if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
690 lov_set_finished(set, 0)) {
691 lov_statfs_interpret(NULL, set, set->set_count !=
692 cfs_atomic_read(&set->set_success));
698 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
699 struct lov_request_set **reqset)
701 struct lov_request_set *set;
702 struct lov_obd *lov = &obd->u.lov;
706 OBD_ALLOC(set, sizeof(*set));
714 /* We only get block data from the OBD */
715 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
716 struct lov_request *req;
718 if (lov->lov_tgts[i] == NULL ||
719 (oinfo->oi_flags & OBD_STATFS_NODELAY &&
720 !lov->lov_tgts[i]->ltd_active)) {
721 CDEBUG(D_HA, "lov idx %d inactive\n", i);
725 if (!lov->lov_tgts[i]->ltd_active)
726 lov_check_and_wait_active(lov, i);
728 /* skip targets that have been explicitely disabled by the
730 if (!lov->lov_tgts[i]->ltd_exp) {
731 CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
735 OBD_ALLOC(req, sizeof(*req));
737 GOTO(out_set, rc = -ENOMEM);
739 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
740 if (req->rq_oi.oi_osfs == NULL) {
741 OBD_FREE(req, sizeof(*req));
742 GOTO(out_set, rc = -ENOMEM);
746 req->rq_oi.oi_cb_up = cb_statfs_update;
747 req->rq_oi.oi_flags = oinfo->oi_flags;
749 lov_set_add_req(req, set);
752 GOTO(out_set, rc = -EIO);
756 lov_fini_statfs_set(set);