Whamcloud - gitweb
LU-4788 lfsck: verify .lustre/lost+found at the LFSCK start
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_reint.c
37  *
38  * Lustre Metadata Target (mdt) reintegration routines
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  * Author: Huang Hua <huanghua@clusterfs.com>
44  * Author: Yury Umanets <umka@clusterfs.com>
45  */
46
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include "mdt_internal.h"
50 #include <lustre_lmv.h>
51
52 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
53                                      struct md_attr *ma)
54 {
55         ma->ma_need = MA_INODE;
56         ma->ma_valid = 0;
57 }
58
59 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
60                                 struct mdt_object *object,
61                                 struct mdt_body *repbody)
62 {
63         ENTRY;
64
65         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
66          * we won't go to below*/
67         if (repbody->mbo_valid & OBD_MD_FLMDSCAPA)
68                 RETURN(rc);
69
70         if (rc == 0 && info->mti_mdt->mdt_lut.lut_mds_capa &&
71             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
72                 struct lustre_capa *capa;
73
74                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
75                 LASSERT(capa);
76                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
77                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
78                                  0);
79                 if (rc == 0)
80                         repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
81         }
82
83         RETURN(rc);
84 }
85
86 /**
87  * Get version of object by fid.
88  *
89  * Return real version or ENOENT_VERSION if object doesn't exist
90  */
91 static void mdt_obj_version_get(struct mdt_thread_info *info,
92                                 struct mdt_object *o, __u64 *version)
93 {
94         LASSERT(o);
95         if (mdt_object_exists(o) && !mdt_object_remote(o) &&
96             !fid_is_obf(mdt_object_fid(o)))
97                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
98         else
99                 *version = ENOENT_VERSION;
100         CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
101                PFID(mdt_object_fid(o)), *version);
102 }
103
104 /**
105  * Check version is correct.
106  *
107  * Should be called only during replay.
108  */
109 static int mdt_version_check(struct ptlrpc_request *req,
110                              __u64 version, int idx)
111 {
112         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
113         ENTRY;
114
115         if (!exp_connect_vbr(req->rq_export))
116                 RETURN(0);
117
118         LASSERT(req_is_replay(req));
119         /** VBR: version is checked always because costs nothing */
120         LASSERT(idx < PTLRPC_NUM_VERSIONS);
121         /** Sanity check for malformed buffers */
122         if (pre_ver == NULL) {
123                 CERROR("No versions in request buffer\n");
124                 spin_lock(&req->rq_export->exp_lock);
125                 req->rq_export->exp_vbr_failed = 1;
126                 spin_unlock(&req->rq_export->exp_lock);
127                 RETURN(-EOVERFLOW);
128         } else if (pre_ver[idx] != version) {
129                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
130                        pre_ver[idx], version);
131                 spin_lock(&req->rq_export->exp_lock);
132                 req->rq_export->exp_vbr_failed = 1;
133                 spin_unlock(&req->rq_export->exp_lock);
134                 RETURN(-EOVERFLOW);
135         }
136         RETURN(0);
137 }
138
139 /**
140  * Save pre-versions in reply.
141  */
142 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
143                              int idx)
144 {
145         __u64 *reply_ver;
146
147         if (!exp_connect_vbr(req->rq_export))
148                 return;
149
150         LASSERT(!req_is_replay(req));
151         LASSERT(req->rq_repmsg != NULL);
152         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
153         if (reply_ver)
154                 reply_ver[idx] = version;
155 }
156
157 /**
158  * Save enoent version, it is needed when it is obvious that object doesn't
159  * exist, e.g. child during create.
160  */
161 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
162 {
163         /* save version of file name for replay, it must be ENOENT here */
164         if (!req_is_replay(mdt_info_req(info))) {
165                 info->mti_ver[idx] = ENOENT_VERSION;
166                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
167         }
168 }
169
170 /**
171  * Get version from disk and save in reply buffer.
172  *
173  * Versions are saved in reply only during normal operations not replays.
174  */
175 void mdt_version_get_save(struct mdt_thread_info *info,
176                           struct mdt_object *mto, int idx)
177 {
178         /* don't save versions during replay */
179         if (!req_is_replay(mdt_info_req(info))) {
180                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
181                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
182         }
183 }
184
185 /**
186  * Get version from disk and check it, no save in reply.
187  */
188 int mdt_version_get_check(struct mdt_thread_info *info,
189                           struct mdt_object *mto, int idx)
190 {
191         /* only check versions during replay */
192         if (!req_is_replay(mdt_info_req(info)))
193                 return 0;
194
195         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
196         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
197 }
198
199 /**
200  * Get version from disk and check if recovery or just save.
201  */
202 int mdt_version_get_check_save(struct mdt_thread_info *info,
203                                struct mdt_object *mto, int idx)
204 {
205         int rc = 0;
206
207         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
208         if (req_is_replay(mdt_info_req(info)))
209                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
210                                        idx);
211         else
212                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
213         return rc;
214 }
215
216 /**
217  * Lookup with version checking.
218  *
219  * This checks version of 'name'. Many reint functions uses 'name' for child not
220  * FID, therefore we need to get object by name and check its version.
221  */
222 int mdt_lookup_version_check(struct mdt_thread_info *info,
223                              struct mdt_object *p, const struct lu_name *lname,
224                              struct lu_fid *fid, int idx)
225 {
226         int rc, vbrc;
227
228         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
229                         &info->mti_spec);
230         /* Check version only during replay */
231         if (!req_is_replay(mdt_info_req(info)))
232                 return rc;
233
234         info->mti_ver[idx] = ENOENT_VERSION;
235         if (rc == 0) {
236                 struct mdt_object *child;
237                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
238                 if (likely(!IS_ERR(child))) {
239                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
240                         mdt_object_put(info->mti_env, child);
241                 }
242         }
243         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
244         return vbrc ? vbrc : rc;
245
246 }
247
248 /**
249  * mdt_remote_permission: Check whether the remote operation is permitted,
250  *
251  * Before we implement async cross-MDT updates (DNE phase 2). There are a few
252  * limitations here:
253  *
254  * 1.Only sysadmin can create remote directory and striped directory and
255  *   migrate directory now, unless
256  *   lctl set_param mdt.*.enable_remote_dir_gid=allow_gid.
257  * 2.Remote directory can only be created on MDT0, unless
258  *   lctl set_param mdt.*.enable_remote_dir = 1
259  * 3.Only new clients can access remote dir( >= 2.4) and striped dir(>= 2.6),
260  *   old client will return -ENOTSUPP.
261  *
262  * XXX these check are only needed for remote synchronization, once async
263  * update is supported, these check will be removed.
264  *
265  * param[in]info:       execution environment.
266  * param[in]parent:     the directory of this operation.
267  * param[in]child:      the child of this operation.
268  *
269  * retval       = 0 remote operation is allowed.
270  *              < 0 remote operation is denied.
271  */
272 static int mdt_remote_permission(struct mdt_thread_info *info,
273                                  struct mdt_object *parent,
274                                  struct mdt_object *child)
275 {
276         struct mdt_device       *mdt = info->mti_mdt;
277         struct lu_ucred         *uc  = mdt_ucred(info);
278         struct md_op_spec       *spec = &info->mti_spec;
279         struct lu_attr          *attr = &info->mti_attr.ma_attr;
280         struct obd_export       *exp = mdt_info_req(info)->rq_export;
281
282         /* Only check create remote directory, striped directory and
283          * migration */
284         if (mdt_object_remote(parent) == 0 && mdt_object_remote(child) == 0 &&
285             !(S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
286                                         spec->u.sp_ea.eadatalen != 0) &&
287             info->mti_rr.rr_opcode != REINT_MIGRATE)
288                 return 0;
289
290         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
291                 if (uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
292                     mdt->mdt_enable_remote_dir_gid != -1)
293                         return -EPERM;
294         }
295
296         if (mdt->mdt_enable_remote_dir == 0) {
297                 struct seq_server_site  *ss = mdt_seq_site(mdt);
298                 struct lu_seq_range     range = { 0 };
299                 int                     rc;
300
301                 fld_range_set_type(&range, LU_SEQ_RANGE_MDT);
302                 rc = fld_server_lookup(info->mti_env, ss->ss_server_fld,
303                                        fid_seq(mdt_object_fid(parent)), &range);
304                 if (rc != 0)
305                         return rc;
306
307                 if (range.lsr_index != 0)
308                         return -EPERM;
309         }
310
311         if (!mdt_is_dne_client(exp))
312                 return -ENOTSUPP;
313
314         if (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
315             spec->u.sp_ea.eadatalen != 0 && !mdt_is_striped_client(exp))
316                 return -ENOTSUPP;
317
318         return 0;
319 }
320
321 /*
322  * VBR: we save three versions in reply:
323  * 0 - parent. Check that parent version is the same during replay.
324  * 1 - name. Version of 'name' if file exists with the same name or
325  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
326  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
327  * check.
328  */
329 static int mdt_md_create(struct mdt_thread_info *info)
330 {
331         struct mdt_device       *mdt = info->mti_mdt;
332         struct mdt_object       *parent;
333         struct mdt_object       *child;
334         struct mdt_lock_handle  *lh;
335         struct mdt_body         *repbody;
336         struct md_attr          *ma = &info->mti_attr;
337         struct mdt_reint_record *rr = &info->mti_rr;
338         int rc;
339         ENTRY;
340
341         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  ("DNAME"->"DFID") "
342                   "in "DFID,
343                   PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
344
345         if (!fid_is_md_operative(rr->rr_fid1))
346                 RETURN(-EPERM);
347
348         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
349
350         parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
351         if (IS_ERR(parent))
352                 RETURN(PTR_ERR(parent));
353
354         if (!mdt_object_exists(parent))
355                 GOTO(put_parent, rc = -ENOENT);
356
357         lh = &info->mti_lh[MDT_LH_PARENT];
358         mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
359         rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE,
360                              MDT_CROSS_LOCK);
361         if (rc)
362                 GOTO(put_parent, rc);
363
364         if (!mdt_object_remote(parent)) {
365                 rc = mdt_version_get_check_save(info, parent, 0);
366                 if (rc)
367                         GOTO(unlock_parent, rc);
368         }
369
370         /*
371          * Check child name version during replay.
372          * During create replay a file may exist with same name.
373          */
374         rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
375                                       &info->mti_tmp_fid1, 1);
376         if (rc == 0)
377                 GOTO(unlock_parent, rc = -EEXIST);
378
379         /* -ENOENT is expected here */
380         if (rc != -ENOENT)
381                 GOTO(unlock_parent, rc);
382
383         /* save version of file name for replay, it must be ENOENT here */
384         mdt_enoent_version_save(info, 1);
385
386         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
387         if (likely(!IS_ERR(child))) {
388                 struct md_object *next = mdt_object_child(parent);
389
390                 rc = mdt_remote_permission(info, parent, child);
391                 if (rc != 0)
392                         GOTO(out_put_child, rc);
393
394                 ma->ma_need = MA_INODE;
395                 ma->ma_valid = 0;
396                 /* capa for cross-ref will be stored here */
397                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
398                                                      &RMF_CAPA1);
399                 LASSERT(ma->ma_capa);
400
401                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
402                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
403
404                 /* Version of child will be updated on disk. */
405                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
406                 rc = mdt_version_get_check_save(info, child, 2);
407                 if (rc)
408                         GOTO(out_put_child, rc);
409
410                 /* Let lower layer know current lock mode. */
411                 info->mti_spec.sp_cr_mode =
412                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
413
414                 /*
415                  * Do not perform lookup sanity check. We know that name does
416                  * not exist.
417                  */
418                 info->mti_spec.sp_cr_lookup = 0;
419                 info->mti_spec.sp_feat = &dt_directory_features;
420
421                 rc = mdo_create(info->mti_env, next, &rr->rr_name,
422                                 mdt_object_child(child), &info->mti_spec, ma);
423                 if (rc == 0)
424                         rc = mdt_attr_get_complex(info, child, ma);
425
426                 if (rc == 0) {
427                         /* Return fid & attr to client. */
428                         if (ma->ma_valid & MA_INODE)
429                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
430                                                    mdt_object_fid(child));
431                 }
432 out_put_child:
433                 mdt_create_pack_capa(info, rc, child, repbody);
434                 mdt_object_put(info->mti_env, child);
435         } else {
436                 rc = PTR_ERR(child);
437                 mdt_create_pack_capa(info, rc, NULL, repbody);
438         }
439 unlock_parent:
440         mdt_object_unlock(info, parent, lh, rc);
441 put_parent:
442         mdt_object_put(info->mti_env, parent);
443         RETURN(rc);
444 }
445
446 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
447                              struct mdt_object *obj, __u64 ibits,
448                              struct mdt_lock_handle *s0_lh,
449                              struct mdt_object *s0_obj,
450                              struct ldlm_enqueue_info *einfo)
451 {
452         ldlm_policy_data_t      *policy = &mti->mti_policy;
453         int                     rc;
454         ENTRY;
455
456         if (!S_ISDIR(obj->mot_header.loh_attr))
457                 RETURN(0);
458
459         /* Unlock stripe 0 */
460         if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
461                 LASSERT(s0_obj != NULL);
462                 mdt_object_unlock_put(mti, s0_obj, s0_lh, 1);
463         }
464
465         memset(policy, 0, sizeof(*policy));
466         policy->l_inodebits.bits = ibits;
467
468         rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
469                               policy);
470         RETURN(rc);
471 }
472
473 /**
474  * Lock slave stripes if necessary, the lock handles of slave stripes
475  * will be stored in einfo->ei_cbdata.
476  **/
477 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
478                            ldlm_mode_t mode, __u64 ibits,
479                            struct mdt_lock_handle *s0_lh,
480                            struct mdt_object **s0_objp,
481                            struct ldlm_enqueue_info *einfo)
482 {
483         ldlm_policy_data_t      *policy = &mti->mti_policy;
484         struct lu_buf           *buf = &mti->mti_buf;
485         struct lmv_mds_md_v1    *lmv;
486         struct lu_fid           *fid = &mti->mti_tmp_fid1;
487         int rc;
488         ENTRY;
489
490         if (!S_ISDIR(obj->mot_header.loh_attr))
491                 RETURN(0);
492
493         buf->lb_buf = mti->mti_xattr_buf;
494         buf->lb_len = sizeof(mti->mti_xattr_buf);
495         rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
496                           XATTR_NAME_LMV);
497         if (rc == -ERANGE) {
498                 rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
499                 if (rc > 0) {
500                         buf->lb_buf = mti->mti_big_lmm;
501                         buf->lb_len = mti->mti_big_lmmsize;
502                 }
503         }
504
505         if (rc == -ENODATA || rc == -ENOENT)
506                 RETURN(0);
507
508         if (rc <= 0)
509                 RETURN(rc);
510
511         lmv = buf->lb_buf;
512         if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
513                 RETURN(-EINVAL);
514
515         /* Sigh, 0_stripe and master object are different
516          * object, though they are in the same MDT, to avoid
517          * adding osd_object_lock here, so we will enqueue the
518          * stripe0 lock in MDT0 for now */
519         fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
520         *s0_objp = mdt_object_find_lock(mti, fid, s0_lh, ibits);
521         if (IS_ERR(*s0_objp))
522                 RETURN(PTR_ERR(*s0_objp));
523
524         memset(einfo, 0, sizeof(*einfo));
525         einfo->ei_type = LDLM_IBITS;
526         einfo->ei_mode = mode;
527         einfo->ei_cb_bl = mdt_remote_blocking_ast;
528         einfo->ei_cb_cp = ldlm_completion_ast;
529         einfo->ei_enq_slave = 1;
530         memset(policy, 0, sizeof(*policy));
531         policy->l_inodebits.bits = ibits;
532
533         rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
534                             policy);
535         RETURN(rc);
536 }
537
538 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
539                  struct md_attr *ma, int flags)
540 {
541         struct mdt_lock_handle  *lh;
542         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
543         __u64 lockpart = MDS_INODELOCK_UPDATE;
544         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
545         struct mdt_lock_handle  *s0_lh;
546         struct mdt_object       *s0_obj = NULL;
547         int rc;
548         ENTRY;
549
550         /* attr shouldn't be set on remote object */
551         LASSERT(!mdt_object_remote(mo));
552
553         lh = &info->mti_lh[MDT_LH_PARENT];
554         mdt_lock_reg_init(lh, LCK_PW);
555
556         /* Even though the new MDT will grant PERM lock to the old
557          * client, but the old client will almost ignore that during
558          * So it needs to revoke both LOOKUP and PERM lock here, so
559          * both new and old client can cancel the dcache */
560         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
561                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
562
563         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
564         if (rc != 0)
565                 RETURN(rc);
566
567         s0_lh = &info->mti_lh[MDT_LH_LOCAL];
568         mdt_lock_reg_init(s0_lh, LCK_EX);
569         rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
570         if (rc != 0)
571                 GOTO(out_unlock, rc);
572
573         if (mdt_object_exists(mo) == 0)
574                 GOTO(out_unlock, rc = -ENOENT);
575
576         /* all attrs are packed into mti_attr in unpack_setattr */
577         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
578                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
579
580         /* This is only for set ctime when rename's source is on remote MDS. */
581         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
582                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
583
584         /* VBR: update version if attr changed are important for recovery */
585         if (do_vbr) {
586                 /* update on-disk version of changed object */
587                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
588                 rc = mdt_version_get_check_save(info, mo, 0);
589                 if (rc)
590                         GOTO(out_unlock, rc);
591         }
592
593         /* Ensure constant striping during chown(). See LU-2789. */
594         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
595                 mutex_lock(&mo->mot_lov_mutex);
596
597         /* all attrs are packed into mti_attr in unpack_setattr */
598         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
599
600         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
601                 mutex_unlock(&mo->mot_lov_mutex);
602
603         if (rc != 0)
604                 GOTO(out_unlock, rc);
605
606         EXIT;
607 out_unlock:
608         mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
609         mdt_object_unlock(info, mo, lh, rc);
610         return rc;
611 }
612
613 /**
614  * Check HSM flags and add HS_DIRTY flag if relevant.
615  *
616  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
617  * and is not RELEASED.
618  */
619 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
620                         struct md_attr *ma)
621 {
622         int rc;
623         ENTRY;
624
625         /* If the file was modified, add the dirty flag */
626         ma->ma_need = MA_HSM;
627         rc = mdt_attr_get_complex(info, mo, ma);
628         if (rc) {
629                 CERROR("file attribute read error for "DFID": %d.\n",
630                         PFID(mdt_object_fid(mo)), rc);
631                 RETURN(rc);
632         }
633
634         /* If an up2date copy exists in the backend, add dirty flag */
635         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
636             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
637                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
638
639                 ma->ma_hsm.mh_flags |= HS_DIRTY;
640
641                 mdt_lock_reg_init(lh, LCK_PW);
642                 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
643                                      MDT_LOCAL_LOCK);
644                 if (rc != 0)
645                         RETURN(rc);
646
647                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
648                 if (rc)
649                         CERROR("file attribute change error for "DFID": %d\n",
650                                 PFID(mdt_object_fid(mo)), rc);
651                 mdt_object_unlock(info, mo, lh, rc);
652         }
653
654         RETURN(rc);
655 }
656
657 static int mdt_reint_setattr(struct mdt_thread_info *info,
658                              struct mdt_lock_handle *lhc)
659 {
660         struct md_attr          *ma = &info->mti_attr;
661         struct mdt_reint_record *rr = &info->mti_rr;
662         struct ptlrpc_request   *req = mdt_info_req(info);
663         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
664         struct mdt_file_data    *mfd;
665         struct mdt_object       *mo;
666         struct mdt_body         *repbody;
667         int                      som_au, rc, rc2;
668         ENTRY;
669
670         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
671                   (unsigned int)ma->ma_attr.la_valid);
672
673         if (info->mti_dlm_req)
674                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
675
676         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
677         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
678         if (IS_ERR(mo))
679                 GOTO(out, rc = PTR_ERR(mo));
680
681         /* start a log jounal handle if needed */
682         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
683                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
684                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
685                         /* Check write access for the O_TRUNC case */
686                         if (mdt_write_read(mo) < 0)
687                                 GOTO(out_put, rc = -ETXTBSY);
688                 }
689         } else if (info->mti_ioepoch &&
690                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
691                 /* Truncate case. IOEpoch is opened. */
692                 rc = mdt_write_get(mo);
693                 if (rc)
694                         GOTO(out_put, rc);
695
696                 mfd = mdt_mfd_new(med);
697                 if (mfd == NULL) {
698                         mdt_write_put(mo);
699                         GOTO(out_put, rc = -ENOMEM);
700                 }
701
702                 mdt_ioepoch_open(info, mo, 0);
703                 repbody->mbo_ioepoch = mo->mot_ioepoch;
704
705                 mdt_object_get(info->mti_env, mo);
706                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
707                 mfd->mfd_object = mo;
708                 mfd->mfd_xid = req->rq_xid;
709
710                 spin_lock(&med->med_open_lock);
711                 list_add(&mfd->mfd_list, &med->med_open_head);
712                 spin_unlock(&med->med_open_lock);
713                 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
714         }
715
716         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
717         if (som_au) {
718                 /* SOM Attribute update case. Find the proper mfd and update
719                  * SOM attributes on the proper object. */
720                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
721                 LASSERT(info->mti_ioepoch);
722
723                 spin_lock(&med->med_open_lock);
724                 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
725                                      req_is_replay(req));
726                 if (mfd == NULL) {
727                         spin_unlock(&med->med_open_lock);
728                         CDEBUG(D_INODE, "no handle for file close: "
729                                "fid = "DFID": cookie = "LPX64"\n",
730                                PFID(info->mti_rr.rr_fid1),
731                                info->mti_ioepoch->handle.cookie);
732                         GOTO(out_put, rc = -ESTALE);
733                 }
734                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
735                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
736
737                 class_handle_unhash(&mfd->mfd_handle);
738                 list_del_init(&mfd->mfd_list);
739                 spin_unlock(&med->med_open_lock);
740
741                 mdt_mfd_close(info, mfd);
742         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
743                 LASSERT((ma->ma_valid & MA_LOV) == 0);
744                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
745                 if (rc)
746                         GOTO(out_put, rc);
747         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
748                 struct lu_buf *buf  = &info->mti_buf;
749                 LASSERT(ma->ma_attr.la_valid == 0);
750                 buf->lb_buf = ma->ma_lmm;
751                 buf->lb_len = ma->ma_lmm_size;
752                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
753                                   buf, XATTR_NAME_LOV, 0);
754                 if (rc)
755                         GOTO(out_put, rc);
756         } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
757                 struct lu_buf *buf  = &info->mti_buf;
758
759                 LASSERT(ma->ma_attr.la_valid == 0);
760                 buf->lb_buf = ma->ma_lmv;
761                 buf->lb_len = ma->ma_lmv_size;
762                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
763                                   buf, XATTR_NAME_DEFAULT_LMV, 0);
764                 if (rc)
765                         GOTO(out_put, rc);
766         } else
767                 LBUG();
768
769         /* If file data is modified, add the dirty flag */
770         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
771                 rc = mdt_add_dirty_flag(info, mo, ma);
772
773         ma->ma_need = MA_INODE;
774         ma->ma_valid = 0;
775         rc = mdt_attr_get_complex(info, mo, ma);
776         if (rc != 0)
777                 GOTO(out_put, rc);
778
779         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
780
781         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
782             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
783             S_ISREG(lu_object_attr(&mo->mot_obj)) &&
784             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
785                 struct lustre_capa *capa;
786
787                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
788                 LASSERT(capa);
789                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
790                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
791                 if (rc)
792                         GOTO(out_put, rc);
793                 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
794         }
795
796         EXIT;
797 out_put:
798         mdt_object_put(info->mti_env, mo);
799 out:
800         if (rc == 0)
801                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
802
803         mdt_client_compatibility(info);
804         rc2 = mdt_fix_reply(info);
805         if (rc == 0)
806                 rc = rc2;
807         return rc;
808 }
809
810 static int mdt_reint_create(struct mdt_thread_info *info,
811                             struct mdt_lock_handle *lhc)
812 {
813         struct ptlrpc_request   *req = mdt_info_req(info);
814         int                     rc;
815         ENTRY;
816
817         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
818                 RETURN(err_serious(-ESTALE));
819
820         if (info->mti_dlm_req)
821                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
822
823         if (!lu_name_is_valid(&info->mti_rr.rr_name))
824                 RETURN(-EPROTO);
825
826         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
827         case S_IFDIR:
828                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
829                 break;
830         case S_IFREG:
831         case S_IFLNK:
832         case S_IFCHR:
833         case S_IFBLK:
834         case S_IFIFO:
835         case S_IFSOCK:
836                 /* Special file should stay on the same node as parent. */
837                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
838                 break;
839         default:
840                 CERROR("%s: Unsupported mode %o\n",
841                        mdt_obd_name(info->mti_mdt),
842                        info->mti_attr.ma_attr.la_mode);
843                 RETURN(err_serious(-EOPNOTSUPP));
844         }
845
846         rc = mdt_md_create(info);
847         RETURN(rc);
848 }
849
850 /*
851  * VBR: save parent version in reply and child version getting by its name.
852  * Version of child is getting and checking during its lookup. If
853  */
854 static int mdt_reint_unlink(struct mdt_thread_info *info,
855                             struct mdt_lock_handle *lhc)
856 {
857         struct mdt_reint_record *rr = &info->mti_rr;
858         struct ptlrpc_request   *req = mdt_info_req(info);
859         struct md_attr          *ma = &info->mti_attr;
860         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
861         struct mdt_object       *mp;
862         struct mdt_object       *mc;
863         struct mdt_lock_handle  *parent_lh;
864         struct mdt_lock_handle  *child_lh;
865         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
866         struct mdt_lock_handle  *s0_lh = NULL;
867         struct mdt_object       *s0_obj = NULL;
868         int                     rc;
869         int                     no_name = 0;
870         ENTRY;
871
872         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
873                   PNAME(&rr->rr_name));
874
875         if (info->mti_dlm_req)
876                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
877
878         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
879                 RETURN(err_serious(-ENOENT));
880
881         if (!fid_is_md_operative(rr->rr_fid1))
882                 RETURN(-EPERM);
883
884         /*
885          * step 1: Found the parent.
886          */
887         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
888         if (IS_ERR(mp)) {
889                 rc = PTR_ERR(mp);
890                 GOTO(out, rc);
891         }
892
893         parent_lh = &info->mti_lh[MDT_LH_PARENT];
894         mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
895         rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
896                              MDT_CROSS_LOCK);
897         if (rc != 0)
898                 GOTO(put_parent, rc);
899
900         if (!mdt_object_remote(mp)) {
901                 rc = mdt_version_get_check_save(info, mp, 0);
902                 if (rc)
903                         GOTO(unlock_parent, rc);
904         }
905
906         /* step 2: find & lock the child */
907         /* lookup child object along with version checking */
908         fid_zero(child_fid);
909         rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
910         if (rc != 0) {
911                 /* Name might not be able to find during resend of
912                  * remote unlink, considering following case.
913                  * dir_A is a remote directory, the name entry of
914                  * dir_A is on MDT0, the directory is on MDT1,
915                  *
916                  * 1. client sends unlink req to MDT1.
917                  * 2. MDT1 sends name delete update to MDT0.
918                  * 3. name entry is being deleted in MDT0 synchronously.
919                  * 4. MDT1 is restarted.
920                  * 5. client resends unlink req to MDT1. So it can not
921                  *    find the name entry on MDT0 anymore.
922                  * In this case, MDT1 only needs to destory the local
923                  * directory.
924                  * */
925                 if (mdt_object_remote(mp) && rc == -ENOENT &&
926                     !fid_is_zero(rr->rr_fid2) &&
927                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
928                         no_name = 1;
929                         *child_fid = *rr->rr_fid2;
930                  } else {
931                         GOTO(unlock_parent, rc);
932                  }
933         }
934
935         if (!fid_is_md_operative(child_fid))
936                 GOTO(unlock_parent, rc = -EPERM);
937
938         /* We will lock the child regardless it is local or remote. No harm. */
939         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
940         if (IS_ERR(mc))
941                 GOTO(unlock_parent, rc = PTR_ERR(mc));
942
943         child_lh = &info->mti_lh[MDT_LH_CHILD];
944         mdt_lock_reg_init(child_lh, LCK_EX);
945         if (mdt_object_remote(mc)) {
946                 struct mdt_body  *repbody;
947
948                 if (!fid_is_zero(rr->rr_fid2)) {
949                         CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
950                                mdt_obd_name(info->mti_mdt),
951                                PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
952                         GOTO(put_child, rc = -ENOENT);
953                 }
954                 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
955                        mdt_obd_name(info->mti_mdt),
956                        PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
957
958                 if (!mdt_is_dne_client(req->rq_export))
959                         /* Return -ENOTSUPP for old client */
960                         GOTO(put_child, rc = -ENOTSUPP);
961
962                 if (info->mti_spec.sp_rm_entry) {
963                         struct lu_ucred *uc  = mdt_ucred(info);
964
965                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
966                                 CERROR("%s: unlink remote entry is only "
967                                        "permitted for administrator: rc = %d\n",
968                                         mdt_obd_name(info->mti_mdt),
969                                         -EPERM);
970                                 GOTO(put_child, rc = -EPERM);
971                         }
972
973                         ma->ma_need = MA_INODE;
974                         ma->ma_valid = 0;
975                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
976                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
977                                         NULL, &rr->rr_name, ma, no_name);
978                         GOTO(put_child, rc);
979                 }
980                 /* Revoke the LOOKUP lock of the remote object granted by
981                  * this MDT. Since the unlink will happen on another MDT,
982                  * it will release the LOOKUP lock right away. Then What
983                  * would happen if another client try to grab the LOOKUP
984                  * lock at the same time with unlink XXX */
985                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
986                                 MDT_CROSS_LOCK);
987                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
988                 LASSERT(repbody != NULL);
989                 repbody->mbo_fid1 = *mdt_object_fid(mc);
990                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
991                 GOTO(unlock_child, rc = -EREMOTE);
992         } else if (info->mti_spec.sp_rm_entry) {
993                 rc = -EPERM;
994                 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
995                        "rc = %d\n",
996                        mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
997                 GOTO(put_child, rc);
998         }
999
1000         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1001          * this now because a running HSM restore on the child (unlink
1002          * victim) will hold the layout lock. See LU-4002. */
1003         rc = mdt_object_lock(info, mc, child_lh,
1004                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
1005                              MDT_CROSS_LOCK);
1006         if (rc != 0)
1007                 GOTO(put_child, rc);
1008         /*
1009          * Now we can only make sure we need MA_INODE, in mdd layer, will check
1010          * whether need MA_LOV and MA_COOKIE.
1011          */
1012         ma->ma_need = MA_INODE;
1013         ma->ma_valid = 0;
1014
1015         s0_lh = &info->mti_lh[MDT_LH_LOCAL];
1016         mdt_lock_reg_init(s0_lh, LCK_EX);
1017         rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
1018                              &s0_obj, einfo);
1019         if (rc != 0)
1020                 GOTO(unlock_child, rc);
1021
1022         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1023                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1024         /* save version when object is locked */
1025         mdt_version_get_save(info, mc, 1);
1026
1027         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1028
1029         mutex_lock(&mc->mot_lov_mutex);
1030
1031         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1032                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
1033
1034         mutex_unlock(&mc->mot_lov_mutex);
1035
1036         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
1037                 rc = mdt_attr_get_complex(info, mc, ma);
1038         if (rc == 0)
1039                 mdt_handle_last_unlink(info, mc, ma);
1040
1041         if (ma->ma_valid & MA_INODE) {
1042                 switch (ma->ma_attr.la_mode & S_IFMT) {
1043                 case S_IFDIR:
1044                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
1045                         break;
1046                 case S_IFREG:
1047                 case S_IFLNK:
1048                 case S_IFCHR:
1049                 case S_IFBLK:
1050                 case S_IFIFO:
1051                 case S_IFSOCK:
1052                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
1053                         break;
1054                 default:
1055                         LASSERTF(0, "bad file type %o unlinking\n",
1056                                  ma->ma_attr.la_mode);
1057                 }
1058         }
1059
1060         EXIT;
1061
1062 unlock_child:
1063         mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
1064         mdt_object_unlock(info, mc, child_lh, rc);
1065 put_child:
1066         mdt_object_put(info->mti_env, mc);
1067 unlock_parent:
1068         mdt_object_unlock(info, mp, parent_lh, rc);
1069 put_parent:
1070         mdt_object_put(info->mti_env, mp);
1071 out:
1072         return rc;
1073 }
1074
1075 /*
1076  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1077  * name.
1078  */
1079 static int mdt_reint_link(struct mdt_thread_info *info,
1080                           struct mdt_lock_handle *lhc)
1081 {
1082         struct mdt_reint_record *rr = &info->mti_rr;
1083         struct ptlrpc_request   *req = mdt_info_req(info);
1084         struct md_attr          *ma = &info->mti_attr;
1085         struct mdt_object       *ms;
1086         struct mdt_object       *mp;
1087         struct mdt_lock_handle  *lhs;
1088         struct mdt_lock_handle  *lhp;
1089         int rc;
1090         ENTRY;
1091
1092         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1093                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1094
1095         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1096                 RETURN(err_serious(-ENOENT));
1097
1098         if (info->mti_dlm_req)
1099                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1100
1101         /* Invalid case so return error immediately instead of
1102          * processing it */
1103         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1104                 RETURN(-EPERM);
1105
1106         if (!fid_is_md_operative(rr->rr_fid1) ||
1107             !fid_is_md_operative(rr->rr_fid2))
1108                 RETURN(-EPERM);
1109
1110         /* step 1: find & lock the target parent dir */
1111         lhp = &info->mti_lh[MDT_LH_PARENT];
1112         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1113         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1114                                   MDS_INODELOCK_UPDATE);
1115         if (IS_ERR(mp))
1116                 RETURN(PTR_ERR(mp));
1117
1118         rc = mdt_version_get_check_save(info, mp, 0);
1119         if (rc)
1120                 GOTO(out_unlock_parent, rc);
1121
1122         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1123
1124         /* step 2: find & lock the source */
1125         lhs = &info->mti_lh[MDT_LH_CHILD];
1126         mdt_lock_reg_init(lhs, LCK_EX);
1127
1128         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1129         if (IS_ERR(ms))
1130                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1131
1132         if (!mdt_object_exists(ms)) {
1133                 mdt_object_put(info->mti_env, ms);
1134                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1135                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1136                 GOTO(out_unlock_parent, rc = -ENOENT);
1137         }
1138
1139         if (mdt_object_remote(ms)) {
1140                 mdt_object_put(info->mti_env, ms);
1141                 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1142                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1143                        PFID(rr->rr_fid2));
1144                 GOTO(out_unlock_parent, rc = -EXDEV);
1145         }
1146
1147         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1148                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1149         if (rc != 0) {
1150                 mdt_object_put(info->mti_env, ms);
1151                 GOTO(out_unlock_parent, rc);
1152         }
1153
1154         /* step 3: link it */
1155         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1156                        OBD_FAIL_MDS_REINT_LINK_WRITE);
1157
1158         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1159         rc = mdt_version_get_check_save(info, ms, 1);
1160         if (rc)
1161                 GOTO(out_unlock_child, rc);
1162
1163         /** check target version by name during replay */
1164         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1165                                       &info->mti_tmp_fid1, 2);
1166         if (rc != 0 && rc != -ENOENT)
1167                 GOTO(out_unlock_child, rc);
1168         /* save version of file name for replay, it must be ENOENT here */
1169         if (!req_is_replay(mdt_info_req(info))) {
1170                 if (rc != -ENOENT) {
1171                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1172                                PNAME(&rr->rr_name));
1173                         GOTO(out_unlock_child, rc = -EEXIST);
1174                 }
1175                 info->mti_ver[2] = ENOENT_VERSION;
1176                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1177         }
1178
1179         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1180               mdt_object_child(ms), &rr->rr_name, ma);
1181
1182         if (rc == 0)
1183                 mdt_counter_incr(req, LPROC_MDT_LINK);
1184
1185         EXIT;
1186 out_unlock_child:
1187         mdt_object_unlock_put(info, ms, lhs, rc);
1188 out_unlock_parent:
1189         mdt_object_unlock_put(info, mp, lhp, rc);
1190         return rc;
1191 }
1192 /**
1193  * lock the part of the directory according to the hash of the name
1194  * (lh->mlh_pdo_hash) in parallel directory lock.
1195  */
1196 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1197                               struct mdt_lock_handle *lh,
1198                               struct mdt_object *obj, __u64 ibits)
1199 {
1200         struct ldlm_res_id *res = &info->mti_res_id;
1201         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1202         ldlm_policy_data_t *policy = &info->mti_policy;
1203         int rc;
1204
1205         /*
1206          * Finish res_id initializing by name hash marking part of
1207          * directory which is taking modification.
1208          */
1209         LASSERT(lh->mlh_pdo_hash != 0);
1210         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1211         memset(policy, 0, sizeof(*policy));
1212         policy->l_inodebits.bits = ibits;
1213         /*
1214          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1215          * going to be sent to client. If it is - mdt_intent_policy() path will
1216          * fix it up and turn FL_LOCAL flag off.
1217          */
1218         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1219                           res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1220                           &info->mti_exp->exp_handle.h_cookie);
1221         return rc;
1222 }
1223
1224 enum mdt_rename_lock {
1225         MRL_RENAME,
1226         MRL_MIGRATE,
1227 };
1228
1229 /**
1230  * Get BFL lock for rename or migrate process, right now, it does not support
1231  * cross-MDT rename, so we only need global rename lock during migration.
1232  **/
1233 static int mdt_rename_lock(struct mdt_thread_info *info,
1234                            struct lustre_handle *lh,
1235                            enum mdt_rename_lock rename_lock)
1236 {
1237         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
1238         ldlm_policy_data_t      *policy = &info->mti_policy;
1239         struct ldlm_res_id      *res_id = &info->mti_res_id;
1240         __u64                   flags = 0;
1241         int                     rc;
1242         ENTRY;
1243
1244         /* XXX only do global rename lock for migration */
1245         if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0 &&
1246             rename_lock == MRL_MIGRATE) {
1247                 struct lu_fid *fid = &info->mti_tmp_fid1;
1248                 struct mdt_object *obj;
1249
1250                 /* XXX, right now, it has to use object API to
1251                  * enqueue lock cross MDT, so it will enqueue
1252                  * rename lock(with LUSTRE_BFL_FID) by root object */
1253                 lu_root_fid(fid);
1254                 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1255                 if (IS_ERR(obj))
1256                         RETURN(PTR_ERR(obj));
1257
1258                 LASSERT(mdt_object_remote(obj));
1259                 rc = mdt_remote_object_lock(info, obj,
1260                                             &LUSTRE_BFL_FID, lh,
1261                                             LCK_EX,
1262                                             MDS_INODELOCK_UPDATE);
1263                 mdt_object_put(info->mti_env, obj);
1264         } else {
1265                 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1266                 memset(policy, 0, sizeof *policy);
1267                 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1268                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1269                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1270                                             LCK_EX, &flags, ldlm_blocking_ast,
1271                                             ldlm_completion_ast, NULL, NULL, 0,
1272                                             LVB_T_NONE,
1273                                             &info->mti_exp->exp_handle.h_cookie,
1274                                             lh);
1275         }
1276
1277         RETURN(rc);
1278 }
1279
1280 static void mdt_rename_unlock(struct lustre_handle *lh)
1281 {
1282         ENTRY;
1283         LASSERT(lustre_handle_is_used(lh));
1284         /* Cancel the single rename lock right away */
1285         ldlm_lock_decref_and_cancel(lh, LCK_EX);
1286         EXIT;
1287 }
1288
1289 /*
1290  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1291  * target. Source should not be ancestor of target dir. May be other rename
1292  * checks can be moved here later.
1293  */
1294 static int mdt_is_subdir(struct mdt_thread_info *info,
1295                          struct mdt_object *dir,
1296                          const struct lu_fid *fid)
1297 {
1298         struct lu_fid dir_fid = dir->mot_header.loh_fid;
1299         int rc = 0;
1300         ENTRY;
1301
1302         /* If the source and target are in the same directory, they can not
1303          * be parent/child relationship, so subdir check is not needed */
1304         if (lu_fid_eq(&dir_fid, fid))
1305                 return 0;
1306
1307         if (!mdt_object_exists(dir))
1308                 RETURN(-ENOENT);
1309
1310         rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1311                            fid, &dir_fid);
1312         if (rc < 0) {
1313                 CERROR("%s: failed subdir check in "DFID" for "DFID
1314                        ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1315                        PFID(&dir_fid), PFID(fid), rc);
1316                 /* Return EINVAL only if a parent is the @fid */
1317                 if (rc == -EINVAL)
1318                         rc = -EIO;
1319         } else {
1320                 /* check the found fid */
1321                 if (lu_fid_eq(&dir_fid, fid))
1322                         rc = -EINVAL;
1323         }
1324
1325         RETURN(rc);
1326 }
1327
1328 /* Update object linkEA */
1329 struct mdt_lock_list {
1330         struct mdt_object       *mll_obj;
1331         struct mdt_lock_handle  mll_lh;
1332         struct list_head        mll_list;
1333 };
1334
1335 static void mdt_unlock_list(struct mdt_thread_info *info,
1336                             struct list_head *list, int rc)
1337 {
1338         struct mdt_lock_list *mll;
1339         struct mdt_lock_list *mll2;
1340
1341         list_for_each_entry_safe(mll, mll2, list, mll_list) {
1342                 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1343                 list_del(&mll->mll_list);
1344                 OBD_FREE_PTR(mll);
1345         }
1346 }
1347
1348 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1349                                       struct mdt_object *obj,
1350                                       struct mdt_object *pobj,
1351                                       struct list_head *lock_list)
1352 {
1353         struct lu_buf           *buf = &info->mti_big_buf;
1354         struct linkea_data      ldata = { 0 };
1355         int                     count;
1356         int                     rc;
1357         ENTRY;
1358
1359         if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1360                 RETURN(0);
1361
1362         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1363         if (buf->lb_buf == NULL)
1364                 RETURN(-ENOMEM);
1365
1366         ldata.ld_buf = buf;
1367         rc = mdt_links_read(info, obj, &ldata);
1368         if (rc != 0) {
1369                 if (rc == -ENOENT || rc == -ENODATA)
1370                         rc = 0;
1371                 RETURN(rc);
1372         }
1373
1374         LASSERT(ldata.ld_leh != NULL);
1375         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1376         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1377                 struct mdt_device *mdt = info->mti_mdt;
1378                 struct mdt_object *mdt_pobj;
1379                 struct mdt_lock_list *mll;
1380                 struct lu_name name;
1381                 struct lu_fid  fid;
1382
1383                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1384                                     &name, &fid);
1385                 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1386                                                          ldata.ld_reclen);
1387                 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1388                 if (IS_ERR(mdt_pobj)) {
1389                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1390                               mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1391                         continue;
1392                 }
1393
1394                 if (!mdt_object_exists(mdt_pobj)) {
1395                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1396                               mdt_obd_name(mdt), PFID(&fid));
1397                         mdt_object_put(info->mti_env, mdt_pobj);
1398                         continue;
1399                 }
1400
1401                 if (mdt_pobj == pobj) {
1402                         CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1403                                mdt_obd_name(mdt), PFID(&fid));
1404                         mdt_object_put(info->mti_env, mdt_pobj);
1405                         continue;
1406                 }
1407
1408                 OBD_ALLOC_PTR(mll);
1409                 if (mll == NULL) {
1410                         mdt_object_put(info->mti_env, mdt_pobj);
1411                         GOTO(out, rc = -ENOMEM);
1412                 }
1413
1414                 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1415                 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1416                                      MDS_INODELOCK_UPDATE,
1417                                      MDT_CROSS_LOCK);
1418                 if (rc != 0) {
1419                         CERROR("%s: cannot lock "DFID": rc =%d\n",
1420                                mdt_obd_name(mdt), PFID(&fid), rc);
1421                         mdt_object_put(info->mti_env, mdt_pobj);
1422                         OBD_FREE_PTR(mll);
1423                         GOTO(out, rc);
1424                 }
1425
1426                 INIT_LIST_HEAD(&mll->mll_list);
1427                 mll->mll_obj = mdt_pobj;
1428                 list_add_tail(&mll->mll_list, lock_list);
1429         }
1430 out:
1431         if (rc != 0)
1432                 mdt_unlock_list(info, lock_list, rc);
1433         RETURN(rc);
1434 }
1435
1436 /* migrate files from one MDT to another MDT */
1437 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1438                                       struct mdt_lock_handle *lhc)
1439 {
1440         struct mdt_reint_record *rr = &info->mti_rr;
1441         struct md_attr          *ma = &info->mti_attr;
1442         struct mdt_object       *msrcdir;
1443         struct mdt_object       *mold;
1444         struct mdt_object       *mnew = NULL;
1445         struct mdt_lock_handle  *lh_dirp;
1446         struct mdt_lock_handle  *lh_childp;
1447         struct mdt_lock_handle  *lh_tgtp = NULL;
1448         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1449         struct list_head        lock_list;
1450         int                     rc;
1451         ENTRY;
1452
1453         CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1454                PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1455
1456         /* 1: lock the source dir. */
1457         msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1458         if (IS_ERR(msrcdir)) {
1459                 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1460                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1461                         (int)PTR_ERR(msrcdir));
1462                 RETURN(PTR_ERR(msrcdir));
1463         }
1464
1465         lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1466         mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1467         rc = mdt_object_lock(info, msrcdir, lh_dirp,
1468                              MDS_INODELOCK_UPDATE,
1469                              MDT_CROSS_LOCK);
1470         if (rc)
1471                 GOTO(out_put_parent, rc);
1472
1473         if (!mdt_object_remote(msrcdir)) {
1474                 rc = mdt_version_get_check_save(info, msrcdir, 0);
1475                 if (rc)
1476                         GOTO(out_unlock_parent, rc);
1477         }
1478
1479         /* 2: sanity check and find the object to be migrated. */
1480         fid_zero(old_fid);
1481         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1482         if (rc != 0)
1483                 GOTO(out_unlock_parent, rc);
1484
1485         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1486                 GOTO(out_unlock_parent, rc = -EINVAL);
1487
1488         if (!fid_is_md_operative(old_fid))
1489                 GOTO(out_unlock_parent, rc = -EPERM);
1490
1491         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1492         if (IS_ERR(mold))
1493                 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1494
1495         if (mdt_object_remote(mold)) {
1496                 CERROR("%s: source "DFID" is on the remote MDT\n",
1497                        mdt_obd_name(info->mti_mdt), PFID(old_fid));
1498                 GOTO(out_put_child, rc = -EREMOTE);
1499         }
1500
1501         if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1502             !mdt_object_remote(msrcdir)) {
1503                 CERROR("%s: parent "DFID" is still on the same"
1504                        " MDT, which should be migrated first:"
1505                        " rc = %d\n", mdt_obd_name(info->mti_mdt),
1506                        PFID(mdt_object_fid(msrcdir)), -EPERM);
1507                 GOTO(out_put_child, rc = -EPERM);
1508         }
1509
1510         rc = mdt_remote_permission(info, msrcdir, mold);
1511         if (rc != 0)
1512                 GOTO(out_put_child, rc);
1513
1514         /* 3: iterate the linkea of the object and lock all of the objects */
1515         INIT_LIST_HEAD(&lock_list);
1516         rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1517         if (rc != 0)
1518                 GOTO(out_put_child, rc);
1519
1520         /* 4: lock of the object migrated object */
1521         lh_childp = &info->mti_lh[MDT_LH_OLD];
1522         mdt_lock_reg_init(lh_childp, LCK_EX);
1523         rc = mdt_object_lock(info, mold, lh_childp,
1524                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1525                              MDS_INODELOCK_LAYOUT, MDT_CROSS_LOCK);
1526         if (rc != 0)
1527                 GOTO(out_unlock_list, rc);
1528
1529         ma->ma_need = MA_LMV;
1530         ma->ma_valid = 0;
1531         ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1532         ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1533         rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1534         if (rc != 0)
1535                 GOTO(out_unlock_list, rc);
1536
1537         if ((ma->ma_valid & MA_LMV)) {
1538                 struct lmv_mds_md_v1 *lmm1;
1539
1540                 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1541                 lmm1 = &ma->ma_lmv->lmv_md_v1;
1542                 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1543                         CERROR("%s: can not migrate striped dir "DFID
1544                                ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1545                                PFID(mdt_object_fid(mold)), -EPERM);
1546                         GOTO(out_unlock_child, rc = -EPERM);
1547                 }
1548
1549                 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1550                         GOTO(out_unlock_child, rc = -EINVAL);
1551
1552                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1553                                        &lmm1->lmv_stripe_fids[1]);
1554                 if (IS_ERR(mnew))
1555                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1556
1557                 if (!mdt_object_remote(mnew)) {
1558                         CERROR("%s: "DFID" being migrated is on this MDT:"
1559                                " rc  = %d\n", mdt_obd_name(info->mti_mdt),
1560                                PFID(rr->rr_fid2), -EPERM);
1561                         GOTO(out_put_new, rc = -EPERM);
1562                 }
1563
1564                 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1565                 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1566                 rc = mdt_remote_object_lock(info, mnew,
1567                                             mdt_object_fid(mnew),
1568                                             &lh_tgtp->mlh_rreg_lh,
1569                                             lh_tgtp->mlh_rreg_mode,
1570                                             MDS_INODELOCK_UPDATE);
1571                 if (rc != 0) {
1572                         lh_tgtp = NULL;
1573                         GOTO(out_put_new, rc);
1574                 }
1575         } else {
1576                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1577                                        rr->rr_fid2);
1578                 if (IS_ERR(mnew))
1579                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1580                 if (!mdt_object_remote(mnew)) {
1581                         CERROR("%s: Migration "DFID" is on this MDT:"
1582                                " rc = %d\n", mdt_obd_name(info->mti_mdt),
1583                                PFID(rr->rr_fid2), -EXDEV);
1584                         GOTO(out_put_new, rc = -EXDEV);
1585                 }
1586         }
1587
1588         /* 5: migrate it */
1589         mdt_reint_init_ma(info, ma);
1590
1591         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1592                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1593
1594         rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1595                          mdt_object_child(mold), &rr->rr_name,
1596                          mdt_object_child(mnew), ma);
1597         if (rc != 0)
1598                 GOTO(out_unlock_new, rc);
1599 out_unlock_new:
1600         if (lh_tgtp != NULL)
1601                 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1602 out_put_new:
1603         if (mnew)
1604                 mdt_object_put(info->mti_env, mnew);
1605 out_unlock_child:
1606         mdt_object_unlock(info, mold, lh_childp, rc);
1607 out_unlock_list:
1608         mdt_unlock_list(info, &lock_list, rc);
1609 out_put_child:
1610         mdt_object_put(info->mti_env, mold);
1611 out_unlock_parent:
1612         mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1613 out_put_parent:
1614         mdt_object_put(info->mti_env, msrcdir);
1615
1616         RETURN(rc);
1617 }
1618
1619 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1620                                                 const struct lu_fid *fid,
1621                                                 int idx)
1622 {
1623         struct mdt_object *dir;
1624         int rc;
1625         ENTRY;
1626
1627         dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1628         if (IS_ERR(dir))
1629                 RETURN(dir);
1630
1631         /* check early, the real version will be saved after locking */
1632         rc = mdt_version_get_check(info, dir, idx);
1633         if (rc)
1634                 GOTO(out_put, rc);
1635
1636         RETURN(dir);
1637 out_put:
1638         mdt_object_put(info->mti_env, dir);
1639         return ERR_PTR(rc);
1640 }
1641
1642 static int mdt_object_lock_save(struct mdt_thread_info *info,
1643                                 struct mdt_object *dir,
1644                                 struct mdt_lock_handle *lh,
1645                                 int idx)
1646 {
1647         int rc;
1648
1649         /* we lock the target dir if it is local */
1650         rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
1651                              MDT_LOCAL_LOCK);
1652         if (rc != 0)
1653                 return rc;
1654
1655         /* get and save correct version after locking */
1656         mdt_version_get_save(info, dir, idx);
1657         return 0;
1658 }
1659
1660
1661 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1662                                    struct mdt_object **srcp,
1663                                    struct mdt_object **tgtp)
1664 {
1665         struct mdt_reint_record *rr = &info->mti_rr;
1666         const struct lu_fid     *fid_src = rr->rr_fid1;
1667         const struct lu_fid     *fid_tgt = rr->rr_fid2;
1668         struct mdt_lock_handle  *lh_src = &info->mti_lh[MDT_LH_PARENT];
1669         struct mdt_lock_handle  *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1670         struct mdt_object       *src;
1671         struct mdt_object       *tgt;
1672         int                      reverse = 0;
1673         int                      rc;
1674         ENTRY;
1675
1676         /* find both parents. */
1677         src = mdt_object_find_check(info, fid_src, 0);
1678         if (IS_ERR(src))
1679                 RETURN(PTR_ERR(src));
1680
1681         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1682
1683         if (lu_fid_eq(fid_src, fid_tgt)) {
1684                 tgt = src;
1685                 mdt_object_get(info->mti_env, tgt);
1686         } else {
1687                 /* Check if the @src is not a child of the @tgt, otherwise a
1688                  * reverse locking must take place. */
1689                 rc = mdt_is_subdir(info, src, fid_tgt);
1690                 if (rc == -EINVAL)
1691                         reverse = 1;
1692                 else if (rc)
1693                         GOTO(err_src_put, rc);
1694
1695                 tgt = mdt_object_find_check(info, fid_tgt, 1);
1696                 if (IS_ERR(tgt))
1697                         GOTO(err_src_put, rc = PTR_ERR(tgt));
1698
1699                 if (unlikely(mdt_object_remote(tgt))) {
1700                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1701                                "on different MDTs\n", PFID(fid_src),
1702                                PFID(fid_tgt));
1703                         GOTO(err_tgt_put, rc = -EXDEV);
1704                 }
1705         }
1706
1707         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1708
1709         /* lock parents in the proper order. */
1710         if (reverse) {
1711                 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1712                 if (rc)
1713                         GOTO(err_tgt_put, rc);
1714
1715                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1716
1717                 rc = mdt_object_lock_save(info, src, lh_src, 0);
1718         } else {
1719                 rc = mdt_object_lock_save(info, src, lh_src, 0);
1720                 if (rc)
1721                         GOTO(err_tgt_put, rc);
1722
1723                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1724
1725                 if (tgt != src)
1726                         rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1727                 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1728                         rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1729                                                 MDS_INODELOCK_UPDATE);
1730                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1731                 }
1732         }
1733         if (rc)
1734                 GOTO(err_unlock, rc);
1735
1736         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1737
1738         *srcp = src;
1739         *tgtp = tgt;
1740         RETURN(0);
1741
1742 err_unlock:
1743         /* The order does not matter as the handle is checked inside,
1744          * as well as not used handle. */
1745         mdt_object_unlock(info, src, lh_src, rc);
1746         mdt_object_unlock(info, tgt, lh_tgt, rc);
1747 err_tgt_put:
1748         mdt_object_put(info->mti_env, tgt);
1749 err_src_put:
1750         mdt_object_put(info->mti_env, src);
1751         RETURN(rc);
1752 }
1753
1754 /*
1755  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1756  * 2 - src child; 3 - tgt child.
1757  * Update on disk version of src child.
1758  */
1759 /**
1760  * For DNE phase I, only these renames are allowed
1761  *      mv src_p/src_c tgt_p/tgt_c
1762  * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1763  * 2. src_p and tgt_p are same directory, and tgt_c does not
1764  *    exists. In this case, all of modification will happen
1765  *    in the MDT where ithesource parent is, only one remote
1766  *    update is needed, i.e. set c_time/m_time on the child.
1767  *    And tgt_c will be still in the same MDT as the original
1768  *    src_c.
1769  */
1770 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1771                                      struct mdt_lock_handle *lhc)
1772 {
1773         struct mdt_reint_record *rr = &info->mti_rr;
1774         struct md_attr          *ma = &info->mti_attr;
1775         struct ptlrpc_request   *req = mdt_info_req(info);
1776         struct mdt_object       *msrcdir = NULL;
1777         struct mdt_object       *mtgtdir = NULL;
1778         struct mdt_object       *mold;
1779         struct mdt_object       *mnew = NULL;
1780         struct mdt_lock_handle  *lh_srcdirp;
1781         struct mdt_lock_handle  *lh_tgtdirp;
1782         struct mdt_lock_handle  *lh_oldp = NULL;
1783         struct mdt_lock_handle  *lh_newp = NULL;
1784         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1785         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1786         int                      rc;
1787         ENTRY;
1788
1789         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1790                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1791                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1792
1793         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1794         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1795         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1796         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1797
1798         /* step 1&2: lock the source and target dirs. */
1799         rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1800         if (rc)
1801                 RETURN(rc);
1802
1803         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1804
1805         /* step 3: find & lock the old object. */
1806         fid_zero(old_fid);
1807         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1808         if (rc != 0)
1809                 GOTO(out_unlock_parents, rc);
1810
1811         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1812                 GOTO(out_unlock_parents, rc = -EINVAL);
1813
1814         if (!fid_is_md_operative(old_fid))
1815                 GOTO(out_unlock_parents, rc = -EPERM);
1816
1817         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1818         if (IS_ERR(mold))
1819                 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1820
1821         /* Check if @mtgtdir is subdir of @mold, before locking child
1822          * to avoid reverse locking. */
1823         rc = mdt_is_subdir(info, mtgtdir, old_fid);
1824         if (rc)
1825                 GOTO(out_put_old, rc);
1826
1827         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1828         /* save version after locking */
1829         mdt_version_get_save(info, mold, 2);
1830         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1831
1832         /* step 4: find & lock the new object. */
1833         /* new target object may not exist now */
1834         /* lookup with version checking */
1835         fid_zero(new_fid);
1836         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1837                                       3);
1838         if (rc == 0) {
1839                 /* the new_fid should have been filled at this moment */
1840                 if (lu_fid_eq(old_fid, new_fid))
1841                         GOTO(out_put_old, rc);
1842
1843                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1844                     lu_fid_eq(new_fid, rr->rr_fid2))
1845                         GOTO(out_put_old, rc = -EINVAL);
1846
1847                 if (!fid_is_md_operative(new_fid))
1848                         GOTO(out_put_old, rc = -EPERM);
1849
1850                 if (mdt_object_remote(mold)) {
1851                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1852                                PFID(old_fid));
1853                         GOTO(out_put_old, rc = -EXDEV);
1854                 }
1855
1856                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1857                 if (IS_ERR(mnew))
1858                         GOTO(out_put_old, rc = PTR_ERR(mnew));
1859
1860                 if (mdt_object_remote(mnew)) {
1861                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1862                                PFID(new_fid));
1863                         GOTO(out_put_new, rc = -EXDEV);
1864                 }
1865
1866                 /* Before locking the target dir, check we do not replace
1867                  * a dir with a non-dir, otherwise it may deadlock with
1868                  * link op which tries to create a link in this dir
1869                  * back to this non-dir. */
1870                 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1871                     !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1872                         GOTO(out_put_new, rc = -EISDIR);
1873
1874                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1875                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1876                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1877                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1878                 if (rc != 0)
1879                         GOTO(out_put_new, rc);
1880
1881                 /* Check if @msrcdir is subdir of @mnew, before locking child
1882                  * to avoid reverse locking. */
1883                 rc = mdt_is_subdir(info, msrcdir, new_fid);
1884                 if (rc)
1885                         GOTO(out_unlock_old, rc);
1886
1887                 /* We used to acquire MDS_INODELOCK_FULL here but we
1888                  * can't do this now because a running HSM restore on
1889                  * the rename onto victim will hold the layout
1890                  * lock. See LU-4002. */
1891
1892                 lh_newp = &info->mti_lh[MDT_LH_NEW];
1893                 mdt_lock_reg_init(lh_newp, LCK_EX);
1894                 rc = mdt_object_lock(info, mnew, lh_newp,
1895                                      MDS_INODELOCK_LOOKUP |
1896                                      MDS_INODELOCK_UPDATE,
1897                                      MDT_LOCAL_LOCK);
1898                 if (rc != 0)
1899                         GOTO(out_unlock_old, rc);
1900
1901                 /* get and save version after locking */
1902                 mdt_version_get_save(info, mnew, 3);
1903                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1904         } else if (rc != -EREMOTE && rc != -ENOENT) {
1905                 GOTO(out_put_old, rc);
1906         } else {
1907                 /* If mnew does not exist and mold are remote directory,
1908                  * it only allows rename if they are under same directory */
1909                 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1910                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1911                                PFID(old_fid));
1912                         GOTO(out_put_old, rc = -EXDEV);
1913                 }
1914
1915                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1916                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1917                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1918                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1919                 if (rc != 0)
1920                         GOTO(out_put_old, rc);
1921
1922                 mdt_enoent_version_save(info, 3);
1923         }
1924
1925         /* step 5: rename it */
1926         mdt_reint_init_ma(info, ma);
1927
1928         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1929                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1930
1931         if (mnew != NULL)
1932                 mutex_lock(&mnew->mot_lov_mutex);
1933
1934         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1935                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1936                         mnew != NULL ? mdt_object_child(mnew) : NULL,
1937                         &rr->rr_tgt_name, ma);
1938
1939         if (mnew != NULL)
1940                 mutex_unlock(&mnew->mot_lov_mutex);
1941
1942         /* handle last link of tgt object */
1943         if (rc == 0) {
1944                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1945                 if (mnew)
1946                         mdt_handle_last_unlink(info, mnew, ma);
1947
1948                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1949                                          msrcdir, mtgtdir);
1950         }
1951
1952         EXIT;
1953         if (mnew != NULL)
1954                 mdt_object_unlock(info, mnew, lh_newp, rc);
1955 out_unlock_old:
1956         mdt_object_unlock(info, mold, lh_oldp, rc);
1957 out_put_new:
1958         if (mnew != NULL)
1959                 mdt_object_put(info->mti_env, mnew);
1960 out_put_old:
1961         mdt_object_put(info->mti_env, mold);
1962 out_unlock_parents:
1963         mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1964         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1965         return rc;
1966 }
1967
1968 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1969                                        struct mdt_lock_handle *lhc,
1970                                        enum mdt_rename_lock rename_lock)
1971 {
1972         struct mdt_reint_record *rr = &info->mti_rr;
1973         struct ptlrpc_request   *req = mdt_info_req(info);
1974         struct lustre_handle    rename_lh = { 0 };
1975         int                     rc;
1976         ENTRY;
1977
1978         if (info->mti_dlm_req)
1979                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1980
1981         if (!fid_is_md_operative(rr->rr_fid1) ||
1982             !fid_is_md_operative(rr->rr_fid2))
1983                 RETURN(-EPERM);
1984
1985         rc = mdt_rename_lock(info, &rename_lh, rename_lock);
1986         if (rc != 0) {
1987                 CERROR("%s: can't lock FS for rename: rc  = %d\n",
1988                        mdt_obd_name(info->mti_mdt), rc);
1989                 RETURN(rc);
1990         }
1991
1992         if (rename_lock == MRL_RENAME)
1993                 rc = mdt_reint_rename_internal(info, lhc);
1994         else
1995                 rc = mdt_reint_migrate_internal(info, lhc);
1996
1997         if (lustre_handle_is_used(&rename_lh))
1998                 mdt_rename_unlock(&rename_lh);
1999
2000         RETURN(rc);
2001 }
2002
2003 static int mdt_reint_rename(struct mdt_thread_info *info,
2004                             struct mdt_lock_handle *lhc)
2005 {
2006         return mdt_reint_rename_or_migrate(info, lhc, MRL_RENAME);
2007 }
2008
2009 static int mdt_reint_migrate(struct mdt_thread_info *info,
2010                             struct mdt_lock_handle *lhc)
2011 {
2012         return mdt_reint_rename_or_migrate(info, lhc, MRL_MIGRATE);
2013 }
2014
2015 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
2016                            struct mdt_lock_handle *lhc);
2017
2018 static mdt_reinter reinters[REINT_MAX] = {
2019         [REINT_SETATTR]  = mdt_reint_setattr,
2020         [REINT_CREATE]   = mdt_reint_create,
2021         [REINT_LINK]     = mdt_reint_link,
2022         [REINT_UNLINK]   = mdt_reint_unlink,
2023         [REINT_RENAME]   = mdt_reint_rename,
2024         [REINT_OPEN]     = mdt_reint_open,
2025         [REINT_SETXATTR] = mdt_reint_setxattr,
2026         [REINT_RMENTRY]  = mdt_reint_unlink,
2027         [REINT_MIGRATE]   = mdt_reint_migrate,
2028 };
2029
2030 int mdt_reint_rec(struct mdt_thread_info *info,
2031                   struct mdt_lock_handle *lhc)
2032 {
2033         int rc;
2034         ENTRY;
2035
2036         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
2037
2038         RETURN(rc);
2039 }