4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LMV
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
45 #include <asm/div64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
49 #include <liblustre.h>
52 #include <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lprocfs_status.h>
58 #include <lustre_lite.h>
59 #include <lustre_fid.h>
60 #include "lmv_internal.h"
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);
76 * -EINVAL : UUID can't be found in the LMV's target list
77 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
78 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
80 static int lmv_set_mdc_active(struct lmv_obd *lmv,
81 const struct obd_uuid *uuid,
84 struct lmv_tgt_desc *tgt = NULL;
85 struct obd_device *obd;
90 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
91 lmv, uuid->uuid, activate);
93 spin_lock(&lmv->lmv_lock);
94 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
96 if (tgt == NULL || tgt->ltd_exp == NULL)
99 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n", i,
100 tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
102 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
106 if (i == lmv->desc.ld_tgt_count)
107 GOTO(out_lmv_lock, rc = -EINVAL);
109 obd = class_exp2obd(tgt->ltd_exp);
111 GOTO(out_lmv_lock, rc = -ENOTCONN);
113 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
114 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
115 obd->obd_type->typ_name, i);
116 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
118 if (tgt->ltd_active == activate) {
119 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
120 activate ? "" : "in");
121 GOTO(out_lmv_lock, rc);
124 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
125 activate ? "" : "in");
126 lmv_activate_target(lmv, tgt, activate);
130 spin_unlock(&lmv->lmv_lock);
134 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
136 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
138 return obd_get_uuid(lmv->tgts[0]->ltd_exp);
141 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
142 enum obd_notify_event ev, void *data)
144 struct obd_connect_data *conn_data;
145 struct lmv_obd *lmv = &obd->u.lmv;
146 struct obd_uuid *uuid;
150 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
151 CERROR("unexpected notification of %s %s!\n",
152 watched->obd_type->typ_name,
157 uuid = &watched->u.cli.cl_target_uuid;
158 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
160 * Set MDC as active before notifying the observer, so the
161 * observer can use the MDC normally.
163 rc = lmv_set_mdc_active(lmv, uuid,
164 ev == OBD_NOTIFY_ACTIVE);
166 CERROR("%sactivation of %s failed: %d\n",
167 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
171 } else if (ev == OBD_NOTIFY_OCD) {
172 conn_data = &watched->u.cli.cl_import->imp_connect_data;
174 * XXX: Make sure that ocd_connect_flags from all targets are
175 * the same. Otherwise one of MDTs runs wrong version or
176 * something like this. --umka
178 obd->obd_self_export->exp_connect_data = *conn_data;
181 else if (ev == OBD_NOTIFY_DISCON) {
183 * For disconnect event, flush fld cache for failout MDS case.
185 fld_client_flush(&lmv->lmv_fld);
189 * Pass the notification up the chain.
191 if (obd->obd_observer)
192 rc = obd_notify(obd->obd_observer, watched, ev, data);
198 * This is fake connect function. Its purpose is to initialize lmv and say
199 * caller that everything is okay. Real connection will be performed later.
201 static int lmv_connect(const struct lu_env *env,
202 struct obd_export **exp, struct obd_device *obd,
203 struct obd_uuid *cluuid, struct obd_connect_data *data,
207 struct proc_dir_entry *lmv_proc_dir;
209 struct lmv_obd *lmv = &obd->u.lmv;
210 struct lustre_handle conn = { 0 };
215 * We don't want to actually do the underlying connections more than
216 * once, so keep track.
219 if (lmv->refcount > 1) {
224 rc = class_connect(&conn, obd, cluuid);
226 CERROR("class_connection() returned %d\n", rc);
230 *exp = class_conn2export(&conn);
231 class_export_get(*exp);
235 lmv->cluuid = *cluuid;
238 lmv->conn_data = *data;
241 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
243 if (IS_ERR(lmv_proc_dir)) {
244 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
245 obd->obd_type->typ_name, obd->obd_name);
251 * All real clients should perform actual connection right away, because
252 * it is possible, that LMV will not have opportunity to connect targets
253 * and MDC stuff will be called directly, for instance while reading
254 * ../mdc/../kbytesfree procfs file, etc.
256 if (data != NULL && (data->ocd_connect_flags & OBD_CONNECT_REAL))
257 rc = lmv_check_connect(obd);
262 lprocfs_remove(&lmv_proc_dir);
269 static void lmv_set_timeouts(struct obd_device *obd)
271 struct lmv_tgt_desc *tgt;
276 if (lmv->server_timeout == 0)
279 if (lmv->connected == 0)
282 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
284 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
287 obd_set_info_async(NULL, tgt->ltd_exp, sizeof(KEY_INTERMDS),
288 KEY_INTERMDS, 0, NULL, NULL);
292 static int lmv_init_ea_size(struct obd_export *exp, int easize,
293 int def_easize, int cookiesize)
295 struct obd_device *obd = exp->exp_obd;
296 struct lmv_obd *lmv = &obd->u.lmv;
302 if (lmv->max_easize < easize) {
303 lmv->max_easize = easize;
306 if (lmv->max_def_easize < def_easize) {
307 lmv->max_def_easize = def_easize;
310 if (lmv->max_cookiesize < cookiesize) {
311 lmv->max_cookiesize = cookiesize;
317 if (lmv->connected == 0)
320 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
321 if (lmv->tgts[i] == NULL ||
322 lmv->tgts[i]->ltd_exp == NULL ||
323 lmv->tgts[i]->ltd_active == 0) {
324 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
328 rc = md_init_ea_size(lmv->tgts[i]->ltd_exp, easize, def_easize,
331 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
332 " rc = %d.\n", obd->obd_name, i, rc);
339 #define MAX_STRING_SIZE 128
341 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
344 struct proc_dir_entry *lmv_proc_dir;
346 struct lmv_obd *lmv = &obd->u.lmv;
347 struct obd_uuid *cluuid = &lmv->cluuid;
348 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
349 struct obd_device *mdc_obd;
350 struct obd_export *mdc_exp;
351 struct lu_fld_target target;
355 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
358 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
362 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
363 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
364 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
367 if (!mdc_obd->obd_set_up) {
368 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
372 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
373 &lmv->conn_data, NULL);
375 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
380 * Init fid sequence client for this mdc and add new fld target.
382 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
386 target.ft_srv = NULL;
387 target.ft_exp = mdc_exp;
388 target.ft_idx = tgt->ltd_idx;
390 fld_client_add_target(&lmv->lmv_fld, &target);
392 rc = obd_register_observer(mdc_obd, obd);
394 obd_disconnect(mdc_exp);
395 CERROR("target %s register_observer error %d\n",
396 tgt->ltd_uuid.uuid, rc);
400 if (obd->obd_observer) {
402 * Tell the observer about the new target.
404 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
406 (void *)(tgt - lmv->tgts[0]));
408 obd_disconnect(mdc_exp);
414 tgt->ltd_exp = mdc_exp;
415 lmv->desc.ld_active_tgt_count++;
417 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
418 lmv->max_def_easize, lmv->max_cookiesize);
420 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
421 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
422 cfs_atomic_read(&obd->obd_refcount));
425 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
427 struct proc_dir_entry *mdc_symlink;
429 LASSERT(mdc_obd->obd_type != NULL);
430 LASSERT(mdc_obd->obd_type->typ_name != NULL);
431 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
434 mdc_obd->obd_type->typ_name,
436 if (mdc_symlink == NULL) {
437 CERROR("Could not register LMV target "
438 "/proc/fs/lustre/%s/%s/target_obds/%s.",
439 obd->obd_type->typ_name, obd->obd_name,
441 lprocfs_remove(&lmv_proc_dir);
449 static void lmv_del_target(struct lmv_obd *lmv, int index)
451 if (lmv->tgts[index] == NULL)
454 OBD_FREE_PTR(lmv->tgts[index]);
455 lmv->tgts[index] = NULL;
459 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
460 __u32 index, int gen)
462 struct lmv_obd *lmv = &obd->u.lmv;
463 struct lmv_tgt_desc *tgt;
467 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
471 if (lmv->desc.ld_tgt_count == 0) {
472 struct obd_device *mdc_obd;
474 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
477 lmv_init_unlock(lmv);
478 CERROR("%s: Target %s not attached: rc = %d\n",
479 obd->obd_name, uuidp->uuid, -EINVAL);
484 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
485 tgt = lmv->tgts[index];
486 CERROR("%s: UUID %s already assigned at LOV target index %d:"
487 " rc = %d\n", obd->obd_name,
488 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
489 lmv_init_unlock(lmv);
493 if (index >= lmv->tgts_size) {
494 /* We need to reallocate the lmv target array. */
495 struct lmv_tgt_desc **newtgts, **old = NULL;
499 while (newsize < index + 1)
500 newsize = newsize << 1;
501 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
502 if (newtgts == NULL) {
503 lmv_init_unlock(lmv);
507 if (lmv->tgts_size) {
508 memcpy(newtgts, lmv->tgts,
509 sizeof(*newtgts) * lmv->tgts_size);
511 oldsize = lmv->tgts_size;
515 lmv->tgts_size = newsize;
518 OBD_FREE(old, sizeof(*old) * oldsize);
520 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
526 lmv_init_unlock(lmv);
530 mutex_init(&tgt->ltd_fid_mutex);
531 tgt->ltd_idx = index;
532 tgt->ltd_uuid = *uuidp;
534 lmv->tgts[index] = tgt;
535 if (index >= lmv->desc.ld_tgt_count)
536 lmv->desc.ld_tgt_count = index + 1;
538 if (lmv->connected) {
539 rc = lmv_connect_mdc(obd, tgt);
541 spin_lock(&lmv->lmv_lock);
542 lmv->desc.ld_tgt_count--;
543 memset(tgt, 0, sizeof(*tgt));
544 spin_unlock(&lmv->lmv_lock);
546 int easize = sizeof(struct lmv_stripe_md) +
547 lmv->desc.ld_tgt_count *
548 sizeof(struct lu_fid);
549 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
553 lmv_init_unlock(lmv);
557 int lmv_check_connect(struct obd_device *obd)
559 struct lmv_obd *lmv = &obd->u.lmv;
560 struct lmv_tgt_desc *tgt;
570 if (lmv->connected) {
571 lmv_init_unlock(lmv);
575 if (lmv->desc.ld_tgt_count == 0) {
576 lmv_init_unlock(lmv);
577 CERROR("%s: no targets configured.\n", obd->obd_name);
581 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
582 lmv->cluuid.uuid, obd->obd_name);
584 LASSERT(lmv->tgts != NULL);
586 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
590 rc = lmv_connect_mdc(obd, tgt);
595 lmv_set_timeouts(obd);
596 class_export_put(lmv->exp);
598 easize = lmv_get_easize(lmv);
599 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
600 lmv_init_unlock(lmv);
611 --lmv->desc.ld_active_tgt_count;
612 rc2 = obd_disconnect(tgt->ltd_exp);
614 CERROR("LMV target %s disconnect on "
615 "MDC idx %d: error %d\n",
616 tgt->ltd_uuid.uuid, i, rc2);
620 class_disconnect(lmv->exp);
621 lmv_init_unlock(lmv);
625 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
628 struct proc_dir_entry *lmv_proc_dir;
630 struct lmv_obd *lmv = &obd->u.lmv;
631 struct obd_device *mdc_obd;
635 LASSERT(tgt != NULL);
636 LASSERT(obd != NULL);
638 mdc_obd = class_exp2obd(tgt->ltd_exp);
641 mdc_obd->obd_force = obd->obd_force;
642 mdc_obd->obd_fail = obd->obd_fail;
643 mdc_obd->obd_no_recov = obd->obd_no_recov;
647 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
649 struct proc_dir_entry *mdc_symlink;
651 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
653 lprocfs_remove(&mdc_symlink);
655 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
656 obd->obd_type->typ_name, obd->obd_name,
661 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
663 CERROR("Can't finanize fids factory\n");
665 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
666 tgt->ltd_exp->exp_obd->obd_name,
667 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
669 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
670 rc = obd_disconnect(tgt->ltd_exp);
672 if (tgt->ltd_active) {
673 CERROR("Target %s disconnect error %d\n",
674 tgt->ltd_uuid.uuid, rc);
678 lmv_activate_target(lmv, tgt, 0);
683 static int lmv_disconnect(struct obd_export *exp)
685 struct obd_device *obd = class_exp2obd(exp);
687 struct proc_dir_entry *lmv_proc_dir;
689 struct lmv_obd *lmv = &obd->u.lmv;
698 * Only disconnect the underlying layers on the final disconnect.
701 if (lmv->refcount != 0)
704 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
705 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
708 lmv_disconnect_mdc(obd, lmv->tgts[i]);
712 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
714 lprocfs_remove(&lmv_proc_dir);
716 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
717 obd->obd_type->typ_name, obd->obd_name);
723 * This is the case when no real connection is established by
724 * lmv_check_connect().
727 class_export_put(exp);
728 rc = class_disconnect(exp);
729 if (lmv->refcount == 0)
734 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
736 struct obd_device *obddev = class_exp2obd(exp);
737 struct lmv_obd *lmv = &obddev->u.lmv;
738 struct getinfo_fid2path *gf;
739 struct lmv_tgt_desc *tgt;
740 struct getinfo_fid2path *remote_gf = NULL;
741 int remote_gf_size = 0;
744 gf = (struct getinfo_fid2path *)karg;
745 tgt = lmv_find_target(lmv, &gf->gf_fid);
747 RETURN(PTR_ERR(tgt));
750 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
751 if (rc != 0 && rc != -EREMOTE)
752 GOTO(out_fid2path, rc);
754 /* If remote_gf != NULL, it means just building the
755 * path on the remote MDT, copy this path segement to gf */
756 if (remote_gf != NULL) {
757 struct getinfo_fid2path *ori_gf;
760 ori_gf = (struct getinfo_fid2path *)karg;
761 if (strlen(ori_gf->gf_path) +
762 strlen(gf->gf_path) > ori_gf->gf_pathlen)
763 GOTO(out_fid2path, rc = -EOVERFLOW);
765 ptr = ori_gf->gf_path;
767 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
768 strlen(ori_gf->gf_path));
770 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
771 ptr += strlen(gf->gf_path);
775 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: "LPU64" ln: %u\n",
776 tgt->ltd_exp->exp_obd->obd_name,
777 gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
781 GOTO(out_fid2path, rc);
783 /* sigh, has to go to another MDT to do path building further */
784 if (remote_gf == NULL) {
785 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
786 OBD_ALLOC(remote_gf, remote_gf_size);
787 if (remote_gf == NULL)
788 GOTO(out_fid2path, rc = -ENOMEM);
789 remote_gf->gf_pathlen = PATH_MAX;
792 if (!fid_is_sane(&gf->gf_fid)) {
793 CERROR("%s: invalid FID "DFID": rc = %d\n",
794 tgt->ltd_exp->exp_obd->obd_name,
795 PFID(&gf->gf_fid), -EINVAL);
796 GOTO(out_fid2path, rc = -EINVAL);
799 tgt = lmv_find_target(lmv, &gf->gf_fid);
801 GOTO(out_fid2path, rc = -EINVAL);
803 remote_gf->gf_fid = gf->gf_fid;
804 remote_gf->gf_recno = -1;
805 remote_gf->gf_linkno = -1;
806 memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
808 goto repeat_fid2path;
811 if (remote_gf != NULL)
812 OBD_FREE(remote_gf, remote_gf_size);
816 static int lmv_hsm_req_count(struct lmv_obd *lmv,
817 const struct hsm_user_request *hur,
818 const struct lmv_tgt_desc *tgt_mds)
822 struct lmv_tgt_desc *curr_tgt;
824 /* count how many requests must be sent to the given target */
825 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
826 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
827 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
833 static void lmv_hsm_req_build(struct lmv_obd *lmv,
834 struct hsm_user_request *hur_in,
835 const struct lmv_tgt_desc *tgt_mds,
836 struct hsm_user_request *hur_out)
839 struct lmv_tgt_desc *curr_tgt;
841 /* build the hsm_user_request for the given target */
842 hur_out->hur_request = hur_in->hur_request;
844 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
845 curr_tgt = lmv_find_target(lmv,
846 &hur_in->hur_user_item[i].hui_fid);
847 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
848 hur_out->hur_user_item[nr_out] =
849 hur_in->hur_user_item[i];
853 hur_out->hur_request.hr_itemcount = nr_out;
854 memcpy(hur_data(hur_out), hur_data(hur_in),
855 hur_in->hur_request.hr_data_len);
858 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
859 struct lustre_kernelcomm *lk, void *uarg)
865 /* unregister request (call from llapi_hsm_copytool_fini) */
866 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
867 /* best effort: try to clean as much as possible
868 * (continue on error) */
869 obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len, lk, uarg);
872 /* Whatever the result, remove copytool from kuc groups.
873 * Unreached coordinators will get EPIPE on next requests
874 * and will unregister automatically.
876 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
880 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
881 struct lustre_kernelcomm *lk, void *uarg)
886 bool any_set = false;
889 /* All or nothing: try to register to all MDS.
890 * In case of failure, unregister from previous MDS,
891 * except if it because of inactive target. */
892 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
893 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
896 if (lmv->tgts[i]->ltd_active) {
897 /* permanent error */
898 CERROR("%s: iocontrol MDC %s on MDT"
899 " idx %d cmd %x: err = %d\n",
900 class_exp2obd(lmv->exp)->obd_name,
901 lmv->tgts[i]->ltd_uuid.uuid,
904 lk->lk_flags |= LK_FLG_STOP;
905 /* unregister from previous MDS */
906 for (j = 0; j < i; j++)
908 lmv->tgts[j]->ltd_exp,
912 /* else: transient error.
913 * kuc will register to the missing MDT
921 /* no registration done: return error */
924 /* at least one registration done, with no failure */
925 filp = fget(lk->lk_wfd);
929 rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, lk->lk_data);
930 if (rc != 0 && filp != NULL)
938 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
939 int len, void *karg, void *uarg)
941 struct obd_device *obddev = class_exp2obd(exp);
942 struct lmv_obd *lmv = &obddev->u.lmv;
946 __u32 count = lmv->desc.ld_tgt_count;
953 case IOC_OBD_STATFS: {
954 struct obd_ioctl_data *data = karg;
955 struct obd_device *mdc_obd;
956 struct obd_statfs stat_buf = {0};
959 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
960 if ((index >= count))
963 if (lmv->tgts[index] == NULL ||
964 lmv->tgts[index]->ltd_active == 0)
967 mdc_obd = class_exp2obd(lmv->tgts[index]->ltd_exp);
972 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
973 min((int) data->ioc_plen2,
974 (int) sizeof(struct obd_uuid))))
977 rc = obd_statfs(NULL, lmv->tgts[index]->ltd_exp, &stat_buf,
978 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
982 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
983 min((int) data->ioc_plen1,
984 (int) sizeof(stat_buf))))
988 case OBD_IOC_QUOTACTL: {
989 struct if_quotactl *qctl = karg;
990 struct lmv_tgt_desc *tgt = NULL;
991 struct obd_quotactl *oqctl;
993 if (qctl->qc_valid == QC_MDTIDX) {
994 if (count <= qctl->qc_idx)
997 tgt = lmv->tgts[qctl->qc_idx];
998 if (tgt == NULL || tgt->ltd_exp == NULL)
1000 } else if (qctl->qc_valid == QC_UUID) {
1001 for (i = 0; i < count; i++) {
1005 if (!obd_uuid_equals(&tgt->ltd_uuid,
1009 if (tgt->ltd_exp == NULL)
1021 LASSERT(tgt && tgt->ltd_exp);
1022 OBD_ALLOC_PTR(oqctl);
1026 QCTL_COPY(oqctl, qctl);
1027 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1029 QCTL_COPY(qctl, oqctl);
1030 qctl->qc_valid = QC_MDTIDX;
1031 qctl->obd_uuid = tgt->ltd_uuid;
1033 OBD_FREE_PTR(oqctl);
1036 case OBD_IOC_CHANGELOG_SEND:
1037 case OBD_IOC_CHANGELOG_CLEAR: {
1038 struct ioc_changelog *icc = karg;
1040 if (icc->icc_mdtindex >= count)
1043 if (lmv->tgts[icc->icc_mdtindex] == NULL ||
1044 lmv->tgts[icc->icc_mdtindex]->ltd_exp == NULL ||
1045 lmv->tgts[icc->icc_mdtindex]->ltd_active == 0)
1047 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex]->ltd_exp,
1048 sizeof(*icc), icc, NULL);
1051 case LL_IOC_GET_CONNECT_FLAGS: {
1052 if (lmv->tgts[0] == NULL)
1054 rc = obd_iocontrol(cmd, lmv->tgts[0]->ltd_exp, len, karg, uarg);
1057 case OBD_IOC_FID2PATH: {
1058 rc = lmv_fid2path(exp, len, karg, uarg);
1061 case LL_IOC_HSM_STATE_GET:
1062 case LL_IOC_HSM_STATE_SET:
1063 case LL_IOC_HSM_ACTION: {
1064 struct md_op_data *op_data = karg;
1065 struct lmv_tgt_desc *tgt;
1067 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1069 RETURN(PTR_ERR(tgt));
1071 if (tgt->ltd_exp == NULL)
1074 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1077 case LL_IOC_HSM_PROGRESS: {
1078 const struct hsm_progress_kernel *hpk = karg;
1079 struct lmv_tgt_desc *tgt;
1081 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1083 RETURN(PTR_ERR(tgt));
1084 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1087 case LL_IOC_HSM_REQUEST: {
1088 struct hsm_user_request *hur = karg;
1089 struct lmv_tgt_desc *tgt;
1090 unsigned int reqcount = hur->hur_request.hr_itemcount;
1095 /* if the request is about a single fid
1096 * or if there is a single MDS, no need to split
1098 if (reqcount == 1 || count == 1) {
1099 tgt = lmv_find_target(lmv,
1100 &hur->hur_user_item[0].hui_fid);
1102 RETURN(PTR_ERR(tgt));
1103 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1105 /* split fid list to their respective MDS */
1106 for (i = 0; i < count; i++) {
1107 unsigned int nr, reqlen;
1109 struct hsm_user_request *req;
1111 nr = lmv_hsm_req_count(lmv, hur, lmv->tgts[i]);
1112 if (nr == 0) /* nothing for this MDS */
1115 /* build a request with fids for this MDS */
1116 reqlen = offsetof(typeof(*hur),
1118 + hur->hur_request.hr_data_len;
1119 OBD_ALLOC_LARGE(req, reqlen);
1123 lmv_hsm_req_build(lmv, hur, lmv->tgts[i], req);
1125 rc1 = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
1127 if (rc1 != 0 && rc == 0)
1129 OBD_FREE_LARGE(req, reqlen);
1134 case LL_IOC_LOV_SWAP_LAYOUTS: {
1135 struct md_op_data *op_data = karg;
1136 struct lmv_tgt_desc *tgt1, *tgt2;
1138 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1140 RETURN(PTR_ERR(tgt1));
1142 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1144 RETURN(PTR_ERR(tgt2));
1146 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1149 /* only files on same MDT can have their layouts swapped */
1150 if (tgt1->ltd_idx != tgt2->ltd_idx)
1153 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1156 case LL_IOC_HSM_CT_START: {
1157 struct lustre_kernelcomm *lk = karg;
1158 if (lk->lk_flags & LK_FLG_STOP)
1159 rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1161 rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1165 for (i = 0; i < count; i++) {
1166 struct obd_device *mdc_obd;
1169 if (lmv->tgts[i] == NULL ||
1170 lmv->tgts[i]->ltd_exp == NULL)
1172 /* ll_umount_begin() sets force flag but for lmv, not
1173 * mdc. Let's pass it through */
1174 mdc_obd = class_exp2obd(lmv->tgts[i]->ltd_exp);
1175 mdc_obd->obd_force = obddev->obd_force;
1176 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len,
1178 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1181 if (lmv->tgts[i]->ltd_active) {
1182 CERROR("error: iocontrol MDC %s on MDT"
1183 " idx %d cmd %x: err = %d\n",
1184 lmv->tgts[i]->ltd_uuid.uuid,
1199 static int lmv_all_chars_policy(int count, const char *name,
1210 static int lmv_nid_policy(struct lmv_obd *lmv)
1212 struct obd_import *imp;
1216 * XXX: To get nid we assume that underlying obd device is mdc.
1218 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1219 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1220 return id % lmv->desc.ld_tgt_count;
1223 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1224 placement_policy_t placement)
1226 switch (placement) {
1227 case PLACEMENT_CHAR_POLICY:
1228 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1230 op_data->op_namelen);
1231 case PLACEMENT_NID_POLICY:
1232 return lmv_nid_policy(lmv);
1238 CERROR("Unsupported placement policy %x\n", placement);
1244 * This is _inode_ placement policy function (not name).
1246 static int lmv_placement_policy(struct obd_device *obd,
1247 struct md_op_data *op_data,
1250 struct lmv_obd *lmv = &obd->u.lmv;
1253 LASSERT(mds != NULL);
1255 if (lmv->desc.ld_tgt_count == 1) {
1261 * If stripe_offset is provided during setdirstripe
1262 * (setdirstripe -i xx), xx MDS will be choosen.
1264 if (op_data->op_cli_flags & CLI_SET_MEA) {
1265 struct lmv_user_md *lum;
1267 lum = (struct lmv_user_md *)op_data->op_data;
1268 if (lum->lum_type == LMV_STRIPE_TYPE &&
1269 lum->lum_stripe_offset != -1) {
1270 if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
1271 CERROR("%s: Stripe_offset %d > MDT count %d:"
1272 " rc = %d\n", obd->obd_name,
1273 lum->lum_stripe_offset,
1274 lmv->desc.ld_tgt_count, -ERANGE);
1277 *mds = lum->lum_stripe_offset;
1282 /* Allocate new fid on target according to operation type and parent
1284 *mds = op_data->op_mds;
1288 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
1291 struct lmv_tgt_desc *tgt;
1295 tgt = lmv_get_target(lmv, mds);
1297 RETURN(PTR_ERR(tgt));
1300 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1301 * on server that seq in new allocated fid is not yet known.
1303 mutex_lock(&tgt->ltd_fid_mutex);
1305 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1306 GOTO(out, rc = -ENODEV);
1309 * Asking underlaying tgt layer to allocate new fid.
1311 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1313 LASSERT(fid_is_sane(fid));
1319 mutex_unlock(&tgt->ltd_fid_mutex);
1323 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1324 struct md_op_data *op_data)
1326 struct obd_device *obd = class_exp2obd(exp);
1327 struct lmv_obd *lmv = &obd->u.lmv;
1332 LASSERT(op_data != NULL);
1333 LASSERT(fid != NULL);
1335 rc = lmv_placement_policy(obd, op_data, &mds);
1337 CERROR("Can't get target for allocating fid, "
1342 rc = __lmv_fid_alloc(lmv, fid, mds);
1344 CERROR("Can't alloc new fid, rc %d\n", rc);
1351 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1353 struct lmv_obd *lmv = &obd->u.lmv;
1354 struct lprocfs_static_vars lvars;
1355 struct lmv_desc *desc;
1359 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1360 CERROR("LMV setup requires a descriptor\n");
1364 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1365 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1366 CERROR("Lmv descriptor size wrong: %d > %d\n",
1367 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1371 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1372 if (lmv->tgts == NULL)
1374 lmv->tgts_size = 32;
1376 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1377 lmv->desc.ld_tgt_count = 0;
1378 lmv->desc.ld_active_tgt_count = 0;
1379 lmv->max_cookiesize = 0;
1380 lmv->max_def_easize = 0;
1381 lmv->max_easize = 0;
1382 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1384 spin_lock_init(&lmv->lmv_lock);
1385 mutex_init(&lmv->init_mutex);
1387 lprocfs_lmv_init_vars(&lvars);
1389 lprocfs_obd_setup(obd, lvars.obd_vars);
1390 lprocfs_alloc_md_stats(obd, 0);
1393 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1394 0444, &lmv_proc_target_fops, obd);
1396 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1400 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1401 LUSTRE_CLI_FLD_HASH_DHT);
1403 CERROR("Can't init FLD, err %d\n", rc);
1413 static int lmv_cleanup(struct obd_device *obd)
1415 struct lmv_obd *lmv = &obd->u.lmv;
1418 fld_client_fini(&lmv->lmv_fld);
1419 if (lmv->tgts != NULL) {
1421 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1422 if (lmv->tgts[i] == NULL)
1424 lmv_del_target(lmv, i);
1426 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1432 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1434 struct lustre_cfg *lcfg = buf;
1435 struct obd_uuid obd_uuid;
1441 switch (lcfg->lcfg_command) {
1443 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1444 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1445 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1446 GOTO(out, rc = -EINVAL);
1448 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1450 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1451 GOTO(out, rc = -EINVAL);
1452 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1453 GOTO(out, rc = -EINVAL);
1454 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1457 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1458 GOTO(out, rc = -EINVAL);
1464 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1465 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1467 struct obd_device *obd = class_exp2obd(exp);
1468 struct lmv_obd *lmv = &obd->u.lmv;
1469 struct obd_statfs *temp;
1474 rc = lmv_check_connect(obd);
1478 OBD_ALLOC(temp, sizeof(*temp));
1482 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1483 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1486 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1489 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1490 lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1492 GOTO(out_free_temp, rc);
1497 /* If the statfs is from mount, it will needs
1498 * retrieve necessary information from MDT0.
1499 * i.e. mount does not need the merged osfs
1501 * And also clients can be mounted as long as
1502 * MDT0 is in service*/
1503 if (flags & OBD_STATFS_FOR_MDT0)
1504 GOTO(out_free_temp, rc);
1506 osfs->os_bavail += temp->os_bavail;
1507 osfs->os_blocks += temp->os_blocks;
1508 osfs->os_ffree += temp->os_ffree;
1509 osfs->os_files += temp->os_files;
1515 OBD_FREE(temp, sizeof(*temp));
1519 static int lmv_getstatus(struct obd_export *exp,
1521 struct obd_capa **pc)
1523 struct obd_device *obd = exp->exp_obd;
1524 struct lmv_obd *lmv = &obd->u.lmv;
1528 rc = lmv_check_connect(obd);
1532 rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1536 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1537 struct obd_capa *oc, obd_valid valid, const char *name,
1538 const char *input, int input_size, int output_size,
1539 int flags, struct ptlrpc_request **request)
1541 struct obd_device *obd = exp->exp_obd;
1542 struct lmv_obd *lmv = &obd->u.lmv;
1543 struct lmv_tgt_desc *tgt;
1547 rc = lmv_check_connect(obd);
1551 tgt = lmv_find_target(lmv, fid);
1553 RETURN(PTR_ERR(tgt));
1555 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1556 input_size, output_size, flags, request);
1561 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1562 struct obd_capa *oc, obd_valid valid, const char *name,
1563 const char *input, int input_size, int output_size,
1564 int flags, __u32 suppgid,
1565 struct ptlrpc_request **request)
1567 struct obd_device *obd = exp->exp_obd;
1568 struct lmv_obd *lmv = &obd->u.lmv;
1569 struct lmv_tgt_desc *tgt;
1573 rc = lmv_check_connect(obd);
1577 tgt = lmv_find_target(lmv, fid);
1579 RETURN(PTR_ERR(tgt));
1581 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1582 input_size, output_size, flags, suppgid,
1588 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1589 struct ptlrpc_request **request)
1591 struct obd_device *obd = exp->exp_obd;
1592 struct lmv_obd *lmv = &obd->u.lmv;
1593 struct lmv_tgt_desc *tgt;
1597 rc = lmv_check_connect(obd);
1601 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1603 RETURN(PTR_ERR(tgt));
1605 if (op_data->op_flags & MF_GET_MDT_IDX) {
1606 op_data->op_mds = tgt->ltd_idx;
1610 rc = md_getattr(tgt->ltd_exp, op_data, request);
1615 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1617 struct obd_device *obd = exp->exp_obd;
1618 struct lmv_obd *lmv = &obd->u.lmv;
1623 rc = lmv_check_connect(obd);
1627 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1630 * With DNE every object can have two locks in different namespaces:
1631 * lookup lock in space of MDT storing direntry and update/open lock in
1632 * space of MDT storing inode.
1634 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1635 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1637 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1643 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1644 ldlm_iterator_t it, void *data)
1646 struct obd_device *obd = exp->exp_obd;
1647 struct lmv_obd *lmv = &obd->u.lmv;
1652 rc = lmv_check_connect(obd);
1656 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1659 * With DNE every object can have two locks in different namespaces:
1660 * lookup lock in space of MDT storing direntry and update/open lock in
1661 * space of MDT storing inode.
1663 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1664 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1666 rc = md_find_cbdata(lmv->tgts[i]->ltd_exp, fid, it, data);
1675 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1676 struct md_open_data *mod, struct ptlrpc_request **request)
1678 struct obd_device *obd = exp->exp_obd;
1679 struct lmv_obd *lmv = &obd->u.lmv;
1680 struct lmv_tgt_desc *tgt;
1684 rc = lmv_check_connect(obd);
1688 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1690 RETURN(PTR_ERR(tgt));
1692 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1693 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1698 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1701 struct lmv_tgt_desc *tgt;
1703 tgt = lmv_find_target(lmv, fid);
1707 op_data->op_mds = tgt->ltd_idx;
1712 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1713 const void *data, int datalen, int mode, __u32 uid,
1714 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1715 struct ptlrpc_request **request)
1717 struct obd_device *obd = exp->exp_obd;
1718 struct lmv_obd *lmv = &obd->u.lmv;
1719 struct lmv_tgt_desc *tgt;
1723 rc = lmv_check_connect(obd);
1727 if (!lmv->desc.ld_active_tgt_count)
1730 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1732 RETURN(PTR_ERR(tgt));
1734 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1738 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1739 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1742 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1743 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1744 cap_effective, rdev, request);
1747 if (*request == NULL)
1749 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1754 static int lmv_done_writing(struct obd_export *exp,
1755 struct md_op_data *op_data,
1756 struct md_open_data *mod)
1758 struct obd_device *obd = exp->exp_obd;
1759 struct lmv_obd *lmv = &obd->u.lmv;
1760 struct lmv_tgt_desc *tgt;
1764 rc = lmv_check_connect(obd);
1768 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1770 RETURN(PTR_ERR(tgt));
1772 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1777 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1778 struct lookup_intent *it, struct md_op_data *op_data,
1779 struct lustre_handle *lockh, void *lmm, int lmmsize,
1780 int extra_lock_flags)
1782 struct ptlrpc_request *req = it->d.lustre.it_data;
1783 struct obd_device *obd = exp->exp_obd;
1784 struct lmv_obd *lmv = &obd->u.lmv;
1785 struct lustre_handle plock;
1786 struct lmv_tgt_desc *tgt;
1787 struct md_op_data *rdata;
1789 struct mdt_body *body;
1794 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1795 LASSERT(body != NULL);
1797 if (!(body->valid & OBD_MD_MDS))
1800 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1801 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1804 * We got LOOKUP lock, but we really need attrs.
1806 pmode = it->d.lustre.it_lock_mode;
1807 LASSERT(pmode != 0);
1808 memcpy(&plock, lockh, sizeof(plock));
1809 it->d.lustre.it_lock_mode = 0;
1810 it->d.lustre.it_data = NULL;
1813 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1814 ptlrpc_req_finished(req);
1816 tgt = lmv_find_target(lmv, &fid1);
1818 GOTO(out, rc = PTR_ERR(tgt));
1820 OBD_ALLOC_PTR(rdata);
1822 GOTO(out, rc = -ENOMEM);
1824 rdata->op_fid1 = fid1;
1825 rdata->op_bias = MDS_CROSS_REF;
1827 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1828 lmm, lmmsize, NULL, extra_lock_flags);
1829 OBD_FREE_PTR(rdata);
1832 ldlm_lock_decref(&plock, pmode);
1837 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1838 struct lookup_intent *it, struct md_op_data *op_data,
1839 struct lustre_handle *lockh, void *lmm, int lmmsize,
1840 struct ptlrpc_request **req, __u64 extra_lock_flags)
1842 struct obd_device *obd = exp->exp_obd;
1843 struct lmv_obd *lmv = &obd->u.lmv;
1844 struct lmv_tgt_desc *tgt;
1848 rc = lmv_check_connect(obd);
1852 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1853 LL_IT2STR(it), PFID(&op_data->op_fid1));
1855 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1857 RETURN(PTR_ERR(tgt));
1859 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1860 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1862 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1863 lmm, lmmsize, req, extra_lock_flags);
1865 if (rc == 0 && it && it->it_op == IT_OPEN) {
1866 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1867 lmm, lmmsize, extra_lock_flags);
1873 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1874 struct ptlrpc_request **request)
1876 struct ptlrpc_request *req = NULL;
1877 struct obd_device *obd = exp->exp_obd;
1878 struct lmv_obd *lmv = &obd->u.lmv;
1879 struct lmv_tgt_desc *tgt;
1880 struct mdt_body *body;
1884 rc = lmv_check_connect(obd);
1888 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1890 RETURN(PTR_ERR(tgt));
1892 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1893 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1896 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1900 body = req_capsule_server_get(&(*request)->rq_pill,
1902 LASSERT(body != NULL);
1904 if (body->valid & OBD_MD_MDS) {
1905 struct lu_fid rid = body->fid1;
1906 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1909 tgt = lmv_find_target(lmv, &rid);
1911 ptlrpc_req_finished(*request);
1912 RETURN(PTR_ERR(tgt));
1915 op_data->op_fid1 = rid;
1916 op_data->op_valid |= OBD_MD_FLCROSSREF;
1917 op_data->op_namelen = 0;
1918 op_data->op_name = NULL;
1919 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1920 ptlrpc_req_finished(*request);
1927 #define md_op_data_fid(op_data, fl) \
1928 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1929 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1930 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1931 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1934 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1935 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1937 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1938 struct obd_device *obd = exp->exp_obd;
1939 struct lmv_obd *lmv = &obd->u.lmv;
1940 struct lmv_tgt_desc *tgt;
1941 ldlm_policy_data_t policy = {{0}};
1945 if (!fid_is_sane(fid))
1948 tgt = lmv_find_target(lmv, fid);
1950 RETURN(PTR_ERR(tgt));
1952 if (tgt->ltd_idx != op_tgt) {
1953 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1954 policy.l_inodebits.bits = bits;
1955 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1956 mode, LCF_ASYNC, NULL);
1959 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1961 op_data->op_flags |= flag;
1969 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1972 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1973 struct ptlrpc_request **request)
1975 struct obd_device *obd = exp->exp_obd;
1976 struct lmv_obd *lmv = &obd->u.lmv;
1977 struct lmv_tgt_desc *tgt;
1981 rc = lmv_check_connect(obd);
1985 LASSERT(op_data->op_namelen != 0);
1987 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1988 PFID(&op_data->op_fid2), op_data->op_namelen,
1989 op_data->op_name, PFID(&op_data->op_fid1));
1991 op_data->op_fsuid = current_fsuid();
1992 op_data->op_fsgid = current_fsgid();
1993 op_data->op_cap = cfs_curproc_cap_pack();
1994 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1996 RETURN(PTR_ERR(tgt));
1999 * Cancel UPDATE lock on child (fid1).
2001 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2002 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2003 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2007 rc = md_link(tgt->ltd_exp, op_data, request);
2012 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2013 const char *old, int oldlen, const char *new, int newlen,
2014 struct ptlrpc_request **request)
2016 struct obd_device *obd = exp->exp_obd;
2017 struct lmv_obd *lmv = &obd->u.lmv;
2018 struct lmv_tgt_desc *src_tgt;
2019 struct lmv_tgt_desc *tgt_tgt;
2023 LASSERT(oldlen != 0);
2025 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2026 oldlen, old, PFID(&op_data->op_fid1),
2027 newlen, new, PFID(&op_data->op_fid2));
2029 rc = lmv_check_connect(obd);
2033 op_data->op_fsuid = current_fsuid();
2034 op_data->op_fsgid = current_fsgid();
2035 op_data->op_cap = cfs_curproc_cap_pack();
2036 src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2037 if (IS_ERR(src_tgt))
2038 RETURN(PTR_ERR(src_tgt));
2040 tgt_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2041 if (IS_ERR(tgt_tgt))
2042 RETURN(PTR_ERR(tgt_tgt));
2044 * LOOKUP lock on src child (fid3) should also be cancelled for
2045 * src_tgt in mdc_rename.
2047 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2050 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2053 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2054 LCK_EX, MDS_INODELOCK_UPDATE,
2055 MF_MDC_CANCEL_FID2);
2058 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2061 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2062 LCK_EX, MDS_INODELOCK_LOOKUP,
2063 MF_MDC_CANCEL_FID4);
2067 * Cancel all the locks on tgt child (fid4).
2070 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2071 LCK_EX, MDS_INODELOCK_FULL,
2072 MF_MDC_CANCEL_FID4);
2075 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2076 new, newlen, request);
2080 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2081 void *ea, int ealen, void *ea2, int ea2len,
2082 struct ptlrpc_request **request,
2083 struct md_open_data **mod)
2085 struct obd_device *obd = exp->exp_obd;
2086 struct lmv_obd *lmv = &obd->u.lmv;
2087 struct lmv_tgt_desc *tgt;
2091 rc = lmv_check_connect(obd);
2095 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2096 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2098 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2099 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2101 RETURN(PTR_ERR(tgt));
2103 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2104 ea2len, request, mod);
2109 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2110 struct obd_capa *oc, struct ptlrpc_request **request)
2112 struct obd_device *obd = exp->exp_obd;
2113 struct lmv_obd *lmv = &obd->u.lmv;
2114 struct lmv_tgt_desc *tgt;
2118 rc = lmv_check_connect(obd);
2122 tgt = lmv_find_target(lmv, fid);
2124 RETURN(PTR_ERR(tgt));
2126 rc = md_fsync(tgt->ltd_exp, fid, oc, request);
2131 * Adjust a set of pages, each page containing an array of lu_dirpages,
2132 * so that each page can be used as a single logical lu_dirpage.
2134 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
2135 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
2136 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
2137 * value is used as a cookie to request the next lu_dirpage in a
2138 * directory listing that spans multiple pages (two in this example):
2141 * .|--------v------- -----.
2142 * |s|e|f|p|ent|ent| ... |ent|
2143 * '--|-------------- -----' Each CFS_PAGE contains a single
2144 * '------. lu_dirpage.
2145 * .---------v------- -----.
2146 * |s|e|f|p|ent| 0 | ... | 0 |
2147 * '----------------- -----'
2149 * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
2150 * larger than LU_PAGE_SIZE, a single host page may contain multiple
2151 * lu_dirpages. After reading the lu_dirpages from the MDS, the
2152 * ldp_hash_end of the first lu_dirpage refers to the one immediately
2153 * after it in the same CFS_PAGE (arrows simplified for brevity, but
2154 * in general e0==s1, e1==s2, etc.):
2156 * .-------------------- -----.
2157 * |s0|e0|f0|p|ent|ent| ... |ent|
2158 * |---v---------------- -----|
2159 * |s1|e1|f1|p|ent|ent| ... |ent|
2160 * |---v---------------- -----| Here, each CFS_PAGE contains
2161 * ... multiple lu_dirpages.
2162 * |---v---------------- -----|
2163 * |s'|e'|f'|p|ent|ent| ... |ent|
2164 * '---|---------------- -----'
2166 * .----------------------------.
2169 * This structure is transformed into a single logical lu_dirpage as follows:
2171 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
2172 * labeled 'next CFS_PAGE'.
2174 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
2175 * a hash collision with the next page exists.
2177 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
2178 * to the first entry of the next lu_dirpage.
2180 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
2181 static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
2185 for (i = 0; i < ncfspgs; i++) {
2186 struct lu_dirpage *dp = kmap(pages[i]);
2187 struct lu_dirpage *first = dp;
2188 struct lu_dirent *end_dirent = NULL;
2189 struct lu_dirent *ent;
2190 __u64 hash_end = dp->ldp_hash_end;
2191 __u32 flags = dp->ldp_flags;
2193 while (--nlupgs > 0) {
2194 ent = lu_dirent_start(dp);
2195 for (end_dirent = ent; ent != NULL;
2196 end_dirent = ent, ent = lu_dirent_next(ent));
2198 /* Advance dp to next lu_dirpage. */
2199 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2201 /* Check if we've reached the end of the CFS_PAGE. */
2202 if (!((unsigned long)dp & ~CFS_PAGE_MASK))
2205 /* Save the hash and flags of this lu_dirpage. */
2206 hash_end = dp->ldp_hash_end;
2207 flags = dp->ldp_flags;
2209 /* Check if lu_dirpage contains no entries. */
2213 /* Enlarge the end entry lde_reclen from 0 to
2214 * first entry of next lu_dirpage. */
2215 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
2216 end_dirent->lde_reclen =
2217 cpu_to_le16((char *)(dp->ldp_entries) -
2218 (char *)end_dirent);
2221 first->ldp_hash_end = hash_end;
2222 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2223 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2227 LASSERTF(nlupgs == 0, "left = %d", nlupgs);
2230 #define lmv_adjust_dirpages(pages, ncfspgs, nlupgs) do {} while (0)
2231 #endif /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
2233 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2234 struct page **pages, struct ptlrpc_request **request)
2236 struct obd_device *obd = exp->exp_obd;
2237 struct lmv_obd *lmv = &obd->u.lmv;
2238 __u64 offset = op_data->op_offset;
2240 int ncfspgs; /* pages read in PAGE_CACHE_SIZE */
2241 int nlupgs; /* pages read in LU_PAGE_SIZE */
2242 struct lmv_tgt_desc *tgt;
2245 rc = lmv_check_connect(obd);
2249 CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n",
2250 offset, PFID(&op_data->op_fid1));
2252 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2254 RETURN(PTR_ERR(tgt));
2256 rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2260 ncfspgs = ((*request)->rq_bulk->bd_nob_transferred +
2261 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2262 nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2263 LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2264 LASSERT(ncfspgs > 0 && ncfspgs <= op_data->op_npages);
2266 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", ncfspgs, nlupgs,
2267 op_data->op_npages);
2269 lmv_adjust_dirpages(pages, ncfspgs, nlupgs);
2274 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2275 struct ptlrpc_request **request)
2277 struct obd_device *obd = exp->exp_obd;
2278 struct lmv_obd *lmv = &obd->u.lmv;
2279 struct lmv_tgt_desc *tgt = NULL;
2280 struct mdt_body *body;
2284 rc = lmv_check_connect(obd);
2288 /* Send unlink requests to the MDT where the child is located */
2289 if (likely(!fid_is_zero(&op_data->op_fid2)))
2290 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2292 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2294 RETURN(PTR_ERR(tgt));
2296 op_data->op_fsuid = current_fsuid();
2297 op_data->op_fsgid = current_fsgid();
2298 op_data->op_cap = cfs_curproc_cap_pack();
2301 * If child's fid is given, cancel unused locks for it if it is from
2302 * another export than parent.
2304 * LOOKUP lock for child (fid3) should also be cancelled on parent
2305 * tgt_tgt in mdc_unlink().
2307 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2310 * Cancel FULL locks on child (fid3).
2312 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2313 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2318 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2319 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2321 rc = md_unlink(tgt->ltd_exp, op_data, request);
2322 if (rc != 0 && rc != -EREMOTE)
2325 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2329 /* Not cross-ref case, just get out of here. */
2330 if (likely(!(body->valid & OBD_MD_MDS)))
2333 CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2334 exp->exp_obd->obd_name, PFID(&body->fid1));
2336 /* This is a remote object, try remote MDT, Note: it may
2337 * try more than 1 time here, Considering following case
2338 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2339 * 1. Initially A does not know where remote1 is, it send
2340 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2341 * resend unlink RPC to MDT1 (retry 1st time).
2343 * 2. During the unlink RPC in flight,
2344 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2345 * and create new remote1, but on MDT0
2347 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2348 * /mnt/lustre, then lookup get fid of remote1, and find
2349 * it is remote dir again, and replay -EREMOTE again.
2351 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2353 * In theory, it might try unlimited time here, but it should
2354 * be very rare case. */
2355 op_data->op_fid2 = body->fid1;
2356 ptlrpc_req_finished(*request);
2362 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2364 struct lmv_obd *lmv = &obd->u.lmv;
2368 case OBD_CLEANUP_EARLY:
2369 /* XXX: here should be calling obd_precleanup() down to
2372 case OBD_CLEANUP_EXPORTS:
2373 fld_client_proc_fini(&lmv->lmv_fld);
2374 lprocfs_obd_cleanup(obd);
2375 lprocfs_free_md_stats(obd);
2383 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2384 __u32 keylen, void *key, __u32 *vallen, void *val,
2385 struct lov_stripe_md *lsm)
2387 struct obd_device *obd;
2388 struct lmv_obd *lmv;
2392 obd = class_exp2obd(exp);
2394 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2395 exp->exp_handle.h_cookie);
2400 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2401 struct lmv_tgt_desc *tgt;
2404 rc = lmv_check_connect(obd);
2408 LASSERT(*vallen == sizeof(__u32));
2409 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2412 * All tgts should be connected when this gets called.
2414 if (tgt == NULL || tgt->ltd_exp == NULL)
2417 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2422 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2423 rc = lmv_check_connect(obd);
2428 * Forwarding this request to first MDS, it should know LOV
2431 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2433 if (!rc && KEY_IS(KEY_CONN_DATA))
2434 exp->exp_connect_data = *(struct obd_connect_data *)val;
2436 } else if (KEY_IS(KEY_TGT_COUNT)) {
2437 *((int *)val) = lmv->desc.ld_tgt_count;
2441 CDEBUG(D_IOCTL, "Invalid key\n");
2445 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2446 obd_count keylen, void *key, obd_count vallen,
2447 void *val, struct ptlrpc_request_set *set)
2449 struct lmv_tgt_desc *tgt;
2450 struct obd_device *obd;
2451 struct lmv_obd *lmv;
2455 obd = class_exp2obd(exp);
2457 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2458 exp->exp_handle.h_cookie);
2463 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2466 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2469 if (tgt == NULL || tgt->ltd_exp == NULL)
2472 err = obd_set_info_async(env, tgt->ltd_exp,
2473 keylen, key, vallen, val, set);
2484 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2485 struct lov_stripe_md *lsm)
2487 struct obd_device *obd = class_exp2obd(exp);
2488 struct lmv_obd *lmv = &obd->u.lmv;
2489 struct lmv_stripe_md *meap;
2490 struct lmv_stripe_md *lsmp;
2495 mea_size = lmv_get_easize(lmv);
2499 if (*lmmp && !lsm) {
2500 OBD_FREE_LARGE(*lmmp, mea_size);
2505 if (*lmmp == NULL) {
2506 OBD_ALLOC_LARGE(*lmmp, mea_size);
2514 lsmp = (struct lmv_stripe_md *)lsm;
2515 meap = (struct lmv_stripe_md *)*lmmp;
2517 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2518 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2521 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2522 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2523 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2525 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2526 meap->mea_ids[i] = lsmp->mea_ids[i];
2527 fid_cpu_to_le(&meap->mea_ids[i], &lsmp->mea_ids[i]);
2533 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2534 struct lov_mds_md *lmm, int lmm_size)
2536 struct obd_device *obd = class_exp2obd(exp);
2537 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2538 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2539 struct lmv_obd *lmv = &obd->u.lmv;
2545 mea_size = lmv_get_easize(lmv);
2549 if (*lsmp != NULL && lmm == NULL) {
2550 OBD_FREE_LARGE(*tmea, mea_size);
2555 LASSERT(mea_size == lmm_size);
2557 OBD_ALLOC_LARGE(*tmea, mea_size);
2564 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2565 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2566 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2568 magic = le32_to_cpu(mea->mea_magic);
2571 * Old mea is not handled here.
2573 CERROR("Old not supportable EA is found\n");
2577 (*tmea)->mea_magic = magic;
2578 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2579 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2581 for (i = 0; i < (*tmea)->mea_count; i++) {
2582 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2583 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2588 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2589 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2590 ldlm_cancel_flags_t flags, void *opaque)
2592 struct obd_device *obd = exp->exp_obd;
2593 struct lmv_obd *lmv = &obd->u.lmv;
2599 LASSERT(fid != NULL);
2601 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2602 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL ||
2603 lmv->tgts[i]->ltd_active == 0)
2606 err = md_cancel_unused(lmv->tgts[i]->ltd_exp, fid,
2607 policy, mode, flags, opaque);
2614 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2617 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2621 rc = md_set_lock_data(lmv->tgts[0]->ltd_exp, lockh, data, bits);
2625 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2626 const struct lu_fid *fid, ldlm_type_t type,
2627 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2628 struct lustre_handle *lockh)
2630 struct obd_device *obd = exp->exp_obd;
2631 struct lmv_obd *lmv = &obd->u.lmv;
2636 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2639 * With CMD every object can have two locks in different namespaces:
2640 * lookup lock in space of mds storing direntry and update/open lock in
2641 * space of mds storing inode. Thus we check all targets, not only that
2642 * one fid was created in.
2644 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2645 if (lmv->tgts[i] == NULL ||
2646 lmv->tgts[i]->ltd_exp == NULL ||
2647 lmv->tgts[i]->ltd_active == 0)
2650 rc = md_lock_match(lmv->tgts[i]->ltd_exp, flags, fid,
2651 type, policy, mode, lockh);
2659 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2660 struct obd_export *dt_exp, struct obd_export *md_exp,
2661 struct lustre_md *md)
2663 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2665 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2668 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2670 struct obd_device *obd = exp->exp_obd;
2671 struct lmv_obd *lmv = &obd->u.lmv;
2675 obd_free_memmd(exp, (void *)&md->mea);
2676 RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
2679 int lmv_set_open_replay_data(struct obd_export *exp,
2680 struct obd_client_handle *och,
2681 struct ptlrpc_request *open_req)
2683 struct obd_device *obd = exp->exp_obd;
2684 struct lmv_obd *lmv = &obd->u.lmv;
2685 struct lmv_tgt_desc *tgt;
2688 tgt = lmv_find_target(lmv, &och->och_fid);
2690 RETURN(PTR_ERR(tgt));
2692 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2695 int lmv_clear_open_replay_data(struct obd_export *exp,
2696 struct obd_client_handle *och)
2698 struct obd_device *obd = exp->exp_obd;
2699 struct lmv_obd *lmv = &obd->u.lmv;
2700 struct lmv_tgt_desc *tgt;
2703 tgt = lmv_find_target(lmv, &och->och_fid);
2705 RETURN(PTR_ERR(tgt));
2707 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2710 static int lmv_get_remote_perm(struct obd_export *exp,
2711 const struct lu_fid *fid,
2712 struct obd_capa *oc, __u32 suppgid,
2713 struct ptlrpc_request **request)
2715 struct obd_device *obd = exp->exp_obd;
2716 struct lmv_obd *lmv = &obd->u.lmv;
2717 struct lmv_tgt_desc *tgt;
2721 rc = lmv_check_connect(obd);
2725 tgt = lmv_find_target(lmv, fid);
2727 RETURN(PTR_ERR(tgt));
2729 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2733 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2736 struct obd_device *obd = exp->exp_obd;
2737 struct lmv_obd *lmv = &obd->u.lmv;
2738 struct lmv_tgt_desc *tgt;
2742 rc = lmv_check_connect(obd);
2746 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2748 RETURN(PTR_ERR(tgt));
2750 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2754 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2755 const struct req_msg_field *field, struct obd_capa **oc)
2757 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2759 return md_unpack_capa(lmv->tgts[0]->ltd_exp, req, field, oc);
2762 int lmv_intent_getattr_async(struct obd_export *exp,
2763 struct md_enqueue_info *minfo,
2764 struct ldlm_enqueue_info *einfo)
2766 struct md_op_data *op_data = &minfo->mi_data;
2767 struct obd_device *obd = exp->exp_obd;
2768 struct lmv_obd *lmv = &obd->u.lmv;
2769 struct lmv_tgt_desc *tgt = NULL;
2773 rc = lmv_check_connect(obd);
2777 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2779 RETURN(PTR_ERR(tgt));
2781 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2785 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2786 struct lu_fid *fid, __u64 *bits)
2788 struct obd_device *obd = exp->exp_obd;
2789 struct lmv_obd *lmv = &obd->u.lmv;
2790 struct lmv_tgt_desc *tgt;
2794 rc = lmv_check_connect(obd);
2798 tgt = lmv_find_target(lmv, fid);
2800 RETURN(PTR_ERR(tgt));
2802 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2807 * For lmv, only need to send request to master MDT, and the master MDT will
2808 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2809 * we directly fetch data from the slave MDTs.
2811 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2812 struct obd_quotactl *oqctl)
2814 struct obd_device *obd = class_exp2obd(exp);
2815 struct lmv_obd *lmv = &obd->u.lmv;
2816 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2819 __u64 curspace, curinodes;
2822 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
2823 CERROR("master lmv inactive\n");
2827 if (oqctl->qc_cmd != Q_GETOQUOTA) {
2828 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2832 curspace = curinodes = 0;
2833 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2837 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
2839 if (!tgt->ltd_active) {
2840 CDEBUG(D_HA, "mdt %d is inactive.\n", i);
2844 err = obd_quotactl(tgt->ltd_exp, oqctl);
2846 CERROR("getquota on mdt %d failed. %d\n", i, err);
2850 curspace += oqctl->qc_dqblk.dqb_curspace;
2851 curinodes += oqctl->qc_dqblk.dqb_curinodes;
2854 oqctl->qc_dqblk.dqb_curspace = curspace;
2855 oqctl->qc_dqblk.dqb_curinodes = curinodes;
2860 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
2861 struct obd_quotactl *oqctl)
2863 struct obd_device *obd = class_exp2obd(exp);
2864 struct lmv_obd *lmv = &obd->u.lmv;
2865 struct lmv_tgt_desc *tgt;
2870 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2873 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
2874 CERROR("lmv idx %d inactive\n", i);
2878 err = obd_quotacheck(tgt->ltd_exp, oqctl);
2886 struct obd_ops lmv_obd_ops = {
2887 .o_owner = THIS_MODULE,
2888 .o_setup = lmv_setup,
2889 .o_cleanup = lmv_cleanup,
2890 .o_precleanup = lmv_precleanup,
2891 .o_process_config = lmv_process_config,
2892 .o_connect = lmv_connect,
2893 .o_disconnect = lmv_disconnect,
2894 .o_statfs = lmv_statfs,
2895 .o_get_info = lmv_get_info,
2896 .o_set_info_async = lmv_set_info_async,
2897 .o_packmd = lmv_packmd,
2898 .o_unpackmd = lmv_unpackmd,
2899 .o_notify = lmv_notify,
2900 .o_get_uuid = lmv_get_uuid,
2901 .o_iocontrol = lmv_iocontrol,
2902 .o_quotacheck = lmv_quotacheck,
2903 .o_quotactl = lmv_quotactl
2906 struct md_ops lmv_md_ops = {
2907 .m_getstatus = lmv_getstatus,
2908 .m_null_inode = lmv_null_inode,
2909 .m_find_cbdata = lmv_find_cbdata,
2910 .m_close = lmv_close,
2911 .m_create = lmv_create,
2912 .m_done_writing = lmv_done_writing,
2913 .m_enqueue = lmv_enqueue,
2914 .m_getattr = lmv_getattr,
2915 .m_getxattr = lmv_getxattr,
2916 .m_getattr_name = lmv_getattr_name,
2917 .m_intent_lock = lmv_intent_lock,
2919 .m_rename = lmv_rename,
2920 .m_setattr = lmv_setattr,
2921 .m_setxattr = lmv_setxattr,
2922 .m_fsync = lmv_fsync,
2923 .m_readpage = lmv_readpage,
2924 .m_unlink = lmv_unlink,
2925 .m_init_ea_size = lmv_init_ea_size,
2926 .m_cancel_unused = lmv_cancel_unused,
2927 .m_set_lock_data = lmv_set_lock_data,
2928 .m_lock_match = lmv_lock_match,
2929 .m_get_lustre_md = lmv_get_lustre_md,
2930 .m_free_lustre_md = lmv_free_lustre_md,
2931 .m_set_open_replay_data = lmv_set_open_replay_data,
2932 .m_clear_open_replay_data = lmv_clear_open_replay_data,
2933 .m_renew_capa = lmv_renew_capa,
2934 .m_unpack_capa = lmv_unpack_capa,
2935 .m_get_remote_perm = lmv_get_remote_perm,
2936 .m_intent_getattr_async = lmv_intent_getattr_async,
2937 .m_revalidate_lock = lmv_revalidate_lock
2940 int __init lmv_init(void)
2942 struct lprocfs_static_vars lvars;
2945 lprocfs_lmv_init_vars(&lvars);
2947 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2948 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2953 static void lmv_exit(void)
2955 class_unregister_type(LUSTRE_LMV_NAME);
2958 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2959 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2960 MODULE_LICENSE("GPL");
2962 module_init(lmv_init);
2963 module_exit(lmv_exit);