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 OBDO_FREE(req->rq_oi.oi_oa);
72 OBD_FREE_LARGE(req->rq_oi.oi_md, req->rq_buflen);
73 if (req->rq_oi.oi_osfs)
74 OBD_FREE(req->rq_oi.oi_osfs,
75 sizeof(*req->rq_oi.oi_osfs));
76 OBD_FREE(req, sizeof(*req));
80 int len = set->set_oabufs * sizeof(*set->set_pga);
81 OBD_FREE_LARGE(set->set_pga, len);
84 lov_llh_put(set->set_lockh);
86 OBD_FREE(set, sizeof(*set));
89 int lov_set_finished(struct lov_request_set *set, int idempotent)
91 int completes = atomic_read(&set->set_completes);
93 CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
95 if (completes == set->set_count) {
98 if (atomic_inc_return(&set->set_finish_checked) == 1)
104 void lov_update_set(struct lov_request_set *set,
105 struct lov_request *req, int rc)
107 req->rq_complete = 1;
110 atomic_inc(&set->set_completes);
112 atomic_inc(&set->set_success);
114 wake_up(&set->set_waitq);
117 int lov_update_common_set(struct lov_request_set *set,
118 struct lov_request *req, int rc)
120 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 list_add_tail(&req->rq_link, &set->set_list);
140 static int lov_check_set(struct lov_obd *lov, int idx)
143 struct lov_tgt_desc *tgt;
145 mutex_lock(&lov->lov_lock);
146 tgt = lov->lov_tgts[idx];
147 rc = !tgt || tgt->ltd_active ||
149 class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried);
150 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);
186 if (tgt != NULL && tgt->ltd_active)
192 mutex_unlock(&lov->lov_lock);
196 static int common_attr_done(struct lov_request_set *set)
198 struct list_head *pos;
199 struct lov_request *req;
201 int rc = 0, attrset = 0;
203 LASSERT(set->set_oi != NULL);
205 if (set->set_oi->oi_oa == NULL)
208 if (!atomic_read(&set->set_success))
213 GOTO(out, rc = -ENOMEM);
215 list_for_each(pos, &set->set_list) {
216 req = list_entry(pos, struct lov_request, rq_link);
218 if (!req->rq_complete || req->rq_rc)
220 if (req->rq_oi.oi_oa->o_valid == 0) /* inactive stripe */
222 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
223 req->rq_oi.oi_oa->o_valid,
224 set->set_oi->oi_md, req->rq_stripe, &attrset);
227 CERROR("No stripes had valid attrs\n");
230 if ((set->set_oi->oi_oa->o_valid & OBD_MD_FLEPOCH) &&
231 (set->set_oi->oi_md->lsm_stripe_count != attrset)) {
232 /* When we take attributes of some epoch, we require all the
233 * ost to be active. */
234 CERROR("Not all the stripes had valid attrs\n");
235 GOTO(out, rc = -EIO);
238 tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
239 memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
247 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 finalizes 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;
269 lovreq = container_of(oinfo, struct lov_request, rq_oi);
270 return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
273 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
274 struct lov_request_set **reqset)
276 struct lov_request_set *set;
277 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)
334 LASSERT(set->set_exp);
335 if (atomic_read(&set->set_completes)) {
336 /* FIXME update qos data here */
344 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
345 struct obdo *src_oa, struct lov_stripe_md *lsm,
346 struct obd_trans_info *oti,
347 struct lov_request_set **reqset)
349 struct lov_request_set *set;
350 struct lov_obd *lov = &exp->exp_obd->u.lov;
353 OBD_ALLOC(set, sizeof(*set));
360 set->set_oi->oi_md = lsm;
361 set->set_oi->oi_oa = src_oa;
363 if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
364 set->set_cookies = oti->oti_logcookies;
366 for (i = 0; i < lsm->lsm_stripe_count; i++) {
367 struct lov_oinfo *loi;
368 struct lov_request *req;
370 loi = lsm->lsm_oinfo[i];
371 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
372 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
376 OBD_ALLOC(req, sizeof(*req));
378 GOTO(out_set, rc = -ENOMEM);
381 req->rq_idx = loi->loi_ost_idx;
383 OBDO_ALLOC(req->rq_oi.oi_oa);
384 if (req->rq_oi.oi_oa == NULL) {
385 OBD_FREE(req, sizeof(*req));
386 GOTO(out_set, rc = -ENOMEM);
388 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
389 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
390 lov_set_add_req(req, set);
393 GOTO(out_set, rc = -EIO);
397 lov_fini_destroy_set(set);
401 int lov_fini_setattr_set(struct lov_request_set *set)
407 LASSERT(set->set_exp);
408 if (atomic_read(&set->set_completes)) {
409 rc = common_attr_done(set);
410 /* FIXME update qos data here */
417 int lov_update_setattr_set(struct lov_request_set *set,
418 struct lov_request *req, int rc)
420 struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
421 struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
423 lov_update_set(set, req, rc);
425 /* grace error on inactive ost */
426 if (rc && !(lov->lov_tgts[req->rq_idx] &&
427 lov->lov_tgts[req->rq_idx]->ltd_active))
431 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
432 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
433 req->rq_oi.oi_oa->o_ctime;
434 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
435 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
436 req->rq_oi.oi_oa->o_mtime;
437 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
438 lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
439 req->rq_oi.oi_oa->o_atime;
445 /* The callback for osc_setattr_async that finalizes a request info when a
446 * response is received. */
447 static int cb_setattr_update(void *cookie, int rc)
449 struct obd_info *oinfo = cookie;
450 struct lov_request *lovreq;
452 lovreq = container_of(oinfo, struct lov_request, rq_oi);
453 return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
456 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
457 struct obd_trans_info *oti,
458 struct lov_request_set **reqset)
460 struct lov_request_set *set;
461 struct lov_obd *lov = &exp->exp_obd->u.lov;
464 OBD_ALLOC(set, sizeof(*set));
472 if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
473 set->set_cookies = oti->oti_logcookies;
475 for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
476 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
477 struct lov_request *req;
479 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
480 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
484 OBD_ALLOC(req, sizeof(*req));
486 GOTO(out_set, rc = -ENOMEM);
488 req->rq_idx = loi->loi_ost_idx;
490 OBDO_ALLOC(req->rq_oi.oi_oa);
491 if (req->rq_oi.oi_oa == NULL) {
492 OBD_FREE(req, sizeof(*req));
493 GOTO(out_set, rc = -ENOMEM);
495 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
496 sizeof(*req->rq_oi.oi_oa));
497 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
498 req->rq_oi.oi_oa->o_stripe_idx = i;
499 req->rq_oi.oi_cb_up = cb_setattr_update;
500 req->rq_oi.oi_capa = oinfo->oi_capa;
502 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
503 int off = lov_stripe_offset(oinfo->oi_md,
504 oinfo->oi_oa->o_size, i,
505 &req->rq_oi.oi_oa->o_size);
507 if (off < 0 && req->rq_oi.oi_oa->o_size)
508 req->rq_oi.oi_oa->o_size--;
510 CDEBUG(D_INODE, "stripe %d has size %llu/%llu\n",
511 i, req->rq_oi.oi_oa->o_size,
512 oinfo->oi_oa->o_size);
514 lov_set_add_req(req, set);
517 GOTO(out_set, rc = -EIO);
521 lov_fini_setattr_set(set);
525 #define LOV_U64_MAX ((__u64)~0ULL)
526 #define LOV_SUM_MAX(tot, add) \
528 if ((tot) + (add) < (tot)) \
529 (tot) = LOV_U64_MAX; \
534 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,
538 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
540 if (osfs->os_files != LOV_U64_MAX)
541 lov_do_div64(osfs->os_files, expected_stripes);
542 if (osfs->os_ffree != LOV_U64_MAX)
543 lov_do_div64(osfs->os_ffree, expected_stripes);
545 spin_lock(&obd->obd_osfs_lock);
546 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
547 obd->obd_osfs_age = cfs_time_current_64();
548 spin_unlock(&obd->obd_osfs_lock);
555 int lov_fini_statfs_set(struct lov_request_set *set)
562 if (atomic_read(&set->set_completes)) {
563 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
564 atomic_read(&set->set_success));
570 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
573 int shift = 0, quit = 0;
577 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
579 if (osfs->os_bsize != lov_sfs->os_bsize) {
580 /* assume all block sizes are always powers of 2 */
581 /* get the bits difference */
582 tmp = osfs->os_bsize | lov_sfs->os_bsize;
583 for (shift = 0; shift <= 64; ++shift) {
595 if (osfs->os_bsize < lov_sfs->os_bsize) {
596 osfs->os_bsize = lov_sfs->os_bsize;
598 osfs->os_bfree >>= shift;
599 osfs->os_bavail >>= shift;
600 osfs->os_blocks >>= shift;
601 } else if (shift != 0) {
602 lov_sfs->os_bfree >>= shift;
603 lov_sfs->os_bavail >>= shift;
604 lov_sfs->os_blocks >>= shift;
606 osfs->os_bfree += lov_sfs->os_bfree;
607 osfs->os_bavail += lov_sfs->os_bavail;
608 osfs->os_blocks += lov_sfs->os_blocks;
609 /* XXX not sure about this one - depends on policy.
610 * - could be minimum if we always stripe on all OBDs
611 * (but that would be wrong for any other policy,
612 * if one of the OBDs has no more objects left)
613 * - could be sum if we stripe whole objects
614 * - could be average, just to give a nice number
616 * To give a "reasonable" (if not wholly accurate)
617 * number, we divide the total number of free objects
618 * by expected stripe count (watch out for overflow).
620 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
621 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
625 /* The callback for osc_statfs_async that finalizes a request info when a
626 * response is received. */
627 static int cb_statfs_update(void *cookie, int rc)
629 struct obd_info *oinfo = cookie;
630 struct lov_request *lovreq;
631 struct lov_request_set *set;
632 struct obd_statfs *osfs, *lov_sfs;
634 struct lov_tgt_desc *tgt;
635 struct obd_device *lovobd, *tgtobd;
638 lovreq = container_of(oinfo, struct lov_request, rq_oi);
639 set = lovreq->rq_rqset;
640 lovobd = set->set_obd;
641 lov = &lovobd->u.lov;
642 osfs = set->set_oi->oi_osfs;
643 lov_sfs = oinfo->oi_osfs;
644 success = atomic_read(&set->set_success);
645 /* XXX: the same is done in lov_update_common_set, however
646 lovset->set_exp is not initialized. */
647 lov_update_set(set, lovreq, rc);
652 tgt = lov->lov_tgts[lovreq->rq_idx];
653 if (!tgt || !tgt->ltd_active)
654 GOTO(out_update, rc);
656 tgtobd = class_exp2obd(tgt->ltd_exp);
657 spin_lock(&tgtobd->obd_osfs_lock);
658 memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
659 if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
660 tgtobd->obd_osfs_age = cfs_time_current_64();
661 spin_unlock(&tgtobd->obd_osfs_lock);
664 lov_update_statfs(osfs, lov_sfs, success);
668 if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
669 lov_set_finished(set, 0)) {
670 lov_statfs_interpret(NULL, set, set->set_count !=
671 atomic_read(&set->set_success));
677 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
678 struct lov_request_set **reqset)
680 struct lov_request_set *set;
681 struct lov_obd *lov = &obd->u.lov;
684 OBD_ALLOC(set, sizeof(*set));
692 /* We only get block data from the OBD */
693 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
694 struct lov_request *req;
696 if (lov->lov_tgts[i] == NULL ||
697 (!lov_check_and_wait_active(lov, i) &&
698 (oinfo->oi_flags & OBD_STATFS_NODELAY))) {
699 CDEBUG(D_HA, "lov idx %d inactive\n", i);
703 /* skip targets that have been explicitly disabled by the
705 if (!lov->lov_tgts[i]->ltd_exp) {
706 CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
710 OBD_ALLOC(req, sizeof(*req));
712 GOTO(out_set, rc = -ENOMEM);
714 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
715 if (req->rq_oi.oi_osfs == NULL) {
716 OBD_FREE(req, sizeof(*req));
717 GOTO(out_set, rc = -ENOMEM);
721 req->rq_oi.oi_cb_up = cb_statfs_update;
722 req->rq_oi.oi_flags = oinfo->oi_flags;
724 lov_set_add_req(req, set);
727 GOTO(out_set, rc = -EIO);
731 lov_fini_statfs_set(set);