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)
225 struct proc_dir_entry *lmv_proc_dir;
227 struct lmv_obd *lmv = &obd->u.lmv;
228 struct obd_export *exp;
232 rc = class_connect(conn, obd, cluuid);
234 CERROR("class_connection() returned %d\n", rc);
238 exp = class_conn2export(conn);
240 /* we don't want to actually do the underlying connections more than
241 * once, so keep track. */
243 if (lmv->refcount > 1) {
244 class_export_put(exp);
250 lmv->cluuid = *cluuid;
253 lmv->conn_data = *data;
256 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
258 if (IS_ERR(lmv_proc_dir)) {
259 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
260 obd->obd_type->typ_name, obd->obd_name);
265 /* all real clients should perform actual connection right away, because
266 * it is possible, that LMV will not have opportunity to connect targets
267 * and MDC stuff will be called directly, for instance while reading
268 * ../mdc/../kbytesfree procfs file, etc. */
269 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
270 rc = lmv_check_connect(obd);
275 lprocfs_remove(&lmv_proc_dir);
282 static void lmv_set_timeouts(struct obd_device *obd)
284 struct lmv_tgt_desc *tgts;
289 if (lmv->server_timeout == 0)
292 if (lmv->connected == 0)
295 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
296 if (tgts->ltd_exp == NULL)
299 obd_set_info_async(tgts->ltd_exp, strlen("inter_mds"),
300 "inter_mds", 0, NULL, NULL);
304 static int lmv_init_ea_size(struct obd_export *exp, int easize,
305 int def_easize, int cookiesize)
307 struct obd_device *obd = exp->exp_obd;
308 struct lmv_obd *lmv = &obd->u.lmv;
309 int i, rc = 0, change = 0;
312 if (lmv->max_easize < easize) {
313 lmv->max_easize = easize;
316 if (lmv->max_def_easize < def_easize) {
317 lmv->max_def_easize = def_easize;
320 if (lmv->max_cookiesize < cookiesize) {
321 lmv->max_cookiesize = cookiesize;
327 if (lmv->connected == 0)
330 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
331 if (lmv->tgts[i].ltd_exp == NULL) {
332 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
336 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
339 CERROR("obd_init_ea_size() failed on MDT target %d, "
340 "error %d.\n", i, rc);
347 #define MAX_STRING_SIZE 128
349 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
351 struct lmv_obd *lmv = &obd->u.lmv;
352 struct obd_uuid *cluuid = &lmv->cluuid;
353 struct obd_connect_data *mdc_data = NULL;
354 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
355 struct lustre_handle conn = {0, };
356 struct obd_device *mdc_obd;
357 struct obd_export *mdc_exp;
358 struct lu_fld_target target;
361 struct proc_dir_entry *lmv_proc_dir;
365 /* for MDS: don't connect to yourself */
366 if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
367 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
368 /* XXX - the old code didn't increment active tgt count.
373 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
376 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
380 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
381 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
382 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
385 if (!mdc_obd->obd_set_up) {
386 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
390 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
393 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
397 mdc_exp = class_conn2export(&conn);
399 /* Init fid sequence client for this mdc. */
400 rc = obd_fid_init(mdc_exp);
404 /* Add new FLD target. */
405 target.ft_srv = NULL;
406 target.ft_exp = mdc_exp;
407 target.ft_idx = tgt->ltd_idx;
409 fld_client_add_target(&lmv->lmv_fld, &target);
411 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
413 rc = obd_register_observer(mdc_obd, obd);
415 obd_disconnect(mdc_exp);
416 CERROR("target %s register_observer error %d\n",
417 tgt->ltd_uuid.uuid, rc);
421 if (obd->obd_observer) {
422 /* tell the mds_lmv about the new target */
423 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
424 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
426 obd_disconnect(mdc_exp);
432 tgt->ltd_exp = mdc_exp;
433 lmv->desc.ld_active_tgt_count++;
435 /* copy connect data, it may be used later */
436 lmv->datas[tgt->ltd_idx] = *mdc_data;
438 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
439 lmv->max_def_easize, lmv->max_cookiesize);
441 CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
442 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
443 atomic_read(&obd->obd_refcount));
446 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
448 struct proc_dir_entry *mdc_symlink;
449 char name[MAX_STRING_SIZE + 1];
451 LASSERT(mdc_obd->obd_type != NULL);
452 LASSERT(mdc_obd->obd_type->typ_name != NULL);
453 name[MAX_STRING_SIZE] = '\0';
454 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
455 mdc_obd->obd_type->typ_name,
457 mdc_symlink = proc_symlink(mdc_obd->obd_name,
459 if (mdc_symlink == NULL) {
460 CERROR("could not register LMV target "
461 "/proc/fs/lustre/%s/%s/target_obds/%s.",
462 obd->obd_type->typ_name, obd->obd_name,
464 lprocfs_remove(&lmv_proc_dir);
472 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
474 struct lmv_obd *lmv = &obd->u.lmv;
475 struct lmv_tgt_desc *tgt;
479 CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
483 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
484 lmv_init_unlock(lmv);
485 CERROR("can't add %s, LMV module compiled for %d MDCs. "
486 "That many MDCs already configured.\n",
487 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
490 if (lmv->desc.ld_tgt_count == 0) {
491 struct obd_device *mdc_obd;
493 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
496 lmv_init_unlock(lmv);
497 CERROR("Target %s not attached\n", tgt_uuid->uuid);
501 rc = obd_llog_init(obd, OBD_LLOG_GROUP, mdc_obd, 0, NULL, tgt_uuid);
503 lmv_init_unlock(lmv);
504 CERROR("lmv failed to setup llogging subsystems\n");
507 spin_lock(&lmv->lmv_lock);
508 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
509 tgt->ltd_uuid = *tgt_uuid;
510 spin_unlock(&lmv->lmv_lock);
512 if (lmv->connected) {
513 rc = lmv_connect_mdc(obd, tgt);
515 spin_lock(&lmv->lmv_lock);
516 lmv->desc.ld_tgt_count--;
517 memset(tgt, 0, sizeof(*tgt));
518 spin_unlock(&lmv->lmv_lock);
520 int easize = sizeof(struct lmv_stripe_md) +
521 lmv->desc.ld_tgt_count *
522 sizeof(struct lu_fid);
523 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
527 lmv_init_unlock(lmv);
531 /* performs a check if passed obd is connected. If no - connect it. */
532 int lmv_check_connect(struct obd_device *obd)
534 struct lmv_obd *lmv = &obd->u.lmv;
535 struct lmv_tgt_desc *tgt;
543 if (lmv->connected) {
544 lmv_init_unlock(lmv);
548 if (lmv->desc.ld_tgt_count == 0) {
549 CERROR("%s: no targets configured.\n", obd->obd_name);
553 CDEBUG(D_CONFIG, "time to connect %s to %s\n",
554 lmv->cluuid.uuid, obd->obd_name);
556 LASSERT(lmv->tgts != NULL);
558 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
559 rc = lmv_connect_mdc(obd, tgt);
564 lmv_set_timeouts(obd);
565 class_export_put(lmv->exp);
567 easize = lmv_get_easize(lmv);
568 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
569 lmv_init_unlock(lmv);
578 --lmv->desc.ld_active_tgt_count;
579 rc2 = obd_disconnect(tgt->ltd_exp);
581 CERROR("error: LMV target %s disconnect on "
582 "MDC idx %d: error %d\n",
583 tgt->ltd_uuid.uuid, i, rc2);
587 class_disconnect(lmv->exp);
588 lmv_init_unlock(lmv);
592 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
595 struct proc_dir_entry *lmv_proc_dir;
597 struct lmv_obd *lmv = &obd->u.lmv;
598 struct obd_device *mdc_obd;
602 LASSERT(tgt != NULL);
603 LASSERT(obd != NULL);
605 mdc_obd = class_exp2obd(tgt->ltd_exp);
608 mdc_obd->obd_no_recov = obd->obd_no_recov;
611 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
613 struct proc_dir_entry *mdc_symlink;
615 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
617 lprocfs_remove(&mdc_symlink);
619 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
620 obd->obd_type->typ_name, obd->obd_name,
625 rc = obd_fid_fini(tgt->ltd_exp);
627 CERROR("Can't finanize fids factory\n");
629 CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
630 tgt->ltd_exp->exp_obd->obd_name,
631 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
633 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
634 rc = obd_disconnect(tgt->ltd_exp);
636 if (tgt->ltd_active) {
637 CERROR("Target %s disconnect error %d\n",
638 tgt->ltd_uuid.uuid, rc);
642 lmv_activate_target(lmv, tgt, 0);
647 static int lmv_disconnect(struct obd_export *exp)
649 struct obd_device *obd = class_exp2obd(exp);
651 struct proc_dir_entry *lmv_proc_dir;
653 struct lmv_obd *lmv = &obd->u.lmv;
660 /* Only disconnect the underlying layers on the final disconnect. */
662 if (lmv->refcount != 0)
665 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
666 if (lmv->tgts[i].ltd_exp == NULL)
668 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
672 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
674 lprocfs_remove(&lmv_proc_dir);
676 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
677 obd->obd_type->typ_name, obd->obd_name);
683 * This is the case when no real connection is established by
684 * lmv_check_connect().
687 class_export_put(exp);
688 rc = class_disconnect(exp);
689 if (lmv->refcount == 0)
694 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
695 int len, void *karg, void *uarg)
697 struct obd_device *obddev = class_exp2obd(exp);
698 struct lmv_obd *lmv = &obddev->u.lmv;
699 int i, rc = 0, set = 0;
702 if (lmv->desc.ld_tgt_count == 0)
706 case IOC_OBD_STATFS: {
707 struct obd_ioctl_data *data = karg;
708 struct obd_device *mdc_obd;
709 struct obd_statfs stat_buf = {0};
712 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
713 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
715 if ((index >= lmv->desc.ld_tgt_count))
718 if (!lmv->tgts[index].ltd_active)
721 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
725 /* got statfs data */
726 rc = obd_statfs(mdc_obd, &stat_buf,
727 cfs_time_current_64() - HZ, 0);
730 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
733 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
738 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
741 if (lmv->tgts[i].ltd_exp == NULL)
744 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
747 if (lmv->tgts[i].ltd_active) {
748 CERROR("error: iocontrol MDC %s on MDT"
749 "idx %d cmd %x: err = %d\n",
750 lmv->tgts[i].ltd_uuid.uuid,
770 static int lmv_all_chars_policy(int count, const char *name,
781 static int lmv_nid_policy(struct lmv_obd *lmv)
783 struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
786 * XXX Hack: to get nid we assume that underlying obd device is mdc.
788 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
789 return id % lmv->desc.ld_tgt_count;
792 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
797 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
799 op_data->op_namelen);
801 return lmv_nid_policy(lmv);
807 CERROR("unsupport type %d \n", type);
811 /* This is _inode_ placement policy function (not name). */
812 static int lmv_placement_policy(struct obd_device *obd,
813 struct md_op_data *op_data,
816 struct lmv_obd *lmv = &obd->u.lmv;
821 LASSERT(mds != NULL);
824 * Allocate new fid on target according to operation type and parent
827 obj = lmv_obj_grab(obd, &op_data->op_fid1);
828 if (obj != NULL || op_data->op_name == NULL ||
829 op_data->op_opc != LUSTRE_OPC_MKDIR) {
831 * Allocate fid for non-dir or for null name or for case parent
838 * If we have this flag turned on, and we see that
839 * parent dir is split, this means, that caller did not
840 * notice split yet. This is race and we would like to
841 * let caller know that.
843 if (op_data->op_bias & MDS_CHECK_SPLIT)
848 * Allocate new fid on same mds where parent fid is located and
849 * where operation will be sent. In case of split dir, ->op_fid1
850 * and ->op_mds here will contain fid and mds of slave directory
851 * object (assigned by caller).
853 *mds = op_data->op_mds;
857 /* XXX: This should be removed later wehn we sure it is not
859 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
865 * Parent directory is not split and we want to create a
866 * directory in it. Let's calculate where to place it according
869 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
877 CERROR("Can't choose MDS, err = %d\n", rc);
879 LASSERT(*mds < lmv->desc.ld_tgt_count);
885 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
888 struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
892 /* New seq alloc and FLD setup should be atomic. */
893 down(&tgt->ltd_fid_sem);
895 /* Asking underlaying tgt layer to allocate new fid. */
896 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
898 LASSERT(fid_is_sane(fid));
900 /* Client switches to new sequence, setup FLD. */
901 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
904 CERROR("Can't create fld entry, rc %d\n", rc);
905 /* Delete just allocated fid sequence */
906 obd_fid_delete(tgt->ltd_exp, NULL);
909 up(&tgt->ltd_fid_sem);
913 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
914 struct md_op_data *op_data)
916 struct obd_device *obd = class_exp2obd(exp);
917 struct lmv_obd *lmv = &obd->u.lmv;
922 LASSERT(op_data != NULL);
923 LASSERT(fid != NULL);
925 rc = lmv_placement_policy(obd, op_data, &mds);
927 CERROR("Can't get target for allocating fid, "
932 rc = __lmv_fid_alloc(lmv, fid, mds);
934 CERROR("Can't alloc new fid, rc %d\n", rc);
941 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
946 if (lmv_obj_delete(exp, fid)) {
947 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
953 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
955 struct lmv_obd *lmv = &obd->u.lmv;
956 struct lprocfs_static_vars lvars;
957 struct lmv_desc *desc;
961 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
962 CERROR("LMV setup requires a descriptor\n");
966 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
967 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
968 CERROR("descriptor size wrong: %d > %d\n",
969 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
973 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
975 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
976 if (lmv->tgts == NULL)
979 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
980 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
981 lmv->tgts[i].ltd_idx = i;
984 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
986 OBD_ALLOC(lmv->datas, lmv->datas_size);
987 if (lmv->datas == NULL)
988 GOTO(out_free_tgts, rc = -ENOMEM);
990 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
991 lmv->desc.ld_tgt_count = 0;
992 lmv->desc.ld_active_tgt_count = 0;
993 lmv->max_cookiesize = 0;
994 lmv->max_def_easize = 0;
997 spin_lock_init(&lmv->lmv_lock);
998 sema_init(&lmv->init_sem, 1);
1000 rc = lmv_obj_setup(obd);
1002 CERROR("Can't setup LMV object manager, "
1004 GOTO(out_free_datas, rc);
1007 lprocfs_lmv_init_vars(&lvars);
1008 lprocfs_obd_setup(obd, lvars.obd_vars);
1011 struct proc_dir_entry *entry;
1013 entry = create_proc_entry("target_obd_status", 0444,
1014 obd->obd_proc_entry);
1015 if (entry != NULL) {
1016 entry->proc_fops = &lmv_proc_target_fops;
1021 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1022 LUSTRE_CLI_FLD_HASH_DHT);
1024 CERROR("can't init FLD, err %d\n",
1026 GOTO(out_free_datas, rc);
1032 OBD_FREE(lmv->datas, lmv->datas_size);
1035 OBD_FREE(lmv->tgts, lmv->tgts_size);
1040 static int lmv_cleanup(struct obd_device *obd)
1042 struct lmv_obd *lmv = &obd->u.lmv;
1045 fld_client_fini(&lmv->lmv_fld);
1046 lprocfs_obd_cleanup(obd);
1047 lmv_obj_cleanup(obd);
1048 OBD_FREE(lmv->datas, lmv->datas_size);
1049 OBD_FREE(lmv->tgts, lmv->tgts_size);
1054 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1056 struct lustre_cfg *lcfg = buf;
1057 struct obd_uuid tgt_uuid;
1061 switch(lcfg->lcfg_command) {
1063 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1064 GOTO(out, rc = -EINVAL);
1066 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1067 rc = lmv_add_target(obd, &tgt_uuid);
1070 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1071 GOTO(out, rc = -EINVAL);
1078 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1079 __u64 max_age, __u32 flags)
1081 struct lmv_obd *lmv = &obd->u.lmv;
1082 struct obd_statfs *temp;
1086 rc = lmv_check_connect(obd);
1090 OBD_ALLOC(temp, sizeof(*temp));
1094 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1095 if (lmv->tgts[i].ltd_exp == NULL)
1098 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1101 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1102 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1104 GOTO(out_free_temp, rc);
1109 osfs->os_bavail += temp->os_bavail;
1110 osfs->os_blocks += temp->os_blocks;
1111 osfs->os_ffree += temp->os_ffree;
1112 osfs->os_files += temp->os_files;
1118 OBD_FREE(temp, sizeof(*temp));
1122 static int lmv_getstatus(struct obd_export *exp,
1124 struct obd_capa **pc)
1126 struct obd_device *obd = exp->exp_obd;
1127 struct lmv_obd *lmv = &obd->u.lmv;
1131 rc = lmv_check_connect(obd);
1135 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1140 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1141 struct obd_capa *oc, obd_valid valid, const char *name,
1142 const char *input, int input_size, int output_size,
1143 int flags, struct ptlrpc_request **request)
1145 struct obd_device *obd = exp->exp_obd;
1146 struct lmv_obd *lmv = &obd->u.lmv;
1147 struct obd_export *tgt_exp;
1151 rc = lmv_check_connect(obd);
1155 tgt_exp = lmv_find_export(lmv, fid);
1156 if (IS_ERR(tgt_exp))
1157 RETURN(PTR_ERR(tgt_exp));
1159 rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1160 output_size, flags, request);
1165 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1166 struct obd_capa *oc, obd_valid valid, const char *name,
1167 const char *input, int input_size, int output_size,
1168 int flags, __u32 suppgid,
1169 struct ptlrpc_request **request)
1171 struct obd_device *obd = exp->exp_obd;
1172 struct lmv_obd *lmv = &obd->u.lmv;
1173 struct obd_export *tgt_exp;
1177 rc = lmv_check_connect(obd);
1181 tgt_exp = lmv_find_export(lmv, fid);
1182 if (IS_ERR(tgt_exp))
1183 RETURN(PTR_ERR(tgt_exp));
1185 rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1186 input, input_size, output_size, flags, suppgid,
1192 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1193 struct obd_capa *oc, obd_valid valid, int ea_size,
1194 struct ptlrpc_request **request)
1196 struct obd_device *obd = exp->exp_obd;
1197 struct lmv_obd *lmv = &obd->u.lmv;
1198 struct obd_export *tgt_exp;
1199 struct lmv_obj *obj;
1203 rc = lmv_check_connect(obd);
1207 tgt_exp = lmv_find_export(lmv, fid);
1208 if (IS_ERR(tgt_exp))
1209 RETURN(PTR_ERR(tgt_exp));
1211 rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1215 obj = lmv_obj_grab(obd, fid);
1217 CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1218 obj ? "(split)" : "");
1221 * If object is split, then we loop over all the slaves and gather size
1222 * attribute. In ideal world we would have to gather also mds field from
1223 * all slaves, as object is spread over the cluster and this is
1224 * definitely interesting information and it is not good to loss it,
1228 struct mdt_body *body;
1230 if (*request == NULL) {
1235 body = req_capsule_server_get(&(*request)->rq_pill,
1237 LASSERT(body != NULL);
1241 for (i = 0; i < obj->lo_objcount; i++) {
1242 if (lmv->tgts[i].ltd_exp == NULL) {
1243 CWARN("%s: NULL export for %d\n",
1248 /* skip master obj. */
1249 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1252 lmv_update_body(body, &obj->lo_inodes[i]);
1255 lmv_obj_unlock(obj);
1262 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1263 ldlm_iterator_t it, void *data)
1265 struct obd_device *obd = exp->exp_obd;
1266 struct lmv_obd *lmv = &obd->u.lmv;
1270 rc = lmv_check_connect(obd);
1274 CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1277 * With CMD every object can have two locks in different namespaces:
1278 * lookup lock in space of mds storing direntry and update/open lock in
1279 * space of mds storing inode.
1281 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1282 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1287 static int lmv_close(struct obd_export *exp,
1288 struct md_op_data *op_data,
1289 struct md_open_data *mod,
1290 struct ptlrpc_request **request)
1292 struct obd_device *obd = exp->exp_obd;
1293 struct lmv_obd *lmv = &obd->u.lmv;
1294 struct obd_export *tgt_exp;
1298 rc = lmv_check_connect(obd);
1302 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1303 if (IS_ERR(tgt_exp))
1304 RETURN(PTR_ERR(tgt_exp));
1306 CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1307 rc = md_close(tgt_exp, op_data, mod, request);
1312 * Called in the case MDS returns -ERESTART on create on open, what means that
1313 * directory is split and its LMV presentation object has to be updated.
1315 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1317 struct obd_device *obd = exp->exp_obd;
1318 struct lmv_obd *lmv = &obd->u.lmv;
1319 struct ptlrpc_request *req = NULL;
1320 struct obd_export *tgt_exp;
1321 struct lmv_obj *obj;
1322 struct lustre_md md;
1328 mealen = lmv_get_easize(lmv);
1330 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1332 tgt_exp = lmv_find_export(lmv, fid);
1333 if (IS_ERR(tgt_exp))
1334 RETURN(PTR_ERR(tgt_exp));
1336 /* time to update mea of parent fid */
1337 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1339 CERROR("md_getattr() failed, error %d\n", rc);
1343 rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1345 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1350 GOTO(cleanup, rc = -ENODATA);
1352 obj = lmv_obj_create(exp, fid, md.mea);
1358 obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
1363 ptlrpc_req_finished(req);
1367 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1368 const void *data, int datalen, int mode, __u32 uid,
1369 __u32 gid, __u32 cap_effective, __u64 rdev,
1370 struct ptlrpc_request **request)
1372 struct obd_device *obd = exp->exp_obd;
1373 struct lmv_obd *lmv = &obd->u.lmv;
1374 struct obd_export *tgt_exp;
1375 struct lmv_obj *obj;
1379 rc = lmv_check_connect(obd);
1383 if (!lmv->desc.ld_active_tgt_count)
1388 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1392 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1393 op_data->op_name, op_data->op_namelen);
1394 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1395 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1396 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1397 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1400 struct lmv_tgt_desc *tgt;
1402 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1403 op_data->op_bias |= MDS_CHECK_SPLIT;
1404 op_data->op_mds = tgt->ltd_idx;
1405 tgt_exp = tgt->ltd_exp;
1408 if (IS_ERR(tgt_exp))
1409 RETURN(PTR_ERR(tgt_exp));
1411 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1412 if (rc == -ERESTART)
1417 CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1418 op_data->op_name, PFID(&op_data->op_fid1));
1420 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1421 rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1422 cap_effective, rdev, request);
1424 if (*request == NULL)
1426 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1427 } else if (rc == -ERESTART) {
1428 LASSERT(*request != NULL);
1429 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1430 "Got -ERESTART during create!\n");
1431 ptlrpc_req_finished(*request);
1435 * Directory got split. Time to update local object and repeat
1436 * the request with proper MDS.
1438 rc = lmv_handle_split(exp, &op_data->op_fid1);
1440 rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1441 op_data, &op_data->op_fid2);
1450 static int lmv_done_writing(struct obd_export *exp,
1451 struct md_op_data *op_data,
1452 struct md_open_data *mod)
1454 struct obd_device *obd = exp->exp_obd;
1455 struct lmv_obd *lmv = &obd->u.lmv;
1456 struct obd_export *tgt_exp;
1460 rc = lmv_check_connect(obd);
1464 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1465 if (IS_ERR(tgt_exp))
1466 RETURN(PTR_ERR(tgt_exp));
1468 rc = md_done_writing(tgt_exp, op_data, mod);
1473 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1474 struct lookup_intent *it, struct md_op_data *op_data,
1475 struct lustre_handle *lockh, void *lmm, int lmmsize)
1477 struct obd_device *obd = exp->exp_obd;
1478 struct lmv_obd *lmv = &obd->u.lmv;
1479 struct lmv_stripe_md *mea = op_data->op_mea1;
1480 struct md_op_data *op_data2;
1481 struct obd_export *tgt_exp;
1485 OBD_ALLOC_PTR(op_data2);
1486 if (op_data2 == NULL)
1489 LASSERT(mea != NULL);
1490 for (i = 0; i < mea->mea_count; i++) {
1491 memset(op_data2, 0, sizeof(*op_data2));
1492 op_data2->op_fid1 = mea->mea_ids[i];
1493 op_data2->op_bias = 0;
1495 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1496 if (IS_ERR(tgt_exp))
1497 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1499 if (tgt_exp == NULL)
1502 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1503 lockh + i, lmm, lmmsize, 0);
1505 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1506 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1511 if (it->d.lustre.it_data) {
1512 struct ptlrpc_request *req;
1513 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1514 ptlrpc_req_finished(req);
1517 if (it->d.lustre.it_status)
1518 GOTO(cleanup, rc = it->d.lustre.it_status);
1523 OBD_FREE_PTR(op_data2);
1526 /* drop all taken locks */
1528 if (lockh[i].cookie)
1529 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1530 lockh[i].cookie = 0;
1537 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1538 struct lookup_intent *it, struct md_op_data *op_data,
1539 struct lustre_handle *lockh, void *lmm, int lmmsize,
1540 int extra_lock_flags)
1542 struct ptlrpc_request *req = it->d.lustre.it_data;
1543 struct obd_device *obd = exp->exp_obd;
1544 struct lmv_obd *lmv = &obd->u.lmv;
1545 struct lustre_handle plock;
1546 struct obd_export *tgt_exp;
1547 struct md_op_data *rdata;
1548 struct lu_fid fid_copy;
1549 struct mdt_body *body;
1553 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1554 LASSERT(body != NULL);
1556 if (!(body->valid & OBD_MD_MDS))
1559 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1560 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1562 /* We got LOOKUP lock, but we really need attrs */
1563 pmode = it->d.lustre.it_lock_mode;
1564 LASSERT(pmode != 0);
1565 memcpy(&plock, lockh, sizeof(plock));
1566 it->d.lustre.it_lock_mode = 0;
1567 it->d.lustre.it_data = NULL;
1568 fid_copy = body->fid1;
1570 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1571 ptlrpc_req_finished(req);
1573 tgt_exp = lmv_find_export(lmv, &fid_copy);
1574 if (IS_ERR(tgt_exp))
1575 GOTO(out, rc = PTR_ERR(tgt_exp));
1577 OBD_ALLOC_PTR(rdata);
1579 GOTO(out, rc = -ENOMEM);
1581 rdata->op_fid1 = fid_copy;
1582 rdata->op_bias = MDS_CROSS_REF;
1584 rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1585 lmm, lmmsize, extra_lock_flags);
1586 OBD_FREE_PTR(rdata);
1589 ldlm_lock_decref(&plock, pmode);
1594 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1595 struct lookup_intent *it, struct md_op_data *op_data,
1596 struct lustre_handle *lockh, void *lmm, int lmmsize,
1597 int extra_lock_flags)
1599 struct obd_device *obd = exp->exp_obd;
1600 struct lmv_obd *lmv = &obd->u.lmv;
1601 struct obd_export *tgt_exp = NULL;
1602 struct lmv_obj *obj;
1606 rc = lmv_check_connect(obd);
1610 if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1611 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1612 lockh, lmm, lmmsize);
1616 if (op_data->op_namelen) {
1617 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1621 /* directory is split. look for right mds for this
1623 mea_idx = raw_name2idx(obj->lo_hashtype,
1625 (char *)op_data->op_name,
1626 op_data->op_namelen);
1627 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1628 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1633 if (tgt_exp == NULL)
1634 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1635 if (IS_ERR(tgt_exp))
1636 RETURN(PTR_ERR(tgt_exp));
1638 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1639 PFID(&op_data->op_fid1));
1641 rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1642 lmm, lmmsize, extra_lock_flags);
1644 if (rc == 0 && it->it_op == IT_OPEN)
1645 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1646 lmm, lmmsize, extra_lock_flags);
1651 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1652 struct obd_capa *oc, const char *filename, int namelen,
1653 obd_valid valid, int ea_size, __u32 suppgid,
1654 struct ptlrpc_request **request)
1656 struct obd_device *obd = exp->exp_obd;
1657 struct lmv_obd *lmv = &obd->u.lmv;
1658 struct lu_fid rid = *fid;
1659 struct obd_export *tgt_exp;
1660 struct mdt_body *body;
1661 struct lmv_obj *obj;
1665 rc = lmv_check_connect(obd);
1672 obj = lmv_obj_grab(obd, &rid);
1676 /* Directory is split. Look for right mds for this name */
1677 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1678 filename, namelen - 1);
1679 rid = obj->lo_inodes[mea_idx].li_fid;
1680 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1682 valid &= ~OBD_MD_FLCKSPLIT;
1684 tgt_exp = lmv_find_export(lmv, &rid);
1685 valid |= OBD_MD_FLCKSPLIT;
1687 if (IS_ERR(tgt_exp))
1688 RETURN(PTR_ERR(tgt_exp));
1690 CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1691 namelen, filename, PFID(fid), PFID(&rid));
1693 rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1694 ea_size, suppgid, request);
1696 body = req_capsule_server_get(&(*request)->rq_pill,
1698 LASSERT(body != NULL);
1700 if (body->valid & OBD_MD_MDS) {
1701 struct ptlrpc_request *req = NULL;
1704 CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1707 tgt_exp = lmv_find_export(lmv, &rid);
1708 if (IS_ERR(tgt_exp)) {
1709 ptlrpc_req_finished(*request);
1710 RETURN(PTR_ERR(tgt_exp));
1713 rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1714 valid | OBD_MD_FLCROSSREF,
1715 ea_size, suppgid, &req);
1716 ptlrpc_req_finished(*request);
1719 } else if (rc == -ERESTART) {
1720 LASSERT(*request != NULL);
1721 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1722 "Got -ERESTART during getattr!\n");
1723 ptlrpc_req_finished(*request);
1727 * Directory got split. Time to update local object and repeat
1728 * the request with proper MDS.
1730 rc = lmv_handle_split(exp, &rid);
1737 #define md_op_data_fid(op_data, fl) \
1738 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1739 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1740 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1741 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1744 /* @tgt_exp is the export the metadata request is sent.
1745 * @fid_exp is the export the cancel should be sent for the current fid.
1746 * if @fid_exp is NULL, the export is found for the current fid.
1747 * @op_data keeps the current fid, which is pointed through @flag.
1748 * @mode, @bits -- lock match parameters. */
1749 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1750 struct obd_export *fid_exp,
1751 struct md_op_data *op_data,
1752 ldlm_mode_t mode, int bits, int flag)
1754 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1755 ldlm_policy_data_t policy = {{0}};
1759 if (!fid_is_sane(fid))
1762 if (fid_exp == NULL)
1763 fid_exp = lmv_find_export(lmv, fid);
1765 if (tgt_exp == fid_exp) {
1766 /* The export is the same as on the target server, cancel
1767 * will be sent along with the main metadata operation. */
1768 op_data->op_flags |= flag;
1772 policy.l_inodebits.bits = bits;
1773 rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1777 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1778 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1779 * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1780 * md_cancel_unused for child export(s). */
1781 static int lmv_early_cancel_stripes(struct obd_export *exp,
1782 struct obd_export *tgt_exp,
1783 struct md_op_data *op_data,
1784 ldlm_mode_t mode, int bits, int flag)
1786 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1787 struct obd_device *obd = exp->exp_obd;
1788 struct lmv_obd *lmv = &obd->u.lmv;
1789 struct obd_export *st_exp;
1790 struct lmv_obj *obj;
1794 if (!fid_is_sane(fid))
1797 obj = lmv_obj_grab(obd, fid);
1799 ldlm_policy_data_t policy = {{0}};
1800 struct lu_fid *st_fid;
1803 policy.l_inodebits.bits = bits;
1804 for (i = 0; i < obj->lo_objcount; i++) {
1805 st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1806 st_fid = &obj->lo_inodes[i].li_fid;
1807 if (tgt_exp != st_exp) {
1808 rc = md_cancel_unused(st_exp, st_fid, &policy,
1809 mode, LDLM_FL_ASYNC,
1814 /* Some export matches to @tgt_exp, do cancel
1815 * for its fid in mdc */
1817 op_data->op_flags |= flag;
1822 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1830 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1833 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1834 struct ptlrpc_request **request)
1836 struct obd_device *obd = exp->exp_obd;
1837 struct lmv_obd *lmv = &obd->u.lmv;
1838 struct obd_export *tgt_exp;
1839 struct lmv_obj *obj;
1844 rc = lmv_check_connect(obd);
1851 if (op_data->op_namelen != 0) {
1854 /* Usual link request */
1855 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1857 mea_idx = raw_name2idx(obj->lo_hashtype,
1860 op_data->op_namelen);
1861 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1862 mds = obj->lo_inodes[mea_idx].li_mds;
1865 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1870 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1871 PFID(&op_data->op_fid2), op_data->op_namelen,
1872 op_data->op_name, PFID(&op_data->op_fid1));
1874 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1878 /* request from MDS to acquire i_links for inode by fid1 */
1879 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1880 PFID(&op_data->op_fid1));
1883 CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1884 mds, PFID(&op_data->op_fid1));
1886 op_data->op_fsuid = current->fsuid;
1887 op_data->op_fsgid = current->fsgid;
1888 op_data->op_cap = current->cap_effective;
1890 tgt_exp = lmv->tgts[mds].ltd_exp;
1891 if (op_data->op_namelen) {
1892 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1893 /* Cancel UPDATE lock on child (fid1). */
1894 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1895 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1898 rc = md_link(tgt_exp, op_data, request);
1899 if (rc == -ERESTART) {
1900 LASSERT(*request != NULL);
1901 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1902 "Got -ERESTART during link!\n");
1903 ptlrpc_req_finished(*request);
1907 * Directory got split. Time to update local object and repeat
1908 * the request with proper MDS.
1910 rc = lmv_handle_split(exp, &op_data->op_fid2);
1918 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1919 const char *old, int oldlen, const char *new, int newlen,
1920 struct ptlrpc_request **request)
1922 struct obd_export *tgt_exp = NULL, *src_exp;
1923 struct obd_device *obd = exp->exp_obd;
1924 struct lmv_obd *lmv = &obd->u.lmv;
1925 int rc, mea_idx, loop = 0;
1926 struct lmv_obj *obj;
1930 CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1931 oldlen, old, PFID(&op_data->op_fid1),
1932 newlen, new, PFID(&op_data->op_fid2));
1934 rc = lmv_check_connect(obd);
1940 * MDS with old dir entry is asking another MDS to create name
1944 "create %*s(%d/%d) in "DFID" pointing "
1945 "to "DFID"\n", newlen, new, oldlen, newlen,
1946 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1948 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1953 * Target directory can be split, sowe should forward request to
1956 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1958 mea_idx = raw_name2idx(obj->lo_hashtype,
1960 (char *)new, newlen);
1961 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1962 CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1963 PFID(&op_data->op_fid2));
1972 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1975 * directory is already split, so we have to forward request to
1978 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1979 (char *)old, oldlen);
1980 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1981 mds1 = obj->lo_inodes[mea_idx].li_mds;
1982 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1985 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
1990 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1993 * Directory is already split, so we have to forward request to
1996 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1997 (char *)new, newlen);
1999 mds2 = obj->lo_inodes[mea_idx].li_mds;
2000 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
2001 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2004 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2010 op_data->op_fsuid = current->fsuid;
2011 op_data->op_fsgid = current->fsgid;
2012 op_data->op_cap = current->cap_effective;
2014 src_exp = lmv_get_export(lmv, mds1);
2015 tgt_exp = lmv_get_export(lmv, mds2);
2017 /* LOOKUP lock on src child (fid3) should also be cancelled for
2018 * src_exp in mdc_rename. */
2019 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2021 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2023 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2024 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2026 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2028 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2029 LCK_EX, MDS_INODELOCK_LOOKUP,
2030 MF_MDC_CANCEL_FID4);
2032 /* XXX: the case when child is a striped dir is not supported.
2033 * Only the master stripe has all locks cancelled early. */
2034 /* Cancel all the locks on tgt child (fid4). */
2036 rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2037 LCK_EX, MDS_INODELOCK_FULL,
2038 MF_MDC_CANCEL_FID4);
2042 rc = md_rename(src_exp, op_data, old, oldlen,
2043 new, newlen, request);
2044 if (rc == -ERESTART) {
2045 LASSERT(*request != NULL);
2046 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2047 "Got -ERESTART during rename!\n");
2048 ptlrpc_req_finished(*request);
2052 * Directory got split. Time to update local object and repeat
2053 * the request with proper MDS.
2055 rc = lmv_handle_split(exp, &op_data->op_fid1);
2062 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2063 void *ea, int ealen, void *ea2, int ea2len,
2064 struct ptlrpc_request **request,
2065 struct md_open_data **mod)
2067 struct obd_device *obd = exp->exp_obd;
2068 struct lmv_obd *lmv = &obd->u.lmv;
2069 struct ptlrpc_request *req;
2070 struct obd_export *tgt_exp;
2071 struct lmv_obj *obj;
2075 rc = lmv_check_connect(obd);
2079 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2081 CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2082 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2083 obj ? ", split" : "");
2085 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2087 for (i = 0; i < obj->lo_objcount; i++) {
2088 op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2090 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2091 if (IS_ERR(tgt_exp)) {
2092 rc = PTR_ERR(tgt_exp);
2096 rc = md_setattr(tgt_exp, op_data, ea, ealen,
2097 ea2, ea2len, &req, mod);
2099 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2101 * this is master object and this request should
2102 * be returned back to llite.
2106 ptlrpc_req_finished(req);
2114 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2115 if (IS_ERR(tgt_exp))
2116 RETURN(PTR_ERR(tgt_exp));
2118 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2119 ea2len, request, mod);
2124 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2125 struct obd_capa *oc, struct ptlrpc_request **request)
2127 struct obd_device *obd = exp->exp_obd;
2128 struct lmv_obd *lmv = &obd->u.lmv;
2129 struct obd_export *tgt_exp;
2133 rc = lmv_check_connect(obd);
2137 tgt_exp = lmv_find_export(lmv, fid);
2138 if (IS_ERR(tgt_exp))
2139 RETURN(PTR_ERR(tgt_exp));
2141 rc = md_sync(tgt_exp, fid, oc, request);
2145 /* main purpose of LMV blocking ast is to remove split directory LMV
2146 * presentation object (struct lmv_obj) attached to the lock being revoked. */
2147 int lmv_blocking_ast(struct ldlm_lock *lock,
2148 struct ldlm_lock_desc *desc,
2149 void *data, int flag)
2151 struct lustre_handle lockh;
2152 struct lmv_obj *obj;
2157 case LDLM_CB_BLOCKING:
2158 ldlm_lock2handle(lock, &lockh);
2159 rc = ldlm_cli_cancel(&lockh);
2161 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2165 case LDLM_CB_CANCELING:
2166 /* time to drop cached attrs for dirobj */
2167 obj = lock->l_ast_data;
2169 CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2170 ", master "DFID"\n",
2171 lock->l_resource->lr_name.name[3] == 1 ?
2172 "LOOKUP" : "UPDATE",
2173 lock->l_resource->lr_name.name[0],
2174 lock->l_resource->lr_name.name[1],
2175 PFID(&obj->lo_fid));
2185 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2189 val = le64_to_cpu(*hash);
2191 val += MAX_HASH_SIZE;
2192 if (val != DIR_END_OFF)
2193 *hash = cpu_to_le64(val - hash_adj);
2196 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2199 struct obd_import *imp;
2202 * XXX Hack: to get nid we assume that underlying obd device is mdc.
2204 imp = class_exp2cliimp(exp);
2205 id = imp->imp_connection->c_self + fid_flatten(fid);
2207 CDEBUG(D_INFO, "node rank: %llx "DFID" %llx %llx\n",
2208 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2210 return id ^ (id >> 32);
2213 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2214 struct obd_capa *oc, __u64 offset64, struct page *page,
2215 struct ptlrpc_request **request)
2217 struct obd_device *obd = exp->exp_obd;
2218 struct lmv_obd *lmv = &obd->u.lmv;
2219 struct obd_export *tgt_exp;
2220 struct lu_fid rid = *fid;
2221 struct lmv_obj *obj;
2235 rc = lmv_check_connect(obd);
2239 CDEBUG(D_INFO, "READPAGE at %llx from "DFID"\n", offset, PFID(&rid));
2241 obj = lmv_obj_grab(obd, fid);
2245 * This case handle directory lookup in clustered metadata case (i.e.
2246 * split directory is located on multiple md servers.)
2247 * each server keeps directory entries for certain range of hashes.
2248 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2249 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2250 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2252 * readdir can simply start reading entries from 0 - N server in
2253 * order but that will not scale well as all client will request dir in
2254 * to server in same order.
2255 * Following algorithm does optimization:
2256 * Instead of doing readdir in 1, 2, ...., N order, client with a
2257 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2258 * (every client has rank R)
2259 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2260 * since client ask dir from MDS{R} client has pages with offsets
2261 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2262 * on hash values that we get.
2265 struct lmv_inode *loi;
2269 nr = obj->lo_objcount;
2271 seg_size = MAX_HASH_SIZE;
2272 do_div(seg_size, nr);
2273 loi = obj->lo_inodes;
2274 rank = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2277 do_div(tgt_tmp, seg_size);
2278 tgt0 = do_div(tgt_tmp, nr);
2279 tgt = (tgt0 + rank) % nr;
2285 * Last segment has unusual length due to division
2288 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2292 hash_adj += rank * seg_size;
2294 CDEBUG(D_INFO, "hash_adj: %x %llx %llx/%x -> %llx/%x\n",
2295 rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2297 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2298 rid = obj->lo_inodes[tgt].li_fid;
2299 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2301 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2302 PFID(&rid), (unsigned long)offset, tgt);
2304 tgt_exp = lmv_find_export(lmv, &rid);
2306 if (IS_ERR(tgt_exp))
2307 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2309 rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2313 struct lu_dirpage *dp;
2314 struct lu_dirent *ent;
2316 dp = cfs_kmap(page);
2318 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2319 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2320 LASSERT(cpu_to_le32(dp->ldp_hash_start) <= offset64);
2322 for (ent = lu_dirent_start(dp); ent != NULL;
2323 ent = lu_dirent_next(ent))
2324 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2326 if (tgt0 != nr - 1) {
2329 end = le32_to_cpu(dp->ldp_hash_end);
2330 if (end == DIR_END_OFF) {
2331 dp->ldp_hash_end = cpu_to_le32(seg_size *
2333 CDEBUG(D_INFO, ""DFID" reset end %llx tgt %d\n",
2335 le64_to_cpu(dp->ldp_hash_end), tgt);
2341 * Here we could remove "." and ".." from all pages which at not from
2342 * master. But MDS has only "." and ".." for master dir.
2347 lmv_obj_unlock(obj);
2353 static int lmv_unlink_slaves(struct obd_export *exp,
2354 struct md_op_data *op_data,
2355 struct ptlrpc_request **req)
2357 struct obd_device *obd = exp->exp_obd;
2358 struct lmv_obd *lmv = &obd->u.lmv;
2359 struct lmv_stripe_md *mea = op_data->op_mea1;
2360 struct md_op_data *op_data2;
2361 struct obd_export *tgt_exp;
2365 OBD_ALLOC_PTR(op_data2);
2366 if (op_data2 == NULL)
2369 op_data2->op_mode = S_IFDIR;
2370 op_data2->op_fsuid = current->fsuid;
2371 op_data2->op_fsgid = current->fsgid;
2372 op_data2->op_bias = 0;
2374 LASSERT(mea != NULL);
2375 for (i = 0; i < mea->mea_count; i++) {
2376 memset(op_data2, 0, sizeof(*op_data2));
2377 op_data2->op_fid1 = mea->mea_ids[i];
2378 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2379 if (IS_ERR(tgt_exp))
2380 GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2382 if (tgt_exp == NULL)
2385 rc = md_unlink(tgt_exp, op_data2, req);
2387 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2388 PFID(&mea->mea_ids[i]), rc);
2391 ptlrpc_req_finished(*req);
2395 GOTO(out_free_op_data2, rc);
2400 OBD_FREE_PTR(op_data2);
2404 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2405 struct ptlrpc_request **request)
2407 struct obd_device *obd = exp->exp_obd;
2408 struct lmv_obd *lmv = &obd->u.lmv;
2409 struct obd_export *tgt_exp = NULL;
2410 struct lmv_obj *obj;
2414 rc = lmv_check_connect(obd);
2418 if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2419 /* mds asks to remove slave objects */
2420 rc = lmv_unlink_slaves(exp, op_data, request);
2427 if (op_data->op_namelen != 0) {
2430 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2432 mea_idx = raw_name2idx(obj->lo_hashtype,
2435 op_data->op_namelen);
2436 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2437 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2438 tgt_exp = lmv_get_export(lmv,
2439 obj->lo_inodes[mea_idx].li_mds);
2441 CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2442 op_data->op_namelen, op_data->op_name,
2443 PFID(&op_data->op_fid1), mea_idx);
2446 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2447 PFID(&op_data->op_fid1));
2449 if (tgt_exp == NULL) {
2450 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2451 if (IS_ERR(tgt_exp))
2452 RETURN(PTR_ERR(tgt_exp));
2453 op_data->op_bias |= MDS_CHECK_SPLIT;
2456 op_data->op_fsuid = current->fsuid;
2457 op_data->op_fsgid = current->fsgid;
2458 op_data->op_cap = current->cap_effective;
2460 /* If child's fid is given, cancel unused locks for it if it is from
2461 * another export than parent. */
2462 if (op_data->op_namelen) {
2463 /* LOOKUP lock for child (fid3) should also be cancelled on
2464 * parent tgt_exp in mdc_unlink(). */
2465 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2467 /* XXX: the case when child is a striped dir is not supported.
2468 * Only the master stripe has all locks cancelled early. */
2469 /* Cancel FULL locks on child (fid3). */
2470 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2471 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2474 rc = md_unlink(tgt_exp, op_data, request);
2475 if (rc == -ERESTART) {
2476 LASSERT(*request != NULL);
2477 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2478 "Got -ERESTART during unlink!\n");
2479 ptlrpc_req_finished(*request);
2483 * Directory got split. Time to update local object and repeat
2484 * the request with proper MDS.
2486 rc = lmv_handle_split(exp, &op_data->op_fid1);
2493 static int lmv_llog_init(struct obd_device *obd, int group,
2494 struct obd_device *tgt, int count,
2495 struct llog_catid *logid, struct obd_uuid *uuid)
2498 struct llog_ctxt *ctxt;
2502 LASSERT(group == OBD_LLOG_GROUP);
2503 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2506 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2507 llog_initiator_connect(ctxt, tgt);
2508 llog_ctxt_put(ctxt);
2516 static int lmv_llog_finish(struct obd_device *obd, int count)
2518 struct llog_ctxt *ctxt;
2522 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2524 rc = llog_cleanup(ctxt);
2529 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2534 case OBD_CLEANUP_EARLY:
2535 /* XXX: here should be calling obd_precleanup() down to
2538 case OBD_CLEANUP_SELF_EXP:
2539 rc = obd_llog_finish(obd, 0);
2541 CERROR("failed to cleanup llogging subsystems\n");
2549 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2550 void *key, __u32 *vallen, void *val)
2552 struct obd_device *obd;
2553 struct lmv_obd *lmv;
2557 obd = class_exp2obd(exp);
2559 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2560 exp->exp_handle.h_cookie);
2565 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2566 struct lmv_tgt_desc *tgts;
2569 rc = lmv_check_connect(obd);
2573 LASSERT(*vallen == sizeof(__u32));
2574 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2577 /* all tgts should be connected when this get called. */
2578 if (!tgts || !tgts->ltd_exp) {
2579 CERROR("target not setup?\n");
2583 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2588 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2589 rc = lmv_check_connect(obd);
2593 /* forwarding this request to first MDS, it should know LOV
2595 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2597 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2598 exp->exp_connect_flags =
2599 ((struct obd_connect_data *)val)->ocd_connect_flags;
2604 CDEBUG(D_IOCTL, "invalid key\n");
2608 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2609 void *key, obd_count vallen, void *val,
2610 struct ptlrpc_request_set *set)
2612 struct lmv_tgt_desc *tgt;
2613 struct obd_device *obd;
2614 struct lmv_obd *lmv;
2618 obd = class_exp2obd(exp);
2620 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2621 exp->exp_handle.h_cookie);
2626 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2627 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2630 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2631 tgt = &lmv->tgts[i];
2636 err = obd_set_info_async(tgt->ltd_exp,
2637 keylen, key, vallen, val, set);
2648 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2649 struct lov_stripe_md *lsm)
2651 struct obd_device *obd = class_exp2obd(exp);
2652 struct lmv_obd *lmv = &obd->u.lmv;
2653 struct lmv_stripe_md *meap, *lsmp;
2657 mea_size = lmv_get_easize(lmv);
2661 if (*lmmp && !lsm) {
2662 OBD_FREE(*lmmp, mea_size);
2667 if (*lmmp == NULL) {
2668 OBD_ALLOC(*lmmp, mea_size);
2676 lsmp = (struct lmv_stripe_md *)lsm;
2677 meap = (struct lmv_stripe_md *)*lmmp;
2679 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2680 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2683 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2684 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2685 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2687 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2688 meap->mea_ids[i] = meap->mea_ids[i];
2689 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2695 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2696 struct lov_mds_md *lmm, int lmm_size)
2698 struct obd_device *obd = class_exp2obd(exp);
2699 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2700 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2701 struct lmv_obd *lmv = &obd->u.lmv;
2706 mea_size = lmv_get_easize(lmv);
2710 if (*lsmp != NULL && lmm == NULL) {
2711 OBD_FREE(*tmea, mea_size);
2716 LASSERT(mea_size == lmm_size);
2718 OBD_ALLOC(*tmea, mea_size);
2725 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2726 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2727 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2729 magic = le32_to_cpu(mea->mea_magic);
2731 /* old mea is not handled here */
2735 (*tmea)->mea_magic = magic;
2736 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2737 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2739 for (i = 0; i < (*tmea)->mea_count; i++) {
2740 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2741 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2746 static int lmv_cancel_unused(struct obd_export *exp,
2747 const struct lu_fid *fid,
2748 ldlm_policy_data_t *policy,
2749 ldlm_mode_t mode, int flags, void *opaque)
2751 struct obd_device *obd = exp->exp_obd;
2752 struct lmv_obd *lmv = &obd->u.lmv;
2756 LASSERT(fid != NULL);
2758 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2759 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2762 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2763 policy, mode, flags, opaque);
2770 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2772 struct obd_device *obd = exp->exp_obd;
2773 struct lmv_obd *lmv = &obd->u.lmv;
2776 RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2779 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2780 const struct lu_fid *fid, ldlm_type_t type,
2781 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2782 struct lustre_handle *lockh)
2784 struct obd_device *obd = exp->exp_obd;
2785 struct lmv_obd *lmv = &obd->u.lmv;
2790 CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2792 /* with CMD every object can have two locks in different namespaces:
2793 * lookup lock in space of mds storing direntry and update/open lock in
2794 * space of mds storing inode. Thus we check all targets, not only that
2795 * one fid was created in. */
2796 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2797 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2798 type, policy, mode, lockh);
2806 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2807 struct obd_export *dt_exp, struct obd_export *md_exp,
2808 struct lustre_md *md)
2810 struct obd_device *obd = exp->exp_obd;
2811 struct lmv_obd *lmv = &obd->u.lmv;
2815 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2819 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2821 struct obd_device *obd = exp->exp_obd;
2822 struct lmv_obd *lmv = &obd->u.lmv;
2826 obd_free_memmd(exp, (struct lov_stripe_md**)&md->mea);
2827 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2830 int lmv_set_open_replay_data(struct obd_export *exp,
2831 struct obd_client_handle *och,
2832 struct ptlrpc_request *open_req)
2834 struct obd_device *obd = exp->exp_obd;
2835 struct lmv_obd *lmv = &obd->u.lmv;
2836 struct obd_export *tgt_exp;
2840 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2841 if (IS_ERR(tgt_exp))
2842 RETURN(PTR_ERR(tgt_exp));
2844 RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2847 int lmv_clear_open_replay_data(struct obd_export *exp,
2848 struct obd_client_handle *och)
2850 struct obd_device *obd = exp->exp_obd;
2851 struct lmv_obd *lmv = &obd->u.lmv;
2852 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_clear_open_replay_data(tgt_exp, och));
2862 static int lmv_get_remote_perm(struct obd_export *exp,
2863 const struct lu_fid *fid,
2864 struct obd_capa *oc, __u32 suppgid,
2865 struct ptlrpc_request **request)
2867 struct obd_device *obd = exp->exp_obd;
2868 struct lmv_obd *lmv = &obd->u.lmv;
2869 struct obd_export *tgt_exp;
2874 rc = lmv_check_connect(obd);
2878 tgt_exp = lmv_find_export(lmv, fid);
2879 if (IS_ERR(tgt_exp))
2880 RETURN(PTR_ERR(tgt_exp));
2882 rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2887 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2890 struct obd_device *obd = exp->exp_obd;
2891 struct lmv_obd *lmv = &obd->u.lmv;
2892 struct obd_export *tgt_exp;
2896 rc = lmv_check_connect(obd);
2900 tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2901 if (IS_ERR(tgt_exp))
2902 RETURN(PTR_ERR(tgt_exp));
2904 rc = md_renew_capa(tgt_exp, oc, cb);
2908 struct obd_ops lmv_obd_ops = {
2909 .o_owner = THIS_MODULE,
2910 .o_setup = lmv_setup,
2911 .o_cleanup = lmv_cleanup,
2912 .o_precleanup = lmv_precleanup,
2913 .o_process_config = lmv_process_config,
2914 .o_connect = lmv_connect,
2915 .o_disconnect = lmv_disconnect,
2916 .o_statfs = lmv_statfs,
2917 .o_llog_init = lmv_llog_init,
2918 .o_llog_finish = lmv_llog_finish,
2919 .o_get_info = lmv_get_info,
2920 .o_set_info_async = lmv_set_info_async,
2921 .o_packmd = lmv_packmd,
2922 .o_unpackmd = lmv_unpackmd,
2923 .o_notify = lmv_notify,
2924 .o_iocontrol = lmv_iocontrol,
2925 .o_fid_delete = lmv_fid_delete
2928 struct md_ops lmv_md_ops = {
2929 .m_getstatus = lmv_getstatus,
2930 .m_change_cbdata = lmv_change_cbdata,
2931 .m_close = lmv_close,
2932 .m_create = lmv_create,
2933 .m_done_writing = lmv_done_writing,
2934 .m_enqueue = lmv_enqueue,
2935 .m_getattr = lmv_getattr,
2936 .m_getxattr = lmv_getxattr,
2937 .m_getattr_name = lmv_getattr_name,
2938 .m_intent_lock = lmv_intent_lock,
2940 .m_rename = lmv_rename,
2941 .m_setattr = lmv_setattr,
2942 .m_setxattr = lmv_setxattr,
2944 .m_readpage = lmv_readpage,
2945 .m_unlink = lmv_unlink,
2946 .m_init_ea_size = lmv_init_ea_size,
2947 .m_cancel_unused = lmv_cancel_unused,
2948 .m_set_lock_data = lmv_set_lock_data,
2949 .m_lock_match = lmv_lock_match,
2950 .m_get_lustre_md = lmv_get_lustre_md,
2951 .m_free_lustre_md = lmv_free_lustre_md,
2952 .m_set_open_replay_data = lmv_set_open_replay_data,
2953 .m_clear_open_replay_data = lmv_clear_open_replay_data,
2954 .m_get_remote_perm = lmv_get_remote_perm,
2955 .m_renew_capa = lmv_renew_capa
2958 int __init lmv_init(void)
2960 struct lprocfs_static_vars lvars;
2963 obj_cache = cfs_mem_cache_create("lmv_objects",
2964 sizeof(struct lmv_obj),
2967 CERROR("error allocating lmv objects cache\n");
2971 lprocfs_lmv_init_vars(&lvars);
2972 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2973 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2975 cfs_mem_cache_destroy(obj_cache);
2981 static void lmv_exit(void)
2985 class_unregister_type(LUSTRE_LMV_NAME);
2987 rc = cfs_mem_cache_destroy(obj_cache);
2989 "can't free lmv objects cache, %d object(s)"
2990 "still in use\n", atomic_read(&obj_cache_count));
2993 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2994 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2995 MODULE_LICENSE("GPL");
2997 module_init(lmv_init);
2998 module_exit(lmv_exit);