1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mdt/mdt_reint.c
5 * Lustre Metadata Target (mdt) reintegration routines
7 * Copyright (C) 2002-2006 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
11 * Author: Huang Hua <huanghua@clusterfs.com>
12 * Author: Yury Umanets <umka@clusterfs.com>
14 * This file is part of the Lustre file system, http://www.lustre.org
15 * Lustre is a trademark of Cluster File Systems, Inc.
17 * You may have signed or agreed to another license before downloading
18 * this software. If so, you are bound by the terms and conditions
19 * of that agreement, and the following does not apply to you. See the
20 * LICENSE file included with this distribution for more information.
22 * If you did not agree to a different license, then this copy of Lustre
23 * is open source software; you can redistribute it and/or modify it
24 * under the terms of version 2 of the GNU General Public License as
25 * published by the Free Software Foundation.
27 * In either case, Lustre is distributed in the hope that it will be
28 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * license text for more details.
34 # define EXPORT_SYMTAB
36 #define DEBUG_SUBSYSTEM S_MDS
38 #include "mdt_internal.h"
41 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
44 ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
45 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
46 &RMF_MDT_MD, RCL_SERVER);
48 ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
50 ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
54 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
58 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
59 struct mdt_object *object,
60 struct mdt_body *repbody)
64 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
65 * we won't go to below*/
66 if (repbody->valid & OBD_MD_FLMDSCAPA)
69 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa) {
70 struct lustre_capa *capa;
72 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
74 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
75 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
78 repbody->valid |= OBD_MD_FLMDSCAPA;
80 if (!(repbody->valid & OBD_MD_FLMDSCAPA))
81 lustre_shrink_reply(mdt_info_req(info), REPLY_REC_OFF+1, 0, 1);
86 static int mdt_md_create(struct mdt_thread_info *info)
88 struct mdt_device *mdt = info->mti_mdt;
89 struct mdt_object *parent;
90 struct mdt_object *child;
91 struct mdt_lock_handle *lh;
92 struct mdt_body *repbody;
93 struct md_attr *ma = &info->mti_attr;
94 struct mdt_reint_record *rr = &info->mti_rr;
95 struct lu_name *lname;
99 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
100 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
102 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
104 lh = &info->mti_lh[MDT_LH_PARENT];
105 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
107 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
108 MDS_INODELOCK_UPDATE);
110 RETURN(PTR_ERR(parent));
112 child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
113 if (likely(!IS_ERR(child))) {
114 struct md_object *next = mdt_object_child(parent);
116 ma->ma_need = MA_INODE;
118 /* capa for cross-ref will be stored here */
119 ma->ma_capa = req_capsule_server_get(&info->mti_pill,
121 LASSERT(ma->ma_capa);
123 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
124 OBD_FAIL_MDS_REINT_CREATE_WRITE);
126 /* Let lower layer know current lock mode. */
127 info->mti_spec.sp_cr_mode =
128 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
131 * Do perform lookup sanity check. We do not know if name exists
134 info->mti_spec.sp_cr_lookup = 1;
136 lname = mdt_name(info->mti_env, (char *)rr->rr_name,
138 rc = mdo_create(info->mti_env, next, lname,
139 mdt_object_child(child),
140 &info->mti_spec, ma);
142 /* Return fid & attr to client. */
143 if (ma->ma_valid & MA_INODE)
144 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
145 mdt_object_fid(child));
147 mdt_object_put(info->mti_env, child);
151 mdt_create_pack_capa(info, rc, child, repbody);
152 mdt_object_unlock_put(info, parent, lh, rc);
156 /* Partial request to create object only */
157 static int mdt_md_mkobj(struct mdt_thread_info *info)
159 struct mdt_device *mdt = info->mti_mdt;
160 struct mdt_object *o;
161 struct mdt_body *repbody;
162 struct md_attr *ma = &info->mti_attr;
166 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
167 PFID(info->mti_rr.rr_fid2));
169 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
171 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
173 struct md_object *next = mdt_object_child(o);
175 ma->ma_need = MA_INODE;
179 * Cross-ref create can encounter already created obj in case of
180 * recovery, just get attr in that case.
182 if (mdt_object_exists(o) == 1) {
183 rc = mo_attr_get(info->mti_env, next, ma);
186 * Here, NO permission check for object_create,
187 * such check has been done on the original MDS.
189 rc = mo_object_create(info->mti_env, next,
190 &info->mti_spec, ma);
193 /* Return fid & attr to client. */
194 if (ma->ma_valid & MA_INODE)
195 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
198 mdt_object_put(info->mti_env, o);
202 mdt_create_pack_capa(info, rc, o, repbody);
206 /* In the raw-setattr case, we lock the child inode.
207 * In the write-back case or if being called from open,
208 * the client holds a lock already.
209 * We use the ATTR_FROM_OPEN (translated into MRF_SETATTR_LOCKED by
210 * mdt_setattr_unpack()) flag to tell these cases apart. */
211 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo, int flags)
213 struct md_attr *ma = &info->mti_attr;
214 struct mdt_lock_handle *lh;
219 /* attr shouldn't be set on remote object */
220 LASSERT(mdt_object_exists(mo) >= 0);
223 som_update = (info->mti_epoch->flags & MF_SOM_CHANGE);
225 /* Try to avoid object_lock if another epoch has been started
227 if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
230 lh = &info->mti_lh[MDT_LH_PARENT];
231 mdt_lock_reg_init(lh, LCK_PW);
233 if (!(flags & MRF_SETATTR_LOCKED)) {
234 __u64 lockpart = MDS_INODELOCK_UPDATE;
235 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
236 lockpart |= MDS_INODELOCK_LOOKUP;
238 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
243 /* Setattrs are syncronized through dlm lock taken above. If another
244 * epoch started, its attributes may be already flushed on disk,
246 if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
247 GOTO(out_unlock, rc = 0);
249 if (mdt_object_exists(mo) == 0)
250 GOTO(out_unlock, rc = -ENOENT);
252 /* all attrs are packed into mti_attr in unpack_setattr */
253 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
254 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
256 /* This is only for set ctime when rename's source is on remote MDS. */
257 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
258 ma->ma_attr_flags |= MDS_VTX_BYPASS;
260 /* all attrs are packed into mti_attr in unpack_setattr */
261 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
263 GOTO(out_unlock, rc);
265 /* Re-enable SIZEONMDS. */
267 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
268 mo->mot_ioepoch, PFID(mdt_object_fid(mo)),
270 mdt_sizeonmds_enable(info, mo);
275 mdt_object_unlock(info, mo, lh, rc);
279 static int mdt_reint_setattr(struct mdt_thread_info *info,
280 struct mdt_lock_handle *lhc)
282 struct mdt_device *mdt = info->mti_mdt;
283 struct md_attr *ma = &info->mti_attr;
284 struct mdt_reint_record *rr = &info->mti_rr;
285 struct ptlrpc_request *req = mdt_info_req(info);
286 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
287 struct mdt_file_data *mfd;
288 struct mdt_object *mo;
289 struct md_object *next;
290 struct mdt_body *repbody;
294 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
295 (unsigned int)ma->ma_attr.la_valid);
297 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
298 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
300 GOTO(out, rc = PTR_ERR(mo));
302 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
303 if ((ma->ma_attr.la_valid & LA_SIZE) ||
304 (rr->rr_flags & MRF_SETATTR_LOCKED)) {
305 /* Check write access for the O_TRUNC case */
306 if (mdt_write_read(info->mti_mdt, mo) < 0)
307 GOTO(out_put, rc = -ETXTBSY);
309 } else if (info->mti_epoch &&
310 (info->mti_epoch->flags & MF_EPOCH_OPEN)) {
312 rc = mdt_write_get(info->mti_mdt, mo);
318 GOTO(out_put, rc = -ENOMEM);
320 mdt_epoch_open(info, mo);
321 repbody->ioepoch = mo->mot_ioepoch;
323 mdt_object_get(info->mti_env, mo);
324 mdt_mfd_set_mode(mfd, FMODE_EPOCHLCK);
325 mfd->mfd_object = mo;
326 mfd->mfd_xid = req->rq_xid;
328 spin_lock(&med->med_open_lock);
329 list_add(&mfd->mfd_list, &med->med_open_head);
330 spin_unlock(&med->med_open_lock);
331 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
334 rc = mdt_attr_set(info, mo, rr->rr_flags);
338 if (info->mti_epoch && (info->mti_epoch->flags & MF_SOM_CHANGE)) {
339 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
340 LASSERT(info->mti_epoch);
342 spin_lock(&med->med_open_lock);
343 /* Size-on-MDS Update. Find and free mfd. */
344 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
346 spin_unlock(&med->med_open_lock);
347 CDEBUG(D_INODE | D_ERROR, "no handle for file close: "
348 "fid = "DFID": cookie = "LPX64"\n",
349 PFID(info->mti_rr.rr_fid1),
350 info->mti_epoch->handle.cookie);
351 GOTO(out_put, rc = -ESTALE);
353 if (mfd->mfd_mode != FMODE_SOM) {
354 CWARN("mfd_mode %d not right should be in replay\n",
356 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
358 LASSERT(mfd->mfd_mode == FMODE_EPOCH);
359 mfd->mfd_mode = FMODE_SOM;
362 LASSERT(ma->ma_attr.la_valid & LA_SIZE);
363 LASSERT(!(info->mti_epoch->flags & MF_EPOCH_CLOSE));
365 class_handle_unhash(&mfd->mfd_handle);
366 list_del_init(&mfd->mfd_list);
367 spin_unlock(&med->med_open_lock);
369 mdt_mfd_close(info, mfd);
372 ma->ma_need = MA_INODE;
374 next = mdt_object_child(mo);
375 rc = mo_attr_get(info->mti_env, next, ma);
379 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
381 if (mdt->mdt_opts.mo_oss_capa &&
382 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
383 (ma->ma_attr.la_valid & LA_SIZE)) {
384 struct lustre_capa *capa;
386 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
388 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
389 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
392 repbody->valid |= OBD_MD_FLOSSCAPA;
397 mdt_object_put(info->mti_env, mo);
402 static int mdt_reint_create(struct mdt_thread_info *info,
403 struct mdt_lock_handle *lhc)
408 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
409 GOTO(out, rc = err_serious(-ESTALE));
411 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
413 /* Cross-ref case. */
414 if (info->mti_cross_ref) {
415 rc = mdt_md_mkobj(info);
425 /* Special file should stay on the same node as parent. */
426 LASSERT(info->mti_rr.rr_namelen > 0);
427 rc = mdt_md_create(info);
431 rc = err_serious(-EOPNOTSUPP);
438 static int mdt_reint_unlink(struct mdt_thread_info *info,
439 struct mdt_lock_handle *lhc)
441 struct mdt_reint_record *rr = &info->mti_rr;
442 struct ptlrpc_request *req = mdt_info_req(info);
443 struct md_attr *ma = &info->mti_attr;
444 struct lu_fid *child_fid = &info->mti_tmp_fid1;
445 struct mdt_object *mp;
446 struct mdt_object *mc;
447 struct mdt_lock_handle *parent_lh;
448 struct mdt_lock_handle *child_lh;
449 struct lu_name *lname;
453 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
456 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
457 GOTO(out, rc = err_serious(-ENOENT));
459 /* step 1: lock the parent */
460 parent_lh = &info->mti_lh[MDT_LH_PARENT];
461 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
464 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
465 MDS_INODELOCK_UPDATE);
468 /* errors are possible here in cross-ref cases, see below */
469 if (info->mti_cross_ref)
474 mdt_reint_init_ma(info, ma);
475 if (!ma->ma_lmm || !ma->ma_cookie)
476 GOTO(out_unlock_parent, rc = -EINVAL);
478 if (info->mti_cross_ref) {
480 * Remote partial operation. It is possible that replay may
481 * happen on parent MDT and this operation will be repeated.
482 * Therefore the object absense is allowed case and nothing
483 * should be done here.
485 if (mdt_object_exists(mp) > 0) {
486 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
487 rc = mo_ref_del(info->mti_env,
488 mdt_object_child(mp), ma);
489 mdt_handle_last_unlink(info, mp, ma);
492 GOTO(out_unlock_parent, rc);
495 /* step 2: find & lock the child */
496 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
497 rc = mdo_lookup(info->mti_env, mdt_object_child(mp),
498 lname, child_fid, &info->mti_spec);
500 GOTO(out_unlock_parent, rc);
502 /* We will lock the child regardless it is local or remote. No harm. */
503 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
505 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
506 child_lh = &info->mti_lh[MDT_LH_CHILD];
507 mdt_lock_reg_init(child_lh, LCK_EX);
508 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
511 mdt_object_put(info->mti_env, mc);
512 GOTO(out_unlock_parent, rc);
515 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
516 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
519 * Now we can only make sure we need MA_INODE, in mdd layer, will check
520 * whether need MA_LOV and MA_COOKIE.
522 ma->ma_need = MA_INODE;
524 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
525 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
526 mdt_object_child(mc), lname, ma);
528 mdt_handle_last_unlink(info, mc, ma);
531 mdt_object_unlock_put(info, mc, child_lh, rc);
533 mdt_object_unlock_put(info, mp, parent_lh, rc);
535 mdt_shrink_reply(info);
539 static int mdt_reint_link(struct mdt_thread_info *info,
540 struct mdt_lock_handle *lhc)
542 struct mdt_reint_record *rr = &info->mti_rr;
543 struct ptlrpc_request *req = mdt_info_req(info);
544 struct md_attr *ma = &info->mti_attr;
545 struct mdt_object *ms;
546 struct mdt_object *mp;
547 struct mdt_lock_handle *lhs;
548 struct mdt_lock_handle *lhp;
549 struct lu_name *lname;
553 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
554 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
556 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
557 GOTO(out, rc = err_serious(-ENOENT));
559 if (info->mti_cross_ref) {
560 /* MDT holding name ask us to add ref. */
561 lhs = &info->mti_lh[MDT_LH_CHILD];
562 mdt_lock_reg_init(lhs, LCK_EX);
563 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
564 MDS_INODELOCK_UPDATE);
566 GOTO(out, rc = PTR_ERR(ms));
568 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
569 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
570 mdt_object_unlock_put(info, ms, lhs, rc);
574 /* Invalid case so return error immediately instead of
576 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
577 GOTO(out, rc = -EPERM);
579 /* step 1: find & lock the target parent dir */
580 lhp = &info->mti_lh[MDT_LH_PARENT];
581 mdt_lock_pdo_init(lhp, LCK_EX, rr->rr_name,
583 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
584 MDS_INODELOCK_UPDATE);
586 GOTO(out, rc = PTR_ERR(mp));
588 /* step 2: find & lock the source */
589 lhs = &info->mti_lh[MDT_LH_CHILD];
590 mdt_lock_reg_init(lhs, LCK_EX);
592 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
594 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
596 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
599 mdt_object_put(info->mti_env, ms);
600 GOTO(out_unlock_parent, rc);
603 /* step 3: link it */
604 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
605 OBD_FAIL_MDS_REINT_LINK_WRITE);
607 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
608 rc = mdo_link(info->mti_env, mdt_object_child(mp),
609 mdt_object_child(ms), lname, ma);
612 mdt_object_unlock_put(info, ms, lhs, rc);
614 mdt_object_unlock_put(info, mp, lhp, rc);
619 /* partial operation for rename */
620 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
622 struct mdt_reint_record *rr = &info->mti_rr;
623 struct ptlrpc_request *req = mdt_info_req(info);
624 struct md_attr *ma = &info->mti_attr;
625 struct mdt_object *mtgtdir;
626 struct mdt_object *mtgt = NULL;
627 struct mdt_lock_handle *lh_tgtdir;
628 struct mdt_lock_handle *lh_tgt = NULL;
629 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
630 struct lu_name *lname;
634 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
635 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
637 /* step 1: lookup & lock the tgt dir. */
638 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
639 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
641 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
642 MDS_INODELOCK_UPDATE);
644 GOTO(out, rc = PTR_ERR(mtgtdir));
646 /* step 2: find & lock the target object if exists. */
647 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
648 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
649 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
650 lname, tgt_fid, &info->mti_spec);
651 if (rc != 0 && rc != -ENOENT) {
652 GOTO(out_unlock_tgtdir, rc);
653 } else if (rc == 0) {
655 * In case of replay that name can be already inserted, check
656 * that and do nothing if so.
658 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
659 GOTO(out_unlock_tgtdir, rc);
661 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
662 mdt_lock_reg_init(lh_tgt, LCK_EX);
664 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
665 MDS_INODELOCK_LOOKUP);
667 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
669 mdt_reint_init_ma(info, ma);
670 if (!ma->ma_lmm || !ma->ma_cookie)
671 GOTO(out_unlock_tgt, rc = -EINVAL);
673 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
674 mdt_object_child(mtgt), rr->rr_fid2,
676 } else /* -ENOENT */ {
677 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
678 lname, rr->rr_fid2, ma);
681 /* handle last link of tgt object */
683 mdt_handle_last_unlink(info, mtgt, ma);
688 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
690 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
692 mdt_shrink_reply(info);
696 static int mdt_rename_lock(struct mdt_thread_info *info,
697 struct lustre_handle *lh)
699 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
700 ldlm_policy_data_t *policy = &info->mti_policy;
701 struct ldlm_res_id *res_id = &info->mti_res_id;
707 * Disable global rename BFL lock temporarily because
708 * when a mds do rename recoverying, which might enqueue
709 * BFL lock to the controller mds. and this req might be
710 * replay req for controller mds. but we did not have
711 * such handling in controller mds. XXX
714 ls = info->mti_mdt->mdt_md_dev.md_lu_dev.ld_site;
715 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
717 memset(policy, 0, sizeof *policy);
718 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
720 if (ls->ls_control_exp == NULL) {
721 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
724 * Current node is controller, that is mdt0, where we should
727 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
728 LCK_EX, &flags, ldlm_blocking_ast,
729 ldlm_completion_ast, NULL, NULL, 0,
735 * This is the case mdt0 is remote node, issue DLM lock like
738 rc = ldlm_cli_enqueue(ls->ls_control_exp, NULL, res_id,
739 LDLM_IBITS, policy, LCK_EX, &flags,
740 ldlm_blocking_ast, ldlm_completion_ast,
741 NULL, NULL, NULL, 0, NULL, lh, 0);
747 static void mdt_rename_unlock(struct lustre_handle *lh)
750 /* Disable global rename BFL lock temporarily. see above XXX*/
753 LASSERT(lustre_handle_is_used(lh));
754 ldlm_lock_decref(lh, LCK_EX);
759 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
760 * target. Source should not be ancestor of target dir. May be other rename
761 * checks can be moved here later.
763 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
765 struct mdt_reint_record *rr = &info->mti_rr;
766 struct lu_fid dst_fid = *rr->rr_fid2;
767 struct mdt_object *dst;
772 LASSERT(fid_is_sane(&dst_fid));
773 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
775 rc = mdo_is_subdir(info->mti_env,
776 mdt_object_child(dst), fid,
778 mdt_object_put(info->mti_env, dst);
779 if (rc != -EREMOTE && rc < 0) {
780 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
782 /* check the found fid */
783 if (lu_fid_eq(&dst_fid, fid))
789 } while (rc == -EREMOTE);
794 static int mdt_reint_rename(struct mdt_thread_info *info,
795 struct mdt_lock_handle *lhc)
797 struct mdt_reint_record *rr = &info->mti_rr;
798 struct md_attr *ma = &info->mti_attr;
799 struct ptlrpc_request *req = mdt_info_req(info);
800 struct mdt_object *msrcdir;
801 struct mdt_object *mtgtdir;
802 struct mdt_object *mold;
803 struct mdt_object *mnew = NULL;
804 struct mdt_lock_handle *lh_srcdirp;
805 struct mdt_lock_handle *lh_tgtdirp;
806 struct mdt_lock_handle *lh_oldp;
807 struct mdt_lock_handle *lh_newp;
808 struct lu_fid *old_fid = &info->mti_tmp_fid1;
809 struct lu_fid *new_fid = &info->mti_tmp_fid2;
810 struct lustre_handle rename_lh = { 0 };
811 struct lu_name slname = { 0 };
812 struct lu_name *lname;
816 if (info->mti_cross_ref) {
817 rc = mdt_reint_rename_tgt(info);
821 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
822 PFID(rr->rr_fid1), rr->rr_name,
823 PFID(rr->rr_fid2), rr->rr_tgt);
825 rc = mdt_rename_lock(info, &rename_lh);
827 CERROR("Can't lock FS for rename, rc %d\n", rc);
831 lh_newp = &info->mti_lh[MDT_LH_NEW];
833 /* step 1: lock the source dir. */
834 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
835 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
837 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
838 MDS_INODELOCK_UPDATE);
840 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
842 /* step 2: find & lock the target dir. */
843 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
844 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
846 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
847 mdt_object_get(info->mti_env, msrcdir);
850 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
853 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
855 rc = mdt_object_exists(mtgtdir);
857 GOTO(out_unlock_target, rc = -ESTALE);
859 /* we lock the target dir if it is local */
860 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
861 MDS_INODELOCK_UPDATE,
864 GOTO(out_unlock_target, rc);
868 /* step 3: find & lock the old object. */
869 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
870 mdt_name_copy(&slname, lname);
871 rc = mdo_lookup(info->mti_env, mdt_object_child(msrcdir),
872 &slname, old_fid, &info->mti_spec);
874 GOTO(out_unlock_target, rc);
876 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
877 GOTO(out_unlock_target, rc = -EINVAL);
879 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
881 GOTO(out_unlock_target, rc = PTR_ERR(mold));
883 lh_oldp = &info->mti_lh[MDT_LH_OLD];
884 mdt_lock_reg_init(lh_oldp, LCK_EX);
885 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
888 mdt_object_put(info->mti_env, mold);
889 GOTO(out_unlock_target, rc);
891 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
893 /* step 4: find & lock the new object. */
894 /* new target object may not exist now */
895 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
896 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
897 lname, new_fid, &info->mti_spec);
899 /* the new_fid should have been filled at this moment */
900 if (lu_fid_eq(old_fid, new_fid))
901 GOTO(out_unlock_old, rc);
903 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
904 lu_fid_eq(new_fid, rr->rr_fid2))
905 GOTO(out_unlock_old, rc = -EINVAL);
907 mdt_lock_reg_init(lh_newp, LCK_EX);
908 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
910 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
912 rc = mdt_object_lock(info, mnew, lh_newp,
913 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
915 mdt_object_put(info->mti_env, mnew);
916 GOTO(out_unlock_old, rc);
918 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
919 } else if (rc != -EREMOTE && rc != -ENOENT)
920 GOTO(out_unlock_old, rc);
922 /* step 5: rename it */
923 mdt_reint_init_ma(info, ma);
924 if (!ma->ma_lmm || !ma->ma_cookie)
925 GOTO(out_unlock_new, rc = -EINVAL);
927 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
928 OBD_FAIL_MDS_REINT_RENAME_WRITE);
931 /* Check if @dst is subdir of @src. */
932 rc = mdt_rename_sanity(info, old_fid);
934 GOTO(out_unlock_new, rc);
936 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
937 mdt_object_child(mtgtdir), old_fid, &slname,
938 (mnew ? mdt_object_child(mnew) : NULL),
941 /* handle last link of tgt object */
943 mdt_handle_last_unlink(info, mnew, ma);
948 mdt_object_unlock_put(info, mnew, lh_newp, rc);
950 mdt_object_unlock_put(info, mold, lh_oldp, rc);
952 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
954 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
956 mdt_rename_unlock(&rename_lh);
958 mdt_shrink_reply(info);
962 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
963 struct mdt_lock_handle *lhc);
965 static mdt_reinter reinters[REINT_MAX] = {
966 [REINT_SETATTR] = mdt_reint_setattr,
967 [REINT_CREATE] = mdt_reint_create,
968 [REINT_LINK] = mdt_reint_link,
969 [REINT_UNLINK] = mdt_reint_unlink,
970 [REINT_RENAME] = mdt_reint_rename,
971 [REINT_OPEN] = mdt_reint_open
974 int mdt_reint_rec(struct mdt_thread_info *info,
975 struct mdt_lock_handle *lhc)
980 rc = reinters[info->mti_rr.rr_opcode](info, lhc);