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, 2012, 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
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/pagemap.h>
43 #include <asm/div64.h>
44 #include <linux/seq_file.h>
45 #include <linux/namei.h>
46 #include <linux/uaccess.h>
48 #include "../include/lustre/lustre_idl.h"
49 #include "../include/obd_support.h"
50 #include "../include/lustre_lib.h"
51 #include "../include/lustre_net.h"
52 #include "../include/obd_class.h"
53 #include "../include/lprocfs_status.h"
54 #include "../include/lustre_lite.h"
55 #include "../include/lustre_fid.h"
56 #include "lmv_internal.h"
58 static void lmv_activate_target(struct lmv_obd *lmv,
59 struct lmv_tgt_desc *tgt,
62 if (tgt->ltd_active == activate)
65 tgt->ltd_active = activate;
66 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
72 * -EINVAL : UUID can't be found in the LMV's target list
73 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
74 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
76 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
79 struct lmv_tgt_desc *uninitialized_var(tgt);
80 struct obd_device *obd;
84 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
85 lmv, uuid->uuid, activate);
87 spin_lock(&lmv->lmv_lock);
88 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
90 if (tgt == NULL || tgt->ltd_exp == NULL)
93 CDEBUG(D_INFO, "Target idx %d is %s conn %#llx\n", i,
94 tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
96 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
100 if (i == lmv->desc.ld_tgt_count) {
105 obd = class_exp2obd(tgt->ltd_exp);
111 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
112 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
113 obd->obd_type->typ_name, i);
114 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
116 if (tgt->ltd_active == activate) {
117 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
118 activate ? "" : "in");
122 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
123 activate ? "" : "in");
124 lmv_activate_target(lmv, tgt, activate);
127 spin_unlock(&lmv->lmv_lock);
131 static struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
133 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
135 return obd_get_uuid(lmv->tgts[0]->ltd_exp);
138 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
139 enum obd_notify_event ev, void *data)
141 struct obd_connect_data *conn_data;
142 struct lmv_obd *lmv = &obd->u.lmv;
143 struct obd_uuid *uuid;
146 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
147 CERROR("unexpected notification of %s %s!\n",
148 watched->obd_type->typ_name,
153 uuid = &watched->u.cli.cl_target_uuid;
154 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
156 * Set MDC as active before notifying the observer, so the
157 * observer can use the MDC normally.
159 rc = lmv_set_mdc_active(lmv, uuid,
160 ev == OBD_NOTIFY_ACTIVE);
162 CERROR("%sactivation of %s failed: %d\n",
163 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
167 } else if (ev == OBD_NOTIFY_OCD) {
168 conn_data = &watched->u.cli.cl_import->imp_connect_data;
170 * XXX: Make sure that ocd_connect_flags from all targets are
171 * the same. Otherwise one of MDTs runs wrong version or
172 * something like this. --umka
174 obd->obd_self_export->exp_connect_data = *conn_data;
177 else if (ev == OBD_NOTIFY_DISCON) {
179 * For disconnect event, flush fld cache for failout MDS case.
181 fld_client_flush(&lmv->lmv_fld);
185 * Pass the notification up the chain.
187 if (obd->obd_observer)
188 rc = obd_notify(obd->obd_observer, watched, ev, data);
194 * This is fake connect function. Its purpose is to initialize lmv and say
195 * caller that everything is okay. Real connection will be performed later.
197 static int lmv_connect(const struct lu_env *env,
198 struct obd_export **exp, struct obd_device *obd,
199 struct obd_uuid *cluuid, struct obd_connect_data *data,
202 struct proc_dir_entry *lmv_proc_dir;
203 struct lmv_obd *lmv = &obd->u.lmv;
204 struct lustre_handle conn = { 0 };
208 * We don't want to actually do the underlying connections more than
209 * once, so keep track.
212 if (lmv->refcount > 1) {
217 rc = class_connect(&conn, obd, cluuid);
219 CERROR("class_connection() returned %d\n", rc);
223 *exp = class_conn2export(&conn);
224 class_export_get(*exp);
228 lmv->cluuid = *cluuid;
231 lmv->conn_data = *data;
233 if (obd->obd_proc_private != NULL) {
234 lmv_proc_dir = obd->obd_proc_private;
236 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
238 if (IS_ERR(lmv_proc_dir)) {
239 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
240 obd->obd_type->typ_name, obd->obd_name);
243 obd->obd_proc_private = lmv_proc_dir;
247 * All real clients should perform actual connection right away, because
248 * it is possible, that LMV will not have opportunity to connect targets
249 * and MDC stuff will be called directly, for instance while reading
250 * ../mdc/../kbytesfree procfs file, etc.
252 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
253 rc = lmv_check_connect(obd);
255 if (rc && lmv_proc_dir) {
256 lprocfs_remove(&lmv_proc_dir);
257 obd->obd_proc_private = NULL;
263 static void lmv_set_timeouts(struct obd_device *obd)
265 struct lmv_tgt_desc *tgt;
270 if (lmv->server_timeout == 0)
273 if (lmv->connected == 0)
276 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
278 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
281 obd_set_info_async(NULL, tgt->ltd_exp, sizeof(KEY_INTERMDS),
282 KEY_INTERMDS, 0, NULL, NULL);
286 static int lmv_init_ea_size(struct obd_export *exp, int easize,
287 int def_easize, int cookiesize, int def_cookiesize)
289 struct obd_device *obd = exp->exp_obd;
290 struct lmv_obd *lmv = &obd->u.lmv;
295 if (lmv->max_easize < easize) {
296 lmv->max_easize = easize;
299 if (lmv->max_def_easize < def_easize) {
300 lmv->max_def_easize = def_easize;
303 if (lmv->max_cookiesize < cookiesize) {
304 lmv->max_cookiesize = cookiesize;
307 if (lmv->max_def_cookiesize < def_cookiesize) {
308 lmv->max_def_cookiesize = def_cookiesize;
314 if (lmv->connected == 0)
317 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
318 if (lmv->tgts[i] == NULL ||
319 lmv->tgts[i]->ltd_exp == NULL ||
320 lmv->tgts[i]->ltd_active == 0) {
321 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
325 rc = md_init_ea_size(lmv->tgts[i]->ltd_exp, easize, def_easize,
326 cookiesize, def_cookiesize);
328 CERROR("%s: obd_init_ea_size() failed on MDT target %d: rc = %d.\n",
329 obd->obd_name, i, rc);
336 #define MAX_STRING_SIZE 128
338 static int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
340 struct proc_dir_entry *lmv_proc_dir;
341 struct lmv_obd *lmv = &obd->u.lmv;
342 struct obd_uuid *cluuid = &lmv->cluuid;
343 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
344 struct obd_device *mdc_obd;
345 struct obd_export *mdc_exp;
346 struct lu_fld_target target;
349 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
352 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
356 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
357 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
358 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
361 if (!mdc_obd->obd_set_up) {
362 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
366 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
367 &lmv->conn_data, NULL);
369 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
374 * Init fid sequence client for this mdc and add new fld target.
376 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
380 target.ft_srv = NULL;
381 target.ft_exp = mdc_exp;
382 target.ft_idx = tgt->ltd_idx;
384 fld_client_add_target(&lmv->lmv_fld, &target);
386 rc = obd_register_observer(mdc_obd, obd);
388 obd_disconnect(mdc_exp);
389 CERROR("target %s register_observer error %d\n",
390 tgt->ltd_uuid.uuid, rc);
394 if (obd->obd_observer) {
396 * Tell the observer about the new target.
398 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
400 (void *)(tgt - lmv->tgts[0]));
402 obd_disconnect(mdc_exp);
408 tgt->ltd_exp = mdc_exp;
409 lmv->desc.ld_active_tgt_count++;
411 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize,
412 lmv->max_cookiesize, lmv->max_def_cookiesize);
414 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
415 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
416 atomic_read(&obd->obd_refcount));
418 lmv_proc_dir = obd->obd_proc_private;
420 struct proc_dir_entry *mdc_symlink;
422 LASSERT(mdc_obd->obd_type != NULL);
423 LASSERT(mdc_obd->obd_type->typ_name != NULL);
424 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
427 mdc_obd->obd_type->typ_name,
429 if (mdc_symlink == NULL) {
430 CERROR("Could not register LMV target /proc/fs/lustre/%s/%s/target_obds/%s.",
431 obd->obd_type->typ_name, obd->obd_name,
433 lprocfs_remove(&lmv_proc_dir);
434 obd->obd_proc_private = NULL;
440 static void lmv_del_target(struct lmv_obd *lmv, int index)
442 if (lmv->tgts[index] == NULL)
445 OBD_FREE_PTR(lmv->tgts[index]);
446 lmv->tgts[index] = NULL;
450 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
451 __u32 index, int gen)
453 struct lmv_obd *lmv = &obd->u.lmv;
454 struct lmv_tgt_desc *tgt;
457 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
461 if (lmv->desc.ld_tgt_count == 0) {
462 struct obd_device *mdc_obd;
464 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
467 lmv_init_unlock(lmv);
468 CERROR("%s: Target %s not attached: rc = %d\n",
469 obd->obd_name, uuidp->uuid, -EINVAL);
474 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
475 tgt = lmv->tgts[index];
476 CERROR("%s: UUID %s already assigned at LOV target index %d: rc = %d\n",
478 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
479 lmv_init_unlock(lmv);
483 if (index >= lmv->tgts_size) {
484 /* We need to reallocate the lmv target array. */
485 struct lmv_tgt_desc **newtgts, **old = NULL;
489 while (newsize < index + 1)
491 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
492 if (newtgts == NULL) {
493 lmv_init_unlock(lmv);
497 if (lmv->tgts_size) {
498 memcpy(newtgts, lmv->tgts,
499 sizeof(*newtgts) * lmv->tgts_size);
501 oldsize = lmv->tgts_size;
505 lmv->tgts_size = newsize;
508 OBD_FREE(old, sizeof(*old) * oldsize);
510 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
516 lmv_init_unlock(lmv);
520 mutex_init(&tgt->ltd_fid_mutex);
521 tgt->ltd_idx = index;
522 tgt->ltd_uuid = *uuidp;
524 lmv->tgts[index] = tgt;
525 if (index >= lmv->desc.ld_tgt_count)
526 lmv->desc.ld_tgt_count = index + 1;
528 if (lmv->connected) {
529 rc = lmv_connect_mdc(obd, tgt);
531 spin_lock(&lmv->lmv_lock);
532 lmv->desc.ld_tgt_count--;
533 memset(tgt, 0, sizeof(*tgt));
534 spin_unlock(&lmv->lmv_lock);
536 int easize = sizeof(struct lmv_stripe_md) +
537 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
538 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
542 lmv_init_unlock(lmv);
546 int lmv_check_connect(struct obd_device *obd)
548 struct lmv_obd *lmv = &obd->u.lmv;
549 struct lmv_tgt_desc *tgt;
558 if (lmv->connected) {
559 lmv_init_unlock(lmv);
563 if (lmv->desc.ld_tgt_count == 0) {
564 lmv_init_unlock(lmv);
565 CERROR("%s: no targets configured.\n", obd->obd_name);
569 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
570 lmv->cluuid.uuid, obd->obd_name);
572 LASSERT(lmv->tgts != NULL);
574 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
578 rc = lmv_connect_mdc(obd, tgt);
583 lmv_set_timeouts(obd);
584 class_export_put(lmv->exp);
586 easize = lmv_get_easize(lmv);
587 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
588 lmv_init_unlock(lmv);
599 --lmv->desc.ld_active_tgt_count;
600 rc2 = obd_disconnect(tgt->ltd_exp);
602 CERROR("LMV target %s disconnect on MDC idx %d: error %d\n",
603 tgt->ltd_uuid.uuid, i, rc2);
607 class_disconnect(lmv->exp);
608 lmv_init_unlock(lmv);
612 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
614 struct proc_dir_entry *lmv_proc_dir;
615 struct lmv_obd *lmv = &obd->u.lmv;
616 struct obd_device *mdc_obd;
619 LASSERT(tgt != NULL);
620 LASSERT(obd != NULL);
622 mdc_obd = class_exp2obd(tgt->ltd_exp);
625 mdc_obd->obd_force = obd->obd_force;
626 mdc_obd->obd_fail = obd->obd_fail;
627 mdc_obd->obd_no_recov = obd->obd_no_recov;
630 lmv_proc_dir = obd->obd_proc_private;
632 lprocfs_remove_proc_entry(mdc_obd->obd_name, lmv_proc_dir);
634 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
636 CERROR("Can't finalize fids factory\n");
638 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
639 tgt->ltd_exp->exp_obd->obd_name,
640 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
642 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
643 rc = obd_disconnect(tgt->ltd_exp);
645 if (tgt->ltd_active) {
646 CERROR("Target %s disconnect error %d\n",
647 tgt->ltd_uuid.uuid, rc);
651 lmv_activate_target(lmv, tgt, 0);
656 static int lmv_disconnect(struct obd_export *exp)
658 struct obd_device *obd = class_exp2obd(exp);
659 struct lmv_obd *lmv = &obd->u.lmv;
667 * Only disconnect the underlying layers on the final disconnect.
670 if (lmv->refcount != 0)
673 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
674 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
677 lmv_disconnect_mdc(obd, lmv->tgts[i]);
680 if (obd->obd_proc_private)
681 lprocfs_remove((struct proc_dir_entry **)&obd->obd_proc_private);
683 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
684 obd->obd_type->typ_name, obd->obd_name);
688 * This is the case when no real connection is established by
689 * lmv_check_connect().
692 class_export_put(exp);
693 rc = class_disconnect(exp);
694 if (lmv->refcount == 0)
699 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
701 struct obd_device *obddev = class_exp2obd(exp);
702 struct lmv_obd *lmv = &obddev->u.lmv;
703 struct getinfo_fid2path *gf;
704 struct lmv_tgt_desc *tgt;
705 struct getinfo_fid2path *remote_gf = NULL;
706 int remote_gf_size = 0;
709 gf = (struct getinfo_fid2path *)karg;
710 tgt = lmv_find_target(lmv, &gf->gf_fid);
715 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
716 if (rc != 0 && rc != -EREMOTE)
719 /* If remote_gf != NULL, it means just building the
720 * path on the remote MDT, copy this path segment to gf */
721 if (remote_gf != NULL) {
722 struct getinfo_fid2path *ori_gf;
725 ori_gf = (struct getinfo_fid2path *)karg;
726 if (strlen(ori_gf->gf_path) +
727 strlen(gf->gf_path) > ori_gf->gf_pathlen) {
732 ptr = ori_gf->gf_path;
734 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
735 strlen(ori_gf->gf_path));
737 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
738 ptr += strlen(gf->gf_path);
742 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
743 tgt->ltd_exp->exp_obd->obd_name,
744 gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
750 /* sigh, has to go to another MDT to do path building further */
751 if (remote_gf == NULL) {
752 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
753 OBD_ALLOC(remote_gf, remote_gf_size);
754 if (remote_gf == NULL) {
758 remote_gf->gf_pathlen = PATH_MAX;
761 if (!fid_is_sane(&gf->gf_fid)) {
762 CERROR("%s: invalid FID "DFID": rc = %d\n",
763 tgt->ltd_exp->exp_obd->obd_name,
764 PFID(&gf->gf_fid), -EINVAL);
769 tgt = lmv_find_target(lmv, &gf->gf_fid);
775 remote_gf->gf_fid = gf->gf_fid;
776 remote_gf->gf_recno = -1;
777 remote_gf->gf_linkno = -1;
778 memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
780 goto repeat_fid2path;
783 if (remote_gf != NULL)
784 OBD_FREE(remote_gf, remote_gf_size);
788 static int lmv_hsm_req_count(struct lmv_obd *lmv,
789 const struct hsm_user_request *hur,
790 const struct lmv_tgt_desc *tgt_mds)
793 struct lmv_tgt_desc *curr_tgt;
795 /* count how many requests must be sent to the given target */
796 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
797 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
798 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
804 static void lmv_hsm_req_build(struct lmv_obd *lmv,
805 struct hsm_user_request *hur_in,
806 const struct lmv_tgt_desc *tgt_mds,
807 struct hsm_user_request *hur_out)
810 struct lmv_tgt_desc *curr_tgt;
812 /* build the hsm_user_request for the given target */
813 hur_out->hur_request = hur_in->hur_request;
815 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
816 curr_tgt = lmv_find_target(lmv,
817 &hur_in->hur_user_item[i].hui_fid);
818 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
819 hur_out->hur_user_item[nr_out] =
820 hur_in->hur_user_item[i];
824 hur_out->hur_request.hr_itemcount = nr_out;
825 memcpy(hur_data(hur_out), hur_data(hur_in),
826 hur_in->hur_request.hr_data_len);
829 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
830 struct lustre_kernelcomm *lk, void *uarg)
834 /* unregister request (call from llapi_hsm_copytool_fini) */
835 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
836 /* best effort: try to clean as much as possible
837 * (continue on error) */
838 obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len, lk, uarg);
841 /* Whatever the result, remove copytool from kuc groups.
842 * Unreached coordinators will get EPIPE on next requests
843 * and will unregister automatically.
845 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
849 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
850 struct lustre_kernelcomm *lk, void *uarg)
855 bool any_set = false;
857 /* All or nothing: try to register to all MDS.
858 * In case of failure, unregister from previous MDS,
859 * except if it because of inactive target. */
860 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
861 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
864 if (lmv->tgts[i]->ltd_active) {
865 /* permanent error */
866 CERROR("error: iocontrol MDC %s on MDTidx %d cmd %x: err = %d\n",
867 lmv->tgts[i]->ltd_uuid.uuid,
870 lk->lk_flags |= LK_FLG_STOP;
871 /* unregister from previous MDS */
872 for (j = 0; j < i; j++)
874 lmv->tgts[j]->ltd_exp,
878 /* else: transient error.
879 * kuc will register to the missing MDT
887 /* no registration done: return error */
890 /* at least one registration done, with no failure */
891 filp = fget(lk->lk_wfd);
895 rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, lk->lk_data);
896 if (rc != 0 && filp != NULL)
904 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
905 int len, void *karg, void *uarg)
907 struct obd_device *obddev = class_exp2obd(exp);
908 struct lmv_obd *lmv = &obddev->u.lmv;
912 int count = lmv->desc.ld_tgt_count;
918 case IOC_OBD_STATFS: {
919 struct obd_ioctl_data *data = karg;
920 struct obd_device *mdc_obd;
921 struct obd_statfs stat_buf = {0};
924 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
928 if (lmv->tgts[index] == NULL ||
929 lmv->tgts[index]->ltd_active == 0)
932 mdc_obd = class_exp2obd(lmv->tgts[index]->ltd_exp);
937 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
938 min((int) data->ioc_plen2,
939 (int) sizeof(struct obd_uuid))))
942 rc = obd_statfs(NULL, lmv->tgts[index]->ltd_exp, &stat_buf,
943 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
947 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
948 min((int) data->ioc_plen1,
949 (int) sizeof(stat_buf))))
953 case OBD_IOC_QUOTACTL: {
954 struct if_quotactl *qctl = karg;
955 struct lmv_tgt_desc *tgt = NULL;
956 struct obd_quotactl *oqctl;
958 if (qctl->qc_valid == QC_MDTIDX) {
959 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
962 tgt = lmv->tgts[qctl->qc_idx];
963 if (tgt == NULL || tgt->ltd_exp == NULL)
965 } else if (qctl->qc_valid == QC_UUID) {
966 for (i = 0; i < count; i++) {
970 if (!obd_uuid_equals(&tgt->ltd_uuid,
974 if (tgt->ltd_exp == NULL)
986 LASSERT(tgt && tgt->ltd_exp);
987 OBD_ALLOC_PTR(oqctl);
991 QCTL_COPY(oqctl, qctl);
992 rc = obd_quotactl(tgt->ltd_exp, oqctl);
994 QCTL_COPY(qctl, oqctl);
995 qctl->qc_valid = QC_MDTIDX;
996 qctl->obd_uuid = tgt->ltd_uuid;
1001 case OBD_IOC_CHANGELOG_SEND:
1002 case OBD_IOC_CHANGELOG_CLEAR: {
1003 struct ioc_changelog *icc = karg;
1005 if (icc->icc_mdtindex >= count)
1008 if (lmv->tgts[icc->icc_mdtindex] == NULL ||
1009 lmv->tgts[icc->icc_mdtindex]->ltd_exp == NULL ||
1010 lmv->tgts[icc->icc_mdtindex]->ltd_active == 0)
1012 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex]->ltd_exp,
1013 sizeof(*icc), icc, NULL);
1016 case LL_IOC_GET_CONNECT_FLAGS: {
1017 if (lmv->tgts[0] == NULL)
1019 rc = obd_iocontrol(cmd, lmv->tgts[0]->ltd_exp, len, karg, uarg);
1022 case OBD_IOC_FID2PATH: {
1023 rc = lmv_fid2path(exp, len, karg, uarg);
1026 case LL_IOC_HSM_STATE_GET:
1027 case LL_IOC_HSM_STATE_SET:
1028 case LL_IOC_HSM_ACTION: {
1029 struct md_op_data *op_data = karg;
1030 struct lmv_tgt_desc *tgt;
1032 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1034 return PTR_ERR(tgt);
1036 if (tgt->ltd_exp == NULL)
1039 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1042 case LL_IOC_HSM_PROGRESS: {
1043 const struct hsm_progress_kernel *hpk = karg;
1044 struct lmv_tgt_desc *tgt;
1046 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1048 return PTR_ERR(tgt);
1049 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1052 case LL_IOC_HSM_REQUEST: {
1053 struct hsm_user_request *hur = karg;
1054 struct lmv_tgt_desc *tgt;
1055 unsigned int reqcount = hur->hur_request.hr_itemcount;
1060 /* if the request is about a single fid
1061 * or if there is a single MDS, no need to split
1063 if (reqcount == 1 || count == 1) {
1064 tgt = lmv_find_target(lmv,
1065 &hur->hur_user_item[0].hui_fid);
1067 return PTR_ERR(tgt);
1068 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1070 /* split fid list to their respective MDS */
1071 for (i = 0; i < count; i++) {
1072 unsigned int nr, reqlen;
1074 struct hsm_user_request *req;
1076 nr = lmv_hsm_req_count(lmv, hur, lmv->tgts[i]);
1077 if (nr == 0) /* nothing for this MDS */
1080 /* build a request with fids for this MDS */
1081 reqlen = offsetof(typeof(*hur),
1083 + hur->hur_request.hr_data_len;
1084 OBD_ALLOC_LARGE(req, reqlen);
1088 lmv_hsm_req_build(lmv, hur, lmv->tgts[i], req);
1090 rc1 = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
1092 if (rc1 != 0 && rc == 0)
1094 OBD_FREE_LARGE(req, reqlen);
1099 case LL_IOC_LOV_SWAP_LAYOUTS: {
1100 struct md_op_data *op_data = karg;
1101 struct lmv_tgt_desc *tgt1, *tgt2;
1103 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1105 return PTR_ERR(tgt1);
1107 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1109 return PTR_ERR(tgt2);
1111 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1114 /* only files on same MDT can have their layouts swapped */
1115 if (tgt1->ltd_idx != tgt2->ltd_idx)
1118 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1121 case LL_IOC_HSM_CT_START: {
1122 struct lustre_kernelcomm *lk = karg;
1123 if (lk->lk_flags & LK_FLG_STOP)
1124 rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1126 rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1130 for (i = 0; i < count; i++) {
1131 struct obd_device *mdc_obd;
1134 if (lmv->tgts[i] == NULL ||
1135 lmv->tgts[i]->ltd_exp == NULL)
1137 /* ll_umount_begin() sets force flag but for lmv, not
1138 * mdc. Let's pass it through */
1139 mdc_obd = class_exp2obd(lmv->tgts[i]->ltd_exp);
1140 mdc_obd->obd_force = obddev->obd_force;
1141 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len,
1143 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1146 if (lmv->tgts[i]->ltd_active) {
1147 CERROR("error: iocontrol MDC %s on MDTidx %d cmd %x: err = %d\n",
1148 lmv->tgts[i]->ltd_uuid.uuid,
1163 static int lmv_all_chars_policy(int count, const char *name,
1174 static int lmv_nid_policy(struct lmv_obd *lmv)
1176 struct obd_import *imp;
1180 * XXX: To get nid we assume that underlying obd device is mdc.
1182 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1183 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1184 return id % lmv->desc.ld_tgt_count;
1187 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1188 enum placement_policy placement)
1190 switch (placement) {
1191 case PLACEMENT_CHAR_POLICY:
1192 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1194 op_data->op_namelen);
1195 case PLACEMENT_NID_POLICY:
1196 return lmv_nid_policy(lmv);
1202 CERROR("Unsupported placement policy %x\n", placement);
1208 * This is _inode_ placement policy function (not name).
1210 static int lmv_placement_policy(struct obd_device *obd,
1211 struct md_op_data *op_data, u32 *mds)
1213 struct lmv_obd *lmv = &obd->u.lmv;
1215 LASSERT(mds != NULL);
1217 if (lmv->desc.ld_tgt_count == 1) {
1223 * If stripe_offset is provided during setdirstripe
1224 * (setdirstripe -i xx), xx MDS will be chosen.
1226 if (op_data->op_cli_flags & CLI_SET_MEA) {
1227 struct lmv_user_md *lum;
1229 lum = (struct lmv_user_md *)op_data->op_data;
1230 if (lum->lum_type == LMV_STRIPE_TYPE &&
1231 lum->lum_stripe_offset != -1) {
1232 if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
1233 CERROR("%s: Stripe_offset %d > MDT count %d: rc = %d\n",
1235 lum->lum_stripe_offset,
1236 lmv->desc.ld_tgt_count, -ERANGE);
1239 *mds = lum->lum_stripe_offset;
1244 /* Allocate new fid on target according to operation type and parent
1246 *mds = op_data->op_mds;
1250 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1252 struct lmv_tgt_desc *tgt;
1255 tgt = lmv_get_target(lmv, mds);
1257 return PTR_ERR(tgt);
1260 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1261 * on server that seq in new allocated fid is not yet known.
1263 mutex_lock(&tgt->ltd_fid_mutex);
1265 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL) {
1271 * Asking underlaying tgt layer to allocate new fid.
1273 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1275 LASSERT(fid_is_sane(fid));
1280 mutex_unlock(&tgt->ltd_fid_mutex);
1284 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1285 struct md_op_data *op_data)
1287 struct obd_device *obd = class_exp2obd(exp);
1288 struct lmv_obd *lmv = &obd->u.lmv;
1292 LASSERT(op_data != NULL);
1293 LASSERT(fid != NULL);
1295 rc = lmv_placement_policy(obd, op_data, &mds);
1297 CERROR("Can't get target for allocating fid, rc %d\n",
1302 rc = __lmv_fid_alloc(lmv, fid, mds);
1304 CERROR("Can't alloc new fid, rc %d\n", rc);
1311 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1313 struct lmv_obd *lmv = &obd->u.lmv;
1314 struct lprocfs_static_vars lvars;
1315 struct lmv_desc *desc;
1318 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1319 CERROR("LMV setup requires a descriptor\n");
1323 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1324 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1325 CERROR("Lmv descriptor size wrong: %d > %d\n",
1326 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1330 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1331 if (lmv->tgts == NULL)
1333 lmv->tgts_size = 32;
1335 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1336 lmv->desc.ld_tgt_count = 0;
1337 lmv->desc.ld_active_tgt_count = 0;
1338 lmv->max_cookiesize = 0;
1339 lmv->max_def_easize = 0;
1340 lmv->max_easize = 0;
1341 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1343 spin_lock_init(&lmv->lmv_lock);
1344 mutex_init(&lmv->init_mutex);
1346 lprocfs_lmv_init_vars(&lvars);
1348 lprocfs_obd_setup(obd, lvars.obd_vars);
1349 #if defined (CONFIG_PROC_FS)
1351 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1352 0444, &lmv_proc_target_fops, obd);
1354 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1358 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1359 LUSTRE_CLI_FLD_HASH_DHT);
1361 CERROR("Can't init FLD, err %d\n", rc);
1371 static int lmv_cleanup(struct obd_device *obd)
1373 struct lmv_obd *lmv = &obd->u.lmv;
1375 fld_client_fini(&lmv->lmv_fld);
1376 if (lmv->tgts != NULL) {
1378 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1379 if (lmv->tgts[i] == NULL)
1381 lmv_del_target(lmv, i);
1383 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1389 static int lmv_process_config(struct obd_device *obd, u32 len, void *buf)
1391 struct lustre_cfg *lcfg = buf;
1392 struct obd_uuid obd_uuid;
1397 switch (lcfg->lcfg_command) {
1399 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1400 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1401 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid)) {
1406 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1408 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1) {
1412 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1) {
1416 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1419 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1427 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1428 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1430 struct obd_device *obd = class_exp2obd(exp);
1431 struct lmv_obd *lmv = &obd->u.lmv;
1432 struct obd_statfs *temp;
1436 rc = lmv_check_connect(obd);
1440 OBD_ALLOC(temp, sizeof(*temp));
1444 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1445 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1448 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1451 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1452 lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1459 /* If the statfs is from mount, it will needs
1460 * retrieve necessary information from MDT0.
1461 * i.e. mount does not need the merged osfs
1463 * And also clients can be mounted as long as
1464 * MDT0 is in service*/
1465 if (flags & OBD_STATFS_FOR_MDT0)
1468 osfs->os_bavail += temp->os_bavail;
1469 osfs->os_blocks += temp->os_blocks;
1470 osfs->os_ffree += temp->os_ffree;
1471 osfs->os_files += temp->os_files;
1476 OBD_FREE(temp, sizeof(*temp));
1480 static int lmv_getstatus(struct obd_export *exp,
1482 struct obd_capa **pc)
1484 struct obd_device *obd = exp->exp_obd;
1485 struct lmv_obd *lmv = &obd->u.lmv;
1488 rc = lmv_check_connect(obd);
1492 rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1496 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1497 struct obd_capa *oc, u64 valid, const char *name,
1498 const char *input, int input_size, int output_size,
1499 int flags, struct ptlrpc_request **request)
1501 struct obd_device *obd = exp->exp_obd;
1502 struct lmv_obd *lmv = &obd->u.lmv;
1503 struct lmv_tgt_desc *tgt;
1506 rc = lmv_check_connect(obd);
1510 tgt = lmv_find_target(lmv, fid);
1512 return PTR_ERR(tgt);
1514 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1515 input_size, output_size, flags, request);
1520 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1521 struct obd_capa *oc, u64 valid, const char *name,
1522 const char *input, int input_size, int output_size,
1523 int flags, __u32 suppgid,
1524 struct ptlrpc_request **request)
1526 struct obd_device *obd = exp->exp_obd;
1527 struct lmv_obd *lmv = &obd->u.lmv;
1528 struct lmv_tgt_desc *tgt;
1531 rc = lmv_check_connect(obd);
1535 tgt = lmv_find_target(lmv, fid);
1537 return PTR_ERR(tgt);
1539 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1540 input_size, output_size, flags, suppgid,
1546 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1547 struct ptlrpc_request **request)
1549 struct obd_device *obd = exp->exp_obd;
1550 struct lmv_obd *lmv = &obd->u.lmv;
1551 struct lmv_tgt_desc *tgt;
1554 rc = lmv_check_connect(obd);
1558 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1560 return PTR_ERR(tgt);
1562 if (op_data->op_flags & MF_GET_MDT_IDX) {
1563 op_data->op_mds = tgt->ltd_idx;
1567 rc = md_getattr(tgt->ltd_exp, op_data, request);
1572 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1574 struct obd_device *obd = exp->exp_obd;
1575 struct lmv_obd *lmv = &obd->u.lmv;
1579 rc = lmv_check_connect(obd);
1583 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1586 * With DNE every object can have two locks in different namespaces:
1587 * lookup lock in space of MDT storing direntry and update/open lock in
1588 * space of MDT storing inode.
1590 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1591 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1593 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1599 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1600 ldlm_iterator_t it, void *data)
1602 struct obd_device *obd = exp->exp_obd;
1603 struct lmv_obd *lmv = &obd->u.lmv;
1607 rc = lmv_check_connect(obd);
1611 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1614 * With DNE every object can have two locks in different namespaces:
1615 * lookup lock in space of MDT storing direntry and update/open lock in
1616 * space of MDT storing inode.
1618 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1619 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1621 rc = md_find_cbdata(lmv->tgts[i]->ltd_exp, fid, it, data);
1630 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1631 struct md_open_data *mod, struct ptlrpc_request **request)
1633 struct obd_device *obd = exp->exp_obd;
1634 struct lmv_obd *lmv = &obd->u.lmv;
1635 struct lmv_tgt_desc *tgt;
1638 rc = lmv_check_connect(obd);
1642 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1644 return PTR_ERR(tgt);
1646 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1647 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1652 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1655 struct lmv_tgt_desc *tgt;
1657 tgt = lmv_find_target(lmv, fid);
1661 op_data->op_mds = tgt->ltd_idx;
1666 static int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1667 const void *data, int datalen, int mode, __u32 uid,
1668 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1669 struct ptlrpc_request **request)
1671 struct obd_device *obd = exp->exp_obd;
1672 struct lmv_obd *lmv = &obd->u.lmv;
1673 struct lmv_tgt_desc *tgt;
1676 rc = lmv_check_connect(obd);
1680 if (!lmv->desc.ld_active_tgt_count)
1683 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1685 return PTR_ERR(tgt);
1687 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1691 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1692 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1695 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1696 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1697 cap_effective, rdev, request);
1700 if (*request == NULL)
1702 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1707 static int lmv_done_writing(struct obd_export *exp,
1708 struct md_op_data *op_data,
1709 struct md_open_data *mod)
1711 struct obd_device *obd = exp->exp_obd;
1712 struct lmv_obd *lmv = &obd->u.lmv;
1713 struct lmv_tgt_desc *tgt;
1716 rc = lmv_check_connect(obd);
1720 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1722 return PTR_ERR(tgt);
1724 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1729 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1730 struct lookup_intent *it, struct md_op_data *op_data,
1731 struct lustre_handle *lockh, void *lmm, int lmmsize,
1732 __u64 extra_lock_flags)
1734 struct ptlrpc_request *req = it->d.lustre.it_data;
1735 struct obd_device *obd = exp->exp_obd;
1736 struct lmv_obd *lmv = &obd->u.lmv;
1737 struct lustre_handle plock;
1738 struct lmv_tgt_desc *tgt;
1739 struct md_op_data *rdata;
1741 struct mdt_body *body;
1745 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1746 LASSERT(body != NULL);
1748 if (!(body->valid & OBD_MD_MDS))
1751 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1752 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1755 * We got LOOKUP lock, but we really need attrs.
1757 pmode = it->d.lustre.it_lock_mode;
1758 LASSERT(pmode != 0);
1759 memcpy(&plock, lockh, sizeof(plock));
1760 it->d.lustre.it_lock_mode = 0;
1761 it->d.lustre.it_data = NULL;
1764 ptlrpc_req_finished(req);
1766 tgt = lmv_find_target(lmv, &fid1);
1772 OBD_ALLOC_PTR(rdata);
1773 if (rdata == NULL) {
1778 rdata->op_fid1 = fid1;
1779 rdata->op_bias = MDS_CROSS_REF;
1781 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1782 lmm, lmmsize, NULL, extra_lock_flags);
1783 OBD_FREE_PTR(rdata);
1785 ldlm_lock_decref(&plock, pmode);
1790 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1791 struct lookup_intent *it, struct md_op_data *op_data,
1792 struct lustre_handle *lockh, void *lmm, int lmmsize,
1793 struct ptlrpc_request **req, __u64 extra_lock_flags)
1795 struct obd_device *obd = exp->exp_obd;
1796 struct lmv_obd *lmv = &obd->u.lmv;
1797 struct lmv_tgt_desc *tgt;
1800 rc = lmv_check_connect(obd);
1804 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1805 LL_IT2STR(it), PFID(&op_data->op_fid1));
1807 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1809 return PTR_ERR(tgt);
1811 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1812 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1814 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1815 lmm, lmmsize, req, extra_lock_flags);
1817 if (rc == 0 && it && it->it_op == IT_OPEN) {
1818 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1819 lmm, lmmsize, extra_lock_flags);
1825 lmv_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
1826 struct ptlrpc_request **request)
1828 struct ptlrpc_request *req = NULL;
1829 struct obd_device *obd = exp->exp_obd;
1830 struct lmv_obd *lmv = &obd->u.lmv;
1831 struct lmv_tgt_desc *tgt;
1832 struct mdt_body *body;
1835 rc = lmv_check_connect(obd);
1839 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1841 return PTR_ERR(tgt);
1843 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1844 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1847 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1851 body = req_capsule_server_get(&(*request)->rq_pill,
1853 LASSERT(body != NULL);
1855 if (body->valid & OBD_MD_MDS) {
1856 struct lu_fid rid = body->fid1;
1857 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1860 tgt = lmv_find_target(lmv, &rid);
1862 ptlrpc_req_finished(*request);
1863 return PTR_ERR(tgt);
1866 op_data->op_fid1 = rid;
1867 op_data->op_valid |= OBD_MD_FLCROSSREF;
1868 op_data->op_namelen = 0;
1869 op_data->op_name = NULL;
1870 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1871 ptlrpc_req_finished(*request);
1878 #define md_op_data_fid(op_data, fl) \
1879 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1880 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1881 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1882 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1885 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1886 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1888 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1889 struct obd_device *obd = exp->exp_obd;
1890 struct lmv_obd *lmv = &obd->u.lmv;
1891 struct lmv_tgt_desc *tgt;
1892 ldlm_policy_data_t policy = {{0}};
1895 if (!fid_is_sane(fid))
1898 tgt = lmv_find_target(lmv, fid);
1900 return PTR_ERR(tgt);
1902 if (tgt->ltd_idx != op_tgt) {
1903 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1904 policy.l_inodebits.bits = bits;
1905 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1906 mode, LCF_ASYNC, NULL);
1909 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1911 op_data->op_flags |= flag;
1919 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1922 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1923 struct ptlrpc_request **request)
1925 struct obd_device *obd = exp->exp_obd;
1926 struct lmv_obd *lmv = &obd->u.lmv;
1927 struct lmv_tgt_desc *tgt;
1930 rc = lmv_check_connect(obd);
1934 LASSERT(op_data->op_namelen != 0);
1936 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1937 PFID(&op_data->op_fid2), op_data->op_namelen,
1938 op_data->op_name, PFID(&op_data->op_fid1));
1940 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1941 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1942 op_data->op_cap = cfs_curproc_cap_pack();
1943 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1945 return PTR_ERR(tgt);
1948 * Cancel UPDATE lock on child (fid1).
1950 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1951 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
1952 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1956 rc = md_link(tgt->ltd_exp, op_data, request);
1961 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1962 const char *old, int oldlen, const char *new, int newlen,
1963 struct ptlrpc_request **request)
1965 struct obd_device *obd = exp->exp_obd;
1966 struct lmv_obd *lmv = &obd->u.lmv;
1967 struct lmv_tgt_desc *src_tgt;
1968 struct lmv_tgt_desc *tgt_tgt;
1971 LASSERT(oldlen != 0);
1973 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
1974 oldlen, old, PFID(&op_data->op_fid1),
1975 newlen, new, PFID(&op_data->op_fid2));
1977 rc = lmv_check_connect(obd);
1981 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1982 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1983 op_data->op_cap = cfs_curproc_cap_pack();
1984 src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1985 if (IS_ERR(src_tgt))
1986 return PTR_ERR(src_tgt);
1988 tgt_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1989 if (IS_ERR(tgt_tgt))
1990 return PTR_ERR(tgt_tgt);
1992 * LOOKUP lock on src child (fid3) should also be cancelled for
1993 * src_tgt in mdc_rename.
1995 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1998 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2001 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2002 LCK_EX, MDS_INODELOCK_UPDATE,
2003 MF_MDC_CANCEL_FID2);
2006 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2009 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2010 LCK_EX, MDS_INODELOCK_LOOKUP,
2011 MF_MDC_CANCEL_FID4);
2015 * Cancel all the locks on tgt child (fid4).
2018 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2019 LCK_EX, MDS_INODELOCK_FULL,
2020 MF_MDC_CANCEL_FID4);
2023 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2024 new, newlen, request);
2028 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2029 void *ea, int ealen, void *ea2, int ea2len,
2030 struct ptlrpc_request **request,
2031 struct md_open_data **mod)
2033 struct obd_device *obd = exp->exp_obd;
2034 struct lmv_obd *lmv = &obd->u.lmv;
2035 struct lmv_tgt_desc *tgt;
2038 rc = lmv_check_connect(obd);
2042 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2043 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2045 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2046 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2048 return PTR_ERR(tgt);
2050 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2051 ea2len, request, mod);
2056 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2057 struct obd_capa *oc, struct ptlrpc_request **request)
2059 struct obd_device *obd = exp->exp_obd;
2060 struct lmv_obd *lmv = &obd->u.lmv;
2061 struct lmv_tgt_desc *tgt;
2064 rc = lmv_check_connect(obd);
2068 tgt = lmv_find_target(lmv, fid);
2070 return PTR_ERR(tgt);
2072 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2077 * Adjust a set of pages, each page containing an array of lu_dirpages,
2078 * so that each page can be used as a single logical lu_dirpage.
2080 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
2081 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
2082 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
2083 * value is used as a cookie to request the next lu_dirpage in a
2084 * directory listing that spans multiple pages (two in this example):
2087 * .|--------v------- -----.
2088 * |s|e|f|p|ent|ent| ... |ent|
2089 * '--|-------------- -----' Each CFS_PAGE contains a single
2090 * '------. lu_dirpage.
2091 * .---------v------- -----.
2092 * |s|e|f|p|ent| 0 | ... | 0 |
2093 * '----------------- -----'
2095 * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
2096 * larger than LU_PAGE_SIZE, a single host page may contain multiple
2097 * lu_dirpages. After reading the lu_dirpages from the MDS, the
2098 * ldp_hash_end of the first lu_dirpage refers to the one immediately
2099 * after it in the same CFS_PAGE (arrows simplified for brevity, but
2100 * in general e0==s1, e1==s2, etc.):
2102 * .-------------------- -----.
2103 * |s0|e0|f0|p|ent|ent| ... |ent|
2104 * |---v---------------- -----|
2105 * |s1|e1|f1|p|ent|ent| ... |ent|
2106 * |---v---------------- -----| Here, each CFS_PAGE contains
2107 * ... multiple lu_dirpages.
2108 * |---v---------------- -----|
2109 * |s'|e'|f'|p|ent|ent| ... |ent|
2110 * '---|---------------- -----'
2112 * .----------------------------.
2115 * This structure is transformed into a single logical lu_dirpage as follows:
2117 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
2118 * labeled 'next CFS_PAGE'.
2120 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
2121 * a hash collision with the next page exists.
2123 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
2124 * to the first entry of the next lu_dirpage.
2126 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
2127 static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
2131 for (i = 0; i < ncfspgs; i++) {
2132 struct lu_dirpage *dp = kmap(pages[i]);
2133 struct lu_dirpage *first = dp;
2134 struct lu_dirent *end_dirent = NULL;
2135 struct lu_dirent *ent;
2136 __u64 hash_end = dp->ldp_hash_end;
2137 __u32 flags = dp->ldp_flags;
2139 while (--nlupgs > 0) {
2140 ent = lu_dirent_start(dp);
2141 for (end_dirent = ent; ent != NULL;
2142 end_dirent = ent, ent = lu_dirent_next(ent));
2144 /* Advance dp to next lu_dirpage. */
2145 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2147 /* Check if we've reached the end of the CFS_PAGE. */
2148 if (!((unsigned long)dp & ~CFS_PAGE_MASK))
2151 /* Save the hash and flags of this lu_dirpage. */
2152 hash_end = dp->ldp_hash_end;
2153 flags = dp->ldp_flags;
2155 /* Check if lu_dirpage contains no entries. */
2159 /* Enlarge the end entry lde_reclen from 0 to
2160 * first entry of next lu_dirpage. */
2161 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
2162 end_dirent->lde_reclen =
2163 cpu_to_le16((char *)(dp->ldp_entries) -
2164 (char *)end_dirent);
2167 first->ldp_hash_end = hash_end;
2168 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2169 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2173 LASSERTF(nlupgs == 0, "left = %d", nlupgs);
2176 #define lmv_adjust_dirpages(pages, ncfspgs, nlupgs) do {} while (0)
2177 #endif /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
2179 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2180 struct page **pages, struct ptlrpc_request **request)
2182 struct obd_device *obd = exp->exp_obd;
2183 struct lmv_obd *lmv = &obd->u.lmv;
2184 __u64 offset = op_data->op_offset;
2186 int ncfspgs; /* pages read in PAGE_CACHE_SIZE */
2187 int nlupgs; /* pages read in LU_PAGE_SIZE */
2188 struct lmv_tgt_desc *tgt;
2190 rc = lmv_check_connect(obd);
2194 CDEBUG(D_INODE, "READPAGE at %#llx from "DFID"\n",
2195 offset, PFID(&op_data->op_fid1));
2197 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2199 return PTR_ERR(tgt);
2201 rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2205 ncfspgs = ((*request)->rq_bulk->bd_nob_transferred + PAGE_CACHE_SIZE - 1)
2206 >> PAGE_CACHE_SHIFT;
2207 nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2208 LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2209 LASSERT(ncfspgs > 0 && ncfspgs <= op_data->op_npages);
2211 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", ncfspgs, nlupgs,
2212 op_data->op_npages);
2214 lmv_adjust_dirpages(pages, ncfspgs, nlupgs);
2219 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2220 struct ptlrpc_request **request)
2222 struct obd_device *obd = exp->exp_obd;
2223 struct lmv_obd *lmv = &obd->u.lmv;
2224 struct lmv_tgt_desc *tgt = NULL;
2225 struct mdt_body *body;
2228 rc = lmv_check_connect(obd);
2232 /* Send unlink requests to the MDT where the child is located */
2233 if (likely(!fid_is_zero(&op_data->op_fid2)))
2234 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2236 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2238 return PTR_ERR(tgt);
2240 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2241 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2242 op_data->op_cap = cfs_curproc_cap_pack();
2245 * If child's fid is given, cancel unused locks for it if it is from
2246 * another export than parent.
2248 * LOOKUP lock for child (fid3) should also be cancelled on parent
2249 * tgt_tgt in mdc_unlink().
2251 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2254 * Cancel FULL locks on child (fid3).
2256 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2257 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2262 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2263 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2265 rc = md_unlink(tgt->ltd_exp, op_data, request);
2266 if (rc != 0 && rc != -EREMOTE)
2269 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2273 /* Not cross-ref case, just get out of here. */
2274 if (likely(!(body->valid & OBD_MD_MDS)))
2277 CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2278 exp->exp_obd->obd_name, PFID(&body->fid1));
2280 /* This is a remote object, try remote MDT, Note: it may
2281 * try more than 1 time here, Considering following case
2282 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2283 * 1. Initially A does not know where remote1 is, it send
2284 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2285 * resend unlink RPC to MDT1 (retry 1st time).
2287 * 2. During the unlink RPC in flight,
2288 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2289 * and create new remote1, but on MDT0
2291 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2292 * /mnt/lustre, then lookup get fid of remote1, and find
2293 * it is remote dir again, and replay -EREMOTE again.
2295 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2297 * In theory, it might try unlimited time here, but it should
2298 * be very rare case. */
2299 op_data->op_fid2 = body->fid1;
2300 ptlrpc_req_finished(*request);
2306 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2308 struct lmv_obd *lmv = &obd->u.lmv;
2311 case OBD_CLEANUP_EARLY:
2312 /* XXX: here should be calling obd_precleanup() down to
2315 case OBD_CLEANUP_EXPORTS:
2316 fld_client_proc_fini(&lmv->lmv_fld);
2317 lprocfs_obd_cleanup(obd);
2325 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2326 __u32 keylen, void *key, __u32 *vallen, void *val,
2327 struct lov_stripe_md *lsm)
2329 struct obd_device *obd;
2330 struct lmv_obd *lmv;
2333 obd = class_exp2obd(exp);
2335 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2336 exp->exp_handle.h_cookie);
2341 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2342 struct lmv_tgt_desc *tgt;
2345 rc = lmv_check_connect(obd);
2349 LASSERT(*vallen == sizeof(__u32));
2350 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2353 * All tgts should be connected when this gets called.
2355 if (tgt == NULL || tgt->ltd_exp == NULL)
2358 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2363 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2364 KEY_IS(KEY_DEFAULT_EASIZE) ||
2365 KEY_IS(KEY_MAX_COOKIESIZE) ||
2366 KEY_IS(KEY_DEFAULT_COOKIESIZE) ||
2367 KEY_IS(KEY_CONN_DATA)) {
2368 rc = lmv_check_connect(obd);
2373 * Forwarding this request to first MDS, it should know LOV
2376 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2378 if (!rc && KEY_IS(KEY_CONN_DATA))
2379 exp->exp_connect_data = *(struct obd_connect_data *)val;
2381 } else if (KEY_IS(KEY_TGT_COUNT)) {
2382 *((int *)val) = lmv->desc.ld_tgt_count;
2386 CDEBUG(D_IOCTL, "Invalid key\n");
2390 static int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2391 u32 keylen, void *key, u32 vallen,
2392 void *val, struct ptlrpc_request_set *set)
2394 struct lmv_tgt_desc *tgt;
2395 struct obd_device *obd;
2396 struct lmv_obd *lmv;
2399 obd = class_exp2obd(exp);
2401 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2402 exp->exp_handle.h_cookie);
2407 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2410 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2413 if (tgt == NULL || tgt->ltd_exp == NULL)
2416 err = obd_set_info_async(env, tgt->ltd_exp,
2417 keylen, key, vallen, val, set);
2428 static int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2429 struct lov_stripe_md *lsm)
2431 struct obd_device *obd = class_exp2obd(exp);
2432 struct lmv_obd *lmv = &obd->u.lmv;
2433 struct lmv_stripe_md *meap;
2434 struct lmv_stripe_md *lsmp;
2438 mea_size = lmv_get_easize(lmv);
2442 if (*lmmp && !lsm) {
2443 OBD_FREE_LARGE(*lmmp, mea_size);
2448 if (*lmmp == NULL) {
2449 OBD_ALLOC_LARGE(*lmmp, mea_size);
2457 lsmp = (struct lmv_stripe_md *)lsm;
2458 meap = (struct lmv_stripe_md *)*lmmp;
2460 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2461 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2464 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2465 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2466 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2468 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2469 meap->mea_ids[i] = lsmp->mea_ids[i];
2470 fid_cpu_to_le(&meap->mea_ids[i], &lsmp->mea_ids[i]);
2476 static int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2477 struct lov_mds_md *lmm, int lmm_size)
2479 struct obd_device *obd = class_exp2obd(exp);
2480 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2481 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2482 struct lmv_obd *lmv = &obd->u.lmv;
2487 mea_size = lmv_get_easize(lmv);
2491 if (*lsmp != NULL && lmm == NULL) {
2492 OBD_FREE_LARGE(*tmea, mea_size);
2497 LASSERT(mea_size == lmm_size);
2499 OBD_ALLOC_LARGE(*tmea, mea_size);
2506 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2507 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2508 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT) {
2509 magic = le32_to_cpu(mea->mea_magic);
2512 * Old mea is not handled here.
2514 CERROR("Old not supportable EA is found\n");
2518 (*tmea)->mea_magic = magic;
2519 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2520 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2522 for (i = 0; i < (*tmea)->mea_count; i++) {
2523 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2524 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2529 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2530 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2531 ldlm_cancel_flags_t flags, void *opaque)
2533 struct obd_device *obd = exp->exp_obd;
2534 struct lmv_obd *lmv = &obd->u.lmv;
2539 LASSERT(fid != NULL);
2541 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2542 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL ||
2543 lmv->tgts[i]->ltd_active == 0)
2546 err = md_cancel_unused(lmv->tgts[i]->ltd_exp, fid,
2547 policy, mode, flags, opaque);
2554 static int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2557 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2560 rc = md_set_lock_data(lmv->tgts[0]->ltd_exp, lockh, data, bits);
2564 static ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2565 const struct lu_fid *fid, ldlm_type_t type,
2566 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2567 struct lustre_handle *lockh)
2569 struct obd_device *obd = exp->exp_obd;
2570 struct lmv_obd *lmv = &obd->u.lmv;
2574 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2577 * With CMD every object can have two locks in different namespaces:
2578 * lookup lock in space of mds storing direntry and update/open lock in
2579 * space of mds storing inode. Thus we check all targets, not only that
2580 * one fid was created in.
2582 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2583 if (lmv->tgts[i] == NULL ||
2584 lmv->tgts[i]->ltd_exp == NULL ||
2585 lmv->tgts[i]->ltd_active == 0)
2588 rc = md_lock_match(lmv->tgts[i]->ltd_exp, flags, fid,
2589 type, policy, mode, lockh);
2597 static int lmv_get_lustre_md(struct obd_export *exp,
2598 struct ptlrpc_request *req,
2599 struct obd_export *dt_exp,
2600 struct obd_export *md_exp,
2601 struct lustre_md *md)
2603 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2605 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2608 static int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2610 struct obd_device *obd = exp->exp_obd;
2611 struct lmv_obd *lmv = &obd->u.lmv;
2614 obd_free_memmd(exp, (void *)&md->mea);
2615 return md_free_lustre_md(lmv->tgts[0]->ltd_exp, md);
2618 static int lmv_set_open_replay_data(struct obd_export *exp,
2619 struct obd_client_handle *och,
2620 struct lookup_intent *it)
2622 struct obd_device *obd = exp->exp_obd;
2623 struct lmv_obd *lmv = &obd->u.lmv;
2624 struct lmv_tgt_desc *tgt;
2626 tgt = lmv_find_target(lmv, &och->och_fid);
2628 return PTR_ERR(tgt);
2630 return md_set_open_replay_data(tgt->ltd_exp, och, it);
2633 static int lmv_clear_open_replay_data(struct obd_export *exp,
2634 struct obd_client_handle *och)
2636 struct obd_device *obd = exp->exp_obd;
2637 struct lmv_obd *lmv = &obd->u.lmv;
2638 struct lmv_tgt_desc *tgt;
2640 tgt = lmv_find_target(lmv, &och->och_fid);
2642 return PTR_ERR(tgt);
2644 return md_clear_open_replay_data(tgt->ltd_exp, och);
2647 static int lmv_get_remote_perm(struct obd_export *exp,
2648 const struct lu_fid *fid,
2649 struct obd_capa *oc, __u32 suppgid,
2650 struct ptlrpc_request **request)
2652 struct obd_device *obd = exp->exp_obd;
2653 struct lmv_obd *lmv = &obd->u.lmv;
2654 struct lmv_tgt_desc *tgt;
2657 rc = lmv_check_connect(obd);
2661 tgt = lmv_find_target(lmv, fid);
2663 return PTR_ERR(tgt);
2665 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2669 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2672 struct obd_device *obd = exp->exp_obd;
2673 struct lmv_obd *lmv = &obd->u.lmv;
2674 struct lmv_tgt_desc *tgt;
2677 rc = lmv_check_connect(obd);
2681 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2683 return PTR_ERR(tgt);
2685 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2689 static int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2690 const struct req_msg_field *field,
2691 struct obd_capa **oc)
2693 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2695 return md_unpack_capa(lmv->tgts[0]->ltd_exp, req, field, oc);
2698 static int lmv_intent_getattr_async(struct obd_export *exp,
2699 struct md_enqueue_info *minfo,
2700 struct ldlm_enqueue_info *einfo)
2702 struct md_op_data *op_data = &minfo->mi_data;
2703 struct obd_device *obd = exp->exp_obd;
2704 struct lmv_obd *lmv = &obd->u.lmv;
2705 struct lmv_tgt_desc *tgt = NULL;
2708 rc = lmv_check_connect(obd);
2712 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2714 return PTR_ERR(tgt);
2716 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2720 static int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2721 struct lu_fid *fid, __u64 *bits)
2723 struct obd_device *obd = exp->exp_obd;
2724 struct lmv_obd *lmv = &obd->u.lmv;
2725 struct lmv_tgt_desc *tgt;
2728 rc = lmv_check_connect(obd);
2732 tgt = lmv_find_target(lmv, fid);
2734 return PTR_ERR(tgt);
2736 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2741 * For lmv, only need to send request to master MDT, and the master MDT will
2742 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2743 * we directly fetch data from the slave MDTs.
2745 static int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2746 struct obd_quotactl *oqctl)
2748 struct obd_device *obd = class_exp2obd(exp);
2749 struct lmv_obd *lmv = &obd->u.lmv;
2750 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2752 __u64 curspace, curinodes;
2754 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
2755 CERROR("master lmv inactive\n");
2759 if (oqctl->qc_cmd != Q_GETOQUOTA) {
2760 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2764 curspace = curinodes = 0;
2765 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2769 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
2771 if (!tgt->ltd_active) {
2772 CDEBUG(D_HA, "mdt %d is inactive.\n", i);
2776 err = obd_quotactl(tgt->ltd_exp, oqctl);
2778 CERROR("getquota on mdt %d failed. %d\n", i, err);
2782 curspace += oqctl->qc_dqblk.dqb_curspace;
2783 curinodes += oqctl->qc_dqblk.dqb_curinodes;
2786 oqctl->qc_dqblk.dqb_curspace = curspace;
2787 oqctl->qc_dqblk.dqb_curinodes = curinodes;
2792 static int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
2793 struct obd_quotactl *oqctl)
2795 struct obd_device *obd = class_exp2obd(exp);
2796 struct lmv_obd *lmv = &obd->u.lmv;
2797 struct lmv_tgt_desc *tgt;
2800 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2803 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
2804 CERROR("lmv idx %d inactive\n", i);
2808 err = obd_quotacheck(tgt->ltd_exp, oqctl);
2816 static struct obd_ops lmv_obd_ops = {
2817 .o_owner = THIS_MODULE,
2818 .o_setup = lmv_setup,
2819 .o_cleanup = lmv_cleanup,
2820 .o_precleanup = lmv_precleanup,
2821 .o_process_config = lmv_process_config,
2822 .o_connect = lmv_connect,
2823 .o_disconnect = lmv_disconnect,
2824 .o_statfs = lmv_statfs,
2825 .o_get_info = lmv_get_info,
2826 .o_set_info_async = lmv_set_info_async,
2827 .o_packmd = lmv_packmd,
2828 .o_unpackmd = lmv_unpackmd,
2829 .o_notify = lmv_notify,
2830 .o_get_uuid = lmv_get_uuid,
2831 .o_iocontrol = lmv_iocontrol,
2832 .o_quotacheck = lmv_quotacheck,
2833 .o_quotactl = lmv_quotactl
2836 static struct md_ops lmv_md_ops = {
2837 .m_getstatus = lmv_getstatus,
2838 .m_null_inode = lmv_null_inode,
2839 .m_find_cbdata = lmv_find_cbdata,
2840 .m_close = lmv_close,
2841 .m_create = lmv_create,
2842 .m_done_writing = lmv_done_writing,
2843 .m_enqueue = lmv_enqueue,
2844 .m_getattr = lmv_getattr,
2845 .m_getxattr = lmv_getxattr,
2846 .m_getattr_name = lmv_getattr_name,
2847 .m_intent_lock = lmv_intent_lock,
2849 .m_rename = lmv_rename,
2850 .m_setattr = lmv_setattr,
2851 .m_setxattr = lmv_setxattr,
2853 .m_readpage = lmv_readpage,
2854 .m_unlink = lmv_unlink,
2855 .m_init_ea_size = lmv_init_ea_size,
2856 .m_cancel_unused = lmv_cancel_unused,
2857 .m_set_lock_data = lmv_set_lock_data,
2858 .m_lock_match = lmv_lock_match,
2859 .m_get_lustre_md = lmv_get_lustre_md,
2860 .m_free_lustre_md = lmv_free_lustre_md,
2861 .m_set_open_replay_data = lmv_set_open_replay_data,
2862 .m_clear_open_replay_data = lmv_clear_open_replay_data,
2863 .m_renew_capa = lmv_renew_capa,
2864 .m_unpack_capa = lmv_unpack_capa,
2865 .m_get_remote_perm = lmv_get_remote_perm,
2866 .m_intent_getattr_async = lmv_intent_getattr_async,
2867 .m_revalidate_lock = lmv_revalidate_lock
2870 static int __init lmv_init(void)
2872 struct lprocfs_static_vars lvars;
2875 lprocfs_lmv_init_vars(&lvars);
2877 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2878 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2882 static void lmv_exit(void)
2884 class_unregister_type(LUSTRE_LMV_NAME);
2887 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2888 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2889 MODULE_LICENSE("GPL");
2891 module_init(lmv_init);
2892 module_exit(lmv_exit);