Whamcloud - gitweb
LU-5453 mdt: mdt_reint_setattr() and mdt_attr_set() fixes
[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 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
539                         struct md_attr *ma)
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         lh = &info->mti_lh[MDT_LH_PARENT];
551         mdt_lock_reg_init(lh, LCK_PW);
552
553         /* Even though the new MDT will grant PERM lock to the old
554          * client, but the old client will almost ignore that during
555          * So it needs to revoke both LOOKUP and PERM lock here, so
556          * both new and old client can cancel the dcache */
557         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
558                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
559
560         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
561         if (rc != 0)
562                 RETURN(rc);
563
564         s0_lh = &info->mti_lh[MDT_LH_LOCAL];
565         mdt_lock_reg_init(s0_lh, LCK_PW);
566         rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
567         if (rc != 0)
568                 GOTO(out_unlock, rc);
569
570         /* all attrs are packed into mti_attr in unpack_setattr */
571         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
572                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
573
574         /* This is only for set ctime when rename's source is on remote MDS. */
575         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
576                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
577
578         /* VBR: update version if attr changed are important for recovery */
579         if (do_vbr) {
580                 /* update on-disk version of changed object */
581                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
582                 rc = mdt_version_get_check_save(info, mo, 0);
583                 if (rc)
584                         GOTO(out_unlock, rc);
585         }
586
587         /* Ensure constant striping during chown(). See LU-2789. */
588         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
589                 mutex_lock(&mo->mot_lov_mutex);
590
591         /* all attrs are packed into mti_attr in unpack_setattr */
592         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
593
594         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
595                 mutex_unlock(&mo->mot_lov_mutex);
596
597         if (rc != 0)
598                 GOTO(out_unlock, rc);
599
600         EXIT;
601 out_unlock:
602         mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
603         mdt_object_unlock(info, mo, lh, rc);
604         return rc;
605 }
606
607 /**
608  * Check HSM flags and add HS_DIRTY flag if relevant.
609  *
610  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
611  * and is not RELEASED.
612  */
613 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
614                         struct md_attr *ma)
615 {
616         int rc;
617         ENTRY;
618
619         /* If the file was modified, add the dirty flag */
620         ma->ma_need = MA_HSM;
621         rc = mdt_attr_get_complex(info, mo, ma);
622         if (rc) {
623                 CERROR("file attribute read error for "DFID": %d.\n",
624                         PFID(mdt_object_fid(mo)), rc);
625                 RETURN(rc);
626         }
627
628         /* If an up2date copy exists in the backend, add dirty flag */
629         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
630             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
631                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
632
633                 ma->ma_hsm.mh_flags |= HS_DIRTY;
634
635                 mdt_lock_reg_init(lh, LCK_PW);
636                 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
637                                      MDT_LOCAL_LOCK);
638                 if (rc != 0)
639                         RETURN(rc);
640
641                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
642                 if (rc)
643                         CERROR("file attribute change error for "DFID": %d\n",
644                                 PFID(mdt_object_fid(mo)), rc);
645                 mdt_object_unlock(info, mo, lh, rc);
646         }
647
648         RETURN(rc);
649 }
650
651 static int mdt_reint_setattr(struct mdt_thread_info *info,
652                              struct mdt_lock_handle *lhc)
653 {
654         struct md_attr          *ma = &info->mti_attr;
655         struct mdt_reint_record *rr = &info->mti_rr;
656         struct ptlrpc_request   *req = mdt_info_req(info);
657         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
658         struct mdt_file_data    *mfd;
659         struct mdt_object       *mo;
660         struct mdt_body         *repbody;
661         int                      som_au, rc, rc2;
662         ENTRY;
663
664         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
665                   (unsigned int)ma->ma_attr.la_valid);
666
667         if (info->mti_dlm_req)
668                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
669
670         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
671         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
672         if (IS_ERR(mo))
673                 GOTO(out, rc = PTR_ERR(mo));
674
675         if (!mdt_object_exists(mo))
676                 GOTO(out_put, rc = -ENOENT);
677
678         if (mdt_object_remote(mo))
679                 GOTO(out_put, rc = -EREMOTE);
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                 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM))
721                         GOTO(out_put, rc = -EPROTO);
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
735                 if (mfd->mfd_mode != MDS_FMODE_SOM ||
736                     (info->mti_ioepoch->flags & MF_EPOCH_CLOSE))
737                         GOTO(out_put, rc = -EPROTO);
738
739                 class_handle_unhash(&mfd->mfd_handle);
740                 list_del_init(&mfd->mfd_list);
741                 spin_unlock(&med->med_open_lock);
742
743                 mdt_mfd_close(info, mfd);
744         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
745                 if (ma->ma_valid & MA_LOV)
746                         GOTO(out_put, rc = -EPROTO);
747
748                 rc = mdt_attr_set(info, mo, ma);
749                 if (rc)
750                         GOTO(out_put, rc);
751         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
752                 struct lu_buf *buf  = &info->mti_buf;
753
754                 if (ma->ma_attr.la_valid != 0)
755                         GOTO(out_put, rc = -EPROTO);
756
757                 buf->lb_buf = ma->ma_lmm;
758                 buf->lb_len = ma->ma_lmm_size;
759                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
760                                   buf, XATTR_NAME_LOV, 0);
761                 if (rc)
762                         GOTO(out_put, rc);
763         } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
764                 struct lu_buf *buf  = &info->mti_buf;
765
766                 if (ma->ma_attr.la_valid != 0)
767                         GOTO(out_put, rc = -EPROTO);
768
769                 buf->lb_buf = ma->ma_lmv;
770                 buf->lb_len = ma->ma_lmv_size;
771                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
772                                   buf, XATTR_NAME_DEFAULT_LMV, 0);
773                 if (rc)
774                         GOTO(out_put, rc);
775         } else {
776                 GOTO(out_put, rc = -EPROTO);
777         }
778
779         /* If file data is modified, add the dirty flag */
780         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
781                 rc = mdt_add_dirty_flag(info, mo, ma);
782
783         ma->ma_need = MA_INODE;
784         ma->ma_valid = 0;
785         rc = mdt_attr_get_complex(info, mo, ma);
786         if (rc != 0)
787                 GOTO(out_put, rc);
788
789         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
790
791         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
792             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
793             S_ISREG(lu_object_attr(&mo->mot_obj)) &&
794             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
795                 struct lustre_capa *capa;
796
797                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
798                 LASSERT(capa);
799                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
800                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
801                 if (rc)
802                         GOTO(out_put, rc);
803                 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
804         }
805
806         EXIT;
807 out_put:
808         mdt_object_put(info->mti_env, mo);
809 out:
810         if (rc == 0)
811                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
812
813         mdt_client_compatibility(info);
814         rc2 = mdt_fix_reply(info);
815         if (rc == 0)
816                 rc = rc2;
817         return rc;
818 }
819
820 static int mdt_reint_create(struct mdt_thread_info *info,
821                             struct mdt_lock_handle *lhc)
822 {
823         struct ptlrpc_request   *req = mdt_info_req(info);
824         int                     rc;
825         ENTRY;
826
827         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
828                 RETURN(err_serious(-ESTALE));
829
830         if (info->mti_dlm_req)
831                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
832
833         if (!lu_name_is_valid(&info->mti_rr.rr_name))
834                 RETURN(-EPROTO);
835
836         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
837         case S_IFDIR:
838                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
839                 break;
840         case S_IFREG:
841         case S_IFLNK:
842         case S_IFCHR:
843         case S_IFBLK:
844         case S_IFIFO:
845         case S_IFSOCK:
846                 /* Special file should stay on the same node as parent. */
847                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
848                 break;
849         default:
850                 CERROR("%s: Unsupported mode %o\n",
851                        mdt_obd_name(info->mti_mdt),
852                        info->mti_attr.ma_attr.la_mode);
853                 RETURN(err_serious(-EOPNOTSUPP));
854         }
855
856         rc = mdt_md_create(info);
857         RETURN(rc);
858 }
859
860 /*
861  * VBR: save parent version in reply and child version getting by its name.
862  * Version of child is getting and checking during its lookup. If
863  */
864 static int mdt_reint_unlink(struct mdt_thread_info *info,
865                             struct mdt_lock_handle *lhc)
866 {
867         struct mdt_reint_record *rr = &info->mti_rr;
868         struct ptlrpc_request   *req = mdt_info_req(info);
869         struct md_attr          *ma = &info->mti_attr;
870         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
871         struct mdt_object       *mp;
872         struct mdt_object       *mc;
873         struct mdt_lock_handle  *parent_lh;
874         struct mdt_lock_handle  *child_lh;
875         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
876         struct mdt_lock_handle  *s0_lh = NULL;
877         struct mdt_object       *s0_obj = NULL;
878         int                     rc;
879         int                     no_name = 0;
880         ENTRY;
881
882         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
883                   PNAME(&rr->rr_name));
884
885         if (info->mti_dlm_req)
886                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
887
888         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
889                 RETURN(err_serious(-ENOENT));
890
891         if (!fid_is_md_operative(rr->rr_fid1))
892                 RETURN(-EPERM);
893
894         /*
895          * step 1: Found the parent.
896          */
897         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
898         if (IS_ERR(mp)) {
899                 rc = PTR_ERR(mp);
900                 GOTO(out, rc);
901         }
902
903         parent_lh = &info->mti_lh[MDT_LH_PARENT];
904         mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
905         rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
906                              MDT_CROSS_LOCK);
907         if (rc != 0)
908                 GOTO(put_parent, rc);
909
910         if (!mdt_object_remote(mp)) {
911                 rc = mdt_version_get_check_save(info, mp, 0);
912                 if (rc)
913                         GOTO(unlock_parent, rc);
914         }
915
916         /* step 2: find & lock the child */
917         /* lookup child object along with version checking */
918         fid_zero(child_fid);
919         rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
920         if (rc != 0) {
921                 /* Name might not be able to find during resend of
922                  * remote unlink, considering following case.
923                  * dir_A is a remote directory, the name entry of
924                  * dir_A is on MDT0, the directory is on MDT1,
925                  *
926                  * 1. client sends unlink req to MDT1.
927                  * 2. MDT1 sends name delete update to MDT0.
928                  * 3. name entry is being deleted in MDT0 synchronously.
929                  * 4. MDT1 is restarted.
930                  * 5. client resends unlink req to MDT1. So it can not
931                  *    find the name entry on MDT0 anymore.
932                  * In this case, MDT1 only needs to destory the local
933                  * directory.
934                  * */
935                 if (mdt_object_remote(mp) && rc == -ENOENT &&
936                     !fid_is_zero(rr->rr_fid2) &&
937                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
938                         no_name = 1;
939                         *child_fid = *rr->rr_fid2;
940                  } else {
941                         GOTO(unlock_parent, rc);
942                  }
943         }
944
945         if (!fid_is_md_operative(child_fid))
946                 GOTO(unlock_parent, rc = -EPERM);
947
948         /* We will lock the child regardless it is local or remote. No harm. */
949         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
950         if (IS_ERR(mc))
951                 GOTO(unlock_parent, rc = PTR_ERR(mc));
952
953         child_lh = &info->mti_lh[MDT_LH_CHILD];
954         mdt_lock_reg_init(child_lh, LCK_EX);
955         if (mdt_object_remote(mc)) {
956                 struct mdt_body  *repbody;
957
958                 if (!fid_is_zero(rr->rr_fid2)) {
959                         CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
960                                mdt_obd_name(info->mti_mdt),
961                                PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
962                         GOTO(put_child, rc = -ENOENT);
963                 }
964                 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
965                        mdt_obd_name(info->mti_mdt),
966                        PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
967
968                 if (!mdt_is_dne_client(req->rq_export))
969                         /* Return -ENOTSUPP for old client */
970                         GOTO(put_child, rc = -ENOTSUPP);
971
972                 if (info->mti_spec.sp_rm_entry) {
973                         struct lu_ucred *uc  = mdt_ucred(info);
974
975                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
976                                 CERROR("%s: unlink remote entry is only "
977                                        "permitted for administrator: rc = %d\n",
978                                         mdt_obd_name(info->mti_mdt),
979                                         -EPERM);
980                                 GOTO(put_child, rc = -EPERM);
981                         }
982
983                         ma->ma_need = MA_INODE;
984                         ma->ma_valid = 0;
985                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
986                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
987                                         NULL, &rr->rr_name, ma, no_name);
988                         GOTO(put_child, rc);
989                 }
990                 /* Revoke the LOOKUP lock of the remote object granted by
991                  * this MDT. Since the unlink will happen on another MDT,
992                  * it will release the LOOKUP lock right away. Then What
993                  * would happen if another client try to grab the LOOKUP
994                  * lock at the same time with unlink XXX */
995                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
996                                 MDT_CROSS_LOCK);
997                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
998                 LASSERT(repbody != NULL);
999                 repbody->mbo_fid1 = *mdt_object_fid(mc);
1000                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1001                 GOTO(unlock_child, rc = -EREMOTE);
1002         } else if (info->mti_spec.sp_rm_entry) {
1003                 rc = -EPERM;
1004                 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
1005                        "rc = %d\n",
1006                        mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
1007                 GOTO(put_child, rc);
1008         }
1009
1010         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1011          * this now because a running HSM restore on the child (unlink
1012          * victim) will hold the layout lock. See LU-4002. */
1013         rc = mdt_object_lock(info, mc, child_lh,
1014                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
1015                              MDT_CROSS_LOCK);
1016         if (rc != 0)
1017                 GOTO(put_child, rc);
1018         /*
1019          * Now we can only make sure we need MA_INODE, in mdd layer, will check
1020          * whether need MA_LOV and MA_COOKIE.
1021          */
1022         ma->ma_need = MA_INODE;
1023         ma->ma_valid = 0;
1024
1025         s0_lh = &info->mti_lh[MDT_LH_LOCAL];
1026         mdt_lock_reg_init(s0_lh, LCK_EX);
1027         rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
1028                              &s0_obj, einfo);
1029         if (rc != 0)
1030                 GOTO(unlock_child, rc);
1031
1032         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1033                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1034         /* save version when object is locked */
1035         mdt_version_get_save(info, mc, 1);
1036
1037         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1038
1039         mutex_lock(&mc->mot_lov_mutex);
1040
1041         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1042                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
1043
1044         mutex_unlock(&mc->mot_lov_mutex);
1045
1046         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
1047                 rc = mdt_attr_get_complex(info, mc, ma);
1048         if (rc == 0)
1049                 mdt_handle_last_unlink(info, mc, ma);
1050
1051         if (ma->ma_valid & MA_INODE) {
1052                 switch (ma->ma_attr.la_mode & S_IFMT) {
1053                 case S_IFDIR:
1054                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
1055                         break;
1056                 case S_IFREG:
1057                 case S_IFLNK:
1058                 case S_IFCHR:
1059                 case S_IFBLK:
1060                 case S_IFIFO:
1061                 case S_IFSOCK:
1062                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
1063                         break;
1064                 default:
1065                         LASSERTF(0, "bad file type %o unlinking\n",
1066                                  ma->ma_attr.la_mode);
1067                 }
1068         }
1069
1070         EXIT;
1071
1072 unlock_child:
1073         mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
1074         mdt_object_unlock(info, mc, child_lh, rc);
1075 put_child:
1076         mdt_object_put(info->mti_env, mc);
1077 unlock_parent:
1078         mdt_object_unlock(info, mp, parent_lh, rc);
1079 put_parent:
1080         mdt_object_put(info->mti_env, mp);
1081 out:
1082         return rc;
1083 }
1084
1085 /*
1086  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1087  * name.
1088  */
1089 static int mdt_reint_link(struct mdt_thread_info *info,
1090                           struct mdt_lock_handle *lhc)
1091 {
1092         struct mdt_reint_record *rr = &info->mti_rr;
1093         struct ptlrpc_request   *req = mdt_info_req(info);
1094         struct md_attr          *ma = &info->mti_attr;
1095         struct mdt_object       *ms;
1096         struct mdt_object       *mp;
1097         struct mdt_lock_handle  *lhs;
1098         struct mdt_lock_handle  *lhp;
1099         int rc;
1100         ENTRY;
1101
1102         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1103                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1104
1105         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1106                 RETURN(err_serious(-ENOENT));
1107
1108         if (info->mti_dlm_req)
1109                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1110
1111         /* Invalid case so return error immediately instead of
1112          * processing it */
1113         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1114                 RETURN(-EPERM);
1115
1116         if (!fid_is_md_operative(rr->rr_fid1) ||
1117             !fid_is_md_operative(rr->rr_fid2))
1118                 RETURN(-EPERM);
1119
1120         /* step 1: find & lock the target parent dir */
1121         lhp = &info->mti_lh[MDT_LH_PARENT];
1122         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1123         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1124                                   MDS_INODELOCK_UPDATE);
1125         if (IS_ERR(mp))
1126                 RETURN(PTR_ERR(mp));
1127
1128         rc = mdt_version_get_check_save(info, mp, 0);
1129         if (rc)
1130                 GOTO(out_unlock_parent, rc);
1131
1132         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1133
1134         /* step 2: find & lock the source */
1135         lhs = &info->mti_lh[MDT_LH_CHILD];
1136         mdt_lock_reg_init(lhs, LCK_EX);
1137
1138         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1139         if (IS_ERR(ms))
1140                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1141
1142         if (!mdt_object_exists(ms)) {
1143                 mdt_object_put(info->mti_env, ms);
1144                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1145                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1146                 GOTO(out_unlock_parent, rc = -ENOENT);
1147         }
1148
1149         if (mdt_object_remote(ms)) {
1150                 mdt_object_put(info->mti_env, ms);
1151                 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1152                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1153                        PFID(rr->rr_fid2));
1154                 GOTO(out_unlock_parent, rc = -EXDEV);
1155         }
1156
1157         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1158                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1159         if (rc != 0) {
1160                 mdt_object_put(info->mti_env, ms);
1161                 GOTO(out_unlock_parent, rc);
1162         }
1163
1164         /* step 3: link it */
1165         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1166                        OBD_FAIL_MDS_REINT_LINK_WRITE);
1167
1168         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1169         rc = mdt_version_get_check_save(info, ms, 1);
1170         if (rc)
1171                 GOTO(out_unlock_child, rc);
1172
1173         /** check target version by name during replay */
1174         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1175                                       &info->mti_tmp_fid1, 2);
1176         if (rc != 0 && rc != -ENOENT)
1177                 GOTO(out_unlock_child, rc);
1178         /* save version of file name for replay, it must be ENOENT here */
1179         if (!req_is_replay(mdt_info_req(info))) {
1180                 if (rc != -ENOENT) {
1181                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1182                                PNAME(&rr->rr_name));
1183                         GOTO(out_unlock_child, rc = -EEXIST);
1184                 }
1185                 info->mti_ver[2] = ENOENT_VERSION;
1186                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1187         }
1188
1189         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1190               mdt_object_child(ms), &rr->rr_name, ma);
1191
1192         if (rc == 0)
1193                 mdt_counter_incr(req, LPROC_MDT_LINK);
1194
1195         EXIT;
1196 out_unlock_child:
1197         mdt_object_unlock_put(info, ms, lhs, rc);
1198 out_unlock_parent:
1199         mdt_object_unlock_put(info, mp, lhp, rc);
1200         return rc;
1201 }
1202 /**
1203  * lock the part of the directory according to the hash of the name
1204  * (lh->mlh_pdo_hash) in parallel directory lock.
1205  */
1206 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1207                               struct mdt_lock_handle *lh,
1208                               struct mdt_object *obj, __u64 ibits)
1209 {
1210         struct ldlm_res_id *res = &info->mti_res_id;
1211         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1212         ldlm_policy_data_t *policy = &info->mti_policy;
1213         int rc;
1214
1215         /*
1216          * Finish res_id initializing by name hash marking part of
1217          * directory which is taking modification.
1218          */
1219         LASSERT(lh->mlh_pdo_hash != 0);
1220         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1221         memset(policy, 0, sizeof(*policy));
1222         policy->l_inodebits.bits = ibits;
1223         /*
1224          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1225          * going to be sent to client. If it is - mdt_intent_policy() path will
1226          * fix it up and turn FL_LOCAL flag off.
1227          */
1228         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1229                           res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1230                           &info->mti_exp->exp_handle.h_cookie);
1231         return rc;
1232 }
1233
1234 enum mdt_rename_lock {
1235         MRL_RENAME,
1236         MRL_MIGRATE,
1237 };
1238
1239 /**
1240  * Get BFL lock for rename or migrate process, right now, it does not support
1241  * cross-MDT rename, so we only need global rename lock during migration.
1242  **/
1243 static int mdt_rename_lock(struct mdt_thread_info *info,
1244                            struct lustre_handle *lh,
1245                            enum mdt_rename_lock rename_lock)
1246 {
1247         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
1248         ldlm_policy_data_t      *policy = &info->mti_policy;
1249         struct ldlm_res_id      *res_id = &info->mti_res_id;
1250         __u64                   flags = 0;
1251         int                     rc;
1252         ENTRY;
1253
1254         /* XXX only do global rename lock for migration */
1255         if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0 &&
1256             rename_lock == MRL_MIGRATE) {
1257                 struct lu_fid *fid = &info->mti_tmp_fid1;
1258                 struct mdt_object *obj;
1259
1260                 /* XXX, right now, it has to use object API to
1261                  * enqueue lock cross MDT, so it will enqueue
1262                  * rename lock(with LUSTRE_BFL_FID) by root object */
1263                 lu_root_fid(fid);
1264                 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1265                 if (IS_ERR(obj))
1266                         RETURN(PTR_ERR(obj));
1267
1268                 LASSERT(mdt_object_remote(obj));
1269                 rc = mdt_remote_object_lock(info, obj,
1270                                             &LUSTRE_BFL_FID, lh,
1271                                             LCK_EX,
1272                                             MDS_INODELOCK_UPDATE);
1273                 mdt_object_put(info->mti_env, obj);
1274         } else {
1275                 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1276                 memset(policy, 0, sizeof *policy);
1277                 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1278                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1279                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1280                                             LCK_EX, &flags, ldlm_blocking_ast,
1281                                             ldlm_completion_ast, NULL, NULL, 0,
1282                                             LVB_T_NONE,
1283                                             &info->mti_exp->exp_handle.h_cookie,
1284                                             lh);
1285         }
1286
1287         RETURN(rc);
1288 }
1289
1290 static void mdt_rename_unlock(struct lustre_handle *lh)
1291 {
1292         ENTRY;
1293         LASSERT(lustre_handle_is_used(lh));
1294         /* Cancel the single rename lock right away */
1295         ldlm_lock_decref_and_cancel(lh, LCK_EX);
1296         EXIT;
1297 }
1298
1299 /*
1300  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1301  * target. Source should not be ancestor of target dir. May be other rename
1302  * checks can be moved here later.
1303  */
1304 static int mdt_is_subdir(struct mdt_thread_info *info,
1305                          struct mdt_object *dir,
1306                          const struct lu_fid *fid)
1307 {
1308         struct lu_fid dir_fid = dir->mot_header.loh_fid;
1309         int rc = 0;
1310         ENTRY;
1311
1312         /* If the source and target are in the same directory, they can not
1313          * be parent/child relationship, so subdir check is not needed */
1314         if (lu_fid_eq(&dir_fid, fid))
1315                 return 0;
1316
1317         if (!mdt_object_exists(dir))
1318                 RETURN(-ENOENT);
1319
1320         rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1321                            fid, &dir_fid);
1322         if (rc < 0) {
1323                 CERROR("%s: failed subdir check in "DFID" for "DFID
1324                        ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1325                        PFID(&dir_fid), PFID(fid), rc);
1326                 /* Return EINVAL only if a parent is the @fid */
1327                 if (rc == -EINVAL)
1328                         rc = -EIO;
1329         } else {
1330                 /* check the found fid */
1331                 if (lu_fid_eq(&dir_fid, fid))
1332                         rc = -EINVAL;
1333         }
1334
1335         RETURN(rc);
1336 }
1337
1338 /* Update object linkEA */
1339 struct mdt_lock_list {
1340         struct mdt_object       *mll_obj;
1341         struct mdt_lock_handle  mll_lh;
1342         struct list_head        mll_list;
1343 };
1344
1345 static void mdt_unlock_list(struct mdt_thread_info *info,
1346                             struct list_head *list, int rc)
1347 {
1348         struct mdt_lock_list *mll;
1349         struct mdt_lock_list *mll2;
1350
1351         list_for_each_entry_safe(mll, mll2, list, mll_list) {
1352                 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1353                 list_del(&mll->mll_list);
1354                 OBD_FREE_PTR(mll);
1355         }
1356 }
1357
1358 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1359                                       struct mdt_object *obj,
1360                                       struct mdt_object *pobj,
1361                                       struct list_head *lock_list)
1362 {
1363         struct lu_buf           *buf = &info->mti_big_buf;
1364         struct linkea_data      ldata = { 0 };
1365         int                     count;
1366         int                     rc;
1367         ENTRY;
1368
1369         if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1370                 RETURN(0);
1371
1372         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1373         if (buf->lb_buf == NULL)
1374                 RETURN(-ENOMEM);
1375
1376         ldata.ld_buf = buf;
1377         rc = mdt_links_read(info, obj, &ldata);
1378         if (rc != 0) {
1379                 if (rc == -ENOENT || rc == -ENODATA)
1380                         rc = 0;
1381                 RETURN(rc);
1382         }
1383
1384         LASSERT(ldata.ld_leh != NULL);
1385         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1386         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1387                 struct mdt_device *mdt = info->mti_mdt;
1388                 struct mdt_object *mdt_pobj;
1389                 struct mdt_lock_list *mll;
1390                 struct lu_name name;
1391                 struct lu_fid  fid;
1392
1393                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1394                                     &name, &fid);
1395                 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1396                                                          ldata.ld_reclen);
1397                 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1398                 if (IS_ERR(mdt_pobj)) {
1399                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1400                               mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1401                         continue;
1402                 }
1403
1404                 if (!mdt_object_exists(mdt_pobj)) {
1405                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1406                               mdt_obd_name(mdt), PFID(&fid));
1407                         mdt_object_put(info->mti_env, mdt_pobj);
1408                         continue;
1409                 }
1410
1411                 if (mdt_pobj == pobj) {
1412                         CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1413                                mdt_obd_name(mdt), PFID(&fid));
1414                         mdt_object_put(info->mti_env, mdt_pobj);
1415                         continue;
1416                 }
1417
1418                 OBD_ALLOC_PTR(mll);
1419                 if (mll == NULL) {
1420                         mdt_object_put(info->mti_env, mdt_pobj);
1421                         GOTO(out, rc = -ENOMEM);
1422                 }
1423
1424                 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1425                 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1426                                      MDS_INODELOCK_UPDATE,
1427                                      MDT_CROSS_LOCK);
1428                 if (rc != 0) {
1429                         CERROR("%s: cannot lock "DFID": rc =%d\n",
1430                                mdt_obd_name(mdt), PFID(&fid), rc);
1431                         mdt_object_put(info->mti_env, mdt_pobj);
1432                         OBD_FREE_PTR(mll);
1433                         GOTO(out, rc);
1434                 }
1435
1436                 INIT_LIST_HEAD(&mll->mll_list);
1437                 mll->mll_obj = mdt_pobj;
1438                 list_add_tail(&mll->mll_list, lock_list);
1439         }
1440 out:
1441         if (rc != 0)
1442                 mdt_unlock_list(info, lock_list, rc);
1443         RETURN(rc);
1444 }
1445
1446 /* migrate files from one MDT to another MDT */
1447 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1448                                       struct mdt_lock_handle *lhc)
1449 {
1450         struct mdt_reint_record *rr = &info->mti_rr;
1451         struct md_attr          *ma = &info->mti_attr;
1452         struct mdt_object       *msrcdir;
1453         struct mdt_object       *mold;
1454         struct mdt_object       *mnew = NULL;
1455         struct mdt_lock_handle  *lh_dirp;
1456         struct mdt_lock_handle  *lh_childp;
1457         struct mdt_lock_handle  *lh_tgtp = NULL;
1458         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1459         struct list_head        lock_list;
1460         int                     rc;
1461         ENTRY;
1462
1463         CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1464                PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1465
1466         /* 1: lock the source dir. */
1467         msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1468         if (IS_ERR(msrcdir)) {
1469                 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1470                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1471                         (int)PTR_ERR(msrcdir));
1472                 RETURN(PTR_ERR(msrcdir));
1473         }
1474
1475         lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1476         mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1477         rc = mdt_object_lock(info, msrcdir, lh_dirp,
1478                              MDS_INODELOCK_UPDATE,
1479                              MDT_CROSS_LOCK);
1480         if (rc)
1481                 GOTO(out_put_parent, rc);
1482
1483         if (!mdt_object_remote(msrcdir)) {
1484                 rc = mdt_version_get_check_save(info, msrcdir, 0);
1485                 if (rc)
1486                         GOTO(out_unlock_parent, rc);
1487         }
1488
1489         /* 2: sanity check and find the object to be migrated. */
1490         fid_zero(old_fid);
1491         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1492         if (rc != 0)
1493                 GOTO(out_unlock_parent, rc);
1494
1495         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1496                 GOTO(out_unlock_parent, rc = -EINVAL);
1497
1498         if (!fid_is_md_operative(old_fid))
1499                 GOTO(out_unlock_parent, rc = -EPERM);
1500
1501         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1502         if (IS_ERR(mold))
1503                 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1504
1505         if (mdt_object_remote(mold)) {
1506                 CERROR("%s: source "DFID" is on the remote MDT\n",
1507                        mdt_obd_name(info->mti_mdt), PFID(old_fid));
1508                 GOTO(out_put_child, rc = -EREMOTE);
1509         }
1510
1511         if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1512             !mdt_object_remote(msrcdir)) {
1513                 CERROR("%s: parent "DFID" is still on the same"
1514                        " MDT, which should be migrated first:"
1515                        " rc = %d\n", mdt_obd_name(info->mti_mdt),
1516                        PFID(mdt_object_fid(msrcdir)), -EPERM);
1517                 GOTO(out_put_child, rc = -EPERM);
1518         }
1519
1520         rc = mdt_remote_permission(info, msrcdir, mold);
1521         if (rc != 0)
1522                 GOTO(out_put_child, rc);
1523
1524         /* 3: iterate the linkea of the object and lock all of the objects */
1525         INIT_LIST_HEAD(&lock_list);
1526         rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1527         if (rc != 0)
1528                 GOTO(out_put_child, rc);
1529
1530         /* 4: lock of the object migrated object */
1531         lh_childp = &info->mti_lh[MDT_LH_OLD];
1532         mdt_lock_reg_init(lh_childp, LCK_EX);
1533         rc = mdt_object_lock(info, mold, lh_childp,
1534                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1535                              MDS_INODELOCK_LAYOUT, MDT_CROSS_LOCK);
1536         if (rc != 0)
1537                 GOTO(out_unlock_list, rc);
1538
1539         ma->ma_need = MA_LMV;
1540         ma->ma_valid = 0;
1541         ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1542         ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1543         rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1544         if (rc != 0)
1545                 GOTO(out_unlock_list, rc);
1546
1547         if ((ma->ma_valid & MA_LMV)) {
1548                 struct lmv_mds_md_v1 *lmm1;
1549
1550                 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1551                 lmm1 = &ma->ma_lmv->lmv_md_v1;
1552                 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1553                         CERROR("%s: can not migrate striped dir "DFID
1554                                ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1555                                PFID(mdt_object_fid(mold)), -EPERM);
1556                         GOTO(out_unlock_child, rc = -EPERM);
1557                 }
1558
1559                 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1560                         GOTO(out_unlock_child, rc = -EINVAL);
1561
1562                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1563                                        &lmm1->lmv_stripe_fids[1]);
1564                 if (IS_ERR(mnew))
1565                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1566
1567                 if (!mdt_object_remote(mnew)) {
1568                         CERROR("%s: "DFID" being migrated is on this MDT:"
1569                                " rc  = %d\n", mdt_obd_name(info->mti_mdt),
1570                                PFID(rr->rr_fid2), -EPERM);
1571                         GOTO(out_put_new, rc = -EPERM);
1572                 }
1573
1574                 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1575                 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1576                 rc = mdt_remote_object_lock(info, mnew,
1577                                             mdt_object_fid(mnew),
1578                                             &lh_tgtp->mlh_rreg_lh,
1579                                             lh_tgtp->mlh_rreg_mode,
1580                                             MDS_INODELOCK_UPDATE);
1581                 if (rc != 0) {
1582                         lh_tgtp = NULL;
1583                         GOTO(out_put_new, rc);
1584                 }
1585         } else {
1586                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1587                                        rr->rr_fid2);
1588                 if (IS_ERR(mnew))
1589                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1590                 if (!mdt_object_remote(mnew)) {
1591                         CERROR("%s: Migration "DFID" is on this MDT:"
1592                                " rc = %d\n", mdt_obd_name(info->mti_mdt),
1593                                PFID(rr->rr_fid2), -EXDEV);
1594                         GOTO(out_put_new, rc = -EXDEV);
1595                 }
1596         }
1597
1598         /* 5: migrate it */
1599         mdt_reint_init_ma(info, ma);
1600
1601         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1602                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1603
1604         rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1605                          mdt_object_child(mold), &rr->rr_name,
1606                          mdt_object_child(mnew), ma);
1607         if (rc != 0)
1608                 GOTO(out_unlock_new, rc);
1609 out_unlock_new:
1610         if (lh_tgtp != NULL)
1611                 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1612 out_put_new:
1613         if (mnew)
1614                 mdt_object_put(info->mti_env, mnew);
1615 out_unlock_child:
1616         mdt_object_unlock(info, mold, lh_childp, rc);
1617 out_unlock_list:
1618         mdt_unlock_list(info, &lock_list, rc);
1619 out_put_child:
1620         mdt_object_put(info->mti_env, mold);
1621 out_unlock_parent:
1622         mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1623 out_put_parent:
1624         mdt_object_put(info->mti_env, msrcdir);
1625
1626         RETURN(rc);
1627 }
1628
1629 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1630                                                 const struct lu_fid *fid,
1631                                                 int idx)
1632 {
1633         struct mdt_object *dir;
1634         int rc;
1635         ENTRY;
1636
1637         dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1638         if (IS_ERR(dir))
1639                 RETURN(dir);
1640
1641         /* check early, the real version will be saved after locking */
1642         rc = mdt_version_get_check(info, dir, idx);
1643         if (rc)
1644                 GOTO(out_put, rc);
1645
1646         RETURN(dir);
1647 out_put:
1648         mdt_object_put(info->mti_env, dir);
1649         return ERR_PTR(rc);
1650 }
1651
1652 static int mdt_object_lock_save(struct mdt_thread_info *info,
1653                                 struct mdt_object *dir,
1654                                 struct mdt_lock_handle *lh,
1655                                 int idx)
1656 {
1657         int rc;
1658
1659         /* we lock the target dir if it is local */
1660         rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
1661                              MDT_LOCAL_LOCK);
1662         if (rc != 0)
1663                 return rc;
1664
1665         /* get and save correct version after locking */
1666         mdt_version_get_save(info, dir, idx);
1667         return 0;
1668 }
1669
1670
1671 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1672                                    struct mdt_object **srcp,
1673                                    struct mdt_object **tgtp)
1674 {
1675         struct mdt_reint_record *rr = &info->mti_rr;
1676         const struct lu_fid     *fid_src = rr->rr_fid1;
1677         const struct lu_fid     *fid_tgt = rr->rr_fid2;
1678         struct mdt_lock_handle  *lh_src = &info->mti_lh[MDT_LH_PARENT];
1679         struct mdt_lock_handle  *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1680         struct mdt_object       *src;
1681         struct mdt_object       *tgt;
1682         int                      reverse = 0;
1683         int                      rc;
1684         ENTRY;
1685
1686         /* find both parents. */
1687         src = mdt_object_find_check(info, fid_src, 0);
1688         if (IS_ERR(src))
1689                 RETURN(PTR_ERR(src));
1690
1691         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1692
1693         if (lu_fid_eq(fid_src, fid_tgt)) {
1694                 tgt = src;
1695                 mdt_object_get(info->mti_env, tgt);
1696         } else {
1697                 /* Check if the @src is not a child of the @tgt, otherwise a
1698                  * reverse locking must take place. */
1699                 rc = mdt_is_subdir(info, src, fid_tgt);
1700                 if (rc == -EINVAL)
1701                         reverse = 1;
1702                 else if (rc)
1703                         GOTO(err_src_put, rc);
1704
1705                 tgt = mdt_object_find_check(info, fid_tgt, 1);
1706                 if (IS_ERR(tgt))
1707                         GOTO(err_src_put, rc = PTR_ERR(tgt));
1708
1709                 if (unlikely(mdt_object_remote(tgt))) {
1710                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1711                                "on different MDTs\n", PFID(fid_src),
1712                                PFID(fid_tgt));
1713                         GOTO(err_tgt_put, rc = -EXDEV);
1714                 }
1715         }
1716
1717         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1718
1719         /* lock parents in the proper order. */
1720         if (reverse) {
1721                 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1722                 if (rc)
1723                         GOTO(err_tgt_put, rc);
1724
1725                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1726
1727                 rc = mdt_object_lock_save(info, src, lh_src, 0);
1728         } else {
1729                 rc = mdt_object_lock_save(info, src, lh_src, 0);
1730                 if (rc)
1731                         GOTO(err_tgt_put, rc);
1732
1733                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1734
1735                 if (tgt != src)
1736                         rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1737                 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1738                         rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1739                                                 MDS_INODELOCK_UPDATE);
1740                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1741                 }
1742         }
1743         if (rc)
1744                 GOTO(err_unlock, rc);
1745
1746         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1747
1748         *srcp = src;
1749         *tgtp = tgt;
1750         RETURN(0);
1751
1752 err_unlock:
1753         /* The order does not matter as the handle is checked inside,
1754          * as well as not used handle. */
1755         mdt_object_unlock(info, src, lh_src, rc);
1756         mdt_object_unlock(info, tgt, lh_tgt, rc);
1757 err_tgt_put:
1758         mdt_object_put(info->mti_env, tgt);
1759 err_src_put:
1760         mdt_object_put(info->mti_env, src);
1761         RETURN(rc);
1762 }
1763
1764 /*
1765  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1766  * 2 - src child; 3 - tgt child.
1767  * Update on disk version of src child.
1768  */
1769 /**
1770  * For DNE phase I, only these renames are allowed
1771  *      mv src_p/src_c tgt_p/tgt_c
1772  * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1773  * 2. src_p and tgt_p are same directory, and tgt_c does not
1774  *    exists. In this case, all of modification will happen
1775  *    in the MDT where ithesource parent is, only one remote
1776  *    update is needed, i.e. set c_time/m_time on the child.
1777  *    And tgt_c will be still in the same MDT as the original
1778  *    src_c.
1779  */
1780 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1781                                      struct mdt_lock_handle *lhc)
1782 {
1783         struct mdt_reint_record *rr = &info->mti_rr;
1784         struct md_attr          *ma = &info->mti_attr;
1785         struct ptlrpc_request   *req = mdt_info_req(info);
1786         struct mdt_object       *msrcdir = NULL;
1787         struct mdt_object       *mtgtdir = NULL;
1788         struct mdt_object       *mold;
1789         struct mdt_object       *mnew = NULL;
1790         struct mdt_lock_handle  *lh_srcdirp;
1791         struct mdt_lock_handle  *lh_tgtdirp;
1792         struct mdt_lock_handle  *lh_oldp = NULL;
1793         struct mdt_lock_handle  *lh_newp = NULL;
1794         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1795         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1796         int                      rc;
1797         ENTRY;
1798
1799         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1800                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1801                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1802
1803         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1804         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1805         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1806         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1807
1808         /* step 1&2: lock the source and target dirs. */
1809         rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1810         if (rc)
1811                 RETURN(rc);
1812
1813         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1814
1815         /* step 3: find & lock the old object. */
1816         fid_zero(old_fid);
1817         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1818         if (rc != 0)
1819                 GOTO(out_unlock_parents, rc);
1820
1821         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1822                 GOTO(out_unlock_parents, rc = -EINVAL);
1823
1824         if (!fid_is_md_operative(old_fid))
1825                 GOTO(out_unlock_parents, rc = -EPERM);
1826
1827         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1828         if (IS_ERR(mold))
1829                 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1830
1831         /* Check if @mtgtdir is subdir of @mold, before locking child
1832          * to avoid reverse locking. */
1833         rc = mdt_is_subdir(info, mtgtdir, old_fid);
1834         if (rc)
1835                 GOTO(out_put_old, rc);
1836
1837         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1838         /* save version after locking */
1839         mdt_version_get_save(info, mold, 2);
1840         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1841
1842         /* step 4: find & lock the new object. */
1843         /* new target object may not exist now */
1844         /* lookup with version checking */
1845         fid_zero(new_fid);
1846         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1847                                       3);
1848         if (rc == 0) {
1849                 /* the new_fid should have been filled at this moment */
1850                 if (lu_fid_eq(old_fid, new_fid))
1851                         GOTO(out_put_old, rc);
1852
1853                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1854                     lu_fid_eq(new_fid, rr->rr_fid2))
1855                         GOTO(out_put_old, rc = -EINVAL);
1856
1857                 if (!fid_is_md_operative(new_fid))
1858                         GOTO(out_put_old, rc = -EPERM);
1859
1860                 if (mdt_object_remote(mold)) {
1861                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1862                                PFID(old_fid));
1863                         GOTO(out_put_old, rc = -EXDEV);
1864                 }
1865
1866                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1867                 if (IS_ERR(mnew))
1868                         GOTO(out_put_old, rc = PTR_ERR(mnew));
1869
1870                 if (mdt_object_remote(mnew)) {
1871                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1872                                PFID(new_fid));
1873                         GOTO(out_put_new, rc = -EXDEV);
1874                 }
1875
1876                 /* Before locking the target dir, check we do not replace
1877                  * a dir with a non-dir, otherwise it may deadlock with
1878                  * link op which tries to create a link in this dir
1879                  * back to this non-dir. */
1880                 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1881                     !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1882                         GOTO(out_put_new, rc = -EISDIR);
1883
1884                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1885                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1886                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1887                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1888                 if (rc != 0)
1889                         GOTO(out_put_new, rc);
1890
1891                 /* Check if @msrcdir is subdir of @mnew, before locking child
1892                  * to avoid reverse locking. */
1893                 rc = mdt_is_subdir(info, msrcdir, new_fid);
1894                 if (rc)
1895                         GOTO(out_unlock_old, rc);
1896
1897                 /* We used to acquire MDS_INODELOCK_FULL here but we
1898                  * can't do this now because a running HSM restore on
1899                  * the rename onto victim will hold the layout
1900                  * lock. See LU-4002. */
1901
1902                 lh_newp = &info->mti_lh[MDT_LH_NEW];
1903                 mdt_lock_reg_init(lh_newp, LCK_EX);
1904                 rc = mdt_object_lock(info, mnew, lh_newp,
1905                                      MDS_INODELOCK_LOOKUP |
1906                                      MDS_INODELOCK_UPDATE,
1907                                      MDT_LOCAL_LOCK);
1908                 if (rc != 0)
1909                         GOTO(out_unlock_old, rc);
1910
1911                 /* get and save version after locking */
1912                 mdt_version_get_save(info, mnew, 3);
1913                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1914         } else if (rc != -EREMOTE && rc != -ENOENT) {
1915                 GOTO(out_put_old, rc);
1916         } else {
1917                 /* If mnew does not exist and mold are remote directory,
1918                  * it only allows rename if they are under same directory */
1919                 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1920                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1921                                PFID(old_fid));
1922                         GOTO(out_put_old, rc = -EXDEV);
1923                 }
1924
1925                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1926                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1927                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1928                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1929                 if (rc != 0)
1930                         GOTO(out_put_old, rc);
1931
1932                 mdt_enoent_version_save(info, 3);
1933         }
1934
1935         /* step 5: rename it */
1936         mdt_reint_init_ma(info, ma);
1937
1938         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1939                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1940
1941         if (mnew != NULL)
1942                 mutex_lock(&mnew->mot_lov_mutex);
1943
1944         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1945                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1946                         mnew != NULL ? mdt_object_child(mnew) : NULL,
1947                         &rr->rr_tgt_name, ma);
1948
1949         if (mnew != NULL)
1950                 mutex_unlock(&mnew->mot_lov_mutex);
1951
1952         /* handle last link of tgt object */
1953         if (rc == 0) {
1954                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1955                 if (mnew)
1956                         mdt_handle_last_unlink(info, mnew, ma);
1957
1958                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1959                                          msrcdir, mtgtdir);
1960         }
1961
1962         EXIT;
1963         if (mnew != NULL)
1964                 mdt_object_unlock(info, mnew, lh_newp, rc);
1965 out_unlock_old:
1966         mdt_object_unlock(info, mold, lh_oldp, rc);
1967 out_put_new:
1968         if (mnew != NULL)
1969                 mdt_object_put(info->mti_env, mnew);
1970 out_put_old:
1971         mdt_object_put(info->mti_env, mold);
1972 out_unlock_parents:
1973         mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1974         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1975         return rc;
1976 }
1977
1978 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1979                                        struct mdt_lock_handle *lhc,
1980                                        enum mdt_rename_lock rename_lock)
1981 {
1982         struct mdt_reint_record *rr = &info->mti_rr;
1983         struct ptlrpc_request   *req = mdt_info_req(info);
1984         struct lustre_handle    rename_lh = { 0 };
1985         int                     rc;
1986         ENTRY;
1987
1988         if (info->mti_dlm_req)
1989                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1990
1991         if (!fid_is_md_operative(rr->rr_fid1) ||
1992             !fid_is_md_operative(rr->rr_fid2))
1993                 RETURN(-EPERM);
1994
1995         rc = mdt_rename_lock(info, &rename_lh, rename_lock);
1996         if (rc != 0) {
1997                 CERROR("%s: can't lock FS for rename: rc  = %d\n",
1998                        mdt_obd_name(info->mti_mdt), rc);
1999                 RETURN(rc);
2000         }
2001
2002         if (rename_lock == MRL_RENAME)
2003                 rc = mdt_reint_rename_internal(info, lhc);
2004         else
2005                 rc = mdt_reint_migrate_internal(info, lhc);
2006
2007         if (lustre_handle_is_used(&rename_lh))
2008                 mdt_rename_unlock(&rename_lh);
2009
2010         RETURN(rc);
2011 }
2012
2013 static int mdt_reint_rename(struct mdt_thread_info *info,
2014                             struct mdt_lock_handle *lhc)
2015 {
2016         return mdt_reint_rename_or_migrate(info, lhc, MRL_RENAME);
2017 }
2018
2019 static int mdt_reint_migrate(struct mdt_thread_info *info,
2020                             struct mdt_lock_handle *lhc)
2021 {
2022         return mdt_reint_rename_or_migrate(info, lhc, MRL_MIGRATE);
2023 }
2024
2025 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
2026                            struct mdt_lock_handle *lhc);
2027
2028 static mdt_reinter reinters[REINT_MAX] = {
2029         [REINT_SETATTR]  = mdt_reint_setattr,
2030         [REINT_CREATE]   = mdt_reint_create,
2031         [REINT_LINK]     = mdt_reint_link,
2032         [REINT_UNLINK]   = mdt_reint_unlink,
2033         [REINT_RENAME]   = mdt_reint_rename,
2034         [REINT_OPEN]     = mdt_reint_open,
2035         [REINT_SETXATTR] = mdt_reint_setxattr,
2036         [REINT_RMENTRY]  = mdt_reint_unlink,
2037         [REINT_MIGRATE]   = mdt_reint_migrate,
2038 };
2039
2040 int mdt_reint_rec(struct mdt_thread_info *info,
2041                   struct mdt_lock_handle *lhc)
2042 {
2043         int rc;
2044         ENTRY;
2045
2046         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
2047
2048         RETURN(rc);
2049 }