Whamcloud - gitweb
6c657dac4bcf98883c5d7f253980955465b27482
[fs/lustre-release.git] / lustre / mdt / mdt_reint.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_reint.c
37  *
38  * Lustre Metadata Target (mdt) reintegration routines
39  *
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>
45  */
46
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include "mdt_internal.h"
50 #include <lustre_lmv.h>
51
52 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
53                                      struct md_attr *ma)
54 {
55         ma->ma_need = MA_INODE;
56         ma->ma_valid = 0;
57 }
58
59 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
60                                 struct mdt_object *object,
61                                 struct mdt_body *repbody)
62 {
63         ENTRY;
64
65         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
66          * we won't go to below*/
67         if (repbody->valid & OBD_MD_FLMDSCAPA)
68                 RETURN(rc);
69
70         if (rc == 0 && info->mti_mdt->mdt_lut.lut_mds_capa &&
71             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
72                 struct lustre_capa *capa;
73
74                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
75                 LASSERT(capa);
76                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
77                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
78                                  0);
79                 if (rc == 0)
80                         repbody->valid |= OBD_MD_FLMDSCAPA;
81         }
82
83         RETURN(rc);
84 }
85
86 /**
87  * Get version of object by fid.
88  *
89  * Return real version or ENOENT_VERSION if object doesn't exist
90  */
91 static void mdt_obj_version_get(struct mdt_thread_info *info,
92                                 struct mdt_object *o, __u64 *version)
93 {
94         LASSERT(o);
95         if (mdt_object_exists(o) && !mdt_object_remote(o) &&
96             !fid_is_obf(mdt_object_fid(o)))
97                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
98         else
99                 *version = ENOENT_VERSION;
100         CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
101                PFID(mdt_object_fid(o)), *version);
102 }
103
104 /**
105  * Check version is correct.
106  *
107  * Should be called only during replay.
108  */
109 static int mdt_version_check(struct ptlrpc_request *req,
110                              __u64 version, int idx)
111 {
112         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
113         ENTRY;
114
115         if (!exp_connect_vbr(req->rq_export))
116                 RETURN(0);
117
118         LASSERT(req_is_replay(req));
119         /** VBR: version is checked always because costs nothing */
120         LASSERT(idx < PTLRPC_NUM_VERSIONS);
121         /** Sanity check for malformed buffers */
122         if (pre_ver == NULL) {
123                 CERROR("No versions in request buffer\n");
124                 spin_lock(&req->rq_export->exp_lock);
125                 req->rq_export->exp_vbr_failed = 1;
126                 spin_unlock(&req->rq_export->exp_lock);
127                 RETURN(-EOVERFLOW);
128         } else if (pre_ver[idx] != version) {
129                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
130                        pre_ver[idx], version);
131                 spin_lock(&req->rq_export->exp_lock);
132                 req->rq_export->exp_vbr_failed = 1;
133                 spin_unlock(&req->rq_export->exp_lock);
134                 RETURN(-EOVERFLOW);
135         }
136         RETURN(0);
137 }
138
139 /**
140  * Save pre-versions in reply.
141  */
142 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
143                              int idx)
144 {
145         __u64 *reply_ver;
146
147         if (!exp_connect_vbr(req->rq_export))
148                 return;
149
150         LASSERT(!req_is_replay(req));
151         LASSERT(req->rq_repmsg != NULL);
152         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
153         if (reply_ver)
154                 reply_ver[idx] = version;
155 }
156
157 /**
158  * Save enoent version, it is needed when it is obvious that object doesn't
159  * exist, e.g. child during create.
160  */
161 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
162 {
163         /* save version of file name for replay, it must be ENOENT here */
164         if (!req_is_replay(mdt_info_req(info))) {
165                 info->mti_ver[idx] = ENOENT_VERSION;
166                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
167         }
168 }
169
170 /**
171  * Get version from disk and save in reply buffer.
172  *
173  * Versions are saved in reply only during normal operations not replays.
174  */
175 void mdt_version_get_save(struct mdt_thread_info *info,
176                           struct mdt_object *mto, int idx)
177 {
178         /* don't save versions during replay */
179         if (!req_is_replay(mdt_info_req(info))) {
180                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
181                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
182         }
183 }
184
185 /**
186  * Get version from disk and check it, no save in reply.
187  */
188 int mdt_version_get_check(struct mdt_thread_info *info,
189                           struct mdt_object *mto, int idx)
190 {
191         /* only check versions during replay */
192         if (!req_is_replay(mdt_info_req(info)))
193                 return 0;
194
195         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
196         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
197 }
198
199 /**
200  * Get version from disk and check if recovery or just save.
201  */
202 int mdt_version_get_check_save(struct mdt_thread_info *info,
203                                struct mdt_object *mto, int idx)
204 {
205         int rc = 0;
206
207         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
208         if (req_is_replay(mdt_info_req(info)))
209                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
210                                        idx);
211         else
212                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
213         return rc;
214 }
215
216 /**
217  * Lookup with version checking.
218  *
219  * This checks version of 'name'. Many reint functions uses 'name' for child not
220  * FID, therefore we need to get object by name and check its version.
221  */
222 int mdt_lookup_version_check(struct mdt_thread_info *info,
223                              struct mdt_object *p, const struct lu_name *lname,
224                              struct lu_fid *fid, int idx)
225 {
226         int rc, vbrc;
227
228         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
229                         &info->mti_spec);
230         /* Check version only during replay */
231         if (!req_is_replay(mdt_info_req(info)))
232                 return rc;
233
234         info->mti_ver[idx] = ENOENT_VERSION;
235         if (rc == 0) {
236                 struct mdt_object *child;
237                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
238                 if (likely(!IS_ERR(child))) {
239                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
240                         mdt_object_put(info->mti_env, child);
241                 }
242         }
243         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
244         return vbrc ? vbrc : rc;
245
246 }
247
248 /*
249  * VBR: we save three versions in reply:
250  * 0 - parent. Check that parent version is the same during replay.
251  * 1 - name. Version of 'name' if file exists with the same name or
252  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
253  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
254  * check.
255  */
256 static int mdt_md_create(struct mdt_thread_info *info)
257 {
258         struct mdt_device       *mdt = info->mti_mdt;
259         struct mdt_object       *parent;
260         struct mdt_object       *child;
261         struct mdt_lock_handle  *lh;
262         struct mdt_body         *repbody;
263         struct md_attr          *ma = &info->mti_attr;
264         struct mdt_reint_record *rr = &info->mti_rr;
265         int rc;
266         ENTRY;
267
268         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  ("DNAME"->"DFID") "
269                   "in "DFID,
270                   PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
271
272         if (!fid_is_md_operative(rr->rr_fid1))
273                 RETURN(-EPERM);
274
275         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
276
277         parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
278         if (IS_ERR(parent))
279                 RETURN(PTR_ERR(parent));
280
281         if (!mdt_object_exists(parent))
282                 GOTO(put_parent, rc = -ENOENT);
283
284         lh = &info->mti_lh[MDT_LH_PARENT];
285         mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
286         rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE,
287                              MDT_CROSS_LOCK);
288         if (rc)
289                 GOTO(put_parent, rc);
290
291         if (!mdt_object_remote(parent)) {
292                 rc = mdt_version_get_check_save(info, parent, 0);
293                 if (rc)
294                         GOTO(unlock_parent, rc);
295         }
296
297         /*
298          * Check child name version during replay.
299          * During create replay a file may exist with same name.
300          */
301         rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
302                                       &info->mti_tmp_fid1, 1);
303         if (rc == 0)
304                 GOTO(unlock_parent, rc = -EEXIST);
305
306         /* -ENOENT is expected here */
307         if (rc != -ENOENT)
308                 GOTO(unlock_parent, rc);
309
310         /* save version of file name for replay, it must be ENOENT here */
311         mdt_enoent_version_save(info, 1);
312
313         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
314         if (likely(!IS_ERR(child))) {
315                 struct md_object *next = mdt_object_child(parent);
316
317                 if (mdt_object_remote(child)) {
318                         struct seq_server_site *ss;
319                         struct lu_ucred *uc  = mdt_ucred(info);
320
321                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
322                                 if (uc->uc_gid !=
323                                     mdt->mdt_enable_remote_dir_gid &&
324                                     mdt->mdt_enable_remote_dir_gid != -1) {
325                                         CERROR("%s: Creating remote dir is only"
326                                                " permitted for administrator or"
327                                                " set mdt_enable_remote_dir_gid:"
328                                                " rc = %d\n",
329                                                 mdt_obd_name(mdt), -EPERM);
330                                         GOTO(out_put_child, rc = -EPERM);
331                                 }
332                         }
333
334                         ss = mdt_seq_site(mdt);
335                         if (ss->ss_node_id != 0 &&
336                             mdt->mdt_enable_remote_dir == 0) {
337                                 CERROR("%s: remote dir is only permitted on"
338                                        " MDT0 or set_param"
339                                        " mdt.*.enable_remote_dir=1\n",
340                                        mdt_obd_name(mdt));
341                                 GOTO(out_put_child, rc = -EPERM);
342                         }
343                         if (!mdt_is_dne_client(mdt_info_req(info)->rq_export)) {
344                                 /* Return -EIO for old client */
345                                 GOTO(out_put_child, rc = -EIO);
346                         }
347
348                 }
349                 ma->ma_need = MA_INODE;
350                 ma->ma_valid = 0;
351                 /* capa for cross-ref will be stored here */
352                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
353                                                      &RMF_CAPA1);
354                 LASSERT(ma->ma_capa);
355
356                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
357                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
358
359                 /* Version of child will be updated on disk. */
360                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
361                 rc = mdt_version_get_check_save(info, child, 2);
362                 if (rc)
363                         GOTO(out_put_child, rc);
364
365                 /* Let lower layer know current lock mode. */
366                 info->mti_spec.sp_cr_mode =
367                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
368
369                 /*
370                  * Do not perform lookup sanity check. We know that name does
371                  * not exist.
372                  */
373                 info->mti_spec.sp_cr_lookup = 0;
374                 info->mti_spec.sp_feat = &dt_directory_features;
375
376                 rc = mdo_create(info->mti_env, next, &rr->rr_name,
377                                 mdt_object_child(child), &info->mti_spec, ma);
378                 if (rc == 0)
379                         rc = mdt_attr_get_complex(info, child, ma);
380
381                 if (rc == 0) {
382                         /* Return fid & attr to client. */
383                         if (ma->ma_valid & MA_INODE)
384                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
385                                                    mdt_object_fid(child));
386                 }
387 out_put_child:
388                 mdt_object_put(info->mti_env, child);
389         } else {
390                 rc = PTR_ERR(child);
391         }
392         mdt_create_pack_capa(info, rc, child, repbody);
393 unlock_parent:
394         mdt_object_unlock(info, parent, lh, rc);
395 put_parent:
396         mdt_object_put(info->mti_env, parent);
397         RETURN(rc);
398 }
399
400 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
401                              struct mdt_object *obj, __u64 ibits,
402                              struct ldlm_enqueue_info *einfo)
403 {
404         ldlm_policy_data_t      *policy = &mti->mti_policy;
405         int                     rc;
406         ENTRY;
407
408         if (!S_ISDIR(obj->mot_header.loh_attr))
409                 RETURN(0);
410
411         memset(policy, 0, sizeof(*policy));
412         policy->l_inodebits.bits = ibits;
413
414         rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
415                               policy);
416         RETURN(rc);
417 }
418
419 /**
420  * Lock slave stripes if necessary, the lock handles of slave stripes
421  * will be stored in einfo->ei_cbdata.
422  **/
423 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
424                            ldlm_mode_t mode, __u64 ibits,
425                            struct ldlm_enqueue_info *einfo)
426 {
427         ldlm_policy_data_t      *policy = &mti->mti_policy;
428         int                     rc;
429         ENTRY;
430
431         if (!S_ISDIR(obj->mot_header.loh_attr))
432                 RETURN(0);
433
434         memset(einfo, 0, sizeof(*einfo));
435         einfo->ei_type = LDLM_IBITS;
436         einfo->ei_mode = mode;
437         einfo->ei_cb_bl = mdt_remote_blocking_ast;
438         einfo->ei_cb_cp = ldlm_completion_ast;
439         einfo->ei_enq_slave = 1;
440         memset(policy, 0, sizeof(*policy));
441         policy->l_inodebits.bits = ibits;
442
443         rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
444                             policy);
445         RETURN(rc);
446 }
447
448 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
449                  struct md_attr *ma, int flags)
450 {
451         struct mdt_lock_handle  *lh;
452         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
453         __u64 lockpart = MDS_INODELOCK_UPDATE;
454         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
455         int rc;
456         ENTRY;
457
458         /* attr shouldn't be set on remote object */
459         LASSERT(!mdt_object_remote(mo));
460
461         lh = &info->mti_lh[MDT_LH_PARENT];
462         mdt_lock_reg_init(lh, LCK_PW);
463
464         /* Even though the new MDT will grant PERM lock to the old
465          * client, but the old client will almost ignore that during
466          * So it needs to revoke both LOOKUP and PERM lock here, so
467          * both new and old client can cancel the dcache */
468         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
469                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
470
471         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
472         if (rc != 0)
473                 RETURN(rc);
474
475         rc = mdt_lock_slaves(info, mo, LCK_EX, lockpart, einfo);
476         if (rc != 0)
477                 GOTO(out_unlock, rc);
478
479         if (mdt_object_exists(mo) == 0)
480                 GOTO(out_unlock, rc = -ENOENT);
481
482         /* all attrs are packed into mti_attr in unpack_setattr */
483         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
484                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
485
486         /* This is only for set ctime when rename's source is on remote MDS. */
487         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
488                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
489
490         /* VBR: update version if attr changed are important for recovery */
491         if (do_vbr) {
492                 /* update on-disk version of changed object */
493                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
494                 rc = mdt_version_get_check_save(info, mo, 0);
495                 if (rc)
496                         GOTO(out_unlock, rc);
497         }
498
499         /* Ensure constant striping during chown(). See LU-2789. */
500         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
501                 mutex_lock(&mo->mot_lov_mutex);
502
503         /* all attrs are packed into mti_attr in unpack_setattr */
504         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
505
506         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
507                 mutex_unlock(&mo->mot_lov_mutex);
508
509         if (rc != 0)
510                 GOTO(out_unlock, rc);
511
512         EXIT;
513 out_unlock:
514         mdt_unlock_slaves(info, mo, lockpart, einfo);
515         mdt_object_unlock(info, mo, lh, rc);
516         return rc;
517 }
518
519 /**
520  * Check HSM flags and add HS_DIRTY flag if relevant.
521  *
522  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
523  * and is not RELEASED.
524  */
525 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
526                         struct md_attr *ma)
527 {
528         int rc;
529         ENTRY;
530
531         /* If the file was modified, add the dirty flag */
532         ma->ma_need = MA_HSM;
533         rc = mdt_attr_get_complex(info, mo, ma);
534         if (rc) {
535                 CERROR("file attribute read error for "DFID": %d.\n",
536                         PFID(mdt_object_fid(mo)), rc);
537                 RETURN(rc);
538         }
539
540         /* If an up2date copy exists in the backend, add dirty flag */
541         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
542             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
543                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
544
545                 ma->ma_hsm.mh_flags |= HS_DIRTY;
546
547                 mdt_lock_reg_init(lh, LCK_PW);
548                 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
549                                      MDT_LOCAL_LOCK);
550                 if (rc != 0)
551                         RETURN(rc);
552
553                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
554                 if (rc)
555                         CERROR("file attribute change error for "DFID": %d\n",
556                                 PFID(mdt_object_fid(mo)), rc);
557                 mdt_object_unlock(info, mo, lh, rc);
558         }
559
560         RETURN(rc);
561 }
562
563 static int mdt_reint_setattr(struct mdt_thread_info *info,
564                              struct mdt_lock_handle *lhc)
565 {
566         struct md_attr          *ma = &info->mti_attr;
567         struct mdt_reint_record *rr = &info->mti_rr;
568         struct ptlrpc_request   *req = mdt_info_req(info);
569         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
570         struct mdt_file_data    *mfd;
571         struct mdt_object       *mo;
572         struct mdt_body         *repbody;
573         int                      som_au, rc, rc2;
574         ENTRY;
575
576         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
577                   (unsigned int)ma->ma_attr.la_valid);
578
579         if (info->mti_dlm_req)
580                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
581
582         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
583         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
584         if (IS_ERR(mo))
585                 GOTO(out, rc = PTR_ERR(mo));
586
587         /* start a log jounal handle if needed */
588         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
589                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
590                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
591                         /* Check write access for the O_TRUNC case */
592                         if (mdt_write_read(mo) < 0)
593                                 GOTO(out_put, rc = -ETXTBSY);
594                 }
595         } else if (info->mti_ioepoch &&
596                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
597                 /* Truncate case. IOEpoch is opened. */
598                 rc = mdt_write_get(mo);
599                 if (rc)
600                         GOTO(out_put, rc);
601
602                 mfd = mdt_mfd_new(med);
603                 if (mfd == NULL) {
604                         mdt_write_put(mo);
605                         GOTO(out_put, rc = -ENOMEM);
606                 }
607
608                 mdt_ioepoch_open(info, mo, 0);
609                 repbody->ioepoch = mo->mot_ioepoch;
610
611                 mdt_object_get(info->mti_env, mo);
612                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
613                 mfd->mfd_object = mo;
614                 mfd->mfd_xid = req->rq_xid;
615
616                 spin_lock(&med->med_open_lock);
617                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
618                 spin_unlock(&med->med_open_lock);
619                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
620         }
621
622         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
623         if (som_au) {
624                 /* SOM Attribute update case. Find the proper mfd and update
625                  * SOM attributes on the proper object. */
626                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
627                 LASSERT(info->mti_ioepoch);
628
629                 spin_lock(&med->med_open_lock);
630                 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
631                                      req_is_replay(req));
632                 if (mfd == NULL) {
633                         spin_unlock(&med->med_open_lock);
634                         CDEBUG(D_INODE, "no handle for file close: "
635                                "fid = "DFID": cookie = "LPX64"\n",
636                                PFID(info->mti_rr.rr_fid1),
637                                info->mti_ioepoch->handle.cookie);
638                         GOTO(out_put, rc = -ESTALE);
639                 }
640                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
641                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
642
643                 class_handle_unhash(&mfd->mfd_handle);
644                 cfs_list_del_init(&mfd->mfd_list);
645                 spin_unlock(&med->med_open_lock);
646
647                 mdt_mfd_close(info, mfd);
648         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
649                 LASSERT((ma->ma_valid & MA_LOV) == 0);
650                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
651                 if (rc)
652                         GOTO(out_put, rc);
653         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
654                 struct lu_buf *buf  = &info->mti_buf;
655                 LASSERT(ma->ma_attr.la_valid == 0);
656                 buf->lb_buf = ma->ma_lmm;
657                 buf->lb_len = ma->ma_lmm_size;
658                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
659                                   buf, XATTR_NAME_LOV, 0);
660                 if (rc)
661                         GOTO(out_put, rc);
662         } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
663                 struct lu_buf *buf  = &info->mti_buf;
664
665                 LASSERT(ma->ma_attr.la_valid == 0);
666                 buf->lb_buf = ma->ma_lmv;
667                 buf->lb_len = ma->ma_lmv_size;
668                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
669                                   buf, XATTR_NAME_DEFAULT_LMV, 0);
670                 if (rc)
671                         GOTO(out_put, rc);
672         } else
673                 LBUG();
674
675         /* If file data is modified, add the dirty flag */
676         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
677                 rc = mdt_add_dirty_flag(info, mo, ma);
678
679         ma->ma_need = MA_INODE;
680         ma->ma_valid = 0;
681         rc = mdt_attr_get_complex(info, mo, ma);
682         if (rc != 0)
683                 GOTO(out_put, rc);
684
685         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
686
687         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
688             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
689             S_ISREG(lu_object_attr(&mo->mot_obj)) &&
690             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
691                 struct lustre_capa *capa;
692
693                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
694                 LASSERT(capa);
695                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
696                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
697                 if (rc)
698                         GOTO(out_put, rc);
699                 repbody->valid |= OBD_MD_FLOSSCAPA;
700         }
701
702         EXIT;
703 out_put:
704         mdt_object_put(info->mti_env, mo);
705 out:
706         if (rc == 0)
707                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
708
709         mdt_client_compatibility(info);
710         rc2 = mdt_fix_reply(info);
711         if (rc == 0)
712                 rc = rc2;
713         return rc;
714 }
715
716 static int mdt_reint_create(struct mdt_thread_info *info,
717                             struct mdt_lock_handle *lhc)
718 {
719         struct ptlrpc_request   *req = mdt_info_req(info);
720         int                     rc;
721         ENTRY;
722
723         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
724                 RETURN(err_serious(-ESTALE));
725
726         if (info->mti_dlm_req)
727                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
728
729         if (!lu_name_is_valid(&info->mti_rr.rr_name))
730                 RETURN(-EPROTO);
731
732         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
733         case S_IFDIR:
734                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
735                 break;
736         case S_IFREG:
737         case S_IFLNK:
738         case S_IFCHR:
739         case S_IFBLK:
740         case S_IFIFO:
741         case S_IFSOCK:
742                 /* Special file should stay on the same node as parent. */
743                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
744                 break;
745         default:
746                 CERROR("%s: Unsupported mode %o\n",
747                        mdt_obd_name(info->mti_mdt),
748                        info->mti_attr.ma_attr.la_mode);
749                 RETURN(err_serious(-EOPNOTSUPP));
750         }
751
752         rc = mdt_md_create(info);
753         RETURN(rc);
754 }
755
756 /*
757  * VBR: save parent version in reply and child version getting by its name.
758  * Version of child is getting and checking during its lookup. If
759  */
760 static int mdt_reint_unlink(struct mdt_thread_info *info,
761                             struct mdt_lock_handle *lhc)
762 {
763         struct mdt_reint_record *rr = &info->mti_rr;
764         struct ptlrpc_request   *req = mdt_info_req(info);
765         struct md_attr          *ma = &info->mti_attr;
766         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
767         struct mdt_object       *mp;
768         struct mdt_object       *mc;
769         struct mdt_lock_handle  *parent_lh;
770         struct mdt_lock_handle  *child_lh;
771         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
772         int                     rc;
773         int                     no_name = 0;
774         ENTRY;
775
776         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
777                   PNAME(&rr->rr_name));
778
779         if (info->mti_dlm_req)
780                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
781
782         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
783                 RETURN(err_serious(-ENOENT));
784
785         if (!fid_is_md_operative(rr->rr_fid1))
786                 RETURN(-EPERM);
787
788         /*
789          * step 1: Found the parent.
790          */
791         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
792         if (IS_ERR(mp)) {
793                 rc = PTR_ERR(mp);
794                 GOTO(out, rc);
795         }
796
797         parent_lh = &info->mti_lh[MDT_LH_PARENT];
798         mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
799         rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
800                              MDT_CROSS_LOCK);
801         if (rc != 0)
802                 GOTO(put_parent, rc);
803
804         if (!mdt_object_remote(mp)) {
805                 rc = mdt_version_get_check_save(info, mp, 0);
806                 if (rc)
807                         GOTO(unlock_parent, rc);
808         }
809
810         /* step 2: find & lock the child */
811         /* lookup child object along with version checking */
812         fid_zero(child_fid);
813         rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
814         if (rc != 0) {
815                 /* Name might not be able to find during resend of
816                  * remote unlink, considering following case.
817                  * dir_A is a remote directory, the name entry of
818                  * dir_A is on MDT0, the directory is on MDT1,
819                  *
820                  * 1. client sends unlink req to MDT1.
821                  * 2. MDT1 sends name delete update to MDT0.
822                  * 3. name entry is being deleted in MDT0 synchronously.
823                  * 4. MDT1 is restarted.
824                  * 5. client resends unlink req to MDT1. So it can not
825                  *    find the name entry on MDT0 anymore.
826                  * In this case, MDT1 only needs to destory the local
827                  * directory.
828                  * */
829                 if (mdt_object_remote(mp) && rc == -ENOENT &&
830                     !fid_is_zero(rr->rr_fid2) &&
831                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
832                         no_name = 1;
833                         *child_fid = *rr->rr_fid2;
834                  } else {
835                         GOTO(unlock_parent, rc);
836                  }
837         }
838
839         if (!fid_is_md_operative(child_fid))
840                 GOTO(unlock_parent, rc = -EPERM);
841
842         /* We will lock the child regardless it is local or remote. No harm. */
843         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
844         if (IS_ERR(mc))
845                 GOTO(unlock_parent, rc = PTR_ERR(mc));
846
847         child_lh = &info->mti_lh[MDT_LH_CHILD];
848         mdt_lock_reg_init(child_lh, LCK_EX);
849         if (mdt_object_remote(mc)) {
850                 struct mdt_body  *repbody;
851
852                 if (!fid_is_zero(rr->rr_fid2)) {
853                         CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
854                                mdt_obd_name(info->mti_mdt),
855                                PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
856                         GOTO(put_child, rc = -ENOENT);
857                 }
858                 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
859                        mdt_obd_name(info->mti_mdt),
860                        PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
861
862                 if (!mdt_is_dne_client(req->rq_export))
863                         /* Return -EIO for old client */
864                         GOTO(put_child, rc = -EIO);
865
866                 if (info->mti_spec.sp_rm_entry) {
867                         struct lu_ucred *uc  = mdt_ucred(info);
868
869                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
870                                 CERROR("%s: unlink remote entry is only "
871                                        "permitted for administrator: rc = %d\n",
872                                         mdt_obd_name(info->mti_mdt),
873                                         -EPERM);
874                                 GOTO(put_child, rc = -EPERM);
875                         }
876
877                         ma->ma_need = MA_INODE;
878                         ma->ma_valid = 0;
879                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
880                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
881                                         NULL, &rr->rr_name, ma, no_name);
882                         GOTO(put_child, rc);
883                 }
884                 /* Revoke the LOOKUP lock of the remote object granted by
885                  * this MDT. Since the unlink will happen on another MDT,
886                  * it will release the LOOKUP lock right away. Then What
887                  * would happen if another client try to grab the LOOKUP
888                  * lock at the same time with unlink XXX */
889                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
890                                 MDT_CROSS_LOCK);
891                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
892                 LASSERT(repbody != NULL);
893                 repbody->fid1 = *mdt_object_fid(mc);
894                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
895                 GOTO(unlock_child, rc = -EREMOTE);
896         } else if (info->mti_spec.sp_rm_entry) {
897                 rc = -EPERM;
898                 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
899                        "rc = %d\n",
900                        mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
901                 GOTO(put_child, rc);
902         }
903
904         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
905          * this now because a running HSM restore on the child (unlink
906          * victim) will hold the layout lock. See LU-4002. */
907         rc = mdt_object_lock(info, mc, child_lh,
908                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
909                              MDT_CROSS_LOCK);
910         if (rc != 0)
911                 GOTO(put_child, rc);
912         /*
913          * Now we can only make sure we need MA_INODE, in mdd layer, will check
914          * whether need MA_LOV and MA_COOKIE.
915          */
916         ma->ma_need = MA_INODE;
917         ma->ma_valid = 0;
918
919         rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, einfo);
920         if (rc != 0)
921                 GOTO(unlock_child, rc);
922
923         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
924                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
925         /* save version when object is locked */
926         mdt_version_get_save(info, mc, 1);
927
928         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
929
930         mutex_lock(&mc->mot_lov_mutex);
931
932         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
933                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
934
935         mutex_unlock(&mc->mot_lov_mutex);
936
937         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
938                 rc = mdt_attr_get_complex(info, mc, ma);
939         if (rc == 0)
940                 mdt_handle_last_unlink(info, mc, ma);
941
942         if (ma->ma_valid & MA_INODE) {
943                 switch (ma->ma_attr.la_mode & S_IFMT) {
944                 case S_IFDIR:
945                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
946                         break;
947                 case S_IFREG:
948                 case S_IFLNK:
949                 case S_IFCHR:
950                 case S_IFBLK:
951                 case S_IFIFO:
952                 case S_IFSOCK:
953                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
954                         break;
955                 default:
956                         LASSERTF(0, "bad file type %o unlinking\n",
957                                  ma->ma_attr.la_mode);
958                 }
959         }
960
961         EXIT;
962
963 unlock_child:
964         mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, einfo);
965         mdt_object_unlock(info, mc, child_lh, rc);
966
967         /* Since we do not need reply md striped dir info to client, so
968          * reset mti_big_lmm_used to avoid confusing mdt_fix_reply */
969         if (info->mti_big_lmm_used)
970                 info->mti_big_lmm_used = 0;
971 put_child:
972         mdt_object_put(info->mti_env, mc);
973 unlock_parent:
974         mdt_object_unlock(info, mp, parent_lh, rc);
975 put_parent:
976         mdt_object_put(info->mti_env, mp);
977 out:
978         return rc;
979 }
980
981 /*
982  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
983  * name.
984  */
985 static int mdt_reint_link(struct mdt_thread_info *info,
986                           struct mdt_lock_handle *lhc)
987 {
988         struct mdt_reint_record *rr = &info->mti_rr;
989         struct ptlrpc_request   *req = mdt_info_req(info);
990         struct md_attr          *ma = &info->mti_attr;
991         struct mdt_object       *ms;
992         struct mdt_object       *mp;
993         struct mdt_lock_handle  *lhs;
994         struct mdt_lock_handle  *lhp;
995         int rc;
996         ENTRY;
997
998         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
999                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1000
1001         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1002                 RETURN(err_serious(-ENOENT));
1003
1004         if (info->mti_dlm_req)
1005                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1006
1007         /* Invalid case so return error immediately instead of
1008          * processing it */
1009         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1010                 RETURN(-EPERM);
1011
1012         if (!fid_is_md_operative(rr->rr_fid1) ||
1013             !fid_is_md_operative(rr->rr_fid2))
1014                 RETURN(-EPERM);
1015
1016         /* step 1: find & lock the target parent dir */
1017         lhp = &info->mti_lh[MDT_LH_PARENT];
1018         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1019         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1020                                   MDS_INODELOCK_UPDATE);
1021         if (IS_ERR(mp))
1022                 RETURN(PTR_ERR(mp));
1023
1024         rc = mdt_version_get_check_save(info, mp, 0);
1025         if (rc)
1026                 GOTO(out_unlock_parent, rc);
1027
1028         /* step 2: find & lock the source */
1029         lhs = &info->mti_lh[MDT_LH_CHILD];
1030         mdt_lock_reg_init(lhs, LCK_EX);
1031
1032         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1033         if (IS_ERR(ms))
1034                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1035
1036         if (!mdt_object_exists(ms)) {
1037                 mdt_object_put(info->mti_env, ms);
1038                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1039                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1040                 GOTO(out_unlock_parent, rc = -ENOENT);
1041         }
1042
1043         if (mdt_object_remote(ms)) {
1044                 mdt_object_put(info->mti_env, ms);
1045                 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1046                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1047                        PFID(rr->rr_fid2));
1048                 GOTO(out_unlock_parent, rc = -EXDEV);
1049         }
1050
1051         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1052                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1053         if (rc != 0) {
1054                 mdt_object_put(info->mti_env, ms);
1055                 GOTO(out_unlock_parent, rc);
1056         }
1057
1058         /* step 3: link it */
1059         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1060                        OBD_FAIL_MDS_REINT_LINK_WRITE);
1061
1062         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1063         rc = mdt_version_get_check_save(info, ms, 1);
1064         if (rc)
1065                 GOTO(out_unlock_child, rc);
1066
1067         /** check target version by name during replay */
1068         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1069                                       &info->mti_tmp_fid1, 2);
1070         if (rc != 0 && rc != -ENOENT)
1071                 GOTO(out_unlock_child, rc);
1072         /* save version of file name for replay, it must be ENOENT here */
1073         if (!req_is_replay(mdt_info_req(info))) {
1074                 if (rc != -ENOENT) {
1075                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1076                                PNAME(&rr->rr_name));
1077                         GOTO(out_unlock_child, rc = -EEXIST);
1078                 }
1079                 info->mti_ver[2] = ENOENT_VERSION;
1080                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1081         }
1082
1083         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1084               mdt_object_child(ms), &rr->rr_name, ma);
1085
1086         if (rc == 0)
1087                 mdt_counter_incr(req, LPROC_MDT_LINK);
1088
1089         EXIT;
1090 out_unlock_child:
1091         mdt_object_unlock_put(info, ms, lhs, rc);
1092 out_unlock_parent:
1093         mdt_object_unlock_put(info, mp, lhp, rc);
1094         return rc;
1095 }
1096 /**
1097  * lock the part of the directory according to the hash of the name
1098  * (lh->mlh_pdo_hash) in parallel directory lock.
1099  */
1100 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1101                               struct mdt_lock_handle *lh,
1102                               struct mdt_object *obj, __u64 ibits)
1103 {
1104         struct ldlm_res_id *res = &info->mti_res_id;
1105         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1106         ldlm_policy_data_t *policy = &info->mti_policy;
1107         int rc;
1108
1109         /*
1110          * Finish res_id initializing by name hash marking part of
1111          * directory which is taking modification.
1112          */
1113         LASSERT(lh->mlh_pdo_hash != 0);
1114         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1115         memset(policy, 0, sizeof(*policy));
1116         policy->l_inodebits.bits = ibits;
1117         /*
1118          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1119          * going to be sent to client. If it is - mdt_intent_policy() path will
1120          * fix it up and turn FL_LOCAL flag off.
1121          */
1122         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1123                           res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1124                           &info->mti_exp->exp_handle.h_cookie);
1125         return rc;
1126 }
1127
1128 enum mdt_rename_lock {
1129         MRL_RENAME,
1130         MRL_MIGRATE,
1131 };
1132
1133 /**
1134  * Get BFL lock for rename or migrate process, right now, it does not support
1135  * cross-MDT rename, so we only need global rename lock during migration.
1136  **/
1137 static int mdt_rename_lock(struct mdt_thread_info *info,
1138                            struct lustre_handle *lh,
1139                            enum mdt_rename_lock rename_lock)
1140 {
1141         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
1142         ldlm_policy_data_t      *policy = &info->mti_policy;
1143         struct ldlm_res_id      *res_id = &info->mti_res_id;
1144         __u64                   flags = 0;
1145         int                     rc;
1146         ENTRY;
1147
1148         /* XXX only do global rename lock for migration */
1149         if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0 &&
1150             rename_lock == MRL_MIGRATE) {
1151                 struct lu_fid *fid = &info->mti_tmp_fid1;
1152                 struct mdt_object *obj;
1153
1154                 /* XXX, right now, it has to use object API to
1155                  * enqueue lock cross MDT, so it will enqueue
1156                  * rename lock(with LUSTRE_BFL_FID) by root object */
1157                 lu_root_fid(fid);
1158                 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1159                 if (IS_ERR(obj))
1160                         RETURN(PTR_ERR(obj));
1161
1162                 LASSERT(mdt_object_remote(obj));
1163                 rc = mdt_remote_object_lock(info, obj,
1164                                             &LUSTRE_BFL_FID, lh,
1165                                             LCK_EX,
1166                                             MDS_INODELOCK_UPDATE);
1167                 mdt_object_put(info->mti_env, obj);
1168         } else {
1169                 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1170                 memset(policy, 0, sizeof *policy);
1171                 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1172                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1173                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1174                                             LCK_EX, &flags, ldlm_blocking_ast,
1175                                             ldlm_completion_ast, NULL, NULL, 0,
1176                                             LVB_T_NONE,
1177                                             &info->mti_exp->exp_handle.h_cookie,
1178                                             lh);
1179         }
1180
1181         RETURN(rc);
1182 }
1183
1184 static void mdt_rename_unlock(struct lustre_handle *lh)
1185 {
1186         ENTRY;
1187         LASSERT(lustre_handle_is_used(lh));
1188         /* Cancel the single rename lock right away */
1189         ldlm_lock_decref_and_cancel(lh, LCK_EX);
1190         EXIT;
1191 }
1192
1193 /*
1194  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1195  * target. Source should not be ancestor of target dir. May be other rename
1196  * checks can be moved here later.
1197  */
1198 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1199 {
1200         struct mdt_reint_record *rr = &info->mti_rr;
1201         struct lu_fid dst_fid = *rr->rr_fid2;
1202         struct mdt_object *dst;
1203         int rc = 0;
1204         ENTRY;
1205
1206         /* If the source and target are in the same directory, they can not
1207          * be parent/child relationship, so subdir check is not needed */
1208         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1209                 return 0;
1210
1211         do {
1212                 LASSERT(fid_is_sane(&dst_fid));
1213                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1214                 if (!IS_ERR(dst)) {
1215                         rc = mdo_is_subdir(info->mti_env,
1216                                            mdt_object_child(dst), fid,
1217                                            &dst_fid);
1218                         mdt_object_put(info->mti_env, dst);
1219                         if (rc != -EREMOTE && rc < 0) {
1220                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1221                         } else {
1222                                 /* check the found fid */
1223                                 if (lu_fid_eq(&dst_fid, fid))
1224                                         rc = -EINVAL;
1225                         }
1226                 } else {
1227                         rc = PTR_ERR(dst);
1228                 }
1229         } while (rc == -EREMOTE);
1230
1231         RETURN(rc);
1232 }
1233
1234 /* Update object linkEA */
1235 struct mdt_lock_list {
1236         struct mdt_object       *mll_obj;
1237         struct mdt_lock_handle  mll_lh;
1238         struct list_head        mll_list;
1239 };
1240
1241 static void mdt_unlock_list(struct mdt_thread_info *info,
1242                             struct list_head *list, int rc)
1243 {
1244         struct mdt_lock_list *mll;
1245         struct mdt_lock_list *mll2;
1246
1247         list_for_each_entry_safe(mll, mll2, list, mll_list) {
1248                 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1249                 list_del(&mll->mll_list);
1250                 OBD_FREE_PTR(mll);
1251         }
1252 }
1253
1254 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1255                                       struct mdt_object *obj,
1256                                       struct mdt_object *pobj,
1257                                       struct list_head *lock_list)
1258 {
1259         struct lu_buf           *buf = &info->mti_big_buf;
1260         struct linkea_data      ldata = { 0 };
1261         int                     count;
1262         int                     rc;
1263         ENTRY;
1264
1265         if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1266                 RETURN(0);
1267
1268         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1269         if (buf->lb_buf == NULL)
1270                 RETURN(-ENOMEM);
1271
1272         ldata.ld_buf = buf;
1273         rc = mdt_links_read(info, obj, &ldata);
1274         if (rc != 0) {
1275                 if (rc == -ENOENT || rc == -ENODATA)
1276                         rc = 0;
1277                 RETURN(rc);
1278         }
1279
1280         LASSERT(ldata.ld_leh != NULL);
1281         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1282         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1283                 struct mdt_device *mdt = info->mti_mdt;
1284                 struct mdt_object *mdt_pobj;
1285                 struct mdt_lock_list *mll;
1286                 struct lu_name name;
1287                 struct lu_fid  fid;
1288
1289                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1290                                     &name, &fid);
1291                 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1292                                                          ldata.ld_reclen);
1293                 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1294                 if (IS_ERR(mdt_pobj)) {
1295                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1296                               mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1297                         continue;
1298                 }
1299
1300                 if (!mdt_object_exists(mdt_pobj)) {
1301                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1302                               mdt_obd_name(mdt), PFID(&fid));
1303                         mdt_object_put(info->mti_env, mdt_pobj);
1304                         continue;
1305                 }
1306
1307                 if (mdt_pobj == pobj) {
1308                         CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1309                                mdt_obd_name(mdt), PFID(&fid));
1310                         mdt_object_put(info->mti_env, mdt_pobj);
1311                         continue;
1312                 }
1313
1314                 OBD_ALLOC_PTR(mll);
1315                 if (mll == NULL) {
1316                         mdt_object_put(info->mti_env, mdt_pobj);
1317                         GOTO(out, rc = -ENOMEM);
1318                 }
1319
1320                 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1321                 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1322                                      MDS_INODELOCK_UPDATE,
1323                                      MDT_CROSS_LOCK);
1324                 if (rc != 0) {
1325                         CERROR("%s: cannot lock "DFID": rc =%d\n",
1326                                mdt_obd_name(mdt), PFID(&fid), rc);
1327                         mdt_object_put(info->mti_env, mdt_pobj);
1328                         OBD_FREE_PTR(mll);
1329                         GOTO(out, rc);
1330                 }
1331
1332                 CFS_INIT_LIST_HEAD(&mll->mll_list);
1333                 mll->mll_obj = mdt_pobj;
1334                 list_add_tail(&mll->mll_list, lock_list);
1335         }
1336 out:
1337         if (rc != 0)
1338                 mdt_unlock_list(info, lock_list, rc);
1339         RETURN(rc);
1340 }
1341
1342 /* migrate files from one MDT to another MDT */
1343 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1344                                       struct mdt_lock_handle *lhc)
1345 {
1346         struct mdt_reint_record *rr = &info->mti_rr;
1347         struct md_attr          *ma = &info->mti_attr;
1348         struct mdt_object       *msrcdir;
1349         struct mdt_object       *mold;
1350         struct mdt_object       *mnew = NULL;
1351         struct mdt_lock_handle  *lh_dirp;
1352         struct mdt_lock_handle  *lh_childp;
1353         struct mdt_lock_handle  *lh_tgtp = NULL;
1354         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1355         struct list_head        lock_list;
1356         int                     rc;
1357         ENTRY;
1358
1359         CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1360                PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1361         /* 1: lock the source dir. */
1362         msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1363         if (IS_ERR(msrcdir)) {
1364                 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1365                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1366                         (int)PTR_ERR(msrcdir));
1367                 RETURN(PTR_ERR(msrcdir));
1368         }
1369
1370         lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1371         mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1372         rc = mdt_object_lock(info, msrcdir, lh_dirp,
1373                              MDS_INODELOCK_UPDATE,
1374                              MDT_CROSS_LOCK);
1375         if (rc)
1376                 GOTO(out_put_parent, rc);
1377
1378         if (!mdt_object_remote(msrcdir)) {
1379                 rc = mdt_version_get_check_save(info, msrcdir, 0);
1380                 if (rc)
1381                         GOTO(out_unlock_parent, rc);
1382         }
1383
1384         /* 2: sanity check and find the object to be migrated. */
1385         fid_zero(old_fid);
1386         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1387         if (rc != 0)
1388                 GOTO(out_unlock_parent, rc);
1389
1390         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1391                 GOTO(out_unlock_parent, rc = -EINVAL);
1392
1393         if (!fid_is_md_operative(old_fid))
1394                 GOTO(out_unlock_parent, rc = -EPERM);
1395
1396         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1397         if (IS_ERR(mold))
1398                 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1399
1400         if (mdt_object_remote(mold)) {
1401                 CERROR("%s: source "DFID" is on the remote MDT\n",
1402                        mdt_obd_name(info->mti_mdt), PFID(old_fid));
1403                 GOTO(out_put_child, rc = -EREMOTE);
1404         }
1405
1406         if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1407             !mdt_object_remote(msrcdir)) {
1408                 CERROR("%s: parent "DFID" is still on the same"
1409                        " MDT, which should be migrated first:"
1410                        " rc = %d\n", mdt_obd_name(info->mti_mdt),
1411                        PFID(mdt_object_fid(msrcdir)), -EPERM);
1412                 GOTO(out_put_child, rc = -EPERM);
1413         }
1414
1415         /* 3: iterate the linkea of the object and lock all of the objects */
1416         CFS_INIT_LIST_HEAD(&lock_list);
1417         rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1418         if (rc != 0)
1419                 GOTO(out_put_child, rc);
1420
1421         /* 4: lock of the object migrated object */
1422         lh_childp = &info->mti_lh[MDT_LH_OLD];
1423         mdt_lock_reg_init(lh_childp, LCK_EX);
1424         rc = mdt_object_lock(info, mold, lh_childp,
1425                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1426                              MDS_INODELOCK_LAYOUT, MDT_CROSS_LOCK);
1427         if (rc != 0)
1428                 GOTO(out_unlock_list, rc);
1429
1430         ma->ma_need = MA_LMV;
1431         ma->ma_valid = 0;
1432         ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1433         ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1434         rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1435         if (rc != 0)
1436                 GOTO(out_unlock_list, rc);
1437
1438         if ((ma->ma_valid & MA_LMV)) {
1439                 struct lmv_mds_md_v1 *lmm1;
1440
1441                 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1442                 lmm1 = &ma->ma_lmv->lmv_md_v1;
1443                 if (lmm1->lmv_magic != LMV_MAGIC_MIGRATE) {
1444                         CERROR("%s: can not migrate striped dir "DFID
1445                                ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1446                                PFID(mdt_object_fid(mold)), -EPERM);
1447                         GOTO(out_unlock_child, rc = -EPERM);
1448                 }
1449
1450                 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1451                         GOTO(out_unlock_child, rc = -EINVAL);
1452
1453                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1454                                        &lmm1->lmv_stripe_fids[1]);
1455                 if (IS_ERR(mnew))
1456                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1457
1458                 if (!mdt_object_remote(mnew)) {
1459                         CERROR("%s: "DFID" being migrated is on this MDT:"
1460                                " rc  = %d\n", mdt_obd_name(info->mti_mdt),
1461                                PFID(rr->rr_fid2), -EPERM);
1462                         GOTO(out_put_new, rc = -EPERM);
1463                 }
1464
1465                 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1466                 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1467                 rc = mdt_remote_object_lock(info, mnew,
1468                                             mdt_object_fid(mnew),
1469                                             &lh_tgtp->mlh_rreg_lh,
1470                                             lh_tgtp->mlh_rreg_mode,
1471                                             MDS_INODELOCK_UPDATE);
1472                 if (rc != 0) {
1473                         lh_tgtp = NULL;
1474                         GOTO(out_put_new, rc);
1475                 }
1476         } else {
1477                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1478                                        rr->rr_fid2);
1479                 if (IS_ERR(mnew))
1480                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1481                 if (!mdt_object_remote(mnew)) {
1482                         CERROR("%s: Migration "DFID" is on this MDT:"
1483                                " rc = %d\n", mdt_obd_name(info->mti_mdt),
1484                                PFID(rr->rr_fid2), -EXDEV);
1485                         GOTO(out_put_new, rc = -EXDEV);
1486                 }
1487         }
1488
1489         /* 5: migrate it */
1490         mdt_reint_init_ma(info, ma);
1491
1492         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1493                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1494
1495         rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1496                          mdt_object_child(mold), &rr->rr_name,
1497                          mdt_object_child(mnew), ma);
1498         if (rc != 0)
1499                 GOTO(out_unlock_new, rc);
1500 out_unlock_new:
1501         if (lh_tgtp != NULL)
1502                 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1503 out_put_new:
1504         if (mnew)
1505                 mdt_object_put(info->mti_env, mnew);
1506 out_unlock_child:
1507         mdt_object_unlock(info, mold, lh_childp, rc);
1508 out_unlock_list:
1509         mdt_unlock_list(info, &lock_list, rc);
1510 out_put_child:
1511         mdt_object_put(info->mti_env, mold);
1512 out_unlock_parent:
1513         mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1514 out_put_parent:
1515         mdt_object_put(info->mti_env, msrcdir);
1516
1517         RETURN(rc);
1518 }
1519
1520 /*
1521  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1522  * 2 - src child; 3 - tgt child.
1523  * Update on disk version of src child.
1524  */
1525 /**
1526  * For DNE phase I, only these renames are allowed
1527  *      mv src_p/src_c tgt_p/tgt_c
1528  * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1529  * 2. src_p and tgt_p are same directory, and tgt_c does not
1530  *    exists. In this case, all of modification will happen
1531  *    in the MDT where ithesource parent is, only one remote
1532  *    update is needed, i.e. set c_time/m_time on the child.
1533  *    And tgt_c will be still in the same MDT as the original
1534  *    src_c.
1535  */
1536 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1537                                      struct mdt_lock_handle *lhc)
1538 {
1539         struct mdt_reint_record *rr = &info->mti_rr;
1540         struct md_attr          *ma = &info->mti_attr;
1541         struct ptlrpc_request   *req = mdt_info_req(info);
1542         struct mdt_object       *msrcdir;
1543         struct mdt_object       *mtgtdir;
1544         struct mdt_object       *mold;
1545         struct mdt_object       *mnew = NULL;
1546         struct mdt_lock_handle  *lh_srcdirp;
1547         struct mdt_lock_handle  *lh_tgtdirp;
1548         struct mdt_lock_handle  *lh_oldp = NULL;
1549         struct mdt_lock_handle  *lh_newp = NULL;
1550         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1551         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1552         int                      rc;
1553         ENTRY;
1554
1555         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1556                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1557                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1558
1559         /* step 1: lock the source dir. */
1560         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1561         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1562         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1563                                        MDS_INODELOCK_UPDATE);
1564         if (IS_ERR(msrcdir))
1565                 RETURN(PTR_ERR(msrcdir));
1566
1567         rc = mdt_version_get_check_save(info, msrcdir, 0);
1568         if (rc)
1569                 GOTO(out_unlock_source, rc);
1570
1571         /* step 2: find & lock the target dir. */
1572         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1573         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1574         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1575                 mdt_object_get(info->mti_env, msrcdir);
1576                 mtgtdir = msrcdir;
1577                 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1578                         rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1579                                          MDS_INODELOCK_UPDATE);
1580                         if (rc != 0)
1581                                 GOTO(out_unlock_source, rc);
1582                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1583                 }
1584         } else {
1585                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1586                                           rr->rr_fid2);
1587                 if (IS_ERR(mtgtdir))
1588                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1589
1590                 /* check early, the real version will be saved after locking */
1591                 rc = mdt_version_get_check(info, mtgtdir, 1);
1592                 if (rc)
1593                         GOTO(out_put_target, rc);
1594
1595                 if (unlikely(mdt_object_remote(mtgtdir))) {
1596                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1597                                "on different MDTs\n", PFID(rr->rr_fid1),
1598                                PFID(rr->rr_fid2));
1599                         GOTO(out_put_target, rc = -EXDEV);
1600                 } else {
1601                         if (likely(mdt_object_exists(mtgtdir))) {
1602                                 /* we lock the target dir if it is local */
1603                                 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1604                                                      MDS_INODELOCK_UPDATE,
1605                                                      MDT_LOCAL_LOCK);
1606                                 if (rc != 0)
1607                                         GOTO(out_put_target, rc);
1608                                 /* get and save correct version after locking */
1609                                 mdt_version_get_save(info, mtgtdir, 1);
1610                         } else {
1611                                 GOTO(out_put_target, rc = -ESTALE);
1612                         }
1613                 }
1614         }
1615
1616         /* step 3: find & lock the old object. */
1617         fid_zero(old_fid);
1618         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1619         if (rc != 0)
1620                 GOTO(out_unlock_target, rc);
1621
1622         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1623                 GOTO(out_unlock_target, rc = -EINVAL);
1624
1625         if (!fid_is_md_operative(old_fid))
1626                 GOTO(out_unlock_target, rc = -EPERM);
1627
1628         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1629         if (IS_ERR(mold))
1630                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1631
1632         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1633         /* save version after locking */
1634         mdt_version_get_save(info, mold, 2);
1635         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1636
1637         /* step 4: find & lock the new object. */
1638         /* new target object may not exist now */
1639         /* lookup with version checking */
1640         fid_zero(new_fid);
1641         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1642                                       3);
1643         if (rc == 0) {
1644                 /* the new_fid should have been filled at this moment */
1645                 if (lu_fid_eq(old_fid, new_fid))
1646                         GOTO(out_put_old, rc);
1647
1648                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1649                     lu_fid_eq(new_fid, rr->rr_fid2))
1650                         GOTO(out_put_old, rc = -EINVAL);
1651
1652                 if (!fid_is_md_operative(new_fid))
1653                         GOTO(out_put_old, rc = -EPERM);
1654
1655                 if (mdt_object_remote(mold)) {
1656                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1657                                PFID(old_fid));
1658                         GOTO(out_put_old, rc = -EXDEV);
1659                 }
1660
1661                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1662                 if (IS_ERR(mnew))
1663                         GOTO(out_put_old, rc = PTR_ERR(mnew));
1664
1665                 if (mdt_object_remote(mnew)) {
1666                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1667                                PFID(new_fid));
1668                         GOTO(out_put_new, rc = -EXDEV);
1669                 }
1670
1671                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1672                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1673                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1674                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1675                 if (rc != 0)
1676                         GOTO(out_put_new, rc);
1677
1678                 /* We used to acquire MDS_INODELOCK_FULL here but we
1679                  * can't do this now because a running HSM restore on
1680                  * the rename onto victim will hold the layout
1681                  * lock. See LU-4002. */
1682
1683                 lh_newp = &info->mti_lh[MDT_LH_NEW];
1684                 mdt_lock_reg_init(lh_newp, LCK_EX);
1685                 rc = mdt_object_lock(info, mnew, lh_newp,
1686                                      MDS_INODELOCK_LOOKUP |
1687                                      MDS_INODELOCK_UPDATE,
1688                                      MDT_LOCAL_LOCK);
1689                 if (rc != 0)
1690                         GOTO(out_unlock_old, rc);
1691
1692                 /* get and save version after locking */
1693                 mdt_version_get_save(info, mnew, 3);
1694                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1695         } else if (rc != -EREMOTE && rc != -ENOENT) {
1696                 GOTO(out_put_old, rc);
1697         } else {
1698                 /* If mnew does not exist and mold are remote directory,
1699                  * it only allows rename if they are under same directory */
1700                 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1701                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1702                                PFID(old_fid));
1703                         GOTO(out_put_old, rc = -EXDEV);
1704                 }
1705
1706                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1707                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1708                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1709                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1710                 if (rc != 0)
1711                         GOTO(out_put_old, rc);
1712
1713                 mdt_enoent_version_save(info, 3);
1714         }
1715
1716         /* step 5: rename it */
1717         mdt_reint_init_ma(info, ma);
1718
1719         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1720                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1721
1722         /* Check if @dst is subdir of @src. */
1723         rc = mdt_rename_sanity(info, old_fid);
1724         if (rc)
1725                 GOTO(out_unlock_new, rc);
1726
1727         if (mnew != NULL)
1728                 mutex_lock(&mnew->mot_lov_mutex);
1729
1730         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1731                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1732                         mnew != NULL ? mdt_object_child(mnew) : NULL,
1733                         &rr->rr_tgt_name, ma);
1734
1735         if (mnew != NULL)
1736                 mutex_unlock(&mnew->mot_lov_mutex);
1737
1738         /* handle last link of tgt object */
1739         if (rc == 0) {
1740                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1741                 if (mnew)
1742                         mdt_handle_last_unlink(info, mnew, ma);
1743
1744                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1745                                          msrcdir, mtgtdir);
1746         }
1747
1748         EXIT;
1749 out_unlock_new:
1750         if (mnew != NULL)
1751                 mdt_object_unlock(info, mnew, lh_newp, rc);
1752 out_unlock_old:
1753         mdt_object_unlock(info, mold, lh_oldp, rc);
1754 out_put_new:
1755         if (mnew != NULL)
1756                 mdt_object_put(info->mti_env, mnew);
1757 out_put_old:
1758         mdt_object_put(info->mti_env, mold);
1759 out_unlock_target:
1760         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1761 out_put_target:
1762         mdt_object_put(info->mti_env, mtgtdir);
1763 out_unlock_source:
1764         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1765         return rc;
1766 }
1767
1768 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1769                                        struct mdt_lock_handle *lhc,
1770                                        enum mdt_rename_lock rename_lock)
1771 {
1772         struct mdt_reint_record *rr = &info->mti_rr;
1773         struct ptlrpc_request   *req = mdt_info_req(info);
1774         struct lustre_handle    rename_lh = { 0 };
1775         int                     rc;
1776         ENTRY;
1777
1778         if (info->mti_dlm_req)
1779                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1780
1781         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1) ||
1782             fid_is_obf(rr->rr_fid2) || fid_is_dot_lustre(rr->rr_fid2))
1783                 RETURN(-EPERM);
1784
1785         rc = mdt_rename_lock(info, &rename_lh, rename_lock);
1786         if (rc != 0) {
1787                 CERROR("%s: can't lock FS for rename: rc  = %d\n",
1788                        mdt_obd_name(info->mti_mdt), rc);
1789                 RETURN(rc);
1790         }
1791
1792         if (rename_lock == MRL_RENAME)
1793                 rc = mdt_reint_rename_internal(info, lhc);
1794         else
1795                 rc = mdt_reint_migrate_internal(info, lhc);
1796
1797         if (lustre_handle_is_used(&rename_lh))
1798                 mdt_rename_unlock(&rename_lh);
1799
1800         RETURN(rc);
1801 }
1802
1803 static int mdt_reint_rename(struct mdt_thread_info *info,
1804                             struct mdt_lock_handle *lhc)
1805 {
1806         return mdt_reint_rename_or_migrate(info, lhc, MRL_RENAME);
1807 }
1808
1809 static int mdt_reint_migrate(struct mdt_thread_info *info,
1810                             struct mdt_lock_handle *lhc)
1811 {
1812         return mdt_reint_rename_or_migrate(info, lhc, MRL_MIGRATE);
1813 }
1814
1815 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1816                            struct mdt_lock_handle *lhc);
1817
1818 static mdt_reinter reinters[REINT_MAX] = {
1819         [REINT_SETATTR]  = mdt_reint_setattr,
1820         [REINT_CREATE]   = mdt_reint_create,
1821         [REINT_LINK]     = mdt_reint_link,
1822         [REINT_UNLINK]   = mdt_reint_unlink,
1823         [REINT_RENAME]   = mdt_reint_rename,
1824         [REINT_OPEN]     = mdt_reint_open,
1825         [REINT_SETXATTR] = mdt_reint_setxattr,
1826         [REINT_RMENTRY]  = mdt_reint_unlink,
1827         [REINT_MIGRATE]   = mdt_reint_migrate,
1828 };
1829
1830 int mdt_reint_rec(struct mdt_thread_info *info,
1831                   struct mdt_lock_handle *lhc)
1832 {
1833         int rc;
1834         ENTRY;
1835
1836         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1837
1838         RETURN(rc);
1839 }