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