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, 2014, 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/ll_fiemap.h>
59 #include <lustre_fid.h>
61 #include "lov_internal.h"
63 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
64 Any function that expects lov_tgts to remain stationary must take a ref. */
65 static void lov_getref(struct obd_device *obd)
67 struct lov_obd *lov = &obd->u.lov;
69 /* nobody gets through here until lov_putref is done */
70 mutex_lock(&lov->lov_lock);
71 atomic_inc(&lov->lov_refcount);
72 mutex_unlock(&lov->lov_lock);
76 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
78 static void lov_putref(struct obd_device *obd)
80 struct lov_obd *lov = &obd->u.lov;
82 mutex_lock(&lov->lov_lock);
83 /* ok to dec to 0 more than once -- ltd_exp's will be null */
84 if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
85 struct list_head kill = LIST_HEAD_INIT(kill);
86 struct lov_tgt_desc *tgt, *n;
89 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
91 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
92 tgt = lov->lov_tgts[i];
94 if (!tgt || !tgt->ltd_reap)
96 list_add(&tgt->ltd_kill, &kill);
97 /* XXX - right now there is a dependency on ld_tgt_count
98 * being the maximum tgt index for computing the
99 * mds_max_easize. So we can't shrink it. */
100 lov_ost_pool_remove(&lov->lov_packed, i);
101 lov->lov_tgts[i] = NULL;
102 lov->lov_death_row--;
104 mutex_unlock(&lov->lov_lock);
106 list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
107 list_del(&tgt->ltd_kill);
109 __lov_del_obd(obd, tgt);
112 mutex_unlock(&lov->lov_lock);
116 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
117 enum obd_notify_event ev);
118 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
119 enum obd_notify_event ev, void *data);
121 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
122 struct obd_connect_data *data)
124 struct lov_obd *lov = &obd->u.lov;
125 struct obd_uuid *tgt_uuid;
126 struct obd_device *tgt_obd;
127 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
128 struct obd_import *imp;
132 if (lov->lov_tgts[index] == NULL)
135 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
136 tgt_obd = lov->lov_tgts[index]->ltd_obd;
138 if (!tgt_obd->obd_set_up) {
139 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
143 /* override the sp_me from lov */
144 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
146 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
147 data->ocd_index = index;
150 * Divine LOV knows that OBDs under it are OSCs.
152 imp = tgt_obd->u.cli.cl_import;
155 tgt_obd->obd_no_recov = 0;
156 /* FIXME this is probably supposed to be
157 ptlrpc_set_import_active. Horrible naming. */
158 ptlrpc_activate_import(imp);
161 rc = obd_register_observer(tgt_obd, obd);
163 CERROR("Target %s register_observer error %d\n",
164 obd_uuid2str(tgt_uuid), rc);
169 if (imp->imp_invalid) {
170 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
171 "disabled\n", obd_uuid2str(tgt_uuid));
175 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
176 &lov_osc_uuid, data, NULL);
177 if (rc || !lov->lov_tgts[index]->ltd_exp) {
178 CERROR("Target %s connect error %d\n",
179 obd_uuid2str(tgt_uuid), rc);
183 lov->lov_tgts[index]->ltd_reap = 0;
185 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
186 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
188 if (lov->targets_proc_entry != NULL) {
189 struct proc_dir_entry *osc_symlink;
190 struct obd_device *osc_obd;
192 osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
194 LASSERT(osc_obd != NULL);
195 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
196 LASSERT(osc_obd->obd_type->typ_name != NULL);
198 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
199 lov->targets_proc_entry,
201 osc_obd->obd_type->typ_name,
203 if (osc_symlink == NULL) {
204 CERROR("cannot register LOV target "
205 "/proc/fs/lustre/%s/%s/target_obds/%s\n",
206 obd->obd_type->typ_name, obd->obd_name,
213 static int lov_connect(const struct lu_env *env,
214 struct obd_export **exp, struct obd_device *obd,
215 struct obd_uuid *cluuid, struct obd_connect_data *data,
218 struct lov_obd *lov = &obd->u.lov;
219 struct lov_tgt_desc *tgt;
220 struct lustre_handle conn;
224 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
226 rc = class_connect(&conn, obd, cluuid);
230 *exp = class_conn2export(&conn);
232 /* Why should there ever be more than 1 connect? */
234 LASSERT(lov->lov_connects == 1);
236 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
238 lov->lov_ocd = *data;
240 lov->targets_proc_entry = lprocfs_register("target_obds",
243 if (IS_ERR(lov->targets_proc_entry)) {
244 CERROR("%s: cannot register "
245 "/proc/fs/lustre/%s/%s/target_obds\n",
246 obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
247 lov->targets_proc_entry = NULL;
251 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
252 tgt = lov->lov_tgts[i];
253 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
255 /* Flags will be lowest common denominator */
256 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
258 CERROR("%s: lov connect tgt %d failed: %d\n",
259 obd->obd_name, i, rc);
262 /* connect to administrative disabled ost */
263 if (!lov->lov_tgts[i]->ltd_exp)
266 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
267 OBD_NOTIFY_CONNECT, (void *)&i);
269 CERROR("%s error sending notify %d\n",
278 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
280 struct lov_obd *lov = &obd->u.lov;
281 struct obd_device *osc_obd;
285 osc_obd = class_exp2obd(tgt->ltd_exp);
286 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
287 obd->obd_name, osc_obd->obd_name);
289 if (tgt->ltd_active) {
291 lov->desc.ld_active_tgt_count--;
292 tgt->ltd_exp->exp_obd->obd_inactive = 1;
296 /* Pass it on to our clients.
297 * XXX This should be an argument to disconnect,
298 * XXX not a back-door flag on the OBD. Ah well.
300 osc_obd->obd_force = obd->obd_force;
301 osc_obd->obd_fail = obd->obd_fail;
302 osc_obd->obd_no_recov = obd->obd_no_recov;
304 if (lov->targets_proc_entry != NULL)
305 lprocfs_remove_proc_entry(osc_obd->obd_name,
306 lov->targets_proc_entry);
309 obd_register_observer(osc_obd, NULL);
311 rc = obd_disconnect(tgt->ltd_exp);
313 CERROR("Target %s disconnect error %d\n",
314 tgt->ltd_uuid.uuid, rc);
322 static int lov_disconnect(struct obd_export *exp)
324 struct obd_device *obd = class_exp2obd(exp);
325 struct lov_obd *lov = &obd->u.lov;
332 /* Only disconnect the underlying layers on the final disconnect. */
334 if (lov->lov_connects != 0) {
335 /* why should there be more than 1 connect? */
336 CERROR("disconnect #%d\n", lov->lov_connects);
340 /* Let's hold another reference so lov_del_obd doesn't spin through
344 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
345 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
346 /* Disconnection is the last we know about an obd */
347 lov_del_target(obd, i, NULL, lov->lov_tgts[i]->ltd_gen);
352 if (lov->targets_proc_entry != NULL)
353 lprocfs_remove(&lov->targets_proc_entry);
356 rc = class_disconnect(exp); /* bz 9811 */
362 * -EINVAL : UUID can't be found in the LOV's target list
363 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
364 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
365 * any >= 0 : is log target index
367 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
368 enum obd_notify_event ev)
370 struct lov_obd *lov = &obd->u.lov;
371 struct lov_tgt_desc *tgt;
372 int index, activate, active;
375 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
376 lov, uuid->uuid, ev);
379 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
380 tgt = lov->lov_tgts[index];
384 * LU-642, initially inactive OSC could miss the obd_connect,
385 * we make up for it here.
387 if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
388 obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
389 struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
391 obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
392 &lov_osc_uuid, &lov->lov_ocd, NULL);
397 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
398 index, obd_uuid2str(&tgt->ltd_uuid),
399 tgt->ltd_exp->exp_handle.h_cookie);
400 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
404 if (index == lov->desc.ld_tgt_count)
405 GOTO(out, index = -EINVAL);
407 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
408 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
410 if (lov->lov_tgts[index]->ltd_activate == activate) {
411 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
412 uuid->uuid, activate ? "" : "de");
414 lov->lov_tgts[index]->ltd_activate = activate;
415 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
416 activate ? "" : "de", obd_uuid2str(uuid));
419 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
420 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
422 if (lov->lov_tgts[index]->ltd_active == active) {
423 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
424 uuid->uuid, active ? "" : "in");
427 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
428 obd_uuid2str(uuid), active ? "" : "in");
431 lov->lov_tgts[index]->ltd_active = active;
433 lov->desc.ld_active_tgt_count++;
434 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
436 lov->desc.ld_active_tgt_count--;
437 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
440 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
449 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
450 enum obd_notify_event ev, void *data)
453 struct lov_obd *lov = &obd->u.lov;
456 down_read(&lov->lov_notify_lock);
457 if (!lov->lov_connects) {
458 up_read(&lov->lov_notify_lock);
462 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
463 ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
464 struct obd_uuid *uuid;
468 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
469 up_read(&lov->lov_notify_lock);
470 CERROR("unexpected notification of %s %s!\n",
471 watched->obd_type->typ_name,
475 uuid = &watched->u.cli.cl_target_uuid;
477 /* Set OSC as active before notifying the observer, so the
478 * observer can use the OSC normally.
480 rc = lov_set_osc_active(obd, uuid, ev);
482 up_read(&lov->lov_notify_lock);
483 CERROR("event(%d) of %s failed: %d\n", ev,
484 obd_uuid2str(uuid), rc);
487 /* active event should be pass lov target index as data */
491 /* Pass the notification up the chain. */
493 rc = obd_notify_observer(obd, watched, ev, data);
495 /* NULL watched means all osc's in the lov (only for syncs) */
496 /* sync event should be send lov idx as data */
497 struct lov_obd *lov = &obd->u.lov;
501 is_sync = (ev == OBD_NOTIFY_SYNC) ||
502 (ev == OBD_NOTIFY_SYNC_NONBLOCK);
505 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
506 if (!lov->lov_tgts[i])
509 /* don't send sync event if target not
510 * connected/activated */
511 if (is_sync && !lov->lov_tgts[i]->ltd_active)
514 rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
517 CERROR("%s: notify %s of %s failed %d\n",
519 obd->obd_observer->obd_name,
520 lov->lov_tgts[i]->ltd_obd->obd_name,
527 up_read(&lov->lov_notify_lock);
531 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
532 __u32 index, int gen, int active)
534 struct lov_obd *lov = &obd->u.lov;
535 struct lov_tgt_desc *tgt;
536 struct obd_device *tgt_obd;
540 CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
541 uuidp->uuid, index, gen, active);
544 CERROR("request to add OBD %s with invalid generation: %d\n",
549 tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
554 mutex_lock(&lov->lov_lock);
556 if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
557 tgt = lov->lov_tgts[index];
558 CERROR("UUID %s already assigned at LOV target index %d\n",
559 obd_uuid2str(&tgt->ltd_uuid), index);
560 mutex_unlock(&lov->lov_lock);
564 if (index >= lov->lov_tgt_size) {
565 /* We need to reallocate the lov target array. */
566 struct lov_tgt_desc **newtgts, **old = NULL;
567 __u32 newsize, oldsize = 0;
569 newsize = max(lov->lov_tgt_size, (__u32)2);
570 while (newsize < index + 1)
571 newsize = newsize << 1;
572 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
573 if (newtgts == NULL) {
574 mutex_unlock(&lov->lov_lock);
578 if (lov->lov_tgt_size) {
579 memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
582 oldsize = lov->lov_tgt_size;
585 lov->lov_tgts = newtgts;
586 lov->lov_tgt_size = newsize;
589 OBD_FREE(old, sizeof(*old) * oldsize);
591 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
592 lov->lov_tgts, lov->lov_tgt_size);
597 mutex_unlock(&lov->lov_lock);
601 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
603 mutex_unlock(&lov->lov_lock);
608 tgt->ltd_uuid = *uuidp;
609 tgt->ltd_obd = tgt_obd;
610 /* XXX - add a sanity check on the generation number. */
612 tgt->ltd_index = index;
613 tgt->ltd_activate = active;
614 lov->lov_tgts[index] = tgt;
615 if (index >= lov->desc.ld_tgt_count)
616 lov->desc.ld_tgt_count = index + 1;
618 mutex_unlock(&lov->lov_lock);
620 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
621 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
623 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
625 if (lov->lov_connects == 0) {
626 /* lov_connect hasn't been called yet. We'll do the
627 lov_connect_obd on this target when that fn first runs,
628 because we don't know the connect flags yet. */
634 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
638 /* connect to administrative disabled ost */
642 if (lov->lov_cache != NULL) {
643 rc = obd_set_info_async(NULL, tgt->ltd_exp,
644 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
645 sizeof(struct cl_client_cache), lov->lov_cache,
651 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
652 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
657 CERROR("add failed (%d), deleting %s\n", rc,
658 obd_uuid2str(&tgt->ltd_uuid));
659 lov_del_target(obd, index, NULL, 0);
665 /* Schedule a target for deletion */
666 int lov_del_target(struct obd_device *obd, __u32 index,
667 struct obd_uuid *uuidp, int gen)
669 struct lov_obd *lov = &obd->u.lov;
670 int count = lov->desc.ld_tgt_count;
674 if (index >= count) {
675 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
680 /* to make sure there's no ongoing lov_notify() now */
681 down_write(&lov->lov_notify_lock);
684 if (!lov->lov_tgts[index]) {
685 CERROR("LOV target at index %d is not setup.\n", index);
686 GOTO(out, rc = -EINVAL);
689 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
690 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
691 lov_uuid2str(lov, index), index,
692 obd_uuid2str(uuidp));
693 GOTO(out, rc = -EINVAL);
696 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
697 lov_uuid2str(lov, index), index,
698 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
699 lov->lov_tgts[index]->ltd_active);
701 lov->lov_tgts[index]->ltd_reap = 1;
702 lov->lov_death_row++;
703 /* we really delete it from obd_putref */
706 up_write(&lov->lov_notify_lock);
711 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
713 struct obd_device *osc_obd;
716 LASSERT(tgt->ltd_reap);
718 osc_obd = class_exp2obd(tgt->ltd_exp);
720 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
722 osc_obd ? osc_obd->obd_name : "<no obd>");
725 lov_disconnect_obd(obd, tgt);
729 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
730 do it ourselves. And we can't do it from lov_cleanup,
731 because we just lost our only reference to it. */
733 class_manual_cleanup(osc_obd);
736 void lov_fix_desc_stripe_size(__u64 *val)
738 if (*val < LOV_MIN_STRIPE_SIZE) {
740 LCONSOLE_INFO("Increasing default stripe size to "
742 LOV_DESC_STRIPE_SIZE_DEFAULT);
743 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
744 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
745 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
746 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
748 *val, LOV_MIN_STRIPE_SIZE);
752 void lov_fix_desc_stripe_count(__u32 *val)
758 void lov_fix_desc_pattern(__u32 *val)
760 /* from lov_setstripe */
761 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
762 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
767 void lov_fix_desc_qos_maxage(__u32 *val)
770 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
773 void lov_fix_desc(struct lov_desc *desc)
775 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
776 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
777 lov_fix_desc_pattern(&desc->ld_pattern);
778 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
781 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
783 struct lov_desc *desc;
784 struct lov_obd *lov = &obd->u.lov;
785 #ifdef CONFIG_PROC_FS
786 struct obd_type *type;
791 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
792 CERROR("LOV setup requires a descriptor\n");
796 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
798 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
799 CERROR("descriptor size wrong: %d > %d\n",
800 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
804 if (desc->ld_magic != LOV_DESC_MAGIC) {
805 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
806 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
807 obd->obd_name, desc);
808 lustre_swab_lov_desc(desc);
810 CERROR("%s: Bad lov desc magic: %#x\n",
811 obd->obd_name, desc->ld_magic);
818 desc->ld_active_tgt_count = 0;
820 lov->lov_tgt_size = 0;
822 mutex_init(&lov->lov_lock);
823 atomic_set(&lov->lov_refcount, 0);
824 lov->lov_sp_me = LUSTRE_SP_CLI;
826 init_rwsem(&lov->lov_notify_lock);
828 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
830 HASH_POOLS_BKT_BITS, 0,
833 &pool_hash_operations,
835 INIT_LIST_HEAD(&lov->lov_pool_list);
836 lov->lov_pool_count = 0;
837 rc = lov_ost_pool_init(&lov->lov_packed, 0);
841 #ifdef CONFIG_PROC_FS
842 obd->obd_vars = lprocfs_lov_obd_vars;
843 /* If this is true then both client (lov) and server
844 * (lod) are on the same node. The lod layer if loaded
845 * first will register the lov proc directory. In that
846 * case obd->obd_type->typ_procroot will be not set.
847 * Instead we use type->typ_procsym as the parent. */
848 type = class_search_type(LUSTRE_LOD_NAME);
849 if (type != NULL && type->typ_procsym != NULL) {
850 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
853 if (IS_ERR(obd->obd_proc_entry)) {
854 rc = PTR_ERR(obd->obd_proc_entry);
855 CERROR("error %d setting up lprocfs for %s\n", rc,
857 obd->obd_proc_entry = NULL;
860 rc = lprocfs_obd_setup(obd);
864 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
865 0444, &lov_proc_target_fops, obd);
867 CWARN("Error adding the target_obd file\n");
869 lov->lov_pool_proc_entry = lprocfs_register("pools",
872 if (IS_ERR(lov->lov_pool_proc_entry)) {
873 rc = PTR_ERR(lov->lov_pool_proc_entry);
874 CERROR("error %d setting up lprocfs for pools\n", rc);
875 lov->lov_pool_proc_entry = NULL;
885 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
888 struct lov_obd *lov = &obd->u.lov;
893 case OBD_CLEANUP_EARLY: {
895 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
896 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
898 obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
910 static int lov_cleanup(struct obd_device *obd)
912 struct lov_obd *lov = &obd->u.lov;
913 struct list_head *pos, *tmp;
914 struct pool_desc *pool;
917 list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
918 pool = list_entry(pos, struct pool_desc, pool_list);
919 /* free pool structs */
920 CDEBUG(D_INFO, "delete pool %p\n", pool);
921 /* In the function below, .hs_keycmp resolves to
922 * pool_hashkey_keycmp() */
923 /* coverity[overrun-buffer-val] */
924 lov_pool_del(obd, pool->pool_name);
926 cfs_hash_putref(lov->lov_pools_hash_body);
927 lov_ost_pool_free(&lov->lov_packed);
929 lprocfs_obd_cleanup(obd);
933 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
934 if (!lov->lov_tgts[i])
937 /* Inactive targets may never have connected */
938 if (lov->lov_tgts[i]->ltd_active ||
939 atomic_read(&lov->lov_refcount))
940 /* We should never get here - these
941 * should have been removed in the
943 CERROR("%s: lov tgt %d not cleaned! "
944 "deathrow=%d, lovrc=%d\n",
945 obd->obd_name, i, lov->lov_death_row,
946 atomic_read(&lov->lov_refcount));
947 lov_del_target(obd, i, NULL, 0);
950 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
952 lov->lov_tgt_size = 0;
955 if (lov->lov_cache != NULL) {
956 cl_cache_decref(lov->lov_cache);
957 lov->lov_cache = NULL;
963 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
964 __u32 *indexp, int *genp)
966 struct obd_uuid obd_uuid;
971 switch(cmd = lcfg->lcfg_command) {
972 case LCFG_LOV_ADD_OBD:
973 case LCFG_LOV_ADD_INA:
974 case LCFG_LOV_DEL_OBD: {
977 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
978 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
979 GOTO(out, rc = -EINVAL);
981 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
983 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", indexp) != 1)
984 GOTO(out, rc = -EINVAL);
985 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
986 GOTO(out, rc = -EINVAL);
989 if (cmd == LCFG_LOV_ADD_OBD)
990 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
991 else if (cmd == LCFG_LOV_ADD_INA)
992 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
994 rc = lov_del_target(obd, index, &obd_uuid, gen);
998 struct lov_desc *desc = &(obd->u.lov.desc);
1001 GOTO(out, rc = -EINVAL);
1003 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
1016 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1017 GOTO(out, rc = -EINVAL);
1025 #define ASSERT_LSM_MAGIC(lsmp) \
1027 LASSERT((lsmp) != NULL); \
1028 LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 || \
1029 (lsmp)->lsm_magic == LOV_MAGIC_V3), \
1030 "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic); \
1033 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1036 struct lov_request_set *lovset = (struct lov_request_set *)data;
1040 /* don't do attribute merge if this aysnc op failed */
1042 atomic_set(&lovset->set_completes, 0);
1043 err = lov_fini_getattr_set(lovset);
1044 RETURN(rc ? rc : err);
1047 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1048 struct ptlrpc_request_set *rqset)
1050 struct lov_request_set *lovset;
1051 struct lov_obd *lov;
1052 struct list_head *pos;
1053 struct lov_request *req;
1058 ASSERT_LSM_MAGIC(oinfo->oi_md);
1060 if (!exp || !exp->exp_obd)
1063 lov = &exp->exp_obd->u.lov;
1065 rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1069 CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1070 POSTID(&oinfo->oi_md->lsm_oi), oinfo->oi_md->lsm_stripe_count,
1071 oinfo->oi_md->lsm_stripe_size);
1073 list_for_each(pos, &lovset->set_list) {
1074 req = list_entry(pos, struct lov_request, rq_link);
1076 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1077 "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1078 POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1079 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1080 &req->rq_oi, rqset);
1082 CERROR("%s: getattr objid "DOSTID" subobj"
1083 DOSTID" on OST idx %d: rc = %d\n",
1084 exp->exp_obd->obd_name,
1085 POSTID(&oinfo->oi_oa->o_oi),
1086 POSTID(&req->rq_oi.oi_oa->o_oi),
1092 if (!list_empty(&rqset->set_requests)) {
1094 LASSERT (rqset->set_interpret == NULL);
1095 rqset->set_interpret = lov_getattr_interpret;
1096 rqset->set_arg = (void *)lovset;
1101 atomic_set(&lovset->set_completes, 0);
1102 err = lov_fini_getattr_set(lovset);
1103 RETURN(rc ? rc : err);
1106 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1109 struct lov_request_set *lovset = (struct lov_request_set *)data;
1114 atomic_set(&lovset->set_completes, 0);
1115 err = lov_fini_setattr_set(lovset);
1116 RETURN(rc ? rc : err);
1119 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1120 needed. Otherwise, a client is waiting for responses. */
1121 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1122 struct obd_trans_info *oti,
1123 struct ptlrpc_request_set *rqset)
1125 struct lov_request_set *set;
1126 struct lov_request *req;
1127 struct list_head *pos;
1128 struct lov_obd *lov;
1133 ASSERT_LSM_MAGIC(oinfo->oi_md);
1134 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1136 LASSERT(oti->oti_logcookies);
1139 if (!exp || !exp->exp_obd)
1142 lov = &exp->exp_obd->u.lov;
1143 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1147 CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1148 POSTID(&oinfo->oi_md->lsm_oi),
1149 oinfo->oi_md->lsm_stripe_count,
1150 oinfo->oi_md->lsm_stripe_size);
1152 list_for_each(pos, &set->set_list) {
1153 req = list_entry(pos, struct lov_request, rq_link);
1155 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1156 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1158 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1159 "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1160 POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1162 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1163 &req->rq_oi, oti, rqset);
1165 CERROR("error: setattr objid "DOSTID" subobj"
1166 DOSTID" on OST idx %d: rc = %d\n",
1167 POSTID(&set->set_oi->oi_oa->o_oi),
1168 POSTID(&req->rq_oi.oi_oa->o_oi),
1174 /* If we are not waiting for responses on async requests, return. */
1175 if (rc || !rqset || list_empty(&rqset->set_requests)) {
1178 atomic_set(&set->set_completes, 0);
1179 err = lov_fini_setattr_set(set);
1180 RETURN(rc ? rc : err);
1183 LASSERT(rqset->set_interpret == NULL);
1184 rqset->set_interpret = lov_setattr_interpret;
1185 rqset->set_arg = (void *)set;
1190 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1192 struct lov_request_set *lovset = (struct lov_request_set *)data;
1197 atomic_set(&lovset->set_completes, 0);
1199 err = lov_fini_statfs_set(lovset);
1200 RETURN(rc ? rc : err);
1203 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1204 __u64 max_age, struct ptlrpc_request_set *rqset)
1206 struct obd_device *obd = class_exp2obd(exp);
1207 struct lov_request_set *set;
1208 struct lov_request *req;
1209 struct list_head *pos;
1210 struct lov_obd *lov;
1214 LASSERT(oinfo != NULL);
1215 LASSERT(oinfo->oi_osfs != NULL);
1218 rc = lov_prep_statfs_set(obd, oinfo, &set);
1222 list_for_each(pos, &set->set_list) {
1223 req = list_entry(pos, struct lov_request, rq_link);
1224 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1225 &req->rq_oi, max_age, rqset);
1230 if (rc || list_empty(&rqset->set_requests)) {
1233 atomic_set(&set->set_completes, 0);
1234 err = lov_fini_statfs_set(set);
1235 RETURN(rc ? rc : err);
1238 LASSERT(rqset->set_interpret == NULL);
1239 rqset->set_interpret = lov_statfs_interpret;
1240 rqset->set_arg = (void *)set;
1244 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1245 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1247 struct ptlrpc_request_set *set = NULL;
1248 struct obd_info oinfo = { { { 0 } } };
1253 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1254 * statfs requests */
1255 set = ptlrpc_prep_set();
1259 oinfo.oi_osfs = osfs;
1260 oinfo.oi_flags = flags;
1261 rc = lov_statfs_async(exp, &oinfo, max_age, set);
1263 rc = ptlrpc_set_wait(set);
1264 ptlrpc_set_destroy(set);
1269 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1270 void *karg, void __user *uarg)
1272 struct obd_device *obddev = class_exp2obd(exp);
1273 struct lov_obd *lov = &obddev->u.lov;
1274 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1275 struct obd_uuid *uuidp;
1279 case IOC_OBD_STATFS: {
1280 struct obd_ioctl_data *data = karg;
1281 struct obd_device *osc_obd;
1282 struct obd_statfs stat_buf = {0};
1286 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1287 if ((index >= count))
1290 if (!lov->lov_tgts[index])
1291 /* Try again with the next index */
1293 if (!lov->lov_tgts[index]->ltd_active)
1296 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1301 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1302 min((int)data->ioc_plen2,
1303 (int)sizeof(struct obd_uuid))))
1306 flags = uarg ? *(__u32 __user *)uarg : 0;
1307 /* got statfs data */
1308 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1309 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1313 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1314 min((int) data->ioc_plen1,
1315 (int) sizeof(stat_buf))))
1319 case OBD_IOC_LOV_GET_CONFIG: {
1320 struct obd_ioctl_data *data;
1321 struct lov_desc *desc;
1326 if (obd_ioctl_getdata(&buf, &len, uarg))
1329 data = (struct obd_ioctl_data *)buf;
1331 if (sizeof(*desc) > data->ioc_inllen1) {
1332 obd_ioctl_freedata(buf, len);
1336 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1337 obd_ioctl_freedata(buf, len);
1341 if (sizeof(__u32) * count > data->ioc_inllen3) {
1342 obd_ioctl_freedata(buf, len);
1346 desc = (struct lov_desc *)data->ioc_inlbuf1;
1347 memcpy(desc, &(lov->desc), sizeof(*desc));
1349 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1350 genp = (__u32 *)data->ioc_inlbuf3;
1351 /* the uuid will be empty for deleted OSTs */
1352 for (i = 0; i < count; i++, uuidp++, genp++) {
1353 if (!lov->lov_tgts[i])
1355 *uuidp = lov->lov_tgts[i]->ltd_uuid;
1356 *genp = lov->lov_tgts[i]->ltd_gen;
1359 if (copy_to_user(uarg, buf, len))
1361 obd_ioctl_freedata(buf, len);
1364 case OBD_IOC_QUOTACTL: {
1365 struct if_quotactl *qctl = karg;
1366 struct lov_tgt_desc *tgt = NULL;
1367 struct obd_quotactl *oqctl;
1369 if (qctl->qc_valid == QC_OSTIDX) {
1370 if (count <= qctl->qc_idx)
1373 tgt = lov->lov_tgts[qctl->qc_idx];
1374 if (!tgt || !tgt->ltd_exp)
1376 } else if (qctl->qc_valid == QC_UUID) {
1377 for (i = 0; i < count; i++) {
1378 tgt = lov->lov_tgts[i];
1380 !obd_uuid_equals(&tgt->ltd_uuid,
1384 if (tgt->ltd_exp == NULL)
1396 LASSERT(tgt && tgt->ltd_exp);
1397 OBD_ALLOC_PTR(oqctl);
1401 QCTL_COPY(oqctl, qctl);
1402 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1404 QCTL_COPY(qctl, oqctl);
1405 qctl->qc_valid = QC_OSTIDX;
1406 qctl->obd_uuid = tgt->ltd_uuid;
1408 OBD_FREE_PTR(oqctl);
1417 for (i = 0; i < count; i++) {
1419 struct obd_device *osc_obd;
1421 /* OST was disconnected */
1422 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1425 /* ll_umount_begin() sets force flag but for lov, not
1426 * osc. Let's pass it through */
1427 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1428 osc_obd->obd_force = obddev->obd_force;
1429 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1431 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1434 if (lov->lov_tgts[i]->ltd_active) {
1435 CDEBUG(err == -ENOTTY ?
1436 D_IOCTL : D_WARNING,
1437 "iocontrol OSC %s on OST "
1438 "idx %d cmd %x: err = %d\n",
1439 lov_uuid2str(lov, i),
1456 #define FIEMAP_BUFFER_SIZE 4096
1459 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1460 * call. The local end offset and the device are sent in the first
1461 * fm_extent. This function calculates the stripe number from the index.
1462 * This function returns a stripe_no on which mapping is to be restarted.
1464 * This function returns fm_end_offset which is the in-OST offset at which
1465 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1466 * will re-calculate proper offset in next stripe.
1467 * Note that the first extent is passed to lov_get_info via the value field.
1469 * \param fiemap fiemap request header
1470 * \param lsm striping information for the file
1471 * \param fm_start logical start of mapping
1472 * \param fm_end logical end of mapping
1473 * \param start_stripe starting stripe will be returned in this
1475 static loff_t fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
1476 struct lov_stripe_md *lsm,
1478 loff_t fm_end, int *start_stripe)
1480 loff_t local_end = fiemap->fm_extents[0].fe_logical;
1483 loff_t fm_end_offset;
1487 if (fiemap->fm_extent_count == 0 ||
1488 fiemap->fm_extents[0].fe_logical == 0)
1491 /* Find out stripe_no from ost_index saved in the fe_device */
1492 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1493 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
1495 if (lov_oinfo_is_dummy(oinfo))
1498 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1504 if (stripe_no == -1)
1507 /* If we have finished mapping on previous device, shift logical
1508 * offset to start of next device */
1509 if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
1510 &lun_start, &lun_end)) != 0 &&
1511 local_end < lun_end) {
1512 fm_end_offset = local_end;
1513 *start_stripe = stripe_no;
1515 /* This is a special value to indicate that caller should
1516 * calculate offset in next stripe. */
1518 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
1521 return fm_end_offset;
1525 * We calculate on which OST the mapping will end. If the length of mapping
1526 * is greater than (stripe_size * stripe_count) then the last_stripe will
1527 * will be one just before start_stripe. Else we check if the mapping
1528 * intersects each OST and find last_stripe.
1529 * This function returns the last_stripe and also sets the stripe_count
1530 * over which the mapping is spread
1532 * \param lsm striping information for the file
1533 * \param fm_start logical start of mapping
1534 * \param fm_end logical end of mapping
1535 * \param start_stripe starting stripe of the mapping
1536 * \param stripe_count the number of stripes across which to map is returned
1538 * \retval last_stripe return the last stripe of the mapping
1540 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm,
1541 loff_t fm_start, loff_t fm_end,
1542 int start_stripe, int *stripe_count)
1549 if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
1550 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
1552 *stripe_count = lsm->lsm_stripe_count;
1554 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
1555 i = (i + 1) % lsm->lsm_stripe_count, j++) {
1556 if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
1557 &obd_start, &obd_end)) == 0)
1561 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
1568 * Set fe_device and copy extents from local buffer into main return buffer.
1570 * \param fiemap fiemap request header
1571 * \param lcl_fm_ext array of local fiemap extents to be copied
1572 * \param ost_index OST index to be written into the fm_device field for each
1574 * \param ext_count number of extents to be copied
1575 * \param current_extent where to start copying in main extent array
1577 static void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
1578 struct ll_fiemap_extent *lcl_fm_ext,
1579 int ost_index, unsigned int ext_count,
1585 for (ext = 0; ext < ext_count; ext++) {
1586 lcl_fm_ext[ext].fe_device = ost_index;
1587 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1590 /* Copy fm_extent's from fm_local to return buffer */
1591 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1592 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
1596 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1597 * This also handles the restarting of FIEMAP calls in case mapping overflows
1598 * the available number of extents in single call.
1600 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
1601 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1603 struct ll_fiemap_info_key *fm_key = key;
1604 struct ll_user_fiemap *fiemap = val;
1605 struct ll_user_fiemap *fm_local = NULL;
1606 struct ll_fiemap_extent *lcl_fm_ext;
1608 unsigned int get_num_extents = 0;
1609 int ost_index = 0, actual_start_stripe, start_stripe;
1613 loff_t fm_end_offset;
1615 int current_extent = 0, rc = 0, i;
1616 /* Whether have we collected enough extents */
1617 bool enough = false;
1618 int ost_eof = 0; /* EOF for object */
1619 int ost_done = 0; /* done with required mapping for this OST? */
1621 int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
1622 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1624 if (!lsm_has_objects(lsm)) {
1625 if (lsm && lsm_is_released(lsm) && (fm_key->fiemap.fm_start <
1626 fm_key->oa.o_size)) {
1627 /* released file, return a minimal FIEMAP if
1628 * request fits in file-size.
1630 fiemap->fm_mapped_extents = 1;
1631 fiemap->fm_extents[0].fe_logical =
1632 fm_key->fiemap.fm_start;
1633 if (fm_key->fiemap.fm_start + fm_key->fiemap.fm_length <
1635 fiemap->fm_extents[0].fe_length =
1636 fm_key->fiemap.fm_length;
1638 fiemap->fm_extents[0].fe_length =
1640 fm_key->fiemap.fm_start;
1641 fiemap->fm_extents[0].fe_flags |=
1642 (FIEMAP_EXTENT_UNKNOWN |
1643 FIEMAP_EXTENT_LAST);
1648 if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
1649 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
1651 OBD_ALLOC_LARGE(fm_local, buffer_size);
1652 if (fm_local == NULL)
1653 GOTO(out, rc = -ENOMEM);
1654 lcl_fm_ext = &fm_local->fm_extents[0];
1656 count_local = fiemap_size_to_count(buffer_size);
1658 memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
1659 fm_start = fiemap->fm_start;
1660 fm_length = fiemap->fm_length;
1661 /* Calculate start stripe, last stripe and length of mapping */
1662 actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
1663 fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
1664 fm_start + fm_length - 1);
1665 /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
1666 if (fm_end > fm_key->oa.o_size)
1667 fm_end = fm_key->oa.o_size;
1669 last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
1670 actual_start_stripe, &stripe_count);
1672 fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start,
1673 fm_end, &start_stripe);
1674 if (fm_end_offset == -EINVAL)
1675 GOTO(out, rc = -EINVAL);
1677 if (fiemap_count_to_size(fiemap->fm_extent_count) > *vallen)
1678 fiemap->fm_extent_count = fiemap_size_to_count(*vallen);
1679 if (fiemap->fm_extent_count == 0) {
1680 get_num_extents = 1;
1683 /* Check each stripe */
1684 for (cur_stripe = start_stripe, i = 0; i < stripe_count;
1685 i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
1686 loff_t req_fm_len; /* Stores length of required mapping */
1687 loff_t len_mapped_single_call;
1690 loff_t obd_object_end;
1691 unsigned int ext_count;
1693 cur_stripe_wrap = cur_stripe;
1695 /* Find out range of mapping on this stripe */
1696 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
1697 &lun_start, &obd_object_end)) == 0)
1700 if (lov_oinfo_is_dummy(lsm->lsm_oinfo[cur_stripe]))
1701 GOTO(out, rc = -EIO);
1703 /* If this is a continuation FIEMAP call and we are on
1704 * starting stripe then lun_start needs to be set to
1706 if (fm_end_offset != 0 && cur_stripe == start_stripe)
1707 lun_start = fm_end_offset;
1709 if (fm_length != ~0ULL) {
1710 /* Handle fm_start + fm_length overflow */
1711 if (fm_start + fm_length < fm_start)
1712 fm_length = ~0ULL - fm_start;
1713 lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
1719 if (lun_start == lun_end)
1722 req_fm_len = obd_object_end - lun_start;
1723 fm_local->fm_length = 0;
1724 len_mapped_single_call = 0;
1726 /* If the output buffer is very large and the objects have many
1727 * extents we may need to loop on a single OST repeatedly */
1731 if (get_num_extents == 0) {
1732 /* Don't get too many extents. */
1733 if (current_extent + count_local >
1734 fiemap->fm_extent_count)
1735 count_local = fiemap->fm_extent_count -
1739 lun_start += len_mapped_single_call;
1740 fm_local->fm_length = req_fm_len - len_mapped_single_call;
1741 req_fm_len = fm_local->fm_length;
1742 fm_local->fm_extent_count = enough ? 1 : count_local;
1743 fm_local->fm_mapped_extents = 0;
1744 fm_local->fm_flags = fiemap->fm_flags;
1746 fm_key->oa.o_oi = lsm->lsm_oinfo[cur_stripe]->loi_oi;
1747 ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
1749 if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
1750 GOTO(out, rc = -EINVAL);
1752 /* If OST is inactive, return extent with UNKNOWN flag */
1753 if (!lov->lov_tgts[ost_index]->ltd_active) {
1754 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
1755 fm_local->fm_mapped_extents = 1;
1757 lcl_fm_ext[0].fe_logical = lun_start;
1758 lcl_fm_ext[0].fe_length = obd_object_end -
1760 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1765 fm_local->fm_start = lun_start;
1766 fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1767 memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
1768 *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
1769 rc = obd_get_info(NULL,
1770 lov->lov_tgts[ost_index]->ltd_exp,
1771 keylen, key, vallen, fm_local, lsm);
1776 ext_count = fm_local->fm_mapped_extents;
1777 if (ext_count == 0) {
1779 /* If last stripe has hole at the end,
1780 * then we need to return */
1781 if (cur_stripe_wrap == last_stripe) {
1782 fiemap->fm_mapped_extents = 0;
1786 } else if (enough) {
1788 * We've collected enough extents and there are
1789 * more extents after it.
1794 /* If we just need num of extents then go to next device */
1795 if (get_num_extents) {
1796 current_extent += ext_count;
1800 len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
1801 lun_start + lcl_fm_ext[ext_count - 1].fe_length;
1803 /* Have we finished mapping on this device? */
1804 if (req_fm_len <= len_mapped_single_call)
1807 /* Clear the EXTENT_LAST flag which can be present on
1809 if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
1810 lcl_fm_ext[ext_count - 1].fe_flags &=
1811 ~FIEMAP_EXTENT_LAST;
1813 curr_loc = lov_stripe_size(lsm,
1814 lcl_fm_ext[ext_count - 1].fe_logical+
1815 lcl_fm_ext[ext_count - 1].fe_length,
1817 if (curr_loc >= fm_key->oa.o_size)
1820 fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
1821 ost_index, ext_count,
1824 current_extent += ext_count;
1826 /* Ran out of available extents? */
1827 if (current_extent >= fiemap->fm_extent_count)
1829 } while (ost_done == 0 && ost_eof == 0);
1831 if (cur_stripe_wrap == last_stripe)
1836 /* Indicate that we are returning device offsets unless file just has
1838 if (lsm->lsm_stripe_count > 1)
1839 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
1841 if (get_num_extents)
1842 goto skip_last_device_calc;
1844 /* Check if we have reached the last stripe and whether mapping for that
1845 * stripe is done. */
1846 if (cur_stripe_wrap == last_stripe) {
1847 if (ost_done || ost_eof)
1848 fiemap->fm_extents[current_extent - 1].fe_flags |=
1852 skip_last_device_calc:
1853 fiemap->fm_mapped_extents = current_extent;
1857 OBD_FREE_LARGE(fm_local, buffer_size);
1861 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1862 __u32 keylen, void *key,
1863 __u32 *vallen, void *val,
1864 struct lov_stripe_md *lsm)
1866 struct obd_device *obddev = class_exp2obd(exp);
1867 struct lov_obd *lov = &obddev->u.lov;
1871 if (!vallen || !val)
1876 if (KEY_IS(KEY_LOVDESC)) {
1877 struct lov_desc *desc_ret = val;
1878 *desc_ret = lov->desc;
1881 } else if (KEY_IS(KEY_FIEMAP)) {
1882 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
1884 } else if (KEY_IS(KEY_TGT_COUNT)) {
1885 *((int *)val) = lov->desc.ld_tgt_count;
1896 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1897 __u32 keylen, void *key,
1898 __u32 vallen, void *val,
1899 struct ptlrpc_request_set *set)
1901 struct obd_device *obddev = class_exp2obd(exp);
1902 struct lov_obd *lov = &obddev->u.lov;
1903 struct lov_tgt_desc *tgt;
1904 int do_inactive = 0;
1914 set = ptlrpc_prep_set();
1920 count = lov->desc.ld_tgt_count;
1922 if (KEY_IS(KEY_CHECKSUM)) {
1924 } else if (KEY_IS(KEY_CACHE_SET)) {
1925 LASSERT(lov->lov_cache == NULL);
1926 lov->lov_cache = val;
1928 cl_cache_incref(lov->lov_cache);
1931 for (i = 0; i < count; i++) {
1932 tgt = lov->lov_tgts[i];
1934 /* OST was disconnected */
1935 if (!tgt || !tgt->ltd_exp)
1938 /* OST is inactive and we don't want inactive OSCs */
1939 if (!tgt->ltd_active && !do_inactive)
1942 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1950 err = ptlrpc_set_wait(set);
1953 ptlrpc_set_destroy(set);
1958 void lov_stripe_lock(struct lov_stripe_md *md)
1959 __acquires(&md->lsm_lock)
1961 LASSERT(md->lsm_lock_owner != current_pid());
1962 spin_lock(&md->lsm_lock);
1963 LASSERT(md->lsm_lock_owner == 0);
1964 md->lsm_lock_owner = current_pid();
1967 void lov_stripe_unlock(struct lov_stripe_md *md)
1968 __releases(&md->lsm_lock)
1970 LASSERT(md->lsm_lock_owner == current_pid());
1971 md->lsm_lock_owner = 0;
1972 spin_unlock(&md->lsm_lock);
1975 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1976 struct obd_quotactl *oqctl)
1978 struct lov_obd *lov = &obd->u.lov;
1979 struct lov_tgt_desc *tgt;
1981 __u64 bhardlimit = 0;
1985 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
1986 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
1987 oqctl->qc_cmd != Q_GETOQUOTA &&
1988 oqctl->qc_cmd != Q_INITQUOTA &&
1989 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
1990 oqctl->qc_cmd != Q_FINVALIDATE) {
1991 CERROR("%s: bad quota opc %x for lov obd\n",
1992 obd->obd_name, oqctl->qc_cmd);
1998 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2001 tgt = lov->lov_tgts[i];
2006 if (!tgt->ltd_active || tgt->ltd_reap) {
2007 if (oqctl->qc_cmd == Q_GETOQUOTA &&
2008 lov->lov_tgts[i]->ltd_activate) {
2010 CERROR("ost %d is inactive\n", i);
2012 CDEBUG(D_HA, "ost %d is inactive\n", i);
2017 err = obd_quotactl(tgt->ltd_exp, oqctl);
2019 if (tgt->ltd_active && !rc)
2024 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2025 curspace += oqctl->qc_dqblk.dqb_curspace;
2026 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
2031 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2032 oqctl->qc_dqblk.dqb_curspace = curspace;
2033 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
2038 static int lov_quotacheck(struct obd_device *obd, struct obd_export *exp,
2039 struct obd_quotactl *oqctl)
2041 struct lov_obd *lov = &obd->u.lov;
2047 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2048 if (!lov->lov_tgts[i])
2051 /* Skip quota check on the administratively disabled OSTs. */
2052 if (!lov->lov_tgts[i]->ltd_activate) {
2053 CWARN("lov idx %d was administratively disabled, "
2054 "skip quotacheck on it.\n", i);
2058 if (!lov->lov_tgts[i]->ltd_active) {
2059 CERROR("lov idx %d inactive\n", i);
2065 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2068 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_activate)
2071 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
2082 static struct obd_ops lov_obd_ops = {
2083 .o_owner = THIS_MODULE,
2084 .o_setup = lov_setup,
2085 .o_precleanup = lov_precleanup,
2086 .o_cleanup = lov_cleanup,
2087 .o_connect = lov_connect,
2088 .o_disconnect = lov_disconnect,
2089 .o_statfs = lov_statfs,
2090 .o_statfs_async = lov_statfs_async,
2091 .o_packmd = lov_packmd,
2092 .o_unpackmd = lov_unpackmd,
2093 .o_getattr_async = lov_getattr_async,
2094 .o_setattr_async = lov_setattr_async,
2095 .o_iocontrol = lov_iocontrol,
2096 .o_get_info = lov_get_info,
2097 .o_set_info_async = lov_set_info_async,
2098 .o_notify = lov_notify,
2099 .o_pool_new = lov_pool_new,
2100 .o_pool_rem = lov_pool_remove,
2101 .o_pool_add = lov_pool_add,
2102 .o_pool_del = lov_pool_del,
2103 .o_getref = lov_getref,
2104 .o_putref = lov_putref,
2105 .o_quotactl = lov_quotactl,
2106 .o_quotacheck = lov_quotacheck,
2109 struct kmem_cache *lov_oinfo_slab;
2111 static int __init lov_init(void)
2113 bool enable_proc = true;
2114 struct obd_type *type;
2118 /* print an address of _any_ initialized kernel symbol from this
2119 * module, to allow debugging with gdb that doesn't support data
2120 * symbols from modules.*/
2121 CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
2123 rc = lu_kmem_init(lov_caches);
2127 lov_oinfo_slab = kmem_cache_create("lov_oinfo",
2128 sizeof(struct lov_oinfo), 0,
2129 SLAB_HWCACHE_ALIGN, NULL);
2130 if (lov_oinfo_slab == NULL) {
2131 lu_kmem_fini(lov_caches);
2135 type = class_search_type(LUSTRE_LOD_NAME);
2136 if (type != NULL && type->typ_procsym != NULL)
2137 enable_proc = false;
2139 rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
2140 LUSTRE_LOV_NAME, &lov_device_type);
2143 kmem_cache_destroy(lov_oinfo_slab);
2144 lu_kmem_fini(lov_caches);
2150 static void /*__exit*/ lov_exit(void)
2152 class_unregister_type(LUSTRE_LOV_NAME);
2153 kmem_cache_destroy(lov_oinfo_slab);
2154 lu_kmem_fini(lov_caches);
2157 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2158 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2159 MODULE_LICENSE("GPL");
2161 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);