Whamcloud - gitweb
LU-7091 mdd: refresh nlink after update linkea
[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 static inline int mdt_remote_permission_check(struct mdt_thread_info *info)
224 {
225         struct lu_ucred *uc  = mdt_ucred(info);
226         struct mdt_device *mdt = info->mti_mdt;
227
228         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
229                 if (uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
230                     mdt->mdt_enable_remote_dir_gid != -1)
231                         return -EPERM;
232         }
233
234         return 0;
235 }
236
237 /**
238  * mdt_remote_permission: Check whether the remote operation is permitted,
239  *
240  * Only sysadmin can create remote directory / striped directory,
241  * migrate directory and set default stripedEA on directory, unless
242  *
243  * lctl set_param mdt.*.enable_remote_dir_gid=allow_gid.
244  *
245  * param[in] info: mdt_thread_info.
246  *
247  * retval       = 0 remote operation is allowed.
248  *              < 0 remote operation is denied.
249  */
250 static int mdt_remote_permission(struct mdt_thread_info *info)
251 {
252         struct md_op_spec *spec = &info->mti_spec;
253         struct lu_attr *attr = &info->mti_attr.ma_attr;
254         struct obd_export *exp = mdt_info_req(info)->rq_export;
255         int rc;
256
257         if (info->mti_rr.rr_opcode == REINT_MIGRATE) {
258                 rc = mdt_remote_permission_check(info);
259                 if (rc != 0)
260                         return rc;
261         }
262
263         if (info->mti_rr.rr_opcode == REINT_CREATE &&
264             (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
265              spec->u.sp_ea.eadatalen != 0)) {
266                 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
267
268                 /* Only new clients can create remote dir( >= 2.4) and
269                  * striped dir(>= 2.6), old client will return -ENOTSUPP */
270                 if (!mdt_is_dne_client(exp))
271                         return -ENOTSUPP;
272
273                 if (le32_to_cpu(lum->lum_stripe_count) > 1 &&
274                     !mdt_is_striped_client(exp))
275                         return -ENOTSUPP;
276
277                 rc = mdt_remote_permission_check(info);
278                 if (rc != 0)
279                         return rc;
280         }
281
282         if (info->mti_rr.rr_opcode == REINT_SETATTR) {
283                 struct md_attr *ma = &info->mti_attr;
284
285                 if ((ma->ma_valid & MA_LMV)) {
286                         rc = mdt_remote_permission_check(info);
287                         if (rc != 0)
288                                 return rc;
289                 }
290         }
291
292         return 0;
293 }
294
295 /*
296  * VBR: we save three versions in reply:
297  * 0 - parent. Check that parent version is the same during replay.
298  * 1 - name. Version of 'name' if file exists with the same name or
299  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
300  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
301  * check.
302  */
303 static int mdt_md_create(struct mdt_thread_info *info)
304 {
305         struct mdt_device       *mdt = info->mti_mdt;
306         struct mdt_object       *parent;
307         struct mdt_object       *child;
308         struct mdt_lock_handle  *lh;
309         struct mdt_body         *repbody;
310         struct md_attr          *ma = &info->mti_attr;
311         struct mdt_reint_record *rr = &info->mti_rr;
312         int rc;
313         ENTRY;
314
315         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  ("DNAME"->"DFID") "
316                   "in "DFID,
317                   PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
318
319         if (!fid_is_md_operative(rr->rr_fid1))
320                 RETURN(-EPERM);
321
322         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
323
324         parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
325         if (IS_ERR(parent))
326                 RETURN(PTR_ERR(parent));
327
328         if (!mdt_object_exists(parent))
329                 GOTO(put_parent, rc = -ENOENT);
330
331         lh = &info->mti_lh[MDT_LH_PARENT];
332         mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
333         rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
334         if (rc)
335                 GOTO(put_parent, rc);
336
337         if (!mdt_object_remote(parent)) {
338                 rc = mdt_version_get_check_save(info, parent, 0);
339                 if (rc)
340                         GOTO(unlock_parent, rc);
341         }
342
343         /*
344          * Check child name version during replay.
345          * During create replay a file may exist with same name.
346          */
347         rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
348                                       &info->mti_tmp_fid1, 1);
349         if (rc == 0)
350                 GOTO(unlock_parent, rc = -EEXIST);
351
352         /* -ENOENT is expected here */
353         if (rc != -ENOENT)
354                 GOTO(unlock_parent, rc);
355
356         /* save version of file name for replay, it must be ENOENT here */
357         mdt_enoent_version_save(info, 1);
358
359         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
360         if (likely(!IS_ERR(child))) {
361                 struct md_object *next = mdt_object_child(parent);
362
363                 rc = mdt_remote_permission(info);
364                 if (rc != 0)
365                         GOTO(out_put_child, rc);
366
367                 ma->ma_need = MA_INODE;
368                 ma->ma_valid = 0;
369
370                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
371                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
372
373                 /* Version of child will be updated on disk. */
374                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
375                 rc = mdt_version_get_check_save(info, child, 2);
376                 if (rc)
377                         GOTO(out_put_child, rc);
378
379                 /* Let lower layer know current lock mode. */
380                 info->mti_spec.sp_cr_mode =
381                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
382
383                 /*
384                  * Do not perform lookup sanity check. We know that name does
385                  * not exist.
386                  */
387                 info->mti_spec.sp_cr_lookup = 0;
388                 info->mti_spec.sp_feat = &dt_directory_features;
389
390                 rc = mdo_create(info->mti_env, next, &rr->rr_name,
391                                 mdt_object_child(child), &info->mti_spec, ma);
392                 if (rc == 0)
393                         rc = mdt_attr_get_complex(info, child, ma);
394
395                 if (rc == 0) {
396                         /* Return fid & attr to client. */
397                         if (ma->ma_valid & MA_INODE)
398                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
399                                                    mdt_object_fid(child));
400                 }
401 out_put_child:
402                 mdt_object_put(info->mti_env, child);
403         } else {
404                 rc = PTR_ERR(child);
405         }
406 unlock_parent:
407         mdt_object_unlock(info, parent, lh, rc);
408 put_parent:
409         mdt_object_put(info->mti_env, parent);
410         RETURN(rc);
411 }
412
413 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
414                              struct mdt_object *obj, __u64 ibits,
415                              struct mdt_lock_handle *s0_lh,
416                              struct mdt_object *s0_obj,
417                              struct ldlm_enqueue_info *einfo)
418 {
419         union ldlm_policy_data *policy = &mti->mti_policy;
420         int rc;
421         ENTRY;
422
423         if (!S_ISDIR(obj->mot_header.loh_attr))
424                 RETURN(0);
425
426         /* Unlock stripe 0 */
427         if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
428                 LASSERT(s0_obj != NULL);
429                 mdt_object_unlock_put(mti, s0_obj, s0_lh, 1);
430         }
431
432         memset(policy, 0, sizeof(*policy));
433         policy->l_inodebits.bits = ibits;
434
435         rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
436                               policy);
437         RETURN(rc);
438 }
439
440 /**
441  * Lock slave stripes if necessary, the lock handles of slave stripes
442  * will be stored in einfo->ei_cbdata.
443  **/
444 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
445                            enum ldlm_mode mode, __u64 ibits,
446                            struct mdt_lock_handle *s0_lh,
447                            struct mdt_object **s0_objp,
448                            struct ldlm_enqueue_info *einfo)
449 {
450         union ldlm_policy_data *policy = &mti->mti_policy;
451         struct lu_buf *buf = &mti->mti_buf;
452         struct lmv_mds_md_v1 *lmv;
453         struct lu_fid *fid = &mti->mti_tmp_fid1;
454         int rc;
455         ENTRY;
456
457         if (!S_ISDIR(obj->mot_header.loh_attr))
458                 RETURN(0);
459
460         buf->lb_buf = mti->mti_xattr_buf;
461         buf->lb_len = sizeof(mti->mti_xattr_buf);
462         rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
463                           XATTR_NAME_LMV);
464         if (rc == -ERANGE) {
465                 rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
466                 if (rc > 0) {
467                         buf->lb_buf = mti->mti_big_lmm;
468                         buf->lb_len = mti->mti_big_lmmsize;
469                 }
470         }
471
472         if (rc == -ENODATA || rc == -ENOENT)
473                 RETURN(0);
474
475         if (rc <= 0)
476                 RETURN(rc);
477
478         lmv = buf->lb_buf;
479         if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
480                 RETURN(-EINVAL);
481
482         fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
483         if (!lu_fid_eq(fid, mdt_object_fid(obj))) {
484                 /* Except migrating object, whose 0_stripe and master
485                  * object are the same object, 0_stripe and master
486                  * object are different, though they are in the same
487                  * MDT, to avoid adding osd_object_lock here, so we
488                  * will enqueue the stripe0 lock in MDT0 for now */
489                 *s0_objp = mdt_object_find_lock(mti, fid, s0_lh, ibits);
490                 if (IS_ERR(*s0_objp))
491                         RETURN(PTR_ERR(*s0_objp));
492         }
493
494         memset(einfo, 0, sizeof(*einfo));
495         einfo->ei_type = LDLM_IBITS;
496         einfo->ei_mode = mode;
497         einfo->ei_cb_bl = mdt_remote_blocking_ast;
498         einfo->ei_cb_local_bl = mdt_blocking_ast;
499         einfo->ei_cb_cp = ldlm_completion_ast;
500         einfo->ei_enq_slave = 1;
501         einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
502         memset(policy, 0, sizeof(*policy));
503         policy->l_inodebits.bits = ibits;
504
505         rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
506                             policy);
507         RETURN(rc);
508 }
509
510 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
511                         struct md_attr *ma)
512 {
513         struct mdt_lock_handle  *lh;
514         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
515         __u64 lockpart = MDS_INODELOCK_UPDATE;
516         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
517         struct mdt_lock_handle  *s0_lh;
518         struct mdt_object       *s0_obj = NULL;
519         int rc;
520         ENTRY;
521
522         lh = &info->mti_lh[MDT_LH_PARENT];
523         mdt_lock_reg_init(lh, LCK_PW);
524
525         /* Even though the new MDT will grant PERM lock to the old
526          * client, but the old client will almost ignore that during
527          * So it needs to revoke both LOOKUP and PERM lock here, so
528          * both new and old client can cancel the dcache */
529         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
530                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
531
532         rc = mdt_object_lock(info, mo, lh, lockpart);
533         if (rc != 0)
534                 RETURN(rc);
535
536         s0_lh = &info->mti_lh[MDT_LH_LOCAL];
537         mdt_lock_reg_init(s0_lh, LCK_PW);
538         rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
539         if (rc != 0)
540                 GOTO(out_unlock, rc);
541
542         /* all attrs are packed into mti_attr in unpack_setattr */
543         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
544                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
545
546         /* This is only for set ctime when rename's source is on remote MDS. */
547         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
548                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
549
550         /* VBR: update version if attr changed are important for recovery */
551         if (do_vbr) {
552                 /* update on-disk version of changed object */
553                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
554                 rc = mdt_version_get_check_save(info, mo, 0);
555                 if (rc)
556                         GOTO(out_unlock, rc);
557         }
558
559         /* Ensure constant striping during chown(). See LU-2789. */
560         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
561                 mutex_lock(&mo->mot_lov_mutex);
562
563         /* all attrs are packed into mti_attr in unpack_setattr */
564         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
565
566         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
567                 mutex_unlock(&mo->mot_lov_mutex);
568
569         if (rc != 0)
570                 GOTO(out_unlock, rc);
571
572         EXIT;
573 out_unlock:
574         mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
575         mdt_object_unlock(info, mo, lh, rc);
576         return rc;
577 }
578
579 /**
580  * Check HSM flags and add HS_DIRTY flag if relevant.
581  *
582  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
583  * and is not RELEASED.
584  */
585 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
586                         struct md_attr *ma)
587 {
588         int rc;
589         ENTRY;
590
591         /* If the file was modified, add the dirty flag */
592         ma->ma_need = MA_HSM;
593         rc = mdt_attr_get_complex(info, mo, ma);
594         if (rc) {
595                 CERROR("file attribute read error for "DFID": %d.\n",
596                         PFID(mdt_object_fid(mo)), rc);
597                 RETURN(rc);
598         }
599
600         /* If an up2date copy exists in the backend, add dirty flag */
601         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
602             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
603                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
604
605                 ma->ma_hsm.mh_flags |= HS_DIRTY;
606
607                 mdt_lock_reg_init(lh, LCK_PW);
608                 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR);
609                 if (rc != 0)
610                         RETURN(rc);
611
612                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
613                 if (rc)
614                         CERROR("file attribute change error for "DFID": %d\n",
615                                 PFID(mdt_object_fid(mo)), rc);
616                 mdt_object_unlock(info, mo, lh, rc);
617         }
618
619         RETURN(rc);
620 }
621
622 static int mdt_reint_setattr(struct mdt_thread_info *info,
623                              struct mdt_lock_handle *lhc)
624 {
625         struct md_attr          *ma = &info->mti_attr;
626         struct mdt_reint_record *rr = &info->mti_rr;
627         struct ptlrpc_request   *req = mdt_info_req(info);
628         struct mdt_object       *mo;
629         struct mdt_body         *repbody;
630         int                      rc, rc2;
631         ENTRY;
632
633         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
634                   (unsigned int)ma->ma_attr.la_valid);
635
636         if (info->mti_dlm_req)
637                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
638
639         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
640         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
641         if (IS_ERR(mo))
642                 GOTO(out, rc = PTR_ERR(mo));
643
644         if (!mdt_object_exists(mo))
645                 GOTO(out_put, rc = -ENOENT);
646
647         if (mdt_object_remote(mo))
648                 GOTO(out_put, rc = -EREMOTE);
649
650         if ((ma->ma_attr.la_valid & LA_SIZE) ||
651             (rr->rr_flags & MRF_OPEN_TRUNC)) {
652                 /* Check write access for the O_TRUNC case */
653                 if (mdt_write_read(mo) < 0)
654                         GOTO(out_put, rc = -ETXTBSY);
655         }
656
657         if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
658                 if (ma->ma_valid & MA_LOV)
659                         GOTO(out_put, rc = -EPROTO);
660
661                 rc = mdt_attr_set(info, mo, ma);
662                 if (rc)
663                         GOTO(out_put, rc);
664         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
665                 struct lu_buf *buf  = &info->mti_buf;
666
667                 if (ma->ma_attr.la_valid != 0)
668                         GOTO(out_put, rc = -EPROTO);
669
670                 buf->lb_buf = ma->ma_lmm;
671                 buf->lb_len = ma->ma_lmm_size;
672                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
673                                   buf, XATTR_NAME_LOV, 0);
674                 if (rc)
675                         GOTO(out_put, rc);
676         } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
677                 struct lu_buf *buf  = &info->mti_buf;
678                 struct mdt_lock_handle  *lh;
679
680                 rc = mdt_remote_permission(info);
681                 if (rc < 0)
682                         GOTO(out_put, rc);
683
684                 if (ma->ma_attr.la_valid != 0)
685                         GOTO(out_put, rc = -EPROTO);
686
687                 lh = &info->mti_lh[MDT_LH_PARENT];
688                 mdt_lock_reg_init(lh, LCK_PW);
689
690                 rc = mdt_object_lock(info, mo, lh,
691                                      MDS_INODELOCK_XATTR);
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         __u64                   lock_ibits;
793         int                     rc;
794         int                     no_name = 0;
795         ENTRY;
796
797         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
798                   PNAME(&rr->rr_name));
799
800         if (info->mti_dlm_req)
801                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
802
803         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
804                 RETURN(err_serious(-ENOENT));
805
806         if (!fid_is_md_operative(rr->rr_fid1))
807                 RETURN(-EPERM);
808
809         /*
810          * step 1: Found the parent.
811          */
812         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
813         if (IS_ERR(mp)) {
814                 rc = PTR_ERR(mp);
815                 GOTO(out, rc);
816         }
817
818         parent_lh = &info->mti_lh[MDT_LH_PARENT];
819         mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
820         rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE);
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                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
910                 LASSERT(repbody != NULL);
911                 repbody->mbo_fid1 = *mdt_object_fid(mc);
912                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
913                 GOTO(unlock_child, rc = -EREMOTE);
914         } else if (info->mti_spec.sp_rm_entry) {
915                 rc = -EPERM;
916                 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
917                        "rc = %d\n",
918                        mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
919                 GOTO(put_child, rc);
920         }
921
922         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
923          * this now because a running HSM restore on the child (unlink
924          * victim) will hold the layout lock. See LU-4002. */
925         lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
926         if (mdt_object_remote(mp)) {
927                 /* Enqueue lookup lock from parent MDT */
928                 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
929                                             &child_lh->mlh_rreg_lh,
930                                             child_lh->mlh_rreg_mode,
931                                             MDS_INODELOCK_LOOKUP, false);
932                 if (rc != ELDLM_OK)
933                         GOTO(put_child, rc);
934
935                 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
936         }
937
938         rc = mdt_object_lock(info, mc, child_lh, lock_ibits);
939         if (rc != 0)
940                 GOTO(put_child, rc);
941         /*
942          * Now we can only make sure we need MA_INODE, in mdd layer, will check
943          * whether need MA_LOV and MA_COOKIE.
944          */
945         ma->ma_need = MA_INODE;
946         ma->ma_valid = 0;
947
948         s0_lh = &info->mti_lh[MDT_LH_LOCAL];
949         mdt_lock_reg_init(s0_lh, LCK_EX);
950         rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
951                              &s0_obj, einfo);
952         if (rc != 0)
953                 GOTO(unlock_child, rc);
954
955         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
956                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
957         /* save version when object is locked */
958         mdt_version_get_save(info, mc, 1);
959
960         mutex_lock(&mc->mot_lov_mutex);
961
962         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
963                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
964
965         mutex_unlock(&mc->mot_lov_mutex);
966
967         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
968                 rc = mdt_attr_get_complex(info, mc, ma);
969         if (rc == 0)
970                 mdt_handle_last_unlink(info, mc, ma);
971
972         if (ma->ma_valid & MA_INODE) {
973                 switch (ma->ma_attr.la_mode & S_IFMT) {
974                 case S_IFDIR:
975                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
976                         break;
977                 case S_IFREG:
978                 case S_IFLNK:
979                 case S_IFCHR:
980                 case S_IFBLK:
981                 case S_IFIFO:
982                 case S_IFSOCK:
983                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
984                         break;
985                 default:
986                         LASSERTF(0, "bad file type %o unlinking\n",
987                                  ma->ma_attr.la_mode);
988                 }
989         }
990
991         EXIT;
992
993 unlock_child:
994         mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
995         mdt_object_unlock(info, mc, child_lh, rc);
996 put_child:
997         mdt_object_put(info->mti_env, mc);
998 unlock_parent:
999         mdt_object_unlock(info, mp, parent_lh, rc);
1000 put_parent:
1001         mdt_object_put(info->mti_env, mp);
1002 out:
1003         return rc;
1004 }
1005
1006 /*
1007  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1008  * name.
1009  */
1010 static int mdt_reint_link(struct mdt_thread_info *info,
1011                           struct mdt_lock_handle *lhc)
1012 {
1013         struct mdt_reint_record *rr = &info->mti_rr;
1014         struct ptlrpc_request   *req = mdt_info_req(info);
1015         struct md_attr          *ma = &info->mti_attr;
1016         struct mdt_object       *ms;
1017         struct mdt_object       *mp;
1018         struct mdt_lock_handle  *lhs;
1019         struct mdt_lock_handle  *lhp;
1020         int rc;
1021         ENTRY;
1022
1023         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1024                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1025
1026         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1027                 RETURN(err_serious(-ENOENT));
1028
1029         if (info->mti_dlm_req)
1030                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1031
1032         /* Invalid case so return error immediately instead of
1033          * processing it */
1034         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1035                 RETURN(-EPERM);
1036
1037         if (!fid_is_md_operative(rr->rr_fid1) ||
1038             !fid_is_md_operative(rr->rr_fid2))
1039                 RETURN(-EPERM);
1040
1041         /* step 1: find & lock the target parent dir */
1042         lhp = &info->mti_lh[MDT_LH_PARENT];
1043         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1044         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1045                                   MDS_INODELOCK_UPDATE);
1046         if (IS_ERR(mp))
1047                 RETURN(PTR_ERR(mp));
1048
1049         rc = mdt_version_get_check_save(info, mp, 0);
1050         if (rc)
1051                 GOTO(out_unlock_parent, rc);
1052
1053         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1054
1055         /* step 2: find & lock the source */
1056         lhs = &info->mti_lh[MDT_LH_CHILD];
1057         mdt_lock_reg_init(lhs, LCK_EX);
1058
1059         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1060         if (IS_ERR(ms))
1061                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1062
1063         if (!mdt_object_exists(ms)) {
1064                 mdt_object_put(info->mti_env, ms);
1065                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1066                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1067                 GOTO(out_unlock_parent, rc = -ENOENT);
1068         }
1069
1070         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1071                              MDS_INODELOCK_XATTR);
1072         if (rc != 0) {
1073                 mdt_object_put(info->mti_env, ms);
1074                 GOTO(out_unlock_parent, rc);
1075         }
1076
1077         /* step 3: link it */
1078         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1079                        OBD_FAIL_MDS_REINT_LINK_WRITE);
1080
1081         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1082         rc = mdt_version_get_check_save(info, ms, 1);
1083         if (rc)
1084                 GOTO(out_unlock_child, rc);
1085
1086         /** check target version by name during replay */
1087         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1088                                       &info->mti_tmp_fid1, 2);
1089         if (rc != 0 && rc != -ENOENT)
1090                 GOTO(out_unlock_child, rc);
1091         /* save version of file name for replay, it must be ENOENT here */
1092         if (!req_is_replay(mdt_info_req(info))) {
1093                 if (rc != -ENOENT) {
1094                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1095                                PNAME(&rr->rr_name));
1096                         GOTO(out_unlock_child, rc = -EEXIST);
1097                 }
1098                 info->mti_ver[2] = ENOENT_VERSION;
1099                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1100         }
1101
1102         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1103               mdt_object_child(ms), &rr->rr_name, ma);
1104
1105         if (rc == 0)
1106                 mdt_counter_incr(req, LPROC_MDT_LINK);
1107
1108         EXIT;
1109 out_unlock_child:
1110         mdt_object_unlock_put(info, ms, lhs, rc);
1111 out_unlock_parent:
1112         mdt_object_unlock_put(info, mp, lhp, rc);
1113         return rc;
1114 }
1115 /**
1116  * lock the part of the directory according to the hash of the name
1117  * (lh->mlh_pdo_hash) in parallel directory lock.
1118  */
1119 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1120                               struct mdt_lock_handle *lh,
1121                               struct mdt_object *obj, __u64 ibits)
1122 {
1123         struct ldlm_res_id *res = &info->mti_res_id;
1124         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1125         union ldlm_policy_data *policy = &info->mti_policy;
1126         int rc;
1127
1128         /*
1129          * Finish res_id initializing by name hash marking part of
1130          * directory which is taking modification.
1131          */
1132         LASSERT(lh->mlh_pdo_hash != 0);
1133         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1134         memset(policy, 0, sizeof(*policy));
1135         policy->l_inodebits.bits = ibits;
1136         /*
1137          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1138          * going to be sent to client. If it is - mdt_intent_policy() path will
1139          * fix it up and turn FL_LOCAL flag off.
1140          */
1141         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1142                           res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1143                           &info->mti_exp->exp_handle.h_cookie);
1144         return rc;
1145 }
1146
1147 /**
1148  * Get BFL lock for rename or migrate process.
1149  **/
1150 static int mdt_rename_lock(struct mdt_thread_info *info,
1151                            struct lustre_handle *lh)
1152 {
1153         int     rc;
1154         ENTRY;
1155
1156         if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1157                 struct lu_fid *fid = &info->mti_tmp_fid1;
1158                 struct mdt_object *obj;
1159
1160                 /* XXX, right now, it has to use object API to
1161                  * enqueue lock cross MDT, so it will enqueue
1162                  * rename lock(with LUSTRE_BFL_FID) by root object */
1163                 lu_root_fid(fid);
1164                 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1165                 if (IS_ERR(obj))
1166                         RETURN(PTR_ERR(obj));
1167
1168                 rc = mdt_remote_object_lock(info, obj,
1169                                             &LUSTRE_BFL_FID, lh,
1170                                             LCK_EX,
1171                                             MDS_INODELOCK_UPDATE, false);
1172                 mdt_object_put(info->mti_env, obj);
1173         } else {
1174                 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1175                 union ldlm_policy_data *policy = &info->mti_policy;
1176                 struct ldlm_res_id *res_id = &info->mti_res_id;
1177                 __u64 flags = 0;
1178
1179                 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1180                 memset(policy, 0, sizeof *policy);
1181                 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1182                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1183                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1184                                            LCK_EX, &flags, ldlm_blocking_ast,
1185                                            ldlm_completion_ast, NULL, NULL, 0,
1186                                            LVB_T_NONE,
1187                                            &info->mti_exp->exp_handle.h_cookie,
1188                                            lh);
1189                 RETURN(rc);
1190         }
1191         RETURN(rc);
1192 }
1193
1194 static void mdt_rename_unlock(struct lustre_handle *lh)
1195 {
1196         ENTRY;
1197         LASSERT(lustre_handle_is_used(lh));
1198         /* Cancel the single rename lock right away */
1199         ldlm_lock_decref_and_cancel(lh, LCK_EX);
1200         EXIT;
1201 }
1202
1203 /*
1204  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1205  * target. Source should not be ancestor of target dir. May be other rename
1206  * checks can be moved here later.
1207  */
1208 static int mdt_is_subdir(struct mdt_thread_info *info,
1209                          struct mdt_object *dir,
1210                          const struct lu_fid *fid)
1211 {
1212         struct lu_fid dir_fid = dir->mot_header.loh_fid;
1213         int rc = 0;
1214         ENTRY;
1215
1216         /* If the source and target are in the same directory, they can not
1217          * be parent/child relationship, so subdir check is not needed */
1218         if (lu_fid_eq(&dir_fid, fid))
1219                 return 0;
1220
1221         if (!mdt_object_exists(dir))
1222                 RETURN(-ENOENT);
1223
1224         rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1225                            fid, &dir_fid);
1226         if (rc < 0) {
1227                 CERROR("%s: failed subdir check in "DFID" for "DFID
1228                        ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1229                        PFID(&dir_fid), PFID(fid), rc);
1230                 /* Return EINVAL only if a parent is the @fid */
1231                 if (rc == -EINVAL)
1232                         rc = -EIO;
1233         } else {
1234                 /* check the found fid */
1235                 if (lu_fid_eq(&dir_fid, fid))
1236                         rc = -EINVAL;
1237         }
1238
1239         RETURN(rc);
1240 }
1241
1242 /* Update object linkEA */
1243 struct mdt_lock_list {
1244         struct mdt_object       *mll_obj;
1245         struct mdt_lock_handle  mll_lh;
1246         struct list_head        mll_list;
1247 };
1248
1249 static void mdt_unlock_list(struct mdt_thread_info *info,
1250                             struct list_head *list, int rc)
1251 {
1252         struct mdt_lock_list *mll;
1253         struct mdt_lock_list *mll2;
1254
1255         list_for_each_entry_safe(mll, mll2, list, mll_list) {
1256                 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1257                 list_del(&mll->mll_list);
1258                 OBD_FREE_PTR(mll);
1259         }
1260 }
1261
1262 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1263                                       struct mdt_object *obj,
1264                                       struct mdt_object *pobj,
1265                                       struct list_head *lock_list)
1266 {
1267         struct lu_buf           *buf = &info->mti_big_buf;
1268         struct linkea_data      ldata = { NULL };
1269         int                     count;
1270         int                     rc;
1271         ENTRY;
1272
1273         if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1274                 RETURN(0);
1275
1276         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1277         if (buf->lb_buf == NULL)
1278                 RETURN(-ENOMEM);
1279
1280         ldata.ld_buf = buf;
1281         rc = mdt_links_read(info, obj, &ldata);
1282         if (rc != 0) {
1283                 if (rc == -ENOENT || rc == -ENODATA)
1284                         rc = 0;
1285                 RETURN(rc);
1286         }
1287
1288         LASSERT(ldata.ld_leh != NULL);
1289         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1290         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1291                 struct mdt_device *mdt = info->mti_mdt;
1292                 struct mdt_object *mdt_pobj;
1293                 struct mdt_lock_list *mll;
1294                 struct lu_name name;
1295                 struct lu_fid  fid;
1296
1297                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1298                                     &name, &fid);
1299                 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1300                 if (IS_ERR(mdt_pobj)) {
1301                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1302                               mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1303                         goto next;
1304                 }
1305
1306                 if (!mdt_object_exists(mdt_pobj)) {
1307                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1308                               mdt_obd_name(mdt), PFID(&fid));
1309                         mdt_object_put(info->mti_env, mdt_pobj);
1310                         goto next;
1311                 }
1312
1313                 /* Check if the object already exists in the list */
1314                 list_for_each_entry(mll, lock_list, mll_list) {
1315                         if (mll->mll_obj == mdt_pobj) {
1316                                 mdt_object_put(info->mti_env, mdt_pobj);
1317                                 goto next;
1318                         }
1319                 }
1320
1321                 if (mdt_pobj == pobj) {
1322                         CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1323                                mdt_obd_name(mdt), PFID(&fid));
1324                         mdt_object_put(info->mti_env, mdt_pobj);
1325                         goto next;
1326                 }
1327
1328                 OBD_ALLOC_PTR(mll);
1329                 if (mll == NULL) {
1330                         mdt_object_put(info->mti_env, mdt_pobj);
1331                         GOTO(out, rc = -ENOMEM);
1332                 }
1333
1334                 /* Since this needs to lock all of objects in linkea, to avoid
1335                  * deadlocks, because it does not follow parent-child order as
1336                  * other MDT operation, let's use try_lock here, i.e. it will
1337                  * return immediately once there are conflict locks, and return
1338                  * EBUSY to client */
1339                 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1340                 rc = mdt_object_lock_try(info, mdt_pobj, &mll->mll_lh,
1341                                          MDS_INODELOCK_UPDATE);
1342                 if (rc == 0) {
1343                         CDEBUG(D_ERROR, "%s: cannot lock "DFID": rc =%d\n",
1344                                mdt_obd_name(mdt), PFID(&fid), rc);
1345                         mdt_object_put(info->mti_env, mdt_pobj);
1346                         OBD_FREE_PTR(mll);
1347                         GOTO(out, rc = -EBUSY);
1348                 }
1349                 rc = 0;
1350                 INIT_LIST_HEAD(&mll->mll_list);
1351                 mll->mll_obj = mdt_pobj;
1352                 list_add_tail(&mll->mll_list, lock_list);
1353 next:
1354                 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1355                                                          ldata.ld_reclen);
1356
1357         }
1358 out:
1359         if (rc != 0)
1360                 mdt_unlock_list(info, lock_list, rc);
1361         RETURN(rc);
1362 }
1363
1364 /* migrate files from one MDT to another MDT */
1365 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1366                                       struct mdt_lock_handle *lhc)
1367 {
1368         struct mdt_reint_record *rr = &info->mti_rr;
1369         struct md_attr          *ma = &info->mti_attr;
1370         struct mdt_object       *msrcdir;
1371         struct mdt_object       *mold;
1372         struct mdt_object       *mnew = NULL;
1373         struct mdt_lock_handle  *lh_dirp;
1374         struct mdt_lock_handle  *lh_childp;
1375         struct mdt_lock_handle  *lh_tgtp = NULL;
1376         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1377         struct list_head        lock_list;
1378         __u64                   lock_ibits;
1379         struct ldlm_lock        *lease = NULL;
1380         bool                    lock_open_sem = false;
1381         int                     rc;
1382         ENTRY;
1383
1384         CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1385                PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1386
1387         /* 1: lock the source dir. */
1388         msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1389         if (IS_ERR(msrcdir)) {
1390                 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1391                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1392                         (int)PTR_ERR(msrcdir));
1393                 RETURN(PTR_ERR(msrcdir));
1394         }
1395
1396         lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1397         mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1398         rc = mdt_object_lock(info, msrcdir, lh_dirp,
1399                              MDS_INODELOCK_UPDATE);
1400         if (rc)
1401                 GOTO(out_put_parent, rc);
1402
1403         if (!mdt_object_remote(msrcdir)) {
1404                 rc = mdt_version_get_check_save(info, msrcdir, 0);
1405                 if (rc)
1406                         GOTO(out_unlock_parent, rc);
1407         }
1408
1409         /* 2: sanity check and find the object to be migrated. */
1410         fid_zero(old_fid);
1411         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1412         if (rc != 0)
1413                 GOTO(out_unlock_parent, rc);
1414
1415         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1416                 GOTO(out_unlock_parent, rc = -EINVAL);
1417
1418         if (!fid_is_md_operative(old_fid))
1419                 GOTO(out_unlock_parent, rc = -EPERM);
1420
1421         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1422         if (IS_ERR(mold))
1423                 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1424
1425         if (mdt_object_remote(mold)) {
1426                 CERROR("%s: source "DFID" is on the remote MDT\n",
1427                        mdt_obd_name(info->mti_mdt), PFID(old_fid));
1428                 GOTO(out_put_child, rc = -EREMOTE);
1429         }
1430
1431         if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1432             !mdt_object_remote(msrcdir)) {
1433                 CERROR("%s: parent "DFID" is still on the same"
1434                        " MDT, which should be migrated first:"
1435                        " rc = %d\n", mdt_obd_name(info->mti_mdt),
1436                        PFID(mdt_object_fid(msrcdir)), -EPERM);
1437                 GOTO(out_put_child, rc = -EPERM);
1438         }
1439
1440         rc = mdt_remote_permission(info);
1441         if (rc != 0)
1442                 GOTO(out_put_child, rc);
1443
1444         /* 3: iterate the linkea of the object and lock all of the objects */
1445         INIT_LIST_HEAD(&lock_list);
1446         rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1447         if (rc != 0)
1448                 GOTO(out_put_child, rc);
1449
1450         if (info->mti_spec.sp_migrate_close) {
1451                 struct close_data *data;
1452                 struct mdt_body  *repbody;
1453                 bool lease_broken = false;
1454
1455                 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
1456                                       RCL_CLIENT) ||
1457                     !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
1458                                       RCL_CLIENT))
1459                         GOTO(out_lease, rc = -EPROTO);
1460
1461                 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1462                 if (data == NULL)
1463                         GOTO(out_lease, rc = -EPROTO);
1464
1465                 lease = ldlm_handle2lock(&data->cd_handle);
1466                 if (lease == NULL)
1467                         GOTO(out_lease, rc = -ESTALE);
1468
1469                 /* try to hold open_sem so that nobody else can open the file */
1470                 if (!down_write_trylock(&mold->mot_open_sem)) {
1471                         ldlm_lock_cancel(lease);
1472                         GOTO(out_lease, rc = -EBUSY);
1473                 }
1474
1475                 lock_open_sem = true;
1476                 /* Check if the lease open lease has already canceled */
1477                 lock_res_and_lock(lease);
1478                 lease_broken = ldlm_is_cancel(lease);
1479                 unlock_res_and_lock(lease);
1480
1481                 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
1482                            PFID(mdt_object_fid(mold)), lease_broken);
1483
1484                 /* Cancel server side lease. Client side counterpart should
1485                  * have been cancelled. It's okay to cancel it now as we've
1486                  * held mot_open_sem. */
1487                 ldlm_lock_cancel(lease);
1488
1489                 if (lease_broken)
1490                         GOTO(out_lease, rc = -EAGAIN);
1491 out_lease:
1492                 rc = mdt_close_internal(info, mdt_info_req(info), NULL);
1493                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1494                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1495                 if (rc != 0)
1496                         GOTO(out_unlock_list, rc);
1497         }
1498
1499         /* 4: lock of the object migrated object */
1500         lh_childp = &info->mti_lh[MDT_LH_OLD];
1501         mdt_lock_reg_init(lh_childp, LCK_EX);
1502         lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1503                      MDS_INODELOCK_LAYOUT;
1504         if (mdt_object_remote(msrcdir)) {
1505                 /* Enqueue lookup lock from the parent MDT */
1506                 rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
1507                                             &lh_childp->mlh_rreg_lh,
1508                                             lh_childp->mlh_rreg_mode,
1509                                             MDS_INODELOCK_LOOKUP, false);
1510                 if (rc != ELDLM_OK)
1511                         GOTO(out_unlock_list, rc);
1512
1513                 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1514         }
1515
1516         rc = mdt_object_lock(info, mold, lh_childp, lock_ibits);
1517         if (rc != 0)
1518                 GOTO(out_unlock_child, rc);
1519
1520         ma->ma_need = MA_LMV;
1521         ma->ma_valid = 0;
1522         ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1523         ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1524         rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1525         if (rc != 0)
1526                 GOTO(out_unlock_child, rc);
1527
1528         if ((ma->ma_valid & MA_LMV)) {
1529                 struct lmv_mds_md_v1 *lmm1;
1530
1531                 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1532                 lmm1 = &ma->ma_lmv->lmv_md_v1;
1533                 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1534                         CERROR("%s: can not migrate striped dir "DFID
1535                                ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1536                                PFID(mdt_object_fid(mold)), -EPERM);
1537                         GOTO(out_unlock_child, rc = -EPERM);
1538                 }
1539
1540                 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1541                         GOTO(out_unlock_child, rc = -EINVAL);
1542
1543                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1544                                        &lmm1->lmv_stripe_fids[1]);
1545                 if (IS_ERR(mnew))
1546                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1547
1548                 if (!mdt_object_remote(mnew)) {
1549                         CERROR("%s: "DFID" being migrated is on this MDT:"
1550                                " rc  = %d\n", mdt_obd_name(info->mti_mdt),
1551                                PFID(rr->rr_fid2), -EPERM);
1552                         GOTO(out_put_new, rc = -EPERM);
1553                 }
1554
1555                 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1556                 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1557                 rc = mdt_remote_object_lock(info, mnew,
1558                                             mdt_object_fid(mnew),
1559                                             &lh_tgtp->mlh_rreg_lh,
1560                                             lh_tgtp->mlh_rreg_mode,
1561                                             MDS_INODELOCK_UPDATE, false);
1562                 if (rc != 0) {
1563                         lh_tgtp = NULL;
1564                         GOTO(out_put_new, rc);
1565                 }
1566         } else {
1567                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1568                                        rr->rr_fid2);
1569                 if (IS_ERR(mnew))
1570                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1571                 if (!mdt_object_remote(mnew)) {
1572                         CERROR("%s: Migration "DFID" is on this MDT:"
1573                                " rc = %d\n", mdt_obd_name(info->mti_mdt),
1574                                PFID(rr->rr_fid2), -EXDEV);
1575                         GOTO(out_put_new, rc = -EXDEV);
1576                 }
1577         }
1578
1579         /* 5: migrate it */
1580         mdt_reint_init_ma(info, ma);
1581
1582         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1583                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1584
1585         rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1586                          mdt_object_child(mold), &rr->rr_name,
1587                          mdt_object_child(mnew), ma);
1588         if (rc != 0)
1589                 GOTO(out_unlock_new, rc);
1590
1591 out_unlock_new:
1592         if (lh_tgtp != NULL)
1593                 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1594 out_put_new:
1595         if (mnew)
1596                 mdt_object_put(info->mti_env, mnew);
1597 out_unlock_child:
1598         mdt_object_unlock(info, mold, lh_childp, rc);
1599 out_unlock_list:
1600         mdt_unlock_list(info, &lock_list, rc);
1601         if (lease != NULL) {
1602                 ldlm_reprocess_all(lease->l_resource);
1603                 LDLM_LOCK_PUT(lease);
1604         }
1605
1606         if (lock_open_sem)
1607                 up_write(&mold->mot_open_sem);
1608 out_put_child:
1609         mdt_object_put(info->mti_env, mold);
1610 out_unlock_parent:
1611         mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1612 out_put_parent:
1613         mdt_object_put(info->mti_env, msrcdir);
1614
1615         RETURN(rc);
1616 }
1617
1618 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1619                                                 const struct lu_fid *fid,
1620                                                 int idx)
1621 {
1622         struct mdt_object *dir;
1623         int rc;
1624         ENTRY;
1625
1626         dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1627         if (IS_ERR(dir))
1628                 RETURN(dir);
1629
1630         /* check early, the real version will be saved after locking */
1631         rc = mdt_version_get_check(info, dir, idx);
1632         if (rc)
1633                 GOTO(out_put, rc);
1634
1635         RETURN(dir);
1636 out_put:
1637         mdt_object_put(info->mti_env, dir);
1638         return ERR_PTR(rc);
1639 }
1640
1641 static int mdt_object_lock_save(struct mdt_thread_info *info,
1642                                 struct mdt_object *dir,
1643                                 struct mdt_lock_handle *lh,
1644                                 int idx)
1645 {
1646         int rc;
1647
1648         /* we lock the target dir if it is local */
1649         rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE);
1650         if (rc != 0)
1651                 return rc;
1652
1653         /* get and save correct version after locking */
1654         mdt_version_get_save(info, dir, idx);
1655         return 0;
1656 }
1657
1658
1659 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1660                                    struct mdt_object **srcp,
1661                                    struct mdt_object **tgtp)
1662 {
1663         struct mdt_reint_record *rr = &info->mti_rr;
1664         const struct lu_fid     *fid_src = rr->rr_fid1;
1665         const struct lu_fid     *fid_tgt = rr->rr_fid2;
1666         struct mdt_lock_handle  *lh_src = &info->mti_lh[MDT_LH_PARENT];
1667         struct mdt_lock_handle  *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1668         struct mdt_object       *src;
1669         struct mdt_object       *tgt;
1670         int                      reverse = 0;
1671         int                      rc;
1672         ENTRY;
1673
1674         /* find both parents. */
1675         src = mdt_object_find_check(info, fid_src, 0);
1676         if (IS_ERR(src))
1677                 RETURN(PTR_ERR(src));
1678
1679         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1680
1681         if (lu_fid_eq(fid_src, fid_tgt)) {
1682                 tgt = src;
1683                 mdt_object_get(info->mti_env, tgt);
1684         } else {
1685                 /* Check if the @src is not a child of the @tgt, otherwise a
1686                  * reverse locking must take place. */
1687                 rc = mdt_is_subdir(info, src, fid_tgt);
1688                 if (rc == -EINVAL)
1689                         reverse = 1;
1690                 else if (rc)
1691                         GOTO(err_src_put, rc);
1692
1693                 tgt = mdt_object_find_check(info, fid_tgt, 1);
1694                 if (IS_ERR(tgt))
1695                         GOTO(err_src_put, rc = PTR_ERR(tgt));
1696         }
1697
1698         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1699
1700         /* lock parents in the proper order. */
1701         if (reverse) {
1702                 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1703                 if (rc)
1704                         GOTO(err_tgt_put, rc);
1705
1706                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1707
1708                 rc = mdt_object_lock_save(info, src, lh_src, 0);
1709         } else {
1710                 rc = mdt_object_lock_save(info, src, lh_src, 0);
1711                 if (rc)
1712                         GOTO(err_tgt_put, rc);
1713
1714                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1715
1716                 if (tgt != src)
1717                         rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1718                 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1719                         rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1720                                                 MDS_INODELOCK_UPDATE);
1721                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1722                 }
1723         }
1724         if (rc)
1725                 GOTO(err_unlock, rc);
1726
1727         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1728
1729         *srcp = src;
1730         *tgtp = tgt;
1731         RETURN(0);
1732
1733 err_unlock:
1734         /* The order does not matter as the handle is checked inside,
1735          * as well as not used handle. */
1736         mdt_object_unlock(info, src, lh_src, rc);
1737         mdt_object_unlock(info, tgt, lh_tgt, rc);
1738 err_tgt_put:
1739         mdt_object_put(info->mti_env, tgt);
1740 err_src_put:
1741         mdt_object_put(info->mti_env, src);
1742         RETURN(rc);
1743 }
1744
1745 /*
1746  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1747  * 2 - src child; 3 - tgt child.
1748  * Update on disk version of src child.
1749  */
1750 /**
1751  * For DNE phase I, only these renames are allowed
1752  *      mv src_p/src_c tgt_p/tgt_c
1753  * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1754  * 2. src_p and tgt_p are same directory, and tgt_c does not
1755  *    exists. In this case, all of modification will happen
1756  *    in the MDT where ithesource parent is, only one remote
1757  *    update is needed, i.e. set c_time/m_time on the child.
1758  *    And tgt_c will be still in the same MDT as the original
1759  *    src_c.
1760  */
1761 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1762                                      struct mdt_lock_handle *lhc)
1763 {
1764         struct mdt_reint_record *rr = &info->mti_rr;
1765         struct md_attr          *ma = &info->mti_attr;
1766         struct ptlrpc_request   *req = mdt_info_req(info);
1767         struct mdt_object       *msrcdir = NULL;
1768         struct mdt_object       *mtgtdir = NULL;
1769         struct mdt_object       *mold;
1770         struct mdt_object       *mnew = NULL;
1771         struct mdt_lock_handle  *lh_srcdirp;
1772         struct mdt_lock_handle  *lh_tgtdirp;
1773         struct mdt_lock_handle  *lh_oldp = NULL;
1774         struct mdt_lock_handle  *lh_newp = NULL;
1775         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1776         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1777         __u64                   lock_ibits;
1778         int                      rc;
1779         ENTRY;
1780
1781         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1782                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1783                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1784
1785         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1786         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1787         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1788         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1789
1790         /* step 1&2: lock the source and target dirs. */
1791         rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1792         if (rc)
1793                 RETURN(rc);
1794
1795         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1796
1797         /* step 3: find & lock the old object. */
1798         fid_zero(old_fid);
1799         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1800         if (rc != 0)
1801                 GOTO(out_unlock_parents, rc);
1802
1803         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1804                 GOTO(out_unlock_parents, rc = -EINVAL);
1805
1806         if (!fid_is_md_operative(old_fid))
1807                 GOTO(out_unlock_parents, rc = -EPERM);
1808
1809         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1810         if (IS_ERR(mold))
1811                 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1812
1813         /* Check if @mtgtdir is subdir of @mold, before locking child
1814          * to avoid reverse locking. */
1815         rc = mdt_is_subdir(info, mtgtdir, old_fid);
1816         if (rc)
1817                 GOTO(out_put_old, rc);
1818
1819         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1820         /* save version after locking */
1821         mdt_version_get_save(info, mold, 2);
1822
1823         /* step 4: find & lock the new object. */
1824         /* new target object may not exist now */
1825         /* lookup with version checking */
1826         fid_zero(new_fid);
1827         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1828                                       3);
1829         if (rc == 0) {
1830                 /* the new_fid should have been filled at this moment */
1831                 if (lu_fid_eq(old_fid, new_fid))
1832                         GOTO(out_put_old, rc);
1833
1834                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1835                     lu_fid_eq(new_fid, rr->rr_fid2))
1836                         GOTO(out_put_old, rc = -EINVAL);
1837
1838                 if (!fid_is_md_operative(new_fid))
1839                         GOTO(out_put_old, rc = -EPERM);
1840
1841                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1842                 if (IS_ERR(mnew))
1843                         GOTO(out_put_old, rc = PTR_ERR(mnew));
1844
1845                 if (mdt_object_remote(mnew)) {
1846                         struct mdt_body  *repbody;
1847
1848                         /* Always send rename req to the target child MDT */
1849                         repbody = req_capsule_server_get(info->mti_pill,
1850                                                          &RMF_MDT_BODY);
1851                         LASSERT(repbody != NULL);
1852                         repbody->mbo_fid1 = *new_fid;
1853                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1854                         GOTO(out_put_old, rc = -EXDEV);
1855                 }
1856                 /* Before locking the target dir, check we do not replace
1857                  * a dir with a non-dir, otherwise it may deadlock with
1858                  * link op which tries to create a link in this dir
1859                  * back to this non-dir. */
1860                 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1861                     !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1862                         GOTO(out_put_new, rc = -EISDIR);
1863
1864                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1865                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1866
1867                 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
1868                 if (mdt_object_remote(msrcdir)) {
1869                         /* Enqueue lookup lock from the parent MDT */
1870                         rc = mdt_remote_object_lock(info, msrcdir,
1871                                                     mdt_object_fid(mold),
1872                                                     &lh_oldp->mlh_rreg_lh,
1873                                                     lh_oldp->mlh_rreg_mode,
1874                                                     MDS_INODELOCK_LOOKUP,
1875                                                     false);
1876                         if (rc != ELDLM_OK)
1877                                 GOTO(out_put_new, rc);
1878
1879                         lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1880                 }
1881
1882                 rc = mdt_object_lock(info, mold, lh_oldp, lock_ibits);
1883                 if (rc != 0)
1884                         GOTO(out_unlock_old, rc);
1885
1886                 /* Check if @msrcdir is subdir of @mnew, before locking child
1887                  * to avoid reverse locking. */
1888                 rc = mdt_is_subdir(info, msrcdir, new_fid);
1889                 if (rc)
1890                         GOTO(out_unlock_old, rc);
1891
1892                 /* We used to acquire MDS_INODELOCK_FULL here but we
1893                  * can't do this now because a running HSM restore on
1894                  * the rename onto victim will hold the layout
1895                  * lock. See LU-4002. */
1896
1897                 lh_newp = &info->mti_lh[MDT_LH_NEW];
1898                 mdt_lock_reg_init(lh_newp, LCK_EX);
1899                 rc = mdt_object_lock(info, mnew, lh_newp,
1900                                      MDS_INODELOCK_LOOKUP |
1901                                      MDS_INODELOCK_UPDATE);
1902                 if (rc != 0)
1903                         GOTO(out_unlock_old, rc);
1904
1905                 /* get and save version after locking */
1906                 mdt_version_get_save(info, mnew, 3);
1907         } else if (rc != -EREMOTE && rc != -ENOENT) {
1908                 GOTO(out_put_old, rc);
1909         } else {
1910                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1911                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1912
1913                 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
1914                 if (mdt_object_remote(msrcdir)) {
1915                         /* Enqueue lookup lock from the parent MDT */
1916                         rc = mdt_remote_object_lock(info, msrcdir,
1917                                                     mdt_object_fid(mold),
1918                                                     &lh_oldp->mlh_rreg_lh,
1919                                                     lh_oldp->mlh_rreg_mode,
1920                                                     MDS_INODELOCK_LOOKUP,
1921                                                     false);
1922                         if (rc != ELDLM_OK)
1923                                 GOTO(out_put_new, rc);
1924
1925                         lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1926                 }
1927
1928                 rc = mdt_object_lock(info, mold, lh_oldp, lock_ibits);
1929                 if (rc != 0)
1930                         GOTO(out_put_old, rc);
1931
1932                 mdt_enoent_version_save(info, 3);
1933         }
1934
1935         /* step 5: rename it */
1936         mdt_reint_init_ma(info, ma);
1937
1938         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1939                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1940
1941         if (mnew != NULL)
1942                 mutex_lock(&mnew->mot_lov_mutex);
1943
1944         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1945                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1946                         mnew != NULL ? mdt_object_child(mnew) : NULL,
1947                         &rr->rr_tgt_name, ma);
1948
1949         if (mnew != NULL)
1950                 mutex_unlock(&mnew->mot_lov_mutex);
1951
1952         /* handle last link of tgt object */
1953         if (rc == 0) {
1954                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1955                 if (mnew)
1956                         mdt_handle_last_unlink(info, mnew, ma);
1957
1958                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1959                                          msrcdir, mtgtdir);
1960         }
1961
1962         EXIT;
1963         if (mnew != NULL)
1964                 mdt_object_unlock(info, mnew, lh_newp, rc);
1965 out_unlock_old:
1966         mdt_object_unlock(info, mold, lh_oldp, rc);
1967 out_put_new:
1968         if (mnew != NULL)
1969                 mdt_object_put(info->mti_env, mnew);
1970 out_put_old:
1971         mdt_object_put(info->mti_env, mold);
1972 out_unlock_parents:
1973         mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1974         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1975         return rc;
1976 }
1977
1978 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1979                                        struct mdt_lock_handle *lhc, bool rename)
1980 {
1981         struct mdt_reint_record *rr = &info->mti_rr;
1982         struct ptlrpc_request   *req = mdt_info_req(info);
1983         struct lustre_handle    rename_lh = { 0 };
1984         int                     rc;
1985         ENTRY;
1986
1987         if (info->mti_dlm_req)
1988                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1989
1990         if (!fid_is_md_operative(rr->rr_fid1) ||
1991             !fid_is_md_operative(rr->rr_fid2))
1992                 RETURN(-EPERM);
1993
1994         /* Note: do not enqueue rename lock for replay request, because
1995          * if other MDT holds rename lock, but being blocked to wait for
1996          * this MDT to finish its recovery, and the failover MDT can not
1997          * get rename lock, which will cause deadlock. */
1998         if (!req_is_replay(req)) {
1999                 rc = mdt_rename_lock(info, &rename_lh);
2000                 if (rc != 0) {
2001                         CERROR("%s: can't lock FS for rename: rc  = %d\n",
2002                                mdt_obd_name(info->mti_mdt), rc);
2003                         RETURN(rc);
2004                 }
2005         }
2006
2007         if (rename)
2008                 rc = mdt_reint_rename_internal(info, lhc);
2009         else
2010                 rc = mdt_reint_migrate_internal(info, lhc);
2011
2012         if (lustre_handle_is_used(&rename_lh))
2013                 mdt_rename_unlock(&rename_lh);
2014
2015         RETURN(rc);
2016 }
2017
2018 static int mdt_reint_rename(struct mdt_thread_info *info,
2019                             struct mdt_lock_handle *lhc)
2020 {
2021         return mdt_reint_rename_or_migrate(info, lhc, true);
2022 }
2023
2024 static int mdt_reint_migrate(struct mdt_thread_info *info,
2025                             struct mdt_lock_handle *lhc)
2026 {
2027         return mdt_reint_rename_or_migrate(info, lhc, false);
2028 }
2029
2030 struct mdt_reinter {
2031         int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
2032         enum lprocfs_extra_opc mr_extra_opc;
2033 };
2034
2035 static const struct mdt_reinter mdt_reinters[] = {
2036         [REINT_SETATTR] = {
2037                 .mr_handler = &mdt_reint_setattr,
2038                 .mr_extra_opc = MDS_REINT_SETATTR,
2039         },
2040         [REINT_CREATE] = {
2041                 .mr_handler = &mdt_reint_create,
2042                 .mr_extra_opc = MDS_REINT_CREATE,
2043         },
2044         [REINT_LINK] = {
2045                 .mr_handler = &mdt_reint_link,
2046                 .mr_extra_opc = MDS_REINT_LINK,
2047         },
2048         [REINT_UNLINK] = {
2049                 .mr_handler = &mdt_reint_unlink,
2050                 .mr_extra_opc = MDS_REINT_UNLINK,
2051         },
2052         [REINT_RENAME] = {
2053                 .mr_handler = &mdt_reint_rename,
2054                 .mr_extra_opc = MDS_REINT_RENAME,
2055         },
2056         [REINT_OPEN] = {
2057                 .mr_handler = &mdt_reint_open,
2058                 .mr_extra_opc = MDS_REINT_OPEN,
2059         },
2060         [REINT_SETXATTR] = {
2061                 .mr_handler = &mdt_reint_setxattr,
2062                 .mr_extra_opc = MDS_REINT_SETXATTR,
2063         },
2064         [REINT_RMENTRY] = {
2065                 .mr_handler = &mdt_reint_unlink,
2066                 .mr_extra_opc = MDS_REINT_UNLINK,
2067         },
2068         [REINT_MIGRATE] = {
2069                 .mr_handler = &mdt_reint_migrate,
2070                 .mr_extra_opc = MDS_REINT_RENAME,
2071         },
2072 };
2073
2074 int mdt_reint_rec(struct mdt_thread_info *info,
2075                   struct mdt_lock_handle *lhc)
2076 {
2077         const struct mdt_reinter *mr;
2078         int rc;
2079         ENTRY;
2080
2081         if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2082                 RETURN(-EPROTO);
2083
2084         mr = &mdt_reinters[info->mti_rr.rr_opcode];
2085         if (mr->mr_handler == NULL)
2086                 RETURN(-EPROTO);
2087
2088         rc = (*mr->mr_handler)(info, lhc);
2089
2090         lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2091                              PTLRPC_LAST_CNTR + mr->mr_extra_opc);
2092
2093         RETURN(rc);
2094 }