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, Intel Corporation.
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"
51 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
54 ma->ma_need = MA_INODE;
58 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
59 struct mdt_object *object,
60 struct mdt_body *repbody)
64 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
65 * we won't go to below*/
66 if (repbody->valid & OBD_MD_FLMDSCAPA)
69 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
70 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
71 struct lustre_capa *capa;
73 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
75 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
76 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
79 repbody->valid |= OBD_MD_FLMDSCAPA;
86 * Get version of object by fid.
88 * Return real version or ENOENT_VERSION if object doesn't exist
90 static void mdt_obj_version_get(struct mdt_thread_info *info,
91 struct mdt_object *o, __u64 *version)
94 LASSERT(mdt_object_exists(o) >= 0);
95 if (mdt_object_exists(o) > 0 && !mdt_object_obf(o))
96 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
98 *version = ENOENT_VERSION;
99 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
100 PFID(mdt_object_fid(o)), *version);
104 * Check version is correct.
106 * Should be called only during replay.
108 static int mdt_version_check(struct ptlrpc_request *req,
109 __u64 version, int idx)
111 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
114 if (!exp_connect_vbr(req->rq_export))
117 LASSERT(req_is_replay(req));
118 /** VBR: version is checked always because costs nothing */
119 LASSERT(idx < PTLRPC_NUM_VERSIONS);
120 /** Sanity check for malformed buffers */
121 if (pre_ver == NULL) {
122 CERROR("No versions in request buffer\n");
123 spin_lock(&req->rq_export->exp_lock);
124 req->rq_export->exp_vbr_failed = 1;
125 spin_unlock(&req->rq_export->exp_lock);
127 } else if (pre_ver[idx] != version) {
128 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
129 pre_ver[idx], version);
130 spin_lock(&req->rq_export->exp_lock);
131 req->rq_export->exp_vbr_failed = 1;
132 spin_unlock(&req->rq_export->exp_lock);
139 * Save pre-versions in reply.
141 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
146 if (!exp_connect_vbr(req->rq_export))
149 LASSERT(!req_is_replay(req));
150 LASSERT(req->rq_repmsg != NULL);
151 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
153 reply_ver[idx] = version;
157 * Save enoent version, it is needed when it is obvious that object doesn't
158 * exist, e.g. child during create.
160 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
162 /* save version of file name for replay, it must be ENOENT here */
163 if (!req_is_replay(mdt_info_req(info))) {
164 info->mti_ver[idx] = ENOENT_VERSION;
165 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
170 * Get version from disk and save in reply buffer.
172 * Versions are saved in reply only during normal operations not replays.
174 void mdt_version_get_save(struct mdt_thread_info *info,
175 struct mdt_object *mto, int idx)
177 /* don't save versions during replay */
178 if (!req_is_replay(mdt_info_req(info))) {
179 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
180 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
185 * Get version from disk and check it, no save in reply.
187 int mdt_version_get_check(struct mdt_thread_info *info,
188 struct mdt_object *mto, int idx)
190 /* only check versions during replay */
191 if (!req_is_replay(mdt_info_req(info)))
194 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
195 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
199 * Get version from disk and check if recovery or just save.
201 int mdt_version_get_check_save(struct mdt_thread_info *info,
202 struct mdt_object *mto, int idx)
206 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
207 if (req_is_replay(mdt_info_req(info)))
208 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
211 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
216 * Lookup with version checking.
218 * This checks version of 'name'. Many reint functions uses 'name' for child not
219 * FID, therefore we need to get object by name and check its version.
221 int mdt_lookup_version_check(struct mdt_thread_info *info,
222 struct mdt_object *p, struct lu_name *lname,
223 struct lu_fid *fid, int idx)
227 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
229 /* Check version only during replay */
230 if (!req_is_replay(mdt_info_req(info)))
233 info->mti_ver[idx] = ENOENT_VERSION;
235 struct mdt_object *child;
236 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
237 if (likely(!IS_ERR(child))) {
238 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
239 mdt_object_put(info->mti_env, child);
242 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
243 return vbrc ? vbrc : rc;
248 * VBR: we save three versions in reply:
249 * 0 - parent. Check that parent version is the same during replay.
250 * 1 - name. Version of 'name' if file exists with the same name or
251 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
252 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
255 static int mdt_md_create(struct mdt_thread_info *info)
257 struct mdt_device *mdt = info->mti_mdt;
258 struct mdt_object *parent;
259 struct mdt_object *child;
260 struct mdt_lock_handle *lh;
261 struct mdt_body *repbody;
262 struct md_attr *ma = &info->mti_attr;
263 struct mdt_reint_record *rr = &info->mti_rr;
264 struct lu_name *lname;
268 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
269 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
271 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
273 lh = &info->mti_lh[MDT_LH_PARENT];
274 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
276 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
277 MDS_INODELOCK_UPDATE);
279 RETURN(PTR_ERR(parent));
281 if (mdt_object_obf(parent))
282 GOTO(out_put_parent, rc = -EPERM);
284 rc = mdt_version_get_check_save(info, parent, 0);
286 GOTO(out_put_parent, rc);
289 * Check child name version during replay.
290 * During create replay a file may exist with same name.
292 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
293 rc = mdt_lookup_version_check(info, parent, lname,
294 &info->mti_tmp_fid1, 1);
296 GOTO(out_put_parent, rc = -EEXIST);
298 /* -ENOENT is expected here */
300 GOTO(out_put_parent, rc);
302 /* save version of file name for replay, it must be ENOENT here */
303 mdt_enoent_version_save(info, 1);
305 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
306 if (likely(!IS_ERR(child))) {
307 struct md_object *next = mdt_object_child(parent);
309 if (mdt_object_exists(child) < 0) {
310 struct seq_server_site *ss;
312 ss = mdt_seq_site(mdt);
313 if (ss->ss_node_id != 0 &&
314 mdt->mdt_enable_remote_dir == 0) {
315 CERROR("%s: remote dir is only permitted on"
317 " mdt.*.enable_remote_dir=1\n",
318 mdt2obd_dev(mdt)->obd_name);
319 GOTO(out_put_child, rc = -EPERM);
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 not perform lookup sanity check. We know that name does
346 info->mti_spec.sp_cr_lookup = 0;
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 rc = mdt_attr_get_complex(info, child, ma);
356 /* Return fid & attr to client. */
357 if (ma->ma_valid & MA_INODE)
358 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
359 mdt_object_fid(child));
362 mdt_object_put(info->mti_env, child);
366 mdt_create_pack_capa(info, rc, child, repbody);
368 mdt_object_unlock_put(info, parent, lh, rc);
372 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
373 struct md_attr *ma, int flags)
375 struct mdt_lock_handle *lh;
376 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
377 __u64 lockpart = MDS_INODELOCK_UPDATE;
381 /* attr shouldn't be set on remote object */
382 LASSERT(mdt_object_exists(mo) >= 0);
384 lh = &info->mti_lh[MDT_LH_PARENT];
385 mdt_lock_reg_init(lh, LCK_PW);
387 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
388 lockpart |= MDS_INODELOCK_LOOKUP;
390 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
394 if (mdt_object_exists(mo) == 0)
395 GOTO(out_unlock, rc = -ENOENT);
397 /* all attrs are packed into mti_attr in unpack_setattr */
398 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
399 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
401 /* This is only for set ctime when rename's source is on remote MDS. */
402 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
403 ma->ma_attr_flags |= MDS_VTX_BYPASS;
405 /* VBR: update version if attr changed are important for recovery */
407 /* update on-disk version of changed object */
409 rc = mdt_version_get_check_save(info, mo, 0);
411 GOTO(out_unlock, rc);
414 /* all attrs are packed into mti_attr in unpack_setattr */
415 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
417 GOTO(out_unlock, rc);
421 mdt_object_unlock(info, mo, lh, rc);
426 * Check HSM flags and add HS_DIRTY flag if relevant.
428 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
429 * and is not RELEASED.
431 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
437 /* If the file was modified, add the dirty flag */
438 ma->ma_need = MA_HSM;
439 rc = mdt_attr_get_complex(info, mo, ma);
441 CERROR("file attribute read error for "DFID": %d.\n",
442 PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
446 /* If an up2date copy exists in the backend, add dirty flag */
447 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
448 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
450 ma->ma_hsm.mh_flags |= HS_DIRTY;
451 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
453 CERROR("file attribute change error for "DFID": %d\n",
454 PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
462 static int mdt_reint_setattr(struct mdt_thread_info *info,
463 struct mdt_lock_handle *lhc)
465 struct md_attr *ma = &info->mti_attr;
466 struct mdt_reint_record *rr = &info->mti_rr;
467 struct ptlrpc_request *req = mdt_info_req(info);
468 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
469 struct mdt_file_data *mfd;
470 struct mdt_object *mo;
471 struct mdt_body *repbody;
475 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
476 (unsigned int)ma->ma_attr.la_valid);
478 if (info->mti_dlm_req)
479 ldlm_request_cancel(req, info->mti_dlm_req, 0);
481 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
482 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
484 GOTO(out, rc = PTR_ERR(mo));
486 if (mdt_object_obf(mo))
487 GOTO(out_put, rc = -EPERM);
489 /* start a log jounal handle if needed */
490 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
491 if ((ma->ma_attr.la_valid & LA_SIZE) ||
492 (rr->rr_flags & MRF_OPEN_TRUNC)) {
493 /* Check write access for the O_TRUNC case */
494 if (mdt_write_read(mo) < 0)
495 GOTO(out_put, rc = -ETXTBSY);
497 } else if (info->mti_ioepoch &&
498 (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
499 /* Truncate case. IOEpoch is opened. */
500 rc = mdt_write_get(mo);
507 GOTO(out_put, rc = -ENOMEM);
510 mdt_ioepoch_open(info, mo, 0);
511 repbody->ioepoch = mo->mot_ioepoch;
513 mdt_object_get(info->mti_env, mo);
514 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
515 mfd->mfd_object = mo;
516 mfd->mfd_xid = req->rq_xid;
518 spin_lock(&med->med_open_lock);
519 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
520 spin_unlock(&med->med_open_lock);
521 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
524 som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
526 /* SOM Attribute update case. Find the proper mfd and update
527 * SOM attributes on the proper object. */
528 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
529 LASSERT(info->mti_ioepoch);
531 spin_lock(&med->med_open_lock);
532 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
534 spin_unlock(&med->med_open_lock);
535 CDEBUG(D_INODE, "no handle for file close: "
536 "fid = "DFID": cookie = "LPX64"\n",
537 PFID(info->mti_rr.rr_fid1),
538 info->mti_ioepoch->handle.cookie);
539 GOTO(out_put, rc = -ESTALE);
541 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
542 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
544 class_handle_unhash(&mfd->mfd_handle);
545 cfs_list_del_init(&mfd->mfd_list);
546 spin_unlock(&med->med_open_lock);
548 mdt_mfd_close(info, mfd);
549 } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
550 LASSERT((ma->ma_valid & MA_LOV) == 0);
551 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
554 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
555 struct lu_buf *buf = &info->mti_buf;
556 LASSERT(ma->ma_attr.la_valid == 0);
557 buf->lb_buf = ma->ma_lmm;
558 buf->lb_len = ma->ma_lmm_size;
559 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
560 buf, XATTR_NAME_LOV, 0);
566 /* If file data is modified, add the dirty flag */
567 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
568 rc = mdt_add_dirty_flag(info, mo, ma);
570 ma->ma_need = MA_INODE;
572 rc = mdt_attr_get_complex(info, mo, 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, LPROC_MDT_SETATTR);
600 mdt_client_compatibility(info);
601 rc2 = mdt_fix_reply(info);
607 static int mdt_reint_create(struct mdt_thread_info *info,
608 struct mdt_lock_handle *lhc)
610 struct ptlrpc_request *req = mdt_info_req(info);
614 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
615 RETURN(err_serious(-ESTALE));
617 if (info->mti_dlm_req)
618 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
620 LASSERT(info->mti_rr.rr_namelen > 0);
621 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
623 mdt_counter_incr(req, LPROC_MDT_MKDIR);
631 /* Special file should stay on the same node as parent. */
632 mdt_counter_incr(req, LPROC_MDT_MKNOD);
635 CERROR("%s: Unsupported mode %o\n",
636 mdt2obd_dev(info->mti_mdt)->obd_name,
637 info->mti_attr.ma_attr.la_mode);
638 RETURN(err_serious(-EOPNOTSUPP));
641 rc = mdt_md_create(info);
646 * VBR: save parent version in reply and child version getting by its name.
647 * Version of child is getting and checking during its lookup. If
649 static int mdt_reint_unlink(struct mdt_thread_info *info,
650 struct mdt_lock_handle *lhc)
652 struct mdt_reint_record *rr = &info->mti_rr;
653 struct ptlrpc_request *req = mdt_info_req(info);
654 struct md_attr *ma = &info->mti_attr;
655 struct lu_fid *child_fid = &info->mti_tmp_fid1;
656 struct mdt_object *mp;
657 struct mdt_object *mc;
658 struct mdt_lock_handle *parent_lh;
659 struct mdt_lock_handle *child_lh;
660 struct lu_name *lname;
664 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
667 if (info->mti_dlm_req)
668 ldlm_request_cancel(req, info->mti_dlm_req, 0);
670 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
671 RETURN(err_serious(-ENOENT));
674 * step 1: lock the parent.
676 parent_lh = &info->mti_lh[MDT_LH_PARENT];
677 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
680 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
681 MDS_INODELOCK_UPDATE);
683 GOTO(out, rc = PTR_ERR(mp));
685 if (mdt_object_obf(mp))
686 GOTO(out_unlock_parent, rc = -EPERM);
688 rc = mdt_version_get_check_save(info, mp, 0);
690 GOTO(out_unlock_parent, rc);
692 mdt_reint_init_ma(info, ma);
694 /* step 2: find & lock the child */
695 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
696 /* lookup child object along with version checking */
698 rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
700 GOTO(out_unlock_parent, rc);
702 /* We will lock the child regardless it is local or remote. No harm. */
703 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
705 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
706 child_lh = &info->mti_lh[MDT_LH_CHILD];
707 mdt_lock_reg_init(child_lh, LCK_EX);
708 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
711 mdt_object_put(info->mti_env, mc);
712 GOTO(out_unlock_parent, rc);
715 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
716 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
717 /* save version when object is locked */
718 mdt_version_get_save(info, mc, 1);
720 * Now we can only make sure we need MA_INODE, in mdd layer, will check
721 * whether need MA_LOV and MA_COOKIE.
723 ma->ma_need = MA_INODE;
725 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
726 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
727 mdt_object_child(mc), lname, ma);
728 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
729 rc = mdt_attr_get_complex(info, mc, ma);
731 mdt_handle_last_unlink(info, mc, ma);
733 if (ma->ma_valid & MA_INODE) {
734 switch (ma->ma_attr.la_mode & S_IFMT) {
736 mdt_counter_incr(req, LPROC_MDT_RMDIR);
744 mdt_counter_incr(req, LPROC_MDT_UNLINK);
747 LASSERTF(0, "bad file type %o unlinking\n",
748 ma->ma_attr.la_mode);
754 mdt_object_unlock_put(info, mc, child_lh, rc);
756 mdt_object_unlock_put(info, mp, parent_lh, rc);
762 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
765 static int mdt_reint_link(struct mdt_thread_info *info,
766 struct mdt_lock_handle *lhc)
768 struct mdt_reint_record *rr = &info->mti_rr;
769 struct ptlrpc_request *req = mdt_info_req(info);
770 struct md_attr *ma = &info->mti_attr;
771 struct mdt_object *ms;
772 struct mdt_object *mp;
773 struct mdt_lock_handle *lhs;
774 struct mdt_lock_handle *lhp;
775 struct lu_name *lname;
779 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
780 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
782 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
783 RETURN(err_serious(-ENOENT));
785 if (info->mti_dlm_req)
786 ldlm_request_cancel(req, info->mti_dlm_req, 0);
788 /* Invalid case so return error immediately instead of
790 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
793 /* step 1: find & lock the target parent dir */
794 lhp = &info->mti_lh[MDT_LH_PARENT];
795 mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
797 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
798 MDS_INODELOCK_UPDATE);
802 if (mdt_object_obf(mp))
803 GOTO(out_unlock_parent, rc = -EPERM);
805 rc = mdt_version_get_check_save(info, mp, 0);
807 GOTO(out_unlock_parent, rc);
809 /* step 2: find & lock the source */
810 lhs = &info->mti_lh[MDT_LH_CHILD];
811 mdt_lock_reg_init(lhs, LCK_EX);
813 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
815 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
817 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
820 mdt_object_put(info->mti_env, ms);
821 GOTO(out_unlock_parent, rc);
824 /* step 3: link it */
825 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
826 OBD_FAIL_MDS_REINT_LINK_WRITE);
829 rc = mdt_version_get_check_save(info, ms, 1);
831 GOTO(out_unlock_child, rc);
833 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
834 /** check target version by name during replay */
835 rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
836 if (rc != 0 && rc != -ENOENT)
837 GOTO(out_unlock_child, rc);
838 /* save version of file name for replay, it must be ENOENT here */
839 if (!req_is_replay(mdt_info_req(info))) {
840 info->mti_ver[2] = ENOENT_VERSION;
841 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
844 rc = mdo_link(info->mti_env, mdt_object_child(mp),
845 mdt_object_child(ms), lname, ma);
848 mdt_counter_incr(req, LPROC_MDT_LINK);
852 mdt_object_unlock_put(info, ms, lhs, rc);
854 mdt_object_unlock_put(info, mp, lhp, rc);
858 * lock the part of the directory according to the hash of the name
859 * (lh->mlh_pdo_hash) in parallel directory lock.
861 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
862 struct mdt_lock_handle *lh,
863 struct mdt_object *obj, __u64 ibits)
865 struct ldlm_res_id *res_id = &info->mti_res_id;
866 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
867 ldlm_policy_data_t *policy = &info->mti_policy;
871 * Finish res_id initializing by name hash marking part of
872 * directory which is taking modification.
874 LASSERT(lh->mlh_pdo_hash != 0);
875 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
876 memset(policy, 0, sizeof(*policy));
877 policy->l_inodebits.bits = ibits;
879 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
880 * going to be sent to client. If it is - mdt_intent_policy() path will
881 * fix it up and turn FL_LOCAL flag off.
883 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
884 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
885 &info->mti_exp->exp_handle.h_cookie);
889 static int mdt_rename_lock(struct mdt_thread_info *info,
890 struct lustre_handle *lh)
892 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
893 ldlm_policy_data_t *policy = &info->mti_policy;
894 struct ldlm_res_id *res_id = &info->mti_res_id;
899 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
901 memset(policy, 0, sizeof *policy);
902 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
903 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 4, 53, 0)
904 /* In phase I, we will not do cross-rename, so local BFL lock would
907 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
909 * Current node is controller, that is mdt0, where we should
912 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
913 LCK_EX, &flags, ldlm_blocking_ast,
914 ldlm_completion_ast, NULL, NULL, 0,
916 &info->mti_exp->exp_handle.h_cookie,
919 #warning "Local rename lock is invalid for DNE phase II."
924 static void mdt_rename_unlock(struct lustre_handle *lh)
927 LASSERT(lustre_handle_is_used(lh));
928 ldlm_lock_decref(lh, LCK_EX);
933 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
934 * target. Source should not be ancestor of target dir. May be other rename
935 * checks can be moved here later.
937 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
939 struct mdt_reint_record *rr = &info->mti_rr;
940 struct lu_fid dst_fid = *rr->rr_fid2;
941 struct mdt_object *dst;
946 LASSERT(fid_is_sane(&dst_fid));
947 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
949 rc = mdo_is_subdir(info->mti_env,
950 mdt_object_child(dst), fid,
952 mdt_object_put(info->mti_env, dst);
953 if (rc != -EREMOTE && rc < 0) {
954 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
956 /* check the found fid */
957 if (lu_fid_eq(&dst_fid, fid))
963 } while (rc == -EREMOTE);
969 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
970 * 2 - src child; 3 - tgt child.
971 * Update on disk version of src child.
973 static int mdt_reint_rename(struct mdt_thread_info *info,
974 struct mdt_lock_handle *lhc)
976 struct mdt_reint_record *rr = &info->mti_rr;
977 struct md_attr *ma = &info->mti_attr;
978 struct ptlrpc_request *req = mdt_info_req(info);
979 struct mdt_object *msrcdir;
980 struct mdt_object *mtgtdir;
981 struct mdt_object *mold;
982 struct mdt_object *mnew = NULL;
983 struct mdt_lock_handle *lh_srcdirp;
984 struct mdt_lock_handle *lh_tgtdirp;
985 struct mdt_lock_handle *lh_oldp;
986 struct mdt_lock_handle *lh_newp;
987 struct lu_fid *old_fid = &info->mti_tmp_fid1;
988 struct lu_fid *new_fid = &info->mti_tmp_fid2;
989 struct lustre_handle rename_lh = { 0 };
990 struct lu_name slname = { 0 };
991 struct lu_name *lname;
995 if (info->mti_dlm_req)
996 ldlm_request_cancel(req, info->mti_dlm_req, 0);
998 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
999 PFID(rr->rr_fid1), rr->rr_name,
1000 PFID(rr->rr_fid2), rr->rr_tgt);
1002 rc = mdt_rename_lock(info, &rename_lh);
1004 CERROR("Can't lock FS for rename, rc %d\n", rc);
1008 lh_newp = &info->mti_lh[MDT_LH_NEW];
1010 /* step 1: lock the source dir. */
1011 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1012 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1014 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1015 MDS_INODELOCK_UPDATE);
1016 if (IS_ERR(msrcdir))
1017 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1019 if (mdt_object_obf(msrcdir))
1020 GOTO(out_unlock_source, rc = -EPERM);
1022 rc = mdt_version_get_check_save(info, msrcdir, 0);
1024 GOTO(out_unlock_source, rc);
1026 /* step 2: find & lock the target dir. */
1027 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1028 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1030 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1031 mdt_object_get(info->mti_env, msrcdir);
1033 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1034 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1035 MDS_INODELOCK_UPDATE);
1037 GOTO(out_unlock_source, rc);
1038 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1041 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1043 if (IS_ERR(mtgtdir))
1044 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1046 if (mdt_object_obf(mtgtdir))
1047 GOTO(out_put_target, rc = -EPERM);
1049 /* check early, the real version will be saved after locking */
1050 rc = mdt_version_get_check(info, mtgtdir, 1);
1052 GOTO(out_put_target, rc);
1054 rc = mdt_object_exists(mtgtdir);
1056 GOTO(out_put_target, rc = -ESTALE);
1057 } else if (rc > 0) {
1058 /* we lock the target dir if it is local */
1059 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1060 MDS_INODELOCK_UPDATE,
1063 GOTO(out_put_target, rc);
1064 /* get and save correct version after locking */
1065 mdt_version_get_save(info, mtgtdir, 1);
1069 /* step 3: find & lock the old object. */
1070 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1071 mdt_name_copy(&slname, lname);
1073 rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1075 GOTO(out_unlock_target, rc);
1077 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1078 GOTO(out_unlock_target, rc = -EINVAL);
1080 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1082 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1084 if (mdt_object_obf(mold)) {
1085 mdt_object_put(info->mti_env, mold);
1086 GOTO(out_unlock_target, rc = -EPERM);
1089 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1090 mdt_lock_reg_init(lh_oldp, LCK_EX);
1091 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1094 mdt_object_put(info->mti_env, mold);
1095 GOTO(out_unlock_target, rc);
1098 info->mti_mos = mold;
1099 /* save version after locking */
1100 mdt_version_get_save(info, mold, 2);
1101 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1103 /* step 4: find & lock the new object. */
1104 /* new target object may not exist now */
1105 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1106 /* lookup with version checking */
1108 rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1110 /* the new_fid should have been filled at this moment */
1111 if (lu_fid_eq(old_fid, new_fid))
1112 GOTO(out_unlock_old, rc);
1114 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1115 lu_fid_eq(new_fid, rr->rr_fid2))
1116 GOTO(out_unlock_old, rc = -EINVAL);
1118 mdt_lock_reg_init(lh_newp, LCK_EX);
1119 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1121 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1123 if (mdt_object_obf(mnew)) {
1124 mdt_object_put(info->mti_env, mnew);
1125 GOTO(out_unlock_old, rc = -EPERM);
1128 rc = mdt_object_lock(info, mnew, lh_newp,
1129 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1131 mdt_object_put(info->mti_env, mnew);
1132 GOTO(out_unlock_old, rc);
1134 /* get and save version after locking */
1135 mdt_version_get_save(info, mnew, 3);
1136 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1137 } else if (rc != -EREMOTE && rc != -ENOENT) {
1138 GOTO(out_unlock_old, rc);
1140 mdt_enoent_version_save(info, 3);
1143 /* step 5: rename it */
1144 mdt_reint_init_ma(info, ma);
1146 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1147 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1150 /* Check if @dst is subdir of @src. */
1151 rc = mdt_rename_sanity(info, old_fid);
1153 GOTO(out_unlock_new, rc);
1155 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1156 mdt_object_child(mtgtdir), old_fid, &slname,
1157 (mnew ? mdt_object_child(mnew) : NULL),
1160 /* handle last link of tgt object */
1162 mdt_counter_incr(req, LPROC_MDT_RENAME);
1164 mdt_handle_last_unlink(info, mnew, ma);
1166 mdt_rename_counter_tally(info, info->mti_mdt, req,
1173 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1175 mdt_object_unlock_put(info, mold, lh_oldp, rc);
1177 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1179 mdt_object_put(info->mti_env, mtgtdir);
1181 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1183 if (lustre_handle_is_used(&rename_lh))
1184 mdt_rename_unlock(&rename_lh);
1188 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1189 struct mdt_lock_handle *lhc);
1191 static mdt_reinter reinters[REINT_MAX] = {
1192 [REINT_SETATTR] = mdt_reint_setattr,
1193 [REINT_CREATE] = mdt_reint_create,
1194 [REINT_LINK] = mdt_reint_link,
1195 [REINT_UNLINK] = mdt_reint_unlink,
1196 [REINT_RENAME] = mdt_reint_rename,
1197 [REINT_OPEN] = mdt_reint_open,
1198 [REINT_SETXATTR] = mdt_reint_setxattr
1201 int mdt_reint_rec(struct mdt_thread_info *info,
1202 struct mdt_lock_handle *lhc)
1207 rc = reinters[info->mti_rr.rr_opcode](info, lhc);