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