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