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_log.h>
66 #include "mdt_internal.h"
67 #include <lustre_acl.h>
68 #include <lustre_param.h>
69 #include <lustre_quota.h>
70 #include <lustre_linkea.h>
71 #include <lustre_lfsck.h>
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 ldlm_mode_t mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
96 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
134 return (rep->lock_policy_res1 & flag);
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, int flag)
141 info->mti_opdata &= ~flag;
143 rep->lock_policy_res1 &= ~flag;
146 void mdt_set_disposition(struct mdt_thread_info *info,
147 struct ldlm_reply *rep, int flag)
150 info->mti_opdata |= flag;
152 rep->lock_policy_res1 |= flag;
155 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
157 lh->mlh_pdo_hash = 0;
158 lh->mlh_reg_mode = lm;
159 lh->mlh_rreg_mode = lm;
160 lh->mlh_type = MDT_REG_LOCK;
163 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
164 const char *name, int namelen)
166 lh->mlh_reg_mode = lm;
167 lh->mlh_rreg_mode = lm;
168 lh->mlh_type = MDT_PDO_LOCK;
170 if (name != NULL && (name[0] != '\0')) {
171 LASSERT(namelen > 0);
172 lh->mlh_pdo_hash = full_name_hash(name, namelen);
173 /* XXX Workaround for LU-2856
174 * Zero is a valid return value of full_name_hash, but several
175 * users of mlh_pdo_hash assume a non-zero hash value. We
176 * therefore map zero onto an arbitrary, but consistent
177 * value (1) to avoid problems further down the road. */
178 if (unlikely(!lh->mlh_pdo_hash))
179 lh->mlh_pdo_hash = 1;
181 LASSERT(namelen == 0);
182 lh->mlh_pdo_hash = 0ull;
186 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
187 struct mdt_lock_handle *lh)
193 * Any dir access needs couple of locks:
195 * 1) on part of dir we gonna take lookup/modify;
197 * 2) on whole dir to protect it from concurrent splitting and/or to
198 * flush client's cache for readdir().
200 * so, for a given mode and object this routine decides what lock mode
201 * to use for lock #2:
203 * 1) if caller's gonna lookup in dir then we need to protect dir from
204 * being splitted only - LCK_CR
206 * 2) if caller's gonna modify dir then we need to protect dir from
207 * being splitted and to flush cache - LCK_CW
209 * 3) if caller's gonna modify dir and that dir seems ready for
210 * splitting then we need to protect it from any type of access
211 * (lookup/modify/split) - LCK_EX --bzzz
214 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
215 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
218 * Ask underlaying level its opinion about preferable PDO lock mode
219 * having access type passed as regular lock mode:
221 * - MDL_MINMODE means that lower layer does not want to specify lock
224 * - MDL_NL means that no PDO lock should be taken. This is used in some
225 * cases. Say, for non-splittable directories no need to use PDO locks
228 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
229 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
231 if (mode != MDL_MINMODE) {
232 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
235 * Lower layer does not want to specify locking mode. We do it
236 * our selves. No special protection is needed, just flush
237 * client's cache on modification and allow concurrent
240 switch (lh->mlh_reg_mode) {
242 lh->mlh_pdo_mode = LCK_EX;
245 lh->mlh_pdo_mode = LCK_CR;
248 lh->mlh_pdo_mode = LCK_CW;
251 CERROR("Not expected lock type (0x%x)\n",
252 (int)lh->mlh_reg_mode);
257 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
261 int mdt_getstatus(struct mdt_thread_info *info)
263 struct mdt_device *mdt = info->mti_mdt;
264 struct mdt_body *repbody;
268 rc = mdt_check_ucred(info);
270 RETURN(err_serious(rc));
272 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
273 RETURN(err_serious(-ENOMEM));
275 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
276 repbody->fid1 = mdt->mdt_md_root_fid;
277 repbody->valid |= OBD_MD_FLID;
279 if (mdt->mdt_opts.mo_mds_capa &&
280 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
281 struct mdt_object *root;
282 struct lustre_capa *capa;
284 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
286 RETURN(PTR_ERR(root));
288 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
290 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
291 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
293 mdt_object_put(info->mti_env, root);
295 repbody->valid |= OBD_MD_FLMDSCAPA;
301 int mdt_statfs(struct mdt_thread_info *info)
303 struct ptlrpc_request *req = mdt_info_req(info);
304 struct md_device *next = info->mti_mdt->mdt_child;
305 struct ptlrpc_service_part *svcpt;
306 struct obd_statfs *osfs;
311 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
313 /* This will trigger a watchdog timeout */
314 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
315 (MDT_SERVICE_WATCHDOG_FACTOR *
316 at_get(&svcpt->scp_at_estimate)) + 1);
318 rc = mdt_check_ucred(info);
320 RETURN(err_serious(rc));
322 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
323 RETURN(err_serious(-ENOMEM));
325 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
329 /** statfs information are cached in the mdt_device */
330 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
331 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
332 /** statfs data is too old, get up-to-date one */
333 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
336 spin_lock(&info->mti_mdt->mdt_osfs_lock);
337 info->mti_mdt->mdt_osfs = *osfs;
338 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
339 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
341 /** use cached statfs data */
342 spin_lock(&info->mti_mdt->mdt_osfs_lock);
343 *osfs = info->mti_mdt->mdt_osfs;
344 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
348 mdt_counter_incr(req, LPROC_MDT_STATFS);
354 * Pack SOM attributes into the reply.
355 * Call under a DLM UPDATE lock.
357 static void mdt_pack_size2body(struct mdt_thread_info *info,
358 struct mdt_object *mo)
361 struct md_attr *ma = &info->mti_attr;
363 LASSERT(ma->ma_attr.la_valid & LA_MODE);
364 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
366 /* Check if Size-on-MDS is supported, if this is a regular file,
367 * if SOM is enabled on the object and if SOM cache exists and valid.
368 * Otherwise do not pack Size-on-MDS attributes to the reply. */
369 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
370 !S_ISREG(ma->ma_attr.la_mode) ||
371 !mdt_object_is_som_enabled(mo) ||
372 !(ma->ma_valid & MA_SOM))
375 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
376 b->size = ma->ma_som->msd_size;
377 b->blocks = ma->ma_som->msd_blocks;
380 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
381 const struct lu_attr *attr, const struct lu_fid *fid)
383 struct md_attr *ma = &info->mti_attr;
385 LASSERT(ma->ma_valid & MA_INODE);
387 b->atime = attr->la_atime;
388 b->mtime = attr->la_mtime;
389 b->ctime = attr->la_ctime;
390 b->mode = attr->la_mode;
391 b->size = attr->la_size;
392 b->blocks = attr->la_blocks;
393 b->uid = attr->la_uid;
394 b->gid = attr->la_gid;
395 b->flags = attr->la_flags;
396 b->nlink = attr->la_nlink;
397 b->rdev = attr->la_rdev;
399 /*XXX should pack the reply body according to lu_valid*/
400 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
401 OBD_MD_FLGID | OBD_MD_FLTYPE |
402 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
403 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
405 if (!S_ISREG(attr->la_mode)) {
406 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
407 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
408 /* means no objects are allocated on osts. */
409 LASSERT(!(ma->ma_valid & MA_LOV));
410 /* just ignore blocks occupied by extend attributes on MDS */
412 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
413 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
414 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
415 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
416 /* A released file stores its size on MDS. */
417 /* But return 1 block for released file, unless tools like tar
418 * will consider it fully sparse. (LU-3864)
420 if (unlikely(b->size == 0))
424 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
429 b->valid |= OBD_MD_FLID;
430 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
431 PFID(fid), b->nlink, b->mode, b->size);
435 mdt_body_reverse_idmap(info, b);
437 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
438 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
439 PFID(fid), (unsigned long long)b->size);
442 static inline int mdt_body_has_lov(const struct lu_attr *la,
443 const struct mdt_body *body)
445 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
446 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
449 void mdt_client_compatibility(struct mdt_thread_info *info)
451 struct mdt_body *body;
452 struct ptlrpc_request *req = mdt_info_req(info);
453 struct obd_export *exp = req->rq_export;
454 struct md_attr *ma = &info->mti_attr;
455 struct lu_attr *la = &ma->ma_attr;
458 if (exp_connect_layout(exp))
459 /* the client can deal with 16-bit lmm_stripe_count */
462 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
464 if (!mdt_body_has_lov(la, body))
467 /* now we have a reply with a lov for a client not compatible with the
468 * layout lock so we have to clean the layout generation number */
469 if (S_ISREG(la->la_mode))
470 ma->ma_lmm->lmm_layout_gen = 0;
474 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
477 const struct lu_env *env = info->mti_env;
481 LASSERT(info->mti_big_lmm_used == 0);
482 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
486 /* big_lmm may need to be grown */
487 if (info->mti_big_lmmsize < rc) {
488 int size = size_roundup_power2(rc);
490 if (info->mti_big_lmmsize > 0) {
491 /* free old buffer */
492 LASSERT(info->mti_big_lmm);
493 OBD_FREE_LARGE(info->mti_big_lmm,
494 info->mti_big_lmmsize);
495 info->mti_big_lmm = NULL;
496 info->mti_big_lmmsize = 0;
499 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
500 if (info->mti_big_lmm == NULL)
502 info->mti_big_lmmsize = size;
504 LASSERT(info->mti_big_lmmsize >= rc);
506 info->mti_buf.lb_buf = info->mti_big_lmm;
507 info->mti_buf.lb_len = info->mti_big_lmmsize;
508 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
513 int mdt_attr_get_lov(struct mdt_thread_info *info,
514 struct mdt_object *o, struct md_attr *ma)
516 struct md_object *next = mdt_object_child(o);
517 struct lu_buf *buf = &info->mti_buf;
520 buf->lb_buf = ma->ma_lmm;
521 buf->lb_len = ma->ma_lmm_size;
522 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
524 ma->ma_lmm_size = rc;
525 ma->ma_valid |= MA_LOV;
527 } else if (rc == -ENODATA) {
530 } else if (rc == -ERANGE) {
531 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
533 info->mti_big_lmm_used = 1;
534 ma->ma_valid |= MA_LOV;
535 ma->ma_lmm = info->mti_big_lmm;
536 ma->ma_lmm_size = rc;
537 /* update mdt_max_mdsize so all clients
538 * will be aware about that */
539 if (info->mti_mdt->mdt_max_mdsize < rc)
540 info->mti_mdt->mdt_max_mdsize = rc;
548 int mdt_attr_get_pfid(struct mdt_thread_info *info,
549 struct mdt_object *o, struct lu_fid *pfid)
551 struct lu_buf *buf = &info->mti_buf;
552 struct link_ea_header *leh;
553 struct link_ea_entry *lee;
557 buf->lb_buf = info->mti_big_lmm;
558 buf->lb_len = info->mti_big_lmmsize;
559 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
560 buf, XATTR_NAME_LINK);
561 /* ignore errors, MA_PFID won't be set and it is
562 * up to the caller to treat this as an error */
563 if (rc == -ERANGE || buf->lb_len == 0) {
564 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
565 buf->lb_buf = info->mti_big_lmm;
566 buf->lb_len = info->mti_big_lmmsize;
571 if (rc < sizeof(*leh)) {
572 CERROR("short LinkEA on "DFID": rc = %d\n",
573 PFID(mdt_object_fid(o)), rc);
577 leh = (struct link_ea_header *) buf->lb_buf;
578 lee = (struct link_ea_entry *)(leh + 1);
579 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
580 leh->leh_magic = LINK_EA_MAGIC;
581 leh->leh_reccount = __swab32(leh->leh_reccount);
582 leh->leh_len = __swab64(leh->leh_len);
584 if (leh->leh_magic != LINK_EA_MAGIC)
586 if (leh->leh_reccount == 0)
589 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
590 fid_be_to_cpu(pfid, pfid);
595 int mdt_attr_get_complex(struct mdt_thread_info *info,
596 struct mdt_object *o, struct md_attr *ma)
598 const struct lu_env *env = info->mti_env;
599 struct md_object *next = mdt_object_child(o);
600 struct lu_buf *buf = &info->mti_buf;
601 u32 mode = lu_object_attr(&next->mo_lu);
602 int need = ma->ma_need;
608 if (need & MA_INODE) {
609 ma->ma_need = MA_INODE;
610 rc = mo_attr_get(env, next, ma);
613 ma->ma_valid |= MA_INODE;
616 if (need & MA_PFID) {
617 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
619 ma->ma_valid |= MA_PFID;
620 /* ignore this error, parent fid is not mandatory */
624 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
625 rc = mdt_attr_get_lov(info, o, ma);
630 if (need & MA_LMV && S_ISDIR(mode)) {
631 buf->lb_buf = ma->ma_lmv;
632 buf->lb_len = ma->ma_lmv_size;
633 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
635 ma->ma_lmv_size = rc2;
636 ma->ma_valid |= MA_LMV;
637 } else if (rc2 == -ENODATA) {
644 if (need & MA_SOM && S_ISREG(mode)) {
645 buf->lb_buf = info->mti_xattr_buf;
646 buf->lb_len = sizeof(info->mti_xattr_buf);
647 CLASSERT(sizeof(struct som_attrs) <=
648 sizeof(info->mti_xattr_buf));
649 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
650 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
652 ma->ma_valid |= MA_SOM;
653 else if (rc2 < 0 && rc2 != -ENODATA)
657 if (need & MA_HSM && S_ISREG(mode)) {
658 buf->lb_buf = info->mti_xattr_buf;
659 buf->lb_len = sizeof(info->mti_xattr_buf);
660 CLASSERT(sizeof(struct hsm_attrs) <=
661 sizeof(info->mti_xattr_buf));
662 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
663 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
665 ma->ma_valid |= MA_HSM;
666 else if (rc2 < 0 && rc2 != -ENODATA)
670 #ifdef CONFIG_FS_POSIX_ACL
671 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
672 buf->lb_buf = ma->ma_acl;
673 buf->lb_len = ma->ma_acl_size;
674 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
676 ma->ma_acl_size = rc2;
677 ma->ma_valid |= MA_ACL_DEF;
678 } else if (rc2 == -ENODATA) {
687 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
688 rc, ma->ma_valid, ma->ma_lmm);
692 static int mdt_getattr_internal(struct mdt_thread_info *info,
693 struct mdt_object *o, int ma_need)
695 struct md_object *next = mdt_object_child(o);
696 const struct mdt_body *reqbody = info->mti_body;
697 struct ptlrpc_request *req = mdt_info_req(info);
698 struct md_attr *ma = &info->mti_attr;
699 struct lu_attr *la = &ma->ma_attr;
700 struct req_capsule *pill = info->mti_pill;
701 const struct lu_env *env = info->mti_env;
702 struct mdt_body *repbody;
703 struct lu_buf *buffer = &info->mti_buf;
708 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
709 RETURN(err_serious(-ENOMEM));
711 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
715 if (mdt_object_remote(o)) {
716 /* This object is located on remote node.*/
717 /* Return -EIO for old client */
718 if (!mdt_is_dne_client(req->rq_export))
719 GOTO(out, rc = -EIO);
721 repbody->fid1 = *mdt_object_fid(o);
722 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
726 buffer->lb_len = reqbody->eadatasize;
727 if (buffer->lb_len > 0)
728 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
730 buffer->lb_buf = NULL;
732 /* If it is dir object and client require MEA, then we got MEA */
733 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
734 reqbody->valid & OBD_MD_MEA) {
735 /* Assumption: MDT_MD size is enough for lmv size. */
736 ma->ma_lmv = buffer->lb_buf;
737 ma->ma_lmv_size = buffer->lb_len;
738 ma->ma_need = MA_LMV | MA_INODE;
740 ma->ma_lmm = buffer->lb_buf;
741 ma->ma_lmm_size = buffer->lb_len;
742 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
745 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
746 reqbody->valid & OBD_MD_FLDIREA &&
747 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
748 /* get default stripe info for this dir. */
749 ma->ma_need |= MA_LOV_DEF;
751 ma->ma_need |= ma_need;
752 if (ma->ma_need & MA_SOM)
753 ma->ma_som = &info->mti_u.som.data;
755 rc = mdt_attr_get_complex(info, o, ma);
757 CERROR("%s: getattr error for "DFID": rc = %d\n",
758 mdt_obd_name(info->mti_mdt),
759 PFID(mdt_object_fid(o)), rc);
763 /* if file is released, check if a restore is running */
764 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
765 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
766 repbody->t_state = MS_RESTORE;
767 repbody->valid |= OBD_MD_TSTATE;
770 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
772 /* the Lustre protocol supposes to return default striping
773 * on the user-visible root if explicitly requested */
774 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
775 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
776 struct lu_fid rootfid;
777 struct mdt_object *root;
778 struct mdt_device *mdt = info->mti_mdt;
780 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
783 root = mdt_object_find(env, mdt, &rootfid);
785 RETURN(PTR_ERR(root));
786 rc = mdt_attr_get_lov(info, root, ma);
787 mdt_object_put(info->mti_env, root);
789 CERROR("%s: getattr error for "DFID": rc = %d\n",
790 mdt_obd_name(info->mti_mdt),
791 PFID(mdt_object_fid(o)), rc);
796 if (likely(ma->ma_valid & MA_INODE))
797 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
801 if (mdt_body_has_lov(la, reqbody)) {
802 if (ma->ma_valid & MA_LOV) {
803 LASSERT(ma->ma_lmm_size);
804 mdt_dump_lmm(D_INFO, ma->ma_lmm);
805 repbody->eadatasize = ma->ma_lmm_size;
806 if (S_ISDIR(la->la_mode))
807 repbody->valid |= OBD_MD_FLDIREA;
809 repbody->valid |= OBD_MD_FLEASIZE;
811 if (ma->ma_valid & MA_LMV) {
812 LASSERT(S_ISDIR(la->la_mode));
813 repbody->eadatasize = ma->ma_lmv_size;
814 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
816 } else if (S_ISLNK(la->la_mode) &&
817 reqbody->valid & OBD_MD_LINKNAME) {
818 buffer->lb_buf = ma->ma_lmm;
819 /* eadatasize from client includes NULL-terminator, so
820 * there is no need to read it */
821 buffer->lb_len = reqbody->eadatasize - 1;
822 rc = mo_readlink(env, next, buffer);
823 if (unlikely(rc <= 0)) {
824 CERROR("%s: readlink failed for "DFID": rc = %d\n",
825 mdt_obd_name(info->mti_mdt),
826 PFID(mdt_object_fid(o)), rc);
829 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
831 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
833 repbody->valid |= OBD_MD_LINKNAME;
834 /* we need to report back size with NULL-terminator
835 * because client expects that */
836 repbody->eadatasize = rc + 1;
837 if (repbody->eadatasize != reqbody->eadatasize)
838 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
839 "on "DFID ", expected %d\n",
840 mdt_obd_name(info->mti_mdt),
841 rc, PFID(mdt_object_fid(o)),
842 reqbody->eadatasize - 1);
844 ((char *)ma->ma_lmm)[rc] = 0;
846 /* If the total CDEBUG() size is larger than a page, it
847 * will print a warning to the console, avoid this by
848 * printing just the last part of the symlink. */
849 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
850 print_limit < rc ? "..." : "", print_limit,
851 (char *)ma->ma_lmm + rc - print_limit, rc);
856 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
857 repbody->max_cookiesize = 0;
858 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
859 repbody->valid |= OBD_MD_FLMODEASIZE;
860 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
861 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
862 repbody->max_cookiesize);
865 if (exp_connect_rmtclient(info->mti_exp) &&
866 reqbody->valid & OBD_MD_FLRMTPERM) {
867 void *buf = req_capsule_server_get(pill, &RMF_ACL);
869 /* mdt_getattr_lock only */
870 rc = mdt_pack_remote_perm(info, o, buf);
872 repbody->valid &= ~OBD_MD_FLRMTPERM;
873 repbody->aclsize = 0;
876 repbody->valid |= OBD_MD_FLRMTPERM;
877 repbody->aclsize = sizeof(struct mdt_remote_perm);
880 #ifdef CONFIG_FS_POSIX_ACL
881 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
882 (reqbody->valid & OBD_MD_FLACL)) {
883 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
884 buffer->lb_len = req_capsule_get_size(pill,
885 &RMF_ACL, RCL_SERVER);
886 if (buffer->lb_len > 0) {
887 rc = mo_xattr_get(env, next, buffer,
888 XATTR_NAME_ACL_ACCESS);
890 if (rc == -ENODATA) {
891 repbody->aclsize = 0;
892 repbody->valid |= OBD_MD_FLACL;
894 } else if (rc == -EOPNOTSUPP) {
897 CERROR("%s: unable to read "DFID
899 mdt_obd_name(info->mti_mdt),
900 PFID(mdt_object_fid(o)), rc);
903 repbody->aclsize = rc;
904 repbody->valid |= OBD_MD_FLACL;
911 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
912 info->mti_mdt->mdt_opts.mo_mds_capa &&
913 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
914 struct lustre_capa *capa;
916 capa = req_capsule_server_get(pill, &RMF_CAPA1);
918 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
919 rc = mo_capa_get(env, next, capa, 0);
922 repbody->valid |= OBD_MD_FLMDSCAPA;
927 mdt_counter_incr(req, LPROC_MDT_GETATTR);
932 static int mdt_renew_capa(struct mdt_thread_info *info)
934 struct mdt_object *obj = info->mti_object;
935 struct mdt_body *body;
936 struct lustre_capa *capa, *c;
940 /* if object doesn't exist, or server has disabled capability,
941 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
944 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
945 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
948 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
949 LASSERT(body != NULL);
951 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
954 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
958 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
960 body->valid |= OBD_MD_FLOSSCAPA;
964 int mdt_getattr(struct mdt_thread_info *info)
966 struct mdt_object *obj = info->mti_object;
967 struct req_capsule *pill = info->mti_pill;
968 struct mdt_body *reqbody;
969 struct mdt_body *repbody;
974 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
977 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
978 rc = req_capsule_server_pack(pill);
980 RETURN(err_serious(rc));
981 rc = mdt_renew_capa(info);
982 GOTO(out_shrink, rc);
985 LASSERT(obj != NULL);
986 LASSERT(lu_object_assert_exists(&obj->mot_obj));
988 mode = lu_object_attr(&obj->mot_obj);
990 /* old clients may not report needed easize, use max value then */
991 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
992 reqbody->eadatasize == 0 ?
993 info->mti_mdt->mdt_max_mdsize :
994 reqbody->eadatasize);
996 rc = req_capsule_server_pack(pill);
997 if (unlikely(rc != 0))
998 RETURN(err_serious(rc));
1000 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1001 LASSERT(repbody != NULL);
1002 repbody->eadatasize = 0;
1003 repbody->aclsize = 0;
1005 if (reqbody->valid & OBD_MD_FLRMTPERM)
1006 rc = mdt_init_ucred(info, reqbody);
1008 rc = mdt_check_ucred(info);
1010 GOTO(out_shrink, rc);
1012 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1015 * Don't check capability at all, because rename might getattr for
1016 * remote obj, and at that time no capability is available.
1018 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1019 rc = mdt_getattr_internal(info, obj, 0);
1020 if (reqbody->valid & OBD_MD_FLRMTPERM)
1021 mdt_exit_ucred(info);
1024 mdt_client_compatibility(info);
1025 rc2 = mdt_fix_reply(info);
1031 int mdt_is_subdir(struct mdt_thread_info *info)
1033 struct mdt_object *o = info->mti_object;
1034 struct req_capsule *pill = info->mti_pill;
1035 const struct mdt_body *body = info->mti_body;
1036 struct mdt_body *repbody;
1042 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1045 * We save last checked parent fid to @repbody->fid1 for remote
1048 LASSERT(fid_is_sane(&body->fid2));
1049 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1050 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1051 &body->fid2, &repbody->fid1);
1052 if (rc == 0 || rc == -EREMOTE)
1053 repbody->valid |= OBD_MD_FLID;
1058 int mdt_swap_layouts(struct mdt_thread_info *info)
1060 struct ptlrpc_request *req = mdt_info_req(info);
1061 struct obd_export *exp = req->rq_export;
1062 struct mdt_object *o1, *o2, *o;
1063 struct mdt_lock_handle *lh1, *lh2;
1064 struct mdc_swap_layouts *msl;
1068 /* client does not support layout lock, so layout swaping
1070 * FIXME: there is a problem for old clients which don't support
1071 * layout lock yet. If those clients have already opened the file
1072 * they won't be notified at all so that old layout may still be
1073 * used to do IO. This can be fixed after file release is landed by
1074 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1075 if (!exp_connect_layout(exp))
1076 RETURN(-EOPNOTSUPP);
1078 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1079 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1080 req_capsule_client_get(info->mti_pill,
1083 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1084 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1085 req_capsule_client_get(info->mti_pill,
1088 o1 = info->mti_object;
1089 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1090 &info->mti_body->fid2);
1092 GOTO(out, rc = PTR_ERR(o));
1094 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1095 GOTO(put, rc = -ENOENT);
1097 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1098 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1104 /* permission check. Make sure the calling process having permission
1105 * to write both files. */
1106 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1111 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1116 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1118 GOTO(put, rc = -EPROTO);
1120 lh1 = &info->mti_lh[MDT_LH_NEW];
1121 mdt_lock_reg_init(lh1, LCK_EX);
1122 lh2 = &info->mti_lh[MDT_LH_OLD];
1123 mdt_lock_reg_init(lh2, LCK_EX);
1125 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1126 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1130 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1131 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1135 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1136 mdt_object_child(o2), msl->msl_flags);
1139 mdt_object_unlock(info, o2, lh2, rc);
1141 mdt_object_unlock(info, o1, lh1, rc);
1143 mdt_object_put(info->mti_env, o);
1148 static int mdt_raw_lookup(struct mdt_thread_info *info,
1149 struct mdt_object *parent,
1150 const struct lu_name *lname,
1151 struct ldlm_reply *ldlm_rep)
1153 struct md_object *next = mdt_object_child(info->mti_object);
1154 const struct mdt_body *reqbody = info->mti_body;
1155 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1156 struct mdt_body *repbody;
1160 if (reqbody->valid != OBD_MD_FLID)
1163 LASSERT(!info->mti_cross_ref);
1165 /* Only got the fid of this obj by name */
1166 fid_zero(child_fid);
1167 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1170 /* XXX is raw_lookup possible as intent operation? */
1173 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1176 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1178 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1181 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1182 repbody->fid1 = *child_fid;
1183 repbody->valid = OBD_MD_FLID;
1189 * UPDATE lock should be taken against parent, and be release before exit;
1190 * child_bits lock should be taken against child, and be returned back:
1191 * (1)normal request should release the child lock;
1192 * (2)intent request will grant the lock to client.
1194 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1195 struct mdt_lock_handle *lhc,
1197 struct ldlm_reply *ldlm_rep)
1199 struct ptlrpc_request *req = mdt_info_req(info);
1200 struct mdt_body *reqbody = NULL;
1201 struct mdt_object *parent = info->mti_object;
1202 struct mdt_object *child;
1203 struct md_object *next = mdt_object_child(parent);
1204 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1205 struct lu_name *lname = NULL;
1206 const char *name = NULL;
1208 struct mdt_lock_handle *lhp = NULL;
1209 struct ldlm_lock *lock;
1210 struct ldlm_res_id *res_id;
1217 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1218 LASSERT(ergo(is_resent,
1219 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1221 LASSERT(parent != NULL);
1222 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1224 RETURN(err_serious(-EFAULT));
1226 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1228 if (!info->mti_cross_ref) {
1230 * XXX: Check for "namelen == 0" is for getattr by fid
1231 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1232 * that is the name must contain at least one character and
1233 * the terminating '\0'
1236 reqbody = req_capsule_client_get(info->mti_pill,
1238 if (unlikely(reqbody == NULL))
1239 RETURN(err_serious(-EFAULT));
1241 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1242 RETURN(err_serious(-EINVAL));
1245 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1247 PFID(mdt_object_fid(parent)),
1248 PFID(&reqbody->fid2), ldlm_rep);
1250 lname = mdt_name(info->mti_env, (char *)name, namelen);
1251 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1252 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1256 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1258 if (unlikely(!mdt_object_exists(parent))) {
1259 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1261 "Parent doesn't exist!\n");
1263 } else if (!info->mti_cross_ref) {
1264 LASSERTF(!mdt_object_remote(parent),
1265 "Parent "DFID" is on remote server\n",
1266 PFID(mdt_object_fid(parent)));
1269 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1277 if (info->mti_cross_ref) {
1278 /* Only getattr on the child. Parent is on another node. */
1279 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1281 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1282 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1285 /* Do not take lock for resent case. */
1286 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1287 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1288 lhc->mlh_reg_lh.cookie);
1289 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1290 &lock->l_resource->lr_name));
1291 LDLM_LOCK_PUT(lock);
1294 mdt_lock_handle_init(lhc);
1295 mdt_lock_reg_init(lhc, LCK_PR);
1298 * Object's name is on another MDS, no lookup lock is
1299 * needed here but update is.
1301 child_bits &= ~MDS_INODELOCK_LOOKUP;
1302 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1304 rc = mdt_object_lock(info, child, lhc, child_bits,
1308 /* Finally, we can get attr for child. */
1309 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1311 rc = mdt_getattr_internal(info, child, 0);
1312 if (unlikely(rc != 0))
1313 mdt_object_unlock(info, child, lhc, 1);
1319 /* step 1: lock parent only if parent is a directory */
1320 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1321 lhp = &info->mti_lh[MDT_LH_PARENT];
1322 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1323 rc = mdt_object_lock(info, parent, lhp,
1324 MDS_INODELOCK_UPDATE,
1326 if (unlikely(rc != 0))
1330 /* step 2: lookup child's fid by name */
1331 fid_zero(child_fid);
1332 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1337 mdt_set_disposition(info, ldlm_rep,
1339 GOTO(out_parent, rc);
1341 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1343 *child_fid = reqbody->fid2;
1344 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1348 *step 3: find the child object by fid & lock it.
1349 * regardless if it is local or remote.
1351 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1353 if (unlikely(IS_ERR(child)))
1354 GOTO(out_parent, rc = PTR_ERR(child));
1356 /* Do not take lock for resent case. */
1357 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1358 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1359 lhc->mlh_reg_lh.cookie);
1361 res_id = &lock->l_resource->lr_name;
1362 if (!fid_res_name_eq(mdt_object_fid(child),
1363 &lock->l_resource->lr_name)) {
1364 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1365 &lock->l_resource->lr_name),
1366 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1367 PLDLMRES(lock->l_resource),
1368 PFID(mdt_object_fid(parent)));
1369 CWARN("Although resent, but still not get child lock"
1370 "parent:"DFID" child:"DFID"\n",
1371 PFID(mdt_object_fid(parent)),
1372 PFID(mdt_object_fid(child)));
1373 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1374 LDLM_LOCK_PUT(lock);
1377 LDLM_LOCK_PUT(lock);
1380 bool try_layout = false;
1383 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1384 mdt_lock_handle_init(lhc);
1385 mdt_lock_reg_init(lhc, LCK_PR);
1387 if (!mdt_object_exists(child)) {
1388 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1390 "Object doesn't exist!\n");
1391 GOTO(out_child, rc = -ENOENT);
1394 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1395 mdt_object_exists(child) && !mdt_object_remote(child)) {
1396 struct md_attr *ma = &info->mti_attr;
1399 ma->ma_need = MA_INODE;
1400 rc = mdt_attr_get_complex(info, child, ma);
1401 if (unlikely(rc != 0))
1402 GOTO(out_child, rc);
1404 /* If the file has not been changed for some time, we
1405 * return not only a LOOKUP lock, but also an UPDATE
1406 * lock and this might save us RPC on later STAT. For
1407 * directories, it also let negative dentry cache start
1408 * working for this dir. */
1409 if (ma->ma_valid & MA_INODE &&
1410 ma->ma_attr.la_valid & LA_CTIME &&
1411 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1412 ma->ma_attr.la_ctime < cfs_time_current_sec())
1413 child_bits |= MDS_INODELOCK_UPDATE;
1416 /* layout lock must be granted in a best-effort way
1417 * for IT operations */
1418 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1419 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1420 exp_connect_layout(info->mti_exp) &&
1421 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1423 /* try to grant layout lock for regular file. */
1429 child_bits |= MDS_INODELOCK_LAYOUT;
1430 /* try layout lock, it may fail to be granted due to
1431 * contention at LOOKUP or UPDATE */
1432 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1434 child_bits &= ~MDS_INODELOCK_LAYOUT;
1435 LASSERT(child_bits != 0);
1436 rc = mdt_object_lock(info, child, lhc,
1437 child_bits, MDT_CROSS_LOCK);
1442 rc = mdt_object_lock(info, child, lhc, child_bits,
1445 if (unlikely(rc != 0))
1446 GOTO(out_child, rc);
1449 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1450 /* Get MA_SOM attributes if update lock is given. */
1452 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1453 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1456 /* finally, we can get attr for child. */
1457 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1458 rc = mdt_getattr_internal(info, child, ma_need);
1459 if (unlikely(rc != 0)) {
1460 mdt_object_unlock(info, child, lhc, 1);
1462 /* Debugging code. */
1463 res_id = &lock->l_resource->lr_name;
1464 LDLM_DEBUG(lock, "Returning lock to client");
1465 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1466 &lock->l_resource->lr_name),
1467 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1468 PLDLMRES(lock->l_resource),
1469 PFID(mdt_object_fid(child)));
1470 if (mdt_object_exists(child) && !mdt_object_remote(child))
1471 mdt_pack_size2body(info, child);
1474 LDLM_LOCK_PUT(lock);
1478 mdt_object_put(info->mti_env, child);
1481 mdt_object_unlock(info, parent, lhp, 1);
1485 /* normal handler: should release the child lock */
1486 int mdt_getattr_name(struct mdt_thread_info *info)
1488 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1489 struct mdt_body *reqbody;
1490 struct mdt_body *repbody;
1494 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1495 LASSERT(reqbody != NULL);
1496 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1497 LASSERT(repbody != NULL);
1499 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1500 repbody->eadatasize = 0;
1501 repbody->aclsize = 0;
1503 rc = mdt_init_ucred(info, reqbody);
1505 GOTO(out_shrink, rc);
1507 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1508 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1509 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1510 lhc->mlh_reg_lh.cookie = 0;
1512 mdt_exit_ucred(info);
1515 mdt_client_compatibility(info);
1516 rc2 = mdt_fix_reply(info);
1522 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1523 void *karg, void *uarg);
1525 int mdt_set_info(struct mdt_thread_info *info)
1527 struct ptlrpc_request *req = mdt_info_req(info);
1530 int keylen, vallen, rc = 0;
1533 rc = req_capsule_server_pack(info->mti_pill);
1537 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1539 DEBUG_REQ(D_HA, req, "no set_info key");
1543 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1546 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1548 DEBUG_REQ(D_HA, req, "no set_info val");
1552 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1555 /* Swab any part of val you need to here */
1556 if (KEY_IS(KEY_READ_ONLY)) {
1558 lustre_msg_set_status(req->rq_repmsg, 0);
1560 spin_lock(&req->rq_export->exp_lock);
1562 *exp_connect_flags_ptr(req->rq_export) |=
1565 *exp_connect_flags_ptr(req->rq_export) &=
1566 ~OBD_CONNECT_RDONLY;
1567 spin_unlock(&req->rq_export->exp_lock);
1569 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1570 struct changelog_setinfo *cs =
1571 (struct changelog_setinfo *)val;
1572 if (vallen != sizeof(*cs)) {
1573 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1576 if (ptlrpc_req_need_swab(req)) {
1577 __swab64s(&cs->cs_recno);
1578 __swab32s(&cs->cs_id);
1581 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1583 lustre_msg_set_status(req->rq_repmsg, rc);
1591 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
1592 struct ptlrpc_request *req);
1595 * Top-level handler for MDT connection requests.
1597 int mdt_connect(struct mdt_thread_info *info)
1600 struct obd_connect_data *reply;
1601 struct obd_export *exp;
1602 struct ptlrpc_request *req = mdt_info_req(info);
1606 rc = target_handle_connect(req);
1608 RETURN(err_serious(rc));
1610 LASSERT(req->rq_export != NULL);
1611 exp = req->rq_export;
1612 info->mti_exp = exp;
1613 info->mti_mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
1614 rc = mdt_init_sec_level(info);
1618 rc = mdt_connect_check_sptlrpc(info->mti_mdt, exp, req);
1622 /* To avoid exposing partially initialized connection flags, changes up
1623 * to this point have been staged in reply->ocd_connect_flags. Now that
1624 * connection handling has completed successfully, atomically update
1625 * the connect flags in the shared export data structure. LU-1623 */
1626 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1627 spin_lock(&exp->exp_lock);
1628 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1629 exp->exp_mdt_data.med_ibits_known = reply->ocd_ibits_known;
1630 exp->exp_connect_data.ocd_brw_size = reply->ocd_brw_size;
1631 spin_unlock(&exp->exp_lock);
1633 rc = mdt_init_idmap(info);
1638 obd_disconnect(class_export_get(req->rq_export));
1642 int mdt_disconnect(struct mdt_thread_info *info)
1647 rc = target_handle_disconnect(mdt_info_req(info));
1649 rc = err_serious(rc);
1653 static int mdt_sendpage(struct mdt_thread_info *info,
1654 struct lu_rdpg *rdpg, int nob)
1656 struct ptlrpc_request *req = mdt_info_req(info);
1657 struct obd_export *exp = req->rq_export;
1658 struct ptlrpc_bulk_desc *desc;
1659 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1666 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1671 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1672 /* old client requires reply size in it's PAGE_SIZE,
1673 * which is rdpg->rp_count */
1674 nob = rdpg->rp_count;
1676 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1677 i++, tmpcount -= tmpsize) {
1678 tmpsize = min_t(int, tmpcount, PAGE_CACHE_SIZE);
1679 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1682 LASSERT(desc->bd_nob == nob);
1683 rc = target_bulk_io(exp, desc, lwi);
1684 ptlrpc_free_bulk_pin(desc);
1688 int mdt_readpage(struct mdt_thread_info *info)
1690 struct mdt_object *object = info->mti_object;
1691 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1692 struct mdt_body *reqbody;
1693 struct mdt_body *repbody;
1698 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1699 RETURN(err_serious(-ENOMEM));
1701 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1702 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1703 if (reqbody == NULL || repbody == NULL)
1704 RETURN(err_serious(-EFAULT));
1707 * prepare @rdpg before calling lower layers and transfer itself. Here
1708 * reqbody->size contains offset of where to start to read and
1709 * reqbody->nlink contains number bytes to read.
1711 rdpg->rp_hash = reqbody->size;
1712 if (rdpg->rp_hash != reqbody->size) {
1713 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1714 rdpg->rp_hash, reqbody->size);
1718 rdpg->rp_attrs = reqbody->mode;
1719 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1720 rdpg->rp_attrs |= LUDA_64BITHASH;
1721 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1722 exp_max_brw_size(info->mti_exp));
1723 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1725 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1726 if (rdpg->rp_pages == NULL)
1729 for (i = 0; i < rdpg->rp_npages; ++i) {
1730 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1731 if (rdpg->rp_pages[i] == NULL)
1732 GOTO(free_rdpg, rc = -ENOMEM);
1735 /* call lower layers to fill allocated pages with directory data */
1736 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1738 GOTO(free_rdpg, rc);
1740 /* send pages to client */
1741 rc = mdt_sendpage(info, rdpg, rc);
1746 for (i = 0; i < rdpg->rp_npages; i++)
1747 if (rdpg->rp_pages[i] != NULL)
1748 __free_page(rdpg->rp_pages[i]);
1749 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1751 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1757 static int mdt_reint_internal(struct mdt_thread_info *info,
1758 struct mdt_lock_handle *lhc,
1761 struct req_capsule *pill = info->mti_pill;
1762 struct mdt_body *repbody;
1767 rc = mdt_reint_unpack(info, op);
1769 CERROR("Can't unpack reint, rc %d\n", rc);
1770 RETURN(err_serious(rc));
1773 /* for replay (no_create) lmm is not needed, client has it already */
1774 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1775 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1776 info->mti_rr.rr_eadatalen);
1778 /* llog cookies are always 0, the field is kept for compatibility */
1779 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1780 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1782 rc = req_capsule_server_pack(pill);
1784 CERROR("Can't pack response, rc %d\n", rc);
1785 RETURN(err_serious(rc));
1788 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1789 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1791 repbody->eadatasize = 0;
1792 repbody->aclsize = 0;
1795 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1797 /* for replay no cookkie / lmm need, because client have this already */
1798 if (info->mti_spec.no_create)
1799 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1800 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1802 rc = mdt_init_ucred_reint(info);
1804 GOTO(out_shrink, rc);
1806 rc = mdt_fix_attr_ucred(info, op);
1808 GOTO(out_ucred, rc = err_serious(rc));
1810 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1811 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1812 GOTO(out_ucred, rc);
1814 rc = mdt_reint_rec(info, lhc);
1817 mdt_exit_ucred(info);
1819 mdt_client_compatibility(info);
1820 rc2 = mdt_fix_reply(info);
1826 static long mdt_reint_opcode(struct mdt_thread_info *info,
1827 const struct req_format **fmt)
1829 struct mdt_rec_reint *rec;
1832 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1834 opc = rec->rr_opcode;
1835 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1836 if (opc < REINT_MAX && fmt[opc] != NULL)
1837 req_capsule_extend(info->mti_pill, fmt[opc]);
1839 CERROR("%s: Unsupported opcode '%ld' from client '%s': "
1840 "rc = %d\n", mdt_obd_name(info->mti_mdt), opc,
1841 info->mti_mdt->mdt_ldlm_client->cli_name,
1843 opc = err_serious(-EFAULT);
1846 opc = err_serious(-EFAULT);
1851 int mdt_reint(struct mdt_thread_info *info)
1856 static const struct req_format *reint_fmts[REINT_MAX] = {
1857 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1858 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1859 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1860 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1861 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1862 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1863 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1864 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1869 opc = mdt_reint_opcode(info, reint_fmts);
1872 * No lock possible here from client to pass it to reint code
1875 rc = mdt_reint_internal(info, NULL, opc);
1880 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1884 /* this should sync the whole device */
1885 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1887 struct dt_device *dt = mdt->mdt_bottom;
1891 rc = dt->dd_ops->dt_sync(env, dt);
1895 /* this should sync this object */
1896 static int mdt_object_sync(struct mdt_thread_info *info)
1898 struct md_object *next;
1902 if (!mdt_object_exists(info->mti_object)) {
1903 CWARN("Non existing object "DFID"!\n",
1904 PFID(mdt_object_fid(info->mti_object)));
1907 next = mdt_object_child(info->mti_object);
1908 rc = mo_object_sync(info->mti_env, next);
1913 int mdt_sync(struct mdt_thread_info *info)
1915 struct ptlrpc_request *req = mdt_info_req(info);
1916 struct req_capsule *pill = info->mti_pill;
1917 struct mdt_body *body;
1921 /* The fid may be zero, so we req_capsule_set manually */
1922 req_capsule_set(pill, &RQF_MDS_SYNC);
1924 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1926 RETURN(err_serious(-EINVAL));
1928 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1929 RETURN(err_serious(-ENOMEM));
1931 if (fid_seq(&body->fid1) == 0) {
1932 /* sync the whole device */
1933 rc = req_capsule_server_pack(pill);
1935 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1937 rc = err_serious(rc);
1939 /* sync an object */
1940 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1942 rc = mdt_object_sync(info);
1944 const struct lu_fid *fid;
1945 struct lu_attr *la = &info->mti_attr.ma_attr;
1947 info->mti_attr.ma_need = MA_INODE;
1948 info->mti_attr.ma_valid = 0;
1949 rc = mdt_attr_get_complex(info, info->mti_object,
1952 body = req_capsule_server_get(pill,
1954 fid = mdt_object_fid(info->mti_object);
1955 mdt_pack_attr2body(info, body, la, fid);
1959 rc = err_serious(rc);
1962 mdt_counter_incr(req, LPROC_MDT_SYNC);
1968 * Quotacheck handler.
1969 * in-kernel quotacheck isn't supported any more.
1971 int mdt_quotacheck(struct mdt_thread_info *info)
1973 struct obd_quotactl *oqctl;
1977 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1979 RETURN(err_serious(-EPROTO));
1981 rc = req_capsule_server_pack(info->mti_pill);
1983 RETURN(err_serious(rc));
1985 /* deprecated, not used any more */
1986 RETURN(-EOPNOTSUPP);
1990 * Handle quota control requests to consult current usage/limit, but also
1991 * to configure quota enforcement
1993 int mdt_quotactl(struct mdt_thread_info *info)
1995 struct obd_export *exp = info->mti_exp;
1996 struct req_capsule *pill = info->mti_pill;
1997 struct obd_quotactl *oqctl, *repoqc;
1999 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2002 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2004 RETURN(err_serious(-EPROTO));
2006 rc = req_capsule_server_pack(pill);
2008 RETURN(err_serious(rc));
2010 switch (oqctl->qc_cmd) {
2012 case LUSTRE_Q_INVALIDATE:
2013 case LUSTRE_Q_FINVALIDATE:
2017 /* deprecated, not used any more */
2018 RETURN(-EOPNOTSUPP);
2019 /* master quotactl */
2025 RETURN(-EOPNOTSUPP);
2026 /* slave quotactl */
2031 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2035 /* map uid/gid for remote client */
2037 if (exp_connect_rmtclient(exp)) {
2038 struct lustre_idmap_table *idmap;
2040 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2042 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2043 oqctl->qc_cmd != Q_GETINFO))
2046 if (oqctl->qc_type == USRQUOTA)
2047 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2049 else if (oqctl->qc_type == GRPQUOTA)
2050 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2055 if (id == CFS_IDMAP_NOTFOUND) {
2056 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2061 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2063 RETURN(err_serious(-EFAULT));
2065 if (oqctl->qc_id != id)
2066 swap(oqctl->qc_id, id);
2068 switch (oqctl->qc_cmd) {
2074 /* forward quotactl request to QMT */
2075 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2080 /* slave quotactl */
2081 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2086 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2090 if (oqctl->qc_id != id)
2091 swap(oqctl->qc_id, id);
2098 * OBD PING and other handlers.
2100 int mdt_obd_ping(struct mdt_thread_info *info)
2105 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2107 rc = target_handle_ping(mdt_info_req(info));
2109 rc = err_serious(rc);
2114 * OBD_IDX_READ handler
2116 int mdt_obd_idx_read(struct mdt_thread_info *info)
2118 struct mdt_device *mdt = info->mti_mdt;
2119 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2120 struct idx_info *req_ii, *rep_ii;
2124 memset(rdpg, 0, sizeof(*rdpg));
2125 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2127 /* extract idx_info buffer from request & reply */
2128 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2129 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2130 RETURN(err_serious(-EPROTO));
2132 rc = req_capsule_server_pack(info->mti_pill);
2134 RETURN(err_serious(rc));
2136 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2138 RETURN(err_serious(-EFAULT));
2139 rep_ii->ii_magic = IDX_INFO_MAGIC;
2141 /* extract hash to start with */
2142 rdpg->rp_hash = req_ii->ii_hash_start;
2144 /* extract requested attributes */
2145 rdpg->rp_attrs = req_ii->ii_attrs;
2147 /* check that fid packed in request is valid and supported */
2148 if (!fid_is_sane(&req_ii->ii_fid))
2150 rep_ii->ii_fid = req_ii->ii_fid;
2153 rep_ii->ii_flags = req_ii->ii_flags;
2155 /* compute number of pages to allocate, ii_count is the number of 4KB
2157 if (req_ii->ii_count <= 0)
2158 GOTO(out, rc = -EFAULT);
2159 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2160 exp_max_brw_size(info->mti_exp));
2161 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
2164 /* allocate pages to store the containers */
2165 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2166 if (rdpg->rp_pages == NULL)
2167 GOTO(out, rc = -ENOMEM);
2168 for (i = 0; i < rdpg->rp_npages; i++) {
2169 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
2170 if (rdpg->rp_pages[i] == NULL)
2171 GOTO(out, rc = -ENOMEM);
2174 /* populate pages with key/record pairs */
2175 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2179 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2180 "asked %d > %d\n", rc, rdpg->rp_count);
2182 /* send pages to client */
2183 rc = mdt_sendpage(info, rdpg, rc);
2187 if (rdpg->rp_pages) {
2188 for (i = 0; i < rdpg->rp_npages; i++)
2189 if (rdpg->rp_pages[i])
2190 __free_page(rdpg->rp_pages[i]);
2191 OBD_FREE(rdpg->rp_pages,
2192 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2197 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2199 return err_serious(-EOPNOTSUPP);
2202 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2204 return err_serious(-EOPNOTSUPP);
2211 /** clone llog ctxt from child (mdd)
2212 * This allows remote llog (replicator) access.
2213 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2214 * context was originally set up, or we can handle them directly.
2215 * I choose the latter, but that means I need any llog
2216 * contexts set up by child to be accessable by the mdt. So we clone the
2217 * context into our context list here.
2219 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2222 struct md_device *next = mdt->mdt_child;
2223 struct llog_ctxt *ctxt;
2226 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2229 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2230 if (rc || ctxt == NULL) {
2234 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2236 CERROR("Can't set mdt ctxt %d\n", rc);
2241 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2242 struct mdt_device *mdt, int idx)
2244 struct llog_ctxt *ctxt;
2246 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2249 /* Put once for the get we just did, and once for the clone */
2250 llog_ctxt_put(ctxt);
2251 llog_ctxt_put(ctxt);
2255 int mdt_llog_create(struct mdt_thread_info *info)
2259 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2260 rc = llog_origin_handle_open(mdt_info_req(info));
2261 return (rc < 0 ? err_serious(rc) : rc);
2264 int mdt_llog_destroy(struct mdt_thread_info *info)
2268 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2269 rc = llog_origin_handle_destroy(mdt_info_req(info));
2270 return (rc < 0 ? err_serious(rc) : rc);
2273 int mdt_llog_read_header(struct mdt_thread_info *info)
2277 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2278 rc = llog_origin_handle_read_header(mdt_info_req(info));
2279 return (rc < 0 ? err_serious(rc) : rc);
2282 int mdt_llog_next_block(struct mdt_thread_info *info)
2286 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2287 rc = llog_origin_handle_next_block(mdt_info_req(info));
2288 return (rc < 0 ? err_serious(rc) : rc);
2291 int mdt_llog_prev_block(struct mdt_thread_info *info)
2295 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2296 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2297 return (rc < 0 ? err_serious(rc) : rc);
2305 static struct ldlm_callback_suite cbs = {
2306 .lcs_completion = ldlm_server_completion_ast,
2307 .lcs_blocking = ldlm_server_blocking_ast,
2308 .lcs_glimpse = ldlm_server_glimpse_ast
2311 int mdt_enqueue(struct mdt_thread_info *info)
2313 struct ptlrpc_request *req;
2317 * info->mti_dlm_req already contains swapped and (if necessary)
2318 * converted dlm request.
2320 LASSERT(info->mti_dlm_req != NULL);
2322 req = mdt_info_req(info);
2323 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2324 req, info->mti_dlm_req, &cbs);
2325 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2326 return rc ? err_serious(rc) : req->rq_status;
2329 int mdt_convert(struct mdt_thread_info *info)
2332 struct ptlrpc_request *req;
2334 LASSERT(info->mti_dlm_req);
2335 req = mdt_info_req(info);
2336 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2337 return rc ? err_serious(rc) : req->rq_status;
2340 int mdt_bl_callback(struct mdt_thread_info *info)
2342 CERROR("bl callbacks should not happen on MDS\n");
2344 return err_serious(-EOPNOTSUPP);
2347 int mdt_cp_callback(struct mdt_thread_info *info)
2349 CERROR("cp callbacks should not happen on MDS\n");
2351 return err_serious(-EOPNOTSUPP);
2355 * sec context handlers
2357 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2361 rc = mdt_handle_idmap(info);
2364 struct ptlrpc_request *req = mdt_info_req(info);
2367 opc = lustre_msg_get_opc(req->rq_reqmsg);
2368 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2369 sptlrpc_svc_ctx_invalidate(req);
2372 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2378 * quota request handlers
2380 int mdt_quota_dqacq(struct mdt_thread_info *info)
2382 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2387 RETURN(err_serious(-EOPNOTSUPP));
2389 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2393 static struct mdt_object *mdt_obj(struct lu_object *o)
2395 LASSERT(lu_device_is_mdt(o->lo_dev));
2396 return container_of0(o, struct mdt_object, mot_obj);
2399 struct mdt_object *mdt_object_new(const struct lu_env *env,
2400 struct mdt_device *d,
2401 const struct lu_fid *f)
2403 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2404 struct lu_object *o;
2405 struct mdt_object *m;
2408 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2409 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2410 if (unlikely(IS_ERR(o)))
2411 m = (struct mdt_object *)o;
2417 struct mdt_object *mdt_object_find(const struct lu_env *env,
2418 struct mdt_device *d,
2419 const struct lu_fid *f)
2421 struct lu_object *o;
2422 struct mdt_object *m;
2425 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2426 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2427 if (unlikely(IS_ERR(o)))
2428 m = (struct mdt_object *)o;
2436 * Asyncronous commit for mdt device.
2438 * Pass asynchonous commit call down the MDS stack.
2440 * \param env environment
2441 * \param mdt the mdt device
2443 static void mdt_device_commit_async(const struct lu_env *env,
2444 struct mdt_device *mdt)
2446 struct dt_device *dt = mdt->mdt_bottom;
2449 rc = dt->dd_ops->dt_commit_async(env, dt);
2450 if (unlikely(rc != 0))
2451 CWARN("async commit start failed with rc = %d", rc);
2455 * Mark the lock as "synchonous".
2457 * Mark the lock to deffer transaction commit to the unlock time.
2459 * \param lock the lock to mark as "synchonous"
2461 * \see mdt_is_lock_sync
2462 * \see mdt_save_lock
2464 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2466 lock->l_ast_data = (void*)1;
2470 * Check whehter the lock "synchonous" or not.
2472 * \param lock the lock to check
2473 * \retval 1 the lock is "synchonous"
2474 * \retval 0 the lock isn't "synchronous"
2476 * \see mdt_set_lock_sync
2477 * \see mdt_save_lock
2479 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2481 return lock->l_ast_data != NULL;
2485 * Blocking AST for mdt locks.
2487 * Starts transaction commit if in case of COS lock conflict or
2488 * deffers such a commit to the mdt_save_lock.
2490 * \param lock the lock which blocks a request or cancelling lock
2491 * \param desc unused
2492 * \param data unused
2493 * \param flag indicates whether this cancelling or blocking callback
2495 * \see ldlm_blocking_ast_nocheck
2497 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2498 void *data, int flag)
2500 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2501 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2505 if (flag == LDLM_CB_CANCELING)
2507 lock_res_and_lock(lock);
2508 if (lock->l_blocking_ast != mdt_blocking_ast) {
2509 unlock_res_and_lock(lock);
2512 if (mdt_cos_is_enabled(mdt) &&
2513 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2514 lock->l_blocking_lock != NULL &&
2515 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2516 mdt_set_lock_sync(lock);
2518 rc = ldlm_blocking_ast_nocheck(lock);
2520 /* There is no lock conflict if l_blocking_lock == NULL,
2521 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2522 * when the last reference to a local lock was released */
2523 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2526 rc = lu_env_init(&env, LCT_LOCAL);
2527 if (unlikely(rc != 0))
2528 CWARN("lu_env initialization failed with rc = %d,"
2529 "cannot start asynchronous commit\n", rc);
2531 mdt_device_commit_async(&env, mdt);
2537 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2538 void *data, int flag)
2540 struct lustre_handle lockh;
2544 case LDLM_CB_BLOCKING:
2545 ldlm_lock2handle(lock, &lockh);
2546 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2548 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2552 case LDLM_CB_CANCELING:
2553 LDLM_DEBUG(lock, "Revoke remote lock\n");
2561 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2562 struct mdt_object *o, struct lustre_handle *lh,
2563 ldlm_mode_t mode, __u64 ibits)
2565 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2566 ldlm_policy_data_t *policy = &mti->mti_policy;
2570 LASSERT(mdt_object_remote(o));
2572 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2574 memset(einfo, 0, sizeof(*einfo));
2575 einfo->ei_type = LDLM_IBITS;
2576 einfo->ei_mode = mode;
2577 einfo->ei_cb_bl = mdt_md_blocking_ast;
2578 einfo->ei_cb_cp = ldlm_completion_ast;
2580 memset(policy, 0, sizeof(*policy));
2581 policy->l_inodebits.bits = ibits;
2583 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2588 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2589 struct mdt_lock_handle *lh, __u64 ibits,
2590 bool nonblock, int locality)
2592 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2593 ldlm_policy_data_t *policy = &info->mti_policy;
2594 struct ldlm_res_id *res_id = &info->mti_res_id;
2599 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2600 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2601 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2602 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2604 if (mdt_object_remote(o)) {
2605 if (locality == MDT_CROSS_LOCK) {
2606 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2607 ibits |= MDS_INODELOCK_LOOKUP;
2610 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2611 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2612 mdt_obd_name(info->mti_mdt), ibits,
2613 PFID(mdt_object_fid(o)));
2614 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2616 /* No PDO lock on remote object */
2617 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2620 if (lh->mlh_type == MDT_PDO_LOCK) {
2621 /* check for exists after object is locked */
2622 if (mdt_object_exists(o) == 0) {
2623 /* Non-existent object shouldn't have PDO lock */
2626 /* Non-dir object shouldn't have PDO lock */
2627 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2632 memset(policy, 0, sizeof(*policy));
2633 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2635 dlmflags = LDLM_FL_ATOMIC_CB;
2637 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2640 * Take PDO lock on whole directory and build correct @res_id for lock
2641 * on part of directory.
2643 if (lh->mlh_pdo_hash != 0) {
2644 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2645 mdt_lock_pdo_mode(info, o, lh);
2646 if (lh->mlh_pdo_mode != LCK_NL) {
2648 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2649 * is never going to be sent to client and we do not
2650 * want it slowed down due to possible cancels.
2652 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2653 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2654 policy, res_id, dlmflags,
2655 &info->mti_exp->exp_handle.h_cookie);
2661 * Finish res_id initializing by name hash marking part of
2662 * directory which is taking modification.
2664 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2667 policy->l_inodebits.bits = ibits;
2670 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2671 * going to be sent to client. If it is - mdt_intent_policy() path will
2672 * fix it up and turn FL_LOCAL flag off.
2674 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2675 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2676 &info->mti_exp->exp_handle.h_cookie);
2678 mdt_object_unlock(info, o, lh, 1);
2679 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2680 lh->mlh_pdo_hash != 0 &&
2681 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2682 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2688 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2689 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2691 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2694 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2695 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2697 struct mdt_lock_handle tmp = *lh;
2700 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2708 * Save a lock within request object.
2710 * Keep the lock referenced until whether client ACK or transaction
2711 * commit happens or release the lock immediately depending on input
2712 * parameters. If COS is ON, a write lock is converted to COS lock
2715 * \param info thead info object
2716 * \param h lock handle
2717 * \param mode lock mode
2718 * \param decref force immediate lock releasing
2721 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2722 ldlm_mode_t mode, int decref)
2726 if (lustre_handle_is_used(h)) {
2727 if (decref || !info->mti_has_trans ||
2728 !(mode & (LCK_PW | LCK_EX))){
2729 mdt_fid_unlock(h, mode);
2731 struct mdt_device *mdt = info->mti_mdt;
2732 struct ldlm_lock *lock = ldlm_handle2lock(h);
2733 struct ptlrpc_request *req = mdt_info_req(info);
2736 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2738 CDEBUG(D_HA, "request = %p reply state = %p"
2739 " transno = "LPD64"\n",
2740 req, req->rq_reply_state, req->rq_transno);
2741 if (mdt_cos_is_enabled(mdt)) {
2743 ldlm_lock_downgrade(lock, LCK_COS);
2746 ptlrpc_save_lock(req, h, mode, no_ack);
2747 if (mdt_is_lock_sync(lock)) {
2748 CDEBUG(D_HA, "found sync-lock,"
2749 " async commit started\n");
2750 mdt_device_commit_async(info->mti_env,
2753 LDLM_LOCK_PUT(lock);
2762 * Unlock mdt object.
2764 * Immeditely release the regular lock and the PDO lock or save the
2765 * lock in reqeuest and keep them referenced until client ACK or
2766 * transaction commit.
2768 * \param info thread info object
2769 * \param o mdt object
2770 * \param lh mdt lock handle referencing regular and PDO locks
2771 * \param decref force immediate lock releasing
2773 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2774 struct mdt_lock_handle *lh, int decref)
2778 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2779 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2781 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2782 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2787 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2788 const struct lu_fid *f,
2789 struct mdt_lock_handle *lh,
2792 struct mdt_object *o;
2794 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2798 rc = mdt_object_lock(info, o, lh, ibits,
2801 mdt_object_put(info->mti_env, o);
2808 void mdt_object_unlock_put(struct mdt_thread_info * info,
2809 struct mdt_object * o,
2810 struct mdt_lock_handle *lh,
2813 mdt_object_unlock(info, o, lh, decref);
2814 mdt_object_put(info->mti_env, o);
2817 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2819 struct mdt_opc_slice *s;
2820 struct mdt_handler *h;
2823 for (s = supported; s->mos_hs != NULL; s++) {
2824 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2825 h = s->mos_hs + (opc - s->mos_opc_start);
2826 if (likely(h->mh_opc != 0))
2827 LASSERTF(h->mh_opc == opc,
2828 "opcode mismatch %d != %d\n",
2831 h = NULL; /* unsupported opc */
2838 static int mdt_lock_resname_compat(struct mdt_device *m,
2839 struct ldlm_request *req)
2841 /* XXX something... later. */
2845 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2847 /* XXX something... later. */
2852 * Generic code handling requests that have struct mdt_body passed in:
2854 * - extract mdt_body from request and save it in @info, if present;
2856 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2859 * - if HABEO_CORPUS flag is set for this request type check whether object
2860 * actually exists on storage (lu_object_exists()).
2863 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2865 const struct mdt_body *body;
2866 struct mdt_object *obj;
2867 const struct lu_env *env;
2868 struct req_capsule *pill;
2872 env = info->mti_env;
2873 pill = info->mti_pill;
2875 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2879 if (!(body->valid & OBD_MD_FLID))
2882 if (!fid_is_sane(&body->fid1)) {
2883 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2888 * Do not get size or any capa fields before we check that request
2889 * contains capa actually. There are some requests which do not, for
2890 * instance MDS_IS_SUBDIR.
2892 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2893 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2894 mdt_set_capainfo(info, 0, &body->fid1,
2895 req_capsule_client_get(pill, &RMF_CAPA1));
2897 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2899 if ((flags & HABEO_CORPUS) &&
2900 !mdt_object_exists(obj)) {
2901 mdt_object_put(env, obj);
2902 /* for capability renew ENOENT will be handled in
2904 if (body->valid & OBD_MD_FLOSSCAPA)
2909 info->mti_object = obj;
2918 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2920 struct req_capsule *pill = info->mti_pill;
2924 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2925 rc = mdt_body_unpack(info, flags);
2929 if (rc == 0 && (flags & HABEO_REFERO)) {
2931 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2932 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2933 info->mti_body->eadatasize);
2934 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2935 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2938 rc = req_capsule_server_pack(pill);
2943 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2945 struct md_device *next = m->mdt_child;
2947 return next->md_ops->mdo_init_capa_ctxt(env, next,
2948 m->mdt_opts.mo_mds_capa,
2949 m->mdt_capa_timeout,
2955 * Invoke handler for this request opc. Also do necessary preprocessing
2956 * (according to handler ->mh_flags), and post-processing (setting of
2957 * ->last_{xid,committed}).
2959 static int mdt_req_handle(struct mdt_thread_info *info,
2960 struct mdt_handler *h, struct ptlrpc_request *req)
2962 int rc, serious = 0;
2967 LASSERT(h->mh_act != NULL);
2968 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2969 LASSERT(current->journal_info == NULL);
2972 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2973 * to put same checks into handlers like mdt_close(), mdt_reint(),
2974 * etc., without talking to mdt authors first. Checking same thing
2975 * there again is useless and returning 0 error without packing reply
2976 * is buggy! Handlers either pack reply or return error.
2978 * We return 0 here and do not send any reply in order to emulate
2979 * network failure. Do not send any reply in case any of NET related
2980 * fail_id has occured.
2982 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2986 flags = h->mh_flags;
2987 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2989 if (h->mh_fmt != NULL) {
2990 req_capsule_set(info->mti_pill, h->mh_fmt);
2991 rc = mdt_unpack_req_pack_rep(info, flags);
2994 if (rc == 0 && flags & MUTABOR &&
2995 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2996 /* should it be rq_status? */
2999 if (rc == 0 && flags & HABEO_CLAVIS) {
3000 struct ldlm_request *dlm_req;
3002 LASSERT(h->mh_fmt != NULL);
3004 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3005 if (dlm_req != NULL) {
3006 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
3008 dlm_req->lock_desc.l_policy_data.\
3009 l_inodebits.bits == 0)) {
3011 * Lock without inodebits makes no sense and
3012 * will oops later in ldlm. If client miss to
3013 * set such bits, do not trigger ASSERTION.
3015 * For liblustre flock case, it maybe zero.
3019 if (info->mti_mdt &&
3020 info->mti_mdt->mdt_opts.mo_compat_resname)
3021 rc = mdt_lock_resname_compat(
3024 info->mti_dlm_req = dlm_req;
3031 /* capability setting changed via /proc, needs reinitialize ctxt */
3032 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
3033 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
3034 info->mti_mdt->mdt_capa_conf = 0;
3037 if (likely(rc == 0)) {
3039 * Process request, there can be two types of rc:
3040 * 1) errors with msg unpack/pack, other failures outside the
3041 * operation itself. This is counted as serious errors;
3042 * 2) errors during fs operation, should be placed in rq_status
3045 rc = h->mh_act(info);
3047 !req->rq_no_reply && req->rq_reply_state == NULL) {
3048 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3049 "pack reply and returned 0 error\n",
3053 serious = is_serious(rc);
3054 rc = clear_serious(rc);
3058 req->rq_status = rc;
3061 * ELDLM_* codes which > 0 should be in rq_status only as well as
3062 * all non-serious errors.
3064 if (rc > 0 || !serious)
3067 LASSERT(current->journal_info == NULL);
3069 if (rc == 0 && (flags & HABEO_CLAVIS) && info->mti_mdt &&
3070 info->mti_mdt->mdt_opts.mo_compat_resname) {
3071 struct ldlm_reply *dlmrep;
3073 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3075 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3078 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3079 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3080 target_committed_to_req(req);
3082 if (unlikely(req_is_replay(req) &&
3083 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3084 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3088 target_send_reply(req, rc, info->mti_fail_id);
3092 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3094 lh->mlh_type = MDT_NUL_LOCK;
3095 lh->mlh_reg_lh.cookie = 0ull;
3096 lh->mlh_reg_mode = LCK_MINMODE;
3097 lh->mlh_pdo_lh.cookie = 0ull;
3098 lh->mlh_pdo_mode = LCK_MINMODE;
3099 lh->mlh_rreg_lh.cookie = 0ull;
3100 lh->mlh_rreg_mode = LCK_MINMODE;
3103 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3105 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3106 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3110 * Initialize fields of struct mdt_thread_info. Other fields are left in
3111 * uninitialized state, because it's too expensive to zero out whole
3112 * mdt_thread_info (> 1K) on each request arrival.
3114 void mdt_thread_info_init(struct ptlrpc_request *req,
3115 struct mdt_thread_info *info)
3119 info->mti_pill = &req->rq_pill;
3122 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3123 mdt_lock_handle_init(&info->mti_lh[i]);
3125 /* mdt device: it can be NULL while CONNECT */
3126 if (req->rq_export) {
3127 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3128 info->mti_exp = req->rq_export;
3130 info->mti_mdt = NULL;
3131 info->mti_env = req->rq_svc_thread->t_env;
3132 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3133 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3134 info->mti_mos = NULL;
3136 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3137 info->mti_big_buf = LU_BUF_NULL;
3138 info->mti_body = NULL;
3139 info->mti_object = NULL;
3140 info->mti_dlm_req = NULL;
3141 info->mti_has_trans = 0;
3142 info->mti_cross_ref = 0;
3143 info->mti_opdata = 0;
3144 info->mti_big_lmm_used = 0;
3146 /* To not check for split by default. */
3147 info->mti_spec.no_create = 0;
3148 info->mti_spec.sp_rm_entry = 0;
3151 void mdt_thread_info_fini(struct mdt_thread_info *info)
3155 if (info->mti_object != NULL) {
3156 mdt_object_put(info->mti_env, info->mti_object);
3157 info->mti_object = NULL;
3160 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3161 mdt_lock_handle_fini(&info->mti_lh[i]);
3162 info->mti_env = NULL;
3163 info->mti_pill = NULL;
3164 info->mti_exp = NULL;
3166 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3167 lu_buf_free(&info->mti_big_buf);
3170 int mdt_tgt_connect(struct tgt_session_info *tsi)
3172 struct ptlrpc_request *req = tgt_ses_req(tsi);
3173 struct mdt_thread_info *mti;
3178 rc = tgt_connect(tsi);
3182 /* XXX: switch mdt_init_idmap() to use tgt_session_info */
3183 lu_env_refill((void *)tsi->tsi_env);
3184 mti = lu_context_key_get(&tsi->tsi_env->le_ctx, &mdt_thread_key);
3185 LASSERT(mti != NULL);
3187 mdt_thread_info_init(req, mti);
3188 rc = mdt_init_idmap(mti);
3189 mdt_thread_info_fini(mti);
3194 obd_disconnect(class_export_get(req->rq_export));
3198 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3199 struct obd_device *obd, int *process)
3201 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3202 case MDS_CONNECT: /* This will never get here, but for completeness. */
3203 case OST_CONNECT: /* This will never get here, but for completeness. */
3204 case MDS_DISCONNECT:
3205 case OST_DISCONNECT:
3211 case MDS_DONE_WRITING:
3212 case MDS_SYNC: /* used in unmounting */
3219 *process = target_queue_recovery_request(req, obd);
3223 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3230 * Handle recovery. Return:
3231 * +1: continue request processing;
3232 * -ve: abort immediately with the given error code;
3233 * 0: send reply with error code in req->rq_status;
3235 static int mdt_recovery(struct mdt_thread_info *info)
3237 struct ptlrpc_request *req = mdt_info_req(info);
3238 struct obd_device *obd;
3242 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3245 case SEC_CTX_INIT_CONT:
3251 rc = mdt_handle_idmap(info);
3260 if (unlikely(!class_connected_export(req->rq_export))) {
3261 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3262 lustre_msg_get_opc(req->rq_reqmsg),
3263 libcfs_id2str(req->rq_peer));
3264 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3265 * mds_A will get -ENOTCONN(especially for ping req),
3266 * which will cause that mds_A deactive timeout, then when
3267 * mds_A cleanup, the cleanup process will be suspended since
3268 * deactive timeout is not zero.
3270 req->rq_status = -ENOTCONN;
3271 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3275 /* sanity check: if the xid matches, the request must be marked as a
3276 * resent or replayed */
3277 if (req_xid_is_last(req)) {
3278 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3279 (MSG_RESENT | MSG_REPLAY))) {
3280 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3281 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3282 lustre_msg_get_flags(req->rq_reqmsg));
3284 req->rq_status = -ENOTCONN;
3289 /* else: note the opposite is not always true; a RESENT req after a
3290 * failover will usually not match the last_xid, since it was likely
3291 * never committed. A REPLAYed request will almost never match the
3292 * last xid, however it could for a committed, but still retained,
3295 obd = req->rq_export->exp_obd;
3297 /* Check for aborted recovery... */
3298 if (unlikely(obd->obd_recovering)) {
3301 DEBUG_REQ(D_INFO, req, "Got new replay");
3302 rc = mdt_filter_recovery_request(req, obd, &should_process);
3303 if (rc != 0 || !should_process)
3305 else if (should_process < 0) {
3306 req->rq_status = should_process;
3307 rc = ptlrpc_error(req);
3314 static int mdt_msg_check_version(struct lustre_msg *msg)
3318 switch (lustre_msg_get_opc(msg)) {
3320 case MDS_DISCONNECT:
3323 case SEC_CTX_INIT_CONT:
3326 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3328 CERROR("bad opc %u version %08x, expecting %08x\n",
3329 lustre_msg_get_opc(msg),
3330 lustre_msg_get_version(msg),
3331 LUSTRE_OBD_VERSION);
3335 case MDS_GETATTR_NAME:
3342 case MDS_DONE_WRITING:
3349 case MDS_HSM_PROGRESS:
3350 case MDS_HSM_REQUEST:
3351 case MDS_HSM_CT_REGISTER:
3352 case MDS_HSM_CT_UNREGISTER:
3353 case MDS_HSM_STATE_GET:
3354 case MDS_HSM_STATE_SET:
3355 case MDS_HSM_ACTION:
3356 case MDS_QUOTACHECK:
3359 case MDS_SWAP_LAYOUTS:
3364 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3366 CERROR("bad opc %u version %08x, expecting %08x\n",
3367 lustre_msg_get_opc(msg),
3368 lustre_msg_get_version(msg),
3369 LUSTRE_MDS_VERSION);
3373 case LDLM_BL_CALLBACK:
3374 case LDLM_CP_CALLBACK:
3375 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3377 CERROR("bad opc %u version %08x, expecting %08x\n",
3378 lustre_msg_get_opc(msg),
3379 lustre_msg_get_version(msg),
3380 LUSTRE_DLM_VERSION);
3382 case OBD_LOG_CANCEL:
3383 case LLOG_ORIGIN_HANDLE_CREATE:
3384 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3385 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3386 case LLOG_ORIGIN_HANDLE_CLOSE:
3387 case LLOG_ORIGIN_HANDLE_DESTROY:
3388 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3390 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3392 CERROR("bad opc %u version %08x, expecting %08x\n",
3393 lustre_msg_get_opc(msg),
3394 lustre_msg_get_version(msg),
3395 LUSTRE_LOG_VERSION);
3398 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3404 static int mdt_handle0(struct ptlrpc_request *req,
3405 struct mdt_thread_info *info,
3406 struct mdt_opc_slice *supported)
3408 struct mdt_handler *h;
3409 struct lustre_msg *msg;
3414 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3417 LASSERT(current->journal_info == NULL);
3419 msg = req->rq_reqmsg;
3420 rc = mdt_msg_check_version(msg);
3421 if (likely(rc == 0)) {
3422 rc = mdt_recovery(info);
3423 if (likely(rc == +1)) {
3424 h = mdt_handler_find(lustre_msg_get_opc(msg),
3426 if (likely(h != NULL)) {
3427 rc = mdt_req_handle(info, h, req);
3429 CERROR("%s: opc unsupported: 0x%x\n",
3430 mdt_obd_name(info->mti_mdt),
3431 lustre_msg_get_opc(msg));
3432 req->rq_status = -ENOTSUPP;
3433 rc = ptlrpc_error(req);
3438 CDEBUG(D_INFO, "%s: drops mal-formed request: rc = %d\n",
3439 mdt_obd_name(info->mti_mdt), rc);
3440 req->rq_status = rc;
3441 rc = ptlrpc_error(req);
3447 * MDT handler function called by ptlrpc service thread when request comes.
3449 * XXX common "target" functionality should be factored into separate module
3450 * shared by mdt, ost and stand-alone services like fld.
3452 int mdt_handle_common(struct ptlrpc_request *req,
3453 struct mdt_opc_slice *supported)
3456 struct mdt_thread_info *info;
3460 env = req->rq_svc_thread->t_env;
3461 LASSERT(env != NULL);
3462 /* Refill(initilize) the context(mdt_thread_info), in case it is
3463 * not initialized yet. Usually it happens during start up, after
3464 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3465 * before MDT_thread_info is initialized */
3467 LASSERT(env->le_ses != NULL);
3468 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3469 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3470 LASSERT(info != NULL);
3472 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3473 mdt_thread_info_init(req, info);
3475 rc = mdt_handle0(req, info, supported);
3477 mdt_thread_info_fini(info);
3478 req_capsule_fini(&req->rq_pill);
3483 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3486 int mdt_recovery_handle(struct ptlrpc_request *req)
3492 rc = mdt_handle_common(req, mdt_regular_handlers);
3512 static int mdt_intent_getattr(enum mdt_it_code opcode,
3513 struct mdt_thread_info *info,
3514 struct ldlm_lock **,
3517 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3518 struct mdt_thread_info *info,
3519 struct ldlm_lock **lockp,
3522 static int mdt_intent_layout(enum mdt_it_code opcode,
3523 struct mdt_thread_info *info,
3524 struct ldlm_lock **,
3526 static int mdt_intent_reint(enum mdt_it_code opcode,
3527 struct mdt_thread_info *info,
3528 struct ldlm_lock **,
3531 static struct mdt_it_flavor {
3532 const struct req_format *it_fmt;
3534 int (*it_act)(enum mdt_it_code ,
3535 struct mdt_thread_info *,
3536 struct ldlm_lock **,
3539 } mdt_it_flavor[] = {
3541 .it_fmt = &RQF_LDLM_INTENT,
3542 /*.it_flags = HABEO_REFERO,*/
3544 .it_act = mdt_intent_reint,
3545 .it_reint = REINT_OPEN
3548 .it_fmt = &RQF_LDLM_INTENT,
3550 * OCREAT is not a MUTABOR request as if the file
3552 * We do the extra check of OBD_CONNECT_RDONLY in
3553 * mdt_reint_open() when we really need to create
3557 .it_act = mdt_intent_reint,
3558 .it_reint = REINT_OPEN
3561 .it_fmt = &RQF_LDLM_INTENT,
3562 .it_flags = MUTABOR,
3563 .it_act = mdt_intent_reint,
3564 .it_reint = REINT_CREATE
3566 [MDT_IT_GETATTR] = {
3567 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3568 .it_flags = HABEO_REFERO,
3569 .it_act = mdt_intent_getattr
3571 [MDT_IT_READDIR] = {
3577 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3578 .it_flags = HABEO_REFERO,
3579 .it_act = mdt_intent_getattr
3582 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3583 .it_flags = MUTABOR,
3585 .it_reint = REINT_UNLINK
3589 .it_flags = MUTABOR,
3592 [MDT_IT_GETXATTR] = {
3593 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3594 .it_flags = HABEO_CORPUS,
3595 .it_act = mdt_intent_getxattr
3598 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3600 .it_act = mdt_intent_layout
3604 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3605 struct ldlm_lock **lockp,
3606 struct ldlm_lock *new_lock,
3607 struct mdt_lock_handle *lh,
3610 struct ptlrpc_request *req = mdt_info_req(info);
3611 struct ldlm_lock *lock = *lockp;
3614 * Get new lock only for cases when possible resent did not find any
3617 if (new_lock == NULL)
3618 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3620 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3621 lh->mlh_reg_lh.cookie = 0;
3625 LASSERTF(new_lock != NULL,
3626 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3629 * If we've already given this lock to a client once, then we should
3630 * have no readers or writers. Otherwise, we should have one reader
3631 * _or_ writer ref (which will be zeroed below) before returning the
3634 if (new_lock->l_export == req->rq_export) {
3635 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3637 LASSERT(new_lock->l_export == NULL);
3638 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3643 if (new_lock->l_export == req->rq_export) {
3645 * Already gave this to the client, which means that we
3646 * reconstructed a reply.
3648 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3650 lh->mlh_reg_lh.cookie = 0;
3651 RETURN(ELDLM_LOCK_REPLACED);
3655 * Fixup the lock to be given to the client.
3657 lock_res_and_lock(new_lock);
3658 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3659 * possible blocking AST. */
3660 while (new_lock->l_readers > 0) {
3661 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3662 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3663 new_lock->l_readers--;
3665 while (new_lock->l_writers > 0) {
3666 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3667 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3668 new_lock->l_writers--;
3671 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3672 new_lock->l_blocking_ast = lock->l_blocking_ast;
3673 new_lock->l_completion_ast = lock->l_completion_ast;
3674 new_lock->l_remote_handle = lock->l_remote_handle;
3675 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3677 unlock_res_and_lock(new_lock);
3679 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3680 &new_lock->l_remote_handle,
3681 &new_lock->l_exp_hash);
3683 LDLM_LOCK_RELEASE(new_lock);
3684 lh->mlh_reg_lh.cookie = 0;
3686 RETURN(ELDLM_LOCK_REPLACED);
3689 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3690 struct ldlm_lock *new_lock,
3691 struct ldlm_lock **old_lock,
3692 struct mdt_lock_handle *lh)
3694 struct ptlrpc_request *req = mdt_info_req(info);
3695 struct obd_export *exp = req->rq_export;
3696 struct lustre_handle remote_hdl;
3697 struct ldlm_request *dlmreq;
3698 struct ldlm_lock *lock;
3700 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3703 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3704 remote_hdl = dlmreq->lock_handle[0];
3706 /* In the function below, .hs_keycmp resolves to
3707 * ldlm_export_lock_keycmp() */
3708 /* coverity[overrun-buffer-val] */
3709 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3711 if (lock != new_lock) {
3712 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3713 lh->mlh_reg_mode = lock->l_granted_mode;
3715 LDLM_DEBUG(lock, "Restoring lock cookie");
3716 DEBUG_REQ(D_DLMTRACE, req,
3717 "restoring lock cookie "LPX64,
3718 lh->mlh_reg_lh.cookie);
3720 *old_lock = LDLM_LOCK_GET(lock);
3721 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3725 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3729 * If the xid matches, then we know this is a resent request, and allow
3730 * it. (It's probably an OPEN, for which we don't send a lock.
3732 if (req_xid_is_last(req))
3736 * This remote handle isn't enqueued, so we never received or processed
3737 * this request. Clear MSG_RESENT, because it can be handled like any
3738 * normal request now.
3740 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3742 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3746 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3747 struct mdt_thread_info *info,
3748 struct ldlm_lock **lockp,
3751 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3752 struct ldlm_reply *ldlm_rep = NULL;
3756 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3757 * (for the resend case) or a new lock. Below we will use it to
3758 * replace the original lock.
3760 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3761 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3762 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3763 rc = mdt_object_lock(info, info->mti_object, lhc,
3764 MDS_INODELOCK_XATTR,
3770 grc = mdt_getxattr(info);
3772 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3774 if (mdt_info_req(info)->rq_repmsg != NULL)
3775 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3776 if (ldlm_rep == NULL)
3777 RETURN(err_serious(-EFAULT));
3779 ldlm_rep->lock_policy_res2 = grc;
3784 static int mdt_intent_getattr(enum mdt_it_code opcode,
3785 struct mdt_thread_info *info,
3786 struct ldlm_lock **lockp,
3789 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3790 struct ldlm_lock *new_lock = NULL;
3792 struct ldlm_reply *ldlm_rep;
3793 struct ptlrpc_request *req;
3794 struct mdt_body *reqbody;
3795 struct mdt_body *repbody;
3799 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3802 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3805 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3806 repbody->eadatasize = 0;
3807 repbody->aclsize = 0;
3811 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3813 case MDT_IT_GETATTR:
3814 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3818 CERROR("Unsupported intent (%d)\n", opcode);
3819 GOTO(out_shrink, rc = -EINVAL);
3822 rc = mdt_init_ucred(info, reqbody);
3824 GOTO(out_shrink, rc);
3826 req = info->mti_pill->rc_req;
3827 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3828 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3830 /* Get lock from request for possible resent case. */
3831 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3833 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3834 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3836 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3837 ldlm_rep->lock_policy_res2 = 0;
3838 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3839 ldlm_rep->lock_policy_res2) {
3840 lhc->mlh_reg_lh.cookie = 0ull;
3841 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3844 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3847 mdt_exit_ucred(info);
3849 mdt_client_compatibility(info);
3850 rc2 = mdt_fix_reply(info);
3856 static int mdt_intent_layout(enum mdt_it_code opcode,
3857 struct mdt_thread_info *info,
3858 struct ldlm_lock **lockp,
3861 struct layout_intent *layout;
3863 struct mdt_object *obj = NULL;
3864 struct md_object *child = NULL;
3868 if (opcode != MDT_IT_LAYOUT) {
3869 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3874 fid = &info->mti_tmp_fid2;
3875 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3877 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3879 RETURN(PTR_ERR(obj));
3881 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3882 child = mdt_object_child(obj);
3884 /* get the length of lsm */
3885 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3888 if (rc > info->mti_mdt->mdt_max_mdsize)
3889 info->mti_mdt->mdt_max_mdsize = rc;
3892 mdt_object_put(info->mti_env, obj);
3894 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3895 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3896 ldlm_lvbo_size(*lockp));
3897 rc = req_capsule_server_pack(info->mti_pill);
3901 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3902 LASSERT(layout != NULL);
3903 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3904 /* return to normal ldlm handling */
3907 CERROR("%s: Unsupported layout intent (%d)\n",
3908 mdt_obd_name(info->mti_mdt), layout->li_opc);
3912 static int mdt_intent_reint(enum mdt_it_code opcode,
3913 struct mdt_thread_info *info,
3914 struct ldlm_lock **lockp,
3917 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3918 struct ldlm_reply *rep = NULL;
3922 static const struct req_format *intent_fmts[REINT_MAX] = {
3923 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3924 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3929 opc = mdt_reint_opcode(info, intent_fmts);
3933 if (mdt_it_flavor[opcode].it_reint != opc) {
3934 CERROR("Reint code %ld doesn't match intent: %d\n",
3936 RETURN(err_serious(-EPROTO));
3939 /* Get lock from request for possible resent case. */
3940 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3942 rc = mdt_reint_internal(info, lhc, opc);
3944 /* Check whether the reply has been packed successfully. */
3945 if (mdt_info_req(info)->rq_repmsg != NULL)
3946 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3948 RETURN(err_serious(-EFAULT));
3950 /* MDC expects this in any case */
3952 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3954 /* the open lock or the lock for cross-ref object should be
3955 * returned to the client */
3956 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3957 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3958 rep->lock_policy_res2 = 0;
3959 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3963 rep->lock_policy_res2 = clear_serious(rc);
3965 if (rep->lock_policy_res2 == -ENOENT &&
3966 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3967 rep->lock_policy_res2 = 0;
3969 if (rc == -ENOTCONN || rc == -ENODEV ||
3970 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3972 * If it is the disconnect error (ENODEV & ENOCONN), the error
3973 * will be returned by rq_status, and client at ptlrpc layer
3974 * will detect this, then disconnect, reconnect the import
3975 * immediately, instead of impacting the following the rpc.
3977 lhc->mlh_reg_lh.cookie = 0ull;
3981 * For other cases, the error will be returned by intent.
3982 * and client will retrieve the result from intent.
3985 * FIXME: when open lock is finished, that should be
3988 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3989 LASSERTF(rc == 0, "Error occurred but lock handle "
3990 "is still in use, rc = %d\n", rc);
3991 rep->lock_policy_res2 = 0;
3992 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3995 lhc->mlh_reg_lh.cookie = 0ull;
3996 RETURN(ELDLM_LOCK_ABORTED);
4001 static int mdt_intent_code(long itcode)
4009 case IT_OPEN|IT_CREAT:
4016 rc = MDT_IT_READDIR;
4019 rc = MDT_IT_GETATTR;
4031 rc = MDT_IT_GETXATTR;
4036 case IT_QUOTA_DQACQ:
4041 CERROR("Unknown intent opcode: %ld\n", itcode);
4048 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
4049 struct ldlm_lock **lockp, __u64 flags)
4051 struct req_capsule *pill;
4052 struct mdt_it_flavor *flv;
4057 opc = mdt_intent_code(itopc);
4061 pill = info->mti_pill;
4063 if (opc == MDT_IT_QUOTA) {
4064 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4067 RETURN(-EOPNOTSUPP);
4069 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4070 /* pass the request to quota master */
4071 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4072 mdt_info_req(info), lockp,
4077 flv = &mdt_it_flavor[opc];
4078 if (flv->it_fmt != NULL)
4079 req_capsule_extend(pill, flv->it_fmt);
4081 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
4083 struct ptlrpc_request *req = mdt_info_req(info);
4084 if (flv->it_flags & MUTABOR &&
4085 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
4088 if (rc == 0 && flv->it_act != NULL) {
4089 struct ldlm_reply *rep;
4091 /* execute policy */
4092 rc = flv->it_act(opc, info, lockp, flags);
4094 /* Check whether the reply has been packed successfully. */
4095 if (mdt_info_req(info)->rq_repmsg != NULL) {
4096 rep = req_capsule_server_get(info->mti_pill,
4098 rep->lock_policy_res2 =
4099 ptlrpc_status_hton(rep->lock_policy_res2);
4107 static int mdt_intent_policy(struct ldlm_namespace *ns,
4108 struct ldlm_lock **lockp, void *req_cookie,
4109 ldlm_mode_t mode, __u64 flags, void *data)
4111 struct mdt_thread_info *info;
4112 struct ptlrpc_request *req = req_cookie;
4113 struct ldlm_intent *it;
4114 struct req_capsule *pill;
4119 LASSERT(req != NULL);
4121 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
4123 LASSERT(info != NULL);
4124 pill = info->mti_pill;
4125 LASSERT(pill->rc_req == req);
4127 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4128 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4129 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4131 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4135 /* Lock without inodebits makes no sense and will oops
4136 * later in ldlm. Let's check it now to see if we have
4137 * ibits corrupted somewhere in mdt_intent_opc().
4138 * The case for client miss to set ibits has been
4139 * processed by others. */
4140 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
4141 lr_type == LDLM_IBITS,
4142 info->mti_dlm_req->lock_desc.\
4143 l_policy_data.l_inodebits.bits != 0));
4145 rc = err_serious(-EFAULT);
4147 /* No intent was provided */
4148 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4149 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4150 rc = req_capsule_server_pack(pill);
4152 rc = err_serious(rc);
4157 static int mdt_seq_fini(const struct lu_env *env,
4158 struct mdt_device *m)
4160 return seq_site_fini(env, mdt_seq_site(m));
4163 static int mdt_seq_init(const struct lu_env *env,
4165 struct mdt_device *m)
4167 struct seq_server_site *ss;
4172 ss = mdt_seq_site(m);
4175 * This is sequence-controller node. Init seq-controller server on local
4178 if (ss->ss_node_id == 0) {
4179 LASSERT(ss->ss_control_seq == NULL);
4181 OBD_ALLOC_PTR(ss->ss_control_seq);
4182 if (ss->ss_control_seq == NULL)
4185 rc = seq_server_init(ss->ss_control_seq,
4186 m->mdt_bottom, uuid,
4187 LUSTRE_SEQ_CONTROLLER,
4192 GOTO(out_seq_fini, rc);
4194 OBD_ALLOC_PTR(ss->ss_client_seq);
4195 if (ss->ss_client_seq == NULL)
4196 GOTO(out_seq_fini, rc = -ENOMEM);
4198 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4199 if (prefix == NULL) {
4200 OBD_FREE_PTR(ss->ss_client_seq);
4201 GOTO(out_seq_fini, rc = -ENOMEM);
4204 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4208 * Init seq-controller client after seq-controller server is
4209 * ready. Pass ss->ss_control_seq to it for direct talking.
4211 rc = seq_client_init(ss->ss_client_seq, NULL,
4212 LUSTRE_SEQ_METADATA, prefix,
4213 ss->ss_control_seq);
4214 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4217 GOTO(out_seq_fini, rc);
4220 /* Init seq-server on local MDT */
4221 LASSERT(ss->ss_server_seq == NULL);
4223 OBD_ALLOC_PTR(ss->ss_server_seq);
4224 if (ss->ss_server_seq == NULL)
4225 GOTO(out_seq_fini, rc = -ENOMEM);
4227 rc = seq_server_init(ss->ss_server_seq,
4228 m->mdt_bottom, uuid,
4233 GOTO(out_seq_fini, rc = -ENOMEM);
4235 /* Assign seq-controller client to local seq-server. */
4236 if (ss->ss_node_id == 0) {
4237 LASSERT(ss->ss_client_seq != NULL);
4239 rc = seq_server_set_cli(ss->ss_server_seq,
4247 mdt_seq_fini(env, m);
4255 static int mdt_fld_fini(const struct lu_env *env,
4256 struct mdt_device *m)
4258 struct seq_server_site *ss = mdt_seq_site(m);
4261 if (ss && ss->ss_server_fld) {
4262 fld_server_fini(env, ss->ss_server_fld);
4263 OBD_FREE_PTR(ss->ss_server_fld);
4264 ss->ss_server_fld = NULL;
4270 static int mdt_fld_init(const struct lu_env *env,
4272 struct mdt_device *m)
4274 struct seq_server_site *ss;
4278 ss = mdt_seq_site(m);
4280 OBD_ALLOC_PTR(ss->ss_server_fld);
4281 if (ss->ss_server_fld == NULL)
4282 RETURN(rc = -ENOMEM);
4284 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4285 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4287 OBD_FREE_PTR(ss->ss_server_fld);
4288 ss->ss_server_fld = NULL;
4295 static void mdt_stack_pre_fini(const struct lu_env *env,
4296 struct mdt_device *m, struct lu_device *top)
4298 struct obd_device *obd;
4299 struct lustre_cfg_bufs *bufs;
4300 struct lustre_cfg *lcfg;
4301 struct mdt_thread_info *info;
4306 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4307 LASSERT(info != NULL);
4309 bufs = &info->mti_u.bufs;
4311 LASSERT(m->mdt_child_exp);
4312 LASSERT(m->mdt_child_exp->exp_obd);
4313 obd = m->mdt_child_exp->exp_obd;
4315 /* process cleanup, pass mdt obd name to get obd umount flags */
4316 /* XXX: this is needed because all layers are referenced by
4317 * objects (some of them are pinned by osd, for example *
4318 * the proper solution should be a model where object used
4319 * by osd only doesn't have mdt/mdd slices -bzzz */
4320 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4321 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4322 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4324 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4327 top->ld_ops->ldo_process_config(env, top, lcfg);
4328 lustre_cfg_free(lcfg);
4332 static void mdt_stack_fini(const struct lu_env *env,
4333 struct mdt_device *m, struct lu_device *top)
4335 struct obd_device *obd = mdt2obd_dev(m);
4336 struct lustre_cfg_bufs *bufs;
4337 struct lustre_cfg *lcfg;
4338 struct mdt_thread_info *info;
4342 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4343 LASSERT(info != NULL);
4345 lu_dev_del_linkage(top->ld_site, top);
4347 lu_site_purge(env, top->ld_site, -1);
4349 bufs = &info->mti_u.bufs;
4350 /* process cleanup, pass mdt obd name to get obd umount flags */
4351 /* another purpose is to let all layers to release their objects */
4352 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4357 lustre_cfg_bufs_set_string(bufs, 1, flags);
4358 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4360 CERROR("Cannot alloc lcfg!\n");
4364 top->ld_ops->ldo_process_config(env, top, lcfg);
4365 lustre_cfg_free(lcfg);
4367 lu_site_purge(env, top->ld_site, -1);
4369 m->mdt_child = NULL;
4370 m->mdt_bottom = NULL;
4372 obd_disconnect(m->mdt_child_exp);
4373 m->mdt_child_exp = NULL;
4375 obd_disconnect(m->mdt_bottom_exp);
4376 m->mdt_child_exp = NULL;
4379 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4380 const char *next, struct obd_export **exp)
4382 struct obd_connect_data *data = NULL;
4383 struct obd_device *obd;
4387 OBD_ALLOC_PTR(data);
4389 GOTO(out, rc = -ENOMEM);
4391 obd = class_name2obd(next);
4393 CERROR("%s: can't locate next device: %s\n",
4394 mdt_obd_name(m), next);
4395 GOTO(out, rc = -ENOTCONN);
4398 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4399 data->ocd_version = LUSTRE_VERSION_CODE;
4401 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4403 CERROR("%s: cannot connect to next dev %s (%d)\n",
4404 mdt_obd_name(m), next, rc);
4414 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4415 struct lustre_cfg *cfg)
4417 char *dev = lustre_cfg_string(cfg, 0);
4418 int rc, name_size, uuid_size;
4419 char *name, *uuid, *p;
4420 struct lustre_cfg_bufs *bufs;
4421 struct lustre_cfg *lcfg;
4422 struct obd_device *obd;
4423 struct lustre_profile *lprof;
4424 struct lu_site *site;
4427 /* in 1.8 we had the only device in the stack - MDS.
4428 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4429 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4430 * to generate names and setup MDT, MDD. MDT will be using
4431 * generated name to connect to MDD. for MDD the next device
4432 * will be LOD with name taken from so called "profile" which
4433 * is generated by mount_option line
4435 * 1.8 MGS generates config. commands like this:
4436 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4437 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4438 * 2.0 MGS generates config. commands like this:
4439 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4440 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4441 * 3:lustre-MDT0000-mdtlov 4:f
4443 * we generate MDD name from MDT one, just replacing T with D
4445 * after all the preparations, the logical equivalent will be
4446 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4447 * 3:lustre-MDT0000-mdtlov 4:f
4448 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4449 * 3:lustre-MDD0000 4:f
4451 * notice we build the stack from down to top: MDD first, then MDT */
4453 name_size = MAX_OBD_NAME;
4454 uuid_size = MAX_OBD_NAME;
4456 OBD_ALLOC(name, name_size);
4457 OBD_ALLOC(uuid, uuid_size);
4458 if (name == NULL || uuid == NULL)
4459 GOTO(cleanup_mem, rc = -ENOMEM);
4461 OBD_ALLOC_PTR(bufs);
4463 GOTO(cleanup_mem, rc = -ENOMEM);
4466 p = strstr(name, "-MDT");
4468 GOTO(cleanup_mem, rc = -ENOMEM);
4471 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4473 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4474 if (lprof == NULL || lprof->lp_dt == NULL) {
4475 CERROR("can't find the profile: %s\n",
4476 lustre_cfg_string(cfg, 0));
4477 GOTO(cleanup_mem, rc = -EINVAL);
4480 lustre_cfg_bufs_reset(bufs, name);
4481 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4482 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4483 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4485 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4487 GOTO(free_bufs, rc = -ENOMEM);
4489 rc = class_attach(lcfg);
4491 GOTO(lcfg_cleanup, rc);
4493 obd = class_name2obd(name);
4495 CERROR("Can not find obd %s (%s in config)\n",
4496 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4497 GOTO(class_detach, rc = -EINVAL);
4500 lustre_cfg_free(lcfg);
4502 lustre_cfg_bufs_reset(bufs, name);
4503 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4504 lustre_cfg_bufs_set_string(bufs, 2, dev);
4505 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4507 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4509 rc = class_setup(obd, lcfg);
4511 GOTO(class_detach, rc);
4513 /* connect to MDD we just setup */
4514 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4518 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4520 LASSERT(mdt_lu_site(mdt) == NULL);
4521 mdt->mdt_lu_dev.ld_site = site;
4522 site->ls_top_dev = &mdt->mdt_lu_dev;
4523 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4526 /* now connect to bottom OSD */
4527 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4528 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4532 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4535 rc = lu_env_refill((struct lu_env *)env);
4537 CERROR("Failure to refill session: '%d'\n", rc);
4539 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4544 class_detach(obd, lcfg);
4546 lustre_cfg_free(lcfg);
4551 OBD_FREE(name, name_size);
4553 OBD_FREE(uuid, uuid_size);
4557 /* setup quota master target on MDT0 */
4558 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4559 struct lustre_cfg *cfg)
4561 struct obd_device *obd;
4562 char *dev = lustre_cfg_string(cfg, 0);
4563 char *qmtname, *uuid, *p;
4564 struct lustre_cfg_bufs *bufs;
4565 struct lustre_cfg *lcfg;
4566 struct lustre_profile *lprof;
4567 struct obd_connect_data *data;
4571 LASSERT(mdt->mdt_qmt_exp == NULL);
4572 LASSERT(mdt->mdt_qmt_dev == NULL);
4574 /* quota master is on MDT0 only for now */
4575 if (mdt->mdt_seq_site.ss_node_id != 0)
4578 /* MGS generates config commands which look as follows:
4579 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4580 * 3:lustre-MDT0000-mdtlov 4:f
4582 * We generate the QMT name from the MDT one, just replacing MD with QM
4583 * after all the preparations, the logical equivalent will be:
4584 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4585 * 3:lustre-MDT0000-osd 4:f */
4586 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4587 OBD_ALLOC(uuid, UUID_MAX);
4588 OBD_ALLOC_PTR(bufs);
4589 OBD_ALLOC_PTR(data);
4590 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4591 GOTO(cleanup_mem, rc = -ENOMEM);
4593 strcpy(qmtname, dev);
4594 p = strstr(qmtname, "-MDT");
4596 GOTO(cleanup_mem, rc = -ENOMEM);
4597 /* replace MD with QM */
4601 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4603 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4604 if (lprof == NULL || lprof->lp_dt == NULL) {
4605 CERROR("can't find profile for %s\n",
4606 lustre_cfg_string(cfg, 0));
4607 GOTO(cleanup_mem, rc = -EINVAL);
4610 lustre_cfg_bufs_reset(bufs, qmtname);
4611 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4612 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4613 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4615 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4617 GOTO(cleanup_mem, rc = -ENOMEM);
4619 rc = class_attach(lcfg);
4621 GOTO(lcfg_cleanup, rc);
4623 obd = class_name2obd(qmtname);
4625 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4626 lustre_cfg_string(cfg, 0));
4627 GOTO(class_detach, rc = -EINVAL);
4630 lustre_cfg_free(lcfg);
4632 lustre_cfg_bufs_reset(bufs, qmtname);
4633 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4634 lustre_cfg_bufs_set_string(bufs, 2, dev);
4636 /* for quota, the next device should be the OSD device */
4637 lustre_cfg_bufs_set_string(bufs, 3,
4638 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4640 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4642 rc = class_setup(obd, lcfg);
4644 GOTO(class_detach, rc);
4646 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4648 /* configure local quota objects */
4649 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4653 GOTO(class_cleanup, rc);
4655 /* connect to quota master target */
4656 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4657 data->ocd_version = LUSTRE_VERSION_CODE;
4658 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4661 CERROR("cannot connect to quota master device %s (%d)\n",
4663 GOTO(class_cleanup, rc);
4669 class_manual_cleanup(obd);
4670 mdt->mdt_qmt_dev = NULL;
4674 class_detach(obd, lcfg);
4676 lustre_cfg_free(lcfg);
4681 OBD_FREE(qmtname, MAX_OBD_NAME);
4683 OBD_FREE(uuid, UUID_MAX);
4689 /* Shutdown quota master target associated with mdt */
4690 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4694 if (mdt->mdt_qmt_exp == NULL)
4696 LASSERT(mdt->mdt_qmt_dev != NULL);
4698 /* the qmt automatically shuts down when the mdt disconnects */
4699 obd_disconnect(mdt->mdt_qmt_exp);
4700 mdt->mdt_qmt_exp = NULL;
4701 mdt->mdt_qmt_dev = NULL;
4705 static struct tgt_handler mdt_tgt_handlers[] = {
4706 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4707 0, MDS_CONNECT, mdt_tgt_connect,
4708 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4709 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4710 0, MDS_DISCONNECT, tgt_disconnect,
4711 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4714 static struct tgt_opc_slice mdt_common_slice[] = {
4716 .tos_opc_start = MDS_FIRST_OPC,
4717 .tos_opc_end = MDS_LAST_OPC,
4718 .tos_hs = mdt_tgt_handlers
4721 .tos_opc_start = OBD_FIRST_OPC,
4722 .tos_opc_end = OBD_LAST_OPC,
4723 .tos_hs = tgt_obd_handlers
4726 .tos_opc_start = LDLM_FIRST_OPC,
4727 .tos_opc_end = LDLM_LAST_OPC,
4728 .tos_hs = tgt_dlm_handlers
4731 .tos_opc_start = SEC_FIRST_OPC,
4732 .tos_opc_end = SEC_LAST_OPC,
4733 .tos_hs = tgt_sec_ctx_handlers
4736 .tos_opc_start = UPDATE_OBJ,
4737 .tos_opc_end = UPDATE_LAST_OPC,
4738 .tos_hs = tgt_out_handlers
4741 .tos_opc_start = FLD_FIRST_OPC,
4742 .tos_opc_end = FLD_LAST_OPC,
4743 .tos_hs = fld_handlers
4746 .tos_opc_start = SEQ_FIRST_OPC,
4747 .tos_opc_end = SEQ_LAST_OPC,
4748 .tos_hs = seq_handlers
4755 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4757 struct md_device *next = m->mdt_child;
4758 struct lu_device *d = &m->mdt_lu_dev;
4759 struct obd_device *obd = mdt2obd_dev(m);
4762 target_recovery_fini(obd);
4764 ping_evictor_stop();
4766 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4768 if (m->mdt_opts.mo_coordinator)
4769 mdt_hsm_cdt_stop(m);
4771 mdt_hsm_cdt_fini(m);
4773 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4774 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4775 obd_exports_barrier(obd);
4776 obd_zombie_barrier();
4780 tgt_fini(env, &m->mdt_lut);
4781 mdt_fs_cleanup(env, m);
4782 upcall_cache_cleanup(m->mdt_identity_cache);
4783 m->mdt_identity_cache = NULL;
4785 if (m->mdt_namespace != NULL) {
4786 ldlm_namespace_free(m->mdt_namespace, NULL,
4787 d->ld_obd->obd_force);
4788 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4791 mdt_quota_fini(env, m);
4793 cfs_free_nidlist(&m->mdt_nosquash_nids);
4794 if (m->mdt_nosquash_str) {
4795 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4796 m->mdt_nosquash_str = NULL;
4797 m->mdt_nosquash_strlen = 0;
4800 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4802 mdt_seq_fini(env, m);
4803 mdt_fld_fini(env, m);
4804 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4806 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4807 cfs_timer_disarm(&m->mdt_ck_timer);
4808 mdt_ck_thread_stop(m);
4813 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4815 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4817 server_put_mount(mdt_obd_name(m), NULL);
4822 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4824 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4825 struct sptlrpc_rule_set tmp_rset;
4828 sptlrpc_rule_set_init(&tmp_rset);
4829 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4831 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4832 mdt_obd_name(m), rc);
4836 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4838 write_lock(&m->mdt_sptlrpc_lock);
4839 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4840 m->mdt_sptlrpc_rset = tmp_rset;
4841 write_unlock(&m->mdt_sptlrpc_lock);
4846 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4848 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4849 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4851 struct mdt_thread_info *info;
4852 struct obd_device *obd;
4853 const char *dev = lustre_cfg_string(cfg, 0);
4854 const char *num = lustre_cfg_string(cfg, 2);
4855 struct lustre_mount_info *lmi = NULL;
4856 struct lustre_sb_info *lsi;
4858 struct seq_server_site *ss_site;
4859 const char *identity_upcall = "NONE";
4860 struct md_device *next;
4866 lu_device_init(&m->mdt_lu_dev, ldt);
4868 * Environment (env) might be missing mdt_thread_key values at that
4869 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4872 * Usually device allocation path doesn't use module key values, but
4873 * mdt has to do a lot of work here, so allocate key value.
4875 rc = lu_env_refill((struct lu_env *)env);
4879 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4880 LASSERT(info != NULL);
4882 obd = class_name2obd(dev);
4883 LASSERT(obd != NULL);
4885 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4887 m->mdt_som_conf = 0;
4889 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4891 /* default is coordinator off, it is started through conf_param
4893 m->mdt_opts.mo_coordinator = 0;
4895 lmi = server_get_mount(dev);
4897 CERROR("Cannot get mount info for %s!\n", dev);
4900 lsi = s2lsi(lmi->lmi_sb);
4901 /* CMD is supported only in IAM mode */
4903 node_id = simple_strtol(num, NULL, 10);
4904 obd->u.obt.obt_magic = OBT_MAGIC;
4907 rwlock_init(&m->mdt_sptlrpc_lock);
4908 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4910 spin_lock_init(&m->mdt_ioepoch_lock);
4911 m->mdt_opts.mo_compat_resname = 0;
4912 m->mdt_opts.mo_mds_capa = 1;
4913 m->mdt_opts.mo_oss_capa = 1;
4914 m->mdt_capa_timeout = CAPA_TIMEOUT;
4915 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4916 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4917 m->mdt_squash_uid = 0;
4918 m->mdt_squash_gid = 0;
4919 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4920 m->mdt_nosquash_str = NULL;
4921 m->mdt_nosquash_strlen = 0;
4922 init_rwsem(&m->mdt_squash_sem);
4923 spin_lock_init(&m->mdt_osfs_lock);
4924 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4925 m->mdt_enable_remote_dir = 0;
4926 m->mdt_enable_remote_dir_gid = 0;
4928 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4929 m->mdt_lu_dev.ld_obd = obd;
4930 /* Set this lu_device to obd for error handling purposes. */
4931 obd->obd_lu_dev = &m->mdt_lu_dev;
4933 /* init the stack */
4934 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4936 CERROR("%s: Can't init device stack, rc %d\n",
4937 mdt_obd_name(m), rc);
4942 ss_site = mdt_seq_site(m);
4943 s->ld_seq_site = ss_site;
4946 /* set server index */
4947 ss_site->ss_node_id = node_id;
4949 /* failover is the default
4950 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4951 * assumed whose ss_node_id == 0 XXX
4953 obd->obd_replayable = 1;
4954 /* No connection accepted until configurations will finish */
4955 obd->obd_no_conn = 1;
4957 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4958 char *str = lustre_cfg_string(cfg, 4);
4959 if (strchr(str, 'n')) {
4960 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4961 obd->obd_replayable = 0;
4965 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4967 GOTO(err_fini_stack, rc);
4969 rc = mdt_seq_init(env, mdt_obd_name(m), m);
4971 GOTO(err_fini_fld, rc);
4973 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4974 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4975 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4976 LDLM_NAMESPACE_SERVER,
4977 LDLM_NAMESPACE_GREEDY,
4979 if (m->mdt_namespace == NULL)
4980 GOTO(err_fini_seq, rc = -ENOMEM);
4982 m->mdt_namespace->ns_lvbp = m;
4983 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4985 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4986 /* set obd_namespace for compatibility with old code */
4987 obd->obd_namespace = m->mdt_namespace;
4989 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4991 rc = mdt_hsm_cdt_init(m);
4993 CERROR("%s: error initializing coordinator, rc %d\n",
4994 mdt_obd_name(m), rc);
4995 GOTO(err_free_ns, rc);
4998 rc = mdt_ck_thread_start(m);
5000 GOTO(err_free_hsm, rc);
5002 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5003 OBD_FAIL_MDS_ALL_REQUEST_NET,
5004 OBD_FAIL_MDS_ALL_REPLY_NET);
5008 rc = mdt_fs_setup(env, m, obd, lsi);
5012 mdt_adapt_sptlrpc_conf(obd, 1);
5014 next = m->mdt_child;
5015 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5018 GOTO(err_fs_cleanup, rc);
5020 if (mntopts & MNTOPT_USERXATTR)
5021 m->mdt_opts.mo_user_xattr = 1;
5023 m->mdt_opts.mo_user_xattr = 0;
5025 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
5027 GOTO(err_fs_cleanup, rc);
5029 if (mntopts & MNTOPT_ACL)
5030 m->mdt_opts.mo_acl = 1;
5032 m->mdt_opts.mo_acl = 0;
5034 /* XXX: to support suppgid for ACL, we enable identity_upcall
5035 * by default, otherwise, maybe got unexpected -EACCESS. */
5036 if (m->mdt_opts.mo_acl)
5037 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5039 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5041 &mdt_identity_upcall_cache_ops);
5042 if (IS_ERR(m->mdt_identity_cache)) {
5043 rc = PTR_ERR(m->mdt_identity_cache);
5044 m->mdt_identity_cache = NULL;
5045 GOTO(err_fs_cleanup, rc);
5048 rc = mdt_procfs_init(m, dev);
5050 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5051 GOTO(err_recovery, rc);
5054 rc = mdt_quota_init(env, m, cfg);
5056 GOTO(err_procfs, rc);
5058 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5059 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5060 "mdt_ldlm_client", m->mdt_ldlm_client);
5062 ping_evictor_start();
5064 /* recovery will be started upon mdt_prepare()
5065 * when the whole stack is complete and ready
5066 * to serve the requests */
5068 mdt_init_capa_ctxt(env, m);
5070 /* Reduce the initial timeout on an MDS because it doesn't need such
5071 * a long timeout as an OST does. Adaptive timeouts will adjust this
5072 * value appropriately. */
5073 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5074 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5080 target_recovery_fini(obd);
5081 upcall_cache_cleanup(m->mdt_identity_cache);
5082 m->mdt_identity_cache = NULL;
5084 mdt_fs_cleanup(env, m);
5086 tgt_fini(env, &m->mdt_lut);
5088 cfs_timer_disarm(&m->mdt_ck_timer);
5089 mdt_ck_thread_stop(m);
5091 mdt_hsm_cdt_fini(m);
5093 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5094 obd->obd_namespace = m->mdt_namespace = NULL;
5096 mdt_seq_fini(env, m);
5098 mdt_fld_fini(env, m);
5100 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5103 server_put_mount(dev, lmi->lmi_mnt);
5107 /* For interoperability, the left element is old parameter, the right one
5108 * is the new version of the parameter, if some parameter is deprecated,
5109 * the new version should be set as NULL. */
5110 static struct cfg_interop_param mdt_interop_param[] = {
5111 { "mdt.group_upcall", NULL },
5112 { "mdt.quota_type", NULL },
5113 { "mdd.quota_type", NULL },
5114 { "mdt.rootsquash", "mdt.root_squash" },
5115 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5119 /* used by MGS to process specific configurations */
5120 static int mdt_process_config(const struct lu_env *env,
5121 struct lu_device *d, struct lustre_cfg *cfg)
5123 struct mdt_device *m = mdt_dev(d);
5124 struct md_device *md_next = m->mdt_child;
5125 struct lu_device *next = md2lu_dev(md_next);
5129 switch (cfg->lcfg_command) {
5131 struct lprocfs_static_vars lvars;
5132 struct obd_device *obd = d->ld_obd;
5134 /* For interoperability */
5135 struct cfg_interop_param *ptr = NULL;
5136 struct lustre_cfg *old_cfg = NULL;
5139 param = lustre_cfg_string(cfg, 1);
5140 if (param == NULL) {
5141 CERROR("param is empty\n");
5146 ptr = class_find_old_param(param, mdt_interop_param);
5148 if (ptr->new_param == NULL) {
5150 CWARN("For interoperability, skip this %s."
5151 " It is obsolete.\n", ptr->old_param);
5155 CWARN("Found old param %s, changed it to %s.\n",
5156 ptr->old_param, ptr->new_param);
5159 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5166 lprocfs_mdt_init_vars(&lvars);
5167 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5169 if (rc > 0 || rc == -ENOSYS)
5170 /* we don't understand; pass it on */
5171 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5173 if (old_cfg != NULL)
5174 lustre_cfg_free(cfg);
5179 /* others are passed further */
5180 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5186 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5187 const struct lu_object_header *hdr,
5188 struct lu_device *d)
5190 struct mdt_object *mo;
5194 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
5196 struct lu_object *o;
5197 struct lu_object_header *h;
5200 h = &mo->mot_header;
5201 lu_object_header_init(h);
5202 lu_object_init(o, h, d);
5203 lu_object_add_top(h, o);
5204 o->lo_ops = &mdt_obj_ops;
5205 mutex_init(&mo->mot_ioepoch_mutex);
5206 mutex_init(&mo->mot_lov_mutex);
5207 init_rwsem(&mo->mot_open_sem);
5213 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5214 const struct lu_object_conf *unused)
5216 struct mdt_device *d = mdt_dev(o->lo_dev);
5217 struct lu_device *under;
5218 struct lu_object *below;
5222 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5223 PFID(lu_object_fid(o)));
5225 under = &d->mdt_child->md_lu_dev;
5226 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5227 if (below != NULL) {
5228 lu_object_add(o, below);
5235 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5237 struct mdt_object *mo = mdt_obj(o);
5238 struct lu_object_header *h;
5242 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5243 PFID(lu_object_fid(o)));
5245 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5246 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5249 lu_object_header_fini(h);
5250 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5255 static int mdt_object_print(const struct lu_env *env, void *cookie,
5256 lu_printer_t p, const struct lu_object *o)
5258 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5259 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5260 "flags="LPX64", epochcount=%d, writecount=%d)",
5261 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5262 mdto->mot_ioepoch_count, mdto->mot_writecount);
5265 static int mdt_prepare(const struct lu_env *env,
5266 struct lu_device *pdev,
5267 struct lu_device *cdev)
5269 struct mdt_device *mdt = mdt_dev(cdev);
5270 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5271 struct obd_device *obd = cdev->ld_obd;
5272 struct lfsck_start_param lsp;
5279 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5283 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5287 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5291 lsp.lsp_start = NULL;
5292 lsp.lsp_namespace = mdt->mdt_namespace;
5293 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5294 OBD_IOC_START_LFSCK,
5297 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5298 mdt_obd_name(mdt), rc);
5302 if (mdt->mdt_seq_site.ss_node_id == 0) {
5303 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5304 &mdt->mdt_md_root_fid);
5309 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5310 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5311 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5312 LASSERT(obd->obd_no_conn);
5313 spin_lock(&obd->obd_dev_lock);
5314 obd->obd_no_conn = 0;
5315 spin_unlock(&obd->obd_dev_lock);
5317 if (obd->obd_recovering == 0)
5318 mdt_postrecov(env, mdt);
5323 const struct lu_device_operations mdt_lu_ops = {
5324 .ldo_object_alloc = mdt_object_alloc,
5325 .ldo_process_config = mdt_process_config,
5326 .ldo_prepare = mdt_prepare,
5329 static const struct lu_object_operations mdt_obj_ops = {
5330 .loo_object_init = mdt_object_init,
5331 .loo_object_free = mdt_object_free,
5332 .loo_object_print = mdt_object_print
5335 static int mdt_obd_set_info_async(const struct lu_env *env,
5336 struct obd_export *exp,
5337 __u32 keylen, void *key,
5338 __u32 vallen, void *val,
5339 struct ptlrpc_request_set *set)
5341 struct obd_device *obd = exp->exp_obd;
5347 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5348 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5356 * Match client and server connection feature flags.
5358 * Compute the compatibility flags for a connection request based on
5359 * features mutually supported by client and server.
5361 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5362 * must not be updated here, otherwise a partially initialized value may
5363 * be exposed. After the connection request is successfully processed,
5364 * the top-level MDT connect request handler atomically updates the export
5365 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5366 * reply. \see mdt_connect().
5368 * \param exp the obd_export associated with this client/target pair
5369 * \param mdt the target device for the connection
5370 * \param data stores data for this connect request
5373 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5374 * \retval -EBADE client and server feature requirements are incompatible
5376 static int mdt_connect_internal(struct obd_export *exp,
5377 struct mdt_device *mdt,
5378 struct obd_connect_data *data)
5380 LASSERT(data != NULL);
5382 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5383 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5385 /* If no known bits (which should not happen, probably,
5386 as everybody should support LOOKUP and UPDATE bits at least)
5387 revert to compat mode with plain locks. */
5388 if (!data->ocd_ibits_known &&
5389 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5390 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5392 if (!mdt->mdt_opts.mo_acl)
5393 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5395 if (!mdt->mdt_opts.mo_user_xattr)
5396 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5398 if (!mdt->mdt_som_conf)
5399 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5401 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5402 data->ocd_brw_size = min(data->ocd_brw_size,
5403 (__u32)MD_MAX_BRW_SIZE);
5404 if (data->ocd_brw_size == 0) {
5405 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5406 " ocd_version: %x ocd_grant: %d "
5407 "ocd_index: %u ocd_brw_size is "
5408 "unexpectedly zero, network data "
5409 "corruption? Refusing connection of this"
5412 exp->exp_client_uuid.uuid,
5413 exp, data->ocd_connect_flags, data->ocd_version,
5414 data->ocd_grant, data->ocd_index);
5419 /* NB: Disregard the rule against updating
5420 * exp_connect_data.ocd_connect_flags in this case, since
5421 * tgt_client_new() needs to know if this is a lightweight
5422 * connection, and it is safe to expose this flag before
5423 * connection processing completes. */
5424 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5425 spin_lock(&exp->exp_lock);
5426 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5427 spin_unlock(&exp->exp_lock);
5430 data->ocd_version = LUSTRE_VERSION_CODE;
5432 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5433 CWARN("%s: MDS requires FID support, but client not\n",
5438 if (mdt->mdt_som_conf &&
5439 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5440 OBD_CONNECT_MDS_MDS |
5441 OBD_CONNECT_SOM))) {
5442 CWARN("%s: MDS has SOM enabled, but client does not support "
5443 "it\n", mdt_obd_name(mdt));
5447 if (OCD_HAS_FLAG(data, PINGLESS)) {
5448 if (ptlrpc_pinger_suppress_pings()) {
5449 spin_lock(&exp->exp_obd->obd_dev_lock);
5450 list_del_init(&exp->exp_obd_chain_timed);
5451 spin_unlock(&exp->exp_obd->obd_dev_lock);
5453 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5457 data->ocd_max_easize = mdt->mdt_max_ea_size;
5462 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
5463 struct ptlrpc_request *req)
5465 struct sptlrpc_flavor flvr;
5468 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5469 read_lock(&mdt->mdt_sptlrpc_lock);
5470 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5474 read_unlock(&mdt->mdt_sptlrpc_lock);
5476 spin_lock(&exp->exp_lock);
5478 exp->exp_sp_peer = req->rq_sp_from;
5479 exp->exp_flvr = flvr;
5481 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5482 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5483 CERROR("unauthorized rpc flavor %x from %s, "
5484 "expect %x\n", req->rq_flvr.sf_rpc,
5485 libcfs_nid2str(req->rq_peer.nid),
5486 exp->exp_flvr.sf_rpc);
5490 spin_unlock(&exp->exp_lock);
5492 if (exp->exp_sp_peer != req->rq_sp_from) {
5493 CERROR("RPC source %s doesn't match %s\n",
5494 sptlrpc_part2name(req->rq_sp_from),
5495 sptlrpc_part2name(exp->exp_sp_peer));
5498 rc = sptlrpc_target_export_check(exp, req);
5505 /* mds_connect copy */
5506 static int mdt_obd_connect(const struct lu_env *env,
5507 struct obd_export **exp, struct obd_device *obd,
5508 struct obd_uuid *cluuid,
5509 struct obd_connect_data *data,
5512 struct obd_export *lexp;
5513 struct lustre_handle conn = { 0 };
5514 struct mdt_device *mdt;
5518 LASSERT(env != NULL);
5519 if (!exp || !obd || !cluuid)
5522 mdt = mdt_dev(obd->obd_lu_dev);
5525 * first, check whether the stack is ready to handle requests
5526 * XXX: probably not very appropriate method is used now
5527 * at some point we should find a better one
5529 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5530 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5531 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5534 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5537 rc = class_connect(&conn, obd, cluuid);
5541 lexp = class_conn2export(&conn);
5542 LASSERT(lexp != NULL);
5544 rc = mdt_connect_internal(lexp, mdt, data);
5546 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5549 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5550 rc = tgt_client_new(env, lexp);
5552 mdt_export_stats_init(obd, lexp, localdata);
5556 class_disconnect(lexp);
5565 static int mdt_obd_reconnect(const struct lu_env *env,
5566 struct obd_export *exp, struct obd_device *obd,
5567 struct obd_uuid *cluuid,
5568 struct obd_connect_data *data,
5574 if (exp == NULL || obd == NULL || cluuid == NULL)
5577 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5579 mdt_export_stats_init(obd, exp, localdata);
5584 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5585 struct mdt_thread_info *info,
5586 struct mdt_file_data *mfd)
5588 struct lu_context ses;
5592 rc = lu_context_init(&ses, LCT_SESSION);
5597 lu_context_enter(&ses);
5599 mdt_ucred(info)->uc_valid = UCRED_OLD;
5600 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5602 lu_context_exit(&ses);
5603 lu_context_fini(&ses);
5609 static int mdt_export_cleanup(struct obd_export *exp)
5611 struct mdt_export_data *med = &exp->exp_mdt_data;
5612 struct obd_device *obd = exp->exp_obd;
5613 struct mdt_device *mdt;
5614 struct mdt_thread_info *info;
5616 CFS_LIST_HEAD(closing_list);
5617 struct mdt_file_data *mfd, *n;
5621 spin_lock(&med->med_open_lock);
5622 while (!cfs_list_empty(&med->med_open_head)) {
5623 cfs_list_t *tmp = med->med_open_head.next;
5624 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5626 /* Remove mfd handle so it can't be found again.
5627 * We are consuming the mfd_list reference here. */
5628 class_handle_unhash(&mfd->mfd_handle);
5629 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5631 spin_unlock(&med->med_open_lock);
5632 mdt = mdt_dev(obd->obd_lu_dev);
5633 LASSERT(mdt != NULL);
5635 rc = lu_env_init(&env, LCT_MD_THREAD);
5639 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5640 LASSERT(info != NULL);
5641 memset(info, 0, sizeof *info);
5642 info->mti_env = &env;
5643 info->mti_mdt = mdt;
5644 info->mti_exp = exp;
5646 if (!cfs_list_empty(&closing_list)) {
5647 struct md_attr *ma = &info->mti_attr;
5649 /* Close any open files (which may also cause orphan unlinking). */
5650 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5651 cfs_list_del_init(&mfd->mfd_list);
5652 ma->ma_need = ma->ma_valid = 0;
5654 /* This file is being closed due to an eviction, it
5655 * could have been modified and now dirty regarding to
5656 * HSM archive, check this!
5657 * The logic here is to mark a file dirty if there's a
5658 * chance it was dirtied before the client was evicted,
5659 * so that we don't have to wait for a release attempt
5660 * before finding out the file was actually dirty and
5661 * fail the release. Aggressively marking it dirty here
5662 * will cause the policy engine to attempt to
5663 * re-archive it; when rearchiving, we can compare the
5664 * current version to the HSM data_version and make the
5665 * archive request into a noop if it's not actually
5668 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5669 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5671 /* Don't unlink orphan on failover umount, LU-184 */
5672 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5673 ma->ma_valid = MA_FLAGS;
5674 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5676 mdt_mfd_close(info, mfd);
5679 info->mti_mdt = NULL;
5680 /* cleanup client slot early */
5681 /* Do not erase record for recoverable client. */
5682 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5683 tgt_client_del(&env, exp);
5689 static int mdt_obd_disconnect(struct obd_export *exp)
5695 class_export_get(exp);
5697 rc = server_disconnect_export(exp);
5699 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5701 rc = mdt_export_cleanup(exp);
5702 class_export_put(exp);
5706 /* FIXME: Can we avoid using these two interfaces? */
5707 static int mdt_init_export(struct obd_export *exp)
5709 struct mdt_export_data *med = &exp->exp_mdt_data;
5713 CFS_INIT_LIST_HEAD(&med->med_open_head);
5714 spin_lock_init(&med->med_open_lock);
5715 mutex_init(&med->med_idmap_mutex);
5716 med->med_idmap = NULL;
5717 spin_lock(&exp->exp_lock);
5718 exp->exp_connecting = 1;
5719 spin_unlock(&exp->exp_lock);
5721 /* self-export doesn't need client data and ldlm initialization */
5722 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5723 &exp->exp_client_uuid)))
5726 rc = tgt_client_alloc(exp);
5730 rc = ldlm_init_export(exp);
5737 tgt_client_free(exp);
5739 CERROR("%s: Failed to initialize export: rc = %d\n",
5740 exp->exp_obd->obd_name, rc);
5744 static int mdt_destroy_export(struct obd_export *exp)
5748 if (exp_connect_rmtclient(exp))
5749 mdt_cleanup_idmap(&exp->exp_mdt_data);
5751 target_destroy_export(exp);
5752 /* destroy can be called from failed obd_setup, so
5753 * checking uuid is safer than obd_self_export */
5754 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5755 &exp->exp_client_uuid)))
5758 ldlm_destroy_export(exp);
5759 tgt_client_free(exp);
5761 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5762 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5767 /** The maximum depth that fid2path() will search.
5768 * This is limited only because we want to store the fids for
5769 * historical path lookup purposes.
5771 #define MAX_PATH_DEPTH 100
5773 /** mdt_path() lookup structure. */
5774 struct path_lookup_info {
5775 __u64 pli_recno; /**< history point */
5776 __u64 pli_currec; /**< current record */
5777 struct lu_fid pli_fid;
5778 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5779 struct mdt_object *pli_mdt_obj;
5780 char *pli_path; /**< full path */
5782 int pli_linkno; /**< which hardlink to follow */
5783 int pli_fidcount; /**< number of \a pli_fids */
5786 static int mdt_links_read(struct mdt_thread_info *info,
5787 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5791 LASSERT(ldata->ld_buf->lb_buf != NULL);
5793 if (!mdt_object_exists(mdt_obj))
5796 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5797 ldata->ld_buf, XATTR_NAME_LINK);
5798 if (rc == -ERANGE) {
5799 /* Buf was too small, figure out what we need. */
5800 lu_buf_free(ldata->ld_buf);
5801 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5802 ldata->ld_buf, XATTR_NAME_LINK);
5805 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5806 if (ldata->ld_buf->lb_buf == NULL)
5808 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5809 ldata->ld_buf, XATTR_NAME_LINK);
5814 return linkea_init(ldata);
5817 static int mdt_path_current(struct mdt_thread_info *info,
5818 struct path_lookup_info *pli)
5820 struct mdt_device *mdt = info->mti_mdt;
5821 struct mdt_object *mdt_obj;
5822 struct link_ea_header *leh;
5823 struct link_ea_entry *lee;
5824 struct lu_name *tmpname = &info->mti_name;
5825 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5826 struct lu_buf *buf = &info->mti_big_buf;
5829 struct linkea_data ldata = { 0 };
5833 /* temp buffer for path element, the buffer will be finally freed
5834 * in mdt_thread_info_fini */
5835 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5836 if (buf->lb_buf == NULL)
5840 ptr = pli->pli_path + pli->pli_pathlen - 1;
5843 pli->pli_fidcount = 0;
5844 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5846 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5847 * so checking root_fid can only happen on MDT0. */
5848 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5849 &pli->pli_fids[pli->pli_fidcount])) {
5850 mdt_obj = mdt_object_find(info->mti_env, mdt,
5851 &pli->pli_fids[pli->pli_fidcount]);
5852 if (IS_ERR(mdt_obj))
5853 GOTO(out, rc = PTR_ERR(mdt_obj));
5854 if (mdt_object_remote(mdt_obj)) {
5855 mdt_object_put(info->mti_env, mdt_obj);
5856 GOTO(remote_out, rc = -EREMOTE);
5858 if (!mdt_object_exists(mdt_obj)) {
5859 mdt_object_put(info->mti_env, mdt_obj);
5860 GOTO(out, rc = -ENOENT);
5863 rc = mdt_links_read(info, mdt_obj, &ldata);
5864 mdt_object_put(info->mti_env, mdt_obj);
5869 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5870 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5871 /* If set, use link #linkno for path lookup, otherwise use
5872 link #0. Only do this for the final path element. */
5873 if (pli->pli_fidcount == 0 &&
5874 pli->pli_linkno < leh->leh_reccount) {
5876 for (count = 0; count < pli->pli_linkno; count++) {
5877 lee = (struct link_ea_entry *)
5878 ((char *)lee + reclen);
5879 linkea_entry_unpack(lee, &reclen, tmpname,
5882 if (pli->pli_linkno < leh->leh_reccount - 1)
5883 /* indicate to user there are more links */
5887 /* Pack the name in the end of the buffer */
5888 ptr -= tmpname->ln_namelen;
5889 if (ptr - 1 <= pli->pli_path)
5890 GOTO(out, rc = -EOVERFLOW);
5891 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5894 /* Store the parent fid for historic lookup */
5895 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5896 GOTO(out, rc = -EOVERFLOW);
5897 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5901 ptr++; /* skip leading / */
5902 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5909 /* Returns the full path to this fid, as of changelog record recno. */
5910 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5911 char *path, int pathlen, __u64 *recno, int *linkno,
5914 struct mdt_device *mdt = info->mti_mdt;
5915 struct path_lookup_info *pli;
5923 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5932 pli->pli_mdt_obj = obj;
5933 pli->pli_recno = *recno;
5934 pli->pli_path = path;
5935 pli->pli_pathlen = pathlen;
5936 pli->pli_linkno = *linkno;
5938 /* Retry multiple times in case file is being moved */
5939 while (tries-- && rc == -EAGAIN)
5940 rc = mdt_path_current(info, pli);
5942 /* return the last resolved fids to the client, so the client will
5943 * build the left path on another MDT for remote object */
5944 *fid = pli->pli_fids[pli->pli_fidcount];
5946 *recno = pli->pli_currec;
5947 /* Return next link index to caller */
5948 *linkno = pli->pli_linkno;
5955 static int mdt_fid2path(struct mdt_thread_info *info,
5956 struct getinfo_fid2path *fp)
5958 struct mdt_device *mdt = info->mti_mdt;
5959 struct mdt_object *obj;
5963 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5964 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5966 if (!fid_is_sane(&fp->gf_fid))
5969 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5970 CWARN("%s: "DFID" is invalid, sequence should be "
5971 ">= "LPX64"\n", mdt_obd_name(mdt),
5972 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5976 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5977 if (obj == NULL || IS_ERR(obj)) {
5978 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5983 if (mdt_object_remote(obj))
5985 else if (!mdt_object_exists(obj))
5991 mdt_object_put(info->mti_env, obj);
5992 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5993 PFID(&fp->gf_fid), rc);
5997 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5998 &fp->gf_linkno, &fp->gf_fid);
6000 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
6001 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
6003 mdt_object_put(info->mti_env, obj);
6008 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
6009 void *val, int vallen)
6011 struct getinfo_fid2path *fpout, *fpin;
6014 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6017 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6018 lustre_swab_fid2path(fpin);
6020 memcpy(fpout, fpin, sizeof(*fpin));
6021 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6024 rc = mdt_fid2path(info, fpout);
6028 int mdt_get_info(struct mdt_thread_info *info)
6030 struct ptlrpc_request *req = mdt_info_req(info);
6038 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
6040 CDEBUG(D_IOCTL, "No GETINFO key");
6043 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
6046 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
6047 if (vallen == NULL) {
6048 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
6052 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6054 rc = req_capsule_server_pack(info->mti_pill);
6055 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
6056 if (valout == NULL) {
6057 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
6061 if (KEY_IS(KEY_FID2PATH))
6062 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
6066 lustre_msg_set_status(req->rq_repmsg, rc);
6071 /* Pass the ioc down */
6072 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
6073 unsigned int cmd, int len, void *data)
6075 struct lu_context ioctl_session;
6076 struct md_device *next = mdt->mdt_child;
6080 rc = lu_context_init(&ioctl_session, LCT_SESSION);
6083 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
6084 lu_context_enter(&ioctl_session);
6085 env->le_ses = &ioctl_session;
6087 LASSERT(next->md_ops->mdo_iocontrol);
6088 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
6090 lu_context_exit(&ioctl_session);
6091 lu_context_fini(&ioctl_session);
6095 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6097 struct obd_ioctl_data *data = karg;
6100 struct mdt_object *obj;
6101 struct mdt_lock_handle *lh;
6105 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6106 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6109 fid = (struct lu_fid *)data->ioc_inlbuf1;
6111 if (!fid_is_sane(fid))
6114 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6116 lh = &mti->mti_lh[MDT_LH_PARENT];
6117 mdt_lock_reg_init(lh, LCK_CR);
6119 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6121 RETURN(PTR_ERR(obj));
6123 if (mdt_object_remote(obj)) {
6126 * before calling version get the correct MDS should be
6127 * fid, this is error to find remote object here
6129 CERROR("nonlocal object "DFID"\n", PFID(fid));
6130 } else if (!mdt_object_exists(obj)) {
6131 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6134 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6135 *(__u64 *)data->ioc_inlbuf2 = version;
6138 mdt_object_unlock_put(mti, obj, lh, 1);
6142 /* ioctls on obd dev */
6143 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6144 void *karg, void *uarg)
6147 struct obd_device *obd = exp->exp_obd;
6148 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6149 struct dt_device *dt = mdt->mdt_bottom;
6153 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6154 rc = lu_env_init(&env, LCT_MD_THREAD);
6160 rc = mdt_device_sync(&env, mdt);
6162 case OBD_IOC_SET_READONLY:
6163 rc = dt->dd_ops->dt_ro(&env, dt);
6165 case OBD_IOC_ABORT_RECOVERY:
6166 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6167 target_stop_recovery_thread(obd);
6170 case OBD_IOC_CHANGELOG_REG:
6171 case OBD_IOC_CHANGELOG_DEREG:
6172 case OBD_IOC_CHANGELOG_CLEAR:
6173 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6175 case OBD_IOC_START_LFSCK: {
6176 struct md_device *next = mdt->mdt_child;
6177 struct obd_ioctl_data *data = karg;
6178 struct lfsck_start_param lsp;
6180 if (unlikely(data == NULL)) {
6185 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6186 lsp.lsp_namespace = mdt->mdt_namespace;
6187 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6190 case OBD_IOC_STOP_LFSCK: {
6191 struct md_device *next = mdt->mdt_child;
6193 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
6196 case OBD_IOC_GET_OBJ_VERSION: {
6197 struct mdt_thread_info *mti;
6198 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6199 memset(mti, 0, sizeof *mti);
6200 mti->mti_env = &env;
6204 rc = mdt_ioc_version_get(mti, karg);
6209 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6210 mdt_obd_name(mdt), cmd, rc);
6217 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6219 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6223 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6227 int mdt_obd_postrecov(struct obd_device *obd)
6232 rc = lu_env_init(&env, LCT_MD_THREAD);
6235 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6240 static struct obd_ops mdt_obd_device_ops = {
6241 .o_owner = THIS_MODULE,
6242 .o_set_info_async = mdt_obd_set_info_async,
6243 .o_connect = mdt_obd_connect,
6244 .o_reconnect = mdt_obd_reconnect,
6245 .o_disconnect = mdt_obd_disconnect,
6246 .o_init_export = mdt_init_export,
6247 .o_destroy_export = mdt_destroy_export,
6248 .o_iocontrol = mdt_iocontrol,
6249 .o_postrecov = mdt_obd_postrecov,
6252 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6253 struct lu_device *d)
6255 struct mdt_device *m = mdt_dev(d);
6262 static struct lu_device *mdt_device_free(const struct lu_env *env,
6263 struct lu_device *d)
6265 struct mdt_device *m = mdt_dev(d);
6268 lu_device_fini(&m->mdt_lu_dev);
6274 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6275 struct lu_device_type *t,
6276 struct lustre_cfg *cfg)
6278 struct lu_device *l;
6279 struct mdt_device *m;
6286 rc = mdt_init0(env, m, t, cfg);
6288 mdt_device_free(env, l);
6293 l = ERR_PTR(-ENOMEM);
6297 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6298 LU_KEY_INIT(mdt, struct mdt_thread_info);
6300 static void mdt_key_fini(const struct lu_context *ctx,
6301 struct lu_context_key *key, void* data)
6303 struct mdt_thread_info *info = data;
6305 if (info->mti_big_lmm) {
6306 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6307 info->mti_big_lmm = NULL;
6308 info->mti_big_lmmsize = 0;
6313 /* context key: mdt_thread_key */
6314 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6316 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6318 return lu_ucred(info->mti_env);
6321 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6323 return lu_ucred_check(info->mti_env);
6327 * Enable/disable COS (Commit On Sharing).
6329 * Set/Clear the COS flag in mdt options.
6331 * \param mdt mdt device
6332 * \param val 0 disables COS, other values enable COS
6334 void mdt_enable_cos(struct mdt_device *mdt, int val)
6339 mdt->mdt_opts.mo_cos = !!val;
6340 rc = lu_env_init(&env, LCT_LOCAL);
6341 if (unlikely(rc != 0)) {
6342 CWARN("lu_env initialization failed with rc = %d,"
6343 "cannot sync\n", rc);
6346 mdt_device_sync(&env, mdt);
6351 * Check COS (Commit On Sharing) status.
6353 * Return COS flag status.
6355 * \param mdt mdt device
6357 int mdt_cos_is_enabled(struct mdt_device *mdt)
6359 return mdt->mdt_opts.mo_cos != 0;
6362 static struct lu_device_type_operations mdt_device_type_ops = {
6363 .ldto_device_alloc = mdt_device_alloc,
6364 .ldto_device_free = mdt_device_free,
6365 .ldto_device_fini = mdt_device_fini
6368 static struct lu_device_type mdt_device_type = {
6369 .ldt_tags = LU_DEVICE_MD,
6370 .ldt_name = LUSTRE_MDT_NAME,
6371 .ldt_ops = &mdt_device_type_ops,
6372 .ldt_ctx_tags = LCT_MD_THREAD
6375 static int __init mdt_mod_init(void)
6377 struct lprocfs_static_vars lvars;
6380 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6381 FID_NOBRACE_LEN + 1);
6382 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6384 rc = lu_kmem_init(mdt_caches);
6388 rc = mds_mod_init();
6392 lprocfs_mdt_init_vars(&lvars);
6393 rc = class_register_type(&mdt_obd_device_ops, NULL,
6394 lvars.module_vars, LUSTRE_MDT_NAME,
6400 lu_kmem_fini(mdt_caches);
6407 static void __exit mdt_mod_exit(void)
6409 class_unregister_type(LUSTRE_MDT_NAME);
6411 lu_kmem_fini(mdt_caches);
6414 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6415 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6416 MODULE_LICENSE("GPL");
6418 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);