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