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