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