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. */
418 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
423 b->valid |= OBD_MD_FLID;
424 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
425 PFID(fid), b->nlink, b->mode, b->size);
429 mdt_body_reverse_idmap(info, b);
431 if (b->valid & OBD_MD_FLSIZE)
432 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
433 PFID(fid), (unsigned long long)b->size);
436 static inline int mdt_body_has_lov(const struct lu_attr *la,
437 const struct mdt_body *body)
439 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
440 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
443 void mdt_client_compatibility(struct mdt_thread_info *info)
445 struct mdt_body *body;
446 struct ptlrpc_request *req = mdt_info_req(info);
447 struct obd_export *exp = req->rq_export;
448 struct md_attr *ma = &info->mti_attr;
449 struct lu_attr *la = &ma->ma_attr;
452 if (exp_connect_layout(exp))
453 /* the client can deal with 16-bit lmm_stripe_count */
456 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
458 if (!mdt_body_has_lov(la, body))
461 /* now we have a reply with a lov for a client not compatible with the
462 * layout lock so we have to clean the layout generation number */
463 if (S_ISREG(la->la_mode))
464 ma->ma_lmm->lmm_layout_gen = 0;
468 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
471 const struct lu_env *env = info->mti_env;
475 LASSERT(info->mti_big_lmm_used == 0);
476 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
480 /* big_lmm may need to be grown */
481 if (info->mti_big_lmmsize < rc) {
482 int size = size_roundup_power2(rc);
484 if (info->mti_big_lmmsize > 0) {
485 /* free old buffer */
486 LASSERT(info->mti_big_lmm);
487 OBD_FREE_LARGE(info->mti_big_lmm,
488 info->mti_big_lmmsize);
489 info->mti_big_lmm = NULL;
490 info->mti_big_lmmsize = 0;
493 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
494 if (info->mti_big_lmm == NULL)
496 info->mti_big_lmmsize = size;
498 LASSERT(info->mti_big_lmmsize >= rc);
500 info->mti_buf.lb_buf = info->mti_big_lmm;
501 info->mti_buf.lb_len = info->mti_big_lmmsize;
502 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
507 int mdt_attr_get_lov(struct mdt_thread_info *info,
508 struct mdt_object *o, struct md_attr *ma)
510 struct md_object *next = mdt_object_child(o);
511 struct lu_buf *buf = &info->mti_buf;
514 buf->lb_buf = ma->ma_lmm;
515 buf->lb_len = ma->ma_lmm_size;
516 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
518 ma->ma_lmm_size = rc;
519 ma->ma_valid |= MA_LOV;
521 } else if (rc == -ENODATA) {
524 } else if (rc == -ERANGE) {
525 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
527 info->mti_big_lmm_used = 1;
528 ma->ma_valid |= MA_LOV;
529 ma->ma_lmm = info->mti_big_lmm;
530 ma->ma_lmm_size = rc;
531 /* update mdt_max_mdsize so all clients
532 * will be aware about that */
533 if (info->mti_mdt->mdt_max_mdsize < rc)
534 info->mti_mdt->mdt_max_mdsize = rc;
542 int mdt_attr_get_pfid(struct mdt_thread_info *info,
543 struct mdt_object *o, struct lu_fid *pfid)
545 struct lu_buf *buf = &info->mti_buf;
546 struct link_ea_header *leh;
547 struct link_ea_entry *lee;
551 buf->lb_buf = info->mti_big_lmm;
552 buf->lb_len = info->mti_big_lmmsize;
553 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
554 buf, XATTR_NAME_LINK);
555 /* ignore errors, MA_PFID won't be set and it is
556 * up to the caller to treat this as an error */
557 if (rc == -ERANGE || buf->lb_len == 0) {
558 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
559 buf->lb_buf = info->mti_big_lmm;
560 buf->lb_len = info->mti_big_lmmsize;
565 if (rc < sizeof(*leh)) {
566 CERROR("short LinkEA on "DFID": rc = %d\n",
567 PFID(mdt_object_fid(o)), rc);
571 leh = (struct link_ea_header *) buf->lb_buf;
572 lee = (struct link_ea_entry *)(leh + 1);
573 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
574 leh->leh_magic = LINK_EA_MAGIC;
575 leh->leh_reccount = __swab32(leh->leh_reccount);
576 leh->leh_len = __swab64(leh->leh_len);
578 if (leh->leh_magic != LINK_EA_MAGIC)
580 if (leh->leh_reccount == 0)
583 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
584 fid_be_to_cpu(pfid, pfid);
589 int mdt_attr_get_complex(struct mdt_thread_info *info,
590 struct mdt_object *o, struct md_attr *ma)
592 const struct lu_env *env = info->mti_env;
593 struct md_object *next = mdt_object_child(o);
594 struct lu_buf *buf = &info->mti_buf;
595 u32 mode = lu_object_attr(&next->mo_lu);
596 int need = ma->ma_need;
602 if (need & MA_INODE) {
603 ma->ma_need = MA_INODE;
604 rc = mo_attr_get(env, next, ma);
607 ma->ma_valid |= MA_INODE;
610 if (need & MA_PFID) {
611 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
613 ma->ma_valid |= MA_PFID;
614 /* ignore this error, parent fid is not mandatory */
618 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
619 rc = mdt_attr_get_lov(info, o, ma);
624 if (need & MA_LMV && S_ISDIR(mode)) {
625 buf->lb_buf = ma->ma_lmv;
626 buf->lb_len = ma->ma_lmv_size;
627 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
629 ma->ma_lmv_size = rc2;
630 ma->ma_valid |= MA_LMV;
631 } else if (rc2 == -ENODATA) {
638 if (need & MA_SOM && S_ISREG(mode)) {
639 buf->lb_buf = info->mti_xattr_buf;
640 buf->lb_len = sizeof(info->mti_xattr_buf);
641 CLASSERT(sizeof(struct som_attrs) <=
642 sizeof(info->mti_xattr_buf));
643 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
644 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
646 ma->ma_valid |= MA_SOM;
647 else if (rc2 < 0 && rc2 != -ENODATA)
651 if (need & MA_HSM && S_ISREG(mode)) {
652 buf->lb_buf = info->mti_xattr_buf;
653 buf->lb_len = sizeof(info->mti_xattr_buf);
654 CLASSERT(sizeof(struct hsm_attrs) <=
655 sizeof(info->mti_xattr_buf));
656 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
657 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
659 ma->ma_valid |= MA_HSM;
660 else if (rc2 < 0 && rc2 != -ENODATA)
664 #ifdef CONFIG_FS_POSIX_ACL
665 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
666 buf->lb_buf = ma->ma_acl;
667 buf->lb_len = ma->ma_acl_size;
668 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
670 ma->ma_acl_size = rc2;
671 ma->ma_valid |= MA_ACL_DEF;
672 } else if (rc2 == -ENODATA) {
681 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
682 rc, ma->ma_valid, ma->ma_lmm);
686 static int mdt_getattr_internal(struct mdt_thread_info *info,
687 struct mdt_object *o, int ma_need)
689 struct md_object *next = mdt_object_child(o);
690 const struct mdt_body *reqbody = info->mti_body;
691 struct ptlrpc_request *req = mdt_info_req(info);
692 struct md_attr *ma = &info->mti_attr;
693 struct lu_attr *la = &ma->ma_attr;
694 struct req_capsule *pill = info->mti_pill;
695 const struct lu_env *env = info->mti_env;
696 struct mdt_body *repbody;
697 struct lu_buf *buffer = &info->mti_buf;
702 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
703 RETURN(err_serious(-ENOMEM));
705 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
709 if (mdt_object_remote(o)) {
710 /* This object is located on remote node.*/
711 /* Return -EIO for old client */
712 if (!mdt_is_dne_client(req->rq_export))
713 GOTO(out, rc = -EIO);
715 repbody->fid1 = *mdt_object_fid(o);
716 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
720 buffer->lb_len = reqbody->eadatasize;
721 if (buffer->lb_len > 0)
722 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
724 buffer->lb_buf = NULL;
726 /* If it is dir object and client require MEA, then we got MEA */
727 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
728 reqbody->valid & OBD_MD_MEA) {
729 /* Assumption: MDT_MD size is enough for lmv size. */
730 ma->ma_lmv = buffer->lb_buf;
731 ma->ma_lmv_size = buffer->lb_len;
732 ma->ma_need = MA_LMV | MA_INODE;
734 ma->ma_lmm = buffer->lb_buf;
735 ma->ma_lmm_size = buffer->lb_len;
736 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
739 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
740 reqbody->valid & OBD_MD_FLDIREA &&
741 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
742 /* get default stripe info for this dir. */
743 ma->ma_need |= MA_LOV_DEF;
745 ma->ma_need |= ma_need;
746 if (ma->ma_need & MA_SOM)
747 ma->ma_som = &info->mti_u.som.data;
749 rc = mdt_attr_get_complex(info, o, ma);
751 CERROR("%s: getattr error for "DFID": rc = %d\n",
752 mdt_obd_name(info->mti_mdt),
753 PFID(mdt_object_fid(o)), rc);
757 /* if file is released, check if a restore is running */
758 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
759 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
760 repbody->t_state = MS_RESTORE;
761 repbody->valid |= OBD_MD_TSTATE;
764 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
766 /* the Lustre protocol supposes to return default striping
767 * on the user-visible root if explicitly requested */
768 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
769 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
770 struct lu_fid rootfid;
771 struct mdt_object *root;
772 struct mdt_device *mdt = info->mti_mdt;
774 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
777 root = mdt_object_find(env, mdt, &rootfid);
779 RETURN(PTR_ERR(root));
780 rc = mdt_attr_get_lov(info, root, ma);
781 mdt_object_put(info->mti_env, root);
783 CERROR("%s: getattr error for "DFID": rc = %d\n",
784 mdt_obd_name(info->mti_mdt),
785 PFID(mdt_object_fid(o)), rc);
790 if (likely(ma->ma_valid & MA_INODE))
791 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
795 if (mdt_body_has_lov(la, reqbody)) {
796 if (ma->ma_valid & MA_LOV) {
797 LASSERT(ma->ma_lmm_size);
798 mdt_dump_lmm(D_INFO, ma->ma_lmm);
799 repbody->eadatasize = ma->ma_lmm_size;
800 if (S_ISDIR(la->la_mode))
801 repbody->valid |= OBD_MD_FLDIREA;
803 repbody->valid |= OBD_MD_FLEASIZE;
805 if (ma->ma_valid & MA_LMV) {
806 LASSERT(S_ISDIR(la->la_mode));
807 repbody->eadatasize = ma->ma_lmv_size;
808 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
810 } else if (S_ISLNK(la->la_mode) &&
811 reqbody->valid & OBD_MD_LINKNAME) {
812 buffer->lb_buf = ma->ma_lmm;
813 /* eadatasize from client includes NULL-terminator, so
814 * there is no need to read it */
815 buffer->lb_len = reqbody->eadatasize - 1;
816 rc = mo_readlink(env, next, buffer);
817 if (unlikely(rc <= 0)) {
818 CERROR("%s: readlink failed for "DFID": rc = %d\n",
819 mdt_obd_name(info->mti_mdt),
820 PFID(mdt_object_fid(o)), rc);
823 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
825 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
827 repbody->valid |= OBD_MD_LINKNAME;
828 /* we need to report back size with NULL-terminator
829 * because client expects that */
830 repbody->eadatasize = rc + 1;
831 if (repbody->eadatasize != reqbody->eadatasize)
832 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
833 "on "DFID ", expected %d\n",
834 mdt_obd_name(info->mti_mdt),
835 rc, PFID(mdt_object_fid(o)),
836 reqbody->eadatasize - 1);
838 ((char *)ma->ma_lmm)[rc] = 0;
840 /* If the total CDEBUG() size is larger than a page, it
841 * will print a warning to the console, avoid this by
842 * printing just the last part of the symlink. */
843 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
844 print_limit < rc ? "..." : "", print_limit,
845 (char *)ma->ma_lmm + rc - print_limit, rc);
850 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
851 repbody->max_cookiesize = 0;
852 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
853 repbody->valid |= OBD_MD_FLMODEASIZE;
854 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
855 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
856 repbody->max_cookiesize);
859 if (exp_connect_rmtclient(info->mti_exp) &&
860 reqbody->valid & OBD_MD_FLRMTPERM) {
861 void *buf = req_capsule_server_get(pill, &RMF_ACL);
863 /* mdt_getattr_lock only */
864 rc = mdt_pack_remote_perm(info, o, buf);
866 repbody->valid &= ~OBD_MD_FLRMTPERM;
867 repbody->aclsize = 0;
870 repbody->valid |= OBD_MD_FLRMTPERM;
871 repbody->aclsize = sizeof(struct mdt_remote_perm);
874 #ifdef CONFIG_FS_POSIX_ACL
875 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
876 (reqbody->valid & OBD_MD_FLACL)) {
877 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
878 buffer->lb_len = req_capsule_get_size(pill,
879 &RMF_ACL, RCL_SERVER);
880 if (buffer->lb_len > 0) {
881 rc = mo_xattr_get(env, next, buffer,
882 XATTR_NAME_ACL_ACCESS);
884 if (rc == -ENODATA) {
885 repbody->aclsize = 0;
886 repbody->valid |= OBD_MD_FLACL;
888 } else if (rc == -EOPNOTSUPP) {
891 CERROR("%s: unable to read "DFID
893 mdt_obd_name(info->mti_mdt),
894 PFID(mdt_object_fid(o)), rc);
897 repbody->aclsize = rc;
898 repbody->valid |= OBD_MD_FLACL;
905 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
906 info->mti_mdt->mdt_opts.mo_mds_capa &&
907 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
908 struct lustre_capa *capa;
910 capa = req_capsule_server_get(pill, &RMF_CAPA1);
912 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
913 rc = mo_capa_get(env, next, capa, 0);
916 repbody->valid |= OBD_MD_FLMDSCAPA;
921 mdt_counter_incr(req, LPROC_MDT_GETATTR);
926 static int mdt_renew_capa(struct mdt_thread_info *info)
928 struct mdt_object *obj = info->mti_object;
929 struct mdt_body *body;
930 struct lustre_capa *capa, *c;
934 /* if object doesn't exist, or server has disabled capability,
935 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
938 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
939 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
942 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
943 LASSERT(body != NULL);
945 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
948 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
952 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
954 body->valid |= OBD_MD_FLOSSCAPA;
958 int mdt_getattr(struct mdt_thread_info *info)
960 struct mdt_object *obj = info->mti_object;
961 struct req_capsule *pill = info->mti_pill;
962 struct mdt_body *reqbody;
963 struct mdt_body *repbody;
968 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
971 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
972 rc = req_capsule_server_pack(pill);
974 RETURN(err_serious(rc));
975 rc = mdt_renew_capa(info);
976 GOTO(out_shrink, rc);
979 LASSERT(obj != NULL);
980 LASSERT(lu_object_assert_exists(&obj->mot_obj));
982 mode = lu_object_attr(&obj->mot_obj);
984 /* old clients may not report needed easize, use max value then */
985 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
986 reqbody->eadatasize == 0 ?
987 info->mti_mdt->mdt_max_mdsize :
988 reqbody->eadatasize);
990 rc = req_capsule_server_pack(pill);
991 if (unlikely(rc != 0))
992 RETURN(err_serious(rc));
994 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
995 LASSERT(repbody != NULL);
996 repbody->eadatasize = 0;
997 repbody->aclsize = 0;
999 if (reqbody->valid & OBD_MD_FLRMTPERM)
1000 rc = mdt_init_ucred(info, reqbody);
1002 rc = mdt_check_ucred(info);
1004 GOTO(out_shrink, rc);
1006 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1009 * Don't check capability at all, because rename might getattr for
1010 * remote obj, and at that time no capability is available.
1012 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1013 rc = mdt_getattr_internal(info, obj, 0);
1014 if (reqbody->valid & OBD_MD_FLRMTPERM)
1015 mdt_exit_ucred(info);
1018 mdt_client_compatibility(info);
1019 rc2 = mdt_fix_reply(info);
1025 int mdt_is_subdir(struct mdt_thread_info *info)
1027 struct mdt_object *o = info->mti_object;
1028 struct req_capsule *pill = info->mti_pill;
1029 const struct mdt_body *body = info->mti_body;
1030 struct mdt_body *repbody;
1036 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1039 * We save last checked parent fid to @repbody->fid1 for remote
1042 LASSERT(fid_is_sane(&body->fid2));
1043 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1044 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1045 &body->fid2, &repbody->fid1);
1046 if (rc == 0 || rc == -EREMOTE)
1047 repbody->valid |= OBD_MD_FLID;
1052 int mdt_swap_layouts(struct mdt_thread_info *info)
1054 struct ptlrpc_request *req = mdt_info_req(info);
1055 struct obd_export *exp = req->rq_export;
1056 struct mdt_object *o1, *o2, *o;
1057 struct mdt_lock_handle *lh1, *lh2;
1058 struct mdc_swap_layouts *msl;
1062 /* client does not support layout lock, so layout swaping
1064 * FIXME: there is a problem for old clients which don't support
1065 * layout lock yet. If those clients have already opened the file
1066 * they won't be notified at all so that old layout may still be
1067 * used to do IO. This can be fixed after file release is landed by
1068 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1069 if (!exp_connect_layout(exp))
1070 RETURN(-EOPNOTSUPP);
1072 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1073 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1074 req_capsule_client_get(info->mti_pill,
1077 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1078 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1079 req_capsule_client_get(info->mti_pill,
1082 o1 = info->mti_object;
1083 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1084 &info->mti_body->fid2);
1086 GOTO(out, rc = PTR_ERR(o));
1088 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1089 GOTO(put, rc = -ENOENT);
1091 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1092 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1098 /* permission check. Make sure the calling process having permission
1099 * to write both files. */
1100 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1105 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1110 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1112 GOTO(put, rc = -EPROTO);
1114 lh1 = &info->mti_lh[MDT_LH_NEW];
1115 mdt_lock_reg_init(lh1, LCK_EX);
1116 lh2 = &info->mti_lh[MDT_LH_OLD];
1117 mdt_lock_reg_init(lh2, LCK_EX);
1119 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT,
1124 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT,
1129 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1130 mdt_object_child(o2), msl->msl_flags);
1133 mdt_object_unlock(info, o2, lh2, rc);
1135 mdt_object_unlock(info, o1, lh1, rc);
1137 mdt_object_put(info->mti_env, o);
1142 static int mdt_raw_lookup(struct mdt_thread_info *info,
1143 struct mdt_object *parent,
1144 const struct lu_name *lname,
1145 struct ldlm_reply *ldlm_rep)
1147 struct md_object *next = mdt_object_child(info->mti_object);
1148 const struct mdt_body *reqbody = info->mti_body;
1149 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1150 struct mdt_body *repbody;
1154 if (reqbody->valid != OBD_MD_FLID)
1157 LASSERT(!info->mti_cross_ref);
1159 /* Only got the fid of this obj by name */
1160 fid_zero(child_fid);
1161 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1164 /* XXX is raw_lookup possible as intent operation? */
1167 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1170 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1172 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1175 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1176 repbody->fid1 = *child_fid;
1177 repbody->valid = OBD_MD_FLID;
1183 * UPDATE lock should be taken against parent, and be release before exit;
1184 * child_bits lock should be taken against child, and be returned back:
1185 * (1)normal request should release the child lock;
1186 * (2)intent request will grant the lock to client.
1188 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1189 struct mdt_lock_handle *lhc,
1191 struct ldlm_reply *ldlm_rep)
1193 struct ptlrpc_request *req = mdt_info_req(info);
1194 struct mdt_body *reqbody = NULL;
1195 struct mdt_object *parent = info->mti_object;
1196 struct mdt_object *child;
1197 struct md_object *next = mdt_object_child(parent);
1198 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1199 struct lu_name *lname = NULL;
1200 const char *name = NULL;
1202 struct mdt_lock_handle *lhp = NULL;
1203 struct ldlm_lock *lock;
1204 struct ldlm_res_id *res_id;
1211 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1212 LASSERT(ergo(is_resent,
1213 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1215 LASSERT(parent != NULL);
1216 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1218 RETURN(err_serious(-EFAULT));
1220 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1222 if (!info->mti_cross_ref) {
1224 * XXX: Check for "namelen == 0" is for getattr by fid
1225 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1226 * that is the name must contain at least one character and
1227 * the terminating '\0'
1230 reqbody = req_capsule_client_get(info->mti_pill,
1232 if (unlikely(reqbody == NULL))
1233 RETURN(err_serious(-EFAULT));
1235 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1236 RETURN(err_serious(-EINVAL));
1239 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1241 PFID(mdt_object_fid(parent)),
1242 PFID(&reqbody->fid2), ldlm_rep);
1244 lname = mdt_name(info->mti_env, (char *)name, namelen);
1245 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1246 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1250 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1252 if (unlikely(!mdt_object_exists(parent))) {
1253 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1255 "Parent doesn't exist!\n");
1257 } else if (!info->mti_cross_ref) {
1258 LASSERTF(!mdt_object_remote(parent),
1259 "Parent "DFID" is on remote server\n",
1260 PFID(mdt_object_fid(parent)));
1263 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1271 if (info->mti_cross_ref) {
1272 /* Only getattr on the child. Parent is on another node. */
1273 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1275 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1276 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1279 /* Do not take lock for resent case. */
1280 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1281 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1282 lhc->mlh_reg_lh.cookie);
1283 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1284 &lock->l_resource->lr_name));
1285 LDLM_LOCK_PUT(lock);
1288 mdt_lock_handle_init(lhc);
1289 mdt_lock_reg_init(lhc, LCK_PR);
1292 * Object's name is on another MDS, no lookup lock is
1293 * needed here but update is.
1295 child_bits &= ~MDS_INODELOCK_LOOKUP;
1296 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1298 rc = mdt_object_lock(info, child, lhc, child_bits,
1302 /* Finally, we can get attr for child. */
1303 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1305 rc = mdt_getattr_internal(info, child, 0);
1306 if (unlikely(rc != 0))
1307 mdt_object_unlock(info, child, lhc, 1);
1313 /* step 1: lock parent only if parent is a directory */
1314 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1315 lhp = &info->mti_lh[MDT_LH_PARENT];
1316 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1317 rc = mdt_object_lock(info, parent, lhp,
1318 MDS_INODELOCK_UPDATE,
1320 if (unlikely(rc != 0))
1324 /* step 2: lookup child's fid by name */
1325 fid_zero(child_fid);
1326 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1331 mdt_set_disposition(info, ldlm_rep,
1333 GOTO(out_parent, rc);
1335 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1337 *child_fid = reqbody->fid2;
1338 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1342 *step 3: find the child object by fid & lock it.
1343 * regardless if it is local or remote.
1345 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1347 if (unlikely(IS_ERR(child)))
1348 GOTO(out_parent, rc = PTR_ERR(child));
1350 /* Do not take lock for resent case. */
1351 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1352 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1353 lhc->mlh_reg_lh.cookie);
1355 res_id = &lock->l_resource->lr_name;
1356 if (!fid_res_name_eq(mdt_object_fid(child),
1357 &lock->l_resource->lr_name)) {
1358 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1359 &lock->l_resource->lr_name),
1360 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1361 PLDLMRES(lock->l_resource),
1362 PFID(mdt_object_fid(parent)));
1363 CWARN("Although resent, but still not get child lock"
1364 "parent:"DFID" child:"DFID"\n",
1365 PFID(mdt_object_fid(parent)),
1366 PFID(mdt_object_fid(child)));
1367 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1368 LDLM_LOCK_PUT(lock);
1371 LDLM_LOCK_PUT(lock);
1374 bool try_layout = false;
1377 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1378 mdt_lock_handle_init(lhc);
1379 mdt_lock_reg_init(lhc, LCK_PR);
1381 if (!mdt_object_exists(child)) {
1382 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1384 "Object doesn't exist!\n");
1385 GOTO(out_child, rc = -ENOENT);
1388 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1389 mdt_object_exists(child) && !mdt_object_remote(child)) {
1390 struct md_attr *ma = &info->mti_attr;
1393 ma->ma_need = MA_INODE;
1394 rc = mdt_attr_get_complex(info, child, ma);
1395 if (unlikely(rc != 0))
1396 GOTO(out_child, rc);
1398 /* If the file has not been changed for some time, we
1399 * return not only a LOOKUP lock, but also an UPDATE
1400 * lock and this might save us RPC on later STAT. For
1401 * directories, it also let negative dentry starts
1402 * working for this dir. */
1403 if (ma->ma_valid & MA_INODE &&
1404 ma->ma_attr.la_valid & LA_CTIME &&
1405 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1406 ma->ma_attr.la_ctime < cfs_time_current_sec())
1407 child_bits |= MDS_INODELOCK_UPDATE;
1410 /* layout lock must be granted in a best-effort way
1411 * for IT operations */
1412 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1413 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1414 exp_connect_layout(info->mti_exp) &&
1415 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1417 /* try to grant layout lock for regular file. */
1423 child_bits |= MDS_INODELOCK_LAYOUT;
1424 /* try layout lock, it may fail to be granted due to
1425 * contention at LOOKUP or UPDATE */
1426 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1428 child_bits &= ~MDS_INODELOCK_LAYOUT;
1429 LASSERT(child_bits != 0);
1430 rc = mdt_object_lock(info, child, lhc,
1431 child_bits, MDT_CROSS_LOCK);
1436 rc = mdt_object_lock(info, child, lhc, child_bits,
1439 if (unlikely(rc != 0))
1440 GOTO(out_child, rc);
1443 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1444 /* Get MA_SOM attributes if update lock is given. */
1446 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1447 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1450 /* finally, we can get attr for child. */
1451 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1452 rc = mdt_getattr_internal(info, child, ma_need);
1453 if (unlikely(rc != 0)) {
1454 mdt_object_unlock(info, child, lhc, 1);
1456 /* Debugging code. */
1457 res_id = &lock->l_resource->lr_name;
1458 LDLM_DEBUG(lock, "Returning lock to client");
1459 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1460 &lock->l_resource->lr_name),
1461 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1462 PLDLMRES(lock->l_resource),
1463 PFID(mdt_object_fid(child)));
1464 if (mdt_object_exists(child) && !mdt_object_remote(child))
1465 mdt_pack_size2body(info, child);
1468 LDLM_LOCK_PUT(lock);
1472 mdt_object_put(info->mti_env, child);
1475 mdt_object_unlock(info, parent, lhp, 1);
1479 /* normal handler: should release the child lock */
1480 int mdt_getattr_name(struct mdt_thread_info *info)
1482 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1483 struct mdt_body *reqbody;
1484 struct mdt_body *repbody;
1488 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1489 LASSERT(reqbody != NULL);
1490 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1491 LASSERT(repbody != NULL);
1493 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1494 repbody->eadatasize = 0;
1495 repbody->aclsize = 0;
1497 rc = mdt_init_ucred(info, reqbody);
1499 GOTO(out_shrink, rc);
1501 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1502 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1503 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1504 lhc->mlh_reg_lh.cookie = 0;
1506 mdt_exit_ucred(info);
1509 mdt_client_compatibility(info);
1510 rc2 = mdt_fix_reply(info);
1516 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1517 void *karg, void *uarg);
1519 int mdt_set_info(struct mdt_thread_info *info)
1521 struct ptlrpc_request *req = mdt_info_req(info);
1524 int keylen, vallen, rc = 0;
1527 rc = req_capsule_server_pack(info->mti_pill);
1531 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1533 DEBUG_REQ(D_HA, req, "no set_info key");
1537 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1540 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1542 DEBUG_REQ(D_HA, req, "no set_info val");
1546 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1549 /* Swab any part of val you need to here */
1550 if (KEY_IS(KEY_READ_ONLY)) {
1552 lustre_msg_set_status(req->rq_repmsg, 0);
1554 spin_lock(&req->rq_export->exp_lock);
1556 *exp_connect_flags_ptr(req->rq_export) |=
1559 *exp_connect_flags_ptr(req->rq_export) &=
1560 ~OBD_CONNECT_RDONLY;
1561 spin_unlock(&req->rq_export->exp_lock);
1563 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1564 struct changelog_setinfo *cs =
1565 (struct changelog_setinfo *)val;
1566 if (vallen != sizeof(*cs)) {
1567 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1570 if (ptlrpc_req_need_swab(req)) {
1571 __swab64s(&cs->cs_recno);
1572 __swab32s(&cs->cs_id);
1575 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1577 lustre_msg_set_status(req->rq_repmsg, rc);
1585 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
1586 struct ptlrpc_request *req);
1589 * Top-level handler for MDT connection requests.
1591 int mdt_connect(struct mdt_thread_info *info)
1594 struct obd_connect_data *reply;
1595 struct obd_export *exp;
1596 struct ptlrpc_request *req = mdt_info_req(info);
1600 rc = target_handle_connect(req);
1602 RETURN(err_serious(rc));
1604 LASSERT(req->rq_export != NULL);
1605 exp = req->rq_export;
1606 info->mti_exp = exp;
1607 info->mti_mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
1608 rc = mdt_init_sec_level(info);
1612 rc = mdt_connect_check_sptlrpc(info->mti_mdt, exp, req);
1616 /* To avoid exposing partially initialized connection flags, changes up
1617 * to this point have been staged in reply->ocd_connect_flags. Now that
1618 * connection handling has completed successfully, atomically update
1619 * the connect flags in the shared export data structure. LU-1623 */
1620 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1621 spin_lock(&exp->exp_lock);
1622 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1623 spin_unlock(&exp->exp_lock);
1625 rc = mdt_init_idmap(info);
1630 obd_disconnect(class_export_get(req->rq_export));
1634 int mdt_disconnect(struct mdt_thread_info *info)
1639 rc = target_handle_disconnect(mdt_info_req(info));
1641 rc = err_serious(rc);
1645 static int mdt_sendpage(struct mdt_thread_info *info,
1646 struct lu_rdpg *rdpg, int nob)
1648 struct ptlrpc_request *req = mdt_info_req(info);
1649 struct obd_export *exp = req->rq_export;
1650 struct ptlrpc_bulk_desc *desc;
1651 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1658 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1663 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1664 /* old client requires reply size in it's PAGE_SIZE,
1665 * which is rdpg->rp_count */
1666 nob = rdpg->rp_count;
1668 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1669 i++, tmpcount -= tmpsize) {
1670 tmpsize = min_t(int, tmpcount, PAGE_CACHE_SIZE);
1671 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1674 LASSERT(desc->bd_nob == nob);
1675 rc = target_bulk_io(exp, desc, lwi);
1676 ptlrpc_free_bulk_pin(desc);
1680 int mdt_readpage(struct mdt_thread_info *info)
1682 struct mdt_object *object = info->mti_object;
1683 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1684 struct mdt_body *reqbody;
1685 struct mdt_body *repbody;
1690 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1691 RETURN(err_serious(-ENOMEM));
1693 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1694 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1695 if (reqbody == NULL || repbody == NULL)
1696 RETURN(err_serious(-EFAULT));
1699 * prepare @rdpg before calling lower layers and transfer itself. Here
1700 * reqbody->size contains offset of where to start to read and
1701 * reqbody->nlink contains number bytes to read.
1703 rdpg->rp_hash = reqbody->size;
1704 if (rdpg->rp_hash != reqbody->size) {
1705 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1706 rdpg->rp_hash, reqbody->size);
1710 rdpg->rp_attrs = reqbody->mode;
1711 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1712 rdpg->rp_attrs |= LUDA_64BITHASH;
1713 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1714 exp_max_brw_size(info->mti_exp));
1715 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1717 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1718 if (rdpg->rp_pages == NULL)
1721 for (i = 0; i < rdpg->rp_npages; ++i) {
1722 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1723 if (rdpg->rp_pages[i] == NULL)
1724 GOTO(free_rdpg, rc = -ENOMEM);
1727 /* call lower layers to fill allocated pages with directory data */
1728 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1730 GOTO(free_rdpg, rc);
1732 /* send pages to client */
1733 rc = mdt_sendpage(info, rdpg, rc);
1738 for (i = 0; i < rdpg->rp_npages; i++)
1739 if (rdpg->rp_pages[i] != NULL)
1740 __free_page(rdpg->rp_pages[i]);
1741 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1743 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1749 static int mdt_reint_internal(struct mdt_thread_info *info,
1750 struct mdt_lock_handle *lhc,
1753 struct req_capsule *pill = info->mti_pill;
1754 struct mdt_body *repbody;
1759 rc = mdt_reint_unpack(info, op);
1761 CERROR("Can't unpack reint, rc %d\n", rc);
1762 RETURN(err_serious(rc));
1765 /* for replay (no_create) lmm is not needed, client has it already */
1766 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1767 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1768 info->mti_rr.rr_eadatalen);
1770 /* llog cookies are always 0, the field is kept for compatibility */
1771 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1772 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1774 rc = req_capsule_server_pack(pill);
1776 CERROR("Can't pack response, rc %d\n", rc);
1777 RETURN(err_serious(rc));
1780 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1781 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1783 repbody->eadatasize = 0;
1784 repbody->aclsize = 0;
1787 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1789 /* for replay no cookkie / lmm need, because client have this already */
1790 if (info->mti_spec.no_create)
1791 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1792 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1794 rc = mdt_init_ucred_reint(info);
1796 GOTO(out_shrink, rc);
1798 rc = mdt_fix_attr_ucred(info, op);
1800 GOTO(out_ucred, rc = err_serious(rc));
1802 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1803 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1804 GOTO(out_ucred, rc);
1806 rc = mdt_reint_rec(info, lhc);
1809 mdt_exit_ucred(info);
1811 mdt_client_compatibility(info);
1812 rc2 = mdt_fix_reply(info);
1818 static long mdt_reint_opcode(struct mdt_thread_info *info,
1819 const struct req_format **fmt)
1821 struct mdt_rec_reint *rec;
1824 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1826 opc = rec->rr_opcode;
1827 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1828 if (opc < REINT_MAX && fmt[opc] != NULL)
1829 req_capsule_extend(info->mti_pill, fmt[opc]);
1831 CERROR("%s: Unsupported opcode '%ld' from client '%s': "
1832 "rc = %d\n", mdt_obd_name(info->mti_mdt), opc,
1833 info->mti_mdt->mdt_ldlm_client->cli_name,
1835 opc = err_serious(-EFAULT);
1838 opc = err_serious(-EFAULT);
1843 int mdt_reint(struct mdt_thread_info *info)
1848 static const struct req_format *reint_fmts[REINT_MAX] = {
1849 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1850 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1851 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1852 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1853 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1854 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1855 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1856 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1861 opc = mdt_reint_opcode(info, reint_fmts);
1864 * No lock possible here from client to pass it to reint code
1867 rc = mdt_reint_internal(info, NULL, opc);
1872 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1876 /* this should sync the whole device */
1877 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1879 struct dt_device *dt = mdt->mdt_bottom;
1883 rc = dt->dd_ops->dt_sync(env, dt);
1887 /* this should sync this object */
1888 static int mdt_object_sync(struct mdt_thread_info *info)
1890 struct md_object *next;
1894 if (!mdt_object_exists(info->mti_object)) {
1895 CWARN("Non existing object "DFID"!\n",
1896 PFID(mdt_object_fid(info->mti_object)));
1899 next = mdt_object_child(info->mti_object);
1900 rc = mo_object_sync(info->mti_env, next);
1905 int mdt_sync(struct mdt_thread_info *info)
1907 struct ptlrpc_request *req = mdt_info_req(info);
1908 struct req_capsule *pill = info->mti_pill;
1909 struct mdt_body *body;
1913 /* The fid may be zero, so we req_capsule_set manually */
1914 req_capsule_set(pill, &RQF_MDS_SYNC);
1916 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1918 RETURN(err_serious(-EINVAL));
1920 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1921 RETURN(err_serious(-ENOMEM));
1923 if (fid_seq(&body->fid1) == 0) {
1924 /* sync the whole device */
1925 rc = req_capsule_server_pack(pill);
1927 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1929 rc = err_serious(rc);
1931 /* sync an object */
1932 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1934 rc = mdt_object_sync(info);
1936 const struct lu_fid *fid;
1937 struct lu_attr *la = &info->mti_attr.ma_attr;
1939 info->mti_attr.ma_need = MA_INODE;
1940 info->mti_attr.ma_valid = 0;
1941 rc = mdt_attr_get_complex(info, info->mti_object,
1944 body = req_capsule_server_get(pill,
1946 fid = mdt_object_fid(info->mti_object);
1947 mdt_pack_attr2body(info, body, la, fid);
1951 rc = err_serious(rc);
1954 mdt_counter_incr(req, LPROC_MDT_SYNC);
1960 * Quotacheck handler.
1961 * in-kernel quotacheck isn't supported any more.
1963 int mdt_quotacheck(struct mdt_thread_info *info)
1965 struct obd_quotactl *oqctl;
1969 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1971 RETURN(err_serious(-EPROTO));
1973 rc = req_capsule_server_pack(info->mti_pill);
1975 RETURN(err_serious(rc));
1977 /* deprecated, not used any more */
1978 RETURN(-EOPNOTSUPP);
1982 * Handle quota control requests to consult current usage/limit, but also
1983 * to configure quota enforcement
1985 int mdt_quotactl(struct mdt_thread_info *info)
1987 struct obd_export *exp = info->mti_exp;
1988 struct req_capsule *pill = info->mti_pill;
1989 struct obd_quotactl *oqctl, *repoqc;
1991 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1994 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1996 RETURN(err_serious(-EPROTO));
1998 rc = req_capsule_server_pack(pill);
2000 RETURN(err_serious(rc));
2002 switch (oqctl->qc_cmd) {
2004 case LUSTRE_Q_INVALIDATE:
2005 case LUSTRE_Q_FINVALIDATE:
2009 /* deprecated, not used any more */
2010 RETURN(-EOPNOTSUPP);
2011 /* master quotactl */
2017 RETURN(-EOPNOTSUPP);
2018 /* slave quotactl */
2023 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2027 /* map uid/gid for remote client */
2029 if (exp_connect_rmtclient(exp)) {
2030 struct lustre_idmap_table *idmap;
2032 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2034 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2035 oqctl->qc_cmd != Q_GETINFO))
2038 if (oqctl->qc_type == USRQUOTA)
2039 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2041 else if (oqctl->qc_type == GRPQUOTA)
2042 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2047 if (id == CFS_IDMAP_NOTFOUND) {
2048 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2053 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2055 RETURN(err_serious(-EFAULT));
2057 if (oqctl->qc_id != id)
2058 swap(oqctl->qc_id, id);
2060 switch (oqctl->qc_cmd) {
2066 /* forward quotactl request to QMT */
2067 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2072 /* slave quotactl */
2073 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2078 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2082 if (oqctl->qc_id != id)
2083 swap(oqctl->qc_id, id);
2090 * OBD PING and other handlers.
2092 int mdt_obd_ping(struct mdt_thread_info *info)
2097 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2099 rc = target_handle_ping(mdt_info_req(info));
2101 rc = err_serious(rc);
2106 * OBD_IDX_READ handler
2108 int mdt_obd_idx_read(struct mdt_thread_info *info)
2110 struct mdt_device *mdt = info->mti_mdt;
2111 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2112 struct idx_info *req_ii, *rep_ii;
2116 memset(rdpg, 0, sizeof(*rdpg));
2117 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2119 /* extract idx_info buffer from request & reply */
2120 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2121 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2122 RETURN(err_serious(-EPROTO));
2124 rc = req_capsule_server_pack(info->mti_pill);
2126 RETURN(err_serious(rc));
2128 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2130 RETURN(err_serious(-EFAULT));
2131 rep_ii->ii_magic = IDX_INFO_MAGIC;
2133 /* extract hash to start with */
2134 rdpg->rp_hash = req_ii->ii_hash_start;
2136 /* extract requested attributes */
2137 rdpg->rp_attrs = req_ii->ii_attrs;
2139 /* check that fid packed in request is valid and supported */
2140 if (!fid_is_sane(&req_ii->ii_fid))
2142 rep_ii->ii_fid = req_ii->ii_fid;
2145 rep_ii->ii_flags = req_ii->ii_flags;
2147 /* compute number of pages to allocate, ii_count is the number of 4KB
2149 if (req_ii->ii_count <= 0)
2150 GOTO(out, rc = -EFAULT);
2151 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2152 exp_max_brw_size(info->mti_exp));
2153 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
2156 /* allocate pages to store the containers */
2157 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2158 if (rdpg->rp_pages == NULL)
2159 GOTO(out, rc = -ENOMEM);
2160 for (i = 0; i < rdpg->rp_npages; i++) {
2161 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
2162 if (rdpg->rp_pages[i] == NULL)
2163 GOTO(out, rc = -ENOMEM);
2166 /* populate pages with key/record pairs */
2167 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2171 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2172 "asked %d > %d\n", rc, rdpg->rp_count);
2174 /* send pages to client */
2175 rc = mdt_sendpage(info, rdpg, rc);
2179 if (rdpg->rp_pages) {
2180 for (i = 0; i < rdpg->rp_npages; i++)
2181 if (rdpg->rp_pages[i])
2182 __free_page(rdpg->rp_pages[i]);
2183 OBD_FREE(rdpg->rp_pages,
2184 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2189 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2191 return err_serious(-EOPNOTSUPP);
2194 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2196 return err_serious(-EOPNOTSUPP);
2203 /** clone llog ctxt from child (mdd)
2204 * This allows remote llog (replicator) access.
2205 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2206 * context was originally set up, or we can handle them directly.
2207 * I choose the latter, but that means I need any llog
2208 * contexts set up by child to be accessable by the mdt. So we clone the
2209 * context into our context list here.
2211 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2214 struct md_device *next = mdt->mdt_child;
2215 struct llog_ctxt *ctxt;
2218 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2221 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2222 if (rc || ctxt == NULL) {
2226 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2228 CERROR("Can't set mdt ctxt %d\n", rc);
2233 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2234 struct mdt_device *mdt, int idx)
2236 struct llog_ctxt *ctxt;
2238 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2241 /* Put once for the get we just did, and once for the clone */
2242 llog_ctxt_put(ctxt);
2243 llog_ctxt_put(ctxt);
2247 int mdt_llog_create(struct mdt_thread_info *info)
2251 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2252 rc = llog_origin_handle_open(mdt_info_req(info));
2253 return (rc < 0 ? err_serious(rc) : rc);
2256 int mdt_llog_destroy(struct mdt_thread_info *info)
2260 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2261 rc = llog_origin_handle_destroy(mdt_info_req(info));
2262 return (rc < 0 ? err_serious(rc) : rc);
2265 int mdt_llog_read_header(struct mdt_thread_info *info)
2269 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2270 rc = llog_origin_handle_read_header(mdt_info_req(info));
2271 return (rc < 0 ? err_serious(rc) : rc);
2274 int mdt_llog_next_block(struct mdt_thread_info *info)
2278 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2279 rc = llog_origin_handle_next_block(mdt_info_req(info));
2280 return (rc < 0 ? err_serious(rc) : rc);
2283 int mdt_llog_prev_block(struct mdt_thread_info *info)
2287 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2288 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2289 return (rc < 0 ? err_serious(rc) : rc);
2297 static struct ldlm_callback_suite cbs = {
2298 .lcs_completion = ldlm_server_completion_ast,
2299 .lcs_blocking = ldlm_server_blocking_ast,
2300 .lcs_glimpse = ldlm_server_glimpse_ast
2303 int mdt_enqueue(struct mdt_thread_info *info)
2305 struct ptlrpc_request *req;
2309 * info->mti_dlm_req already contains swapped and (if necessary)
2310 * converted dlm request.
2312 LASSERT(info->mti_dlm_req != NULL);
2314 req = mdt_info_req(info);
2315 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2316 req, info->mti_dlm_req, &cbs);
2317 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2318 return rc ? err_serious(rc) : req->rq_status;
2321 int mdt_convert(struct mdt_thread_info *info)
2324 struct ptlrpc_request *req;
2326 LASSERT(info->mti_dlm_req);
2327 req = mdt_info_req(info);
2328 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2329 return rc ? err_serious(rc) : req->rq_status;
2332 int mdt_bl_callback(struct mdt_thread_info *info)
2334 CERROR("bl callbacks should not happen on MDS\n");
2336 return err_serious(-EOPNOTSUPP);
2339 int mdt_cp_callback(struct mdt_thread_info *info)
2341 CERROR("cp callbacks should not happen on MDS\n");
2343 return err_serious(-EOPNOTSUPP);
2347 * sec context handlers
2349 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2353 rc = mdt_handle_idmap(info);
2356 struct ptlrpc_request *req = mdt_info_req(info);
2359 opc = lustre_msg_get_opc(req->rq_reqmsg);
2360 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2361 sptlrpc_svc_ctx_invalidate(req);
2364 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2370 * quota request handlers
2372 int mdt_quota_dqacq(struct mdt_thread_info *info)
2374 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2379 RETURN(err_serious(-EOPNOTSUPP));
2381 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2385 static struct mdt_object *mdt_obj(struct lu_object *o)
2387 LASSERT(lu_device_is_mdt(o->lo_dev));
2388 return container_of0(o, struct mdt_object, mot_obj);
2391 struct mdt_object *mdt_object_new(const struct lu_env *env,
2392 struct mdt_device *d,
2393 const struct lu_fid *f)
2395 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2396 struct lu_object *o;
2397 struct mdt_object *m;
2400 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2401 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2402 if (unlikely(IS_ERR(o)))
2403 m = (struct mdt_object *)o;
2409 struct mdt_object *mdt_object_find(const struct lu_env *env,
2410 struct mdt_device *d,
2411 const struct lu_fid *f)
2413 struct lu_object *o;
2414 struct mdt_object *m;
2417 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2418 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2419 if (unlikely(IS_ERR(o)))
2420 m = (struct mdt_object *)o;
2428 * Asyncronous commit for mdt device.
2430 * Pass asynchonous commit call down the MDS stack.
2432 * \param env environment
2433 * \param mdt the mdt device
2435 static void mdt_device_commit_async(const struct lu_env *env,
2436 struct mdt_device *mdt)
2438 struct dt_device *dt = mdt->mdt_bottom;
2441 rc = dt->dd_ops->dt_commit_async(env, dt);
2442 if (unlikely(rc != 0))
2443 CWARN("async commit start failed with rc = %d", rc);
2447 * Mark the lock as "synchonous".
2449 * Mark the lock to deffer transaction commit to the unlock time.
2451 * \param lock the lock to mark as "synchonous"
2453 * \see mdt_is_lock_sync
2454 * \see mdt_save_lock
2456 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2458 lock->l_ast_data = (void*)1;
2462 * Check whehter the lock "synchonous" or not.
2464 * \param lock the lock to check
2465 * \retval 1 the lock is "synchonous"
2466 * \retval 0 the lock isn't "synchronous"
2468 * \see mdt_set_lock_sync
2469 * \see mdt_save_lock
2471 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2473 return lock->l_ast_data != NULL;
2477 * Blocking AST for mdt locks.
2479 * Starts transaction commit if in case of COS lock conflict or
2480 * deffers such a commit to the mdt_save_lock.
2482 * \param lock the lock which blocks a request or cancelling lock
2483 * \param desc unused
2484 * \param data unused
2485 * \param flag indicates whether this cancelling or blocking callback
2487 * \see ldlm_blocking_ast_nocheck
2489 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2490 void *data, int flag)
2492 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2493 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2497 if (flag == LDLM_CB_CANCELING)
2499 lock_res_and_lock(lock);
2500 if (lock->l_blocking_ast != mdt_blocking_ast) {
2501 unlock_res_and_lock(lock);
2504 if (mdt_cos_is_enabled(mdt) &&
2505 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2506 lock->l_blocking_lock != NULL &&
2507 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2508 mdt_set_lock_sync(lock);
2510 rc = ldlm_blocking_ast_nocheck(lock);
2512 /* There is no lock conflict if l_blocking_lock == NULL,
2513 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2514 * when the last reference to a local lock was released */
2515 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2518 rc = lu_env_init(&env, LCT_LOCAL);
2519 if (unlikely(rc != 0))
2520 CWARN("lu_env initialization failed with rc = %d,"
2521 "cannot start asynchronous commit\n", rc);
2523 mdt_device_commit_async(&env, mdt);
2529 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2530 void *data, int flag)
2532 struct lustre_handle lockh;
2536 case LDLM_CB_BLOCKING:
2537 ldlm_lock2handle(lock, &lockh);
2538 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2540 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2544 case LDLM_CB_CANCELING:
2545 LDLM_DEBUG(lock, "Revoke remote lock\n");
2553 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2554 struct mdt_object *o, struct lustre_handle *lh,
2555 ldlm_mode_t mode, __u64 ibits)
2557 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2558 ldlm_policy_data_t *policy = &mti->mti_policy;
2562 LASSERT(mdt_object_remote(o));
2564 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2566 memset(einfo, 0, sizeof(*einfo));
2567 einfo->ei_type = LDLM_IBITS;
2568 einfo->ei_mode = mode;
2569 einfo->ei_cb_bl = mdt_md_blocking_ast;
2570 einfo->ei_cb_cp = ldlm_completion_ast;
2572 memset(policy, 0, sizeof(*policy));
2573 policy->l_inodebits.bits = ibits;
2575 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2580 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2581 struct mdt_lock_handle *lh, __u64 ibits,
2582 bool nonblock, int locality)
2584 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2585 ldlm_policy_data_t *policy = &info->mti_policy;
2586 struct ldlm_res_id *res_id = &info->mti_res_id;
2591 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2592 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2593 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2594 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2596 if (mdt_object_remote(o)) {
2597 if (locality == MDT_CROSS_LOCK) {
2598 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2599 ibits |= MDS_INODELOCK_LOOKUP;
2602 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2603 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2604 mdt_obd_name(info->mti_mdt), ibits,
2605 PFID(mdt_object_fid(o)));
2606 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2608 /* No PDO lock on remote object */
2609 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2612 if (lh->mlh_type == MDT_PDO_LOCK) {
2613 /* check for exists after object is locked */
2614 if (mdt_object_exists(o) == 0) {
2615 /* Non-existent object shouldn't have PDO lock */
2618 /* Non-dir object shouldn't have PDO lock */
2619 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2624 memset(policy, 0, sizeof(*policy));
2625 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2627 dlmflags = LDLM_FL_ATOMIC_CB;
2629 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2632 * Take PDO lock on whole directory and build correct @res_id for lock
2633 * on part of directory.
2635 if (lh->mlh_pdo_hash != 0) {
2636 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2637 mdt_lock_pdo_mode(info, o, lh);
2638 if (lh->mlh_pdo_mode != LCK_NL) {
2640 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2641 * is never going to be sent to client and we do not
2642 * want it slowed down due to possible cancels.
2644 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2645 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2646 policy, res_id, dlmflags,
2647 &info->mti_exp->exp_handle.h_cookie);
2653 * Finish res_id initializing by name hash marking part of
2654 * directory which is taking modification.
2656 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2659 policy->l_inodebits.bits = ibits;
2662 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2663 * going to be sent to client. If it is - mdt_intent_policy() path will
2664 * fix it up and turn FL_LOCAL flag off.
2666 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2667 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2668 &info->mti_exp->exp_handle.h_cookie);
2670 mdt_object_unlock(info, o, lh, 1);
2671 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2672 lh->mlh_pdo_hash != 0 &&
2673 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2674 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2680 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2681 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2683 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2686 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2687 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2689 struct mdt_lock_handle tmp = *lh;
2692 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2700 * Save a lock within request object.
2702 * Keep the lock referenced until whether client ACK or transaction
2703 * commit happens or release the lock immediately depending on input
2704 * parameters. If COS is ON, a write lock is converted to COS lock
2707 * \param info thead info object
2708 * \param h lock handle
2709 * \param mode lock mode
2710 * \param decref force immediate lock releasing
2713 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2714 ldlm_mode_t mode, int decref)
2718 if (lustre_handle_is_used(h)) {
2719 if (decref || !info->mti_has_trans ||
2720 !(mode & (LCK_PW | LCK_EX))){
2721 mdt_fid_unlock(h, mode);
2723 struct mdt_device *mdt = info->mti_mdt;
2724 struct ldlm_lock *lock = ldlm_handle2lock(h);
2725 struct ptlrpc_request *req = mdt_info_req(info);
2728 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2730 CDEBUG(D_HA, "request = %p reply state = %p"
2731 " transno = "LPD64"\n",
2732 req, req->rq_reply_state, req->rq_transno);
2733 if (mdt_cos_is_enabled(mdt)) {
2735 ldlm_lock_downgrade(lock, LCK_COS);
2738 ptlrpc_save_lock(req, h, mode, no_ack);
2739 if (mdt_is_lock_sync(lock)) {
2740 CDEBUG(D_HA, "found sync-lock,"
2741 " async commit started\n");
2742 mdt_device_commit_async(info->mti_env,
2745 LDLM_LOCK_PUT(lock);
2754 * Unlock mdt object.
2756 * Immeditely release the regular lock and the PDO lock or save the
2757 * lock in reqeuest and keep them referenced until client ACK or
2758 * transaction commit.
2760 * \param info thread info object
2761 * \param o mdt object
2762 * \param lh mdt lock handle referencing regular and PDO locks
2763 * \param decref force immediate lock releasing
2765 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2766 struct mdt_lock_handle *lh, int decref)
2770 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2771 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2773 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2774 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2779 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2780 const struct lu_fid *f,
2781 struct mdt_lock_handle *lh,
2784 struct mdt_object *o;
2786 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2790 rc = mdt_object_lock(info, o, lh, ibits,
2793 mdt_object_put(info->mti_env, o);
2800 void mdt_object_unlock_put(struct mdt_thread_info * info,
2801 struct mdt_object * o,
2802 struct mdt_lock_handle *lh,
2805 mdt_object_unlock(info, o, lh, decref);
2806 mdt_object_put(info->mti_env, o);
2809 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2811 struct mdt_opc_slice *s;
2812 struct mdt_handler *h;
2815 for (s = supported; s->mos_hs != NULL; s++) {
2816 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2817 h = s->mos_hs + (opc - s->mos_opc_start);
2818 if (likely(h->mh_opc != 0))
2819 LASSERTF(h->mh_opc == opc,
2820 "opcode mismatch %d != %d\n",
2823 h = NULL; /* unsupported opc */
2830 static int mdt_lock_resname_compat(struct mdt_device *m,
2831 struct ldlm_request *req)
2833 /* XXX something... later. */
2837 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2839 /* XXX something... later. */
2844 * Generic code handling requests that have struct mdt_body passed in:
2846 * - extract mdt_body from request and save it in @info, if present;
2848 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2851 * - if HABEO_CORPUS flag is set for this request type check whether object
2852 * actually exists on storage (lu_object_exists()).
2855 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2857 const struct mdt_body *body;
2858 struct mdt_object *obj;
2859 const struct lu_env *env;
2860 struct req_capsule *pill;
2864 env = info->mti_env;
2865 pill = info->mti_pill;
2867 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2871 if (!(body->valid & OBD_MD_FLID))
2874 if (!fid_is_sane(&body->fid1)) {
2875 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2880 * Do not get size or any capa fields before we check that request
2881 * contains capa actually. There are some requests which do not, for
2882 * instance MDS_IS_SUBDIR.
2884 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2885 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2886 mdt_set_capainfo(info, 0, &body->fid1,
2887 req_capsule_client_get(pill, &RMF_CAPA1));
2889 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2891 if ((flags & HABEO_CORPUS) &&
2892 !mdt_object_exists(obj)) {
2893 mdt_object_put(env, obj);
2894 /* for capability renew ENOENT will be handled in
2896 if (body->valid & OBD_MD_FLOSSCAPA)
2901 info->mti_object = obj;
2910 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2912 struct req_capsule *pill = info->mti_pill;
2916 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2917 rc = mdt_body_unpack(info, flags);
2921 if (rc == 0 && (flags & HABEO_REFERO)) {
2923 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2924 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2925 info->mti_body->eadatasize);
2926 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2927 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2930 rc = req_capsule_server_pack(pill);
2935 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2937 struct md_device *next = m->mdt_child;
2939 return next->md_ops->mdo_init_capa_ctxt(env, next,
2940 m->mdt_opts.mo_mds_capa,
2941 m->mdt_capa_timeout,
2947 * Invoke handler for this request opc. Also do necessary preprocessing
2948 * (according to handler ->mh_flags), and post-processing (setting of
2949 * ->last_{xid,committed}).
2951 static int mdt_req_handle(struct mdt_thread_info *info,
2952 struct mdt_handler *h, struct ptlrpc_request *req)
2954 int rc, serious = 0;
2959 LASSERT(h->mh_act != NULL);
2960 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2961 LASSERT(current->journal_info == NULL);
2964 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2965 * to put same checks into handlers like mdt_close(), mdt_reint(),
2966 * etc., without talking to mdt authors first. Checking same thing
2967 * there again is useless and returning 0 error without packing reply
2968 * is buggy! Handlers either pack reply or return error.
2970 * We return 0 here and do not send any reply in order to emulate
2971 * network failure. Do not send any reply in case any of NET related
2972 * fail_id has occured.
2974 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2978 flags = h->mh_flags;
2979 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2981 if (h->mh_fmt != NULL) {
2982 req_capsule_set(info->mti_pill, h->mh_fmt);
2983 rc = mdt_unpack_req_pack_rep(info, flags);
2986 if (rc == 0 && flags & MUTABOR &&
2987 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2988 /* should it be rq_status? */
2991 if (rc == 0 && flags & HABEO_CLAVIS) {
2992 struct ldlm_request *dlm_req;
2994 LASSERT(h->mh_fmt != NULL);
2996 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2997 if (dlm_req != NULL) {
2998 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
3000 dlm_req->lock_desc.l_policy_data.\
3001 l_inodebits.bits == 0)) {
3003 * Lock without inodebits makes no sense and
3004 * will oops later in ldlm. If client miss to
3005 * set such bits, do not trigger ASSERTION.
3007 * For liblustre flock case, it maybe zero.
3011 if (info->mti_mdt &&
3012 info->mti_mdt->mdt_opts.mo_compat_resname)
3013 rc = mdt_lock_resname_compat(
3016 info->mti_dlm_req = dlm_req;
3023 /* capability setting changed via /proc, needs reinitialize ctxt */
3024 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
3025 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
3026 info->mti_mdt->mdt_capa_conf = 0;
3029 if (likely(rc == 0)) {
3031 * Process request, there can be two types of rc:
3032 * 1) errors with msg unpack/pack, other failures outside the
3033 * operation itself. This is counted as serious errors;
3034 * 2) errors during fs operation, should be placed in rq_status
3037 rc = h->mh_act(info);
3039 !req->rq_no_reply && req->rq_reply_state == NULL) {
3040 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3041 "pack reply and returned 0 error\n",
3045 serious = is_serious(rc);
3046 rc = clear_serious(rc);
3050 req->rq_status = rc;
3053 * ELDLM_* codes which > 0 should be in rq_status only as well as
3054 * all non-serious errors.
3056 if (rc > 0 || !serious)
3059 LASSERT(current->journal_info == NULL);
3061 if (rc == 0 && (flags & HABEO_CLAVIS) && info->mti_mdt &&
3062 info->mti_mdt->mdt_opts.mo_compat_resname) {
3063 struct ldlm_reply *dlmrep;
3065 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3067 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3070 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3071 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3072 target_committed_to_req(req);
3074 if (unlikely(req_is_replay(req) &&
3075 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3076 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3080 target_send_reply(req, rc, info->mti_fail_id);
3084 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3086 lh->mlh_type = MDT_NUL_LOCK;
3087 lh->mlh_reg_lh.cookie = 0ull;
3088 lh->mlh_reg_mode = LCK_MINMODE;
3089 lh->mlh_pdo_lh.cookie = 0ull;
3090 lh->mlh_pdo_mode = LCK_MINMODE;
3091 lh->mlh_rreg_lh.cookie = 0ull;
3092 lh->mlh_rreg_mode = LCK_MINMODE;
3095 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3097 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3098 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3102 * Initialize fields of struct mdt_thread_info. Other fields are left in
3103 * uninitialized state, because it's too expensive to zero out whole
3104 * mdt_thread_info (> 1K) on each request arrival.
3106 void mdt_thread_info_init(struct ptlrpc_request *req,
3107 struct mdt_thread_info *info)
3111 info->mti_pill = &req->rq_pill;
3114 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3115 mdt_lock_handle_init(&info->mti_lh[i]);
3117 /* mdt device: it can be NULL while CONNECT */
3118 if (req->rq_export) {
3119 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3120 info->mti_exp = req->rq_export;
3122 info->mti_mdt = NULL;
3123 info->mti_env = req->rq_svc_thread->t_env;
3124 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3125 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3126 info->mti_mos = NULL;
3128 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3129 info->mti_big_buf = LU_BUF_NULL;
3130 info->mti_body = NULL;
3131 info->mti_object = NULL;
3132 info->mti_dlm_req = NULL;
3133 info->mti_has_trans = 0;
3134 info->mti_cross_ref = 0;
3135 info->mti_opdata = 0;
3136 info->mti_big_lmm_used = 0;
3138 /* To not check for split by default. */
3139 info->mti_spec.no_create = 0;
3140 info->mti_spec.sp_rm_entry = 0;
3143 void mdt_thread_info_fini(struct mdt_thread_info *info)
3147 if (info->mti_object != NULL) {
3148 mdt_object_put(info->mti_env, info->mti_object);
3149 info->mti_object = NULL;
3152 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3153 mdt_lock_handle_fini(&info->mti_lh[i]);
3154 info->mti_env = NULL;
3155 info->mti_pill = NULL;
3156 info->mti_exp = NULL;
3158 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3159 lu_buf_free(&info->mti_big_buf);
3162 int mdt_tgt_connect(struct tgt_session_info *tsi)
3164 struct ptlrpc_request *req = tgt_ses_req(tsi);
3165 struct mdt_thread_info *mti;
3170 rc = tgt_connect(tsi);
3174 /* XXX: switch mdt_init_idmap() to use tgt_session_info */
3175 lu_env_refill((void *)tsi->tsi_env);
3176 mti = lu_context_key_get(&tsi->tsi_env->le_ctx, &mdt_thread_key);
3177 LASSERT(mti != NULL);
3179 mdt_thread_info_init(req, mti);
3180 rc = mdt_init_idmap(mti);
3181 mdt_thread_info_fini(mti);
3186 obd_disconnect(class_export_get(req->rq_export));
3190 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3191 struct obd_device *obd, int *process)
3193 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3194 case MDS_CONNECT: /* This will never get here, but for completeness. */
3195 case OST_CONNECT: /* This will never get here, but for completeness. */
3196 case MDS_DISCONNECT:
3197 case OST_DISCONNECT:
3203 case MDS_DONE_WRITING:
3204 case MDS_SYNC: /* used in unmounting */
3211 *process = target_queue_recovery_request(req, obd);
3215 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3222 * Handle recovery. Return:
3223 * +1: continue request processing;
3224 * -ve: abort immediately with the given error code;
3225 * 0: send reply with error code in req->rq_status;
3227 static int mdt_recovery(struct mdt_thread_info *info)
3229 struct ptlrpc_request *req = mdt_info_req(info);
3230 struct obd_device *obd;
3234 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3237 case SEC_CTX_INIT_CONT:
3243 rc = mdt_handle_idmap(info);
3252 if (unlikely(!class_connected_export(req->rq_export))) {
3253 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3254 lustre_msg_get_opc(req->rq_reqmsg),
3255 libcfs_id2str(req->rq_peer));
3256 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3257 * mds_A will get -ENOTCONN(especially for ping req),
3258 * which will cause that mds_A deactive timeout, then when
3259 * mds_A cleanup, the cleanup process will be suspended since
3260 * deactive timeout is not zero.
3262 req->rq_status = -ENOTCONN;
3263 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3267 /* sanity check: if the xid matches, the request must be marked as a
3268 * resent or replayed */
3269 if (req_xid_is_last(req)) {
3270 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3271 (MSG_RESENT | MSG_REPLAY))) {
3272 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3273 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3274 lustre_msg_get_flags(req->rq_reqmsg));
3276 req->rq_status = -ENOTCONN;
3281 /* else: note the opposite is not always true; a RESENT req after a
3282 * failover will usually not match the last_xid, since it was likely
3283 * never committed. A REPLAYed request will almost never match the
3284 * last xid, however it could for a committed, but still retained,
3287 obd = req->rq_export->exp_obd;
3289 /* Check for aborted recovery... */
3290 if (unlikely(obd->obd_recovering)) {
3293 DEBUG_REQ(D_INFO, req, "Got new replay");
3294 rc = mdt_filter_recovery_request(req, obd, &should_process);
3295 if (rc != 0 || !should_process)
3297 else if (should_process < 0) {
3298 req->rq_status = should_process;
3299 rc = ptlrpc_error(req);
3306 static int mdt_msg_check_version(struct lustre_msg *msg)
3310 switch (lustre_msg_get_opc(msg)) {
3312 case MDS_DISCONNECT:
3315 case SEC_CTX_INIT_CONT:
3318 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3320 CERROR("bad opc %u version %08x, expecting %08x\n",
3321 lustre_msg_get_opc(msg),
3322 lustre_msg_get_version(msg),
3323 LUSTRE_OBD_VERSION);
3327 case MDS_GETATTR_NAME:
3334 case MDS_DONE_WRITING:
3341 case MDS_HSM_PROGRESS:
3342 case MDS_HSM_REQUEST:
3343 case MDS_HSM_CT_REGISTER:
3344 case MDS_HSM_CT_UNREGISTER:
3345 case MDS_HSM_STATE_GET:
3346 case MDS_HSM_STATE_SET:
3347 case MDS_HSM_ACTION:
3348 case MDS_QUOTACHECK:
3351 case MDS_SWAP_LAYOUTS:
3356 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3358 CERROR("bad opc %u version %08x, expecting %08x\n",
3359 lustre_msg_get_opc(msg),
3360 lustre_msg_get_version(msg),
3361 LUSTRE_MDS_VERSION);
3365 case LDLM_BL_CALLBACK:
3366 case LDLM_CP_CALLBACK:
3367 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3369 CERROR("bad opc %u version %08x, expecting %08x\n",
3370 lustre_msg_get_opc(msg),
3371 lustre_msg_get_version(msg),
3372 LUSTRE_DLM_VERSION);
3374 case OBD_LOG_CANCEL:
3375 case LLOG_ORIGIN_HANDLE_CREATE:
3376 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3377 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3378 case LLOG_ORIGIN_HANDLE_CLOSE:
3379 case LLOG_ORIGIN_HANDLE_DESTROY:
3380 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3382 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3384 CERROR("bad opc %u version %08x, expecting %08x\n",
3385 lustre_msg_get_opc(msg),
3386 lustre_msg_get_version(msg),
3387 LUSTRE_LOG_VERSION);
3390 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3396 static int mdt_handle0(struct ptlrpc_request *req,
3397 struct mdt_thread_info *info,
3398 struct mdt_opc_slice *supported)
3400 struct mdt_handler *h;
3401 struct lustre_msg *msg;
3406 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3409 LASSERT(current->journal_info == NULL);
3411 msg = req->rq_reqmsg;
3412 rc = mdt_msg_check_version(msg);
3413 if (likely(rc == 0)) {
3414 rc = mdt_recovery(info);
3415 if (likely(rc == +1)) {
3416 h = mdt_handler_find(lustre_msg_get_opc(msg),
3418 if (likely(h != NULL)) {
3419 rc = mdt_req_handle(info, h, req);
3421 CERROR("%s: opc unsupported: 0x%x\n",
3422 mdt_obd_name(info->mti_mdt),
3423 lustre_msg_get_opc(msg));
3424 req->rq_status = -ENOTSUPP;
3425 rc = ptlrpc_error(req);
3430 CDEBUG(D_INFO, "%s: drops mal-formed request: rc = %d\n",
3431 mdt_obd_name(info->mti_mdt), rc);
3432 req->rq_status = rc;
3433 rc = ptlrpc_error(req);
3439 * MDT handler function called by ptlrpc service thread when request comes.
3441 * XXX common "target" functionality should be factored into separate module
3442 * shared by mdt, ost and stand-alone services like fld.
3444 int mdt_handle_common(struct ptlrpc_request *req,
3445 struct mdt_opc_slice *supported)
3448 struct mdt_thread_info *info;
3452 env = req->rq_svc_thread->t_env;
3453 LASSERT(env != NULL);
3454 /* Refill(initilize) the context(mdt_thread_info), in case it is
3455 * not initialized yet. Usually it happens during start up, after
3456 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3457 * before MDT_thread_info is initialized */
3459 LASSERT(env->le_ses != NULL);
3460 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3461 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3462 LASSERT(info != NULL);
3464 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3465 mdt_thread_info_init(req, info);
3467 rc = mdt_handle0(req, info, supported);
3469 mdt_thread_info_fini(info);
3470 req_capsule_fini(&req->rq_pill);
3475 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3478 int mdt_recovery_handle(struct ptlrpc_request *req)
3484 rc = mdt_handle_common(req, mdt_regular_handlers);
3504 static int mdt_intent_getattr(enum mdt_it_code opcode,
3505 struct mdt_thread_info *info,
3506 struct ldlm_lock **,
3509 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3510 struct mdt_thread_info *info,
3511 struct ldlm_lock **lockp,
3514 static int mdt_intent_layout(enum mdt_it_code opcode,
3515 struct mdt_thread_info *info,
3516 struct ldlm_lock **,
3518 static int mdt_intent_reint(enum mdt_it_code opcode,
3519 struct mdt_thread_info *info,
3520 struct ldlm_lock **,
3523 static struct mdt_it_flavor {
3524 const struct req_format *it_fmt;
3526 int (*it_act)(enum mdt_it_code ,
3527 struct mdt_thread_info *,
3528 struct ldlm_lock **,
3531 } mdt_it_flavor[] = {
3533 .it_fmt = &RQF_LDLM_INTENT,
3534 /*.it_flags = HABEO_REFERO,*/
3536 .it_act = mdt_intent_reint,
3537 .it_reint = REINT_OPEN
3540 .it_fmt = &RQF_LDLM_INTENT,
3542 * OCREAT is not a MUTABOR request as if the file
3544 * We do the extra check of OBD_CONNECT_RDONLY in
3545 * mdt_reint_open() when we really need to create
3549 .it_act = mdt_intent_reint,
3550 .it_reint = REINT_OPEN
3553 .it_fmt = &RQF_LDLM_INTENT,
3554 .it_flags = MUTABOR,
3555 .it_act = mdt_intent_reint,
3556 .it_reint = REINT_CREATE
3558 [MDT_IT_GETATTR] = {
3559 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3560 .it_flags = HABEO_REFERO,
3561 .it_act = mdt_intent_getattr
3563 [MDT_IT_READDIR] = {
3569 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3570 .it_flags = HABEO_REFERO,
3571 .it_act = mdt_intent_getattr
3574 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3575 .it_flags = MUTABOR,
3577 .it_reint = REINT_UNLINK
3581 .it_flags = MUTABOR,
3584 [MDT_IT_GETXATTR] = {
3585 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3586 .it_flags = HABEO_CORPUS,
3587 .it_act = mdt_intent_getxattr
3590 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3592 .it_act = mdt_intent_layout
3596 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3597 struct ldlm_lock **lockp,
3598 struct ldlm_lock *new_lock,
3599 struct mdt_lock_handle *lh,
3602 struct ptlrpc_request *req = mdt_info_req(info);
3603 struct ldlm_lock *lock = *lockp;
3606 * Get new lock only for cases when possible resent did not find any
3609 if (new_lock == NULL)
3610 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3612 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3613 lh->mlh_reg_lh.cookie = 0;
3617 LASSERTF(new_lock != NULL,
3618 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3621 * If we've already given this lock to a client once, then we should
3622 * have no readers or writers. Otherwise, we should have one reader
3623 * _or_ writer ref (which will be zeroed below) before returning the
3626 if (new_lock->l_export == req->rq_export) {
3627 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3629 LASSERT(new_lock->l_export == NULL);
3630 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3635 if (new_lock->l_export == req->rq_export) {
3637 * Already gave this to the client, which means that we
3638 * reconstructed a reply.
3640 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3642 lh->mlh_reg_lh.cookie = 0;
3643 RETURN(ELDLM_LOCK_REPLACED);
3647 * Fixup the lock to be given to the client.
3649 lock_res_and_lock(new_lock);
3650 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3651 * possible blocking AST. */
3652 while (new_lock->l_readers > 0) {
3653 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3654 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3655 new_lock->l_readers--;
3657 while (new_lock->l_writers > 0) {
3658 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3659 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3660 new_lock->l_writers--;
3663 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3664 new_lock->l_blocking_ast = lock->l_blocking_ast;
3665 new_lock->l_completion_ast = lock->l_completion_ast;
3666 new_lock->l_remote_handle = lock->l_remote_handle;
3667 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3669 unlock_res_and_lock(new_lock);
3671 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3672 &new_lock->l_remote_handle,
3673 &new_lock->l_exp_hash);
3675 LDLM_LOCK_RELEASE(new_lock);
3676 lh->mlh_reg_lh.cookie = 0;
3678 RETURN(ELDLM_LOCK_REPLACED);
3681 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3682 struct ldlm_lock *new_lock,
3683 struct ldlm_lock **old_lock,
3684 struct mdt_lock_handle *lh)
3686 struct ptlrpc_request *req = mdt_info_req(info);
3687 struct obd_export *exp = req->rq_export;
3688 struct lustre_handle remote_hdl;
3689 struct ldlm_request *dlmreq;
3690 struct ldlm_lock *lock;
3692 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3695 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3696 remote_hdl = dlmreq->lock_handle[0];
3698 /* In the function below, .hs_keycmp resolves to
3699 * ldlm_export_lock_keycmp() */
3700 /* coverity[overrun-buffer-val] */
3701 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3703 if (lock != new_lock) {
3704 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3705 lh->mlh_reg_mode = lock->l_granted_mode;
3707 LDLM_DEBUG(lock, "Restoring lock cookie");
3708 DEBUG_REQ(D_DLMTRACE, req,
3709 "restoring lock cookie "LPX64,
3710 lh->mlh_reg_lh.cookie);
3712 *old_lock = LDLM_LOCK_GET(lock);
3713 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3717 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3721 * If the xid matches, then we know this is a resent request, and allow
3722 * it. (It's probably an OPEN, for which we don't send a lock.
3724 if (req_xid_is_last(req))
3728 * This remote handle isn't enqueued, so we never received or processed
3729 * this request. Clear MSG_RESENT, because it can be handled like any
3730 * normal request now.
3732 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3734 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3738 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3739 struct mdt_thread_info *info,
3740 struct ldlm_lock **lockp,
3743 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3744 struct ldlm_reply *ldlm_rep = NULL;
3748 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3749 * (for the resend case) or a new lock. Below we will use it to
3750 * replace the original lock.
3752 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3753 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3754 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3755 rc = mdt_object_lock(info, info->mti_object, lhc,
3756 MDS_INODELOCK_XATTR,
3762 grc = mdt_getxattr(info);
3764 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3766 if (mdt_info_req(info)->rq_repmsg != NULL)
3767 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3768 if (ldlm_rep == NULL)
3769 RETURN(err_serious(-EFAULT));
3771 ldlm_rep->lock_policy_res2 = grc;
3776 static int mdt_intent_getattr(enum mdt_it_code opcode,
3777 struct mdt_thread_info *info,
3778 struct ldlm_lock **lockp,
3781 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3782 struct ldlm_lock *new_lock = NULL;
3784 struct ldlm_reply *ldlm_rep;
3785 struct ptlrpc_request *req;
3786 struct mdt_body *reqbody;
3787 struct mdt_body *repbody;
3791 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3794 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3797 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3798 repbody->eadatasize = 0;
3799 repbody->aclsize = 0;
3803 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3805 case MDT_IT_GETATTR:
3806 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3810 CERROR("Unsupported intent (%d)\n", opcode);
3811 GOTO(out_shrink, rc = -EINVAL);
3814 rc = mdt_init_ucred(info, reqbody);
3816 GOTO(out_shrink, rc);
3818 req = info->mti_pill->rc_req;
3819 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3820 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3822 /* Get lock from request for possible resent case. */
3823 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3825 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3826 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3828 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3829 ldlm_rep->lock_policy_res2 = 0;
3830 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3831 ldlm_rep->lock_policy_res2) {
3832 lhc->mlh_reg_lh.cookie = 0ull;
3833 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3836 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3839 mdt_exit_ucred(info);
3841 mdt_client_compatibility(info);
3842 rc2 = mdt_fix_reply(info);
3848 static int mdt_intent_layout(enum mdt_it_code opcode,
3849 struct mdt_thread_info *info,
3850 struct ldlm_lock **lockp,
3853 struct layout_intent *layout;
3855 struct mdt_object *obj = NULL;
3856 struct md_object *child = NULL;
3860 if (opcode != MDT_IT_LAYOUT) {
3861 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3866 fid = &info->mti_tmp_fid2;
3867 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3869 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3871 RETURN(PTR_ERR(obj));
3873 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3874 child = mdt_object_child(obj);
3876 /* get the length of lsm */
3877 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3880 if (rc > info->mti_mdt->mdt_max_mdsize)
3881 info->mti_mdt->mdt_max_mdsize = rc;
3884 mdt_object_put(info->mti_env, obj);
3886 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3887 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3888 ldlm_lvbo_size(*lockp));
3889 rc = req_capsule_server_pack(info->mti_pill);
3893 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3894 LASSERT(layout != NULL);
3895 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3896 /* return to normal ldlm handling */
3899 CERROR("%s: Unsupported layout intent (%d)\n",
3900 mdt_obd_name(info->mti_mdt), layout->li_opc);
3904 static int mdt_intent_reint(enum mdt_it_code opcode,
3905 struct mdt_thread_info *info,
3906 struct ldlm_lock **lockp,
3909 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3910 struct ldlm_reply *rep = NULL;
3914 static const struct req_format *intent_fmts[REINT_MAX] = {
3915 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3916 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3921 opc = mdt_reint_opcode(info, intent_fmts);
3925 if (mdt_it_flavor[opcode].it_reint != opc) {
3926 CERROR("Reint code %ld doesn't match intent: %d\n",
3928 RETURN(err_serious(-EPROTO));
3931 /* Get lock from request for possible resent case. */
3932 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3934 rc = mdt_reint_internal(info, lhc, opc);
3936 /* Check whether the reply has been packed successfully. */
3937 if (mdt_info_req(info)->rq_repmsg != NULL)
3938 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3940 RETURN(err_serious(-EFAULT));
3942 /* MDC expects this in any case */
3944 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3946 /* the open lock or the lock for cross-ref object should be
3947 * returned to the client */
3948 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3949 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3950 rep->lock_policy_res2 = 0;
3951 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3955 rep->lock_policy_res2 = clear_serious(rc);
3957 if (rep->lock_policy_res2 == -ENOENT &&
3958 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3959 rep->lock_policy_res2 = 0;
3961 if (rc == -ENOTCONN || rc == -ENODEV ||
3962 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3964 * If it is the disconnect error (ENODEV & ENOCONN), the error
3965 * will be returned by rq_status, and client at ptlrpc layer
3966 * will detect this, then disconnect, reconnect the import
3967 * immediately, instead of impacting the following the rpc.
3969 lhc->mlh_reg_lh.cookie = 0ull;
3973 * For other cases, the error will be returned by intent.
3974 * and client will retrieve the result from intent.
3977 * FIXME: when open lock is finished, that should be
3980 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3981 LASSERTF(rc == 0, "Error occurred but lock handle "
3982 "is still in use, rc = %d\n", rc);
3983 rep->lock_policy_res2 = 0;
3984 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3987 lhc->mlh_reg_lh.cookie = 0ull;
3988 RETURN(ELDLM_LOCK_ABORTED);
3993 static int mdt_intent_code(long itcode)
4001 case IT_OPEN|IT_CREAT:
4008 rc = MDT_IT_READDIR;
4011 rc = MDT_IT_GETATTR;
4023 rc = MDT_IT_GETXATTR;
4028 case IT_QUOTA_DQACQ:
4033 CERROR("Unknown intent opcode: %ld\n", itcode);
4040 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
4041 struct ldlm_lock **lockp, __u64 flags)
4043 struct req_capsule *pill;
4044 struct mdt_it_flavor *flv;
4049 opc = mdt_intent_code(itopc);
4053 pill = info->mti_pill;
4055 if (opc == MDT_IT_QUOTA) {
4056 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4059 RETURN(-EOPNOTSUPP);
4061 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4062 /* pass the request to quota master */
4063 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4064 mdt_info_req(info), lockp,
4069 flv = &mdt_it_flavor[opc];
4070 if (flv->it_fmt != NULL)
4071 req_capsule_extend(pill, flv->it_fmt);
4073 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
4075 struct ptlrpc_request *req = mdt_info_req(info);
4076 if (flv->it_flags & MUTABOR &&
4077 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
4080 if (rc == 0 && flv->it_act != NULL) {
4081 struct ldlm_reply *rep;
4083 /* execute policy */
4084 rc = flv->it_act(opc, info, lockp, flags);
4086 /* Check whether the reply has been packed successfully. */
4087 if (mdt_info_req(info)->rq_repmsg != NULL) {
4088 rep = req_capsule_server_get(info->mti_pill,
4090 rep->lock_policy_res2 =
4091 ptlrpc_status_hton(rep->lock_policy_res2);
4099 static int mdt_intent_policy(struct ldlm_namespace *ns,
4100 struct ldlm_lock **lockp, void *req_cookie,
4101 ldlm_mode_t mode, __u64 flags, void *data)
4103 struct mdt_thread_info *info;
4104 struct ptlrpc_request *req = req_cookie;
4105 struct ldlm_intent *it;
4106 struct req_capsule *pill;
4111 LASSERT(req != NULL);
4113 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
4115 LASSERT(info != NULL);
4116 pill = info->mti_pill;
4117 LASSERT(pill->rc_req == req);
4119 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4120 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4121 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4123 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4127 /* Lock without inodebits makes no sense and will oops
4128 * later in ldlm. Let's check it now to see if we have
4129 * ibits corrupted somewhere in mdt_intent_opc().
4130 * The case for client miss to set ibits has been
4131 * processed by others. */
4132 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
4133 lr_type == LDLM_IBITS,
4134 info->mti_dlm_req->lock_desc.\
4135 l_policy_data.l_inodebits.bits != 0));
4137 rc = err_serious(-EFAULT);
4139 /* No intent was provided */
4140 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4141 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4142 rc = req_capsule_server_pack(pill);
4144 rc = err_serious(rc);
4149 static int mdt_seq_fini(const struct lu_env *env,
4150 struct mdt_device *m)
4152 return seq_site_fini(env, mdt_seq_site(m));
4155 static int mdt_seq_init(const struct lu_env *env,
4157 struct mdt_device *m)
4159 struct seq_server_site *ss;
4164 ss = mdt_seq_site(m);
4167 * This is sequence-controller node. Init seq-controller server on local
4170 if (ss->ss_node_id == 0) {
4171 LASSERT(ss->ss_control_seq == NULL);
4173 OBD_ALLOC_PTR(ss->ss_control_seq);
4174 if (ss->ss_control_seq == NULL)
4177 rc = seq_server_init(ss->ss_control_seq,
4178 m->mdt_bottom, uuid,
4179 LUSTRE_SEQ_CONTROLLER,
4184 GOTO(out_seq_fini, rc);
4186 OBD_ALLOC_PTR(ss->ss_client_seq);
4187 if (ss->ss_client_seq == NULL)
4188 GOTO(out_seq_fini, rc = -ENOMEM);
4190 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4191 if (prefix == NULL) {
4192 OBD_FREE_PTR(ss->ss_client_seq);
4193 GOTO(out_seq_fini, rc = -ENOMEM);
4196 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4200 * Init seq-controller client after seq-controller server is
4201 * ready. Pass ss->ss_control_seq to it for direct talking.
4203 rc = seq_client_init(ss->ss_client_seq, NULL,
4204 LUSTRE_SEQ_METADATA, prefix,
4205 ss->ss_control_seq);
4206 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4209 GOTO(out_seq_fini, rc);
4212 /* Init seq-server on local MDT */
4213 LASSERT(ss->ss_server_seq == NULL);
4215 OBD_ALLOC_PTR(ss->ss_server_seq);
4216 if (ss->ss_server_seq == NULL)
4217 GOTO(out_seq_fini, rc = -ENOMEM);
4219 rc = seq_server_init(ss->ss_server_seq,
4220 m->mdt_bottom, uuid,
4225 GOTO(out_seq_fini, rc = -ENOMEM);
4227 /* Assign seq-controller client to local seq-server. */
4228 if (ss->ss_node_id == 0) {
4229 LASSERT(ss->ss_client_seq != NULL);
4231 rc = seq_server_set_cli(ss->ss_server_seq,
4239 mdt_seq_fini(env, m);
4247 static int mdt_fld_fini(const struct lu_env *env,
4248 struct mdt_device *m)
4250 struct seq_server_site *ss = mdt_seq_site(m);
4253 if (ss && ss->ss_server_fld) {
4254 fld_server_fini(env, ss->ss_server_fld);
4255 OBD_FREE_PTR(ss->ss_server_fld);
4256 ss->ss_server_fld = NULL;
4262 static int mdt_fld_init(const struct lu_env *env,
4264 struct mdt_device *m)
4266 struct seq_server_site *ss;
4270 ss = mdt_seq_site(m);
4272 OBD_ALLOC_PTR(ss->ss_server_fld);
4273 if (ss->ss_server_fld == NULL)
4274 RETURN(rc = -ENOMEM);
4276 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4277 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4279 OBD_FREE_PTR(ss->ss_server_fld);
4280 ss->ss_server_fld = NULL;
4287 static void mdt_stack_pre_fini(const struct lu_env *env,
4288 struct mdt_device *m, struct lu_device *top)
4290 struct obd_device *obd;
4291 struct lustre_cfg_bufs *bufs;
4292 struct lustre_cfg *lcfg;
4293 struct mdt_thread_info *info;
4298 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4299 LASSERT(info != NULL);
4301 bufs = &info->mti_u.bufs;
4303 LASSERT(m->mdt_child_exp);
4304 LASSERT(m->mdt_child_exp->exp_obd);
4305 obd = m->mdt_child_exp->exp_obd;
4307 /* process cleanup, pass mdt obd name to get obd umount flags */
4308 /* XXX: this is needed because all layers are referenced by
4309 * objects (some of them are pinned by osd, for example *
4310 * the proper solution should be a model where object used
4311 * by osd only doesn't have mdt/mdd slices -bzzz */
4312 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4313 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4314 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4316 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4319 top->ld_ops->ldo_process_config(env, top, lcfg);
4320 lustre_cfg_free(lcfg);
4324 static void mdt_stack_fini(const struct lu_env *env,
4325 struct mdt_device *m, struct lu_device *top)
4327 struct obd_device *obd = mdt2obd_dev(m);
4328 struct lustre_cfg_bufs *bufs;
4329 struct lustre_cfg *lcfg;
4330 struct mdt_thread_info *info;
4334 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4335 LASSERT(info != NULL);
4337 lu_dev_del_linkage(top->ld_site, top);
4339 lu_site_purge(env, top->ld_site, -1);
4341 bufs = &info->mti_u.bufs;
4342 /* process cleanup, pass mdt obd name to get obd umount flags */
4343 /* another purpose is to let all layers to release their objects */
4344 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4349 lustre_cfg_bufs_set_string(bufs, 1, flags);
4350 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4352 CERROR("Cannot alloc lcfg!\n");
4356 top->ld_ops->ldo_process_config(env, top, lcfg);
4357 lustre_cfg_free(lcfg);
4359 lu_site_purge(env, top->ld_site, -1);
4361 m->mdt_child = NULL;
4362 m->mdt_bottom = NULL;
4364 obd_disconnect(m->mdt_child_exp);
4365 m->mdt_child_exp = NULL;
4367 obd_disconnect(m->mdt_bottom_exp);
4368 m->mdt_child_exp = NULL;
4371 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4372 const char *next, struct obd_export **exp)
4374 struct obd_connect_data *data = NULL;
4375 struct obd_device *obd;
4379 OBD_ALLOC_PTR(data);
4381 GOTO(out, rc = -ENOMEM);
4383 obd = class_name2obd(next);
4385 CERROR("%s: can't locate next device: %s\n",
4386 mdt_obd_name(m), next);
4387 GOTO(out, rc = -ENOTCONN);
4390 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4391 data->ocd_version = LUSTRE_VERSION_CODE;
4393 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4395 CERROR("%s: cannot connect to next dev %s (%d)\n",
4396 mdt_obd_name(m), next, rc);
4406 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4407 struct lustre_cfg *cfg)
4409 char *dev = lustre_cfg_string(cfg, 0);
4410 int rc, name_size, uuid_size;
4411 char *name, *uuid, *p;
4412 struct lustre_cfg_bufs *bufs;
4413 struct lustre_cfg *lcfg;
4414 struct obd_device *obd;
4415 struct lustre_profile *lprof;
4416 struct lu_site *site;
4419 /* in 1.8 we had the only device in the stack - MDS.
4420 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4421 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4422 * to generate names and setup MDT, MDD. MDT will be using
4423 * generated name to connect to MDD. for MDD the next device
4424 * will be LOD with name taken from so called "profile" which
4425 * is generated by mount_option line
4427 * 1.8 MGS generates config. commands like this:
4428 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4429 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4430 * 2.0 MGS generates config. commands like this:
4431 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4432 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4433 * 3:lustre-MDT0000-mdtlov 4:f
4435 * we generate MDD name from MDT one, just replacing T with D
4437 * after all the preparations, the logical equivalent will be
4438 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4439 * 3:lustre-MDT0000-mdtlov 4:f
4440 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4441 * 3:lustre-MDD0000 4:f
4443 * notice we build the stack from down to top: MDD first, then MDT */
4445 name_size = MAX_OBD_NAME;
4446 uuid_size = MAX_OBD_NAME;
4448 OBD_ALLOC(name, name_size);
4449 OBD_ALLOC(uuid, uuid_size);
4450 if (name == NULL || uuid == NULL)
4451 GOTO(cleanup_mem, rc = -ENOMEM);
4453 OBD_ALLOC_PTR(bufs);
4455 GOTO(cleanup_mem, rc = -ENOMEM);
4458 p = strstr(name, "-MDT");
4460 GOTO(cleanup_mem, rc = -ENOMEM);
4463 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4465 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4466 if (lprof == NULL || lprof->lp_dt == NULL) {
4467 CERROR("can't find the profile: %s\n",
4468 lustre_cfg_string(cfg, 0));
4469 GOTO(cleanup_mem, rc = -EINVAL);
4472 lustre_cfg_bufs_reset(bufs, name);
4473 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4474 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4475 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4477 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4479 GOTO(free_bufs, rc = -ENOMEM);
4481 rc = class_attach(lcfg);
4483 GOTO(lcfg_cleanup, rc);
4485 obd = class_name2obd(name);
4487 CERROR("Can not find obd %s (%s in config)\n",
4488 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4489 GOTO(class_detach, rc = -EINVAL);
4492 lustre_cfg_free(lcfg);
4494 lustre_cfg_bufs_reset(bufs, name);
4495 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4496 lustre_cfg_bufs_set_string(bufs, 2, dev);
4497 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4499 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4501 rc = class_setup(obd, lcfg);
4503 GOTO(class_detach, rc);
4505 /* connect to MDD we just setup */
4506 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4510 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4512 LASSERT(mdt_lu_site(mdt) == NULL);
4513 mdt->mdt_lu_dev.ld_site = site;
4514 site->ls_top_dev = &mdt->mdt_lu_dev;
4515 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4518 /* now connect to bottom OSD */
4519 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4520 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4524 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4527 rc = lu_env_refill((struct lu_env *)env);
4529 CERROR("Failure to refill session: '%d'\n", rc);
4531 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4536 class_detach(obd, lcfg);
4538 lustre_cfg_free(lcfg);
4543 OBD_FREE(name, name_size);
4545 OBD_FREE(uuid, uuid_size);
4549 /* setup quota master target on MDT0 */
4550 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4551 struct lustre_cfg *cfg)
4553 struct obd_device *obd;
4554 char *dev = lustre_cfg_string(cfg, 0);
4555 char *qmtname, *uuid, *p;
4556 struct lustre_cfg_bufs *bufs;
4557 struct lustre_cfg *lcfg;
4558 struct lustre_profile *lprof;
4559 struct obd_connect_data *data;
4563 LASSERT(mdt->mdt_qmt_exp == NULL);
4564 LASSERT(mdt->mdt_qmt_dev == NULL);
4566 /* quota master is on MDT0 only for now */
4567 if (mdt->mdt_seq_site.ss_node_id != 0)
4570 /* MGS generates config commands which look as follows:
4571 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4572 * 3:lustre-MDT0000-mdtlov 4:f
4574 * We generate the QMT name from the MDT one, just replacing MD with QM
4575 * after all the preparations, the logical equivalent will be:
4576 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4577 * 3:lustre-MDT0000-osd 4:f */
4578 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4579 OBD_ALLOC(uuid, UUID_MAX);
4580 OBD_ALLOC_PTR(bufs);
4581 OBD_ALLOC_PTR(data);
4582 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4583 GOTO(cleanup_mem, rc = -ENOMEM);
4585 strcpy(qmtname, dev);
4586 p = strstr(qmtname, "-MDT");
4588 GOTO(cleanup_mem, rc = -ENOMEM);
4589 /* replace MD with QM */
4593 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4595 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4596 if (lprof == NULL || lprof->lp_dt == NULL) {
4597 CERROR("can't find profile for %s\n",
4598 lustre_cfg_string(cfg, 0));
4599 GOTO(cleanup_mem, rc = -EINVAL);
4602 lustre_cfg_bufs_reset(bufs, qmtname);
4603 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4604 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4605 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4607 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4609 GOTO(cleanup_mem, rc = -ENOMEM);
4611 rc = class_attach(lcfg);
4613 GOTO(lcfg_cleanup, rc);
4615 obd = class_name2obd(qmtname);
4617 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4618 lustre_cfg_string(cfg, 0));
4619 GOTO(class_detach, rc = -EINVAL);
4622 lustre_cfg_free(lcfg);
4624 lustre_cfg_bufs_reset(bufs, qmtname);
4625 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4626 lustre_cfg_bufs_set_string(bufs, 2, dev);
4628 /* for quota, the next device should be the OSD device */
4629 lustre_cfg_bufs_set_string(bufs, 3,
4630 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4632 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4634 rc = class_setup(obd, lcfg);
4636 GOTO(class_detach, rc);
4638 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4640 /* configure local quota objects */
4641 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4645 GOTO(class_cleanup, rc);
4647 /* connect to quota master target */
4648 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4649 data->ocd_version = LUSTRE_VERSION_CODE;
4650 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4653 CERROR("cannot connect to quota master device %s (%d)\n",
4655 GOTO(class_cleanup, rc);
4661 class_manual_cleanup(obd);
4662 mdt->mdt_qmt_dev = NULL;
4666 class_detach(obd, lcfg);
4668 lustre_cfg_free(lcfg);
4673 OBD_FREE(qmtname, MAX_OBD_NAME);
4675 OBD_FREE(uuid, UUID_MAX);
4681 /* Shutdown quota master target associated with mdt */
4682 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4686 if (mdt->mdt_qmt_exp == NULL)
4688 LASSERT(mdt->mdt_qmt_dev != NULL);
4690 /* the qmt automatically shuts down when the mdt disconnects */
4691 obd_disconnect(mdt->mdt_qmt_exp);
4692 mdt->mdt_qmt_exp = NULL;
4693 mdt->mdt_qmt_dev = NULL;
4697 static struct tgt_handler mdt_tgt_handlers[] = {
4698 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4699 0, MDS_CONNECT, mdt_tgt_connect,
4700 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4701 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4702 0, MDS_DISCONNECT, tgt_disconnect,
4703 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4706 static struct tgt_opc_slice mdt_common_slice[] = {
4708 .tos_opc_start = MDS_FIRST_OPC,
4709 .tos_opc_end = MDS_LAST_OPC,
4710 .tos_hs = mdt_tgt_handlers
4713 .tos_opc_start = OBD_FIRST_OPC,
4714 .tos_opc_end = OBD_LAST_OPC,
4715 .tos_hs = tgt_obd_handlers
4718 .tos_opc_start = LDLM_FIRST_OPC,
4719 .tos_opc_end = LDLM_LAST_OPC,
4720 .tos_hs = tgt_dlm_handlers
4723 .tos_opc_start = SEC_FIRST_OPC,
4724 .tos_opc_end = SEC_LAST_OPC,
4725 .tos_hs = tgt_sec_ctx_handlers
4728 .tos_opc_start = UPDATE_OBJ,
4729 .tos_opc_end = UPDATE_LAST_OPC,
4730 .tos_hs = tgt_out_handlers
4733 .tos_opc_start = FLD_FIRST_OPC,
4734 .tos_opc_end = FLD_LAST_OPC,
4735 .tos_hs = fld_handlers
4738 .tos_opc_start = SEQ_FIRST_OPC,
4739 .tos_opc_end = SEQ_LAST_OPC,
4740 .tos_hs = seq_handlers
4747 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4749 struct md_device *next = m->mdt_child;
4750 struct lu_device *d = &m->mdt_lu_dev;
4751 struct obd_device *obd = mdt2obd_dev(m);
4754 target_recovery_fini(obd);
4756 ping_evictor_stop();
4758 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4760 if (m->mdt_opts.mo_coordinator)
4761 mdt_hsm_cdt_stop(m);
4763 mdt_hsm_cdt_fini(m);
4765 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4766 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4767 obd_exports_barrier(obd);
4768 obd_zombie_barrier();
4772 tgt_fini(env, &m->mdt_lut);
4773 mdt_fs_cleanup(env, m);
4774 upcall_cache_cleanup(m->mdt_identity_cache);
4775 m->mdt_identity_cache = NULL;
4777 if (m->mdt_namespace != NULL) {
4778 ldlm_namespace_free(m->mdt_namespace, NULL,
4779 d->ld_obd->obd_force);
4780 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4783 mdt_quota_fini(env, m);
4785 cfs_free_nidlist(&m->mdt_nosquash_nids);
4786 if (m->mdt_nosquash_str) {
4787 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4788 m->mdt_nosquash_str = NULL;
4789 m->mdt_nosquash_strlen = 0;
4792 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4794 mdt_seq_fini(env, m);
4795 mdt_fld_fini(env, m);
4796 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4798 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4799 cfs_timer_disarm(&m->mdt_ck_timer);
4800 mdt_ck_thread_stop(m);
4805 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4807 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4809 server_put_mount(mdt_obd_name(m), NULL);
4814 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4816 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4817 struct sptlrpc_rule_set tmp_rset;
4820 sptlrpc_rule_set_init(&tmp_rset);
4821 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4823 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4824 mdt_obd_name(m), rc);
4828 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4830 write_lock(&m->mdt_sptlrpc_lock);
4831 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4832 m->mdt_sptlrpc_rset = tmp_rset;
4833 write_unlock(&m->mdt_sptlrpc_lock);
4838 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4840 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4841 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4843 struct mdt_thread_info *info;
4844 struct obd_device *obd;
4845 const char *dev = lustre_cfg_string(cfg, 0);
4846 const char *num = lustre_cfg_string(cfg, 2);
4847 struct lustre_mount_info *lmi = NULL;
4848 struct lustre_sb_info *lsi;
4850 struct seq_server_site *ss_site;
4851 const char *identity_upcall = "NONE";
4852 struct md_device *next;
4858 lu_device_init(&m->mdt_lu_dev, ldt);
4860 * Environment (env) might be missing mdt_thread_key values at that
4861 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4864 * Usually device allocation path doesn't use module key values, but
4865 * mdt has to do a lot of work here, so allocate key value.
4867 rc = lu_env_refill((struct lu_env *)env);
4871 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4872 LASSERT(info != NULL);
4874 obd = class_name2obd(dev);
4875 LASSERT(obd != NULL);
4877 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4879 m->mdt_som_conf = 0;
4881 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4883 /* default is coordinator off, it is started through conf_param
4885 m->mdt_opts.mo_coordinator = 0;
4887 lmi = server_get_mount(dev);
4889 CERROR("Cannot get mount info for %s!\n", dev);
4892 lsi = s2lsi(lmi->lmi_sb);
4893 /* CMD is supported only in IAM mode */
4895 node_id = simple_strtol(num, NULL, 10);
4896 obd->u.obt.obt_magic = OBT_MAGIC;
4899 rwlock_init(&m->mdt_sptlrpc_lock);
4900 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4902 spin_lock_init(&m->mdt_ioepoch_lock);
4903 m->mdt_opts.mo_compat_resname = 0;
4904 m->mdt_opts.mo_mds_capa = 1;
4905 m->mdt_opts.mo_oss_capa = 1;
4906 m->mdt_capa_timeout = CAPA_TIMEOUT;
4907 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4908 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4909 m->mdt_squash_uid = 0;
4910 m->mdt_squash_gid = 0;
4911 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4912 m->mdt_nosquash_str = NULL;
4913 m->mdt_nosquash_strlen = 0;
4914 init_rwsem(&m->mdt_squash_sem);
4915 spin_lock_init(&m->mdt_osfs_lock);
4916 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4917 m->mdt_enable_remote_dir = 0;
4918 m->mdt_enable_remote_dir_gid = 0;
4920 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4921 m->mdt_lu_dev.ld_obd = obd;
4922 /* Set this lu_device to obd for error handling purposes. */
4923 obd->obd_lu_dev = &m->mdt_lu_dev;
4925 /* init the stack */
4926 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4928 CERROR("%s: Can't init device stack, rc %d\n",
4929 mdt_obd_name(m), rc);
4934 ss_site = mdt_seq_site(m);
4935 s->ld_seq_site = ss_site;
4938 /* set server index */
4939 ss_site->ss_node_id = node_id;
4941 /* failover is the default
4942 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4943 * assumed whose ss_node_id == 0 XXX
4945 obd->obd_replayable = 1;
4946 /* No connection accepted until configurations will finish */
4947 obd->obd_no_conn = 1;
4949 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4950 char *str = lustre_cfg_string(cfg, 4);
4951 if (strchr(str, 'n')) {
4952 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4953 obd->obd_replayable = 0;
4957 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4959 GOTO(err_fini_stack, rc);
4961 rc = mdt_seq_init(env, mdt_obd_name(m), m);
4963 GOTO(err_fini_fld, rc);
4965 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4966 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4967 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4968 LDLM_NAMESPACE_SERVER,
4969 LDLM_NAMESPACE_GREEDY,
4971 if (m->mdt_namespace == NULL)
4972 GOTO(err_fini_seq, rc = -ENOMEM);
4974 m->mdt_namespace->ns_lvbp = m;
4975 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4977 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4978 /* set obd_namespace for compatibility with old code */
4979 obd->obd_namespace = m->mdt_namespace;
4981 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4983 rc = mdt_hsm_cdt_init(m);
4985 CERROR("%s: error initializing coordinator, rc %d\n",
4986 mdt_obd_name(m), rc);
4987 GOTO(err_free_ns, rc);
4990 rc = mdt_ck_thread_start(m);
4992 GOTO(err_free_hsm, rc);
4994 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4995 OBD_FAIL_MDS_ALL_REQUEST_NET,
4996 OBD_FAIL_MDS_ALL_REPLY_NET);
5000 rc = mdt_fs_setup(env, m, obd, lsi);
5004 mdt_adapt_sptlrpc_conf(obd, 1);
5006 next = m->mdt_child;
5007 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5010 GOTO(err_fs_cleanup, rc);
5012 if (mntopts & MNTOPT_USERXATTR)
5013 m->mdt_opts.mo_user_xattr = 1;
5015 m->mdt_opts.mo_user_xattr = 0;
5017 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
5019 GOTO(err_fs_cleanup, rc);
5021 if (mntopts & MNTOPT_ACL)
5022 m->mdt_opts.mo_acl = 1;
5024 m->mdt_opts.mo_acl = 0;
5026 /* XXX: to support suppgid for ACL, we enable identity_upcall
5027 * by default, otherwise, maybe got unexpected -EACCESS. */
5028 if (m->mdt_opts.mo_acl)
5029 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5031 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5033 &mdt_identity_upcall_cache_ops);
5034 if (IS_ERR(m->mdt_identity_cache)) {
5035 rc = PTR_ERR(m->mdt_identity_cache);
5036 m->mdt_identity_cache = NULL;
5037 GOTO(err_fs_cleanup, rc);
5040 rc = mdt_procfs_init(m, dev);
5042 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5043 GOTO(err_recovery, rc);
5046 rc = mdt_quota_init(env, m, cfg);
5048 GOTO(err_procfs, rc);
5050 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5051 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5052 "mdt_ldlm_client", m->mdt_ldlm_client);
5054 ping_evictor_start();
5056 /* recovery will be started upon mdt_prepare()
5057 * when the whole stack is complete and ready
5058 * to serve the requests */
5060 mdt_init_capa_ctxt(env, m);
5062 /* Reduce the initial timeout on an MDS because it doesn't need such
5063 * a long timeout as an OST does. Adaptive timeouts will adjust this
5064 * value appropriately. */
5065 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5066 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5072 target_recovery_fini(obd);
5073 upcall_cache_cleanup(m->mdt_identity_cache);
5074 m->mdt_identity_cache = NULL;
5076 mdt_fs_cleanup(env, m);
5078 tgt_fini(env, &m->mdt_lut);
5080 cfs_timer_disarm(&m->mdt_ck_timer);
5081 mdt_ck_thread_stop(m);
5083 mdt_hsm_cdt_fini(m);
5085 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5086 obd->obd_namespace = m->mdt_namespace = NULL;
5088 mdt_seq_fini(env, m);
5090 mdt_fld_fini(env, m);
5092 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5095 server_put_mount(dev, lmi->lmi_mnt);
5099 /* For interoperability, the left element is old parameter, the right one
5100 * is the new version of the parameter, if some parameter is deprecated,
5101 * the new version should be set as NULL. */
5102 static struct cfg_interop_param mdt_interop_param[] = {
5103 { "mdt.group_upcall", NULL },
5104 { "mdt.quota_type", NULL },
5105 { "mdd.quota_type", NULL },
5106 { "mdt.rootsquash", "mdt.root_squash" },
5107 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5111 /* used by MGS to process specific configurations */
5112 static int mdt_process_config(const struct lu_env *env,
5113 struct lu_device *d, struct lustre_cfg *cfg)
5115 struct mdt_device *m = mdt_dev(d);
5116 struct md_device *md_next = m->mdt_child;
5117 struct lu_device *next = md2lu_dev(md_next);
5121 switch (cfg->lcfg_command) {
5123 struct lprocfs_static_vars lvars;
5124 struct obd_device *obd = d->ld_obd;
5126 /* For interoperability */
5127 struct cfg_interop_param *ptr = NULL;
5128 struct lustre_cfg *old_cfg = NULL;
5131 param = lustre_cfg_string(cfg, 1);
5132 if (param == NULL) {
5133 CERROR("param is empty\n");
5138 ptr = class_find_old_param(param, mdt_interop_param);
5140 if (ptr->new_param == NULL) {
5142 CWARN("For interoperability, skip this %s."
5143 " It is obsolete.\n", ptr->old_param);
5147 CWARN("Found old param %s, changed it to %s.\n",
5148 ptr->old_param, ptr->new_param);
5151 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5158 lprocfs_mdt_init_vars(&lvars);
5159 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5161 if (rc > 0 || rc == -ENOSYS)
5162 /* we don't understand; pass it on */
5163 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5165 if (old_cfg != NULL)
5166 lustre_cfg_free(cfg);
5171 /* others are passed further */
5172 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5178 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5179 const struct lu_object_header *hdr,
5180 struct lu_device *d)
5182 struct mdt_object *mo;
5186 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
5188 struct lu_object *o;
5189 struct lu_object_header *h;
5192 h = &mo->mot_header;
5193 lu_object_header_init(h);
5194 lu_object_init(o, h, d);
5195 lu_object_add_top(h, o);
5196 o->lo_ops = &mdt_obj_ops;
5197 mutex_init(&mo->mot_ioepoch_mutex);
5198 mutex_init(&mo->mot_lov_mutex);
5199 init_rwsem(&mo->mot_open_sem);
5200 init_rwsem(&mo->mot_xattr_sem);
5206 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5207 const struct lu_object_conf *unused)
5209 struct mdt_device *d = mdt_dev(o->lo_dev);
5210 struct lu_device *under;
5211 struct lu_object *below;
5215 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5216 PFID(lu_object_fid(o)));
5218 under = &d->mdt_child->md_lu_dev;
5219 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5220 if (below != NULL) {
5221 lu_object_add(o, below);
5228 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5230 struct mdt_object *mo = mdt_obj(o);
5231 struct lu_object_header *h;
5235 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5236 PFID(lu_object_fid(o)));
5238 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5239 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5242 lu_object_header_fini(h);
5243 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5248 static int mdt_object_print(const struct lu_env *env, void *cookie,
5249 lu_printer_t p, const struct lu_object *o)
5251 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5252 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5253 "flags="LPX64", epochcount=%d, writecount=%d)",
5254 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5255 mdto->mot_ioepoch_count, mdto->mot_writecount);
5258 static int mdt_prepare(const struct lu_env *env,
5259 struct lu_device *pdev,
5260 struct lu_device *cdev)
5262 struct mdt_device *mdt = mdt_dev(cdev);
5263 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5264 struct obd_device *obd = cdev->ld_obd;
5265 struct lfsck_start_param lsp;
5272 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5276 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5280 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5284 lsp.lsp_start = NULL;
5285 lsp.lsp_namespace = mdt->mdt_namespace;
5286 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5287 OBD_IOC_START_LFSCK,
5290 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5291 mdt_obd_name(mdt), rc);
5295 if (mdt->mdt_seq_site.ss_node_id == 0) {
5296 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5297 &mdt->mdt_md_root_fid);
5302 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5303 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5304 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5305 LASSERT(obd->obd_no_conn);
5306 spin_lock(&obd->obd_dev_lock);
5307 obd->obd_no_conn = 0;
5308 spin_unlock(&obd->obd_dev_lock);
5310 if (obd->obd_recovering == 0)
5311 mdt_postrecov(env, mdt);
5316 const struct lu_device_operations mdt_lu_ops = {
5317 .ldo_object_alloc = mdt_object_alloc,
5318 .ldo_process_config = mdt_process_config,
5319 .ldo_prepare = mdt_prepare,
5322 static const struct lu_object_operations mdt_obj_ops = {
5323 .loo_object_init = mdt_object_init,
5324 .loo_object_free = mdt_object_free,
5325 .loo_object_print = mdt_object_print
5328 static int mdt_obd_set_info_async(const struct lu_env *env,
5329 struct obd_export *exp,
5330 __u32 keylen, void *key,
5331 __u32 vallen, void *val,
5332 struct ptlrpc_request_set *set)
5334 struct obd_device *obd = exp->exp_obd;
5340 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5341 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5349 * Match client and server connection feature flags.
5351 * Compute the compatibility flags for a connection request based on
5352 * features mutually supported by client and server.
5354 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5355 * must not be updated here, otherwise a partially initialized value may
5356 * be exposed. After the connection request is successfully processed,
5357 * the top-level MDT connect request handler atomically updates the export
5358 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5359 * reply. \see mdt_connect().
5361 * \param exp the obd_export associated with this client/target pair
5362 * \param mdt the target device for the connection
5363 * \param data stores data for this connect request
5366 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5367 * \retval -EBADE client and server feature requirements are incompatible
5369 static int mdt_connect_internal(struct obd_export *exp,
5370 struct mdt_device *mdt,
5371 struct obd_connect_data *data)
5373 LASSERT(data != NULL);
5375 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5376 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5378 /* If no known bits (which should not happen, probably,
5379 as everybody should support LOOKUP and UPDATE bits at least)
5380 revert to compat mode with plain locks. */
5381 if (!data->ocd_ibits_known &&
5382 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5383 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5385 if (!mdt->mdt_opts.mo_acl)
5386 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5388 if (!mdt->mdt_opts.mo_user_xattr)
5389 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5391 if (!mdt->mdt_som_conf)
5392 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5394 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5395 data->ocd_brw_size = min(data->ocd_brw_size,
5396 (__u32)MD_MAX_BRW_SIZE);
5397 if (data->ocd_brw_size == 0) {
5398 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5399 " ocd_version: %x ocd_grant: %d "
5400 "ocd_index: %u ocd_brw_size is "
5401 "unexpectedly zero, network data "
5402 "corruption? Refusing connection of this"
5405 exp->exp_client_uuid.uuid,
5406 exp, data->ocd_connect_flags, data->ocd_version,
5407 data->ocd_grant, data->ocd_index);
5412 /* NB: Disregard the rule against updating
5413 * exp_connect_data.ocd_connect_flags in this case, since
5414 * tgt_client_new() needs to know if this is a lightweight
5415 * connection, and it is safe to expose this flag before
5416 * connection processing completes. */
5417 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5418 spin_lock(&exp->exp_lock);
5419 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5420 spin_unlock(&exp->exp_lock);
5423 data->ocd_version = LUSTRE_VERSION_CODE;
5424 exp->exp_connect_data = *data;
5425 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5427 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5428 CWARN("%s: MDS requires FID support, but client not\n",
5433 if (mdt->mdt_som_conf &&
5434 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5435 OBD_CONNECT_MDS_MDS |
5436 OBD_CONNECT_SOM))) {
5437 CWARN("%s: MDS has SOM enabled, but client does not support "
5438 "it\n", mdt_obd_name(mdt));
5442 if (OCD_HAS_FLAG(data, PINGLESS)) {
5443 if (ptlrpc_pinger_suppress_pings()) {
5444 spin_lock(&exp->exp_obd->obd_dev_lock);
5445 list_del_init(&exp->exp_obd_chain_timed);
5446 spin_unlock(&exp->exp_obd->obd_dev_lock);
5448 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5452 data->ocd_max_easize = mdt->mdt_max_ea_size;
5457 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
5458 struct ptlrpc_request *req)
5460 struct sptlrpc_flavor flvr;
5463 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5464 read_lock(&mdt->mdt_sptlrpc_lock);
5465 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5469 read_unlock(&mdt->mdt_sptlrpc_lock);
5471 spin_lock(&exp->exp_lock);
5473 exp->exp_sp_peer = req->rq_sp_from;
5474 exp->exp_flvr = flvr;
5476 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5477 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5478 CERROR("unauthorized rpc flavor %x from %s, "
5479 "expect %x\n", req->rq_flvr.sf_rpc,
5480 libcfs_nid2str(req->rq_peer.nid),
5481 exp->exp_flvr.sf_rpc);
5485 spin_unlock(&exp->exp_lock);
5487 if (exp->exp_sp_peer != req->rq_sp_from) {
5488 CERROR("RPC source %s doesn't match %s\n",
5489 sptlrpc_part2name(req->rq_sp_from),
5490 sptlrpc_part2name(exp->exp_sp_peer));
5493 rc = sptlrpc_target_export_check(exp, req);
5500 /* mds_connect copy */
5501 static int mdt_obd_connect(const struct lu_env *env,
5502 struct obd_export **exp, struct obd_device *obd,
5503 struct obd_uuid *cluuid,
5504 struct obd_connect_data *data,
5507 struct obd_export *lexp;
5508 struct lustre_handle conn = { 0 };
5509 struct mdt_device *mdt;
5513 LASSERT(env != NULL);
5514 if (!exp || !obd || !cluuid)
5517 mdt = mdt_dev(obd->obd_lu_dev);
5520 * first, check whether the stack is ready to handle requests
5521 * XXX: probably not very appropriate method is used now
5522 * at some point we should find a better one
5524 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5525 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5526 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5529 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5532 rc = class_connect(&conn, obd, cluuid);
5536 lexp = class_conn2export(&conn);
5537 LASSERT(lexp != NULL);
5539 rc = mdt_connect_internal(lexp, mdt, data);
5541 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5544 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5545 rc = tgt_client_new(env, lexp);
5547 mdt_export_stats_init(obd, lexp, localdata);
5551 class_disconnect(lexp);
5560 static int mdt_obd_reconnect(const struct lu_env *env,
5561 struct obd_export *exp, struct obd_device *obd,
5562 struct obd_uuid *cluuid,
5563 struct obd_connect_data *data,
5569 if (exp == NULL || obd == NULL || cluuid == NULL)
5572 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5574 mdt_export_stats_init(obd, exp, localdata);
5579 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5580 struct mdt_thread_info *info,
5581 struct mdt_file_data *mfd)
5583 struct lu_context ses;
5587 rc = lu_context_init(&ses, LCT_SESSION);
5592 lu_context_enter(&ses);
5594 mdt_ucred(info)->uc_valid = UCRED_OLD;
5595 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5597 lu_context_exit(&ses);
5598 lu_context_fini(&ses);
5604 static int mdt_export_cleanup(struct obd_export *exp)
5606 struct mdt_export_data *med = &exp->exp_mdt_data;
5607 struct obd_device *obd = exp->exp_obd;
5608 struct mdt_device *mdt;
5609 struct mdt_thread_info *info;
5611 CFS_LIST_HEAD(closing_list);
5612 struct mdt_file_data *mfd, *n;
5616 spin_lock(&med->med_open_lock);
5617 while (!cfs_list_empty(&med->med_open_head)) {
5618 cfs_list_t *tmp = med->med_open_head.next;
5619 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5621 /* Remove mfd handle so it can't be found again.
5622 * We are consuming the mfd_list reference here. */
5623 class_handle_unhash(&mfd->mfd_handle);
5624 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5626 spin_unlock(&med->med_open_lock);
5627 mdt = mdt_dev(obd->obd_lu_dev);
5628 LASSERT(mdt != NULL);
5630 rc = lu_env_init(&env, LCT_MD_THREAD);
5634 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5635 LASSERT(info != NULL);
5636 memset(info, 0, sizeof *info);
5637 info->mti_env = &env;
5638 info->mti_mdt = mdt;
5639 info->mti_exp = exp;
5641 if (!cfs_list_empty(&closing_list)) {
5642 struct md_attr *ma = &info->mti_attr;
5644 /* Close any open files (which may also cause orphan unlinking). */
5645 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5646 cfs_list_del_init(&mfd->mfd_list);
5647 ma->ma_need = ma->ma_valid = 0;
5649 /* This file is being closed due to an eviction, it
5650 * could have been modified and now dirty regarding to
5651 * HSM archive, check this!
5652 * The logic here is to mark a file dirty if there's a
5653 * chance it was dirtied before the client was evicted,
5654 * so that we don't have to wait for a release attempt
5655 * before finding out the file was actually dirty and
5656 * fail the release. Aggressively marking it dirty here
5657 * will cause the policy engine to attempt to
5658 * re-archive it; when rearchiving, we can compare the
5659 * current version to the HSM data_version and make the
5660 * archive request into a noop if it's not actually
5663 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5664 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5666 /* Don't unlink orphan on failover umount, LU-184 */
5667 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5668 ma->ma_valid = MA_FLAGS;
5669 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5671 mdt_mfd_close(info, mfd);
5674 info->mti_mdt = NULL;
5675 /* cleanup client slot early */
5676 /* Do not erase record for recoverable client. */
5677 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5678 tgt_client_del(&env, exp);
5684 static int mdt_obd_disconnect(struct obd_export *exp)
5690 class_export_get(exp);
5692 rc = server_disconnect_export(exp);
5694 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5696 rc = mdt_export_cleanup(exp);
5697 class_export_put(exp);
5701 /* FIXME: Can we avoid using these two interfaces? */
5702 static int mdt_init_export(struct obd_export *exp)
5704 struct mdt_export_data *med = &exp->exp_mdt_data;
5708 CFS_INIT_LIST_HEAD(&med->med_open_head);
5709 spin_lock_init(&med->med_open_lock);
5710 mutex_init(&med->med_idmap_mutex);
5711 med->med_idmap = NULL;
5712 spin_lock(&exp->exp_lock);
5713 exp->exp_connecting = 1;
5714 spin_unlock(&exp->exp_lock);
5716 /* self-export doesn't need client data and ldlm initialization */
5717 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5718 &exp->exp_client_uuid)))
5721 rc = tgt_client_alloc(exp);
5725 rc = ldlm_init_export(exp);
5732 tgt_client_free(exp);
5734 CERROR("%s: Failed to initialize export: rc = %d\n",
5735 exp->exp_obd->obd_name, rc);
5739 static int mdt_destroy_export(struct obd_export *exp)
5743 if (exp_connect_rmtclient(exp))
5744 mdt_cleanup_idmap(&exp->exp_mdt_data);
5746 target_destroy_export(exp);
5747 /* destroy can be called from failed obd_setup, so
5748 * checking uuid is safer than obd_self_export */
5749 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5750 &exp->exp_client_uuid)))
5753 ldlm_destroy_export(exp);
5754 tgt_client_free(exp);
5756 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5757 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5762 /** The maximum depth that fid2path() will search.
5763 * This is limited only because we want to store the fids for
5764 * historical path lookup purposes.
5766 #define MAX_PATH_DEPTH 100
5768 /** mdt_path() lookup structure. */
5769 struct path_lookup_info {
5770 __u64 pli_recno; /**< history point */
5771 __u64 pli_currec; /**< current record */
5772 struct lu_fid pli_fid;
5773 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5774 struct mdt_object *pli_mdt_obj;
5775 char *pli_path; /**< full path */
5777 int pli_linkno; /**< which hardlink to follow */
5778 int pli_fidcount; /**< number of \a pli_fids */
5781 static int mdt_links_read(struct mdt_thread_info *info,
5782 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5786 LASSERT(ldata->ld_buf->lb_buf != NULL);
5788 if (!mdt_object_exists(mdt_obj))
5791 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5792 ldata->ld_buf, XATTR_NAME_LINK);
5793 if (rc == -ERANGE) {
5794 /* Buf was too small, figure out what we need. */
5795 lu_buf_free(ldata->ld_buf);
5796 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5797 ldata->ld_buf, XATTR_NAME_LINK);
5800 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5801 if (ldata->ld_buf->lb_buf == NULL)
5803 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5804 ldata->ld_buf, XATTR_NAME_LINK);
5809 return linkea_init(ldata);
5812 static int mdt_path_current(struct mdt_thread_info *info,
5813 struct path_lookup_info *pli)
5815 struct mdt_device *mdt = info->mti_mdt;
5816 struct mdt_object *mdt_obj;
5817 struct link_ea_header *leh;
5818 struct link_ea_entry *lee;
5819 struct lu_name *tmpname = &info->mti_name;
5820 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5821 struct lu_buf *buf = &info->mti_big_buf;
5824 struct linkea_data ldata = { 0 };
5828 /* temp buffer for path element, the buffer will be finally freed
5829 * in mdt_thread_info_fini */
5830 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5831 if (buf->lb_buf == NULL)
5835 ptr = pli->pli_path + pli->pli_pathlen - 1;
5838 pli->pli_fidcount = 0;
5839 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5841 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5842 * so checking root_fid can only happen on MDT0. */
5843 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5844 &pli->pli_fids[pli->pli_fidcount])) {
5845 mdt_obj = mdt_object_find(info->mti_env, mdt,
5846 &pli->pli_fids[pli->pli_fidcount]);
5847 if (IS_ERR(mdt_obj))
5848 GOTO(out, rc = PTR_ERR(mdt_obj));
5849 if (mdt_object_remote(mdt_obj)) {
5850 mdt_object_put(info->mti_env, mdt_obj);
5851 GOTO(remote_out, rc = -EREMOTE);
5853 if (!mdt_object_exists(mdt_obj)) {
5854 mdt_object_put(info->mti_env, mdt_obj);
5855 GOTO(out, rc = -ENOENT);
5858 rc = mdt_links_read(info, mdt_obj, &ldata);
5859 mdt_object_put(info->mti_env, mdt_obj);
5864 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5865 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5866 /* If set, use link #linkno for path lookup, otherwise use
5867 link #0. Only do this for the final path element. */
5868 if (pli->pli_fidcount == 0 &&
5869 pli->pli_linkno < leh->leh_reccount) {
5871 for (count = 0; count < pli->pli_linkno; count++) {
5872 lee = (struct link_ea_entry *)
5873 ((char *)lee + reclen);
5874 linkea_entry_unpack(lee, &reclen, tmpname,
5877 if (pli->pli_linkno < leh->leh_reccount - 1)
5878 /* indicate to user there are more links */
5882 /* Pack the name in the end of the buffer */
5883 ptr -= tmpname->ln_namelen;
5884 if (ptr - 1 <= pli->pli_path)
5885 GOTO(out, rc = -EOVERFLOW);
5886 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5889 /* Store the parent fid for historic lookup */
5890 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5891 GOTO(out, rc = -EOVERFLOW);
5892 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5896 ptr++; /* skip leading / */
5897 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5904 /* Returns the full path to this fid, as of changelog record recno. */
5905 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5906 char *path, int pathlen, __u64 *recno, int *linkno,
5909 struct mdt_device *mdt = info->mti_mdt;
5910 struct path_lookup_info *pli;
5918 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5927 pli->pli_mdt_obj = obj;
5928 pli->pli_recno = *recno;
5929 pli->pli_path = path;
5930 pli->pli_pathlen = pathlen;
5931 pli->pli_linkno = *linkno;
5933 /* Retry multiple times in case file is being moved */
5934 while (tries-- && rc == -EAGAIN)
5935 rc = mdt_path_current(info, pli);
5937 /* return the last resolved fids to the client, so the client will
5938 * build the left path on another MDT for remote object */
5939 *fid = pli->pli_fids[pli->pli_fidcount];
5941 *recno = pli->pli_currec;
5942 /* Return next link index to caller */
5943 *linkno = pli->pli_linkno;
5950 static int mdt_fid2path(struct mdt_thread_info *info,
5951 struct getinfo_fid2path *fp)
5953 struct mdt_device *mdt = info->mti_mdt;
5954 struct mdt_object *obj;
5958 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5959 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5961 if (!fid_is_sane(&fp->gf_fid))
5964 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5965 CWARN("%s: "DFID" is invalid, sequence should be "
5966 ">= "LPX64"\n", mdt_obd_name(mdt),
5967 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5971 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5972 if (obj == NULL || IS_ERR(obj)) {
5973 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5978 if (mdt_object_remote(obj))
5980 else if (!mdt_object_exists(obj))
5986 mdt_object_put(info->mti_env, obj);
5987 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5988 PFID(&fp->gf_fid), rc);
5992 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5993 &fp->gf_linkno, &fp->gf_fid);
5995 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5996 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5998 mdt_object_put(info->mti_env, obj);
6003 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
6004 void *val, int vallen)
6006 struct getinfo_fid2path *fpout, *fpin;
6009 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6012 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6013 lustre_swab_fid2path(fpin);
6015 memcpy(fpout, fpin, sizeof(*fpin));
6016 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6019 rc = mdt_fid2path(info, fpout);
6023 int mdt_get_info(struct mdt_thread_info *info)
6025 struct ptlrpc_request *req = mdt_info_req(info);
6033 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
6035 CDEBUG(D_IOCTL, "No GETINFO key");
6038 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
6041 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
6042 if (vallen == NULL) {
6043 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
6047 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6049 rc = req_capsule_server_pack(info->mti_pill);
6050 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
6051 if (valout == NULL) {
6052 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
6056 if (KEY_IS(KEY_FID2PATH))
6057 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
6061 lustre_msg_set_status(req->rq_repmsg, rc);
6066 /* Pass the ioc down */
6067 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
6068 unsigned int cmd, int len, void *data)
6070 struct lu_context ioctl_session;
6071 struct md_device *next = mdt->mdt_child;
6075 rc = lu_context_init(&ioctl_session, LCT_SESSION);
6078 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
6079 lu_context_enter(&ioctl_session);
6080 env->le_ses = &ioctl_session;
6082 LASSERT(next->md_ops->mdo_iocontrol);
6083 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
6085 lu_context_exit(&ioctl_session);
6086 lu_context_fini(&ioctl_session);
6090 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6092 struct obd_ioctl_data *data = karg;
6095 struct mdt_object *obj;
6096 struct mdt_lock_handle *lh;
6100 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6101 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6104 fid = (struct lu_fid *)data->ioc_inlbuf1;
6106 if (!fid_is_sane(fid))
6109 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6111 lh = &mti->mti_lh[MDT_LH_PARENT];
6112 mdt_lock_reg_init(lh, LCK_CR);
6114 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6116 RETURN(PTR_ERR(obj));
6118 if (mdt_object_remote(obj)) {
6121 * before calling version get the correct MDS should be
6122 * fid, this is error to find remote object here
6124 CERROR("nonlocal object "DFID"\n", PFID(fid));
6125 } else if (!mdt_object_exists(obj)) {
6126 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6129 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6130 *(__u64 *)data->ioc_inlbuf2 = version;
6133 mdt_object_unlock_put(mti, obj, lh, 1);
6137 /* ioctls on obd dev */
6138 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6139 void *karg, void *uarg)
6142 struct obd_device *obd = exp->exp_obd;
6143 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6144 struct dt_device *dt = mdt->mdt_bottom;
6148 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6149 rc = lu_env_init(&env, LCT_MD_THREAD);
6155 rc = mdt_device_sync(&env, mdt);
6157 case OBD_IOC_SET_READONLY:
6158 rc = dt->dd_ops->dt_ro(&env, dt);
6160 case OBD_IOC_ABORT_RECOVERY:
6161 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6162 target_stop_recovery_thread(obd);
6165 case OBD_IOC_CHANGELOG_REG:
6166 case OBD_IOC_CHANGELOG_DEREG:
6167 case OBD_IOC_CHANGELOG_CLEAR:
6168 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6170 case OBD_IOC_START_LFSCK: {
6171 struct md_device *next = mdt->mdt_child;
6172 struct obd_ioctl_data *data = karg;
6173 struct lfsck_start_param lsp;
6175 if (unlikely(data == NULL)) {
6180 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6181 lsp.lsp_namespace = mdt->mdt_namespace;
6182 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6185 case OBD_IOC_STOP_LFSCK: {
6186 struct md_device *next = mdt->mdt_child;
6188 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
6191 case OBD_IOC_GET_OBJ_VERSION: {
6192 struct mdt_thread_info *mti;
6193 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6194 memset(mti, 0, sizeof *mti);
6195 mti->mti_env = &env;
6199 rc = mdt_ioc_version_get(mti, karg);
6204 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6205 mdt_obd_name(mdt), cmd, rc);
6212 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6214 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6218 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6222 int mdt_obd_postrecov(struct obd_device *obd)
6227 rc = lu_env_init(&env, LCT_MD_THREAD);
6230 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6235 static struct obd_ops mdt_obd_device_ops = {
6236 .o_owner = THIS_MODULE,
6237 .o_set_info_async = mdt_obd_set_info_async,
6238 .o_connect = mdt_obd_connect,
6239 .o_reconnect = mdt_obd_reconnect,
6240 .o_disconnect = mdt_obd_disconnect,
6241 .o_init_export = mdt_init_export,
6242 .o_destroy_export = mdt_destroy_export,
6243 .o_iocontrol = mdt_iocontrol,
6244 .o_postrecov = mdt_obd_postrecov,
6247 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6248 struct lu_device *d)
6250 struct mdt_device *m = mdt_dev(d);
6257 static struct lu_device *mdt_device_free(const struct lu_env *env,
6258 struct lu_device *d)
6260 struct mdt_device *m = mdt_dev(d);
6263 lu_device_fini(&m->mdt_lu_dev);
6269 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6270 struct lu_device_type *t,
6271 struct lustre_cfg *cfg)
6273 struct lu_device *l;
6274 struct mdt_device *m;
6281 rc = mdt_init0(env, m, t, cfg);
6283 mdt_device_free(env, l);
6288 l = ERR_PTR(-ENOMEM);
6292 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6293 LU_KEY_INIT(mdt, struct mdt_thread_info);
6295 static void mdt_key_fini(const struct lu_context *ctx,
6296 struct lu_context_key *key, void* data)
6298 struct mdt_thread_info *info = data;
6300 if (info->mti_big_lmm) {
6301 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6302 info->mti_big_lmm = NULL;
6303 info->mti_big_lmmsize = 0;
6308 /* context key: mdt_thread_key */
6309 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6311 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6313 return lu_ucred(info->mti_env);
6316 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6318 return lu_ucred_check(info->mti_env);
6322 * Enable/disable COS (Commit On Sharing).
6324 * Set/Clear the COS flag in mdt options.
6326 * \param mdt mdt device
6327 * \param val 0 disables COS, other values enable COS
6329 void mdt_enable_cos(struct mdt_device *mdt, int val)
6334 mdt->mdt_opts.mo_cos = !!val;
6335 rc = lu_env_init(&env, LCT_LOCAL);
6336 if (unlikely(rc != 0)) {
6337 CWARN("lu_env initialization failed with rc = %d,"
6338 "cannot sync\n", rc);
6341 mdt_device_sync(&env, mdt);
6346 * Check COS (Commit On Sharing) status.
6348 * Return COS flag status.
6350 * \param mdt mdt device
6352 int mdt_cos_is_enabled(struct mdt_device *mdt)
6354 return mdt->mdt_opts.mo_cos != 0;
6357 static struct lu_device_type_operations mdt_device_type_ops = {
6358 .ldto_device_alloc = mdt_device_alloc,
6359 .ldto_device_free = mdt_device_free,
6360 .ldto_device_fini = mdt_device_fini
6363 static struct lu_device_type mdt_device_type = {
6364 .ldt_tags = LU_DEVICE_MD,
6365 .ldt_name = LUSTRE_MDT_NAME,
6366 .ldt_ops = &mdt_device_type_ops,
6367 .ldt_ctx_tags = LCT_MD_THREAD
6370 static int __init mdt_mod_init(void)
6372 struct lprocfs_static_vars lvars;
6375 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6376 FID_NOBRACE_LEN + 1);
6377 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6379 rc = lu_kmem_init(mdt_caches);
6383 rc = mds_mod_init();
6387 lprocfs_mdt_init_vars(&lvars);
6388 rc = class_register_type(&mdt_obd_device_ops, NULL,
6389 lvars.module_vars, LUSTRE_MDT_NAME,
6395 lu_kmem_fini(mdt_caches);
6402 static void __exit mdt_mod_exit(void)
6404 class_unregister_type(LUSTRE_MDT_NAME);
6406 lu_kmem_fini(mdt_caches);
6409 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6410 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6411 MODULE_LICENSE("GPL");
6413 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);