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 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/mdt/mdt_reint.c
41 * Lustre Metadata Target (mdt) reintegration routines
43 * Author: Peter Braam <braam@clusterfs.com>
44 * Author: Andreas Dilger <adilger@clusterfs.com>
45 * Author: Phil Schwan <phil@clusterfs.com>
46 * Author: Huang Hua <huanghua@clusterfs.com>
47 * Author: Yury Umanets <umka@clusterfs.com>
51 # define EXPORT_SYMTAB
53 #define DEBUG_SUBSYSTEM S_MDS
55 #include "mdt_internal.h"
58 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
61 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
62 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
63 &RMF_MDT_MD, RCL_SERVER);
65 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
67 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
71 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
75 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
76 struct mdt_object *object,
77 struct mdt_body *repbody)
81 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
82 * we won't go to below*/
83 if (repbody->valid & OBD_MD_FLMDSCAPA)
86 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
87 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
88 struct lustre_capa *capa;
90 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
92 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
93 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
96 repbody->valid |= OBD_MD_FLMDSCAPA;
103 * Get version of object by fid.
105 * Return real version or ENOENT_VERSION if object doesn't exist
107 static void mdt_obj_version_get(struct mdt_thread_info *info,
108 struct mdt_object *o, __u64 *version)
111 LASSERT(mdt_object_exists(o) >= 0);
112 if (mdt_object_exists(o) > 0)
113 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
115 *version = ENOENT_VERSION;
116 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
117 PFID(mdt_object_fid(o)), *version);
121 * Check version is correct.
123 * Should be called only during replay.
125 static int mdt_version_check(struct ptlrpc_request *req,
126 __u64 version, int idx)
128 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
131 if (!exp_connect_vbr(req->rq_export))
134 LASSERT(req_is_replay(req));
135 /** VBR: version is checked always because costs nothing */
136 LASSERT(idx < PTLRPC_NUM_VERSIONS);
137 /** Sanity check for malformed buffers */
138 if (pre_ver == NULL) {
139 CERROR("No versions in request buffer\n");
140 cfs_spin_lock(&req->rq_export->exp_lock);
141 req->rq_export->exp_vbr_failed = 1;
142 cfs_spin_unlock(&req->rq_export->exp_lock);
144 } else if (pre_ver[idx] != version) {
145 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
146 pre_ver[idx], version);
147 cfs_spin_lock(&req->rq_export->exp_lock);
148 req->rq_export->exp_vbr_failed = 1;
149 cfs_spin_unlock(&req->rq_export->exp_lock);
156 * Save pre-versions in reply.
158 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
163 if (!exp_connect_vbr(req->rq_export))
166 LASSERT(!req_is_replay(req));
167 LASSERT(req->rq_repmsg != NULL);
168 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
170 reply_ver[idx] = version;
174 * Save enoent version, it is needed when it is obvious that object doesn't
175 * exist, e.g. child during create.
177 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
179 /* save version of file name for replay, it must be ENOENT here */
180 if (!req_is_replay(mdt_info_req(info))) {
181 info->mti_ver[idx] = ENOENT_VERSION;
182 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
187 * Get version from disk and save in reply buffer.
189 * Versions are saved in reply only during normal operations not replays.
191 void mdt_version_get_save(struct mdt_thread_info *info,
192 struct mdt_object *mto, int idx)
194 /* don't save versions during replay */
195 if (!req_is_replay(mdt_info_req(info))) {
196 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
197 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
202 * Get version from disk and check it, no save in reply.
204 int mdt_version_get_check(struct mdt_thread_info *info,
205 struct mdt_object *mto, int idx)
207 /* only check versions during replay */
208 if (!req_is_replay(mdt_info_req(info)))
211 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
212 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
216 * Get version from disk and check if recovery or just save.
218 int mdt_version_get_check_save(struct mdt_thread_info *info,
219 struct mdt_object *mto, int idx)
223 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
224 if (req_is_replay(mdt_info_req(info)))
225 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
228 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
233 * Lookup with version checking.
235 * This checks version of 'name'. Many reint functions uses 'name' for child not
236 * FID, therefore we need to get object by name and check its version.
238 int mdt_lookup_version_check(struct mdt_thread_info *info,
239 struct mdt_object *p, struct lu_name *lname,
240 struct lu_fid *fid, int idx)
244 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
246 /* Check version only during replay */
247 if (!req_is_replay(mdt_info_req(info)))
250 info->mti_ver[idx] = ENOENT_VERSION;
252 struct mdt_object *child;
253 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
254 if (likely(!IS_ERR(child))) {
255 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
256 mdt_object_put(info->mti_env, child);
259 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
260 return vbrc ? vbrc : rc;
265 * VBR: we save three versions in reply:
266 * 0 - parent. Check that parent version is the same during replay.
267 * 1 - name. Version of 'name' if file exists with the same name or
268 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
269 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
272 static int mdt_md_create(struct mdt_thread_info *info)
274 struct mdt_device *mdt = info->mti_mdt;
275 struct mdt_object *parent;
276 struct mdt_object *child;
277 struct mdt_lock_handle *lh;
278 struct mdt_body *repbody;
279 struct md_attr *ma = &info->mti_attr;
280 struct mdt_reint_record *rr = &info->mti_rr;
281 struct lu_name *lname;
285 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
286 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
288 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
290 lh = &info->mti_lh[MDT_LH_PARENT];
291 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
293 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
294 MDS_INODELOCK_UPDATE);
296 RETURN(PTR_ERR(parent));
298 rc = mdt_version_get_check_save(info, parent, 0);
300 GOTO(out_put_parent, rc);
303 * Check child name version during replay.
304 * During create replay a file may exist with same name.
306 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
307 rc = mdt_lookup_version_check(info, parent, lname,
308 &info->mti_tmp_fid1, 1);
309 /* -ENOENT is expected here */
310 if (rc != 0 && rc != -ENOENT)
311 GOTO(out_put_parent, rc);
313 /* save version of file name for replay, it must be ENOENT here */
314 mdt_enoent_version_save(info, 1);
316 child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
317 if (likely(!IS_ERR(child))) {
318 struct md_object *next = mdt_object_child(parent);
320 ma->ma_need = MA_INODE;
322 /* capa for cross-ref will be stored here */
323 ma->ma_capa = req_capsule_server_get(info->mti_pill,
325 LASSERT(ma->ma_capa);
327 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
328 OBD_FAIL_MDS_REINT_CREATE_WRITE);
330 /* Version of child will be updated on disk. */
331 info->mti_mos = child;
332 rc = mdt_version_get_check_save(info, child, 2);
334 GOTO(out_put_child, rc);
336 /* Let lower layer know current lock mode. */
337 info->mti_spec.sp_cr_mode =
338 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
341 * Do perform lookup sanity check. We do not know if name exists
344 info->mti_spec.sp_cr_lookup = 1;
345 info->mti_spec.sp_feat = &dt_directory_features;
347 rc = mdo_create(info->mti_env, next, lname,
348 mdt_object_child(child),
349 &info->mti_spec, ma);
351 /* Return fid & attr to client. */
352 if (ma->ma_valid & MA_INODE)
353 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
354 mdt_object_fid(child));
357 mdt_object_put(info->mti_env, child);
361 mdt_create_pack_capa(info, rc, child, repbody);
363 mdt_object_unlock_put(info, parent, lh, rc);
367 /* Partial request to create object only */
368 static int mdt_md_mkobj(struct mdt_thread_info *info)
370 struct mdt_device *mdt = info->mti_mdt;
371 struct mdt_object *o;
372 struct mdt_body *repbody;
373 struct md_attr *ma = &info->mti_attr;
377 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
378 PFID(info->mti_rr.rr_fid2));
380 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
382 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
384 struct md_object *next = mdt_object_child(o);
386 ma->ma_need = MA_INODE;
390 * Cross-ref create can encounter already created obj in case of
391 * recovery, just get attr in that case.
393 if (mdt_object_exists(o) == 1) {
394 rc = mo_attr_get(info->mti_env, next, ma);
397 * Here, NO permission check for object_create,
398 * such check has been done on the original MDS.
400 rc = mo_object_create(info->mti_env, next,
401 &info->mti_spec, ma);
404 /* Return fid & attr to client. */
405 if (ma->ma_valid & MA_INODE)
406 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
409 mdt_object_put(info->mti_env, o);
413 mdt_create_pack_capa(info, rc, o, repbody);
417 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
418 struct md_attr *ma, int flags)
420 struct mdt_lock_handle *lh;
421 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
422 __u64 lockpart = MDS_INODELOCK_UPDATE;
426 /* attr shouldn't be set on remote object */
427 LASSERT(mdt_object_exists(mo) >= 0);
429 lh = &info->mti_lh[MDT_LH_PARENT];
430 mdt_lock_reg_init(lh, LCK_PW);
432 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
433 lockpart |= MDS_INODELOCK_LOOKUP;
435 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
439 if (mdt_object_exists(mo) == 0)
440 GOTO(out_unlock, rc = -ENOENT);
442 /* all attrs are packed into mti_attr in unpack_setattr */
443 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
444 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
446 /* This is only for set ctime when rename's source is on remote MDS. */
447 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
448 ma->ma_attr_flags |= MDS_VTX_BYPASS;
450 /* VBR: update version if attr changed are important for recovery */
452 /* update on-disk version of changed object */
454 rc = mdt_version_get_check_save(info, mo, 0);
456 GOTO(out_unlock, rc);
459 /* all attrs are packed into mti_attr in unpack_setattr */
460 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
462 GOTO(out_unlock, rc);
466 mdt_object_unlock(info, mo, lh, rc);
470 static int mdt_reint_setattr(struct mdt_thread_info *info,
471 struct mdt_lock_handle *lhc)
473 struct md_attr *ma = &info->mti_attr;
474 struct mdt_reint_record *rr = &info->mti_rr;
475 struct ptlrpc_request *req = mdt_info_req(info);
476 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
477 struct mdt_file_data *mfd;
478 struct mdt_object *mo;
479 struct md_object *next;
480 struct mdt_body *repbody;
484 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
485 (unsigned int)ma->ma_attr.la_valid);
487 if (info->mti_dlm_req)
488 ldlm_request_cancel(req, info->mti_dlm_req, 0);
490 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
491 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
493 GOTO(out, rc = PTR_ERR(mo));
495 /* start a log jounal handle if needed */
496 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
497 if ((ma->ma_attr.la_valid & LA_SIZE) ||
498 (rr->rr_flags & MRF_OPEN_TRUNC)) {
499 /* Check write access for the O_TRUNC case */
500 if (mdt_write_read(mo) < 0)
501 GOTO(out_put, rc = -ETXTBSY);
503 } else if (info->mti_ioepoch &&
504 (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
505 /* Truncate case. IOEpoch is opened. */
506 rc = mdt_write_get(mo);
513 GOTO(out_put, rc = -ENOMEM);
516 mdt_ioepoch_open(info, mo, 0);
517 repbody->ioepoch = mo->mot_ioepoch;
519 mdt_object_get(info->mti_env, mo);
520 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
521 mfd->mfd_object = mo;
522 mfd->mfd_xid = req->rq_xid;
524 cfs_spin_lock(&med->med_open_lock);
525 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
526 cfs_spin_unlock(&med->med_open_lock);
527 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
530 som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
532 /* SOM Attribute update case. Find the proper mfd and update
533 * SOM attributes on the proper object. */
534 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
535 LASSERT(info->mti_ioepoch);
537 cfs_spin_lock(&med->med_open_lock);
538 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
540 cfs_spin_unlock(&med->med_open_lock);
541 CDEBUG(D_INODE, "no handle for file close: "
542 "fid = "DFID": cookie = "LPX64"\n",
543 PFID(info->mti_rr.rr_fid1),
544 info->mti_ioepoch->handle.cookie);
545 GOTO(out_put, rc = -ESTALE);
547 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
548 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
550 class_handle_unhash(&mfd->mfd_handle);
551 cfs_list_del_init(&mfd->mfd_list);
552 cfs_spin_unlock(&med->med_open_lock);
554 /* Close the found mfd, update attributes. */
555 ma->ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
556 OBD_ALLOC_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
557 if (ma->ma_lmm == NULL)
558 GOTO(out_put, rc = -ENOMEM);
560 mdt_mfd_close(info, mfd);
562 OBD_FREE_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
564 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
569 ma->ma_need = MA_INODE;
571 next = mdt_object_child(mo);
572 rc = mo_attr_get(info->mti_env, next, ma);
576 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
578 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
579 info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
580 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
581 (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
582 struct lustre_capa *capa;
584 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
586 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
587 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
590 repbody->valid |= OBD_MD_FLOSSCAPA;
595 mdt_object_put(info->mti_env, mo);
598 mdt_counter_incr(req->rq_export, LPROC_MDT_SETATTR);
600 mdt_client_compatibility(info);
601 mdt_shrink_reply(info);
605 static int mdt_reint_create(struct mdt_thread_info *info,
606 struct mdt_lock_handle *lhc)
608 struct ptlrpc_request *req = mdt_info_req(info);
612 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
613 RETURN(err_serious(-ESTALE));
615 if (info->mti_dlm_req)
616 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
618 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
620 /* Cross-ref case. */
621 /* TODO: we can add LPROC_MDT_CROSS for cross-ref stats */
622 if (info->mti_cross_ref) {
623 rc = mdt_md_mkobj(info);
625 LASSERT(info->mti_rr.rr_namelen > 0);
626 mdt_counter_incr(req->rq_export, LPROC_MDT_MKDIR);
627 rc = mdt_md_create(info);
637 /* Special file should stay on the same node as parent. */
638 LASSERT(info->mti_rr.rr_namelen > 0);
639 mdt_counter_incr(req->rq_export, LPROC_MDT_MKNOD);
640 rc = mdt_md_create(info);
644 rc = err_serious(-EOPNOTSUPP);
650 * VBR: save parent version in reply and child version getting by its name.
651 * Version of child is getting and checking during its lookup. If
653 static int mdt_reint_unlink(struct mdt_thread_info *info,
654 struct mdt_lock_handle *lhc)
656 struct mdt_reint_record *rr = &info->mti_rr;
657 struct ptlrpc_request *req = mdt_info_req(info);
658 struct md_attr *ma = &info->mti_attr;
659 struct lu_fid *child_fid = &info->mti_tmp_fid1;
660 struct mdt_object *mp;
661 struct mdt_object *mc;
662 struct mdt_lock_handle *parent_lh;
663 struct mdt_lock_handle *child_lh;
664 struct lu_name *lname;
668 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
671 if (info->mti_dlm_req)
672 ldlm_request_cancel(req, info->mti_dlm_req, 0);
674 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
675 RETURN(err_serious(-ENOENT));
678 * step 1: lock the parent. Note, this may be child in case of
679 * remote operation denoted by ->mti_cross_ref flag.
681 parent_lh = &info->mti_lh[MDT_LH_PARENT];
682 if (info->mti_cross_ref) {
684 * Init reg lock for cross ref case when we need to do only
687 mdt_lock_reg_init(parent_lh, LCK_PW);
689 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
692 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
693 MDS_INODELOCK_UPDATE);
696 /* errors are possible here in cross-ref cases, see below */
697 if (info->mti_cross_ref)
702 rc = mdt_version_get_check_save(info, mp, 0);
704 GOTO(out_unlock_parent, rc);
706 mdt_reint_init_ma(info, ma);
707 if (!ma->ma_lmm || !ma->ma_cookie)
708 GOTO(out_unlock_parent, rc = -EINVAL);
710 if (info->mti_cross_ref) {
712 * Remote partial operation. It is possible that replay may
713 * happen on parent MDT and this operation will be repeated.
714 * Therefore the object absense is allowed case and nothing
715 * should be done here.
717 if (mdt_object_exists(mp) > 0) {
718 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
719 rc = mo_ref_del(info->mti_env,
720 mdt_object_child(mp), ma);
722 mdt_handle_last_unlink(info, mp, ma);
725 GOTO(out_unlock_parent, rc);
728 /* step 2: find & lock the child */
729 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
730 /* lookup child object along with version checking */
731 rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
733 GOTO(out_unlock_parent, rc);
735 /* We will lock the child regardless it is local or remote. No harm. */
736 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
738 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
739 child_lh = &info->mti_lh[MDT_LH_CHILD];
740 mdt_lock_reg_init(child_lh, LCK_EX);
741 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
744 mdt_object_put(info->mti_env, mc);
745 GOTO(out_unlock_parent, rc);
748 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
749 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
750 /* save version when object is locked */
751 mdt_version_get_save(info, mc, 1);
753 * Now we can only make sure we need MA_INODE, in mdd layer, will check
754 * whether need MA_LOV and MA_COOKIE.
756 ma->ma_need = MA_INODE;
758 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
759 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
760 mdt_object_child(mc), lname, ma);
762 mdt_handle_last_unlink(info, mc, ma);
764 if (ma->ma_valid & MA_INODE) {
765 switch (ma->ma_attr.la_mode & S_IFMT) {
767 mdt_counter_incr(req->rq_export, LPROC_MDT_RMDIR);
775 mdt_counter_incr(req->rq_export, LPROC_MDT_UNLINK);
778 LASSERTF(0, "bad file type %o unlinking\n",
779 ma->ma_attr.la_mode);
785 mdt_object_unlock_put(info, mc, child_lh, rc);
787 mdt_object_unlock_put(info, mp, parent_lh, rc);
793 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
796 static int mdt_reint_link(struct mdt_thread_info *info,
797 struct mdt_lock_handle *lhc)
799 struct mdt_reint_record *rr = &info->mti_rr;
800 struct ptlrpc_request *req = mdt_info_req(info);
801 struct md_attr *ma = &info->mti_attr;
802 struct mdt_object *ms;
803 struct mdt_object *mp;
804 struct mdt_lock_handle *lhs;
805 struct mdt_lock_handle *lhp;
806 struct lu_name *lname;
810 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
811 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
813 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
814 RETURN(err_serious(-ENOENT));
816 if (info->mti_dlm_req)
817 ldlm_request_cancel(req, info->mti_dlm_req, 0);
819 if (info->mti_cross_ref) {
820 /* MDT holding name ask us to add ref. */
821 lhs = &info->mti_lh[MDT_LH_CHILD];
822 mdt_lock_reg_init(lhs, LCK_EX);
823 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
824 MDS_INODELOCK_UPDATE);
828 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
829 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
830 mdt_object_unlock_put(info, ms, lhs, rc);
834 /* Invalid case so return error immediately instead of
836 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
839 /* step 1: find & lock the target parent dir */
840 lhp = &info->mti_lh[MDT_LH_PARENT];
841 mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
843 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
844 MDS_INODELOCK_UPDATE);
848 rc = mdt_version_get_check_save(info, mp, 0);
850 GOTO(out_unlock_parent, rc);
852 /* step 2: find & lock the source */
853 lhs = &info->mti_lh[MDT_LH_CHILD];
854 mdt_lock_reg_init(lhs, LCK_EX);
856 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
858 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
860 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
863 mdt_object_put(info->mti_env, ms);
864 GOTO(out_unlock_parent, rc);
867 /* step 3: link it */
868 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
869 OBD_FAIL_MDS_REINT_LINK_WRITE);
872 rc = mdt_version_get_check_save(info, ms, 1);
874 GOTO(out_unlock_child, rc);
876 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
877 /** check target version by name during replay */
878 rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
879 if (rc != 0 && rc != -ENOENT)
880 GOTO(out_unlock_child, rc);
881 /* save version of file name for replay, it must be ENOENT here */
882 if (!req_is_replay(mdt_info_req(info))) {
883 info->mti_ver[2] = ENOENT_VERSION;
884 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
887 rc = mdo_link(info->mti_env, mdt_object_child(mp),
888 mdt_object_child(ms), lname, ma);
891 mdt_counter_incr(req->rq_export, LPROC_MDT_LINK);
895 mdt_object_unlock_put(info, ms, lhs, rc);
897 mdt_object_unlock_put(info, mp, lhp, rc);
901 * lock the part of the directory according to the hash of the name
902 * (lh->mlh_pdo_hash) in parallel directory lock.
904 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
905 struct mdt_lock_handle *lh,
906 struct mdt_object *obj, __u64 ibits)
908 struct ldlm_res_id *res_id = &info->mti_res_id;
909 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
910 ldlm_policy_data_t *policy = &info->mti_policy;
914 * Finish res_id initializing by name hash marking part of
915 * directory which is taking modification.
917 LASSERT(lh->mlh_pdo_hash != 0);
918 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
919 memset(policy, 0, sizeof(*policy));
920 policy->l_inodebits.bits = ibits;
922 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
923 * going to be sent to client. If it is - mdt_intent_policy() path will
924 * fix it up and turn FL_LOCAL flag off.
926 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
927 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
928 &info->mti_exp->exp_handle.h_cookie);
932 /* partial operation for rename */
933 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
935 struct mdt_reint_record *rr = &info->mti_rr;
936 struct ptlrpc_request *req = mdt_info_req(info);
937 struct md_attr *ma = &info->mti_attr;
938 struct mdt_object *mtgtdir;
939 struct mdt_object *mtgt = NULL;
940 struct mdt_lock_handle *lh_tgtdir;
941 struct mdt_lock_handle *lh_tgt = NULL;
942 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
943 struct lu_name *lname;
947 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
948 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
950 /* step 1: lookup & lock the tgt dir. */
951 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
952 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
954 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
955 MDS_INODELOCK_UPDATE);
957 RETURN(PTR_ERR(mtgtdir));
959 /* step 2: find & lock the target object if exists. */
960 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
961 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
962 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
963 lname, tgt_fid, &info->mti_spec);
964 if (rc != 0 && rc != -ENOENT) {
965 GOTO(out_unlock_tgtdir, rc);
966 } else if (rc == 0) {
968 * In case of replay that name can be already inserted, check
969 * that and do nothing if so.
971 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
972 GOTO(out_unlock_tgtdir, rc);
974 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
975 mdt_lock_reg_init(lh_tgt, LCK_EX);
977 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
978 MDS_INODELOCK_LOOKUP);
980 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
982 mdt_reint_init_ma(info, ma);
983 if (!ma->ma_lmm || !ma->ma_cookie)
984 GOTO(out_unlock_tgt, rc = -EINVAL);
986 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
987 mdt_object_child(mtgt), rr->rr_fid2,
989 } else /* -ENOENT */ {
990 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
991 lname, rr->rr_fid2, ma);
994 /* handle last link of tgt object */
996 mdt_handle_last_unlink(info, mtgt, ma);
1001 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
1003 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
1007 static int mdt_rename_lock(struct mdt_thread_info *info,
1008 struct lustre_handle *lh)
1010 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1011 ldlm_policy_data_t *policy = &info->mti_policy;
1012 struct ldlm_res_id *res_id = &info->mti_res_id;
1017 ms = mdt_md_site(info->mti_mdt);
1018 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1020 memset(policy, 0, sizeof *policy);
1021 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1023 if (ms->ms_control_exp == NULL) {
1024 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1027 * Current node is controller, that is mdt0, where we should
1030 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1031 LCK_EX, &flags, ldlm_blocking_ast,
1032 ldlm_completion_ast, NULL, NULL, 0,
1033 &info->mti_exp->exp_handle.h_cookie,
1036 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
1037 ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1041 * This is the case mdt0 is remote node, issue DLM lock like
1044 rc = ldlm_cli_enqueue(ms->ms_control_exp, NULL, &einfo, res_id,
1045 policy, &flags, NULL, 0, lh, 0);
1051 static void mdt_rename_unlock(struct lustre_handle *lh)
1054 LASSERT(lustre_handle_is_used(lh));
1055 ldlm_lock_decref(lh, LCK_EX);
1060 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1061 * target. Source should not be ancestor of target dir. May be other rename
1062 * checks can be moved here later.
1064 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1066 struct mdt_reint_record *rr = &info->mti_rr;
1067 struct lu_fid dst_fid = *rr->rr_fid2;
1068 struct mdt_object *dst;
1073 LASSERT(fid_is_sane(&dst_fid));
1074 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1076 rc = mdo_is_subdir(info->mti_env,
1077 mdt_object_child(dst), fid,
1079 mdt_object_put(info->mti_env, dst);
1080 if (rc != -EREMOTE && rc < 0) {
1081 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1083 /* check the found fid */
1084 if (lu_fid_eq(&dst_fid, fid))
1090 } while (rc == -EREMOTE);
1096 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1097 * 2 - src child; 3 - tgt child.
1098 * Update on disk version of src child.
1100 static int mdt_reint_rename(struct mdt_thread_info *info,
1101 struct mdt_lock_handle *lhc)
1103 struct mdt_reint_record *rr = &info->mti_rr;
1104 struct md_attr *ma = &info->mti_attr;
1105 struct ptlrpc_request *req = mdt_info_req(info);
1106 struct mdt_object *msrcdir;
1107 struct mdt_object *mtgtdir;
1108 struct mdt_object *mold;
1109 struct mdt_object *mnew = NULL;
1110 struct mdt_lock_handle *lh_srcdirp;
1111 struct mdt_lock_handle *lh_tgtdirp;
1112 struct mdt_lock_handle *lh_oldp;
1113 struct mdt_lock_handle *lh_newp;
1114 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1115 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1116 struct lustre_handle rename_lh = { 0 };
1117 struct lu_name slname = { 0 };
1118 struct lu_name *lname;
1122 if (info->mti_dlm_req)
1123 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1125 if (info->mti_cross_ref) {
1126 rc = mdt_reint_rename_tgt(info);
1130 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1131 PFID(rr->rr_fid1), rr->rr_name,
1132 PFID(rr->rr_fid2), rr->rr_tgt);
1134 rc = mdt_rename_lock(info, &rename_lh);
1136 CERROR("Can't lock FS for rename, rc %d\n", rc);
1140 lh_newp = &info->mti_lh[MDT_LH_NEW];
1142 /* step 1: lock the source dir. */
1143 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1144 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1146 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1147 MDS_INODELOCK_UPDATE);
1148 if (IS_ERR(msrcdir))
1149 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1151 rc = mdt_version_get_check_save(info, msrcdir, 0);
1153 GOTO(out_unlock_source, rc);
1155 /* step 2: find & lock the target dir. */
1156 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1157 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1159 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1160 mdt_object_get(info->mti_env, msrcdir);
1162 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1163 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1164 MDS_INODELOCK_UPDATE);
1166 GOTO(out_unlock_source, rc);
1167 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1170 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1172 if (IS_ERR(mtgtdir))
1173 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1175 /* check early, the real version will be saved after locking */
1176 rc = mdt_version_get_check(info, mtgtdir, 1);
1178 GOTO(out_put_target, rc);
1180 rc = mdt_object_exists(mtgtdir);
1182 GOTO(out_put_target, rc = -ESTALE);
1183 } else if (rc > 0) {
1184 /* we lock the target dir if it is local */
1185 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1186 MDS_INODELOCK_UPDATE,
1189 GOTO(out_put_target, rc);
1190 /* get and save correct version after locking */
1191 mdt_version_get_save(info, mtgtdir, 1);
1195 /* step 3: find & lock the old object. */
1196 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1197 mdt_name_copy(&slname, lname);
1198 rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1200 GOTO(out_unlock_target, rc);
1202 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1203 GOTO(out_unlock_target, rc = -EINVAL);
1205 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1207 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1209 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1210 mdt_lock_reg_init(lh_oldp, LCK_EX);
1211 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1214 mdt_object_put(info->mti_env, mold);
1215 GOTO(out_unlock_target, rc);
1218 info->mti_mos = mold;
1219 /* save version after locking */
1220 mdt_version_get_save(info, mold, 2);
1221 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1223 /* step 4: find & lock the new object. */
1224 /* new target object may not exist now */
1225 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1226 /* lookup with version checking */
1227 rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1229 /* the new_fid should have been filled at this moment */
1230 if (lu_fid_eq(old_fid, new_fid))
1231 GOTO(out_unlock_old, rc);
1233 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1234 lu_fid_eq(new_fid, rr->rr_fid2))
1235 GOTO(out_unlock_old, rc = -EINVAL);
1237 mdt_lock_reg_init(lh_newp, LCK_EX);
1238 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1240 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1242 rc = mdt_object_lock(info, mnew, lh_newp,
1243 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1245 mdt_object_put(info->mti_env, mnew);
1246 GOTO(out_unlock_old, rc);
1248 /* get and save version after locking */
1249 mdt_version_get_save(info, mnew, 3);
1250 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1251 } else if (rc != -EREMOTE && rc != -ENOENT) {
1252 GOTO(out_unlock_old, rc);
1254 mdt_enoent_version_save(info, 3);
1257 /* step 5: rename it */
1258 mdt_reint_init_ma(info, ma);
1259 if (!ma->ma_lmm || !ma->ma_cookie)
1260 GOTO(out_unlock_new, rc = -EINVAL);
1262 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1263 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1266 /* Check if @dst is subdir of @src. */
1267 rc = mdt_rename_sanity(info, old_fid);
1269 GOTO(out_unlock_new, rc);
1271 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1272 mdt_object_child(mtgtdir), old_fid, &slname,
1273 (mnew ? mdt_object_child(mnew) : NULL),
1276 /* handle last link of tgt object */
1278 mdt_counter_incr(req->rq_export, LPROC_MDT_RENAME);
1280 mdt_handle_last_unlink(info, mnew, ma);
1282 mdt_rename_counter_tally(info, info->mti_mdt, req->rq_export,
1289 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1291 mdt_object_unlock_put(info, mold, lh_oldp, rc);
1293 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1295 mdt_object_put(info->mti_env, mtgtdir);
1297 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1299 mdt_rename_unlock(&rename_lh);
1303 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1304 struct mdt_lock_handle *lhc);
1306 static mdt_reinter reinters[REINT_MAX] = {
1307 [REINT_SETATTR] = mdt_reint_setattr,
1308 [REINT_CREATE] = mdt_reint_create,
1309 [REINT_LINK] = mdt_reint_link,
1310 [REINT_UNLINK] = mdt_reint_unlink,
1311 [REINT_RENAME] = mdt_reint_rename,
1312 [REINT_OPEN] = mdt_reint_open,
1313 [REINT_SETXATTR] = mdt_reint_setxattr
1316 int mdt_reint_rec(struct mdt_thread_info *info,
1317 struct mdt_lock_handle *lhc)
1322 rc = reinters[info->mti_rr.rr_opcode](info, lhc);