4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LMV
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
45 #include <linux/math64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
49 #include <liblustre.h>
52 #include <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lustre_lmv.h>
58 #include <lprocfs_status.h>
59 #include <cl_object.h>
61 #include <lustre_lite.h>
62 #include <lustre_fid.h>
63 #include <lustre_ioctl.h>
64 #include "lmv_internal.h"
66 /* This hash is only for testing purpose */
67 static inline unsigned int
68 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
71 const unsigned char *p = (const unsigned char *)name;
73 while (--namelen >= 0)
81 static inline unsigned int
82 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
86 hash = lustre_hash_fnv_1a_64(name, namelen);
93 int lmv_name_to_stripe_index(__u32 lmv_hash_type, unsigned int stripe_count,
94 const char *name, int namelen)
97 __u32 hash_type = lmv_hash_type & LMV_HASH_TYPE_MASK;
100 if (stripe_count <= 1)
103 /* for migrating object, always start from 0 stripe */
104 if (lmv_hash_type & LMV_HASH_FLAG_MIGRATION)
108 case LMV_HASH_TYPE_ALL_CHARS:
109 idx = lmv_hash_all_chars(stripe_count, name, namelen);
111 case LMV_HASH_TYPE_FNV_1A_64:
112 idx = lmv_hash_fnv1a(stripe_count, name, namelen);
115 CERROR("Unknown hash type 0x%x\n", hash_type);
119 CDEBUG(D_INFO, "name %.*s hash_type %d idx %d\n", namelen, name,
125 static void lmv_activate_target(struct lmv_obd *lmv,
126 struct lmv_tgt_desc *tgt,
129 if (tgt->ltd_active == activate)
132 tgt->ltd_active = activate;
133 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
139 * -EINVAL : UUID can't be found in the LMV's target list
140 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
141 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
143 static int lmv_set_mdc_active(struct lmv_obd *lmv,
144 const struct obd_uuid *uuid,
147 struct lmv_tgt_desc *tgt = NULL;
148 struct obd_device *obd;
153 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
154 lmv, uuid->uuid, activate);
156 spin_lock(&lmv->lmv_lock);
157 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
159 if (tgt == NULL || tgt->ltd_exp == NULL)
162 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n", i,
163 tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
165 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
169 if (i == lmv->desc.ld_tgt_count)
170 GOTO(out_lmv_lock, rc = -EINVAL);
172 obd = class_exp2obd(tgt->ltd_exp);
174 GOTO(out_lmv_lock, rc = -ENOTCONN);
176 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
177 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
178 obd->obd_type->typ_name, i);
179 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
181 if (tgt->ltd_active == activate) {
182 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
183 activate ? "" : "in");
184 GOTO(out_lmv_lock, rc);
187 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
188 activate ? "" : "in");
189 lmv_activate_target(lmv, tgt, activate);
193 spin_unlock(&lmv->lmv_lock);
197 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
199 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
200 struct lmv_tgt_desc *tgt = lmv->tgts[0];
202 return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
205 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
206 enum obd_notify_event ev, void *data)
208 struct obd_connect_data *conn_data;
209 struct lmv_obd *lmv = &obd->u.lmv;
210 struct obd_uuid *uuid;
214 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
215 CERROR("unexpected notification of %s %s!\n",
216 watched->obd_type->typ_name,
221 uuid = &watched->u.cli.cl_target_uuid;
222 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
224 * Set MDC as active before notifying the observer, so the
225 * observer can use the MDC normally.
227 rc = lmv_set_mdc_active(lmv, uuid,
228 ev == OBD_NOTIFY_ACTIVE);
230 CERROR("%sactivation of %s failed: %d\n",
231 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
235 } else if (ev == OBD_NOTIFY_OCD) {
236 conn_data = &watched->u.cli.cl_import->imp_connect_data;
238 * XXX: Make sure that ocd_connect_flags from all targets are
239 * the same. Otherwise one of MDTs runs wrong version or
240 * something like this. --umka
242 obd->obd_self_export->exp_connect_data = *conn_data;
245 else if (ev == OBD_NOTIFY_DISCON) {
247 * For disconnect event, flush fld cache for failout MDS case.
249 fld_client_flush(&lmv->lmv_fld);
253 * Pass the notification up the chain.
255 if (obd->obd_observer)
256 rc = obd_notify(obd->obd_observer, watched, ev, data);
262 * This is fake connect function. Its purpose is to initialize lmv and say
263 * caller that everything is okay. Real connection will be performed later.
265 static int lmv_connect(const struct lu_env *env,
266 struct obd_export **exp, struct obd_device *obd,
267 struct obd_uuid *cluuid, struct obd_connect_data *data,
270 struct lmv_obd *lmv = &obd->u.lmv;
271 struct lustre_handle conn = { 0 };
276 * We don't want to actually do the underlying connections more than
277 * once, so keep track.
280 if (lmv->refcount > 1) {
285 rc = class_connect(&conn, obd, cluuid);
287 CERROR("class_connection() returned %d\n", rc);
291 *exp = class_conn2export(&conn);
292 class_export_get(*exp);
296 lmv->cluuid = *cluuid;
299 lmv->conn_data = *data;
301 if (lmv->targets_proc_entry == NULL) {
302 lmv->targets_proc_entry = lprocfs_seq_register("target_obds",
305 if (IS_ERR(lmv->targets_proc_entry)) {
306 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
307 obd->obd_type->typ_name, obd->obd_name);
308 lmv->targets_proc_entry = NULL;
313 * All real clients should perform actual connection right away, because
314 * it is possible, that LMV will not have opportunity to connect targets
315 * and MDC stuff will be called directly, for instance while reading
316 * ../mdc/../kbytesfree procfs file, etc.
318 if (data != NULL && (data->ocd_connect_flags & OBD_CONNECT_REAL))
319 rc = lmv_check_connect(obd);
321 if (rc && lmv->targets_proc_entry != NULL)
322 lprocfs_remove(&lmv->targets_proc_entry);
326 static void lmv_set_timeouts(struct obd_device *obd)
332 if (lmv->server_timeout == 0)
335 if (lmv->connected == 0)
338 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
339 struct lmv_tgt_desc *tgt = lmv->tgts[i];
341 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
344 obd_set_info_async(NULL, tgt->ltd_exp, sizeof(KEY_INTERMDS),
345 KEY_INTERMDS, 0, NULL, NULL);
349 static int lmv_init_ea_size(struct obd_export *exp, int easize,
350 int def_easize, int cookiesize, int def_cookiesize)
352 struct obd_device *obd = exp->exp_obd;
353 struct lmv_obd *lmv = &obd->u.lmv;
359 if (lmv->max_easize < easize) {
360 lmv->max_easize = easize;
363 if (lmv->max_def_easize < def_easize) {
364 lmv->max_def_easize = def_easize;
367 if (lmv->max_cookiesize < cookiesize) {
368 lmv->max_cookiesize = cookiesize;
371 if (lmv->max_def_cookiesize < def_cookiesize) {
372 lmv->max_def_cookiesize = def_cookiesize;
378 if (lmv->connected == 0)
381 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
382 struct lmv_tgt_desc *tgt = lmv->tgts[i];
384 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
385 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
389 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize,
390 cookiesize, def_cookiesize);
392 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
393 " rc = %d.\n", obd->obd_name, i, rc);
400 #define MAX_STRING_SIZE 128
402 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
404 struct lmv_obd *lmv = &obd->u.lmv;
405 struct obd_uuid *cluuid = &lmv->cluuid;
406 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
407 struct obd_device *mdc_obd;
408 struct obd_export *mdc_exp;
409 struct lu_fld_target target;
413 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
416 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
420 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
421 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
422 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
425 if (!mdc_obd->obd_set_up) {
426 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
430 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
431 &lmv->conn_data, NULL);
433 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
438 * Init fid sequence client for this mdc and add new fld target.
440 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
444 target.ft_srv = NULL;
445 target.ft_exp = mdc_exp;
446 target.ft_idx = tgt->ltd_idx;
448 fld_client_add_target(&lmv->lmv_fld, &target);
450 rc = obd_register_observer(mdc_obd, obd);
452 obd_disconnect(mdc_exp);
453 CERROR("target %s register_observer error %d\n",
454 tgt->ltd_uuid.uuid, rc);
458 if (obd->obd_observer) {
460 * Tell the observer about the new target.
462 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
464 (void *)(tgt - lmv->tgts[0]));
466 obd_disconnect(mdc_exp);
472 tgt->ltd_exp = mdc_exp;
473 lmv->desc.ld_active_tgt_count++;
475 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize,
476 lmv->max_cookiesize, lmv->max_def_cookiesize);
478 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
479 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
480 atomic_read(&obd->obd_refcount));
482 if (lmv->targets_proc_entry != NULL) {
483 struct proc_dir_entry *mdc_symlink;
485 LASSERT(mdc_obd->obd_type != NULL);
486 LASSERT(mdc_obd->obd_type->typ_name != NULL);
487 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
488 lmv->targets_proc_entry,
490 mdc_obd->obd_type->typ_name,
492 if (mdc_symlink == NULL) {
493 CERROR("Could not register LMV target "
494 "/proc/fs/lustre/%s/%s/target_obds/%s.",
495 obd->obd_type->typ_name, obd->obd_name,
502 static void lmv_del_target(struct lmv_obd *lmv, int index)
504 if (lmv->tgts[index] == NULL)
507 OBD_FREE_PTR(lmv->tgts[index]);
508 lmv->tgts[index] = NULL;
512 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
513 __u32 index, int gen)
515 struct lmv_obd *lmv = &obd->u.lmv;
516 struct lmv_tgt_desc *tgt;
520 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
524 if (lmv->desc.ld_tgt_count == 0) {
525 struct obd_device *mdc_obd;
527 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
530 lmv_init_unlock(lmv);
531 CERROR("%s: Target %s not attached: rc = %d\n",
532 obd->obd_name, uuidp->uuid, -EINVAL);
537 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
538 tgt = lmv->tgts[index];
539 CERROR("%s: UUID %s already assigned at LOV target index %d:"
540 " rc = %d\n", obd->obd_name,
541 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
542 lmv_init_unlock(lmv);
546 if (index >= lmv->tgts_size) {
547 /* We need to reallocate the lmv target array. */
548 struct lmv_tgt_desc **newtgts, **old = NULL;
552 while (newsize < index + 1)
553 newsize = newsize << 1;
554 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
555 if (newtgts == NULL) {
556 lmv_init_unlock(lmv);
560 if (lmv->tgts_size) {
561 memcpy(newtgts, lmv->tgts,
562 sizeof(*newtgts) * lmv->tgts_size);
564 oldsize = lmv->tgts_size;
568 lmv->tgts_size = newsize;
571 OBD_FREE(old, sizeof(*old) * oldsize);
573 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
579 lmv_init_unlock(lmv);
583 mutex_init(&tgt->ltd_fid_mutex);
584 tgt->ltd_idx = index;
585 tgt->ltd_uuid = *uuidp;
587 lmv->tgts[index] = tgt;
588 if (index >= lmv->desc.ld_tgt_count)
589 lmv->desc.ld_tgt_count = index + 1;
591 if (lmv->connected) {
592 rc = lmv_connect_mdc(obd, tgt);
594 spin_lock(&lmv->lmv_lock);
595 lmv->desc.ld_tgt_count--;
596 memset(tgt, 0, sizeof(*tgt));
597 spin_unlock(&lmv->lmv_lock);
599 int easize = sizeof(struct lmv_stripe_md) +
600 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
601 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
605 lmv_init_unlock(lmv);
609 int lmv_check_connect(struct obd_device *obd)
611 struct lmv_obd *lmv = &obd->u.lmv;
612 struct lmv_tgt_desc *tgt;
622 if (lmv->connected) {
623 lmv_init_unlock(lmv);
627 if (lmv->desc.ld_tgt_count == 0) {
628 lmv_init_unlock(lmv);
629 CERROR("%s: no targets configured.\n", obd->obd_name);
633 LASSERT(lmv->tgts != NULL);
635 if (lmv->tgts[0] == NULL) {
636 lmv_init_unlock(lmv);
637 CERROR("%s: no target configured for index 0.\n",
642 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
643 lmv->cluuid.uuid, obd->obd_name);
645 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
649 rc = lmv_connect_mdc(obd, tgt);
654 lmv_set_timeouts(obd);
655 class_export_put(lmv->exp);
657 easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
658 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0, 0);
659 lmv_init_unlock(lmv);
670 --lmv->desc.ld_active_tgt_count;
671 rc2 = obd_disconnect(tgt->ltd_exp);
673 CERROR("LMV target %s disconnect on "
674 "MDC idx %d: error %d\n",
675 tgt->ltd_uuid.uuid, i, rc2);
679 class_disconnect(lmv->exp);
680 lmv_init_unlock(lmv);
684 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
686 struct lmv_obd *lmv = &obd->u.lmv;
687 struct obd_device *mdc_obd;
691 LASSERT(tgt != NULL);
692 LASSERT(obd != NULL);
694 mdc_obd = class_exp2obd(tgt->ltd_exp);
697 mdc_obd->obd_force = obd->obd_force;
698 mdc_obd->obd_fail = obd->obd_fail;
699 mdc_obd->obd_no_recov = obd->obd_no_recov;
702 if (lmv->targets_proc_entry != NULL)
703 lprocfs_remove_proc_entry(mdc_obd->obd_name,
704 lmv->targets_proc_entry);
706 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
708 CERROR("Can't finanize fids factory\n");
710 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
711 tgt->ltd_exp->exp_obd->obd_name,
712 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
714 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
715 rc = obd_disconnect(tgt->ltd_exp);
717 if (tgt->ltd_active) {
718 CERROR("Target %s disconnect error %d\n",
719 tgt->ltd_uuid.uuid, rc);
723 lmv_activate_target(lmv, tgt, 0);
728 static int lmv_disconnect(struct obd_export *exp)
730 struct obd_device *obd = class_exp2obd(exp);
731 struct lmv_obd *lmv = &obd->u.lmv;
740 * Only disconnect the underlying layers on the final disconnect.
743 if (lmv->refcount != 0)
746 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
747 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
750 lmv_disconnect_mdc(obd, lmv->tgts[i]);
753 if (lmv->targets_proc_entry != NULL)
754 lprocfs_remove(&lmv->targets_proc_entry);
756 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
757 obd->obd_type->typ_name, obd->obd_name);
761 * This is the case when no real connection is established by
762 * lmv_check_connect().
765 class_export_put(exp);
766 rc = class_disconnect(exp);
767 if (lmv->refcount == 0)
772 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
774 struct obd_device *obddev = class_exp2obd(exp);
775 struct lmv_obd *lmv = &obddev->u.lmv;
776 struct getinfo_fid2path *gf;
777 struct lmv_tgt_desc *tgt;
778 struct getinfo_fid2path *remote_gf = NULL;
779 int remote_gf_size = 0;
782 gf = (struct getinfo_fid2path *)karg;
783 tgt = lmv_find_target(lmv, &gf->gf_fid);
785 RETURN(PTR_ERR(tgt));
788 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
789 if (rc != 0 && rc != -EREMOTE)
790 GOTO(out_fid2path, rc);
792 /* If remote_gf != NULL, it means just building the
793 * path on the remote MDT, copy this path segement to gf */
794 if (remote_gf != NULL) {
795 struct getinfo_fid2path *ori_gf;
798 ori_gf = (struct getinfo_fid2path *)karg;
799 if (strlen(ori_gf->gf_path) +
800 strlen(gf->gf_path) > ori_gf->gf_pathlen)
801 GOTO(out_fid2path, rc = -EOVERFLOW);
803 ptr = ori_gf->gf_path;
805 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
806 strlen(ori_gf->gf_path));
808 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
809 ptr += strlen(gf->gf_path);
813 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: "LPU64" ln: %u\n",
814 tgt->ltd_exp->exp_obd->obd_name,
815 gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
819 GOTO(out_fid2path, rc);
821 /* sigh, has to go to another MDT to do path building further */
822 if (remote_gf == NULL) {
823 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
824 OBD_ALLOC(remote_gf, remote_gf_size);
825 if (remote_gf == NULL)
826 GOTO(out_fid2path, rc = -ENOMEM);
827 remote_gf->gf_pathlen = PATH_MAX;
830 if (!fid_is_sane(&gf->gf_fid)) {
831 CERROR("%s: invalid FID "DFID": rc = %d\n",
832 tgt->ltd_exp->exp_obd->obd_name,
833 PFID(&gf->gf_fid), -EINVAL);
834 GOTO(out_fid2path, rc = -EINVAL);
837 tgt = lmv_find_target(lmv, &gf->gf_fid);
839 GOTO(out_fid2path, rc = -EINVAL);
841 remote_gf->gf_fid = gf->gf_fid;
842 remote_gf->gf_recno = -1;
843 remote_gf->gf_linkno = -1;
844 memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
846 goto repeat_fid2path;
849 if (remote_gf != NULL)
850 OBD_FREE(remote_gf, remote_gf_size);
854 static int lmv_hsm_req_count(struct lmv_obd *lmv,
855 const struct hsm_user_request *hur,
856 const struct lmv_tgt_desc *tgt_mds)
860 struct lmv_tgt_desc *curr_tgt;
862 /* count how many requests must be sent to the given target */
863 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
864 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
865 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
871 static void lmv_hsm_req_build(struct lmv_obd *lmv,
872 struct hsm_user_request *hur_in,
873 const struct lmv_tgt_desc *tgt_mds,
874 struct hsm_user_request *hur_out)
877 struct lmv_tgt_desc *curr_tgt;
879 /* build the hsm_user_request for the given target */
880 hur_out->hur_request = hur_in->hur_request;
882 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
883 curr_tgt = lmv_find_target(lmv,
884 &hur_in->hur_user_item[i].hui_fid);
885 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
886 hur_out->hur_user_item[nr_out] =
887 hur_in->hur_user_item[i];
891 hur_out->hur_request.hr_itemcount = nr_out;
892 memcpy(hur_data(hur_out), hur_data(hur_in),
893 hur_in->hur_request.hr_data_len);
896 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
897 struct lustre_kernelcomm *lk, void *uarg)
901 struct kkuc_ct_data *kcd = NULL;
904 /* unregister request (call from llapi_hsm_copytool_fini) */
905 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
906 struct lmv_tgt_desc *tgt = lmv->tgts[i];
908 if (tgt == NULL || tgt->ltd_exp == NULL)
910 /* best effort: try to clean as much as possible
911 * (continue on error) */
912 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
915 /* Whatever the result, remove copytool from kuc groups.
916 * Unreached coordinators will get EPIPE on next requests
917 * and will unregister automatically.
919 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group, (void **)&kcd);
926 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
927 struct lustre_kernelcomm *lk, void *uarg)
932 bool any_set = false;
933 struct kkuc_ct_data *kcd;
936 /* All or nothing: try to register to all MDS.
937 * In case of failure, unregister from previous MDS,
938 * except if it because of inactive target. */
939 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
940 struct lmv_tgt_desc *tgt = lmv->tgts[i];
942 if (tgt == NULL || tgt->ltd_exp == NULL)
944 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
946 if (tgt->ltd_active) {
947 /* permanent error */
948 CERROR("%s: iocontrol MDC %s on MDT"
949 " idx %d cmd %x: err = %d\n",
950 class_exp2obd(lmv->exp)->obd_name,
951 tgt->ltd_uuid.uuid, i, cmd, err);
953 lk->lk_flags |= LK_FLG_STOP;
954 /* unregister from previous MDS */
955 for (j = 0; j < i; j++) {
957 if (tgt == NULL || tgt->ltd_exp == NULL)
959 obd_iocontrol(cmd, tgt->ltd_exp, len,
964 /* else: transient error.
965 * kuc will register to the missing MDT
973 /* no registration done: return error */
976 /* at least one registration done, with no failure */
977 filp = fget(lk->lk_wfd);
986 kcd->kcd_magic = KKUC_CT_DATA_MAGIC;
987 kcd->kcd_uuid = lmv->cluuid;
988 kcd->kcd_archive = lk->lk_data;
990 rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, kcd);
1003 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
1004 int len, void *karg, void *uarg)
1006 struct obd_device *obddev = class_exp2obd(exp);
1007 struct lmv_obd *lmv = &obddev->u.lmv;
1008 struct lmv_tgt_desc *tgt = NULL;
1012 __u32 count = lmv->desc.ld_tgt_count;
1019 case IOC_OBD_STATFS: {
1020 struct obd_ioctl_data *data = karg;
1021 struct obd_device *mdc_obd;
1022 struct obd_statfs stat_buf = {0};
1025 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1026 if ((index >= count))
1029 tgt = lmv->tgts[index];
1030 if (tgt == NULL || !tgt->ltd_active)
1033 mdc_obd = class_exp2obd(tgt->ltd_exp);
1038 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
1039 min((int) data->ioc_plen2,
1040 (int) sizeof(struct obd_uuid))))
1043 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
1044 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1048 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1049 min((int) data->ioc_plen1,
1050 (int) sizeof(stat_buf))))
1054 case OBD_IOC_QUOTACTL: {
1055 struct if_quotactl *qctl = karg;
1056 struct obd_quotactl *oqctl;
1058 if (qctl->qc_valid == QC_MDTIDX) {
1059 if (count <= qctl->qc_idx)
1062 tgt = lmv->tgts[qctl->qc_idx];
1063 if (tgt == NULL || tgt->ltd_exp == NULL)
1065 } else if (qctl->qc_valid == QC_UUID) {
1066 for (i = 0; i < count; i++) {
1070 if (!obd_uuid_equals(&tgt->ltd_uuid,
1074 if (tgt->ltd_exp == NULL)
1086 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
1087 OBD_ALLOC_PTR(oqctl);
1091 QCTL_COPY(oqctl, qctl);
1092 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1094 QCTL_COPY(qctl, oqctl);
1095 qctl->qc_valid = QC_MDTIDX;
1096 qctl->obd_uuid = tgt->ltd_uuid;
1098 OBD_FREE_PTR(oqctl);
1101 case OBD_IOC_CHANGELOG_SEND:
1102 case OBD_IOC_CHANGELOG_CLEAR: {
1103 struct ioc_changelog *icc = karg;
1105 if (icc->icc_mdtindex >= count)
1108 tgt = lmv->tgts[icc->icc_mdtindex];
1109 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
1111 rc = obd_iocontrol(cmd, tgt->ltd_exp, sizeof(*icc), icc, NULL);
1114 case LL_IOC_GET_CONNECT_FLAGS: {
1116 if (tgt == NULL || tgt->ltd_exp == NULL)
1118 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1121 case OBD_IOC_FID2PATH: {
1122 rc = lmv_fid2path(exp, len, karg, uarg);
1125 case LL_IOC_HSM_STATE_GET:
1126 case LL_IOC_HSM_STATE_SET:
1127 case LL_IOC_HSM_ACTION: {
1128 struct md_op_data *op_data = karg;
1130 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1132 RETURN(PTR_ERR(tgt));
1134 if (tgt->ltd_exp == NULL)
1137 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1140 case LL_IOC_HSM_PROGRESS: {
1141 const struct hsm_progress_kernel *hpk = karg;
1143 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1145 RETURN(PTR_ERR(tgt));
1146 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1149 case LL_IOC_HSM_REQUEST: {
1150 struct hsm_user_request *hur = karg;
1151 unsigned int reqcount = hur->hur_request.hr_itemcount;
1156 /* if the request is about a single fid
1157 * or if there is a single MDS, no need to split
1159 if (reqcount == 1 || count == 1) {
1160 tgt = lmv_find_target(lmv,
1161 &hur->hur_user_item[0].hui_fid);
1163 RETURN(PTR_ERR(tgt));
1164 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1166 /* split fid list to their respective MDS */
1167 for (i = 0; i < count; i++) {
1168 unsigned int nr, reqlen;
1170 struct hsm_user_request *req;
1173 if (tgt == NULL || tgt->ltd_exp == NULL)
1176 nr = lmv_hsm_req_count(lmv, hur, tgt);
1177 if (nr == 0) /* nothing for this MDS */
1180 /* build a request with fids for this MDS */
1181 reqlen = offsetof(typeof(*hur),
1183 + hur->hur_request.hr_data_len;
1184 OBD_ALLOC_LARGE(req, reqlen);
1188 lmv_hsm_req_build(lmv, hur, tgt, req);
1190 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1192 if (rc1 != 0 && rc == 0)
1194 OBD_FREE_LARGE(req, reqlen);
1199 case LL_IOC_LOV_SWAP_LAYOUTS: {
1200 struct md_op_data *op_data = karg;
1201 struct lmv_tgt_desc *tgt1, *tgt2;
1203 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1205 RETURN(PTR_ERR(tgt1));
1207 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1209 RETURN(PTR_ERR(tgt2));
1211 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1214 /* only files on same MDT can have their layouts swapped */
1215 if (tgt1->ltd_idx != tgt2->ltd_idx)
1218 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1221 case LL_IOC_HSM_CT_START: {
1222 struct lustre_kernelcomm *lk = karg;
1223 if (lk->lk_flags & LK_FLG_STOP)
1224 rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1226 rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1230 for (i = 0; i < count; i++) {
1231 struct obd_device *mdc_obd;
1235 if (tgt == NULL || tgt->ltd_exp == NULL)
1237 /* ll_umount_begin() sets force flag but for lmv, not
1238 * mdc. Let's pass it through */
1239 mdc_obd = class_exp2obd(tgt->ltd_exp);
1240 mdc_obd->obd_force = obddev->obd_force;
1241 err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1242 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1245 if (tgt->ltd_active) {
1246 CERROR("error: iocontrol MDC %s on MDT"
1247 " idx %d cmd %x: err = %d\n",
1248 tgt->ltd_uuid.uuid, i, cmd, err);
1262 static int lmv_all_chars_policy(int count, const char *name,
1273 static int lmv_nid_policy(struct lmv_obd *lmv)
1275 struct obd_import *imp;
1279 * XXX: To get nid we assume that underlying obd device is mdc.
1281 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1282 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1283 return id % lmv->desc.ld_tgt_count;
1286 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1287 placement_policy_t placement)
1289 switch (placement) {
1290 case PLACEMENT_CHAR_POLICY:
1291 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1293 op_data->op_namelen);
1294 case PLACEMENT_NID_POLICY:
1295 return lmv_nid_policy(lmv);
1301 CERROR("Unsupported placement policy %x\n", placement);
1307 * This is _inode_ placement policy function (not name).
1309 static int lmv_placement_policy(struct obd_device *obd,
1310 struct md_op_data *op_data,
1313 struct lmv_obd *lmv = &obd->u.lmv;
1316 LASSERT(mds != NULL);
1318 if (lmv->desc.ld_tgt_count == 1) {
1324 * If stripe_offset is provided during setdirstripe
1325 * (setdirstripe -i xx), xx MDS will be choosen.
1327 if (op_data->op_cli_flags & CLI_SET_MEA && op_data->op_data != NULL) {
1328 struct lmv_user_md *lum;
1330 lum = op_data->op_data;
1332 if (le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1333 *mds = le32_to_cpu(lum->lum_stripe_offset);
1335 /* -1 means default, which will be in the same MDT with
1337 *mds = op_data->op_mds;
1338 lum->lum_stripe_offset = cpu_to_le32(op_data->op_mds);
1341 /* Allocate new fid on target according to operation type and
1342 * parent home mds. */
1343 *mds = op_data->op_mds;
1349 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
1352 struct lmv_tgt_desc *tgt;
1356 tgt = lmv_get_target(lmv, mds);
1358 RETURN(PTR_ERR(tgt));
1361 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1362 * on server that seq in new allocated fid is not yet known.
1364 mutex_lock(&tgt->ltd_fid_mutex);
1366 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1367 GOTO(out, rc = -ENODEV);
1370 * Asking underlying tgt layer to allocate new fid.
1372 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1374 LASSERT(fid_is_sane(fid));
1380 mutex_unlock(&tgt->ltd_fid_mutex);
1384 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1385 struct lu_fid *fid, struct md_op_data *op_data)
1387 struct obd_device *obd = class_exp2obd(exp);
1388 struct lmv_obd *lmv = &obd->u.lmv;
1393 LASSERT(op_data != NULL);
1394 LASSERT(fid != NULL);
1396 rc = lmv_placement_policy(obd, op_data, &mds);
1398 CERROR("Can't get target for allocating fid, "
1403 rc = __lmv_fid_alloc(lmv, fid, mds);
1405 CERROR("Can't alloc new fid, rc %d\n", rc);
1412 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1414 struct lmv_obd *lmv = &obd->u.lmv;
1415 struct lmv_desc *desc;
1419 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1420 CERROR("LMV setup requires a descriptor\n");
1424 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1425 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1426 CERROR("Lmv descriptor size wrong: %d > %d\n",
1427 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1431 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1432 if (lmv->tgts == NULL)
1434 lmv->tgts_size = 32;
1436 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1437 lmv->desc.ld_tgt_count = 0;
1438 lmv->desc.ld_active_tgt_count = 0;
1439 lmv->max_cookiesize = 0;
1440 lmv->max_def_easize = 0;
1441 lmv->max_easize = 0;
1442 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1444 spin_lock_init(&lmv->lmv_lock);
1445 mutex_init(&lmv->init_mutex);
1448 obd->obd_vars = lprocfs_lmv_obd_vars;
1449 lprocfs_seq_obd_setup(obd);
1450 lprocfs_alloc_md_stats(obd, 0);
1451 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1452 0444, &lmv_proc_target_fops, obd);
1454 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1457 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1458 LUSTRE_CLI_FLD_HASH_DHT);
1460 CERROR("Can't init FLD, err %d\n", rc);
1470 static int lmv_cleanup(struct obd_device *obd)
1472 struct lmv_obd *lmv = &obd->u.lmv;
1475 fld_client_fini(&lmv->lmv_fld);
1476 if (lmv->tgts != NULL) {
1478 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1479 if (lmv->tgts[i] == NULL)
1481 lmv_del_target(lmv, i);
1483 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1489 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1491 struct lustre_cfg *lcfg = buf;
1492 struct obd_uuid obd_uuid;
1498 switch (lcfg->lcfg_command) {
1500 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1501 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1502 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1503 GOTO(out, rc = -EINVAL);
1505 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1507 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1508 GOTO(out, rc = -EINVAL);
1509 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1510 GOTO(out, rc = -EINVAL);
1511 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1514 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1515 GOTO(out, rc = -EINVAL);
1521 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1522 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1524 struct obd_device *obd = class_exp2obd(exp);
1525 struct lmv_obd *lmv = &obd->u.lmv;
1526 struct obd_statfs *temp;
1531 rc = lmv_check_connect(obd);
1535 OBD_ALLOC(temp, sizeof(*temp));
1539 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1540 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1543 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1546 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1547 lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1549 GOTO(out_free_temp, rc);
1554 /* If the statfs is from mount, it will needs
1555 * retrieve necessary information from MDT0.
1556 * i.e. mount does not need the merged osfs
1558 * And also clients can be mounted as long as
1559 * MDT0 is in service*/
1560 if (flags & OBD_STATFS_FOR_MDT0)
1561 GOTO(out_free_temp, rc);
1563 osfs->os_bavail += temp->os_bavail;
1564 osfs->os_blocks += temp->os_blocks;
1565 osfs->os_ffree += temp->os_ffree;
1566 osfs->os_files += temp->os_files;
1572 OBD_FREE(temp, sizeof(*temp));
1576 static int lmv_getstatus(struct obd_export *exp,
1578 struct obd_capa **pc)
1580 struct obd_device *obd = exp->exp_obd;
1581 struct lmv_obd *lmv = &obd->u.lmv;
1585 rc = lmv_check_connect(obd);
1589 rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1593 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1594 struct obd_capa *oc, obd_valid valid, const char *name,
1595 const char *input, int input_size, int output_size,
1596 int flags, struct ptlrpc_request **request)
1598 struct obd_device *obd = exp->exp_obd;
1599 struct lmv_obd *lmv = &obd->u.lmv;
1600 struct lmv_tgt_desc *tgt;
1604 rc = lmv_check_connect(obd);
1608 tgt = lmv_find_target(lmv, fid);
1610 RETURN(PTR_ERR(tgt));
1612 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1613 input_size, output_size, flags, request);
1618 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1619 struct obd_capa *oc, obd_valid valid, const char *name,
1620 const char *input, int input_size, int output_size,
1621 int flags, __u32 suppgid,
1622 struct ptlrpc_request **request)
1624 struct obd_device *obd = exp->exp_obd;
1625 struct lmv_obd *lmv = &obd->u.lmv;
1626 struct lmv_tgt_desc *tgt;
1630 rc = lmv_check_connect(obd);
1634 tgt = lmv_find_target(lmv, fid);
1636 RETURN(PTR_ERR(tgt));
1638 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1639 input_size, output_size, flags, suppgid,
1645 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1646 struct ptlrpc_request **request)
1648 struct obd_device *obd = exp->exp_obd;
1649 struct lmv_obd *lmv = &obd->u.lmv;
1650 struct lmv_tgt_desc *tgt;
1654 rc = lmv_check_connect(obd);
1658 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1660 RETURN(PTR_ERR(tgt));
1662 if (op_data->op_flags & MF_GET_MDT_IDX) {
1663 op_data->op_mds = tgt->ltd_idx;
1667 rc = md_getattr(tgt->ltd_exp, op_data, request);
1672 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1674 struct obd_device *obd = exp->exp_obd;
1675 struct lmv_obd *lmv = &obd->u.lmv;
1680 rc = lmv_check_connect(obd);
1684 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1687 * With DNE every object can have two locks in different namespaces:
1688 * lookup lock in space of MDT storing direntry and update/open lock in
1689 * space of MDT storing inode.
1691 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1692 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1694 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1700 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1701 ldlm_iterator_t it, void *data)
1703 struct obd_device *obd = exp->exp_obd;
1704 struct lmv_obd *lmv = &obd->u.lmv;
1709 rc = lmv_check_connect(obd);
1713 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1716 * With DNE every object can have two locks in different namespaces:
1717 * lookup lock in space of MDT storing direntry and update/open lock in
1718 * space of MDT storing inode.
1720 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1721 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1723 rc = md_find_cbdata(lmv->tgts[i]->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((void *)oinfo);
1771 *fid = oinfo->lmo_fid;
1772 *mds = oinfo->lmo_mds;
1773 tgt = lmv_get_target(lmv, *mds);
1775 CDEBUG(D_INFO, "locate on mds %u "DFID"\n", *mds, PFID(fid));
1780 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1783 struct lmv_stripe_md *lsm = op_data->op_mea1;
1784 struct lmv_tgt_desc *tgt;
1786 if (lsm == NULL || op_data->op_namelen == 0) {
1787 tgt = lmv_find_target(lmv, fid);
1791 op_data->op_mds = tgt->ltd_idx;
1795 return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
1796 op_data->op_namelen, fid,
1800 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1801 const void *data, int datalen, int mode, __u32 uid,
1802 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1803 struct ptlrpc_request **request)
1805 struct obd_device *obd = exp->exp_obd;
1806 struct lmv_obd *lmv = &obd->u.lmv;
1807 struct lmv_tgt_desc *tgt;
1811 rc = lmv_check_connect(obd);
1815 if (!lmv->desc.ld_active_tgt_count)
1818 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1820 RETURN(PTR_ERR(tgt));
1822 CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1823 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1826 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1830 /* Send the create request to the MDT where the object
1831 * will be located */
1832 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1834 RETURN(PTR_ERR(tgt));
1836 op_data->op_mds = tgt->ltd_idx;
1838 CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1839 PFID(&op_data->op_fid2), op_data->op_mds);
1841 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1842 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1843 cap_effective, rdev, request);
1845 if (*request == NULL)
1847 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1852 static int lmv_done_writing(struct obd_export *exp,
1853 struct md_op_data *op_data,
1854 struct md_open_data *mod)
1856 struct obd_device *obd = exp->exp_obd;
1857 struct lmv_obd *lmv = &obd->u.lmv;
1858 struct lmv_tgt_desc *tgt;
1862 rc = lmv_check_connect(obd);
1866 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1868 RETURN(PTR_ERR(tgt));
1870 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1875 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1876 const union ldlm_policy_data *policy,
1877 struct lookup_intent *it, struct md_op_data *op_data,
1878 struct lustre_handle *lockh, __u64 extra_lock_flags)
1880 struct obd_device *obd = exp->exp_obd;
1881 struct lmv_obd *lmv = &obd->u.lmv;
1882 struct lmv_tgt_desc *tgt;
1886 rc = lmv_check_connect(obd);
1890 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1891 LL_IT2STR(it), PFID(&op_data->op_fid1));
1893 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1895 RETURN(PTR_ERR(tgt));
1897 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1898 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1900 rc = md_enqueue(tgt->ltd_exp, einfo, policy, it, op_data, lockh,
1907 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1908 struct ptlrpc_request **preq)
1910 struct ptlrpc_request *req = NULL;
1911 struct obd_device *obd = exp->exp_obd;
1912 struct lmv_obd *lmv = &obd->u.lmv;
1913 struct lmv_tgt_desc *tgt;
1914 struct mdt_body *body;
1918 rc = lmv_check_connect(obd);
1922 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1924 RETURN(PTR_ERR(tgt));
1926 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1927 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1930 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1934 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1935 LASSERT(body != NULL);
1937 if (body->valid & OBD_MD_MDS) {
1938 struct lu_fid rid = body->fid1;
1939 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1942 tgt = lmv_find_target(lmv, &rid);
1944 ptlrpc_req_finished(*preq);
1946 RETURN(PTR_ERR(tgt));
1949 op_data->op_fid1 = rid;
1950 op_data->op_valid |= OBD_MD_FLCROSSREF;
1951 op_data->op_namelen = 0;
1952 op_data->op_name = NULL;
1953 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1954 ptlrpc_req_finished(*preq);
1961 #define md_op_data_fid(op_data, fl) \
1962 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1963 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1964 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1965 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1968 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1969 struct md_op_data *op_data,
1970 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1972 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1973 struct obd_device *obd = exp->exp_obd;
1974 struct lmv_obd *lmv = &obd->u.lmv;
1975 ldlm_policy_data_t policy = {{ 0 }};
1979 if (!fid_is_sane(fid))
1983 tgt = lmv_find_target(lmv, fid);
1985 RETURN(PTR_ERR(tgt));
1988 if (tgt->ltd_idx != op_tgt) {
1989 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1990 policy.l_inodebits.bits = bits;
1991 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1992 mode, LCF_ASYNC, NULL);
1995 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1997 op_data->op_flags |= flag;
2005 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
2008 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
2009 struct ptlrpc_request **request)
2011 struct obd_device *obd = exp->exp_obd;
2012 struct lmv_obd *lmv = &obd->u.lmv;
2013 struct lmv_tgt_desc *tgt;
2017 rc = lmv_check_connect(obd);
2021 LASSERT(op_data->op_namelen != 0);
2023 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2024 PFID(&op_data->op_fid2), op_data->op_namelen,
2025 op_data->op_name, PFID(&op_data->op_fid1));
2027 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2028 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2029 op_data->op_cap = cfs_curproc_cap_pack();
2030 if (op_data->op_mea2 != NULL) {
2031 struct lmv_stripe_md *lsm = op_data->op_mea2;
2032 const struct lmv_oinfo *oinfo;
2034 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
2035 op_data->op_namelen);
2037 RETURN(PTR_ERR(oinfo));
2039 op_data->op_fid2 = oinfo->lmo_fid;
2042 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2044 RETURN(PTR_ERR(tgt));
2047 * Cancel UPDATE lock on child (fid1).
2049 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2050 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2051 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2055 rc = md_link(tgt->ltd_exp, op_data, request);
2060 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2061 const char *old, int oldlen, const char *new, int newlen,
2062 struct ptlrpc_request **request)
2064 struct obd_device *obd = exp->exp_obd;
2065 struct lmv_obd *lmv = &obd->u.lmv;
2066 struct lmv_tgt_desc *src_tgt;
2070 LASSERT(oldlen != 0);
2072 CDEBUG(D_INODE, "RENAME %.*s in "DFID":%d to %.*s in "DFID":%d\n",
2073 oldlen, old, PFID(&op_data->op_fid1),
2074 op_data->op_mea1 ? op_data->op_mea1->lsm_md_stripe_count : 0,
2075 newlen, new, PFID(&op_data->op_fid2),
2076 op_data->op_mea2 ? op_data->op_mea2->lsm_md_stripe_count : 0);
2078 rc = lmv_check_connect(obd);
2082 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2083 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2084 op_data->op_cap = cfs_curproc_cap_pack();
2085 if (op_data->op_cli_flags & CLI_MIGRATE) {
2086 LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID "DFID"\n",
2087 PFID(&op_data->op_fid3));
2088 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
2091 src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid3);
2093 if (op_data->op_mea1 != NULL) {
2094 struct lmv_stripe_md *lsm = op_data->op_mea1;
2096 src_tgt = lmv_locate_target_for_name(lmv, lsm, old,
2100 if (IS_ERR(src_tgt))
2101 RETURN(PTR_ERR(src_tgt));
2103 src_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2104 if (IS_ERR(src_tgt))
2105 RETURN(PTR_ERR(src_tgt));
2107 op_data->op_mds = src_tgt->ltd_idx;
2110 if (op_data->op_mea2) {
2111 struct lmv_stripe_md *lsm = op_data->op_mea2;
2112 const struct lmv_oinfo *oinfo;
2114 oinfo = lsm_name_to_stripe_info(lsm, new, newlen);
2116 RETURN(PTR_ERR(oinfo));
2118 op_data->op_fid2 = oinfo->lmo_fid;
2121 if (IS_ERR(src_tgt))
2122 RETURN(PTR_ERR(src_tgt));
2125 * LOOKUP lock on src child (fid3) should also be cancelled for
2126 * src_tgt in mdc_rename.
2128 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2131 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2134 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
2135 LCK_EX, MDS_INODELOCK_UPDATE,
2136 MF_MDC_CANCEL_FID2);
2141 * Cancel LOOKUP locks on source child (fid3) for parent tgt_tgt.
2143 if (fid_is_sane(&op_data->op_fid3)) {
2144 struct lmv_tgt_desc *tgt;
2146 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2148 RETURN(PTR_ERR(tgt));
2150 /* Cancel LOOKUP lock on its parent */
2151 rc = lmv_early_cancel(exp, tgt, op_data, src_tgt->ltd_idx,
2152 LCK_EX, MDS_INODELOCK_LOOKUP,
2153 MF_MDC_CANCEL_FID3);
2157 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
2158 LCK_EX, MDS_INODELOCK_FULL,
2159 MF_MDC_CANCEL_FID3);
2165 * Cancel all the locks on tgt child (fid4).
2167 if (fid_is_sane(&op_data->op_fid4))
2168 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
2169 LCK_EX, MDS_INODELOCK_FULL,
2170 MF_MDC_CANCEL_FID4);
2172 CDEBUG(D_INODE, DFID":m%d to "DFID"\n", PFID(&op_data->op_fid1),
2173 op_data->op_mds, PFID(&op_data->op_fid2));
2175 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2181 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2182 void *ea, int ealen, void *ea2, int ea2len,
2183 struct ptlrpc_request **request,
2184 struct md_open_data **mod)
2186 struct obd_device *obd = exp->exp_obd;
2187 struct lmv_obd *lmv = &obd->u.lmv;
2188 struct lmv_tgt_desc *tgt;
2192 rc = lmv_check_connect(obd);
2196 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2197 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2199 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2200 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2202 RETURN(PTR_ERR(tgt));
2204 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2205 ea2len, request, mod);
2210 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2211 struct obd_capa *oc, struct ptlrpc_request **request)
2213 struct obd_device *obd = exp->exp_obd;
2214 struct lmv_obd *lmv = &obd->u.lmv;
2215 struct lmv_tgt_desc *tgt;
2219 rc = lmv_check_connect(obd);
2223 tgt = lmv_find_target(lmv, fid);
2225 RETURN(PTR_ERR(tgt));
2227 rc = md_fsync(tgt->ltd_exp, fid, oc, request);
2232 * Adjust a set of pages, each page containing an array of lu_dirpages,
2233 * so that each page can be used as a single logical lu_dirpage.
2235 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
2236 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
2237 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
2238 * value is used as a cookie to request the next lu_dirpage in a
2239 * directory listing that spans multiple pages (two in this example):
2242 * .|--------v------- -----.
2243 * |s|e|f|p|ent|ent| ... |ent|
2244 * '--|-------------- -----' Each CFS_PAGE contains a single
2245 * '------. lu_dirpage.
2246 * .---------v------- -----.
2247 * |s|e|f|p|ent| 0 | ... | 0 |
2248 * '----------------- -----'
2250 * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
2251 * larger than LU_PAGE_SIZE, a single host page may contain multiple
2252 * lu_dirpages. After reading the lu_dirpages from the MDS, the
2253 * ldp_hash_end of the first lu_dirpage refers to the one immediately
2254 * after it in the same CFS_PAGE (arrows simplified for brevity, but
2255 * in general e0==s1, e1==s2, etc.):
2257 * .-------------------- -----.
2258 * |s0|e0|f0|p|ent|ent| ... |ent|
2259 * |---v---------------- -----|
2260 * |s1|e1|f1|p|ent|ent| ... |ent|
2261 * |---v---------------- -----| Here, each CFS_PAGE contains
2262 * ... multiple lu_dirpages.
2263 * |---v---------------- -----|
2264 * |s'|e'|f'|p|ent|ent| ... |ent|
2265 * '---|---------------- -----'
2267 * .----------------------------.
2270 * This structure is transformed into a single logical lu_dirpage as follows:
2272 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
2273 * labeled 'next CFS_PAGE'.
2275 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
2276 * a hash collision with the next page exists.
2278 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
2279 * to the first entry of the next lu_dirpage.
2281 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
2282 static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
2286 for (i = 0; i < ncfspgs; i++) {
2287 struct lu_dirpage *dp = kmap(pages[i]);
2288 struct lu_dirpage *first = dp;
2289 struct lu_dirent *end_dirent = NULL;
2290 struct lu_dirent *ent;
2291 __u64 hash_end = dp->ldp_hash_end;
2292 __u32 flags = dp->ldp_flags;
2294 while (--nlupgs > 0) {
2295 ent = lu_dirent_start(dp);
2296 for (end_dirent = ent; ent != NULL;
2297 end_dirent = ent, ent = lu_dirent_next(ent));
2299 /* Advance dp to next lu_dirpage. */
2300 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2302 /* Check if we've reached the end of the CFS_PAGE. */
2303 if (!((unsigned long)dp & ~CFS_PAGE_MASK))
2306 /* Save the hash and flags of this lu_dirpage. */
2307 hash_end = dp->ldp_hash_end;
2308 flags = dp->ldp_flags;
2310 /* Check if lu_dirpage contains no entries. */
2314 /* Enlarge the end entry lde_reclen from 0 to
2315 * first entry of next lu_dirpage. */
2316 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
2317 end_dirent->lde_reclen =
2318 cpu_to_le16((char *)(dp->ldp_entries) -
2319 (char *)end_dirent);
2322 first->ldp_hash_end = hash_end;
2323 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2324 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2328 LASSERTF(nlupgs == 0, "left = %d", nlupgs);
2331 #define lmv_adjust_dirpages(pages, ncfspgs, nlupgs) do {} while (0)
2332 #endif /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
2335 * This function will read entry from a striped directory, bascially, it will
2336 * read entries from all of stripes, and choose one closest to the required
2337 * offset(&op_data->op_hash_offset). A few notes
2338 * 1. skip . and .. for non-zero stripes, because there can only have one .
2339 * and .. in a directory.
2340 * 2. op_data will be shared by all of stripes, instead of allocating new
2341 * one, so need to restore before reusing.
2342 * 3. release the entry page if that is not being chosen.
2344 * param[in]exp obd export refer to LMV
2345 * param[in]op_data hold those MD parameters of read_entry.
2346 * param[in]cb_op ldlm callback being used in enqueue in mdc_read_entry
2347 * param[out]ldp the entry being read.
2348 * param[out]ppage the page holding the entry, note: because the entry
2349 * will be accessed in upper layer, so we need hold the
2350 * page until the usages of entry is finished, see
2351 * ll_dir_entry_next.
2353 * retval =0 if get entry successfully
2354 * <0 can not get entry.
2356 #define NORMAL_MAX_STRIPES 4
2357 static int lmv_read_striped_entry(struct obd_export *exp,
2358 struct md_op_data *op_data,
2359 struct md_callback *cb_op,
2360 struct lu_dirent **ldp,
2361 struct page **ppage)
2363 struct obd_device *obd = exp->exp_obd;
2364 struct lmv_obd *lmv = &obd->u.lmv;
2365 struct lmv_stripe_md *lsm = op_data->op_mea1;
2366 struct lmv_tgt_desc *tgt;
2367 struct lu_dirent *tmp_ents[NORMAL_MAX_STRIPES];
2368 struct lu_dirent **ents = NULL;
2369 struct lu_fid master_fid = op_data->op_fid1;
2370 void *master_data = op_data->op_data;
2371 __u64 last_idx = op_data->op_stripe_offset;
2372 __u64 hash_offset = op_data->op_hash_offset;
2373 __u32 same_hash_offset = op_data->op_same_hash_offset;
2374 __u32 cli_flags = op_data->op_cli_flags;
2377 int min_same_hash_offset = 0;
2379 struct page *min_page = NULL;
2384 LASSERT(lsm != NULL);
2386 rc = lmv_check_connect(obd);
2390 /* . and .. will be stored on the master object, so we need iterate
2391 * the master object as well */
2392 stripe_count = lsm->lsm_md_stripe_count;
2393 if (stripe_count > NORMAL_MAX_STRIPES) {
2394 OBD_ALLOC(ents, sizeof(ents[0]) * stripe_count);
2396 GOTO(out, rc = -ENOMEM);
2399 memset(ents, 0, sizeof(ents[0]) * stripe_count);
2402 min_hash = MDS_DIR_END_OFF;
2403 for (i = 0; i < stripe_count; i++) {
2404 struct page *page = NULL;
2406 tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds);
2408 GOTO(out, rc = PTR_ERR(tgt));
2411 op_data->op_same_hash_offset = 0;
2413 op_data->op_same_hash_offset = same_hash_offset;
2415 /* op_data will be shared by each stripe, so we need
2416 * reset these value for each stripe */
2417 op_data->op_stripe_offset = i;
2418 op_data->op_hash_offset = hash_offset;
2419 op_data->op_cli_flags = cli_flags;
2420 op_data->op_fid1 = lsm->lsm_md_oinfo[i].lmo_fid;
2421 op_data->op_fid2 = lsm->lsm_md_oinfo[i].lmo_fid;
2422 op_data->op_data = lsm->lsm_md_oinfo[i].lmo_root;
2425 rc = md_read_entry(tgt->ltd_exp, op_data, cb_op, &ents[i],
2430 if (ents[i] != NULL &&
2431 (strncmp(ents[i]->lde_name, ".",
2432 le16_to_cpu(ents[i]->lde_namelen)) == 0 ||
2433 strncmp(ents[i]->lde_name, "..",
2434 le16_to_cpu(ents[i]->lde_namelen)) == 0)) {
2436 /* replace . with master FID */
2437 if (le16_to_cpu(ents[i]->lde_namelen) == 1)
2438 fid_cpu_to_le(&ents[i]->lde_fid,
2441 fid_cpu_to_le(&ents[i]->lde_fid,
2444 /* skip . and .. for other stripes */
2445 op_data->op_cli_flags |= CLI_NEXT_ENTRY;
2446 op_data->op_hash_offset =
2447 le64_to_cpu(ents[i]->lde_hash);
2449 page_cache_release(page);
2454 if (ents[i] != NULL) {
2455 /* If the hash value of read_entry is equal to the
2456 * current min_hash, which is very rare and only
2457 * happens if two entries have the same hash value
2458 * but on different stripes, in this case, we need
2459 * make sure these entries are being reading forward,
2460 * not backward, i.e. only reset the min_entry, if
2461 * current stripe is ahead of last entry. Note: if
2462 * there are hash conflict inside the entry, MDC
2463 * (see mdc_read_entry) will resolve them. */
2464 if (le64_to_cpu(ents[i]->lde_hash) < min_hash ||
2465 (le64_to_cpu(ents[i]->lde_hash) == min_hash &&
2467 if (min_page != NULL) {
2469 page_cache_release(min_page);
2472 min_hash = le64_to_cpu(ents[i]->lde_hash);
2473 min_same_hash_offset =
2474 op_data->op_same_hash_offset;
2478 page_cache_release(page);
2483 if (min_hash != MDS_DIR_END_OFF) {
2484 *ldp = ents[min_idx];
2485 op_data->op_stripe_offset = min_idx;
2486 op_data->op_same_hash_offset = min_same_hash_offset;
2493 /* We do not want to allocate md_op_data during each
2494 * dir entry reading, so op_data will be shared by every stripe,
2495 * then we need to restore it back to original value before
2496 * return to the upper layer */
2497 op_data->op_hash_offset = hash_offset;
2498 op_data->op_fid1 = master_fid;
2499 op_data->op_fid2 = master_fid;
2500 op_data->op_data = master_data;
2501 op_data->op_cli_flags = cli_flags;
2502 if (stripe_count > NORMAL_MAX_STRIPES && ents != NULL)
2503 OBD_FREE(ents, sizeof(ents[0]) * stripe_count);
2505 if (rc != 0 && min_page != NULL) {
2507 page_cache_release(min_page);
2513 int lmv_read_entry(struct obd_export *exp, struct md_op_data *op_data,
2514 struct md_callback *cb_op, struct lu_dirent **ldp,
2515 struct page **ppage)
2517 struct obd_device *obd = exp->exp_obd;
2518 struct lmv_obd *lmv = &obd->u.lmv;
2519 struct lmv_stripe_md *lsm = op_data->op_mea1;
2520 struct lmv_tgt_desc *tgt;
2524 rc = lmv_check_connect(obd);
2528 if (unlikely(lsm != NULL)) {
2529 rc = lmv_read_striped_entry(exp, op_data, cb_op,
2534 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2536 RETURN(PTR_ERR(tgt));
2538 rc = md_read_entry(tgt->ltd_exp, op_data, cb_op, ldp,
2543 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2544 struct ptlrpc_request **request)
2546 struct obd_device *obd = exp->exp_obd;
2547 struct lmv_obd *lmv = &obd->u.lmv;
2548 struct lmv_tgt_desc *tgt = NULL;
2549 struct lmv_tgt_desc *parent_tgt = NULL;
2550 struct mdt_body *body;
2554 rc = lmv_check_connect(obd);
2558 /* Send unlink requests to the MDT where the child is located */
2559 if (likely(!fid_is_zero(&op_data->op_fid2))) {
2560 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2562 RETURN(PTR_ERR(tgt));
2564 /* For striped dir, we need to locate the parent as well */
2565 if (op_data->op_mea1 != NULL) {
2566 struct lmv_tgt_desc *tmp;
2568 LASSERT(op_data->op_name != NULL &&
2569 op_data->op_namelen != 0);
2570 tmp = lmv_locate_target_for_name(lmv,
2573 op_data->op_namelen,
2577 RETURN(PTR_ERR(tmp));
2580 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2582 RETURN(PTR_ERR(tgt));
2585 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2586 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2587 op_data->op_cap = cfs_curproc_cap_pack();
2590 * If child's fid is given, cancel unused locks for it if it is from
2591 * another export than parent.
2593 * LOOKUP lock for child (fid3) should also be cancelled on parent
2594 * tgt_tgt in mdc_unlink().
2596 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2599 * Cancel FULL locks on child (fid3).
2601 parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2602 if (IS_ERR(parent_tgt))
2603 RETURN(PTR_ERR(parent_tgt));
2605 if (parent_tgt != tgt) {
2606 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2607 LCK_EX, MDS_INODELOCK_LOOKUP,
2608 MF_MDC_CANCEL_FID3);
2611 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2612 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2616 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2617 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2619 rc = md_unlink(tgt->ltd_exp, op_data, request);
2620 if (rc != 0 && rc != -EREMOTE)
2623 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2627 /* Not cross-ref case, just get out of here. */
2628 if (likely(!(body->valid & OBD_MD_MDS)))
2631 CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2632 exp->exp_obd->obd_name, PFID(&body->fid1));
2634 /* This is a remote object, try remote MDT, Note: it may
2635 * try more than 1 time here, Considering following case
2636 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2637 * 1. Initially A does not know where remote1 is, it send
2638 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2639 * resend unlink RPC to MDT1 (retry 1st time).
2641 * 2. During the unlink RPC in flight,
2642 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2643 * and create new remote1, but on MDT0
2645 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2646 * /mnt/lustre, then lookup get fid of remote1, and find
2647 * it is remote dir again, and replay -EREMOTE again.
2649 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2651 * In theory, it might try unlimited time here, but it should
2652 * be very rare case. */
2653 op_data->op_fid2 = body->fid1;
2654 ptlrpc_req_finished(*request);
2660 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2662 struct lmv_obd *lmv = &obd->u.lmv;
2666 case OBD_CLEANUP_EARLY:
2667 /* XXX: here should be calling obd_precleanup() down to
2670 case OBD_CLEANUP_EXPORTS:
2671 fld_client_proc_fini(&lmv->lmv_fld);
2672 lprocfs_obd_cleanup(obd);
2673 lprocfs_free_md_stats(obd);
2681 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2682 __u32 keylen, void *key, __u32 *vallen, void *val,
2683 struct lov_stripe_md *lsm)
2685 struct obd_device *obd;
2686 struct lmv_obd *lmv;
2690 obd = class_exp2obd(exp);
2692 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2693 exp->exp_handle.h_cookie);
2698 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2701 rc = lmv_check_connect(obd);
2705 LASSERT(*vallen == sizeof(__u32));
2706 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2707 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2709 * All tgts should be connected when this gets called.
2711 if (tgt == NULL || tgt->ltd_exp == NULL)
2714 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2719 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2720 KEY_IS(KEY_DEFAULT_EASIZE) ||
2721 KEY_IS(KEY_MAX_COOKIESIZE) ||
2722 KEY_IS(KEY_DEFAULT_COOKIESIZE) ||
2723 KEY_IS(KEY_CONN_DATA)) {
2724 rc = lmv_check_connect(obd);
2729 * Forwarding this request to first MDS, it should know LOV
2732 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2734 if (!rc && KEY_IS(KEY_CONN_DATA))
2735 exp->exp_connect_data = *(struct obd_connect_data *)val;
2737 } else if (KEY_IS(KEY_TGT_COUNT)) {
2738 *((int *)val) = lmv->desc.ld_tgt_count;
2742 CDEBUG(D_IOCTL, "Invalid key\n");
2746 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2747 obd_count keylen, void *key, obd_count vallen,
2748 void *val, struct ptlrpc_request_set *set)
2750 struct lmv_tgt_desc *tgt = NULL;
2751 struct obd_device *obd;
2752 struct lmv_obd *lmv;
2756 obd = class_exp2obd(exp);
2758 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2759 exp->exp_handle.h_cookie);
2764 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2767 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2770 if (tgt == NULL || tgt->ltd_exp == NULL)
2773 err = obd_set_info_async(env, tgt->ltd_exp,
2774 keylen, key, vallen, val, set);
2785 static int lmv_pack_md_v1(const struct lmv_stripe_md *lsm,
2786 struct lmv_mds_md_v1 *lmm1)
2791 lmm1->lmv_magic = cpu_to_le32(lsm->lsm_md_magic);
2792 lmm1->lmv_stripe_count = cpu_to_le32(lsm->lsm_md_stripe_count);
2793 lmm1->lmv_master_mdt_index = cpu_to_le32(lsm->lsm_md_master_mdt_index);
2794 lmm1->lmv_hash_type = cpu_to_le32(lsm->lsm_md_hash_type);
2795 cplen = strlcpy(lmm1->lmv_pool_name, lsm->lsm_md_pool_name,
2796 sizeof(lmm1->lmv_pool_name));
2797 if (cplen >= sizeof(lmm1->lmv_pool_name))
2800 for (i = 0; i < lsm->lsm_md_stripe_count; i++)
2801 fid_cpu_to_le(&lmm1->lmv_stripe_fids[i],
2802 &lsm->lsm_md_oinfo[i].lmo_fid);
2806 int lmv_pack_md(union lmv_mds_md **lmmp, const struct lmv_stripe_md *lsm,
2810 bool allocated = false;
2814 LASSERT(lmmp != NULL);
2816 if (*lmmp != NULL && lsm == NULL) {
2819 stripe_count = lmv_mds_md_stripe_count_get(*lmmp);
2820 lmm_size = lmv_mds_md_size(stripe_count,
2821 le32_to_cpu((*lmmp)->lmv_magic));
2824 OBD_FREE(*lmmp, lmm_size);
2830 if (*lmmp == NULL && lsm == NULL) {
2831 lmm_size = lmv_mds_md_size(stripe_count, LMV_MAGIC);
2832 LASSERT(lmm_size > 0);
2833 OBD_ALLOC(*lmmp, lmm_size);
2836 lmv_mds_md_stripe_count_set(*lmmp, stripe_count);
2837 (*lmmp)->lmv_magic = cpu_to_le32(LMV_MAGIC);
2842 LASSERT(lsm != NULL);
2843 lmm_size = lmv_mds_md_size(lsm->lsm_md_stripe_count, lsm->lsm_md_magic);
2844 if (*lmmp == NULL) {
2845 OBD_ALLOC(*lmmp, lmm_size);
2851 switch (lsm->lsm_md_magic) {
2853 rc = lmv_pack_md_v1(lsm, &(*lmmp)->lmv_md_v1);
2860 if (rc != 0 && allocated) {
2861 OBD_FREE(*lmmp, lmm_size);
2867 EXPORT_SYMBOL(lmv_pack_md);
2869 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2870 const struct lmv_mds_md_v1 *lmm1)
2872 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2879 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2880 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2881 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2882 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2883 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2884 fid_le_to_cpu(&lsm->lsm_md_master_fid, &lmm1->lmv_master_fid);
2885 cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2886 sizeof(lsm->lsm_md_pool_name));
2888 if (!fid_is_sane(&lsm->lsm_md_master_fid))
2891 if (cplen >= sizeof(lsm->lsm_md_pool_name))
2894 CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %d"
2895 "layout_version %d\n", lsm->lsm_md_stripe_count,
2896 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
2897 lsm->lsm_md_layout_version);
2899 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2900 for (i = 0; i < le32_to_cpu(stripe_count); i++) {
2901 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
2902 &lmm1->lmv_stripe_fids[i]);
2903 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
2904 &lsm->lsm_md_oinfo[i].lmo_mds);
2907 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
2908 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
2914 int lmv_unpack_md(struct obd_export *exp, struct lmv_stripe_md **lsmp,
2915 const union lmv_mds_md *lmm, int stripe_count)
2917 struct lmv_stripe_md *lsm;
2920 bool allocated = false;
2923 LASSERT(lsmp != NULL);
2927 if (lsm != NULL && lmm == NULL) {
2930 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
2931 /* For migrating inode, the master stripe and master
2932 * object will be the same, so do not need iput, see
2933 * ll_update_lsm_md */
2934 if (!(lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION &&
2935 i == 0) && lsm->lsm_md_oinfo[i].lmo_root != NULL)
2936 iput(lsm->lsm_md_oinfo[i].lmo_root);
2939 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
2940 OBD_FREE(lsm, lsm_size);
2946 if (lsm == NULL && lmm == NULL) {
2947 lsm_size = lmv_stripe_md_size(stripe_count);
2948 OBD_ALLOC(lsm, lsm_size);
2951 lsm->lsm_md_stripe_count = stripe_count;
2956 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
2960 if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
2961 le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
2962 CERROR("%s: invalid lmv magic %x: rc = %d\n",
2963 exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
2968 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
2969 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2972 * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
2973 * stripecount should be 0 then.
2975 lsm_size = lmv_stripe_md_size(0);
2977 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2979 OBD_ALLOC(lsm, lsm_size);
2986 switch (le32_to_cpu(lmm->lmv_magic)) {
2988 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
2991 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
2992 le32_to_cpu(lmm->lmv_magic));
2997 if (rc != 0 && allocated) {
2998 OBD_FREE(lsm, lsm_size);
3005 int lmv_alloc_memmd(struct lmv_stripe_md **lsmp, int stripes)
3007 return lmv_unpack_md(NULL, lsmp, NULL, stripes);
3009 EXPORT_SYMBOL(lmv_alloc_memmd);
3011 void lmv_free_memmd(struct lmv_stripe_md *lsm)
3013 lmv_unpack_md(NULL, &lsm, NULL, 0);
3015 EXPORT_SYMBOL(lmv_free_memmd);
3017 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
3018 struct lov_mds_md *lmm, int disk_len)
3020 return lmv_unpack_md(exp, (struct lmv_stripe_md **)lsmp,
3021 (union lmv_mds_md *)lmm, disk_len);
3024 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
3025 struct lov_stripe_md *lsm)
3027 struct obd_device *obd = exp->exp_obd;
3028 struct lmv_obd *lmv_obd = &obd->u.lmv;
3029 const struct lmv_stripe_md *lmv = (struct lmv_stripe_md *)lsm;
3034 stripe_count = lmv->lsm_md_stripe_count;
3036 stripe_count = lmv_obd->desc.ld_tgt_count;
3038 return lmv_mds_md_size(stripe_count, LMV_MAGIC_V1);
3041 return lmv_pack_md((union lmv_mds_md **)lmmp, lmv, 0);
3044 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3045 ldlm_policy_data_t *policy, ldlm_mode_t mode,
3046 ldlm_cancel_flags_t flags, void *opaque)
3048 struct obd_device *obd = exp->exp_obd;
3049 struct lmv_obd *lmv = &obd->u.lmv;
3055 LASSERT(fid != NULL);
3057 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3058 struct lmv_tgt_desc *tgt = lmv->tgts[i];
3060 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3063 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3071 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
3074 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3075 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3079 if (tgt == NULL || tgt->ltd_exp == NULL)
3081 rc = md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3085 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
3086 const struct lu_fid *fid, ldlm_type_t type,
3087 ldlm_policy_data_t *policy, ldlm_mode_t mode,
3088 struct lustre_handle *lockh)
3090 struct obd_device *obd = exp->exp_obd;
3091 struct lmv_obd *lmv = &obd->u.lmv;
3096 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
3099 * With CMD every object can have two locks in different namespaces:
3100 * lookup lock in space of mds storing direntry and update/open lock in
3101 * space of mds storing inode. Thus we check all targets, not only that
3102 * one fid was created in.
3104 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3105 struct lmv_tgt_desc *tgt = lmv->tgts[i];
3107 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3110 rc = md_lock_match(tgt->ltd_exp, flags, fid, type, policy, mode,
3119 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
3120 struct obd_export *dt_exp, struct obd_export *md_exp,
3121 struct lustre_md *md)
3123 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3124 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3126 if (tgt == NULL || tgt->ltd_exp == NULL)
3129 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
3132 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
3134 struct obd_device *obd = exp->exp_obd;
3135 struct lmv_obd *lmv = &obd->u.lmv;
3136 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3139 if (md->lmv != NULL) {
3140 lmv_free_memmd(md->lmv);
3143 if (tgt == NULL || tgt->ltd_exp == NULL)
3145 RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
3148 int lmv_set_open_replay_data(struct obd_export *exp,
3149 struct obd_client_handle *och,
3150 struct lookup_intent *it)
3152 struct obd_device *obd = exp->exp_obd;
3153 struct lmv_obd *lmv = &obd->u.lmv;
3154 struct lmv_tgt_desc *tgt;
3157 tgt = lmv_find_target(lmv, &och->och_fid);
3159 RETURN(PTR_ERR(tgt));
3161 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3164 int lmv_clear_open_replay_data(struct obd_export *exp,
3165 struct obd_client_handle *och)
3167 struct obd_device *obd = exp->exp_obd;
3168 struct lmv_obd *lmv = &obd->u.lmv;
3169 struct lmv_tgt_desc *tgt;
3172 tgt = lmv_find_target(lmv, &och->och_fid);
3174 RETURN(PTR_ERR(tgt));
3176 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3179 static int lmv_get_remote_perm(struct obd_export *exp,
3180 const struct lu_fid *fid,
3181 struct obd_capa *oc, __u32 suppgid,
3182 struct ptlrpc_request **request)
3184 struct obd_device *obd = exp->exp_obd;
3185 struct lmv_obd *lmv = &obd->u.lmv;
3186 struct lmv_tgt_desc *tgt;
3190 rc = lmv_check_connect(obd);
3194 tgt = lmv_find_target(lmv, fid);
3196 RETURN(PTR_ERR(tgt));
3198 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
3202 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3205 struct obd_device *obd = exp->exp_obd;
3206 struct lmv_obd *lmv = &obd->u.lmv;
3207 struct lmv_tgt_desc *tgt;
3211 rc = lmv_check_connect(obd);
3215 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3217 RETURN(PTR_ERR(tgt));
3219 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3223 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3224 const struct req_msg_field *field, struct obd_capa **oc)
3226 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3227 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3229 if (tgt == NULL || tgt->ltd_exp == NULL)
3231 return md_unpack_capa(tgt->ltd_exp, req, field, oc);
3234 int lmv_intent_getattr_async(struct obd_export *exp,
3235 struct md_enqueue_info *minfo,
3236 struct ldlm_enqueue_info *einfo)
3238 struct md_op_data *op_data = &minfo->mi_data;
3239 struct obd_device *obd = exp->exp_obd;
3240 struct lmv_obd *lmv = &obd->u.lmv;
3241 struct lmv_tgt_desc *tgt = NULL;
3245 rc = lmv_check_connect(obd);
3249 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
3251 RETURN(PTR_ERR(tgt));
3253 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3257 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3258 struct lu_fid *fid, __u64 *bits)
3260 struct obd_device *obd = exp->exp_obd;
3261 struct lmv_obd *lmv = &obd->u.lmv;
3262 struct lmv_tgt_desc *tgt;
3266 rc = lmv_check_connect(obd);
3270 tgt = lmv_find_target(lmv, fid);
3272 RETURN(PTR_ERR(tgt));
3274 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3278 int lmv_get_fid_from_lsm(struct obd_export *exp,
3279 const struct lmv_stripe_md *lsm,
3280 const char *name, int namelen, struct lu_fid *fid)
3282 const struct lmv_oinfo *oinfo;
3284 LASSERT(lsm != NULL);
3285 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
3287 return PTR_ERR(oinfo);
3289 *fid = oinfo->lmo_fid;
3295 * For lmv, only need to send request to master MDT, and the master MDT will
3296 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3297 * we directly fetch data from the slave MDTs.
3299 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3300 struct obd_quotactl *oqctl)
3302 struct obd_device *obd = class_exp2obd(exp);
3303 struct lmv_obd *lmv = &obd->u.lmv;
3304 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3307 __u64 curspace, curinodes;
3311 tgt->ltd_exp == NULL ||
3313 lmv->desc.ld_tgt_count == 0) {
3314 CERROR("master lmv inactive\n");
3318 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3319 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3323 curspace = curinodes = 0;
3324 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3328 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3331 err = obd_quotactl(tgt->ltd_exp, oqctl);
3333 CERROR("getquota on mdt %d failed. %d\n", i, err);
3337 curspace += oqctl->qc_dqblk.dqb_curspace;
3338 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3341 oqctl->qc_dqblk.dqb_curspace = curspace;
3342 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3347 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3348 struct obd_quotactl *oqctl)
3350 struct obd_device *obd = class_exp2obd(exp);
3351 struct lmv_obd *lmv = &obd->u.lmv;
3352 struct lmv_tgt_desc *tgt;
3357 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3360 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
3361 CERROR("lmv idx %d inactive\n", i);
3365 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3373 int lmv_update_lsm_md(struct obd_export *exp, struct lmv_stripe_md *lsm,
3374 struct mdt_body *body, ldlm_blocking_callback cb_blocking)
3376 return lmv_revalidate_slaves(exp, body, lsm, cb_blocking, 0);
3379 int lmv_merge_attr(struct obd_export *exp, const struct lmv_stripe_md *lsm,
3380 struct cl_attr *attr)
3385 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3386 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3388 CDEBUG(D_INFO, ""DFID" size %llu, nlink %u, atime %lu ctime"
3389 "%lu, mtime %lu.\n", PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3390 i_size_read(inode), inode->i_nlink,
3391 LTIME_S(inode->i_atime), LTIME_S(inode->i_ctime),
3392 LTIME_S(inode->i_mtime));
3394 /* for slave stripe, it needs to subtract nlink for . and .. */
3396 attr->cat_nlink += inode->i_nlink - 2;
3398 attr->cat_nlink = inode->i_nlink;
3400 attr->cat_size += i_size_read(inode);
3402 if (attr->cat_atime < LTIME_S(inode->i_atime))
3403 attr->cat_atime = LTIME_S(inode->i_atime);
3405 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3406 attr->cat_ctime = LTIME_S(inode->i_ctime);
3408 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3409 attr->cat_mtime = LTIME_S(inode->i_mtime);
3415 struct obd_ops lmv_obd_ops = {
3416 .o_owner = THIS_MODULE,
3417 .o_setup = lmv_setup,
3418 .o_cleanup = lmv_cleanup,
3419 .o_precleanup = lmv_precleanup,
3420 .o_process_config = lmv_process_config,
3421 .o_connect = lmv_connect,
3422 .o_disconnect = lmv_disconnect,
3423 .o_statfs = lmv_statfs,
3424 .o_get_info = lmv_get_info,
3425 .o_set_info_async = lmv_set_info_async,
3426 .o_packmd = lmv_packmd,
3427 .o_unpackmd = lmv_unpackmd,
3428 .o_notify = lmv_notify,
3429 .o_get_uuid = lmv_get_uuid,
3430 .o_iocontrol = lmv_iocontrol,
3431 .o_quotacheck = lmv_quotacheck,
3432 .o_quotactl = lmv_quotactl
3435 struct md_ops lmv_md_ops = {
3436 .m_getstatus = lmv_getstatus,
3437 .m_null_inode = lmv_null_inode,
3438 .m_find_cbdata = lmv_find_cbdata,
3439 .m_close = lmv_close,
3440 .m_create = lmv_create,
3441 .m_done_writing = lmv_done_writing,
3442 .m_enqueue = lmv_enqueue,
3443 .m_getattr = lmv_getattr,
3444 .m_getxattr = lmv_getxattr,
3445 .m_getattr_name = lmv_getattr_name,
3446 .m_intent_lock = lmv_intent_lock,
3448 .m_rename = lmv_rename,
3449 .m_setattr = lmv_setattr,
3450 .m_setxattr = lmv_setxattr,
3451 .m_fsync = lmv_fsync,
3452 .m_read_entry = lmv_read_entry,
3453 .m_unlink = lmv_unlink,
3454 .m_init_ea_size = lmv_init_ea_size,
3455 .m_cancel_unused = lmv_cancel_unused,
3456 .m_set_lock_data = lmv_set_lock_data,
3457 .m_lock_match = lmv_lock_match,
3458 .m_get_lustre_md = lmv_get_lustre_md,
3459 .m_free_lustre_md = lmv_free_lustre_md,
3460 .m_update_lsm_md = lmv_update_lsm_md,
3461 .m_merge_attr = lmv_merge_attr,
3462 .m_set_open_replay_data = lmv_set_open_replay_data,
3463 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3464 .m_renew_capa = lmv_renew_capa,
3465 .m_unpack_capa = lmv_unpack_capa,
3466 .m_get_remote_perm = lmv_get_remote_perm,
3467 .m_intent_getattr_async = lmv_intent_getattr_async,
3468 .m_revalidate_lock = lmv_revalidate_lock,
3469 .m_get_fid_from_lsm = lmv_get_fid_from_lsm,
3472 int __init lmv_init(void)
3474 return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3475 #ifndef HAVE_ONLY_PROCFS_SEQ
3478 LUSTRE_LMV_NAME, NULL);
3482 static void lmv_exit(void)
3484 class_unregister_type(LUSTRE_LMV_NAME);
3487 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3488 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3489 MODULE_LICENSE("GPL");
3491 module_init(lmv_init);
3492 module_exit(lmv_exit);