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 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
164 struct obd_device *obd = exp->exp_obd;
165 struct lmv_obd *lmv = &obd->u.lmv;
166 return obd_get_uuid(lmv->tgts[0].ltd_exp);
169 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
170 enum obd_notify_event ev, void *data)
172 struct lmv_obd *lmv = &obd->u.lmv;
173 struct obd_uuid *uuid;
177 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
178 CERROR("unexpected notification of %s %s!\n",
179 watched->obd_type->typ_name,
184 uuid = &watched->u.cli.cl_target_uuid;
185 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
187 * Set MDC as active before notifying the observer, so the
188 * observer can use the MDC normally.
190 rc = lmv_set_mdc_active(lmv, uuid,
191 ev == OBD_NOTIFY_ACTIVE);
193 CERROR("%sactivation of %s failed: %d\n",
194 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
198 } else if (ev == OBD_NOTIFY_OCD) {
199 struct obd_connect_data *conn_data =
200 &watched->u.cli.cl_import->imp_connect_data;
202 /* Set connect data to desired target, update
203 * exp_connect_flags. */
204 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
206 CERROR("can't set connect data to target %s, rc %d\n",
212 * XXX: Make sure that ocd_connect_flags from all targets are
213 * the same. Otherwise one of MDTs runs wrong version or
214 * something like this. --umka
216 obd->obd_self_export->exp_connect_flags =
217 conn_data->ocd_connect_flags;
220 else if (ev == OBD_NOTIFY_DISCON) {
221 /* For disconnect event, flush fld cache for failout MDS case. */
222 fld_client_flush(&lmv->lmv_fld);
225 /* Pass the notification up the chain. */
226 if (obd->obd_observer)
227 rc = obd_notify(obd->obd_observer, watched, ev, data);
232 /* this is fake connect function. Its purpose is to initialize lmv and say
233 * caller that everything is okay. Real connection will be performed later. */
234 static int lmv_connect(const struct lu_env *env,
235 struct lustre_handle *conn, struct obd_device *obd,
236 struct obd_uuid *cluuid, struct obd_connect_data *data,
240 struct proc_dir_entry *lmv_proc_dir;
242 struct lmv_obd *lmv = &obd->u.lmv;
243 struct obd_export *exp;
247 rc = class_connect(conn, obd, cluuid);
249 CERROR("class_connection() returned %d\n", rc);
253 exp = class_conn2export(conn);
255 /* we don't want to actually do the underlying connections more than
256 * once, so keep track. */
258 if (lmv->refcount > 1) {
259 class_export_put(exp);
265 lmv->cluuid = *cluuid;
268 lmv->conn_data = *data;
271 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
273 if (IS_ERR(lmv_proc_dir)) {
274 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
275 obd->obd_type->typ_name, obd->obd_name);
280 /* all real clients should perform actual connection right away, because
281 * it is possible, that LMV will not have opportunity to connect targets
282 * and MDC stuff will be called directly, for instance while reading
283 * ../mdc/../kbytesfree procfs file, etc. */
284 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
285 rc = lmv_check_connect(obd);
290 lprocfs_remove(&lmv_proc_dir);
297 static void lmv_set_timeouts(struct obd_device *obd)
299 struct lmv_tgt_desc *tgts;
304 if (lmv->server_timeout == 0)
307 if (lmv->connected == 0)
310 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
311 if (tgts->ltd_exp == NULL)
314 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
315 KEY_INTERMDS, 0, NULL, NULL);
319 static int lmv_init_ea_size(struct obd_export *exp, int easize,
320 int def_easize, int cookiesize)
322 struct obd_device *obd = exp->exp_obd;
323 struct lmv_obd *lmv = &obd->u.lmv;
324 int i, rc = 0, change = 0;
327 if (lmv->max_easize < easize) {
328 lmv->max_easize = easize;
331 if (lmv->max_def_easize < def_easize) {
332 lmv->max_def_easize = def_easize;
335 if (lmv->max_cookiesize < cookiesize) {
336 lmv->max_cookiesize = cookiesize;
342 if (lmv->connected == 0)
345 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
346 if (lmv->tgts[i].ltd_exp == NULL) {
347 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
351 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
354 CERROR("obd_init_ea_size() failed on MDT target %d, "
355 "error %d.\n", i, rc);
362 #define MAX_STRING_SIZE 128
364 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
366 struct lmv_obd *lmv = &obd->u.lmv;
367 struct obd_uuid *cluuid = &lmv->cluuid;
368 struct obd_connect_data *mdc_data = NULL;
369 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
370 struct lustre_handle conn = {0, };
371 struct obd_device *mdc_obd;
372 struct obd_export *mdc_exp;
373 struct lu_fld_target target;
376 struct proc_dir_entry *lmv_proc_dir;
380 /* for MDS: don't connect to yourself */
381 if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
382 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
383 /* XXX - the old code didn't increment active tgt count.
388 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
391 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
395 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
396 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
397 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
400 if (!mdc_obd->obd_set_up) {
401 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
405 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
406 &lmv->conn_data, NULL);
408 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
412 mdc_exp = class_conn2export(&conn);
414 /* Init fid sequence client for this mdc. */
415 rc = obd_fid_init(mdc_exp);
419 /* Add new FLD target. */
420 target.ft_srv = NULL;
421 target.ft_exp = mdc_exp;
422 target.ft_idx = tgt->ltd_idx;
424 fld_client_add_target(&lmv->lmv_fld, &target);
426 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
428 rc = obd_register_observer(mdc_obd, obd);
430 obd_disconnect(mdc_exp);
431 CERROR("target %s register_observer error %d\n",
432 tgt->ltd_uuid.uuid, rc);
436 if (obd->obd_observer) {
437 /* tell the mds_lmv about the new target */
438 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
439 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
441 obd_disconnect(mdc_exp);
447 tgt->ltd_exp = mdc_exp;
448 lmv->desc.ld_active_tgt_count++;
450 /* copy connect data, it may be used later */
451 lmv->datas[tgt->ltd_idx] = *mdc_data;
453 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
454 lmv->max_def_easize, lmv->max_cookiesize);
456 CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
457 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
458 atomic_read(&obd->obd_refcount));
461 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
463 struct proc_dir_entry *mdc_symlink;
464 char name[MAX_STRING_SIZE + 1];
466 LASSERT(mdc_obd->obd_type != NULL);
467 LASSERT(mdc_obd->obd_type->typ_name != NULL);
468 name[MAX_STRING_SIZE] = '\0';
469 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
470 mdc_obd->obd_type->typ_name,
472 mdc_symlink = proc_symlink(mdc_obd->obd_name,
474 if (mdc_symlink == NULL) {
475 CERROR("could not register LMV target "
476 "/proc/fs/lustre/%s/%s/target_obds/%s.",
477 obd->obd_type->typ_name, obd->obd_name,
479 lprocfs_remove(&lmv_proc_dir);
487 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
489 struct lmv_obd *lmv = &obd->u.lmv;
490 struct lmv_tgt_desc *tgt;
494 CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
498 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
499 lmv_init_unlock(lmv);
500 CERROR("can't add %s, LMV module compiled for %d MDCs. "
501 "That many MDCs already configured.\n",
502 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
505 if (lmv->desc.ld_tgt_count == 0) {
506 struct obd_device *mdc_obd;
508 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
511 lmv_init_unlock(lmv);
512 CERROR("Target %s not attached\n", tgt_uuid->uuid);
516 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, 0, NULL, tgt_uuid);
518 lmv_init_unlock(lmv);
519 CERROR("lmv failed to setup llogging subsystems\n");
522 spin_lock(&lmv->lmv_lock);
523 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
524 tgt->ltd_uuid = *tgt_uuid;
525 spin_unlock(&lmv->lmv_lock);
527 if (lmv->connected) {
528 rc = lmv_connect_mdc(obd, tgt);
530 spin_lock(&lmv->lmv_lock);
531 lmv->desc.ld_tgt_count--;
532 memset(tgt, 0, sizeof(*tgt));
533 spin_unlock(&lmv->lmv_lock);
535 int easize = sizeof(struct lmv_stripe_md) +
536 lmv->desc.ld_tgt_count *
537 sizeof(struct lu_fid);
538 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
542 lmv_init_unlock(lmv);
546 /* performs a check if passed obd is connected. If no - connect it. */
547 int lmv_check_connect(struct obd_device *obd)
549 struct lmv_obd *lmv = &obd->u.lmv;
550 struct lmv_tgt_desc *tgt;
558 if (lmv->connected) {
559 lmv_init_unlock(lmv);
563 if (lmv->desc.ld_tgt_count == 0) {
564 CERROR("%s: no targets configured.\n", obd->obd_name);
568 CDEBUG(D_CONFIG, "time to connect %s to %s\n",
569 lmv->cluuid.uuid, obd->obd_name);
571 LASSERT(lmv->tgts != NULL);
573 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
574 rc = lmv_connect_mdc(obd, tgt);
579 lmv_set_timeouts(obd);
580 class_export_put(lmv->exp);
582 easize = lmv_get_easize(lmv);
583 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
584 lmv_init_unlock(lmv);
593 --lmv->desc.ld_active_tgt_count;
594 rc2 = obd_disconnect(tgt->ltd_exp);
596 CERROR("error: LMV target %s disconnect on "
597 "MDC idx %d: error %d\n",
598 tgt->ltd_uuid.uuid, i, rc2);
602 class_disconnect(lmv->exp);
603 lmv_init_unlock(lmv);
607 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
610 struct proc_dir_entry *lmv_proc_dir;
612 struct lmv_obd *lmv = &obd->u.lmv;
613 struct obd_device *mdc_obd;
617 LASSERT(tgt != NULL);
618 LASSERT(obd != NULL);
620 mdc_obd = class_exp2obd(tgt->ltd_exp);
623 mdc_obd->obd_no_recov = obd->obd_no_recov;
626 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
628 struct proc_dir_entry *mdc_symlink;
630 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
632 lprocfs_remove(&mdc_symlink);
634 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
635 obd->obd_type->typ_name, obd->obd_name,
640 rc = obd_fid_fini(tgt->ltd_exp);
642 CERROR("Can't finanize fids factory\n");
644 CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
645 tgt->ltd_exp->exp_obd->obd_name,
646 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
648 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
649 rc = obd_disconnect(tgt->ltd_exp);
651 if (tgt->ltd_active) {
652 CERROR("Target %s disconnect error %d\n",
653 tgt->ltd_uuid.uuid, rc);
657 lmv_activate_target(lmv, tgt, 0);
662 static int lmv_disconnect(struct obd_export *exp)
664 struct obd_device *obd = class_exp2obd(exp);
666 struct proc_dir_entry *lmv_proc_dir;
668 struct lmv_obd *lmv = &obd->u.lmv;
675 /* Only disconnect the underlying layers on the final disconnect. */
677 if (lmv->refcount != 0)
680 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
681 if (lmv->tgts[i].ltd_exp == NULL)
683 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
687 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
689 lprocfs_remove(&lmv_proc_dir);
691 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
692 obd->obd_type->typ_name, obd->obd_name);
698 * This is the case when no real connection is established by
699 * lmv_check_connect().
702 class_export_put(exp);
703 rc = class_disconnect(exp);
704 if (lmv->refcount == 0)
709 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
710 int len, void *karg, void *uarg)
712 struct obd_device *obddev = class_exp2obd(exp);
713 struct lmv_obd *lmv = &obddev->u.lmv;
714 int i, rc = 0, set = 0;
717 if (lmv->desc.ld_tgt_count == 0)
721 case IOC_OBD_STATFS: {
722 struct obd_ioctl_data *data = karg;
723 struct obd_device *mdc_obd;
724 struct obd_statfs stat_buf = {0};
727 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
728 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
730 if ((index >= lmv->desc.ld_tgt_count))
733 if (!lmv->tgts[index].ltd_active)
736 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
740 /* got statfs data */
741 rc = obd_statfs(mdc_obd, &stat_buf,
742 cfs_time_current_64() - HZ, 0);
745 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
748 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
753 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
756 if (lmv->tgts[i].ltd_exp == NULL)
759 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
762 if (lmv->tgts[i].ltd_active) {
763 CERROR("error: iocontrol MDC %s on MDT"
764 "idx %d cmd %x: err = %d\n",
765 lmv->tgts[i].ltd_uuid.uuid,
785 static int lmv_all_chars_policy(int count, const char *name,
796 static int lmv_nid_policy(struct lmv_obd *lmv)
798 struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
801 * XXX Hack: to get nid we assume that underlying obd device is mdc.
803 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
804 return id % lmv->desc.ld_tgt_count;
807 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
812 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
814 op_data->op_namelen);
816 return lmv_nid_policy(lmv);
822 CERROR("unsupport type %d \n", type);
826 /* This is _inode_ placement policy function (not name). */
827 static int lmv_placement_policy(struct obd_device *obd,
828 struct md_op_data *op_data,
831 struct lmv_obd *lmv = &obd->u.lmv;
836 LASSERT(mds != NULL);
839 * Allocate new fid on target according to operation type and parent
842 obj = lmv_obj_grab(obd, &op_data->op_fid1);
843 if (obj != NULL || op_data->op_name == NULL ||
844 op_data->op_opc != LUSTRE_OPC_MKDIR) {
846 * Allocate fid for non-dir or for null name or for case parent
853 * If we have this flag turned on, and we see that
854 * parent dir is split, this means, that caller did not
855 * notice split yet. This is race and we would like to
856 * let caller know that.
858 if (op_data->op_bias & MDS_CHECK_SPLIT)
863 * Allocate new fid on same mds where parent fid is located and
864 * where operation will be sent. In case of split dir, ->op_fid1
865 * and ->op_mds here will contain fid and mds of slave directory
866 * object (assigned by caller).
868 *mds = op_data->op_mds;
872 /* XXX: This should be removed later wehn we sure it is not
874 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
880 * Parent directory is not split and we want to create a
881 * directory in it. Let's calculate where to place it according
884 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
892 CERROR("Can't choose MDS, err = %d\n", rc);
894 LASSERT(*mds < lmv->desc.ld_tgt_count);
900 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
903 struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
907 /* New seq alloc and FLD setup should be atomic. */
908 down(&tgt->ltd_fid_sem);
910 /* Asking underlaying tgt layer to allocate new fid. */
911 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
913 LASSERT(fid_is_sane(fid));
915 /* Client switches to new sequence, setup FLD. */
916 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
919 CERROR("Can't create fld entry, rc %d\n", rc);
920 /* Delete just allocated fid sequence */
921 obd_fid_delete(tgt->ltd_exp, NULL);
924 up(&tgt->ltd_fid_sem);
928 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
929 struct md_op_data *op_data)
931 struct obd_device *obd = class_exp2obd(exp);
932 struct lmv_obd *lmv = &obd->u.lmv;
937 LASSERT(op_data != NULL);
938 LASSERT(fid != NULL);
940 rc = lmv_placement_policy(obd, op_data, &mds);
942 CERROR("Can't get target for allocating fid, "
947 rc = __lmv_fid_alloc(lmv, fid, mds);
949 CERROR("Can't alloc new fid, rc %d\n", rc);
956 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
961 if (lmv_obj_delete(exp, fid)) {
962 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
968 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
970 struct lmv_obd *lmv = &obd->u.lmv;
971 struct lprocfs_static_vars lvars;
972 struct lmv_desc *desc;
976 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
977 CERROR("LMV setup requires a descriptor\n");
981 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
982 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
983 CERROR("descriptor size wrong: %d > %d\n",
984 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
988 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
990 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
991 if (lmv->tgts == NULL)
994 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
995 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
996 lmv->tgts[i].ltd_idx = i;
999 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1001 OBD_ALLOC(lmv->datas, lmv->datas_size);
1002 if (lmv->datas == NULL)
1003 GOTO(out_free_tgts, rc = -ENOMEM);
1005 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1006 lmv->desc.ld_tgt_count = 0;
1007 lmv->desc.ld_active_tgt_count = 0;
1008 lmv->max_cookiesize = 0;
1009 lmv->max_def_easize = 0;
1010 lmv->max_easize = 0;
1012 spin_lock_init(&lmv->lmv_lock);
1013 sema_init(&lmv->init_sem, 1);
1015 rc = lmv_obj_setup(obd);
1017 CERROR("Can't setup LMV object manager, "
1019 GOTO(out_free_datas, rc);
1022 lprocfs_lmv_init_vars(&lvars);
1023 lprocfs_obd_setup(obd, lvars.obd_vars);
1026 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1027 0444, &lmv_proc_target_fops, obd);
1029 CWARN("Error adding the target_obd_status file\n");
1032 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1033 LUSTRE_CLI_FLD_HASH_DHT);
1035 CERROR("can't init FLD, err %d\n",
1037 GOTO(out_free_datas, rc);
1043 OBD_FREE(lmv->datas, lmv->datas_size);
1046 OBD_FREE(lmv->tgts, lmv->tgts_size);
1051 static int lmv_cleanup(struct obd_device *obd)
1053 struct lmv_obd *lmv = &obd->u.lmv;
1056 fld_client_fini(&lmv->lmv_fld);
1057 lprocfs_obd_cleanup(obd);
1058 lmv_obj_cleanup(obd);
1059 OBD_FREE(lmv->datas, lmv->datas_size);
1060 OBD_FREE(lmv->tgts, lmv->tgts_size);
1065 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1067 struct lustre_cfg *lcfg = buf;
1068 struct obd_uuid tgt_uuid;
1072 switch(lcfg->lcfg_command) {
1074 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1075 GOTO(out, rc = -EINVAL);
1077 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1078 rc = lmv_add_target(obd, &tgt_uuid);
1081 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1082 GOTO(out, rc = -EINVAL);
1089 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1090 __u64 max_age, __u32 flags)
1092 struct lmv_obd *lmv = &obd->u.lmv;
1093 struct obd_statfs *temp;
1097 rc = lmv_check_connect(obd);
1101 OBD_ALLOC(temp, sizeof(*temp));
1105 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1106 if (lmv->tgts[i].ltd_exp == NULL)
1109 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1112 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1113 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1115 GOTO(out_free_temp, rc);
1120 osfs->os_bavail += temp->os_bavail;
1121 osfs->os_blocks += temp->os_blocks;
1122 osfs->os_ffree += temp->os_ffree;
1123 osfs->os_files += temp->os_files;
1129 OBD_FREE(temp, sizeof(*temp));
1133 static int lmv_getstatus(struct obd_export *exp,
1135 struct obd_capa **pc)
1137 struct obd_device *obd = exp->exp_obd;
1138 struct lmv_obd *lmv = &obd->u.lmv;
1142 rc = lmv_check_connect(obd);
1146 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1151 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1152 struct obd_capa *oc, obd_valid valid, const char *name,
1153 const char *input, int input_size, int output_size,
1154 int flags, struct ptlrpc_request **request)
1156 struct obd_device *obd = exp->exp_obd;
1157 struct lmv_obd *lmv = &obd->u.lmv;
1158 struct obd_export *tgt_exp;
1162 rc = lmv_check_connect(obd);
1166 tgt_exp = lmv_find_export(lmv, fid);
1167 if (IS_ERR(tgt_exp))
1168 RETURN(PTR_ERR(tgt_exp));
1170 rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1171 output_size, flags, request);
1176 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1177 struct obd_capa *oc, obd_valid valid, const char *name,
1178 const char *input, int input_size, int output_size,
1179 int flags, __u32 suppgid,
1180 struct ptlrpc_request **request)
1182 struct obd_device *obd = exp->exp_obd;
1183 struct lmv_obd *lmv = &obd->u.lmv;
1184 struct obd_export *tgt_exp;
1188 rc = lmv_check_connect(obd);
1192 tgt_exp = lmv_find_export(lmv, fid);
1193 if (IS_ERR(tgt_exp))
1194 RETURN(PTR_ERR(tgt_exp));
1196 rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1197 input, input_size, output_size, flags, suppgid,
1203 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1204 struct obd_capa *oc, obd_valid valid, int ea_size,
1205 struct ptlrpc_request **request)
1207 struct obd_device *obd = exp->exp_obd;
1208 struct lmv_obd *lmv = &obd->u.lmv;
1209 struct obd_export *tgt_exp;
1210 struct lmv_obj *obj;
1214 rc = lmv_check_connect(obd);
1218 tgt_exp = lmv_find_export(lmv, fid);
1219 if (IS_ERR(tgt_exp))
1220 RETURN(PTR_ERR(tgt_exp));
1222 rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1226 obj = lmv_obj_grab(obd, fid);
1228 CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1229 obj ? "(split)" : "");
1232 * If object is split, then we loop over all the slaves and gather size
1233 * attribute. In ideal world we would have to gather also mds field from
1234 * all slaves, as object is spread over the cluster and this is
1235 * definitely interesting information and it is not good to loss it,
1239 struct mdt_body *body;
1241 if (*request == NULL) {
1246 body = req_capsule_server_get(&(*request)->rq_pill,
1248 LASSERT(body != NULL);
1252 for (i = 0; i < obj->lo_objcount; i++) {
1253 if (lmv->tgts[i].ltd_exp == NULL) {
1254 CWARN("%s: NULL export for %d\n",
1259 /* skip master obj. */
1260 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1263 lmv_update_body(body, &obj->lo_inodes[i]);
1266 lmv_obj_unlock(obj);
1273 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1274 ldlm_iterator_t it, void *data)
1276 struct obd_device *obd = exp->exp_obd;
1277 struct lmv_obd *lmv = &obd->u.lmv;
1281 rc = lmv_check_connect(obd);
1285 CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1288 * With CMD every object can have two locks in different namespaces:
1289 * lookup lock in space of mds storing direntry and update/open lock in
1290 * space of mds storing inode.
1292 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1293 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1298 static int lmv_close(struct obd_export *exp,
1299 struct md_op_data *op_data,
1300 struct md_open_data *mod,
1301 struct ptlrpc_request **request)
1303 struct obd_device *obd = exp->exp_obd;
1304 struct lmv_obd *lmv = &obd->u.lmv;
1305 struct obd_export *tgt_exp;
1309 rc = lmv_check_connect(obd);
1313 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1314 if (IS_ERR(tgt_exp))
1315 RETURN(PTR_ERR(tgt_exp));
1317 CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1318 rc = md_close(tgt_exp, op_data, mod, request);
1323 * Called in the case MDS returns -ERESTART on create on open, what means that
1324 * directory is split and its LMV presentation object has to be updated.
1326 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1328 struct obd_device *obd = exp->exp_obd;
1329 struct lmv_obd *lmv = &obd->u.lmv;
1330 struct ptlrpc_request *req = NULL;
1331 struct obd_export *tgt_exp;
1332 struct lmv_obj *obj;
1333 struct lustre_md md;
1339 mealen = lmv_get_easize(lmv);
1341 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1343 tgt_exp = lmv_find_export(lmv, fid);
1344 if (IS_ERR(tgt_exp))
1345 RETURN(PTR_ERR(tgt_exp));
1347 /* time to update mea of parent fid */
1348 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1350 CERROR("md_getattr() failed, error %d\n", rc);
1354 rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1356 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1361 GOTO(cleanup, rc = -ENODATA);
1363 obj = lmv_obj_create(exp, fid, md.mea);
1369 /* XXX LOV STACKING */
1370 obd_free_memmd(exp, (void *)&md.mea);
1375 ptlrpc_req_finished(req);
1379 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1380 const void *data, int datalen, int mode, __u32 uid,
1381 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1382 struct ptlrpc_request **request)
1384 struct obd_device *obd = exp->exp_obd;
1385 struct lmv_obd *lmv = &obd->u.lmv;
1386 struct obd_export *tgt_exp;
1387 struct lmv_obj *obj;
1391 rc = lmv_check_connect(obd);
1395 if (!lmv->desc.ld_active_tgt_count)
1400 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1404 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1405 op_data->op_name, op_data->op_namelen);
1406 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1407 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1408 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1409 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1412 struct lmv_tgt_desc *tgt;
1414 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1415 op_data->op_bias |= MDS_CHECK_SPLIT;
1416 op_data->op_mds = tgt->ltd_idx;
1417 tgt_exp = tgt->ltd_exp;
1420 if (IS_ERR(tgt_exp))
1421 RETURN(PTR_ERR(tgt_exp));
1423 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1424 if (rc == -ERESTART)
1429 CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1430 op_data->op_name, PFID(&op_data->op_fid1));
1432 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1433 rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1434 cap_effective, rdev, request);
1436 if (*request == NULL)
1438 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1439 } else if (rc == -ERESTART) {
1440 LASSERT(*request != NULL);
1441 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1442 "Got -ERESTART during create!\n");
1443 ptlrpc_req_finished(*request);
1447 * Directory got split. Time to update local object and repeat
1448 * the request with proper MDS.
1450 rc = lmv_handle_split(exp, &op_data->op_fid1);
1452 rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1453 op_data, &op_data->op_fid2);
1462 static int lmv_done_writing(struct obd_export *exp,
1463 struct md_op_data *op_data,
1464 struct md_open_data *mod)
1466 struct obd_device *obd = exp->exp_obd;
1467 struct lmv_obd *lmv = &obd->u.lmv;
1468 struct obd_export *tgt_exp;
1472 rc = lmv_check_connect(obd);
1476 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1477 if (IS_ERR(tgt_exp))
1478 RETURN(PTR_ERR(tgt_exp));
1480 rc = md_done_writing(tgt_exp, op_data, mod);
1485 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1486 struct lookup_intent *it, struct md_op_data *op_data,
1487 struct lustre_handle *lockh, void *lmm, int lmmsize)
1489 struct obd_device *obd = exp->exp_obd;
1490 struct lmv_obd *lmv = &obd->u.lmv;
1491 struct lmv_stripe_md *mea = op_data->op_mea1;
1492 struct md_op_data *op_data2;
1493 struct obd_export *tgt_exp;
1497 OBD_ALLOC_PTR(op_data2);
1498 if (op_data2 == NULL)
1501 LASSERT(mea != NULL);
1502 for (i = 0; i < mea->mea_count; i++) {
1503 memset(op_data2, 0, sizeof(*op_data2));
1504 op_data2->op_fid1 = mea->mea_ids[i];
1505 op_data2->op_bias = 0;
1507 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1508 if (IS_ERR(tgt_exp))
1509 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1511 if (tgt_exp == NULL)
1514 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1515 lockh + i, lmm, lmmsize, NULL, 0);
1517 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1518 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1523 if (it->d.lustre.it_data) {
1524 struct ptlrpc_request *req;
1525 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1526 ptlrpc_req_finished(req);
1529 if (it->d.lustre.it_status)
1530 GOTO(cleanup, rc = it->d.lustre.it_status);
1535 OBD_FREE_PTR(op_data2);
1538 /* drop all taken locks */
1540 if (lockh[i].cookie)
1541 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1542 lockh[i].cookie = 0;
1549 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1550 struct lookup_intent *it, struct md_op_data *op_data,
1551 struct lustre_handle *lockh, void *lmm, int lmmsize,
1552 int extra_lock_flags)
1554 struct ptlrpc_request *req = it->d.lustre.it_data;
1555 struct obd_device *obd = exp->exp_obd;
1556 struct lmv_obd *lmv = &obd->u.lmv;
1557 struct lustre_handle plock;
1558 struct obd_export *tgt_exp;
1559 struct md_op_data *rdata;
1560 struct lu_fid fid_copy;
1561 struct mdt_body *body;
1565 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1566 LASSERT(body != NULL);
1568 if (!(body->valid & OBD_MD_MDS))
1571 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1572 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1574 /* We got LOOKUP lock, but we really need attrs */
1575 pmode = it->d.lustre.it_lock_mode;
1576 LASSERT(pmode != 0);
1577 memcpy(&plock, lockh, sizeof(plock));
1578 it->d.lustre.it_lock_mode = 0;
1579 it->d.lustre.it_data = NULL;
1580 fid_copy = body->fid1;
1582 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1583 ptlrpc_req_finished(req);
1585 tgt_exp = lmv_find_export(lmv, &fid_copy);
1586 if (IS_ERR(tgt_exp))
1587 GOTO(out, rc = PTR_ERR(tgt_exp));
1589 OBD_ALLOC_PTR(rdata);
1591 GOTO(out, rc = -ENOMEM);
1593 rdata->op_fid1 = fid_copy;
1594 rdata->op_bias = MDS_CROSS_REF;
1596 rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1597 lmm, lmmsize, NULL, extra_lock_flags);
1598 OBD_FREE_PTR(rdata);
1601 ldlm_lock_decref(&plock, pmode);
1606 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1607 struct lookup_intent *it, struct md_op_data *op_data,
1608 struct lustre_handle *lockh, void *lmm, int lmmsize,
1609 struct ptlrpc_request **req, int extra_lock_flags)
1611 struct obd_device *obd = exp->exp_obd;
1612 struct lmv_obd *lmv = &obd->u.lmv;
1613 struct obd_export *tgt_exp = NULL;
1614 struct lmv_obj *obj;
1618 rc = lmv_check_connect(obd);
1622 if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1623 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1624 lockh, lmm, lmmsize);
1628 if (op_data->op_namelen) {
1629 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1633 /* directory is split. look for right mds for this
1635 mea_idx = raw_name2idx(obj->lo_hashtype,
1637 (char *)op_data->op_name,
1638 op_data->op_namelen);
1639 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1640 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1645 if (tgt_exp == NULL)
1646 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1647 if (IS_ERR(tgt_exp))
1648 RETURN(PTR_ERR(tgt_exp));
1650 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1651 PFID(&op_data->op_fid1));
1653 rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1654 lmm, lmmsize, req, extra_lock_flags);
1656 if (rc == 0 && it && it->it_op == IT_OPEN)
1657 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1658 lmm, lmmsize, extra_lock_flags);
1663 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1664 struct obd_capa *oc, const char *filename, int namelen,
1665 obd_valid valid, int ea_size, __u32 suppgid,
1666 struct ptlrpc_request **request)
1668 struct obd_device *obd = exp->exp_obd;
1669 struct lmv_obd *lmv = &obd->u.lmv;
1670 struct lu_fid rid = *fid;
1671 struct obd_export *tgt_exp;
1672 struct mdt_body *body;
1673 struct lmv_obj *obj;
1677 rc = lmv_check_connect(obd);
1684 obj = lmv_obj_grab(obd, &rid);
1688 /* Directory is split. Look for right mds for this name */
1689 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1690 filename, namelen - 1);
1691 rid = obj->lo_inodes[mea_idx].li_fid;
1692 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1694 valid &= ~OBD_MD_FLCKSPLIT;
1696 tgt_exp = lmv_find_export(lmv, &rid);
1697 valid |= OBD_MD_FLCKSPLIT;
1699 if (IS_ERR(tgt_exp))
1700 RETURN(PTR_ERR(tgt_exp));
1702 CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1703 namelen, filename, PFID(fid), PFID(&rid));
1705 rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1706 ea_size, suppgid, request);
1708 body = req_capsule_server_get(&(*request)->rq_pill,
1710 LASSERT(body != NULL);
1712 if (body->valid & OBD_MD_MDS) {
1713 struct ptlrpc_request *req = NULL;
1716 CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1719 tgt_exp = lmv_find_export(lmv, &rid);
1720 if (IS_ERR(tgt_exp)) {
1721 ptlrpc_req_finished(*request);
1722 RETURN(PTR_ERR(tgt_exp));
1725 rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1726 valid | OBD_MD_FLCROSSREF,
1727 ea_size, suppgid, &req);
1728 ptlrpc_req_finished(*request);
1731 } else if (rc == -ERESTART) {
1732 LASSERT(*request != NULL);
1733 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1734 "Got -ERESTART during getattr!\n");
1735 ptlrpc_req_finished(*request);
1739 * Directory got split. Time to update local object and repeat
1740 * the request with proper MDS.
1742 rc = lmv_handle_split(exp, &rid);
1749 #define md_op_data_fid(op_data, fl) \
1750 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1751 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1752 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1753 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1756 /* @tgt_exp is the export the metadata request is sent.
1757 * @fid_exp is the export the cancel should be sent for the current fid.
1758 * if @fid_exp is NULL, the export is found for the current fid.
1759 * @op_data keeps the current fid, which is pointed through @flag.
1760 * @mode, @bits -- lock match parameters. */
1761 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1762 struct obd_export *fid_exp,
1763 struct md_op_data *op_data,
1764 ldlm_mode_t mode, int bits, int flag)
1766 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1767 ldlm_policy_data_t policy = {{0}};
1771 if (!fid_is_sane(fid))
1774 if (fid_exp == NULL)
1775 fid_exp = lmv_find_export(lmv, fid);
1777 if (tgt_exp == fid_exp) {
1778 /* The export is the same as on the target server, cancel
1779 * will be sent along with the main metadata operation. */
1780 op_data->op_flags |= flag;
1784 policy.l_inodebits.bits = bits;
1785 rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1789 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1790 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1791 * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1792 * md_cancel_unused for child export(s). */
1793 static int lmv_early_cancel_stripes(struct obd_export *exp,
1794 struct obd_export *tgt_exp,
1795 struct md_op_data *op_data,
1796 ldlm_mode_t mode, int bits, int flag)
1798 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1799 struct obd_device *obd = exp->exp_obd;
1800 struct lmv_obd *lmv = &obd->u.lmv;
1801 struct obd_export *st_exp;
1802 struct lmv_obj *obj;
1806 if (!fid_is_sane(fid))
1809 obj = lmv_obj_grab(obd, fid);
1811 ldlm_policy_data_t policy = {{0}};
1812 struct lu_fid *st_fid;
1815 policy.l_inodebits.bits = bits;
1816 for (i = 0; i < obj->lo_objcount; i++) {
1817 st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1818 st_fid = &obj->lo_inodes[i].li_fid;
1819 if (tgt_exp != st_exp) {
1820 rc = md_cancel_unused(st_exp, st_fid, &policy,
1821 mode, LDLM_FL_ASYNC,
1826 /* Some export matches to @tgt_exp, do cancel
1827 * for its fid in mdc */
1829 op_data->op_flags |= flag;
1834 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1842 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1845 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1846 struct ptlrpc_request **request)
1848 struct obd_device *obd = exp->exp_obd;
1849 struct lmv_obd *lmv = &obd->u.lmv;
1850 struct obd_export *tgt_exp;
1851 struct lmv_obj *obj;
1856 rc = lmv_check_connect(obd);
1863 if (op_data->op_namelen != 0) {
1866 /* Usual link request */
1867 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1869 mea_idx = raw_name2idx(obj->lo_hashtype,
1872 op_data->op_namelen);
1873 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1874 mds = obj->lo_inodes[mea_idx].li_mds;
1877 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1882 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1883 PFID(&op_data->op_fid2), op_data->op_namelen,
1884 op_data->op_name, PFID(&op_data->op_fid1));
1886 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1890 /* request from MDS to acquire i_links for inode by fid1 */
1891 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1892 PFID(&op_data->op_fid1));
1895 CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1896 mds, PFID(&op_data->op_fid1));
1898 op_data->op_fsuid = current->fsuid;
1899 op_data->op_fsgid = current->fsgid;
1900 op_data->op_cap = cfs_curproc_cap_pack();
1902 tgt_exp = lmv->tgts[mds].ltd_exp;
1903 if (op_data->op_namelen) {
1904 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1905 /* Cancel UPDATE lock on child (fid1). */
1906 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1907 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1910 rc = md_link(tgt_exp, op_data, request);
1911 if (rc == -ERESTART) {
1912 LASSERT(*request != NULL);
1913 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1914 "Got -ERESTART during link!\n");
1915 ptlrpc_req_finished(*request);
1919 * Directory got split. Time to update local object and repeat
1920 * the request with proper MDS.
1922 rc = lmv_handle_split(exp, &op_data->op_fid2);
1930 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1931 const char *old, int oldlen, const char *new, int newlen,
1932 struct ptlrpc_request **request)
1934 struct obd_export *tgt_exp = NULL, *src_exp;
1935 struct obd_device *obd = exp->exp_obd;
1936 struct lmv_obd *lmv = &obd->u.lmv;
1937 int rc, mea_idx, loop = 0;
1938 struct lmv_obj *obj;
1942 CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1943 oldlen, old, PFID(&op_data->op_fid1),
1944 newlen, new, PFID(&op_data->op_fid2));
1946 rc = lmv_check_connect(obd);
1952 * MDS with old dir entry is asking another MDS to create name
1956 "create %*s(%d/%d) in "DFID" pointing "
1957 "to "DFID"\n", newlen, new, oldlen, newlen,
1958 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1960 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1965 * Target directory can be split, sowe should forward request to
1968 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1970 mea_idx = raw_name2idx(obj->lo_hashtype,
1972 (char *)new, newlen);
1973 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1974 CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1975 PFID(&op_data->op_fid2));
1984 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1987 * directory is already split, so we have to forward request to
1990 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1991 (char *)old, oldlen);
1992 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1993 mds1 = obj->lo_inodes[mea_idx].li_mds;
1994 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1997 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2002 obj = lmv_obj_grab(obd, &op_data->op_fid2);
2005 * Directory is already split, so we have to forward request to
2008 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2009 (char *)new, newlen);
2011 mds2 = obj->lo_inodes[mea_idx].li_mds;
2012 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
2013 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2016 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2022 op_data->op_fsuid = current->fsuid;
2023 op_data->op_fsgid = current->fsgid;
2024 op_data->op_cap = cfs_curproc_cap_pack();
2026 src_exp = lmv_get_export(lmv, mds1);
2027 tgt_exp = lmv_get_export(lmv, mds2);
2029 /* LOOKUP lock on src child (fid3) should also be cancelled for
2030 * src_exp in mdc_rename. */
2031 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2033 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2035 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2036 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2038 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2040 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2041 LCK_EX, MDS_INODELOCK_LOOKUP,
2042 MF_MDC_CANCEL_FID4);
2044 /* XXX: the case when child is a striped dir is not supported.
2045 * Only the master stripe has all locks cancelled early. */
2046 /* Cancel all the locks on tgt child (fid4). */
2048 rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2049 LCK_EX, MDS_INODELOCK_FULL,
2050 MF_MDC_CANCEL_FID4);
2054 rc = md_rename(src_exp, op_data, old, oldlen,
2055 new, newlen, request);
2056 if (rc == -ERESTART) {
2057 LASSERT(*request != NULL);
2058 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2059 "Got -ERESTART during rename!\n");
2060 ptlrpc_req_finished(*request);
2064 * Directory got split. Time to update local object and repeat
2065 * the request with proper MDS.
2067 rc = lmv_handle_split(exp, &op_data->op_fid1);
2074 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2075 void *ea, int ealen, void *ea2, int ea2len,
2076 struct ptlrpc_request **request,
2077 struct md_open_data **mod)
2079 struct obd_device *obd = exp->exp_obd;
2080 struct lmv_obd *lmv = &obd->u.lmv;
2081 struct ptlrpc_request *req;
2082 struct obd_export *tgt_exp;
2083 struct lmv_obj *obj;
2087 rc = lmv_check_connect(obd);
2091 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2093 CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2094 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2095 obj ? ", split" : "");
2097 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2099 for (i = 0; i < obj->lo_objcount; i++) {
2100 op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2102 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2103 if (IS_ERR(tgt_exp)) {
2104 rc = PTR_ERR(tgt_exp);
2108 rc = md_setattr(tgt_exp, op_data, ea, ealen,
2109 ea2, ea2len, &req, mod);
2111 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2113 * this is master object and this request should
2114 * be returned back to llite.
2118 ptlrpc_req_finished(req);
2126 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2127 if (IS_ERR(tgt_exp))
2128 RETURN(PTR_ERR(tgt_exp));
2130 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2131 ea2len, request, mod);
2136 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2137 struct obd_capa *oc, struct ptlrpc_request **request)
2139 struct obd_device *obd = exp->exp_obd;
2140 struct lmv_obd *lmv = &obd->u.lmv;
2141 struct obd_export *tgt_exp;
2145 rc = lmv_check_connect(obd);
2149 tgt_exp = lmv_find_export(lmv, fid);
2150 if (IS_ERR(tgt_exp))
2151 RETURN(PTR_ERR(tgt_exp));
2153 rc = md_sync(tgt_exp, fid, oc, request);
2157 /* main purpose of LMV blocking ast is to remove split directory LMV
2158 * presentation object (struct lmv_obj) attached to the lock being revoked. */
2159 int lmv_blocking_ast(struct ldlm_lock *lock,
2160 struct ldlm_lock_desc *desc,
2161 void *data, int flag)
2163 struct lustre_handle lockh;
2164 struct lmv_obj *obj;
2169 case LDLM_CB_BLOCKING:
2170 ldlm_lock2handle(lock, &lockh);
2171 rc = ldlm_cli_cancel(&lockh);
2173 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2177 case LDLM_CB_CANCELING:
2178 /* time to drop cached attrs for dirobj */
2179 obj = lock->l_ast_data;
2181 CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2182 ", master "DFID"\n",
2183 lock->l_resource->lr_name.name[3] == 1 ?
2184 "LOOKUP" : "UPDATE",
2185 lock->l_resource->lr_name.name[0],
2186 lock->l_resource->lr_name.name[1],
2187 PFID(&obj->lo_fid));
2197 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2201 val = le64_to_cpu(*hash);
2203 val += MAX_HASH_SIZE;
2204 if (val != DIR_END_OFF)
2205 *hash = cpu_to_le64(val - hash_adj);
2208 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2211 struct obd_import *imp;
2214 * XXX Hack: to get nid we assume that underlying obd device is mdc.
2216 imp = class_exp2cliimp(exp);
2217 id = imp->imp_connection->c_self + fid_flatten(fid);
2219 CDEBUG(D_INFO, "node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2220 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2222 return id ^ (id >> 32);
2225 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2226 struct obd_capa *oc, __u64 offset64, struct page *page,
2227 struct ptlrpc_request **request)
2229 struct obd_device *obd = exp->exp_obd;
2230 struct lmv_obd *lmv = &obd->u.lmv;
2231 struct obd_export *tgt_exp;
2232 struct lu_fid rid = *fid;
2233 struct lmv_obj *obj;
2247 rc = lmv_check_connect(obd);
2251 CDEBUG(D_INFO, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2253 obj = lmv_obj_grab(obd, fid);
2257 * This case handle directory lookup in clustered metadata case (i.e.
2258 * split directory is located on multiple md servers.)
2259 * each server keeps directory entries for certain range of hashes.
2260 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2261 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2262 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2264 * readdir can simply start reading entries from 0 - N server in
2265 * order but that will not scale well as all client will request dir in
2266 * to server in same order.
2267 * Following algorithm does optimization:
2268 * Instead of doing readdir in 1, 2, ...., N order, client with a
2269 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2270 * (every client has rank R)
2271 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2272 * since client ask dir from MDS{R} client has pages with offsets
2273 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2274 * on hash values that we get.
2277 struct lmv_inode *loi;
2281 nr = obj->lo_objcount;
2283 seg_size = MAX_HASH_SIZE;
2284 do_div(seg_size, nr);
2285 loi = obj->lo_inodes;
2286 rank = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2289 do_div(tgt_tmp, seg_size);
2290 tgt0 = do_div(tgt_tmp, nr);
2291 tgt = (tgt0 + rank) % nr;
2297 * Last segment has unusual length due to division
2300 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2304 hash_adj += rank * seg_size;
2306 CDEBUG(D_INFO, "hash_adj: %x "LPX64" "LPX64"/%x -> "LPX64"/%x\n",
2307 rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2309 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2310 rid = obj->lo_inodes[tgt].li_fid;
2311 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2313 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2314 PFID(&rid), (unsigned long)offset, tgt);
2316 tgt_exp = lmv_find_export(lmv, &rid);
2318 if (IS_ERR(tgt_exp))
2319 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2321 rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2325 struct lu_dirpage *dp;
2326 struct lu_dirent *ent;
2328 dp = cfs_kmap(page);
2330 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2331 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2332 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2334 for (ent = lu_dirent_start(dp); ent != NULL;
2335 ent = lu_dirent_next(ent))
2336 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2338 if (tgt0 != nr - 1) {
2341 end = le64_to_cpu(dp->ldp_hash_end);
2342 if (end == DIR_END_OFF) {
2343 dp->ldp_hash_end = cpu_to_le32(seg_size *
2346 ""DFID" reset end "LPX64" tgt %d\n",
2348 le64_to_cpu(dp->ldp_hash_end), tgt);
2354 * Here we could remove "." and ".." from all pages which at not from
2355 * master. But MDS has only "." and ".." for master dir.
2360 lmv_obj_unlock(obj);
2366 static int lmv_unlink_slaves(struct obd_export *exp,
2367 struct md_op_data *op_data,
2368 struct ptlrpc_request **req)
2370 struct obd_device *obd = exp->exp_obd;
2371 struct lmv_obd *lmv = &obd->u.lmv;
2372 struct lmv_stripe_md *mea = op_data->op_mea1;
2373 struct md_op_data *op_data2;
2374 struct obd_export *tgt_exp;
2378 OBD_ALLOC_PTR(op_data2);
2379 if (op_data2 == NULL)
2382 op_data2->op_mode = S_IFDIR;
2383 op_data2->op_fsuid = current->fsuid;
2384 op_data2->op_fsgid = current->fsgid;
2385 op_data2->op_bias = 0;
2387 LASSERT(mea != NULL);
2388 for (i = 0; i < mea->mea_count; i++) {
2389 memset(op_data2, 0, sizeof(*op_data2));
2390 op_data2->op_fid1 = mea->mea_ids[i];
2391 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2392 if (IS_ERR(tgt_exp))
2393 GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2395 if (tgt_exp == NULL)
2398 rc = md_unlink(tgt_exp, op_data2, req);
2400 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2401 PFID(&mea->mea_ids[i]), rc);
2404 ptlrpc_req_finished(*req);
2408 GOTO(out_free_op_data2, rc);
2413 OBD_FREE_PTR(op_data2);
2417 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2418 struct ptlrpc_request **request)
2420 struct obd_device *obd = exp->exp_obd;
2421 struct lmv_obd *lmv = &obd->u.lmv;
2422 struct obd_export *tgt_exp = NULL;
2423 struct lmv_obj *obj;
2427 rc = lmv_check_connect(obd);
2431 if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2432 /* mds asks to remove slave objects */
2433 rc = lmv_unlink_slaves(exp, op_data, request);
2440 if (op_data->op_namelen != 0) {
2443 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2445 mea_idx = raw_name2idx(obj->lo_hashtype,
2448 op_data->op_namelen);
2449 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2450 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2451 tgt_exp = lmv_get_export(lmv,
2452 obj->lo_inodes[mea_idx].li_mds);
2454 CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2455 op_data->op_namelen, op_data->op_name,
2456 PFID(&op_data->op_fid1), mea_idx);
2459 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2460 PFID(&op_data->op_fid1));
2462 if (tgt_exp == NULL) {
2463 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2464 if (IS_ERR(tgt_exp))
2465 RETURN(PTR_ERR(tgt_exp));
2466 op_data->op_bias |= MDS_CHECK_SPLIT;
2469 op_data->op_fsuid = current->fsuid;
2470 op_data->op_fsgid = current->fsgid;
2471 op_data->op_cap = cfs_curproc_cap_pack();
2473 /* If child's fid is given, cancel unused locks for it if it is from
2474 * another export than parent. */
2475 if (op_data->op_namelen) {
2476 /* LOOKUP lock for child (fid3) should also be cancelled on
2477 * parent tgt_exp in mdc_unlink(). */
2478 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2480 /* XXX: the case when child is a striped dir is not supported.
2481 * Only the master stripe has all locks cancelled early. */
2482 /* Cancel FULL locks on child (fid3). */
2483 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2484 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2487 rc = md_unlink(tgt_exp, op_data, request);
2488 if (rc == -ERESTART) {
2489 LASSERT(*request != NULL);
2490 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2491 "Got -ERESTART during unlink!\n");
2492 ptlrpc_req_finished(*request);
2496 * Directory got split. Time to update local object and repeat
2497 * the request with proper MDS.
2499 rc = lmv_handle_split(exp, &op_data->op_fid1);
2506 static int lmv_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2507 struct obd_device *tgt, int count,
2508 struct llog_catid *logid, struct obd_uuid *uuid)
2511 struct llog_ctxt *ctxt;
2515 LASSERT(group == OBD_LLOG_GROUP);
2516 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2519 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2520 llog_initiator_connect(ctxt, tgt);
2521 llog_ctxt_put(ctxt);
2529 static int lmv_llog_finish(struct obd_device *obd, int count)
2531 struct llog_ctxt *ctxt;
2535 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2537 rc = llog_cleanup(ctxt);
2542 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2547 case OBD_CLEANUP_EARLY:
2548 /* XXX: here should be calling obd_precleanup() down to
2551 case OBD_CLEANUP_EXPORTS:
2552 rc = obd_llog_finish(obd, 0);
2554 CERROR("failed to cleanup llogging subsystems\n");
2562 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2563 void *key, __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2565 struct obd_device *obd;
2566 struct lmv_obd *lmv;
2570 obd = class_exp2obd(exp);
2572 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2573 exp->exp_handle.h_cookie);
2578 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2579 struct lmv_tgt_desc *tgts;
2582 rc = lmv_check_connect(obd);
2586 LASSERT(*vallen == sizeof(__u32));
2587 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2590 /* all tgts should be connected when this get called. */
2591 if (!tgts || !tgts->ltd_exp) {
2592 CERROR("target not setup?\n");
2596 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2601 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2602 rc = lmv_check_connect(obd);
2606 /* forwarding this request to first MDS, it should know LOV
2608 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2610 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2611 exp->exp_connect_flags =
2612 ((struct obd_connect_data *)val)->ocd_connect_flags;
2617 CDEBUG(D_IOCTL, "invalid key\n");
2621 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2622 void *key, obd_count vallen, void *val,
2623 struct ptlrpc_request_set *set)
2625 struct lmv_tgt_desc *tgt;
2626 struct obd_device *obd;
2627 struct lmv_obd *lmv;
2631 obd = class_exp2obd(exp);
2633 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2634 exp->exp_handle.h_cookie);
2639 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2640 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2643 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2644 tgt = &lmv->tgts[i];
2649 err = obd_set_info_async(tgt->ltd_exp,
2650 keylen, key, vallen, val, set);
2661 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2662 struct lov_stripe_md *lsm)
2664 struct obd_device *obd = class_exp2obd(exp);
2665 struct lmv_obd *lmv = &obd->u.lmv;
2666 struct lmv_stripe_md *meap, *lsmp;
2670 mea_size = lmv_get_easize(lmv);
2674 if (*lmmp && !lsm) {
2675 OBD_FREE(*lmmp, mea_size);
2680 if (*lmmp == NULL) {
2681 OBD_ALLOC(*lmmp, mea_size);
2689 lsmp = (struct lmv_stripe_md *)lsm;
2690 meap = (struct lmv_stripe_md *)*lmmp;
2692 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2693 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2696 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2697 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2698 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2700 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2701 meap->mea_ids[i] = meap->mea_ids[i];
2702 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2708 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2709 struct lov_mds_md *lmm, int lmm_size)
2711 struct obd_device *obd = class_exp2obd(exp);
2712 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2713 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2714 struct lmv_obd *lmv = &obd->u.lmv;
2719 mea_size = lmv_get_easize(lmv);
2723 if (*lsmp != NULL && lmm == NULL) {
2724 OBD_FREE(*tmea, mea_size);
2729 LASSERT(mea_size == lmm_size);
2731 OBD_ALLOC(*tmea, mea_size);
2738 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2739 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2740 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2742 magic = le32_to_cpu(mea->mea_magic);
2744 /* old mea is not handled here */
2748 (*tmea)->mea_magic = magic;
2749 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2750 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2752 for (i = 0; i < (*tmea)->mea_count; i++) {
2753 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2754 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2759 static int lmv_cancel_unused(struct obd_export *exp,
2760 const struct lu_fid *fid,
2761 ldlm_policy_data_t *policy,
2762 ldlm_mode_t mode, int flags, void *opaque)
2764 struct obd_device *obd = exp->exp_obd;
2765 struct lmv_obd *lmv = &obd->u.lmv;
2769 LASSERT(fid != NULL);
2771 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2772 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2775 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2776 policy, mode, flags, opaque);
2783 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2785 struct obd_device *obd = exp->exp_obd;
2786 struct lmv_obd *lmv = &obd->u.lmv;
2789 RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2792 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2793 const struct lu_fid *fid, ldlm_type_t type,
2794 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2795 struct lustre_handle *lockh)
2797 struct obd_device *obd = exp->exp_obd;
2798 struct lmv_obd *lmv = &obd->u.lmv;
2803 CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2805 /* with CMD every object can have two locks in different namespaces:
2806 * lookup lock in space of mds storing direntry and update/open lock in
2807 * space of mds storing inode. Thus we check all targets, not only that
2808 * one fid was created in. */
2809 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2810 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2811 type, policy, mode, lockh);
2819 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2820 struct obd_export *dt_exp, struct obd_export *md_exp,
2821 struct lustre_md *md)
2823 struct obd_device *obd = exp->exp_obd;
2824 struct lmv_obd *lmv = &obd->u.lmv;
2828 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2832 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2834 struct obd_device *obd = exp->exp_obd;
2835 struct lmv_obd *lmv = &obd->u.lmv;
2839 /* XXX LOV STACKING */
2841 obd_free_memmd(exp, (void *)&md->mea);
2842 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2845 int lmv_set_open_replay_data(struct obd_export *exp,
2846 struct obd_client_handle *och,
2847 struct ptlrpc_request *open_req)
2849 struct obd_device *obd = exp->exp_obd;
2850 struct lmv_obd *lmv = &obd->u.lmv;
2851 struct obd_export *tgt_exp;
2855 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2856 if (IS_ERR(tgt_exp))
2857 RETURN(PTR_ERR(tgt_exp));
2859 RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2862 int lmv_clear_open_replay_data(struct obd_export *exp,
2863 struct obd_client_handle *och)
2865 struct obd_device *obd = exp->exp_obd;
2866 struct lmv_obd *lmv = &obd->u.lmv;
2867 struct obd_export *tgt_exp;
2870 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2871 if (IS_ERR(tgt_exp))
2872 RETURN(PTR_ERR(tgt_exp));
2874 RETURN(md_clear_open_replay_data(tgt_exp, och));
2877 static int lmv_get_remote_perm(struct obd_export *exp,
2878 const struct lu_fid *fid,
2879 struct obd_capa *oc, __u32 suppgid,
2880 struct ptlrpc_request **request)
2882 struct obd_device *obd = exp->exp_obd;
2883 struct lmv_obd *lmv = &obd->u.lmv;
2884 struct obd_export *tgt_exp;
2889 rc = lmv_check_connect(obd);
2893 tgt_exp = lmv_find_export(lmv, fid);
2894 if (IS_ERR(tgt_exp))
2895 RETURN(PTR_ERR(tgt_exp));
2897 rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2902 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2905 struct obd_device *obd = exp->exp_obd;
2906 struct lmv_obd *lmv = &obd->u.lmv;
2907 struct obd_export *tgt_exp;
2911 rc = lmv_check_connect(obd);
2915 tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2916 if (IS_ERR(tgt_exp))
2917 RETURN(PTR_ERR(tgt_exp));
2919 rc = md_renew_capa(tgt_exp, oc, cb);
2923 int lmv_intent_getattr_async(struct obd_export *exp,
2924 struct md_enqueue_info *minfo,
2925 struct ldlm_enqueue_info *einfo)
2927 struct obd_device *obd = exp->exp_obd;
2928 struct lmv_obd *lmv = &obd->u.lmv;
2929 struct obd_export *tgt_exp;
2933 rc = lmv_check_connect(obd);
2937 if (fid_is_zero(&minfo->mi_data.op_fid2))
2938 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid1);
2940 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid2);
2941 if (IS_ERR(tgt_exp))
2942 RETURN(PTR_ERR(tgt_exp));
2944 rc = md_intent_getattr_async(tgt_exp, minfo, einfo);
2948 int lmv_revalidate_lock(struct obd_export *exp,
2949 struct lookup_intent *it,
2952 struct obd_device *obd = exp->exp_obd;
2953 struct lmv_obd *lmv = &obd->u.lmv;
2954 struct obd_export *tgt_exp;
2958 rc = lmv_check_connect(obd);
2962 tgt_exp = lmv_find_export(lmv, fid);
2963 if (IS_ERR(tgt_exp))
2964 RETURN(PTR_ERR(tgt_exp));
2966 rc = md_revalidate_lock(tgt_exp, it, fid);
2971 struct obd_ops lmv_obd_ops = {
2972 .o_owner = THIS_MODULE,
2973 .o_setup = lmv_setup,
2974 .o_cleanup = lmv_cleanup,
2975 .o_precleanup = lmv_precleanup,
2976 .o_process_config = lmv_process_config,
2977 .o_connect = lmv_connect,
2978 .o_disconnect = lmv_disconnect,
2979 .o_statfs = lmv_statfs,
2980 .o_llog_init = lmv_llog_init,
2981 .o_llog_finish = lmv_llog_finish,
2982 .o_get_info = lmv_get_info,
2983 .o_set_info_async = lmv_set_info_async,
2984 .o_packmd = lmv_packmd,
2985 .o_unpackmd = lmv_unpackmd,
2986 .o_notify = lmv_notify,
2987 .o_get_uuid = lmv_get_uuid,
2988 .o_iocontrol = lmv_iocontrol,
2989 .o_fid_delete = lmv_fid_delete
2992 struct md_ops lmv_md_ops = {
2993 .m_getstatus = lmv_getstatus,
2994 .m_change_cbdata = lmv_change_cbdata,
2995 .m_close = lmv_close,
2996 .m_create = lmv_create,
2997 .m_done_writing = lmv_done_writing,
2998 .m_enqueue = lmv_enqueue,
2999 .m_getattr = lmv_getattr,
3000 .m_getxattr = lmv_getxattr,
3001 .m_getattr_name = lmv_getattr_name,
3002 .m_intent_lock = lmv_intent_lock,
3004 .m_rename = lmv_rename,
3005 .m_setattr = lmv_setattr,
3006 .m_setxattr = lmv_setxattr,
3008 .m_readpage = lmv_readpage,
3009 .m_unlink = lmv_unlink,
3010 .m_init_ea_size = lmv_init_ea_size,
3011 .m_cancel_unused = lmv_cancel_unused,
3012 .m_set_lock_data = lmv_set_lock_data,
3013 .m_lock_match = lmv_lock_match,
3014 .m_get_lustre_md = lmv_get_lustre_md,
3015 .m_free_lustre_md = lmv_free_lustre_md,
3016 .m_set_open_replay_data = lmv_set_open_replay_data,
3017 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3018 .m_renew_capa = lmv_renew_capa,
3019 .m_get_remote_perm = lmv_get_remote_perm,
3020 .m_intent_getattr_async = lmv_intent_getattr_async,
3021 .m_revalidate_lock = lmv_revalidate_lock
3024 int __init lmv_init(void)
3026 struct lprocfs_static_vars lvars;
3029 obj_cache = cfs_mem_cache_create("lmv_objects",
3030 sizeof(struct lmv_obj),
3033 CERROR("error allocating lmv objects cache\n");
3037 lprocfs_lmv_init_vars(&lvars);
3038 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3039 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3041 cfs_mem_cache_destroy(obj_cache);
3047 static void lmv_exit(void)
3051 class_unregister_type(LUSTRE_LMV_NAME);
3053 rc = cfs_mem_cache_destroy(obj_cache);
3055 "can't free lmv objects cache, %d object(s)"
3056 "still in use\n", atomic_read(&obj_cache_count));
3059 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3060 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3061 MODULE_LICENSE("GPL");
3063 module_init(lmv_init);
3064 module_exit(lmv_exit);