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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 * struct OBD_{ALLOC,FREE}*()
55 #include <obd_support.h>
56 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_mdt.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
70 #include <lustre_quota.h>
71 #include <lustre_linkea.h>
72 #include <lustre_lfsck.h>
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 ldlm_mode_t mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
104 static struct lu_kmem_descr mdt_caches[] = {
106 .ckd_cache = &mdt_object_kmem,
107 .ckd_name = "mdt_obj",
108 .ckd_size = sizeof(struct mdt_object)
115 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
119 return (rep->lock_policy_res1 & flag);
122 void mdt_clear_disposition(struct mdt_thread_info *info,
123 struct ldlm_reply *rep, int flag)
126 info->mti_opdata &= ~flag;
128 rep->lock_policy_res1 &= ~flag;
131 void mdt_set_disposition(struct mdt_thread_info *info,
132 struct ldlm_reply *rep, int flag)
135 info->mti_opdata |= flag;
137 rep->lock_policy_res1 |= flag;
140 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
142 lh->mlh_pdo_hash = 0;
143 lh->mlh_reg_mode = lm;
144 lh->mlh_rreg_mode = lm;
145 lh->mlh_type = MDT_REG_LOCK;
148 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
149 const char *name, int namelen)
151 lh->mlh_reg_mode = lm;
152 lh->mlh_rreg_mode = lm;
153 lh->mlh_type = MDT_PDO_LOCK;
155 if (name != NULL && (name[0] != '\0')) {
156 LASSERT(namelen > 0);
157 lh->mlh_pdo_hash = full_name_hash(name, namelen);
158 /* XXX Workaround for LU-2856
159 * Zero is a valid return value of full_name_hash, but several
160 * users of mlh_pdo_hash assume a non-zero hash value. We
161 * therefore map zero onto an arbitrary, but consistent
162 * value (1) to avoid problems further down the road. */
163 if (unlikely(!lh->mlh_pdo_hash))
164 lh->mlh_pdo_hash = 1;
166 LASSERT(namelen == 0);
167 lh->mlh_pdo_hash = 0ull;
171 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
172 struct mdt_lock_handle *lh)
178 * Any dir access needs couple of locks:
180 * 1) on part of dir we gonna take lookup/modify;
182 * 2) on whole dir to protect it from concurrent splitting and/or to
183 * flush client's cache for readdir().
185 * so, for a given mode and object this routine decides what lock mode
186 * to use for lock #2:
188 * 1) if caller's gonna lookup in dir then we need to protect dir from
189 * being splitted only - LCK_CR
191 * 2) if caller's gonna modify dir then we need to protect dir from
192 * being splitted and to flush cache - LCK_CW
194 * 3) if caller's gonna modify dir and that dir seems ready for
195 * splitting then we need to protect it from any type of access
196 * (lookup/modify/split) - LCK_EX --bzzz
199 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
200 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
203 * Ask underlaying level its opinion about preferable PDO lock mode
204 * having access type passed as regular lock mode:
206 * - MDL_MINMODE means that lower layer does not want to specify lock
209 * - MDL_NL means that no PDO lock should be taken. This is used in some
210 * cases. Say, for non-splittable directories no need to use PDO locks
213 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
214 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
216 if (mode != MDL_MINMODE) {
217 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
220 * Lower layer does not want to specify locking mode. We do it
221 * our selves. No special protection is needed, just flush
222 * client's cache on modification and allow concurrent
225 switch (lh->mlh_reg_mode) {
227 lh->mlh_pdo_mode = LCK_EX;
230 lh->mlh_pdo_mode = LCK_CR;
233 lh->mlh_pdo_mode = LCK_CW;
236 CERROR("Not expected lock type (0x%x)\n",
237 (int)lh->mlh_reg_mode);
242 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
246 int mdt_getstatus(struct mdt_thread_info *info)
248 struct mdt_device *mdt = info->mti_mdt;
249 struct mdt_body *repbody;
253 rc = mdt_check_ucred(info);
255 RETURN(err_serious(rc));
257 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
258 RETURN(err_serious(-ENOMEM));
260 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
261 repbody->fid1 = mdt->mdt_md_root_fid;
262 repbody->valid |= OBD_MD_FLID;
264 if (mdt->mdt_opts.mo_mds_capa &&
265 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
266 struct mdt_object *root;
267 struct lustre_capa *capa;
269 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
271 RETURN(PTR_ERR(root));
273 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
275 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
276 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
278 mdt_object_put(info->mti_env, root);
280 repbody->valid |= OBD_MD_FLMDSCAPA;
286 int mdt_statfs(struct mdt_thread_info *info)
288 struct ptlrpc_request *req = mdt_info_req(info);
289 struct md_device *next = info->mti_mdt->mdt_child;
290 struct ptlrpc_service_part *svcpt;
291 struct obd_statfs *osfs;
296 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
298 /* This will trigger a watchdog timeout */
299 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
300 (MDT_SERVICE_WATCHDOG_FACTOR *
301 at_get(&svcpt->scp_at_estimate)) + 1);
303 rc = mdt_check_ucred(info);
305 RETURN(err_serious(rc));
307 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
308 RETURN(err_serious(-ENOMEM));
310 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
314 /** statfs information are cached in the mdt_device */
315 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
316 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
317 /** statfs data is too old, get up-to-date one */
318 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
321 spin_lock(&info->mti_mdt->mdt_osfs_lock);
322 info->mti_mdt->mdt_osfs = *osfs;
323 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
324 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
326 /** use cached statfs data */
327 spin_lock(&info->mti_mdt->mdt_osfs_lock);
328 *osfs = info->mti_mdt->mdt_osfs;
329 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
333 mdt_counter_incr(req, LPROC_MDT_STATFS);
339 * Pack SOM attributes into the reply.
340 * Call under a DLM UPDATE lock.
342 static void mdt_pack_size2body(struct mdt_thread_info *info,
343 struct mdt_object *mo)
346 struct md_attr *ma = &info->mti_attr;
348 LASSERT(ma->ma_attr.la_valid & LA_MODE);
349 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
351 /* Check if Size-on-MDS is supported, if this is a regular file,
352 * if SOM is enabled on the object and if SOM cache exists and valid.
353 * Otherwise do not pack Size-on-MDS attributes to the reply. */
354 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
355 !S_ISREG(ma->ma_attr.la_mode) ||
356 !mdt_object_is_som_enabled(mo) ||
357 !(ma->ma_valid & MA_SOM))
360 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
361 b->size = ma->ma_som->msd_size;
362 b->blocks = ma->ma_som->msd_blocks;
365 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
366 const struct lu_attr *attr, const struct lu_fid *fid)
368 struct md_attr *ma = &info->mti_attr;
370 LASSERT(ma->ma_valid & MA_INODE);
372 b->atime = attr->la_atime;
373 b->mtime = attr->la_mtime;
374 b->ctime = attr->la_ctime;
375 b->mode = attr->la_mode;
376 b->size = attr->la_size;
377 b->blocks = attr->la_blocks;
378 b->uid = attr->la_uid;
379 b->gid = attr->la_gid;
380 b->flags = attr->la_flags;
381 b->nlink = attr->la_nlink;
382 b->rdev = attr->la_rdev;
384 /*XXX should pack the reply body according to lu_valid*/
385 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
386 OBD_MD_FLGID | OBD_MD_FLTYPE |
387 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
388 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
390 if (!S_ISREG(attr->la_mode)) {
391 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
392 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
393 /* means no objects are allocated on osts. */
394 LASSERT(!(ma->ma_valid & MA_LOV));
395 /* just ignore blocks occupied by extend attributes on MDS */
397 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
398 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
399 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm &&
400 (ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
401 /* A released file stores its size on MDS. */
403 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
408 b->valid |= OBD_MD_FLID;
409 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
410 PFID(fid), b->nlink, b->mode, b->size);
414 mdt_body_reverse_idmap(info, b);
416 if (b->valid & OBD_MD_FLSIZE)
417 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
418 PFID(fid), (unsigned long long)b->size);
421 static inline int mdt_body_has_lov(const struct lu_attr *la,
422 const struct mdt_body *body)
424 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
425 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
428 void mdt_client_compatibility(struct mdt_thread_info *info)
430 struct mdt_body *body;
431 struct ptlrpc_request *req = mdt_info_req(info);
432 struct obd_export *exp = req->rq_export;
433 struct md_attr *ma = &info->mti_attr;
434 struct lu_attr *la = &ma->ma_attr;
437 if (exp_connect_layout(exp))
438 /* the client can deal with 16-bit lmm_stripe_count */
441 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
443 if (!mdt_body_has_lov(la, body))
446 /* now we have a reply with a lov for a client not compatible with the
447 * layout lock so we have to clean the layout generation number */
448 if (S_ISREG(la->la_mode))
449 ma->ma_lmm->lmm_layout_gen = 0;
453 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
456 const struct lu_env *env = info->mti_env;
460 LASSERT(info->mti_big_lmm_used == 0);
461 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
465 /* big_lmm may need to be grown */
466 if (info->mti_big_lmmsize < rc) {
467 int size = size_roundup_power2(rc);
469 if (info->mti_big_lmmsize > 0) {
470 /* free old buffer */
471 LASSERT(info->mti_big_lmm);
472 OBD_FREE_LARGE(info->mti_big_lmm,
473 info->mti_big_lmmsize);
474 info->mti_big_lmm = NULL;
475 info->mti_big_lmmsize = 0;
478 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
479 if (info->mti_big_lmm == NULL)
481 info->mti_big_lmmsize = size;
483 LASSERT(info->mti_big_lmmsize >= rc);
485 info->mti_buf.lb_buf = info->mti_big_lmm;
486 info->mti_buf.lb_len = info->mti_big_lmmsize;
487 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
492 int mdt_attr_get_lov(struct mdt_thread_info *info,
493 struct mdt_object *o, struct md_attr *ma)
495 struct md_object *next = mdt_object_child(o);
496 struct lu_buf *buf = &info->mti_buf;
499 buf->lb_buf = ma->ma_lmm;
500 buf->lb_len = ma->ma_lmm_size;
501 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
503 ma->ma_lmm_size = rc;
504 ma->ma_valid |= MA_LOV;
506 } else if (rc == -ENODATA) {
509 } else if (rc == -ERANGE) {
510 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
512 info->mti_big_lmm_used = 1;
513 ma->ma_valid |= MA_LOV;
514 ma->ma_lmm = info->mti_big_lmm;
515 ma->ma_lmm_size = rc;
516 /* update mdt_max_mdsize so all clients
517 * will be aware about that */
518 if (info->mti_mdt->mdt_max_mdsize < rc)
519 info->mti_mdt->mdt_max_mdsize = rc;
527 int mdt_attr_get_pfid(struct mdt_thread_info *info,
528 struct mdt_object *o, struct lu_fid *pfid)
530 struct lu_buf *buf = &info->mti_buf;
531 struct link_ea_header *leh;
532 struct link_ea_entry *lee;
536 buf->lb_buf = info->mti_big_lmm;
537 buf->lb_len = info->mti_big_lmmsize;
538 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
539 buf, XATTR_NAME_LINK);
540 /* ignore errors, MA_PFID won't be set and it is
541 * up to the caller to treat this as an error */
542 if (rc == -ERANGE || buf->lb_len == 0) {
543 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
544 buf->lb_buf = info->mti_big_lmm;
545 buf->lb_len = info->mti_big_lmmsize;
550 if (rc < sizeof(*leh)) {
551 CERROR("short LinkEA on "DFID": rc = %d\n",
552 PFID(mdt_object_fid(o)), rc);
556 leh = (struct link_ea_header *) buf->lb_buf;
557 lee = (struct link_ea_entry *)(leh + 1);
558 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
559 leh->leh_magic = LINK_EA_MAGIC;
560 leh->leh_reccount = __swab32(leh->leh_reccount);
561 leh->leh_len = __swab64(leh->leh_len);
563 if (leh->leh_magic != LINK_EA_MAGIC)
565 if (leh->leh_reccount == 0)
568 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
569 fid_be_to_cpu(pfid, pfid);
574 int mdt_attr_get_complex(struct mdt_thread_info *info,
575 struct mdt_object *o, struct md_attr *ma)
577 const struct lu_env *env = info->mti_env;
578 struct md_object *next = mdt_object_child(o);
579 struct lu_buf *buf = &info->mti_buf;
580 u32 mode = lu_object_attr(&next->mo_lu);
581 int need = ma->ma_need;
587 if (need & MA_INODE) {
588 ma->ma_need = MA_INODE;
589 rc = mo_attr_get(env, next, ma);
592 ma->ma_valid |= MA_INODE;
595 if (need & MA_PFID) {
596 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
598 ma->ma_valid |= MA_PFID;
599 /* ignore this error, parent fid is not mandatory */
603 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
604 rc = mdt_attr_get_lov(info, o, ma);
609 if (need & MA_LMV && S_ISDIR(mode)) {
610 buf->lb_buf = ma->ma_lmv;
611 buf->lb_len = ma->ma_lmv_size;
612 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
614 ma->ma_lmv_size = rc2;
615 ma->ma_valid |= MA_LMV;
616 } else if (rc2 == -ENODATA) {
623 if (need & MA_SOM && S_ISREG(mode)) {
624 buf->lb_buf = info->mti_xattr_buf;
625 buf->lb_len = sizeof(info->mti_xattr_buf);
626 CLASSERT(sizeof(struct som_attrs) <=
627 sizeof(info->mti_xattr_buf));
628 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
629 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
631 ma->ma_valid |= MA_SOM;
632 else if (rc2 < 0 && rc2 != -ENODATA)
636 if (need & MA_HSM && S_ISREG(mode)) {
637 buf->lb_buf = info->mti_xattr_buf;
638 buf->lb_len = sizeof(info->mti_xattr_buf);
639 CLASSERT(sizeof(struct hsm_attrs) <=
640 sizeof(info->mti_xattr_buf));
641 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
642 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
644 ma->ma_valid |= MA_HSM;
645 else if (rc2 < 0 && rc2 != -ENODATA)
649 #ifdef CONFIG_FS_POSIX_ACL
650 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
651 buf->lb_buf = ma->ma_acl;
652 buf->lb_len = ma->ma_acl_size;
653 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
655 ma->ma_acl_size = rc2;
656 ma->ma_valid |= MA_ACL_DEF;
657 } else if (rc2 == -ENODATA) {
666 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
667 rc, ma->ma_valid, ma->ma_lmm);
671 static int mdt_getattr_internal(struct mdt_thread_info *info,
672 struct mdt_object *o, int ma_need)
674 struct md_object *next = mdt_object_child(o);
675 const struct mdt_body *reqbody = info->mti_body;
676 struct ptlrpc_request *req = mdt_info_req(info);
677 struct md_attr *ma = &info->mti_attr;
678 struct lu_attr *la = &ma->ma_attr;
679 struct req_capsule *pill = info->mti_pill;
680 const struct lu_env *env = info->mti_env;
681 struct mdt_body *repbody;
682 struct lu_buf *buffer = &info->mti_buf;
687 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
688 RETURN(err_serious(-ENOMEM));
690 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
694 if (mdt_object_remote(o)) {
695 /* This object is located on remote node.*/
696 /* Return -EIO for old client */
697 if (!mdt_is_dne_client(req->rq_export))
698 GOTO(out, rc = -EIO);
700 repbody->fid1 = *mdt_object_fid(o);
701 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
705 buffer->lb_len = reqbody->eadatasize;
706 if (buffer->lb_len > 0)
707 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
709 buffer->lb_buf = NULL;
711 /* If it is dir object and client require MEA, then we got MEA */
712 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
713 reqbody->valid & OBD_MD_MEA) {
714 /* Assumption: MDT_MD size is enough for lmv size. */
715 ma->ma_lmv = buffer->lb_buf;
716 ma->ma_lmv_size = buffer->lb_len;
717 ma->ma_need = MA_LMV | MA_INODE;
719 ma->ma_lmm = buffer->lb_buf;
720 ma->ma_lmm_size = buffer->lb_len;
721 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
724 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
725 reqbody->valid & OBD_MD_FLDIREA &&
726 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
727 /* get default stripe info for this dir. */
728 ma->ma_need |= MA_LOV_DEF;
730 ma->ma_need |= ma_need;
731 if (ma->ma_need & MA_SOM)
732 ma->ma_som = &info->mti_u.som.data;
734 rc = mdt_attr_get_complex(info, o, ma);
736 CERROR("%s: getattr error for "DFID": rc = %d\n",
737 mdt_obd_name(info->mti_mdt),
738 PFID(mdt_object_fid(o)), rc);
742 /* if file is released, check if a restore is running */
743 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
744 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
745 repbody->t_state = MS_RESTORE;
746 repbody->valid |= OBD_MD_TSTATE;
749 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
751 /* the Lustre protocol supposes to return default striping
752 * on the user-visible root if explicitly requested */
753 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
754 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
755 struct lu_fid rootfid;
756 struct mdt_object *root;
757 struct mdt_device *mdt = info->mti_mdt;
759 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
762 root = mdt_object_find(env, mdt, &rootfid);
764 RETURN(PTR_ERR(root));
765 rc = mdt_attr_get_lov(info, root, ma);
766 mdt_object_put(info->mti_env, root);
768 CERROR("%s: getattr error for "DFID": rc = %d\n",
769 mdt_obd_name(info->mti_mdt),
770 PFID(mdt_object_fid(o)), rc);
775 if (likely(ma->ma_valid & MA_INODE))
776 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
780 if (mdt_body_has_lov(la, reqbody)) {
781 if (ma->ma_valid & MA_LOV) {
782 LASSERT(ma->ma_lmm_size);
783 mdt_dump_lmm(D_INFO, ma->ma_lmm);
784 repbody->eadatasize = ma->ma_lmm_size;
785 if (S_ISDIR(la->la_mode))
786 repbody->valid |= OBD_MD_FLDIREA;
788 repbody->valid |= OBD_MD_FLEASIZE;
790 if (ma->ma_valid & MA_LMV) {
791 LASSERT(S_ISDIR(la->la_mode));
792 repbody->eadatasize = ma->ma_lmv_size;
793 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
795 } else if (S_ISLNK(la->la_mode) &&
796 reqbody->valid & OBD_MD_LINKNAME) {
797 buffer->lb_buf = ma->ma_lmm;
798 /* eadatasize from client includes NULL-terminator, so
799 * there is no need to read it */
800 buffer->lb_len = reqbody->eadatasize - 1;
801 rc = mo_readlink(env, next, buffer);
802 if (unlikely(rc <= 0)) {
803 CERROR("%s: readlink failed for "DFID": rc = %d\n",
804 mdt_obd_name(info->mti_mdt),
805 PFID(mdt_object_fid(o)), rc);
808 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
810 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
812 repbody->valid |= OBD_MD_LINKNAME;
813 /* we need to report back size with NULL-terminator
814 * because client expects that */
815 repbody->eadatasize = rc + 1;
816 if (repbody->eadatasize != reqbody->eadatasize)
817 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
818 "on "DFID ", expected %d\n",
819 mdt_obd_name(info->mti_mdt),
820 rc, PFID(mdt_object_fid(o)),
821 reqbody->eadatasize - 1);
823 ((char *)ma->ma_lmm)[rc] = 0;
825 /* If the total CDEBUG() size is larger than a page, it
826 * will print a warning to the console, avoid this by
827 * printing just the last part of the symlink. */
828 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
829 print_limit < rc ? "..." : "", print_limit,
830 (char *)ma->ma_lmm + rc - print_limit, rc);
835 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
836 repbody->max_cookiesize = 0;
837 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
838 repbody->valid |= OBD_MD_FLMODEASIZE;
839 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
840 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
841 repbody->max_cookiesize);
844 if (exp_connect_rmtclient(info->mti_exp) &&
845 reqbody->valid & OBD_MD_FLRMTPERM) {
846 void *buf = req_capsule_server_get(pill, &RMF_ACL);
848 /* mdt_getattr_lock only */
849 rc = mdt_pack_remote_perm(info, o, buf);
851 repbody->valid &= ~OBD_MD_FLRMTPERM;
852 repbody->aclsize = 0;
855 repbody->valid |= OBD_MD_FLRMTPERM;
856 repbody->aclsize = sizeof(struct mdt_remote_perm);
859 #ifdef CONFIG_FS_POSIX_ACL
860 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
861 (reqbody->valid & OBD_MD_FLACL)) {
862 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
863 buffer->lb_len = req_capsule_get_size(pill,
864 &RMF_ACL, RCL_SERVER);
865 if (buffer->lb_len > 0) {
866 rc = mo_xattr_get(env, next, buffer,
867 XATTR_NAME_ACL_ACCESS);
869 if (rc == -ENODATA) {
870 repbody->aclsize = 0;
871 repbody->valid |= OBD_MD_FLACL;
873 } else if (rc == -EOPNOTSUPP) {
876 CERROR("%s: unable to read "DFID
878 mdt_obd_name(info->mti_mdt),
879 PFID(mdt_object_fid(o)), rc);
882 repbody->aclsize = rc;
883 repbody->valid |= OBD_MD_FLACL;
890 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
891 info->mti_mdt->mdt_opts.mo_mds_capa &&
892 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
893 struct lustre_capa *capa;
895 capa = req_capsule_server_get(pill, &RMF_CAPA1);
897 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
898 rc = mo_capa_get(env, next, capa, 0);
901 repbody->valid |= OBD_MD_FLMDSCAPA;
906 mdt_counter_incr(req, LPROC_MDT_GETATTR);
911 static int mdt_renew_capa(struct mdt_thread_info *info)
913 struct mdt_object *obj = info->mti_object;
914 struct mdt_body *body;
915 struct lustre_capa *capa, *c;
919 /* if object doesn't exist, or server has disabled capability,
920 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
923 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
924 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
927 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
928 LASSERT(body != NULL);
930 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
933 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
937 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
939 body->valid |= OBD_MD_FLOSSCAPA;
943 int mdt_getattr(struct mdt_thread_info *info)
945 struct mdt_object *obj = info->mti_object;
946 struct req_capsule *pill = info->mti_pill;
947 struct mdt_body *reqbody;
948 struct mdt_body *repbody;
953 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
956 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
957 rc = req_capsule_server_pack(pill);
959 RETURN(err_serious(rc));
960 rc = mdt_renew_capa(info);
961 GOTO(out_shrink, rc);
964 LASSERT(obj != NULL);
965 LASSERT(lu_object_assert_exists(&obj->mot_obj));
967 mode = lu_object_attr(&obj->mot_obj);
969 /* old clients may not report needed easize, use max value then */
970 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
971 reqbody->eadatasize == 0 ?
972 info->mti_mdt->mdt_max_mdsize :
973 reqbody->eadatasize);
975 rc = req_capsule_server_pack(pill);
976 if (unlikely(rc != 0))
977 RETURN(err_serious(rc));
979 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
980 LASSERT(repbody != NULL);
981 repbody->eadatasize = 0;
982 repbody->aclsize = 0;
984 if (reqbody->valid & OBD_MD_FLRMTPERM)
985 rc = mdt_init_ucred(info, reqbody);
987 rc = mdt_check_ucred(info);
989 GOTO(out_shrink, rc);
991 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
994 * Don't check capability at all, because rename might getattr for
995 * remote obj, and at that time no capability is available.
997 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
998 rc = mdt_getattr_internal(info, obj, 0);
999 if (reqbody->valid & OBD_MD_FLRMTPERM)
1000 mdt_exit_ucred(info);
1003 mdt_client_compatibility(info);
1004 rc2 = mdt_fix_reply(info);
1010 int mdt_is_subdir(struct mdt_thread_info *info)
1012 struct mdt_object *o = info->mti_object;
1013 struct req_capsule *pill = info->mti_pill;
1014 const struct mdt_body *body = info->mti_body;
1015 struct mdt_body *repbody;
1021 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1024 * We save last checked parent fid to @repbody->fid1 for remote
1027 LASSERT(fid_is_sane(&body->fid2));
1028 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1029 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1030 &body->fid2, &repbody->fid1);
1031 if (rc == 0 || rc == -EREMOTE)
1032 repbody->valid |= OBD_MD_FLID;
1037 int mdt_swap_layouts(struct mdt_thread_info *info)
1039 struct ptlrpc_request *req = mdt_info_req(info);
1040 struct obd_export *exp = req->rq_export;
1041 struct mdt_object *o1, *o2, *o;
1042 struct mdt_lock_handle *lh1, *lh2;
1043 struct mdc_swap_layouts *msl;
1047 /* client does not support layout lock, so layout swaping
1049 * FIXME: there is a problem for old clients which don't support
1050 * layout lock yet. If those clients have already opened the file
1051 * they won't be notified at all so that old layout may still be
1052 * used to do IO. This can be fixed after file release is landed by
1053 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1054 if (!exp_connect_layout(exp))
1055 RETURN(-EOPNOTSUPP);
1057 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1058 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1059 req_capsule_client_get(info->mti_pill,
1062 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1063 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1064 req_capsule_client_get(info->mti_pill,
1067 o1 = info->mti_object;
1068 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1069 &info->mti_body->fid2);
1071 GOTO(out, rc = PTR_ERR(o));
1073 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1074 GOTO(put, rc = -ENOENT);
1076 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1077 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1083 /* permission check. Make sure the calling process having permission
1084 * to write both files. */
1085 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1090 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1095 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1097 GOTO(put, rc = -EPROTO);
1099 lh1 = &info->mti_lh[MDT_LH_NEW];
1100 mdt_lock_reg_init(lh1, LCK_EX);
1101 lh2 = &info->mti_lh[MDT_LH_OLD];
1102 mdt_lock_reg_init(lh2, LCK_EX);
1104 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT,
1109 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT,
1114 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1115 mdt_object_child(o2), msl->msl_flags);
1118 mdt_object_unlock(info, o2, lh2, rc);
1120 mdt_object_unlock(info, o1, lh1, rc);
1122 mdt_object_put(info->mti_env, o);
1127 static int mdt_raw_lookup(struct mdt_thread_info *info,
1128 struct mdt_object *parent,
1129 const struct lu_name *lname,
1130 struct ldlm_reply *ldlm_rep)
1132 struct md_object *next = mdt_object_child(info->mti_object);
1133 const struct mdt_body *reqbody = info->mti_body;
1134 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1135 struct mdt_body *repbody;
1139 if (reqbody->valid != OBD_MD_FLID)
1142 LASSERT(!info->mti_cross_ref);
1144 /* Only got the fid of this obj by name */
1145 fid_zero(child_fid);
1146 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1149 /* XXX is raw_lookup possible as intent operation? */
1152 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1155 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1157 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1160 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1161 repbody->fid1 = *child_fid;
1162 repbody->valid = OBD_MD_FLID;
1168 * UPDATE lock should be taken against parent, and be release before exit;
1169 * child_bits lock should be taken against child, and be returned back:
1170 * (1)normal request should release the child lock;
1171 * (2)intent request will grant the lock to client.
1173 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1174 struct mdt_lock_handle *lhc,
1176 struct ldlm_reply *ldlm_rep)
1178 struct ptlrpc_request *req = mdt_info_req(info);
1179 struct mdt_body *reqbody = NULL;
1180 struct mdt_object *parent = info->mti_object;
1181 struct mdt_object *child;
1182 struct md_object *next = mdt_object_child(parent);
1183 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1184 struct lu_name *lname = NULL;
1185 const char *name = NULL;
1187 struct mdt_lock_handle *lhp = NULL;
1188 struct ldlm_lock *lock;
1189 struct ldlm_res_id *res_id;
1196 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1197 LASSERT(ergo(is_resent,
1198 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1200 LASSERT(parent != NULL);
1201 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1203 RETURN(err_serious(-EFAULT));
1205 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1207 if (!info->mti_cross_ref) {
1209 * XXX: Check for "namelen == 0" is for getattr by fid
1210 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1211 * that is the name must contain at least one character and
1212 * the terminating '\0'
1215 reqbody = req_capsule_client_get(info->mti_pill,
1217 if (unlikely(reqbody == NULL))
1218 RETURN(err_serious(-EFAULT));
1220 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1221 RETURN(err_serious(-EINVAL));
1224 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1226 PFID(mdt_object_fid(parent)),
1227 PFID(&reqbody->fid2), ldlm_rep);
1229 lname = mdt_name(info->mti_env, (char *)name, namelen);
1230 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1231 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1235 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1237 if (unlikely(!mdt_object_exists(parent))) {
1238 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1240 "Parent doesn't exist!\n");
1242 } else if (!info->mti_cross_ref) {
1243 LASSERTF(!mdt_object_remote(parent),
1244 "Parent "DFID" is on remote server\n",
1245 PFID(mdt_object_fid(parent)));
1248 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1256 if (info->mti_cross_ref) {
1257 /* Only getattr on the child. Parent is on another node. */
1258 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1260 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1261 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1264 /* Do not take lock for resent case. */
1265 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1266 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1267 lhc->mlh_reg_lh.cookie);
1268 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1269 &lock->l_resource->lr_name));
1270 LDLM_LOCK_PUT(lock);
1273 mdt_lock_handle_init(lhc);
1274 mdt_lock_reg_init(lhc, LCK_PR);
1277 * Object's name is on another MDS, no lookup lock is
1278 * needed here but update is.
1280 child_bits &= ~MDS_INODELOCK_LOOKUP;
1281 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1283 rc = mdt_object_lock(info, child, lhc, child_bits,
1287 /* Finally, we can get attr for child. */
1288 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1290 rc = mdt_getattr_internal(info, child, 0);
1291 if (unlikely(rc != 0))
1292 mdt_object_unlock(info, child, lhc, 1);
1298 /* step 1: lock parent only if parent is a directory */
1299 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1300 lhp = &info->mti_lh[MDT_LH_PARENT];
1301 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1302 rc = mdt_object_lock(info, parent, lhp,
1303 MDS_INODELOCK_UPDATE,
1305 if (unlikely(rc != 0))
1309 /* step 2: lookup child's fid by name */
1310 fid_zero(child_fid);
1311 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1316 mdt_set_disposition(info, ldlm_rep,
1318 GOTO(out_parent, rc);
1320 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1322 *child_fid = reqbody->fid2;
1323 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1327 *step 3: find the child object by fid & lock it.
1328 * regardless if it is local or remote.
1330 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1332 if (unlikely(IS_ERR(child)))
1333 GOTO(out_parent, rc = PTR_ERR(child));
1335 /* Do not take lock for resent case. */
1336 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1337 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1338 lhc->mlh_reg_lh.cookie);
1340 res_id = &lock->l_resource->lr_name;
1341 if (!fid_res_name_eq(mdt_object_fid(child),
1342 &lock->l_resource->lr_name)) {
1343 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1344 &lock->l_resource->lr_name),
1345 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1346 PLDLMRES(lock->l_resource),
1347 PFID(mdt_object_fid(parent)));
1348 CWARN("Although resent, but still not get child lock"
1349 "parent:"DFID" child:"DFID"\n",
1350 PFID(mdt_object_fid(parent)),
1351 PFID(mdt_object_fid(child)));
1352 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1353 LDLM_LOCK_PUT(lock);
1356 LDLM_LOCK_PUT(lock);
1359 bool try_layout = false;
1362 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1363 mdt_lock_handle_init(lhc);
1364 mdt_lock_reg_init(lhc, LCK_PR);
1366 if (!mdt_object_exists(child)) {
1367 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1369 "Object doesn't exist!\n");
1370 GOTO(out_child, rc = -ENOENT);
1373 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1374 mdt_object_exists(child) && !mdt_object_remote(child)) {
1375 struct md_attr *ma = &info->mti_attr;
1378 ma->ma_need = MA_INODE;
1379 rc = mdt_attr_get_complex(info, child, ma);
1380 if (unlikely(rc != 0))
1381 GOTO(out_child, rc);
1383 /* If the file has not been changed for some time, we
1384 * return not only a LOOKUP lock, but also an UPDATE
1385 * lock and this might save us RPC on later STAT. For
1386 * directories, it also let negative dentry starts
1387 * working for this dir. */
1388 if (ma->ma_valid & MA_INODE &&
1389 ma->ma_attr.la_valid & LA_CTIME &&
1390 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1391 ma->ma_attr.la_ctime < cfs_time_current_sec())
1392 child_bits |= MDS_INODELOCK_UPDATE;
1395 /* layout lock must be granted in a best-effort way
1396 * for IT operations */
1397 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1398 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1399 exp_connect_layout(info->mti_exp) &&
1400 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1402 /* try to grant layout lock for regular file. */
1408 child_bits |= MDS_INODELOCK_LAYOUT;
1409 /* try layout lock, it may fail to be granted due to
1410 * contention at LOOKUP or UPDATE */
1411 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1413 child_bits &= ~MDS_INODELOCK_LAYOUT;
1414 LASSERT(child_bits != 0);
1415 rc = mdt_object_lock(info, child, lhc,
1416 child_bits, MDT_CROSS_LOCK);
1421 rc = mdt_object_lock(info, child, lhc, child_bits,
1424 if (unlikely(rc != 0))
1425 GOTO(out_child, rc);
1428 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1429 /* Get MA_SOM attributes if update lock is given. */
1431 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1432 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1435 /* finally, we can get attr for child. */
1436 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1437 rc = mdt_getattr_internal(info, child, ma_need);
1438 if (unlikely(rc != 0)) {
1439 mdt_object_unlock(info, child, lhc, 1);
1441 /* Debugging code. */
1442 res_id = &lock->l_resource->lr_name;
1443 LDLM_DEBUG(lock, "Returning lock to client");
1444 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1445 &lock->l_resource->lr_name),
1446 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1447 PLDLMRES(lock->l_resource),
1448 PFID(mdt_object_fid(child)));
1449 if (mdt_object_exists(child) && !mdt_object_remote(child))
1450 mdt_pack_size2body(info, child);
1453 LDLM_LOCK_PUT(lock);
1457 mdt_object_put(info->mti_env, child);
1460 mdt_object_unlock(info, parent, lhp, 1);
1464 /* normal handler: should release the child lock */
1465 int mdt_getattr_name(struct mdt_thread_info *info)
1467 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1468 struct mdt_body *reqbody;
1469 struct mdt_body *repbody;
1473 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1474 LASSERT(reqbody != NULL);
1475 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1476 LASSERT(repbody != NULL);
1478 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1479 repbody->eadatasize = 0;
1480 repbody->aclsize = 0;
1482 rc = mdt_init_ucred(info, reqbody);
1484 GOTO(out_shrink, rc);
1486 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1487 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1488 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1489 lhc->mlh_reg_lh.cookie = 0;
1491 mdt_exit_ucred(info);
1494 mdt_client_compatibility(info);
1495 rc2 = mdt_fix_reply(info);
1501 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1502 void *karg, void *uarg);
1504 int mdt_set_info(struct mdt_thread_info *info)
1506 struct ptlrpc_request *req = mdt_info_req(info);
1509 int keylen, vallen, rc = 0;
1512 rc = req_capsule_server_pack(info->mti_pill);
1516 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1518 DEBUG_REQ(D_HA, req, "no set_info key");
1522 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1525 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1527 DEBUG_REQ(D_HA, req, "no set_info val");
1531 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1534 /* Swab any part of val you need to here */
1535 if (KEY_IS(KEY_READ_ONLY)) {
1537 lustre_msg_set_status(req->rq_repmsg, 0);
1539 spin_lock(&req->rq_export->exp_lock);
1541 *exp_connect_flags_ptr(req->rq_export) |=
1544 *exp_connect_flags_ptr(req->rq_export) &=
1545 ~OBD_CONNECT_RDONLY;
1546 spin_unlock(&req->rq_export->exp_lock);
1548 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1549 struct changelog_setinfo *cs =
1550 (struct changelog_setinfo *)val;
1551 if (vallen != sizeof(*cs)) {
1552 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1555 if (ptlrpc_req_need_swab(req)) {
1556 __swab64s(&cs->cs_recno);
1557 __swab32s(&cs->cs_id);
1560 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1562 lustre_msg_set_status(req->rq_repmsg, rc);
1571 * Top-level handler for MDT connection requests.
1573 int mdt_connect(struct mdt_thread_info *info)
1576 struct obd_connect_data *reply;
1577 struct obd_export *exp;
1578 struct ptlrpc_request *req = mdt_info_req(info);
1580 rc = target_handle_connect(req);
1582 return err_serious(rc);
1584 LASSERT(req->rq_export != NULL);
1585 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1586 rc = mdt_init_sec_level(info);
1588 obd_disconnect(class_export_get(req->rq_export));
1592 /* To avoid exposing partially initialized connection flags, changes up
1593 * to this point have been staged in reply->ocd_connect_flags. Now that
1594 * connection handling has completed successfully, atomically update
1595 * the connect flags in the shared export data structure. LU-1623 */
1596 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1597 exp = req->rq_export;
1598 spin_lock(&exp->exp_lock);
1599 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1600 spin_unlock(&exp->exp_lock);
1602 rc = mdt_init_idmap(info);
1604 obd_disconnect(class_export_get(req->rq_export));
1609 int mdt_disconnect(struct mdt_thread_info *info)
1614 rc = target_handle_disconnect(mdt_info_req(info));
1616 rc = err_serious(rc);
1620 static int mdt_sendpage(struct mdt_thread_info *info,
1621 struct lu_rdpg *rdpg, int nob)
1623 struct ptlrpc_request *req = mdt_info_req(info);
1624 struct obd_export *exp = req->rq_export;
1625 struct ptlrpc_bulk_desc *desc;
1626 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1633 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1638 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1639 /* old client requires reply size in it's PAGE_SIZE,
1640 * which is rdpg->rp_count */
1641 nob = rdpg->rp_count;
1643 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1644 i++, tmpcount -= tmpsize) {
1645 tmpsize = min_t(int, tmpcount, PAGE_CACHE_SIZE);
1646 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1649 LASSERT(desc->bd_nob == nob);
1650 rc = target_bulk_io(exp, desc, lwi);
1651 ptlrpc_free_bulk_pin(desc);
1655 int mdt_readpage(struct mdt_thread_info *info)
1657 struct mdt_object *object = info->mti_object;
1658 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1659 struct mdt_body *reqbody;
1660 struct mdt_body *repbody;
1665 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1666 RETURN(err_serious(-ENOMEM));
1668 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1669 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1670 if (reqbody == NULL || repbody == NULL)
1671 RETURN(err_serious(-EFAULT));
1674 * prepare @rdpg before calling lower layers and transfer itself. Here
1675 * reqbody->size contains offset of where to start to read and
1676 * reqbody->nlink contains number bytes to read.
1678 rdpg->rp_hash = reqbody->size;
1679 if (rdpg->rp_hash != reqbody->size) {
1680 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1681 rdpg->rp_hash, reqbody->size);
1685 rdpg->rp_attrs = reqbody->mode;
1686 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1687 rdpg->rp_attrs |= LUDA_64BITHASH;
1688 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1689 exp_max_brw_size(info->mti_exp));
1690 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1692 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1693 if (rdpg->rp_pages == NULL)
1696 for (i = 0; i < rdpg->rp_npages; ++i) {
1697 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1698 if (rdpg->rp_pages[i] == NULL)
1699 GOTO(free_rdpg, rc = -ENOMEM);
1702 /* call lower layers to fill allocated pages with directory data */
1703 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1705 GOTO(free_rdpg, rc);
1707 /* send pages to client */
1708 rc = mdt_sendpage(info, rdpg, rc);
1713 for (i = 0; i < rdpg->rp_npages; i++)
1714 if (rdpg->rp_pages[i] != NULL)
1715 __free_page(rdpg->rp_pages[i]);
1716 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1718 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1724 static int mdt_reint_internal(struct mdt_thread_info *info,
1725 struct mdt_lock_handle *lhc,
1728 struct req_capsule *pill = info->mti_pill;
1729 struct mdt_body *repbody;
1734 rc = mdt_reint_unpack(info, op);
1736 CERROR("Can't unpack reint, rc %d\n", rc);
1737 RETURN(err_serious(rc));
1740 /* for replay (no_create) lmm is not needed, client has it already */
1741 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1742 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1743 info->mti_rr.rr_eadatalen);
1745 /* llog cookies are always 0, the field is kept for compatibility */
1746 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1747 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1749 rc = req_capsule_server_pack(pill);
1751 CERROR("Can't pack response, rc %d\n", rc);
1752 RETURN(err_serious(rc));
1755 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1756 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1758 repbody->eadatasize = 0;
1759 repbody->aclsize = 0;
1762 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1764 /* for replay no cookkie / lmm need, because client have this already */
1765 if (info->mti_spec.no_create)
1766 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1767 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1769 rc = mdt_init_ucred_reint(info);
1771 GOTO(out_shrink, rc);
1773 rc = mdt_fix_attr_ucred(info, op);
1775 GOTO(out_ucred, rc = err_serious(rc));
1777 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1778 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1779 GOTO(out_ucred, rc);
1781 rc = mdt_reint_rec(info, lhc);
1784 mdt_exit_ucred(info);
1786 mdt_client_compatibility(info);
1787 rc2 = mdt_fix_reply(info);
1793 static long mdt_reint_opcode(struct mdt_thread_info *info,
1794 const struct req_format **fmt)
1796 struct mdt_rec_reint *rec;
1799 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1801 opc = rec->rr_opcode;
1802 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1803 if (opc < REINT_MAX && fmt[opc] != NULL)
1804 req_capsule_extend(info->mti_pill, fmt[opc]);
1806 CERROR("%s: Unsupported opcode '%ld' from client '%s': "
1807 "rc = %d\n", mdt_obd_name(info->mti_mdt), opc,
1808 info->mti_mdt->mdt_ldlm_client->cli_name,
1810 opc = err_serious(-EFAULT);
1813 opc = err_serious(-EFAULT);
1818 int mdt_reint(struct mdt_thread_info *info)
1823 static const struct req_format *reint_fmts[REINT_MAX] = {
1824 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1825 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1826 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1827 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1828 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1829 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1830 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1831 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1836 opc = mdt_reint_opcode(info, reint_fmts);
1839 * No lock possible here from client to pass it to reint code
1842 rc = mdt_reint_internal(info, NULL, opc);
1847 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1851 /* this should sync the whole device */
1852 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1854 struct dt_device *dt = mdt->mdt_bottom;
1858 rc = dt->dd_ops->dt_sync(env, dt);
1862 /* this should sync this object */
1863 static int mdt_object_sync(struct mdt_thread_info *info)
1865 struct md_object *next;
1869 if (!mdt_object_exists(info->mti_object)) {
1870 CWARN("Non existing object "DFID"!\n",
1871 PFID(mdt_object_fid(info->mti_object)));
1874 next = mdt_object_child(info->mti_object);
1875 rc = mo_object_sync(info->mti_env, next);
1880 int mdt_sync(struct mdt_thread_info *info)
1882 struct ptlrpc_request *req = mdt_info_req(info);
1883 struct req_capsule *pill = info->mti_pill;
1884 struct mdt_body *body;
1888 /* The fid may be zero, so we req_capsule_set manually */
1889 req_capsule_set(pill, &RQF_MDS_SYNC);
1891 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1893 RETURN(err_serious(-EINVAL));
1895 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1896 RETURN(err_serious(-ENOMEM));
1898 if (fid_seq(&body->fid1) == 0) {
1899 /* sync the whole device */
1900 rc = req_capsule_server_pack(pill);
1902 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1904 rc = err_serious(rc);
1906 /* sync an object */
1907 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1909 rc = mdt_object_sync(info);
1911 const struct lu_fid *fid;
1912 struct lu_attr *la = &info->mti_attr.ma_attr;
1914 info->mti_attr.ma_need = MA_INODE;
1915 info->mti_attr.ma_valid = 0;
1916 rc = mdt_attr_get_complex(info, info->mti_object,
1919 body = req_capsule_server_get(pill,
1921 fid = mdt_object_fid(info->mti_object);
1922 mdt_pack_attr2body(info, body, la, fid);
1926 rc = err_serious(rc);
1929 mdt_counter_incr(req, LPROC_MDT_SYNC);
1935 * Quotacheck handler.
1936 * in-kernel quotacheck isn't supported any more.
1938 int mdt_quotacheck(struct mdt_thread_info *info)
1940 struct obd_quotactl *oqctl;
1944 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1946 RETURN(err_serious(-EPROTO));
1948 rc = req_capsule_server_pack(info->mti_pill);
1950 RETURN(err_serious(rc));
1952 /* deprecated, not used any more */
1953 RETURN(-EOPNOTSUPP);
1957 * Handle quota control requests to consult current usage/limit, but also
1958 * to configure quota enforcement
1960 int mdt_quotactl(struct mdt_thread_info *info)
1962 struct obd_export *exp = info->mti_exp;
1963 struct req_capsule *pill = info->mti_pill;
1964 struct obd_quotactl *oqctl, *repoqc;
1966 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1969 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1971 RETURN(err_serious(-EPROTO));
1973 rc = req_capsule_server_pack(pill);
1975 RETURN(err_serious(rc));
1977 switch (oqctl->qc_cmd) {
1979 case LUSTRE_Q_INVALIDATE:
1980 case LUSTRE_Q_FINVALIDATE:
1984 /* deprecated, not used any more */
1985 RETURN(-EOPNOTSUPP);
1986 /* master quotactl */
1992 RETURN(-EOPNOTSUPP);
1993 /* slave quotactl */
1998 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2002 /* map uid/gid for remote client */
2004 if (exp_connect_rmtclient(exp)) {
2005 struct lustre_idmap_table *idmap;
2007 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2009 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2010 oqctl->qc_cmd != Q_GETINFO))
2013 if (oqctl->qc_type == USRQUOTA)
2014 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2016 else if (oqctl->qc_type == GRPQUOTA)
2017 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2022 if (id == CFS_IDMAP_NOTFOUND) {
2023 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2028 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2030 RETURN(err_serious(-EFAULT));
2032 if (oqctl->qc_id != id)
2033 swap(oqctl->qc_id, id);
2035 switch (oqctl->qc_cmd) {
2041 /* forward quotactl request to QMT */
2042 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2047 /* slave quotactl */
2048 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2053 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2057 if (oqctl->qc_id != id)
2058 swap(oqctl->qc_id, id);
2065 * OBD PING and other handlers.
2067 int mdt_obd_ping(struct mdt_thread_info *info)
2072 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2074 rc = target_handle_ping(mdt_info_req(info));
2076 rc = err_serious(rc);
2081 * OBD_IDX_READ handler
2083 int mdt_obd_idx_read(struct mdt_thread_info *info)
2085 struct mdt_device *mdt = info->mti_mdt;
2086 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2087 struct idx_info *req_ii, *rep_ii;
2091 memset(rdpg, 0, sizeof(*rdpg));
2092 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2094 /* extract idx_info buffer from request & reply */
2095 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2096 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2097 RETURN(err_serious(-EPROTO));
2099 rc = req_capsule_server_pack(info->mti_pill);
2101 RETURN(err_serious(rc));
2103 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2105 RETURN(err_serious(-EFAULT));
2106 rep_ii->ii_magic = IDX_INFO_MAGIC;
2108 /* extract hash to start with */
2109 rdpg->rp_hash = req_ii->ii_hash_start;
2111 /* extract requested attributes */
2112 rdpg->rp_attrs = req_ii->ii_attrs;
2114 /* check that fid packed in request is valid and supported */
2115 if (!fid_is_sane(&req_ii->ii_fid))
2117 rep_ii->ii_fid = req_ii->ii_fid;
2120 rep_ii->ii_flags = req_ii->ii_flags;
2122 /* compute number of pages to allocate, ii_count is the number of 4KB
2124 if (req_ii->ii_count <= 0)
2125 GOTO(out, rc = -EFAULT);
2126 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2127 exp_max_brw_size(info->mti_exp));
2128 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
2131 /* allocate pages to store the containers */
2132 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2133 if (rdpg->rp_pages == NULL)
2134 GOTO(out, rc = -ENOMEM);
2135 for (i = 0; i < rdpg->rp_npages; i++) {
2136 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
2137 if (rdpg->rp_pages[i] == NULL)
2138 GOTO(out, rc = -ENOMEM);
2141 /* populate pages with key/record pairs */
2142 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2146 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2147 "asked %d > %d\n", rc, rdpg->rp_count);
2149 /* send pages to client */
2150 rc = mdt_sendpage(info, rdpg, rc);
2154 if (rdpg->rp_pages) {
2155 for (i = 0; i < rdpg->rp_npages; i++)
2156 if (rdpg->rp_pages[i])
2157 __free_page(rdpg->rp_pages[i]);
2158 OBD_FREE(rdpg->rp_pages,
2159 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2164 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2166 return err_serious(-EOPNOTSUPP);
2169 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2171 return err_serious(-EOPNOTSUPP);
2178 /** clone llog ctxt from child (mdd)
2179 * This allows remote llog (replicator) access.
2180 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2181 * context was originally set up, or we can handle them directly.
2182 * I choose the latter, but that means I need any llog
2183 * contexts set up by child to be accessable by the mdt. So we clone the
2184 * context into our context list here.
2186 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2189 struct md_device *next = mdt->mdt_child;
2190 struct llog_ctxt *ctxt;
2193 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2196 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2197 if (rc || ctxt == NULL) {
2201 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2203 CERROR("Can't set mdt ctxt %d\n", rc);
2208 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2209 struct mdt_device *mdt, int idx)
2211 struct llog_ctxt *ctxt;
2213 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2216 /* Put once for the get we just did, and once for the clone */
2217 llog_ctxt_put(ctxt);
2218 llog_ctxt_put(ctxt);
2222 int mdt_llog_create(struct mdt_thread_info *info)
2226 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2227 rc = llog_origin_handle_open(mdt_info_req(info));
2228 return (rc < 0 ? err_serious(rc) : rc);
2231 int mdt_llog_destroy(struct mdt_thread_info *info)
2235 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2236 rc = llog_origin_handle_destroy(mdt_info_req(info));
2237 return (rc < 0 ? err_serious(rc) : rc);
2240 int mdt_llog_read_header(struct mdt_thread_info *info)
2244 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2245 rc = llog_origin_handle_read_header(mdt_info_req(info));
2246 return (rc < 0 ? err_serious(rc) : rc);
2249 int mdt_llog_next_block(struct mdt_thread_info *info)
2253 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2254 rc = llog_origin_handle_next_block(mdt_info_req(info));
2255 return (rc < 0 ? err_serious(rc) : rc);
2258 int mdt_llog_prev_block(struct mdt_thread_info *info)
2262 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2263 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2264 return (rc < 0 ? err_serious(rc) : rc);
2272 static struct ldlm_callback_suite cbs = {
2273 .lcs_completion = ldlm_server_completion_ast,
2274 .lcs_blocking = ldlm_server_blocking_ast,
2275 .lcs_glimpse = ldlm_server_glimpse_ast
2278 int mdt_enqueue(struct mdt_thread_info *info)
2280 struct ptlrpc_request *req;
2284 * info->mti_dlm_req already contains swapped and (if necessary)
2285 * converted dlm request.
2287 LASSERT(info->mti_dlm_req != NULL);
2289 req = mdt_info_req(info);
2290 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2291 req, info->mti_dlm_req, &cbs);
2292 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2293 return rc ? err_serious(rc) : req->rq_status;
2296 int mdt_convert(struct mdt_thread_info *info)
2299 struct ptlrpc_request *req;
2301 LASSERT(info->mti_dlm_req);
2302 req = mdt_info_req(info);
2303 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2304 return rc ? err_serious(rc) : req->rq_status;
2307 int mdt_bl_callback(struct mdt_thread_info *info)
2309 CERROR("bl callbacks should not happen on MDS\n");
2311 return err_serious(-EOPNOTSUPP);
2314 int mdt_cp_callback(struct mdt_thread_info *info)
2316 CERROR("cp callbacks should not happen on MDS\n");
2318 return err_serious(-EOPNOTSUPP);
2322 * sec context handlers
2324 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2328 rc = mdt_handle_idmap(info);
2331 struct ptlrpc_request *req = mdt_info_req(info);
2334 opc = lustre_msg_get_opc(req->rq_reqmsg);
2335 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2336 sptlrpc_svc_ctx_invalidate(req);
2339 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2345 * quota request handlers
2347 int mdt_quota_dqacq(struct mdt_thread_info *info)
2349 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2354 RETURN(err_serious(-EOPNOTSUPP));
2356 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2360 static struct mdt_object *mdt_obj(struct lu_object *o)
2362 LASSERT(lu_device_is_mdt(o->lo_dev));
2363 return container_of0(o, struct mdt_object, mot_obj);
2366 struct mdt_object *mdt_object_new(const struct lu_env *env,
2367 struct mdt_device *d,
2368 const struct lu_fid *f)
2370 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2371 struct lu_object *o;
2372 struct mdt_object *m;
2375 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2376 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2377 if (unlikely(IS_ERR(o)))
2378 m = (struct mdt_object *)o;
2384 struct mdt_object *mdt_object_find(const struct lu_env *env,
2385 struct mdt_device *d,
2386 const struct lu_fid *f)
2388 struct lu_object *o;
2389 struct mdt_object *m;
2392 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2393 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2394 if (unlikely(IS_ERR(o)))
2395 m = (struct mdt_object *)o;
2403 * Asyncronous commit for mdt device.
2405 * Pass asynchonous commit call down the MDS stack.
2407 * \param env environment
2408 * \param mdt the mdt device
2410 static void mdt_device_commit_async(const struct lu_env *env,
2411 struct mdt_device *mdt)
2413 struct dt_device *dt = mdt->mdt_bottom;
2416 rc = dt->dd_ops->dt_commit_async(env, dt);
2417 if (unlikely(rc != 0))
2418 CWARN("async commit start failed with rc = %d", rc);
2422 * Mark the lock as "synchonous".
2424 * Mark the lock to deffer transaction commit to the unlock time.
2426 * \param lock the lock to mark as "synchonous"
2428 * \see mdt_is_lock_sync
2429 * \see mdt_save_lock
2431 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2433 lock->l_ast_data = (void*)1;
2437 * Check whehter the lock "synchonous" or not.
2439 * \param lock the lock to check
2440 * \retval 1 the lock is "synchonous"
2441 * \retval 0 the lock isn't "synchronous"
2443 * \see mdt_set_lock_sync
2444 * \see mdt_save_lock
2446 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2448 return lock->l_ast_data != NULL;
2452 * Blocking AST for mdt locks.
2454 * Starts transaction commit if in case of COS lock conflict or
2455 * deffers such a commit to the mdt_save_lock.
2457 * \param lock the lock which blocks a request or cancelling lock
2458 * \param desc unused
2459 * \param data unused
2460 * \param flag indicates whether this cancelling or blocking callback
2462 * \see ldlm_blocking_ast_nocheck
2464 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2465 void *data, int flag)
2467 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2468 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2472 if (flag == LDLM_CB_CANCELING)
2474 lock_res_and_lock(lock);
2475 if (lock->l_blocking_ast != mdt_blocking_ast) {
2476 unlock_res_and_lock(lock);
2479 if (mdt_cos_is_enabled(mdt) &&
2480 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2481 lock->l_blocking_lock != NULL &&
2482 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2483 mdt_set_lock_sync(lock);
2485 rc = ldlm_blocking_ast_nocheck(lock);
2487 /* There is no lock conflict if l_blocking_lock == NULL,
2488 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2489 * when the last reference to a local lock was released */
2490 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2493 rc = lu_env_init(&env, LCT_LOCAL);
2494 if (unlikely(rc != 0))
2495 CWARN("lu_env initialization failed with rc = %d,"
2496 "cannot start asynchronous commit\n", rc);
2498 mdt_device_commit_async(&env, mdt);
2504 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2505 void *data, int flag)
2507 struct lustre_handle lockh;
2511 case LDLM_CB_BLOCKING:
2512 ldlm_lock2handle(lock, &lockh);
2513 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2515 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2519 case LDLM_CB_CANCELING:
2520 LDLM_DEBUG(lock, "Revoke remote lock\n");
2528 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2529 struct mdt_object *o, struct lustre_handle *lh,
2530 ldlm_mode_t mode, __u64 ibits)
2532 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2533 ldlm_policy_data_t *policy = &mti->mti_policy;
2537 LASSERT(mdt_object_remote(o));
2539 LASSERT((ibits & MDS_INODELOCK_UPDATE));
2541 memset(einfo, 0, sizeof(*einfo));
2542 einfo->ei_type = LDLM_IBITS;
2543 einfo->ei_mode = mode;
2544 einfo->ei_cb_bl = mdt_md_blocking_ast;
2545 einfo->ei_cb_cp = ldlm_completion_ast;
2547 memset(policy, 0, sizeof(*policy));
2548 policy->l_inodebits.bits = ibits;
2550 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2555 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2556 struct mdt_lock_handle *lh, __u64 ibits,
2557 bool nonblock, int locality)
2559 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2560 ldlm_policy_data_t *policy = &info->mti_policy;
2561 struct ldlm_res_id *res_id = &info->mti_res_id;
2566 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2567 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2568 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2569 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2571 if (mdt_object_remote(o)) {
2572 if (locality == MDT_CROSS_LOCK) {
2573 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2574 ibits |= MDS_INODELOCK_LOOKUP;
2577 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2578 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2579 mdt_obd_name(info->mti_mdt), ibits,
2580 PFID(mdt_object_fid(o)));
2581 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2583 /* No PDO lock on remote object */
2584 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2587 if (lh->mlh_type == MDT_PDO_LOCK) {
2588 /* check for exists after object is locked */
2589 if (mdt_object_exists(o) == 0) {
2590 /* Non-existent object shouldn't have PDO lock */
2593 /* Non-dir object shouldn't have PDO lock */
2594 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2599 memset(policy, 0, sizeof(*policy));
2600 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2602 dlmflags = LDLM_FL_ATOMIC_CB;
2604 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2607 * Take PDO lock on whole directory and build correct @res_id for lock
2608 * on part of directory.
2610 if (lh->mlh_pdo_hash != 0) {
2611 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2612 mdt_lock_pdo_mode(info, o, lh);
2613 if (lh->mlh_pdo_mode != LCK_NL) {
2615 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2616 * is never going to be sent to client and we do not
2617 * want it slowed down due to possible cancels.
2619 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2620 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2621 policy, res_id, dlmflags,
2622 &info->mti_exp->exp_handle.h_cookie);
2628 * Finish res_id initializing by name hash marking part of
2629 * directory which is taking modification.
2631 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2634 policy->l_inodebits.bits = ibits;
2637 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2638 * going to be sent to client. If it is - mdt_intent_policy() path will
2639 * fix it up and turn FL_LOCAL flag off.
2641 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2642 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2643 &info->mti_exp->exp_handle.h_cookie);
2645 mdt_object_unlock(info, o, lh, 1);
2646 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2647 lh->mlh_pdo_hash != 0 &&
2648 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2649 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2655 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2656 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2658 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2661 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2662 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2664 struct mdt_lock_handle tmp = *lh;
2667 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2675 * Save a lock within request object.
2677 * Keep the lock referenced until whether client ACK or transaction
2678 * commit happens or release the lock immediately depending on input
2679 * parameters. If COS is ON, a write lock is converted to COS lock
2682 * \param info thead info object
2683 * \param h lock handle
2684 * \param mode lock mode
2685 * \param decref force immediate lock releasing
2688 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2689 ldlm_mode_t mode, int decref)
2693 if (lustre_handle_is_used(h)) {
2694 if (decref || !info->mti_has_trans ||
2695 !(mode & (LCK_PW | LCK_EX))){
2696 mdt_fid_unlock(h, mode);
2698 struct mdt_device *mdt = info->mti_mdt;
2699 struct ldlm_lock *lock = ldlm_handle2lock(h);
2700 struct ptlrpc_request *req = mdt_info_req(info);
2703 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2705 CDEBUG(D_HA, "request = %p reply state = %p"
2706 " transno = "LPD64"\n",
2707 req, req->rq_reply_state, req->rq_transno);
2708 if (mdt_cos_is_enabled(mdt)) {
2710 ldlm_lock_downgrade(lock, LCK_COS);
2713 ptlrpc_save_lock(req, h, mode, no_ack);
2714 if (mdt_is_lock_sync(lock)) {
2715 CDEBUG(D_HA, "found sync-lock,"
2716 " async commit started\n");
2717 mdt_device_commit_async(info->mti_env,
2720 LDLM_LOCK_PUT(lock);
2729 * Unlock mdt object.
2731 * Immeditely release the regular lock and the PDO lock or save the
2732 * lock in reqeuest and keep them referenced until client ACK or
2733 * transaction commit.
2735 * \param info thread info object
2736 * \param o mdt object
2737 * \param lh mdt lock handle referencing regular and PDO locks
2738 * \param decref force immediate lock releasing
2740 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2741 struct mdt_lock_handle *lh, int decref)
2745 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2746 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2748 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2749 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2754 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2755 const struct lu_fid *f,
2756 struct mdt_lock_handle *lh,
2759 struct mdt_object *o;
2761 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2765 rc = mdt_object_lock(info, o, lh, ibits,
2768 mdt_object_put(info->mti_env, o);
2775 void mdt_object_unlock_put(struct mdt_thread_info * info,
2776 struct mdt_object * o,
2777 struct mdt_lock_handle *lh,
2780 mdt_object_unlock(info, o, lh, decref);
2781 mdt_object_put(info->mti_env, o);
2784 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2786 struct mdt_opc_slice *s;
2787 struct mdt_handler *h;
2790 for (s = supported; s->mos_hs != NULL; s++) {
2791 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2792 h = s->mos_hs + (opc - s->mos_opc_start);
2793 if (likely(h->mh_opc != 0))
2794 LASSERTF(h->mh_opc == opc,
2795 "opcode mismatch %d != %d\n",
2798 h = NULL; /* unsupported opc */
2805 static int mdt_lock_resname_compat(struct mdt_device *m,
2806 struct ldlm_request *req)
2808 /* XXX something... later. */
2812 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2814 /* XXX something... later. */
2819 * Generic code handling requests that have struct mdt_body passed in:
2821 * - extract mdt_body from request and save it in @info, if present;
2823 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2826 * - if HABEO_CORPUS flag is set for this request type check whether object
2827 * actually exists on storage (lu_object_exists()).
2830 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2832 const struct mdt_body *body;
2833 struct mdt_object *obj;
2834 const struct lu_env *env;
2835 struct req_capsule *pill;
2839 env = info->mti_env;
2840 pill = info->mti_pill;
2842 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2846 if (!(body->valid & OBD_MD_FLID))
2849 if (!fid_is_sane(&body->fid1)) {
2850 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2855 * Do not get size or any capa fields before we check that request
2856 * contains capa actually. There are some requests which do not, for
2857 * instance MDS_IS_SUBDIR.
2859 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2860 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2861 mdt_set_capainfo(info, 0, &body->fid1,
2862 req_capsule_client_get(pill, &RMF_CAPA1));
2864 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2866 if ((flags & HABEO_CORPUS) &&
2867 !mdt_object_exists(obj)) {
2868 mdt_object_put(env, obj);
2869 /* for capability renew ENOENT will be handled in
2871 if (body->valid & OBD_MD_FLOSSCAPA)
2876 info->mti_object = obj;
2885 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2887 struct req_capsule *pill = info->mti_pill;
2891 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2892 rc = mdt_body_unpack(info, flags);
2896 if (rc == 0 && (flags & HABEO_REFERO)) {
2898 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2899 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2900 info->mti_body->eadatasize);
2901 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2902 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2905 rc = req_capsule_server_pack(pill);
2910 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2912 struct md_device *next = m->mdt_child;
2914 return next->md_ops->mdo_init_capa_ctxt(env, next,
2915 m->mdt_opts.mo_mds_capa,
2916 m->mdt_capa_timeout,
2922 * Invoke handler for this request opc. Also do necessary preprocessing
2923 * (according to handler ->mh_flags), and post-processing (setting of
2924 * ->last_{xid,committed}).
2926 static int mdt_req_handle(struct mdt_thread_info *info,
2927 struct mdt_handler *h, struct ptlrpc_request *req)
2929 int rc, serious = 0;
2934 LASSERT(h->mh_act != NULL);
2935 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2936 LASSERT(current->journal_info == NULL);
2939 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2940 * to put same checks into handlers like mdt_close(), mdt_reint(),
2941 * etc., without talking to mdt authors first. Checking same thing
2942 * there again is useless and returning 0 error without packing reply
2943 * is buggy! Handlers either pack reply or return error.
2945 * We return 0 here and do not send any reply in order to emulate
2946 * network failure. Do not send any reply in case any of NET related
2947 * fail_id has occured.
2949 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2953 flags = h->mh_flags;
2954 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2956 if (h->mh_fmt != NULL) {
2957 req_capsule_set(info->mti_pill, h->mh_fmt);
2958 rc = mdt_unpack_req_pack_rep(info, flags);
2961 if (rc == 0 && flags & MUTABOR &&
2962 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2963 /* should it be rq_status? */
2966 if (rc == 0 && flags & HABEO_CLAVIS) {
2967 struct ldlm_request *dlm_req;
2969 LASSERT(h->mh_fmt != NULL);
2971 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2972 if (dlm_req != NULL) {
2973 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2975 dlm_req->lock_desc.l_policy_data.\
2976 l_inodebits.bits == 0)) {
2978 * Lock without inodebits makes no sense and
2979 * will oops later in ldlm. If client miss to
2980 * set such bits, do not trigger ASSERTION.
2982 * For liblustre flock case, it maybe zero.
2986 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2987 rc = mdt_lock_resname_compat(
2990 info->mti_dlm_req = dlm_req;
2997 /* capability setting changed via /proc, needs reinitialize ctxt */
2998 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2999 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
3000 info->mti_mdt->mdt_capa_conf = 0;
3003 if (likely(rc == 0)) {
3005 * Process request, there can be two types of rc:
3006 * 1) errors with msg unpack/pack, other failures outside the
3007 * operation itself. This is counted as serious errors;
3008 * 2) errors during fs operation, should be placed in rq_status
3011 rc = h->mh_act(info);
3013 !req->rq_no_reply && req->rq_reply_state == NULL) {
3014 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3015 "pack reply and returned 0 error\n",
3019 serious = is_serious(rc);
3020 rc = clear_serious(rc);
3024 req->rq_status = rc;
3027 * ELDLM_* codes which > 0 should be in rq_status only as well as
3028 * all non-serious errors.
3030 if (rc > 0 || !serious)
3033 LASSERT(current->journal_info == NULL);
3035 if (rc == 0 && (flags & HABEO_CLAVIS) &&
3036 info->mti_mdt->mdt_opts.mo_compat_resname) {
3037 struct ldlm_reply *dlmrep;
3039 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3041 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3044 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3045 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3046 target_committed_to_req(req);
3048 if (unlikely(req_is_replay(req) &&
3049 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3050 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3054 target_send_reply(req, rc, info->mti_fail_id);
3058 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3060 lh->mlh_type = MDT_NUL_LOCK;
3061 lh->mlh_reg_lh.cookie = 0ull;
3062 lh->mlh_reg_mode = LCK_MINMODE;
3063 lh->mlh_pdo_lh.cookie = 0ull;
3064 lh->mlh_pdo_mode = LCK_MINMODE;
3065 lh->mlh_rreg_lh.cookie = 0ull;
3066 lh->mlh_rreg_mode = LCK_MINMODE;
3069 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3071 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3072 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3076 * Initialize fields of struct mdt_thread_info. Other fields are left in
3077 * uninitialized state, because it's too expensive to zero out whole
3078 * mdt_thread_info (> 1K) on each request arrival.
3080 static void mdt_thread_info_init(struct ptlrpc_request *req,
3081 struct mdt_thread_info *info)
3085 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3086 info->mti_pill = &req->rq_pill;
3089 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3090 mdt_lock_handle_init(&info->mti_lh[i]);
3092 /* mdt device: it can be NULL while CONNECT */
3093 if (req->rq_export) {
3094 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3095 info->mti_exp = req->rq_export;
3097 info->mti_mdt = NULL;
3098 info->mti_env = req->rq_svc_thread->t_env;
3099 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3100 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3101 info->mti_mos = NULL;
3103 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3104 info->mti_big_buf = LU_BUF_NULL;
3105 info->mti_body = NULL;
3106 info->mti_object = NULL;
3107 info->mti_dlm_req = NULL;
3108 info->mti_has_trans = 0;
3109 info->mti_cross_ref = 0;
3110 info->mti_opdata = 0;
3111 info->mti_big_lmm_used = 0;
3113 /* To not check for split by default. */
3114 info->mti_spec.no_create = 0;
3115 info->mti_spec.sp_rm_entry = 0;
3118 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3122 req_capsule_fini(info->mti_pill);
3123 if (info->mti_object != NULL) {
3124 mdt_object_put(info->mti_env, info->mti_object);
3125 info->mti_object = NULL;
3128 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3129 mdt_lock_handle_fini(&info->mti_lh[i]);
3130 info->mti_env = NULL;
3132 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3133 lu_buf_free(&info->mti_big_buf);
3136 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3137 struct obd_device *obd, int *process)
3139 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3140 case MDS_CONNECT: /* This will never get here, but for completeness. */
3141 case OST_CONNECT: /* This will never get here, but for completeness. */
3142 case MDS_DISCONNECT:
3143 case OST_DISCONNECT:
3149 case MDS_DONE_WRITING:
3150 case MDS_SYNC: /* used in unmounting */
3157 *process = target_queue_recovery_request(req, obd);
3161 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3168 * Handle recovery. Return:
3169 * +1: continue request processing;
3170 * -ve: abort immediately with the given error code;
3171 * 0: send reply with error code in req->rq_status;
3173 static int mdt_recovery(struct mdt_thread_info *info)
3175 struct ptlrpc_request *req = mdt_info_req(info);
3176 struct obd_device *obd;
3180 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3183 case SEC_CTX_INIT_CONT:
3189 rc = mdt_handle_idmap(info);
3198 if (unlikely(!class_connected_export(req->rq_export))) {
3199 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3200 lustre_msg_get_opc(req->rq_reqmsg),
3201 libcfs_id2str(req->rq_peer));
3202 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3203 * mds_A will get -ENOTCONN(especially for ping req),
3204 * which will cause that mds_A deactive timeout, then when
3205 * mds_A cleanup, the cleanup process will be suspended since
3206 * deactive timeout is not zero.
3208 req->rq_status = -ENOTCONN;
3209 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3213 /* sanity check: if the xid matches, the request must be marked as a
3214 * resent or replayed */
3215 if (req_xid_is_last(req)) {
3216 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3217 (MSG_RESENT | MSG_REPLAY))) {
3218 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3219 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3220 lustre_msg_get_flags(req->rq_reqmsg));
3222 req->rq_status = -ENOTCONN;
3227 /* else: note the opposite is not always true; a RESENT req after a
3228 * failover will usually not match the last_xid, since it was likely
3229 * never committed. A REPLAYed request will almost never match the
3230 * last xid, however it could for a committed, but still retained,
3233 obd = req->rq_export->exp_obd;
3235 /* Check for aborted recovery... */
3236 if (unlikely(obd->obd_recovering)) {
3239 DEBUG_REQ(D_INFO, req, "Got new replay");
3240 rc = mdt_filter_recovery_request(req, obd, &should_process);
3241 if (rc != 0 || !should_process)
3243 else if (should_process < 0) {
3244 req->rq_status = should_process;
3245 rc = ptlrpc_error(req);
3252 static int mdt_msg_check_version(struct lustre_msg *msg)
3256 switch (lustre_msg_get_opc(msg)) {
3258 case MDS_DISCONNECT:
3261 case SEC_CTX_INIT_CONT:
3264 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3266 CERROR("bad opc %u version %08x, expecting %08x\n",
3267 lustre_msg_get_opc(msg),
3268 lustre_msg_get_version(msg),
3269 LUSTRE_OBD_VERSION);
3273 case MDS_GETATTR_NAME:
3280 case MDS_DONE_WRITING:
3287 case MDS_HSM_PROGRESS:
3288 case MDS_HSM_REQUEST:
3289 case MDS_HSM_CT_REGISTER:
3290 case MDS_HSM_CT_UNREGISTER:
3291 case MDS_HSM_STATE_GET:
3292 case MDS_HSM_STATE_SET:
3293 case MDS_HSM_ACTION:
3294 case MDS_QUOTACHECK:
3297 case MDS_SWAP_LAYOUTS:
3302 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3304 CERROR("bad opc %u version %08x, expecting %08x\n",
3305 lustre_msg_get_opc(msg),
3306 lustre_msg_get_version(msg),
3307 LUSTRE_MDS_VERSION);
3311 case LDLM_BL_CALLBACK:
3312 case LDLM_CP_CALLBACK:
3313 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3315 CERROR("bad opc %u version %08x, expecting %08x\n",
3316 lustre_msg_get_opc(msg),
3317 lustre_msg_get_version(msg),
3318 LUSTRE_DLM_VERSION);
3320 case OBD_LOG_CANCEL:
3321 case LLOG_ORIGIN_HANDLE_CREATE:
3322 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3323 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3324 case LLOG_ORIGIN_HANDLE_CLOSE:
3325 case LLOG_ORIGIN_HANDLE_DESTROY:
3326 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3328 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3330 CERROR("bad opc %u version %08x, expecting %08x\n",
3331 lustre_msg_get_opc(msg),
3332 lustre_msg_get_version(msg),
3333 LUSTRE_LOG_VERSION);
3336 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3342 static int mdt_handle0(struct ptlrpc_request *req,
3343 struct mdt_thread_info *info,
3344 struct mdt_opc_slice *supported)
3346 struct mdt_handler *h;
3347 struct lustre_msg *msg;
3352 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3355 LASSERT(current->journal_info == NULL);
3357 msg = req->rq_reqmsg;
3358 rc = mdt_msg_check_version(msg);
3359 if (likely(rc == 0)) {
3360 rc = mdt_recovery(info);
3361 if (likely(rc == +1)) {
3362 h = mdt_handler_find(lustre_msg_get_opc(msg),
3364 if (likely(h != NULL)) {
3365 rc = mdt_req_handle(info, h, req);
3367 CERROR("%s: opc unsupported: 0x%x\n",
3368 mdt_obd_name(info->mti_mdt),
3369 lustre_msg_get_opc(msg));
3370 req->rq_status = -ENOTSUPP;
3371 rc = ptlrpc_error(req);
3376 CDEBUG(D_INFO, "%s: drops mal-formed request: rc = %d\n",
3377 mdt_obd_name(info->mti_mdt), rc);
3378 req->rq_status = rc;
3379 rc = ptlrpc_error(req);
3385 * MDT handler function called by ptlrpc service thread when request comes.
3387 * XXX common "target" functionality should be factored into separate module
3388 * shared by mdt, ost and stand-alone services like fld.
3390 int mdt_handle_common(struct ptlrpc_request *req,
3391 struct mdt_opc_slice *supported)
3394 struct mdt_thread_info *info;
3398 env = req->rq_svc_thread->t_env;
3399 /* Refill(initilize) the context(mdt_thread_info), in case it is
3400 * not initialized yet. Usually it happens during start up, after
3401 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3402 * before MDT_thread_info is initialized */
3404 LASSERT(env != NULL);
3405 LASSERT(env->le_ses != NULL);
3406 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3407 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3408 LASSERT(info != NULL);
3410 mdt_thread_info_init(req, info);
3412 rc = mdt_handle0(req, info, supported);
3414 mdt_thread_info_fini(info);
3419 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3422 int mdt_recovery_handle(struct ptlrpc_request *req)
3427 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3429 rc = mdt_handle_common(req, mdt_fld_handlers);
3432 rc = mdt_handle_common(req, mdt_seq_handlers);
3435 rc = mdt_handle_common(req, mdt_regular_handlers);
3457 static int mdt_intent_getattr(enum mdt_it_code opcode,
3458 struct mdt_thread_info *info,
3459 struct ldlm_lock **,
3461 static int mdt_intent_layout(enum mdt_it_code opcode,
3462 struct mdt_thread_info *info,
3463 struct ldlm_lock **,
3465 static int mdt_intent_reint(enum mdt_it_code opcode,
3466 struct mdt_thread_info *info,
3467 struct ldlm_lock **,
3470 static struct mdt_it_flavor {
3471 const struct req_format *it_fmt;
3473 int (*it_act)(enum mdt_it_code ,
3474 struct mdt_thread_info *,
3475 struct ldlm_lock **,
3478 } mdt_it_flavor[] = {
3480 .it_fmt = &RQF_LDLM_INTENT,
3481 /*.it_flags = HABEO_REFERO,*/
3483 .it_act = mdt_intent_reint,
3484 .it_reint = REINT_OPEN
3487 .it_fmt = &RQF_LDLM_INTENT,
3488 .it_flags = MUTABOR,
3489 .it_act = mdt_intent_reint,
3490 .it_reint = REINT_OPEN
3493 .it_fmt = &RQF_LDLM_INTENT,
3494 .it_flags = MUTABOR,
3495 .it_act = mdt_intent_reint,
3496 .it_reint = REINT_CREATE
3498 [MDT_IT_GETATTR] = {
3499 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3500 .it_flags = HABEO_REFERO,
3501 .it_act = mdt_intent_getattr
3503 [MDT_IT_READDIR] = {
3509 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3510 .it_flags = HABEO_REFERO,
3511 .it_act = mdt_intent_getattr
3514 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3515 .it_flags = MUTABOR,
3517 .it_reint = REINT_UNLINK
3521 .it_flags = MUTABOR,
3524 [MDT_IT_GETXATTR] = {
3530 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3532 .it_act = mdt_intent_layout
3536 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3537 struct ldlm_lock **lockp,
3538 struct ldlm_lock *new_lock,
3539 struct mdt_lock_handle *lh,
3542 struct ptlrpc_request *req = mdt_info_req(info);
3543 struct ldlm_lock *lock = *lockp;
3546 * Get new lock only for cases when possible resent did not find any
3549 if (new_lock == NULL)
3550 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3552 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3553 lh->mlh_reg_lh.cookie = 0;
3557 LASSERTF(new_lock != NULL,
3558 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3561 * If we've already given this lock to a client once, then we should
3562 * have no readers or writers. Otherwise, we should have one reader
3563 * _or_ writer ref (which will be zeroed below) before returning the
3566 if (new_lock->l_export == req->rq_export) {
3567 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3569 LASSERT(new_lock->l_export == NULL);
3570 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3575 if (new_lock->l_export == req->rq_export) {
3577 * Already gave this to the client, which means that we
3578 * reconstructed a reply.
3580 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3582 lh->mlh_reg_lh.cookie = 0;
3583 RETURN(ELDLM_LOCK_REPLACED);
3587 * Fixup the lock to be given to the client.
3589 lock_res_and_lock(new_lock);
3590 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3591 * possible blocking AST. */
3592 while (new_lock->l_readers > 0) {
3593 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3594 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3595 new_lock->l_readers--;
3597 while (new_lock->l_writers > 0) {
3598 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3599 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3600 new_lock->l_writers--;
3603 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3604 new_lock->l_blocking_ast = lock->l_blocking_ast;
3605 new_lock->l_completion_ast = lock->l_completion_ast;
3606 new_lock->l_remote_handle = lock->l_remote_handle;
3607 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3609 unlock_res_and_lock(new_lock);
3611 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3612 &new_lock->l_remote_handle,
3613 &new_lock->l_exp_hash);
3615 LDLM_LOCK_RELEASE(new_lock);
3616 lh->mlh_reg_lh.cookie = 0;
3618 RETURN(ELDLM_LOCK_REPLACED);
3621 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3622 struct ldlm_lock *new_lock,
3623 struct ldlm_lock **old_lock,
3624 struct mdt_lock_handle *lh)
3626 struct ptlrpc_request *req = mdt_info_req(info);
3627 struct obd_export *exp = req->rq_export;
3628 struct lustre_handle remote_hdl;
3629 struct ldlm_request *dlmreq;
3630 struct ldlm_lock *lock;
3632 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3635 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3636 remote_hdl = dlmreq->lock_handle[0];
3638 /* In the function below, .hs_keycmp resolves to
3639 * ldlm_export_lock_keycmp() */
3640 /* coverity[overrun-buffer-val] */
3641 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3643 if (lock != new_lock) {
3644 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3645 lh->mlh_reg_mode = lock->l_granted_mode;
3647 LDLM_DEBUG(lock, "Restoring lock cookie");
3648 DEBUG_REQ(D_DLMTRACE, req,
3649 "restoring lock cookie "LPX64,
3650 lh->mlh_reg_lh.cookie);
3652 *old_lock = LDLM_LOCK_GET(lock);
3653 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3657 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3661 * If the xid matches, then we know this is a resent request, and allow
3662 * it. (It's probably an OPEN, for which we don't send a lock.
3664 if (req_xid_is_last(req))
3668 * This remote handle isn't enqueued, so we never received or processed
3669 * this request. Clear MSG_RESENT, because it can be handled like any
3670 * normal request now.
3672 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3674 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3678 static int mdt_intent_getattr(enum mdt_it_code opcode,
3679 struct mdt_thread_info *info,
3680 struct ldlm_lock **lockp,
3683 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3684 struct ldlm_lock *new_lock = NULL;
3686 struct ldlm_reply *ldlm_rep;
3687 struct ptlrpc_request *req;
3688 struct mdt_body *reqbody;
3689 struct mdt_body *repbody;
3693 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3696 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3699 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3700 repbody->eadatasize = 0;
3701 repbody->aclsize = 0;
3705 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3707 case MDT_IT_GETATTR:
3708 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3712 CERROR("Unsupported intent (%d)\n", opcode);
3713 GOTO(out_shrink, rc = -EINVAL);
3716 rc = mdt_init_ucred(info, reqbody);
3718 GOTO(out_shrink, rc);
3720 req = info->mti_pill->rc_req;
3721 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3722 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3724 /* Get lock from request for possible resent case. */
3725 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3727 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3728 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3730 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3731 ldlm_rep->lock_policy_res2 = 0;
3732 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3733 ldlm_rep->lock_policy_res2) {
3734 lhc->mlh_reg_lh.cookie = 0ull;
3735 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3738 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3741 mdt_exit_ucred(info);
3743 mdt_client_compatibility(info);
3744 rc2 = mdt_fix_reply(info);
3750 static int mdt_intent_layout(enum mdt_it_code opcode,
3751 struct mdt_thread_info *info,
3752 struct ldlm_lock **lockp,
3755 struct layout_intent *layout;
3757 struct mdt_object *obj = NULL;
3758 struct md_object *child = NULL;
3762 if (opcode != MDT_IT_LAYOUT) {
3763 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3768 fid = &info->mti_tmp_fid2;
3769 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3771 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3773 RETURN(PTR_ERR(obj));
3775 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3776 child = mdt_object_child(obj);
3778 /* get the length of lsm */
3779 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3782 if (rc > info->mti_mdt->mdt_max_mdsize)
3783 info->mti_mdt->mdt_max_mdsize = rc;
3786 mdt_object_put(info->mti_env, obj);
3788 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3789 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3790 ldlm_lvbo_size(*lockp));
3791 rc = req_capsule_server_pack(info->mti_pill);
3795 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3796 LASSERT(layout != NULL);
3797 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3798 /* return to normal ldlm handling */
3801 CERROR("%s: Unsupported layout intent (%d)\n",
3802 mdt_obd_name(info->mti_mdt), layout->li_opc);
3806 static int mdt_intent_reint(enum mdt_it_code opcode,
3807 struct mdt_thread_info *info,
3808 struct ldlm_lock **lockp,
3811 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3812 struct ldlm_reply *rep = NULL;
3816 static const struct req_format *intent_fmts[REINT_MAX] = {
3817 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3818 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3823 opc = mdt_reint_opcode(info, intent_fmts);
3827 if (mdt_it_flavor[opcode].it_reint != opc) {
3828 CERROR("Reint code %ld doesn't match intent: %d\n",
3830 RETURN(err_serious(-EPROTO));
3833 /* Get lock from request for possible resent case. */
3834 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3836 rc = mdt_reint_internal(info, lhc, opc);
3838 /* Check whether the reply has been packed successfully. */
3839 if (mdt_info_req(info)->rq_repmsg != NULL)
3840 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3842 RETURN(err_serious(-EFAULT));
3844 /* MDC expects this in any case */
3846 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3848 /* the open lock or the lock for cross-ref object should be
3849 * returned to the client */
3850 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3851 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3852 rep->lock_policy_res2 = 0;
3853 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3857 rep->lock_policy_res2 = clear_serious(rc);
3859 if (rep->lock_policy_res2 == -ENOENT &&
3860 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3861 rep->lock_policy_res2 = 0;
3863 if (rc == -ENOTCONN || rc == -ENODEV ||
3864 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3866 * If it is the disconnect error (ENODEV & ENOCONN), the error
3867 * will be returned by rq_status, and client at ptlrpc layer
3868 * will detect this, then disconnect, reconnect the import
3869 * immediately, instead of impacting the following the rpc.
3871 lhc->mlh_reg_lh.cookie = 0ull;
3875 * For other cases, the error will be returned by intent.
3876 * and client will retrieve the result from intent.
3879 * FIXME: when open lock is finished, that should be
3882 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3883 LASSERTF(rc == 0, "Error occurred but lock handle "
3884 "is still in use, rc = %d\n", rc);
3885 rep->lock_policy_res2 = 0;
3886 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3889 lhc->mlh_reg_lh.cookie = 0ull;
3890 RETURN(ELDLM_LOCK_ABORTED);
3895 static int mdt_intent_code(long itcode)
3903 case IT_OPEN|IT_CREAT:
3910 rc = MDT_IT_READDIR;
3913 rc = MDT_IT_GETATTR;
3925 rc = MDT_IT_GETXATTR;
3930 case IT_QUOTA_DQACQ:
3935 CERROR("Unknown intent opcode: %ld\n", itcode);
3942 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3943 struct ldlm_lock **lockp, __u64 flags)
3945 struct req_capsule *pill;
3946 struct mdt_it_flavor *flv;
3951 opc = mdt_intent_code(itopc);
3955 pill = info->mti_pill;
3957 if (opc == MDT_IT_QUOTA) {
3958 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3961 RETURN(-EOPNOTSUPP);
3963 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3964 /* pass the request to quota master */
3965 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3966 mdt_info_req(info), lockp,
3971 flv = &mdt_it_flavor[opc];
3972 if (flv->it_fmt != NULL)
3973 req_capsule_extend(pill, flv->it_fmt);
3975 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3977 struct ptlrpc_request *req = mdt_info_req(info);
3978 if (flv->it_flags & MUTABOR &&
3979 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3982 if (rc == 0 && flv->it_act != NULL) {
3983 struct ldlm_reply *rep;
3985 /* execute policy */
3986 rc = flv->it_act(opc, info, lockp, flags);
3988 /* Check whether the reply has been packed successfully. */
3989 if (mdt_info_req(info)->rq_repmsg != NULL) {
3990 rep = req_capsule_server_get(info->mti_pill,
3992 rep->lock_policy_res2 =
3993 ptlrpc_status_hton(rep->lock_policy_res2);
4001 static int mdt_intent_policy(struct ldlm_namespace *ns,
4002 struct ldlm_lock **lockp, void *req_cookie,
4003 ldlm_mode_t mode, __u64 flags, void *data)
4005 struct mdt_thread_info *info;
4006 struct ptlrpc_request *req = req_cookie;
4007 struct ldlm_intent *it;
4008 struct req_capsule *pill;
4013 LASSERT(req != NULL);
4015 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
4017 LASSERT(info != NULL);
4018 pill = info->mti_pill;
4019 LASSERT(pill->rc_req == req);
4021 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4022 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4023 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4025 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4029 /* Lock without inodebits makes no sense and will oops
4030 * later in ldlm. Let's check it now to see if we have
4031 * ibits corrupted somewhere in mdt_intent_opc().
4032 * The case for client miss to set ibits has been
4033 * processed by others. */
4034 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
4035 lr_type == LDLM_IBITS,
4036 info->mti_dlm_req->lock_desc.\
4037 l_policy_data.l_inodebits.bits != 0));
4039 rc = err_serious(-EFAULT);
4041 /* No intent was provided */
4042 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4043 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4044 rc = req_capsule_server_pack(pill);
4046 rc = err_serious(rc);
4051 static int mdt_seq_fini(const struct lu_env *env,
4052 struct mdt_device *m)
4054 return seq_site_fini(env, mdt_seq_site(m));
4057 static int mdt_seq_init(const struct lu_env *env,
4059 struct mdt_device *m)
4061 struct seq_server_site *ss;
4066 ss = mdt_seq_site(m);
4069 * This is sequence-controller node. Init seq-controller server on local
4072 if (ss->ss_node_id == 0) {
4073 LASSERT(ss->ss_control_seq == NULL);
4075 OBD_ALLOC_PTR(ss->ss_control_seq);
4076 if (ss->ss_control_seq == NULL)
4079 rc = seq_server_init(ss->ss_control_seq,
4080 m->mdt_bottom, uuid,
4081 LUSTRE_SEQ_CONTROLLER,
4086 GOTO(out_seq_fini, rc);
4088 OBD_ALLOC_PTR(ss->ss_client_seq);
4089 if (ss->ss_client_seq == NULL)
4090 GOTO(out_seq_fini, rc = -ENOMEM);
4092 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4093 if (prefix == NULL) {
4094 OBD_FREE_PTR(ss->ss_client_seq);
4095 GOTO(out_seq_fini, rc = -ENOMEM);
4098 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4102 * Init seq-controller client after seq-controller server is
4103 * ready. Pass ss->ss_control_seq to it for direct talking.
4105 rc = seq_client_init(ss->ss_client_seq, NULL,
4106 LUSTRE_SEQ_METADATA, prefix,
4107 ss->ss_control_seq);
4108 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4111 GOTO(out_seq_fini, rc);
4114 /* Init seq-server on local MDT */
4115 LASSERT(ss->ss_server_seq == NULL);
4117 OBD_ALLOC_PTR(ss->ss_server_seq);
4118 if (ss->ss_server_seq == NULL)
4119 GOTO(out_seq_fini, rc = -ENOMEM);
4121 rc = seq_server_init(ss->ss_server_seq,
4122 m->mdt_bottom, uuid,
4127 GOTO(out_seq_fini, rc = -ENOMEM);
4129 /* Assign seq-controller client to local seq-server. */
4130 if (ss->ss_node_id == 0) {
4131 LASSERT(ss->ss_client_seq != NULL);
4133 rc = seq_server_set_cli(ss->ss_server_seq,
4141 mdt_seq_fini(env, m);
4149 static int mdt_fld_fini(const struct lu_env *env,
4150 struct mdt_device *m)
4152 struct seq_server_site *ss = mdt_seq_site(m);
4155 if (ss && ss->ss_server_fld) {
4156 fld_server_fini(env, ss->ss_server_fld);
4157 OBD_FREE_PTR(ss->ss_server_fld);
4158 ss->ss_server_fld = NULL;
4164 static int mdt_fld_init(const struct lu_env *env,
4166 struct mdt_device *m)
4168 struct seq_server_site *ss;
4172 ss = mdt_seq_site(m);
4174 OBD_ALLOC_PTR(ss->ss_server_fld);
4175 if (ss->ss_server_fld == NULL)
4176 RETURN(rc = -ENOMEM);
4178 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4179 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4181 OBD_FREE_PTR(ss->ss_server_fld);
4182 ss->ss_server_fld = NULL;
4189 static void mdt_stack_pre_fini(const struct lu_env *env,
4190 struct mdt_device *m, struct lu_device *top)
4192 struct obd_device *obd;
4193 struct lustre_cfg_bufs *bufs;
4194 struct lustre_cfg *lcfg;
4195 struct mdt_thread_info *info;
4200 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4201 LASSERT(info != NULL);
4203 bufs = &info->mti_u.bufs;
4205 LASSERT(m->mdt_child_exp);
4206 LASSERT(m->mdt_child_exp->exp_obd);
4207 obd = m->mdt_child_exp->exp_obd;
4209 /* process cleanup, pass mdt obd name to get obd umount flags */
4210 /* XXX: this is needed because all layers are referenced by
4211 * objects (some of them are pinned by osd, for example *
4212 * the proper solution should be a model where object used
4213 * by osd only doesn't have mdt/mdd slices -bzzz */
4214 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4215 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4216 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4218 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4221 top->ld_ops->ldo_process_config(env, top, lcfg);
4222 lustre_cfg_free(lcfg);
4226 static void mdt_stack_fini(const struct lu_env *env,
4227 struct mdt_device *m, struct lu_device *top)
4229 struct obd_device *obd = mdt2obd_dev(m);
4230 struct lustre_cfg_bufs *bufs;
4231 struct lustre_cfg *lcfg;
4232 struct mdt_thread_info *info;
4236 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4237 LASSERT(info != NULL);
4239 lu_dev_del_linkage(top->ld_site, top);
4241 lu_site_purge(env, top->ld_site, -1);
4243 bufs = &info->mti_u.bufs;
4244 /* process cleanup, pass mdt obd name to get obd umount flags */
4245 /* another purpose is to let all layers to release their objects */
4246 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4251 lustre_cfg_bufs_set_string(bufs, 1, flags);
4252 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4254 CERROR("Cannot alloc lcfg!\n");
4258 top->ld_ops->ldo_process_config(env, top, lcfg);
4259 lustre_cfg_free(lcfg);
4261 lu_site_purge(env, top->ld_site, -1);
4263 m->mdt_child = NULL;
4264 m->mdt_bottom = NULL;
4266 obd_disconnect(m->mdt_child_exp);
4267 m->mdt_child_exp = NULL;
4269 obd_disconnect(m->mdt_bottom_exp);
4270 m->mdt_child_exp = NULL;
4273 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4274 const char *next, struct obd_export **exp)
4276 struct obd_connect_data *data = NULL;
4277 struct obd_device *obd;
4281 OBD_ALLOC_PTR(data);
4283 GOTO(out, rc = -ENOMEM);
4285 obd = class_name2obd(next);
4287 CERROR("%s: can't locate next device: %s\n",
4288 mdt_obd_name(m), next);
4289 GOTO(out, rc = -ENOTCONN);
4292 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4293 data->ocd_version = LUSTRE_VERSION_CODE;
4295 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4297 CERROR("%s: cannot connect to next dev %s (%d)\n",
4298 mdt_obd_name(m), next, rc);
4308 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4309 struct lustre_cfg *cfg)
4311 char *dev = lustre_cfg_string(cfg, 0);
4312 int rc, name_size, uuid_size;
4313 char *name, *uuid, *p;
4314 struct lustre_cfg_bufs *bufs;
4315 struct lustre_cfg *lcfg;
4316 struct obd_device *obd;
4317 struct lustre_profile *lprof;
4318 struct lu_site *site;
4321 /* in 1.8 we had the only device in the stack - MDS.
4322 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4323 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4324 * to generate names and setup MDT, MDD. MDT will be using
4325 * generated name to connect to MDD. for MDD the next device
4326 * will be LOD with name taken from so called "profile" which
4327 * is generated by mount_option line
4329 * 1.8 MGS generates config. commands like this:
4330 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4331 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4332 * 2.0 MGS generates config. commands like this:
4333 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4334 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4335 * 3:lustre-MDT0000-mdtlov 4:f
4337 * we generate MDD name from MDT one, just replacing T with D
4339 * after all the preparations, the logical equivalent will be
4340 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4341 * 3:lustre-MDT0000-mdtlov 4:f
4342 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4343 * 3:lustre-MDD0000 4:f
4345 * notice we build the stack from down to top: MDD first, then MDT */
4347 name_size = MAX_OBD_NAME;
4348 uuid_size = MAX_OBD_NAME;
4350 OBD_ALLOC(name, name_size);
4351 OBD_ALLOC(uuid, uuid_size);
4352 if (name == NULL || uuid == NULL)
4353 GOTO(cleanup_mem, rc = -ENOMEM);
4355 OBD_ALLOC_PTR(bufs);
4357 GOTO(cleanup_mem, rc = -ENOMEM);
4360 p = strstr(name, "-MDT");
4362 GOTO(cleanup_mem, rc = -ENOMEM);
4365 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4367 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4368 if (lprof == NULL || lprof->lp_dt == NULL) {
4369 CERROR("can't find the profile: %s\n",
4370 lustre_cfg_string(cfg, 0));
4371 GOTO(cleanup_mem, rc = -EINVAL);
4374 lustre_cfg_bufs_reset(bufs, name);
4375 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4376 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4377 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4379 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4381 GOTO(free_bufs, rc = -ENOMEM);
4383 rc = class_attach(lcfg);
4385 GOTO(lcfg_cleanup, rc);
4387 obd = class_name2obd(name);
4389 CERROR("Can not find obd %s (%s in config)\n",
4390 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4391 GOTO(class_detach, rc = -EINVAL);
4394 lustre_cfg_free(lcfg);
4396 lustre_cfg_bufs_reset(bufs, name);
4397 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4398 lustre_cfg_bufs_set_string(bufs, 2, dev);
4399 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4401 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4403 rc = class_setup(obd, lcfg);
4405 GOTO(class_detach, rc);
4407 /* connect to MDD we just setup */
4408 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4412 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4414 LASSERT(mdt_lu_site(mdt) == NULL);
4415 mdt->mdt_lu_dev.ld_site = site;
4416 site->ls_top_dev = &mdt->mdt_lu_dev;
4417 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4420 /* now connect to bottom OSD */
4421 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4422 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4426 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4429 rc = lu_env_refill((struct lu_env *)env);
4431 CERROR("Failure to refill session: '%d'\n", rc);
4433 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4438 class_detach(obd, lcfg);
4440 lustre_cfg_free(lcfg);
4445 OBD_FREE(name, name_size);
4447 OBD_FREE(uuid, uuid_size);
4451 /* setup quota master target on MDT0 */
4452 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4453 struct lustre_cfg *cfg)
4455 struct obd_device *obd;
4456 char *dev = lustre_cfg_string(cfg, 0);
4457 char *qmtname, *uuid, *p;
4458 struct lustre_cfg_bufs *bufs;
4459 struct lustre_cfg *lcfg;
4460 struct lustre_profile *lprof;
4461 struct obd_connect_data *data;
4465 LASSERT(mdt->mdt_qmt_exp == NULL);
4466 LASSERT(mdt->mdt_qmt_dev == NULL);
4468 /* quota master is on MDT0 only for now */
4469 if (mdt->mdt_seq_site.ss_node_id != 0)
4472 /* MGS generates config commands which look as follows:
4473 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4474 * 3:lustre-MDT0000-mdtlov 4:f
4476 * We generate the QMT name from the MDT one, just replacing MD with QM
4477 * after all the preparations, the logical equivalent will be:
4478 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4479 * 3:lustre-MDT0000-osd 4:f */
4480 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4481 OBD_ALLOC(uuid, UUID_MAX);
4482 OBD_ALLOC_PTR(bufs);
4483 OBD_ALLOC_PTR(data);
4484 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4485 GOTO(cleanup_mem, rc = -ENOMEM);
4487 strcpy(qmtname, dev);
4488 p = strstr(qmtname, "-MDT");
4490 GOTO(cleanup_mem, rc = -ENOMEM);
4491 /* replace MD with QM */
4495 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4497 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4498 if (lprof == NULL || lprof->lp_dt == NULL) {
4499 CERROR("can't find profile for %s\n",
4500 lustre_cfg_string(cfg, 0));
4501 GOTO(cleanup_mem, rc = -EINVAL);
4504 lustre_cfg_bufs_reset(bufs, qmtname);
4505 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4506 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4507 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4509 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4511 GOTO(cleanup_mem, rc = -ENOMEM);
4513 rc = class_attach(lcfg);
4515 GOTO(lcfg_cleanup, rc);
4517 obd = class_name2obd(qmtname);
4519 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4520 lustre_cfg_string(cfg, 0));
4521 GOTO(class_detach, rc = -EINVAL);
4524 lustre_cfg_free(lcfg);
4526 lustre_cfg_bufs_reset(bufs, qmtname);
4527 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4528 lustre_cfg_bufs_set_string(bufs, 2, dev);
4530 /* for quota, the next device should be the OSD device */
4531 lustre_cfg_bufs_set_string(bufs, 3,
4532 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4534 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4536 rc = class_setup(obd, lcfg);
4538 GOTO(class_detach, rc);
4540 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4542 /* configure local quota objects */
4543 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4547 GOTO(class_cleanup, rc);
4549 /* connect to quota master target */
4550 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4551 data->ocd_version = LUSTRE_VERSION_CODE;
4552 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4555 CERROR("cannot connect to quota master device %s (%d)\n",
4557 GOTO(class_cleanup, rc);
4563 class_manual_cleanup(obd);
4564 mdt->mdt_qmt_dev = NULL;
4568 class_detach(obd, lcfg);
4570 lustre_cfg_free(lcfg);
4575 OBD_FREE(qmtname, MAX_OBD_NAME);
4577 OBD_FREE(uuid, UUID_MAX);
4583 /* Shutdown quota master target associated with mdt */
4584 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4588 if (mdt->mdt_qmt_exp == NULL)
4590 LASSERT(mdt->mdt_qmt_dev != NULL);
4592 /* the qmt automatically shuts down when the mdt disconnects */
4593 obd_disconnect(mdt->mdt_qmt_exp);
4594 mdt->mdt_qmt_exp = NULL;
4595 mdt->mdt_qmt_dev = NULL;
4599 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4601 struct md_device *next = m->mdt_child;
4602 struct lu_device *d = &m->mdt_lu_dev;
4603 struct obd_device *obd = mdt2obd_dev(m);
4606 target_recovery_fini(obd);
4608 ping_evictor_stop();
4610 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4612 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4613 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4614 obd_exports_barrier(obd);
4615 obd_zombie_barrier();
4619 tgt_fini(env, &m->mdt_lut);
4620 mdt_fs_cleanup(env, m);
4621 upcall_cache_cleanup(m->mdt_identity_cache);
4622 m->mdt_identity_cache = NULL;
4624 if (m->mdt_namespace != NULL) {
4625 ldlm_namespace_free(m->mdt_namespace, NULL,
4626 d->ld_obd->obd_force);
4627 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4630 mdt_quota_fini(env, m);
4632 cfs_free_nidlist(&m->mdt_nosquash_nids);
4633 if (m->mdt_nosquash_str) {
4634 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4635 m->mdt_nosquash_str = NULL;
4636 m->mdt_nosquash_strlen = 0;
4639 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4641 mdt_seq_fini(env, m);
4642 mdt_fld_fini(env, m);
4643 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4645 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4646 cfs_timer_disarm(&m->mdt_ck_timer);
4647 mdt_ck_thread_stop(m);
4652 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4654 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4656 server_put_mount(mdt_obd_name(m), NULL);
4661 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4663 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4664 struct sptlrpc_rule_set tmp_rset;
4667 sptlrpc_rule_set_init(&tmp_rset);
4668 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4670 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4671 mdt_obd_name(m), rc);
4675 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4677 write_lock(&m->mdt_sptlrpc_lock);
4678 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4679 m->mdt_sptlrpc_rset = tmp_rset;
4680 write_unlock(&m->mdt_sptlrpc_lock);
4685 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4687 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4688 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4690 struct mdt_thread_info *info;
4691 struct obd_device *obd;
4692 const char *dev = lustre_cfg_string(cfg, 0);
4693 const char *num = lustre_cfg_string(cfg, 2);
4694 struct lustre_mount_info *lmi = NULL;
4695 struct lustre_sb_info *lsi;
4697 struct seq_server_site *ss_site;
4698 const char *identity_upcall = "NONE";
4699 struct md_device *next;
4705 lu_device_init(&m->mdt_lu_dev, ldt);
4707 * Environment (env) might be missing mdt_thread_key values at that
4708 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4711 * Usually device allocation path doesn't use module key values, but
4712 * mdt has to do a lot of work here, so allocate key value.
4714 rc = lu_env_refill((struct lu_env *)env);
4718 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4719 LASSERT(info != NULL);
4721 obd = class_name2obd(dev);
4722 LASSERT(obd != NULL);
4724 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4726 m->mdt_som_conf = 0;
4728 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4730 lmi = server_get_mount(dev);
4732 CERROR("Cannot get mount info for %s!\n", dev);
4735 lsi = s2lsi(lmi->lmi_sb);
4736 /* CMD is supported only in IAM mode */
4738 node_id = simple_strtol(num, NULL, 10);
4739 obd->u.obt.obt_magic = OBT_MAGIC;
4742 rwlock_init(&m->mdt_sptlrpc_lock);
4743 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4745 spin_lock_init(&m->mdt_ioepoch_lock);
4746 m->mdt_opts.mo_compat_resname = 0;
4747 m->mdt_opts.mo_mds_capa = 1;
4748 m->mdt_opts.mo_oss_capa = 1;
4749 m->mdt_capa_timeout = CAPA_TIMEOUT;
4750 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4751 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4752 m->mdt_squash_uid = 0;
4753 m->mdt_squash_gid = 0;
4754 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4755 m->mdt_nosquash_str = NULL;
4756 m->mdt_nosquash_strlen = 0;
4757 init_rwsem(&m->mdt_squash_sem);
4758 spin_lock_init(&m->mdt_osfs_lock);
4759 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4760 m->mdt_enable_remote_dir = 0;
4761 m->mdt_enable_remote_dir_gid = 0;
4763 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4764 m->mdt_lu_dev.ld_obd = obd;
4765 /* Set this lu_device to obd for error handling purposes. */
4766 obd->obd_lu_dev = &m->mdt_lu_dev;
4768 /* init the stack */
4769 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4771 CERROR("%s: Can't init device stack, rc %d\n",
4772 mdt_obd_name(m), rc);
4777 ss_site = mdt_seq_site(m);
4778 s->ld_seq_site = ss_site;
4781 /* set server index */
4782 ss_site->ss_node_id = node_id;
4784 /* failover is the default
4785 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4786 * assumed whose ss_node_id == 0 XXX
4788 obd->obd_replayable = 1;
4789 /* No connection accepted until configurations will finish */
4790 obd->obd_no_conn = 1;
4792 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4793 char *str = lustre_cfg_string(cfg, 4);
4794 if (strchr(str, 'n')) {
4795 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4796 obd->obd_replayable = 0;
4800 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4802 GOTO(err_fini_stack, rc);
4804 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4808 rc = mdt_seq_init(env, mdt_obd_name(m), m);
4810 GOTO(err_fini_fld, rc);
4812 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4813 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4814 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4815 LDLM_NAMESPACE_SERVER,
4816 LDLM_NAMESPACE_GREEDY,
4818 if (m->mdt_namespace == NULL)
4819 GOTO(err_fini_seq, rc = -ENOMEM);
4821 m->mdt_namespace->ns_lvbp = m;
4822 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4824 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4825 /* set obd_namespace for compatibility with old code */
4826 obd->obd_namespace = m->mdt_namespace;
4828 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4830 rc = mdt_ck_thread_start(m);
4832 GOTO(err_free_ns, rc);
4834 rc = mdt_fs_setup(env, m, obd, lsi);
4838 mdt_adapt_sptlrpc_conf(obd, 1);
4840 next = m->mdt_child;
4841 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4844 GOTO(err_llog_cleanup, rc);
4846 if (mntopts & MNTOPT_USERXATTR)
4847 m->mdt_opts.mo_user_xattr = 1;
4849 m->mdt_opts.mo_user_xattr = 0;
4851 if (mntopts & MNTOPT_ACL)
4852 m->mdt_opts.mo_acl = 1;
4854 m->mdt_opts.mo_acl = 0;
4856 /* XXX: to support suppgid for ACL, we enable identity_upcall
4857 * by default, otherwise, maybe got unexpected -EACCESS. */
4858 if (m->mdt_opts.mo_acl)
4859 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4861 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4863 &mdt_identity_upcall_cache_ops);
4864 if (IS_ERR(m->mdt_identity_cache)) {
4865 rc = PTR_ERR(m->mdt_identity_cache);
4866 m->mdt_identity_cache = NULL;
4867 GOTO(err_llog_cleanup, rc);
4870 rc = mdt_procfs_init(m, dev);
4872 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4873 GOTO(err_recovery, rc);
4876 rc = mdt_quota_init(env, m, cfg);
4878 GOTO(err_procfs, rc);
4880 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4881 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4882 "mdt_ldlm_client", m->mdt_ldlm_client);
4884 ping_evictor_start();
4886 /* recovery will be started upon mdt_prepare()
4887 * when the whole stack is complete and ready
4888 * to serve the requests */
4890 mdt_init_capa_ctxt(env, m);
4892 /* Reduce the initial timeout on an MDS because it doesn't need such
4893 * a long timeout as an OST does. Adaptive timeouts will adjust this
4894 * value appropriately. */
4895 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4896 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4903 target_recovery_fini(obd);
4904 upcall_cache_cleanup(m->mdt_identity_cache);
4905 m->mdt_identity_cache = NULL;
4907 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4908 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4909 mdt_fs_cleanup(env, m);
4911 cfs_timer_disarm(&m->mdt_ck_timer);
4912 mdt_ck_thread_stop(m);
4914 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4915 obd->obd_namespace = m->mdt_namespace = NULL;
4917 mdt_seq_fini(env, m);
4919 mdt_fld_fini(env, m);
4921 tgt_fini(env, &m->mdt_lut);
4923 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4926 server_put_mount(dev, lmi->lmi_mnt);
4930 /* For interoperability, the left element is old parameter, the right one
4931 * is the new version of the parameter, if some parameter is deprecated,
4932 * the new version should be set as NULL. */
4933 static struct cfg_interop_param mdt_interop_param[] = {
4934 { "mdt.group_upcall", NULL },
4935 { "mdt.quota_type", NULL },
4936 { "mdd.quota_type", NULL },
4937 { "mdt.rootsquash", "mdt.root_squash" },
4938 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4942 /* used by MGS to process specific configurations */
4943 static int mdt_process_config(const struct lu_env *env,
4944 struct lu_device *d, struct lustre_cfg *cfg)
4946 struct mdt_device *m = mdt_dev(d);
4947 struct md_device *md_next = m->mdt_child;
4948 struct lu_device *next = md2lu_dev(md_next);
4952 switch (cfg->lcfg_command) {
4954 struct lprocfs_static_vars lvars;
4955 struct obd_device *obd = d->ld_obd;
4957 /* For interoperability */
4958 struct cfg_interop_param *ptr = NULL;
4959 struct lustre_cfg *old_cfg = NULL;
4962 param = lustre_cfg_string(cfg, 1);
4963 if (param == NULL) {
4964 CERROR("param is empty\n");
4969 ptr = class_find_old_param(param, mdt_interop_param);
4971 if (ptr->new_param == NULL) {
4973 CWARN("For interoperability, skip this %s."
4974 " It is obsolete.\n", ptr->old_param);
4978 CWARN("Found old param %s, changed it to %s.\n",
4979 ptr->old_param, ptr->new_param);
4982 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4989 lprocfs_mdt_init_vars(&lvars);
4990 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4992 if (rc > 0 || rc == -ENOSYS)
4993 /* we don't understand; pass it on */
4994 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4996 if (old_cfg != NULL)
4997 lustre_cfg_free(cfg);
5002 /* others are passed further */
5003 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5009 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5010 const struct lu_object_header *hdr,
5011 struct lu_device *d)
5013 struct mdt_object *mo;
5017 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
5019 struct lu_object *o;
5020 struct lu_object_header *h;
5023 h = &mo->mot_header;
5024 lu_object_header_init(h);
5025 lu_object_init(o, h, d);
5026 lu_object_add_top(h, o);
5027 o->lo_ops = &mdt_obj_ops;
5028 mutex_init(&mo->mot_ioepoch_mutex);
5029 mutex_init(&mo->mot_lov_mutex);
5030 init_rwsem(&mo->mot_open_sem);
5036 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5037 const struct lu_object_conf *unused)
5039 struct mdt_device *d = mdt_dev(o->lo_dev);
5040 struct lu_device *under;
5041 struct lu_object *below;
5045 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5046 PFID(lu_object_fid(o)));
5048 under = &d->mdt_child->md_lu_dev;
5049 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5050 if (below != NULL) {
5051 lu_object_add(o, below);
5058 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5060 struct mdt_object *mo = mdt_obj(o);
5061 struct lu_object_header *h;
5065 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5066 PFID(lu_object_fid(o)));
5068 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5069 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5072 lu_object_header_fini(h);
5073 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5078 static int mdt_object_print(const struct lu_env *env, void *cookie,
5079 lu_printer_t p, const struct lu_object *o)
5081 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5082 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5083 "flags="LPX64", epochcount=%d, writecount=%d)",
5084 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5085 mdto->mot_ioepoch_count, mdto->mot_writecount);
5088 static int mdt_prepare(const struct lu_env *env,
5089 struct lu_device *pdev,
5090 struct lu_device *cdev)
5092 struct mdt_device *mdt = mdt_dev(cdev);
5093 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5094 struct obd_device *obd = cdev->ld_obd;
5095 struct lfsck_start_param lsp;
5102 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5106 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5110 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5114 lsp.lsp_start = NULL;
5115 lsp.lsp_namespace = mdt->mdt_namespace;
5116 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5117 OBD_IOC_START_LFSCK,
5120 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5121 mdt_obd_name(mdt), rc);
5125 if (mdt->mdt_seq_site.ss_node_id == 0) {
5126 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5127 &mdt->mdt_md_root_fid);
5132 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5133 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5134 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5135 LASSERT(obd->obd_no_conn);
5136 spin_lock(&obd->obd_dev_lock);
5137 obd->obd_no_conn = 0;
5138 spin_unlock(&obd->obd_dev_lock);
5140 if (obd->obd_recovering == 0)
5141 mdt_postrecov(env, mdt);
5146 const struct lu_device_operations mdt_lu_ops = {
5147 .ldo_object_alloc = mdt_object_alloc,
5148 .ldo_process_config = mdt_process_config,
5149 .ldo_prepare = mdt_prepare,
5152 static const struct lu_object_operations mdt_obj_ops = {
5153 .loo_object_init = mdt_object_init,
5154 .loo_object_free = mdt_object_free,
5155 .loo_object_print = mdt_object_print
5158 static int mdt_obd_set_info_async(const struct lu_env *env,
5159 struct obd_export *exp,
5160 __u32 keylen, void *key,
5161 __u32 vallen, void *val,
5162 struct ptlrpc_request_set *set)
5164 struct obd_device *obd = exp->exp_obd;
5170 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5171 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5179 * Match client and server connection feature flags.
5181 * Compute the compatibility flags for a connection request based on
5182 * features mutually supported by client and server.
5184 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5185 * must not be updated here, otherwise a partially initialized value may
5186 * be exposed. After the connection request is successfully processed,
5187 * the top-level MDT connect request handler atomically updates the export
5188 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5189 * reply. \see mdt_connect().
5191 * \param exp the obd_export associated with this client/target pair
5192 * \param mdt the target device for the connection
5193 * \param data stores data for this connect request
5196 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5197 * \retval -EBADE client and server feature requirements are incompatible
5199 static int mdt_connect_internal(struct obd_export *exp,
5200 struct mdt_device *mdt,
5201 struct obd_connect_data *data)
5203 LASSERT(data != NULL);
5205 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5206 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5208 /* If no known bits (which should not happen, probably,
5209 as everybody should support LOOKUP and UPDATE bits at least)
5210 revert to compat mode with plain locks. */
5211 if (!data->ocd_ibits_known &&
5212 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5213 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5215 if (!mdt->mdt_opts.mo_acl)
5216 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5218 if (!mdt->mdt_opts.mo_user_xattr)
5219 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5221 if (!mdt->mdt_som_conf)
5222 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5224 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5225 data->ocd_brw_size = min(data->ocd_brw_size,
5226 (__u32)MD_MAX_BRW_SIZE);
5227 if (data->ocd_brw_size == 0) {
5228 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5229 " ocd_version: %x ocd_grant: %d "
5230 "ocd_index: %u ocd_brw_size is "
5231 "unexpectedly zero, network data "
5232 "corruption? Refusing connection of this"
5235 exp->exp_client_uuid.uuid,
5236 exp, data->ocd_connect_flags, data->ocd_version,
5237 data->ocd_grant, data->ocd_index);
5242 /* NB: Disregard the rule against updating
5243 * exp_connect_data.ocd_connect_flags in this case, since
5244 * tgt_client_new() needs to know if this is a lightweight
5245 * connection, and it is safe to expose this flag before
5246 * connection processing completes. */
5247 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5248 spin_lock(&exp->exp_lock);
5249 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5250 spin_unlock(&exp->exp_lock);
5253 data->ocd_version = LUSTRE_VERSION_CODE;
5254 exp->exp_connect_data = *data;
5255 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5257 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5258 CWARN("%s: MDS requires FID support, but client not\n",
5263 if (mdt->mdt_som_conf &&
5264 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5265 OBD_CONNECT_MDS_MDS |
5266 OBD_CONNECT_SOM))) {
5267 CWARN("%s: MDS has SOM enabled, but client does not support "
5268 "it\n", mdt_obd_name(mdt));
5272 if (OCD_HAS_FLAG(data, PINGLESS)) {
5273 if (ptlrpc_pinger_suppress_pings()) {
5274 spin_lock(&exp->exp_obd->obd_dev_lock);
5275 list_del_init(&exp->exp_obd_chain_timed);
5276 spin_unlock(&exp->exp_obd->obd_dev_lock);
5278 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5285 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5286 struct obd_export *exp,
5287 struct ptlrpc_request *req)
5289 struct sptlrpc_flavor flvr;
5292 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5293 read_lock(&mdt->mdt_sptlrpc_lock);
5294 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5298 read_unlock(&mdt->mdt_sptlrpc_lock);
5300 spin_lock(&exp->exp_lock);
5302 exp->exp_sp_peer = req->rq_sp_from;
5303 exp->exp_flvr = flvr;
5305 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5306 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5307 CERROR("unauthorized rpc flavor %x from %s, "
5308 "expect %x\n", req->rq_flvr.sf_rpc,
5309 libcfs_nid2str(req->rq_peer.nid),
5310 exp->exp_flvr.sf_rpc);
5314 spin_unlock(&exp->exp_lock);
5316 if (exp->exp_sp_peer != req->rq_sp_from) {
5317 CERROR("RPC source %s doesn't match %s\n",
5318 sptlrpc_part2name(req->rq_sp_from),
5319 sptlrpc_part2name(exp->exp_sp_peer));
5322 rc = sptlrpc_target_export_check(exp, req);
5329 /* mds_connect copy */
5330 static int mdt_obd_connect(const struct lu_env *env,
5331 struct obd_export **exp, struct obd_device *obd,
5332 struct obd_uuid *cluuid,
5333 struct obd_connect_data *data,
5336 struct mdt_thread_info *info;
5337 struct obd_export *lexp;
5338 struct lustre_handle conn = { 0 };
5339 struct mdt_device *mdt;
5340 struct ptlrpc_request *req;
5344 LASSERT(env != NULL);
5345 if (!exp || !obd || !cluuid)
5348 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5349 req = info->mti_pill->rc_req;
5350 mdt = mdt_dev(obd->obd_lu_dev);
5353 * first, check whether the stack is ready to handle requests
5354 * XXX: probably not very appropriate method is used now
5355 * at some point we should find a better one
5357 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5358 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5359 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5362 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5365 rc = class_connect(&conn, obd, cluuid);
5369 lexp = class_conn2export(&conn);
5370 LASSERT(lexp != NULL);
5372 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5376 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5377 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5379 rc = mdt_connect_internal(lexp, mdt, data);
5381 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5384 info->mti_exp = lexp;
5385 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5386 rc = tgt_client_new(env, lexp);
5388 mdt_export_stats_init(obd, lexp, localdata);
5393 class_disconnect(lexp);
5402 static int mdt_obd_reconnect(const struct lu_env *env,
5403 struct obd_export *exp, struct obd_device *obd,
5404 struct obd_uuid *cluuid,
5405 struct obd_connect_data *data,
5408 struct mdt_thread_info *info;
5409 struct mdt_device *mdt;
5410 struct ptlrpc_request *req;
5414 if (exp == NULL || obd == NULL || cluuid == NULL)
5417 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5418 req = info->mti_pill->rc_req;
5419 mdt = mdt_dev(obd->obd_lu_dev);
5421 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5425 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5427 mdt_export_stats_init(obd, exp, localdata);
5432 static int mdt_export_cleanup(struct obd_export *exp)
5434 struct mdt_export_data *med = &exp->exp_mdt_data;
5435 struct obd_device *obd = exp->exp_obd;
5436 struct mdt_device *mdt;
5437 struct mdt_thread_info *info;
5439 CFS_LIST_HEAD(closing_list);
5440 struct mdt_file_data *mfd, *n;
5444 spin_lock(&med->med_open_lock);
5445 while (!cfs_list_empty(&med->med_open_head)) {
5446 cfs_list_t *tmp = med->med_open_head.next;
5447 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5449 /* Remove mfd handle so it can't be found again.
5450 * We are consuming the mfd_list reference here. */
5451 class_handle_unhash(&mfd->mfd_handle);
5452 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5454 spin_unlock(&med->med_open_lock);
5455 mdt = mdt_dev(obd->obd_lu_dev);
5456 LASSERT(mdt != NULL);
5458 rc = lu_env_init(&env, LCT_MD_THREAD);
5462 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5463 LASSERT(info != NULL);
5464 memset(info, 0, sizeof *info);
5465 info->mti_env = &env;
5466 info->mti_mdt = mdt;
5467 info->mti_exp = exp;
5469 if (!cfs_list_empty(&closing_list)) {
5470 struct md_attr *ma = &info->mti_attr;
5472 /* Close any open files (which may also cause orphan unlinking). */
5473 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5474 cfs_list_del_init(&mfd->mfd_list);
5475 ma->ma_need = ma->ma_valid = 0;
5476 /* Don't unlink orphan on failover umount, LU-184 */
5477 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5478 ma->ma_valid = MA_FLAGS;
5479 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5481 mdt_mfd_close(info, mfd);
5484 info->mti_mdt = NULL;
5485 /* cleanup client slot early */
5486 /* Do not erase record for recoverable client. */
5487 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5488 tgt_client_del(&env, exp);
5494 static int mdt_obd_disconnect(struct obd_export *exp)
5500 class_export_get(exp);
5502 rc = server_disconnect_export(exp);
5504 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5506 rc = mdt_export_cleanup(exp);
5507 class_export_put(exp);
5511 /* FIXME: Can we avoid using these two interfaces? */
5512 static int mdt_init_export(struct obd_export *exp)
5514 struct mdt_export_data *med = &exp->exp_mdt_data;
5518 CFS_INIT_LIST_HEAD(&med->med_open_head);
5519 spin_lock_init(&med->med_open_lock);
5520 mutex_init(&med->med_idmap_mutex);
5521 med->med_idmap = NULL;
5522 spin_lock(&exp->exp_lock);
5523 exp->exp_connecting = 1;
5524 spin_unlock(&exp->exp_lock);
5526 /* self-export doesn't need client data and ldlm initialization */
5527 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5528 &exp->exp_client_uuid)))
5531 rc = tgt_client_alloc(exp);
5535 rc = ldlm_init_export(exp);
5542 tgt_client_free(exp);
5544 CERROR("%s: Failed to initialize export: rc = %d\n",
5545 exp->exp_obd->obd_name, rc);
5549 static int mdt_destroy_export(struct obd_export *exp)
5553 if (exp_connect_rmtclient(exp))
5554 mdt_cleanup_idmap(&exp->exp_mdt_data);
5556 target_destroy_export(exp);
5557 /* destroy can be called from failed obd_setup, so
5558 * checking uuid is safer than obd_self_export */
5559 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5560 &exp->exp_client_uuid)))
5563 ldlm_destroy_export(exp);
5564 tgt_client_free(exp);
5566 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5567 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5572 /** The maximum depth that fid2path() will search.
5573 * This is limited only because we want to store the fids for
5574 * historical path lookup purposes.
5576 #define MAX_PATH_DEPTH 100
5578 /** mdt_path() lookup structure. */
5579 struct path_lookup_info {
5580 __u64 pli_recno; /**< history point */
5581 __u64 pli_currec; /**< current record */
5582 struct lu_fid pli_fid;
5583 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5584 struct mdt_object *pli_mdt_obj;
5585 char *pli_path; /**< full path */
5587 int pli_linkno; /**< which hardlink to follow */
5588 int pli_fidcount; /**< number of \a pli_fids */
5591 static int mdt_links_read(struct mdt_thread_info *info,
5592 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5596 LASSERT(ldata->ld_buf->lb_buf != NULL);
5598 if (!mdt_object_exists(mdt_obj))
5601 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5602 ldata->ld_buf, XATTR_NAME_LINK);
5603 if (rc == -ERANGE) {
5604 /* Buf was too small, figure out what we need. */
5605 lu_buf_free(ldata->ld_buf);
5606 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5607 ldata->ld_buf, XATTR_NAME_LINK);
5610 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5611 if (ldata->ld_buf->lb_buf == NULL)
5613 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5614 ldata->ld_buf, XATTR_NAME_LINK);
5619 return linkea_init(ldata);
5622 static int mdt_path_current(struct mdt_thread_info *info,
5623 struct path_lookup_info *pli)
5625 struct mdt_device *mdt = info->mti_mdt;
5626 struct mdt_object *mdt_obj;
5627 struct link_ea_header *leh;
5628 struct link_ea_entry *lee;
5629 struct lu_name *tmpname = &info->mti_name;
5630 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5631 struct lu_buf *buf = &info->mti_big_buf;
5634 struct linkea_data ldata = { 0 };
5638 /* temp buffer for path element, the buffer will be finally freed
5639 * in mdt_thread_info_fini */
5640 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5641 if (buf->lb_buf == NULL)
5645 ptr = pli->pli_path + pli->pli_pathlen - 1;
5648 pli->pli_fidcount = 0;
5649 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5651 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5652 * so checking root_fid can only happen on MDT0. */
5653 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5654 &pli->pli_fids[pli->pli_fidcount])) {
5655 mdt_obj = mdt_object_find(info->mti_env, mdt,
5656 &pli->pli_fids[pli->pli_fidcount]);
5657 if (IS_ERR(mdt_obj))
5658 GOTO(out, rc = PTR_ERR(mdt_obj));
5659 if (mdt_object_remote(mdt_obj)) {
5660 mdt_object_put(info->mti_env, mdt_obj);
5661 GOTO(remote_out, rc = -EREMOTE);
5663 if (!mdt_object_exists(mdt_obj)) {
5664 mdt_object_put(info->mti_env, mdt_obj);
5665 GOTO(out, rc = -ENOENT);
5668 rc = mdt_links_read(info, mdt_obj, &ldata);
5669 mdt_object_put(info->mti_env, mdt_obj);
5674 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5675 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5676 /* If set, use link #linkno for path lookup, otherwise use
5677 link #0. Only do this for the final path element. */
5678 if ((pli->pli_fidcount == 0) &&
5679 (pli->pli_linkno < leh->leh_reccount)) {
5681 for (count = 0; count < pli->pli_linkno; count++) {
5682 lee = (struct link_ea_entry *)
5683 ((char *)lee + reclen);
5684 linkea_entry_unpack(lee, &reclen, tmpname,
5687 if (pli->pli_linkno < leh->leh_reccount - 1)
5688 /* indicate to user there are more links */
5692 /* Pack the name in the end of the buffer */
5693 ptr -= tmpname->ln_namelen;
5694 if (ptr - 1 <= pli->pli_path)
5695 GOTO(out, rc = -EOVERFLOW);
5696 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5699 /* Store the parent fid for historic lookup */
5700 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5701 GOTO(out, rc = -EOVERFLOW);
5702 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5706 ptr++; /* skip leading / */
5707 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5714 /* Returns the full path to this fid, as of changelog record recno. */
5715 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5716 char *path, int pathlen, __u64 *recno, int *linkno,
5719 struct mdt_device *mdt = info->mti_mdt;
5720 struct path_lookup_info *pli;
5728 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5737 pli->pli_mdt_obj = obj;
5738 pli->pli_recno = *recno;
5739 pli->pli_path = path;
5740 pli->pli_pathlen = pathlen;
5741 pli->pli_linkno = *linkno;
5743 /* Retry multiple times in case file is being moved */
5744 while (tries-- && rc == -EAGAIN)
5745 rc = mdt_path_current(info, pli);
5747 /* return the last resolved fids to the client, so the client will
5748 * build the left path on another MDT for remote object */
5749 *fid = pli->pli_fids[pli->pli_fidcount];
5751 *recno = pli->pli_currec;
5752 /* Return next link index to caller */
5753 *linkno = pli->pli_linkno;
5760 static int mdt_fid2path(struct mdt_thread_info *info,
5761 struct getinfo_fid2path *fp)
5763 struct mdt_device *mdt = info->mti_mdt;
5764 struct mdt_object *obj;
5768 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5769 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5771 if (!fid_is_sane(&fp->gf_fid))
5774 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5775 CWARN("%s: "DFID" is invalid, sequence should be "
5776 ">= "LPX64"\n", mdt_obd_name(mdt),
5777 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5781 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5782 if (obj == NULL || IS_ERR(obj)) {
5783 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5788 if (mdt_object_remote(obj))
5790 else if (!mdt_object_exists(obj))
5796 mdt_object_put(info->mti_env, obj);
5797 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5798 PFID(&fp->gf_fid), rc);
5802 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5803 &fp->gf_linkno, &fp->gf_fid);
5805 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5806 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5808 mdt_object_put(info->mti_env, obj);
5813 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5814 void *val, int vallen)
5816 struct getinfo_fid2path *fpout, *fpin;
5819 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5822 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5823 lustre_swab_fid2path(fpin);
5825 memcpy(fpout, fpin, sizeof(*fpin));
5826 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5829 rc = mdt_fid2path(info, fpout);
5833 int mdt_get_info(struct mdt_thread_info *info)
5835 struct ptlrpc_request *req = mdt_info_req(info);
5843 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5845 CDEBUG(D_IOCTL, "No GETINFO key");
5848 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5851 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5852 if (vallen == NULL) {
5853 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5857 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5859 rc = req_capsule_server_pack(info->mti_pill);
5860 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5861 if (valout == NULL) {
5862 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5866 if (KEY_IS(KEY_FID2PATH))
5867 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5871 lustre_msg_set_status(req->rq_repmsg, rc);
5876 /* Pass the ioc down */
5877 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5878 unsigned int cmd, int len, void *data)
5880 struct lu_context ioctl_session;
5881 struct md_device *next = mdt->mdt_child;
5885 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5888 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5889 lu_context_enter(&ioctl_session);
5890 env->le_ses = &ioctl_session;
5892 LASSERT(next->md_ops->mdo_iocontrol);
5893 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5895 lu_context_exit(&ioctl_session);
5896 lu_context_fini(&ioctl_session);
5900 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5902 struct obd_ioctl_data *data = karg;
5905 struct mdt_object *obj;
5906 struct mdt_lock_handle *lh;
5910 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5911 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5914 fid = (struct lu_fid *)data->ioc_inlbuf1;
5916 if (!fid_is_sane(fid))
5919 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5921 lh = &mti->mti_lh[MDT_LH_PARENT];
5922 mdt_lock_reg_init(lh, LCK_CR);
5924 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5926 RETURN(PTR_ERR(obj));
5928 if (mdt_object_remote(obj)) {
5931 * before calling version get the correct MDS should be
5932 * fid, this is error to find remote object here
5934 CERROR("nonlocal object "DFID"\n", PFID(fid));
5935 } else if (!mdt_object_exists(obj)) {
5936 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5939 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5940 *(__u64 *)data->ioc_inlbuf2 = version;
5943 mdt_object_unlock_put(mti, obj, lh, 1);
5947 /* ioctls on obd dev */
5948 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5949 void *karg, void *uarg)
5952 struct obd_device *obd = exp->exp_obd;
5953 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5954 struct dt_device *dt = mdt->mdt_bottom;
5958 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5959 rc = lu_env_init(&env, LCT_MD_THREAD);
5965 rc = mdt_device_sync(&env, mdt);
5967 case OBD_IOC_SET_READONLY:
5968 rc = dt->dd_ops->dt_ro(&env, dt);
5970 case OBD_IOC_ABORT_RECOVERY:
5971 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5972 target_stop_recovery_thread(obd);
5975 case OBD_IOC_CHANGELOG_REG:
5976 case OBD_IOC_CHANGELOG_DEREG:
5977 case OBD_IOC_CHANGELOG_CLEAR:
5978 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5980 case OBD_IOC_START_LFSCK: {
5981 struct md_device *next = mdt->mdt_child;
5982 struct obd_ioctl_data *data = karg;
5983 struct lfsck_start_param lsp;
5985 if (unlikely(data == NULL)) {
5990 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5991 lsp.lsp_namespace = mdt->mdt_namespace;
5992 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5995 case OBD_IOC_STOP_LFSCK: {
5996 struct md_device *next = mdt->mdt_child;
5998 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
6001 case OBD_IOC_GET_OBJ_VERSION: {
6002 struct mdt_thread_info *mti;
6003 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6004 memset(mti, 0, sizeof *mti);
6005 mti->mti_env = &env;
6009 rc = mdt_ioc_version_get(mti, karg);
6014 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6015 mdt_obd_name(mdt), cmd, rc);
6022 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6024 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6028 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6032 int mdt_obd_postrecov(struct obd_device *obd)
6037 rc = lu_env_init(&env, LCT_MD_THREAD);
6040 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6045 static struct obd_ops mdt_obd_device_ops = {
6046 .o_owner = THIS_MODULE,
6047 .o_set_info_async = mdt_obd_set_info_async,
6048 .o_connect = mdt_obd_connect,
6049 .o_reconnect = mdt_obd_reconnect,
6050 .o_disconnect = mdt_obd_disconnect,
6051 .o_init_export = mdt_init_export,
6052 .o_destroy_export = mdt_destroy_export,
6053 .o_iocontrol = mdt_iocontrol,
6054 .o_postrecov = mdt_obd_postrecov,
6057 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6058 struct lu_device *d)
6060 struct mdt_device *m = mdt_dev(d);
6067 static struct lu_device *mdt_device_free(const struct lu_env *env,
6068 struct lu_device *d)
6070 struct mdt_device *m = mdt_dev(d);
6073 lu_device_fini(&m->mdt_lu_dev);
6079 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6080 struct lu_device_type *t,
6081 struct lustre_cfg *cfg)
6083 struct lu_device *l;
6084 struct mdt_device *m;
6091 rc = mdt_init0(env, m, t, cfg);
6093 mdt_device_free(env, l);
6098 l = ERR_PTR(-ENOMEM);
6102 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6103 LU_KEY_INIT(mdt, struct mdt_thread_info);
6105 static void mdt_key_fini(const struct lu_context *ctx,
6106 struct lu_context_key *key, void* data)
6108 struct mdt_thread_info *info = data;
6110 if (info->mti_big_lmm) {
6111 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6112 info->mti_big_lmm = NULL;
6113 info->mti_big_lmmsize = 0;
6118 /* context key: mdt_thread_key */
6119 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6121 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6123 return lu_ucred(info->mti_env);
6126 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6128 return lu_ucred_check(info->mti_env);
6132 * Enable/disable COS (Commit On Sharing).
6134 * Set/Clear the COS flag in mdt options.
6136 * \param mdt mdt device
6137 * \param val 0 disables COS, other values enable COS
6139 void mdt_enable_cos(struct mdt_device *mdt, int val)
6144 mdt->mdt_opts.mo_cos = !!val;
6145 rc = lu_env_init(&env, LCT_LOCAL);
6146 if (unlikely(rc != 0)) {
6147 CWARN("lu_env initialization failed with rc = %d,"
6148 "cannot sync\n", rc);
6151 mdt_device_sync(&env, mdt);
6156 * Check COS (Commit On Sharing) status.
6158 * Return COS flag status.
6160 * \param mdt mdt device
6162 int mdt_cos_is_enabled(struct mdt_device *mdt)
6164 return mdt->mdt_opts.mo_cos != 0;
6167 static struct lu_device_type_operations mdt_device_type_ops = {
6168 .ldto_device_alloc = mdt_device_alloc,
6169 .ldto_device_free = mdt_device_free,
6170 .ldto_device_fini = mdt_device_fini
6173 static struct lu_device_type mdt_device_type = {
6174 .ldt_tags = LU_DEVICE_MD,
6175 .ldt_name = LUSTRE_MDT_NAME,
6176 .ldt_ops = &mdt_device_type_ops,
6177 .ldt_ctx_tags = LCT_MD_THREAD
6180 static int __init mdt_mod_init(void)
6182 struct lprocfs_static_vars lvars;
6185 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6186 FID_NOBRACE_LEN + 1);
6187 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6189 rc = lu_kmem_init(mdt_caches);
6193 rc = mds_mod_init();
6197 lprocfs_mdt_init_vars(&lvars);
6198 rc = class_register_type(&mdt_obd_device_ops, NULL,
6199 lvars.module_vars, LUSTRE_MDT_NAME,
6205 lu_kmem_fini(mdt_caches);
6212 static void __exit mdt_mod_exit(void)
6214 class_unregister_type(LUSTRE_MDT_NAME);
6216 lu_kmem_fini(mdt_caches);
6219 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6220 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6221 MODULE_LICENSE("GPL");
6223 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);