4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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>
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include "mdt_internal.h"
52 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
55 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
56 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
57 &RMF_MDT_MD, RCL_SERVER);
59 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
61 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
65 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
69 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
70 struct mdt_object *object,
71 struct mdt_body *repbody)
75 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
76 * we won't go to below*/
77 if (repbody->valid & OBD_MD_FLMDSCAPA)
80 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
81 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
82 struct lustre_capa *capa;
84 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
86 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
87 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
90 repbody->valid |= OBD_MD_FLMDSCAPA;
97 * Get version of object by fid.
99 * Return real version or ENOENT_VERSION if object doesn't exist
101 static void mdt_obj_version_get(struct mdt_thread_info *info,
102 struct mdt_object *o, __u64 *version)
105 LASSERT(mdt_object_exists(o) >= 0);
106 if (mdt_object_exists(o) > 0)
107 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
109 *version = ENOENT_VERSION;
110 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
111 PFID(mdt_object_fid(o)), *version);
115 * Check version is correct.
117 * Should be called only during replay.
119 static int mdt_version_check(struct ptlrpc_request *req,
120 __u64 version, int idx)
122 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
125 if (!exp_connect_vbr(req->rq_export))
128 LASSERT(req_is_replay(req));
129 /** VBR: version is checked always because costs nothing */
130 LASSERT(idx < PTLRPC_NUM_VERSIONS);
131 /** Sanity check for malformed buffers */
132 if (pre_ver == NULL) {
133 CERROR("No versions in request buffer\n");
134 cfs_spin_lock(&req->rq_export->exp_lock);
135 req->rq_export->exp_vbr_failed = 1;
136 cfs_spin_unlock(&req->rq_export->exp_lock);
138 } else if (pre_ver[idx] != version) {
139 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
140 pre_ver[idx], version);
141 cfs_spin_lock(&req->rq_export->exp_lock);
142 req->rq_export->exp_vbr_failed = 1;
143 cfs_spin_unlock(&req->rq_export->exp_lock);
150 * Save pre-versions in reply.
152 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
157 if (!exp_connect_vbr(req->rq_export))
160 LASSERT(!req_is_replay(req));
161 LASSERT(req->rq_repmsg != NULL);
162 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
164 reply_ver[idx] = version;
168 * Save enoent version, it is needed when it is obvious that object doesn't
169 * exist, e.g. child during create.
171 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
173 /* save version of file name for replay, it must be ENOENT here */
174 if (!req_is_replay(mdt_info_req(info))) {
175 info->mti_ver[idx] = ENOENT_VERSION;
176 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
181 * Get version from disk and save in reply buffer.
183 * Versions are saved in reply only during normal operations not replays.
185 void mdt_version_get_save(struct mdt_thread_info *info,
186 struct mdt_object *mto, int idx)
188 /* don't save versions during replay */
189 if (!req_is_replay(mdt_info_req(info))) {
190 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
191 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
196 * Get version from disk and check it, no save in reply.
198 int mdt_version_get_check(struct mdt_thread_info *info,
199 struct mdt_object *mto, int idx)
201 /* only check versions during replay */
202 if (!req_is_replay(mdt_info_req(info)))
205 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
206 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
210 * Get version from disk and check if recovery or just save.
212 int mdt_version_get_check_save(struct mdt_thread_info *info,
213 struct mdt_object *mto, int idx)
217 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
218 if (req_is_replay(mdt_info_req(info)))
219 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
222 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
227 * Lookup with version checking.
229 * This checks version of 'name'. Many reint functions uses 'name' for child not
230 * FID, therefore we need to get object by name and check its version.
232 int mdt_lookup_version_check(struct mdt_thread_info *info,
233 struct mdt_object *p, struct lu_name *lname,
234 struct lu_fid *fid, int idx)
238 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
240 /* Check version only during replay */
241 if (!req_is_replay(mdt_info_req(info)))
244 info->mti_ver[idx] = ENOENT_VERSION;
246 struct mdt_object *child;
247 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
248 if (likely(!IS_ERR(child))) {
249 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
250 mdt_object_put(info->mti_env, child);
253 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
254 return vbrc ? vbrc : rc;
259 * VBR: we save three versions in reply:
260 * 0 - parent. Check that parent version is the same during replay.
261 * 1 - name. Version of 'name' if file exists with the same name or
262 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
263 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
266 static int mdt_md_create(struct mdt_thread_info *info)
268 struct mdt_device *mdt = info->mti_mdt;
269 struct mdt_object *parent;
270 struct mdt_object *child;
271 struct mdt_lock_handle *lh;
272 struct mdt_body *repbody;
273 struct md_attr *ma = &info->mti_attr;
274 struct mdt_reint_record *rr = &info->mti_rr;
275 struct lu_name *lname;
279 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
280 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
282 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
284 lh = &info->mti_lh[MDT_LH_PARENT];
285 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
287 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
288 MDS_INODELOCK_UPDATE);
290 RETURN(PTR_ERR(parent));
292 if (mdt_object_obf(parent))
293 GOTO(out_put_parent, rc = -EPERM);
295 rc = mdt_version_get_check_save(info, parent, 0);
297 GOTO(out_put_parent, rc);
300 * Check child name version during replay.
301 * During create replay a file may exist with same name.
303 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
304 rc = mdt_lookup_version_check(info, parent, lname,
305 &info->mti_tmp_fid1, 1);
306 /* -ENOENT is expected here */
307 if (rc != 0 && rc != -ENOENT)
308 GOTO(out_put_parent, rc);
310 /* save version of file name for replay, it must be ENOENT here */
311 mdt_enoent_version_save(info, 1);
313 child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
314 if (likely(!IS_ERR(child))) {
315 struct md_object *next = mdt_object_child(parent);
317 ma->ma_need = MA_INODE;
319 /* capa for cross-ref will be stored here */
320 ma->ma_capa = req_capsule_server_get(info->mti_pill,
322 LASSERT(ma->ma_capa);
324 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
325 OBD_FAIL_MDS_REINT_CREATE_WRITE);
327 /* Version of child will be updated on disk. */
328 info->mti_mos = child;
329 rc = mdt_version_get_check_save(info, child, 2);
331 GOTO(out_put_child, rc);
333 /* Let lower layer know current lock mode. */
334 info->mti_spec.sp_cr_mode =
335 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
338 * Do perform lookup sanity check. We do not know if name exists
341 info->mti_spec.sp_cr_lookup = 1;
342 info->mti_spec.sp_feat = &dt_directory_features;
344 rc = mdo_create(info->mti_env, next, lname,
345 mdt_object_child(child),
346 &info->mti_spec, ma);
348 /* Return fid & attr to client. */
349 if (ma->ma_valid & MA_INODE)
350 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
351 mdt_object_fid(child));
354 mdt_object_put(info->mti_env, child);
358 mdt_create_pack_capa(info, rc, child, repbody);
360 mdt_object_unlock_put(info, parent, lh, rc);
364 /* Partial request to create object only */
365 static int mdt_md_mkobj(struct mdt_thread_info *info)
367 struct mdt_device *mdt = info->mti_mdt;
368 struct mdt_object *o;
369 struct mdt_body *repbody;
370 struct md_attr *ma = &info->mti_attr;
374 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
375 PFID(info->mti_rr.rr_fid2));
377 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
379 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
381 struct md_object *next = mdt_object_child(o);
383 ma->ma_need = MA_INODE;
387 * Cross-ref create can encounter already created obj in case of
388 * recovery, just get attr in that case.
390 if (mdt_object_exists(o) == 1) {
391 rc = mo_attr_get(info->mti_env, next, ma);
394 * Here, NO permission check for object_create,
395 * such check has been done on the original MDS.
397 rc = mo_object_create(info->mti_env, next,
398 &info->mti_spec, ma);
401 /* Return fid & attr to client. */
402 if (ma->ma_valid & MA_INODE)
403 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
406 mdt_object_put(info->mti_env, o);
410 mdt_create_pack_capa(info, rc, o, repbody);
414 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
415 struct md_attr *ma, int flags)
417 struct mdt_lock_handle *lh;
418 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
419 __u64 lockpart = MDS_INODELOCK_UPDATE;
423 /* attr shouldn't be set on remote object */
424 LASSERT(mdt_object_exists(mo) >= 0);
426 lh = &info->mti_lh[MDT_LH_PARENT];
427 mdt_lock_reg_init(lh, LCK_PW);
429 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
430 lockpart |= MDS_INODELOCK_LOOKUP;
432 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
436 if (mdt_object_exists(mo) == 0)
437 GOTO(out_unlock, rc = -ENOENT);
439 /* all attrs are packed into mti_attr in unpack_setattr */
440 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
441 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
443 /* This is only for set ctime when rename's source is on remote MDS. */
444 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
445 ma->ma_attr_flags |= MDS_VTX_BYPASS;
447 /* VBR: update version if attr changed are important for recovery */
449 /* update on-disk version of changed object */
451 rc = mdt_version_get_check_save(info, mo, 0);
453 GOTO(out_unlock, rc);
456 /* all attrs are packed into mti_attr in unpack_setattr */
457 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
459 GOTO(out_unlock, rc);
463 mdt_object_unlock(info, mo, lh, rc);
467 static int mdt_reint_setattr(struct mdt_thread_info *info,
468 struct mdt_lock_handle *lhc)
470 struct md_attr *ma = &info->mti_attr;
471 struct mdt_reint_record *rr = &info->mti_rr;
472 struct ptlrpc_request *req = mdt_info_req(info);
473 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
474 struct mdt_file_data *mfd;
475 struct mdt_object *mo;
476 struct md_object *next;
477 struct mdt_body *repbody;
481 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
482 (unsigned int)ma->ma_attr.la_valid);
484 if (info->mti_dlm_req)
485 ldlm_request_cancel(req, info->mti_dlm_req, 0);
487 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
488 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
490 GOTO(out, rc = PTR_ERR(mo));
492 /* start a log jounal handle if needed */
493 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
494 if ((ma->ma_attr.la_valid & LA_SIZE) ||
495 (rr->rr_flags & MRF_OPEN_TRUNC)) {
496 /* Check write access for the O_TRUNC case */
497 if (mdt_write_read(mo) < 0)
498 GOTO(out_put, rc = -ETXTBSY);
500 } else if (info->mti_ioepoch &&
501 (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
502 /* Truncate case. IOEpoch is opened. */
503 rc = mdt_write_get(mo);
510 GOTO(out_put, rc = -ENOMEM);
513 mdt_ioepoch_open(info, mo, 0);
514 repbody->ioepoch = mo->mot_ioepoch;
516 mdt_object_get(info->mti_env, mo);
517 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
518 mfd->mfd_object = mo;
519 mfd->mfd_xid = req->rq_xid;
521 cfs_spin_lock(&med->med_open_lock);
522 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
523 cfs_spin_unlock(&med->med_open_lock);
524 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
527 som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
529 /* SOM Attribute update case. Find the proper mfd and update
530 * SOM attributes on the proper object. */
531 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
532 LASSERT(info->mti_ioepoch);
534 cfs_spin_lock(&med->med_open_lock);
535 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
537 cfs_spin_unlock(&med->med_open_lock);
538 CDEBUG(D_INODE, "no handle for file close: "
539 "fid = "DFID": cookie = "LPX64"\n",
540 PFID(info->mti_rr.rr_fid1),
541 info->mti_ioepoch->handle.cookie);
542 GOTO(out_put, rc = -ESTALE);
544 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
545 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
547 class_handle_unhash(&mfd->mfd_handle);
548 cfs_list_del_init(&mfd->mfd_list);
549 cfs_spin_unlock(&med->med_open_lock);
551 /* Close the found mfd, update attributes. */
552 ma->ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
553 OBD_ALLOC_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
554 if (ma->ma_lmm == NULL)
555 GOTO(out_put, rc = -ENOMEM);
557 mdt_mfd_close(info, mfd);
559 OBD_FREE_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
561 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
566 ma->ma_need = MA_INODE;
568 next = mdt_object_child(mo);
569 rc = mo_attr_get(info->mti_env, next, ma);
573 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
575 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
576 info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
577 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
578 (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
579 struct lustre_capa *capa;
581 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
583 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
584 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
587 repbody->valid |= OBD_MD_FLOSSCAPA;
592 mdt_object_put(info->mti_env, mo);
595 mdt_counter_incr(req, LPROC_MDT_SETATTR);
597 mdt_client_compatibility(info);
598 rc2 = mdt_fix_reply(info);
604 static int mdt_reint_create(struct mdt_thread_info *info,
605 struct mdt_lock_handle *lhc)
607 struct ptlrpc_request *req = mdt_info_req(info);
611 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
612 RETURN(err_serious(-ESTALE));
614 if (info->mti_dlm_req)
615 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
617 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
619 /* Cross-ref case. */
620 /* TODO: we can add LPROC_MDT_CROSS for cross-ref stats */
621 if (info->mti_cross_ref) {
622 rc = mdt_md_mkobj(info);
624 LASSERT(info->mti_rr.rr_namelen > 0);
625 mdt_counter_incr(req, LPROC_MDT_MKDIR);
626 rc = mdt_md_create(info);
636 /* Special file should stay on the same node as parent. */
637 LASSERT(info->mti_rr.rr_namelen > 0);
638 mdt_counter_incr(req, LPROC_MDT_MKNOD);
639 rc = mdt_md_create(info);
643 rc = err_serious(-EOPNOTSUPP);
649 * VBR: save parent version in reply and child version getting by its name.
650 * Version of child is getting and checking during its lookup. If
652 static int mdt_reint_unlink(struct mdt_thread_info *info,
653 struct mdt_lock_handle *lhc)
655 struct mdt_reint_record *rr = &info->mti_rr;
656 struct ptlrpc_request *req = mdt_info_req(info);
657 struct md_attr *ma = &info->mti_attr;
658 struct lu_fid *child_fid = &info->mti_tmp_fid1;
659 struct mdt_object *mp;
660 struct mdt_object *mc;
661 struct mdt_lock_handle *parent_lh;
662 struct mdt_lock_handle *child_lh;
663 struct lu_name *lname;
667 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
670 if (info->mti_dlm_req)
671 ldlm_request_cancel(req, info->mti_dlm_req, 0);
673 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
674 RETURN(err_serious(-ENOENT));
677 * step 1: lock the parent. Note, this may be child in case of
678 * remote operation denoted by ->mti_cross_ref flag.
680 parent_lh = &info->mti_lh[MDT_LH_PARENT];
681 if (info->mti_cross_ref) {
683 * Init reg lock for cross ref case when we need to do only
686 mdt_lock_reg_init(parent_lh, LCK_PW);
688 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
691 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
692 MDS_INODELOCK_UPDATE);
695 /* errors are possible here in cross-ref cases, see below */
696 if (info->mti_cross_ref)
701 if (mdt_object_obf(mp))
702 GOTO(out_unlock_parent, rc = -EPERM);
704 rc = mdt_version_get_check_save(info, mp, 0);
706 GOTO(out_unlock_parent, rc);
708 mdt_reint_init_ma(info, ma);
709 if (!ma->ma_lmm || !ma->ma_cookie)
710 GOTO(out_unlock_parent, rc = -EINVAL);
712 if (info->mti_cross_ref) {
714 * Remote partial operation. It is possible that replay may
715 * happen on parent MDT and this operation will be repeated.
716 * Therefore the object absense is allowed case and nothing
717 * should be done here.
719 if (mdt_object_exists(mp) > 0) {
720 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
721 rc = mo_ref_del(info->mti_env,
722 mdt_object_child(mp), ma);
724 mdt_handle_last_unlink(info, mp, ma);
727 GOTO(out_unlock_parent, rc);
730 /* step 2: find & lock the child */
731 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
732 /* lookup child object along with version checking */
734 rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
736 GOTO(out_unlock_parent, rc);
738 /* We will lock the child regardless it is local or remote. No harm. */
739 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
741 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
742 child_lh = &info->mti_lh[MDT_LH_CHILD];
743 mdt_lock_reg_init(child_lh, LCK_EX);
744 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
747 mdt_object_put(info->mti_env, mc);
748 GOTO(out_unlock_parent, rc);
751 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
752 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
753 /* save version when object is locked */
754 mdt_version_get_save(info, mc, 1);
756 * Now we can only make sure we need MA_INODE, in mdd layer, will check
757 * whether need MA_LOV and MA_COOKIE.
759 ma->ma_need = MA_INODE;
761 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
762 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
763 mdt_object_child(mc), lname, ma);
765 mdt_handle_last_unlink(info, mc, ma);
767 if (ma->ma_valid & MA_INODE) {
768 switch (ma->ma_attr.la_mode & S_IFMT) {
770 mdt_counter_incr(req, LPROC_MDT_RMDIR);
778 mdt_counter_incr(req, LPROC_MDT_UNLINK);
781 LASSERTF(0, "bad file type %o unlinking\n",
782 ma->ma_attr.la_mode);
788 mdt_object_unlock_put(info, mc, child_lh, rc);
790 mdt_object_unlock_put(info, mp, parent_lh, rc);
796 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
799 static int mdt_reint_link(struct mdt_thread_info *info,
800 struct mdt_lock_handle *lhc)
802 struct mdt_reint_record *rr = &info->mti_rr;
803 struct ptlrpc_request *req = mdt_info_req(info);
804 struct md_attr *ma = &info->mti_attr;
805 struct mdt_object *ms;
806 struct mdt_object *mp;
807 struct mdt_lock_handle *lhs;
808 struct mdt_lock_handle *lhp;
809 struct lu_name *lname;
813 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
814 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
816 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
817 RETURN(err_serious(-ENOENT));
819 if (info->mti_dlm_req)
820 ldlm_request_cancel(req, info->mti_dlm_req, 0);
822 if (info->mti_cross_ref) {
823 /* MDT holding name ask us to add ref. */
824 lhs = &info->mti_lh[MDT_LH_CHILD];
825 mdt_lock_reg_init(lhs, LCK_EX);
826 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
827 MDS_INODELOCK_UPDATE);
831 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
832 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
833 mdt_object_unlock_put(info, ms, lhs, rc);
837 /* Invalid case so return error immediately instead of
839 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
842 /* step 1: find & lock the target parent dir */
843 lhp = &info->mti_lh[MDT_LH_PARENT];
844 mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
846 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
847 MDS_INODELOCK_UPDATE);
851 if (mdt_object_obf(mp))
852 GOTO(out_unlock_parent, rc = -EPERM);
854 rc = mdt_version_get_check_save(info, mp, 0);
856 GOTO(out_unlock_parent, rc);
858 /* step 2: find & lock the source */
859 lhs = &info->mti_lh[MDT_LH_CHILD];
860 mdt_lock_reg_init(lhs, LCK_EX);
862 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
864 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
866 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
869 mdt_object_put(info->mti_env, ms);
870 GOTO(out_unlock_parent, rc);
873 /* step 3: link it */
874 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
875 OBD_FAIL_MDS_REINT_LINK_WRITE);
878 rc = mdt_version_get_check_save(info, ms, 1);
880 GOTO(out_unlock_child, rc);
882 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
883 /** check target version by name during replay */
884 rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
885 if (rc != 0 && rc != -ENOENT)
886 GOTO(out_unlock_child, rc);
887 /* save version of file name for replay, it must be ENOENT here */
888 if (!req_is_replay(mdt_info_req(info))) {
889 info->mti_ver[2] = ENOENT_VERSION;
890 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
893 rc = mdo_link(info->mti_env, mdt_object_child(mp),
894 mdt_object_child(ms), lname, ma);
897 mdt_counter_incr(req, LPROC_MDT_LINK);
901 mdt_object_unlock_put(info, ms, lhs, rc);
903 mdt_object_unlock_put(info, mp, lhp, rc);
907 * lock the part of the directory according to the hash of the name
908 * (lh->mlh_pdo_hash) in parallel directory lock.
910 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
911 struct mdt_lock_handle *lh,
912 struct mdt_object *obj, __u64 ibits)
914 struct ldlm_res_id *res_id = &info->mti_res_id;
915 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
916 ldlm_policy_data_t *policy = &info->mti_policy;
920 * Finish res_id initializing by name hash marking part of
921 * directory which is taking modification.
923 LASSERT(lh->mlh_pdo_hash != 0);
924 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
925 memset(policy, 0, sizeof(*policy));
926 policy->l_inodebits.bits = ibits;
928 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
929 * going to be sent to client. If it is - mdt_intent_policy() path will
930 * fix it up and turn FL_LOCAL flag off.
932 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
933 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
934 &info->mti_exp->exp_handle.h_cookie);
938 /* partial operation for rename */
939 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
941 struct mdt_reint_record *rr = &info->mti_rr;
942 struct ptlrpc_request *req = mdt_info_req(info);
943 struct md_attr *ma = &info->mti_attr;
944 struct mdt_object *mtgtdir;
945 struct mdt_object *mtgt = NULL;
946 struct mdt_lock_handle *lh_tgtdir;
947 struct mdt_lock_handle *lh_tgt = NULL;
948 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
949 struct lu_name *lname;
953 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
954 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
956 /* step 1: lookup & lock the tgt dir. */
957 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
958 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
960 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
961 MDS_INODELOCK_UPDATE);
963 RETURN(PTR_ERR(mtgtdir));
965 /* step 2: find & lock the target object if exists. */
966 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
967 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
968 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
969 lname, tgt_fid, &info->mti_spec);
970 if (rc != 0 && rc != -ENOENT) {
971 GOTO(out_unlock_tgtdir, rc);
972 } else if (rc == 0) {
974 * In case of replay that name can be already inserted, check
975 * that and do nothing if so.
977 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
978 GOTO(out_unlock_tgtdir, rc);
980 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
981 mdt_lock_reg_init(lh_tgt, LCK_EX);
983 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
984 MDS_INODELOCK_LOOKUP);
986 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
988 mdt_reint_init_ma(info, ma);
989 if (!ma->ma_lmm || !ma->ma_cookie)
990 GOTO(out_unlock_tgt, rc = -EINVAL);
992 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
993 mdt_object_child(mtgt), rr->rr_fid2,
995 } else /* -ENOENT */ {
996 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
997 lname, rr->rr_fid2, ma);
1000 /* handle last link of tgt object */
1001 if (rc == 0 && mtgt)
1002 mdt_handle_last_unlink(info, mtgt, ma);
1007 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
1009 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
1013 static int mdt_rename_lock(struct mdt_thread_info *info,
1014 struct lustre_handle *lh)
1016 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1017 ldlm_policy_data_t *policy = &info->mti_policy;
1018 struct ldlm_res_id *res_id = &info->mti_res_id;
1023 ms = mdt_md_site(info->mti_mdt);
1024 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1026 memset(policy, 0, sizeof *policy);
1027 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1029 if (ms->ms_control_exp == NULL) {
1030 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1033 * Current node is controller, that is mdt0, where we should
1036 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1037 LCK_EX, &flags, ldlm_blocking_ast,
1038 ldlm_completion_ast, NULL, NULL, 0,
1039 &info->mti_exp->exp_handle.h_cookie,
1042 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
1043 ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1047 * This is the case mdt0 is remote node, issue DLM lock like
1050 rc = ldlm_cli_enqueue(ms->ms_control_exp, NULL, &einfo, res_id,
1051 policy, &flags, NULL, 0, lh, 0);
1057 static void mdt_rename_unlock(struct lustre_handle *lh)
1060 LASSERT(lustre_handle_is_used(lh));
1061 ldlm_lock_decref(lh, LCK_EX);
1066 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1067 * target. Source should not be ancestor of target dir. May be other rename
1068 * checks can be moved here later.
1070 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1072 struct mdt_reint_record *rr = &info->mti_rr;
1073 struct lu_fid dst_fid = *rr->rr_fid2;
1074 struct mdt_object *dst;
1079 LASSERT(fid_is_sane(&dst_fid));
1080 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1082 rc = mdo_is_subdir(info->mti_env,
1083 mdt_object_child(dst), fid,
1085 mdt_object_put(info->mti_env, dst);
1086 if (rc != -EREMOTE && rc < 0) {
1087 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1089 /* check the found fid */
1090 if (lu_fid_eq(&dst_fid, fid))
1096 } while (rc == -EREMOTE);
1102 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1103 * 2 - src child; 3 - tgt child.
1104 * Update on disk version of src child.
1106 static int mdt_reint_rename(struct mdt_thread_info *info,
1107 struct mdt_lock_handle *lhc)
1109 struct mdt_reint_record *rr = &info->mti_rr;
1110 struct md_attr *ma = &info->mti_attr;
1111 struct ptlrpc_request *req = mdt_info_req(info);
1112 struct mdt_object *msrcdir;
1113 struct mdt_object *mtgtdir;
1114 struct mdt_object *mold;
1115 struct mdt_object *mnew = NULL;
1116 struct mdt_lock_handle *lh_srcdirp;
1117 struct mdt_lock_handle *lh_tgtdirp;
1118 struct mdt_lock_handle *lh_oldp;
1119 struct mdt_lock_handle *lh_newp;
1120 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1121 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1122 struct lustre_handle rename_lh = { 0 };
1123 struct lu_name slname = { 0 };
1124 struct lu_name *lname;
1128 if (info->mti_dlm_req)
1129 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1131 if (info->mti_cross_ref) {
1132 rc = mdt_reint_rename_tgt(info);
1136 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1137 PFID(rr->rr_fid1), rr->rr_name,
1138 PFID(rr->rr_fid2), rr->rr_tgt);
1140 rc = mdt_rename_lock(info, &rename_lh);
1142 CERROR("Can't lock FS for rename, rc %d\n", rc);
1146 lh_newp = &info->mti_lh[MDT_LH_NEW];
1148 /* step 1: lock the source dir. */
1149 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1150 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1152 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1153 MDS_INODELOCK_UPDATE);
1154 if (IS_ERR(msrcdir))
1155 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1157 if (mdt_object_obf(msrcdir))
1158 GOTO(out_unlock_source, rc = -EPERM);
1160 rc = mdt_version_get_check_save(info, msrcdir, 0);
1162 GOTO(out_unlock_source, rc);
1164 /* step 2: find & lock the target dir. */
1165 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1166 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1168 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1169 mdt_object_get(info->mti_env, msrcdir);
1171 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1172 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1173 MDS_INODELOCK_UPDATE);
1175 GOTO(out_unlock_source, rc);
1176 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1179 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1181 if (IS_ERR(mtgtdir))
1182 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1184 if (mdt_object_obf(mtgtdir))
1185 GOTO(out_put_target, rc = -EPERM);
1187 /* check early, the real version will be saved after locking */
1188 rc = mdt_version_get_check(info, mtgtdir, 1);
1190 GOTO(out_put_target, rc);
1192 rc = mdt_object_exists(mtgtdir);
1194 GOTO(out_put_target, rc = -ESTALE);
1195 } else if (rc > 0) {
1196 /* we lock the target dir if it is local */
1197 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1198 MDS_INODELOCK_UPDATE,
1201 GOTO(out_put_target, rc);
1202 /* get and save correct version after locking */
1203 mdt_version_get_save(info, mtgtdir, 1);
1207 /* step 3: find & lock the old object. */
1208 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1209 mdt_name_copy(&slname, lname);
1211 rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1213 GOTO(out_unlock_target, rc);
1215 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1216 GOTO(out_unlock_target, rc = -EINVAL);
1218 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1220 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1222 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1223 mdt_lock_reg_init(lh_oldp, LCK_EX);
1224 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1227 mdt_object_put(info->mti_env, mold);
1228 GOTO(out_unlock_target, rc);
1231 info->mti_mos = mold;
1232 /* save version after locking */
1233 mdt_version_get_save(info, mold, 2);
1234 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1236 /* step 4: find & lock the new object. */
1237 /* new target object may not exist now */
1238 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1239 /* lookup with version checking */
1241 rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1243 /* the new_fid should have been filled at this moment */
1244 if (lu_fid_eq(old_fid, new_fid))
1245 GOTO(out_unlock_old, rc);
1247 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1248 lu_fid_eq(new_fid, rr->rr_fid2))
1249 GOTO(out_unlock_old, rc = -EINVAL);
1251 mdt_lock_reg_init(lh_newp, LCK_EX);
1252 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1254 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1256 rc = mdt_object_lock(info, mnew, lh_newp,
1257 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1259 mdt_object_put(info->mti_env, mnew);
1260 GOTO(out_unlock_old, rc);
1262 /* get and save version after locking */
1263 mdt_version_get_save(info, mnew, 3);
1264 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1265 } else if (rc != -EREMOTE && rc != -ENOENT) {
1266 GOTO(out_unlock_old, rc);
1268 mdt_enoent_version_save(info, 3);
1271 /* step 5: rename it */
1272 mdt_reint_init_ma(info, ma);
1273 if (!ma->ma_lmm || !ma->ma_cookie)
1274 GOTO(out_unlock_new, rc = -EINVAL);
1276 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1277 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1280 /* Check if @dst is subdir of @src. */
1281 rc = mdt_rename_sanity(info, old_fid);
1283 GOTO(out_unlock_new, rc);
1285 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1286 mdt_object_child(mtgtdir), old_fid, &slname,
1287 (mnew ? mdt_object_child(mnew) : NULL),
1290 /* handle last link of tgt object */
1292 mdt_counter_incr(req, LPROC_MDT_RENAME);
1294 mdt_handle_last_unlink(info, mnew, ma);
1296 mdt_rename_counter_tally(info, info->mti_mdt, req,
1303 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1305 mdt_object_unlock_put(info, mold, lh_oldp, rc);
1307 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1309 mdt_object_put(info->mti_env, mtgtdir);
1311 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1313 mdt_rename_unlock(&rename_lh);
1317 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1318 struct mdt_lock_handle *lhc);
1320 static mdt_reinter reinters[REINT_MAX] = {
1321 [REINT_SETATTR] = mdt_reint_setattr,
1322 [REINT_CREATE] = mdt_reint_create,
1323 [REINT_LINK] = mdt_reint_link,
1324 [REINT_UNLINK] = mdt_reint_unlink,
1325 [REINT_RENAME] = mdt_reint_rename,
1326 [REINT_OPEN] = mdt_reint_open,
1327 [REINT_SETXATTR] = mdt_reint_setxattr
1330 int mdt_reint_rec(struct mdt_thread_info *info,
1331 struct mdt_lock_handle *lhc)
1336 rc = reinters[info->mti_rr.rr_opcode](info, lhc);