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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lov/lov_obd.c
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Peter Braam <braam@clusterfs.com>
40 * Author: Mike Shaver <shaver@clusterfs.com>
41 * Author: Nathan Rutman <nathan@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_LOV
46 #include <libcfs/libcfs.h>
48 #include <liblustre.h>
51 #include <obd_support.h>
52 #include <lustre_lib.h>
53 #include <lustre_net.h>
54 #include <lustre/lustre_idl.h>
55 #include <lustre_dlm.h>
56 #include <lustre_mds.h>
57 #include <lustre_debug.h>
58 #include <obd_class.h>
61 #include <lprocfs_status.h>
62 #include <lustre_param.h>
63 #include <cl_object.h>
64 #include <lclient.h> /* for cl_client_lru */
65 #include <lustre/ll_fiemap.h>
66 #include <lustre_log.h>
68 #include "lov_internal.h"
70 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
71 Any function that expects lov_tgts to remain stationary must take a ref. */
72 static void lov_getref(struct obd_device *obd)
74 struct lov_obd *lov = &obd->u.lov;
76 /* nobody gets through here until lov_putref is done */
77 cfs_mutex_lock(&lov->lov_lock);
78 cfs_atomic_inc(&lov->lov_refcount);
79 cfs_mutex_unlock(&lov->lov_lock);
83 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
85 static void lov_putref(struct obd_device *obd)
87 struct lov_obd *lov = &obd->u.lov;
89 cfs_mutex_lock(&lov->lov_lock);
90 /* ok to dec to 0 more than once -- ltd_exp's will be null */
91 if (cfs_atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
94 struct lov_tgt_desc *tgt, *n;
95 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
97 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
98 tgt = lov->lov_tgts[i];
100 if (!tgt || !tgt->ltd_reap)
102 cfs_list_add(&tgt->ltd_kill, &kill);
103 /* XXX - right now there is a dependency on ld_tgt_count
104 * being the maximum tgt index for computing the
105 * mds_max_easize. So we can't shrink it. */
106 lov_ost_pool_remove(&lov->lov_packed, i);
107 lov->lov_tgts[i] = NULL;
108 lov->lov_death_row--;
110 cfs_mutex_unlock(&lov->lov_lock);
112 cfs_list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
113 cfs_list_del(&tgt->ltd_kill);
115 __lov_del_obd(obd, tgt);
118 cfs_mutex_unlock(&lov->lov_lock);
122 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
123 enum obd_notify_event ev);
124 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
125 enum obd_notify_event ev, void *data);
128 #define MAX_STRING_SIZE 128
129 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
130 struct obd_connect_data *data)
132 struct lov_obd *lov = &obd->u.lov;
133 struct obd_uuid *tgt_uuid;
134 struct obd_device *tgt_obd;
135 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
136 struct obd_import *imp;
138 cfs_proc_dir_entry_t *lov_proc_dir;
143 if (!lov->lov_tgts[index])
146 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
147 tgt_obd = lov->lov_tgts[index]->ltd_obd;
149 if (!tgt_obd->obd_set_up) {
150 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
154 /* override the sp_me from lov */
155 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
157 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
158 data->ocd_index = index;
161 * Divine LOV knows that OBDs under it are OSCs.
163 imp = tgt_obd->u.cli.cl_import;
166 tgt_obd->obd_no_recov = 0;
167 /* FIXME this is probably supposed to be
168 ptlrpc_set_import_active. Horrible naming. */
169 ptlrpc_activate_import(imp);
172 rc = obd_register_observer(tgt_obd, obd);
174 CERROR("Target %s register_observer error %d\n",
175 obd_uuid2str(tgt_uuid), rc);
180 if (imp->imp_invalid) {
181 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
182 "disabled\n", obd_uuid2str(tgt_uuid));
186 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
187 &lov_osc_uuid, data, NULL);
188 if (rc || !lov->lov_tgts[index]->ltd_exp) {
189 CERROR("Target %s connect error %d\n",
190 obd_uuid2str(tgt_uuid), rc);
194 lov->lov_tgts[index]->ltd_reap = 0;
196 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
197 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
200 lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
202 struct obd_device *osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
203 cfs_proc_dir_entry_t *osc_symlink;
205 LASSERT(osc_obd != NULL);
206 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
207 LASSERT(osc_obd->obd_type->typ_name != NULL);
209 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
212 osc_obd->obd_type->typ_name,
214 if (osc_symlink == NULL) {
215 CERROR("could not register LOV target "
216 "/proc/fs/lustre/%s/%s/target_obds/%s.",
217 obd->obd_type->typ_name, obd->obd_name,
219 lprocfs_remove(&lov_proc_dir);
224 rc = qos_add_tgt(obd, index);
226 CERROR("qos_add_tgt failed %d\n", rc);
231 static int lov_connect(const struct lu_env *env,
232 struct obd_export **exp, struct obd_device *obd,
233 struct obd_uuid *cluuid, struct obd_connect_data *data,
236 struct lov_obd *lov = &obd->u.lov;
237 struct lov_tgt_desc *tgt;
238 struct lustre_handle conn;
242 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
244 rc = class_connect(&conn, obd, cluuid);
248 *exp = class_conn2export(&conn);
250 /* Why should there ever be more than 1 connect? */
252 LASSERT(lov->lov_connects == 1);
254 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
256 lov->lov_ocd = *data;
259 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
260 tgt = lov->lov_tgts[i];
261 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
263 /* Flags will be lowest common denominator */
264 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
266 CERROR("%s: lov connect tgt %d failed: %d\n",
267 obd->obd_name, i, rc);
270 /* connect to administrative disabled ost */
271 if (!lov->lov_tgts[i]->ltd_exp)
274 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
275 OBD_NOTIFY_CONNECT, (void *)&i);
277 CERROR("%s error sending notify %d\n",
286 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
288 cfs_proc_dir_entry_t *lov_proc_dir;
289 struct lov_obd *lov = &obd->u.lov;
290 struct obd_device *osc_obd;
294 osc_obd = class_exp2obd(tgt->ltd_exp);
295 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
296 obd->obd_name, osc_obd->obd_name);
298 if (tgt->ltd_active) {
300 lov->desc.ld_active_tgt_count--;
301 tgt->ltd_exp->exp_obd->obd_inactive = 1;
304 lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
306 cfs_proc_dir_entry_t *osc_symlink;
308 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
310 lprocfs_remove(&osc_symlink);
312 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing.",
313 obd->obd_type->typ_name, obd->obd_name,
319 /* Pass it on to our clients.
320 * XXX This should be an argument to disconnect,
321 * XXX not a back-door flag on the OBD. Ah well.
323 osc_obd->obd_force = obd->obd_force;
324 osc_obd->obd_fail = obd->obd_fail;
325 osc_obd->obd_no_recov = obd->obd_no_recov;
328 obd_register_observer(osc_obd, NULL);
330 rc = obd_disconnect(tgt->ltd_exp);
332 CERROR("Target %s disconnect error %d\n",
333 tgt->ltd_uuid.uuid, rc);
337 qos_del_tgt(obd, tgt);
343 static int lov_disconnect(struct obd_export *exp)
345 struct obd_device *obd = class_exp2obd(exp);
346 struct lov_obd *lov = &obd->u.lov;
353 /* Only disconnect the underlying layers on the final disconnect. */
355 if (lov->lov_connects != 0) {
356 /* why should there be more than 1 connect? */
357 CERROR("disconnect #%d\n", lov->lov_connects);
361 /* Let's hold another reference so lov_del_obd doesn't spin through
365 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
366 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
367 /* Disconnection is the last we know about an obd */
368 lov_del_target(obd, i, 0, lov->lov_tgts[i]->ltd_gen);
374 rc = class_disconnect(exp); /* bz 9811 */
380 * -EINVAL : UUID can't be found in the LOV's target list
381 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
382 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
383 * any >= 0 : is log target index
385 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
386 enum obd_notify_event ev)
388 struct lov_obd *lov = &obd->u.lov;
389 struct lov_tgt_desc *tgt;
390 int index, activate, active;
393 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
394 lov, uuid->uuid, ev);
397 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
398 tgt = lov->lov_tgts[index];
399 if (!tgt || !tgt->ltd_exp)
402 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
403 index, obd_uuid2str(&tgt->ltd_uuid),
404 tgt->ltd_exp->exp_handle.h_cookie);
405 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
409 if (index == lov->desc.ld_tgt_count)
410 GOTO(out, index = -EINVAL);
412 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
413 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
415 if (lov->lov_tgts[index]->ltd_activate == activate) {
416 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
417 uuid->uuid, activate ? "" : "de");
419 lov->lov_tgts[index]->ltd_activate = activate;
420 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
421 activate ? "" : "de", obd_uuid2str(uuid));
424 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
425 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
427 if (lov->lov_tgts[index]->ltd_active == active) {
428 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
429 uuid->uuid, active ? "" : "in");
432 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
433 obd_uuid2str(uuid), active ? "" : "in");
436 lov->lov_tgts[index]->ltd_active = active;
438 lov->desc.ld_active_tgt_count++;
439 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
441 lov->desc.ld_active_tgt_count--;
442 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
444 /* remove any old qos penalty */
445 lov->lov_tgts[index]->ltd_qos.ltq_penalty = 0;
447 CERROR("Unknown event(%d) for uuid %s", ev, uuid->uuid);
455 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
456 enum obd_notify_event ev, void *data)
461 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
462 ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
463 struct obd_uuid *uuid;
467 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
468 CERROR("unexpected notification of %s %s!\n",
469 watched->obd_type->typ_name,
473 uuid = &watched->u.cli.cl_target_uuid;
475 /* Set OSC as active before notifying the observer, so the
476 * observer can use the OSC normally.
478 rc = lov_set_osc_active(obd, uuid, ev);
480 CERROR("event(%d) of %s failed: %d\n", ev,
481 obd_uuid2str(uuid), rc);
484 /* active event should be pass lov target index as data */
488 /* Pass the notification up the chain. */
490 rc = obd_notify_observer(obd, watched, ev, data);
492 /* NULL watched means all osc's in the lov (only for syncs) */
493 /* sync event should be send lov idx as data */
494 struct lov_obd *lov = &obd->u.lov;
498 is_sync = (ev == OBD_NOTIFY_SYNC) ||
499 (ev == OBD_NOTIFY_SYNC_NONBLOCK);
502 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
503 if (!lov->lov_tgts[i])
506 /* don't send sync event if target not
507 * connected/activated */
508 if (is_sync && !lov->lov_tgts[i]->ltd_active)
511 rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
514 CERROR("%s: notify %s of %s failed %d\n",
516 obd->obd_observer->obd_name,
517 lov->lov_tgts[i]->ltd_obd->obd_name,
527 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
528 __u32 index, int gen, int active)
530 struct lov_obd *lov = &obd->u.lov;
531 struct lov_tgt_desc *tgt;
532 struct obd_device *tgt_obd;
536 CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
537 uuidp->uuid, index, gen, active);
540 CERROR("request to add OBD %s with invalid generation: %d\n",
545 tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
550 cfs_mutex_lock(&lov->lov_lock);
552 if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
553 tgt = lov->lov_tgts[index];
554 CERROR("UUID %s already assigned at LOV target index %d\n",
555 obd_uuid2str(&tgt->ltd_uuid), index);
556 cfs_mutex_unlock(&lov->lov_lock);
560 if (index >= lov->lov_tgt_size) {
561 /* We need to reallocate the lov target array. */
562 struct lov_tgt_desc **newtgts, **old = NULL;
563 __u32 newsize, oldsize = 0;
565 newsize = max(lov->lov_tgt_size, (__u32)2);
566 while (newsize < index + 1)
567 newsize = newsize << 1;
568 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
569 if (newtgts == NULL) {
570 cfs_mutex_unlock(&lov->lov_lock);
574 if (lov->lov_tgt_size) {
575 memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
578 oldsize = lov->lov_tgt_size;
581 lov->lov_tgts = newtgts;
582 lov->lov_tgt_size = newsize;
587 OBD_FREE(old, sizeof(*old) * oldsize);
589 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
590 lov->lov_tgts, lov->lov_tgt_size);
595 cfs_mutex_unlock(&lov->lov_lock);
599 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
601 cfs_mutex_unlock(&lov->lov_lock);
606 tgt->ltd_uuid = *uuidp;
607 tgt->ltd_obd = tgt_obd;
608 /* XXX - add a sanity check on the generation number. */
610 tgt->ltd_index = index;
611 tgt->ltd_activate = active;
612 lov->lov_tgts[index] = tgt;
613 if (index >= lov->desc.ld_tgt_count)
614 lov->desc.ld_tgt_count = index + 1;
616 cfs_mutex_unlock(&lov->lov_lock);
618 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
619 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
621 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
623 if (lov->lov_connects == 0) {
624 /* lov_connect hasn't been called yet. We'll do the
625 lov_connect_obd on this target when that fn first runs,
626 because we don't know the connect flags yet. */
632 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
636 /* connect to administrative disabled ost */
640 if (lov->lov_lru != NULL) {
641 rc = obd_set_info_async(NULL, tgt->ltd_exp,
642 sizeof(KEY_LRU_SET), KEY_LRU_SET,
643 sizeof(struct cl_client_lru), lov->lov_lru,
649 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
650 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
655 CERROR("add failed (%d), deleting %s\n", rc,
656 obd_uuid2str(&tgt->ltd_uuid));
657 lov_del_target(obd, index, 0, 0);
663 /* Schedule a target for deletion */
664 int lov_del_target(struct obd_device *obd, __u32 index,
665 struct obd_uuid *uuidp, int gen)
667 struct lov_obd *lov = &obd->u.lov;
668 int count = lov->desc.ld_tgt_count;
672 if (index >= count) {
673 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
680 if (!lov->lov_tgts[index]) {
681 CERROR("LOV target at index %d is not setup.\n", index);
682 GOTO(out, rc = -EINVAL);
685 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
686 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
687 lov_uuid2str(lov, index), index,
688 obd_uuid2str(uuidp));
689 GOTO(out, rc = -EINVAL);
692 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
693 lov_uuid2str(lov, index), index,
694 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
695 lov->lov_tgts[index]->ltd_active);
697 lov->lov_tgts[index]->ltd_reap = 1;
698 lov->lov_death_row++;
699 /* we really delete it from obd_putref */
706 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
708 struct obd_device *osc_obd;
711 LASSERT(tgt->ltd_reap);
713 osc_obd = class_exp2obd(tgt->ltd_exp);
715 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
717 osc_obd ? osc_obd->obd_name : "<no obd>");
720 lov_disconnect_obd(obd, tgt);
724 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
725 do it ourselves. And we can't do it from lov_cleanup,
726 because we just lost our only reference to it. */
728 class_manual_cleanup(osc_obd);
731 void lov_fix_desc_stripe_size(__u64 *val)
733 if (*val < PTLRPC_MAX_BRW_SIZE) {
734 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
735 PTLRPC_MAX_BRW_SIZE);
736 *val = PTLRPC_MAX_BRW_SIZE;
737 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
738 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
739 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
741 *val, LOV_MIN_STRIPE_SIZE);
745 void lov_fix_desc_stripe_count(__u32 *val)
751 void lov_fix_desc_pattern(__u32 *val)
753 /* from lov_setstripe */
754 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
755 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
760 void lov_fix_desc_qos_maxage(__u32 *val)
764 *val = QOS_DEFAULT_MAXAGE;
767 void lov_fix_desc(struct lov_desc *desc)
769 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
770 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
771 lov_fix_desc_pattern(&desc->ld_pattern);
772 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
775 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
777 struct lprocfs_static_vars lvars = { 0 };
778 struct lov_desc *desc;
779 struct lov_obd *lov = &obd->u.lov;
783 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
784 CERROR("LOV setup requires a descriptor\n");
788 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
790 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
791 CERROR("descriptor size wrong: %d > %d\n",
792 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
796 if (desc->ld_magic != LOV_DESC_MAGIC) {
797 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
798 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
799 obd->obd_name, desc);
800 lustre_swab_lov_desc(desc);
802 CERROR("%s: Bad lov desc magic: %#x\n",
803 obd->obd_name, desc->ld_magic);
810 desc->ld_active_tgt_count = 0;
812 lov->lov_tgt_size = 0;
814 cfs_mutex_init(&lov->lov_lock);
815 cfs_atomic_set(&lov->lov_refcount, 0);
816 CFS_INIT_LIST_HEAD(&lov->lov_qos.lq_oss_list);
817 cfs_init_rwsem(&lov->lov_qos.lq_rw_sem);
818 lov->lov_sp_me = LUSTRE_SP_CLI;
819 lov->lov_qos.lq_dirty = 1;
820 lov->lov_qos.lq_rr.lqr_dirty = 1;
821 lov->lov_qos.lq_reset = 1;
822 /* Default priority is toward free space balance */
823 lov->lov_qos.lq_prio_free = 232;
824 /* Default threshold for rr (roughly 17%) */
825 lov->lov_qos.lq_threshold_rr = 43;
826 /* Init statfs fields */
827 OBD_ALLOC_PTR(lov->lov_qos.lq_statfs_data);
828 if (NULL == lov->lov_qos.lq_statfs_data)
830 cfs_waitq_init(&lov->lov_qos.lq_statfs_waitq);
832 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
834 HASH_POOLS_BKT_BITS, 0,
837 &pool_hash_operations,
839 CFS_INIT_LIST_HEAD(&lov->lov_pool_list);
840 lov->lov_pool_count = 0;
841 rc = lov_ost_pool_init(&lov->lov_packed, 0);
843 GOTO(out_free_statfs, rc);
844 rc = lov_ost_pool_init(&lov->lov_qos.lq_rr.lqr_pool, 0);
846 GOTO(out_free_lov_packed, rc);
848 lprocfs_lov_init_vars(&lvars);
849 lprocfs_obd_setup(obd, lvars.obd_vars);
854 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
855 0444, &lov_proc_target_fops, obd);
857 CWARN("Error adding the target_obd file\n");
860 lov->lov_pool_proc_entry = lprocfs_register("pools",
867 lov_ost_pool_free(&lov->lov_packed);
869 OBD_FREE_PTR(lov->lov_qos.lq_statfs_data);
873 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
876 struct lov_obd *lov = &obd->u.lov;
881 case OBD_CLEANUP_EARLY: {
883 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
884 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
886 obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
891 case OBD_CLEANUP_EXPORTS:
892 rc = obd_llog_finish(obd, 0);
894 CERROR("failed to cleanup llogging subsystems\n");
900 static int lov_cleanup(struct obd_device *obd)
902 struct lov_obd *lov = &obd->u.lov;
903 cfs_list_t *pos, *tmp;
904 struct pool_desc *pool;
907 cfs_list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
908 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
909 /* free pool structs */
910 CDEBUG(D_INFO, "delete pool %p\n", pool);
911 /* In the function below, .hs_keycmp resolves to
912 * pool_hashkey_keycmp() */
913 /* coverity[overrun-buffer-val] */
914 lov_pool_del(obd, pool->pool_name);
916 cfs_hash_putref(lov->lov_pools_hash_body);
917 lov_ost_pool_free(&(lov->lov_qos.lq_rr.lqr_pool));
918 lov_ost_pool_free(&lov->lov_packed);
920 lprocfs_obd_cleanup(obd);
924 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
925 if (!lov->lov_tgts[i])
928 /* Inactive targets may never have connected */
929 if (lov->lov_tgts[i]->ltd_active ||
930 cfs_atomic_read(&lov->lov_refcount))
931 /* We should never get here - these
932 should have been removed in the
934 CERROR("lov tgt %d not cleaned!"
935 " deathrow=%d, lovrc=%d\n",
936 i, lov->lov_death_row,
937 cfs_atomic_read(&lov->lov_refcount));
938 lov_del_target(obd, i, 0, 0);
941 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
943 lov->lov_tgt_size = 0;
945 OBD_FREE_PTR(lov->lov_qos.lq_statfs_data);
949 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
950 __u32 *indexp, int *genp)
952 struct obd_uuid obd_uuid;
957 switch(cmd = lcfg->lcfg_command) {
958 case LCFG_LOV_ADD_OBD:
959 case LCFG_LOV_ADD_INA:
960 case LCFG_LOV_DEL_OBD: {
963 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
964 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
965 GOTO(out, rc = -EINVAL);
967 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
969 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", indexp) != 1)
970 GOTO(out, rc = -EINVAL);
971 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
972 GOTO(out, rc = -EINVAL);
975 if (cmd == LCFG_LOV_ADD_OBD)
976 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
977 else if (cmd == LCFG_LOV_ADD_INA)
978 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
980 rc = lov_del_target(obd, index, &obd_uuid, gen);
984 struct lprocfs_static_vars lvars = { 0 };
985 struct lov_desc *desc = &(obd->u.lov.desc);
988 GOTO(out, rc = -EINVAL);
990 lprocfs_lov_init_vars(&lvars);
992 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
1005 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1006 GOTO(out, rc = -EINVAL);
1015 #define log2(n) cfs_ffz(~(n))
1018 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
1019 struct lov_stripe_md **ea,
1020 struct obd_trans_info *oti)
1022 struct lov_obd *lov;
1023 struct obdo *tmp_oa;
1024 struct obd_uuid *ost_uuid = NULL;
1028 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1029 src_oa->o_flags == OBD_FL_DELORPHAN);
1031 lov = &export->exp_obd->u.lov;
1037 if (oti->oti_ost_uuid) {
1038 ost_uuid = oti->oti_ost_uuid;
1039 CDEBUG(D_HA, "clearing orphans only for %s\n",
1043 obd_getref(export->exp_obd);
1044 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1045 struct lov_stripe_md obj_md;
1046 struct lov_stripe_md *obj_mdp = &obj_md;
1047 struct lov_tgt_desc *tgt;
1050 tgt = lov->lov_tgts[i];
1054 /* if called for a specific target, we don't
1055 care if it is not active. */
1056 if (!lov->lov_tgts[i]->ltd_active && ost_uuid == NULL) {
1057 CDEBUG(D_HA, "lov idx %d inactive\n", i);
1061 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->ltd_uuid))
1064 CDEBUG(D_CONFIG,"Clear orphans for %d:%s\n", i,
1065 obd_uuid2str(ost_uuid));
1067 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1069 LASSERT(lov->lov_tgts[i]->ltd_exp);
1070 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1071 err = obd_create(NULL, lov->lov_tgts[i]->ltd_exp,
1072 tmp_oa, &obj_mdp, oti);
1074 /* This export will be disabled until it is recovered,
1075 and then orphan recovery will be completed. */
1076 CERROR("error in orphan recovery on OST idx %d/%d: "
1077 "rc = %d\n", i, lov->desc.ld_tgt_count, err);
1084 obd_putref(export->exp_obd);
1090 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
1091 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1093 struct lov_stripe_md *obj_mdp, *lsm;
1094 struct lov_obd *lov = &exp->exp_obd->u.lov;
1099 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1100 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
1102 OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
1103 if (obj_mdp == NULL)
1106 ost_idx = src_oa->o_nlink;
1109 GOTO(out, rc = -EINVAL);
1110 if (ost_idx >= lov->desc.ld_tgt_count ||
1111 !lov->lov_tgts[ost_idx])
1112 GOTO(out, rc = -EINVAL);
1114 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1115 if (lsm->lsm_oinfo[i]->loi_ost_idx == ost_idx) {
1116 if (lsm->lsm_oinfo[i]->loi_id != src_oa->o_id)
1117 GOTO(out, rc = -EINVAL);
1121 if (i == lsm->lsm_stripe_count)
1122 GOTO(out, rc = -EINVAL);
1124 rc = obd_create(NULL, lov->lov_tgts[ost_idx]->ltd_exp,
1125 src_oa, &obj_mdp, oti);
1127 OBD_FREE(obj_mdp, sizeof(*obj_mdp));
1131 /* the LOV expects oa->o_id to be set to the LOV object id */
1132 static int lov_create(const struct lu_env *env, struct obd_export *exp,
1133 struct obdo *src_oa, struct lov_stripe_md **ea,
1134 struct obd_trans_info *oti)
1136 struct lov_obd *lov;
1137 struct obd_info oinfo;
1138 struct lov_request_set *set = NULL;
1139 struct lov_request *req;
1140 struct l_wait_info lwi = { 0 };
1144 LASSERT(ea != NULL);
1148 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1149 src_oa->o_flags == OBD_FL_DELORPHAN) {
1150 rc = lov_clear_orphans(exp, src_oa, ea, oti);
1154 lov = &exp->exp_obd->u.lov;
1155 if (!lov->desc.ld_active_tgt_count)
1158 obd_getref(exp->exp_obd);
1159 /* Recreate a specific object id at the given OST index */
1160 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1161 (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1162 rc = lov_recreate(exp, src_oa, ea, oti);
1166 /* issue statfs rpcs if the osfs data is older than qos_maxage - 1s,
1167 * later in alloc_qos(), we will wait for those rpcs to complete if
1168 * the osfs age is older than 2 * qos_maxage */
1169 qos_statfs_update(exp->exp_obd,
1170 cfs_time_shift_64(-lov->desc.ld_qos_maxage +
1171 OBD_STATFS_CACHE_SECONDS),
1174 rc = lov_prep_create_set(exp, &oinfo, ea, src_oa, oti, &set);
1178 cfs_list_for_each_entry(req, &set->set_list, rq_link) {
1179 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1180 rc = obd_create_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1181 &req->rq_oi, &req->rq_oi.oi_md, oti);
1184 /* osc_create have timeout equ obd_timeout/2 so waiting don't be
1185 * longer then this */
1186 l_wait_event(set->set_waitq, lov_set_finished(set, 1), &lwi);
1188 /* we not have ptlrpc set for assign set->interpret and should
1189 * be call interpret function himself. calling from cb_create_update
1190 * not permited because lov_fini_create_set can sleep for long time,
1191 * but we must avoid sleeping in ptlrpcd interpret function. */
1192 rc = lov_fini_create_set(set, ea);
1194 obd_putref(exp->exp_obd);
1198 #define ASSERT_LSM_MAGIC(lsmp) \
1200 LASSERT((lsmp) != NULL); \
1201 LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 || \
1202 (lsmp)->lsm_magic == LOV_MAGIC_V3), \
1203 "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic); \
1206 static int lov_destroy(const struct lu_env *env, struct obd_export *exp,
1207 struct obdo *oa, struct lov_stripe_md *lsm,
1208 struct obd_trans_info *oti, struct obd_export *md_exp,
1211 struct lov_request_set *set;
1212 struct obd_info oinfo;
1213 struct lov_request *req;
1215 struct lov_obd *lov;
1216 int rc = 0, err = 0;
1219 ASSERT_LSM_MAGIC(lsm);
1221 if (!exp || !exp->exp_obd)
1224 if (oa->o_valid & OBD_MD_FLCOOKIE) {
1226 LASSERT(oti->oti_logcookies);
1229 lov = &exp->exp_obd->u.lov;
1230 obd_getref(exp->exp_obd);
1231 rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1235 cfs_list_for_each (pos, &set->set_list) {
1236 req = cfs_list_entry(pos, struct lov_request, rq_link);
1238 if (oa->o_valid & OBD_MD_FLCOOKIE)
1239 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1241 err = obd_destroy(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1242 req->rq_oi.oi_oa, NULL, oti, NULL, capa);
1243 err = lov_update_common_set(set, req, err);
1245 CERROR("error: destroying objid "LPX64" subobj "
1246 LPX64" on OST idx %d: rc = %d\n",
1247 oa->o_id, req->rq_oi.oi_oa->o_id,
1255 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1256 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1258 err = lov_fini_destroy_set(set);
1260 obd_putref(exp->exp_obd);
1261 RETURN(rc ? rc : err);
1264 static int lov_getattr(const struct lu_env *env, struct obd_export *exp,
1265 struct obd_info *oinfo)
1267 struct lov_request_set *set;
1268 struct lov_request *req;
1270 struct lov_obd *lov;
1271 int err = 0, rc = 0;
1275 ASSERT_LSM_MAGIC(oinfo->oi_md);
1277 if (!exp || !exp->exp_obd)
1280 lov = &exp->exp_obd->u.lov;
1282 rc = lov_prep_getattr_set(exp, oinfo, &set);
1286 cfs_list_for_each (pos, &set->set_list) {
1287 req = cfs_list_entry(pos, struct lov_request, rq_link);
1289 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1290 "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1291 req->rq_oi.oi_oa->o_id, req->rq_idx);
1293 rc = obd_getattr(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1295 err = lov_update_common_set(set, req, rc);
1297 CERROR("error: getattr objid "LPX64" subobj "
1298 LPX64" on OST idx %d: rc = %d\n",
1299 oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1305 rc = lov_fini_getattr_set(set);
1311 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1314 struct lov_request_set *lovset = (struct lov_request_set *)data;
1318 /* don't do attribute merge if this aysnc op failed */
1320 cfs_atomic_set(&lovset->set_completes, 0);
1321 err = lov_fini_getattr_set(lovset);
1322 RETURN(rc ? rc : err);
1325 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1326 struct ptlrpc_request_set *rqset)
1328 struct lov_request_set *lovset;
1329 struct lov_obd *lov;
1331 struct lov_request *req;
1336 ASSERT_LSM_MAGIC(oinfo->oi_md);
1338 if (!exp || !exp->exp_obd)
1341 lov = &exp->exp_obd->u.lov;
1343 rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1347 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1348 oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1349 oinfo->oi_md->lsm_stripe_size);
1351 cfs_list_for_each (pos, &lovset->set_list) {
1352 req = cfs_list_entry(pos, struct lov_request, rq_link);
1354 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1355 "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1356 req->rq_oi.oi_oa->o_id, req->rq_idx);
1357 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1358 &req->rq_oi, rqset);
1360 CERROR("error: getattr objid "LPX64" subobj "
1361 LPX64" on OST idx %d: rc = %d\n",
1362 oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1368 if (!cfs_list_empty(&rqset->set_requests)) {
1370 LASSERT (rqset->set_interpret == NULL);
1371 rqset->set_interpret = lov_getattr_interpret;
1372 rqset->set_arg = (void *)lovset;
1377 cfs_atomic_set(&lovset->set_completes, 0);
1378 err = lov_fini_getattr_set(lovset);
1379 RETURN(rc ? rc : err);
1382 static int lov_setattr(const struct lu_env *env, struct obd_export *exp,
1383 struct obd_info *oinfo, struct obd_trans_info *oti)
1385 struct lov_request_set *set;
1386 struct lov_obd *lov;
1388 struct lov_request *req;
1389 int err = 0, rc = 0;
1393 ASSERT_LSM_MAGIC(oinfo->oi_md);
1395 if (!exp || !exp->exp_obd)
1398 /* for now, we only expect the following updates here */
1399 LASSERT(!(oinfo->oi_oa->o_valid & ~(OBD_MD_FLID | OBD_MD_FLTYPE |
1400 OBD_MD_FLMODE | OBD_MD_FLATIME |
1401 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1402 OBD_MD_FLFLAGS | OBD_MD_FLSIZE |
1403 OBD_MD_FLGROUP | OBD_MD_FLUID |
1404 OBD_MD_FLGID | OBD_MD_FLFID |
1406 lov = &exp->exp_obd->u.lov;
1407 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1411 cfs_list_for_each (pos, &set->set_list) {
1412 req = cfs_list_entry(pos, struct lov_request, rq_link);
1414 rc = obd_setattr(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1416 err = lov_update_setattr_set(set, req, rc);
1418 CERROR("error: setattr objid "LPX64" subobj "
1419 LPX64" on OST idx %d: rc = %d\n",
1420 set->set_oi->oi_oa->o_id,
1421 req->rq_oi.oi_oa->o_id, req->rq_idx, err);
1426 err = lov_fini_setattr_set(set);
1432 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1435 struct lov_request_set *lovset = (struct lov_request_set *)data;
1440 cfs_atomic_set(&lovset->set_completes, 0);
1441 err = lov_fini_setattr_set(lovset);
1442 RETURN(rc ? rc : err);
1445 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1446 needed. Otherwise, a client is waiting for responses. */
1447 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1448 struct obd_trans_info *oti,
1449 struct ptlrpc_request_set *rqset)
1451 struct lov_request_set *set;
1452 struct lov_request *req;
1454 struct lov_obd *lov;
1459 ASSERT_LSM_MAGIC(oinfo->oi_md);
1460 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1462 LASSERT(oti->oti_logcookies);
1465 if (!exp || !exp->exp_obd)
1468 lov = &exp->exp_obd->u.lov;
1469 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1473 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1474 oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1475 oinfo->oi_md->lsm_stripe_size);
1477 cfs_list_for_each (pos, &set->set_list) {
1478 req = cfs_list_entry(pos, struct lov_request, rq_link);
1480 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1481 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1483 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1484 "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1485 req->rq_oi.oi_oa->o_id, req->rq_idx);
1487 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1488 &req->rq_oi, oti, rqset);
1490 CERROR("error: setattr objid "LPX64" subobj "
1491 LPX64" on OST idx %d: rc = %d\n",
1492 set->set_oi->oi_oa->o_id,
1493 req->rq_oi.oi_oa->o_id,
1499 /* If we are not waiting for responses on async requests, return. */
1500 if (rc || !rqset || cfs_list_empty(&rqset->set_requests)) {
1503 cfs_atomic_set(&set->set_completes, 0);
1504 err = lov_fini_setattr_set(set);
1505 RETURN(rc ? rc : err);
1508 LASSERT(rqset->set_interpret == NULL);
1509 rqset->set_interpret = lov_setattr_interpret;
1510 rqset->set_arg = (void *)set;
1515 static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
1518 struct lov_request_set *lovset = (struct lov_request_set *)data;
1523 cfs_atomic_set(&lovset->set_completes, 0);
1524 err = lov_fini_punch_set(lovset);
1525 RETURN(rc ? rc : err);
1528 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1529 * we can send this 'punch' to just the authoritative node and the nodes
1530 * that the punch will affect. */
1531 static int lov_punch(const struct lu_env *env, struct obd_export *exp,
1532 struct obd_info *oinfo, struct obd_trans_info *oti,
1533 struct ptlrpc_request_set *rqset)
1535 struct lov_request_set *set;
1536 struct lov_obd *lov;
1538 struct lov_request *req;
1543 ASSERT_LSM_MAGIC(oinfo->oi_md);
1545 if (!exp || !exp->exp_obd)
1548 lov = &exp->exp_obd->u.lov;
1549 rc = lov_prep_punch_set(exp, oinfo, oti, &set);
1553 cfs_list_for_each (pos, &set->set_list) {
1554 req = cfs_list_entry(pos, struct lov_request, rq_link);
1556 rc = obd_punch(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1557 &req->rq_oi, NULL, rqset);
1559 CERROR("error: punch objid "LPX64" subobj "LPX64
1560 " on OST idx %d: rc = %d\n",
1561 set->set_oi->oi_oa->o_id,
1562 req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1567 if (rc || cfs_list_empty(&rqset->set_requests)) {
1569 err = lov_fini_punch_set(set);
1570 RETURN(rc ? rc : err);
1573 LASSERT(rqset->set_interpret == NULL);
1574 rqset->set_interpret = lov_punch_interpret;
1575 rqset->set_arg = (void *)set;
1580 static int lov_sync_interpret(struct ptlrpc_request_set *rqset,
1583 struct lov_request_set *lovset = data;
1588 cfs_atomic_set(&lovset->set_completes, 0);
1589 err = lov_fini_sync_set(lovset);
1593 static int lov_sync(const struct lu_env *env, struct obd_export *exp,
1594 struct obd_info *oinfo, obd_off start, obd_off end,
1595 struct ptlrpc_request_set *rqset)
1597 struct lov_request_set *set = NULL;
1598 struct lov_obd *lov;
1600 struct lov_request *req;
1604 ASSERT_LSM_MAGIC(oinfo->oi_md);
1605 LASSERT(rqset != NULL);
1610 lov = &exp->exp_obd->u.lov;
1611 rc = lov_prep_sync_set(exp, oinfo, start, end, &set);
1615 CDEBUG(D_INFO, "fsync objid "LPX64" ["LPX64", "LPX64"]\n",
1616 set->set_oi->oi_oa->o_id, start, end);
1618 cfs_list_for_each (pos, &set->set_list) {
1619 req = cfs_list_entry(pos, struct lov_request, rq_link);
1621 rc = obd_sync(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1622 &req->rq_oi, req->rq_oi.oi_policy.l_extent.start,
1623 req->rq_oi.oi_policy.l_extent.end, rqset);
1625 CERROR("error: fsync objid "LPX64" subobj "LPX64
1626 " on OST idx %d: rc = %d\n",
1627 set->set_oi->oi_oa->o_id,
1628 req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1633 /* If we are not waiting for responses on async requests, return. */
1634 if (rc || cfs_list_empty(&rqset->set_requests)) {
1635 int err = lov_fini_sync_set(set);
1640 LASSERT(rqset->set_interpret == NULL);
1641 rqset->set_interpret = lov_sync_interpret;
1642 rqset->set_arg = (void *)set;
1647 static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
1648 obd_count oa_bufs, struct brw_page *pga)
1650 struct obd_info oinfo = { { { 0 } } };
1653 oinfo.oi_oa = lov_oinfo->oi_oa;
1655 /* The caller just wants to know if there's a chance that this
1656 * I/O can succeed */
1657 for (i = 0; i < oa_bufs; i++) {
1658 int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
1659 int ost = lov_oinfo->oi_md->lsm_oinfo[stripe]->loi_ost_idx;
1662 if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
1663 pga[i].off + pga[i].count - 1,
1667 if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
1668 CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1672 rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
1680 static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
1681 obd_count oa_bufs, struct brw_page *pga,
1682 struct obd_trans_info *oti)
1684 struct lov_request_set *set;
1685 struct lov_request *req;
1687 struct lov_obd *lov = &exp->exp_obd->u.lov;
1691 ASSERT_LSM_MAGIC(oinfo->oi_md);
1693 if (cmd == OBD_BRW_CHECK) {
1694 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1698 rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
1702 cfs_list_for_each (pos, &set->set_list) {
1703 struct obd_export *sub_exp;
1704 struct brw_page *sub_pga;
1705 req = cfs_list_entry(pos, struct lov_request, rq_link);
1707 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1708 sub_pga = set->set_pga + req->rq_pgaidx;
1709 rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1713 lov_update_common_set(set, req, rc);
1716 err = lov_fini_brw_set(set);
1722 static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
1725 struct lov_request_set *lovset = (struct lov_request_set *)data;
1727 rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc, rqset);
1731 static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
1732 struct ldlm_enqueue_info *einfo,
1733 struct ptlrpc_request_set *rqset)
1735 ldlm_mode_t mode = einfo->ei_mode;
1736 struct lov_request_set *set;
1737 struct lov_request *req;
1739 struct lov_obd *lov;
1744 ASSERT_LSM_MAGIC(oinfo->oi_md);
1745 LASSERT(mode == (mode & -mode));
1747 /* we should never be asked to replay a lock this way. */
1748 LASSERT((oinfo->oi_flags & LDLM_FL_REPLAY) == 0);
1750 if (!exp || !exp->exp_obd)
1753 lov = &exp->exp_obd->u.lov;
1754 rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
1758 cfs_list_for_each (pos, &set->set_list) {
1759 req = cfs_list_entry(pos, struct lov_request, rq_link);
1761 rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
1762 &req->rq_oi, einfo, rqset);
1767 if (rqset && !cfs_list_empty(&rqset->set_requests)) {
1769 LASSERT(rqset->set_interpret == NULL);
1770 rqset->set_interpret = lov_enqueue_interpret;
1771 rqset->set_arg = (void *)set;
1775 rc = lov_fini_enqueue_set(set, mode, rc, rqset);
1779 static int lov_change_cbdata(struct obd_export *exp,
1780 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1783 struct lov_obd *lov;
1787 ASSERT_LSM_MAGIC(lsm);
1789 if (!exp || !exp->exp_obd)
1792 lov = &exp->exp_obd->u.lov;
1793 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1794 struct lov_stripe_md submd;
1795 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1797 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1798 CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1802 LASSERT_SEQ_IS_MDT(loi->loi_seq);
1803 submd.lsm_object_id = loi->loi_id;
1804 submd.lsm_object_seq = loi->loi_seq;
1805 submd.lsm_stripe_count = 0;
1806 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1812 /* find any ldlm lock of the inode in lov
1816 static int lov_find_cbdata(struct obd_export *exp,
1817 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1820 struct lov_obd *lov;
1824 ASSERT_LSM_MAGIC(lsm);
1826 if (!exp || !exp->exp_obd)
1829 lov = &exp->exp_obd->u.lov;
1830 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1831 struct lov_stripe_md submd;
1832 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1834 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1835 CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1839 LASSERT_SEQ_IS_MDT(loi->loi_seq);
1840 submd.lsm_object_id = loi->loi_id;
1841 submd.lsm_object_seq = loi->loi_seq;
1842 submd.lsm_stripe_count = 0;
1843 rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1851 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1852 __u32 mode, struct lustre_handle *lockh)
1854 struct lov_request_set *set;
1855 struct obd_info oinfo;
1856 struct lov_request *req;
1858 struct lov_obd *lov;
1859 struct lustre_handle *lov_lockhp;
1860 int err = 0, rc = 0;
1863 ASSERT_LSM_MAGIC(lsm);
1865 if (!exp || !exp->exp_obd)
1868 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
1870 lov = &exp->exp_obd->u.lov;
1871 rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
1875 cfs_list_for_each (pos, &set->set_list) {
1876 req = cfs_list_entry(pos, struct lov_request, rq_link);
1877 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1879 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
1880 req->rq_oi.oi_md, mode, lov_lockhp);
1881 rc = lov_update_common_set(set, req, rc);
1883 CERROR("error: cancel objid "LPX64" subobj "
1884 LPX64" on OST idx %d: rc = %d\n",
1886 req->rq_oi.oi_md->lsm_object_id,
1892 lov_fini_cancel_set(set);
1896 static int lov_cancel_unused(struct obd_export *exp,
1897 struct lov_stripe_md *lsm,
1898 ldlm_cancel_flags_t flags, void *opaque)
1900 struct lov_obd *lov;
1904 if (!exp || !exp->exp_obd)
1907 lov = &exp->exp_obd->u.lov;
1909 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1911 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1914 err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
1922 ASSERT_LSM_MAGIC(lsm);
1924 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
1925 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1926 struct lov_stripe_md submd;
1927 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1930 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1931 CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1935 if (!lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
1936 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1938 submd.lsm_object_id = loi->loi_id;
1939 submd.lsm_object_seq = loi->loi_seq;
1940 submd.lsm_stripe_count = 0;
1941 err = obd_cancel_unused(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1942 &submd, flags, opaque);
1943 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
1944 CERROR("error: cancel unused objid "LPX64" subobj "LPX64
1945 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1946 loi->loi_id, loi->loi_ost_idx, err);
1954 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1956 struct lov_request_set *lovset = (struct lov_request_set *)data;
1961 cfs_atomic_set(&lovset->set_completes, 0);
1963 err = lov_fini_statfs_set(lovset);
1964 RETURN(rc ? rc : err);
1967 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1968 __u64 max_age, struct ptlrpc_request_set *rqset)
1970 struct obd_device *obd = class_exp2obd(exp);
1971 struct lov_request_set *set;
1972 struct lov_request *req;
1974 struct lov_obd *lov;
1978 LASSERT(oinfo != NULL);
1979 LASSERT(oinfo->oi_osfs != NULL);
1982 rc = lov_prep_statfs_set(obd, oinfo, &set);
1986 cfs_list_for_each (pos, &set->set_list) {
1987 req = cfs_list_entry(pos, struct lov_request, rq_link);
1988 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1989 &req->rq_oi, max_age, rqset);
1994 if (rc || cfs_list_empty(&rqset->set_requests)) {
1997 cfs_atomic_set(&set->set_completes, 0);
1998 err = lov_fini_statfs_set(set);
1999 RETURN(rc ? rc : err);
2002 LASSERT(rqset->set_interpret == NULL);
2003 rqset->set_interpret = lov_statfs_interpret;
2004 rqset->set_arg = (void *)set;
2008 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
2009 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
2011 struct ptlrpc_request_set *set = NULL;
2012 struct obd_info oinfo = { { { 0 } } };
2017 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
2018 * statfs requests */
2019 set = ptlrpc_prep_set();
2023 oinfo.oi_osfs = osfs;
2024 oinfo.oi_flags = flags;
2025 rc = lov_statfs_async(exp, &oinfo, max_age, set);
2027 rc = ptlrpc_set_wait(set);
2028 ptlrpc_set_destroy(set);
2033 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2034 void *karg, void *uarg)
2036 struct obd_device *obddev = class_exp2obd(exp);
2037 struct lov_obd *lov = &obddev->u.lov;
2038 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
2039 struct obd_uuid *uuidp;
2043 case IOC_OBD_STATFS: {
2044 struct obd_ioctl_data *data = karg;
2045 struct obd_device *osc_obd;
2046 struct obd_statfs stat_buf = {0};
2050 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2051 if ((index >= count))
2054 if (!lov->lov_tgts[index])
2055 /* Try again with the next index */
2057 if (!lov->lov_tgts[index]->ltd_active)
2060 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2065 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
2066 min((int) data->ioc_plen2,
2067 (int) sizeof(struct obd_uuid))))
2070 flags = uarg ? *(__u32*)uarg : 0;
2071 /* got statfs data */
2072 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
2073 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2077 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
2078 min((int) data->ioc_plen1,
2079 (int) sizeof(stat_buf))))
2083 case OBD_IOC_LOV_GET_CONFIG: {
2084 struct obd_ioctl_data *data;
2085 struct lov_desc *desc;
2090 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2093 data = (struct obd_ioctl_data *)buf;
2095 if (sizeof(*desc) > data->ioc_inllen1) {
2096 obd_ioctl_freedata(buf, len);
2100 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2101 obd_ioctl_freedata(buf, len);
2105 if (sizeof(__u32) * count > data->ioc_inllen3) {
2106 obd_ioctl_freedata(buf, len);
2110 desc = (struct lov_desc *)data->ioc_inlbuf1;
2111 memcpy(desc, &(lov->desc), sizeof(*desc));
2113 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2114 genp = (__u32 *)data->ioc_inlbuf3;
2115 /* the uuid will be empty for deleted OSTs */
2116 for (i = 0; i < count; i++, uuidp++, genp++) {
2117 if (!lov->lov_tgts[i])
2119 *uuidp = lov->lov_tgts[i]->ltd_uuid;
2120 *genp = lov->lov_tgts[i]->ltd_gen;
2123 if (cfs_copy_to_user((void *)uarg, buf, len))
2125 obd_ioctl_freedata(buf, len);
2128 case LL_IOC_LOV_SETSTRIPE:
2129 rc = lov_setstripe(exp, len, karg, uarg);
2131 case LL_IOC_LOV_GETSTRIPE:
2132 rc = lov_getstripe(exp, karg, uarg);
2134 case LL_IOC_LOV_SETEA:
2135 rc = lov_setea(exp, karg, uarg);
2137 case OBD_IOC_QUOTACTL: {
2138 struct if_quotactl *qctl = karg;
2139 struct lov_tgt_desc *tgt = NULL;
2140 struct obd_quotactl *oqctl;
2142 if (qctl->qc_valid == QC_OSTIDX) {
2143 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
2146 tgt = lov->lov_tgts[qctl->qc_idx];
2147 if (!tgt || !tgt->ltd_exp)
2149 } else if (qctl->qc_valid == QC_UUID) {
2150 for (i = 0; i < count; i++) {
2151 tgt = lov->lov_tgts[i];
2153 !obd_uuid_equals(&tgt->ltd_uuid,
2157 if (tgt->ltd_exp == NULL)
2169 LASSERT(tgt && tgt->ltd_exp);
2170 OBD_ALLOC_PTR(oqctl);
2174 QCTL_COPY(oqctl, qctl);
2175 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2177 QCTL_COPY(qctl, oqctl);
2178 qctl->qc_valid = QC_OSTIDX;
2179 qctl->obd_uuid = tgt->ltd_uuid;
2181 OBD_FREE_PTR(oqctl);
2190 for (i = 0; i < count; i++) {
2192 struct obd_device *osc_obd;
2194 /* OST was disconnected */
2195 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2198 /* ll_umount_begin() sets force flag but for lov, not
2199 * osc. Let's pass it through */
2200 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
2201 osc_obd->obd_force = obddev->obd_force;
2202 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
2204 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
2207 if (lov->lov_tgts[i]->ltd_active) {
2208 CDEBUG(err == -ENOTTY ?
2209 D_IOCTL : D_WARNING,
2210 "iocontrol OSC %s on OST "
2211 "idx %d cmd %x: err = %d\n",
2212 lov_uuid2str(lov, i),
2229 #define FIEMAP_BUFFER_SIZE 4096
2232 * Non-zero fe_logical indicates that this is a continuation FIEMAP
2233 * call. The local end offset and the device are sent in the first
2234 * fm_extent. This function calculates the stripe number from the index.
2235 * This function returns a stripe_no on which mapping is to be restarted.
2237 * This function returns fm_end_offset which is the in-OST offset at which
2238 * mapping should be restarted. If fm_end_offset=0 is returned then caller
2239 * will re-calculate proper offset in next stripe.
2240 * Note that the first extent is passed to lov_get_info via the value field.
2242 * \param fiemap fiemap request header
2243 * \param lsm striping information for the file
2244 * \param fm_start logical start of mapping
2245 * \param fm_end logical end of mapping
2246 * \param start_stripe starting stripe will be returned in this
2248 obd_size fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
2249 struct lov_stripe_md *lsm, obd_size fm_start,
2250 obd_size fm_end, int *start_stripe)
2252 obd_size local_end = fiemap->fm_extents[0].fe_logical;
2253 obd_off lun_start, lun_end;
2254 obd_size fm_end_offset;
2255 int stripe_no = -1, i;
2257 if (fiemap->fm_extent_count == 0 ||
2258 fiemap->fm_extents[0].fe_logical == 0)
2261 /* Find out stripe_no from ost_index saved in the fe_device */
2262 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2263 if (lsm->lsm_oinfo[i]->loi_ost_idx ==
2264 fiemap->fm_extents[0].fe_device) {
2269 if (stripe_no == -1)
2272 /* If we have finished mapping on previous device, shift logical
2273 * offset to start of next device */
2274 if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
2275 &lun_start, &lun_end)) != 0 &&
2276 local_end < lun_end) {
2277 fm_end_offset = local_end;
2278 *start_stripe = stripe_no;
2280 /* This is a special value to indicate that caller should
2281 * calculate offset in next stripe. */
2283 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
2286 return fm_end_offset;
2290 * We calculate on which OST the mapping will end. If the length of mapping
2291 * is greater than (stripe_size * stripe_count) then the last_stripe will
2292 * will be one just before start_stripe. Else we check if the mapping
2293 * intersects each OST and find last_stripe.
2294 * This function returns the last_stripe and also sets the stripe_count
2295 * over which the mapping is spread
2297 * \param lsm striping information for the file
2298 * \param fm_start logical start of mapping
2299 * \param fm_end logical end of mapping
2300 * \param start_stripe starting stripe of the mapping
2301 * \param stripe_count the number of stripes across which to map is returned
2303 * \retval last_stripe return the last stripe of the mapping
2305 int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, obd_size fm_start,
2306 obd_size fm_end, int start_stripe,
2310 obd_off obd_start, obd_end;
2313 if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
2314 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
2316 *stripe_count = lsm->lsm_stripe_count;
2318 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
2319 i = (i + 1) % lsm->lsm_stripe_count, j++) {
2320 if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
2321 &obd_start, &obd_end)) == 0)
2325 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
2332 * Set fe_device and copy extents from local buffer into main return buffer.
2334 * \param fiemap fiemap request header
2335 * \param lcl_fm_ext array of local fiemap extents to be copied
2336 * \param ost_index OST index to be written into the fm_device field for each
2338 * \param ext_count number of extents to be copied
2339 * \param current_extent where to start copying in main extent array
2341 void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
2342 struct ll_fiemap_extent *lcl_fm_ext,
2343 int ost_index, unsigned int ext_count,
2349 for (ext = 0; ext < ext_count; ext++) {
2350 lcl_fm_ext[ext].fe_device = ost_index;
2351 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
2354 /* Copy fm_extent's from fm_local to return buffer */
2355 to = (char *)fiemap + fiemap_count_to_size(current_extent);
2356 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
2360 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
2361 * This also handles the restarting of FIEMAP calls in case mapping overflows
2362 * the available number of extents in single call.
2364 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
2365 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2367 struct ll_fiemap_info_key *fm_key = key;
2368 struct ll_user_fiemap *fiemap = val;
2369 struct ll_user_fiemap *fm_local = NULL;
2370 struct ll_fiemap_extent *lcl_fm_ext;
2372 unsigned int get_num_extents = 0;
2373 int ost_index = 0, actual_start_stripe, start_stripe;
2374 obd_size fm_start, fm_end, fm_length, fm_end_offset;
2376 int current_extent = 0, rc = 0, i;
2377 int ost_eof = 0; /* EOF for object */
2378 int ost_done = 0; /* done with required mapping for this OST? */
2380 int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
2381 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
2386 if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
2387 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
2389 OBD_ALLOC_LARGE(fm_local, buffer_size);
2390 if (fm_local == NULL)
2391 GOTO(out, rc = -ENOMEM);
2392 lcl_fm_ext = &fm_local->fm_extents[0];
2394 count_local = fiemap_size_to_count(buffer_size);
2396 memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
2397 fm_start = fiemap->fm_start;
2398 fm_length = fiemap->fm_length;
2399 /* Calculate start stripe, last stripe and length of mapping */
2400 actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
2401 fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
2402 fm_start + fm_length - 1);
2403 /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
2404 if (fm_end > fm_key->oa.o_size)
2405 fm_end = fm_key->oa.o_size;
2407 last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
2408 actual_start_stripe, &stripe_count);
2410 fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start,
2411 fm_end, &start_stripe);
2412 if (fm_end_offset == -EINVAL)
2415 if (fiemap->fm_extent_count == 0) {
2416 get_num_extents = 1;
2420 /* Check each stripe */
2421 for (cur_stripe = start_stripe, i = 0; i < stripe_count;
2422 i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
2423 obd_size req_fm_len; /* Stores length of required mapping */
2424 obd_size len_mapped_single_call;
2425 obd_off lun_start, lun_end, obd_object_end;
2426 unsigned int ext_count;
2428 cur_stripe_wrap = cur_stripe;
2430 /* Find out range of mapping on this stripe */
2431 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
2432 &lun_start, &obd_object_end)) == 0)
2435 /* If this is a continuation FIEMAP call and we are on
2436 * starting stripe then lun_start needs to be set to
2438 if (fm_end_offset != 0 && cur_stripe == start_stripe)
2439 lun_start = fm_end_offset;
2441 if (fm_length != ~0ULL) {
2442 /* Handle fm_start + fm_length overflow */
2443 if (fm_start + fm_length < fm_start)
2444 fm_length = ~0ULL - fm_start;
2445 lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
2451 if (lun_start == lun_end)
2454 req_fm_len = obd_object_end - lun_start;
2455 fm_local->fm_length = 0;
2456 len_mapped_single_call = 0;
2458 /* If the output buffer is very large and the objects have many
2459 * extents we may need to loop on a single OST repeatedly */
2463 if (get_num_extents == 0) {
2464 /* Don't get too many extents. */
2465 if (current_extent + count_local >
2466 fiemap->fm_extent_count)
2467 count_local = fiemap->fm_extent_count -
2471 lun_start += len_mapped_single_call;
2472 fm_local->fm_length = req_fm_len - len_mapped_single_call;
2473 req_fm_len = fm_local->fm_length;
2474 fm_local->fm_extent_count = count_local;
2475 fm_local->fm_mapped_extents = 0;
2476 fm_local->fm_flags = fiemap->fm_flags;
2478 fm_key->oa.o_id = lsm->lsm_oinfo[cur_stripe]->loi_id;
2479 fm_key->oa.o_seq = lsm->lsm_oinfo[cur_stripe]->loi_seq;
2480 ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
2482 if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
2483 GOTO(out, rc = -EINVAL);
2485 /* If OST is inactive, return extent with UNKNOWN flag */
2486 if (!lov->lov_tgts[ost_index]->ltd_active) {
2487 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
2488 fm_local->fm_mapped_extents = 1;
2490 lcl_fm_ext[0].fe_logical = lun_start;
2491 lcl_fm_ext[0].fe_length = obd_object_end -
2493 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
2498 fm_local->fm_start = lun_start;
2499 fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
2500 memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
2501 *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
2502 rc = obd_get_info(NULL,
2503 lov->lov_tgts[ost_index]->ltd_exp,
2504 keylen, key, vallen, fm_local, lsm);
2509 ext_count = fm_local->fm_mapped_extents;
2510 if (ext_count == 0) {
2512 /* If last stripe has hole at the end,
2513 * then we need to return */
2514 if (cur_stripe_wrap == last_stripe) {
2515 fiemap->fm_mapped_extents = 0;
2521 /* If we just need num of extents then go to next device */
2522 if (get_num_extents) {
2523 current_extent += ext_count;
2527 len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
2528 lun_start + lcl_fm_ext[ext_count - 1].fe_length;
2530 /* Have we finished mapping on this device? */
2531 if (req_fm_len <= len_mapped_single_call)
2534 /* Clear the EXTENT_LAST flag which can be present on
2536 if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
2537 lcl_fm_ext[ext_count - 1].fe_flags &=
2538 ~FIEMAP_EXTENT_LAST;
2540 curr_loc = lov_stripe_size(lsm,
2541 lcl_fm_ext[ext_count - 1].fe_logical+
2542 lcl_fm_ext[ext_count - 1].fe_length,
2544 if (curr_loc >= fm_key->oa.o_size)
2547 fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
2548 ost_index, ext_count,
2551 current_extent += ext_count;
2553 /* Ran out of available extents? */
2554 if (current_extent >= fiemap->fm_extent_count)
2556 } while (ost_done == 0 && ost_eof == 0);
2558 if (cur_stripe_wrap == last_stripe)
2563 /* Indicate that we are returning device offsets unless file just has
2565 if (lsm->lsm_stripe_count > 1)
2566 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2568 if (get_num_extents)
2569 goto skip_last_device_calc;
2571 /* Check if we have reached the last stripe and whether mapping for that
2572 * stripe is done. */
2573 if (cur_stripe_wrap == last_stripe) {
2574 if (ost_done || ost_eof)
2575 fiemap->fm_extents[current_extent - 1].fe_flags |=
2579 skip_last_device_calc:
2580 fiemap->fm_mapped_extents = current_extent;
2583 OBD_FREE_LARGE(fm_local, buffer_size);
2587 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
2588 __u32 keylen, void *key, __u32 *vallen, void *val,
2589 struct lov_stripe_md *lsm)
2591 struct obd_device *obddev = class_exp2obd(exp);
2592 struct lov_obd *lov = &obddev->u.lov;
2596 if (!vallen || !val)
2601 if (KEY_IS(KEY_LOCK_TO_STRIPE)) {
2604 struct ldlm_lock *lock;
2606 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2607 struct lov_oinfo *loi;
2608 __u32 *stripe = val;
2610 if (*vallen < sizeof(*stripe))
2611 GOTO(out, rc = -EFAULT);
2612 *vallen = sizeof(*stripe);
2614 /* XXX This is another one of those bits that will need to
2615 * change if we ever actually support nested LOVs. It uses
2616 * the lock's export to find out which stripe it is. */
2617 /* XXX - it's assumed all the locks for deleted OSTs have
2618 * been cancelled. Also, the export for deleted OSTs will
2619 * be NULL and won't match the lock's export. */
2620 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2621 loi = lsm->lsm_oinfo[i];
2622 if (!lov->lov_tgts[loi->loi_ost_idx])
2624 if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2625 data->lock->l_conn_export &&
2626 osc_res_name_eq(loi->loi_id, loi->loi_seq, res_id)) {
2631 LDLM_ERROR(data->lock, "lock on inode without such object");
2632 dump_lsm(D_ERROR, lsm);
2633 GOTO(out, rc = -ENXIO);
2634 } else if (KEY_IS(KEY_LAST_ID)) {
2635 struct obd_id_info *info = val;
2636 __u32 size = sizeof(obd_id);
2637 struct lov_tgt_desc *tgt;
2639 LASSERT(*vallen == sizeof(struct obd_id_info));
2640 tgt = lov->lov_tgts[info->idx];
2642 if (!tgt || !tgt->ltd_active)
2643 GOTO(out, rc = -ESRCH);
2645 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2646 &size, info->data, NULL);
2648 } else if (KEY_IS(KEY_LOVDESC)) {
2649 struct lov_desc *desc_ret = val;
2650 *desc_ret = lov->desc;
2653 } else if (KEY_IS(KEY_FIEMAP)) {
2654 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
2656 } else if (KEY_IS(KEY_CONNECT_FLAG)) {
2657 struct lov_tgt_desc *tgt;
2658 __u64 ost_idx = *((__u64*)val);
2660 LASSERT(*vallen == sizeof(__u64));
2661 LASSERT(ost_idx < lov->desc.ld_tgt_count);
2662 tgt = lov->lov_tgts[ost_idx];
2664 if (!tgt || !tgt->ltd_exp)
2665 GOTO(out, rc = -ESRCH);
2667 *((__u64*)val) = tgt->ltd_exp->exp_connect_flags;
2669 } else if (KEY_IS(KEY_TGT_COUNT)) {
2670 *((int *)val) = lov->desc.ld_tgt_count;
2681 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
2682 obd_count keylen, void *key, obd_count vallen,
2683 void *val, struct ptlrpc_request_set *set)
2685 struct obd_device *obddev = class_exp2obd(exp);
2686 struct lov_obd *lov = &obddev->u.lov;
2689 struct lov_tgt_desc *tgt;
2690 unsigned incr, check_uuid,
2691 do_inactive, no_set;
2692 unsigned next_id = 0, mds_con = 0, capa = 0;
2695 incr = check_uuid = do_inactive = no_set = 0;
2698 set = ptlrpc_prep_set();
2704 count = lov->desc.ld_tgt_count;
2706 if (KEY_IS(KEY_NEXT_ID)) {
2707 count = vallen / sizeof(struct obd_id_info);
2708 vallen = sizeof(obd_id);
2709 incr = sizeof(struct obd_id_info);
2712 } else if (KEY_IS(KEY_CHECKSUM)) {
2714 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2715 /* use defaults: do_inactive = incr = 0; */
2716 } else if (KEY_IS(KEY_MDS_CONN)) {
2718 } else if (KEY_IS(KEY_CAPA_KEY)) {
2720 } else if (KEY_IS(KEY_LRU_SET)) {
2721 LASSERT(lov->lov_lru == NULL);
2726 for (i = 0; i < count; i++, val = (char *)val + incr) {
2728 tgt = lov->lov_tgts[((struct obd_id_info*)val)->idx];
2730 tgt = lov->lov_tgts[i];
2732 /* OST was disconnected */
2733 if (!tgt || !tgt->ltd_exp)
2736 /* OST is inactive and we don't want inactive OSCs */
2737 if (!tgt->ltd_active && !do_inactive)
2741 struct mds_group_info *mgi;
2743 LASSERT(vallen == sizeof(*mgi));
2744 mgi = (struct mds_group_info *)val;
2746 /* Only want a specific OSC */
2747 if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2751 err = obd_set_info_async(env, tgt->ltd_exp,
2752 keylen, key, sizeof(int),
2754 } else if (next_id) {
2755 err = obd_set_info_async(env, tgt->ltd_exp,
2756 keylen, key, vallen,
2757 ((struct obd_id_info*)val)->data, set);
2759 struct mds_capa_info *info = (struct mds_capa_info*)val;
2761 LASSERT(vallen == sizeof(*info));
2763 /* Only want a specific OSC */
2765 !obd_uuid_equals(info->uuid, &tgt->ltd_uuid))
2768 err = obd_set_info_async(env, tgt->ltd_exp, keylen,
2769 key, sizeof(*info->capa),
2772 /* Only want a specific OSC */
2774 !obd_uuid_equals(val, &tgt->ltd_uuid))
2777 err = obd_set_info_async(env, tgt->ltd_exp,
2778 keylen, key, vallen, val, set);
2787 err = ptlrpc_set_wait(set);
2790 ptlrpc_set_destroy(set);
2795 static int lov_extent_calc(struct obd_export *exp, struct lov_stripe_md *lsm,
2796 int cmd, __u64 *offset)
2798 __u32 ssize = lsm->lsm_stripe_size;
2802 lov_do_div64(start, ssize);
2803 start = start * ssize;
2805 CDEBUG(D_DLMTRACE, "offset "LPU64", stripe %u, start "LPU64
2806 ", end "LPU64"\n", *offset, ssize, start,
2808 if (cmd == OBD_CALC_STRIPE_END) {
2809 *offset = start + ssize - 1;
2810 } else if (cmd == OBD_CALC_STRIPE_START) {
2819 void lov_stripe_lock(struct lov_stripe_md *md)
2821 LASSERT(md->lsm_lock_owner != cfs_curproc_pid());
2822 cfs_spin_lock(&md->lsm_lock);
2823 LASSERT(md->lsm_lock_owner == 0);
2824 md->lsm_lock_owner = cfs_curproc_pid();
2826 EXPORT_SYMBOL(lov_stripe_lock);
2828 void lov_stripe_unlock(struct lov_stripe_md *md)
2830 LASSERT(md->lsm_lock_owner == cfs_curproc_pid());
2831 md->lsm_lock_owner = 0;
2832 cfs_spin_unlock(&md->lsm_lock);
2834 EXPORT_SYMBOL(lov_stripe_unlock);
2836 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
2837 struct obd_quotactl *oqctl)
2839 struct lov_obd *lov = &obd->u.lov;
2840 struct lov_tgt_desc *tgt;
2842 __u64 bhardlimit = 0;
2846 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
2847 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
2848 oqctl->qc_cmd != Q_GETOQUOTA &&
2849 oqctl->qc_cmd != Q_INITQUOTA &&
2850 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
2851 oqctl->qc_cmd != Q_FINVALIDATE) {
2852 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
2858 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2861 tgt = lov->lov_tgts[i];
2866 if (!tgt->ltd_active || tgt->ltd_reap) {
2867 if (oqctl->qc_cmd == Q_GETOQUOTA &&
2868 lov->lov_tgts[i]->ltd_activate) {
2870 CERROR("ost %d is inactive\n", i);
2872 CDEBUG(D_HA, "ost %d is inactive\n", i);
2877 err = obd_quotactl(tgt->ltd_exp, oqctl);
2879 if (tgt->ltd_active && !rc)
2884 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2885 curspace += oqctl->qc_dqblk.dqb_curspace;
2886 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
2891 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2892 oqctl->qc_dqblk.dqb_curspace = curspace;
2893 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
2898 static int lov_quotacheck(struct obd_device *obd, struct obd_export *exp,
2899 struct obd_quotactl *oqctl)
2901 struct lov_obd *lov = &obd->u.lov;
2907 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2908 if (!lov->lov_tgts[i])
2911 /* Skip quota check on the administratively disabled OSTs. */
2912 if (!lov->lov_tgts[i]->ltd_activate) {
2913 CWARN("lov idx %d was administratively disabled, "
2914 "skip quotacheck on it.\n", i);
2918 if (!lov->lov_tgts[i]->ltd_active) {
2919 CERROR("lov idx %d inactive\n", i);
2925 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2928 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_activate)
2931 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
2942 struct obd_ops lov_obd_ops = {
2943 .o_owner = THIS_MODULE,
2944 .o_setup = lov_setup,
2945 .o_precleanup = lov_precleanup,
2946 .o_cleanup = lov_cleanup,
2947 //.o_process_config = lov_process_config,
2948 .o_connect = lov_connect,
2949 .o_disconnect = lov_disconnect,
2950 .o_statfs = lov_statfs,
2951 .o_statfs_async = lov_statfs_async,
2952 .o_packmd = lov_packmd,
2953 .o_unpackmd = lov_unpackmd,
2954 .o_create = lov_create,
2955 .o_destroy = lov_destroy,
2956 .o_getattr = lov_getattr,
2957 .o_getattr_async = lov_getattr_async,
2958 .o_setattr = lov_setattr,
2959 .o_setattr_async = lov_setattr_async,
2961 .o_merge_lvb = lov_merge_lvb,
2962 .o_adjust_kms = lov_adjust_kms,
2963 .o_punch = lov_punch,
2965 .o_enqueue = lov_enqueue,
2966 .o_change_cbdata = lov_change_cbdata,
2967 .o_find_cbdata = lov_find_cbdata,
2968 .o_cancel = lov_cancel,
2969 .o_cancel_unused = lov_cancel_unused,
2970 .o_iocontrol = lov_iocontrol,
2971 .o_get_info = lov_get_info,
2972 .o_set_info_async = lov_set_info_async,
2973 .o_extent_calc = lov_extent_calc,
2974 .o_llog_init = lov_llog_init,
2975 .o_llog_finish = lov_llog_finish,
2976 .o_notify = lov_notify,
2977 .o_pool_new = lov_pool_new,
2978 .o_pool_rem = lov_pool_remove,
2979 .o_pool_add = lov_pool_add,
2980 .o_pool_del = lov_pool_del,
2981 .o_getref = lov_getref,
2982 .o_putref = lov_putref,
2983 .o_quotactl = lov_quotactl,
2984 .o_quotacheck = lov_quotacheck,
2987 cfs_mem_cache_t *lov_oinfo_slab;
2989 extern struct lu_kmem_descr lov_caches[];
2991 int __init lov_init(void)
2993 struct lprocfs_static_vars lvars = { 0 };
2997 /* print an address of _any_ initialized kernel symbol from this
2998 * module, to allow debugging with gdb that doesn't support data
2999 * symbols from modules.*/
3000 CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
3002 rc = lu_kmem_init(lov_caches);
3006 lov_oinfo_slab = cfs_mem_cache_create("lov_oinfo",
3007 sizeof(struct lov_oinfo),
3008 0, CFS_SLAB_HWCACHE_ALIGN);
3009 if (lov_oinfo_slab == NULL) {
3010 lu_kmem_fini(lov_caches);
3013 lprocfs_lov_init_vars(&lvars);
3015 rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
3016 LUSTRE_LOV_NAME, &lov_device_type);
3019 rc2 = cfs_mem_cache_destroy(lov_oinfo_slab);
3021 lu_kmem_fini(lov_caches);
3028 static void /*__exit*/ lov_exit(void)
3032 class_unregister_type(LUSTRE_LOV_NAME);
3033 rc = cfs_mem_cache_destroy(lov_oinfo_slab);
3036 lu_kmem_fini(lov_caches);
3039 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3040 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
3041 MODULE_LICENSE("GPL");
3043 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);