4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LMV
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/pagemap.h>
44 #include <linux/math64.h>
45 #include <linux/seq_file.h>
46 #include <linux/namei.h>
48 #include <lustre/lustre_idl.h>
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_lmv.h>
54 #include <lprocfs_status.h>
55 #include <cl_object.h>
57 #include <lustre_fid.h>
58 #include <lustre_ioctl.h>
59 #include "lmv_internal.h"
61 /* This hash is only for testing purpose */
62 static inline unsigned int
63 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
66 const unsigned char *p = (const unsigned char *)name;
68 while (--namelen >= 0)
76 static inline unsigned int
77 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
81 hash = lustre_hash_fnv_1a_64(name, namelen);
88 int lmv_name_to_stripe_index(__u32 lmv_hash_type, unsigned int stripe_count,
89 const char *name, int namelen)
92 __u32 hash_type = lmv_hash_type & LMV_HASH_TYPE_MASK;
95 if (stripe_count <= 1)
98 /* for migrating object, always start from 0 stripe */
99 if (lmv_hash_type & LMV_HASH_FLAG_MIGRATION)
103 case LMV_HASH_TYPE_ALL_CHARS:
104 idx = lmv_hash_all_chars(stripe_count, name, namelen);
106 case LMV_HASH_TYPE_FNV_1A_64:
107 idx = lmv_hash_fnv1a(stripe_count, name, namelen);
114 CDEBUG(D_INFO, "name %.*s hash_type %d idx %d\n", namelen, name,
120 static void lmv_activate_target(struct lmv_obd *lmv,
121 struct lmv_tgt_desc *tgt,
124 if (tgt->ltd_active == activate)
127 tgt->ltd_active = activate;
128 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
134 * -EINVAL : UUID can't be found in the LMV's target list
135 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
136 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
138 static int lmv_set_mdc_active(struct lmv_obd *lmv,
139 const struct obd_uuid *uuid,
142 struct lmv_tgt_desc *tgt = NULL;
143 struct obd_device *obd;
148 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
149 lmv, uuid->uuid, activate);
151 spin_lock(&lmv->lmv_lock);
152 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
154 if (tgt == NULL || tgt->ltd_exp == NULL)
157 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n", i,
158 tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
160 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
164 if (i == lmv->desc.ld_tgt_count)
165 GOTO(out_lmv_lock, rc = -EINVAL);
167 obd = class_exp2obd(tgt->ltd_exp);
169 GOTO(out_lmv_lock, rc = -ENOTCONN);
171 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
172 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
173 obd->obd_type->typ_name, i);
174 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
176 if (tgt->ltd_active == activate) {
177 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
178 activate ? "" : "in");
179 GOTO(out_lmv_lock, rc);
182 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
183 activate ? "" : "in");
184 lmv_activate_target(lmv, tgt, activate);
188 spin_unlock(&lmv->lmv_lock);
192 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
194 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
195 struct lmv_tgt_desc *tgt = lmv->tgts[0];
197 return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
200 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
201 enum obd_notify_event ev, void *data)
203 struct obd_connect_data *conn_data;
204 struct lmv_obd *lmv = &obd->u.lmv;
205 struct obd_uuid *uuid;
209 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
210 CERROR("unexpected notification of %s %s!\n",
211 watched->obd_type->typ_name,
216 uuid = &watched->u.cli.cl_target_uuid;
217 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
219 * Set MDC as active before notifying the observer, so the
220 * observer can use the MDC normally.
222 rc = lmv_set_mdc_active(lmv, uuid,
223 ev == OBD_NOTIFY_ACTIVE);
225 CERROR("%sactivation of %s failed: %d\n",
226 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
230 } else if (ev == OBD_NOTIFY_OCD) {
231 conn_data = &watched->u.cli.cl_import->imp_connect_data;
233 * XXX: Make sure that ocd_connect_flags from all targets are
234 * the same. Otherwise one of MDTs runs wrong version or
235 * something like this. --umka
237 obd->obd_self_export->exp_connect_data = *conn_data;
240 else if (ev == OBD_NOTIFY_DISCON) {
242 * For disconnect event, flush fld cache for failout MDS case.
244 fld_client_flush(&lmv->lmv_fld);
248 * Pass the notification up the chain.
250 if (obd->obd_observer)
251 rc = obd_notify(obd->obd_observer, watched, ev, data);
257 * This is fake connect function. Its purpose is to initialize lmv and say
258 * caller that everything is okay. Real connection will be performed later.
260 static int lmv_connect(const struct lu_env *env,
261 struct obd_export **exp, struct obd_device *obd,
262 struct obd_uuid *cluuid, struct obd_connect_data *data,
265 struct lmv_obd *lmv = &obd->u.lmv;
266 struct lustre_handle conn = { 0 };
271 * We don't want to actually do the underlying connections more than
272 * once, so keep track.
275 if (lmv->refcount > 1) {
280 rc = class_connect(&conn, obd, cluuid);
282 CERROR("class_connection() returned %d\n", rc);
286 *exp = class_conn2export(&conn);
287 class_export_get(*exp);
291 lmv->cluuid = *cluuid;
294 lmv->conn_data = *data;
296 if (lmv->targets_proc_entry == NULL) {
297 lmv->targets_proc_entry = lprocfs_seq_register("target_obds",
300 if (IS_ERR(lmv->targets_proc_entry)) {
301 CERROR("%s: cannot register "
302 "/proc/fs/lustre/%s/%s/target_obds\n",
303 obd->obd_name, obd->obd_type->typ_name,
305 lmv->targets_proc_entry = NULL;
310 * All real clients should perform actual connection right away, because
311 * it is possible, that LMV will not have opportunity to connect targets
312 * and MDC stuff will be called directly, for instance while reading
313 * ../mdc/../kbytesfree procfs file, etc.
315 if (data != NULL && (data->ocd_connect_flags & OBD_CONNECT_REAL))
316 rc = lmv_check_connect(obd);
318 if (rc && lmv->targets_proc_entry != NULL)
319 lprocfs_remove(&lmv->targets_proc_entry);
323 static int lmv_init_ea_size(struct obd_export *exp, int easize,
324 int def_easize, int cookiesize, int def_cookiesize)
326 struct obd_device *obd = exp->exp_obd;
327 struct lmv_obd *lmv = &obd->u.lmv;
333 if (lmv->max_easize < easize) {
334 lmv->max_easize = easize;
337 if (lmv->max_def_easize < def_easize) {
338 lmv->max_def_easize = def_easize;
341 if (lmv->max_cookiesize < cookiesize) {
342 lmv->max_cookiesize = cookiesize;
345 if (lmv->max_def_cookiesize < def_cookiesize) {
346 lmv->max_def_cookiesize = def_cookiesize;
352 if (lmv->connected == 0)
355 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
356 struct lmv_tgt_desc *tgt = lmv->tgts[i];
358 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
359 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
363 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize,
364 cookiesize, def_cookiesize);
366 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
367 " rc = %d\n", obd->obd_name, i, rc);
374 #define MAX_STRING_SIZE 128
376 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
378 struct lmv_obd *lmv = &obd->u.lmv;
379 struct obd_uuid *cluuid = &lmv->cluuid;
380 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
381 struct obd_device *mdc_obd;
382 struct obd_export *mdc_exp;
383 struct lu_fld_target target;
387 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
390 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
394 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
395 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
396 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
399 if (!mdc_obd->obd_set_up) {
400 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
404 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
405 &lmv->conn_data, NULL);
407 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
412 * Init fid sequence client for this mdc and add new fld target.
414 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
418 target.ft_srv = NULL;
419 target.ft_exp = mdc_exp;
420 target.ft_idx = tgt->ltd_idx;
422 fld_client_add_target(&lmv->lmv_fld, &target);
424 rc = obd_register_observer(mdc_obd, obd);
426 obd_disconnect(mdc_exp);
427 CERROR("target %s register_observer error %d\n",
428 tgt->ltd_uuid.uuid, rc);
432 if (obd->obd_observer) {
434 * Tell the observer about the new target.
436 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
438 (void *)(tgt - lmv->tgts[0]));
440 obd_disconnect(mdc_exp);
446 tgt->ltd_exp = mdc_exp;
447 lmv->desc.ld_active_tgt_count++;
449 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize,
450 lmv->max_cookiesize, lmv->max_def_cookiesize);
452 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
453 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
454 atomic_read(&obd->obd_refcount));
456 if (lmv->targets_proc_entry != NULL) {
457 struct proc_dir_entry *mdc_symlink;
459 LASSERT(mdc_obd->obd_type != NULL);
460 LASSERT(mdc_obd->obd_type->typ_name != NULL);
461 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
462 lmv->targets_proc_entry,
464 mdc_obd->obd_type->typ_name,
466 if (mdc_symlink == NULL) {
467 CERROR("cannot register LMV target "
468 "/proc/fs/lustre/%s/%s/target_obds/%s\n",
469 obd->obd_type->typ_name, obd->obd_name,
476 static void lmv_del_target(struct lmv_obd *lmv, int index)
478 if (lmv->tgts[index] == NULL)
481 OBD_FREE_PTR(lmv->tgts[index]);
482 lmv->tgts[index] = NULL;
486 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
487 __u32 index, int gen)
489 struct lmv_obd *lmv = &obd->u.lmv;
490 struct lmv_tgt_desc *tgt;
491 int orig_tgt_count = 0;
495 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
499 if (lmv->desc.ld_tgt_count == 0) {
500 struct obd_device *mdc_obd;
502 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
505 lmv_init_unlock(lmv);
506 CERROR("%s: Target %s not attached: rc = %d\n",
507 obd->obd_name, uuidp->uuid, -EINVAL);
512 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
513 tgt = lmv->tgts[index];
514 CERROR("%s: UUID %s already assigned at LOV target index %d:"
515 " rc = %d\n", obd->obd_name,
516 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
517 lmv_init_unlock(lmv);
521 if (index >= lmv->tgts_size) {
522 /* We need to reallocate the lmv target array. */
523 struct lmv_tgt_desc **newtgts, **old = NULL;
527 while (newsize < index + 1)
528 newsize = newsize << 1;
529 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
530 if (newtgts == NULL) {
531 lmv_init_unlock(lmv);
535 if (lmv->tgts_size) {
536 memcpy(newtgts, lmv->tgts,
537 sizeof(*newtgts) * lmv->tgts_size);
539 oldsize = lmv->tgts_size;
543 lmv->tgts_size = newsize;
546 OBD_FREE(old, sizeof(*old) * oldsize);
548 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
554 lmv_init_unlock(lmv);
558 mutex_init(&tgt->ltd_fid_mutex);
559 tgt->ltd_idx = index;
560 tgt->ltd_uuid = *uuidp;
562 lmv->tgts[index] = tgt;
563 if (index >= lmv->desc.ld_tgt_count) {
564 orig_tgt_count = lmv->desc.ld_tgt_count;
565 lmv->desc.ld_tgt_count = index + 1;
568 if (lmv->connected) {
569 rc = lmv_connect_mdc(obd, tgt);
571 spin_lock(&lmv->lmv_lock);
572 if (lmv->desc.ld_tgt_count == index + 1)
573 lmv->desc.ld_tgt_count = orig_tgt_count;
574 memset(tgt, 0, sizeof(*tgt));
575 spin_unlock(&lmv->lmv_lock);
577 int easize = sizeof(struct lmv_stripe_md) +
578 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
579 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
583 lmv_init_unlock(lmv);
587 int lmv_check_connect(struct obd_device *obd)
589 struct lmv_obd *lmv = &obd->u.lmv;
590 struct lmv_tgt_desc *tgt;
600 if (lmv->connected) {
601 lmv_init_unlock(lmv);
605 if (lmv->desc.ld_tgt_count == 0) {
606 lmv_init_unlock(lmv);
607 CERROR("%s: no targets configured.\n", obd->obd_name);
611 LASSERT(lmv->tgts != NULL);
613 if (lmv->tgts[0] == NULL) {
614 lmv_init_unlock(lmv);
615 CERROR("%s: no target configured for index 0.\n",
620 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
621 lmv->cluuid.uuid, obd->obd_name);
623 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
627 rc = lmv_connect_mdc(obd, tgt);
632 class_export_put(lmv->exp);
634 easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
635 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
636 lmv_init_unlock(lmv);
647 --lmv->desc.ld_active_tgt_count;
648 rc2 = obd_disconnect(tgt->ltd_exp);
650 CERROR("LMV target %s disconnect on "
651 "MDC idx %d: error %d\n",
652 tgt->ltd_uuid.uuid, i, rc2);
656 class_disconnect(lmv->exp);
657 lmv_init_unlock(lmv);
661 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
663 struct lmv_obd *lmv = &obd->u.lmv;
664 struct obd_device *mdc_obd;
668 LASSERT(tgt != NULL);
669 LASSERT(obd != NULL);
671 mdc_obd = class_exp2obd(tgt->ltd_exp);
674 mdc_obd->obd_force = obd->obd_force;
675 mdc_obd->obd_fail = obd->obd_fail;
676 mdc_obd->obd_no_recov = obd->obd_no_recov;
679 if (lmv->targets_proc_entry != NULL)
680 lprocfs_remove_proc_entry(mdc_obd->obd_name,
681 lmv->targets_proc_entry);
683 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
685 CERROR("Can't finanize fids factory\n");
687 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
688 tgt->ltd_exp->exp_obd->obd_name,
689 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
691 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
692 rc = obd_disconnect(tgt->ltd_exp);
694 if (tgt->ltd_active) {
695 CERROR("Target %s disconnect error %d\n",
696 tgt->ltd_uuid.uuid, rc);
700 lmv_activate_target(lmv, tgt, 0);
705 static int lmv_disconnect(struct obd_export *exp)
707 struct obd_device *obd = class_exp2obd(exp);
708 struct lmv_obd *lmv = &obd->u.lmv;
717 * Only disconnect the underlying layers on the final disconnect.
720 if (lmv->refcount != 0)
723 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
724 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
727 lmv_disconnect_mdc(obd, lmv->tgts[i]);
730 if (lmv->targets_proc_entry != NULL)
731 lprocfs_remove(&lmv->targets_proc_entry);
733 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
734 obd->obd_type->typ_name, obd->obd_name);
738 * This is the case when no real connection is established by
739 * lmv_check_connect().
742 class_export_put(exp);
743 rc = class_disconnect(exp);
744 if (lmv->refcount == 0)
749 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
751 struct obd_device *obddev = class_exp2obd(exp);
752 struct lmv_obd *lmv = &obddev->u.lmv;
753 struct getinfo_fid2path *gf;
754 struct lmv_tgt_desc *tgt;
755 struct getinfo_fid2path *remote_gf = NULL;
756 int remote_gf_size = 0;
759 gf = (struct getinfo_fid2path *)karg;
760 tgt = lmv_find_target(lmv, &gf->gf_fid);
762 RETURN(PTR_ERR(tgt));
765 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
766 if (rc != 0 && rc != -EREMOTE)
767 GOTO(out_fid2path, rc);
769 /* If remote_gf != NULL, it means just building the
770 * path on the remote MDT, copy this path segement to gf */
771 if (remote_gf != NULL) {
772 struct getinfo_fid2path *ori_gf;
775 ori_gf = (struct getinfo_fid2path *)karg;
776 if (strlen(ori_gf->gf_path) +
777 strlen(gf->gf_path) > ori_gf->gf_pathlen)
778 GOTO(out_fid2path, rc = -EOVERFLOW);
780 ptr = ori_gf->gf_path;
782 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
783 strlen(ori_gf->gf_path));
785 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
786 ptr += strlen(gf->gf_path);
790 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: "LPU64" ln: %u\n",
791 tgt->ltd_exp->exp_obd->obd_name,
792 gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
796 GOTO(out_fid2path, rc);
798 /* sigh, has to go to another MDT to do path building further */
799 if (remote_gf == NULL) {
800 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
801 OBD_ALLOC(remote_gf, remote_gf_size);
802 if (remote_gf == NULL)
803 GOTO(out_fid2path, rc = -ENOMEM);
804 remote_gf->gf_pathlen = PATH_MAX;
807 if (!fid_is_sane(&gf->gf_fid)) {
808 CERROR("%s: invalid FID "DFID": rc = %d\n",
809 tgt->ltd_exp->exp_obd->obd_name,
810 PFID(&gf->gf_fid), -EINVAL);
811 GOTO(out_fid2path, rc = -EINVAL);
814 tgt = lmv_find_target(lmv, &gf->gf_fid);
816 GOTO(out_fid2path, rc = -EINVAL);
818 remote_gf->gf_fid = gf->gf_fid;
819 remote_gf->gf_recno = -1;
820 remote_gf->gf_linkno = -1;
821 memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
823 goto repeat_fid2path;
826 if (remote_gf != NULL)
827 OBD_FREE(remote_gf, remote_gf_size);
831 static int lmv_hsm_req_count(struct lmv_obd *lmv,
832 const struct hsm_user_request *hur,
833 const struct lmv_tgt_desc *tgt_mds)
837 struct lmv_tgt_desc *curr_tgt;
839 /* count how many requests must be sent to the given target */
840 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
841 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
842 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
848 static void lmv_hsm_req_build(struct lmv_obd *lmv,
849 struct hsm_user_request *hur_in,
850 const struct lmv_tgt_desc *tgt_mds,
851 struct hsm_user_request *hur_out)
854 struct lmv_tgt_desc *curr_tgt;
856 /* build the hsm_user_request for the given target */
857 hur_out->hur_request = hur_in->hur_request;
859 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
860 curr_tgt = lmv_find_target(lmv,
861 &hur_in->hur_user_item[i].hui_fid);
862 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
863 hur_out->hur_user_item[nr_out] =
864 hur_in->hur_user_item[i];
868 hur_out->hur_request.hr_itemcount = nr_out;
869 memcpy(hur_data(hur_out), hur_data(hur_in),
870 hur_in->hur_request.hr_data_len);
873 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
874 struct lustre_kernelcomm *lk, void *uarg)
878 struct kkuc_ct_data *kcd = NULL;
881 /* unregister request (call from llapi_hsm_copytool_fini) */
882 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
883 struct lmv_tgt_desc *tgt = lmv->tgts[i];
885 if (tgt == NULL || tgt->ltd_exp == NULL)
887 /* best effort: try to clean as much as possible
888 * (continue on error) */
889 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
892 /* Whatever the result, remove copytool from kuc groups.
893 * Unreached coordinators will get EPIPE on next requests
894 * and will unregister automatically.
896 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group, (void **)&kcd);
903 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
904 struct lustre_kernelcomm *lk, void *uarg)
909 bool any_set = false;
910 struct kkuc_ct_data *kcd;
913 /* All or nothing: try to register to all MDS.
914 * In case of failure, unregister from previous MDS,
915 * except if it because of inactive target. */
916 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
917 struct lmv_tgt_desc *tgt = lmv->tgts[i];
919 if (tgt == NULL || tgt->ltd_exp == NULL)
921 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
923 if (tgt->ltd_active) {
924 /* permanent error */
925 CERROR("%s: iocontrol MDC %s on MDT"
926 " idx %d cmd %x: err = %d\n",
927 class_exp2obd(lmv->exp)->obd_name,
928 tgt->ltd_uuid.uuid, i, cmd, err);
930 lk->lk_flags |= LK_FLG_STOP;
931 /* unregister from previous MDS */
932 for (j = 0; j < i; j++) {
934 if (tgt == NULL || tgt->ltd_exp == NULL)
936 obd_iocontrol(cmd, tgt->ltd_exp, len,
941 /* else: transient error.
942 * kuc will register to the missing MDT
950 /* no registration done: return error */
953 /* at least one registration done, with no failure */
954 filp = fget(lk->lk_wfd);
963 kcd->kcd_magic = KKUC_CT_DATA_MAGIC;
964 kcd->kcd_uuid = lmv->cluuid;
965 kcd->kcd_archive = lk->lk_data;
967 rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, kcd);
980 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
981 int len, void *karg, void *uarg)
983 struct obd_device *obddev = class_exp2obd(exp);
984 struct lmv_obd *lmv = &obddev->u.lmv;
985 struct lmv_tgt_desc *tgt = NULL;
989 __u32 count = lmv->desc.ld_tgt_count;
996 case IOC_OBD_STATFS: {
997 struct obd_ioctl_data *data = karg;
998 struct obd_device *mdc_obd;
999 struct obd_statfs stat_buf = {0};
1002 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1003 if ((index >= count))
1006 tgt = lmv->tgts[index];
1007 if (tgt == NULL || !tgt->ltd_active)
1010 mdc_obd = class_exp2obd(tgt->ltd_exp);
1015 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
1016 min((int) data->ioc_plen2,
1017 (int) sizeof(struct obd_uuid))))
1020 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
1021 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1025 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1026 min((int) data->ioc_plen1,
1027 (int) sizeof(stat_buf))))
1031 case OBD_IOC_QUOTACTL: {
1032 struct if_quotactl *qctl = karg;
1033 struct obd_quotactl *oqctl;
1035 if (qctl->qc_valid == QC_MDTIDX) {
1036 if (count <= qctl->qc_idx)
1039 tgt = lmv->tgts[qctl->qc_idx];
1040 if (tgt == NULL || tgt->ltd_exp == NULL)
1042 } else if (qctl->qc_valid == QC_UUID) {
1043 for (i = 0; i < count; i++) {
1047 if (!obd_uuid_equals(&tgt->ltd_uuid,
1051 if (tgt->ltd_exp == NULL)
1063 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
1064 OBD_ALLOC_PTR(oqctl);
1068 QCTL_COPY(oqctl, qctl);
1069 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1071 QCTL_COPY(qctl, oqctl);
1072 qctl->qc_valid = QC_MDTIDX;
1073 qctl->obd_uuid = tgt->ltd_uuid;
1075 OBD_FREE_PTR(oqctl);
1078 case OBD_IOC_CHANGELOG_SEND:
1079 case OBD_IOC_CHANGELOG_CLEAR: {
1080 struct ioc_changelog *icc = karg;
1082 if (icc->icc_mdtindex >= count)
1085 tgt = lmv->tgts[icc->icc_mdtindex];
1086 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
1088 rc = obd_iocontrol(cmd, tgt->ltd_exp, sizeof(*icc), icc, NULL);
1091 case LL_IOC_GET_CONNECT_FLAGS: {
1093 if (tgt == NULL || tgt->ltd_exp == NULL)
1095 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1098 case LL_IOC_FID2MDTIDX: {
1099 struct lu_fid *fid = karg;
1102 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
1106 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
1107 * point to user space memory for FID2MDTIDX. */
1108 *(__u32 *)uarg = mdt_index;
1111 case OBD_IOC_FID2PATH: {
1112 rc = lmv_fid2path(exp, len, karg, uarg);
1115 case LL_IOC_HSM_STATE_GET:
1116 case LL_IOC_HSM_STATE_SET:
1117 case LL_IOC_HSM_ACTION: {
1118 struct md_op_data *op_data = karg;
1120 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1122 RETURN(PTR_ERR(tgt));
1124 if (tgt->ltd_exp == NULL)
1127 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1130 case LL_IOC_HSM_PROGRESS: {
1131 const struct hsm_progress_kernel *hpk = karg;
1133 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1135 RETURN(PTR_ERR(tgt));
1136 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1139 case LL_IOC_HSM_REQUEST: {
1140 struct hsm_user_request *hur = karg;
1141 unsigned int reqcount = hur->hur_request.hr_itemcount;
1146 /* if the request is about a single fid
1147 * or if there is a single MDS, no need to split
1149 if (reqcount == 1 || count == 1) {
1150 tgt = lmv_find_target(lmv,
1151 &hur->hur_user_item[0].hui_fid);
1153 RETURN(PTR_ERR(tgt));
1154 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1156 /* split fid list to their respective MDS */
1157 for (i = 0; i < count; i++) {
1158 unsigned int nr, reqlen;
1160 struct hsm_user_request *req;
1163 if (tgt == NULL || tgt->ltd_exp == NULL)
1166 nr = lmv_hsm_req_count(lmv, hur, tgt);
1167 if (nr == 0) /* nothing for this MDS */
1170 /* build a request with fids for this MDS */
1171 reqlen = offsetof(typeof(*hur),
1173 + hur->hur_request.hr_data_len;
1174 OBD_ALLOC_LARGE(req, reqlen);
1178 lmv_hsm_req_build(lmv, hur, tgt, req);
1180 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1182 if (rc1 != 0 && rc == 0)
1184 OBD_FREE_LARGE(req, reqlen);
1189 case LL_IOC_LOV_SWAP_LAYOUTS: {
1190 struct md_op_data *op_data = karg;
1191 struct lmv_tgt_desc *tgt1, *tgt2;
1193 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1195 RETURN(PTR_ERR(tgt1));
1197 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1199 RETURN(PTR_ERR(tgt2));
1201 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1204 /* only files on same MDT can have their layouts swapped */
1205 if (tgt1->ltd_idx != tgt2->ltd_idx)
1208 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1211 case LL_IOC_HSM_CT_START: {
1212 struct lustre_kernelcomm *lk = karg;
1213 if (lk->lk_flags & LK_FLG_STOP)
1214 rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1216 rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1220 for (i = 0; i < count; i++) {
1221 struct obd_device *mdc_obd;
1225 if (tgt == NULL || tgt->ltd_exp == NULL)
1227 /* ll_umount_begin() sets force flag but for lmv, not
1228 * mdc. Let's pass it through */
1229 mdc_obd = class_exp2obd(tgt->ltd_exp);
1230 mdc_obd->obd_force = obddev->obd_force;
1231 err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1232 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1235 if (tgt->ltd_active) {
1236 CERROR("error: iocontrol MDC %s on MDT"
1237 " idx %d cmd %x: err = %d\n",
1238 tgt->ltd_uuid.uuid, i, cmd, err);
1252 static int lmv_all_chars_policy(int count, const char *name,
1263 static int lmv_nid_policy(struct lmv_obd *lmv)
1265 struct obd_import *imp;
1269 * XXX: To get nid we assume that underlying obd device is mdc.
1271 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1272 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1273 return id % lmv->desc.ld_tgt_count;
1276 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1277 placement_policy_t placement)
1279 switch (placement) {
1280 case PLACEMENT_CHAR_POLICY:
1281 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1283 op_data->op_namelen);
1284 case PLACEMENT_NID_POLICY:
1285 return lmv_nid_policy(lmv);
1291 CERROR("Unsupported placement policy %x\n", placement);
1297 * This is _inode_ placement policy function (not name).
1299 static int lmv_placement_policy(struct obd_device *obd,
1300 struct md_op_data *op_data, u32 *mds)
1302 struct lmv_obd *lmv = &obd->u.lmv;
1305 LASSERT(mds != NULL);
1307 if (lmv->desc.ld_tgt_count == 1) {
1313 * If stripe_offset is provided during setdirstripe
1314 * (setdirstripe -i xx), xx MDS will be choosen.
1316 if (op_data->op_cli_flags & CLI_SET_MEA && op_data->op_data != NULL) {
1317 struct lmv_user_md *lum;
1319 lum = op_data->op_data;
1321 if (le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1322 *mds = le32_to_cpu(lum->lum_stripe_offset);
1324 /* -1 means default, which will be in the same MDT with
1326 *mds = op_data->op_mds;
1327 lum->lum_stripe_offset = cpu_to_le32(op_data->op_mds);
1330 /* Allocate new fid on target according to operation type and
1331 * parent home mds. */
1332 *mds = op_data->op_mds;
1338 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1340 struct lmv_tgt_desc *tgt;
1344 tgt = lmv_get_target(lmv, mds, NULL);
1346 RETURN(PTR_ERR(tgt));
1349 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1350 * on server that seq in new allocated fid is not yet known.
1352 mutex_lock(&tgt->ltd_fid_mutex);
1354 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1355 GOTO(out, rc = -ENODEV);
1358 * Asking underlying tgt layer to allocate new fid.
1360 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1362 LASSERT(fid_is_sane(fid));
1368 mutex_unlock(&tgt->ltd_fid_mutex);
1372 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1373 struct lu_fid *fid, struct md_op_data *op_data)
1375 struct obd_device *obd = class_exp2obd(exp);
1376 struct lmv_obd *lmv = &obd->u.lmv;
1381 LASSERT(op_data != NULL);
1382 LASSERT(fid != NULL);
1384 rc = lmv_placement_policy(obd, op_data, &mds);
1386 CERROR("Can't get target for allocating fid, "
1391 rc = __lmv_fid_alloc(lmv, fid, mds);
1393 CERROR("Can't alloc new fid, rc %d\n", rc);
1400 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1402 struct lmv_obd *lmv = &obd->u.lmv;
1403 struct lmv_desc *desc;
1407 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1408 CERROR("LMV setup requires a descriptor\n");
1412 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1413 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1414 CERROR("Lmv descriptor size wrong: %d > %d\n",
1415 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1419 lmv->tgts_size = 32U;
1420 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1421 if (lmv->tgts == NULL)
1424 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1425 lmv->desc.ld_tgt_count = 0;
1426 lmv->desc.ld_active_tgt_count = 0;
1427 lmv->max_cookiesize = 0;
1428 lmv->max_def_easize = 0;
1429 lmv->max_easize = 0;
1430 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1432 spin_lock_init(&lmv->lmv_lock);
1433 mutex_init(&lmv->init_mutex);
1436 obd->obd_vars = lprocfs_lmv_obd_vars;
1437 lprocfs_obd_setup(obd);
1438 lprocfs_alloc_md_stats(obd, 0);
1439 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1440 0444, &lmv_proc_target_fops, obd);
1442 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1445 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1446 LUSTRE_CLI_FLD_HASH_DHT);
1448 CERROR("Can't init FLD, err %d\n", rc);
1458 static int lmv_cleanup(struct obd_device *obd)
1460 struct lmv_obd *lmv = &obd->u.lmv;
1463 fld_client_fini(&lmv->lmv_fld);
1464 if (lmv->tgts != NULL) {
1466 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1467 if (lmv->tgts[i] == NULL)
1469 lmv_del_target(lmv, i);
1471 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1477 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1479 struct lustre_cfg *lcfg = buf;
1480 struct obd_uuid obd_uuid;
1486 switch (lcfg->lcfg_command) {
1488 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1489 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1490 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1491 GOTO(out, rc = -EINVAL);
1493 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1495 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1496 GOTO(out, rc = -EINVAL);
1497 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1498 GOTO(out, rc = -EINVAL);
1499 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1502 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1503 GOTO(out, rc = -EINVAL);
1509 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1510 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1512 struct obd_device *obd = class_exp2obd(exp);
1513 struct lmv_obd *lmv = &obd->u.lmv;
1514 struct obd_statfs *temp;
1519 rc = lmv_check_connect(obd);
1523 OBD_ALLOC(temp, sizeof(*temp));
1527 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1528 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1531 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1534 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1535 lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1537 GOTO(out_free_temp, rc);
1542 /* If the statfs is from mount, it will needs
1543 * retrieve necessary information from MDT0.
1544 * i.e. mount does not need the merged osfs
1546 * And also clients can be mounted as long as
1547 * MDT0 is in service*/
1548 if (flags & OBD_STATFS_FOR_MDT0)
1549 GOTO(out_free_temp, rc);
1551 osfs->os_bavail += temp->os_bavail;
1552 osfs->os_blocks += temp->os_blocks;
1553 osfs->os_ffree += temp->os_ffree;
1554 osfs->os_files += temp->os_files;
1560 OBD_FREE(temp, sizeof(*temp));
1564 static int lmv_getstatus(struct obd_export *exp,
1566 struct obd_capa **pc)
1568 struct obd_device *obd = exp->exp_obd;
1569 struct lmv_obd *lmv = &obd->u.lmv;
1573 rc = lmv_check_connect(obd);
1577 rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1581 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1582 struct obd_capa *oc, obd_valid valid, const char *name,
1583 const char *input, int input_size, int output_size,
1584 int flags, struct ptlrpc_request **request)
1586 struct obd_device *obd = exp->exp_obd;
1587 struct lmv_obd *lmv = &obd->u.lmv;
1588 struct lmv_tgt_desc *tgt;
1592 rc = lmv_check_connect(obd);
1596 tgt = lmv_find_target(lmv, fid);
1598 RETURN(PTR_ERR(tgt));
1600 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1601 input_size, output_size, flags, request);
1606 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1607 struct obd_capa *oc, obd_valid valid, const char *name,
1608 const char *input, int input_size, int output_size,
1609 int flags, __u32 suppgid,
1610 struct ptlrpc_request **request)
1612 struct obd_device *obd = exp->exp_obd;
1613 struct lmv_obd *lmv = &obd->u.lmv;
1614 struct lmv_tgt_desc *tgt;
1618 rc = lmv_check_connect(obd);
1622 tgt = lmv_find_target(lmv, fid);
1624 RETURN(PTR_ERR(tgt));
1626 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1627 input_size, output_size, flags, suppgid,
1633 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1634 struct ptlrpc_request **request)
1636 struct obd_device *obd = exp->exp_obd;
1637 struct lmv_obd *lmv = &obd->u.lmv;
1638 struct lmv_tgt_desc *tgt;
1642 rc = lmv_check_connect(obd);
1646 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1648 RETURN(PTR_ERR(tgt));
1650 if (op_data->op_flags & MF_GET_MDT_IDX) {
1651 op_data->op_mds = tgt->ltd_idx;
1655 rc = md_getattr(tgt->ltd_exp, op_data, request);
1660 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1662 struct obd_device *obd = exp->exp_obd;
1663 struct lmv_obd *lmv = &obd->u.lmv;
1668 rc = lmv_check_connect(obd);
1672 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1675 * With DNE every object can have two locks in different namespaces:
1676 * lookup lock in space of MDT storing direntry and update/open lock in
1677 * space of MDT storing inode.
1679 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1680 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1682 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1688 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1689 ldlm_iterator_t it, void *data)
1691 struct obd_device *obd = exp->exp_obd;
1692 struct lmv_obd *lmv = &obd->u.lmv;
1698 rc = lmv_check_connect(obd);
1702 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1705 * With DNE every object can have two locks in different namespaces:
1706 * lookup lock in space of MDT storing direntry and update/open lock in
1707 * space of MDT storing inode. Try the MDT that the FID maps to first,
1708 * since this can be easily found, and only try others if that fails.
1710 for (i = 0, tgt = lmv_find_target_index(lmv, fid);
1711 i < lmv->desc.ld_tgt_count;
1712 i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
1714 CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
1715 obd->obd_name, PFID(fid), tgt);
1719 if (lmv->tgts[tgt] == NULL ||
1720 lmv->tgts[tgt]->ltd_exp == NULL)
1723 rc = md_find_cbdata(lmv->tgts[tgt]->ltd_exp, fid, it, data);
1732 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1733 struct md_open_data *mod, struct ptlrpc_request **request)
1735 struct obd_device *obd = exp->exp_obd;
1736 struct lmv_obd *lmv = &obd->u.lmv;
1737 struct lmv_tgt_desc *tgt;
1741 rc = lmv_check_connect(obd);
1745 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1747 RETURN(PTR_ERR(tgt));
1749 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1750 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1755 * Choosing the MDT by name or FID in @op_data.
1756 * For non-striped directory, it will locate MDT by fid.
1757 * For striped-directory, it will locate MDT by name. And also
1758 * it will reset op_fid1 with the FID of the choosen stripe.
1760 struct lmv_tgt_desc *
1761 lmv_locate_target_for_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1762 const char *name, int namelen, struct lu_fid *fid,
1765 struct lmv_tgt_desc *tgt;
1766 const struct lmv_oinfo *oinfo;
1768 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
1770 RETURN(ERR_CAST(oinfo));
1771 *fid = oinfo->lmo_fid;
1772 *mds = oinfo->lmo_mds;
1773 tgt = lmv_get_target(lmv, *mds, NULL);
1775 CDEBUG(D_INFO, "locate on mds %u "DFID"\n", *mds, PFID(fid));
1780 * Locate mds by fid or name
1782 * For striped directory (lsm != NULL), it will locate the stripe
1783 * by name hash (see lsm_name_to_stripe_info()). Note: if the hash_type
1784 * is unknown, it will return -EBADFD, and lmv_intent_lookup might need
1785 * walk through all of stripes to locate the entry.
1787 * For normal direcotry, it will locate MDS by FID directly.
1788 * \param[in] lmv LMV device
1789 * \param[in] op_data client MD stack parameters, name, namelen
1791 * \param[in] fid object FID used to locate MDS.
1793 * retval pointer to the lmv_tgt_desc if succeed.
1794 * ERR_PTR(errno) if failed.
1796 struct lmv_tgt_desc*
1797 lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1800 struct lmv_stripe_md *lsm = op_data->op_mea1;
1801 struct lmv_tgt_desc *tgt;
1803 /* During creating VOLATILE file, it should honor the mdt
1804 * index if the file under striped dir is being restored, see
1806 if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1807 (int)op_data->op_mds != -1 && lsm != NULL) {
1809 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
1813 /* refill the right parent fid */
1814 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1815 struct lmv_oinfo *oinfo;
1817 oinfo = &lsm->lsm_md_oinfo[i];
1818 if (oinfo->lmo_mds == op_data->op_mds) {
1819 *fid = oinfo->lmo_fid;
1824 /* Hmm, can not find the stripe by mdt_index(op_mds) */
1825 if (i == lsm->lsm_md_stripe_count)
1826 tgt = ERR_PTR(-EINVAL);
1831 if (lsm == NULL || op_data->op_namelen == 0) {
1832 tgt = lmv_find_target(lmv, fid);
1836 op_data->op_mds = tgt->ltd_idx;
1840 return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
1841 op_data->op_namelen, fid,
1845 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1846 const void *data, int datalen, int mode, __u32 uid,
1847 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1848 struct ptlrpc_request **request)
1850 struct obd_device *obd = exp->exp_obd;
1851 struct lmv_obd *lmv = &obd->u.lmv;
1852 struct lmv_tgt_desc *tgt;
1856 rc = lmv_check_connect(obd);
1860 if (!lmv->desc.ld_active_tgt_count)
1863 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1865 RETURN(PTR_ERR(tgt));
1867 CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1868 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1871 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1875 /* Send the create request to the MDT where the object
1876 * will be located */
1877 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1879 RETURN(PTR_ERR(tgt));
1881 op_data->op_mds = tgt->ltd_idx;
1883 CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1884 PFID(&op_data->op_fid2), op_data->op_mds);
1886 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1887 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1888 cap_effective, rdev, request);
1890 if (*request == NULL)
1892 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1897 static int lmv_done_writing(struct obd_export *exp,
1898 struct md_op_data *op_data,
1899 struct md_open_data *mod)
1901 struct obd_device *obd = exp->exp_obd;
1902 struct lmv_obd *lmv = &obd->u.lmv;
1903 struct lmv_tgt_desc *tgt;
1907 rc = lmv_check_connect(obd);
1911 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1913 RETURN(PTR_ERR(tgt));
1915 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1920 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1921 const union ldlm_policy_data *policy,
1922 struct lookup_intent *it, struct md_op_data *op_data,
1923 struct lustre_handle *lockh, __u64 extra_lock_flags)
1925 struct obd_device *obd = exp->exp_obd;
1926 struct lmv_obd *lmv = &obd->u.lmv;
1927 struct lmv_tgt_desc *tgt;
1931 rc = lmv_check_connect(obd);
1935 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1936 LL_IT2STR(it), PFID(&op_data->op_fid1));
1938 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1940 RETURN(PTR_ERR(tgt));
1942 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%u\n",
1943 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1945 rc = md_enqueue(tgt->ltd_exp, einfo, policy, it, op_data, lockh,
1952 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1953 struct ptlrpc_request **preq)
1955 struct ptlrpc_request *req = NULL;
1956 struct obd_device *obd = exp->exp_obd;
1957 struct lmv_obd *lmv = &obd->u.lmv;
1958 struct lmv_tgt_desc *tgt;
1959 struct mdt_body *body;
1963 rc = lmv_check_connect(obd);
1967 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1969 RETURN(PTR_ERR(tgt));
1971 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%u\n",
1972 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1975 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1979 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1980 LASSERT(body != NULL);
1982 if (body->mbo_valid & OBD_MD_MDS) {
1983 struct lu_fid rid = body->mbo_fid1;
1984 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1987 tgt = lmv_find_target(lmv, &rid);
1989 ptlrpc_req_finished(*preq);
1991 RETURN(PTR_ERR(tgt));
1994 op_data->op_fid1 = rid;
1995 op_data->op_valid |= OBD_MD_FLCROSSREF;
1996 op_data->op_namelen = 0;
1997 op_data->op_name = NULL;
1998 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1999 ptlrpc_req_finished(*preq);
2006 #define md_op_data_fid(op_data, fl) \
2007 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
2008 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
2009 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
2010 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
2013 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
2014 struct md_op_data *op_data,
2015 __u32 op_tgt, ldlm_mode_t mode, int bits, int flag)
2017 struct lu_fid *fid = md_op_data_fid(op_data, flag);
2018 struct obd_device *obd = exp->exp_obd;
2019 struct lmv_obd *lmv = &obd->u.lmv;
2020 ldlm_policy_data_t policy = {{ 0 }};
2024 if (!fid_is_sane(fid))
2028 tgt = lmv_find_target(lmv, fid);
2030 RETURN(PTR_ERR(tgt));
2033 if (tgt->ltd_idx != op_tgt) {
2034 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
2035 policy.l_inodebits.bits = bits;
2036 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
2037 mode, LCF_ASYNC, NULL);
2040 "EARLY_CANCEL skip operation target %d on "DFID"\n",
2042 op_data->op_flags |= flag;
2050 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
2053 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
2054 struct ptlrpc_request **request)
2056 struct obd_device *obd = exp->exp_obd;
2057 struct lmv_obd *lmv = &obd->u.lmv;
2058 struct lmv_tgt_desc *tgt;
2062 rc = lmv_check_connect(obd);
2066 LASSERT(op_data->op_namelen != 0);
2068 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2069 PFID(&op_data->op_fid2), op_data->op_namelen,
2070 op_data->op_name, PFID(&op_data->op_fid1));
2072 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2073 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2074 op_data->op_cap = cfs_curproc_cap_pack();
2075 if (op_data->op_mea2 != NULL) {
2076 struct lmv_stripe_md *lsm = op_data->op_mea2;
2077 const struct lmv_oinfo *oinfo;
2079 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
2080 op_data->op_namelen);
2082 RETURN(PTR_ERR(oinfo));
2084 op_data->op_fid2 = oinfo->lmo_fid;
2087 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2089 RETURN(PTR_ERR(tgt));
2092 * Cancel UPDATE lock on child (fid1).
2094 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2095 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2096 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2100 rc = md_link(tgt->ltd_exp, op_data, request);
2105 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2106 const char *old, int oldlen, const char *new, int newlen,
2107 struct ptlrpc_request **request)
2109 struct obd_device *obd = exp->exp_obd;
2110 struct lmv_obd *lmv = &obd->u.lmv;
2111 struct lmv_tgt_desc *src_tgt;
2115 LASSERT(oldlen != 0);
2117 CDEBUG(D_INODE, "RENAME %.*s in "DFID":%d to %.*s in "DFID":%d\n",
2118 oldlen, old, PFID(&op_data->op_fid1),
2119 op_data->op_mea1 ? op_data->op_mea1->lsm_md_stripe_count : 0,
2120 newlen, new, PFID(&op_data->op_fid2),
2121 op_data->op_mea2 ? op_data->op_mea2->lsm_md_stripe_count : 0);
2123 rc = lmv_check_connect(obd);
2127 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2128 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2129 op_data->op_cap = cfs_curproc_cap_pack();
2130 if (op_data->op_cli_flags & CLI_MIGRATE) {
2131 LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID "DFID"\n",
2132 PFID(&op_data->op_fid3));
2133 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
2136 src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid3);
2138 if (op_data->op_mea1 != NULL) {
2139 struct lmv_stripe_md *lsm = op_data->op_mea1;
2141 src_tgt = lmv_locate_target_for_name(lmv, lsm, old,
2145 if (IS_ERR(src_tgt))
2146 RETURN(PTR_ERR(src_tgt));
2148 src_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2149 if (IS_ERR(src_tgt))
2150 RETURN(PTR_ERR(src_tgt));
2152 op_data->op_mds = src_tgt->ltd_idx;
2155 if (op_data->op_mea2) {
2156 struct lmv_stripe_md *lsm = op_data->op_mea2;
2157 const struct lmv_oinfo *oinfo;
2159 oinfo = lsm_name_to_stripe_info(lsm, new, newlen);
2161 RETURN(PTR_ERR(oinfo));
2163 op_data->op_fid2 = oinfo->lmo_fid;
2166 if (IS_ERR(src_tgt))
2167 RETURN(PTR_ERR(src_tgt));
2170 * LOOKUP lock on src child (fid3) should also be cancelled for
2171 * src_tgt in mdc_rename.
2173 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2176 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2179 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
2180 LCK_EX, MDS_INODELOCK_UPDATE,
2181 MF_MDC_CANCEL_FID2);
2186 * Cancel LOOKUP locks on source child (fid3) for parent tgt_tgt.
2188 if (fid_is_sane(&op_data->op_fid3)) {
2189 struct lmv_tgt_desc *tgt;
2191 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2193 RETURN(PTR_ERR(tgt));
2195 /* Cancel LOOKUP lock on its parent */
2196 rc = lmv_early_cancel(exp, tgt, op_data, src_tgt->ltd_idx,
2197 LCK_EX, MDS_INODELOCK_LOOKUP,
2198 MF_MDC_CANCEL_FID3);
2202 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
2203 LCK_EX, MDS_INODELOCK_FULL,
2204 MF_MDC_CANCEL_FID3);
2210 * Cancel all the locks on tgt child (fid4).
2212 if (fid_is_sane(&op_data->op_fid4))
2213 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
2214 LCK_EX, MDS_INODELOCK_FULL,
2215 MF_MDC_CANCEL_FID4);
2217 CDEBUG(D_INODE, DFID":m%d to "DFID"\n", PFID(&op_data->op_fid1),
2218 op_data->op_mds, PFID(&op_data->op_fid2));
2220 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2226 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2227 void *ea, int ealen, void *ea2, int ea2len,
2228 struct ptlrpc_request **request,
2229 struct md_open_data **mod)
2231 struct obd_device *obd = exp->exp_obd;
2232 struct lmv_obd *lmv = &obd->u.lmv;
2233 struct lmv_tgt_desc *tgt;
2237 rc = lmv_check_connect(obd);
2241 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2242 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2244 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2245 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2247 RETURN(PTR_ERR(tgt));
2249 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2250 ea2len, request, mod);
2255 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2256 struct obd_capa *oc, struct ptlrpc_request **request)
2258 struct obd_device *obd = exp->exp_obd;
2259 struct lmv_obd *lmv = &obd->u.lmv;
2260 struct lmv_tgt_desc *tgt;
2264 rc = lmv_check_connect(obd);
2268 tgt = lmv_find_target(lmv, fid);
2270 RETURN(PTR_ERR(tgt));
2272 rc = md_fsync(tgt->ltd_exp, fid, oc, request);
2277 * Get current minimum entry from striped directory
2279 * This function will search the dir entry, whose hash value is the
2280 * closest(>=) to @hash_offset, from all of sub-stripes, and it is
2281 * only being called for striped directory.
2283 * \param[in] exp export of LMV
2284 * \param[in] op_data parameters transferred beween client MD stack
2285 * stripe_information will be included in this
2287 * \param[in] cb_op ldlm callback being used in enqueue in
2289 * \param[in] hash_offset the hash value, which is used to locate
2290 * minum(closet) dir entry
2291 * \param[in|out] stripe_offset the caller use this to indicate the stripe
2292 * index of last entry, so to avoid hash conflict
2293 * between stripes. It will also be used to
2294 * return the stripe index of current dir entry.
2295 * \param[in|out] entp the minum entry and it also is being used
2296 * to input the last dir entry to resolve the
2299 * \param[out] ppage the page which holds the minum entry
2301 * \retval = 0 get the entry successfully
2302 * negative errno (< 0) does not get the entry
2304 static int lmv_get_min_striped_entry(struct obd_export *exp,
2305 struct md_op_data *op_data,
2306 struct md_callback *cb_op,
2307 __u64 hash_offset, int *stripe_offset,
2308 struct lu_dirent **entp,
2309 struct page **ppage)
2311 struct obd_device *obd = exp->exp_obd;
2312 struct lmv_obd *lmv = &obd->u.lmv;
2313 struct lmv_stripe_md *lsm = op_data->op_mea1;
2314 struct lmv_tgt_desc *tgt;
2316 struct lu_dirent *min_ent = NULL;
2317 struct page *min_page = NULL;
2323 stripe_count = lsm->lsm_md_stripe_count;
2324 for (i = 0; i < stripe_count; i++) {
2325 struct lu_dirent *ent = NULL;
2326 struct page *page = NULL;
2327 struct lu_dirpage *dp;
2328 __u64 stripe_hash = hash_offset;
2330 tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds, NULL);
2332 GOTO(out, rc = PTR_ERR(tgt));
2334 /* op_data will be shared by each stripe, so we need
2335 * reset these value for each stripe */
2336 op_data->op_stripe_offset = i;
2337 op_data->op_fid1 = lsm->lsm_md_oinfo[i].lmo_fid;
2338 op_data->op_fid2 = lsm->lsm_md_oinfo[i].lmo_fid;
2339 op_data->op_data = lsm->lsm_md_oinfo[i].lmo_root;
2341 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, stripe_hash,
2346 dp = page_address(page);
2347 for (ent = lu_dirent_start(dp); ent != NULL;
2348 ent = lu_dirent_next(ent)) {
2349 /* Skip dummy entry */
2350 if (le16_to_cpu(ent->lde_namelen) == 0)
2353 if (le64_to_cpu(ent->lde_hash) < hash_offset)
2356 if (le64_to_cpu(ent->lde_hash) == hash_offset &&
2357 (*entp == ent || i < *stripe_offset))
2360 /* skip . and .. for other stripes */
2362 (strncmp(ent->lde_name, ".",
2363 le16_to_cpu(ent->lde_namelen)) == 0 ||
2364 strncmp(ent->lde_name, "..",
2365 le16_to_cpu(ent->lde_namelen)) == 0))
2371 stripe_hash = le64_to_cpu(dp->ldp_hash_end);
2374 page_cache_release(page);
2377 /* reach the end of current stripe, go to next stripe */
2378 if (stripe_hash == MDS_DIR_END_OFF)
2384 if (min_ent != NULL) {
2385 if (le64_to_cpu(min_ent->lde_hash) >
2386 le64_to_cpu(ent->lde_hash)) {
2389 page_cache_release(min_page);
2394 page_cache_release(page);
2405 if (*ppage != NULL) {
2407 page_cache_release(*ppage);
2409 *stripe_offset = min_idx;
2416 * Build dir entry page from a striped directory
2418 * This function gets one entry by @offset from a striped directory. It will
2419 * read entries from all of stripes, and choose one closest to the required
2420 * offset(&offset). A few notes
2421 * 1. skip . and .. for non-zero stripes, because there can only have one .
2422 * and .. in a directory.
2423 * 2. op_data will be shared by all of stripes, instead of allocating new
2424 * one, so need to restore before reusing.
2425 * 3. release the entry page if that is not being chosen.
2427 * \param[in] exp obd export refer to LMV
2428 * \param[in] op_data hold those MD parameters of read_entry
2429 * \param[in] cb_op ldlm callback being used in enqueue in mdc_read_entry
2430 * \param[out] ldp the entry being read
2431 * \param[out] ppage the page holding the entry. Note: because the entry
2432 * will be accessed in upper layer, so we need hold the
2433 * page until the usages of entry is finished, see
2434 * ll_dir_entry_next.
2436 * retval =0 if get entry successfully
2437 * <0 cannot get entry
2439 static int lmv_read_striped_page(struct obd_export *exp,
2440 struct md_op_data *op_data,
2441 struct md_callback *cb_op,
2442 __u64 offset, struct page **ppage)
2444 struct obd_device *obd = exp->exp_obd;
2445 struct lu_fid master_fid = op_data->op_fid1;
2446 struct inode *master_inode = op_data->op_data;
2447 __u64 hash_offset = offset;
2448 struct lu_dirpage *dp;
2449 struct page *min_ent_page = NULL;
2450 struct page *ent_page = NULL;
2451 struct lu_dirent *ent;
2454 struct lu_dirent *min_ent = NULL;
2455 struct lu_dirent *last_ent;
2460 rc = lmv_check_connect(obd);
2464 /* Allocate a page and read entries from all of stripes and fill
2465 * the page by hash order */
2466 ent_page = alloc_page(GFP_KERNEL);
2467 if (ent_page == NULL)
2470 /* Initialize the entry page */
2471 dp = kmap(ent_page);
2472 memset(dp, 0, sizeof(*dp));
2473 dp->ldp_hash_start = cpu_to_le64(offset);
2474 dp->ldp_flags |= LDF_COLLIDE;
2477 left_bytes = PAGE_CACHE_SIZE - sizeof(*dp);
2483 /* Find the minum entry from all sub-stripes */
2484 rc = lmv_get_min_striped_entry(exp, op_data, cb_op, hash_offset,
2490 /* If it can not get minum entry, it means it already reaches
2491 * the end of this directory */
2492 if (min_ent == NULL) {
2493 last_ent->lde_reclen = 0;
2494 hash_offset = MDS_DIR_END_OFF;
2498 ent_size = le16_to_cpu(min_ent->lde_reclen);
2500 /* the last entry lde_reclen is 0, but it might not
2501 * the end of this entry of this temporay entry */
2503 ent_size = lu_dirent_calc_size(
2504 le16_to_cpu(min_ent->lde_namelen),
2505 le32_to_cpu(min_ent->lde_attrs));
2506 if (ent_size > left_bytes) {
2507 last_ent->lde_reclen = cpu_to_le16(0);
2508 hash_offset = le64_to_cpu(min_ent->lde_hash);
2512 memcpy(ent, min_ent, ent_size);
2514 /* Replace . with master FID and Replace .. with the parent FID
2515 * of master object */
2516 if (strncmp(ent->lde_name, ".",
2517 le16_to_cpu(ent->lde_namelen)) == 0 &&
2518 le16_to_cpu(ent->lde_namelen) == 1)
2519 fid_cpu_to_le(&ent->lde_fid, &master_fid);
2520 else if (strncmp(ent->lde_name, "..",
2521 le16_to_cpu(ent->lde_namelen)) == 0 &&
2522 le16_to_cpu(ent->lde_namelen) == 2)
2523 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2525 left_bytes -= ent_size;
2526 ent->lde_reclen = cpu_to_le16(ent_size);
2528 ent = (void *)ent + ent_size;
2529 hash_offset = le64_to_cpu(min_ent->lde_hash);
2530 if (hash_offset == MDS_DIR_END_OFF) {
2531 last_ent->lde_reclen = 0;
2536 if (min_ent_page != NULL) {
2537 kunmap(min_ent_page);
2538 page_cache_release(min_ent_page);
2541 if (unlikely(rc != 0)) {
2542 __free_page(ent_page);
2546 dp->ldp_flags |= LDF_EMPTY;
2547 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
2548 dp->ldp_hash_end = cpu_to_le64(hash_offset);
2551 /* We do not want to allocate md_op_data during each
2552 * dir entry reading, so op_data will be shared by every stripe,
2553 * then we need to restore it back to original value before
2554 * return to the upper layer */
2555 op_data->op_fid1 = master_fid;
2556 op_data->op_fid2 = master_fid;
2557 op_data->op_data = master_inode;
2564 int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2565 struct md_callback *cb_op, __u64 offset,
2566 struct page **ppage)
2568 struct obd_device *obd = exp->exp_obd;
2569 struct lmv_obd *lmv = &obd->u.lmv;
2570 struct lmv_stripe_md *lsm = op_data->op_mea1;
2571 struct lmv_tgt_desc *tgt;
2575 rc = lmv_check_connect(obd);
2579 if (unlikely(lsm != NULL)) {
2580 rc = lmv_read_striped_page(exp, op_data, cb_op, offset, ppage);
2584 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2586 RETURN(PTR_ERR(tgt));
2588 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2594 * Unlink a file/directory
2596 * Unlink a file or directory under the parent dir. The unlink request
2597 * usually will be sent to the MDT where the child is located, but if
2598 * the client does not have the child FID then request will be sent to the
2599 * MDT where the parent is located.
2601 * If the parent is a striped directory then it also needs to locate which
2602 * stripe the name of the child is located, and replace the parent FID
2603 * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2604 * it will walk through all of sub-stripes until the child is being
2607 * \param[in] exp export refer to LMV
2608 * \param[in] op_data different parameters transferred beween client
2609 * MD stacks, name, namelen, FIDs etc.
2610 * op_fid1 is the parent FID, op_fid2 is the child
2612 * \param[out] request point to the request of unlink.
2614 * retval 0 if succeed
2615 * negative errno if failed.
2617 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2618 struct ptlrpc_request **request)
2620 struct obd_device *obd = exp->exp_obd;
2621 struct lmv_obd *lmv = &obd->u.lmv;
2622 struct lmv_tgt_desc *tgt = NULL;
2623 struct lmv_tgt_desc *parent_tgt = NULL;
2624 struct mdt_body *body;
2626 int stripe_index = 0;
2627 struct lmv_stripe_md *lsm = op_data->op_mea1;
2630 rc = lmv_check_connect(obd);
2634 /* For striped dir, we need to locate the parent as well */
2636 struct lmv_tgt_desc *tmp;
2638 LASSERT(op_data->op_name != NULL &&
2639 op_data->op_namelen != 0);
2641 tmp = lmv_locate_target_for_name(lmv, lsm,
2643 op_data->op_namelen,
2647 /* return -EBADFD means unknown hash type, might
2648 * need try all sub-stripe here */
2649 if (IS_ERR(tmp) && PTR_ERR(tmp) != -EBADFD)
2650 RETURN(PTR_ERR(tmp));
2652 /* Note: both migrating dir and unknown hash dir need to
2653 * try all of sub-stripes, so we need start search the
2654 * name from stripe 0, but migrating dir is already handled
2655 * inside lmv_locate_target_for_name(), so we only check
2656 * unknown hash type directory here */
2657 if (!lmv_is_known_hash_type(lsm)) {
2658 struct lmv_oinfo *oinfo;
2660 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2662 op_data->op_fid1 = oinfo->lmo_fid;
2663 op_data->op_mds = oinfo->lmo_mds;
2668 /* Send unlink requests to the MDT where the child is located */
2669 if (likely(!fid_is_zero(&op_data->op_fid2)))
2670 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2671 else if (lsm != NULL)
2672 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
2674 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2677 RETURN(PTR_ERR(tgt));
2679 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2680 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2681 op_data->op_cap = cfs_curproc_cap_pack();
2684 * If child's fid is given, cancel unused locks for it if it is from
2685 * another export than parent.
2687 * LOOKUP lock for child (fid3) should also be cancelled on parent
2688 * tgt_tgt in mdc_unlink().
2690 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2693 * Cancel FULL locks on child (fid3).
2695 parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2696 if (IS_ERR(parent_tgt))
2697 RETURN(PTR_ERR(parent_tgt));
2699 if (parent_tgt != tgt) {
2700 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2701 LCK_EX, MDS_INODELOCK_LOOKUP,
2702 MF_MDC_CANCEL_FID3);
2705 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2706 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2710 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
2711 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2713 rc = md_unlink(tgt->ltd_exp, op_data, request);
2714 if (rc != 0 && rc != -EREMOTE && rc != -ENOENT)
2717 /* Try next stripe if it is needed. */
2718 if (rc == -ENOENT && lsm != NULL && lmv_need_try_all_stripes(lsm)) {
2719 struct lmv_oinfo *oinfo;
2722 if (stripe_index >= lsm->lsm_md_stripe_count)
2725 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2727 op_data->op_fid1 = oinfo->lmo_fid;
2728 op_data->op_mds = oinfo->lmo_mds;
2730 ptlrpc_req_finished(*request);
2733 goto try_next_stripe;
2736 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2740 /* Not cross-ref case, just get out of here. */
2741 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2744 CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2745 exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2747 /* This is a remote object, try remote MDT, Note: it may
2748 * try more than 1 time here, Considering following case
2749 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2750 * 1. Initially A does not know where remote1 is, it send
2751 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2752 * resend unlink RPC to MDT1 (retry 1st time).
2754 * 2. During the unlink RPC in flight,
2755 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2756 * and create new remote1, but on MDT0
2758 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2759 * /mnt/lustre, then lookup get fid of remote1, and find
2760 * it is remote dir again, and replay -EREMOTE again.
2762 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2764 * In theory, it might try unlimited time here, but it should
2765 * be very rare case. */
2766 op_data->op_fid2 = body->mbo_fid1;
2767 ptlrpc_req_finished(*request);
2773 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2775 struct lmv_obd *lmv = &obd->u.lmv;
2779 case OBD_CLEANUP_EARLY:
2780 /* XXX: here should be calling obd_precleanup() down to
2783 case OBD_CLEANUP_EXPORTS:
2784 fld_client_proc_fini(&lmv->lmv_fld);
2785 lprocfs_obd_cleanup(obd);
2786 lprocfs_free_md_stats(obd);
2794 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2795 __u32 keylen, void *key, __u32 *vallen, void *val,
2796 struct lov_stripe_md *lsm)
2798 struct obd_device *obd;
2799 struct lmv_obd *lmv;
2803 obd = class_exp2obd(exp);
2805 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2806 exp->exp_handle.h_cookie);
2811 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2814 rc = lmv_check_connect(obd);
2818 LASSERT(*vallen == sizeof(__u32));
2819 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2820 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2822 * All tgts should be connected when this gets called.
2824 if (tgt == NULL || tgt->ltd_exp == NULL)
2827 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2832 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2833 KEY_IS(KEY_DEFAULT_EASIZE) ||
2834 KEY_IS(KEY_MAX_COOKIESIZE) ||
2835 KEY_IS(KEY_DEFAULT_COOKIESIZE) ||
2836 KEY_IS(KEY_CONN_DATA)) {
2837 rc = lmv_check_connect(obd);
2842 * Forwarding this request to first MDS, it should know LOV
2845 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2847 if (!rc && KEY_IS(KEY_CONN_DATA))
2848 exp->exp_connect_data = *(struct obd_connect_data *)val;
2850 } else if (KEY_IS(KEY_TGT_COUNT)) {
2851 *((int *)val) = lmv->desc.ld_tgt_count;
2855 CDEBUG(D_IOCTL, "Invalid key\n");
2859 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2860 obd_count keylen, void *key, obd_count vallen,
2861 void *val, struct ptlrpc_request_set *set)
2863 struct lmv_tgt_desc *tgt = NULL;
2864 struct obd_device *obd;
2865 struct lmv_obd *lmv;
2869 obd = class_exp2obd(exp);
2871 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2872 exp->exp_handle.h_cookie);
2877 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2880 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2883 if (tgt == NULL || tgt->ltd_exp == NULL)
2886 err = obd_set_info_async(env, tgt->ltd_exp,
2887 keylen, key, vallen, val, set);
2898 static int lmv_pack_md_v1(const struct lmv_stripe_md *lsm,
2899 struct lmv_mds_md_v1 *lmm1)
2904 lmm1->lmv_magic = cpu_to_le32(lsm->lsm_md_magic);
2905 lmm1->lmv_stripe_count = cpu_to_le32(lsm->lsm_md_stripe_count);
2906 lmm1->lmv_master_mdt_index = cpu_to_le32(lsm->lsm_md_master_mdt_index);
2907 lmm1->lmv_hash_type = cpu_to_le32(lsm->lsm_md_hash_type);
2908 cplen = strlcpy(lmm1->lmv_pool_name, lsm->lsm_md_pool_name,
2909 sizeof(lmm1->lmv_pool_name));
2910 if (cplen >= sizeof(lmm1->lmv_pool_name))
2913 for (i = 0; i < lsm->lsm_md_stripe_count; i++)
2914 fid_cpu_to_le(&lmm1->lmv_stripe_fids[i],
2915 &lsm->lsm_md_oinfo[i].lmo_fid);
2919 int lmv_pack_md(union lmv_mds_md **lmmp, const struct lmv_stripe_md *lsm,
2923 bool allocated = false;
2927 LASSERT(lmmp != NULL);
2929 if (*lmmp != NULL && lsm == NULL) {
2932 stripe_count = lmv_mds_md_stripe_count_get(*lmmp);
2933 lmm_size = lmv_mds_md_size(stripe_count,
2934 le32_to_cpu((*lmmp)->lmv_magic));
2937 OBD_FREE(*lmmp, lmm_size);
2943 if (*lmmp == NULL && lsm == NULL) {
2944 lmm_size = lmv_mds_md_size(stripe_count, LMV_MAGIC);
2945 LASSERT(lmm_size > 0);
2946 OBD_ALLOC(*lmmp, lmm_size);
2949 lmv_mds_md_stripe_count_set(*lmmp, stripe_count);
2950 (*lmmp)->lmv_magic = cpu_to_le32(LMV_MAGIC);
2955 LASSERT(lsm != NULL);
2956 lmm_size = lmv_mds_md_size(lsm->lsm_md_stripe_count, lsm->lsm_md_magic);
2957 if (*lmmp == NULL) {
2958 OBD_ALLOC(*lmmp, lmm_size);
2964 switch (lsm->lsm_md_magic) {
2966 rc = lmv_pack_md_v1(lsm, &(*lmmp)->lmv_md_v1);
2973 if (rc != 0 && allocated) {
2974 OBD_FREE(*lmmp, lmm_size);
2980 EXPORT_SYMBOL(lmv_pack_md);
2982 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2983 const struct lmv_mds_md_v1 *lmm1)
2985 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2992 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2993 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2994 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2995 if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
2996 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
2998 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2999 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
3000 cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
3001 sizeof(lsm->lsm_md_pool_name));
3003 if (cplen >= sizeof(lsm->lsm_md_pool_name))
3006 CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %d"
3007 "layout_version %d\n", lsm->lsm_md_stripe_count,
3008 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
3009 lsm->lsm_md_layout_version);
3011 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3012 for (i = 0; i < stripe_count; i++) {
3013 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
3014 &lmm1->lmv_stripe_fids[i]);
3015 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
3016 &lsm->lsm_md_oinfo[i].lmo_mds);
3019 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
3020 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
3026 int lmv_unpack_md(struct obd_export *exp, struct lmv_stripe_md **lsmp,
3027 const union lmv_mds_md *lmm, int stripe_count)
3029 struct lmv_stripe_md *lsm;
3032 bool allocated = false;
3035 LASSERT(lsmp != NULL);
3039 if (lsm != NULL && lmm == NULL) {
3041 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3042 /* For migrating inode, the master stripe and master
3043 * object will be the same, so do not need iput, see
3044 * ll_update_lsm_md */
3045 if (!(lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION &&
3046 i == 0) && lsm->lsm_md_oinfo[i].lmo_root != NULL)
3047 iput(lsm->lsm_md_oinfo[i].lmo_root);
3049 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
3050 OBD_FREE(lsm, lsm_size);
3056 if (lsm == NULL && lmm == NULL) {
3057 lsm_size = lmv_stripe_md_size(stripe_count);
3058 OBD_ALLOC(lsm, lsm_size);
3061 lsm->lsm_md_stripe_count = stripe_count;
3066 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
3070 if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
3071 le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
3072 CERROR("%s: invalid lmv magic %x: rc = %d\n",
3073 exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
3078 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
3079 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
3082 * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
3083 * stripecount should be 0 then.
3085 lsm_size = lmv_stripe_md_size(0);
3087 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
3089 OBD_ALLOC(lsm, lsm_size);
3096 switch (le32_to_cpu(lmm->lmv_magic)) {
3098 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
3101 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
3102 le32_to_cpu(lmm->lmv_magic));
3107 if (rc != 0 && allocated) {
3108 OBD_FREE(lsm, lsm_size);
3115 int lmv_alloc_memmd(struct lmv_stripe_md **lsmp, int stripes)
3117 return lmv_unpack_md(NULL, lsmp, NULL, stripes);
3119 EXPORT_SYMBOL(lmv_alloc_memmd);
3121 void lmv_free_memmd(struct lmv_stripe_md *lsm)
3123 lmv_unpack_md(NULL, &lsm, NULL, 0);
3125 EXPORT_SYMBOL(lmv_free_memmd);
3127 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
3128 struct lov_mds_md *lmm, int disk_len)
3130 return lmv_unpack_md(exp, (struct lmv_stripe_md **)lsmp,
3131 (union lmv_mds_md *)lmm, disk_len);
3134 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
3135 struct lov_stripe_md *lsm)
3137 struct obd_device *obd = exp->exp_obd;
3138 struct lmv_obd *lmv_obd = &obd->u.lmv;
3139 const struct lmv_stripe_md *lmv = (struct lmv_stripe_md *)lsm;
3144 stripe_count = lmv->lsm_md_stripe_count;
3146 stripe_count = lmv_obd->desc.ld_tgt_count;
3148 return lmv_mds_md_size(stripe_count, LMV_MAGIC_V1);
3151 return lmv_pack_md((union lmv_mds_md **)lmmp, lmv, 0);
3154 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3155 ldlm_policy_data_t *policy, ldlm_mode_t mode,
3156 ldlm_cancel_flags_t flags, void *opaque)
3158 struct obd_device *obd = exp->exp_obd;
3159 struct lmv_obd *lmv = &obd->u.lmv;
3165 LASSERT(fid != NULL);
3167 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3168 struct lmv_tgt_desc *tgt = lmv->tgts[i];
3170 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3173 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3181 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
3184 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3185 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3189 if (tgt == NULL || tgt->ltd_exp == NULL)
3191 rc = md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3195 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
3196 const struct lu_fid *fid, ldlm_type_t type,
3197 ldlm_policy_data_t *policy, ldlm_mode_t mode,
3198 struct lustre_handle *lockh)
3200 struct obd_device *obd = exp->exp_obd;
3201 struct lmv_obd *lmv = &obd->u.lmv;
3207 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
3210 * With DNE every object can have two locks in different namespaces:
3211 * lookup lock in space of MDT storing direntry and update/open lock in
3212 * space of MDT storing inode. Try the MDT that the FID maps to first,
3213 * since this can be easily found, and only try others if that fails.
3215 for (i = 0, tgt = lmv_find_target_index(lmv, fid);
3216 i < lmv->desc.ld_tgt_count;
3217 i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
3219 CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
3220 obd->obd_name, PFID(fid), tgt);
3224 if (lmv->tgts[tgt] == NULL ||
3225 lmv->tgts[tgt]->ltd_exp == NULL ||
3226 lmv->tgts[tgt]->ltd_active == 0)
3229 rc = md_lock_match(lmv->tgts[tgt]->ltd_exp, flags, fid,
3230 type, policy, mode, lockh);
3238 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
3239 struct obd_export *dt_exp, struct obd_export *md_exp,
3240 struct lustre_md *md)
3242 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3243 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3245 if (tgt == NULL || tgt->ltd_exp == NULL)
3248 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
3251 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
3253 struct obd_device *obd = exp->exp_obd;
3254 struct lmv_obd *lmv = &obd->u.lmv;
3255 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3258 if (md->lmv != NULL) {
3259 lmv_free_memmd(md->lmv);
3262 if (tgt == NULL || tgt->ltd_exp == NULL)
3264 RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
3267 int lmv_set_open_replay_data(struct obd_export *exp,
3268 struct obd_client_handle *och,
3269 struct lookup_intent *it)
3271 struct obd_device *obd = exp->exp_obd;
3272 struct lmv_obd *lmv = &obd->u.lmv;
3273 struct lmv_tgt_desc *tgt;
3276 tgt = lmv_find_target(lmv, &och->och_fid);
3278 RETURN(PTR_ERR(tgt));
3280 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3283 int lmv_clear_open_replay_data(struct obd_export *exp,
3284 struct obd_client_handle *och)
3286 struct obd_device *obd = exp->exp_obd;
3287 struct lmv_obd *lmv = &obd->u.lmv;
3288 struct lmv_tgt_desc *tgt;
3291 tgt = lmv_find_target(lmv, &och->och_fid);
3293 RETURN(PTR_ERR(tgt));
3295 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3298 static int lmv_get_remote_perm(struct obd_export *exp,
3299 const struct lu_fid *fid,
3300 struct obd_capa *oc, __u32 suppgid,
3301 struct ptlrpc_request **request)
3303 struct obd_device *obd = exp->exp_obd;
3304 struct lmv_obd *lmv = &obd->u.lmv;
3305 struct lmv_tgt_desc *tgt;
3309 rc = lmv_check_connect(obd);
3313 tgt = lmv_find_target(lmv, fid);
3315 RETURN(PTR_ERR(tgt));
3317 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
3321 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3324 struct obd_device *obd = exp->exp_obd;
3325 struct lmv_obd *lmv = &obd->u.lmv;
3326 struct lmv_tgt_desc *tgt;
3330 rc = lmv_check_connect(obd);
3334 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3336 RETURN(PTR_ERR(tgt));
3338 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3342 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3343 const struct req_msg_field *field, struct obd_capa **oc)
3345 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3346 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3348 if (tgt == NULL || tgt->ltd_exp == NULL)
3350 return md_unpack_capa(tgt->ltd_exp, req, field, oc);
3353 int lmv_intent_getattr_async(struct obd_export *exp,
3354 struct md_enqueue_info *minfo,
3355 struct ldlm_enqueue_info *einfo)
3357 struct md_op_data *op_data = &minfo->mi_data;
3358 struct obd_device *obd = exp->exp_obd;
3359 struct lmv_obd *lmv = &obd->u.lmv;
3360 struct lmv_tgt_desc *tgt = NULL;
3364 rc = lmv_check_connect(obd);
3368 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
3370 RETURN(PTR_ERR(tgt));
3372 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3376 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3377 struct lu_fid *fid, __u64 *bits)
3379 struct obd_device *obd = exp->exp_obd;
3380 struct lmv_obd *lmv = &obd->u.lmv;
3381 struct lmv_tgt_desc *tgt;
3385 rc = lmv_check_connect(obd);
3389 tgt = lmv_find_target(lmv, fid);
3391 RETURN(PTR_ERR(tgt));
3393 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3397 int lmv_get_fid_from_lsm(struct obd_export *exp,
3398 const struct lmv_stripe_md *lsm,
3399 const char *name, int namelen, struct lu_fid *fid)
3401 const struct lmv_oinfo *oinfo;
3403 LASSERT(lsm != NULL);
3404 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
3406 return PTR_ERR(oinfo);
3408 *fid = oinfo->lmo_fid;
3414 * For lmv, only need to send request to master MDT, and the master MDT will
3415 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3416 * we directly fetch data from the slave MDTs.
3418 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3419 struct obd_quotactl *oqctl)
3421 struct obd_device *obd = class_exp2obd(exp);
3422 struct lmv_obd *lmv = &obd->u.lmv;
3423 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3426 __u64 curspace, curinodes;
3430 tgt->ltd_exp == NULL ||
3432 lmv->desc.ld_tgt_count == 0) {
3433 CERROR("master lmv inactive\n");
3437 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3438 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3442 curspace = curinodes = 0;
3443 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3447 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3450 err = obd_quotactl(tgt->ltd_exp, oqctl);
3452 CERROR("getquota on mdt %d failed. %d\n", i, err);
3456 curspace += oqctl->qc_dqblk.dqb_curspace;
3457 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3460 oqctl->qc_dqblk.dqb_curspace = curspace;
3461 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3466 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3467 struct obd_quotactl *oqctl)
3469 struct obd_device *obd = class_exp2obd(exp);
3470 struct lmv_obd *lmv = &obd->u.lmv;
3471 struct lmv_tgt_desc *tgt;
3476 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3479 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
3480 CERROR("lmv idx %d inactive\n", i);
3484 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3492 int lmv_update_lsm_md(struct obd_export *exp, struct lmv_stripe_md *lsm,
3493 struct mdt_body *body, ldlm_blocking_callback cb_blocking)
3495 return lmv_revalidate_slaves(exp, body, lsm, cb_blocking, 0);
3498 int lmv_merge_attr(struct obd_export *exp, const struct lmv_stripe_md *lsm,
3499 struct cl_attr *attr)
3503 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3504 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3506 CDEBUG(D_INFO, ""DFID" size %llu, nlink %u, atime %lu ctime"
3507 "%lu, mtime %lu.\n", PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3508 i_size_read(inode), inode->i_nlink,
3509 LTIME_S(inode->i_atime), LTIME_S(inode->i_ctime),
3510 LTIME_S(inode->i_mtime));
3512 /* for slave stripe, it needs to subtract nlink for . and .. */
3514 attr->cat_nlink += inode->i_nlink - 2;
3516 attr->cat_nlink = inode->i_nlink;
3518 attr->cat_size += i_size_read(inode);
3520 if (attr->cat_atime < LTIME_S(inode->i_atime))
3521 attr->cat_atime = LTIME_S(inode->i_atime);
3523 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3524 attr->cat_ctime = LTIME_S(inode->i_ctime);
3526 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3527 attr->cat_mtime = LTIME_S(inode->i_mtime);
3532 struct obd_ops lmv_obd_ops = {
3533 .o_owner = THIS_MODULE,
3534 .o_setup = lmv_setup,
3535 .o_cleanup = lmv_cleanup,
3536 .o_precleanup = lmv_precleanup,
3537 .o_process_config = lmv_process_config,
3538 .o_connect = lmv_connect,
3539 .o_disconnect = lmv_disconnect,
3540 .o_statfs = lmv_statfs,
3541 .o_get_info = lmv_get_info,
3542 .o_set_info_async = lmv_set_info_async,
3543 .o_packmd = lmv_packmd,
3544 .o_unpackmd = lmv_unpackmd,
3545 .o_notify = lmv_notify,
3546 .o_get_uuid = lmv_get_uuid,
3547 .o_iocontrol = lmv_iocontrol,
3548 .o_quotacheck = lmv_quotacheck,
3549 .o_quotactl = lmv_quotactl
3552 struct md_ops lmv_md_ops = {
3553 .m_getstatus = lmv_getstatus,
3554 .m_null_inode = lmv_null_inode,
3555 .m_find_cbdata = lmv_find_cbdata,
3556 .m_close = lmv_close,
3557 .m_create = lmv_create,
3558 .m_done_writing = lmv_done_writing,
3559 .m_enqueue = lmv_enqueue,
3560 .m_getattr = lmv_getattr,
3561 .m_getxattr = lmv_getxattr,
3562 .m_getattr_name = lmv_getattr_name,
3563 .m_intent_lock = lmv_intent_lock,
3565 .m_rename = lmv_rename,
3566 .m_setattr = lmv_setattr,
3567 .m_setxattr = lmv_setxattr,
3568 .m_fsync = lmv_fsync,
3569 .m_read_page = lmv_read_page,
3570 .m_unlink = lmv_unlink,
3571 .m_init_ea_size = lmv_init_ea_size,
3572 .m_cancel_unused = lmv_cancel_unused,
3573 .m_set_lock_data = lmv_set_lock_data,
3574 .m_lock_match = lmv_lock_match,
3575 .m_get_lustre_md = lmv_get_lustre_md,
3576 .m_free_lustre_md = lmv_free_lustre_md,
3577 .m_update_lsm_md = lmv_update_lsm_md,
3578 .m_merge_attr = lmv_merge_attr,
3579 .m_set_open_replay_data = lmv_set_open_replay_data,
3580 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3581 .m_renew_capa = lmv_renew_capa,
3582 .m_unpack_capa = lmv_unpack_capa,
3583 .m_get_remote_perm = lmv_get_remote_perm,
3584 .m_intent_getattr_async = lmv_intent_getattr_async,
3585 .m_revalidate_lock = lmv_revalidate_lock,
3586 .m_get_fid_from_lsm = lmv_get_fid_from_lsm,
3589 int __init lmv_init(void)
3591 return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3592 LUSTRE_LMV_NAME, NULL);
3595 static void lmv_exit(void)
3597 class_unregister_type(LUSTRE_LMV_NAME);
3600 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3601 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3602 MODULE_LICENSE("GPL");
3604 module_init(lmv_init);
3605 module_exit(lmv_exit);