1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_reint.c
38 * Lustre Metadata Target (mdt) reintegration routines
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Huang Hua <huanghua@clusterfs.com>
44 * Author: Yury Umanets <umka@clusterfs.com>
48 # define EXPORT_SYMTAB
50 #define DEBUG_SUBSYSTEM S_MDS
52 #include "mdt_internal.h"
55 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
58 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
59 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
60 &RMF_MDT_MD, RCL_SERVER);
62 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
64 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
68 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
72 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
73 struct mdt_object *object,
74 struct mdt_body *repbody)
78 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
79 * we won't go to below*/
80 if (repbody->valid & OBD_MD_FLMDSCAPA)
83 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
84 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
85 struct lustre_capa *capa;
87 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
89 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
90 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
93 repbody->valid |= OBD_MD_FLMDSCAPA;
99 static int mdt_md_create(struct mdt_thread_info *info)
101 struct mdt_device *mdt = info->mti_mdt;
102 struct mdt_object *parent;
103 struct mdt_object *child;
104 struct mdt_lock_handle *lh;
105 struct mdt_body *repbody;
106 struct md_attr *ma = &info->mti_attr;
107 struct mdt_reint_record *rr = &info->mti_rr;
108 struct lu_name *lname;
112 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
113 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
115 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
117 lh = &info->mti_lh[MDT_LH_PARENT];
118 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
120 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
121 MDS_INODELOCK_UPDATE);
123 RETURN(PTR_ERR(parent));
125 child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
126 if (likely(!IS_ERR(child))) {
127 struct md_object *next = mdt_object_child(parent);
129 ma->ma_need = MA_INODE;
131 /* capa for cross-ref will be stored here */
132 ma->ma_capa = req_capsule_server_get(info->mti_pill,
134 LASSERT(ma->ma_capa);
136 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
137 OBD_FAIL_MDS_REINT_CREATE_WRITE);
139 /* Let lower layer know current lock mode. */
140 info->mti_spec.sp_cr_mode =
141 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
144 * Do perform lookup sanity check. We do not know if name exists
147 info->mti_spec.sp_cr_lookup = 1;
148 info->mti_spec.sp_feat = &dt_directory_features;
150 lname = mdt_name(info->mti_env, (char *)rr->rr_name,
152 rc = mdo_create(info->mti_env, next, lname,
153 mdt_object_child(child),
154 &info->mti_spec, ma);
156 /* Return fid & attr to client. */
157 if (ma->ma_valid & MA_INODE)
158 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
159 mdt_object_fid(child));
161 mdt_object_put(info->mti_env, child);
165 mdt_create_pack_capa(info, rc, child, repbody);
166 mdt_object_unlock_put(info, parent, lh, rc);
170 /* Partial request to create object only */
171 static int mdt_md_mkobj(struct mdt_thread_info *info)
173 struct mdt_device *mdt = info->mti_mdt;
174 struct mdt_object *o;
175 struct mdt_body *repbody;
176 struct md_attr *ma = &info->mti_attr;
180 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
181 PFID(info->mti_rr.rr_fid2));
183 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
185 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
187 struct md_object *next = mdt_object_child(o);
189 ma->ma_need = MA_INODE;
193 * Cross-ref create can encounter already created obj in case of
194 * recovery, just get attr in that case.
196 if (mdt_object_exists(o) == 1) {
197 rc = mo_attr_get(info->mti_env, next, ma);
200 * Here, NO permission check for object_create,
201 * such check has been done on the original MDS.
203 rc = mo_object_create(info->mti_env, next,
204 &info->mti_spec, ma);
207 /* Return fid & attr to client. */
208 if (ma->ma_valid & MA_INODE)
209 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
212 mdt_object_put(info->mti_env, o);
216 mdt_create_pack_capa(info, rc, o, repbody);
220 /* In the raw-setattr case, we lock the child inode.
221 * In the write-back case or if being called from open,
222 * the client holds a lock already.
223 * We use the ATTR_FROM_OPEN (translated into MRF_SETATTR_LOCKED by
224 * mdt_setattr_unpack()) flag to tell these cases apart. */
225 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo, int flags)
227 struct md_attr *ma = &info->mti_attr;
228 struct mdt_lock_handle *lh;
233 /* attr shouldn't be set on remote object */
234 LASSERT(mdt_object_exists(mo) >= 0);
237 som_update = (info->mti_epoch->flags & MF_SOM_CHANGE);
239 /* Try to avoid object_lock if another epoch has been started
241 if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
244 lh = &info->mti_lh[MDT_LH_PARENT];
245 mdt_lock_reg_init(lh, LCK_PW);
247 if (!(flags & MRF_SETATTR_LOCKED)) {
248 __u64 lockpart = MDS_INODELOCK_UPDATE;
249 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
250 lockpart |= MDS_INODELOCK_LOOKUP;
252 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
257 /* Setattrs are syncronized through dlm lock taken above. If another
258 * epoch started, its attributes may be already flushed on disk,
260 if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
261 GOTO(out_unlock, rc = 0);
263 if (mdt_object_exists(mo) == 0)
264 GOTO(out_unlock, rc = -ENOENT);
266 /* all attrs are packed into mti_attr in unpack_setattr */
267 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
268 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
270 /* This is only for set ctime when rename's source is on remote MDS. */
271 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
272 ma->ma_attr_flags |= MDS_VTX_BYPASS;
274 /* all attrs are packed into mti_attr in unpack_setattr */
275 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
277 GOTO(out_unlock, rc);
279 /* Re-enable SIZEONMDS. */
281 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
282 mo->mot_ioepoch, PFID(mdt_object_fid(mo)),
284 mdt_sizeonmds_enable(info, mo);
289 mdt_object_unlock(info, mo, lh, rc);
293 static int mdt_reint_setattr(struct mdt_thread_info *info,
294 struct mdt_lock_handle *lhc)
296 struct md_attr *ma = &info->mti_attr;
297 struct mdt_reint_record *rr = &info->mti_rr;
298 struct ptlrpc_request *req = mdt_info_req(info);
299 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
300 struct mdt_file_data *mfd;
301 struct mdt_object *mo;
302 struct md_object *next;
303 struct mdt_body *repbody;
307 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
308 (unsigned int)ma->ma_attr.la_valid);
310 if (info->mti_dlm_req)
311 ldlm_request_cancel(req, info->mti_dlm_req, 0);
313 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
314 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
316 GOTO(out, rc = PTR_ERR(mo));
318 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
319 if ((ma->ma_attr.la_valid & LA_SIZE) ||
320 (rr->rr_flags & MRF_SETATTR_LOCKED)) {
321 /* Check write access for the O_TRUNC case */
322 if (mdt_write_read(info->mti_mdt, mo) < 0)
323 GOTO(out_put, rc = -ETXTBSY);
325 } else if (info->mti_epoch &&
326 (info->mti_epoch->flags & MF_EPOCH_OPEN)) {
328 rc = mdt_write_get(info->mti_mdt, mo);
334 GOTO(out_put, rc = -ENOMEM);
336 mdt_epoch_open(info, mo);
337 repbody->ioepoch = mo->mot_ioepoch;
339 mdt_object_get(info->mti_env, mo);
340 mdt_mfd_set_mode(mfd, FMODE_EPOCHLCK);
341 mfd->mfd_object = mo;
342 mfd->mfd_xid = req->rq_xid;
344 spin_lock(&med->med_open_lock);
345 list_add(&mfd->mfd_list, &med->med_open_head);
346 spin_unlock(&med->med_open_lock);
347 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
350 if (info->mti_epoch && (info->mti_epoch->flags & MF_SOM_CHANGE))
351 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
353 rc = mdt_attr_set(info, mo, rr->rr_flags);
357 if (info->mti_epoch && (info->mti_epoch->flags & MF_SOM_CHANGE)) {
358 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
359 LASSERT(info->mti_epoch);
361 spin_lock(&med->med_open_lock);
362 /* Size-on-MDS Update. Find and free mfd. */
363 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
365 spin_unlock(&med->med_open_lock);
366 CDEBUG(D_INODE | D_ERROR, "no handle for file close: "
367 "fid = "DFID": cookie = "LPX64"\n",
368 PFID(info->mti_rr.rr_fid1),
369 info->mti_epoch->handle.cookie);
370 GOTO(out_put, rc = -ESTALE);
372 LASSERT(mfd->mfd_mode == FMODE_SOM);
373 LASSERT(!(info->mti_epoch->flags & MF_EPOCH_CLOSE));
375 class_handle_unhash(&mfd->mfd_handle);
376 list_del_init(&mfd->mfd_list);
377 spin_unlock(&med->med_open_lock);
379 mdt_mfd_close(info, mfd);
382 ma->ma_need = MA_INODE;
384 next = mdt_object_child(mo);
385 rc = mo_attr_get(info->mti_env, next, ma);
389 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
391 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
392 info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
393 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
394 (ma->ma_attr.la_valid & LA_SIZE)) {
395 struct lustre_capa *capa;
397 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
399 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
400 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
403 repbody->valid |= OBD_MD_FLOSSCAPA;
408 mdt_object_put(info->mti_env, mo);
410 mdt_shrink_reply(info);
414 static int mdt_reint_create(struct mdt_thread_info *info,
415 struct mdt_lock_handle *lhc)
420 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
421 RETURN(err_serious(-ESTALE));
423 if (info->mti_dlm_req)
424 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
426 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
428 /* Cross-ref case. */
429 if (info->mti_cross_ref) {
430 rc = mdt_md_mkobj(info);
440 /* Special file should stay on the same node as parent. */
441 LASSERT(info->mti_rr.rr_namelen > 0);
442 rc = mdt_md_create(info);
446 rc = err_serious(-EOPNOTSUPP);
451 static int mdt_reint_unlink(struct mdt_thread_info *info,
452 struct mdt_lock_handle *lhc)
454 struct mdt_reint_record *rr = &info->mti_rr;
455 struct ptlrpc_request *req = mdt_info_req(info);
456 struct md_attr *ma = &info->mti_attr;
457 struct lu_fid *child_fid = &info->mti_tmp_fid1;
458 struct mdt_object *mp;
459 struct mdt_object *mc;
460 struct mdt_lock_handle *parent_lh;
461 struct mdt_lock_handle *child_lh;
462 struct lu_name *lname;
466 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
469 if (info->mti_dlm_req)
470 ldlm_request_cancel(req, info->mti_dlm_req, 0);
472 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
473 RETURN(err_serious(-ENOENT));
476 * step 1: lock the parent. Note, this may be child in case of
477 * remote operation denoted by ->mti_cross_ref flag.
479 parent_lh = &info->mti_lh[MDT_LH_PARENT];
480 if (info->mti_cross_ref) {
482 * Init reg lock for cross ref case when we need to do only
485 mdt_lock_reg_init(parent_lh, LCK_PW);
487 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
490 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
491 MDS_INODELOCK_UPDATE);
494 /* errors are possible here in cross-ref cases, see below */
495 if (info->mti_cross_ref)
500 mdt_reint_init_ma(info, ma);
501 if (!ma->ma_lmm || !ma->ma_cookie)
502 GOTO(out_unlock_parent, rc = -EINVAL);
504 if (info->mti_cross_ref) {
506 * Remote partial operation. It is possible that replay may
507 * happen on parent MDT and this operation will be repeated.
508 * Therefore the object absense is allowed case and nothing
509 * should be done here.
511 if (mdt_object_exists(mp) > 0) {
512 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
513 rc = mo_ref_del(info->mti_env,
514 mdt_object_child(mp), ma);
516 mdt_handle_last_unlink(info, mp, ma);
519 GOTO(out_unlock_parent, rc);
522 /* step 2: find & lock the child */
523 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
524 rc = mdo_lookup(info->mti_env, mdt_object_child(mp),
525 lname, child_fid, &info->mti_spec);
527 GOTO(out_unlock_parent, rc);
529 /* We will lock the child regardless it is local or remote. No harm. */
530 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
532 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
533 child_lh = &info->mti_lh[MDT_LH_CHILD];
534 mdt_lock_reg_init(child_lh, LCK_EX);
535 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
538 mdt_object_put(info->mti_env, mc);
539 GOTO(out_unlock_parent, rc);
542 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
543 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
546 * Now we can only make sure we need MA_INODE, in mdd layer, will check
547 * whether need MA_LOV and MA_COOKIE.
549 ma->ma_need = MA_INODE;
551 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
552 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
553 mdt_object_child(mc), lname, ma);
555 mdt_handle_last_unlink(info, mc, ma);
558 mdt_object_unlock_put(info, mc, child_lh, rc);
560 mdt_object_unlock_put(info, mp, parent_lh, rc);
565 static int mdt_reint_link(struct mdt_thread_info *info,
566 struct mdt_lock_handle *lhc)
568 struct mdt_reint_record *rr = &info->mti_rr;
569 struct ptlrpc_request *req = mdt_info_req(info);
570 struct md_attr *ma = &info->mti_attr;
571 struct mdt_object *ms;
572 struct mdt_object *mp;
573 struct mdt_lock_handle *lhs;
574 struct mdt_lock_handle *lhp;
575 struct lu_name *lname;
579 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
580 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
582 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
583 RETURN(err_serious(-ENOENT));
585 if (info->mti_dlm_req)
586 ldlm_request_cancel(req, info->mti_dlm_req, 0);
588 if (info->mti_cross_ref) {
589 /* MDT holding name ask us to add ref. */
590 lhs = &info->mti_lh[MDT_LH_CHILD];
591 mdt_lock_reg_init(lhs, LCK_EX);
592 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
593 MDS_INODELOCK_UPDATE);
597 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
598 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
599 mdt_object_unlock_put(info, ms, lhs, rc);
603 /* Invalid case so return error immediately instead of
605 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
608 /* step 1: find & lock the target parent dir */
609 lhp = &info->mti_lh[MDT_LH_PARENT];
610 mdt_lock_pdo_init(lhp, LCK_EX, rr->rr_name,
612 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
613 MDS_INODELOCK_UPDATE);
617 /* step 2: find & lock the source */
618 lhs = &info->mti_lh[MDT_LH_CHILD];
619 mdt_lock_reg_init(lhs, LCK_EX);
621 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
623 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
625 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
628 mdt_object_put(info->mti_env, ms);
629 GOTO(out_unlock_parent, rc);
632 /* step 3: link it */
633 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
634 OBD_FAIL_MDS_REINT_LINK_WRITE);
636 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
637 rc = mdo_link(info->mti_env, mdt_object_child(mp),
638 mdt_object_child(ms), lname, ma);
641 mdt_object_unlock_put(info, ms, lhs, rc);
643 mdt_object_unlock_put(info, mp, lhp, rc);
647 /* partial operation for rename */
648 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
650 struct mdt_reint_record *rr = &info->mti_rr;
651 struct ptlrpc_request *req = mdt_info_req(info);
652 struct md_attr *ma = &info->mti_attr;
653 struct mdt_object *mtgtdir;
654 struct mdt_object *mtgt = NULL;
655 struct mdt_lock_handle *lh_tgtdir;
656 struct mdt_lock_handle *lh_tgt = NULL;
657 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
658 struct lu_name *lname;
662 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
663 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
665 /* step 1: lookup & lock the tgt dir. */
666 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
667 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
669 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
670 MDS_INODELOCK_UPDATE);
672 RETURN(PTR_ERR(mtgtdir));
674 /* step 2: find & lock the target object if exists. */
675 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
676 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
677 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
678 lname, tgt_fid, &info->mti_spec);
679 if (rc != 0 && rc != -ENOENT) {
680 GOTO(out_unlock_tgtdir, rc);
681 } else if (rc == 0) {
683 * In case of replay that name can be already inserted, check
684 * that and do nothing if so.
686 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
687 GOTO(out_unlock_tgtdir, rc);
689 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
690 mdt_lock_reg_init(lh_tgt, LCK_EX);
692 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
693 MDS_INODELOCK_LOOKUP);
695 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
697 mdt_reint_init_ma(info, ma);
698 if (!ma->ma_lmm || !ma->ma_cookie)
699 GOTO(out_unlock_tgt, rc = -EINVAL);
701 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
702 mdt_object_child(mtgt), rr->rr_fid2,
704 } else /* -ENOENT */ {
705 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
706 lname, rr->rr_fid2, ma);
709 /* handle last link of tgt object */
711 mdt_handle_last_unlink(info, mtgt, ma);
716 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
718 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
722 static int mdt_rename_lock(struct mdt_thread_info *info,
723 struct lustre_handle *lh)
725 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
726 ldlm_policy_data_t *policy = &info->mti_policy;
727 struct ldlm_res_id *res_id = &info->mti_res_id;
733 * Disable global rename BFL lock temporarily because
734 * when a mds do rename recoverying, which might enqueue
735 * BFL lock to the controller mds. and this req might be
736 * replay req for controller mds. but we did not have
737 * such handling in controller mds. XXX
740 ms = mdt_md_site(info->mti_mdt);
741 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
743 memset(policy, 0, sizeof *policy);
744 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
746 if (ms->ms_control_exp == NULL) {
747 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
750 * Current node is controller, that is mdt0, where we should
753 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
754 LCK_EX, &flags, ldlm_blocking_ast,
755 ldlm_completion_ast, NULL, NULL, 0,
757 &info->mti_exp->exp_handle.h_cookie,
760 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
761 ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
765 * This is the case mdt0 is remote node, issue DLM lock like
768 rc = ldlm_cli_enqueue(ms->ms_control_exp,
769 NULL, &einfo, res_id,
770 policy, &flags, NULL, 0, NULL, lh, 0);
776 static void mdt_rename_unlock(struct lustre_handle *lh)
779 /* Disable global rename BFL lock temporarily. see above XXX*/
782 LASSERT(lustre_handle_is_used(lh));
783 ldlm_lock_decref(lh, LCK_EX);
788 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
789 * target. Source should not be ancestor of target dir. May be other rename
790 * checks can be moved here later.
792 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
794 struct mdt_reint_record *rr = &info->mti_rr;
795 struct lu_fid dst_fid = *rr->rr_fid2;
796 struct mdt_object *dst;
801 LASSERT(fid_is_sane(&dst_fid));
802 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
804 rc = mdo_is_subdir(info->mti_env,
805 mdt_object_child(dst), fid,
807 mdt_object_put(info->mti_env, dst);
808 if (rc != -EREMOTE && rc < 0) {
809 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
811 /* check the found fid */
812 if (lu_fid_eq(&dst_fid, fid))
818 } while (rc == -EREMOTE);
823 static int mdt_reint_rename(struct mdt_thread_info *info,
824 struct mdt_lock_handle *lhc)
826 struct mdt_reint_record *rr = &info->mti_rr;
827 struct md_attr *ma = &info->mti_attr;
828 struct ptlrpc_request *req = mdt_info_req(info);
829 struct mdt_object *msrcdir;
830 struct mdt_object *mtgtdir;
831 struct mdt_object *mold;
832 struct mdt_object *mnew = NULL;
833 struct mdt_lock_handle *lh_srcdirp;
834 struct mdt_lock_handle *lh_tgtdirp;
835 struct mdt_lock_handle *lh_oldp;
836 struct mdt_lock_handle *lh_newp;
837 struct lu_fid *old_fid = &info->mti_tmp_fid1;
838 struct lu_fid *new_fid = &info->mti_tmp_fid2;
839 struct lustre_handle rename_lh = { 0 };
840 struct lu_name slname = { 0 };
841 struct lu_name *lname;
845 if (info->mti_dlm_req)
846 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
848 if (info->mti_cross_ref) {
849 rc = mdt_reint_rename_tgt(info);
853 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
854 PFID(rr->rr_fid1), rr->rr_name,
855 PFID(rr->rr_fid2), rr->rr_tgt);
857 rc = mdt_rename_lock(info, &rename_lh);
859 CERROR("Can't lock FS for rename, rc %d\n", rc);
863 lh_newp = &info->mti_lh[MDT_LH_NEW];
865 /* step 1: lock the source dir. */
866 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
867 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
869 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
870 MDS_INODELOCK_UPDATE);
872 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
874 /* step 2: find & lock the target dir. */
875 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
876 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
878 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
879 mdt_object_get(info->mti_env, msrcdir);
882 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
885 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
887 rc = mdt_object_exists(mtgtdir);
889 GOTO(out_unlock_target, rc = -ESTALE);
891 /* we lock the target dir if it is local */
892 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
893 MDS_INODELOCK_UPDATE,
896 GOTO(out_unlock_target, rc);
900 /* step 3: find & lock the old object. */
901 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
902 mdt_name_copy(&slname, lname);
903 rc = mdo_lookup(info->mti_env, mdt_object_child(msrcdir),
904 &slname, old_fid, &info->mti_spec);
906 GOTO(out_unlock_target, rc);
908 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
909 GOTO(out_unlock_target, rc = -EINVAL);
911 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
913 GOTO(out_unlock_target, rc = PTR_ERR(mold));
915 lh_oldp = &info->mti_lh[MDT_LH_OLD];
916 mdt_lock_reg_init(lh_oldp, LCK_EX);
917 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
920 mdt_object_put(info->mti_env, mold);
921 GOTO(out_unlock_target, rc);
923 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
925 /* step 4: find & lock the new object. */
926 /* new target object may not exist now */
927 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
928 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
929 lname, new_fid, &info->mti_spec);
931 /* the new_fid should have been filled at this moment */
932 if (lu_fid_eq(old_fid, new_fid))
933 GOTO(out_unlock_old, rc);
935 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
936 lu_fid_eq(new_fid, rr->rr_fid2))
937 GOTO(out_unlock_old, rc = -EINVAL);
939 mdt_lock_reg_init(lh_newp, LCK_EX);
940 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
942 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
944 rc = mdt_object_lock(info, mnew, lh_newp,
945 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
947 mdt_object_put(info->mti_env, mnew);
948 GOTO(out_unlock_old, rc);
950 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
951 } else if (rc != -EREMOTE && rc != -ENOENT)
952 GOTO(out_unlock_old, rc);
954 /* step 5: rename it */
955 mdt_reint_init_ma(info, ma);
956 if (!ma->ma_lmm || !ma->ma_cookie)
957 GOTO(out_unlock_new, rc = -EINVAL);
959 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
960 OBD_FAIL_MDS_REINT_RENAME_WRITE);
963 /* Check if @dst is subdir of @src. */
964 rc = mdt_rename_sanity(info, old_fid);
966 GOTO(out_unlock_new, rc);
968 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
969 mdt_object_child(mtgtdir), old_fid, &slname,
970 (mnew ? mdt_object_child(mnew) : NULL),
973 /* handle last link of tgt object */
975 mdt_handle_last_unlink(info, mnew, ma);
980 mdt_object_unlock_put(info, mnew, lh_newp, rc);
982 mdt_object_unlock_put(info, mold, lh_oldp, rc);
984 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
986 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
988 mdt_rename_unlock(&rename_lh);
992 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
993 struct mdt_lock_handle *lhc);
995 static mdt_reinter reinters[REINT_MAX] = {
996 [REINT_SETATTR] = mdt_reint_setattr,
997 [REINT_CREATE] = mdt_reint_create,
998 [REINT_LINK] = mdt_reint_link,
999 [REINT_UNLINK] = mdt_reint_unlink,
1000 [REINT_RENAME] = mdt_reint_rename,
1001 [REINT_OPEN] = mdt_reint_open,
1002 [REINT_SETXATTR] = mdt_reint_setxattr
1005 int mdt_reint_rec(struct mdt_thread_info *info,
1006 struct mdt_lock_handle *lhc)
1011 rc = reinters[info->mti_rr.rr_opcode](info, lhc);