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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/mdt/mdt_reint.c
40 * Lustre Metadata Target (mdt) reintegration routines
42 * Author: Peter Braam <braam@clusterfs.com>
43 * Author: Andreas Dilger <adilger@clusterfs.com>
44 * Author: Phil Schwan <phil@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
50 # define EXPORT_SYMTAB
52 #define DEBUG_SUBSYSTEM S_MDS
54 #include "mdt_internal.h"
57 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
60 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
61 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
62 &RMF_MDT_MD, RCL_SERVER);
64 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
66 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
70 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
74 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
75 struct mdt_object *object,
76 struct mdt_body *repbody)
80 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
81 * we won't go to below*/
82 if (repbody->valid & OBD_MD_FLMDSCAPA)
85 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
86 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
87 struct lustre_capa *capa;
89 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
91 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
92 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
95 repbody->valid |= OBD_MD_FLMDSCAPA;
102 * Get version of object by fid.
104 * Return real version or ENOENT_VERSION if object doesn't exist
106 static void mdt_obj_version_get(struct mdt_thread_info *info,
107 struct mdt_object *o, __u64 *version)
110 LASSERT(mdt_object_exists(o) >= 0);
111 if (mdt_object_exists(o) > 0)
112 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
114 *version = ENOENT_VERSION;
115 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
116 PFID(mdt_object_fid(o)), *version);
120 * Check version is correct.
122 * Should be called only during replay.
124 static int mdt_version_check(struct ptlrpc_request *req,
125 __u64 version, int idx)
127 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
130 if (!exp_connect_vbr(req->rq_export))
133 LASSERT(req_is_replay(req));
134 /** VBR: version is checked always because costs nothing */
135 LASSERT(idx < PTLRPC_NUM_VERSIONS);
136 /** Sanity check for malformed buffers */
137 if (pre_ver == NULL) {
138 CERROR("No versions in request buffer\n");
139 cfs_spin_lock(&req->rq_export->exp_lock);
140 req->rq_export->exp_vbr_failed = 1;
141 cfs_spin_unlock(&req->rq_export->exp_lock);
143 } else if (pre_ver[idx] != version) {
144 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
145 pre_ver[idx], version);
146 cfs_spin_lock(&req->rq_export->exp_lock);
147 req->rq_export->exp_vbr_failed = 1;
148 cfs_spin_unlock(&req->rq_export->exp_lock);
155 * Save pre-versions in reply.
157 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
162 if (!exp_connect_vbr(req->rq_export))
165 LASSERT(!req_is_replay(req));
166 LASSERT(req->rq_repmsg != NULL);
167 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
169 reply_ver[idx] = version;
173 * Save enoent version, it is needed when it is obvious that object doesn't
174 * exist, e.g. child during create.
176 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
178 /* save version of file name for replay, it must be ENOENT here */
179 if (!req_is_replay(mdt_info_req(info))) {
180 info->mti_ver[idx] = ENOENT_VERSION;
181 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
186 * Get version from disk and save in reply buffer.
188 * Versions are saved in reply only during normal operations not replays.
190 void mdt_version_get_save(struct mdt_thread_info *info,
191 struct mdt_object *mto, int idx)
193 /* don't save versions during replay */
194 if (!req_is_replay(mdt_info_req(info))) {
195 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
196 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
201 * Get version from disk and check it, no save in reply.
203 int mdt_version_get_check(struct mdt_thread_info *info,
204 struct mdt_object *mto, int idx)
206 /* only check versions during replay */
207 if (!req_is_replay(mdt_info_req(info)))
210 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
211 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
215 * Get version from disk and check if recovery or just save.
217 int mdt_version_get_check_save(struct mdt_thread_info *info,
218 struct mdt_object *mto, int idx)
222 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
223 if (req_is_replay(mdt_info_req(info)))
224 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
227 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
232 * Lookup with version checking.
234 * This checks version of 'name'. Many reint functions uses 'name' for child not
235 * FID, therefore we need to get object by name and check its version.
237 int mdt_lookup_version_check(struct mdt_thread_info *info,
238 struct mdt_object *p, struct lu_name *lname,
239 struct lu_fid *fid, int idx)
243 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
245 /* Check version only during replay */
246 if (!req_is_replay(mdt_info_req(info)))
249 info->mti_ver[idx] = ENOENT_VERSION;
251 struct mdt_object *child;
252 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
253 if (likely(!IS_ERR(child))) {
254 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
255 mdt_object_put(info->mti_env, child);
258 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
259 return vbrc ? vbrc : rc;
264 * VBR: we save three versions in reply:
265 * 0 - parent. Check that parent version is the same during replay.
266 * 1 - name. Version of 'name' if file exists with the same name or
267 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
268 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
271 static int mdt_md_create(struct mdt_thread_info *info)
273 struct mdt_device *mdt = info->mti_mdt;
274 struct mdt_object *parent;
275 struct mdt_object *child;
276 struct mdt_lock_handle *lh;
277 struct mdt_body *repbody;
278 struct md_attr *ma = &info->mti_attr;
279 struct mdt_reint_record *rr = &info->mti_rr;
280 struct lu_name *lname;
284 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
285 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
287 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
289 lh = &info->mti_lh[MDT_LH_PARENT];
290 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
292 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
293 MDS_INODELOCK_UPDATE);
295 RETURN(PTR_ERR(parent));
297 if (mdt_object_obf(parent))
298 GOTO(out_put_parent, rc = -EPERM);
300 rc = mdt_version_get_check_save(info, parent, 0);
302 GOTO(out_put_parent, rc);
305 * Check child name version during replay.
306 * During create replay a file may exist with same name.
308 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
309 rc = mdt_lookup_version_check(info, parent, lname,
310 &info->mti_tmp_fid1, 1);
311 /* -ENOENT is expected here */
312 if (rc != 0 && rc != -ENOENT)
313 GOTO(out_put_parent, rc);
315 /* save version of file name for replay, it must be ENOENT here */
316 mdt_enoent_version_save(info, 1);
318 child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
319 if (likely(!IS_ERR(child))) {
320 struct md_object *next = mdt_object_child(parent);
322 ma->ma_need = MA_INODE;
324 /* capa for cross-ref will be stored here */
325 ma->ma_capa = req_capsule_server_get(info->mti_pill,
327 LASSERT(ma->ma_capa);
329 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
330 OBD_FAIL_MDS_REINT_CREATE_WRITE);
332 /* Version of child will be updated on disk. */
333 info->mti_mos = child;
334 rc = mdt_version_get_check_save(info, child, 2);
336 GOTO(out_put_child, rc);
338 /* Let lower layer know current lock mode. */
339 info->mti_spec.sp_cr_mode =
340 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
343 * Do perform lookup sanity check. We do not know if name exists
346 info->mti_spec.sp_cr_lookup = 1;
347 info->mti_spec.sp_feat = &dt_directory_features;
349 rc = mdo_create(info->mti_env, next, lname,
350 mdt_object_child(child),
351 &info->mti_spec, ma);
353 /* Return fid & attr to client. */
354 if (ma->ma_valid & MA_INODE)
355 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
356 mdt_object_fid(child));
359 mdt_object_put(info->mti_env, child);
363 mdt_create_pack_capa(info, rc, child, repbody);
365 mdt_object_unlock_put(info, parent, lh, rc);
369 /* Partial request to create object only */
370 static int mdt_md_mkobj(struct mdt_thread_info *info)
372 struct mdt_device *mdt = info->mti_mdt;
373 struct mdt_object *o;
374 struct mdt_body *repbody;
375 struct md_attr *ma = &info->mti_attr;
379 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
380 PFID(info->mti_rr.rr_fid2));
382 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
384 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
386 struct md_object *next = mdt_object_child(o);
388 ma->ma_need = MA_INODE;
392 * Cross-ref create can encounter already created obj in case of
393 * recovery, just get attr in that case.
395 if (mdt_object_exists(o) == 1) {
396 rc = mo_attr_get(info->mti_env, next, ma);
399 * Here, NO permission check for object_create,
400 * such check has been done on the original MDS.
402 rc = mo_object_create(info->mti_env, next,
403 &info->mti_spec, ma);
406 /* Return fid & attr to client. */
407 if (ma->ma_valid & MA_INODE)
408 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
411 mdt_object_put(info->mti_env, o);
415 mdt_create_pack_capa(info, rc, o, repbody);
419 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
420 struct md_attr *ma, int flags)
422 struct mdt_lock_handle *lh;
423 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
424 __u64 lockpart = MDS_INODELOCK_UPDATE;
428 /* attr shouldn't be set on remote object */
429 LASSERT(mdt_object_exists(mo) >= 0);
431 lh = &info->mti_lh[MDT_LH_PARENT];
432 mdt_lock_reg_init(lh, LCK_PW);
434 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
435 lockpart |= MDS_INODELOCK_LOOKUP;
437 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
441 if (mdt_object_exists(mo) == 0)
442 GOTO(out_unlock, rc = -ENOENT);
444 /* all attrs are packed into mti_attr in unpack_setattr */
445 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
446 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
448 /* This is only for set ctime when rename's source is on remote MDS. */
449 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
450 ma->ma_attr_flags |= MDS_VTX_BYPASS;
452 /* VBR: update version if attr changed are important for recovery */
454 /* update on-disk version of changed object */
456 rc = mdt_version_get_check_save(info, mo, 0);
458 GOTO(out_unlock, rc);
461 /* all attrs are packed into mti_attr in unpack_setattr */
462 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
464 GOTO(out_unlock, rc);
468 mdt_object_unlock(info, mo, lh, rc);
472 static int mdt_reint_setattr(struct mdt_thread_info *info,
473 struct mdt_lock_handle *lhc)
475 struct md_attr *ma = &info->mti_attr;
476 struct mdt_reint_record *rr = &info->mti_rr;
477 struct ptlrpc_request *req = mdt_info_req(info);
478 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
479 struct mdt_file_data *mfd;
480 struct mdt_object *mo;
481 struct md_object *next;
482 struct mdt_body *repbody;
486 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
487 (unsigned int)ma->ma_attr.la_valid);
489 if (info->mti_dlm_req)
490 ldlm_request_cancel(req, info->mti_dlm_req, 0);
492 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
493 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
495 GOTO(out, rc = PTR_ERR(mo));
497 /* start a log jounal handle if needed */
498 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
499 if ((ma->ma_attr.la_valid & LA_SIZE) ||
500 (rr->rr_flags & MRF_OPEN_TRUNC)) {
501 /* Check write access for the O_TRUNC case */
502 if (mdt_write_read(mo) < 0)
503 GOTO(out_put, rc = -ETXTBSY);
505 } else if (info->mti_ioepoch &&
506 (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
507 /* Truncate case. IOEpoch is opened. */
508 rc = mdt_write_get(mo);
515 GOTO(out_put, rc = -ENOMEM);
518 mdt_ioepoch_open(info, mo, 0);
519 repbody->ioepoch = mo->mot_ioepoch;
521 mdt_object_get(info->mti_env, mo);
522 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
523 mfd->mfd_object = mo;
524 mfd->mfd_xid = req->rq_xid;
526 cfs_spin_lock(&med->med_open_lock);
527 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
528 cfs_spin_unlock(&med->med_open_lock);
529 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
532 som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
534 /* SOM Attribute update case. Find the proper mfd and update
535 * SOM attributes on the proper object. */
536 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
537 LASSERT(info->mti_ioepoch);
539 cfs_spin_lock(&med->med_open_lock);
540 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
542 cfs_spin_unlock(&med->med_open_lock);
543 CDEBUG(D_INODE, "no handle for file close: "
544 "fid = "DFID": cookie = "LPX64"\n",
545 PFID(info->mti_rr.rr_fid1),
546 info->mti_ioepoch->handle.cookie);
547 GOTO(out_put, rc = -ESTALE);
549 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
550 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
552 class_handle_unhash(&mfd->mfd_handle);
553 cfs_list_del_init(&mfd->mfd_list);
554 cfs_spin_unlock(&med->med_open_lock);
556 /* Close the found mfd, update attributes. */
557 ma->ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
558 OBD_ALLOC_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
559 if (ma->ma_lmm == NULL)
560 GOTO(out_put, rc = -ENOMEM);
562 mdt_mfd_close(info, mfd);
564 OBD_FREE_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
566 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
571 ma->ma_need = MA_INODE;
573 next = mdt_object_child(mo);
574 rc = mo_attr_get(info->mti_env, next, ma);
578 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
580 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
581 info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
582 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
583 (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
584 struct lustre_capa *capa;
586 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
588 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
589 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
592 repbody->valid |= OBD_MD_FLOSSCAPA;
597 mdt_object_put(info->mti_env, mo);
600 mdt_counter_incr(req->rq_export, LPROC_MDT_SETATTR);
602 mdt_client_compatibility(info);
603 rc2 = mdt_fix_reply(info);
609 static int mdt_reint_create(struct mdt_thread_info *info,
610 struct mdt_lock_handle *lhc)
612 struct ptlrpc_request *req = mdt_info_req(info);
616 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
617 RETURN(err_serious(-ESTALE));
619 if (info->mti_dlm_req)
620 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
622 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
624 /* Cross-ref case. */
625 /* TODO: we can add LPROC_MDT_CROSS for cross-ref stats */
626 if (info->mti_cross_ref) {
627 rc = mdt_md_mkobj(info);
629 LASSERT(info->mti_rr.rr_namelen > 0);
630 mdt_counter_incr(req->rq_export, LPROC_MDT_MKDIR);
631 rc = mdt_md_create(info);
641 /* Special file should stay on the same node as parent. */
642 LASSERT(info->mti_rr.rr_namelen > 0);
643 mdt_counter_incr(req->rq_export, LPROC_MDT_MKNOD);
644 rc = mdt_md_create(info);
648 rc = err_serious(-EOPNOTSUPP);
654 * VBR: save parent version in reply and child version getting by its name.
655 * Version of child is getting and checking during its lookup. If
657 static int mdt_reint_unlink(struct mdt_thread_info *info,
658 struct mdt_lock_handle *lhc)
660 struct mdt_reint_record *rr = &info->mti_rr;
661 struct ptlrpc_request *req = mdt_info_req(info);
662 struct md_attr *ma = &info->mti_attr;
663 struct lu_fid *child_fid = &info->mti_tmp_fid1;
664 struct mdt_object *mp;
665 struct mdt_object *mc;
666 struct mdt_lock_handle *parent_lh;
667 struct mdt_lock_handle *child_lh;
668 struct lu_name *lname;
672 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
675 if (info->mti_dlm_req)
676 ldlm_request_cancel(req, info->mti_dlm_req, 0);
678 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
679 RETURN(err_serious(-ENOENT));
682 * step 1: lock the parent. Note, this may be child in case of
683 * remote operation denoted by ->mti_cross_ref flag.
685 parent_lh = &info->mti_lh[MDT_LH_PARENT];
686 if (info->mti_cross_ref) {
688 * Init reg lock for cross ref case when we need to do only
691 mdt_lock_reg_init(parent_lh, LCK_PW);
693 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
696 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
697 MDS_INODELOCK_UPDATE);
700 /* errors are possible here in cross-ref cases, see below */
701 if (info->mti_cross_ref)
706 if (mdt_object_obf(mp))
707 GOTO(out_unlock_parent, rc = -EPERM);
709 rc = mdt_version_get_check_save(info, mp, 0);
711 GOTO(out_unlock_parent, rc);
713 mdt_reint_init_ma(info, ma);
714 if (!ma->ma_lmm || !ma->ma_cookie)
715 GOTO(out_unlock_parent, rc = -EINVAL);
717 if (info->mti_cross_ref) {
719 * Remote partial operation. It is possible that replay may
720 * happen on parent MDT and this operation will be repeated.
721 * Therefore the object absense is allowed case and nothing
722 * should be done here.
724 if (mdt_object_exists(mp) > 0) {
725 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
726 rc = mo_ref_del(info->mti_env,
727 mdt_object_child(mp), ma);
729 mdt_handle_last_unlink(info, mp, ma);
732 GOTO(out_unlock_parent, rc);
735 /* step 2: find & lock the child */
736 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
737 /* lookup child object along with version checking */
739 rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
741 GOTO(out_unlock_parent, rc);
743 /* We will lock the child regardless it is local or remote. No harm. */
744 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
746 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
747 child_lh = &info->mti_lh[MDT_LH_CHILD];
748 mdt_lock_reg_init(child_lh, LCK_EX);
749 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
752 mdt_object_put(info->mti_env, mc);
753 GOTO(out_unlock_parent, rc);
756 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
757 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
758 /* save version when object is locked */
759 mdt_version_get_save(info, mc, 1);
761 * Now we can only make sure we need MA_INODE, in mdd layer, will check
762 * whether need MA_LOV and MA_COOKIE.
764 ma->ma_need = MA_INODE;
766 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
767 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
768 mdt_object_child(mc), lname, ma);
770 mdt_handle_last_unlink(info, mc, ma);
772 if (ma->ma_valid & MA_INODE) {
773 switch (ma->ma_attr.la_mode & S_IFMT) {
775 mdt_counter_incr(req->rq_export, LPROC_MDT_RMDIR);
783 mdt_counter_incr(req->rq_export, LPROC_MDT_UNLINK);
786 LASSERTF(0, "bad file type %o unlinking\n",
787 ma->ma_attr.la_mode);
793 mdt_object_unlock_put(info, mc, child_lh, rc);
795 mdt_object_unlock_put(info, mp, parent_lh, rc);
801 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
804 static int mdt_reint_link(struct mdt_thread_info *info,
805 struct mdt_lock_handle *lhc)
807 struct mdt_reint_record *rr = &info->mti_rr;
808 struct ptlrpc_request *req = mdt_info_req(info);
809 struct md_attr *ma = &info->mti_attr;
810 struct mdt_object *ms;
811 struct mdt_object *mp;
812 struct mdt_lock_handle *lhs;
813 struct mdt_lock_handle *lhp;
814 struct lu_name *lname;
818 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
819 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
821 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
822 RETURN(err_serious(-ENOENT));
824 if (info->mti_dlm_req)
825 ldlm_request_cancel(req, info->mti_dlm_req, 0);
827 if (info->mti_cross_ref) {
828 /* MDT holding name ask us to add ref. */
829 lhs = &info->mti_lh[MDT_LH_CHILD];
830 mdt_lock_reg_init(lhs, LCK_EX);
831 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
832 MDS_INODELOCK_UPDATE);
836 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
837 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
838 mdt_object_unlock_put(info, ms, lhs, rc);
842 /* Invalid case so return error immediately instead of
844 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
847 /* step 1: find & lock the target parent dir */
848 lhp = &info->mti_lh[MDT_LH_PARENT];
849 mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
851 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
852 MDS_INODELOCK_UPDATE);
856 if (mdt_object_obf(mp))
857 GOTO(out_unlock_parent, rc = -EPERM);
859 rc = mdt_version_get_check_save(info, mp, 0);
861 GOTO(out_unlock_parent, rc);
863 /* step 2: find & lock the source */
864 lhs = &info->mti_lh[MDT_LH_CHILD];
865 mdt_lock_reg_init(lhs, LCK_EX);
867 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
869 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
871 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
874 mdt_object_put(info->mti_env, ms);
875 GOTO(out_unlock_parent, rc);
878 /* step 3: link it */
879 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
880 OBD_FAIL_MDS_REINT_LINK_WRITE);
883 rc = mdt_version_get_check_save(info, ms, 1);
885 GOTO(out_unlock_child, rc);
887 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
888 /** check target version by name during replay */
889 rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
890 if (rc != 0 && rc != -ENOENT)
891 GOTO(out_unlock_child, rc);
892 /* save version of file name for replay, it must be ENOENT here */
893 if (!req_is_replay(mdt_info_req(info))) {
894 info->mti_ver[2] = ENOENT_VERSION;
895 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
898 rc = mdo_link(info->mti_env, mdt_object_child(mp),
899 mdt_object_child(ms), lname, ma);
902 mdt_counter_incr(req->rq_export, LPROC_MDT_LINK);
906 mdt_object_unlock_put(info, ms, lhs, rc);
908 mdt_object_unlock_put(info, mp, lhp, rc);
912 * lock the part of the directory according to the hash of the name
913 * (lh->mlh_pdo_hash) in parallel directory lock.
915 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
916 struct mdt_lock_handle *lh,
917 struct mdt_object *obj, __u64 ibits)
919 struct ldlm_res_id *res_id = &info->mti_res_id;
920 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
921 ldlm_policy_data_t *policy = &info->mti_policy;
925 * Finish res_id initializing by name hash marking part of
926 * directory which is taking modification.
928 LASSERT(lh->mlh_pdo_hash != 0);
929 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
930 memset(policy, 0, sizeof(*policy));
931 policy->l_inodebits.bits = ibits;
933 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
934 * going to be sent to client. If it is - mdt_intent_policy() path will
935 * fix it up and turn FL_LOCAL flag off.
937 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
938 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
939 &info->mti_exp->exp_handle.h_cookie);
943 /* partial operation for rename */
944 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
946 struct mdt_reint_record *rr = &info->mti_rr;
947 struct ptlrpc_request *req = mdt_info_req(info);
948 struct md_attr *ma = &info->mti_attr;
949 struct mdt_object *mtgtdir;
950 struct mdt_object *mtgt = NULL;
951 struct mdt_lock_handle *lh_tgtdir;
952 struct mdt_lock_handle *lh_tgt = NULL;
953 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
954 struct lu_name *lname;
958 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
959 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
961 /* step 1: lookup & lock the tgt dir. */
962 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
963 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
965 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
966 MDS_INODELOCK_UPDATE);
968 RETURN(PTR_ERR(mtgtdir));
970 /* step 2: find & lock the target object if exists. */
971 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
972 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
973 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
974 lname, tgt_fid, &info->mti_spec);
975 if (rc != 0 && rc != -ENOENT) {
976 GOTO(out_unlock_tgtdir, rc);
977 } else if (rc == 0) {
979 * In case of replay that name can be already inserted, check
980 * that and do nothing if so.
982 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
983 GOTO(out_unlock_tgtdir, rc);
985 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
986 mdt_lock_reg_init(lh_tgt, LCK_EX);
988 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
989 MDS_INODELOCK_LOOKUP);
991 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
993 mdt_reint_init_ma(info, ma);
994 if (!ma->ma_lmm || !ma->ma_cookie)
995 GOTO(out_unlock_tgt, rc = -EINVAL);
997 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
998 mdt_object_child(mtgt), rr->rr_fid2,
1000 } else /* -ENOENT */ {
1001 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
1002 lname, rr->rr_fid2, ma);
1005 /* handle last link of tgt object */
1006 if (rc == 0 && mtgt)
1007 mdt_handle_last_unlink(info, mtgt, ma);
1012 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
1014 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
1018 static int mdt_rename_lock(struct mdt_thread_info *info,
1019 struct lustre_handle *lh)
1021 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1022 ldlm_policy_data_t *policy = &info->mti_policy;
1023 struct ldlm_res_id *res_id = &info->mti_res_id;
1028 ms = mdt_md_site(info->mti_mdt);
1029 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1031 memset(policy, 0, sizeof *policy);
1032 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1034 if (ms->ms_control_exp == NULL) {
1035 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1038 * Current node is controller, that is mdt0, where we should
1041 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1042 LCK_EX, &flags, ldlm_blocking_ast,
1043 ldlm_completion_ast, NULL, NULL, 0,
1044 &info->mti_exp->exp_handle.h_cookie,
1047 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
1048 ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1052 * This is the case mdt0 is remote node, issue DLM lock like
1055 rc = ldlm_cli_enqueue(ms->ms_control_exp, NULL, &einfo, res_id,
1056 policy, &flags, NULL, 0, lh, 0);
1062 static void mdt_rename_unlock(struct lustre_handle *lh)
1065 LASSERT(lustre_handle_is_used(lh));
1066 ldlm_lock_decref(lh, LCK_EX);
1071 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1072 * target. Source should not be ancestor of target dir. May be other rename
1073 * checks can be moved here later.
1075 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1077 struct mdt_reint_record *rr = &info->mti_rr;
1078 struct lu_fid dst_fid = *rr->rr_fid2;
1079 struct mdt_object *dst;
1084 LASSERT(fid_is_sane(&dst_fid));
1085 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1087 rc = mdo_is_subdir(info->mti_env,
1088 mdt_object_child(dst), fid,
1090 mdt_object_put(info->mti_env, dst);
1091 if (rc != -EREMOTE && rc < 0) {
1092 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1094 /* check the found fid */
1095 if (lu_fid_eq(&dst_fid, fid))
1101 } while (rc == -EREMOTE);
1107 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1108 * 2 - src child; 3 - tgt child.
1109 * Update on disk version of src child.
1111 static int mdt_reint_rename(struct mdt_thread_info *info,
1112 struct mdt_lock_handle *lhc)
1114 struct mdt_reint_record *rr = &info->mti_rr;
1115 struct md_attr *ma = &info->mti_attr;
1116 struct ptlrpc_request *req = mdt_info_req(info);
1117 struct mdt_object *msrcdir;
1118 struct mdt_object *mtgtdir;
1119 struct mdt_object *mold;
1120 struct mdt_object *mnew = NULL;
1121 struct mdt_lock_handle *lh_srcdirp;
1122 struct mdt_lock_handle *lh_tgtdirp;
1123 struct mdt_lock_handle *lh_oldp;
1124 struct mdt_lock_handle *lh_newp;
1125 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1126 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1127 struct lustre_handle rename_lh = { 0 };
1128 struct lu_name slname = { 0 };
1129 struct lu_name *lname;
1133 if (info->mti_dlm_req)
1134 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1136 if (info->mti_cross_ref) {
1137 rc = mdt_reint_rename_tgt(info);
1141 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1142 PFID(rr->rr_fid1), rr->rr_name,
1143 PFID(rr->rr_fid2), rr->rr_tgt);
1145 rc = mdt_rename_lock(info, &rename_lh);
1147 CERROR("Can't lock FS for rename, rc %d\n", rc);
1151 lh_newp = &info->mti_lh[MDT_LH_NEW];
1153 /* step 1: lock the source dir. */
1154 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1155 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1157 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1158 MDS_INODELOCK_UPDATE);
1159 if (IS_ERR(msrcdir))
1160 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1162 if (mdt_object_obf(msrcdir))
1163 GOTO(out_unlock_source, rc = -EPERM);
1165 rc = mdt_version_get_check_save(info, msrcdir, 0);
1167 GOTO(out_unlock_source, rc);
1169 /* step 2: find & lock the target dir. */
1170 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1171 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1173 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1174 mdt_object_get(info->mti_env, msrcdir);
1176 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1177 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1178 MDS_INODELOCK_UPDATE);
1180 GOTO(out_unlock_source, rc);
1181 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1184 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1186 if (IS_ERR(mtgtdir))
1187 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1189 if (mdt_object_obf(mtgtdir))
1190 GOTO(out_put_target, rc = -EPERM);
1192 /* check early, the real version will be saved after locking */
1193 rc = mdt_version_get_check(info, mtgtdir, 1);
1195 GOTO(out_put_target, rc);
1197 rc = mdt_object_exists(mtgtdir);
1199 GOTO(out_put_target, rc = -ESTALE);
1200 } else if (rc > 0) {
1201 /* we lock the target dir if it is local */
1202 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1203 MDS_INODELOCK_UPDATE,
1206 GOTO(out_put_target, rc);
1207 /* get and save correct version after locking */
1208 mdt_version_get_save(info, mtgtdir, 1);
1212 /* step 3: find & lock the old object. */
1213 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1214 mdt_name_copy(&slname, lname);
1216 rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1218 GOTO(out_unlock_target, rc);
1220 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1221 GOTO(out_unlock_target, rc = -EINVAL);
1223 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1225 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1227 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1228 mdt_lock_reg_init(lh_oldp, LCK_EX);
1229 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1232 mdt_object_put(info->mti_env, mold);
1233 GOTO(out_unlock_target, rc);
1236 info->mti_mos = mold;
1237 /* save version after locking */
1238 mdt_version_get_save(info, mold, 2);
1239 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1241 /* step 4: find & lock the new object. */
1242 /* new target object may not exist now */
1243 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1244 /* lookup with version checking */
1246 rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1248 /* the new_fid should have been filled at this moment */
1249 if (lu_fid_eq(old_fid, new_fid))
1250 GOTO(out_unlock_old, rc);
1252 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1253 lu_fid_eq(new_fid, rr->rr_fid2))
1254 GOTO(out_unlock_old, rc = -EINVAL);
1256 mdt_lock_reg_init(lh_newp, LCK_EX);
1257 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1259 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1261 rc = mdt_object_lock(info, mnew, lh_newp,
1262 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1264 mdt_object_put(info->mti_env, mnew);
1265 GOTO(out_unlock_old, rc);
1267 /* get and save version after locking */
1268 mdt_version_get_save(info, mnew, 3);
1269 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1270 } else if (rc != -EREMOTE && rc != -ENOENT) {
1271 GOTO(out_unlock_old, rc);
1273 mdt_enoent_version_save(info, 3);
1276 /* step 5: rename it */
1277 mdt_reint_init_ma(info, ma);
1278 if (!ma->ma_lmm || !ma->ma_cookie)
1279 GOTO(out_unlock_new, rc = -EINVAL);
1281 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1282 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1285 /* Check if @dst is subdir of @src. */
1286 rc = mdt_rename_sanity(info, old_fid);
1288 GOTO(out_unlock_new, rc);
1290 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1291 mdt_object_child(mtgtdir), old_fid, &slname,
1292 (mnew ? mdt_object_child(mnew) : NULL),
1295 /* handle last link of tgt object */
1297 mdt_counter_incr(req->rq_export, LPROC_MDT_RENAME);
1299 mdt_handle_last_unlink(info, mnew, ma);
1301 mdt_rename_counter_tally(info, info->mti_mdt, req->rq_export,
1308 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1310 mdt_object_unlock_put(info, mold, lh_oldp, rc);
1312 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1314 mdt_object_put(info->mti_env, mtgtdir);
1316 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1318 mdt_rename_unlock(&rename_lh);
1322 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1323 struct mdt_lock_handle *lhc);
1325 static mdt_reinter reinters[REINT_MAX] = {
1326 [REINT_SETATTR] = mdt_reint_setattr,
1327 [REINT_CREATE] = mdt_reint_create,
1328 [REINT_LINK] = mdt_reint_link,
1329 [REINT_UNLINK] = mdt_reint_unlink,
1330 [REINT_RENAME] = mdt_reint_rename,
1331 [REINT_OPEN] = mdt_reint_open,
1332 [REINT_SETXATTR] = mdt_reint_setxattr
1335 int mdt_reint_rec(struct mdt_thread_info *info,
1336 struct mdt_lock_handle *lhc)
1341 rc = reinters[info->mti_rr.rr_opcode](info, lhc);