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