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