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