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;
149 lname = mdt_name(info->mti_env, (char *)rr->rr_name,
151 rc = mdo_create(info->mti_env, next, lname,
152 mdt_object_child(child),
153 &info->mti_spec, ma);
155 /* Return fid & attr to client. */
156 if (ma->ma_valid & MA_INODE)
157 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
158 mdt_object_fid(child));
160 mdt_object_put(info->mti_env, child);
164 mdt_create_pack_capa(info, rc, child, repbody);
165 mdt_object_unlock_put(info, parent, lh, rc);
169 /* Partial request to create object only */
170 static int mdt_md_mkobj(struct mdt_thread_info *info)
172 struct mdt_device *mdt = info->mti_mdt;
173 struct mdt_object *o;
174 struct mdt_body *repbody;
175 struct md_attr *ma = &info->mti_attr;
179 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
180 PFID(info->mti_rr.rr_fid2));
182 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
184 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
186 struct md_object *next = mdt_object_child(o);
188 ma->ma_need = MA_INODE;
192 * Cross-ref create can encounter already created obj in case of
193 * recovery, just get attr in that case.
195 if (mdt_object_exists(o) == 1) {
196 rc = mo_attr_get(info->mti_env, next, ma);
199 * Here, NO permission check for object_create,
200 * such check has been done on the original MDS.
202 rc = mo_object_create(info->mti_env, next,
203 &info->mti_spec, ma);
206 /* Return fid & attr to client. */
207 if (ma->ma_valid & MA_INODE)
208 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
211 mdt_object_put(info->mti_env, o);
215 mdt_create_pack_capa(info, rc, o, repbody);
219 /* In the raw-setattr case, we lock the child inode.
220 * In the write-back case or if being called from open,
221 * the client holds a lock already.
222 * We use the ATTR_FROM_OPEN (translated into MRF_SETATTR_LOCKED by
223 * mdt_setattr_unpack()) flag to tell these cases apart. */
224 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo, int flags)
226 struct md_attr *ma = &info->mti_attr;
227 struct mdt_lock_handle *lh;
232 /* attr shouldn't be set on remote object */
233 LASSERT(mdt_object_exists(mo) >= 0);
236 som_update = (info->mti_epoch->flags & MF_SOM_CHANGE);
238 /* Try to avoid object_lock if another epoch has been started
240 if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
243 lh = &info->mti_lh[MDT_LH_PARENT];
244 mdt_lock_reg_init(lh, LCK_PW);
246 if (!(flags & MRF_SETATTR_LOCKED)) {
247 __u64 lockpart = MDS_INODELOCK_UPDATE;
248 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
249 lockpart |= MDS_INODELOCK_LOOKUP;
251 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
256 /* Setattrs are syncronized through dlm lock taken above. If another
257 * epoch started, its attributes may be already flushed on disk,
259 if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
260 GOTO(out_unlock, rc = 0);
262 if (mdt_object_exists(mo) == 0)
263 GOTO(out_unlock, rc = -ENOENT);
265 /* all attrs are packed into mti_attr in unpack_setattr */
266 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
267 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
269 /* This is only for set ctime when rename's source is on remote MDS. */
270 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
271 ma->ma_attr_flags |= MDS_VTX_BYPASS;
273 /* all attrs are packed into mti_attr in unpack_setattr */
274 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
276 GOTO(out_unlock, rc);
278 /* Re-enable SIZEONMDS. */
280 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
281 mo->mot_ioepoch, PFID(mdt_object_fid(mo)),
283 mdt_sizeonmds_enable(info, mo);
288 mdt_object_unlock(info, mo, lh, rc);
292 static int mdt_reint_setattr(struct mdt_thread_info *info,
293 struct mdt_lock_handle *lhc)
295 struct md_attr *ma = &info->mti_attr;
296 struct mdt_reint_record *rr = &info->mti_rr;
297 struct ptlrpc_request *req = mdt_info_req(info);
298 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
299 struct mdt_file_data *mfd;
300 struct mdt_object *mo;
301 struct md_object *next;
302 struct mdt_body *repbody;
306 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
307 (unsigned int)ma->ma_attr.la_valid);
309 if (info->mti_dlm_req)
310 ldlm_request_cancel(req, info->mti_dlm_req, 0);
312 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
313 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
315 GOTO(out, rc = PTR_ERR(mo));
317 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
318 if ((ma->ma_attr.la_valid & LA_SIZE) ||
319 (rr->rr_flags & MRF_SETATTR_LOCKED)) {
320 /* Check write access for the O_TRUNC case */
321 if (mdt_write_read(info->mti_mdt, mo) < 0)
322 GOTO(out_put, rc = -ETXTBSY);
324 } else if (info->mti_epoch &&
325 (info->mti_epoch->flags & MF_EPOCH_OPEN)) {
327 rc = mdt_write_get(info->mti_mdt, mo);
333 GOTO(out_put, rc = -ENOMEM);
335 mdt_epoch_open(info, mo);
336 repbody->ioepoch = mo->mot_ioepoch;
338 mdt_object_get(info->mti_env, mo);
339 mdt_mfd_set_mode(mfd, FMODE_EPOCHLCK);
340 mfd->mfd_object = mo;
341 mfd->mfd_xid = req->rq_xid;
343 spin_lock(&med->med_open_lock);
344 list_add(&mfd->mfd_list, &med->med_open_head);
345 spin_unlock(&med->med_open_lock);
346 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
349 if (info->mti_epoch && (info->mti_epoch->flags & MF_SOM_CHANGE))
350 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
352 rc = mdt_attr_set(info, mo, rr->rr_flags);
356 if (info->mti_epoch && (info->mti_epoch->flags & MF_SOM_CHANGE)) {
357 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
358 LASSERT(info->mti_epoch);
360 spin_lock(&med->med_open_lock);
361 /* Size-on-MDS Update. Find and free mfd. */
362 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
364 spin_unlock(&med->med_open_lock);
365 CDEBUG(D_INODE | D_ERROR, "no handle for file close: "
366 "fid = "DFID": cookie = "LPX64"\n",
367 PFID(info->mti_rr.rr_fid1),
368 info->mti_epoch->handle.cookie);
369 GOTO(out_put, rc = -ESTALE);
371 LASSERT(mfd->mfd_mode == FMODE_SOM);
372 LASSERT(!(info->mti_epoch->flags & MF_EPOCH_CLOSE));
374 class_handle_unhash(&mfd->mfd_handle);
375 list_del_init(&mfd->mfd_list);
376 spin_unlock(&med->med_open_lock);
378 mdt_mfd_close(info, mfd);
381 ma->ma_need = MA_INODE;
383 next = mdt_object_child(mo);
384 rc = mo_attr_get(info->mti_env, next, ma);
388 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
390 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
391 info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
392 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
393 (ma->ma_attr.la_valid & LA_SIZE)) {
394 struct lustre_capa *capa;
396 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
398 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
399 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
402 repbody->valid |= OBD_MD_FLOSSCAPA;
407 mdt_object_put(info->mti_env, mo);
409 mdt_shrink_reply(info);
413 static int mdt_reint_create(struct mdt_thread_info *info,
414 struct mdt_lock_handle *lhc)
419 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
420 RETURN(err_serious(-ESTALE));
422 if (info->mti_dlm_req)
423 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
425 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
427 /* Cross-ref case. */
428 if (info->mti_cross_ref) {
429 rc = mdt_md_mkobj(info);
439 /* Special file should stay on the same node as parent. */
440 LASSERT(info->mti_rr.rr_namelen > 0);
441 rc = mdt_md_create(info);
445 rc = err_serious(-EOPNOTSUPP);
450 static int mdt_reint_unlink(struct mdt_thread_info *info,
451 struct mdt_lock_handle *lhc)
453 struct mdt_reint_record *rr = &info->mti_rr;
454 struct ptlrpc_request *req = mdt_info_req(info);
455 struct md_attr *ma = &info->mti_attr;
456 struct lu_fid *child_fid = &info->mti_tmp_fid1;
457 struct mdt_object *mp;
458 struct mdt_object *mc;
459 struct mdt_lock_handle *parent_lh;
460 struct mdt_lock_handle *child_lh;
461 struct lu_name *lname;
465 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
468 if (info->mti_dlm_req)
469 ldlm_request_cancel(req, info->mti_dlm_req, 0);
471 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
472 RETURN(err_serious(-ENOENT));
475 * step 1: lock the parent. Note, this may be child in case of
476 * remote operation denoted by ->mti_cross_ref flag.
478 parent_lh = &info->mti_lh[MDT_LH_PARENT];
479 if (info->mti_cross_ref) {
481 * Init reg lock for cross ref case when we need to do only
484 mdt_lock_reg_init(parent_lh, LCK_PW);
486 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
489 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
490 MDS_INODELOCK_UPDATE);
493 /* errors are possible here in cross-ref cases, see below */
494 if (info->mti_cross_ref)
499 mdt_reint_init_ma(info, ma);
500 if (!ma->ma_lmm || !ma->ma_cookie)
501 GOTO(out_unlock_parent, rc = -EINVAL);
503 if (info->mti_cross_ref) {
505 * Remote partial operation. It is possible that replay may
506 * happen on parent MDT and this operation will be repeated.
507 * Therefore the object absense is allowed case and nothing
508 * should be done here.
510 if (mdt_object_exists(mp) > 0) {
511 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
512 rc = mo_ref_del(info->mti_env,
513 mdt_object_child(mp), ma);
515 mdt_handle_last_unlink(info, mp, ma);
518 GOTO(out_unlock_parent, rc);
521 /* step 2: find & lock the child */
522 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
523 rc = mdo_lookup(info->mti_env, mdt_object_child(mp),
524 lname, child_fid, &info->mti_spec);
526 GOTO(out_unlock_parent, rc);
528 /* We will lock the child regardless it is local or remote. No harm. */
529 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
531 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
532 child_lh = &info->mti_lh[MDT_LH_CHILD];
533 mdt_lock_reg_init(child_lh, LCK_EX);
534 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
537 mdt_object_put(info->mti_env, mc);
538 GOTO(out_unlock_parent, rc);
541 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
542 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
545 * Now we can only make sure we need MA_INODE, in mdd layer, will check
546 * whether need MA_LOV and MA_COOKIE.
548 ma->ma_need = MA_INODE;
550 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
551 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
552 mdt_object_child(mc), lname, ma);
554 mdt_handle_last_unlink(info, mc, ma);
557 mdt_object_unlock_put(info, mc, child_lh, rc);
559 mdt_object_unlock_put(info, mp, parent_lh, rc);
564 static int mdt_reint_link(struct mdt_thread_info *info,
565 struct mdt_lock_handle *lhc)
567 struct mdt_reint_record *rr = &info->mti_rr;
568 struct ptlrpc_request *req = mdt_info_req(info);
569 struct md_attr *ma = &info->mti_attr;
570 struct mdt_object *ms;
571 struct mdt_object *mp;
572 struct mdt_lock_handle *lhs;
573 struct mdt_lock_handle *lhp;
574 struct lu_name *lname;
578 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
579 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
581 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
582 RETURN(err_serious(-ENOENT));
584 if (info->mti_dlm_req)
585 ldlm_request_cancel(req, info->mti_dlm_req, 0);
587 if (info->mti_cross_ref) {
588 /* MDT holding name ask us to add ref. */
589 lhs = &info->mti_lh[MDT_LH_CHILD];
590 mdt_lock_reg_init(lhs, LCK_EX);
591 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
592 MDS_INODELOCK_UPDATE);
596 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
597 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
598 mdt_object_unlock_put(info, ms, lhs, rc);
602 /* Invalid case so return error immediately instead of
604 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
607 /* step 1: find & lock the target parent dir */
608 lhp = &info->mti_lh[MDT_LH_PARENT];
609 mdt_lock_pdo_init(lhp, LCK_EX, rr->rr_name,
611 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
612 MDS_INODELOCK_UPDATE);
616 /* step 2: find & lock the source */
617 lhs = &info->mti_lh[MDT_LH_CHILD];
618 mdt_lock_reg_init(lhs, LCK_EX);
620 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
622 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
624 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
627 mdt_object_put(info->mti_env, ms);
628 GOTO(out_unlock_parent, rc);
631 /* step 3: link it */
632 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
633 OBD_FAIL_MDS_REINT_LINK_WRITE);
635 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
636 rc = mdo_link(info->mti_env, mdt_object_child(mp),
637 mdt_object_child(ms), lname, ma);
640 mdt_object_unlock_put(info, ms, lhs, rc);
642 mdt_object_unlock_put(info, mp, lhp, rc);
646 /* partial operation for rename */
647 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
649 struct mdt_reint_record *rr = &info->mti_rr;
650 struct ptlrpc_request *req = mdt_info_req(info);
651 struct md_attr *ma = &info->mti_attr;
652 struct mdt_object *mtgtdir;
653 struct mdt_object *mtgt = NULL;
654 struct mdt_lock_handle *lh_tgtdir;
655 struct mdt_lock_handle *lh_tgt = NULL;
656 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
657 struct lu_name *lname;
661 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
662 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
664 /* step 1: lookup & lock the tgt dir. */
665 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
666 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
668 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
669 MDS_INODELOCK_UPDATE);
671 RETURN(PTR_ERR(mtgtdir));
673 /* step 2: find & lock the target object if exists. */
674 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
675 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
676 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
677 lname, tgt_fid, &info->mti_spec);
678 if (rc != 0 && rc != -ENOENT) {
679 GOTO(out_unlock_tgtdir, rc);
680 } else if (rc == 0) {
682 * In case of replay that name can be already inserted, check
683 * that and do nothing if so.
685 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
686 GOTO(out_unlock_tgtdir, rc);
688 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
689 mdt_lock_reg_init(lh_tgt, LCK_EX);
691 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
692 MDS_INODELOCK_LOOKUP);
694 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
696 mdt_reint_init_ma(info, ma);
697 if (!ma->ma_lmm || !ma->ma_cookie)
698 GOTO(out_unlock_tgt, rc = -EINVAL);
700 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
701 mdt_object_child(mtgt), rr->rr_fid2,
703 } else /* -ENOENT */ {
704 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
705 lname, rr->rr_fid2, ma);
708 /* handle last link of tgt object */
710 mdt_handle_last_unlink(info, mtgt, ma);
715 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
717 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
721 static int mdt_rename_lock(struct mdt_thread_info *info,
722 struct lustre_handle *lh)
724 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
725 ldlm_policy_data_t *policy = &info->mti_policy;
726 struct ldlm_res_id *res_id = &info->mti_res_id;
732 * Disable global rename BFL lock temporarily because
733 * when a mds do rename recoverying, which might enqueue
734 * BFL lock to the controller mds. and this req might be
735 * replay req for controller mds. but we did not have
736 * such handling in controller mds. XXX
739 ms = mdt_md_site(info->mti_mdt);
740 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
742 memset(policy, 0, sizeof *policy);
743 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
745 if (ms->ms_control_exp == NULL) {
746 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
749 * Current node is controller, that is mdt0, where we should
752 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
753 LCK_EX, &flags, ldlm_blocking_ast,
754 ldlm_completion_ast, NULL, NULL, 0,
756 &info->mti_exp->exp_handle.h_cookie,
759 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
760 ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
764 * This is the case mdt0 is remote node, issue DLM lock like
767 rc = ldlm_cli_enqueue(ms->ms_control_exp,
768 NULL, &einfo, res_id,
769 policy, &flags, NULL, 0, NULL, lh, 0);
775 static void mdt_rename_unlock(struct lustre_handle *lh)
778 /* Disable global rename BFL lock temporarily. see above XXX*/
781 LASSERT(lustre_handle_is_used(lh));
782 ldlm_lock_decref(lh, LCK_EX);
787 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
788 * target. Source should not be ancestor of target dir. May be other rename
789 * checks can be moved here later.
791 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
793 struct mdt_reint_record *rr = &info->mti_rr;
794 struct lu_fid dst_fid = *rr->rr_fid2;
795 struct mdt_object *dst;
800 LASSERT(fid_is_sane(&dst_fid));
801 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
803 rc = mdo_is_subdir(info->mti_env,
804 mdt_object_child(dst), fid,
806 mdt_object_put(info->mti_env, dst);
807 if (rc != -EREMOTE && rc < 0) {
808 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
810 /* check the found fid */
811 if (lu_fid_eq(&dst_fid, fid))
817 } while (rc == -EREMOTE);
822 static int mdt_reint_rename(struct mdt_thread_info *info,
823 struct mdt_lock_handle *lhc)
825 struct mdt_reint_record *rr = &info->mti_rr;
826 struct md_attr *ma = &info->mti_attr;
827 struct ptlrpc_request *req = mdt_info_req(info);
828 struct mdt_object *msrcdir;
829 struct mdt_object *mtgtdir;
830 struct mdt_object *mold;
831 struct mdt_object *mnew = NULL;
832 struct mdt_lock_handle *lh_srcdirp;
833 struct mdt_lock_handle *lh_tgtdirp;
834 struct mdt_lock_handle *lh_oldp;
835 struct mdt_lock_handle *lh_newp;
836 struct lu_fid *old_fid = &info->mti_tmp_fid1;
837 struct lu_fid *new_fid = &info->mti_tmp_fid2;
838 struct lustre_handle rename_lh = { 0 };
839 struct lu_name slname = { 0 };
840 struct lu_name *lname;
844 if (info->mti_dlm_req)
845 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
847 if (info->mti_cross_ref) {
848 rc = mdt_reint_rename_tgt(info);
852 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
853 PFID(rr->rr_fid1), rr->rr_name,
854 PFID(rr->rr_fid2), rr->rr_tgt);
856 rc = mdt_rename_lock(info, &rename_lh);
858 CERROR("Can't lock FS for rename, rc %d\n", rc);
862 lh_newp = &info->mti_lh[MDT_LH_NEW];
864 /* step 1: lock the source dir. */
865 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
866 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
868 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
869 MDS_INODELOCK_UPDATE);
871 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
873 /* step 2: find & lock the target dir. */
874 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
875 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
877 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
878 mdt_object_get(info->mti_env, msrcdir);
881 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
884 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
886 rc = mdt_object_exists(mtgtdir);
888 GOTO(out_unlock_target, rc = -ESTALE);
890 /* we lock the target dir if it is local */
891 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
892 MDS_INODELOCK_UPDATE,
895 GOTO(out_unlock_target, rc);
899 /* step 3: find & lock the old object. */
900 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
901 mdt_name_copy(&slname, lname);
902 rc = mdo_lookup(info->mti_env, mdt_object_child(msrcdir),
903 &slname, old_fid, &info->mti_spec);
905 GOTO(out_unlock_target, rc);
907 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
908 GOTO(out_unlock_target, rc = -EINVAL);
910 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
912 GOTO(out_unlock_target, rc = PTR_ERR(mold));
914 lh_oldp = &info->mti_lh[MDT_LH_OLD];
915 mdt_lock_reg_init(lh_oldp, LCK_EX);
916 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
919 mdt_object_put(info->mti_env, mold);
920 GOTO(out_unlock_target, rc);
922 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
924 /* step 4: find & lock the new object. */
925 /* new target object may not exist now */
926 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
927 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
928 lname, new_fid, &info->mti_spec);
930 /* the new_fid should have been filled at this moment */
931 if (lu_fid_eq(old_fid, new_fid))
932 GOTO(out_unlock_old, rc);
934 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
935 lu_fid_eq(new_fid, rr->rr_fid2))
936 GOTO(out_unlock_old, rc = -EINVAL);
938 mdt_lock_reg_init(lh_newp, LCK_EX);
939 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
941 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
943 rc = mdt_object_lock(info, mnew, lh_newp,
944 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
946 mdt_object_put(info->mti_env, mnew);
947 GOTO(out_unlock_old, rc);
949 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
950 } else if (rc != -EREMOTE && rc != -ENOENT)
951 GOTO(out_unlock_old, rc);
953 /* step 5: rename it */
954 mdt_reint_init_ma(info, ma);
955 if (!ma->ma_lmm || !ma->ma_cookie)
956 GOTO(out_unlock_new, rc = -EINVAL);
958 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
959 OBD_FAIL_MDS_REINT_RENAME_WRITE);
962 /* Check if @dst is subdir of @src. */
963 rc = mdt_rename_sanity(info, old_fid);
965 GOTO(out_unlock_new, rc);
967 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
968 mdt_object_child(mtgtdir), old_fid, &slname,
969 (mnew ? mdt_object_child(mnew) : NULL),
972 /* handle last link of tgt object */
974 mdt_handle_last_unlink(info, mnew, ma);
979 mdt_object_unlock_put(info, mnew, lh_newp, rc);
981 mdt_object_unlock_put(info, mold, lh_oldp, rc);
983 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
985 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
987 mdt_rename_unlock(&rename_lh);
991 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
992 struct mdt_lock_handle *lhc);
994 static mdt_reinter reinters[REINT_MAX] = {
995 [REINT_SETATTR] = mdt_reint_setattr,
996 [REINT_CREATE] = mdt_reint_create,
997 [REINT_LINK] = mdt_reint_link,
998 [REINT_UNLINK] = mdt_reint_unlink,
999 [REINT_RENAME] = mdt_reint_rename,
1000 [REINT_OPEN] = mdt_reint_open,
1001 [REINT_SETXATTR] = mdt_reint_setxattr
1004 int mdt_reint_rec(struct mdt_thread_info *info,
1005 struct mdt_lock_handle *lhc)
1010 rc = reinters[info->mti_rr.rr_opcode](info, lhc);