Whamcloud - gitweb
LU-11025 dne: support directory restripe
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_reint.c
33  *
34  * Lustre Metadata Target (mdt) reintegration routines
35  *
36  * Author: Peter Braam <braam@clusterfs.com>
37  * Author: Andreas Dilger <adilger@clusterfs.com>
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Huang Hua <huanghua@clusterfs.com>
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <lprocfs_status.h>
46 #include "mdt_internal.h"
47 #include <lustre_lmv.h>
48
49 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
50                                      struct md_attr *ma)
51 {
52         ma->ma_need = MA_INODE;
53         ma->ma_valid = 0;
54 }
55
56 /**
57  * Get version of object by fid.
58  *
59  * Return real version or ENOENT_VERSION if object doesn't exist
60  */
61 static void mdt_obj_version_get(struct mdt_thread_info *info,
62                                 struct mdt_object *o, __u64 *version)
63 {
64         LASSERT(o);
65
66         if (mdt_object_exists(o) && !mdt_object_remote(o) &&
67             !fid_is_obf(mdt_object_fid(o)))
68                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
69         else
70                 *version = ENOENT_VERSION;
71         CDEBUG(D_INODE, "FID "DFID" version is %#llx\n",
72                PFID(mdt_object_fid(o)), *version);
73 }
74
75 /**
76  * Check version is correct.
77  *
78  * Should be called only during replay.
79  */
80 static int mdt_version_check(struct ptlrpc_request *req,
81                              __u64 version, int idx)
82 {
83         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
84         ENTRY;
85
86         if (!exp_connect_vbr(req->rq_export))
87                 RETURN(0);
88
89         LASSERT(req_is_replay(req));
90         /** VBR: version is checked always because costs nothing */
91         LASSERT(idx < PTLRPC_NUM_VERSIONS);
92         /** Sanity check for malformed buffers */
93         if (pre_ver == NULL) {
94                 CERROR("No versions in request buffer\n");
95                 spin_lock(&req->rq_export->exp_lock);
96                 req->rq_export->exp_vbr_failed = 1;
97                 spin_unlock(&req->rq_export->exp_lock);
98                 RETURN(-EOVERFLOW);
99         } else if (pre_ver[idx] != version) {
100                 CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
101                        pre_ver[idx], version);
102                 spin_lock(&req->rq_export->exp_lock);
103                 req->rq_export->exp_vbr_failed = 1;
104                 spin_unlock(&req->rq_export->exp_lock);
105                 RETURN(-EOVERFLOW);
106         }
107         RETURN(0);
108 }
109
110 /**
111  * Save pre-versions in reply.
112  */
113 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
114                              int idx)
115 {
116         __u64 *reply_ver;
117
118         if (!exp_connect_vbr(req->rq_export))
119                 return;
120
121         LASSERT(!req_is_replay(req));
122         LASSERT(req->rq_repmsg != NULL);
123         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
124         if (reply_ver)
125                 reply_ver[idx] = version;
126 }
127
128 /**
129  * Save enoent version, it is needed when it is obvious that object doesn't
130  * exist, e.g. child during create.
131  */
132 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
133 {
134         /* save version of file name for replay, it must be ENOENT here */
135         if (!req_is_replay(mdt_info_req(info))) {
136                 info->mti_ver[idx] = ENOENT_VERSION;
137                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
138         }
139 }
140
141 /**
142  * Get version from disk and save in reply buffer.
143  *
144  * Versions are saved in reply only during normal operations not replays.
145  */
146 void mdt_version_get_save(struct mdt_thread_info *info,
147                           struct mdt_object *mto, int idx)
148 {
149         /* don't save versions during replay */
150         if (!req_is_replay(mdt_info_req(info))) {
151                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
152                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
153         }
154 }
155
156 /**
157  * Get version from disk and check it, no save in reply.
158  */
159 int mdt_version_get_check(struct mdt_thread_info *info,
160                           struct mdt_object *mto, int idx)
161 {
162         /* only check versions during replay */
163         if (!req_is_replay(mdt_info_req(info)))
164                 return 0;
165
166         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
167         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
168 }
169
170 /**
171  * Get version from disk and check if recovery or just save.
172  */
173 int mdt_version_get_check_save(struct mdt_thread_info *info,
174                                struct mdt_object *mto, int idx)
175 {
176         int rc = 0;
177
178         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
179         if (req_is_replay(mdt_info_req(info)))
180                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
181                                        idx);
182         else
183                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
184         return rc;
185 }
186
187 /**
188  * Lookup with version checking.
189  *
190  * This checks version of 'name'. Many reint functions uses 'name' for child not
191  * FID, therefore we need to get object by name and check its version.
192  */
193 static int mdt_lookup_version_check(struct mdt_thread_info *info,
194                                     struct mdt_object *p,
195                                     const struct lu_name *lname,
196                                     struct lu_fid *fid, int idx)
197 {
198         int rc, vbrc;
199
200         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
201                         &info->mti_spec);
202         /* Check version only during replay */
203         if (!req_is_replay(mdt_info_req(info)))
204                 return rc;
205
206         info->mti_ver[idx] = ENOENT_VERSION;
207         if (rc == 0) {
208                 struct mdt_object *child;
209                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
210                 if (likely(!IS_ERR(child))) {
211                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
212                         mdt_object_put(info->mti_env, child);
213                 }
214         }
215         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
216         return vbrc ? vbrc : rc;
217
218 }
219
220 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
221                              struct mdt_object *obj,
222                              struct ldlm_enqueue_info *einfo,
223                              int decref)
224 {
225         union ldlm_policy_data *policy = &mti->mti_policy;
226         struct mdt_lock_handle *lh = &mti->mti_lh[MDT_LH_LOCAL];
227         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
228         int i;
229
230         LASSERT(S_ISDIR(obj->mot_header.loh_attr));
231         LASSERT(slave_locks);
232
233         memset(policy, 0, sizeof(*policy));
234         policy->l_inodebits.bits = einfo->ei_inodebits;
235         mdt_lock_handle_init(lh);
236         mdt_lock_reg_init(lh, einfo->ei_mode);
237         for (i = 0; i < slave_locks->ha_count; i++) {
238                 if (test_bit(i, (void *)slave_locks->ha_map))
239                         lh->mlh_rreg_lh = slave_locks->ha_handles[i];
240                 else
241                         lh->mlh_reg_lh = slave_locks->ha_handles[i];
242                 mdt_object_unlock(mti, NULL, lh, decref);
243                 slave_locks->ha_handles[i].cookie = 0ull;
244         }
245
246         return mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
247                                 policy);
248 }
249
250 static inline int mdt_object_striped(struct mdt_thread_info *mti,
251                                      struct mdt_object *obj)
252 {
253         struct lu_device *bottom_dev;
254         struct lu_object *bottom_obj;
255         int rc;
256
257         if (!S_ISDIR(obj->mot_header.loh_attr))
258                 return 0;
259
260         /* getxattr from bottom obj to avoid reading in shard FIDs */
261         bottom_dev = dt2lu_dev(mti->mti_mdt->mdt_bottom);
262         bottom_obj = lu_object_find_slice(mti->mti_env, bottom_dev,
263                                           mdt_object_fid(obj), NULL);
264         if (IS_ERR(bottom_obj))
265                 return PTR_ERR(bottom_obj);
266
267         rc = dt_xattr_get(mti->mti_env, lu2dt(bottom_obj), &LU_BUF_NULL,
268                           XATTR_NAME_LMV);
269         lu_object_put(mti->mti_env, bottom_obj);
270
271         return (rc > 0) ? 1 : (rc == -ENODATA) ? 0 : rc;
272 }
273
274 /**
275  * Lock slave stripes if necessary, the lock handles of slave stripes
276  * will be stored in einfo->ei_cbdata.
277  **/
278 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
279                            enum ldlm_mode mode, __u64 ibits,
280                            struct ldlm_enqueue_info *einfo)
281 {
282         union ldlm_policy_data *policy = &mti->mti_policy;
283
284         LASSERT(S_ISDIR(obj->mot_header.loh_attr));
285
286         einfo->ei_type = LDLM_IBITS;
287         einfo->ei_mode = mode;
288         einfo->ei_cb_bl = mdt_remote_blocking_ast;
289         einfo->ei_cb_local_bl = mdt_blocking_ast;
290         einfo->ei_cb_cp = ldlm_completion_ast;
291         einfo->ei_enq_slave = 1;
292         einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
293         einfo->ei_inodebits = ibits;
294         memset(policy, 0, sizeof(*policy));
295         policy->l_inodebits.bits = ibits;
296
297         return mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
298                               policy);
299 }
300
301 int mdt_reint_striped_lock(struct mdt_thread_info *info,
302                            struct mdt_object *o,
303                            struct mdt_lock_handle *lh,
304                            __u64 ibits,
305                            struct ldlm_enqueue_info *einfo,
306                            bool cos_incompat)
307 {
308         int rc;
309
310         LASSERT(!mdt_object_remote(o));
311
312         memset(einfo, 0, sizeof(*einfo));
313
314         rc = mdt_reint_object_lock(info, o, lh, ibits, cos_incompat);
315         if (rc)
316                 return rc;
317
318         rc = mdt_object_striped(info, o);
319         if (rc != 1) {
320                 if (rc < 0)
321                         mdt_object_unlock(info, o, lh, rc);
322                 return rc;
323         }
324
325         rc = mdt_lock_slaves(info, o, lh->mlh_reg_mode, ibits, einfo);
326         if (rc) {
327                 mdt_object_unlock(info, o, lh, rc);
328                 if (rc == -EIO && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME))
329                         rc = 0;
330         }
331
332         return rc;
333 }
334
335 void mdt_reint_striped_unlock(struct mdt_thread_info *info,
336                               struct mdt_object *o,
337                               struct mdt_lock_handle *lh,
338                               struct ldlm_enqueue_info *einfo, int decref)
339 {
340         if (einfo->ei_cbdata)
341                 mdt_unlock_slaves(info, o, einfo, decref);
342         mdt_object_unlock(info, o, lh, decref);
343 }
344
345 static int mdt_restripe(struct mdt_thread_info *info,
346                         struct mdt_object *pobj,
347                         const struct lu_name *lname,
348                         const struct lu_fid *tfid,
349                         struct md_op_spec *spec,
350                         struct md_attr *ma)
351 {
352         const struct lu_env *env = info->mti_env;
353         struct mdt_device *mdt = info->mti_mdt;
354         struct lu_fid *cfid = &info->mti_tmp_fid2;
355         struct lmv_user_md *lum = spec->u.sp_ea.eadata;
356         struct md_layout_change *mlc = &info->mti_mlc;
357         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
358         struct lmv_mds_md_v1 *lmv;
359         struct mdt_object *child;
360         struct mdt_object *tobj = NULL;
361         struct mdt_lock_handle *lhp = NULL;
362         struct mdt_lock_handle *lhc;
363         struct mdt_body *repbody;
364         u32 lmv_stripe_count = 0;
365         int rc;
366
367         ENTRY;
368
369         if (!mdt->mdt_enable_dir_restripe)
370                 RETURN(-EPERM);
371
372         /* mti_big_lmm is used to save LMV, but it may be uninitialized. */
373         if (unlikely(!info->mti_big_lmm)) {
374                 info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
375                 OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
376                 if (!info->mti_big_lmm)
377                         RETURN(-ENOMEM);
378         }
379
380         rc = mdt_version_get_check_save(info, pobj, 0);
381         if (rc)
382                 RETURN(rc);
383
384         ma->ma_lmv = info->mti_big_lmm;
385         ma->ma_lmv_size = info->mti_big_lmmsize;
386         ma->ma_valid = 0;
387         rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
388         if (rc)
389                 RETURN(rc);
390
391         if (ma->ma_valid & MA_LMV) {
392                 /* don't allow restripe if parent dir layout is changing */
393                 lmv = &ma->ma_lmv->lmv_md_v1;
394                 if (!lmv_is_sane(lmv))
395                         RETURN(-EBADF);
396
397                 if (lmv_is_layout_changing(lmv))
398                         RETURN(-EBUSY);
399         }
400
401         lhp = &info->mti_lh[MDT_LH_PARENT];
402         mdt_lock_pdo_init(lhp, LCK_PW, lname);
403         rc = mdt_reint_object_lock(info, pobj, lhp, MDS_INODELOCK_UPDATE, true);
404         if (rc)
405                 RETURN(rc);
406
407         fid_zero(cfid);
408         rc = mdt_lookup_version_check(info, pobj, lname, cfid, 1);
409         if (rc)
410                 GOTO(unlock_parent, rc);
411
412         child = mdt_object_find(info->mti_env, mdt, cfid);
413         if (IS_ERR(child))
414                 GOTO(unlock_parent, rc = PTR_ERR(child));
415
416         if (!mdt_object_exists(child))
417                 GOTO(out_child, rc = -ENOENT);
418
419         if (mdt_object_remote(child)) {
420                 struct mdt_body *repbody;
421
422                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
423                 if (!repbody)
424                         GOTO(out_child, rc = -EPROTO);
425
426                 repbody->mbo_fid1 = *cfid;
427                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
428                 GOTO(out_child, rc = -EREMOTE);
429         }
430
431         /* lock object */
432         lhc = &info->mti_lh[MDT_LH_CHILD];
433         mdt_lock_reg_init(lhc, LCK_EX);
434
435         /* enqueue object remote LOOKUP lock */
436         if (mdt_object_remote(pobj)) {
437                 rc = mdt_remote_object_lock(info, pobj, cfid, &lhc->mlh_rreg_lh,
438                                             lhc->mlh_rreg_mode,
439                                             MDS_INODELOCK_LOOKUP, false);
440                 if (rc != ELDLM_OK)
441                         GOTO(out_child, rc);
442         }
443
444         rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
445                                     true);
446         if (rc)
447                 GOTO(unlock_child, rc);
448
449         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
450         rc = mdt_version_get_check_save(info, child, 1);
451         if (rc)
452                 GOTO(unlock_child, rc);
453
454         ma->ma_valid = 0;
455         rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
456         if (rc)
457                 GOTO(unlock_child, rc);
458
459         if (ma->ma_valid & MA_LMV) {
460                 lmv = &ma->ma_lmv->lmv_md_v1;
461                 if (!lmv_is_sane(lmv))
462                         GOTO(unlock_child, rc = -EBADF);
463
464                 /* don't allow restripe if dir layout is changing */
465                 if (lmv_is_layout_changing(lmv))
466                         GOTO(unlock_child, rc = -EBUSY);
467
468                 /* check whether stripe count and hash unchanged */
469                 if (lum->lum_stripe_count == lmv->lmv_stripe_count &&
470                     lum->lum_hash_type == lmv->lmv_hash_type)
471                         GOTO(unlock_child, rc = -EALREADY);
472
473                 lmv_stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
474         } else if (le32_to_cpu(lum->lum_stripe_count) < 2) {
475                 /* stripe count unchanged for plain directory */
476                 GOTO(unlock_child, rc = -EALREADY);
477         }
478
479         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
480         if (!repbody)
481                 GOTO(unlock_child, rc = -EPROTO);
482
483         if (le32_to_cpu(lum->lum_stripe_count) > lmv_stripe_count) {
484                 /* split */
485                 ma->ma_need = MA_INODE;
486                 ma->ma_valid = 0;
487                 rc = mdt_attr_get_complex(info, child, ma);
488                 if (rc)
489                         GOTO(unlock_child, rc);
490
491                 if (!(ma->ma_valid & MA_INODE))
492                         GOTO(unlock_child, rc = -EBADF);
493
494                 if (!lmv_stripe_count) {
495                         /* if child is plain directory, allocate @tobj as the
496                          * master object, and make child the first stripe of
497                          * @tobj.
498                          */
499                         tobj = mdt_object_new(info->mti_env, mdt, tfid);
500                         if (unlikely(IS_ERR(tobj)))
501                                 GOTO(unlock_child, rc = PTR_ERR(tobj));
502                 }
503
504                 mlc->mlc_opc = MD_LAYOUT_SPLIT;
505                 mlc->mlc_parent = mdt_object_child(pobj);
506                 mlc->mlc_target = tobj ? mdt_object_child(tobj) : NULL;
507                 mlc->mlc_attr = &ma->ma_attr;
508                 mlc->mlc_name = lname;
509                 mlc->mlc_spec = spec;
510                 rc = mo_layout_change(env, mdt_object_child(child), mlc);
511                 if (rc)
512                         GOTO(out_tobj, rc);
513         } else {
514                 /* merge only needs to override LMV */
515                 struct lu_buf *buf = &info->mti_buf;
516                 __u32 version;
517
518                 LASSERT(ma->ma_valid & MA_LMV);
519                 lmv = &ma->ma_lmv->lmv_md_v1;
520                 version = cpu_to_le32(lmv->lmv_layout_version);
521
522                 /* adjust 0 to 1 */
523                 if (lum->lum_stripe_count == 0)
524                         lum->lum_stripe_count = cpu_to_le32(1);
525
526                 lmv->lmv_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MERGE |
527                                                   LMV_HASH_FLAG_MIGRATION);
528                 lmv->lmv_merge_offset = lum->lum_stripe_count;
529                 lmv->lmv_merge_hash = lum->lum_hash_type;
530                 lmv->lmv_layout_version = cpu_to_le32(++version);
531
532                 buf->lb_buf = lmv;
533                 buf->lb_len = sizeof(*lmv);
534                 rc = mo_xattr_set(env, mdt_object_child(child), buf,
535                                   XATTR_NAME_LMV, LU_XATTR_REPLACE);
536                 if (rc)
537                         GOTO(unlock_child, rc);
538         }
539
540         ma->ma_need = MA_INODE;
541         ma->ma_valid = 0;
542         rc = mdt_attr_get_complex(info, tobj ? tobj : child, ma);
543         if (rc)
544                 GOTO(out_tobj, rc);
545
546         if (!(ma->ma_valid & MA_INODE))
547                 GOTO(out_tobj, rc = -EBADF);
548
549         mdt_pack_attr2body(info, repbody, &ma->ma_attr,
550                            mdt_object_fid(tobj ? tobj : child));
551         EXIT;
552
553 out_tobj:
554         if (tobj)
555                 mdt_object_put(env, tobj);
556 unlock_child:
557         mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
558 out_child:
559         mdt_object_put(env, child);
560 unlock_parent:
561         mdt_object_unlock(info, pobj, lhp, rc);
562
563         return rc;
564 }
565
566 /*
567  * VBR: we save three versions in reply:
568  * 0 - parent. Check that parent version is the same during replay.
569  * 1 - name. Version of 'name' if file exists with the same name or
570  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
571  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
572  * check.
573  */
574 static int mdt_create(struct mdt_thread_info *info)
575 {
576         struct mdt_device *mdt = info->mti_mdt;
577         struct mdt_object *parent;
578         struct mdt_object *child;
579         struct mdt_lock_handle *lh;
580         struct mdt_body *repbody;
581         struct md_attr *ma = &info->mti_attr;
582         struct mdt_reint_record *rr = &info->mti_rr;
583         struct md_op_spec *spec = &info->mti_spec;
584         bool restripe = false;
585         int rc;
586         ENTRY;
587
588         DEBUG_REQ(D_INODE, mdt_info_req(info),
589                   "Create ("DNAME"->"DFID") in "DFID,
590                   PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
591
592         if (!fid_is_md_operative(rr->rr_fid1))
593                 RETURN(-EPERM);
594
595         if (S_ISDIR(ma->ma_attr.la_mode) &&
596             spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
597                 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
598                 struct lu_ucred *uc = mdt_ucred(info);
599                 struct obd_export *exp = mdt_info_req(info)->rq_export;
600
601                 /* Only new clients can create remote dir( >= 2.4) and
602                  * striped dir(>= 2.6), old client will return -ENOTSUPP */
603                 if (!mdt_is_dne_client(exp))
604                         RETURN(-ENOTSUPP);
605
606                 if (le32_to_cpu(lum->lum_stripe_count) > 1) {
607                         if (!mdt_is_striped_client(exp))
608                                 RETURN(-ENOTSUPP);
609
610                         if (!mdt->mdt_enable_striped_dir)
611                                 RETURN(-EPERM);
612                 } else if (!mdt->mdt_enable_remote_dir) {
613                         RETURN(-EPERM);
614                 }
615
616                 if ((!(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH)) &&
617                     (le32_to_cpu(lum->lum_hash_type) & LMV_HASH_TYPE_MASK) ==
618                     LMV_HASH_TYPE_CRUSH)
619                         RETURN(-EPROTO);
620
621                 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
622                     uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
623                     mdt->mdt_enable_remote_dir_gid != -1)
624                         RETURN(-EPERM);
625
626                 /* restripe if later found dir exists */
627                 if (le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT)
628                         restripe = true;
629         }
630
631         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
632
633         parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
634         if (IS_ERR(parent))
635                 RETURN(PTR_ERR(parent));
636
637         if (!mdt_object_exists(parent))
638                 GOTO(put_parent, rc = -ENOENT);
639
640         /*
641          * LU-10235: check if name exists locklessly first to avoid massive
642          * lock recalls on existing directories.
643          */
644         rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
645                                       &info->mti_tmp_fid1, 1);
646         if (rc == 0) {
647                 if (!restripe)
648                         GOTO(put_parent, rc = -EEXIST);
649
650                 rc = mdt_restripe(info, parent, &rr->rr_name, rr->rr_fid2, spec,
651                                   ma);
652         }
653
654         /* -ENOENT is expected here */
655         if (rc != -ENOENT)
656                 GOTO(put_parent, rc);
657
658         /* save version of file name for replay, it must be ENOENT here */
659         mdt_enoent_version_save(info, 1);
660
661         OBD_RACE(OBD_FAIL_MDS_CREATE_RACE);
662
663         lh = &info->mti_lh[MDT_LH_PARENT];
664         mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
665         rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
666         if (rc)
667                 GOTO(put_parent, rc);
668
669         if (!mdt_object_remote(parent)) {
670                 rc = mdt_version_get_check_save(info, parent, 0);
671                 if (rc)
672                         GOTO(unlock_parent, rc);
673         }
674
675         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
676         if (unlikely(IS_ERR(child)))
677                 GOTO(unlock_parent, rc = PTR_ERR(child));
678
679         ma->ma_need = MA_INODE;
680         ma->ma_valid = 0;
681
682         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
683                         OBD_FAIL_MDS_REINT_CREATE_WRITE);
684
685         /* Version of child will be updated on disk. */
686         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
687         rc = mdt_version_get_check_save(info, child, 2);
688         if (rc)
689                 GOTO(put_child, rc);
690
691         /* Let lower layer know current lock mode. */
692         info->mti_spec.sp_cr_mode = mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
693
694         /*
695          * Do not perform lookup sanity check. We know that name does
696          * not exist.
697          */
698         info->mti_spec.sp_cr_lookup = 0;
699         info->mti_spec.sp_feat = &dt_directory_features;
700
701         rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
702                         mdt_object_child(child), &info->mti_spec, ma);
703         if (rc == 0)
704                 rc = mdt_attr_get_complex(info, child, ma);
705
706         if (rc < 0)
707                 GOTO(put_child, rc);
708
709         /*
710          * On DNE, we need to eliminate dependey between 'mkdir a' and
711          * 'mkdir a/b' if b is a striped directory, to achieve this, two
712          * things are done below:
713          * 1. save child and slaves lock.
714          * 2. if the child is a striped directory, relock parent so to
715          *    compare against with COS locks to ensure parent was
716          *    committed to disk.
717          */
718         if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
719                 struct mdt_lock_handle *lhc;
720                 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
721                 bool cos_incompat;
722
723                 rc = mdt_object_striped(info, child);
724                 if (rc < 0)
725                         GOTO(put_child, rc);
726
727                 cos_incompat = rc;
728                 if (cos_incompat) {
729                         if (!mdt_object_remote(parent)) {
730                                 mdt_object_unlock(info, parent, lh, 1);
731                                 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
732                                 rc = mdt_reint_object_lock(info, parent, lh,
733                                                            MDS_INODELOCK_UPDATE,
734                                                            true);
735                                 if (rc)
736                                         GOTO(put_child, rc);
737                         }
738                 }
739
740                 lhc = &info->mti_lh[MDT_LH_CHILD];
741                 mdt_lock_handle_init(lhc);
742                 mdt_lock_reg_init(lhc, LCK_PW);
743                 rc = mdt_reint_striped_lock(info, child, lhc,
744                                             MDS_INODELOCK_UPDATE, einfo,
745                                             cos_incompat);
746                 if (rc)
747                         GOTO(put_child, rc);
748
749                 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
750         }
751
752         /* Return fid & attr to client. */
753         if (ma->ma_valid & MA_INODE)
754                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
755                                    mdt_object_fid(child));
756 put_child:
757         mdt_object_put(info->mti_env, child);
758 unlock_parent:
759         mdt_object_unlock(info, parent, lh, rc);
760 put_parent:
761         mdt_object_put(info->mti_env, parent);
762         RETURN(rc);
763 }
764
765 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
766                         struct md_attr *ma)
767 {
768         struct mdt_lock_handle  *lh;
769         int do_vbr = ma->ma_attr.la_valid &
770                         (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
771         __u64 lockpart = MDS_INODELOCK_UPDATE;
772         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
773         bool cos_incompat;
774         int rc;
775         ENTRY;
776
777         rc = mdt_object_striped(info, mo);
778         if (rc < 0)
779                 RETURN(rc);
780
781         cos_incompat = rc;
782
783         lh = &info->mti_lh[MDT_LH_PARENT];
784         mdt_lock_reg_init(lh, LCK_PW);
785
786         /* Even though the new MDT will grant PERM lock to the old
787          * client, but the old client will almost ignore that during
788          * So it needs to revoke both LOOKUP and PERM lock here, so
789          * both new and old client can cancel the dcache */
790         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
791                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
792
793         rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
794                                     cos_incompat);
795         if (rc != 0)
796                 RETURN(rc);
797
798         /* all attrs are packed into mti_attr in unpack_setattr */
799         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
800                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
801
802         /* VBR: update version if attr changed are important for recovery */
803         if (do_vbr) {
804                 /* update on-disk version of changed object */
805                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
806                 rc = mdt_version_get_check_save(info, mo, 0);
807                 if (rc)
808                         GOTO(out_unlock, rc);
809         }
810
811         /* Ensure constant striping during chown(). See LU-2789. */
812         if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
813                 mutex_lock(&mo->mot_lov_mutex);
814
815         /* all attrs are packed into mti_attr in unpack_setattr */
816         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
817
818         if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
819                 mutex_unlock(&mo->mot_lov_mutex);
820
821         if (rc != 0)
822                 GOTO(out_unlock, rc);
823         mdt_dom_obj_lvb_update(info->mti_env, mo, false);
824         EXIT;
825 out_unlock:
826         mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
827         return rc;
828 }
829
830 /**
831  * Check HSM flags and add HS_DIRTY flag if relevant.
832  *
833  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
834  * and is not RELEASED.
835  */
836 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
837                         struct md_attr *ma)
838 {
839         struct lu_ucred *uc = mdt_ucred(info);
840         cfs_cap_t cap_saved;
841         int rc;
842         ENTRY;
843
844         /* If the file was modified, add the dirty flag */
845         ma->ma_need = MA_HSM;
846         rc = mdt_attr_get_complex(info, mo, ma);
847         if (rc) {
848                 CERROR("file attribute read error for "DFID": %d.\n",
849                         PFID(mdt_object_fid(mo)), rc);
850                 RETURN(rc);
851         }
852
853         /* If an up2date copy exists in the backend, add dirty flag */
854         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
855             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
856                 ma->ma_hsm.mh_flags |= HS_DIRTY;
857
858                 /* Bump cap so that closes from non-owner writers can
859                  * set the HSM state to dirty. */
860                 cap_saved = uc->uc_cap;
861                 uc->uc_cap |= MD_CAP_TO_MASK(CFS_CAP_FOWNER);
862                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
863                 uc->uc_cap = cap_saved;
864                 if (rc)
865                         CERROR("file attribute change error for "DFID": %d\n",
866                                 PFID(mdt_object_fid(mo)), rc);
867         }
868
869         RETURN(rc);
870 }
871
872 static int mdt_reint_setattr(struct mdt_thread_info *info,
873                              struct mdt_lock_handle *lhc)
874 {
875         struct mdt_device *mdt = info->mti_mdt;
876         struct md_attr *ma = &info->mti_attr;
877         struct mdt_reint_record *rr = &info->mti_rr;
878         struct ptlrpc_request *req = mdt_info_req(info);
879         struct mdt_object *mo;
880         struct mdt_body *repbody;
881         int rc, rc2;
882         ENTRY;
883
884         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
885                   (unsigned int)ma->ma_attr.la_valid);
886
887         if (info->mti_dlm_req)
888                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
889
890         OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
891
892         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
893         mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
894         if (IS_ERR(mo))
895                 GOTO(out, rc = PTR_ERR(mo));
896
897         if (!mdt_object_exists(mo))
898                 GOTO(out_put, rc = -ENOENT);
899
900         if (mdt_object_remote(mo))
901                 GOTO(out_put, rc = -EREMOTE);
902
903         ma->ma_enable_chprojid_gid = mdt->mdt_enable_chprojid_gid;
904         /* revoke lease lock if size is going to be changed */
905         if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
906                      !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
907                      atomic_read(&mo->mot_lease_count) > 0)) {
908                 down_read(&mo->mot_open_sem);
909
910                 if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
911                         lhc = &info->mti_lh[MDT_LH_LOCAL];
912                         mdt_lock_reg_init(lhc, LCK_CW);
913
914                         rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
915                         if (rc != 0) {
916                                 up_read(&mo->mot_open_sem);
917                                 GOTO(out_put, rc);
918                         }
919
920                         /* revoke lease lock */
921                         mdt_object_unlock(info, mo, lhc, 1);
922                 }
923                 up_read(&mo->mot_open_sem);
924         }
925
926         if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
927                 /* Check write access for the O_TRUNC case */
928                 if (mdt_write_read(mo) < 0)
929                         GOTO(out_put, rc = -ETXTBSY);
930
931                 /* LU-10286: compatibility check for FLR.
932                  * Please check the comment in mdt_finish_open() for details */
933                 if (!exp_connect_flr(info->mti_exp) ||
934                     !exp_connect_overstriping(info->mti_exp)) {
935                         rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
936                         if (rc < 0 && rc != -ENODATA)
937                                 GOTO(out_put, rc);
938
939                         if (!exp_connect_flr(info->mti_exp)) {
940                                 if (rc > 0 &&
941                                     mdt_lmm_is_flr(info->mti_big_lmm))
942                                         GOTO(out_put, rc = -EOPNOTSUPP);
943                         }
944
945                         if (!exp_connect_overstriping(info->mti_exp)) {
946                                 if (rc > 0 &&
947                                     mdt_lmm_is_overstriping(info->mti_big_lmm))
948                                         GOTO(out_put, rc = -EOPNOTSUPP);
949                         }
950                 }
951
952                 /* For truncate, the file size sent from client
953                  * is believable, but the blocks are incorrect,
954                  * which makes the block size in LSOM attribute
955                  * inconsisent with the real block size.
956                  */
957                 rc = mdt_lsom_update(info, mo, true);
958                 if (rc)
959                         GOTO(out_put, rc);
960         }
961
962         if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
963                 if (ma->ma_valid & MA_LOV)
964                         GOTO(out_put, rc = -EPROTO);
965
966                 /* MDT supports FMD for regular files due to Data-on-MDT */
967                 if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
968                     ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
969                         tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
970                                        req->rq_xid);
971
972                 rc = mdt_attr_set(info, mo, ma);
973                 if (rc)
974                         GOTO(out_put, rc);
975         } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
976                    (ma->ma_valid & MA_INODE)) {
977                 struct lu_buf *buf = &info->mti_buf;
978                 struct lu_ucred *uc = mdt_ucred(info);
979                 struct mdt_lock_handle *lh;
980                 const char *name;
981                 __u64 lockpart = MDS_INODELOCK_XATTR;
982
983                 /* reject if either remote or striped dir is disabled */
984                 if (ma->ma_valid & MA_LMV) {
985                         if (!mdt->mdt_enable_remote_dir ||
986                             !mdt->mdt_enable_striped_dir)
987                                 GOTO(out_put, rc = -EPERM);
988
989                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
990                             uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
991                             mdt->mdt_enable_remote_dir_gid != -1)
992                                 GOTO(out_put, rc = -EPERM);
993                 }
994
995                 if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
996                         GOTO(out_put, rc = -ENOTDIR);
997
998                 if (ma->ma_attr.la_valid != 0)
999                         GOTO(out_put, rc = -EPROTO);
1000
1001                 if (ma->ma_valid & MA_LOV) {
1002                         buf->lb_buf = ma->ma_lmm;
1003                         buf->lb_len = ma->ma_lmm_size;
1004                         name = XATTR_NAME_LOV;
1005                 } else {
1006                         struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
1007
1008                         buf->lb_buf = lmu;
1009                         buf->lb_len = ma->ma_lmv_size;
1010                         name = XATTR_NAME_DEFAULT_LMV;
1011                         /* force client to update dir default layout */
1012                         lockpart |= MDS_INODELOCK_LOOKUP;
1013                 }
1014
1015                 lh = &info->mti_lh[MDT_LH_PARENT];
1016                 mdt_lock_reg_init(lh, LCK_PW);
1017
1018                 rc = mdt_object_lock(info, mo, lh, lockpart);
1019                 if (rc != 0)
1020                         GOTO(out_put, rc);
1021
1022                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
1023                                   name, 0);
1024
1025                 mdt_object_unlock(info, mo, lh, rc);
1026                 if (rc)
1027                         GOTO(out_put, rc);
1028         } else {
1029                 GOTO(out_put, rc = -EPROTO);
1030         }
1031
1032         /* If file data is modified, add the dirty flag */
1033         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
1034                 rc = mdt_add_dirty_flag(info, mo, ma);
1035
1036         ma->ma_need = MA_INODE;
1037         ma->ma_valid = 0;
1038         rc = mdt_attr_get_complex(info, mo, ma);
1039         if (rc != 0)
1040                 GOTO(out_put, rc);
1041
1042         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
1043
1044         EXIT;
1045 out_put:
1046         mdt_object_put(info->mti_env, mo);
1047 out:
1048         if (rc == 0)
1049                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
1050
1051         mdt_client_compatibility(info);
1052         rc2 = mdt_fix_reply(info);
1053         if (rc == 0)
1054                 rc = rc2;
1055         return rc;
1056 }
1057
1058 static int mdt_reint_create(struct mdt_thread_info *info,
1059                             struct mdt_lock_handle *lhc)
1060 {
1061         struct ptlrpc_request   *req = mdt_info_req(info);
1062         int                     rc;
1063         ENTRY;
1064
1065         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
1066                 RETURN(err_serious(-ESTALE));
1067
1068         if (info->mti_dlm_req)
1069                 ldlm_request_cancel(mdt_info_req(info),
1070                                     info->mti_dlm_req, 0, LATF_SKIP);
1071
1072         if (!lu_name_is_valid(&info->mti_rr.rr_name))
1073                 RETURN(-EPROTO);
1074
1075         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
1076         case S_IFDIR:
1077                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
1078                 break;
1079         case S_IFREG:
1080         case S_IFLNK:
1081         case S_IFCHR:
1082         case S_IFBLK:
1083         case S_IFIFO:
1084         case S_IFSOCK:
1085                 /* Special file should stay on the same node as parent. */
1086                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
1087                 break;
1088         default:
1089                 CERROR("%s: Unsupported mode %o\n",
1090                        mdt_obd_name(info->mti_mdt),
1091                        info->mti_attr.ma_attr.la_mode);
1092                 RETURN(err_serious(-EOPNOTSUPP));
1093         }
1094
1095         rc = mdt_create(info);
1096         RETURN(rc);
1097 }
1098
1099 /*
1100  * VBR: save parent version in reply and child version getting by its name.
1101  * Version of child is getting and checking during its lookup. If
1102  */
1103 static int mdt_reint_unlink(struct mdt_thread_info *info,
1104                             struct mdt_lock_handle *lhc)
1105 {
1106         struct mdt_reint_record *rr = &info->mti_rr;
1107         struct ptlrpc_request *req = mdt_info_req(info);
1108         struct md_attr *ma = &info->mti_attr;
1109         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1110         struct mdt_object *mp;
1111         struct mdt_object *mc;
1112         struct mdt_lock_handle *parent_lh;
1113         struct mdt_lock_handle *child_lh;
1114         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
1115         __u64 lock_ibits;
1116         bool cos_incompat = false;
1117         int no_name = 0;
1118         int rc;
1119
1120         ENTRY;
1121
1122         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
1123                   PNAME(&rr->rr_name));
1124
1125         if (info->mti_dlm_req)
1126                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1127
1128         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1129                 RETURN(err_serious(-ENOENT));
1130
1131         if (!fid_is_md_operative(rr->rr_fid1))
1132                 RETURN(-EPERM);
1133
1134         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1135         if (IS_ERR(mp))
1136                 RETURN(PTR_ERR(mp));
1137
1138         if (mdt_object_remote(mp)) {
1139                 cos_incompat = true;
1140         } else {
1141                 rc = mdt_version_get_check_save(info, mp, 0);
1142                 if (rc)
1143                         GOTO(put_parent, rc);
1144         }
1145
1146 relock:
1147         parent_lh = &info->mti_lh[MDT_LH_PARENT];
1148         mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
1149         rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
1150                                    cos_incompat);
1151         if (rc != 0)
1152                 GOTO(put_parent, rc);
1153
1154         /* lookup child object along with version checking */
1155         fid_zero(child_fid);
1156         rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
1157         if (rc != 0) {
1158                 /* Name might not be able to find during resend of
1159                  * remote unlink, considering following case.
1160                  * dir_A is a remote directory, the name entry of
1161                  * dir_A is on MDT0, the directory is on MDT1,
1162                  *
1163                  * 1. client sends unlink req to MDT1.
1164                  * 2. MDT1 sends name delete update to MDT0.
1165                  * 3. name entry is being deleted in MDT0 synchronously.
1166                  * 4. MDT1 is restarted.
1167                  * 5. client resends unlink req to MDT1. So it can not
1168                  *    find the name entry on MDT0 anymore.
1169                  * In this case, MDT1 only needs to destory the local
1170                  * directory.
1171                  * */
1172                 if (mdt_object_remote(mp) && rc == -ENOENT &&
1173                     !fid_is_zero(rr->rr_fid2) &&
1174                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1175                         no_name = 1;
1176                         *child_fid = *rr->rr_fid2;
1177                  } else {
1178                         GOTO(unlock_parent, rc);
1179                  }
1180         }
1181
1182         if (!fid_is_md_operative(child_fid))
1183                 GOTO(unlock_parent, rc = -EPERM);
1184
1185         /* We will lock the child regardless it is local or remote. No harm. */
1186         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1187         if (IS_ERR(mc))
1188                 GOTO(unlock_parent, rc = PTR_ERR(mc));
1189
1190         if (!cos_incompat) {
1191                 rc = mdt_object_striped(info, mc);
1192                 if (rc < 0)
1193                         GOTO(put_child, rc);
1194
1195                 cos_incompat = rc;
1196                 if (cos_incompat) {
1197                         mdt_object_put(info->mti_env, mc);
1198                         mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
1199                         goto relock;
1200                 }
1201         }
1202
1203         child_lh = &info->mti_lh[MDT_LH_CHILD];
1204         mdt_lock_reg_init(child_lh, LCK_EX);
1205         if (info->mti_spec.sp_rm_entry) {
1206                 struct lu_ucred *uc  = mdt_ucred(info);
1207
1208                 if (!mdt_is_dne_client(req->rq_export))
1209                         /* Return -ENOTSUPP for old client */
1210                         GOTO(put_child, rc = -ENOTSUPP);
1211
1212                 if (!md_capable(uc, CFS_CAP_SYS_ADMIN))
1213                         GOTO(put_child, rc = -EPERM);
1214
1215                 ma->ma_need = MA_INODE;
1216                 ma->ma_valid = 0;
1217                 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1218                                 NULL, &rr->rr_name, ma, no_name);
1219                 GOTO(put_child, rc);
1220         }
1221
1222         if (mdt_object_remote(mc)) {
1223                 struct mdt_body  *repbody;
1224
1225                 if (!fid_is_zero(rr->rr_fid2)) {
1226                         CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
1227                                mdt_obd_name(info->mti_mdt),
1228                                PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1229                         GOTO(put_child, rc = -ENOENT);
1230                 }
1231                 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
1232                        mdt_obd_name(info->mti_mdt),
1233                        PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1234
1235                 if (!mdt_is_dne_client(req->rq_export))
1236                         /* Return -ENOTSUPP for old client */
1237                         GOTO(put_child, rc = -ENOTSUPP);
1238
1239                 /* Revoke the LOOKUP lock of the remote object granted by
1240                  * this MDT. Since the unlink will happen on another MDT,
1241                  * it will release the LOOKUP lock right away. Then What
1242                  * would happen if another client try to grab the LOOKUP
1243                  * lock at the same time with unlink XXX */
1244                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1245                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1246                 LASSERT(repbody != NULL);
1247                 repbody->mbo_fid1 = *mdt_object_fid(mc);
1248                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1249                 GOTO(unlock_child, rc = -EREMOTE);
1250         }
1251         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1252          * this now because a running HSM restore on the child (unlink
1253          * victim) will hold the layout lock. See LU-4002. */
1254         lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1255         if (mdt_object_remote(mp)) {
1256                 /* Enqueue lookup lock from parent MDT */
1257                 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1258                                             &child_lh->mlh_rreg_lh,
1259                                             child_lh->mlh_rreg_mode,
1260                                             MDS_INODELOCK_LOOKUP, false);
1261                 if (rc != ELDLM_OK)
1262                         GOTO(put_child, rc);
1263
1264                 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1265         }
1266
1267         rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
1268                                     cos_incompat);
1269         if (rc != 0)
1270                 GOTO(put_child, rc);
1271
1272         /*
1273          * Now we can only make sure we need MA_INODE, in mdd layer, will check
1274          * whether need MA_LOV and MA_COOKIE.
1275          */
1276         ma->ma_need = MA_INODE;
1277         ma->ma_valid = 0;
1278
1279         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1280                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1281         /* save version when object is locked */
1282         mdt_version_get_save(info, mc, 1);
1283
1284         mutex_lock(&mc->mot_lov_mutex);
1285
1286         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1287                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
1288
1289         mutex_unlock(&mc->mot_lov_mutex);
1290         if (rc != 0)
1291                 GOTO(unlock_child, rc);
1292
1293         if (!lu_object_is_dying(&mc->mot_header)) {
1294                 rc = mdt_attr_get_complex(info, mc, ma);
1295                 if (rc)
1296                         GOTO(out_stat, rc);
1297         } else if (mdt_dom_check_for_discard(info, mc)) {
1298                 mdt_dom_discard_data(info, mc);
1299         }
1300         mdt_handle_last_unlink(info, mc, ma);
1301
1302 out_stat:
1303         if (ma->ma_valid & MA_INODE) {
1304                 switch (ma->ma_attr.la_mode & S_IFMT) {
1305                 case S_IFDIR:
1306                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
1307                         break;
1308                 case S_IFREG:
1309                 case S_IFLNK:
1310                 case S_IFCHR:
1311                 case S_IFBLK:
1312                 case S_IFIFO:
1313                 case S_IFSOCK:
1314                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
1315                         break;
1316                 default:
1317                         LASSERTF(0, "bad file type %o unlinking\n",
1318                                 ma->ma_attr.la_mode);
1319                 }
1320         }
1321
1322         EXIT;
1323
1324 unlock_child:
1325         mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
1326 put_child:
1327         mdt_object_put(info->mti_env, mc);
1328 unlock_parent:
1329         mdt_object_unlock(info, mp, parent_lh, rc);
1330 put_parent:
1331         mdt_object_put(info->mti_env, mp);
1332         CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
1333         return rc;
1334 }
1335
1336 /*
1337  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1338  * name.
1339  */
1340 static int mdt_reint_link(struct mdt_thread_info *info,
1341                           struct mdt_lock_handle *lhc)
1342 {
1343         struct mdt_reint_record *rr = &info->mti_rr;
1344         struct ptlrpc_request   *req = mdt_info_req(info);
1345         struct md_attr          *ma = &info->mti_attr;
1346         struct mdt_object       *ms;
1347         struct mdt_object       *mp;
1348         struct mdt_lock_handle  *lhs;
1349         struct mdt_lock_handle  *lhp;
1350         bool cos_incompat;
1351         int rc;
1352         ENTRY;
1353
1354         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1355                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1356
1357         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1358                 RETURN(err_serious(-ENOENT));
1359
1360         if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE)) {
1361                 req->rq_no_reply = 1;
1362                 RETURN(err_serious(-ENOENT));
1363         }
1364
1365         if (info->mti_dlm_req)
1366                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1367
1368         /* Invalid case so return error immediately instead of
1369          * processing it */
1370         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1371                 RETURN(-EPERM);
1372
1373         if (!fid_is_md_operative(rr->rr_fid1) ||
1374             !fid_is_md_operative(rr->rr_fid2))
1375                 RETURN(-EPERM);
1376
1377         /* step 1: find target parent dir */
1378         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1379         if (IS_ERR(mp))
1380                 RETURN(PTR_ERR(mp));
1381
1382         rc = mdt_version_get_check_save(info, mp, 0);
1383         if (rc)
1384                 GOTO(put_parent, rc);
1385
1386         /* step 2: find source */
1387         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1388         if (IS_ERR(ms))
1389                 GOTO(put_parent, rc = PTR_ERR(ms));
1390
1391         if (!mdt_object_exists(ms)) {
1392                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1393                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1394                 GOTO(put_source, rc = -ENOENT);
1395         }
1396
1397         cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1398
1399         lhp = &info->mti_lh[MDT_LH_PARENT];
1400         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1401         rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1402                                    cos_incompat);
1403         if (rc != 0)
1404                 GOTO(put_source, rc);
1405
1406         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1407
1408         lhs = &info->mti_lh[MDT_LH_CHILD];
1409         mdt_lock_reg_init(lhs, LCK_EX);
1410         rc = mdt_reint_object_lock(info, ms, lhs,
1411                                    MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1412                                    cos_incompat);
1413         if (rc != 0)
1414                 GOTO(unlock_parent, rc);
1415
1416         /* step 3: link it */
1417         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1418                         OBD_FAIL_MDS_REINT_LINK_WRITE);
1419
1420         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1421         rc = mdt_version_get_check_save(info, ms, 1);
1422         if (rc)
1423                 GOTO(unlock_source, rc);
1424
1425         /** check target version by name during replay */
1426         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1427                                       &info->mti_tmp_fid1, 2);
1428         if (rc != 0 && rc != -ENOENT)
1429                 GOTO(unlock_source, rc);
1430         /* save version of file name for replay, it must be ENOENT here */
1431         if (!req_is_replay(mdt_info_req(info))) {
1432                 if (rc != -ENOENT) {
1433                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1434                                PNAME(&rr->rr_name));
1435                         GOTO(unlock_source, rc = -EEXIST);
1436                 }
1437                 info->mti_ver[2] = ENOENT_VERSION;
1438                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1439         }
1440
1441         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1442                       mdt_object_child(ms), &rr->rr_name, ma);
1443
1444         if (rc == 0)
1445                 mdt_counter_incr(req, LPROC_MDT_LINK);
1446
1447         EXIT;
1448 unlock_source:
1449         mdt_object_unlock(info, ms, lhs, rc);
1450 unlock_parent:
1451         mdt_object_unlock(info, mp, lhp, rc);
1452 put_source:
1453         mdt_object_put(info->mti_env, ms);
1454 put_parent:
1455         mdt_object_put(info->mti_env, mp);
1456         return rc;
1457 }
1458 /**
1459  * lock the part of the directory according to the hash of the name
1460  * (lh->mlh_pdo_hash) in parallel directory lock.
1461  */
1462 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1463                               struct mdt_lock_handle *lh,
1464                               struct mdt_object *obj, __u64 ibits,
1465                               bool cos_incompat)
1466 {
1467         struct ldlm_res_id *res = &info->mti_res_id;
1468         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1469         union ldlm_policy_data *policy = &info->mti_policy;
1470         __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1471         int rc;
1472
1473         /*
1474          * Finish res_id initializing by name hash marking part of
1475          * directory which is taking modification.
1476          */
1477         LASSERT(lh->mlh_pdo_hash != 0);
1478         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1479         memset(policy, 0, sizeof(*policy));
1480         policy->l_inodebits.bits = ibits;
1481         if (cos_incompat &&
1482             (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1483                 dlmflags |= LDLM_FL_COS_INCOMPAT;
1484         /*
1485          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1486          * going to be sent to client. If it is - mdt_intent_policy() path will
1487          * fix it up and turn FL_LOCAL flag off.
1488          */
1489         rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
1490                           policy, res, dlmflags,
1491                           &info->mti_exp->exp_handle.h_cookie);
1492         return rc;
1493 }
1494
1495 /**
1496  * Get BFL lock for rename or migrate process.
1497  **/
1498 static int mdt_rename_lock(struct mdt_thread_info *info,
1499                            struct lustre_handle *lh)
1500 {
1501         int     rc;
1502         ENTRY;
1503
1504         if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1505                 struct lu_fid *fid = &info->mti_tmp_fid1;
1506                 struct mdt_object *obj;
1507
1508                 /* XXX, right now, it has to use object API to
1509                  * enqueue lock cross MDT, so it will enqueue
1510                  * rename lock(with LUSTRE_BFL_FID) by root object */
1511                 lu_root_fid(fid);
1512                 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1513                 if (IS_ERR(obj))
1514                         RETURN(PTR_ERR(obj));
1515
1516                 rc = mdt_remote_object_lock(info, obj,
1517                                             &LUSTRE_BFL_FID, lh,
1518                                             LCK_EX,
1519                                             MDS_INODELOCK_UPDATE, false);
1520                 mdt_object_put(info->mti_env, obj);
1521         } else {
1522                 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1523                 union ldlm_policy_data *policy = &info->mti_policy;
1524                 struct ldlm_res_id *res_id = &info->mti_res_id;
1525                 __u64 flags = 0;
1526
1527                 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1528                 memset(policy, 0, sizeof *policy);
1529                 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1530                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1531                 rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
1532                                             LDLM_IBITS, policy, LCK_EX, &flags,
1533                                             ldlm_blocking_ast,
1534                                             ldlm_completion_ast, NULL, NULL, 0,
1535                                             LVB_T_NONE,
1536                                             &info->mti_exp->exp_handle.h_cookie,
1537                                             lh);
1538                 RETURN(rc);
1539         }
1540         RETURN(rc);
1541 }
1542
1543 static void mdt_rename_unlock(struct lustre_handle *lh)
1544 {
1545         ENTRY;
1546         LASSERT(lustre_handle_is_used(lh));
1547         /* Cancel the single rename lock right away */
1548         ldlm_lock_decref_and_cancel(lh, LCK_EX);
1549         EXIT;
1550 }
1551
1552 static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
1553                                                 const struct lu_fid *fid,
1554                                                 int idx)
1555 {
1556         struct mdt_object *dir;
1557         int rc;
1558
1559         ENTRY;
1560
1561         dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1562         if (IS_ERR(dir))
1563                 RETURN(dir);
1564
1565         /* check early, the real version will be saved after locking */
1566         rc = mdt_version_get_check(info, dir, idx);
1567         if (rc)
1568                 GOTO(out_put, rc);
1569
1570         if (!mdt_object_exists(dir))
1571                 GOTO(out_put, rc = -ENOENT);
1572
1573         if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
1574                 GOTO(out_put, rc = -ENOTDIR);
1575
1576         RETURN(dir);
1577 out_put:
1578         mdt_object_put(info->mti_env, dir);
1579         return ERR_PTR(rc);
1580 }
1581
1582 /*
1583  * in case obj is remote obj on its parent, revoke LOOKUP lock,
1584  * herein we don't really check it, just do revoke.
1585  */
1586 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
1587                                   struct mdt_object *pobj,
1588                                   struct mdt_object *obj)
1589 {
1590         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1591         int rc;
1592
1593         mdt_lock_handle_init(lh);
1594         mdt_lock_reg_init(lh, LCK_EX);
1595
1596         if (mdt_object_remote(pobj)) {
1597                 /* don't bother to check if pobj and obj are on the same MDT. */
1598                 rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
1599                                             &lh->mlh_rreg_lh, LCK_EX,
1600                                             MDS_INODELOCK_LOOKUP, false);
1601         } else if (mdt_object_remote(obj)) {
1602                 struct ldlm_res_id *res = &info->mti_res_id;
1603                 union ldlm_policy_data *policy = &info->mti_policy;
1604                 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
1605                                  LDLM_FL_COS_INCOMPAT;
1606
1607                 fid_build_reg_res_name(mdt_object_fid(obj), res);
1608                 memset(policy, 0, sizeof(*policy));
1609                 policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
1610                 rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
1611                                   &lh->mlh_reg_lh, LCK_EX, policy, res,
1612                                   dlmflags, NULL);
1613         } else {
1614                 /* do nothing if both are local */
1615                 return 0;
1616         }
1617
1618         if (rc != ELDLM_OK)
1619                 return rc;
1620
1621         /*
1622          * TODO, currently we don't save this lock because there is no place to
1623          * hold this lock handle, but to avoid race we need to save this lock.
1624          */
1625         mdt_object_unlock(info, NULL, lh, 1);
1626
1627         return 0;
1628 }
1629
1630 /*
1631  * operation may takes locks of linkea, or directory stripes, group them in
1632  * different list.
1633  */
1634 struct mdt_sub_lock {
1635         struct mdt_object      *msl_obj;
1636         struct mdt_lock_handle  msl_lh;
1637         struct list_head        msl_linkage;
1638 };
1639
1640 static void mdt_unlock_list(struct mdt_thread_info *info,
1641                             struct list_head *list, int decref)
1642 {
1643         struct mdt_sub_lock *msl;
1644         struct mdt_sub_lock *tmp;
1645
1646         list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
1647                 mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
1648                 list_del(&msl->msl_linkage);
1649                 OBD_FREE_PTR(msl);
1650         }
1651 }
1652
1653 static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
1654                                              struct mdt_object *obj,
1655                                              struct mdt_lock_handle *lh,
1656                                              struct ldlm_enqueue_info *einfo,
1657                                              struct list_head *slave_locks,
1658                                              int decref)
1659 {
1660         if (mdt_object_remote(obj)) {
1661                 mdt_unlock_list(info, slave_locks, decref);
1662                 mdt_object_unlock(info, obj, lh, decref);
1663         } else {
1664                 mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
1665         }
1666 }
1667
1668 /*
1669  * lock parents of links, and also check whether total locks don't exceed
1670  * RS_MAX_LOCKS.
1671  *
1672  * \retval      0 on success, and locks can be saved in ptlrpc_reply_stat
1673  * \retval      1 on success, but total lock count may exceed RS_MAX_LOCKS
1674  * \retval      -ev negative errno upon error
1675  */
1676 static int mdt_link_parents_lock(struct mdt_thread_info *info,
1677                                  struct mdt_object *pobj,
1678                                  const struct md_attr *ma,
1679                                  struct mdt_object *obj,
1680                                  struct mdt_lock_handle *lhp,
1681                                  struct ldlm_enqueue_info *peinfo,
1682                                  struct list_head *parent_slave_locks,
1683                                  struct list_head *link_locks)
1684 {
1685         struct mdt_device *mdt = info->mti_mdt;
1686         struct lu_buf *buf = &info->mti_big_buf;
1687         struct lu_name *lname = &info->mti_name;
1688         struct linkea_data ldata = { NULL };
1689         bool blocked = false;
1690         int local_lnkp_cnt = 0;
1691         int rc;
1692
1693         ENTRY;
1694
1695         if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1696                 RETURN(0);
1697
1698         buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
1699         if (buf->lb_buf == NULL)
1700                 RETURN(-ENOMEM);
1701
1702         ldata.ld_buf = buf;
1703         rc = mdt_links_read(info, obj, &ldata);
1704         if (rc) {
1705                 if (rc == -ENOENT || rc == -ENODATA)
1706                         rc = 0;
1707                 RETURN(rc);
1708         }
1709
1710         for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
1711              linkea_next_entry(&ldata)) {
1712                 struct mdt_object *lnkp;
1713                 struct mdt_sub_lock *msl;
1714                 struct lu_fid fid;
1715                 __u64 ibits;
1716
1717                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
1718                                     &fid);
1719
1720                 /* check if it's also linked to parent */
1721                 if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
1722                         CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
1723                                PFID(&fid), PNAME(lname));
1724                         /* in case link is remote object, revoke LOOKUP lock */
1725                         rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1726                         continue;
1727                 }
1728
1729                 lnkp = NULL;
1730
1731                 /* check if it's linked to a stripe of parent */
1732                 if (ma->ma_valid & MA_LMV) {
1733                         struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1734                         struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
1735                         int j = 0;
1736
1737                         for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
1738                                 fid_le_to_cpu(stripe_fid,
1739                                               &lmv->lmv_stripe_fids[j]);
1740                                 if (lu_fid_eq(stripe_fid, &fid)) {
1741                                         CDEBUG(D_INFO, "skip stripe "DFID
1742                                                ", reovke "DNAME"\n",
1743                                                PFID(&fid), PNAME(lname));
1744                                         lnkp = mdt_object_find(info->mti_env,
1745                                                                mdt, &fid);
1746                                         if (IS_ERR(lnkp))
1747                                                 GOTO(out, rc = PTR_ERR(lnkp));
1748                                         break;
1749                                 }
1750                         }
1751
1752                         if (lnkp) {
1753                                 rc = mdt_revoke_remote_lookup_lock(info, lnkp,
1754                                                                    obj);
1755                                 mdt_object_put(info->mti_env, lnkp);
1756                                 continue;
1757                         }
1758                 }
1759
1760                 /* Check if it's already locked */
1761                 list_for_each_entry(msl, link_locks, msl_linkage) {
1762                         if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
1763                                 CDEBUG(D_INFO,
1764                                        DFID" was locked, revoke "DNAME"\n",
1765                                        PFID(&fid), PNAME(lname));
1766                                 lnkp = msl->msl_obj;
1767                                 break;
1768                         }
1769                 }
1770
1771                 if (lnkp) {
1772                         rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1773                         continue;
1774                 }
1775
1776                 CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
1777                        PFID(&fid), PNAME(lname));
1778
1779                 lnkp = mdt_object_find(info->mti_env, mdt, &fid);
1780                 if (IS_ERR(lnkp)) {
1781                         CWARN("%s: cannot find obj "DFID": %ld\n",
1782                               mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
1783                         continue;
1784                 }
1785
1786                 if (!mdt_object_exists(lnkp)) {
1787                         CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
1788                               PFID(&fid), PNAME(lname));
1789                         mdt_object_put(info->mti_env, lnkp);
1790                         continue;
1791                 }
1792
1793                 if (!mdt_object_remote(lnkp))
1794                         local_lnkp_cnt++;
1795
1796                 OBD_ALLOC_PTR(msl);
1797                 if (msl == NULL)
1798                         GOTO(out, rc = -ENOMEM);
1799
1800                 /*
1801                  * we can't follow parent-child lock order like other MD
1802                  * operations, use lock_try here to avoid deadlock, if the lock
1803                  * cannot be taken, drop all locks taken, revoke the blocked
1804                  * one, and continue processing the remaining entries, and in
1805                  * the end of the loop restart from beginning.
1806                  */
1807                 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1808                 ibits = 0;
1809                 rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
1810                                          MDS_INODELOCK_UPDATE, true);
1811                 if (!(ibits & MDS_INODELOCK_UPDATE)) {
1812
1813                         CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
1814                                PFID(&fid), PNAME(lname));
1815
1816                         mdt_unlock_list(info, link_locks, 1);
1817                         /* also unlock parent locks to avoid deadlock */
1818                         if (!blocked)
1819                                 mdt_migrate_object_unlock(info, pobj, lhp,
1820                                                           peinfo,
1821                                                           parent_slave_locks,
1822                                                           1);
1823
1824                         blocked = true;
1825
1826                         mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1827                         rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
1828                                              MDS_INODELOCK_UPDATE);
1829                         if (rc) {
1830                                 mdt_object_put(info->mti_env, lnkp);
1831                                 OBD_FREE_PTR(msl);
1832                                 GOTO(out, rc);
1833                         }
1834
1835                         if (mdt_object_remote(lnkp)) {
1836                                 struct ldlm_lock *lock;
1837
1838                                 /*
1839                                  * for remote object, set lock cb_atomic,
1840                                  * so lock can be released in blocking_ast()
1841                                  * immediately, then the next lock_try will
1842                                  * have better chance of success.
1843                                  */
1844                                 lock = ldlm_handle2lock(
1845                                                 &msl->msl_lh.mlh_rreg_lh);
1846                                 LASSERT(lock != NULL);
1847                                 lock_res_and_lock(lock);
1848                                 ldlm_set_atomic_cb(lock);
1849                                 unlock_res_and_lock(lock);
1850                                 LDLM_LOCK_PUT(lock);
1851                         }
1852
1853                         mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
1854                         OBD_FREE_PTR(msl);
1855                         continue;
1856                 }
1857
1858                 INIT_LIST_HEAD(&msl->msl_linkage);
1859                 msl->msl_obj = lnkp;
1860                 list_add_tail(&msl->msl_linkage, link_locks);
1861
1862                 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1863         }
1864
1865         if (blocked)
1866                 GOTO(out, rc = -EBUSY);
1867
1868         EXIT;
1869 out:
1870         if (rc)
1871                 mdt_unlock_list(info, link_locks, rc);
1872         else if (local_lnkp_cnt > RS_MAX_LOCKS - 6)
1873                 /*
1874                  * parent may have 3 local objects: master object and 2 stripes
1875                  * (if it's being migrated too); source may have 2 local
1876                  * objects: master and 1 stripe; target has 1 local object.
1877                  */
1878                 rc = 1;
1879         return rc;
1880 }
1881
1882 static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
1883                                   struct mdt_object *obj,
1884                                   const struct md_attr *ma,
1885                                   struct list_head *slave_locks)
1886 {
1887         struct mdt_device *mdt = info->mti_mdt;
1888         const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1889         struct lu_fid *fid = &info->mti_tmp_fid1;
1890         struct mdt_object *slave;
1891         struct mdt_sub_lock *msl;
1892         int i;
1893         int rc;
1894
1895         ENTRY;
1896
1897         LASSERT(mdt_object_remote(obj));
1898         LASSERT(ma->ma_valid & MA_LMV);
1899         LASSERT(lmv);
1900
1901         if (!lmv_is_sane(lmv))
1902                 RETURN(-EINVAL);
1903
1904         for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
1905                 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
1906
1907                 if (!fid_is_sane(fid))
1908                         continue;
1909
1910                 slave = mdt_object_find(info->mti_env, mdt, fid);
1911                 if (IS_ERR(slave))
1912                         GOTO(out, rc = PTR_ERR(slave));
1913
1914                 OBD_ALLOC_PTR(msl);
1915                 if (!msl) {
1916                         mdt_object_put(info->mti_env, slave);
1917                         GOTO(out, rc = -ENOMEM);
1918                 }
1919
1920                 mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
1921                 rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
1922                                            MDS_INODELOCK_UPDATE, true);
1923                 if (rc) {
1924                         OBD_FREE_PTR(msl);
1925                         mdt_object_put(info->mti_env, slave);
1926                         GOTO(out, rc);
1927                 }
1928
1929                 INIT_LIST_HEAD(&msl->msl_linkage);
1930                 msl->msl_obj = slave;
1931                 list_add_tail(&msl->msl_linkage, slave_locks);
1932         }
1933         EXIT;
1934
1935 out:
1936         if (rc)
1937                 mdt_unlock_list(info, slave_locks, rc);
1938         return rc;
1939 }
1940
1941 /* lock parent and its stripes */
1942 static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
1943                                    struct mdt_object *obj,
1944                                    const struct md_attr *ma,
1945                                    struct mdt_lock_handle *lh,
1946                                    struct ldlm_enqueue_info *einfo,
1947                                    struct list_head *slave_locks)
1948 {
1949         int rc;
1950
1951         if (mdt_object_remote(obj)) {
1952                 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1953                                             &lh->mlh_rreg_lh, LCK_PW,
1954                                             MDS_INODELOCK_UPDATE, false);
1955                 if (rc != ELDLM_OK)
1956                         return rc;
1957
1958                 /*
1959                  * if obj is remote and striped, lock its stripes explicitly
1960                  * because it's not striped in LOD layer on this MDT.
1961                  */
1962                 if (ma->ma_valid & MA_LMV) {
1963                         rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
1964                         if (rc)
1965                                 mdt_object_unlock(info, obj, lh, rc);
1966                 }
1967         } else {
1968                 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
1969                                             einfo, true);
1970         }
1971
1972         return rc;
1973 }
1974
1975 /*
1976  * in migration, object may be remote, and we need take full lock of it and its
1977  * stripes if it's directory, besides, object may be a remote object on its
1978  * parent, revoke its LOOKUP lock on where its parent is located.
1979  */
1980 static int mdt_migrate_object_lock(struct mdt_thread_info *info,
1981                                    struct mdt_object *pobj,
1982                                    struct mdt_object *obj,
1983                                    struct mdt_lock_handle *lh,
1984                                    struct ldlm_enqueue_info *einfo,
1985                                    struct list_head *slave_locks)
1986 {
1987         int rc;
1988
1989         if (mdt_object_remote(obj)) {
1990                 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1991                 if (rc)
1992                         return rc;
1993
1994                 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1995                                             &lh->mlh_rreg_lh, LCK_EX,
1996                                             MDS_INODELOCK_FULL, false);
1997                 if (rc != ELDLM_OK)
1998                         return rc;
1999
2000                 /*
2001                  * if obj is remote and striped, lock its stripes explicitly
2002                  * because it's not striped in LOD layer on this MDT.
2003                  */
2004                 if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
2005                         struct md_attr *ma = &info->mti_attr;
2006
2007                         ma->ma_lmv = info->mti_big_lmm;
2008                         ma->ma_lmv_size = info->mti_big_lmmsize;
2009                         ma->ma_valid = 0;
2010                         rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
2011                         if (rc) {
2012                                 mdt_object_unlock(info, obj, lh, rc);
2013                                 return rc;
2014                         }
2015
2016                         if (ma->ma_valid & MA_LMV) {
2017                                 rc = mdt_lock_remote_slaves(info, obj, ma,
2018                                                             slave_locks);
2019                                 if (rc)
2020                                         mdt_object_unlock(info, obj, lh, rc);
2021                         }
2022                 }
2023         } else {
2024                 if (mdt_object_remote(pobj)) {
2025                         rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
2026                         if (rc)
2027                                 return rc;
2028                 }
2029
2030                 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
2031                                             einfo, true);
2032         }
2033
2034         return rc;
2035 }
2036
2037 /*
2038  * lookup source by name, if parent is striped directory, we need to find the
2039  * corresponding stripe where source is located, and then lookup there.
2040  *
2041  * besides, if parent is migrating too, and file is already in target stripe,
2042  * this should be a redo of 'lfs migrate' on client side.
2043  */
2044 static int mdt_migrate_lookup(struct mdt_thread_info *info,
2045                               struct mdt_object *pobj,
2046                               const struct md_attr *ma,
2047                               const struct lu_name *lname,
2048                               struct mdt_object **spobj,
2049                               struct mdt_object **sobj)
2050 {
2051         const struct lu_env *env = info->mti_env;
2052         struct lu_fid *fid = &info->mti_tmp_fid1;
2053         struct mdt_object *stripe;
2054         int rc;
2055
2056         if (ma->ma_valid & MA_LMV) {
2057                 /* if parent is striped, lookup on corresponding stripe */
2058                 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
2059
2060                 if (!lmv_is_sane(lmv))
2061                         return -EBADF;
2062
2063                 rc = lmv_name_to_stripe_index_old(lmv, lname->ln_name,
2064                                                   lname->ln_namelen);
2065                 if (rc < 0)
2066                         return rc;
2067
2068                 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2069
2070                 stripe = mdt_object_find(env, info->mti_mdt, fid);
2071                 if (IS_ERR(stripe))
2072                         return PTR_ERR(stripe);
2073
2074                 fid_zero(fid);
2075                 rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
2076                                 &info->mti_spec);
2077                 if (rc == -ENOENT && lmv_is_layout_changing(lmv)) {
2078                         /*
2079                          * if parent layout is changeing, and lookup child
2080                          * failed on source stripe, lookup again on target
2081                          *  stripe, if it exists, it means previous migration
2082                          *  was interrupted, and current file was migrated
2083                          *  already.
2084                          */
2085                         mdt_object_put(env, stripe);
2086
2087                         rc = lmv_name_to_stripe_index(lmv, lname->ln_name,
2088                                                       lname->ln_namelen);
2089                         if (rc < 0)
2090                                 return rc;
2091
2092                         fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2093
2094                         stripe = mdt_object_find(env, info->mti_mdt, fid);
2095                         if (IS_ERR(stripe))
2096                                 return PTR_ERR(stripe);
2097
2098                         fid_zero(fid);
2099                         rc = mdo_lookup(env, mdt_object_child(stripe), lname,
2100                                         fid, &info->mti_spec);
2101                         mdt_object_put(env, stripe);
2102                         return rc ?: -EALREADY;
2103                 } else if (rc) {
2104                         mdt_object_put(env, stripe);
2105                         return rc;
2106                 }
2107         } else {
2108                 fid_zero(fid);
2109                 rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
2110                                 &info->mti_spec);
2111                 if (rc)
2112                         return rc;
2113
2114                 stripe = pobj;
2115                 mdt_object_get(env, stripe);
2116         }
2117
2118         *spobj = stripe;
2119
2120         *sobj = mdt_object_find(env, info->mti_mdt, fid);
2121         if (IS_ERR(*sobj)) {
2122                 mdt_object_put(env, stripe);
2123                 rc = PTR_ERR(*sobj);
2124                 *spobj = NULL;
2125                 *sobj = NULL;
2126         }
2127
2128         return rc;
2129 }
2130
2131 /* end lease and close file for regular file */
2132 static int mdd_migrate_close(struct mdt_thread_info *info,
2133                              struct mdt_object *obj)
2134 {
2135         struct close_data *data;
2136         struct mdt_body *repbody;
2137         struct ldlm_lock *lease;
2138         int rc;
2139         int rc2;
2140
2141         rc = -EPROTO;
2142         if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
2143                                       RCL_CLIENT) ||
2144             !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
2145                                       RCL_CLIENT))
2146                 goto close;
2147
2148         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2149         if (!data)
2150                 goto close;
2151
2152         rc = -ESTALE;
2153         lease = ldlm_handle2lock(&data->cd_handle);
2154         if (!lease)
2155                 goto close;
2156
2157         /* check if the lease was already canceled */
2158         lock_res_and_lock(lease);
2159         rc = ldlm_is_cancel(lease);
2160         unlock_res_and_lock(lease);
2161
2162         if (rc) {
2163                 rc = -EAGAIN;
2164                 LDLM_DEBUG(lease, DFID" lease broken",
2165                            PFID(mdt_object_fid(obj)));
2166         }
2167
2168         /*
2169          * cancel server side lease, client side counterpart should have been
2170          * cancelled, it's okay to cancel it now as we've held mot_open_sem.
2171          */
2172         ldlm_lock_cancel(lease);
2173         ldlm_reprocess_all(lease->l_resource, lease);
2174         LDLM_LOCK_PUT(lease);
2175
2176 close:
2177         rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
2178         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2179         repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2180
2181         return rc ?: rc2;
2182 }
2183
2184 /*
2185  * migrate file in below steps:
2186  *  1. lock parent and its stripes
2187  *  2. lookup source by name
2188  *  3. lock parents of source links if source is not directory
2189  *  4. reject if source is in HSM
2190  *  5. take source open_sem and close file if source is regular file
2191  *  6. lock source and its stripes if it's directory
2192  *  7. lock target so subsequent change to it can trigger COS
2193  *  8. migrate file
2194  *  9. unlock above locks
2195  * 10. sync device if source has links
2196  */
2197 static int mdt_reint_migrate(struct mdt_thread_info *info,
2198                              struct mdt_lock_handle *unused)
2199 {
2200         const struct lu_env *env = info->mti_env;
2201         struct mdt_device *mdt = info->mti_mdt;
2202         struct ptlrpc_request *req = mdt_info_req(info);
2203         struct mdt_reint_record *rr = &info->mti_rr;
2204         struct lu_ucred *uc = mdt_ucred(info);
2205         struct md_attr *ma = &info->mti_attr;
2206         struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
2207         struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
2208         struct mdt_object *pobj;
2209         struct mdt_object *spobj = NULL;
2210         struct mdt_object *sobj = NULL;
2211         struct mdt_object *tobj;
2212         struct lustre_handle rename_lh = { 0 };
2213         struct mdt_lock_handle *lhp;
2214         struct mdt_lock_handle *lhs;
2215         struct mdt_lock_handle *lht;
2216         LIST_HEAD(parent_slave_locks);
2217         LIST_HEAD(child_slave_locks);
2218         LIST_HEAD(link_locks);
2219         int lock_retries = 5;
2220         bool open_sem_locked = false;
2221         bool do_sync = false;
2222         int rc;
2223         ENTRY;
2224
2225         CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
2226                PNAME(&rr->rr_name), PFID(rr->rr_fid2));
2227
2228         if (info->mti_dlm_req)
2229                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2230
2231         if (!fid_is_md_operative(rr->rr_fid1) ||
2232             !fid_is_md_operative(rr->rr_fid2))
2233                 RETURN(-EPERM);
2234
2235         /* don't allow migrate . or .. */
2236         if (lu_name_is_dot_or_dotdot(&rr->rr_name))
2237                 RETURN(-EBUSY);
2238
2239         if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
2240                 RETURN(-EPERM);
2241
2242         if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
2243             uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
2244             mdt->mdt_enable_remote_dir_gid != -1)
2245                 RETURN(-EPERM);
2246
2247         /*
2248          * Note: do not enqueue rename lock for replay request, because
2249          * if other MDT holds rename lock, but being blocked to wait for
2250          * this MDT to finish its recovery, and the failover MDT can not
2251          * get rename lock, which will cause deadlock.
2252          */
2253         if (!req_is_replay(req)) {
2254                 rc = mdt_rename_lock(info, &rename_lh);
2255                 if (rc != 0) {
2256                         CERROR("%s: can't lock FS for rename: rc = %d\n",
2257                                mdt_obd_name(info->mti_mdt), rc);
2258                         RETURN(rc);
2259                 }
2260         }
2261
2262         /* pobj is master object of parent */
2263         pobj = mdt_parent_find_check(info, rr->rr_fid1, 0);
2264         if (IS_ERR(pobj))
2265                 GOTO(unlock_rename, rc = PTR_ERR(pobj));
2266
2267         if (unlikely(!info->mti_big_lmm)) {
2268                 info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
2269                 OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
2270                 if (!info->mti_big_lmm)
2271                         GOTO(put_parent, rc = -ENOMEM);
2272         }
2273
2274         ma->ma_lmv = info->mti_big_lmm;
2275         ma->ma_lmv_size = info->mti_big_lmmsize;
2276         ma->ma_valid = 0;
2277         rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
2278         if (rc)
2279                 GOTO(put_parent, rc);
2280
2281 lock_parent:
2282         /* lock parent object */
2283         lhp = &info->mti_lh[MDT_LH_PARENT];
2284         mdt_lock_reg_init(lhp, LCK_PW);
2285         rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
2286                                      &parent_slave_locks);
2287         if (rc)
2288                 GOTO(put_parent, rc);
2289
2290         /*
2291          * spobj is the corresponding stripe against name if pobj is striped
2292          * directory, which is the real parent, and no need to lock, because
2293          * we've taken full lock of pobj.
2294          */
2295         rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
2296         if (rc)
2297                 GOTO(unlock_parent, rc);
2298
2299         /* lock parents of source links, and revoke LOOKUP lock of links */
2300         rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
2301                                    &parent_slave_locks, &link_locks);
2302         if (rc == -EBUSY && lock_retries-- > 0) {
2303                 mdt_object_put(env, sobj);
2304                 mdt_object_put(env, spobj);
2305                 goto lock_parent;
2306         }
2307
2308         if (rc < 0)
2309                 GOTO(put_source, rc);
2310
2311         /*
2312          * RS_MAX_LOCKS is the limit of number of locks that can be saved along
2313          * with one request, if total lock count exceeds this limit, we will
2314          * drop all locks after migration, and synchronous device in the end.
2315          */
2316         do_sync = rc;
2317
2318         /* TODO: DoM migration is not supported yet */
2319         if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
2320                 ma->ma_lmm = info->mti_big_lmm;
2321                 ma->ma_lmm_size = info->mti_big_lmmsize;
2322                 ma->ma_valid = 0;
2323                 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
2324                 if (rc)
2325                         GOTO(put_source, rc);
2326
2327                 if (ma->ma_valid & MA_LOV && mdt_lmm_dom_stripesize(ma->ma_lmm))
2328                         GOTO(put_source, rc = -EOPNOTSUPP);
2329         }
2330
2331         /* if migration HSM is allowed */
2332         if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
2333                 ma->ma_need = MA_HSM;
2334                 ma->ma_valid = 0;
2335                 rc = mdt_attr_get_complex(info, sobj, ma);
2336                 if (rc)
2337                         GOTO(unlock_links, rc);
2338
2339                 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
2340                         GOTO(unlock_links, rc = -EOPNOTSUPP);
2341         }
2342
2343         /* end lease and close file for regular file */
2344         if (info->mti_spec.sp_migrate_close) {
2345                 /* try to hold open_sem so that nobody else can open the file */
2346                 if (!down_write_trylock(&sobj->mot_open_sem)) {
2347                         /* close anyway */
2348                         mdd_migrate_close(info, sobj);
2349                         GOTO(unlock_links, rc = -EBUSY);
2350                 } else {
2351                         open_sem_locked = true;
2352                         rc = mdd_migrate_close(info, sobj);
2353                         if (rc)
2354                                 GOTO(unlock_open_sem, rc);
2355                 }
2356         }
2357
2358         /* lock source */
2359         lhs = &info->mti_lh[MDT_LH_OLD];
2360         mdt_lock_reg_init(lhs, LCK_EX);
2361         rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
2362                                      &child_slave_locks);
2363         if (rc)
2364                 GOTO(unlock_open_sem, rc);
2365
2366         /* lock target */
2367         tobj = mdt_object_find(env, mdt, rr->rr_fid2);
2368         if (IS_ERR(tobj))
2369                 GOTO(unlock_source, rc = PTR_ERR(tobj));
2370
2371         lht = &info->mti_lh[MDT_LH_NEW];
2372         mdt_lock_reg_init(lht, LCK_EX);
2373         rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
2374         if (rc)
2375                 GOTO(put_target, rc);
2376
2377         /* Don't do lookup sanity check. We know name doesn't exist. */
2378         info->mti_spec.sp_cr_lookup = 0;
2379         info->mti_spec.sp_feat = &dt_directory_features;
2380
2381         rc = mdo_migrate(env, mdt_object_child(pobj),
2382                          mdt_object_child(sobj), &rr->rr_name,
2383                          mdt_object_child(tobj),
2384                          &info->mti_spec, ma);
2385         if (!rc)
2386                 mdt_counter_incr(req, LPROC_MDT_MIGRATE);
2387         EXIT;
2388
2389         mdt_object_unlock(info, tobj, lht, rc);
2390 put_target:
2391         mdt_object_put(env, tobj);
2392 unlock_source:
2393         mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
2394                                   &child_slave_locks, rc);
2395 unlock_open_sem:
2396         if (open_sem_locked)
2397                 up_write(&sobj->mot_open_sem);
2398 unlock_links:
2399         /* if we've got too many locks to save into RPC,
2400          * then just commit before the locks are released */
2401         if (!rc && do_sync)
2402                 mdt_device_sync(env, mdt);
2403         mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
2404 put_source:
2405         mdt_object_put(env, sobj);
2406         mdt_object_put(env, spobj);
2407 unlock_parent:
2408         mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
2409                                   &parent_slave_locks, rc);
2410 put_parent:
2411         mdt_object_put(env, pobj);
2412 unlock_rename:
2413         if (lustre_handle_is_used(&rename_lh))
2414                 mdt_rename_unlock(&rename_lh);
2415
2416         return rc;
2417 }
2418
2419 static int mdt_object_lock_save(struct mdt_thread_info *info,
2420                                 struct mdt_object *dir,
2421                                 struct mdt_lock_handle *lh,
2422                                 int idx, bool cos_incompat)
2423 {
2424         int rc;
2425
2426         /* we lock the target dir if it is local */
2427         rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
2428                                    cos_incompat);
2429         if (rc != 0)
2430                 return rc;
2431
2432         /* get and save correct version after locking */
2433         mdt_version_get_save(info, dir, idx);
2434         return 0;
2435 }
2436
2437 /*
2438  * determine lock order of sobj and tobj
2439  *
2440  * there are two situations we need to lock tobj before sobj:
2441  * 1. sobj is child of tobj
2442  * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
2443  *    larger than that of tobj
2444  *
2445  * \retval      1 lock tobj before sobj
2446  * \retval      0 lock sobj before tobj
2447  * \retval      -ev negative errno upon error
2448  */
2449 static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
2450                                            struct mdt_object *sobj,
2451                                            struct mdt_object *tobj)
2452 {
2453         struct md_attr *ma = &info->mti_attr;
2454         struct lu_fid *spfid = &info->mti_tmp_fid1;
2455         struct lu_fid *tpfid = &info->mti_tmp_fid2;
2456         struct lmv_mds_md_v1 *lmv;
2457         __u32 sindex;
2458         __u32 tindex;
2459         int rc;
2460
2461         /* sobj and tobj are the same */
2462         if (sobj == tobj)
2463                 return 0;
2464
2465         if (fid_is_root(mdt_object_fid(sobj)))
2466                 return 0;
2467
2468         if (fid_is_root(mdt_object_fid(tobj)))
2469                 return 1;
2470
2471         /* check whether sobj is child of tobj */
2472         rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
2473                            mdt_object_fid(tobj));
2474         if (rc < 0)
2475                 return rc;
2476
2477         if (rc == 1)
2478                 return 1;
2479
2480         /* check whether sobj and tobj are children of the same parent */
2481         rc = mdt_attr_get_pfid(info, sobj, spfid);
2482         if (rc)
2483                 return rc;
2484
2485         rc = mdt_attr_get_pfid(info, tobj, tpfid);
2486         if (rc)
2487                 return rc;
2488
2489         if (!lu_fid_eq(spfid, tpfid))
2490                 return 0;
2491
2492         /* check whether sobj and tobj are sibling stripes */
2493         ma->ma_need = MA_LMV;
2494         ma->ma_valid = 0;
2495         ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
2496         ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
2497         rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2498         if (rc)
2499                 return rc;
2500
2501         if (!(ma->ma_valid & MA_LMV))
2502                 return 0;
2503
2504         lmv = &ma->ma_lmv->lmv_md_v1;
2505         if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2506                 return 0;
2507         sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2508
2509         ma->ma_valid = 0;
2510         rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
2511         if (rc)
2512                 return rc;
2513
2514         if (!(ma->ma_valid & MA_LMV))
2515                 return -ENODATA;
2516
2517         lmv = &ma->ma_lmv->lmv_md_v1;
2518         if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2519                 return -EINVAL;
2520         tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2521
2522         /* check stripe index of sobj and tobj */
2523         if (sindex == tindex)
2524                 return -EINVAL;
2525
2526         return sindex < tindex ? 0 : 1;
2527 }
2528
2529 /*
2530  * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
2531  * 2 - srcdir child; 3 - tgtdir child.
2532  * Update on disk version of srcdir child.
2533  */
2534 static int mdt_reint_rename(struct mdt_thread_info *info,
2535                             struct mdt_lock_handle *unused)
2536 {
2537         struct mdt_device *mdt = info->mti_mdt;
2538         struct mdt_reint_record *rr = &info->mti_rr;
2539         struct md_attr *ma = &info->mti_attr;
2540         struct ptlrpc_request *req = mdt_info_req(info);
2541         struct mdt_object *msrcdir = NULL;
2542         struct mdt_object *mtgtdir = NULL;
2543         struct mdt_object *mold;
2544         struct mdt_object *mnew = NULL;
2545         struct lustre_handle rename_lh = { 0 };
2546         struct mdt_lock_handle *lh_srcdirp;
2547         struct mdt_lock_handle *lh_tgtdirp;
2548         struct mdt_lock_handle *lh_oldp = NULL;
2549         struct mdt_lock_handle *lh_newp = NULL;
2550         struct lu_fid *old_fid = &info->mti_tmp_fid1;
2551         struct lu_fid *new_fid = &info->mti_tmp_fid2;
2552         __u64 lock_ibits;
2553         bool reverse = false, discard = false;
2554         bool cos_incompat;
2555         int rc;
2556         ENTRY;
2557
2558         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
2559                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
2560                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
2561
2562         if (info->mti_dlm_req)
2563                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2564
2565         if (!fid_is_md_operative(rr->rr_fid1) ||
2566             !fid_is_md_operative(rr->rr_fid2))
2567                 RETURN(-EPERM);
2568
2569         /* find both parents. */
2570         msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
2571         if (IS_ERR(msrcdir))
2572                 RETURN(PTR_ERR(msrcdir));
2573
2574         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
2575
2576         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
2577                 mtgtdir = msrcdir;
2578                 mdt_object_get(info->mti_env, mtgtdir);
2579         } else {
2580                 mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
2581                 if (IS_ERR(mtgtdir))
2582                         GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
2583         }
2584
2585         /*
2586          * Note: do not enqueue rename lock for replay request, because
2587          * if other MDT holds rename lock, but being blocked to wait for
2588          * this MDT to finish its recovery, and the failover MDT can not
2589          * get rename lock, which will cause deadlock.
2590          */
2591         if (!req_is_replay(req)) {
2592                 /*
2593                  * Normally rename RPC is handled on the MDT with the target
2594                  * directory (if target exists, it's on the MDT with the
2595                  * target), if the source directory is remote, it's a hint that
2596                  * source is remote too (this may not be true, but it won't
2597                  * cause any issue), return -EXDEV early to avoid taking
2598                  * rename_lock.
2599                  */
2600                 if (!mdt->mdt_enable_remote_rename &&
2601                     mdt_object_remote(msrcdir))
2602                         GOTO(out_put_tgtdir, rc = -EXDEV);
2603
2604                 rc = mdt_rename_lock(info, &rename_lh);
2605                 if (rc != 0) {
2606                         CERROR("%s: can't lock FS for rename: rc = %d\n",
2607                                mdt_obd_name(mdt), rc);
2608                         GOTO(out_put_tgtdir, rc);
2609                 }
2610         }
2611
2612         rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
2613         if (rc < 0)
2614                 GOTO(out_unlock_rename, rc);
2615
2616         reverse = rc;
2617
2618         /* source needs to be looked up after locking source parent, otherwise
2619          * this rename may race with unlink source, and cause rename hang, see
2620          * sanityn.sh 55b, so check parents first, if later we found source is
2621          * remote, relock parents. */
2622         cos_incompat = (mdt_object_remote(msrcdir) ||
2623                         mdt_object_remote(mtgtdir));
2624
2625         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2626
2627         /* lock parents in the proper order. */
2628         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
2629         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
2630
2631 relock:
2632         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
2633         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
2634
2635         if (reverse) {
2636                 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2637                                           cos_incompat);
2638                 if (rc)
2639                         GOTO(out_unlock_rename, rc);
2640
2641                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2642
2643                 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2644                                           cos_incompat);
2645                 if (rc != 0) {
2646                         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2647                         GOTO(out_unlock_rename, rc);
2648                 }
2649         } else {
2650                 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2651                                           cos_incompat);
2652                 if (rc)
2653                         GOTO(out_unlock_rename, rc);
2654
2655                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2656
2657                 if (mtgtdir != msrcdir) {
2658                         rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2659                                                   cos_incompat);
2660                 } else if (!mdt_object_remote(mtgtdir) &&
2661                            lh_srcdirp->mlh_pdo_hash !=
2662                            lh_tgtdirp->mlh_pdo_hash) {
2663                         rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
2664                                                 MDS_INODELOCK_UPDATE,
2665                                                 cos_incompat);
2666                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
2667                 }
2668                 if (rc != 0) {
2669                         mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2670                         GOTO(out_unlock_rename, rc);
2671                 }
2672         }
2673
2674         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2675         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
2676
2677         /* find mold object. */
2678         fid_zero(old_fid);
2679         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
2680         if (rc != 0)
2681                 GOTO(out_unlock_parents, rc);
2682
2683         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
2684                 GOTO(out_unlock_parents, rc = -EINVAL);
2685
2686         if (!fid_is_md_operative(old_fid))
2687                 GOTO(out_unlock_parents, rc = -EPERM);
2688
2689         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
2690         if (IS_ERR(mold))
2691                 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
2692
2693         if (!mdt_object_exists(mold)) {
2694                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2695                                 &mold->mot_obj,
2696                                 "object does not exist");
2697                 GOTO(out_put_old, rc = -ENOENT);
2698         }
2699
2700         if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
2701                 GOTO(out_put_old, rc = -EXDEV);
2702
2703         /* Check if @mtgtdir is subdir of @mold, before locking child
2704          * to avoid reverse locking. */
2705         if (mtgtdir != msrcdir) {
2706                 rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
2707                                    old_fid);
2708                 if (rc) {
2709                         if (rc == 1)
2710                                 rc = -EINVAL;
2711                         GOTO(out_put_old, rc);
2712                 }
2713         }
2714
2715         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
2716         /* save version after locking */
2717         mdt_version_get_save(info, mold, 2);
2718
2719         if (!cos_incompat && mdt_object_remote(mold)) {
2720                 cos_incompat = true;
2721                 mdt_object_put(info->mti_env, mold);
2722                 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
2723                 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
2724                 goto relock;
2725         }
2726
2727         /* find mnew object:
2728          * mnew target object may not exist now
2729          * lookup with version checking */
2730         fid_zero(new_fid);
2731         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
2732                                       3);
2733         if (rc == 0) {
2734                 /* the new_fid should have been filled at this moment */
2735                 if (lu_fid_eq(old_fid, new_fid))
2736                         GOTO(out_put_old, rc);
2737
2738                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
2739                     lu_fid_eq(new_fid, rr->rr_fid2))
2740                         GOTO(out_put_old, rc = -EINVAL);
2741
2742                 if (!fid_is_md_operative(new_fid))
2743                         GOTO(out_put_old, rc = -EPERM);
2744
2745                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
2746                 if (IS_ERR(mnew))
2747                         GOTO(out_put_old, rc = PTR_ERR(mnew));
2748
2749                 if (!mdt_object_exists(mnew)) {
2750                         LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2751                                         &mnew->mot_obj,
2752                                         "object does not exist");
2753                         GOTO(out_put_new, rc = -ENOENT);
2754                 }
2755
2756                 if (mdt_object_remote(mnew)) {
2757                         struct mdt_body  *repbody;
2758
2759                         /* Always send rename req to the target child MDT */
2760                         repbody = req_capsule_server_get(info->mti_pill,
2761                                                          &RMF_MDT_BODY);
2762                         LASSERT(repbody != NULL);
2763                         repbody->mbo_fid1 = *new_fid;
2764                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2765                         GOTO(out_put_new, rc = -EXDEV);
2766                 }
2767                 /* Before locking the target dir, check we do not replace
2768                  * a dir with a non-dir, otherwise it may deadlock with
2769                  * link op which tries to create a link in this dir
2770                  * back to this non-dir. */
2771                 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2772                     !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2773                         GOTO(out_put_new, rc = -EISDIR);
2774
2775                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2776                 mdt_lock_reg_init(lh_oldp, LCK_EX);
2777                 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2778                 if (mdt_object_remote(msrcdir)) {
2779                         /* Enqueue lookup lock from the parent MDT */
2780                         rc = mdt_remote_object_lock(info, msrcdir,
2781                                                     mdt_object_fid(mold),
2782                                                     &lh_oldp->mlh_rreg_lh,
2783                                                     lh_oldp->mlh_rreg_mode,
2784                                                     MDS_INODELOCK_LOOKUP,
2785                                                     false);
2786                         if (rc != ELDLM_OK)
2787                                 GOTO(out_put_new, rc);
2788
2789                         lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2790                 }
2791
2792                 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2793                                            cos_incompat);
2794                 if (rc != 0)
2795                         GOTO(out_unlock_old, rc);
2796
2797                 /* Check if @msrcdir is subdir of @mnew, before locking child
2798                  * to avoid reverse locking. */
2799                 if (mtgtdir != msrcdir) {
2800                         rc = mdo_is_subdir(info->mti_env,
2801                                            mdt_object_child(msrcdir), new_fid);
2802                         if (rc) {
2803                                 if (rc == 1)
2804                                         rc = -EINVAL;
2805                                 GOTO(out_unlock_old, rc);
2806                         }
2807                 }
2808
2809                 /* We used to acquire MDS_INODELOCK_FULL here but we
2810                  * can't do this now because a running HSM restore on
2811                  * the rename onto victim will hold the layout
2812                  * lock. See LU-4002. */
2813
2814                 lh_newp = &info->mti_lh[MDT_LH_NEW];
2815                 mdt_lock_reg_init(lh_newp, LCK_EX);
2816                 rc = mdt_reint_object_lock(info, mnew, lh_newp,
2817                                            MDS_INODELOCK_LOOKUP |
2818                                            MDS_INODELOCK_UPDATE,
2819                                            cos_incompat);
2820                 if (rc != 0)
2821                         GOTO(out_unlock_old, rc);
2822
2823                 /* get and save version after locking */
2824                 mdt_version_get_save(info, mnew, 3);
2825         } else if (rc != -EREMOTE && rc != -ENOENT) {
2826                 GOTO(out_put_old, rc);
2827         } else {
2828                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2829                 mdt_lock_reg_init(lh_oldp, LCK_EX);
2830                 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2831                 if (mdt_object_remote(msrcdir)) {
2832                         /* Enqueue lookup lock from the parent MDT */
2833                         rc = mdt_remote_object_lock(info, msrcdir,
2834                                                     mdt_object_fid(mold),
2835                                                     &lh_oldp->mlh_rreg_lh,
2836                                                     lh_oldp->mlh_rreg_mode,
2837                                                     MDS_INODELOCK_LOOKUP,
2838                                                     false);
2839                         if (rc != ELDLM_OK)
2840                                 GOTO(out_put_old, rc);
2841
2842                         lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2843                 }
2844
2845                 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2846                                            cos_incompat);
2847                 if (rc != 0)
2848                         GOTO(out_unlock_old, rc);
2849
2850                 mdt_enoent_version_save(info, 3);
2851         }
2852
2853         /* step 5: rename it */
2854         mdt_reint_init_ma(info, ma);
2855
2856         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
2857                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
2858
2859         if (mnew != NULL)
2860                 mutex_lock(&mnew->mot_lov_mutex);
2861
2862         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
2863                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
2864                         mnew != NULL ? mdt_object_child(mnew) : NULL,
2865                         &rr->rr_tgt_name, ma);
2866
2867         if (mnew != NULL)
2868                 mutex_unlock(&mnew->mot_lov_mutex);
2869
2870         /* handle last link of tgt object */
2871         if (rc == 0) {
2872                 mdt_counter_incr(req, LPROC_MDT_RENAME);
2873                 if (mnew) {
2874                         mdt_handle_last_unlink(info, mnew, ma);
2875                         discard = mdt_dom_check_for_discard(info, mnew);
2876                 }
2877                 mdt_rename_counter_tally(info, info->mti_mdt, req,
2878                                          msrcdir, mtgtdir);
2879         }
2880
2881         EXIT;
2882         if (mnew != NULL)
2883                 mdt_object_unlock(info, mnew, lh_newp, rc);
2884 out_unlock_old:
2885         mdt_object_unlock(info, mold, lh_oldp, rc);
2886 out_put_new:
2887         if (mnew && !discard)
2888                 mdt_object_put(info->mti_env, mnew);
2889 out_put_old:
2890         mdt_object_put(info->mti_env, mold);
2891 out_unlock_parents:
2892         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2893         mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2894 out_unlock_rename:
2895         if (lustre_handle_is_used(&rename_lh))
2896                 mdt_rename_unlock(&rename_lh);
2897 out_put_tgtdir:
2898         mdt_object_put(info->mti_env, mtgtdir);
2899 out_put_srcdir:
2900         mdt_object_put(info->mti_env, msrcdir);
2901
2902         /* The DoM discard can be done right in the place above where it is
2903          * assigned, meanwhile it is done here after rename unlock due to
2904          * compatibility with old clients, for them the discard blocks
2905          * the main thread until completion. Check LU-11359 for details.
2906          */
2907         if (discard) {
2908                 mdt_dom_discard_data(info, mnew);
2909                 mdt_object_put(info->mti_env, mnew);
2910         }
2911         return rc;
2912 }
2913
2914 static int mdt_reint_resync(struct mdt_thread_info *info,
2915                             struct mdt_lock_handle *lhc)
2916 {
2917         struct mdt_reint_record *rr = &info->mti_rr;
2918         struct ptlrpc_request *req = mdt_info_req(info);
2919         struct md_attr *ma = &info->mti_attr;
2920         struct mdt_object *mo;
2921         struct ldlm_lock *lease;
2922         struct mdt_body *repbody;
2923         struct md_layout_change layout = { .mlc_mirror_id = rr->rr_mirror_id };
2924         bool lease_broken;
2925         int rc, rc2;
2926
2927         ENTRY;
2928
2929         DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
2930
2931         if (info->mti_dlm_req)
2932                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2933
2934         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
2935         if (IS_ERR(mo))
2936                 GOTO(out, rc = PTR_ERR(mo));
2937
2938         if (!mdt_object_exists(mo))
2939                 GOTO(out_obj, rc = -ENOENT);
2940
2941         if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
2942                 GOTO(out_obj, rc = -EINVAL);
2943
2944         if (mdt_object_remote(mo))
2945                 GOTO(out_obj, rc = -EREMOTE);
2946
2947         lease = ldlm_handle2lock(rr->rr_lease_handle);
2948         if (lease == NULL)
2949                 GOTO(out_obj, rc = -ESTALE);
2950
2951         /* It's really necessary to grab open_sem and check if the lease lock
2952          * has been lost. There would exist a concurrent writer coming in and
2953          * generating some dirty data in memory cache, the writeback would fail
2954          * after the layout version is increased by MDS_REINT_RESYNC RPC. */
2955         if (!down_write_trylock(&mo->mot_open_sem))
2956                 GOTO(out_put_lease, rc = -EBUSY);
2957
2958         lock_res_and_lock(lease);
2959         lease_broken = ldlm_is_cancel(lease);
2960         unlock_res_and_lock(lease);
2961         if (lease_broken)
2962                 GOTO(out_unlock, rc = -EBUSY);
2963
2964         /* the file has yet opened by anyone else after we took the lease. */
2965         layout.mlc_opc = MD_LAYOUT_RESYNC;
2966         lhc = &info->mti_lh[MDT_LH_LOCAL];
2967         rc = mdt_layout_change(info, mo, lhc, &layout);
2968         if (rc)
2969                 GOTO(out_unlock, rc);
2970
2971         mdt_object_unlock(info, mo, lhc, 0);
2972
2973         ma->ma_need = MA_INODE;
2974         ma->ma_valid = 0;
2975         rc = mdt_attr_get_complex(info, mo, ma);
2976         if (rc != 0)
2977                 GOTO(out_unlock, rc);
2978
2979         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2980         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
2981
2982         EXIT;
2983 out_unlock:
2984         up_write(&mo->mot_open_sem);
2985 out_put_lease:
2986         LDLM_LOCK_PUT(lease);
2987 out_obj:
2988         mdt_object_put(info->mti_env, mo);
2989 out:
2990         mdt_client_compatibility(info);
2991         rc2 = mdt_fix_reply(info);
2992         if (rc == 0)
2993                 rc = rc2;
2994         return rc;
2995 }
2996
2997 struct mdt_reinter {
2998         int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
2999         enum lprocfs_extra_opc mr_extra_opc;
3000 };
3001
3002 static const struct mdt_reinter mdt_reinters[] = {
3003         [REINT_SETATTR] = {
3004                 .mr_handler = &mdt_reint_setattr,
3005                 .mr_extra_opc = MDS_REINT_SETATTR,
3006         },
3007         [REINT_CREATE] = {
3008                 .mr_handler = &mdt_reint_create,
3009                 .mr_extra_opc = MDS_REINT_CREATE,
3010         },
3011         [REINT_LINK] = {
3012                 .mr_handler = &mdt_reint_link,
3013                 .mr_extra_opc = MDS_REINT_LINK,
3014         },
3015         [REINT_UNLINK] = {
3016                 .mr_handler = &mdt_reint_unlink,
3017                 .mr_extra_opc = MDS_REINT_UNLINK,
3018         },
3019         [REINT_RENAME] = {
3020                 .mr_handler = &mdt_reint_rename,
3021                 .mr_extra_opc = MDS_REINT_RENAME,
3022         },
3023         [REINT_OPEN] = {
3024                 .mr_handler = &mdt_reint_open,
3025                 .mr_extra_opc = MDS_REINT_OPEN,
3026         },
3027         [REINT_SETXATTR] = {
3028                 .mr_handler = &mdt_reint_setxattr,
3029                 .mr_extra_opc = MDS_REINT_SETXATTR,
3030         },
3031         [REINT_RMENTRY] = {
3032                 .mr_handler = &mdt_reint_unlink,
3033                 .mr_extra_opc = MDS_REINT_UNLINK,
3034         },
3035         [REINT_MIGRATE] = {
3036                 .mr_handler = &mdt_reint_migrate,
3037                 .mr_extra_opc = MDS_REINT_RENAME,
3038         },
3039         [REINT_RESYNC] = {
3040                 .mr_handler = &mdt_reint_resync,
3041                 .mr_extra_opc = MDS_REINT_RESYNC,
3042         },
3043 };
3044
3045 int mdt_reint_rec(struct mdt_thread_info *info,
3046                   struct mdt_lock_handle *lhc)
3047 {
3048         const struct mdt_reinter *mr;
3049         int rc;
3050         ENTRY;
3051
3052         if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
3053                 RETURN(-EPROTO);
3054
3055         mr = &mdt_reinters[info->mti_rr.rr_opcode];
3056         if (mr->mr_handler == NULL)
3057                 RETURN(-EPROTO);
3058
3059         rc = (*mr->mr_handler)(info, lhc);
3060
3061         lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
3062                              PTLRPC_LAST_CNTR + mr->mr_extra_opc);
3063
3064         RETURN(rc);
3065 }