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, 2013, 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>
59 #include <lustre/ll_fiemap.h>
60 #include <lustre_fid.h>
62 #include "lov_internal.h"
64 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
65 Any function that expects lov_tgts to remain stationary must take a ref. */
66 static void lov_getref(struct obd_device *obd)
68 struct lov_obd *lov = &obd->u.lov;
70 /* nobody gets through here until lov_putref is done */
71 mutex_lock(&lov->lov_lock);
72 atomic_inc(&lov->lov_refcount);
73 mutex_unlock(&lov->lov_lock);
77 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
79 static void lov_putref(struct obd_device *obd)
81 struct lov_obd *lov = &obd->u.lov;
83 mutex_lock(&lov->lov_lock);
84 /* ok to dec to 0 more than once -- ltd_exp's will be null */
85 if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
86 struct list_head kill = LIST_HEAD_INIT(kill);
87 struct lov_tgt_desc *tgt, *n;
90 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
92 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
93 tgt = lov->lov_tgts[i];
95 if (!tgt || !tgt->ltd_reap)
97 list_add(&tgt->ltd_kill, &kill);
98 /* XXX - right now there is a dependency on ld_tgt_count
99 * being the maximum tgt index for computing the
100 * mds_max_easize. So we can't shrink it. */
101 lov_ost_pool_remove(&lov->lov_packed, i);
102 lov->lov_tgts[i] = NULL;
103 lov->lov_death_row--;
105 mutex_unlock(&lov->lov_lock);
107 list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
108 list_del(&tgt->ltd_kill);
110 __lov_del_obd(obd, tgt);
113 mutex_unlock(&lov->lov_lock);
117 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
118 enum obd_notify_event ev);
119 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
120 enum obd_notify_event ev, void *data);
122 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
123 struct obd_connect_data *data)
125 struct lov_obd *lov = &obd->u.lov;
126 struct obd_uuid *tgt_uuid;
127 struct obd_device *tgt_obd;
128 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
129 struct obd_import *imp;
133 if (lov->lov_tgts[index] == NULL)
136 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
137 tgt_obd = lov->lov_tgts[index]->ltd_obd;
139 if (!tgt_obd->obd_set_up) {
140 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
144 /* override the sp_me from lov */
145 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
147 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
148 data->ocd_index = index;
151 * Divine LOV knows that OBDs under it are OSCs.
153 imp = tgt_obd->u.cli.cl_import;
156 tgt_obd->obd_no_recov = 0;
157 /* FIXME this is probably supposed to be
158 ptlrpc_set_import_active. Horrible naming. */
159 ptlrpc_activate_import(imp);
162 rc = obd_register_observer(tgt_obd, obd);
164 CERROR("Target %s register_observer error %d\n",
165 obd_uuid2str(tgt_uuid), rc);
170 if (imp->imp_invalid) {
171 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
172 "disabled\n", obd_uuid2str(tgt_uuid));
176 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
177 &lov_osc_uuid, data, NULL);
178 if (rc || !lov->lov_tgts[index]->ltd_exp) {
179 CERROR("Target %s connect error %d\n",
180 obd_uuid2str(tgt_uuid), rc);
184 lov->lov_tgts[index]->ltd_reap = 0;
186 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
187 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
189 if (lov->targets_proc_entry != NULL) {
190 struct proc_dir_entry *osc_symlink;
191 struct obd_device *osc_obd;
193 osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
195 LASSERT(osc_obd != NULL);
196 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
197 LASSERT(osc_obd->obd_type->typ_name != NULL);
199 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
200 lov->targets_proc_entry,
202 osc_obd->obd_type->typ_name,
204 if (osc_symlink == NULL) {
205 CERROR("cannot register LOV target "
206 "/proc/fs/lustre/%s/%s/target_obds/%s\n",
207 obd->obd_type->typ_name, obd->obd_name,
214 static int lov_connect(const struct lu_env *env,
215 struct obd_export **exp, struct obd_device *obd,
216 struct obd_uuid *cluuid, struct obd_connect_data *data,
219 struct lov_obd *lov = &obd->u.lov;
220 struct lov_tgt_desc *tgt;
221 struct lustre_handle conn;
225 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
227 rc = class_connect(&conn, obd, cluuid);
231 *exp = class_conn2export(&conn);
233 /* Why should there ever be more than 1 connect? */
235 LASSERT(lov->lov_connects == 1);
237 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
239 lov->lov_ocd = *data;
241 lov->targets_proc_entry = lprocfs_seq_register("target_obds",
244 if (IS_ERR(lov->targets_proc_entry)) {
245 CERROR("%s: cannot register "
246 "/proc/fs/lustre/%s/%s/target_obds\n",
247 obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
248 lov->targets_proc_entry = NULL;
252 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
253 tgt = lov->lov_tgts[i];
254 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
256 /* Flags will be lowest common denominator */
257 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
259 CERROR("%s: lov connect tgt %d failed: %d\n",
260 obd->obd_name, i, rc);
263 /* connect to administrative disabled ost */
264 if (!lov->lov_tgts[i]->ltd_exp)
267 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
268 OBD_NOTIFY_CONNECT, (void *)&i);
270 CERROR("%s error sending notify %d\n",
279 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
281 struct lov_obd *lov = &obd->u.lov;
282 struct obd_device *osc_obd;
286 osc_obd = class_exp2obd(tgt->ltd_exp);
287 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
288 obd->obd_name, osc_obd->obd_name);
290 if (tgt->ltd_active) {
292 lov->desc.ld_active_tgt_count--;
293 tgt->ltd_exp->exp_obd->obd_inactive = 1;
297 /* Pass it on to our clients.
298 * XXX This should be an argument to disconnect,
299 * XXX not a back-door flag on the OBD. Ah well.
301 osc_obd->obd_force = obd->obd_force;
302 osc_obd->obd_fail = obd->obd_fail;
303 osc_obd->obd_no_recov = obd->obd_no_recov;
305 if (lov->targets_proc_entry != NULL)
306 lprocfs_remove_proc_entry(osc_obd->obd_name,
307 lov->targets_proc_entry);
310 obd_register_observer(osc_obd, NULL);
312 rc = obd_disconnect(tgt->ltd_exp);
314 CERROR("Target %s disconnect error %d\n",
315 tgt->ltd_uuid.uuid, rc);
323 static int lov_disconnect(struct obd_export *exp)
325 struct obd_device *obd = class_exp2obd(exp);
326 struct lov_obd *lov = &obd->u.lov;
333 /* Only disconnect the underlying layers on the final disconnect. */
335 if (lov->lov_connects != 0) {
336 /* why should there be more than 1 connect? */
337 CERROR("disconnect #%d\n", lov->lov_connects);
341 /* Let's hold another reference so lov_del_obd doesn't spin through
345 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
346 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
347 /* Disconnection is the last we know about an obd */
348 lov_del_target(obd, i, 0, lov->lov_tgts[i]->ltd_gen);
353 if (lov->targets_proc_entry != NULL)
354 lprocfs_remove(&lov->targets_proc_entry);
357 rc = class_disconnect(exp); /* bz 9811 */
363 * -EINVAL : UUID can't be found in the LOV's target list
364 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
365 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
366 * any >= 0 : is log target index
368 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
369 enum obd_notify_event ev)
371 struct lov_obd *lov = &obd->u.lov;
372 struct lov_tgt_desc *tgt;
373 int index, activate, active;
376 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
377 lov, uuid->uuid, ev);
380 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
381 tgt = lov->lov_tgts[index];
385 * LU-642, initially inactive OSC could miss the obd_connect,
386 * we make up for it here.
388 if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
389 obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
390 struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
392 obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
393 &lov_osc_uuid, &lov->lov_ocd, NULL);
398 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
399 index, obd_uuid2str(&tgt->ltd_uuid),
400 tgt->ltd_exp->exp_handle.h_cookie);
401 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
405 if (index == lov->desc.ld_tgt_count)
406 GOTO(out, index = -EINVAL);
408 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
409 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
411 if (lov->lov_tgts[index]->ltd_activate == activate) {
412 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
413 uuid->uuid, activate ? "" : "de");
415 lov->lov_tgts[index]->ltd_activate = activate;
416 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
417 activate ? "" : "de", obd_uuid2str(uuid));
420 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
421 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
423 if (lov->lov_tgts[index]->ltd_active == active) {
424 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
425 uuid->uuid, active ? "" : "in");
428 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
429 obd_uuid2str(uuid), active ? "" : "in");
432 lov->lov_tgts[index]->ltd_active = active;
434 lov->desc.ld_active_tgt_count++;
435 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
437 lov->desc.ld_active_tgt_count--;
438 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
441 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
450 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
451 enum obd_notify_event ev, void *data)
454 struct lov_obd *lov = &obd->u.lov;
457 down_read(&lov->lov_notify_lock);
458 if (!lov->lov_connects) {
459 up_read(&lov->lov_notify_lock);
463 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
464 ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
465 struct obd_uuid *uuid;
469 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
470 up_read(&lov->lov_notify_lock);
471 CERROR("unexpected notification of %s %s!\n",
472 watched->obd_type->typ_name,
476 uuid = &watched->u.cli.cl_target_uuid;
478 /* Set OSC as active before notifying the observer, so the
479 * observer can use the OSC normally.
481 rc = lov_set_osc_active(obd, uuid, ev);
483 up_read(&lov->lov_notify_lock);
484 CERROR("event(%d) of %s failed: %d\n", ev,
485 obd_uuid2str(uuid), rc);
488 /* active event should be pass lov target index as data */
492 /* Pass the notification up the chain. */
494 rc = obd_notify_observer(obd, watched, ev, data);
496 /* NULL watched means all osc's in the lov (only for syncs) */
497 /* sync event should be send lov idx as data */
498 struct lov_obd *lov = &obd->u.lov;
502 is_sync = (ev == OBD_NOTIFY_SYNC) ||
503 (ev == OBD_NOTIFY_SYNC_NONBLOCK);
506 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
507 if (!lov->lov_tgts[i])
510 /* don't send sync event if target not
511 * connected/activated */
512 if (is_sync && !lov->lov_tgts[i]->ltd_active)
515 rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
518 CERROR("%s: notify %s of %s failed %d\n",
520 obd->obd_observer->obd_name,
521 lov->lov_tgts[i]->ltd_obd->obd_name,
528 up_read(&lov->lov_notify_lock);
532 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
533 __u32 index, int gen, int active)
535 struct lov_obd *lov = &obd->u.lov;
536 struct lov_tgt_desc *tgt;
537 struct obd_device *tgt_obd;
541 CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
542 uuidp->uuid, index, gen, active);
545 CERROR("request to add OBD %s with invalid generation: %d\n",
550 tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
555 mutex_lock(&lov->lov_lock);
557 if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
558 tgt = lov->lov_tgts[index];
559 CERROR("UUID %s already assigned at LOV target index %d\n",
560 obd_uuid2str(&tgt->ltd_uuid), index);
561 mutex_unlock(&lov->lov_lock);
565 if (index >= lov->lov_tgt_size) {
566 /* We need to reallocate the lov target array. */
567 struct lov_tgt_desc **newtgts, **old = NULL;
568 __u32 newsize, oldsize = 0;
570 newsize = max(lov->lov_tgt_size, (__u32)2);
571 while (newsize < index + 1)
572 newsize = newsize << 1;
573 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
574 if (newtgts == NULL) {
575 mutex_unlock(&lov->lov_lock);
579 if (lov->lov_tgt_size) {
580 memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
583 oldsize = lov->lov_tgt_size;
586 lov->lov_tgts = newtgts;
587 lov->lov_tgt_size = newsize;
590 OBD_FREE(old, sizeof(*old) * oldsize);
592 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
593 lov->lov_tgts, lov->lov_tgt_size);
598 mutex_unlock(&lov->lov_lock);
602 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
604 mutex_unlock(&lov->lov_lock);
609 tgt->ltd_uuid = *uuidp;
610 tgt->ltd_obd = tgt_obd;
611 /* XXX - add a sanity check on the generation number. */
613 tgt->ltd_index = index;
614 tgt->ltd_activate = active;
615 lov->lov_tgts[index] = tgt;
616 if (index >= lov->desc.ld_tgt_count)
617 lov->desc.ld_tgt_count = index + 1;
619 mutex_unlock(&lov->lov_lock);
621 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
622 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
624 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
626 if (lov->lov_connects == 0) {
627 /* lov_connect hasn't been called yet. We'll do the
628 lov_connect_obd on this target when that fn first runs,
629 because we don't know the connect flags yet. */
635 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
639 /* connect to administrative disabled ost */
643 if (lov->lov_cache != NULL) {
644 rc = obd_set_info_async(NULL, tgt->ltd_exp,
645 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
646 sizeof(struct cl_client_cache), lov->lov_cache,
652 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
653 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
658 CERROR("add failed (%d), deleting %s\n", rc,
659 obd_uuid2str(&tgt->ltd_uuid));
660 lov_del_target(obd, index, 0, 0);
666 /* Schedule a target for deletion */
667 int lov_del_target(struct obd_device *obd, __u32 index,
668 struct obd_uuid *uuidp, int gen)
670 struct lov_obd *lov = &obd->u.lov;
671 int count = lov->desc.ld_tgt_count;
675 if (index >= count) {
676 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
681 /* to make sure there's no ongoing lov_notify() now */
682 down_write(&lov->lov_notify_lock);
685 if (!lov->lov_tgts[index]) {
686 CERROR("LOV target at index %d is not setup.\n", index);
687 GOTO(out, rc = -EINVAL);
690 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
691 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
692 lov_uuid2str(lov, index), index,
693 obd_uuid2str(uuidp));
694 GOTO(out, rc = -EINVAL);
697 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
698 lov_uuid2str(lov, index), index,
699 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
700 lov->lov_tgts[index]->ltd_active);
702 lov->lov_tgts[index]->ltd_reap = 1;
703 lov->lov_death_row++;
704 /* we really delete it from obd_putref */
707 up_write(&lov->lov_notify_lock);
712 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
714 struct obd_device *osc_obd;
717 LASSERT(tgt->ltd_reap);
719 osc_obd = class_exp2obd(tgt->ltd_exp);
721 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
723 osc_obd ? osc_obd->obd_name : "<no obd>");
726 lov_disconnect_obd(obd, tgt);
730 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
731 do it ourselves. And we can't do it from lov_cleanup,
732 because we just lost our only reference to it. */
734 class_manual_cleanup(osc_obd);
737 void lov_fix_desc_stripe_size(__u64 *val)
739 if (*val < LOV_MIN_STRIPE_SIZE) {
741 LCONSOLE_INFO("Increasing default stripe size to "
743 LOV_DESC_STRIPE_SIZE_DEFAULT);
744 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
745 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
746 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
747 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
749 *val, LOV_MIN_STRIPE_SIZE);
753 void lov_fix_desc_stripe_count(__u32 *val)
759 void lov_fix_desc_pattern(__u32 *val)
761 /* from lov_setstripe */
762 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
763 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
768 void lov_fix_desc_qos_maxage(__u32 *val)
771 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
774 void lov_fix_desc(struct lov_desc *desc)
776 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
777 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
778 lov_fix_desc_pattern(&desc->ld_pattern);
779 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
782 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
784 struct lov_desc *desc;
785 struct lov_obd *lov = &obd->u.lov;
787 struct obd_type *type;
792 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
793 CERROR("LOV setup requires a descriptor\n");
797 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
799 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
800 CERROR("descriptor size wrong: %d > %d\n",
801 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
805 if (desc->ld_magic != LOV_DESC_MAGIC) {
806 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
807 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
808 obd->obd_name, desc);
809 lustre_swab_lov_desc(desc);
811 CERROR("%s: Bad lov desc magic: %#x\n",
812 obd->obd_name, desc->ld_magic);
819 desc->ld_active_tgt_count = 0;
821 lov->lov_tgt_size = 0;
823 mutex_init(&lov->lov_lock);
824 atomic_set(&lov->lov_refcount, 0);
825 lov->lov_sp_me = LUSTRE_SP_CLI;
827 init_rwsem(&lov->lov_notify_lock);
829 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
831 HASH_POOLS_BKT_BITS, 0,
834 &pool_hash_operations,
836 INIT_LIST_HEAD(&lov->lov_pool_list);
837 lov->lov_pool_count = 0;
838 rc = lov_ost_pool_init(&lov->lov_packed, 0);
843 obd->obd_vars = lprocfs_lov_obd_vars;
844 /* If this is true then both client (lov) and server
845 * (lod) are on the same node. The lod layer if loaded
846 * first will register the lov proc directory. In that
847 * case obd->obd_type->typ_procroot will be not set.
848 * Instead we use type->typ_procsym as the parent. */
849 type = class_search_type(LUSTRE_LOD_NAME);
850 if (type != NULL && type->typ_procsym != NULL) {
851 obd->obd_proc_entry = lprocfs_seq_register(obd->obd_name,
854 if (IS_ERR(obd->obd_proc_entry)) {
855 rc = PTR_ERR(obd->obd_proc_entry);
856 CERROR("error %d setting up lprocfs for %s\n", rc,
858 obd->obd_proc_entry = NULL;
861 rc = lprocfs_obd_setup(obd);
865 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
866 0444, &lov_proc_target_fops, obd);
868 CWARN("Error adding the target_obd file\n");
870 lov->lov_pool_proc_entry = lprocfs_seq_register("pools",
873 if (IS_ERR(lov->lov_pool_proc_entry)) {
874 rc = PTR_ERR(lov->lov_pool_proc_entry);
875 CERROR("error %d setting up lprocfs for pools\n", rc);
876 lov->lov_pool_proc_entry = NULL;
886 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
889 struct lov_obd *lov = &obd->u.lov;
894 case OBD_CLEANUP_EARLY: {
896 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
897 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
899 obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
911 static int lov_cleanup(struct obd_device *obd)
913 struct lov_obd *lov = &obd->u.lov;
914 struct list_head *pos, *tmp;
915 struct pool_desc *pool;
918 list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
919 pool = list_entry(pos, struct pool_desc, pool_list);
920 /* free pool structs */
921 CDEBUG(D_INFO, "delete pool %p\n", pool);
922 /* In the function below, .hs_keycmp resolves to
923 * pool_hashkey_keycmp() */
924 /* coverity[overrun-buffer-val] */
925 lov_pool_del(obd, pool->pool_name);
927 cfs_hash_putref(lov->lov_pools_hash_body);
928 lov_ost_pool_free(&lov->lov_packed);
930 lprocfs_obd_cleanup(obd);
934 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
935 if (!lov->lov_tgts[i])
938 /* Inactive targets may never have connected */
939 if (lov->lov_tgts[i]->ltd_active ||
940 atomic_read(&lov->lov_refcount))
941 /* We should never get here - these
942 * should have been removed in the
944 CERROR("%s: lov tgt %d not cleaned! "
945 "deathrow=%d, lovrc=%d\n",
946 obd->obd_name, i, lov->lov_death_row,
947 atomic_read(&lov->lov_refcount));
948 lov_del_target(obd, i, 0, 0);
951 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
953 lov->lov_tgt_size = 0;
958 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
959 __u32 *indexp, int *genp)
961 struct obd_uuid obd_uuid;
966 switch(cmd = lcfg->lcfg_command) {
967 case LCFG_LOV_ADD_OBD:
968 case LCFG_LOV_ADD_INA:
969 case LCFG_LOV_DEL_OBD: {
972 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
973 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
974 GOTO(out, rc = -EINVAL);
976 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
978 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", indexp) != 1)
979 GOTO(out, rc = -EINVAL);
980 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
981 GOTO(out, rc = -EINVAL);
984 if (cmd == LCFG_LOV_ADD_OBD)
985 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
986 else if (cmd == LCFG_LOV_ADD_INA)
987 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
989 rc = lov_del_target(obd, index, &obd_uuid, gen);
993 struct lov_desc *desc = &(obd->u.lov.desc);
996 GOTO(out, rc = -EINVAL);
998 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
1011 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1012 GOTO(out, rc = -EINVAL);
1020 #define ASSERT_LSM_MAGIC(lsmp) \
1022 LASSERT((lsmp) != NULL); \
1023 LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 || \
1024 (lsmp)->lsm_magic == LOV_MAGIC_V3), \
1025 "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic); \
1028 static int lov_destroy(const struct lu_env *env, struct obd_export *exp,
1029 struct obdo *oa, struct lov_stripe_md *lsm,
1030 struct obd_trans_info *oti, struct obd_export *md_exp,
1033 struct lov_request_set *set;
1034 struct obd_info oinfo;
1035 struct lov_request *req;
1036 struct list_head *pos;
1037 struct lov_obd *lov;
1038 int rc = 0, err = 0;
1041 ASSERT_LSM_MAGIC(lsm);
1043 if (!exp || !exp->exp_obd)
1046 if (oa->o_valid & OBD_MD_FLCOOKIE) {
1048 LASSERT(oti->oti_logcookies);
1051 lov = &exp->exp_obd->u.lov;
1052 obd_getref(exp->exp_obd);
1053 rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1057 list_for_each(pos, &set->set_list) {
1058 req = list_entry(pos, struct lov_request, rq_link);
1060 if (oa->o_valid & OBD_MD_FLCOOKIE)
1061 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1063 err = obd_destroy(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1064 req->rq_oi.oi_oa, NULL, oti, NULL, capa);
1065 err = lov_update_common_set(set, req, err);
1067 CERROR("%s: destroying objid "DOSTID" subobj "
1068 DOSTID" on OST idx %d: rc = %d\n",
1069 exp->exp_obd->obd_name, POSTID(&oa->o_oi),
1070 POSTID(&req->rq_oi.oi_oa->o_oi),
1078 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1079 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1081 err = lov_fini_destroy_set(set);
1083 obd_putref(exp->exp_obd);
1084 RETURN(rc ? rc : err);
1087 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1090 struct lov_request_set *lovset = (struct lov_request_set *)data;
1094 /* don't do attribute merge if this aysnc op failed */
1096 atomic_set(&lovset->set_completes, 0);
1097 err = lov_fini_getattr_set(lovset);
1098 RETURN(rc ? rc : err);
1101 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1102 struct ptlrpc_request_set *rqset)
1104 struct lov_request_set *lovset;
1105 struct lov_obd *lov;
1106 struct list_head *pos;
1107 struct lov_request *req;
1112 ASSERT_LSM_MAGIC(oinfo->oi_md);
1114 if (!exp || !exp->exp_obd)
1117 lov = &exp->exp_obd->u.lov;
1119 rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1123 CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1124 POSTID(&oinfo->oi_md->lsm_oi), oinfo->oi_md->lsm_stripe_count,
1125 oinfo->oi_md->lsm_stripe_size);
1127 list_for_each(pos, &lovset->set_list) {
1128 req = list_entry(pos, struct lov_request, rq_link);
1130 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1131 "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1132 POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1133 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1134 &req->rq_oi, rqset);
1136 CERROR("%s: getattr objid "DOSTID" subobj"
1137 DOSTID" on OST idx %d: rc = %d\n",
1138 exp->exp_obd->obd_name,
1139 POSTID(&oinfo->oi_oa->o_oi),
1140 POSTID(&req->rq_oi.oi_oa->o_oi),
1146 if (!list_empty(&rqset->set_requests)) {
1148 LASSERT (rqset->set_interpret == NULL);
1149 rqset->set_interpret = lov_getattr_interpret;
1150 rqset->set_arg = (void *)lovset;
1155 atomic_set(&lovset->set_completes, 0);
1156 err = lov_fini_getattr_set(lovset);
1157 RETURN(rc ? rc : err);
1160 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1163 struct lov_request_set *lovset = (struct lov_request_set *)data;
1168 atomic_set(&lovset->set_completes, 0);
1169 err = lov_fini_setattr_set(lovset);
1170 RETURN(rc ? rc : err);
1173 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1174 needed. Otherwise, a client is waiting for responses. */
1175 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1176 struct obd_trans_info *oti,
1177 struct ptlrpc_request_set *rqset)
1179 struct lov_request_set *set;
1180 struct lov_request *req;
1181 struct list_head *pos;
1182 struct lov_obd *lov;
1187 ASSERT_LSM_MAGIC(oinfo->oi_md);
1188 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1190 LASSERT(oti->oti_logcookies);
1193 if (!exp || !exp->exp_obd)
1196 lov = &exp->exp_obd->u.lov;
1197 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1201 CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1202 POSTID(&oinfo->oi_md->lsm_oi),
1203 oinfo->oi_md->lsm_stripe_count,
1204 oinfo->oi_md->lsm_stripe_size);
1206 list_for_each(pos, &set->set_list) {
1207 req = list_entry(pos, struct lov_request, rq_link);
1209 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1210 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1212 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1213 "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1214 POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1216 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1217 &req->rq_oi, oti, rqset);
1219 CERROR("error: setattr objid "DOSTID" subobj"
1220 DOSTID" on OST idx %d: rc = %d\n",
1221 POSTID(&set->set_oi->oi_oa->o_oi),
1222 POSTID(&req->rq_oi.oi_oa->o_oi),
1228 /* If we are not waiting for responses on async requests, return. */
1229 if (rc || !rqset || list_empty(&rqset->set_requests)) {
1232 atomic_set(&set->set_completes, 0);
1233 err = lov_fini_setattr_set(set);
1234 RETURN(rc ? rc : err);
1237 LASSERT(rqset->set_interpret == NULL);
1238 rqset->set_interpret = lov_setattr_interpret;
1239 rqset->set_arg = (void *)set;
1244 static int lov_change_cbdata(struct obd_export *exp,
1245 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1248 struct lov_obd *lov;
1252 ASSERT_LSM_MAGIC(lsm);
1254 if (!exp || !exp->exp_obd)
1257 lov = &exp->exp_obd->u.lov;
1258 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1259 struct lov_stripe_md submd;
1260 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1262 if (lov_oinfo_is_dummy(loi))
1265 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1266 CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1270 submd.lsm_oi = loi->loi_oi;
1271 submd.lsm_stripe_count = 0;
1272 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1278 /* find any ldlm lock of the inode in lov
1282 static int lov_find_cbdata(struct obd_export *exp,
1283 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1286 struct lov_obd *lov;
1290 ASSERT_LSM_MAGIC(lsm);
1292 if (!exp || !exp->exp_obd)
1295 lov = &exp->exp_obd->u.lov;
1296 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1297 struct lov_stripe_md submd;
1298 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1300 if (lov_oinfo_is_dummy(loi))
1303 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1304 CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1308 submd.lsm_oi = loi->loi_oi;
1309 submd.lsm_stripe_count = 0;
1310 rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1318 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1320 struct lov_request_set *lovset = (struct lov_request_set *)data;
1325 atomic_set(&lovset->set_completes, 0);
1327 err = lov_fini_statfs_set(lovset);
1328 RETURN(rc ? rc : err);
1331 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1332 __u64 max_age, struct ptlrpc_request_set *rqset)
1334 struct obd_device *obd = class_exp2obd(exp);
1335 struct lov_request_set *set;
1336 struct lov_request *req;
1337 struct list_head *pos;
1338 struct lov_obd *lov;
1342 LASSERT(oinfo != NULL);
1343 LASSERT(oinfo->oi_osfs != NULL);
1346 rc = lov_prep_statfs_set(obd, oinfo, &set);
1350 list_for_each(pos, &set->set_list) {
1351 req = list_entry(pos, struct lov_request, rq_link);
1352 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1353 &req->rq_oi, max_age, rqset);
1358 if (rc || list_empty(&rqset->set_requests)) {
1361 atomic_set(&set->set_completes, 0);
1362 err = lov_fini_statfs_set(set);
1363 RETURN(rc ? rc : err);
1366 LASSERT(rqset->set_interpret == NULL);
1367 rqset->set_interpret = lov_statfs_interpret;
1368 rqset->set_arg = (void *)set;
1372 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1373 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1375 struct ptlrpc_request_set *set = NULL;
1376 struct obd_info oinfo = { { { 0 } } };
1381 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1382 * statfs requests */
1383 set = ptlrpc_prep_set();
1387 oinfo.oi_osfs = osfs;
1388 oinfo.oi_flags = flags;
1389 rc = lov_statfs_async(exp, &oinfo, max_age, set);
1391 rc = ptlrpc_set_wait(set);
1392 ptlrpc_set_destroy(set);
1397 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1398 void *karg, void __user *uarg)
1400 struct obd_device *obddev = class_exp2obd(exp);
1401 struct lov_obd *lov = &obddev->u.lov;
1402 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1403 struct obd_uuid *uuidp;
1407 case IOC_OBD_STATFS: {
1408 struct obd_ioctl_data *data = karg;
1409 struct obd_device *osc_obd;
1410 struct obd_statfs stat_buf = {0};
1414 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1415 if ((index >= count))
1418 if (!lov->lov_tgts[index])
1419 /* Try again with the next index */
1421 if (!lov->lov_tgts[index]->ltd_active)
1424 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1429 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1430 min((int)data->ioc_plen2,
1431 (int)sizeof(struct obd_uuid))))
1434 flags = uarg ? *(__u32 __user *)uarg : 0;
1435 /* got statfs data */
1436 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1437 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1441 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1442 min((int) data->ioc_plen1,
1443 (int) sizeof(stat_buf))))
1447 case OBD_IOC_LOV_GET_CONFIG: {
1448 struct obd_ioctl_data *data;
1449 struct lov_desc *desc;
1454 if (obd_ioctl_getdata(&buf, &len, uarg))
1457 data = (struct obd_ioctl_data *)buf;
1459 if (sizeof(*desc) > data->ioc_inllen1) {
1460 obd_ioctl_freedata(buf, len);
1464 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1465 obd_ioctl_freedata(buf, len);
1469 if (sizeof(__u32) * count > data->ioc_inllen3) {
1470 obd_ioctl_freedata(buf, len);
1474 desc = (struct lov_desc *)data->ioc_inlbuf1;
1475 memcpy(desc, &(lov->desc), sizeof(*desc));
1477 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1478 genp = (__u32 *)data->ioc_inlbuf3;
1479 /* the uuid will be empty for deleted OSTs */
1480 for (i = 0; i < count; i++, uuidp++, genp++) {
1481 if (!lov->lov_tgts[i])
1483 *uuidp = lov->lov_tgts[i]->ltd_uuid;
1484 *genp = lov->lov_tgts[i]->ltd_gen;
1487 if (copy_to_user(uarg, buf, len))
1489 obd_ioctl_freedata(buf, len);
1492 case LL_IOC_LOV_GETSTRIPE:
1493 rc = lov_getstripe(exp, karg, uarg);
1495 case OBD_IOC_QUOTACTL: {
1496 struct if_quotactl *qctl = karg;
1497 struct lov_tgt_desc *tgt = NULL;
1498 struct obd_quotactl *oqctl;
1500 if (qctl->qc_valid == QC_OSTIDX) {
1501 if (count <= qctl->qc_idx)
1504 tgt = lov->lov_tgts[qctl->qc_idx];
1505 if (!tgt || !tgt->ltd_exp)
1507 } else if (qctl->qc_valid == QC_UUID) {
1508 for (i = 0; i < count; i++) {
1509 tgt = lov->lov_tgts[i];
1511 !obd_uuid_equals(&tgt->ltd_uuid,
1515 if (tgt->ltd_exp == NULL)
1527 LASSERT(tgt && tgt->ltd_exp);
1528 OBD_ALLOC_PTR(oqctl);
1532 QCTL_COPY(oqctl, qctl);
1533 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1535 QCTL_COPY(qctl, oqctl);
1536 qctl->qc_valid = QC_OSTIDX;
1537 qctl->obd_uuid = tgt->ltd_uuid;
1539 OBD_FREE_PTR(oqctl);
1548 for (i = 0; i < count; i++) {
1550 struct obd_device *osc_obd;
1552 /* OST was disconnected */
1553 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1556 /* ll_umount_begin() sets force flag but for lov, not
1557 * osc. Let's pass it through */
1558 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1559 osc_obd->obd_force = obddev->obd_force;
1560 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1562 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1565 if (lov->lov_tgts[i]->ltd_active) {
1566 CDEBUG(err == -ENOTTY ?
1567 D_IOCTL : D_WARNING,
1568 "iocontrol OSC %s on OST "
1569 "idx %d cmd %x: err = %d\n",
1570 lov_uuid2str(lov, i),
1587 #define FIEMAP_BUFFER_SIZE 4096
1590 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1591 * call. The local end offset and the device are sent in the first
1592 * fm_extent. This function calculates the stripe number from the index.
1593 * This function returns a stripe_no on which mapping is to be restarted.
1595 * This function returns fm_end_offset which is the in-OST offset at which
1596 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1597 * will re-calculate proper offset in next stripe.
1598 * Note that the first extent is passed to lov_get_info via the value field.
1600 * \param fiemap fiemap request header
1601 * \param lsm striping information for the file
1602 * \param fm_start logical start of mapping
1603 * \param fm_end logical end of mapping
1604 * \param start_stripe starting stripe will be returned in this
1606 obd_size fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
1607 struct lov_stripe_md *lsm, obd_size fm_start,
1608 obd_size fm_end, int *start_stripe)
1610 obd_size local_end = fiemap->fm_extents[0].fe_logical;
1611 obd_off lun_start, lun_end;
1612 obd_size fm_end_offset;
1613 int stripe_no = -1, i;
1615 if (fiemap->fm_extent_count == 0 ||
1616 fiemap->fm_extents[0].fe_logical == 0)
1619 /* Find out stripe_no from ost_index saved in the fe_device */
1620 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1621 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
1623 if (lov_oinfo_is_dummy(oinfo))
1626 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1632 if (stripe_no == -1)
1635 /* If we have finished mapping on previous device, shift logical
1636 * offset to start of next device */
1637 if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
1638 &lun_start, &lun_end)) != 0 &&
1639 local_end < lun_end) {
1640 fm_end_offset = local_end;
1641 *start_stripe = stripe_no;
1643 /* This is a special value to indicate that caller should
1644 * calculate offset in next stripe. */
1646 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
1649 return fm_end_offset;
1653 * We calculate on which OST the mapping will end. If the length of mapping
1654 * is greater than (stripe_size * stripe_count) then the last_stripe will
1655 * will be one just before start_stripe. Else we check if the mapping
1656 * intersects each OST and find last_stripe.
1657 * This function returns the last_stripe and also sets the stripe_count
1658 * over which the mapping is spread
1660 * \param lsm striping information for the file
1661 * \param fm_start logical start of mapping
1662 * \param fm_end logical end of mapping
1663 * \param start_stripe starting stripe of the mapping
1664 * \param stripe_count the number of stripes across which to map is returned
1666 * \retval last_stripe return the last stripe of the mapping
1668 int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, obd_size fm_start,
1669 obd_size fm_end, int start_stripe,
1673 obd_off obd_start, obd_end;
1676 if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
1677 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
1679 *stripe_count = lsm->lsm_stripe_count;
1681 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
1682 i = (i + 1) % lsm->lsm_stripe_count, j++) {
1683 if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
1684 &obd_start, &obd_end)) == 0)
1688 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
1695 * Set fe_device and copy extents from local buffer into main return buffer.
1697 * \param fiemap fiemap request header
1698 * \param lcl_fm_ext array of local fiemap extents to be copied
1699 * \param ost_index OST index to be written into the fm_device field for each
1701 * \param ext_count number of extents to be copied
1702 * \param current_extent where to start copying in main extent array
1704 void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
1705 struct ll_fiemap_extent *lcl_fm_ext,
1706 int ost_index, unsigned int ext_count,
1712 for (ext = 0; ext < ext_count; ext++) {
1713 lcl_fm_ext[ext].fe_device = ost_index;
1714 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1717 /* Copy fm_extent's from fm_local to return buffer */
1718 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1719 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
1723 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1724 * This also handles the restarting of FIEMAP calls in case mapping overflows
1725 * the available number of extents in single call.
1727 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
1728 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1730 struct ll_fiemap_info_key *fm_key = key;
1731 struct ll_user_fiemap *fiemap = val;
1732 struct ll_user_fiemap *fm_local = NULL;
1733 struct ll_fiemap_extent *lcl_fm_ext;
1735 unsigned int get_num_extents = 0;
1736 int ost_index = 0, actual_start_stripe, start_stripe;
1737 obd_size fm_start, fm_end, fm_length, fm_end_offset;
1739 int current_extent = 0, rc = 0, i;
1740 int ost_eof = 0; /* EOF for object */
1741 int ost_done = 0; /* done with required mapping for this OST? */
1743 int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
1744 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1746 if (!lsm_has_objects(lsm)) {
1747 if (lsm && lsm_is_released(lsm) && (fm_key->fiemap.fm_start <
1748 fm_key->oa.o_size)) {
1749 /* released file, return a minimal FIEMAP if
1750 * request fits in file-size.
1752 fiemap->fm_mapped_extents = 1;
1753 fiemap->fm_extents[0].fe_logical =
1754 fm_key->fiemap.fm_start;
1755 if (fm_key->fiemap.fm_start + fm_key->fiemap.fm_length <
1757 fiemap->fm_extents[0].fe_length =
1758 fm_key->fiemap.fm_length;
1760 fiemap->fm_extents[0].fe_length =
1762 fm_key->fiemap.fm_start;
1763 fiemap->fm_extents[0].fe_flags |=
1764 (FIEMAP_EXTENT_UNKNOWN |
1765 FIEMAP_EXTENT_LAST);
1770 if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
1771 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
1773 OBD_ALLOC_LARGE(fm_local, buffer_size);
1774 if (fm_local == NULL)
1775 GOTO(out, rc = -ENOMEM);
1776 lcl_fm_ext = &fm_local->fm_extents[0];
1778 count_local = fiemap_size_to_count(buffer_size);
1780 memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
1781 fm_start = fiemap->fm_start;
1782 fm_length = fiemap->fm_length;
1783 /* Calculate start stripe, last stripe and length of mapping */
1784 actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
1785 fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
1786 fm_start + fm_length - 1);
1787 /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
1788 if (fm_end > fm_key->oa.o_size)
1789 fm_end = fm_key->oa.o_size;
1791 last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
1792 actual_start_stripe, &stripe_count);
1794 fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start,
1795 fm_end, &start_stripe);
1796 if (fm_end_offset == -EINVAL)
1797 GOTO(out, rc = -EINVAL);
1799 if (fiemap_count_to_size(fiemap->fm_extent_count) > *vallen)
1800 fiemap->fm_extent_count = fiemap_size_to_count(*vallen);
1801 if (fiemap->fm_extent_count == 0) {
1802 get_num_extents = 1;
1805 /* Check each stripe */
1806 for (cur_stripe = start_stripe, i = 0; i < stripe_count;
1807 i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
1808 obd_size req_fm_len; /* Stores length of required mapping */
1809 obd_size len_mapped_single_call;
1810 obd_off lun_start, lun_end, obd_object_end;
1811 unsigned int ext_count;
1813 cur_stripe_wrap = cur_stripe;
1815 /* Find out range of mapping on this stripe */
1816 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
1817 &lun_start, &obd_object_end)) == 0)
1820 if (lov_oinfo_is_dummy(lsm->lsm_oinfo[cur_stripe]))
1821 GOTO(out, rc = -EIO);
1823 /* If this is a continuation FIEMAP call and we are on
1824 * starting stripe then lun_start needs to be set to
1826 if (fm_end_offset != 0 && cur_stripe == start_stripe)
1827 lun_start = fm_end_offset;
1829 if (fm_length != ~0ULL) {
1830 /* Handle fm_start + fm_length overflow */
1831 if (fm_start + fm_length < fm_start)
1832 fm_length = ~0ULL - fm_start;
1833 lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
1839 if (lun_start == lun_end)
1842 req_fm_len = obd_object_end - lun_start;
1843 fm_local->fm_length = 0;
1844 len_mapped_single_call = 0;
1846 /* If the output buffer is very large and the objects have many
1847 * extents we may need to loop on a single OST repeatedly */
1851 if (get_num_extents == 0) {
1852 /* Don't get too many extents. */
1853 if (current_extent + count_local >
1854 fiemap->fm_extent_count)
1855 count_local = fiemap->fm_extent_count -
1859 lun_start += len_mapped_single_call;
1860 fm_local->fm_length = req_fm_len - len_mapped_single_call;
1861 req_fm_len = fm_local->fm_length;
1862 fm_local->fm_extent_count = count_local;
1863 fm_local->fm_mapped_extents = 0;
1864 fm_local->fm_flags = fiemap->fm_flags;
1866 fm_key->oa.o_oi = lsm->lsm_oinfo[cur_stripe]->loi_oi;
1867 ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
1869 if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
1870 GOTO(out, rc = -EINVAL);
1872 /* If OST is inactive, return extent with UNKNOWN flag */
1873 if (!lov->lov_tgts[ost_index]->ltd_active) {
1874 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
1875 fm_local->fm_mapped_extents = 1;
1877 lcl_fm_ext[0].fe_logical = lun_start;
1878 lcl_fm_ext[0].fe_length = obd_object_end -
1880 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1885 fm_local->fm_start = lun_start;
1886 fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1887 memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
1888 *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
1889 rc = obd_get_info(NULL,
1890 lov->lov_tgts[ost_index]->ltd_exp,
1891 keylen, key, vallen, fm_local, lsm);
1896 ext_count = fm_local->fm_mapped_extents;
1897 if (ext_count == 0) {
1899 /* If last stripe has hole at the end,
1900 * then we need to return */
1901 if (cur_stripe_wrap == last_stripe) {
1902 fiemap->fm_mapped_extents = 0;
1908 /* If we just need num of extents then go to next device */
1909 if (get_num_extents) {
1910 current_extent += ext_count;
1914 len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
1915 lun_start + lcl_fm_ext[ext_count - 1].fe_length;
1917 /* Have we finished mapping on this device? */
1918 if (req_fm_len <= len_mapped_single_call)
1921 /* Clear the EXTENT_LAST flag which can be present on
1923 if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
1924 lcl_fm_ext[ext_count - 1].fe_flags &=
1925 ~FIEMAP_EXTENT_LAST;
1927 curr_loc = lov_stripe_size(lsm,
1928 lcl_fm_ext[ext_count - 1].fe_logical+
1929 lcl_fm_ext[ext_count - 1].fe_length,
1931 if (curr_loc >= fm_key->oa.o_size)
1934 fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
1935 ost_index, ext_count,
1938 current_extent += ext_count;
1940 /* Ran out of available extents? */
1941 if (current_extent >= fiemap->fm_extent_count)
1943 } while (ost_done == 0 && ost_eof == 0);
1945 if (cur_stripe_wrap == last_stripe)
1950 /* Indicate that we are returning device offsets unless file just has
1952 if (lsm->lsm_stripe_count > 1)
1953 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
1955 if (get_num_extents)
1956 goto skip_last_device_calc;
1958 /* Check if we have reached the last stripe and whether mapping for that
1959 * stripe is done. */
1960 if (cur_stripe_wrap == last_stripe) {
1961 if (ost_done || ost_eof)
1962 fiemap->fm_extents[current_extent - 1].fe_flags |=
1966 skip_last_device_calc:
1967 fiemap->fm_mapped_extents = current_extent;
1971 OBD_FREE_LARGE(fm_local, buffer_size);
1975 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1976 __u32 keylen, void *key, __u32 *vallen, void *val,
1977 struct lov_stripe_md *lsm)
1979 struct obd_device *obddev = class_exp2obd(exp);
1980 struct lov_obd *lov = &obddev->u.lov;
1984 if (!vallen || !val)
1989 if (KEY_IS(KEY_LOVDESC)) {
1990 struct lov_desc *desc_ret = val;
1991 *desc_ret = lov->desc;
1994 } else if (KEY_IS(KEY_FIEMAP)) {
1995 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
1997 } else if (KEY_IS(KEY_TGT_COUNT)) {
1998 *((int *)val) = lov->desc.ld_tgt_count;
2009 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
2010 obd_count keylen, void *key, obd_count vallen,
2011 void *val, struct ptlrpc_request_set *set)
2013 struct obd_device *obddev = class_exp2obd(exp);
2014 struct lov_obd *lov = &obddev->u.lov;
2017 struct lov_tgt_desc *tgt;
2018 int do_inactive = 0;
2024 set = ptlrpc_prep_set();
2030 count = lov->desc.ld_tgt_count;
2032 if (KEY_IS(KEY_CHECKSUM)) {
2034 } else if (KEY_IS(KEY_CACHE_SET)) {
2035 LASSERT(lov->lov_cache == NULL);
2036 lov->lov_cache = val;
2040 for (i = 0; i < count; i++) {
2041 tgt = lov->lov_tgts[i];
2043 /* OST was disconnected */
2044 if (!tgt || !tgt->ltd_exp)
2047 /* OST is inactive and we don't want inactive OSCs */
2048 if (!tgt->ltd_active && !do_inactive)
2051 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2059 err = ptlrpc_set_wait(set);
2062 ptlrpc_set_destroy(set);
2067 void lov_stripe_lock(struct lov_stripe_md *md)
2068 __acquires(&md->lsm_lock)
2070 LASSERT(md->lsm_lock_owner != current_pid());
2071 spin_lock(&md->lsm_lock);
2072 LASSERT(md->lsm_lock_owner == 0);
2073 md->lsm_lock_owner = current_pid();
2076 void lov_stripe_unlock(struct lov_stripe_md *md)
2077 __releases(&md->lsm_lock)
2079 LASSERT(md->lsm_lock_owner == current_pid());
2080 md->lsm_lock_owner = 0;
2081 spin_unlock(&md->lsm_lock);
2084 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
2085 struct obd_quotactl *oqctl)
2087 struct lov_obd *lov = &obd->u.lov;
2088 struct lov_tgt_desc *tgt;
2090 __u64 bhardlimit = 0;
2094 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
2095 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
2096 oqctl->qc_cmd != Q_GETOQUOTA &&
2097 oqctl->qc_cmd != Q_INITQUOTA &&
2098 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
2099 oqctl->qc_cmd != Q_FINVALIDATE) {
2100 CERROR("%s: bad quota opc %x for lov obd\n",
2101 obd->obd_name, oqctl->qc_cmd);
2107 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2110 tgt = lov->lov_tgts[i];
2115 if (!tgt->ltd_active || tgt->ltd_reap) {
2116 if (oqctl->qc_cmd == Q_GETOQUOTA &&
2117 lov->lov_tgts[i]->ltd_activate) {
2119 CERROR("ost %d is inactive\n", i);
2121 CDEBUG(D_HA, "ost %d is inactive\n", i);
2126 err = obd_quotactl(tgt->ltd_exp, oqctl);
2128 if (tgt->ltd_active && !rc)
2133 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2134 curspace += oqctl->qc_dqblk.dqb_curspace;
2135 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
2140 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2141 oqctl->qc_dqblk.dqb_curspace = curspace;
2142 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
2147 static int lov_quotacheck(struct obd_device *obd, struct obd_export *exp,
2148 struct obd_quotactl *oqctl)
2150 struct lov_obd *lov = &obd->u.lov;
2156 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2157 if (!lov->lov_tgts[i])
2160 /* Skip quota check on the administratively disabled OSTs. */
2161 if (!lov->lov_tgts[i]->ltd_activate) {
2162 CWARN("lov idx %d was administratively disabled, "
2163 "skip quotacheck on it.\n", i);
2167 if (!lov->lov_tgts[i]->ltd_active) {
2168 CERROR("lov idx %d inactive\n", i);
2174 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2177 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_activate)
2180 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
2191 static struct obd_ops lov_obd_ops = {
2192 .o_owner = THIS_MODULE,
2193 .o_setup = lov_setup,
2194 .o_precleanup = lov_precleanup,
2195 .o_cleanup = lov_cleanup,
2196 .o_connect = lov_connect,
2197 .o_disconnect = lov_disconnect,
2198 .o_statfs = lov_statfs,
2199 .o_statfs_async = lov_statfs_async,
2200 .o_packmd = lov_packmd,
2201 .o_unpackmd = lov_unpackmd,
2202 .o_destroy = lov_destroy,
2203 .o_getattr_async = lov_getattr_async,
2204 .o_setattr_async = lov_setattr_async,
2205 .o_change_cbdata = lov_change_cbdata,
2206 .o_find_cbdata = lov_find_cbdata,
2207 .o_iocontrol = lov_iocontrol,
2208 .o_get_info = lov_get_info,
2209 .o_set_info_async = lov_set_info_async,
2210 .o_notify = lov_notify,
2211 .o_pool_new = lov_pool_new,
2212 .o_pool_rem = lov_pool_remove,
2213 .o_pool_add = lov_pool_add,
2214 .o_pool_del = lov_pool_del,
2215 .o_getref = lov_getref,
2216 .o_putref = lov_putref,
2217 .o_quotactl = lov_quotactl,
2218 .o_quotacheck = lov_quotacheck,
2221 struct kmem_cache *lov_oinfo_slab;
2223 extern struct lu_kmem_descr lov_caches[];
2225 int __init lov_init(void)
2227 bool enable_proc = true;
2228 struct obd_type *type;
2232 /* print an address of _any_ initialized kernel symbol from this
2233 * module, to allow debugging with gdb that doesn't support data
2234 * symbols from modules.*/
2235 CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
2237 rc = lu_kmem_init(lov_caches);
2241 lov_oinfo_slab = kmem_cache_create("lov_oinfo",
2242 sizeof(struct lov_oinfo), 0,
2243 SLAB_HWCACHE_ALIGN, NULL);
2244 if (lov_oinfo_slab == NULL) {
2245 lu_kmem_fini(lov_caches);
2249 type = class_search_type(LUSTRE_LOD_NAME);
2250 if (type != NULL && type->typ_procsym != NULL)
2251 enable_proc = false;
2253 rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
2254 LUSTRE_LOV_NAME, &lov_device_type);
2257 kmem_cache_destroy(lov_oinfo_slab);
2258 lu_kmem_fini(lov_caches);
2264 static void /*__exit*/ lov_exit(void)
2266 class_unregister_type(LUSTRE_LOV_NAME);
2267 kmem_cache_destroy(lov_oinfo_slab);
2268 lu_kmem_fini(lov_caches);
2271 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2272 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2273 MODULE_LICENSE("GPL");
2275 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);