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"
66 /* not defined for liblustre building */
67 #if !defined(ATOMIC_INIT)
68 #define ATOMIC_INIT(val) { (val) }
72 cfs_mem_cache_t *obj_cache;
73 atomic_t obj_cache_count = ATOMIC_INIT(0);
75 static void lmv_activate_target(struct lmv_obd *lmv,
76 struct lmv_tgt_desc *tgt,
79 if (tgt->ltd_active == activate)
82 tgt->ltd_active = activate;
83 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
88 * -EINVAL : UUID can't be found in the LMV's target list
89 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
90 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
92 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
95 struct lmv_tgt_desc *tgt;
96 struct obd_device *obd;
100 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
101 lmv, uuid->uuid, activate);
103 spin_lock(&lmv->lmv_lock);
104 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
105 if (tgt->ltd_exp == NULL)
108 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
109 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
111 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
115 if (i == lmv->desc.ld_tgt_count)
116 GOTO(out_lmv_lock, rc = -EINVAL);
118 obd = class_exp2obd(tgt->ltd_exp);
120 GOTO(out_lmv_lock, rc = -ENOTCONN);
122 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
123 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
124 obd->obd_type->typ_name, i);
125 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
127 if (tgt->ltd_active == activate) {
128 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
129 activate ? "" : "in");
130 GOTO(out_lmv_lock, rc);
133 CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
134 obd, activate ? "" : "in");
136 lmv_activate_target(lmv, tgt, activate);
141 spin_unlock(&lmv->lmv_lock);
145 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
146 struct obd_connect_data *data)
148 struct lmv_tgt_desc *tgt;
152 LASSERT(data != NULL);
154 spin_lock(&lmv->lmv_lock);
155 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
156 if (tgt->ltd_exp == NULL)
159 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
160 lmv->datas[tgt->ltd_idx] = *data;
164 spin_unlock(&lmv->lmv_lock);
168 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
169 enum obd_notify_event ev, void *data)
171 struct lmv_obd *lmv = &obd->u.lmv;
172 struct obd_uuid *uuid;
176 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
177 CERROR("unexpected notification of %s %s!\n",
178 watched->obd_type->typ_name,
183 uuid = &watched->u.cli.cl_target_uuid;
184 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
186 * Set MDC as active before notifying the observer, so the
187 * observer can use the MDC normally.
189 rc = lmv_set_mdc_active(lmv, uuid,
190 ev == OBD_NOTIFY_ACTIVE);
192 CERROR("%sactivation of %s failed: %d\n",
193 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
197 } else if (ev == OBD_NOTIFY_OCD) {
198 struct obd_connect_data *conn_data =
199 &watched->u.cli.cl_import->imp_connect_data;
201 /* Set connect data to desired target, update
202 * exp_connect_flags. */
203 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
205 CERROR("can't set connect data to target %s, rc %d\n",
211 * XXX: Make sure that ocd_connect_flags from all targets are
212 * the same. Otherwise one of MDTs runs wrong version or
213 * something like this. --umka
215 obd->obd_self_export->exp_connect_flags =
216 conn_data->ocd_connect_flags;
219 else if (ev == OBD_NOTIFY_DISCON) {
220 /* For disconnect event, flush fld cache for failout MDS case. */
221 fld_client_flush(&lmv->lmv_fld);
224 /* Pass the notification up the chain. */
225 if (obd->obd_observer)
226 rc = obd_notify(obd->obd_observer, watched, ev, data);
231 /* this is fake connect function. Its purpose is to initialize lmv and say
232 * caller that everything is okay. Real connection will be performed later. */
233 static int lmv_connect(const struct lu_env *env,
234 struct lustre_handle *conn, struct obd_device *obd,
235 struct obd_uuid *cluuid, struct obd_connect_data *data,
239 struct proc_dir_entry *lmv_proc_dir;
241 struct lmv_obd *lmv = &obd->u.lmv;
242 struct obd_export *exp;
246 rc = class_connect(conn, obd, cluuid);
248 CERROR("class_connection() returned %d\n", rc);
252 exp = class_conn2export(conn);
254 /* we don't want to actually do the underlying connections more than
255 * once, so keep track. */
257 if (lmv->refcount > 1) {
258 class_export_put(exp);
264 lmv->cluuid = *cluuid;
267 lmv->conn_data = *data;
270 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
272 if (IS_ERR(lmv_proc_dir)) {
273 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
274 obd->obd_type->typ_name, obd->obd_name);
279 /* all real clients should perform actual connection right away, because
280 * it is possible, that LMV will not have opportunity to connect targets
281 * and MDC stuff will be called directly, for instance while reading
282 * ../mdc/../kbytesfree procfs file, etc. */
283 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
284 rc = lmv_check_connect(obd);
289 lprocfs_remove(&lmv_proc_dir);
296 static void lmv_set_timeouts(struct obd_device *obd)
298 struct lmv_tgt_desc *tgts;
303 if (lmv->server_timeout == 0)
306 if (lmv->connected == 0)
309 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
310 if (tgts->ltd_exp == NULL)
313 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
314 KEY_INTERMDS, 0, NULL, NULL);
318 static int lmv_init_ea_size(struct obd_export *exp, int easize,
319 int def_easize, int cookiesize)
321 struct obd_device *obd = exp->exp_obd;
322 struct lmv_obd *lmv = &obd->u.lmv;
323 int i, rc = 0, change = 0;
326 if (lmv->max_easize < easize) {
327 lmv->max_easize = easize;
330 if (lmv->max_def_easize < def_easize) {
331 lmv->max_def_easize = def_easize;
334 if (lmv->max_cookiesize < cookiesize) {
335 lmv->max_cookiesize = cookiesize;
341 if (lmv->connected == 0)
344 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
345 if (lmv->tgts[i].ltd_exp == NULL) {
346 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
350 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
353 CERROR("obd_init_ea_size() failed on MDT target %d, "
354 "error %d.\n", i, rc);
361 #define MAX_STRING_SIZE 128
363 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
365 struct lmv_obd *lmv = &obd->u.lmv;
366 struct obd_uuid *cluuid = &lmv->cluuid;
367 struct obd_connect_data *mdc_data = NULL;
368 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
369 struct lustre_handle conn = {0, };
370 struct obd_device *mdc_obd;
371 struct obd_export *mdc_exp;
372 struct lu_fld_target target;
375 struct proc_dir_entry *lmv_proc_dir;
379 /* for MDS: don't connect to yourself */
380 if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
381 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
382 /* XXX - the old code didn't increment active tgt count.
387 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
390 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
394 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
395 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
396 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
399 if (!mdc_obd->obd_set_up) {
400 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
404 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
405 &lmv->conn_data, NULL);
407 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
411 mdc_exp = class_conn2export(&conn);
413 /* Init fid sequence client for this mdc. */
414 rc = obd_fid_init(mdc_exp);
418 /* Add new FLD target. */
419 target.ft_srv = NULL;
420 target.ft_exp = mdc_exp;
421 target.ft_idx = tgt->ltd_idx;
423 fld_client_add_target(&lmv->lmv_fld, &target);
425 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
427 rc = obd_register_observer(mdc_obd, obd);
429 obd_disconnect(mdc_exp);
430 CERROR("target %s register_observer error %d\n",
431 tgt->ltd_uuid.uuid, rc);
435 if (obd->obd_observer) {
436 /* tell the mds_lmv about the new target */
437 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
438 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
440 obd_disconnect(mdc_exp);
446 tgt->ltd_exp = mdc_exp;
447 lmv->desc.ld_active_tgt_count++;
449 /* copy connect data, it may be used later */
450 lmv->datas[tgt->ltd_idx] = *mdc_data;
452 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
453 lmv->max_def_easize, lmv->max_cookiesize);
455 CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
456 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
457 atomic_read(&obd->obd_refcount));
460 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
462 struct proc_dir_entry *mdc_symlink;
463 char name[MAX_STRING_SIZE + 1];
465 LASSERT(mdc_obd->obd_type != NULL);
466 LASSERT(mdc_obd->obd_type->typ_name != NULL);
467 name[MAX_STRING_SIZE] = '\0';
468 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
469 mdc_obd->obd_type->typ_name,
471 mdc_symlink = proc_symlink(mdc_obd->obd_name,
473 if (mdc_symlink == NULL) {
474 CERROR("could not register LMV target "
475 "/proc/fs/lustre/%s/%s/target_obds/%s.",
476 obd->obd_type->typ_name, obd->obd_name,
478 lprocfs_remove(&lmv_proc_dir);
486 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
488 struct lmv_obd *lmv = &obd->u.lmv;
489 struct lmv_tgt_desc *tgt;
493 CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
497 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
498 lmv_init_unlock(lmv);
499 CERROR("can't add %s, LMV module compiled for %d MDCs. "
500 "That many MDCs already configured.\n",
501 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
504 if (lmv->desc.ld_tgt_count == 0) {
505 struct obd_device *mdc_obd;
507 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
510 lmv_init_unlock(lmv);
511 CERROR("Target %s not attached\n", tgt_uuid->uuid);
515 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, 0, NULL, tgt_uuid);
517 lmv_init_unlock(lmv);
518 CERROR("lmv failed to setup llogging subsystems\n");
521 spin_lock(&lmv->lmv_lock);
522 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
523 tgt->ltd_uuid = *tgt_uuid;
524 spin_unlock(&lmv->lmv_lock);
526 if (lmv->connected) {
527 rc = lmv_connect_mdc(obd, tgt);
529 spin_lock(&lmv->lmv_lock);
530 lmv->desc.ld_tgt_count--;
531 memset(tgt, 0, sizeof(*tgt));
532 spin_unlock(&lmv->lmv_lock);
534 int easize = sizeof(struct lmv_stripe_md) +
535 lmv->desc.ld_tgt_count *
536 sizeof(struct lu_fid);
537 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
541 lmv_init_unlock(lmv);
545 /* performs a check if passed obd is connected. If no - connect it. */
546 int lmv_check_connect(struct obd_device *obd)
548 struct lmv_obd *lmv = &obd->u.lmv;
549 struct lmv_tgt_desc *tgt;
557 if (lmv->connected) {
558 lmv_init_unlock(lmv);
562 if (lmv->desc.ld_tgt_count == 0) {
563 CERROR("%s: no targets configured.\n", obd->obd_name);
567 CDEBUG(D_CONFIG, "time to connect %s to %s\n",
568 lmv->cluuid.uuid, obd->obd_name);
570 LASSERT(lmv->tgts != NULL);
572 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
573 rc = lmv_connect_mdc(obd, tgt);
578 lmv_set_timeouts(obd);
579 class_export_put(lmv->exp);
581 easize = lmv_get_easize(lmv);
582 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
583 lmv_init_unlock(lmv);
592 --lmv->desc.ld_active_tgt_count;
593 rc2 = obd_disconnect(tgt->ltd_exp);
595 CERROR("error: LMV target %s disconnect on "
596 "MDC idx %d: error %d\n",
597 tgt->ltd_uuid.uuid, i, rc2);
601 class_disconnect(lmv->exp);
602 lmv_init_unlock(lmv);
606 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
609 struct proc_dir_entry *lmv_proc_dir;
611 struct lmv_obd *lmv = &obd->u.lmv;
612 struct obd_device *mdc_obd;
616 LASSERT(tgt != NULL);
617 LASSERT(obd != NULL);
619 mdc_obd = class_exp2obd(tgt->ltd_exp);
622 mdc_obd->obd_no_recov = obd->obd_no_recov;
625 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
627 struct proc_dir_entry *mdc_symlink;
629 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
631 lprocfs_remove(&mdc_symlink);
633 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
634 obd->obd_type->typ_name, obd->obd_name,
639 rc = obd_fid_fini(tgt->ltd_exp);
641 CERROR("Can't finanize fids factory\n");
643 CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
644 tgt->ltd_exp->exp_obd->obd_name,
645 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
647 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
648 rc = obd_disconnect(tgt->ltd_exp);
650 if (tgt->ltd_active) {
651 CERROR("Target %s disconnect error %d\n",
652 tgt->ltd_uuid.uuid, rc);
656 lmv_activate_target(lmv, tgt, 0);
661 static int lmv_disconnect(struct obd_export *exp)
663 struct obd_device *obd = class_exp2obd(exp);
665 struct proc_dir_entry *lmv_proc_dir;
667 struct lmv_obd *lmv = &obd->u.lmv;
674 /* Only disconnect the underlying layers on the final disconnect. */
676 if (lmv->refcount != 0)
679 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
680 if (lmv->tgts[i].ltd_exp == NULL)
682 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
686 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
688 lprocfs_remove(&lmv_proc_dir);
690 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
691 obd->obd_type->typ_name, obd->obd_name);
697 * This is the case when no real connection is established by
698 * lmv_check_connect().
701 class_export_put(exp);
702 rc = class_disconnect(exp);
703 if (lmv->refcount == 0)
708 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
709 int len, void *karg, void *uarg)
711 struct obd_device *obddev = class_exp2obd(exp);
712 struct lmv_obd *lmv = &obddev->u.lmv;
713 int i, rc = 0, set = 0;
716 if (lmv->desc.ld_tgt_count == 0)
720 case IOC_OBD_STATFS: {
721 struct obd_ioctl_data *data = karg;
722 struct obd_device *mdc_obd;
723 struct obd_statfs stat_buf = {0};
726 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
727 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
729 if ((index >= lmv->desc.ld_tgt_count))
732 if (!lmv->tgts[index].ltd_active)
735 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
739 /* got statfs data */
740 rc = obd_statfs(mdc_obd, &stat_buf,
741 cfs_time_current_64() - HZ, 0);
744 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
747 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
752 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
755 if (lmv->tgts[i].ltd_exp == NULL)
758 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
761 if (lmv->tgts[i].ltd_active) {
762 CERROR("error: iocontrol MDC %s on MDT"
763 "idx %d cmd %x: err = %d\n",
764 lmv->tgts[i].ltd_uuid.uuid,
784 static int lmv_all_chars_policy(int count, const char *name,
795 static int lmv_nid_policy(struct lmv_obd *lmv)
797 struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
800 * XXX Hack: to get nid we assume that underlying obd device is mdc.
802 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
803 return id % lmv->desc.ld_tgt_count;
806 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
811 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
813 op_data->op_namelen);
815 return lmv_nid_policy(lmv);
821 CERROR("unsupport type %d \n", type);
825 /* This is _inode_ placement policy function (not name). */
826 static int lmv_placement_policy(struct obd_device *obd,
827 struct md_op_data *op_data,
830 struct lmv_obd *lmv = &obd->u.lmv;
835 LASSERT(mds != NULL);
838 * Allocate new fid on target according to operation type and parent
841 obj = lmv_obj_grab(obd, &op_data->op_fid1);
842 if (obj != NULL || op_data->op_name == NULL ||
843 op_data->op_opc != LUSTRE_OPC_MKDIR) {
845 * Allocate fid for non-dir or for null name or for case parent
852 * If we have this flag turned on, and we see that
853 * parent dir is split, this means, that caller did not
854 * notice split yet. This is race and we would like to
855 * let caller know that.
857 if (op_data->op_bias & MDS_CHECK_SPLIT)
862 * Allocate new fid on same mds where parent fid is located and
863 * where operation will be sent. In case of split dir, ->op_fid1
864 * and ->op_mds here will contain fid and mds of slave directory
865 * object (assigned by caller).
867 *mds = op_data->op_mds;
871 /* XXX: This should be removed later wehn we sure it is not
873 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
879 * Parent directory is not split and we want to create a
880 * directory in it. Let's calculate where to place it according
883 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
891 CERROR("Can't choose MDS, err = %d\n", rc);
893 LASSERT(*mds < lmv->desc.ld_tgt_count);
899 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
902 struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
906 /* New seq alloc and FLD setup should be atomic. */
907 down(&tgt->ltd_fid_sem);
909 /* Asking underlaying tgt layer to allocate new fid. */
910 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
912 LASSERT(fid_is_sane(fid));
914 /* Client switches to new sequence, setup FLD. */
915 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
918 CERROR("Can't create fld entry, rc %d\n", rc);
919 /* Delete just allocated fid sequence */
920 obd_fid_delete(tgt->ltd_exp, NULL);
923 up(&tgt->ltd_fid_sem);
927 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
928 struct md_op_data *op_data)
930 struct obd_device *obd = class_exp2obd(exp);
931 struct lmv_obd *lmv = &obd->u.lmv;
936 LASSERT(op_data != NULL);
937 LASSERT(fid != NULL);
939 rc = lmv_placement_policy(obd, op_data, &mds);
941 CERROR("Can't get target for allocating fid, "
946 rc = __lmv_fid_alloc(lmv, fid, mds);
948 CERROR("Can't alloc new fid, rc %d\n", rc);
955 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
960 if (lmv_obj_delete(exp, fid)) {
961 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
967 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
969 struct lmv_obd *lmv = &obd->u.lmv;
970 struct lprocfs_static_vars lvars;
971 struct lmv_desc *desc;
975 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
976 CERROR("LMV setup requires a descriptor\n");
980 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
981 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
982 CERROR("descriptor size wrong: %d > %d\n",
983 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
987 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
989 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
990 if (lmv->tgts == NULL)
993 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
994 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
995 lmv->tgts[i].ltd_idx = i;
998 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1000 OBD_ALLOC(lmv->datas, lmv->datas_size);
1001 if (lmv->datas == NULL)
1002 GOTO(out_free_tgts, rc = -ENOMEM);
1004 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1005 lmv->desc.ld_tgt_count = 0;
1006 lmv->desc.ld_active_tgt_count = 0;
1007 lmv->max_cookiesize = 0;
1008 lmv->max_def_easize = 0;
1009 lmv->max_easize = 0;
1011 spin_lock_init(&lmv->lmv_lock);
1012 sema_init(&lmv->init_sem, 1);
1014 rc = lmv_obj_setup(obd);
1016 CERROR("Can't setup LMV object manager, "
1018 GOTO(out_free_datas, rc);
1021 lprocfs_lmv_init_vars(&lvars);
1022 lprocfs_obd_setup(obd, lvars.obd_vars);
1025 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1026 0444, &lmv_proc_target_fops, obd);
1028 CWARN("Error adding the target_obd_status file\n");
1031 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1032 LUSTRE_CLI_FLD_HASH_DHT);
1034 CERROR("can't init FLD, err %d\n",
1036 GOTO(out_free_datas, rc);
1042 OBD_FREE(lmv->datas, lmv->datas_size);
1045 OBD_FREE(lmv->tgts, lmv->tgts_size);
1050 static int lmv_cleanup(struct obd_device *obd)
1052 struct lmv_obd *lmv = &obd->u.lmv;
1055 fld_client_fini(&lmv->lmv_fld);
1056 lprocfs_obd_cleanup(obd);
1057 lmv_obj_cleanup(obd);
1058 OBD_FREE(lmv->datas, lmv->datas_size);
1059 OBD_FREE(lmv->tgts, lmv->tgts_size);
1064 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1066 struct lustre_cfg *lcfg = buf;
1067 struct obd_uuid tgt_uuid;
1071 switch(lcfg->lcfg_command) {
1073 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1074 GOTO(out, rc = -EINVAL);
1076 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1077 rc = lmv_add_target(obd, &tgt_uuid);
1080 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1081 GOTO(out, rc = -EINVAL);
1088 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1089 __u64 max_age, __u32 flags)
1091 struct lmv_obd *lmv = &obd->u.lmv;
1092 struct obd_statfs *temp;
1096 rc = lmv_check_connect(obd);
1100 OBD_ALLOC(temp, sizeof(*temp));
1104 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1105 if (lmv->tgts[i].ltd_exp == NULL)
1108 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1111 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1112 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1114 GOTO(out_free_temp, rc);
1119 osfs->os_bavail += temp->os_bavail;
1120 osfs->os_blocks += temp->os_blocks;
1121 osfs->os_ffree += temp->os_ffree;
1122 osfs->os_files += temp->os_files;
1128 OBD_FREE(temp, sizeof(*temp));
1132 static int lmv_getstatus(struct obd_export *exp,
1134 struct obd_capa **pc)
1136 struct obd_device *obd = exp->exp_obd;
1137 struct lmv_obd *lmv = &obd->u.lmv;
1141 rc = lmv_check_connect(obd);
1145 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1150 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1151 struct obd_capa *oc, obd_valid valid, const char *name,
1152 const char *input, int input_size, int output_size,
1153 int flags, struct ptlrpc_request **request)
1155 struct obd_device *obd = exp->exp_obd;
1156 struct lmv_obd *lmv = &obd->u.lmv;
1157 struct obd_export *tgt_exp;
1161 rc = lmv_check_connect(obd);
1165 tgt_exp = lmv_find_export(lmv, fid);
1166 if (IS_ERR(tgt_exp))
1167 RETURN(PTR_ERR(tgt_exp));
1169 rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1170 output_size, flags, request);
1175 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1176 struct obd_capa *oc, obd_valid valid, const char *name,
1177 const char *input, int input_size, int output_size,
1178 int flags, __u32 suppgid,
1179 struct ptlrpc_request **request)
1181 struct obd_device *obd = exp->exp_obd;
1182 struct lmv_obd *lmv = &obd->u.lmv;
1183 struct obd_export *tgt_exp;
1187 rc = lmv_check_connect(obd);
1191 tgt_exp = lmv_find_export(lmv, fid);
1192 if (IS_ERR(tgt_exp))
1193 RETURN(PTR_ERR(tgt_exp));
1195 rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1196 input, input_size, output_size, flags, suppgid,
1202 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1203 struct obd_capa *oc, obd_valid valid, int ea_size,
1204 struct ptlrpc_request **request)
1206 struct obd_device *obd = exp->exp_obd;
1207 struct lmv_obd *lmv = &obd->u.lmv;
1208 struct obd_export *tgt_exp;
1209 struct lmv_obj *obj;
1213 rc = lmv_check_connect(obd);
1217 tgt_exp = lmv_find_export(lmv, fid);
1218 if (IS_ERR(tgt_exp))
1219 RETURN(PTR_ERR(tgt_exp));
1221 rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1225 obj = lmv_obj_grab(obd, fid);
1227 CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1228 obj ? "(split)" : "");
1231 * If object is split, then we loop over all the slaves and gather size
1232 * attribute. In ideal world we would have to gather also mds field from
1233 * all slaves, as object is spread over the cluster and this is
1234 * definitely interesting information and it is not good to loss it,
1238 struct mdt_body *body;
1240 if (*request == NULL) {
1245 body = req_capsule_server_get(&(*request)->rq_pill,
1247 LASSERT(body != NULL);
1251 for (i = 0; i < obj->lo_objcount; i++) {
1252 if (lmv->tgts[i].ltd_exp == NULL) {
1253 CWARN("%s: NULL export for %d\n",
1258 /* skip master obj. */
1259 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1262 lmv_update_body(body, &obj->lo_inodes[i]);
1265 lmv_obj_unlock(obj);
1272 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1273 ldlm_iterator_t it, void *data)
1275 struct obd_device *obd = exp->exp_obd;
1276 struct lmv_obd *lmv = &obd->u.lmv;
1280 rc = lmv_check_connect(obd);
1284 CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1287 * With CMD every object can have two locks in different namespaces:
1288 * lookup lock in space of mds storing direntry and update/open lock in
1289 * space of mds storing inode.
1291 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1292 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1297 static int lmv_close(struct obd_export *exp,
1298 struct md_op_data *op_data,
1299 struct md_open_data *mod,
1300 struct ptlrpc_request **request)
1302 struct obd_device *obd = exp->exp_obd;
1303 struct lmv_obd *lmv = &obd->u.lmv;
1304 struct obd_export *tgt_exp;
1308 rc = lmv_check_connect(obd);
1312 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1313 if (IS_ERR(tgt_exp))
1314 RETURN(PTR_ERR(tgt_exp));
1316 CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1317 rc = md_close(tgt_exp, op_data, mod, request);
1322 * Called in the case MDS returns -ERESTART on create on open, what means that
1323 * directory is split and its LMV presentation object has to be updated.
1325 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1327 struct obd_device *obd = exp->exp_obd;
1328 struct lmv_obd *lmv = &obd->u.lmv;
1329 struct ptlrpc_request *req = NULL;
1330 struct obd_export *tgt_exp;
1331 struct lmv_obj *obj;
1332 struct lustre_md md;
1338 mealen = lmv_get_easize(lmv);
1340 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1342 tgt_exp = lmv_find_export(lmv, fid);
1343 if (IS_ERR(tgt_exp))
1344 RETURN(PTR_ERR(tgt_exp));
1346 /* time to update mea of parent fid */
1347 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1349 CERROR("md_getattr() failed, error %d\n", rc);
1353 rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1355 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1360 GOTO(cleanup, rc = -ENODATA);
1362 obj = lmv_obj_create(exp, fid, md.mea);
1368 /* XXX LOV STACKING */
1369 obd_free_memmd(exp, (void *)&md.mea);
1374 ptlrpc_req_finished(req);
1378 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1379 const void *data, int datalen, int mode, __u32 uid,
1380 __u32 gid, __u32 cap_effective, __u64 rdev,
1381 struct ptlrpc_request **request)
1383 struct obd_device *obd = exp->exp_obd;
1384 struct lmv_obd *lmv = &obd->u.lmv;
1385 struct obd_export *tgt_exp;
1386 struct lmv_obj *obj;
1390 rc = lmv_check_connect(obd);
1394 if (!lmv->desc.ld_active_tgt_count)
1399 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1403 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1404 op_data->op_name, op_data->op_namelen);
1405 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1406 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1407 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1408 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1411 struct lmv_tgt_desc *tgt;
1413 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1414 op_data->op_bias |= MDS_CHECK_SPLIT;
1415 op_data->op_mds = tgt->ltd_idx;
1416 tgt_exp = tgt->ltd_exp;
1419 if (IS_ERR(tgt_exp))
1420 RETURN(PTR_ERR(tgt_exp));
1422 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1423 if (rc == -ERESTART)
1428 CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1429 op_data->op_name, PFID(&op_data->op_fid1));
1431 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1432 rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1433 cap_effective, rdev, request);
1435 if (*request == NULL)
1437 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1438 } else if (rc == -ERESTART) {
1439 LASSERT(*request != NULL);
1440 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1441 "Got -ERESTART during create!\n");
1442 ptlrpc_req_finished(*request);
1446 * Directory got split. Time to update local object and repeat
1447 * the request with proper MDS.
1449 rc = lmv_handle_split(exp, &op_data->op_fid1);
1451 rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1452 op_data, &op_data->op_fid2);
1461 static int lmv_done_writing(struct obd_export *exp,
1462 struct md_op_data *op_data,
1463 struct md_open_data *mod)
1465 struct obd_device *obd = exp->exp_obd;
1466 struct lmv_obd *lmv = &obd->u.lmv;
1467 struct obd_export *tgt_exp;
1471 rc = lmv_check_connect(obd);
1475 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1476 if (IS_ERR(tgt_exp))
1477 RETURN(PTR_ERR(tgt_exp));
1479 rc = md_done_writing(tgt_exp, op_data, mod);
1484 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1485 struct lookup_intent *it, struct md_op_data *op_data,
1486 struct lustre_handle *lockh, void *lmm, int lmmsize)
1488 struct obd_device *obd = exp->exp_obd;
1489 struct lmv_obd *lmv = &obd->u.lmv;
1490 struct lmv_stripe_md *mea = op_data->op_mea1;
1491 struct md_op_data *op_data2;
1492 struct obd_export *tgt_exp;
1496 OBD_ALLOC_PTR(op_data2);
1497 if (op_data2 == NULL)
1500 LASSERT(mea != NULL);
1501 for (i = 0; i < mea->mea_count; i++) {
1502 memset(op_data2, 0, sizeof(*op_data2));
1503 op_data2->op_fid1 = mea->mea_ids[i];
1504 op_data2->op_bias = 0;
1506 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1507 if (IS_ERR(tgt_exp))
1508 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1510 if (tgt_exp == NULL)
1513 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1514 lockh + i, lmm, lmmsize, 0);
1516 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1517 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1522 if (it->d.lustre.it_data) {
1523 struct ptlrpc_request *req;
1524 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1525 ptlrpc_req_finished(req);
1528 if (it->d.lustre.it_status)
1529 GOTO(cleanup, rc = it->d.lustre.it_status);
1534 OBD_FREE_PTR(op_data2);
1537 /* drop all taken locks */
1539 if (lockh[i].cookie)
1540 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1541 lockh[i].cookie = 0;
1548 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1549 struct lookup_intent *it, struct md_op_data *op_data,
1550 struct lustre_handle *lockh, void *lmm, int lmmsize,
1551 int extra_lock_flags)
1553 struct ptlrpc_request *req = it->d.lustre.it_data;
1554 struct obd_device *obd = exp->exp_obd;
1555 struct lmv_obd *lmv = &obd->u.lmv;
1556 struct lustre_handle plock;
1557 struct obd_export *tgt_exp;
1558 struct md_op_data *rdata;
1559 struct lu_fid fid_copy;
1560 struct mdt_body *body;
1564 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1565 LASSERT(body != NULL);
1567 if (!(body->valid & OBD_MD_MDS))
1570 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1571 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1573 /* We got LOOKUP lock, but we really need attrs */
1574 pmode = it->d.lustre.it_lock_mode;
1575 LASSERT(pmode != 0);
1576 memcpy(&plock, lockh, sizeof(plock));
1577 it->d.lustre.it_lock_mode = 0;
1578 it->d.lustre.it_data = NULL;
1579 fid_copy = body->fid1;
1581 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1582 ptlrpc_req_finished(req);
1584 tgt_exp = lmv_find_export(lmv, &fid_copy);
1585 if (IS_ERR(tgt_exp))
1586 GOTO(out, rc = PTR_ERR(tgt_exp));
1588 OBD_ALLOC_PTR(rdata);
1590 GOTO(out, rc = -ENOMEM);
1592 rdata->op_fid1 = fid_copy;
1593 rdata->op_bias = MDS_CROSS_REF;
1595 rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1596 lmm, lmmsize, extra_lock_flags);
1597 OBD_FREE_PTR(rdata);
1600 ldlm_lock_decref(&plock, pmode);
1605 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1606 struct lookup_intent *it, struct md_op_data *op_data,
1607 struct lustre_handle *lockh, void *lmm, int lmmsize,
1608 int extra_lock_flags)
1610 struct obd_device *obd = exp->exp_obd;
1611 struct lmv_obd *lmv = &obd->u.lmv;
1612 struct obd_export *tgt_exp = NULL;
1613 struct lmv_obj *obj;
1617 rc = lmv_check_connect(obd);
1621 if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1622 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1623 lockh, lmm, lmmsize);
1627 if (op_data->op_namelen) {
1628 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1632 /* directory is split. look for right mds for this
1634 mea_idx = raw_name2idx(obj->lo_hashtype,
1636 (char *)op_data->op_name,
1637 op_data->op_namelen);
1638 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1639 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1644 if (tgt_exp == NULL)
1645 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1646 if (IS_ERR(tgt_exp))
1647 RETURN(PTR_ERR(tgt_exp));
1649 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1650 PFID(&op_data->op_fid1));
1652 rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1653 lmm, lmmsize, extra_lock_flags);
1655 if (rc == 0 && it->it_op == IT_OPEN)
1656 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1657 lmm, lmmsize, extra_lock_flags);
1662 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1663 struct obd_capa *oc, const char *filename, int namelen,
1664 obd_valid valid, int ea_size, __u32 suppgid,
1665 struct ptlrpc_request **request)
1667 struct obd_device *obd = exp->exp_obd;
1668 struct lmv_obd *lmv = &obd->u.lmv;
1669 struct lu_fid rid = *fid;
1670 struct obd_export *tgt_exp;
1671 struct mdt_body *body;
1672 struct lmv_obj *obj;
1676 rc = lmv_check_connect(obd);
1683 obj = lmv_obj_grab(obd, &rid);
1687 /* Directory is split. Look for right mds for this name */
1688 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1689 filename, namelen - 1);
1690 rid = obj->lo_inodes[mea_idx].li_fid;
1691 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1693 valid &= ~OBD_MD_FLCKSPLIT;
1695 tgt_exp = lmv_find_export(lmv, &rid);
1696 valid |= OBD_MD_FLCKSPLIT;
1698 if (IS_ERR(tgt_exp))
1699 RETURN(PTR_ERR(tgt_exp));
1701 CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1702 namelen, filename, PFID(fid), PFID(&rid));
1704 rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1705 ea_size, suppgid, request);
1707 body = req_capsule_server_get(&(*request)->rq_pill,
1709 LASSERT(body != NULL);
1711 if (body->valid & OBD_MD_MDS) {
1712 struct ptlrpc_request *req = NULL;
1715 CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1718 tgt_exp = lmv_find_export(lmv, &rid);
1719 if (IS_ERR(tgt_exp)) {
1720 ptlrpc_req_finished(*request);
1721 RETURN(PTR_ERR(tgt_exp));
1724 rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1725 valid | OBD_MD_FLCROSSREF,
1726 ea_size, suppgid, &req);
1727 ptlrpc_req_finished(*request);
1730 } else if (rc == -ERESTART) {
1731 LASSERT(*request != NULL);
1732 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1733 "Got -ERESTART during getattr!\n");
1734 ptlrpc_req_finished(*request);
1738 * Directory got split. Time to update local object and repeat
1739 * the request with proper MDS.
1741 rc = lmv_handle_split(exp, &rid);
1748 #define md_op_data_fid(op_data, fl) \
1749 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1750 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1751 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1752 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1755 /* @tgt_exp is the export the metadata request is sent.
1756 * @fid_exp is the export the cancel should be sent for the current fid.
1757 * if @fid_exp is NULL, the export is found for the current fid.
1758 * @op_data keeps the current fid, which is pointed through @flag.
1759 * @mode, @bits -- lock match parameters. */
1760 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1761 struct obd_export *fid_exp,
1762 struct md_op_data *op_data,
1763 ldlm_mode_t mode, int bits, int flag)
1765 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1766 ldlm_policy_data_t policy = {{0}};
1770 if (!fid_is_sane(fid))
1773 if (fid_exp == NULL)
1774 fid_exp = lmv_find_export(lmv, fid);
1776 if (tgt_exp == fid_exp) {
1777 /* The export is the same as on the target server, cancel
1778 * will be sent along with the main metadata operation. */
1779 op_data->op_flags |= flag;
1783 policy.l_inodebits.bits = bits;
1784 rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1788 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1789 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1790 * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1791 * md_cancel_unused for child export(s). */
1792 static int lmv_early_cancel_stripes(struct obd_export *exp,
1793 struct obd_export *tgt_exp,
1794 struct md_op_data *op_data,
1795 ldlm_mode_t mode, int bits, int flag)
1797 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1798 struct obd_device *obd = exp->exp_obd;
1799 struct lmv_obd *lmv = &obd->u.lmv;
1800 struct obd_export *st_exp;
1801 struct lmv_obj *obj;
1805 if (!fid_is_sane(fid))
1808 obj = lmv_obj_grab(obd, fid);
1810 ldlm_policy_data_t policy = {{0}};
1811 struct lu_fid *st_fid;
1814 policy.l_inodebits.bits = bits;
1815 for (i = 0; i < obj->lo_objcount; i++) {
1816 st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1817 st_fid = &obj->lo_inodes[i].li_fid;
1818 if (tgt_exp != st_exp) {
1819 rc = md_cancel_unused(st_exp, st_fid, &policy,
1820 mode, LDLM_FL_ASYNC,
1825 /* Some export matches to @tgt_exp, do cancel
1826 * for its fid in mdc */
1828 op_data->op_flags |= flag;
1833 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1841 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1844 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1845 struct ptlrpc_request **request)
1847 struct obd_device *obd = exp->exp_obd;
1848 struct lmv_obd *lmv = &obd->u.lmv;
1849 struct obd_export *tgt_exp;
1850 struct lmv_obj *obj;
1855 rc = lmv_check_connect(obd);
1862 if (op_data->op_namelen != 0) {
1865 /* Usual link request */
1866 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1868 mea_idx = raw_name2idx(obj->lo_hashtype,
1871 op_data->op_namelen);
1872 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1873 mds = obj->lo_inodes[mea_idx].li_mds;
1876 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1881 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1882 PFID(&op_data->op_fid2), op_data->op_namelen,
1883 op_data->op_name, PFID(&op_data->op_fid1));
1885 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1889 /* request from MDS to acquire i_links for inode by fid1 */
1890 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1891 PFID(&op_data->op_fid1));
1894 CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1895 mds, PFID(&op_data->op_fid1));
1897 op_data->op_fsuid = current->fsuid;
1898 op_data->op_fsgid = current->fsgid;
1899 op_data->op_cap = current->cap_effective;
1901 tgt_exp = lmv->tgts[mds].ltd_exp;
1902 if (op_data->op_namelen) {
1903 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1904 /* Cancel UPDATE lock on child (fid1). */
1905 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1906 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1909 rc = md_link(tgt_exp, op_data, request);
1910 if (rc == -ERESTART) {
1911 LASSERT(*request != NULL);
1912 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1913 "Got -ERESTART during link!\n");
1914 ptlrpc_req_finished(*request);
1918 * Directory got split. Time to update local object and repeat
1919 * the request with proper MDS.
1921 rc = lmv_handle_split(exp, &op_data->op_fid2);
1929 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1930 const char *old, int oldlen, const char *new, int newlen,
1931 struct ptlrpc_request **request)
1933 struct obd_export *tgt_exp = NULL, *src_exp;
1934 struct obd_device *obd = exp->exp_obd;
1935 struct lmv_obd *lmv = &obd->u.lmv;
1936 int rc, mea_idx, loop = 0;
1937 struct lmv_obj *obj;
1941 CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1942 oldlen, old, PFID(&op_data->op_fid1),
1943 newlen, new, PFID(&op_data->op_fid2));
1945 rc = lmv_check_connect(obd);
1951 * MDS with old dir entry is asking another MDS to create name
1955 "create %*s(%d/%d) in "DFID" pointing "
1956 "to "DFID"\n", newlen, new, oldlen, newlen,
1957 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1959 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1964 * Target directory can be split, sowe should forward request to
1967 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1969 mea_idx = raw_name2idx(obj->lo_hashtype,
1971 (char *)new, newlen);
1972 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1973 CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1974 PFID(&op_data->op_fid2));
1983 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1986 * directory is already split, so we have to forward request to
1989 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1990 (char *)old, oldlen);
1991 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1992 mds1 = obj->lo_inodes[mea_idx].li_mds;
1993 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1996 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2001 obj = lmv_obj_grab(obd, &op_data->op_fid2);
2004 * Directory is already split, so we have to forward request to
2007 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2008 (char *)new, newlen);
2010 mds2 = obj->lo_inodes[mea_idx].li_mds;
2011 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
2012 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2015 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2021 op_data->op_fsuid = current->fsuid;
2022 op_data->op_fsgid = current->fsgid;
2023 op_data->op_cap = current->cap_effective;
2025 src_exp = lmv_get_export(lmv, mds1);
2026 tgt_exp = lmv_get_export(lmv, mds2);
2028 /* LOOKUP lock on src child (fid3) should also be cancelled for
2029 * src_exp in mdc_rename. */
2030 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2032 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2034 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2035 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2037 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2039 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2040 LCK_EX, MDS_INODELOCK_LOOKUP,
2041 MF_MDC_CANCEL_FID4);
2043 /* XXX: the case when child is a striped dir is not supported.
2044 * Only the master stripe has all locks cancelled early. */
2045 /* Cancel all the locks on tgt child (fid4). */
2047 rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2048 LCK_EX, MDS_INODELOCK_FULL,
2049 MF_MDC_CANCEL_FID4);
2053 rc = md_rename(src_exp, op_data, old, oldlen,
2054 new, newlen, request);
2055 if (rc == -ERESTART) {
2056 LASSERT(*request != NULL);
2057 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2058 "Got -ERESTART during rename!\n");
2059 ptlrpc_req_finished(*request);
2063 * Directory got split. Time to update local object and repeat
2064 * the request with proper MDS.
2066 rc = lmv_handle_split(exp, &op_data->op_fid1);
2073 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2074 void *ea, int ealen, void *ea2, int ea2len,
2075 struct ptlrpc_request **request,
2076 struct md_open_data **mod)
2078 struct obd_device *obd = exp->exp_obd;
2079 struct lmv_obd *lmv = &obd->u.lmv;
2080 struct ptlrpc_request *req;
2081 struct obd_export *tgt_exp;
2082 struct lmv_obj *obj;
2086 rc = lmv_check_connect(obd);
2090 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2092 CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2093 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2094 obj ? ", split" : "");
2096 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2098 for (i = 0; i < obj->lo_objcount; i++) {
2099 op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2101 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2102 if (IS_ERR(tgt_exp)) {
2103 rc = PTR_ERR(tgt_exp);
2107 rc = md_setattr(tgt_exp, op_data, ea, ealen,
2108 ea2, ea2len, &req, mod);
2110 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2112 * this is master object and this request should
2113 * be returned back to llite.
2117 ptlrpc_req_finished(req);
2125 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2126 if (IS_ERR(tgt_exp))
2127 RETURN(PTR_ERR(tgt_exp));
2129 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2130 ea2len, request, mod);
2135 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2136 struct obd_capa *oc, struct ptlrpc_request **request)
2138 struct obd_device *obd = exp->exp_obd;
2139 struct lmv_obd *lmv = &obd->u.lmv;
2140 struct obd_export *tgt_exp;
2144 rc = lmv_check_connect(obd);
2148 tgt_exp = lmv_find_export(lmv, fid);
2149 if (IS_ERR(tgt_exp))
2150 RETURN(PTR_ERR(tgt_exp));
2152 rc = md_sync(tgt_exp, fid, oc, request);
2156 /* main purpose of LMV blocking ast is to remove split directory LMV
2157 * presentation object (struct lmv_obj) attached to the lock being revoked. */
2158 int lmv_blocking_ast(struct ldlm_lock *lock,
2159 struct ldlm_lock_desc *desc,
2160 void *data, int flag)
2162 struct lustre_handle lockh;
2163 struct lmv_obj *obj;
2168 case LDLM_CB_BLOCKING:
2169 ldlm_lock2handle(lock, &lockh);
2170 rc = ldlm_cli_cancel(&lockh);
2172 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2176 case LDLM_CB_CANCELING:
2177 /* time to drop cached attrs for dirobj */
2178 obj = lock->l_ast_data;
2180 CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2181 ", master "DFID"\n",
2182 lock->l_resource->lr_name.name[3] == 1 ?
2183 "LOOKUP" : "UPDATE",
2184 lock->l_resource->lr_name.name[0],
2185 lock->l_resource->lr_name.name[1],
2186 PFID(&obj->lo_fid));
2196 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2200 val = le64_to_cpu(*hash);
2202 val += MAX_HASH_SIZE;
2203 if (val != DIR_END_OFF)
2204 *hash = cpu_to_le64(val - hash_adj);
2207 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2210 struct obd_import *imp;
2213 * XXX Hack: to get nid we assume that underlying obd device is mdc.
2215 imp = class_exp2cliimp(exp);
2216 id = imp->imp_connection->c_self + fid_flatten(fid);
2218 CDEBUG(D_INFO, "node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2219 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2221 return id ^ (id >> 32);
2224 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2225 struct obd_capa *oc, __u64 offset64, struct page *page,
2226 struct ptlrpc_request **request)
2228 struct obd_device *obd = exp->exp_obd;
2229 struct lmv_obd *lmv = &obd->u.lmv;
2230 struct obd_export *tgt_exp;
2231 struct lu_fid rid = *fid;
2232 struct lmv_obj *obj;
2246 rc = lmv_check_connect(obd);
2250 CDEBUG(D_INFO, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2252 obj = lmv_obj_grab(obd, fid);
2256 * This case handle directory lookup in clustered metadata case (i.e.
2257 * split directory is located on multiple md servers.)
2258 * each server keeps directory entries for certain range of hashes.
2259 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2260 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2261 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2263 * readdir can simply start reading entries from 0 - N server in
2264 * order but that will not scale well as all client will request dir in
2265 * to server in same order.
2266 * Following algorithm does optimization:
2267 * Instead of doing readdir in 1, 2, ...., N order, client with a
2268 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2269 * (every client has rank R)
2270 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2271 * since client ask dir from MDS{R} client has pages with offsets
2272 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2273 * on hash values that we get.
2276 struct lmv_inode *loi;
2280 nr = obj->lo_objcount;
2282 seg_size = MAX_HASH_SIZE;
2283 do_div(seg_size, nr);
2284 loi = obj->lo_inodes;
2285 rank = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2288 do_div(tgt_tmp, seg_size);
2289 tgt0 = do_div(tgt_tmp, nr);
2290 tgt = (tgt0 + rank) % nr;
2296 * Last segment has unusual length due to division
2299 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2303 hash_adj += rank * seg_size;
2305 CDEBUG(D_INFO, "hash_adj: %x "LPX64" "LPX64"/%x -> "LPX64"/%x\n",
2306 rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2308 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2309 rid = obj->lo_inodes[tgt].li_fid;
2310 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2312 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2313 PFID(&rid), (unsigned long)offset, tgt);
2315 tgt_exp = lmv_find_export(lmv, &rid);
2317 if (IS_ERR(tgt_exp))
2318 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2320 rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2324 struct lu_dirpage *dp;
2325 struct lu_dirent *ent;
2327 dp = cfs_kmap(page);
2329 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2330 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2331 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2333 for (ent = lu_dirent_start(dp); ent != NULL;
2334 ent = lu_dirent_next(ent))
2335 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2337 if (tgt0 != nr - 1) {
2340 end = le64_to_cpu(dp->ldp_hash_end);
2341 if (end == DIR_END_OFF) {
2342 dp->ldp_hash_end = cpu_to_le32(seg_size *
2345 ""DFID" reset end "LPX64" tgt %d\n",
2347 le64_to_cpu(dp->ldp_hash_end), tgt);
2353 * Here we could remove "." and ".." from all pages which at not from
2354 * master. But MDS has only "." and ".." for master dir.
2359 lmv_obj_unlock(obj);
2365 static int lmv_unlink_slaves(struct obd_export *exp,
2366 struct md_op_data *op_data,
2367 struct ptlrpc_request **req)
2369 struct obd_device *obd = exp->exp_obd;
2370 struct lmv_obd *lmv = &obd->u.lmv;
2371 struct lmv_stripe_md *mea = op_data->op_mea1;
2372 struct md_op_data *op_data2;
2373 struct obd_export *tgt_exp;
2377 OBD_ALLOC_PTR(op_data2);
2378 if (op_data2 == NULL)
2381 op_data2->op_mode = S_IFDIR;
2382 op_data2->op_fsuid = current->fsuid;
2383 op_data2->op_fsgid = current->fsgid;
2384 op_data2->op_bias = 0;
2386 LASSERT(mea != NULL);
2387 for (i = 0; i < mea->mea_count; i++) {
2388 memset(op_data2, 0, sizeof(*op_data2));
2389 op_data2->op_fid1 = mea->mea_ids[i];
2390 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2391 if (IS_ERR(tgt_exp))
2392 GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2394 if (tgt_exp == NULL)
2397 rc = md_unlink(tgt_exp, op_data2, req);
2399 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2400 PFID(&mea->mea_ids[i]), rc);
2403 ptlrpc_req_finished(*req);
2407 GOTO(out_free_op_data2, rc);
2412 OBD_FREE_PTR(op_data2);
2416 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2417 struct ptlrpc_request **request)
2419 struct obd_device *obd = exp->exp_obd;
2420 struct lmv_obd *lmv = &obd->u.lmv;
2421 struct obd_export *tgt_exp = NULL;
2422 struct lmv_obj *obj;
2426 rc = lmv_check_connect(obd);
2430 if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2431 /* mds asks to remove slave objects */
2432 rc = lmv_unlink_slaves(exp, op_data, request);
2439 if (op_data->op_namelen != 0) {
2442 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2444 mea_idx = raw_name2idx(obj->lo_hashtype,
2447 op_data->op_namelen);
2448 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2449 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2450 tgt_exp = lmv_get_export(lmv,
2451 obj->lo_inodes[mea_idx].li_mds);
2453 CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2454 op_data->op_namelen, op_data->op_name,
2455 PFID(&op_data->op_fid1), mea_idx);
2458 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2459 PFID(&op_data->op_fid1));
2461 if (tgt_exp == NULL) {
2462 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2463 if (IS_ERR(tgt_exp))
2464 RETURN(PTR_ERR(tgt_exp));
2465 op_data->op_bias |= MDS_CHECK_SPLIT;
2468 op_data->op_fsuid = current->fsuid;
2469 op_data->op_fsgid = current->fsgid;
2470 op_data->op_cap = current->cap_effective;
2472 /* If child's fid is given, cancel unused locks for it if it is from
2473 * another export than parent. */
2474 if (op_data->op_namelen) {
2475 /* LOOKUP lock for child (fid3) should also be cancelled on
2476 * parent tgt_exp in mdc_unlink(). */
2477 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2479 /* XXX: the case when child is a striped dir is not supported.
2480 * Only the master stripe has all locks cancelled early. */
2481 /* Cancel FULL locks on child (fid3). */
2482 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2483 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2486 rc = md_unlink(tgt_exp, op_data, request);
2487 if (rc == -ERESTART) {
2488 LASSERT(*request != NULL);
2489 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2490 "Got -ERESTART during unlink!\n");
2491 ptlrpc_req_finished(*request);
2495 * Directory got split. Time to update local object and repeat
2496 * the request with proper MDS.
2498 rc = lmv_handle_split(exp, &op_data->op_fid1);
2505 static int lmv_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2506 struct obd_device *tgt, int count,
2507 struct llog_catid *logid, struct obd_uuid *uuid)
2510 struct llog_ctxt *ctxt;
2514 LASSERT(group == OBD_LLOG_GROUP);
2515 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2518 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2519 llog_initiator_connect(ctxt, tgt);
2520 llog_ctxt_put(ctxt);
2528 static int lmv_llog_finish(struct obd_device *obd, int count)
2530 struct llog_ctxt *ctxt;
2534 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2536 rc = llog_cleanup(ctxt);
2541 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2546 case OBD_CLEANUP_EARLY:
2547 /* XXX: here should be calling obd_precleanup() down to
2550 case OBD_CLEANUP_EXPORTS:
2551 rc = obd_llog_finish(obd, 0);
2553 CERROR("failed to cleanup llogging subsystems\n");
2561 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2562 void *key, __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2564 struct obd_device *obd;
2565 struct lmv_obd *lmv;
2569 obd = class_exp2obd(exp);
2571 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2572 exp->exp_handle.h_cookie);
2577 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2578 struct lmv_tgt_desc *tgts;
2581 rc = lmv_check_connect(obd);
2585 LASSERT(*vallen == sizeof(__u32));
2586 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2589 /* all tgts should be connected when this get called. */
2590 if (!tgts || !tgts->ltd_exp) {
2591 CERROR("target not setup?\n");
2595 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2600 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2601 rc = lmv_check_connect(obd);
2605 /* forwarding this request to first MDS, it should know LOV
2607 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2609 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2610 exp->exp_connect_flags =
2611 ((struct obd_connect_data *)val)->ocd_connect_flags;
2616 CDEBUG(D_IOCTL, "invalid key\n");
2620 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2621 void *key, obd_count vallen, void *val,
2622 struct ptlrpc_request_set *set)
2624 struct lmv_tgt_desc *tgt;
2625 struct obd_device *obd;
2626 struct lmv_obd *lmv;
2630 obd = class_exp2obd(exp);
2632 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2633 exp->exp_handle.h_cookie);
2638 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2639 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2642 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2643 tgt = &lmv->tgts[i];
2648 err = obd_set_info_async(tgt->ltd_exp,
2649 keylen, key, vallen, val, set);
2660 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2661 struct lov_stripe_md *lsm)
2663 struct obd_device *obd = class_exp2obd(exp);
2664 struct lmv_obd *lmv = &obd->u.lmv;
2665 struct lmv_stripe_md *meap, *lsmp;
2669 mea_size = lmv_get_easize(lmv);
2673 if (*lmmp && !lsm) {
2674 OBD_FREE(*lmmp, mea_size);
2679 if (*lmmp == NULL) {
2680 OBD_ALLOC(*lmmp, mea_size);
2688 lsmp = (struct lmv_stripe_md *)lsm;
2689 meap = (struct lmv_stripe_md *)*lmmp;
2691 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2692 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2695 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2696 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2697 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2699 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2700 meap->mea_ids[i] = meap->mea_ids[i];
2701 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2707 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2708 struct lov_mds_md *lmm, int lmm_size)
2710 struct obd_device *obd = class_exp2obd(exp);
2711 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2712 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2713 struct lmv_obd *lmv = &obd->u.lmv;
2718 mea_size = lmv_get_easize(lmv);
2722 if (*lsmp != NULL && lmm == NULL) {
2723 OBD_FREE(*tmea, mea_size);
2728 LASSERT(mea_size == lmm_size);
2730 OBD_ALLOC(*tmea, mea_size);
2737 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2738 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2739 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2741 magic = le32_to_cpu(mea->mea_magic);
2743 /* old mea is not handled here */
2747 (*tmea)->mea_magic = magic;
2748 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2749 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2751 for (i = 0; i < (*tmea)->mea_count; i++) {
2752 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2753 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2758 static int lmv_cancel_unused(struct obd_export *exp,
2759 const struct lu_fid *fid,
2760 ldlm_policy_data_t *policy,
2761 ldlm_mode_t mode, int flags, void *opaque)
2763 struct obd_device *obd = exp->exp_obd;
2764 struct lmv_obd *lmv = &obd->u.lmv;
2768 LASSERT(fid != NULL);
2770 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2771 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2774 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2775 policy, mode, flags, opaque);
2782 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2784 struct obd_device *obd = exp->exp_obd;
2785 struct lmv_obd *lmv = &obd->u.lmv;
2788 RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2791 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2792 const struct lu_fid *fid, ldlm_type_t type,
2793 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2794 struct lustre_handle *lockh)
2796 struct obd_device *obd = exp->exp_obd;
2797 struct lmv_obd *lmv = &obd->u.lmv;
2802 CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2804 /* with CMD every object can have two locks in different namespaces:
2805 * lookup lock in space of mds storing direntry and update/open lock in
2806 * space of mds storing inode. Thus we check all targets, not only that
2807 * one fid was created in. */
2808 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2809 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2810 type, policy, mode, lockh);
2818 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2819 struct obd_export *dt_exp, struct obd_export *md_exp,
2820 struct lustre_md *md)
2822 struct obd_device *obd = exp->exp_obd;
2823 struct lmv_obd *lmv = &obd->u.lmv;
2827 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2831 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2833 struct obd_device *obd = exp->exp_obd;
2834 struct lmv_obd *lmv = &obd->u.lmv;
2838 /* XXX LOV STACKING */
2840 obd_free_memmd(exp, (void *)&md->mea);
2841 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2844 int lmv_set_open_replay_data(struct obd_export *exp,
2845 struct obd_client_handle *och,
2846 struct ptlrpc_request *open_req)
2848 struct obd_device *obd = exp->exp_obd;
2849 struct lmv_obd *lmv = &obd->u.lmv;
2850 struct obd_export *tgt_exp;
2854 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2855 if (IS_ERR(tgt_exp))
2856 RETURN(PTR_ERR(tgt_exp));
2858 RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2861 int lmv_clear_open_replay_data(struct obd_export *exp,
2862 struct obd_client_handle *och)
2864 struct obd_device *obd = exp->exp_obd;
2865 struct lmv_obd *lmv = &obd->u.lmv;
2866 struct obd_export *tgt_exp;
2869 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2870 if (IS_ERR(tgt_exp))
2871 RETURN(PTR_ERR(tgt_exp));
2873 RETURN(md_clear_open_replay_data(tgt_exp, och));
2876 static int lmv_get_remote_perm(struct obd_export *exp,
2877 const struct lu_fid *fid,
2878 struct obd_capa *oc, __u32 suppgid,
2879 struct ptlrpc_request **request)
2881 struct obd_device *obd = exp->exp_obd;
2882 struct lmv_obd *lmv = &obd->u.lmv;
2883 struct obd_export *tgt_exp;
2888 rc = lmv_check_connect(obd);
2892 tgt_exp = lmv_find_export(lmv, fid);
2893 if (IS_ERR(tgt_exp))
2894 RETURN(PTR_ERR(tgt_exp));
2896 rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2901 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2904 struct obd_device *obd = exp->exp_obd;
2905 struct lmv_obd *lmv = &obd->u.lmv;
2906 struct obd_export *tgt_exp;
2910 rc = lmv_check_connect(obd);
2914 tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2915 if (IS_ERR(tgt_exp))
2916 RETURN(PTR_ERR(tgt_exp));
2918 rc = md_renew_capa(tgt_exp, oc, cb);
2922 int lmv_intent_getattr_async(struct obd_export *exp,
2923 struct md_enqueue_info *minfo,
2924 struct ldlm_enqueue_info *einfo)
2926 struct obd_device *obd = exp->exp_obd;
2927 struct lmv_obd *lmv = &obd->u.lmv;
2928 struct obd_export *tgt_exp;
2932 rc = lmv_check_connect(obd);
2936 if (fid_is_zero(&minfo->mi_data.op_fid2))
2937 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid1);
2939 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid2);
2940 if (IS_ERR(tgt_exp))
2941 RETURN(PTR_ERR(tgt_exp));
2943 rc = md_intent_getattr_async(tgt_exp, minfo, einfo);
2947 int lmv_revalidate_lock(struct obd_export *exp,
2948 struct lookup_intent *it,
2951 struct obd_device *obd = exp->exp_obd;
2952 struct lmv_obd *lmv = &obd->u.lmv;
2953 struct obd_export *tgt_exp;
2957 rc = lmv_check_connect(obd);
2961 tgt_exp = lmv_find_export(lmv, fid);
2962 if (IS_ERR(tgt_exp))
2963 RETURN(PTR_ERR(tgt_exp));
2965 rc = md_revalidate_lock(tgt_exp, it, fid);
2970 struct obd_ops lmv_obd_ops = {
2971 .o_owner = THIS_MODULE,
2972 .o_setup = lmv_setup,
2973 .o_cleanup = lmv_cleanup,
2974 .o_precleanup = lmv_precleanup,
2975 .o_process_config = lmv_process_config,
2976 .o_connect = lmv_connect,
2977 .o_disconnect = lmv_disconnect,
2978 .o_statfs = lmv_statfs,
2979 .o_llog_init = lmv_llog_init,
2980 .o_llog_finish = lmv_llog_finish,
2981 .o_get_info = lmv_get_info,
2982 .o_set_info_async = lmv_set_info_async,
2983 .o_packmd = lmv_packmd,
2984 .o_unpackmd = lmv_unpackmd,
2985 .o_notify = lmv_notify,
2986 .o_iocontrol = lmv_iocontrol,
2987 .o_fid_delete = lmv_fid_delete
2990 struct md_ops lmv_md_ops = {
2991 .m_getstatus = lmv_getstatus,
2992 .m_change_cbdata = lmv_change_cbdata,
2993 .m_close = lmv_close,
2994 .m_create = lmv_create,
2995 .m_done_writing = lmv_done_writing,
2996 .m_enqueue = lmv_enqueue,
2997 .m_getattr = lmv_getattr,
2998 .m_getxattr = lmv_getxattr,
2999 .m_getattr_name = lmv_getattr_name,
3000 .m_intent_lock = lmv_intent_lock,
3002 .m_rename = lmv_rename,
3003 .m_setattr = lmv_setattr,
3004 .m_setxattr = lmv_setxattr,
3006 .m_readpage = lmv_readpage,
3007 .m_unlink = lmv_unlink,
3008 .m_init_ea_size = lmv_init_ea_size,
3009 .m_cancel_unused = lmv_cancel_unused,
3010 .m_set_lock_data = lmv_set_lock_data,
3011 .m_lock_match = lmv_lock_match,
3012 .m_get_lustre_md = lmv_get_lustre_md,
3013 .m_free_lustre_md = lmv_free_lustre_md,
3014 .m_set_open_replay_data = lmv_set_open_replay_data,
3015 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3016 .m_renew_capa = lmv_renew_capa,
3017 .m_get_remote_perm = lmv_get_remote_perm,
3018 .m_intent_getattr_async = lmv_intent_getattr_async,
3019 .m_revalidate_lock = lmv_revalidate_lock
3022 int __init lmv_init(void)
3024 struct lprocfs_static_vars lvars;
3027 obj_cache = cfs_mem_cache_create("lmv_objects",
3028 sizeof(struct lmv_obj),
3031 CERROR("error allocating lmv objects cache\n");
3035 lprocfs_lmv_init_vars(&lvars);
3036 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3037 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3039 cfs_mem_cache_destroy(obj_cache);
3045 static void lmv_exit(void)
3049 class_unregister_type(LUSTRE_LMV_NAME);
3051 rc = cfs_mem_cache_destroy(obj_cache);
3053 "can't free lmv objects cache, %d object(s)"
3054 "still in use\n", atomic_read(&obj_cache_count));
3057 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3058 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3059 MODULE_LICENSE("GPL");
3061 module_init(lmv_init);
3062 module_exit(lmv_exit);