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 <asm/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)
101 GOTO(out_lmv_lock, rc = -EINVAL);
103 obd = class_exp2obd(tgt->ltd_exp);
105 GOTO(out_lmv_lock, rc = -ENOTCONN);
107 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
108 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
109 obd->obd_type->typ_name, i);
110 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
112 if (tgt->ltd_active == activate) {
113 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
114 activate ? "" : "in");
115 GOTO(out_lmv_lock, rc);
118 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
119 activate ? "" : "in");
120 lmv_activate_target(lmv, tgt, activate);
123 spin_unlock(&lmv->lmv_lock);
127 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
129 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
131 return obd_get_uuid(lmv->tgts[0]->ltd_exp);
134 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
135 enum obd_notify_event ev, void *data)
137 struct obd_connect_data *conn_data;
138 struct lmv_obd *lmv = &obd->u.lmv;
139 struct obd_uuid *uuid;
142 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
143 CERROR("unexpected notification of %s %s!\n",
144 watched->obd_type->typ_name,
149 uuid = &watched->u.cli.cl_target_uuid;
150 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
152 * Set MDC as active before notifying the observer, so the
153 * observer can use the MDC normally.
155 rc = lmv_set_mdc_active(lmv, uuid,
156 ev == OBD_NOTIFY_ACTIVE);
158 CERROR("%sactivation of %s failed: %d\n",
159 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
163 } else if (ev == OBD_NOTIFY_OCD) {
164 conn_data = &watched->u.cli.cl_import->imp_connect_data;
166 * XXX: Make sure that ocd_connect_flags from all targets are
167 * the same. Otherwise one of MDTs runs wrong version or
168 * something like this. --umka
170 obd->obd_self_export->exp_connect_data = *conn_data;
173 else if (ev == OBD_NOTIFY_DISCON) {
175 * For disconnect event, flush fld cache for failout MDS case.
177 fld_client_flush(&lmv->lmv_fld);
181 * Pass the notification up the chain.
183 if (obd->obd_observer)
184 rc = obd_notify(obd->obd_observer, watched, ev, data);
190 * This is fake connect function. Its purpose is to initialize lmv and say
191 * caller that everything is okay. Real connection will be performed later.
193 static int lmv_connect(const struct lu_env *env,
194 struct obd_export **exp, struct obd_device *obd,
195 struct obd_uuid *cluuid, struct obd_connect_data *data,
198 struct proc_dir_entry *lmv_proc_dir;
199 struct lmv_obd *lmv = &obd->u.lmv;
200 struct lustre_handle conn = { 0 };
204 * We don't want to actually do the underlying connections more than
205 * once, so keep track.
208 if (lmv->refcount > 1) {
213 rc = class_connect(&conn, obd, cluuid);
215 CERROR("class_connection() returned %d\n", rc);
219 *exp = class_conn2export(&conn);
220 class_export_get(*exp);
224 lmv->cluuid = *cluuid;
227 lmv->conn_data = *data;
229 if (obd->obd_proc_private != NULL) {
230 lmv_proc_dir = obd->obd_proc_private;
232 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
234 if (IS_ERR(lmv_proc_dir)) {
235 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
236 obd->obd_type->typ_name, obd->obd_name);
239 obd->obd_proc_private = lmv_proc_dir;
243 * All real clients should perform actual connection right away, because
244 * it is possible, that LMV will not have opportunity to connect targets
245 * and MDC stuff will be called directly, for instance while reading
246 * ../mdc/../kbytesfree procfs file, etc.
248 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
249 rc = lmv_check_connect(obd);
251 if (rc && lmv_proc_dir) {
252 lprocfs_remove(&lmv_proc_dir);
253 obd->obd_proc_private = NULL;
259 static void lmv_set_timeouts(struct obd_device *obd)
261 struct lmv_tgt_desc *tgt;
266 if (lmv->server_timeout == 0)
269 if (lmv->connected == 0)
272 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
274 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
277 obd_set_info_async(NULL, tgt->ltd_exp, sizeof(KEY_INTERMDS),
278 KEY_INTERMDS, 0, NULL, NULL);
282 static int lmv_init_ea_size(struct obd_export *exp, int easize,
283 int def_easize, int cookiesize, int def_cookiesize)
285 struct obd_device *obd = exp->exp_obd;
286 struct lmv_obd *lmv = &obd->u.lmv;
291 if (lmv->max_easize < easize) {
292 lmv->max_easize = easize;
295 if (lmv->max_def_easize < def_easize) {
296 lmv->max_def_easize = def_easize;
299 if (lmv->max_cookiesize < cookiesize) {
300 lmv->max_cookiesize = cookiesize;
303 if (lmv->max_def_cookiesize < def_cookiesize) {
304 lmv->max_def_cookiesize = def_cookiesize;
310 if (lmv->connected == 0)
313 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
314 if (lmv->tgts[i] == NULL ||
315 lmv->tgts[i]->ltd_exp == NULL ||
316 lmv->tgts[i]->ltd_active == 0) {
317 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
321 rc = md_init_ea_size(lmv->tgts[i]->ltd_exp, easize, def_easize,
322 cookiesize, def_cookiesize);
324 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
325 " rc = %d.\n", obd->obd_name, i, rc);
332 #define MAX_STRING_SIZE 128
334 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
336 struct proc_dir_entry *lmv_proc_dir;
337 struct lmv_obd *lmv = &obd->u.lmv;
338 struct obd_uuid *cluuid = &lmv->cluuid;
339 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
340 struct obd_device *mdc_obd;
341 struct obd_export *mdc_exp;
342 struct lu_fld_target target;
345 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
348 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
352 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
353 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
354 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
357 if (!mdc_obd->obd_set_up) {
358 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
362 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
363 &lmv->conn_data, NULL);
365 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
370 * Init fid sequence client for this mdc and add new fld target.
372 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
376 target.ft_srv = NULL;
377 target.ft_exp = mdc_exp;
378 target.ft_idx = tgt->ltd_idx;
380 fld_client_add_target(&lmv->lmv_fld, &target);
382 rc = obd_register_observer(mdc_obd, obd);
384 obd_disconnect(mdc_exp);
385 CERROR("target %s register_observer error %d\n",
386 tgt->ltd_uuid.uuid, rc);
390 if (obd->obd_observer) {
392 * Tell the observer about the new target.
394 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
396 (void *)(tgt - lmv->tgts[0]));
398 obd_disconnect(mdc_exp);
404 tgt->ltd_exp = mdc_exp;
405 lmv->desc.ld_active_tgt_count++;
407 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize,
408 lmv->max_cookiesize, lmv->max_def_cookiesize);
410 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
411 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
412 atomic_read(&obd->obd_refcount));
414 lmv_proc_dir = obd->obd_proc_private;
416 struct proc_dir_entry *mdc_symlink;
418 LASSERT(mdc_obd->obd_type != NULL);
419 LASSERT(mdc_obd->obd_type->typ_name != NULL);
420 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
423 mdc_obd->obd_type->typ_name,
425 if (mdc_symlink == NULL) {
426 CERROR("Could not register LMV target "
427 "/proc/fs/lustre/%s/%s/target_obds/%s.",
428 obd->obd_type->typ_name, obd->obd_name,
430 lprocfs_remove(&lmv_proc_dir);
431 obd->obd_proc_private = NULL;
437 static void lmv_del_target(struct lmv_obd *lmv, int index)
439 if (lmv->tgts[index] == NULL)
442 OBD_FREE_PTR(lmv->tgts[index]);
443 lmv->tgts[index] = NULL;
447 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
448 __u32 index, int gen)
450 struct lmv_obd *lmv = &obd->u.lmv;
451 struct lmv_tgt_desc *tgt;
454 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
458 if (lmv->desc.ld_tgt_count == 0) {
459 struct obd_device *mdc_obd;
461 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
464 lmv_init_unlock(lmv);
465 CERROR("%s: Target %s not attached: rc = %d\n",
466 obd->obd_name, uuidp->uuid, -EINVAL);
471 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
472 tgt = lmv->tgts[index];
473 CERROR("%s: UUID %s already assigned at LOV target index %d:"
474 " rc = %d\n", obd->obd_name,
475 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
476 lmv_init_unlock(lmv);
480 if (index >= lmv->tgts_size) {
481 /* We need to reallocate the lmv target array. */
482 struct lmv_tgt_desc **newtgts, **old = NULL;
486 while (newsize < index + 1)
487 newsize = newsize << 1;
488 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
489 if (newtgts == NULL) {
490 lmv_init_unlock(lmv);
494 if (lmv->tgts_size) {
495 memcpy(newtgts, lmv->tgts,
496 sizeof(*newtgts) * lmv->tgts_size);
498 oldsize = lmv->tgts_size;
502 lmv->tgts_size = newsize;
505 OBD_FREE(old, sizeof(*old) * oldsize);
507 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
513 lmv_init_unlock(lmv);
517 mutex_init(&tgt->ltd_fid_mutex);
518 tgt->ltd_idx = index;
519 tgt->ltd_uuid = *uuidp;
521 lmv->tgts[index] = tgt;
522 if (index >= lmv->desc.ld_tgt_count)
523 lmv->desc.ld_tgt_count = index + 1;
525 if (lmv->connected) {
526 rc = lmv_connect_mdc(obd, tgt);
528 spin_lock(&lmv->lmv_lock);
529 lmv->desc.ld_tgt_count--;
530 memset(tgt, 0, sizeof(*tgt));
531 spin_unlock(&lmv->lmv_lock);
533 int easize = sizeof(struct lmv_stripe_md) +
534 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
535 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
539 lmv_init_unlock(lmv);
543 int lmv_check_connect(struct obd_device *obd)
545 struct lmv_obd *lmv = &obd->u.lmv;
546 struct lmv_tgt_desc *tgt;
555 if (lmv->connected) {
556 lmv_init_unlock(lmv);
560 if (lmv->desc.ld_tgt_count == 0) {
561 lmv_init_unlock(lmv);
562 CERROR("%s: no targets configured.\n", obd->obd_name);
566 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
567 lmv->cluuid.uuid, obd->obd_name);
569 LASSERT(lmv->tgts != NULL);
571 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
575 rc = lmv_connect_mdc(obd, tgt);
580 lmv_set_timeouts(obd);
581 class_export_put(lmv->exp);
583 easize = lmv_get_easize(lmv);
584 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
585 lmv_init_unlock(lmv);
596 --lmv->desc.ld_active_tgt_count;
597 rc2 = obd_disconnect(tgt->ltd_exp);
599 CERROR("LMV target %s disconnect on "
600 "MDC idx %d: error %d\n",
601 tgt->ltd_uuid.uuid, i, rc2);
605 class_disconnect(lmv->exp);
606 lmv_init_unlock(lmv);
610 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
612 struct proc_dir_entry *lmv_proc_dir;
613 struct lmv_obd *lmv = &obd->u.lmv;
614 struct obd_device *mdc_obd;
617 LASSERT(tgt != NULL);
618 LASSERT(obd != NULL);
620 mdc_obd = class_exp2obd(tgt->ltd_exp);
623 mdc_obd->obd_force = obd->obd_force;
624 mdc_obd->obd_fail = obd->obd_fail;
625 mdc_obd->obd_no_recov = obd->obd_no_recov;
628 lmv_proc_dir = obd->obd_proc_private;
630 lprocfs_remove_proc_entry(mdc_obd->obd_name, lmv_proc_dir);
632 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
634 CERROR("Can't finalize fids factory\n");
636 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
637 tgt->ltd_exp->exp_obd->obd_name,
638 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
640 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
641 rc = obd_disconnect(tgt->ltd_exp);
643 if (tgt->ltd_active) {
644 CERROR("Target %s disconnect error %d\n",
645 tgt->ltd_uuid.uuid, rc);
649 lmv_activate_target(lmv, tgt, 0);
654 static int lmv_disconnect(struct obd_export *exp)
656 struct obd_device *obd = class_exp2obd(exp);
657 struct lmv_obd *lmv = &obd->u.lmv;
665 * Only disconnect the underlying layers on the final disconnect.
668 if (lmv->refcount != 0)
671 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
672 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
675 lmv_disconnect_mdc(obd, lmv->tgts[i]);
678 if (obd->obd_proc_private)
679 lprocfs_remove((struct proc_dir_entry **)&obd->obd_proc_private);
681 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
682 obd->obd_type->typ_name, obd->obd_name);
686 * This is the case when no real connection is established by
687 * lmv_check_connect().
690 class_export_put(exp);
691 rc = class_disconnect(exp);
692 if (lmv->refcount == 0)
697 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
699 struct obd_device *obddev = class_exp2obd(exp);
700 struct lmv_obd *lmv = &obddev->u.lmv;
701 struct getinfo_fid2path *gf;
702 struct lmv_tgt_desc *tgt;
703 struct getinfo_fid2path *remote_gf = NULL;
704 int remote_gf_size = 0;
707 gf = (struct getinfo_fid2path *)karg;
708 tgt = lmv_find_target(lmv, &gf->gf_fid);
713 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
714 if (rc != 0 && rc != -EREMOTE)
715 GOTO(out_fid2path, rc);
717 /* If remote_gf != NULL, it means just building the
718 * path on the remote MDT, copy this path segment to gf */
719 if (remote_gf != NULL) {
720 struct getinfo_fid2path *ori_gf;
723 ori_gf = (struct getinfo_fid2path *)karg;
724 if (strlen(ori_gf->gf_path) +
725 strlen(gf->gf_path) > ori_gf->gf_pathlen)
726 GOTO(out_fid2path, rc = -EOVERFLOW);
728 ptr = ori_gf->gf_path;
730 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
731 strlen(ori_gf->gf_path));
733 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
734 ptr += strlen(gf->gf_path);
738 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
739 tgt->ltd_exp->exp_obd->obd_name,
740 gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
744 GOTO(out_fid2path, rc);
746 /* sigh, has to go to another MDT to do path building further */
747 if (remote_gf == NULL) {
748 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
749 OBD_ALLOC(remote_gf, remote_gf_size);
750 if (remote_gf == NULL)
751 GOTO(out_fid2path, rc = -ENOMEM);
752 remote_gf->gf_pathlen = PATH_MAX;
755 if (!fid_is_sane(&gf->gf_fid)) {
756 CERROR("%s: invalid FID "DFID": rc = %d\n",
757 tgt->ltd_exp->exp_obd->obd_name,
758 PFID(&gf->gf_fid), -EINVAL);
759 GOTO(out_fid2path, rc = -EINVAL);
762 tgt = lmv_find_target(lmv, &gf->gf_fid);
764 GOTO(out_fid2path, rc = -EINVAL);
766 remote_gf->gf_fid = gf->gf_fid;
767 remote_gf->gf_recno = -1;
768 remote_gf->gf_linkno = -1;
769 memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
771 goto repeat_fid2path;
774 if (remote_gf != NULL)
775 OBD_FREE(remote_gf, remote_gf_size);
779 static int lmv_hsm_req_count(struct lmv_obd *lmv,
780 const struct hsm_user_request *hur,
781 const struct lmv_tgt_desc *tgt_mds)
784 struct lmv_tgt_desc *curr_tgt;
786 /* count how many requests must be sent to the given target */
787 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
788 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
789 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
795 static void lmv_hsm_req_build(struct lmv_obd *lmv,
796 struct hsm_user_request *hur_in,
797 const struct lmv_tgt_desc *tgt_mds,
798 struct hsm_user_request *hur_out)
801 struct lmv_tgt_desc *curr_tgt;
803 /* build the hsm_user_request for the given target */
804 hur_out->hur_request = hur_in->hur_request;
806 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
807 curr_tgt = lmv_find_target(lmv,
808 &hur_in->hur_user_item[i].hui_fid);
809 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
810 hur_out->hur_user_item[nr_out] =
811 hur_in->hur_user_item[i];
815 hur_out->hur_request.hr_itemcount = nr_out;
816 memcpy(hur_data(hur_out), hur_data(hur_in),
817 hur_in->hur_request.hr_data_len);
820 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
821 struct lustre_kernelcomm *lk, void *uarg)
825 /* unregister request (call from llapi_hsm_copytool_fini) */
826 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
827 /* best effort: try to clean as much as possible
828 * (continue on error) */
829 obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len, lk, uarg);
832 /* Whatever the result, remove copytool from kuc groups.
833 * Unreached coordinators will get EPIPE on next requests
834 * and will unregister automatically.
836 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
840 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
841 struct lustre_kernelcomm *lk, void *uarg)
846 bool any_set = false;
848 /* All or nothing: try to register to all MDS.
849 * In case of failure, unregister from previous MDS,
850 * except if it because of inactive target. */
851 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
852 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
855 if (lmv->tgts[i]->ltd_active) {
856 /* permanent error */
857 CERROR("error: iocontrol MDC %s on MDT"
858 "idx %d cmd %x: err = %d\n",
859 lmv->tgts[i]->ltd_uuid.uuid,
862 lk->lk_flags |= LK_FLG_STOP;
863 /* unregister from previous MDS */
864 for (j = 0; j < i; j++)
866 lmv->tgts[j]->ltd_exp,
870 /* else: transient error.
871 * kuc will register to the missing MDT
879 /* no registration done: return error */
882 /* at least one registration done, with no failure */
883 filp = fget(lk->lk_wfd);
887 rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, lk->lk_data);
888 if (rc != 0 && filp != NULL)
896 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
897 int len, void *karg, void *uarg)
899 struct obd_device *obddev = class_exp2obd(exp);
900 struct lmv_obd *lmv = &obddev->u.lmv;
904 int count = lmv->desc.ld_tgt_count;
910 case IOC_OBD_STATFS: {
911 struct obd_ioctl_data *data = karg;
912 struct obd_device *mdc_obd;
913 struct obd_statfs stat_buf = {0};
916 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
917 if ((index >= count))
920 if (lmv->tgts[index] == NULL ||
921 lmv->tgts[index]->ltd_active == 0)
924 mdc_obd = class_exp2obd(lmv->tgts[index]->ltd_exp);
929 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
930 min((int) data->ioc_plen2,
931 (int) sizeof(struct obd_uuid))))
934 rc = obd_statfs(NULL, lmv->tgts[index]->ltd_exp, &stat_buf,
935 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
939 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
940 min((int) data->ioc_plen1,
941 (int) sizeof(stat_buf))))
945 case OBD_IOC_QUOTACTL: {
946 struct if_quotactl *qctl = karg;
947 struct lmv_tgt_desc *tgt = NULL;
948 struct obd_quotactl *oqctl;
950 if (qctl->qc_valid == QC_MDTIDX) {
951 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
954 tgt = lmv->tgts[qctl->qc_idx];
955 if (tgt == NULL || tgt->ltd_exp == NULL)
957 } else if (qctl->qc_valid == QC_UUID) {
958 for (i = 0; i < count; i++) {
962 if (!obd_uuid_equals(&tgt->ltd_uuid,
966 if (tgt->ltd_exp == NULL)
978 LASSERT(tgt && tgt->ltd_exp);
979 OBD_ALLOC_PTR(oqctl);
983 QCTL_COPY(oqctl, qctl);
984 rc = obd_quotactl(tgt->ltd_exp, oqctl);
986 QCTL_COPY(qctl, oqctl);
987 qctl->qc_valid = QC_MDTIDX;
988 qctl->obd_uuid = tgt->ltd_uuid;
993 case OBD_IOC_CHANGELOG_SEND:
994 case OBD_IOC_CHANGELOG_CLEAR: {
995 struct ioc_changelog *icc = karg;
997 if (icc->icc_mdtindex >= count)
1000 if (lmv->tgts[icc->icc_mdtindex] == NULL ||
1001 lmv->tgts[icc->icc_mdtindex]->ltd_exp == NULL ||
1002 lmv->tgts[icc->icc_mdtindex]->ltd_active == 0)
1004 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex]->ltd_exp,
1005 sizeof(*icc), icc, NULL);
1008 case LL_IOC_GET_CONNECT_FLAGS: {
1009 if (lmv->tgts[0] == NULL)
1011 rc = obd_iocontrol(cmd, lmv->tgts[0]->ltd_exp, len, karg, uarg);
1014 case OBD_IOC_FID2PATH: {
1015 rc = lmv_fid2path(exp, len, karg, uarg);
1018 case LL_IOC_HSM_STATE_GET:
1019 case LL_IOC_HSM_STATE_SET:
1020 case LL_IOC_HSM_ACTION: {
1021 struct md_op_data *op_data = karg;
1022 struct lmv_tgt_desc *tgt;
1024 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1026 return PTR_ERR(tgt);
1028 if (tgt->ltd_exp == NULL)
1031 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1034 case LL_IOC_HSM_PROGRESS: {
1035 const struct hsm_progress_kernel *hpk = karg;
1036 struct lmv_tgt_desc *tgt;
1038 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1040 return PTR_ERR(tgt);
1041 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1044 case LL_IOC_HSM_REQUEST: {
1045 struct hsm_user_request *hur = karg;
1046 struct lmv_tgt_desc *tgt;
1047 unsigned int reqcount = hur->hur_request.hr_itemcount;
1052 /* if the request is about a single fid
1053 * or if there is a single MDS, no need to split
1055 if (reqcount == 1 || count == 1) {
1056 tgt = lmv_find_target(lmv,
1057 &hur->hur_user_item[0].hui_fid);
1059 return PTR_ERR(tgt);
1060 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1062 /* split fid list to their respective MDS */
1063 for (i = 0; i < count; i++) {
1064 unsigned int nr, reqlen;
1066 struct hsm_user_request *req;
1068 nr = lmv_hsm_req_count(lmv, hur, lmv->tgts[i]);
1069 if (nr == 0) /* nothing for this MDS */
1072 /* build a request with fids for this MDS */
1073 reqlen = offsetof(typeof(*hur),
1075 + hur->hur_request.hr_data_len;
1076 OBD_ALLOC_LARGE(req, reqlen);
1080 lmv_hsm_req_build(lmv, hur, lmv->tgts[i], req);
1082 rc1 = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
1084 if (rc1 != 0 && rc == 0)
1086 OBD_FREE_LARGE(req, reqlen);
1091 case LL_IOC_LOV_SWAP_LAYOUTS: {
1092 struct md_op_data *op_data = karg;
1093 struct lmv_tgt_desc *tgt1, *tgt2;
1095 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1097 return PTR_ERR(tgt1);
1099 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1101 return PTR_ERR(tgt2);
1103 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1106 /* only files on same MDT can have their layouts swapped */
1107 if (tgt1->ltd_idx != tgt2->ltd_idx)
1110 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1113 case LL_IOC_HSM_CT_START: {
1114 struct lustre_kernelcomm *lk = karg;
1115 if (lk->lk_flags & LK_FLG_STOP)
1116 rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1118 rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1122 for (i = 0; i < count; i++) {
1123 struct obd_device *mdc_obd;
1126 if (lmv->tgts[i] == NULL ||
1127 lmv->tgts[i]->ltd_exp == NULL)
1129 /* ll_umount_begin() sets force flag but for lmv, not
1130 * mdc. Let's pass it through */
1131 mdc_obd = class_exp2obd(lmv->tgts[i]->ltd_exp);
1132 mdc_obd->obd_force = obddev->obd_force;
1133 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len,
1135 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1138 if (lmv->tgts[i]->ltd_active) {
1139 CERROR("error: iocontrol MDC %s on MDT"
1140 "idx %d cmd %x: err = %d\n",
1141 lmv->tgts[i]->ltd_uuid.uuid,
1156 static int lmv_all_chars_policy(int count, const char *name,
1167 static int lmv_nid_policy(struct lmv_obd *lmv)
1169 struct obd_import *imp;
1173 * XXX: To get nid we assume that underlying obd device is mdc.
1175 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1176 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1177 return id % lmv->desc.ld_tgt_count;
1180 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1181 placement_policy_t placement)
1183 switch (placement) {
1184 case PLACEMENT_CHAR_POLICY:
1185 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1187 op_data->op_namelen);
1188 case PLACEMENT_NID_POLICY:
1189 return lmv_nid_policy(lmv);
1195 CERROR("Unsupported placement policy %x\n", placement);
1201 * This is _inode_ placement policy function (not name).
1203 static int lmv_placement_policy(struct obd_device *obd,
1204 struct md_op_data *op_data,
1207 struct lmv_obd *lmv = &obd->u.lmv;
1209 LASSERT(mds != NULL);
1211 if (lmv->desc.ld_tgt_count == 1) {
1217 * If stripe_offset is provided during setdirstripe
1218 * (setdirstripe -i xx), xx MDS will be chosen.
1220 if (op_data->op_cli_flags & CLI_SET_MEA) {
1221 struct lmv_user_md *lum;
1223 lum = (struct lmv_user_md *)op_data->op_data;
1224 if (lum->lum_type == LMV_STRIPE_TYPE &&
1225 lum->lum_stripe_offset != -1) {
1226 if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
1227 CERROR("%s: Stripe_offset %d > MDT count %d:"
1228 " rc = %d\n", obd->obd_name,
1229 lum->lum_stripe_offset,
1230 lmv->desc.ld_tgt_count, -ERANGE);
1233 *mds = lum->lum_stripe_offset;
1238 /* Allocate new fid on target according to operation type and parent
1240 *mds = op_data->op_mds;
1244 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
1247 struct lmv_tgt_desc *tgt;
1250 tgt = lmv_get_target(lmv, mds);
1252 return PTR_ERR(tgt);
1255 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1256 * on server that seq in new allocated fid is not yet known.
1258 mutex_lock(&tgt->ltd_fid_mutex);
1260 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1261 GOTO(out, rc = -ENODEV);
1264 * Asking underlaying tgt layer to allocate new fid.
1266 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1268 LASSERT(fid_is_sane(fid));
1273 mutex_unlock(&tgt->ltd_fid_mutex);
1277 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1278 struct md_op_data *op_data)
1280 struct obd_device *obd = class_exp2obd(exp);
1281 struct lmv_obd *lmv = &obd->u.lmv;
1285 LASSERT(op_data != NULL);
1286 LASSERT(fid != NULL);
1288 rc = lmv_placement_policy(obd, op_data, &mds);
1290 CERROR("Can't get target for allocating fid, "
1295 rc = __lmv_fid_alloc(lmv, fid, mds);
1297 CERROR("Can't alloc new fid, rc %d\n", rc);
1304 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1306 struct lmv_obd *lmv = &obd->u.lmv;
1307 struct lprocfs_static_vars lvars;
1308 struct lmv_desc *desc;
1311 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1312 CERROR("LMV setup requires a descriptor\n");
1316 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1317 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1318 CERROR("Lmv descriptor size wrong: %d > %d\n",
1319 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1323 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1324 if (lmv->tgts == NULL)
1326 lmv->tgts_size = 32;
1328 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1329 lmv->desc.ld_tgt_count = 0;
1330 lmv->desc.ld_active_tgt_count = 0;
1331 lmv->max_cookiesize = 0;
1332 lmv->max_def_easize = 0;
1333 lmv->max_easize = 0;
1334 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1336 spin_lock_init(&lmv->lmv_lock);
1337 mutex_init(&lmv->init_mutex);
1339 lprocfs_lmv_init_vars(&lvars);
1341 lprocfs_obd_setup(obd, lvars.obd_vars);
1342 #if defined (CONFIG_PROC_FS)
1344 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1345 0444, &lmv_proc_target_fops, obd);
1347 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1351 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1352 LUSTRE_CLI_FLD_HASH_DHT);
1354 CERROR("Can't init FLD, err %d\n", rc);
1364 static int lmv_cleanup(struct obd_device *obd)
1366 struct lmv_obd *lmv = &obd->u.lmv;
1368 fld_client_fini(&lmv->lmv_fld);
1369 if (lmv->tgts != NULL) {
1371 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1372 if (lmv->tgts[i] == NULL)
1374 lmv_del_target(lmv, i);
1376 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1382 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1384 struct lustre_cfg *lcfg = buf;
1385 struct obd_uuid obd_uuid;
1390 switch (lcfg->lcfg_command) {
1392 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1393 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1394 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1395 GOTO(out, rc = -EINVAL);
1397 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1399 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1400 GOTO(out, rc = -EINVAL);
1401 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1402 GOTO(out, rc = -EINVAL);
1403 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1406 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1407 GOTO(out, rc = -EINVAL);
1413 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1414 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1416 struct obd_device *obd = class_exp2obd(exp);
1417 struct lmv_obd *lmv = &obd->u.lmv;
1418 struct obd_statfs *temp;
1422 rc = lmv_check_connect(obd);
1426 OBD_ALLOC(temp, sizeof(*temp));
1430 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1431 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1434 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1437 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1438 lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1440 GOTO(out_free_temp, rc);
1445 /* If the statfs is from mount, it will needs
1446 * retrieve necessary information from MDT0.
1447 * i.e. mount does not need the merged osfs
1449 * And also clients can be mounted as long as
1450 * MDT0 is in service*/
1451 if (flags & OBD_STATFS_FOR_MDT0)
1452 GOTO(out_free_temp, rc);
1454 osfs->os_bavail += temp->os_bavail;
1455 osfs->os_blocks += temp->os_blocks;
1456 osfs->os_ffree += temp->os_ffree;
1457 osfs->os_files += temp->os_files;
1462 OBD_FREE(temp, sizeof(*temp));
1466 static int lmv_getstatus(struct obd_export *exp,
1468 struct obd_capa **pc)
1470 struct obd_device *obd = exp->exp_obd;
1471 struct lmv_obd *lmv = &obd->u.lmv;
1474 rc = lmv_check_connect(obd);
1478 rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1482 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1483 struct obd_capa *oc, obd_valid valid, const char *name,
1484 const char *input, int input_size, int output_size,
1485 int flags, struct ptlrpc_request **request)
1487 struct obd_device *obd = exp->exp_obd;
1488 struct lmv_obd *lmv = &obd->u.lmv;
1489 struct lmv_tgt_desc *tgt;
1492 rc = lmv_check_connect(obd);
1496 tgt = lmv_find_target(lmv, fid);
1498 return PTR_ERR(tgt);
1500 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1501 input_size, output_size, flags, request);
1506 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1507 struct obd_capa *oc, obd_valid valid, const char *name,
1508 const char *input, int input_size, int output_size,
1509 int flags, __u32 suppgid,
1510 struct ptlrpc_request **request)
1512 struct obd_device *obd = exp->exp_obd;
1513 struct lmv_obd *lmv = &obd->u.lmv;
1514 struct lmv_tgt_desc *tgt;
1517 rc = lmv_check_connect(obd);
1521 tgt = lmv_find_target(lmv, fid);
1523 return PTR_ERR(tgt);
1525 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1526 input_size, output_size, flags, suppgid,
1532 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1533 struct ptlrpc_request **request)
1535 struct obd_device *obd = exp->exp_obd;
1536 struct lmv_obd *lmv = &obd->u.lmv;
1537 struct lmv_tgt_desc *tgt;
1540 rc = lmv_check_connect(obd);
1544 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1546 return PTR_ERR(tgt);
1548 if (op_data->op_flags & MF_GET_MDT_IDX) {
1549 op_data->op_mds = tgt->ltd_idx;
1553 rc = md_getattr(tgt->ltd_exp, op_data, request);
1558 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1560 struct obd_device *obd = exp->exp_obd;
1561 struct lmv_obd *lmv = &obd->u.lmv;
1565 rc = lmv_check_connect(obd);
1569 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1572 * With DNE every object can have two locks in different namespaces:
1573 * lookup lock in space of MDT storing direntry and update/open lock in
1574 * space of MDT storing inode.
1576 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1577 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1579 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1585 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1586 ldlm_iterator_t it, void *data)
1588 struct obd_device *obd = exp->exp_obd;
1589 struct lmv_obd *lmv = &obd->u.lmv;
1593 rc = lmv_check_connect(obd);
1597 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1600 * With DNE every object can have two locks in different namespaces:
1601 * lookup lock in space of MDT storing direntry and update/open lock in
1602 * space of MDT storing inode.
1604 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1605 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1607 rc = md_find_cbdata(lmv->tgts[i]->ltd_exp, fid, it, data);
1616 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1617 struct md_open_data *mod, struct ptlrpc_request **request)
1619 struct obd_device *obd = exp->exp_obd;
1620 struct lmv_obd *lmv = &obd->u.lmv;
1621 struct lmv_tgt_desc *tgt;
1624 rc = lmv_check_connect(obd);
1628 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1630 return PTR_ERR(tgt);
1632 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1633 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1638 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1641 struct lmv_tgt_desc *tgt;
1643 tgt = lmv_find_target(lmv, fid);
1647 op_data->op_mds = tgt->ltd_idx;
1652 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1653 const void *data, int datalen, int mode, __u32 uid,
1654 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1655 struct ptlrpc_request **request)
1657 struct obd_device *obd = exp->exp_obd;
1658 struct lmv_obd *lmv = &obd->u.lmv;
1659 struct lmv_tgt_desc *tgt;
1662 rc = lmv_check_connect(obd);
1666 if (!lmv->desc.ld_active_tgt_count)
1669 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1671 return PTR_ERR(tgt);
1673 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1677 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1678 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1681 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1682 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1683 cap_effective, rdev, request);
1686 if (*request == NULL)
1688 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1693 static int lmv_done_writing(struct obd_export *exp,
1694 struct md_op_data *op_data,
1695 struct md_open_data *mod)
1697 struct obd_device *obd = exp->exp_obd;
1698 struct lmv_obd *lmv = &obd->u.lmv;
1699 struct lmv_tgt_desc *tgt;
1702 rc = lmv_check_connect(obd);
1706 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1708 return PTR_ERR(tgt);
1710 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1715 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1716 struct lookup_intent *it, struct md_op_data *op_data,
1717 struct lustre_handle *lockh, void *lmm, int lmmsize,
1718 __u64 extra_lock_flags)
1720 struct ptlrpc_request *req = it->d.lustre.it_data;
1721 struct obd_device *obd = exp->exp_obd;
1722 struct lmv_obd *lmv = &obd->u.lmv;
1723 struct lustre_handle plock;
1724 struct lmv_tgt_desc *tgt;
1725 struct md_op_data *rdata;
1727 struct mdt_body *body;
1731 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1732 LASSERT(body != NULL);
1734 if (!(body->valid & OBD_MD_MDS))
1737 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1738 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1741 * We got LOOKUP lock, but we really need attrs.
1743 pmode = it->d.lustre.it_lock_mode;
1744 LASSERT(pmode != 0);
1745 memcpy(&plock, lockh, sizeof(plock));
1746 it->d.lustre.it_lock_mode = 0;
1747 it->d.lustre.it_data = NULL;
1750 ptlrpc_req_finished(req);
1752 tgt = lmv_find_target(lmv, &fid1);
1754 GOTO(out, rc = PTR_ERR(tgt));
1756 OBD_ALLOC_PTR(rdata);
1758 GOTO(out, rc = -ENOMEM);
1760 rdata->op_fid1 = fid1;
1761 rdata->op_bias = MDS_CROSS_REF;
1763 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1764 lmm, lmmsize, NULL, extra_lock_flags);
1765 OBD_FREE_PTR(rdata);
1767 ldlm_lock_decref(&plock, pmode);
1772 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1773 struct lookup_intent *it, struct md_op_data *op_data,
1774 struct lustre_handle *lockh, void *lmm, int lmmsize,
1775 struct ptlrpc_request **req, __u64 extra_lock_flags)
1777 struct obd_device *obd = exp->exp_obd;
1778 struct lmv_obd *lmv = &obd->u.lmv;
1779 struct lmv_tgt_desc *tgt;
1782 rc = lmv_check_connect(obd);
1786 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1787 LL_IT2STR(it), PFID(&op_data->op_fid1));
1789 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1791 return PTR_ERR(tgt);
1793 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1794 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1796 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1797 lmm, lmmsize, req, extra_lock_flags);
1799 if (rc == 0 && it && it->it_op == IT_OPEN) {
1800 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1801 lmm, lmmsize, extra_lock_flags);
1807 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1808 struct ptlrpc_request **request)
1810 struct ptlrpc_request *req = NULL;
1811 struct obd_device *obd = exp->exp_obd;
1812 struct lmv_obd *lmv = &obd->u.lmv;
1813 struct lmv_tgt_desc *tgt;
1814 struct mdt_body *body;
1817 rc = lmv_check_connect(obd);
1821 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1823 return PTR_ERR(tgt);
1825 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1826 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1829 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1833 body = req_capsule_server_get(&(*request)->rq_pill,
1835 LASSERT(body != NULL);
1837 if (body->valid & OBD_MD_MDS) {
1838 struct lu_fid rid = body->fid1;
1839 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1842 tgt = lmv_find_target(lmv, &rid);
1844 ptlrpc_req_finished(*request);
1845 return PTR_ERR(tgt);
1848 op_data->op_fid1 = rid;
1849 op_data->op_valid |= OBD_MD_FLCROSSREF;
1850 op_data->op_namelen = 0;
1851 op_data->op_name = NULL;
1852 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1853 ptlrpc_req_finished(*request);
1860 #define md_op_data_fid(op_data, fl) \
1861 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1862 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1863 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1864 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1867 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1868 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1870 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1871 struct obd_device *obd = exp->exp_obd;
1872 struct lmv_obd *lmv = &obd->u.lmv;
1873 struct lmv_tgt_desc *tgt;
1874 ldlm_policy_data_t policy = {{0}};
1877 if (!fid_is_sane(fid))
1880 tgt = lmv_find_target(lmv, fid);
1882 return PTR_ERR(tgt);
1884 if (tgt->ltd_idx != op_tgt) {
1885 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1886 policy.l_inodebits.bits = bits;
1887 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1888 mode, LCF_ASYNC, NULL);
1891 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1893 op_data->op_flags |= flag;
1901 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1904 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1905 struct ptlrpc_request **request)
1907 struct obd_device *obd = exp->exp_obd;
1908 struct lmv_obd *lmv = &obd->u.lmv;
1909 struct lmv_tgt_desc *tgt;
1912 rc = lmv_check_connect(obd);
1916 LASSERT(op_data->op_namelen != 0);
1918 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1919 PFID(&op_data->op_fid2), op_data->op_namelen,
1920 op_data->op_name, PFID(&op_data->op_fid1));
1922 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1923 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1924 op_data->op_cap = cfs_curproc_cap_pack();
1925 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1927 return PTR_ERR(tgt);
1930 * Cancel UPDATE lock on child (fid1).
1932 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1933 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
1934 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1938 rc = md_link(tgt->ltd_exp, op_data, request);
1943 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1944 const char *old, int oldlen, const char *new, int newlen,
1945 struct ptlrpc_request **request)
1947 struct obd_device *obd = exp->exp_obd;
1948 struct lmv_obd *lmv = &obd->u.lmv;
1949 struct lmv_tgt_desc *src_tgt;
1950 struct lmv_tgt_desc *tgt_tgt;
1953 LASSERT(oldlen != 0);
1955 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
1956 oldlen, old, PFID(&op_data->op_fid1),
1957 newlen, new, PFID(&op_data->op_fid2));
1959 rc = lmv_check_connect(obd);
1963 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1964 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1965 op_data->op_cap = cfs_curproc_cap_pack();
1966 src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1967 if (IS_ERR(src_tgt))
1968 return PTR_ERR(src_tgt);
1970 tgt_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1971 if (IS_ERR(tgt_tgt))
1972 return PTR_ERR(tgt_tgt);
1974 * LOOKUP lock on src child (fid3) should also be cancelled for
1975 * src_tgt in mdc_rename.
1977 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1980 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
1983 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
1984 LCK_EX, MDS_INODELOCK_UPDATE,
1985 MF_MDC_CANCEL_FID2);
1988 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
1991 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
1992 LCK_EX, MDS_INODELOCK_LOOKUP,
1993 MF_MDC_CANCEL_FID4);
1997 * Cancel all the locks on tgt child (fid4).
2000 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2001 LCK_EX, MDS_INODELOCK_FULL,
2002 MF_MDC_CANCEL_FID4);
2005 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2006 new, newlen, request);
2010 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2011 void *ea, int ealen, void *ea2, int ea2len,
2012 struct ptlrpc_request **request,
2013 struct md_open_data **mod)
2015 struct obd_device *obd = exp->exp_obd;
2016 struct lmv_obd *lmv = &obd->u.lmv;
2017 struct lmv_tgt_desc *tgt;
2020 rc = lmv_check_connect(obd);
2024 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2025 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2027 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2028 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2030 return PTR_ERR(tgt);
2032 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2033 ea2len, request, mod);
2038 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2039 struct obd_capa *oc, struct ptlrpc_request **request)
2041 struct obd_device *obd = exp->exp_obd;
2042 struct lmv_obd *lmv = &obd->u.lmv;
2043 struct lmv_tgt_desc *tgt;
2046 rc = lmv_check_connect(obd);
2050 tgt = lmv_find_target(lmv, fid);
2052 return PTR_ERR(tgt);
2054 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2059 * Adjust a set of pages, each page containing an array of lu_dirpages,
2060 * so that each page can be used as a single logical lu_dirpage.
2062 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
2063 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
2064 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
2065 * value is used as a cookie to request the next lu_dirpage in a
2066 * directory listing that spans multiple pages (two in this example):
2069 * .|--------v------- -----.
2070 * |s|e|f|p|ent|ent| ... |ent|
2071 * '--|-------------- -----' Each CFS_PAGE contains a single
2072 * '------. lu_dirpage.
2073 * .---------v------- -----.
2074 * |s|e|f|p|ent| 0 | ... | 0 |
2075 * '----------------- -----'
2077 * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
2078 * larger than LU_PAGE_SIZE, a single host page may contain multiple
2079 * lu_dirpages. After reading the lu_dirpages from the MDS, the
2080 * ldp_hash_end of the first lu_dirpage refers to the one immediately
2081 * after it in the same CFS_PAGE (arrows simplified for brevity, but
2082 * in general e0==s1, e1==s2, etc.):
2084 * .-------------------- -----.
2085 * |s0|e0|f0|p|ent|ent| ... |ent|
2086 * |---v---------------- -----|
2087 * |s1|e1|f1|p|ent|ent| ... |ent|
2088 * |---v---------------- -----| Here, each CFS_PAGE contains
2089 * ... multiple lu_dirpages.
2090 * |---v---------------- -----|
2091 * |s'|e'|f'|p|ent|ent| ... |ent|
2092 * '---|---------------- -----'
2094 * .----------------------------.
2097 * This structure is transformed into a single logical lu_dirpage as follows:
2099 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
2100 * labeled 'next CFS_PAGE'.
2102 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
2103 * a hash collision with the next page exists.
2105 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
2106 * to the first entry of the next lu_dirpage.
2108 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
2109 static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
2113 for (i = 0; i < ncfspgs; i++) {
2114 struct lu_dirpage *dp = kmap(pages[i]);
2115 struct lu_dirpage *first = dp;
2116 struct lu_dirent *end_dirent = NULL;
2117 struct lu_dirent *ent;
2118 __u64 hash_end = dp->ldp_hash_end;
2119 __u32 flags = dp->ldp_flags;
2121 while (--nlupgs > 0) {
2122 ent = lu_dirent_start(dp);
2123 for (end_dirent = ent; ent != NULL;
2124 end_dirent = ent, ent = lu_dirent_next(ent));
2126 /* Advance dp to next lu_dirpage. */
2127 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2129 /* Check if we've reached the end of the CFS_PAGE. */
2130 if (!((unsigned long)dp & ~CFS_PAGE_MASK))
2133 /* Save the hash and flags of this lu_dirpage. */
2134 hash_end = dp->ldp_hash_end;
2135 flags = dp->ldp_flags;
2137 /* Check if lu_dirpage contains no entries. */
2141 /* Enlarge the end entry lde_reclen from 0 to
2142 * first entry of next lu_dirpage. */
2143 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
2144 end_dirent->lde_reclen =
2145 cpu_to_le16((char *)(dp->ldp_entries) -
2146 (char *)end_dirent);
2149 first->ldp_hash_end = hash_end;
2150 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2151 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2155 LASSERTF(nlupgs == 0, "left = %d", nlupgs);
2158 #define lmv_adjust_dirpages(pages, ncfspgs, nlupgs) do {} while (0)
2159 #endif /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
2161 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2162 struct page **pages, struct ptlrpc_request **request)
2164 struct obd_device *obd = exp->exp_obd;
2165 struct lmv_obd *lmv = &obd->u.lmv;
2166 __u64 offset = op_data->op_offset;
2168 int ncfspgs; /* pages read in PAGE_CACHE_SIZE */
2169 int nlupgs; /* pages read in LU_PAGE_SIZE */
2170 struct lmv_tgt_desc *tgt;
2172 rc = lmv_check_connect(obd);
2176 CDEBUG(D_INODE, "READPAGE at %#llx from "DFID"\n",
2177 offset, PFID(&op_data->op_fid1));
2179 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2181 return PTR_ERR(tgt);
2183 rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2187 ncfspgs = ((*request)->rq_bulk->bd_nob_transferred + PAGE_CACHE_SIZE - 1)
2188 >> PAGE_CACHE_SHIFT;
2189 nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2190 LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2191 LASSERT(ncfspgs > 0 && ncfspgs <= op_data->op_npages);
2193 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", ncfspgs, nlupgs,
2194 op_data->op_npages);
2196 lmv_adjust_dirpages(pages, ncfspgs, nlupgs);
2201 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2202 struct ptlrpc_request **request)
2204 struct obd_device *obd = exp->exp_obd;
2205 struct lmv_obd *lmv = &obd->u.lmv;
2206 struct lmv_tgt_desc *tgt = NULL;
2207 struct mdt_body *body;
2210 rc = lmv_check_connect(obd);
2214 /* Send unlink requests to the MDT where the child is located */
2215 if (likely(!fid_is_zero(&op_data->op_fid2)))
2216 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2218 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2220 return PTR_ERR(tgt);
2222 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2223 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2224 op_data->op_cap = cfs_curproc_cap_pack();
2227 * If child's fid is given, cancel unused locks for it if it is from
2228 * another export than parent.
2230 * LOOKUP lock for child (fid3) should also be cancelled on parent
2231 * tgt_tgt in mdc_unlink().
2233 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2236 * Cancel FULL locks on child (fid3).
2238 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2239 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2244 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2245 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2247 rc = md_unlink(tgt->ltd_exp, op_data, request);
2248 if (rc != 0 && rc != -EREMOTE)
2251 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2255 /* Not cross-ref case, just get out of here. */
2256 if (likely(!(body->valid & OBD_MD_MDS)))
2259 CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2260 exp->exp_obd->obd_name, PFID(&body->fid1));
2262 /* This is a remote object, try remote MDT, Note: it may
2263 * try more than 1 time here, Considering following case
2264 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2265 * 1. Initially A does not know where remote1 is, it send
2266 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2267 * resend unlink RPC to MDT1 (retry 1st time).
2269 * 2. During the unlink RPC in flight,
2270 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2271 * and create new remote1, but on MDT0
2273 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2274 * /mnt/lustre, then lookup get fid of remote1, and find
2275 * it is remote dir again, and replay -EREMOTE again.
2277 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2279 * In theory, it might try unlimited time here, but it should
2280 * be very rare case. */
2281 op_data->op_fid2 = body->fid1;
2282 ptlrpc_req_finished(*request);
2288 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2290 struct lmv_obd *lmv = &obd->u.lmv;
2294 case OBD_CLEANUP_EARLY:
2295 /* XXX: here should be calling obd_precleanup() down to
2298 case OBD_CLEANUP_EXPORTS:
2299 fld_client_proc_fini(&lmv->lmv_fld);
2300 lprocfs_obd_cleanup(obd);
2308 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2309 __u32 keylen, void *key, __u32 *vallen, void *val,
2310 struct lov_stripe_md *lsm)
2312 struct obd_device *obd;
2313 struct lmv_obd *lmv;
2316 obd = class_exp2obd(exp);
2318 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2319 exp->exp_handle.h_cookie);
2324 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2325 struct lmv_tgt_desc *tgt;
2328 rc = lmv_check_connect(obd);
2332 LASSERT(*vallen == sizeof(__u32));
2333 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2336 * All tgts should be connected when this gets called.
2338 if (tgt == NULL || tgt->ltd_exp == NULL)
2341 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2346 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2347 KEY_IS(KEY_DEFAULT_EASIZE) ||
2348 KEY_IS(KEY_MAX_COOKIESIZE) ||
2349 KEY_IS(KEY_DEFAULT_COOKIESIZE) ||
2350 KEY_IS(KEY_CONN_DATA)) {
2351 rc = lmv_check_connect(obd);
2356 * Forwarding this request to first MDS, it should know LOV
2359 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2361 if (!rc && KEY_IS(KEY_CONN_DATA))
2362 exp->exp_connect_data = *(struct obd_connect_data *)val;
2364 } else if (KEY_IS(KEY_TGT_COUNT)) {
2365 *((int *)val) = lmv->desc.ld_tgt_count;
2369 CDEBUG(D_IOCTL, "Invalid key\n");
2373 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2374 obd_count keylen, void *key, obd_count vallen,
2375 void *val, struct ptlrpc_request_set *set)
2377 struct lmv_tgt_desc *tgt;
2378 struct obd_device *obd;
2379 struct lmv_obd *lmv;
2382 obd = class_exp2obd(exp);
2384 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2385 exp->exp_handle.h_cookie);
2390 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2393 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2396 if (tgt == NULL || tgt->ltd_exp == NULL)
2399 err = obd_set_info_async(env, tgt->ltd_exp,
2400 keylen, key, vallen, val, set);
2411 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2412 struct lov_stripe_md *lsm)
2414 struct obd_device *obd = class_exp2obd(exp);
2415 struct lmv_obd *lmv = &obd->u.lmv;
2416 struct lmv_stripe_md *meap;
2417 struct lmv_stripe_md *lsmp;
2421 mea_size = lmv_get_easize(lmv);
2425 if (*lmmp && !lsm) {
2426 OBD_FREE_LARGE(*lmmp, mea_size);
2431 if (*lmmp == NULL) {
2432 OBD_ALLOC_LARGE(*lmmp, mea_size);
2440 lsmp = (struct lmv_stripe_md *)lsm;
2441 meap = (struct lmv_stripe_md *)*lmmp;
2443 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2444 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2447 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2448 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2449 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2451 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2452 meap->mea_ids[i] = lsmp->mea_ids[i];
2453 fid_cpu_to_le(&meap->mea_ids[i], &lsmp->mea_ids[i]);
2459 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2460 struct lov_mds_md *lmm, int lmm_size)
2462 struct obd_device *obd = class_exp2obd(exp);
2463 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2464 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2465 struct lmv_obd *lmv = &obd->u.lmv;
2470 mea_size = lmv_get_easize(lmv);
2474 if (*lsmp != NULL && lmm == NULL) {
2475 OBD_FREE_LARGE(*tmea, mea_size);
2480 LASSERT(mea_size == lmm_size);
2482 OBD_ALLOC_LARGE(*tmea, mea_size);
2489 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2490 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2491 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2493 magic = le32_to_cpu(mea->mea_magic);
2496 * Old mea is not handled here.
2498 CERROR("Old not supportable EA is found\n");
2502 (*tmea)->mea_magic = magic;
2503 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2504 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2506 for (i = 0; i < (*tmea)->mea_count; i++) {
2507 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2508 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2513 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2514 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2515 ldlm_cancel_flags_t flags, void *opaque)
2517 struct obd_device *obd = exp->exp_obd;
2518 struct lmv_obd *lmv = &obd->u.lmv;
2523 LASSERT(fid != NULL);
2525 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2526 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL ||
2527 lmv->tgts[i]->ltd_active == 0)
2530 err = md_cancel_unused(lmv->tgts[i]->ltd_exp, fid,
2531 policy, mode, flags, opaque);
2538 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2541 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2544 rc = md_set_lock_data(lmv->tgts[0]->ltd_exp, lockh, data, bits);
2548 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2549 const struct lu_fid *fid, ldlm_type_t type,
2550 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2551 struct lustre_handle *lockh)
2553 struct obd_device *obd = exp->exp_obd;
2554 struct lmv_obd *lmv = &obd->u.lmv;
2558 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2561 * With CMD every object can have two locks in different namespaces:
2562 * lookup lock in space of mds storing direntry and update/open lock in
2563 * space of mds storing inode. Thus we check all targets, not only that
2564 * one fid was created in.
2566 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2567 if (lmv->tgts[i] == NULL ||
2568 lmv->tgts[i]->ltd_exp == NULL ||
2569 lmv->tgts[i]->ltd_active == 0)
2572 rc = md_lock_match(lmv->tgts[i]->ltd_exp, flags, fid,
2573 type, policy, mode, lockh);
2581 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2582 struct obd_export *dt_exp, struct obd_export *md_exp,
2583 struct lustre_md *md)
2585 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2587 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2590 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2592 struct obd_device *obd = exp->exp_obd;
2593 struct lmv_obd *lmv = &obd->u.lmv;
2596 obd_free_memmd(exp, (void *)&md->mea);
2597 return md_free_lustre_md(lmv->tgts[0]->ltd_exp, md);
2600 int lmv_set_open_replay_data(struct obd_export *exp,
2601 struct obd_client_handle *och,
2602 struct lookup_intent *it)
2604 struct obd_device *obd = exp->exp_obd;
2605 struct lmv_obd *lmv = &obd->u.lmv;
2606 struct lmv_tgt_desc *tgt;
2608 tgt = lmv_find_target(lmv, &och->och_fid);
2610 return PTR_ERR(tgt);
2612 return md_set_open_replay_data(tgt->ltd_exp, och, it);
2615 int lmv_clear_open_replay_data(struct obd_export *exp,
2616 struct obd_client_handle *och)
2618 struct obd_device *obd = exp->exp_obd;
2619 struct lmv_obd *lmv = &obd->u.lmv;
2620 struct lmv_tgt_desc *tgt;
2622 tgt = lmv_find_target(lmv, &och->och_fid);
2624 return PTR_ERR(tgt);
2626 return md_clear_open_replay_data(tgt->ltd_exp, och);
2629 static int lmv_get_remote_perm(struct obd_export *exp,
2630 const struct lu_fid *fid,
2631 struct obd_capa *oc, __u32 suppgid,
2632 struct ptlrpc_request **request)
2634 struct obd_device *obd = exp->exp_obd;
2635 struct lmv_obd *lmv = &obd->u.lmv;
2636 struct lmv_tgt_desc *tgt;
2639 rc = lmv_check_connect(obd);
2643 tgt = lmv_find_target(lmv, fid);
2645 return PTR_ERR(tgt);
2647 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2651 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2654 struct obd_device *obd = exp->exp_obd;
2655 struct lmv_obd *lmv = &obd->u.lmv;
2656 struct lmv_tgt_desc *tgt;
2659 rc = lmv_check_connect(obd);
2663 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2665 return PTR_ERR(tgt);
2667 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2671 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2672 const struct req_msg_field *field, struct obd_capa **oc)
2674 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2676 return md_unpack_capa(lmv->tgts[0]->ltd_exp, req, field, oc);
2679 int lmv_intent_getattr_async(struct obd_export *exp,
2680 struct md_enqueue_info *minfo,
2681 struct ldlm_enqueue_info *einfo)
2683 struct md_op_data *op_data = &minfo->mi_data;
2684 struct obd_device *obd = exp->exp_obd;
2685 struct lmv_obd *lmv = &obd->u.lmv;
2686 struct lmv_tgt_desc *tgt = NULL;
2689 rc = lmv_check_connect(obd);
2693 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2695 return PTR_ERR(tgt);
2697 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2701 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2702 struct lu_fid *fid, __u64 *bits)
2704 struct obd_device *obd = exp->exp_obd;
2705 struct lmv_obd *lmv = &obd->u.lmv;
2706 struct lmv_tgt_desc *tgt;
2709 rc = lmv_check_connect(obd);
2713 tgt = lmv_find_target(lmv, fid);
2715 return PTR_ERR(tgt);
2717 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2722 * For lmv, only need to send request to master MDT, and the master MDT will
2723 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2724 * we directly fetch data from the slave MDTs.
2726 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2727 struct obd_quotactl *oqctl)
2729 struct obd_device *obd = class_exp2obd(exp);
2730 struct lmv_obd *lmv = &obd->u.lmv;
2731 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2733 __u64 curspace, curinodes;
2735 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
2736 CERROR("master lmv inactive\n");
2740 if (oqctl->qc_cmd != Q_GETOQUOTA) {
2741 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2745 curspace = curinodes = 0;
2746 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2750 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
2752 if (!tgt->ltd_active) {
2753 CDEBUG(D_HA, "mdt %d is inactive.\n", i);
2757 err = obd_quotactl(tgt->ltd_exp, oqctl);
2759 CERROR("getquota on mdt %d failed. %d\n", i, err);
2763 curspace += oqctl->qc_dqblk.dqb_curspace;
2764 curinodes += oqctl->qc_dqblk.dqb_curinodes;
2767 oqctl->qc_dqblk.dqb_curspace = curspace;
2768 oqctl->qc_dqblk.dqb_curinodes = curinodes;
2773 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
2774 struct obd_quotactl *oqctl)
2776 struct obd_device *obd = class_exp2obd(exp);
2777 struct lmv_obd *lmv = &obd->u.lmv;
2778 struct lmv_tgt_desc *tgt;
2781 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2784 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
2785 CERROR("lmv idx %d inactive\n", i);
2789 err = obd_quotacheck(tgt->ltd_exp, oqctl);
2797 struct obd_ops lmv_obd_ops = {
2798 .o_owner = THIS_MODULE,
2799 .o_setup = lmv_setup,
2800 .o_cleanup = lmv_cleanup,
2801 .o_precleanup = lmv_precleanup,
2802 .o_process_config = lmv_process_config,
2803 .o_connect = lmv_connect,
2804 .o_disconnect = lmv_disconnect,
2805 .o_statfs = lmv_statfs,
2806 .o_get_info = lmv_get_info,
2807 .o_set_info_async = lmv_set_info_async,
2808 .o_packmd = lmv_packmd,
2809 .o_unpackmd = lmv_unpackmd,
2810 .o_notify = lmv_notify,
2811 .o_get_uuid = lmv_get_uuid,
2812 .o_iocontrol = lmv_iocontrol,
2813 .o_quotacheck = lmv_quotacheck,
2814 .o_quotactl = lmv_quotactl
2817 struct md_ops lmv_md_ops = {
2818 .m_getstatus = lmv_getstatus,
2819 .m_null_inode = lmv_null_inode,
2820 .m_find_cbdata = lmv_find_cbdata,
2821 .m_close = lmv_close,
2822 .m_create = lmv_create,
2823 .m_done_writing = lmv_done_writing,
2824 .m_enqueue = lmv_enqueue,
2825 .m_getattr = lmv_getattr,
2826 .m_getxattr = lmv_getxattr,
2827 .m_getattr_name = lmv_getattr_name,
2828 .m_intent_lock = lmv_intent_lock,
2830 .m_rename = lmv_rename,
2831 .m_setattr = lmv_setattr,
2832 .m_setxattr = lmv_setxattr,
2834 .m_readpage = lmv_readpage,
2835 .m_unlink = lmv_unlink,
2836 .m_init_ea_size = lmv_init_ea_size,
2837 .m_cancel_unused = lmv_cancel_unused,
2838 .m_set_lock_data = lmv_set_lock_data,
2839 .m_lock_match = lmv_lock_match,
2840 .m_get_lustre_md = lmv_get_lustre_md,
2841 .m_free_lustre_md = lmv_free_lustre_md,
2842 .m_set_open_replay_data = lmv_set_open_replay_data,
2843 .m_clear_open_replay_data = lmv_clear_open_replay_data,
2844 .m_renew_capa = lmv_renew_capa,
2845 .m_unpack_capa = lmv_unpack_capa,
2846 .m_get_remote_perm = lmv_get_remote_perm,
2847 .m_intent_getattr_async = lmv_intent_getattr_async,
2848 .m_revalidate_lock = lmv_revalidate_lock
2851 int __init lmv_init(void)
2853 struct lprocfs_static_vars lvars;
2856 lprocfs_lmv_init_vars(&lvars);
2858 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2859 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2863 static void lmv_exit(void)
2865 class_unregister_type(LUSTRE_LMV_NAME);
2868 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2869 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2870 MODULE_LICENSE("GPL");
2872 module_init(lmv_init);
2873 module_exit(lmv_exit);