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