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 exp_connect_flags(info->mti_exp) & 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 if (mdt_object_exists(o) && !mdt_object_remote(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_remote(child)) {
310 struct seq_server_site *ss;
311 struct lu_ucred *uc = mdt_ucred(info);
313 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
314 CERROR("%s: Creating remote dir is only "
315 "permitted for administrator: rc = %d\n",
316 mdt2obd_dev(mdt)->obd_name, -EPERM);
317 GOTO(out_put_child, rc = -EPERM);
320 ss = mdt_seq_site(mdt);
321 if (ss->ss_node_id != 0 &&
322 mdt->mdt_enable_remote_dir == 0) {
323 CERROR("%s: remote dir is only permitted on"
325 " mdt.*.enable_remote_dir=1\n",
326 mdt2obd_dev(mdt)->obd_name);
327 GOTO(out_put_child, rc = -EPERM);
330 ma->ma_need = MA_INODE;
332 /* capa for cross-ref will be stored here */
333 ma->ma_capa = req_capsule_server_get(info->mti_pill,
335 LASSERT(ma->ma_capa);
337 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
338 OBD_FAIL_MDS_REINT_CREATE_WRITE);
340 /* Version of child will be updated on disk. */
341 info->mti_mos = child;
342 rc = mdt_version_get_check_save(info, child, 2);
344 GOTO(out_put_child, rc);
346 /* Let lower layer know current lock mode. */
347 info->mti_spec.sp_cr_mode =
348 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
351 * Do not perform lookup sanity check. We know that name does
354 info->mti_spec.sp_cr_lookup = 0;
355 info->mti_spec.sp_feat = &dt_directory_features;
357 rc = mdo_create(info->mti_env, next, lname,
358 mdt_object_child(child),
359 &info->mti_spec, ma);
361 rc = mdt_attr_get_complex(info, child, ma);
364 /* Return fid & attr to client. */
365 if (ma->ma_valid & MA_INODE)
366 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
367 mdt_object_fid(child));
370 mdt_object_put(info->mti_env, child);
374 mdt_create_pack_capa(info, rc, child, repbody);
376 mdt_object_unlock_put(info, parent, lh, rc);
380 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
381 struct md_attr *ma, int flags)
383 struct mdt_lock_handle *lh;
384 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
385 __u64 lockpart = MDS_INODELOCK_UPDATE;
389 /* attr shouldn't be set on remote object */
390 LASSERT(!mdt_object_remote(mo));
392 lh = &info->mti_lh[MDT_LH_PARENT];
393 mdt_lock_reg_init(lh, LCK_PW);
395 /* Even though the new MDT will grant PERM lock to the old
396 * client, but the old client will almost ignore that during
397 * So it needs to revoke both LOOKUP and PERM lock here, so
398 * both new and old client can cancel the dcache */
399 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
400 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
402 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
406 if (mdt_object_exists(mo) == 0)
407 GOTO(out_unlock, rc = -ENOENT);
409 /* all attrs are packed into mti_attr in unpack_setattr */
410 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
411 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
413 /* This is only for set ctime when rename's source is on remote MDS. */
414 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
415 ma->ma_attr_flags |= MDS_VTX_BYPASS;
417 /* VBR: update version if attr changed are important for recovery */
419 /* update on-disk version of changed object */
421 rc = mdt_version_get_check_save(info, mo, 0);
423 GOTO(out_unlock, rc);
426 /* all attrs are packed into mti_attr in unpack_setattr */
427 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
429 GOTO(out_unlock, rc);
433 mdt_object_unlock(info, mo, lh, rc);
438 * Check HSM flags and add HS_DIRTY flag if relevant.
440 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
441 * and is not RELEASED.
443 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
449 /* If the file was modified, add the dirty flag */
450 ma->ma_need = MA_HSM;
451 rc = mdt_attr_get_complex(info, mo, ma);
453 CERROR("file attribute read error for "DFID": %d.\n",
454 PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
458 /* If an up2date copy exists in the backend, add dirty flag */
459 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
460 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
462 ma->ma_hsm.mh_flags |= HS_DIRTY;
463 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
465 CERROR("file attribute change error for "DFID": %d\n",
466 PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
474 static int mdt_reint_setattr(struct mdt_thread_info *info,
475 struct mdt_lock_handle *lhc)
477 struct md_attr *ma = &info->mti_attr;
478 struct mdt_reint_record *rr = &info->mti_rr;
479 struct ptlrpc_request *req = mdt_info_req(info);
480 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
481 struct mdt_file_data *mfd;
482 struct mdt_object *mo;
483 struct mdt_body *repbody;
487 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
488 (unsigned int)ma->ma_attr.la_valid);
490 if (info->mti_dlm_req)
491 ldlm_request_cancel(req, info->mti_dlm_req, 0);
493 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
494 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
496 GOTO(out, rc = PTR_ERR(mo));
498 if (mdt_object_obf(mo))
499 GOTO(out_put, rc = -EPERM);
501 /* start a log jounal handle if needed */
502 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
503 if ((ma->ma_attr.la_valid & LA_SIZE) ||
504 (rr->rr_flags & MRF_OPEN_TRUNC)) {
505 /* Check write access for the O_TRUNC case */
506 if (mdt_write_read(mo) < 0)
507 GOTO(out_put, rc = -ETXTBSY);
509 } else if (info->mti_ioepoch &&
510 (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
511 /* Truncate case. IOEpoch is opened. */
512 rc = mdt_write_get(mo);
519 GOTO(out_put, rc = -ENOMEM);
522 mdt_ioepoch_open(info, mo, 0);
523 repbody->ioepoch = mo->mot_ioepoch;
525 mdt_object_get(info->mti_env, mo);
526 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
527 mfd->mfd_object = mo;
528 mfd->mfd_xid = req->rq_xid;
530 spin_lock(&med->med_open_lock);
531 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
532 spin_unlock(&med->med_open_lock);
533 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
536 som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
538 /* SOM Attribute update case. Find the proper mfd and update
539 * SOM attributes on the proper object. */
540 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
541 LASSERT(info->mti_ioepoch);
543 spin_lock(&med->med_open_lock);
544 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
546 spin_unlock(&med->med_open_lock);
547 CDEBUG(D_INODE, "no handle for file close: "
548 "fid = "DFID": cookie = "LPX64"\n",
549 PFID(info->mti_rr.rr_fid1),
550 info->mti_ioepoch->handle.cookie);
551 GOTO(out_put, rc = -ESTALE);
553 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
554 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
556 class_handle_unhash(&mfd->mfd_handle);
557 cfs_list_del_init(&mfd->mfd_list);
558 spin_unlock(&med->med_open_lock);
560 mdt_mfd_close(info, mfd);
561 } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
562 LASSERT((ma->ma_valid & MA_LOV) == 0);
563 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
566 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
567 struct lu_buf *buf = &info->mti_buf;
568 LASSERT(ma->ma_attr.la_valid == 0);
569 buf->lb_buf = ma->ma_lmm;
570 buf->lb_len = ma->ma_lmm_size;
571 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
572 buf, XATTR_NAME_LOV, 0);
578 /* If file data is modified, add the dirty flag */
579 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
580 rc = mdt_add_dirty_flag(info, mo, ma);
582 ma->ma_need = MA_INODE;
584 rc = mdt_attr_get_complex(info, mo, ma);
588 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
590 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
591 exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
592 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
593 (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
594 struct lustre_capa *capa;
596 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
598 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
599 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
602 repbody->valid |= OBD_MD_FLOSSCAPA;
607 mdt_object_put(info->mti_env, mo);
610 mdt_counter_incr(req, LPROC_MDT_SETATTR);
612 mdt_client_compatibility(info);
613 rc2 = mdt_fix_reply(info);
619 static int mdt_reint_create(struct mdt_thread_info *info,
620 struct mdt_lock_handle *lhc)
622 struct ptlrpc_request *req = mdt_info_req(info);
626 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
627 RETURN(err_serious(-ESTALE));
629 if (info->mti_dlm_req)
630 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
632 LASSERT(info->mti_rr.rr_namelen > 0);
633 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
635 mdt_counter_incr(req, LPROC_MDT_MKDIR);
643 /* Special file should stay on the same node as parent. */
644 mdt_counter_incr(req, LPROC_MDT_MKNOD);
647 CERROR("%s: Unsupported mode %o\n",
648 mdt2obd_dev(info->mti_mdt)->obd_name,
649 info->mti_attr.ma_attr.la_mode);
650 RETURN(err_serious(-EOPNOTSUPP));
653 rc = mdt_md_create(info);
658 * VBR: save parent version in reply and child version getting by its name.
659 * Version of child is getting and checking during its lookup. If
661 static int mdt_reint_unlink(struct mdt_thread_info *info,
662 struct mdt_lock_handle *lhc)
664 struct mdt_reint_record *rr = &info->mti_rr;
665 struct ptlrpc_request *req = mdt_info_req(info);
666 struct md_attr *ma = &info->mti_attr;
667 struct lu_fid *child_fid = &info->mti_tmp_fid1;
668 struct mdt_object *mp;
669 struct mdt_object *mc;
670 struct mdt_lock_handle *parent_lh;
671 struct mdt_lock_handle *child_lh;
672 struct lu_name *lname;
676 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
679 if (info->mti_dlm_req)
680 ldlm_request_cancel(req, info->mti_dlm_req, 0);
682 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
683 RETURN(err_serious(-ENOENT));
686 * step 1: Found the parent.
688 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
694 if (mdt_object_obf(mp))
695 GOTO(put_parent, rc = -EPERM);
697 parent_lh = &info->mti_lh[MDT_LH_PARENT];
698 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
699 if (mdt_object_remote(mp)) {
700 mdt_lock_reg_init(parent_lh, LCK_EX);
701 rc = mdt_remote_object_lock(info, mp, &parent_lh->mlh_rreg_lh,
702 parent_lh->mlh_rreg_mode,
703 MDS_INODELOCK_UPDATE);
705 GOTO(put_parent, rc);
708 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
710 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
713 GOTO(put_parent, rc);
715 rc = mdt_version_get_check_save(info, mp, 0);
717 GOTO(unlock_parent, rc);
720 /* step 2: find & lock the child */
721 /* lookup child object along with version checking */
723 rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
725 GOTO(unlock_parent, rc);
727 mdt_reint_init_ma(info, ma);
729 /* We will lock the child regardless it is local or remote. No harm. */
730 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
732 GOTO(unlock_parent, rc = PTR_ERR(mc));
734 child_lh = &info->mti_lh[MDT_LH_CHILD];
735 mdt_lock_reg_init(child_lh, LCK_EX);
736 if (mdt_object_remote(mc)) {
737 struct mdt_body *repbody;
739 if (!fid_is_zero(rr->rr_fid2)) {
740 CDEBUG(D_INFO, "%s: name %s can not find "DFID"\n",
741 mdt2obd_dev(info->mti_mdt)->obd_name,
742 (char *)rr->rr_name, PFID(mdt_object_fid(mc)));
743 GOTO(unlock_parent, rc = -ENOENT);
745 CDEBUG(D_INFO, "%s: name %s: "DFID" is another MDT\n",
746 mdt2obd_dev(info->mti_mdt)->obd_name,
747 (char *)rr->rr_name, PFID(mdt_object_fid(mc)));
749 if (info->mti_spec.sp_rm_entry) {
750 struct lu_ucred *uc = mdt_ucred(info);
752 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
753 CERROR("%s: unlink remote entry is only "
754 "permitted for administrator: rc = %d\n",
755 mdt2obd_dev(info->mti_mdt)->obd_name,
757 GOTO(unlock_parent, rc = -EPERM);
760 ma->ma_need = MA_INODE;
762 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
763 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
765 mdt_object_put(info->mti_env, mc);
766 GOTO(unlock_parent, rc);
768 /* Revoke the LOOKUP lock of the remote object granted by
769 * this MDT. Since the unlink will happen on another MDT,
770 * it will release the LOOKUP lock right away. Then What
771 * would happen if another client try to grab the LOOKUP
772 * lock at the same time with unlink XXX */
773 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
775 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
776 LASSERT(repbody != NULL);
777 repbody->fid1 = *mdt_object_fid(mc);
778 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
779 mdt_object_unlock_put(info, mc, child_lh, rc);
780 GOTO(unlock_parent, rc = -EREMOTE);
781 } else if (info->mti_spec.sp_rm_entry) {
782 CERROR("%s: lfs rmdir should not be used on local dir %s\n",
783 mdt2obd_dev(info->mti_mdt)->obd_name,
784 (char *)rr->rr_name);
785 mdt_object_put(info->mti_env, mc);
786 GOTO(unlock_parent, rc = -EPERM);
789 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
792 mdt_object_put(info->mti_env, mc);
793 GOTO(unlock_parent, rc);
796 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
797 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
798 /* save version when object is locked */
799 mdt_version_get_save(info, mc, 1);
801 * Now we can only make sure we need MA_INODE, in mdd layer, will check
802 * whether need MA_LOV and MA_COOKIE.
804 ma->ma_need = MA_INODE;
806 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
807 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
808 mdt_object_child(mc), lname, ma);
809 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
810 rc = mdt_attr_get_complex(info, mc, ma);
812 mdt_handle_last_unlink(info, mc, ma);
814 if (ma->ma_valid & MA_INODE) {
815 switch (ma->ma_attr.la_mode & S_IFMT) {
817 mdt_counter_incr(req, LPROC_MDT_RMDIR);
825 mdt_counter_incr(req, LPROC_MDT_UNLINK);
828 LASSERTF(0, "bad file type %o unlinking\n",
829 ma->ma_attr.la_mode);
835 mdt_object_unlock_put(info, mc, child_lh, rc);
837 mdt_object_unlock(info, mp, parent_lh, rc);
839 mdt_object_put(info->mti_env, mp);
845 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
848 static int mdt_reint_link(struct mdt_thread_info *info,
849 struct mdt_lock_handle *lhc)
851 struct mdt_reint_record *rr = &info->mti_rr;
852 struct ptlrpc_request *req = mdt_info_req(info);
853 struct md_attr *ma = &info->mti_attr;
854 struct mdt_object *ms;
855 struct mdt_object *mp;
856 struct mdt_lock_handle *lhs;
857 struct mdt_lock_handle *lhp;
858 struct lu_name *lname;
862 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
863 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
865 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
866 RETURN(err_serious(-ENOENT));
868 if (info->mti_dlm_req)
869 ldlm_request_cancel(req, info->mti_dlm_req, 0);
871 /* Invalid case so return error immediately instead of
873 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
876 /* step 1: find & lock the target parent dir */
877 lhp = &info->mti_lh[MDT_LH_PARENT];
878 mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
880 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
881 MDS_INODELOCK_UPDATE);
885 if (mdt_object_obf(mp))
886 GOTO(out_unlock_parent, rc = -EPERM);
888 rc = mdt_version_get_check_save(info, mp, 0);
890 GOTO(out_unlock_parent, rc);
892 /* step 2: find & lock the source */
893 lhs = &info->mti_lh[MDT_LH_CHILD];
894 mdt_lock_reg_init(lhs, LCK_EX);
896 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
898 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
900 if (mdt_object_remote(ms)) {
901 mdt_object_put(info->mti_env, ms);
902 CERROR("Target directory "DFID" is on another MDT\n",
904 GOTO(out_unlock_parent, rc = -EXDEV);
907 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
910 mdt_object_put(info->mti_env, ms);
911 GOTO(out_unlock_parent, rc);
914 /* step 3: link it */
915 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
916 OBD_FAIL_MDS_REINT_LINK_WRITE);
919 rc = mdt_version_get_check_save(info, ms, 1);
921 GOTO(out_unlock_child, rc);
923 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
924 /** check target version by name during replay */
925 rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
926 if (rc != 0 && rc != -ENOENT)
927 GOTO(out_unlock_child, rc);
928 /* save version of file name for replay, it must be ENOENT here */
929 if (!req_is_replay(mdt_info_req(info))) {
930 info->mti_ver[2] = ENOENT_VERSION;
931 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
934 rc = mdo_link(info->mti_env, mdt_object_child(mp),
935 mdt_object_child(ms), lname, ma);
938 mdt_counter_incr(req, LPROC_MDT_LINK);
942 mdt_object_unlock_put(info, ms, lhs, rc);
944 mdt_object_unlock_put(info, mp, lhp, rc);
948 * lock the part of the directory according to the hash of the name
949 * (lh->mlh_pdo_hash) in parallel directory lock.
951 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
952 struct mdt_lock_handle *lh,
953 struct mdt_object *obj, __u64 ibits)
955 struct ldlm_res_id *res_id = &info->mti_res_id;
956 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
957 ldlm_policy_data_t *policy = &info->mti_policy;
961 * Finish res_id initializing by name hash marking part of
962 * directory which is taking modification.
964 LASSERT(lh->mlh_pdo_hash != 0);
965 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
966 memset(policy, 0, sizeof(*policy));
967 policy->l_inodebits.bits = ibits;
969 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
970 * going to be sent to client. If it is - mdt_intent_policy() path will
971 * fix it up and turn FL_LOCAL flag off.
973 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
974 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
975 &info->mti_exp->exp_handle.h_cookie);
979 static int mdt_rename_lock(struct mdt_thread_info *info,
980 struct lustre_handle *lh)
982 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
983 ldlm_policy_data_t *policy = &info->mti_policy;
984 struct ldlm_res_id *res_id = &info->mti_res_id;
989 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
991 memset(policy, 0, sizeof *policy);
992 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
993 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 4, 53, 0)
994 /* In phase I, we will not do cross-rename, so local BFL lock would
997 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
999 * Current node is controller, that is mdt0, where we should
1002 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1003 LCK_EX, &flags, ldlm_blocking_ast,
1004 ldlm_completion_ast, NULL, NULL, 0,
1006 &info->mti_exp->exp_handle.h_cookie,
1009 #warning "Local rename lock is invalid for DNE phase II."
1014 static void mdt_rename_unlock(struct lustre_handle *lh)
1017 LASSERT(lustre_handle_is_used(lh));
1018 ldlm_lock_decref(lh, LCK_EX);
1023 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1024 * target. Source should not be ancestor of target dir. May be other rename
1025 * checks can be moved here later.
1027 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1029 struct mdt_reint_record *rr = &info->mti_rr;
1030 struct lu_fid dst_fid = *rr->rr_fid2;
1031 struct mdt_object *dst;
1036 LASSERT(fid_is_sane(&dst_fid));
1037 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1039 rc = mdo_is_subdir(info->mti_env,
1040 mdt_object_child(dst), fid,
1042 mdt_object_put(info->mti_env, dst);
1043 if (rc != -EREMOTE && rc < 0) {
1044 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1046 /* check the found fid */
1047 if (lu_fid_eq(&dst_fid, fid))
1053 } while (rc == -EREMOTE);
1059 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1060 * 2 - src child; 3 - tgt child.
1061 * Update on disk version of src child.
1063 static int mdt_reint_rename(struct mdt_thread_info *info,
1064 struct mdt_lock_handle *lhc)
1066 struct mdt_reint_record *rr = &info->mti_rr;
1067 struct md_attr *ma = &info->mti_attr;
1068 struct ptlrpc_request *req = mdt_info_req(info);
1069 struct mdt_object *msrcdir;
1070 struct mdt_object *mtgtdir;
1071 struct mdt_object *mold;
1072 struct mdt_object *mnew = NULL;
1073 struct mdt_lock_handle *lh_srcdirp;
1074 struct mdt_lock_handle *lh_tgtdirp;
1075 struct mdt_lock_handle *lh_oldp;
1076 struct mdt_lock_handle *lh_newp;
1077 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1078 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1079 struct lustre_handle rename_lh = { 0 };
1080 struct lu_name slname = { 0 };
1081 struct lu_name *lname;
1085 if (info->mti_dlm_req)
1086 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1088 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1089 PFID(rr->rr_fid1), rr->rr_name,
1090 PFID(rr->rr_fid2), rr->rr_tgt);
1092 rc = mdt_rename_lock(info, &rename_lh);
1094 CERROR("Can't lock FS for rename, rc %d\n", rc);
1098 lh_newp = &info->mti_lh[MDT_LH_NEW];
1100 /* step 1: lock the source dir. */
1101 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1102 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1104 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1105 MDS_INODELOCK_UPDATE);
1106 if (IS_ERR(msrcdir))
1107 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1109 if (mdt_object_obf(msrcdir))
1110 GOTO(out_unlock_source, rc = -EPERM);
1112 rc = mdt_version_get_check_save(info, msrcdir, 0);
1114 GOTO(out_unlock_source, rc);
1116 /* step 2: find & lock the target dir. */
1117 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1118 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1120 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1121 mdt_object_get(info->mti_env, msrcdir);
1123 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1124 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1125 MDS_INODELOCK_UPDATE);
1127 GOTO(out_unlock_source, rc);
1128 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1131 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1133 if (IS_ERR(mtgtdir))
1134 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1136 if (mdt_object_obf(mtgtdir))
1137 GOTO(out_put_target, rc = -EPERM);
1139 /* check early, the real version will be saved after locking */
1140 rc = mdt_version_get_check(info, mtgtdir, 1);
1142 GOTO(out_put_target, rc);
1144 if (unlikely(mdt_object_remote(mtgtdir))) {
1145 CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1146 "on different MDTs\n", PFID(rr->rr_fid1),
1148 GOTO(out_put_target, rc = -EXDEV);
1150 if (likely(mdt_object_exists(mtgtdir))) {
1151 /* we lock the target dir if it is local */
1152 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1153 MDS_INODELOCK_UPDATE,
1156 GOTO(out_put_target, rc);
1157 /* get and save correct version after locking */
1158 mdt_version_get_save(info, mtgtdir, 1);
1160 GOTO(out_put_target, rc = -ESTALE);
1165 /* step 3: find & lock the old object. */
1166 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1167 mdt_name_copy(&slname, lname);
1169 rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1171 GOTO(out_unlock_target, rc);
1173 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1174 GOTO(out_unlock_target, rc = -EINVAL);
1176 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1178 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1179 if (mdt_object_remote(mold)) {
1180 mdt_object_put(info->mti_env, mold);
1181 CDEBUG(D_INFO, "Source child "DFID" is on another MDT\n",
1183 GOTO(out_unlock_target, rc = -EXDEV);
1186 if (mdt_object_obf(mold)) {
1187 mdt_object_put(info->mti_env, mold);
1188 GOTO(out_unlock_target, rc = -EPERM);
1191 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1192 mdt_lock_reg_init(lh_oldp, LCK_EX);
1193 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1196 mdt_object_put(info->mti_env, mold);
1197 GOTO(out_unlock_target, rc);
1200 info->mti_mos = mold;
1201 /* save version after locking */
1202 mdt_version_get_save(info, mold, 2);
1203 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1205 /* step 4: find & lock the new object. */
1206 /* new target object may not exist now */
1207 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1208 /* lookup with version checking */
1210 rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1212 /* the new_fid should have been filled at this moment */
1213 if (lu_fid_eq(old_fid, new_fid))
1214 GOTO(out_unlock_old, rc);
1216 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1217 lu_fid_eq(new_fid, rr->rr_fid2))
1218 GOTO(out_unlock_old, rc = -EINVAL);
1220 mdt_lock_reg_init(lh_newp, LCK_EX);
1221 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1223 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1225 if (mdt_object_obf(mnew)) {
1226 mdt_object_put(info->mti_env, mnew);
1227 GOTO(out_unlock_old, rc = -EPERM);
1230 if (mdt_object_remote(mnew)) {
1231 mdt_object_put(info->mti_env, mnew);
1232 CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1234 GOTO(out_unlock_old, rc = -EXDEV);
1237 rc = mdt_object_lock(info, mnew, lh_newp,
1238 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1240 mdt_object_put(info->mti_env, mnew);
1241 GOTO(out_unlock_old, rc);
1243 /* get and save version after locking */
1244 mdt_version_get_save(info, mnew, 3);
1245 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1246 } else if (rc != -EREMOTE && rc != -ENOENT) {
1247 GOTO(out_unlock_old, rc);
1249 mdt_enoent_version_save(info, 3);
1252 /* step 5: rename it */
1253 mdt_reint_init_ma(info, ma);
1255 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1256 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1259 /* Check if @dst is subdir of @src. */
1260 rc = mdt_rename_sanity(info, old_fid);
1262 GOTO(out_unlock_new, rc);
1264 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1265 mdt_object_child(mtgtdir), old_fid, &slname,
1266 (mnew ? mdt_object_child(mnew) : NULL),
1269 /* handle last link of tgt object */
1271 mdt_counter_incr(req, LPROC_MDT_RENAME);
1273 mdt_handle_last_unlink(info, mnew, ma);
1275 mdt_rename_counter_tally(info, info->mti_mdt, req,
1282 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1284 mdt_object_unlock_put(info, mold, lh_oldp, rc);
1286 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1288 mdt_object_put(info->mti_env, mtgtdir);
1290 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1292 if (lustre_handle_is_used(&rename_lh))
1293 mdt_rename_unlock(&rename_lh);
1297 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1298 struct mdt_lock_handle *lhc);
1300 static mdt_reinter reinters[REINT_MAX] = {
1301 [REINT_SETATTR] = mdt_reint_setattr,
1302 [REINT_CREATE] = mdt_reint_create,
1303 [REINT_LINK] = mdt_reint_link,
1304 [REINT_UNLINK] = mdt_reint_unlink,
1305 [REINT_RENAME] = mdt_reint_rename,
1306 [REINT_OPEN] = mdt_reint_open,
1307 [REINT_SETXATTR] = mdt_reint_setxattr,
1308 [REINT_RMENTRY] = mdt_reint_unlink
1311 int mdt_reint_rec(struct mdt_thread_info *info,
1312 struct mdt_lock_handle *lhc)
1317 rc = reinters[info->mti_rr.rr_opcode](info, lhc);