1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/lov/lov_obd.c
41 * Author: Phil Schwan <phil@clusterfs.com>
42 * Author: Peter Braam <braam@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nathan Rutman <nathan@clusterfs.com>
48 # define EXPORT_SYMTAB
50 #define DEBUG_SUBSYSTEM S_LOV
52 #include <libcfs/libcfs.h>
54 #include <liblustre.h>
57 #include <obd_support.h>
58 #include <lustre_lib.h>
59 #include <lustre_net.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_dlm.h>
62 #include <lustre_mds.h>
63 #include <lustre_debug.h>
64 #include <obd_class.h>
67 #include <lprocfs_status.h>
68 #include <lustre_param.h>
69 #include <cl_object.h>
70 #include <lustre/ll_fiemap.h>
72 #include "lov_internal.h"
74 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
75 Any function that expects lov_tgts to remain stationary must take a ref. */
76 static void lov_getref(struct obd_device *obd)
78 struct lov_obd *lov = &obd->u.lov;
80 /* nobody gets through here until lov_putref is done */
81 cfs_mutex_down(&lov->lov_lock);
82 cfs_atomic_inc(&lov->lov_refcount);
83 cfs_mutex_up(&lov->lov_lock);
87 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
89 static void lov_putref(struct obd_device *obd)
91 struct lov_obd *lov = &obd->u.lov;
93 cfs_mutex_down(&lov->lov_lock);
94 /* ok to dec to 0 more than once -- ltd_exp's will be null */
95 if (cfs_atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
98 struct lov_tgt_desc *tgt, *n;
99 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
101 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
102 tgt = lov->lov_tgts[i];
104 if (!tgt || !tgt->ltd_reap)
106 cfs_list_add(&tgt->ltd_kill, &kill);
107 /* XXX - right now there is a dependency on ld_tgt_count
108 * being the maximum tgt index for computing the
109 * mds_max_easize. So we can't shrink it. */
110 lov_ost_pool_remove(&lov->lov_packed, i);
111 lov->lov_tgts[i] = NULL;
112 lov->lov_death_row--;
114 cfs_mutex_up(&lov->lov_lock);
116 cfs_list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
117 cfs_list_del(&tgt->ltd_kill);
119 __lov_del_obd(obd, tgt);
122 cfs_mutex_up(&lov->lov_lock);
126 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
127 enum obd_notify_event ev);
128 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
129 enum obd_notify_event ev, void *data);
132 #define MAX_STRING_SIZE 128
133 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
134 struct obd_connect_data *data)
136 struct lov_obd *lov = &obd->u.lov;
137 struct obd_uuid *tgt_uuid;
138 struct obd_device *tgt_obd;
139 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
140 struct obd_import *imp;
142 cfs_proc_dir_entry_t *lov_proc_dir;
147 if (!lov->lov_tgts[index])
150 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
151 tgt_obd = lov->lov_tgts[index]->ltd_obd;
153 if (!tgt_obd->obd_set_up) {
154 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
158 /* override the sp_me from lov */
159 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
161 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
162 data->ocd_index = index;
165 * Divine LOV knows that OBDs under it are OSCs.
167 imp = tgt_obd->u.cli.cl_import;
170 tgt_obd->obd_no_recov = 0;
171 /* FIXME this is probably supposed to be
172 ptlrpc_set_import_active. Horrible naming. */
173 ptlrpc_activate_import(imp);
176 rc = obd_register_observer(tgt_obd, obd);
178 CERROR("Target %s register_observer error %d\n",
179 obd_uuid2str(tgt_uuid), rc);
184 if (imp->imp_invalid) {
185 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
186 "disabled\n", obd_uuid2str(tgt_uuid));
190 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
191 &lov_osc_uuid, data, NULL);
192 if (rc || !lov->lov_tgts[index]->ltd_exp) {
193 CERROR("Target %s connect error %d\n",
194 obd_uuid2str(tgt_uuid), rc);
198 lov->lov_tgts[index]->ltd_reap = 0;
200 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
201 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
204 lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
206 struct obd_device *osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
207 cfs_proc_dir_entry_t *osc_symlink;
209 LASSERT(osc_obd != NULL);
210 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
211 LASSERT(osc_obd->obd_type->typ_name != NULL);
213 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
216 osc_obd->obd_type->typ_name,
218 if (osc_symlink == NULL) {
219 CERROR("could not register LOV target "
220 "/proc/fs/lustre/%s/%s/target_obds/%s.",
221 obd->obd_type->typ_name, obd->obd_name,
223 lprocfs_remove(&lov_proc_dir);
228 rc = qos_add_tgt(obd, index);
230 CERROR("qos_add_tgt failed %d\n", rc);
235 static int lov_connect(const struct lu_env *env,
236 struct obd_export **exp, struct obd_device *obd,
237 struct obd_uuid *cluuid, struct obd_connect_data *data,
240 struct lov_obd *lov = &obd->u.lov;
241 struct lov_tgt_desc *tgt;
242 struct lustre_handle conn;
246 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
248 rc = class_connect(&conn, obd, cluuid);
252 *exp = class_conn2export(&conn);
254 /* Why should there ever be more than 1 connect? */
256 LASSERT(lov->lov_connects == 1);
258 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
260 lov->lov_ocd = *data;
263 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
264 tgt = lov->lov_tgts[i];
265 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
267 /* Flags will be lowest common denominator */
268 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
270 CERROR("%s: lov connect tgt %d failed: %d\n",
271 obd->obd_name, i, rc);
274 /* connect to administrative disabled ost */
275 if (!lov->lov_tgts[i]->ltd_exp)
278 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
279 OBD_NOTIFY_CONNECT, (void *)&i);
281 CERROR("%s error sending notify %d\n",
290 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
292 cfs_proc_dir_entry_t *lov_proc_dir;
293 struct lov_obd *lov = &obd->u.lov;
294 struct obd_device *osc_obd;
298 osc_obd = class_exp2obd(tgt->ltd_exp);
299 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
300 obd->obd_name, osc_obd->obd_name);
302 if (tgt->ltd_active) {
304 lov->desc.ld_active_tgt_count--;
305 tgt->ltd_exp->exp_obd->obd_inactive = 1;
308 lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
310 cfs_proc_dir_entry_t *osc_symlink;
312 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
314 lprocfs_remove(&osc_symlink);
316 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing.",
317 obd->obd_type->typ_name, obd->obd_name,
323 /* Pass it on to our clients.
324 * XXX This should be an argument to disconnect,
325 * XXX not a back-door flag on the OBD. Ah well.
327 osc_obd->obd_force = obd->obd_force;
328 osc_obd->obd_fail = obd->obd_fail;
329 osc_obd->obd_no_recov = obd->obd_no_recov;
332 obd_register_observer(osc_obd, NULL);
334 rc = obd_disconnect(tgt->ltd_exp);
336 CERROR("Target %s disconnect error %d\n",
337 tgt->ltd_uuid.uuid, rc);
341 qos_del_tgt(obd, tgt);
347 static int lov_disconnect(struct obd_export *exp)
349 struct obd_device *obd = class_exp2obd(exp);
350 struct lov_obd *lov = &obd->u.lov;
357 /* Only disconnect the underlying layers on the final disconnect. */
359 if (lov->lov_connects != 0) {
360 /* why should there be more than 1 connect? */
361 CERROR("disconnect #%d\n", lov->lov_connects);
365 /* Let's hold another reference so lov_del_obd doesn't spin through
369 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
370 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
371 /* Disconnection is the last we know about an obd */
372 lov_del_target(obd, i, 0, lov->lov_tgts[i]->ltd_gen);
378 rc = class_disconnect(exp); /* bz 9811 */
384 * -EINVAL : UUID can't be found in the LOV's target list
385 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
386 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
387 * any >= 0 : is log target index
389 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
390 enum obd_notify_event ev)
392 struct lov_obd *lov = &obd->u.lov;
393 struct lov_tgt_desc *tgt;
394 int index, activate, active;
397 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
398 lov, uuid->uuid, ev);
401 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
402 tgt = lov->lov_tgts[index];
403 if (!tgt || !tgt->ltd_exp)
406 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
407 index, obd_uuid2str(&tgt->ltd_uuid),
408 tgt->ltd_exp->exp_handle.h_cookie);
409 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
413 if (index == lov->desc.ld_tgt_count)
414 GOTO(out, index = -EINVAL);
416 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
417 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
419 if (lov->lov_tgts[index]->ltd_activate == activate) {
420 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
421 uuid->uuid, activate ? "" : "de");
423 lov->lov_tgts[index]->ltd_activate = activate;
424 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
425 activate ? "" : "de", obd_uuid2str(uuid));
428 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
429 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
431 if (lov->lov_tgts[index]->ltd_active == active) {
432 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
433 uuid->uuid, active ? "" : "in");
436 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
437 obd_uuid2str(uuid), active ? "" : "in");
440 lov->lov_tgts[index]->ltd_active = active;
442 lov->desc.ld_active_tgt_count++;
443 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
445 lov->desc.ld_active_tgt_count--;
446 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
448 /* remove any old qos penalty */
449 lov->lov_tgts[index]->ltd_qos.ltq_penalty = 0;
451 CERROR("Unknown event(%d) for uuid %s", ev, uuid->uuid);
459 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
460 enum obd_notify_event ev, void *data)
465 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
466 ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
467 struct obd_uuid *uuid;
471 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
472 CERROR("unexpected notification of %s %s!\n",
473 watched->obd_type->typ_name,
477 uuid = &watched->u.cli.cl_target_uuid;
479 /* Set OSC as active before notifying the observer, so the
480 * observer can use the OSC normally.
482 rc = lov_set_osc_active(obd, uuid, ev);
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,
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 cfs_mutex_down(&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 cfs_mutex_up(&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 cfs_mutex_up(&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;
591 OBD_FREE(old, sizeof(*old) * oldsize);
593 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
594 lov->lov_tgts, lov->lov_tgt_size);
599 cfs_mutex_up(&lov->lov_lock);
603 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
605 cfs_mutex_up(&lov->lov_lock);
610 tgt->ltd_uuid = *uuidp;
611 tgt->ltd_obd = tgt_obd;
612 /* XXX - add a sanity check on the generation number. */
614 tgt->ltd_index = index;
615 tgt->ltd_activate = active;
616 lov->lov_tgts[index] = tgt;
617 if (index >= lov->desc.ld_tgt_count)
618 lov->desc.ld_tgt_count = index + 1;
620 cfs_mutex_up(&lov->lov_lock);
622 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
623 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
625 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
627 if (lov->lov_connects == 0) {
628 /* lov_connect hasn't been called yet. We'll do the
629 lov_connect_obd on this target when that fn first runs,
630 because we don't know the connect flags yet. */
636 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
640 /* connect to administrative disabled ost */
644 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
645 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
650 CERROR("add failed (%d), deleting %s\n", rc,
651 obd_uuid2str(&tgt->ltd_uuid));
652 lov_del_target(obd, index, 0, 0);
658 /* Schedule a target for deletion */
659 int lov_del_target(struct obd_device *obd, __u32 index,
660 struct obd_uuid *uuidp, int gen)
662 struct lov_obd *lov = &obd->u.lov;
663 int count = lov->desc.ld_tgt_count;
667 if (index >= count) {
668 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
675 if (!lov->lov_tgts[index]) {
676 CERROR("LOV target at index %d is not setup.\n", index);
677 GOTO(out, rc = -EINVAL);
680 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
681 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
682 lov_uuid2str(lov, index), index,
683 obd_uuid2str(uuidp));
684 GOTO(out, rc = -EINVAL);
687 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
688 lov_uuid2str(lov, index), index,
689 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
690 lov->lov_tgts[index]->ltd_active);
692 lov->lov_tgts[index]->ltd_reap = 1;
693 lov->lov_death_row++;
694 /* we really delete it from obd_putref */
701 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
703 struct obd_device *osc_obd;
706 LASSERT(tgt->ltd_reap);
708 osc_obd = class_exp2obd(tgt->ltd_exp);
710 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
712 osc_obd ? osc_obd->obd_name : "<no obd>");
715 lov_disconnect_obd(obd, tgt);
719 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
720 do it ourselves. And we can't do it from lov_cleanup,
721 because we just lost our only reference to it. */
723 class_manual_cleanup(osc_obd);
726 void lov_fix_desc_stripe_size(__u64 *val)
728 if (*val < PTLRPC_MAX_BRW_SIZE) {
729 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
730 PTLRPC_MAX_BRW_SIZE);
731 *val = PTLRPC_MAX_BRW_SIZE;
732 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
733 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
734 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
736 *val, LOV_MIN_STRIPE_SIZE);
740 void lov_fix_desc_stripe_count(__u32 *val)
746 void lov_fix_desc_pattern(__u32 *val)
748 /* from lov_setstripe */
749 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
750 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
755 void lov_fix_desc_qos_maxage(__u32 *val)
759 *val = QOS_DEFAULT_MAXAGE;
762 void lov_fix_desc(struct lov_desc *desc)
764 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
765 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
766 lov_fix_desc_pattern(&desc->ld_pattern);
767 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
770 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
772 struct lprocfs_static_vars lvars = { 0 };
773 struct lov_desc *desc;
774 struct lov_obd *lov = &obd->u.lov;
778 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
779 CERROR("LOV setup requires a descriptor\n");
783 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
785 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
786 CERROR("descriptor size wrong: %d > %d\n",
787 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
791 if (desc->ld_magic != LOV_DESC_MAGIC) {
792 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
793 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
794 obd->obd_name, desc);
795 lustre_swab_lov_desc(desc);
797 CERROR("%s: Bad lov desc magic: %#x\n",
798 obd->obd_name, desc->ld_magic);
805 desc->ld_active_tgt_count = 0;
807 lov->lov_tgt_size = 0;
809 cfs_sema_init(&lov->lov_lock, 1);
810 cfs_atomic_set(&lov->lov_refcount, 0);
811 CFS_INIT_LIST_HEAD(&lov->lov_qos.lq_oss_list);
812 cfs_init_rwsem(&lov->lov_qos.lq_rw_sem);
813 lov->lov_sp_me = LUSTRE_SP_CLI;
814 lov->lov_qos.lq_dirty = 1;
815 lov->lov_qos.lq_rr.lqr_dirty = 1;
816 lov->lov_qos.lq_reset = 1;
817 /* Default priority is toward free space balance */
818 lov->lov_qos.lq_prio_free = 232;
819 /* Default threshold for rr (roughly 17%) */
820 lov->lov_qos.lq_threshold_rr = 43;
821 /* Init statfs fields */
822 OBD_ALLOC_PTR(lov->lov_qos.lq_statfs_data);
823 if (NULL == lov->lov_qos.lq_statfs_data)
825 cfs_waitq_init(&lov->lov_qos.lq_statfs_waitq);
827 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
829 HASH_POOLS_BKT_BITS, 0,
832 &pool_hash_operations,
834 CFS_INIT_LIST_HEAD(&lov->lov_pool_list);
835 lov->lov_pool_count = 0;
836 rc = lov_ost_pool_init(&lov->lov_packed, 0);
838 GOTO(out_free_statfs, rc);
839 rc = lov_ost_pool_init(&lov->lov_qos.lq_rr.lqr_pool, 0);
841 GOTO(out_free_lov_packed, rc);
843 lprocfs_lov_init_vars(&lvars);
844 lprocfs_obd_setup(obd, lvars.obd_vars);
849 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
850 0444, &lov_proc_target_fops, obd);
852 CWARN("Error adding the target_obd file\n");
855 lov->lov_pool_proc_entry = lprocfs_register("pools",
862 lov_ost_pool_free(&lov->lov_packed);
864 OBD_FREE_PTR(lov->lov_qos.lq_statfs_data);
868 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
871 struct lov_obd *lov = &obd->u.lov;
876 case OBD_CLEANUP_EARLY: {
878 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
879 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
881 obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
886 case OBD_CLEANUP_EXPORTS:
887 rc = obd_llog_finish(obd, 0);
889 CERROR("failed to cleanup llogging subsystems\n");
895 static int lov_cleanup(struct obd_device *obd)
897 struct lov_obd *lov = &obd->u.lov;
898 cfs_list_t *pos, *tmp;
899 struct pool_desc *pool;
901 cfs_list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
902 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
903 /* free pool structs */
904 CDEBUG(D_INFO, "delete pool %p\n", pool);
905 lov_pool_del(obd, pool->pool_name);
907 cfs_hash_putref(lov->lov_pools_hash_body);
908 lov_ost_pool_free(&(lov->lov_qos.lq_rr.lqr_pool));
909 lov_ost_pool_free(&lov->lov_packed);
914 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
915 if (!lov->lov_tgts[i])
918 /* Inactive targets may never have connected */
919 if (lov->lov_tgts[i]->ltd_active ||
920 cfs_atomic_read(&lov->lov_refcount))
921 /* We should never get here - these
922 should have been removed in the
924 CERROR("lov tgt %d not cleaned!"
925 " deathrow=%d, lovrc=%d\n",
926 i, lov->lov_death_row,
927 cfs_atomic_read(&lov->lov_refcount));
928 lov_del_target(obd, i, 0, 0);
931 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
933 lov->lov_tgt_size = 0;
936 /* clear pools parent proc entry only after all pools is killed */
937 lprocfs_obd_cleanup(obd);
939 OBD_FREE_PTR(lov->lov_qos.lq_statfs_data);
943 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
944 __u32 *indexp, int *genp)
946 struct obd_uuid obd_uuid;
951 switch(cmd = lcfg->lcfg_command) {
952 case LCFG_LOV_ADD_OBD:
953 case LCFG_LOV_ADD_INA:
954 case LCFG_LOV_DEL_OBD: {
957 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
958 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
959 GOTO(out, rc = -EINVAL);
961 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
963 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", indexp) != 1)
964 GOTO(out, rc = -EINVAL);
965 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
966 GOTO(out, rc = -EINVAL);
969 if (cmd == LCFG_LOV_ADD_OBD)
970 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
971 else if (cmd == LCFG_LOV_ADD_INA)
972 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
974 rc = lov_del_target(obd, index, &obd_uuid, gen);
978 struct lprocfs_static_vars lvars = { 0 };
979 struct lov_desc *desc = &(obd->u.lov.desc);
982 GOTO(out, rc = -EINVAL);
984 lprocfs_lov_init_vars(&lvars);
986 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
999 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1000 GOTO(out, rc = -EINVAL);
1009 #define log2(n) cfs_ffz(~(n))
1012 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
1013 struct lov_stripe_md **ea,
1014 struct obd_trans_info *oti)
1016 struct lov_obd *lov;
1017 struct obdo *tmp_oa;
1018 struct obd_uuid *ost_uuid = NULL;
1022 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1023 src_oa->o_flags == OBD_FL_DELORPHAN);
1025 lov = &export->exp_obd->u.lov;
1031 if (oti->oti_ost_uuid) {
1032 ost_uuid = oti->oti_ost_uuid;
1033 CDEBUG(D_HA, "clearing orphans only for %s\n",
1037 obd_getref(export->exp_obd);
1038 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1039 struct lov_stripe_md obj_md;
1040 struct lov_stripe_md *obj_mdp = &obj_md;
1041 struct lov_tgt_desc *tgt;
1044 tgt = lov->lov_tgts[i];
1048 /* if called for a specific target, we don't
1049 care if it is not active. */
1050 if (!lov->lov_tgts[i]->ltd_active && ost_uuid == NULL) {
1051 CDEBUG(D_HA, "lov idx %d inactive\n", i);
1055 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->ltd_uuid))
1058 CDEBUG(D_CONFIG,"Clear orphans for %d:%s\n", i,
1059 obd_uuid2str(ost_uuid));
1061 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1063 LASSERT(lov->lov_tgts[i]->ltd_exp);
1064 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1065 err = obd_create(lov->lov_tgts[i]->ltd_exp,
1066 tmp_oa, &obj_mdp, oti);
1068 /* This export will be disabled until it is recovered,
1069 and then orphan recovery will be completed. */
1070 CERROR("error in orphan recovery on OST idx %d/%d: "
1071 "rc = %d\n", i, lov->desc.ld_tgt_count, err);
1078 obd_putref(export->exp_obd);
1084 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
1085 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1087 struct lov_stripe_md *obj_mdp, *lsm;
1088 struct lov_obd *lov = &exp->exp_obd->u.lov;
1093 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1094 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
1096 OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
1097 if (obj_mdp == NULL)
1100 ost_idx = src_oa->o_nlink;
1103 GOTO(out, rc = -EINVAL);
1104 if (ost_idx >= lov->desc.ld_tgt_count ||
1105 !lov->lov_tgts[ost_idx])
1106 GOTO(out, rc = -EINVAL);
1108 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1109 if (lsm->lsm_oinfo[i]->loi_ost_idx == ost_idx) {
1110 if (lsm->lsm_oinfo[i]->loi_id != src_oa->o_id)
1111 GOTO(out, rc = -EINVAL);
1115 if (i == lsm->lsm_stripe_count)
1116 GOTO(out, rc = -EINVAL);
1118 rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp, src_oa, &obj_mdp, oti);
1120 OBD_FREE(obj_mdp, sizeof(*obj_mdp));
1124 /* the LOV expects oa->o_id to be set to the LOV object id */
1125 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
1126 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1128 struct lov_obd *lov;
1129 struct obd_info oinfo;
1130 struct lov_request_set *set = NULL;
1131 struct lov_request *req;
1132 struct l_wait_info lwi = { 0 };
1136 LASSERT(ea != NULL);
1140 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1141 src_oa->o_flags == OBD_FL_DELORPHAN) {
1142 rc = lov_clear_orphans(exp, src_oa, ea, oti);
1146 lov = &exp->exp_obd->u.lov;
1147 if (!lov->desc.ld_active_tgt_count)
1150 obd_getref(exp->exp_obd);
1151 /* Recreate a specific object id at the given OST index */
1152 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1153 (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1154 rc = lov_recreate(exp, src_oa, ea, oti);
1158 /* issue statfs rpcs if the osfs data is older than qos_maxage - 1s,
1159 * later in alloc_qos(), we will wait for those rpcs to complete if
1160 * the osfs age is older than 2 * qos_maxage */
1161 qos_statfs_update(exp->exp_obd,
1162 cfs_time_shift_64(-lov->desc.ld_qos_maxage +
1163 OBD_STATFS_CACHE_SECONDS),
1166 rc = lov_prep_create_set(exp, &oinfo, ea, src_oa, oti, &set);
1170 cfs_list_for_each_entry(req, &set->set_list, rq_link) {
1171 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1172 rc = obd_create_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1173 &req->rq_oi, &req->rq_oi.oi_md, oti);
1176 /* osc_create have timeout equ obd_timeout/2 so waiting don't be
1177 * longer then this */
1178 l_wait_event(set->set_waitq, lov_finished_set(set), &lwi);
1180 /* we not have ptlrpc set for assign set->interpret and should
1181 * be call interpret function himself. calling from cb_create_update
1182 * not permited because lov_fini_create_set can sleep for long time,
1183 * but we must avoid sleeping in ptlrpcd interpret function. */
1184 rc = lov_fini_create_set(set, ea);
1186 obd_putref(exp->exp_obd);
1190 #define ASSERT_LSM_MAGIC(lsmp) \
1192 LASSERT((lsmp) != NULL); \
1193 LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 || \
1194 (lsmp)->lsm_magic == LOV_MAGIC_V3), \
1195 "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic); \
1198 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
1199 struct lov_stripe_md *lsm, struct obd_trans_info *oti,
1200 struct obd_export *md_exp, void *capa)
1202 struct lov_request_set *set;
1203 struct obd_info oinfo;
1204 struct lov_request *req;
1206 struct lov_obd *lov;
1207 int rc = 0, err = 0;
1210 ASSERT_LSM_MAGIC(lsm);
1212 if (!exp || !exp->exp_obd)
1215 if (oa->o_valid & OBD_MD_FLCOOKIE) {
1217 LASSERT(oti->oti_logcookies);
1220 lov = &exp->exp_obd->u.lov;
1221 obd_getref(exp->exp_obd);
1222 rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1226 cfs_list_for_each (pos, &set->set_list) {
1227 req = cfs_list_entry(pos, struct lov_request, rq_link);
1229 if (oa->o_valid & OBD_MD_FLCOOKIE)
1230 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1232 err = obd_destroy(lov->lov_tgts[req->rq_idx]->ltd_exp,
1233 req->rq_oi.oi_oa, NULL, oti, NULL, capa);
1234 err = lov_update_common_set(set, req, err);
1236 CERROR("error: destroying objid "LPX64" subobj "
1237 LPX64" on OST idx %d: rc = %d\n",
1238 oa->o_id, req->rq_oi.oi_oa->o_id,
1246 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1247 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1249 err = lov_fini_destroy_set(set);
1251 obd_putref(exp->exp_obd);
1252 RETURN(rc ? rc : err);
1255 static int lov_getattr(struct obd_export *exp, struct obd_info *oinfo)
1257 struct lov_request_set *set;
1258 struct lov_request *req;
1260 struct lov_obd *lov;
1261 int err = 0, rc = 0;
1265 ASSERT_LSM_MAGIC(oinfo->oi_md);
1267 if (!exp || !exp->exp_obd)
1270 lov = &exp->exp_obd->u.lov;
1272 rc = lov_prep_getattr_set(exp, oinfo, &set);
1276 cfs_list_for_each (pos, &set->set_list) {
1277 req = cfs_list_entry(pos, struct lov_request, rq_link);
1279 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1280 "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1281 req->rq_oi.oi_oa->o_id, req->rq_idx);
1283 rc = obd_getattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1285 err = lov_update_common_set(set, req, rc);
1287 CERROR("error: getattr objid "LPX64" subobj "
1288 LPX64" on OST idx %d: rc = %d\n",
1289 oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1295 rc = lov_fini_getattr_set(set);
1301 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1304 struct lov_request_set *lovset = (struct lov_request_set *)data;
1308 /* don't do attribute merge if this aysnc op failed */
1310 cfs_atomic_set(&lovset->set_completes, 0);
1311 err = lov_fini_getattr_set(lovset);
1312 RETURN(rc ? rc : err);
1315 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1316 struct ptlrpc_request_set *rqset)
1318 struct lov_request_set *lovset;
1319 struct lov_obd *lov;
1321 struct lov_request *req;
1326 ASSERT_LSM_MAGIC(oinfo->oi_md);
1328 if (!exp || !exp->exp_obd)
1331 lov = &exp->exp_obd->u.lov;
1333 rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1337 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1338 oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1339 oinfo->oi_md->lsm_stripe_size);
1341 cfs_list_for_each (pos, &lovset->set_list) {
1342 req = cfs_list_entry(pos, struct lov_request, rq_link);
1344 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1345 "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1346 req->rq_oi.oi_oa->o_id, req->rq_idx);
1347 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1348 &req->rq_oi, rqset);
1350 CERROR("error: getattr objid "LPX64" subobj "
1351 LPX64" on OST idx %d: rc = %d\n",
1352 oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1358 if (!cfs_list_empty(&rqset->set_requests)) {
1360 LASSERT (rqset->set_interpret == NULL);
1361 rqset->set_interpret = lov_getattr_interpret;
1362 rqset->set_arg = (void *)lovset;
1367 cfs_atomic_set(&lovset->set_completes, 0);
1368 err = lov_fini_getattr_set(lovset);
1369 RETURN(rc ? rc : err);
1372 static int lov_setattr(struct obd_export *exp, struct obd_info *oinfo,
1373 struct obd_trans_info *oti)
1375 struct lov_request_set *set;
1376 struct lov_obd *lov;
1378 struct lov_request *req;
1379 int err = 0, rc = 0;
1383 ASSERT_LSM_MAGIC(oinfo->oi_md);
1385 if (!exp || !exp->exp_obd)
1388 /* for now, we only expect the following updates here */
1389 LASSERT(!(oinfo->oi_oa->o_valid & ~(OBD_MD_FLID | OBD_MD_FLTYPE |
1390 OBD_MD_FLMODE | OBD_MD_FLATIME |
1391 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1392 OBD_MD_FLFLAGS | OBD_MD_FLSIZE |
1393 OBD_MD_FLGROUP | OBD_MD_FLUID |
1394 OBD_MD_FLGID | OBD_MD_FLFID |
1396 lov = &exp->exp_obd->u.lov;
1397 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1401 cfs_list_for_each (pos, &set->set_list) {
1402 req = cfs_list_entry(pos, struct lov_request, rq_link);
1404 rc = obd_setattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1406 err = lov_update_setattr_set(set, req, rc);
1408 CERROR("error: setattr objid "LPX64" subobj "
1409 LPX64" on OST idx %d: rc = %d\n",
1410 set->set_oi->oi_oa->o_id,
1411 req->rq_oi.oi_oa->o_id, req->rq_idx, err);
1416 err = lov_fini_setattr_set(set);
1422 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1425 struct lov_request_set *lovset = (struct lov_request_set *)data;
1430 cfs_atomic_set(&lovset->set_completes, 0);
1431 err = lov_fini_setattr_set(lovset);
1432 RETURN(rc ? rc : err);
1435 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1436 needed. Otherwise, a client is waiting for responses. */
1437 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1438 struct obd_trans_info *oti,
1439 struct ptlrpc_request_set *rqset)
1441 struct lov_request_set *set;
1442 struct lov_request *req;
1444 struct lov_obd *lov;
1449 ASSERT_LSM_MAGIC(oinfo->oi_md);
1450 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1452 LASSERT(oti->oti_logcookies);
1455 if (!exp || !exp->exp_obd)
1458 lov = &exp->exp_obd->u.lov;
1459 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1463 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1464 oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1465 oinfo->oi_md->lsm_stripe_size);
1467 cfs_list_for_each (pos, &set->set_list) {
1468 req = cfs_list_entry(pos, struct lov_request, rq_link);
1470 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1471 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1473 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1474 "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1475 req->rq_oi.oi_oa->o_id, req->rq_idx);
1477 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1478 &req->rq_oi, oti, rqset);
1480 CERROR("error: setattr objid "LPX64" subobj "
1481 LPX64" on OST idx %d: rc = %d\n",
1482 set->set_oi->oi_oa->o_id,
1483 req->rq_oi.oi_oa->o_id,
1489 /* If we are not waiting for responses on async requests, return. */
1490 if (rc || !rqset || cfs_list_empty(&rqset->set_requests)) {
1493 cfs_atomic_set(&set->set_completes, 0);
1494 err = lov_fini_setattr_set(set);
1495 RETURN(rc ? rc : err);
1498 LASSERT(rqset->set_interpret == NULL);
1499 rqset->set_interpret = lov_setattr_interpret;
1500 rqset->set_arg = (void *)set;
1505 static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
1508 struct lov_request_set *lovset = (struct lov_request_set *)data;
1513 cfs_atomic_set(&lovset->set_completes, 0);
1514 err = lov_fini_punch_set(lovset);
1515 RETURN(rc ? rc : err);
1518 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1519 * we can send this 'punch' to just the authoritative node and the nodes
1520 * that the punch will affect. */
1521 static int lov_punch(struct obd_export *exp, struct obd_info *oinfo,
1522 struct obd_trans_info *oti,
1523 struct ptlrpc_request_set *rqset)
1525 struct lov_request_set *set;
1526 struct lov_obd *lov;
1528 struct lov_request *req;
1533 ASSERT_LSM_MAGIC(oinfo->oi_md);
1535 if (!exp || !exp->exp_obd)
1538 lov = &exp->exp_obd->u.lov;
1539 rc = lov_prep_punch_set(exp, oinfo, oti, &set);
1543 cfs_list_for_each (pos, &set->set_list) {
1544 req = cfs_list_entry(pos, struct lov_request, rq_link);
1546 rc = obd_punch(lov->lov_tgts[req->rq_idx]->ltd_exp,
1547 &req->rq_oi, NULL, rqset);
1549 CERROR("error: punch objid "LPX64" subobj "LPX64
1550 " on OST idx %d: rc = %d\n",
1551 set->set_oi->oi_oa->o_id,
1552 req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1557 if (rc || cfs_list_empty(&rqset->set_requests)) {
1559 err = lov_fini_punch_set(set);
1560 RETURN(rc ? rc : err);
1563 LASSERT(rqset->set_interpret == NULL);
1564 rqset->set_interpret = lov_punch_interpret;
1565 rqset->set_arg = (void *)set;
1570 static int lov_sync_interpret(struct ptlrpc_request_set *rqset,
1573 struct lov_request_set *lovset = data;
1578 cfs_atomic_set(&lovset->set_completes, 0);
1579 err = lov_fini_sync_set(lovset);
1583 static int lov_sync(struct obd_export *exp, struct obd_info *oinfo,
1584 obd_off start, obd_off end,
1585 struct ptlrpc_request_set *rqset)
1587 struct lov_request_set *set = NULL;
1588 struct lov_obd *lov;
1590 struct lov_request *req;
1594 ASSERT_LSM_MAGIC(oinfo->oi_md);
1595 LASSERT(rqset != NULL);
1600 lov = &exp->exp_obd->u.lov;
1601 rc = lov_prep_sync_set(exp, oinfo, start, end, &set);
1605 CDEBUG(D_INFO, "fsync objid "LPX64" ["LPX64", "LPX64"]\n",
1606 set->set_oi->oi_oa->o_id, start, end);
1608 cfs_list_for_each (pos, &set->set_list) {
1609 req = cfs_list_entry(pos, struct lov_request, rq_link);
1611 rc = obd_sync(lov->lov_tgts[req->rq_idx]->ltd_exp, &req->rq_oi,
1612 req->rq_oi.oi_policy.l_extent.start,
1613 req->rq_oi.oi_policy.l_extent.end, rqset);
1615 CERROR("error: fsync objid "LPX64" subobj "LPX64
1616 " on OST idx %d: rc = %d\n",
1617 set->set_oi->oi_oa->o_id,
1618 req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1623 /* If we are not waiting for responses on async requests, return. */
1624 if (rc || cfs_list_empty(&rqset->set_requests)) {
1625 int err = lov_fini_sync_set(set);
1630 LASSERT(rqset->set_interpret == NULL);
1631 rqset->set_interpret = lov_sync_interpret;
1632 rqset->set_arg = (void *)set;
1637 static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
1638 obd_count oa_bufs, struct brw_page *pga)
1640 struct obd_info oinfo = { { { 0 } } };
1643 oinfo.oi_oa = lov_oinfo->oi_oa;
1645 /* The caller just wants to know if there's a chance that this
1646 * I/O can succeed */
1647 for (i = 0; i < oa_bufs; i++) {
1648 int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
1649 int ost = lov_oinfo->oi_md->lsm_oinfo[stripe]->loi_ost_idx;
1652 if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
1653 pga[i].off + pga[i].count - 1,
1657 if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
1658 CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1662 rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
1670 static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
1671 obd_count oa_bufs, struct brw_page *pga,
1672 struct obd_trans_info *oti)
1674 struct lov_request_set *set;
1675 struct lov_request *req;
1677 struct lov_obd *lov = &exp->exp_obd->u.lov;
1681 ASSERT_LSM_MAGIC(oinfo->oi_md);
1683 if (cmd == OBD_BRW_CHECK) {
1684 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1688 rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
1692 cfs_list_for_each (pos, &set->set_list) {
1693 struct obd_export *sub_exp;
1694 struct brw_page *sub_pga;
1695 req = cfs_list_entry(pos, struct lov_request, rq_link);
1697 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1698 sub_pga = set->set_pga + req->rq_pgaidx;
1699 rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1703 lov_update_common_set(set, req, rc);
1706 err = lov_fini_brw_set(set);
1712 static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
1715 struct lov_request_set *lovset = (struct lov_request_set *)data;
1717 rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc, rqset);
1721 static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
1722 struct ldlm_enqueue_info *einfo,
1723 struct ptlrpc_request_set *rqset)
1725 ldlm_mode_t mode = einfo->ei_mode;
1726 struct lov_request_set *set;
1727 struct lov_request *req;
1729 struct lov_obd *lov;
1734 ASSERT_LSM_MAGIC(oinfo->oi_md);
1735 LASSERT(mode == (mode & -mode));
1737 /* we should never be asked to replay a lock this way. */
1738 LASSERT((oinfo->oi_flags & LDLM_FL_REPLAY) == 0);
1740 if (!exp || !exp->exp_obd)
1743 lov = &exp->exp_obd->u.lov;
1744 rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
1748 cfs_list_for_each (pos, &set->set_list) {
1749 req = cfs_list_entry(pos, struct lov_request, rq_link);
1751 rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
1752 &req->rq_oi, einfo, rqset);
1757 if (rqset && !cfs_list_empty(&rqset->set_requests)) {
1759 LASSERT(rqset->set_interpret == NULL);
1760 rqset->set_interpret = lov_enqueue_interpret;
1761 rqset->set_arg = (void *)set;
1765 rc = lov_fini_enqueue_set(set, mode, rc, rqset);
1769 static int lov_change_cbdata(struct obd_export *exp,
1770 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1773 struct lov_obd *lov;
1777 ASSERT_LSM_MAGIC(lsm);
1779 if (!exp || !exp->exp_obd)
1782 lov = &exp->exp_obd->u.lov;
1783 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1784 struct lov_stripe_md submd;
1785 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1787 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1788 CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1792 LASSERT_SEQ_IS_MDT(loi->loi_seq);
1793 submd.lsm_object_id = loi->loi_id;
1794 submd.lsm_object_seq = loi->loi_seq;
1795 submd.lsm_stripe_count = 0;
1796 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1802 /* find any ldlm lock of the inode in lov
1806 static int lov_find_cbdata(struct obd_export *exp,
1807 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1810 struct lov_obd *lov;
1814 ASSERT_LSM_MAGIC(lsm);
1816 if (!exp || !exp->exp_obd)
1819 lov = &exp->exp_obd->u.lov;
1820 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1821 struct lov_stripe_md submd;
1822 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1824 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1825 CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1829 LASSERT_SEQ_IS_MDT(loi->loi_seq);
1830 submd.lsm_object_id = loi->loi_id;
1831 submd.lsm_object_seq = loi->loi_seq;
1832 submd.lsm_stripe_count = 0;
1833 rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1841 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1842 __u32 mode, struct lustre_handle *lockh)
1844 struct lov_request_set *set;
1845 struct obd_info oinfo;
1846 struct lov_request *req;
1848 struct lov_obd *lov;
1849 struct lustre_handle *lov_lockhp;
1850 int err = 0, rc = 0;
1853 ASSERT_LSM_MAGIC(lsm);
1855 if (!exp || !exp->exp_obd)
1858 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
1860 lov = &exp->exp_obd->u.lov;
1861 rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
1865 cfs_list_for_each (pos, &set->set_list) {
1866 req = cfs_list_entry(pos, struct lov_request, rq_link);
1867 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1869 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
1870 req->rq_oi.oi_md, mode, lov_lockhp);
1871 rc = lov_update_common_set(set, req, rc);
1873 CERROR("error: cancel objid "LPX64" subobj "
1874 LPX64" on OST idx %d: rc = %d\n",
1876 req->rq_oi.oi_md->lsm_object_id,
1882 lov_fini_cancel_set(set);
1886 static int lov_cancel_unused(struct obd_export *exp,
1887 struct lov_stripe_md *lsm,
1888 ldlm_cancel_flags_t flags, void *opaque)
1890 struct lov_obd *lov;
1894 if (!exp || !exp->exp_obd)
1897 lov = &exp->exp_obd->u.lov;
1899 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1901 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1904 err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
1912 ASSERT_LSM_MAGIC(lsm);
1914 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
1915 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1916 struct lov_stripe_md submd;
1917 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1920 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1921 CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1925 if (!lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
1926 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1928 submd.lsm_object_id = loi->loi_id;
1929 submd.lsm_object_seq = loi->loi_seq;
1930 submd.lsm_stripe_count = 0;
1931 err = obd_cancel_unused(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1932 &submd, flags, opaque);
1933 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
1934 CERROR("error: cancel unused objid "LPX64" subobj "LPX64
1935 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1936 loi->loi_id, loi->loi_ost_idx, err);
1944 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1946 struct lov_request_set *lovset = (struct lov_request_set *)data;
1951 cfs_atomic_set(&lovset->set_completes, 0);
1953 err = lov_fini_statfs_set(lovset);
1954 RETURN(rc ? rc : err);
1957 static int lov_statfs_async(struct obd_device *obd, struct obd_info *oinfo,
1958 __u64 max_age, struct ptlrpc_request_set *rqset)
1960 struct lov_request_set *set;
1961 struct lov_request *req;
1963 struct lov_obd *lov;
1967 LASSERT(oinfo != NULL);
1968 LASSERT(oinfo->oi_osfs != NULL);
1971 rc = lov_prep_statfs_set(obd, oinfo, &set);
1975 cfs_list_for_each (pos, &set->set_list) {
1976 struct obd_device *osc_obd;
1978 req = cfs_list_entry(pos, struct lov_request, rq_link);
1980 osc_obd = class_exp2obd(lov->lov_tgts[req->rq_idx]->ltd_exp);
1981 rc = obd_statfs_async(osc_obd, &req->rq_oi, max_age, rqset);
1986 if (rc || cfs_list_empty(&rqset->set_requests)) {
1989 cfs_atomic_set(&set->set_completes, 0);
1990 err = lov_fini_statfs_set(set);
1991 RETURN(rc ? rc : err);
1994 LASSERT(rqset->set_interpret == NULL);
1995 rqset->set_interpret = lov_statfs_interpret;
1996 rqset->set_arg = (void *)set;
2000 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2001 __u64 max_age, __u32 flags)
2003 struct ptlrpc_request_set *set = NULL;
2004 struct obd_info oinfo = { { { 0 } } };
2009 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
2010 * statfs requests */
2011 set = ptlrpc_prep_set();
2015 oinfo.oi_osfs = osfs;
2016 oinfo.oi_flags = flags;
2017 rc = lov_statfs_async(obd, &oinfo, max_age, set);
2019 rc = ptlrpc_set_wait(set);
2020 ptlrpc_set_destroy(set);
2025 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2026 void *karg, void *uarg)
2028 struct obd_device *obddev = class_exp2obd(exp);
2029 struct lov_obd *lov = &obddev->u.lov;
2030 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
2031 struct obd_uuid *uuidp;
2035 case IOC_OBD_STATFS: {
2036 struct obd_ioctl_data *data = karg;
2037 struct obd_device *osc_obd;
2038 struct obd_statfs stat_buf = {0};
2041 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2042 if ((index >= count))
2045 if (!lov->lov_tgts[index])
2046 /* Try again with the next index */
2048 if (!lov->lov_tgts[index]->ltd_active)
2051 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2056 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
2057 min((int) data->ioc_plen2,
2058 (int) sizeof(struct obd_uuid))))
2061 /* got statfs data */
2062 rc = obd_statfs(osc_obd, &stat_buf,
2063 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2067 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
2068 min((int) data->ioc_plen1,
2069 (int) sizeof(stat_buf))))
2073 case OBD_IOC_LOV_GET_CONFIG: {
2074 struct obd_ioctl_data *data;
2075 struct lov_desc *desc;
2080 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2083 data = (struct obd_ioctl_data *)buf;
2085 if (sizeof(*desc) > data->ioc_inllen1) {
2086 obd_ioctl_freedata(buf, len);
2090 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2091 obd_ioctl_freedata(buf, len);
2095 if (sizeof(__u32) * count > data->ioc_inllen3) {
2096 obd_ioctl_freedata(buf, len);
2100 desc = (struct lov_desc *)data->ioc_inlbuf1;
2101 memcpy(desc, &(lov->desc), sizeof(*desc));
2103 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2104 genp = (__u32 *)data->ioc_inlbuf3;
2105 /* the uuid will be empty for deleted OSTs */
2106 for (i = 0; i < count; i++, uuidp++, genp++) {
2107 if (!lov->lov_tgts[i])
2109 *uuidp = lov->lov_tgts[i]->ltd_uuid;
2110 *genp = lov->lov_tgts[i]->ltd_gen;
2113 if (cfs_copy_to_user((void *)uarg, buf, len))
2115 obd_ioctl_freedata(buf, len);
2118 case LL_IOC_LOV_SETSTRIPE:
2119 rc = lov_setstripe(exp, len, karg, uarg);
2121 case LL_IOC_LOV_GETSTRIPE:
2122 rc = lov_getstripe(exp, karg, uarg);
2124 case LL_IOC_LOV_SETEA:
2125 rc = lov_setea(exp, karg, uarg);
2127 case OBD_IOC_QUOTACTL: {
2128 struct if_quotactl *qctl = karg;
2129 struct lov_tgt_desc *tgt = NULL;
2130 struct obd_quotactl *oqctl;
2132 if (qctl->qc_valid == QC_OSTIDX) {
2133 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
2136 tgt = lov->lov_tgts[qctl->qc_idx];
2137 if (!tgt || !tgt->ltd_exp)
2139 } else if (qctl->qc_valid == QC_UUID) {
2140 for (i = 0; i < count; i++) {
2141 tgt = lov->lov_tgts[i];
2143 !obd_uuid_equals(&tgt->ltd_uuid,
2147 if (tgt->ltd_exp == NULL)
2159 LASSERT(tgt && tgt->ltd_exp);
2160 OBD_ALLOC_PTR(oqctl);
2164 QCTL_COPY(oqctl, qctl);
2165 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2167 QCTL_COPY(qctl, oqctl);
2168 qctl->qc_valid = QC_OSTIDX;
2169 qctl->obd_uuid = tgt->ltd_uuid;
2171 OBD_FREE_PTR(oqctl);
2180 for (i = 0; i < count; i++) {
2182 struct obd_device *osc_obd;
2184 /* OST was disconnected */
2185 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2188 /* ll_umount_begin() sets force flag but for lov, not
2189 * osc. Let's pass it through */
2190 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
2191 osc_obd->obd_force = obddev->obd_force;
2192 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
2194 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
2197 if (lov->lov_tgts[i]->ltd_active) {
2198 CDEBUG(err == -ENOTTY ?
2199 D_IOCTL : D_WARNING,
2200 "iocontrol OSC %s on OST "
2201 "idx %d cmd %x: err = %d\n",
2202 lov_uuid2str(lov, i),
2219 #define FIEMAP_BUFFER_SIZE 4096
2222 * Non-zero fe_logical indicates that this is a continuation FIEMAP
2223 * call. The local end offset and the device are sent in the first
2224 * fm_extent. This function calculates the stripe number from the index.
2225 * This function returns a stripe_no on which mapping is to be restarted.
2227 * This function returns fm_end_offset which is the in-OST offset at which
2228 * mapping should be restarted. If fm_end_offset=0 is returned then caller
2229 * will re-calculate proper offset in next stripe.
2230 * Note that the first extent is passed to lov_get_info via the value field.
2232 * \param fiemap fiemap request header
2233 * \param lsm striping information for the file
2234 * \param fm_start logical start of mapping
2235 * \param fm_end logical end of mapping
2236 * \param start_stripe starting stripe will be returned in this
2238 obd_size fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
2239 struct lov_stripe_md *lsm, obd_size fm_start,
2240 obd_size fm_end, int *start_stripe)
2242 obd_size local_end = fiemap->fm_extents[0].fe_logical;
2243 obd_off lun_start, lun_end;
2244 obd_size fm_end_offset;
2245 int stripe_no = -1, i;
2247 if (fiemap->fm_extent_count == 0 ||
2248 fiemap->fm_extents[0].fe_logical == 0)
2251 /* Find out stripe_no from ost_index saved in the fe_device */
2252 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2253 if (lsm->lsm_oinfo[i]->loi_ost_idx ==
2254 fiemap->fm_extents[0].fe_device) {
2260 /* If we have finished mapping on previous device, shift logical
2261 * offset to start of next device */
2262 if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
2263 &lun_start, &lun_end)) != 0 &&
2264 local_end < lun_end) {
2265 fm_end_offset = local_end;
2266 *start_stripe = stripe_no;
2268 /* This is a special value to indicate that caller should
2269 * calculate offset in next stripe. */
2271 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
2274 return fm_end_offset;
2278 * We calculate on which OST the mapping will end. If the length of mapping
2279 * is greater than (stripe_size * stripe_count) then the last_stripe will
2280 * will be one just before start_stripe. Else we check if the mapping
2281 * intersects each OST and find last_stripe.
2282 * This function returns the last_stripe and also sets the stripe_count
2283 * over which the mapping is spread
2285 * \param lsm striping information for the file
2286 * \param fm_start logical start of mapping
2287 * \param fm_end logical end of mapping
2288 * \param start_stripe starting stripe of the mapping
2289 * \param stripe_count the number of stripes across which to map is returned
2291 * \retval last_stripe return the last stripe of the mapping
2293 int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, obd_size fm_start,
2294 obd_size fm_end, int start_stripe,
2298 obd_off obd_start, obd_end;
2301 if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
2302 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
2304 *stripe_count = lsm->lsm_stripe_count;
2306 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
2307 i = (i + 1) % lsm->lsm_stripe_count, j++) {
2308 if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
2309 &obd_start, &obd_end)) == 0)
2313 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
2320 * Set fe_device and copy extents from local buffer into main return buffer.
2322 * \param fiemap fiemap request header
2323 * \param lcl_fm_ext array of local fiemap extents to be copied
2324 * \param ost_index OST index to be written into the fm_device field for each
2326 * \param ext_count number of extents to be copied
2327 * \param current_extent where to start copying in main extent array
2329 void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
2330 struct ll_fiemap_extent *lcl_fm_ext,
2331 int ost_index, unsigned int ext_count,
2337 for (ext = 0; ext < ext_count; ext++) {
2338 lcl_fm_ext[ext].fe_device = ost_index;
2339 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
2342 /* Copy fm_extent's from fm_local to return buffer */
2343 to = (char *)fiemap + fiemap_count_to_size(current_extent);
2344 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
2348 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
2349 * This also handles the restarting of FIEMAP calls in case mapping overflows
2350 * the available number of extents in single call.
2352 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
2353 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2355 struct ll_fiemap_info_key *fm_key = key;
2356 struct ll_user_fiemap *fiemap = val;
2357 struct ll_user_fiemap *fm_local = NULL;
2358 struct ll_fiemap_extent *lcl_fm_ext;
2360 unsigned int get_num_extents = 0;
2361 int ost_index = 0, actual_start_stripe, start_stripe;
2362 obd_size fm_start, fm_end, fm_length, fm_end_offset = 0;
2364 int current_extent = 0, rc = 0, i;
2365 int ost_eof = 0; /* EOF for object */
2366 int ost_done = 0; /* done with required mapping for this OST? */
2368 int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
2369 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
2374 if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
2375 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
2377 OBD_ALLOC_LARGE(fm_local, buffer_size);
2378 if (fm_local == NULL)
2379 GOTO(out, rc = -ENOMEM);
2380 lcl_fm_ext = &fm_local->fm_extents[0];
2382 count_local = fiemap_size_to_count(buffer_size);
2384 memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
2385 fm_start = fiemap->fm_start;
2386 fm_length = fiemap->fm_length;
2387 /* Calculate start stripe, last stripe and length of mapping */
2388 actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
2389 fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
2390 fm_start + fm_length - 1);
2391 /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
2392 if (fm_end > fm_key->oa.o_size)
2393 fm_end = fm_key->oa.o_size;
2395 last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
2396 actual_start_stripe, &stripe_count);
2398 fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start, fm_end,
2401 if (fiemap->fm_extent_count == 0) {
2402 get_num_extents = 1;
2406 /* Check each stripe */
2407 for (cur_stripe = start_stripe, i = 0; i < stripe_count;
2408 i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
2409 obd_size req_fm_len; /* Stores length of required mapping */
2410 obd_size len_mapped_single_call;
2411 obd_off lun_start, lun_end, obd_object_end;
2412 unsigned int ext_count;
2414 cur_stripe_wrap = cur_stripe;
2416 /* Find out range of mapping on this stripe */
2417 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
2418 &lun_start, &obd_object_end)) == 0)
2421 /* If this is a continuation FIEMAP call and we are on
2422 * starting stripe then lun_start needs to be set to
2424 if (fm_end_offset != 0 && cur_stripe == start_stripe)
2425 lun_start = fm_end_offset;
2427 if (fm_length != ~0ULL) {
2428 /* Handle fm_start + fm_length overflow */
2429 if (fm_start + fm_length < fm_start)
2430 fm_length = ~0ULL - fm_start;
2431 lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
2437 if (lun_start == lun_end)
2440 req_fm_len = obd_object_end - lun_start;
2441 fm_local->fm_length = 0;
2442 len_mapped_single_call = 0;
2444 /* If the output buffer is very large and the objects have many
2445 * extents we may need to loop on a single OST repeatedly */
2449 if (get_num_extents == 0) {
2450 /* Don't get too many extents. */
2451 if (current_extent + count_local >
2452 fiemap->fm_extent_count)
2453 count_local = fiemap->fm_extent_count -
2457 lun_start += len_mapped_single_call;
2458 fm_local->fm_length = req_fm_len - len_mapped_single_call;
2459 req_fm_len = fm_local->fm_length;
2460 fm_local->fm_extent_count = count_local;
2461 fm_local->fm_mapped_extents = 0;
2462 fm_local->fm_flags = fiemap->fm_flags;
2464 fm_key->oa.o_id = lsm->lsm_oinfo[cur_stripe]->loi_id;
2465 fm_key->oa.o_seq = lsm->lsm_oinfo[cur_stripe]->loi_seq;
2466 ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
2468 if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
2469 GOTO(out, rc = -EINVAL);
2471 /* If OST is inactive, return extent with UNKNOWN flag */
2472 if (!lov->lov_tgts[ost_index]->ltd_active) {
2473 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
2474 fm_local->fm_mapped_extents = 1;
2476 lcl_fm_ext[0].fe_logical = lun_start;
2477 lcl_fm_ext[0].fe_length = obd_object_end -
2479 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
2484 fm_local->fm_start = lun_start;
2485 fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
2486 memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
2487 *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
2488 rc = obd_get_info(lov->lov_tgts[ost_index]->ltd_exp,
2489 keylen, key, vallen, fm_local, lsm);
2494 ext_count = fm_local->fm_mapped_extents;
2495 if (ext_count == 0) {
2497 /* If last stripe has hole at the end,
2498 * then we need to return */
2499 if (cur_stripe_wrap == last_stripe) {
2500 fiemap->fm_mapped_extents = 0;
2506 /* If we just need num of extents then go to next device */
2507 if (get_num_extents) {
2508 current_extent += ext_count;
2512 len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
2513 lun_start + lcl_fm_ext[ext_count - 1].fe_length;
2515 /* Have we finished mapping on this device? */
2516 if (req_fm_len <= len_mapped_single_call)
2519 /* Clear the EXTENT_LAST flag which can be present on
2521 if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
2522 lcl_fm_ext[ext_count - 1].fe_flags &=
2523 ~FIEMAP_EXTENT_LAST;
2525 curr_loc = lov_stripe_size(lsm,
2526 lcl_fm_ext[ext_count - 1].fe_logical+
2527 lcl_fm_ext[ext_count - 1].fe_length,
2529 if (curr_loc >= fm_key->oa.o_size)
2532 fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
2533 ost_index, ext_count,
2536 current_extent += ext_count;
2538 /* Ran out of available extents? */
2539 if (current_extent >= fiemap->fm_extent_count)
2541 } while (ost_done == 0 && ost_eof == 0);
2543 if (cur_stripe_wrap == last_stripe)
2548 /* Indicate that we are returning device offsets unless file just has
2550 if (lsm->lsm_stripe_count > 1)
2551 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2553 if (get_num_extents)
2554 goto skip_last_device_calc;
2556 /* Check if we have reached the last stripe and whether mapping for that
2557 * stripe is done. */
2558 if (cur_stripe_wrap == last_stripe) {
2559 if (ost_done || ost_eof)
2560 fiemap->fm_extents[current_extent - 1].fe_flags |=
2564 skip_last_device_calc:
2565 fiemap->fm_mapped_extents = current_extent;
2568 OBD_FREE_LARGE(fm_local, buffer_size);
2572 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2573 void *key, __u32 *vallen, void *val,
2574 struct lov_stripe_md *lsm)
2576 struct obd_device *obddev = class_exp2obd(exp);
2577 struct lov_obd *lov = &obddev->u.lov;
2581 if (!vallen || !val)
2586 if (KEY_IS(KEY_LOCK_TO_STRIPE)) {
2589 struct ldlm_lock *lock;
2591 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2592 struct lov_oinfo *loi;
2593 __u32 *stripe = val;
2595 if (*vallen < sizeof(*stripe))
2596 GOTO(out, rc = -EFAULT);
2597 *vallen = sizeof(*stripe);
2599 /* XXX This is another one of those bits that will need to
2600 * change if we ever actually support nested LOVs. It uses
2601 * the lock's export to find out which stripe it is. */
2602 /* XXX - it's assumed all the locks for deleted OSTs have
2603 * been cancelled. Also, the export for deleted OSTs will
2604 * be NULL and won't match the lock's export. */
2605 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2606 loi = lsm->lsm_oinfo[i];
2607 if (!lov->lov_tgts[loi->loi_ost_idx])
2609 if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2610 data->lock->l_conn_export &&
2611 osc_res_name_eq(loi->loi_id, loi->loi_seq, res_id)) {
2616 LDLM_ERROR(data->lock, "lock on inode without such object");
2617 dump_lsm(D_ERROR, lsm);
2618 GOTO(out, rc = -ENXIO);
2619 } else if (KEY_IS(KEY_LAST_ID)) {
2620 struct obd_id_info *info = val;
2621 __u32 size = sizeof(obd_id);
2622 struct lov_tgt_desc *tgt;
2624 LASSERT(*vallen == sizeof(struct obd_id_info));
2625 tgt = lov->lov_tgts[info->idx];
2627 if (!tgt || !tgt->ltd_active)
2628 GOTO(out, rc = -ESRCH);
2630 rc = obd_get_info(tgt->ltd_exp, keylen, key, &size, info->data, NULL);
2632 } else if (KEY_IS(KEY_LOVDESC)) {
2633 struct lov_desc *desc_ret = val;
2634 *desc_ret = lov->desc;
2637 } else if (KEY_IS(KEY_FIEMAP)) {
2638 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
2640 } else if (KEY_IS(KEY_CONNECT_FLAG)) {
2641 struct lov_tgt_desc *tgt;
2642 __u64 ost_idx = *((__u64*)val);
2644 LASSERT(*vallen == sizeof(__u64));
2645 LASSERT(ost_idx < lov->desc.ld_tgt_count);
2646 tgt = lov->lov_tgts[ost_idx];
2648 if (!tgt || !tgt->ltd_exp)
2649 GOTO(out, rc = -ESRCH);
2651 *((__u64*)val) = tgt->ltd_exp->exp_connect_flags;
2653 } else if (KEY_IS(KEY_TGT_COUNT)) {
2654 *((int *)val) = lov->desc.ld_tgt_count;
2665 static int lov_set_info_async(struct obd_export *exp, obd_count keylen,
2666 void *key, obd_count vallen, void *val,
2667 struct ptlrpc_request_set *set)
2669 struct obd_device *obddev = class_exp2obd(exp);
2670 struct lov_obd *lov = &obddev->u.lov;
2673 struct lov_tgt_desc *tgt;
2674 unsigned incr, check_uuid,
2675 do_inactive, no_set;
2676 unsigned next_id = 0, mds_con = 0, capa = 0;
2679 incr = check_uuid = do_inactive = no_set = 0;
2682 set = ptlrpc_prep_set();
2688 count = lov->desc.ld_tgt_count;
2690 if (KEY_IS(KEY_NEXT_ID)) {
2691 count = vallen / sizeof(struct obd_id_info);
2692 vallen = sizeof(obd_id);
2693 incr = sizeof(struct obd_id_info);
2696 } else if (KEY_IS(KEY_CHECKSUM)) {
2698 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2699 /* use defaults: do_inactive = incr = 0; */
2700 } else if (KEY_IS(KEY_MDS_CONN)) {
2702 } else if (KEY_IS(KEY_CAPA_KEY)) {
2706 for (i = 0; i < count; i++, val = (char *)val + incr) {
2708 tgt = lov->lov_tgts[((struct obd_id_info*)val)->idx];
2710 tgt = lov->lov_tgts[i];
2712 /* OST was disconnected */
2713 if (!tgt || !tgt->ltd_exp)
2716 /* OST is inactive and we don't want inactive OSCs */
2717 if (!tgt->ltd_active && !do_inactive)
2721 struct mds_group_info *mgi;
2723 LASSERT(vallen == sizeof(*mgi));
2724 mgi = (struct mds_group_info *)val;
2726 /* Only want a specific OSC */
2727 if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2731 err = obd_set_info_async(tgt->ltd_exp,
2732 keylen, key, sizeof(int),
2734 } else if (next_id) {
2735 err = obd_set_info_async(tgt->ltd_exp,
2736 keylen, key, vallen,
2737 ((struct obd_id_info*)val)->data, set);
2739 struct mds_capa_info *info = (struct mds_capa_info*)val;
2741 LASSERT(vallen == sizeof(*info));
2743 /* Only want a specific OSC */
2745 !obd_uuid_equals(info->uuid, &tgt->ltd_uuid))
2748 err = obd_set_info_async(tgt->ltd_exp, keylen, key,
2749 sizeof(*info->capa),
2752 /* Only want a specific OSC */
2754 !obd_uuid_equals(val, &tgt->ltd_uuid))
2757 err = obd_set_info_async(tgt->ltd_exp,
2758 keylen, key, vallen, val, set);
2767 err = ptlrpc_set_wait(set);
2770 ptlrpc_set_destroy(set);
2775 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm)
2780 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2781 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
2782 if (loi->loi_ar.ar_rc && !rc)
2783 rc = loi->loi_ar.ar_rc;
2784 loi->loi_ar.ar_rc = 0;
2788 EXPORT_SYMBOL(lov_test_and_clear_async_rc);
2791 static int lov_extent_calc(struct obd_export *exp, struct lov_stripe_md *lsm,
2792 int cmd, __u64 *offset)
2794 __u32 ssize = lsm->lsm_stripe_size;
2798 do_div(start, ssize);
2799 start = start * ssize;
2801 CDEBUG(D_DLMTRACE, "offset "LPU64", stripe %u, start "LPU64
2802 ", end "LPU64"\n", *offset, ssize, start,
2804 if (cmd == OBD_CALC_STRIPE_END) {
2805 *offset = start + ssize - 1;
2806 } else if (cmd == OBD_CALC_STRIPE_START) {
2815 void lov_stripe_lock(struct lov_stripe_md *md)
2817 LASSERT(md->lsm_lock_owner != cfs_curproc_pid());
2818 cfs_spin_lock(&md->lsm_lock);
2819 LASSERT(md->lsm_lock_owner == 0);
2820 md->lsm_lock_owner = cfs_curproc_pid();
2822 EXPORT_SYMBOL(lov_stripe_lock);
2824 void lov_stripe_unlock(struct lov_stripe_md *md)
2826 LASSERT(md->lsm_lock_owner == cfs_curproc_pid());
2827 md->lsm_lock_owner = 0;
2828 cfs_spin_unlock(&md->lsm_lock);
2830 EXPORT_SYMBOL(lov_stripe_unlock);
2833 struct obd_ops lov_obd_ops = {
2834 .o_owner = THIS_MODULE,
2835 .o_setup = lov_setup,
2836 .o_precleanup = lov_precleanup,
2837 .o_cleanup = lov_cleanup,
2838 //.o_process_config = lov_process_config,
2839 .o_connect = lov_connect,
2840 .o_disconnect = lov_disconnect,
2841 .o_statfs = lov_statfs,
2842 .o_statfs_async = lov_statfs_async,
2843 .o_packmd = lov_packmd,
2844 .o_unpackmd = lov_unpackmd,
2845 .o_create = lov_create,
2846 .o_destroy = lov_destroy,
2847 .o_getattr = lov_getattr,
2848 .o_getattr_async = lov_getattr_async,
2849 .o_setattr = lov_setattr,
2850 .o_setattr_async = lov_setattr_async,
2852 .o_merge_lvb = lov_merge_lvb,
2853 .o_adjust_kms = lov_adjust_kms,
2854 .o_punch = lov_punch,
2856 .o_enqueue = lov_enqueue,
2857 .o_change_cbdata = lov_change_cbdata,
2858 .o_find_cbdata = lov_find_cbdata,
2859 .o_cancel = lov_cancel,
2860 .o_cancel_unused = lov_cancel_unused,
2861 .o_iocontrol = lov_iocontrol,
2862 .o_get_info = lov_get_info,
2863 .o_set_info_async = lov_set_info_async,
2864 .o_extent_calc = lov_extent_calc,
2865 .o_llog_init = lov_llog_init,
2866 .o_llog_finish = lov_llog_finish,
2867 .o_notify = lov_notify,
2868 .o_pool_new = lov_pool_new,
2869 .o_pool_rem = lov_pool_remove,
2870 .o_pool_add = lov_pool_add,
2871 .o_pool_del = lov_pool_del,
2872 .o_getref = lov_getref,
2873 .o_putref = lov_putref,
2876 static quota_interface_t *quota_interface;
2877 extern quota_interface_t lov_quota_interface;
2879 cfs_mem_cache_t *lov_oinfo_slab;
2881 extern struct lu_kmem_descr lov_caches[];
2883 int __init lov_init(void)
2885 struct lprocfs_static_vars lvars = { 0 };
2889 /* print an address of _any_ initialized kernel symbol from this
2890 * module, to allow debugging with gdb that doesn't support data
2891 * symbols from modules.*/
2892 CDEBUG(D_CONSOLE, "Lustre LOV module (%p).\n", &lov_caches);
2894 rc = lu_kmem_init(lov_caches);
2898 lov_oinfo_slab = cfs_mem_cache_create("lov_oinfo",
2899 sizeof(struct lov_oinfo),
2900 0, CFS_SLAB_HWCACHE_ALIGN);
2901 if (lov_oinfo_slab == NULL) {
2902 lu_kmem_fini(lov_caches);
2905 lprocfs_lov_init_vars(&lvars);
2907 cfs_request_module("lquota");
2908 quota_interface = PORTAL_SYMBOL_GET(lov_quota_interface);
2909 init_obd_quota_ops(quota_interface, &lov_obd_ops);
2911 rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2912 LUSTRE_LOV_NAME, &lov_device_type);
2915 if (quota_interface)
2916 PORTAL_SYMBOL_PUT(lov_quota_interface);
2917 rc2 = cfs_mem_cache_destroy(lov_oinfo_slab);
2919 lu_kmem_fini(lov_caches);
2926 static void /*__exit*/ lov_exit(void)
2930 lu_device_type_fini(&lov_device_type);
2931 lu_kmem_fini(lov_caches);
2933 if (quota_interface)
2934 PORTAL_SYMBOL_PUT(lov_quota_interface);
2936 class_unregister_type(LUSTRE_LOV_NAME);
2937 rc = cfs_mem_cache_destroy(lov_oinfo_slab);
2941 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2942 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2943 MODULE_LICENSE("GPL");
2945 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);