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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/lov/lov_obd.c
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Mike Shaver <shaver@clusterfs.com>
37 * Author: Nathan Rutman <nathan@clusterfs.com>
40 #define DEBUG_SUBSYSTEM S_LOV
41 #include "../../include/linux/libcfs/libcfs.h"
43 #include "../include/lustre/lustre_idl.h"
44 #include "../include/lustre/lustre_ioctl.h"
46 #include "../include/cl_object.h"
47 #include "../include/lustre_dlm.h"
48 #include "../include/lustre_fid.h"
49 #include "../include/lustre_lib.h"
50 #include "../include/lustre_mds.h"
51 #include "../include/lustre_net.h"
52 #include "../include/lustre_param.h"
53 #include "../include/lustre_swab.h"
54 #include "../include/lprocfs_status.h"
55 #include "../include/obd_class.h"
56 #include "../include/obd_support.h"
58 #include "lov_internal.h"
60 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
61 * Any function that expects lov_tgts to remain stationary must take a ref.
63 static void lov_getref(struct obd_device *obd)
65 struct lov_obd *lov = &obd->u.lov;
67 /* nobody gets through here until lov_putref is done */
68 mutex_lock(&lov->lov_lock);
69 atomic_inc(&lov->lov_refcount);
70 mutex_unlock(&lov->lov_lock);
74 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
76 static void lov_putref(struct obd_device *obd)
78 struct lov_obd *lov = &obd->u.lov;
80 mutex_lock(&lov->lov_lock);
81 /* ok to dec to 0 more than once -- ltd_exp's will be null */
82 if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
85 struct lov_tgt_desc *tgt, *n;
87 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
89 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
90 tgt = lov->lov_tgts[i];
92 if (!tgt || !tgt->ltd_reap)
94 list_add(&tgt->ltd_kill, &kill);
95 /* XXX - right now there is a dependency on ld_tgt_count
96 * being the maximum tgt index for computing the
97 * 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 if (lov->lov_tgts_kobj)
112 kobject_put(lov->lov_tgts_kobj);
115 mutex_unlock(&lov->lov_lock);
119 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
120 enum obd_notify_event ev);
121 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
122 enum obd_notify_event ev, void *data);
124 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
125 struct obd_connect_data *data)
127 struct lov_obd *lov = &obd->u.lov;
128 struct obd_uuid *tgt_uuid;
129 struct obd_device *tgt_obd;
130 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
131 struct obd_import *imp;
134 if (!lov->lov_tgts[index])
137 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
138 tgt_obd = lov->lov_tgts[index]->ltd_obd;
140 if (!tgt_obd->obd_set_up) {
141 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
145 /* override the sp_me from lov */
146 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
148 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
149 data->ocd_index = index;
152 * Divine LOV knows that OBDs under it are OSCs.
154 imp = tgt_obd->u.cli.cl_import;
157 tgt_obd->obd_no_recov = 0;
158 /* FIXME this is probably supposed to be
159 * ptlrpc_set_import_active. Horrible naming.
161 ptlrpc_activate_import(imp);
164 rc = obd_register_observer(tgt_obd, obd);
166 CERROR("Target %s register_observer error %d\n",
167 obd_uuid2str(tgt_uuid), rc);
171 if (imp->imp_invalid) {
172 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively disabled\n",
173 obd_uuid2str(tgt_uuid));
177 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
178 &lov_osc_uuid, data, NULL);
179 if (rc || !lov->lov_tgts[index]->ltd_exp) {
180 CERROR("Target %s connect error %d\n",
181 obd_uuid2str(tgt_uuid), rc);
185 lov->lov_tgts[index]->ltd_reap = 0;
187 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
188 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
190 if (lov->lov_tgts_kobj)
191 /* Even if we failed, that's ok */
192 rc = sysfs_create_link(lov->lov_tgts_kobj, &tgt_obd->obd_kobj,
198 static int lov_connect(const struct lu_env *env,
199 struct obd_export **exp, struct obd_device *obd,
200 struct obd_uuid *cluuid, struct obd_connect_data *data,
203 struct lov_obd *lov = &obd->u.lov;
204 struct lov_tgt_desc *tgt;
205 struct lustre_handle conn;
208 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
210 rc = class_connect(&conn, obd, cluuid);
214 *exp = class_conn2export(&conn);
216 /* Why should there ever be more than 1 connect? */
218 LASSERT(lov->lov_connects == 1);
220 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
222 lov->lov_ocd = *data;
226 lov->lov_tgts_kobj = kobject_create_and_add("target_obds",
229 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
230 tgt = lov->lov_tgts[i];
231 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
233 /* Flags will be lowest common denominator */
234 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
236 CERROR("%s: lov connect tgt %d failed: %d\n",
237 obd->obd_name, i, rc);
240 /* connect to administrative disabled ost */
241 if (!lov->lov_tgts[i]->ltd_exp)
244 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
245 OBD_NOTIFY_CONNECT, (void *)&i);
247 CERROR("%s error sending notify %d\n",
256 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
258 struct lov_obd *lov = &obd->u.lov;
259 struct obd_device *osc_obd;
262 osc_obd = class_exp2obd(tgt->ltd_exp);
263 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
264 obd->obd_name, osc_obd ? osc_obd->obd_name : "NULL");
266 if (tgt->ltd_active) {
268 lov->desc.ld_active_tgt_count--;
269 tgt->ltd_exp->exp_obd->obd_inactive = 1;
273 if (lov->lov_tgts_kobj)
274 sysfs_remove_link(lov->lov_tgts_kobj,
277 /* Pass it on to our clients.
278 * XXX This should be an argument to disconnect,
279 * XXX not a back-door flag on the OBD. Ah well.
281 osc_obd->obd_force = obd->obd_force;
282 osc_obd->obd_fail = obd->obd_fail;
283 osc_obd->obd_no_recov = obd->obd_no_recov;
286 obd_register_observer(osc_obd, NULL);
288 rc = obd_disconnect(tgt->ltd_exp);
290 CERROR("Target %s disconnect error %d\n",
291 tgt->ltd_uuid.uuid, rc);
299 static int lov_disconnect(struct obd_export *exp)
301 struct obd_device *obd = class_exp2obd(exp);
302 struct lov_obd *lov = &obd->u.lov;
308 /* Only disconnect the underlying layers on the final disconnect. */
310 if (lov->lov_connects != 0) {
311 /* why should there be more than 1 connect? */
312 CERROR("disconnect #%d\n", lov->lov_connects);
316 /* Let's hold another reference so lov_del_obd doesn't spin through
321 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
322 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
323 /* Disconnection is the last we know about an obd */
324 lov_del_target(obd, i, NULL, lov->lov_tgts[i]->ltd_gen);
331 rc = class_disconnect(exp); /* bz 9811 */
337 * -EINVAL : UUID can't be found in the LOV's target list
338 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
339 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
340 * any >= 0 : is log target index
342 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
343 enum obd_notify_event ev)
345 struct lov_obd *lov = &obd->u.lov;
346 struct lov_tgt_desc *tgt;
347 int index, activate, active;
349 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
350 lov, uuid->uuid, ev);
353 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
354 tgt = lov->lov_tgts[index];
358 * LU-642, initially inactive OSC could miss the obd_connect,
359 * we make up for it here.
361 if (ev == OBD_NOTIFY_ACTIVATE && !tgt->ltd_exp &&
362 obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
363 struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
365 obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
366 &lov_osc_uuid, &lov->lov_ocd, NULL);
371 CDEBUG(D_INFO, "lov idx %d is %s conn %#llx\n",
372 index, obd_uuid2str(&tgt->ltd_uuid),
373 tgt->ltd_exp->exp_handle.h_cookie);
374 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
378 if (index == lov->desc.ld_tgt_count) {
383 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
384 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
386 if (lov->lov_tgts[index]->ltd_activate == activate) {
387 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
388 uuid->uuid, activate ? "" : "de");
390 lov->lov_tgts[index]->ltd_activate = activate;
391 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
392 activate ? "" : "de", obd_uuid2str(uuid));
395 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
396 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
398 if (lov->lov_tgts[index]->ltd_active == active) {
399 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
400 uuid->uuid, active ? "" : "in");
403 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
404 obd_uuid2str(uuid), active ? "" : "in");
406 lov->lov_tgts[index]->ltd_active = active;
408 lov->desc.ld_active_tgt_count++;
409 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
411 lov->desc.ld_active_tgt_count--;
412 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
415 CERROR("Unknown event(%d) for uuid %s", ev, uuid->uuid);
423 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
424 enum obd_notify_event ev, void *data)
427 struct lov_obd *lov = &obd->u.lov;
429 down_read(&lov->lov_notify_lock);
430 if (!lov->lov_connects) {
431 up_read(&lov->lov_notify_lock);
435 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
436 ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
437 struct obd_uuid *uuid;
441 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
442 up_read(&lov->lov_notify_lock);
443 CERROR("unexpected notification of %s %s!\n",
444 watched->obd_type->typ_name,
448 uuid = &watched->u.cli.cl_target_uuid;
450 /* Set OSC as active before notifying the observer, so the
451 * observer can use the OSC normally.
453 rc = lov_set_osc_active(obd, uuid, ev);
455 up_read(&lov->lov_notify_lock);
456 CERROR("event(%d) of %s failed: %d\n", ev,
457 obd_uuid2str(uuid), rc);
460 /* active event should be pass lov target index as data */
464 /* Pass the notification up the chain. */
466 rc = obd_notify_observer(obd, watched, ev, data);
468 /* NULL watched means all osc's in the lov (only for syncs) */
469 /* sync event should be send lov idx as data */
470 struct lov_obd *lov = &obd->u.lov;
474 is_sync = (ev == OBD_NOTIFY_SYNC) ||
475 (ev == OBD_NOTIFY_SYNC_NONBLOCK);
478 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
479 if (!lov->lov_tgts[i])
482 /* don't send sync event if target not
483 * connected/activated
485 if (is_sync && !lov->lov_tgts[i]->ltd_active)
488 rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
491 CERROR("%s: notify %s of %s failed %d\n",
493 obd->obd_observer->obd_name,
494 lov->lov_tgts[i]->ltd_obd->obd_name,
501 up_read(&lov->lov_notify_lock);
505 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
506 __u32 index, int gen, int active)
508 struct lov_obd *lov = &obd->u.lov;
509 struct lov_tgt_desc *tgt;
510 struct obd_device *tgt_obd;
513 CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
514 uuidp->uuid, index, gen, active);
517 CERROR("request to add OBD %s with invalid generation: %d\n",
522 tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
527 mutex_lock(&lov->lov_lock);
529 if ((index < lov->lov_tgt_size) && lov->lov_tgts[index]) {
530 tgt = lov->lov_tgts[index];
531 CERROR("UUID %s already assigned at LOV target index %d\n",
532 obd_uuid2str(&tgt->ltd_uuid), index);
533 mutex_unlock(&lov->lov_lock);
537 if (index >= lov->lov_tgt_size) {
538 /* We need to reallocate the lov target array. */
539 struct lov_tgt_desc **newtgts, **old = NULL;
540 __u32 newsize, oldsize = 0;
542 newsize = max_t(__u32, lov->lov_tgt_size, 2);
543 while (newsize < index + 1)
545 newtgts = kcalloc(newsize, sizeof(*newtgts), GFP_NOFS);
547 mutex_unlock(&lov->lov_lock);
551 if (lov->lov_tgt_size) {
552 memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
555 oldsize = lov->lov_tgt_size;
558 lov->lov_tgts = newtgts;
559 lov->lov_tgt_size = newsize;
563 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
564 lov->lov_tgts, lov->lov_tgt_size);
567 tgt = kzalloc(sizeof(*tgt), GFP_NOFS);
569 mutex_unlock(&lov->lov_lock);
573 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
575 mutex_unlock(&lov->lov_lock);
580 tgt->ltd_uuid = *uuidp;
581 tgt->ltd_obd = tgt_obd;
582 /* XXX - add a sanity check on the generation number. */
584 tgt->ltd_index = index;
585 tgt->ltd_activate = active;
586 lov->lov_tgts[index] = tgt;
587 if (index >= lov->desc.ld_tgt_count)
588 lov->desc.ld_tgt_count = index + 1;
590 mutex_unlock(&lov->lov_lock);
592 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
593 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
595 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
597 if (lov->lov_connects == 0) {
598 /* lov_connect hasn't been called yet. We'll do the
599 * lov_connect_obd on this target when that fn first runs,
600 * because we don't know the connect flags yet.
607 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
611 /* connect to administrative disabled ost */
617 if (lov->lov_cache) {
618 rc = obd_set_info_async(NULL, tgt->ltd_exp,
619 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
620 sizeof(struct cl_client_cache),
621 lov->lov_cache, NULL);
626 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
627 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
632 CERROR("add failed (%d), deleting %s\n", rc,
633 obd_uuid2str(&tgt->ltd_uuid));
634 lov_del_target(obd, index, NULL, 0);
640 /* Schedule a target for deletion */
641 int lov_del_target(struct obd_device *obd, __u32 index,
642 struct obd_uuid *uuidp, int gen)
644 struct lov_obd *lov = &obd->u.lov;
645 int count = lov->desc.ld_tgt_count;
648 if (index >= count) {
649 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
654 /* to make sure there's no ongoing lov_notify() now */
655 down_write(&lov->lov_notify_lock);
658 if (!lov->lov_tgts[index]) {
659 CERROR("LOV target at index %d is not setup.\n", index);
664 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
665 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
666 lov_uuid2str(lov, index), index,
667 obd_uuid2str(uuidp));
672 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
673 lov_uuid2str(lov, index), index,
674 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
675 lov->lov_tgts[index]->ltd_active);
677 lov->lov_tgts[index]->ltd_reap = 1;
678 lov->lov_death_row++;
679 /* we really delete it from obd_putref */
682 up_write(&lov->lov_notify_lock);
687 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
689 struct obd_device *osc_obd;
692 LASSERT(tgt->ltd_reap);
694 osc_obd = class_exp2obd(tgt->ltd_exp);
696 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
698 osc_obd ? osc_obd->obd_name : "<no obd>");
701 lov_disconnect_obd(obd, tgt);
705 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
706 * do it ourselves. And we can't do it from lov_cleanup,
707 * because we just lost our only reference to it.
710 class_manual_cleanup(osc_obd);
713 void lov_fix_desc_stripe_size(__u64 *val)
715 if (*val < LOV_MIN_STRIPE_SIZE) {
717 LCONSOLE_INFO("Increasing default stripe size to minimum %u\n",
718 LOV_DESC_STRIPE_SIZE_DEFAULT);
719 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
720 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
721 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
722 LCONSOLE_WARN("Changing default stripe size to %llu (a multiple of %u)\n",
723 *val, LOV_MIN_STRIPE_SIZE);
727 void lov_fix_desc_stripe_count(__u32 *val)
733 void lov_fix_desc_pattern(__u32 *val)
735 /* from lov_setstripe */
736 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
737 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
742 void lov_fix_desc_qos_maxage(__u32 *val)
745 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
748 void lov_fix_desc(struct lov_desc *desc)
750 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
751 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
752 lov_fix_desc_pattern(&desc->ld_pattern);
753 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
756 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
758 struct lprocfs_static_vars lvars = { NULL };
759 struct lov_desc *desc;
760 struct lov_obd *lov = &obd->u.lov;
763 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
764 CERROR("LOV setup requires a descriptor\n");
768 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
770 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
771 CERROR("descriptor size wrong: %d > %d\n",
772 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
776 if (desc->ld_magic != LOV_DESC_MAGIC) {
777 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
778 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
779 obd->obd_name, desc);
780 lustre_swab_lov_desc(desc);
782 CERROR("%s: Bad lov desc magic: %#x\n",
783 obd->obd_name, desc->ld_magic);
790 desc->ld_active_tgt_count = 0;
792 lov->lov_tgt_size = 0;
794 mutex_init(&lov->lov_lock);
795 atomic_set(&lov->lov_refcount, 0);
796 lov->lov_sp_me = LUSTRE_SP_CLI;
798 init_rwsem(&lov->lov_notify_lock);
800 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
802 HASH_POOLS_BKT_BITS, 0,
805 &pool_hash_operations,
807 INIT_LIST_HEAD(&lov->lov_pool_list);
808 lov->lov_pool_count = 0;
809 rc = lov_ost_pool_init(&lov->lov_packed, 0);
813 lprocfs_lov_init_vars(&lvars);
814 lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
816 rc = ldebugfs_seq_create(obd->obd_debugfs_entry, "target_obd",
817 0444, &lov_proc_target_fops, obd);
819 CWARN("Error adding the target_obd file\n");
821 lov->lov_pool_debugfs_entry = ldebugfs_register("pools",
822 obd->obd_debugfs_entry,
830 static int lov_cleanup(struct obd_device *obd)
832 struct lov_obd *lov = &obd->u.lov;
833 struct list_head *pos, *tmp;
834 struct pool_desc *pool;
836 list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
837 pool = list_entry(pos, struct pool_desc, pool_list);
838 /* free pool structs */
839 CDEBUG(D_INFO, "delete pool %p\n", pool);
840 /* In the function below, .hs_keycmp resolves to
841 * pool_hashkey_keycmp()
843 /* coverity[overrun-buffer-val] */
844 lov_pool_del(obd, pool->pool_name);
846 cfs_hash_putref(lov->lov_pools_hash_body);
847 lov_ost_pool_free(&lov->lov_packed);
849 lprocfs_obd_cleanup(obd);
854 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
855 if (!lov->lov_tgts[i])
858 /* Inactive targets may never have connected */
859 if (lov->lov_tgts[i]->ltd_active ||
860 atomic_read(&lov->lov_refcount))
861 /* We should never get here - these
862 * should have been removed in the
865 CERROR("lov tgt %d not cleaned! deathrow=%d, lovrc=%d\n",
866 i, lov->lov_death_row,
867 atomic_read(&lov->lov_refcount));
868 lov_del_target(obd, i, NULL, 0);
871 kfree(lov->lov_tgts);
872 lov->lov_tgt_size = 0;
875 if (lov->lov_cache) {
876 cl_cache_decref(lov->lov_cache);
877 lov->lov_cache = NULL;
883 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
884 __u32 *indexp, int *genp)
886 struct obd_uuid obd_uuid;
890 switch (cmd = lcfg->lcfg_command) {
891 case LCFG_LOV_ADD_OBD:
892 case LCFG_LOV_ADD_INA:
893 case LCFG_LOV_DEL_OBD: {
896 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
897 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid)) {
902 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
904 rc = kstrtoint(lustre_cfg_buf(lcfg, 2), 10, indexp);
907 rc = kstrtoint(lustre_cfg_buf(lcfg, 3), 10, genp);
912 if (cmd == LCFG_LOV_ADD_OBD)
913 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
914 else if (cmd == LCFG_LOV_ADD_INA)
915 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
917 rc = lov_del_target(obd, index, &obd_uuid, gen);
921 struct lprocfs_static_vars lvars = { NULL };
922 struct lov_desc *desc = &obd->u.lov.desc;
929 lprocfs_lov_init_vars(&lvars);
931 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
944 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
953 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
955 struct lov_request_set *lovset = (struct lov_request_set *)data;
959 atomic_set(&lovset->set_completes, 0);
961 err = lov_fini_statfs_set(lovset);
962 return rc ? rc : err;
965 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
966 __u64 max_age, struct ptlrpc_request_set *rqset)
968 struct obd_device *obd = class_exp2obd(exp);
969 struct lov_request_set *set;
970 struct lov_request *req;
974 LASSERT(oinfo->oi_osfs);
977 rc = lov_prep_statfs_set(obd, oinfo, &set);
981 list_for_each_entry(req, &set->set_list, rq_link) {
982 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
983 &req->rq_oi, max_age, rqset);
988 if (rc || list_empty(&rqset->set_requests)) {
992 atomic_set(&set->set_completes, 0);
993 err = lov_fini_statfs_set(set);
994 return rc ? rc : err;
997 LASSERT(!rqset->set_interpret);
998 rqset->set_interpret = lov_statfs_interpret;
999 rqset->set_arg = (void *)set;
1003 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1004 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1006 struct ptlrpc_request_set *set = NULL;
1007 struct obd_info oinfo = {
1013 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1016 set = ptlrpc_prep_set();
1020 rc = lov_statfs_async(exp, &oinfo, max_age, set);
1022 rc = ptlrpc_set_wait(set);
1023 ptlrpc_set_destroy(set);
1028 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1029 void *karg, void __user *uarg)
1031 struct obd_device *obddev = class_exp2obd(exp);
1032 struct lov_obd *lov = &obddev->u.lov;
1033 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1034 struct obd_uuid *uuidp;
1037 case IOC_OBD_STATFS: {
1038 struct obd_ioctl_data *data = karg;
1039 struct obd_device *osc_obd;
1040 struct obd_statfs stat_buf = {0};
1044 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1048 if (!lov->lov_tgts[index])
1049 /* Try again with the next index */
1051 if (!lov->lov_tgts[index]->ltd_active)
1054 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1059 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1060 min_t(unsigned long, data->ioc_plen2,
1061 sizeof(struct obd_uuid))))
1064 memcpy(&flags, data->ioc_inlbuf1, sizeof(__u32));
1065 flags = flags & LL_STATFS_NODELAY ? OBD_STATFS_NODELAY : 0;
1067 /* got statfs data */
1068 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1069 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1073 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1074 min_t(unsigned long, data->ioc_plen1,
1079 case OBD_IOC_LOV_GET_CONFIG: {
1080 struct obd_ioctl_data *data;
1081 struct lov_desc *desc;
1086 if (obd_ioctl_getdata(&buf, &len, uarg))
1089 data = (struct obd_ioctl_data *)buf;
1091 if (sizeof(*desc) > data->ioc_inllen1) {
1092 obd_ioctl_freedata(buf, len);
1096 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1097 obd_ioctl_freedata(buf, len);
1101 if (sizeof(__u32) * count > data->ioc_inllen3) {
1102 obd_ioctl_freedata(buf, len);
1106 desc = (struct lov_desc *)data->ioc_inlbuf1;
1107 memcpy(desc, &lov->desc, sizeof(*desc));
1109 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1110 genp = (__u32 *)data->ioc_inlbuf3;
1111 /* the uuid will be empty for deleted OSTs */
1112 for (i = 0; i < count; i++, uuidp++, genp++) {
1113 if (!lov->lov_tgts[i])
1115 *uuidp = lov->lov_tgts[i]->ltd_uuid;
1116 *genp = lov->lov_tgts[i]->ltd_gen;
1119 if (copy_to_user(uarg, buf, len))
1121 obd_ioctl_freedata(buf, len);
1124 case OBD_IOC_QUOTACTL: {
1125 struct if_quotactl *qctl = karg;
1126 struct lov_tgt_desc *tgt = NULL;
1127 struct obd_quotactl *oqctl;
1129 if (qctl->qc_valid == QC_OSTIDX) {
1130 if (count <= qctl->qc_idx)
1133 tgt = lov->lov_tgts[qctl->qc_idx];
1134 if (!tgt || !tgt->ltd_exp)
1136 } else if (qctl->qc_valid == QC_UUID) {
1137 for (i = 0; i < count; i++) {
1138 tgt = lov->lov_tgts[i];
1140 !obd_uuid_equals(&tgt->ltd_uuid,
1156 LASSERT(tgt && tgt->ltd_exp);
1157 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
1161 QCTL_COPY(oqctl, qctl);
1162 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1164 QCTL_COPY(qctl, oqctl);
1165 qctl->qc_valid = QC_OSTIDX;
1166 qctl->obd_uuid = tgt->ltd_uuid;
1177 for (i = 0; i < count; i++) {
1179 struct obd_device *osc_obd;
1181 /* OST was disconnected */
1182 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1185 /* ll_umount_begin() sets force flag but for lov, not
1186 * osc. Let's pass it through
1188 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1189 osc_obd->obd_force = obddev->obd_force;
1190 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1193 if (lov->lov_tgts[i]->ltd_active) {
1194 CDEBUG(err == -ENOTTY ?
1195 D_IOCTL : D_WARNING,
1196 "iocontrol OSC %s on OST idx %d cmd %x: err = %d\n",
1197 lov_uuid2str(lov, i),
1214 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1215 __u32 keylen, void *key, __u32 *vallen, void *val)
1217 struct obd_device *obddev = class_exp2obd(exp);
1218 struct lov_obd *lov = &obddev->u.lov;
1219 struct lov_desc *ld = &lov->desc;
1222 if (!vallen || !val)
1227 if (KEY_IS(KEY_MAX_EASIZE)) {
1228 u32 max_stripe_count = min_t(u32, ld->ld_active_tgt_count,
1229 LOV_MAX_STRIPE_COUNT);
1231 *((u32 *)val) = lov_mds_md_size(max_stripe_count, LOV_MAGIC_V3);
1232 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
1233 u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
1234 LOV_MAX_STRIPE_COUNT);
1236 *((u32 *)val) = lov_mds_md_size(def_stripe_count, LOV_MAGIC_V3);
1237 } else if (KEY_IS(KEY_TGT_COUNT)) {
1238 *((int *)val) = lov->desc.ld_tgt_count;
1247 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1248 u32 keylen, void *key, u32 vallen,
1249 void *val, struct ptlrpc_request_set *set)
1251 struct obd_device *obddev = class_exp2obd(exp);
1252 struct lov_obd *lov = &obddev->u.lov;
1255 struct lov_tgt_desc *tgt;
1256 int do_inactive = 0, no_set = 0;
1260 set = ptlrpc_prep_set();
1266 count = lov->desc.ld_tgt_count;
1268 if (KEY_IS(KEY_CHECKSUM)) {
1270 } else if (KEY_IS(KEY_CACHE_SET)) {
1271 LASSERT(!lov->lov_cache);
1272 lov->lov_cache = val;
1274 cl_cache_incref(lov->lov_cache);
1277 for (i = 0; i < count; i++) {
1278 tgt = lov->lov_tgts[i];
1280 /* OST was disconnected */
1281 if (!tgt || !tgt->ltd_exp)
1284 /* OST is inactive and we don't want inactive OSCs */
1285 if (!tgt->ltd_active && !do_inactive)
1288 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1296 err = ptlrpc_set_wait(set);
1299 ptlrpc_set_destroy(set);
1304 void lov_stripe_lock(struct lov_stripe_md *md)
1305 __acquires(&md->lsm_lock)
1307 LASSERT(md->lsm_lock_owner != current_pid());
1308 spin_lock(&md->lsm_lock);
1309 LASSERT(md->lsm_lock_owner == 0);
1310 md->lsm_lock_owner = current_pid();
1313 void lov_stripe_unlock(struct lov_stripe_md *md)
1314 __releases(&md->lsm_lock)
1316 LASSERT(md->lsm_lock_owner == current_pid());
1317 md->lsm_lock_owner = 0;
1318 spin_unlock(&md->lsm_lock);
1321 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1322 struct obd_quotactl *oqctl)
1324 struct lov_obd *lov = &obd->u.lov;
1325 struct lov_tgt_desc *tgt;
1327 __u64 bhardlimit = 0;
1330 if (oqctl->qc_cmd != Q_GETOQUOTA &&
1331 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA) {
1332 CERROR("bad quota opc %x for lov obd\n", oqctl->qc_cmd);
1338 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1341 tgt = lov->lov_tgts[i];
1346 if (!tgt->ltd_active || tgt->ltd_reap) {
1347 if (oqctl->qc_cmd == Q_GETOQUOTA &&
1348 lov->lov_tgts[i]->ltd_activate) {
1350 CERROR("ost %d is inactive\n", i);
1352 CDEBUG(D_HA, "ost %d is inactive\n", i);
1357 err = obd_quotactl(tgt->ltd_exp, oqctl);
1359 if (tgt->ltd_active && !rc)
1364 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1365 curspace += oqctl->qc_dqblk.dqb_curspace;
1366 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
1371 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1372 oqctl->qc_dqblk.dqb_curspace = curspace;
1373 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
1378 static struct obd_ops lov_obd_ops = {
1379 .owner = THIS_MODULE,
1381 .cleanup = lov_cleanup,
1382 /*.process_config = lov_process_config,*/
1383 .connect = lov_connect,
1384 .disconnect = lov_disconnect,
1385 .statfs = lov_statfs,
1386 .statfs_async = lov_statfs_async,
1387 .iocontrol = lov_iocontrol,
1388 .get_info = lov_get_info,
1389 .set_info_async = lov_set_info_async,
1390 .notify = lov_notify,
1391 .pool_new = lov_pool_new,
1392 .pool_rem = lov_pool_remove,
1393 .pool_add = lov_pool_add,
1394 .pool_del = lov_pool_del,
1395 .getref = lov_getref,
1396 .putref = lov_putref,
1397 .quotactl = lov_quotactl,
1400 struct kmem_cache *lov_oinfo_slab;
1402 static int __init lov_init(void)
1404 struct lprocfs_static_vars lvars = { NULL };
1407 /* print an address of _any_ initialized kernel symbol from this
1408 * module, to allow debugging with gdb that doesn't support data
1409 * symbols from modules.
1411 CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
1413 rc = lu_kmem_init(lov_caches);
1417 lov_oinfo_slab = kmem_cache_create("lov_oinfo",
1418 sizeof(struct lov_oinfo),
1419 0, SLAB_HWCACHE_ALIGN, NULL);
1420 if (!lov_oinfo_slab) {
1421 lu_kmem_fini(lov_caches);
1424 lprocfs_lov_init_vars(&lvars);
1426 rc = class_register_type(&lov_obd_ops, NULL,
1427 LUSTRE_LOV_NAME, &lov_device_type);
1430 kmem_cache_destroy(lov_oinfo_slab);
1431 lu_kmem_fini(lov_caches);
1437 static void /*__exit*/ lov_exit(void)
1439 class_unregister_type(LUSTRE_LOV_NAME);
1440 kmem_cache_destroy(lov_oinfo_slab);
1442 lu_kmem_fini(lov_caches);
1445 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1446 MODULE_DESCRIPTION("Lustre Logical Object Volume");
1447 MODULE_LICENSE("GPL");
1448 MODULE_VERSION(LUSTRE_VERSION_STRING);
1450 module_init(lov_init);
1451 module_exit(lov_exit);