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;
142 tgt_opdata_clear(info->mti_env, flag);
145 rep->lock_policy_res1 &= ~flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, int flag)
152 info->mti_opdata |= flag;
153 tgt_opdata_set(info->mti_env, flag);
156 rep->lock_policy_res1 |= flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
168 const struct lu_name *lname)
170 lh->mlh_reg_mode = lock_mode;
171 lh->mlh_rreg_mode = lock_mode;
172 lh->mlh_type = MDT_PDO_LOCK;
174 if (lu_name_is_valid(lname)) {
175 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
177 /* XXX Workaround for LU-2856
179 * Zero is a valid return value of full_name_hash, but
180 * several users of mlh_pdo_hash assume a non-zero
181 * hash value. We therefore map zero onto an
182 * arbitrary, but consistent value (1) to avoid
183 * problems further down the road. */
184 if (unlikely(lh->mlh_pdo_hash == 0))
185 lh->mlh_pdo_hash = 1;
187 lh->mlh_pdo_hash = 0;
191 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
192 struct mdt_lock_handle *lh)
198 * Any dir access needs couple of locks:
200 * 1) on part of dir we gonna take lookup/modify;
202 * 2) on whole dir to protect it from concurrent splitting and/or to
203 * flush client's cache for readdir().
205 * so, for a given mode and object this routine decides what lock mode
206 * to use for lock #2:
208 * 1) if caller's gonna lookup in dir then we need to protect dir from
209 * being splitted only - LCK_CR
211 * 2) if caller's gonna modify dir then we need to protect dir from
212 * being splitted and to flush cache - LCK_CW
214 * 3) if caller's gonna modify dir and that dir seems ready for
215 * splitting then we need to protect it from any type of access
216 * (lookup/modify/split) - LCK_EX --bzzz
219 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
220 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
223 * Ask underlaying level its opinion about preferable PDO lock mode
224 * having access type passed as regular lock mode:
226 * - MDL_MINMODE means that lower layer does not want to specify lock
229 * - MDL_NL means that no PDO lock should be taken. This is used in some
230 * cases. Say, for non-splittable directories no need to use PDO locks
233 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
234 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
236 if (mode != MDL_MINMODE) {
237 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
240 * Lower layer does not want to specify locking mode. We do it
241 * our selves. No special protection is needed, just flush
242 * client's cache on modification and allow concurrent
245 switch (lh->mlh_reg_mode) {
247 lh->mlh_pdo_mode = LCK_EX;
250 lh->mlh_pdo_mode = LCK_CR;
253 lh->mlh_pdo_mode = LCK_CW;
256 CERROR("Not expected lock type (0x%x)\n",
257 (int)lh->mlh_reg_mode);
262 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
266 int mdt_getstatus(struct tgt_session_info *tsi)
268 struct mdt_thread_info *info = tsi2mdt_info(tsi);
269 struct mdt_device *mdt = info->mti_mdt;
270 struct mdt_body *repbody;
275 rc = mdt_check_ucred(info);
277 GOTO(out, rc = err_serious(rc));
279 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
280 GOTO(out, rc = err_serious(-ENOMEM));
282 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
283 repbody->fid1 = mdt->mdt_md_root_fid;
284 repbody->valid |= OBD_MD_FLID;
286 if (tsi->tsi_tgt->lut_mds_capa &&
287 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
288 struct mdt_object *root;
289 struct lustre_capa *capa;
291 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
293 GOTO(out, rc = PTR_ERR(root));
295 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
297 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
298 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
300 mdt_object_put(info->mti_env, root);
302 repbody->valid |= OBD_MD_FLMDSCAPA;
306 mdt_thread_info_fini(info);
310 int mdt_statfs(struct tgt_session_info *tsi)
312 struct ptlrpc_request *req = tgt_ses_req(tsi);
313 struct mdt_thread_info *info = tsi2mdt_info(tsi);
314 struct md_device *next = info->mti_mdt->mdt_child;
315 struct ptlrpc_service_part *svcpt;
316 struct obd_statfs *osfs;
321 svcpt = req->rq_rqbd->rqbd_svcpt;
323 /* This will trigger a watchdog timeout */
324 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
325 (MDT_SERVICE_WATCHDOG_FACTOR *
326 at_get(&svcpt->scp_at_estimate)) + 1);
328 rc = mdt_check_ucred(info);
330 GOTO(out, rc = err_serious(rc));
332 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
333 GOTO(out, rc = err_serious(-ENOMEM));
335 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
337 GOTO(out, rc = -EPROTO);
339 /** statfs information are cached in the mdt_device */
340 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
341 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
342 /** statfs data is too old, get up-to-date one */
343 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
346 spin_lock(&info->mti_mdt->mdt_osfs_lock);
347 info->mti_mdt->mdt_osfs = *osfs;
348 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
349 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
351 /** use cached statfs data */
352 spin_lock(&info->mti_mdt->mdt_osfs_lock);
353 *osfs = info->mti_mdt->mdt_osfs;
354 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
358 mdt_counter_incr(req, LPROC_MDT_STATFS);
360 mdt_thread_info_fini(info);
365 * Pack SOM attributes into the reply.
366 * Call under a DLM UPDATE lock.
368 static void mdt_pack_size2body(struct mdt_thread_info *info,
369 struct mdt_object *mo)
372 struct md_attr *ma = &info->mti_attr;
374 LASSERT(ma->ma_attr.la_valid & LA_MODE);
375 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
377 /* Check if Size-on-MDS is supported, if this is a regular file,
378 * if SOM is enabled on the object and if SOM cache exists and valid.
379 * Otherwise do not pack Size-on-MDS attributes to the reply. */
380 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
381 !S_ISREG(ma->ma_attr.la_mode) ||
382 !mdt_object_is_som_enabled(mo) ||
383 !(ma->ma_valid & MA_SOM))
386 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
387 b->size = ma->ma_som->msd_size;
388 b->blocks = ma->ma_som->msd_blocks;
391 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
392 const struct lu_attr *attr, const struct lu_fid *fid)
394 struct md_attr *ma = &info->mti_attr;
396 LASSERT(ma->ma_valid & MA_INODE);
398 b->atime = attr->la_atime;
399 b->mtime = attr->la_mtime;
400 b->ctime = attr->la_ctime;
401 b->mode = attr->la_mode;
402 b->size = attr->la_size;
403 b->blocks = attr->la_blocks;
404 b->uid = attr->la_uid;
405 b->gid = attr->la_gid;
406 b->flags = attr->la_flags;
407 b->nlink = attr->la_nlink;
408 b->rdev = attr->la_rdev;
410 /*XXX should pack the reply body according to lu_valid*/
411 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
412 OBD_MD_FLGID | OBD_MD_FLTYPE |
413 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
414 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
416 if (!S_ISREG(attr->la_mode)) {
417 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
418 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
419 /* means no objects are allocated on osts. */
420 LASSERT(!(ma->ma_valid & MA_LOV));
421 /* just ignore blocks occupied by extend attributes on MDS */
423 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
424 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
425 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
426 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
427 /* A released file stores its size on MDS. */
428 /* But return 1 block for released file, unless tools like tar
429 * will consider it fully sparse. (LU-3864)
431 if (unlikely(b->size == 0))
435 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
440 b->valid |= OBD_MD_FLID;
441 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
442 PFID(fid), b->nlink, b->mode, b->size);
446 mdt_body_reverse_idmap(info, b);
448 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
449 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
450 PFID(fid), (unsigned long long)b->size);
453 static inline int mdt_body_has_lov(const struct lu_attr *la,
454 const struct mdt_body *body)
456 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
457 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
460 void mdt_client_compatibility(struct mdt_thread_info *info)
462 struct mdt_body *body;
463 struct ptlrpc_request *req = mdt_info_req(info);
464 struct obd_export *exp = req->rq_export;
465 struct md_attr *ma = &info->mti_attr;
466 struct lu_attr *la = &ma->ma_attr;
469 if (exp_connect_layout(exp))
470 /* the client can deal with 16-bit lmm_stripe_count */
473 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
475 if (!mdt_body_has_lov(la, body))
478 /* now we have a reply with a lov for a client not compatible with the
479 * layout lock so we have to clean the layout generation number */
480 if (S_ISREG(la->la_mode))
481 ma->ma_lmm->lmm_layout_gen = 0;
485 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
488 const struct lu_env *env = info->mti_env;
492 LASSERT(info->mti_big_lmm_used == 0);
493 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
497 /* big_lmm may need to be grown */
498 if (info->mti_big_lmmsize < rc) {
499 int size = size_roundup_power2(rc);
501 if (info->mti_big_lmmsize > 0) {
502 /* free old buffer */
503 LASSERT(info->mti_big_lmm);
504 OBD_FREE_LARGE(info->mti_big_lmm,
505 info->mti_big_lmmsize);
506 info->mti_big_lmm = NULL;
507 info->mti_big_lmmsize = 0;
510 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
511 if (info->mti_big_lmm == NULL)
513 info->mti_big_lmmsize = size;
515 LASSERT(info->mti_big_lmmsize >= rc);
517 info->mti_buf.lb_buf = info->mti_big_lmm;
518 info->mti_buf.lb_len = info->mti_big_lmmsize;
519 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
524 static int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
525 struct md_attr *ma, char *name)
527 struct md_object *next = mdt_object_child(o);
528 struct lu_buf *buf = &info->mti_buf;
531 if (strcmp(name, XATTR_NAME_LOV) == 0) {
532 buf->lb_buf = ma->ma_lmm;
533 buf->lb_len = ma->ma_lmm_size;
534 LASSERT(!(ma->ma_valid & MA_LOV));
535 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
536 buf->lb_buf = ma->ma_lmv;
537 buf->lb_len = ma->ma_lmv_size;
538 LASSERT(!(ma->ma_valid & MA_LMV));
539 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
540 buf->lb_buf = ma->ma_lmv;
541 buf->lb_len = ma->ma_lmv_size;
542 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
547 rc = mo_xattr_get(info->mti_env, next, buf, name);
549 if (strcmp(name, XATTR_NAME_LOV) == 0) {
550 ma->ma_lmm_size = rc;
551 ma->ma_valid |= MA_LOV;
552 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
553 ma->ma_lmv_size = rc;
554 ma->ma_valid |= MA_LMV;
555 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
556 ma->ma_lmv_size = rc;
557 ma->ma_valid |= MA_LMV_DEF;
561 } else if (rc == -ENODATA) {
564 } else if (rc == -ERANGE) {
565 /* Default LMV has fixed size, so it must be able to fit
566 * in the original buffer */
567 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
569 rc = mdt_big_xattr_get(info, o, name);
571 info->mti_big_lmm_used = 1;
572 if (!strcmp(name, XATTR_NAME_LOV)) {
573 ma->ma_valid |= MA_LOV;
574 ma->ma_lmm = info->mti_big_lmm;
575 ma->ma_lmm_size = rc;
576 } else if (!strcmp(name, XATTR_NAME_LMV)) {
577 ma->ma_valid |= MA_LMV;
578 ma->ma_lmv = info->mti_big_lmm;
579 ma->ma_lmv_size = rc;
584 /* update mdt_max_mdsize so all clients
585 * will be aware about that */
586 if (info->mti_mdt->mdt_max_mdsize < rc)
587 info->mti_mdt->mdt_max_mdsize = rc;
595 int mdt_attr_get_pfid(struct mdt_thread_info *info,
596 struct mdt_object *o, struct lu_fid *pfid)
598 struct lu_buf *buf = &info->mti_buf;
599 struct link_ea_header *leh;
600 struct link_ea_entry *lee;
604 buf->lb_buf = info->mti_big_lmm;
605 buf->lb_len = info->mti_big_lmmsize;
606 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
607 buf, XATTR_NAME_LINK);
608 /* ignore errors, MA_PFID won't be set and it is
609 * up to the caller to treat this as an error */
610 if (rc == -ERANGE || buf->lb_len == 0) {
611 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
612 buf->lb_buf = info->mti_big_lmm;
613 buf->lb_len = info->mti_big_lmmsize;
618 if (rc < sizeof(*leh)) {
619 CERROR("short LinkEA on "DFID": rc = %d\n",
620 PFID(mdt_object_fid(o)), rc);
624 leh = (struct link_ea_header *) buf->lb_buf;
625 lee = (struct link_ea_entry *)(leh + 1);
626 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
627 leh->leh_magic = LINK_EA_MAGIC;
628 leh->leh_reccount = __swab32(leh->leh_reccount);
629 leh->leh_len = __swab64(leh->leh_len);
631 if (leh->leh_magic != LINK_EA_MAGIC)
633 if (leh->leh_reccount == 0)
636 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
637 fid_be_to_cpu(pfid, pfid);
642 int mdt_attr_get_complex(struct mdt_thread_info *info,
643 struct mdt_object *o, struct md_attr *ma)
645 const struct lu_env *env = info->mti_env;
646 struct md_object *next = mdt_object_child(o);
647 struct lu_buf *buf = &info->mti_buf;
648 u32 mode = lu_object_attr(&next->mo_lu);
649 int need = ma->ma_need;
655 if (need & MA_INODE) {
656 ma->ma_need = MA_INODE;
657 rc = mo_attr_get(env, next, ma);
660 ma->ma_valid |= MA_INODE;
663 if (need & MA_PFID) {
664 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
666 ma->ma_valid |= MA_PFID;
667 /* ignore this error, parent fid is not mandatory */
671 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
672 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
677 if (need & MA_LMV && S_ISDIR(mode)) {
678 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
683 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
684 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
689 if (need & MA_SOM && S_ISREG(mode)) {
690 buf->lb_buf = info->mti_xattr_buf;
691 buf->lb_len = sizeof(info->mti_xattr_buf);
692 CLASSERT(sizeof(struct som_attrs) <=
693 sizeof(info->mti_xattr_buf));
694 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
695 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
697 ma->ma_valid |= MA_SOM;
698 else if (rc2 < 0 && rc2 != -ENODATA)
702 if (need & MA_HSM && S_ISREG(mode)) {
703 buf->lb_buf = info->mti_xattr_buf;
704 buf->lb_len = sizeof(info->mti_xattr_buf);
705 CLASSERT(sizeof(struct hsm_attrs) <=
706 sizeof(info->mti_xattr_buf));
707 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
708 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
710 ma->ma_valid |= MA_HSM;
711 else if (rc2 < 0 && rc2 != -ENODATA)
715 #ifdef CONFIG_FS_POSIX_ACL
716 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
717 buf->lb_buf = ma->ma_acl;
718 buf->lb_len = ma->ma_acl_size;
719 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
721 ma->ma_acl_size = rc2;
722 ma->ma_valid |= MA_ACL_DEF;
723 } else if (rc2 == -ENODATA) {
732 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
733 rc, ma->ma_valid, ma->ma_lmm);
737 static int mdt_getattr_internal(struct mdt_thread_info *info,
738 struct mdt_object *o, int ma_need)
740 struct md_object *next = mdt_object_child(o);
741 const struct mdt_body *reqbody = info->mti_body;
742 struct ptlrpc_request *req = mdt_info_req(info);
743 struct md_attr *ma = &info->mti_attr;
744 struct lu_attr *la = &ma->ma_attr;
745 struct req_capsule *pill = info->mti_pill;
746 const struct lu_env *env = info->mti_env;
747 struct mdt_body *repbody;
748 struct lu_buf *buffer = &info->mti_buf;
753 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
754 RETURN(err_serious(-ENOMEM));
756 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
760 if (mdt_object_remote(o)) {
761 /* This object is located on remote node.*/
762 /* Return -EIO for old client */
763 if (!mdt_is_dne_client(req->rq_export))
764 GOTO(out, rc = -EIO);
766 repbody->fid1 = *mdt_object_fid(o);
767 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
771 buffer->lb_len = reqbody->eadatasize;
772 if (buffer->lb_len > 0) {
773 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
774 if (buffer->lb_buf == NULL)
775 GOTO(out, rc = -EPROTO);
777 buffer->lb_buf = NULL;
778 ma_need &= ~(MA_LOV | MA_LMV);
779 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
780 mdt_obd_name(info->mti_mdt),
781 req->rq_export->exp_client_uuid.uuid);
784 /* If it is dir object and client require MEA, then we got MEA */
785 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
786 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
787 /* Assumption: MDT_MD size is enough for lmv size. */
788 ma->ma_lmv = buffer->lb_buf;
789 ma->ma_lmv_size = buffer->lb_len;
790 ma->ma_need = MA_INODE;
791 if (ma->ma_lmv_size > 0) {
792 if (reqbody->valid & OBD_MD_MEA)
793 ma->ma_need |= MA_LMV;
794 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
795 ma->ma_need |= MA_LMV_DEF;
798 ma->ma_lmm = buffer->lb_buf;
799 ma->ma_lmm_size = buffer->lb_len;
800 ma->ma_need = MA_INODE | MA_HSM;
801 if (ma->ma_lmm_size > 0)
802 ma->ma_need |= MA_LOV;
805 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
806 reqbody->valid & OBD_MD_FLDIREA &&
807 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
808 /* get default stripe info for this dir. */
809 ma->ma_need |= MA_LOV_DEF;
811 ma->ma_need |= ma_need;
812 if (ma->ma_need & MA_SOM)
813 ma->ma_som = &info->mti_u.som.data;
815 rc = mdt_attr_get_complex(info, o, ma);
817 CERROR("%s: getattr error for "DFID": rc = %d\n",
818 mdt_obd_name(info->mti_mdt),
819 PFID(mdt_object_fid(o)), rc);
823 /* if file is released, check if a restore is running */
824 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
825 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
826 repbody->t_state = MS_RESTORE;
827 repbody->valid |= OBD_MD_TSTATE;
830 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
832 /* the Lustre protocol supposes to return default striping
833 * on the user-visible root if explicitly requested */
834 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
835 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
836 struct lu_fid rootfid;
837 struct mdt_object *root;
838 struct mdt_device *mdt = info->mti_mdt;
840 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
843 root = mdt_object_find(env, mdt, &rootfid);
845 RETURN(PTR_ERR(root));
846 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
847 mdt_object_put(info->mti_env, root);
849 CERROR("%s: getattr error for "DFID": rc = %d\n",
850 mdt_obd_name(info->mti_mdt),
851 PFID(mdt_object_fid(o)), rc);
856 if (likely(ma->ma_valid & MA_INODE))
857 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
861 if (mdt_body_has_lov(la, reqbody)) {
862 if (ma->ma_valid & MA_LOV) {
863 LASSERT(ma->ma_lmm_size);
864 mdt_dump_lmm(D_INFO, ma->ma_lmm);
865 repbody->eadatasize = ma->ma_lmm_size;
866 if (S_ISDIR(la->la_mode))
867 repbody->valid |= OBD_MD_FLDIREA;
869 repbody->valid |= OBD_MD_FLEASIZE;
871 if (ma->ma_valid & MA_LMV) {
872 LASSERT(S_ISDIR(la->la_mode));
873 mdt_dump_lmv(D_INFO, ma->ma_lmv);
874 repbody->eadatasize = ma->ma_lmv_size;
875 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
877 if (ma->ma_valid & MA_LMV_DEF) {
878 LASSERT(S_ISDIR(la->la_mode));
879 repbody->eadatasize = ma->ma_lmv_size;
880 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
882 } else if (S_ISLNK(la->la_mode) &&
883 reqbody->valid & OBD_MD_LINKNAME) {
884 buffer->lb_buf = ma->ma_lmm;
885 /* eadatasize from client includes NULL-terminator, so
886 * there is no need to read it */
887 buffer->lb_len = reqbody->eadatasize - 1;
888 rc = mo_readlink(env, next, buffer);
889 if (unlikely(rc <= 0)) {
890 CERROR("%s: readlink failed for "DFID": rc = %d\n",
891 mdt_obd_name(info->mti_mdt),
892 PFID(mdt_object_fid(o)), rc);
895 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
897 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
899 repbody->valid |= OBD_MD_LINKNAME;
900 /* we need to report back size with NULL-terminator
901 * because client expects that */
902 repbody->eadatasize = rc + 1;
903 if (repbody->eadatasize != reqbody->eadatasize)
904 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
905 "on "DFID ", expected %d\n",
906 mdt_obd_name(info->mti_mdt),
907 rc, PFID(mdt_object_fid(o)),
908 reqbody->eadatasize - 1);
910 ((char *)ma->ma_lmm)[rc] = 0;
912 /* If the total CDEBUG() size is larger than a page, it
913 * will print a warning to the console, avoid this by
914 * printing just the last part of the symlink. */
915 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
916 print_limit < rc ? "..." : "", print_limit,
917 (char *)ma->ma_lmm + rc - print_limit, rc);
922 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
923 repbody->max_cookiesize = 0;
924 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
925 repbody->valid |= OBD_MD_FLMODEASIZE;
926 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
927 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
928 repbody->max_cookiesize);
931 if (exp_connect_rmtclient(info->mti_exp) &&
932 reqbody->valid & OBD_MD_FLRMTPERM) {
933 void *buf = req_capsule_server_get(pill, &RMF_ACL);
935 /* mdt_getattr_lock only */
936 rc = mdt_pack_remote_perm(info, o, buf);
938 repbody->valid &= ~OBD_MD_FLRMTPERM;
939 repbody->aclsize = 0;
942 repbody->valid |= OBD_MD_FLRMTPERM;
943 repbody->aclsize = sizeof(struct mdt_remote_perm);
946 #ifdef CONFIG_FS_POSIX_ACL
947 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
948 (reqbody->valid & OBD_MD_FLACL)) {
949 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
950 buffer->lb_len = req_capsule_get_size(pill,
951 &RMF_ACL, RCL_SERVER);
952 if (buffer->lb_len > 0) {
953 rc = mo_xattr_get(env, next, buffer,
954 XATTR_NAME_ACL_ACCESS);
956 if (rc == -ENODATA) {
957 repbody->aclsize = 0;
958 repbody->valid |= OBD_MD_FLACL;
960 } else if (rc == -EOPNOTSUPP) {
963 CERROR("%s: unable to read "DFID
965 mdt_obd_name(info->mti_mdt),
966 PFID(mdt_object_fid(o)), rc);
969 repbody->aclsize = rc;
970 repbody->valid |= OBD_MD_FLACL;
977 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
978 info->mti_mdt->mdt_lut.lut_mds_capa &&
979 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
980 struct lustre_capa *capa;
982 capa = req_capsule_server_get(pill, &RMF_CAPA1);
984 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
985 rc = mo_capa_get(env, next, capa, 0);
988 repbody->valid |= OBD_MD_FLMDSCAPA;
993 mdt_counter_incr(req, LPROC_MDT_GETATTR);
998 static int mdt_renew_capa(struct mdt_thread_info *info)
1000 struct mdt_object *obj = info->mti_object;
1001 struct mdt_body *body;
1002 struct lustre_capa *capa, *c;
1006 /* if object doesn't exist, or server has disabled capability,
1007 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1010 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1011 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1014 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1015 LASSERT(body != NULL);
1017 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1020 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1024 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1026 body->valid |= OBD_MD_FLOSSCAPA;
1030 int mdt_getattr(struct tgt_session_info *tsi)
1032 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1033 struct mdt_object *obj = info->mti_object;
1034 struct req_capsule *pill = info->mti_pill;
1035 struct mdt_body *reqbody;
1036 struct mdt_body *repbody;
1041 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1044 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1045 rc = req_capsule_server_pack(pill);
1047 RETURN(err_serious(rc));
1048 rc = mdt_renew_capa(info);
1049 GOTO(out_shrink, rc);
1052 LASSERT(obj != NULL);
1053 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1055 mode = lu_object_attr(&obj->mot_obj);
1057 /* old clients may not report needed easize, use max value then */
1058 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1059 reqbody->eadatasize == 0 ?
1060 info->mti_mdt->mdt_max_mdsize :
1061 reqbody->eadatasize);
1063 rc = req_capsule_server_pack(pill);
1064 if (unlikely(rc != 0))
1065 GOTO(out, rc = err_serious(rc));
1067 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1068 LASSERT(repbody != NULL);
1069 repbody->eadatasize = 0;
1070 repbody->aclsize = 0;
1072 if (reqbody->valid & OBD_MD_FLRMTPERM)
1073 rc = mdt_init_ucred(info, reqbody);
1075 rc = mdt_check_ucred(info);
1077 GOTO(out_shrink, rc);
1079 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1082 * Don't check capability at all, because rename might getattr for
1083 * remote obj, and at that time no capability is available.
1085 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1086 rc = mdt_getattr_internal(info, obj, 0);
1087 if (reqbody->valid & OBD_MD_FLRMTPERM)
1088 mdt_exit_ucred(info);
1091 mdt_client_compatibility(info);
1092 rc2 = mdt_fix_reply(info);
1096 mdt_thread_info_fini(info);
1100 int mdt_is_subdir(struct tgt_session_info *tsi)
1102 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1103 struct mdt_object *o = info->mti_object;
1104 struct req_capsule *pill = info->mti_pill;
1105 const struct mdt_body *body = info->mti_body;
1106 struct mdt_body *repbody;
1112 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1115 * We save last checked parent fid to @repbody->fid1 for remote
1118 LASSERT(fid_is_sane(&body->fid2));
1119 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1120 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1121 &body->fid2, &repbody->fid1);
1122 if (rc == 0 || rc == -EREMOTE)
1123 repbody->valid |= OBD_MD_FLID;
1125 mdt_thread_info_fini(info);
1129 int mdt_swap_layouts(struct tgt_session_info *tsi)
1131 struct mdt_thread_info *info;
1132 struct ptlrpc_request *req = tgt_ses_req(tsi);
1133 struct obd_export *exp = req->rq_export;
1134 struct mdt_object *o1, *o2, *o;
1135 struct mdt_lock_handle *lh1, *lh2;
1136 struct mdc_swap_layouts *msl;
1140 /* client does not support layout lock, so layout swaping
1142 * FIXME: there is a problem for old clients which don't support
1143 * layout lock yet. If those clients have already opened the file
1144 * they won't be notified at all so that old layout may still be
1145 * used to do IO. This can be fixed after file release is landed by
1146 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1147 if (!exp_connect_layout(exp))
1148 RETURN(-EOPNOTSUPP);
1150 info = tsi2mdt_info(tsi);
1151 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1152 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1153 req_capsule_client_get(info->mti_pill,
1156 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1157 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1158 req_capsule_client_get(info->mti_pill,
1161 o1 = info->mti_object;
1162 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1163 &info->mti_body->fid2);
1165 GOTO(out, rc = PTR_ERR(o));
1167 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1168 GOTO(put, rc = -ENOENT);
1170 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1171 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1177 /* permission check. Make sure the calling process having permission
1178 * to write both files. */
1179 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1184 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1189 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1191 GOTO(put, rc = -EPROTO);
1193 lh1 = &info->mti_lh[MDT_LH_NEW];
1194 mdt_lock_reg_init(lh1, LCK_EX);
1195 lh2 = &info->mti_lh[MDT_LH_OLD];
1196 mdt_lock_reg_init(lh2, LCK_EX);
1198 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1199 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1203 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1204 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1208 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1209 mdt_object_child(o2), msl->msl_flags);
1212 mdt_object_unlock(info, o2, lh2, rc);
1214 mdt_object_unlock(info, o1, lh1, rc);
1216 mdt_object_put(info->mti_env, o);
1218 mdt_thread_info_fini(info);
1222 static int mdt_raw_lookup(struct mdt_thread_info *info,
1223 struct mdt_object *parent,
1224 const struct lu_name *lname,
1225 struct ldlm_reply *ldlm_rep)
1227 struct md_object *next = mdt_object_child(info->mti_object);
1228 const struct mdt_body *reqbody = info->mti_body;
1229 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1230 struct mdt_body *repbody;
1234 if (reqbody->valid != OBD_MD_FLID)
1237 LASSERT(!info->mti_cross_ref);
1239 /* Only got the fid of this obj by name */
1240 fid_zero(child_fid);
1241 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1244 /* XXX is raw_lookup possible as intent operation? */
1247 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1250 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1252 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1255 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1256 repbody->fid1 = *child_fid;
1257 repbody->valid = OBD_MD_FLID;
1263 * UPDATE lock should be taken against parent, and be release before exit;
1264 * child_bits lock should be taken against child, and be returned back:
1265 * (1)normal request should release the child lock;
1266 * (2)intent request will grant the lock to client.
1268 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1269 struct mdt_lock_handle *lhc,
1271 struct ldlm_reply *ldlm_rep)
1273 struct ptlrpc_request *req = mdt_info_req(info);
1274 struct mdt_body *reqbody = NULL;
1275 struct mdt_object *parent = info->mti_object;
1276 struct mdt_object *child;
1277 struct md_object *next = mdt_object_child(parent);
1278 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1279 struct lu_name *lname = NULL;
1280 struct mdt_lock_handle *lhp = NULL;
1281 struct ldlm_lock *lock;
1282 struct ldlm_res_id *res_id;
1289 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1290 LASSERT(ergo(is_resent,
1291 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1293 LASSERT(parent != NULL);
1295 lname = &info->mti_name;
1296 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1298 if (!info->mti_cross_ref) {
1300 * XXX: Check for anonymous name is for getattr by fid
1301 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1302 * that is the name must contain at least one character and
1303 * the terminating '\0'.
1305 if (!lu_name_is_valid(lname)) {
1306 reqbody = req_capsule_client_get(info->mti_pill,
1308 if (unlikely(reqbody == NULL))
1309 RETURN(err_serious(-EFAULT));
1311 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1312 RETURN(err_serious(-EINVAL));
1314 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1316 PFID(mdt_object_fid(parent)),
1317 PFID(&reqbody->fid2), ldlm_rep);
1319 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1320 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1321 PNAME(lname), ldlm_rep);
1325 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1327 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1328 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1330 "Parent doesn't exist!\n");
1332 } else if (!info->mti_cross_ref) {
1333 LASSERTF(!mdt_object_remote(parent),
1334 "Parent "DFID" is on remote server\n",
1335 PFID(mdt_object_fid(parent)));
1338 if (lu_name_is_valid(lname)) {
1339 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1347 if (info->mti_cross_ref) {
1348 /* Only getattr on the child. Parent is on another node. */
1349 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1351 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1352 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1355 /* Do not take lock for resent case. */
1356 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1357 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1358 lhc->mlh_reg_lh.cookie);
1359 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1360 &lock->l_resource->lr_name));
1361 LDLM_LOCK_PUT(lock);
1364 mdt_lock_handle_init(lhc);
1365 mdt_lock_reg_init(lhc, LCK_PR);
1368 * Object's name is on another MDS, no lookup or layout
1369 * lock is needed here but update lock is.
1371 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1372 MDS_INODELOCK_LAYOUT);
1373 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1375 rc = mdt_object_lock(info, child, lhc, child_bits,
1379 /* Finally, we can get attr for child. */
1380 if (!mdt_object_exists(child)) {
1381 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1383 "remote object doesn't exist.\n");
1384 mdt_object_unlock(info, child, lhc, 1);
1388 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1390 rc = mdt_getattr_internal(info, child, 0);
1391 if (unlikely(rc != 0))
1392 mdt_object_unlock(info, child, lhc, 1);
1397 if (lu_name_is_valid(lname)) {
1398 /* step 1: lock parent only if parent is a directory */
1399 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1400 lhp = &info->mti_lh[MDT_LH_PARENT];
1401 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1402 rc = mdt_object_lock(info, parent, lhp,
1403 MDS_INODELOCK_UPDATE,
1405 if (unlikely(rc != 0))
1409 /* step 2: lookup child's fid by name */
1410 fid_zero(child_fid);
1411 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1416 mdt_set_disposition(info, ldlm_rep,
1418 GOTO(out_parent, rc);
1420 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1422 *child_fid = reqbody->fid2;
1423 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1427 *step 3: find the child object by fid & lock it.
1428 * regardless if it is local or remote.
1430 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1431 * set parent dir fid the same as child fid in getattr by fid case
1432 * we should not lu_object_find() the object again, could lead
1433 * to hung if there is a concurrent unlink destroyed the object.
1435 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1436 mdt_object_get(info->mti_env, parent);
1439 child = mdt_object_find(info->mti_env, info->mti_mdt,
1443 if (unlikely(IS_ERR(child)))
1444 GOTO(out_parent, rc = PTR_ERR(child));
1446 /* Do not take lock for resent case. */
1447 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1448 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1449 lhc->mlh_reg_lh.cookie);
1451 res_id = &lock->l_resource->lr_name;
1452 if (!fid_res_name_eq(mdt_object_fid(child),
1453 &lock->l_resource->lr_name)) {
1454 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1455 &lock->l_resource->lr_name),
1456 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1457 PLDLMRES(lock->l_resource),
1458 PFID(mdt_object_fid(parent)));
1459 CWARN("Although resent, but still not get child lock"
1460 "parent:"DFID" child:"DFID"\n",
1461 PFID(mdt_object_fid(parent)),
1462 PFID(mdt_object_fid(child)));
1463 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1464 LDLM_LOCK_PUT(lock);
1467 LDLM_LOCK_PUT(lock);
1470 bool try_layout = false;
1473 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1474 mdt_lock_handle_init(lhc);
1475 mdt_lock_reg_init(lhc, LCK_PR);
1477 if (!mdt_object_exists(child)) {
1478 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1480 "Object doesn't exist!\n");
1481 GOTO(out_child, rc = -ENOENT);
1484 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1485 mdt_object_exists(child) && !mdt_object_remote(child)) {
1486 struct md_attr *ma = &info->mti_attr;
1489 ma->ma_need = MA_INODE;
1490 rc = mdt_attr_get_complex(info, child, ma);
1491 if (unlikely(rc != 0))
1492 GOTO(out_child, rc);
1494 /* If the file has not been changed for some time, we
1495 * return not only a LOOKUP lock, but also an UPDATE
1496 * lock and this might save us RPC on later STAT. For
1497 * directories, it also let negative dentry cache start
1498 * working for this dir. */
1499 if (ma->ma_valid & MA_INODE &&
1500 ma->ma_attr.la_valid & LA_CTIME &&
1501 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1502 ma->ma_attr.la_ctime < cfs_time_current_sec())
1503 child_bits |= MDS_INODELOCK_UPDATE;
1506 /* layout lock must be granted in a best-effort way
1507 * for IT operations */
1508 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1509 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1510 exp_connect_layout(info->mti_exp) &&
1511 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1512 !mdt_object_remote(child) && ldlm_rep != NULL) {
1513 /* try to grant layout lock for regular file. */
1519 child_bits |= MDS_INODELOCK_LAYOUT;
1520 /* try layout lock, it may fail to be granted due to
1521 * contention at LOOKUP or UPDATE */
1522 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1524 child_bits &= ~MDS_INODELOCK_LAYOUT;
1525 LASSERT(child_bits != 0);
1526 rc = mdt_object_lock(info, child, lhc,
1527 child_bits, MDT_CROSS_LOCK);
1532 rc = mdt_object_lock(info, child, lhc, child_bits,
1535 if (unlikely(rc != 0))
1536 GOTO(out_child, rc);
1539 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1540 /* Get MA_SOM attributes if update lock is given. */
1542 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1543 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1546 /* finally, we can get attr for child. */
1547 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1548 rc = mdt_getattr_internal(info, child, ma_need);
1549 if (unlikely(rc != 0)) {
1550 mdt_object_unlock(info, child, lhc, 1);
1552 /* Debugging code. */
1553 res_id = &lock->l_resource->lr_name;
1554 LDLM_DEBUG(lock, "Returning lock to client");
1555 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1556 &lock->l_resource->lr_name),
1557 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1558 PLDLMRES(lock->l_resource),
1559 PFID(mdt_object_fid(child)));
1560 if (mdt_object_exists(child) && !mdt_object_remote(child))
1561 mdt_pack_size2body(info, child);
1564 LDLM_LOCK_PUT(lock);
1568 mdt_object_put(info->mti_env, child);
1571 mdt_object_unlock(info, parent, lhp, 1);
1575 /* normal handler: should release the child lock */
1576 int mdt_getattr_name(struct tgt_session_info *tsi)
1578 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1579 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1580 struct mdt_body *reqbody;
1581 struct mdt_body *repbody;
1585 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1586 LASSERT(reqbody != NULL);
1587 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1588 LASSERT(repbody != NULL);
1590 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1591 repbody->eadatasize = 0;
1592 repbody->aclsize = 0;
1594 rc = mdt_init_ucred(info, reqbody);
1596 GOTO(out_shrink, rc);
1598 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1599 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1600 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1601 lhc->mlh_reg_lh.cookie = 0;
1603 mdt_exit_ucred(info);
1606 mdt_client_compatibility(info);
1607 rc2 = mdt_fix_reply(info);
1610 mdt_thread_info_fini(info);
1614 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1615 void *karg, void *uarg);
1617 int mdt_set_info(struct tgt_session_info *tsi)
1619 struct ptlrpc_request *req = tgt_ses_req(tsi);
1622 int keylen, vallen, rc = 0;
1626 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1628 DEBUG_REQ(D_HA, req, "no set_info key");
1629 RETURN(err_serious(-EFAULT));
1632 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1635 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1637 DEBUG_REQ(D_HA, req, "no set_info val");
1638 RETURN(err_serious(-EFAULT));
1641 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1644 /* Swab any part of val you need to here */
1645 if (KEY_IS(KEY_READ_ONLY)) {
1646 spin_lock(&req->rq_export->exp_lock);
1648 *exp_connect_flags_ptr(req->rq_export) |=
1651 *exp_connect_flags_ptr(req->rq_export) &=
1652 ~OBD_CONNECT_RDONLY;
1653 spin_unlock(&req->rq_export->exp_lock);
1654 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1655 struct changelog_setinfo *cs = val;
1657 if (vallen != sizeof(*cs)) {
1658 CERROR("%s: bad changelog_clear setinfo size %d\n",
1659 tgt_name(tsi->tsi_tgt), vallen);
1662 if (ptlrpc_req_need_swab(req)) {
1663 __swab64s(&cs->cs_recno);
1664 __swab32s(&cs->cs_id);
1667 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1675 int mdt_readpage(struct tgt_session_info *tsi)
1677 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1678 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1679 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1680 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1681 struct mdt_body *repbody;
1687 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1688 RETURN(err_serious(-ENOMEM));
1690 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1691 if (repbody == NULL || reqbody == NULL)
1692 RETURN(err_serious(-EFAULT));
1695 * prepare @rdpg before calling lower layers and transfer itself. Here
1696 * reqbody->size contains offset of where to start to read and
1697 * reqbody->nlink contains number bytes to read.
1699 rdpg->rp_hash = reqbody->size;
1700 if (rdpg->rp_hash != reqbody->size) {
1701 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1702 rdpg->rp_hash, reqbody->size);
1706 rdpg->rp_attrs = reqbody->mode;
1707 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1708 rdpg->rp_attrs |= LUDA_64BITHASH;
1709 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1710 exp_max_brw_size(tsi->tsi_exp));
1711 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1713 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1714 if (rdpg->rp_pages == NULL)
1717 for (i = 0; i < rdpg->rp_npages; ++i) {
1718 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1719 if (rdpg->rp_pages[i] == NULL)
1720 GOTO(free_rdpg, rc = -ENOMEM);
1723 /* call lower layers to fill allocated pages with directory data */
1724 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1726 GOTO(free_rdpg, rc);
1728 /* send pages to client */
1729 rc = tgt_sendpage(tsi, rdpg, rc);
1734 for (i = 0; i < rdpg->rp_npages; i++)
1735 if (rdpg->rp_pages[i] != NULL)
1736 __free_page(rdpg->rp_pages[i]);
1737 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1739 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1745 static int mdt_reint_internal(struct mdt_thread_info *info,
1746 struct mdt_lock_handle *lhc,
1749 struct req_capsule *pill = info->mti_pill;
1750 struct mdt_body *repbody;
1755 rc = mdt_reint_unpack(info, op);
1757 CERROR("Can't unpack reint, rc %d\n", rc);
1758 RETURN(err_serious(rc));
1761 /* for replay (no_create) lmm is not needed, client has it already */
1762 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1763 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1764 info->mti_rr.rr_eadatalen);
1766 /* llog cookies are always 0, the field is kept for compatibility */
1767 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1768 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1770 rc = req_capsule_server_pack(pill);
1772 CERROR("Can't pack response, rc %d\n", rc);
1773 RETURN(err_serious(rc));
1776 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1777 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1779 repbody->eadatasize = 0;
1780 repbody->aclsize = 0;
1783 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1785 /* for replay no cookkie / lmm need, because client have this already */
1786 if (info->mti_spec.no_create)
1787 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1788 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1790 rc = mdt_init_ucred_reint(info);
1792 GOTO(out_shrink, rc);
1794 rc = mdt_fix_attr_ucred(info, op);
1796 GOTO(out_ucred, rc = err_serious(rc));
1798 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1799 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1800 GOTO(out_ucred, rc);
1802 rc = mdt_reint_rec(info, lhc);
1805 mdt_exit_ucred(info);
1807 mdt_client_compatibility(info);
1808 rc2 = mdt_fix_reply(info);
1814 static long mdt_reint_opcode(struct ptlrpc_request *req,
1815 const struct req_format **fmt)
1817 struct mdt_device *mdt;
1818 struct mdt_rec_reint *rec;
1821 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1823 opc = rec->rr_opcode;
1824 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1825 if (opc < REINT_MAX && fmt[opc] != NULL)
1826 req_capsule_extend(&req->rq_pill, fmt[opc]);
1828 mdt = mdt_exp2dev(req->rq_export);
1829 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1830 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1831 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1832 opc = err_serious(-EFAULT);
1835 opc = err_serious(-EFAULT);
1840 int mdt_reint(struct tgt_session_info *tsi)
1844 static const struct req_format *reint_fmts[REINT_MAX] = {
1845 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1846 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1847 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1848 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1849 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1850 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1851 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1852 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1857 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1859 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1861 * No lock possible here from client to pass it to reint code
1864 rc = mdt_reint_internal(info, NULL, opc);
1865 mdt_thread_info_fini(info);
1870 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1874 /* this should sync the whole device */
1875 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1877 struct dt_device *dt = mdt->mdt_bottom;
1881 rc = dt->dd_ops->dt_sync(env, dt);
1885 /* this should sync this object */
1886 static int mdt_object_sync(struct mdt_thread_info *info)
1888 struct md_object *next;
1892 if (!mdt_object_exists(info->mti_object)) {
1893 CWARN("Non existing object "DFID"!\n",
1894 PFID(mdt_object_fid(info->mti_object)));
1897 next = mdt_object_child(info->mti_object);
1898 rc = mo_object_sync(info->mti_env, next);
1903 int mdt_sync(struct tgt_session_info *tsi)
1905 struct ptlrpc_request *req = tgt_ses_req(tsi);
1906 struct req_capsule *pill = tsi->tsi_pill;
1907 struct mdt_body *body;
1912 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1913 RETURN(err_serious(-ENOMEM));
1915 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1916 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1918 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1920 /* sync an object */
1921 rc = mdt_object_sync(info);
1923 const struct lu_fid *fid;
1924 struct lu_attr *la = &info->mti_attr.ma_attr;
1926 info->mti_attr.ma_need = MA_INODE;
1927 info->mti_attr.ma_valid = 0;
1928 rc = mdt_attr_get_complex(info, info->mti_object,
1931 body = req_capsule_server_get(pill,
1933 fid = mdt_object_fid(info->mti_object);
1934 mdt_pack_attr2body(info, body, la, fid);
1937 mdt_thread_info_fini(info);
1940 mdt_counter_incr(req, LPROC_MDT_SYNC);
1946 * Handle quota control requests to consult current usage/limit, but also
1947 * to configure quota enforcement
1949 int mdt_quotactl(struct tgt_session_info *tsi)
1951 struct obd_export *exp = tsi->tsi_exp;
1952 struct req_capsule *pill = tsi->tsi_pill;
1953 struct obd_quotactl *oqctl, *repoqc;
1955 struct mdt_device *mdt = mdt_exp2dev(exp);
1956 struct lu_device *qmt = mdt->mdt_qmt_dev;
1959 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1961 RETURN(err_serious(-EPROTO));
1963 rc = req_capsule_server_pack(pill);
1965 RETURN(err_serious(rc));
1967 switch (oqctl->qc_cmd) {
1969 case LUSTRE_Q_INVALIDATE:
1970 case LUSTRE_Q_FINVALIDATE:
1974 /* deprecated, not used any more */
1975 RETURN(-EOPNOTSUPP);
1976 /* master quotactl */
1982 RETURN(-EOPNOTSUPP);
1983 /* slave quotactl */
1988 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1992 /* map uid/gid for remote client */
1994 if (exp_connect_rmtclient(exp)) {
1995 struct lustre_idmap_table *idmap;
1997 idmap = exp->exp_mdt_data.med_idmap;
1999 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2000 oqctl->qc_cmd != Q_GETINFO))
2003 if (oqctl->qc_type == USRQUOTA)
2004 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2006 else if (oqctl->qc_type == GRPQUOTA)
2007 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2012 if (id == CFS_IDMAP_NOTFOUND) {
2013 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2018 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2020 RETURN(err_serious(-EFAULT));
2022 if (oqctl->qc_id != id)
2023 swap(oqctl->qc_id, id);
2025 switch (oqctl->qc_cmd) {
2031 /* forward quotactl request to QMT */
2032 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2037 /* slave quotactl */
2038 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2043 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2047 if (oqctl->qc_id != id)
2048 swap(oqctl->qc_id, id);
2054 /** clone llog ctxt from child (mdd)
2055 * This allows remote llog (replicator) access.
2056 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2057 * context was originally set up, or we can handle them directly.
2058 * I choose the latter, but that means I need any llog
2059 * contexts set up by child to be accessable by the mdt. So we clone the
2060 * context into our context list here.
2062 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2065 struct md_device *next = mdt->mdt_child;
2066 struct llog_ctxt *ctxt;
2069 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2072 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2073 if (rc || ctxt == NULL) {
2077 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2079 CERROR("Can't set mdt ctxt %d\n", rc);
2084 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2085 struct mdt_device *mdt, int idx)
2087 struct llog_ctxt *ctxt;
2089 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2092 /* Put once for the get we just did, and once for the clone */
2093 llog_ctxt_put(ctxt);
2094 llog_ctxt_put(ctxt);
2099 * sec context handlers
2101 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2105 rc = mdt_handle_idmap(tsi);
2107 struct ptlrpc_request *req = tgt_ses_req(tsi);
2110 opc = lustre_msg_get_opc(req->rq_reqmsg);
2111 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2112 sptlrpc_svc_ctx_invalidate(req);
2115 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2121 * quota request handlers
2123 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2125 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2126 struct lu_device *qmt = mdt->mdt_qmt_dev;
2131 RETURN(err_serious(-EOPNOTSUPP));
2133 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2137 struct mdt_object *mdt_object_new(const struct lu_env *env,
2138 struct mdt_device *d,
2139 const struct lu_fid *f)
2141 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2142 struct lu_object *o;
2143 struct mdt_object *m;
2146 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2147 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2148 if (unlikely(IS_ERR(o)))
2149 m = (struct mdt_object *)o;
2155 struct mdt_object *mdt_object_find(const struct lu_env *env,
2156 struct mdt_device *d,
2157 const struct lu_fid *f)
2159 struct lu_object *o;
2160 struct mdt_object *m;
2163 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2164 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2165 if (unlikely(IS_ERR(o)))
2166 m = (struct mdt_object *)o;
2174 * Asyncronous commit for mdt device.
2176 * Pass asynchonous commit call down the MDS stack.
2178 * \param env environment
2179 * \param mdt the mdt device
2181 static void mdt_device_commit_async(const struct lu_env *env,
2182 struct mdt_device *mdt)
2184 struct dt_device *dt = mdt->mdt_bottom;
2187 rc = dt->dd_ops->dt_commit_async(env, dt);
2188 if (unlikely(rc != 0))
2189 CWARN("async commit start failed with rc = %d", rc);
2193 * Mark the lock as "synchonous".
2195 * Mark the lock to deffer transaction commit to the unlock time.
2197 * \param lock the lock to mark as "synchonous"
2199 * \see mdt_is_lock_sync
2200 * \see mdt_save_lock
2202 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2204 lock->l_ast_data = (void*)1;
2208 * Check whehter the lock "synchonous" or not.
2210 * \param lock the lock to check
2211 * \retval 1 the lock is "synchonous"
2212 * \retval 0 the lock isn't "synchronous"
2214 * \see mdt_set_lock_sync
2215 * \see mdt_save_lock
2217 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2219 return lock->l_ast_data != NULL;
2223 * Blocking AST for mdt locks.
2225 * Starts transaction commit if in case of COS lock conflict or
2226 * deffers such a commit to the mdt_save_lock.
2228 * \param lock the lock which blocks a request or cancelling lock
2229 * \param desc unused
2230 * \param data unused
2231 * \param flag indicates whether this cancelling or blocking callback
2233 * \see ldlm_blocking_ast_nocheck
2235 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2236 void *data, int flag)
2238 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2239 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2243 if (flag == LDLM_CB_CANCELING)
2245 lock_res_and_lock(lock);
2246 if (lock->l_blocking_ast != mdt_blocking_ast) {
2247 unlock_res_and_lock(lock);
2250 if (mdt_cos_is_enabled(mdt) &&
2251 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2252 lock->l_blocking_lock != NULL &&
2253 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2254 mdt_set_lock_sync(lock);
2256 rc = ldlm_blocking_ast_nocheck(lock);
2258 /* There is no lock conflict if l_blocking_lock == NULL,
2259 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2260 * when the last reference to a local lock was released */
2261 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2264 rc = lu_env_init(&env, LCT_LOCAL);
2265 if (unlikely(rc != 0))
2266 CWARN("lu_env initialization failed with rc = %d,"
2267 "cannot start asynchronous commit\n", rc);
2269 mdt_device_commit_async(&env, mdt);
2275 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2276 void *data, int flag)
2278 struct lustre_handle lockh;
2282 case LDLM_CB_BLOCKING:
2283 ldlm_lock2handle(lock, &lockh);
2284 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2286 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2290 case LDLM_CB_CANCELING:
2291 LDLM_DEBUG(lock, "Revoke remote lock\n");
2299 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2300 struct mdt_object *o, struct lustre_handle *lh,
2301 ldlm_mode_t mode, __u64 ibits)
2303 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2304 ldlm_policy_data_t *policy = &mti->mti_policy;
2308 LASSERT(mdt_object_remote(o));
2310 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2312 memset(einfo, 0, sizeof(*einfo));
2313 einfo->ei_type = LDLM_IBITS;
2314 einfo->ei_mode = mode;
2315 einfo->ei_cb_bl = mdt_md_blocking_ast;
2316 einfo->ei_cb_cp = ldlm_completion_ast;
2318 memset(policy, 0, sizeof(*policy));
2319 policy->l_inodebits.bits = ibits;
2321 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2326 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2327 struct mdt_lock_handle *lh, __u64 ibits,
2328 bool nonblock, int locality)
2330 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2331 ldlm_policy_data_t *policy = &info->mti_policy;
2332 struct ldlm_res_id *res_id = &info->mti_res_id;
2337 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2338 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2339 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2340 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2342 if (mdt_object_remote(o)) {
2343 if (locality == MDT_CROSS_LOCK) {
2344 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2345 MDS_INODELOCK_LAYOUT);
2346 ibits |= MDS_INODELOCK_LOOKUP;
2349 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2350 MDS_INODELOCK_LAYOUT)),
2351 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2352 mdt_obd_name(info->mti_mdt), ibits,
2353 PFID(mdt_object_fid(o)));
2354 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2356 /* No PDO lock on remote object */
2357 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2360 if (lh->mlh_type == MDT_PDO_LOCK) {
2361 /* check for exists after object is locked */
2362 if (mdt_object_exists(o) == 0) {
2363 /* Non-existent object shouldn't have PDO lock */
2366 /* Non-dir object shouldn't have PDO lock */
2367 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2372 memset(policy, 0, sizeof(*policy));
2373 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2375 dlmflags = LDLM_FL_ATOMIC_CB;
2377 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2380 * Take PDO lock on whole directory and build correct @res_id for lock
2381 * on part of directory.
2383 if (lh->mlh_pdo_hash != 0) {
2384 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2385 mdt_lock_pdo_mode(info, o, lh);
2386 if (lh->mlh_pdo_mode != LCK_NL) {
2388 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2389 * is never going to be sent to client and we do not
2390 * want it slowed down due to possible cancels.
2392 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2393 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2394 policy, res_id, dlmflags,
2395 &info->mti_exp->exp_handle.h_cookie);
2401 * Finish res_id initializing by name hash marking part of
2402 * directory which is taking modification.
2404 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2407 policy->l_inodebits.bits = ibits;
2410 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2411 * going to be sent to client. If it is - mdt_intent_policy() path will
2412 * fix it up and turn FL_LOCAL flag off.
2414 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2415 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2416 &info->mti_exp->exp_handle.h_cookie);
2418 mdt_object_unlock(info, o, lh, 1);
2419 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2420 lh->mlh_pdo_hash != 0 &&
2421 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2422 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2428 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2429 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2431 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2434 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2435 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2437 struct mdt_lock_handle tmp = *lh;
2440 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2448 * Save a lock within request object.
2450 * Keep the lock referenced until whether client ACK or transaction
2451 * commit happens or release the lock immediately depending on input
2452 * parameters. If COS is ON, a write lock is converted to COS lock
2455 * \param info thead info object
2456 * \param h lock handle
2457 * \param mode lock mode
2458 * \param decref force immediate lock releasing
2461 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2462 ldlm_mode_t mode, int decref)
2466 if (lustre_handle_is_used(h)) {
2467 if (decref || !info->mti_has_trans ||
2468 !(mode & (LCK_PW | LCK_EX))){
2469 mdt_fid_unlock(h, mode);
2471 struct mdt_device *mdt = info->mti_mdt;
2472 struct ldlm_lock *lock = ldlm_handle2lock(h);
2473 struct ptlrpc_request *req = mdt_info_req(info);
2476 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2478 /* there is no request if mdt_object_unlock() is called
2479 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2480 if (likely(req != NULL)) {
2481 CDEBUG(D_HA, "request = %p reply state = %p"
2482 " transno = "LPD64"\n", req,
2483 req->rq_reply_state, req->rq_transno);
2484 if (mdt_cos_is_enabled(mdt)) {
2486 ldlm_lock_downgrade(lock, LCK_COS);
2489 ptlrpc_save_lock(req, h, mode, no_ack);
2491 ldlm_lock_decref(h, mode);
2493 if (mdt_is_lock_sync(lock)) {
2494 CDEBUG(D_HA, "found sync-lock,"
2495 " async commit started\n");
2496 mdt_device_commit_async(info->mti_env,
2499 LDLM_LOCK_PUT(lock);
2508 * Unlock mdt object.
2510 * Immeditely release the regular lock and the PDO lock or save the
2511 * lock in reqeuest and keep them referenced until client ACK or
2512 * transaction commit.
2514 * \param info thread info object
2515 * \param o mdt object
2516 * \param lh mdt lock handle referencing regular and PDO locks
2517 * \param decref force immediate lock releasing
2519 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2520 struct mdt_lock_handle *lh, int decref)
2524 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2525 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2527 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2528 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2533 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2534 const struct lu_fid *f,
2535 struct mdt_lock_handle *lh,
2538 struct mdt_object *o;
2540 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2544 rc = mdt_object_lock(info, o, lh, ibits,
2547 mdt_object_put(info->mti_env, o);
2554 void mdt_object_unlock_put(struct mdt_thread_info * info,
2555 struct mdt_object * o,
2556 struct mdt_lock_handle *lh,
2559 mdt_object_unlock(info, o, lh, decref);
2560 mdt_object_put(info->mti_env, o);
2564 * Generic code handling requests that have struct mdt_body passed in:
2566 * - extract mdt_body from request and save it in @info, if present;
2568 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2571 * - if HABEO_CORPUS flag is set for this request type check whether object
2572 * actually exists on storage (lu_object_exists()).
2575 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2577 const struct mdt_body *body;
2578 struct mdt_object *obj;
2579 const struct lu_env *env;
2580 struct req_capsule *pill;
2584 env = info->mti_env;
2585 pill = info->mti_pill;
2587 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2591 if (!(body->valid & OBD_MD_FLID))
2594 if (!fid_is_sane(&body->fid1)) {
2595 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2600 * Do not get size or any capa fields before we check that request
2601 * contains capa actually. There are some requests which do not, for
2602 * instance MDS_IS_SUBDIR.
2604 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2605 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2606 mdt_set_capainfo(info, 0, &body->fid1,
2607 req_capsule_client_get(pill, &RMF_CAPA1));
2609 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2611 if ((flags & HABEO_CORPUS) &&
2612 !mdt_object_exists(obj)) {
2613 mdt_object_put(env, obj);
2614 /* for capability renew ENOENT will be handled in
2616 if (body->valid & OBD_MD_FLOSSCAPA)
2621 info->mti_object = obj;
2630 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2632 struct req_capsule *pill = info->mti_pill;
2636 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2637 rc = mdt_body_unpack(info, flags);
2641 if (rc == 0 && (flags & HABEO_REFERO)) {
2643 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2644 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2645 info->mti_body->eadatasize);
2646 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2647 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2650 rc = req_capsule_server_pack(pill);
2655 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2657 struct md_device *next = m->mdt_child;
2659 return next->md_ops->mdo_init_capa_ctxt(env, next,
2660 m->mdt_lut.lut_mds_capa,
2661 m->mdt_capa_timeout,
2666 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2668 lh->mlh_type = MDT_NUL_LOCK;
2669 lh->mlh_reg_lh.cookie = 0ull;
2670 lh->mlh_reg_mode = LCK_MINMODE;
2671 lh->mlh_pdo_lh.cookie = 0ull;
2672 lh->mlh_pdo_mode = LCK_MINMODE;
2673 lh->mlh_rreg_lh.cookie = 0ull;
2674 lh->mlh_rreg_mode = LCK_MINMODE;
2677 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2679 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2680 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2684 * Initialize fields of struct mdt_thread_info. Other fields are left in
2685 * uninitialized state, because it's too expensive to zero out whole
2686 * mdt_thread_info (> 1K) on each request arrival.
2688 void mdt_thread_info_init(struct ptlrpc_request *req,
2689 struct mdt_thread_info *info)
2693 info->mti_pill = &req->rq_pill;
2696 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2697 mdt_lock_handle_init(&info->mti_lh[i]);
2699 /* mdt device: it can be NULL while CONNECT */
2700 if (req->rq_export) {
2701 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2702 info->mti_exp = req->rq_export;
2704 info->mti_mdt = NULL;
2705 info->mti_env = req->rq_svc_thread->t_env;
2706 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2708 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2709 info->mti_big_buf = LU_BUF_NULL;
2710 info->mti_body = NULL;
2711 info->mti_object = NULL;
2712 info->mti_dlm_req = NULL;
2713 info->mti_has_trans = 0;
2714 info->mti_cross_ref = 0;
2715 info->mti_opdata = 0;
2716 info->mti_big_lmm_used = 0;
2718 info->mti_spec.no_create = 0;
2719 info->mti_spec.sp_rm_entry = 0;
2722 void mdt_thread_info_fini(struct mdt_thread_info *info)
2726 if (info->mti_object != NULL) {
2727 mdt_object_put(info->mti_env, info->mti_object);
2728 info->mti_object = NULL;
2731 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2732 mdt_lock_handle_fini(&info->mti_lh[i]);
2733 info->mti_env = NULL;
2734 info->mti_pill = NULL;
2735 info->mti_exp = NULL;
2737 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2738 lu_buf_free(&info->mti_big_buf);
2741 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2743 struct mdt_thread_info *mti;
2744 struct lustre_capa *lc;
2746 mti = mdt_th_info(tsi->tsi_env);
2747 LASSERT(mti != NULL);
2749 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2750 if (tsi->tsi_corpus != NULL) {
2751 struct req_capsule *pill = tsi->tsi_pill;
2753 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2754 lu_object_get(tsi->tsi_corpus);
2757 * XXX: must be part of tgt_mdt_body_unpack but moved here
2758 * due to mdt_set_capainfo().
2760 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2761 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2762 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2763 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2766 mti->mti_body = tsi->tsi_mdt_body;
2767 mti->mti_dlm_req = tsi->tsi_dlm_req;
2772 int mdt_tgt_connect(struct tgt_session_info *tsi)
2774 struct ptlrpc_request *req = tgt_ses_req(tsi);
2779 rc = tgt_connect(tsi);
2783 rc = mdt_init_idmap(tsi);
2788 obd_disconnect(class_export_get(req->rq_export));
2807 static int mdt_intent_getattr(enum mdt_it_code opcode,
2808 struct mdt_thread_info *info,
2809 struct ldlm_lock **,
2812 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2813 struct mdt_thread_info *info,
2814 struct ldlm_lock **lockp,
2817 static int mdt_intent_layout(enum mdt_it_code opcode,
2818 struct mdt_thread_info *info,
2819 struct ldlm_lock **,
2821 static int mdt_intent_reint(enum mdt_it_code opcode,
2822 struct mdt_thread_info *info,
2823 struct ldlm_lock **,
2826 static struct mdt_it_flavor {
2827 const struct req_format *it_fmt;
2829 int (*it_act)(enum mdt_it_code ,
2830 struct mdt_thread_info *,
2831 struct ldlm_lock **,
2834 } mdt_it_flavor[] = {
2836 .it_fmt = &RQF_LDLM_INTENT,
2837 /*.it_flags = HABEO_REFERO,*/
2839 .it_act = mdt_intent_reint,
2840 .it_reint = REINT_OPEN
2843 .it_fmt = &RQF_LDLM_INTENT,
2845 * OCREAT is not a MUTABOR request as if the file
2847 * We do the extra check of OBD_CONNECT_RDONLY in
2848 * mdt_reint_open() when we really need to create
2852 .it_act = mdt_intent_reint,
2853 .it_reint = REINT_OPEN
2856 .it_fmt = &RQF_LDLM_INTENT,
2857 .it_flags = MUTABOR,
2858 .it_act = mdt_intent_reint,
2859 .it_reint = REINT_CREATE
2861 [MDT_IT_GETATTR] = {
2862 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2863 .it_flags = HABEO_REFERO,
2864 .it_act = mdt_intent_getattr
2866 [MDT_IT_READDIR] = {
2872 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2873 .it_flags = HABEO_REFERO,
2874 .it_act = mdt_intent_getattr
2877 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2878 .it_flags = MUTABOR,
2880 .it_reint = REINT_UNLINK
2884 .it_flags = MUTABOR,
2887 [MDT_IT_GETXATTR] = {
2888 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2889 .it_flags = HABEO_CORPUS,
2890 .it_act = mdt_intent_getxattr
2893 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2895 .it_act = mdt_intent_layout
2899 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2900 struct ldlm_lock **lockp,
2901 struct ldlm_lock *new_lock,
2902 struct mdt_lock_handle *lh,
2905 struct ptlrpc_request *req = mdt_info_req(info);
2906 struct ldlm_lock *lock = *lockp;
2909 * Get new lock only for cases when possible resent did not find any
2912 if (new_lock == NULL)
2913 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2915 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2916 lh->mlh_reg_lh.cookie = 0;
2920 LASSERTF(new_lock != NULL,
2921 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2924 * If we've already given this lock to a client once, then we should
2925 * have no readers or writers. Otherwise, we should have one reader
2926 * _or_ writer ref (which will be zeroed below) before returning the
2929 if (new_lock->l_export == req->rq_export) {
2930 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2932 LASSERT(new_lock->l_export == NULL);
2933 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2938 if (new_lock->l_export == req->rq_export) {
2940 * Already gave this to the client, which means that we
2941 * reconstructed a reply.
2943 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2945 lh->mlh_reg_lh.cookie = 0;
2946 RETURN(ELDLM_LOCK_REPLACED);
2950 * Fixup the lock to be given to the client.
2952 lock_res_and_lock(new_lock);
2953 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2954 * possible blocking AST. */
2955 while (new_lock->l_readers > 0) {
2956 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2957 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2958 new_lock->l_readers--;
2960 while (new_lock->l_writers > 0) {
2961 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2962 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2963 new_lock->l_writers--;
2966 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2967 new_lock->l_blocking_ast = lock->l_blocking_ast;
2968 new_lock->l_completion_ast = lock->l_completion_ast;
2969 new_lock->l_remote_handle = lock->l_remote_handle;
2970 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2972 unlock_res_and_lock(new_lock);
2974 cfs_hash_add(new_lock->l_export->exp_lock_hash,
2975 &new_lock->l_remote_handle,
2976 &new_lock->l_exp_hash);
2978 LDLM_LOCK_RELEASE(new_lock);
2979 lh->mlh_reg_lh.cookie = 0;
2981 RETURN(ELDLM_LOCK_REPLACED);
2984 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2985 struct ldlm_lock *new_lock,
2986 struct ldlm_lock **old_lock,
2987 struct mdt_lock_handle *lh,
2988 enum mdt_it_code opcode)
2990 struct ptlrpc_request *req = mdt_info_req(info);
2991 struct obd_export *exp = req->rq_export;
2992 struct lustre_handle remote_hdl;
2993 struct ldlm_request *dlmreq;
2994 struct ldlm_lock *lock;
2996 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2999 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3000 remote_hdl = dlmreq->lock_handle[0];
3001 /* If the client does not require open lock, it does not need to
3002 * search lock in exp_lock_hash, since the server thread will
3003 * make sure the lock will be released, and the resend request
3004 * can always re-enqueue the lock */
3005 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3006 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3007 /* In the function below, .hs_keycmp resolves to
3008 * ldlm_export_lock_keycmp() */
3009 /* coverity[overrun-buffer-val] */
3010 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3012 lock_res_and_lock(lock);
3013 if (lock != new_lock) {
3014 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3015 lh->mlh_reg_mode = lock->l_granted_mode;
3017 LDLM_DEBUG(lock, "Restoring lock cookie");
3018 DEBUG_REQ(D_DLMTRACE, req,
3019 "restoring lock cookie "LPX64,
3020 lh->mlh_reg_lh.cookie);
3022 *old_lock = LDLM_LOCK_GET(lock);
3023 cfs_hash_put(exp->exp_lock_hash,
3025 unlock_res_and_lock(lock);
3028 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3029 unlock_res_and_lock(lock);
3033 * If the xid matches, then we know this is a resent request, and allow
3034 * it. (It's probably an OPEN, for which we don't send a lock.
3036 if (req_xid_is_last(req))
3040 * This remote handle isn't enqueued, so we never received or processed
3041 * this request. Clear MSG_RESENT, because it can be handled like any
3042 * normal request now.
3044 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3046 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3050 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3051 struct mdt_thread_info *info,
3052 struct ldlm_lock **lockp,
3055 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3056 struct ldlm_reply *ldlm_rep = NULL;
3060 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3061 * (for the resend case) or a new lock. Below we will use it to
3062 * replace the original lock.
3064 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3065 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3066 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3067 rc = mdt_object_lock(info, info->mti_object, lhc,
3068 MDS_INODELOCK_XATTR,
3074 grc = mdt_getxattr(info);
3076 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3078 if (mdt_info_req(info)->rq_repmsg != NULL)
3079 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3080 if (ldlm_rep == NULL)
3081 RETURN(err_serious(-EFAULT));
3083 ldlm_rep->lock_policy_res2 = grc;
3088 static int mdt_intent_getattr(enum mdt_it_code opcode,
3089 struct mdt_thread_info *info,
3090 struct ldlm_lock **lockp,
3093 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3094 struct ldlm_lock *new_lock = NULL;
3096 struct ldlm_reply *ldlm_rep;
3097 struct ptlrpc_request *req;
3098 struct mdt_body *reqbody;
3099 struct mdt_body *repbody;
3103 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3106 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3109 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3110 repbody->eadatasize = 0;
3111 repbody->aclsize = 0;
3115 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3117 case MDT_IT_GETATTR:
3118 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3122 CERROR("Unsupported intent (%d)\n", opcode);
3123 GOTO(out_shrink, rc = -EINVAL);
3126 rc = mdt_init_ucred(info, reqbody);
3128 GOTO(out_shrink, rc);
3130 req = info->mti_pill->rc_req;
3131 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3132 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3134 /* Get lock from request for possible resent case. */
3135 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3137 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3138 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3140 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3141 ldlm_rep->lock_policy_res2 = 0;
3142 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3143 ldlm_rep->lock_policy_res2) {
3144 lhc->mlh_reg_lh.cookie = 0ull;
3145 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3148 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3151 mdt_exit_ucred(info);
3153 mdt_client_compatibility(info);
3154 rc2 = mdt_fix_reply(info);
3160 static int mdt_intent_layout(enum mdt_it_code opcode,
3161 struct mdt_thread_info *info,
3162 struct ldlm_lock **lockp,
3165 struct layout_intent *layout;
3167 struct mdt_object *obj = NULL;
3168 struct md_object *child = NULL;
3172 if (opcode != MDT_IT_LAYOUT) {
3173 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3178 fid = &info->mti_tmp_fid2;
3179 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3181 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3183 RETURN(PTR_ERR(obj));
3185 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3186 child = mdt_object_child(obj);
3188 /* get the length of lsm */
3189 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3192 if (rc > info->mti_mdt->mdt_max_mdsize)
3193 info->mti_mdt->mdt_max_mdsize = rc;
3196 mdt_object_put(info->mti_env, obj);
3198 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3199 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3200 ldlm_lvbo_size(*lockp));
3201 rc = req_capsule_server_pack(info->mti_pill);
3205 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3206 LASSERT(layout != NULL);
3207 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3208 /* return to normal ldlm handling */
3211 CERROR("%s: Unsupported layout intent (%d)\n",
3212 mdt_obd_name(info->mti_mdt), layout->li_opc);
3216 static int mdt_intent_reint(enum mdt_it_code opcode,
3217 struct mdt_thread_info *info,
3218 struct ldlm_lock **lockp,
3221 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3222 struct ldlm_reply *rep = NULL;
3226 static const struct req_format *intent_fmts[REINT_MAX] = {
3227 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3228 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3233 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3237 if (mdt_it_flavor[opcode].it_reint != opc) {
3238 CERROR("Reint code %ld doesn't match intent: %d\n",
3240 RETURN(err_serious(-EPROTO));
3243 /* Get lock from request for possible resent case. */
3244 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3246 rc = mdt_reint_internal(info, lhc, opc);
3248 /* Check whether the reply has been packed successfully. */
3249 if (mdt_info_req(info)->rq_repmsg != NULL)
3250 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3252 RETURN(err_serious(-EFAULT));
3254 /* MDC expects this in any case */
3256 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3258 /* the open lock or the lock for cross-ref object should be
3259 * returned to the client */
3260 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3261 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3262 rep->lock_policy_res2 = 0;
3263 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3267 rep->lock_policy_res2 = clear_serious(rc);
3269 if (rep->lock_policy_res2 == -ENOENT &&
3270 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3271 rep->lock_policy_res2 = 0;
3273 if (rc == -ENOTCONN || rc == -ENODEV ||
3274 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3276 * If it is the disconnect error (ENODEV & ENOCONN), the error
3277 * will be returned by rq_status, and client at ptlrpc layer
3278 * will detect this, then disconnect, reconnect the import
3279 * immediately, instead of impacting the following the rpc.
3281 lhc->mlh_reg_lh.cookie = 0ull;
3285 * For other cases, the error will be returned by intent.
3286 * and client will retrieve the result from intent.
3289 * FIXME: when open lock is finished, that should be
3292 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3293 LASSERTF(rc == 0, "Error occurred but lock handle "
3294 "is still in use, rc = %d\n", rc);
3295 rep->lock_policy_res2 = 0;
3296 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3299 lhc->mlh_reg_lh.cookie = 0ull;
3300 RETURN(ELDLM_LOCK_ABORTED);
3305 static int mdt_intent_code(long itcode)
3313 case IT_OPEN|IT_CREAT:
3320 rc = MDT_IT_READDIR;
3323 rc = MDT_IT_GETATTR;
3335 rc = MDT_IT_GETXATTR;
3340 case IT_QUOTA_DQACQ:
3345 CERROR("Unknown intent opcode: %ld\n", itcode);
3352 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3353 struct ldlm_lock **lockp, __u64 flags)
3355 struct req_capsule *pill;
3356 struct mdt_it_flavor *flv;
3361 opc = mdt_intent_code(itopc);
3365 pill = info->mti_pill;
3367 if (opc == MDT_IT_QUOTA) {
3368 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3371 RETURN(-EOPNOTSUPP);
3373 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3374 /* pass the request to quota master */
3375 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3376 mdt_info_req(info), lockp,
3381 flv = &mdt_it_flavor[opc];
3382 if (flv->it_fmt != NULL)
3383 req_capsule_extend(pill, flv->it_fmt);
3385 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3387 struct ptlrpc_request *req = mdt_info_req(info);
3388 if (flv->it_flags & MUTABOR &&
3389 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3392 if (rc == 0 && flv->it_act != NULL) {
3393 struct ldlm_reply *rep;
3395 /* execute policy */
3396 rc = flv->it_act(opc, info, lockp, flags);
3398 /* Check whether the reply has been packed successfully. */
3399 if (mdt_info_req(info)->rq_repmsg != NULL) {
3400 rep = req_capsule_server_get(info->mti_pill,
3402 rep->lock_policy_res2 =
3403 ptlrpc_status_hton(rep->lock_policy_res2);
3411 static int mdt_intent_policy(struct ldlm_namespace *ns,
3412 struct ldlm_lock **lockp, void *req_cookie,
3413 ldlm_mode_t mode, __u64 flags, void *data)
3415 struct tgt_session_info *tsi;
3416 struct mdt_thread_info *info;
3417 struct ptlrpc_request *req = req_cookie;
3418 struct ldlm_intent *it;
3419 struct req_capsule *pill;
3424 LASSERT(req != NULL);
3426 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3428 info = tsi2mdt_info(tsi);
3429 LASSERT(info != NULL);
3430 pill = info->mti_pill;
3431 LASSERT(pill->rc_req == req);
3433 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3434 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3435 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3437 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3441 /* Lock without inodebits makes no sense and will oops
3442 * later in ldlm. Let's check it now to see if we have
3443 * ibits corrupted somewhere in mdt_intent_opc().
3444 * The case for client miss to set ibits has been
3445 * processed by others. */
3446 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3447 lr_type == LDLM_IBITS,
3448 info->mti_dlm_req->lock_desc.\
3449 l_policy_data.l_inodebits.bits != 0));
3451 rc = err_serious(-EFAULT);
3453 /* No intent was provided */
3454 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3455 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3456 rc = req_capsule_server_pack(pill);
3458 rc = err_serious(rc);
3460 mdt_thread_info_fini(info);
3464 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3466 struct seq_server_site *ss = mdt_seq_site(mdt);
3468 if (ss->ss_node_id == 0)
3471 if (ss->ss_client_seq != NULL) {
3472 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3473 ss->ss_client_seq->lcs_exp = NULL;
3476 if (ss->ss_server_fld != NULL) {
3477 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3478 ss->ss_server_fld->lsf_control_exp = NULL;
3482 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3484 struct seq_server_site *ss = mdt_seq_site(mdt);
3489 if (ss->ss_server_seq == NULL)
3490 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3495 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3497 mdt_seq_fini_cli(mdt);
3498 mdt_deregister_seq_exp(mdt);
3500 return seq_site_fini(env, mdt_seq_site(mdt));
3504 * It will retrieve its FLDB entries from MDT0, and it only happens
3505 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3506 * and it needs to refresh FLDB from the MDT0.
3508 static int mdt_register_lwp_callback(void *data)
3511 struct mdt_device *mdt = data;
3512 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3516 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3518 if (!likely(fld->lsf_new))
3521 rc = lu_env_init(&env, LCT_MD_THREAD);
3523 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3527 rc = fld_update_from_controller(&env, fld);
3529 CERROR("%s: cannot update controller: rc = %d\n",
3530 mdt_obd_name(mdt), rc);
3538 static int mdt_register_seq_exp(struct mdt_device *mdt)
3540 struct seq_server_site *ss = mdt_seq_site(mdt);
3541 char *lwp_name = NULL;
3544 if (ss->ss_node_id == 0)
3547 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3548 if (lwp_name == NULL)
3549 GOTO(out_free, rc = -ENOMEM);
3551 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3555 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3560 rc = lustre_register_lwp_item(lwp_name,
3561 &ss->ss_server_fld->lsf_control_exp,
3562 mdt_register_lwp_callback, mdt);
3564 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3565 ss->ss_client_seq->lcs_exp = NULL;
3569 if (lwp_name != NULL)
3570 OBD_FREE(lwp_name, MAX_OBD_NAME);
3576 * Init client sequence manager which is used by local MDS to talk to sequence
3577 * controller on remote node.
3579 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3581 struct seq_server_site *ss = mdt_seq_site(mdt);
3586 /* check if this is adding the first MDC and controller is not yet
3588 OBD_ALLOC_PTR(ss->ss_client_seq);
3589 if (ss->ss_client_seq == NULL)
3592 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3593 if (prefix == NULL) {
3594 OBD_FREE_PTR(ss->ss_client_seq);
3595 ss->ss_client_seq = NULL;
3599 /* Note: seq_client_fini will be called in seq_site_fini */
3600 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3601 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3602 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3604 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3606 OBD_FREE_PTR(ss->ss_client_seq);
3607 ss->ss_client_seq = NULL;
3611 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3616 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3618 struct seq_server_site *ss;
3622 ss = mdt_seq_site(mdt);
3623 /* init sequence controller server(MDT0) */
3624 if (ss->ss_node_id == 0) {
3625 OBD_ALLOC_PTR(ss->ss_control_seq);
3626 if (ss->ss_control_seq == NULL)
3629 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3630 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3633 GOTO(out_seq_fini, rc);
3636 /* Init normal sequence server */
3637 OBD_ALLOC_PTR(ss->ss_server_seq);
3638 if (ss->ss_server_seq == NULL)
3639 GOTO(out_seq_fini, rc = -ENOMEM);
3641 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3642 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3644 GOTO(out_seq_fini, rc);
3646 /* init seq client for seq server to talk to seq controller(MDT0) */
3647 rc = mdt_seq_init_cli(env, mdt);
3649 GOTO(out_seq_fini, rc);
3651 if (ss->ss_node_id != 0)
3652 /* register controler export through lwp */
3653 rc = mdt_register_seq_exp(mdt);
3658 mdt_seq_fini(env, mdt);
3666 static int mdt_fld_fini(const struct lu_env *env,
3667 struct mdt_device *m)
3669 struct seq_server_site *ss = mdt_seq_site(m);
3672 if (ss && ss->ss_server_fld) {
3673 fld_server_fini(env, ss->ss_server_fld);
3674 OBD_FREE_PTR(ss->ss_server_fld);
3675 ss->ss_server_fld = NULL;
3681 static int mdt_fld_init(const struct lu_env *env,
3683 struct mdt_device *m)
3685 struct seq_server_site *ss;
3689 ss = mdt_seq_site(m);
3691 OBD_ALLOC_PTR(ss->ss_server_fld);
3692 if (ss->ss_server_fld == NULL)
3693 RETURN(rc = -ENOMEM);
3695 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3698 OBD_FREE_PTR(ss->ss_server_fld);
3699 ss->ss_server_fld = NULL;
3706 static void mdt_stack_pre_fini(const struct lu_env *env,
3707 struct mdt_device *m, struct lu_device *top)
3709 struct obd_device *obd;
3710 struct lustre_cfg_bufs *bufs;
3711 struct lustre_cfg *lcfg;
3712 struct mdt_thread_info *info;
3717 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3718 LASSERT(info != NULL);
3720 bufs = &info->mti_u.bufs;
3722 LASSERT(m->mdt_child_exp);
3723 LASSERT(m->mdt_child_exp->exp_obd);
3724 obd = m->mdt_child_exp->exp_obd;
3726 /* process cleanup, pass mdt obd name to get obd umount flags */
3727 /* XXX: this is needed because all layers are referenced by
3728 * objects (some of them are pinned by osd, for example *
3729 * the proper solution should be a model where object used
3730 * by osd only doesn't have mdt/mdd slices -bzzz */
3731 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3732 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3733 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3735 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3738 top->ld_ops->ldo_process_config(env, top, lcfg);
3739 lustre_cfg_free(lcfg);
3743 static void mdt_stack_fini(const struct lu_env *env,
3744 struct mdt_device *m, struct lu_device *top)
3746 struct obd_device *obd = mdt2obd_dev(m);
3747 struct lustre_cfg_bufs *bufs;
3748 struct lustre_cfg *lcfg;
3749 struct mdt_thread_info *info;
3753 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3754 LASSERT(info != NULL);
3756 lu_dev_del_linkage(top->ld_site, top);
3758 lu_site_purge(env, top->ld_site, -1);
3760 bufs = &info->mti_u.bufs;
3761 /* process cleanup, pass mdt obd name to get obd umount flags */
3762 /* another purpose is to let all layers to release their objects */
3763 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3768 lustre_cfg_bufs_set_string(bufs, 1, flags);
3769 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3771 CERROR("Cannot alloc lcfg!\n");
3775 top->ld_ops->ldo_process_config(env, top, lcfg);
3776 lustre_cfg_free(lcfg);
3778 lu_site_purge(env, top->ld_site, -1);
3780 m->mdt_child = NULL;
3781 m->mdt_bottom = NULL;
3783 obd_disconnect(m->mdt_child_exp);
3784 m->mdt_child_exp = NULL;
3786 obd_disconnect(m->mdt_bottom_exp);
3787 m->mdt_child_exp = NULL;
3790 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3791 const char *next, struct obd_export **exp)
3793 struct obd_connect_data *data = NULL;
3794 struct obd_device *obd;
3798 OBD_ALLOC_PTR(data);
3800 GOTO(out, rc = -ENOMEM);
3802 obd = class_name2obd(next);
3804 CERROR("%s: can't locate next device: %s\n",
3805 mdt_obd_name(m), next);
3806 GOTO(out, rc = -ENOTCONN);
3809 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3810 data->ocd_version = LUSTRE_VERSION_CODE;
3812 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3814 CERROR("%s: cannot connect to next dev %s (%d)\n",
3815 mdt_obd_name(m), next, rc);
3825 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3826 struct lustre_cfg *cfg)
3828 char *dev = lustre_cfg_string(cfg, 0);
3829 int rc, name_size, uuid_size;
3830 char *name, *uuid, *p;
3831 struct lustre_cfg_bufs *bufs;
3832 struct lustre_cfg *lcfg;
3833 struct obd_device *obd;
3834 struct lustre_profile *lprof;
3835 struct lu_site *site;
3838 /* in 1.8 we had the only device in the stack - MDS.
3839 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3840 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3841 * to generate names and setup MDT, MDD. MDT will be using
3842 * generated name to connect to MDD. for MDD the next device
3843 * will be LOD with name taken from so called "profile" which
3844 * is generated by mount_option line
3846 * 1.8 MGS generates config. commands like this:
3847 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3848 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3849 * 2.0 MGS generates config. commands like this:
3850 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3851 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3852 * 3:lustre-MDT0000-mdtlov 4:f
3854 * we generate MDD name from MDT one, just replacing T with D
3856 * after all the preparations, the logical equivalent will be
3857 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3858 * 3:lustre-MDT0000-mdtlov 4:f
3859 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3860 * 3:lustre-MDD0000 4:f
3862 * notice we build the stack from down to top: MDD first, then MDT */
3864 name_size = MAX_OBD_NAME;
3865 uuid_size = MAX_OBD_NAME;
3867 OBD_ALLOC(name, name_size);
3868 OBD_ALLOC(uuid, uuid_size);
3869 if (name == NULL || uuid == NULL)
3870 GOTO(cleanup_mem, rc = -ENOMEM);
3872 OBD_ALLOC_PTR(bufs);
3874 GOTO(cleanup_mem, rc = -ENOMEM);
3877 p = strstr(name, "-MDT");
3879 GOTO(free_bufs, rc = -ENOMEM);
3882 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3884 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3885 if (lprof == NULL || lprof->lp_dt == NULL) {
3886 CERROR("can't find the profile: %s\n",
3887 lustre_cfg_string(cfg, 0));
3888 GOTO(free_bufs, rc = -EINVAL);
3891 lustre_cfg_bufs_reset(bufs, name);
3892 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3893 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3894 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3896 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3898 GOTO(free_bufs, rc = -ENOMEM);
3900 rc = class_attach(lcfg);
3902 GOTO(lcfg_cleanup, rc);
3904 obd = class_name2obd(name);
3906 CERROR("Can not find obd %s (%s in config)\n",
3907 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
3908 GOTO(class_detach, rc = -EINVAL);
3911 lustre_cfg_free(lcfg);
3913 lustre_cfg_bufs_reset(bufs, name);
3914 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3915 lustre_cfg_bufs_set_string(bufs, 2, dev);
3916 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3918 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3920 rc = class_setup(obd, lcfg);
3922 GOTO(class_detach, rc);
3924 /* connect to MDD we just setup */
3925 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
3927 GOTO(class_detach, rc);
3929 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
3931 LASSERT(mdt_lu_site(mdt) == NULL);
3932 mdt->mdt_lu_dev.ld_site = site;
3933 site->ls_top_dev = &mdt->mdt_lu_dev;
3934 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
3937 /* now connect to bottom OSD */
3938 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
3939 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
3941 GOTO(class_detach, rc);
3943 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
3946 rc = lu_env_refill((struct lu_env *)env);
3948 CERROR("Failure to refill session: '%d'\n", rc);
3950 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
3955 class_detach(obd, lcfg);
3957 lustre_cfg_free(lcfg);
3962 OBD_FREE(name, name_size);
3964 OBD_FREE(uuid, uuid_size);
3968 /* setup quota master target on MDT0 */
3969 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
3970 struct lustre_cfg *cfg)
3972 struct obd_device *obd;
3973 char *dev = lustre_cfg_string(cfg, 0);
3974 char *qmtname, *uuid, *p;
3975 struct lustre_cfg_bufs *bufs;
3976 struct lustre_cfg *lcfg;
3977 struct lustre_profile *lprof;
3978 struct obd_connect_data *data;
3982 LASSERT(mdt->mdt_qmt_exp == NULL);
3983 LASSERT(mdt->mdt_qmt_dev == NULL);
3985 /* quota master is on MDT0 only for now */
3986 if (mdt->mdt_seq_site.ss_node_id != 0)
3989 /* MGS generates config commands which look as follows:
3990 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3991 * 3:lustre-MDT0000-mdtlov 4:f
3993 * We generate the QMT name from the MDT one, just replacing MD with QM
3994 * after all the preparations, the logical equivalent will be:
3995 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
3996 * 3:lustre-MDT0000-osd 4:f */
3997 OBD_ALLOC(qmtname, MAX_OBD_NAME);
3998 OBD_ALLOC(uuid, UUID_MAX);
3999 OBD_ALLOC_PTR(bufs);
4000 OBD_ALLOC_PTR(data);
4001 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4002 GOTO(cleanup_mem, rc = -ENOMEM);
4004 strcpy(qmtname, dev);
4005 p = strstr(qmtname, "-MDT");
4007 GOTO(cleanup_mem, rc = -ENOMEM);
4008 /* replace MD with QM */
4012 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4014 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4015 if (lprof == NULL || lprof->lp_dt == NULL) {
4016 CERROR("can't find profile for %s\n",
4017 lustre_cfg_string(cfg, 0));
4018 GOTO(cleanup_mem, rc = -EINVAL);
4021 lustre_cfg_bufs_reset(bufs, qmtname);
4022 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4023 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4024 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4026 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4028 GOTO(cleanup_mem, rc = -ENOMEM);
4030 rc = class_attach(lcfg);
4032 GOTO(lcfg_cleanup, rc);
4034 obd = class_name2obd(qmtname);
4036 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4037 lustre_cfg_string(cfg, 0));
4038 GOTO(class_detach, rc = -EINVAL);
4041 lustre_cfg_free(lcfg);
4043 lustre_cfg_bufs_reset(bufs, qmtname);
4044 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4045 lustre_cfg_bufs_set_string(bufs, 2, dev);
4047 /* for quota, the next device should be the OSD device */
4048 lustre_cfg_bufs_set_string(bufs, 3,
4049 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4051 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4053 rc = class_setup(obd, lcfg);
4055 GOTO(class_detach, rc);
4057 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4059 /* configure local quota objects */
4060 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4064 GOTO(class_cleanup, rc);
4066 /* connect to quota master target */
4067 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4068 data->ocd_version = LUSTRE_VERSION_CODE;
4069 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4072 CERROR("cannot connect to quota master device %s (%d)\n",
4074 GOTO(class_cleanup, rc);
4080 class_manual_cleanup(obd);
4081 mdt->mdt_qmt_dev = NULL;
4085 class_detach(obd, lcfg);
4087 lustre_cfg_free(lcfg);
4092 OBD_FREE(qmtname, MAX_OBD_NAME);
4094 OBD_FREE(uuid, UUID_MAX);
4100 /* Shutdown quota master target associated with mdt */
4101 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4105 if (mdt->mdt_qmt_exp == NULL)
4107 LASSERT(mdt->mdt_qmt_dev != NULL);
4109 /* the qmt automatically shuts down when the mdt disconnects */
4110 obd_disconnect(mdt->mdt_qmt_exp);
4111 mdt->mdt_qmt_exp = NULL;
4112 mdt->mdt_qmt_dev = NULL;
4116 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4117 * for now. This will be removed along with converting rest of MDT code
4118 * to use tgt_session_info */
4119 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4121 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4124 rc = mdt_getxattr(info);
4126 mdt_thread_info_fini(info);
4130 static struct tgt_handler mdt_tgt_handlers[] = {
4131 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4132 0, MDS_CONNECT, mdt_tgt_connect,
4133 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4134 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4135 0, MDS_DISCONNECT, tgt_disconnect,
4136 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4137 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4138 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4139 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4140 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4141 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4142 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4143 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4145 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4146 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4147 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4148 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4149 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4151 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4152 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4153 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4154 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4155 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4157 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4158 mdt_hsm_ct_register),
4159 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4160 mdt_hsm_ct_unregister),
4161 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4163 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4165 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4166 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4168 TGT_MDT_HDL(HABEO_CORPUS|HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
4172 static struct tgt_handler mdt_sec_ctx_ops[] = {
4173 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4174 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4175 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4178 static struct tgt_handler mdt_quota_ops[] = {
4179 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4182 static struct tgt_opc_slice mdt_common_slice[] = {
4184 .tos_opc_start = MDS_FIRST_OPC,
4185 .tos_opc_end = MDS_LAST_OPC,
4186 .tos_hs = mdt_tgt_handlers
4189 .tos_opc_start = OBD_FIRST_OPC,
4190 .tos_opc_end = OBD_LAST_OPC,
4191 .tos_hs = tgt_obd_handlers
4194 .tos_opc_start = LDLM_FIRST_OPC,
4195 .tos_opc_end = LDLM_LAST_OPC,
4196 .tos_hs = tgt_dlm_handlers
4199 .tos_opc_start = SEC_FIRST_OPC,
4200 .tos_opc_end = SEC_LAST_OPC,
4201 .tos_hs = mdt_sec_ctx_ops
4204 .tos_opc_start = UPDATE_OBJ,
4205 .tos_opc_end = UPDATE_LAST_OPC,
4206 .tos_hs = tgt_out_handlers
4209 .tos_opc_start = FLD_FIRST_OPC,
4210 .tos_opc_end = FLD_LAST_OPC,
4211 .tos_hs = fld_handlers
4214 .tos_opc_start = SEQ_FIRST_OPC,
4215 .tos_opc_end = SEQ_LAST_OPC,
4216 .tos_hs = seq_handlers
4219 .tos_opc_start = QUOTA_DQACQ,
4220 .tos_opc_end = QUOTA_LAST_OPC,
4221 .tos_hs = mdt_quota_ops
4224 .tos_opc_start = LLOG_FIRST_OPC,
4225 .tos_opc_end = LLOG_LAST_OPC,
4226 .tos_hs = tgt_llog_handlers
4229 .tos_opc_start = LFSCK_FIRST_OPC,
4230 .tos_opc_end = LFSCK_LAST_OPC,
4231 .tos_hs = tgt_lfsck_handlers
4239 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4241 struct md_device *next = m->mdt_child;
4242 struct lu_device *d = &m->mdt_lu_dev;
4243 struct obd_device *obd = mdt2obd_dev(m);
4244 struct lfsck_stop stop;
4247 target_recovery_fini(obd);
4248 ping_evictor_stop();
4249 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4251 if (m->mdt_opts.mo_coordinator)
4252 mdt_hsm_cdt_stop(m);
4254 mdt_hsm_cdt_fini(m);
4256 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4257 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4258 obd_exports_barrier(obd);
4259 obd_zombie_barrier();
4263 tgt_fini(env, &m->mdt_lut);
4264 mdt_fs_cleanup(env, m);
4265 upcall_cache_cleanup(m->mdt_identity_cache);
4266 m->mdt_identity_cache = NULL;
4268 if (m->mdt_namespace != NULL) {
4269 ldlm_namespace_free(m->mdt_namespace, NULL,
4270 d->ld_obd->obd_force);
4271 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4274 mdt_quota_fini(env, m);
4276 cfs_free_nidlist(&m->mdt_nosquash_nids);
4277 if (m->mdt_nosquash_str) {
4278 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4279 m->mdt_nosquash_str = NULL;
4280 m->mdt_nosquash_strlen = 0;
4283 stop.ls_status = LS_PAUSED;
4285 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4287 mdt_seq_fini(env, m);
4288 mdt_fld_fini(env, m);
4290 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4291 cfs_timer_disarm(&m->mdt_ck_timer);
4292 mdt_ck_thread_stop(m);
4297 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4299 LASSERT(atomic_read(&d->ld_ref) == 0);
4301 server_put_mount(mdt_obd_name(m));
4306 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4308 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4309 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4311 struct mdt_thread_info *info;
4312 struct obd_device *obd;
4313 const char *dev = lustre_cfg_string(cfg, 0);
4314 const char *num = lustre_cfg_string(cfg, 2);
4315 struct lustre_mount_info *lmi = NULL;
4316 struct lustre_sb_info *lsi;
4318 struct seq_server_site *ss_site;
4319 const char *identity_upcall = "NONE";
4320 struct md_device *next;
4326 lu_device_init(&m->mdt_lu_dev, ldt);
4328 * Environment (env) might be missing mdt_thread_key values at that
4329 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4332 * Usually device allocation path doesn't use module key values, but
4333 * mdt has to do a lot of work here, so allocate key value.
4335 rc = lu_env_refill((struct lu_env *)env);
4339 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4340 LASSERT(info != NULL);
4342 obd = class_name2obd(dev);
4343 LASSERT(obd != NULL);
4345 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4347 m->mdt_som_conf = 0;
4349 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4351 /* default is coordinator off, it is started through conf_param
4353 m->mdt_opts.mo_coordinator = 0;
4355 lmi = server_get_mount(dev);
4357 CERROR("Cannot get mount info for %s!\n", dev);
4360 lsi = s2lsi(lmi->lmi_sb);
4361 /* CMD is supported only in IAM mode */
4363 node_id = simple_strtol(num, NULL, 10);
4364 obd->u.obt.obt_magic = OBT_MAGIC;
4367 spin_lock_init(&m->mdt_ioepoch_lock);
4368 m->mdt_capa_timeout = CAPA_TIMEOUT;
4369 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4370 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4371 m->mdt_squash_uid = 0;
4372 m->mdt_squash_gid = 0;
4373 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4374 m->mdt_nosquash_str = NULL;
4375 m->mdt_nosquash_strlen = 0;
4376 init_rwsem(&m->mdt_squash_sem);
4377 spin_lock_init(&m->mdt_osfs_lock);
4378 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4379 m->mdt_enable_remote_dir = 0;
4380 m->mdt_enable_remote_dir_gid = 0;
4382 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4383 m->mdt_lu_dev.ld_obd = obd;
4384 /* Set this lu_device to obd for error handling purposes. */
4385 obd->obd_lu_dev = &m->mdt_lu_dev;
4387 /* init the stack */
4388 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4390 CERROR("%s: Can't init device stack, rc %d\n",
4391 mdt_obd_name(m), rc);
4396 ss_site = mdt_seq_site(m);
4397 s->ld_seq_site = ss_site;
4400 /* set server index */
4401 ss_site->ss_node_id = node_id;
4403 /* failover is the default
4404 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4405 * assumed whose ss_node_id == 0 XXX
4407 obd->obd_replayable = 1;
4408 /* No connection accepted until configurations will finish */
4409 obd->obd_no_conn = 1;
4411 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4412 char *str = lustre_cfg_string(cfg, 4);
4413 if (strchr(str, 'n')) {
4414 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4415 obd->obd_replayable = 0;
4419 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4421 GOTO(err_fini_stack, rc);
4423 rc = mdt_seq_init(env, m);
4425 GOTO(err_fini_fld, rc);
4427 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4428 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4429 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4430 LDLM_NAMESPACE_SERVER,
4431 LDLM_NAMESPACE_GREEDY,
4433 if (m->mdt_namespace == NULL)
4434 GOTO(err_fini_seq, rc = -ENOMEM);
4436 m->mdt_namespace->ns_lvbp = m;
4437 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4439 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4440 /* set obd_namespace for compatibility with old code */
4441 obd->obd_namespace = m->mdt_namespace;
4443 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4445 rc = mdt_hsm_cdt_init(m);
4447 CERROR("%s: error initializing coordinator, rc %d\n",
4448 mdt_obd_name(m), rc);
4449 GOTO(err_free_ns, rc);
4452 rc = mdt_ck_thread_start(m);
4454 GOTO(err_free_hsm, rc);
4456 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4457 OBD_FAIL_MDS_ALL_REQUEST_NET,
4458 OBD_FAIL_MDS_ALL_REPLY_NET);
4462 rc = mdt_fs_setup(env, m, obd, lsi);
4466 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4468 next = m->mdt_child;
4469 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4472 GOTO(err_fs_cleanup, rc);
4474 if (mntopts & MNTOPT_USERXATTR)
4475 m->mdt_opts.mo_user_xattr = 1;
4477 m->mdt_opts.mo_user_xattr = 0;
4479 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4481 GOTO(err_fs_cleanup, rc);
4483 if (mntopts & MNTOPT_ACL)
4484 m->mdt_opts.mo_acl = 1;
4486 m->mdt_opts.mo_acl = 0;
4488 /* XXX: to support suppgid for ACL, we enable identity_upcall
4489 * by default, otherwise, maybe got unexpected -EACCESS. */
4490 if (m->mdt_opts.mo_acl)
4491 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4493 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4495 &mdt_identity_upcall_cache_ops);
4496 if (IS_ERR(m->mdt_identity_cache)) {
4497 rc = PTR_ERR(m->mdt_identity_cache);
4498 m->mdt_identity_cache = NULL;
4499 GOTO(err_fs_cleanup, rc);
4502 rc = mdt_procfs_init(m, dev);
4504 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4505 GOTO(err_recovery, rc);
4508 rc = mdt_quota_init(env, m, cfg);
4510 GOTO(err_procfs, rc);
4512 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4513 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4514 "mdt_ldlm_client", m->mdt_ldlm_client);
4516 ping_evictor_start();
4518 /* recovery will be started upon mdt_prepare()
4519 * when the whole stack is complete and ready
4520 * to serve the requests */
4522 mdt_init_capa_ctxt(env, m);
4524 /* Reduce the initial timeout on an MDS because it doesn't need such
4525 * a long timeout as an OST does. Adaptive timeouts will adjust this
4526 * value appropriately. */
4527 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4528 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4534 target_recovery_fini(obd);
4535 upcall_cache_cleanup(m->mdt_identity_cache);
4536 m->mdt_identity_cache = NULL;
4538 mdt_fs_cleanup(env, m);
4540 tgt_fini(env, &m->mdt_lut);
4542 cfs_timer_disarm(&m->mdt_ck_timer);
4543 mdt_ck_thread_stop(m);
4545 mdt_hsm_cdt_fini(m);
4547 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4548 obd->obd_namespace = m->mdt_namespace = NULL;
4550 mdt_seq_fini(env, m);
4552 mdt_fld_fini(env, m);
4554 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4557 server_put_mount(dev);
4561 /* For interoperability, the left element is old parameter, the right one
4562 * is the new version of the parameter, if some parameter is deprecated,
4563 * the new version should be set as NULL. */
4564 static struct cfg_interop_param mdt_interop_param[] = {
4565 { "mdt.group_upcall", NULL },
4566 { "mdt.quota_type", NULL },
4567 { "mdd.quota_type", NULL },
4568 { "mdt.rootsquash", "mdt.root_squash" },
4569 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4573 /* used by MGS to process specific configurations */
4574 static int mdt_process_config(const struct lu_env *env,
4575 struct lu_device *d, struct lustre_cfg *cfg)
4577 struct mdt_device *m = mdt_dev(d);
4578 struct md_device *md_next = m->mdt_child;
4579 struct lu_device *next = md2lu_dev(md_next);
4583 switch (cfg->lcfg_command) {
4585 struct lprocfs_static_vars lvars;
4586 struct obd_device *obd = d->ld_obd;
4588 /* For interoperability */
4589 struct cfg_interop_param *ptr = NULL;
4590 struct lustre_cfg *old_cfg = NULL;
4593 param = lustre_cfg_string(cfg, 1);
4594 if (param == NULL) {
4595 CERROR("param is empty\n");
4600 ptr = class_find_old_param(param, mdt_interop_param);
4602 if (ptr->new_param == NULL) {
4604 CWARN("For interoperability, skip this %s."
4605 " It is obsolete.\n", ptr->old_param);
4609 CWARN("Found old param %s, changed it to %s.\n",
4610 ptr->old_param, ptr->new_param);
4613 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4620 lprocfs_mdt_init_vars(&lvars);
4621 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4623 if (rc > 0 || rc == -ENOSYS) {
4624 /* is it an HSM var ? */
4625 rc = class_process_proc_param(PARAM_HSM,
4626 hsm_cdt_get_proc_vars(),
4628 if (rc > 0 || rc == -ENOSYS)
4629 /* we don't understand; pass it on */
4630 rc = next->ld_ops->ldo_process_config(env, next,
4634 if (old_cfg != NULL)
4635 lustre_cfg_free(cfg);
4640 /* others are passed further */
4641 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4647 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4648 const struct lu_object_header *hdr,
4649 struct lu_device *d)
4651 struct mdt_object *mo;
4655 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
4657 struct lu_object *o;
4658 struct lu_object_header *h;
4661 h = &mo->mot_header;
4662 lu_object_header_init(h);
4663 lu_object_init(o, h, d);
4664 lu_object_add_top(h, o);
4665 o->lo_ops = &mdt_obj_ops;
4666 mutex_init(&mo->mot_ioepoch_mutex);
4667 mutex_init(&mo->mot_lov_mutex);
4668 init_rwsem(&mo->mot_open_sem);
4674 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4675 const struct lu_object_conf *unused)
4677 struct mdt_device *d = mdt_dev(o->lo_dev);
4678 struct lu_device *under;
4679 struct lu_object *below;
4683 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4684 PFID(lu_object_fid(o)));
4686 under = &d->mdt_child->md_lu_dev;
4687 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4688 if (below != NULL) {
4689 lu_object_add(o, below);
4696 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4698 struct mdt_object *mo = mdt_obj(o);
4699 struct lu_object_header *h;
4703 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4704 PFID(lu_object_fid(o)));
4706 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4707 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4710 lu_object_header_fini(h);
4711 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4716 static int mdt_object_print(const struct lu_env *env, void *cookie,
4717 lu_printer_t p, const struct lu_object *o)
4719 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4720 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4721 "flags="LPX64", epochcount=%d, writecount=%d)",
4722 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4723 mdto->mot_ioepoch_count, mdto->mot_writecount);
4726 static int mdt_prepare(const struct lu_env *env,
4727 struct lu_device *pdev,
4728 struct lu_device *cdev)
4730 struct mdt_device *mdt = mdt_dev(cdev);
4731 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4732 struct obd_device *obd = cdev->ld_obd;
4733 struct lfsck_start_param lsp;
4740 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4744 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4748 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4752 lsp.lsp_namespace = mdt->mdt_namespace;
4753 lsp.lsp_start = NULL;
4754 lsp.lsp_index_valid = 0;
4755 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
4756 OBD_IOC_START_LFSCK,
4759 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
4760 mdt_obd_name(mdt), rc);
4764 if (mdt->mdt_seq_site.ss_node_id == 0) {
4765 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4766 &mdt->mdt_md_root_fid);
4771 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4772 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4773 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4774 LASSERT(obd->obd_no_conn);
4775 spin_lock(&obd->obd_dev_lock);
4776 obd->obd_no_conn = 0;
4777 spin_unlock(&obd->obd_dev_lock);
4779 if (obd->obd_recovering == 0)
4780 mdt_postrecov(env, mdt);
4785 const struct lu_device_operations mdt_lu_ops = {
4786 .ldo_object_alloc = mdt_object_alloc,
4787 .ldo_process_config = mdt_process_config,
4788 .ldo_prepare = mdt_prepare,
4791 static const struct lu_object_operations mdt_obj_ops = {
4792 .loo_object_init = mdt_object_init,
4793 .loo_object_free = mdt_object_free,
4794 .loo_object_print = mdt_object_print
4797 static int mdt_obd_set_info_async(const struct lu_env *env,
4798 struct obd_export *exp,
4799 __u32 keylen, void *key,
4800 __u32 vallen, void *val,
4801 struct ptlrpc_request_set *set)
4807 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4808 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4816 * Match client and server connection feature flags.
4818 * Compute the compatibility flags for a connection request based on
4819 * features mutually supported by client and server.
4821 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4822 * must not be updated here, otherwise a partially initialized value may
4823 * be exposed. After the connection request is successfully processed,
4824 * the top-level MDT connect request handler atomically updates the export
4825 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4826 * reply. \see mdt_connect().
4828 * \param exp the obd_export associated with this client/target pair
4829 * \param mdt the target device for the connection
4830 * \param data stores data for this connect request
4833 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4834 * \retval -EBADE client and server feature requirements are incompatible
4836 static int mdt_connect_internal(struct obd_export *exp,
4837 struct mdt_device *mdt,
4838 struct obd_connect_data *data)
4840 LASSERT(data != NULL);
4842 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4843 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4845 /* If no known bits (which should not happen, probably,
4846 as everybody should support LOOKUP and UPDATE bits at least)
4847 revert to compat mode with plain locks. */
4848 if (!data->ocd_ibits_known &&
4849 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4850 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4852 if (!mdt->mdt_opts.mo_acl)
4853 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4855 if (!mdt->mdt_opts.mo_user_xattr)
4856 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4858 if (!mdt->mdt_som_conf)
4859 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4861 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4862 data->ocd_brw_size = min(data->ocd_brw_size,
4863 (__u32)MD_MAX_BRW_SIZE);
4864 if (data->ocd_brw_size == 0) {
4865 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4866 " ocd_version: %x ocd_grant: %d "
4867 "ocd_index: %u ocd_brw_size is "
4868 "unexpectedly zero, network data "
4869 "corruption? Refusing connection of this"
4872 exp->exp_client_uuid.uuid,
4873 exp, data->ocd_connect_flags, data->ocd_version,
4874 data->ocd_grant, data->ocd_index);
4879 /* NB: Disregard the rule against updating
4880 * exp_connect_data.ocd_connect_flags in this case, since
4881 * tgt_client_new() needs to know if this is a lightweight
4882 * connection, and it is safe to expose this flag before
4883 * connection processing completes. */
4884 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4885 spin_lock(&exp->exp_lock);
4886 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4887 spin_unlock(&exp->exp_lock);
4890 data->ocd_version = LUSTRE_VERSION_CODE;
4892 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4893 CWARN("%s: MDS requires FID support, but client not\n",
4898 if (mdt->mdt_som_conf &&
4899 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4900 OBD_CONNECT_MDS_MDS |
4901 OBD_CONNECT_SOM))) {
4902 CWARN("%s: MDS has SOM enabled, but client does not support "
4903 "it\n", mdt_obd_name(mdt));
4907 if (OCD_HAS_FLAG(data, PINGLESS)) {
4908 if (ptlrpc_pinger_suppress_pings()) {
4909 spin_lock(&exp->exp_obd->obd_dev_lock);
4910 list_del_init(&exp->exp_obd_chain_timed);
4911 spin_unlock(&exp->exp_obd->obd_dev_lock);
4913 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4917 data->ocd_max_easize = mdt->mdt_max_ea_size;
4922 /* mds_connect copy */
4923 static int mdt_obd_connect(const struct lu_env *env,
4924 struct obd_export **exp, struct obd_device *obd,
4925 struct obd_uuid *cluuid,
4926 struct obd_connect_data *data,
4929 struct obd_export *lexp;
4930 struct lustre_handle conn = { 0 };
4931 struct mdt_device *mdt;
4935 LASSERT(env != NULL);
4936 if (!exp || !obd || !cluuid)
4939 mdt = mdt_dev(obd->obd_lu_dev);
4942 * first, check whether the stack is ready to handle requests
4943 * XXX: probably not very appropriate method is used now
4944 * at some point we should find a better one
4946 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
4947 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
4948 rc = obd_get_info(env, mdt->mdt_child_exp,
4949 sizeof(KEY_OSP_CONNECTED),
4950 KEY_OSP_CONNECTED, NULL, NULL, NULL);
4953 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
4956 rc = class_connect(&conn, obd, cluuid);
4960 lexp = class_conn2export(&conn);
4961 LASSERT(lexp != NULL);
4963 rc = mdt_connect_internal(lexp, mdt, data);
4965 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
4968 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
4969 rc = tgt_client_new(env, lexp);
4971 mdt_export_stats_init(obd, lexp, localdata);
4973 /* For phase I, sync for cross-ref operation. */
4974 lexp->exp_keep_sync = 1;
4978 class_disconnect(lexp);
4987 static int mdt_obd_reconnect(const struct lu_env *env,
4988 struct obd_export *exp, struct obd_device *obd,
4989 struct obd_uuid *cluuid,
4990 struct obd_connect_data *data,
4996 if (exp == NULL || obd == NULL || cluuid == NULL)
4999 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5001 mdt_export_stats_init(obd, exp, localdata);
5006 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5007 struct mdt_thread_info *info,
5008 struct mdt_file_data *mfd)
5010 struct lu_context ses;
5014 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5019 lu_context_enter(&ses);
5021 mdt_ucred(info)->uc_valid = UCRED_OLD;
5022 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5024 lu_context_exit(&ses);
5025 lu_context_fini(&ses);
5031 static int mdt_export_cleanup(struct obd_export *exp)
5033 struct mdt_export_data *med = &exp->exp_mdt_data;
5034 struct obd_device *obd = exp->exp_obd;
5035 struct mdt_device *mdt;
5036 struct mdt_thread_info *info;
5038 CFS_LIST_HEAD(closing_list);
5039 struct mdt_file_data *mfd, *n;
5043 spin_lock(&med->med_open_lock);
5044 while (!cfs_list_empty(&med->med_open_head)) {
5045 cfs_list_t *tmp = med->med_open_head.next;
5046 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5048 /* Remove mfd handle so it can't be found again.
5049 * We are consuming the mfd_list reference here. */
5050 class_handle_unhash(&mfd->mfd_handle);
5051 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5053 spin_unlock(&med->med_open_lock);
5054 mdt = mdt_dev(obd->obd_lu_dev);
5055 LASSERT(mdt != NULL);
5057 rc = lu_env_init(&env, LCT_MD_THREAD);
5061 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5062 LASSERT(info != NULL);
5063 memset(info, 0, sizeof *info);
5064 info->mti_env = &env;
5065 info->mti_mdt = mdt;
5066 info->mti_exp = exp;
5068 if (!cfs_list_empty(&closing_list)) {
5069 struct md_attr *ma = &info->mti_attr;
5071 /* Close any open files (which may also cause orphan unlinking). */
5072 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5073 cfs_list_del_init(&mfd->mfd_list);
5074 ma->ma_need = ma->ma_valid = 0;
5076 /* This file is being closed due to an eviction, it
5077 * could have been modified and now dirty regarding to
5078 * HSM archive, check this!
5079 * The logic here is to mark a file dirty if there's a
5080 * chance it was dirtied before the client was evicted,
5081 * so that we don't have to wait for a release attempt
5082 * before finding out the file was actually dirty and
5083 * fail the release. Aggressively marking it dirty here
5084 * will cause the policy engine to attempt to
5085 * re-archive it; when rearchiving, we can compare the
5086 * current version to the HSM data_version and make the
5087 * archive request into a noop if it's not actually
5090 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5091 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5093 /* Don't unlink orphan on failover umount, LU-184 */
5094 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5095 ma->ma_valid = MA_FLAGS;
5096 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5098 mdt_mfd_close(info, mfd);
5101 info->mti_mdt = NULL;
5102 /* cleanup client slot early */
5103 /* Do not erase record for recoverable client. */
5104 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5105 tgt_client_del(&env, exp);
5111 static int mdt_obd_disconnect(struct obd_export *exp)
5117 class_export_get(exp);
5119 rc = server_disconnect_export(exp);
5121 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5123 rc = mdt_export_cleanup(exp);
5124 class_export_put(exp);
5128 /* FIXME: Can we avoid using these two interfaces? */
5129 static int mdt_init_export(struct obd_export *exp)
5131 struct mdt_export_data *med = &exp->exp_mdt_data;
5135 CFS_INIT_LIST_HEAD(&med->med_open_head);
5136 spin_lock_init(&med->med_open_lock);
5137 mutex_init(&med->med_idmap_mutex);
5138 med->med_idmap = NULL;
5139 spin_lock(&exp->exp_lock);
5140 exp->exp_connecting = 1;
5141 spin_unlock(&exp->exp_lock);
5143 /* self-export doesn't need client data and ldlm initialization */
5144 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5145 &exp->exp_client_uuid)))
5148 rc = tgt_client_alloc(exp);
5152 rc = ldlm_init_export(exp);
5159 tgt_client_free(exp);
5161 CERROR("%s: Failed to initialize export: rc = %d\n",
5162 exp->exp_obd->obd_name, rc);
5166 static int mdt_destroy_export(struct obd_export *exp)
5170 if (exp_connect_rmtclient(exp))
5171 mdt_cleanup_idmap(&exp->exp_mdt_data);
5173 target_destroy_export(exp);
5174 /* destroy can be called from failed obd_setup, so
5175 * checking uuid is safer than obd_self_export */
5176 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5177 &exp->exp_client_uuid)))
5180 ldlm_destroy_export(exp);
5181 tgt_client_free(exp);
5183 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5184 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5189 /** The maximum depth that fid2path() will search.
5190 * This is limited only because we want to store the fids for
5191 * historical path lookup purposes.
5193 #define MAX_PATH_DEPTH 100
5195 /** mdt_path() lookup structure. */
5196 struct path_lookup_info {
5197 __u64 pli_recno; /**< history point */
5198 __u64 pli_currec; /**< current record */
5199 struct lu_fid pli_fid;
5200 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5201 struct mdt_object *pli_mdt_obj;
5202 char *pli_path; /**< full path */
5204 int pli_linkno; /**< which hardlink to follow */
5205 int pli_fidcount; /**< number of \a pli_fids */
5208 static int mdt_links_read(struct mdt_thread_info *info,
5209 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5213 LASSERT(ldata->ld_buf->lb_buf != NULL);
5215 if (!mdt_object_exists(mdt_obj))
5218 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5219 ldata->ld_buf, XATTR_NAME_LINK);
5220 if (rc == -ERANGE) {
5221 /* Buf was too small, figure out what we need. */
5222 lu_buf_free(ldata->ld_buf);
5223 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5224 ldata->ld_buf, XATTR_NAME_LINK);
5227 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5228 if (ldata->ld_buf->lb_buf == NULL)
5230 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5231 ldata->ld_buf, XATTR_NAME_LINK);
5236 return linkea_init(ldata);
5239 static int mdt_path_current(struct mdt_thread_info *info,
5240 struct path_lookup_info *pli)
5242 struct mdt_device *mdt = info->mti_mdt;
5243 struct mdt_object *mdt_obj;
5244 struct link_ea_header *leh;
5245 struct link_ea_entry *lee;
5246 struct lu_name *tmpname = &info->mti_name;
5247 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5248 struct lu_buf *buf = &info->mti_big_buf;
5251 struct linkea_data ldata = { 0 };
5255 /* temp buffer for path element, the buffer will be finally freed
5256 * in mdt_thread_info_fini */
5257 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5258 if (buf->lb_buf == NULL)
5262 ptr = pli->pli_path + pli->pli_pathlen - 1;
5265 pli->pli_fidcount = 0;
5266 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5268 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5269 * so checking root_fid can only happen on MDT0. */
5270 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5271 &pli->pli_fids[pli->pli_fidcount])) {
5272 mdt_obj = mdt_object_find(info->mti_env, mdt,
5273 &pli->pli_fids[pli->pli_fidcount]);
5274 if (IS_ERR(mdt_obj))
5275 GOTO(out, rc = PTR_ERR(mdt_obj));
5276 if (mdt_object_remote(mdt_obj)) {
5277 mdt_object_put(info->mti_env, mdt_obj);
5278 GOTO(remote_out, rc = -EREMOTE);
5280 if (!mdt_object_exists(mdt_obj)) {
5281 mdt_object_put(info->mti_env, mdt_obj);
5282 GOTO(out, rc = -ENOENT);
5285 rc = mdt_links_read(info, mdt_obj, &ldata);
5286 mdt_object_put(info->mti_env, mdt_obj);
5291 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5292 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5293 /* If set, use link #linkno for path lookup, otherwise use
5294 link #0. Only do this for the final path element. */
5295 if (pli->pli_fidcount == 0 &&
5296 pli->pli_linkno < leh->leh_reccount) {
5298 for (count = 0; count < pli->pli_linkno; count++) {
5299 lee = (struct link_ea_entry *)
5300 ((char *)lee + reclen);
5301 linkea_entry_unpack(lee, &reclen, tmpname,
5304 if (pli->pli_linkno < leh->leh_reccount - 1)
5305 /* indicate to user there are more links */
5309 /* Pack the name in the end of the buffer */
5310 ptr -= tmpname->ln_namelen;
5311 if (ptr - 1 <= pli->pli_path)
5312 GOTO(out, rc = -EOVERFLOW);
5313 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5316 /* Store the parent fid for historic lookup */
5317 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5318 GOTO(out, rc = -EOVERFLOW);
5319 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5323 ptr++; /* skip leading / */
5324 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5331 /* Returns the full path to this fid, as of changelog record recno. */
5332 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5333 char *path, int pathlen, __u64 *recno, int *linkno,
5336 struct mdt_device *mdt = info->mti_mdt;
5337 struct path_lookup_info *pli;
5345 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5354 pli->pli_mdt_obj = obj;
5355 pli->pli_recno = *recno;
5356 pli->pli_path = path;
5357 pli->pli_pathlen = pathlen;
5358 pli->pli_linkno = *linkno;
5360 /* Retry multiple times in case file is being moved */
5361 while (tries-- && rc == -EAGAIN)
5362 rc = mdt_path_current(info, pli);
5364 /* return the last resolved fids to the client, so the client will
5365 * build the left path on another MDT for remote object */
5366 *fid = pli->pli_fids[pli->pli_fidcount];
5368 *recno = pli->pli_currec;
5369 /* Return next link index to caller */
5370 *linkno = pli->pli_linkno;
5377 static int mdt_fid2path(struct mdt_thread_info *info,
5378 struct getinfo_fid2path *fp)
5380 struct mdt_device *mdt = info->mti_mdt;
5381 struct mdt_object *obj;
5385 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5386 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5388 if (!fid_is_sane(&fp->gf_fid))
5391 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5392 CWARN("%s: "DFID" is invalid, sequence should be "
5393 ">= "LPX64"\n", mdt_obd_name(mdt),
5394 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5398 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5399 if (obj == NULL || IS_ERR(obj)) {
5400 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5405 if (mdt_object_remote(obj))
5407 else if (!mdt_object_exists(obj))
5413 mdt_object_put(info->mti_env, obj);
5414 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5415 PFID(&fp->gf_fid), rc);
5419 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5420 &fp->gf_linkno, &fp->gf_fid);
5422 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5423 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5425 mdt_object_put(info->mti_env, obj);
5430 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5431 void *val, int vallen)
5433 struct getinfo_fid2path *fpout, *fpin;
5436 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5439 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5440 lustre_swab_fid2path(fpin);
5442 memcpy(fpout, fpin, sizeof(*fpin));
5443 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5446 rc = mdt_fid2path(info, fpout);
5450 int mdt_get_info(struct tgt_session_info *tsi)
5460 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5462 CDEBUG(D_IOCTL, "No GETINFO key");
5463 RETURN(err_serious(-EFAULT));
5465 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5468 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5469 if (vallen == NULL) {
5470 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5471 RETURN(err_serious(-EFAULT));
5474 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5476 rc = req_capsule_server_pack(tsi->tsi_pill);
5478 RETURN(err_serious(rc));
5480 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5481 if (valout == NULL) {
5482 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5483 RETURN(err_serious(-EFAULT));
5486 if (KEY_IS(KEY_FID2PATH)) {
5487 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5489 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5490 mdt_thread_info_fini(info);
5497 /* Pass the ioc down */
5498 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5499 unsigned int cmd, int len, void *data)
5501 struct lu_context ioctl_session;
5502 struct md_device *next = mdt->mdt_child;
5506 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5509 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5510 lu_context_enter(&ioctl_session);
5511 env->le_ses = &ioctl_session;
5513 LASSERT(next->md_ops->mdo_iocontrol);
5514 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5516 lu_context_exit(&ioctl_session);
5517 lu_context_fini(&ioctl_session);
5521 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5523 struct obd_ioctl_data *data = karg;
5526 struct mdt_object *obj;
5527 struct mdt_lock_handle *lh;
5531 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5532 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5535 fid = (struct lu_fid *)data->ioc_inlbuf1;
5537 if (!fid_is_sane(fid))
5540 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5542 lh = &mti->mti_lh[MDT_LH_PARENT];
5543 mdt_lock_reg_init(lh, LCK_CR);
5545 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5547 RETURN(PTR_ERR(obj));
5549 if (mdt_object_remote(obj)) {
5552 * before calling version get the correct MDS should be
5553 * fid, this is error to find remote object here
5555 CERROR("nonlocal object "DFID"\n", PFID(fid));
5556 } else if (!mdt_object_exists(obj)) {
5557 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5560 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5561 *(__u64 *)data->ioc_inlbuf2 = version;
5564 mdt_object_unlock_put(mti, obj, lh, 1);
5568 /* ioctls on obd dev */
5569 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5570 void *karg, void *uarg)
5573 struct obd_device *obd = exp->exp_obd;
5574 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5575 struct dt_device *dt = mdt->mdt_bottom;
5579 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5580 rc = lu_env_init(&env, LCT_MD_THREAD);
5586 rc = mdt_device_sync(&env, mdt);
5588 case OBD_IOC_SET_READONLY:
5589 rc = dt->dd_ops->dt_ro(&env, dt);
5591 case OBD_IOC_ABORT_RECOVERY:
5592 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5593 target_stop_recovery_thread(obd);
5596 case OBD_IOC_CHANGELOG_REG:
5597 case OBD_IOC_CHANGELOG_DEREG:
5598 case OBD_IOC_CHANGELOG_CLEAR:
5599 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5601 case OBD_IOC_START_LFSCK: {
5602 struct md_device *next = mdt->mdt_child;
5603 struct obd_ioctl_data *data = karg;
5604 struct lfsck_start_param lsp;
5606 if (unlikely(data == NULL)) {
5611 lsp.lsp_namespace = mdt->mdt_namespace;
5612 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5613 lsp.lsp_index_valid = 0;
5614 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5617 case OBD_IOC_STOP_LFSCK: {
5618 struct md_device *next = mdt->mdt_child;
5619 struct obd_ioctl_data *data = karg;
5620 struct lfsck_stop *stop =
5621 (struct lfsck_stop *)(data->ioc_inlbuf1);
5623 stop->ls_status = LS_STOPPED;
5624 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, stop);
5627 case OBD_IOC_GET_OBJ_VERSION: {
5628 struct mdt_thread_info *mti;
5629 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5630 memset(mti, 0, sizeof *mti);
5631 mti->mti_env = &env;
5635 rc = mdt_ioc_version_get(mti, karg);
5638 case OBD_IOC_CATLOGLIST:
5639 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg);
5643 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5644 mdt_obd_name(mdt), cmd, rc);
5651 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5653 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5657 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5661 int mdt_obd_postrecov(struct obd_device *obd)
5666 rc = lu_env_init(&env, LCT_MD_THREAD);
5669 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5674 static struct obd_ops mdt_obd_device_ops = {
5675 .o_owner = THIS_MODULE,
5676 .o_set_info_async = mdt_obd_set_info_async,
5677 .o_connect = mdt_obd_connect,
5678 .o_reconnect = mdt_obd_reconnect,
5679 .o_disconnect = mdt_obd_disconnect,
5680 .o_init_export = mdt_init_export,
5681 .o_destroy_export = mdt_destroy_export,
5682 .o_iocontrol = mdt_iocontrol,
5683 .o_postrecov = mdt_obd_postrecov,
5686 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5687 struct lu_device *d)
5689 struct mdt_device *m = mdt_dev(d);
5696 static struct lu_device *mdt_device_free(const struct lu_env *env,
5697 struct lu_device *d)
5699 struct mdt_device *m = mdt_dev(d);
5702 lu_device_fini(&m->mdt_lu_dev);
5708 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5709 struct lu_device_type *t,
5710 struct lustre_cfg *cfg)
5712 struct lu_device *l;
5713 struct mdt_device *m;
5720 rc = mdt_init0(env, m, t, cfg);
5722 mdt_device_free(env, l);
5727 l = ERR_PTR(-ENOMEM);
5731 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5732 LU_KEY_INIT(mdt, struct mdt_thread_info);
5734 static void mdt_key_fini(const struct lu_context *ctx,
5735 struct lu_context_key *key, void* data)
5737 struct mdt_thread_info *info = data;
5739 if (info->mti_big_lmm) {
5740 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5741 info->mti_big_lmm = NULL;
5742 info->mti_big_lmmsize = 0;
5747 /* context key: mdt_thread_key */
5748 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5750 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5752 return lu_ucred(info->mti_env);
5755 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5757 return lu_ucred_check(info->mti_env);
5761 * Enable/disable COS (Commit On Sharing).
5763 * Set/Clear the COS flag in mdt options.
5765 * \param mdt mdt device
5766 * \param val 0 disables COS, other values enable COS
5768 void mdt_enable_cos(struct mdt_device *mdt, int val)
5773 mdt->mdt_opts.mo_cos = !!val;
5774 rc = lu_env_init(&env, LCT_LOCAL);
5775 if (unlikely(rc != 0)) {
5776 CWARN("lu_env initialization failed with rc = %d,"
5777 "cannot sync\n", rc);
5780 mdt_device_sync(&env, mdt);
5785 * Check COS (Commit On Sharing) status.
5787 * Return COS flag status.
5789 * \param mdt mdt device
5791 int mdt_cos_is_enabled(struct mdt_device *mdt)
5793 return mdt->mdt_opts.mo_cos != 0;
5796 static struct lu_device_type_operations mdt_device_type_ops = {
5797 .ldto_device_alloc = mdt_device_alloc,
5798 .ldto_device_free = mdt_device_free,
5799 .ldto_device_fini = mdt_device_fini
5802 static struct lu_device_type mdt_device_type = {
5803 .ldt_tags = LU_DEVICE_MD,
5804 .ldt_name = LUSTRE_MDT_NAME,
5805 .ldt_ops = &mdt_device_type_ops,
5806 .ldt_ctx_tags = LCT_MD_THREAD
5809 static int __init mdt_mod_init(void)
5811 struct lprocfs_static_vars lvars;
5814 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5815 FID_NOBRACE_LEN + 1);
5816 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5818 rc = lu_kmem_init(mdt_caches);
5822 rc = mds_mod_init();
5826 lprocfs_mdt_init_vars(&lvars);
5827 rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
5828 #ifndef HAVE_ONLY_PROCFS_SEQ
5831 LUSTRE_MDT_NAME, &mdt_device_type);
5836 lu_kmem_fini(mdt_caches);
5843 static void __exit mdt_mod_exit(void)
5845 class_unregister_type(LUSTRE_MDT_NAME);
5847 lu_kmem_fini(mdt_caches);
5850 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5851 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5852 MODULE_LICENSE("GPL");
5854 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);