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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LMV
42 #include <linux/slab.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/pagemap.h>
48 #include <asm/div64.h>
49 #include <linux/seq_file.h>
50 #include <linux/namei.h>
52 #include <liblustre.h>
55 #include <lustre/lustre_idl.h>
56 #include <lustre_log.h>
57 #include <obd_support.h>
58 #include <lustre_lib.h>
59 #include <lustre_net.h>
60 #include <obd_class.h>
61 #include <lprocfs_status.h>
62 #include <lustre_lite.h>
63 #include <lustre_fid.h>
64 #include "lmv_internal.h"
67 cfs_mem_cache_t *obj_cache;
68 atomic_t obj_cache_count = ATOMIC_INIT(0);
70 static void lmv_activate_target(struct lmv_obd *lmv,
71 struct lmv_tgt_desc *tgt,
74 if (tgt->ltd_active == activate)
77 tgt->ltd_active = activate;
78 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
83 * -EINVAL : UUID can't be found in the LMV's target list
84 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
85 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
87 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
90 struct lmv_tgt_desc *tgt;
91 struct obd_device *obd;
95 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
96 lmv, uuid->uuid, activate);
98 spin_lock(&lmv->lmv_lock);
99 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
100 if (tgt->ltd_exp == NULL)
103 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
104 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
106 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
110 if (i == lmv->desc.ld_tgt_count)
111 GOTO(out_lmv_lock, rc = -EINVAL);
113 obd = class_exp2obd(tgt->ltd_exp);
115 GOTO(out_lmv_lock, rc = -ENOTCONN);
117 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
118 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
119 obd->obd_type->typ_name, i);
120 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
122 if (tgt->ltd_active == activate) {
123 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
124 activate ? "" : "in");
125 GOTO(out_lmv_lock, rc);
128 CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
129 obd, activate ? "" : "in");
131 lmv_activate_target(lmv, tgt, activate);
136 spin_unlock(&lmv->lmv_lock);
140 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
141 struct obd_connect_data *data)
143 struct lmv_tgt_desc *tgt;
147 LASSERT(data != NULL);
149 spin_lock(&lmv->lmv_lock);
150 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
151 if (tgt->ltd_exp == NULL)
154 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
155 lmv->datas[tgt->ltd_idx] = *data;
159 spin_unlock(&lmv->lmv_lock);
163 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
164 enum obd_notify_event ev, void *data)
166 struct lmv_obd *lmv = &obd->u.lmv;
167 struct obd_uuid *uuid;
171 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
172 CERROR("unexpected notification of %s %s!\n",
173 watched->obd_type->typ_name,
178 uuid = &watched->u.cli.cl_target_uuid;
179 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
181 * Set MDC as active before notifying the observer, so the
182 * observer can use the MDC normally.
184 rc = lmv_set_mdc_active(lmv, uuid,
185 ev == OBD_NOTIFY_ACTIVE);
187 CERROR("%sactivation of %s failed: %d\n",
188 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
192 } else if (ev == OBD_NOTIFY_OCD) {
193 struct obd_connect_data *conn_data =
194 &watched->u.cli.cl_import->imp_connect_data;
196 /* Set connect data to desired target, update
197 * exp_connect_flags. */
198 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
200 CERROR("can't set connect data to target %s, rc %d\n",
206 * XXX: Make sure that ocd_connect_flags from all targets are
207 * the same. Otherwise one of MDTs runs wrong version or
208 * something like this. --umka
210 obd->obd_self_export->exp_connect_flags =
211 conn_data->ocd_connect_flags;
214 else if (ev == OBD_NOTIFY_DISCON) {
215 /* For disconnect event, flush fld cache for failout MDS case. */
216 fld_client_flush(&lmv->lmv_fld);
219 /* Pass the notification up the chain. */
220 if (obd->obd_observer)
221 rc = obd_notify(obd->obd_observer, watched, ev, data);
226 /* this is fake connect function. Its purpose is to initialize lmv and say
227 * caller that everything is okay. Real connection will be performed later. */
228 static int lmv_connect(const struct lu_env *env,
229 struct lustre_handle *conn, struct obd_device *obd,
230 struct obd_uuid *cluuid, struct obd_connect_data *data,
234 struct proc_dir_entry *lmv_proc_dir;
236 struct lmv_obd *lmv = &obd->u.lmv;
237 struct obd_export *exp;
241 rc = class_connect(conn, obd, cluuid);
243 CERROR("class_connection() returned %d\n", rc);
247 exp = class_conn2export(conn);
249 /* we don't want to actually do the underlying connections more than
250 * once, so keep track. */
252 if (lmv->refcount > 1) {
253 class_export_put(exp);
259 lmv->cluuid = *cluuid;
262 lmv->conn_data = *data;
265 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
267 if (IS_ERR(lmv_proc_dir)) {
268 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
269 obd->obd_type->typ_name, obd->obd_name);
274 /* all real clients should perform actual connection right away, because
275 * it is possible, that LMV will not have opportunity to connect targets
276 * and MDC stuff will be called directly, for instance while reading
277 * ../mdc/../kbytesfree procfs file, etc. */
278 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
279 rc = lmv_check_connect(obd);
284 lprocfs_remove(&lmv_proc_dir);
291 static void lmv_set_timeouts(struct obd_device *obd)
293 struct lmv_tgt_desc *tgts;
298 if (lmv->server_timeout == 0)
301 if (lmv->connected == 0)
304 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
305 if (tgts->ltd_exp == NULL)
308 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
309 KEY_INTERMDS, 0, NULL, NULL);
313 static int lmv_init_ea_size(struct obd_export *exp, int easize,
314 int def_easize, int cookiesize)
316 struct obd_device *obd = exp->exp_obd;
317 struct lmv_obd *lmv = &obd->u.lmv;
318 int i, rc = 0, change = 0;
321 if (lmv->max_easize < easize) {
322 lmv->max_easize = easize;
325 if (lmv->max_def_easize < def_easize) {
326 lmv->max_def_easize = def_easize;
329 if (lmv->max_cookiesize < cookiesize) {
330 lmv->max_cookiesize = cookiesize;
336 if (lmv->connected == 0)
339 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
340 if (lmv->tgts[i].ltd_exp == NULL) {
341 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
345 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
348 CERROR("obd_init_ea_size() failed on MDT target %d, "
349 "error %d.\n", i, rc);
356 #define MAX_STRING_SIZE 128
358 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
360 struct lmv_obd *lmv = &obd->u.lmv;
361 struct obd_uuid *cluuid = &lmv->cluuid;
362 struct obd_connect_data *mdc_data = NULL;
363 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
364 struct lustre_handle conn = {0, };
365 struct obd_device *mdc_obd;
366 struct obd_export *mdc_exp;
367 struct lu_fld_target target;
370 struct proc_dir_entry *lmv_proc_dir;
374 /* for MDS: don't connect to yourself */
375 if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
376 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
377 /* XXX - the old code didn't increment active tgt count.
382 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
385 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
389 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
390 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
391 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
394 if (!mdc_obd->obd_set_up) {
395 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
399 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
400 &lmv->conn_data, NULL);
402 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
406 mdc_exp = class_conn2export(&conn);
408 /* Init fid sequence client for this mdc. */
409 rc = obd_fid_init(mdc_exp);
413 /* Add new FLD target. */
414 target.ft_srv = NULL;
415 target.ft_exp = mdc_exp;
416 target.ft_idx = tgt->ltd_idx;
418 fld_client_add_target(&lmv->lmv_fld, &target);
420 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
422 rc = obd_register_observer(mdc_obd, obd);
424 obd_disconnect(mdc_exp);
425 CERROR("target %s register_observer error %d\n",
426 tgt->ltd_uuid.uuid, rc);
430 if (obd->obd_observer) {
431 /* tell the mds_lmv about the new target */
432 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
433 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
435 obd_disconnect(mdc_exp);
441 tgt->ltd_exp = mdc_exp;
442 lmv->desc.ld_active_tgt_count++;
444 /* copy connect data, it may be used later */
445 lmv->datas[tgt->ltd_idx] = *mdc_data;
447 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
448 lmv->max_def_easize, lmv->max_cookiesize);
450 CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
451 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
452 atomic_read(&obd->obd_refcount));
455 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
457 struct proc_dir_entry *mdc_symlink;
458 char name[MAX_STRING_SIZE + 1];
460 LASSERT(mdc_obd->obd_type != NULL);
461 LASSERT(mdc_obd->obd_type->typ_name != NULL);
462 name[MAX_STRING_SIZE] = '\0';
463 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
464 mdc_obd->obd_type->typ_name,
466 mdc_symlink = proc_symlink(mdc_obd->obd_name,
468 if (mdc_symlink == NULL) {
469 CERROR("could not register LMV target "
470 "/proc/fs/lustre/%s/%s/target_obds/%s.",
471 obd->obd_type->typ_name, obd->obd_name,
473 lprocfs_remove(&lmv_proc_dir);
481 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
483 struct lmv_obd *lmv = &obd->u.lmv;
484 struct lmv_tgt_desc *tgt;
488 CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
492 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
493 lmv_init_unlock(lmv);
494 CERROR("can't add %s, LMV module compiled for %d MDCs. "
495 "That many MDCs already configured.\n",
496 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
499 if (lmv->desc.ld_tgt_count == 0) {
500 struct obd_device *mdc_obd;
502 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
505 lmv_init_unlock(lmv);
506 CERROR("Target %s not attached\n", tgt_uuid->uuid);
510 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, 0, NULL, tgt_uuid);
512 lmv_init_unlock(lmv);
513 CERROR("lmv failed to setup llogging subsystems\n");
516 spin_lock(&lmv->lmv_lock);
517 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
518 tgt->ltd_uuid = *tgt_uuid;
519 spin_unlock(&lmv->lmv_lock);
521 if (lmv->connected) {
522 rc = lmv_connect_mdc(obd, tgt);
524 spin_lock(&lmv->lmv_lock);
525 lmv->desc.ld_tgt_count--;
526 memset(tgt, 0, sizeof(*tgt));
527 spin_unlock(&lmv->lmv_lock);
529 int easize = sizeof(struct lmv_stripe_md) +
530 lmv->desc.ld_tgt_count *
531 sizeof(struct lu_fid);
532 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
536 lmv_init_unlock(lmv);
540 /* performs a check if passed obd is connected. If no - connect it. */
541 int lmv_check_connect(struct obd_device *obd)
543 struct lmv_obd *lmv = &obd->u.lmv;
544 struct lmv_tgt_desc *tgt;
552 if (lmv->connected) {
553 lmv_init_unlock(lmv);
557 if (lmv->desc.ld_tgt_count == 0) {
558 CERROR("%s: no targets configured.\n", obd->obd_name);
562 CDEBUG(D_CONFIG, "time to connect %s to %s\n",
563 lmv->cluuid.uuid, obd->obd_name);
565 LASSERT(lmv->tgts != NULL);
567 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
568 rc = lmv_connect_mdc(obd, tgt);
573 lmv_set_timeouts(obd);
574 class_export_put(lmv->exp);
576 easize = lmv_get_easize(lmv);
577 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
578 lmv_init_unlock(lmv);
587 --lmv->desc.ld_active_tgt_count;
588 rc2 = obd_disconnect(tgt->ltd_exp);
590 CERROR("error: LMV target %s disconnect on "
591 "MDC idx %d: error %d\n",
592 tgt->ltd_uuid.uuid, i, rc2);
596 class_disconnect(lmv->exp);
597 lmv_init_unlock(lmv);
601 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
604 struct proc_dir_entry *lmv_proc_dir;
606 struct lmv_obd *lmv = &obd->u.lmv;
607 struct obd_device *mdc_obd;
611 LASSERT(tgt != NULL);
612 LASSERT(obd != NULL);
614 mdc_obd = class_exp2obd(tgt->ltd_exp);
617 mdc_obd->obd_no_recov = obd->obd_no_recov;
620 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
622 struct proc_dir_entry *mdc_symlink;
624 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
626 lprocfs_remove(&mdc_symlink);
628 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
629 obd->obd_type->typ_name, obd->obd_name,
634 rc = obd_fid_fini(tgt->ltd_exp);
636 CERROR("Can't finanize fids factory\n");
638 CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
639 tgt->ltd_exp->exp_obd->obd_name,
640 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
642 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
643 rc = obd_disconnect(tgt->ltd_exp);
645 if (tgt->ltd_active) {
646 CERROR("Target %s disconnect error %d\n",
647 tgt->ltd_uuid.uuid, rc);
651 lmv_activate_target(lmv, tgt, 0);
656 static int lmv_disconnect(struct obd_export *exp)
658 struct obd_device *obd = class_exp2obd(exp);
660 struct proc_dir_entry *lmv_proc_dir;
662 struct lmv_obd *lmv = &obd->u.lmv;
669 /* Only disconnect the underlying layers on the final disconnect. */
671 if (lmv->refcount != 0)
674 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
675 if (lmv->tgts[i].ltd_exp == NULL)
677 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
681 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
683 lprocfs_remove(&lmv_proc_dir);
685 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
686 obd->obd_type->typ_name, obd->obd_name);
692 * This is the case when no real connection is established by
693 * lmv_check_connect().
696 class_export_put(exp);
697 rc = class_disconnect(exp);
698 if (lmv->refcount == 0)
703 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
704 int len, void *karg, void *uarg)
706 struct obd_device *obddev = class_exp2obd(exp);
707 struct lmv_obd *lmv = &obddev->u.lmv;
708 int i, rc = 0, set = 0;
711 if (lmv->desc.ld_tgt_count == 0)
715 case IOC_OBD_STATFS: {
716 struct obd_ioctl_data *data = karg;
717 struct obd_device *mdc_obd;
718 struct obd_statfs stat_buf = {0};
721 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
722 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
724 if ((index >= lmv->desc.ld_tgt_count))
727 if (!lmv->tgts[index].ltd_active)
730 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
734 /* got statfs data */
735 rc = obd_statfs(mdc_obd, &stat_buf,
736 cfs_time_current_64() - HZ, 0);
739 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
742 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
747 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
750 if (lmv->tgts[i].ltd_exp == NULL)
753 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
756 if (lmv->tgts[i].ltd_active) {
757 CERROR("error: iocontrol MDC %s on MDT"
758 "idx %d cmd %x: err = %d\n",
759 lmv->tgts[i].ltd_uuid.uuid,
779 static int lmv_all_chars_policy(int count, const char *name,
790 static int lmv_nid_policy(struct lmv_obd *lmv)
792 struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
795 * XXX Hack: to get nid we assume that underlying obd device is mdc.
797 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
798 return id % lmv->desc.ld_tgt_count;
801 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
806 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
808 op_data->op_namelen);
810 return lmv_nid_policy(lmv);
816 CERROR("unsupport type %d \n", type);
820 /* This is _inode_ placement policy function (not name). */
821 static int lmv_placement_policy(struct obd_device *obd,
822 struct md_op_data *op_data,
825 struct lmv_obd *lmv = &obd->u.lmv;
830 LASSERT(mds != NULL);
833 * Allocate new fid on target according to operation type and parent
836 obj = lmv_obj_grab(obd, &op_data->op_fid1);
837 if (obj != NULL || op_data->op_name == NULL ||
838 op_data->op_opc != LUSTRE_OPC_MKDIR) {
840 * Allocate fid for non-dir or for null name or for case parent
847 * If we have this flag turned on, and we see that
848 * parent dir is split, this means, that caller did not
849 * notice split yet. This is race and we would like to
850 * let caller know that.
852 if (op_data->op_bias & MDS_CHECK_SPLIT)
857 * Allocate new fid on same mds where parent fid is located and
858 * where operation will be sent. In case of split dir, ->op_fid1
859 * and ->op_mds here will contain fid and mds of slave directory
860 * object (assigned by caller).
862 *mds = op_data->op_mds;
866 /* XXX: This should be removed later wehn we sure it is not
868 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
874 * Parent directory is not split and we want to create a
875 * directory in it. Let's calculate where to place it according
878 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
886 CERROR("Can't choose MDS, err = %d\n", rc);
888 LASSERT(*mds < lmv->desc.ld_tgt_count);
894 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
897 struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
901 /* New seq alloc and FLD setup should be atomic. */
902 down(&tgt->ltd_fid_sem);
904 /* Asking underlaying tgt layer to allocate new fid. */
905 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
907 LASSERT(fid_is_sane(fid));
909 /* Client switches to new sequence, setup FLD. */
910 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
913 CERROR("Can't create fld entry, rc %d\n", rc);
914 /* Delete just allocated fid sequence */
915 obd_fid_delete(tgt->ltd_exp, NULL);
918 up(&tgt->ltd_fid_sem);
922 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
923 struct md_op_data *op_data)
925 struct obd_device *obd = class_exp2obd(exp);
926 struct lmv_obd *lmv = &obd->u.lmv;
931 LASSERT(op_data != NULL);
932 LASSERT(fid != NULL);
934 rc = lmv_placement_policy(obd, op_data, &mds);
936 CERROR("Can't get target for allocating fid, "
941 rc = __lmv_fid_alloc(lmv, fid, mds);
943 CERROR("Can't alloc new fid, rc %d\n", rc);
950 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
955 if (lmv_obj_delete(exp, fid)) {
956 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
962 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
964 struct lmv_obd *lmv = &obd->u.lmv;
965 struct lprocfs_static_vars lvars;
966 struct lmv_desc *desc;
970 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
971 CERROR("LMV setup requires a descriptor\n");
975 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
976 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
977 CERROR("descriptor size wrong: %d > %d\n",
978 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
982 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
984 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
985 if (lmv->tgts == NULL)
988 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
989 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
990 lmv->tgts[i].ltd_idx = i;
993 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
995 OBD_ALLOC(lmv->datas, lmv->datas_size);
996 if (lmv->datas == NULL)
997 GOTO(out_free_tgts, rc = -ENOMEM);
999 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1000 lmv->desc.ld_tgt_count = 0;
1001 lmv->desc.ld_active_tgt_count = 0;
1002 lmv->max_cookiesize = 0;
1003 lmv->max_def_easize = 0;
1004 lmv->max_easize = 0;
1006 spin_lock_init(&lmv->lmv_lock);
1007 sema_init(&lmv->init_sem, 1);
1009 rc = lmv_obj_setup(obd);
1011 CERROR("Can't setup LMV object manager, "
1013 GOTO(out_free_datas, rc);
1016 lprocfs_lmv_init_vars(&lvars);
1017 lprocfs_obd_setup(obd, lvars.obd_vars);
1020 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1021 0444, &lmv_proc_target_fops, obd);
1023 CWARN("Error adding the target_obd_status file\n");
1026 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1027 LUSTRE_CLI_FLD_HASH_DHT);
1029 CERROR("can't init FLD, err %d\n",
1031 GOTO(out_free_datas, rc);
1037 OBD_FREE(lmv->datas, lmv->datas_size);
1040 OBD_FREE(lmv->tgts, lmv->tgts_size);
1045 static int lmv_cleanup(struct obd_device *obd)
1047 struct lmv_obd *lmv = &obd->u.lmv;
1050 fld_client_fini(&lmv->lmv_fld);
1051 lprocfs_obd_cleanup(obd);
1052 lmv_obj_cleanup(obd);
1053 OBD_FREE(lmv->datas, lmv->datas_size);
1054 OBD_FREE(lmv->tgts, lmv->tgts_size);
1059 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1061 struct lustre_cfg *lcfg = buf;
1062 struct obd_uuid tgt_uuid;
1066 switch(lcfg->lcfg_command) {
1068 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1069 GOTO(out, rc = -EINVAL);
1071 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1072 rc = lmv_add_target(obd, &tgt_uuid);
1075 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1076 GOTO(out, rc = -EINVAL);
1083 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1084 __u64 max_age, __u32 flags)
1086 struct lmv_obd *lmv = &obd->u.lmv;
1087 struct obd_statfs *temp;
1091 rc = lmv_check_connect(obd);
1095 OBD_ALLOC(temp, sizeof(*temp));
1099 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1100 if (lmv->tgts[i].ltd_exp == NULL)
1103 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1106 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1107 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1109 GOTO(out_free_temp, rc);
1114 osfs->os_bavail += temp->os_bavail;
1115 osfs->os_blocks += temp->os_blocks;
1116 osfs->os_ffree += temp->os_ffree;
1117 osfs->os_files += temp->os_files;
1123 OBD_FREE(temp, sizeof(*temp));
1127 static int lmv_getstatus(struct obd_export *exp,
1129 struct obd_capa **pc)
1131 struct obd_device *obd = exp->exp_obd;
1132 struct lmv_obd *lmv = &obd->u.lmv;
1136 rc = lmv_check_connect(obd);
1140 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1145 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1146 struct obd_capa *oc, obd_valid valid, const char *name,
1147 const char *input, int input_size, int output_size,
1148 int flags, struct ptlrpc_request **request)
1150 struct obd_device *obd = exp->exp_obd;
1151 struct lmv_obd *lmv = &obd->u.lmv;
1152 struct obd_export *tgt_exp;
1156 rc = lmv_check_connect(obd);
1160 tgt_exp = lmv_find_export(lmv, fid);
1161 if (IS_ERR(tgt_exp))
1162 RETURN(PTR_ERR(tgt_exp));
1164 rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1165 output_size, flags, request);
1170 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1171 struct obd_capa *oc, obd_valid valid, const char *name,
1172 const char *input, int input_size, int output_size,
1173 int flags, __u32 suppgid,
1174 struct ptlrpc_request **request)
1176 struct obd_device *obd = exp->exp_obd;
1177 struct lmv_obd *lmv = &obd->u.lmv;
1178 struct obd_export *tgt_exp;
1182 rc = lmv_check_connect(obd);
1186 tgt_exp = lmv_find_export(lmv, fid);
1187 if (IS_ERR(tgt_exp))
1188 RETURN(PTR_ERR(tgt_exp));
1190 rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1191 input, input_size, output_size, flags, suppgid,
1197 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1198 struct obd_capa *oc, obd_valid valid, int ea_size,
1199 struct ptlrpc_request **request)
1201 struct obd_device *obd = exp->exp_obd;
1202 struct lmv_obd *lmv = &obd->u.lmv;
1203 struct obd_export *tgt_exp;
1204 struct lmv_obj *obj;
1208 rc = lmv_check_connect(obd);
1212 tgt_exp = lmv_find_export(lmv, fid);
1213 if (IS_ERR(tgt_exp))
1214 RETURN(PTR_ERR(tgt_exp));
1216 rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1220 obj = lmv_obj_grab(obd, fid);
1222 CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1223 obj ? "(split)" : "");
1226 * If object is split, then we loop over all the slaves and gather size
1227 * attribute. In ideal world we would have to gather also mds field from
1228 * all slaves, as object is spread over the cluster and this is
1229 * definitely interesting information and it is not good to loss it,
1233 struct mdt_body *body;
1235 if (*request == NULL) {
1240 body = req_capsule_server_get(&(*request)->rq_pill,
1242 LASSERT(body != NULL);
1246 for (i = 0; i < obj->lo_objcount; i++) {
1247 if (lmv->tgts[i].ltd_exp == NULL) {
1248 CWARN("%s: NULL export for %d\n",
1253 /* skip master obj. */
1254 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1257 lmv_update_body(body, &obj->lo_inodes[i]);
1260 lmv_obj_unlock(obj);
1267 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1268 ldlm_iterator_t it, void *data)
1270 struct obd_device *obd = exp->exp_obd;
1271 struct lmv_obd *lmv = &obd->u.lmv;
1275 rc = lmv_check_connect(obd);
1279 CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1282 * With CMD every object can have two locks in different namespaces:
1283 * lookup lock in space of mds storing direntry and update/open lock in
1284 * space of mds storing inode.
1286 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1287 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1292 static int lmv_close(struct obd_export *exp,
1293 struct md_op_data *op_data,
1294 struct md_open_data *mod,
1295 struct ptlrpc_request **request)
1297 struct obd_device *obd = exp->exp_obd;
1298 struct lmv_obd *lmv = &obd->u.lmv;
1299 struct obd_export *tgt_exp;
1303 rc = lmv_check_connect(obd);
1307 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1308 if (IS_ERR(tgt_exp))
1309 RETURN(PTR_ERR(tgt_exp));
1311 CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1312 rc = md_close(tgt_exp, op_data, mod, request);
1317 * Called in the case MDS returns -ERESTART on create on open, what means that
1318 * directory is split and its LMV presentation object has to be updated.
1320 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1322 struct obd_device *obd = exp->exp_obd;
1323 struct lmv_obd *lmv = &obd->u.lmv;
1324 struct ptlrpc_request *req = NULL;
1325 struct obd_export *tgt_exp;
1326 struct lmv_obj *obj;
1327 struct lustre_md md;
1333 mealen = lmv_get_easize(lmv);
1335 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1337 tgt_exp = lmv_find_export(lmv, fid);
1338 if (IS_ERR(tgt_exp))
1339 RETURN(PTR_ERR(tgt_exp));
1341 /* time to update mea of parent fid */
1342 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1344 CERROR("md_getattr() failed, error %d\n", rc);
1348 rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1350 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1355 GOTO(cleanup, rc = -ENODATA);
1357 obj = lmv_obj_create(exp, fid, md.mea);
1363 /* XXX LOV STACKING */
1364 obd_free_memmd(exp, (void *)&md.mea);
1369 ptlrpc_req_finished(req);
1373 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1374 const void *data, int datalen, int mode, __u32 uid,
1375 __u32 gid, __u32 cap_effective, __u64 rdev,
1376 struct ptlrpc_request **request)
1378 struct obd_device *obd = exp->exp_obd;
1379 struct lmv_obd *lmv = &obd->u.lmv;
1380 struct obd_export *tgt_exp;
1381 struct lmv_obj *obj;
1385 rc = lmv_check_connect(obd);
1389 if (!lmv->desc.ld_active_tgt_count)
1394 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1398 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1399 op_data->op_name, op_data->op_namelen);
1400 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1401 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1402 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1403 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1406 struct lmv_tgt_desc *tgt;
1408 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1409 op_data->op_bias |= MDS_CHECK_SPLIT;
1410 op_data->op_mds = tgt->ltd_idx;
1411 tgt_exp = tgt->ltd_exp;
1414 if (IS_ERR(tgt_exp))
1415 RETURN(PTR_ERR(tgt_exp));
1417 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1418 if (rc == -ERESTART)
1423 CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1424 op_data->op_name, PFID(&op_data->op_fid1));
1426 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1427 rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1428 cap_effective, rdev, request);
1430 if (*request == NULL)
1432 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1433 } else if (rc == -ERESTART) {
1434 LASSERT(*request != NULL);
1435 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1436 "Got -ERESTART during create!\n");
1437 ptlrpc_req_finished(*request);
1441 * Directory got split. Time to update local object and repeat
1442 * the request with proper MDS.
1444 rc = lmv_handle_split(exp, &op_data->op_fid1);
1446 rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1447 op_data, &op_data->op_fid2);
1456 static int lmv_done_writing(struct obd_export *exp,
1457 struct md_op_data *op_data,
1458 struct md_open_data *mod)
1460 struct obd_device *obd = exp->exp_obd;
1461 struct lmv_obd *lmv = &obd->u.lmv;
1462 struct obd_export *tgt_exp;
1466 rc = lmv_check_connect(obd);
1470 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1471 if (IS_ERR(tgt_exp))
1472 RETURN(PTR_ERR(tgt_exp));
1474 rc = md_done_writing(tgt_exp, op_data, mod);
1479 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1480 struct lookup_intent *it, struct md_op_data *op_data,
1481 struct lustre_handle *lockh, void *lmm, int lmmsize)
1483 struct obd_device *obd = exp->exp_obd;
1484 struct lmv_obd *lmv = &obd->u.lmv;
1485 struct lmv_stripe_md *mea = op_data->op_mea1;
1486 struct md_op_data *op_data2;
1487 struct obd_export *tgt_exp;
1491 OBD_ALLOC_PTR(op_data2);
1492 if (op_data2 == NULL)
1495 LASSERT(mea != NULL);
1496 for (i = 0; i < mea->mea_count; i++) {
1497 memset(op_data2, 0, sizeof(*op_data2));
1498 op_data2->op_fid1 = mea->mea_ids[i];
1499 op_data2->op_bias = 0;
1501 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1502 if (IS_ERR(tgt_exp))
1503 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1505 if (tgt_exp == NULL)
1508 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1509 lockh + i, lmm, lmmsize, 0);
1511 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1512 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1517 if (it->d.lustre.it_data) {
1518 struct ptlrpc_request *req;
1519 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1520 ptlrpc_req_finished(req);
1523 if (it->d.lustre.it_status)
1524 GOTO(cleanup, rc = it->d.lustre.it_status);
1529 OBD_FREE_PTR(op_data2);
1532 /* drop all taken locks */
1534 if (lockh[i].cookie)
1535 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1536 lockh[i].cookie = 0;
1543 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1544 struct lookup_intent *it, struct md_op_data *op_data,
1545 struct lustre_handle *lockh, void *lmm, int lmmsize,
1546 int extra_lock_flags)
1548 struct ptlrpc_request *req = it->d.lustre.it_data;
1549 struct obd_device *obd = exp->exp_obd;
1550 struct lmv_obd *lmv = &obd->u.lmv;
1551 struct lustre_handle plock;
1552 struct obd_export *tgt_exp;
1553 struct md_op_data *rdata;
1554 struct lu_fid fid_copy;
1555 struct mdt_body *body;
1559 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1560 LASSERT(body != NULL);
1562 if (!(body->valid & OBD_MD_MDS))
1565 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1566 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1568 /* We got LOOKUP lock, but we really need attrs */
1569 pmode = it->d.lustre.it_lock_mode;
1570 LASSERT(pmode != 0);
1571 memcpy(&plock, lockh, sizeof(plock));
1572 it->d.lustre.it_lock_mode = 0;
1573 it->d.lustre.it_data = NULL;
1574 fid_copy = body->fid1;
1576 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1577 ptlrpc_req_finished(req);
1579 tgt_exp = lmv_find_export(lmv, &fid_copy);
1580 if (IS_ERR(tgt_exp))
1581 GOTO(out, rc = PTR_ERR(tgt_exp));
1583 OBD_ALLOC_PTR(rdata);
1585 GOTO(out, rc = -ENOMEM);
1587 rdata->op_fid1 = fid_copy;
1588 rdata->op_bias = MDS_CROSS_REF;
1590 rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1591 lmm, lmmsize, extra_lock_flags);
1592 OBD_FREE_PTR(rdata);
1595 ldlm_lock_decref(&plock, pmode);
1600 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1601 struct lookup_intent *it, struct md_op_data *op_data,
1602 struct lustre_handle *lockh, void *lmm, int lmmsize,
1603 int extra_lock_flags)
1605 struct obd_device *obd = exp->exp_obd;
1606 struct lmv_obd *lmv = &obd->u.lmv;
1607 struct obd_export *tgt_exp = NULL;
1608 struct lmv_obj *obj;
1612 rc = lmv_check_connect(obd);
1616 if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1617 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1618 lockh, lmm, lmmsize);
1622 if (op_data->op_namelen) {
1623 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1627 /* directory is split. look for right mds for this
1629 mea_idx = raw_name2idx(obj->lo_hashtype,
1631 (char *)op_data->op_name,
1632 op_data->op_namelen);
1633 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1634 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1639 if (tgt_exp == NULL)
1640 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1641 if (IS_ERR(tgt_exp))
1642 RETURN(PTR_ERR(tgt_exp));
1644 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1645 PFID(&op_data->op_fid1));
1647 rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1648 lmm, lmmsize, extra_lock_flags);
1650 if (rc == 0 && it->it_op == IT_OPEN)
1651 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1652 lmm, lmmsize, extra_lock_flags);
1657 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1658 struct obd_capa *oc, const char *filename, int namelen,
1659 obd_valid valid, int ea_size, __u32 suppgid,
1660 struct ptlrpc_request **request)
1662 struct obd_device *obd = exp->exp_obd;
1663 struct lmv_obd *lmv = &obd->u.lmv;
1664 struct lu_fid rid = *fid;
1665 struct obd_export *tgt_exp;
1666 struct mdt_body *body;
1667 struct lmv_obj *obj;
1671 rc = lmv_check_connect(obd);
1678 obj = lmv_obj_grab(obd, &rid);
1682 /* Directory is split. Look for right mds for this name */
1683 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1684 filename, namelen - 1);
1685 rid = obj->lo_inodes[mea_idx].li_fid;
1686 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1688 valid &= ~OBD_MD_FLCKSPLIT;
1690 tgt_exp = lmv_find_export(lmv, &rid);
1691 valid |= OBD_MD_FLCKSPLIT;
1693 if (IS_ERR(tgt_exp))
1694 RETURN(PTR_ERR(tgt_exp));
1696 CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1697 namelen, filename, PFID(fid), PFID(&rid));
1699 rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1700 ea_size, suppgid, request);
1702 body = req_capsule_server_get(&(*request)->rq_pill,
1704 LASSERT(body != NULL);
1706 if (body->valid & OBD_MD_MDS) {
1707 struct ptlrpc_request *req = NULL;
1710 CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1713 tgt_exp = lmv_find_export(lmv, &rid);
1714 if (IS_ERR(tgt_exp)) {
1715 ptlrpc_req_finished(*request);
1716 RETURN(PTR_ERR(tgt_exp));
1719 rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1720 valid | OBD_MD_FLCROSSREF,
1721 ea_size, suppgid, &req);
1722 ptlrpc_req_finished(*request);
1725 } else if (rc == -ERESTART) {
1726 LASSERT(*request != NULL);
1727 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1728 "Got -ERESTART during getattr!\n");
1729 ptlrpc_req_finished(*request);
1733 * Directory got split. Time to update local object and repeat
1734 * the request with proper MDS.
1736 rc = lmv_handle_split(exp, &rid);
1743 #define md_op_data_fid(op_data, fl) \
1744 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1745 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1746 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1747 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1750 /* @tgt_exp is the export the metadata request is sent.
1751 * @fid_exp is the export the cancel should be sent for the current fid.
1752 * if @fid_exp is NULL, the export is found for the current fid.
1753 * @op_data keeps the current fid, which is pointed through @flag.
1754 * @mode, @bits -- lock match parameters. */
1755 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1756 struct obd_export *fid_exp,
1757 struct md_op_data *op_data,
1758 ldlm_mode_t mode, int bits, int flag)
1760 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1761 ldlm_policy_data_t policy = {{0}};
1765 if (!fid_is_sane(fid))
1768 if (fid_exp == NULL)
1769 fid_exp = lmv_find_export(lmv, fid);
1771 if (tgt_exp == fid_exp) {
1772 /* The export is the same as on the target server, cancel
1773 * will be sent along with the main metadata operation. */
1774 op_data->op_flags |= flag;
1778 policy.l_inodebits.bits = bits;
1779 rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1783 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1784 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1785 * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1786 * md_cancel_unused for child export(s). */
1787 static int lmv_early_cancel_stripes(struct obd_export *exp,
1788 struct obd_export *tgt_exp,
1789 struct md_op_data *op_data,
1790 ldlm_mode_t mode, int bits, int flag)
1792 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1793 struct obd_device *obd = exp->exp_obd;
1794 struct lmv_obd *lmv = &obd->u.lmv;
1795 struct obd_export *st_exp;
1796 struct lmv_obj *obj;
1800 if (!fid_is_sane(fid))
1803 obj = lmv_obj_grab(obd, fid);
1805 ldlm_policy_data_t policy = {{0}};
1806 struct lu_fid *st_fid;
1809 policy.l_inodebits.bits = bits;
1810 for (i = 0; i < obj->lo_objcount; i++) {
1811 st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1812 st_fid = &obj->lo_inodes[i].li_fid;
1813 if (tgt_exp != st_exp) {
1814 rc = md_cancel_unused(st_exp, st_fid, &policy,
1815 mode, LDLM_FL_ASYNC,
1820 /* Some export matches to @tgt_exp, do cancel
1821 * for its fid in mdc */
1823 op_data->op_flags |= flag;
1828 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1836 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1839 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1840 struct ptlrpc_request **request)
1842 struct obd_device *obd = exp->exp_obd;
1843 struct lmv_obd *lmv = &obd->u.lmv;
1844 struct obd_export *tgt_exp;
1845 struct lmv_obj *obj;
1850 rc = lmv_check_connect(obd);
1857 if (op_data->op_namelen != 0) {
1860 /* Usual link request */
1861 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1863 mea_idx = raw_name2idx(obj->lo_hashtype,
1866 op_data->op_namelen);
1867 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1868 mds = obj->lo_inodes[mea_idx].li_mds;
1871 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1876 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1877 PFID(&op_data->op_fid2), op_data->op_namelen,
1878 op_data->op_name, PFID(&op_data->op_fid1));
1880 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1884 /* request from MDS to acquire i_links for inode by fid1 */
1885 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1886 PFID(&op_data->op_fid1));
1889 CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1890 mds, PFID(&op_data->op_fid1));
1892 op_data->op_fsuid = current->fsuid;
1893 op_data->op_fsgid = current->fsgid;
1894 op_data->op_cap = current->cap_effective;
1896 tgt_exp = lmv->tgts[mds].ltd_exp;
1897 if (op_data->op_namelen) {
1898 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1899 /* Cancel UPDATE lock on child (fid1). */
1900 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1901 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1904 rc = md_link(tgt_exp, op_data, request);
1905 if (rc == -ERESTART) {
1906 LASSERT(*request != NULL);
1907 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1908 "Got -ERESTART during link!\n");
1909 ptlrpc_req_finished(*request);
1913 * Directory got split. Time to update local object and repeat
1914 * the request with proper MDS.
1916 rc = lmv_handle_split(exp, &op_data->op_fid2);
1924 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1925 const char *old, int oldlen, const char *new, int newlen,
1926 struct ptlrpc_request **request)
1928 struct obd_export *tgt_exp = NULL, *src_exp;
1929 struct obd_device *obd = exp->exp_obd;
1930 struct lmv_obd *lmv = &obd->u.lmv;
1931 int rc, mea_idx, loop = 0;
1932 struct lmv_obj *obj;
1936 CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1937 oldlen, old, PFID(&op_data->op_fid1),
1938 newlen, new, PFID(&op_data->op_fid2));
1940 rc = lmv_check_connect(obd);
1946 * MDS with old dir entry is asking another MDS to create name
1950 "create %*s(%d/%d) in "DFID" pointing "
1951 "to "DFID"\n", newlen, new, oldlen, newlen,
1952 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1954 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1959 * Target directory can be split, sowe should forward request to
1962 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1964 mea_idx = raw_name2idx(obj->lo_hashtype,
1966 (char *)new, newlen);
1967 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1968 CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1969 PFID(&op_data->op_fid2));
1978 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1981 * directory is already split, so we have to forward request to
1984 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1985 (char *)old, oldlen);
1986 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1987 mds1 = obj->lo_inodes[mea_idx].li_mds;
1988 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1991 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
1996 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1999 * Directory is already split, so we have to forward request to
2002 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2003 (char *)new, newlen);
2005 mds2 = obj->lo_inodes[mea_idx].li_mds;
2006 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
2007 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2010 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2016 op_data->op_fsuid = current->fsuid;
2017 op_data->op_fsgid = current->fsgid;
2018 op_data->op_cap = current->cap_effective;
2020 src_exp = lmv_get_export(lmv, mds1);
2021 tgt_exp = lmv_get_export(lmv, mds2);
2023 /* LOOKUP lock on src child (fid3) should also be cancelled for
2024 * src_exp in mdc_rename. */
2025 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2027 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2029 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2030 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2032 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2034 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2035 LCK_EX, MDS_INODELOCK_LOOKUP,
2036 MF_MDC_CANCEL_FID4);
2038 /* XXX: the case when child is a striped dir is not supported.
2039 * Only the master stripe has all locks cancelled early. */
2040 /* Cancel all the locks on tgt child (fid4). */
2042 rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2043 LCK_EX, MDS_INODELOCK_FULL,
2044 MF_MDC_CANCEL_FID4);
2048 rc = md_rename(src_exp, op_data, old, oldlen,
2049 new, newlen, request);
2050 if (rc == -ERESTART) {
2051 LASSERT(*request != NULL);
2052 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2053 "Got -ERESTART during rename!\n");
2054 ptlrpc_req_finished(*request);
2058 * Directory got split. Time to update local object and repeat
2059 * the request with proper MDS.
2061 rc = lmv_handle_split(exp, &op_data->op_fid1);
2068 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2069 void *ea, int ealen, void *ea2, int ea2len,
2070 struct ptlrpc_request **request,
2071 struct md_open_data **mod)
2073 struct obd_device *obd = exp->exp_obd;
2074 struct lmv_obd *lmv = &obd->u.lmv;
2075 struct ptlrpc_request *req;
2076 struct obd_export *tgt_exp;
2077 struct lmv_obj *obj;
2081 rc = lmv_check_connect(obd);
2085 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2087 CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2088 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2089 obj ? ", split" : "");
2091 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2093 for (i = 0; i < obj->lo_objcount; i++) {
2094 op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2096 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2097 if (IS_ERR(tgt_exp)) {
2098 rc = PTR_ERR(tgt_exp);
2102 rc = md_setattr(tgt_exp, op_data, ea, ealen,
2103 ea2, ea2len, &req, mod);
2105 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2107 * this is master object and this request should
2108 * be returned back to llite.
2112 ptlrpc_req_finished(req);
2120 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2121 if (IS_ERR(tgt_exp))
2122 RETURN(PTR_ERR(tgt_exp));
2124 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2125 ea2len, request, mod);
2130 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2131 struct obd_capa *oc, struct ptlrpc_request **request)
2133 struct obd_device *obd = exp->exp_obd;
2134 struct lmv_obd *lmv = &obd->u.lmv;
2135 struct obd_export *tgt_exp;
2139 rc = lmv_check_connect(obd);
2143 tgt_exp = lmv_find_export(lmv, fid);
2144 if (IS_ERR(tgt_exp))
2145 RETURN(PTR_ERR(tgt_exp));
2147 rc = md_sync(tgt_exp, fid, oc, request);
2151 /* main purpose of LMV blocking ast is to remove split directory LMV
2152 * presentation object (struct lmv_obj) attached to the lock being revoked. */
2153 int lmv_blocking_ast(struct ldlm_lock *lock,
2154 struct ldlm_lock_desc *desc,
2155 void *data, int flag)
2157 struct lustre_handle lockh;
2158 struct lmv_obj *obj;
2163 case LDLM_CB_BLOCKING:
2164 ldlm_lock2handle(lock, &lockh);
2165 rc = ldlm_cli_cancel(&lockh);
2167 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2171 case LDLM_CB_CANCELING:
2172 /* time to drop cached attrs for dirobj */
2173 obj = lock->l_ast_data;
2175 CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2176 ", master "DFID"\n",
2177 lock->l_resource->lr_name.name[3] == 1 ?
2178 "LOOKUP" : "UPDATE",
2179 lock->l_resource->lr_name.name[0],
2180 lock->l_resource->lr_name.name[1],
2181 PFID(&obj->lo_fid));
2191 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2195 val = le64_to_cpu(*hash);
2197 val += MAX_HASH_SIZE;
2198 if (val != DIR_END_OFF)
2199 *hash = cpu_to_le64(val - hash_adj);
2202 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2205 struct obd_import *imp;
2208 * XXX Hack: to get nid we assume that underlying obd device is mdc.
2210 imp = class_exp2cliimp(exp);
2211 id = imp->imp_connection->c_self + fid_flatten(fid);
2213 CDEBUG(D_INFO, "node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2214 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2216 return id ^ (id >> 32);
2219 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2220 struct obd_capa *oc, __u64 offset64, struct page *page,
2221 struct ptlrpc_request **request)
2223 struct obd_device *obd = exp->exp_obd;
2224 struct lmv_obd *lmv = &obd->u.lmv;
2225 struct obd_export *tgt_exp;
2226 struct lu_fid rid = *fid;
2227 struct lmv_obj *obj;
2241 rc = lmv_check_connect(obd);
2245 CDEBUG(D_INFO, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2247 obj = lmv_obj_grab(obd, fid);
2251 * This case handle directory lookup in clustered metadata case (i.e.
2252 * split directory is located on multiple md servers.)
2253 * each server keeps directory entries for certain range of hashes.
2254 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2255 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2256 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2258 * readdir can simply start reading entries from 0 - N server in
2259 * order but that will not scale well as all client will request dir in
2260 * to server in same order.
2261 * Following algorithm does optimization:
2262 * Instead of doing readdir in 1, 2, ...., N order, client with a
2263 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2264 * (every client has rank R)
2265 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2266 * since client ask dir from MDS{R} client has pages with offsets
2267 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2268 * on hash values that we get.
2271 struct lmv_inode *loi;
2275 nr = obj->lo_objcount;
2277 seg_size = MAX_HASH_SIZE;
2278 do_div(seg_size, nr);
2279 loi = obj->lo_inodes;
2280 rank = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2283 do_div(tgt_tmp, seg_size);
2284 tgt0 = do_div(tgt_tmp, nr);
2285 tgt = (tgt0 + rank) % nr;
2291 * Last segment has unusual length due to division
2294 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2298 hash_adj += rank * seg_size;
2300 CDEBUG(D_INFO, "hash_adj: %x "LPX64" "LPX64"/%x -> "LPX64"/%x\n",
2301 rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2303 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2304 rid = obj->lo_inodes[tgt].li_fid;
2305 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2307 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2308 PFID(&rid), (unsigned long)offset, tgt);
2310 tgt_exp = lmv_find_export(lmv, &rid);
2312 if (IS_ERR(tgt_exp))
2313 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2315 rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2319 struct lu_dirpage *dp;
2320 struct lu_dirent *ent;
2322 dp = cfs_kmap(page);
2324 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2325 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2326 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2328 for (ent = lu_dirent_start(dp); ent != NULL;
2329 ent = lu_dirent_next(ent))
2330 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2332 if (tgt0 != nr - 1) {
2335 end = le64_to_cpu(dp->ldp_hash_end);
2336 if (end == DIR_END_OFF) {
2337 dp->ldp_hash_end = cpu_to_le32(seg_size *
2340 ""DFID" reset end "LPX64" tgt %d\n",
2342 le64_to_cpu(dp->ldp_hash_end), tgt);
2348 * Here we could remove "." and ".." from all pages which at not from
2349 * master. But MDS has only "." and ".." for master dir.
2354 lmv_obj_unlock(obj);
2360 static int lmv_unlink_slaves(struct obd_export *exp,
2361 struct md_op_data *op_data,
2362 struct ptlrpc_request **req)
2364 struct obd_device *obd = exp->exp_obd;
2365 struct lmv_obd *lmv = &obd->u.lmv;
2366 struct lmv_stripe_md *mea = op_data->op_mea1;
2367 struct md_op_data *op_data2;
2368 struct obd_export *tgt_exp;
2372 OBD_ALLOC_PTR(op_data2);
2373 if (op_data2 == NULL)
2376 op_data2->op_mode = S_IFDIR;
2377 op_data2->op_fsuid = current->fsuid;
2378 op_data2->op_fsgid = current->fsgid;
2379 op_data2->op_bias = 0;
2381 LASSERT(mea != NULL);
2382 for (i = 0; i < mea->mea_count; i++) {
2383 memset(op_data2, 0, sizeof(*op_data2));
2384 op_data2->op_fid1 = mea->mea_ids[i];
2385 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2386 if (IS_ERR(tgt_exp))
2387 GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2389 if (tgt_exp == NULL)
2392 rc = md_unlink(tgt_exp, op_data2, req);
2394 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2395 PFID(&mea->mea_ids[i]), rc);
2398 ptlrpc_req_finished(*req);
2402 GOTO(out_free_op_data2, rc);
2407 OBD_FREE_PTR(op_data2);
2411 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2412 struct ptlrpc_request **request)
2414 struct obd_device *obd = exp->exp_obd;
2415 struct lmv_obd *lmv = &obd->u.lmv;
2416 struct obd_export *tgt_exp = NULL;
2417 struct lmv_obj *obj;
2421 rc = lmv_check_connect(obd);
2425 if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2426 /* mds asks to remove slave objects */
2427 rc = lmv_unlink_slaves(exp, op_data, request);
2434 if (op_data->op_namelen != 0) {
2437 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2439 mea_idx = raw_name2idx(obj->lo_hashtype,
2442 op_data->op_namelen);
2443 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2444 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2445 tgt_exp = lmv_get_export(lmv,
2446 obj->lo_inodes[mea_idx].li_mds);
2448 CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2449 op_data->op_namelen, op_data->op_name,
2450 PFID(&op_data->op_fid1), mea_idx);
2453 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2454 PFID(&op_data->op_fid1));
2456 if (tgt_exp == NULL) {
2457 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2458 if (IS_ERR(tgt_exp))
2459 RETURN(PTR_ERR(tgt_exp));
2460 op_data->op_bias |= MDS_CHECK_SPLIT;
2463 op_data->op_fsuid = current->fsuid;
2464 op_data->op_fsgid = current->fsgid;
2465 op_data->op_cap = current->cap_effective;
2467 /* If child's fid is given, cancel unused locks for it if it is from
2468 * another export than parent. */
2469 if (op_data->op_namelen) {
2470 /* LOOKUP lock for child (fid3) should also be cancelled on
2471 * parent tgt_exp in mdc_unlink(). */
2472 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2474 /* XXX: the case when child is a striped dir is not supported.
2475 * Only the master stripe has all locks cancelled early. */
2476 /* Cancel FULL locks on child (fid3). */
2477 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2478 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2481 rc = md_unlink(tgt_exp, op_data, request);
2482 if (rc == -ERESTART) {
2483 LASSERT(*request != NULL);
2484 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2485 "Got -ERESTART during unlink!\n");
2486 ptlrpc_req_finished(*request);
2490 * Directory got split. Time to update local object and repeat
2491 * the request with proper MDS.
2493 rc = lmv_handle_split(exp, &op_data->op_fid1);
2500 static int lmv_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2501 struct obd_device *tgt, int count,
2502 struct llog_catid *logid, struct obd_uuid *uuid)
2505 struct llog_ctxt *ctxt;
2509 LASSERT(group == OBD_LLOG_GROUP);
2510 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2513 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2514 llog_initiator_connect(ctxt, tgt);
2515 llog_ctxt_put(ctxt);
2523 static int lmv_llog_finish(struct obd_device *obd, int count)
2525 struct llog_ctxt *ctxt;
2529 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2531 rc = llog_cleanup(ctxt);
2536 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2541 case OBD_CLEANUP_EARLY:
2542 /* XXX: here should be calling obd_precleanup() down to
2545 case OBD_CLEANUP_EXPORTS:
2546 rc = obd_llog_finish(obd, 0);
2548 CERROR("failed to cleanup llogging subsystems\n");
2556 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2557 void *key, __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2559 struct obd_device *obd;
2560 struct lmv_obd *lmv;
2564 obd = class_exp2obd(exp);
2566 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2567 exp->exp_handle.h_cookie);
2572 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2573 struct lmv_tgt_desc *tgts;
2576 rc = lmv_check_connect(obd);
2580 LASSERT(*vallen == sizeof(__u32));
2581 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2584 /* all tgts should be connected when this get called. */
2585 if (!tgts || !tgts->ltd_exp) {
2586 CERROR("target not setup?\n");
2590 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2595 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2596 rc = lmv_check_connect(obd);
2600 /* forwarding this request to first MDS, it should know LOV
2602 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2604 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2605 exp->exp_connect_flags =
2606 ((struct obd_connect_data *)val)->ocd_connect_flags;
2611 CDEBUG(D_IOCTL, "invalid key\n");
2615 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2616 void *key, obd_count vallen, void *val,
2617 struct ptlrpc_request_set *set)
2619 struct lmv_tgt_desc *tgt;
2620 struct obd_device *obd;
2621 struct lmv_obd *lmv;
2625 obd = class_exp2obd(exp);
2627 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2628 exp->exp_handle.h_cookie);
2633 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2634 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2637 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2638 tgt = &lmv->tgts[i];
2643 err = obd_set_info_async(tgt->ltd_exp,
2644 keylen, key, vallen, val, set);
2655 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2656 struct lov_stripe_md *lsm)
2658 struct obd_device *obd = class_exp2obd(exp);
2659 struct lmv_obd *lmv = &obd->u.lmv;
2660 struct lmv_stripe_md *meap, *lsmp;
2664 mea_size = lmv_get_easize(lmv);
2668 if (*lmmp && !lsm) {
2669 OBD_FREE(*lmmp, mea_size);
2674 if (*lmmp == NULL) {
2675 OBD_ALLOC(*lmmp, mea_size);
2683 lsmp = (struct lmv_stripe_md *)lsm;
2684 meap = (struct lmv_stripe_md *)*lmmp;
2686 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2687 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2690 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2691 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2692 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2694 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2695 meap->mea_ids[i] = meap->mea_ids[i];
2696 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2702 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2703 struct lov_mds_md *lmm, int lmm_size)
2705 struct obd_device *obd = class_exp2obd(exp);
2706 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2707 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2708 struct lmv_obd *lmv = &obd->u.lmv;
2713 mea_size = lmv_get_easize(lmv);
2717 if (*lsmp != NULL && lmm == NULL) {
2718 OBD_FREE(*tmea, mea_size);
2723 LASSERT(mea_size == lmm_size);
2725 OBD_ALLOC(*tmea, mea_size);
2732 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2733 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2734 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2736 magic = le32_to_cpu(mea->mea_magic);
2738 /* old mea is not handled here */
2742 (*tmea)->mea_magic = magic;
2743 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2744 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2746 for (i = 0; i < (*tmea)->mea_count; i++) {
2747 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2748 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2753 static int lmv_cancel_unused(struct obd_export *exp,
2754 const struct lu_fid *fid,
2755 ldlm_policy_data_t *policy,
2756 ldlm_mode_t mode, int flags, void *opaque)
2758 struct obd_device *obd = exp->exp_obd;
2759 struct lmv_obd *lmv = &obd->u.lmv;
2763 LASSERT(fid != NULL);
2765 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2766 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2769 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2770 policy, mode, flags, opaque);
2777 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2779 struct obd_device *obd = exp->exp_obd;
2780 struct lmv_obd *lmv = &obd->u.lmv;
2783 RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2786 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2787 const struct lu_fid *fid, ldlm_type_t type,
2788 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2789 struct lustre_handle *lockh)
2791 struct obd_device *obd = exp->exp_obd;
2792 struct lmv_obd *lmv = &obd->u.lmv;
2797 CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2799 /* with CMD every object can have two locks in different namespaces:
2800 * lookup lock in space of mds storing direntry and update/open lock in
2801 * space of mds storing inode. Thus we check all targets, not only that
2802 * one fid was created in. */
2803 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2804 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2805 type, policy, mode, lockh);
2813 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2814 struct obd_export *dt_exp, struct obd_export *md_exp,
2815 struct lustre_md *md)
2817 struct obd_device *obd = exp->exp_obd;
2818 struct lmv_obd *lmv = &obd->u.lmv;
2822 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2826 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2828 struct obd_device *obd = exp->exp_obd;
2829 struct lmv_obd *lmv = &obd->u.lmv;
2833 /* XXX LOV STACKING */
2835 obd_free_memmd(exp, (void *)&md->mea);
2836 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2839 int lmv_set_open_replay_data(struct obd_export *exp,
2840 struct obd_client_handle *och,
2841 struct ptlrpc_request *open_req)
2843 struct obd_device *obd = exp->exp_obd;
2844 struct lmv_obd *lmv = &obd->u.lmv;
2845 struct obd_export *tgt_exp;
2849 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2850 if (IS_ERR(tgt_exp))
2851 RETURN(PTR_ERR(tgt_exp));
2853 RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2856 int lmv_clear_open_replay_data(struct obd_export *exp,
2857 struct obd_client_handle *och)
2859 struct obd_device *obd = exp->exp_obd;
2860 struct lmv_obd *lmv = &obd->u.lmv;
2861 struct obd_export *tgt_exp;
2864 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2865 if (IS_ERR(tgt_exp))
2866 RETURN(PTR_ERR(tgt_exp));
2868 RETURN(md_clear_open_replay_data(tgt_exp, och));
2871 static int lmv_get_remote_perm(struct obd_export *exp,
2872 const struct lu_fid *fid,
2873 struct obd_capa *oc, __u32 suppgid,
2874 struct ptlrpc_request **request)
2876 struct obd_device *obd = exp->exp_obd;
2877 struct lmv_obd *lmv = &obd->u.lmv;
2878 struct obd_export *tgt_exp;
2883 rc = lmv_check_connect(obd);
2887 tgt_exp = lmv_find_export(lmv, fid);
2888 if (IS_ERR(tgt_exp))
2889 RETURN(PTR_ERR(tgt_exp));
2891 rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2896 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2899 struct obd_device *obd = exp->exp_obd;
2900 struct lmv_obd *lmv = &obd->u.lmv;
2901 struct obd_export *tgt_exp;
2905 rc = lmv_check_connect(obd);
2909 tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2910 if (IS_ERR(tgt_exp))
2911 RETURN(PTR_ERR(tgt_exp));
2913 rc = md_renew_capa(tgt_exp, oc, cb);
2917 int lmv_intent_getattr_async(struct obd_export *exp,
2918 struct md_enqueue_info *minfo,
2919 struct ldlm_enqueue_info *einfo)
2921 struct obd_device *obd = exp->exp_obd;
2922 struct lmv_obd *lmv = &obd->u.lmv;
2923 struct obd_export *tgt_exp;
2927 rc = lmv_check_connect(obd);
2931 if (fid_is_zero(&minfo->mi_data.op_fid2))
2932 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid1);
2934 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid2);
2935 if (IS_ERR(tgt_exp))
2936 RETURN(PTR_ERR(tgt_exp));
2938 rc = md_intent_getattr_async(tgt_exp, minfo, einfo);
2942 int lmv_revalidate_lock(struct obd_export *exp,
2943 struct lookup_intent *it,
2946 struct obd_device *obd = exp->exp_obd;
2947 struct lmv_obd *lmv = &obd->u.lmv;
2948 struct obd_export *tgt_exp;
2952 rc = lmv_check_connect(obd);
2956 tgt_exp = lmv_find_export(lmv, fid);
2957 if (IS_ERR(tgt_exp))
2958 RETURN(PTR_ERR(tgt_exp));
2960 rc = md_revalidate_lock(tgt_exp, it, fid);
2965 struct obd_ops lmv_obd_ops = {
2966 .o_owner = THIS_MODULE,
2967 .o_setup = lmv_setup,
2968 .o_cleanup = lmv_cleanup,
2969 .o_precleanup = lmv_precleanup,
2970 .o_process_config = lmv_process_config,
2971 .o_connect = lmv_connect,
2972 .o_disconnect = lmv_disconnect,
2973 .o_statfs = lmv_statfs,
2974 .o_llog_init = lmv_llog_init,
2975 .o_llog_finish = lmv_llog_finish,
2976 .o_get_info = lmv_get_info,
2977 .o_set_info_async = lmv_set_info_async,
2978 .o_packmd = lmv_packmd,
2979 .o_unpackmd = lmv_unpackmd,
2980 .o_notify = lmv_notify,
2981 .o_iocontrol = lmv_iocontrol,
2982 .o_fid_delete = lmv_fid_delete
2985 struct md_ops lmv_md_ops = {
2986 .m_getstatus = lmv_getstatus,
2987 .m_change_cbdata = lmv_change_cbdata,
2988 .m_close = lmv_close,
2989 .m_create = lmv_create,
2990 .m_done_writing = lmv_done_writing,
2991 .m_enqueue = lmv_enqueue,
2992 .m_getattr = lmv_getattr,
2993 .m_getxattr = lmv_getxattr,
2994 .m_getattr_name = lmv_getattr_name,
2995 .m_intent_lock = lmv_intent_lock,
2997 .m_rename = lmv_rename,
2998 .m_setattr = lmv_setattr,
2999 .m_setxattr = lmv_setxattr,
3001 .m_readpage = lmv_readpage,
3002 .m_unlink = lmv_unlink,
3003 .m_init_ea_size = lmv_init_ea_size,
3004 .m_cancel_unused = lmv_cancel_unused,
3005 .m_set_lock_data = lmv_set_lock_data,
3006 .m_lock_match = lmv_lock_match,
3007 .m_get_lustre_md = lmv_get_lustre_md,
3008 .m_free_lustre_md = lmv_free_lustre_md,
3009 .m_set_open_replay_data = lmv_set_open_replay_data,
3010 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3011 .m_renew_capa = lmv_renew_capa,
3012 .m_get_remote_perm = lmv_get_remote_perm,
3013 .m_intent_getattr_async = lmv_intent_getattr_async,
3014 .m_revalidate_lock = lmv_revalidate_lock
3017 int __init lmv_init(void)
3019 struct lprocfs_static_vars lvars;
3022 obj_cache = cfs_mem_cache_create("lmv_objects",
3023 sizeof(struct lmv_obj),
3026 CERROR("error allocating lmv objects cache\n");
3030 lprocfs_lmv_init_vars(&lvars);
3031 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3032 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3034 cfs_mem_cache_destroy(obj_cache);
3040 static void lmv_exit(void)
3044 class_unregister_type(LUSTRE_LMV_NAME);
3046 rc = cfs_mem_cache_destroy(obj_cache);
3048 "can't free lmv objects cache, %d object(s)"
3049 "still in use\n", atomic_read(&obj_cache_count));
3052 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3053 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3054 MODULE_LICENSE("GPL");
3056 module_init(lmv_init);
3057 module_exit(lmv_exit);