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 CFS_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 cfs_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)
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 cfs_list_for_each (pos, &set->set_list) {
215 req = cfs_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_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. */
297 GOTO(out_set, rc = -EIO);
301 OBD_ALLOC(req, sizeof(*req));
303 GOTO(out_set, rc = -ENOMEM);
306 req->rq_idx = loi->loi_ost_idx;
308 OBDO_ALLOC(req->rq_oi.oi_oa);
309 if (req->rq_oi.oi_oa == NULL) {
310 OBD_FREE(req, sizeof(*req));
311 GOTO(out_set, rc = -ENOMEM);
313 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
314 sizeof(*req->rq_oi.oi_oa));
315 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
316 req->rq_oi.oi_cb_up = cb_getattr_update;
317 req->rq_oi.oi_capa = oinfo->oi_capa;
319 lov_set_add_req(req, set);
322 GOTO(out_set, rc = -EIO);
326 lov_fini_getattr_set(set);
330 int lov_fini_destroy_set(struct lov_request_set *set)
336 LASSERT(set->set_exp);
337 if (atomic_read(&set->set_completes)) {
338 /* FIXME update qos data here */
346 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
347 struct obdo *src_oa, struct lov_stripe_md *lsm,
348 struct obd_trans_info *oti,
349 struct lov_request_set **reqset)
351 struct lov_request_set *set;
352 struct lov_obd *lov = &exp->exp_obd->u.lov;
356 OBD_ALLOC(set, sizeof(*set));
363 set->set_oi->oi_md = lsm;
364 set->set_oi->oi_oa = src_oa;
365 if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
366 set->set_cookies = oti->oti_logcookies;
368 for (i = 0; i < lsm->lsm_stripe_count; i++) {
369 struct lov_oinfo *loi;
370 struct lov_request *req;
372 loi = lsm->lsm_oinfo[i];
373 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
374 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
378 OBD_ALLOC(req, sizeof(*req));
380 GOTO(out_set, rc = -ENOMEM);
383 req->rq_idx = loi->loi_ost_idx;
385 OBDO_ALLOC(req->rq_oi.oi_oa);
386 if (req->rq_oi.oi_oa == NULL) {
387 OBD_FREE(req, sizeof(*req));
388 GOTO(out_set, rc = -ENOMEM);
390 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
391 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
392 lov_set_add_req(req, set);
395 GOTO(out_set, rc = -EIO);
399 lov_fini_destroy_set(set);
403 int lov_fini_setattr_set(struct lov_request_set *set)
410 LASSERT(set->set_exp);
411 if (atomic_read(&set->set_completes)) {
412 rc = common_attr_done(set);
413 /* FIXME update qos data here */
420 int lov_update_setattr_set(struct lov_request_set *set,
421 struct lov_request *req, int rc)
423 struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
424 struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
427 lov_update_set(set, req, rc);
429 /* grace error on inactive ost */
430 if (rc && !(lov->lov_tgts[req->rq_idx] &&
431 lov->lov_tgts[req->rq_idx]->ltd_active))
435 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
436 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
437 req->rq_oi.oi_oa->o_ctime;
438 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
439 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
440 req->rq_oi.oi_oa->o_mtime;
441 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
442 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
443 req->rq_oi.oi_oa->o_atime;
449 /* The callback for osc_setattr_async that finilizes a request info when a
450 * response is received. */
451 static int cb_setattr_update(void *cookie, int rc)
453 struct obd_info *oinfo = cookie;
454 struct lov_request *lovreq;
455 lovreq = container_of(oinfo, struct lov_request, rq_oi);
456 return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
459 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
460 struct obd_trans_info *oti,
461 struct lov_request_set **reqset)
463 struct lov_request_set *set;
464 struct lov_obd *lov = &exp->exp_obd->u.lov;
468 OBD_ALLOC(set, sizeof(*set));
475 if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
476 set->set_cookies = oti->oti_logcookies;
478 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
479 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
480 struct lov_request *req;
482 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
483 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
487 OBD_ALLOC(req, sizeof(*req));
489 GOTO(out_set, rc = -ENOMEM);
491 req->rq_idx = loi->loi_ost_idx;
493 OBDO_ALLOC(req->rq_oi.oi_oa);
494 if (req->rq_oi.oi_oa == NULL) {
495 OBD_FREE(req, sizeof(*req));
496 GOTO(out_set, rc = -ENOMEM);
498 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
499 sizeof(*req->rq_oi.oi_oa));
500 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
501 req->rq_oi.oi_oa->o_stripe_idx = i;
502 req->rq_oi.oi_cb_up = cb_setattr_update;
503 req->rq_oi.oi_capa = oinfo->oi_capa;
505 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
506 int off = lov_stripe_offset(oinfo->oi_md,
507 oinfo->oi_oa->o_size, i,
508 &req->rq_oi.oi_oa->o_size);
510 if (off < 0 && req->rq_oi.oi_oa->o_size)
511 req->rq_oi.oi_oa->o_size--;
513 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
514 i, req->rq_oi.oi_oa->o_size,
515 oinfo->oi_oa->o_size);
517 lov_set_add_req(req, set);
520 GOTO(out_set, rc = -EIO);
524 lov_fini_setattr_set(set);
528 #define LOV_U64_MAX ((__u64)~0ULL)
529 #define LOV_SUM_MAX(tot, add) \
531 if ((tot) + (add) < (tot)) \
532 (tot) = LOV_U64_MAX; \
537 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
542 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
544 if (osfs->os_files != LOV_U64_MAX)
545 lov_do_div64(osfs->os_files, expected_stripes);
546 if (osfs->os_ffree != LOV_U64_MAX)
547 lov_do_div64(osfs->os_ffree, expected_stripes);
549 spin_lock(&obd->obd_osfs_lock);
550 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
551 obd->obd_osfs_age = cfs_time_current_64();
552 spin_unlock(&obd->obd_osfs_lock);
559 int lov_fini_statfs_set(struct lov_request_set *set)
567 if (atomic_read(&set->set_completes)) {
568 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
569 atomic_read(&set->set_success));
575 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
578 int shift = 0, quit = 0;
582 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
584 if (osfs->os_bsize != lov_sfs->os_bsize) {
585 /* assume all block sizes are always powers of 2 */
586 /* get the bits difference */
587 tmp = osfs->os_bsize | lov_sfs->os_bsize;
588 for (shift = 0; shift <= 64; ++shift) {
600 if (osfs->os_bsize < lov_sfs->os_bsize) {
601 osfs->os_bsize = lov_sfs->os_bsize;
603 osfs->os_bfree >>= shift;
604 osfs->os_bavail >>= shift;
605 osfs->os_blocks >>= shift;
606 } else if (shift != 0) {
607 lov_sfs->os_bfree >>= shift;
608 lov_sfs->os_bavail >>= shift;
609 lov_sfs->os_blocks >>= shift;
612 /* Sandia requested that df (and so, statfs) only
613 returned minimal available space on
614 a single OST, so people would be able to
615 write this much data guaranteed. */
616 if (osfs->os_bavail > lov_sfs->os_bavail) {
617 /* Presumably if new bavail is smaller,
618 new bfree is bigger as well */
619 osfs->os_bfree = lov_sfs->os_bfree;
620 osfs->os_bavail = lov_sfs->os_bavail;
623 osfs->os_bfree += lov_sfs->os_bfree;
624 osfs->os_bavail += lov_sfs->os_bavail;
626 osfs->os_blocks += lov_sfs->os_blocks;
627 /* XXX not sure about this one - depends on policy.
628 * - could be minimum if we always stripe on all OBDs
629 * (but that would be wrong for any other policy,
630 * if one of the OBDs has no more objects left)
631 * - could be sum if we stripe whole objects
632 * - could be average, just to give a nice number
634 * To give a "reasonable" (if not wholly accurate)
635 * number, we divide the total number of free objects
636 * by expected stripe count (watch out for overflow).
638 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
639 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
643 /* The callback for osc_statfs_async that finilizes a request info when a
644 * response is received. */
645 static int cb_statfs_update(void *cookie, int rc)
647 struct obd_info *oinfo = cookie;
648 struct lov_request *lovreq;
649 struct lov_request_set *set;
650 struct obd_statfs *osfs, *lov_sfs;
652 struct lov_tgt_desc *tgt;
653 struct obd_device *lovobd, *tgtobd;
657 lovreq = container_of(oinfo, struct lov_request, rq_oi);
658 set = lovreq->rq_rqset;
659 lovobd = set->set_obd;
660 lov = &lovobd->u.lov;
661 osfs = set->set_oi->oi_osfs;
662 lov_sfs = oinfo->oi_osfs;
663 success = atomic_read(&set->set_success);
664 /* XXX: the same is done in lov_update_common_set, however
665 lovset->set_exp is not initialized. */
666 lov_update_set(set, lovreq, rc);
671 tgt = lov->lov_tgts[lovreq->rq_idx];
672 if (!tgt || !tgt->ltd_active)
673 GOTO(out_update, rc);
675 tgtobd = class_exp2obd(tgt->ltd_exp);
676 spin_lock(&tgtobd->obd_osfs_lock);
677 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
678 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
679 tgtobd->obd_osfs_age = cfs_time_current_64();
680 spin_unlock(&tgtobd->obd_osfs_lock);
683 lov_update_statfs(osfs, lov_sfs, success);
687 if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
688 lov_set_finished(set, 0)) {
689 lov_statfs_interpret(NULL, set, set->set_count !=
690 atomic_read(&set->set_success));
696 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
697 struct lov_request_set **reqset)
699 struct lov_request_set *set;
700 struct lov_obd *lov = &obd->u.lov;
704 OBD_ALLOC(set, sizeof(*set));
712 /* We only get block data from the OBD */
713 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
714 struct lov_request *req;
716 if (lov->lov_tgts[i] == NULL ||
717 (oinfo->oi_flags & OBD_STATFS_NODELAY &&
718 !lov->lov_tgts[i]->ltd_active)) {
719 CDEBUG(D_HA, "lov idx %d inactive\n", i);
723 if (!lov->lov_tgts[i]->ltd_active)
724 lov_check_and_wait_active(lov, i);
726 /* skip targets that have been explicitely disabled by the
728 if (!lov->lov_tgts[i]->ltd_exp) {
729 CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
733 OBD_ALLOC(req, sizeof(*req));
735 GOTO(out_set, rc = -ENOMEM);
737 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
738 if (req->rq_oi.oi_osfs == NULL) {
739 OBD_FREE(req, sizeof(*req));
740 GOTO(out_set, rc = -ENOMEM);
744 req->rq_oi.oi_cb_up = cb_statfs_update;
745 req->rq_oi.oi_flags = oinfo->oi_flags;
747 lov_set_add_req(req, set);
750 GOTO(out_set, rc = -EIO);
754 lov_fini_statfs_set(set);