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