1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LMV
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
33 #include <asm/div64.h>
34 #include <linux/seq_file.h>
35 #include <linux/namei.h>
37 #include <liblustre.h>
40 #include <linux/ext2_fs.h>
42 #include <lustre/lustre_idl.h>
43 #include <lustre_log.h>
44 #include <obd_support.h>
45 #include <lustre_lib.h>
46 #include <lustre_net.h>
47 #include <obd_class.h>
48 #include <lprocfs_status.h>
49 #include <lustre_lite.h>
50 #include <lustre_fid.h>
51 #include "lmv_internal.h"
53 /* not defined for liblustre building */
54 #if !defined(ATOMIC_INIT)
55 #define ATOMIC_INIT(val) { (val) }
59 cfs_mem_cache_t *obj_cache;
60 atomic_t obj_cache_count = ATOMIC_INIT(0);
62 static void lmv_activate_target(struct lmv_obd *lmv,
63 struct lmv_tgt_desc *tgt,
66 if (tgt->ltd_active == activate)
69 tgt->ltd_active = activate;
70 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
75 * -EINVAL : UUID can't be found in the LMV's target list
76 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
77 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
79 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
82 struct lmv_tgt_desc *tgt;
83 struct obd_device *obd;
87 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
88 lmv, uuid->uuid, activate);
90 spin_lock(&lmv->lmv_lock);
91 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
92 if (tgt->ltd_exp == NULL)
95 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
96 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
98 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
102 if (i == lmv->desc.ld_tgt_count)
103 GOTO(out_lmv_lock, rc = -EINVAL);
105 obd = class_exp2obd(tgt->ltd_exp);
107 GOTO(out_lmv_lock, rc = -ENOTCONN);
109 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
110 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
111 obd->obd_type->typ_name, i);
112 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
114 if (tgt->ltd_active == activate) {
115 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
116 activate ? "" : "in");
117 GOTO(out_lmv_lock, rc);
120 CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
121 obd, activate ? "" : "in");
123 lmv_activate_target(lmv, tgt, activate);
128 spin_unlock(&lmv->lmv_lock);
132 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
133 struct obd_connect_data *data)
135 struct lmv_tgt_desc *tgt;
139 LASSERT(data != NULL);
141 spin_lock(&lmv->lmv_lock);
142 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
143 if (tgt->ltd_exp == NULL)
146 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
147 lmv->datas[tgt->ltd_idx] = *data;
151 spin_unlock(&lmv->lmv_lock);
155 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
156 enum obd_notify_event ev, void *data)
158 struct lmv_obd *lmv = &obd->u.lmv;
159 struct obd_uuid *uuid;
163 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
164 CERROR("unexpected notification of %s %s!\n",
165 watched->obd_type->typ_name,
170 uuid = &watched->u.cli.cl_target_uuid;
171 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
173 * Set MDC as active before notifying the observer, so the
174 * observer can use the MDC normally.
176 rc = lmv_set_mdc_active(lmv, uuid,
177 ev == OBD_NOTIFY_ACTIVE);
179 CERROR("%sactivation of %s failed: %d\n",
180 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
184 } else if (ev == OBD_NOTIFY_OCD) {
185 struct obd_connect_data *conn_data =
186 &watched->u.cli.cl_import->imp_connect_data;
188 /* Set connect data to desired target, update
189 * exp_connect_flags. */
190 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
192 CERROR("can't set connect data to target %s, rc %d\n",
198 * XXX: Make sure that ocd_connect_flags from all targets are
199 * the same. Otherwise one of MDTs runs wrong version or
200 * something like this. --umka
202 obd->obd_self_export->exp_connect_flags =
203 conn_data->ocd_connect_flags;
206 else if (ev == OBD_NOTIFY_DISCON) {
207 /* For disconnect event, flush fld cache for failout MDS case. */
208 fld_client_flush(&lmv->lmv_fld);
211 /* Pass the notification up the chain. */
212 if (obd->obd_observer)
213 rc = obd_notify(obd->obd_observer, watched, ev, data);
218 /* this is fake connect function. Its purpose is to initialize lmv and say
219 * caller that everything is okay. Real connection will be performed later. */
220 static int lmv_connect(const struct lu_env *env,
221 struct lustre_handle *conn, struct obd_device *obd,
222 struct obd_uuid *cluuid, struct obd_connect_data *data,
226 struct proc_dir_entry *lmv_proc_dir;
228 struct lmv_obd *lmv = &obd->u.lmv;
229 struct obd_export *exp;
233 rc = class_connect(conn, obd, cluuid);
235 CERROR("class_connection() returned %d\n", rc);
239 exp = class_conn2export(conn);
241 /* we don't want to actually do the underlying connections more than
242 * once, so keep track. */
244 if (lmv->refcount > 1) {
245 class_export_put(exp);
251 lmv->cluuid = *cluuid;
254 lmv->conn_data = *data;
257 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
259 if (IS_ERR(lmv_proc_dir)) {
260 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
261 obd->obd_type->typ_name, obd->obd_name);
266 /* all real clients should perform actual connection right away, because
267 * it is possible, that LMV will not have opportunity to connect targets
268 * and MDC stuff will be called directly, for instance while reading
269 * ../mdc/../kbytesfree procfs file, etc. */
270 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
271 rc = lmv_check_connect(obd);
276 lprocfs_remove(&lmv_proc_dir);
283 static void lmv_set_timeouts(struct obd_device *obd)
285 struct lmv_tgt_desc *tgts;
290 if (lmv->server_timeout == 0)
293 if (lmv->connected == 0)
296 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
297 if (tgts->ltd_exp == NULL)
300 obd_set_info_async(tgts->ltd_exp, strlen("inter_mds"),
301 "inter_mds", 0, NULL, NULL);
305 static int lmv_init_ea_size(struct obd_export *exp, int easize,
306 int def_easize, int cookiesize)
308 struct obd_device *obd = exp->exp_obd;
309 struct lmv_obd *lmv = &obd->u.lmv;
310 int i, rc = 0, change = 0;
313 if (lmv->max_easize < easize) {
314 lmv->max_easize = easize;
317 if (lmv->max_def_easize < def_easize) {
318 lmv->max_def_easize = def_easize;
321 if (lmv->max_cookiesize < cookiesize) {
322 lmv->max_cookiesize = cookiesize;
328 if (lmv->connected == 0)
331 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
332 if (lmv->tgts[i].ltd_exp == NULL) {
333 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
337 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
340 CERROR("obd_init_ea_size() failed on MDT target %d, "
341 "error %d.\n", i, rc);
348 #define MAX_STRING_SIZE 128
350 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
352 struct lmv_obd *lmv = &obd->u.lmv;
353 struct obd_uuid *cluuid = &lmv->cluuid;
354 struct obd_connect_data *mdc_data = NULL;
355 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
356 struct lustre_handle conn = {0, };
357 struct obd_device *mdc_obd;
358 struct obd_export *mdc_exp;
359 struct lu_fld_target target;
362 struct proc_dir_entry *lmv_proc_dir;
366 /* for MDS: don't connect to yourself */
367 if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
368 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
369 /* XXX - the old code didn't increment active tgt count.
374 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
377 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
381 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
382 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
383 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
386 if (!mdc_obd->obd_set_up) {
387 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
391 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
392 &lmv->conn_data, NULL);
394 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
398 mdc_exp = class_conn2export(&conn);
400 /* Init fid sequence client for this mdc. */
401 rc = obd_fid_init(mdc_exp);
405 /* Add new FLD target. */
406 target.ft_srv = NULL;
407 target.ft_exp = mdc_exp;
408 target.ft_idx = tgt->ltd_idx;
410 fld_client_add_target(&lmv->lmv_fld, &target);
412 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
414 rc = obd_register_observer(mdc_obd, obd);
416 obd_disconnect(mdc_exp);
417 CERROR("target %s register_observer error %d\n",
418 tgt->ltd_uuid.uuid, rc);
422 if (obd->obd_observer) {
423 /* tell the mds_lmv about the new target */
424 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
425 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
427 obd_disconnect(mdc_exp);
433 tgt->ltd_exp = mdc_exp;
434 lmv->desc.ld_active_tgt_count++;
436 /* copy connect data, it may be used later */
437 lmv->datas[tgt->ltd_idx] = *mdc_data;
439 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
440 lmv->max_def_easize, lmv->max_cookiesize);
442 CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
443 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
444 atomic_read(&obd->obd_refcount));
447 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
449 struct proc_dir_entry *mdc_symlink;
450 char name[MAX_STRING_SIZE + 1];
452 LASSERT(mdc_obd->obd_type != NULL);
453 LASSERT(mdc_obd->obd_type->typ_name != NULL);
454 name[MAX_STRING_SIZE] = '\0';
455 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
456 mdc_obd->obd_type->typ_name,
458 mdc_symlink = proc_symlink(mdc_obd->obd_name,
460 if (mdc_symlink == NULL) {
461 CERROR("could not register LMV target "
462 "/proc/fs/lustre/%s/%s/target_obds/%s.",
463 obd->obd_type->typ_name, obd->obd_name,
465 lprocfs_remove(&lmv_proc_dir);
473 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
475 struct lmv_obd *lmv = &obd->u.lmv;
476 struct lmv_tgt_desc *tgt;
480 CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
484 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
485 lmv_init_unlock(lmv);
486 CERROR("can't add %s, LMV module compiled for %d MDCs. "
487 "That many MDCs already configured.\n",
488 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
491 if (lmv->desc.ld_tgt_count == 0) {
492 struct obd_device *mdc_obd;
494 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
497 lmv_init_unlock(lmv);
498 CERROR("Target %s not attached\n", tgt_uuid->uuid);
502 rc = obd_llog_init(obd, OBD_LLOG_GROUP, mdc_obd, 0, NULL, tgt_uuid);
504 lmv_init_unlock(lmv);
505 CERROR("lmv failed to setup llogging subsystems\n");
508 spin_lock(&lmv->lmv_lock);
509 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
510 tgt->ltd_uuid = *tgt_uuid;
511 spin_unlock(&lmv->lmv_lock);
513 if (lmv->connected) {
514 rc = lmv_connect_mdc(obd, tgt);
516 spin_lock(&lmv->lmv_lock);
517 lmv->desc.ld_tgt_count--;
518 memset(tgt, 0, sizeof(*tgt));
519 spin_unlock(&lmv->lmv_lock);
521 int easize = sizeof(struct lmv_stripe_md) +
522 lmv->desc.ld_tgt_count *
523 sizeof(struct lu_fid);
524 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
528 lmv_init_unlock(lmv);
532 /* performs a check if passed obd is connected. If no - connect it. */
533 int lmv_check_connect(struct obd_device *obd)
535 struct lmv_obd *lmv = &obd->u.lmv;
536 struct lmv_tgt_desc *tgt;
544 if (lmv->connected) {
545 lmv_init_unlock(lmv);
549 if (lmv->desc.ld_tgt_count == 0) {
550 CERROR("%s: no targets configured.\n", obd->obd_name);
554 CDEBUG(D_CONFIG, "time to connect %s to %s\n",
555 lmv->cluuid.uuid, obd->obd_name);
557 LASSERT(lmv->tgts != NULL);
559 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
560 rc = lmv_connect_mdc(obd, tgt);
565 lmv_set_timeouts(obd);
566 class_export_put(lmv->exp);
568 easize = lmv_get_easize(lmv);
569 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
570 lmv_init_unlock(lmv);
579 --lmv->desc.ld_active_tgt_count;
580 rc2 = obd_disconnect(tgt->ltd_exp);
582 CERROR("error: LMV target %s disconnect on "
583 "MDC idx %d: error %d\n",
584 tgt->ltd_uuid.uuid, i, rc2);
588 class_disconnect(lmv->exp);
589 lmv_init_unlock(lmv);
593 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
596 struct proc_dir_entry *lmv_proc_dir;
598 struct lmv_obd *lmv = &obd->u.lmv;
599 struct obd_device *mdc_obd;
603 LASSERT(tgt != NULL);
604 LASSERT(obd != NULL);
606 mdc_obd = class_exp2obd(tgt->ltd_exp);
609 mdc_obd->obd_no_recov = obd->obd_no_recov;
612 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
614 struct proc_dir_entry *mdc_symlink;
616 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
618 lprocfs_remove(&mdc_symlink);
620 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
621 obd->obd_type->typ_name, obd->obd_name,
626 rc = obd_fid_fini(tgt->ltd_exp);
628 CERROR("Can't finanize fids factory\n");
630 CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
631 tgt->ltd_exp->exp_obd->obd_name,
632 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
634 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
635 rc = obd_disconnect(tgt->ltd_exp);
637 if (tgt->ltd_active) {
638 CERROR("Target %s disconnect error %d\n",
639 tgt->ltd_uuid.uuid, rc);
643 lmv_activate_target(lmv, tgt, 0);
648 static int lmv_disconnect(struct obd_export *exp)
650 struct obd_device *obd = class_exp2obd(exp);
652 struct proc_dir_entry *lmv_proc_dir;
654 struct lmv_obd *lmv = &obd->u.lmv;
661 /* Only disconnect the underlying layers on the final disconnect. */
663 if (lmv->refcount != 0)
666 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
667 if (lmv->tgts[i].ltd_exp == NULL)
669 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
673 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
675 lprocfs_remove(&lmv_proc_dir);
677 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
678 obd->obd_type->typ_name, obd->obd_name);
684 * This is the case when no real connection is established by
685 * lmv_check_connect().
688 class_export_put(exp);
689 rc = class_disconnect(exp);
690 if (lmv->refcount == 0)
695 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
696 int len, void *karg, void *uarg)
698 struct obd_device *obddev = class_exp2obd(exp);
699 struct lmv_obd *lmv = &obddev->u.lmv;
700 int i, rc = 0, set = 0;
703 if (lmv->desc.ld_tgt_count == 0)
707 case IOC_OBD_STATFS: {
708 struct obd_ioctl_data *data = karg;
709 struct obd_device *mdc_obd;
710 struct obd_statfs stat_buf = {0};
713 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
714 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
716 if ((index >= lmv->desc.ld_tgt_count))
719 if (!lmv->tgts[index].ltd_active)
722 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
726 /* got statfs data */
727 rc = obd_statfs(mdc_obd, &stat_buf,
728 cfs_time_current_64() - HZ, 0);
731 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
734 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
739 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
742 if (lmv->tgts[i].ltd_exp == NULL)
745 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
748 if (lmv->tgts[i].ltd_active) {
749 CERROR("error: iocontrol MDC %s on MDT"
750 "idx %d cmd %x: err = %d\n",
751 lmv->tgts[i].ltd_uuid.uuid,
771 static int lmv_all_chars_policy(int count, const char *name,
782 static int lmv_nid_policy(struct lmv_obd *lmv)
784 struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
787 * XXX Hack: to get nid we assume that underlying obd device is mdc.
789 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
790 return id % lmv->desc.ld_tgt_count;
793 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
798 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
800 op_data->op_namelen);
802 return lmv_nid_policy(lmv);
808 CERROR("unsupport type %d \n", type);
812 /* This is _inode_ placement policy function (not name). */
813 static int lmv_placement_policy(struct obd_device *obd,
814 struct md_op_data *op_data,
817 struct lmv_obd *lmv = &obd->u.lmv;
822 LASSERT(mds != NULL);
825 * Allocate new fid on target according to operation type and parent
828 obj = lmv_obj_grab(obd, &op_data->op_fid1);
829 if (obj != NULL || op_data->op_name == NULL ||
830 op_data->op_opc != LUSTRE_OPC_MKDIR) {
832 * Allocate fid for non-dir or for null name or for case parent
839 * If we have this flag turned on, and we see that
840 * parent dir is split, this means, that caller did not
841 * notice split yet. This is race and we would like to
842 * let caller know that.
844 if (op_data->op_bias & MDS_CHECK_SPLIT)
849 * Allocate new fid on same mds where parent fid is located and
850 * where operation will be sent. In case of split dir, ->op_fid1
851 * and ->op_mds here will contain fid and mds of slave directory
852 * object (assigned by caller).
854 *mds = op_data->op_mds;
858 /* XXX: This should be removed later wehn we sure it is not
860 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
866 * Parent directory is not split and we want to create a
867 * directory in it. Let's calculate where to place it according
870 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
878 CERROR("Can't choose MDS, err = %d\n", rc);
880 LASSERT(*mds < lmv->desc.ld_tgt_count);
886 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
889 struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
893 /* New seq alloc and FLD setup should be atomic. */
894 down(&tgt->ltd_fid_sem);
896 /* Asking underlaying tgt layer to allocate new fid. */
897 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
899 LASSERT(fid_is_sane(fid));
901 /* Client switches to new sequence, setup FLD. */
902 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
905 CERROR("Can't create fld entry, rc %d\n", rc);
906 /* Delete just allocated fid sequence */
907 obd_fid_delete(tgt->ltd_exp, NULL);
910 up(&tgt->ltd_fid_sem);
914 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
915 struct md_op_data *op_data)
917 struct obd_device *obd = class_exp2obd(exp);
918 struct lmv_obd *lmv = &obd->u.lmv;
923 LASSERT(op_data != NULL);
924 LASSERT(fid != NULL);
926 rc = lmv_placement_policy(obd, op_data, &mds);
928 CERROR("Can't get target for allocating fid, "
933 rc = __lmv_fid_alloc(lmv, fid, mds);
935 CERROR("Can't alloc new fid, rc %d\n", rc);
942 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
947 if (lmv_obj_delete(exp, fid)) {
948 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
954 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
956 struct lmv_obd *lmv = &obd->u.lmv;
957 struct lprocfs_static_vars lvars;
958 struct lmv_desc *desc;
962 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
963 CERROR("LMV setup requires a descriptor\n");
967 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
968 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
969 CERROR("descriptor size wrong: %d > %d\n",
970 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
974 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
976 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
977 if (lmv->tgts == NULL)
980 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
981 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
982 lmv->tgts[i].ltd_idx = i;
985 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
987 OBD_ALLOC(lmv->datas, lmv->datas_size);
988 if (lmv->datas == NULL)
989 GOTO(out_free_tgts, rc = -ENOMEM);
991 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
992 lmv->desc.ld_tgt_count = 0;
993 lmv->desc.ld_active_tgt_count = 0;
994 lmv->max_cookiesize = 0;
995 lmv->max_def_easize = 0;
998 spin_lock_init(&lmv->lmv_lock);
999 sema_init(&lmv->init_sem, 1);
1001 rc = lmv_obj_setup(obd);
1003 CERROR("Can't setup LMV object manager, "
1005 GOTO(out_free_datas, rc);
1008 lprocfs_lmv_init_vars(&lvars);
1009 lprocfs_obd_setup(obd, lvars.obd_vars);
1012 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1013 0444, &lmv_proc_target_fops, obd);
1015 CWARN("Error adding the target_obd_status file\n");
1018 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1019 LUSTRE_CLI_FLD_HASH_DHT);
1021 CERROR("can't init FLD, err %d\n",
1023 GOTO(out_free_datas, rc);
1029 OBD_FREE(lmv->datas, lmv->datas_size);
1032 OBD_FREE(lmv->tgts, lmv->tgts_size);
1037 static int lmv_cleanup(struct obd_device *obd)
1039 struct lmv_obd *lmv = &obd->u.lmv;
1042 fld_client_fini(&lmv->lmv_fld);
1043 lprocfs_obd_cleanup(obd);
1044 lmv_obj_cleanup(obd);
1045 OBD_FREE(lmv->datas, lmv->datas_size);
1046 OBD_FREE(lmv->tgts, lmv->tgts_size);
1051 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1053 struct lustre_cfg *lcfg = buf;
1054 struct obd_uuid tgt_uuid;
1058 switch(lcfg->lcfg_command) {
1060 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1061 GOTO(out, rc = -EINVAL);
1063 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1064 rc = lmv_add_target(obd, &tgt_uuid);
1067 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1068 GOTO(out, rc = -EINVAL);
1075 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1076 __u64 max_age, __u32 flags)
1078 struct lmv_obd *lmv = &obd->u.lmv;
1079 struct obd_statfs *temp;
1083 rc = lmv_check_connect(obd);
1087 OBD_ALLOC(temp, sizeof(*temp));
1091 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1092 if (lmv->tgts[i].ltd_exp == NULL)
1095 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1098 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1099 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1101 GOTO(out_free_temp, rc);
1106 osfs->os_bavail += temp->os_bavail;
1107 osfs->os_blocks += temp->os_blocks;
1108 osfs->os_ffree += temp->os_ffree;
1109 osfs->os_files += temp->os_files;
1115 OBD_FREE(temp, sizeof(*temp));
1119 static int lmv_getstatus(struct obd_export *exp,
1121 struct obd_capa **pc)
1123 struct obd_device *obd = exp->exp_obd;
1124 struct lmv_obd *lmv = &obd->u.lmv;
1128 rc = lmv_check_connect(obd);
1132 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1137 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1138 struct obd_capa *oc, obd_valid valid, const char *name,
1139 const char *input, int input_size, int output_size,
1140 int flags, struct ptlrpc_request **request)
1142 struct obd_device *obd = exp->exp_obd;
1143 struct lmv_obd *lmv = &obd->u.lmv;
1144 struct obd_export *tgt_exp;
1148 rc = lmv_check_connect(obd);
1152 tgt_exp = lmv_find_export(lmv, fid);
1153 if (IS_ERR(tgt_exp))
1154 RETURN(PTR_ERR(tgt_exp));
1156 rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1157 output_size, flags, request);
1162 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1163 struct obd_capa *oc, obd_valid valid, const char *name,
1164 const char *input, int input_size, int output_size,
1165 int flags, __u32 suppgid,
1166 struct ptlrpc_request **request)
1168 struct obd_device *obd = exp->exp_obd;
1169 struct lmv_obd *lmv = &obd->u.lmv;
1170 struct obd_export *tgt_exp;
1174 rc = lmv_check_connect(obd);
1178 tgt_exp = lmv_find_export(lmv, fid);
1179 if (IS_ERR(tgt_exp))
1180 RETURN(PTR_ERR(tgt_exp));
1182 rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1183 input, input_size, output_size, flags, suppgid,
1189 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1190 struct obd_capa *oc, obd_valid valid, int ea_size,
1191 struct ptlrpc_request **request)
1193 struct obd_device *obd = exp->exp_obd;
1194 struct lmv_obd *lmv = &obd->u.lmv;
1195 struct obd_export *tgt_exp;
1196 struct lmv_obj *obj;
1200 rc = lmv_check_connect(obd);
1204 tgt_exp = lmv_find_export(lmv, fid);
1205 if (IS_ERR(tgt_exp))
1206 RETURN(PTR_ERR(tgt_exp));
1208 rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1212 obj = lmv_obj_grab(obd, fid);
1214 CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1215 obj ? "(split)" : "");
1218 * If object is split, then we loop over all the slaves and gather size
1219 * attribute. In ideal world we would have to gather also mds field from
1220 * all slaves, as object is spread over the cluster and this is
1221 * definitely interesting information and it is not good to loss it,
1225 struct mdt_body *body;
1227 if (*request == NULL) {
1232 body = req_capsule_server_get(&(*request)->rq_pill,
1234 LASSERT(body != NULL);
1238 for (i = 0; i < obj->lo_objcount; i++) {
1239 if (lmv->tgts[i].ltd_exp == NULL) {
1240 CWARN("%s: NULL export for %d\n",
1245 /* skip master obj. */
1246 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1249 lmv_update_body(body, &obj->lo_inodes[i]);
1252 lmv_obj_unlock(obj);
1259 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1260 ldlm_iterator_t it, void *data)
1262 struct obd_device *obd = exp->exp_obd;
1263 struct lmv_obd *lmv = &obd->u.lmv;
1267 rc = lmv_check_connect(obd);
1271 CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1274 * With CMD every object can have two locks in different namespaces:
1275 * lookup lock in space of mds storing direntry and update/open lock in
1276 * space of mds storing inode.
1278 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1279 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1284 static int lmv_close(struct obd_export *exp,
1285 struct md_op_data *op_data,
1286 struct md_open_data *mod,
1287 struct ptlrpc_request **request)
1289 struct obd_device *obd = exp->exp_obd;
1290 struct lmv_obd *lmv = &obd->u.lmv;
1291 struct obd_export *tgt_exp;
1295 rc = lmv_check_connect(obd);
1299 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1300 if (IS_ERR(tgt_exp))
1301 RETURN(PTR_ERR(tgt_exp));
1303 CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1304 rc = md_close(tgt_exp, op_data, mod, request);
1309 * Called in the case MDS returns -ERESTART on create on open, what means that
1310 * directory is split and its LMV presentation object has to be updated.
1312 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1314 struct obd_device *obd = exp->exp_obd;
1315 struct lmv_obd *lmv = &obd->u.lmv;
1316 struct ptlrpc_request *req = NULL;
1317 struct obd_export *tgt_exp;
1318 struct lmv_obj *obj;
1319 struct lustre_md md;
1325 mealen = lmv_get_easize(lmv);
1327 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1329 tgt_exp = lmv_find_export(lmv, fid);
1330 if (IS_ERR(tgt_exp))
1331 RETURN(PTR_ERR(tgt_exp));
1333 /* time to update mea of parent fid */
1334 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1336 CERROR("md_getattr() failed, error %d\n", rc);
1340 rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1342 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1347 GOTO(cleanup, rc = -ENODATA);
1349 obj = lmv_obj_create(exp, fid, md.mea);
1355 obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
1360 ptlrpc_req_finished(req);
1364 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1365 const void *data, int datalen, int mode, __u32 uid,
1366 __u32 gid, __u32 cap_effective, __u64 rdev,
1367 struct ptlrpc_request **request)
1369 struct obd_device *obd = exp->exp_obd;
1370 struct lmv_obd *lmv = &obd->u.lmv;
1371 struct obd_export *tgt_exp;
1372 struct lmv_obj *obj;
1376 rc = lmv_check_connect(obd);
1380 if (!lmv->desc.ld_active_tgt_count)
1385 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1389 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1390 op_data->op_name, op_data->op_namelen);
1391 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1392 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1393 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1394 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1397 struct lmv_tgt_desc *tgt;
1399 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1400 op_data->op_bias |= MDS_CHECK_SPLIT;
1401 op_data->op_mds = tgt->ltd_idx;
1402 tgt_exp = tgt->ltd_exp;
1405 if (IS_ERR(tgt_exp))
1406 RETURN(PTR_ERR(tgt_exp));
1408 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1409 if (rc == -ERESTART)
1414 CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1415 op_data->op_name, PFID(&op_data->op_fid1));
1417 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1418 rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1419 cap_effective, rdev, request);
1421 if (*request == NULL)
1423 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1424 } else if (rc == -ERESTART) {
1425 LASSERT(*request != NULL);
1426 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1427 "Got -ERESTART during create!\n");
1428 ptlrpc_req_finished(*request);
1432 * Directory got split. Time to update local object and repeat
1433 * the request with proper MDS.
1435 rc = lmv_handle_split(exp, &op_data->op_fid1);
1437 rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1438 op_data, &op_data->op_fid2);
1447 static int lmv_done_writing(struct obd_export *exp,
1448 struct md_op_data *op_data,
1449 struct md_open_data *mod)
1451 struct obd_device *obd = exp->exp_obd;
1452 struct lmv_obd *lmv = &obd->u.lmv;
1453 struct obd_export *tgt_exp;
1457 rc = lmv_check_connect(obd);
1461 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1462 if (IS_ERR(tgt_exp))
1463 RETURN(PTR_ERR(tgt_exp));
1465 rc = md_done_writing(tgt_exp, op_data, mod);
1470 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1471 struct lookup_intent *it, struct md_op_data *op_data,
1472 struct lustre_handle *lockh, void *lmm, int lmmsize)
1474 struct obd_device *obd = exp->exp_obd;
1475 struct lmv_obd *lmv = &obd->u.lmv;
1476 struct lmv_stripe_md *mea = op_data->op_mea1;
1477 struct md_op_data *op_data2;
1478 struct obd_export *tgt_exp;
1482 OBD_ALLOC_PTR(op_data2);
1483 if (op_data2 == NULL)
1486 LASSERT(mea != NULL);
1487 for (i = 0; i < mea->mea_count; i++) {
1488 memset(op_data2, 0, sizeof(*op_data2));
1489 op_data2->op_fid1 = mea->mea_ids[i];
1490 op_data2->op_bias = 0;
1492 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1493 if (IS_ERR(tgt_exp))
1494 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1496 if (tgt_exp == NULL)
1499 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1500 lockh + i, lmm, lmmsize, 0);
1502 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1503 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1508 if (it->d.lustre.it_data) {
1509 struct ptlrpc_request *req;
1510 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1511 ptlrpc_req_finished(req);
1514 if (it->d.lustre.it_status)
1515 GOTO(cleanup, rc = it->d.lustre.it_status);
1520 OBD_FREE_PTR(op_data2);
1523 /* drop all taken locks */
1525 if (lockh[i].cookie)
1526 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1527 lockh[i].cookie = 0;
1534 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1535 struct lookup_intent *it, struct md_op_data *op_data,
1536 struct lustre_handle *lockh, void *lmm, int lmmsize,
1537 int extra_lock_flags)
1539 struct ptlrpc_request *req = it->d.lustre.it_data;
1540 struct obd_device *obd = exp->exp_obd;
1541 struct lmv_obd *lmv = &obd->u.lmv;
1542 struct lustre_handle plock;
1543 struct obd_export *tgt_exp;
1544 struct md_op_data *rdata;
1545 struct lu_fid fid_copy;
1546 struct mdt_body *body;
1550 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1551 LASSERT(body != NULL);
1553 if (!(body->valid & OBD_MD_MDS))
1556 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1557 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1559 /* We got LOOKUP lock, but we really need attrs */
1560 pmode = it->d.lustre.it_lock_mode;
1561 LASSERT(pmode != 0);
1562 memcpy(&plock, lockh, sizeof(plock));
1563 it->d.lustre.it_lock_mode = 0;
1564 it->d.lustre.it_data = NULL;
1565 fid_copy = body->fid1;
1567 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1568 ptlrpc_req_finished(req);
1570 tgt_exp = lmv_find_export(lmv, &fid_copy);
1571 if (IS_ERR(tgt_exp))
1572 GOTO(out, rc = PTR_ERR(tgt_exp));
1574 OBD_ALLOC_PTR(rdata);
1576 GOTO(out, rc = -ENOMEM);
1578 rdata->op_fid1 = fid_copy;
1579 rdata->op_bias = MDS_CROSS_REF;
1581 rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1582 lmm, lmmsize, extra_lock_flags);
1583 OBD_FREE_PTR(rdata);
1586 ldlm_lock_decref(&plock, pmode);
1591 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1592 struct lookup_intent *it, struct md_op_data *op_data,
1593 struct lustre_handle *lockh, void *lmm, int lmmsize,
1594 int extra_lock_flags)
1596 struct obd_device *obd = exp->exp_obd;
1597 struct lmv_obd *lmv = &obd->u.lmv;
1598 struct obd_export *tgt_exp = NULL;
1599 struct lmv_obj *obj;
1603 rc = lmv_check_connect(obd);
1607 if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1608 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1609 lockh, lmm, lmmsize);
1613 if (op_data->op_namelen) {
1614 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1618 /* directory is split. look for right mds for this
1620 mea_idx = raw_name2idx(obj->lo_hashtype,
1622 (char *)op_data->op_name,
1623 op_data->op_namelen);
1624 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1625 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1630 if (tgt_exp == NULL)
1631 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1632 if (IS_ERR(tgt_exp))
1633 RETURN(PTR_ERR(tgt_exp));
1635 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1636 PFID(&op_data->op_fid1));
1638 rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1639 lmm, lmmsize, extra_lock_flags);
1641 if (rc == 0 && it->it_op == IT_OPEN)
1642 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1643 lmm, lmmsize, extra_lock_flags);
1648 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1649 struct obd_capa *oc, const char *filename, int namelen,
1650 obd_valid valid, int ea_size, __u32 suppgid,
1651 struct ptlrpc_request **request)
1653 struct obd_device *obd = exp->exp_obd;
1654 struct lmv_obd *lmv = &obd->u.lmv;
1655 struct lu_fid rid = *fid;
1656 struct obd_export *tgt_exp;
1657 struct mdt_body *body;
1658 struct lmv_obj *obj;
1662 rc = lmv_check_connect(obd);
1669 obj = lmv_obj_grab(obd, &rid);
1673 /* Directory is split. Look for right mds for this name */
1674 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1675 filename, namelen - 1);
1676 rid = obj->lo_inodes[mea_idx].li_fid;
1677 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1679 valid &= ~OBD_MD_FLCKSPLIT;
1681 tgt_exp = lmv_find_export(lmv, &rid);
1682 valid |= OBD_MD_FLCKSPLIT;
1684 if (IS_ERR(tgt_exp))
1685 RETURN(PTR_ERR(tgt_exp));
1687 CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1688 namelen, filename, PFID(fid), PFID(&rid));
1690 rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1691 ea_size, suppgid, request);
1693 body = req_capsule_server_get(&(*request)->rq_pill,
1695 LASSERT(body != NULL);
1697 if (body->valid & OBD_MD_MDS) {
1698 struct ptlrpc_request *req = NULL;
1701 CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1704 tgt_exp = lmv_find_export(lmv, &rid);
1705 if (IS_ERR(tgt_exp)) {
1706 ptlrpc_req_finished(*request);
1707 RETURN(PTR_ERR(tgt_exp));
1710 rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1711 valid | OBD_MD_FLCROSSREF,
1712 ea_size, suppgid, &req);
1713 ptlrpc_req_finished(*request);
1716 } else if (rc == -ERESTART) {
1717 LASSERT(*request != NULL);
1718 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1719 "Got -ERESTART during getattr!\n");
1720 ptlrpc_req_finished(*request);
1724 * Directory got split. Time to update local object and repeat
1725 * the request with proper MDS.
1727 rc = lmv_handle_split(exp, &rid);
1734 #define md_op_data_fid(op_data, fl) \
1735 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1736 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1737 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1738 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1741 /* @tgt_exp is the export the metadata request is sent.
1742 * @fid_exp is the export the cancel should be sent for the current fid.
1743 * if @fid_exp is NULL, the export is found for the current fid.
1744 * @op_data keeps the current fid, which is pointed through @flag.
1745 * @mode, @bits -- lock match parameters. */
1746 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1747 struct obd_export *fid_exp,
1748 struct md_op_data *op_data,
1749 ldlm_mode_t mode, int bits, int flag)
1751 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1752 ldlm_policy_data_t policy = {{0}};
1756 if (!fid_is_sane(fid))
1759 if (fid_exp == NULL)
1760 fid_exp = lmv_find_export(lmv, fid);
1762 if (tgt_exp == fid_exp) {
1763 /* The export is the same as on the target server, cancel
1764 * will be sent along with the main metadata operation. */
1765 op_data->op_flags |= flag;
1769 policy.l_inodebits.bits = bits;
1770 rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1774 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1775 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1776 * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1777 * md_cancel_unused for child export(s). */
1778 static int lmv_early_cancel_stripes(struct obd_export *exp,
1779 struct obd_export *tgt_exp,
1780 struct md_op_data *op_data,
1781 ldlm_mode_t mode, int bits, int flag)
1783 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1784 struct obd_device *obd = exp->exp_obd;
1785 struct lmv_obd *lmv = &obd->u.lmv;
1786 struct obd_export *st_exp;
1787 struct lmv_obj *obj;
1791 if (!fid_is_sane(fid))
1794 obj = lmv_obj_grab(obd, fid);
1796 ldlm_policy_data_t policy = {{0}};
1797 struct lu_fid *st_fid;
1800 policy.l_inodebits.bits = bits;
1801 for (i = 0; i < obj->lo_objcount; i++) {
1802 st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1803 st_fid = &obj->lo_inodes[i].li_fid;
1804 if (tgt_exp != st_exp) {
1805 rc = md_cancel_unused(st_exp, st_fid, &policy,
1806 mode, LDLM_FL_ASYNC,
1811 /* Some export matches to @tgt_exp, do cancel
1812 * for its fid in mdc */
1814 op_data->op_flags |= flag;
1819 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1827 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1830 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1831 struct ptlrpc_request **request)
1833 struct obd_device *obd = exp->exp_obd;
1834 struct lmv_obd *lmv = &obd->u.lmv;
1835 struct obd_export *tgt_exp;
1836 struct lmv_obj *obj;
1841 rc = lmv_check_connect(obd);
1848 if (op_data->op_namelen != 0) {
1851 /* Usual link request */
1852 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1854 mea_idx = raw_name2idx(obj->lo_hashtype,
1857 op_data->op_namelen);
1858 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1859 mds = obj->lo_inodes[mea_idx].li_mds;
1862 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1867 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1868 PFID(&op_data->op_fid2), op_data->op_namelen,
1869 op_data->op_name, PFID(&op_data->op_fid1));
1871 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1875 /* request from MDS to acquire i_links for inode by fid1 */
1876 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1877 PFID(&op_data->op_fid1));
1880 CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1881 mds, PFID(&op_data->op_fid1));
1883 op_data->op_fsuid = current->fsuid;
1884 op_data->op_fsgid = current->fsgid;
1885 op_data->op_cap = current->cap_effective;
1887 tgt_exp = lmv->tgts[mds].ltd_exp;
1888 if (op_data->op_namelen) {
1889 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1890 /* Cancel UPDATE lock on child (fid1). */
1891 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1892 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1895 rc = md_link(tgt_exp, op_data, request);
1896 if (rc == -ERESTART) {
1897 LASSERT(*request != NULL);
1898 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1899 "Got -ERESTART during link!\n");
1900 ptlrpc_req_finished(*request);
1904 * Directory got split. Time to update local object and repeat
1905 * the request with proper MDS.
1907 rc = lmv_handle_split(exp, &op_data->op_fid2);
1915 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1916 const char *old, int oldlen, const char *new, int newlen,
1917 struct ptlrpc_request **request)
1919 struct obd_export *tgt_exp = NULL, *src_exp;
1920 struct obd_device *obd = exp->exp_obd;
1921 struct lmv_obd *lmv = &obd->u.lmv;
1922 int rc, mea_idx, loop = 0;
1923 struct lmv_obj *obj;
1927 CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1928 oldlen, old, PFID(&op_data->op_fid1),
1929 newlen, new, PFID(&op_data->op_fid2));
1931 rc = lmv_check_connect(obd);
1937 * MDS with old dir entry is asking another MDS to create name
1941 "create %*s(%d/%d) in "DFID" pointing "
1942 "to "DFID"\n", newlen, new, oldlen, newlen,
1943 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1945 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1950 * Target directory can be split, sowe should forward request to
1953 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1955 mea_idx = raw_name2idx(obj->lo_hashtype,
1957 (char *)new, newlen);
1958 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1959 CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1960 PFID(&op_data->op_fid2));
1969 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1972 * directory is already split, so we have to forward request to
1975 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1976 (char *)old, oldlen);
1977 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1978 mds1 = obj->lo_inodes[mea_idx].li_mds;
1979 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1982 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
1987 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1990 * Directory is already split, so we have to forward request to
1993 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1994 (char *)new, newlen);
1996 mds2 = obj->lo_inodes[mea_idx].li_mds;
1997 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1998 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2001 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2007 op_data->op_fsuid = current->fsuid;
2008 op_data->op_fsgid = current->fsgid;
2009 op_data->op_cap = current->cap_effective;
2011 src_exp = lmv_get_export(lmv, mds1);
2012 tgt_exp = lmv_get_export(lmv, mds2);
2014 /* LOOKUP lock on src child (fid3) should also be cancelled for
2015 * src_exp in mdc_rename. */
2016 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2018 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2020 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2021 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2023 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2025 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2026 LCK_EX, MDS_INODELOCK_LOOKUP,
2027 MF_MDC_CANCEL_FID4);
2029 /* XXX: the case when child is a striped dir is not supported.
2030 * Only the master stripe has all locks cancelled early. */
2031 /* Cancel all the locks on tgt child (fid4). */
2033 rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2034 LCK_EX, MDS_INODELOCK_FULL,
2035 MF_MDC_CANCEL_FID4);
2039 rc = md_rename(src_exp, op_data, old, oldlen,
2040 new, newlen, request);
2041 if (rc == -ERESTART) {
2042 LASSERT(*request != NULL);
2043 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2044 "Got -ERESTART during rename!\n");
2045 ptlrpc_req_finished(*request);
2049 * Directory got split. Time to update local object and repeat
2050 * the request with proper MDS.
2052 rc = lmv_handle_split(exp, &op_data->op_fid1);
2059 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2060 void *ea, int ealen, void *ea2, int ea2len,
2061 struct ptlrpc_request **request,
2062 struct md_open_data **mod)
2064 struct obd_device *obd = exp->exp_obd;
2065 struct lmv_obd *lmv = &obd->u.lmv;
2066 struct ptlrpc_request *req;
2067 struct obd_export *tgt_exp;
2068 struct lmv_obj *obj;
2072 rc = lmv_check_connect(obd);
2076 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2078 CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2079 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2080 obj ? ", split" : "");
2082 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2084 for (i = 0; i < obj->lo_objcount; i++) {
2085 op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2087 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2088 if (IS_ERR(tgt_exp)) {
2089 rc = PTR_ERR(tgt_exp);
2093 rc = md_setattr(tgt_exp, op_data, ea, ealen,
2094 ea2, ea2len, &req, mod);
2096 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2098 * this is master object and this request should
2099 * be returned back to llite.
2103 ptlrpc_req_finished(req);
2111 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2112 if (IS_ERR(tgt_exp))
2113 RETURN(PTR_ERR(tgt_exp));
2115 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2116 ea2len, request, mod);
2121 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2122 struct obd_capa *oc, struct ptlrpc_request **request)
2124 struct obd_device *obd = exp->exp_obd;
2125 struct lmv_obd *lmv = &obd->u.lmv;
2126 struct obd_export *tgt_exp;
2130 rc = lmv_check_connect(obd);
2134 tgt_exp = lmv_find_export(lmv, fid);
2135 if (IS_ERR(tgt_exp))
2136 RETURN(PTR_ERR(tgt_exp));
2138 rc = md_sync(tgt_exp, fid, oc, request);
2142 /* main purpose of LMV blocking ast is to remove split directory LMV
2143 * presentation object (struct lmv_obj) attached to the lock being revoked. */
2144 int lmv_blocking_ast(struct ldlm_lock *lock,
2145 struct ldlm_lock_desc *desc,
2146 void *data, int flag)
2148 struct lustre_handle lockh;
2149 struct lmv_obj *obj;
2154 case LDLM_CB_BLOCKING:
2155 ldlm_lock2handle(lock, &lockh);
2156 rc = ldlm_cli_cancel(&lockh);
2158 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2162 case LDLM_CB_CANCELING:
2163 /* time to drop cached attrs for dirobj */
2164 obj = lock->l_ast_data;
2166 CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2167 ", master "DFID"\n",
2168 lock->l_resource->lr_name.name[3] == 1 ?
2169 "LOOKUP" : "UPDATE",
2170 lock->l_resource->lr_name.name[0],
2171 lock->l_resource->lr_name.name[1],
2172 PFID(&obj->lo_fid));
2182 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2186 val = le64_to_cpu(*hash);
2188 val += MAX_HASH_SIZE;
2189 if (val != DIR_END_OFF)
2190 *hash = cpu_to_le64(val - hash_adj);
2193 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2196 struct obd_import *imp;
2199 * XXX Hack: to get nid we assume that underlying obd device is mdc.
2201 imp = class_exp2cliimp(exp);
2202 id = imp->imp_connection->c_self + fid_flatten(fid);
2204 CDEBUG(D_INFO, "node rank: %llx "DFID" %llx %llx\n",
2205 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2207 return id ^ (id >> 32);
2210 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2211 struct obd_capa *oc, __u64 offset64, struct page *page,
2212 struct ptlrpc_request **request)
2214 struct obd_device *obd = exp->exp_obd;
2215 struct lmv_obd *lmv = &obd->u.lmv;
2216 struct obd_export *tgt_exp;
2217 struct lu_fid rid = *fid;
2218 struct lmv_obj *obj;
2232 rc = lmv_check_connect(obd);
2236 CDEBUG(D_INFO, "READPAGE at %llx from "DFID"\n", offset, PFID(&rid));
2238 obj = lmv_obj_grab(obd, fid);
2242 * This case handle directory lookup in clustered metadata case (i.e.
2243 * split directory is located on multiple md servers.)
2244 * each server keeps directory entries for certain range of hashes.
2245 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2246 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2247 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2249 * readdir can simply start reading entries from 0 - N server in
2250 * order but that will not scale well as all client will request dir in
2251 * to server in same order.
2252 * Following algorithm does optimization:
2253 * Instead of doing readdir in 1, 2, ...., N order, client with a
2254 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2255 * (every client has rank R)
2256 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2257 * since client ask dir from MDS{R} client has pages with offsets
2258 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2259 * on hash values that we get.
2262 struct lmv_inode *loi;
2266 nr = obj->lo_objcount;
2268 seg_size = MAX_HASH_SIZE;
2269 do_div(seg_size, nr);
2270 loi = obj->lo_inodes;
2271 rank = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2274 do_div(tgt_tmp, seg_size);
2275 tgt0 = do_div(tgt_tmp, nr);
2276 tgt = (tgt0 + rank) % nr;
2282 * Last segment has unusual length due to division
2285 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2289 hash_adj += rank * seg_size;
2291 CDEBUG(D_INFO, "hash_adj: %x %llx %llx/%x -> %llx/%x\n",
2292 rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2294 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2295 rid = obj->lo_inodes[tgt].li_fid;
2296 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2298 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2299 PFID(&rid), (unsigned long)offset, tgt);
2301 tgt_exp = lmv_find_export(lmv, &rid);
2303 if (IS_ERR(tgt_exp))
2304 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2306 rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2310 struct lu_dirpage *dp;
2311 struct lu_dirent *ent;
2313 dp = cfs_kmap(page);
2315 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2316 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2317 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2319 for (ent = lu_dirent_start(dp); ent != NULL;
2320 ent = lu_dirent_next(ent))
2321 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2323 if (tgt0 != nr - 1) {
2326 end = le64_to_cpu(dp->ldp_hash_end);
2327 if (end == DIR_END_OFF) {
2328 dp->ldp_hash_end = cpu_to_le32(seg_size *
2330 CDEBUG(D_INFO, ""DFID" reset end %llx tgt %d\n",
2332 le64_to_cpu(dp->ldp_hash_end), tgt);
2338 * Here we could remove "." and ".." from all pages which at not from
2339 * master. But MDS has only "." and ".." for master dir.
2344 lmv_obj_unlock(obj);
2350 static int lmv_unlink_slaves(struct obd_export *exp,
2351 struct md_op_data *op_data,
2352 struct ptlrpc_request **req)
2354 struct obd_device *obd = exp->exp_obd;
2355 struct lmv_obd *lmv = &obd->u.lmv;
2356 struct lmv_stripe_md *mea = op_data->op_mea1;
2357 struct md_op_data *op_data2;
2358 struct obd_export *tgt_exp;
2362 OBD_ALLOC_PTR(op_data2);
2363 if (op_data2 == NULL)
2366 op_data2->op_mode = S_IFDIR;
2367 op_data2->op_fsuid = current->fsuid;
2368 op_data2->op_fsgid = current->fsgid;
2369 op_data2->op_bias = 0;
2371 LASSERT(mea != NULL);
2372 for (i = 0; i < mea->mea_count; i++) {
2373 memset(op_data2, 0, sizeof(*op_data2));
2374 op_data2->op_fid1 = mea->mea_ids[i];
2375 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2376 if (IS_ERR(tgt_exp))
2377 GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2379 if (tgt_exp == NULL)
2382 rc = md_unlink(tgt_exp, op_data2, req);
2384 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2385 PFID(&mea->mea_ids[i]), rc);
2388 ptlrpc_req_finished(*req);
2392 GOTO(out_free_op_data2, rc);
2397 OBD_FREE_PTR(op_data2);
2401 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2402 struct ptlrpc_request **request)
2404 struct obd_device *obd = exp->exp_obd;
2405 struct lmv_obd *lmv = &obd->u.lmv;
2406 struct obd_export *tgt_exp = NULL;
2407 struct lmv_obj *obj;
2411 rc = lmv_check_connect(obd);
2415 if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2416 /* mds asks to remove slave objects */
2417 rc = lmv_unlink_slaves(exp, op_data, request);
2424 if (op_data->op_namelen != 0) {
2427 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2429 mea_idx = raw_name2idx(obj->lo_hashtype,
2432 op_data->op_namelen);
2433 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2434 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2435 tgt_exp = lmv_get_export(lmv,
2436 obj->lo_inodes[mea_idx].li_mds);
2438 CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2439 op_data->op_namelen, op_data->op_name,
2440 PFID(&op_data->op_fid1), mea_idx);
2443 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2444 PFID(&op_data->op_fid1));
2446 if (tgt_exp == NULL) {
2447 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2448 if (IS_ERR(tgt_exp))
2449 RETURN(PTR_ERR(tgt_exp));
2450 op_data->op_bias |= MDS_CHECK_SPLIT;
2453 op_data->op_fsuid = current->fsuid;
2454 op_data->op_fsgid = current->fsgid;
2455 op_data->op_cap = current->cap_effective;
2457 /* If child's fid is given, cancel unused locks for it if it is from
2458 * another export than parent. */
2459 if (op_data->op_namelen) {
2460 /* LOOKUP lock for child (fid3) should also be cancelled on
2461 * parent tgt_exp in mdc_unlink(). */
2462 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2464 /* XXX: the case when child is a striped dir is not supported.
2465 * Only the master stripe has all locks cancelled early. */
2466 /* Cancel FULL locks on child (fid3). */
2467 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2468 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2471 rc = md_unlink(tgt_exp, op_data, request);
2472 if (rc == -ERESTART) {
2473 LASSERT(*request != NULL);
2474 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2475 "Got -ERESTART during unlink!\n");
2476 ptlrpc_req_finished(*request);
2480 * Directory got split. Time to update local object and repeat
2481 * the request with proper MDS.
2483 rc = lmv_handle_split(exp, &op_data->op_fid1);
2490 static int lmv_llog_init(struct obd_device *obd, int group,
2491 struct obd_device *tgt, int count,
2492 struct llog_catid *logid, struct obd_uuid *uuid)
2495 struct llog_ctxt *ctxt;
2499 LASSERT(group == OBD_LLOG_GROUP);
2500 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2503 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2504 llog_initiator_connect(ctxt, tgt);
2505 llog_ctxt_put(ctxt);
2513 static int lmv_llog_finish(struct obd_device *obd, int count)
2515 struct llog_ctxt *ctxt;
2519 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2521 rc = llog_cleanup(ctxt);
2526 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2531 case OBD_CLEANUP_EARLY:
2532 /* XXX: here should be calling obd_precleanup() down to
2535 case OBD_CLEANUP_SELF_EXP:
2536 rc = obd_llog_finish(obd, 0);
2538 CERROR("failed to cleanup llogging subsystems\n");
2546 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2547 void *key, __u32 *vallen, void *val)
2549 struct obd_device *obd;
2550 struct lmv_obd *lmv;
2554 obd = class_exp2obd(exp);
2556 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2557 exp->exp_handle.h_cookie);
2562 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2563 struct lmv_tgt_desc *tgts;
2566 rc = lmv_check_connect(obd);
2570 LASSERT(*vallen == sizeof(__u32));
2571 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2574 /* all tgts should be connected when this get called. */
2575 if (!tgts || !tgts->ltd_exp) {
2576 CERROR("target not setup?\n");
2580 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2585 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2586 rc = lmv_check_connect(obd);
2590 /* forwarding this request to first MDS, it should know LOV
2592 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2594 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2595 exp->exp_connect_flags =
2596 ((struct obd_connect_data *)val)->ocd_connect_flags;
2601 CDEBUG(D_IOCTL, "invalid key\n");
2605 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2606 void *key, obd_count vallen, void *val,
2607 struct ptlrpc_request_set *set)
2609 struct lmv_tgt_desc *tgt;
2610 struct obd_device *obd;
2611 struct lmv_obd *lmv;
2615 obd = class_exp2obd(exp);
2617 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2618 exp->exp_handle.h_cookie);
2623 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2624 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2627 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2628 tgt = &lmv->tgts[i];
2633 err = obd_set_info_async(tgt->ltd_exp,
2634 keylen, key, vallen, val, set);
2645 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2646 struct lov_stripe_md *lsm)
2648 struct obd_device *obd = class_exp2obd(exp);
2649 struct lmv_obd *lmv = &obd->u.lmv;
2650 struct lmv_stripe_md *meap, *lsmp;
2654 mea_size = lmv_get_easize(lmv);
2658 if (*lmmp && !lsm) {
2659 OBD_FREE(*lmmp, mea_size);
2664 if (*lmmp == NULL) {
2665 OBD_ALLOC(*lmmp, mea_size);
2673 lsmp = (struct lmv_stripe_md *)lsm;
2674 meap = (struct lmv_stripe_md *)*lmmp;
2676 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2677 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2680 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2681 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2682 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2684 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2685 meap->mea_ids[i] = meap->mea_ids[i];
2686 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2692 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2693 struct lov_mds_md *lmm, int lmm_size)
2695 struct obd_device *obd = class_exp2obd(exp);
2696 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2697 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2698 struct lmv_obd *lmv = &obd->u.lmv;
2703 mea_size = lmv_get_easize(lmv);
2707 if (*lsmp != NULL && lmm == NULL) {
2708 OBD_FREE(*tmea, mea_size);
2713 LASSERT(mea_size == lmm_size);
2715 OBD_ALLOC(*tmea, mea_size);
2722 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2723 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2724 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2726 magic = le32_to_cpu(mea->mea_magic);
2728 /* old mea is not handled here */
2732 (*tmea)->mea_magic = magic;
2733 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2734 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2736 for (i = 0; i < (*tmea)->mea_count; i++) {
2737 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2738 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2743 static int lmv_cancel_unused(struct obd_export *exp,
2744 const struct lu_fid *fid,
2745 ldlm_policy_data_t *policy,
2746 ldlm_mode_t mode, int flags, void *opaque)
2748 struct obd_device *obd = exp->exp_obd;
2749 struct lmv_obd *lmv = &obd->u.lmv;
2753 LASSERT(fid != NULL);
2755 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2756 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2759 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2760 policy, mode, flags, opaque);
2767 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2769 struct obd_device *obd = exp->exp_obd;
2770 struct lmv_obd *lmv = &obd->u.lmv;
2773 RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2776 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2777 const struct lu_fid *fid, ldlm_type_t type,
2778 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2779 struct lustre_handle *lockh)
2781 struct obd_device *obd = exp->exp_obd;
2782 struct lmv_obd *lmv = &obd->u.lmv;
2787 CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2789 /* with CMD every object can have two locks in different namespaces:
2790 * lookup lock in space of mds storing direntry and update/open lock in
2791 * space of mds storing inode. Thus we check all targets, not only that
2792 * one fid was created in. */
2793 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2794 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2795 type, policy, mode, lockh);
2803 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2804 struct obd_export *dt_exp, struct obd_export *md_exp,
2805 struct lustre_md *md)
2807 struct obd_device *obd = exp->exp_obd;
2808 struct lmv_obd *lmv = &obd->u.lmv;
2812 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2816 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2818 struct obd_device *obd = exp->exp_obd;
2819 struct lmv_obd *lmv = &obd->u.lmv;
2823 obd_free_memmd(exp, (struct lov_stripe_md**)&md->mea);
2824 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2827 int lmv_set_open_replay_data(struct obd_export *exp,
2828 struct obd_client_handle *och,
2829 struct ptlrpc_request *open_req)
2831 struct obd_device *obd = exp->exp_obd;
2832 struct lmv_obd *lmv = &obd->u.lmv;
2833 struct obd_export *tgt_exp;
2837 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2838 if (IS_ERR(tgt_exp))
2839 RETURN(PTR_ERR(tgt_exp));
2841 RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2844 int lmv_clear_open_replay_data(struct obd_export *exp,
2845 struct obd_client_handle *och)
2847 struct obd_device *obd = exp->exp_obd;
2848 struct lmv_obd *lmv = &obd->u.lmv;
2849 struct obd_export *tgt_exp;
2852 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2853 if (IS_ERR(tgt_exp))
2854 RETURN(PTR_ERR(tgt_exp));
2856 RETURN(md_clear_open_replay_data(tgt_exp, och));
2859 static int lmv_get_remote_perm(struct obd_export *exp,
2860 const struct lu_fid *fid,
2861 struct obd_capa *oc, __u32 suppgid,
2862 struct ptlrpc_request **request)
2864 struct obd_device *obd = exp->exp_obd;
2865 struct lmv_obd *lmv = &obd->u.lmv;
2866 struct obd_export *tgt_exp;
2871 rc = lmv_check_connect(obd);
2875 tgt_exp = lmv_find_export(lmv, fid);
2876 if (IS_ERR(tgt_exp))
2877 RETURN(PTR_ERR(tgt_exp));
2879 rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2884 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2887 struct obd_device *obd = exp->exp_obd;
2888 struct lmv_obd *lmv = &obd->u.lmv;
2889 struct obd_export *tgt_exp;
2893 rc = lmv_check_connect(obd);
2897 tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2898 if (IS_ERR(tgt_exp))
2899 RETURN(PTR_ERR(tgt_exp));
2901 rc = md_renew_capa(tgt_exp, oc, cb);
2905 int lmv_intent_getattr_async(struct obd_export *exp,
2906 struct md_enqueue_info *minfo,
2907 struct ldlm_enqueue_info *einfo)
2909 struct obd_device *obd = exp->exp_obd;
2910 struct lmv_obd *lmv = &obd->u.lmv;
2911 struct obd_export *tgt_exp;
2915 rc = lmv_check_connect(obd);
2919 if (fid_is_zero(&minfo->mi_data.op_fid2))
2920 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid1);
2922 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid2);
2923 if (IS_ERR(tgt_exp))
2924 RETURN(PTR_ERR(tgt_exp));
2926 rc = md_intent_getattr_async(tgt_exp, minfo, einfo);
2930 int lmv_revalidate_lock(struct obd_export *exp,
2931 struct lookup_intent *it,
2934 struct obd_device *obd = exp->exp_obd;
2935 struct lmv_obd *lmv = &obd->u.lmv;
2936 struct obd_export *tgt_exp;
2940 rc = lmv_check_connect(obd);
2944 tgt_exp = lmv_find_export(lmv, fid);
2945 if (IS_ERR(tgt_exp))
2946 RETURN(PTR_ERR(tgt_exp));
2948 rc = md_revalidate_lock(tgt_exp, it, fid);
2953 struct obd_ops lmv_obd_ops = {
2954 .o_owner = THIS_MODULE,
2955 .o_setup = lmv_setup,
2956 .o_cleanup = lmv_cleanup,
2957 .o_precleanup = lmv_precleanup,
2958 .o_process_config = lmv_process_config,
2959 .o_connect = lmv_connect,
2960 .o_disconnect = lmv_disconnect,
2961 .o_statfs = lmv_statfs,
2962 .o_llog_init = lmv_llog_init,
2963 .o_llog_finish = lmv_llog_finish,
2964 .o_get_info = lmv_get_info,
2965 .o_set_info_async = lmv_set_info_async,
2966 .o_packmd = lmv_packmd,
2967 .o_unpackmd = lmv_unpackmd,
2968 .o_notify = lmv_notify,
2969 .o_iocontrol = lmv_iocontrol,
2970 .o_fid_delete = lmv_fid_delete
2973 struct md_ops lmv_md_ops = {
2974 .m_getstatus = lmv_getstatus,
2975 .m_change_cbdata = lmv_change_cbdata,
2976 .m_close = lmv_close,
2977 .m_create = lmv_create,
2978 .m_done_writing = lmv_done_writing,
2979 .m_enqueue = lmv_enqueue,
2980 .m_getattr = lmv_getattr,
2981 .m_getxattr = lmv_getxattr,
2982 .m_getattr_name = lmv_getattr_name,
2983 .m_intent_lock = lmv_intent_lock,
2985 .m_rename = lmv_rename,
2986 .m_setattr = lmv_setattr,
2987 .m_setxattr = lmv_setxattr,
2989 .m_readpage = lmv_readpage,
2990 .m_unlink = lmv_unlink,
2991 .m_init_ea_size = lmv_init_ea_size,
2992 .m_cancel_unused = lmv_cancel_unused,
2993 .m_set_lock_data = lmv_set_lock_data,
2994 .m_lock_match = lmv_lock_match,
2995 .m_get_lustre_md = lmv_get_lustre_md,
2996 .m_free_lustre_md = lmv_free_lustre_md,
2997 .m_set_open_replay_data = lmv_set_open_replay_data,
2998 .m_clear_open_replay_data = lmv_clear_open_replay_data,
2999 .m_renew_capa = lmv_renew_capa,
3000 .m_get_remote_perm = lmv_get_remote_perm,
3001 .m_intent_getattr_async = lmv_intent_getattr_async,
3002 .m_revalidate_lock = lmv_revalidate_lock
3005 int __init lmv_init(void)
3007 struct lprocfs_static_vars lvars;
3010 obj_cache = cfs_mem_cache_create("lmv_objects",
3011 sizeof(struct lmv_obj),
3014 CERROR("error allocating lmv objects cache\n");
3018 lprocfs_lmv_init_vars(&lvars);
3019 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3020 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3022 cfs_mem_cache_destroy(obj_cache);
3028 static void lmv_exit(void)
3032 class_unregister_type(LUSTRE_LMV_NAME);
3034 rc = cfs_mem_cache_destroy(obj_cache);
3036 "can't free lmv objects cache, %d object(s)"
3037 "still in use\n", atomic_read(&obj_cache_count));
3040 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3041 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3042 MODULE_LICENSE("GPL");
3044 module_init(lmv_init);
3045 module_exit(lmv_exit);