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, 2015, Intel Corporation.
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
45 #include <libcfs/libcfs.h>
47 #include <obd_support.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_lib.h>
50 #include <lustre_net.h>
51 #include <lustre/lustre_idl.h>
52 #include <lustre_dlm.h>
53 #include <lustre_mds.h>
54 #include <obd_class.h>
55 #include <lprocfs_status.h>
56 #include <lustre_param.h>
57 #include <cl_object.h>
58 #include <lustre_fid.h>
60 #include "lov_internal.h"
62 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
63 Any function that expects lov_tgts to remain stationary must take a ref. */
64 static void lov_getref(struct obd_device *obd)
66 struct lov_obd *lov = &obd->u.lov;
68 /* nobody gets through here until lov_putref is done */
69 mutex_lock(&lov->lov_lock);
70 atomic_inc(&lov->lov_refcount);
71 mutex_unlock(&lov->lov_lock);
75 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
77 static void lov_putref(struct obd_device *obd)
79 struct lov_obd *lov = &obd->u.lov;
81 mutex_lock(&lov->lov_lock);
82 /* ok to dec to 0 more than once -- ltd_exp's will be null */
83 if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
84 struct list_head kill = LIST_HEAD_INIT(kill);
85 struct lov_tgt_desc *tgt, *n;
88 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
90 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
91 tgt = lov->lov_tgts[i];
93 if (!tgt || !tgt->ltd_reap)
95 list_add(&tgt->ltd_kill, &kill);
96 /* XXX - right now there is a dependency on ld_tgt_count
97 * being the maximum tgt index for computing the
98 * mds_max_easize. So we can't shrink it. */
99 lov_ost_pool_remove(&lov->lov_packed, i);
100 lov->lov_tgts[i] = NULL;
101 lov->lov_death_row--;
103 mutex_unlock(&lov->lov_lock);
105 list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
106 list_del(&tgt->ltd_kill);
108 __lov_del_obd(obd, tgt);
111 mutex_unlock(&lov->lov_lock);
115 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
116 enum obd_notify_event ev);
117 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
118 enum obd_notify_event ev, void *data);
120 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
121 struct obd_connect_data *data)
123 struct lov_obd *lov = &obd->u.lov;
124 struct obd_uuid *tgt_uuid;
125 struct obd_device *tgt_obd;
126 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
127 struct obd_import *imp;
131 if (lov->lov_tgts[index] == NULL)
134 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
135 tgt_obd = lov->lov_tgts[index]->ltd_obd;
137 if (!tgt_obd->obd_set_up) {
138 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
142 /* override the sp_me from lov */
143 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
145 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
146 data->ocd_index = index;
149 * Divine LOV knows that OBDs under it are OSCs.
151 imp = tgt_obd->u.cli.cl_import;
154 tgt_obd->obd_no_recov = 0;
155 /* FIXME this is probably supposed to be
156 ptlrpc_set_import_active. Horrible naming. */
157 ptlrpc_activate_import(imp);
160 rc = obd_register_observer(tgt_obd, obd);
162 CERROR("Target %s register_observer error %d\n",
163 obd_uuid2str(tgt_uuid), rc);
168 if (imp->imp_invalid) {
169 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
170 "disabled\n", obd_uuid2str(tgt_uuid));
174 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
175 &lov_osc_uuid, data, NULL);
176 if (rc || !lov->lov_tgts[index]->ltd_exp) {
177 CERROR("Target %s connect error %d\n",
178 obd_uuid2str(tgt_uuid), rc);
182 lov->lov_tgts[index]->ltd_reap = 0;
184 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
185 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
187 if (lov->targets_proc_entry != NULL) {
188 struct proc_dir_entry *osc_symlink;
189 struct obd_device *osc_obd;
191 osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
193 LASSERT(osc_obd != NULL);
194 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
195 LASSERT(osc_obd->obd_type->typ_name != NULL);
197 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
198 lov->targets_proc_entry,
200 osc_obd->obd_type->typ_name,
202 if (osc_symlink == NULL) {
203 CERROR("cannot register LOV target "
204 "/proc/fs/lustre/%s/%s/target_obds/%s\n",
205 obd->obd_type->typ_name, obd->obd_name,
212 static int lov_connect(const struct lu_env *env,
213 struct obd_export **exp, struct obd_device *obd,
214 struct obd_uuid *cluuid, struct obd_connect_data *data,
217 struct lov_obd *lov = &obd->u.lov;
218 struct lov_tgt_desc *tgt;
219 struct lustre_handle conn;
223 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
225 rc = class_connect(&conn, obd, cluuid);
229 *exp = class_conn2export(&conn);
231 /* Why should there ever be more than 1 connect? */
233 LASSERT(lov->lov_connects == 1);
235 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
237 lov->lov_ocd = *data;
239 lov->targets_proc_entry = lprocfs_register("target_obds",
242 if (IS_ERR(lov->targets_proc_entry)) {
243 CERROR("%s: cannot register "
244 "/proc/fs/lustre/%s/%s/target_obds\n",
245 obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
246 lov->targets_proc_entry = NULL;
250 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
251 tgt = lov->lov_tgts[i];
252 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
254 /* Flags will be lowest common denominator */
255 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
257 CERROR("%s: lov connect tgt %d failed: %d\n",
258 obd->obd_name, i, rc);
261 /* connect to administrative disabled ost */
262 if (!lov->lov_tgts[i]->ltd_exp)
265 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
266 OBD_NOTIFY_CONNECT, (void *)&i);
268 CERROR("%s error sending notify %d\n",
277 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
279 struct lov_obd *lov = &obd->u.lov;
280 struct obd_device *osc_obd;
284 osc_obd = class_exp2obd(tgt->ltd_exp);
285 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
286 obd->obd_name, osc_obd->obd_name);
288 if (tgt->ltd_active) {
290 lov->desc.ld_active_tgt_count--;
291 tgt->ltd_exp->exp_obd->obd_inactive = 1;
295 /* Pass it on to our clients.
296 * XXX This should be an argument to disconnect,
297 * XXX not a back-door flag on the OBD. Ah well.
299 osc_obd->obd_force = obd->obd_force;
300 osc_obd->obd_fail = obd->obd_fail;
301 osc_obd->obd_no_recov = obd->obd_no_recov;
303 if (lov->targets_proc_entry != NULL)
304 lprocfs_remove_proc_entry(osc_obd->obd_name,
305 lov->targets_proc_entry);
308 obd_register_observer(osc_obd, NULL);
310 rc = obd_disconnect(tgt->ltd_exp);
312 CERROR("Target %s disconnect error %d\n",
313 tgt->ltd_uuid.uuid, rc);
321 static int lov_disconnect(struct obd_export *exp)
323 struct obd_device *obd = class_exp2obd(exp);
324 struct lov_obd *lov = &obd->u.lov;
331 /* Only disconnect the underlying layers on the final disconnect. */
333 if (lov->lov_connects != 0) {
334 /* why should there be more than 1 connect? */
335 CERROR("disconnect #%d\n", lov->lov_connects);
339 /* Let's hold another reference so lov_del_obd doesn't spin through
343 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
344 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
345 /* Disconnection is the last we know about an obd */
346 lov_del_target(obd, i, NULL, lov->lov_tgts[i]->ltd_gen);
351 if (lov->targets_proc_entry != NULL)
352 lprocfs_remove(&lov->targets_proc_entry);
355 rc = class_disconnect(exp); /* bz 9811 */
361 * -EINVAL : UUID can't be found in the LOV's target list
362 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
363 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
364 * any >= 0 : is log target index
366 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
367 enum obd_notify_event ev)
369 struct lov_obd *lov = &obd->u.lov;
370 struct lov_tgt_desc *tgt;
371 int index, activate, active;
374 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
375 lov, uuid->uuid, ev);
378 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
379 tgt = lov->lov_tgts[index];
383 * LU-642, initially inactive OSC could miss the obd_connect,
384 * we make up for it here.
386 if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
387 obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
388 struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
390 obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
391 &lov_osc_uuid, &lov->lov_ocd, NULL);
396 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
397 index, obd_uuid2str(&tgt->ltd_uuid),
398 tgt->ltd_exp->exp_handle.h_cookie);
399 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
403 if (index == lov->desc.ld_tgt_count)
404 GOTO(out, index = -EINVAL);
406 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
407 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
409 if (lov->lov_tgts[index]->ltd_activate == activate) {
410 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
411 uuid->uuid, activate ? "" : "de");
413 lov->lov_tgts[index]->ltd_activate = activate;
414 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
415 activate ? "" : "de", obd_uuid2str(uuid));
418 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
419 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
421 if (lov->lov_tgts[index]->ltd_active == active) {
422 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
423 uuid->uuid, active ? "" : "in");
426 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
427 obd_uuid2str(uuid), active ? "" : "in");
430 lov->lov_tgts[index]->ltd_active = active;
432 lov->desc.ld_active_tgt_count++;
433 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
435 lov->desc.ld_active_tgt_count--;
436 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
439 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
448 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
449 enum obd_notify_event ev, void *data)
452 struct lov_obd *lov = &obd->u.lov;
455 down_read(&lov->lov_notify_lock);
456 if (!lov->lov_connects) {
457 up_read(&lov->lov_notify_lock);
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 up_read(&lov->lov_notify_lock);
469 CERROR("unexpected notification of %s %s!\n",
470 watched->obd_type->typ_name,
474 uuid = &watched->u.cli.cl_target_uuid;
476 /* Set OSC as active before notifying the observer, so the
477 * observer can use the OSC normally.
479 rc = lov_set_osc_active(obd, uuid, ev);
481 up_read(&lov->lov_notify_lock);
482 CERROR("event(%d) of %s failed: %d\n", ev,
483 obd_uuid2str(uuid), rc);
486 /* active event should be pass lov target index as data */
490 /* Pass the notification up the chain. */
492 rc = obd_notify_observer(obd, watched, ev, data);
494 /* NULL watched means all osc's in the lov (only for syncs) */
495 /* sync event should be send lov idx as data */
496 struct lov_obd *lov = &obd->u.lov;
500 is_sync = (ev == OBD_NOTIFY_SYNC) ||
501 (ev == OBD_NOTIFY_SYNC_NONBLOCK);
504 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
505 if (!lov->lov_tgts[i])
508 /* don't send sync event if target not
509 * connected/activated */
510 if (is_sync && !lov->lov_tgts[i]->ltd_active)
513 rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
516 CERROR("%s: notify %s of %s failed %d\n",
518 obd->obd_observer->obd_name,
519 lov->lov_tgts[i]->ltd_obd->obd_name,
526 up_read(&lov->lov_notify_lock);
530 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
531 __u32 index, int gen, int active)
533 struct lov_obd *lov = &obd->u.lov;
534 struct lov_tgt_desc *tgt;
535 struct obd_device *tgt_obd;
539 CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
540 uuidp->uuid, index, gen, active);
543 CERROR("request to add OBD %s with invalid generation: %d\n",
548 tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
553 mutex_lock(&lov->lov_lock);
555 if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
556 tgt = lov->lov_tgts[index];
557 CERROR("UUID %s already assigned at LOV target index %d\n",
558 obd_uuid2str(&tgt->ltd_uuid), index);
559 mutex_unlock(&lov->lov_lock);
563 if (index >= lov->lov_tgt_size) {
564 /* We need to reallocate the lov target array. */
565 struct lov_tgt_desc **newtgts, **old = NULL;
566 __u32 newsize, oldsize = 0;
568 newsize = max(lov->lov_tgt_size, (__u32)2);
569 while (newsize < index + 1)
570 newsize = newsize << 1;
571 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
572 if (newtgts == NULL) {
573 mutex_unlock(&lov->lov_lock);
577 if (lov->lov_tgt_size) {
578 memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
581 oldsize = lov->lov_tgt_size;
584 lov->lov_tgts = newtgts;
585 lov->lov_tgt_size = newsize;
588 OBD_FREE(old, sizeof(*old) * oldsize);
590 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
591 lov->lov_tgts, lov->lov_tgt_size);
596 mutex_unlock(&lov->lov_lock);
600 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
602 mutex_unlock(&lov->lov_lock);
607 tgt->ltd_uuid = *uuidp;
608 tgt->ltd_obd = tgt_obd;
609 /* XXX - add a sanity check on the generation number. */
611 tgt->ltd_index = index;
612 tgt->ltd_activate = active;
613 lov->lov_tgts[index] = tgt;
614 if (index >= lov->desc.ld_tgt_count)
615 lov->desc.ld_tgt_count = index + 1;
617 mutex_unlock(&lov->lov_lock);
619 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
620 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
622 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
624 if (lov->lov_connects == 0) {
625 /* lov_connect hasn't been called yet. We'll do the
626 lov_connect_obd on this target when that fn first runs,
627 because we don't know the connect flags yet. */
633 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
637 /* connect to administrative disabled ost */
641 if (lov->lov_cache != NULL) {
642 rc = obd_set_info_async(NULL, tgt->ltd_exp,
643 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
644 sizeof(struct cl_client_cache), lov->lov_cache,
650 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
651 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
656 CERROR("add failed (%d), deleting %s\n", rc,
657 obd_uuid2str(&tgt->ltd_uuid));
658 lov_del_target(obd, index, NULL, 0);
664 /* Schedule a target for deletion */
665 int lov_del_target(struct obd_device *obd, __u32 index,
666 struct obd_uuid *uuidp, int gen)
668 struct lov_obd *lov = &obd->u.lov;
669 int count = lov->desc.ld_tgt_count;
673 if (index >= count) {
674 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
679 /* to make sure there's no ongoing lov_notify() now */
680 down_write(&lov->lov_notify_lock);
683 if (!lov->lov_tgts[index]) {
684 CERROR("LOV target at index %d is not setup.\n", index);
685 GOTO(out, rc = -EINVAL);
688 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
689 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
690 lov_uuid2str(lov, index), index,
691 obd_uuid2str(uuidp));
692 GOTO(out, rc = -EINVAL);
695 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
696 lov_uuid2str(lov, index), index,
697 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
698 lov->lov_tgts[index]->ltd_active);
700 lov->lov_tgts[index]->ltd_reap = 1;
701 lov->lov_death_row++;
702 /* we really delete it from obd_putref */
705 up_write(&lov->lov_notify_lock);
710 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
712 struct obd_device *osc_obd;
715 LASSERT(tgt->ltd_reap);
717 osc_obd = class_exp2obd(tgt->ltd_exp);
719 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
721 osc_obd ? osc_obd->obd_name : "<no obd>");
724 lov_disconnect_obd(obd, tgt);
728 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
729 do it ourselves. And we can't do it from lov_cleanup,
730 because we just lost our only reference to it. */
732 class_manual_cleanup(osc_obd);
735 void lov_fix_desc_stripe_size(__u64 *val)
737 if (*val < LOV_MIN_STRIPE_SIZE) {
739 LCONSOLE_INFO("Increasing default stripe size to "
741 LOV_DESC_STRIPE_SIZE_DEFAULT);
742 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
743 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
744 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
745 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
747 *val, LOV_MIN_STRIPE_SIZE);
751 void lov_fix_desc_stripe_count(__u32 *val)
757 void lov_fix_desc_pattern(__u32 *val)
759 /* from lov_setstripe */
760 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
761 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
766 void lov_fix_desc_qos_maxage(__u32 *val)
769 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
772 void lov_fix_desc(struct lov_desc *desc)
774 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
775 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
776 lov_fix_desc_pattern(&desc->ld_pattern);
777 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
780 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
782 struct lov_desc *desc;
783 struct lov_obd *lov = &obd->u.lov;
784 #ifdef CONFIG_PROC_FS
785 struct obd_type *type;
790 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
791 CERROR("LOV setup requires a descriptor\n");
795 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
797 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
798 CERROR("descriptor size wrong: %d > %d\n",
799 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
803 if (desc->ld_magic != LOV_DESC_MAGIC) {
804 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
805 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
806 obd->obd_name, desc);
807 lustre_swab_lov_desc(desc);
809 CERROR("%s: Bad lov desc magic: %#x\n",
810 obd->obd_name, desc->ld_magic);
817 desc->ld_active_tgt_count = 0;
819 lov->lov_tgt_size = 0;
821 mutex_init(&lov->lov_lock);
822 atomic_set(&lov->lov_refcount, 0);
823 lov->lov_sp_me = LUSTRE_SP_CLI;
825 init_rwsem(&lov->lov_notify_lock);
827 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
829 HASH_POOLS_BKT_BITS, 0,
832 &pool_hash_operations,
834 INIT_LIST_HEAD(&lov->lov_pool_list);
835 lov->lov_pool_count = 0;
836 rc = lov_ost_pool_init(&lov->lov_packed, 0);
840 #ifdef CONFIG_PROC_FS
841 obd->obd_vars = lprocfs_lov_obd_vars;
842 /* If this is true then both client (lov) and server
843 * (lod) are on the same node. The lod layer if loaded
844 * first will register the lov proc directory. In that
845 * case obd->obd_type->typ_procroot will be not set.
846 * Instead we use type->typ_procsym as the parent. */
847 type = class_search_type(LUSTRE_LOD_NAME);
848 if (type != NULL && type->typ_procsym != NULL) {
849 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
852 if (IS_ERR(obd->obd_proc_entry)) {
853 rc = PTR_ERR(obd->obd_proc_entry);
854 CERROR("error %d setting up lprocfs for %s\n", rc,
856 obd->obd_proc_entry = NULL;
859 rc = lprocfs_obd_setup(obd);
863 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
864 0444, &lov_proc_target_fops, obd);
866 CWARN("Error adding the target_obd file\n");
868 lov->lov_pool_proc_entry = lprocfs_register("pools",
871 if (IS_ERR(lov->lov_pool_proc_entry)) {
872 rc = PTR_ERR(lov->lov_pool_proc_entry);
873 CERROR("error %d setting up lprocfs for pools\n", rc);
874 lov->lov_pool_proc_entry = NULL;
884 static int lov_cleanup(struct obd_device *obd)
886 struct lov_obd *lov = &obd->u.lov;
887 struct list_head *pos, *tmp;
888 struct pool_desc *pool;
891 list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
892 pool = list_entry(pos, struct pool_desc, pool_list);
893 /* free pool structs */
894 CDEBUG(D_INFO, "delete pool %p\n", pool);
895 /* In the function below, .hs_keycmp resolves to
896 * pool_hashkey_keycmp() */
897 /* coverity[overrun-buffer-val] */
898 lov_pool_del(obd, pool->pool_name);
900 cfs_hash_putref(lov->lov_pools_hash_body);
901 lov_ost_pool_free(&lov->lov_packed);
903 lprocfs_obd_cleanup(obd);
907 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
908 if (!lov->lov_tgts[i])
911 /* Inactive targets may never have connected */
912 if (lov->lov_tgts[i]->ltd_active ||
913 atomic_read(&lov->lov_refcount))
914 /* We should never get here - these
915 * should have been removed in the
917 CERROR("%s: lov tgt %d not cleaned! "
918 "deathrow=%d, lovrc=%d\n",
919 obd->obd_name, i, lov->lov_death_row,
920 atomic_read(&lov->lov_refcount));
921 lov_del_target(obd, i, NULL, 0);
924 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
926 lov->lov_tgt_size = 0;
929 if (lov->lov_cache != NULL) {
930 cl_cache_decref(lov->lov_cache);
931 lov->lov_cache = NULL;
937 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
938 __u32 *indexp, int *genp)
940 struct obd_uuid obd_uuid;
945 switch(cmd = lcfg->lcfg_command) {
946 case LCFG_LOV_ADD_OBD:
947 case LCFG_LOV_ADD_INA:
948 case LCFG_LOV_DEL_OBD: {
951 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
952 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
953 GOTO(out, rc = -EINVAL);
955 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
957 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", indexp) != 1)
958 GOTO(out, rc = -EINVAL);
959 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
960 GOTO(out, rc = -EINVAL);
963 if (cmd == LCFG_LOV_ADD_OBD)
964 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
965 else if (cmd == LCFG_LOV_ADD_INA)
966 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
968 rc = lov_del_target(obd, index, &obd_uuid, gen);
972 struct lov_desc *desc = &(obd->u.lov.desc);
975 GOTO(out, rc = -EINVAL);
977 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
990 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
991 GOTO(out, rc = -EINVAL);
999 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1001 struct lov_request_set *lovset = (struct lov_request_set *)data;
1006 atomic_set(&lovset->set_completes, 0);
1008 err = lov_fini_statfs_set(lovset);
1009 RETURN(rc ? rc : err);
1012 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1013 __u64 max_age, struct ptlrpc_request_set *rqset)
1015 struct obd_device *obd = class_exp2obd(exp);
1016 struct lov_request_set *set;
1017 struct lov_request *req;
1018 struct list_head *pos;
1019 struct lov_obd *lov;
1023 LASSERT(oinfo != NULL);
1024 LASSERT(oinfo->oi_osfs != NULL);
1027 rc = lov_prep_statfs_set(obd, oinfo, &set);
1031 list_for_each(pos, &set->set_list) {
1032 req = list_entry(pos, struct lov_request, rq_link);
1033 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1034 &req->rq_oi, max_age, rqset);
1039 if (rc || list_empty(&rqset->set_requests)) {
1042 atomic_set(&set->set_completes, 0);
1043 err = lov_fini_statfs_set(set);
1044 RETURN(rc ? rc : err);
1047 LASSERT(rqset->set_interpret == NULL);
1048 rqset->set_interpret = lov_statfs_interpret;
1049 rqset->set_arg = (void *)set;
1053 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1054 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1056 struct ptlrpc_request_set *set = NULL;
1057 struct obd_info oinfo = {
1065 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1066 * statfs requests */
1067 set = ptlrpc_prep_set();
1071 rc = lov_statfs_async(exp, &oinfo, max_age, set);
1073 rc = ptlrpc_set_wait(set);
1075 ptlrpc_set_destroy(set);
1080 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1081 void *karg, void __user *uarg)
1083 struct obd_device *obddev = class_exp2obd(exp);
1084 struct lov_obd *lov = &obddev->u.lov;
1085 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1086 struct obd_uuid *uuidp;
1090 case IOC_OBD_STATFS: {
1091 struct obd_ioctl_data *data = karg;
1092 struct obd_device *osc_obd;
1093 struct obd_statfs stat_buf = {0};
1097 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1098 if ((index >= count))
1101 if (!lov->lov_tgts[index])
1102 /* Try again with the next index */
1104 if (!lov->lov_tgts[index]->ltd_active)
1107 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1112 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1113 min_t(unsigned long, data->ioc_plen2,
1114 sizeof(struct obd_uuid))))
1117 flags = uarg ? *(__u32 __user *)uarg : 0;
1118 /* got statfs data */
1119 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1120 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1124 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1125 min_t(unsigned long, data->ioc_plen1,
1126 sizeof(struct obd_statfs))))
1130 case OBD_IOC_LOV_GET_CONFIG: {
1131 struct obd_ioctl_data *data;
1132 struct lov_desc *desc;
1137 if (obd_ioctl_getdata(&buf, &len, uarg))
1140 data = (struct obd_ioctl_data *)buf;
1142 if (sizeof(*desc) > data->ioc_inllen1) {
1143 obd_ioctl_freedata(buf, len);
1147 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1148 obd_ioctl_freedata(buf, len);
1152 if (sizeof(__u32) * count > data->ioc_inllen3) {
1153 obd_ioctl_freedata(buf, len);
1157 desc = (struct lov_desc *)data->ioc_inlbuf1;
1158 memcpy(desc, &(lov->desc), sizeof(*desc));
1160 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1161 genp = (__u32 *)data->ioc_inlbuf3;
1162 /* the uuid will be empty for deleted OSTs */
1163 for (i = 0; i < count; i++, uuidp++, genp++) {
1164 if (!lov->lov_tgts[i])
1166 *uuidp = lov->lov_tgts[i]->ltd_uuid;
1167 *genp = lov->lov_tgts[i]->ltd_gen;
1170 if (copy_to_user(uarg, buf, len))
1172 obd_ioctl_freedata(buf, len);
1175 case OBD_IOC_QUOTACTL: {
1176 struct if_quotactl *qctl = karg;
1177 struct lov_tgt_desc *tgt = NULL;
1178 struct obd_quotactl *oqctl;
1180 if (qctl->qc_valid == QC_OSTIDX) {
1181 if (count <= qctl->qc_idx)
1184 tgt = lov->lov_tgts[qctl->qc_idx];
1185 if (!tgt || !tgt->ltd_exp)
1187 } else if (qctl->qc_valid == QC_UUID) {
1188 for (i = 0; i < count; i++) {
1189 tgt = lov->lov_tgts[i];
1191 !obd_uuid_equals(&tgt->ltd_uuid,
1195 if (tgt->ltd_exp == NULL)
1207 LASSERT(tgt && tgt->ltd_exp);
1208 OBD_ALLOC_PTR(oqctl);
1212 QCTL_COPY(oqctl, qctl);
1213 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1215 QCTL_COPY(qctl, oqctl);
1216 qctl->qc_valid = QC_OSTIDX;
1217 qctl->obd_uuid = tgt->ltd_uuid;
1219 OBD_FREE_PTR(oqctl);
1228 for (i = 0; i < count; i++) {
1230 struct obd_device *osc_obd;
1232 /* OST was disconnected */
1233 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1236 /* ll_umount_begin() sets force flag but for lov, not
1237 * osc. Let's pass it through */
1238 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1239 osc_obd->obd_force = obddev->obd_force;
1240 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1243 if (lov->lov_tgts[i]->ltd_active) {
1244 CDEBUG(err == -ENOTTY ?
1245 D_IOCTL : D_WARNING,
1246 "iocontrol OSC %s on OST "
1247 "idx %d cmd %x: err = %d\n",
1248 lov_uuid2str(lov, i),
1265 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1266 __u32 keylen, void *key, __u32 *vallen, void *val)
1268 struct obd_device *obddev = class_exp2obd(exp);
1269 struct lov_obd *lov = &obddev->u.lov;
1270 struct lov_desc *ld = &lov->desc;
1274 if (vallen == NULL || val == NULL)
1279 if (KEY_IS(KEY_MAX_EASIZE)) {
1280 u32 max_stripe_count = min_t(u32, ld->ld_active_tgt_count,
1281 LOV_MAX_STRIPE_COUNT);
1283 *((u32 *)val) = lov_mds_md_size(max_stripe_count, LOV_MAGIC_V3);
1284 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
1285 u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
1286 LOV_MAX_STRIPE_COUNT);
1288 *((u32 *)val) = lov_mds_md_size(def_stripe_count, LOV_MAGIC_V3);
1289 } else if (KEY_IS(KEY_TGT_COUNT)) {
1290 *((int *)val) = lov->desc.ld_tgt_count;
1300 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1301 __u32 keylen, void *key,
1302 __u32 vallen, void *val,
1303 struct ptlrpc_request_set *set)
1305 struct obd_device *obddev = class_exp2obd(exp);
1306 struct lov_obd *lov = &obddev->u.lov;
1307 struct lov_tgt_desc *tgt;
1308 int do_inactive = 0;
1318 set = ptlrpc_prep_set();
1324 count = lov->desc.ld_tgt_count;
1326 if (KEY_IS(KEY_CHECKSUM)) {
1328 } else if (KEY_IS(KEY_CACHE_SET)) {
1329 LASSERT(lov->lov_cache == NULL);
1330 lov->lov_cache = val;
1332 cl_cache_incref(lov->lov_cache);
1335 for (i = 0; i < count; i++) {
1336 tgt = lov->lov_tgts[i];
1338 /* OST was disconnected */
1339 if (!tgt || !tgt->ltd_exp)
1342 /* OST is inactive and we don't want inactive OSCs */
1343 if (!tgt->ltd_active && !do_inactive)
1346 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1354 err = ptlrpc_set_wait(set);
1357 ptlrpc_set_destroy(set);
1362 void lov_stripe_lock(struct lov_stripe_md *md)
1363 __acquires(&md->lsm_lock)
1365 LASSERT(md->lsm_lock_owner != current_pid());
1366 spin_lock(&md->lsm_lock);
1367 LASSERT(md->lsm_lock_owner == 0);
1368 md->lsm_lock_owner = current_pid();
1371 void lov_stripe_unlock(struct lov_stripe_md *md)
1372 __releases(&md->lsm_lock)
1374 LASSERT(md->lsm_lock_owner == current_pid());
1375 md->lsm_lock_owner = 0;
1376 spin_unlock(&md->lsm_lock);
1379 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1380 struct obd_quotactl *oqctl)
1382 struct lov_obd *lov = &obd->u.lov;
1383 struct lov_tgt_desc *tgt;
1385 __u64 bhardlimit = 0;
1389 if (oqctl->qc_cmd != Q_GETOQUOTA &&
1390 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA) {
1391 CERROR("%s: bad quota opc %x for lov obd\n",
1392 obd->obd_name, oqctl->qc_cmd);
1398 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1401 tgt = lov->lov_tgts[i];
1406 if (!tgt->ltd_active || tgt->ltd_reap) {
1407 if (oqctl->qc_cmd == Q_GETOQUOTA &&
1408 lov->lov_tgts[i]->ltd_activate) {
1410 CERROR("ost %d is inactive\n", i);
1412 CDEBUG(D_HA, "ost %d is inactive\n", i);
1417 err = obd_quotactl(tgt->ltd_exp, oqctl);
1419 if (tgt->ltd_active && !rc)
1424 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1425 curspace += oqctl->qc_dqblk.dqb_curspace;
1426 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
1431 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1432 oqctl->qc_dqblk.dqb_curspace = curspace;
1433 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
1438 static struct obd_ops lov_obd_ops = {
1439 .o_owner = THIS_MODULE,
1440 .o_setup = lov_setup,
1441 .o_cleanup = lov_cleanup,
1442 .o_connect = lov_connect,
1443 .o_disconnect = lov_disconnect,
1444 .o_statfs = lov_statfs,
1445 .o_statfs_async = lov_statfs_async,
1446 .o_iocontrol = lov_iocontrol,
1447 .o_get_info = lov_get_info,
1448 .o_set_info_async = lov_set_info_async,
1449 .o_notify = lov_notify,
1450 .o_pool_new = lov_pool_new,
1451 .o_pool_rem = lov_pool_remove,
1452 .o_pool_add = lov_pool_add,
1453 .o_pool_del = lov_pool_del,
1454 .o_getref = lov_getref,
1455 .o_putref = lov_putref,
1456 .o_quotactl = lov_quotactl,
1459 struct kmem_cache *lov_oinfo_slab;
1461 static int __init lov_init(void)
1463 bool enable_proc = true;
1464 struct obd_type *type;
1468 /* print an address of _any_ initialized kernel symbol from this
1469 * module, to allow debugging with gdb that doesn't support data
1470 * symbols from modules.*/
1471 CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
1473 rc = lu_kmem_init(lov_caches);
1477 lov_oinfo_slab = kmem_cache_create("lov_oinfo",
1478 sizeof(struct lov_oinfo), 0,
1479 SLAB_HWCACHE_ALIGN, NULL);
1480 if (lov_oinfo_slab == NULL) {
1481 lu_kmem_fini(lov_caches);
1485 type = class_search_type(LUSTRE_LOD_NAME);
1486 if (type != NULL && type->typ_procsym != NULL)
1487 enable_proc = false;
1489 rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
1490 LUSTRE_LOV_NAME, &lov_device_type);
1493 kmem_cache_destroy(lov_oinfo_slab);
1494 lu_kmem_fini(lov_caches);
1500 static void /*__exit*/ lov_exit(void)
1502 class_unregister_type(LUSTRE_LOV_NAME);
1503 kmem_cache_destroy(lov_oinfo_slab);
1504 lu_kmem_fini(lov_caches);
1507 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1508 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
1509 MODULE_VERSION(LUSTRE_VERSION_STRING);
1510 MODULE_LICENSE("GPL");
1512 module_init(lov_init);
1513 module_exit(lov_exit);