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